Home | History | Annotate | Line # | Download | only in ifconfig
af_inet6.c revision 1.30
      1 /*	$NetBSD: af_inet6.c,v 1.30 2014/10/20 14:50:09 roy Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *      The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the 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.
     18  *
     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.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __RCSID("$NetBSD: af_inet6.c,v 1.30 2014/10/20 14:50:09 roy Exp $");
     35 #endif /* not lint */
     36 
     37 #include <sys/param.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/socket.h>
     40 
     41 #include <net/if.h>
     42 #include <netinet/in.h>
     43 #include <netinet/in_var.h>
     44 #include <netinet6/nd6.h>
     45 
     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>
     54 
     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"
     61 
     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);
     66 
     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 *);
     71 
     72 static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *);
     73 
     74 static void in6_status(prop_dictionary_t, prop_dictionary_t, bool);
     75 
     76 static struct usage_func usage;
     77 static cmdloop_branch_t branch[2];
     78 
     79 static const struct kwinst ia6flagskw[] = {
     80 	  IFKW("anycast",	IN6_IFF_ANYCAST)
     81 	, IFKW("deprecated",	IN6_IFF_DEPRECATED)
     82 };
     83 
     84 static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime,
     85     "pltime", 0, NULL, "pltime", &command_root.pb_parser);
     86 
     87 static struct pinteger parse_vltime = PINTEGER_INITIALIZER(&parse_vltime,
     88     "vltime", 0, NULL, "vltime", &command_root.pb_parser);
     89 
     90 static const struct kwinst inet6kw[] = {
     91 	  {.k_word = "pltime", .k_nextparser = &parse_pltime.pi_parser}
     92 	, {.k_word = "vltime", .k_nextparser = &parse_vltime.pi_parser}
     93 	, {.k_word = "eui64", .k_key = "eui64", .k_type = KW_T_BOOL,
     94 	   .k_bool = true, .k_nextparser = &command_root.pb_parser}
     95 };
     96 
     97 struct pkw ia6flags = PKW_INITIALIZER(&ia6flags, "ia6flags", NULL,
     98     "ia6flag", ia6flagskw, __arraycount(ia6flagskw), &command_root.pb_parser);
     99 struct pkw inet6 = PKW_INITIALIZER(&inet6, "IPv6 keywords", NULL,
    100     NULL, inet6kw, __arraycount(inet6kw), NULL);
    101 
    102 static struct afswtch in6af = {
    103 	.af_name = "inet6", .af_af = AF_INET6, .af_status = in6_status,
    104 	.af_addr_commit = in6_commit_address
    105 };
    106 
    107 static int
    108 prefix(void *val, int size)
    109 {
    110 	u_char *pname = (u_char *)val;
    111 	int byte, bit, plen = 0;
    112 
    113 	for (byte = 0; byte < size; byte++, plen += 8)
    114 		if (pname[byte] != 0xff)
    115 			break;
    116 	if (byte == size)
    117 		return (plen);
    118 	for (bit = 7; bit != 0; bit--, plen++)
    119 		if (!(pname[byte] & (1 << bit)))
    120 			break;
    121 	for (; bit != 0; bit--)
    122 		if (pname[byte] & (1 << bit))
    123 			return(0);
    124 	byte++;
    125 	for (; byte < size; byte++)
    126 		if (pname[byte])
    127 			return(0);
    128 	return (plen);
    129 }
    130 
    131 int
    132 setia6flags_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    133 {
    134 	int64_t ia6flag;
    135 
    136 	if (!prop_dictionary_get_int64(env, "ia6flag", &ia6flag)) {
    137 		errno = ENOENT;
    138 		return -1;
    139 	}
    140 
    141 	if (ia6flag < 0) {
    142 		ia6flag = -ia6flag;
    143 		ifra->ifra_flags &= ~ia6flag;
    144 	} else
    145 		ifra->ifra_flags |= ia6flag;
    146 	return 0;
    147 }
    148 
    149 int
    150 setia6pltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    151 {
    152 	int64_t pltime;
    153 
    154 	if (!prop_dictionary_get_int64(env, "pltime", &pltime)) {
    155 		errno = ENOENT;
    156 		return -1;
    157 	}
    158 
    159 	return setia6lifetime(env, pltime,
    160 	    &ifra->ifra_lifetime.ia6t_preferred,
    161 	    &ifra->ifra_lifetime.ia6t_pltime);
    162 }
    163 
    164 int
    165 setia6vltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    166 {
    167 	int64_t vltime;
    168 
    169 	if (!prop_dictionary_get_int64(env, "vltime", &vltime)) {
    170 		errno = ENOENT;
    171 		return -1;
    172 	}
    173 
    174 	return setia6lifetime(env, vltime,
    175 		&ifra->ifra_lifetime.ia6t_expire,
    176 		&ifra->ifra_lifetime.ia6t_vltime);
    177 }
    178 
    179 static int
    180 setia6lifetime(prop_dictionary_t env, int64_t val, time_t *timep,
    181     uint32_t *ivalp)
    182 {
    183 	time_t t;
    184 	int af;
    185 
    186 	if ((af = getaf(env)) == -1 || af != AF_INET6) {
    187 		errx(EXIT_FAILURE,
    188 		    "inet6 address lifetime not allowed for the AF");
    189 	}
    190 
    191 	t = time(NULL);
    192 	*timep = t + val;
    193 	*ivalp = val;
    194 	return 0;
    195 }
    196 
    197 int
    198 setia6eui64_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    199 {
    200 	char buf[2][80];
    201 	struct ifaddrs *ifap, *ifa;
    202 	const struct sockaddr_in6 *sin6 = NULL;
    203 	const struct in6_addr *lladdr = NULL;
    204 	struct in6_addr *in6;
    205 	const char *ifname;
    206 	bool doit = false;
    207 	int af;
    208 
    209 	if (!prop_dictionary_get_bool(env, "eui64", &doit) || !doit) {
    210 		errno = ENOENT;
    211 		return -1;
    212 	}
    213 
    214 	if ((ifname = getifname(env)) == NULL)
    215 		return -1;
    216 
    217 	af = getaf(env);
    218 	if (af != AF_INET6) {
    219 		errx(EXIT_FAILURE,
    220 		    "eui64 address modifier not allowed for the AF");
    221 	}
    222  	in6 = &ifra->ifra_addr.sin6_addr;
    223 	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) {
    224 		union {
    225 			struct sockaddr_in6 sin6;
    226 			struct sockaddr sa;
    227 		} any = {.sin6 = {.sin6_family = AF_INET6}};
    228 		memcpy(&any.sin6.sin6_addr, &in6addr_any,
    229 		    sizeof(any.sin6.sin6_addr));
    230 		(void)sockaddr_snprintf(buf[0], sizeof(buf[0]), "%a%%S",
    231 		    &any.sa);
    232 		(void)sockaddr_snprintf(buf[1], sizeof(buf[1]), "%a%%S",
    233 		    (const struct sockaddr *)&ifra->ifra_addr);
    234 		errx(EXIT_FAILURE, "interface index is already filled, %s | %s",
    235 		    buf[0], buf[1]);
    236 	}
    237 	if (getifaddrs(&ifap) != 0)
    238 		err(EXIT_FAILURE, "getifaddrs");
    239 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    240 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
    241 		    strcmp(ifa->ifa_name, ifname) == 0) {
    242 			sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
    243 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    244 				lladdr = &sin6->sin6_addr;
    245 				break;
    246 			}
    247 		}
    248 	}
    249 	if (lladdr == NULL)
    250 		errx(EXIT_FAILURE, "could not determine link local address");
    251 
    252  	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
    253 
    254 	freeifaddrs(ifap);
    255 	return 0;
    256 }
    257 
    258 /* XXX not really an alias */
    259 void
    260 in6_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
    261     struct in6_ifreq *creq)
    262 {
    263 	struct in6_ifreq ifr6;
    264 	struct sockaddr_in6 *sin6;
    265 	char hbuf[NI_MAXHOST];
    266 	u_int32_t scopeid;
    267 	int s;
    268 	const int niflag = Nflag ? 0 : NI_NUMERICHOST;
    269 	unsigned short flags;
    270 
    271 	/* Get the non-alias address for this interface. */
    272 	if ((s = getsock(AF_INET6)) == -1) {
    273 		if (errno == EAFNOSUPPORT)
    274 			return;
    275 		err(EXIT_FAILURE, "socket");
    276 	}
    277 
    278 	sin6 = &creq->ifr_addr;
    279 
    280 	inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
    281 	scopeid = sin6->sin6_scope_id;
    282 	if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len,
    283 			hbuf, sizeof(hbuf), NULL, 0, niflag))
    284 		strlcpy(hbuf, "", sizeof(hbuf));	/* some message? */
    285 	printf("\tinet6 %s", hbuf);
    286 
    287 	if (getifflags(env, oenv, &flags) == -1)
    288 		err(EXIT_FAILURE, "%s: getifflags", __func__);
    289 
    290 	if (flags & IFF_POINTOPOINT) {
    291 		ifr6 = *creq;
    292 		if (prog_ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
    293 			if (errno != EADDRNOTAVAIL)
    294 				warn("SIOCGIFDSTADDR_IN6");
    295 			memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
    296 			ifr6.ifr_addr.sin6_family = AF_INET6;
    297 			ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
    298 		}
    299 		sin6 = &ifr6.ifr_addr;
    300 		inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
    301 		hbuf[0] = '\0';
    302 		if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
    303 				hbuf, sizeof(hbuf), NULL, 0, niflag))
    304 			strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
    305 		printf(" -> %s", hbuf);
    306 	}
    307 
    308 	ifr6 = *creq;
    309 	if (prog_ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
    310 		if (errno != EADDRNOTAVAIL)
    311 			warn("SIOCGIFNETMASK_IN6");
    312 	} else {
    313 		sin6 = &ifr6.ifr_addr;
    314 		printf(" prefixlen %d", prefix(&sin6->sin6_addr,
    315 					       sizeof(struct in6_addr)));
    316 	}
    317 
    318 	ifr6 = *creq;
    319 	if (prog_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
    320 		if (errno != EADDRNOTAVAIL)
    321 			warn("SIOCGIFAFLAG_IN6");
    322 	} else {
    323 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
    324 			printf(" anycast");
    325 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
    326 			printf(" tentative");
    327 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
    328 			printf(" duplicated");
    329 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
    330 			printf(" detached");
    331 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
    332 			printf(" deprecated");
    333 	}
    334 
    335 	if (scopeid)
    336 		printf(" scopeid 0x%x", scopeid);
    337 
    338 	if (get_flag('L')) {
    339 		struct in6_addrlifetime *lifetime;
    340 		ifr6 = *creq;
    341 		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
    342 		if (prog_ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
    343 			if (errno != EADDRNOTAVAIL)
    344 				warn("SIOCGIFALIFETIME_IN6");
    345 		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
    346 			time_t t = time(NULL);
    347 			printf(" pltime ");
    348 			if (lifetime->ia6t_preferred) {
    349 				printf("%lu",
    350 				    (unsigned long)(lifetime->ia6t_preferred -
    351 				        MIN(t, lifetime->ia6t_preferred)));
    352 			} else
    353 				printf("infty");
    354 
    355 			printf(" vltime ");
    356 			if (lifetime->ia6t_expire) {
    357 				printf("%lu",
    358 				    (unsigned long)(lifetime->ia6t_expire -
    359 				        MIN(t, lifetime->ia6t_expire)));
    360 			} else
    361 				printf("infty");
    362 		}
    363 	}
    364 }
    365 
    366 static void
    367 in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
    368 {
    369 	struct ifaddrs *ifap, *ifa;
    370 	struct in6_ifreq ifr;
    371 	const char *ifname;
    372 	bool printprefs = false;
    373 
    374 	if ((ifname = getifname(env)) == NULL)
    375 		err(EXIT_FAILURE, "%s: getifname", __func__);
    376 
    377 	if (getifaddrs(&ifap) != 0)
    378 		err(EXIT_FAILURE, "getifaddrs");
    379 	printprefs = ifa_any_preferences(ifname, ifap, AF_INET6);
    380 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    381 		if (strcmp(ifname, ifa->ifa_name) != 0)
    382 			continue;
    383 		if (ifa->ifa_addr->sa_family != AF_INET6)
    384 			continue;
    385 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
    386 			continue;
    387 
    388 		memset(&ifr, 0, sizeof(ifr));
    389 		estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    390 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    391 		in6_alias(ifname, env, oenv, &ifr);
    392 		if (printprefs)
    393 			ifa_print_preference(ifa->ifa_name, ifa->ifa_addr);
    394 		printf("\n");
    395 	}
    396 	freeifaddrs(ifap);
    397 }
    398 
    399 static int
    400 in6_pre_aifaddr(prop_dictionary_t env, const struct afparam *param)
    401 {
    402 	struct in6_aliasreq *ifra = param->req.buf;
    403 
    404 	setia6eui64_impl(env, ifra);
    405 	setia6vltime_impl(env, ifra);
    406 	setia6pltime_impl(env, ifra);
    407 	setia6flags_impl(env, ifra);
    408 	inet6_putscopeid(&ifra->ifra_addr, INET6_IS_ADDR_LINKLOCAL);
    409 	inet6_putscopeid(&ifra->ifra_dstaddr, INET6_IS_ADDR_LINKLOCAL);
    410 
    411 	return 0;
    412 }
    413 
    414 static void
    415 in6_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
    416 {
    417 	struct in6_ifreq in6_ifr = {
    418 		.ifr_addr = {
    419 			.sin6_family = AF_INET6,
    420 			.sin6_len = sizeof(in6_ifr.ifr_addr),
    421 			.sin6_addr = {
    422 				.s6_addr =
    423 				    {0xff, 0xff, 0xff, 0xff,
    424 				     0xff, 0xff, 0xff, 0xff}
    425 			}
    426 		}
    427 	};
    428 	static struct sockaddr_in6 in6_defmask = {
    429 		.sin6_family = AF_INET6,
    430 		.sin6_len = sizeof(in6_defmask),
    431 		.sin6_addr = {
    432 			.s6_addr = {0xff, 0xff, 0xff, 0xff,
    433 			            0xff, 0xff, 0xff, 0xff}
    434 		}
    435 	};
    436 
    437 	struct in6_aliasreq in6_ifra = {
    438 		.ifra_prefixmask = {
    439 			.sin6_family = AF_INET6,
    440 			.sin6_len = sizeof(in6_ifra.ifra_prefixmask),
    441 			.sin6_addr = {
    442 				.s6_addr =
    443 				    {0xff, 0xff, 0xff, 0xff,
    444 				     0xff, 0xff, 0xff, 0xff}}},
    445 		.ifra_lifetime = {
    446 			  .ia6t_pltime = ND6_INFINITE_LIFETIME
    447 			, .ia6t_vltime = ND6_INFINITE_LIFETIME
    448 		}
    449 	};
    450 	struct afparam in6param = {
    451 		  .req = BUFPARAM(in6_ifra)
    452 		, .dgreq = BUFPARAM(in6_ifr)
    453 		, .name = {
    454 			{.buf = in6_ifr.ifr_name,
    455 			 .buflen = sizeof(in6_ifr.ifr_name)},
    456 			{.buf = in6_ifra.ifra_name,
    457 			 .buflen = sizeof(in6_ifra.ifra_name)}
    458 		  }
    459 		, .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
    460 		, .addr = BUFPARAM(in6_ifra.ifra_addr)
    461 		, .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
    462 		, .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
    463 		, .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
    464 		, .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
    465 		, .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
    466 		, .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
    467 		, .defmask = BUFPARAM(in6_defmask)
    468 		, .pre_aifaddr = in6_pre_aifaddr
    469 	};
    470 	commit_address(env, oenv, &in6param);
    471 }
    472 
    473 static void
    474 in6_usage(prop_dictionary_t env)
    475 {
    476 	fprintf(stderr,
    477 	    "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
    478 	    "\t[ pltime n ] [ vltime n ] "
    479 	    "[ eui64 ]\n");
    480 }
    481 
    482 static void
    483 in6_constructor(void)
    484 {
    485 	if (register_flag('L') != 0)
    486 		err(EXIT_FAILURE, __func__);
    487 	register_family(&in6af);
    488 	usage_func_init(&usage, in6_usage);
    489 	register_usage(&usage);
    490 	cmdloop_branch_init(&branch[0], &ia6flags.pk_parser);
    491 	cmdloop_branch_init(&branch[1], &inet6.pk_parser);
    492 	register_cmdloop_branch(&branch[0]);
    493 	register_cmdloop_branch(&branch[1]);
    494 }
    495