1 | /* $NetBSD: in6.c,v 1.222 2016/11/18 06:50:04 knakahara Exp $ */ |
2 | /* $KAME: in6.c,v 1.198 2001/07/18 09:12:38 itojun Exp $ */ |
3 | |
4 | /* |
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
6 | * All rights reserved. |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions |
10 | * are met: |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
16 | * 3. Neither the name of the project nor the names of its contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | * SUCH DAMAGE. |
31 | */ |
32 | |
33 | /* |
34 | * Copyright (c) 1982, 1986, 1991, 1993 |
35 | * The Regents of the University of California. All rights reserved. |
36 | * |
37 | * Redistribution and use in source and binary forms, with or without |
38 | * modification, are permitted provided that the following conditions |
39 | * are met: |
40 | * 1. Redistributions of source code must retain the above copyright |
41 | * notice, this list of conditions and the following disclaimer. |
42 | * 2. Redistributions in binary form must reproduce the above copyright |
43 | * notice, this list of conditions and the following disclaimer in the |
44 | * documentation and/or other materials provided with the distribution. |
45 | * 3. Neither the name of the University nor the names of its contributors |
46 | * may be used to endorse or promote products derived from this software |
47 | * without specific prior written permission. |
48 | * |
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
59 | * SUCH DAMAGE. |
60 | * |
61 | * @(#)in.c 8.2 (Berkeley) 11/15/93 |
62 | */ |
63 | |
64 | #include <sys/cdefs.h> |
65 | __KERNEL_RCSID(0, "$NetBSD: in6.c,v 1.222 2016/11/18 06:50:04 knakahara Exp $" ); |
66 | |
67 | #ifdef _KERNEL_OPT |
68 | #include "opt_inet.h" |
69 | #include "opt_compat_netbsd.h" |
70 | #include "opt_net_mpsafe.h" |
71 | #endif |
72 | |
73 | #include <sys/param.h> |
74 | #include <sys/ioctl.h> |
75 | #include <sys/errno.h> |
76 | #include <sys/malloc.h> |
77 | #include <sys/socket.h> |
78 | #include <sys/socketvar.h> |
79 | #include <sys/sockio.h> |
80 | #include <sys/systm.h> |
81 | #include <sys/proc.h> |
82 | #include <sys/time.h> |
83 | #include <sys/kernel.h> |
84 | #include <sys/syslog.h> |
85 | #include <sys/kauth.h> |
86 | #include <sys/cprng.h> |
87 | #include <sys/kmem.h> |
88 | |
89 | #include <net/if.h> |
90 | #include <net/if_types.h> |
91 | #include <net/if_llatbl.h> |
92 | #include <net/if_ether.h> |
93 | #include <net/if_dl.h> |
94 | #include <net/pfil.h> |
95 | #include <net/route.h> |
96 | |
97 | #include <netinet/in.h> |
98 | #include <netinet/in_var.h> |
99 | |
100 | #include <netinet/ip6.h> |
101 | #include <netinet6/ip6_var.h> |
102 | #include <netinet6/nd6.h> |
103 | #include <netinet6/mld6_var.h> |
104 | #include <netinet6/ip6_mroute.h> |
105 | #include <netinet6/in6_ifattach.h> |
106 | #include <netinet6/scope6_var.h> |
107 | |
108 | #include <net/net_osdep.h> |
109 | |
110 | #ifdef COMPAT_50 |
111 | #include <compat/netinet6/in6_var.h> |
112 | #endif |
113 | |
114 | MALLOC_DEFINE(M_IP6OPT, "ip6_options" , "IPv6 options" ); |
115 | |
116 | /* enable backward compatibility code for obsoleted ioctls */ |
117 | #define COMPAT_IN6IFIOCTL |
118 | |
119 | #ifdef IN6_DEBUG |
120 | #define IN6_DPRINTF(__fmt, ...) printf(__fmt, __VA_ARGS__) |
121 | #else |
122 | #define IN6_DPRINTF(__fmt, ...) do { } while (/*CONSTCOND*/0) |
123 | #endif /* IN6_DEBUG */ |
124 | |
125 | /* |
126 | * Definitions of some constant IP6 addresses. |
127 | */ |
128 | const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; |
129 | const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; |
130 | const struct in6_addr in6addr_nodelocal_allnodes = |
131 | IN6ADDR_NODELOCAL_ALLNODES_INIT; |
132 | const struct in6_addr in6addr_linklocal_allnodes = |
133 | IN6ADDR_LINKLOCAL_ALLNODES_INIT; |
134 | const struct in6_addr in6addr_linklocal_allrouters = |
135 | IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; |
136 | |
137 | const struct in6_addr in6mask0 = IN6MASK0; |
138 | const struct in6_addr in6mask32 = IN6MASK32; |
139 | const struct in6_addr in6mask64 = IN6MASK64; |
140 | const struct in6_addr in6mask96 = IN6MASK96; |
141 | const struct in6_addr in6mask128 = IN6MASK128; |
142 | |
143 | const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6, |
144 | 0, 0, IN6ADDR_ANY_INIT, 0}; |
145 | |
146 | struct pslist_head in6_ifaddr_list; |
147 | kmutex_t in6_ifaddr_lock; |
148 | |
149 | static int in6_lifaddr_ioctl(struct socket *, u_long, void *, |
150 | struct ifnet *); |
151 | static int in6_ifaddprefix(struct in6_ifaddr *); |
152 | static int in6_ifremprefix(struct in6_ifaddr *); |
153 | static int in6_ifinit(struct ifnet *, struct in6_ifaddr *, |
154 | const struct sockaddr_in6 *, int); |
155 | static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *); |
156 | |
157 | void |
158 | in6_init(void) |
159 | { |
160 | |
161 | PSLIST_INIT(&in6_ifaddr_list); |
162 | mutex_init(&in6_ifaddr_lock, MUTEX_DEFAULT, IPL_NONE); |
163 | } |
164 | |
165 | /* |
166 | * Add ownaddr as loopback rtentry. We previously add the route only if |
167 | * necessary (ex. on a p2p link). However, since we now manage addresses |
168 | * separately from prefixes, we should always add the route. We can't |
169 | * rely on the cloning mechanism from the corresponding interface route |
170 | * any more. |
171 | */ |
172 | void |
173 | in6_ifaddlocal(struct ifaddr *ifa) |
174 | { |
175 | |
176 | if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &in6addr_any) || |
177 | (ifa->ifa_ifp->if_flags & IFF_POINTOPOINT && |
178 | IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), IFA_DSTIN6(ifa)))) |
179 | { |
180 | rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); |
181 | return; |
182 | } |
183 | |
184 | rt_ifa_addlocal(ifa); |
185 | } |
186 | |
187 | /* |
188 | * Remove loopback rtentry of ownaddr generated by in6_ifaddlocal(), |
189 | * if it exists. |
190 | */ |
191 | void |
192 | in6_ifremlocal(struct ifaddr *ifa) |
193 | { |
194 | struct in6_ifaddr *ia; |
195 | struct ifaddr *alt_ifa = NULL; |
196 | int ia_count = 0; |
197 | struct psref psref; |
198 | int s; |
199 | |
200 | /* |
201 | * Some of BSD variants do not remove cloned routes |
202 | * from an interface direct route, when removing the direct route |
203 | * (see comments in net/net_osdep.h). Even for variants that do remove |
204 | * cloned routes, they could fail to remove the cloned routes when |
205 | * we handle multple addresses that share a common prefix. |
206 | * So, we should remove the route corresponding to the deleted address. |
207 | */ |
208 | |
209 | /* |
210 | * Delete the entry only if exactly one ifaddr matches the |
211 | * address, ifa->ifa_addr. |
212 | * |
213 | * If more than one ifaddr matches, replace the ifaddr in |
214 | * the routing table, rt_ifa, with a different ifaddr than |
215 | * the one we are purging, ifa. It is important to do |
216 | * this, or else the routing table can accumulate dangling |
217 | * pointers rt->rt_ifa->ifa_ifp to destroyed interfaces, |
218 | * which will lead to crashes, later. (More than one ifaddr |
219 | * can match if we assign the same address to multiple---probably |
220 | * p2p---interfaces.) |
221 | * |
222 | * XXX An old comment at this place said, "we should avoid |
223 | * XXX such a configuration [i.e., interfaces with the same |
224 | * XXX addressed assigned --ed.] in IPv6...". I do not |
225 | * XXX agree, especially now that I have fixed the dangling |
226 | * XXX ifp-pointers bug. |
227 | */ |
228 | s = pserialize_read_enter(); |
229 | IN6_ADDRLIST_READER_FOREACH(ia) { |
230 | if (!IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) |
231 | continue; |
232 | if (ia->ia_ifp != ifa->ifa_ifp) |
233 | alt_ifa = &ia->ia_ifa; |
234 | if (++ia_count > 1 && alt_ifa != NULL) |
235 | break; |
236 | } |
237 | if (ia_count > 1 && alt_ifa != NULL) |
238 | ifa_acquire(alt_ifa, &psref); |
239 | pserialize_read_exit(s); |
240 | |
241 | if (ia_count == 0) |
242 | return; |
243 | |
244 | rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa); |
245 | |
246 | if (ia_count > 1 && alt_ifa != NULL) |
247 | ifa_release(alt_ifa, &psref); |
248 | } |
249 | |
250 | /* Add prefix route for the network. */ |
251 | static int |
252 | in6_ifaddprefix(struct in6_ifaddr *ia) |
253 | { |
254 | int error, flags = 0; |
255 | |
256 | if (in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) == 128) { |
257 | if (ia->ia_dstaddr.sin6_family != AF_INET6) |
258 | /* We don't need to install a host route. */ |
259 | return 0; |
260 | flags |= RTF_HOST; |
261 | } |
262 | |
263 | /* Is this a connected route for neighbour discovery? */ |
264 | if (nd6_need_cache(ia->ia_ifp)) |
265 | flags |= RTF_CONNECTED; |
266 | |
267 | if ((error = rtinit(&ia->ia_ifa, RTM_ADD, RTF_UP | flags)) == 0) |
268 | ia->ia_flags |= IFA_ROUTE; |
269 | else if (error == EEXIST) |
270 | /* Existance of the route is not an error. */ |
271 | error = 0; |
272 | |
273 | return error; |
274 | } |
275 | |
276 | /* Delete network prefix route if present. |
277 | * Re-add it to another address if the prefix matches. */ |
278 | static int |
279 | in6_ifremprefix(struct in6_ifaddr *target) |
280 | { |
281 | int error, s; |
282 | struct in6_ifaddr *ia; |
283 | |
284 | if ((target->ia_flags & IFA_ROUTE) == 0) |
285 | return 0; |
286 | |
287 | s = pserialize_read_enter(); |
288 | IN6_ADDRLIST_READER_FOREACH(ia) { |
289 | if (target->ia_dstaddr.sin6_len) { |
290 | if (ia->ia_dstaddr.sin6_len == 0 || |
291 | !IN6_ARE_ADDR_EQUAL(&ia->ia_dstaddr.sin6_addr, |
292 | &target->ia_dstaddr.sin6_addr)) |
293 | continue; |
294 | } else { |
295 | if (!IN6_ARE_MASKED_ADDR_EQUAL(&ia->ia_addr.sin6_addr, |
296 | &target->ia_addr.sin6_addr, |
297 | &target->ia_prefixmask.sin6_addr)) |
298 | continue; |
299 | } |
300 | |
301 | /* |
302 | * if we got a matching prefix route, move IFA_ROUTE to him |
303 | */ |
304 | if ((ia->ia_flags & IFA_ROUTE) == 0) { |
305 | struct psref psref; |
306 | int bound = curlwp_bind(); |
307 | |
308 | ia6_acquire(ia, &psref); |
309 | pserialize_read_exit(s); |
310 | |
311 | rtinit(&target->ia_ifa, RTM_DELETE, 0); |
312 | target->ia_flags &= ~IFA_ROUTE; |
313 | |
314 | error = in6_ifaddprefix(ia); |
315 | |
316 | ia6_release(ia, &psref); |
317 | curlwp_bindx(bound); |
318 | |
319 | return error; |
320 | } |
321 | } |
322 | pserialize_read_exit(s); |
323 | |
324 | /* |
325 | * noone seem to have prefix route. remove it. |
326 | */ |
327 | rtinit(&target->ia_ifa, RTM_DELETE, 0); |
328 | target->ia_flags &= ~IFA_ROUTE; |
329 | return 0; |
330 | } |
331 | |
332 | int |
333 | in6_mask2len(struct in6_addr *mask, u_char *lim0) |
334 | { |
335 | int x = 0, y; |
336 | u_char *lim = lim0, *p; |
337 | |
338 | /* ignore the scope_id part */ |
339 | if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask)) |
340 | lim = (u_char *)mask + sizeof(*mask); |
341 | for (p = (u_char *)mask; p < lim; x++, p++) { |
342 | if (*p != 0xff) |
343 | break; |
344 | } |
345 | y = 0; |
346 | if (p < lim) { |
347 | for (y = 0; y < NBBY; y++) { |
348 | if ((*p & (0x80 >> y)) == 0) |
349 | break; |
350 | } |
351 | } |
352 | |
353 | /* |
354 | * when the limit pointer is given, do a stricter check on the |
355 | * remaining bits. |
356 | */ |
357 | if (p < lim) { |
358 | if (y != 0 && (*p & (0x00ff >> y)) != 0) |
359 | return -1; |
360 | for (p = p + 1; p < lim; p++) |
361 | if (*p != 0) |
362 | return -1; |
363 | } |
364 | |
365 | return x * NBBY + y; |
366 | } |
367 | |
368 | #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) |
369 | #define ia62ifa(ia6) (&((ia6)->ia_ifa)) |
370 | |
371 | static int |
372 | in6_control1(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) |
373 | { |
374 | struct in6_ifreq *ifr = (struct in6_ifreq *)data; |
375 | struct in6_ifaddr *ia = NULL; |
376 | struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; |
377 | struct sockaddr_in6 *sa6; |
378 | int error, bound; |
379 | struct psref psref; |
380 | bool run_hooks = false; |
381 | |
382 | switch (cmd) { |
383 | case SIOCAADDRCTL_POLICY: |
384 | case SIOCDADDRCTL_POLICY: |
385 | /* Privileged. */ |
386 | return in6_src_ioctl(cmd, data); |
387 | /* |
388 | * XXX: Fix me, once we fix SIOCSIFADDR, SIOCIFDSTADDR, etc. |
389 | */ |
390 | case SIOCSIFADDR: |
391 | case SIOCSIFDSTADDR: |
392 | case SIOCSIFBRDADDR: |
393 | case SIOCSIFNETMASK: |
394 | return EOPNOTSUPP; |
395 | case SIOCGETSGCNT_IN6: |
396 | case SIOCGETMIFCNT_IN6: |
397 | return mrt6_ioctl(cmd, data); |
398 | case SIOCGIFADDRPREF: |
399 | case SIOCSIFADDRPREF: |
400 | if (ifp == NULL) |
401 | return EINVAL; |
402 | return ifaddrpref_ioctl(so, cmd, data, ifp); |
403 | } |
404 | |
405 | if (ifp == NULL) |
406 | return EOPNOTSUPP; |
407 | |
408 | switch (cmd) { |
409 | case SIOCSNDFLUSH_IN6: |
410 | case SIOCSPFXFLUSH_IN6: |
411 | case SIOCSRTRFLUSH_IN6: |
412 | case SIOCSDEFIFACE_IN6: |
413 | case SIOCSIFINFO_FLAGS: |
414 | case SIOCSIFINFO_IN6: |
415 | /* Privileged. */ |
416 | /* FALLTHROUGH */ |
417 | case OSIOCGIFINFO_IN6: |
418 | case SIOCGIFINFO_IN6: |
419 | case SIOCGDRLST_IN6: |
420 | case SIOCGPRLST_IN6: |
421 | case SIOCGNBRINFO_IN6: |
422 | case SIOCGDEFIFACE_IN6: |
423 | return nd6_ioctl(cmd, data, ifp); |
424 | } |
425 | |
426 | switch (cmd) { |
427 | case SIOCSIFPREFIX_IN6: |
428 | case SIOCDIFPREFIX_IN6: |
429 | case SIOCAIFPREFIX_IN6: |
430 | case SIOCCIFPREFIX_IN6: |
431 | case SIOCSGIFPREFIX_IN6: |
432 | case SIOCGIFPREFIX_IN6: |
433 | log(LOG_NOTICE, |
434 | "prefix ioctls are now invalidated. " |
435 | "please use ifconfig.\n" ); |
436 | return EOPNOTSUPP; |
437 | } |
438 | |
439 | switch (cmd) { |
440 | case SIOCALIFADDR: |
441 | case SIOCDLIFADDR: |
442 | /* Privileged. */ |
443 | /* FALLTHROUGH */ |
444 | case SIOCGLIFADDR: |
445 | return in6_lifaddr_ioctl(so, cmd, data, ifp); |
446 | } |
447 | |
448 | /* |
449 | * Find address for this interface, if it exists. |
450 | * |
451 | * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation |
452 | * only, and used the first interface address as the target of other |
453 | * operations (without checking ifra_addr). This was because netinet |
454 | * code/API assumed at most 1 interface address per interface. |
455 | * Since IPv6 allows a node to assign multiple addresses |
456 | * on a single interface, we almost always look and check the |
457 | * presence of ifra_addr, and reject invalid ones here. |
458 | * It also decreases duplicated code among SIOC*_IN6 operations. |
459 | */ |
460 | switch (cmd) { |
461 | case SIOCAIFADDR_IN6: |
462 | #ifdef OSIOCAIFADDR_IN6 |
463 | case OSIOCAIFADDR_IN6: |
464 | #endif |
465 | #ifdef OSIOCSIFPHYADDR_IN6 |
466 | case OSIOCSIFPHYADDR_IN6: |
467 | #endif |
468 | case SIOCSIFPHYADDR_IN6: |
469 | sa6 = &ifra->ifra_addr; |
470 | break; |
471 | case SIOCSIFADDR_IN6: |
472 | case SIOCGIFADDR_IN6: |
473 | case SIOCSIFDSTADDR_IN6: |
474 | case SIOCSIFNETMASK_IN6: |
475 | case SIOCGIFDSTADDR_IN6: |
476 | case SIOCGIFNETMASK_IN6: |
477 | case SIOCDIFADDR_IN6: |
478 | case SIOCGIFPSRCADDR_IN6: |
479 | case SIOCGIFPDSTADDR_IN6: |
480 | case SIOCGIFAFLAG_IN6: |
481 | case SIOCSNDFLUSH_IN6: |
482 | case SIOCSPFXFLUSH_IN6: |
483 | case SIOCSRTRFLUSH_IN6: |
484 | case SIOCGIFALIFETIME_IN6: |
485 | #ifdef OSIOCGIFALIFETIME_IN6 |
486 | case OSIOCGIFALIFETIME_IN6: |
487 | #endif |
488 | case SIOCGIFSTAT_IN6: |
489 | case SIOCGIFSTAT_ICMP6: |
490 | sa6 = &ifr->ifr_addr; |
491 | break; |
492 | default: |
493 | sa6 = NULL; |
494 | break; |
495 | } |
496 | |
497 | error = 0; |
498 | bound = curlwp_bind(); |
499 | if (sa6 && sa6->sin6_family == AF_INET6) { |
500 | if (sa6->sin6_scope_id != 0) |
501 | error = sa6_embedscope(sa6, 0); |
502 | else |
503 | error = in6_setscope(&sa6->sin6_addr, ifp, NULL); |
504 | if (error != 0) |
505 | goto out; |
506 | ia = in6ifa_ifpwithaddr_psref(ifp, &sa6->sin6_addr, &psref); |
507 | } else |
508 | ia = NULL; |
509 | |
510 | switch (cmd) { |
511 | case SIOCSIFADDR_IN6: |
512 | case SIOCSIFDSTADDR_IN6: |
513 | case SIOCSIFNETMASK_IN6: |
514 | /* |
515 | * Since IPv6 allows a node to assign multiple addresses |
516 | * on a single interface, SIOCSIFxxx ioctls are deprecated. |
517 | */ |
518 | error = EINVAL; |
519 | goto release; |
520 | |
521 | case SIOCDIFADDR_IN6: |
522 | /* |
523 | * for IPv4, we look for existing in_ifaddr here to allow |
524 | * "ifconfig if0 delete" to remove the first IPv4 address on |
525 | * the interface. For IPv6, as the spec allows multiple |
526 | * interface address from the day one, we consider "remove the |
527 | * first one" semantics to be not preferable. |
528 | */ |
529 | if (ia == NULL) { |
530 | error = EADDRNOTAVAIL; |
531 | goto out; |
532 | } |
533 | /* FALLTHROUGH */ |
534 | #ifdef OSIOCAIFADDR_IN6 |
535 | case OSIOCAIFADDR_IN6: |
536 | #endif |
537 | case SIOCAIFADDR_IN6: |
538 | /* |
539 | * We always require users to specify a valid IPv6 address for |
540 | * the corresponding operation. |
541 | */ |
542 | if (ifra->ifra_addr.sin6_family != AF_INET6 || |
543 | ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) { |
544 | error = EAFNOSUPPORT; |
545 | goto release; |
546 | } |
547 | /* Privileged. */ |
548 | |
549 | break; |
550 | |
551 | case SIOCGIFADDR_IN6: |
552 | /* This interface is basically deprecated. use SIOCGIFCONF. */ |
553 | /* FALLTHROUGH */ |
554 | case SIOCGIFAFLAG_IN6: |
555 | case SIOCGIFNETMASK_IN6: |
556 | case SIOCGIFDSTADDR_IN6: |
557 | case SIOCGIFALIFETIME_IN6: |
558 | #ifdef OSIOCGIFALIFETIME_IN6 |
559 | case OSIOCGIFALIFETIME_IN6: |
560 | #endif |
561 | /* must think again about its semantics */ |
562 | if (ia == NULL) { |
563 | error = EADDRNOTAVAIL; |
564 | goto out; |
565 | } |
566 | break; |
567 | } |
568 | |
569 | switch (cmd) { |
570 | |
571 | case SIOCGIFADDR_IN6: |
572 | ifr->ifr_addr = ia->ia_addr; |
573 | error = sa6_recoverscope(&ifr->ifr_addr); |
574 | break; |
575 | |
576 | case SIOCGIFDSTADDR_IN6: |
577 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { |
578 | error = EINVAL; |
579 | break; |
580 | } |
581 | /* |
582 | * XXX: should we check if ifa_dstaddr is NULL and return |
583 | * an error? |
584 | */ |
585 | ifr->ifr_dstaddr = ia->ia_dstaddr; |
586 | error = sa6_recoverscope(&ifr->ifr_dstaddr); |
587 | break; |
588 | |
589 | case SIOCGIFNETMASK_IN6: |
590 | ifr->ifr_addr = ia->ia_prefixmask; |
591 | break; |
592 | |
593 | case SIOCGIFAFLAG_IN6: |
594 | ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; |
595 | break; |
596 | |
597 | case SIOCGIFSTAT_IN6: |
598 | if (ifp == NULL) { |
599 | error = EINVAL; |
600 | break; |
601 | } |
602 | memset(&ifr->ifr_ifru.ifru_stat, 0, |
603 | sizeof(ifr->ifr_ifru.ifru_stat)); |
604 | ifr->ifr_ifru.ifru_stat = |
605 | *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat; |
606 | break; |
607 | |
608 | case SIOCGIFSTAT_ICMP6: |
609 | if (ifp == NULL) { |
610 | error = EINVAL; |
611 | break; |
612 | } |
613 | memset(&ifr->ifr_ifru.ifru_icmp6stat, 0, |
614 | sizeof(ifr->ifr_ifru.ifru_icmp6stat)); |
615 | ifr->ifr_ifru.ifru_icmp6stat = |
616 | *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat; |
617 | break; |
618 | |
619 | #ifdef OSIOCGIFALIFETIME_IN6 |
620 | case OSIOCGIFALIFETIME_IN6: |
621 | #endif |
622 | case SIOCGIFALIFETIME_IN6: |
623 | ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; |
624 | if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { |
625 | time_t maxexpire; |
626 | struct in6_addrlifetime *retlt = |
627 | &ifr->ifr_ifru.ifru_lifetime; |
628 | |
629 | /* |
630 | * XXX: adjust expiration time assuming time_t is |
631 | * signed. |
632 | */ |
633 | maxexpire = ((time_t)~0) & |
634 | ~((time_t)1 << ((sizeof(maxexpire) * NBBY) - 1)); |
635 | if (ia->ia6_lifetime.ia6t_vltime < |
636 | maxexpire - ia->ia6_updatetime) { |
637 | retlt->ia6t_expire = ia->ia6_updatetime + |
638 | ia->ia6_lifetime.ia6t_vltime; |
639 | retlt->ia6t_expire = retlt->ia6t_expire ? |
640 | time_mono_to_wall(retlt->ia6t_expire) : |
641 | 0; |
642 | } else |
643 | retlt->ia6t_expire = maxexpire; |
644 | } |
645 | if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { |
646 | time_t maxexpire; |
647 | struct in6_addrlifetime *retlt = |
648 | &ifr->ifr_ifru.ifru_lifetime; |
649 | |
650 | /* |
651 | * XXX: adjust expiration time assuming time_t is |
652 | * signed. |
653 | */ |
654 | maxexpire = ((time_t)~0) & |
655 | ~((time_t)1 << ((sizeof(maxexpire) * NBBY) - 1)); |
656 | if (ia->ia6_lifetime.ia6t_pltime < |
657 | maxexpire - ia->ia6_updatetime) { |
658 | retlt->ia6t_preferred = ia->ia6_updatetime + |
659 | ia->ia6_lifetime.ia6t_pltime; |
660 | retlt->ia6t_preferred = retlt->ia6t_preferred ? |
661 | time_mono_to_wall(retlt->ia6t_preferred) : |
662 | 0; |
663 | } else |
664 | retlt->ia6t_preferred = maxexpire; |
665 | } |
666 | #ifdef OSIOCFIFALIFETIME_IN6 |
667 | if (cmd == OSIOCFIFALIFETIME_IN6) |
668 | in6_addrlifetime_to_in6_addrlifetime50( |
669 | &ifr->ifru.ifru_lifetime); |
670 | #endif |
671 | break; |
672 | |
673 | #ifdef OSIOCAIFADDR_IN6 |
674 | case OSIOCAIFADDR_IN6: |
675 | in6_aliasreq50_to_in6_aliasreq(ifra); |
676 | /*FALLTHROUGH*/ |
677 | #endif |
678 | case SIOCAIFADDR_IN6: |
679 | { |
680 | struct in6_addrlifetime *lt; |
681 | |
682 | /* reject read-only flags */ |
683 | if ((ifra->ifra_flags & IN6_IFF_DUPLICATED) != 0 || |
684 | (ifra->ifra_flags & IN6_IFF_DETACHED) != 0 || |
685 | (ifra->ifra_flags & IN6_IFF_TENTATIVE) != 0 || |
686 | (ifra->ifra_flags & IN6_IFF_NODAD) != 0 || |
687 | (ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0) { |
688 | error = EINVAL; |
689 | break; |
690 | } |
691 | /* |
692 | * ia6t_expire and ia6t_preferred won't be used for now, |
693 | * so just in case. |
694 | */ |
695 | lt = &ifra->ifra_lifetime; |
696 | if (lt->ia6t_expire != 0) |
697 | lt->ia6t_expire = time_wall_to_mono(lt->ia6t_expire); |
698 | if (lt->ia6t_preferred != 0) |
699 | lt->ia6t_preferred = |
700 | time_wall_to_mono(lt->ia6t_preferred); |
701 | /* |
702 | * make (ia == NULL) or update (ia != NULL) the interface |
703 | * address structure, and link it to the list. |
704 | */ |
705 | if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0) |
706 | break; |
707 | run_hooks = true; |
708 | break; |
709 | } |
710 | |
711 | case SIOCDIFADDR_IN6: |
712 | { |
713 | struct nd_prefix *pr; |
714 | |
715 | /* |
716 | * If the address being deleted is the only one that owns |
717 | * the corresponding prefix, expire the prefix as well. |
718 | * Note that in6_purgeaddr() will decrement ndpr_refcnt. |
719 | */ |
720 | pr = ia->ia6_ndpr; |
721 | ia6_release(ia, &psref); |
722 | in6_purgeaddr(&ia->ia_ifa); |
723 | ia = NULL; |
724 | if (pr && pr->ndpr_refcnt == 0) |
725 | prelist_remove(pr); |
726 | run_hooks = true; |
727 | break; |
728 | } |
729 | |
730 | default: |
731 | error = ENOTTY; |
732 | } |
733 | release: |
734 | ia6_release(ia, &psref); |
735 | |
736 | if (run_hooks) |
737 | pfil_run_hooks(if_pfil, (struct mbuf **)cmd, ifp, PFIL_IFADDR); |
738 | out: |
739 | curlwp_bindx(bound); |
740 | return error; |
741 | } |
742 | |
743 | int |
744 | in6_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) |
745 | { |
746 | int error, s; |
747 | |
748 | switch (cmd) { |
749 | case SIOCSNDFLUSH_IN6: |
750 | case SIOCSPFXFLUSH_IN6: |
751 | case SIOCSRTRFLUSH_IN6: |
752 | case SIOCSDEFIFACE_IN6: |
753 | case SIOCSIFINFO_FLAGS: |
754 | case SIOCSIFINFO_IN6: |
755 | |
756 | case SIOCALIFADDR: |
757 | case SIOCDLIFADDR: |
758 | |
759 | case SIOCDIFADDR_IN6: |
760 | #ifdef OSIOCAIFADDR_IN6 |
761 | case OSIOCAIFADDR_IN6: |
762 | #endif |
763 | case SIOCAIFADDR_IN6: |
764 | |
765 | case SIOCAADDRCTL_POLICY: |
766 | case SIOCDADDRCTL_POLICY: |
767 | |
768 | if (kauth_authorize_network(curlwp->l_cred, |
769 | KAUTH_NETWORK_SOCKET, |
770 | KAUTH_REQ_NETWORK_SOCKET_SETPRIV, |
771 | so, NULL, NULL)) |
772 | return EPERM; |
773 | break; |
774 | } |
775 | |
776 | s = splnet(); |
777 | #ifndef NET_MPSAFE |
778 | mutex_enter(softnet_lock); |
779 | #endif |
780 | error = in6_control1(so , cmd, data, ifp); |
781 | #ifndef NET_MPSAFE |
782 | mutex_exit(softnet_lock); |
783 | #endif |
784 | splx(s); |
785 | return error; |
786 | } |
787 | |
788 | /* |
789 | * Update parameters of an IPv6 interface address. |
790 | * If necessary, a new entry is created and linked into address chains. |
791 | * This function is separated from in6_control(). |
792 | * XXX: should this be performed under splnet()? |
793 | */ |
794 | static int |
795 | in6_update_ifa1(struct ifnet *ifp, struct in6_aliasreq *ifra, |
796 | struct in6_ifaddr *ia, int flags) |
797 | { |
798 | int error = 0, hostIsNew = 0, plen = -1; |
799 | struct sockaddr_in6 dst6; |
800 | struct in6_addrlifetime *lt; |
801 | struct in6_multi_mship *imm; |
802 | struct in6_multi *in6m_sol; |
803 | struct rtentry *rt; |
804 | int dad_delay, was_tentative; |
805 | |
806 | in6m_sol = NULL; |
807 | |
808 | /* Validate parameters */ |
809 | if (ifp == NULL || ifra == NULL) /* this maybe redundant */ |
810 | return EINVAL; |
811 | |
812 | /* |
813 | * The destination address for a p2p link must have a family |
814 | * of AF_UNSPEC or AF_INET6. |
815 | */ |
816 | if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && |
817 | ifra->ifra_dstaddr.sin6_family != AF_INET6 && |
818 | ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) |
819 | return EAFNOSUPPORT; |
820 | /* |
821 | * validate ifra_prefixmask. don't check sin6_family, netmask |
822 | * does not carry fields other than sin6_len. |
823 | */ |
824 | if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) |
825 | return EINVAL; |
826 | /* |
827 | * Because the IPv6 address architecture is classless, we require |
828 | * users to specify a (non 0) prefix length (mask) for a new address. |
829 | * We also require the prefix (when specified) mask is valid, and thus |
830 | * reject a non-consecutive mask. |
831 | */ |
832 | if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) |
833 | return EINVAL; |
834 | if (ifra->ifra_prefixmask.sin6_len != 0) { |
835 | plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, |
836 | (u_char *)&ifra->ifra_prefixmask + |
837 | ifra->ifra_prefixmask.sin6_len); |
838 | if (plen <= 0) |
839 | return EINVAL; |
840 | } else { |
841 | /* |
842 | * In this case, ia must not be NULL. We just use its prefix |
843 | * length. |
844 | */ |
845 | plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); |
846 | } |
847 | /* |
848 | * If the destination address on a p2p interface is specified, |
849 | * and the address is a scoped one, validate/set the scope |
850 | * zone identifier. |
851 | */ |
852 | dst6 = ifra->ifra_dstaddr; |
853 | if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 && |
854 | (dst6.sin6_family == AF_INET6)) { |
855 | struct in6_addr in6_tmp; |
856 | u_int32_t zoneid; |
857 | |
858 | in6_tmp = dst6.sin6_addr; |
859 | if (in6_setscope(&in6_tmp, ifp, &zoneid)) |
860 | return EINVAL; /* XXX: should be impossible */ |
861 | |
862 | if (dst6.sin6_scope_id != 0) { |
863 | if (dst6.sin6_scope_id != zoneid) |
864 | return EINVAL; |
865 | } else /* user omit to specify the ID. */ |
866 | dst6.sin6_scope_id = zoneid; |
867 | |
868 | /* convert into the internal form */ |
869 | if (sa6_embedscope(&dst6, 0)) |
870 | return EINVAL; /* XXX: should be impossible */ |
871 | } |
872 | /* |
873 | * The destination address can be specified only for a p2p or a |
874 | * loopback interface. If specified, the corresponding prefix length |
875 | * must be 128. |
876 | */ |
877 | if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { |
878 | #ifdef FORCE_P2PPLEN |
879 | int i; |
880 | #endif |
881 | |
882 | if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) { |
883 | /* XXX: noisy message */ |
884 | nd6log(LOG_INFO, "a destination can " |
885 | "be specified for a p2p or a loopback IF only\n" ); |
886 | return EINVAL; |
887 | } |
888 | if (plen != 128) { |
889 | nd6log(LOG_INFO, "prefixlen should " |
890 | "be 128 when dstaddr is specified\n" ); |
891 | #ifdef FORCE_P2PPLEN |
892 | /* |
893 | * To be compatible with old configurations, |
894 | * such as ifconfig gif0 inet6 2001::1 2001::2 |
895 | * prefixlen 126, we override the specified |
896 | * prefixmask as if the prefix length was 128. |
897 | */ |
898 | ifra->ifra_prefixmask.sin6_len = |
899 | sizeof(struct sockaddr_in6); |
900 | for (i = 0; i < 4; i++) |
901 | ifra->ifra_prefixmask.sin6_addr.s6_addr32[i] = |
902 | 0xffffffff; |
903 | plen = 128; |
904 | #else |
905 | return EINVAL; |
906 | #endif |
907 | } |
908 | } |
909 | /* lifetime consistency check */ |
910 | lt = &ifra->ifra_lifetime; |
911 | if (lt->ia6t_pltime > lt->ia6t_vltime) |
912 | return EINVAL; |
913 | if (lt->ia6t_vltime == 0) { |
914 | /* |
915 | * the following log might be noisy, but this is a typical |
916 | * configuration mistake or a tool's bug. |
917 | */ |
918 | nd6log(LOG_INFO, "valid lifetime is 0 for %s\n" , |
919 | ip6_sprintf(&ifra->ifra_addr.sin6_addr)); |
920 | |
921 | if (ia == NULL) |
922 | return 0; /* there's nothing to do */ |
923 | } |
924 | |
925 | /* |
926 | * If this is a new address, allocate a new ifaddr and link it |
927 | * into chains. |
928 | */ |
929 | if (ia == NULL) { |
930 | hostIsNew = 1; |
931 | /* |
932 | * When in6_update_ifa() is called in a process of a received |
933 | * RA, it is called under an interrupt context. So, we should |
934 | * call malloc with M_NOWAIT. |
935 | */ |
936 | ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR, |
937 | M_NOWAIT); |
938 | if (ia == NULL) |
939 | return ENOBUFS; |
940 | memset(ia, 0, sizeof(*ia)); |
941 | LIST_INIT(&ia->ia6_memberships); |
942 | /* Initialize the address and masks, and put time stamp */ |
943 | ia->ia_ifa.ifa_addr = sin6tosa(&ia->ia_addr); |
944 | ia->ia_addr.sin6_family = AF_INET6; |
945 | ia->ia_addr.sin6_len = sizeof(ia->ia_addr); |
946 | ia->ia6_createtime = time_uptime; |
947 | if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { |
948 | /* |
949 | * XXX: some functions expect that ifa_dstaddr is not |
950 | * NULL for p2p interfaces. |
951 | */ |
952 | ia->ia_ifa.ifa_dstaddr = sin6tosa(&ia->ia_dstaddr); |
953 | } else { |
954 | ia->ia_ifa.ifa_dstaddr = NULL; |
955 | } |
956 | ia->ia_ifa.ifa_netmask = sin6tosa(&ia->ia_prefixmask); |
957 | |
958 | ia->ia_ifp = ifp; |
959 | IN6_ADDRLIST_ENTRY_INIT(ia); |
960 | ifa_psref_init(&ia->ia_ifa); |
961 | } |
962 | |
963 | /* update timestamp */ |
964 | ia->ia6_updatetime = time_uptime; |
965 | |
966 | /* set prefix mask */ |
967 | if (ifra->ifra_prefixmask.sin6_len) { |
968 | if (ia->ia_prefixmask.sin6_len) { |
969 | /* |
970 | * We prohibit changing the prefix length of an |
971 | * existing autoconf address, because the operation |
972 | * would confuse prefix management. |
973 | */ |
974 | if (ia->ia6_ndpr != NULL && |
975 | in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != |
976 | plen) |
977 | { |
978 | nd6log(LOG_INFO, "the prefix length of an" |
979 | " existing (%s) autoconf address should" |
980 | " not be changed\n" , |
981 | ip6_sprintf(&ia->ia_addr.sin6_addr)); |
982 | error = EINVAL; |
983 | if (hostIsNew) |
984 | free(ia, M_IFADDR); |
985 | goto exit; |
986 | } |
987 | |
988 | if (!IN6_ARE_ADDR_EQUAL(&ia->ia_prefixmask.sin6_addr, |
989 | &ifra->ifra_prefixmask.sin6_addr)) |
990 | in6_ifremprefix(ia); |
991 | } |
992 | ia->ia_prefixmask = ifra->ifra_prefixmask; |
993 | } |
994 | |
995 | /* Set destination address. */ |
996 | if (dst6.sin6_family == AF_INET6) { |
997 | if (!IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, |
998 | &ia->ia_dstaddr.sin6_addr)) |
999 | in6_ifremprefix(ia); |
1000 | ia->ia_dstaddr = dst6; |
1001 | } |
1002 | |
1003 | /* |
1004 | * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred |
1005 | * to see if the address is deprecated or invalidated, but initialize |
1006 | * these members for applications. |
1007 | */ |
1008 | ia->ia6_lifetime = ifra->ifra_lifetime; |
1009 | if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { |
1010 | ia->ia6_lifetime.ia6t_expire = |
1011 | time_uptime + ia->ia6_lifetime.ia6t_vltime; |
1012 | } else |
1013 | ia->ia6_lifetime.ia6t_expire = 0; |
1014 | if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { |
1015 | ia->ia6_lifetime.ia6t_preferred = |
1016 | time_uptime + ia->ia6_lifetime.ia6t_pltime; |
1017 | } else |
1018 | ia->ia6_lifetime.ia6t_preferred = 0; |
1019 | |
1020 | /* |
1021 | * configure address flags. |
1022 | * We need to preserve tentative state so DAD works if |
1023 | * something adds the same address before DAD finishes. |
1024 | */ |
1025 | was_tentative = ia->ia6_flags & (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED); |
1026 | ia->ia6_flags = ifra->ifra_flags; |
1027 | |
1028 | /* |
1029 | * Make the address tentative before joining multicast addresses, |
1030 | * so that corresponding MLD responses would not have a tentative |
1031 | * source address. |
1032 | */ |
1033 | ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ |
1034 | if (ifp->if_link_state == LINK_STATE_DOWN) { |
1035 | ia->ia6_flags |= IN6_IFF_DETACHED; |
1036 | ia->ia6_flags &= ~IN6_IFF_TENTATIVE; |
1037 | } else if ((hostIsNew || was_tentative) && if_do_dad(ifp)) |
1038 | ia->ia6_flags |= IN6_IFF_TENTATIVE; |
1039 | |
1040 | /* |
1041 | * backward compatibility - if IN6_IFF_DEPRECATED is set from the |
1042 | * userland, make it deprecated. |
1043 | */ |
1044 | if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { |
1045 | ia->ia6_lifetime.ia6t_pltime = 0; |
1046 | ia->ia6_lifetime.ia6t_preferred = time_uptime; |
1047 | } |
1048 | |
1049 | /* reset the interface and routing table appropriately. */ |
1050 | error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew); |
1051 | if (error != 0) { |
1052 | if (hostIsNew) |
1053 | free(ia, M_IFADDR); |
1054 | goto exit; |
1055 | } |
1056 | |
1057 | /* |
1058 | * We are done if we have simply modified an existing address. |
1059 | */ |
1060 | if (!hostIsNew) |
1061 | return error; |
1062 | |
1063 | /* |
1064 | * Insert ia to the global list and ifa to the interface's list. |
1065 | */ |
1066 | mutex_enter(&in6_ifaddr_lock); |
1067 | IN6_ADDRLIST_WRITER_INSERT_TAIL(ia); |
1068 | mutex_exit(&in6_ifaddr_lock); |
1069 | |
1070 | /* gain a refcnt for the link from in6_ifaddr */ |
1071 | ifaref(&ia->ia_ifa); |
1072 | ifa_insert(ifp, &ia->ia_ifa); |
1073 | |
1074 | /* |
1075 | * Beyond this point, we should call in6_purgeaddr upon an error, |
1076 | * not just go to unlink. |
1077 | */ |
1078 | |
1079 | /* join necessary multicast groups */ |
1080 | if ((ifp->if_flags & IFF_MULTICAST) != 0) { |
1081 | struct sockaddr_in6 mltaddr, mltmask; |
1082 | struct in6_addr llsol; |
1083 | |
1084 | /* join solicited multicast addr for new host id */ |
1085 | memset(&llsol, 0, sizeof(struct in6_addr)); |
1086 | llsol.s6_addr16[0] = htons(0xff02); |
1087 | llsol.s6_addr32[1] = 0; |
1088 | llsol.s6_addr32[2] = htonl(1); |
1089 | llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3]; |
1090 | llsol.s6_addr8[12] = 0xff; |
1091 | if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) { |
1092 | /* XXX: should not happen */ |
1093 | log(LOG_ERR, "%s: in6_setscope failed\n" , __func__); |
1094 | goto cleanup; |
1095 | } |
1096 | dad_delay = 0; |
1097 | if ((flags & IN6_IFAUPDATE_DADDELAY)) { |
1098 | /* |
1099 | * We need a random delay for DAD on the address |
1100 | * being configured. It also means delaying |
1101 | * transmission of the corresponding MLD report to |
1102 | * avoid report collision. |
1103 | * [draft-ietf-ipv6-rfc2462bis-02.txt] |
1104 | */ |
1105 | dad_delay = cprng_fast32() % |
1106 | (MAX_RTR_SOLICITATION_DELAY * hz); |
1107 | } |
1108 | |
1109 | #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */ |
1110 | /* join solicited multicast addr for new host id */ |
1111 | imm = in6_joingroup(ifp, &llsol, &error, dad_delay); |
1112 | if (!imm) { |
1113 | nd6log(LOG_ERR, |
1114 | "addmulti failed for %s on %s (errno=%d)\n" , |
1115 | ip6_sprintf(&llsol), if_name(ifp), error); |
1116 | goto cleanup; |
1117 | } |
1118 | LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); |
1119 | in6m_sol = imm->i6mm_maddr; |
1120 | |
1121 | sockaddr_in6_init(&mltmask, &in6mask32, 0, 0, 0); |
1122 | |
1123 | /* |
1124 | * join link-local all-nodes address |
1125 | */ |
1126 | sockaddr_in6_init(&mltaddr, &in6addr_linklocal_allnodes, |
1127 | 0, 0, 0); |
1128 | if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0) |
1129 | goto cleanup; /* XXX: should not fail */ |
1130 | |
1131 | /* |
1132 | * XXX: do we really need this automatic routes? |
1133 | * We should probably reconsider this stuff. Most applications |
1134 | * actually do not need the routes, since they usually specify |
1135 | * the outgoing interface. |
1136 | */ |
1137 | rt = rtalloc1(sin6tosa(&mltaddr), 0); |
1138 | if (rt) { |
1139 | if (memcmp(&mltaddr.sin6_addr, |
1140 | &satocsin6(rt_getkey(rt))->sin6_addr, |
1141 | MLTMASK_LEN)) { |
1142 | rtfree(rt); |
1143 | rt = NULL; |
1144 | } else if (rt->rt_ifp != ifp) { |
1145 | IN6_DPRINTF("%s: rt_ifp %p -> %p (%s) " |
1146 | "network %04x:%04x::/32 = %04x:%04x::/32\n" , |
1147 | __func__, rt->rt_ifp, ifp, ifp->if_xname, |
1148 | ntohs(mltaddr.sin6_addr.s6_addr16[0]), |
1149 | ntohs(mltaddr.sin6_addr.s6_addr16[1]), |
1150 | satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[0], |
1151 | satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[1]); |
1152 | rt_replace_ifa(rt, &ia->ia_ifa); |
1153 | rt->rt_ifp = ifp; |
1154 | } |
1155 | } |
1156 | if (!rt) { |
1157 | struct rt_addrinfo info; |
1158 | |
1159 | memset(&info, 0, sizeof(info)); |
1160 | info.rti_info[RTAX_DST] = sin6tosa(&mltaddr); |
1161 | info.rti_info[RTAX_GATEWAY] = sin6tosa(&ia->ia_addr); |
1162 | info.rti_info[RTAX_NETMASK] = sin6tosa(&mltmask); |
1163 | info.rti_info[RTAX_IFA] = sin6tosa(&ia->ia_addr); |
1164 | /* XXX: we need RTF_CONNECTED to fake nd6_rtrequest */ |
1165 | info.rti_flags = RTF_UP | RTF_CONNECTED; |
1166 | error = rtrequest1(RTM_ADD, &info, NULL); |
1167 | if (error) |
1168 | goto cleanup; |
1169 | } else { |
1170 | rtfree(rt); |
1171 | } |
1172 | imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); |
1173 | if (!imm) { |
1174 | nd6log(LOG_WARNING, |
1175 | "addmulti failed for %s on %s (errno=%d)\n" , |
1176 | ip6_sprintf(&mltaddr.sin6_addr), |
1177 | if_name(ifp), error); |
1178 | goto cleanup; |
1179 | } |
1180 | LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); |
1181 | |
1182 | /* |
1183 | * join node information group address |
1184 | */ |
1185 | dad_delay = 0; |
1186 | if ((flags & IN6_IFAUPDATE_DADDELAY)) { |
1187 | /* |
1188 | * The spec doesn't say anything about delay for this |
1189 | * group, but the same logic should apply. |
1190 | */ |
1191 | dad_delay = cprng_fast32() % |
1192 | (MAX_RTR_SOLICITATION_DELAY * hz); |
1193 | } |
1194 | if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr) != 0) |
1195 | ; |
1196 | else if ((imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, |
1197 | dad_delay)) == NULL) { /* XXX jinmei */ |
1198 | nd6log(LOG_WARNING, |
1199 | "addmulti failed for %s on %s (errno=%d)\n" , |
1200 | ip6_sprintf(&mltaddr.sin6_addr), |
1201 | if_name(ifp), error); |
1202 | /* XXX not very fatal, go on... */ |
1203 | } else { |
1204 | LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); |
1205 | } |
1206 | |
1207 | |
1208 | /* |
1209 | * join interface-local all-nodes address. |
1210 | * (ff01::1%ifN, and ff01::%ifN/32) |
1211 | */ |
1212 | mltaddr.sin6_addr = in6addr_nodelocal_allnodes; |
1213 | if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0) |
1214 | goto cleanup; /* XXX: should not fail */ |
1215 | |
1216 | /* XXX: again, do we really need the route? */ |
1217 | rt = rtalloc1(sin6tosa(&mltaddr), 0); |
1218 | if (rt) { |
1219 | /* 32bit came from "mltmask" */ |
1220 | if (memcmp(&mltaddr.sin6_addr, |
1221 | &satocsin6(rt_getkey(rt))->sin6_addr, |
1222 | 32 / NBBY)) { |
1223 | rtfree(rt); |
1224 | rt = NULL; |
1225 | } else if (rt->rt_ifp != ifp) { |
1226 | IN6_DPRINTF("%s: rt_ifp %p -> %p (%s) " |
1227 | "network %04x:%04x::/32 = %04x:%04x::/32\n" , |
1228 | __func__, rt->rt_ifp, ifp, ifp->if_xname, |
1229 | ntohs(mltaddr.sin6_addr.s6_addr16[0]), |
1230 | ntohs(mltaddr.sin6_addr.s6_addr16[1]), |
1231 | satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[0], |
1232 | satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[1]); |
1233 | rt_replace_ifa(rt, &ia->ia_ifa); |
1234 | rt->rt_ifp = ifp; |
1235 | } |
1236 | } |
1237 | if (!rt) { |
1238 | struct rt_addrinfo info; |
1239 | |
1240 | memset(&info, 0, sizeof(info)); |
1241 | info.rti_info[RTAX_DST] = sin6tosa(&mltaddr); |
1242 | info.rti_info[RTAX_GATEWAY] = sin6tosa(&ia->ia_addr); |
1243 | info.rti_info[RTAX_NETMASK] = sin6tosa(&mltmask); |
1244 | info.rti_info[RTAX_IFA] = sin6tosa(&ia->ia_addr); |
1245 | info.rti_flags = RTF_UP | RTF_CONNECTED; |
1246 | error = rtrequest1(RTM_ADD, &info, NULL); |
1247 | if (error) |
1248 | goto cleanup; |
1249 | #undef MLTMASK_LEN |
1250 | } else { |
1251 | rtfree(rt); |
1252 | } |
1253 | imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); |
1254 | if (!imm) { |
1255 | nd6log(LOG_WARNING, |
1256 | "addmulti failed for %s on %s (errno=%d)\n" , |
1257 | ip6_sprintf(&mltaddr.sin6_addr), |
1258 | if_name(ifp), error); |
1259 | goto cleanup; |
1260 | } else { |
1261 | LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); |
1262 | } |
1263 | } |
1264 | |
1265 | /* Add local address to lltable, if necessary (ex. on p2p link). */ |
1266 | error = nd6_add_ifa_lle(ia); |
1267 | if (error != 0) |
1268 | goto cleanup; |
1269 | |
1270 | /* |
1271 | * Perform DAD, if needed. |
1272 | * XXX It may be of use, if we can administratively |
1273 | * disable DAD. |
1274 | */ |
1275 | if (hostIsNew && if_do_dad(ifp) && |
1276 | ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) && |
1277 | (ia->ia6_flags & IN6_IFF_TENTATIVE)) |
1278 | { |
1279 | int mindelay, maxdelay; |
1280 | |
1281 | dad_delay = 0; |
1282 | if ((flags & IN6_IFAUPDATE_DADDELAY)) { |
1283 | /* |
1284 | * We need to impose a delay before sending an NS |
1285 | * for DAD. Check if we also needed a delay for the |
1286 | * corresponding MLD message. If we did, the delay |
1287 | * should be larger than the MLD delay (this could be |
1288 | * relaxed a bit, but this simple logic is at least |
1289 | * safe). |
1290 | */ |
1291 | mindelay = 0; |
1292 | if (in6m_sol != NULL && |
1293 | in6m_sol->in6m_state == MLD_REPORTPENDING) { |
1294 | mindelay = in6m_sol->in6m_timer; |
1295 | } |
1296 | maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; |
1297 | if (maxdelay - mindelay == 0) |
1298 | dad_delay = 0; |
1299 | else { |
1300 | dad_delay = |
1301 | (cprng_fast32() % (maxdelay - mindelay)) + |
1302 | mindelay; |
1303 | } |
1304 | } |
1305 | /* +1 ensures callout is always used */ |
1306 | nd6_dad_start(&ia->ia_ifa, dad_delay + 1); |
1307 | } |
1308 | |
1309 | return 0; |
1310 | |
1311 | cleanup: |
1312 | in6_purgeaddr(&ia->ia_ifa); |
1313 | exit: |
1314 | return error; |
1315 | } |
1316 | |
1317 | int |
1318 | in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, |
1319 | struct in6_ifaddr *ia, int flags) |
1320 | { |
1321 | int rc, s; |
1322 | |
1323 | s = splnet(); |
1324 | rc = in6_update_ifa1(ifp, ifra, ia, flags); |
1325 | splx(s); |
1326 | return rc; |
1327 | } |
1328 | |
1329 | void |
1330 | in6_purgeaddr(struct ifaddr *ifa) |
1331 | { |
1332 | struct ifnet *ifp = ifa->ifa_ifp; |
1333 | struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; |
1334 | struct in6_multi_mship *imm; |
1335 | |
1336 | /* stop DAD processing */ |
1337 | nd6_dad_stop(ifa); |
1338 | |
1339 | /* Delete any network route. */ |
1340 | in6_ifremprefix(ia); |
1341 | |
1342 | /* Remove ownaddr's loopback rtentry, if it exists. */ |
1343 | in6_ifremlocal(&(ia->ia_ifa)); |
1344 | |
1345 | /* |
1346 | * leave from multicast groups we have joined for the interface |
1347 | */ |
1348 | while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) { |
1349 | LIST_REMOVE(imm, i6mm_chain); |
1350 | in6_leavegroup(imm); |
1351 | } |
1352 | |
1353 | in6_unlink_ifa(ia, ifp); |
1354 | } |
1355 | |
1356 | static void |
1357 | in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) |
1358 | { |
1359 | int s = splnet(); |
1360 | |
1361 | mutex_enter(&in6_ifaddr_lock); |
1362 | IN6_ADDRLIST_WRITER_REMOVE(ia); |
1363 | ifa_remove(ifp, &ia->ia_ifa); |
1364 | mutex_exit(&in6_ifaddr_lock); |
1365 | |
1366 | /* |
1367 | * XXX thorpej@NetBSD.org -- if the interface is going |
1368 | * XXX away, don't save the multicast entries, delete them! |
1369 | */ |
1370 | if (LIST_EMPTY(&ia->ia6_multiaddrs)) |
1371 | ; |
1372 | else if (if_is_deactivated(ia->ia_ifa.ifa_ifp)) { |
1373 | struct in6_multi *in6m, *next; |
1374 | |
1375 | for (in6m = LIST_FIRST(&ia->ia6_multiaddrs); in6m != NULL; |
1376 | in6m = next) { |
1377 | next = LIST_NEXT(in6m, in6m_entry); |
1378 | in6_delmulti(in6m); |
1379 | } |
1380 | } else |
1381 | in6_savemkludge(ia); |
1382 | |
1383 | /* |
1384 | * Release the reference to the ND prefix. |
1385 | */ |
1386 | if (ia->ia6_ndpr != NULL) { |
1387 | ia->ia6_ndpr->ndpr_refcnt--; |
1388 | ia->ia6_ndpr = NULL; |
1389 | } |
1390 | |
1391 | /* |
1392 | * Also, if the address being removed is autoconf'ed, call |
1393 | * pfxlist_onlink_check() since the release might affect the status of |
1394 | * other (detached) addresses. |
1395 | */ |
1396 | if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0) |
1397 | pfxlist_onlink_check(); |
1398 | |
1399 | IN6_ADDRLIST_ENTRY_DESTROY(ia); |
1400 | |
1401 | /* |
1402 | * release another refcnt for the link from in6_ifaddr. |
1403 | * Note that we should decrement the refcnt at least once for all *BSD. |
1404 | */ |
1405 | ifafree(&ia->ia_ifa); |
1406 | |
1407 | splx(s); |
1408 | } |
1409 | |
1410 | void |
1411 | in6_purgeif(struct ifnet *ifp) |
1412 | { |
1413 | |
1414 | in6_ifdetach(ifp); |
1415 | } |
1416 | |
1417 | /* |
1418 | * SIOC[GAD]LIFADDR. |
1419 | * SIOCGLIFADDR: get first address. (?) |
1420 | * SIOCGLIFADDR with IFLR_PREFIX: |
1421 | * get first address that matches the specified prefix. |
1422 | * SIOCALIFADDR: add the specified address. |
1423 | * SIOCALIFADDR with IFLR_PREFIX: |
1424 | * add the specified prefix, filling hostid part from |
1425 | * the first link-local address. prefixlen must be <= 64. |
1426 | * SIOCDLIFADDR: delete the specified address. |
1427 | * SIOCDLIFADDR with IFLR_PREFIX: |
1428 | * delete the first address that matches the specified prefix. |
1429 | * return values: |
1430 | * EINVAL on invalid parameters |
1431 | * EADDRNOTAVAIL on prefix match failed/specified address not found |
1432 | * other values may be returned from in6_ioctl() |
1433 | * |
1434 | * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. |
1435 | * this is to accommodate address naming scheme other than RFC2374, |
1436 | * in the future. |
1437 | * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 |
1438 | * address encoding scheme. (see figure on page 8) |
1439 | */ |
1440 | static int |
1441 | in6_lifaddr_ioctl(struct socket *so, u_long cmd, void *data, |
1442 | struct ifnet *ifp) |
1443 | { |
1444 | struct in6_ifaddr *ia = NULL; /* XXX gcc 4.8 maybe-uninitialized */ |
1445 | struct if_laddrreq *iflr = (struct if_laddrreq *)data; |
1446 | struct ifaddr *ifa; |
1447 | struct sockaddr *sa; |
1448 | |
1449 | /* sanity checks */ |
1450 | if (!data || !ifp) { |
1451 | panic("invalid argument to in6_lifaddr_ioctl" ); |
1452 | /* NOTREACHED */ |
1453 | } |
1454 | |
1455 | switch (cmd) { |
1456 | case SIOCGLIFADDR: |
1457 | /* address must be specified on GET with IFLR_PREFIX */ |
1458 | if ((iflr->flags & IFLR_PREFIX) == 0) |
1459 | break; |
1460 | /* FALLTHROUGH */ |
1461 | case SIOCALIFADDR: |
1462 | case SIOCDLIFADDR: |
1463 | /* address must be specified on ADD and DELETE */ |
1464 | sa = (struct sockaddr *)&iflr->addr; |
1465 | if (sa->sa_family != AF_INET6) |
1466 | return EINVAL; |
1467 | if (sa->sa_len != sizeof(struct sockaddr_in6)) |
1468 | return EINVAL; |
1469 | /* XXX need improvement */ |
1470 | sa = (struct sockaddr *)&iflr->dstaddr; |
1471 | if (sa->sa_family && sa->sa_family != AF_INET6) |
1472 | return EINVAL; |
1473 | if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) |
1474 | return EINVAL; |
1475 | break; |
1476 | default: /* shouldn't happen */ |
1477 | #if 0 |
1478 | panic("invalid cmd to in6_lifaddr_ioctl" ); |
1479 | /* NOTREACHED */ |
1480 | #else |
1481 | return EOPNOTSUPP; |
1482 | #endif |
1483 | } |
1484 | if (sizeof(struct in6_addr) * NBBY < iflr->prefixlen) |
1485 | return EINVAL; |
1486 | |
1487 | switch (cmd) { |
1488 | case SIOCALIFADDR: |
1489 | { |
1490 | struct in6_aliasreq ifra; |
1491 | struct in6_addr *xhostid = NULL; |
1492 | int prefixlen; |
1493 | int bound = curlwp_bind(); |
1494 | struct psref psref; |
1495 | |
1496 | if ((iflr->flags & IFLR_PREFIX) != 0) { |
1497 | struct sockaddr_in6 *sin6; |
1498 | |
1499 | /* |
1500 | * xhostid is to fill in the hostid part of the |
1501 | * address. xhostid points to the first link-local |
1502 | * address attached to the interface. |
1503 | */ |
1504 | ia = in6ifa_ifpforlinklocal_psref(ifp, 0, &psref); |
1505 | if (ia == NULL) { |
1506 | curlwp_bindx(bound); |
1507 | return EADDRNOTAVAIL; |
1508 | } |
1509 | xhostid = IFA_IN6(&ia->ia_ifa); |
1510 | |
1511 | /* prefixlen must be <= 64. */ |
1512 | if (64 < iflr->prefixlen) { |
1513 | ia6_release(ia, &psref); |
1514 | curlwp_bindx(bound); |
1515 | return EINVAL; |
1516 | } |
1517 | prefixlen = iflr->prefixlen; |
1518 | |
1519 | /* hostid part must be zero. */ |
1520 | sin6 = (struct sockaddr_in6 *)&iflr->addr; |
1521 | if (sin6->sin6_addr.s6_addr32[2] != 0 |
1522 | || sin6->sin6_addr.s6_addr32[3] != 0) { |
1523 | ia6_release(ia, &psref); |
1524 | curlwp_bindx(bound); |
1525 | return EINVAL; |
1526 | } |
1527 | } else |
1528 | prefixlen = iflr->prefixlen; |
1529 | |
1530 | /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ |
1531 | memset(&ifra, 0, sizeof(ifra)); |
1532 | memcpy(ifra.ifra_name, iflr->iflr_name, sizeof(ifra.ifra_name)); |
1533 | |
1534 | memcpy(&ifra.ifra_addr, &iflr->addr, |
1535 | ((struct sockaddr *)&iflr->addr)->sa_len); |
1536 | if (xhostid) { |
1537 | /* fill in hostid part */ |
1538 | ifra.ifra_addr.sin6_addr.s6_addr32[2] = |
1539 | xhostid->s6_addr32[2]; |
1540 | ifra.ifra_addr.sin6_addr.s6_addr32[3] = |
1541 | xhostid->s6_addr32[3]; |
1542 | } |
1543 | |
1544 | if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ |
1545 | memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr, |
1546 | ((struct sockaddr *)&iflr->dstaddr)->sa_len); |
1547 | if (xhostid) { |
1548 | ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = |
1549 | xhostid->s6_addr32[2]; |
1550 | ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = |
1551 | xhostid->s6_addr32[3]; |
1552 | } |
1553 | } |
1554 | if (xhostid) { |
1555 | ia6_release(ia, &psref); |
1556 | ia = NULL; |
1557 | } |
1558 | curlwp_bindx(bound); |
1559 | |
1560 | ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); |
1561 | in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); |
1562 | |
1563 | ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; |
1564 | ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; |
1565 | ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; |
1566 | return in6_control(so, SIOCAIFADDR_IN6, &ifra, ifp); |
1567 | } |
1568 | case SIOCGLIFADDR: |
1569 | case SIOCDLIFADDR: |
1570 | { |
1571 | struct in6_addr mask, candidate, match; |
1572 | struct sockaddr_in6 *sin6; |
1573 | int cmp; |
1574 | int error, s; |
1575 | |
1576 | memset(&mask, 0, sizeof(mask)); |
1577 | if (iflr->flags & IFLR_PREFIX) { |
1578 | /* lookup a prefix rather than address. */ |
1579 | in6_prefixlen2mask(&mask, iflr->prefixlen); |
1580 | |
1581 | sin6 = (struct sockaddr_in6 *)&iflr->addr; |
1582 | memcpy(&match, &sin6->sin6_addr, sizeof(match)); |
1583 | match.s6_addr32[0] &= mask.s6_addr32[0]; |
1584 | match.s6_addr32[1] &= mask.s6_addr32[1]; |
1585 | match.s6_addr32[2] &= mask.s6_addr32[2]; |
1586 | match.s6_addr32[3] &= mask.s6_addr32[3]; |
1587 | |
1588 | /* if you set extra bits, that's wrong */ |
1589 | if (memcmp(&match, &sin6->sin6_addr, sizeof(match))) |
1590 | return EINVAL; |
1591 | |
1592 | cmp = 1; |
1593 | } else { |
1594 | if (cmd == SIOCGLIFADDR) { |
1595 | /* on getting an address, take the 1st match */ |
1596 | cmp = 0; /* XXX */ |
1597 | } else { |
1598 | /* on deleting an address, do exact match */ |
1599 | in6_prefixlen2mask(&mask, 128); |
1600 | sin6 = (struct sockaddr_in6 *)&iflr->addr; |
1601 | memcpy(&match, &sin6->sin6_addr, sizeof(match)); |
1602 | |
1603 | cmp = 1; |
1604 | } |
1605 | } |
1606 | |
1607 | s = pserialize_read_enter(); |
1608 | IFADDR_READER_FOREACH(ifa, ifp) { |
1609 | if (ifa->ifa_addr->sa_family != AF_INET6) |
1610 | continue; |
1611 | if (!cmp) |
1612 | break; |
1613 | |
1614 | /* |
1615 | * XXX: this is adhoc, but is necessary to allow |
1616 | * a user to specify fe80::/64 (not /10) for a |
1617 | * link-local address. |
1618 | */ |
1619 | memcpy(&candidate, IFA_IN6(ifa), sizeof(candidate)); |
1620 | in6_clearscope(&candidate); |
1621 | candidate.s6_addr32[0] &= mask.s6_addr32[0]; |
1622 | candidate.s6_addr32[1] &= mask.s6_addr32[1]; |
1623 | candidate.s6_addr32[2] &= mask.s6_addr32[2]; |
1624 | candidate.s6_addr32[3] &= mask.s6_addr32[3]; |
1625 | if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) |
1626 | break; |
1627 | } |
1628 | if (!ifa) { |
1629 | error = EADDRNOTAVAIL; |
1630 | goto error; |
1631 | } |
1632 | ia = ifa2ia6(ifa); |
1633 | |
1634 | if (cmd == SIOCGLIFADDR) { |
1635 | /* fill in the if_laddrreq structure */ |
1636 | memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin6_len); |
1637 | error = sa6_recoverscope( |
1638 | (struct sockaddr_in6 *)&iflr->addr); |
1639 | if (error != 0) |
1640 | goto error; |
1641 | |
1642 | if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { |
1643 | memcpy(&iflr->dstaddr, &ia->ia_dstaddr, |
1644 | ia->ia_dstaddr.sin6_len); |
1645 | error = sa6_recoverscope( |
1646 | (struct sockaddr_in6 *)&iflr->dstaddr); |
1647 | if (error != 0) |
1648 | goto error; |
1649 | } else |
1650 | memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr)); |
1651 | |
1652 | iflr->prefixlen = |
1653 | in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); |
1654 | |
1655 | iflr->flags = ia->ia6_flags; /* XXX */ |
1656 | |
1657 | error = 0; |
1658 | } else { |
1659 | struct in6_aliasreq ifra; |
1660 | |
1661 | /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ |
1662 | memset(&ifra, 0, sizeof(ifra)); |
1663 | memcpy(ifra.ifra_name, iflr->iflr_name, |
1664 | sizeof(ifra.ifra_name)); |
1665 | |
1666 | memcpy(&ifra.ifra_addr, &ia->ia_addr, |
1667 | ia->ia_addr.sin6_len); |
1668 | if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { |
1669 | memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr, |
1670 | ia->ia_dstaddr.sin6_len); |
1671 | } else { |
1672 | memset(&ifra.ifra_dstaddr, 0, |
1673 | sizeof(ifra.ifra_dstaddr)); |
1674 | } |
1675 | memcpy(&ifra.ifra_dstaddr, &ia->ia_prefixmask, |
1676 | ia->ia_prefixmask.sin6_len); |
1677 | |
1678 | ifra.ifra_flags = ia->ia6_flags; |
1679 | pserialize_read_exit(s); |
1680 | |
1681 | return in6_control(so, SIOCDIFADDR_IN6, &ifra, ifp); |
1682 | } |
1683 | error: |
1684 | pserialize_read_exit(s); |
1685 | return error; |
1686 | } |
1687 | } |
1688 | |
1689 | return EOPNOTSUPP; /* just for safety */ |
1690 | } |
1691 | |
1692 | /* |
1693 | * Initialize an interface's internet6 address |
1694 | * and routing table entry. |
1695 | */ |
1696 | static int |
1697 | in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, |
1698 | const struct sockaddr_in6 *sin6, int newhost) |
1699 | { |
1700 | int error = 0, ifacount = 0; |
1701 | int s = splnet(); |
1702 | struct ifaddr *ifa; |
1703 | |
1704 | /* |
1705 | * Give the interface a chance to initialize |
1706 | * if this is its first address, |
1707 | * and to validate the address if necessary. |
1708 | */ |
1709 | IFADDR_READER_FOREACH(ifa, ifp) { |
1710 | if (ifa->ifa_addr->sa_family != AF_INET6) |
1711 | continue; |
1712 | ifacount++; |
1713 | } |
1714 | |
1715 | ia->ia_addr = *sin6; |
1716 | |
1717 | if (ifacount <= 0 && |
1718 | (error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0) { |
1719 | splx(s); |
1720 | return error; |
1721 | } |
1722 | splx(s); |
1723 | |
1724 | ia->ia_ifa.ifa_metric = ifp->if_metric; |
1725 | |
1726 | /* we could do in(6)_socktrim here, but just omit it at this moment. */ |
1727 | |
1728 | /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ |
1729 | if (newhost) { |
1730 | /* set the rtrequest function to create llinfo */ |
1731 | if (ifp->if_flags & IFF_POINTOPOINT) |
1732 | ia->ia_ifa.ifa_rtrequest = p2p_rtrequest; |
1733 | else if ((ifp->if_flags & IFF_LOOPBACK) == 0) |
1734 | ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; |
1735 | in6_ifaddlocal(&ia->ia_ifa); |
1736 | } else { |
1737 | /* Inform the routing socket of new flags/timings */ |
1738 | rt_newaddrmsg(RTM_NEWADDR, &ia->ia_ifa, 0, NULL); |
1739 | } |
1740 | |
1741 | /* Add the network prefix route. */ |
1742 | if ((error = in6_ifaddprefix(ia)) != 0) { |
1743 | if (newhost) |
1744 | in6_ifremlocal(&ia->ia_ifa); |
1745 | return error; |
1746 | } |
1747 | |
1748 | if (ifp->if_flags & IFF_MULTICAST) |
1749 | in6_restoremkludge(ia, ifp); |
1750 | |
1751 | return error; |
1752 | } |
1753 | |
1754 | static struct ifaddr * |
1755 | bestifa(struct ifaddr *best_ifa, struct ifaddr *ifa) |
1756 | { |
1757 | if (best_ifa == NULL || best_ifa->ifa_preference < ifa->ifa_preference) |
1758 | return ifa; |
1759 | return best_ifa; |
1760 | } |
1761 | |
1762 | /* |
1763 | * Find an IPv6 interface link-local address specific to an interface. |
1764 | */ |
1765 | struct in6_ifaddr * |
1766 | in6ifa_ifpforlinklocal(const struct ifnet *ifp, const int ignoreflags) |
1767 | { |
1768 | struct ifaddr *best_ifa = NULL, *ifa; |
1769 | |
1770 | IFADDR_READER_FOREACH(ifa, ifp) { |
1771 | if (ifa->ifa_addr->sa_family != AF_INET6) |
1772 | continue; |
1773 | if (!IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) |
1774 | continue; |
1775 | if ((((struct in6_ifaddr *)ifa)->ia6_flags & ignoreflags) != 0) |
1776 | continue; |
1777 | best_ifa = bestifa(best_ifa, ifa); |
1778 | } |
1779 | |
1780 | return (struct in6_ifaddr *)best_ifa; |
1781 | } |
1782 | |
1783 | struct in6_ifaddr * |
1784 | in6ifa_ifpforlinklocal_psref(const struct ifnet *ifp, const int ignoreflags, |
1785 | struct psref *psref) |
1786 | { |
1787 | struct in6_ifaddr *ia; |
1788 | int s = pserialize_read_enter(); |
1789 | |
1790 | ia = in6ifa_ifpforlinklocal(ifp, ignoreflags); |
1791 | if (ia != NULL) |
1792 | ia6_acquire(ia, psref); |
1793 | pserialize_read_exit(s); |
1794 | |
1795 | return ia; |
1796 | } |
1797 | |
1798 | /* |
1799 | * find the internet address corresponding to a given address. |
1800 | * ifaddr is returned referenced. |
1801 | */ |
1802 | struct in6_ifaddr * |
1803 | in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid) |
1804 | { |
1805 | struct in6_ifaddr *ia; |
1806 | int s; |
1807 | |
1808 | s = pserialize_read_enter(); |
1809 | IN6_ADDRLIST_READER_FOREACH(ia) { |
1810 | if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) { |
1811 | if (zoneid != 0 && |
1812 | zoneid != ia->ia_addr.sin6_scope_id) |
1813 | continue; |
1814 | ifaref(&ia->ia_ifa); |
1815 | break; |
1816 | } |
1817 | } |
1818 | pserialize_read_exit(s); |
1819 | |
1820 | return ia; |
1821 | } |
1822 | |
1823 | /* |
1824 | * find the internet address corresponding to a given interface and address. |
1825 | */ |
1826 | struct in6_ifaddr * |
1827 | in6ifa_ifpwithaddr(const struct ifnet *ifp, const struct in6_addr *addr) |
1828 | { |
1829 | struct ifaddr *best_ifa = NULL, *ifa; |
1830 | |
1831 | IFADDR_READER_FOREACH(ifa, ifp) { |
1832 | if (ifa->ifa_addr->sa_family != AF_INET6) |
1833 | continue; |
1834 | if (!IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) |
1835 | continue; |
1836 | best_ifa = bestifa(best_ifa, ifa); |
1837 | } |
1838 | |
1839 | return (struct in6_ifaddr *)best_ifa; |
1840 | } |
1841 | |
1842 | struct in6_ifaddr * |
1843 | in6ifa_ifpwithaddr_psref(const struct ifnet *ifp, const struct in6_addr *addr, |
1844 | struct psref *psref) |
1845 | { |
1846 | struct in6_ifaddr *ia; |
1847 | int s = pserialize_read_enter(); |
1848 | |
1849 | ia = in6ifa_ifpwithaddr(ifp, addr); |
1850 | if (ia != NULL) |
1851 | ia6_acquire(ia, psref); |
1852 | pserialize_read_exit(s); |
1853 | |
1854 | return ia; |
1855 | } |
1856 | |
1857 | static struct in6_ifaddr * |
1858 | bestia(struct in6_ifaddr *best_ia, struct in6_ifaddr *ia) |
1859 | { |
1860 | if (best_ia == NULL || |
1861 | best_ia->ia_ifa.ifa_preference < ia->ia_ifa.ifa_preference) |
1862 | return ia; |
1863 | return best_ia; |
1864 | } |
1865 | |
1866 | /* |
1867 | * Convert IP6 address to printable (loggable) representation. |
1868 | */ |
1869 | char * |
1870 | ip6_sprintf(const struct in6_addr *addr) |
1871 | { |
1872 | static int ip6round = 0; |
1873 | static char ip6buf[8][INET6_ADDRSTRLEN]; |
1874 | char *cp = ip6buf[ip6round++ & 7]; |
1875 | |
1876 | in6_print(cp, INET6_ADDRSTRLEN, addr); |
1877 | return cp; |
1878 | } |
1879 | |
1880 | /* |
1881 | * Determine if an address is on a local network. |
1882 | */ |
1883 | int |
1884 | in6_localaddr(const struct in6_addr *in6) |
1885 | { |
1886 | struct in6_ifaddr *ia; |
1887 | int s; |
1888 | |
1889 | if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) |
1890 | return 1; |
1891 | |
1892 | s = pserialize_read_enter(); |
1893 | IN6_ADDRLIST_READER_FOREACH(ia) { |
1894 | if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, |
1895 | &ia->ia_prefixmask.sin6_addr)) { |
1896 | pserialize_read_exit(s); |
1897 | return 1; |
1898 | } |
1899 | } |
1900 | pserialize_read_exit(s); |
1901 | |
1902 | return 0; |
1903 | } |
1904 | |
1905 | int |
1906 | in6_is_addr_deprecated(struct sockaddr_in6 *sa6) |
1907 | { |
1908 | struct in6_ifaddr *ia; |
1909 | int s; |
1910 | |
1911 | s = pserialize_read_enter(); |
1912 | IN6_ADDRLIST_READER_FOREACH(ia) { |
1913 | if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, |
1914 | &sa6->sin6_addr) && |
1915 | #ifdef SCOPEDROUTING |
1916 | ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id && |
1917 | #endif |
1918 | (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) { |
1919 | pserialize_read_exit(s); |
1920 | return 1; /* true */ |
1921 | } |
1922 | |
1923 | /* XXX: do we still have to go thru the rest of the list? */ |
1924 | } |
1925 | pserialize_read_exit(s); |
1926 | |
1927 | return 0; /* false */ |
1928 | } |
1929 | |
1930 | /* |
1931 | * return length of part which dst and src are equal |
1932 | * hard coding... |
1933 | */ |
1934 | int |
1935 | in6_matchlen(struct in6_addr *src, struct in6_addr *dst) |
1936 | { |
1937 | int match = 0; |
1938 | u_char *s = (u_char *)src, *d = (u_char *)dst; |
1939 | u_char *lim = s + 16, r; |
1940 | |
1941 | while (s < lim) |
1942 | if ((r = (*d++ ^ *s++)) != 0) { |
1943 | while (r < 128) { |
1944 | match++; |
1945 | r <<= 1; |
1946 | } |
1947 | break; |
1948 | } else |
1949 | match += NBBY; |
1950 | return match; |
1951 | } |
1952 | |
1953 | /* XXX: to be scope conscious */ |
1954 | int |
1955 | in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) |
1956 | { |
1957 | int bytelen, bitlen; |
1958 | |
1959 | /* sanity check */ |
1960 | if (len < 0 || len > 128) { |
1961 | log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n" , |
1962 | len); |
1963 | return 0; |
1964 | } |
1965 | |
1966 | bytelen = len / NBBY; |
1967 | bitlen = len % NBBY; |
1968 | |
1969 | if (memcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) |
1970 | return 0; |
1971 | if (bitlen != 0 && |
1972 | p1->s6_addr[bytelen] >> (NBBY - bitlen) != |
1973 | p2->s6_addr[bytelen] >> (NBBY - bitlen)) |
1974 | return 0; |
1975 | |
1976 | return 1; |
1977 | } |
1978 | |
1979 | void |
1980 | in6_prefixlen2mask(struct in6_addr *maskp, int len) |
1981 | { |
1982 | static const u_char maskarray[NBBY] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; |
1983 | int bytelen, bitlen, i; |
1984 | |
1985 | /* sanity check */ |
1986 | if (len < 0 || len > 128) { |
1987 | log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n" , |
1988 | len); |
1989 | return; |
1990 | } |
1991 | |
1992 | memset(maskp, 0, sizeof(*maskp)); |
1993 | bytelen = len / NBBY; |
1994 | bitlen = len % NBBY; |
1995 | for (i = 0; i < bytelen; i++) |
1996 | maskp->s6_addr[i] = 0xff; |
1997 | if (bitlen) |
1998 | maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; |
1999 | } |
2000 | |
2001 | /* |
2002 | * return the best address out of the same scope. if no address was |
2003 | * found, return the first valid address from designated IF. |
2004 | */ |
2005 | struct in6_ifaddr * |
2006 | in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) |
2007 | { |
2008 | int dst_scope = in6_addrscope(dst), blen = -1, tlen; |
2009 | struct ifaddr *ifa; |
2010 | struct in6_ifaddr *best_ia = NULL, *ia; |
2011 | struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ |
2012 | |
2013 | dep[0] = dep[1] = NULL; |
2014 | |
2015 | /* |
2016 | * We first look for addresses in the same scope. |
2017 | * If there is one, return it. |
2018 | * If two or more, return one which matches the dst longest. |
2019 | * If none, return one of global addresses assigned other ifs. |
2020 | */ |
2021 | IFADDR_READER_FOREACH(ifa, ifp) { |
2022 | if (ifa->ifa_addr->sa_family != AF_INET6) |
2023 | continue; |
2024 | ia = (struct in6_ifaddr *)ifa; |
2025 | if (ia->ia6_flags & IN6_IFF_ANYCAST) |
2026 | continue; /* XXX: is there any case to allow anycast? */ |
2027 | if (ia->ia6_flags & IN6_IFF_NOTREADY) |
2028 | continue; /* don't use this interface */ |
2029 | if (ia->ia6_flags & IN6_IFF_DETACHED) |
2030 | continue; |
2031 | if (ia->ia6_flags & IN6_IFF_DEPRECATED) { |
2032 | if (ip6_use_deprecated) |
2033 | dep[0] = ia; |
2034 | continue; |
2035 | } |
2036 | |
2037 | if (dst_scope != in6_addrscope(IFA_IN6(ifa))) |
2038 | continue; |
2039 | /* |
2040 | * call in6_matchlen() as few as possible |
2041 | */ |
2042 | if (best_ia == NULL) { |
2043 | best_ia = ia; |
2044 | continue; |
2045 | } |
2046 | if (blen == -1) |
2047 | blen = in6_matchlen(&best_ia->ia_addr.sin6_addr, dst); |
2048 | tlen = in6_matchlen(IFA_IN6(ifa), dst); |
2049 | if (tlen > blen) { |
2050 | blen = tlen; |
2051 | best_ia = ia; |
2052 | } else if (tlen == blen) |
2053 | best_ia = bestia(best_ia, ia); |
2054 | } |
2055 | if (best_ia != NULL) |
2056 | return best_ia; |
2057 | |
2058 | IFADDR_READER_FOREACH(ifa, ifp) { |
2059 | if (ifa->ifa_addr->sa_family != AF_INET6) |
2060 | continue; |
2061 | ia = (struct in6_ifaddr *)ifa; |
2062 | if (ia->ia6_flags & IN6_IFF_ANYCAST) |
2063 | continue; /* XXX: is there any case to allow anycast? */ |
2064 | if (ia->ia6_flags & IN6_IFF_NOTREADY) |
2065 | continue; /* don't use this interface */ |
2066 | if (ia->ia6_flags & IN6_IFF_DETACHED) |
2067 | continue; |
2068 | if (ia->ia6_flags & IN6_IFF_DEPRECATED) { |
2069 | if (ip6_use_deprecated) |
2070 | dep[1] = (struct in6_ifaddr *)ifa; |
2071 | continue; |
2072 | } |
2073 | |
2074 | best_ia = bestia(best_ia, ia); |
2075 | } |
2076 | if (best_ia != NULL) |
2077 | return best_ia; |
2078 | |
2079 | /* use the last-resort values, that are, deprecated addresses */ |
2080 | if (dep[0]) |
2081 | return dep[0]; |
2082 | if (dep[1]) |
2083 | return dep[1]; |
2084 | |
2085 | return NULL; |
2086 | } |
2087 | |
2088 | /* |
2089 | * perform DAD when interface becomes IFF_UP. |
2090 | */ |
2091 | void |
2092 | in6_if_link_up(struct ifnet *ifp) |
2093 | { |
2094 | struct ifaddr *ifa; |
2095 | struct in6_ifaddr *ia; |
2096 | int s, bound; |
2097 | |
2098 | /* Ensure it's sane to run DAD */ |
2099 | if (ifp->if_link_state == LINK_STATE_DOWN) |
2100 | return; |
2101 | if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) |
2102 | return; |
2103 | |
2104 | bound = curlwp_bind(); |
2105 | s = pserialize_read_enter(); |
2106 | IFADDR_READER_FOREACH(ifa, ifp) { |
2107 | struct psref psref; |
2108 | |
2109 | if (ifa->ifa_addr->sa_family != AF_INET6) |
2110 | continue; |
2111 | |
2112 | ifa_acquire(ifa, &psref); |
2113 | pserialize_read_exit(s); |
2114 | ia = (struct in6_ifaddr *)ifa; |
2115 | |
2116 | /* If detached then mark as tentative */ |
2117 | if (ia->ia6_flags & IN6_IFF_DETACHED) { |
2118 | ia->ia6_flags &= ~IN6_IFF_DETACHED; |
2119 | if (if_do_dad(ifp)) { |
2120 | ia->ia6_flags |= IN6_IFF_TENTATIVE; |
2121 | nd6log(LOG_ERR, "%s marked tentative\n" , |
2122 | ip6_sprintf(&ia->ia_addr.sin6_addr)); |
2123 | } else if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) |
2124 | rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); |
2125 | } |
2126 | |
2127 | if (ia->ia6_flags & IN6_IFF_TENTATIVE) { |
2128 | int rand_delay; |
2129 | |
2130 | /* Clear the duplicated flag as we're starting DAD. */ |
2131 | ia->ia6_flags &= ~IN6_IFF_DUPLICATED; |
2132 | |
2133 | /* |
2134 | * The TENTATIVE flag was likely set by hand |
2135 | * beforehand, implicitly indicating the need for DAD. |
2136 | * We may be able to skip the random delay in this |
2137 | * case, but we impose delays just in case. |
2138 | */ |
2139 | rand_delay = cprng_fast32() % |
2140 | (MAX_RTR_SOLICITATION_DELAY * hz); |
2141 | /* +1 ensures callout is always used */ |
2142 | nd6_dad_start(ifa, rand_delay + 1); |
2143 | } |
2144 | |
2145 | s = pserialize_read_enter(); |
2146 | ifa_release(ifa, &psref); |
2147 | } |
2148 | pserialize_read_exit(s); |
2149 | curlwp_bindx(bound); |
2150 | |
2151 | /* Restore any detached prefixes */ |
2152 | pfxlist_onlink_check(); |
2153 | } |
2154 | |
2155 | void |
2156 | in6_if_up(struct ifnet *ifp) |
2157 | { |
2158 | |
2159 | /* |
2160 | * special cases, like 6to4, are handled in in6_ifattach |
2161 | */ |
2162 | in6_ifattach(ifp, NULL); |
2163 | |
2164 | /* interface may not support link state, so bring it up also */ |
2165 | in6_if_link_up(ifp); |
2166 | } |
2167 | |
2168 | /* |
2169 | * Mark all addresses as detached. |
2170 | */ |
2171 | void |
2172 | in6_if_link_down(struct ifnet *ifp) |
2173 | { |
2174 | struct ifaddr *ifa; |
2175 | struct in6_ifaddr *ia; |
2176 | int s, bound; |
2177 | |
2178 | /* Any prefixes on this interface should be detached as well */ |
2179 | pfxlist_onlink_check(); |
2180 | |
2181 | bound = curlwp_bind(); |
2182 | s = pserialize_read_enter(); |
2183 | IFADDR_READER_FOREACH(ifa, ifp) { |
2184 | struct psref psref; |
2185 | |
2186 | if (ifa->ifa_addr->sa_family != AF_INET6) |
2187 | continue; |
2188 | |
2189 | ifa_acquire(ifa, &psref); |
2190 | pserialize_read_exit(s); |
2191 | ia = (struct in6_ifaddr *)ifa; |
2192 | |
2193 | /* Stop DAD processing */ |
2194 | nd6_dad_stop(ifa); |
2195 | |
2196 | /* |
2197 | * Mark the address as detached. |
2198 | * This satisfies RFC4862 Section 5.3, but we should apply |
2199 | * this logic to all addresses to be a good citizen and |
2200 | * avoid potential duplicated addresses. |
2201 | * When the interface comes up again, detached addresses |
2202 | * are marked tentative and DAD commences. |
2203 | */ |
2204 | if (!(ia->ia6_flags & IN6_IFF_DETACHED)) { |
2205 | nd6log(LOG_DEBUG, "%s marked detached\n" , |
2206 | ip6_sprintf(&ia->ia_addr.sin6_addr)); |
2207 | ia->ia6_flags |= IN6_IFF_DETACHED; |
2208 | ia->ia6_flags &= |
2209 | ~(IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED); |
2210 | rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); |
2211 | } |
2212 | |
2213 | s = pserialize_read_enter(); |
2214 | ifa_release(ifa, &psref); |
2215 | } |
2216 | pserialize_read_exit(s); |
2217 | curlwp_bindx(bound); |
2218 | } |
2219 | |
2220 | void |
2221 | in6_if_down(struct ifnet *ifp) |
2222 | { |
2223 | |
2224 | in6_if_link_down(ifp); |
2225 | } |
2226 | |
2227 | void |
2228 | in6_if_link_state_change(struct ifnet *ifp, int link_state) |
2229 | { |
2230 | |
2231 | switch (link_state) { |
2232 | case LINK_STATE_DOWN: |
2233 | in6_if_link_down(ifp); |
2234 | break; |
2235 | case LINK_STATE_UP: |
2236 | in6_if_link_up(ifp); |
2237 | break; |
2238 | } |
2239 | } |
2240 | |
2241 | /* |
2242 | * Calculate max IPv6 MTU through all the interfaces and store it |
2243 | * to in6_maxmtu. |
2244 | */ |
2245 | void |
2246 | in6_setmaxmtu(void) |
2247 | { |
2248 | unsigned long maxmtu = 0; |
2249 | struct ifnet *ifp; |
2250 | int s; |
2251 | |
2252 | s = pserialize_read_enter(); |
2253 | IFNET_READER_FOREACH(ifp) { |
2254 | /* this function can be called during ifnet initialization */ |
2255 | if (!ifp->if_afdata[AF_INET6]) |
2256 | continue; |
2257 | if ((ifp->if_flags & IFF_LOOPBACK) == 0 && |
2258 | IN6_LINKMTU(ifp) > maxmtu) |
2259 | maxmtu = IN6_LINKMTU(ifp); |
2260 | } |
2261 | pserialize_read_exit(s); |
2262 | if (maxmtu) /* update only when maxmtu is positive */ |
2263 | in6_maxmtu = maxmtu; |
2264 | } |
2265 | |
2266 | /* |
2267 | * Provide the length of interface identifiers to be used for the link attached |
2268 | * to the given interface. The length should be defined in "IPv6 over |
2269 | * xxx-link" document. Note that address architecture might also define |
2270 | * the length for a particular set of address prefixes, regardless of the |
2271 | * link type. As clarified in rfc2462bis, those two definitions should be |
2272 | * consistent, and those really are as of August 2004. |
2273 | */ |
2274 | int |
2275 | in6_if2idlen(struct ifnet *ifp) |
2276 | { |
2277 | switch (ifp->if_type) { |
2278 | case IFT_ETHER: /* RFC2464 */ |
2279 | case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ |
2280 | case IFT_L2VLAN: /* ditto */ |
2281 | case IFT_IEEE80211: /* ditto */ |
2282 | case IFT_FDDI: /* RFC2467 */ |
2283 | case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */ |
2284 | case IFT_PPP: /* RFC2472 */ |
2285 | case IFT_ARCNET: /* RFC2497 */ |
2286 | case IFT_FRELAY: /* RFC2590 */ |
2287 | case IFT_IEEE1394: /* RFC3146 */ |
2288 | case IFT_GIF: /* draft-ietf-v6ops-mech-v2-07 */ |
2289 | case IFT_LOOP: /* XXX: is this really correct? */ |
2290 | return 64; |
2291 | default: |
2292 | /* |
2293 | * Unknown link type: |
2294 | * It might be controversial to use the today's common constant |
2295 | * of 64 for these cases unconditionally. For full compliance, |
2296 | * we should return an error in this case. On the other hand, |
2297 | * if we simply miss the standard for the link type or a new |
2298 | * standard is defined for a new link type, the IFID length |
2299 | * is very likely to be the common constant. As a compromise, |
2300 | * we always use the constant, but make an explicit notice |
2301 | * indicating the "unknown" case. |
2302 | */ |
2303 | printf("in6_if2idlen: unknown link type (%d)\n" , ifp->if_type); |
2304 | return 64; |
2305 | } |
2306 | } |
2307 | |
2308 | struct in6_llentry { |
2309 | struct llentry base; |
2310 | }; |
2311 | |
2312 | #define IN6_LLTBL_DEFAULT_HSIZE 32 |
2313 | #define IN6_LLTBL_HASH(k, h) \ |
2314 | (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1)) |
2315 | |
2316 | /* |
2317 | * Do actual deallocation of @lle. |
2318 | * Called by LLE_FREE_LOCKED when number of references |
2319 | * drops to zero. |
2320 | */ |
2321 | static void |
2322 | in6_lltable_destroy_lle(struct llentry *lle) |
2323 | { |
2324 | |
2325 | LLE_WUNLOCK(lle); |
2326 | LLE_LOCK_DESTROY(lle); |
2327 | kmem_intr_free(lle, sizeof(struct in6_llentry)); |
2328 | } |
2329 | |
2330 | static struct llentry * |
2331 | in6_lltable_new(const struct in6_addr *addr6, u_int flags) |
2332 | { |
2333 | struct in6_llentry *lle; |
2334 | |
2335 | lle = kmem_intr_zalloc(sizeof(struct in6_llentry), KM_NOSLEEP); |
2336 | if (lle == NULL) /* NB: caller generates msg */ |
2337 | return NULL; |
2338 | |
2339 | lle->base.r_l3addr.addr6 = *addr6; |
2340 | lle->base.lle_refcnt = 1; |
2341 | lle->base.lle_free = in6_lltable_destroy_lle; |
2342 | LLE_LOCK_INIT(&lle->base); |
2343 | callout_init(&lle->base.lle_timer, CALLOUT_MPSAFE); |
2344 | |
2345 | return &lle->base; |
2346 | } |
2347 | |
2348 | static int |
2349 | in6_lltable_match_prefix(const struct sockaddr *prefix, |
2350 | const struct sockaddr *mask, u_int flags, struct llentry *lle) |
2351 | { |
2352 | const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix; |
2353 | const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask; |
2354 | |
2355 | if (IN6_ARE_MASKED_ADDR_EQUAL(&lle->r_l3addr.addr6, |
2356 | &pfx->sin6_addr, &msk->sin6_addr) && |
2357 | ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) |
2358 | return 1; |
2359 | |
2360 | return 0; |
2361 | } |
2362 | |
2363 | static void |
2364 | in6_lltable_free_entry(struct lltable *llt, struct llentry *lle) |
2365 | { |
2366 | struct ifnet *ifp __diagused; |
2367 | |
2368 | LLE_WLOCK_ASSERT(lle); |
2369 | KASSERT(llt != NULL); |
2370 | |
2371 | /* Unlink entry from table */ |
2372 | if ((lle->la_flags & LLE_LINKED) != 0) { |
2373 | |
2374 | ifp = llt->llt_ifp; |
2375 | IF_AFDATA_WLOCK_ASSERT(ifp); |
2376 | lltable_unlink_entry(llt, lle); |
2377 | } |
2378 | |
2379 | #ifdef NET_MPSAFE |
2380 | callout_halt(&lle->lle_timer, NULL); |
2381 | #else |
2382 | KASSERT(mutex_owned(softnet_lock)); |
2383 | callout_halt(&lle->lle_timer, softnet_lock); |
2384 | #endif |
2385 | LLE_REMREF(lle); |
2386 | |
2387 | llentry_free(lle); |
2388 | } |
2389 | |
2390 | static int |
2391 | in6_lltable_rtcheck(struct ifnet *ifp, |
2392 | u_int flags, |
2393 | const struct sockaddr *l3addr) |
2394 | { |
2395 | struct rtentry *rt; |
2396 | |
2397 | KASSERTMSG(l3addr->sa_family == AF_INET6, |
2398 | "sin_family %d" , l3addr->sa_family); |
2399 | |
2400 | rt = rtalloc1(l3addr, 0); |
2401 | if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { |
2402 | int s; |
2403 | struct ifaddr *ifa; |
2404 | /* |
2405 | * Create an ND6 cache for an IPv6 neighbor |
2406 | * that is not covered by our own prefix. |
2407 | */ |
2408 | /* XXX ifaof_ifpforaddr should take a const param */ |
2409 | s = pserialize_read_enter(); |
2410 | ifa = ifaof_ifpforaddr(l3addr, ifp); |
2411 | if (ifa != NULL) { |
2412 | pserialize_read_exit(s); |
2413 | if (rt != NULL) |
2414 | rtfree(rt); |
2415 | return 0; |
2416 | } |
2417 | pserialize_read_exit(s); |
2418 | log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n" , |
2419 | ip6_sprintf(&((const struct sockaddr_in6 *)l3addr)->sin6_addr)); |
2420 | if (rt != NULL) |
2421 | rtfree(rt); |
2422 | return EINVAL; |
2423 | } |
2424 | rtfree(rt); |
2425 | return 0; |
2426 | } |
2427 | |
2428 | static inline uint32_t |
2429 | in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize) |
2430 | { |
2431 | |
2432 | return IN6_LLTBL_HASH(dst->s6_addr32[3], hsize); |
2433 | } |
2434 | |
2435 | static uint32_t |
2436 | in6_lltable_hash(const struct llentry *lle, uint32_t hsize) |
2437 | { |
2438 | |
2439 | return in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize); |
2440 | } |
2441 | |
2442 | static void |
2443 | in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa) |
2444 | { |
2445 | struct sockaddr_in6 *sin6; |
2446 | |
2447 | sin6 = (struct sockaddr_in6 *)sa; |
2448 | bzero(sin6, sizeof(*sin6)); |
2449 | sin6->sin6_family = AF_INET6; |
2450 | sin6->sin6_len = sizeof(*sin6); |
2451 | sin6->sin6_addr = lle->r_l3addr.addr6; |
2452 | } |
2453 | |
2454 | static inline struct llentry * |
2455 | in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst) |
2456 | { |
2457 | struct llentry *lle; |
2458 | struct llentries *lleh; |
2459 | u_int hashidx; |
2460 | |
2461 | hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize); |
2462 | lleh = &llt->lle_head[hashidx]; |
2463 | LIST_FOREACH(lle, lleh, lle_next) { |
2464 | if (lle->la_flags & LLE_DELETED) |
2465 | continue; |
2466 | if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst)) |
2467 | break; |
2468 | } |
2469 | |
2470 | return lle; |
2471 | } |
2472 | |
2473 | static int |
2474 | in6_lltable_delete(struct lltable *llt, u_int flags, |
2475 | const struct sockaddr *l3addr) |
2476 | { |
2477 | const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; |
2478 | struct llentry *lle; |
2479 | |
2480 | IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp); |
2481 | KASSERTMSG(l3addr->sa_family == AF_INET6, |
2482 | "sin_family %d" , l3addr->sa_family); |
2483 | |
2484 | lle = in6_lltable_find_dst(llt, &sin6->sin6_addr); |
2485 | |
2486 | if (lle == NULL) |
2487 | return ENOENT; |
2488 | |
2489 | LLE_WLOCK(lle); |
2490 | lle->la_flags |= LLE_DELETED; |
2491 | #ifdef DIAGNOSTIC |
2492 | log(LOG_INFO, "ifaddr cache = %p is deleted\n" , lle); |
2493 | #endif |
2494 | if ((lle->la_flags & (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC) |
2495 | llentry_free(lle); |
2496 | else |
2497 | LLE_WUNLOCK(lle); |
2498 | |
2499 | return 0; |
2500 | } |
2501 | |
2502 | static struct llentry * |
2503 | in6_lltable_create(struct lltable *llt, u_int flags, |
2504 | const struct sockaddr *l3addr) |
2505 | { |
2506 | const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; |
2507 | struct ifnet *ifp = llt->llt_ifp; |
2508 | struct llentry *lle; |
2509 | |
2510 | IF_AFDATA_WLOCK_ASSERT(ifp); |
2511 | KASSERTMSG(l3addr->sa_family == AF_INET6, |
2512 | "sin_family %d" , l3addr->sa_family); |
2513 | |
2514 | lle = in6_lltable_find_dst(llt, &sin6->sin6_addr); |
2515 | |
2516 | if (lle != NULL) { |
2517 | LLE_WLOCK(lle); |
2518 | return lle; |
2519 | } |
2520 | |
2521 | /* |
2522 | * A route that covers the given address must have |
2523 | * been installed 1st because we are doing a resolution, |
2524 | * verify this. |
2525 | */ |
2526 | if (!(flags & LLE_IFADDR) && |
2527 | in6_lltable_rtcheck(ifp, flags, l3addr) != 0) |
2528 | return NULL; |
2529 | |
2530 | lle = in6_lltable_new(&sin6->sin6_addr, flags); |
2531 | if (lle == NULL) { |
2532 | log(LOG_INFO, "lla_lookup: new lle malloc failed\n" ); |
2533 | return NULL; |
2534 | } |
2535 | lle->la_flags = flags; |
2536 | if ((flags & LLE_IFADDR) == LLE_IFADDR) { |
2537 | memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen); |
2538 | lle->la_flags |= LLE_VALID; |
2539 | } |
2540 | |
2541 | lltable_link_entry(llt, lle); |
2542 | LLE_WLOCK(lle); |
2543 | |
2544 | return lle; |
2545 | } |
2546 | |
2547 | static struct llentry * |
2548 | in6_lltable_lookup(struct lltable *llt, u_int flags, |
2549 | const struct sockaddr *l3addr) |
2550 | { |
2551 | const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; |
2552 | struct llentry *lle; |
2553 | |
2554 | IF_AFDATA_LOCK_ASSERT(llt->llt_ifp); |
2555 | KASSERTMSG(l3addr->sa_family == AF_INET6, |
2556 | "sin_family %d" , l3addr->sa_family); |
2557 | |
2558 | lle = in6_lltable_find_dst(llt, &sin6->sin6_addr); |
2559 | |
2560 | if (lle == NULL) |
2561 | return NULL; |
2562 | |
2563 | if (flags & LLE_EXCLUSIVE) |
2564 | LLE_WLOCK(lle); |
2565 | else |
2566 | LLE_RLOCK(lle); |
2567 | return lle; |
2568 | } |
2569 | |
2570 | static int |
2571 | in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle, |
2572 | struct rt_walkarg *w) |
2573 | { |
2574 | struct sockaddr_in6 sin6; |
2575 | |
2576 | LLTABLE_LOCK_ASSERT(); |
2577 | |
2578 | /* skip deleted entries */ |
2579 | if (lle->la_flags & LLE_DELETED) |
2580 | return 0; |
2581 | |
2582 | sockaddr_in6_init(&sin6, &lle->r_l3addr.addr6, 0, 0, 0); |
2583 | |
2584 | return lltable_dump_entry(llt, lle, w, sin6tosa(&sin6)); |
2585 | } |
2586 | |
2587 | static struct lltable * |
2588 | in6_lltattach(struct ifnet *ifp) |
2589 | { |
2590 | struct lltable *llt; |
2591 | |
2592 | llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE); |
2593 | llt->llt_af = AF_INET6; |
2594 | llt->llt_ifp = ifp; |
2595 | |
2596 | llt->llt_lookup = in6_lltable_lookup; |
2597 | llt->llt_create = in6_lltable_create; |
2598 | llt->llt_delete = in6_lltable_delete; |
2599 | llt->llt_dump_entry = in6_lltable_dump_entry; |
2600 | llt->llt_hash = in6_lltable_hash; |
2601 | llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry; |
2602 | llt->llt_free_entry = in6_lltable_free_entry; |
2603 | llt->llt_match_prefix = in6_lltable_match_prefix; |
2604 | lltable_link(llt); |
2605 | |
2606 | return llt; |
2607 | } |
2608 | |
2609 | void * |
2610 | in6_domifattach(struct ifnet *ifp) |
2611 | { |
2612 | struct in6_ifextra *ext; |
2613 | |
2614 | ext = malloc(sizeof(*ext), M_IFADDR, M_WAITOK|M_ZERO); |
2615 | |
2616 | ext->in6_ifstat = malloc(sizeof(struct in6_ifstat), |
2617 | M_IFADDR, M_WAITOK|M_ZERO); |
2618 | |
2619 | ext->icmp6_ifstat = malloc(sizeof(struct icmp6_ifstat), |
2620 | M_IFADDR, M_WAITOK|M_ZERO); |
2621 | |
2622 | ext->nd_ifinfo = nd6_ifattach(ifp); |
2623 | ext->scope6_id = scope6_ifattach(ifp); |
2624 | ext->nprefixes = 0; |
2625 | ext->ndefrouters = 0; |
2626 | |
2627 | ext->lltable = in6_lltattach(ifp); |
2628 | |
2629 | return ext; |
2630 | } |
2631 | |
2632 | void |
2633 | in6_domifdetach(struct ifnet *ifp, void *aux) |
2634 | { |
2635 | struct in6_ifextra *ext = (struct in6_ifextra *)aux; |
2636 | |
2637 | lltable_free(ext->lltable); |
2638 | ext->lltable = NULL; |
2639 | nd6_ifdetach(ifp, ext); |
2640 | free(ext->in6_ifstat, M_IFADDR); |
2641 | free(ext->icmp6_ifstat, M_IFADDR); |
2642 | scope6_ifdetach(ext->scope6_id); |
2643 | free(ext, M_IFADDR); |
2644 | } |
2645 | |
2646 | /* |
2647 | * Convert IPv4 address stored in struct in_addr to IPv4-Mapped IPv6 address |
2648 | * stored in struct in6_addr as defined in RFC 4921 section 2.5.5.2. |
2649 | */ |
2650 | void |
2651 | in6_in_2_v4mapin6(const struct in_addr *in, struct in6_addr *in6) |
2652 | { |
2653 | in6->s6_addr32[0] = 0; |
2654 | in6->s6_addr32[1] = 0; |
2655 | in6->s6_addr32[2] = IPV6_ADDR_INT32_SMP; |
2656 | in6->s6_addr32[3] = in->s_addr; |
2657 | } |
2658 | |
2659 | /* |
2660 | * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be |
2661 | * v4 mapped addr or v4 compat addr |
2662 | */ |
2663 | void |
2664 | in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) |
2665 | { |
2666 | memset(sin, 0, sizeof(*sin)); |
2667 | sin->sin_len = sizeof(struct sockaddr_in); |
2668 | sin->sin_family = AF_INET; |
2669 | sin->sin_port = sin6->sin6_port; |
2670 | sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; |
2671 | } |
2672 | |
2673 | /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ |
2674 | void |
2675 | in6_sin_2_v4mapsin6(const struct sockaddr_in *sin, struct sockaddr_in6 *sin6) |
2676 | { |
2677 | memset(sin6, 0, sizeof(*sin6)); |
2678 | sin6->sin6_len = sizeof(struct sockaddr_in6); |
2679 | sin6->sin6_family = AF_INET6; |
2680 | sin6->sin6_port = sin->sin_port; |
2681 | in6_in_2_v4mapin6(&sin->sin_addr, &sin6->sin6_addr); |
2682 | } |
2683 | |
2684 | /* Convert sockaddr_in6 into sockaddr_in. */ |
2685 | void |
2686 | in6_sin6_2_sin_in_sock(struct sockaddr *nam) |
2687 | { |
2688 | struct sockaddr_in *sin_p; |
2689 | struct sockaddr_in6 sin6; |
2690 | |
2691 | /* |
2692 | * Save original sockaddr_in6 addr and convert it |
2693 | * to sockaddr_in. |
2694 | */ |
2695 | sin6 = *(struct sockaddr_in6 *)nam; |
2696 | sin_p = (struct sockaddr_in *)nam; |
2697 | in6_sin6_2_sin(sin_p, &sin6); |
2698 | } |
2699 | |
2700 | /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ |
2701 | void |
2702 | in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) |
2703 | { |
2704 | struct sockaddr_in *sin_p; |
2705 | struct sockaddr_in6 *sin6_p; |
2706 | |
2707 | sin6_p = malloc(sizeof(*sin6_p), M_SONAME, M_WAITOK); |
2708 | sin_p = (struct sockaddr_in *)*nam; |
2709 | in6_sin_2_v4mapsin6(sin_p, sin6_p); |
2710 | free(*nam, M_SONAME); |
2711 | *nam = sin6tosa(sin6_p); |
2712 | } |
2713 | |