in6_ifattach.c revision 1.46 1 1.46 itojun /* $NetBSD: in6_ifattach.c,v 1.46 2002/05/29 07:53:40 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.46 itojun __KERNEL_RCSID(0, "$NetBSD: in6_ifattach.c,v 1.46 2002/05/29 07:53:40 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.2 itojun
59 1.13 itojun #include <net/net_osdep.h>
60 1.13 itojun
61 1.2 itojun unsigned long in6_maxmtu = 0;
62 1.2 itojun
63 1.25 itojun static int get_rand_ifid __P((struct ifnet *, struct in6_addr *));
64 1.25 itojun static int get_hw_ifid __P((struct ifnet *, struct in6_addr *));
65 1.25 itojun static int get_ifid __P((struct ifnet *, struct ifnet *, struct in6_addr *));
66 1.25 itojun static int in6_ifattach_addaddr __P((struct ifnet *, struct in6_ifaddr *));
67 1.25 itojun static int in6_ifattach_linklocal __P((struct ifnet *, struct ifnet *));
68 1.25 itojun static int in6_ifattach_loopback __P((struct ifnet *));
69 1.25 itojun
70 1.25 itojun #define EUI64_GBIT 0x01
71 1.25 itojun #define EUI64_UBIT 0x02
72 1.25 itojun #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
73 1.25 itojun #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
74 1.25 itojun #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
75 1.25 itojun #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
76 1.25 itojun #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
77 1.25 itojun
78 1.25 itojun #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
79 1.25 itojun #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
80 1.25 itojun
81 1.25 itojun /*
82 1.25 itojun * Generate a last-resort interface identifier, when the machine has no
83 1.25 itojun * IEEE802/EUI64 address sources.
84 1.25 itojun * The goal here is to get an interface identifier that is
85 1.25 itojun * (1) random enough and (2) does not change across reboot.
86 1.25 itojun * We currently use MD5(hostname) for it.
87 1.25 itojun */
88 1.25 itojun static int
89 1.25 itojun get_rand_ifid(ifp, in6)
90 1.25 itojun struct ifnet *ifp;
91 1.40 itojun struct in6_addr *in6; /* upper 64bits are preserved */
92 1.25 itojun {
93 1.25 itojun MD5_CTX ctxt;
94 1.25 itojun u_int8_t digest[16];
95 1.25 itojun
96 1.25 itojun #if 0
97 1.25 itojun /* we need at least several letters as seed for ifid */
98 1.25 itojun if (hostnamelen < 3)
99 1.25 itojun return -1;
100 1.25 itojun #endif
101 1.25 itojun
102 1.25 itojun /* generate 8 bytes of pseudo-random value. */
103 1.25 itojun bzero(&ctxt, sizeof(ctxt));
104 1.25 itojun MD5Init(&ctxt);
105 1.25 itojun MD5Update(&ctxt, hostname, hostnamelen);
106 1.25 itojun MD5Final(digest, &ctxt);
107 1.25 itojun
108 1.25 itojun /* assumes sizeof(digest) > sizeof(ifid) */
109 1.25 itojun bcopy(digest, &in6->s6_addr[8], 8);
110 1.2 itojun
111 1.25 itojun /* make sure to set "u" bit to local, and "g" bit to individual. */
112 1.25 itojun in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
113 1.25 itojun in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
114 1.25 itojun
115 1.25 itojun /* convert EUI64 into IPv6 interface identifier */
116 1.25 itojun EUI64_TO_IFID(in6);
117 1.25 itojun
118 1.25 itojun return 0;
119 1.25 itojun }
120 1.2 itojun
121 1.25 itojun /*
122 1.25 itojun * Get interface identifier for the specified interface.
123 1.25 itojun * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
124 1.25 itojun */
125 1.7 itojun static int
126 1.25 itojun get_hw_ifid(ifp, in6)
127 1.25 itojun struct ifnet *ifp;
128 1.40 itojun struct in6_addr *in6; /* upper 64bits are preserved */
129 1.2 itojun {
130 1.25 itojun struct ifaddr *ifa;
131 1.25 itojun struct sockaddr_dl *sdl;
132 1.25 itojun u_int8_t *addr;
133 1.25 itojun size_t addrlen;
134 1.25 itojun static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
135 1.25 itojun static u_int8_t allone[8] =
136 1.25 itojun { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
137 1.25 itojun
138 1.25 itojun for (ifa = ifp->if_addrlist.tqh_first;
139 1.25 itojun ifa;
140 1.25 itojun ifa = ifa->ifa_list.tqe_next)
141 1.25 itojun {
142 1.25 itojun if (ifa->ifa_addr->sa_family != AF_LINK)
143 1.25 itojun continue;
144 1.25 itojun sdl = (struct sockaddr_dl *)ifa->ifa_addr;
145 1.25 itojun if (sdl == NULL)
146 1.25 itojun continue;
147 1.25 itojun if (sdl->sdl_alen == 0)
148 1.25 itojun continue;
149 1.25 itojun
150 1.25 itojun goto found;
151 1.25 itojun }
152 1.25 itojun
153 1.25 itojun return -1;
154 1.25 itojun
155 1.25 itojun found:
156 1.25 itojun addr = LLADDR(sdl);
157 1.25 itojun addrlen = sdl->sdl_alen;
158 1.25 itojun
159 1.25 itojun /* get EUI64 */
160 1.25 itojun switch (ifp->if_type) {
161 1.25 itojun case IFT_ETHER:
162 1.25 itojun case IFT_FDDI:
163 1.25 itojun case IFT_ATM:
164 1.32 onoe case IFT_IEEE1394:
165 1.25 itojun /* IEEE802/EUI64 cases - what others? */
166 1.32 onoe /* IEEE1394 uses 16byte length address starting with EUI64 */
167 1.32 onoe if (addrlen > 8)
168 1.32 onoe addrlen = 8;
169 1.13 itojun
170 1.25 itojun /* look at IEEE802/EUI64 only */
171 1.25 itojun if (addrlen != 8 && addrlen != 6)
172 1.25 itojun return -1;
173 1.13 itojun
174 1.25 itojun /*
175 1.25 itojun * check for invalid MAC address - on bsdi, we see it a lot
176 1.25 itojun * since wildboar configures all-zero MAC on pccard before
177 1.25 itojun * card insertion.
178 1.25 itojun */
179 1.25 itojun if (bcmp(addr, allzero, addrlen) == 0)
180 1.25 itojun return -1;
181 1.25 itojun if (bcmp(addr, allone, addrlen) == 0)
182 1.25 itojun return -1;
183 1.25 itojun
184 1.25 itojun /* make EUI64 address */
185 1.25 itojun if (addrlen == 8)
186 1.25 itojun bcopy(addr, &in6->s6_addr[8], 8);
187 1.25 itojun else if (addrlen == 6) {
188 1.25 itojun in6->s6_addr[8] = addr[0];
189 1.25 itojun in6->s6_addr[9] = addr[1];
190 1.25 itojun in6->s6_addr[10] = addr[2];
191 1.25 itojun in6->s6_addr[11] = 0xff;
192 1.26 itojun in6->s6_addr[12] = 0xfe;
193 1.25 itojun in6->s6_addr[13] = addr[3];
194 1.25 itojun in6->s6_addr[14] = addr[4];
195 1.25 itojun in6->s6_addr[15] = addr[5];
196 1.25 itojun }
197 1.7 itojun break;
198 1.25 itojun
199 1.25 itojun case IFT_ARCNET:
200 1.25 itojun if (addrlen != 1)
201 1.25 itojun return -1;
202 1.25 itojun if (!addr[0])
203 1.25 itojun return -1;
204 1.25 itojun
205 1.25 itojun bzero(&in6->s6_addr[8], 8);
206 1.25 itojun in6->s6_addr[15] = addr[0];
207 1.25 itojun
208 1.27 itojun /*
209 1.27 itojun * due to insufficient bitwidth, we mark it local.
210 1.27 itojun */
211 1.25 itojun in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
212 1.25 itojun in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
213 1.7 itojun break;
214 1.25 itojun
215 1.25 itojun case IFT_GIF:
216 1.25 itojun #ifdef IFT_STF
217 1.25 itojun case IFT_STF:
218 1.25 itojun #endif
219 1.25 itojun /*
220 1.34 itojun * RFC2893 says: "SHOULD use IPv4 address as ifid source".
221 1.27 itojun * however, IPv4 address is not very suitable as unique
222 1.27 itojun * identifier source (can be renumbered).
223 1.27 itojun * we don't do this.
224 1.25 itojun */
225 1.25 itojun return -1;
226 1.25 itojun
227 1.7 itojun default:
228 1.25 itojun return -1;
229 1.25 itojun }
230 1.25 itojun
231 1.25 itojun /* sanity check: g bit must not indicate "group" */
232 1.25 itojun if (EUI64_GROUP(in6))
233 1.25 itojun return -1;
234 1.25 itojun
235 1.25 itojun /* convert EUI64 into IPv6 interface identifier */
236 1.25 itojun EUI64_TO_IFID(in6);
237 1.25 itojun
238 1.25 itojun /*
239 1.25 itojun * sanity check: ifid must not be all zero, avoid conflict with
240 1.25 itojun * subnet router anycast
241 1.25 itojun */
242 1.25 itojun if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
243 1.25 itojun bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
244 1.25 itojun return -1;
245 1.7 itojun }
246 1.7 itojun
247 1.7 itojun return 0;
248 1.2 itojun }
249 1.2 itojun
250 1.2 itojun /*
251 1.25 itojun * Get interface identifier for the specified interface. If it is not
252 1.25 itojun * available on ifp0, borrow interface identifier from other information
253 1.25 itojun * sources.
254 1.13 itojun */
255 1.13 itojun static int
256 1.25 itojun get_ifid(ifp0, altifp, in6)
257 1.25 itojun struct ifnet *ifp0;
258 1.25 itojun struct ifnet *altifp; /*secondary EUI64 source*/
259 1.25 itojun struct in6_addr *in6;
260 1.13 itojun {
261 1.25 itojun struct ifnet *ifp;
262 1.25 itojun
263 1.25 itojun /* first, try to get it from the interface itself */
264 1.25 itojun if (get_hw_ifid(ifp0, in6) == 0) {
265 1.34 itojun nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
266 1.34 itojun if_name(ifp0)));
267 1.25 itojun goto success;
268 1.25 itojun }
269 1.25 itojun
270 1.25 itojun /* try secondary EUI64 source. this basically is for ATM PVC */
271 1.25 itojun if (altifp && get_hw_ifid(altifp, in6) == 0) {
272 1.34 itojun nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
273 1.34 itojun if_name(ifp0), if_name(altifp)));
274 1.25 itojun goto success;
275 1.25 itojun }
276 1.25 itojun
277 1.25 itojun /* next, try to get it from some other hardware interface */
278 1.25 itojun for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
279 1.25 itojun {
280 1.25 itojun if (ifp == ifp0)
281 1.25 itojun continue;
282 1.25 itojun if (get_hw_ifid(ifp, in6) != 0)
283 1.25 itojun continue;
284 1.27 itojun
285 1.25 itojun /*
286 1.25 itojun * to borrow ifid from other interface, ifid needs to be
287 1.25 itojun * globally unique
288 1.25 itojun */
289 1.25 itojun if (IFID_UNIVERSAL(in6)) {
290 1.34 itojun nd6log((LOG_DEBUG,
291 1.34 itojun "%s: borrow interface identifier from %s\n",
292 1.34 itojun if_name(ifp0), if_name(ifp)));
293 1.25 itojun goto success;
294 1.25 itojun }
295 1.25 itojun }
296 1.13 itojun
297 1.25 itojun /* last resort: get from random number source */
298 1.25 itojun if (get_rand_ifid(ifp, in6) == 0) {
299 1.34 itojun nd6log((LOG_DEBUG,
300 1.34 itojun "%s: interface identifier generated by random number\n",
301 1.34 itojun if_name(ifp0)));
302 1.25 itojun goto success;
303 1.25 itojun }
304 1.13 itojun
305 1.31 itojun printf("%s: failed to get interface identifier\n", if_name(ifp0));
306 1.25 itojun return -1;
307 1.13 itojun
308 1.25 itojun success:
309 1.34 itojun nd6log((LOG_INFO, "%s: ifid: "
310 1.25 itojun "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
311 1.25 itojun if_name(ifp0),
312 1.25 itojun in6->s6_addr[8], in6->s6_addr[9],
313 1.25 itojun in6->s6_addr[10], in6->s6_addr[11],
314 1.25 itojun in6->s6_addr[12], in6->s6_addr[13],
315 1.34 itojun in6->s6_addr[14], in6->s6_addr[15]));
316 1.13 itojun return 0;
317 1.13 itojun }
318 1.13 itojun
319 1.13 itojun /*
320 1.25 itojun * configure IPv6 interface address. XXX code duplicated with in.c
321 1.2 itojun */
322 1.25 itojun static int
323 1.25 itojun in6_ifattach_addaddr(ifp, ia)
324 1.25 itojun struct ifnet *ifp;
325 1.25 itojun struct in6_ifaddr *ia;
326 1.2 itojun {
327 1.25 itojun struct in6_ifaddr *oia;
328 1.2 itojun struct ifaddr *ifa;
329 1.25 itojun int error;
330 1.25 itojun int rtflag;
331 1.25 itojun struct in6_addr llsol;
332 1.25 itojun
333 1.25 itojun /*
334 1.25 itojun * initialize if_addrlist, if we are the very first one
335 1.25 itojun */
336 1.25 itojun ifa = TAILQ_FIRST(&ifp->if_addrlist);
337 1.25 itojun if (ifa == NULL) {
338 1.25 itojun TAILQ_INIT(&ifp->if_addrlist);
339 1.25 itojun }
340 1.2 itojun
341 1.25 itojun /*
342 1.25 itojun * link the interface address to global list
343 1.25 itojun */
344 1.25 itojun TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
345 1.27 itojun IFAREF(&ia->ia_ifa);
346 1.2 itojun
347 1.25 itojun /*
348 1.25 itojun * Also link into the IPv6 address chain beginning with in6_ifaddr.
349 1.25 itojun * kazu opposed it, but itojun & jinmei wanted.
350 1.25 itojun */
351 1.25 itojun if ((oia = in6_ifaddr) != NULL) {
352 1.25 itojun for (; oia->ia_next; oia = oia->ia_next)
353 1.2 itojun continue;
354 1.25 itojun oia->ia_next = ia;
355 1.25 itojun } else
356 1.25 itojun in6_ifaddr = ia;
357 1.27 itojun IFAREF(&ia->ia_ifa);
358 1.25 itojun
359 1.25 itojun /*
360 1.25 itojun * give the interface a chance to initialize, in case this
361 1.25 itojun * is the first address to be added.
362 1.25 itojun */
363 1.25 itojun if (ifp->if_ioctl != NULL) {
364 1.25 itojun int s;
365 1.35 thorpej s = splnet();
366 1.25 itojun error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
367 1.25 itojun splx(s);
368 1.25 itojun } else
369 1.25 itojun error = 0;
370 1.25 itojun if (error) {
371 1.25 itojun switch (error) {
372 1.25 itojun case EAFNOSUPPORT:
373 1.25 itojun printf("%s: IPv6 not supported\n", if_name(ifp));
374 1.25 itojun break;
375 1.25 itojun default:
376 1.25 itojun printf("%s: SIOCSIFADDR error %d\n", if_name(ifp),
377 1.25 itojun error);
378 1.25 itojun break;
379 1.25 itojun }
380 1.25 itojun
381 1.25 itojun /* undo changes */
382 1.25 itojun TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
383 1.25 itojun IFAFREE(&ia->ia_ifa);
384 1.25 itojun if (oia)
385 1.25 itojun oia->ia_next = ia->ia_next;
386 1.25 itojun else
387 1.25 itojun in6_ifaddr = ia->ia_next;
388 1.25 itojun IFAFREE(&ia->ia_ifa);
389 1.25 itojun return -1;
390 1.25 itojun }
391 1.25 itojun
392 1.25 itojun /* configure link-layer address resolution */
393 1.27 itojun rtflag = 0;
394 1.25 itojun if (IN6_ARE_ADDR_EQUAL(&ia->ia_prefixmask.sin6_addr, &in6mask128))
395 1.25 itojun rtflag = RTF_HOST;
396 1.25 itojun else {
397 1.25 itojun switch (ifp->if_type) {
398 1.25 itojun case IFT_LOOP:
399 1.25 itojun #ifdef IFT_STF
400 1.25 itojun case IFT_STF:
401 1.25 itojun #endif
402 1.25 itojun rtflag = 0;
403 1.25 itojun break;
404 1.25 itojun default:
405 1.25 itojun ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
406 1.25 itojun ia->ia_ifa.ifa_flags |= RTF_CLONING;
407 1.25 itojun rtflag = RTF_CLONING;
408 1.25 itojun break;
409 1.2 itojun }
410 1.2 itojun }
411 1.25 itojun
412 1.25 itojun /* add route to the interface. */
413 1.25 itojun rtrequest(RTM_ADD,
414 1.25 itojun (struct sockaddr *)&ia->ia_addr,
415 1.25 itojun (struct sockaddr *)&ia->ia_addr,
416 1.25 itojun (struct sockaddr *)&ia->ia_prefixmask,
417 1.25 itojun RTF_UP | rtflag,
418 1.25 itojun (struct rtentry **)0);
419 1.25 itojun ia->ia_flags |= IFA_ROUTE;
420 1.25 itojun
421 1.25 itojun if ((rtflag & RTF_CLONING) != 0 &&
422 1.25 itojun (ifp->if_flags & IFF_MULTICAST) != 0) {
423 1.25 itojun /* Restore saved multicast addresses (if any). */
424 1.25 itojun in6_restoremkludge(ia, ifp);
425 1.25 itojun
426 1.25 itojun /*
427 1.25 itojun * join solicited multicast address
428 1.25 itojun */
429 1.25 itojun bzero(&llsol, sizeof(llsol));
430 1.25 itojun llsol.s6_addr16[0] = htons(0xff02);
431 1.25 itojun llsol.s6_addr16[1] = htons(ifp->if_index);
432 1.25 itojun llsol.s6_addr32[1] = 0;
433 1.25 itojun llsol.s6_addr32[2] = htonl(1);
434 1.25 itojun llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
435 1.25 itojun llsol.s6_addr8[12] = 0xff;
436 1.36 itojun if (!in6_addmulti(&llsol, ifp, &error)) {
437 1.36 itojun nd6log((LOG_ERR, "%s: failed to join %s (errno=%d)\n",
438 1.36 itojun if_name(ifp), ip6_sprintf(&llsol), error));
439 1.36 itojun }
440 1.25 itojun
441 1.43 itojun if (in6if_do_dad(ifp)) {
442 1.25 itojun /* mark the address TENTATIVE, if needed. */
443 1.25 itojun ia->ia6_flags |= IN6_IFF_TENTATIVE;
444 1.25 itojun /* nd6_dad_start() will be called in in6_if_up */
445 1.25 itojun }
446 1.25 itojun }
447 1.25 itojun
448 1.25 itojun return 0;
449 1.25 itojun }
450 1.25 itojun
451 1.25 itojun static int
452 1.25 itojun in6_ifattach_linklocal(ifp, altifp)
453 1.25 itojun struct ifnet *ifp;
454 1.25 itojun struct ifnet *altifp; /*secondary EUI64 source*/
455 1.25 itojun {
456 1.25 itojun struct in6_ifaddr *ia;
457 1.2 itojun
458 1.25 itojun /*
459 1.25 itojun * configure link-local address
460 1.25 itojun */
461 1.25 itojun ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
462 1.25 itojun bzero((caddr_t)ia, sizeof(*ia));
463 1.25 itojun ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
464 1.25 itojun if (ifp->if_flags & IFF_POINTOPOINT)
465 1.25 itojun ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
466 1.25 itojun else
467 1.25 itojun ia->ia_ifa.ifa_dstaddr = NULL;
468 1.25 itojun ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
469 1.25 itojun ia->ia_ifp = ifp;
470 1.2 itojun
471 1.25 itojun bzero(&ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
472 1.25 itojun ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
473 1.25 itojun ia->ia_prefixmask.sin6_family = AF_INET6;
474 1.25 itojun ia->ia_prefixmask.sin6_addr = in6mask64;
475 1.5 itojun
476 1.25 itojun /* just in case */
477 1.25 itojun bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
478 1.25 itojun ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
479 1.25 itojun ia->ia_dstaddr.sin6_family = AF_INET6;
480 1.5 itojun
481 1.25 itojun bzero(&ia->ia_addr, sizeof(ia->ia_addr));
482 1.25 itojun ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
483 1.25 itojun ia->ia_addr.sin6_family = AF_INET6;
484 1.25 itojun ia->ia_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
485 1.25 itojun ia->ia_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
486 1.25 itojun ia->ia_addr.sin6_addr.s6_addr32[1] = 0;
487 1.25 itojun if (ifp->if_flags & IFF_LOOPBACK) {
488 1.25 itojun ia->ia_addr.sin6_addr.s6_addr32[2] = 0;
489 1.25 itojun ia->ia_addr.sin6_addr.s6_addr32[3] = htonl(1);
490 1.2 itojun } else {
491 1.25 itojun if (get_ifid(ifp, altifp, &ia->ia_addr.sin6_addr) != 0) {
492 1.34 itojun nd6log((LOG_ERR,
493 1.34 itojun "%s: no ifid available\n", if_name(ifp)));
494 1.25 itojun free(ia, M_IFADDR);
495 1.25 itojun return -1;
496 1.25 itojun }
497 1.25 itojun }
498 1.25 itojun
499 1.25 itojun ia->ia_ifa.ifa_metric = ifp->if_metric;
500 1.25 itojun
501 1.25 itojun if (in6_ifattach_addaddr(ifp, ia) != 0) {
502 1.25 itojun /* ia will be freed on failure */
503 1.25 itojun return -1;
504 1.25 itojun }
505 1.25 itojun
506 1.25 itojun return 0;
507 1.25 itojun }
508 1.25 itojun
509 1.25 itojun static int
510 1.25 itojun in6_ifattach_loopback(ifp)
511 1.25 itojun struct ifnet *ifp; /* must be IFT_LOOP */
512 1.25 itojun {
513 1.25 itojun struct in6_ifaddr *ia;
514 1.25 itojun
515 1.25 itojun /*
516 1.25 itojun * configure link-local address
517 1.25 itojun */
518 1.25 itojun ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
519 1.25 itojun bzero((caddr_t)ia, sizeof(*ia));
520 1.25 itojun ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
521 1.25 itojun ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
522 1.25 itojun ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
523 1.25 itojun ia->ia_ifp = ifp;
524 1.25 itojun
525 1.25 itojun bzero(&ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
526 1.25 itojun ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
527 1.25 itojun ia->ia_prefixmask.sin6_family = AF_INET6;
528 1.25 itojun ia->ia_prefixmask.sin6_addr = in6mask128;
529 1.25 itojun
530 1.25 itojun /*
531 1.25 itojun * Always initialize ia_dstaddr (= broadcast address) to loopback
532 1.25 itojun * address, to make getifaddr happier.
533 1.25 itojun *
534 1.25 itojun * For BSDI, it is mandatory. The BSDI version of
535 1.25 itojun * ifa_ifwithroute() rejects to add a route to the loopback
536 1.25 itojun * interface. Even for other systems, loopback looks somewhat
537 1.25 itojun * special.
538 1.25 itojun */
539 1.25 itojun bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
540 1.25 itojun ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
541 1.25 itojun ia->ia_dstaddr.sin6_family = AF_INET6;
542 1.25 itojun ia->ia_dstaddr.sin6_addr = in6addr_loopback;
543 1.25 itojun
544 1.25 itojun bzero(&ia->ia_addr, sizeof(ia->ia_addr));
545 1.25 itojun ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
546 1.25 itojun ia->ia_addr.sin6_family = AF_INET6;
547 1.25 itojun ia->ia_addr.sin6_addr = in6addr_loopback;
548 1.25 itojun
549 1.25 itojun ia->ia_ifa.ifa_metric = ifp->if_metric;
550 1.25 itojun
551 1.25 itojun if (in6_ifattach_addaddr(ifp, ia) != 0) {
552 1.25 itojun /* ia will be freed on failure */
553 1.25 itojun return -1;
554 1.2 itojun }
555 1.25 itojun
556 1.25 itojun return 0;
557 1.2 itojun }
558 1.2 itojun
559 1.17 itojun /*
560 1.17 itojun * XXX multiple loopback interface needs more care. for instance,
561 1.17 itojun * nodelocal address needs to be configured onto only one of them.
562 1.25 itojun * XXX multiple link-local address case
563 1.17 itojun */
564 1.2 itojun void
565 1.25 itojun in6_ifattach(ifp, altifp)
566 1.2 itojun struct ifnet *ifp;
567 1.25 itojun struct ifnet *altifp; /* secondary EUI64 source */
568 1.2 itojun {
569 1.2 itojun struct sockaddr_in6 mltaddr;
570 1.2 itojun struct sockaddr_in6 mltmask;
571 1.2 itojun struct sockaddr_in6 gate;
572 1.2 itojun struct sockaddr_in6 mask;
573 1.25 itojun struct in6_ifaddr *ia;
574 1.25 itojun struct in6_addr in6;
575 1.13 itojun
576 1.38 itojun /* some of the interfaces are inherently not IPv6 capable */
577 1.38 itojun switch (ifp->if_type) {
578 1.42 itojun case IFT_BRIDGE:
579 1.42 itojun return;
580 1.38 itojun }
581 1.38 itojun
582 1.46 itojun /*
583 1.46 itojun * if link mtu is too small, don't try to configure IPv6.
584 1.46 itojun * remember there could be some link-layer that has special
585 1.46 itojun * fragmentation logic.
586 1.46 itojun */
587 1.46 itojun if (ifp->if_mtu < IPV6_MMTU)
588 1.46 itojun return;
589 1.46 itojun
590 1.37 itojun /* create a multicast kludge storage (if we have not had one) */
591 1.37 itojun in6_createmkludge(ifp);
592 1.2 itojun
593 1.2 itojun /*
594 1.25 itojun * quirks based on interface type
595 1.2 itojun */
596 1.25 itojun switch (ifp->if_type) {
597 1.25 itojun #ifdef IFT_STF
598 1.25 itojun case IFT_STF:
599 1.25 itojun /*
600 1.38 itojun * 6to4 interface is a very special kind of beast.
601 1.38 itojun * no multicast, no linklocal. RFC2529 specifies how to make
602 1.38 itojun * linklocals for 6to4 interface, but there's no use and
603 1.38 itojun * it is rather harmful to have one.
604 1.25 itojun */
605 1.46 itojun return;
606 1.25 itojun #endif
607 1.25 itojun default:
608 1.25 itojun break;
609 1.7 itojun }
610 1.2 itojun
611 1.2 itojun /*
612 1.25 itojun * usually, we require multicast capability to the interface
613 1.2 itojun */
614 1.25 itojun if ((ifp->if_flags & IFF_MULTICAST) == 0) {
615 1.38 itojun log(LOG_INFO, "in6_ifattach: "
616 1.38 itojun "%s is not multicast capable, IPv6 not enabled\n",
617 1.25 itojun if_name(ifp));
618 1.25 itojun return;
619 1.25 itojun }
620 1.15 thorpej
621 1.2 itojun /*
622 1.25 itojun * assign link-local address, if there's none
623 1.2 itojun */
624 1.25 itojun ia = in6ifa_ifpforlinklocal(ifp, 0);
625 1.25 itojun if (ia == NULL) {
626 1.25 itojun if (in6_ifattach_linklocal(ifp, altifp) != 0)
627 1.25 itojun return;
628 1.25 itojun ia = in6ifa_ifpforlinklocal(ifp, 0);
629 1.2 itojun
630 1.25 itojun if (ia == NULL) {
631 1.31 itojun printf("%s: failed to add link-local address\n",
632 1.25 itojun if_name(ifp));
633 1.2 itojun
634 1.25 itojun /* we can't initialize multicasts without link-local */
635 1.46 itojun return;
636 1.2 itojun }
637 1.2 itojun }
638 1.2 itojun
639 1.25 itojun if (ifp->if_flags & IFF_POINTOPOINT) {
640 1.2 itojun /*
641 1.2 itojun * route local address to loopback
642 1.2 itojun */
643 1.2 itojun bzero(&gate, sizeof(gate));
644 1.2 itojun gate.sin6_len = sizeof(struct sockaddr_in6);
645 1.2 itojun gate.sin6_family = AF_INET6;
646 1.2 itojun gate.sin6_addr = in6addr_loopback;
647 1.2 itojun bzero(&mask, sizeof(mask));
648 1.2 itojun mask.sin6_len = sizeof(struct sockaddr_in6);
649 1.2 itojun mask.sin6_family = AF_INET6;
650 1.2 itojun mask.sin6_addr = in6mask64;
651 1.2 itojun rtrequest(RTM_ADD,
652 1.2 itojun (struct sockaddr *)&ia->ia_addr,
653 1.2 itojun (struct sockaddr *)&gate,
654 1.2 itojun (struct sockaddr *)&mask,
655 1.2 itojun RTF_UP|RTF_HOST,
656 1.2 itojun (struct rtentry **)0);
657 1.2 itojun }
658 1.2 itojun
659 1.2 itojun /*
660 1.25 itojun * assign loopback address for loopback interface
661 1.25 itojun * XXX multiple loopback interface case
662 1.2 itojun */
663 1.25 itojun in6 = in6addr_loopback;
664 1.25 itojun if (ifp->if_flags & IFF_LOOPBACK) {
665 1.25 itojun if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
666 1.25 itojun if (in6_ifattach_loopback(ifp) != 0)
667 1.25 itojun return;
668 1.25 itojun }
669 1.25 itojun }
670 1.2 itojun
671 1.25 itojun #ifdef DIAGNOSTIC
672 1.25 itojun if (!ia) {
673 1.25 itojun panic("ia == NULL in in6_ifattach");
674 1.25 itojun /*NOTREACHED*/
675 1.2 itojun }
676 1.25 itojun #endif
677 1.2 itojun
678 1.2 itojun /*
679 1.2 itojun * join multicast
680 1.2 itojun */
681 1.2 itojun if (ifp->if_flags & IFF_MULTICAST) {
682 1.2 itojun int error; /* not used */
683 1.25 itojun struct in6_multi *in6m;
684 1.2 itojun
685 1.25 itojun /* Restore saved multicast addresses (if any). */
686 1.2 itojun in6_restoremkludge(ia, ifp);
687 1.2 itojun
688 1.2 itojun bzero(&mltmask, sizeof(mltmask));
689 1.2 itojun mltmask.sin6_len = sizeof(struct sockaddr_in6);
690 1.2 itojun mltmask.sin6_family = AF_INET6;
691 1.2 itojun mltmask.sin6_addr = in6mask32;
692 1.2 itojun
693 1.2 itojun /*
694 1.2 itojun * join link-local all-nodes address
695 1.2 itojun */
696 1.2 itojun bzero(&mltaddr, sizeof(mltaddr));
697 1.2 itojun mltaddr.sin6_len = sizeof(struct sockaddr_in6);
698 1.2 itojun mltaddr.sin6_family = AF_INET6;
699 1.2 itojun mltaddr.sin6_addr = in6addr_linklocal_allnodes;
700 1.2 itojun mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
701 1.2 itojun
702 1.25 itojun IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
703 1.25 itojun if (in6m == NULL) {
704 1.2 itojun rtrequest(RTM_ADD,
705 1.2 itojun (struct sockaddr *)&mltaddr,
706 1.25 itojun (struct sockaddr *)&ia->ia_addr,
707 1.2 itojun (struct sockaddr *)&mltmask,
708 1.25 itojun RTF_UP|RTF_CLONING, /* xxx */
709 1.2 itojun (struct rtentry **)0);
710 1.36 itojun if (!in6_addmulti(&mltaddr.sin6_addr, ifp, &error)) {
711 1.36 itojun nd6log((LOG_ERR, "%s: failed to join %s "
712 1.36 itojun "(errno=%d)\n", if_name(ifp),
713 1.36 itojun ip6_sprintf(&mltaddr.sin6_addr),
714 1.36 itojun error));
715 1.36 itojun }
716 1.25 itojun }
717 1.25 itojun
718 1.25 itojun if (ifp->if_flags & IFF_LOOPBACK) {
719 1.25 itojun in6 = in6addr_loopback;
720 1.25 itojun ia = in6ifa_ifpwithaddr(ifp, &in6);
721 1.2 itojun /*
722 1.25 itojun * join node-local all-nodes address, on loopback
723 1.2 itojun */
724 1.25 itojun mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
725 1.25 itojun
726 1.25 itojun IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
727 1.25 itojun if (in6m == NULL && ia != NULL) {
728 1.25 itojun rtrequest(RTM_ADD,
729 1.25 itojun (struct sockaddr *)&mltaddr,
730 1.25 itojun (struct sockaddr *)&ia->ia_addr,
731 1.25 itojun (struct sockaddr *)&mltmask,
732 1.25 itojun RTF_UP,
733 1.25 itojun (struct rtentry **)0);
734 1.36 itojun if (!in6_addmulti(&mltaddr.sin6_addr, ifp,
735 1.36 itojun &error)) {
736 1.36 itojun nd6log((LOG_ERR, "%s: failed to join "
737 1.36 itojun "%s (errno=%d)\n", if_name(ifp),
738 1.36 itojun ip6_sprintf(&mltaddr.sin6_addr),
739 1.36 itojun error));
740 1.36 itojun }
741 1.25 itojun }
742 1.2 itojun }
743 1.2 itojun }
744 1.2 itojun }
745 1.2 itojun
746 1.17 itojun /*
747 1.17 itojun * NOTE: in6_ifdetach() does not support loopback if at this moment.
748 1.41 itojun * We don't need this function in bsdi, because interfaces are never removed
749 1.41 itojun * from the ifnet list in bsdi.
750 1.17 itojun */
751 1.2 itojun void
752 1.2 itojun in6_ifdetach(ifp)
753 1.2 itojun struct ifnet *ifp;
754 1.2 itojun {
755 1.2 itojun struct in6_ifaddr *ia, *oia;
756 1.30 itojun struct ifaddr *ifa, *next;
757 1.2 itojun struct rtentry *rt;
758 1.2 itojun short rtflags;
759 1.16 itojun struct sockaddr_in6 sin6;
760 1.2 itojun
761 1.18 itojun /* nuke prefix list. this may try to remove some of ifaddrs as well */
762 1.18 itojun in6_purgeprefix(ifp);
763 1.18 itojun
764 1.18 itojun /* remove neighbor management table */
765 1.18 itojun nd6_purge(ifp);
766 1.18 itojun
767 1.30 itojun /* nuke any of IPv6 addresses we have */
768 1.30 itojun for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
769 1.30 itojun {
770 1.30 itojun next = ifa->ifa_list.tqe_next;
771 1.30 itojun if (ifa->ifa_addr->sa_family != AF_INET6)
772 1.30 itojun continue;
773 1.30 itojun in6_purgeaddr(ifa, ifp);
774 1.30 itojun }
775 1.30 itojun
776 1.30 itojun /* undo everything done by in6_ifattach(), just in case */
777 1.30 itojun for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
778 1.13 itojun {
779 1.30 itojun next = ifa->ifa_list.tqe_next;
780 1.30 itojun
781 1.30 itojun
782 1.2 itojun if (ifa->ifa_addr->sa_family != AF_INET6
783 1.2 itojun || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
784 1.2 itojun continue;
785 1.2 itojun }
786 1.2 itojun
787 1.2 itojun ia = (struct in6_ifaddr *)ifa;
788 1.2 itojun
789 1.2 itojun /* remove from the routing table */
790 1.2 itojun if ((ia->ia_flags & IFA_ROUTE)
791 1.14 itojun && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
792 1.2 itojun rtflags = rt->rt_flags;
793 1.2 itojun rtfree(rt);
794 1.2 itojun rtrequest(RTM_DELETE,
795 1.2 itojun (struct sockaddr *)&ia->ia_addr,
796 1.24 itojun (struct sockaddr *)&ia->ia_addr,
797 1.2 itojun (struct sockaddr *)&ia->ia_prefixmask,
798 1.2 itojun rtflags, (struct rtentry **)0);
799 1.2 itojun }
800 1.2 itojun
801 1.2 itojun /* remove from the linked list */
802 1.2 itojun TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
803 1.30 itojun IFAFREE(&ia->ia_ifa);
804 1.2 itojun
805 1.2 itojun /* also remove from the IPv6 address chain(itojun&jinmei) */
806 1.2 itojun oia = ia;
807 1.2 itojun if (oia == (ia = in6_ifaddr))
808 1.2 itojun in6_ifaddr = ia->ia_next;
809 1.2 itojun else {
810 1.2 itojun while (ia->ia_next && (ia->ia_next != oia))
811 1.2 itojun ia = ia->ia_next;
812 1.2 itojun if (ia->ia_next)
813 1.2 itojun ia->ia_next = oia->ia_next;
814 1.34 itojun else {
815 1.34 itojun nd6log((LOG_ERR,
816 1.34 itojun "%s: didn't unlink in6ifaddr from "
817 1.34 itojun "list\n", if_name(ifp)));
818 1.34 itojun }
819 1.2 itojun }
820 1.2 itojun
821 1.30 itojun IFAFREE(&oia->ia_ifa);
822 1.16 itojun }
823 1.16 itojun
824 1.17 itojun /* cleanup multicast address kludge table, if there is any */
825 1.17 itojun in6_purgemkludge(ifp);
826 1.30 itojun
827 1.41 itojun /*
828 1.41 itojun * remove neighbor management table. we call it twice just to make
829 1.41 itojun * sure we nuke everything. maybe we need just one call.
830 1.41 itojun * XXX: since the first call did not release addresses, some prefixes
831 1.41 itojun * might remain. We should call nd6_purge() again to release the
832 1.41 itojun * prefixes after removing all addresses above.
833 1.41 itojun * (Or can we just delay calling nd6_purge until at this point?)
834 1.41 itojun */
835 1.30 itojun nd6_purge(ifp);
836 1.18 itojun
837 1.16 itojun /* remove route to link-local allnodes multicast (ff02::1) */
838 1.16 itojun bzero(&sin6, sizeof(sin6));
839 1.16 itojun sin6.sin6_len = sizeof(struct sockaddr_in6);
840 1.16 itojun sin6.sin6_family = AF_INET6;
841 1.16 itojun sin6.sin6_addr = in6addr_linklocal_allnodes;
842 1.16 itojun sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
843 1.33 itojun rt = rtalloc1((struct sockaddr *)&sin6, 0);
844 1.33 itojun if (rt && rt->rt_ifp == ifp) {
845 1.16 itojun rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
846 1.16 itojun rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
847 1.16 itojun rtfree(rt);
848 1.2 itojun }
849 1.2 itojun }
850