in6.c revision 1.6 1 /* $NetBSD: in6.c,v 1.6 1999/09/19 21:31:34 is Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)in.c 8.2 (Berkeley) 11/15/93
65 */
66
67 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
68 #include "opt_inet.h"
69 #endif
70
71 #include <sys/param.h>
72 #if !defined(__FreeBSD__) && __FreeBSD__ < 3
73 #include <sys/ioctl.h>
74 #endif
75 #include <sys/errno.h>
76 #include <sys/malloc.h>
77 #include <sys/proc.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/sockio.h>
81 #include <sys/systm.h>
82 #ifdef __NetBSD__
83 #include <sys/proc.h>
84 #endif
85 #include <sys/time.h>
86 #include <sys/kernel.h>
87
88 #include <net/if.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91 #include "gif.h"
92 #if NGIF > 0
93 #include <net/if_gif.h>
94 #endif
95 #include <net/if_dl.h>
96
97 #include <net/if.h>
98 #include <netinet/in.h>
99 #include <netinet/in_var.h>
100 #ifdef __NetBSD__
101 #include <net/if_ether.h>
102 #else
103 #include <netinet/if_ether.h>
104 #endif
105
106 #include <netinet6/nd6.h>
107 #include <netinet6/ip6.h>
108 #include <netinet6/mld6_var.h>
109 #include <netinet6/ip6_mroute.h>
110 #include <netinet6/in6_ifattach.h>
111
112 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
113 MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
114 #else
115 #define time_second time.tv_sec
116 #endif
117
118 /*
119 * Definitions of some costant IP6 addresses.
120 */
121 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
122 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
123 const struct in6_addr in6addr_nodelocal_allnodes =
124 IN6ADDR_NODELOCAL_ALLNODES_INIT;
125 const struct in6_addr in6addr_linklocal_allnodes =
126 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
127 const struct in6_addr in6addr_linklocal_allrouters =
128 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
129
130 const struct in6_addr in6mask0 = IN6MASK0;
131 const struct in6_addr in6mask32 = IN6MASK32;
132 const struct in6_addr in6mask64 = IN6MASK64;
133 const struct in6_addr in6mask96 = IN6MASK96;
134 const struct in6_addr in6mask128 = IN6MASK128;
135
136 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
137 struct ifnet *, struct proc *));
138
139 /*
140 * This structure is used to keep track of in6_multi chains which belong to
141 * deleted interface addresses.
142 */
143 static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
144
145 struct multi6_kludge {
146 LIST_ENTRY(multi6_kludge) mk_entry;
147 struct ifnet *mk_ifp;
148 struct in6_multihead mk_head;
149 };
150
151 /*
152 * Determine whether an IP6 address is in a reserved set of addresses
153 * that may not be forwarded, or whether datagrams to that destination
154 * may be forwarded.
155 */
156 int
157 in6_canforward(src, dst)
158 struct in6_addr *src, *dst;
159 {
160 if (IN6_IS_ADDR_LINKLOCAL(src) ||
161 IN6_IS_ADDR_LINKLOCAL(dst) ||
162 IN6_IS_ADDR_MULTICAST(dst))
163 return(0);
164 return(1);
165 }
166
167 /*
168 * Check if the loopback entry will be automatically generated.
169 * if 0 returned, will not be automatically generated.
170 * if 1 returned, will be automatically generated.
171 */
172 static int
173 in6_is_ifloop_auto(struct ifaddr *ifa)
174 {
175 #define SIN6(s) ((struct sockaddr_in6 *)s)
176 /*
177 * If RTF_CLONING is unset, or (IFF_LOOPBACK | IFF_POINTOPOINT),
178 * or netmask is all0 or all1, then cloning will not happen,
179 * then we can't rely on its loopback entry generation.
180 */
181 if ((ifa->ifa_flags & RTF_CLONING) == 0 ||
182 (ifa->ifa_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) ||
183 (SIN6(ifa->ifa_netmask)->sin6_len == sizeof(struct sockaddr_in6)
184 &&
185 IN6_ARE_ADDR_EQUAL(&SIN6(ifa->ifa_netmask)->sin6_addr,
186 &in6mask128)) ||
187 ((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_len == 0)
188 return 0;
189 else
190 return 1;
191 #undef SIN6
192 }
193
194 /*
195 * Subroutine for in6_ifaddloop() and in6_ifremloop().
196 * This routine does actual work.
197 */
198 static void
199 in6_ifloop_request(int cmd, struct ifaddr *ifa)
200 {
201 struct sockaddr_in6 lo_sa;
202 struct sockaddr_in6 all1_sa;
203 struct rtentry *nrt = NULL;
204
205 bzero(&lo_sa, sizeof(lo_sa));
206 bzero(&all1_sa, sizeof(all1_sa));
207 lo_sa.sin6_family = AF_INET6;
208 lo_sa.sin6_len = sizeof(struct sockaddr_in6);
209 all1_sa = lo_sa;
210 lo_sa.sin6_addr = in6addr_loopback;
211 all1_sa.sin6_addr = in6mask128;
212
213 /* So we add or remove static loopback entry, here. */
214 rtrequest(cmd, ifa->ifa_addr,
215 (struct sockaddr *)&lo_sa,
216 (struct sockaddr *)&all1_sa,
217 RTF_UP|RTF_HOST, &nrt);
218
219 /*
220 * Make sure rt_ifa be equal to IFA, the second argument of the
221 * function.
222 * We need this because when we refer rt_ifa->ia6_flags in ip6_input,
223 * we assume that the rt_ifa points to the address instead of the
224 * loopback address.
225 */
226 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
227 nrt->rt_ifa->ifa_refcnt--;
228 ifa->ifa_refcnt++;
229 nrt->rt_ifa = ifa;
230 }
231 if (nrt)
232 nrt->rt_refcnt--;
233 }
234
235 /*
236 * Add ownaddr as loopback rtentry, if necessary(ex. on p2p link).
237 * Because, KAME needs loopback rtentry for ownaddr check in
238 * ip6_input().
239 */
240 static void
241 in6_ifaddloop(struct ifaddr *ifa)
242 {
243 if (!in6_is_ifloop_auto(ifa)) {
244 struct rtentry *rt;
245
246 /* If there is no loopback entry, allocate one. */
247 rt = rtalloc1(ifa->ifa_addr, 0
248 #ifdef __FreeBSD__
249 , 0
250 #endif /* __FreeBSD__ */
251 );
252 if (rt == 0 || (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
253 in6_ifloop_request(RTM_ADD, ifa);
254 if (rt)
255 rt->rt_refcnt--;
256 }
257 }
258
259 /*
260 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
261 * if it exists.
262 */
263 static void
264 in6_ifremloop(struct ifaddr *ifa)
265 {
266 if (!in6_is_ifloop_auto(ifa)) {
267 struct in6_ifaddr *ia;
268 int ia_count = 0;
269
270 /* If only one ifa for the loopback entry, delete it. */
271 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
272 if (IN6_ARE_ADDR_EQUAL(&IFA_IN6(ifa),
273 &ia->ia_addr.sin6_addr)) {
274 ia_count++;
275 if (ia_count > 1)
276 break;
277 }
278 }
279 if (ia_count == 1)
280 in6_ifloop_request(RTM_DELETE, ifa);
281 }
282 }
283
284 /*
285 * Subroutine for in6_ifaddproxy() and in6_ifremproxy().
286 * This routine does actual work.
287 * call in6_addmulti() when cmd == 1.
288 * call in6_delmulti() when cmd == 2.
289 */
290 static int
291 in6_ifproxy_request(int cmd, struct in6_ifaddr *ia)
292 {
293 int error = 0;
294
295 /*
296 * If we have an IPv6 dstaddr on adding p2p interface,
297 * join dstaddr's solicited multicast on necessary interface.
298 */
299 if ((ia->ia_ifp->if_flags & IFF_POINTOPOINT) &&
300 ia->ia_dstaddr.sin6_family == AF_INET6 &&
301 !IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
302 struct in6_ifaddr *ia_lan;
303
304 /*
305 * TODO: Join only on some specified interfaces by some
306 * configuration.
307 * Unsolicited Neighbor Advertisements will be also necessary.
308 *
309 * Now, join on interfaces which meets following.
310 * -IFF_BROADCAST and IFF_MULTICAST
311 * (NBMA is out of scope)
312 * -the prefix value is same as p2p dstaddr
313 */
314 for (ia_lan = in6_ifaddr; ia_lan; ia_lan = ia_lan->ia_next) {
315 struct in6_addr llsol;
316
317 if ((ia_lan->ia_ifp->if_flags &
318 (IFF_BROADCAST|IFF_MULTICAST)) !=
319 (IFF_BROADCAST|IFF_MULTICAST))
320 continue;
321 if (!IN6_ARE_MASKED_ADDR_EQUAL(IA6_IN6(ia),
322 IA6_IN6(ia_lan),
323 IA6_MASKIN6(ia_lan)))
324 continue;
325 if (ia_lan->ia_ifp == ia->ia_ifp)
326 continue;
327
328 /* init llsol */
329 bzero(&llsol, sizeof(struct in6_addr));
330 llsol.s6_addr16[0] = htons(0xff02);
331 llsol.s6_addr16[1] = htons(ia_lan->ia_ifp->if_index);
332 llsol.s6_addr32[1] = 0;
333 llsol.s6_addr32[2] = htonl(1);
334 llsol.s6_addr32[3] =
335 ia->ia_dstaddr.sin6_addr.s6_addr32[3];
336 llsol.s6_addr8[12] = 0xff;
337
338 if (cmd == 1)
339 (void)in6_addmulti(&llsol,
340 ia_lan->ia_ifp,
341 &error);
342 else if (cmd == 2) {
343 struct in6_multi *in6m;
344
345 IN6_LOOKUP_MULTI(llsol,
346 ia_lan->ia_ifp,
347 in6m);
348 if (in6m)
349 in6_delmulti(in6m);
350 }
351 }
352 }
353 return error;
354 }
355
356 static int
357 in6_ifaddproxy(struct in6_ifaddr *ia)
358 {
359 return(in6_ifproxy_request(1, ia));
360 }
361
362 static void
363 in6_ifremproxy(struct in6_ifaddr *ia)
364 {
365 in6_ifproxy_request(2, ia);
366 }
367
368 int
369 in6_mask2len(mask)
370 struct in6_addr *mask;
371 {
372 int x, y;
373
374 for (x = 0; x < sizeof(*mask); x++) {
375 if (mask->s6_addr8[x] != 0xff)
376 break;
377 }
378 y = 0;
379 if (x < sizeof(*mask)) {
380 for (y = 0; y < 8; y++) {
381 if ((mask->s6_addr8[x] & (0x80 >> y)) == 0)
382 break;
383 }
384 }
385 return x * 8 + y;
386 }
387
388 void
389 in6_len2mask(mask, len)
390 struct in6_addr *mask;
391 int len;
392 {
393 int i;
394
395 bzero(mask, sizeof(*mask));
396 for (i = 0; i < len / 8; i++)
397 mask->s6_addr8[i] = 0xff;
398 if (len % 8)
399 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
400 }
401
402 int in6_interfaces; /* number of external internet interfaces */
403
404 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
405 #define ia62ifa(ia6) ((struct ifaddr *)(ia6))
406
407 int
408 in6_control(so, cmd, data, ifp, p)
409 struct socket *so;
410 u_long cmd;
411 caddr_t data;
412 struct ifnet *ifp;
413 struct proc *p;
414 {
415 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
416 struct in6_ifaddr *ia, *oia;
417 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
418 struct sockaddr_in6 oldaddr, net;
419 int error = 0, hostIsNew, prefixIsNew;
420
421 /*
422 * xxx should prevent processes for link-local addresses?
423 */
424 #if NGIF > 0
425 if (ifp && ifp->if_type == IFT_GIF) {
426 switch (cmd) {
427 case SIOCSIFPHYADDR_IN6:
428 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
429 return(EPERM);
430 /*fall through*/
431 case SIOCGIFPSRCADDR_IN6:
432 case SIOCGIFPDSTADDR_IN6:
433 return gif_ioctl(ifp, cmd, data);
434 }
435 }
436 #endif
437 switch (cmd) {
438 case SIOCGETSGCNT_IN6:
439 case SIOCGETMIFCNT_IN6:
440 return (mrt6_ioctl(cmd, data));
441 }
442
443 if (ifp == 0)
444 return(EOPNOTSUPP);
445
446 switch (cmd) {
447 case SIOCSNDFLUSH_IN6:
448 case SIOCSPFXFLUSH_IN6:
449 case SIOCSRTRFLUSH_IN6:
450 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
451 return(EPERM);
452 /*fall through*/
453 case SIOCGIFINFO_IN6:
454 case SIOCGDRLST_IN6:
455 case SIOCGPRLST_IN6:
456 case SIOCGNBRINFO_IN6:
457 return(nd6_ioctl(cmd, data, ifp));
458 }
459
460 switch (cmd) {
461 case SIOCSIFPREFIX_IN6:
462 case SIOCDIFPREFIX_IN6:
463 case SIOCAIFPREFIX_IN6:
464 case SIOCCIFPREFIX_IN6:
465 case SIOCSGIFPREFIX_IN6:
466 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
467 return(EPERM);
468 /*fall through*/
469 case SIOCGIFPREFIX_IN6:
470 return(in6_prefix_ioctl(cmd, data, ifp));
471 }
472
473 switch (cmd) {
474 case SIOCALIFADDR:
475 case SIOCDLIFADDR:
476 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
477 return(EPERM);
478 /*fall through*/
479 case SIOCGLIFADDR:
480 return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
481 }
482
483 /*
484 * Find address for this interface, if it exists.
485 */
486 {
487 struct in6_addr *addr = &ifra->ifra_addr.sin6_addr;
488 if (IN6_IS_ADDR_LINKLOCAL(addr)) {
489 if (addr->s6_addr16[1] == 0) {
490 /* interface ID is not embedded by the user */
491 addr->s6_addr16[1] = htons(ifp->if_index);
492 }
493 else
494 if (addr->s6_addr16[1] != htons(ifp->if_index))
495 return(EINVAL); /* ifid is contradict */
496 }
497 }
498 #if 0
499 if (ifra->ifra_addr.sin6_family == AF_INET6) {
500 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
501 }
502 #else
503 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
504 #endif
505
506 switch (cmd) {
507
508 case SIOCDIFADDR_IN6:
509 if (ia == 0)
510 return(EADDRNOTAVAIL);
511 /* FALLTHROUGH */
512 case SIOCAIFADDR_IN6:
513 case SIOCSIFADDR_IN6:
514 case SIOCSIFNETMASK_IN6:
515 case SIOCSIFDSTADDR_IN6:
516 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
517 return(EPERM);
518 if (ia == 0) {
519 ia = (struct in6_ifaddr *)
520 malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
521 if (ia == NULL)
522 return (ENOBUFS);
523 bzero((caddr_t)ia, sizeof(*ia));
524 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
525 ia->ia_ifa.ifa_dstaddr
526 = (struct sockaddr *)&ia->ia_dstaddr;
527 ia->ia_ifa.ifa_netmask
528 = (struct sockaddr *)&ia->ia_prefixmask;
529
530 ia->ia_ifp = ifp;
531 if ((oia = in6_ifaddr) != NULL) {
532 for ( ; oia->ia_next; oia = oia->ia_next)
533 continue;
534 oia->ia_next = ia;
535 } else
536 in6_ifaddr = ia;
537 #if 0 /* this search seems to be meaningless */
538 if ((ifa = ifp->if_addrlist.tqh_first) != NULL) {
539 for ( ; ifa->ifa_list.tqe_next; ifa = ifa->ifa_list.tqe_next)
540 continue;
541 }
542 #endif
543 TAILQ_INSERT_TAIL(&ifp->if_addrlist,
544 (struct ifaddr *)ia, ifa_list);
545 if ((ifp->if_flags & IFF_LOOPBACK) == 0)
546 in6_interfaces++; /*XXX*/
547 }
548
549 if (cmd == SIOCAIFADDR_IN6) {
550 /* sanity for overflow - beware unsigned */
551 struct in6_addrlifetime *lt;
552 lt = &ifra->ifra_lifetime;
553 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
554 && lt->ia6t_vltime + time_second < time_second) {
555 return EINVAL;
556 }
557 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
558 && lt->ia6t_pltime + time_second < time_second) {
559 return EINVAL;
560 }
561 }
562 break;
563
564 case SIOCGIFADDR_IN6:
565 /* This interface is basically deprecated. use SIOCGIFCONF. */
566 /* fall through */
567 case SIOCGIFAFLAG_IN6:
568 case SIOCGIFNETMASK_IN6:
569 case SIOCGIFDSTADDR_IN6:
570 /* must think again about its semantics */
571 if (ia == 0)
572 return(EADDRNOTAVAIL);
573 break;
574 case SIOCSIFALIFETIME_IN6:
575 {
576 struct in6_addrlifetime *lt;
577
578 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
579 return(EPERM);
580 if (ia == 0)
581 return(EADDRNOTAVAIL);
582 /* sanity for overflow - beware unsigned */
583 lt = &ifr->ifr_ifru.ifru_lifetime;
584 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
585 && lt->ia6t_vltime + time_second < time_second) {
586 return EINVAL;
587 }
588 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
589 && lt->ia6t_pltime + time_second < time_second) {
590 return EINVAL;
591 }
592 break;
593 }
594 }
595
596 switch (cmd) {
597
598 case SIOCGIFADDR_IN6:
599 ifr->ifr_addr = ia->ia_addr;
600 break;
601
602 case SIOCGIFDSTADDR_IN6:
603 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
604 return(EINVAL);
605 ifr->ifr_dstaddr = ia->ia_dstaddr;
606 break;
607
608 case SIOCGIFNETMASK_IN6:
609 ifr->ifr_addr = ia->ia_prefixmask;
610 break;
611
612 case SIOCGIFAFLAG_IN6:
613 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
614 break;
615
616 case SIOCSIFDSTADDR_IN6:
617 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
618 return(EINVAL);
619 oldaddr = ia->ia_dstaddr;
620 ia->ia_dstaddr = ifr->ifr_dstaddr;
621
622 /* link-local index check */
623 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
624 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
625 /* interface ID is not embedded by the user */
626 ia->ia_dstaddr.sin6_addr.s6_addr16[1]
627 = htons(ifp->if_index);
628 }
629 else
630 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
631 htons(ifp->if_index)) {
632 ia->ia_dstaddr = oldaddr;
633 return(EINVAL); /* ifid is contradict */
634 }
635 }
636
637 if (ifp->if_ioctl && (error = (ifp->if_ioctl)
638 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
639 ia->ia_dstaddr = oldaddr;
640 return(error);
641 }
642 if (ia->ia_flags & IFA_ROUTE) {
643 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
644 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
645 ia->ia_ifa.ifa_dstaddr =
646 (struct sockaddr *)&ia->ia_dstaddr;
647 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
648 }
649 break;
650
651 case SIOCGIFALIFETIME_IN6:
652 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
653 break;
654
655 case SIOCSIFALIFETIME_IN6:
656 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
657 /* for sanity */
658 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
659 ia->ia6_lifetime.ia6t_expire =
660 time_second + ia->ia6_lifetime.ia6t_vltime;
661 } else
662 ia->ia6_lifetime.ia6t_expire = 0;
663 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
664 ia->ia6_lifetime.ia6t_preferred =
665 time_second + ia->ia6_lifetime.ia6t_pltime;
666 } else
667 ia->ia6_lifetime.ia6t_preferred = 0;
668 break;
669
670 case SIOCSIFADDR_IN6:
671 return(in6_ifinit(ifp, ia, &ifr->ifr_addr, 1));
672
673 case SIOCSIFNETMASK_IN6:
674 ia->ia_prefixmask = ifr->ifr_addr;
675 bzero(&net, sizeof(net));
676 net.sin6_len = sizeof(struct sockaddr_in6);
677 net.sin6_family = AF_INET6;
678 net.sin6_port = htons(0);
679 net.sin6_flowinfo = htonl(0);
680 net.sin6_addr.s6_addr32[0]
681 = ia->ia_addr.sin6_addr.s6_addr32[0] &
682 ia->ia_prefixmask.sin6_addr.s6_addr32[0];
683 net.sin6_addr.s6_addr32[1]
684 = ia->ia_addr.sin6_addr.s6_addr32[1] &
685 ia->ia_prefixmask.sin6_addr.s6_addr32[1];
686 net.sin6_addr.s6_addr32[2]
687 = ia->ia_addr.sin6_addr.s6_addr32[2] &
688 ia->ia_prefixmask.sin6_addr.s6_addr32[2];
689 net.sin6_addr.s6_addr32[3]
690 = ia->ia_addr.sin6_addr.s6_addr32[3] &
691 ia->ia_prefixmask.sin6_addr.s6_addr32[3];
692 ia->ia_net = net;
693 break;
694
695 case SIOCAIFADDR_IN6:
696 prefixIsNew = 0;
697 hostIsNew = 1;
698
699 if (ifra->ifra_addr.sin6_len == 0) {
700 ifra->ifra_addr = ia->ia_addr;
701 hostIsNew = 0;
702 } else if (IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr,
703 &ia->ia_addr.sin6_addr))
704 hostIsNew = 0;
705
706 if (ifra->ifra_prefixmask.sin6_len) {
707 in6_ifscrub(ifp, ia);
708 ia->ia_prefixmask = ifra->ifra_prefixmask;
709 prefixIsNew = 1;
710 }
711 if ((ifp->if_flags & IFF_POINTOPOINT) &&
712 (ifra->ifra_dstaddr.sin6_family == AF_INET6)) {
713 in6_ifscrub(ifp, ia);
714 ia->ia_dstaddr = ifra->ifra_dstaddr;
715 /* link-local index check: should be a separate function? */
716 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
717 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
718 /*
719 * interface ID is not embedded by
720 * the user
721 */
722 ia->ia_dstaddr.sin6_addr.s6_addr16[1]
723 = htons(ifp->if_index);
724 }
725 else
726 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
727 htons(ifp->if_index)) {
728 ia->ia_dstaddr = oldaddr;
729 return(EINVAL); /* ifid is contradict */
730 }
731 }
732 prefixIsNew = 1; /* We lie; but effect's the same */
733 }
734 if (ifra->ifra_addr.sin6_family == AF_INET6 &&
735 (hostIsNew || prefixIsNew))
736 error = in6_ifinit(ifp, ia, &ifra->ifra_addr, 0);
737 if (ifra->ifra_addr.sin6_family == AF_INET6
738 && hostIsNew && (ifp->if_flags & IFF_MULTICAST)) {
739 int error_local = 0;
740
741 /*
742 * join solicited multicast addr for new host id
743 */
744 struct in6_addr llsol;
745 bzero(&llsol, sizeof(struct in6_addr));
746 llsol.s6_addr16[0] = htons(0xff02);
747 llsol.s6_addr16[1] = htons(ifp->if_index);
748 llsol.s6_addr32[1] = 0;
749 llsol.s6_addr32[2] = htonl(1);
750 llsol.s6_addr32[3] =
751 ifra->ifra_addr.sin6_addr.s6_addr32[3];
752 llsol.s6_addr8[12] = 0xff;
753 (void)in6_addmulti(&llsol, ifp, &error_local);
754 if (error == 0)
755 error = error_local;
756 }
757 /* Join dstaddr's solicited multicast if necessary. */
758 if (nd6_proxyall && hostIsNew) {
759 int error_local;
760
761 error_local = in6_ifaddproxy(ia);
762 if (error == 0)
763 error = error_local;
764 }
765
766 ia->ia6_flags = ifra->ifra_flags;
767 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
768
769 ia->ia6_lifetime = ifra->ifra_lifetime;
770 /* for sanity */
771 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
772 ia->ia6_lifetime.ia6t_expire =
773 time_second + ia->ia6_lifetime.ia6t_vltime;
774 } else
775 ia->ia6_lifetime.ia6t_expire = 0;
776 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
777 ia->ia6_lifetime.ia6t_preferred =
778 time_second + ia->ia6_lifetime.ia6t_pltime;
779 } else
780 ia->ia6_lifetime.ia6t_preferred = 0;
781
782 /*
783 * Perform DAD, if needed.
784 * XXX It may be of use, if we can administratively
785 * disable DAD.
786 */
787 switch (ifp->if_type) {
788 case IFT_ARCNET:
789 case IFT_ETHER:
790 case IFT_FDDI:
791 #if 0
792 case IFT_ATM:
793 case IFT_SLIP:
794 case IFT_PPP:
795 #endif
796 ia->ia6_flags |= IN6_IFF_TENTATIVE;
797 nd6_dad_start((struct ifaddr *)ia, NULL);
798 break;
799 case IFT_GIF:
800 case IFT_LOOP:
801 default:
802 break;
803 }
804
805 return(error);
806
807 case SIOCDIFADDR_IN6:
808 in6_ifscrub(ifp, ia);
809
810 if (ifp->if_flags & IFF_MULTICAST) {
811 /*
812 * delete solicited multicast addr for deleting host id
813 */
814 struct in6_multi *in6m;
815 struct in6_addr llsol;
816 bzero(&llsol, sizeof(struct in6_addr));
817 llsol.s6_addr16[0] = htons(0xff02);
818 llsol.s6_addr16[1] = htons(ifp->if_index);
819 llsol.s6_addr32[1] = 0;
820 llsol.s6_addr32[2] = htonl(1);
821 llsol.s6_addr32[3] =
822 ia->ia_addr.sin6_addr.s6_addr32[3];
823 llsol.s6_addr8[12] = 0xff;
824
825 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
826 if (in6m)
827 in6_delmulti(in6m);
828 }
829 /* Leave dstaddr's solicited multicast if necessary. */
830 if (nd6_proxyall)
831 in6_ifremproxy(ia);
832
833 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
834 oia = ia;
835 if (oia == (ia = in6_ifaddr))
836 in6_ifaddr = ia->ia_next;
837 else {
838 while (ia->ia_next && (ia->ia_next != oia))
839 ia = ia->ia_next;
840 if (ia->ia_next)
841 ia->ia_next = oia->ia_next;
842 else
843 printf("Didn't unlink in6_ifaddr from list\n");
844 }
845
846 if (oia->ia6_multiaddrs.lh_first == NULL) {
847 IFAFREE(&oia->ia_ifa);
848 break;
849 }
850 else
851 in6_savemkludge(oia);
852
853 IFAFREE((&oia->ia_ifa));
854 break;
855
856 default:
857 if (ifp == 0 || ifp->if_ioctl == 0)
858 return(EOPNOTSUPP);
859 return((*ifp->if_ioctl)(ifp, cmd, data));
860 }
861 return(0);
862 }
863
864 /*
865 * SIOC[GAD]LIFADDR.
866 * SIOCGLIFADDR: get first address. (???)
867 * SIOCGLIFADDR with IFLR_PREFIX:
868 * get first address that matches the specified prefix.
869 * SIOCALIFADDR: add the specified address.
870 * SIOCALIFADDR with IFLR_PREFIX:
871 * add the specified prefix, filling hostid part from
872 * the first link-local address. prefixlen must be <= 64.
873 * SIOCDLIFADDR: delete the specified address.
874 * SIOCDLIFADDR with IFLR_PREFIX:
875 * delete the first address that matches the specified prefix.
876 * return values:
877 * EINVAL on invalid parameters
878 * EADDRNOTAVAIL on prefix match failed/specified address not found
879 * other values may be returned from in6_ioctl()
880 *
881 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
882 * this is to accomodate address naming scheme other than RFC2374,
883 * in the future.
884 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
885 * address encoding scheme. (see figure on page 8)
886 */
887 static int
888 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
889 struct socket *so;
890 u_long cmd;
891 caddr_t data;
892 struct ifnet *ifp;
893 struct proc *p;
894 {
895 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
896 struct ifaddr *ifa;
897
898 /* sanity checks */
899 if (!data || !ifp) {
900 panic("invalid argument to in6_lifaddr_ioctl");
901 /*NOTRECHED*/
902 }
903
904 switch (cmd) {
905 case SIOCGLIFADDR:
906 /* address must be specified on GET with IFLR_PREFIX */
907 if ((iflr->flags & IFLR_PREFIX) == 0)
908 break;
909 /*FALLTHROUGH*/
910 case SIOCALIFADDR:
911 case SIOCDLIFADDR:
912 /* address must be specified on ADD and DELETE */
913 if (iflr->addr.__ss_family != AF_INET6)
914 return EINVAL;
915 if (iflr->addr.__ss_len != sizeof(struct sockaddr_in6))
916 return EINVAL;
917 /* XXX need improvement */
918 if (iflr->dstaddr.__ss_family
919 && iflr->dstaddr.__ss_family != AF_INET6)
920 return EINVAL;
921 if (iflr->dstaddr.__ss_family
922 && iflr->dstaddr.__ss_len != sizeof(struct sockaddr_in6))
923 return EINVAL;
924 break;
925 default: /*shouldn't happen*/
926 #if 0
927 panic("invalid cmd to in6_lifaddr_ioctl");
928 /*NOTREACHED*/
929 #else
930 return EOPNOTSUPP;
931 #endif
932 }
933 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
934 return EINVAL;
935
936 switch (cmd) {
937 case SIOCALIFADDR:
938 {
939 struct in6_aliasreq ifra;
940 struct in6_addr *hostid = NULL;
941 int prefixlen;
942
943 if ((iflr->flags & IFLR_PREFIX) != 0) {
944 struct sockaddr_in6 *sin6;
945
946 /*
947 * hostid is to fill in the hostid part of the
948 * address. hostid points to the first link-local
949 * address attached to the interface.
950 */
951 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp);
952 if (!ifa)
953 return EADDRNOTAVAIL;
954 hostid = &IFA_IN6(ifa);
955
956 /* prefixlen must be <= 64. */
957 if (64 < iflr->prefixlen)
958 return EINVAL;
959 prefixlen = iflr->prefixlen;
960
961 /* hostid part must be zero. */
962 sin6 = (struct sockaddr_in6 *)&iflr->addr;
963 if (sin6->sin6_addr.s6_addr32[2] != 0
964 || sin6->sin6_addr.s6_addr32[3] != 0) {
965 return EINVAL;
966 }
967 } else
968 prefixlen = iflr->prefixlen;
969
970 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
971 bzero(&ifra, sizeof(ifra));
972 bcopy(iflr->iflr_name, ifra.ifra_name,
973 sizeof(ifra.ifra_name));
974
975 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.__ss_len);
976 if (hostid) {
977 /* fill in hostid part */
978 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
979 hostid->s6_addr32[2];
980 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
981 hostid->s6_addr32[3];
982 }
983
984 if (iflr->dstaddr.__ss_family) { /*XXX*/
985 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
986 iflr->dstaddr.__ss_len);
987 if (hostid) {
988 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
989 hostid->s6_addr32[2];
990 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
991 hostid->s6_addr32[3];
992 }
993 }
994
995 ifra.ifra_prefixmask.sin6_family = AF_INET6;
996 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
997 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
998
999 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1000 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1001 }
1002 case SIOCGLIFADDR:
1003 case SIOCDLIFADDR:
1004 {
1005 struct in6_ifaddr *ia;
1006 struct in6_addr mask, candidate, match;
1007 struct sockaddr_in6 *sin6;
1008 int cmp;
1009
1010 bzero(&mask, sizeof(mask));
1011 if (iflr->flags & IFLR_PREFIX) {
1012 /* lookup a prefix rather than address. */
1013 in6_len2mask(&mask, iflr->prefixlen);
1014
1015 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1016 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1017 match.s6_addr32[0] &= mask.s6_addr32[0];
1018 match.s6_addr32[1] &= mask.s6_addr32[1];
1019 match.s6_addr32[2] &= mask.s6_addr32[2];
1020 match.s6_addr32[3] &= mask.s6_addr32[3];
1021
1022 /* if you set extra bits, that's wrong */
1023 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1024 return EINVAL;
1025
1026 cmp = 1;
1027 } else {
1028 if (cmd == SIOCGLIFADDR) {
1029 /* on getting an address, take the 1st match */
1030 cmp = 0; /*XXX*/
1031 } else {
1032 /* on deleting an address, do exact match */
1033 in6_len2mask(&mask, 128);
1034 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1035 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1036
1037 cmp = 1;
1038 }
1039 }
1040
1041 for (ifa = ifp->if_addrlist.tqh_first;
1042 ifa;
1043 ifa = ifa->ifa_list.tqe_next) {
1044 if (ifa->ifa_addr->sa_family != AF_INET6)
1045 continue;
1046 if (!cmp)
1047 break;
1048 bcopy(&IFA_IN6(ifa), &candidate, sizeof(candidate));
1049 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1050 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1051 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1052 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1053 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1054 break;
1055 }
1056 if (!ifa)
1057 return EADDRNOTAVAIL;
1058 ia = ifa2ia6(ifa);
1059
1060 if (cmd == SIOCGLIFADDR) {
1061 /* fill in the if_laddrreq structure */
1062 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1063
1064 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1065 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1066 ia->ia_dstaddr.sin6_len);
1067 } else
1068 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1069
1070 iflr->prefixlen =
1071 in6_mask2len(&ia->ia_prefixmask.sin6_addr);
1072
1073 iflr->flags = ia->ia6_flags; /*XXX*/
1074
1075 return 0;
1076 } else {
1077 struct in6_aliasreq ifra;
1078
1079 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1080 bzero(&ifra, sizeof(ifra));
1081 bcopy(iflr->iflr_name, ifra.ifra_name,
1082 sizeof(ifra.ifra_name));
1083
1084 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1085 ia->ia_addr.sin6_len);
1086 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1087 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1088 ia->ia_dstaddr.sin6_len);
1089 }
1090 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1091 ia->ia_prefixmask.sin6_len);
1092
1093 ifra.ifra_flags = ia->ia6_flags;
1094 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1095 ifp, p);
1096 }
1097 }
1098 }
1099
1100 return EOPNOTSUPP; /*just for safety*/
1101 }
1102
1103 /*
1104 * Delete any existing route for an interface.
1105 */
1106 void
1107 in6_ifscrub(ifp, ia)
1108 register struct ifnet *ifp;
1109 register struct in6_ifaddr *ia;
1110 {
1111 if ((ia->ia_flags & IFA_ROUTE) == 0)
1112 return;
1113 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
1114 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
1115 else
1116 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
1117 ia->ia_flags &= ~IFA_ROUTE;
1118
1119 /* Remove ownaddr's loopback rtentry, if it exists. */
1120 in6_ifremloop(&(ia->ia_ifa));
1121 }
1122
1123 /*
1124 * Initialize an interface's intetnet6 address
1125 * and routing table entry.
1126 */
1127 int
1128 in6_ifinit(ifp, ia, sin6, scrub)
1129 struct ifnet *ifp;
1130 struct in6_ifaddr *ia;
1131 struct sockaddr_in6 *sin6;
1132 int scrub;
1133 {
1134 struct sockaddr_in6 oldaddr;
1135 int error, flags = RTF_UP;
1136 int s = splimp();
1137
1138 oldaddr = ia->ia_addr;
1139 ia->ia_addr = *sin6;
1140 /*
1141 * Give the interface a chance to initialize
1142 * if this is its first address,
1143 * and to validate the address if necessary.
1144 */
1145 if (ifp->if_ioctl &&
1146 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1147 splx(s);
1148 ia->ia_addr = oldaddr;
1149 return(error);
1150 }
1151
1152 switch (ifp->if_type) {
1153 case IFT_ETHER:
1154 case IFT_FDDI:
1155 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1156 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1157 break;
1158 case IFT_PPP:
1159 ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest;
1160 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1161 break;
1162 }
1163
1164 splx(s);
1165 if (scrub) {
1166 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1167 in6_ifscrub(ifp, ia);
1168 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1169 }
1170 /* xxx
1171 * in_socktrim
1172 */
1173 /*
1174 * Add route for the network.
1175 */
1176 ia->ia_ifa.ifa_metric = ifp->if_metric;
1177 if (ifp->if_flags & IFF_LOOPBACK) {
1178 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1179 flags |= RTF_HOST;
1180 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1181 if (ia->ia_dstaddr.sin6_family != AF_INET6)
1182 return(0);
1183 flags |= RTF_HOST;
1184 }
1185 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
1186 ia->ia_flags |= IFA_ROUTE;
1187
1188 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
1189 in6_ifaddloop(&(ia->ia_ifa));
1190
1191 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
1192 if (ifp->if_flags & IFF_MULTICAST)
1193 in6_restoremkludge(ia, ifp);
1194 #endif
1195
1196 return(error);
1197 }
1198
1199 /*
1200 * Multicast address kludge:
1201 * If there were any multicast addresses attached to this interface address,
1202 * either move them to another address on this interface, or save them until
1203 * such time as this interface is reconfigured for IPv6.
1204 */
1205 void
1206 in6_savemkludge(oia)
1207 struct in6_ifaddr *oia;
1208 {
1209 struct in6_ifaddr *ia;
1210 struct in6_multi *in6m, *next;
1211
1212 IFP_TO_IA6(oia->ia_ifp, ia);
1213 if (ia) { /* there is another address */
1214 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1215 next = in6m->in6m_entry.le_next;
1216 IFAFREE(&in6m->in6m_ia->ia_ifa);
1217 ia->ia_ifa.ifa_refcnt++;
1218 in6m->in6m_ia = ia;
1219 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1220 }
1221 } else { /* last address on this if deleted, save */
1222 struct multi6_kludge *mk;
1223
1224 mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
1225
1226 LIST_INIT(&mk->mk_head);
1227 mk->mk_ifp = oia->ia_ifp;
1228
1229 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1230 next = in6m->in6m_entry.le_next;
1231 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
1232 }
1233
1234 if (mk->mk_head.lh_first != NULL) {
1235 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
1236 }
1237 else {
1238 FREE(mk, M_IPMADDR);
1239 }
1240 }
1241 }
1242
1243 /*
1244 * Continuation of multicast address hack:
1245 * If there was a multicast group list previously saved for this interface,
1246 * then we re-attach it to the first address configured on the i/f.
1247 */
1248 void
1249 in6_restoremkludge(ia, ifp)
1250 struct in6_ifaddr *ia;
1251 struct ifnet *ifp;
1252 {
1253 struct multi6_kludge *mk;
1254
1255 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1256 if (mk->mk_ifp == ifp) {
1257 struct in6_multi *in6m, *next;
1258
1259 for (in6m = mk->mk_head.lh_first; in6m; in6m = next){
1260 next = in6m->in6m_entry.le_next;
1261 LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
1262 in6m, in6m_entry);
1263 }
1264 LIST_REMOVE(mk, mk_entry);
1265 free(mk, M_IPMADDR);
1266 break;
1267 }
1268 }
1269 }
1270
1271 /*
1272 * Add an address to the list of IP6 multicast addresses for a
1273 * given interface.
1274 */
1275 struct in6_multi *
1276 in6_addmulti(maddr6, ifp, errorp)
1277 register struct in6_addr *maddr6;
1278 register struct ifnet *ifp;
1279 int *errorp;
1280 {
1281 struct in6_ifaddr *ia;
1282 struct in6_ifreq ifr;
1283 struct in6_multi *in6m;
1284 int s = splsoftnet();
1285
1286 *errorp = 0;
1287 /*
1288 * See if address already in list.
1289 */
1290 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
1291 if (in6m != NULL) {
1292 /*
1293 * Found it; just increment the refrence count.
1294 */
1295 in6m->in6m_refcount++;
1296 } else {
1297 /*
1298 * New address; allocate a new multicast record
1299 * and link it into the interface's multicast list.
1300 */
1301 in6m = (struct in6_multi *)
1302 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1303 if (in6m == NULL) {
1304 splx(s);
1305 *errorp = ENOBUFS;
1306 return(NULL);
1307 }
1308 in6m->in6m_addr = *maddr6;
1309 in6m->in6m_ifp = ifp;
1310 in6m->in6m_refcount = 1;
1311 IFP_TO_IA6(ifp, ia);
1312 if (ia == NULL) {
1313 free(in6m, M_IPMADDR);
1314 splx(s);
1315 *errorp = EADDRNOTAVAIL; /* appropriate? */
1316 return(NULL);
1317 }
1318 in6m->in6m_ia = ia;
1319 ia->ia_ifa.ifa_refcnt++; /* gain a reference */
1320 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1321
1322 /*
1323 * Ask the network driver to update its multicast reception
1324 * filter appropriately for the new address.
1325 */
1326 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1327 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1328 ifr.ifr_addr.sin6_family = AF_INET6;
1329 ifr.ifr_addr.sin6_addr = *maddr6;
1330 if (ifp->if_ioctl == NULL)
1331 *errorp = ENXIO; /* XXX: appropriate? */
1332 else
1333 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
1334 (caddr_t)&ifr);
1335 if (*errorp) {
1336 LIST_REMOVE(in6m, in6m_entry);
1337 free(in6m, M_IPMADDR);
1338 splx(s);
1339 return(NULL);
1340 }
1341 /*
1342 * Let MLD6 know that we have joined a new IP6 multicast
1343 * group.
1344 */
1345 mld6_start_listening(in6m);
1346 }
1347 splx(s);
1348 return(in6m);
1349 }
1350
1351 /*
1352 * Delete a multicast address record.
1353 */
1354 void
1355 in6_delmulti(in6m)
1356 struct in6_multi *in6m;
1357 {
1358 struct in6_ifreq ifr;
1359 int s = splsoftnet();
1360
1361 if (--in6m->in6m_refcount == 0) {
1362 /*
1363 * No remaining claims to this record; let MLD6 know
1364 * that we are leaving the multicast group.
1365 */
1366 mld6_stop_listening(in6m);
1367
1368 /*
1369 * Unlink from list.
1370 */
1371 LIST_REMOVE(in6m, in6m_entry);
1372 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1373
1374 /*
1375 * Notify the network driver to update its multicast
1376 * reception filter.
1377 */
1378 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1379 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1380 ifr.ifr_addr.sin6_family = AF_INET6;
1381 ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
1382 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
1383 SIOCDELMULTI, (caddr_t)&ifr);
1384 free(in6m, M_IPMADDR);
1385 }
1386 splx(s);
1387 }
1388
1389 /*
1390 * Find an IPv6 interface link-local address specific to an interface.
1391 */
1392 struct in6_ifaddr *
1393 in6ifa_ifpforlinklocal(ifp)
1394 struct ifnet *ifp;
1395 {
1396 register struct ifaddr *ifa;
1397
1398 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) {
1399 if (ifa->ifa_addr == NULL)
1400 continue; /* just for safety */
1401 if (ifa->ifa_addr->sa_family != AF_INET6)
1402 continue;
1403 if (IN6_IS_ADDR_LINKLOCAL(&IFA_IN6(ifa)))
1404 break;
1405 }
1406
1407 return((struct in6_ifaddr *)ifa);
1408 }
1409
1410
1411 /*
1412 * find the internet address corresponding to a given interface and address.
1413 */
1414 struct in6_ifaddr *
1415 in6ifa_ifpwithaddr(ifp, addr)
1416 struct ifnet *ifp;
1417 struct in6_addr *addr;
1418 {
1419 register struct ifaddr *ifa;
1420
1421 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) {
1422 if (ifa->ifa_addr == NULL)
1423 continue; /* just for safety */
1424 if (ifa->ifa_addr->sa_family != AF_INET6)
1425 continue;
1426 if (IN6_ARE_ADDR_EQUAL(addr, &IFA_IN6(ifa)))
1427 break;
1428 }
1429
1430 return((struct in6_ifaddr *)ifa);
1431 }
1432
1433 /*
1434 * Convert IP6 address to printable (loggable) representation.
1435 */
1436 static char digits[] = "0123456789abcdef";
1437 static int ip6round = 0;
1438 char *
1439 ip6_sprintf(addr)
1440 register struct in6_addr *addr;
1441 {
1442 static char ip6buf[8][48];
1443 register int i;
1444 register char *cp;
1445 register u_short *a = (u_short *)addr;
1446 register u_char *d;
1447 int dcolon = 0;
1448
1449 ip6round = (ip6round + 1) & 7;
1450 cp = ip6buf[ip6round];
1451
1452 for (i = 0; i < 8; i++) {
1453 if (dcolon == 1) {
1454 if (*a == 0) {
1455 if (i == 7)
1456 *cp++ = ':';
1457 a++;
1458 continue;
1459 } else
1460 dcolon = 2;
1461 }
1462 if (*a == 0) {
1463 if (dcolon == 0 && *(a + 1) == 0) {
1464 if (i == 0)
1465 *cp++ = ':';
1466 *cp++ = ':';
1467 dcolon = 1;
1468 } else {
1469 *cp++ = '0';
1470 *cp++ = ':';
1471 }
1472 a++;
1473 continue;
1474 }
1475 d = (u_char *)a;
1476 *cp++ = digits[*d >> 4];
1477 *cp++ = digits[*d++ & 0xf];
1478 *cp++ = digits[*d >> 4];
1479 *cp++ = digits[*d & 0xf];
1480 *cp++ = ':';
1481 a++;
1482 }
1483 *--cp = 0;
1484 return(ip6buf[ip6round]);
1485 }
1486
1487 int
1488 in6_localaddr(in6)
1489 struct in6_addr *in6;
1490 {
1491 struct in6_ifaddr *ia;
1492
1493 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1494 return 1;
1495
1496 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1497 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1498 &ia->ia_prefixmask.sin6_addr))
1499 return 1;
1500
1501 return (0);
1502 }
1503
1504 /*
1505 * Get a scope of the address. Node-local, link-local, site-local or global.
1506 */
1507 int
1508 in6_addrscope (addr)
1509 struct in6_addr *addr;
1510 {
1511 int scope;
1512
1513 if (addr->s6_addr8[0] == 0xfe) {
1514 scope = addr->s6_addr8[1] & 0xc0;
1515
1516 switch (scope) {
1517 case 0x80:
1518 return IPV6_ADDR_SCOPE_LINKLOCAL;
1519 break;
1520 case 0xc0:
1521 return IPV6_ADDR_SCOPE_SITELOCAL;
1522 break;
1523 default:
1524 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
1525 break;
1526 }
1527 }
1528
1529
1530 if (addr->s6_addr8[0] == 0xff) {
1531 scope = addr->s6_addr8[1] & 0x0f;
1532
1533 /*
1534 * due to other scope such as reserved,
1535 * return scope doesn't work.
1536 */
1537 switch (scope) {
1538 case IPV6_ADDR_SCOPE_NODELOCAL:
1539 return IPV6_ADDR_SCOPE_NODELOCAL;
1540 break;
1541 case IPV6_ADDR_SCOPE_LINKLOCAL:
1542 return IPV6_ADDR_SCOPE_LINKLOCAL;
1543 break;
1544 case IPV6_ADDR_SCOPE_SITELOCAL:
1545 return IPV6_ADDR_SCOPE_SITELOCAL;
1546 break;
1547 default:
1548 return IPV6_ADDR_SCOPE_GLOBAL;
1549 break;
1550 }
1551 }
1552
1553 if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) {
1554 if (addr->s6_addr8[15] == 1) /* loopback */
1555 return IPV6_ADDR_SCOPE_NODELOCAL;
1556 if (addr->s6_addr8[15] == 0) /* unspecified */
1557 return IPV6_ADDR_SCOPE_LINKLOCAL;
1558 }
1559
1560 return IPV6_ADDR_SCOPE_GLOBAL;
1561 }
1562
1563 /*
1564 * return length of part which dst and src are equal
1565 * hard coding...
1566 */
1567
1568 int
1569 in6_matchlen(src, dst)
1570 struct in6_addr *src, *dst;
1571 {
1572 int match = 0;
1573 u_char *s = (u_char *)src, *d = (u_char *)dst;
1574 u_char *lim = s + 16, r;
1575
1576 while (s < lim)
1577 if ((r = (*d++ ^ *s++)) != 0) {
1578 while (r < 128) {
1579 match++;
1580 r <<= 1;
1581 }
1582 break;
1583 } else
1584 match += 8;
1585 return match;
1586 }
1587
1588 /*
1589 * return the best address out of the same scope
1590 */
1591
1592 struct in6_ifaddr *
1593 in6_ifawithscope(ifp, dst)
1594 register struct ifnet *ifp;
1595 register struct in6_addr *dst;
1596 {
1597 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1598 struct ifaddr *ifa;
1599 struct in6_ifaddr *besta = 0, *ia;
1600
1601 /*
1602 * We first look for addresses in the same scope.
1603 * If there is one, return it.
1604 * If two or more, return one which matches the dst longest.
1605 * If none, return one of global addresses assigned other ifs.
1606 */
1607 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) {
1608 if (ifa->ifa_addr->sa_family != AF_INET6)
1609 continue;
1610 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1611 continue; /* XXX: is there any case to allow anycast? */
1612 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1613 continue; /* don't use this interface */
1614 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1615 continue;
1616 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED)
1617 continue;
1618
1619 if (dst_scope == in6_addrscope(&IFA_IN6(ifa))) {
1620 /*
1621 * call in6_matchlen() as few as possible
1622 */
1623 if (besta) {
1624 if (blen == -1)
1625 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1626 tlen = in6_matchlen(&IFA_IN6(ifa), dst);
1627 if (tlen > blen) {
1628 blen = tlen;
1629 besta = (struct in6_ifaddr *)ifa;
1630 }
1631 } else
1632 besta = (struct in6_ifaddr *)ifa;
1633 }
1634 }
1635 if (besta)
1636 return(besta);
1637
1638 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1639 if (IPV6_ADDR_SCOPE_GLOBAL ==
1640 in6_addrscope(&(ia->ia_addr.sin6_addr))
1641 && (ia->ia6_flags & IN6_IFF_ANYCAST) == 0
1642 && (ia->ia6_flags & IN6_IFF_NOTREADY) == 0
1643 && (ia->ia6_flags & IN6_IFF_DETACHED) == 0
1644 && (ia->ia6_flags & IN6_IFF_DEPRECATED) == 0) {
1645 /* XXX: is there any case to allow anycast? */
1646 return ia;
1647 }
1648 }
1649
1650 return NULL;
1651 }
1652
1653 /*
1654 * return the best address out of the same scope. if no address was
1655 * found, return the first valid address from designated IF.
1656 */
1657
1658 struct in6_ifaddr *
1659 in6_ifawithifp(ifp, dst)
1660 register struct ifnet *ifp;
1661 register struct in6_addr *dst;
1662 {
1663 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1664 struct ifaddr *ifa;
1665 struct in6_ifaddr *besta = 0;
1666
1667 /*
1668 * We first look for addresses in the same scope.
1669 * If there is one, return it.
1670 * If two or more, return one which matches the dst longest.
1671 * If none, return one of global addresses assigned other ifs.
1672 */
1673 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) {
1674 if (ifa->ifa_addr->sa_family != AF_INET6)
1675 continue;
1676 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1677 continue; /* XXX: is there any case to allow anycast? */
1678 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1679 continue; /* don't use this interface */
1680 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1681 continue;
1682 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED)
1683 continue;
1684
1685 if (dst_scope == in6_addrscope(&IFA_IN6(ifa))) {
1686 /*
1687 * call in6_matchlen() as few as possible
1688 */
1689 if (besta) {
1690 if (blen == -1)
1691 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1692 tlen = in6_matchlen(&IFA_IN6(ifa), dst);
1693 if (tlen > blen) {
1694 blen = tlen;
1695 besta = (struct in6_ifaddr *)ifa;
1696 }
1697 } else
1698 besta = (struct in6_ifaddr *)ifa;
1699 }
1700 }
1701 if (besta)
1702 return(besta);
1703
1704 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) {
1705 if (ifa->ifa_addr->sa_family != AF_INET6)
1706 continue;
1707 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1708 continue; /* XXX: is there any case to allow anycast? */
1709 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1710 continue; /* don't use this interface */
1711 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1712 continue;
1713 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED)
1714 continue;
1715
1716 return (struct in6_ifaddr *)ifa;
1717 }
1718
1719 return NULL;
1720 }
1721
1722 /*
1723 * perform DAD when interface becomes IFF_UP.
1724 */
1725 void
1726 in6_if_up(ifp)
1727 struct ifnet *ifp;
1728 {
1729 struct ifaddr *ifa;
1730 struct in6_ifaddr *ia;
1731 struct sockaddr_dl *sdl;
1732 int type;
1733 struct ether_addr ea;
1734 int off;
1735 int dad_delay; /* delay ticks before DAD output */
1736
1737 bzero(&ea, sizeof(ea));
1738 sdl = NULL;
1739
1740 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) {
1741 if (ifa->ifa_addr->sa_family == AF_INET6
1742 && IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
1743 goto dad;
1744 }
1745 if (ifa->ifa_addr->sa_family != AF_LINK)
1746 continue;
1747 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1748 break;
1749 }
1750
1751 switch (ifp->if_type) {
1752 case IFT_SLIP:
1753 case IFT_PPP:
1754 case IFT_GIF:
1755 case IFT_FAITH:
1756 type = IN6_IFT_P2P;
1757 in6_ifattach(ifp, type, 0, 1);
1758 break;
1759 case IFT_ETHER:
1760 case IFT_FDDI:
1761 case IFT_ATM:
1762 type = IN6_IFT_802;
1763 if (sdl == NULL)
1764 break;
1765 off = sdl->sdl_nlen;
1766 if (bcmp(&sdl->sdl_data[off], &ea, sizeof(ea)) != 0)
1767 in6_ifattach(ifp, type, LLADDR(sdl), 0);
1768 break;
1769 case IFT_ARCNET:
1770 type = IN6_IFT_ARCNET;
1771 if (sdl == NULL)
1772 break;
1773 off = sdl->sdl_nlen;
1774 if (sdl->sdl_data[off] != 0) /* XXX ?: */
1775 in6_ifattach(ifp, type, LLADDR(sdl), 0);
1776 break;
1777 default:
1778 break;
1779 }
1780
1781 dad:
1782 dad_delay = 0;
1783 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) {
1784 if (ifa->ifa_addr->sa_family != AF_INET6)
1785 continue;
1786 ia = (struct in6_ifaddr *)ifa;
1787 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
1788 nd6_dad_start(ifa, &dad_delay);
1789 }
1790 }
1791
1792 /*
1793 * Calculate max IPv6 MTU through all the interfaces and store it
1794 * to in6_maxmtu.
1795 */
1796 void
1797 in6_setmaxmtu()
1798 {
1799 unsigned long maxmtu = 0;
1800 struct ifnet *ifp;
1801
1802 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1803 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
1804 nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
1805 maxmtu = nd_ifinfo[ifp->if_index].linkmtu;
1806 }
1807 if (maxmtu) /* update only when maxmtu is positive */
1808 in6_maxmtu = maxmtu;
1809 }
1810