| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: af_inet6.c,v 1.36 2016/09/13 00:20:51 christos Exp $ */ | | 1 | /* $NetBSD: af_inet6.c,v 1.37 2016/09/30 16:47:56 roy Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1983, 1993 | | 4 | * Copyright (c) 1983, 1993 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -21,27 +21,27 @@ | | | @@ -21,27 +21,27 @@ |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 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 | | 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 | | 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 | | 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. | | 29 | * SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #include <sys/cdefs.h> | | 32 | #include <sys/cdefs.h> |
33 | #ifndef lint | | 33 | #ifndef lint |
34 | __RCSID("$NetBSD: af_inet6.c,v 1.36 2016/09/13 00:20:51 christos Exp $"); | | 34 | __RCSID("$NetBSD: af_inet6.c,v 1.37 2016/09/30 16:47:56 roy Exp $"); |
35 | #endif /* not lint */ | | 35 | #endif /* not lint */ |
36 | | | 36 | |
37 | #include <sys/param.h> | | 37 | #include <sys/param.h> |
38 | #include <sys/ioctl.h> | | 38 | #include <sys/ioctl.h> |
39 | #include <sys/socket.h> | | 39 | #include <sys/socket.h> |
40 | | | 40 | |
41 | #include <net/if.h> | | 41 | #include <net/if.h> |
42 | #include <netinet/in.h> | | 42 | #include <netinet/in.h> |
43 | #include <netinet/in_var.h> | | 43 | #include <netinet/in_var.h> |
44 | #include <netinet6/nd6.h> | | 44 | #include <netinet6/nd6.h> |
45 | | | 45 | |
46 | #include <err.h> | | 46 | #include <err.h> |
47 | #include <errno.h> | | 47 | #include <errno.h> |
| @@ -50,39 +50,37 @@ __RCSID("$NetBSD: af_inet6.c,v 1.36 2016 | | | @@ -50,39 +50,37 @@ __RCSID("$NetBSD: af_inet6.c,v 1.36 2016 |
50 | #include <string.h> | | 50 | #include <string.h> |
51 | #include <stdlib.h> | | 51 | #include <stdlib.h> |
52 | #include <stdio.h> | | 52 | #include <stdio.h> |
53 | #include <util.h> | | 53 | #include <util.h> |
54 | | | 54 | |
55 | #include "env.h" | | 55 | #include "env.h" |
56 | #include "extern.h" | | 56 | #include "extern.h" |
57 | #include "parse.h" | | 57 | #include "parse.h" |
58 | #include "extern.h" | | 58 | #include "extern.h" |
59 | #include "af_inetany.h" | | 59 | #include "af_inetany.h" |
60 | #include "prog_ops.h" | | 60 | #include "prog_ops.h" |
61 | | | 61 | |
62 | static void in6_constructor(void) __attribute__((constructor)); | | 62 | static void in6_constructor(void) __attribute__((constructor)); |
63 | static void in6_alias(const char *, prop_dictionary_t, prop_dictionary_t, | | 63 | static void in6_alias(struct ifaddrs *, prop_dictionary_t, prop_dictionary_t); |
64 | struct in6_ifreq *); | | | |
65 | static void in6_commit_address(prop_dictionary_t, prop_dictionary_t); | | 64 | static void in6_commit_address(prop_dictionary_t, prop_dictionary_t); |
66 | | | 65 | |
67 | static int setia6eui64_impl(prop_dictionary_t, struct in6_aliasreq *); | | 66 | static int setia6eui64_impl(prop_dictionary_t, struct in6_aliasreq *); |
68 | static int setia6flags_impl(prop_dictionary_t, struct in6_aliasreq *); | | 67 | static int setia6flags_impl(prop_dictionary_t, struct in6_aliasreq *); |
69 | static int setia6pltime_impl(prop_dictionary_t, struct in6_aliasreq *); | | 68 | static int setia6pltime_impl(prop_dictionary_t, struct in6_aliasreq *); |
70 | static int setia6vltime_impl(prop_dictionary_t, struct in6_aliasreq *); | | 69 | static int setia6vltime_impl(prop_dictionary_t, struct in6_aliasreq *); |
71 | | | 70 | |
72 | static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *); | | 71 | static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *); |
73 | | | 72 | |
74 | static void in6_status(prop_dictionary_t, prop_dictionary_t, bool); | | 73 | static void in6_status(prop_dictionary_t, prop_dictionary_t, bool); |
75 | static bool in6_addr_flags(struct ifaddrs *ifa, int); | | | |
76 | static bool in6_addr_tentative(struct ifaddrs *ifa); | | 74 | static bool in6_addr_tentative(struct ifaddrs *ifa); |
77 | static bool in6_addr_tentative_or_detached(struct ifaddrs *ifa); | | 75 | static bool in6_addr_tentative_or_detached(struct ifaddrs *ifa); |
78 | | | 76 | |
79 | static struct usage_func usage; | | 77 | static struct usage_func usage; |
80 | static cmdloop_branch_t branch[2]; | | 78 | static cmdloop_branch_t branch[2]; |
81 | | | 79 | |
82 | static const struct kwinst ia6flagskw[] = { | | 80 | static const struct kwinst ia6flagskw[] = { |
83 | IFKW("anycast", IN6_IFF_ANYCAST) | | 81 | IFKW("anycast", IN6_IFF_ANYCAST) |
84 | , IFKW("deprecated", IN6_IFF_DEPRECATED) | | 82 | , IFKW("deprecated", IN6_IFF_DEPRECATED) |
85 | }; | | 83 | }; |
86 | | | 84 | |
87 | static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime, | | 85 | static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime, |
88 | "pltime", 0, NULL, "pltime", &command_root.pb_parser); | | 86 | "pltime", 0, NULL, "pltime", &command_root.pb_parser); |
| @@ -252,101 +250,77 @@ setia6eui64_impl(prop_dictionary_t env, | | | @@ -252,101 +250,77 @@ setia6eui64_impl(prop_dictionary_t env, |
252 | } | | 250 | } |
253 | } | | 251 | } |
254 | if (lladdr == NULL) | | 252 | if (lladdr == NULL) |
255 | errx(EXIT_FAILURE, "could not determine link local address"); | | 253 | errx(EXIT_FAILURE, "could not determine link local address"); |
256 | | | 254 | |
257 | memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8); | | 255 | memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8); |
258 | | | 256 | |
259 | freeifaddrs(ifap); | | 257 | freeifaddrs(ifap); |
260 | return 0; | | 258 | return 0; |
261 | } | | 259 | } |
262 | | | 260 | |
263 | /* XXX not really an alias */ | | 261 | /* XXX not really an alias */ |
264 | void | | 262 | void |
265 | in6_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv, | | 263 | in6_alias(struct ifaddrs *ifa, prop_dictionary_t env, prop_dictionary_t oenv) |
266 | struct in6_ifreq *creq) | | | |
267 | { | | 264 | { |
268 | struct in6_ifreq ifr6; | | | |
269 | struct sockaddr_in6 *sin6; | | 265 | struct sockaddr_in6 *sin6; |
270 | char hbuf[NI_MAXHOST]; | | 266 | char hbuf[NI_MAXHOST]; |
271 | u_int32_t scopeid; | | 267 | u_int32_t scopeid; |
272 | int s; | | | |
273 | const int niflag = Nflag ? 0 : NI_NUMERICHOST; | | 268 | const int niflag = Nflag ? 0 : NI_NUMERICHOST; |
274 | unsigned short flags; | | 269 | char fbuf[1024]; |
275 | | | | |
276 | /* Get the non-alias address for this interface. */ | | | |
277 | if ((s = getsock(AF_INET6)) == -1) { | | | |
278 | if (errno == EAFNOSUPPORT) | | | |
279 | return; | | | |
280 | err(EXIT_FAILURE, "socket"); | | | |
281 | } | | | |
282 | | | | |
283 | sin6 = &creq->ifr_addr; | | | |
284 | | | 270 | |
| | | 271 | sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; |
285 | inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL); | | 272 | inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL); |
286 | scopeid = sin6->sin6_scope_id; | | 273 | scopeid = sin6->sin6_scope_id; |
287 | if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len, | | 274 | if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len, |
288 | hbuf, sizeof(hbuf), NULL, 0, niflag)) | | 275 | hbuf, sizeof(hbuf), NULL, 0, niflag)) |
289 | strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */ | | 276 | strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */ |
290 | printf("\tinet6 %s", hbuf); | | 277 | printf("\tinet6 %s", hbuf); |
| | | 278 | inet6_putscopeid(sin6, INET6_IS_ADDR_LINKLOCAL); |
291 | | | 279 | |
292 | if (getifflags(env, oenv, &flags) == -1) | | 280 | if (ifa->ifa_flags & IFF_POINTOPOINT) { |
293 | err(EXIT_FAILURE, "%s: getifflags", __func__); | | 281 | sin6 = (struct sockaddr_in6 *)ifa->ifa_dstaddr; |
294 | | | | |
295 | if (flags & IFF_POINTOPOINT) { | | | |
296 | ifr6 = *creq; | | | |
297 | if (prog_ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) { | | | |
298 | if (errno != EADDRNOTAVAIL) | | | |
299 | warn("SIOCGIFDSTADDR_IN6"); | | | |
300 | memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr)); | | | |
301 | ifr6.ifr_addr.sin6_family = AF_INET6; | | | |
302 | ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6); | | | |
303 | } | | | |
304 | sin6 = &ifr6.ifr_addr; | | | |
305 | inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL); | | 282 | inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL); |
306 | hbuf[0] = '\0'; | | 283 | hbuf[0] = '\0'; |
307 | if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len, | | 284 | if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len, |
308 | hbuf, sizeof(hbuf), NULL, 0, niflag)) | | 285 | hbuf, sizeof(hbuf), NULL, 0, niflag)) |
309 | strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */ | | 286 | strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */ |
310 | printf(" -> %s", hbuf); | | 287 | printf(" -> %s", hbuf); |
311 | } | | 288 | } |
312 | | | 289 | |
313 | ifr6 = *creq; | | 290 | sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask; |
314 | if (prog_ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) { | | 291 | printf(" prefixlen %d", prefix(&sin6->sin6_addr, |
315 | if (errno != EADDRNOTAVAIL) | | 292 | sizeof(struct in6_addr))); |
316 | warn("SIOCGIFNETMASK_IN6"); | | 293 | |
317 | } else { | | 294 | (void)snprintb(fbuf, sizeof(fbuf), IN6_IFFBITS, ifa->ifa_addrflags); |
318 | sin6 = &ifr6.ifr_addr; | | 295 | printf(" flags %s", fbuf); |
319 | printf(" prefixlen %d", prefix(&sin6->sin6_addr, | | | |
320 | sizeof(struct in6_addr))); | | | |
321 | } | | | |
322 | | | | |
323 | ifr6 = *creq; | | | |
324 | if (prog_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) { | | | |
325 | if (errno != EADDRNOTAVAIL) | | | |
326 | warn("SIOCGIFAFLAG_IN6"); | | | |
327 | } else { | | | |
328 | char fbuf[1024]; | | | |
329 | (void)snprintb(fbuf, sizeof(fbuf), IN6_IFFBITS, | | | |
330 | ifr6.ifr_ifru.ifru_flags6); | | | |
331 | printf(" flags %s", fbuf); | | | |
332 | } | | | |
333 | | | 296 | |
334 | if (scopeid) | | 297 | if (scopeid) |
335 | printf(" scopeid 0x%x", scopeid); | | 298 | printf(" scopeid 0x%x", scopeid); |
336 | | | 299 | |
337 | if (get_flag('L')) { | | 300 | if (get_flag('L')) { |
| | | 301 | int s; |
| | | 302 | struct in6_ifreq ifr6; |
338 | struct in6_addrlifetime *lifetime; | | 303 | struct in6_addrlifetime *lifetime; |
339 | ifr6 = *creq; | | 304 | |
| | | 305 | if ((s = getsock(AF_INET6)) == -1) { |
| | | 306 | if (errno == EAFNOSUPPORT) |
| | | 307 | return; |
| | | 308 | err(EXIT_FAILURE, "socket"); |
| | | 309 | } |
| | | 310 | |
| | | 311 | memset(&ifr6, 0, sizeof(ifr6)); |
| | | 312 | estrlcpy(ifr6.ifr_name, ifa->ifa_name, sizeof(ifr6.ifr_name)); |
| | | 313 | memcpy(&ifr6.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len); |
340 | lifetime = &ifr6.ifr_ifru.ifru_lifetime; | | 314 | lifetime = &ifr6.ifr_ifru.ifru_lifetime; |
341 | if (prog_ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) { | | 315 | if (prog_ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) { |
342 | if (errno != EADDRNOTAVAIL) | | 316 | if (errno != EADDRNOTAVAIL) |
343 | warn("SIOCGIFALIFETIME_IN6"); | | 317 | warn("SIOCGIFALIFETIME_IN6"); |
344 | } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) { | | 318 | } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) { |
345 | time_t t = time(NULL); | | 319 | time_t t = time(NULL); |
346 | printf(" pltime "); | | 320 | printf(" pltime "); |
347 | if (lifetime->ia6t_preferred) { | | 321 | if (lifetime->ia6t_preferred) { |
348 | printf("%lu", | | 322 | printf("%lu", |
349 | (unsigned long)(lifetime->ia6t_preferred - | | 323 | (unsigned long)(lifetime->ia6t_preferred - |
350 | MIN(t, lifetime->ia6t_preferred))); | | 324 | MIN(t, lifetime->ia6t_preferred))); |
351 | } else | | 325 | } else |
352 | printf("infty"); | | 326 | printf("infty"); |
| @@ -356,48 +330,41 @@ in6_alias(const char *ifname, prop_dicti | | | @@ -356,48 +330,41 @@ in6_alias(const char *ifname, prop_dicti |
356 | printf("%lu", | | 330 | printf("%lu", |
357 | (unsigned long)(lifetime->ia6t_expire - | | 331 | (unsigned long)(lifetime->ia6t_expire - |
358 | MIN(t, lifetime->ia6t_expire))); | | 332 | MIN(t, lifetime->ia6t_expire))); |
359 | } else | | 333 | } else |
360 | printf("infty"); | | 334 | printf("infty"); |
361 | } | | 335 | } |
362 | } | | 336 | } |
363 | } | | 337 | } |
364 | | | 338 | |
365 | static void | | 339 | static void |
366 | in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force) | | 340 | in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force) |
367 | { | | 341 | { |
368 | struct ifaddrs *ifap, *ifa; | | 342 | struct ifaddrs *ifap, *ifa; |
369 | struct in6_ifreq ifr; | | | |
370 | const char *ifname; | | 343 | const char *ifname; |
371 | bool printprefs = false; | | 344 | bool printprefs = false; |
372 | | | 345 | |
373 | if ((ifname = getifname(env)) == NULL) | | 346 | if ((ifname = getifname(env)) == NULL) |
374 | err(EXIT_FAILURE, "%s: getifname", __func__); | | 347 | err(EXIT_FAILURE, "%s: getifname", __func__); |
375 | | | 348 | |
376 | if (getifaddrs(&ifap) != 0) | | 349 | if (getifaddrs(&ifap) != 0) |
377 | err(EXIT_FAILURE, "getifaddrs"); | | 350 | err(EXIT_FAILURE, "getifaddrs"); |
378 | printprefs = ifa_any_preferences(ifname, ifap, AF_INET6); | | 351 | printprefs = ifa_any_preferences(ifname, ifap, AF_INET6); |
379 | for (ifa = ifap; ifa; ifa = ifa->ifa_next) { | | 352 | for (ifa = ifap; ifa; ifa = ifa->ifa_next) { |
380 | if (strcmp(ifname, ifa->ifa_name) != 0) | | 353 | if (strcmp(ifname, ifa->ifa_name) != 0) |
381 | continue; | | 354 | continue; |
382 | if (ifa->ifa_addr->sa_family != AF_INET6) | | 355 | if (ifa->ifa_addr->sa_family != AF_INET6) |
383 | continue; | | 356 | continue; |
384 | if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len) | | 357 | in6_alias(ifa, env, oenv); |
385 | continue; | | | |
386 | | | | |
387 | memset(&ifr, 0, sizeof(ifr)); | | | |
388 | estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name)); | | | |
389 | memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len); | | | |
390 | in6_alias(ifname, env, oenv, &ifr); | | | |
391 | if (printprefs) | | 358 | if (printprefs) |
392 | ifa_print_preference(ifa->ifa_name, ifa->ifa_addr); | | 359 | ifa_print_preference(ifa->ifa_name, ifa->ifa_addr); |
393 | printf("\n"); | | 360 | printf("\n"); |
394 | } | | 361 | } |
395 | freeifaddrs(ifap); | | 362 | freeifaddrs(ifap); |
396 | } | | 363 | } |
397 | | | 364 | |
398 | static int | | 365 | static int |
399 | in6_pre_aifaddr(prop_dictionary_t env, const struct afparam *param) | | 366 | in6_pre_aifaddr(prop_dictionary_t env, const struct afparam *param) |
400 | { | | 367 | { |
401 | struct in6_aliasreq *ifra = param->req.buf; | | 368 | struct in6_aliasreq *ifra = param->req.buf; |
402 | | | 369 | |
403 | setia6eui64_impl(env, ifra); | | 370 | setia6eui64_impl(env, ifra); |