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