]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_prefix.c
1 /* $KAME: in6_prefix.c,v 1.27 2000/03/29 23:13:13 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)in.c 8.2 (Berkeley) 11/15/93
67 #include <sys/param.h>
68 #include <sys/malloc.h>
69 #include <sys/kernel.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/sockio.h>
73 #include <sys/systm.h>
74 #include <sys/syslog.h>
79 #include <netinet/in.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip6.h>
82 #include <netinet6/in6_prefix.h>
83 #include <netinet6/ip6_var.h>
86 #define M_IP6RR M_IP6MISC
87 #define M_RR_ADDR M_IP6MISC
89 static MALLOC_DEFINE(M_IP6RR
, "ip6rr", "IPv6 Router Renumbering Prefix");
90 static MALLOC_DEFINE(M_RR_ADDR
, "rp_addr", "IPv6 Router Renumbering Ifid");
93 struct rr_prhead rr_prefix
;
95 #include <net/net_osdep.h>
97 static void add_each_addr(struct socket
*so
, struct rr_prefix
*rpp
,
99 static int create_ra_entry(struct rp_addr
**rapp
);
100 static int add_each_prefix(struct socket
*so
, struct rr_prefix
*rpp
);
101 static void free_rp_entries(struct rr_prefix
*rpp
);
102 static int link_stray_ia6s(struct rr_prefix
*rpp
);
103 static void rp_remove(struct rr_prefix
*rpp
);
104 extern lck_mtx_t
*prefix6_mutex
;
107 * Copy bits from src to tgt, from off bit for len bits.
108 * Caller must specify collect tgtsize and srcsize.
111 bit_copy(char *tgt
, u_int tgtsize
, char *src
, u_int srcsize
,
112 u_int off
, u_int len
)
116 /* arg values check */
117 if (srcsize
< off
|| srcsize
< (off
+ len
) ||
118 tgtsize
< off
|| tgtsize
< (off
+ len
)) {
120 "in6_prefix.c: bit_copy: invalid args: srcsize %d,\n"
121 "tgtsize %d, off %d, len %d\n", srcsize
, tgtsize
, off
,
126 /* search start point */
127 for (sp
= src
, tp
= tgt
; off
>= 8; sp
++, tp
++)
129 /* copy starting bits */
134 startbits
= min((8 - off
), len
);
136 for (setbit
= (0x80 >> off
); startbits
;
137 setbit
>>= 1, startbits
--, len
--)
138 *tp
|= (setbit
& *sp
);
142 /* copy midium bits */
143 for (; len
>= 8; sp
++, tp
++) {
147 /* copy ending bits */
151 for (setbit
= 0x80; len
; setbit
>>= 1, len
--)
152 *tp
|= (setbit
& *sp
);
156 static struct ifprefix
*
157 in6_prefixwithifp(struct ifnet
*ifp
, int plen
, struct in6_addr
*dst
)
159 struct ifprefix
*ifpr
;
161 /* search matched prefix */
162 ifnet_lock_shared(ifp
);
163 for (ifpr
= TAILQ_FIRST(&ifp
->if_prefixhead
); ifpr
;
164 ifpr
= TAILQ_NEXT(ifpr
, ifpr_list
))
166 if (ifpr
->ifpr_prefix
->sa_family
!= AF_INET6
||
167 ifpr
->ifpr_type
!= IN6_PREFIX_RR
)
169 if (plen
<= in6_matchlen(dst
, IFPR_IN6(ifpr
)))
172 ifnet_lock_done(ifp
);
177 * Search prefix which matches arg prefix as specified in
178 * draft-ietf-ipngwg-router-renum-08.txt
180 static struct rr_prefix
*
181 search_matched_prefix(struct ifnet
*ifp
, struct in6_prefixreq
*ipr
)
183 struct ifprefix
*ifpr
;
185 struct rr_prefix
*rpp
;
187 /* search matched prefix */
188 ifpr
= in6_prefixwithifp(ifp
, ipr
->ipr_plen
,
189 &ipr
->ipr_prefix
.sin6_addr
);
191 return ifpr2rp(ifpr
);
194 * search matched addr, and then search prefix
195 * which matches the addr
198 ifnet_lock_shared(ifp
);
199 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
201 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
204 in6_matchlen(&ipr
->ipr_prefix
.sin6_addr
, IFA_IN6(ifa
)))
208 ifnet_lock_done(ifp
);
212 rpp
= ifpr2rp(((struct in6_ifaddr
*)ifa
)->ia6_ifpr
);
214 ifnet_lock_done(ifp
);
218 for (ifpr
= TAILQ_FIRST(&ifp
->if_prefixhead
); ifpr
;
219 ifpr
= TAILQ_NEXT(ifpr
, ifpr_list
))
221 if (ifpr
->ifpr_prefix
->sa_family
!= AF_INET6
||
222 ifpr
->ifpr_type
!= IN6_PREFIX_RR
)
224 if (ifpr
->ifpr_plen
<= in6_matchlen(IFA_IN6(ifa
),
228 ifnet_lock_done(ifp
);
230 log(LOG_ERR
, "in6_prefix.c: search_matched_prefix: addr %s"
231 "has no pointer to prefix %s\n", ip6_sprintf(IFA_IN6(ifa
)),
232 ip6_sprintf(IFPR_IN6(ifpr
)));
233 return ifpr2rp(ifpr
);
237 * Search prefix which matches arg prefix as specified in
238 * draft-ietf-ipngwg-router-renum-08.txt, and mark it if exists.
239 * Return 1 if anything matched, and 0 if nothing matched.
242 mark_matched_prefixes(u_long cmd
, struct ifnet
*ifp
, struct in6_rrenumreq
*irr
)
244 struct ifprefix
*ifpr
;
246 int matchlen
, matched
= 0;
248 /* search matched prefixes */
249 ifnet_lock_exclusive(ifp
); /* Should if_prefixhead be protected by IPv6?? */
250 for (ifpr
= TAILQ_FIRST(&ifp
->if_prefixhead
); ifpr
;
251 ifpr
= TAILQ_NEXT(ifpr
, ifpr_list
))
253 if (ifpr
->ifpr_prefix
->sa_family
!= AF_INET6
||
254 ifpr
->ifpr_type
!= IN6_PREFIX_RR
)
256 matchlen
= in6_matchlen(&irr
->irr_matchprefix
.sin6_addr
,
258 if (irr
->irr_m_minlen
> ifpr
->ifpr_plen
||
259 irr
->irr_m_maxlen
< ifpr
->ifpr_plen
||
260 irr
->irr_m_len
> matchlen
)
263 ifpr2rp(ifpr
)->rp_statef_addmark
= 1;
264 if (cmd
== SIOCCIFPREFIX_IN6
)
265 ifpr2rp(ifpr
)->rp_statef_delmark
= 1;
269 * search matched addr, and then search prefixes
270 * which matche the addr
272 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
274 struct rr_prefix
*rpp
;
276 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
278 matchlen
= in6_matchlen(&irr
->irr_matchprefix
.sin6_addr
,
280 if (irr
->irr_m_minlen
> matchlen
||
281 irr
->irr_m_maxlen
< matchlen
|| irr
->irr_m_len
> matchlen
)
283 rpp
= ifpr2rp(((struct in6_ifaddr
*)ifa
)->ia6_ifpr
);
286 rpp
->rp_statef_addmark
= 1;
287 if (cmd
== SIOCCIFPREFIX_IN6
)
288 rpp
->rp_statef_delmark
= 1;
290 log(LOG_WARNING
, "in6_prefix.c: mark_matched_prefixes:"
291 "no back pointer to ifprefix for %s. "
292 "ND autoconfigured addr?\n",
293 ip6_sprintf(IFA_IN6(ifa
)));
295 ifnet_lock_done(ifp
);
300 * Mark global prefixes as to be deleted.
303 delmark_global_prefixes(struct ifnet
*ifp
, struct in6_rrenumreq
*irr
)
305 struct ifprefix
*ifpr
;
307 /* search matched prefixes */
308 ifnet_lock_exclusive(ifp
);
309 for (ifpr
= TAILQ_FIRST(&ifp
->if_prefixhead
); ifpr
;
310 ifpr
= TAILQ_NEXT(ifpr
, ifpr_list
))
312 if (ifpr
->ifpr_prefix
->sa_family
!= AF_INET6
||
313 ifpr
->ifpr_type
!= IN6_PREFIX_RR
)
315 /* mark delete global prefix */
316 if (in6_addrscope(RP_IN6(ifpr2rp(ifpr
))) ==
317 IPV6_ADDR_SCOPE_GLOBAL
)
318 ifpr2rp(ifpr
)->rp_statef_delmark
= 1;
320 ifnet_lock_done(ifp
);
323 /* Unmark prefixes */
325 unmark_prefixes(struct ifnet
*ifp
)
327 struct ifprefix
*ifpr
;
329 /* unmark all prefix */
330 ifnet_lock_exclusive(ifp
);
331 for (ifpr
= TAILQ_FIRST(&ifp
->if_prefixhead
); ifpr
;
332 ifpr
= TAILQ_NEXT(ifpr
, ifpr_list
))
334 if (ifpr
->ifpr_prefix
->sa_family
!= AF_INET6
||
335 ifpr
->ifpr_type
!= IN6_PREFIX_RR
)
338 ifpr2rp(ifpr
)->rp_statef_addmark
= 0;
339 ifpr2rp(ifpr
)->rp_statef_delmark
= 0;
341 ifnet_lock_done(ifp
);
345 init_prefix_ltimes(struct rr_prefix
*rpp
)
347 struct timeval timenow
;
349 getmicrotime(&timenow
);
351 if (rpp
->rp_pltime
== RR_INFINITE_LIFETIME
||
352 rpp
->rp_rrf_decrprefd
== 0)
353 rpp
->rp_preferred
= 0;
355 rpp
->rp_preferred
= timenow
.tv_sec
+ rpp
->rp_pltime
;
356 if (rpp
->rp_vltime
== RR_INFINITE_LIFETIME
||
357 rpp
->rp_rrf_decrvalid
== 0)
360 rpp
->rp_expire
= timenow
.tv_sec
+ rpp
->rp_vltime
;
364 rr_are_ifid_equal(struct in6_addr
*ii1
, struct in6_addr
*ii2
, int ii_len
)
366 int ii_bytelen
, ii_bitlen
;
367 int p_bytelen
, p_bitlen
;
371 ii_len
> 124) { /* as RFC2373, prefix is at least 4 bit */
372 log(LOG_ERR
, "rr_are_ifid_equal: invalid ifid length(%d)\n",
377 ii_bytelen
= ii_len
/ 8;
378 ii_bitlen
= ii_len
% 8;
380 p_bytelen
= sizeof(struct in6_addr
) - ii_bytelen
- 1;
381 p_bitlen
= 8 - ii_bitlen
;
383 if (bcmp(ii1
->s6_addr
+ p_bytelen
+ 1, ii2
->s6_addr
+ p_bytelen
+ 1,
386 if (((ii1
->s6_addr
[p_bytelen
] << p_bitlen
) & 0xff) !=
387 ((ii2
->s6_addr
[p_bytelen
] << p_bitlen
) & 0xff))
393 static struct rp_addr
*
394 search_ifidwithprefix(struct rr_prefix
*rpp
, struct in6_addr
*ifid
)
398 lck_mtx_lock(prefix6_mutex
);
399 LIST_FOREACH(rap
, &rpp
->rp_addrhead
, ra_entry
)
401 if (rr_are_ifid_equal(ifid
, &rap
->ra_ifid
,
402 (sizeof(struct in6_addr
) << 3) -
406 lck_mtx_unlock(prefix6_mutex
);
411 assign_ra_entry(struct rr_prefix
*rpp
, int iilen
, struct in6_ifaddr
*ia
)
417 if ((error
= create_ra_entry(&rap
)) != 0)
420 /* copy interface id part */
421 bit_copy((caddr_t
)&rap
->ra_ifid
, sizeof(rap
->ra_ifid
) << 3,
422 (caddr_t
)IA6_IN6(ia
),
423 sizeof(*IA6_IN6(ia
)) << 3, rpp
->rp_plen
, iilen
);
424 /* link to ia, and put into list */
426 ifaref(&rap
->ra_addr
->ia_ifa
);
427 #if 0 /* Can't do this now, because rpp may be on th stack. should fix it? */
428 ia
->ia6_ifpr
= rp2ifpr(rpp
);
430 lck_mtx_lock(prefix6_mutex
);
431 LIST_INSERT_HEAD(&rpp
->rp_addrhead
, rap
, ra_entry
);
432 lck_mtx_unlock(prefix6_mutex
);
438 * add a link-local address to an interface. we will add new interface address
439 * (prefix database + new interface id).
442 in6_prefix_add_llifid(int iilen
, struct in6_ifaddr
*ia
)
444 struct rr_prefix
*rpp
;
449 if ((error
= create_ra_entry(&rap
)) != 0)
451 /* copy interface id part */
452 bit_copy((caddr_t
)&rap
->ra_ifid
, sizeof(rap
->ra_ifid
) << 3,
453 (caddr_t
)IA6_IN6(ia
), sizeof(*IA6_IN6(ia
)) << 3,
454 64, (sizeof(rap
->ra_ifid
) << 3) - 64);
455 /* XXX: init dummy so */
456 bzero(&so
, sizeof(so
));
457 /* insert into list */
458 lck_mtx_lock(prefix6_mutex
);
459 LIST_FOREACH(rpp
, &rr_prefix
, rp_entry
)
462 * do not attempt to add an address, if ifp does not match
464 if (rpp
->rp_ifp
!= ia
->ia_ifp
)
467 LIST_INSERT_HEAD(&rpp
->rp_addrhead
, rap
, ra_entry
);
468 add_each_addr(&so
, rpp
, rap
);
470 lck_mtx_unlock(prefix6_mutex
);
475 * add an address to an interface. if the interface id portion is new,
476 * we will add new interface address (prefix database + new interface id).
479 in6_prefix_add_ifid(int iilen
, struct in6_ifaddr
*ia
)
481 int plen
= (sizeof(*IA6_IN6(ia
)) << 3) - iilen
;
482 struct ifprefix
*ifpr
;
486 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia
)))
487 return(in6_prefix_add_llifid(iilen
, ia
));
488 ifpr
= in6_prefixwithifp(ia
->ia_ifp
, plen
, IA6_IN6(ia
));
492 int pplen
= (plen
== 128) ? 64 : plen
; /* XXX hardcoded 64 is bad */
494 /* allocate a prefix for ia, with default properties */
497 bzero(&rp
, sizeof(rp
));
498 rp
.rp_type
= IN6_PREFIX_RR
;
499 rp
.rp_ifp
= ia
->ia_ifp
;
501 rp
.rp_prefix
.sin6_len
= sizeof(rp
.rp_prefix
);
502 rp
.rp_prefix
.sin6_family
= AF_INET6
;
503 bit_copy((char *)RP_IN6(&rp
), sizeof(*RP_IN6(&rp
)) << 3,
504 (char *)&ia
->ia_addr
.sin6_addr
,
505 sizeof(ia
->ia_addr
.sin6_addr
) << 3,
507 rp
.rp_vltime
= rp
.rp_pltime
= RR_INFINITE_LIFETIME
;
508 rp
.rp_raf_onlink
= 1;
510 /* Is some FlagMasks for rrf necessary? */
511 rp
.rp_rrf_decrvalid
= rp
.rp_rrf_decrprefd
= 0;
512 rp
.rp_origin
= PR_ORIG_RR
; /* can be renumbered */
514 /* create ra_entry */
515 error
= link_stray_ia6s(&rp
);
517 free_rp_entries(&rp
);
521 /* XXX: init dummy so */
522 bzero(&so
, sizeof(so
));
524 error
= add_each_prefix(&so
, &rp
);
526 /* free each rp_addr entry */
527 free_rp_entries(&rp
);
533 ifpr
= in6_prefixwithifp(ia
->ia_ifp
, pplen
, IA6_IN6(ia
));
537 rap
= search_ifidwithprefix(ifpr2rp(ifpr
), IA6_IN6(ia
));
539 if (rap
->ra_addr
== NULL
) {
541 ifaref(&rap
->ra_addr
->ia_ifa
);
542 } else if (rap
->ra_addr
!= ia
) {
543 /* There may be some inconsistencies between addrs. */
544 log(LOG_ERR
, "ip6_prefix.c: addr %s/%d matched prefix"
545 " already has another ia %p(%s) on its ifid list\n",
546 ip6_sprintf(IA6_IN6(ia
)), plen
,
548 ip6_sprintf(IA6_IN6(rap
->ra_addr
)));
549 return EADDRINUSE
/* XXX */;
554 error
= assign_ra_entry(ifpr2rp(ifpr
), iilen
, ia
);
561 in6_prefix_remove_ifid(int iilen
, struct in6_ifaddr
*ia
)
565 if (ia
->ia6_ifpr
== NULL
)
567 rap
= search_ifidwithprefix(ifpr2rp(ia
->ia6_ifpr
), IA6_IN6(ia
));
569 lck_mtx_lock(prefix6_mutex
);
570 LIST_REMOVE(rap
, ra_entry
);
571 lck_mtx_unlock(prefix6_mutex
);
573 ifafree(&rap
->ra_addr
->ia_ifa
);
574 FREE(rap
, M_RR_ADDR
);
577 if (LIST_EMPTY(&ifpr2rp(ia
->ia6_ifpr
)->rp_addrhead
))
578 rp_remove(ifpr2rp(ia
->ia6_ifpr
));
585 struct ifprefix
*ifpr
, *nextifpr
;
587 /* delete prefixes before ifnet goes away */
588 ifnet_lock_exclusive(ifp
);
589 for (ifpr
= TAILQ_FIRST(&ifp
->if_prefixhead
); ifpr
;
592 nextifpr
= TAILQ_NEXT(ifpr
, ifpr_list
);
593 if (ifpr
->ifpr_prefix
->sa_family
!= AF_INET6
||
594 ifpr
->ifpr_type
!= IN6_PREFIX_RR
)
596 (void)delete_each_prefix(ifpr2rp(ifpr
), PR_ORIG_KERNEL
);
598 ifnet_lock_done(ifp
);
602 add_each_addr(struct socket
*so
, struct rr_prefix
*rpp
, struct rp_addr
*rap
)
604 struct in6_ifaddr
*ia6
;
605 struct in6_aliasreq ifra
;
609 bzero(&ifra
, sizeof(ifra
));
610 strncpy(ifra
.ifra_name
, if_name(rpp
->rp_ifp
), sizeof(ifra
.ifra_name
));
611 ifra
.ifra_addr
.sin6_family
= ifra
.ifra_prefixmask
.sin6_family
=
613 ifra
.ifra_addr
.sin6_len
= ifra
.ifra_prefixmask
.sin6_len
=
614 sizeof(ifra
.ifra_addr
);
615 /* copy prefix part */
616 bit_copy((char *)&ifra
.ifra_addr
.sin6_addr
,
617 sizeof(ifra
.ifra_addr
.sin6_addr
) << 3,
618 (char *)RP_IN6(rpp
), sizeof(*RP_IN6(rpp
)) << 3,
620 /* copy interface id part */
621 bit_copy((char *)&ifra
.ifra_addr
.sin6_addr
,
622 sizeof(ifra
.ifra_addr
.sin6_addr
) << 3,
623 (char *)&rap
->ra_ifid
, sizeof(rap
->ra_ifid
) << 3,
624 rpp
->rp_plen
, (sizeof(rap
->ra_ifid
) << 3) - rpp
->rp_plen
);
625 in6_prefixlen2mask(&ifra
.ifra_prefixmask
.sin6_addr
, rpp
->rp_plen
);
626 /* don't care ifra_flags for now */
629 * XXX: if we did this with finite lifetime values, the lifetimes would
630 * decrese in time and never incremented.
631 * we should need more clarifications on the prefix mechanism...
633 ifra
.ifra_lifetime
.ia6t_vltime
= rpp
->rp_vltime
;
634 ifra
.ifra_lifetime
.ia6t_pltime
= rpp
->rp_pltime
;
636 ia6
= in6ifa_ifpwithaddr(rpp
->rp_ifp
, &ifra
.ifra_addr
.sin6_addr
);
638 if (ia6
->ia6_ifpr
== NULL
) {
639 /* link this addr and the prefix each other */
641 ifafree(&rap
->ra_addr
->ia_ifa
);
643 ifaref(&rap
->ra_addr
->ia_ifa
);
644 ia6
->ia6_ifpr
= rp2ifpr(rpp
);
647 if (ia6
->ia6_ifpr
== rp2ifpr(rpp
)) {
649 ifafree(&rap
->ra_addr
->ia_ifa
);
651 ifaref(&rap
->ra_addr
->ia_ifa
);
655 * The addr is already assigned to other
657 * There may be some inconsistencies between
659 * e.g. overraped prefixes with common starting
660 * part and different plefixlen.
661 * Or, completely duplicated prefixes?
665 "in6_prefix.c: add_each_addr: addition of an addr %s/%d "
666 "failed because there is already another addr %s/%d\n",
667 ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
), rpp
->rp_plen
,
668 ip6_sprintf(IA6_IN6(ia6
)),
669 in6_mask2len(&ia6
->ia_prefixmask
.sin6_addr
, NULL
));
672 /* propagate ANYCAST flag if it is set for ancestor addr */
673 if (rap
->ra_flags
.anycast
!= 0)
674 ifra
.ifra_flags
|= IN6_IFF_ANYCAST
;
675 error
= in6_control(so
, SIOCAIFADDR_IN6
, (caddr_t
)&ifra
, rpp
->rp_ifp
,
678 log(LOG_ERR
, "in6_prefix.c: add_each_addr: addition of an addr"
679 "%s/%d failed because in6_control failed for error %d\n",
680 ip6_sprintf(&ifra
.ifra_addr
.sin6_addr
), rpp
->rp_plen
,
686 * link beween this addr and the prefix will be done
687 * in in6_prefix_add_ifid
692 rrpr_update(struct socket
*so
, struct rr_prefix
*new)
694 struct rr_prefix
*rpp
;
695 struct ifprefix
*ifpr
;
699 /* search existing prefix */
700 ifnet_lock_exclusive(new->rp_ifp
);
701 for (ifpr
= TAILQ_FIRST(&new->rp_ifp
->if_prefixhead
); ifpr
;
702 ifpr
= TAILQ_NEXT(ifpr
, ifpr_list
))
704 if (ifpr
->ifpr_prefix
->sa_family
!= AF_INET6
||
705 ifpr
->ifpr_type
!= IN6_PREFIX_RR
)
707 if (ifpr
->ifpr_plen
== new->rp_plen
&&
708 in6_are_prefix_equal(IFPR_IN6(ifpr
), RP_IN6(new),
715 * We got a prefix which we have seen in the past.
718 * If the origin of the already-installed prefix is more
719 * preferable than the new one, ignore installation request.
721 if (rpp
->rp_origin
> new->rp_origin
) {
722 ifnet_lock_done(new->rp_ifp
);
726 /* update prefix information */
727 rpp
->rp_flags
.prf_ra
= new->rp_flags
.prf_ra
;
728 if (rpp
->rp_origin
>= PR_ORIG_RR
)
729 rpp
->rp_flags
.prf_rr
= new->rp_flags
.prf_rr
;
730 rpp
->rp_vltime
= new->rp_vltime
;
731 rpp
->rp_pltime
= new->rp_pltime
;
732 rpp
->rp_expire
= new->rp_expire
;
733 rpp
->rp_preferred
= new->rp_preferred
;
734 rpp
->rp_statef_delmark
= 0; /* cancel deletion */
736 * Interface id related update.
737 * add rp_addr entries in new into rpp, if they have not
738 * been already included in rpp.
740 lck_mtx_lock(prefix6_mutex
);
741 while (!LIST_EMPTY(&new->rp_addrhead
))
743 rap
= LIST_FIRST(&new->rp_addrhead
);
744 LIST_REMOVE(rap
, ra_entry
);
745 if (search_ifidwithprefix(rpp
, &rap
->ra_ifid
)
748 ifafree(&rap
->ra_addr
->ia_ifa
);
749 FREE(rap
, M_RR_ADDR
);
752 LIST_INSERT_HEAD(&rpp
->rp_addrhead
, rap
, ra_entry
);
754 lck_mtx_unlock(prefix6_mutex
);
757 * We got a fresh prefix.
759 /* create new prefix */
760 rpp
= (struct rr_prefix
*)_MALLOC(sizeof(*rpp
), M_IP6RR
,
763 log(LOG_ERR
, "in6_prefix.c: rrpr_update:%d"
764 ": ENOBUFS for rr_prefix\n", __LINE__
);
765 ifnet_lock_done(new->rp_ifp
);
769 lck_mtx_lock(prefix6_mutex
);
771 LIST_INIT(&rpp
->rp_addrhead
);
772 /* move rp_addr entries of new to rpp */
773 while (!LIST_EMPTY(&new->rp_addrhead
))
775 rap
= LIST_FIRST(&new->rp_addrhead
);
776 LIST_REMOVE(rap
, ra_entry
);
777 LIST_INSERT_HEAD(&rpp
->rp_addrhead
, rap
, ra_entry
);
779 lck_mtx_unlock(prefix6_mutex
);
781 /* let rp_ifpr.ifpr_prefix point rr_prefix. */
782 rpp
->rp_ifpr
.ifpr_prefix
= (struct sockaddr
*)&rpp
->rp_prefix
;
783 /* link rr_prefix entry to if_prefixlist */
785 struct ifnet
*ifp
= rpp
->rp_ifp
;
786 struct ifprefix
*ifpr
;
788 if ((ifpr
= TAILQ_FIRST(&ifp
->if_prefixhead
))
790 for ( ; TAILQ_NEXT(ifpr
, ifpr_list
);
791 ifpr
= TAILQ_NEXT(ifpr
, ifpr_list
))
793 TAILQ_NEXT(ifpr
, ifpr_list
) = rp2ifpr(rpp
);
795 TAILQ_FIRST(&ifp
->if_prefixhead
) =
797 rp2ifpr(rpp
)->ifpr_type
= IN6_PREFIX_RR
;
799 /* link rr_prefix entry to rr_prefix list */
800 lck_mtx_lock(prefix6_mutex
);
801 LIST_INSERT_HEAD(&rr_prefix
, rpp
, rp_entry
);
802 lck_mtx_unlock(prefix6_mutex
);
804 ifnet_lock_done(new->rp_ifp
);
806 if (!new->rp_raf_auto
)
810 * Add an address for each interface id, if it is not yet
811 * If it existed but not pointing to the prefix yet,
812 * init the prefix pointer.
814 lck_mtx_lock(prefix6_mutex
);
815 LIST_FOREACH(rap
, &rpp
->rp_addrhead
, ra_entry
)
817 if (rap
->ra_addr
!= NULL
) {
818 if (rap
->ra_addr
->ia6_ifpr
== NULL
)
819 rap
->ra_addr
->ia6_ifpr
= rp2ifpr(rpp
);
822 add_each_addr(so
, rpp
, rap
);
824 lck_mtx_unlock(prefix6_mutex
);
829 add_each_prefix(struct socket
*so
, struct rr_prefix
*rpp
)
831 init_prefix_ltimes(rpp
);
832 return(rrpr_update(so
, rpp
));
836 rp_remove(struct rr_prefix
*rpp
)
839 /* unlink rp_entry from if_prefixlist */
840 lck_mtx_lock(prefix6_mutex
);
842 struct ifnet
*ifp
= rpp
->rp_ifp
;
843 struct ifprefix
*ifpr
;
845 ifnet_lock_exclusive(ifp
);
846 if ((ifpr
= TAILQ_FIRST(&ifp
->if_prefixhead
)) == rp2ifpr(rpp
))
847 TAILQ_FIRST(&ifp
->if_prefixhead
) =
848 TAILQ_NEXT(ifpr
, ifpr_list
);
850 while (TAILQ_NEXT(ifpr
, ifpr_list
) != NULL
&&
851 (TAILQ_NEXT(ifpr
, ifpr_list
) != rp2ifpr(rpp
)))
852 ifpr
= TAILQ_NEXT(ifpr
, ifpr_list
);
853 if (TAILQ_NEXT(ifpr
, ifpr_list
))
854 TAILQ_NEXT(ifpr
, ifpr_list
) =
855 TAILQ_NEXT(rp2ifpr(rpp
), ifpr_list
);
857 printf("Couldn't unlink rr_prefix from ifp\n");
859 ifnet_lock_done(ifp
);
861 /* unlink rp_entry from rr_prefix list */
862 LIST_REMOVE(rpp
, rp_entry
);
863 lck_mtx_unlock(prefix6_mutex
);
868 create_ra_entry(struct rp_addr
**rapp
)
870 *rapp
= (struct rp_addr
*)_MALLOC(sizeof(struct rp_addr
), M_RR_ADDR
,
873 log(LOG_ERR
, "in6_prefix.c: init_newprefix:%d: ENOBUFS"
874 "for rp_addr\n", __LINE__
);
877 bzero(*rapp
, sizeof(*(*rapp
)));
883 init_newprefix(struct in6_rrenumreq
*irr
, struct ifprefix
*ifpr
,
884 struct rr_prefix
*rpp
)
886 struct rp_addr
*orap
;
889 bzero(rpp
, sizeof(*rpp
));
890 rpp
->rp_type
= IN6_PREFIX_RR
;
891 rpp
->rp_ifp
= ifpr
->ifpr_ifp
;
892 rpp
->rp_plen
= ifpr
->ifpr_plen
;
893 rpp
->rp_prefix
.sin6_len
= sizeof(rpp
->rp_prefix
);
894 rpp
->rp_prefix
.sin6_family
= AF_INET6
;
895 bit_copy((char *)RP_IN6(rpp
), sizeof(*RP_IN6(rpp
)) << 3,
896 (char *)&irr
->irr_useprefix
.sin6_addr
,
897 sizeof(irr
->irr_useprefix
.sin6_addr
) << 3,
898 0, irr
->irr_u_uselen
);
899 /* copy keeplen part if necessary as necessary len */
900 if (irr
->irr_u_uselen
< ifpr
->ifpr_plen
)
901 bit_copy((char *)RP_IN6(rpp
), sizeof(*RP_IN6(rpp
)) << 3,
902 (char *)IFPR_IN6(ifpr
), sizeof(*IFPR_IN6(ifpr
)) << 3,
904 min(ifpr
->ifpr_plen
- irr
->irr_u_uselen
,
905 irr
->irr_u_keeplen
));
906 lck_mtx_lock(prefix6_mutex
);
907 LIST_FOREACH(orap
, &(ifpr2rp(ifpr
)->rp_addrhead
), ra_entry
)
912 if ((error
= create_ra_entry(&rap
)) != 0)
914 rap
->ra_ifid
= orap
->ra_ifid
;
915 rap
->ra_flags
.anycast
= (orap
->ra_addr
!= NULL
&&
916 (orap
->ra_addr
->ia6_flags
&
917 IN6_IFF_ANYCAST
) != 0) ? 1 : 0;
918 LIST_INSERT_HEAD(&rpp
->rp_addrhead
, rap
, ra_entry
);
920 rpp
->rp_vltime
= irr
->irr_vltime
;
921 rpp
->rp_pltime
= irr
->irr_pltime
;
922 rpp
->rp_raf_onlink
= irr
->irr_raf_mask_onlink
? irr
->irr_raf_onlink
:
923 ifpr2rp(ifpr
)->rp_raf_onlink
;
924 rpp
->rp_raf_auto
= irr
->irr_raf_mask_auto
? irr
->irr_raf_auto
:
925 ifpr2rp(ifpr
)->rp_raf_auto
;
926 /* Is some FlagMasks for rrf necessary? */
927 rpp
->rp_rrf
= irr
->irr_rrf
;
928 rpp
->rp_origin
= irr
->irr_origin
;
929 lck_mtx_unlock(prefix6_mutex
);
935 free_rp_entries(struct rr_prefix
*rpp
)
938 * This func is only called with rpp on stack(not on list).
939 * So no splnet() here
941 lck_mtx_lock(prefix6_mutex
);
942 while (!LIST_EMPTY(&rpp
->rp_addrhead
))
946 rap
= LIST_FIRST(&rpp
->rp_addrhead
);
947 LIST_REMOVE(rap
, ra_entry
);
949 ifafree(&rap
->ra_addr
->ia_ifa
);
950 FREE(rap
, M_RR_ADDR
);
952 lck_mtx_unlock(prefix6_mutex
);
956 add_useprefixes(struct socket
*so
, struct ifnet
*ifp
,
957 struct in6_rrenumreq
*irr
)
959 struct ifprefix
*ifpr
, *nextifpr
;
963 /* add prefixes to each of marked prefix */
964 ifnet_lock_exclusive(ifp
);
965 for (ifpr
= TAILQ_FIRST(&ifp
->if_prefixhead
); ifpr
; ifpr
= nextifpr
)
967 nextifpr
= TAILQ_NEXT(ifpr
, ifpr_list
);
968 if (ifpr
->ifpr_prefix
->sa_family
!= AF_INET6
||
969 ifpr
->ifpr_type
!= IN6_PREFIX_RR
)
971 if (ifpr2rp(ifpr
)->rp_statef_addmark
) {
972 if ((error
= init_newprefix(irr
, ifpr
, &rp
)) != 0)
974 error
= add_each_prefix(so
, &rp
);
977 ifnet_lock_done(ifp
);
978 /* free each rp_addr entry */
979 free_rp_entries(&rp
);
985 unprefer_prefix(struct rr_prefix
*rpp
)
988 struct timeval timenow
;
990 getmicrotime(&timenow
);
992 lck_mtx_lock(prefix6_mutex
);
993 for (rap
= rpp
->rp_addrhead
.lh_first
; rap
!= NULL
;
994 rap
= rap
->ra_entry
.le_next
) {
995 if (rap
->ra_addr
== NULL
)
997 rap
->ra_addr
->ia6_lifetime
.ia6t_preferred
= timenow
.tv_sec
;
998 rap
->ra_addr
->ia6_lifetime
.ia6t_pltime
= 0;
1000 lck_mtx_unlock(prefix6_mutex
);
1005 delete_each_prefix(struct rr_prefix
*rpp
, u_char origin
)
1009 if (rpp
->rp_origin
> origin
)
1012 lck_mtx_lock(prefix6_mutex
);
1013 while (rpp
->rp_addrhead
.lh_first
!= NULL
) {
1014 struct rp_addr
*rap
;
1017 rap
= LIST_FIRST(&rpp
->rp_addrhead
);
1021 LIST_REMOVE(rap
, ra_entry
);
1022 if (rap
->ra_addr
== NULL
) {
1023 FREE(rap
, M_RR_ADDR
);
1026 rap
->ra_addr
->ia6_ifpr
= NULL
;
1028 in6_purgeaddr(&rap
->ra_addr
->ia_ifa
, 0);
1029 ifafree(&rap
->ra_addr
->ia_ifa
);
1030 FREE(rap
, M_RR_ADDR
);
1033 lck_mtx_unlock(prefix6_mutex
);
1039 delete_prefixes(struct ifnet
*ifp
, u_char origin
)
1041 struct ifprefix
*ifpr
, *nextifpr
;
1043 /* delete prefixes marked as tobe deleted */
1044 ifnet_lock_exclusive(ifp
);
1045 for (ifpr
= TAILQ_FIRST(&ifp
->if_prefixhead
); ifpr
; ifpr
= nextifpr
)
1047 nextifpr
= TAILQ_NEXT(ifpr
, ifpr_list
);
1048 if (ifpr
->ifpr_prefix
->sa_family
!= AF_INET6
||
1049 ifpr
->ifpr_type
!= IN6_PREFIX_RR
)
1051 if (ifpr2rp(ifpr
)->rp_statef_delmark
)
1052 (void)delete_each_prefix(ifpr2rp(ifpr
), origin
);
1054 ifnet_lock_done(ifp
);
1058 link_stray_ia6s(struct rr_prefix
*rpp
)
1062 for (ifa
= rpp
->rp_ifp
->if_addrlist
.tqh_first
; ifa
;
1063 ifa
= ifa
->ifa_list
.tqe_next
)
1065 struct rp_addr
*rap
;
1066 struct rr_prefix
*orpp
;
1069 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1071 if (rpp
->rp_plen
> in6_matchlen(RP_IN6(rpp
), IFA_IN6(ifa
)))
1074 orpp
= ifpr2rp(((struct in6_ifaddr
*)ifa
)->ia6_ifpr
);
1076 if (!in6_are_prefix_equal(RP_IN6(orpp
), RP_IN6(rpp
),
1078 log(LOG_ERR
, "in6_prefix.c: link_stray_ia6s:"
1079 "addr %s/%d already linked to a prefix"
1080 "and it matches also %s/%d\n",
1081 ip6_sprintf(IFA_IN6(ifa
)), orpp
->rp_plen
,
1082 ip6_sprintf(RP_IN6(rpp
)),
1086 if ((error
= assign_ra_entry(rpp
,
1087 (sizeof(rap
->ra_ifid
) << 3) -
1089 (struct in6_ifaddr
*)ifa
)) != 0)
1095 /* XXX assumes that permission is already checked by the caller */
1097 in6_prefix_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
1100 struct rr_prefix
*rpp
, rp_tmp
;
1101 struct rp_addr
*rap
;
1102 struct in6_prefixreq
*ipr
= (struct in6_prefixreq
*)data
;
1103 struct in6_rrenumreq
*irr
= (struct in6_rrenumreq
*)data
;
1108 * Failsafe for erroneous address config program.
1109 * Let's hope rrenumd don't make a mistakes.
1111 if (ipr
->ipr_origin
<= PR_ORIG_RA
)
1112 ipr
->ipr_origin
= PR_ORIG_STATIC
;
1115 case SIOCSGIFPREFIX_IN6
:
1116 delmark_global_prefixes(ifp
, irr
);
1118 case SIOCAIFPREFIX_IN6
:
1119 case SIOCCIFPREFIX_IN6
:
1120 /* check if preferred lifetime > valid lifetime */
1121 if (irr
->irr_pltime
> irr
->irr_vltime
) {
1123 "in6_prefix_ioctl: preferred lifetime"
1124 "(%ld) is greater than valid lifetime(%ld)\n",
1125 (u_long
)irr
->irr_pltime
, (u_long
)irr
->irr_vltime
);
1129 if (mark_matched_prefixes(cmd
, ifp
, irr
)) {
1130 if (irr
->irr_u_uselen
!= 0)
1131 if ((error
= add_useprefixes(so
, ifp
, irr
))
1134 if (cmd
!= SIOCAIFPREFIX_IN6
)
1135 delete_prefixes(ifp
, irr
->irr_origin
);
1137 return (EADDRNOTAVAIL
);
1139 unmark_prefixes(ifp
);
1141 case SIOCGIFPREFIX_IN6
:
1142 rpp
= search_matched_prefix(ifp
, ipr
);
1143 if (rpp
== NULL
|| ifp
!= rpp
->rp_ifp
)
1144 return (EADDRNOTAVAIL
);
1146 ipr
->ipr_origin
= rpp
->rp_origin
;
1147 ipr
->ipr_plen
= rpp
->rp_plen
;
1148 ipr
->ipr_vltime
= rpp
->rp_vltime
;
1149 ipr
->ipr_pltime
= rpp
->rp_pltime
;
1150 ipr
->ipr_flags
= rpp
->rp_flags
;
1151 ipr
->ipr_prefix
= rpp
->rp_prefix
;
1154 case SIOCSIFPREFIX_IN6
:
1155 /* check if preferred lifetime > valid lifetime */
1156 if (ipr
->ipr_pltime
> ipr
->ipr_vltime
) {
1158 "in6_prefix_ioctl: preferred lifetime"
1159 "(%ld) is greater than valid lifetime(%ld)\n",
1160 (u_long
)ipr
->ipr_pltime
, (u_long
)ipr
->ipr_vltime
);
1166 bzero((caddr_t
)&rp_tmp
, sizeof(rp_tmp
));
1167 rp_tmp
.rp_ifp
= ifp
;
1168 rp_tmp
.rp_plen
= ipr
->ipr_plen
;
1169 rp_tmp
.rp_prefix
= ipr
->ipr_prefix
;
1170 rp_tmp
.rp_vltime
= ipr
->ipr_vltime
;
1171 rp_tmp
.rp_pltime
= ipr
->ipr_pltime
;
1172 rp_tmp
.rp_flags
= ipr
->ipr_flags
;
1173 rp_tmp
.rp_origin
= ipr
->ipr_origin
;
1175 /* create rp_addr entries, usually at least for lladdr */
1176 if ((error
= link_stray_ia6s(&rp_tmp
)) != 0) {
1177 free_rp_entries(&rp_tmp
);
1180 ifnet_lock_exclusive(ifp
);
1181 for (ifa
= ifp
->if_addrlist
.tqh_first
;
1183 ifa
= ifa
->ifa_list
.tqe_next
)
1185 if (ifa
->ifa_addr
== NULL
)
1186 continue; /* just for safety */
1187 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1189 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
)) == 0)
1192 if ((error
= create_ra_entry(&rap
)) != 0) {
1193 free_rp_entries(&rp_tmp
);
1196 /* copy interface id part */
1197 bit_copy((caddr_t
)&rap
->ra_ifid
,
1198 sizeof(rap
->ra_ifid
) << 3,
1199 (caddr_t
)IFA_IN6(ifa
),
1200 sizeof(*IFA_IN6(ifa
)) << 3,
1202 (sizeof(rap
->ra_ifid
) << 3) - rp_tmp
.rp_plen
);
1203 /* insert into list */
1204 lck_mtx_lock(prefix6_mutex
);
1205 LIST_INSERT_HEAD(&rp_tmp
.rp_addrhead
, rap
, ra_entry
);
1206 lck_mtx_unlock(prefix6_mutex
);
1208 ifnet_lock_done(ifp
);
1210 error
= add_each_prefix(so
, &rp_tmp
);
1212 /* free each rp_addr entry */
1213 free_rp_entries(&rp_tmp
);
1216 case SIOCDIFPREFIX_IN6
:
1217 rpp
= search_matched_prefix(ifp
, ipr
);
1218 if (rpp
== NULL
|| ifp
!= rpp
->rp_ifp
)
1219 return (EADDRNOTAVAIL
);
1221 ifnet_lock_exclusive(ifp
);
1222 error
= delete_each_prefix(rpp
, ipr
->ipr_origin
);
1223 ifnet_lock_done(ifp
);
1231 in6_rr_timer(void *ignored_arg
)
1233 struct rr_prefix
*rpp
;
1234 struct timeval timenow
;
1236 getmicrotime(&timenow
);
1239 lck_mtx_lock(prefix6_mutex
);
1240 rpp
= LIST_FIRST(&rr_prefix
);
1242 if (rpp
->rp_expire
&& rpp
->rp_expire
< timenow
.tv_sec
) {
1243 struct rr_prefix
*next_rpp
;
1245 next_rpp
= LIST_NEXT(rpp
, rp_entry
);
1246 delete_each_prefix(rpp
, PR_ORIG_KERNEL
);
1250 if (rpp
->rp_preferred
&& rpp
->rp_preferred
< timenow
.tv_sec
)
1251 unprefer_prefix(rpp
);
1252 rpp
= LIST_NEXT(rpp
, rp_entry
);
1254 lck_mtx_unlock(prefix6_mutex
);
1255 timeout(in6_rr_timer
, (caddr_t
)0, ip6_rr_prune
* hz
);