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