1 /* $FreeBSD: src/sys/netinet6/in6_prefix.c,v 1.4.2.3 2001/07/03 11:01:52 ume Exp $ */
2 /* $DragonFly: src/sys/netinet6/in6_prefix.c,v 1.13 2008/03/07 11:34:21 sephe Exp $ */
3 /* $KAME: in6_prefix.c,v 1.47 2001/03/25 08:41:39 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)in.c 8.2 (Berkeley) 11/15/93
69 #include <sys/param.h>
70 #include <sys/malloc.h>
71 #include <sys/kernel.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/sockio.h>
75 #include <sys/systm.h>
76 #include <sys/syslog.h>
78 #include <sys/thread2.h>
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/in6_prefix.h>
86 #include <netinet6/ip6_var.h>
88 static MALLOC_DEFINE(M_IP6RR, "ip6rr", "IPv6 Router Renumbering Prefix");
89 static MALLOC_DEFINE(M_RR_ADDR, "rp_addr", "IPv6 Router Renumbering Ifid");
91 struct rr_prhead rr_prefix;
93 struct callout in6_rr_timer_ch;
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);
106 * Copy bits from src to tgt, from off bit for len bits.
107 * Caller must specify collect tgtsize and srcsize.
110 bit_copy(char *tgt, u_int tgtsize, char *src, u_int srcsize,
111 u_int off, u_int len)
115 /* arg values check */
116 if (srcsize < off || srcsize < (off + len) ||
117 tgtsize < off || tgtsize < (off + len)) {
119 "in6_prefix.c: bit_copy: invalid args: srcsize %d,\n"
120 "tgtsize %d, off %d, len %d\n", srcsize, tgtsize, off,
125 /* search start point */
126 for (sp = src, tp = tgt; off >= 8; sp++, tp++)
128 /* copy starting bits */
133 startbits = min((8 - off), len);
135 for (setbit = (0x80 >> off); startbits;
136 setbit >>= 1, startbits--, len--)
137 *tp |= (setbit & *sp);
141 /* copy midium bits */
142 for (; len >= 8; sp++, tp++) {
146 /* copy ending bits */
150 for (setbit = 0x80; len; setbit >>= 1, len--)
151 *tp |= (setbit & *sp);
155 static struct ifprefix *
156 in6_prefixwithifp(struct ifnet *ifp, int plen, struct in6_addr *dst)
158 struct ifprefix *ifpr;
160 /* search matched prefix */
161 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
162 ifpr = TAILQ_NEXT(ifpr, ifpr_list))
164 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
165 ifpr->ifpr_type != IN6_PREFIX_RR)
167 if (plen <= in6_matchlen(dst, IFPR_IN6(ifpr)))
174 * Search prefix which matches arg prefix as specified in
175 * draft-ietf-ipngwg-router-renum-08.txt
177 static struct rr_prefix *
178 search_matched_prefix(struct ifnet *ifp, struct in6_prefixreq *ipr)
180 struct ifprefix *ifpr;
181 struct ifaddr_container *ifac;
183 struct rr_prefix *rpp;
185 /* search matched prefix */
186 ifpr = in6_prefixwithifp(ifp, ipr->ipr_plen,
187 &ipr->ipr_prefix.sin6_addr);
189 return ifpr2rp(ifpr);
192 * search matched addr, and then search prefix
193 * which matches the addr
196 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
199 if (ifa->ifa_addr->sa_family != AF_INET6)
202 in6_matchlen(&ipr->ipr_prefix.sin6_addr, IFA_IN6(ifa)))
208 rpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
212 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
213 ifpr = TAILQ_NEXT(ifpr, ifpr_list))
215 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
216 ifpr->ifpr_type != IN6_PREFIX_RR)
218 if (ifpr->ifpr_plen <= in6_matchlen(IFA_IN6(ifa),
223 log(LOG_ERR, "in6_prefix.c: search_matched_prefix: addr %s"
224 "has no pointer to prefix %s\n", ip6_sprintf(IFA_IN6(ifa)),
225 ip6_sprintf(IFPR_IN6(ifpr)));
226 return ifpr2rp(ifpr);
230 * Search prefix which matches arg prefix as specified in
231 * draft-ietf-ipngwg-router-renum-08.txt, and mark it if exists.
232 * Return 1 if anything matched, and 0 if nothing matched.
235 mark_matched_prefixes(u_long cmd, struct ifnet *ifp, struct in6_rrenumreq *irr)
237 struct ifaddr_container *ifac;
238 struct ifprefix *ifpr;
239 int matchlen, matched = 0;
241 /* search matched prefixes */
242 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
243 ifpr = TAILQ_NEXT(ifpr, ifpr_list))
245 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
246 ifpr->ifpr_type != IN6_PREFIX_RR)
248 matchlen = in6_matchlen(&irr->irr_matchprefix.sin6_addr,
250 if (irr->irr_m_minlen > ifpr->ifpr_plen ||
251 irr->irr_m_maxlen < ifpr->ifpr_plen ||
252 irr->irr_m_len > matchlen)
255 ifpr2rp(ifpr)->rp_statef_addmark = 1;
256 if (cmd == SIOCCIFPREFIX_IN6)
257 ifpr2rp(ifpr)->rp_statef_delmark = 1;
261 * search matched addr, and then search prefixes
262 * which matche the addr
264 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
265 struct ifaddr *ifa = ifac->ifa;
266 struct rr_prefix *rpp;
268 if (ifa->ifa_addr->sa_family != AF_INET6)
270 matchlen = in6_matchlen(&irr->irr_matchprefix.sin6_addr,
272 if (irr->irr_m_minlen > matchlen ||
273 irr->irr_m_maxlen < matchlen || irr->irr_m_len > matchlen)
275 rpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
278 rpp->rp_statef_addmark = 1;
279 if (cmd == SIOCCIFPREFIX_IN6)
280 rpp->rp_statef_delmark = 1;
282 log(LOG_WARNING, "in6_prefix.c: mark_matched_prefixes:"
283 "no back pointer to ifprefix for %s. "
284 "ND autoconfigured addr?\n",
285 ip6_sprintf(IFA_IN6(ifa)));
291 * Mark global prefixes as to be deleted.
294 delmark_global_prefixes(struct ifnet *ifp, struct in6_rrenumreq *irr)
296 struct ifprefix *ifpr;
298 /* search matched prefixes */
299 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
300 ifpr = TAILQ_NEXT(ifpr, ifpr_list))
302 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
303 ifpr->ifpr_type != IN6_PREFIX_RR)
305 /* mark delete global prefix */
306 if (in6_addrscope(RP_IN6(ifpr2rp(ifpr))) ==
307 IPV6_ADDR_SCOPE_GLOBAL)
308 ifpr2rp(ifpr)->rp_statef_delmark = 1;
312 /* Unmark prefixes */
314 unmark_prefixes(struct ifnet *ifp)
316 struct ifprefix *ifpr;
318 /* unmark all prefix */
319 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
320 ifpr = TAILQ_NEXT(ifpr, ifpr_list))
322 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
323 ifpr->ifpr_type != IN6_PREFIX_RR)
326 ifpr2rp(ifpr)->rp_statef_addmark = 0;
327 ifpr2rp(ifpr)->rp_statef_delmark = 0;
332 init_prefix_ltimes(struct rr_prefix *rpp)
335 if (rpp->rp_pltime == RR_INFINITE_LIFETIME ||
336 rpp->rp_rrf_decrprefd == 0)
337 rpp->rp_preferred = 0;
339 rpp->rp_preferred = time_second + rpp->rp_pltime;
340 if (rpp->rp_vltime == RR_INFINITE_LIFETIME ||
341 rpp->rp_rrf_decrvalid == 0)
344 rpp->rp_expire = time_second + rpp->rp_vltime;
348 rr_are_ifid_equal(struct in6_addr *ii1, struct in6_addr *ii2, int ii_len)
350 int ii_bytelen, ii_bitlen;
351 int p_bytelen, p_bitlen;
355 ii_len > 124) { /* as RFC2373, prefix is at least 4 bit */
356 log(LOG_ERR, "rr_are_ifid_equal: invalid ifid length(%d)\n",
361 ii_bytelen = ii_len / 8;
362 ii_bitlen = ii_len % 8;
364 p_bytelen = sizeof(struct in6_addr) - ii_bytelen - 1;
365 p_bitlen = 8 - ii_bitlen;
367 if (bcmp(ii1->s6_addr + p_bytelen + 1, ii2->s6_addr + p_bytelen + 1,
370 if (((ii1->s6_addr[p_bytelen] << p_bitlen) & 0xff) !=
371 ((ii2->s6_addr[p_bytelen] << p_bitlen) & 0xff))
377 static struct rp_addr *
378 search_ifidwithprefix(struct rr_prefix *rpp, struct in6_addr *ifid)
382 LIST_FOREACH(rap, &rpp->rp_addrhead, ra_entry)
384 if (rr_are_ifid_equal(ifid, &rap->ra_ifid,
385 (sizeof(struct in6_addr) << 3) -
393 assign_ra_entry(struct rr_prefix *rpp, int iilen, struct in6_ifaddr *ia)
398 if ((error = create_ra_entry(&rap)) != 0)
401 /* copy interface id part */
402 bit_copy((caddr_t)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3,
403 (caddr_t)IA6_IN6(ia),
404 sizeof(*IA6_IN6(ia)) << 3, rpp->rp_plen, iilen);
405 /* link to ia, and put into list */
408 _IFAREF(&rap->ra_addr->ia_ifa, 0);
409 #if 0 /* Can't do this now, because rpp may be on th stack. should fix it? */
410 ia->ia6_ifpr = rp2ifpr(rpp);
412 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
419 * add a link-local address to an interface. we will add new interface address
420 * (prefix database + new interface id).
423 in6_prefix_add_llifid(int iilen, struct in6_ifaddr *ia)
425 struct rr_prefix *rpp;
430 if ((error = create_ra_entry(&rap)) != 0)
432 /* copy interface id part */
433 bit_copy((caddr_t)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3,
434 (caddr_t)IA6_IN6(ia), sizeof(*IA6_IN6(ia)) << 3,
435 64, (sizeof(rap->ra_ifid) << 3) - 64);
436 /* XXX: init dummy so */
437 bzero(&so, sizeof(so));
438 /* insert into list */
439 LIST_FOREACH(rpp, &rr_prefix, rp_entry)
442 * do not attempt to add an address, if ifp does not match
444 if (rpp->rp_ifp != ia->ia_ifp)
448 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
450 add_each_addr(&so, rpp, rap);
456 * add an address to an interface. if the interface id portion is new,
457 * we will add new interface address (prefix database + new interface id).
460 in6_prefix_add_ifid(int iilen, struct in6_ifaddr *ia)
462 int plen = (sizeof(*IA6_IN6(ia)) << 3) - iilen;
463 struct ifprefix *ifpr;
467 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
468 return (in6_prefix_add_llifid(iilen, ia));
469 ifpr = in6_prefixwithifp(ia->ia_ifp, plen, IA6_IN6(ia));
473 int pplen = (plen == 128) ? 64 : plen; /* XXX hardcoded 64 is bad */
475 /* allocate a prefix for ia, with default properties */
478 bzero(&rp, sizeof(rp));
479 rp.rp_type = IN6_PREFIX_RR;
480 rp.rp_ifp = ia->ia_ifp;
482 rp.rp_prefix.sin6_len = sizeof(rp.rp_prefix);
483 rp.rp_prefix.sin6_family = AF_INET6;
484 bit_copy((char *)RP_IN6(&rp), sizeof(*RP_IN6(&rp)) << 3,
485 (char *)&ia->ia_addr.sin6_addr,
486 sizeof(ia->ia_addr.sin6_addr) << 3,
488 rp.rp_vltime = rp.rp_pltime = RR_INFINITE_LIFETIME;
489 rp.rp_raf_onlink = 1;
491 /* Is some FlagMasks for rrf necessary? */
492 rp.rp_rrf_decrvalid = rp.rp_rrf_decrprefd = 0;
493 rp.rp_origin = PR_ORIG_RR; /* can be renumbered */
495 /* create ra_entry */
496 error = link_stray_ia6s(&rp);
498 free_rp_entries(&rp);
502 /* XXX: init dummy so */
503 bzero(&so, sizeof(so));
505 error = add_each_prefix(&so, &rp);
507 /* free each rp_addr entry */
508 free_rp_entries(&rp);
514 ifpr = in6_prefixwithifp(ia->ia_ifp, pplen, IA6_IN6(ia));
518 rap = search_ifidwithprefix(ifpr2rp(ifpr), IA6_IN6(ia));
520 if (rap->ra_addr == NULL) {
522 crit_enter(); /* XXX MP not MP safe */
523 _IFAREF(&rap->ra_addr->ia_ifa, 0);
525 } else if (rap->ra_addr != ia) {
526 /* There may be some inconsistencies between addrs. */
527 log(LOG_ERR, "ip6_prefix.c: addr %s/%d matched prefix"
528 " already has another ia %p(%s) on its ifid list\n",
529 ip6_sprintf(IA6_IN6(ia)), plen,
531 ip6_sprintf(IA6_IN6(rap->ra_addr)));
532 return EADDRINUSE /* XXX */;
537 error = assign_ra_entry(ifpr2rp(ifpr), iilen, ia);
544 in6_prefix_remove_ifid(int iilen, struct in6_ifaddr *ia)
548 if (ia->ia6_ifpr == NULL)
550 rap = search_ifidwithprefix(ifpr2rp(ia->ia6_ifpr), IA6_IN6(ia));
553 LIST_REMOVE(rap, ra_entry);
555 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
557 kfree(rap, M_RR_ADDR);
560 if (LIST_EMPTY(&ifpr2rp(ia->ia6_ifpr)->rp_addrhead))
561 rp_remove(ifpr2rp(ia->ia6_ifpr));
565 in6_purgeprefix(struct ifnet *ifp)
567 struct ifprefix *ifpr, *nextifpr;
569 /* delete prefixes before ifnet goes away */
570 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
573 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
574 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
575 ifpr->ifpr_type != IN6_PREFIX_RR)
577 delete_each_prefix(ifpr2rp(ifpr), PR_ORIG_KERNEL);
582 add_each_addr(struct socket *so, struct rr_prefix *rpp, struct rp_addr *rap)
584 struct in6_ifaddr *ia6;
585 struct in6_aliasreq ifra;
589 bzero(&ifra, sizeof(ifra));
590 strncpy(ifra.ifra_name, if_name(rpp->rp_ifp), sizeof(ifra.ifra_name));
591 ifra.ifra_addr.sin6_family = ifra.ifra_prefixmask.sin6_family =
593 ifra.ifra_addr.sin6_len = ifra.ifra_prefixmask.sin6_len =
594 sizeof(ifra.ifra_addr);
595 /* copy prefix part */
596 bit_copy((char *)&ifra.ifra_addr.sin6_addr,
597 sizeof(ifra.ifra_addr.sin6_addr) << 3,
598 (char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
600 /* copy interface id part */
601 bit_copy((char *)&ifra.ifra_addr.sin6_addr,
602 sizeof(ifra.ifra_addr.sin6_addr) << 3,
603 (char *)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3,
604 rpp->rp_plen, (sizeof(rap->ra_ifid) << 3) - rpp->rp_plen);
605 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, rpp->rp_plen);
606 /* don't care ifra_flags for now */
609 * XXX: if we did this with finite lifetime values, the lifetimes would
610 * decrese in time and never incremented.
611 * we should need more clarifications on the prefix mechanism...
613 ifra.ifra_lifetime.ia6t_vltime = rpp->rp_vltime;
614 ifra.ifra_lifetime.ia6t_pltime = rpp->rp_pltime;
616 ia6 = in6ifa_ifpwithaddr(rpp->rp_ifp, &ifra.ifra_addr.sin6_addr);
618 if (ia6->ia6_ifpr == NULL) {
619 crit_enter(); /* XXX MP not MP safe */
620 /* link this addr and the prefix each other */
622 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
624 _IFAREF(&rap->ra_addr->ia_ifa, 0);
626 ia6->ia6_ifpr = rp2ifpr(rpp);
629 if (ia6->ia6_ifpr == rp2ifpr(rpp)) {
630 crit_enter(); /* XXX MP not MP safe */
632 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
634 _IFAREF(&rap->ra_addr->ia_ifa, 0);
639 * The addr is already assigned to other
641 * There may be some inconsistencies between
643 * e.g. overraped prefixes with common starting
644 * part and different plefixlen.
645 * Or, completely duplicated prefixes?
649 "in6_prefix.c: add_each_addr: addition of an addr %s/%d "
650 "failed because there is already another addr %s/%d\n",
651 ip6_sprintf(&ifra.ifra_addr.sin6_addr), rpp->rp_plen,
652 ip6_sprintf(IA6_IN6(ia6)),
653 in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL));
656 /* propagate ANYCAST flag if it is set for ancestor addr */
657 if (rap->ra_flags.anycast != 0)
658 ifra.ifra_flags |= IN6_IFF_ANYCAST;
659 error = in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, rpp->rp_ifp,
662 log(LOG_ERR, "in6_prefix.c: add_each_addr: addition of an addr"
663 "%s/%d failed because in6_control failed for error %d\n",
664 ip6_sprintf(&ifra.ifra_addr.sin6_addr), rpp->rp_plen,
670 * link beween this addr and the prefix will be done
671 * in in6_prefix_add_ifid
676 rrpr_update(struct socket *so, struct rr_prefix *new)
678 struct rr_prefix *rpp;
679 struct ifprefix *ifpr;
682 /* search existing prefix */
683 for (ifpr = TAILQ_FIRST(&new->rp_ifp->if_prefixhead); ifpr;
684 ifpr = TAILQ_NEXT(ifpr, ifpr_list))
686 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
687 ifpr->ifpr_type != IN6_PREFIX_RR)
689 if (ifpr->ifpr_plen == new->rp_plen &&
690 in6_are_prefix_equal(IFPR_IN6(ifpr), RP_IN6(new),
697 * We got a prefix which we have seen in the past.
700 * If the origin of the already-installed prefix is more
701 * preferable than the new one, ignore installation request.
703 if (rpp->rp_origin > new->rp_origin)
706 /* update prefix information */
707 rpp->rp_flags.prf_ra = new->rp_flags.prf_ra;
708 if (rpp->rp_origin >= PR_ORIG_RR)
709 rpp->rp_flags.prf_rr = new->rp_flags.prf_rr;
710 rpp->rp_vltime = new->rp_vltime;
711 rpp->rp_pltime = new->rp_pltime;
712 rpp->rp_expire = new->rp_expire;
713 rpp->rp_preferred = new->rp_preferred;
714 rpp->rp_statef_delmark = 0; /* cancel deletion */
716 * Interface id related update.
717 * add rp_addr entries in new into rpp, if they have not
718 * been already included in rpp.
720 while (!LIST_EMPTY(&new->rp_addrhead))
722 rap = LIST_FIRST(&new->rp_addrhead);
723 LIST_REMOVE(rap, ra_entry);
724 if (search_ifidwithprefix(rpp, &rap->ra_ifid)
727 crit_enter(); /* XXX MP not MP safe */
728 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
731 kfree(rap, M_RR_ADDR);
735 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
740 * We got a fresh prefix.
742 /* create new prefix */
743 rpp = (struct rr_prefix *)kmalloc(sizeof(*rpp), M_IP6RR,
746 log(LOG_ERR, "in6_prefix.c: rrpr_update:%d"
747 ": ENOBUFS for rr_prefix\n", __LINE__);
752 LIST_INIT(&rpp->rp_addrhead);
753 /* move rp_addr entries of new to rpp */
754 while (!LIST_EMPTY(&new->rp_addrhead))
756 rap = LIST_FIRST(&new->rp_addrhead);
757 LIST_REMOVE(rap, ra_entry);
758 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
761 /* let rp_ifpr.ifpr_prefix point rr_prefix. */
762 rpp->rp_ifpr.ifpr_prefix = (struct sockaddr *)&rpp->rp_prefix;
763 /* link rr_prefix entry to if_prefixlist */
765 struct ifnet *ifp = rpp->rp_ifp;
766 struct ifprefix *ifpr;
768 if ((ifpr = TAILQ_FIRST(&ifp->if_prefixhead))
770 for ( ; TAILQ_NEXT(ifpr, ifpr_list);
771 ifpr = TAILQ_NEXT(ifpr, ifpr_list))
773 TAILQ_NEXT(ifpr, ifpr_list) = rp2ifpr(rpp);
775 TAILQ_FIRST(&ifp->if_prefixhead) =
777 rp2ifpr(rpp)->ifpr_type = IN6_PREFIX_RR;
779 /* link rr_prefix entry to rr_prefix list */
781 LIST_INSERT_HEAD(&rr_prefix, rpp, rp_entry);
785 if (!new->rp_raf_auto)
789 * Add an address for each interface id, if it is not yet
790 * If it existed but not pointing to the prefix yet,
791 * init the prefix pointer.
793 LIST_FOREACH(rap, &rpp->rp_addrhead, ra_entry)
795 if (rap->ra_addr != NULL) {
796 if (rap->ra_addr->ia6_ifpr == NULL)
797 rap->ra_addr->ia6_ifpr = rp2ifpr(rpp);
800 add_each_addr(so, rpp, rap);
806 add_each_prefix(struct socket *so, struct rr_prefix *rpp)
808 init_prefix_ltimes(rpp);
809 return (rrpr_update(so, rpp));
813 rp_remove(struct rr_prefix *rpp)
816 /* unlink rp_entry from if_prefixlist */
818 struct ifnet *ifp = rpp->rp_ifp;
819 struct ifprefix *ifpr;
821 if ((ifpr = TAILQ_FIRST(&ifp->if_prefixhead)) == rp2ifpr(rpp))
822 TAILQ_FIRST(&ifp->if_prefixhead) =
823 TAILQ_NEXT(ifpr, ifpr_list);
825 while (TAILQ_NEXT(ifpr, ifpr_list) != NULL &&
826 (TAILQ_NEXT(ifpr, ifpr_list) != rp2ifpr(rpp)))
827 ifpr = TAILQ_NEXT(ifpr, ifpr_list);
828 if (TAILQ_NEXT(ifpr, ifpr_list))
829 TAILQ_NEXT(ifpr, ifpr_list) =
830 TAILQ_NEXT(rp2ifpr(rpp), ifpr_list);
832 kprintf("Couldn't unlink rr_prefix from ifp\n");
835 /* unlink rp_entry from rr_prefix list */
836 LIST_REMOVE(rpp, rp_entry);
842 create_ra_entry(struct rp_addr **rapp)
844 *rapp = (struct rp_addr *)kmalloc(sizeof(struct rp_addr), M_RR_ADDR,
847 log(LOG_ERR, "in6_prefix.c: init_newprefix:%d: ENOBUFS"
848 "for rp_addr\n", __LINE__);
856 init_newprefix(struct in6_rrenumreq *irr, struct ifprefix *ifpr,
857 struct rr_prefix *rpp)
859 struct rp_addr *orap;
862 bzero(rpp, sizeof(*rpp));
863 rpp->rp_type = IN6_PREFIX_RR;
864 rpp->rp_ifp = ifpr->ifpr_ifp;
865 rpp->rp_plen = ifpr->ifpr_plen;
866 rpp->rp_prefix.sin6_len = sizeof(rpp->rp_prefix);
867 rpp->rp_prefix.sin6_family = AF_INET6;
868 bit_copy((char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
869 (char *)&irr->irr_useprefix.sin6_addr,
870 sizeof(irr->irr_useprefix.sin6_addr) << 3,
871 0, irr->irr_u_uselen);
872 /* copy keeplen part if necessary as necessary len */
873 if (irr->irr_u_uselen < ifpr->ifpr_plen)
874 bit_copy((char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
875 (char *)IFPR_IN6(ifpr), sizeof(*IFPR_IN6(ifpr)) << 3,
877 min(ifpr->ifpr_plen - irr->irr_u_uselen,
878 irr->irr_u_keeplen));
879 LIST_FOREACH(orap, &(ifpr2rp(ifpr)->rp_addrhead), ra_entry)
884 if ((error = create_ra_entry(&rap)) != 0)
886 rap->ra_ifid = orap->ra_ifid;
887 rap->ra_flags.anycast = (orap->ra_addr != NULL &&
888 (orap->ra_addr->ia6_flags &
889 IN6_IFF_ANYCAST) != 0) ? 1 : 0;
890 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
892 rpp->rp_vltime = irr->irr_vltime;
893 rpp->rp_pltime = irr->irr_pltime;
894 rpp->rp_raf_onlink = irr->irr_raf_mask_onlink ? irr->irr_raf_onlink :
895 ifpr2rp(ifpr)->rp_raf_onlink;
896 rpp->rp_raf_auto = irr->irr_raf_mask_auto ? irr->irr_raf_auto :
897 ifpr2rp(ifpr)->rp_raf_auto;
898 /* Is some FlagMasks for rrf necessary? */
899 rpp->rp_rrf = irr->irr_rrf;
900 rpp->rp_origin = irr->irr_origin;
906 free_rp_entries(struct rr_prefix *rpp)
909 * This func is only called with rpp on stack(not on list).
910 * So no crit_enter() here
912 while (!LIST_EMPTY(&rpp->rp_addrhead))
916 rap = LIST_FIRST(&rpp->rp_addrhead);
917 LIST_REMOVE(rap, ra_entry);
919 crit_enter(); /* XXX MP not MP safe */
920 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
923 kfree(rap, M_RR_ADDR);
928 add_useprefixes(struct socket *so, struct ifnet *ifp,
929 struct in6_rrenumreq *irr)
931 struct ifprefix *ifpr, *nextifpr;
935 /* add prefixes to each of marked prefix */
936 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; ifpr = nextifpr)
938 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
939 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
940 ifpr->ifpr_type != IN6_PREFIX_RR)
942 if (ifpr2rp(ifpr)->rp_statef_addmark) {
943 if ((error = init_newprefix(irr, ifpr, &rp)) != 0)
945 error = add_each_prefix(so, &rp);
948 /* free each rp_addr entry */
949 free_rp_entries(&rp);
955 unprefer_prefix(struct rr_prefix *rpp)
959 for (rap = rpp->rp_addrhead.lh_first; rap != NULL;
960 rap = rap->ra_entry.le_next) {
961 if (rap->ra_addr == NULL)
963 rap->ra_addr->ia6_lifetime.ia6t_preferred = time_second;
964 rap->ra_addr->ia6_lifetime.ia6t_pltime = 0;
969 delete_each_prefix(struct rr_prefix *rpp, u_char origin)
973 if (rpp->rp_origin > origin)
976 while (rpp->rp_addrhead.lh_first != NULL) {
980 rap = LIST_FIRST(&rpp->rp_addrhead);
985 LIST_REMOVE(rap, ra_entry);
987 if (rap->ra_addr == NULL) {
988 kfree(rap, M_RR_ADDR);
991 rap->ra_addr->ia6_ifpr = NULL;
993 in6_purgeaddr(&rap->ra_addr->ia_ifa);
994 crit_enter(); /* XXX MP not MP safe */
995 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
997 kfree(rap, M_RR_ADDR);
1005 delete_prefixes(struct ifnet *ifp, u_char origin)
1007 struct ifprefix *ifpr, *nextifpr;
1009 /* delete prefixes marked as tobe deleted */
1010 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; ifpr = nextifpr)
1012 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
1013 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
1014 ifpr->ifpr_type != IN6_PREFIX_RR)
1016 if (ifpr2rp(ifpr)->rp_statef_delmark)
1017 delete_each_prefix(ifpr2rp(ifpr), origin);
1022 link_stray_ia6s(struct rr_prefix *rpp)
1024 struct ifaddr_container *ifac;
1026 TAILQ_FOREACH(ifac, &rpp->rp_ifp->if_addrheads[mycpuid], ifa_link) {
1027 struct ifaddr *ifa = ifac->ifa;
1028 struct rp_addr *rap;
1029 struct rr_prefix *orpp;
1032 if (ifa->ifa_addr->sa_family != AF_INET6)
1034 if (rpp->rp_plen > in6_matchlen(RP_IN6(rpp), IFA_IN6(ifa)))
1037 orpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
1039 if (!in6_are_prefix_equal(RP_IN6(orpp), RP_IN6(rpp),
1041 log(LOG_ERR, "in6_prefix.c: link_stray_ia6s:"
1042 "addr %s/%d already linked to a prefix"
1043 "and it matches also %s/%d\n",
1044 ip6_sprintf(IFA_IN6(ifa)), orpp->rp_plen,
1045 ip6_sprintf(RP_IN6(rpp)),
1049 if ((error = assign_ra_entry(rpp,
1050 (sizeof(rap->ra_ifid) << 3) -
1052 (struct in6_ifaddr *)ifa)) != 0)
1058 /* XXX assumes that permission is already checked by the caller */
1060 in6_prefix_ioctl(struct socket *so, u_long cmd, caddr_t data,
1063 struct ifaddr_container *ifac;
1064 struct rr_prefix *rpp, rp_tmp;
1065 struct rp_addr *rap;
1066 struct in6_prefixreq *ipr = (struct in6_prefixreq *)data;
1067 struct in6_rrenumreq *irr = (struct in6_rrenumreq *)data;
1071 * Failsafe for errneous address config program.
1072 * Let's hope rrenumd don't make a mistakes.
1074 if (ipr->ipr_origin <= PR_ORIG_RA)
1075 ipr->ipr_origin = PR_ORIG_STATIC;
1078 case SIOCSGIFPREFIX_IN6:
1079 delmark_global_prefixes(ifp, irr);
1081 case SIOCAIFPREFIX_IN6:
1082 case SIOCCIFPREFIX_IN6:
1083 /* check if preferred lifetime > valid lifetime */
1084 if (irr->irr_pltime > irr->irr_vltime) {
1086 "in6_prefix_ioctl: preferred lifetime"
1087 "(%ld) is greater than valid lifetime(%ld)\n",
1088 (u_long)irr->irr_pltime, (u_long)irr->irr_vltime);
1092 if (mark_matched_prefixes(cmd, ifp, irr)) {
1093 if (irr->irr_u_uselen != 0)
1094 if ((error = add_useprefixes(so, ifp, irr))
1097 if (cmd != SIOCAIFPREFIX_IN6)
1098 delete_prefixes(ifp, irr->irr_origin);
1100 return (EADDRNOTAVAIL);
1102 unmark_prefixes(ifp);
1104 case SIOCGIFPREFIX_IN6:
1105 rpp = search_matched_prefix(ifp, ipr);
1106 if (rpp == NULL || ifp != rpp->rp_ifp)
1107 return (EADDRNOTAVAIL);
1109 ipr->ipr_origin = rpp->rp_origin;
1110 ipr->ipr_plen = rpp->rp_plen;
1111 ipr->ipr_vltime = rpp->rp_vltime;
1112 ipr->ipr_pltime = rpp->rp_pltime;
1113 ipr->ipr_flags = rpp->rp_flags;
1114 ipr->ipr_prefix = rpp->rp_prefix;
1117 case SIOCSIFPREFIX_IN6:
1118 /* check if preferred lifetime > valid lifetime */
1119 if (ipr->ipr_pltime > ipr->ipr_vltime) {
1121 "in6_prefix_ioctl: preferred lifetime"
1122 "(%ld) is greater than valid lifetime(%ld)\n",
1123 (u_long)ipr->ipr_pltime, (u_long)ipr->ipr_vltime);
1129 bzero((caddr_t)&rp_tmp, sizeof(rp_tmp));
1130 rp_tmp.rp_ifp = ifp;
1131 rp_tmp.rp_plen = ipr->ipr_plen;
1132 rp_tmp.rp_prefix = ipr->ipr_prefix;
1133 rp_tmp.rp_vltime = ipr->ipr_vltime;
1134 rp_tmp.rp_pltime = ipr->ipr_pltime;
1135 rp_tmp.rp_flags = ipr->ipr_flags;
1136 rp_tmp.rp_origin = ipr->ipr_origin;
1138 /* create rp_addr entries, usually at least for lladdr */
1139 if ((error = link_stray_ia6s(&rp_tmp)) != 0) {
1140 free_rp_entries(&rp_tmp);
1143 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_list) {
1144 struct ifaddr *ifa = ifac->ifa;
1146 if (ifa->ifa_addr == NULL)
1147 continue; /* just for safety */
1148 if (ifa->ifa_addr->sa_family != AF_INET6)
1150 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)) == 0)
1153 if ((error = create_ra_entry(&rap)) != 0) {
1154 free_rp_entries(&rp_tmp);
1157 /* copy interface id part */
1158 bit_copy((caddr_t)&rap->ra_ifid,
1159 sizeof(rap->ra_ifid) << 3,
1160 (caddr_t)IFA_IN6(ifa),
1161 sizeof(*IFA_IN6(ifa)) << 3,
1163 (sizeof(rap->ra_ifid) << 3) - rp_tmp.rp_plen);
1164 /* insert into list */
1165 LIST_INSERT_HEAD(&rp_tmp.rp_addrhead, rap, ra_entry);
1168 error = add_each_prefix(so, &rp_tmp);
1170 /* free each rp_addr entry */
1171 free_rp_entries(&rp_tmp);
1174 case SIOCDIFPREFIX_IN6:
1175 rpp = search_matched_prefix(ifp, ipr);
1176 if (rpp == NULL || ifp != rpp->rp_ifp)
1177 return (EADDRNOTAVAIL);
1179 error = delete_each_prefix(rpp, ipr->ipr_origin);
1187 in6_rr_timer(void *ignored_arg)
1189 struct rr_prefix *rpp;
1191 callout_reset(&in6_rr_timer_ch, ip6_rr_prune * hz,
1192 in6_rr_timer, NULL);
1196 rpp = LIST_FIRST(&rr_prefix);
1198 if (rpp->rp_expire && rpp->rp_expire < time_second) {
1199 struct rr_prefix *next_rpp;
1201 next_rpp = LIST_NEXT(rpp, rp_entry);
1202 delete_each_prefix(rpp, PR_ORIG_KERNEL);
1206 if (rpp->rp_preferred && rpp->rp_preferred < time_second)
1207 unprefer_prefix(rpp);
1208 rpp = LIST_NEXT(rpp, rp_entry);