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