Home | History | Annotate | Line # | Download | only in ifconfig
ifconfig.c revision 1.163
      1 /*	$NetBSD: ifconfig.c,v 1.163 2005/03/20 00:02:58 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1983, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 #ifndef lint
     71 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     72 	The Regents of the University of California.  All rights reserved.\n");
     73 #endif /* not lint */
     74 
     75 #ifndef lint
     76 #if 0
     77 static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
     78 #else
     79 __RCSID("$NetBSD: ifconfig.c,v 1.163 2005/03/20 00:02:58 thorpej Exp $");
     80 #endif
     81 #endif /* not lint */
     82 
     83 #include <sys/param.h>
     84 #include <sys/socket.h>
     85 #include <sys/ioctl.h>
     86 
     87 #include <net/if.h>
     88 #include <net/if_dl.h>
     89 #include <net/if_media.h>
     90 #include <net/if_ether.h>
     91 #include <netinet/in.h>
     92 #include <netinet/in_var.h>
     93 #ifdef INET6
     94 #include <netinet6/nd6.h>
     95 #endif
     96 #include <arpa/inet.h>
     97 
     98 #include <netdb.h>
     99 
    100 #include <sys/protosw.h>
    101 
    102 #include <ctype.h>
    103 #include <err.h>
    104 #include <errno.h>
    105 #include <stddef.h>
    106 #include <stdio.h>
    107 #include <stdlib.h>
    108 #include <string.h>
    109 #include <unistd.h>
    110 #include <ifaddrs.h>
    111 #include <util.h>
    112 
    113 #include "extern.h"
    114 
    115 #ifndef INET_ONLY
    116 #include "af_atalk.h"
    117 #include "af_iso.h"
    118 #include "af_ns.h"
    119 #endif /* ! INET_ONLY */
    120 
    121 #include "agr.h"
    122 #include "ieee80211.h"
    123 #include "tunnel.h"
    124 #include "vlan.h"
    125 
    126 struct	ifreq		ifr, ridreq;
    127 struct	ifaliasreq	addreq __attribute__((aligned(4)));
    128 struct	in_aliasreq	in_addreq;
    129 #ifdef INET6
    130 struct	in6_ifreq	ifr6;
    131 struct	in6_ifreq	in6_ridreq;
    132 struct	in6_aliasreq	in6_addreq;
    133 #endif
    134 struct	sockaddr_in	netmask;
    135 
    136 char	name[30];
    137 u_short	flags;
    138 int	setaddr, setipdst, doalias;
    139 u_long	metric, mtu;
    140 int	clearaddr, s;
    141 int	newaddr = -1;
    142 int	conflicting = 0;
    143 int	af;
    144 int	aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag, vflag, zflag;
    145 #ifdef INET6
    146 int	Lflag;
    147 #endif
    148 int	explicit_prefix = 0;
    149 
    150 struct ifcapreq g_ifcr;
    151 int	g_ifcr_updated;
    152 
    153 void 	notealias(const char *, int);
    154 void 	notrailers(const char *, int);
    155 void 	setifaddr(const char *, int);
    156 void 	setifdstaddr(const char *, int);
    157 void 	setifflags(const char *, int);
    158 void	setifcaps(const char *, int);
    159 void 	setifbroadaddr(const char *, int);
    160 void 	setifipdst(const char *, int);
    161 void 	setifmetric(const char *, int);
    162 void 	setifmtu(const char *, int);
    163 void 	setifnetmask(const char *, int);
    164 void	setifprefixlen(const char *, int);
    165 #ifdef INET6
    166 void 	setia6flags(const char *, int);
    167 void	setia6pltime(const char *, int);
    168 void	setia6vltime(const char *, int);
    169 void	setia6lifetime(const char *, const char *);
    170 void	setia6eui64(const char *, int);
    171 #endif
    172 void	setmedia(const char *, int);
    173 void	setmediamode(const char *, int);
    174 void	setmediaopt(const char *, int);
    175 void	unsetmediaopt(const char *, int);
    176 void	setmediainst(const char *, int);
    177 void	clone_create(const char *, int);
    178 void	clone_destroy(const char *, int);
    179 int	main(int, char *[]);
    180 
    181 /*
    182  * Media stuff.  Whenever a media command is first performed, the
    183  * currently select media is grabbed for this interface.  If `media'
    184  * is given, the current media word is modifed.  `mediaopt' commands
    185  * only modify the set and clear words.  They then operate on the
    186  * current media word later.
    187  */
    188 int	media_current;
    189 int	mediaopt_set;
    190 int	mediaopt_clear;
    191 
    192 int	actions;			/* Actions performed */
    193 
    194 #define	A_MEDIA		0x0001		/* media command */
    195 #define	A_MEDIAOPTSET	0x0002		/* mediaopt command */
    196 #define	A_MEDIAOPTCLR	0x0004		/* -mediaopt command */
    197 #define	A_MEDIAOPT	(A_MEDIAOPTSET|A_MEDIAOPTCLR)
    198 #define	A_MEDIAINST	0x0008		/* instance or inst command */
    199 #define	A_MEDIAMODE	0x0010		/* mode command */
    200 
    201 #define	NEXTARG		0xffffff
    202 #define	NEXTARG2	0xfffffe
    203 
    204 const struct cmd {
    205 	const char *c_name;
    206 	int	c_parameter;	/* NEXTARG means next argv */
    207 	int	c_action;	/* defered action */
    208 	void	(*c_func)(const char *, int);
    209 	void	(*c_func2)(const char *, const char *);
    210 } cmds[] = {
    211 	{ "up",		IFF_UP,		0,		setifflags } ,
    212 	{ "down",	-IFF_UP,	0,		setifflags },
    213 	{ "trailers",	-1,		0,		notrailers },
    214 	{ "-trailers",	1,		0,		notrailers },
    215 	{ "arp",	-IFF_NOARP,	0,		setifflags },
    216 	{ "-arp",	IFF_NOARP,	0,		setifflags },
    217 	{ "debug",	IFF_DEBUG,	0,		setifflags },
    218 	{ "-debug",	-IFF_DEBUG,	0,		setifflags },
    219 	{ "alias",	IFF_UP,		0,		notealias },
    220 	{ "-alias",	-IFF_UP,	0,		notealias },
    221 	{ "delete",	-IFF_UP,	0,		notealias },
    222 #ifdef notdef
    223 #define	EN_SWABIPS	0x1000
    224 	{ "swabips",	EN_SWABIPS,	0,		setifflags },
    225 	{ "-swabips",	-EN_SWABIPS,	0,		setifflags },
    226 #endif
    227 	{ "netmask",	NEXTARG,	0,		setifnetmask },
    228 	{ "metric",	NEXTARG,	0,		setifmetric },
    229 	{ "mtu",	NEXTARG,	0,		setifmtu },
    230 	{ "bssid",	NEXTARG,	0,		setifbssid },
    231 	{ "-bssid",	-1,		0,		setifbssid },
    232 	{ "chan",	NEXTARG,	0,		setifchan },
    233 	{ "-chan",	-1,		0,		setifchan },
    234 	{ "ssid",	NEXTARG,	0,		setifnwid },
    235 	{ "nwid",	NEXTARG,	0,		setifnwid },
    236 	{ "nwkey",	NEXTARG,	0,		setifnwkey },
    237 	{ "-nwkey",	-1,		0,		setifnwkey },
    238 	{ "powersave",	1,		0,		setifpowersave },
    239 	{ "-powersave",	0,		0,		setifpowersave },
    240 	{ "powersavesleep", NEXTARG,	0,		setifpowersavesleep },
    241 	{ "broadcast",	NEXTARG,	0,		setifbroadaddr },
    242 	{ "ipdst",	NEXTARG,	0,		setifipdst },
    243 	{ "prefixlen",	NEXTARG,	0,		setifprefixlen},
    244 #ifdef INET6
    245 	{ "anycast",	IN6_IFF_ANYCAST,	0,	setia6flags },
    246 	{ "-anycast",	-IN6_IFF_ANYCAST,	0,	setia6flags },
    247 	{ "tentative",	IN6_IFF_TENTATIVE,	0,	setia6flags },
    248 	{ "-tentative",	-IN6_IFF_TENTATIVE,	0,	setia6flags },
    249 	{ "deprecated",	IN6_IFF_DEPRECATED,	0,	setia6flags },
    250 	{ "-deprecated", -IN6_IFF_DEPRECATED,	0,	setia6flags },
    251 	{ "pltime",	NEXTARG,	0,		setia6pltime },
    252 	{ "vltime",	NEXTARG,	0,		setia6vltime },
    253 	{ "eui64",	0,		0,		setia6eui64 },
    254 #endif /*INET6*/
    255 #ifndef INET_ONLY
    256 	{ "range",	NEXTARG,	0,		setatrange },
    257 	{ "phase",	NEXTARG,	0,		setatphase },
    258 	{ "snpaoffset",	NEXTARG,	0,		setsnpaoffset },
    259 	{ "nsellength",	NEXTARG,	0,		setnsellength },
    260 #endif	/* INET_ONLY */
    261 	{ "tunnel",	NEXTARG2,	0,		NULL,
    262 							settunnel } ,
    263 	{ "deletetunnel", 0,		0,		deletetunnel },
    264 	{ "vlan",	NEXTARG,	0,		setvlan } ,
    265 	{ "vlanif",	NEXTARG,	0,		setvlanif } ,
    266 	{ "-vlanif",	0,		0,		unsetvlanif } ,
    267 #if 0
    268 	/* XXX `create' special-cased below */
    269 	{ "create",	0,		0,		clone_create } ,
    270 #endif
    271 	{ "destroy",	0,		0,		clone_destroy } ,
    272 	{ "link0",	IFF_LINK0,	0,		setifflags } ,
    273 	{ "-link0",	-IFF_LINK0,	0,		setifflags } ,
    274 	{ "link1",	IFF_LINK1,	0,		setifflags } ,
    275 	{ "-link1",	-IFF_LINK1,	0,		setifflags } ,
    276 	{ "link2",	IFF_LINK2,	0,		setifflags } ,
    277 	{ "-link2",	-IFF_LINK2,	0,		setifflags } ,
    278 	{ "media",	NEXTARG,	A_MEDIA,	setmedia },
    279 	{ "mediaopt",	NEXTARG,	A_MEDIAOPTSET,	setmediaopt },
    280 	{ "-mediaopt",	NEXTARG,	A_MEDIAOPTCLR,	unsetmediaopt },
    281 	{ "mode",	NEXTARG,	A_MEDIAMODE,	setmediamode },
    282 	{ "instance",	NEXTARG,	A_MEDIAINST,	setmediainst },
    283 	{ "inst",	NEXTARG,	A_MEDIAINST,	setmediainst },
    284 	{ "ip4csum",	IFCAP_CSUM_IPv4,0,		setifcaps },
    285 	{ "-ip4csum",	-IFCAP_CSUM_IPv4,0,		setifcaps },
    286 	{ "tcp4csum",	IFCAP_CSUM_TCPv4,0,		setifcaps },
    287 	{ "-tcp4csum",	-IFCAP_CSUM_TCPv4,0,		setifcaps },
    288 	{ "udp4csum",	IFCAP_CSUM_UDPv4,0,		setifcaps },
    289 	{ "-udp4csum",	-IFCAP_CSUM_UDPv4,0,		setifcaps },
    290 	{ "tcp6csum",	IFCAP_CSUM_TCPv6,0,		setifcaps },
    291 	{ "-tcp6csum",	-IFCAP_CSUM_TCPv6,0,		setifcaps },
    292 	{ "udp6csum",	IFCAP_CSUM_UDPv6,0,		setifcaps },
    293 	{ "-udp6csum",	-IFCAP_CSUM_UDPv6,0,		setifcaps },
    294 	{ "tcp4csum-rx",IFCAP_CSUM_TCPv4_Rx,0,		setifcaps },
    295 	{ "-tcp4csum-rx",-IFCAP_CSUM_TCPv4_Rx,0,	setifcaps },
    296 	{ "udp4csum-rx",IFCAP_CSUM_UDPv4_Rx,0,		setifcaps },
    297 	{ "-udp4csum-rx",-IFCAP_CSUM_UDPv4_Rx,0,	setifcaps },
    298 	{ "tso4",	IFCAP_TSOv4,	0,		setifcaps },
    299 	{ "-tso4",	-IFCAP_TSOv4,	0,		setifcaps },
    300 	{ "agrport",	NEXTARG,	0,		agraddport } ,
    301 	{ "-agrport",	NEXTARG,	0,		agrremport } ,
    302 	{ 0,		0,		0,		setifaddr },
    303 	{ 0,		0,		0,		setifdstaddr },
    304 };
    305 
    306 int	getinfo(struct ifreq *);
    307 int	carrier(void);
    308 void	printall(const char *);
    309 void	list_cloners(void);
    310 int	prefix(void *, int);
    311 void 	status(const struct sockaddr_dl *);
    312 void 	usage(void);
    313 char	*sec2str(time_t);
    314 
    315 void	print_media_word(int, const char *);
    316 void	process_media_commands(void);
    317 void	init_current_media(void);
    318 
    319 /*
    320  * XNS support liberally adapted from code written at the University of
    321  * Maryland principally by James O'Toole and Chris Torek.
    322  */
    323 void	in_alias(struct ifreq *);
    324 void	in_status(int);
    325 void 	in_getaddr(const char *, int);
    326 void 	in_getprefix(const char *, int);
    327 #ifdef INET6
    328 void	in6_fillscopeid(struct sockaddr_in6 *sin6);
    329 void	in6_alias(struct in6_ifreq *);
    330 void	in6_status(int);
    331 void 	in6_getaddr(const char *, int);
    332 void 	in6_getprefix(const char *, int);
    333 #endif
    334 
    335 /* Known address families */
    336 const struct afswtch afs[] = {
    337 	{ "inet", AF_INET, in_status, in_getaddr, in_getprefix,
    338 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &in_addreq },
    339 #ifdef INET6
    340 	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
    341 	     SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
    342 	     /*
    343 	      * Deleting the first address before setting new one is
    344 	      * not prefered way in this protocol.
    345 	      */
    346 	     0,
    347 	     &in6_ridreq, &in6_addreq },
    348 #endif
    349 #ifndef INET_ONLY	/* small version, for boot media */
    350 	{ "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
    351 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &addreq, &addreq },
    352 	{ "ns", AF_NS, xns_status, xns_getaddr, NULL,
    353 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &addreq },
    354 	{ "iso", AF_ISO, iso_status, iso_getaddr, NULL,
    355 	     SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, SIOCGIFADDR_ISO,
    356 	     &iso_ridreq, &iso_addreq },
    357 #endif	/* INET_ONLY */
    358 	{ 0,	0,	    0,		0 }
    359 };
    360 
    361 const struct afswtch *afp;	/*the address family being set or asked about*/
    362 
    363 int
    364 main(int argc, char *argv[])
    365 {
    366 	int ch;
    367 
    368 	/* Parse command-line options */
    369 	aflag = mflag = vflag = zflag = 0;
    370 	while ((ch = getopt(argc, argv, "AabCdlmsuvz"
    371 #ifdef INET6
    372 					"L"
    373 #endif
    374 			)) != -1) {
    375 		switch (ch) {
    376 		case 'A':
    377 			warnx("-A is deprecated");
    378 			break;
    379 
    380 		case 'a':
    381 			aflag = 1;
    382 			break;
    383 
    384 		case 'b':
    385 			bflag = 1;
    386 			break;
    387 
    388 		case 'C':
    389 			Cflag = 1;
    390 			break;
    391 
    392 		case 'd':
    393 			dflag = 1;
    394 			break;
    395 
    396 #ifdef INET6
    397 		case 'L':
    398 			Lflag = 1;
    399 			break;
    400 #endif
    401 
    402 		case 'l':
    403 			lflag = 1;
    404 			break;
    405 
    406 		case 'm':
    407 			mflag = 1;
    408 			break;
    409 
    410 		case 's':
    411 			sflag = 1;
    412 			break;
    413 
    414 		case 'u':
    415 			uflag = 1;
    416 			break;
    417 
    418 		case 'v':
    419 			vflag = 1;
    420 			break;
    421 
    422 		case 'z':
    423 			zflag = 1;
    424 			break;
    425 
    426 
    427 		default:
    428 			usage();
    429 			/* NOTREACHED */
    430 		}
    431 	}
    432 	argc -= optind;
    433 	argv += optind;
    434 
    435 	/*
    436 	 * -l means "list all interfaces", and is mutally exclusive with
    437 	 * all other flags/commands.
    438 	 *
    439 	 * -C means "list all names of cloners", and it mutually exclusive
    440 	 * with all other flags/commands.
    441 	 *
    442 	 * -a means "print status of all interfaces".
    443 	 */
    444 	if ((lflag || Cflag) && (aflag || mflag || vflag || argc || zflag))
    445 		usage();
    446 #ifdef INET6
    447 	if ((lflag || Cflag) && Lflag)
    448 		usage();
    449 #endif
    450 	if (lflag && Cflag)
    451 		usage();
    452 	if (Cflag) {
    453 		if (argc)
    454 			usage();
    455 		list_cloners();
    456 		exit(0);
    457 	}
    458 	if (aflag || lflag) {
    459 		if (argc > 1)
    460 			usage();
    461 		else if (argc == 1) {
    462 			afp = lookup_af_byname(argv[0]);
    463 			if (afp == NULL)
    464 				usage();
    465 		}
    466 		if (afp)
    467 			af = ifr.ifr_addr.sa_family = afp->af_af;
    468 		else
    469 			af = ifr.ifr_addr.sa_family = afs[0].af_af;
    470 		printall(NULL);
    471 		exit(0);
    472 	}
    473 
    474 	/* Make sure there's an interface name. */
    475 	if (argc < 1)
    476 		usage();
    477 	if (strlcpy(name, argv[0], sizeof(name)) >= sizeof(name))
    478 		errx(1, "interface name '%s' too long", argv[0]);
    479 	argc--; argv++;
    480 
    481 	/*
    482 	 * NOTE:  We must special-case the `create' command right
    483 	 * here as we would otherwise fail in getinfo().
    484 	 */
    485 	if (argc > 0 && strcmp(argv[0], "create") == 0) {
    486 		clone_create(argv[0], 0);
    487 		argc--, argv++;
    488 		if (argc == 0)
    489 			exit(0);
    490 	}
    491 
    492 	/* Check for address family. */
    493 	afp = NULL;
    494 	if (argc > 0) {
    495 		afp = lookup_af_byname(argv[0]);
    496 		if (afp != NULL) {
    497 			argv++;
    498 			argc--;
    499 		}
    500 	}
    501 
    502 	/* Initialize af, just for use in getinfo(). */
    503 	if (afp == NULL)
    504 		af = afs->af_af;
    505 	else
    506 		af = afp->af_af;
    507 
    508 	/* Get information about the interface. */
    509 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    510 	if (getinfo(&ifr) < 0)
    511 		exit(1);
    512 
    513 	if (sflag) {
    514 		if (argc != 0)
    515 			usage();
    516 		else
    517 			exit(carrier());
    518 	}
    519 
    520 	/* No more arguments means interface status. */
    521 	if (argc == 0) {
    522 		printall(name);
    523 		exit(0);
    524 	}
    525 
    526 	/* The following operations assume inet family as the default. */
    527 	if (afp == NULL)
    528 		afp = afs;
    529 	af = ifr.ifr_addr.sa_family = afp->af_af;
    530 
    531 #ifdef INET6
    532 	/* initialization */
    533 	in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    534 	in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    535 #endif
    536 
    537 	/* Process commands. */
    538 	while (argc > 0) {
    539 		const struct cmd *p;
    540 
    541 		for (p = cmds; p->c_name; p++)
    542 			if (strcmp(argv[0], p->c_name) == 0)
    543 				break;
    544 		if (p->c_name == 0 && setaddr) {
    545 			if ((flags & IFF_POINTOPOINT) == 0) {
    546 				errx(EXIT_FAILURE,
    547 				    "can't set destination address %s",
    548 				     "on non-point-to-point link");
    549 			}
    550 			p++;	/* got src, do dst */
    551 		}
    552 		if (p->c_func != NULL || p->c_func2 != NULL) {
    553 			if (p->c_parameter == NEXTARG) {
    554 				if (argc < 2)
    555 					errx(EXIT_FAILURE,
    556 					    "'%s' requires argument",
    557 					    p->c_name);
    558 				(*p->c_func)(argv[1], 0);
    559 				argc--, argv++;
    560 			} else if (p->c_parameter == NEXTARG2) {
    561 				if (argc < 3)
    562 					errx(EXIT_FAILURE,
    563 					    "'%s' requires 2 arguments",
    564 					    p->c_name);
    565 				(*p->c_func2)(argv[1], argv[2]);
    566 				argc -= 2, argv += 2;
    567 			} else
    568 				(*p->c_func)(argv[0], p->c_parameter);
    569 			actions |= p->c_action;
    570 		}
    571 		argc--, argv++;
    572 	}
    573 
    574 	/*
    575 	 * See if multiple alias, -alias, or delete commands were
    576 	 * specified. More than one constitutes an invalid command line
    577 	 */
    578 
    579 	if (conflicting > 1)
    580 		errx(EXIT_FAILURE,
    581 		    "Only one use of alias, -alias or delete is valid.");
    582 
    583 	/* Process any media commands that may have been issued. */
    584 	process_media_commands();
    585 
    586 	if (af == AF_INET6 && explicit_prefix == 0) {
    587 		/*
    588 		 * Aggregatable address architecture defines all prefixes
    589 		 * are 64. So, it is convenient to set prefixlen to 64 if
    590 		 * it is not specified.
    591 		 */
    592 		setifprefixlen("64", 0);
    593 		/* in6_getprefix("64", MASK) if MASK is available here... */
    594 	}
    595 
    596 #ifndef INET_ONLY
    597 	if (af == AF_ISO)
    598 		adjust_nsellength();
    599 
    600 	if (af == AF_APPLETALK)
    601 		checkatrange(&addreq.ifra_addr);
    602 
    603 	if (setipdst && af == AF_NS)
    604 		xns_set_nsip_route(&addreq.ifra_addr, &addreq.ifra_dstaddr);
    605 #endif	/* INET_ONLY */
    606 
    607 	if (clearaddr) {
    608 		(void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
    609 		if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
    610 			err(EXIT_FAILURE, "SIOCDIFADDR");
    611 	}
    612 	if (newaddr > 0) {
    613 		(void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
    614 		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
    615 			warn("SIOCAIFADDR");
    616 	}
    617 
    618 	if (g_ifcr_updated) {
    619 		(void) strncpy(g_ifcr.ifcr_name, name,
    620 		    sizeof(g_ifcr.ifcr_name));
    621 		if (ioctl(s, SIOCSIFCAP, &g_ifcr) == -1)
    622 			err(EXIT_FAILURE, "SIOCSIFCAP");
    623 	}
    624 
    625 	exit(0);
    626 }
    627 
    628 const struct afswtch *
    629 lookup_af_byname(const char *cp)
    630 {
    631 	const struct afswtch *a;
    632 
    633 	for (a = afs; a->af_name != NULL; a++)
    634 		if (strcmp(a->af_name, cp) == 0)
    635 			return (a);
    636 	return (NULL);
    637 }
    638 
    639 const struct afswtch *
    640 lookup_af_bynum(int afnum)
    641 {
    642 	const struct afswtch *a;
    643 
    644 	for (a = afs; a->af_name != NULL; a++)
    645 		if (a->af_af == afnum)
    646 			return (a);
    647 	return (NULL);
    648 }
    649 
    650 void
    651 getsock(int naf)
    652 {
    653 	static int oaf = -1;
    654 
    655 	if (oaf == naf)
    656 		return;
    657 	if (oaf != -1)
    658 		close(s);
    659 	s = socket(naf, SOCK_DGRAM, 0);
    660 	if (s < 0)
    661 		oaf = -1;
    662 	else
    663 		oaf = naf;
    664 }
    665 
    666 int
    667 getinfo(struct ifreq *giifr)
    668 {
    669 
    670 	getsock(af);
    671 	if (s < 0)
    672 		err(EXIT_FAILURE, "socket");
    673 	if (ioctl(s, SIOCGIFFLAGS, giifr) == -1) {
    674 		warn("SIOCGIFFLAGS %s", giifr->ifr_name);
    675 		return (-1);
    676 	}
    677 	flags = giifr->ifr_flags;
    678 	if (ioctl(s, SIOCGIFMETRIC, giifr) == -1) {
    679 		warn("SIOCGIFMETRIC %s", giifr->ifr_name);
    680 		metric = 0;
    681 	} else
    682 		metric = giifr->ifr_metric;
    683 	if (ioctl(s, SIOCGIFMTU, giifr) == -1)
    684 		mtu = 0;
    685 	else
    686 		mtu = giifr->ifr_mtu;
    687 
    688 	memset(&g_ifcr, 0, sizeof(g_ifcr));
    689 	strcpy(g_ifcr.ifcr_name, giifr->ifr_name);
    690 	(void) ioctl(s, SIOCGIFCAP, &g_ifcr);
    691 
    692 	return (0);
    693 }
    694 
    695 void
    696 printall(const char *ifname)
    697 {
    698 	struct ifaddrs *ifap, *ifa;
    699 	struct ifreq paifr;
    700 	const struct sockaddr_dl *sdl = NULL;
    701 	int idx;
    702 	char *p;
    703 
    704 	if (getifaddrs(&ifap) != 0)
    705 		err(EXIT_FAILURE, "getifaddrs");
    706 	p = NULL;
    707 	idx = 0;
    708 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    709 		memset(&paifr, 0, sizeof(paifr));
    710 		strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
    711 		if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
    712 			memcpy(&paifr.ifr_addr, ifa->ifa_addr,
    713 			    ifa->ifa_addr->sa_len);
    714 		}
    715 
    716 		if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
    717 			continue;
    718 		if (ifa->ifa_addr->sa_family == AF_LINK)
    719 			sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
    720 		if (p && strcmp(p, ifa->ifa_name) == 0)
    721 			continue;
    722 		if (strlcpy(name, ifa->ifa_name, sizeof(name)) >= sizeof(name))
    723 			continue;
    724 		p = ifa->ifa_name;
    725 
    726 		if (getinfo(&paifr) < 0)
    727 			continue;
    728 		if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
    729 			continue;
    730 		if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
    731 			continue;
    732 		if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
    733 			continue;
    734 
    735 		if (sflag && carrier())
    736 			continue;
    737 		idx++;
    738 		/*
    739 		 * Are we just listing the interfaces?
    740 		 */
    741 		if (lflag) {
    742 			if (idx > 1)
    743 				printf(" ");
    744 			fputs(name, stdout);
    745 			continue;
    746 		}
    747 
    748 		status(sdl);
    749 		sdl = NULL;
    750 	}
    751 	if (lflag)
    752 		printf("\n");
    753 	freeifaddrs(ifap);
    754 }
    755 
    756 void
    757 list_cloners(void)
    758 {
    759 	struct if_clonereq ifcr;
    760 	char *cp, *buf;
    761 	int idx;
    762 
    763 	memset(&ifcr, 0, sizeof(ifcr));
    764 
    765 	getsock(AF_INET);
    766 
    767 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
    768 		err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
    769 
    770 	buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
    771 	if (buf == NULL)
    772 		err(EXIT_FAILURE, "unable to allocate cloner name buffer");
    773 
    774 	ifcr.ifcr_count = ifcr.ifcr_total;
    775 	ifcr.ifcr_buffer = buf;
    776 
    777 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
    778 		err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
    779 
    780 	/*
    781 	 * In case some disappeared in the mean time, clamp it down.
    782 	 */
    783 	if (ifcr.ifcr_count > ifcr.ifcr_total)
    784 		ifcr.ifcr_count = ifcr.ifcr_total;
    785 
    786 	for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
    787 		if (idx > 0)
    788 			printf(" ");
    789 		printf("%s", cp);
    790 	}
    791 
    792 	printf("\n");
    793 	free(buf);
    794 	return;
    795 }
    796 
    797 /*ARGSUSED*/
    798 void
    799 clone_create(const char *addr, int param)
    800 {
    801 
    802 	/* We're called early... */
    803 	getsock(AF_INET);
    804 
    805 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    806 	if (ioctl(s, SIOCIFCREATE, &ifr) == -1)
    807 		err(EXIT_FAILURE, "SIOCIFCREATE");
    808 }
    809 
    810 /*ARGSUSED*/
    811 void
    812 clone_destroy(const char *addr, int param)
    813 {
    814 
    815 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    816 	if (ioctl(s, SIOCIFDESTROY, &ifr) == -1)
    817 		err(EXIT_FAILURE, "SIOCIFDESTROY");
    818 }
    819 
    820 /*ARGSUSED*/
    821 void
    822 setifaddr(const char *addr, int param)
    823 {
    824 	struct ifreq *siifr;		/* XXX */
    825 
    826 	/*
    827 	 * Delay the ioctl to set the interface addr until flags are all set.
    828 	 * The address interpretation may depend on the flags,
    829 	 * and the flags may change when the address is set.
    830 	 */
    831 	setaddr++;
    832 	if (newaddr == -1)
    833 		newaddr = 1;
    834 	if (doalias == 0 && afp->af_gifaddr != 0) {
    835 		siifr = (struct ifreq *)afp->af_ridreq;
    836 		(void) strncpy(siifr->ifr_name, name, sizeof(siifr->ifr_name));
    837 		siifr->ifr_addr.sa_family = afp->af_af;
    838 		if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
    839 			clearaddr = 1;
    840 		else if (errno == EADDRNOTAVAIL)
    841 			/* No address was assigned yet. */
    842 			;
    843 		else
    844 			err(EXIT_FAILURE, "SIOCGIFADDR");
    845 	}
    846 
    847 	(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
    848 }
    849 
    850 void
    851 setifnetmask(const char *addr, int d)
    852 {
    853 	(*afp->af_getaddr)(addr, MASK);
    854 }
    855 
    856 void
    857 setifbroadaddr(const char *addr, int d)
    858 {
    859 	(*afp->af_getaddr)(addr, DSTADDR);
    860 }
    861 
    862 void
    863 setifipdst(const char *addr, int d)
    864 {
    865 	in_getaddr(addr, DSTADDR);
    866 	setipdst++;
    867 	clearaddr = 0;
    868 	newaddr = 0;
    869 }
    870 
    871 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
    872 /*ARGSUSED*/
    873 void
    874 notealias(const char *addr, int param)
    875 {
    876 	if (setaddr && doalias == 0 && param < 0)
    877 		(void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
    878 		    rqtosa(af_addreq)->sa_len);
    879 	doalias = param;
    880 	if (param < 0) {
    881 		clearaddr = 1;
    882 		newaddr = 0;
    883 		conflicting++;
    884 	} else {
    885 		clearaddr = 0;
    886 		conflicting++;
    887 	}
    888 }
    889 
    890 /*ARGSUSED*/
    891 void
    892 notrailers(const char *vname, int value)
    893 {
    894 	puts("Note: trailers are no longer sent, but always received");
    895 }
    896 
    897 /*ARGSUSED*/
    898 void
    899 setifdstaddr(const char *addr, int param)
    900 {
    901 	(*afp->af_getaddr)(addr, DSTADDR);
    902 }
    903 
    904 void
    905 setifflags(const char *vname, int value)
    906 {
    907 	struct ifreq ifreq;
    908 
    909 	(void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
    910  	if (ioctl(s, SIOCGIFFLAGS, &ifreq) == -1)
    911 		err(EXIT_FAILURE, "SIOCGIFFLAGS");
    912  	flags = ifreq.ifr_flags;
    913 
    914 	if (value < 0) {
    915 		value = -value;
    916 		flags &= ~value;
    917 	} else
    918 		flags |= value;
    919 	ifreq.ifr_flags = flags;
    920 	if (ioctl(s, SIOCSIFFLAGS, &ifreq) == -1)
    921 		err(EXIT_FAILURE, "SIOCSIFFLAGS");
    922 }
    923 
    924 void
    925 setifcaps(const char *vname, int value)
    926 {
    927 
    928 	if (value < 0) {
    929 		value = -value;
    930 		g_ifcr.ifcr_capenable &= ~value;
    931 	} else
    932 		g_ifcr.ifcr_capenable |= value;
    933 
    934 	g_ifcr_updated = 1;
    935 }
    936 
    937 #ifdef INET6
    938 void
    939 setia6flags(const char *vname, int value)
    940 {
    941 
    942 	if (value < 0) {
    943 		value = -value;
    944 		in6_addreq.ifra_flags &= ~value;
    945 	} else
    946 		in6_addreq.ifra_flags |= value;
    947 }
    948 
    949 void
    950 setia6pltime(const char *val, int d)
    951 {
    952 
    953 	setia6lifetime("pltime", val);
    954 }
    955 
    956 void
    957 setia6vltime(const char *val, int d)
    958 {
    959 
    960 	setia6lifetime("vltime", val);
    961 }
    962 
    963 void
    964 setia6lifetime(const char *cmd, const char *val)
    965 {
    966 	time_t newval, t;
    967 	char *ep;
    968 
    969 	t = time(NULL);
    970 	newval = (time_t)strtoul(val, &ep, 0);
    971 	if (val == ep)
    972 		errx(EXIT_FAILURE, "invalid %s", cmd);
    973 	if (afp->af_af != AF_INET6)
    974 		errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
    975 	if (strcmp(cmd, "vltime") == 0) {
    976 		in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
    977 		in6_addreq.ifra_lifetime.ia6t_vltime = newval;
    978 	} else if (strcmp(cmd, "pltime") == 0) {
    979 		in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
    980 		in6_addreq.ifra_lifetime.ia6t_pltime = newval;
    981 	}
    982 }
    983 
    984 void
    985 setia6eui64(const char *cmd, int val)
    986 {
    987 	struct ifaddrs *ifap, *ifa;
    988 	const struct sockaddr_in6 *sin6 = NULL;
    989 	const struct in6_addr *lladdr = NULL;
    990 	struct in6_addr *in6;
    991 
    992 	if (afp->af_af != AF_INET6)
    993 		errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
    994  	in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
    995 	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
    996 		errx(EXIT_FAILURE, "interface index is already filled");
    997 	if (getifaddrs(&ifap) != 0)
    998 		err(EXIT_FAILURE, "getifaddrs");
    999 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   1000 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
   1001 		    strcmp(ifa->ifa_name, name) == 0) {
   1002 			sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
   1003 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   1004 				lladdr = &sin6->sin6_addr;
   1005 				break;
   1006 			}
   1007 		}
   1008 	}
   1009 	if (!lladdr)
   1010 		errx(EXIT_FAILURE, "could not determine link local address");
   1011 
   1012  	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
   1013 
   1014 	freeifaddrs(ifap);
   1015 }
   1016 #endif
   1017 
   1018 void
   1019 setifmetric(const char *val, int d)
   1020 {
   1021 	char *ep = NULL;
   1022 
   1023 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1024 	ifr.ifr_metric = strtoul(val, &ep, 10);
   1025 	if (!ep || *ep)
   1026 		errx(EXIT_FAILURE, "%s: invalid metric", val);
   1027 	if (ioctl(s, SIOCSIFMETRIC, &ifr) == -1)
   1028 		warn("SIOCSIFMETRIC");
   1029 }
   1030 
   1031 void
   1032 setifmtu(const char *val, int d)
   1033 {
   1034 	char *ep = NULL;
   1035 
   1036 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1037 	ifr.ifr_mtu = strtoul(val, &ep, 10);
   1038 	if (!ep || *ep)
   1039 		errx(EXIT_FAILURE, "%s: invalid mtu", val);
   1040 	if (ioctl(s, SIOCSIFMTU, &ifr) == -1)
   1041 		warn("SIOCSIFMTU");
   1042 }
   1043 
   1044 const char *
   1045 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
   1046 {
   1047 	int len;
   1048 	int hexstr;
   1049 	u_int8_t *p;
   1050 
   1051 	len = *lenp;
   1052 	p = buf;
   1053 	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
   1054 	if (hexstr)
   1055 		val += 2;
   1056 	for (;;) {
   1057 		if (*val == '\0')
   1058 			break;
   1059 		if (sep != NULL && strchr(sep, *val) != NULL) {
   1060 			val++;
   1061 			break;
   1062 		}
   1063 		if (hexstr) {
   1064 			if (!isxdigit((u_char)val[0]) ||
   1065 			    !isxdigit((u_char)val[1])) {
   1066 				warnx("bad hexadecimal digits");
   1067 				return NULL;
   1068 			}
   1069 		}
   1070 		if (p > buf + len) {
   1071 			if (hexstr)
   1072 				warnx("hexadecimal digits too long");
   1073 			else
   1074 				warnx("strings too long");
   1075 			return NULL;
   1076 		}
   1077 		if (hexstr) {
   1078 #define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
   1079 			*p++ = (tohex((u_char)val[0]) << 4) |
   1080 			    tohex((u_char)val[1]);
   1081 #undef tohex
   1082 			val += 2;
   1083 		} else
   1084 			*p++ = *val++;
   1085 	}
   1086 	len = p - buf;
   1087 	if (len < *lenp)
   1088 		memset(p, 0, *lenp - len);
   1089 	*lenp = len;
   1090 	return val;
   1091 }
   1092 
   1093 void
   1094 print_string(const u_int8_t *buf, int len)
   1095 {
   1096 	int i;
   1097 	int hasspc;
   1098 
   1099 	i = 0;
   1100 	hasspc = 0;
   1101 	if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
   1102 		for (; i < len; i++) {
   1103 			if (!isprint(buf[i]))
   1104 				break;
   1105 			if (isspace(buf[i]))
   1106 				hasspc++;
   1107 		}
   1108 	}
   1109 	if (i == len) {
   1110 		if (hasspc || len == 0)
   1111 			printf("\"%.*s\"", len, buf);
   1112 		else
   1113 			printf("%.*s", len, buf);
   1114 	} else {
   1115 		printf("0x");
   1116 		for (i = 0; i < len; i++)
   1117 			printf("%02x", buf[i]);
   1118 	}
   1119 }
   1120 
   1121 static void
   1122 media_error(int type, const char *val, const char *opt)
   1123 {
   1124 	errx(EXIT_FAILURE, "unknown %s media %s: %s",
   1125 		get_media_type_string(type), opt, val);
   1126 }
   1127 
   1128 void
   1129 init_current_media(void)
   1130 {
   1131 	struct ifmediareq ifmr;
   1132 
   1133 	/*
   1134 	 * If we have not yet done so, grab the currently-selected
   1135 	 * media.
   1136 	 */
   1137 	if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
   1138 		(void) memset(&ifmr, 0, sizeof(ifmr));
   1139 		(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1140 
   1141 		if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
   1142 			/*
   1143 			 * If we get E2BIG, the kernel is telling us
   1144 			 * that there are more, so we can ignore it.
   1145 			 */
   1146 			if (errno != E2BIG)
   1147 				err(EXIT_FAILURE, "SGIOCGIFMEDIA");
   1148 		}
   1149 
   1150 		media_current = ifmr.ifm_current;
   1151 	}
   1152 
   1153 	/* Sanity. */
   1154 	if (IFM_TYPE(media_current) == 0)
   1155 		errx(EXIT_FAILURE, "%s: no link type?", name);
   1156 }
   1157 
   1158 void
   1159 process_media_commands(void)
   1160 {
   1161 
   1162 	if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
   1163 		/* Nothing to do. */
   1164 		return;
   1165 	}
   1166 
   1167 	/*
   1168 	 * Media already set up, and commands sanity-checked.  Set/clear
   1169 	 * any options, and we're ready to go.
   1170 	 */
   1171 	media_current |= mediaopt_set;
   1172 	media_current &= ~mediaopt_clear;
   1173 
   1174 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1175 	ifr.ifr_media = media_current;
   1176 
   1177 	if (ioctl(s, SIOCSIFMEDIA, &ifr) == -1)
   1178 		err(EXIT_FAILURE, "SIOCSIFMEDIA");
   1179 }
   1180 
   1181 void
   1182 setmedia(const char *val, int d)
   1183 {
   1184 	int type, subtype, inst;
   1185 
   1186 	init_current_media();
   1187 
   1188 	/* Only one media command may be given. */
   1189 	if (actions & A_MEDIA)
   1190 		errx(EXIT_FAILURE, "only one `media' command may be issued");
   1191 
   1192 	/* Must not come after mode commands */
   1193 	if (actions & A_MEDIAMODE)
   1194 		errx(EXIT_FAILURE,
   1195 		    "may not issue `media' after `mode' commands");
   1196 
   1197 	/* Must not come after mediaopt commands */
   1198 	if (actions & A_MEDIAOPT)
   1199 		errx(EXIT_FAILURE,
   1200 		    "may not issue `media' after `mediaopt' commands");
   1201 
   1202 	/*
   1203 	 * No need to check if `instance' has been issued; setmediainst()
   1204 	 * craps out if `media' has not been specified.
   1205 	 */
   1206 
   1207 	type = IFM_TYPE(media_current);
   1208 	inst = IFM_INST(media_current);
   1209 
   1210 	/* Look up the subtype. */
   1211 	subtype = get_media_subtype(type, val);
   1212 	if (subtype == -1)
   1213 		media_error(type, val, "subtype");
   1214 
   1215 	/* Build the new current media word. */
   1216 	media_current = IFM_MAKEWORD(type, subtype, 0, inst);
   1217 
   1218 	/* Media will be set after other processing is complete. */
   1219 }
   1220 
   1221 void
   1222 setmediaopt(const char *val, int d)
   1223 {
   1224 	char *invalid;
   1225 
   1226 	init_current_media();
   1227 
   1228 	/* Can only issue `mediaopt' once. */
   1229 	if (actions & A_MEDIAOPTSET)
   1230 		errx(EXIT_FAILURE, "only one `mediaopt' command may be issued");
   1231 
   1232 	/* Can't issue `mediaopt' if `instance' has already been issued. */
   1233 	if (actions & A_MEDIAINST)
   1234 		errx(EXIT_FAILURE, "may not issue `mediaopt' after `instance'");
   1235 
   1236 	mediaopt_set = get_media_options(media_current, val, &invalid);
   1237 	if (mediaopt_set == -1)
   1238 		media_error(media_current, invalid, "option");
   1239 
   1240 	/* Media will be set after other processing is complete. */
   1241 }
   1242 
   1243 void
   1244 unsetmediaopt(const char *val, int d)
   1245 {
   1246 	char *invalid;
   1247 
   1248 	init_current_media();
   1249 
   1250 	/* Can only issue `-mediaopt' once. */
   1251 	if (actions & A_MEDIAOPTCLR)
   1252 		errx(EXIT_FAILURE,
   1253 		    "only one `-mediaopt' command may be issued");
   1254 
   1255 	/* May not issue `media' and `-mediaopt'. */
   1256 	if (actions & A_MEDIA)
   1257 		errx(EXIT_FAILURE,
   1258 		    "may not issue both `media' and `-mediaopt'");
   1259 
   1260 	/*
   1261 	 * No need to check for A_MEDIAINST, since the test for A_MEDIA
   1262 	 * implicitly checks for A_MEDIAINST.
   1263 	 */
   1264 
   1265 	mediaopt_clear = get_media_options(media_current, val, &invalid);
   1266 	if (mediaopt_clear == -1)
   1267 		media_error(media_current, invalid, "option");
   1268 
   1269 	/* Media will be set after other processing is complete. */
   1270 }
   1271 
   1272 void
   1273 setmediainst(const char *val, int d)
   1274 {
   1275 	int type, subtype, options, inst;
   1276 
   1277 	init_current_media();
   1278 
   1279 	/* Can only issue `instance' once. */
   1280 	if (actions & A_MEDIAINST)
   1281 		errx(EXIT_FAILURE, "only one `instance' command may be issued");
   1282 
   1283 	/* Must have already specified `media' */
   1284 	if ((actions & A_MEDIA) == 0)
   1285 		errx(EXIT_FAILURE, "must specify `media' before `instance'");
   1286 
   1287 	type = IFM_TYPE(media_current);
   1288 	subtype = IFM_SUBTYPE(media_current);
   1289 	options = IFM_OPTIONS(media_current);
   1290 
   1291 	inst = atoi(val);
   1292 	if (inst < 0 || inst > IFM_INST_MAX)
   1293 		errx(EXIT_FAILURE, "invalid media instance: %s", val);
   1294 
   1295 	media_current = IFM_MAKEWORD(type, subtype, options, inst);
   1296 
   1297 	/* Media will be set after other processing is complete. */
   1298 }
   1299 
   1300 void
   1301 setmediamode(const char *val, int d)
   1302 {
   1303 	int type, subtype, options, inst, mode;
   1304 
   1305 	init_current_media();
   1306 
   1307 	/* Can only issue `mode' once. */
   1308 	if (actions & A_MEDIAMODE)
   1309 		errx(EXIT_FAILURE, "only one `mode' command may be issued");
   1310 
   1311 	type = IFM_TYPE(media_current);
   1312 	subtype = IFM_SUBTYPE(media_current);
   1313 	options = IFM_OPTIONS(media_current);
   1314 	inst = IFM_INST(media_current);
   1315 
   1316 	mode = get_media_mode(type, val);
   1317 	if (mode == -1)
   1318 		media_error(type, val, "mode");
   1319 
   1320 	media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
   1321 
   1322 	/* Media will be set after other processing is complete. */
   1323 }
   1324 
   1325 void
   1326 print_media_word(int ifmw, const char *opt_sep)
   1327 {
   1328 	const char *str;
   1329 
   1330 	printf("%s", get_media_subtype_string(ifmw));
   1331 
   1332 	/* Find mode. */
   1333 	if (IFM_MODE(ifmw) != 0) {
   1334 		str = get_media_mode_string(ifmw);
   1335 		if (str != NULL)
   1336 			printf(" mode %s", str);
   1337 	}
   1338 
   1339 	/* Find options. */
   1340 	for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
   1341 		printf("%s%s", opt_sep, str);
   1342 
   1343 	if (IFM_INST(ifmw) != 0)
   1344 		printf(" instance %d", IFM_INST(ifmw));
   1345 }
   1346 
   1347 int
   1348 carrier(void)
   1349 {
   1350 	struct ifmediareq ifmr;
   1351 
   1352 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1353 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1354 
   1355 	if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
   1356 		/*
   1357 		 * Interface doesn't support SIOC{G,S}IFMEDIA;
   1358 		 * assume ok.
   1359 		 */
   1360 		return 0;
   1361 	}
   1362 	if ((ifmr.ifm_status & IFM_AVALID) == 0) {
   1363 		/*
   1364 		 * Interface doesn't report media-valid status.
   1365 		 * assume ok.
   1366 		 */
   1367 		return 0;
   1368 	}
   1369 	/* otherwise, return ok for active, not-ok if not active. */
   1370 	return !(ifmr.ifm_status & IFM_ACTIVE);
   1371 }
   1372 
   1373 
   1374 #define	IFFBITS \
   1375 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
   1376 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
   1377 
   1378 #define	IFCAPBITS \
   1379 "\020\1IP4CSUM\2TCP4CSUM\3UDP4CSUM\4TCP6CSUM\5UDP6CSUM\6TCP4CSUM_Rx\7UDP4CSUM_Rx\10TSO4"
   1380 
   1381 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
   1382 
   1383 const struct ifmedia_status_description ifm_status_descriptions[] =
   1384     IFM_STATUS_DESCRIPTIONS;
   1385 
   1386 /*
   1387  * Print the status of the interface.  If an address family was
   1388  * specified, show it and it only; otherwise, show them all.
   1389  */
   1390 void
   1391 status(const struct sockaddr_dl *sdl)
   1392 {
   1393 	const struct afswtch *p = afp;
   1394 	struct ifmediareq ifmr;
   1395 	struct ifdatareq ifdr;
   1396 	int *media_list, i;
   1397 	char hbuf[NI_MAXHOST];
   1398 	char fbuf[BUFSIZ];
   1399 
   1400 	(void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
   1401 	printf("%s: flags=%s", name, &fbuf[2]);
   1402 	if (metric)
   1403 		printf(" metric %lu", metric);
   1404 	if (mtu)
   1405 		printf(" mtu %lu", mtu);
   1406 	printf("\n");
   1407 
   1408 	if (g_ifcr.ifcr_capabilities) {
   1409 		(void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
   1410 		    g_ifcr.ifcr_capabilities);
   1411 		printf("\tcapabilities=%s\n", &fbuf[2]);
   1412 		(void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
   1413 		    g_ifcr.ifcr_capenable);
   1414 		printf("\tenabled=%s\n", &fbuf[2]);
   1415 	}
   1416 
   1417 	ieee80211_status();
   1418 	vlan_status();
   1419 	tunnel_status();
   1420 	agr_status();
   1421 
   1422 	if (sdl != NULL &&
   1423 	    getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
   1424 		hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
   1425 	    hbuf[0] != '\0')
   1426 		printf("\taddress: %s\n", hbuf);
   1427 
   1428 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1429 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1430 
   1431 	if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
   1432 		/*
   1433 		 * Interface doesn't support SIOC{G,S}IFMEDIA.
   1434 		 */
   1435 		goto iface_stats;
   1436 	}
   1437 
   1438 	if (ifmr.ifm_count == 0) {
   1439 		warnx("%s: no media types?", name);
   1440 		goto iface_stats;
   1441 	}
   1442 
   1443 	media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
   1444 	if (media_list == NULL)
   1445 		err(EXIT_FAILURE, "malloc");
   1446 	ifmr.ifm_ulist = media_list;
   1447 
   1448 	if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
   1449 		err(EXIT_FAILURE, "SIOCGIFMEDIA");
   1450 
   1451 	printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
   1452 	print_media_word(ifmr.ifm_current, " ");
   1453 	if (ifmr.ifm_active != ifmr.ifm_current) {
   1454 		printf(" (");
   1455 		print_media_word(ifmr.ifm_active, " ");
   1456 		printf(")");
   1457 	}
   1458 	printf("\n");
   1459 
   1460 	if (ifmr.ifm_status & IFM_STATUS_VALID) {
   1461 		const struct ifmedia_status_description *ifms;
   1462 		int bitno, found = 0;
   1463 
   1464 		printf("\tstatus: ");
   1465 		for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
   1466 			for (ifms = ifm_status_descriptions;
   1467 			     ifms->ifms_valid != 0; ifms++) {
   1468 				if (ifms->ifms_type !=
   1469 				      IFM_TYPE(ifmr.ifm_current) ||
   1470 				    ifms->ifms_valid !=
   1471 				      ifm_status_valid_list[bitno])
   1472 					continue;
   1473 				printf("%s%s", found ? ", " : "",
   1474 				    IFM_STATUS_DESC(ifms, ifmr.ifm_status));
   1475 				found = 1;
   1476 
   1477 				/*
   1478 				 * For each valid indicator bit, there's
   1479 				 * only one entry for each media type, so
   1480 				 * terminate the inner loop now.
   1481 				 */
   1482 				break;
   1483 			}
   1484 		}
   1485 
   1486 		if (found == 0)
   1487 			printf("unknown");
   1488 		printf("\n");
   1489 	}
   1490 
   1491 	if (mflag) {
   1492 		int type, printed_type;
   1493 
   1494 		for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
   1495 			for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
   1496 				if (IFM_TYPE(media_list[i]) != type)
   1497 					continue;
   1498 				if (printed_type == 0) {
   1499 					printf("\tsupported %s media:\n",
   1500 					    get_media_type_string(type));
   1501 					printed_type = 1;
   1502 				}
   1503 				printf("\t\tmedia ");
   1504 				print_media_word(media_list[i], " mediaopt ");
   1505 				printf("\n");
   1506 			}
   1507 		}
   1508 	}
   1509 
   1510 	free(media_list);
   1511 
   1512  iface_stats:
   1513 	if (!vflag && !zflag)
   1514 		goto proto_status;
   1515 
   1516 	(void) strncpy(ifdr.ifdr_name, name, sizeof(ifdr.ifdr_name));
   1517 
   1518 	if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1) {
   1519 		err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
   1520 	} else {
   1521 		struct if_data * const ifi = &ifdr.ifdr_data;
   1522 #define	PLURAL(n)	((n) == 1 ? "" : "s")
   1523 		printf("\tinput: %llu packet%s, %llu byte%s",
   1524 		    (unsigned long long) ifi->ifi_ipackets,
   1525 		    PLURAL(ifi->ifi_ipackets),
   1526 		    (unsigned long long) ifi->ifi_ibytes,
   1527 		    PLURAL(ifi->ifi_ibytes));
   1528 		if (ifi->ifi_imcasts)
   1529 			printf(", %llu multicast%s",
   1530 			    (unsigned long long) ifi->ifi_imcasts,
   1531 			    PLURAL(ifi->ifi_imcasts));
   1532 		if (ifi->ifi_ierrors)
   1533 			printf(", %llu error%s",
   1534 			    (unsigned long long) ifi->ifi_ierrors,
   1535 			    PLURAL(ifi->ifi_ierrors));
   1536 		if (ifi->ifi_iqdrops)
   1537 			printf(", %llu queue drop%s",
   1538 			    (unsigned long long) ifi->ifi_iqdrops,
   1539 			    PLURAL(ifi->ifi_iqdrops));
   1540 		if (ifi->ifi_noproto)
   1541 			printf(", %llu unknown protocol",
   1542 			    (unsigned long long) ifi->ifi_noproto);
   1543 		printf("\n\toutput: %llu packet%s, %llu byte%s",
   1544 		    (unsigned long long) ifi->ifi_opackets,
   1545 		    PLURAL(ifi->ifi_opackets),
   1546 		    (unsigned long long) ifi->ifi_obytes,
   1547 		    PLURAL(ifi->ifi_obytes));
   1548 		if (ifi->ifi_omcasts)
   1549 			printf(", %llu multicast%s",
   1550 			    (unsigned long long) ifi->ifi_omcasts,
   1551 			    PLURAL(ifi->ifi_omcasts));
   1552 		if (ifi->ifi_oerrors)
   1553 			printf(", %llu error%s",
   1554 			    (unsigned long long) ifi->ifi_oerrors,
   1555 			    PLURAL(ifi->ifi_oerrors));
   1556 		if (ifi->ifi_collisions)
   1557 			printf(", %llu collision%s",
   1558 			    (unsigned long long) ifi->ifi_collisions,
   1559 			    PLURAL(ifi->ifi_collisions));
   1560 		printf("\n");
   1561 #undef PLURAL
   1562 	}
   1563 
   1564 	ieee80211_statistics();
   1565 
   1566  proto_status:
   1567 	if ((p = afp) != NULL) {
   1568 		(*p->af_status)(1);
   1569 	} else for (p = afs; p->af_name; p++) {
   1570 		ifr.ifr_addr.sa_family = p->af_af;
   1571 		(*p->af_status)(0);
   1572 	}
   1573 }
   1574 
   1575 void
   1576 in_alias(struct ifreq *creq)
   1577 {
   1578 	struct sockaddr_in *iasin;
   1579 	int alias;
   1580 
   1581 	if (lflag)
   1582 		return;
   1583 
   1584 	alias = 1;
   1585 
   1586 	/* Get the non-alias address for this interface. */
   1587 	getsock(AF_INET);
   1588 	if (s < 0) {
   1589 		if (errno == EPROTONOSUPPORT)
   1590 			return;
   1591 		err(EXIT_FAILURE, "socket");
   1592 	}
   1593 	(void) memset(&ifr, 0, sizeof(ifr));
   1594 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1595 	if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
   1596 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1597 			return;
   1598 		} else
   1599 			warn("SIOCGIFADDR");
   1600 	}
   1601 	/* If creq and ifr are the same address, this is not an alias. */
   1602 	if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
   1603 		   sizeof(creq->ifr_addr)) == 0)
   1604 		alias = 0;
   1605 	(void) memset(&in_addreq, 0, sizeof(in_addreq));
   1606 	(void) strncpy(in_addreq.ifra_name, name, sizeof(in_addreq.ifra_name));
   1607 	memcpy(&in_addreq.ifra_addr, &creq->ifr_addr,
   1608 	    sizeof(in_addreq.ifra_addr));
   1609 	if (ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
   1610 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1611 			return;
   1612 		} else
   1613 			warn("SIOCGIFALIAS");
   1614 	}
   1615 
   1616 	iasin = &in_addreq.ifra_addr;
   1617 	printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(iasin->sin_addr));
   1618 
   1619 	if (flags & IFF_POINTOPOINT) {
   1620 		iasin = &in_addreq.ifra_dstaddr;
   1621 		printf(" -> %s", inet_ntoa(iasin->sin_addr));
   1622 	}
   1623 
   1624 	iasin = &in_addreq.ifra_mask;
   1625 	printf(" netmask 0x%x", ntohl(iasin->sin_addr.s_addr));
   1626 
   1627 	if (flags & IFF_BROADCAST) {
   1628 		iasin = &in_addreq.ifra_broadaddr;
   1629 		printf(" broadcast %s", inet_ntoa(iasin->sin_addr));
   1630 	}
   1631 	printf("\n");
   1632 }
   1633 
   1634 void
   1635 in_status(int force)
   1636 {
   1637 	struct ifaddrs *ifap, *ifa;
   1638 	struct ifreq isifr;
   1639 
   1640 	if (getifaddrs(&ifap) != 0)
   1641 		err(EXIT_FAILURE, "getifaddrs");
   1642 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   1643 		if (strcmp(name, ifa->ifa_name) != 0)
   1644 			continue;
   1645 		if (ifa->ifa_addr->sa_family != AF_INET)
   1646 			continue;
   1647 		if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
   1648 			continue;
   1649 
   1650 		memset(&isifr, 0, sizeof(isifr));
   1651 		strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
   1652 		memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
   1653 		in_alias(&isifr);
   1654 	}
   1655 	freeifaddrs(ifap);
   1656 }
   1657 
   1658 void
   1659 setifprefixlen(const char *addr, int d)
   1660 {
   1661 	if (*afp->af_getprefix)
   1662 		(*afp->af_getprefix)(addr, MASK);
   1663 	explicit_prefix = 1;
   1664 }
   1665 
   1666 #ifdef INET6
   1667 void
   1668 in6_fillscopeid(struct sockaddr_in6 *sin6)
   1669 {
   1670 #if defined(__KAME__) && defined(KAME_SCOPEID)
   1671 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   1672 		sin6->sin6_scope_id =
   1673 			ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
   1674 		sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
   1675 	}
   1676 #endif
   1677 }
   1678 
   1679 /* XXX not really an alias */
   1680 void
   1681 in6_alias(struct in6_ifreq *creq)
   1682 {
   1683 	struct sockaddr_in6 *sin6;
   1684 	char hbuf[NI_MAXHOST];
   1685 	u_int32_t scopeid;
   1686 	const int niflag = NI_NUMERICHOST;
   1687 
   1688 	/* Get the non-alias address for this interface. */
   1689 	getsock(AF_INET6);
   1690 	if (s < 0) {
   1691 		if (errno == EPROTONOSUPPORT)
   1692 			return;
   1693 		err(EXIT_FAILURE, "socket");
   1694 	}
   1695 
   1696 	sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
   1697 
   1698 	in6_fillscopeid(sin6);
   1699 	scopeid = sin6->sin6_scope_id;
   1700 	if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
   1701 			hbuf, sizeof(hbuf), NULL, 0, niflag))
   1702 		strlcpy(hbuf, "", sizeof(hbuf));	/* some message? */
   1703 	printf("\tinet6 %s", hbuf);
   1704 
   1705 	if (flags & IFF_POINTOPOINT) {
   1706 		(void) memset(&ifr6, 0, sizeof(ifr6));
   1707 		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   1708 		ifr6.ifr_addr = creq->ifr_addr;
   1709 		if (ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
   1710 			if (errno != EADDRNOTAVAIL)
   1711 				warn("SIOCGIFDSTADDR_IN6");
   1712 			(void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
   1713 			ifr6.ifr_addr.sin6_family = AF_INET6;
   1714 			ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
   1715 		}
   1716 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
   1717 		in6_fillscopeid(sin6);
   1718 		hbuf[0] = '\0';
   1719 		if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
   1720 				hbuf, sizeof(hbuf), NULL, 0, niflag))
   1721 			strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
   1722 		printf(" -> %s", hbuf);
   1723 	}
   1724 
   1725 	(void) memset(&ifr6, 0, sizeof(ifr6));
   1726 	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   1727 	ifr6.ifr_addr = creq->ifr_addr;
   1728 	if (ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
   1729 		if (errno != EADDRNOTAVAIL)
   1730 			warn("SIOCGIFNETMASK_IN6");
   1731 	} else {
   1732 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
   1733 		printf(" prefixlen %d", prefix(&sin6->sin6_addr,
   1734 					       sizeof(struct in6_addr)));
   1735 	}
   1736 
   1737 	(void) memset(&ifr6, 0, sizeof(ifr6));
   1738 	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   1739 	ifr6.ifr_addr = creq->ifr_addr;
   1740 	if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
   1741 		if (errno != EADDRNOTAVAIL)
   1742 			warn("SIOCGIFAFLAG_IN6");
   1743 	} else {
   1744 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
   1745 			printf(" anycast");
   1746 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
   1747 			printf(" tentative");
   1748 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
   1749 			printf(" duplicated");
   1750 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
   1751 			printf(" detached");
   1752 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
   1753 			printf(" deprecated");
   1754 	}
   1755 
   1756 	if (scopeid)
   1757 		printf(" scopeid 0x%x", scopeid);
   1758 
   1759 	if (Lflag) {
   1760 		struct in6_addrlifetime *lifetime;
   1761 		(void) memset(&ifr6, 0, sizeof(ifr6));
   1762 		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   1763 		ifr6.ifr_addr = creq->ifr_addr;
   1764 		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
   1765 		if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
   1766 			if (errno != EADDRNOTAVAIL)
   1767 				warn("SIOCGIFALIFETIME_IN6");
   1768 		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
   1769 			time_t t = time(NULL);
   1770 			printf(" pltime ");
   1771 			if (lifetime->ia6t_preferred) {
   1772 				printf("%s", lifetime->ia6t_preferred < t
   1773 					? "0"
   1774 					: sec2str(lifetime->ia6t_preferred - t));
   1775 			} else
   1776 				printf("infty");
   1777 
   1778 			printf(" vltime ");
   1779 			if (lifetime->ia6t_expire) {
   1780 				printf("%s", lifetime->ia6t_expire < t
   1781 					? "0"
   1782 					: sec2str(lifetime->ia6t_expire - t));
   1783 			} else
   1784 				printf("infty");
   1785 		}
   1786 	}
   1787 
   1788 	printf("\n");
   1789 }
   1790 
   1791 void
   1792 in6_status(int force)
   1793 {
   1794 	struct ifaddrs *ifap, *ifa;
   1795 	struct in6_ifreq isifr;
   1796 
   1797 	if (getifaddrs(&ifap) != 0)
   1798 		err(EXIT_FAILURE, "getifaddrs");
   1799 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   1800 		if (strcmp(name, ifa->ifa_name) != 0)
   1801 			continue;
   1802 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1803 			continue;
   1804 		if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
   1805 			continue;
   1806 
   1807 		memset(&isifr, 0, sizeof(isifr));
   1808 		strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
   1809 		memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
   1810 		in6_alias(&isifr);
   1811 	}
   1812 	freeifaddrs(ifap);
   1813 }
   1814 #endif /*INET6*/
   1815 
   1816 #define SIN(x) ((struct sockaddr_in *) &(x))
   1817 struct sockaddr_in *sintab[] = {
   1818     SIN(ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
   1819     SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)};
   1820 
   1821 void
   1822 in_getaddr(const char *str, int which)
   1823 {
   1824 	struct sockaddr_in *gasin = sintab[which];
   1825 	struct hostent *hp;
   1826 	struct netent *np;
   1827 
   1828 	gasin->sin_len = sizeof(*gasin);
   1829 	if (which != MASK)
   1830 		gasin->sin_family = AF_INET;
   1831 
   1832 	if (which == ADDR) {
   1833 		char *p = NULL;
   1834 		if ((p = strrchr(str, '/')) != NULL) {
   1835 			*p = '\0';
   1836 			in_getprefix(p + 1, MASK);
   1837 		}
   1838 	}
   1839 
   1840 	if (inet_aton(str, &gasin->sin_addr) == 0) {
   1841 		if ((hp = gethostbyname(str)) != NULL)
   1842 			(void) memcpy(&gasin->sin_addr, hp->h_addr, hp->h_length);
   1843 		else if ((np = getnetbyname(str)) != NULL)
   1844 			gasin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
   1845 		else
   1846 			errx(EXIT_FAILURE, "%s: bad value", str);
   1847 	}
   1848 }
   1849 
   1850 void
   1851 in_getprefix(const char *plen, int which)
   1852 {
   1853 	struct sockaddr_in *igsin = sintab[which];
   1854 	u_char *cp;
   1855 	int len = strtol(plen, (char **)NULL, 10);
   1856 
   1857 	if ((len < 0) || (len > 32))
   1858 		errx(EXIT_FAILURE, "%s: bad value", plen);
   1859 	igsin->sin_len = sizeof(*igsin);
   1860 	if (which != MASK)
   1861 		igsin->sin_family = AF_INET;
   1862 	if ((len == 0) || (len == 32)) {
   1863 		memset(&igsin->sin_addr, 0xff, sizeof(struct in_addr));
   1864 		return;
   1865 	}
   1866 	memset((void *)&igsin->sin_addr, 0x00, sizeof(igsin->sin_addr));
   1867 	for (cp = (u_char *)&igsin->sin_addr; len > 7; len -= 8)
   1868 		*cp++ = 0xff;
   1869 	if (len)
   1870 		*cp = 0xff << (8 - len);
   1871 }
   1872 
   1873 #ifdef INET6
   1874 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
   1875 struct sockaddr_in6 *sin6tab[] = {
   1876     SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
   1877     SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
   1878 
   1879 void
   1880 in6_getaddr(const char *str, int which)
   1881 {
   1882 #if defined(__KAME__) && defined(KAME_SCOPEID)
   1883 	struct sockaddr_in6 *sin6 = sin6tab[which];
   1884 	struct addrinfo hints, *res;
   1885 	int error;
   1886 	char *slash = NULL;
   1887 
   1888 	if (which == ADDR) {
   1889 		if ((slash = strrchr(str, '/')) != NULL)
   1890 			*slash = '\0';
   1891 	}
   1892 
   1893 	memset(&hints, 0, sizeof(hints));
   1894 	hints.ai_family = AF_INET6;
   1895 	hints.ai_socktype = SOCK_DGRAM;
   1896 #if 0 /* in_getaddr() allows FQDN */
   1897 	hints.ai_flags = AI_NUMERICHOST;
   1898 #endif
   1899 	error = getaddrinfo(str, "0", &hints, &res);
   1900 	if (error && slash) {
   1901 		/* try again treating the '/' as part of the name */
   1902 		*slash = '/';
   1903 		slash = NULL;
   1904 		error = getaddrinfo(str, "0", &hints, &res);
   1905 	}
   1906 	if (error)
   1907 		errx(EXIT_FAILURE, "%s: %s", str, gai_strerror(error));
   1908 	if (res->ai_next)
   1909 		errx(EXIT_FAILURE, "%s: resolved to multiple addresses", str);
   1910 	if (res->ai_addrlen != sizeof(struct sockaddr_in6))
   1911 		errx(EXIT_FAILURE, "%s: bad value", str);
   1912 	memcpy(sin6, res->ai_addr, res->ai_addrlen);
   1913 	freeaddrinfo(res);
   1914 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) {
   1915 		*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] =
   1916 			htons(sin6->sin6_scope_id);
   1917 		sin6->sin6_scope_id = 0;
   1918 	}
   1919 	if (slash) {
   1920 		in6_getprefix(slash + 1, MASK);
   1921 		explicit_prefix = 1;
   1922 	}
   1923 #else
   1924 	struct sockaddr_in6 *gasin = sin6tab[which];
   1925 
   1926 	gasin->sin6_len = sizeof(*gasin);
   1927 	if (which != MASK)
   1928 		gasin->sin6_family = AF_INET6;
   1929 
   1930 	if (which == ADDR) {
   1931 		char *p = NULL;
   1932 		if((p = strrchr(str, '/')) != NULL) {
   1933 			*p = '\0';
   1934 			in6_getprefix(p + 1, MASK);
   1935 			explicit_prefix = 1;
   1936 		}
   1937 	}
   1938 
   1939 	if (inet_pton(AF_INET6, str, &gasin->sin6_addr) != 1)
   1940 		errx(EXIT_FAILURE, "%s: bad value", str);
   1941 #endif
   1942 }
   1943 
   1944 void
   1945 in6_getprefix(const char *plen, int which)
   1946 {
   1947 	struct sockaddr_in6 *gpsin = sin6tab[which];
   1948 	u_char *cp;
   1949 	int len = strtol(plen, (char **)NULL, 10);
   1950 
   1951 	if ((len < 0) || (len > 128))
   1952 		errx(EXIT_FAILURE, "%s: bad value", plen);
   1953 	gpsin->sin6_len = sizeof(*gpsin);
   1954 	if (which != MASK)
   1955 		gpsin->sin6_family = AF_INET6;
   1956 	if ((len == 0) || (len == 128)) {
   1957 		memset(&gpsin->sin6_addr, 0xff, sizeof(struct in6_addr));
   1958 		return;
   1959 	}
   1960 	memset((void *)&gpsin->sin6_addr, 0x00, sizeof(gpsin->sin6_addr));
   1961 	for (cp = (u_char *)&gpsin->sin6_addr; len > 7; len -= 8)
   1962 		*cp++ = 0xff;
   1963 	if (len)
   1964 		*cp = 0xff << (8 - len);
   1965 }
   1966 
   1967 int
   1968 prefix(void *val, int size)
   1969 {
   1970 	u_char *pname = (u_char *)val;
   1971 	int byte, bit, plen = 0;
   1972 
   1973 	for (byte = 0; byte < size; byte++, plen += 8)
   1974 		if (pname[byte] != 0xff)
   1975 			break;
   1976 	if (byte == size)
   1977 		return (plen);
   1978 	for (bit = 7; bit != 0; bit--, plen++)
   1979 		if (!(pname[byte] & (1 << bit)))
   1980 			break;
   1981 	for (; bit != 0; bit--)
   1982 		if (pname[byte] & (1 << bit))
   1983 			return(0);
   1984 	byte++;
   1985 	for (; byte < size; byte++)
   1986 		if (pname[byte])
   1987 			return(0);
   1988 	return (plen);
   1989 }
   1990 #endif /*INET6*/
   1991 
   1992 void
   1993 usage(void)
   1994 {
   1995 	const char *progname = getprogname();
   1996 
   1997 	fprintf(stderr,
   1998 	    "usage: %s [-m] [-v] [-z] "
   1999 #ifdef INET6
   2000 		"[-L] "
   2001 #endif
   2002 		"interface\n"
   2003 		"\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
   2004 		"\t\t[ alias | -alias ] ]\n"
   2005 		"\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
   2006 		"\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
   2007 		"\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
   2008 		"\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
   2009 		"\t[ arp | -arp ]\n"
   2010 		"\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
   2011 		"[ instance minst ]\n"
   2012 		"\t[ vlan n vlanif i ]\n"
   2013 		"\t[ agrport i ] [ -agrport i ]\n"
   2014 		"\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
   2015 		"\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
   2016 		"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
   2017 		"       %s -a [-b] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
   2018 		"       %s -l [-b] [-d] [-u] [-s]\n"
   2019 		"       %s -C\n"
   2020 		"       %s interface create\n"
   2021 		"       %s interface destroy\n",
   2022 		progname, progname, progname, progname, progname, progname);
   2023 	exit(1);
   2024 }
   2025 
   2026 #ifdef INET6
   2027 char *
   2028 sec2str(total)
   2029 	time_t total;
   2030 {
   2031 	static char result[256];
   2032 	int days, hours, mins, secs;
   2033 	int first = 1;
   2034 	char *p = result;
   2035 	char *end = &result[sizeof(result)];
   2036 	int n;
   2037 
   2038 	if (0) {	/*XXX*/
   2039 		days = total / 3600 / 24;
   2040 		hours = (total / 3600) % 24;
   2041 		mins = (total / 60) % 60;
   2042 		secs = total % 60;
   2043 
   2044 		if (days) {
   2045 			first = 0;
   2046 			n = snprintf(p, end - p, "%dd", days);
   2047 			if (n < 0 || n >= end - p)
   2048 				return(result);
   2049 			p += n;
   2050 		}
   2051 		if (!first || hours) {
   2052 			first = 0;
   2053 			n = snprintf(p, end - p, "%dh", hours);
   2054 			if (n < 0 || n >= end - p)
   2055 				return(result);
   2056 			p += n;
   2057 		}
   2058 		if (!first || mins) {
   2059 			first = 0;
   2060 			n = snprintf(p, end - p, "%dm", mins);
   2061 			if (n < 0 || n >= end - p)
   2062 				return(result);
   2063 			p += n;
   2064 		}
   2065 		snprintf(p, end - p, "%ds", secs);
   2066 	} else
   2067 		snprintf(p, end - p, "%lu", (u_long)total);
   2068 
   2069 	return(result);
   2070 }
   2071 #endif
   2072