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