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