Home | History | Annotate | Line # | Download | only in ifconfig
ifconfig.c revision 1.168
      1 /*	$NetBSD: ifconfig.c,v 1.168 2005/05/02 15:35:16 yamt 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.168 2005/05/02 15:35:16 yamt 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>		/* XXX */
     92 #include <netinet/in_var.h>	/* XXX */
     93 
     94 #include <netdb.h>
     95 
     96 #include <sys/protosw.h>
     97 
     98 #include <ctype.h>
     99 #include <err.h>
    100 #include <errno.h>
    101 #include <stddef.h>
    102 #include <stdio.h>
    103 #include <stdlib.h>
    104 #include <string.h>
    105 #include <unistd.h>
    106 #include <ifaddrs.h>
    107 #include <util.h>
    108 
    109 #include "extern.h"
    110 
    111 #ifndef INET_ONLY
    112 #include "af_atalk.h"
    113 #include "af_iso.h"
    114 #include "af_ns.h"
    115 #endif /* ! INET_ONLY */
    116 #include "af_inet.h"
    117 #ifdef INET6
    118 #include "af_inet6.h"
    119 #endif /* INET6 */
    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 
    129 char	name[30];
    130 u_short	flags;
    131 int	setaddr, doalias;
    132 u_long	metric, mtu;
    133 int	clearaddr, s;
    134 int	newaddr = -1;
    135 int	conflicting = 0;
    136 int	af;
    137 int	aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag, vflag, zflag;
    138 #ifdef INET6
    139 int	Lflag;
    140 #endif
    141 int	explicit_prefix = 0;
    142 
    143 struct ifcapreq g_ifcr;
    144 int	g_ifcr_updated;
    145 
    146 void 	notealias(const char *, int);
    147 void 	notrailers(const char *, int);
    148 void 	setifaddr(const char *, int);
    149 void 	setifdstaddr(const char *, int);
    150 void 	setifflags(const char *, int);
    151 void	setifcaps(const char *, int);
    152 void 	setifbroadaddr(const char *, int);
    153 void 	setifipdst(const char *, int);
    154 void 	setifmetric(const char *, int);
    155 void 	setifmtu(const char *, int);
    156 void 	setifnetmask(const char *, int);
    157 void	setifprefixlen(const char *, int);
    158 void	setmedia(const char *, int);
    159 void	setmediamode(const char *, int);
    160 void	setmediaopt(const char *, int);
    161 void	unsetmediaopt(const char *, int);
    162 void	setmediainst(const char *, int);
    163 void	clone_create(const char *, int);
    164 void	clone_destroy(const char *, int);
    165 int	main(int, char *[]);
    166 
    167 /*
    168  * Media stuff.  Whenever a media command is first performed, the
    169  * currently select media is grabbed for this interface.  If `media'
    170  * is given, the current media word is modifed.  `mediaopt' commands
    171  * only modify the set and clear words.  They then operate on the
    172  * current media word later.
    173  */
    174 int	media_current;
    175 int	mediaopt_set;
    176 int	mediaopt_clear;
    177 
    178 int	actions;			/* Actions performed */
    179 
    180 #define	A_MEDIA		0x0001		/* media command */
    181 #define	A_MEDIAOPTSET	0x0002		/* mediaopt command */
    182 #define	A_MEDIAOPTCLR	0x0004		/* -mediaopt command */
    183 #define	A_MEDIAOPT	(A_MEDIAOPTSET|A_MEDIAOPTCLR)
    184 #define	A_MEDIAINST	0x0008		/* instance or inst command */
    185 #define	A_MEDIAMODE	0x0010		/* mode command */
    186 
    187 #define	NEXTARG		0xffffff
    188 #define	NEXTARG2	0xfffffe
    189 
    190 const struct cmd {
    191 	const char *c_name;
    192 	int	c_parameter;	/* NEXTARG means next argv */
    193 	int	c_action;	/* defered action */
    194 	void	(*c_func)(const char *, int);
    195 	void	(*c_func2)(const char *, const char *);
    196 } cmds[] = {
    197 	{ "up",		IFF_UP,		0,		setifflags } ,
    198 	{ "down",	-IFF_UP,	0,		setifflags },
    199 	{ "trailers",	-1,		0,		notrailers },
    200 	{ "-trailers",	1,		0,		notrailers },
    201 	{ "arp",	-IFF_NOARP,	0,		setifflags },
    202 	{ "-arp",	IFF_NOARP,	0,		setifflags },
    203 	{ "debug",	IFF_DEBUG,	0,		setifflags },
    204 	{ "-debug",	-IFF_DEBUG,	0,		setifflags },
    205 	{ "alias",	IFF_UP,		0,		notealias },
    206 	{ "-alias",	-IFF_UP,	0,		notealias },
    207 	{ "delete",	-IFF_UP,	0,		notealias },
    208 #ifdef notdef
    209 #define	EN_SWABIPS	0x1000
    210 	{ "swabips",	EN_SWABIPS,	0,		setifflags },
    211 	{ "-swabips",	-EN_SWABIPS,	0,		setifflags },
    212 #endif
    213 	{ "netmask",	NEXTARG,	0,		setifnetmask },
    214 	{ "metric",	NEXTARG,	0,		setifmetric },
    215 	{ "mtu",	NEXTARG,	0,		setifmtu },
    216 	{ "bssid",	NEXTARG,	0,		setifbssid },
    217 	{ "-bssid",	-1,		0,		setifbssid },
    218 	{ "chan",	NEXTARG,	0,		setifchan },
    219 	{ "-chan",	-1,		0,		setifchan },
    220 	{ "ssid",	NEXTARG,	0,		setifnwid },
    221 	{ "nwid",	NEXTARG,	0,		setifnwid },
    222 	{ "nwkey",	NEXTARG,	0,		setifnwkey },
    223 	{ "-nwkey",	-1,		0,		setifnwkey },
    224 	{ "powersave",	1,		0,		setifpowersave },
    225 	{ "-powersave",	0,		0,		setifpowersave },
    226 	{ "powersavesleep", NEXTARG,	0,		setifpowersavesleep },
    227 	{ "broadcast",	NEXTARG,	0,		setifbroadaddr },
    228 	{ "ipdst",	NEXTARG,	0,		setifipdst },
    229 	{ "prefixlen",	NEXTARG,	0,		setifprefixlen},
    230 #ifdef INET6
    231 	{ "anycast",	IN6_IFF_ANYCAST,	0,	setia6flags },
    232 	{ "-anycast",	-IN6_IFF_ANYCAST,	0,	setia6flags },
    233 	{ "tentative",	IN6_IFF_TENTATIVE,	0,	setia6flags },
    234 	{ "-tentative",	-IN6_IFF_TENTATIVE,	0,	setia6flags },
    235 	{ "deprecated",	IN6_IFF_DEPRECATED,	0,	setia6flags },
    236 	{ "-deprecated", -IN6_IFF_DEPRECATED,	0,	setia6flags },
    237 	{ "pltime",	NEXTARG,	0,		setia6pltime },
    238 	{ "vltime",	NEXTARG,	0,		setia6vltime },
    239 	{ "eui64",	0,		0,		setia6eui64 },
    240 #endif /*INET6*/
    241 #ifndef INET_ONLY
    242 	{ "range",	NEXTARG,	0,		setatrange },
    243 	{ "phase",	NEXTARG,	0,		setatphase },
    244 	{ "snpaoffset",	NEXTARG,	0,		setsnpaoffset },
    245 	{ "nsellength",	NEXTARG,	0,		setnsellength },
    246 #endif	/* INET_ONLY */
    247 	{ "tunnel",	NEXTARG2,	0,		NULL,
    248 							settunnel } ,
    249 	{ "deletetunnel", 0,		0,		deletetunnel },
    250 	{ "vlan",	NEXTARG,	0,		setvlan } ,
    251 	{ "vlanif",	NEXTARG,	0,		setvlanif } ,
    252 	{ "-vlanif",	0,		0,		unsetvlanif } ,
    253 #if 0
    254 	/* XXX `create' special-cased below */
    255 	{ "create",	0,		0,		clone_create } ,
    256 #endif
    257 	{ "destroy",	0,		0,		clone_destroy } ,
    258 	{ "link0",	IFF_LINK0,	0,		setifflags } ,
    259 	{ "-link0",	-IFF_LINK0,	0,		setifflags } ,
    260 	{ "link1",	IFF_LINK1,	0,		setifflags } ,
    261 	{ "-link1",	-IFF_LINK1,	0,		setifflags } ,
    262 	{ "link2",	IFF_LINK2,	0,		setifflags } ,
    263 	{ "-link2",	-IFF_LINK2,	0,		setifflags } ,
    264 	{ "media",	NEXTARG,	A_MEDIA,	setmedia },
    265 	{ "mediaopt",	NEXTARG,	A_MEDIAOPTSET,	setmediaopt },
    266 	{ "-mediaopt",	NEXTARG,	A_MEDIAOPTCLR,	unsetmediaopt },
    267 	{ "mode",	NEXTARG,	A_MEDIAMODE,	setmediamode },
    268 	{ "instance",	NEXTARG,	A_MEDIAINST,	setmediainst },
    269 	{ "inst",	NEXTARG,	A_MEDIAINST,	setmediainst },
    270 	{ "ip4csum-tx",	IFCAP_CSUM_IPv4_Tx,0,		setifcaps },
    271 	{ "-ip4csum-tx",-IFCAP_CSUM_IPv4_Tx,0,		setifcaps },
    272 	{ "ip4csum-rx",	IFCAP_CSUM_IPv4_Rx,0,		setifcaps },
    273 	{ "-ip4csum-rx",-IFCAP_CSUM_IPv4_Rx,0,		setifcaps },
    274 	{ "tcp4csum-tx",IFCAP_CSUM_TCPv4_Tx,0,		setifcaps },
    275 	{ "-tcp4csum-tx",-IFCAP_CSUM_TCPv4_Tx,0,	setifcaps },
    276 	{ "tcp4csum-rx",IFCAP_CSUM_TCPv4_Rx,0,		setifcaps },
    277 	{ "-tcp4csum-rx",-IFCAP_CSUM_TCPv4_Rx,0,	setifcaps },
    278 	{ "udp4csum-tx",IFCAP_CSUM_UDPv4_Tx,0,		setifcaps },
    279 	{ "-udp4csum-tx",-IFCAP_CSUM_UDPv4_Tx,0,	setifcaps },
    280 	{ "udp4csum-rx",IFCAP_CSUM_UDPv4_Rx,0,		setifcaps },
    281 	{ "-udp4csum-rx",-IFCAP_CSUM_UDPv4_Rx,0,	setifcaps },
    282 	{ "tcp6csum-tx",IFCAP_CSUM_TCPv6_Tx,0,		setifcaps },
    283 	{ "-tcp6csum-tx",-IFCAP_CSUM_TCPv6_Tx,0,	setifcaps },
    284 	{ "tcp6csum-rx",IFCAP_CSUM_TCPv6_Rx,0,		setifcaps },
    285 	{ "-tcp6csum-rx",-IFCAP_CSUM_TCPv6_Rx,0,	setifcaps },
    286 	{ "udp6csum-tx",IFCAP_CSUM_UDPv6_Tx,0,		setifcaps },
    287 	{ "-udp6csum-tx",-IFCAP_CSUM_UDPv6_Tx,0,	setifcaps },
    288 	{ "udp6csum-rx",IFCAP_CSUM_UDPv6_Rx,0,		setifcaps },
    289 	{ "-udp6csum-rx",-IFCAP_CSUM_UDPv6_Rx,0,	setifcaps },
    290 	{ "ip4csum",	IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx,
    291 					0,		setifcaps },
    292 	{ "-ip4csum",	-(IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx),
    293 					0,		setifcaps },
    294 	{ "tcp4csum",	IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx,
    295 					0,		setifcaps },
    296 	{ "-tcp4csum",	-(IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx),
    297 					0,		setifcaps },
    298 	{ "udp4csum",	IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx,
    299 					0,		setifcaps },
    300 	{ "-udp4csum",	-(IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx),
    301 					0,		setifcaps },
    302 	{ "tcp6csum",	IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx,
    303 					0,		setifcaps },
    304 	{ "-tcp6csum",	-(IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx),
    305 					0,		setifcaps },
    306 	{ "udp6csum",	IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx,
    307 					0,		setifcaps },
    308 	{ "-udp6csum",	-(IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx),
    309 					0,		setifcaps },
    310 	{ "tso4",	IFCAP_TSOv4,	0,		setifcaps },
    311 	{ "-tso4",	-IFCAP_TSOv4,	0,		setifcaps },
    312 	{ "agrport",	NEXTARG,	0,		agraddport } ,
    313 	{ "-agrport",	NEXTARG,	0,		agrremport } ,
    314 	{ 0,		0,		0,		setifaddr },
    315 	{ 0,		0,		0,		setifdstaddr },
    316 };
    317 
    318 int	getinfo(struct ifreq *);
    319 int	carrier(void);
    320 void	printall(const char *);
    321 void	list_cloners(void);
    322 void 	status(const struct sockaddr_dl *);
    323 void 	usage(void);
    324 
    325 void	print_media_word(int, const char *);
    326 void	process_media_commands(void);
    327 void	init_current_media(void);
    328 
    329 /* Known address families */
    330 const struct afswtch afs[] = {
    331 	{ "inet", AF_INET, in_status, in_getaddr, in_getprefix,
    332 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &in_addreq },
    333 #ifdef INET6
    334 	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
    335 	     SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
    336 	     /*
    337 	      * Deleting the first address before setting new one is
    338 	      * not prefered way in this protocol.
    339 	      */
    340 	     0,
    341 	     &in6_ridreq, &in6_addreq },
    342 #endif
    343 #ifndef INET_ONLY	/* small version, for boot media */
    344 	{ "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
    345 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &addreq, &addreq },
    346 	{ "ns", AF_NS, xns_status, xns_getaddr, NULL,
    347 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &addreq },
    348 	{ "iso", AF_ISO, iso_status, iso_getaddr, NULL,
    349 	     SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, SIOCGIFADDR_ISO,
    350 	     &iso_ridreq, &iso_addreq },
    351 #endif	/* INET_ONLY */
    352 	{ 0,	0,	    0,		0 }
    353 };
    354 
    355 const struct afswtch *afp;	/*the address family being set or asked about*/
    356 
    357 int
    358 main(int argc, char *argv[])
    359 {
    360 	int ch;
    361 
    362 	/* Parse command-line options */
    363 	aflag = mflag = vflag = zflag = 0;
    364 	while ((ch = getopt(argc, argv, "AabCdlmsuvz"
    365 #ifdef INET6
    366 					"L"
    367 #endif
    368 			)) != -1) {
    369 		switch (ch) {
    370 		case 'A':
    371 			warnx("-A is deprecated");
    372 			break;
    373 
    374 		case 'a':
    375 			aflag = 1;
    376 			break;
    377 
    378 		case 'b':
    379 			bflag = 1;
    380 			break;
    381 
    382 		case 'C':
    383 			Cflag = 1;
    384 			break;
    385 
    386 		case 'd':
    387 			dflag = 1;
    388 			break;
    389 
    390 #ifdef INET6
    391 		case 'L':
    392 			Lflag = 1;
    393 			break;
    394 #endif
    395 
    396 		case 'l':
    397 			lflag = 1;
    398 			break;
    399 
    400 		case 'm':
    401 			mflag = 1;
    402 			break;
    403 
    404 		case 's':
    405 			sflag = 1;
    406 			break;
    407 
    408 		case 'u':
    409 			uflag = 1;
    410 			break;
    411 
    412 		case 'v':
    413 			vflag = 1;
    414 			break;
    415 
    416 		case 'z':
    417 			zflag = 1;
    418 			break;
    419 
    420 
    421 		default:
    422 			usage();
    423 			/* NOTREACHED */
    424 		}
    425 	}
    426 	argc -= optind;
    427 	argv += optind;
    428 
    429 	/*
    430 	 * -l means "list all interfaces", and is mutally exclusive with
    431 	 * all other flags/commands.
    432 	 *
    433 	 * -C means "list all names of cloners", and it mutually exclusive
    434 	 * with all other flags/commands.
    435 	 *
    436 	 * -a means "print status of all interfaces".
    437 	 */
    438 	if ((lflag || Cflag) && (aflag || mflag || vflag || argc || zflag))
    439 		usage();
    440 #ifdef INET6
    441 	if ((lflag || Cflag) && Lflag)
    442 		usage();
    443 #endif
    444 	if (lflag && Cflag)
    445 		usage();
    446 	if (Cflag) {
    447 		if (argc)
    448 			usage();
    449 		list_cloners();
    450 		exit(0);
    451 	}
    452 	if (aflag || lflag) {
    453 		if (argc > 1)
    454 			usage();
    455 		else if (argc == 1) {
    456 			afp = lookup_af_byname(argv[0]);
    457 			if (afp == NULL)
    458 				usage();
    459 		}
    460 		if (afp)
    461 			af = ifr.ifr_addr.sa_family = afp->af_af;
    462 		else
    463 			af = ifr.ifr_addr.sa_family = afs[0].af_af;
    464 		printall(NULL);
    465 		exit(0);
    466 	}
    467 
    468 	/* Make sure there's an interface name. */
    469 	if (argc < 1)
    470 		usage();
    471 	if (strlcpy(name, argv[0], sizeof(name)) >= sizeof(name))
    472 		errx(1, "interface name '%s' too long", argv[0]);
    473 	argc--; argv++;
    474 
    475 	/*
    476 	 * NOTE:  We must special-case the `create' command right
    477 	 * here as we would otherwise fail in getinfo().
    478 	 */
    479 	if (argc > 0 && strcmp(argv[0], "create") == 0) {
    480 		clone_create(argv[0], 0);
    481 		argc--, argv++;
    482 		if (argc == 0)
    483 			exit(0);
    484 	}
    485 
    486 	/* Check for address family. */
    487 	afp = NULL;
    488 	if (argc > 0) {
    489 		afp = lookup_af_byname(argv[0]);
    490 		if (afp != NULL) {
    491 			argv++;
    492 			argc--;
    493 		}
    494 	}
    495 
    496 	/* Initialize af, just for use in getinfo(). */
    497 	if (afp == NULL)
    498 		af = afs->af_af;
    499 	else
    500 		af = afp->af_af;
    501 
    502 	/* Get information about the interface. */
    503 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    504 	if (getinfo(&ifr) < 0)
    505 		exit(1);
    506 
    507 	if (sflag) {
    508 		if (argc != 0)
    509 			usage();
    510 		else
    511 			exit(carrier());
    512 	}
    513 
    514 	/* No more arguments means interface status. */
    515 	if (argc == 0) {
    516 		printall(name);
    517 		exit(0);
    518 	}
    519 
    520 	/* The following operations assume inet family as the default. */
    521 	if (afp == NULL)
    522 		afp = afs;
    523 	af = ifr.ifr_addr.sa_family = afp->af_af;
    524 
    525 #ifdef INET6
    526 	in6_init();
    527 #endif
    528 
    529 	/* Process commands. */
    530 	while (argc > 0) {
    531 		const struct cmd *p;
    532 
    533 		for (p = cmds; p->c_name; p++)
    534 			if (strcmp(argv[0], p->c_name) == 0)
    535 				break;
    536 		if (p->c_name == 0 && setaddr) {
    537 			if ((flags & IFF_POINTOPOINT) == 0) {
    538 				errx(EXIT_FAILURE,
    539 				    "can't set destination address %s",
    540 				     "on non-point-to-point link");
    541 			}
    542 			p++;	/* got src, do dst */
    543 		}
    544 		if (p->c_func != NULL || p->c_func2 != NULL) {
    545 			if (p->c_parameter == NEXTARG) {
    546 				if (argc < 2)
    547 					errx(EXIT_FAILURE,
    548 					    "'%s' requires argument",
    549 					    p->c_name);
    550 				(*p->c_func)(argv[1], 0);
    551 				argc--, argv++;
    552 			} else if (p->c_parameter == NEXTARG2) {
    553 				if (argc < 3)
    554 					errx(EXIT_FAILURE,
    555 					    "'%s' requires 2 arguments",
    556 					    p->c_name);
    557 				(*p->c_func2)(argv[1], argv[2]);
    558 				argc -= 2, argv += 2;
    559 			} else
    560 				(*p->c_func)(argv[0], p->c_parameter);
    561 			actions |= p->c_action;
    562 		}
    563 		argc--, argv++;
    564 	}
    565 
    566 	/*
    567 	 * See if multiple alias, -alias, or delete commands were
    568 	 * specified. More than one constitutes an invalid command line
    569 	 */
    570 
    571 	if (conflicting > 1)
    572 		errx(EXIT_FAILURE,
    573 		    "Only one use of alias, -alias or delete is valid.");
    574 
    575 	/* Process any media commands that may have been issued. */
    576 	process_media_commands();
    577 
    578 	if (af == AF_INET6 && explicit_prefix == 0) {
    579 		/*
    580 		 * Aggregatable address architecture defines all prefixes
    581 		 * are 64. So, it is convenient to set prefixlen to 64 if
    582 		 * it is not specified.
    583 		 */
    584 		setifprefixlen("64", 0);
    585 		/* in6_getprefix("64", MASK) if MASK is available here... */
    586 	}
    587 
    588 #ifndef INET_ONLY
    589 	if (af == AF_ISO)
    590 		adjust_nsellength();
    591 
    592 	if (af == AF_APPLETALK)
    593 		checkatrange(&addreq.ifra_addr);
    594 
    595 	if (setipdst && af == AF_NS)
    596 		xns_set_nsip_route(&addreq.ifra_addr, &addreq.ifra_dstaddr);
    597 #endif	/* INET_ONLY */
    598 
    599 	if (clearaddr) {
    600 		(void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
    601 		if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
    602 			err(EXIT_FAILURE, "SIOCDIFADDR");
    603 	}
    604 	if (newaddr > 0) {
    605 		(void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
    606 		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
    607 			warn("SIOCAIFADDR");
    608 	}
    609 
    610 	if (g_ifcr_updated) {
    611 		(void) strncpy(g_ifcr.ifcr_name, name,
    612 		    sizeof(g_ifcr.ifcr_name));
    613 		if (ioctl(s, SIOCSIFCAP, &g_ifcr) == -1)
    614 			err(EXIT_FAILURE, "SIOCSIFCAP");
    615 	}
    616 
    617 	exit(0);
    618 }
    619 
    620 const struct afswtch *
    621 lookup_af_byname(const char *cp)
    622 {
    623 	const struct afswtch *a;
    624 
    625 	for (a = afs; a->af_name != NULL; a++)
    626 		if (strcmp(a->af_name, cp) == 0)
    627 			return (a);
    628 	return (NULL);
    629 }
    630 
    631 const struct afswtch *
    632 lookup_af_bynum(int afnum)
    633 {
    634 	const struct afswtch *a;
    635 
    636 	for (a = afs; a->af_name != NULL; a++)
    637 		if (a->af_af == afnum)
    638 			return (a);
    639 	return (NULL);
    640 }
    641 
    642 void
    643 getsock(int naf)
    644 {
    645 	static int oaf = -1;
    646 
    647 	if (oaf == naf)
    648 		return;
    649 	if (oaf != -1)
    650 		close(s);
    651 	s = socket(naf, SOCK_DGRAM, 0);
    652 	if (s < 0)
    653 		oaf = -1;
    654 	else
    655 		oaf = naf;
    656 }
    657 
    658 int
    659 getinfo(struct ifreq *giifr)
    660 {
    661 
    662 	getsock(af);
    663 	if (s < 0)
    664 		err(EXIT_FAILURE, "socket");
    665 	if (ioctl(s, SIOCGIFFLAGS, giifr) == -1) {
    666 		warn("SIOCGIFFLAGS %s", giifr->ifr_name);
    667 		return (-1);
    668 	}
    669 	flags = giifr->ifr_flags;
    670 	if (ioctl(s, SIOCGIFMETRIC, giifr) == -1) {
    671 		warn("SIOCGIFMETRIC %s", giifr->ifr_name);
    672 		metric = 0;
    673 	} else
    674 		metric = giifr->ifr_metric;
    675 	if (ioctl(s, SIOCGIFMTU, giifr) == -1)
    676 		mtu = 0;
    677 	else
    678 		mtu = giifr->ifr_mtu;
    679 
    680 	memset(&g_ifcr, 0, sizeof(g_ifcr));
    681 	strcpy(g_ifcr.ifcr_name, giifr->ifr_name);
    682 	(void) ioctl(s, SIOCGIFCAP, &g_ifcr);
    683 
    684 	return (0);
    685 }
    686 
    687 void
    688 printall(const char *ifname)
    689 {
    690 	struct ifaddrs *ifap, *ifa;
    691 	struct ifreq paifr;
    692 	const struct sockaddr_dl *sdl = NULL;
    693 	int idx;
    694 	char *p;
    695 
    696 	if (getifaddrs(&ifap) != 0)
    697 		err(EXIT_FAILURE, "getifaddrs");
    698 	p = NULL;
    699 	idx = 0;
    700 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    701 		memset(&paifr, 0, sizeof(paifr));
    702 		strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
    703 		if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
    704 			memcpy(&paifr.ifr_addr, ifa->ifa_addr,
    705 			    ifa->ifa_addr->sa_len);
    706 		}
    707 
    708 		if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
    709 			continue;
    710 		if (ifa->ifa_addr->sa_family == AF_LINK)
    711 			sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
    712 		if (p && strcmp(p, ifa->ifa_name) == 0)
    713 			continue;
    714 		if (strlcpy(name, ifa->ifa_name, sizeof(name)) >= sizeof(name))
    715 			continue;
    716 		p = ifa->ifa_name;
    717 
    718 		if (getinfo(&paifr) < 0)
    719 			continue;
    720 		if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
    721 			continue;
    722 		if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
    723 			continue;
    724 		if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
    725 			continue;
    726 
    727 		if (sflag && carrier())
    728 			continue;
    729 		idx++;
    730 		/*
    731 		 * Are we just listing the interfaces?
    732 		 */
    733 		if (lflag) {
    734 			if (idx > 1)
    735 				printf(" ");
    736 			fputs(name, stdout);
    737 			continue;
    738 		}
    739 
    740 		status(sdl);
    741 		sdl = NULL;
    742 	}
    743 	if (lflag)
    744 		printf("\n");
    745 	freeifaddrs(ifap);
    746 }
    747 
    748 void
    749 list_cloners(void)
    750 {
    751 	struct if_clonereq ifcr;
    752 	char *cp, *buf;
    753 	int idx;
    754 
    755 	memset(&ifcr, 0, sizeof(ifcr));
    756 
    757 	getsock(AF_INET);
    758 
    759 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
    760 		err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
    761 
    762 	buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
    763 	if (buf == NULL)
    764 		err(EXIT_FAILURE, "unable to allocate cloner name buffer");
    765 
    766 	ifcr.ifcr_count = ifcr.ifcr_total;
    767 	ifcr.ifcr_buffer = buf;
    768 
    769 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
    770 		err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
    771 
    772 	/*
    773 	 * In case some disappeared in the mean time, clamp it down.
    774 	 */
    775 	if (ifcr.ifcr_count > ifcr.ifcr_total)
    776 		ifcr.ifcr_count = ifcr.ifcr_total;
    777 
    778 	for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
    779 		if (idx > 0)
    780 			printf(" ");
    781 		printf("%s", cp);
    782 	}
    783 
    784 	printf("\n");
    785 	free(buf);
    786 	return;
    787 }
    788 
    789 /*ARGSUSED*/
    790 void
    791 clone_create(const char *addr, int param)
    792 {
    793 
    794 	/* We're called early... */
    795 	getsock(AF_INET);
    796 
    797 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    798 	if (ioctl(s, SIOCIFCREATE, &ifr) == -1)
    799 		err(EXIT_FAILURE, "SIOCIFCREATE");
    800 }
    801 
    802 /*ARGSUSED*/
    803 void
    804 clone_destroy(const char *addr, int param)
    805 {
    806 
    807 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    808 	if (ioctl(s, SIOCIFDESTROY, &ifr) == -1)
    809 		err(EXIT_FAILURE, "SIOCIFDESTROY");
    810 }
    811 
    812 /*ARGSUSED*/
    813 void
    814 setifaddr(const char *addr, int param)
    815 {
    816 	struct ifreq *siifr;		/* XXX */
    817 
    818 	/*
    819 	 * Delay the ioctl to set the interface addr until flags are all set.
    820 	 * The address interpretation may depend on the flags,
    821 	 * and the flags may change when the address is set.
    822 	 */
    823 	setaddr++;
    824 	if (newaddr == -1)
    825 		newaddr = 1;
    826 	if (doalias == 0 && afp->af_gifaddr != 0) {
    827 		siifr = (struct ifreq *)afp->af_ridreq;
    828 		(void) strncpy(siifr->ifr_name, name, sizeof(siifr->ifr_name));
    829 		siifr->ifr_addr.sa_family = afp->af_af;
    830 		if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
    831 			clearaddr = 1;
    832 		else if (errno == EADDRNOTAVAIL)
    833 			/* No address was assigned yet. */
    834 			;
    835 		else
    836 			err(EXIT_FAILURE, "SIOCGIFADDR");
    837 	}
    838 
    839 	(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
    840 }
    841 
    842 void
    843 setifnetmask(const char *addr, int d)
    844 {
    845 	(*afp->af_getaddr)(addr, MASK);
    846 }
    847 
    848 void
    849 setifbroadaddr(const char *addr, int d)
    850 {
    851 	(*afp->af_getaddr)(addr, DSTADDR);
    852 }
    853 
    854 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
    855 /*ARGSUSED*/
    856 void
    857 notealias(const char *addr, int param)
    858 {
    859 	if (setaddr && doalias == 0 && param < 0)
    860 		(void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
    861 		    rqtosa(af_addreq)->sa_len);
    862 	doalias = param;
    863 	if (param < 0) {
    864 		clearaddr = 1;
    865 		newaddr = 0;
    866 		conflicting++;
    867 	} else {
    868 		clearaddr = 0;
    869 		conflicting++;
    870 	}
    871 }
    872 
    873 /*ARGSUSED*/
    874 void
    875 notrailers(const char *vname, int value)
    876 {
    877 	puts("Note: trailers are no longer sent, but always received");
    878 }
    879 
    880 /*ARGSUSED*/
    881 void
    882 setifdstaddr(const char *addr, int param)
    883 {
    884 	(*afp->af_getaddr)(addr, DSTADDR);
    885 }
    886 
    887 void
    888 setifflags(const char *vname, int value)
    889 {
    890 	struct ifreq ifreq;
    891 
    892 	(void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
    893  	if (ioctl(s, SIOCGIFFLAGS, &ifreq) == -1)
    894 		err(EXIT_FAILURE, "SIOCGIFFLAGS");
    895  	flags = ifreq.ifr_flags;
    896 
    897 	if (value < 0) {
    898 		value = -value;
    899 		flags &= ~value;
    900 	} else
    901 		flags |= value;
    902 	ifreq.ifr_flags = flags;
    903 	if (ioctl(s, SIOCSIFFLAGS, &ifreq) == -1)
    904 		err(EXIT_FAILURE, "SIOCSIFFLAGS");
    905 }
    906 
    907 void
    908 setifcaps(const char *vname, int value)
    909 {
    910 
    911 	if (value < 0) {
    912 		value = -value;
    913 		g_ifcr.ifcr_capenable &= ~value;
    914 	} else
    915 		g_ifcr.ifcr_capenable |= value;
    916 
    917 	g_ifcr_updated = 1;
    918 }
    919 
    920 void
    921 setifmetric(const char *val, int d)
    922 {
    923 	char *ep = NULL;
    924 
    925 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
    926 	ifr.ifr_metric = strtoul(val, &ep, 10);
    927 	if (!ep || *ep)
    928 		errx(EXIT_FAILURE, "%s: invalid metric", val);
    929 	if (ioctl(s, SIOCSIFMETRIC, &ifr) == -1)
    930 		warn("SIOCSIFMETRIC");
    931 }
    932 
    933 void
    934 setifmtu(const char *val, int d)
    935 {
    936 	char *ep = NULL;
    937 
    938 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    939 	ifr.ifr_mtu = strtoul(val, &ep, 10);
    940 	if (!ep || *ep)
    941 		errx(EXIT_FAILURE, "%s: invalid mtu", val);
    942 	if (ioctl(s, SIOCSIFMTU, &ifr) == -1)
    943 		warn("SIOCSIFMTU");
    944 }
    945 
    946 const char *
    947 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
    948 {
    949 	int len;
    950 	int hexstr;
    951 	u_int8_t *p;
    952 
    953 	len = *lenp;
    954 	p = buf;
    955 	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
    956 	if (hexstr)
    957 		val += 2;
    958 	for (;;) {
    959 		if (*val == '\0')
    960 			break;
    961 		if (sep != NULL && strchr(sep, *val) != NULL) {
    962 			val++;
    963 			break;
    964 		}
    965 		if (hexstr) {
    966 			if (!isxdigit((u_char)val[0]) ||
    967 			    !isxdigit((u_char)val[1])) {
    968 				warnx("bad hexadecimal digits");
    969 				return NULL;
    970 			}
    971 		}
    972 		if (p > buf + len) {
    973 			if (hexstr)
    974 				warnx("hexadecimal digits too long");
    975 			else
    976 				warnx("strings too long");
    977 			return NULL;
    978 		}
    979 		if (hexstr) {
    980 #define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
    981 			*p++ = (tohex((u_char)val[0]) << 4) |
    982 			    tohex((u_char)val[1]);
    983 #undef tohex
    984 			val += 2;
    985 		} else
    986 			*p++ = *val++;
    987 	}
    988 	len = p - buf;
    989 	if (len < *lenp)
    990 		memset(p, 0, *lenp - len);
    991 	*lenp = len;
    992 	return val;
    993 }
    994 
    995 void
    996 print_string(const u_int8_t *buf, int len)
    997 {
    998 	int i;
    999 	int hasspc;
   1000 
   1001 	i = 0;
   1002 	hasspc = 0;
   1003 	if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
   1004 		for (; i < len; i++) {
   1005 			if (!isprint(buf[i]))
   1006 				break;
   1007 			if (isspace(buf[i]))
   1008 				hasspc++;
   1009 		}
   1010 	}
   1011 	if (i == len) {
   1012 		if (hasspc || len == 0)
   1013 			printf("\"%.*s\"", len, buf);
   1014 		else
   1015 			printf("%.*s", len, buf);
   1016 	} else {
   1017 		printf("0x");
   1018 		for (i = 0; i < len; i++)
   1019 			printf("%02x", buf[i]);
   1020 	}
   1021 }
   1022 
   1023 static void
   1024 media_error(int type, const char *val, const char *opt)
   1025 {
   1026 	errx(EXIT_FAILURE, "unknown %s media %s: %s",
   1027 		get_media_type_string(type), opt, val);
   1028 }
   1029 
   1030 void
   1031 init_current_media(void)
   1032 {
   1033 	struct ifmediareq ifmr;
   1034 
   1035 	/*
   1036 	 * If we have not yet done so, grab the currently-selected
   1037 	 * media.
   1038 	 */
   1039 	if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
   1040 		(void) memset(&ifmr, 0, sizeof(ifmr));
   1041 		(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1042 
   1043 		if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
   1044 			/*
   1045 			 * If we get E2BIG, the kernel is telling us
   1046 			 * that there are more, so we can ignore it.
   1047 			 */
   1048 			if (errno != E2BIG)
   1049 				err(EXIT_FAILURE, "SGIOCGIFMEDIA");
   1050 		}
   1051 
   1052 		media_current = ifmr.ifm_current;
   1053 	}
   1054 
   1055 	/* Sanity. */
   1056 	if (IFM_TYPE(media_current) == 0)
   1057 		errx(EXIT_FAILURE, "%s: no link type?", name);
   1058 }
   1059 
   1060 void
   1061 process_media_commands(void)
   1062 {
   1063 
   1064 	if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
   1065 		/* Nothing to do. */
   1066 		return;
   1067 	}
   1068 
   1069 	/*
   1070 	 * Media already set up, and commands sanity-checked.  Set/clear
   1071 	 * any options, and we're ready to go.
   1072 	 */
   1073 	media_current |= mediaopt_set;
   1074 	media_current &= ~mediaopt_clear;
   1075 
   1076 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1077 	ifr.ifr_media = media_current;
   1078 
   1079 	if (ioctl(s, SIOCSIFMEDIA, &ifr) == -1)
   1080 		err(EXIT_FAILURE, "SIOCSIFMEDIA");
   1081 }
   1082 
   1083 void
   1084 setmedia(const char *val, int d)
   1085 {
   1086 	int type, subtype, inst;
   1087 
   1088 	init_current_media();
   1089 
   1090 	/* Only one media command may be given. */
   1091 	if (actions & A_MEDIA)
   1092 		errx(EXIT_FAILURE, "only one `media' command may be issued");
   1093 
   1094 	/* Must not come after mode commands */
   1095 	if (actions & A_MEDIAMODE)
   1096 		errx(EXIT_FAILURE,
   1097 		    "may not issue `media' after `mode' commands");
   1098 
   1099 	/* Must not come after mediaopt commands */
   1100 	if (actions & A_MEDIAOPT)
   1101 		errx(EXIT_FAILURE,
   1102 		    "may not issue `media' after `mediaopt' commands");
   1103 
   1104 	/*
   1105 	 * No need to check if `instance' has been issued; setmediainst()
   1106 	 * craps out if `media' has not been specified.
   1107 	 */
   1108 
   1109 	type = IFM_TYPE(media_current);
   1110 	inst = IFM_INST(media_current);
   1111 
   1112 	/* Look up the subtype. */
   1113 	subtype = get_media_subtype(type, val);
   1114 	if (subtype == -1)
   1115 		media_error(type, val, "subtype");
   1116 
   1117 	/* Build the new current media word. */
   1118 	media_current = IFM_MAKEWORD(type, subtype, 0, inst);
   1119 
   1120 	/* Media will be set after other processing is complete. */
   1121 }
   1122 
   1123 void
   1124 setmediaopt(const char *val, int d)
   1125 {
   1126 	char *invalid;
   1127 
   1128 	init_current_media();
   1129 
   1130 	/* Can only issue `mediaopt' once. */
   1131 	if (actions & A_MEDIAOPTSET)
   1132 		errx(EXIT_FAILURE, "only one `mediaopt' command may be issued");
   1133 
   1134 	/* Can't issue `mediaopt' if `instance' has already been issued. */
   1135 	if (actions & A_MEDIAINST)
   1136 		errx(EXIT_FAILURE, "may not issue `mediaopt' after `instance'");
   1137 
   1138 	mediaopt_set = get_media_options(media_current, val, &invalid);
   1139 	if (mediaopt_set == -1)
   1140 		media_error(media_current, invalid, "option");
   1141 
   1142 	/* Media will be set after other processing is complete. */
   1143 }
   1144 
   1145 void
   1146 unsetmediaopt(const char *val, int d)
   1147 {
   1148 	char *invalid;
   1149 
   1150 	init_current_media();
   1151 
   1152 	/* Can only issue `-mediaopt' once. */
   1153 	if (actions & A_MEDIAOPTCLR)
   1154 		errx(EXIT_FAILURE,
   1155 		    "only one `-mediaopt' command may be issued");
   1156 
   1157 	/* May not issue `media' and `-mediaopt'. */
   1158 	if (actions & A_MEDIA)
   1159 		errx(EXIT_FAILURE,
   1160 		    "may not issue both `media' and `-mediaopt'");
   1161 
   1162 	/*
   1163 	 * No need to check for A_MEDIAINST, since the test for A_MEDIA
   1164 	 * implicitly checks for A_MEDIAINST.
   1165 	 */
   1166 
   1167 	mediaopt_clear = get_media_options(media_current, val, &invalid);
   1168 	if (mediaopt_clear == -1)
   1169 		media_error(media_current, invalid, "option");
   1170 
   1171 	/* Media will be set after other processing is complete. */
   1172 }
   1173 
   1174 void
   1175 setmediainst(const char *val, int d)
   1176 {
   1177 	int type, subtype, options, inst;
   1178 
   1179 	init_current_media();
   1180 
   1181 	/* Can only issue `instance' once. */
   1182 	if (actions & A_MEDIAINST)
   1183 		errx(EXIT_FAILURE, "only one `instance' command may be issued");
   1184 
   1185 	/* Must have already specified `media' */
   1186 	if ((actions & A_MEDIA) == 0)
   1187 		errx(EXIT_FAILURE, "must specify `media' before `instance'");
   1188 
   1189 	type = IFM_TYPE(media_current);
   1190 	subtype = IFM_SUBTYPE(media_current);
   1191 	options = IFM_OPTIONS(media_current);
   1192 
   1193 	inst = atoi(val);
   1194 	if (inst < 0 || inst > IFM_INST_MAX)
   1195 		errx(EXIT_FAILURE, "invalid media instance: %s", val);
   1196 
   1197 	media_current = IFM_MAKEWORD(type, subtype, options, inst);
   1198 
   1199 	/* Media will be set after other processing is complete. */
   1200 }
   1201 
   1202 void
   1203 setmediamode(const char *val, int d)
   1204 {
   1205 	int type, subtype, options, inst, mode;
   1206 
   1207 	init_current_media();
   1208 
   1209 	/* Can only issue `mode' once. */
   1210 	if (actions & A_MEDIAMODE)
   1211 		errx(EXIT_FAILURE, "only one `mode' command may be issued");
   1212 
   1213 	type = IFM_TYPE(media_current);
   1214 	subtype = IFM_SUBTYPE(media_current);
   1215 	options = IFM_OPTIONS(media_current);
   1216 	inst = IFM_INST(media_current);
   1217 
   1218 	mode = get_media_mode(type, val);
   1219 	if (mode == -1)
   1220 		media_error(type, val, "mode");
   1221 
   1222 	media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
   1223 
   1224 	/* Media will be set after other processing is complete. */
   1225 }
   1226 
   1227 void
   1228 print_media_word(int ifmw, const char *opt_sep)
   1229 {
   1230 	const char *str;
   1231 
   1232 	printf("%s", get_media_subtype_string(ifmw));
   1233 
   1234 	/* Find mode. */
   1235 	if (IFM_MODE(ifmw) != 0) {
   1236 		str = get_media_mode_string(ifmw);
   1237 		if (str != NULL)
   1238 			printf(" mode %s", str);
   1239 	}
   1240 
   1241 	/* Find options. */
   1242 	for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
   1243 		printf("%s%s", opt_sep, str);
   1244 
   1245 	if (IFM_INST(ifmw) != 0)
   1246 		printf(" instance %d", IFM_INST(ifmw));
   1247 }
   1248 
   1249 int
   1250 carrier(void)
   1251 {
   1252 	struct ifmediareq ifmr;
   1253 
   1254 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1255 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1256 
   1257 	if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
   1258 		/*
   1259 		 * Interface doesn't support SIOC{G,S}IFMEDIA;
   1260 		 * assume ok.
   1261 		 */
   1262 		return 0;
   1263 	}
   1264 	if ((ifmr.ifm_status & IFM_AVALID) == 0) {
   1265 		/*
   1266 		 * Interface doesn't report media-valid status.
   1267 		 * assume ok.
   1268 		 */
   1269 		return 0;
   1270 	}
   1271 	/* otherwise, return ok for active, not-ok if not active. */
   1272 	return !(ifmr.ifm_status & IFM_ACTIVE);
   1273 }
   1274 
   1275 
   1276 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
   1277 
   1278 const struct ifmedia_status_description ifm_status_descriptions[] =
   1279     IFM_STATUS_DESCRIPTIONS;
   1280 
   1281 /*
   1282  * Print the status of the interface.  If an address family was
   1283  * specified, show it and it only; otherwise, show them all.
   1284  */
   1285 void
   1286 status(const struct sockaddr_dl *sdl)
   1287 {
   1288 	const struct afswtch *p = afp;
   1289 	struct ifmediareq ifmr;
   1290 	struct ifdatareq ifdr;
   1291 	int *media_list, i;
   1292 	char hbuf[NI_MAXHOST];
   1293 	char fbuf[BUFSIZ];
   1294 
   1295 	(void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
   1296 	printf("%s: flags=%s", name, &fbuf[2]);
   1297 	if (metric)
   1298 		printf(" metric %lu", metric);
   1299 	if (mtu)
   1300 		printf(" mtu %lu", mtu);
   1301 	printf("\n");
   1302 
   1303 	if (g_ifcr.ifcr_capabilities) {
   1304 		(void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
   1305 		    g_ifcr.ifcr_capabilities);
   1306 		printf("\tcapabilities=%s\n", &fbuf[2]);
   1307 		(void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
   1308 		    g_ifcr.ifcr_capenable);
   1309 		printf("\tenabled=%s\n", &fbuf[2]);
   1310 	}
   1311 
   1312 	ieee80211_status();
   1313 	vlan_status();
   1314 	tunnel_status();
   1315 	agr_status();
   1316 
   1317 	if (sdl != NULL &&
   1318 	    getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
   1319 		hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
   1320 	    hbuf[0] != '\0')
   1321 		printf("\taddress: %s\n", hbuf);
   1322 
   1323 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1324 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1325 
   1326 	if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
   1327 		/*
   1328 		 * Interface doesn't support SIOC{G,S}IFMEDIA.
   1329 		 */
   1330 		goto iface_stats;
   1331 	}
   1332 
   1333 	if (ifmr.ifm_count == 0) {
   1334 		warnx("%s: no media types?", name);
   1335 		goto iface_stats;
   1336 	}
   1337 
   1338 	media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
   1339 	if (media_list == NULL)
   1340 		err(EXIT_FAILURE, "malloc");
   1341 	ifmr.ifm_ulist = media_list;
   1342 
   1343 	if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
   1344 		err(EXIT_FAILURE, "SIOCGIFMEDIA");
   1345 
   1346 	printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
   1347 	print_media_word(ifmr.ifm_current, " ");
   1348 	if (ifmr.ifm_active != ifmr.ifm_current) {
   1349 		printf(" (");
   1350 		print_media_word(ifmr.ifm_active, " ");
   1351 		printf(")");
   1352 	}
   1353 	printf("\n");
   1354 
   1355 	if (ifmr.ifm_status & IFM_STATUS_VALID) {
   1356 		const struct ifmedia_status_description *ifms;
   1357 		int bitno, found = 0;
   1358 
   1359 		printf("\tstatus: ");
   1360 		for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
   1361 			for (ifms = ifm_status_descriptions;
   1362 			     ifms->ifms_valid != 0; ifms++) {
   1363 				if (ifms->ifms_type !=
   1364 				      IFM_TYPE(ifmr.ifm_current) ||
   1365 				    ifms->ifms_valid !=
   1366 				      ifm_status_valid_list[bitno])
   1367 					continue;
   1368 				printf("%s%s", found ? ", " : "",
   1369 				    IFM_STATUS_DESC(ifms, ifmr.ifm_status));
   1370 				found = 1;
   1371 
   1372 				/*
   1373 				 * For each valid indicator bit, there's
   1374 				 * only one entry for each media type, so
   1375 				 * terminate the inner loop now.
   1376 				 */
   1377 				break;
   1378 			}
   1379 		}
   1380 
   1381 		if (found == 0)
   1382 			printf("unknown");
   1383 		printf("\n");
   1384 	}
   1385 
   1386 	if (mflag) {
   1387 		int type, printed_type;
   1388 
   1389 		for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
   1390 			for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
   1391 				if (IFM_TYPE(media_list[i]) != type)
   1392 					continue;
   1393 				if (printed_type == 0) {
   1394 					printf("\tsupported %s media:\n",
   1395 					    get_media_type_string(type));
   1396 					printed_type = 1;
   1397 				}
   1398 				printf("\t\tmedia ");
   1399 				print_media_word(media_list[i], " mediaopt ");
   1400 				printf("\n");
   1401 			}
   1402 		}
   1403 	}
   1404 
   1405 	free(media_list);
   1406 
   1407  iface_stats:
   1408 	if (!vflag && !zflag)
   1409 		goto proto_status;
   1410 
   1411 	(void) strncpy(ifdr.ifdr_name, name, sizeof(ifdr.ifdr_name));
   1412 
   1413 	if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1) {
   1414 		err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
   1415 	} else {
   1416 		struct if_data * const ifi = &ifdr.ifdr_data;
   1417 #define	PLURAL(n)	((n) == 1 ? "" : "s")
   1418 		printf("\tinput: %llu packet%s, %llu byte%s",
   1419 		    (unsigned long long) ifi->ifi_ipackets,
   1420 		    PLURAL(ifi->ifi_ipackets),
   1421 		    (unsigned long long) ifi->ifi_ibytes,
   1422 		    PLURAL(ifi->ifi_ibytes));
   1423 		if (ifi->ifi_imcasts)
   1424 			printf(", %llu multicast%s",
   1425 			    (unsigned long long) ifi->ifi_imcasts,
   1426 			    PLURAL(ifi->ifi_imcasts));
   1427 		if (ifi->ifi_ierrors)
   1428 			printf(", %llu error%s",
   1429 			    (unsigned long long) ifi->ifi_ierrors,
   1430 			    PLURAL(ifi->ifi_ierrors));
   1431 		if (ifi->ifi_iqdrops)
   1432 			printf(", %llu queue drop%s",
   1433 			    (unsigned long long) ifi->ifi_iqdrops,
   1434 			    PLURAL(ifi->ifi_iqdrops));
   1435 		if (ifi->ifi_noproto)
   1436 			printf(", %llu unknown protocol",
   1437 			    (unsigned long long) ifi->ifi_noproto);
   1438 		printf("\n\toutput: %llu packet%s, %llu byte%s",
   1439 		    (unsigned long long) ifi->ifi_opackets,
   1440 		    PLURAL(ifi->ifi_opackets),
   1441 		    (unsigned long long) ifi->ifi_obytes,
   1442 		    PLURAL(ifi->ifi_obytes));
   1443 		if (ifi->ifi_omcasts)
   1444 			printf(", %llu multicast%s",
   1445 			    (unsigned long long) ifi->ifi_omcasts,
   1446 			    PLURAL(ifi->ifi_omcasts));
   1447 		if (ifi->ifi_oerrors)
   1448 			printf(", %llu error%s",
   1449 			    (unsigned long long) ifi->ifi_oerrors,
   1450 			    PLURAL(ifi->ifi_oerrors));
   1451 		if (ifi->ifi_collisions)
   1452 			printf(", %llu collision%s",
   1453 			    (unsigned long long) ifi->ifi_collisions,
   1454 			    PLURAL(ifi->ifi_collisions));
   1455 		printf("\n");
   1456 #undef PLURAL
   1457 	}
   1458 
   1459 	ieee80211_statistics();
   1460 
   1461  proto_status:
   1462 	if ((p = afp) != NULL) {
   1463 		(*p->af_status)(1);
   1464 	} else for (p = afs; p->af_name; p++) {
   1465 		ifr.ifr_addr.sa_family = p->af_af;
   1466 		(*p->af_status)(0);
   1467 	}
   1468 }
   1469 
   1470 void
   1471 setifprefixlen(const char *addr, int d)
   1472 {
   1473 	if (*afp->af_getprefix)
   1474 		(*afp->af_getprefix)(addr, MASK);
   1475 	explicit_prefix = 1;
   1476 }
   1477 
   1478 void
   1479 usage(void)
   1480 {
   1481 	const char *progname = getprogname();
   1482 
   1483 	fprintf(stderr,
   1484 	    "usage: %s [-m] [-v] [-z] "
   1485 #ifdef INET6
   1486 		"[-L] "
   1487 #endif
   1488 		"interface\n"
   1489 		"\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
   1490 		"\t\t[ alias | -alias ] ]\n"
   1491 		"\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
   1492 		"\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
   1493 		"\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
   1494 		"\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
   1495 		"\t[ arp | -arp ]\n"
   1496 		"\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
   1497 		"[ instance minst ]\n"
   1498 		"\t[ vlan n vlanif i ]\n"
   1499 		"\t[ agrport i ] [ -agrport i ]\n"
   1500 		"\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
   1501 		"\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
   1502 		"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
   1503 		"       %s -a [-b] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
   1504 		"       %s -l [-b] [-d] [-u] [-s]\n"
   1505 		"       %s -C\n"
   1506 		"       %s interface create\n"
   1507 		"       %s interface destroy\n",
   1508 		progname, progname, progname, progname, progname, progname);
   1509 	exit(1);
   1510 }
   1511