1 /** $MirOS: src/sys/netinet6/in6.c,v 1.8 2010/09/21 17:42:54 tg Exp $ */
2 /* $OpenBSD: in6.c,v 1.60 2004/10/07 12:08:25 henning Exp $ */
3 /* $KAME: in6.c,v 1.372 2004/06/14 08:14:21 itojun Exp $ */
4
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 /*
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)in.c 8.2 (Berkeley) 11/15/93
63 */
64
65 #include <sys/param.h>
66 #include <sys/ioctl.h>
67 #include <sys/errno.h>
68 #include <sys/malloc.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
71 #include <sys/sockio.h>
72 #include <sys/systm.h>
73 #include <sys/proc.h>
74 #include <sys/time.h>
75 #include <sys/kernel.h>
76 #include <sys/syslog.h>
77
78 #include <dev/rndvar.h>
79
80 #include <net/if.h>
81 #include <net/if_types.h>
82 #include <net/route.h>
83 #include <net/if_dl.h>
84
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/if_ether.h>
88
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet6/nd6.h>
92 #include <netinet6/mld6_var.h>
93 #include <netinet6/ip6_mroute.h>
94 #include <netinet6/in6_ifattach.h>
95
96 /* backward compatibility for a while... */
97 #define COMPAT_IN6IFIOCTL
98
99 /*
100 * Definitions of some costant IP6 addresses.
101 */
102 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
103 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
104 const struct in6_addr in6addr_nodelocal_allnodes =
105 IN6ADDR_NODELOCAL_ALLNODES_INIT;
106 const struct in6_addr in6addr_linklocal_allnodes =
107 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
108 const struct in6_addr in6addr_linklocal_allrouters =
109 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
110
111 const struct in6_addr in6mask0 = IN6MASK0;
112 const struct in6_addr in6mask32 = IN6MASK32;
113 const struct in6_addr in6mask64 = IN6MASK64;
114 const struct in6_addr in6mask96 = IN6MASK96;
115 const struct in6_addr in6mask128 = IN6MASK128;
116
117 static int in6_lifaddr_ioctl(struct socket *, u_long, caddr_t,
118 struct ifnet *, struct proc *);
119 static int in6_ifinit(struct ifnet *, struct in6_ifaddr *,
120 struct sockaddr_in6 *, int);
121 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
122
123 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
124 0, 0, IN6ADDR_ANY_INIT, 0};
125
126 /*
127 * This structure is used to keep track of in6_multi chains which belong to
128 * deleted interface addresses.
129 */
130 static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
131
132 struct multi6_kludge {
133 LIST_ENTRY(multi6_kludge) mk_entry;
134 struct ifnet *mk_ifp;
135 struct in6_multihead mk_head;
136 };
137
138 /*
139 * Subroutine for in6_ifaddloop() and in6_ifremloop().
140 * This routine does actual work.
141 */
142 static void
in6_ifloop_request(int cmd,struct ifaddr * ifa)143 in6_ifloop_request(int cmd, struct ifaddr *ifa)
144 {
145 struct sockaddr_in6 lo_sa;
146 struct sockaddr_in6 all1_sa;
147 struct rtentry *nrt = NULL;
148 int e;
149
150 bzero(&lo_sa, sizeof(lo_sa));
151 bzero(&all1_sa, sizeof(all1_sa));
152 lo_sa.sin6_family = all1_sa.sin6_family = AF_INET6;
153 lo_sa.sin6_len = all1_sa.sin6_len = sizeof(struct sockaddr_in6);
154 lo_sa.sin6_addr = in6addr_loopback;
155 all1_sa.sin6_addr = in6mask128;
156
157 /*
158 * We specify the address itself as the gateway, and set the
159 * RTF_LLINFO flag, so that the corresponding host route would have
160 * the flag, and thus applications that assume traditional behavior
161 * would be happy. Note that we assume the caller of the function
162 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
163 * which changes the outgoing interface to the loopback interface.
164 */
165 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
166 (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
167 if (e != 0) {
168 log(LOG_ERR, "in6_ifloop_request: "
169 "%s operation failed for %s (errno=%d)\n",
170 cmd == RTM_ADD ? "ADD" : "DELETE",
171 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
172 e);
173 }
174
175 /*
176 * Make sure rt_ifa be equal to IFA, the second argument of the
177 * function.
178 * We need this because when we refer to rt_ifa->ia6_flags in
179 * ip6_input, we assume that the rt_ifa points to the address instead
180 * of the loopback address.
181 */
182 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
183 IFAFREE(nrt->rt_ifa);
184 ifa->ifa_refcnt++;
185 nrt->rt_ifa = ifa;
186 }
187
188 /*
189 * Report the addition/removal of the address to the routing socket.
190 * XXX: since we called rtinit for a p2p interface with a destination,
191 * we end up reporting twice in such a case. Should we rather
192 * omit the second report?
193 */
194 if (nrt) {
195 rt_newaddrmsg(cmd, ifa, e, nrt);
196 if (cmd == RTM_DELETE) {
197 if (nrt->rt_refcnt <= 0) {
198 /* XXX: we should free the entry ourselves. */
199 nrt->rt_refcnt++;
200 rtfree(nrt);
201 }
202 } else {
203 /* the cmd must be RTM_ADD here */
204 nrt->rt_refcnt--;
205 }
206 }
207 }
208
209 /*
210 * Add ownaddr as loopback rtentry. We previously add the route only if
211 * necessary (ex. on a p2p link). However, since we now manage addresses
212 * separately from prefixes, we should always add the route. We can't
213 * rely on the cloning mechanism from the corresponding interface route
214 * any more.
215 */
216 void
in6_ifaddloop(struct ifaddr * ifa)217 in6_ifaddloop(struct ifaddr *ifa)
218 {
219 struct rtentry *rt;
220
221 /* If there is no loopback entry, allocate one. */
222 rt = rtalloc1(ifa->ifa_addr, 0);
223 if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
224 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
225 in6_ifloop_request(RTM_ADD, ifa);
226 if (rt)
227 rt->rt_refcnt--;
228 }
229
230 /*
231 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
232 * if it exists.
233 */
234 void
in6_ifremloop(struct ifaddr * ifa)235 in6_ifremloop(struct ifaddr *ifa)
236 {
237 struct in6_ifaddr *ia;
238 struct rtentry *rt;
239 int ia_count = 0;
240
241 /*
242 * Some of BSD variants do not remove cloned routes
243 * from an interface direct route, when removing the direct route
244 * (see comments in net/net_osdep.h). Even for variants that do remove
245 * cloned routes, they could fail to remove the cloned routes when
246 * we handle multple addresses that share a common prefix.
247 * So, we should remove the route corresponding to the deleted address.
248 */
249
250 /*
251 * Delete the entry only if exact one ifa exists. More than one ifa
252 * can exist if we assign a same single address to multiple
253 * (probably p2p) interfaces.
254 * XXX: we should avoid such a configuration in IPv6...
255 */
256 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
257 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
258 ia_count++;
259 if (ia_count > 1)
260 break;
261 }
262 }
263
264 if (ia_count == 1) {
265 /*
266 * Before deleting, check if a corresponding loopbacked host
267 * route surely exists. With this check, we can avoid to
268 * delete an interface direct route whose destination is same
269 * as the address being removed. This can happen when removing
270 * a subnet-router anycast address on an interface attahced
271 * to a shared medium.
272 */
273 rt = rtalloc1(ifa->ifa_addr, 0);
274 if (rt != NULL && (rt->rt_flags & RTF_HOST) != 0 &&
275 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
276 rt->rt_refcnt--;
277 in6_ifloop_request(RTM_DELETE, ifa);
278 }
279 }
280 }
281
282 int
in6_ifindex2scopeid(idx)283 in6_ifindex2scopeid(idx)
284 int idx;
285 {
286 struct ifnet *ifp;
287 struct ifaddr *ifa;
288 struct sockaddr_in6 *sin6;
289
290 if (idx < 0 || if_indexlim <= idx)
291 return -1;
292 ifp = ifindex2ifnet[idx];
293 if (!ifp)
294 return -1;
295
296 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
297 {
298 if (ifa->ifa_addr->sa_family != AF_INET6)
299 continue;
300 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
301 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
302 return sin6->sin6_scope_id & 0xffff;
303 }
304
305 return -1;
306 }
307
308 int
in6_mask2len(mask,lim0)309 in6_mask2len(mask, lim0)
310 struct in6_addr *mask;
311 u_char *lim0;
312 {
313 int x = 0, y;
314 u_char *lim = lim0, *p;
315
316 /* ignore the scope_id part */
317 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
318 lim = (u_char *)mask + sizeof(*mask);
319 for (p = (u_char *)mask; p < lim; x++, p++) {
320 if (*p != 0xff)
321 break;
322 }
323 y = 0;
324 if (p < lim) {
325 for (y = 0; y < 8; y++) {
326 if ((*p & (0x80 >> y)) == 0)
327 break;
328 }
329 }
330
331 /*
332 * when the limit pointer is given, do a stricter check on the
333 * remaining bits.
334 */
335 if (p < lim) {
336 if (y != 0 && (*p & (0x00ff >> y)) != 0)
337 return (-1);
338 for (p = p + 1; p < lim; p++)
339 if (*p != 0)
340 return (-1);
341 }
342
343 return x * 8 + y;
344 }
345
346 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
347 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
348
349 int
in6_control(so,cmd,data,ifp,p)350 in6_control(so, cmd, data, ifp, p)
351 struct socket *so;
352 u_long cmd;
353 caddr_t data;
354 struct ifnet *ifp;
355 struct proc *p;
356 {
357 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
358 struct in6_ifaddr *ia = NULL;
359 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
360 struct sockaddr_in6 *sa6;
361 int privileged;
362
363 privileged = 0;
364 if ((so->so_state & SS_PRIV) != 0)
365 privileged++;
366
367 switch (cmd) {
368 case SIOCGETSGCNT_IN6:
369 case SIOCGETMIFCNT_IN6:
370 return (mrt6_ioctl(cmd, data));
371 }
372
373 if (ifp == NULL)
374 return (EOPNOTSUPP);
375
376 switch (cmd) {
377 case SIOCSNDFLUSH_IN6:
378 case SIOCSPFXFLUSH_IN6:
379 case SIOCSRTRFLUSH_IN6:
380 case SIOCSDEFIFACE_IN6:
381 case SIOCSIFINFO_FLAGS:
382 if (!privileged)
383 return (EPERM);
384 /* FALLTHROUGH */
385 case OSIOCGIFINFO_IN6:
386 case SIOCGIFINFO_IN6:
387 case SIOCGDRLST_IN6:
388 case SIOCGPRLST_IN6:
389 case SIOCGNBRINFO_IN6:
390 case SIOCGDEFIFACE_IN6:
391 return (nd6_ioctl(cmd, data, ifp));
392 }
393
394 switch (cmd) {
395 case SIOCSIFPREFIX_IN6:
396 case SIOCDIFPREFIX_IN6:
397 case SIOCAIFPREFIX_IN6:
398 case SIOCCIFPREFIX_IN6:
399 case SIOCSGIFPREFIX_IN6:
400 case SIOCGIFPREFIX_IN6:
401 log(LOG_NOTICE,
402 "prefix ioctls are now invalidated. "
403 "please use ifconfig.\n");
404 return (EOPNOTSUPP);
405 }
406
407 switch (cmd) {
408 case SIOCALIFADDR:
409 case SIOCDLIFADDR:
410 if (!privileged)
411 return (EPERM);
412 /* FALLTHROUGH */
413 case SIOCGLIFADDR:
414 return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
415 }
416
417 /*
418 * Find address for this interface, if it exists.
419 *
420 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
421 * only, and used the first interface address as the target of other
422 * operations (without checking ifra_addr). This was because netinet
423 * code/API assumed at most 1 interface address per interface.
424 * Since IPv6 allows a node to assign multiple addresses
425 * on a single interface, we almost always look and check the
426 * presence of ifra_addr, and reject invalid ones here.
427 * It also decreases duplicated code among SIOC*_IN6 operations.
428 */
429 switch (cmd) {
430 case SIOCAIFADDR_IN6:
431 case SIOCSIFPHYADDR_IN6:
432 sa6 = &ifra->ifra_addr;
433 break;
434 case SIOCSIFADDR_IN6:
435 case SIOCGIFADDR_IN6:
436 case SIOCSIFDSTADDR_IN6:
437 case SIOCSIFNETMASK_IN6:
438 case SIOCGIFDSTADDR_IN6:
439 case SIOCGIFNETMASK_IN6:
440 case SIOCDIFADDR_IN6:
441 case SIOCGIFPSRCADDR_IN6:
442 case SIOCGIFPDSTADDR_IN6:
443 case SIOCGIFAFLAG_IN6:
444 case SIOCSNDFLUSH_IN6:
445 case SIOCSPFXFLUSH_IN6:
446 case SIOCSRTRFLUSH_IN6:
447 case SIOCGIFALIFETIME_IN6:
448 case SIOCSIFALIFETIME_IN6:
449 case SIOCGIFSTAT_IN6:
450 case SIOCGIFSTAT_ICMP6:
451 sa6 = &ifr->ifr_addr;
452 break;
453 default:
454 sa6 = NULL;
455 break;
456 }
457 if (sa6 && sa6->sin6_family == AF_INET6) {
458 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
459 if (sa6->sin6_addr.s6_addr16[1] == 0) {
460 /* link ID is not embedded by the user */
461 sa6->sin6_addr.s6_addr16[1] =
462 htons(ifp->if_index);
463 } else if (sa6->sin6_addr.s6_addr16[1] !=
464 htons(ifp->if_index)) {
465 return (EINVAL); /* link ID contradicts */
466 }
467 if (sa6->sin6_scope_id) {
468 if (sa6->sin6_scope_id !=
469 (u_int32_t)ifp->if_index)
470 return (EINVAL);
471 sa6->sin6_scope_id = 0; /* XXX: good way? */
472 }
473 }
474 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
475 } else
476 ia = NULL;
477
478 switch (cmd) {
479 case SIOCSIFADDR_IN6:
480 case SIOCSIFDSTADDR_IN6:
481 case SIOCSIFNETMASK_IN6:
482 /*
483 * Since IPv6 allows a node to assign multiple addresses
484 * on a single interface, SIOCSIFxxx ioctls are deprecated.
485 */
486 return (EINVAL);
487
488 case SIOCDIFADDR_IN6:
489 /*
490 * for IPv4, we look for existing in_ifaddr here to allow
491 * "ifconfig if0 delete" to remove the first IPv4 address on
492 * the interface. For IPv6, as the spec allows multiple
493 * interface address from the day one, we consider "remove the
494 * first one" semantics to be not preferable.
495 */
496 if (ia == NULL)
497 return (EADDRNOTAVAIL);
498 /* FALLTHROUGH */
499 case SIOCAIFADDR_IN6:
500 /*
501 * We always require users to specify a valid IPv6 address for
502 * the corresponding operation.
503 */
504 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
505 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
506 return (EAFNOSUPPORT);
507 if (!privileged)
508 return (EPERM);
509
510 break;
511
512 case SIOCGIFADDR_IN6:
513 /* This interface is basically deprecated. use SIOCGIFCONF. */
514 /* FALLTHROUGH */
515 case SIOCGIFAFLAG_IN6:
516 case SIOCGIFNETMASK_IN6:
517 case SIOCGIFDSTADDR_IN6:
518 case SIOCGIFALIFETIME_IN6:
519 /* must think again about its semantics */
520 if (ia == NULL)
521 return (EADDRNOTAVAIL);
522 break;
523 case SIOCSIFALIFETIME_IN6:
524 {
525 struct in6_addrlifetime *lt;
526
527 if (!privileged)
528 return (EPERM);
529 if (ia == NULL)
530 return (EADDRNOTAVAIL);
531 /* sanity for overflow - beware unsigned */
532 lt = &ifr->ifr_ifru.ifru_lifetime;
533 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
534 && lt->ia6t_vltime + time.tv_sec < time.tv_sec) {
535 return EINVAL;
536 }
537 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
538 && lt->ia6t_pltime + time.tv_sec < time.tv_sec) {
539 return EINVAL;
540 }
541 break;
542 }
543 }
544
545 switch (cmd) {
546
547 case SIOCGIFADDR_IN6:
548 ifr->ifr_addr = ia->ia_addr;
549 break;
550
551 case SIOCGIFDSTADDR_IN6:
552 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
553 return (EINVAL);
554 /*
555 * XXX: should we check if ifa_dstaddr is NULL and return
556 * an error?
557 */
558 ifr->ifr_dstaddr = ia->ia_dstaddr;
559 break;
560
561 case SIOCGIFNETMASK_IN6:
562 ifr->ifr_addr = ia->ia_prefixmask;
563 break;
564
565 case SIOCGIFAFLAG_IN6:
566 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
567 break;
568
569 case SIOCGIFSTAT_IN6:
570 if (ifp == NULL)
571 return EINVAL;
572 bzero(&ifr->ifr_ifru.ifru_stat,
573 sizeof(ifr->ifr_ifru.ifru_stat));
574 ifr->ifr_ifru.ifru_stat =
575 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
576 break;
577
578 case SIOCGIFSTAT_ICMP6:
579 if (ifp == NULL)
580 return EINVAL;
581 bzero(&ifr->ifr_ifru.ifru_stat,
582 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
583 ifr->ifr_ifru.ifru_icmp6stat =
584 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
585 break;
586
587 case SIOCGIFALIFETIME_IN6:
588 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
589 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
590 time_t maxexpire;
591 struct in6_addrlifetime *retlt =
592 &ifr->ifr_ifru.ifru_lifetime;
593
594 /*
595 * XXX: adjust expiration time assuming time_t is
596 * signed.
597 */
598 maxexpire = (sizeof(maxexpire) == 8)
599 ? 0x7FFFFFFFFFFFFFFFLL
600 : 0x7FFFFFFFLL;
601 if (ia->ia6_lifetime.ia6t_vltime <
602 maxexpire - ia->ia6_updatetime) {
603 retlt->ia6t_expire = ia->ia6_updatetime +
604 ia->ia6_lifetime.ia6t_vltime;
605 } else
606 retlt->ia6t_expire = maxexpire;
607 }
608 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
609 time_t maxexpire;
610 struct in6_addrlifetime *retlt =
611 &ifr->ifr_ifru.ifru_lifetime;
612
613 /*
614 * XXX: adjust expiration time assuming time_t is
615 * signed.
616 */
617 maxexpire = (sizeof(maxexpire) == 8)
618 ? 0x7FFFFFFFFFFFFFFFLL
619 : 0x7FFFFFFFLL;
620 if (ia->ia6_lifetime.ia6t_pltime <
621 maxexpire - ia->ia6_updatetime) {
622 retlt->ia6t_preferred = ia->ia6_updatetime +
623 ia->ia6_lifetime.ia6t_pltime;
624 } else
625 retlt->ia6t_preferred = maxexpire;
626 }
627 break;
628
629 case SIOCSIFALIFETIME_IN6:
630 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
631 /* for sanity */
632 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
633 ia->ia6_lifetime.ia6t_expire =
634 time.tv_sec + ia->ia6_lifetime.ia6t_vltime;
635 } else
636 ia->ia6_lifetime.ia6t_expire = 0;
637 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
638 ia->ia6_lifetime.ia6t_preferred =
639 time.tv_sec + ia->ia6_lifetime.ia6t_pltime;
640 } else
641 ia->ia6_lifetime.ia6t_preferred = 0;
642 break;
643
644 case SIOCAIFADDR_IN6:
645 {
646 int i, error = 0;
647 struct nd_prefix pr0, *pr;
648
649 /* reject read-only flags */
650 if ((ifra->ifra_flags & IN6_IFF_DUPLICATED) != 0 ||
651 (ifra->ifra_flags & IN6_IFF_DETACHED) != 0 ||
652 (ifra->ifra_flags & IN6_IFF_NODAD) != 0 ||
653 (ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0) {
654 return (EINVAL);
655 }
656 /*
657 * first, make or update the interface address structure,
658 * and link it to the list.
659 */
660 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
661 return (error);
662 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
663 == NULL) {
664 /*
665 * this can happen when the user specify the 0 valid
666 * lifetime.
667 */
668 break;
669 }
670
671 /*
672 * then, make the prefix on-link on the interface.
673 * XXX: we'd rather create the prefix before the address, but
674 * we need at least one address to install the corresponding
675 * interface route, so we configure the address first.
676 */
677
678 /*
679 * convert mask to prefix length (prefixmask has already
680 * been validated in in6_update_ifa().
681 */
682 bzero(&pr0, sizeof(pr0));
683 pr0.ndpr_ifp = ifp;
684 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
685 NULL);
686 if (pr0.ndpr_plen == 128) {
687 break; /* we don't need to install a host route. */
688 }
689 pr0.ndpr_prefix = ifra->ifra_addr;
690 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
691 /* apply the mask for safety. */
692 for (i = 0; i < 4; i++) {
693 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
694 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
695 }
696 /*
697 * XXX: since we don't have an API to set prefix (not address)
698 * lifetimes, we just use the same lifetimes as addresses.
699 * The (temporarily) installed lifetimes can be overridden by
700 * later advertised RAs (when accept_rtadv is non 0), which is
701 * an intended behavior.
702 */
703 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
704 pr0.ndpr_raf_auto =
705 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
706 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
707 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
708
709 /* add the prefix if not yet. */
710 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
711 /*
712 * nd6_prelist_add will install the corresponding
713 * interface route.
714 */
715 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
716 return (error);
717 if (pr == NULL) {
718 log(LOG_ERR, "nd6_prelist_add succeeded but "
719 "no prefix\n");
720 return (EINVAL); /* XXX panic here? */
721 }
722 }
723
724 /* relate the address to the prefix */
725 if (ia->ia6_ndpr == NULL) {
726 ia->ia6_ndpr = pr;
727 pr->ndpr_refcnt++;
728 }
729
730 /*
731 * this might affect the status of autoconfigured addresses,
732 * that is, this address might make other addresses detached.
733 */
734 pfxlist_onlink_check();
735
736 rnd_lopool_addh(ia, sizeof(*ia));
737 break;
738 }
739
740 case SIOCDIFADDR_IN6:
741 {
742 int i = 0, purgeprefix = 0;
743 struct nd_prefix pr0, *pr = NULL;
744
745 /*
746 * If the address being deleted is the only one that owns
747 * the corresponding prefix, expire the prefix as well.
748 * XXX: theoretically, we don't have to worry about such
749 * relationship, since we separate the address management
750 * and the prefix management. We do this, however, to provide
751 * as much backward compatibility as possible in terms of
752 * the ioctl operation.
753 */
754 bzero(&pr0, sizeof(pr0));
755 pr0.ndpr_ifp = ifp;
756 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
757 NULL);
758 if (pr0.ndpr_plen == 128)
759 goto purgeaddr;
760 pr0.ndpr_prefix = ia->ia_addr;
761 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
762 for (i = 0; i < 4; i++) {
763 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
764 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
765 }
766 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
767 pr == ia->ia6_ndpr) {
768 pr->ndpr_refcnt--;
769 if (pr->ndpr_refcnt == 0)
770 purgeprefix = 1;
771 }
772
773 purgeaddr:
774 in6_purgeaddr(&ia->ia_ifa);
775 if (pr && purgeprefix)
776 prelist_remove(pr);
777 break;
778 }
779
780 default:
781 if (ifp == NULL || ifp->if_ioctl == 0)
782 return (EOPNOTSUPP);
783 return ((*ifp->if_ioctl)(ifp, cmd, data));
784 }
785
786 dohooks(ifp->if_addrhooks, 0);
787 return (0);
788 }
789
790 /*
791 * Update parameters of an IPv6 interface address.
792 * If necessary, a new entry is created and linked into address chains.
793 * This function is separated from in6_control().
794 * XXX: should this be performed under splnet()?
795 */
796 int
in6_update_ifa(ifp,ifra,ia)797 in6_update_ifa(ifp, ifra, ia)
798 struct ifnet *ifp;
799 struct in6_aliasreq *ifra;
800 struct in6_ifaddr *ia;
801 {
802 int error = 0, hostIsNew = 0, plen = -1;
803 struct in6_ifaddr *oia;
804 struct sockaddr_in6 dst6;
805 struct in6_addrlifetime *lt;
806 struct in6_multi_mship *imm;
807 struct rtentry *rt;
808
809 /* Validate parameters */
810 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
811 return (EINVAL);
812
813 /*
814 * The destination address for a p2p link must have a family
815 * of AF_UNSPEC or AF_INET6.
816 */
817 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
818 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
819 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
820 return (EAFNOSUPPORT);
821 /*
822 * validate ifra_prefixmask. don't check sin6_family, netmask
823 * does not carry fields other than sin6_len.
824 */
825 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
826 return (EINVAL);
827 /*
828 * Because the IPv6 address architecture is classless, we require
829 * users to specify a (non 0) prefix length (mask) for a new address.
830 * We also require the prefix (when specified) mask is valid, and thus
831 * reject a non-consecutive mask.
832 */
833 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
834 return (EINVAL);
835 if (ifra->ifra_prefixmask.sin6_len != 0) {
836 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
837 (u_char *)&ifra->ifra_prefixmask +
838 ifra->ifra_prefixmask.sin6_len);
839 if (plen <= 0)
840 return (EINVAL);
841 } else {
842 /*
843 * In this case, ia must not be NULL. We just use its prefix
844 * length.
845 */
846 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
847 }
848 /*
849 * If the destination address on a p2p interface is specified,
850 * and the address is a scoped one, validate/set the scope
851 * zone identifier.
852 */
853 dst6 = ifra->ifra_dstaddr;
854 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
855 (dst6.sin6_family == AF_INET6)) {
856 /* link-local index check: should be a separate function? */
857 if (IN6_IS_ADDR_LINKLOCAL(&dst6.sin6_addr)) {
858 if (dst6.sin6_addr.s6_addr16[1] == 0) {
859 /*
860 * interface ID is not embedded by
861 * the user
862 */
863 dst6.sin6_addr.s6_addr16[1] =
864 htons(ifp->if_index);
865 } else if (dst6.sin6_addr.s6_addr16[1] !=
866 htons(ifp->if_index)) {
867 return (EINVAL); /* ifid contradicts */
868 }
869 }
870 }
871 /*
872 * The destination address can be specified only for a p2p or a
873 * loopback interface. If specified, the corresponding prefix length
874 * must be 128.
875 */
876 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
877 #ifdef FORCE_P2PPLEN
878 int i;
879 #endif
880
881 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
882 /* XXX: noisy message */
883 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
884 "be specified for a p2p or a loopback IF only\n"));
885 return (EINVAL);
886 }
887 if (plen != 128) {
888 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
889 "be 128 when dstaddr is specified\n"));
890 #ifdef FORCE_P2PPLEN
891 /*
892 * To be compatible with old configurations,
893 * such as ifconfig gif0 inet6 2001::1 2001::2
894 * prefixlen 126, we override the specified
895 * prefixmask as if the prefix length was 128.
896 */
897 ifra->ifra_prefixmask.sin6_len =
898 sizeof(struct sockaddr_in6);
899 for (i = 0; i < 4; i++)
900 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i] =
901 0xffffffff;
902 plen = 128;
903 #else
904 return (EINVAL);
905 #endif
906 }
907 }
908 /* lifetime consistency check */
909 lt = &ifra->ifra_lifetime;
910 if (lt->ia6t_pltime > lt->ia6t_vltime)
911 return (EINVAL);
912 if (lt->ia6t_vltime == 0) {
913 /*
914 * the following log might be noisy, but this is a typical
915 * configuration mistake or a tool's bug.
916 */
917 nd6log((LOG_INFO,
918 "in6_update_ifa: valid lifetime is 0 for %s\n",
919 ip6_sprintf(&ifra->ifra_addr.sin6_addr)));
920
921 if (ia == NULL)
922 return (0); /* there's nothing to do */
923 }
924
925 /*
926 * If this is a new address, allocate a new ifaddr and link it
927 * into chains.
928 */
929 if (ia == NULL) {
930 hostIsNew = 1;
931 /*
932 * When in6_update_ifa() is called in a process of a received
933 * RA, it is called under an interrupt context. So, we should
934 * call malloc with M_NOWAIT.
935 */
936 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
937 M_NOWAIT);
938 if (ia == NULL)
939 return (ENOBUFS);
940 bzero((caddr_t)ia, sizeof(*ia));
941 LIST_INIT(&ia->ia6_memberships);
942 /* Initialize the address and masks, and put time stamp */
943 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
944 ia->ia_addr.sin6_family = AF_INET6;
945 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
946 ia->ia6_createtime = ia->ia6_updatetime = time.tv_sec;
947 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
948 /*
949 * XXX: some functions expect that ifa_dstaddr is not
950 * NULL for p2p interfaces.
951 */
952 ia->ia_ifa.ifa_dstaddr =
953 (struct sockaddr *)&ia->ia_dstaddr;
954 } else {
955 ia->ia_ifa.ifa_dstaddr = NULL;
956 }
957 ia->ia_ifa.ifa_netmask =
958 (struct sockaddr *)&ia->ia_prefixmask;
959
960 ia->ia_ifp = ifp;
961 if ((oia = in6_ifaddr) != NULL) {
962 for ( ; oia->ia_next; oia = oia->ia_next)
963 continue;
964 oia->ia_next = ia;
965 } else
966 in6_ifaddr = ia;
967 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
968 ifa_list);
969 }
970
971 /* set prefix mask */
972 if (ifra->ifra_prefixmask.sin6_len) {
973 /*
974 * We prohibit changing the prefix length of an existing
975 * address, because
976 * + such an operation should be rare in IPv6, and
977 * + the operation would confuse prefix management.
978 */
979 if (ia->ia_prefixmask.sin6_len &&
980 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
981 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
982 " existing (%s) address should not be changed\n",
983 ip6_sprintf(&ia->ia_addr.sin6_addr)));
984 error = EINVAL;
985 goto unlink;
986 }
987 ia->ia_prefixmask = ifra->ifra_prefixmask;
988 }
989
990 /*
991 * If a new destination address is specified, scrub the old one and
992 * install the new destination. Note that the interface must be
993 * p2p or loopback (see the check above.)
994 */
995 if (dst6.sin6_family == AF_INET6 &&
996 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
997 int e;
998
999 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1000 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
1001 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
1002 "a route to the old destination: %s\n",
1003 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1004 /* proceed anyway... */
1005 } else
1006 ia->ia_flags &= ~IFA_ROUTE;
1007 ia->ia_dstaddr = dst6;
1008 }
1009
1010 /*
1011 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1012 * to see if the address is deprecated or invalidated, but initialize
1013 * these members for applications.
1014 */
1015 ia->ia6_lifetime = ifra->ifra_lifetime;
1016 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1017 ia->ia6_lifetime.ia6t_expire =
1018 time.tv_sec + ia->ia6_lifetime.ia6t_vltime;
1019 } else
1020 ia->ia6_lifetime.ia6t_expire = 0;
1021 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1022 ia->ia6_lifetime.ia6t_preferred =
1023 time.tv_sec + ia->ia6_lifetime.ia6t_pltime;
1024 } else
1025 ia->ia6_lifetime.ia6t_preferred = 0;
1026
1027 /* reset the interface and routing table appropriately. */
1028 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1029 goto unlink;
1030
1031 /*
1032 * configure address flags.
1033 */
1034 ia->ia6_flags = ifra->ifra_flags;
1035 /*
1036 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1037 * userland, make it deprecated.
1038 */
1039 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1040 ia->ia6_lifetime.ia6t_pltime = 0;
1041 ia->ia6_lifetime.ia6t_preferred = time.tv_sec;
1042 }
1043 /*
1044 * Make the address tentative before joining multicast addresses,
1045 * so that corresponding MLD responses would not have a tentative
1046 * source address.
1047 */
1048 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1049 if (hostIsNew && in6if_do_dad(ifp))
1050 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1051
1052 /*
1053 * We are done if we have simply modified an existing address.
1054 */
1055 if (!hostIsNew)
1056 return (error);
1057
1058 /*
1059 * Beyond this point, we should call in6_purgeaddr upon an error,
1060 * not just go to unlink.
1061 */
1062
1063 /* join necessary multiast groups */
1064 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1065 struct sockaddr_in6 mltaddr, mltmask;
1066 #ifndef SCOPEDROUTING
1067 u_int32_t zoneid = 0;
1068 #endif
1069
1070 /* join solicited multicast addr for new host id */
1071 struct sockaddr_in6 llsol;
1072
1073 bzero(&llsol, sizeof(llsol));
1074 llsol.sin6_family = AF_INET6;
1075 llsol.sin6_len = sizeof(llsol);
1076 llsol.sin6_addr.s6_addr16[0] = htons(0xff02);
1077 llsol.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1078 llsol.sin6_addr.s6_addr32[1] = 0;
1079 llsol.sin6_addr.s6_addr32[2] = htonl(1);
1080 llsol.sin6_addr.s6_addr32[3] =
1081 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1082 llsol.sin6_addr.s6_addr8[12] = 0xff;
1083 imm = in6_joingroup(ifp, &llsol.sin6_addr, &error);
1084 if (!imm) {
1085 nd6log((LOG_ERR, "in6_update_ifa: "
1086 "addmulti failed for %s on %s (errno=%d)\n",
1087 ip6_sprintf(&llsol.sin6_addr),
1088 ifp->if_xname, error));
1089 goto cleanup;
1090 }
1091 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1092
1093 bzero(&mltmask, sizeof(mltmask));
1094 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1095 mltmask.sin6_family = AF_INET6;
1096 mltmask.sin6_addr = in6mask32;
1097
1098 /*
1099 * join link-local all-nodes address
1100 */
1101 bzero(&mltaddr, sizeof(mltaddr));
1102 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1103 mltaddr.sin6_family = AF_INET6;
1104 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1105 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1106
1107 /*
1108 * XXX: do we really need this automatic routes?
1109 * We should probably reconsider this stuff. Most applications
1110 * actually do not need the routes, since they usually specify
1111 * the outgoing interface.
1112 */
1113 rt = rtalloc1((struct sockaddr *)&mltaddr, 0);
1114 if (rt) {
1115 /*
1116 * 32bit came from "mltmask"
1117 * XXX: only works in !SCOPEDROUTING case.
1118 */
1119 if (memcmp(&mltaddr.sin6_addr,
1120 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1121 32 / 8)) {
1122 RTFREE(rt);
1123 rt = NULL;
1124 }
1125 }
1126 if (!rt) {
1127 struct rt_addrinfo info;
1128
1129 bzero(&info, sizeof(info));
1130 info.rti_info[RTAX_DST] = (struct sockaddr *)&mltaddr;
1131 info.rti_info[RTAX_GATEWAY] =
1132 (struct sockaddr *)&ia->ia_addr;
1133 info.rti_info[RTAX_NETMASK] =
1134 (struct sockaddr *)&mltmask;
1135 info.rti_info[RTAX_IFA] =
1136 (struct sockaddr *)&ia->ia_addr;
1137 /* XXX: we need RTF_CLONING to fake nd6_rtrequest */
1138 info.rti_flags = RTF_UP | RTF_CLONING;
1139 error = rtrequest1(RTM_ADD, &info, NULL);
1140 if (error)
1141 goto cleanup;
1142 } else {
1143 RTFREE(rt);
1144 }
1145 #ifndef SCOPEDROUTING
1146 mltaddr.sin6_scope_id = zoneid; /* XXX */
1147 #endif
1148 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
1149 if (!imm) {
1150 nd6log((LOG_WARNING,
1151 "in6_update_ifa: addmulti failed for "
1152 "%s on %s (errno=%d)\n",
1153 ip6_sprintf(&mltaddr.sin6_addr),
1154 ifp->if_xname, error));
1155 goto cleanup;
1156 }
1157 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1158
1159 /*
1160 * join node information group address
1161 */
1162 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr) == 0) {
1163 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
1164 if (!imm) {
1165 nd6log((LOG_WARNING, "in6_update_ifa: "
1166 "addmulti failed for %s on %s (errno=%d)\n",
1167 ip6_sprintf(&mltaddr.sin6_addr),
1168 ifp->if_xname, error));
1169 /* XXX not very fatal, go on... */
1170 } else {
1171 LIST_INSERT_HEAD(&ia->ia6_memberships,
1172 imm, i6mm_chain);
1173 }
1174 }
1175
1176 if (ifp->if_flags & IFF_LOOPBACK) {
1177 /*
1178 * join node-local all-nodes address, on loopback.
1179 * (ff01::1%ifN, and ff01::%ifN/32)
1180 */
1181 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1182
1183 /* XXX: again, do we really need the route? */
1184 rt = rtalloc1((struct sockaddr *)&mltaddr, 0);
1185 if (rt) {
1186 /* 32bit came from "mltmask" */
1187 if (memcmp(&mltaddr.sin6_addr,
1188 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1189 32 / 8)) {
1190 RTFREE(rt);
1191 rt = NULL;
1192 }
1193 }
1194 if (!rt) {
1195 struct rt_addrinfo info;
1196
1197 bzero(&info, sizeof(info));
1198 info.rti_info[RTAX_DST] = (struct sockaddr *)&mltaddr;
1199 info.rti_info[RTAX_GATEWAY] =
1200 (struct sockaddr *)&ia->ia_addr;
1201 info.rti_info[RTAX_NETMASK] =
1202 (struct sockaddr *)&mltmask;
1203 info.rti_info[RTAX_IFA] =
1204 (struct sockaddr *)&ia->ia_addr;
1205 info.rti_flags = RTF_UP | RTF_CLONING;
1206 error = rtrequest1(RTM_ADD, &info, NULL);
1207 if (error)
1208 goto cleanup;
1209 } else {
1210 RTFREE(rt);
1211 }
1212 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
1213 if (!imm) {
1214 nd6log((LOG_WARNING, "in6_update_ifa: "
1215 "addmulti failed for %s on %s (errno=%d)\n",
1216 ip6_sprintf(&mltaddr.sin6_addr),
1217 ifp->if_xname, error));
1218 goto cleanup;
1219 }
1220 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1221 }
1222 }
1223
1224 /*
1225 * Perform DAD, if needed.
1226 * XXX It may be of use, if we can administratively
1227 * disable DAD.
1228 */
1229 if (hostIsNew && in6if_do_dad(ifp) &&
1230 (ifra->ifra_flags & IN6_IFF_NODAD) == 0)
1231 {
1232 nd6_dad_start((struct ifaddr *)ia, NULL);
1233 }
1234
1235 return (error);
1236
1237 unlink:
1238 /*
1239 * XXX: if a change of an existing address failed, keep the entry
1240 * anyway.
1241 */
1242 if (hostIsNew)
1243 in6_unlink_ifa(ia, ifp);
1244 return (error);
1245
1246 cleanup:
1247 in6_purgeaddr(&ia->ia_ifa);
1248 return error;
1249 }
1250
1251 void
in6_purgeaddr(ifa)1252 in6_purgeaddr(ifa)
1253 struct ifaddr *ifa;
1254 {
1255 struct ifnet *ifp = ifa->ifa_ifp;
1256 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1257 struct in6_multi_mship *imm;
1258
1259 /* stop DAD processing */
1260 nd6_dad_stop(ifa);
1261
1262 /*
1263 * delete route to the destination of the address being purged.
1264 * The interface must be p2p or loopback in this case.
1265 */
1266 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1267 int e;
1268
1269 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1270 != 0) {
1271 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1272 "a route to the p2p destination: %s on %s, "
1273 "errno=%d\n",
1274 ip6_sprintf(&ia->ia_addr.sin6_addr), ifp->if_xname,
1275 e);
1276 /* proceed anyway... */
1277 } else
1278 ia->ia_flags &= ~IFA_ROUTE;
1279 }
1280
1281 /* Remove ownaddr's loopback rtentry, if it exists. */
1282 in6_ifremloop(&(ia->ia_ifa));
1283
1284 /*
1285 * leave from multicast groups we have joined for the interface
1286 */
1287 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1288 LIST_REMOVE(imm, i6mm_chain);
1289 in6_leavegroup(imm);
1290 }
1291
1292 in6_unlink_ifa(ia, ifp);
1293 }
1294
1295 static void
in6_unlink_ifa(ia,ifp)1296 in6_unlink_ifa(ia, ifp)
1297 struct in6_ifaddr *ia;
1298 struct ifnet *ifp;
1299 {
1300 struct in6_ifaddr *oia;
1301 int s = splnet();
1302
1303 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1304
1305 oia = ia;
1306 if (oia == (ia = in6_ifaddr))
1307 in6_ifaddr = ia->ia_next;
1308 else {
1309 while (ia->ia_next && (ia->ia_next != oia))
1310 ia = ia->ia_next;
1311 if (ia->ia_next)
1312 ia->ia_next = oia->ia_next;
1313 else {
1314 /* search failed */
1315 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1316 }
1317 }
1318
1319 if (oia->ia6_multiaddrs.lh_first != NULL) {
1320 in6_savemkludge(oia);
1321 }
1322
1323 /*
1324 * When an autoconfigured address is being removed, release the
1325 * reference to the base prefix. Also, since the release might
1326 * affect the status of other (detached) addresses, call
1327 * pfxlist_onlink_check().
1328 */
1329 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1330 if (oia->ia6_ndpr == NULL) {
1331 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1332 "%p has no prefix\n", oia);
1333 } else {
1334 oia->ia6_ndpr->ndpr_refcnt--;
1335 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1336 oia->ia6_ndpr = NULL;
1337 }
1338
1339 pfxlist_onlink_check();
1340 }
1341
1342 /*
1343 * release another refcnt for the link from in6_ifaddr.
1344 * Note that we should decrement the refcnt at least once for all *BSD.
1345 */
1346 IFAFREE(&oia->ia_ifa);
1347
1348 splx(s);
1349 }
1350
1351 void
in6_purgeif(ifp)1352 in6_purgeif(ifp)
1353 struct ifnet *ifp;
1354 {
1355 struct ifaddr *ifa, *nifa;
1356
1357 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1358 {
1359 nifa = TAILQ_NEXT(ifa, ifa_list);
1360 if (ifa->ifa_addr->sa_family != AF_INET6)
1361 continue;
1362 in6_purgeaddr(ifa);
1363 }
1364
1365 in6_ifdetach(ifp);
1366 }
1367
1368 /*
1369 * SIOC[GAD]LIFADDR.
1370 * SIOCGLIFADDR: get first address. (?)
1371 * SIOCGLIFADDR with IFLR_PREFIX:
1372 * get first address that matches the specified prefix.
1373 * SIOCALIFADDR: add the specified address.
1374 * SIOCALIFADDR with IFLR_PREFIX:
1375 * add the specified prefix, filling hostid part from
1376 * the first link-local address. prefixlen must be <= 64.
1377 * SIOCDLIFADDR: delete the specified address.
1378 * SIOCDLIFADDR with IFLR_PREFIX:
1379 * delete the first address that matches the specified prefix.
1380 * return values:
1381 * EINVAL on invalid parameters
1382 * EADDRNOTAVAIL on prefix match failed/specified address not found
1383 * other values may be returned from in6_ioctl()
1384 *
1385 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1386 * this is to accommodate address naming scheme other than RFC2374,
1387 * in the future.
1388 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1389 * address encoding scheme. (see figure on page 8)
1390 */
1391 static int
in6_lifaddr_ioctl(so,cmd,data,ifp,p)1392 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
1393 struct socket *so;
1394 u_long cmd;
1395 caddr_t data;
1396 struct ifnet *ifp;
1397 struct proc *p;
1398 {
1399 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1400 struct ifaddr *ifa;
1401 struct sockaddr *sa;
1402
1403 /* sanity checks */
1404 if (!data || !ifp) {
1405 panic("invalid argument to in6_lifaddr_ioctl");
1406 /* NOTREACHED */
1407 }
1408
1409 switch (cmd) {
1410 case SIOCGLIFADDR:
1411 /* address must be specified on GET with IFLR_PREFIX */
1412 if ((iflr->flags & IFLR_PREFIX) == 0)
1413 break;
1414 /* FALLTHROUGH */
1415 case SIOCALIFADDR:
1416 case SIOCDLIFADDR:
1417 /* address must be specified on ADD and DELETE */
1418 sa = (struct sockaddr *)&iflr->addr;
1419 if (sa->sa_family != AF_INET6)
1420 return EINVAL;
1421 if (sa->sa_len != sizeof(struct sockaddr_in6))
1422 return EINVAL;
1423 /* XXX need improvement */
1424 sa = (struct sockaddr *)&iflr->dstaddr;
1425 if (sa->sa_family && sa->sa_family != AF_INET6)
1426 return EINVAL;
1427 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1428 return EINVAL;
1429 break;
1430 default: /* shouldn't happen */
1431 #if 0
1432 panic("invalid cmd to in6_lifaddr_ioctl");
1433 /* NOTREACHED */
1434 #else
1435 return EOPNOTSUPP;
1436 #endif
1437 }
1438 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1439 return EINVAL;
1440
1441 switch (cmd) {
1442 case SIOCALIFADDR:
1443 {
1444 struct in6_aliasreq ifra;
1445 struct in6_addr *hostid = NULL;
1446 int prefixlen;
1447
1448 if ((iflr->flags & IFLR_PREFIX) != 0) {
1449 struct sockaddr_in6 *sin6;
1450
1451 /*
1452 * hostid is to fill in the hostid part of the
1453 * address. hostid points to the first link-local
1454 * address attached to the interface.
1455 */
1456 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1457 if (!ifa)
1458 return EADDRNOTAVAIL;
1459 hostid = IFA_IN6(ifa);
1460
1461 /* prefixlen must be <= 64. */
1462 if (64 < iflr->prefixlen)
1463 return EINVAL;
1464 prefixlen = iflr->prefixlen;
1465
1466 /* hostid part must be zero. */
1467 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1468 if (sin6->sin6_addr.s6_addr32[2] != 0
1469 || sin6->sin6_addr.s6_addr32[3] != 0) {
1470 return EINVAL;
1471 }
1472 } else
1473 prefixlen = iflr->prefixlen;
1474
1475 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1476 bzero(&ifra, sizeof(ifra));
1477 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1478
1479 bcopy(&iflr->addr, &ifra.ifra_addr,
1480 ((struct sockaddr *)&iflr->addr)->sa_len);
1481 if (hostid) {
1482 /* fill in hostid part */
1483 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1484 hostid->s6_addr32[2];
1485 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1486 hostid->s6_addr32[3];
1487 }
1488
1489 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1490 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1491 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1492 if (hostid) {
1493 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1494 hostid->s6_addr32[2];
1495 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1496 hostid->s6_addr32[3];
1497 }
1498 }
1499
1500 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1501 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1502
1503 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1504 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1505 }
1506 case SIOCGLIFADDR:
1507 case SIOCDLIFADDR:
1508 {
1509 struct in6_ifaddr *ia;
1510 struct in6_addr mask, candidate, match;
1511 struct sockaddr_in6 *sin6;
1512 int cmp;
1513
1514 bzero(&mask, sizeof(mask));
1515 if (iflr->flags & IFLR_PREFIX) {
1516 /* lookup a prefix rather than address. */
1517 in6_prefixlen2mask(&mask, iflr->prefixlen);
1518
1519 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1520 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1521 match.s6_addr32[0] &= mask.s6_addr32[0];
1522 match.s6_addr32[1] &= mask.s6_addr32[1];
1523 match.s6_addr32[2] &= mask.s6_addr32[2];
1524 match.s6_addr32[3] &= mask.s6_addr32[3];
1525
1526 /* if you set extra bits, that's wrong */
1527 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1528 return EINVAL;
1529
1530 cmp = 1;
1531 } else {
1532 if (cmd == SIOCGLIFADDR) {
1533 /* on getting an address, take the 1st match */
1534 cmp = 0; /* XXX */
1535 } else {
1536 /* on deleting an address, do exact match */
1537 in6_prefixlen2mask(&mask, 128);
1538 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1539 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1540
1541 cmp = 1;
1542 }
1543 }
1544
1545 for (ifa = ifp->if_addrlist.tqh_first;
1546 ifa;
1547 ifa = ifa->ifa_list.tqe_next)
1548 {
1549 if (ifa->ifa_addr->sa_family != AF_INET6)
1550 continue;
1551 if (!cmp)
1552 break;
1553
1554 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1555 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1556 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1557 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1558 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1559 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1560 break;
1561 }
1562 if (!ifa)
1563 return EADDRNOTAVAIL;
1564 ia = ifa2ia6(ifa);
1565
1566 if (cmd == SIOCGLIFADDR) {
1567 /* fill in the if_laddrreq structure */
1568 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1569 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1570 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1571 ia->ia_dstaddr.sin6_len);
1572 } else
1573 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1574
1575 iflr->prefixlen =
1576 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1577
1578 iflr->flags = ia->ia6_flags; /*XXX*/
1579
1580 return 0;
1581 } else {
1582 struct in6_aliasreq ifra;
1583
1584 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1585 bzero(&ifra, sizeof(ifra));
1586 bcopy(iflr->iflr_name, ifra.ifra_name,
1587 sizeof(ifra.ifra_name));
1588
1589 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1590 ia->ia_addr.sin6_len);
1591 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1592 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1593 ia->ia_dstaddr.sin6_len);
1594 } else {
1595 bzero(&ifra.ifra_dstaddr,
1596 sizeof(ifra.ifra_dstaddr));
1597 }
1598 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1599 ia->ia_prefixmask.sin6_len);
1600
1601 ifra.ifra_flags = ia->ia6_flags;
1602 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1603 ifp, p);
1604 }
1605 }
1606 }
1607
1608 return EOPNOTSUPP; /* just for safety */
1609 }
1610
1611 /*
1612 * Initialize an interface's intetnet6 address
1613 * and routing table entry.
1614 */
1615 static int
in6_ifinit(ifp,ia,sin6,newhost)1616 in6_ifinit(ifp, ia, sin6, newhost)
1617 struct ifnet *ifp;
1618 struct in6_ifaddr *ia;
1619 struct sockaddr_in6 *sin6;
1620 int newhost;
1621 {
1622 int error = 0, plen, ifacount = 0;
1623 int s = splimp();
1624 struct ifaddr *ifa;
1625
1626 /*
1627 * Give the interface a chance to initialize
1628 * if this is its first address,
1629 * and to validate the address if necessary.
1630 */
1631 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1632 ifa = ifa->ifa_list.tqe_next)
1633 {
1634 if (ifa->ifa_addr == NULL)
1635 continue; /* just for safety */
1636 if (ifa->ifa_addr->sa_family != AF_INET6)
1637 continue;
1638 ifacount++;
1639 }
1640
1641 ia->ia_addr = *sin6;
1642
1643 if (ifacount <= 1 && ifp->if_ioctl &&
1644 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1645 splx(s);
1646 return (error);
1647 }
1648 splx(s);
1649
1650 ia->ia_ifa.ifa_metric = ifp->if_metric;
1651
1652 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1653
1654 /*
1655 * Special case:
1656 * If the destination address is specified for a point-to-point
1657 * interface, install a route to the destination as an interface
1658 * direct route.
1659 */
1660 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1661 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1662 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1663 RTF_UP | RTF_HOST)) != 0)
1664 return (error);
1665 ia->ia_flags |= IFA_ROUTE;
1666 }
1667
1668 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1669 if (newhost) {
1670 /* set the rtrequest function to create llinfo */
1671 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1672 in6_ifaddloop(&(ia->ia_ifa));
1673 }
1674
1675 if (ifp->if_flags & IFF_MULTICAST)
1676 in6_restoremkludge(ia, ifp);
1677
1678 return (error);
1679 }
1680
1681 /*
1682 * Multicast address kludge:
1683 * If there were any multicast addresses attached to this interface address,
1684 * either move them to another address on this interface, or save them until
1685 * such time as this interface is reconfigured for IPv6.
1686 */
1687 void
in6_savemkludge(oia)1688 in6_savemkludge(oia)
1689 struct in6_ifaddr *oia;
1690 {
1691 struct in6_ifaddr *ia;
1692 struct in6_multi *in6m, *next;
1693
1694 IFP_TO_IA6(oia->ia_ifp, ia);
1695 if (ia) { /* there is another address */
1696 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1697 next = in6m->in6m_entry.le_next;
1698 IFAFREE(&in6m->in6m_ia->ia_ifa);
1699 ia->ia_ifa.ifa_refcnt++;
1700 in6m->in6m_ia = ia;
1701 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1702 }
1703 } else { /* last address on this if deleted, save */
1704 struct multi6_kludge *mk;
1705
1706 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1707 if (mk->mk_ifp == oia->ia_ifp)
1708 break;
1709 }
1710 if (mk == NULL) /* this should not happen! */
1711 panic("in6_savemkludge: no kludge space");
1712
1713 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1714 next = in6m->in6m_entry.le_next;
1715 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1716 in6m->in6m_ia = NULL;
1717 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
1718 }
1719 }
1720 }
1721
1722 /*
1723 * Continuation of multicast address hack:
1724 * If there was a multicast group list previously saved for this interface,
1725 * then we re-attach it to the first address configured on the i/f.
1726 */
1727 void
in6_restoremkludge(ia,ifp)1728 in6_restoremkludge(ia, ifp)
1729 struct in6_ifaddr *ia;
1730 struct ifnet *ifp;
1731 {
1732 struct multi6_kludge *mk;
1733
1734 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1735 if (mk->mk_ifp == ifp) {
1736 struct in6_multi *in6m, *next;
1737
1738 for (in6m = mk->mk_head.lh_first; in6m; in6m = next) {
1739 next = in6m->in6m_entry.le_next;
1740 in6m->in6m_ia = ia;
1741 ia->ia_ifa.ifa_refcnt++;
1742 LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
1743 in6m, in6m_entry);
1744 }
1745 LIST_INIT(&mk->mk_head);
1746 break;
1747 }
1748 }
1749 }
1750
1751 /*
1752 * Allocate space for the kludge at interface initialization time.
1753 * Formerly, we dynamically allocated the space in in6_savemkludge() with
1754 * malloc(M_WAITOK). However, it was wrong since the function could be called
1755 * under an interrupt context (software timer on address lifetime expiration).
1756 * Also, we cannot just give up allocating the strucutre, since the group
1757 * membership structure is very complex and we need to keep it anyway.
1758 * Of course, this function MUST NOT be called under an interrupt context.
1759 * Specifically, it is expected to be called only from in6_ifattach(), though
1760 * it is a global function.
1761 */
1762 void
in6_createmkludge(ifp)1763 in6_createmkludge(ifp)
1764 struct ifnet *ifp;
1765 {
1766 struct multi6_kludge *mk;
1767
1768 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1769 /* If we've already had one, do not allocate. */
1770 if (mk->mk_ifp == ifp)
1771 return;
1772 }
1773
1774 mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
1775
1776 bzero(mk, sizeof(*mk));
1777 LIST_INIT(&mk->mk_head);
1778 mk->mk_ifp = ifp;
1779 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
1780 }
1781
1782 void
in6_purgemkludge(ifp)1783 in6_purgemkludge(ifp)
1784 struct ifnet *ifp;
1785 {
1786 struct multi6_kludge *mk;
1787 struct in6_multi *in6m;
1788
1789 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1790 if (mk->mk_ifp != ifp)
1791 continue;
1792
1793 /* leave from all multicast groups joined */
1794 while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL)
1795 in6_delmulti(in6m);
1796 LIST_REMOVE(mk, mk_entry);
1797 free(mk, M_IPMADDR);
1798 break;
1799 }
1800 }
1801
1802 /*
1803 * Add an address to the list of IP6 multicast addresses for a
1804 * given interface.
1805 */
1806 struct in6_multi *
in6_addmulti(maddr6,ifp,errorp)1807 in6_addmulti(maddr6, ifp, errorp)
1808 struct in6_addr *maddr6;
1809 struct ifnet *ifp;
1810 int *errorp;
1811 {
1812 struct in6_ifaddr *ia;
1813 struct in6_ifreq ifr;
1814 struct in6_multi *in6m;
1815 int s = splsoftnet();
1816
1817 *errorp = 0;
1818 /*
1819 * See if address already in list.
1820 */
1821 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
1822 if (in6m != NULL) {
1823 /*
1824 * Found it; just increment the refrence count.
1825 */
1826 in6m->in6m_refcount++;
1827 } else {
1828 /*
1829 * New address; allocate a new multicast record
1830 * and link it into the interface's multicast list.
1831 */
1832 in6m = (struct in6_multi *)
1833 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1834 if (in6m == NULL) {
1835 splx(s);
1836 *errorp = ENOBUFS;
1837 return (NULL);
1838 }
1839 in6m->in6m_addr = *maddr6;
1840 in6m->in6m_ifp = ifp;
1841 in6m->in6m_refcount = 1;
1842 IFP_TO_IA6(ifp, ia);
1843 if (ia == NULL) {
1844 free(in6m, M_IPMADDR);
1845 splx(s);
1846 *errorp = EADDRNOTAVAIL; /* appropriate? */
1847 return (NULL);
1848 }
1849 in6m->in6m_ia = ia;
1850 ia->ia_ifa.ifa_refcnt++; /* gain a reference */
1851 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1852
1853 /*
1854 * Ask the network driver to update its multicast reception
1855 * filter appropriately for the new address.
1856 */
1857 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1858 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1859 ifr.ifr_addr.sin6_family = AF_INET6;
1860 ifr.ifr_addr.sin6_addr = *maddr6;
1861 if (ifp->if_ioctl == NULL)
1862 *errorp = ENXIO; /* XXX: appropriate? */
1863 else
1864 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
1865 (caddr_t)&ifr);
1866 if (*errorp) {
1867 LIST_REMOVE(in6m, in6m_entry);
1868 free(in6m, M_IPMADDR);
1869 IFAFREE(&ia->ia_ifa);
1870 splx(s);
1871 return (NULL);
1872 }
1873 /*
1874 * Let MLD6 know that we have joined a new IP6 multicast
1875 * group.
1876 */
1877 mld6_start_listening(in6m);
1878 }
1879 splx(s);
1880 return (in6m);
1881 }
1882
1883 /*
1884 * Delete a multicast address record.
1885 */
1886 void
in6_delmulti(in6m)1887 in6_delmulti(in6m)
1888 struct in6_multi *in6m;
1889 {
1890 struct in6_ifreq ifr;
1891 int s = splsoftnet();
1892
1893 if (--in6m->in6m_refcount == 0) {
1894 /*
1895 * No remaining claims to this record; let MLD6 know
1896 * that we are leaving the multicast group.
1897 */
1898 mld6_stop_listening(in6m);
1899
1900 /*
1901 * Unlink from list.
1902 */
1903 LIST_REMOVE(in6m, in6m_entry);
1904 if (in6m->in6m_ia) {
1905 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1906 }
1907
1908 /*
1909 * Notify the network driver to update its multicast
1910 * reception filter.
1911 */
1912 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1913 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1914 ifr.ifr_addr.sin6_family = AF_INET6;
1915 ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
1916 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
1917 SIOCDELMULTI, (caddr_t)&ifr);
1918 free(in6m, M_IPMADDR);
1919 }
1920 splx(s);
1921 }
1922
1923 struct in6_multi_mship *
in6_joingroup(ifp,addr,errorp)1924 in6_joingroup(ifp, addr, errorp)
1925 struct ifnet *ifp;
1926 struct in6_addr *addr;
1927 int *errorp;
1928 {
1929 struct in6_multi_mship *imm;
1930
1931 imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1932 if (!imm) {
1933 *errorp = ENOBUFS;
1934 return NULL;
1935 }
1936 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1937 if (!imm->i6mm_maddr) {
1938 /* *errorp is alrady set */
1939 free(imm, M_IPMADDR);
1940 return NULL;
1941 }
1942 return imm;
1943 }
1944
1945 int
in6_leavegroup(imm)1946 in6_leavegroup(imm)
1947 struct in6_multi_mship *imm;
1948 {
1949
1950 if (imm->i6mm_maddr)
1951 in6_delmulti(imm->i6mm_maddr);
1952 free(imm, M_IPMADDR);
1953 return 0;
1954 }
1955
1956 /*
1957 * Find an IPv6 interface link-local address specific to an interface.
1958 */
1959 struct in6_ifaddr *
in6ifa_ifpforlinklocal(ifp,ignoreflags)1960 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1961 struct ifnet *ifp;
1962 int ignoreflags;
1963 {
1964 struct ifaddr *ifa;
1965
1966 for (ifa = ifp->if_addrlist.tqh_first;
1967 ifa;
1968 ifa = ifa->ifa_list.tqe_next)
1969 {
1970 if (ifa->ifa_addr == NULL)
1971 continue; /* just for safety */
1972 if (ifa->ifa_addr->sa_family != AF_INET6)
1973 continue;
1974 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1975 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1976 ignoreflags) != 0)
1977 continue;
1978 break;
1979 }
1980 }
1981
1982 return ((struct in6_ifaddr *)ifa);
1983 }
1984
1985
1986 /*
1987 * find the internet address corresponding to a given interface and address.
1988 */
1989 struct in6_ifaddr *
in6ifa_ifpwithaddr(ifp,addr)1990 in6ifa_ifpwithaddr(ifp, addr)
1991 struct ifnet *ifp;
1992 struct in6_addr *addr;
1993 {
1994 struct ifaddr *ifa;
1995
1996 for (ifa = ifp->if_addrlist.tqh_first;
1997 ifa;
1998 ifa = ifa->ifa_list.tqe_next)
1999 {
2000 if (ifa->ifa_addr == NULL)
2001 continue; /* just for safety */
2002 if (ifa->ifa_addr->sa_family != AF_INET6)
2003 continue;
2004 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
2005 break;
2006 }
2007
2008 return ((struct in6_ifaddr *)ifa);
2009 }
2010
2011 /*
2012 * find the internet address on a given interface corresponding to a neighbor's
2013 * address.
2014 */
2015 struct in6_ifaddr *
in6ifa_ifplocaladdr(const struct ifnet * ifp,const struct in6_addr * addr)2016 in6ifa_ifplocaladdr(const struct ifnet *ifp, const struct in6_addr *addr)
2017 {
2018 struct ifaddr *ifa;
2019 struct in6_ifaddr *ia;
2020
2021 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2022 if (ifa->ifa_addr == NULL)
2023 continue; /* just for safety */
2024 if (ifa->ifa_addr->sa_family != AF_INET6)
2025 continue;
2026 ia = (struct in6_ifaddr *)ifa;
2027 if (IN6_ARE_MASKED_ADDR_EQUAL(addr,
2028 &ia->ia_addr.sin6_addr,
2029 &ia->ia_prefixmask.sin6_addr))
2030 return ia;
2031 }
2032
2033 return NULL;
2034 }
2035
2036 /*
2037 * Convert IP6 address to printable (loggable) representation.
2038 */
2039 static char digits[] = "0123456789abcdef";
2040 static int ip6round = 0;
2041 char *
ip6_sprintf(addr)2042 ip6_sprintf(addr)
2043 struct in6_addr *addr;
2044 {
2045 static char ip6buf[8][48];
2046 int i;
2047 char *cp;
2048 u_short *a = (u_short *)addr;
2049 u_char *d;
2050 int dcolon = 0;
2051
2052 ip6round = (ip6round + 1) & 7;
2053 cp = ip6buf[ip6round];
2054
2055 for (i = 0; i < 8; i++) {
2056 if (dcolon == 1) {
2057 if (*a == 0) {
2058 if (i == 7)
2059 *cp++ = ':';
2060 a++;
2061 continue;
2062 } else
2063 dcolon = 2;
2064 }
2065 if (*a == 0) {
2066 if (dcolon == 0 && *(a + 1) == 0) {
2067 if (i == 0)
2068 *cp++ = ':';
2069 *cp++ = ':';
2070 dcolon = 1;
2071 } else {
2072 *cp++ = '0';
2073 *cp++ = ':';
2074 }
2075 a++;
2076 continue;
2077 }
2078 d = (u_char *)a;
2079 *cp++ = digits[*d >> 4];
2080 *cp++ = digits[*d++ & 0xf];
2081 *cp++ = digits[*d >> 4];
2082 *cp++ = digits[*d & 0xf];
2083 *cp++ = ':';
2084 a++;
2085 }
2086 *--cp = 0;
2087 return (ip6buf[ip6round]);
2088 }
2089
2090 /*
2091 * Get a scope of the address. Node-local, link-local, site-local or global.
2092 */
2093 int
in6_addrscope(addr)2094 in6_addrscope (addr)
2095 struct in6_addr *addr;
2096 {
2097 int scope;
2098
2099 if (addr->s6_addr8[0] == 0xfe) {
2100 scope = addr->s6_addr8[1] & 0xc0;
2101
2102 switch (scope) {
2103 case 0x80:
2104 return IPV6_ADDR_SCOPE_LINKLOCAL;
2105 break;
2106 case 0xc0:
2107 return IPV6_ADDR_SCOPE_SITELOCAL;
2108 break;
2109 default:
2110 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
2111 break;
2112 }
2113 }
2114
2115
2116 if (addr->s6_addr8[0] == 0xff) {
2117 scope = addr->s6_addr8[1] & 0x0f;
2118
2119 /*
2120 * due to other scope such as reserved,
2121 * return scope doesn't work.
2122 */
2123 switch (scope) {
2124 case IPV6_ADDR_SCOPE_NODELOCAL:
2125 return IPV6_ADDR_SCOPE_NODELOCAL;
2126 break;
2127 case IPV6_ADDR_SCOPE_LINKLOCAL:
2128 return IPV6_ADDR_SCOPE_LINKLOCAL;
2129 break;
2130 case IPV6_ADDR_SCOPE_SITELOCAL:
2131 return IPV6_ADDR_SCOPE_SITELOCAL;
2132 break;
2133 default:
2134 return IPV6_ADDR_SCOPE_GLOBAL;
2135 break;
2136 }
2137 }
2138
2139 if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
2140 if (addr->s6_addr8[15] == 1) /* loopback */
2141 return IPV6_ADDR_SCOPE_NODELOCAL;
2142 if (addr->s6_addr8[15] == 0) /* unspecified */
2143 return IPV6_ADDR_SCOPE_LINKLOCAL;
2144 }
2145
2146 return IPV6_ADDR_SCOPE_GLOBAL;
2147 }
2148
2149 int
in6_addr2scopeid(ifp,addr)2150 in6_addr2scopeid(ifp, addr)
2151 struct ifnet *ifp; /* must not be NULL */
2152 struct in6_addr *addr; /* must not be NULL */
2153 {
2154 int scope = in6_addrscope(addr);
2155
2156 switch(scope) {
2157 case IPV6_ADDR_SCOPE_NODELOCAL:
2158 return (-1); /* XXX: is this an appropriate value? */
2159
2160 case IPV6_ADDR_SCOPE_LINKLOCAL:
2161 /* XXX: we do not distinguish between a link and an I/F. */
2162 return (ifp->if_index);
2163
2164 case IPV6_ADDR_SCOPE_SITELOCAL:
2165 return (0); /* XXX: invalid. */
2166
2167 default:
2168 return (0); /* XXX: treat as global. */
2169 }
2170 }
2171
2172 int
in6_is_addr_deprecated(sa6)2173 in6_is_addr_deprecated(sa6)
2174 struct sockaddr_in6 *sa6;
2175 {
2176 struct in6_ifaddr *ia;
2177
2178 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
2179 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
2180 &sa6->sin6_addr) &&
2181 #ifdef SCOPEDROUTING
2182 ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
2183 #endif
2184 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2185 return (1); /* true */
2186
2187 /* XXX: do we still have to go thru the rest of the list? */
2188 }
2189
2190 return (0); /* false */
2191 }
2192
2193 /*
2194 * return length of part which dst and src are equal
2195 * hard coding...
2196 */
2197 int
in6_matchlen(src,dst)2198 in6_matchlen(src, dst)
2199 struct in6_addr *src, *dst;
2200 {
2201 int match = 0;
2202 u_char *s = (u_char *)src, *d = (u_char *)dst;
2203 u_char *lim = s + 16, r;
2204
2205 while (s < lim)
2206 if ((r = (*d++ ^ *s++)) != 0) {
2207 while (r < 128) {
2208 match++;
2209 r <<= 1;
2210 }
2211 break;
2212 } else
2213 match += 8;
2214 return match;
2215 }
2216
2217 int
in6_are_prefix_equal(p1,p2,len)2218 in6_are_prefix_equal(p1, p2, len)
2219 struct in6_addr *p1, *p2;
2220 int len;
2221 {
2222 int bytelen, bitlen;
2223
2224 /* sanity check */
2225 if (0 > len || len > 128) {
2226 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2227 len);
2228 return (0);
2229 }
2230
2231 bytelen = len / 8;
2232 bitlen = len % 8;
2233
2234 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2235 return (0);
2236 /* len == 128 is ok because bitlen == 0 then */
2237 if (bitlen != 0 &&
2238 p1->s6_addr[bytelen] >> (8 - bitlen) !=
2239 p2->s6_addr[bytelen] >> (8 - bitlen))
2240 return (0);
2241
2242 return (1);
2243 }
2244
2245 void
in6_prefixlen2mask(maskp,len)2246 in6_prefixlen2mask(maskp, len)
2247 struct in6_addr *maskp;
2248 int len;
2249 {
2250 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2251 int bytelen, bitlen, i;
2252
2253 /* sanity check */
2254 if (0 > len || len > 128) {
2255 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2256 len);
2257 return;
2258 }
2259
2260 bzero(maskp, sizeof(*maskp));
2261 bytelen = len / 8;
2262 bitlen = len % 8;
2263 for (i = 0; i < bytelen; i++)
2264 maskp->s6_addr[i] = 0xff;
2265 /* len == 128 is ok because bitlen == 0 then */
2266 if (bitlen)
2267 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2268 }
2269
2270 /*
2271 * return the best address out of the same scope
2272 */
2273 struct in6_ifaddr *
in6_ifawithscope(oifp,dst)2274 in6_ifawithscope(oifp, dst)
2275 struct ifnet *oifp;
2276 struct in6_addr *dst;
2277 {
2278 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
2279 int blen = -1;
2280 struct ifaddr *ifa;
2281 struct ifnet *ifp;
2282 struct in6_ifaddr *ifa_best = NULL;
2283
2284 if (oifp == NULL) {
2285 printf("in6_ifawithscope: output interface is not specified\n");
2286 return (NULL);
2287 }
2288
2289 /*
2290 * We search for all addresses on all interfaces from the beginning.
2291 * Comparing an interface with the outgoing interface will be done
2292 * only at the final stage of tiebreaking.
2293 */
2294 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2295 {
2296 /*
2297 * We can never take an address that breaks the scope zone
2298 * of the destination.
2299 */
2300 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
2301 continue;
2302
2303 for (ifa = ifp->if_addrlist.tqh_first; ifa;
2304 ifa = ifa->ifa_list.tqe_next)
2305 {
2306 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2307
2308 if (ifa->ifa_addr->sa_family != AF_INET6)
2309 continue;
2310
2311 src_scope = in6_addrscope(IFA_IN6(ifa));
2312
2313 #ifdef ADDRSELECT_DEBUG /* should be removed after stabilization */
2314 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2315 printf("in6_ifawithscope: dst=%s bestaddr=%s, "
2316 "newaddr=%s, scope=%x, dcmp=%d, bcmp=%d, "
2317 "matchlen=%d, flgs=%x\n",
2318 ip6_sprintf(dst),
2319 ifa_best ? ip6_sprintf(&ifa_best->ia_addr.sin6_addr) : "none",
2320 ip6_sprintf(IFA_IN6(ifa)), src_scope,
2321 dscopecmp,
2322 ifa_best ? IN6_ARE_SCOPE_CMP(src_scope, best_scope) : -1,
2323 in6_matchlen(IFA_IN6(ifa), dst),
2324 ((struct in6_ifaddr *)ifa)->ia6_flags);
2325 #endif
2326
2327 /*
2328 * Don't use an address before completing DAD
2329 * nor a duplicated address.
2330 */
2331 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2332 IN6_IFF_NOTREADY)
2333 continue;
2334
2335 /* XXX: is there any case to allow anycasts? */
2336 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2337 IN6_IFF_ANYCAST)
2338 continue;
2339
2340 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2341 IN6_IFF_DETACHED)
2342 continue;
2343
2344 /*
2345 * If this is the first address we find,
2346 * keep it anyway.
2347 */
2348 if (ifa_best == NULL)
2349 goto replace;
2350
2351 /*
2352 * ifa_best is never NULL beyond this line except
2353 * within the block labeled "replace".
2354 */
2355
2356 /*
2357 * If ifa_best has a smaller scope than dst and
2358 * the current address has a larger one than
2359 * (or equal to) dst, always replace ifa_best.
2360 * Also, if the current address has a smaller scope
2361 * than dst, ignore it unless ifa_best also has a
2362 * smaller scope.
2363 */
2364 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2365 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2366 goto replace;
2367 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2368 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2369 continue;
2370
2371 /*
2372 * A deprecated address SHOULD NOT be used in new
2373 * communications if an alternate (non-deprecated)
2374 * address is available and has sufficient scope.
2375 * RFC 2462, Section 5.5.4.
2376 */
2377 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2378 IN6_IFF_DEPRECATED) {
2379 /*
2380 * Ignore any deprecated addresses if
2381 * specified by configuration.
2382 */
2383 if (!ip6_use_deprecated)
2384 continue;
2385
2386 /*
2387 * If we have already found a non-deprecated
2388 * candidate, just ignore deprecated addresses.
2389 */
2390 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2391 == 0)
2392 continue;
2393 }
2394
2395 /*
2396 * A non-deprecated address is always preferred
2397 * to a deprecated one regardless of scopes and
2398 * address matching.
2399 */
2400 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2401 (((struct in6_ifaddr *)ifa)->ia6_flags &
2402 IN6_IFF_DEPRECATED) == 0)
2403 goto replace;
2404
2405 /*
2406 * At this point, we have two cases:
2407 * 1. we are looking at a non-deprecated address,
2408 * and ifa_best is also non-deprecated.
2409 * 2. we are looking at a deprecated address,
2410 * and ifa_best is also deprecated.
2411 * Also, we do not have to consider a case where
2412 * the scope of if_best is larger(smaller) than dst and
2413 * the scope of the current address is smaller(larger)
2414 * than dst. Such a case has already been covered.
2415 * Tiebreaking is done according to the following
2416 * items:
2417 * - the scope comparison between the address and
2418 * dst (dscopecmp)
2419 * - the scope comparison between the address and
2420 * ifa_best (bscopecmp)
2421 * - if the address match dst longer than ifa_best
2422 * (matchcmp)
2423 * - if the address is on the outgoing I/F (outI/F)
2424 *
2425 * Roughly speaking, the selection policy is
2426 * - the most important item is scope. The same scope
2427 * is best. Then search for a larger scope.
2428 * Smaller scopes are the last resort.
2429 * - A deprecated address is chosen only when we have
2430 * no address that has an enough scope, but is
2431 * prefered to any addresses of smaller scopes.
2432 * - Longest address match against dst is considered
2433 * only for addresses that has the same scope of dst.
2434 * - If there is no other reasons to choose one,
2435 * addresses on the outgoing I/F are preferred.
2436 *
2437 * The precise decision table is as follows:
2438 * dscopecmp bscopecmp matchcmp outI/F | replace?
2439 * !equal equal N/A Yes | Yes (1)
2440 * !equal equal N/A No | No (2)
2441 * larger larger N/A N/A | No (3)
2442 * larger smaller N/A N/A | Yes (4)
2443 * smaller larger N/A N/A | Yes (5)
2444 * smaller smaller N/A N/A | No (6)
2445 * equal smaller N/A N/A | Yes (7)
2446 * equal larger (already done)
2447 * equal equal larger N/A | Yes (8)
2448 * equal equal smaller N/A | No (9)
2449 * equal equal equal Yes | Yes (a)
2450 * eaual eqaul equal No | No (b)
2451 */
2452 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2453 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2454
2455 if (dscopecmp && bscopecmp == 0) {
2456 if (oifp == ifp) /* (1) */
2457 goto replace;
2458 continue; /* (2) */
2459 }
2460 if (dscopecmp > 0) {
2461 if (bscopecmp > 0) /* (3) */
2462 continue;
2463 goto replace; /* (4) */
2464 }
2465 if (dscopecmp < 0) {
2466 if (bscopecmp > 0) /* (5) */
2467 goto replace;
2468 continue; /* (6) */
2469 }
2470
2471 /* now dscopecmp must be 0 */
2472 if (bscopecmp < 0)
2473 goto replace; /* (7) */
2474
2475 /*
2476 * At last both dscopecmp and bscopecmp must be 0.
2477 * We need address matching against dst for
2478 * tiebreaking.
2479 */
2480 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2481 matchcmp = tlen - blen;
2482 if (matchcmp > 0) /* (8) */
2483 goto replace;
2484 if (matchcmp < 0) /* (9) */
2485 continue;
2486 if (oifp == ifp) /* (a) */
2487 goto replace;
2488 continue; /* (b) */
2489
2490 replace:
2491 ifa_best = (struct in6_ifaddr *)ifa;
2492 blen = tlen >= 0 ? tlen :
2493 in6_matchlen(IFA_IN6(ifa), dst);
2494 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2495 }
2496 }
2497
2498 /* count statistics for future improvements */
2499 if (ifa_best == NULL)
2500 ip6stat.ip6s_sources_none++;
2501 else {
2502 if (oifp == ifa_best->ia_ifp)
2503 ip6stat.ip6s_sources_sameif[best_scope]++;
2504 else
2505 ip6stat.ip6s_sources_otherif[best_scope]++;
2506
2507 if (best_scope == dst_scope)
2508 ip6stat.ip6s_sources_samescope[best_scope]++;
2509 else
2510 ip6stat.ip6s_sources_otherscope[best_scope]++;
2511
2512 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2513 ip6stat.ip6s_sources_deprecated[best_scope]++;
2514 }
2515
2516 return (ifa_best);
2517 }
2518
2519 /*
2520 * return the best address out of the same scope. if no address was
2521 * found, return the first valid address from designated IF.
2522 */
2523 struct in6_ifaddr *
in6_ifawithifp(ifp,dst)2524 in6_ifawithifp(ifp, dst)
2525 struct ifnet *ifp;
2526 struct in6_addr *dst;
2527 {
2528 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2529 struct ifaddr *ifa;
2530 struct in6_ifaddr *besta = 0;
2531 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
2532
2533 dep[0] = dep[1] = NULL;
2534
2535 /*
2536 * We first look for addresses in the same scope.
2537 * If there is one, return it.
2538 * If two or more, return one which matches the dst longest.
2539 * If none, return one of global addresses assigned other ifs.
2540 */
2541 for (ifa = ifp->if_addrlist.tqh_first;
2542 ifa;
2543 ifa = ifa->ifa_list.tqe_next)
2544 {
2545 if (ifa->ifa_addr->sa_family != AF_INET6)
2546 continue;
2547 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2548 continue; /* XXX: is there any case to allow anycast? */
2549 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2550 continue; /* don't use this interface */
2551 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2552 continue;
2553 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2554 if (ip6_use_deprecated)
2555 dep[0] = (struct in6_ifaddr *)ifa;
2556 continue;
2557 }
2558
2559 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2560 /*
2561 * call in6_matchlen() as few as possible
2562 */
2563 if (besta) {
2564 if (blen == -1)
2565 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2566 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2567 if (tlen > blen) {
2568 blen = tlen;
2569 besta = (struct in6_ifaddr *)ifa;
2570 }
2571 } else
2572 besta = (struct in6_ifaddr *)ifa;
2573 }
2574 }
2575 if (besta)
2576 return (besta);
2577
2578 for (ifa = ifp->if_addrlist.tqh_first;
2579 ifa;
2580 ifa = ifa->ifa_list.tqe_next)
2581 {
2582 if (ifa->ifa_addr->sa_family != AF_INET6)
2583 continue;
2584 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2585 continue; /* XXX: is there any case to allow anycast? */
2586 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2587 continue; /* don't use this interface */
2588 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2589 continue;
2590 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2591 if (ip6_use_deprecated)
2592 dep[1] = (struct in6_ifaddr *)ifa;
2593 continue;
2594 }
2595
2596 return (struct in6_ifaddr *)ifa;
2597 }
2598
2599 /* use the last-resort values, that are, deprecated addresses */
2600 if (dep[0])
2601 return dep[0];
2602 if (dep[1])
2603 return dep[1];
2604
2605 return NULL;
2606 }
2607
2608 /*
2609 * perform DAD when interface becomes IFF_UP.
2610 */
2611 void
in6_if_up(ifp)2612 in6_if_up(ifp)
2613 struct ifnet *ifp;
2614 {
2615 struct ifaddr *ifa;
2616 struct in6_ifaddr *ia;
2617 int dad_delay; /* delay ticks before DAD output */
2618
2619 /*
2620 * special cases, like 6to4, are handled in in6_ifattach
2621 */
2622 in6_ifattach(ifp, NULL);
2623
2624 dad_delay = 0;
2625 for (ifa = ifp->if_addrlist.tqh_first;
2626 ifa;
2627 ifa = ifa->ifa_list.tqe_next)
2628 {
2629 if (ifa->ifa_addr->sa_family != AF_INET6)
2630 continue;
2631 ia = (struct in6_ifaddr *)ifa;
2632 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2633 nd6_dad_start(ifa, &dad_delay);
2634 }
2635 }
2636
2637 int
in6if_do_dad(ifp)2638 in6if_do_dad(ifp)
2639 struct ifnet *ifp;
2640 {
2641 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2642 return (0);
2643
2644 switch (ifp->if_type) {
2645 case IFT_FAITH:
2646 /*
2647 * These interfaces do not have the IFF_LOOPBACK flag,
2648 * but loop packets back. We do not have to do DAD on such
2649 * interfaces. We should even omit it, because loop-backed
2650 * NS would confuse the DAD procedure.
2651 */
2652 return (0);
2653 default:
2654 /*
2655 * Our DAD routine requires the interface up and running.
2656 * However, some interfaces can be up before the RUNNING
2657 * status. Additionaly, users may try to assign addresses
2658 * before the interface becomes up (or running).
2659 * We simply skip DAD in such a case as a work around.
2660 * XXX: we should rather mark "tentative" on such addresses,
2661 * and do DAD after the interface becomes ready.
2662 */
2663 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2664 (IFF_UP|IFF_RUNNING))
2665 return (0);
2666
2667 return (1);
2668 }
2669 }
2670
2671 /*
2672 * Calculate max IPv6 MTU through all the interfaces and store it
2673 * to in6_maxmtu.
2674 */
2675 void
in6_setmaxmtu()2676 in6_setmaxmtu()
2677 {
2678 unsigned long maxmtu = 0;
2679 struct ifnet *ifp;
2680
2681 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2682 {
2683 /* this function can be called during ifnet initialization */
2684 if (!ifp->if_afdata[AF_INET6])
2685 continue;
2686 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2687 IN6_LINKMTU(ifp) > maxmtu)
2688 maxmtu = IN6_LINKMTU(ifp);
2689 }
2690 if (maxmtu) /* update only when maxmtu is positive */
2691 in6_maxmtu = maxmtu;
2692 }
2693
2694 void *
in6_domifattach(ifp)2695 in6_domifattach(ifp)
2696 struct ifnet *ifp;
2697 {
2698 struct in6_ifextra *ext;
2699
2700 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2701 bzero(ext, sizeof(*ext));
2702
2703 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2704 M_IFADDR, M_WAITOK);
2705 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2706
2707 ext->icmp6_ifstat =
2708 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2709 M_IFADDR, M_WAITOK);
2710 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2711
2712 ext->nd_ifinfo = nd6_ifattach(ifp);
2713 return ext;
2714 }
2715
2716 void
in6_domifdetach(ifp,aux)2717 in6_domifdetach(ifp, aux)
2718 struct ifnet *ifp;
2719 void *aux;
2720 {
2721 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2722
2723 nd6_ifdetach(ext->nd_ifinfo);
2724 free(ext->in6_ifstat, M_IFADDR);
2725 free(ext->icmp6_ifstat, M_IFADDR);
2726 free(ext, M_IFADDR);
2727 }
2728