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