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