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