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