kernel/arm: send SIGSEGV to processes
[minix3.git] / sbin / ifconfig / af_inet6.c
blobe5474105a8694c0207d2b3bc1b7248e90df9f025
1 /* $NetBSD: af_inet6.c,v 1.33 2015/05/12 14:05:29 roy Exp $ */
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: af_inet6.c,v 1.33 2015/05/12 14:05:29 roy Exp $");
35 #endif /* not lint */
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/in_var.h>
44 #include <netinet6/nd6.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <ifaddrs.h>
49 #include <netdb.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <util.h>
55 #include "env.h"
56 #include "extern.h"
57 #include "parse.h"
58 #include "extern.h"
59 #include "af_inetany.h"
60 #include "prog_ops.h"
62 static void in6_constructor(void) __attribute__((constructor));
63 static void in6_alias(const char *, prop_dictionary_t, prop_dictionary_t,
64 struct in6_ifreq *);
65 static void in6_commit_address(prop_dictionary_t, prop_dictionary_t);
67 static int setia6eui64_impl(prop_dictionary_t, struct in6_aliasreq *);
68 static int setia6flags_impl(prop_dictionary_t, struct in6_aliasreq *);
69 static int setia6pltime_impl(prop_dictionary_t, struct in6_aliasreq *);
70 static int setia6vltime_impl(prop_dictionary_t, struct in6_aliasreq *);
72 static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *);
74 static void in6_status(prop_dictionary_t, prop_dictionary_t, bool);
75 static bool in6_addr_tentative(struct ifaddrs *ifa);
77 static struct usage_func usage;
78 static cmdloop_branch_t branch[2];
80 static const struct kwinst ia6flagskw[] = {
81 IFKW("anycast", IN6_IFF_ANYCAST)
82 , IFKW("deprecated", IN6_IFF_DEPRECATED)
85 static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime,
86 "pltime", 0, NULL, "pltime", &command_root.pb_parser);
88 static struct pinteger parse_vltime = PINTEGER_INITIALIZER(&parse_vltime,
89 "vltime", 0, NULL, "vltime", &command_root.pb_parser);
91 static const struct kwinst inet6kw[] = {
92 {.k_word = "pltime", .k_nextparser = &parse_pltime.pi_parser}
93 , {.k_word = "vltime", .k_nextparser = &parse_vltime.pi_parser}
94 , {.k_word = "eui64", .k_key = "eui64", .k_type = KW_T_BOOL,
95 .k_bool = true, .k_nextparser = &command_root.pb_parser}
98 struct pkw ia6flags = PKW_INITIALIZER(&ia6flags, "ia6flags", NULL,
99 "ia6flag", ia6flagskw, __arraycount(ia6flagskw), &command_root.pb_parser);
100 struct pkw inet6 = PKW_INITIALIZER(&inet6, "IPv6 keywords", NULL,
101 NULL, inet6kw, __arraycount(inet6kw), NULL);
103 static struct afswtch in6af = {
104 .af_name = "inet6", .af_af = AF_INET6, .af_status = in6_status,
105 .af_addr_commit = in6_commit_address,
106 .af_addr_tentative = in6_addr_tentative
109 static int
110 prefix(void *val, int size)
112 u_char *pname = (u_char *)val;
113 int byte, bit, plen = 0;
115 for (byte = 0; byte < size; byte++, plen += 8)
116 if (pname[byte] != 0xff)
117 break;
118 if (byte == size)
119 return (plen);
120 for (bit = 7; bit != 0; bit--, plen++)
121 if (!(pname[byte] & (1 << bit)))
122 break;
123 for (; bit != 0; bit--)
124 if (pname[byte] & (1 << bit))
125 return(0);
126 byte++;
127 for (; byte < size; byte++)
128 if (pname[byte])
129 return(0);
130 return (plen);
134 setia6flags_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
136 int64_t ia6flag;
138 if (!prop_dictionary_get_int64(env, "ia6flag", &ia6flag)) {
139 errno = ENOENT;
140 return -1;
143 if (ia6flag < 0) {
144 ia6flag = -ia6flag;
145 ifra->ifra_flags &= ~ia6flag;
146 } else
147 ifra->ifra_flags |= ia6flag;
148 return 0;
152 setia6pltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
154 int64_t pltime;
156 if (!prop_dictionary_get_int64(env, "pltime", &pltime)) {
157 errno = ENOENT;
158 return -1;
161 return setia6lifetime(env, pltime,
162 &ifra->ifra_lifetime.ia6t_preferred,
163 &ifra->ifra_lifetime.ia6t_pltime);
167 setia6vltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
169 int64_t vltime;
171 if (!prop_dictionary_get_int64(env, "vltime", &vltime)) {
172 errno = ENOENT;
173 return -1;
176 return setia6lifetime(env, vltime,
177 &ifra->ifra_lifetime.ia6t_expire,
178 &ifra->ifra_lifetime.ia6t_vltime);
181 static int
182 setia6lifetime(prop_dictionary_t env, int64_t val, time_t *timep,
183 uint32_t *ivalp)
185 time_t t;
186 int af;
188 if ((af = getaf(env)) == -1 || af != AF_INET6) {
189 errx(EXIT_FAILURE,
190 "inet6 address lifetime not allowed for the AF");
193 t = time(NULL);
194 *timep = t + val;
195 *ivalp = val;
196 return 0;
200 setia6eui64_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
202 char buf[2][80];
203 struct ifaddrs *ifap, *ifa;
204 const struct sockaddr_in6 *sin6 = NULL;
205 const struct in6_addr *lladdr = NULL;
206 struct in6_addr *in6;
207 const char *ifname;
208 bool doit = false;
209 int af;
211 if (!prop_dictionary_get_bool(env, "eui64", &doit) || !doit) {
212 errno = ENOENT;
213 return -1;
216 if ((ifname = getifname(env)) == NULL)
217 return -1;
219 af = getaf(env);
220 if (af != AF_INET6) {
221 errx(EXIT_FAILURE,
222 "eui64 address modifier not allowed for the AF");
224 in6 = &ifra->ifra_addr.sin6_addr;
225 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) {
226 union {
227 struct sockaddr_in6 sin6;
228 struct sockaddr sa;
229 } any = {.sin6 = {.sin6_family = AF_INET6}};
230 memcpy(&any.sin6.sin6_addr, &in6addr_any,
231 sizeof(any.sin6.sin6_addr));
232 (void)sockaddr_snprintf(buf[0], sizeof(buf[0]), "%a%%S",
233 &any.sa);
234 (void)sockaddr_snprintf(buf[1], sizeof(buf[1]), "%a%%S",
235 (const struct sockaddr *)&ifra->ifra_addr);
236 errx(EXIT_FAILURE, "interface index is already filled, %s | %s",
237 buf[0], buf[1]);
239 if (getifaddrs(&ifap) != 0)
240 err(EXIT_FAILURE, "getifaddrs");
241 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
242 if (ifa->ifa_addr->sa_family == AF_INET6 &&
243 strcmp(ifa->ifa_name, ifname) == 0) {
244 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
245 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
246 lladdr = &sin6->sin6_addr;
247 break;
251 if (lladdr == NULL)
252 errx(EXIT_FAILURE, "could not determine link local address");
254 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
256 freeifaddrs(ifap);
257 return 0;
260 /* XXX not really an alias */
261 void
262 in6_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
263 struct in6_ifreq *creq)
265 struct in6_ifreq ifr6;
266 struct sockaddr_in6 *sin6;
267 char hbuf[NI_MAXHOST];
268 u_int32_t scopeid;
269 int s;
270 const int niflag = Nflag ? 0 : NI_NUMERICHOST;
271 unsigned short flags;
273 /* Get the non-alias address for this interface. */
274 if ((s = getsock(AF_INET6)) == -1) {
275 if (errno == EAFNOSUPPORT)
276 return;
277 err(EXIT_FAILURE, "socket");
280 sin6 = &creq->ifr_addr;
282 inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
283 scopeid = sin6->sin6_scope_id;
284 if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len,
285 hbuf, sizeof(hbuf), NULL, 0, niflag))
286 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
287 printf("\tinet6 %s", hbuf);
289 if (getifflags(env, oenv, &flags) == -1)
290 err(EXIT_FAILURE, "%s: getifflags", __func__);
292 if (flags & IFF_POINTOPOINT) {
293 ifr6 = *creq;
294 if (prog_ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
295 if (errno != EADDRNOTAVAIL)
296 warn("SIOCGIFDSTADDR_IN6");
297 memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
298 ifr6.ifr_addr.sin6_family = AF_INET6;
299 ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
301 sin6 = &ifr6.ifr_addr;
302 inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
303 hbuf[0] = '\0';
304 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
305 hbuf, sizeof(hbuf), NULL, 0, niflag))
306 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
307 printf(" -> %s", hbuf);
310 ifr6 = *creq;
311 if (prog_ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
312 if (errno != EADDRNOTAVAIL)
313 warn("SIOCGIFNETMASK_IN6");
314 } else {
315 sin6 = &ifr6.ifr_addr;
316 printf(" prefixlen %d", prefix(&sin6->sin6_addr,
317 sizeof(struct in6_addr)));
320 ifr6 = *creq;
321 if (prog_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
322 if (errno != EADDRNOTAVAIL)
323 warn("SIOCGIFAFLAG_IN6");
324 } else {
325 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
326 printf(" anycast");
327 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
328 printf(" tentative");
329 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
330 printf(" duplicated");
331 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
332 printf(" detached");
333 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
334 printf(" deprecated");
335 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_AUTOCONF)
336 printf(" autoconf");
337 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TEMPORARY)
338 printf(" temporary");
341 if (scopeid)
342 printf(" scopeid 0x%x", scopeid);
344 if (get_flag('L')) {
345 struct in6_addrlifetime *lifetime;
346 ifr6 = *creq;
347 lifetime = &ifr6.ifr_ifru.ifru_lifetime;
348 if (prog_ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
349 if (errno != EADDRNOTAVAIL)
350 warn("SIOCGIFALIFETIME_IN6");
351 } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
352 time_t t = time(NULL);
353 printf(" pltime ");
354 if (lifetime->ia6t_preferred) {
355 printf("%lu",
356 (unsigned long)(lifetime->ia6t_preferred -
357 MIN(t, lifetime->ia6t_preferred)));
358 } else
359 printf("infty");
361 printf(" vltime ");
362 if (lifetime->ia6t_expire) {
363 printf("%lu",
364 (unsigned long)(lifetime->ia6t_expire -
365 MIN(t, lifetime->ia6t_expire)));
366 } else
367 printf("infty");
372 static void
373 in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
375 struct ifaddrs *ifap, *ifa;
376 struct in6_ifreq ifr;
377 const char *ifname;
378 bool printprefs = false;
380 if ((ifname = getifname(env)) == NULL)
381 err(EXIT_FAILURE, "%s: getifname", __func__);
383 if (getifaddrs(&ifap) != 0)
384 err(EXIT_FAILURE, "getifaddrs");
385 printprefs = ifa_any_preferences(ifname, ifap, AF_INET6);
386 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
387 if (strcmp(ifname, ifa->ifa_name) != 0)
388 continue;
389 if (ifa->ifa_addr->sa_family != AF_INET6)
390 continue;
391 if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
392 continue;
394 memset(&ifr, 0, sizeof(ifr));
395 estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
396 memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
397 in6_alias(ifname, env, oenv, &ifr);
398 if (printprefs)
399 ifa_print_preference(ifa->ifa_name, ifa->ifa_addr);
400 printf("\n");
402 freeifaddrs(ifap);
405 static int
406 in6_pre_aifaddr(prop_dictionary_t env, const struct afparam *param)
408 struct in6_aliasreq *ifra = param->req.buf;
410 setia6eui64_impl(env, ifra);
411 setia6vltime_impl(env, ifra);
412 setia6pltime_impl(env, ifra);
413 setia6flags_impl(env, ifra);
414 inet6_putscopeid(&ifra->ifra_addr, INET6_IS_ADDR_LINKLOCAL);
415 inet6_putscopeid(&ifra->ifra_dstaddr, INET6_IS_ADDR_LINKLOCAL);
417 return 0;
420 static void
421 in6_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
423 struct in6_ifreq in6_ifr = {
424 .ifr_addr = {
425 .sin6_family = AF_INET6,
426 .sin6_len = sizeof(in6_ifr.ifr_addr),
427 .sin6_addr = {
428 .s6_addr =
429 {0xff, 0xff, 0xff, 0xff,
430 0xff, 0xff, 0xff, 0xff}
434 static struct sockaddr_in6 in6_defmask = {
435 .sin6_family = AF_INET6,
436 .sin6_len = sizeof(in6_defmask),
437 .sin6_addr = {
438 .s6_addr = {0xff, 0xff, 0xff, 0xff,
439 0xff, 0xff, 0xff, 0xff}
443 struct in6_aliasreq in6_ifra = {
444 .ifra_prefixmask = {
445 .sin6_family = AF_INET6,
446 .sin6_len = sizeof(in6_ifra.ifra_prefixmask),
447 .sin6_addr = {
448 .s6_addr =
449 {0xff, 0xff, 0xff, 0xff,
450 0xff, 0xff, 0xff, 0xff}}},
451 .ifra_lifetime = {
452 .ia6t_pltime = ND6_INFINITE_LIFETIME
453 , .ia6t_vltime = ND6_INFINITE_LIFETIME
456 struct afparam in6param = {
457 .req = BUFPARAM(in6_ifra)
458 , .dgreq = BUFPARAM(in6_ifr)
459 , .name = {
460 {.buf = in6_ifr.ifr_name,
461 .buflen = sizeof(in6_ifr.ifr_name)},
462 {.buf = in6_ifra.ifra_name,
463 .buflen = sizeof(in6_ifra.ifra_name)}
465 , .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
466 , .addr = BUFPARAM(in6_ifra.ifra_addr)
467 , .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
468 , .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
469 , .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
470 , .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
471 , .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
472 , .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
473 , .defmask = BUFPARAM(in6_defmask)
474 , .pre_aifaddr = in6_pre_aifaddr
476 commit_address(env, oenv, &in6param);
479 static bool
480 in6_addr_tentative(struct ifaddrs *ifa)
482 int s;
483 struct in6_ifreq ifr;
485 if ((s = getsock(AF_INET6)) == -1)
486 err(EXIT_FAILURE, "%s: getsock", __func__);
487 memset(&ifr, 0, sizeof(ifr));
488 strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
489 ifr.ifr_addr = *(struct sockaddr_in6 *)ifa->ifa_addr;
490 if (prog_ioctl(s, SIOCGIFAFLAG_IN6, &ifr) == -1)
491 err(EXIT_FAILURE, "SIOCGIFAFLAG_IN6");
492 return ifr.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE ? true : false;
495 static void
496 in6_usage(prop_dictionary_t env)
498 fprintf(stderr,
499 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
500 "\t[ pltime n ] [ vltime n ] "
501 "[ eui64 ]\n");
504 static void
505 in6_constructor(void)
507 if (register_flag('L') != 0)
508 err(EXIT_FAILURE, __func__);
509 register_family(&in6af);
510 usage_func_init(&usage, in6_usage);
511 register_usage(&usage);
512 cmdloop_branch_init(&branch[0], &ia6flags.pk_parser);
513 cmdloop_branch_init(&branch[1], &inet6.pk_parser);
514 register_cmdloop_branch(&branch[0]);
515 register_cmdloop_branch(&branch[1]);