Home | History | Annotate | Line # | Download | only in ifconfig
ifconfig.c revision 1.110
      1 /*	$NetBSD: ifconfig.c,v 1.110 2001/07/25 17:29:14 itojun 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. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 #ifndef lint
     75 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     76 	The Regents of the University of California.  All rights reserved.\n");
     77 #endif /* not lint */
     78 
     79 #ifndef lint
     80 #if 0
     81 static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
     82 #else
     83 __RCSID("$NetBSD: ifconfig.c,v 1.110 2001/07/25 17:29:14 itojun Exp $");
     84 #endif
     85 #endif /* not lint */
     86 
     87 #include <sys/param.h>
     88 #include <sys/socket.h>
     89 #include <sys/ioctl.h>
     90 
     91 #include <net/if.h>
     92 #include <net/if_dl.h>
     93 #include <net/if_media.h>
     94 #include <net/if_ether.h>
     95 #include <net/if_ieee80211.h>
     96 #include <net/if_vlanvar.h>
     97 #include <netinet/in.h>
     98 #include <netinet/in_var.h>
     99 #ifdef INET6
    100 #include <netinet6/nd6.h>
    101 #endif
    102 #include <arpa/inet.h>
    103 
    104 #include <netatalk/at.h>
    105 
    106 #define	NSIP
    107 #include <netns/ns.h>
    108 #include <netns/ns_if.h>
    109 #include <netdb.h>
    110 
    111 #define EON
    112 #include <netiso/iso.h>
    113 #include <netiso/iso_var.h>
    114 #include <sys/protosw.h>
    115 
    116 #include <ctype.h>
    117 #include <err.h>
    118 #include <errno.h>
    119 #include <stddef.h>
    120 #include <stdio.h>
    121 #include <stdlib.h>
    122 #include <string.h>
    123 #include <unistd.h>
    124 #include <ifaddrs.h>
    125 
    126 struct	ifreq		ifr, ridreq;
    127 struct	ifaliasreq	addreq __attribute__((aligned(4)));
    128 #ifdef INET6
    129 struct	in6_ifreq	ifr6;
    130 struct	in6_ifreq	in6_ridreq;
    131 struct	in6_aliasreq	in6_addreq __attribute__((aligned(4)));
    132 #endif
    133 struct	iso_ifreq	iso_ridreq;
    134 struct	iso_aliasreq	iso_addreq;
    135 struct	sockaddr_in	netmask;
    136 struct	netrange	at_nr;		/* AppleTalk net range */
    137 
    138 char	name[30];
    139 int	flags, metric, mtu, setaddr, setipdst, doalias;
    140 int	clearaddr, s;
    141 int	newaddr = -1;
    142 int	nsellength = 1;
    143 int	af;
    144 int	aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag;
    145 #ifdef INET6
    146 int	Lflag;
    147 #endif
    148 int	reset_if_flags;
    149 int	explicit_prefix = 0;
    150 u_int	vlan_tag = (u_int)-1;
    151 
    152 struct ifcapreq g_ifcr;
    153 int	g_ifcr_updated;
    154 
    155 void 	notealias __P((const char *, int));
    156 void 	notrailers __P((const char *, int));
    157 void 	setifaddr __P((const char *, int));
    158 void 	setifdstaddr __P((const char *, int));
    159 void 	setifflags __P((const char *, int));
    160 void	setifcaps __P((const char *, int));
    161 void 	setifbroadaddr __P((const char *, int));
    162 void 	setifipdst __P((const char *, int));
    163 void 	setifmetric __P((const char *, int));
    164 void 	setifmtu __P((const char *, int));
    165 void	setifnwid __P((const char *, int));
    166 void	setifnwkey __P((const char *, int));
    167 void	setifpowersave __P((const char *, int));
    168 void	setifpowersavesleep __P((const char *, int));
    169 void 	setifnetmask __P((const char *, int));
    170 void	setifprefixlen __P((const char *, int));
    171 void 	setnsellength __P((const char *, int));
    172 void 	setsnpaoffset __P((const char *, int));
    173 void	setatrange __P((const char *, int));
    174 void	setatphase __P((const char *, int));
    175 void	settunnel __P((const char *, const char *));
    176 void	deletetunnel __P((const char *, int));
    177 #ifdef INET6
    178 void 	setia6flags __P((const char *, int));
    179 void	setia6pltime __P((const char *, int));
    180 void	setia6vltime __P((const char *, int));
    181 void	setia6lifetime __P((const char *, const char *));
    182 #endif
    183 void	checkatrange __P ((struct sockaddr_at *));
    184 void	setmedia __P((const char *, int));
    185 void	setmediaopt __P((const char *, int));
    186 void	unsetmediaopt __P((const char *, int));
    187 void	setmediainst __P((const char *, int));
    188 void	clone_create __P((const char *, int));
    189 void	clone_destroy __P((const char *, int));
    190 void	fixnsel __P((struct sockaddr_iso *));
    191 void	setvlan __P((const char *, int));
    192 void	setvlanif __P((const char *, int));
    193 void	unsetvlanif __P((const char *, int));
    194 int	main __P((int, char *[]));
    195 
    196 /*
    197  * Media stuff.  Whenever a media command is first performed, the
    198  * currently select media is grabbed for this interface.  If `media'
    199  * is given, the current media word is modifed.  `mediaopt' commands
    200  * only modify the set and clear words.  They then operate on the
    201  * current media word later.
    202  */
    203 int	media_current;
    204 int	mediaopt_set;
    205 int	mediaopt_clear;
    206 
    207 int	actions;			/* Actions performed */
    208 
    209 #define	A_MEDIA		0x0001		/* media command */
    210 #define	A_MEDIAOPTSET	0x0002		/* mediaopt command */
    211 #define	A_MEDIAOPTCLR	0x0004		/* -mediaopt command */
    212 #define	A_MEDIAOPT	(A_MEDIAOPTSET|A_MEDIAOPTCLR)
    213 #define	A_MEDIAINST	0x0008		/* instance or inst command */
    214 
    215 #define	NEXTARG		0xffffff
    216 #define	NEXTARG2	0xfffffe
    217 
    218 const struct cmd {
    219 	const char *c_name;
    220 	int	c_parameter;	/* NEXTARG means next argv */
    221 	int	c_action;	/* defered action */
    222 	void	(*c_func) __P((const char *, int));
    223 	void	(*c_func2) __P((const char *, const char *));
    224 } cmds[] = {
    225 	{ "up",		IFF_UP,		0,		setifflags } ,
    226 	{ "down",	-IFF_UP,	0,		setifflags },
    227 	{ "trailers",	-1,		0,		notrailers },
    228 	{ "-trailers",	1,		0,		notrailers },
    229 	{ "arp",	-IFF_NOARP,	0,		setifflags },
    230 	{ "-arp",	IFF_NOARP,	0,		setifflags },
    231 	{ "debug",	IFF_DEBUG,	0,		setifflags },
    232 	{ "-debug",	-IFF_DEBUG,	0,		setifflags },
    233 	{ "alias",	IFF_UP,		0,		notealias },
    234 	{ "-alias",	-IFF_UP,	0,		notealias },
    235 	{ "delete",	-IFF_UP,	0,		notealias },
    236 #ifdef notdef
    237 #define	EN_SWABIPS	0x1000
    238 	{ "swabips",	EN_SWABIPS,	0,		setifflags },
    239 	{ "-swabips",	-EN_SWABIPS,	0,		setifflags },
    240 #endif
    241 	{ "netmask",	NEXTARG,	0,		setifnetmask },
    242 	{ "metric",	NEXTARG,	0,		setifmetric },
    243 	{ "mtu",	NEXTARG,	0,		setifmtu },
    244 	{ "nwid",	NEXTARG,	0,		setifnwid },
    245 	{ "nwkey",	NEXTARG,	0,		setifnwkey },
    246 	{ "-nwkey",	-1,		0,		setifnwkey },
    247 	{ "powersave",	1,		0,		setifpowersave },
    248 	{ "-powersave",	0,		0,		setifpowersave },
    249 	{ "powersavesleep", NEXTARG,	0,		setifpowersavesleep },
    250 	{ "broadcast",	NEXTARG,	0,		setifbroadaddr },
    251 	{ "ipdst",	NEXTARG,	0,		setifipdst },
    252 	{ "prefixlen",  NEXTARG,	0,		setifprefixlen},
    253 #ifdef INET6
    254 	{ "anycast",	IN6_IFF_ANYCAST,	0,	setia6flags },
    255 	{ "-anycast",	-IN6_IFF_ANYCAST,	0,	setia6flags },
    256 	{ "tentative",	IN6_IFF_TENTATIVE,	0,	setia6flags },
    257 	{ "-tentative",	-IN6_IFF_TENTATIVE,	0,	setia6flags },
    258 	{ "deprecated",	IN6_IFF_DEPRECATED,	0,	setia6flags },
    259 	{ "-deprecated", -IN6_IFF_DEPRECATED,	0,	setia6flags },
    260 	{ "pltime",	NEXTARG,	0,		setia6pltime },
    261 	{ "vltime",	NEXTARG,	0,		setia6vltime },
    262 #endif /*INET6*/
    263 #ifndef INET_ONLY
    264 	{ "range",	NEXTARG,	0,		setatrange },
    265 	{ "phase",	NEXTARG,	0,		setatphase },
    266 	{ "snpaoffset",	NEXTARG,	0,		setsnpaoffset },
    267 	{ "nsellength",	NEXTARG,	0,		setnsellength },
    268 #endif	/* INET_ONLY */
    269 	{ "tunnel",	NEXTARG2,	0,		NULL,
    270 							settunnel } ,
    271 	{ "deletetunnel", 0,		0,		deletetunnel },
    272 	{ "vlan",	NEXTARG,	0,		setvlan } ,
    273 	{ "vlanif",	NEXTARG,	0,		setvlanif } ,
    274 	{ "-vlanif",	0,		0,		unsetvlanif } ,
    275 	{ "deletetunnel", 0,		0,		deletetunnel },
    276 #if 0
    277 	/* XXX `create' special-cased below */
    278 	{ "create",	0,		0,		clone_create } ,
    279 #endif
    280 	{ "destroy",	0,		0,		clone_destroy } ,
    281 	{ "link0",	IFF_LINK0,	0,		setifflags } ,
    282 	{ "-link0",	-IFF_LINK0,	0,		setifflags } ,
    283 	{ "link1",	IFF_LINK1,	0,		setifflags } ,
    284 	{ "-link1",	-IFF_LINK1,	0,		setifflags } ,
    285 	{ "link2",	IFF_LINK2,	0,		setifflags } ,
    286 	{ "-link2",	-IFF_LINK2,	0,		setifflags } ,
    287 	{ "media",	NEXTARG,	A_MEDIA,	setmedia },
    288 	{ "mediaopt",	NEXTARG,	A_MEDIAOPTSET,	setmediaopt },
    289 	{ "-mediaopt",	NEXTARG,	A_MEDIAOPTCLR,	unsetmediaopt },
    290 	{ "instance",	NEXTARG,	A_MEDIAINST,	setmediainst },
    291 	{ "inst",	NEXTARG,	A_MEDIAINST,	setmediainst },
    292 	{ "ip4csum",	IFCAP_CSUM_IPv4,0,		setifcaps },
    293 	{ "-ip4csum",	-IFCAP_CSUM_IPv4,0,		setifcaps },
    294 	{ "tcp4csum",	IFCAP_CSUM_TCPv4,0,		setifcaps },
    295 	{ "-tcp4csum",	-IFCAP_CSUM_TCPv4,0,		setifcaps },
    296 	{ "udp4csum",	IFCAP_CSUM_UDPv4,0,		setifcaps },
    297 	{ "-udp4csum",	-IFCAP_CSUM_UDPv4,0,		setifcaps },
    298 	{ "tcp6csum",	IFCAP_CSUM_TCPv6,0,		setifcaps },
    299 	{ "-tcp6csum",	-IFCAP_CSUM_TCPv6,0,		setifcaps },
    300 	{ "udp6csum",	IFCAP_CSUM_UDPv6,0,		setifcaps },
    301 	{ "-udp6csum",	-IFCAP_CSUM_UDPv6,0,		setifcaps },
    302 	{ 0,		0,		0,		setifaddr },
    303 	{ 0,		0,		0,		setifdstaddr },
    304 };
    305 
    306 void 	adjust_nsellength __P((void));
    307 int	getinfo __P((struct ifreq *));
    308 int	carrier __P((void));
    309 void	getsock __P((int));
    310 void	printall __P((const char *));
    311 void	list_cloners __P((void));
    312 void 	printb __P((const char *, unsigned short, const char *));
    313 int	prefix __P((void *, int));
    314 void 	status __P((const u_int8_t *, int));
    315 void 	usage __P((void));
    316 const char *get_string __P((const char *, const char *, u_int8_t *, int *));
    317 void	print_string __P((const u_int8_t *, int));
    318 char	*sec2str __P((time_t));
    319 
    320 const char *get_media_type_string __P((int));
    321 const char *get_media_subtype_string __P((int));
    322 int	get_media_subtype __P((int, const char *));
    323 int	get_media_options __P((int, const char *));
    324 int	lookup_media_word __P((struct ifmedia_description *, int,
    325 	    const char *));
    326 void	print_media_word __P((int, int, int));
    327 void	process_media_commands __P((void));
    328 void	init_current_media __P((void));
    329 
    330 /*
    331  * XNS support liberally adapted from code written at the University of
    332  * Maryland principally by James O'Toole and Chris Torek.
    333  */
    334 void	in_alias __P((struct ifreq *));
    335 void	in_status __P((int));
    336 void 	in_getaddr __P((const char *, int));
    337 void 	in_getprefix __P((const char *, int));
    338 #ifdef INET6
    339 void	in6_fillscopeid __P((struct sockaddr_in6 *sin6));
    340 void	in6_alias __P((struct in6_ifreq *));
    341 void	in6_status __P((int));
    342 void 	in6_getaddr __P((const char *, int));
    343 void 	in6_getprefix __P((const char *, int));
    344 #endif
    345 void	at_status __P((int));
    346 void	at_getaddr __P((const char *, int));
    347 void 	xns_status __P((int));
    348 void 	xns_getaddr __P((const char *, int));
    349 void 	iso_status __P((int));
    350 void 	iso_getaddr __P((const char *, int));
    351 
    352 void	ieee80211_status __P((void));
    353 void	tunnel_status __P((void));
    354 void	vlan_status __P((void));
    355 
    356 /* Known address families */
    357 struct afswtch {
    358 	const char *af_name;
    359 	short af_af;
    360 	void (*af_status) __P((int));
    361 	void (*af_getaddr) __P((const char *, int));
    362 	void (*af_getprefix) __P((const char *, int));
    363 	u_long af_difaddr;
    364 	u_long af_aifaddr;
    365 	u_long af_gifaddr;
    366 	caddr_t af_ridreq;
    367 	caddr_t af_addreq;
    368 } afs[] = {
    369 #define C(x) ((caddr_t) &x)
    370 	{ "inet", AF_INET, in_status, in_getaddr, in_getprefix,
    371 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(ridreq), C(addreq) },
    372 #ifdef INET6
    373 	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
    374 	     SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
    375 	     /*
    376 	      * Deleting the first address before setting new one is
    377 	      * not prefered way in this protocol.
    378 	      */
    379 	     0,
    380 	     C(in6_ridreq), C(in6_addreq) },
    381 #endif
    382 #ifndef INET_ONLY	/* small version, for boot media */
    383 	{ "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
    384 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(addreq), C(addreq) },
    385 	{ "ns", AF_NS, xns_status, xns_getaddr, NULL,
    386 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(ridreq), C(addreq) },
    387 	{ "iso", AF_ISO, iso_status, iso_getaddr, NULL,
    388 	     SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, SIOCGIFADDR_ISO,
    389 	     C(iso_ridreq), C(iso_addreq) },
    390 #endif	/* INET_ONLY */
    391 	{ 0,	0,	    0,		0 }
    392 };
    393 
    394 struct afswtch *afp;	/*the address family being set or asked about*/
    395 
    396 struct afswtch *lookup_af __P((const char *));
    397 
    398 int
    399 main(argc, argv)
    400 	int argc;
    401 	char *argv[];
    402 {
    403 	struct ifreq ifreq;
    404 	int ch;
    405 
    406 	/* Parse command-line options */
    407 	aflag = mflag = 0;
    408 	while ((ch = getopt(argc, argv, "AabCdlmsu"
    409 #ifdef INET6
    410 					"L"
    411 #endif
    412 			)) != -1) {
    413 		switch (ch) {
    414 		case 'A':
    415 			warnx("-A is deprecated");
    416 			break;
    417 
    418 		case 'a':
    419 			aflag = 1;
    420 			break;
    421 
    422 		case 'b':
    423 			bflag = 1;
    424 			break;
    425 
    426 
    427 		case 'C':
    428 			Cflag = 1;
    429 			break;
    430 
    431 		case 'd':
    432 			dflag = 1;
    433 			break;
    434 
    435 #ifdef INET6
    436 		case 'L':
    437 			Lflag = 1;
    438 			break;
    439 #endif
    440 
    441 		case 'l':
    442 			lflag = 1;
    443 			break;
    444 
    445 		case 'm':
    446 			mflag = 1;
    447 			break;
    448 
    449 		case 's':
    450 			sflag = 1;
    451 			break;
    452 
    453 		case 'u':
    454 			uflag = 1;
    455 			break;
    456 
    457 
    458 		default:
    459 			usage();
    460 			/* NOTREACHED */
    461 		}
    462 	}
    463 	argc -= optind;
    464 	argv += optind;
    465 
    466 	/*
    467 	 * -l means "list all interfaces", and is mutally exclusive with
    468 	 * all other flags/commands.
    469 	 *
    470 	 * -C means "list all names of cloners", and it mutually exclusive
    471 	 * with all other flags/commands.
    472 	 *
    473 	 * -a means "print status of all interfaces".
    474 	 */
    475 	if ((lflag || Cflag) && (aflag || mflag || argc))
    476 		usage();
    477 #ifdef INET6
    478 	if ((lflag || Cflag) && Lflag)
    479 		usage();
    480 #endif
    481 	if (lflag && Cflag)
    482 		usage();
    483 	if (Cflag) {
    484 		if (argc)
    485 			usage();
    486 		list_cloners();
    487 		exit(0);
    488 	}
    489 	if (aflag || lflag) {
    490 		if (argc > 1)
    491 			usage();
    492 		else if (argc == 1) {
    493 			afp = lookup_af(argv[0]);
    494 			if (afp == NULL)
    495 				usage();
    496 		}
    497 		if (afp)
    498 			af = ifr.ifr_addr.sa_family = afp->af_af;
    499 		else
    500 			af = ifr.ifr_addr.sa_family = afs[0].af_af;
    501 		printall(NULL);
    502 		exit(0);
    503 	}
    504 
    505 	/* Make sure there's an interface name. */
    506 	if (argc < 1)
    507 		usage();
    508 	(void) strncpy(name, argv[0], sizeof(name));
    509 	argc--; argv++;
    510 
    511 	/*
    512 	 * NOTE:  We must special-case the `create' command right
    513 	 * here as we would otherwise fail in getinfo().
    514 	 */
    515 	if (argc > 0 && strcmp(argv[0], "create") == 0) {
    516 		clone_create(argv[0], 0);
    517 		argc--, argv++;
    518 		if (argc == 0)
    519 			exit(0);
    520 	}
    521 
    522 	/* Check for address family. */
    523 	afp = NULL;
    524 	if (argc > 0) {
    525 		afp = lookup_af(argv[0]);
    526 		if (afp != NULL) {
    527 			argv++;
    528 			argc--;
    529 		}
    530 	}
    531 
    532 	/* Initialize af, just for use in getinfo(). */
    533 	if (afp == NULL)
    534 		af = afs->af_af;
    535 	else
    536 		af = afp->af_af;
    537 
    538 	/* Get information about the interface. */
    539 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    540 	if (getinfo(&ifr) < 0)
    541 		exit(1);
    542 
    543 	if (sflag) {
    544 		if (argc != 0)
    545 			usage();
    546 		else
    547 			exit(carrier());
    548 	}
    549 
    550 	/* No more arguments means interface status. */
    551 	if (argc == 0) {
    552 		printall(name);
    553 		exit(0);
    554 	}
    555 
    556 	/* The following operations assume inet family as the default. */
    557 	if (afp == NULL)
    558 		afp = afs;
    559 	af = ifr.ifr_addr.sa_family = afp->af_af;
    560 
    561 #ifdef INET6
    562 	/* initialization */
    563 	in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    564 	in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    565 #endif
    566 
    567 	/* Process commands. */
    568 	while (argc > 0) {
    569 		const struct cmd *p;
    570 
    571 		for (p = cmds; p->c_name; p++)
    572 			if (strcmp(argv[0], p->c_name) == 0)
    573 				break;
    574 		if (p->c_name == 0 && setaddr) {
    575 			if ((flags & IFF_POINTOPOINT) == 0) {
    576 				errx(1, "can't set destination address %s",
    577 				     "on non-point-to-point link");
    578 			}
    579 			p++;	/* got src, do dst */
    580 		}
    581 		if (p->c_func != NULL || p->c_func2 != NULL) {
    582 			if (p->c_parameter == NEXTARG) {
    583 				if (argc < 2)
    584 					errx(1, "'%s' requires argument",
    585 					    p->c_name);
    586 				(*p->c_func)(argv[1], 0);
    587 				argc--, argv++;
    588 			} else if (p->c_parameter == NEXTARG2) {
    589 				if (argc < 3)
    590 					errx(1, "'%s' requires 2 arguments",
    591 					    p->c_name);
    592 				(*p->c_func2)(argv[1], argv[2]);
    593 				argc -= 2, argv += 2;
    594 			} else
    595 				(*p->c_func)(argv[0], p->c_parameter);
    596 			actions |= p->c_action;
    597 		}
    598 		argc--, argv++;
    599 	}
    600 
    601 	/* Process any media commands that may have been issued. */
    602 	process_media_commands();
    603 
    604 	if (af == AF_INET6 && explicit_prefix == 0) {
    605 		/*
    606 		 * Aggregatable address architecture defines all prefixes
    607 		 * are 64. So, it is convenient to set prefixlen to 64 if
    608 		 * it is not specified.
    609 		 */
    610 		setifprefixlen("64", 0);
    611 		/* in6_getprefix("64", MASK) if MASK is available here... */
    612 	}
    613 
    614 #ifndef INET_ONLY
    615 	if (af == AF_ISO)
    616 		adjust_nsellength();
    617 
    618 	if (af == AF_APPLETALK)
    619 		checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
    620 
    621 	if (setipdst && af==AF_NS) {
    622 		struct nsip_req rq;
    623 		int size = sizeof(rq);
    624 
    625 		rq.rq_ns = addreq.ifra_addr;
    626 		rq.rq_ip = addreq.ifra_dstaddr;
    627 
    628 		if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
    629 			warn("encapsulation routing");
    630 	}
    631 
    632 #endif	/* INET_ONLY */
    633 
    634 	if (clearaddr) {
    635 		(void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
    636 		if (ioctl(s, afp->af_difaddr, afp->af_ridreq) < 0)
    637 			err(1, "SIOCDIFADDR");
    638 	}
    639 	if (newaddr > 0) {
    640 		(void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
    641 		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
    642 			warn("SIOCAIFADDR");
    643 	}
    644 
    645 	if (g_ifcr_updated) {
    646 		(void) strncpy(g_ifcr.ifcr_name, name,
    647 		    sizeof(g_ifcr.ifcr_name));
    648 		if (ioctl(s, SIOCSIFCAP, (caddr_t) &g_ifcr) < 0)
    649 			err(1, "SIOCSIFCAP");
    650 	}
    651 
    652 	if (reset_if_flags) {
    653 		(void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
    654 		ifreq.ifr_flags = flags;
    655 		if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) < 0)
    656 			err(1, "SIOCSIFFLAGS");
    657 	}
    658 	exit(0);
    659 }
    660 
    661 struct afswtch *
    662 lookup_af(cp)
    663 	const char *cp;
    664 {
    665 	struct afswtch *a;
    666 
    667 	for (a = afs; a->af_name != NULL; a++)
    668 		if (strcmp(a->af_name, cp) == 0)
    669 			return (a);
    670 	return (NULL);
    671 }
    672 
    673 void
    674 getsock(naf)
    675 	int naf;
    676 {
    677 	static int oaf = -1;
    678 
    679 	if (oaf == naf)
    680 		return;
    681 	if (oaf != -1)
    682 		close(s);
    683 	s = socket(naf, SOCK_DGRAM, 0);
    684 	if (s < 0)
    685 		oaf = -1;
    686 	else
    687 		oaf = naf;
    688 }
    689 
    690 int
    691 getinfo(ifr)
    692 	struct ifreq *ifr;
    693 {
    694 
    695 	getsock(af);
    696 	if (s < 0)
    697 		err(1, "socket");
    698 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
    699 		warn("SIOCGIFFLAGS %s", ifr->ifr_name);
    700 		return (-1);
    701 	}
    702 	flags = ifr->ifr_flags;
    703 	if (ioctl(s, SIOCGIFMETRIC, (caddr_t)ifr) < 0) {
    704 		warn("SIOCGIFMETRIC %s", ifr->ifr_name);
    705 		metric = 0;
    706 	} else
    707 		metric = ifr->ifr_metric;
    708 	if (ioctl(s, SIOCGIFMTU, (caddr_t)ifr) < 0)
    709 		mtu = 0;
    710 	else
    711 		mtu = ifr->ifr_mtu;
    712 
    713 	memset(&g_ifcr, 0, sizeof(g_ifcr));
    714 	strcpy(g_ifcr.ifcr_name, ifr->ifr_name);
    715 	(void) ioctl(s, SIOCGIFCAP, (caddr_t) &g_ifcr);
    716 
    717 	return (0);
    718 }
    719 
    720 void
    721 printall(ifname)
    722 	const char *ifname;
    723 {
    724 	struct ifaddrs *ifap, *ifa;
    725 	struct ifreq ifr;
    726 	const struct sockaddr_dl *sdl = NULL;
    727 	int idx;
    728 	char *p;
    729 
    730 	if (getifaddrs(&ifap) != 0)
    731 		err(1, "getifaddrs");
    732 	p = NULL;
    733 	idx = 0;
    734 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    735 		memset(&ifr, 0, sizeof(ifr));
    736 		strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    737 		if (sizeof(ifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
    738 			memcpy(&ifr.ifr_addr, ifa->ifa_addr,
    739 			    ifa->ifa_addr->sa_len);
    740 		}
    741 
    742 		if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
    743 			continue;
    744 		if (ifa->ifa_addr->sa_family == AF_LINK)
    745 			sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
    746 		if (p && strcmp(p, ifa->ifa_name) == 0)
    747 			continue;
    748 		(void) strncpy(name, ifa->ifa_name, sizeof(name));
    749 		name[sizeof(name) - 1] = '\0';
    750 		p = ifa->ifa_name;
    751 
    752 		if (getinfo(&ifr) < 0)
    753 			continue;
    754 		if (bflag && (ifa->ifa_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))
    755 			continue;
    756 		if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
    757 			continue;
    758 		if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
    759 			continue;
    760 
    761 		if (sflag && carrier())
    762 			continue;
    763 		idx++;
    764 		/*
    765 		 * Are we just listing the interfaces?
    766 		 */
    767 		if (lflag) {
    768 			if (idx > 1)
    769 				putchar(' ');
    770 			fputs(name, stdout);
    771 			continue;
    772 		}
    773 
    774 		if (sdl == NULL) {
    775 			status(NULL, 0);
    776 		} else {
    777 			status(LLADDR(sdl), sdl->sdl_alen);
    778 			sdl = NULL;
    779 		}
    780 	}
    781 	if (lflag)
    782 		putchar('\n');
    783 	freeifaddrs(ifap);
    784 }
    785 
    786 void
    787 list_cloners(void)
    788 {
    789 	struct if_clonereq ifcr;
    790 	char *cp, *buf;
    791 	int idx;
    792 
    793 	memset(&ifcr, 0, sizeof(ifcr));
    794 
    795 	getsock(AF_INET);
    796 
    797 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
    798 		err(1, "SIOCIFGCLONERS for count");
    799 
    800 	buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
    801 	if (buf == NULL)
    802 		err(1, "unable to allocate cloner name buffer");
    803 
    804 	ifcr.ifcr_count = ifcr.ifcr_total;
    805 	ifcr.ifcr_buffer = buf;
    806 
    807 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
    808 		err(1, "SIOCIFGCLONERS for names");
    809 
    810 	/*
    811 	 * In case some disappeared in the mean time, clamp it down.
    812 	 */
    813 	if (ifcr.ifcr_count > ifcr.ifcr_total)
    814 		ifcr.ifcr_count = ifcr.ifcr_total;
    815 
    816 	for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
    817 		if (idx > 0)
    818 			putchar(' ');
    819 		printf("%s", cp);
    820 	}
    821 
    822 	putchar('\n');
    823 	free(buf);
    824 	return;
    825 }
    826 
    827 /*ARGSUSED*/
    828 void
    829 clone_create(addr, param)
    830 	const char *addr;
    831 	int param;
    832 {
    833 
    834 	/* We're called early... */
    835 	getsock(AF_INET);
    836 
    837 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    838 	if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
    839 		err(1, "SIOCIFCREATE");
    840 }
    841 
    842 /*ARGSUSED*/
    843 void
    844 clone_destroy(addr, param)
    845 	const char *addr;
    846 	int param;
    847 {
    848 
    849 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    850 	if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
    851 		err(1, "SIOCIFDESTROY");
    852 }
    853 
    854 #define RIDADDR 0
    855 #define ADDR	1
    856 #define MASK	2
    857 #define DSTADDR	3
    858 
    859 /*ARGSUSED*/
    860 void
    861 setifaddr(addr, param)
    862 	const char *addr;
    863 	int param;
    864 {
    865 	struct ifreq *ifr;		/* XXX */
    866 
    867 	/*
    868 	 * Delay the ioctl to set the interface addr until flags are all set.
    869 	 * The address interpretation may depend on the flags,
    870 	 * and the flags may change when the address is set.
    871 	 */
    872 	setaddr++;
    873 	if (newaddr == -1)
    874 		newaddr = 1;
    875 	if (doalias == 0 && afp->af_gifaddr != 0) {
    876 		ifr = (struct ifreq *)afp->af_ridreq;
    877 		(void) strncpy(ifr->ifr_name, name, sizeof(ifr->ifr_name));
    878 		ifr->ifr_addr.sa_family = afp->af_af;
    879 		if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
    880 			clearaddr = 1;
    881 		else if (errno == EADDRNOTAVAIL)
    882 			/* No address was assigned yet. */
    883 			;
    884 		else
    885 			err(1, "SIOCGIFADDR");
    886 	}
    887 
    888 	(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
    889 }
    890 
    891 void
    892 settunnel(src, dst)
    893 	const char *src, *dst;
    894 {
    895 	struct addrinfo hints, *srcres, *dstres;
    896 	int ecode;
    897 	struct if_laddrreq req;
    898 
    899 	memset(&hints, 0, sizeof(hints));
    900 	hints.ai_family = afp->af_af;
    901 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
    902 
    903 	if ((ecode = getaddrinfo(src, NULL, &hints, &srcres)) != 0)
    904 		errx(1, "error in parsing address string: %s",
    905 		    gai_strerror(ecode));
    906 
    907 	if ((ecode = getaddrinfo(dst, NULL, &hints, &dstres)) != 0)
    908 		errx(1, "error in parsing address string: %s",
    909 		    gai_strerror(ecode));
    910 
    911 	if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
    912 		errx(1,
    913 		    "source and destination address families do not match");
    914 
    915 	if (srcres->ai_addrlen > sizeof(req.addr) ||
    916 	    dstres->ai_addrlen > sizeof(req.dstaddr))
    917 		errx(1, "invalid sockaddr");
    918 
    919 	memset(&req, 0, sizeof(req));
    920 	strncpy(req.iflr_name, name, sizeof(req.iflr_name));
    921 	memcpy(&req.addr, srcres->ai_addr, srcres->ai_addrlen);
    922 	memcpy(&req.dstaddr, dstres->ai_addr, dstres->ai_addrlen);
    923 	if (ioctl(s, SIOCSLIFPHYADDR, &req) < 0)
    924 		warn("SIOCSLIFPHYADDR");
    925 
    926 	freeaddrinfo(srcres);
    927 	freeaddrinfo(dstres);
    928 }
    929 
    930 /* ARGSUSED */
    931 void
    932 deletetunnel(vname, param)
    933 	const char *vname;
    934 	int param;
    935 {
    936 
    937 	if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
    938 		err(1, "SIOCDIFPHYADDR");
    939 }
    940 
    941 void setvlan(val, d)
    942 	const char *val;
    943 	int d;
    944 {
    945 	struct vlanreq vlr;
    946 
    947 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
    948 	    !isdigit(ifr.ifr_name[4]))
    949 		errx(EXIT_FAILURE,
    950 		    "``vlan'' valid only with vlan(4) interfaces");
    951 
    952 	vlan_tag = atoi(val);
    953 
    954 	memset(&vlr, 0, sizeof(vlr));
    955 	ifr.ifr_data = (caddr_t)&vlr;
    956 
    957 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
    958 		err(EXIT_FAILURE, "SIOCGETVLAN");
    959 
    960 	vlr.vlr_tag = vlan_tag;
    961 
    962 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
    963 		err(EXIT_FAILURE, "SIOCSETVLAN");
    964 }
    965 
    966 void setvlanif(val, d)
    967 	const char *val;
    968 	int d;
    969 {
    970 	struct vlanreq vlr;
    971 
    972 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
    973 	    !isdigit(ifr.ifr_name[4]))
    974 		errx(EXIT_FAILURE,
    975 		    "``vlanif'' valid only with vlan(4) interfaces");
    976 
    977 	if (vlan_tag == (u_int)-1)
    978 		errx(EXIT_FAILURE,
    979 		    "must specify both ``vlan'' and ``vlanif''");
    980 
    981 	memset(&vlr, 0, sizeof(vlr));
    982 	ifr.ifr_data = (caddr_t)&vlr;
    983 
    984 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
    985 		err(EXIT_FAILURE, "SIOCGETVLAN");
    986 
    987 	strlcpy(vlr.vlr_parent, val, sizeof(vlr.vlr_parent));
    988 	vlr.vlr_tag = vlan_tag;
    989 
    990 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
    991 		err(EXIT_FAILURE, "SIOCSETVLAN");
    992 }
    993 
    994 void unsetvlanif(val, d)
    995 	const char *val;
    996 	int d;
    997 {
    998 	struct vlanreq vlr;
    999 
   1000 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
   1001 	    !isdigit(ifr.ifr_name[4]))
   1002 		errx(EXIT_FAILURE,
   1003 		    "``vlanif'' valid only with vlan(4) interfaces");
   1004 
   1005 	memset(&vlr, 0, sizeof(vlr));
   1006 	ifr.ifr_data = (caddr_t)&vlr;
   1007 
   1008 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
   1009 		err(EXIT_FAILURE, "SIOCGETVLAN");
   1010 
   1011 	vlr.vlr_parent[0] = '\0';
   1012 	vlr.vlr_tag = 0;
   1013 
   1014 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
   1015 		err(EXIT_FAILURE, "SIOCSETVLAN");
   1016 }
   1017 
   1018 void
   1019 setifnetmask(addr, d)
   1020 	const char *addr;
   1021 	int d;
   1022 {
   1023 	(*afp->af_getaddr)(addr, MASK);
   1024 }
   1025 
   1026 void
   1027 setifbroadaddr(addr, d)
   1028 	const char *addr;
   1029 	int d;
   1030 {
   1031 	(*afp->af_getaddr)(addr, DSTADDR);
   1032 }
   1033 
   1034 void
   1035 setifipdst(addr, d)
   1036 	const char *addr;
   1037 	int d;
   1038 {
   1039 	in_getaddr(addr, DSTADDR);
   1040 	setipdst++;
   1041 	clearaddr = 0;
   1042 	newaddr = 0;
   1043 }
   1044 
   1045 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
   1046 /*ARGSUSED*/
   1047 void
   1048 notealias(addr, param)
   1049 	const char *addr;
   1050 	int param;
   1051 {
   1052 	if (setaddr && doalias == 0 && param < 0)
   1053 		(void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
   1054 		    rqtosa(af_addreq)->sa_len);
   1055 	doalias = param;
   1056 	if (param < 0) {
   1057 		clearaddr = 1;
   1058 		newaddr = 0;
   1059 	} else
   1060 		clearaddr = 0;
   1061 }
   1062 
   1063 /*ARGSUSED*/
   1064 void
   1065 notrailers(vname, value)
   1066 	const char *vname;
   1067 	int value;
   1068 {
   1069 	puts("Note: trailers are no longer sent, but always received");
   1070 }
   1071 
   1072 /*ARGSUSED*/
   1073 void
   1074 setifdstaddr(addr, param)
   1075 	const char *addr;
   1076 	int param;
   1077 {
   1078 	(*afp->af_getaddr)(addr, DSTADDR);
   1079 }
   1080 
   1081 void
   1082 setifflags(vname, value)
   1083 	const char *vname;
   1084 	int value;
   1085 {
   1086 	struct ifreq ifreq;
   1087 
   1088 	(void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
   1089  	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifreq) < 0)
   1090 		err(1, "SIOCGIFFLAGS");
   1091  	flags = ifreq.ifr_flags;
   1092 
   1093 	if (value < 0) {
   1094 		value = -value;
   1095 		flags &= ~value;
   1096 	} else
   1097 		flags |= value;
   1098 	ifreq.ifr_flags = flags;
   1099 	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) < 0)
   1100 		err(1, "SIOCSIFFLAGS");
   1101 
   1102 	reset_if_flags = 1;
   1103 }
   1104 
   1105 void
   1106 setifcaps(vname, value)
   1107 	const char *vname;
   1108 	int value;
   1109 {
   1110 
   1111 	if (value < 0) {
   1112 		value = -value;
   1113 		g_ifcr.ifcr_capenable &= ~value;
   1114 	} else
   1115 		g_ifcr.ifcr_capenable |= value;
   1116 
   1117 	g_ifcr_updated = 1;
   1118 }
   1119 
   1120 #ifdef INET6
   1121 void
   1122 setia6flags(vname, value)
   1123 	const char *vname;
   1124 	int value;
   1125 {
   1126 	if (value < 0) {
   1127 		value = -value;
   1128 		in6_addreq.ifra_flags &= ~value;
   1129 	} else
   1130 		in6_addreq.ifra_flags |= value;
   1131 }
   1132 
   1133 void
   1134 setia6pltime(val, d)
   1135 	const char *val;
   1136 	int d;
   1137 {
   1138 	setia6lifetime("pltime", val);
   1139 }
   1140 
   1141 void
   1142 setia6vltime(val, d)
   1143 	const char *val;
   1144 	int d;
   1145 {
   1146 	setia6lifetime("vltime", val);
   1147 }
   1148 
   1149 void
   1150 setia6lifetime(cmd, val)
   1151 	const char *cmd;
   1152 	const char *val;
   1153 {
   1154 	time_t newval, t;
   1155 	char *ep;
   1156 
   1157 	t = time(NULL);
   1158 	newval = (time_t)strtoul(val, &ep, 0);
   1159 	if (val == ep)
   1160 		errx(1, "invalid %s", cmd);
   1161 	if (afp->af_af != AF_INET6)
   1162 		errx(1, "%s not allowed for the AF", cmd);
   1163 	if (strcmp(cmd, "vltime") == 0) {
   1164 		in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
   1165 		in6_addreq.ifra_lifetime.ia6t_vltime = newval;
   1166 	} else if (strcmp(cmd, "pltime") == 0) {
   1167 		in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
   1168 		in6_addreq.ifra_lifetime.ia6t_pltime = newval;
   1169 	}
   1170 }
   1171 #endif
   1172 
   1173 void
   1174 setifmetric(val, d)
   1175 	const char *val;
   1176 	int d;
   1177 {
   1178 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1179 	ifr.ifr_metric = atoi(val);
   1180 	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
   1181 		warn("SIOCSIFMETRIC");
   1182 }
   1183 
   1184 void
   1185 setifmtu(val, d)
   1186 	const char *val;
   1187 	int d;
   1188 {
   1189 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1190 	ifr.ifr_mtu = atoi(val);
   1191 	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
   1192 		warn("SIOCSIFMTU");
   1193 }
   1194 
   1195 const char *
   1196 get_string(val, sep, buf, lenp)
   1197 	const char *val, *sep;
   1198 	u_int8_t *buf;
   1199 	int *lenp;
   1200 {
   1201 	int len;
   1202 	int hexstr;
   1203 	u_int8_t *p;
   1204 
   1205 	len = *lenp;
   1206 	p = buf;
   1207 	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
   1208 	if (hexstr)
   1209 		val += 2;
   1210 	for (;;) {
   1211 		if (*val == '\0')
   1212 			break;
   1213 		if (sep != NULL && strchr(sep, *val) != NULL) {
   1214 			val++;
   1215 			break;
   1216 		}
   1217 		if (hexstr) {
   1218 			if (!isxdigit((u_char)val[0]) ||
   1219 			    !isxdigit((u_char)val[1])) {
   1220 				warnx("bad hexadecimal digits");
   1221 				return NULL;
   1222 			}
   1223 		}
   1224 		if (p > buf + len) {
   1225 			if (hexstr)
   1226 				warnx("hexadecimal digits too long");
   1227 			else
   1228 				warnx("strings too long");
   1229 			return NULL;
   1230 		}
   1231 		if (hexstr) {
   1232 #define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
   1233 			*p++ = (tohex((u_char)val[0]) << 4) |
   1234 			    tohex((u_char)val[1]);
   1235 #undef tohex
   1236 			val += 2;
   1237 		} else
   1238 			*p++ = *val++;
   1239 	}
   1240 	len = p - buf;
   1241 	if (len < *lenp)
   1242 		memset(p, 0, *lenp - len);
   1243 	*lenp = len;
   1244 	return val;
   1245 }
   1246 
   1247 void
   1248 print_string(buf, len)
   1249 	const u_int8_t *buf;
   1250 	int len;
   1251 {
   1252 	int i;
   1253 	int hasspc;
   1254 
   1255 	i = 0;
   1256 	hasspc = 0;
   1257 	if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
   1258 		for (; i < len; i++) {
   1259 			if (!isprint(buf[i]))
   1260 				break;
   1261 			if (isspace(buf[i]))
   1262 				hasspc++;
   1263 		}
   1264 	}
   1265 	if (i == len) {
   1266 		if (hasspc || len == 0)
   1267 			printf("\"%.*s\"", len, buf);
   1268 		else
   1269 			printf("%.*s", len, buf);
   1270 	} else {
   1271 		printf("0x");
   1272 		for (i = 0; i < len; i++)
   1273 			printf("%02x", buf[i]);
   1274 	}
   1275 }
   1276 
   1277 void
   1278 setifnwid(val, d)
   1279 	const char *val;
   1280 	int d;
   1281 {
   1282 	struct ieee80211_nwid nwid;
   1283 	int len;
   1284 
   1285 	len = sizeof(nwid.i_nwid);
   1286 	if (get_string(val, NULL, nwid.i_nwid, &len) == NULL)
   1287 		return;
   1288 	nwid.i_len = len;
   1289 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1290 	ifr.ifr_data = (caddr_t)&nwid;
   1291 	if (ioctl(s, SIOCS80211NWID, (caddr_t)&ifr) < 0)
   1292 		warn("SIOCS80211NWID");
   1293 }
   1294 
   1295 void
   1296 setifnwkey(val, d)
   1297 	const char *val;
   1298 	int d;
   1299 {
   1300 	struct ieee80211_nwkey nwkey;
   1301 	int i;
   1302 	u_int8_t keybuf[IEEE80211_WEP_NKID][16];
   1303 
   1304 	nwkey.i_wepon = IEEE80211_NWKEY_WEP;
   1305 	nwkey.i_defkid = 1;
   1306 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1307 		nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
   1308 		nwkey.i_key[i].i_keydat = keybuf[i];
   1309 	}
   1310 	if (d != 0) {
   1311 		/* disable WEP encryption */
   1312 		nwkey.i_wepon = 0;
   1313 		i = 0;
   1314 	} else if (strcasecmp("persist", val) == 0) {
   1315 		/* use all values from persistent memory */
   1316 		nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
   1317 		nwkey.i_defkid = 0;
   1318 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
   1319 			nwkey.i_key[i].i_keylen = -1;
   1320 	} else if (strncasecmp("persist:", val, 8) == 0) {
   1321 		val += 8;
   1322 		/* program keys in persistent memory */
   1323 		nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
   1324 		goto set_nwkey;
   1325 	} else {
   1326   set_nwkey:
   1327 		if (isdigit(val[0]) && val[1] == ':') {
   1328 			/* specifying a full set of four keys */
   1329 			nwkey.i_defkid = val[0] - '0';
   1330 			val += 2;
   1331 			for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1332 				val = get_string(val, ",", keybuf[i],
   1333 				    &nwkey.i_key[i].i_keylen);
   1334 				if (val == NULL)
   1335 					return;
   1336 			}
   1337 			if (*val != '\0') {
   1338 				warnx("SIOCS80211NWKEY: too many keys.");
   1339 				return;
   1340 			}
   1341 		} else {
   1342 			val = get_string(val, NULL, keybuf[0],
   1343 			    &nwkey.i_key[0].i_keylen);
   1344 			if (val == NULL)
   1345 				return;
   1346 			i = 1;
   1347 		}
   1348 	}
   1349 	for (; i < IEEE80211_WEP_NKID; i++)
   1350 		nwkey.i_key[i].i_keylen = 0;
   1351 	(void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
   1352 	if (ioctl(s, SIOCS80211NWKEY, (caddr_t)&nwkey) < 0)
   1353 		warn("SIOCS80211NWKEY");
   1354 }
   1355 
   1356 void
   1357 setifpowersave(val, d)
   1358 	const char *val;
   1359 	int d;
   1360 {
   1361 	struct ieee80211_power power;
   1362 
   1363 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
   1364 	if (ioctl(s, SIOCG80211POWER, (caddr_t)&power) < 0) {
   1365 		warn("SIOCG80211POWER");
   1366 		return;
   1367 	}
   1368 
   1369 	power.i_enabled = d;
   1370 	if (ioctl(s, SIOCS80211POWER, (caddr_t)&power) < 0)
   1371 		warn("SIOCS80211POWER");
   1372 }
   1373 
   1374 void
   1375 setifpowersavesleep(val, d)
   1376 	const char *val;
   1377 	int d;
   1378 {
   1379 	struct ieee80211_power power;
   1380 
   1381 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
   1382 	if (ioctl(s, SIOCG80211POWER, (caddr_t)&power) < 0) {
   1383 		warn("SIOCG80211POWER");
   1384 		return;
   1385 	}
   1386 
   1387 	power.i_maxsleep = atoi(val);
   1388 	if (ioctl(s, SIOCS80211POWER, (caddr_t)&power) < 0)
   1389 		warn("SIOCS80211POWER");
   1390 }
   1391 
   1392 void
   1393 ieee80211_status()
   1394 {
   1395 	int i, nwkey_verbose;
   1396 	struct ieee80211_nwid nwid;
   1397 	struct ieee80211_nwkey nwkey;
   1398 	struct ieee80211_power power;
   1399 	u_int8_t keybuf[IEEE80211_WEP_NKID][16];
   1400 
   1401 	memset(&ifr, 0, sizeof(ifr));
   1402 	ifr.ifr_data = (caddr_t)&nwid;
   1403 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1404 	if (ioctl(s, SIOCG80211NWID, (caddr_t)&ifr) != 0)
   1405 		return;
   1406 	if (nwid.i_len > IEEE80211_NWID_LEN) {
   1407 		warnx("SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
   1408 		return;
   1409 	}
   1410 	printf("\tnwid ");
   1411 	print_string(nwid.i_nwid, nwid.i_len);
   1412 	memset(&nwkey, 0, sizeof(nwkey));
   1413 	(void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
   1414 	/* show nwkey only when WEP is enabled */
   1415 	if (ioctl(s, SIOCG80211NWKEY, (caddr_t)&nwkey) != 0 ||
   1416 	    nwkey.i_wepon == 0) {
   1417 		printf("\n");
   1418 		goto skip_wep;
   1419 	}
   1420 
   1421 	printf(" nwkey ");
   1422 	/* try to retrieve WEP keys */
   1423 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1424 		nwkey.i_key[i].i_keydat = keybuf[i];
   1425 		nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
   1426 	}
   1427 	if (ioctl(s, SIOCG80211NWKEY, (caddr_t)&nwkey) != 0) {
   1428 		printf("*****");
   1429 	} else {
   1430 		nwkey_verbose = 0;
   1431 		/* check to see non default key or multiple keys defined */
   1432 		if (nwkey.i_defkid != 1) {
   1433 			nwkey_verbose = 1;
   1434 		} else {
   1435 			for (i = 1; i < IEEE80211_WEP_NKID; i++) {
   1436 				if (nwkey.i_key[i].i_keylen != 0) {
   1437 					nwkey_verbose = 1;
   1438 					break;
   1439 				}
   1440 			}
   1441 		}
   1442 		/* check extra ambiguity with keywords */
   1443 		if (!nwkey_verbose) {
   1444 			if (nwkey.i_key[0].i_keylen >= 2 &&
   1445 			    isdigit(nwkey.i_key[0].i_keydat[0]) &&
   1446 			    nwkey.i_key[0].i_keydat[1] == ':')
   1447 				nwkey_verbose = 1;
   1448 			else if (nwkey.i_key[0].i_keylen >= 7 &&
   1449 			    strncasecmp("persist", nwkey.i_key[0].i_keydat, 7)
   1450 			    == 0)
   1451 				nwkey_verbose = 1;
   1452 		}
   1453 		if (nwkey_verbose)
   1454 			printf("%d:", nwkey.i_defkid);
   1455 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1456 			if (i > 0)
   1457 				printf(",");
   1458 			if (nwkey.i_key[i].i_keylen < 0)
   1459 				printf("persist");
   1460 			else
   1461 				print_string(nwkey.i_key[i].i_keydat,
   1462 				    nwkey.i_key[i].i_keylen);
   1463 			if (!nwkey_verbose)
   1464 				break;
   1465 		}
   1466 	}
   1467 	printf("\n");
   1468 
   1469  skip_wep:
   1470 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
   1471 	if (ioctl(s, SIOCG80211POWER, &power) != 0)
   1472 		return;
   1473 	printf("\tpowersave ");
   1474 	if (power.i_enabled)
   1475 		printf("on (%dms sleep)", power.i_maxsleep);
   1476 	else
   1477 		printf("off");
   1478 	printf("\n");
   1479 }
   1480 
   1481 void
   1482 init_current_media()
   1483 {
   1484 	struct ifmediareq ifmr;
   1485 
   1486 	/*
   1487 	 * If we have not yet done so, grab the currently-selected
   1488 	 * media.
   1489 	 */
   1490 	if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
   1491 		(void) memset(&ifmr, 0, sizeof(ifmr));
   1492 		(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1493 
   1494 		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1495 			/*
   1496 			 * If we get E2BIG, the kernel is telling us
   1497 			 * that there are more, so we can ignore it.
   1498 			 */
   1499 			if (errno != E2BIG)
   1500 				err(1, "SGIOCGIFMEDIA");
   1501 		}
   1502 
   1503 		media_current = ifmr.ifm_current;
   1504 	}
   1505 
   1506 	/* Sanity. */
   1507 	if (IFM_TYPE(media_current) == 0)
   1508 		errx(1, "%s: no link type?", name);
   1509 }
   1510 
   1511 void
   1512 process_media_commands()
   1513 {
   1514 
   1515 	if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
   1516 		/* Nothing to do. */
   1517 		return;
   1518 	}
   1519 
   1520 	/*
   1521 	 * Media already set up, and commands sanity-checked.  Set/clear
   1522 	 * any options, and we're ready to go.
   1523 	 */
   1524 	media_current |= mediaopt_set;
   1525 	media_current &= ~mediaopt_clear;
   1526 
   1527 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1528 	ifr.ifr_media = media_current;
   1529 
   1530 	if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
   1531 		err(1, "SIOCSIFMEDIA");
   1532 }
   1533 
   1534 void
   1535 setmedia(val, d)
   1536 	const char *val;
   1537 	int d;
   1538 {
   1539 	int type, subtype, inst;
   1540 
   1541 	init_current_media();
   1542 
   1543 	/* Only one media command may be given. */
   1544 	if (actions & A_MEDIA)
   1545 		errx(1, "only one `media' command may be issued");
   1546 
   1547 	/* Must not come after mediaopt commands */
   1548 	if (actions & A_MEDIAOPT)
   1549 		errx(1, "may not issue `media' after `mediaopt' commands");
   1550 
   1551 	/*
   1552 	 * No need to check if `instance' has been issued; setmediainst()
   1553 	 * craps out if `media' has not been specified.
   1554 	 */
   1555 
   1556 	type = IFM_TYPE(media_current);
   1557 	inst = IFM_INST(media_current);
   1558 
   1559 	/* Look up the subtype. */
   1560 	subtype = get_media_subtype(type, val);
   1561 
   1562 	/* Build the new current media word. */
   1563 	media_current = IFM_MAKEWORD(type, subtype, 0, inst);
   1564 
   1565 	/* Media will be set after other processing is complete. */
   1566 }
   1567 
   1568 void
   1569 setmediaopt(val, d)
   1570 	const char *val;
   1571 	int d;
   1572 {
   1573 
   1574 	init_current_media();
   1575 
   1576 	/* Can only issue `mediaopt' once. */
   1577 	if (actions & A_MEDIAOPTSET)
   1578 		errx(1, "only one `mediaopt' command may be issued");
   1579 
   1580 	/* Can't issue `mediaopt' if `instance' has already been issued. */
   1581 	if (actions & A_MEDIAINST)
   1582 		errx(1, "may not issue `mediaopt' after `instance'");
   1583 
   1584 	mediaopt_set = get_media_options(IFM_TYPE(media_current), val);
   1585 
   1586 	/* Media will be set after other processing is complete. */
   1587 }
   1588 
   1589 void
   1590 unsetmediaopt(val, d)
   1591 	const char *val;
   1592 	int d;
   1593 {
   1594 
   1595 	init_current_media();
   1596 
   1597 	/* Can only issue `-mediaopt' once. */
   1598 	if (actions & A_MEDIAOPTCLR)
   1599 		errx(1, "only one `-mediaopt' command may be issued");
   1600 
   1601 	/* May not issue `media' and `-mediaopt'. */
   1602 	if (actions & A_MEDIA)
   1603 		errx(1, "may not issue both `media' and `-mediaopt'");
   1604 
   1605 	/*
   1606 	 * No need to check for A_MEDIAINST, since the test for A_MEDIA
   1607 	 * implicitly checks for A_MEDIAINST.
   1608 	 */
   1609 
   1610 	mediaopt_clear = get_media_options(IFM_TYPE(media_current), val);
   1611 
   1612 	/* Media will be set after other processing is complete. */
   1613 }
   1614 
   1615 void
   1616 setmediainst(val, d)
   1617 	const char *val;
   1618 	int d;
   1619 {
   1620 	int type, subtype, options, inst;
   1621 
   1622 	init_current_media();
   1623 
   1624 	/* Can only issue `instance' once. */
   1625 	if (actions & A_MEDIAINST)
   1626 		errx(1, "only one `instance' command may be issued");
   1627 
   1628 	/* Must have already specified `media' */
   1629 	if ((actions & A_MEDIA) == 0)
   1630 		errx(1, "must specify `media' before `instance'");
   1631 
   1632 	type = IFM_TYPE(media_current);
   1633 	subtype = IFM_SUBTYPE(media_current);
   1634 	options = IFM_OPTIONS(media_current);
   1635 
   1636 	inst = atoi(val);
   1637 	if (inst < 0 || inst > IFM_INST_MAX)
   1638 		errx(1, "invalid media instance: %s", val);
   1639 
   1640 	media_current = IFM_MAKEWORD(type, subtype, options, inst);
   1641 
   1642 	/* Media will be set after other processing is complete. */
   1643 }
   1644 
   1645 struct ifmedia_description ifm_type_descriptions[] =
   1646     IFM_TYPE_DESCRIPTIONS;
   1647 
   1648 struct ifmedia_description ifm_subtype_descriptions[] =
   1649     IFM_SUBTYPE_DESCRIPTIONS;
   1650 
   1651 struct ifmedia_description ifm_option_descriptions[] =
   1652     IFM_OPTION_DESCRIPTIONS;
   1653 
   1654 const char *
   1655 get_media_type_string(mword)
   1656 	int mword;
   1657 {
   1658 	struct ifmedia_description *desc;
   1659 
   1660 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
   1661 	     desc++) {
   1662 		if (IFM_TYPE(mword) == desc->ifmt_word)
   1663 			return (desc->ifmt_string);
   1664 	}
   1665 	return ("<unknown type>");
   1666 }
   1667 
   1668 const char *
   1669 get_media_subtype_string(mword)
   1670 	int mword;
   1671 {
   1672 	struct ifmedia_description *desc;
   1673 
   1674 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
   1675 	     desc++) {
   1676 		if (IFM_TYPE_MATCH(desc->ifmt_word, mword) &&
   1677 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword))
   1678 			return (desc->ifmt_string);
   1679 	}
   1680 	return ("<unknown subtype>");
   1681 }
   1682 
   1683 int
   1684 get_media_subtype(type, val)
   1685 	int type;
   1686 	const char *val;
   1687 {
   1688 	int rval;
   1689 
   1690 	rval = lookup_media_word(ifm_subtype_descriptions, type, val);
   1691 	if (rval == -1)
   1692 		errx(1, "unknown %s media subtype: %s",
   1693 		    get_media_type_string(type), val);
   1694 
   1695 	return (rval);
   1696 }
   1697 
   1698 int
   1699 get_media_options(type, val)
   1700 	int type;
   1701 	const char *val;
   1702 {
   1703 	char *optlist, *str;
   1704 	int option, rval = 0;
   1705 
   1706 	/* We muck with the string, so copy it. */
   1707 	optlist = strdup(val);
   1708 	if (optlist == NULL)
   1709 		err(1, "strdup");
   1710 	str = optlist;
   1711 
   1712 	/*
   1713 	 * Look up the options in the user-provided comma-separated list.
   1714 	 */
   1715 	for (; (str = strtok(str, ",")) != NULL; str = NULL) {
   1716 		option = lookup_media_word(ifm_option_descriptions, type, str);
   1717 		if (option == -1)
   1718 			errx(1, "unknown %s media option: %s",
   1719 			    get_media_type_string(type), str);
   1720 		rval |= IFM_OPTIONS(option);
   1721 	}
   1722 
   1723 	free(optlist);
   1724 	return (rval);
   1725 }
   1726 
   1727 int
   1728 lookup_media_word(desc, type, val)
   1729 	struct ifmedia_description *desc;
   1730 	int type;
   1731 	const char *val;
   1732 {
   1733 
   1734 	for (; desc->ifmt_string != NULL; desc++) {
   1735 		if (IFM_TYPE_MATCH(desc->ifmt_word, type) &&
   1736 		    strcasecmp(desc->ifmt_string, val) == 0)
   1737 			return (desc->ifmt_word);
   1738 	}
   1739 	return (-1);
   1740 }
   1741 
   1742 void
   1743 print_media_word(ifmw, print_type, as_syntax)
   1744 	int ifmw, print_type, as_syntax;
   1745 {
   1746 	struct ifmedia_description *desc;
   1747 	int seen_option = 0;
   1748 
   1749 	if (print_type)
   1750 		printf("%s ", get_media_type_string(ifmw));
   1751 	printf("%s%s", as_syntax ? "media " : "",
   1752 	    get_media_subtype_string(ifmw));
   1753 
   1754 	/* Find options. */
   1755 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
   1756 	     desc++) {
   1757 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
   1758 		    (ifmw & IFM_OPTIONS(desc->ifmt_word)) != 0 &&
   1759 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
   1760 			if (seen_option == 0)
   1761 				printf(" %s", as_syntax ? "mediaopt " : "");
   1762 			printf("%s%s", seen_option ? "," : "",
   1763 			    desc->ifmt_string);
   1764 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
   1765 		}
   1766 	}
   1767 	if (IFM_INST(ifmw) != 0)
   1768 		printf(" instance %d", IFM_INST(ifmw));
   1769 }
   1770 
   1771 int carrier()
   1772 {
   1773 	struct ifmediareq ifmr;
   1774 
   1775 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1776 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1777 
   1778 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1779 		/*
   1780 		 * Interface doesn't support SIOC{G,S}IFMEDIA;
   1781 		 * assume ok.
   1782 		 */
   1783 		return 0;
   1784 	}
   1785 	if ((ifmr.ifm_status & IFM_AVALID) == 0) {
   1786 		/*
   1787 		 * Interface doesn't report media-valid status.
   1788 		 * assume ok.
   1789 		 */
   1790 		return 0;
   1791 	}
   1792 	/* otherwise, return ok for active, not-ok if not active. */
   1793 	return !(ifmr.ifm_status & IFM_ACTIVE);
   1794 }
   1795 
   1796 
   1797 #define	IFFBITS \
   1798 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
   1799 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
   1800 
   1801 #define	IFCAPBITS \
   1802 "\020\1IP4CSUM\2TCP4CSUM\3UDP4CSUM\4TCP6CSUM\5UDP6CSUM"
   1803 
   1804 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
   1805 
   1806 const struct ifmedia_status_description ifm_status_descriptions[] =
   1807     IFM_STATUS_DESCRIPTIONS;
   1808 
   1809 /*
   1810  * Print the status of the interface.  If an address family was
   1811  * specified, show it and it only; otherwise, show them all.
   1812  */
   1813 void
   1814 status(ap, alen)
   1815 	const u_int8_t *ap;
   1816 	int alen;
   1817 {
   1818 	struct afswtch *p = afp;
   1819 	struct ifmediareq ifmr;
   1820 	int *media_list, i;
   1821 
   1822 	printf("%s: ", name);
   1823 	printb("flags", flags, IFFBITS);
   1824 	if (metric)
   1825 		printf(" metric %d", metric);
   1826 	if (mtu)
   1827 		printf(" mtu %d", mtu);
   1828 	putchar('\n');
   1829 
   1830 	if (g_ifcr.ifcr_capabilities) {
   1831 		putchar('\t');
   1832 		printb("capabilities", g_ifcr.ifcr_capabilities, IFCAPBITS);
   1833 		putchar('\n');
   1834 
   1835 		putchar('\t');
   1836 		printb("enabled", g_ifcr.ifcr_capenable, IFCAPBITS);
   1837 		putchar('\n');
   1838 	}
   1839 
   1840 	ieee80211_status();
   1841 	vlan_status();
   1842 	tunnel_status();
   1843 
   1844 	if (ap && alen > 0) {
   1845 		printf("\taddress:");
   1846 		for (i = 0; i < alen; i++, ap++)
   1847 			printf("%c%02x", i > 0 ? ':' : ' ', *ap);
   1848 		putchar('\n');
   1849 	}
   1850 
   1851 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1852 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1853 
   1854 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1855 		/*
   1856 		 * Interface doesn't support SIOC{G,S}IFMEDIA.
   1857 		 */
   1858 		goto proto_status;
   1859 	}
   1860 
   1861 	if (ifmr.ifm_count == 0) {
   1862 		warnx("%s: no media types?", name);
   1863 		goto proto_status;
   1864 	}
   1865 
   1866 	media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
   1867 	if (media_list == NULL)
   1868 		err(1, "malloc");
   1869 	ifmr.ifm_ulist = media_list;
   1870 
   1871 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
   1872 		err(1, "SIOCGIFMEDIA");
   1873 
   1874 	printf("\tmedia: ");
   1875 	print_media_word(ifmr.ifm_current, 1, 0);
   1876 	if (ifmr.ifm_active != ifmr.ifm_current) {
   1877 		putchar(' ');
   1878 		putchar('(');
   1879 		print_media_word(ifmr.ifm_active, 0, 0);
   1880 		putchar(')');
   1881 	}
   1882 	putchar('\n');
   1883 
   1884 	if (ifmr.ifm_status & IFM_STATUS_VALID) {
   1885 		const struct ifmedia_status_description *ifms;
   1886 		int bitno, found = 0;
   1887 
   1888 		printf("\tstatus: ");
   1889 		for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
   1890 			for (ifms = ifm_status_descriptions;
   1891 			     ifms->ifms_valid != 0; ifms++) {
   1892 				if (ifms->ifms_type !=
   1893 				      IFM_TYPE(ifmr.ifm_current) ||
   1894 				    ifms->ifms_valid !=
   1895 				      ifm_status_valid_list[bitno])
   1896 					continue;
   1897 				printf("%s%s", found ? ", " : "",
   1898 				    IFM_STATUS_DESC(ifms, ifmr.ifm_status));
   1899 				found = 1;
   1900 
   1901 				/*
   1902 				 * For each valid indicator bit, there's
   1903 				 * only one entry for each media type, so
   1904 				 * terminate the inner loop now.
   1905 				 */
   1906 				break;
   1907 			}
   1908 		}
   1909 
   1910 		if (found == 0)
   1911 			printf("unknown");
   1912 		putchar('\n');
   1913 	}
   1914 
   1915 	if (mflag) {
   1916 		int type, printed_type;
   1917 
   1918 		for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
   1919 			for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
   1920 				if (IFM_TYPE(media_list[i]) == type) {
   1921 					if (printed_type == 0) {
   1922 					    printf("\tsupported %s media:\n",
   1923 					      get_media_type_string(type));
   1924 					    printed_type = 1;
   1925 					}
   1926 					printf("\t\t");
   1927 					print_media_word(media_list[i], 0, 1);
   1928 					printf("\n");
   1929 				}
   1930 			}
   1931 		}
   1932 	}
   1933 
   1934 	free(media_list);
   1935 
   1936  proto_status:
   1937 	if ((p = afp) != NULL) {
   1938 		(*p->af_status)(1);
   1939 	} else for (p = afs; p->af_name; p++) {
   1940 		ifr.ifr_addr.sa_family = p->af_af;
   1941 		(*p->af_status)(0);
   1942 	}
   1943 }
   1944 
   1945 void
   1946 tunnel_status()
   1947 {
   1948 	char psrcaddr[NI_MAXHOST];
   1949 	char pdstaddr[NI_MAXHOST];
   1950 	const char *ver = "";
   1951 #ifdef NI_WITHSCOPEID
   1952 	const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
   1953 #else
   1954 	const int niflag = NI_NUMERICHOST;
   1955 #endif
   1956 	struct if_laddrreq req;
   1957 
   1958 	psrcaddr[0] = pdstaddr[0] = '\0';
   1959 
   1960 	memset(&req, 0, sizeof(req));
   1961 	strncpy(req.iflr_name, name, IFNAMSIZ);
   1962 	if (ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&req) < 0)
   1963 		return;
   1964 #ifdef INET6
   1965 	if (req.addr.ss_family == AF_INET6)
   1966 		in6_fillscopeid((struct sockaddr_in6 *)&req.addr);
   1967 #endif
   1968 	getnameinfo((struct sockaddr *)&req.addr, req.addr.ss_len,
   1969 	    psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
   1970 #ifdef INET6
   1971 	if (req.addr.ss_family == AF_INET6)
   1972 		ver = "6";
   1973 #endif
   1974 
   1975 #ifdef INET6
   1976 	if (req.dstaddr.ss_family == AF_INET6)
   1977 		in6_fillscopeid((struct sockaddr_in6 *)&req.dstaddr);
   1978 #endif
   1979 	getnameinfo((struct sockaddr *)&req.dstaddr, req.dstaddr.ss_len,
   1980 	    pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
   1981 
   1982 	printf("\ttunnel inet%s %s --> %s\n", ver, psrcaddr, pdstaddr);
   1983 }
   1984 
   1985 void
   1986 vlan_status()
   1987 {
   1988 	struct vlanreq vlr;
   1989 
   1990 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
   1991 	    !isdigit(ifr.ifr_name[4]))
   1992 		return;
   1993 
   1994 	memset(&vlr, 0, sizeof(vlr));
   1995 	ifr.ifr_data = (caddr_t)&vlr;
   1996 
   1997 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
   1998 		return;
   1999 
   2000 	if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
   2001 		printf("\tvlan: %d parent: %s\n",
   2002 		    vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
   2003 		    "<none>" : vlr.vlr_parent);
   2004 }
   2005 
   2006 void
   2007 in_alias(creq)
   2008 	struct ifreq *creq;
   2009 {
   2010 	struct sockaddr_in *sin;
   2011 	int alias;
   2012 
   2013 	if (lflag)
   2014 		return;
   2015 
   2016 	alias = 1;
   2017 
   2018 	/* Get the non-alias address for this interface. */
   2019 	getsock(AF_INET);
   2020 	if (s < 0) {
   2021 		if (errno == EPROTONOSUPPORT)
   2022 			return;
   2023 		err(1, "socket");
   2024 	}
   2025 	(void) memset(&ifr, 0, sizeof(ifr));
   2026 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   2027 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   2028 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2029 			return;
   2030 		} else
   2031 			warn("SIOCGIFADDR");
   2032 	}
   2033 	/* If creq and ifr are the same address, this is not an alias. */
   2034 	if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
   2035 		   sizeof(creq->ifr_addr)) == 0)
   2036 		alias = 0;
   2037 	(void) memset(&addreq, 0, sizeof(addreq));
   2038 	(void) strncpy(addreq.ifra_name, name, sizeof(addreq.ifra_name));
   2039 	addreq.ifra_addr = creq->ifr_addr;
   2040 	if (ioctl(s, SIOCGIFALIAS, (caddr_t)&addreq) < 0) {
   2041 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2042 			return;
   2043 		} else
   2044 			warn("SIOCGIFALIAS");
   2045 	}
   2046 
   2047 	sin = (struct sockaddr_in *)&addreq.ifra_addr;
   2048 	printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(sin->sin_addr));
   2049 
   2050 	if (flags & IFF_POINTOPOINT) {
   2051 		sin = (struct sockaddr_in *)&addreq.ifra_dstaddr;
   2052 		printf(" -> %s", inet_ntoa(sin->sin_addr));
   2053 	}
   2054 
   2055 	sin = (struct sockaddr_in *)&addreq.ifra_mask;
   2056 	printf(" netmask 0x%x", ntohl(sin->sin_addr.s_addr));
   2057 
   2058 	if (flags & IFF_BROADCAST) {
   2059 		sin = (struct sockaddr_in *)&addreq.ifra_broadaddr;
   2060 		printf(" broadcast %s", inet_ntoa(sin->sin_addr));
   2061 	}
   2062 	printf("\n");
   2063 }
   2064 
   2065 void
   2066 in_status(force)
   2067 	int force;
   2068 {
   2069 	struct ifaddrs *ifap, *ifa;
   2070 	struct ifreq ifr;
   2071 
   2072 	if (getifaddrs(&ifap) != 0)
   2073 		err(1, "getifaddrs");
   2074 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   2075 		if (strcmp(name, ifa->ifa_name) != 0)
   2076 			continue;
   2077 		if (ifa->ifa_addr->sa_family != AF_INET)
   2078 			continue;
   2079 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
   2080 			continue;
   2081 
   2082 		memset(&ifr, 0, sizeof(ifr));
   2083 		strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
   2084 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
   2085 		in_alias(&ifr);
   2086 	}
   2087 	freeifaddrs(ifap);
   2088 }
   2089 
   2090 void
   2091 setifprefixlen(addr, d)
   2092 	const char *addr;
   2093 	int d;
   2094 {
   2095 	if (*afp->af_getprefix)
   2096 		(*afp->af_getprefix)(addr, MASK);
   2097 	explicit_prefix = 1;
   2098 }
   2099 
   2100 #ifdef INET6
   2101 void
   2102 in6_fillscopeid(sin6)
   2103 	struct sockaddr_in6 *sin6;
   2104 {
   2105 #if defined(__KAME__) && defined(KAME_SCOPEID)
   2106 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   2107 		sin6->sin6_scope_id =
   2108 			ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
   2109 		sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
   2110 	}
   2111 #endif
   2112 }
   2113 
   2114 /* XXX not really an alias */
   2115 void
   2116 in6_alias(creq)
   2117 	struct in6_ifreq *creq;
   2118 {
   2119 	struct sockaddr_in6 *sin6;
   2120 	char hbuf[NI_MAXHOST];
   2121 	u_int32_t scopeid;
   2122 #ifdef NI_WITHSCOPEID
   2123 	const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
   2124 #else
   2125 	const int niflag = NI_NUMERICHOST;
   2126 #endif
   2127 
   2128 	/* Get the non-alias address for this interface. */
   2129 	getsock(AF_INET6);
   2130 	if (s < 0) {
   2131 		if (errno == EPROTONOSUPPORT)
   2132 			return;
   2133 		err(1, "socket");
   2134 	}
   2135 
   2136 	sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
   2137 
   2138 	in6_fillscopeid(sin6);
   2139 	scopeid = sin6->sin6_scope_id;
   2140 	if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
   2141 			hbuf, sizeof(hbuf), NULL, 0, niflag))
   2142 		strncpy(hbuf, "", sizeof(hbuf));	/* some message? */
   2143 	printf("\tinet6 %s", hbuf);
   2144 
   2145 	if (flags & IFF_POINTOPOINT) {
   2146 		(void) memset(&ifr6, 0, sizeof(ifr6));
   2147 		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2148 		ifr6.ifr_addr = creq->ifr_addr;
   2149 		if (ioctl(s, SIOCGIFDSTADDR_IN6, (caddr_t)&ifr6) < 0) {
   2150 			if (errno != EADDRNOTAVAIL)
   2151 				warn("SIOCGIFDSTADDR_IN6");
   2152 			(void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
   2153 			ifr6.ifr_addr.sin6_family = AF_INET6;
   2154 			ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
   2155 		}
   2156 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
   2157 		in6_fillscopeid(sin6);
   2158 		hbuf[0] = '\0';
   2159 		if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
   2160 				hbuf, sizeof(hbuf), NULL, 0, niflag))
   2161 			strncpy(hbuf, "", sizeof(hbuf)); /* some message? */
   2162 		printf(" -> %s", hbuf);
   2163 	}
   2164 
   2165 	(void) memset(&ifr6, 0, sizeof(ifr6));
   2166 	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2167 	ifr6.ifr_addr = creq->ifr_addr;
   2168 	if (ioctl(s, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) {
   2169 		if (errno != EADDRNOTAVAIL)
   2170 			warn("SIOCGIFNETMASK_IN6");
   2171 	} else {
   2172 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
   2173 		printf(" prefixlen %d", prefix(&sin6->sin6_addr,
   2174 					       sizeof(struct in6_addr)));
   2175 	}
   2176 
   2177 	(void) memset(&ifr6, 0, sizeof(ifr6));
   2178 	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2179 	ifr6.ifr_addr = creq->ifr_addr;
   2180 	if (ioctl(s, SIOCGIFAFLAG_IN6, (caddr_t)&ifr6) < 0) {
   2181 		if (errno != EADDRNOTAVAIL)
   2182 			warn("SIOCGIFAFLAG_IN6");
   2183 	} else {
   2184 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
   2185 			printf(" anycast");
   2186 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
   2187 			printf(" tentative");
   2188 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
   2189 			printf(" duplicated");
   2190 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
   2191 			printf(" detached");
   2192 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
   2193 			printf(" deprecated");
   2194 	}
   2195 
   2196 	if (scopeid)
   2197 		printf(" scopeid 0x%x", scopeid);
   2198 
   2199 	if (Lflag) {
   2200 		struct in6_addrlifetime *lifetime;
   2201 		(void) memset(&ifr6, 0, sizeof(ifr6));
   2202 		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2203 		ifr6.ifr_addr = creq->ifr_addr;
   2204 		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
   2205 		if (ioctl(s, SIOCGIFALIFETIME_IN6, (caddr_t)&ifr6) < 0) {
   2206 			if (errno != EADDRNOTAVAIL)
   2207 				warn("SIOCGIFALIFETIME_IN6");
   2208 		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
   2209 			time_t t = time(NULL);
   2210 			printf(" pltime ");
   2211 			if (lifetime->ia6t_preferred) {
   2212 				printf("%s", lifetime->ia6t_preferred < t
   2213 					? "0"
   2214 					: sec2str(lifetime->ia6t_preferred - t));
   2215 			} else
   2216 				printf("infty");
   2217 
   2218 			printf(" vltime ");
   2219 			if (lifetime->ia6t_expire) {
   2220 				printf("%s", lifetime->ia6t_expire < t
   2221 					? "0"
   2222 					: sec2str(lifetime->ia6t_expire - t));
   2223 			} else
   2224 				printf("infty");
   2225 		}
   2226 	}
   2227 
   2228 	printf("\n");
   2229 }
   2230 
   2231 void
   2232 in6_status(force)
   2233 	int force;
   2234 {
   2235 	struct ifaddrs *ifap, *ifa;
   2236 	struct in6_ifreq ifr;
   2237 
   2238 	if (getifaddrs(&ifap) != 0)
   2239 		err(1, "getifaddrs");
   2240 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   2241 		if (strcmp(name, ifa->ifa_name) != 0)
   2242 			continue;
   2243 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2244 			continue;
   2245 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
   2246 			continue;
   2247 
   2248 		memset(&ifr, 0, sizeof(ifr));
   2249 		strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
   2250 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
   2251 		in6_alias(&ifr);
   2252 	}
   2253 	freeifaddrs(ifap);
   2254 }
   2255 #endif /*INET6*/
   2256 
   2257 #ifndef INET_ONLY
   2258 
   2259 void
   2260 at_status(force)
   2261 	int force;
   2262 {
   2263 	struct sockaddr_at *sat, null_sat;
   2264 	struct netrange *nr;
   2265 
   2266 	getsock(AF_APPLETALK);
   2267 	if (s < 0) {
   2268 		if (errno == EPROTONOSUPPORT)
   2269 			return;
   2270 		err(1, "socket");
   2271 	}
   2272 	(void) memset(&ifr, 0, sizeof(ifr));
   2273 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   2274 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   2275 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2276 			if (!force)
   2277 				return;
   2278 			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2279 		} else
   2280 			warn("SIOCGIFADDR");
   2281 	}
   2282 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   2283 	sat = (struct sockaddr_at *)&ifr.ifr_addr;
   2284 
   2285 	(void) memset(&null_sat, 0, sizeof(null_sat));
   2286 
   2287 	nr = (struct netrange *) &sat->sat_zero;
   2288 	printf("\tatalk %d.%d range %d-%d phase %d",
   2289 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
   2290 	    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
   2291 	if (flags & IFF_POINTOPOINT) {
   2292 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   2293 			if (errno == EADDRNOTAVAIL)
   2294 			    (void) memset(&ifr.ifr_addr, 0,
   2295 				sizeof(ifr.ifr_addr));
   2296 			else
   2297 			    warn("SIOCGIFDSTADDR");
   2298 		}
   2299 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   2300 		sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
   2301 		if (!sat)
   2302 			sat = &null_sat;
   2303 		printf("--> %d.%d",
   2304 		    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
   2305 	}
   2306 	if (flags & IFF_BROADCAST) {
   2307 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
   2308 		sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
   2309 		if (sat)
   2310 			printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
   2311 			    sat->sat_addr.s_node);
   2312 	}
   2313 	putchar('\n');
   2314 }
   2315 
   2316 void
   2317 xns_status(force)
   2318 	int force;
   2319 {
   2320 	struct sockaddr_ns *sns;
   2321 
   2322 	getsock(AF_NS);
   2323 	if (s < 0) {
   2324 		if (errno == EPROTONOSUPPORT)
   2325 			return;
   2326 		err(1, "socket");
   2327 	}
   2328 	(void) memset(&ifr, 0, sizeof(ifr));
   2329 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   2330 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   2331 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2332 			if (!force)
   2333 				return;
   2334 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2335 		} else
   2336 			warn("SIOCGIFADDR");
   2337 	}
   2338 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   2339 	sns = (struct sockaddr_ns *)&ifr.ifr_addr;
   2340 	printf("\tns %s ", ns_ntoa(sns->sns_addr));
   2341 	if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
   2342 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   2343 			if (errno == EADDRNOTAVAIL)
   2344 			    memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2345 			else
   2346 			    warn("SIOCGIFDSTADDR");
   2347 		}
   2348 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   2349 		sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
   2350 		printf("--> %s ", ns_ntoa(sns->sns_addr));
   2351 	}
   2352 	putchar('\n');
   2353 }
   2354 
   2355 void
   2356 iso_status(force)
   2357 	int force;
   2358 {
   2359 	struct sockaddr_iso *siso;
   2360 	struct iso_ifreq ifr;
   2361 
   2362 	getsock(AF_ISO);
   2363 	if (s < 0) {
   2364 		if (errno == EPROTONOSUPPORT)
   2365 			return;
   2366 		err(1, "socket");
   2367 	}
   2368 	(void) memset(&ifr, 0, sizeof(ifr));
   2369 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   2370 	if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&ifr) < 0) {
   2371 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2372 			if (!force)
   2373 				return;
   2374 			(void) memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   2375 		} else
   2376 			warn("SIOCGIFADDR_ISO");
   2377 	}
   2378 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   2379 	siso = &ifr.ifr_Addr;
   2380 	printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
   2381 	if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&ifr) < 0) {
   2382 		if (errno == EADDRNOTAVAIL)
   2383 			memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   2384 		else
   2385 			warn("SIOCGIFNETMASK_ISO");
   2386 	} else {
   2387 		if (siso->siso_len > offsetof(struct sockaddr_iso, siso_addr))
   2388 			siso->siso_addr.isoa_len = siso->siso_len
   2389 			    - offsetof(struct sockaddr_iso, siso_addr);
   2390 		printf("\n\t\tnetmask %s ", iso_ntoa(&siso->siso_addr));
   2391 	}
   2392 	if (flags & IFF_POINTOPOINT) {
   2393 		if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&ifr) < 0) {
   2394 			if (errno == EADDRNOTAVAIL)
   2395 			    memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   2396 			else
   2397 			    warn("SIOCGIFDSTADDR_ISO");
   2398 		}
   2399 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   2400 		siso = &ifr.ifr_Addr;
   2401 		printf("--> %s ", iso_ntoa(&siso->siso_addr));
   2402 	}
   2403 	putchar('\n');
   2404 }
   2405 
   2406 #endif	/* INET_ONLY */
   2407 
   2408 #define SIN(x) ((struct sockaddr_in *) &(x))
   2409 struct sockaddr_in *sintab[] = {
   2410 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
   2411 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
   2412 
   2413 void
   2414 in_getaddr(s, which)
   2415 	const char *s;
   2416 	int which;
   2417 {
   2418 	struct sockaddr_in *sin = sintab[which];
   2419 	struct hostent *hp;
   2420 	struct netent *np;
   2421 
   2422 	sin->sin_len = sizeof(*sin);
   2423 	if (which != MASK)
   2424 		sin->sin_family = AF_INET;
   2425 
   2426 	if (which == ADDR) {
   2427 		char *p = NULL;
   2428 		if ((p = strrchr(s, '/')) != NULL) {
   2429 			*p = '\0';
   2430 			in_getprefix(p + 1, MASK);
   2431 		}
   2432 	}
   2433 
   2434 	if (inet_aton(s, &sin->sin_addr) == 0) {
   2435 		if ((hp = gethostbyname(s)) != NULL)
   2436 			(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
   2437 		else if ((np = getnetbyname(s)) != NULL)
   2438 			sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
   2439 		else
   2440 			errx(1, "%s: bad value", s);
   2441 	}
   2442 }
   2443 
   2444 void
   2445 in_getprefix(plen, which)
   2446 	const char *plen;
   2447 	int which;
   2448 {
   2449 	register struct sockaddr_in *sin = sintab[which];
   2450 	register u_char *cp;
   2451 	int len = strtol(plen, (char **)NULL, 10);
   2452 
   2453 	if ((len < 0) || (len > 32))
   2454 		errx(1, "%s: bad value", plen);
   2455 	sin->sin_len = sizeof(*sin);
   2456 	if (which != MASK)
   2457 		sin->sin_family = AF_INET;
   2458 	if ((len == 0) || (len == 32)) {
   2459 		memset(&sin->sin_addr, 0xff, sizeof(struct in_addr));
   2460 		return;
   2461 	}
   2462 	memset((void *)&sin->sin_addr, 0x00, sizeof(sin->sin_addr));
   2463 	for (cp = (u_char *)&sin->sin_addr; len > 7; len -= 8)
   2464 		*cp++ = 0xff;
   2465 	if (len)
   2466 		*cp = 0xff << (8 - len);
   2467 }
   2468 
   2469 /*
   2470  * Print a value a la the %b format of the kernel's printf
   2471  */
   2472 void
   2473 printb(s, v, bits)
   2474 	const char *s;
   2475 	unsigned short v;
   2476 	const char *bits;
   2477 {
   2478 	int i, any = 0;
   2479 	char c;
   2480 
   2481 	if (bits && *bits == 8)
   2482 		printf("%s=%o", s, v);
   2483 	else
   2484 		printf("%s=%x", s, v);
   2485 	bits++;
   2486 	if (bits) {
   2487 		putchar('<');
   2488 		while ((i = *bits++) != 0) {
   2489 			if (v & (1 << (i-1))) {
   2490 				if (any)
   2491 					putchar(',');
   2492 				any = 1;
   2493 				for (; (c = *bits) > 32; bits++)
   2494 					putchar(c);
   2495 			} else
   2496 				for (; *bits > 32; bits++)
   2497 					;
   2498 		}
   2499 		putchar('>');
   2500 	}
   2501 }
   2502 
   2503 #ifdef INET6
   2504 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
   2505 struct sockaddr_in6 *sin6tab[] = {
   2506 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
   2507 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
   2508 
   2509 void
   2510 in6_getaddr(s, which)
   2511 	const char *s;
   2512 	int which;
   2513 {
   2514 #if defined(__KAME__) && defined(KAME_SCOPEID)
   2515 	struct sockaddr_in6 *sin6 = sin6tab[which];
   2516 	struct addrinfo hints, *res;
   2517 	int error;
   2518 
   2519 	memset(&hints, 0, sizeof(hints));
   2520 	hints.ai_family = AF_INET6;
   2521 	hints.ai_socktype = SOCK_DGRAM;
   2522 #if 0 /* in_getaddr() allows FQDN */
   2523 	hints.ai_flags = AI_NUMERICHOST;
   2524 #endif
   2525 	error = getaddrinfo(s, "0", &hints, &res);
   2526 	if (error)
   2527 		errx(1, "%s: %s", s, gai_strerror(error));
   2528 	if (res->ai_next)
   2529 		errx(1, "%s: resolved to multiple hosts", s);
   2530 	if (res->ai_addrlen != sizeof(struct sockaddr_in6))
   2531 		errx(1, "%s: bad value", s);
   2532 	memcpy(sin6, res->ai_addr, res->ai_addrlen);
   2533 	freeaddrinfo(res);
   2534 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) {
   2535 		*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] =
   2536 			htons(sin6->sin6_scope_id);
   2537 		sin6->sin6_scope_id = 0;
   2538 	}
   2539 #else
   2540 	struct sockaddr_in6 *sin = sin6tab[which];
   2541 
   2542 	sin->sin6_len = sizeof(*sin);
   2543 	if (which != MASK)
   2544 		sin->sin6_family = AF_INET6;
   2545 
   2546 	if (which == ADDR) {
   2547 		char *p = NULL;
   2548 		if((p = strrchr(s, '/')) != NULL) {
   2549 			*p = '\0';
   2550 			in6_getprefix(p + 1, MASK);
   2551 			explicit_prefix = 1;
   2552 		}
   2553 	}
   2554 
   2555 	if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
   2556 		errx(1, "%s: bad value", s);
   2557 #endif
   2558 }
   2559 
   2560 void
   2561 in6_getprefix(plen, which)
   2562 	const char *plen;
   2563 	int which;
   2564 {
   2565 	register struct sockaddr_in6 *sin = sin6tab[which];
   2566 	register u_char *cp;
   2567 	int len = strtol(plen, (char **)NULL, 10);
   2568 
   2569 	if ((len < 0) || (len > 128))
   2570 		errx(1, "%s: bad value", plen);
   2571 	sin->sin6_len = sizeof(*sin);
   2572 	if (which != MASK)
   2573 		sin->sin6_family = AF_INET6;
   2574 	if ((len == 0) || (len == 128)) {
   2575 		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
   2576 		return;
   2577 	}
   2578 	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
   2579 	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
   2580 		*cp++ = 0xff;
   2581 	if (len)
   2582 		*cp = 0xff << (8 - len);
   2583 }
   2584 
   2585 int
   2586 prefix(val, size)
   2587 	void *val;
   2588 	int size;
   2589 {
   2590 	register u_char *name = (u_char *)val;
   2591 	register int byte, bit, plen = 0;
   2592 
   2593 	for (byte = 0; byte < size; byte++, plen += 8)
   2594 		if (name[byte] != 0xff)
   2595 			break;
   2596 	if (byte == size)
   2597 		return (plen);
   2598 	for (bit = 7; bit != 0; bit--, plen++)
   2599 		if (!(name[byte] & (1 << bit)))
   2600 			break;
   2601 	for (; bit != 0; bit--)
   2602 		if (name[byte] & (1 << bit))
   2603 			return(0);
   2604 	byte++;
   2605 	for (; byte < size; byte++)
   2606 		if (name[byte])
   2607 			return(0);
   2608 	return (plen);
   2609 }
   2610 #endif /*INET6*/
   2611 
   2612 #ifndef INET_ONLY
   2613 void
   2614 at_getaddr(addr, which)
   2615 	const char *addr;
   2616 	int which;
   2617 {
   2618 	struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
   2619 	u_int net, node;
   2620 
   2621 	sat->sat_family = AF_APPLETALK;
   2622 	sat->sat_len = sizeof(*sat);
   2623 	if (which == MASK)
   2624 		errx(1, "AppleTalk does not use netmasks\n");
   2625 	if (sscanf(addr, "%u.%u", &net, &node) != 2
   2626 	    || net == 0 || net > 0xffff || node == 0 || node > 0xfe)
   2627 		errx(1, "%s: illegal address", addr);
   2628 	sat->sat_addr.s_net = htons(net);
   2629 	sat->sat_addr.s_node = node;
   2630 }
   2631 
   2632 void
   2633 setatrange(range, d)
   2634 	const char *range;
   2635 	int d;
   2636 {
   2637 	u_short	first = 123, last = 123;
   2638 
   2639 	if (sscanf(range, "%hu-%hu", &first, &last) != 2
   2640 	    || first == 0 || first > 0xffff
   2641 	    || last == 0 || last > 0xffff || first > last)
   2642 		errx(1, "%s: illegal net range: %u-%u", range, first, last);
   2643 	at_nr.nr_firstnet = htons(first);
   2644 	at_nr.nr_lastnet = htons(last);
   2645 }
   2646 
   2647 void
   2648 setatphase(phase, d)
   2649 	const char *phase;
   2650 	int d;
   2651 {
   2652 	if (!strcmp(phase, "1"))
   2653 		at_nr.nr_phase = 1;
   2654 	else if (!strcmp(phase, "2"))
   2655 		at_nr.nr_phase = 2;
   2656 	else
   2657 		errx(1, "%s: illegal phase", phase);
   2658 }
   2659 
   2660 void
   2661 checkatrange(sat)
   2662 	struct sockaddr_at *sat;
   2663 {
   2664 	if (at_nr.nr_phase == 0)
   2665 		at_nr.nr_phase = 2;	/* Default phase 2 */
   2666 	if (at_nr.nr_firstnet == 0)
   2667 		at_nr.nr_firstnet =	/* Default range of one */
   2668 		at_nr.nr_lastnet = sat->sat_addr.s_net;
   2669 	printf("\tatalk %d.%d range %d-%d phase %d\n",
   2670 	ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
   2671 	ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
   2672 	if ((u_short) ntohs(at_nr.nr_firstnet) >
   2673 			(u_short) ntohs(sat->sat_addr.s_net)
   2674 		    || (u_short) ntohs(at_nr.nr_lastnet) <
   2675 			(u_short) ntohs(sat->sat_addr.s_net))
   2676 		errx(1, "AppleTalk address is not in range");
   2677 	*((struct netrange *) &sat->sat_zero) = at_nr;
   2678 }
   2679 
   2680 #define SNS(x) ((struct sockaddr_ns *) &(x))
   2681 struct sockaddr_ns *snstab[] = {
   2682 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
   2683 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
   2684 
   2685 void
   2686 xns_getaddr(addr, which)
   2687 	const char *addr;
   2688 	int which;
   2689 {
   2690 	struct sockaddr_ns *sns = snstab[which];
   2691 
   2692 	sns->sns_family = AF_NS;
   2693 	sns->sns_len = sizeof(*sns);
   2694 	sns->sns_addr = ns_addr(addr);
   2695 	if (which == MASK)
   2696 		puts("Attempt to set XNS netmask will be ineffectual");
   2697 }
   2698 
   2699 #define SISO(x) ((struct sockaddr_iso *) &(x))
   2700 struct sockaddr_iso *sisotab[] = {
   2701 SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
   2702 SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
   2703 
   2704 void
   2705 iso_getaddr(addr, which)
   2706 	const char *addr;
   2707 	int which;
   2708 {
   2709 	struct sockaddr_iso *siso = sisotab[which];
   2710 	siso->siso_addr = *iso_addr(addr);
   2711 
   2712 	if (which == MASK) {
   2713 		siso->siso_len = TSEL(siso) - (caddr_t)(siso);
   2714 		siso->siso_nlen = 0;
   2715 	} else {
   2716 		siso->siso_len = sizeof(*siso);
   2717 		siso->siso_family = AF_ISO;
   2718 	}
   2719 }
   2720 
   2721 void
   2722 setsnpaoffset(val, d)
   2723 	const char *val;
   2724 	int d;
   2725 {
   2726 	iso_addreq.ifra_snpaoffset = atoi(val);
   2727 }
   2728 
   2729 void
   2730 setnsellength(val, d)
   2731 	const char *val;
   2732 	int d;
   2733 {
   2734 	nsellength = atoi(val);
   2735 	if (nsellength < 0)
   2736 		errx(1, "Negative NSEL length is absurd");
   2737 	if (afp == 0 || afp->af_af != AF_ISO)
   2738 		errx(1, "Setting NSEL length valid only for iso");
   2739 }
   2740 
   2741 void
   2742 fixnsel(s)
   2743 	struct sockaddr_iso *s;
   2744 {
   2745 	if (s->siso_family == 0)
   2746 		return;
   2747 	s->siso_tlen = nsellength;
   2748 }
   2749 
   2750 void
   2751 adjust_nsellength()
   2752 {
   2753 	fixnsel(sisotab[RIDADDR]);
   2754 	fixnsel(sisotab[ADDR]);
   2755 	fixnsel(sisotab[DSTADDR]);
   2756 }
   2757 
   2758 #endif	/* INET_ONLY */
   2759 
   2760 void
   2761 usage()
   2762 {
   2763 	const char *progname = getprogname();
   2764 
   2765 	fprintf(stderr,
   2766 	    "usage: %s [ -m ] "
   2767 #ifdef INET6
   2768 		"[ -L ] "
   2769 #endif
   2770 		"interface\n"
   2771 		"\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
   2772 		"\t\t[ alias | -alias ] ]\n"
   2773 		"\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
   2774 		"\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
   2775 		"\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
   2776 		"\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
   2777 		"\t[ arp | -arp ]\n"
   2778 		"\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
   2779 		"[ instance minst ]\n"
   2780 		"\t[ vlan n vlanif i ]\n"
   2781 		"\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
   2782 		"\t[ tentative | -tentative ] [ pltime n ] [ vltime n ]\n"
   2783 		"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
   2784 		"       %s -a [ -m ] [ -d ] [ -u ] [ af ]\n"
   2785 		"       %s -l [ -b ] [ -d ] [ -u ] [ -s ]\n"
   2786 		"       %s -C\n"
   2787 		"       %s interface create\n"
   2788 		"       %s interface destroy\n",
   2789 		progname, progname, progname, progname, progname, progname);
   2790 	exit(1);
   2791 }
   2792 
   2793 #ifdef INET6
   2794 char *
   2795 sec2str(total)
   2796 	time_t total;
   2797 {
   2798 	static char result[256];
   2799 	int days, hours, mins, secs;
   2800 	int first = 1;
   2801 	char *p = result;
   2802 
   2803 	if (0) {	/*XXX*/
   2804 		days = total / 3600 / 24;
   2805 		hours = (total / 3600) % 24;
   2806 		mins = (total / 60) % 60;
   2807 		secs = total % 60;
   2808 
   2809 		if (days) {
   2810 			first = 0;
   2811 			p += sprintf(p, "%dd", days);
   2812 		}
   2813 		if (!first || hours) {
   2814 			first = 0;
   2815 			p += sprintf(p, "%dh", hours);
   2816 		}
   2817 		if (!first || mins) {
   2818 			first = 0;
   2819 			p += sprintf(p, "%dm", mins);
   2820 		}
   2821 		sprintf(p, "%ds", secs);
   2822 	} else
   2823 		sprintf(p, "%lu", (u_long)total);
   2824 
   2825 	return(result);
   2826 }
   2827 #endif
   2828