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