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