in6_ifattach.c revision 1.55 1 1.55 itojun /* $NetBSD: in6_ifattach.c,v 1.55 2003/07/08 10:20:45 itojun Exp $ */
2 1.37 itojun /* $KAME: in6_ifattach.c,v 1.124 2001/07/18 08:32:51 jinmei Exp $ */
3 1.3 thorpej
4 1.2 itojun /*
5 1.2 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.2 itojun * All rights reserved.
7 1.24 itojun *
8 1.2 itojun * Redistribution and use in source and binary forms, with or without
9 1.2 itojun * modification, are permitted provided that the following conditions
10 1.2 itojun * are met:
11 1.2 itojun * 1. Redistributions of source code must retain the above copyright
12 1.2 itojun * notice, this list of conditions and the following disclaimer.
13 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.2 itojun * notice, this list of conditions and the following disclaimer in the
15 1.2 itojun * documentation and/or other materials provided with the distribution.
16 1.2 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.2 itojun * may be used to endorse or promote products derived from this software
18 1.2 itojun * without specific prior written permission.
19 1.24 itojun *
20 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.2 itojun * SUCH DAMAGE.
31 1.2 itojun */
32 1.39 lukem
33 1.39 lukem #include <sys/cdefs.h>
34 1.55 itojun __KERNEL_RCSID(0, "$NetBSD: in6_ifattach.c,v 1.55 2003/07/08 10:20:45 itojun Exp $");
35 1.2 itojun
36 1.2 itojun #include <sys/param.h>
37 1.2 itojun #include <sys/systm.h>
38 1.2 itojun #include <sys/malloc.h>
39 1.2 itojun #include <sys/socket.h>
40 1.2 itojun #include <sys/sockio.h>
41 1.13 itojun #include <sys/kernel.h>
42 1.34 itojun #include <sys/syslog.h>
43 1.13 itojun #include <sys/md5.h>
44 1.2 itojun
45 1.2 itojun #include <net/if.h>
46 1.2 itojun #include <net/if_dl.h>
47 1.2 itojun #include <net/if_types.h>
48 1.2 itojun #include <net/route.h>
49 1.2 itojun
50 1.2 itojun #include <netinet/in.h>
51 1.2 itojun #include <netinet/in_var.h>
52 1.2 itojun
53 1.19 itojun #include <netinet/ip6.h>
54 1.2 itojun #include <netinet6/ip6_var.h>
55 1.2 itojun #include <netinet6/in6_ifattach.h>
56 1.2 itojun #include <netinet6/ip6_var.h>
57 1.2 itojun #include <netinet6/nd6.h>
58 1.55 itojun #include <netinet6/ip6_mroute.h>
59 1.2 itojun
60 1.13 itojun #include <net/net_osdep.h>
61 1.13 itojun
62 1.2 itojun unsigned long in6_maxmtu = 0;
63 1.2 itojun
64 1.48 itojun int ip6_auto_linklocal = 1; /* enable by default */
65 1.48 itojun
66 1.25 itojun static int get_rand_ifid __P((struct ifnet *, struct in6_addr *));
67 1.25 itojun static int get_hw_ifid __P((struct ifnet *, struct in6_addr *));
68 1.25 itojun static int get_ifid __P((struct ifnet *, struct ifnet *, struct in6_addr *));
69 1.25 itojun static int in6_ifattach_linklocal __P((struct ifnet *, struct ifnet *));
70 1.25 itojun static int in6_ifattach_loopback __P((struct ifnet *));
71 1.25 itojun
72 1.25 itojun #define EUI64_GBIT 0x01
73 1.25 itojun #define EUI64_UBIT 0x02
74 1.54 perry #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (/*CONSTCOND*/ 0)
75 1.25 itojun #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
76 1.25 itojun #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
77 1.25 itojun #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
78 1.25 itojun #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
79 1.25 itojun
80 1.25 itojun #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
81 1.25 itojun #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
82 1.25 itojun
83 1.25 itojun /*
84 1.25 itojun * Generate a last-resort interface identifier, when the machine has no
85 1.25 itojun * IEEE802/EUI64 address sources.
86 1.25 itojun * The goal here is to get an interface identifier that is
87 1.25 itojun * (1) random enough and (2) does not change across reboot.
88 1.25 itojun * We currently use MD5(hostname) for it.
89 1.25 itojun */
90 1.25 itojun static int
91 1.25 itojun get_rand_ifid(ifp, in6)
92 1.25 itojun struct ifnet *ifp;
93 1.40 itojun struct in6_addr *in6; /* upper 64bits are preserved */
94 1.25 itojun {
95 1.25 itojun MD5_CTX ctxt;
96 1.25 itojun u_int8_t digest[16];
97 1.25 itojun
98 1.25 itojun #if 0
99 1.25 itojun /* we need at least several letters as seed for ifid */
100 1.25 itojun if (hostnamelen < 3)
101 1.25 itojun return -1;
102 1.25 itojun #endif
103 1.25 itojun
104 1.25 itojun /* generate 8 bytes of pseudo-random value. */
105 1.25 itojun bzero(&ctxt, sizeof(ctxt));
106 1.25 itojun MD5Init(&ctxt);
107 1.50 itojun MD5Update(&ctxt, (u_char *)hostname, hostnamelen);
108 1.25 itojun MD5Final(digest, &ctxt);
109 1.25 itojun
110 1.25 itojun /* assumes sizeof(digest) > sizeof(ifid) */
111 1.25 itojun bcopy(digest, &in6->s6_addr[8], 8);
112 1.2 itojun
113 1.25 itojun /* make sure to set "u" bit to local, and "g" bit to individual. */
114 1.25 itojun in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
115 1.25 itojun in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
116 1.25 itojun
117 1.25 itojun /* convert EUI64 into IPv6 interface identifier */
118 1.25 itojun EUI64_TO_IFID(in6);
119 1.25 itojun
120 1.25 itojun return 0;
121 1.25 itojun }
122 1.2 itojun
123 1.25 itojun /*
124 1.25 itojun * Get interface identifier for the specified interface.
125 1.25 itojun * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
126 1.25 itojun */
127 1.7 itojun static int
128 1.25 itojun get_hw_ifid(ifp, in6)
129 1.25 itojun struct ifnet *ifp;
130 1.40 itojun struct in6_addr *in6; /* upper 64bits are preserved */
131 1.2 itojun {
132 1.25 itojun struct ifaddr *ifa;
133 1.25 itojun struct sockaddr_dl *sdl;
134 1.50 itojun char *addr;
135 1.25 itojun size_t addrlen;
136 1.25 itojun static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
137 1.25 itojun static u_int8_t allone[8] =
138 1.25 itojun { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
139 1.25 itojun
140 1.25 itojun for (ifa = ifp->if_addrlist.tqh_first;
141 1.25 itojun ifa;
142 1.25 itojun ifa = ifa->ifa_list.tqe_next)
143 1.25 itojun {
144 1.25 itojun if (ifa->ifa_addr->sa_family != AF_LINK)
145 1.25 itojun continue;
146 1.25 itojun sdl = (struct sockaddr_dl *)ifa->ifa_addr;
147 1.25 itojun if (sdl == NULL)
148 1.25 itojun continue;
149 1.25 itojun if (sdl->sdl_alen == 0)
150 1.25 itojun continue;
151 1.25 itojun
152 1.25 itojun goto found;
153 1.25 itojun }
154 1.25 itojun
155 1.25 itojun return -1;
156 1.25 itojun
157 1.25 itojun found:
158 1.25 itojun addr = LLADDR(sdl);
159 1.25 itojun addrlen = sdl->sdl_alen;
160 1.25 itojun
161 1.48 itojun switch (ifp->if_type) {
162 1.48 itojun case IFT_IEEE1394:
163 1.48 itojun case IFT_IEEE80211:
164 1.48 itojun /* IEEE1394 uses 16byte length address starting with EUI64 */
165 1.48 itojun if (addrlen > 8)
166 1.48 itojun addrlen = 8;
167 1.48 itojun break;
168 1.48 itojun default:
169 1.48 itojun break;
170 1.48 itojun }
171 1.48 itojun
172 1.25 itojun /* get EUI64 */
173 1.25 itojun switch (ifp->if_type) {
174 1.48 itojun /* IEEE802/EUI64 cases - what others? */
175 1.25 itojun case IFT_ETHER:
176 1.25 itojun case IFT_FDDI:
177 1.25 itojun case IFT_ATM:
178 1.32 onoe case IFT_IEEE1394:
179 1.48 itojun case IFT_IEEE80211:
180 1.25 itojun /* look at IEEE802/EUI64 only */
181 1.25 itojun if (addrlen != 8 && addrlen != 6)
182 1.25 itojun return -1;
183 1.13 itojun
184 1.25 itojun /*
185 1.25 itojun * check for invalid MAC address - on bsdi, we see it a lot
186 1.25 itojun * since wildboar configures all-zero MAC on pccard before
187 1.25 itojun * card insertion.
188 1.25 itojun */
189 1.25 itojun if (bcmp(addr, allzero, addrlen) == 0)
190 1.25 itojun return -1;
191 1.25 itojun if (bcmp(addr, allone, addrlen) == 0)
192 1.25 itojun return -1;
193 1.25 itojun
194 1.25 itojun /* make EUI64 address */
195 1.25 itojun if (addrlen == 8)
196 1.25 itojun bcopy(addr, &in6->s6_addr[8], 8);
197 1.25 itojun else if (addrlen == 6) {
198 1.25 itojun in6->s6_addr[8] = addr[0];
199 1.25 itojun in6->s6_addr[9] = addr[1];
200 1.25 itojun in6->s6_addr[10] = addr[2];
201 1.25 itojun in6->s6_addr[11] = 0xff;
202 1.26 itojun in6->s6_addr[12] = 0xfe;
203 1.25 itojun in6->s6_addr[13] = addr[3];
204 1.25 itojun in6->s6_addr[14] = addr[4];
205 1.25 itojun in6->s6_addr[15] = addr[5];
206 1.25 itojun }
207 1.7 itojun break;
208 1.25 itojun
209 1.25 itojun case IFT_ARCNET:
210 1.25 itojun if (addrlen != 1)
211 1.25 itojun return -1;
212 1.25 itojun if (!addr[0])
213 1.25 itojun return -1;
214 1.25 itojun
215 1.25 itojun bzero(&in6->s6_addr[8], 8);
216 1.25 itojun in6->s6_addr[15] = addr[0];
217 1.25 itojun
218 1.27 itojun /*
219 1.27 itojun * due to insufficient bitwidth, we mark it local.
220 1.27 itojun */
221 1.25 itojun in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
222 1.25 itojun in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
223 1.7 itojun break;
224 1.25 itojun
225 1.25 itojun case IFT_GIF:
226 1.25 itojun #ifdef IFT_STF
227 1.25 itojun case IFT_STF:
228 1.25 itojun #endif
229 1.25 itojun /*
230 1.34 itojun * RFC2893 says: "SHOULD use IPv4 address as ifid source".
231 1.27 itojun * however, IPv4 address is not very suitable as unique
232 1.27 itojun * identifier source (can be renumbered).
233 1.27 itojun * we don't do this.
234 1.25 itojun */
235 1.25 itojun return -1;
236 1.25 itojun
237 1.7 itojun default:
238 1.25 itojun return -1;
239 1.25 itojun }
240 1.25 itojun
241 1.25 itojun /* sanity check: g bit must not indicate "group" */
242 1.25 itojun if (EUI64_GROUP(in6))
243 1.25 itojun return -1;
244 1.25 itojun
245 1.25 itojun /* convert EUI64 into IPv6 interface identifier */
246 1.25 itojun EUI64_TO_IFID(in6);
247 1.25 itojun
248 1.25 itojun /*
249 1.25 itojun * sanity check: ifid must not be all zero, avoid conflict with
250 1.25 itojun * subnet router anycast
251 1.25 itojun */
252 1.25 itojun if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
253 1.25 itojun bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
254 1.25 itojun return -1;
255 1.7 itojun }
256 1.7 itojun
257 1.7 itojun return 0;
258 1.2 itojun }
259 1.2 itojun
260 1.2 itojun /*
261 1.25 itojun * Get interface identifier for the specified interface. If it is not
262 1.25 itojun * available on ifp0, borrow interface identifier from other information
263 1.25 itojun * sources.
264 1.13 itojun */
265 1.13 itojun static int
266 1.25 itojun get_ifid(ifp0, altifp, in6)
267 1.25 itojun struct ifnet *ifp0;
268 1.25 itojun struct ifnet *altifp; /*secondary EUI64 source*/
269 1.25 itojun struct in6_addr *in6;
270 1.13 itojun {
271 1.25 itojun struct ifnet *ifp;
272 1.25 itojun
273 1.25 itojun /* first, try to get it from the interface itself */
274 1.25 itojun if (get_hw_ifid(ifp0, in6) == 0) {
275 1.34 itojun nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
276 1.34 itojun if_name(ifp0)));
277 1.25 itojun goto success;
278 1.25 itojun }
279 1.25 itojun
280 1.25 itojun /* try secondary EUI64 source. this basically is for ATM PVC */
281 1.25 itojun if (altifp && get_hw_ifid(altifp, in6) == 0) {
282 1.34 itojun nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
283 1.34 itojun if_name(ifp0), if_name(altifp)));
284 1.25 itojun goto success;
285 1.25 itojun }
286 1.25 itojun
287 1.25 itojun /* next, try to get it from some other hardware interface */
288 1.25 itojun for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
289 1.25 itojun {
290 1.25 itojun if (ifp == ifp0)
291 1.25 itojun continue;
292 1.25 itojun if (get_hw_ifid(ifp, in6) != 0)
293 1.25 itojun continue;
294 1.27 itojun
295 1.25 itojun /*
296 1.25 itojun * to borrow ifid from other interface, ifid needs to be
297 1.25 itojun * globally unique
298 1.25 itojun */
299 1.25 itojun if (IFID_UNIVERSAL(in6)) {
300 1.34 itojun nd6log((LOG_DEBUG,
301 1.34 itojun "%s: borrow interface identifier from %s\n",
302 1.34 itojun if_name(ifp0), if_name(ifp)));
303 1.25 itojun goto success;
304 1.25 itojun }
305 1.25 itojun }
306 1.13 itojun
307 1.25 itojun /* last resort: get from random number source */
308 1.25 itojun if (get_rand_ifid(ifp, in6) == 0) {
309 1.34 itojun nd6log((LOG_DEBUG,
310 1.34 itojun "%s: interface identifier generated by random number\n",
311 1.34 itojun if_name(ifp0)));
312 1.25 itojun goto success;
313 1.25 itojun }
314 1.13 itojun
315 1.31 itojun printf("%s: failed to get interface identifier\n", if_name(ifp0));
316 1.25 itojun return -1;
317 1.13 itojun
318 1.25 itojun success:
319 1.47 itojun nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
320 1.47 itojun if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
321 1.47 itojun in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
322 1.47 itojun in6->s6_addr[14], in6->s6_addr[15]));
323 1.13 itojun return 0;
324 1.13 itojun }
325 1.13 itojun
326 1.25 itojun static int
327 1.48 itojun in6_ifattach_linklocal(ifp, altifp)
328 1.25 itojun struct ifnet *ifp;
329 1.48 itojun struct ifnet *altifp; /*secondary EUI64 source*/
330 1.48 itojun {
331 1.25 itojun struct in6_ifaddr *ia;
332 1.48 itojun struct in6_aliasreq ifra;
333 1.48 itojun struct nd_prefix pr0;
334 1.48 itojun int i, error;
335 1.25 itojun
336 1.25 itojun /*
337 1.48 itojun * configure link-local address.
338 1.25 itojun */
339 1.48 itojun bzero(&ifra, sizeof(ifra));
340 1.2 itojun
341 1.25 itojun /*
342 1.48 itojun * in6_update_ifa() does not use ifra_name, but we accurately set it
343 1.48 itojun * for safety.
344 1.25 itojun */
345 1.48 itojun strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
346 1.2 itojun
347 1.48 itojun ifra.ifra_addr.sin6_family = AF_INET6;
348 1.48 itojun ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
349 1.48 itojun ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
350 1.48 itojun ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
351 1.48 itojun ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
352 1.48 itojun if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
353 1.48 itojun ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
354 1.48 itojun ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
355 1.48 itojun } else {
356 1.48 itojun if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
357 1.48 itojun nd6log((LOG_ERR,
358 1.48 itojun "%s: no ifid available\n", if_name(ifp)));
359 1.51 itojun return (-1);
360 1.25 itojun }
361 1.25 itojun }
362 1.25 itojun
363 1.48 itojun ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
364 1.48 itojun ifra.ifra_prefixmask.sin6_family = AF_INET6;
365 1.48 itojun ifra.ifra_prefixmask.sin6_addr = in6mask64;
366 1.48 itojun #ifdef SCOPEDROUTING
367 1.48 itojun /* take into account the sin6_scope_id field for routing */
368 1.48 itojun ifra.ifra_prefixmask.sin6_scope_id = 0xffffffff;
369 1.25 itojun #endif
370 1.48 itojun /* link-local addresses should NEVER expire. */
371 1.48 itojun ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
372 1.48 itojun ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
373 1.25 itojun
374 1.48 itojun /*
375 1.48 itojun * Do not let in6_update_ifa() do DAD, since we need a random delay
376 1.48 itojun * before sending an NS at the first time the interface becomes up.
377 1.48 itojun * Instead, in6_if_up() will start DAD with a proper random delay.
378 1.48 itojun */
379 1.48 itojun ifra.ifra_flags |= IN6_IFF_NODAD;
380 1.25 itojun
381 1.48 itojun /*
382 1.48 itojun * Now call in6_update_ifa() to do a bunch of procedures to configure
383 1.48 itojun * a link-local address. We can set NULL to the 3rd argument, because
384 1.48 itojun * we know there's no other link-local address on the interface
385 1.48 itojun * and therefore we are adding one (instead of updating one).
386 1.48 itojun */
387 1.48 itojun if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
388 1.25 itojun /*
389 1.48 itojun * XXX: When the interface does not support IPv6, this call
390 1.48 itojun * would fail in the SIOCSIFADDR ioctl. I believe the
391 1.48 itojun * notification is rather confusing in this case, so just
392 1.48 itojun * suppress it. (jinmei (at) kame.net 20010130)
393 1.25 itojun */
394 1.48 itojun if (error != EAFNOSUPPORT)
395 1.49 itojun nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
396 1.48 itojun "configure a link-local address on %s "
397 1.48 itojun "(errno=%d)\n",
398 1.49 itojun if_name(ifp), error));
399 1.51 itojun return (-1);
400 1.25 itojun }
401 1.25 itojun
402 1.25 itojun /*
403 1.48 itojun * Adjust ia6_flags so that in6_if_up will perform DAD.
404 1.48 itojun * XXX: Some P2P interfaces seem not to send packets just after
405 1.48 itojun * becoming up, so we skip p2p interfaces for safety.
406 1.25 itojun */
407 1.48 itojun ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
408 1.48 itojun #ifdef DIAGNOSTIC
409 1.48 itojun if (!ia) {
410 1.48 itojun panic("ia == NULL in in6_ifattach_linklocal");
411 1.48 itojun /* NOTREACHED */
412 1.48 itojun }
413 1.48 itojun #endif
414 1.48 itojun if (in6if_do_dad(ifp) && (ifp->if_flags & IFF_POINTOPOINT) == 0) {
415 1.48 itojun ia->ia6_flags &= ~IN6_IFF_NODAD;
416 1.48 itojun ia->ia6_flags |= IN6_IFF_TENTATIVE;
417 1.25 itojun }
418 1.25 itojun
419 1.48 itojun /*
420 1.48 itojun * Make the link-local prefix (fe80::/64%link) as on-link.
421 1.48 itojun * Since we'd like to manage prefixes separately from addresses,
422 1.48 itojun * we make an ND6 prefix structure for the link-local prefix,
423 1.48 itojun * and add it to the prefix list as a never-expire prefix.
424 1.48 itojun * XXX: this change might affect some existing code base...
425 1.48 itojun */
426 1.48 itojun bzero(&pr0, sizeof(pr0));
427 1.48 itojun pr0.ndpr_ifp = ifp;
428 1.48 itojun /* this should be 64 at this moment. */
429 1.48 itojun pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
430 1.48 itojun pr0.ndpr_mask = ifra.ifra_prefixmask.sin6_addr;
431 1.48 itojun pr0.ndpr_prefix = ifra.ifra_addr;
432 1.48 itojun /* apply the mask for safety. (nd6_prelist_add will apply it again) */
433 1.48 itojun for (i = 0; i < 4; i++) {
434 1.48 itojun pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
435 1.48 itojun in6mask64.s6_addr32[i];
436 1.48 itojun }
437 1.48 itojun /*
438 1.48 itojun * Initialize parameters. The link-local prefix must always be
439 1.48 itojun * on-link, and its lifetimes never expire.
440 1.48 itojun */
441 1.48 itojun pr0.ndpr_raf_onlink = 1;
442 1.48 itojun pr0.ndpr_raf_auto = 1; /* probably meaningless */
443 1.48 itojun pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
444 1.48 itojun pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
445 1.48 itojun /*
446 1.48 itojun * Since there is no other link-local addresses, nd6_prefix_lookup()
447 1.48 itojun * probably returns NULL. However, we cannot always expect the result.
448 1.48 itojun * For example, if we first remove the (only) existing link-local
449 1.48 itojun * address, and then reconfigure another one, the prefix is still
450 1.48 itojun * valid with referring to the old link-local address.
451 1.48 itojun */
452 1.48 itojun if (nd6_prefix_lookup(&pr0) == NULL) {
453 1.48 itojun if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
454 1.51 itojun return (error);
455 1.25 itojun }
456 1.25 itojun
457 1.25 itojun return 0;
458 1.25 itojun }
459 1.25 itojun
460 1.25 itojun static int
461 1.25 itojun in6_ifattach_loopback(ifp)
462 1.25 itojun struct ifnet *ifp; /* must be IFT_LOOP */
463 1.25 itojun {
464 1.48 itojun struct in6_aliasreq ifra;
465 1.48 itojun int error;
466 1.48 itojun
467 1.48 itojun bzero(&ifra, sizeof(ifra));
468 1.25 itojun
469 1.25 itojun /*
470 1.48 itojun * in6_update_ifa() does not use ifra_name, but we accurately set it
471 1.48 itojun * for safety.
472 1.25 itojun */
473 1.48 itojun strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
474 1.48 itojun
475 1.48 itojun ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
476 1.48 itojun ifra.ifra_prefixmask.sin6_family = AF_INET6;
477 1.48 itojun ifra.ifra_prefixmask.sin6_addr = in6mask128;
478 1.25 itojun
479 1.25 itojun /*
480 1.25 itojun * Always initialize ia_dstaddr (= broadcast address) to loopback
481 1.48 itojun * address. Follows IPv4 practice - see in_ifinit().
482 1.48 itojun */
483 1.48 itojun ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
484 1.48 itojun ifra.ifra_dstaddr.sin6_family = AF_INET6;
485 1.48 itojun ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
486 1.48 itojun
487 1.48 itojun ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
488 1.48 itojun ifra.ifra_addr.sin6_family = AF_INET6;
489 1.48 itojun ifra.ifra_addr.sin6_addr = in6addr_loopback;
490 1.48 itojun
491 1.48 itojun /* the loopback address should NEVER expire. */
492 1.48 itojun ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
493 1.48 itojun ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
494 1.48 itojun
495 1.48 itojun /* we don't need to perform DAD on loopback interfaces. */
496 1.48 itojun ifra.ifra_flags |= IN6_IFF_NODAD;
497 1.48 itojun
498 1.48 itojun /*
499 1.48 itojun * We are sure that this is a newly assigned address, so we can set
500 1.48 itojun * NULL to the 3rd arg.
501 1.48 itojun */
502 1.48 itojun if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
503 1.49 itojun nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
504 1.48 itojun "the loopback address on %s (errno=%d)\n",
505 1.49 itojun if_name(ifp), error));
506 1.51 itojun return (-1);
507 1.48 itojun }
508 1.25 itojun
509 1.48 itojun return 0;
510 1.48 itojun }
511 1.48 itojun
512 1.48 itojun /*
513 1.48 itojun * compute NI group address, based on the current hostname setting.
514 1.48 itojun * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
515 1.48 itojun *
516 1.48 itojun * when ifp == NULL, the caller is responsible for filling scopeid.
517 1.48 itojun */
518 1.48 itojun int
519 1.48 itojun in6_nigroup(ifp, name, namelen, sa6)
520 1.48 itojun struct ifnet *ifp;
521 1.48 itojun const char *name;
522 1.48 itojun int namelen;
523 1.48 itojun struct sockaddr_in6 *sa6;
524 1.48 itojun {
525 1.48 itojun const char *p;
526 1.52 itojun u_int8_t *q;
527 1.48 itojun MD5_CTX ctxt;
528 1.48 itojun u_int8_t digest[16];
529 1.50 itojun u_int8_t l;
530 1.50 itojun u_int8_t n[64]; /* a single label must not exceed 63 chars */
531 1.25 itojun
532 1.48 itojun if (!namelen || !name)
533 1.25 itojun return -1;
534 1.48 itojun
535 1.48 itojun p = name;
536 1.48 itojun while (p && *p && *p != '.' && p - name < namelen)
537 1.48 itojun p++;
538 1.48 itojun if (p - name > sizeof(n) - 1)
539 1.48 itojun return -1; /* label too long */
540 1.48 itojun l = p - name;
541 1.50 itojun strncpy((char *)n, name, l);
542 1.48 itojun n[(int)l] = '\0';
543 1.48 itojun for (q = n; *q; q++) {
544 1.48 itojun if ('A' <= *q && *q <= 'Z')
545 1.48 itojun *q = *q - 'A' + 'a';
546 1.2 itojun }
547 1.25 itojun
548 1.48 itojun /* generate 8 bytes of pseudo-random value. */
549 1.48 itojun bzero(&ctxt, sizeof(ctxt));
550 1.48 itojun MD5Init(&ctxt);
551 1.48 itojun MD5Update(&ctxt, &l, sizeof(l));
552 1.48 itojun MD5Update(&ctxt, n, l);
553 1.48 itojun MD5Final(digest, &ctxt);
554 1.48 itojun
555 1.48 itojun bzero(sa6, sizeof(*sa6));
556 1.48 itojun sa6->sin6_family = AF_INET6;
557 1.48 itojun sa6->sin6_len = sizeof(*sa6);
558 1.48 itojun sa6->sin6_addr.s6_addr16[0] = htons(0xff02);
559 1.48 itojun sa6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
560 1.48 itojun sa6->sin6_addr.s6_addr8[11] = 2;
561 1.48 itojun bcopy(digest, &sa6->sin6_addr.s6_addr32[3],
562 1.48 itojun sizeof(sa6->sin6_addr.s6_addr32[3]));
563 1.48 itojun
564 1.25 itojun return 0;
565 1.2 itojun }
566 1.2 itojun
567 1.17 itojun /*
568 1.17 itojun * XXX multiple loopback interface needs more care. for instance,
569 1.17 itojun * nodelocal address needs to be configured onto only one of them.
570 1.25 itojun * XXX multiple link-local address case
571 1.17 itojun */
572 1.2 itojun void
573 1.25 itojun in6_ifattach(ifp, altifp)
574 1.2 itojun struct ifnet *ifp;
575 1.25 itojun struct ifnet *altifp; /* secondary EUI64 source */
576 1.2 itojun {
577 1.25 itojun struct in6_ifaddr *ia;
578 1.25 itojun struct in6_addr in6;
579 1.13 itojun
580 1.38 itojun /* some of the interfaces are inherently not IPv6 capable */
581 1.38 itojun switch (ifp->if_type) {
582 1.42 itojun case IFT_BRIDGE:
583 1.42 itojun return;
584 1.38 itojun }
585 1.38 itojun
586 1.46 itojun /*
587 1.46 itojun * if link mtu is too small, don't try to configure IPv6.
588 1.46 itojun * remember there could be some link-layer that has special
589 1.46 itojun * fragmentation logic.
590 1.46 itojun */
591 1.49 itojun if (ifp->if_mtu < IPV6_MMTU) {
592 1.49 itojun nd6log((LOG_INFO, "in6_ifattach: "
593 1.49 itojun "%s has too small MTU, IPv6 not enabled\n",
594 1.49 itojun if_name(ifp)));
595 1.46 itojun return;
596 1.49 itojun }
597 1.46 itojun
598 1.37 itojun /* create a multicast kludge storage (if we have not had one) */
599 1.37 itojun in6_createmkludge(ifp);
600 1.2 itojun
601 1.2 itojun /*
602 1.25 itojun * quirks based on interface type
603 1.2 itojun */
604 1.25 itojun switch (ifp->if_type) {
605 1.25 itojun #ifdef IFT_STF
606 1.25 itojun case IFT_STF:
607 1.25 itojun /*
608 1.38 itojun * 6to4 interface is a very special kind of beast.
609 1.38 itojun * no multicast, no linklocal. RFC2529 specifies how to make
610 1.38 itojun * linklocals for 6to4 interface, but there's no use and
611 1.38 itojun * it is rather harmful to have one.
612 1.25 itojun */
613 1.46 itojun return;
614 1.25 itojun #endif
615 1.25 itojun default:
616 1.25 itojun break;
617 1.7 itojun }
618 1.2 itojun
619 1.2 itojun /*
620 1.25 itojun * usually, we require multicast capability to the interface
621 1.2 itojun */
622 1.25 itojun if ((ifp->if_flags & IFF_MULTICAST) == 0) {
623 1.49 itojun nd6log((LOG_INFO, "in6_ifattach: "
624 1.38 itojun "%s is not multicast capable, IPv6 not enabled\n",
625 1.49 itojun if_name(ifp)));
626 1.25 itojun return;
627 1.25 itojun }
628 1.15 thorpej
629 1.2 itojun /*
630 1.48 itojun * assign loopback address for loopback interface.
631 1.48 itojun * XXX multiple loopback interface case.
632 1.2 itojun */
633 1.48 itojun if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
634 1.48 itojun in6 = in6addr_loopback;
635 1.25 itojun if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
636 1.25 itojun if (in6_ifattach_loopback(ifp) != 0)
637 1.25 itojun return;
638 1.25 itojun }
639 1.25 itojun }
640 1.2 itojun
641 1.2 itojun /*
642 1.48 itojun * assign a link-local address, if there's none.
643 1.2 itojun */
644 1.48 itojun if (ip6_auto_linklocal) {
645 1.48 itojun ia = in6ifa_ifpforlinklocal(ifp, 0);
646 1.48 itojun if (ia == NULL) {
647 1.48 itojun if (in6_ifattach_linklocal(ifp, altifp) == 0) {
648 1.48 itojun /* linklocal address assigned */
649 1.48 itojun } else {
650 1.48 itojun /* failed to assign linklocal address. bark? */
651 1.25 itojun }
652 1.2 itojun }
653 1.2 itojun }
654 1.2 itojun }
655 1.2 itojun
656 1.17 itojun /*
657 1.17 itojun * NOTE: in6_ifdetach() does not support loopback if at this moment.
658 1.41 itojun * We don't need this function in bsdi, because interfaces are never removed
659 1.41 itojun * from the ifnet list in bsdi.
660 1.17 itojun */
661 1.2 itojun void
662 1.2 itojun in6_ifdetach(ifp)
663 1.2 itojun struct ifnet *ifp;
664 1.2 itojun {
665 1.2 itojun struct in6_ifaddr *ia, *oia;
666 1.30 itojun struct ifaddr *ifa, *next;
667 1.2 itojun struct rtentry *rt;
668 1.2 itojun short rtflags;
669 1.16 itojun struct sockaddr_in6 sin6;
670 1.48 itojun struct in6_multi_mship *imm;
671 1.55 itojun
672 1.55 itojun /* remove ip6_mrouter stuff */
673 1.55 itojun ip6_mrouter_detach(ifp);
674 1.18 itojun
675 1.18 itojun /* remove neighbor management table */
676 1.18 itojun nd6_purge(ifp);
677 1.18 itojun
678 1.30 itojun /* nuke any of IPv6 addresses we have */
679 1.30 itojun for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
680 1.30 itojun {
681 1.30 itojun next = ifa->ifa_list.tqe_next;
682 1.30 itojun if (ifa->ifa_addr->sa_family != AF_INET6)
683 1.30 itojun continue;
684 1.48 itojun in6_purgeaddr(ifa);
685 1.30 itojun }
686 1.30 itojun
687 1.30 itojun /* undo everything done by in6_ifattach(), just in case */
688 1.30 itojun for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
689 1.13 itojun {
690 1.30 itojun next = ifa->ifa_list.tqe_next;
691 1.30 itojun
692 1.2 itojun if (ifa->ifa_addr->sa_family != AF_INET6
693 1.2 itojun || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
694 1.2 itojun continue;
695 1.2 itojun }
696 1.2 itojun
697 1.2 itojun ia = (struct in6_ifaddr *)ifa;
698 1.2 itojun
699 1.48 itojun /*
700 1.48 itojun * leave from multicast groups we have joined for the interface
701 1.48 itojun */
702 1.48 itojun while ((imm = ia->ia6_memberships.lh_first) != NULL) {
703 1.48 itojun LIST_REMOVE(imm, i6mm_chain);
704 1.48 itojun in6_leavegroup(imm);
705 1.48 itojun }
706 1.48 itojun
707 1.2 itojun /* remove from the routing table */
708 1.48 itojun if ((ia->ia_flags & IFA_ROUTE) &&
709 1.48 itojun (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
710 1.2 itojun rtflags = rt->rt_flags;
711 1.2 itojun rtfree(rt);
712 1.47 itojun rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
713 1.47 itojun (struct sockaddr *)&ia->ia_addr,
714 1.47 itojun (struct sockaddr *)&ia->ia_prefixmask,
715 1.47 itojun rtflags, (struct rtentry **)0);
716 1.2 itojun }
717 1.2 itojun
718 1.2 itojun /* remove from the linked list */
719 1.2 itojun TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
720 1.30 itojun IFAFREE(&ia->ia_ifa);
721 1.2 itojun
722 1.2 itojun /* also remove from the IPv6 address chain(itojun&jinmei) */
723 1.2 itojun oia = ia;
724 1.2 itojun if (oia == (ia = in6_ifaddr))
725 1.2 itojun in6_ifaddr = ia->ia_next;
726 1.2 itojun else {
727 1.2 itojun while (ia->ia_next && (ia->ia_next != oia))
728 1.2 itojun ia = ia->ia_next;
729 1.2 itojun if (ia->ia_next)
730 1.2 itojun ia->ia_next = oia->ia_next;
731 1.34 itojun else {
732 1.48 itojun nd6log((LOG_ERR,
733 1.47 itojun "%s: didn't unlink in6ifaddr from list\n",
734 1.47 itojun if_name(ifp)));
735 1.34 itojun }
736 1.2 itojun }
737 1.2 itojun
738 1.30 itojun IFAFREE(&oia->ia_ifa);
739 1.16 itojun }
740 1.16 itojun
741 1.17 itojun /* cleanup multicast address kludge table, if there is any */
742 1.17 itojun in6_purgemkludge(ifp);
743 1.30 itojun
744 1.41 itojun /*
745 1.41 itojun * remove neighbor management table. we call it twice just to make
746 1.41 itojun * sure we nuke everything. maybe we need just one call.
747 1.41 itojun * XXX: since the first call did not release addresses, some prefixes
748 1.41 itojun * might remain. We should call nd6_purge() again to release the
749 1.41 itojun * prefixes after removing all addresses above.
750 1.41 itojun * (Or can we just delay calling nd6_purge until at this point?)
751 1.41 itojun */
752 1.30 itojun nd6_purge(ifp);
753 1.18 itojun
754 1.16 itojun /* remove route to link-local allnodes multicast (ff02::1) */
755 1.16 itojun bzero(&sin6, sizeof(sin6));
756 1.16 itojun sin6.sin6_len = sizeof(struct sockaddr_in6);
757 1.16 itojun sin6.sin6_family = AF_INET6;
758 1.16 itojun sin6.sin6_addr = in6addr_linklocal_allnodes;
759 1.16 itojun sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
760 1.33 itojun rt = rtalloc1((struct sockaddr *)&sin6, 0);
761 1.33 itojun if (rt && rt->rt_ifp == ifp) {
762 1.16 itojun rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
763 1.48 itojun rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
764 1.16 itojun rtfree(rt);
765 1.2 itojun }
766 1.2 itojun }
767