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