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