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