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