Home | History | Annotate | Line # | Download | only in ifconfig
af_inet6.c revision 1.18
      1 /*	$NetBSD: af_inet6.c,v 1.18 2008/05/11 22:30:07 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.18 2008/05/11 22:30:07 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 = &creq->ifr_addr;
    316 
    317 	in6_fillscopeid(sin6);
    318 	scopeid = sin6->sin6_scope_id;
    319 	if (getnameinfo((const 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 		ifr6 = *creq;
    329 		if (ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
    330 			if (errno != EADDRNOTAVAIL)
    331 				warn("SIOCGIFDSTADDR_IN6");
    332 			memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
    333 			ifr6.ifr_addr.sin6_family = AF_INET6;
    334 			ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
    335 		}
    336 		sin6 = &ifr6.ifr_addr;
    337 		in6_fillscopeid(sin6);
    338 		hbuf[0] = '\0';
    339 		if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
    340 				hbuf, sizeof(hbuf), NULL, 0, niflag))
    341 			strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
    342 		printf(" -> %s", hbuf);
    343 	}
    344 
    345 	ifr6 = *creq;
    346 	if (ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
    347 		if (errno != EADDRNOTAVAIL)
    348 			warn("SIOCGIFNETMASK_IN6");
    349 	} else {
    350 		sin6 = &ifr6.ifr_addr;
    351 		printf(" prefixlen %d", prefix(&sin6->sin6_addr,
    352 					       sizeof(struct in6_addr)));
    353 	}
    354 
    355 	ifr6 = *creq;
    356 	if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
    357 		if (errno != EADDRNOTAVAIL)
    358 			warn("SIOCGIFAFLAG_IN6");
    359 	} else {
    360 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
    361 			printf(" anycast");
    362 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
    363 			printf(" tentative");
    364 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
    365 			printf(" duplicated");
    366 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
    367 			printf(" detached");
    368 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
    369 			printf(" deprecated");
    370 	}
    371 
    372 	if (scopeid)
    373 		printf(" scopeid 0x%x", scopeid);
    374 
    375 	if (Lflag) {
    376 		struct in6_addrlifetime *lifetime;
    377 		ifr6 = *creq;
    378 		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
    379 		if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
    380 			if (errno != EADDRNOTAVAIL)
    381 				warn("SIOCGIFALIFETIME_IN6");
    382 		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
    383 			time_t t = time(NULL);
    384 			printf(" pltime ");
    385 			if (lifetime->ia6t_preferred) {
    386 				printf("%s", lifetime->ia6t_preferred < t
    387 					? "0"
    388 					: sec2str(lifetime->ia6t_preferred - t));
    389 			} else
    390 				printf("infty");
    391 
    392 			printf(" vltime ");
    393 			if (lifetime->ia6t_expire) {
    394 				printf("%s", lifetime->ia6t_expire < t
    395 					? "0"
    396 					: sec2str(lifetime->ia6t_expire - t));
    397 			} else
    398 				printf("infty");
    399 		}
    400 	}
    401 
    402 	printf("\n");
    403 }
    404 
    405 void
    406 in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
    407 {
    408 	struct ifaddrs *ifap, *ifa;
    409 	struct in6_ifreq ifr;
    410 	const char *ifname;
    411 
    412 	if ((ifname = getifname(env)) == NULL)
    413 		err(EXIT_FAILURE, "%s: getifname", __func__);
    414 
    415 	if (getifaddrs(&ifap) != 0)
    416 		err(EXIT_FAILURE, "getifaddrs");
    417 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    418 		if (strcmp(ifname, ifa->ifa_name) != 0)
    419 			continue;
    420 		if (ifa->ifa_addr->sa_family != AF_INET6)
    421 			continue;
    422 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
    423 			continue;
    424 
    425 		memset(&ifr, 0, sizeof(ifr));
    426 		estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    427 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    428 		in6_alias(ifname, env, oenv, &ifr);
    429 	}
    430 	freeifaddrs(ifap);
    431 }
    432 
    433 static int
    434 in6_pre_aifaddr(prop_dictionary_t env, struct afparam *param)
    435 {
    436 	struct in6_aliasreq *ifra = param->req.buf;
    437 
    438 	setia6eui64_impl(env, ifra);
    439 	setia6vltime_impl(env, ifra);
    440 	setia6pltime_impl(env, ifra);
    441 	setia6flags_impl(env, ifra);
    442 	in6_delscopeid(&ifra->ifra_addr);
    443 	in6_delscopeid(&ifra->ifra_dstaddr);
    444 
    445 	return 0;
    446 }
    447 
    448 void
    449 in6_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
    450 {
    451 	struct in6_ifreq in6_ifr = {
    452 		.ifr_addr = {
    453 			.sin6_family = AF_INET6,
    454 			.sin6_addr = {
    455 				.s6_addr =
    456 				    {0xff, 0xff, 0xff, 0xff,
    457 				     0xff, 0xff, 0xff, 0xff}
    458 			}
    459 		}
    460 	};
    461 	static struct sockaddr_in6 in6_defmask = {
    462 		.sin6_addr = {
    463 			.s6_addr = {0xff, 0xff, 0xff, 0xff,
    464 			            0xff, 0xff, 0xff, 0xff}
    465 		}
    466 	};
    467 
    468 	struct in6_aliasreq in6_ifra = {
    469 		.ifra_prefixmask = {
    470 			.sin6_addr = {
    471 				.s6_addr =
    472 				    {0xff, 0xff, 0xff, 0xff,
    473 				     0xff, 0xff, 0xff, 0xff}}},
    474 		.ifra_lifetime = {
    475 			  .ia6t_pltime = ND6_INFINITE_LIFETIME
    476 			, .ia6t_vltime = ND6_INFINITE_LIFETIME
    477 		}
    478 	};
    479 	struct afparam in6param = {
    480 		  .req = BUFPARAM(in6_ifra)
    481 		, .dgreq = BUFPARAM(in6_ifr)
    482 		, .name = {
    483 			{.buf = in6_ifr.ifr_name,
    484 			 .buflen = sizeof(in6_ifr.ifr_name)},
    485 			{.buf = in6_ifra.ifra_name,
    486 			 .buflen = sizeof(in6_ifra.ifra_name)}
    487 		  }
    488 		, .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
    489 		, .addr = BUFPARAM(in6_ifra.ifra_addr)
    490 		, .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
    491 		, .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
    492 		, .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
    493 		, .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
    494 		, .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
    495 		, .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
    496 		, .defmask = BUFPARAM(in6_defmask)
    497 		, .pre_aifaddr = in6_pre_aifaddr
    498 	};
    499 	commit_address(env, oenv, &in6param);
    500 }
    501