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