HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / netinet6 / in6_prefix.c
blob0099540c9f15556ba07e115c9339ced4b536c452
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 $ */
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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
31 * SUCH DAMAGE.
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
40 * are met:
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
64 * SUCH DAMAGE.
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>
77 #include <sys/proc.h>
78 #include <sys/thread2.h>
80 #include <net/if.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,
98 struct rp_addr *rap);
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.
109 static void
110 bit_copy(char *tgt, u_int tgtsize, char *src, u_int srcsize,
111 u_int off, u_int len)
113 char *sp, *tp;
115 /* arg values check */
116 if (srcsize < off || srcsize < (off + len) ||
117 tgtsize < off || tgtsize < (off + len)) {
118 log(LOG_ERR,
119 "in6_prefix.c: bit_copy: invalid args: srcsize %d,\n"
120 "tgtsize %d, off %d, len %d\n", srcsize, tgtsize, off,
121 len);
122 return;
125 /* search start point */
126 for (sp = src, tp = tgt; off >= 8; sp++, tp++)
127 off-=8;
128 /* copy starting bits */
129 if (off) {
130 char setbit;
131 int startbits;
133 startbits = min((8 - off), len);
135 for (setbit = (0x80 >> off); startbits;
136 setbit >>= 1, startbits--, len--)
137 *tp |= (setbit & *sp);
138 tp++;
139 sp++;
141 /* copy midium bits */
142 for (; len >= 8; sp++, tp++) {
143 *tp = *sp;
144 len-=8;
146 /* copy ending bits */
147 if (len) {
148 char setbit;
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)
166 continue;
167 if (plen <= in6_matchlen(dst, IFPR_IN6(ifpr)))
168 break;
170 return (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;
182 struct ifaddr *ifa;
183 struct rr_prefix *rpp;
185 /* search matched prefix */
186 ifpr = in6_prefixwithifp(ifp, ipr->ipr_plen,
187 &ipr->ipr_prefix.sin6_addr);
188 if (ifpr != NULL)
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) {
197 ifa = ifac->ifa;
199 if (ifa->ifa_addr->sa_family != AF_INET6)
200 continue;
201 if (ipr->ipr_plen <=
202 in6_matchlen(&ipr->ipr_prefix.sin6_addr, IFA_IN6(ifa)))
203 break;
205 if (ifac == NULL)
206 return NULL;
208 rpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
209 if (rpp != 0)
210 return rpp;
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)
217 continue;
218 if (ifpr->ifpr_plen <= in6_matchlen(IFA_IN6(ifa),
219 IFPR_IN6(ifpr)))
220 break;
222 if (ifpr != NULL)
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.
234 static int
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)
247 continue;
248 matchlen = in6_matchlen(&irr->irr_matchprefix.sin6_addr,
249 IFPR_IN6(ifpr));
250 if (irr->irr_m_minlen > ifpr->ifpr_plen ||
251 irr->irr_m_maxlen < ifpr->ifpr_plen ||
252 irr->irr_m_len > matchlen)
253 continue;
254 matched = 1;
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)
269 continue;
270 matchlen = in6_matchlen(&irr->irr_matchprefix.sin6_addr,
271 IFA_IN6(ifa));
272 if (irr->irr_m_minlen > matchlen ||
273 irr->irr_m_maxlen < matchlen || irr->irr_m_len > matchlen)
274 continue;
275 rpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
276 if (rpp != 0) {
277 matched = 1;
278 rpp->rp_statef_addmark = 1;
279 if (cmd == SIOCCIFPREFIX_IN6)
280 rpp->rp_statef_delmark = 1;
281 } else
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)));
287 return matched;
291 * Mark global prefixes as to be deleted.
293 static void
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)
304 continue;
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 */
313 static void
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)
324 continue;
325 /* unmark prefix */
326 ifpr2rp(ifpr)->rp_statef_addmark = 0;
327 ifpr2rp(ifpr)->rp_statef_delmark = 0;
331 static void
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;
338 else
339 rpp->rp_preferred = time_second + rpp->rp_pltime;
340 if (rpp->rp_vltime == RR_INFINITE_LIFETIME ||
341 rpp->rp_rrf_decrvalid == 0)
342 rpp->rp_expire = 0;
343 else
344 rpp->rp_expire = time_second + rpp->rp_vltime;
347 static int
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;
353 /* sanity check */
354 if (1 > ii_len ||
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",
357 ii_len);
358 return (0);
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,
368 ii_bytelen))
369 return (0);
370 if (((ii1->s6_addr[p_bytelen] << p_bitlen) & 0xff) !=
371 ((ii2->s6_addr[p_bytelen] << p_bitlen) & 0xff))
372 return (0);
374 return (1);
377 static struct rp_addr *
378 search_ifidwithprefix(struct rr_prefix *rpp, struct in6_addr *ifid)
380 struct rp_addr *rap;
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) -
386 rpp->rp_plen))
387 break;
389 return rap;
392 static int
393 assign_ra_entry(struct rr_prefix *rpp, int iilen, struct in6_ifaddr *ia)
395 int error = 0;
396 struct rp_addr *rap;
398 if ((error = create_ra_entry(&rap)) != 0)
399 return error;
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 */
406 rap->ra_addr = ia;
407 crit_enter();
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);
411 #endif
412 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
413 crit_exit();
415 return 0;
419 * add a link-local address to an interface. we will add new interface address
420 * (prefix database + new interface id).
422 static int
423 in6_prefix_add_llifid(int iilen, struct in6_ifaddr *ia)
425 struct rr_prefix *rpp;
426 struct rp_addr *rap;
427 struct socket so;
428 int error;
430 if ((error = create_ra_entry(&rap)) != 0)
431 return (error);
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)
445 continue;
447 crit_enter();
448 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
449 crit_exit();
450 add_each_addr(&so, rpp, rap);
452 return 0;
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;
464 struct rp_addr *rap;
465 int error = 0;
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));
470 if (ifpr == NULL) {
471 struct rr_prefix rp;
472 struct socket so;
473 int pplen = (plen == 128) ? 64 : plen; /* XXX hardcoded 64 is bad */
475 /* allocate a prefix for ia, with default properties */
477 /* init rp */
478 bzero(&rp, sizeof(rp));
479 rp.rp_type = IN6_PREFIX_RR;
480 rp.rp_ifp = ia->ia_ifp;
481 rp.rp_plen = pplen;
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,
487 0, pplen);
488 rp.rp_vltime = rp.rp_pltime = RR_INFINITE_LIFETIME;
489 rp.rp_raf_onlink = 1;
490 rp.rp_raf_auto = 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);
497 if (error != 0) {
498 free_rp_entries(&rp);
499 return error;
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);
510 if (error != 0)
511 return error;
513 /* search again */
514 ifpr = in6_prefixwithifp(ia->ia_ifp, pplen, IA6_IN6(ia));
515 if (ifpr == NULL)
516 return 0;
518 rap = search_ifidwithprefix(ifpr2rp(ifpr), IA6_IN6(ia));
519 if (rap != NULL) {
520 if (rap->ra_addr == NULL) {
521 rap->ra_addr = ia;
522 crit_enter(); /* XXX MP not MP safe */
523 _IFAREF(&rap->ra_addr->ia_ifa, 0);
524 crit_exit();
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,
530 rap->ra_addr,
531 ip6_sprintf(IA6_IN6(rap->ra_addr)));
532 return EADDRINUSE /* XXX */;
534 ia->ia6_ifpr = ifpr;
535 return 0;
537 error = assign_ra_entry(ifpr2rp(ifpr), iilen, ia);
538 if (error == 0)
539 ia->ia6_ifpr = ifpr;
540 return (error);
543 void
544 in6_prefix_remove_ifid(int iilen, struct in6_ifaddr *ia)
546 struct rp_addr *rap;
548 if (ia->ia6_ifpr == NULL)
549 return;
550 rap = search_ifidwithprefix(ifpr2rp(ia->ia6_ifpr), IA6_IN6(ia));
551 if (rap != NULL) {
552 crit_enter();
553 LIST_REMOVE(rap, ra_entry);
554 if (rap->ra_addr)
555 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
556 crit_exit();
557 kfree(rap, M_RR_ADDR);
560 if (LIST_EMPTY(&ifpr2rp(ia->ia6_ifpr)->rp_addrhead))
561 rp_remove(ifpr2rp(ia->ia6_ifpr));
564 void
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;
571 ifpr = nextifpr)
573 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
574 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
575 ifpr->ifpr_type != IN6_PREFIX_RR)
576 continue;
577 delete_each_prefix(ifpr2rp(ifpr), PR_ORIG_KERNEL);
581 static void
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;
586 int error;
588 /* init 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 =
592 AF_INET6;
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,
599 0, rpp->rp_plen);
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);
617 if (ia6 != NULL) {
618 if (ia6->ia6_ifpr == NULL) {
619 crit_enter(); /* XXX MP not MP safe */
620 /* link this addr and the prefix each other */
621 if (rap->ra_addr)
622 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
623 rap->ra_addr = ia6;
624 _IFAREF(&rap->ra_addr->ia_ifa, 0);
625 crit_exit();
626 ia6->ia6_ifpr = rp2ifpr(rpp);
627 return;
629 if (ia6->ia6_ifpr == rp2ifpr(rpp)) {
630 crit_enter(); /* XXX MP not MP safe */
631 if (rap->ra_addr)
632 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
633 rap->ra_addr = ia6;
634 _IFAREF(&rap->ra_addr->ia_ifa, 0);
635 crit_exit();
636 return;
639 * The addr is already assigned to other
640 * prefix.
641 * There may be some inconsistencies between
642 * prefixes.
643 * e.g. overraped prefixes with common starting
644 * part and different plefixlen.
645 * Or, completely duplicated prefixes?
646 * log it and return.
648 log(LOG_ERR,
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));
654 return;
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,
660 curthread);
661 if (error != 0) {
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,
665 error);
666 return;
670 * link beween this addr and the prefix will be done
671 * in in6_prefix_add_ifid
675 static int
676 rrpr_update(struct socket *so, struct rr_prefix *new)
678 struct rr_prefix *rpp;
679 struct ifprefix *ifpr;
680 struct rp_addr *rap;
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)
688 continue;
689 if (ifpr->ifpr_plen == new->rp_plen &&
690 in6_are_prefix_equal(IFPR_IN6(ifpr), RP_IN6(new),
691 ifpr->ifpr_plen))
692 break;
694 rpp = ifpr2rp(ifpr);
695 if (rpp != NULL) {
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)
704 return (EPERM);
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)
725 != NULL) {
726 if (rap->ra_addr) {
727 crit_enter(); /* XXX MP not MP safe */
728 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
729 crit_exit();
731 kfree(rap, M_RR_ADDR);
732 continue;
734 crit_enter();
735 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
736 crit_exit();
738 } else {
740 * We got a fresh prefix.
742 /* create new prefix */
743 rpp = (struct rr_prefix *)kmalloc(sizeof(*rpp), M_IP6RR,
744 M_NOWAIT);
745 if (rpp == NULL) {
746 log(LOG_ERR, "in6_prefix.c: rrpr_update:%d"
747 ": ENOBUFS for rr_prefix\n", __LINE__);
748 return (ENOBUFS);
750 /* initilization */
751 *rpp = *new;
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))
769 != NULL) {
770 for ( ; TAILQ_NEXT(ifpr, ifpr_list);
771 ifpr = TAILQ_NEXT(ifpr, ifpr_list))
772 continue;
773 TAILQ_NEXT(ifpr, ifpr_list) = rp2ifpr(rpp);
774 } else
775 TAILQ_FIRST(&ifp->if_prefixhead) =
776 rp2ifpr(rpp);
777 rp2ifpr(rpp)->ifpr_type = IN6_PREFIX_RR;
779 /* link rr_prefix entry to rr_prefix list */
780 crit_enter();
781 LIST_INSERT_HEAD(&rr_prefix, rpp, rp_entry);
782 crit_exit();
785 if (!new->rp_raf_auto)
786 return 0;
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);
798 continue;
800 add_each_addr(so, rpp, rap);
802 return 0;
805 static int
806 add_each_prefix(struct socket *so, struct rr_prefix *rpp)
808 init_prefix_ltimes(rpp);
809 return (rrpr_update(so, rpp));
812 static void
813 rp_remove(struct rr_prefix *rpp)
815 crit_enter();
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);
824 else {
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);
831 else
832 kprintf("Couldn't unlink rr_prefix from ifp\n");
835 /* unlink rp_entry from rr_prefix list */
836 LIST_REMOVE(rpp, rp_entry);
837 crit_exit();
838 kfree(rpp, M_IP6RR);
841 static int
842 create_ra_entry(struct rp_addr **rapp)
844 *rapp = (struct rp_addr *)kmalloc(sizeof(struct rp_addr), M_RR_ADDR,
845 M_NOWAIT | M_ZERO);
846 if (*rapp == NULL) {
847 log(LOG_ERR, "in6_prefix.c: init_newprefix:%d: ENOBUFS"
848 "for rp_addr\n", __LINE__);
849 return ENOBUFS;
852 return 0;
855 static int
856 init_newprefix(struct in6_rrenumreq *irr, struct ifprefix *ifpr,
857 struct rr_prefix *rpp)
859 struct rp_addr *orap;
861 /* init rp */
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,
876 irr->irr_u_uselen,
877 min(ifpr->ifpr_plen - irr->irr_u_uselen,
878 irr->irr_u_keeplen));
879 LIST_FOREACH(orap, &(ifpr2rp(ifpr)->rp_addrhead), ra_entry)
881 struct rp_addr *rap;
882 int error = 0;
884 if ((error = create_ra_entry(&rap)) != 0)
885 return error;
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;
902 return 0;
905 static void
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))
914 struct rp_addr *rap;
916 rap = LIST_FIRST(&rpp->rp_addrhead);
917 LIST_REMOVE(rap, ra_entry);
918 if (rap->ra_addr) {
919 crit_enter(); /* XXX MP not MP safe */
920 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
921 crit_exit();
923 kfree(rap, M_RR_ADDR);
927 static int
928 add_useprefixes(struct socket *so, struct ifnet *ifp,
929 struct in6_rrenumreq *irr)
931 struct ifprefix *ifpr, *nextifpr;
932 struct rr_prefix rp;
933 int error = 0;
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)
941 continue;
942 if (ifpr2rp(ifpr)->rp_statef_addmark) {
943 if ((error = init_newprefix(irr, ifpr, &rp)) != 0)
944 break;
945 error = add_each_prefix(so, &rp);
948 /* free each rp_addr entry */
949 free_rp_entries(&rp);
951 return error;
954 static void
955 unprefer_prefix(struct rr_prefix *rpp)
957 struct rp_addr *rap;
959 for (rap = rpp->rp_addrhead.lh_first; rap != NULL;
960 rap = rap->ra_entry.le_next) {
961 if (rap->ra_addr == NULL)
962 continue;
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)
971 int error = 0;
973 if (rpp->rp_origin > origin)
974 return (EPERM);
976 while (rpp->rp_addrhead.lh_first != NULL) {
977 struct rp_addr *rap;
979 crit_enter();
980 rap = LIST_FIRST(&rpp->rp_addrhead);
981 if (rap == NULL) {
982 crit_exit();
983 break;
985 LIST_REMOVE(rap, ra_entry);
986 crit_exit();
987 if (rap->ra_addr == NULL) {
988 kfree(rap, M_RR_ADDR);
989 continue;
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);
996 crit_exit();
997 kfree(rap, M_RR_ADDR);
999 rp_remove(rpp);
1001 return error;
1004 static void
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)
1015 continue;
1016 if (ifpr2rp(ifpr)->rp_statef_delmark)
1017 delete_each_prefix(ifpr2rp(ifpr), origin);
1021 static int
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;
1030 int error = 0;
1032 if (ifa->ifa_addr->sa_family != AF_INET6)
1033 continue;
1034 if (rpp->rp_plen > in6_matchlen(RP_IN6(rpp), IFA_IN6(ifa)))
1035 continue;
1037 orpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
1038 if (orpp != NULL) {
1039 if (!in6_are_prefix_equal(RP_IN6(orpp), RP_IN6(rpp),
1040 rpp->rp_plen))
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)),
1046 rpp->rp_plen);
1047 continue;
1049 if ((error = assign_ra_entry(rpp,
1050 (sizeof(rap->ra_ifid) << 3) -
1051 rpp->rp_plen,
1052 (struct in6_ifaddr *)ifa)) != 0)
1053 return error;
1055 return 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,
1061 struct ifnet *ifp)
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;
1068 int error = 0;
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;
1077 switch (cmd) {
1078 case SIOCSGIFPREFIX_IN6:
1079 delmark_global_prefixes(ifp, irr);
1080 /* FALL THROUGH */
1081 case SIOCAIFPREFIX_IN6:
1082 case SIOCCIFPREFIX_IN6:
1083 /* check if preferred lifetime > valid lifetime */
1084 if (irr->irr_pltime > irr->irr_vltime) {
1085 log(LOG_NOTICE,
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);
1089 error = EINVAL;
1090 break;
1092 if (mark_matched_prefixes(cmd, ifp, irr)) {
1093 if (irr->irr_u_uselen != 0)
1094 if ((error = add_useprefixes(so, ifp, irr))
1095 != 0)
1096 goto failed;
1097 if (cmd != SIOCAIFPREFIX_IN6)
1098 delete_prefixes(ifp, irr->irr_origin);
1099 } else
1100 return (EADDRNOTAVAIL);
1101 failed:
1102 unmark_prefixes(ifp);
1103 break;
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;
1116 break;
1117 case SIOCSIFPREFIX_IN6:
1118 /* check if preferred lifetime > valid lifetime */
1119 if (ipr->ipr_pltime > ipr->ipr_vltime) {
1120 log(LOG_NOTICE,
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);
1124 error = EINVAL;
1125 break;
1128 /* init rp_tmp */
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);
1141 break;
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)
1149 continue;
1150 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)) == 0)
1151 continue;
1153 if ((error = create_ra_entry(&rap)) != 0) {
1154 free_rp_entries(&rp_tmp);
1155 goto bad;
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,
1162 rp_tmp.rp_plen,
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);
1173 break;
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);
1180 break;
1182 bad:
1183 return error;
1186 void
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);
1194 crit_enter();
1195 /* expire */
1196 rpp = LIST_FIRST(&rr_prefix);
1197 while (rpp) {
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);
1203 rpp = next_rpp;
1204 continue;
1206 if (rpp->rp_preferred && rpp->rp_preferred < time_second)
1207 unprefer_prefix(rpp);
1208 rpp = LIST_NEXT(rpp, rp_entry);
1210 crit_exit();