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