Home | History | Annotate | Line # | Download | only in ifconfig
ifconfig.c revision 1.102
      1 /*	$NetBSD: ifconfig.c,v 1.102 2001/03/01 08:34:10 itojun Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1983, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 #ifndef lint
     75 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     76 	The Regents of the University of California.  All rights reserved.\n");
     77 #endif /* not lint */
     78 
     79 #ifndef lint
     80 #if 0
     81 static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
     82 #else
     83 __RCSID("$NetBSD: ifconfig.c,v 1.102 2001/03/01 08:34:10 itojun Exp $");
     84 #endif
     85 #endif /* not lint */
     86 
     87 #include <sys/param.h>
     88 #include <sys/socket.h>
     89 #include <sys/ioctl.h>
     90 
     91 #include <net/if.h>
     92 #include <net/if_dl.h>
     93 #include <net/if_media.h>
     94 #include <net/if_ether.h>
     95 #include <net/if_ieee80211.h>
     96 #include <net/if_vlanvar.h>
     97 #include <netinet/in.h>
     98 #include <netinet/in_var.h>
     99 #ifdef INET6
    100 #include <netinet6/nd6.h>
    101 #endif
    102 #include <arpa/inet.h>
    103 
    104 #include <netatalk/at.h>
    105 
    106 #define	NSIP
    107 #include <netns/ns.h>
    108 #include <netns/ns_if.h>
    109 #include <netdb.h>
    110 
    111 #define EON
    112 #include <netiso/iso.h>
    113 #include <netiso/iso_var.h>
    114 #include <sys/protosw.h>
    115 
    116 #include <ctype.h>
    117 #include <err.h>
    118 #include <errno.h>
    119 #include <stddef.h>
    120 #include <stdio.h>
    121 #include <stdlib.h>
    122 #include <string.h>
    123 #include <unistd.h>
    124 #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 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
    948 
    949 	if ((ecode = getaddrinfo(src, NULL, &hints, &srcres)) != 0)
    950 		errx(1, "error in parsing address string: %s",
    951 		    gai_strerror(ecode));
    952 
    953 	if ((ecode = getaddrinfo(dst, NULL, &hints, &dstres)) != 0)
    954 		errx(1, "error in parsing address string: %s",
    955 		    gai_strerror(ecode));
    956 
    957 	if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
    958 		errx(1,
    959 		    "source and destination address families do not match");
    960 
    961 	if (srcres->ai_addrlen > sizeof(req.addr) ||
    962 	    dstres->ai_addrlen > sizeof(req.dstaddr))
    963 		errx(1, "invalid sockaddr");
    964 
    965 	memset(&req, 0, sizeof(req));
    966 	strncpy(req.iflr_name, name, sizeof(req.iflr_name));
    967 	memcpy(&req.addr, srcres->ai_addr, srcres->ai_addrlen);
    968 	memcpy(&req.dstaddr, dstres->ai_addr, dstres->ai_addrlen);
    969 	if (ioctl(s, SIOCSLIFPHYADDR, &req) < 0)
    970 		warn("SIOCSLIFPHYADDR");
    971 
    972 	freeaddrinfo(srcres);
    973 	freeaddrinfo(dstres);
    974 }
    975 
    976 /* ARGSUSED */
    977 void
    978 deletetunnel(vname, param)
    979 	const char *vname;
    980 	int param;
    981 {
    982 
    983 	if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
    984 		err(1, "SIOCDIFPHYADDR");
    985 }
    986 
    987 void setvlan(val, d)
    988 	const char *val;
    989 	int d;
    990 {
    991 	struct vlanreq vlr;
    992 
    993 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
    994 	    !isdigit(ifr.ifr_name[4]))
    995 		errx(EXIT_FAILURE,
    996 		    "``vlan'' valid only with vlan(4) interfaces");
    997 
    998 	vlan_tag = atoi(val);
    999 
   1000 	memset(&vlr, 0, sizeof(vlr));
   1001 	ifr.ifr_data = (caddr_t)&vlr;
   1002 
   1003 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
   1004 		err(EXIT_FAILURE, "SIOCGETVLAN");
   1005 
   1006 	vlr.vlr_tag = vlan_tag;
   1007 
   1008 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
   1009 		err(EXIT_FAILURE, "SIOCSETVLAN");
   1010 }
   1011 
   1012 void setvlanif(val, d)
   1013 	const char *val;
   1014 	int d;
   1015 {
   1016 	struct vlanreq vlr;
   1017 
   1018 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
   1019 	    !isdigit(ifr.ifr_name[4]))
   1020 		errx(EXIT_FAILURE,
   1021 		    "``vlanif'' valid only with vlan(4) interfaces");
   1022 
   1023 	if (vlan_tag == (u_int)-1)
   1024 		errx(EXIT_FAILURE,
   1025 		    "must specify both ``vlan'' and ``vlanif''");
   1026 
   1027 	memset(&vlr, 0, sizeof(vlr));
   1028 	ifr.ifr_data = (caddr_t)&vlr;
   1029 
   1030 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
   1031 		err(EXIT_FAILURE, "SIOCGETVLAN");
   1032 
   1033 	strlcpy(vlr.vlr_parent, val, sizeof(vlr.vlr_parent));
   1034 	vlr.vlr_tag = vlan_tag;
   1035 
   1036 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
   1037 		err(EXIT_FAILURE, "SIOCSETVLAN");
   1038 }
   1039 
   1040 void unsetvlanif(val, d)
   1041 	const char *val;
   1042 	int d;
   1043 {
   1044 	struct vlanreq vlr;
   1045 
   1046 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
   1047 	    !isdigit(ifr.ifr_name[4]))
   1048 		errx(EXIT_FAILURE,
   1049 		    "``vlanif'' valid only with vlan(4) interfaces");
   1050 
   1051 	memset(&vlr, 0, sizeof(vlr));
   1052 	ifr.ifr_data = (caddr_t)&vlr;
   1053 
   1054 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
   1055 		err(EXIT_FAILURE, "SIOCGETVLAN");
   1056 
   1057 	vlr.vlr_parent[0] = '\0';
   1058 	vlr.vlr_tag = 0;
   1059 
   1060 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
   1061 		err(EXIT_FAILURE, "SIOCSETVLAN");
   1062 }
   1063 
   1064 void
   1065 setifnetmask(addr, d)
   1066 	const char *addr;
   1067 	int d;
   1068 {
   1069 	(*afp->af_getaddr)(addr, MASK);
   1070 }
   1071 
   1072 void
   1073 setifbroadaddr(addr, d)
   1074 	const char *addr;
   1075 	int d;
   1076 {
   1077 	(*afp->af_getaddr)(addr, DSTADDR);
   1078 }
   1079 
   1080 void
   1081 setifipdst(addr, d)
   1082 	const char *addr;
   1083 	int d;
   1084 {
   1085 	in_getaddr(addr, DSTADDR);
   1086 	setipdst++;
   1087 	clearaddr = 0;
   1088 	newaddr = 0;
   1089 }
   1090 
   1091 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
   1092 /*ARGSUSED*/
   1093 void
   1094 notealias(addr, param)
   1095 	const char *addr;
   1096 	int param;
   1097 {
   1098 	if (setaddr && doalias == 0 && param < 0)
   1099 		(void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
   1100 		    rqtosa(af_addreq)->sa_len);
   1101 	doalias = param;
   1102 	if (param < 0) {
   1103 		clearaddr = 1;
   1104 		newaddr = 0;
   1105 	} else
   1106 		clearaddr = 0;
   1107 }
   1108 
   1109 /*ARGSUSED*/
   1110 void
   1111 notrailers(vname, value)
   1112 	const char *vname;
   1113 	int value;
   1114 {
   1115 	puts("Note: trailers are no longer sent, but always received");
   1116 }
   1117 
   1118 /*ARGSUSED*/
   1119 void
   1120 setifdstaddr(addr, param)
   1121 	const char *addr;
   1122 	int param;
   1123 {
   1124 	(*afp->af_getaddr)(addr, DSTADDR);
   1125 }
   1126 
   1127 void
   1128 setifflags(vname, value)
   1129 	const char *vname;
   1130 	int value;
   1131 {
   1132 	struct ifreq ifreq;
   1133 
   1134 	(void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
   1135  	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifreq) < 0)
   1136 		err(1, "SIOCGIFFLAGS");
   1137  	flags = ifreq.ifr_flags;
   1138 
   1139 	if (value < 0) {
   1140 		value = -value;
   1141 		flags &= ~value;
   1142 	} else
   1143 		flags |= value;
   1144 	ifreq.ifr_flags = flags;
   1145 	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) < 0)
   1146 		err(1, "SIOCSIFFLAGS");
   1147 
   1148 	reset_if_flags = 1;
   1149 }
   1150 
   1151 #ifdef INET6
   1152 void
   1153 setia6flags(vname, value)
   1154 	const char *vname;
   1155 	int value;
   1156 {
   1157 	if (value < 0) {
   1158 		value = -value;
   1159 		in6_addreq.ifra_flags &= ~value;
   1160 	} else
   1161 		in6_addreq.ifra_flags |= value;
   1162 }
   1163 
   1164 void
   1165 setia6pltime(val, d)
   1166 	const char *val;
   1167 	int d;
   1168 {
   1169 	setia6lifetime("pltime", val);
   1170 }
   1171 
   1172 void
   1173 setia6vltime(val, d)
   1174 	const char *val;
   1175 	int d;
   1176 {
   1177 	setia6lifetime("vltime", val);
   1178 }
   1179 
   1180 void
   1181 setia6lifetime(cmd, val)
   1182 	const char *cmd;
   1183 	const char *val;
   1184 {
   1185 	time_t newval, t;
   1186 	char *ep;
   1187 
   1188 	t = time(NULL);
   1189 	newval = (time_t)strtoul(val, &ep, 0);
   1190 	if (val == ep)
   1191 		errx(1, "invalid %s", cmd);
   1192 	if (afp->af_af != AF_INET6)
   1193 		errx(1, "%s not allowed for the AF", cmd);
   1194 	if (strcmp(cmd, "vltime") == 0) {
   1195 		in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
   1196 		in6_addreq.ifra_lifetime.ia6t_vltime = newval;
   1197 	} else if (strcmp(cmd, "pltime") == 0) {
   1198 		in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
   1199 		in6_addreq.ifra_lifetime.ia6t_pltime = newval;
   1200 	}
   1201 }
   1202 #endif
   1203 
   1204 void
   1205 setifmetric(val, d)
   1206 	const char *val;
   1207 	int d;
   1208 {
   1209 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1210 	ifr.ifr_metric = atoi(val);
   1211 	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
   1212 		warn("SIOCSIFMETRIC");
   1213 }
   1214 
   1215 void
   1216 setifmtu(val, d)
   1217 	const char *val;
   1218 	int d;
   1219 {
   1220 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1221 	ifr.ifr_mtu = atoi(val);
   1222 	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
   1223 		warn("SIOCSIFMTU");
   1224 }
   1225 
   1226 const char *
   1227 get_string(val, sep, buf, lenp)
   1228 	const char *val, *sep;
   1229 	u_int8_t *buf;
   1230 	int *lenp;
   1231 {
   1232 	int len;
   1233 	int hexstr;
   1234 	u_int8_t *p;
   1235 
   1236 	len = *lenp;
   1237 	p = buf;
   1238 	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
   1239 	if (hexstr)
   1240 		val += 2;
   1241 	for (;;) {
   1242 		if (*val == '\0')
   1243 			break;
   1244 		if (sep != NULL && strchr(sep, *val) != NULL) {
   1245 			val++;
   1246 			break;
   1247 		}
   1248 		if (hexstr) {
   1249 			if (!isxdigit((u_char)val[0]) ||
   1250 			    !isxdigit((u_char)val[1])) {
   1251 				warnx("bad hexadecimal digits");
   1252 				return NULL;
   1253 			}
   1254 		}
   1255 		if (p > buf + len) {
   1256 			if (hexstr)
   1257 				warnx("hexadecimal digits too long");
   1258 			else
   1259 				warnx("strings too long");
   1260 			return NULL;
   1261 		}
   1262 		if (hexstr) {
   1263 #define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
   1264 			*p++ = (tohex((u_char)val[0]) << 4) |
   1265 			    tohex((u_char)val[1]);
   1266 #undef tohex
   1267 			val += 2;
   1268 		} else
   1269 			*p++ = *val++;
   1270 	}
   1271 	len = p - buf;
   1272 	if (len < *lenp)
   1273 		memset(p, 0, *lenp - len);
   1274 	*lenp = len;
   1275 	return val;
   1276 }
   1277 
   1278 void
   1279 print_string(buf, len)
   1280 	const u_int8_t *buf;
   1281 	int len;
   1282 {
   1283 	int i;
   1284 	int hasspc;
   1285 
   1286 	i = 0;
   1287 	hasspc = 0;
   1288 	if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
   1289 		for (; i < len; i++) {
   1290 			if (!isprint(buf[i]))
   1291 				break;
   1292 			if (isspace(buf[i]))
   1293 				hasspc++;
   1294 		}
   1295 	}
   1296 	if (i == len) {
   1297 		if (hasspc || len == 0)
   1298 			printf("\"%.*s\"", len, buf);
   1299 		else
   1300 			printf("%.*s", len, buf);
   1301 	} else {
   1302 		printf("0x");
   1303 		for (i = 0; i < len; i++)
   1304 			printf("%02x", buf[i]);
   1305 	}
   1306 }
   1307 
   1308 void
   1309 setifnwid(val, d)
   1310 	const char *val;
   1311 	int d;
   1312 {
   1313 	struct ieee80211_nwid nwid;
   1314 	int len;
   1315 
   1316 	len = sizeof(nwid.i_nwid);
   1317 	if (get_string(val, NULL, nwid.i_nwid, &len) == NULL)
   1318 		return;
   1319 	nwid.i_len = len;
   1320 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1321 	ifr.ifr_data = (caddr_t)&nwid;
   1322 	if (ioctl(s, SIOCS80211NWID, (caddr_t)&ifr) < 0)
   1323 		warn("SIOCS80211NWID");
   1324 }
   1325 
   1326 void
   1327 setifnwkey(val, d)
   1328 	const char *val;
   1329 	int d;
   1330 {
   1331 	struct ieee80211_nwkey nwkey;
   1332 	int i;
   1333 	u_int8_t keybuf[IEEE80211_WEP_NKID][16];
   1334 
   1335 	nwkey.i_wepon = 1;
   1336 	nwkey.i_defkid = 1;
   1337 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1338 		nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
   1339 		nwkey.i_key[i].i_keydat = keybuf[i];
   1340 	}
   1341 	if (d != 0) {
   1342 		/* disable WEP encryption */
   1343 		nwkey.i_wepon = 0;
   1344 		i = 0;
   1345 	} else if (isdigit(val[0]) && val[1] == ':') {
   1346 		/* specifying a full set of four keys */
   1347 		nwkey.i_defkid = val[0] - '0';
   1348 		val += 2;
   1349 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1350 			val = get_string(val, ",", keybuf[i],
   1351 			    &nwkey.i_key[i].i_keylen);
   1352 			if (val == NULL)
   1353 				return;
   1354 		}
   1355 		if (*val != '\0') {
   1356 			warnx("SIOCS80211NWKEY: too many keys.");
   1357 			return;
   1358 		}
   1359 	} else {
   1360 		val = get_string(val, NULL, keybuf[0],
   1361 		    &nwkey.i_key[0].i_keylen);
   1362 		if (val == NULL)
   1363 			return;
   1364 		i = 1;
   1365 	}
   1366 	for (; i < IEEE80211_WEP_NKID; i++)
   1367 		nwkey.i_key[i].i_keylen = 0;
   1368 	(void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
   1369 	if (ioctl(s, SIOCS80211NWKEY, (caddr_t)&nwkey) < 0)
   1370 		warn("SIOCS80211NWKEY");
   1371 }
   1372 
   1373 void
   1374 setifpowersave(val, d)
   1375 	const char *val;
   1376 	int d;
   1377 {
   1378 	struct ieee80211_power power;
   1379 
   1380 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
   1381 	if (ioctl(s, SIOCG80211POWER, (caddr_t)&power) < 0) {
   1382 		warn("SIOCG80211POWER");
   1383 		return;
   1384 	}
   1385 
   1386 	power.i_enabled = d;
   1387 	if (ioctl(s, SIOCS80211POWER, (caddr_t)&power) < 0)
   1388 		warn("SIOCS80211POWER");
   1389 }
   1390 
   1391 void
   1392 setifpowersavesleep(val, d)
   1393 	const char *val;
   1394 	int d;
   1395 {
   1396 	struct ieee80211_power power;
   1397 
   1398 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
   1399 	if (ioctl(s, SIOCG80211POWER, (caddr_t)&power) < 0) {
   1400 		warn("SIOCG80211POWER");
   1401 		return;
   1402 	}
   1403 
   1404 	power.i_maxsleep = atoi(val);
   1405 	if (ioctl(s, SIOCS80211POWER, (caddr_t)&power) < 0)
   1406 		warn("SIOCS80211POWER");
   1407 }
   1408 
   1409 void
   1410 ieee80211_status()
   1411 {
   1412 	int i;
   1413 	struct ieee80211_nwid nwid;
   1414 	struct ieee80211_nwkey nwkey;
   1415 	struct ieee80211_power power;
   1416 	u_int8_t keybuf[IEEE80211_WEP_NKID][16];
   1417 
   1418 	memset(&ifr, 0, sizeof(ifr));
   1419 	ifr.ifr_data = (caddr_t)&nwid;
   1420 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1421 	if (ioctl(s, SIOCG80211NWID, (caddr_t)&ifr) != 0)
   1422 		return;
   1423 	if (nwid.i_len > IEEE80211_NWID_LEN) {
   1424 		warnx("SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
   1425 		return;
   1426 	}
   1427 	printf("\tnwid ");
   1428 	print_string(nwid.i_nwid, nwid.i_len);
   1429 	memset(&nwkey, 0, sizeof(nwkey));
   1430 	(void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
   1431 	/* show nwkey only when WEP is enabled */
   1432 	if (ioctl(s, SIOCG80211NWKEY, (caddr_t)&nwkey) != 0 ||
   1433 	    nwkey.i_wepon == 0) {
   1434 		printf("\n");
   1435 		goto skip_wep;
   1436 	}
   1437 
   1438 	printf(" nwkey ");
   1439 	/* try to retrieve WEP keys */
   1440 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1441 		nwkey.i_key[i].i_keydat = keybuf[i];
   1442 		nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
   1443 	}
   1444 	if (ioctl(s, SIOCG80211NWKEY, (caddr_t)&nwkey) != 0) {
   1445 		printf("*****");
   1446 	} else {
   1447 		if (nwkey.i_defkid != 1) {
   1448 			/* non default key or multiple keys defined */
   1449 			i = 0;
   1450 		} else if (nwkey.i_key[0].i_keylen >= 2 &&
   1451 		    isdigit(nwkey.i_key[0].i_keydat[0]) &&
   1452 		    nwkey.i_key[0].i_keydat[1] == ':') {
   1453 			/* ambiguous */
   1454 			i = 0;
   1455 		} else {
   1456 			for (i = 1; i < IEEE80211_WEP_NKID; i++) {
   1457 				if (nwkey.i_key[i].i_keylen != 0)
   1458 					break;
   1459 			}
   1460 		}
   1461 		if (i == IEEE80211_WEP_NKID) {
   1462 			/* only show the first key */
   1463 			print_string(nwkey.i_key[0].i_keydat,
   1464 			    nwkey.i_key[0].i_keylen);
   1465 		} else {
   1466 			printf("%d:", nwkey.i_defkid);
   1467 			for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1468 				if (i > 0)
   1469 					printf(",");
   1470 				print_string(nwkey.i_key[i].i_keydat,
   1471 				    nwkey.i_key[i].i_keylen);
   1472 			}
   1473 		}
   1474 	}
   1475 	printf("\n");
   1476 
   1477  skip_wep:
   1478 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
   1479 	if (ioctl(s, SIOCG80211POWER, &power) != 0)
   1480 		return;
   1481 	printf("\tpowersave ");
   1482 	if (power.i_enabled)
   1483 		printf("on (%dms sleep)", power.i_maxsleep);
   1484 	else
   1485 		printf("off");
   1486 	printf("\n");
   1487 }
   1488 
   1489 void
   1490 init_current_media()
   1491 {
   1492 	struct ifmediareq ifmr;
   1493 
   1494 	/*
   1495 	 * If we have not yet done so, grab the currently-selected
   1496 	 * media.
   1497 	 */
   1498 	if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
   1499 		(void) memset(&ifmr, 0, sizeof(ifmr));
   1500 		(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1501 
   1502 		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1503 			/*
   1504 			 * If we get E2BIG, the kernel is telling us
   1505 			 * that there are more, so we can ignore it.
   1506 			 */
   1507 			if (errno != E2BIG)
   1508 				err(1, "SGIOCGIFMEDIA");
   1509 		}
   1510 
   1511 		media_current = ifmr.ifm_current;
   1512 	}
   1513 
   1514 	/* Sanity. */
   1515 	if (IFM_TYPE(media_current) == 0)
   1516 		errx(1, "%s: no link type?", name);
   1517 }
   1518 
   1519 void
   1520 process_media_commands()
   1521 {
   1522 
   1523 	if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
   1524 		/* Nothing to do. */
   1525 		return;
   1526 	}
   1527 
   1528 	/*
   1529 	 * Media already set up, and commands sanity-checked.  Set/clear
   1530 	 * any options, and we're ready to go.
   1531 	 */
   1532 	media_current |= mediaopt_set;
   1533 	media_current &= ~mediaopt_clear;
   1534 
   1535 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1536 	ifr.ifr_media = media_current;
   1537 
   1538 	if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
   1539 		err(1, "SIOCSIFMEDIA");
   1540 }
   1541 
   1542 void
   1543 setmedia(val, d)
   1544 	const char *val;
   1545 	int d;
   1546 {
   1547 	int type, subtype, inst;
   1548 
   1549 	init_current_media();
   1550 
   1551 	/* Only one media command may be given. */
   1552 	if (actions & A_MEDIA)
   1553 		errx(1, "only one `media' command may be issued");
   1554 
   1555 	/* Must not come after mediaopt commands */
   1556 	if (actions & A_MEDIAOPT)
   1557 		errx(1, "may not issue `media' after `mediaopt' commands");
   1558 
   1559 	/*
   1560 	 * No need to check if `instance' has been issued; setmediainst()
   1561 	 * craps out if `media' has not been specified.
   1562 	 */
   1563 
   1564 	type = IFM_TYPE(media_current);
   1565 	inst = IFM_INST(media_current);
   1566 
   1567 	/* Look up the subtype. */
   1568 	subtype = get_media_subtype(type, val);
   1569 
   1570 	/* Build the new current media word. */
   1571 	media_current = IFM_MAKEWORD(type, subtype, 0, inst);
   1572 
   1573 	/* Media will be set after other processing is complete. */
   1574 }
   1575 
   1576 void
   1577 setmediaopt(val, d)
   1578 	const char *val;
   1579 	int d;
   1580 {
   1581 
   1582 	init_current_media();
   1583 
   1584 	/* Can only issue `mediaopt' once. */
   1585 	if (actions & A_MEDIAOPTSET)
   1586 		errx(1, "only one `mediaopt' command may be issued");
   1587 
   1588 	/* Can't issue `mediaopt' if `instance' has already been issued. */
   1589 	if (actions & A_MEDIAINST)
   1590 		errx(1, "may not issue `mediaopt' after `instance'");
   1591 
   1592 	mediaopt_set = get_media_options(IFM_TYPE(media_current), val);
   1593 
   1594 	/* Media will be set after other processing is complete. */
   1595 }
   1596 
   1597 void
   1598 unsetmediaopt(val, d)
   1599 	const char *val;
   1600 	int d;
   1601 {
   1602 
   1603 	init_current_media();
   1604 
   1605 	/* Can only issue `-mediaopt' once. */
   1606 	if (actions & A_MEDIAOPTCLR)
   1607 		errx(1, "only one `-mediaopt' command may be issued");
   1608 
   1609 	/* May not issue `media' and `-mediaopt'. */
   1610 	if (actions & A_MEDIA)
   1611 		errx(1, "may not issue both `media' and `-mediaopt'");
   1612 
   1613 	/*
   1614 	 * No need to check for A_MEDIAINST, since the test for A_MEDIA
   1615 	 * implicitly checks for A_MEDIAINST.
   1616 	 */
   1617 
   1618 	mediaopt_clear = get_media_options(IFM_TYPE(media_current), val);
   1619 
   1620 	/* Media will be set after other processing is complete. */
   1621 }
   1622 
   1623 void
   1624 setmediainst(val, d)
   1625 	const char *val;
   1626 	int d;
   1627 {
   1628 	int type, subtype, options, inst;
   1629 
   1630 	init_current_media();
   1631 
   1632 	/* Can only issue `instance' once. */
   1633 	if (actions & A_MEDIAINST)
   1634 		errx(1, "only one `instance' command may be issued");
   1635 
   1636 	/* Must have already specified `media' */
   1637 	if ((actions & A_MEDIA) == 0)
   1638 		errx(1, "must specify `media' before `instance'");
   1639 
   1640 	type = IFM_TYPE(media_current);
   1641 	subtype = IFM_SUBTYPE(media_current);
   1642 	options = IFM_OPTIONS(media_current);
   1643 
   1644 	inst = atoi(val);
   1645 	if (inst < 0 || inst > IFM_INST_MAX)
   1646 		errx(1, "invalid media instance: %s", val);
   1647 
   1648 	media_current = IFM_MAKEWORD(type, subtype, options, inst);
   1649 
   1650 	/* Media will be set after other processing is complete. */
   1651 }
   1652 
   1653 struct ifmedia_description ifm_type_descriptions[] =
   1654     IFM_TYPE_DESCRIPTIONS;
   1655 
   1656 struct ifmedia_description ifm_subtype_descriptions[] =
   1657     IFM_SUBTYPE_DESCRIPTIONS;
   1658 
   1659 struct ifmedia_description ifm_option_descriptions[] =
   1660     IFM_OPTION_DESCRIPTIONS;
   1661 
   1662 const char *
   1663 get_media_type_string(mword)
   1664 	int mword;
   1665 {
   1666 	struct ifmedia_description *desc;
   1667 
   1668 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
   1669 	     desc++) {
   1670 		if (IFM_TYPE(mword) == desc->ifmt_word)
   1671 			return (desc->ifmt_string);
   1672 	}
   1673 	return ("<unknown type>");
   1674 }
   1675 
   1676 const char *
   1677 get_media_subtype_string(mword)
   1678 	int mword;
   1679 {
   1680 	struct ifmedia_description *desc;
   1681 
   1682 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
   1683 	     desc++) {
   1684 		if (IFM_TYPE_MATCH(desc->ifmt_word, mword) &&
   1685 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword))
   1686 			return (desc->ifmt_string);
   1687 	}
   1688 	return ("<unknown subtype>");
   1689 }
   1690 
   1691 int
   1692 get_media_subtype(type, val)
   1693 	int type;
   1694 	const char *val;
   1695 {
   1696 	int rval;
   1697 
   1698 	rval = lookup_media_word(ifm_subtype_descriptions, type, val);
   1699 	if (rval == -1)
   1700 		errx(1, "unknown %s media subtype: %s",
   1701 		    get_media_type_string(type), val);
   1702 
   1703 	return (rval);
   1704 }
   1705 
   1706 int
   1707 get_media_options(type, val)
   1708 	int type;
   1709 	const char *val;
   1710 {
   1711 	char *optlist, *str;
   1712 	int option, rval = 0;
   1713 
   1714 	/* We muck with the string, so copy it. */
   1715 	optlist = strdup(val);
   1716 	if (optlist == NULL)
   1717 		err(1, "strdup");
   1718 	str = optlist;
   1719 
   1720 	/*
   1721 	 * Look up the options in the user-provided comma-separated list.
   1722 	 */
   1723 	for (; (str = strtok(str, ",")) != NULL; str = NULL) {
   1724 		option = lookup_media_word(ifm_option_descriptions, type, str);
   1725 		if (option == -1)
   1726 			errx(1, "unknown %s media option: %s",
   1727 			    get_media_type_string(type), str);
   1728 		rval |= IFM_OPTIONS(option);
   1729 	}
   1730 
   1731 	free(optlist);
   1732 	return (rval);
   1733 }
   1734 
   1735 int
   1736 lookup_media_word(desc, type, val)
   1737 	struct ifmedia_description *desc;
   1738 	int type;
   1739 	const char *val;
   1740 {
   1741 
   1742 	for (; desc->ifmt_string != NULL; desc++) {
   1743 		if (IFM_TYPE_MATCH(desc->ifmt_word, type) &&
   1744 		    strcasecmp(desc->ifmt_string, val) == 0)
   1745 			return (desc->ifmt_word);
   1746 	}
   1747 	return (-1);
   1748 }
   1749 
   1750 void
   1751 print_media_word(ifmw, print_type, as_syntax)
   1752 	int ifmw, print_type, as_syntax;
   1753 {
   1754 	struct ifmedia_description *desc;
   1755 	int seen_option = 0;
   1756 
   1757 	if (print_type)
   1758 		printf("%s ", get_media_type_string(ifmw));
   1759 	printf("%s%s", as_syntax ? "media " : "",
   1760 	    get_media_subtype_string(ifmw));
   1761 
   1762 	/* Find options. */
   1763 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
   1764 	     desc++) {
   1765 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
   1766 		    (ifmw & IFM_OPTIONS(desc->ifmt_word)) != 0 &&
   1767 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
   1768 			if (seen_option == 0)
   1769 				printf(" %s", as_syntax ? "mediaopt " : "");
   1770 			printf("%s%s", seen_option ? "," : "",
   1771 			    desc->ifmt_string);
   1772 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
   1773 		}
   1774 	}
   1775 	if (IFM_INST(ifmw) != 0)
   1776 		printf(" instance %d", IFM_INST(ifmw));
   1777 }
   1778 
   1779 int carrier()
   1780 {
   1781 	struct ifmediareq ifmr;
   1782 
   1783 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1784 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1785 
   1786 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1787 		/*
   1788 		 * Interface doesn't support SIOC{G,S}IFMEDIA;
   1789 		 * assume ok.
   1790 		 */
   1791 		return 0;
   1792 	}
   1793 	if ((ifmr.ifm_status & IFM_AVALID) == 0) {
   1794 		/*
   1795 		 * Interface doesn't report media-valid status.
   1796 		 * assume ok.
   1797 		 */
   1798 		return 0;
   1799 	}
   1800 	/* otherwise, return ok for active, not-ok if not active. */
   1801 	return !(ifmr.ifm_status & IFM_ACTIVE);
   1802 }
   1803 
   1804 
   1805 #define	IFFBITS \
   1806 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
   1807 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
   1808 
   1809 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
   1810 
   1811 const struct ifmedia_status_description ifm_status_descriptions[] =
   1812     IFM_STATUS_DESCRIPTIONS;
   1813 
   1814 /*
   1815  * Print the status of the interface.  If an address family was
   1816  * specified, show it and it only; otherwise, show them all.
   1817  */
   1818 void
   1819 status(ap, alen)
   1820 	const u_int8_t *ap;
   1821 	int alen;
   1822 {
   1823 	struct afswtch *p = afp;
   1824 	struct ifmediareq ifmr;
   1825 	int *media_list, i;
   1826 
   1827 	printf("%s: ", name);
   1828 	printb("flags", flags, IFFBITS);
   1829 	if (metric)
   1830 		printf(" metric %d", metric);
   1831 	if (mtu)
   1832 		printf(" mtu %d", mtu);
   1833 	putchar('\n');
   1834 
   1835 	ieee80211_status();
   1836 	vlan_status();
   1837 	tunnel_status();
   1838 
   1839 	if (ap && alen > 0) {
   1840 		printf("\taddress:");
   1841 		for (i = 0; i < alen; i++, ap++)
   1842 			printf("%c%02x", i > 0 ? ':' : ' ', *ap);
   1843 		putchar('\n');
   1844 	}
   1845 
   1846 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1847 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1848 
   1849 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1850 		/*
   1851 		 * Interface doesn't support SIOC{G,S}IFMEDIA.
   1852 		 */
   1853 		goto proto_status;
   1854 	}
   1855 
   1856 	if (ifmr.ifm_count == 0) {
   1857 		warnx("%s: no media types?", name);
   1858 		goto proto_status;
   1859 	}
   1860 
   1861 	media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
   1862 	if (media_list == NULL)
   1863 		err(1, "malloc");
   1864 	ifmr.ifm_ulist = media_list;
   1865 
   1866 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
   1867 		err(1, "SIOCGIFMEDIA");
   1868 
   1869 	printf("\tmedia: ");
   1870 	print_media_word(ifmr.ifm_current, 1, 0);
   1871 	if (ifmr.ifm_active != ifmr.ifm_current) {
   1872 		putchar(' ');
   1873 		putchar('(');
   1874 		print_media_word(ifmr.ifm_active, 0, 0);
   1875 		putchar(')');
   1876 	}
   1877 	putchar('\n');
   1878 
   1879 	if (ifmr.ifm_status & IFM_STATUS_VALID) {
   1880 		const struct ifmedia_status_description *ifms;
   1881 		int bitno, found = 0;
   1882 
   1883 		printf("\tstatus: ");
   1884 		for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
   1885 			for (ifms = ifm_status_descriptions;
   1886 			     ifms->ifms_valid != 0; ifms++) {
   1887 				if (ifms->ifms_type !=
   1888 				      IFM_TYPE(ifmr.ifm_current) ||
   1889 				    ifms->ifms_valid !=
   1890 				      ifm_status_valid_list[bitno])
   1891 					continue;
   1892 				printf("%s%s", found ? ", " : "",
   1893 				    IFM_STATUS_DESC(ifms, ifmr.ifm_status));
   1894 				found = 1;
   1895 
   1896 				/*
   1897 				 * For each valid indicator bit, there's
   1898 				 * only one entry for each media type, so
   1899 				 * terminate the inner loop now.
   1900 				 */
   1901 				break;
   1902 			}
   1903 		}
   1904 
   1905 		if (found == 0)
   1906 			printf("unknown");
   1907 		putchar('\n');
   1908 	}
   1909 
   1910 	if (mflag) {
   1911 		int type, printed_type;
   1912 
   1913 		for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
   1914 			for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
   1915 				if (IFM_TYPE(media_list[i]) == type) {
   1916 					if (printed_type == 0) {
   1917 					    printf("\tsupported %s media:\n",
   1918 					      get_media_type_string(type));
   1919 					    printed_type = 1;
   1920 					}
   1921 					printf("\t\t");
   1922 					print_media_word(media_list[i], 0, 1);
   1923 					printf("\n");
   1924 				}
   1925 			}
   1926 		}
   1927 	}
   1928 
   1929 	free(media_list);
   1930 
   1931  proto_status:
   1932 	if ((p = afp) != NULL) {
   1933 		(*p->af_status)(1);
   1934 	} else for (p = afs; p->af_name; p++) {
   1935 		ifr.ifr_addr.sa_family = p->af_af;
   1936 		(*p->af_status)(0);
   1937 	}
   1938 }
   1939 
   1940 void
   1941 tunnel_status()
   1942 {
   1943 	char psrcaddr[NI_MAXHOST];
   1944 	char pdstaddr[NI_MAXHOST];
   1945 	const char *ver = "";
   1946 #ifdef NI_WITHSCOPEID
   1947 	const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
   1948 #else
   1949 	const int niflag = NI_NUMERICHOST;
   1950 #endif
   1951 	struct if_laddrreq req;
   1952 
   1953 	psrcaddr[0] = pdstaddr[0] = '\0';
   1954 
   1955 	memset(&req, 0, sizeof(req));
   1956 	strncpy(req.iflr_name, name, IFNAMSIZ);
   1957 	if (ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&req) < 0)
   1958 		return;
   1959 #ifdef INET6
   1960 	if (req.addr.ss_family == AF_INET6)
   1961 		in6_fillscopeid((struct sockaddr_in6 *)&req.addr);
   1962 #endif
   1963 	getnameinfo((struct sockaddr *)&req.addr, req.addr.ss_len,
   1964 	    psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
   1965 #ifdef INET6
   1966 	if (req.addr.ss_family == AF_INET6)
   1967 		ver = "6";
   1968 #endif
   1969 
   1970 #ifdef INET6
   1971 	if (req.dstaddr.ss_family == AF_INET6)
   1972 		in6_fillscopeid((struct sockaddr_in6 *)&req.dstaddr);
   1973 #endif
   1974 	getnameinfo((struct sockaddr *)&req.dstaddr, req.dstaddr.ss_len,
   1975 	    pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
   1976 
   1977 	printf("\ttunnel inet%s %s --> %s\n", ver, psrcaddr, pdstaddr);
   1978 }
   1979 
   1980 void
   1981 vlan_status()
   1982 {
   1983 	struct vlanreq vlr;
   1984 
   1985 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
   1986 	    !isdigit(ifr.ifr_name[4]))
   1987 		return;
   1988 
   1989 	memset(&vlr, 0, sizeof(vlr));
   1990 	ifr.ifr_data = (caddr_t)&vlr;
   1991 
   1992 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
   1993 		return;
   1994 
   1995 	if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
   1996 		printf("\tvlan: %d parent: %s\n",
   1997 		    vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
   1998 		    "<none>" : vlr.vlr_parent);
   1999 }
   2000 
   2001 void
   2002 in_alias(creq)
   2003 	struct ifreq *creq;
   2004 {
   2005 	struct sockaddr_in *sin;
   2006 	int alias;
   2007 
   2008 	if (lflag)
   2009 		return;
   2010 
   2011 	alias = 1;
   2012 
   2013 	/* Get the non-alias address for this interface. */
   2014 	getsock(AF_INET);
   2015 	if (s < 0) {
   2016 		if (errno == EPROTONOSUPPORT)
   2017 			return;
   2018 		err(1, "socket");
   2019 	}
   2020 	(void) memset(&ifr, 0, sizeof(ifr));
   2021 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   2022 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   2023 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2024 			return;
   2025 		} else
   2026 			warn("SIOCGIFADDR");
   2027 	}
   2028 	/* If creq and ifr are the same address, this is not an alias. */
   2029 	if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
   2030 		   sizeof(creq->ifr_addr)) == 0)
   2031 		alias = 0;
   2032 	/* we print aliases only with -A */
   2033 	if (alias && !Aflag)
   2034 		return;
   2035 	(void) memset(&addreq, 0, sizeof(addreq));
   2036 	(void) strncpy(addreq.ifra_name, name, sizeof(addreq.ifra_name));
   2037 	addreq.ifra_addr = creq->ifr_addr;
   2038 	if (ioctl(s, SIOCGIFALIAS, (caddr_t)&addreq) < 0) {
   2039 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2040 			return;
   2041 		} else
   2042 			warn("SIOCGIFALIAS");
   2043 	}
   2044 
   2045 	sin = (struct sockaddr_in *)&addreq.ifra_addr;
   2046 	printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(sin->sin_addr));
   2047 
   2048 	if (flags & IFF_POINTOPOINT) {
   2049 		sin = (struct sockaddr_in *)&addreq.ifra_dstaddr;
   2050 		printf(" -> %s", inet_ntoa(sin->sin_addr));
   2051 	}
   2052 
   2053 	sin = (struct sockaddr_in *)&addreq.ifra_mask;
   2054 	printf(" netmask 0x%x", ntohl(sin->sin_addr.s_addr));
   2055 
   2056 	if (flags & IFF_BROADCAST) {
   2057 		sin = (struct sockaddr_in *)&addreq.ifra_broadaddr;
   2058 		printf(" broadcast %s", inet_ntoa(sin->sin_addr));
   2059 	}
   2060 	printf("\n");
   2061 }
   2062 
   2063 void
   2064 in_status(force)
   2065 	int force;
   2066 {
   2067 #ifdef HAVE_IFADDRS_H
   2068 	struct ifaddrs *ifap, *ifa;
   2069 	struct ifreq ifr;
   2070 
   2071 	if (getifaddrs(&ifap) != 0)
   2072 		err(1, "getifaddrs");
   2073 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   2074 		if (strcmp(name, ifa->ifa_name) != 0)
   2075 			continue;
   2076 		if (ifa->ifa_addr->sa_family != AF_INET)
   2077 			continue;
   2078 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
   2079 			continue;
   2080 
   2081 		memset(&ifr, 0, sizeof(ifr));
   2082 		strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
   2083 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
   2084 		in_alias(&ifr);
   2085 	}
   2086 	freeifaddrs(ifap);
   2087 #else
   2088 	char inbuf[8192];
   2089 	struct ifconf ifc;
   2090 	struct ifreq *ifr;
   2091 	int i, siz;
   2092 	char ifrbuf[8192], *cp;
   2093 
   2094 	ifc.ifc_len = sizeof(inbuf);
   2095 	ifc.ifc_buf = inbuf;
   2096 	getsock(af);
   2097 	if (s < 0)
   2098 		err(1, "socket");
   2099 	if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
   2100 		err(1, "SIOCGIFCONF");
   2101 	for (i = 0; i < ifc.ifc_len; ) {
   2102 		/* Copy the mininum ifreq into the buffer. */
   2103 		cp = ((caddr_t)ifc.ifc_req + i);
   2104 		memcpy(ifrbuf, cp, sizeof(*ifr));
   2105 
   2106 		/* Now compute the actual size of the ifreq. */
   2107 		ifr = (struct ifreq *)ifrbuf;
   2108 		siz = ifr->ifr_addr.sa_len;
   2109 		if (siz < sizeof(ifr->ifr_addr))
   2110 			siz = sizeof(ifr->ifr_addr);
   2111 		siz += sizeof(ifr->ifr_name);
   2112 		i += siz;
   2113 
   2114 		/* Now copy the whole thing. */
   2115 		if (sizeof(ifrbuf) < siz)
   2116 			errx(1, "ifr too big");
   2117 		memcpy(ifrbuf, cp, siz);
   2118 
   2119 		if (!strncmp(name, ifr->ifr_name, sizeof(ifr->ifr_name))) {
   2120 			if (ifr->ifr_addr.sa_family == AF_INET)
   2121 				in_alias(ifr);
   2122 		}
   2123 	}
   2124 #endif
   2125 }
   2126 
   2127 void
   2128 setifprefixlen(addr, d)
   2129 	const char *addr;
   2130 	int d;
   2131 {
   2132 	if (*afp->af_getprefix)
   2133 		(*afp->af_getprefix)(addr, MASK);
   2134 	explicit_prefix = 1;
   2135 }
   2136 
   2137 #ifdef INET6
   2138 void
   2139 in6_fillscopeid(sin6)
   2140 	struct sockaddr_in6 *sin6;
   2141 {
   2142 #if defined(__KAME__) && defined(KAME_SCOPEID)
   2143 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   2144 		sin6->sin6_scope_id =
   2145 			ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
   2146 		sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
   2147 	}
   2148 #endif
   2149 }
   2150 
   2151 /* XXX not really an alias */
   2152 void
   2153 in6_alias(creq)
   2154 	struct in6_ifreq *creq;
   2155 {
   2156 	struct sockaddr_in6 *sin6;
   2157 	char hbuf[NI_MAXHOST];
   2158 	u_int32_t scopeid;
   2159 #ifdef NI_WITHSCOPEID
   2160 	const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
   2161 #else
   2162 	const int niflag = NI_NUMERICHOST;
   2163 #endif
   2164 
   2165 	/* Get the non-alias address for this interface. */
   2166 	getsock(AF_INET6);
   2167 	if (s < 0) {
   2168 		if (errno == EPROTONOSUPPORT)
   2169 			return;
   2170 		err(1, "socket");
   2171 	}
   2172 
   2173 	sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
   2174 
   2175 	in6_fillscopeid(sin6);
   2176 	scopeid = sin6->sin6_scope_id;
   2177 	if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
   2178 			hbuf, sizeof(hbuf), NULL, 0, niflag))
   2179 		strncpy(hbuf, "", sizeof(hbuf));	/* some message? */
   2180 	printf("\tinet6 %s", hbuf);
   2181 
   2182 	if (flags & IFF_POINTOPOINT) {
   2183 		(void) memset(&ifr6, 0, sizeof(ifr6));
   2184 		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2185 		ifr6.ifr_addr = creq->ifr_addr;
   2186 		if (ioctl(s, SIOCGIFDSTADDR_IN6, (caddr_t)&ifr6) < 0) {
   2187 			if (errno != EADDRNOTAVAIL)
   2188 				warn("SIOCGIFDSTADDR_IN6");
   2189 			(void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
   2190 			ifr6.ifr_addr.sin6_family = AF_INET6;
   2191 			ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
   2192 		}
   2193 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
   2194 		in6_fillscopeid(sin6);
   2195 		hbuf[0] = '\0';
   2196 		if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
   2197 				hbuf, sizeof(hbuf), NULL, 0, niflag))
   2198 			strncpy(hbuf, "", sizeof(hbuf)); /* some message? */
   2199 		printf(" -> %s", hbuf);
   2200 	}
   2201 
   2202 	(void) memset(&ifr6, 0, sizeof(ifr6));
   2203 	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2204 	ifr6.ifr_addr = creq->ifr_addr;
   2205 	if (ioctl(s, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) {
   2206 		if (errno != EADDRNOTAVAIL)
   2207 			warn("SIOCGIFNETMASK_IN6");
   2208 	} else {
   2209 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
   2210 		printf(" prefixlen %d", prefix(&sin6->sin6_addr,
   2211 					       sizeof(struct in6_addr)));
   2212 	}
   2213 
   2214 	(void) memset(&ifr6, 0, sizeof(ifr6));
   2215 	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2216 	ifr6.ifr_addr = creq->ifr_addr;
   2217 	if (ioctl(s, SIOCGIFAFLAG_IN6, (caddr_t)&ifr6) < 0) {
   2218 		if (errno != EADDRNOTAVAIL)
   2219 			warn("SIOCGIFAFLAG_IN6");
   2220 	} else {
   2221 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
   2222 			printf(" anycast");
   2223 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
   2224 			printf(" tentative");
   2225 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
   2226 			printf(" duplicated");
   2227 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
   2228 			printf(" detached");
   2229 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
   2230 			printf(" deprecated");
   2231 	}
   2232 
   2233 	if (scopeid)
   2234 		printf(" scopeid 0x%x", scopeid);
   2235 
   2236 	if (Lflag) {
   2237 		struct in6_addrlifetime *lifetime;
   2238 		(void) memset(&ifr6, 0, sizeof(ifr6));
   2239 		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2240 		ifr6.ifr_addr = creq->ifr_addr;
   2241 		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
   2242 		if (ioctl(s, SIOCGIFALIFETIME_IN6, (caddr_t)&ifr6) < 0) {
   2243 			if (errno != EADDRNOTAVAIL)
   2244 				warn("SIOCGIFALIFETIME_IN6");
   2245 		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
   2246 			time_t t = time(NULL);
   2247 			printf(" pltime ");
   2248 			if (lifetime->ia6t_preferred) {
   2249 				printf("%s", lifetime->ia6t_preferred < t
   2250 					? "0"
   2251 					: sec2str(lifetime->ia6t_preferred - t));
   2252 			} else
   2253 				printf("infty");
   2254 
   2255 			printf(" vltime ");
   2256 			if (lifetime->ia6t_expire) {
   2257 				printf("%s", lifetime->ia6t_expire < t
   2258 					? "0"
   2259 					: sec2str(lifetime->ia6t_expire - t));
   2260 			} else
   2261 				printf("infty");
   2262 		}
   2263 	}
   2264 
   2265 	printf("\n");
   2266 }
   2267 
   2268 void
   2269 in6_status(force)
   2270 	int force;
   2271 {
   2272 #ifdef HAVE_IFADDRS_H
   2273 	struct ifaddrs *ifap, *ifa;
   2274 	struct in6_ifreq ifr;
   2275 
   2276 	if (getifaddrs(&ifap) != 0)
   2277 		err(1, "getifaddrs");
   2278 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   2279 		if (strcmp(name, ifa->ifa_name) != 0)
   2280 			continue;
   2281 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2282 			continue;
   2283 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
   2284 			continue;
   2285 
   2286 		memset(&ifr, 0, sizeof(ifr));
   2287 		strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
   2288 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
   2289 		in6_alias(&ifr);
   2290 	}
   2291 	freeifaddrs(ifap);
   2292 #else
   2293 	char inbuf[8192];
   2294 	struct ifconf ifc;
   2295 	struct ifreq *ifr;
   2296 	int i, siz;
   2297 	char ifrbuf[8192], *cp;
   2298 
   2299 	ifc.ifc_len = sizeof(inbuf);
   2300 	ifc.ifc_buf = inbuf;
   2301 	getsock(af);
   2302 	if (s < 0)
   2303 		err(1, "socket");
   2304 	if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
   2305 		err(1, "SIOCGIFCONF");
   2306 	for (i = 0; i < ifc.ifc_len; ) {
   2307 		/* Copy the mininum ifreq into the buffer. */
   2308 		cp = ((caddr_t)ifc.ifc_req + i);
   2309 		memcpy(ifrbuf, cp, sizeof(*ifr));
   2310 
   2311 		/* Now compute the actual size of the ifreq. */
   2312 		ifr = (struct ifreq *)ifrbuf;
   2313 		siz = ifr->ifr_addr.sa_len;
   2314 		if (siz < sizeof(ifr->ifr_addr))
   2315 			siz = sizeof(ifr->ifr_addr);
   2316 		siz += sizeof(ifr->ifr_name);
   2317 		i += siz;
   2318 
   2319 		/* Now copy the whole thing. */
   2320 		if (sizeof(ifrbuf) < siz)
   2321 			errx(1, "ifr too big");
   2322 		memcpy(ifrbuf, cp, siz);
   2323 
   2324 		if (!strncmp(name, ifr->ifr_name, sizeof(ifr->ifr_name))) {
   2325 			if (ifr->ifr_addr.sa_family == AF_INET6)
   2326 				in6_alias((struct in6_ifreq *)ifr);
   2327 		}
   2328 	}
   2329 #endif
   2330 }
   2331 #endif /*INET6*/
   2332 
   2333 #ifndef INET_ONLY
   2334 
   2335 void
   2336 at_status(force)
   2337 	int force;
   2338 {
   2339 	struct sockaddr_at *sat, null_sat;
   2340 	struct netrange *nr;
   2341 
   2342 	getsock(AF_APPLETALK);
   2343 	if (s < 0) {
   2344 		if (errno == EPROTONOSUPPORT)
   2345 			return;
   2346 		err(1, "socket");
   2347 	}
   2348 	(void) memset(&ifr, 0, sizeof(ifr));
   2349 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   2350 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   2351 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2352 			if (!force)
   2353 				return;
   2354 			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2355 		} else
   2356 			warn("SIOCGIFADDR");
   2357 	}
   2358 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   2359 	sat = (struct sockaddr_at *)&ifr.ifr_addr;
   2360 
   2361 	(void) memset(&null_sat, 0, sizeof(null_sat));
   2362 
   2363 	nr = (struct netrange *) &sat->sat_zero;
   2364 	printf("\tatalk %d.%d range %d-%d phase %d",
   2365 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
   2366 	    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
   2367 	if (flags & IFF_POINTOPOINT) {
   2368 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   2369 			if (errno == EADDRNOTAVAIL)
   2370 			    (void) memset(&ifr.ifr_addr, 0,
   2371 				sizeof(ifr.ifr_addr));
   2372 			else
   2373 			    warn("SIOCGIFDSTADDR");
   2374 		}
   2375 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   2376 		sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
   2377 		if (!sat)
   2378 			sat = &null_sat;
   2379 		printf("--> %d.%d",
   2380 		    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
   2381 	}
   2382 	if (flags & IFF_BROADCAST) {
   2383 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
   2384 		sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
   2385 		if (sat)
   2386 			printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
   2387 			    sat->sat_addr.s_node);
   2388 	}
   2389 	putchar('\n');
   2390 }
   2391 
   2392 void
   2393 xns_status(force)
   2394 	int force;
   2395 {
   2396 	struct sockaddr_ns *sns;
   2397 
   2398 	getsock(AF_NS);
   2399 	if (s < 0) {
   2400 		if (errno == EPROTONOSUPPORT)
   2401 			return;
   2402 		err(1, "socket");
   2403 	}
   2404 	(void) memset(&ifr, 0, sizeof(ifr));
   2405 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   2406 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   2407 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2408 			if (!force)
   2409 				return;
   2410 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2411 		} else
   2412 			warn("SIOCGIFADDR");
   2413 	}
   2414 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   2415 	sns = (struct sockaddr_ns *)&ifr.ifr_addr;
   2416 	printf("\tns %s ", ns_ntoa(sns->sns_addr));
   2417 	if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
   2418 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   2419 			if (errno == EADDRNOTAVAIL)
   2420 			    memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2421 			else
   2422 			    warn("SIOCGIFDSTADDR");
   2423 		}
   2424 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   2425 		sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
   2426 		printf("--> %s ", ns_ntoa(sns->sns_addr));
   2427 	}
   2428 	putchar('\n');
   2429 }
   2430 
   2431 void
   2432 iso_status(force)
   2433 	int force;
   2434 {
   2435 	struct sockaddr_iso *siso;
   2436 	struct iso_ifreq ifr;
   2437 
   2438 	getsock(AF_ISO);
   2439 	if (s < 0) {
   2440 		if (errno == EPROTONOSUPPORT)
   2441 			return;
   2442 		err(1, "socket");
   2443 	}
   2444 	(void) memset(&ifr, 0, sizeof(ifr));
   2445 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   2446 	if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&ifr) < 0) {
   2447 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2448 			if (!force)
   2449 				return;
   2450 			(void) memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   2451 		} else
   2452 			warn("SIOCGIFADDR_ISO");
   2453 	}
   2454 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   2455 	siso = &ifr.ifr_Addr;
   2456 	printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
   2457 	if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&ifr) < 0) {
   2458 		if (errno == EADDRNOTAVAIL)
   2459 			memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   2460 		else
   2461 			warn("SIOCGIFNETMASK_ISO");
   2462 	} else {
   2463 		if (siso->siso_len > offsetof(struct sockaddr_iso, siso_addr))
   2464 			siso->siso_addr.isoa_len = siso->siso_len
   2465 			    - offsetof(struct sockaddr_iso, siso_addr);
   2466 		printf("\n\t\tnetmask %s ", iso_ntoa(&siso->siso_addr));
   2467 	}
   2468 	if (flags & IFF_POINTOPOINT) {
   2469 		if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&ifr) < 0) {
   2470 			if (errno == EADDRNOTAVAIL)
   2471 			    memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   2472 			else
   2473 			    warn("SIOCGIFDSTADDR_ISO");
   2474 		}
   2475 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   2476 		siso = &ifr.ifr_Addr;
   2477 		printf("--> %s ", iso_ntoa(&siso->siso_addr));
   2478 	}
   2479 	putchar('\n');
   2480 }
   2481 
   2482 #endif	/* INET_ONLY */
   2483 
   2484 #define SIN(x) ((struct sockaddr_in *) &(x))
   2485 struct sockaddr_in *sintab[] = {
   2486 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
   2487 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
   2488 
   2489 void
   2490 in_getaddr(s, which)
   2491 	const char *s;
   2492 	int which;
   2493 {
   2494 	struct sockaddr_in *sin = sintab[which];
   2495 	struct hostent *hp;
   2496 	struct netent *np;
   2497 
   2498 	sin->sin_len = sizeof(*sin);
   2499 	if (which != MASK)
   2500 		sin->sin_family = AF_INET;
   2501 
   2502 	if (which == ADDR) {
   2503 		char *p = NULL;
   2504 		if ((p = strrchr(s, '/')) != NULL) {
   2505 			*p = '\0';
   2506 			in_getprefix(p + 1, MASK);
   2507 		}
   2508 	}
   2509 
   2510 	if (inet_aton(s, &sin->sin_addr) == 0) {
   2511 		if ((hp = gethostbyname(s)) != NULL)
   2512 			(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
   2513 		else if ((np = getnetbyname(s)) != NULL)
   2514 			sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
   2515 		else
   2516 			errx(1, "%s: bad value", s);
   2517 	}
   2518 }
   2519 
   2520 void
   2521 in_getprefix(plen, which)
   2522 	const char *plen;
   2523 	int which;
   2524 {
   2525 	register struct sockaddr_in *sin = sintab[which];
   2526 	register u_char *cp;
   2527 	int len = strtol(plen, (char **)NULL, 10);
   2528 
   2529 	if ((len < 0) || (len > 32))
   2530 		errx(1, "%s: bad value", plen);
   2531 	sin->sin_len = sizeof(*sin);
   2532 	if (which != MASK)
   2533 		sin->sin_family = AF_INET;
   2534 	if ((len == 0) || (len == 32)) {
   2535 		memset(&sin->sin_addr, 0xff, sizeof(struct in_addr));
   2536 		return;
   2537 	}
   2538 	memset((void *)&sin->sin_addr, 0x00, sizeof(sin->sin_addr));
   2539 	for (cp = (u_char *)&sin->sin_addr; len > 7; len -= 8)
   2540 		*cp++ = 0xff;
   2541 	*cp = 0xff << (8 - len);
   2542 }
   2543 
   2544 /*
   2545  * Print a value a la the %b format of the kernel's printf
   2546  */
   2547 void
   2548 printb(s, v, bits)
   2549 	const char *s;
   2550 	unsigned short v;
   2551 	const char *bits;
   2552 {
   2553 	int i, any = 0;
   2554 	char c;
   2555 
   2556 	if (bits && *bits == 8)
   2557 		printf("%s=%o", s, v);
   2558 	else
   2559 		printf("%s=%x", s, v);
   2560 	bits++;
   2561 	if (bits) {
   2562 		putchar('<');
   2563 		while ((i = *bits++) != 0) {
   2564 			if (v & (1 << (i-1))) {
   2565 				if (any)
   2566 					putchar(',');
   2567 				any = 1;
   2568 				for (; (c = *bits) > 32; bits++)
   2569 					putchar(c);
   2570 			} else
   2571 				for (; *bits > 32; bits++)
   2572 					;
   2573 		}
   2574 		putchar('>');
   2575 	}
   2576 }
   2577 
   2578 #ifdef INET6
   2579 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
   2580 struct sockaddr_in6 *sin6tab[] = {
   2581 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
   2582 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
   2583 
   2584 void
   2585 in6_getaddr(s, which)
   2586 	const char *s;
   2587 	int which;
   2588 {
   2589 #if defined(__KAME__) && defined(KAME_SCOPEID)
   2590 	struct sockaddr_in6 *sin6 = sin6tab[which];
   2591 	struct addrinfo hints, *res;
   2592 	int error;
   2593 
   2594 	memset(&hints, 0, sizeof(hints));
   2595 	hints.ai_family = AF_INET6;
   2596 	hints.ai_socktype = SOCK_DGRAM;
   2597 #if 0 /* in_getaddr() allows FQDN */
   2598 	hints.ai_flags = AI_NUMERICHOST;
   2599 #endif
   2600 	error = getaddrinfo(s, "0", &hints, &res);
   2601 	if (error)
   2602 		errx(1, "%s: %s", s, gai_strerror(error));
   2603 	if (res->ai_next)
   2604 		errx(1, "%s: resolved to multiple hosts", s);
   2605 	if (res->ai_addrlen != sizeof(struct sockaddr_in6))
   2606 		errx(1, "%s: bad value", s);
   2607 	memcpy(sin6, res->ai_addr, res->ai_addrlen);
   2608 	freeaddrinfo(res);
   2609 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) {
   2610 		*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] =
   2611 			htons(sin6->sin6_scope_id);
   2612 		sin6->sin6_scope_id = 0;
   2613 	}
   2614 #else
   2615 	struct sockaddr_in6 *sin = sin6tab[which];
   2616 
   2617 	sin->sin6_len = sizeof(*sin);
   2618 	if (which != MASK)
   2619 		sin->sin6_family = AF_INET6;
   2620 
   2621 	if (which == ADDR) {
   2622 		char *p = NULL;
   2623 		if((p = strrchr(s, '/')) != NULL) {
   2624 			*p = '\0';
   2625 			in6_getprefix(p + 1, MASK);
   2626 			explicit_prefix = 1;
   2627 		}
   2628 	}
   2629 
   2630 	if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
   2631 		errx(1, "%s: bad value", s);
   2632 #endif
   2633 }
   2634 
   2635 void
   2636 in6_getprefix(plen, which)
   2637 	const char *plen;
   2638 	int which;
   2639 {
   2640 	register struct sockaddr_in6 *sin = sin6tab[which];
   2641 	register u_char *cp;
   2642 	int len = strtol(plen, (char **)NULL, 10);
   2643 
   2644 	if ((len < 0) || (len > 128))
   2645 		errx(1, "%s: bad value", plen);
   2646 	sin->sin6_len = sizeof(*sin);
   2647 	if (which != MASK)
   2648 		sin->sin6_family = AF_INET6;
   2649 	if ((len == 0) || (len == 128)) {
   2650 		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
   2651 		return;
   2652 	}
   2653 	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
   2654 	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
   2655 		*cp++ = 0xff;
   2656 	*cp = 0xff << (8 - len);
   2657 }
   2658 
   2659 int
   2660 prefix(val, size)
   2661 	void *val;
   2662 	int size;
   2663 {
   2664 	register u_char *name = (u_char *)val;
   2665 	register int byte, bit, plen = 0;
   2666 
   2667 	for (byte = 0; byte < size; byte++, plen += 8)
   2668 		if (name[byte] != 0xff)
   2669 			break;
   2670 	if (byte == size)
   2671 		return (plen);
   2672 	for (bit = 7; bit != 0; bit--, plen++)
   2673 		if (!(name[byte] & (1 << bit)))
   2674 			break;
   2675 	for (; bit != 0; bit--)
   2676 		if (name[byte] & (1 << bit))
   2677 			return(0);
   2678 	byte++;
   2679 	for (; byte < size; byte++)
   2680 		if (name[byte])
   2681 			return(0);
   2682 	return (plen);
   2683 }
   2684 #endif /*INET6*/
   2685 
   2686 #ifndef INET_ONLY
   2687 void
   2688 at_getaddr(addr, which)
   2689 	const char *addr;
   2690 	int which;
   2691 {
   2692 	struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
   2693 	u_int net, node;
   2694 
   2695 	sat->sat_family = AF_APPLETALK;
   2696 	sat->sat_len = sizeof(*sat);
   2697 	if (which == MASK)
   2698 		errx(1, "AppleTalk does not use netmasks\n");
   2699 	if (sscanf(addr, "%u.%u", &net, &node) != 2
   2700 	    || net == 0 || net > 0xffff || node == 0 || node > 0xfe)
   2701 		errx(1, "%s: illegal address", addr);
   2702 	sat->sat_addr.s_net = htons(net);
   2703 	sat->sat_addr.s_node = node;
   2704 }
   2705 
   2706 void
   2707 setatrange(range, d)
   2708 	const char *range;
   2709 	int d;
   2710 {
   2711 	u_short	first = 123, last = 123;
   2712 
   2713 	if (sscanf(range, "%hu-%hu", &first, &last) != 2
   2714 	    || first == 0 || first > 0xffff
   2715 	    || last == 0 || last > 0xffff || first > last)
   2716 		errx(1, "%s: illegal net range: %u-%u", range, first, last);
   2717 	at_nr.nr_firstnet = htons(first);
   2718 	at_nr.nr_lastnet = htons(last);
   2719 }
   2720 
   2721 void
   2722 setatphase(phase, d)
   2723 	const char *phase;
   2724 	int d;
   2725 {
   2726 	if (!strcmp(phase, "1"))
   2727 		at_nr.nr_phase = 1;
   2728 	else if (!strcmp(phase, "2"))
   2729 		at_nr.nr_phase = 2;
   2730 	else
   2731 		errx(1, "%s: illegal phase", phase);
   2732 }
   2733 
   2734 void
   2735 checkatrange(sat)
   2736 	struct sockaddr_at *sat;
   2737 {
   2738 	if (at_nr.nr_phase == 0)
   2739 		at_nr.nr_phase = 2;	/* Default phase 2 */
   2740 	if (at_nr.nr_firstnet == 0)
   2741 		at_nr.nr_firstnet =	/* Default range of one */
   2742 		at_nr.nr_lastnet = sat->sat_addr.s_net;
   2743 	printf("\tatalk %d.%d range %d-%d phase %d\n",
   2744 	ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
   2745 	ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
   2746 	if ((u_short) ntohs(at_nr.nr_firstnet) >
   2747 			(u_short) ntohs(sat->sat_addr.s_net)
   2748 		    || (u_short) ntohs(at_nr.nr_lastnet) <
   2749 			(u_short) ntohs(sat->sat_addr.s_net))
   2750 		errx(1, "AppleTalk address is not in range");
   2751 	*((struct netrange *) &sat->sat_zero) = at_nr;
   2752 }
   2753 
   2754 #define SNS(x) ((struct sockaddr_ns *) &(x))
   2755 struct sockaddr_ns *snstab[] = {
   2756 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
   2757 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
   2758 
   2759 void
   2760 xns_getaddr(addr, which)
   2761 	const char *addr;
   2762 	int which;
   2763 {
   2764 	struct sockaddr_ns *sns = snstab[which];
   2765 
   2766 	sns->sns_family = AF_NS;
   2767 	sns->sns_len = sizeof(*sns);
   2768 	sns->sns_addr = ns_addr(addr);
   2769 	if (which == MASK)
   2770 		puts("Attempt to set XNS netmask will be ineffectual");
   2771 }
   2772 
   2773 #define SISO(x) ((struct sockaddr_iso *) &(x))
   2774 struct sockaddr_iso *sisotab[] = {
   2775 SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
   2776 SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
   2777 
   2778 void
   2779 iso_getaddr(addr, which)
   2780 	const char *addr;
   2781 	int which;
   2782 {
   2783 	struct sockaddr_iso *siso = sisotab[which];
   2784 	siso->siso_addr = *iso_addr(addr);
   2785 
   2786 	if (which == MASK) {
   2787 		siso->siso_len = TSEL(siso) - (caddr_t)(siso);
   2788 		siso->siso_nlen = 0;
   2789 	} else {
   2790 		siso->siso_len = sizeof(*siso);
   2791 		siso->siso_family = AF_ISO;
   2792 	}
   2793 }
   2794 
   2795 void
   2796 setsnpaoffset(val, d)
   2797 	const char *val;
   2798 	int d;
   2799 {
   2800 	iso_addreq.ifra_snpaoffset = atoi(val);
   2801 }
   2802 
   2803 void
   2804 setnsellength(val, d)
   2805 	const char *val;
   2806 	int d;
   2807 {
   2808 	nsellength = atoi(val);
   2809 	if (nsellength < 0)
   2810 		errx(1, "Negative NSEL length is absurd");
   2811 	if (afp == 0 || afp->af_af != AF_ISO)
   2812 		errx(1, "Setting NSEL length valid only for iso");
   2813 }
   2814 
   2815 void
   2816 fixnsel(s)
   2817 	struct sockaddr_iso *s;
   2818 {
   2819 	if (s->siso_family == 0)
   2820 		return;
   2821 	s->siso_tlen = nsellength;
   2822 }
   2823 
   2824 void
   2825 adjust_nsellength()
   2826 {
   2827 	fixnsel(sisotab[RIDADDR]);
   2828 	fixnsel(sisotab[ADDR]);
   2829 	fixnsel(sisotab[DSTADDR]);
   2830 }
   2831 
   2832 #endif	/* INET_ONLY */
   2833 
   2834 void
   2835 usage()
   2836 {
   2837 	const char *progname = getprogname();
   2838 
   2839 	fprintf(stderr,
   2840 	    "usage: %s [ -m ] [ -A ] "
   2841 #ifdef INET6
   2842 		"[ -L ] "
   2843 #endif
   2844 		"interface\n"
   2845 		"\t[ af [ address [ dest_addr ] ] [ up ] [ down ] "
   2846 		"[ netmask mask ] ]\n"
   2847 		"\t[ metric n ] [ mtu n ]\n"
   2848 		"\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
   2849 		"\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
   2850 		"\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
   2851 		"\t[ arp | -arp ]\n"
   2852 		"\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
   2853 		"[ instance minst ]\n"
   2854 		"\t[ vlan n vlanif i ]\n"
   2855 		"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
   2856 		"       %s -a [ -A ] [ -m ] [ -d ] [ -u ] [ af ]\n"
   2857 		"       %s -l [ -b ] [ -d ] [ -u ] [ -s ]\n"
   2858 		"       %s -C\n"
   2859 		"       %s interface create\n"
   2860 		"       %s interface destroy\n",
   2861 		progname, progname, progname, progname, progname, progname);
   2862 	exit(1);
   2863 }
   2864 
   2865 #ifdef INET6
   2866 char *
   2867 sec2str(total)
   2868 	time_t total;
   2869 {
   2870 	static char result[256];
   2871 	int days, hours, mins, secs;
   2872 	int first = 1;
   2873 	char *p = result;
   2874 
   2875 	if (0) {	/*XXX*/
   2876 		days = total / 3600 / 24;
   2877 		hours = (total / 3600) % 24;
   2878 		mins = (total / 60) % 60;
   2879 		secs = total % 60;
   2880 
   2881 		if (days) {
   2882 			first = 0;
   2883 			p += sprintf(p, "%dd", days);
   2884 		}
   2885 		if (!first || hours) {
   2886 			first = 0;
   2887 			p += sprintf(p, "%dh", hours);
   2888 		}
   2889 		if (!first || mins) {
   2890 			first = 0;
   2891 			p += sprintf(p, "%dm", mins);
   2892 		}
   2893 		sprintf(p, "%ds", secs);
   2894 	} else
   2895 		sprintf(p, "%lu", (u_long)total);
   2896 
   2897 	return(result);
   2898 }
   2899 #endif
   2900