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