]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_prefix.c
xnu-1228.12.14.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_prefix.c
1 /* $KAME: in6_prefix.c,v 1.27 2000/03/29 23:13:13 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 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.
51 *
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
62 * SUCH DAMAGE.
63 *
64 * @(#)in.c 8.2 (Berkeley) 11/15/93
65 */
66
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>
75 #include <sys/proc.h>
76
77 #include <net/if.h>
78
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>
84
85 #ifdef __APPLE__
86 #define M_IP6RR M_IP6MISC
87 #define M_RR_ADDR M_IP6MISC
88 #else
89 static MALLOC_DEFINE(M_IP6RR, "ip6rr", "IPv6 Router Renumbering Prefix");
90 static MALLOC_DEFINE(M_RR_ADDR, "rp_addr", "IPv6 Router Renumbering Ifid");
91 #endif
92
93 struct rr_prhead rr_prefix;
94
95 #include <net/net_osdep.h>
96
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);
104 extern lck_mtx_t *prefix6_mutex;
105
106 /*
107 * Copy bits from src to tgt, from off bit for len bits.
108 * Caller must specify collect tgtsize and srcsize.
109 */
110 static void
111 bit_copy(char *tgt, u_int tgtsize, char *src, u_int srcsize,
112 u_int off, u_int len)
113 {
114 char *sp, *tp;
115
116 /* arg values check */
117 if (srcsize < off || srcsize < (off + len) ||
118 tgtsize < off || tgtsize < (off + len)) {
119 log(LOG_ERR,
120 "in6_prefix.c: bit_copy: invalid args: srcsize %d,\n"
121 "tgtsize %d, off %d, len %d\n", srcsize, tgtsize, off,
122 len);
123 return;
124 }
125
126 /* search start point */
127 for (sp = src, tp = tgt; off >= 8; sp++, tp++)
128 off-=8;
129 /* copy starting bits */
130 if (off) {
131 char setbit;
132 int startbits;
133
134 startbits = min((8 - off), len);
135
136 for (setbit = (0x80 >> off); startbits;
137 setbit >>= 1, startbits--, len--)
138 *tp |= (setbit & *sp);
139 tp++;
140 sp++;
141 }
142 /* copy midium bits */
143 for (; len >= 8; sp++, tp++) {
144 *tp = *sp;
145 len-=8;
146 }
147 /* copy ending bits */
148 if (len) {
149 char setbit;
150
151 for (setbit = 0x80; len; setbit >>= 1, len--)
152 *tp |= (setbit & *sp);
153 }
154 }
155
156 static struct ifprefix *
157 in6_prefixwithifp(struct ifnet *ifp, int plen, struct in6_addr *dst)
158 {
159 struct ifprefix *ifpr;
160
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))
165 {
166 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
167 ifpr->ifpr_type != IN6_PREFIX_RR)
168 continue;
169 if (plen <= in6_matchlen(dst, IFPR_IN6(ifpr)))
170 break;
171 }
172 ifnet_lock_done(ifp);
173 return (ifpr);
174 }
175
176 /*
177 * Search prefix which matches arg prefix as specified in
178 * draft-ietf-ipngwg-router-renum-08.txt
179 */
180 static struct rr_prefix *
181 search_matched_prefix(struct ifnet *ifp, struct in6_prefixreq *ipr)
182 {
183 struct ifprefix *ifpr;
184 struct ifaddr *ifa;
185 struct rr_prefix *rpp;
186
187 /* search matched prefix */
188 ifpr = in6_prefixwithifp(ifp, ipr->ipr_plen,
189 &ipr->ipr_prefix.sin6_addr);
190 if (ifpr != NULL)
191 return ifpr2rp(ifpr);
192
193 /*
194 * search matched addr, and then search prefix
195 * which matches the addr
196 */
197
198 ifnet_lock_shared(ifp);
199 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
200 {
201 if (ifa->ifa_addr->sa_family != AF_INET6)
202 continue;
203 if (ipr->ipr_plen <=
204 in6_matchlen(&ipr->ipr_prefix.sin6_addr, IFA_IN6(ifa)))
205 break;
206 }
207 if (ifa == NULL) {
208 ifnet_lock_done(ifp);
209 return NULL;
210 }
211
212 rpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
213 if (rpp != 0) {
214 ifnet_lock_done(ifp);
215 return rpp;
216 }
217
218 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
219 ifpr = TAILQ_NEXT(ifpr, ifpr_list))
220 {
221 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
222 ifpr->ifpr_type != IN6_PREFIX_RR)
223 continue;
224 if (ifpr->ifpr_plen <= in6_matchlen(IFA_IN6(ifa),
225 IFPR_IN6(ifpr)))
226 break;
227 }
228 ifnet_lock_done(ifp);
229 if (ifpr != NULL)
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);
234 }
235
236 /*
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.
240 */
241 static int
242 mark_matched_prefixes(u_long cmd, struct ifnet *ifp, struct in6_rrenumreq *irr)
243 {
244 struct ifprefix *ifpr;
245 struct ifaddr *ifa;
246 int matchlen, matched = 0;
247
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))
252 {
253 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
254 ifpr->ifpr_type != IN6_PREFIX_RR)
255 continue;
256 matchlen = in6_matchlen(&irr->irr_matchprefix.sin6_addr,
257 IFPR_IN6(ifpr));
258 if (irr->irr_m_minlen > ifpr->ifpr_plen ||
259 irr->irr_m_maxlen < ifpr->ifpr_plen ||
260 irr->irr_m_len > matchlen)
261 continue;
262 matched = 1;
263 ifpr2rp(ifpr)->rp_statef_addmark = 1;
264 if (cmd == SIOCCIFPREFIX_IN6)
265 ifpr2rp(ifpr)->rp_statef_delmark = 1;
266 }
267
268 /*
269 * search matched addr, and then search prefixes
270 * which matche the addr
271 */
272 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
273 {
274 struct rr_prefix *rpp;
275
276 if (ifa->ifa_addr->sa_family != AF_INET6)
277 continue;
278 matchlen = in6_matchlen(&irr->irr_matchprefix.sin6_addr,
279 IFA_IN6(ifa));
280 if (irr->irr_m_minlen > matchlen ||
281 irr->irr_m_maxlen < matchlen || irr->irr_m_len > matchlen)
282 continue;
283 rpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
284 if (rpp != 0) {
285 matched = 1;
286 rpp->rp_statef_addmark = 1;
287 if (cmd == SIOCCIFPREFIX_IN6)
288 rpp->rp_statef_delmark = 1;
289 } else
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)));
294 }
295 ifnet_lock_done(ifp);
296 return matched;
297 }
298
299 /*
300 * Mark global prefixes as to be deleted.
301 */
302 static void
303 delmark_global_prefixes(struct ifnet *ifp, __unused struct in6_rrenumreq *irr)
304 {
305 struct ifprefix *ifpr;
306
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))
311 {
312 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
313 ifpr->ifpr_type != IN6_PREFIX_RR)
314 continue;
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;
319 }
320 ifnet_lock_done(ifp);
321 }
322
323 /* Unmark prefixes */
324 static void
325 unmark_prefixes(struct ifnet *ifp)
326 {
327 struct ifprefix *ifpr;
328
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))
333 {
334 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
335 ifpr->ifpr_type != IN6_PREFIX_RR)
336 continue;
337 /* unmark prefix */
338 ifpr2rp(ifpr)->rp_statef_addmark = 0;
339 ifpr2rp(ifpr)->rp_statef_delmark = 0;
340 }
341 ifnet_lock_done(ifp);
342 }
343
344 static void
345 init_prefix_ltimes(struct rr_prefix *rpp)
346 {
347 struct timeval timenow;
348
349 getmicrotime(&timenow);
350
351 if (rpp->rp_pltime == RR_INFINITE_LIFETIME ||
352 rpp->rp_rrf_decrprefd == 0)
353 rpp->rp_preferred = 0;
354 else
355 rpp->rp_preferred = timenow.tv_sec + rpp->rp_pltime;
356 if (rpp->rp_vltime == RR_INFINITE_LIFETIME ||
357 rpp->rp_rrf_decrvalid == 0)
358 rpp->rp_expire = 0;
359 else
360 rpp->rp_expire = timenow.tv_sec + rpp->rp_vltime;
361 }
362
363 static int
364 rr_are_ifid_equal(struct in6_addr *ii1, struct in6_addr *ii2, int ii_len)
365 {
366 int ii_bytelen, ii_bitlen;
367 int p_bytelen, p_bitlen;
368
369 /* sanity check */
370 if (1 > ii_len ||
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",
373 ii_len);
374 return(0);
375 }
376
377 ii_bytelen = ii_len / 8;
378 ii_bitlen = ii_len % 8;
379
380 p_bytelen = sizeof(struct in6_addr) - ii_bytelen - 1;
381 p_bitlen = 8 - ii_bitlen;
382
383 if (bcmp(ii1->s6_addr + p_bytelen + 1, ii2->s6_addr + p_bytelen + 1,
384 ii_bytelen))
385 return(0);
386 if (((ii1->s6_addr[p_bytelen] << p_bitlen) & 0xff) !=
387 ((ii2->s6_addr[p_bytelen] << p_bitlen) & 0xff))
388 return(0);
389
390 return(1);
391 }
392
393 static struct rp_addr *
394 search_ifidwithprefix(struct rr_prefix *rpp, struct in6_addr *ifid)
395 {
396 struct rp_addr *rap;
397
398 lck_mtx_lock(prefix6_mutex);
399 LIST_FOREACH(rap, &rpp->rp_addrhead, ra_entry)
400 {
401 if (rr_are_ifid_equal(ifid, &rap->ra_ifid,
402 (sizeof(struct in6_addr) << 3) -
403 rpp->rp_plen))
404 break;
405 }
406 lck_mtx_unlock(prefix6_mutex);
407 return rap;
408 }
409
410 static int
411 assign_ra_entry(struct rr_prefix *rpp, int iilen, struct in6_ifaddr *ia)
412 {
413 int error = 0;
414 struct rp_addr *rap;
415
416 if ((error = create_ra_entry(&rap)) != 0)
417 return error;
418
419 /* copy interface id part */
420 bit_copy((caddr_t)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3,
421 (caddr_t)IA6_IN6(ia),
422 sizeof(*IA6_IN6(ia)) << 3, rpp->rp_plen, iilen);
423 /* link to ia, and put into list */
424 rap->ra_addr = ia;
425 ifaref(&rap->ra_addr->ia_ifa);
426 #if 0 /* Can't do this now, because rpp may be on th stack. should fix it? */
427 ia->ia6_ifpr = rp2ifpr(rpp);
428 #endif
429 lck_mtx_lock(prefix6_mutex);
430 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
431 lck_mtx_unlock(prefix6_mutex);
432
433 return 0;
434 }
435
436 /*
437 * add a link-local address to an interface. we will add new interface address
438 * (prefix database + new interface id).
439 */
440 static int
441 in6_prefix_add_llifid(__unused int iilen, struct in6_ifaddr *ia)
442 {
443 struct rr_prefix *rpp;
444 struct rp_addr *rap;
445 struct socket so;
446 int error;
447
448 if ((error = create_ra_entry(&rap)) != 0)
449 return(error);
450 /* copy interface id part */
451 bit_copy((caddr_t)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3,
452 (caddr_t)IA6_IN6(ia), sizeof(*IA6_IN6(ia)) << 3,
453 64, (sizeof(rap->ra_ifid) << 3) - 64);
454 /* XXX: init dummy so */
455 bzero(&so, sizeof(so));
456 /* insert into list */
457 lck_mtx_lock(prefix6_mutex);
458 LIST_FOREACH(rpp, &rr_prefix, rp_entry)
459 {
460 /*
461 * do not attempt to add an address, if ifp does not match
462 */
463 if (rpp->rp_ifp != ia->ia_ifp)
464 continue;
465
466 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
467 add_each_addr(&so, rpp, rap);
468 }
469 lck_mtx_unlock(prefix6_mutex);
470 return 0;
471 }
472
473 /*
474 * add an address to an interface. if the interface id portion is new,
475 * we will add new interface address (prefix database + new interface id).
476 */
477 int
478 in6_prefix_add_ifid(int iilen, struct in6_ifaddr *ia)
479 {
480 int plen = (sizeof(*IA6_IN6(ia)) << 3) - iilen;
481 struct ifprefix *ifpr;
482 struct rp_addr *rap;
483 int error = 0;
484
485 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
486 return(in6_prefix_add_llifid(iilen, ia));
487 ifpr = in6_prefixwithifp(ia->ia_ifp, plen, IA6_IN6(ia));
488 if (ifpr == NULL) {
489 struct rr_prefix rp;
490 struct socket so;
491 int pplen = (plen == 128) ? 64 : plen; /* XXX hardcoded 64 is bad */
492
493 /* allocate a prefix for ia, with default properties */
494
495 /* init rp */
496 bzero(&rp, sizeof(rp));
497 rp.rp_type = IN6_PREFIX_RR;
498 rp.rp_ifp = ia->ia_ifp;
499 rp.rp_plen = pplen;
500 rp.rp_prefix.sin6_len = sizeof(rp.rp_prefix);
501 rp.rp_prefix.sin6_family = AF_INET6;
502 bit_copy((char *)RP_IN6(&rp), sizeof(*RP_IN6(&rp)) << 3,
503 (char *)&ia->ia_addr.sin6_addr,
504 sizeof(ia->ia_addr.sin6_addr) << 3,
505 0, pplen);
506 rp.rp_vltime = rp.rp_pltime = RR_INFINITE_LIFETIME;
507 rp.rp_raf_onlink = 1;
508 rp.rp_raf_auto = 1;
509 /* Is some FlagMasks for rrf necessary? */
510 rp.rp_rrf_decrvalid = rp.rp_rrf_decrprefd = 0;
511 rp.rp_origin = PR_ORIG_RR; /* can be renumbered */
512
513 /* create ra_entry */
514 error = link_stray_ia6s(&rp);
515 if (error != 0) {
516 free_rp_entries(&rp);
517 return error;
518 }
519
520 /* XXX: init dummy so */
521 bzero(&so, sizeof(so));
522
523 error = add_each_prefix(&so, &rp);
524
525 /* free each rp_addr entry */
526 free_rp_entries(&rp);
527
528 if (error != 0)
529 return error;
530
531 /* search again */
532 ifpr = in6_prefixwithifp(ia->ia_ifp, pplen, IA6_IN6(ia));
533 if (ifpr == NULL)
534 return 0;
535 }
536 rap = search_ifidwithprefix(ifpr2rp(ifpr), IA6_IN6(ia));
537 if (rap != NULL) {
538 if (rap->ra_addr == NULL) {
539 rap->ra_addr = ia;
540 ifaref(&rap->ra_addr->ia_ifa);
541 } else if (rap->ra_addr != ia) {
542 /* There may be some inconsistencies between addrs. */
543 log(LOG_ERR, "ip6_prefix.c: addr %s/%d matched prefix"
544 " already has another ia %p(%s) on its ifid list\n",
545 ip6_sprintf(IA6_IN6(ia)), plen,
546 rap->ra_addr,
547 ip6_sprintf(IA6_IN6(rap->ra_addr)));
548 return EADDRINUSE /* XXX */;
549 }
550 ia->ia6_ifpr = ifpr;
551 return 0;
552 }
553 error = assign_ra_entry(ifpr2rp(ifpr), iilen, ia);
554 if (error == 0)
555 ia->ia6_ifpr = ifpr;
556 return (error);
557 }
558
559 void
560 in6_prefix_remove_ifid(__unused int iilen, struct in6_ifaddr *ia)
561 {
562 struct rp_addr *rap;
563
564 if (ia->ia6_ifpr == NULL)
565 return;
566 rap = search_ifidwithprefix(ifpr2rp(ia->ia6_ifpr), IA6_IN6(ia));
567 if (rap != NULL) {
568 lck_mtx_lock(prefix6_mutex);
569 LIST_REMOVE(rap, ra_entry);
570 lck_mtx_unlock(prefix6_mutex);
571 if (rap->ra_addr)
572 ifafree(&rap->ra_addr->ia_ifa);
573 FREE(rap, M_RR_ADDR);
574 }
575
576 if (LIST_EMPTY(&ifpr2rp(ia->ia6_ifpr)->rp_addrhead))
577 rp_remove(ifpr2rp(ia->ia6_ifpr));
578 }
579
580 void
581 in6_purgeprefix(
582 struct ifnet *ifp)
583 {
584 struct ifprefix *ifpr, *nextifpr;
585
586 /* delete prefixes before ifnet goes away */
587 ifnet_lock_exclusive(ifp);
588 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
589 ifpr = nextifpr)
590 {
591 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
592 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
593 ifpr->ifpr_type != IN6_PREFIX_RR)
594 continue;
595 (void)delete_each_prefix(ifpr2rp(ifpr), PR_ORIG_KERNEL);
596 }
597 ifnet_lock_done(ifp);
598 }
599
600 static void
601 add_each_addr(struct socket *so, struct rr_prefix *rpp, struct rp_addr *rap)
602 {
603 struct in6_ifaddr *ia6;
604 struct in6_aliasreq ifra;
605 int error;
606
607 /* init ifra */
608 bzero(&ifra, sizeof(ifra));
609 strncpy(ifra.ifra_name, if_name(rpp->rp_ifp), sizeof(ifra.ifra_name));
610 ifra.ifra_addr.sin6_family = ifra.ifra_prefixmask.sin6_family =
611 AF_INET6;
612 ifra.ifra_addr.sin6_len = ifra.ifra_prefixmask.sin6_len =
613 sizeof(ifra.ifra_addr);
614 /* copy prefix part */
615 bit_copy((char *)&ifra.ifra_addr.sin6_addr,
616 sizeof(ifra.ifra_addr.sin6_addr) << 3,
617 (char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
618 0, rpp->rp_plen);
619 /* copy interface id part */
620 bit_copy((char *)&ifra.ifra_addr.sin6_addr,
621 sizeof(ifra.ifra_addr.sin6_addr) << 3,
622 (char *)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3,
623 rpp->rp_plen, (sizeof(rap->ra_ifid) << 3) - rpp->rp_plen);
624 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, rpp->rp_plen);
625 /* don't care ifra_flags for now */
626
627 /*
628 * XXX: if we did this with finite lifetime values, the lifetimes would
629 * decrese in time and never incremented.
630 * we should need more clarifications on the prefix mechanism...
631 */
632 ifra.ifra_lifetime.ia6t_vltime = rpp->rp_vltime;
633 ifra.ifra_lifetime.ia6t_pltime = rpp->rp_pltime;
634
635 ia6 = in6ifa_ifpwithaddr(rpp->rp_ifp, &ifra.ifra_addr.sin6_addr);
636 if (ia6 != NULL) {
637 if (ia6->ia6_ifpr == NULL) {
638 /* link this addr and the prefix each other */
639 if (rap->ra_addr)
640 ifafree(&rap->ra_addr->ia_ifa);
641 rap->ra_addr = ia6;
642 ifaref(&rap->ra_addr->ia_ifa);
643 ia6->ia6_ifpr = rp2ifpr(rpp);
644 return;
645 }
646 if (ia6->ia6_ifpr == rp2ifpr(rpp)) {
647 if (rap->ra_addr)
648 ifafree(&rap->ra_addr->ia_ifa);
649 rap->ra_addr = ia6;
650 ifaref(&rap->ra_addr->ia_ifa);
651 return;
652 }
653 /*
654 * The addr is already assigned to other
655 * prefix.
656 * There may be some inconsistencies between
657 * prefixes.
658 * e.g. overraped prefixes with common starting
659 * part and different plefixlen.
660 * Or, completely duplicated prefixes?
661 * log it and return.
662 */
663 log(LOG_ERR,
664 "in6_prefix.c: add_each_addr: addition of an addr %s/%d "
665 "failed because there is already another addr %s/%d\n",
666 ip6_sprintf(&ifra.ifra_addr.sin6_addr), rpp->rp_plen,
667 ip6_sprintf(IA6_IN6(ia6)),
668 in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL));
669 return;
670 }
671 /* propagate ANYCAST flag if it is set for ancestor addr */
672 if (rap->ra_flags.anycast != 0)
673 ifra.ifra_flags |= IN6_IFF_ANYCAST;
674 error = in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, rpp->rp_ifp,
675 current_proc());
676 if (error != 0) {
677 log(LOG_ERR, "in6_prefix.c: add_each_addr: addition of an addr"
678 "%s/%d failed because in6_control failed for error %d\n",
679 ip6_sprintf(&ifra.ifra_addr.sin6_addr), rpp->rp_plen,
680 error);
681 return;
682 }
683
684 /*
685 * link beween this addr and the prefix will be done
686 * in in6_prefix_add_ifid
687 */
688 }
689
690 static int
691 rrpr_update(struct socket *so, struct rr_prefix *new)
692 {
693 struct rr_prefix *rpp;
694 struct ifprefix *ifpr;
695 struct rp_addr *rap;
696
697 /* search existing prefix */
698 ifnet_lock_exclusive(new->rp_ifp);
699 for (ifpr = TAILQ_FIRST(&new->rp_ifp->if_prefixhead); ifpr;
700 ifpr = TAILQ_NEXT(ifpr, ifpr_list))
701 {
702 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
703 ifpr->ifpr_type != IN6_PREFIX_RR)
704 continue;
705 if (ifpr->ifpr_plen == new->rp_plen &&
706 in6_are_prefix_equal(IFPR_IN6(ifpr), RP_IN6(new),
707 ifpr->ifpr_plen))
708 break;
709 }
710 rpp = ifpr2rp(ifpr);
711 if (rpp != NULL) {
712 /*
713 * We got a prefix which we have seen in the past.
714 */
715 /*
716 * If the origin of the already-installed prefix is more
717 * preferable than the new one, ignore installation request.
718 */
719 if (rpp->rp_origin > new->rp_origin) {
720 ifnet_lock_done(new->rp_ifp);
721 return(EPERM);
722 }
723
724 /* update prefix information */
725 rpp->rp_flags.prf_ra = new->rp_flags.prf_ra;
726 if (rpp->rp_origin >= PR_ORIG_RR)
727 rpp->rp_flags.prf_rr = new->rp_flags.prf_rr;
728 rpp->rp_vltime = new->rp_vltime;
729 rpp->rp_pltime = new->rp_pltime;
730 rpp->rp_expire = new->rp_expire;
731 rpp->rp_preferred = new->rp_preferred;
732 rpp->rp_statef_delmark = 0; /* cancel deletion */
733 /*
734 * Interface id related update.
735 * add rp_addr entries in new into rpp, if they have not
736 * been already included in rpp.
737 */
738 lck_mtx_lock(prefix6_mutex);
739 while (!LIST_EMPTY(&new->rp_addrhead))
740 {
741 rap = LIST_FIRST(&new->rp_addrhead);
742 LIST_REMOVE(rap, ra_entry);
743 if (search_ifidwithprefix(rpp, &rap->ra_ifid)
744 != NULL) {
745 if (rap->ra_addr)
746 ifafree(&rap->ra_addr->ia_ifa);
747 FREE(rap, M_RR_ADDR);
748 continue;
749 }
750 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
751 }
752 lck_mtx_unlock(prefix6_mutex);
753 } else {
754 /*
755 * We got a fresh prefix.
756 */
757 /* create new prefix */
758 rpp = (struct rr_prefix *)_MALLOC(sizeof(*rpp), M_IP6RR,
759 M_NOWAIT);
760 if (rpp == NULL) {
761 log(LOG_ERR, "in6_prefix.c: rrpr_update:%d"
762 ": ENOBUFS for rr_prefix\n", __LINE__);
763 ifnet_lock_done(new->rp_ifp);
764 return(ENOBUFS);
765 }
766 /* initilization */
767 lck_mtx_lock(prefix6_mutex);
768 *rpp = *new;
769 LIST_INIT(&rpp->rp_addrhead);
770 /* move rp_addr entries of new to rpp */
771 while (!LIST_EMPTY(&new->rp_addrhead))
772 {
773 rap = LIST_FIRST(&new->rp_addrhead);
774 LIST_REMOVE(rap, ra_entry);
775 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
776 }
777 lck_mtx_unlock(prefix6_mutex);
778
779 /* let rp_ifpr.ifpr_prefix point rr_prefix. */
780 rpp->rp_ifpr.ifpr_prefix = (struct sockaddr *)&rpp->rp_prefix;
781 /* link rr_prefix entry to if_prefixlist */
782 {
783 struct ifnet *ifp = rpp->rp_ifp;
784
785 if ((ifpr = TAILQ_FIRST(&ifp->if_prefixhead))
786 != NULL) {
787 for ( ; TAILQ_NEXT(ifpr, ifpr_list);
788 ifpr = TAILQ_NEXT(ifpr, ifpr_list))
789 continue;
790 TAILQ_NEXT(ifpr, ifpr_list) = rp2ifpr(rpp);
791 } else
792 TAILQ_FIRST(&ifp->if_prefixhead) =
793 rp2ifpr(rpp);
794 rp2ifpr(rpp)->ifpr_type = IN6_PREFIX_RR;
795 }
796 /* link rr_prefix entry to rr_prefix list */
797 lck_mtx_lock(prefix6_mutex);
798 LIST_INSERT_HEAD(&rr_prefix, rpp, rp_entry);
799 lck_mtx_unlock(prefix6_mutex);
800 }
801 ifnet_lock_done(new->rp_ifp);
802
803 if (!new->rp_raf_auto)
804 return 0;
805
806 /*
807 * Add an address for each interface id, if it is not yet
808 * If it existed but not pointing to the prefix yet,
809 * init the prefix pointer.
810 */
811 lck_mtx_lock(prefix6_mutex);
812 LIST_FOREACH(rap, &rpp->rp_addrhead, ra_entry)
813 {
814 if (rap->ra_addr != NULL) {
815 if (rap->ra_addr->ia6_ifpr == NULL)
816 rap->ra_addr->ia6_ifpr = rp2ifpr(rpp);
817 continue;
818 }
819 add_each_addr(so, rpp, rap);
820 }
821 lck_mtx_unlock(prefix6_mutex);
822 return 0;
823 }
824
825 static int
826 add_each_prefix(struct socket *so, struct rr_prefix *rpp)
827 {
828 init_prefix_ltimes(rpp);
829 return(rrpr_update(so, rpp));
830 }
831
832 static void
833 rp_remove(struct rr_prefix *rpp)
834 {
835
836 /* unlink rp_entry from if_prefixlist */
837 lck_mtx_lock(prefix6_mutex);
838 {
839 struct ifnet *ifp = rpp->rp_ifp;
840 struct ifprefix *ifpr;
841
842 ifnet_lock_exclusive(ifp);
843 if ((ifpr = TAILQ_FIRST(&ifp->if_prefixhead)) == rp2ifpr(rpp))
844 TAILQ_FIRST(&ifp->if_prefixhead) =
845 TAILQ_NEXT(ifpr, ifpr_list);
846 else {
847 while (TAILQ_NEXT(ifpr, ifpr_list) != NULL &&
848 (TAILQ_NEXT(ifpr, ifpr_list) != rp2ifpr(rpp)))
849 ifpr = TAILQ_NEXT(ifpr, ifpr_list);
850 if (TAILQ_NEXT(ifpr, ifpr_list))
851 TAILQ_NEXT(ifpr, ifpr_list) =
852 TAILQ_NEXT(rp2ifpr(rpp), ifpr_list);
853 else
854 printf("Couldn't unlink rr_prefix from ifp\n");
855 }
856 ifnet_lock_done(ifp);
857 }
858 /* unlink rp_entry from rr_prefix list */
859 LIST_REMOVE(rpp, rp_entry);
860 lck_mtx_unlock(prefix6_mutex);
861 FREE(rpp, M_IP6RR);
862 }
863
864 static int
865 create_ra_entry(struct rp_addr **rapp)
866 {
867 *rapp = (struct rp_addr *)_MALLOC(sizeof(struct rp_addr), M_RR_ADDR,
868 M_NOWAIT);
869 if (*rapp == NULL) {
870 log(LOG_ERR, "in6_prefix.c: init_newprefix:%d: ENOBUFS"
871 "for rp_addr\n", __LINE__);
872 return ENOBUFS;
873 }
874 bzero(*rapp, sizeof(*(*rapp)));
875
876 return 0;
877 }
878
879 static int
880 init_newprefix(struct in6_rrenumreq *irr, struct ifprefix *ifpr,
881 struct rr_prefix *rpp)
882 {
883 struct rp_addr *orap;
884
885 /* init rp */
886 bzero(rpp, sizeof(*rpp));
887 rpp->rp_type = IN6_PREFIX_RR;
888 rpp->rp_ifp = ifpr->ifpr_ifp;
889 rpp->rp_plen = ifpr->ifpr_plen;
890 rpp->rp_prefix.sin6_len = sizeof(rpp->rp_prefix);
891 rpp->rp_prefix.sin6_family = AF_INET6;
892 bit_copy((char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
893 (char *)&irr->irr_useprefix.sin6_addr,
894 sizeof(irr->irr_useprefix.sin6_addr) << 3,
895 0, irr->irr_u_uselen);
896 /* copy keeplen part if necessary as necessary len */
897 if (irr->irr_u_uselen < ifpr->ifpr_plen)
898 bit_copy((char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
899 (char *)IFPR_IN6(ifpr), sizeof(*IFPR_IN6(ifpr)) << 3,
900 irr->irr_u_uselen,
901 min(ifpr->ifpr_plen - irr->irr_u_uselen,
902 irr->irr_u_keeplen));
903 lck_mtx_lock(prefix6_mutex);
904 LIST_FOREACH(orap, &(ifpr2rp(ifpr)->rp_addrhead), ra_entry)
905 {
906 struct rp_addr *rap;
907 int error = 0;
908
909 if ((error = create_ra_entry(&rap)) != 0)
910 return error;
911 rap->ra_ifid = orap->ra_ifid;
912 rap->ra_flags.anycast = (orap->ra_addr != NULL &&
913 (orap->ra_addr->ia6_flags &
914 IN6_IFF_ANYCAST) != 0) ? 1 : 0;
915 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
916 }
917 rpp->rp_vltime = irr->irr_vltime;
918 rpp->rp_pltime = irr->irr_pltime;
919 rpp->rp_raf_onlink = irr->irr_raf_mask_onlink ? irr->irr_raf_onlink :
920 ifpr2rp(ifpr)->rp_raf_onlink;
921 rpp->rp_raf_auto = irr->irr_raf_mask_auto ? irr->irr_raf_auto :
922 ifpr2rp(ifpr)->rp_raf_auto;
923 /* Is some FlagMasks for rrf necessary? */
924 rpp->rp_rrf = irr->irr_rrf;
925 rpp->rp_origin = irr->irr_origin;
926 lck_mtx_unlock(prefix6_mutex);
927
928 return 0;
929 }
930
931 static void
932 free_rp_entries(struct rr_prefix *rpp)
933 {
934 /*
935 * This func is only called with rpp on stack(not on list).
936 * So no splnet() here
937 */
938 lck_mtx_lock(prefix6_mutex);
939 while (!LIST_EMPTY(&rpp->rp_addrhead))
940 {
941 struct rp_addr *rap;
942
943 rap = LIST_FIRST(&rpp->rp_addrhead);
944 LIST_REMOVE(rap, ra_entry);
945 if (rap->ra_addr)
946 ifafree(&rap->ra_addr->ia_ifa);
947 FREE(rap, M_RR_ADDR);
948 }
949 lck_mtx_unlock(prefix6_mutex);
950 }
951
952 static int
953 add_useprefixes(struct socket *so, struct ifnet *ifp,
954 struct in6_rrenumreq *irr)
955 {
956 struct ifprefix *ifpr, *nextifpr;
957 struct rr_prefix rp;
958 int error = 0;
959
960 /* add prefixes to each of marked prefix */
961 ifnet_lock_exclusive(ifp);
962 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; ifpr = nextifpr)
963 {
964 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
965 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
966 ifpr->ifpr_type != IN6_PREFIX_RR)
967 continue;
968 if (ifpr2rp(ifpr)->rp_statef_addmark) {
969 if ((error = init_newprefix(irr, ifpr, &rp)) != 0)
970 break;
971 error = add_each_prefix(so, &rp);
972 }
973 }
974 ifnet_lock_done(ifp);
975 /* free each rp_addr entry */
976 free_rp_entries(&rp);
977
978 return error;
979 }
980
981 static void
982 unprefer_prefix(struct rr_prefix *rpp)
983 {
984 struct rp_addr *rap;
985 struct timeval timenow;
986
987 getmicrotime(&timenow);
988
989 lck_mtx_lock(prefix6_mutex);
990 for (rap = rpp->rp_addrhead.lh_first; rap != NULL;
991 rap = rap->ra_entry.le_next) {
992 if (rap->ra_addr == NULL)
993 continue;
994 rap->ra_addr->ia6_lifetime.ia6t_preferred = timenow.tv_sec;
995 rap->ra_addr->ia6_lifetime.ia6t_pltime = 0;
996 }
997 lck_mtx_unlock(prefix6_mutex);
998
999 }
1000
1001 int
1002 delete_each_prefix(struct rr_prefix *rpp, u_char origin)
1003 {
1004 int error = 0;
1005
1006 if (rpp->rp_origin > origin)
1007 return(EPERM);
1008
1009 lck_mtx_lock(prefix6_mutex);
1010 while (rpp->rp_addrhead.lh_first != NULL) {
1011 struct rp_addr *rap;
1012
1013 rap = LIST_FIRST(&rpp->rp_addrhead);
1014 if (rap == NULL) {
1015 break;
1016 }
1017 LIST_REMOVE(rap, ra_entry);
1018 if (rap->ra_addr == NULL) {
1019 FREE(rap, M_RR_ADDR);
1020 continue;
1021 }
1022 rap->ra_addr->ia6_ifpr = NULL;
1023
1024 in6_purgeaddr(&rap->ra_addr->ia_ifa, 0);
1025 ifafree(&rap->ra_addr->ia_ifa);
1026 FREE(rap, M_RR_ADDR);
1027 }
1028 rp_remove(rpp);
1029 lck_mtx_unlock(prefix6_mutex);
1030
1031 return error;
1032 }
1033
1034 static void
1035 delete_prefixes(struct ifnet *ifp, u_char origin)
1036 {
1037 struct ifprefix *ifpr, *nextifpr;
1038
1039 /* delete prefixes marked as tobe deleted */
1040 ifnet_lock_exclusive(ifp);
1041 for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; ifpr = nextifpr)
1042 {
1043 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
1044 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
1045 ifpr->ifpr_type != IN6_PREFIX_RR)
1046 continue;
1047 if (ifpr2rp(ifpr)->rp_statef_delmark)
1048 (void)delete_each_prefix(ifpr2rp(ifpr), origin);
1049 }
1050 ifnet_lock_done(ifp);
1051 }
1052
1053 static int
1054 link_stray_ia6s(struct rr_prefix *rpp)
1055 {
1056 struct ifaddr *ifa;
1057
1058 for (ifa = rpp->rp_ifp->if_addrlist.tqh_first; ifa;
1059 ifa = ifa->ifa_list.tqe_next)
1060 {
1061 struct rp_addr *rap;
1062 struct rr_prefix *orpp;
1063 int error = 0;
1064
1065 if (ifa->ifa_addr->sa_family != AF_INET6)
1066 continue;
1067 if (rpp->rp_plen > in6_matchlen(RP_IN6(rpp), IFA_IN6(ifa)))
1068 continue;
1069
1070 orpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
1071 if (orpp != NULL) {
1072 if (!in6_are_prefix_equal(RP_IN6(orpp), RP_IN6(rpp),
1073 rpp->rp_plen))
1074 log(LOG_ERR, "in6_prefix.c: link_stray_ia6s:"
1075 "addr %s/%d already linked to a prefix"
1076 "and it matches also %s/%d\n",
1077 ip6_sprintf(IFA_IN6(ifa)), orpp->rp_plen,
1078 ip6_sprintf(RP_IN6(rpp)),
1079 rpp->rp_plen);
1080 continue;
1081 }
1082 if ((error = assign_ra_entry(rpp,
1083 (sizeof(rap->ra_ifid) << 3) -
1084 rpp->rp_plen,
1085 (struct in6_ifaddr *)ifa)) != 0)
1086 return error;
1087 }
1088 return 0;
1089 }
1090
1091 /* XXX assumes that permission is already checked by the caller */
1092 int
1093 in6_prefix_ioctl(struct socket *so, u_long cmd, caddr_t data,
1094 struct ifnet *ifp)
1095 {
1096 struct rr_prefix *rpp, rp_tmp;
1097 struct rp_addr *rap;
1098 struct in6_prefixreq *ipr = (struct in6_prefixreq *)data;
1099 struct in6_rrenumreq *irr = (struct in6_rrenumreq *)data;
1100 struct ifaddr *ifa;
1101 int error = 0;
1102
1103 /*
1104 * Failsafe for erroneous address config program.
1105 * Let's hope rrenumd don't make a mistakes.
1106 */
1107 if (ipr->ipr_origin <= PR_ORIG_RA)
1108 ipr->ipr_origin = PR_ORIG_STATIC;
1109
1110 switch (cmd) {
1111 case SIOCSGIFPREFIX_IN6:
1112 delmark_global_prefixes(ifp, irr);
1113 /* FALL THROUGH */
1114 case SIOCAIFPREFIX_IN6:
1115 case SIOCCIFPREFIX_IN6:
1116 /* check if preferred lifetime > valid lifetime */
1117 if (irr->irr_pltime > irr->irr_vltime) {
1118 log(LOG_NOTICE,
1119 "in6_prefix_ioctl: preferred lifetime"
1120 "(%ld) is greater than valid lifetime(%ld)\n",
1121 (u_long)irr->irr_pltime, (u_long)irr->irr_vltime);
1122 error = EINVAL;
1123 break;
1124 }
1125 if (mark_matched_prefixes(cmd, ifp, irr)) {
1126 if (irr->irr_u_uselen != 0)
1127 if ((error = add_useprefixes(so, ifp, irr))
1128 != 0)
1129 goto failed;
1130 if (cmd != SIOCAIFPREFIX_IN6)
1131 delete_prefixes(ifp, irr->irr_origin);
1132 } else
1133 return (EADDRNOTAVAIL);
1134 failed:
1135 unmark_prefixes(ifp);
1136 break;
1137 case SIOCGIFPREFIX_IN6:
1138 rpp = search_matched_prefix(ifp, ipr);
1139 if (rpp == NULL || ifp != rpp->rp_ifp)
1140 return (EADDRNOTAVAIL);
1141
1142 ipr->ipr_origin = rpp->rp_origin;
1143 ipr->ipr_plen = rpp->rp_plen;
1144 ipr->ipr_vltime = rpp->rp_vltime;
1145 ipr->ipr_pltime = rpp->rp_pltime;
1146 ipr->ipr_flags = rpp->rp_flags;
1147 ipr->ipr_prefix = rpp->rp_prefix;
1148
1149 break;
1150 case SIOCSIFPREFIX_IN6:
1151 /* check if preferred lifetime > valid lifetime */
1152 if (ipr->ipr_pltime > ipr->ipr_vltime) {
1153 log(LOG_NOTICE,
1154 "in6_prefix_ioctl: preferred lifetime"
1155 "(%ld) is greater than valid lifetime(%ld)\n",
1156 (u_long)ipr->ipr_pltime, (u_long)ipr->ipr_vltime);
1157 error = EINVAL;
1158 break;
1159 }
1160
1161 /* init rp_tmp */
1162 bzero((caddr_t)&rp_tmp, sizeof(rp_tmp));
1163 rp_tmp.rp_ifp = ifp;
1164 rp_tmp.rp_plen = ipr->ipr_plen;
1165 rp_tmp.rp_prefix = ipr->ipr_prefix;
1166 rp_tmp.rp_vltime = ipr->ipr_vltime;
1167 rp_tmp.rp_pltime = ipr->ipr_pltime;
1168 rp_tmp.rp_flags = ipr->ipr_flags;
1169 rp_tmp.rp_origin = ipr->ipr_origin;
1170
1171 /* create rp_addr entries, usually at least for lladdr */
1172 if ((error = link_stray_ia6s(&rp_tmp)) != 0) {
1173 free_rp_entries(&rp_tmp);
1174 break;
1175 }
1176 ifnet_lock_exclusive(ifp);
1177 for (ifa = ifp->if_addrlist.tqh_first;
1178 ifa;
1179 ifa = ifa->ifa_list.tqe_next)
1180 {
1181 if (ifa->ifa_addr == NULL)
1182 continue; /* just for safety */
1183 if (ifa->ifa_addr->sa_family != AF_INET6)
1184 continue;
1185 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)) == 0)
1186 continue;
1187
1188 if ((error = create_ra_entry(&rap)) != 0) {
1189 free_rp_entries(&rp_tmp);
1190 goto bad;
1191 }
1192 /* copy interface id part */
1193 bit_copy((caddr_t)&rap->ra_ifid,
1194 sizeof(rap->ra_ifid) << 3,
1195 (caddr_t)IFA_IN6(ifa),
1196 sizeof(*IFA_IN6(ifa)) << 3,
1197 rp_tmp.rp_plen,
1198 (sizeof(rap->ra_ifid) << 3) - rp_tmp.rp_plen);
1199 /* insert into list */
1200 lck_mtx_lock(prefix6_mutex);
1201 LIST_INSERT_HEAD(&rp_tmp.rp_addrhead, rap, ra_entry);
1202 lck_mtx_unlock(prefix6_mutex);
1203 }
1204 ifnet_lock_done(ifp);
1205
1206 error = add_each_prefix(so, &rp_tmp);
1207
1208 /* free each rp_addr entry */
1209 free_rp_entries(&rp_tmp);
1210
1211 break;
1212 case SIOCDIFPREFIX_IN6:
1213 rpp = search_matched_prefix(ifp, ipr);
1214 if (rpp == NULL || ifp != rpp->rp_ifp)
1215 return (EADDRNOTAVAIL);
1216
1217 ifnet_lock_exclusive(ifp);
1218 error = delete_each_prefix(rpp, ipr->ipr_origin);
1219 ifnet_lock_done(ifp);
1220 break;
1221 }
1222 bad:
1223 return error;
1224 }
1225
1226 void
1227 in6_rr_timer(__unused void *ignored_arg)
1228 {
1229 struct rr_prefix *rpp;
1230 struct timeval timenow;
1231
1232 getmicrotime(&timenow);
1233
1234 /* expire */
1235 lck_mtx_lock(prefix6_mutex);
1236 rpp = LIST_FIRST(&rr_prefix);
1237 while (rpp) {
1238 if (rpp->rp_expire && rpp->rp_expire < timenow.tv_sec) {
1239 struct rr_prefix *next_rpp;
1240
1241 next_rpp = LIST_NEXT(rpp, rp_entry);
1242 delete_each_prefix(rpp, PR_ORIG_KERNEL);
1243 rpp = next_rpp;
1244 continue;
1245 }
1246 if (rpp->rp_preferred && rpp->rp_preferred < timenow.tv_sec)
1247 unprefer_prefix(rpp);
1248 rpp = LIST_NEXT(rpp, rp_entry);
1249 }
1250 lck_mtx_unlock(prefix6_mutex);
1251 timeout(in6_rr_timer, (caddr_t)0, ip6_rr_prune * hz);
1252 }