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