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