FreeBSD/Linux Kernel Cross Reference
sys/netinet6/in6.c
1 /*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $
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 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)in.c 8.2 (Berkeley) 11/15/93
61 */
62
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD: releng/6.4/sys/netinet6/in6.c 193893 2009-06-10 10:31:11Z cperciva $");
65
66 #include "opt_inet.h"
67 #include "opt_inet6.h"
68
69 #include <sys/param.h>
70 #include <sys/errno.h>
71 #include <sys/malloc.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/sockio.h>
75 #include <sys/systm.h>
76 #include <sys/proc.h>
77 #include <sys/time.h>
78 #include <sys/kernel.h>
79 #include <sys/syslog.h>
80
81 #include <net/if.h>
82 #include <net/if_types.h>
83 #include <net/route.h>
84 #include <net/if_dl.h>
85
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet/if_ether.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/in_pcb.h>
92
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet6/nd6.h>
96 #include <netinet6/mld6_var.h>
97 #include <netinet6/ip6_mroute.h>
98 #include <netinet6/in6_ifattach.h>
99 #include <netinet6/scope6_var.h>
100 #include <netinet6/in6_pcb.h>
101
102 #include <net/net_osdep.h>
103
104 MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "internet multicast address");
105
106 /*
107 * Definitions of some costant IP6 addresses.
108 */
109 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
110 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
111 const struct in6_addr in6addr_nodelocal_allnodes =
112 IN6ADDR_NODELOCAL_ALLNODES_INIT;
113 const struct in6_addr in6addr_linklocal_allnodes =
114 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
115 const struct in6_addr in6addr_linklocal_allrouters =
116 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
117
118 const struct in6_addr in6mask0 = IN6MASK0;
119 const struct in6_addr in6mask32 = IN6MASK32;
120 const struct in6_addr in6mask64 = IN6MASK64;
121 const struct in6_addr in6mask96 = IN6MASK96;
122 const struct in6_addr in6mask128 = IN6MASK128;
123
124 const struct sockaddr_in6 sa6_any =
125 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
126
127 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
128 struct ifnet *, struct thread *));
129 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
130 struct sockaddr_in6 *, int));
131 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
132
133 struct in6_multihead in6_multihead; /* XXX BSS initialization */
134 int (*faithprefix_p)(struct in6_addr *);
135
136 /*
137 * Subroutine for in6_ifaddloop() and in6_ifremloop().
138 * This routine does actual work.
139 */
140 static void
141 in6_ifloop_request(int cmd, struct ifaddr *ifa)
142 {
143 struct sockaddr_in6 all1_sa;
144 struct rtentry *nrt = NULL;
145 int e;
146
147 bzero(&all1_sa, sizeof(all1_sa));
148 all1_sa.sin6_family = AF_INET6;
149 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
150 all1_sa.sin6_addr = in6mask128;
151
152 /*
153 * We specify the address itself as the gateway, and set the
154 * RTF_LLINFO flag, so that the corresponding host route would have
155 * the flag, and thus applications that assume traditional behavior
156 * would be happy. Note that we assume the caller of the function
157 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
158 * which changes the outgoing interface to the loopback interface.
159 */
160 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
161 (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
162 if (e != 0) {
163 /* XXX need more descriptive message */
164 log(LOG_ERR, "in6_ifloop_request: "
165 "%s operation failed for %s (errno=%d)\n",
166 cmd == RTM_ADD ? "ADD" : "DELETE",
167 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
168 e);
169 }
170
171 /*
172 * Report the addition/removal of the address to the routing socket.
173 * XXX: since we called rtinit for a p2p interface with a destination,
174 * we end up reporting twice in such a case. Should we rather
175 * omit the second report?
176 */
177 if (nrt) {
178 RT_LOCK(nrt);
179 /*
180 * Make sure rt_ifa be equal to IFA, the second argument of
181 * the function. We need this because when we refer to
182 * rt_ifa->ia6_flags in ip6_input, we assume that the rt_ifa
183 * points to the address instead of the loopback address.
184 */
185 if (cmd == RTM_ADD && ifa != nrt->rt_ifa) {
186 IFAFREE(nrt->rt_ifa);
187 IFAREF(ifa);
188 nrt->rt_ifa = ifa;
189 }
190
191 rt_newaddrmsg(cmd, ifa, e, nrt);
192 if (cmd == RTM_DELETE) {
193 rtfree(nrt);
194 } else {
195 /* the cmd must be RTM_ADD here */
196 RT_REMREF(nrt);
197 RT_UNLOCK(nrt);
198 }
199 }
200 }
201
202 /*
203 * Add ownaddr as loopback rtentry. We previously add the route only if
204 * necessary (ex. on a p2p link). However, since we now manage addresses
205 * separately from prefixes, we should always add the route. We can't
206 * rely on the cloning mechanism from the corresponding interface route
207 * any more.
208 */
209 void
210 in6_ifaddloop(struct ifaddr *ifa)
211 {
212 struct rtentry *rt;
213 int need_loop;
214
215 /* If there is no loopback entry, allocate one. */
216 rt = rtalloc1(ifa->ifa_addr, 0, 0);
217 need_loop = (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
218 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0);
219 if (rt)
220 rtfree(rt);
221 if (need_loop)
222 in6_ifloop_request(RTM_ADD, ifa);
223 }
224
225 /*
226 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
227 * if it exists.
228 */
229 void
230 in6_ifremloop(struct ifaddr *ifa)
231 {
232 struct in6_ifaddr *ia;
233 struct rtentry *rt;
234 int ia_count = 0;
235
236 /*
237 * Some of BSD variants do not remove cloned routes
238 * from an interface direct route, when removing the direct route
239 * (see comments in net/net_osdep.h). Even for variants that do remove
240 * cloned routes, they could fail to remove the cloned routes when
241 * we handle multple addresses that share a common prefix.
242 * So, we should remove the route corresponding to the deleted address.
243 */
244
245 /*
246 * Delete the entry only if exact one ifa exists. More than one ifa
247 * can exist if we assign a same single address to multiple
248 * (probably p2p) interfaces.
249 * XXX: we should avoid such a configuration in IPv6...
250 */
251 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
252 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
253 ia_count++;
254 if (ia_count > 1)
255 break;
256 }
257 }
258
259 if (ia_count == 1) {
260 /*
261 * Before deleting, check if a corresponding loopbacked host
262 * route surely exists. With this check, we can avoid to
263 * delete an interface direct route whose destination is same
264 * as the address being removed. This can happen when removing
265 * a subnet-router anycast address on an interface attahced
266 * to a shared medium.
267 */
268 rt = rtalloc1(ifa->ifa_addr, 0, 0);
269 if (rt != NULL) {
270 if ((rt->rt_flags & RTF_HOST) != 0 &&
271 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
272 rtfree(rt);
273 in6_ifloop_request(RTM_DELETE, ifa);
274 } else
275 RT_UNLOCK(rt);
276 }
277 }
278 }
279
280 int
281 in6_mask2len(mask, lim0)
282 struct in6_addr *mask;
283 u_char *lim0;
284 {
285 int x = 0, y;
286 u_char *lim = lim0, *p;
287
288 /* ignore the scope_id part */
289 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
290 lim = (u_char *)mask + sizeof(*mask);
291 for (p = (u_char *)mask; p < lim; x++, p++) {
292 if (*p != 0xff)
293 break;
294 }
295 y = 0;
296 if (p < lim) {
297 for (y = 0; y < 8; y++) {
298 if ((*p & (0x80 >> y)) == 0)
299 break;
300 }
301 }
302
303 /*
304 * when the limit pointer is given, do a stricter check on the
305 * remaining bits.
306 */
307 if (p < lim) {
308 if (y != 0 && (*p & (0x00ff >> y)) != 0)
309 return (-1);
310 for (p = p + 1; p < lim; p++)
311 if (*p != 0)
312 return (-1);
313 }
314
315 return x * 8 + y;
316 }
317
318 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
319 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
320
321 int
322 in6_control(so, cmd, data, ifp, td)
323 struct socket *so;
324 u_long cmd;
325 caddr_t data;
326 struct ifnet *ifp;
327 struct thread *td;
328 {
329 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
330 struct in6_ifaddr *ia = NULL;
331 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
332 int error, privileged;
333 struct sockaddr_in6 *sa6;
334
335 privileged = 0;
336 if (td == NULL || !suser(td))
337 privileged++;
338
339 switch (cmd) {
340 case SIOCGETSGCNT_IN6:
341 case SIOCGETMIFCNT_IN6:
342 return (mrt6_ioctl(cmd, data));
343 }
344
345 switch(cmd) {
346 case SIOCAADDRCTL_POLICY:
347 case SIOCDADDRCTL_POLICY:
348 if (!privileged)
349 return (EPERM);
350 return (in6_src_ioctl(cmd, data));
351 }
352
353 if (ifp == NULL)
354 return (EOPNOTSUPP);
355
356 switch (cmd) {
357 case SIOCSNDFLUSH_IN6:
358 case SIOCSPFXFLUSH_IN6:
359 case SIOCSRTRFLUSH_IN6:
360 case SIOCSDEFIFACE_IN6:
361 case SIOCSIFINFO_FLAGS:
362 case SIOCSIFINFO_IN6:
363 if (!privileged)
364 return (EPERM);
365 /* FALLTHROUGH */
366 case OSIOCGIFINFO_IN6:
367 case SIOCGIFINFO_IN6:
368 case SIOCGDRLST_IN6:
369 case SIOCGPRLST_IN6:
370 case SIOCGNBRINFO_IN6:
371 case SIOCGDEFIFACE_IN6:
372 return (nd6_ioctl(cmd, data, ifp));
373 }
374
375 switch (cmd) {
376 case SIOCSIFPREFIX_IN6:
377 case SIOCDIFPREFIX_IN6:
378 case SIOCAIFPREFIX_IN6:
379 case SIOCCIFPREFIX_IN6:
380 case SIOCSGIFPREFIX_IN6:
381 case SIOCGIFPREFIX_IN6:
382 log(LOG_NOTICE,
383 "prefix ioctls are now invalidated. "
384 "please use ifconfig.\n");
385 return (EOPNOTSUPP);
386 }
387
388 switch (cmd) {
389 case SIOCSSCOPE6:
390 if (!privileged)
391 return (EPERM);
392 return (scope6_set(ifp,
393 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
394 case SIOCGSCOPE6:
395 return (scope6_get(ifp,
396 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
397 case SIOCGSCOPE6DEF:
398 return (scope6_get_default((struct scope6_id *)
399 ifr->ifr_ifru.ifru_scope_id));
400 }
401
402 switch (cmd) {
403 case SIOCALIFADDR:
404 case SIOCDLIFADDR:
405 if (!privileged)
406 return (EPERM);
407 /* FALLTHROUGH */
408 case SIOCGLIFADDR:
409 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
410 }
411
412 /*
413 * Find address for this interface, if it exists.
414 *
415 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
416 * only, and used the first interface address as the target of other
417 * operations (without checking ifra_addr). This was because netinet
418 * code/API assumed at most 1 interface address per interface.
419 * Since IPv6 allows a node to assign multiple addresses
420 * on a single interface, we almost always look and check the
421 * presence of ifra_addr, and reject invalid ones here.
422 * It also decreases duplicated code among SIOC*_IN6 operations.
423 */
424 switch (cmd) {
425 case SIOCAIFADDR_IN6:
426 case SIOCSIFPHYADDR_IN6:
427 sa6 = &ifra->ifra_addr;
428 break;
429 case SIOCSIFADDR_IN6:
430 case SIOCGIFADDR_IN6:
431 case SIOCSIFDSTADDR_IN6:
432 case SIOCSIFNETMASK_IN6:
433 case SIOCGIFDSTADDR_IN6:
434 case SIOCGIFNETMASK_IN6:
435 case SIOCDIFADDR_IN6:
436 case SIOCGIFPSRCADDR_IN6:
437 case SIOCGIFPDSTADDR_IN6:
438 case SIOCGIFAFLAG_IN6:
439 case SIOCSNDFLUSH_IN6:
440 case SIOCSPFXFLUSH_IN6:
441 case SIOCSRTRFLUSH_IN6:
442 case SIOCGIFALIFETIME_IN6:
443 case SIOCSIFALIFETIME_IN6:
444 case SIOCGIFSTAT_IN6:
445 case SIOCGIFSTAT_ICMP6:
446 sa6 = &ifr->ifr_addr;
447 break;
448 default:
449 sa6 = NULL;
450 break;
451 }
452 if (sa6 && sa6->sin6_family == AF_INET6) {
453 int error = 0;
454
455 if (sa6->sin6_scope_id != 0)
456 error = sa6_embedscope(sa6, 0);
457 else
458 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
459 if (error != 0)
460 return (error);
461 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
462 } else
463 ia = NULL;
464
465 switch (cmd) {
466 case SIOCSIFADDR_IN6:
467 case SIOCSIFDSTADDR_IN6:
468 case SIOCSIFNETMASK_IN6:
469 /*
470 * Since IPv6 allows a node to assign multiple addresses
471 * on a single interface, SIOCSIFxxx ioctls are deprecated.
472 */
473 /* we decided to obsolete this command (20000704) */
474 return (EINVAL);
475
476 case SIOCDIFADDR_IN6:
477 /*
478 * for IPv4, we look for existing in_ifaddr here to allow
479 * "ifconfig if0 delete" to remove the first IPv4 address on
480 * the interface. For IPv6, as the spec allows multiple
481 * interface address from the day one, we consider "remove the
482 * first one" semantics to be not preferable.
483 */
484 if (ia == NULL)
485 return (EADDRNOTAVAIL);
486 /* FALLTHROUGH */
487 case SIOCAIFADDR_IN6:
488 /*
489 * We always require users to specify a valid IPv6 address for
490 * the corresponding operation.
491 */
492 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
493 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
494 return (EAFNOSUPPORT);
495 if (!privileged)
496 return (EPERM);
497
498 break;
499
500 case SIOCGIFADDR_IN6:
501 /* This interface is basically deprecated. use SIOCGIFCONF. */
502 /* FALLTHROUGH */
503 case SIOCGIFAFLAG_IN6:
504 case SIOCGIFNETMASK_IN6:
505 case SIOCGIFDSTADDR_IN6:
506 case SIOCGIFALIFETIME_IN6:
507 /* must think again about its semantics */
508 if (ia == NULL)
509 return (EADDRNOTAVAIL);
510 break;
511 case SIOCSIFALIFETIME_IN6:
512 {
513 struct in6_addrlifetime *lt;
514
515 if (!privileged)
516 return (EPERM);
517 if (ia == NULL)
518 return (EADDRNOTAVAIL);
519 /* sanity for overflow - beware unsigned */
520 lt = &ifr->ifr_ifru.ifru_lifetime;
521 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
522 lt->ia6t_vltime + time_second < time_second) {
523 return EINVAL;
524 }
525 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
526 lt->ia6t_pltime + time_second < time_second) {
527 return EINVAL;
528 }
529 break;
530 }
531 }
532
533 switch (cmd) {
534
535 case SIOCGIFADDR_IN6:
536 ifr->ifr_addr = ia->ia_addr;
537 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
538 return (error);
539 break;
540
541 case SIOCGIFDSTADDR_IN6:
542 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
543 return (EINVAL);
544 /*
545 * XXX: should we check if ifa_dstaddr is NULL and return
546 * an error?
547 */
548 ifr->ifr_dstaddr = ia->ia_dstaddr;
549 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
550 return (error);
551 break;
552
553 case SIOCGIFNETMASK_IN6:
554 ifr->ifr_addr = ia->ia_prefixmask;
555 break;
556
557 case SIOCGIFAFLAG_IN6:
558 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
559 break;
560
561 case SIOCGIFSTAT_IN6:
562 if (ifp == NULL)
563 return EINVAL;
564 bzero(&ifr->ifr_ifru.ifru_stat,
565 sizeof(ifr->ifr_ifru.ifru_stat));
566 ifr->ifr_ifru.ifru_stat =
567 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
568 break;
569
570 case SIOCGIFSTAT_ICMP6:
571 if (ifp == NULL)
572 return EINVAL;
573 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
574 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
575 ifr->ifr_ifru.ifru_icmp6stat =
576 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
577 break;
578
579 case SIOCGIFALIFETIME_IN6:
580 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
581 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
582 time_t maxexpire;
583 struct in6_addrlifetime *retlt =
584 &ifr->ifr_ifru.ifru_lifetime;
585
586 /*
587 * XXX: adjust expiration time assuming time_t is
588 * signed.
589 */
590 maxexpire = (-1) &
591 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
592 if (ia->ia6_lifetime.ia6t_vltime <
593 maxexpire - ia->ia6_updatetime) {
594 retlt->ia6t_expire = ia->ia6_updatetime +
595 ia->ia6_lifetime.ia6t_vltime;
596 } else
597 retlt->ia6t_expire = maxexpire;
598 }
599 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
600 time_t maxexpire;
601 struct in6_addrlifetime *retlt =
602 &ifr->ifr_ifru.ifru_lifetime;
603
604 /*
605 * XXX: adjust expiration time assuming time_t is
606 * signed.
607 */
608 maxexpire = (-1) &
609 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
610 if (ia->ia6_lifetime.ia6t_pltime <
611 maxexpire - ia->ia6_updatetime) {
612 retlt->ia6t_preferred = ia->ia6_updatetime +
613 ia->ia6_lifetime.ia6t_pltime;
614 } else
615 retlt->ia6t_preferred = maxexpire;
616 }
617 break;
618
619 case SIOCSIFALIFETIME_IN6:
620 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
621 /* for sanity */
622 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
623 ia->ia6_lifetime.ia6t_expire =
624 time_second + ia->ia6_lifetime.ia6t_vltime;
625 } else
626 ia->ia6_lifetime.ia6t_expire = 0;
627 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
628 ia->ia6_lifetime.ia6t_preferred =
629 time_second + ia->ia6_lifetime.ia6t_pltime;
630 } else
631 ia->ia6_lifetime.ia6t_preferred = 0;
632 break;
633
634 case SIOCAIFADDR_IN6:
635 {
636 int i, error = 0;
637 struct nd_prefixctl pr0;
638 struct nd_prefix *pr;
639
640 /*
641 * first, make or update the interface address structure,
642 * and link it to the list.
643 */
644 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
645 return (error);
646 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
647 == NULL) {
648 /*
649 * this can happen when the user specify the 0 valid
650 * lifetime.
651 */
652 break;
653 }
654
655 /*
656 * then, make the prefix on-link on the interface.
657 * XXX: we'd rather create the prefix before the address, but
658 * we need at least one address to install the corresponding
659 * interface route, so we configure the address first.
660 */
661
662 /*
663 * convert mask to prefix length (prefixmask has already
664 * been validated in in6_update_ifa().
665 */
666 bzero(&pr0, sizeof(pr0));
667 pr0.ndpr_ifp = ifp;
668 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
669 NULL);
670 if (pr0.ndpr_plen == 128) {
671 break; /* we don't need to install a host route. */
672 }
673 pr0.ndpr_prefix = ifra->ifra_addr;
674 /* apply the mask for safety. */
675 for (i = 0; i < 4; i++) {
676 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
677 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
678 }
679 /*
680 * XXX: since we don't have an API to set prefix (not address)
681 * lifetimes, we just use the same lifetimes as addresses.
682 * The (temporarily) installed lifetimes can be overridden by
683 * later advertised RAs (when accept_rtadv is non 0), which is
684 * an intended behavior.
685 */
686 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
687 pr0.ndpr_raf_auto =
688 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
689 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
690 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
691
692 /* add the prefix if not yet. */
693 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
694 /*
695 * nd6_prelist_add will install the corresponding
696 * interface route.
697 */
698 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
699 return (error);
700 if (pr == NULL) {
701 log(LOG_ERR, "nd6_prelist_add succeeded but "
702 "no prefix\n");
703 return (EINVAL); /* XXX panic here? */
704 }
705 }
706
707 /* relate the address to the prefix */
708 if (ia->ia6_ndpr == NULL) {
709 ia->ia6_ndpr = pr;
710 pr->ndpr_refcnt++;
711
712 /*
713 * If this is the first autoconf address from the
714 * prefix, create a temporary address as well
715 * (when required).
716 */
717 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
718 ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
719 int e;
720 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
721 log(LOG_NOTICE, "in6_control: failed "
722 "to create a temporary address, "
723 "errno=%d\n", e);
724 }
725 }
726 }
727
728 /*
729 * this might affect the status of autoconfigured addresses,
730 * that is, this address might make other addresses detached.
731 */
732 pfxlist_onlink_check();
733 if (error == 0 && ia)
734 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
735 break;
736 }
737
738 case SIOCDIFADDR_IN6:
739 {
740 struct nd_prefix *pr;
741
742 /*
743 * If the address being deleted is the only one that owns
744 * the corresponding prefix, expire the prefix as well.
745 * XXX: theoretically, we don't have to worry about such
746 * relationship, since we separate the address management
747 * and the prefix management. We do this, however, to provide
748 * as much backward compatibility as possible in terms of
749 * the ioctl operation.
750 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
751 */
752 pr = ia->ia6_ndpr;
753 in6_purgeaddr(&ia->ia_ifa);
754 if (pr && pr->ndpr_refcnt == 0)
755 prelist_remove(pr);
756 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
757 break;
758 }
759
760 default:
761 if (ifp == NULL || ifp->if_ioctl == 0)
762 return (EOPNOTSUPP);
763 return ((*ifp->if_ioctl)(ifp, cmd, data));
764 }
765
766 return (0);
767 }
768
769 /*
770 * Update parameters of an IPv6 interface address.
771 * If necessary, a new entry is created and linked into address chains.
772 * This function is separated from in6_control().
773 * XXX: should this be performed under splnet()?
774 */
775 int
776 in6_update_ifa(ifp, ifra, ia, flags)
777 struct ifnet *ifp;
778 struct in6_aliasreq *ifra;
779 struct in6_ifaddr *ia;
780 int flags;
781 {
782 int error = 0, hostIsNew = 0, plen = -1;
783 struct in6_ifaddr *oia;
784 struct sockaddr_in6 dst6;
785 struct in6_addrlifetime *lt;
786 struct in6_multi_mship *imm;
787 struct in6_multi *in6m_sol;
788 struct rtentry *rt;
789 int delay;
790
791 /* Validate parameters */
792 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
793 return (EINVAL);
794
795 /*
796 * The destination address for a p2p link must have a family
797 * of AF_UNSPEC or AF_INET6.
798 */
799 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
800 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
801 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
802 return (EAFNOSUPPORT);
803 /*
804 * validate ifra_prefixmask. don't check sin6_family, netmask
805 * does not carry fields other than sin6_len.
806 */
807 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
808 return (EINVAL);
809 /*
810 * Because the IPv6 address architecture is classless, we require
811 * users to specify a (non 0) prefix length (mask) for a new address.
812 * We also require the prefix (when specified) mask is valid, and thus
813 * reject a non-consecutive mask.
814 */
815 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
816 return (EINVAL);
817 if (ifra->ifra_prefixmask.sin6_len != 0) {
818 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
819 (u_char *)&ifra->ifra_prefixmask +
820 ifra->ifra_prefixmask.sin6_len);
821 if (plen <= 0)
822 return (EINVAL);
823 } else {
824 /*
825 * In this case, ia must not be NULL. We just use its prefix
826 * length.
827 */
828 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
829 }
830 /*
831 * If the destination address on a p2p interface is specified,
832 * and the address is a scoped one, validate/set the scope
833 * zone identifier.
834 */
835 dst6 = ifra->ifra_dstaddr;
836 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
837 (dst6.sin6_family == AF_INET6)) {
838 struct in6_addr in6_tmp;
839 u_int32_t zoneid;
840
841 in6_tmp = dst6.sin6_addr;
842 if (in6_setscope(&in6_tmp, ifp, &zoneid))
843 return (EINVAL); /* XXX: should be impossible */
844
845 if (dst6.sin6_scope_id != 0) {
846 if (dst6.sin6_scope_id != zoneid)
847 return (EINVAL);
848 } else /* user omit to specify the ID. */
849 dst6.sin6_scope_id = zoneid;
850
851 /* convert into the internal form */
852 if (sa6_embedscope(&dst6, 0))
853 return (EINVAL); /* XXX: should be impossible */
854 }
855 /*
856 * The destination address can be specified only for a p2p or a
857 * loopback interface. If specified, the corresponding prefix length
858 * must be 128.
859 */
860 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
861 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
862 /* XXX: noisy message */
863 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
864 "be specified for a p2p or a loopback IF only\n"));
865 return (EINVAL);
866 }
867 if (plen != 128) {
868 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
869 "be 128 when dstaddr is specified\n"));
870 return (EINVAL);
871 }
872 }
873 /* lifetime consistency check */
874 lt = &ifra->ifra_lifetime;
875 if (lt->ia6t_pltime > lt->ia6t_vltime)
876 return (EINVAL);
877 if (lt->ia6t_vltime == 0) {
878 /*
879 * the following log might be noisy, but this is a typical
880 * configuration mistake or a tool's bug.
881 */
882 nd6log((LOG_INFO,
883 "in6_update_ifa: valid lifetime is 0 for %s\n",
884 ip6_sprintf(&ifra->ifra_addr.sin6_addr)));
885
886 if (ia == NULL)
887 return (0); /* there's nothing to do */
888 }
889
890 /*
891 * If this is a new address, allocate a new ifaddr and link it
892 * into chains.
893 */
894 if (ia == NULL) {
895 hostIsNew = 1;
896 /*
897 * When in6_update_ifa() is called in a process of a received
898 * RA, it is called under an interrupt context. So, we should
899 * call malloc with M_NOWAIT.
900 */
901 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
902 M_NOWAIT);
903 if (ia == NULL)
904 return (ENOBUFS);
905 bzero((caddr_t)ia, sizeof(*ia));
906 LIST_INIT(&ia->ia6_memberships);
907 /* Initialize the address and masks, and put time stamp */
908 IFA_LOCK_INIT(&ia->ia_ifa);
909 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
910 ia->ia_addr.sin6_family = AF_INET6;
911 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
912 ia->ia6_createtime = time_second;
913 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
914 /*
915 * XXX: some functions expect that ifa_dstaddr is not
916 * NULL for p2p interfaces.
917 */
918 ia->ia_ifa.ifa_dstaddr =
919 (struct sockaddr *)&ia->ia_dstaddr;
920 } else {
921 ia->ia_ifa.ifa_dstaddr = NULL;
922 }
923 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
924
925 ia->ia_ifp = ifp;
926 if ((oia = in6_ifaddr) != NULL) {
927 for ( ; oia->ia_next; oia = oia->ia_next)
928 continue;
929 oia->ia_next = ia;
930 } else
931 in6_ifaddr = ia;
932
933 ia->ia_ifa.ifa_refcnt = 1;
934 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
935 }
936
937 /* update timestamp */
938 ia->ia6_updatetime = time_second;
939
940 /* set prefix mask */
941 if (ifra->ifra_prefixmask.sin6_len) {
942 /*
943 * We prohibit changing the prefix length of an existing
944 * address, because
945 * + such an operation should be rare in IPv6, and
946 * + the operation would confuse prefix management.
947 */
948 if (ia->ia_prefixmask.sin6_len &&
949 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
950 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
951 " existing (%s) address should not be changed\n",
952 ip6_sprintf(&ia->ia_addr.sin6_addr)));
953 error = EINVAL;
954 goto unlink;
955 }
956 ia->ia_prefixmask = ifra->ifra_prefixmask;
957 }
958
959 /*
960 * If a new destination address is specified, scrub the old one and
961 * install the new destination. Note that the interface must be
962 * p2p or loopback (see the check above.)
963 */
964 if (dst6.sin6_family == AF_INET6 &&
965 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
966 int e;
967
968 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
969 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
970 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
971 "a route to the old destination: %s\n",
972 ip6_sprintf(&ia->ia_addr.sin6_addr)));
973 /* proceed anyway... */
974 } else
975 ia->ia_flags &= ~IFA_ROUTE;
976 ia->ia_dstaddr = dst6;
977 }
978
979 /*
980 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
981 * to see if the address is deprecated or invalidated, but initialize
982 * these members for applications.
983 */
984 ia->ia6_lifetime = ifra->ifra_lifetime;
985 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
986 ia->ia6_lifetime.ia6t_expire =
987 time_second + ia->ia6_lifetime.ia6t_vltime;
988 } else
989 ia->ia6_lifetime.ia6t_expire = 0;
990 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
991 ia->ia6_lifetime.ia6t_preferred =
992 time_second + ia->ia6_lifetime.ia6t_pltime;
993 } else
994 ia->ia6_lifetime.ia6t_preferred = 0;
995
996 /* reset the interface and routing table appropriately. */
997 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
998 goto unlink;
999
1000 /*
1001 * configure address flags.
1002 */
1003 ia->ia6_flags = ifra->ifra_flags;
1004 /*
1005 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1006 * userland, make it deprecated.
1007 */
1008 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1009 ia->ia6_lifetime.ia6t_pltime = 0;
1010 ia->ia6_lifetime.ia6t_preferred = time_second;
1011 }
1012 /*
1013 * Make the address tentative before joining multicast addresses,
1014 * so that corresponding MLD responses would not have a tentative
1015 * source address.
1016 */
1017 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1018 if (hostIsNew && in6if_do_dad(ifp))
1019 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1020
1021 /*
1022 * We are done if we have simply modified an existing address.
1023 */
1024 if (!hostIsNew)
1025 return (error);
1026
1027 /*
1028 * Beyond this point, we should call in6_purgeaddr upon an error,
1029 * not just go to unlink.
1030 */
1031
1032 /* Join necessary multicast groups */
1033 in6m_sol = NULL;
1034 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1035 struct sockaddr_in6 mltaddr, mltmask;
1036 struct in6_addr llsol;
1037
1038 /* join solicited multicast addr for new host id */
1039 bzero(&llsol, sizeof(struct in6_addr));
1040 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1041 llsol.s6_addr32[1] = 0;
1042 llsol.s6_addr32[2] = htonl(1);
1043 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
1044 llsol.s6_addr8[12] = 0xff;
1045 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
1046 /* XXX: should not happen */
1047 log(LOG_ERR, "in6_update_ifa: "
1048 "in6_setscope failed\n");
1049 goto cleanup;
1050 }
1051 delay = 0;
1052 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1053 /*
1054 * We need a random delay for DAD on the address
1055 * being configured. It also means delaying
1056 * transmission of the corresponding MLD report to
1057 * avoid report collision.
1058 * [draft-ietf-ipv6-rfc2462bis-02.txt]
1059 */
1060 delay = arc4random() %
1061 (MAX_RTR_SOLICITATION_DELAY * hz);
1062 }
1063 imm = in6_joingroup(ifp, &llsol, &error, delay);
1064 if (imm == NULL) {
1065 nd6log((LOG_WARNING,
1066 "in6_update_ifa: addmulti failed for "
1067 "%s on %s (errno=%d)\n",
1068 ip6_sprintf(&llsol), if_name(ifp),
1069 error));
1070 in6_purgeaddr((struct ifaddr *)ia);
1071 return (error);
1072 }
1073 LIST_INSERT_HEAD(&ia->ia6_memberships,
1074 imm, i6mm_chain);
1075 in6m_sol = imm->i6mm_maddr;
1076
1077 bzero(&mltmask, sizeof(mltmask));
1078 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1079 mltmask.sin6_family = AF_INET6;
1080 mltmask.sin6_addr = in6mask32;
1081 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1082
1083 /*
1084 * join link-local all-nodes address
1085 */
1086 bzero(&mltaddr, sizeof(mltaddr));
1087 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1088 mltaddr.sin6_family = AF_INET6;
1089 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1090 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1091 0)
1092 goto cleanup; /* XXX: should not fail */
1093
1094 /*
1095 * XXX: do we really need this automatic routes?
1096 * We should probably reconsider this stuff. Most applications
1097 * actually do not need the routes, since they usually specify
1098 * the outgoing interface.
1099 */
1100 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1101 if (rt) {
1102 if (memcmp(&mltaddr.sin6_addr,
1103 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1104 MLTMASK_LEN)) {
1105 RTFREE_LOCKED(rt);
1106 rt = NULL;
1107 }
1108 }
1109 if (!rt) {
1110 /* XXX: we need RTF_CLONING to fake nd6_rtrequest */
1111 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1112 (struct sockaddr *)&ia->ia_addr,
1113 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1114 (struct rtentry **)0);
1115 if (error)
1116 goto cleanup;
1117 } else
1118 RTFREE_LOCKED(rt);
1119
1120 /*
1121 * XXX: do we really need this automatic routes?
1122 * We should probably reconsider this stuff. Most applications
1123 * actually do not need the routes, since they usually specify
1124 * the outgoing interface.
1125 */
1126 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1127 if (rt) {
1128 /* XXX: only works in !SCOPEDROUTING case. */
1129 if (memcmp(&mltaddr.sin6_addr,
1130 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1131 MLTMASK_LEN)) {
1132 RTFREE_LOCKED(rt);
1133 rt = NULL;
1134 }
1135 }
1136 if (!rt) {
1137 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1138 (struct sockaddr *)&ia->ia_addr,
1139 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1140 (struct rtentry **)0);
1141 if (error)
1142 goto cleanup;
1143 } else {
1144 RTFREE_LOCKED(rt);
1145 }
1146
1147 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1148 if (!imm) {
1149 nd6log((LOG_WARNING,
1150 "in6_update_ifa: addmulti failed for "
1151 "%s on %s (errno=%d)\n",
1152 ip6_sprintf(&mltaddr.sin6_addr),
1153 if_name(ifp), error));
1154 goto cleanup;
1155 }
1156 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1157
1158 /*
1159 * join node information group address
1160 */
1161 #define hostnamelen strlen(hostname)
1162 delay = 0;
1163 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1164 /*
1165 * The spec doesn't say anything about delay for this
1166 * group, but the same logic should apply.
1167 */
1168 delay = arc4random() %
1169 (MAX_RTR_SOLICITATION_DELAY * hz);
1170 }
1171 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1172 == 0) {
1173 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1174 delay); /* XXX jinmei */
1175 if (!imm) {
1176 nd6log((LOG_WARNING, "in6_update_ifa: "
1177 "addmulti failed for %s on %s "
1178 "(errno=%d)\n",
1179 ip6_sprintf(&mltaddr.sin6_addr),
1180 if_name(ifp), error));
1181 /* XXX not very fatal, go on... */
1182 } else {
1183 LIST_INSERT_HEAD(&ia->ia6_memberships,
1184 imm, i6mm_chain);
1185 }
1186 }
1187 #undef hostnamelen
1188
1189 /*
1190 * join interface-local all-nodes address.
1191 * (ff01::1%ifN, and ff01::%ifN/32)
1192 */
1193 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1194 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
1195 != 0)
1196 goto cleanup; /* XXX: should not fail */
1197 /* XXX: again, do we really need the route? */
1198 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1199 if (rt) {
1200 if (memcmp(&mltaddr.sin6_addr,
1201 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1202 MLTMASK_LEN)) {
1203 RTFREE_LOCKED(rt);
1204 rt = NULL;
1205 }
1206 }
1207 if (!rt) {
1208 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1209 (struct sockaddr *)&ia->ia_addr,
1210 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1211 (struct rtentry **)0);
1212 if (error)
1213 goto cleanup;
1214 } else
1215 RTFREE_LOCKED(rt);
1216
1217 /* XXX: again, do we really need the route? */
1218 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1219 if (rt) {
1220 if (memcmp(&mltaddr.sin6_addr,
1221 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1222 MLTMASK_LEN)) {
1223 RTFREE_LOCKED(rt);
1224 rt = NULL;
1225 }
1226 }
1227 if (!rt) {
1228 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1229 (struct sockaddr *)&ia->ia_addr,
1230 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1231 (struct rtentry **)0);
1232 if (error)
1233 goto cleanup;
1234 } else {
1235 RTFREE_LOCKED(rt);
1236 }
1237
1238 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1239 if (!imm) {
1240 nd6log((LOG_WARNING, "in6_update_ifa: "
1241 "addmulti failed for %s on %s "
1242 "(errno=%d)\n",
1243 ip6_sprintf(&mltaddr.sin6_addr),
1244 if_name(ifp), error));
1245 goto cleanup;
1246 }
1247 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1248 #undef MLTMASK_LEN
1249 }
1250
1251 /*
1252 * Perform DAD, if needed.
1253 * XXX It may be of use, if we can administratively
1254 * disable DAD.
1255 */
1256 if (hostIsNew && in6if_do_dad(ifp) &&
1257 ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1258 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1259 {
1260 int mindelay, maxdelay;
1261
1262 delay = 0;
1263 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1264 /*
1265 * We need to impose a delay before sending an NS
1266 * for DAD. Check if we also needed a delay for the
1267 * corresponding MLD message. If we did, the delay
1268 * should be larger than the MLD delay (this could be
1269 * relaxed a bit, but this simple logic is at least
1270 * safe).
1271 */
1272 mindelay = 0;
1273 if (in6m_sol != NULL &&
1274 in6m_sol->in6m_state == MLD_REPORTPENDING) {
1275 mindelay = in6m_sol->in6m_timer;
1276 }
1277 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1278 if (maxdelay - mindelay == 0)
1279 delay = 0;
1280 else {
1281 delay =
1282 (arc4random() % (maxdelay - mindelay)) +
1283 mindelay;
1284 }
1285 }
1286 nd6_dad_start((struct ifaddr *)ia, delay);
1287 }
1288
1289 return (error);
1290
1291 unlink:
1292 /*
1293 * XXX: if a change of an existing address failed, keep the entry
1294 * anyway.
1295 */
1296 if (hostIsNew)
1297 in6_unlink_ifa(ia, ifp);
1298 return (error);
1299
1300 cleanup:
1301 in6_purgeaddr(&ia->ia_ifa);
1302 return error;
1303 }
1304
1305 void
1306 in6_purgeaddr(ifa)
1307 struct ifaddr *ifa;
1308 {
1309 struct ifnet *ifp = ifa->ifa_ifp;
1310 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1311 struct in6_multi_mship *imm;
1312
1313 /* stop DAD processing */
1314 nd6_dad_stop(ifa);
1315
1316 /*
1317 * delete route to the destination of the address being purged.
1318 * The interface must be p2p or loopback in this case.
1319 */
1320 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1321 int e;
1322
1323 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1324 != 0) {
1325 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1326 "a route to the p2p destination: %s on %s, "
1327 "errno=%d\n",
1328 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1329 e);
1330 /* proceed anyway... */
1331 } else
1332 ia->ia_flags &= ~IFA_ROUTE;
1333 }
1334
1335 /* Remove ownaddr's loopback rtentry, if it exists. */
1336 in6_ifremloop(&(ia->ia_ifa));
1337
1338 /*
1339 * leave from multicast groups we have joined for the interface
1340 */
1341 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1342 LIST_REMOVE(imm, i6mm_chain);
1343 in6_leavegroup(imm);
1344 }
1345
1346 in6_unlink_ifa(ia, ifp);
1347 }
1348
1349 static void
1350 in6_unlink_ifa(ia, ifp)
1351 struct in6_ifaddr *ia;
1352 struct ifnet *ifp;
1353 {
1354 struct in6_ifaddr *oia;
1355 int s = splnet();
1356
1357 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1358
1359 oia = ia;
1360 if (oia == (ia = in6_ifaddr))
1361 in6_ifaddr = ia->ia_next;
1362 else {
1363 while (ia->ia_next && (ia->ia_next != oia))
1364 ia = ia->ia_next;
1365 if (ia->ia_next)
1366 ia->ia_next = oia->ia_next;
1367 else {
1368 /* search failed */
1369 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1370 }
1371 }
1372
1373 /*
1374 * Release the reference to the base prefix. There should be a
1375 * positive reference.
1376 */
1377 if (oia->ia6_ndpr == NULL) {
1378 nd6log((LOG_NOTICE,
1379 "in6_unlink_ifa: autoconf'ed address "
1380 "%p has no prefix\n", oia));
1381 } else {
1382 oia->ia6_ndpr->ndpr_refcnt--;
1383 oia->ia6_ndpr = NULL;
1384 }
1385
1386 /*
1387 * Also, if the address being removed is autoconf'ed, call
1388 * pfxlist_onlink_check() since the release might affect the status of
1389 * other (detached) addresses.
1390 */
1391 if ((oia->ia6_flags & IN6_IFF_AUTOCONF)) {
1392 pfxlist_onlink_check();
1393 }
1394
1395 /*
1396 * release another refcnt for the link from in6_ifaddr.
1397 * Note that we should decrement the refcnt at least once for all *BSD.
1398 */
1399 IFAFREE(&oia->ia_ifa);
1400
1401 splx(s);
1402 }
1403
1404 void
1405 in6_purgeif(ifp)
1406 struct ifnet *ifp;
1407 {
1408 struct ifaddr *ifa, *nifa;
1409
1410 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
1411 nifa = TAILQ_NEXT(ifa, ifa_list);
1412 if (ifa->ifa_addr->sa_family != AF_INET6)
1413 continue;
1414 in6_purgeaddr(ifa);
1415 }
1416
1417 in6_ifdetach(ifp);
1418 }
1419
1420 /*
1421 * SIOC[GAD]LIFADDR.
1422 * SIOCGLIFADDR: get first address. (?)
1423 * SIOCGLIFADDR with IFLR_PREFIX:
1424 * get first address that matches the specified prefix.
1425 * SIOCALIFADDR: add the specified address.
1426 * SIOCALIFADDR with IFLR_PREFIX:
1427 * add the specified prefix, filling hostid part from
1428 * the first link-local address. prefixlen must be <= 64.
1429 * SIOCDLIFADDR: delete the specified address.
1430 * SIOCDLIFADDR with IFLR_PREFIX:
1431 * delete the first address that matches the specified prefix.
1432 * return values:
1433 * EINVAL on invalid parameters
1434 * EADDRNOTAVAIL on prefix match failed/specified address not found
1435 * other values may be returned from in6_ioctl()
1436 *
1437 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1438 * this is to accomodate address naming scheme other than RFC2374,
1439 * in the future.
1440 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1441 * address encoding scheme. (see figure on page 8)
1442 */
1443 static int
1444 in6_lifaddr_ioctl(so, cmd, data, ifp, td)
1445 struct socket *so;
1446 u_long cmd;
1447 caddr_t data;
1448 struct ifnet *ifp;
1449 struct thread *td;
1450 {
1451 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1452 struct ifaddr *ifa;
1453 struct sockaddr *sa;
1454
1455 /* sanity checks */
1456 if (!data || !ifp) {
1457 panic("invalid argument to in6_lifaddr_ioctl");
1458 /* NOTREACHED */
1459 }
1460
1461 switch (cmd) {
1462 case SIOCGLIFADDR:
1463 /* address must be specified on GET with IFLR_PREFIX */
1464 if ((iflr->flags & IFLR_PREFIX) == 0)
1465 break;
1466 /* FALLTHROUGH */
1467 case SIOCALIFADDR:
1468 case SIOCDLIFADDR:
1469 /* address must be specified on ADD and DELETE */
1470 sa = (struct sockaddr *)&iflr->addr;
1471 if (sa->sa_family != AF_INET6)
1472 return EINVAL;
1473 if (sa->sa_len != sizeof(struct sockaddr_in6))
1474 return EINVAL;
1475 /* XXX need improvement */
1476 sa = (struct sockaddr *)&iflr->dstaddr;
1477 if (sa->sa_family && sa->sa_family != AF_INET6)
1478 return EINVAL;
1479 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1480 return EINVAL;
1481 break;
1482 default: /* shouldn't happen */
1483 #if 0
1484 panic("invalid cmd to in6_lifaddr_ioctl");
1485 /* NOTREACHED */
1486 #else
1487 return EOPNOTSUPP;
1488 #endif
1489 }
1490 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1491 return EINVAL;
1492
1493 switch (cmd) {
1494 case SIOCALIFADDR:
1495 {
1496 struct in6_aliasreq ifra;
1497 struct in6_addr *hostid = NULL;
1498 int prefixlen;
1499
1500 if ((iflr->flags & IFLR_PREFIX) != 0) {
1501 struct sockaddr_in6 *sin6;
1502
1503 /*
1504 * hostid is to fill in the hostid part of the
1505 * address. hostid points to the first link-local
1506 * address attached to the interface.
1507 */
1508 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1509 if (!ifa)
1510 return EADDRNOTAVAIL;
1511 hostid = IFA_IN6(ifa);
1512
1513 /* prefixlen must be <= 64. */
1514 if (64 < iflr->prefixlen)
1515 return EINVAL;
1516 prefixlen = iflr->prefixlen;
1517
1518 /* hostid part must be zero. */
1519 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1520 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1521 sin6->sin6_addr.s6_addr32[3] != 0) {
1522 return EINVAL;
1523 }
1524 } else
1525 prefixlen = iflr->prefixlen;
1526
1527 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1528 bzero(&ifra, sizeof(ifra));
1529 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1530
1531 bcopy(&iflr->addr, &ifra.ifra_addr,
1532 ((struct sockaddr *)&iflr->addr)->sa_len);
1533 if (hostid) {
1534 /* fill in hostid part */
1535 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1536 hostid->s6_addr32[2];
1537 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1538 hostid->s6_addr32[3];
1539 }
1540
1541 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1542 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1543 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1544 if (hostid) {
1545 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1546 hostid->s6_addr32[2];
1547 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1548 hostid->s6_addr32[3];
1549 }
1550 }
1551
1552 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1553 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1554
1555 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1556 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1557 }
1558 case SIOCGLIFADDR:
1559 case SIOCDLIFADDR:
1560 {
1561 struct in6_ifaddr *ia;
1562 struct in6_addr mask, candidate, match;
1563 struct sockaddr_in6 *sin6;
1564 int cmp;
1565
1566 bzero(&mask, sizeof(mask));
1567 if (iflr->flags & IFLR_PREFIX) {
1568 /* lookup a prefix rather than address. */
1569 in6_prefixlen2mask(&mask, iflr->prefixlen);
1570
1571 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1572 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1573 match.s6_addr32[0] &= mask.s6_addr32[0];
1574 match.s6_addr32[1] &= mask.s6_addr32[1];
1575 match.s6_addr32[2] &= mask.s6_addr32[2];
1576 match.s6_addr32[3] &= mask.s6_addr32[3];
1577
1578 /* if you set extra bits, that's wrong */
1579 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1580 return EINVAL;
1581
1582 cmp = 1;
1583 } else {
1584 if (cmd == SIOCGLIFADDR) {
1585 /* on getting an address, take the 1st match */
1586 cmp = 0; /* XXX */
1587 } else {
1588 /* on deleting an address, do exact match */
1589 in6_prefixlen2mask(&mask, 128);
1590 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1591 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1592
1593 cmp = 1;
1594 }
1595 }
1596
1597 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1598 if (ifa->ifa_addr->sa_family != AF_INET6)
1599 continue;
1600 if (!cmp)
1601 break;
1602
1603 /*
1604 * XXX: this is adhoc, but is necessary to allow
1605 * a user to specify fe80::/64 (not /10) for a
1606 * link-local address.
1607 */
1608 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1609 in6_clearscope(&candidate);
1610 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1611 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1612 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1613 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1614 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1615 break;
1616 }
1617 if (!ifa)
1618 return EADDRNOTAVAIL;
1619 ia = ifa2ia6(ifa);
1620
1621 if (cmd == SIOCGLIFADDR) {
1622 int error;
1623
1624 /* fill in the if_laddrreq structure */
1625 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1626 error = sa6_recoverscope(
1627 (struct sockaddr_in6 *)&iflr->addr);
1628 if (error != 0)
1629 return (error);
1630
1631 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1632 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1633 ia->ia_dstaddr.sin6_len);
1634 error = sa6_recoverscope(
1635 (struct sockaddr_in6 *)&iflr->dstaddr);
1636 if (error != 0)
1637 return (error);
1638 } else
1639 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1640
1641 iflr->prefixlen =
1642 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1643
1644 iflr->flags = ia->ia6_flags; /* XXX */
1645
1646 return 0;
1647 } else {
1648 struct in6_aliasreq ifra;
1649
1650 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1651 bzero(&ifra, sizeof(ifra));
1652 bcopy(iflr->iflr_name, ifra.ifra_name,
1653 sizeof(ifra.ifra_name));
1654
1655 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1656 ia->ia_addr.sin6_len);
1657 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1658 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1659 ia->ia_dstaddr.sin6_len);
1660 } else {
1661 bzero(&ifra.ifra_dstaddr,
1662 sizeof(ifra.ifra_dstaddr));
1663 }
1664 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1665 ia->ia_prefixmask.sin6_len);
1666
1667 ifra.ifra_flags = ia->ia6_flags;
1668 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1669 ifp, td);
1670 }
1671 }
1672 }
1673
1674 return EOPNOTSUPP; /* just for safety */
1675 }
1676
1677 /*
1678 * Initialize an interface's intetnet6 address
1679 * and routing table entry.
1680 */
1681 static int
1682 in6_ifinit(ifp, ia, sin6, newhost)
1683 struct ifnet *ifp;
1684 struct in6_ifaddr *ia;
1685 struct sockaddr_in6 *sin6;
1686 int newhost;
1687 {
1688 int error = 0, plen, ifacount = 0;
1689 int s = splimp();
1690 struct ifaddr *ifa;
1691
1692 /*
1693 * Give the interface a chance to initialize
1694 * if this is its first address,
1695 * and to validate the address if necessary.
1696 */
1697 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1698 if (ifa->ifa_addr == NULL)
1699 continue; /* just for safety */
1700 if (ifa->ifa_addr->sa_family != AF_INET6)
1701 continue;
1702 ifacount++;
1703 }
1704
1705 ia->ia_addr = *sin6;
1706
1707 if (ifacount <= 1 && ifp->if_ioctl) {
1708 IFF_LOCKGIANT(ifp);
1709 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1710 IFF_UNLOCKGIANT(ifp);
1711 if (error) {
1712 splx(s);
1713 return (error);
1714 }
1715 }
1716 splx(s);
1717
1718 ia->ia_ifa.ifa_metric = ifp->if_metric;
1719
1720 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1721
1722 if (newhost) {
1723 /*
1724 * set the rtrequest function to create llinfo. It also
1725 * adjust outgoing interface of the route for the local
1726 * address when called via in6_ifaddloop() below.
1727 */
1728 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1729 }
1730
1731 /*
1732 * Special case:
1733 * If a new destination address is specified for a point-to-point
1734 * interface, install a route to the destination as an interface
1735 * direct route. In addition, if the link is expected to have neighbor
1736 * cache entries, specify RTF_LLINFO so that a cache entry for the
1737 * destination address will be created.
1738 * created
1739 * XXX: the logic below rejects assigning multiple addresses on a p2p
1740 * interface that share the same destination.
1741 */
1742 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1743 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1744 ia->ia_dstaddr.sin6_family == AF_INET6) {
1745 int rtflags = RTF_UP | RTF_HOST;
1746 struct rtentry *rt = NULL, **rtp = NULL;
1747
1748 if (nd6_need_cache(ifp) != 0) {
1749 rtflags |= RTF_LLINFO;
1750 rtp = &rt;
1751 }
1752
1753 error = rtrequest(RTM_ADD, (struct sockaddr *)&ia->ia_dstaddr,
1754 (struct sockaddr *)&ia->ia_addr,
1755 (struct sockaddr *)&ia->ia_prefixmask,
1756 ia->ia_flags | rtflags, rtp);
1757 if (error != 0)
1758 return (error);
1759 if (rt != NULL) {
1760 struct llinfo_nd6 *ln;
1761
1762 RT_LOCK(rt);
1763 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1764 if (ln != NULL) {
1765 /*
1766 * Set the state to STALE because we don't
1767 * have to perform address resolution on this
1768 * link.
1769 */
1770 ln->ln_state = ND6_LLINFO_STALE;
1771 }
1772 RT_REMREF(rt);
1773 RT_UNLOCK(rt);
1774 }
1775 ia->ia_flags |= IFA_ROUTE;
1776 }
1777 if (plen < 128) {
1778 /*
1779 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1780 */
1781 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1782 }
1783
1784 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1785 if (newhost)
1786 in6_ifaddloop(&(ia->ia_ifa));
1787
1788 return (error);
1789 }
1790
1791 struct in6_multi_mship *
1792 in6_joingroup(ifp, addr, errorp, delay)
1793 struct ifnet *ifp;
1794 struct in6_addr *addr;
1795 int *errorp;
1796 int delay;
1797 {
1798 struct in6_multi_mship *imm;
1799
1800 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
1801 if (!imm) {
1802 *errorp = ENOBUFS;
1803 return NULL;
1804 }
1805 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, delay);
1806 if (!imm->i6mm_maddr) {
1807 /* *errorp is alrady set */
1808 free(imm, M_IP6MADDR);
1809 return NULL;
1810 }
1811 return imm;
1812 }
1813
1814 int
1815 in6_leavegroup(imm)
1816 struct in6_multi_mship *imm;
1817 {
1818
1819 if (imm->i6mm_maddr)
1820 in6_delmulti(imm->i6mm_maddr);
1821 free(imm, M_IP6MADDR);
1822 return 0;
1823 }
1824
1825 /*
1826 * Find an IPv6 interface link-local address specific to an interface.
1827 */
1828 struct in6_ifaddr *
1829 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1830 struct ifnet *ifp;
1831 int ignoreflags;
1832 {
1833 struct ifaddr *ifa;
1834
1835 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1836 if (ifa->ifa_addr == NULL)
1837 continue; /* just for safety */
1838 if (ifa->ifa_addr->sa_family != AF_INET6)
1839 continue;
1840 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1841 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1842 ignoreflags) != 0)
1843 continue;
1844 break;
1845 }
1846 }
1847
1848 return ((struct in6_ifaddr *)ifa);
1849 }
1850
1851
1852 /*
1853 * find the internet address corresponding to a given interface and address.
1854 */
1855 struct in6_ifaddr *
1856 in6ifa_ifpwithaddr(ifp, addr)
1857 struct ifnet *ifp;
1858 struct in6_addr *addr;
1859 {
1860 struct ifaddr *ifa;
1861
1862 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1863 if (ifa->ifa_addr == NULL)
1864 continue; /* just for safety */
1865 if (ifa->ifa_addr->sa_family != AF_INET6)
1866 continue;
1867 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1868 break;
1869 }
1870
1871 return ((struct in6_ifaddr *)ifa);
1872 }
1873
1874 /*
1875 * Convert IP6 address to printable (loggable) representation.
1876 */
1877 static char digits[] = "0123456789abcdef";
1878 static int ip6round = 0;
1879 char *
1880 ip6_sprintf(addr)
1881 const struct in6_addr *addr;
1882 {
1883 static char ip6buf[8][48];
1884 int i;
1885 char *cp;
1886 const u_int16_t *a = (const u_int16_t *)addr;
1887 const u_int8_t *d;
1888 int dcolon = 0;
1889
1890 ip6round = (ip6round + 1) & 7;
1891 cp = ip6buf[ip6round];
1892
1893 for (i = 0; i < 8; i++) {
1894 if (dcolon == 1) {
1895 if (*a == 0) {
1896 if (i == 7)
1897 *cp++ = ':';
1898 a++;
1899 continue;
1900 } else
1901 dcolon = 2;
1902 }
1903 if (*a == 0) {
1904 if (dcolon == 0 && *(a + 1) == 0) {
1905 if (i == 0)
1906 *cp++ = ':';
1907 *cp++ = ':';
1908 dcolon = 1;
1909 } else {
1910 *cp++ = '';
1911 *cp++ = ':';
1912 }
1913 a++;
1914 continue;
1915 }
1916 d = (const u_char *)a;
1917 *cp++ = digits[*d >> 4];
1918 *cp++ = digits[*d++ & 0xf];
1919 *cp++ = digits[*d >> 4];
1920 *cp++ = digits[*d & 0xf];
1921 *cp++ = ':';
1922 a++;
1923 }
1924 *--cp = 0;
1925 return (ip6buf[ip6round]);
1926 }
1927
1928 int
1929 in6_localaddr(in6)
1930 struct in6_addr *in6;
1931 {
1932 struct in6_ifaddr *ia;
1933
1934 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1935 return 1;
1936
1937 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1938 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1939 &ia->ia_prefixmask.sin6_addr)) {
1940 return 1;
1941 }
1942 }
1943
1944 return (0);
1945 }
1946
1947 int
1948 in6_is_addr_deprecated(sa6)
1949 struct sockaddr_in6 *sa6;
1950 {
1951 struct in6_ifaddr *ia;
1952
1953 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1954 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1955 &sa6->sin6_addr) &&
1956 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1957 return (1); /* true */
1958
1959 /* XXX: do we still have to go thru the rest of the list? */
1960 }
1961
1962 return (0); /* false */
1963 }
1964
1965 /*
1966 * return length of part which dst and src are equal
1967 * hard coding...
1968 */
1969 int
1970 in6_matchlen(src, dst)
1971 struct in6_addr *src, *dst;
1972 {
1973 int match = 0;
1974 u_char *s = (u_char *)src, *d = (u_char *)dst;
1975 u_char *lim = s + 16, r;
1976
1977 while (s < lim)
1978 if ((r = (*d++ ^ *s++)) != 0) {
1979 while (r < 128) {
1980 match++;
1981 r <<= 1;
1982 }
1983 break;
1984 } else
1985 match += 8;
1986 return match;
1987 }
1988
1989 /* XXX: to be scope conscious */
1990 int
1991 in6_are_prefix_equal(p1, p2, len)
1992 struct in6_addr *p1, *p2;
1993 int len;
1994 {
1995 int bytelen, bitlen;
1996
1997 /* sanity check */
1998 if (0 > len || len > 128) {
1999 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2000 len);
2001 return (0);
2002 }
2003
2004 bytelen = len / 8;
2005 bitlen = len % 8;
2006
2007 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2008 return (0);
2009 if (bitlen != 0 &&
2010 p1->s6_addr[bytelen] >> (8 - bitlen) !=
2011 p2->s6_addr[bytelen] >> (8 - bitlen))
2012 return (0);
2013
2014 return (1);
2015 }
2016
2017 void
2018 in6_prefixlen2mask(maskp, len)
2019 struct in6_addr *maskp;
2020 int len;
2021 {
2022 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2023 int bytelen, bitlen, i;
2024
2025 /* sanity check */
2026 if (0 > len || len > 128) {
2027 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2028 len);
2029 return;
2030 }
2031
2032 bzero(maskp, sizeof(*maskp));
2033 bytelen = len / 8;
2034 bitlen = len % 8;
2035 for (i = 0; i < bytelen; i++)
2036 maskp->s6_addr[i] = 0xff;
2037 if (bitlen)
2038 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2039 }
2040
2041 /*
2042 * return the best address out of the same scope. if no address was
2043 * found, return the first valid address from designated IF.
2044 */
2045 struct in6_ifaddr *
2046 in6_ifawithifp(ifp, dst)
2047 struct ifnet *ifp;
2048 struct in6_addr *dst;
2049 {
2050 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2051 struct ifaddr *ifa;
2052 struct in6_ifaddr *besta = 0;
2053 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2054
2055 dep[0] = dep[1] = NULL;
2056
2057 /*
2058 * We first look for addresses in the same scope.
2059 * If there is one, return it.
2060 * If two or more, return one which matches the dst longest.
2061 * If none, return one of global addresses assigned other ifs.
2062 */
2063 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2064 if (ifa->ifa_addr->sa_family != AF_INET6)
2065 continue;
2066 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2067 continue; /* XXX: is there any case to allow anycast? */
2068 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2069 continue; /* don't use this interface */
2070 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2071 continue;
2072 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2073 if (ip6_use_deprecated)
2074 dep[0] = (struct in6_ifaddr *)ifa;
2075 continue;
2076 }
2077
2078 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2079 /*
2080 * call in6_matchlen() as few as possible
2081 */
2082 if (besta) {
2083 if (blen == -1)
2084 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2085 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2086 if (tlen > blen) {
2087 blen = tlen;
2088 besta = (struct in6_ifaddr *)ifa;
2089 }
2090 } else
2091 besta = (struct in6_ifaddr *)ifa;
2092 }
2093 }
2094 if (besta)
2095 return (besta);
2096
2097 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2098 if (ifa->ifa_addr->sa_family != AF_INET6)
2099 continue;
2100 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2101 continue; /* XXX: is there any case to allow anycast? */
2102 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2103 continue; /* don't use this interface */
2104 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2105 continue;
2106 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2107 if (ip6_use_deprecated)
2108 dep[1] = (struct in6_ifaddr *)ifa;
2109 continue;
2110 }
2111
2112 return (struct in6_ifaddr *)ifa;
2113 }
2114
2115 /* use the last-resort values, that are, deprecated addresses */
2116 if (dep[0])
2117 return dep[0];
2118 if (dep[1])
2119 return dep[1];
2120
2121 return NULL;
2122 }
2123
2124 /*
2125 * perform DAD when interface becomes IFF_UP.
2126 */
2127 void
2128 in6_if_up(ifp)
2129 struct ifnet *ifp;
2130 {
2131 struct ifaddr *ifa;
2132 struct in6_ifaddr *ia;
2133
2134 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2135 if (ifa->ifa_addr->sa_family != AF_INET6)
2136 continue;
2137 ia = (struct in6_ifaddr *)ifa;
2138 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2139 /*
2140 * The TENTATIVE flag was likely set by hand
2141 * beforehand, implicitly indicating the need for DAD.
2142 * We may be able to skip the random delay in this
2143 * case, but we impose delays just in case.
2144 */
2145 nd6_dad_start(ifa,
2146 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2147 }
2148 }
2149
2150 /*
2151 * special cases, like 6to4, are handled in in6_ifattach
2152 */
2153 in6_ifattach(ifp, NULL);
2154 }
2155
2156 int
2157 in6if_do_dad(ifp)
2158 struct ifnet *ifp;
2159 {
2160 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2161 return (0);
2162
2163 switch (ifp->if_type) {
2164 #ifdef IFT_DUMMY
2165 case IFT_DUMMY:
2166 #endif
2167 case IFT_FAITH:
2168 /*
2169 * These interfaces do not have the IFF_LOOPBACK flag,
2170 * but loop packets back. We do not have to do DAD on such
2171 * interfaces. We should even omit it, because loop-backed
2172 * NS would confuse the DAD procedure.
2173 */
2174 return (0);
2175 default:
2176 /*
2177 * Our DAD routine requires the interface up and running.
2178 * However, some interfaces can be up before the RUNNING
2179 * status. Additionaly, users may try to assign addresses
2180 * before the interface becomes up (or running).
2181 * We simply skip DAD in such a case as a work around.
2182 * XXX: we should rather mark "tentative" on such addresses,
2183 * and do DAD after the interface becomes ready.
2184 */
2185 if (!((ifp->if_flags & IFF_UP) &&
2186 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2187 return (0);
2188
2189 return (1);
2190 }
2191 }
2192
2193 /*
2194 * Calculate max IPv6 MTU through all the interfaces and store it
2195 * to in6_maxmtu.
2196 */
2197 void
2198 in6_setmaxmtu()
2199 {
2200 unsigned long maxmtu = 0;
2201 struct ifnet *ifp;
2202
2203 IFNET_RLOCK();
2204 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
2205 /* this function can be called during ifnet initialization */
2206 if (!ifp->if_afdata[AF_INET6])
2207 continue;
2208 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2209 IN6_LINKMTU(ifp) > maxmtu)
2210 maxmtu = IN6_LINKMTU(ifp);
2211 }
2212 IFNET_RUNLOCK();
2213 if (maxmtu) /* update only when maxmtu is positive */
2214 in6_maxmtu = maxmtu;
2215 }
2216
2217 /*
2218 * Provide the length of interface identifiers to be used for the link attached
2219 * to the given interface. The length should be defined in "IPv6 over
2220 * xxx-link" document. Note that address architecture might also define
2221 * the length for a particular set of address prefixes, regardless of the
2222 * link type. As clarified in rfc2462bis, those two definitions should be
2223 * consistent, and those really are as of August 2004.
2224 */
2225 int
2226 in6_if2idlen(ifp)
2227 struct ifnet *ifp;
2228 {
2229 switch (ifp->if_type) {
2230 case IFT_ETHER: /* RFC2464 */
2231 #ifdef IFT_PROPVIRTUAL
2232 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2233 #endif
2234 #ifdef IFT_L2VLAN
2235 case IFT_L2VLAN: /* ditto */
2236 #endif
2237 #ifdef IFT_IEEE80211
2238 case IFT_IEEE80211: /* ditto */
2239 #endif
2240 #ifdef IFT_MIP
2241 case IFT_MIP: /* ditto */
2242 #endif
2243 return (64);
2244 case IFT_FDDI: /* RFC2467 */
2245 return (64);
2246 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2247 return (64);
2248 case IFT_PPP: /* RFC2472 */
2249 return (64);
2250 case IFT_ARCNET: /* RFC2497 */
2251 return (64);
2252 case IFT_FRELAY: /* RFC2590 */
2253 return (64);
2254 case IFT_IEEE1394: /* RFC3146 */
2255 return (64);
2256 case IFT_GIF:
2257 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2258 case IFT_LOOP:
2259 return (64); /* XXX: is this really correct? */
2260 default:
2261 /*
2262 * Unknown link type:
2263 * It might be controversial to use the today's common constant
2264 * of 64 for these cases unconditionally. For full compliance,
2265 * we should return an error in this case. On the other hand,
2266 * if we simply miss the standard for the link type or a new
2267 * standard is defined for a new link type, the IFID length
2268 * is very likely to be the common constant. As a compromise,
2269 * we always use the constant, but make an explicit notice
2270 * indicating the "unknown" case.
2271 */
2272 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2273 return (64);
2274 }
2275 }
2276
2277 void *
2278 in6_domifattach(ifp)
2279 struct ifnet *ifp;
2280 {
2281 struct in6_ifextra *ext;
2282
2283 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2284 bzero(ext, sizeof(*ext));
2285
2286 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2287 M_IFADDR, M_WAITOK);
2288 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2289
2290 ext->icmp6_ifstat =
2291 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2292 M_IFADDR, M_WAITOK);
2293 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2294
2295 ext->nd_ifinfo = nd6_ifattach(ifp);
2296 ext->scope6_id = scope6_ifattach(ifp);
2297 return ext;
2298 }
2299
2300 void
2301 in6_domifdetach(ifp, aux)
2302 struct ifnet *ifp;
2303 void *aux;
2304 {
2305 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2306
2307 scope6_ifdetach(ext->scope6_id);
2308 nd6_ifdetach(ext->nd_ifinfo);
2309 free(ext->in6_ifstat, M_IFADDR);
2310 free(ext->icmp6_ifstat, M_IFADDR);
2311 free(ext, M_IFADDR);
2312 }
2313
2314 /*
2315 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2316 * v4 mapped addr or v4 compat addr
2317 */
2318 void
2319 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2320 {
2321 bzero(sin, sizeof(*sin));
2322 sin->sin_len = sizeof(struct sockaddr_in);
2323 sin->sin_family = AF_INET;
2324 sin->sin_port = sin6->sin6_port;
2325 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2326 }
2327
2328 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2329 void
2330 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2331 {
2332 bzero(sin6, sizeof(*sin6));
2333 sin6->sin6_len = sizeof(struct sockaddr_in6);
2334 sin6->sin6_family = AF_INET6;
2335 sin6->sin6_port = sin->sin_port;
2336 sin6->sin6_addr.s6_addr32[0] = 0;
2337 sin6->sin6_addr.s6_addr32[1] = 0;
2338 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2339 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2340 }
2341
2342 /* Convert sockaddr_in6 into sockaddr_in. */
2343 void
2344 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2345 {
2346 struct sockaddr_in *sin_p;
2347 struct sockaddr_in6 sin6;
2348
2349 /*
2350 * Save original sockaddr_in6 addr and convert it
2351 * to sockaddr_in.
2352 */
2353 sin6 = *(struct sockaddr_in6 *)nam;
2354 sin_p = (struct sockaddr_in *)nam;
2355 in6_sin6_2_sin(sin_p, &sin6);
2356 }
2357
2358 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2359 void
2360 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2361 {
2362 struct sockaddr_in *sin_p;
2363 struct sockaddr_in6 *sin6_p;
2364
2365 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2366 M_WAITOK);
2367 sin_p = (struct sockaddr_in *)*nam;
2368 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2369 FREE(*nam, M_SONAME);
2370 *nam = (struct sockaddr *)sin6_p;
2371 }
Cache object: 99f45fedeb939d83958e33895fc30808
|