]>
Commit | Line | Data |
---|---|---|
1c79356b A |
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> | |
1c79356b A |
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> | |
1c79356b | 75 | #include <sys/proc.h> |
1c79356b A |
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 | ||
9bccf70c A |
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"); | |
1c79356b A |
91 | #endif |
92 | ||
93 | struct rr_prhead rr_prefix; | |
94 | ||
95 | #include <net/net_osdep.h> | |
96 | ||
91447636 A |
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; | |
1c79356b A |
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 */ | |
91447636 | 162 | ifnet_lock_shared(ifp); |
9bccf70c A |
163 | for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; |
164 | ifpr = TAILQ_NEXT(ifpr, ifpr_list)) | |
165 | { | |
1c79356b A |
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 | } | |
91447636 | 172 | ifnet_lock_done(ifp); |
1c79356b A |
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 | ||
91447636 | 198 | ifnet_lock_shared(ifp); |
9bccf70c | 199 | TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) |
1c79356b A |
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 | } | |
91447636 A |
207 | if (ifa == NULL) { |
208 | ifnet_lock_done(ifp); | |
1c79356b | 209 | return NULL; |
91447636 | 210 | } |
1c79356b A |
211 | |
212 | rpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr); | |
91447636 A |
213 | if (rpp != 0) { |
214 | ifnet_lock_done(ifp); | |
1c79356b | 215 | return rpp; |
91447636 A |
216 | } |
217 | ||
9bccf70c A |
218 | for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; |
219 | ifpr = TAILQ_NEXT(ifpr, ifpr_list)) | |
220 | { | |
1c79356b A |
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 | } | |
91447636 | 228 | ifnet_lock_done(ifp); |
1c79356b A |
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 */ | |
91447636 | 249 | ifnet_lock_exclusive(ifp); /* Should if_prefixhead be protected by IPv6?? */ |
9bccf70c A |
250 | for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; |
251 | ifpr = TAILQ_NEXT(ifpr, ifpr_list)) | |
252 | { | |
1c79356b A |
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 | */ | |
9bccf70c | 272 | TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) |
1c79356b A |
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 | } | |
91447636 | 295 | ifnet_lock_done(ifp); |
1c79356b A |
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, struct in6_rrenumreq *irr) | |
304 | { | |
305 | struct ifprefix *ifpr; | |
306 | ||
307 | /* search matched prefixes */ | |
91447636 | 308 | ifnet_lock_exclusive(ifp); |
9bccf70c A |
309 | for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; |
310 | ifpr = TAILQ_NEXT(ifpr, ifpr_list)) | |
311 | { | |
1c79356b A |
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 | } | |
91447636 | 320 | ifnet_lock_done(ifp); |
1c79356b A |
321 | } |
322 | ||
323 | /* Unmark prefixes */ | |
324 | static void | |
325 | unmark_prefixes(struct ifnet *ifp) | |
326 | { | |
327 | struct ifprefix *ifpr; | |
328 | ||
329 | /* unmark all prefix */ | |
91447636 | 330 | ifnet_lock_exclusive(ifp); |
9bccf70c A |
331 | for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; |
332 | ifpr = TAILQ_NEXT(ifpr, ifpr_list)) | |
333 | { | |
1c79356b A |
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 | } | |
91447636 | 341 | ifnet_lock_done(ifp); |
1c79356b A |
342 | } |
343 | ||
344 | static void | |
345 | init_prefix_ltimes(struct rr_prefix *rpp) | |
346 | { | |
91447636 A |
347 | struct timeval timenow; |
348 | ||
349 | getmicrotime(&timenow); | |
1c79356b A |
350 | |
351 | if (rpp->rp_pltime == RR_INFINITE_LIFETIME || | |
352 | rpp->rp_rrf_decrprefd == 0) | |
353 | rpp->rp_preferred = 0; | |
354 | else | |
91447636 | 355 | rpp->rp_preferred = timenow.tv_sec + rpp->rp_pltime; |
1c79356b A |
356 | if (rpp->rp_vltime == RR_INFINITE_LIFETIME || |
357 | rpp->rp_rrf_decrvalid == 0) | |
358 | rpp->rp_expire = 0; | |
359 | else | |
91447636 | 360 | rpp->rp_expire = timenow.tv_sec + rpp->rp_vltime; |
1c79356b A |
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 | ||
91447636 | 398 | lck_mtx_lock(prefix6_mutex); |
9bccf70c A |
399 | LIST_FOREACH(rap, &rpp->rp_addrhead, ra_entry) |
400 | { | |
1c79356b A |
401 | if (rr_are_ifid_equal(ifid, &rap->ra_ifid, |
402 | (sizeof(struct in6_addr) << 3) - | |
403 | rpp->rp_plen)) | |
404 | break; | |
9bccf70c | 405 | } |
91447636 | 406 | lck_mtx_unlock(prefix6_mutex); |
1c79356b A |
407 | return rap; |
408 | } | |
409 | ||
410 | static int | |
9bccf70c | 411 | assign_ra_entry(struct rr_prefix *rpp, int iilen, struct in6_ifaddr *ia) |
1c79356b A |
412 | { |
413 | int error = 0; | |
414 | struct rp_addr *rap; | |
415 | int s; | |
416 | ||
417 | if ((error = create_ra_entry(&rap)) != 0) | |
418 | return error; | |
419 | ||
420 | /* copy interface id part */ | |
421 | bit_copy((caddr_t)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3, | |
422 | (caddr_t)IA6_IN6(ia), | |
423 | sizeof(*IA6_IN6(ia)) << 3, rpp->rp_plen, iilen); | |
424 | /* link to ia, and put into list */ | |
425 | rap->ra_addr = ia; | |
9bccf70c | 426 | ifaref(&rap->ra_addr->ia_ifa); |
1c79356b A |
427 | #if 0 /* Can't do this now, because rpp may be on th stack. should fix it? */ |
428 | ia->ia6_ifpr = rp2ifpr(rpp); | |
429 | #endif | |
91447636 | 430 | lck_mtx_lock(prefix6_mutex); |
1c79356b | 431 | LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry); |
91447636 | 432 | lck_mtx_unlock(prefix6_mutex); |
1c79356b A |
433 | |
434 | return 0; | |
435 | } | |
436 | ||
9bccf70c A |
437 | /* |
438 | * add a link-local address to an interface. we will add new interface address | |
439 | * (prefix database + new interface id). | |
440 | */ | |
1c79356b A |
441 | static int |
442 | in6_prefix_add_llifid(int iilen, struct in6_ifaddr *ia) | |
443 | { | |
444 | struct rr_prefix *rpp; | |
445 | struct rp_addr *rap; | |
446 | struct socket so; | |
91447636 | 447 | int error; |
1c79356b A |
448 | |
449 | if ((error = create_ra_entry(&rap)) != 0) | |
450 | return(error); | |
451 | /* copy interface id part */ | |
452 | bit_copy((caddr_t)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3, | |
453 | (caddr_t)IA6_IN6(ia), sizeof(*IA6_IN6(ia)) << 3, | |
454 | 64, (sizeof(rap->ra_ifid) << 3) - 64); | |
455 | /* XXX: init dummy so */ | |
456 | bzero(&so, sizeof(so)); | |
457 | /* insert into list */ | |
91447636 | 458 | lck_mtx_lock(prefix6_mutex); |
1c79356b | 459 | LIST_FOREACH(rpp, &rr_prefix, rp_entry) |
1c79356b | 460 | { |
9bccf70c A |
461 | /* |
462 | * do not attempt to add an address, if ifp does not match | |
463 | */ | |
464 | if (rpp->rp_ifp != ia->ia_ifp) | |
465 | continue; | |
466 | ||
1c79356b | 467 | LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry); |
1c79356b A |
468 | add_each_addr(&so, rpp, rap); |
469 | } | |
91447636 | 470 | lck_mtx_unlock(prefix6_mutex); |
1c79356b A |
471 | return 0; |
472 | } | |
473 | ||
9bccf70c A |
474 | /* |
475 | * add an address to an interface. if the interface id portion is new, | |
476 | * we will add new interface address (prefix database + new interface id). | |
477 | */ | |
1c79356b A |
478 | int |
479 | in6_prefix_add_ifid(int iilen, struct in6_ifaddr *ia) | |
480 | { | |
481 | int plen = (sizeof(*IA6_IN6(ia)) << 3) - iilen; | |
482 | struct ifprefix *ifpr; | |
483 | struct rp_addr *rap; | |
484 | int error = 0; | |
485 | ||
486 | if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) | |
487 | return(in6_prefix_add_llifid(iilen, ia)); | |
488 | ifpr = in6_prefixwithifp(ia->ia_ifp, plen, IA6_IN6(ia)); | |
489 | if (ifpr == NULL) { | |
490 | struct rr_prefix rp; | |
491 | struct socket so; | |
9bccf70c | 492 | int pplen = (plen == 128) ? 64 : plen; /* XXX hardcoded 64 is bad */ |
1c79356b A |
493 | |
494 | /* allocate a prefix for ia, with default properties */ | |
495 | ||
496 | /* init rp */ | |
497 | bzero(&rp, sizeof(rp)); | |
498 | rp.rp_type = IN6_PREFIX_RR; | |
499 | rp.rp_ifp = ia->ia_ifp; | |
500 | rp.rp_plen = pplen; | |
501 | rp.rp_prefix.sin6_len = sizeof(rp.rp_prefix); | |
502 | rp.rp_prefix.sin6_family = AF_INET6; | |
503 | bit_copy((char *)RP_IN6(&rp), sizeof(*RP_IN6(&rp)) << 3, | |
504 | (char *)&ia->ia_addr.sin6_addr, | |
505 | sizeof(ia->ia_addr.sin6_addr) << 3, | |
506 | 0, pplen); | |
507 | rp.rp_vltime = rp.rp_pltime = RR_INFINITE_LIFETIME; | |
508 | rp.rp_raf_onlink = 1; | |
509 | rp.rp_raf_auto = 1; | |
510 | /* Is some FlagMasks for rrf necessary? */ | |
511 | rp.rp_rrf_decrvalid = rp.rp_rrf_decrprefd = 0; | |
512 | rp.rp_origin = PR_ORIG_RR; /* can be renumbered */ | |
513 | ||
514 | /* create ra_entry */ | |
515 | error = link_stray_ia6s(&rp); | |
516 | if (error != 0) { | |
517 | free_rp_entries(&rp); | |
518 | return error; | |
519 | } | |
520 | ||
521 | /* XXX: init dummy so */ | |
522 | bzero(&so, sizeof(so)); | |
1c79356b A |
523 | |
524 | error = add_each_prefix(&so, &rp); | |
525 | ||
526 | /* free each rp_addr entry */ | |
527 | free_rp_entries(&rp); | |
528 | ||
529 | if (error != 0) | |
530 | return error; | |
531 | ||
532 | /* search again */ | |
533 | ifpr = in6_prefixwithifp(ia->ia_ifp, pplen, IA6_IN6(ia)); | |
534 | if (ifpr == NULL) | |
535 | return 0; | |
536 | } | |
537 | rap = search_ifidwithprefix(ifpr2rp(ifpr), IA6_IN6(ia)); | |
538 | if (rap != NULL) { | |
539 | if (rap->ra_addr == NULL) { | |
540 | rap->ra_addr = ia; | |
9bccf70c | 541 | ifaref(&rap->ra_addr->ia_ifa); |
1c79356b A |
542 | } else if (rap->ra_addr != ia) { |
543 | /* There may be some inconsistencies between addrs. */ | |
544 | log(LOG_ERR, "ip6_prefix.c: addr %s/%d matched prefix" | |
9bccf70c | 545 | " already has another ia %p(%s) on its ifid list\n", |
1c79356b A |
546 | ip6_sprintf(IA6_IN6(ia)), plen, |
547 | rap->ra_addr, | |
548 | ip6_sprintf(IA6_IN6(rap->ra_addr))); | |
549 | return EADDRINUSE /* XXX */; | |
550 | } | |
551 | ia->ia6_ifpr = ifpr; | |
552 | return 0; | |
553 | } | |
9bccf70c | 554 | error = assign_ra_entry(ifpr2rp(ifpr), iilen, ia); |
1c79356b A |
555 | if (error == 0) |
556 | ia->ia6_ifpr = ifpr; | |
557 | return (error); | |
558 | } | |
559 | ||
560 | void | |
561 | in6_prefix_remove_ifid(int iilen, struct in6_ifaddr *ia) | |
562 | { | |
563 | struct rp_addr *rap; | |
564 | ||
565 | if (ia->ia6_ifpr == NULL) | |
566 | return; | |
567 | rap = search_ifidwithprefix(ifpr2rp(ia->ia6_ifpr), IA6_IN6(ia)); | |
568 | if (rap != NULL) { | |
91447636 | 569 | lck_mtx_lock(prefix6_mutex); |
1c79356b | 570 | LIST_REMOVE(rap, ra_entry); |
91447636 | 571 | lck_mtx_unlock(prefix6_mutex); |
1c79356b | 572 | if (rap->ra_addr) |
9bccf70c A |
573 | ifafree(&rap->ra_addr->ia_ifa); |
574 | FREE(rap, M_RR_ADDR); | |
1c79356b A |
575 | } |
576 | ||
1c79356b | 577 | if (LIST_EMPTY(&ifpr2rp(ia->ia6_ifpr)->rp_addrhead)) |
1c79356b A |
578 | rp_remove(ifpr2rp(ia->ia6_ifpr)); |
579 | } | |
580 | ||
581 | void | |
91447636 A |
582 | in6_purgeprefix( |
583 | struct ifnet *ifp) | |
1c79356b A |
584 | { |
585 | struct ifprefix *ifpr, *nextifpr; | |
586 | ||
587 | /* delete prefixes before ifnet goes away */ | |
91447636 | 588 | ifnet_lock_exclusive(ifp); |
1c79356b A |
589 | for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; |
590 | ifpr = nextifpr) | |
1c79356b | 591 | { |
1c79356b | 592 | nextifpr = TAILQ_NEXT(ifpr, ifpr_list); |
1c79356b A |
593 | if (ifpr->ifpr_prefix->sa_family != AF_INET6 || |
594 | ifpr->ifpr_type != IN6_PREFIX_RR) | |
595 | continue; | |
596 | (void)delete_each_prefix(ifpr2rp(ifpr), PR_ORIG_KERNEL); | |
597 | } | |
91447636 | 598 | ifnet_lock_done(ifp); |
1c79356b A |
599 | } |
600 | ||
601 | static void | |
602 | add_each_addr(struct socket *so, struct rr_prefix *rpp, struct rp_addr *rap) | |
603 | { | |
604 | struct in6_ifaddr *ia6; | |
605 | struct in6_aliasreq ifra; | |
606 | int error; | |
607 | ||
608 | /* init ifra */ | |
609 | bzero(&ifra, sizeof(ifra)); | |
610 | strncpy(ifra.ifra_name, if_name(rpp->rp_ifp), sizeof(ifra.ifra_name)); | |
611 | ifra.ifra_addr.sin6_family = ifra.ifra_prefixmask.sin6_family = | |
612 | AF_INET6; | |
613 | ifra.ifra_addr.sin6_len = ifra.ifra_prefixmask.sin6_len = | |
614 | sizeof(ifra.ifra_addr); | |
615 | /* copy prefix part */ | |
616 | bit_copy((char *)&ifra.ifra_addr.sin6_addr, | |
617 | sizeof(ifra.ifra_addr.sin6_addr) << 3, | |
618 | (char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3, | |
619 | 0, rpp->rp_plen); | |
620 | /* copy interface id part */ | |
621 | bit_copy((char *)&ifra.ifra_addr.sin6_addr, | |
622 | sizeof(ifra.ifra_addr.sin6_addr) << 3, | |
623 | (char *)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3, | |
624 | rpp->rp_plen, (sizeof(rap->ra_ifid) << 3) - rpp->rp_plen); | |
625 | in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, rpp->rp_plen); | |
626 | /* don't care ifra_flags for now */ | |
627 | ||
9bccf70c A |
628 | /* |
629 | * XXX: if we did this with finite lifetime values, the lifetimes would | |
630 | * decrese in time and never incremented. | |
631 | * we should need more clarifications on the prefix mechanism... | |
632 | */ | |
633 | ifra.ifra_lifetime.ia6t_vltime = rpp->rp_vltime; | |
634 | ifra.ifra_lifetime.ia6t_pltime = rpp->rp_pltime; | |
635 | ||
1c79356b A |
636 | ia6 = in6ifa_ifpwithaddr(rpp->rp_ifp, &ifra.ifra_addr.sin6_addr); |
637 | if (ia6 != NULL) { | |
638 | if (ia6->ia6_ifpr == NULL) { | |
639 | /* link this addr and the prefix each other */ | |
9bccf70c A |
640 | if (rap->ra_addr) |
641 | ifafree(&rap->ra_addr->ia_ifa); | |
1c79356b | 642 | rap->ra_addr = ia6; |
9bccf70c | 643 | ifaref(&rap->ra_addr->ia_ifa); |
1c79356b A |
644 | ia6->ia6_ifpr = rp2ifpr(rpp); |
645 | return; | |
646 | } | |
647 | if (ia6->ia6_ifpr == rp2ifpr(rpp)) { | |
9bccf70c A |
648 | if (rap->ra_addr) |
649 | ifafree(&rap->ra_addr->ia_ifa); | |
1c79356b | 650 | rap->ra_addr = ia6; |
9bccf70c | 651 | ifaref(&rap->ra_addr->ia_ifa); |
1c79356b A |
652 | return; |
653 | } | |
654 | /* | |
655 | * The addr is already assigned to other | |
656 | * prefix. | |
657 | * There may be some inconsistencies between | |
658 | * prefixes. | |
659 | * e.g. overraped prefixes with common starting | |
660 | * part and different plefixlen. | |
661 | * Or, completely duplicated prefixes? | |
662 | * log it and return. | |
663 | */ | |
9bccf70c A |
664 | log(LOG_ERR, |
665 | "in6_prefix.c: add_each_addr: addition of an addr %s/%d " | |
666 | "failed because there is already another addr %s/%d\n", | |
1c79356b A |
667 | ip6_sprintf(&ifra.ifra_addr.sin6_addr), rpp->rp_plen, |
668 | ip6_sprintf(IA6_IN6(ia6)), | |
9bccf70c | 669 | in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL)); |
1c79356b A |
670 | return; |
671 | } | |
672 | /* propagate ANYCAST flag if it is set for ancestor addr */ | |
673 | if (rap->ra_flags.anycast != 0) | |
674 | ifra.ifra_flags |= IN6_IFF_ANYCAST; | |
9bccf70c A |
675 | error = in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, rpp->rp_ifp, |
676 | current_proc()); | |
677 | if (error != 0) { | |
1c79356b A |
678 | log(LOG_ERR, "in6_prefix.c: add_each_addr: addition of an addr" |
679 | "%s/%d failed because in6_control failed for error %d\n", | |
680 | ip6_sprintf(&ifra.ifra_addr.sin6_addr), rpp->rp_plen, | |
681 | error); | |
682 | return; | |
9bccf70c | 683 | } |
1c79356b A |
684 | |
685 | /* | |
686 | * link beween this addr and the prefix will be done | |
687 | * in in6_prefix_add_ifid | |
688 | */ | |
689 | } | |
690 | ||
691 | static int | |
692 | rrpr_update(struct socket *so, struct rr_prefix *new) | |
693 | { | |
694 | struct rr_prefix *rpp; | |
695 | struct ifprefix *ifpr; | |
696 | struct rp_addr *rap; | |
697 | int s; | |
698 | ||
699 | /* search existing prefix */ | |
91447636 | 700 | ifnet_lock_exclusive(new->rp_ifp); |
9bccf70c A |
701 | for (ifpr = TAILQ_FIRST(&new->rp_ifp->if_prefixhead); ifpr; |
702 | ifpr = TAILQ_NEXT(ifpr, ifpr_list)) | |
703 | { | |
1c79356b A |
704 | if (ifpr->ifpr_prefix->sa_family != AF_INET6 || |
705 | ifpr->ifpr_type != IN6_PREFIX_RR) | |
706 | continue; | |
707 | if (ifpr->ifpr_plen == new->rp_plen && | |
708 | in6_are_prefix_equal(IFPR_IN6(ifpr), RP_IN6(new), | |
709 | ifpr->ifpr_plen)) | |
710 | break; | |
711 | } | |
712 | rpp = ifpr2rp(ifpr); | |
713 | if (rpp != NULL) { | |
714 | /* | |
715 | * We got a prefix which we have seen in the past. | |
716 | */ | |
717 | /* | |
718 | * If the origin of the already-installed prefix is more | |
719 | * preferable than the new one, ignore installation request. | |
720 | */ | |
91447636 A |
721 | if (rpp->rp_origin > new->rp_origin) { |
722 | ifnet_lock_done(new->rp_ifp); | |
1c79356b | 723 | return(EPERM); |
91447636 | 724 | } |
1c79356b A |
725 | |
726 | /* update prefix information */ | |
727 | rpp->rp_flags.prf_ra = new->rp_flags.prf_ra; | |
728 | if (rpp->rp_origin >= PR_ORIG_RR) | |
729 | rpp->rp_flags.prf_rr = new->rp_flags.prf_rr; | |
730 | rpp->rp_vltime = new->rp_vltime; | |
731 | rpp->rp_pltime = new->rp_pltime; | |
732 | rpp->rp_expire = new->rp_expire; | |
733 | rpp->rp_preferred = new->rp_preferred; | |
734 | rpp->rp_statef_delmark = 0; /* cancel deletion */ | |
735 | /* | |
736 | * Interface id related update. | |
737 | * add rp_addr entries in new into rpp, if they have not | |
738 | * been already included in rpp. | |
739 | */ | |
91447636 | 740 | lck_mtx_lock(prefix6_mutex); |
1c79356b | 741 | while (!LIST_EMPTY(&new->rp_addrhead)) |
1c79356b A |
742 | { |
743 | rap = LIST_FIRST(&new->rp_addrhead); | |
744 | LIST_REMOVE(rap, ra_entry); | |
745 | if (search_ifidwithprefix(rpp, &rap->ra_ifid) | |
746 | != NULL) { | |
747 | if (rap->ra_addr) | |
9bccf70c A |
748 | ifafree(&rap->ra_addr->ia_ifa); |
749 | FREE(rap, M_RR_ADDR); | |
1c79356b A |
750 | continue; |
751 | } | |
1c79356b | 752 | LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry); |
1c79356b | 753 | } |
91447636 | 754 | lck_mtx_unlock(prefix6_mutex); |
1c79356b A |
755 | } else { |
756 | /* | |
757 | * We got a fresh prefix. | |
758 | */ | |
759 | /* create new prefix */ | |
9bccf70c | 760 | rpp = (struct rr_prefix *)_MALLOC(sizeof(*rpp), M_IP6RR, |
1c79356b A |
761 | M_NOWAIT); |
762 | if (rpp == NULL) { | |
763 | log(LOG_ERR, "in6_prefix.c: rrpr_update:%d" | |
764 | ": ENOBUFS for rr_prefix\n", __LINE__); | |
91447636 | 765 | ifnet_lock_done(new->rp_ifp); |
1c79356b A |
766 | return(ENOBUFS); |
767 | } | |
768 | /* initilization */ | |
91447636 | 769 | lck_mtx_lock(prefix6_mutex); |
1c79356b A |
770 | *rpp = *new; |
771 | LIST_INIT(&rpp->rp_addrhead); | |
772 | /* move rp_addr entries of new to rpp */ | |
1c79356b | 773 | while (!LIST_EMPTY(&new->rp_addrhead)) |
1c79356b A |
774 | { |
775 | rap = LIST_FIRST(&new->rp_addrhead); | |
776 | LIST_REMOVE(rap, ra_entry); | |
777 | LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry); | |
778 | } | |
91447636 | 779 | lck_mtx_unlock(prefix6_mutex); |
1c79356b A |
780 | |
781 | /* let rp_ifpr.ifpr_prefix point rr_prefix. */ | |
782 | rpp->rp_ifpr.ifpr_prefix = (struct sockaddr *)&rpp->rp_prefix; | |
783 | /* link rr_prefix entry to if_prefixlist */ | |
784 | { | |
785 | struct ifnet *ifp = rpp->rp_ifp; | |
786 | struct ifprefix *ifpr; | |
787 | ||
9bccf70c A |
788 | if ((ifpr = TAILQ_FIRST(&ifp->if_prefixhead)) |
789 | != NULL) { | |
790 | for ( ; TAILQ_NEXT(ifpr, ifpr_list); | |
791 | ifpr = TAILQ_NEXT(ifpr, ifpr_list)) | |
1c79356b | 792 | continue; |
9bccf70c | 793 | TAILQ_NEXT(ifpr, ifpr_list) = rp2ifpr(rpp); |
1c79356b | 794 | } else |
9bccf70c A |
795 | TAILQ_FIRST(&ifp->if_prefixhead) = |
796 | rp2ifpr(rpp); | |
1c79356b A |
797 | rp2ifpr(rpp)->ifpr_type = IN6_PREFIX_RR; |
798 | } | |
799 | /* link rr_prefix entry to rr_prefix list */ | |
91447636 | 800 | lck_mtx_lock(prefix6_mutex); |
1c79356b | 801 | LIST_INSERT_HEAD(&rr_prefix, rpp, rp_entry); |
91447636 | 802 | lck_mtx_unlock(prefix6_mutex); |
1c79356b | 803 | } |
91447636 | 804 | ifnet_lock_done(new->rp_ifp); |
1c79356b A |
805 | |
806 | if (!new->rp_raf_auto) | |
807 | return 0; | |
808 | ||
809 | /* | |
810 | * Add an address for each interface id, if it is not yet | |
811 | * If it existed but not pointing to the prefix yet, | |
812 | * init the prefix pointer. | |
813 | */ | |
91447636 | 814 | lck_mtx_lock(prefix6_mutex); |
9bccf70c A |
815 | LIST_FOREACH(rap, &rpp->rp_addrhead, ra_entry) |
816 | { | |
1c79356b A |
817 | if (rap->ra_addr != NULL) { |
818 | if (rap->ra_addr->ia6_ifpr == NULL) | |
819 | rap->ra_addr->ia6_ifpr = rp2ifpr(rpp); | |
820 | continue; | |
821 | } | |
822 | add_each_addr(so, rpp, rap); | |
823 | } | |
91447636 | 824 | lck_mtx_unlock(prefix6_mutex); |
1c79356b A |
825 | return 0; |
826 | } | |
827 | ||
828 | static int | |
829 | add_each_prefix(struct socket *so, struct rr_prefix *rpp) | |
830 | { | |
831 | init_prefix_ltimes(rpp); | |
832 | return(rrpr_update(so, rpp)); | |
833 | } | |
834 | ||
835 | static void | |
836 | rp_remove(struct rr_prefix *rpp) | |
837 | { | |
1c79356b | 838 | |
1c79356b | 839 | /* unlink rp_entry from if_prefixlist */ |
91447636 | 840 | lck_mtx_lock(prefix6_mutex); |
1c79356b A |
841 | { |
842 | struct ifnet *ifp = rpp->rp_ifp; | |
843 | struct ifprefix *ifpr; | |
844 | ||
91447636 | 845 | ifnet_lock_exclusive(ifp); |
9bccf70c A |
846 | if ((ifpr = TAILQ_FIRST(&ifp->if_prefixhead)) == rp2ifpr(rpp)) |
847 | TAILQ_FIRST(&ifp->if_prefixhead) = | |
848 | TAILQ_NEXT(ifpr, ifpr_list); | |
1c79356b | 849 | else { |
9bccf70c A |
850 | while (TAILQ_NEXT(ifpr, ifpr_list) != NULL && |
851 | (TAILQ_NEXT(ifpr, ifpr_list) != rp2ifpr(rpp))) | |
852 | ifpr = TAILQ_NEXT(ifpr, ifpr_list); | |
853 | if (TAILQ_NEXT(ifpr, ifpr_list)) | |
854 | TAILQ_NEXT(ifpr, ifpr_list) = | |
855 | TAILQ_NEXT(rp2ifpr(rpp), ifpr_list); | |
856 | else | |
857 | printf("Couldn't unlink rr_prefix from ifp\n"); | |
1c79356b | 858 | } |
91447636 | 859 | ifnet_lock_done(ifp); |
1c79356b A |
860 | } |
861 | /* unlink rp_entry from rr_prefix list */ | |
862 | LIST_REMOVE(rpp, rp_entry); | |
91447636 | 863 | lck_mtx_unlock(prefix6_mutex); |
9bccf70c | 864 | FREE(rpp, M_IP6RR); |
1c79356b A |
865 | } |
866 | ||
867 | static int | |
868 | create_ra_entry(struct rp_addr **rapp) | |
869 | { | |
9bccf70c | 870 | *rapp = (struct rp_addr *)_MALLOC(sizeof(struct rp_addr), M_RR_ADDR, |
1c79356b A |
871 | M_NOWAIT); |
872 | if (*rapp == NULL) { | |
873 | log(LOG_ERR, "in6_prefix.c: init_newprefix:%d: ENOBUFS" | |
874 | "for rp_addr\n", __LINE__); | |
875 | return ENOBUFS; | |
876 | } | |
877 | bzero(*rapp, sizeof(*(*rapp))); | |
878 | ||
879 | return 0; | |
880 | } | |
881 | ||
882 | static int | |
883 | init_newprefix(struct in6_rrenumreq *irr, struct ifprefix *ifpr, | |
884 | struct rr_prefix *rpp) | |
885 | { | |
886 | struct rp_addr *orap; | |
887 | ||
888 | /* init rp */ | |
889 | bzero(rpp, sizeof(*rpp)); | |
890 | rpp->rp_type = IN6_PREFIX_RR; | |
891 | rpp->rp_ifp = ifpr->ifpr_ifp; | |
892 | rpp->rp_plen = ifpr->ifpr_plen; | |
893 | rpp->rp_prefix.sin6_len = sizeof(rpp->rp_prefix); | |
894 | rpp->rp_prefix.sin6_family = AF_INET6; | |
895 | bit_copy((char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3, | |
896 | (char *)&irr->irr_useprefix.sin6_addr, | |
897 | sizeof(irr->irr_useprefix.sin6_addr) << 3, | |
898 | 0, irr->irr_u_uselen); | |
899 | /* copy keeplen part if necessary as necessary len */ | |
900 | if (irr->irr_u_uselen < ifpr->ifpr_plen) | |
901 | bit_copy((char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3, | |
902 | (char *)IFPR_IN6(ifpr), sizeof(*IFPR_IN6(ifpr)) << 3, | |
903 | irr->irr_u_uselen, | |
904 | min(ifpr->ifpr_plen - irr->irr_u_uselen, | |
905 | irr->irr_u_keeplen)); | |
91447636 | 906 | lck_mtx_lock(prefix6_mutex); |
9bccf70c A |
907 | LIST_FOREACH(orap, &(ifpr2rp(ifpr)->rp_addrhead), ra_entry) |
908 | { | |
1c79356b A |
909 | struct rp_addr *rap; |
910 | int error = 0; | |
911 | ||
912 | if ((error = create_ra_entry(&rap)) != 0) | |
913 | return error; | |
914 | rap->ra_ifid = orap->ra_ifid; | |
915 | rap->ra_flags.anycast = (orap->ra_addr != NULL && | |
916 | (orap->ra_addr->ia6_flags & | |
917 | IN6_IFF_ANYCAST) != 0) ? 1 : 0; | |
918 | LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry); | |
919 | } | |
920 | rpp->rp_vltime = irr->irr_vltime; | |
921 | rpp->rp_pltime = irr->irr_pltime; | |
922 | rpp->rp_raf_onlink = irr->irr_raf_mask_onlink ? irr->irr_raf_onlink : | |
923 | ifpr2rp(ifpr)->rp_raf_onlink; | |
924 | rpp->rp_raf_auto = irr->irr_raf_mask_auto ? irr->irr_raf_auto : | |
925 | ifpr2rp(ifpr)->rp_raf_auto; | |
926 | /* Is some FlagMasks for rrf necessary? */ | |
927 | rpp->rp_rrf = irr->irr_rrf; | |
928 | rpp->rp_origin = irr->irr_origin; | |
91447636 | 929 | lck_mtx_unlock(prefix6_mutex); |
1c79356b A |
930 | |
931 | return 0; | |
932 | } | |
933 | ||
934 | static void | |
935 | free_rp_entries(struct rr_prefix *rpp) | |
936 | { | |
937 | /* | |
938 | * This func is only called with rpp on stack(not on list). | |
939 | * So no splnet() here | |
940 | */ | |
91447636 | 941 | lck_mtx_lock(prefix6_mutex); |
1c79356b | 942 | while (!LIST_EMPTY(&rpp->rp_addrhead)) |
1c79356b A |
943 | { |
944 | struct rp_addr *rap; | |
945 | ||
946 | rap = LIST_FIRST(&rpp->rp_addrhead); | |
947 | LIST_REMOVE(rap, ra_entry); | |
948 | if (rap->ra_addr) | |
9bccf70c A |
949 | ifafree(&rap->ra_addr->ia_ifa); |
950 | FREE(rap, M_RR_ADDR); | |
1c79356b | 951 | } |
91447636 | 952 | lck_mtx_unlock(prefix6_mutex); |
1c79356b A |
953 | } |
954 | ||
955 | static int | |
956 | add_useprefixes(struct socket *so, struct ifnet *ifp, | |
957 | struct in6_rrenumreq *irr) | |
958 | { | |
959 | struct ifprefix *ifpr, *nextifpr; | |
960 | struct rr_prefix rp; | |
961 | int error = 0; | |
962 | ||
963 | /* add prefixes to each of marked prefix */ | |
91447636 | 964 | ifnet_lock_exclusive(ifp); |
9bccf70c A |
965 | for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; ifpr = nextifpr) |
966 | { | |
967 | nextifpr = TAILQ_NEXT(ifpr, ifpr_list); | |
1c79356b A |
968 | if (ifpr->ifpr_prefix->sa_family != AF_INET6 || |
969 | ifpr->ifpr_type != IN6_PREFIX_RR) | |
970 | continue; | |
971 | if (ifpr2rp(ifpr)->rp_statef_addmark) { | |
972 | if ((error = init_newprefix(irr, ifpr, &rp)) != 0) | |
973 | break; | |
974 | error = add_each_prefix(so, &rp); | |
975 | } | |
976 | } | |
91447636 | 977 | ifnet_lock_done(ifp); |
1c79356b A |
978 | /* free each rp_addr entry */ |
979 | free_rp_entries(&rp); | |
980 | ||
981 | return error; | |
982 | } | |
983 | ||
984 | static void | |
985 | unprefer_prefix(struct rr_prefix *rpp) | |
986 | { | |
987 | struct rp_addr *rap; | |
91447636 | 988 | struct timeval timenow; |
1c79356b | 989 | |
91447636 A |
990 | getmicrotime(&timenow); |
991 | ||
992 | lck_mtx_lock(prefix6_mutex); | |
1c79356b A |
993 | for (rap = rpp->rp_addrhead.lh_first; rap != NULL; |
994 | rap = rap->ra_entry.le_next) { | |
995 | if (rap->ra_addr == NULL) | |
996 | continue; | |
91447636 | 997 | rap->ra_addr->ia6_lifetime.ia6t_preferred = timenow.tv_sec; |
1c79356b A |
998 | rap->ra_addr->ia6_lifetime.ia6t_pltime = 0; |
999 | } | |
91447636 A |
1000 | lck_mtx_unlock(prefix6_mutex); |
1001 | ||
1c79356b A |
1002 | } |
1003 | ||
1004 | int | |
1005 | delete_each_prefix(struct rr_prefix *rpp, u_char origin) | |
1006 | { | |
1007 | int error = 0; | |
1008 | ||
1009 | if (rpp->rp_origin > origin) | |
1010 | return(EPERM); | |
1011 | ||
91447636 | 1012 | lck_mtx_lock(prefix6_mutex); |
1c79356b A |
1013 | while (rpp->rp_addrhead.lh_first != NULL) { |
1014 | struct rp_addr *rap; | |
1015 | int s; | |
1016 | ||
1c79356b | 1017 | rap = LIST_FIRST(&rpp->rp_addrhead); |
9bccf70c | 1018 | if (rap == NULL) { |
1c79356b | 1019 | break; |
9bccf70c | 1020 | } |
1c79356b | 1021 | LIST_REMOVE(rap, ra_entry); |
1c79356b | 1022 | if (rap->ra_addr == NULL) { |
9bccf70c | 1023 | FREE(rap, M_RR_ADDR); |
1c79356b A |
1024 | continue; |
1025 | } | |
1026 | rap->ra_addr->ia6_ifpr = NULL; | |
1027 | ||
91447636 | 1028 | in6_purgeaddr(&rap->ra_addr->ia_ifa, 0); |
9bccf70c A |
1029 | ifafree(&rap->ra_addr->ia_ifa); |
1030 | FREE(rap, M_RR_ADDR); | |
1c79356b A |
1031 | } |
1032 | rp_remove(rpp); | |
91447636 | 1033 | lck_mtx_unlock(prefix6_mutex); |
1c79356b A |
1034 | |
1035 | return error; | |
1036 | } | |
1037 | ||
1038 | static void | |
1039 | delete_prefixes(struct ifnet *ifp, u_char origin) | |
1040 | { | |
1041 | struct ifprefix *ifpr, *nextifpr; | |
1042 | ||
1043 | /* delete prefixes marked as tobe deleted */ | |
91447636 | 1044 | ifnet_lock_exclusive(ifp); |
9bccf70c A |
1045 | for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; ifpr = nextifpr) |
1046 | { | |
1047 | nextifpr = TAILQ_NEXT(ifpr, ifpr_list); | |
1c79356b A |
1048 | if (ifpr->ifpr_prefix->sa_family != AF_INET6 || |
1049 | ifpr->ifpr_type != IN6_PREFIX_RR) | |
1050 | continue; | |
1051 | if (ifpr2rp(ifpr)->rp_statef_delmark) | |
1052 | (void)delete_each_prefix(ifpr2rp(ifpr), origin); | |
1053 | } | |
91447636 | 1054 | ifnet_lock_done(ifp); |
1c79356b A |
1055 | } |
1056 | ||
1057 | static int | |
1058 | link_stray_ia6s(struct rr_prefix *rpp) | |
1059 | { | |
1060 | struct ifaddr *ifa; | |
1061 | ||
1c79356b A |
1062 | for (ifa = rpp->rp_ifp->if_addrlist.tqh_first; ifa; |
1063 | ifa = ifa->ifa_list.tqe_next) | |
1c79356b A |
1064 | { |
1065 | struct rp_addr *rap; | |
1066 | struct rr_prefix *orpp; | |
1067 | int error = 0; | |
1068 | ||
1069 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
1070 | continue; | |
1071 | if (rpp->rp_plen > in6_matchlen(RP_IN6(rpp), IFA_IN6(ifa))) | |
1072 | continue; | |
1073 | ||
1074 | orpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr); | |
1075 | if (orpp != NULL) { | |
1076 | if (!in6_are_prefix_equal(RP_IN6(orpp), RP_IN6(rpp), | |
1077 | rpp->rp_plen)) | |
1078 | log(LOG_ERR, "in6_prefix.c: link_stray_ia6s:" | |
1079 | "addr %s/%d already linked to a prefix" | |
1080 | "and it matches also %s/%d\n", | |
1081 | ip6_sprintf(IFA_IN6(ifa)), orpp->rp_plen, | |
1082 | ip6_sprintf(RP_IN6(rpp)), | |
1083 | rpp->rp_plen); | |
1084 | continue; | |
1085 | } | |
9bccf70c | 1086 | if ((error = assign_ra_entry(rpp, |
1c79356b A |
1087 | (sizeof(rap->ra_ifid) << 3) - |
1088 | rpp->rp_plen, | |
1089 | (struct in6_ifaddr *)ifa)) != 0) | |
1090 | return error; | |
1091 | } | |
1092 | return 0; | |
1093 | } | |
1094 | ||
1095 | /* XXX assumes that permission is already checked by the caller */ | |
1096 | int | |
1097 | in6_prefix_ioctl(struct socket *so, u_long cmd, caddr_t data, | |
1098 | struct ifnet *ifp) | |
1099 | { | |
1100 | struct rr_prefix *rpp, rp_tmp; | |
1101 | struct rp_addr *rap; | |
1102 | struct in6_prefixreq *ipr = (struct in6_prefixreq *)data; | |
1103 | struct in6_rrenumreq *irr = (struct in6_rrenumreq *)data; | |
1104 | struct ifaddr *ifa; | |
1105 | int error = 0; | |
1106 | ||
1107 | /* | |
91447636 | 1108 | * Failsafe for erroneous address config program. |
1c79356b A |
1109 | * Let's hope rrenumd don't make a mistakes. |
1110 | */ | |
1111 | if (ipr->ipr_origin <= PR_ORIG_RA) | |
1112 | ipr->ipr_origin = PR_ORIG_STATIC; | |
1113 | ||
1114 | switch (cmd) { | |
1115 | case SIOCSGIFPREFIX_IN6: | |
1116 | delmark_global_prefixes(ifp, irr); | |
1117 | /* FALL THROUGH */ | |
1118 | case SIOCAIFPREFIX_IN6: | |
1119 | case SIOCCIFPREFIX_IN6: | |
1120 | /* check if preferred lifetime > valid lifetime */ | |
1121 | if (irr->irr_pltime > irr->irr_vltime) { | |
1122 | log(LOG_NOTICE, | |
1123 | "in6_prefix_ioctl: preferred lifetime" | |
1124 | "(%ld) is greater than valid lifetime(%ld)\n", | |
1125 | (u_long)irr->irr_pltime, (u_long)irr->irr_vltime); | |
1126 | error = EINVAL; | |
1127 | break; | |
1128 | } | |
1129 | if (mark_matched_prefixes(cmd, ifp, irr)) { | |
1130 | if (irr->irr_u_uselen != 0) | |
1131 | if ((error = add_useprefixes(so, ifp, irr)) | |
1132 | != 0) | |
1133 | goto failed; | |
1134 | if (cmd != SIOCAIFPREFIX_IN6) | |
1135 | delete_prefixes(ifp, irr->irr_origin); | |
1136 | } else | |
1137 | return (EADDRNOTAVAIL); | |
1138 | failed: | |
1139 | unmark_prefixes(ifp); | |
1140 | break; | |
1141 | case SIOCGIFPREFIX_IN6: | |
1142 | rpp = search_matched_prefix(ifp, ipr); | |
1143 | if (rpp == NULL || ifp != rpp->rp_ifp) | |
1144 | return (EADDRNOTAVAIL); | |
1145 | ||
1146 | ipr->ipr_origin = rpp->rp_origin; | |
1147 | ipr->ipr_plen = rpp->rp_plen; | |
1148 | ipr->ipr_vltime = rpp->rp_vltime; | |
1149 | ipr->ipr_pltime = rpp->rp_pltime; | |
1150 | ipr->ipr_flags = rpp->rp_flags; | |
1151 | ipr->ipr_prefix = rpp->rp_prefix; | |
1152 | ||
1153 | break; | |
1154 | case SIOCSIFPREFIX_IN6: | |
1155 | /* check if preferred lifetime > valid lifetime */ | |
1156 | if (ipr->ipr_pltime > ipr->ipr_vltime) { | |
1157 | log(LOG_NOTICE, | |
1158 | "in6_prefix_ioctl: preferred lifetime" | |
1159 | "(%ld) is greater than valid lifetime(%ld)\n", | |
1160 | (u_long)ipr->ipr_pltime, (u_long)ipr->ipr_vltime); | |
1161 | error = EINVAL; | |
1162 | break; | |
1163 | } | |
1164 | ||
1165 | /* init rp_tmp */ | |
1166 | bzero((caddr_t)&rp_tmp, sizeof(rp_tmp)); | |
1167 | rp_tmp.rp_ifp = ifp; | |
1168 | rp_tmp.rp_plen = ipr->ipr_plen; | |
1169 | rp_tmp.rp_prefix = ipr->ipr_prefix; | |
1170 | rp_tmp.rp_vltime = ipr->ipr_vltime; | |
1171 | rp_tmp.rp_pltime = ipr->ipr_pltime; | |
1172 | rp_tmp.rp_flags = ipr->ipr_flags; | |
1173 | rp_tmp.rp_origin = ipr->ipr_origin; | |
1174 | ||
1175 | /* create rp_addr entries, usually at least for lladdr */ | |
1176 | if ((error = link_stray_ia6s(&rp_tmp)) != 0) { | |
1177 | free_rp_entries(&rp_tmp); | |
1178 | break; | |
1179 | } | |
91447636 | 1180 | ifnet_lock_exclusive(ifp); |
1c79356b A |
1181 | for (ifa = ifp->if_addrlist.tqh_first; |
1182 | ifa; | |
1183 | ifa = ifa->ifa_list.tqe_next) | |
1c79356b A |
1184 | { |
1185 | if (ifa->ifa_addr == NULL) | |
1186 | continue; /* just for safety */ | |
1187 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
1188 | continue; | |
1189 | if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)) == 0) | |
1190 | continue; | |
1191 | ||
1192 | if ((error = create_ra_entry(&rap)) != 0) { | |
1193 | free_rp_entries(&rp_tmp); | |
1194 | goto bad; | |
1195 | } | |
1196 | /* copy interface id part */ | |
1197 | bit_copy((caddr_t)&rap->ra_ifid, | |
1198 | sizeof(rap->ra_ifid) << 3, | |
1199 | (caddr_t)IFA_IN6(ifa), | |
1200 | sizeof(*IFA_IN6(ifa)) << 3, | |
1201 | rp_tmp.rp_plen, | |
1202 | (sizeof(rap->ra_ifid) << 3) - rp_tmp.rp_plen); | |
1203 | /* insert into list */ | |
91447636 | 1204 | lck_mtx_lock(prefix6_mutex); |
1c79356b | 1205 | LIST_INSERT_HEAD(&rp_tmp.rp_addrhead, rap, ra_entry); |
91447636 | 1206 | lck_mtx_unlock(prefix6_mutex); |
1c79356b | 1207 | } |
91447636 | 1208 | ifnet_lock_done(ifp); |
1c79356b A |
1209 | |
1210 | error = add_each_prefix(so, &rp_tmp); | |
1211 | ||
1212 | /* free each rp_addr entry */ | |
1213 | free_rp_entries(&rp_tmp); | |
1214 | ||
1215 | break; | |
1216 | case SIOCDIFPREFIX_IN6: | |
1217 | rpp = search_matched_prefix(ifp, ipr); | |
1218 | if (rpp == NULL || ifp != rpp->rp_ifp) | |
1219 | return (EADDRNOTAVAIL); | |
1220 | ||
91447636 | 1221 | ifnet_lock_exclusive(ifp); |
1c79356b | 1222 | error = delete_each_prefix(rpp, ipr->ipr_origin); |
91447636 | 1223 | ifnet_lock_done(ifp); |
1c79356b A |
1224 | break; |
1225 | } | |
1226 | bad: | |
1227 | return error; | |
1228 | } | |
1229 | ||
1230 | void | |
1231 | in6_rr_timer(void *ignored_arg) | |
1232 | { | |
1c79356b | 1233 | struct rr_prefix *rpp; |
91447636 | 1234 | struct timeval timenow; |
1c79356b | 1235 | |
91447636 | 1236 | getmicrotime(&timenow); |
1c79356b | 1237 | |
1c79356b | 1238 | /* expire */ |
91447636 | 1239 | lck_mtx_lock(prefix6_mutex); |
1c79356b A |
1240 | rpp = LIST_FIRST(&rr_prefix); |
1241 | while (rpp) { | |
91447636 | 1242 | if (rpp->rp_expire && rpp->rp_expire < timenow.tv_sec) { |
1c79356b A |
1243 | struct rr_prefix *next_rpp; |
1244 | ||
1245 | next_rpp = LIST_NEXT(rpp, rp_entry); | |
1246 | delete_each_prefix(rpp, PR_ORIG_KERNEL); | |
1247 | rpp = next_rpp; | |
1248 | continue; | |
1249 | } | |
91447636 | 1250 | if (rpp->rp_preferred && rpp->rp_preferred < timenow.tv_sec) |
1c79356b A |
1251 | unprefer_prefix(rpp); |
1252 | rpp = LIST_NEXT(rpp, rp_entry); | |
1253 | } | |
91447636 A |
1254 | lck_mtx_unlock(prefix6_mutex); |
1255 | timeout(in6_rr_timer, (caddr_t)0, ip6_rr_prune * hz); | |
1c79356b | 1256 | } |