Home | History | Annotate | Line # | Download | only in ifconfig
af_inet6.c revision 1.16
      1 /*	$NetBSD: af_inet6.c,v 1.16 2008/05/11 22:16:29 dyoung 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.16 2008/05/11 22:16:29 dyoung 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 "parse.h"
     57 #include "extern.h"
     58 #include "af_inet6.h"
     59 #include "af_inetany.h"
     60 
     61 struct in6_ifreq    in6_ridreq = {
     62 	.ifr_addr = {
     63 		.sin6_family = AF_INET6,
     64 		.sin6_addr = {
     65 			.s6_addr =
     66 			    {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
     67 		}
     68 	}
     69 };
     70 
     71 struct in6_aliasreq in6_addreq = {
     72 	.ifra_prefixmask = {
     73 		.sin6_len = sizeof(in6_addreq.ifra_prefixmask),
     74 		.sin6_addr = {
     75 			.s6_addr =
     76 			    {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}},
     77 	.ifra_lifetime = {
     78 		  .ia6t_pltime = ND6_INFINITE_LIFETIME
     79 		, .ia6t_vltime = ND6_INFINITE_LIFETIME
     80 	}
     81 };
     82 
     83 static const struct kwinst ia6flagskw[] = {
     84 	  IFKW("anycast",	IN6_IFF_ANYCAST)
     85 	, IFKW("tentative",	IN6_IFF_TENTATIVE)
     86 	, IFKW("deprecated",	IN6_IFF_DEPRECATED)
     87 };
     88 
     89 static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime,
     90     "pltime", 0, NULL, "pltime", &command_root.pb_parser);
     91 
     92 static struct pinteger parse_vltime = PINTEGER_INITIALIZER(&parse_vltime,
     93     "vltime", 0, NULL, "vltime", &command_root.pb_parser);
     94 
     95 static const struct kwinst inet6kw[] = {
     96 	  {.k_word = "pltime", .k_nextparser = &parse_pltime.pi_parser}
     97 	, {.k_word = "vltime", .k_nextparser = &parse_vltime.pi_parser}
     98 	, {.k_word = "eui64", .k_key = "eui64", .k_type = KW_T_BOOL,
     99 	   .k_bool = true, .k_nextparser = &command_root.pb_parser}
    100 };
    101 
    102 struct pkw ia6flags = PKW_INITIALIZER(&ia6flags, "ia6flags", NULL,
    103     "ia6flag", ia6flagskw, __arraycount(ia6flagskw), &command_root.pb_parser);
    104 struct pkw inet6 = PKW_INITIALIZER(&inet6, "IPv6 keywords", NULL,
    105     NULL, inet6kw, __arraycount(inet6kw), NULL);
    106 
    107 static void in6_delscopeid(struct sockaddr_in6 *sin6);
    108 static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *);
    109 static void in6_alias(const char *, prop_dictionary_t, prop_dictionary_t,
    110     struct in6_ifreq *);
    111 
    112 static char *
    113 sec2str(time_t total)
    114 {
    115 	static char result[256];
    116 	snprintf(result, sizeof(result), "%lu", (u_long)total);
    117 
    118 	return result;
    119 }
    120 
    121 static int
    122 prefix(void *val, int size)
    123 {
    124 	u_char *pname = (u_char *)val;
    125 	int byte, bit, plen = 0;
    126 
    127 	for (byte = 0; byte < size; byte++, plen += 8)
    128 		if (pname[byte] != 0xff)
    129 			break;
    130 	if (byte == size)
    131 		return (plen);
    132 	for (bit = 7; bit != 0; bit--, plen++)
    133 		if (!(pname[byte] & (1 << bit)))
    134 			break;
    135 	for (; bit != 0; bit--)
    136 		if (pname[byte] & (1 << bit))
    137 			return(0);
    138 	byte++;
    139 	for (; byte < size; byte++)
    140 		if (pname[byte])
    141 			return(0);
    142 	return (plen);
    143 }
    144 
    145 int
    146 setia6flags_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    147 {
    148 	int64_t ia6flag;
    149 
    150 	if (!prop_dictionary_get_int64(env, "ia6flag", &ia6flag)) {
    151 		errno = ENOENT;
    152 		return -1;
    153 	}
    154 
    155 	if (ia6flag < 0) {
    156 		ia6flag = -ia6flag;
    157 		ifra->ifra_flags &= ~ia6flag;
    158 	} else
    159 		ifra->ifra_flags |= ia6flag;
    160 	return 0;
    161 }
    162 
    163 int
    164 setia6pltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    165 {
    166 	int64_t pltime;
    167 
    168 	if (!prop_dictionary_get_int64(env, "pltime", &pltime)) {
    169 		errno = ENOENT;
    170 		return -1;
    171 	}
    172 
    173 	return setia6lifetime(env, pltime,
    174 	    &ifra->ifra_lifetime.ia6t_preferred,
    175 	    &ifra->ifra_lifetime.ia6t_pltime);
    176 }
    177 
    178 int
    179 setia6vltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    180 {
    181 	int64_t vltime;
    182 
    183 	if (!prop_dictionary_get_int64(env, "vltime", &vltime)) {
    184 		errno = ENOENT;
    185 		return -1;
    186 	}
    187 
    188 	return setia6lifetime(env, vltime,
    189 		&ifra->ifra_lifetime.ia6t_expire,
    190 		&ifra->ifra_lifetime.ia6t_vltime);
    191 }
    192 
    193 static int
    194 setia6lifetime(prop_dictionary_t env, int64_t val, time_t *timep,
    195     uint32_t *ivalp)
    196 {
    197 	time_t t;
    198 	int af;
    199 
    200 	if ((af = getaf(env)) == -1 || af != AF_INET6) {
    201 		errx(EXIT_FAILURE,
    202 		    "inet6 address lifetime not allowed for the AF");
    203 	}
    204 
    205 	t = time(NULL);
    206 	*timep = t + val;
    207 	*ivalp = val;
    208 	return 0;
    209 }
    210 
    211 int
    212 setia6eui64_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
    213 {
    214 	char buf[2][80];
    215 	struct ifaddrs *ifap, *ifa;
    216 	const struct sockaddr_in6 *sin6 = NULL;
    217 	const struct in6_addr *lladdr = NULL;
    218 	struct in6_addr *in6;
    219 	const char *ifname;
    220 	bool doit = false;
    221 	int af;
    222 
    223 	if (!prop_dictionary_get_bool(env, "eui64", &doit) || !doit) {
    224 		errno = ENOENT;
    225 		return -1;
    226 	}
    227 
    228 	if ((ifname = getifname(env)) == NULL)
    229 		return -1;
    230 
    231 	af = getaf(env);
    232 	if (af != AF_INET6) {
    233 		errx(EXIT_FAILURE,
    234 		    "eui64 address modifier not allowed for the AF");
    235 	}
    236  	in6 = &ifra->ifra_addr.sin6_addr;
    237 	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) {
    238 		union {
    239 			struct sockaddr_in6 sin6;
    240 			struct sockaddr sa;
    241 		} any = {.sin6 = {.sin6_family = AF_INET6}};
    242 		memcpy(&any.sin6.sin6_addr, &in6addr_any,
    243 		    sizeof(any.sin6.sin6_addr));
    244 		(void)sockaddr_snprintf(buf[0], sizeof(buf[0]), "%a%%S",
    245 		    &any.sa);
    246 		(void)sockaddr_snprintf(buf[1], sizeof(buf[1]), "%a%%S",
    247 		    (const struct sockaddr *)&ifra->ifra_addr);
    248 		errx(EXIT_FAILURE, "interface index is already filled, %s | %s",
    249 		    buf[0], buf[1]);
    250 	}
    251 	if (getifaddrs(&ifap) != 0)
    252 		err(EXIT_FAILURE, "getifaddrs");
    253 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    254 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
    255 		    strcmp(ifa->ifa_name, ifname) == 0) {
    256 			sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
    257 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    258 				lladdr = &sin6->sin6_addr;
    259 				break;
    260 			}
    261 		}
    262 	}
    263 	if (!lladdr)
    264 		errx(EXIT_FAILURE, "could not determine link local address");
    265 
    266  	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
    267 
    268 	freeifaddrs(ifap);
    269 	return 0;
    270 }
    271 
    272 /* KAME idiosyncrasy */
    273 static void
    274 in6_delscopeid(struct sockaddr_in6 *sin6)
    275 {
    276 	if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
    277 	    sin6->sin6_scope_id == 0)
    278 		return;
    279 
    280 	*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = htons(sin6->sin6_scope_id);
    281 	sin6->sin6_scope_id = 0;
    282 }
    283 
    284 /* KAME idiosyncrasy */
    285 void
    286 in6_fillscopeid(struct sockaddr_in6 *sin6)
    287 {
    288 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    289 		sin6->sin6_scope_id =
    290 			ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
    291 		sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
    292 	}
    293 }
    294 
    295 /* XXX not really an alias */
    296 void
    297 in6_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
    298     struct in6_ifreq *creq)
    299 {
    300 	struct in6_ifreq ifr6;
    301 	struct sockaddr_in6 *sin6;
    302 	char hbuf[NI_MAXHOST];
    303 	u_int32_t scopeid;
    304 	int s;
    305 	const int niflag = NI_NUMERICHOST;
    306 	unsigned short flags;
    307 
    308 	/* Get the non-alias address for this interface. */
    309 	if ((s = getsock(AF_INET6)) == -1) {
    310 		if (errno == EAFNOSUPPORT)
    311 			return;
    312 		err(EXIT_FAILURE, "socket");
    313 	}
    314 
    315 	sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
    316 
    317 	in6_fillscopeid(sin6);
    318 	scopeid = sin6->sin6_scope_id;
    319 	if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
    320 			hbuf, sizeof(hbuf), NULL, 0, niflag))
    321 		strlcpy(hbuf, "", sizeof(hbuf));	/* some message? */
    322 	printf("\tinet6 %s", hbuf);
    323 
    324 	if (getifflags(env, oenv, &flags) == -1)
    325 		err(EXIT_FAILURE, "%s: getifflags", __func__);
    326 
    327 	if (flags & IFF_POINTOPOINT) {
    328 		memset(&ifr6, 0, sizeof(ifr6));
    329 		estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
    330 		ifr6.ifr_addr = creq->ifr_addr;
    331 		if (ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
    332 			if (errno != EADDRNOTAVAIL)
    333 				warn("SIOCGIFDSTADDR_IN6");
    334 			memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
    335 			ifr6.ifr_addr.sin6_family = AF_INET6;
    336 			ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
    337 		}
    338 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
    339 		in6_fillscopeid(sin6);
    340 		hbuf[0] = '\0';
    341 		if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
    342 				hbuf, sizeof(hbuf), NULL, 0, niflag))
    343 			strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
    344 		printf(" -> %s", hbuf);
    345 	}
    346 
    347 	memset(&ifr6, 0, sizeof(ifr6));
    348 	estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
    349 	ifr6.ifr_addr = creq->ifr_addr;
    350 	if (ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
    351 		if (errno != EADDRNOTAVAIL)
    352 			warn("SIOCGIFNETMASK_IN6");
    353 	} else {
    354 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
    355 		printf(" prefixlen %d", prefix(&sin6->sin6_addr,
    356 					       sizeof(struct in6_addr)));
    357 	}
    358 
    359 	memset(&ifr6, 0, sizeof(ifr6));
    360 	estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
    361 	ifr6.ifr_addr = creq->ifr_addr;
    362 	if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
    363 		if (errno != EADDRNOTAVAIL)
    364 			warn("SIOCGIFAFLAG_IN6");
    365 	} else {
    366 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
    367 			printf(" anycast");
    368 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
    369 			printf(" tentative");
    370 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
    371 			printf(" duplicated");
    372 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
    373 			printf(" detached");
    374 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
    375 			printf(" deprecated");
    376 	}
    377 
    378 	if (scopeid)
    379 		printf(" scopeid 0x%x", scopeid);
    380 
    381 	if (Lflag) {
    382 		struct in6_addrlifetime *lifetime;
    383 		memset(&ifr6, 0, sizeof(ifr6));
    384 		estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
    385 		ifr6.ifr_addr = creq->ifr_addr;
    386 		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
    387 		if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
    388 			if (errno != EADDRNOTAVAIL)
    389 				warn("SIOCGIFALIFETIME_IN6");
    390 		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
    391 			time_t t = time(NULL);
    392 			printf(" pltime ");
    393 			if (lifetime->ia6t_preferred) {
    394 				printf("%s", lifetime->ia6t_preferred < t
    395 					? "0"
    396 					: sec2str(lifetime->ia6t_preferred - t));
    397 			} else
    398 				printf("infty");
    399 
    400 			printf(" vltime ");
    401 			if (lifetime->ia6t_expire) {
    402 				printf("%s", lifetime->ia6t_expire < t
    403 					? "0"
    404 					: sec2str(lifetime->ia6t_expire - t));
    405 			} else
    406 				printf("infty");
    407 		}
    408 	}
    409 
    410 	printf("\n");
    411 }
    412 
    413 void
    414 in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
    415 {
    416 	struct ifaddrs *ifap, *ifa;
    417 	struct in6_ifreq ifr;
    418 	const char *ifname;
    419 
    420 	if ((ifname = getifname(env)) == NULL)
    421 		err(EXIT_FAILURE, "%s: getifname", __func__);
    422 
    423 	if (getifaddrs(&ifap) != 0)
    424 		err(EXIT_FAILURE, "getifaddrs");
    425 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    426 		if (strcmp(ifname, ifa->ifa_name) != 0)
    427 			continue;
    428 		if (ifa->ifa_addr->sa_family != AF_INET6)
    429 			continue;
    430 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
    431 			continue;
    432 
    433 		memset(&ifr, 0, sizeof(ifr));
    434 		estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    435 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    436 		in6_alias(ifname, env, oenv, &ifr);
    437 	}
    438 	freeifaddrs(ifap);
    439 }
    440 
    441 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
    442 struct sockaddr_in6 *sin6tab[] = {
    443     SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
    444     SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
    445 
    446 static int
    447 in6_pre_aifaddr(prop_dictionary_t env, struct afparam *param)
    448 {
    449 	struct in6_aliasreq *ifra = param->req.buf;
    450 
    451 	setia6eui64_impl(env, ifra);
    452 	setia6vltime_impl(env, ifra);
    453 	setia6pltime_impl(env, ifra);
    454 	setia6flags_impl(env, ifra);
    455 	in6_delscopeid(&ifra->ifra_addr);
    456 	in6_delscopeid(&ifra->ifra_dstaddr);
    457 
    458 	return 0;
    459 }
    460 
    461 void
    462 in6_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
    463 {
    464 	struct in6_ifreq in6_ifr = {
    465 		.ifr_addr = {
    466 			.sin6_family = AF_INET6,
    467 			.sin6_addr = {
    468 				.s6_addr =
    469 				    {0xff, 0xff, 0xff, 0xff,
    470 				     0xff, 0xff, 0xff, 0xff}
    471 			}
    472 		}
    473 	};
    474 	static struct sockaddr_in6 in6_defmask = {
    475 		.sin6_addr = {
    476 			.s6_addr = {0xff, 0xff, 0xff, 0xff,
    477 			            0xff, 0xff, 0xff, 0xff}
    478 		}
    479 	};
    480 
    481 	struct in6_aliasreq in6_ifra = {
    482 		.ifra_prefixmask = {
    483 			.sin6_addr = {
    484 				.s6_addr =
    485 				    {0xff, 0xff, 0xff, 0xff,
    486 				     0xff, 0xff, 0xff, 0xff}}},
    487 		.ifra_lifetime = {
    488 			  .ia6t_pltime = ND6_INFINITE_LIFETIME
    489 			, .ia6t_vltime = ND6_INFINITE_LIFETIME
    490 		}
    491 	};
    492 	struct afparam in6param = {
    493 		  .req = BUFPARAM(in6_ifra)
    494 		, .dgreq = BUFPARAM(in6_ifr)
    495 		, .name = {
    496 			{.buf = in6_ifr.ifr_name,
    497 			 .buflen = sizeof(in6_ifr.ifr_name)},
    498 			{.buf = in6_ifra.ifra_name,
    499 			 .buflen = sizeof(in6_ifra.ifra_name)}
    500 		  }
    501 		, .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
    502 		, .addr = BUFPARAM(in6_ifra.ifra_addr)
    503 		, .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
    504 		, .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
    505 		, .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
    506 		, .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
    507 		, .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
    508 		, .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
    509 		, .defmask = BUFPARAM(in6_defmask)
    510 		, .pre_aifaddr = in6_pre_aifaddr
    511 	};
    512 	commit_address(env, oenv, &in6param);
    513 }
    514