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