Home | History | Annotate | Line # | Download | only in ifconfig
ifconfig.c revision 1.58
      1 /*	$NetBSD: ifconfig.c,v 1.58 1999/07/29 15:40:48 itojun Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1983, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 #ifndef lint
     75 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     76 	The Regents of the University of California.  All rights reserved.\n");
     77 #endif /* not lint */
     78 
     79 #ifndef lint
     80 #if 0
     81 static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
     82 #else
     83 __RCSID("$NetBSD: ifconfig.c,v 1.58 1999/07/29 15:40:48 itojun Exp $");
     84 #endif
     85 #endif /* not lint */
     86 
     87 #include <sys/param.h>
     88 #include <sys/socket.h>
     89 #include <sys/ioctl.h>
     90 
     91 #include <net/if.h>
     92 #include <net/if_dl.h>
     93 #include <net/if_media.h>
     94 #include <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 
    122 struct	ifreq		ifr, ridreq;
    123 struct	ifaliasreq	addreq __attribute__((aligned(4)));
    124 #ifdef INET6
    125 struct	in6_ifreq	ifr6;
    126 struct	in6_ifreq	in6_ridreq;
    127 struct	in6_aliasreq	in6_addreq __attribute__((aligned(4)));
    128 #endif
    129 struct	iso_ifreq	iso_ridreq;
    130 struct	iso_aliasreq	iso_addreq;
    131 struct	sockaddr_in	netmask;
    132 struct	netrange	at_nr;		/* AppleTalk net range */
    133 
    134 char	name[30];
    135 int	flags, metric, mtu, setaddr, setipdst, doalias;
    136 int	clearaddr, s;
    137 int	newaddr = -1;
    138 int	nsellength = 1;
    139 int	af;
    140 int	Aflag, aflag, bflag, dflag, lflag, mflag, sflag, uflag;
    141 #ifdef INET6
    142 int	Lflag;
    143 #endif
    144 int	reset_if_flags;
    145 int	explicit_prefix = 0;
    146 #if !defined(INET_ONLY) && defined(INET6)
    147 char ntop_buf[INET6_ADDRSTRLEN];	/*inet_ntop()*/
    148 #endif /* INET_ONLY */
    149 
    150 void 	notealias __P((char *, int));
    151 void 	notrailers __P((char *, int));
    152 void 	setifaddr __P((char *, int));
    153 void 	setifdstaddr __P((char *, int));
    154 void 	setifflags __P((char *, int));
    155 void 	setifbroadaddr __P((char *, int));
    156 void 	setifipdst __P((char *, int));
    157 void 	setifmetric __P((char *, int));
    158 void 	setifmtu __P((char *, int));
    159 void 	setifnetmask __P((char *, int));
    160 void	setifprefixlen __P((char *, int));
    161 void 	setnsellength __P((char *, int));
    162 void 	setsnpaoffset __P((char *, int));
    163 void	setatrange __P((char *, int));
    164 void	setatphase __P((char *, int));
    165 #ifdef INET6
    166 void 	setia6flags __P((char *, int));
    167 void	setia6pltime __P((char *, int));
    168 void	setia6vltime __P((char *, int));
    169 void	setia6lifetime __P((char *, char *));
    170 #endif
    171 void	checkatrange __P ((struct sockaddr_at *));
    172 void	setmedia __P((char *, int));
    173 void	setmediaopt __P((char *, int));
    174 void	unsetmediaopt __P((char *, int));
    175 void	setmediainst __P((char *, int));
    176 void	fixnsel __P((struct sockaddr_iso *));
    177 int	main __P((int, char *[]));
    178 
    179 /*
    180  * Media stuff.  Whenever a media command is first performed, the
    181  * currently select media is grabbed for this interface.  If `media'
    182  * is given, the current media word is modifed.  `mediaopt' commands
    183  * only modify the set and clear words.  They then operate on the
    184  * current media word later.
    185  */
    186 int	media_current;
    187 int	mediaopt_set;
    188 int	mediaopt_clear;
    189 
    190 int	actions;			/* Actions performed */
    191 
    192 #define	A_MEDIA		0x0001		/* media command */
    193 #define	A_MEDIAOPTSET	0x0002		/* mediaopt command */
    194 #define	A_MEDIAOPTCLR	0x0004		/* -mediaopt command */
    195 #define	A_MEDIAOPT	(A_MEDIAOPTSET|A_MEDIAOPTCLR)
    196 #define	A_MEDIAINST	0x0008		/* instance or inst command */
    197 
    198 #define	NEXTARG		0xffffff
    199 
    200 struct	cmd {
    201 	char	*c_name;
    202 	int	c_parameter;		/* NEXTARG means next argv */
    203 	int	c_action;	/* defered action */
    204 	void	(*c_func) __P((char *, int));
    205 } cmds[] = {
    206 	{ "up",		IFF_UP,		0,		setifflags } ,
    207 	{ "down",	-IFF_UP,	0,		setifflags },
    208 	{ "trailers",	-1,		0,		notrailers },
    209 	{ "-trailers",	1,		0,		notrailers },
    210 	{ "arp",	-IFF_NOARP,	0,		setifflags },
    211 	{ "-arp",	IFF_NOARP,	0,		setifflags },
    212 	{ "debug",	IFF_DEBUG,	0,		setifflags },
    213 	{ "-debug",	-IFF_DEBUG,	0,		setifflags },
    214 	{ "alias",	IFF_UP,		0,		notealias },
    215 	{ "-alias",	-IFF_UP,	0,		notealias },
    216 	{ "delete",	-IFF_UP,	0,		notealias },
    217 #ifdef notdef
    218 #define	EN_SWABIPS	0x1000
    219 	{ "swabips",	EN_SWABIPS,	0,		setifflags },
    220 	{ "-swabips",	-EN_SWABIPS,	0,		setifflags },
    221 #endif
    222 	{ "netmask",	NEXTARG,	0,		setifnetmask },
    223 	{ "metric",	NEXTARG,	0,		setifmetric },
    224 	{ "mtu",	NEXTARG,	0,		setifmtu },
    225 	{ "broadcast",	NEXTARG,	0,		setifbroadaddr },
    226 	{ "ipdst",	NEXTARG,	0,		setifipdst },
    227 	{ "prefixlen",  NEXTARG,	0,		setifprefixlen},
    228 #ifdef INET6
    229 	{ "anycast",	IN6_IFF_ANYCAST,	0,	setia6flags },
    230 	{ "-anycast",	-IN6_IFF_ANYCAST,	0,	setia6flags },
    231 	{ "tentative",	IN6_IFF_TENTATIVE,	0,	setia6flags },
    232 	{ "-tentative",	-IN6_IFF_TENTATIVE,	0,	setia6flags },
    233 	{ "pltime",	NEXTARG,	0,		setia6pltime },
    234 	{ "vltime",	NEXTARG,	0,		setia6vltime },
    235 #endif /*INET6*/
    236 #ifndef INET_ONLY
    237 	{ "range",	NEXTARG,	0,		setatrange },
    238 	{ "phase",	NEXTARG,	0,		setatphase },
    239 	{ "snpaoffset",	NEXTARG,	0,		setsnpaoffset },
    240 	{ "nsellength",	NEXTARG,	0,		setnsellength },
    241 #endif	/* INET_ONLY */
    242 	{ "link0",	IFF_LINK0,	0,		setifflags } ,
    243 	{ "-link0",	-IFF_LINK0,	0,		setifflags } ,
    244 	{ "link1",	IFF_LINK1,	0,		setifflags } ,
    245 	{ "-link1",	-IFF_LINK1,	0,		setifflags } ,
    246 	{ "link2",	IFF_LINK2,	0,		setifflags } ,
    247 	{ "-link2",	-IFF_LINK2,	0,		setifflags } ,
    248 	{ "media",	NEXTARG,	A_MEDIA,	setmedia },
    249 	{ "mediaopt",	NEXTARG,	A_MEDIAOPTSET,	setmediaopt },
    250 	{ "-mediaopt",	NEXTARG,	A_MEDIAOPTCLR,	unsetmediaopt },
    251 	{ "instance",	NEXTARG,	A_MEDIAINST,	setmediainst },
    252 	{ "inst",	NEXTARG,	A_MEDIAINST,	setmediainst },
    253 	{ 0,		0,		0,		setifaddr },
    254 	{ 0,		0,		0,		setifdstaddr },
    255 };
    256 
    257 void 	adjust_nsellength __P((void));
    258 int	getinfo __P((struct ifreq *));
    259 int	carrier __P((void));
    260 void	getsock __P((int));
    261 void	printall __P((void));
    262 void	printalias __P((const char *, int));
    263 void 	printb __P((char *, unsigned short, char *));
    264 int	prefix __P((void *, int));
    265 void 	status __P((const u_int8_t *, int));
    266 void 	usage __P((void));
    267 char	*sec2str __P((time_t));
    268 
    269 const char *get_media_type_string __P((int));
    270 const char *get_media_subtype_string __P((int));
    271 int	get_media_subtype __P((int, const char *));
    272 int	get_media_options __P((int, const char *));
    273 int	lookup_media_word __P((struct ifmedia_description *, int,
    274 	    const char *));
    275 void	print_media_word __P((int, int, int));
    276 void	process_media_commands __P((void));
    277 void	init_current_media __P((void));
    278 
    279 /*
    280  * XNS support liberally adapted from code written at the University of
    281  * Maryland principally by James O'Toole and Chris Torek.
    282  */
    283 void	in_alias __P((struct ifreq *));
    284 void	in_status __P((int));
    285 void 	in_getaddr __P((char *, int));
    286 #ifdef INET6
    287 void	in6_alias __P((struct in6_ifreq *));
    288 void	in6_status __P((int));
    289 void 	in6_getaddr __P((char *, int));
    290 void 	in6_getprefix __P((char *, int));
    291 #endif
    292 void	at_status __P((int));
    293 void	at_getaddr __P((char *, int));
    294 void 	xns_status __P((int));
    295 void 	xns_getaddr __P((char *, int));
    296 void 	iso_status __P((int));
    297 void 	iso_getaddr __P((char *, int));
    298 
    299 /* Known address families */
    300 struct afswtch {
    301 	char *af_name;
    302 	short af_af;
    303 	void (*af_status) __P((int));
    304 	void (*af_getaddr) __P((char *, int));
    305 	void (*af_getprefix) __P((char *, int));
    306 	u_long af_difaddr;
    307 	u_long af_aifaddr;
    308 	caddr_t af_ridreq;
    309 	caddr_t af_addreq;
    310 } afs[] = {
    311 #define C(x) ((caddr_t) &x)
    312 	{ "inet", AF_INET, in_status, in_getaddr, NULL,
    313 	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
    314 #ifdef INET6
    315 	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
    316 	     SIOCDIFADDR_IN6, SIOCAIFADDR_IN6, C(in6_ridreq), C(in6_addreq) },
    317 #endif
    318 #ifndef INET_ONLY	/* small version, for boot media */
    319 	{ "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
    320 	     SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
    321 	{ "ns", AF_NS, xns_status, xns_getaddr, NULL,
    322 	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
    323 	{ "iso", AF_ISO, iso_status, iso_getaddr, NULL,
    324 	     SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, C(iso_ridreq), C(iso_addreq) },
    325 #endif	/* INET_ONLY */
    326 	{ 0,	0,	    0,		0 }
    327 };
    328 
    329 struct afswtch *afp;	/*the address family being set or asked about*/
    330 
    331 struct afswtch *lookup_af __P((const char *));
    332 
    333 int
    334 main(argc, argv)
    335 	int argc;
    336 	char *argv[];
    337 {
    338 	int ch;
    339 
    340 	/* Parse command-line options */
    341 	aflag = mflag = 0;
    342 	while ((ch = getopt(argc, argv, "Aabdlmsu"
    343 #ifdef INET6
    344 					"L"
    345 #endif
    346 			)) != -1) {
    347 		switch (ch) {
    348 		case 'A':
    349 			Aflag = 1;
    350 			break;
    351 
    352 		case 'a':
    353 			aflag = 1;
    354 			break;
    355 
    356 		case 'b':
    357 			bflag = 1;
    358 			break;
    359 
    360 		case 'd':
    361 			dflag = 1;
    362 			break;
    363 
    364 #ifdef INET6
    365 		case 'L':
    366 			Lflag = 1;
    367 			break;
    368 #endif
    369 
    370 		case 'l':
    371 			lflag = 1;
    372 			break;
    373 
    374 		case 'm':
    375 			mflag = 1;
    376 			break;
    377 
    378 		case 's':
    379 			sflag = 1;
    380 			break;
    381 
    382 		case 'u':
    383 			uflag = 1;
    384 			break;
    385 
    386 
    387 		default:
    388 			usage();
    389 			/* NOTREACHED */
    390 		}
    391 	}
    392 	argc -= optind;
    393 	argv += optind;
    394 
    395 	/*
    396 	 * -l means "list all interfaces", and is mutally exclusive with
    397 	 * all other flags/commands.
    398 	 *
    399 	 * -a means "print status of all interfaces".
    400 	 */
    401 	if (lflag && (aflag || mflag || Aflag || argc))
    402 		usage();
    403 #ifdef INET6
    404 	if (lflag && Lflag)
    405 		usage();
    406 #endif
    407 	if (aflag || lflag) {
    408 		if (argc > 1)
    409 			usage();
    410 		else if (argc == 1) {
    411 			afp = lookup_af(argv[0]);
    412 			if (afp == NULL)
    413 				usage();
    414 		}
    415 		if (afp)
    416 			af = ifr.ifr_addr.sa_family = afp->af_af;
    417 		else
    418 			af = ifr.ifr_addr.sa_family = afs[0].af_af;
    419 		printall();
    420 		exit(0);
    421 	}
    422 
    423 	/* Make sure there's an interface name. */
    424 	if (argc < 1)
    425 		usage();
    426 	(void) strncpy(name, argv[0], sizeof(name));
    427 	argc--; argv++;
    428 
    429 	/* Check for address family. */
    430 	afp = NULL;
    431 	if (argc > 0) {
    432 		afp = lookup_af(argv[0]);
    433 		if (afp != NULL) {
    434 			argv++;
    435 			argc--;
    436 		}
    437 	}
    438 
    439 	/* Initialize af, just for use in getinfo(). */
    440 	if (afp == NULL)
    441 		af = afs->af_af;
    442 	else
    443 		af = afp->af_af;
    444 
    445 	/* Get information about the interface. */
    446 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    447 	if (getinfo(&ifr) < 0)
    448 		exit(1);
    449 
    450 	if (sflag) {
    451 		if (argc != 0)
    452 			usage();
    453 		else
    454 			exit(carrier());
    455 	}
    456 
    457 	/* No more arguments means interface status. */
    458 	if (argc == 0) {
    459 		status(NULL, 0);
    460 		exit(0);
    461 	}
    462 
    463 	/* following operations assume inet familiy as default */
    464 	if (afp == NULL)
    465 		afp = afs;
    466 	af = ifr.ifr_addr.sa_family = afp->af_af;
    467 
    468 #ifdef INET6
    469 	/* initialization */
    470 	in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    471 	in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    472 #endif
    473 
    474 	/* Process commands. */
    475 	while (argc > 0) {
    476 		struct cmd *p;
    477 
    478 		for (p = cmds; p->c_name; p++)
    479 			if (strcmp(argv[0], p->c_name) == 0)
    480 				break;
    481 		if (p->c_name == 0 && setaddr) {
    482 			if ((flags & IFF_POINTOPOINT) == 0) {
    483 				errx(1, "can't set destination address %s",
    484 				     "on non-point-to-point link");
    485 			}
    486 			p++;	/* got src, do dst */
    487 		}
    488 		if (p->c_func) {
    489 			if (p->c_parameter == NEXTARG) {
    490 				if (argc < 2)
    491 					errx(1, "'%s' requires argument",
    492 					    p->c_name);
    493 				(*p->c_func)(argv[1], 0);
    494 				argc--, argv++;
    495 			} else
    496 				(*p->c_func)(argv[0], p->c_parameter);
    497 			actions |= p->c_action;
    498 		}
    499 		argc--, argv++;
    500 	}
    501 
    502 	/* Process any media commands that may have been issued. */
    503 	process_media_commands();
    504 
    505 	if (af == AF_INET6 && explicit_prefix == 0) {
    506 		/*
    507 		 * Aggregatable address architecture defines all prefixes
    508 		 * are 64. So, it is convenient to set prefixlen to 64 if
    509 		 * it is not specified.
    510 		 */
    511 		setifprefixlen("64", 0);
    512 		/* in6_getprefix("64", MASK) if MASK is available here... */
    513 	}
    514 
    515 #ifndef INET_ONLY
    516 	if (af == AF_ISO)
    517 		adjust_nsellength();
    518 
    519 	if (af == AF_APPLETALK)
    520 		checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
    521 
    522 	if (setipdst && af==AF_NS) {
    523 		struct nsip_req rq;
    524 		int size = sizeof(rq);
    525 
    526 		rq.rq_ns = addreq.ifra_addr;
    527 		rq.rq_ip = addreq.ifra_dstaddr;
    528 
    529 		if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
    530 			warn("encapsulation routing");
    531 	}
    532 
    533 #endif	/* INET_ONLY */
    534 
    535 	if (clearaddr) {
    536 		int ret;
    537 		(void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
    538 		if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
    539 			if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
    540 				/* means no previous address for interface */
    541 			} else
    542 				warn("SIOCDIFADDR");
    543 		}
    544 	}
    545 	if (newaddr > 0) {
    546 		(void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
    547 		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
    548 			warn("SIOCAIFADDR");
    549 	}
    550 	if (reset_if_flags && ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
    551 		err(1, "SIOCSIFFLAGS");
    552 	exit(0);
    553 }
    554 
    555 struct afswtch *
    556 lookup_af(cp)
    557 	const char *cp;
    558 {
    559 	struct afswtch *a;
    560 
    561 	for (a = afs; a->af_name != NULL; a++)
    562 		if (strcmp(a->af_name, cp) == 0)
    563 			return (a);
    564 	return (NULL);
    565 }
    566 
    567 void
    568 getsock(naf)
    569 	int naf;
    570 {
    571 	static int oaf = -1;
    572 
    573 	if (oaf == naf)
    574 		return;
    575 	if (oaf != -1)
    576 		close(s);
    577 	s = socket(naf, SOCK_DGRAM, 0);
    578 	if (s < 0)
    579 		oaf = -1;
    580 	else
    581 		oaf = naf;
    582 }
    583 
    584 int
    585 getinfo(ifr)
    586 	struct ifreq *ifr;
    587 {
    588 
    589 	getsock(af);
    590 	if (s < 0)
    591 		err(1, "socket");
    592 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
    593 		warn("SIOCGIFFLAGS %s", ifr->ifr_name);
    594 		return (-1);
    595 	}
    596 	flags = ifr->ifr_flags;
    597 	if (ioctl(s, SIOCGIFMETRIC, (caddr_t)ifr) < 0) {
    598 		warn("SIOCGIFMETRIC %s", ifr->ifr_name);
    599 		metric = 0;
    600 	} else
    601 		metric = ifr->ifr_metric;
    602 	if (ioctl(s, SIOCGIFMTU, (caddr_t)ifr) < 0)
    603 		mtu = 0;
    604 	else
    605 		mtu = ifr->ifr_mtu;
    606 	return (0);
    607 }
    608 
    609 void
    610 printalias(iname, af)
    611 	const char *iname;
    612 	int af;
    613 {
    614 	char inbuf[8192];
    615 	struct ifconf ifc;
    616 	struct ifreq *ifr;
    617 	int i, siz;
    618 	char ifrbuf[8192];
    619 
    620 	ifc.ifc_len = sizeof(inbuf);
    621 	ifc.ifc_buf = inbuf;
    622 	getsock(af);
    623 	if (s < 0)
    624 		err(1, "socket");
    625 	if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
    626 		err(1, "SIOCGIFCONF");
    627 	ifr = ifc.ifc_req;
    628 	for (i = 0; i < ifc.ifc_len; ) {
    629 		ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
    630 		siz = sizeof(ifr->ifr_name) +
    631 			(ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
    632 				? ifr->ifr_addr.sa_len
    633 				: sizeof(struct sockaddr));
    634 		i += siz;
    635 		/* avoid alignment issue */
    636 		if (sizeof(ifrbuf) < siz)
    637 			errx(1, "ifr too big");
    638 		memcpy(ifrbuf, ifr, siz);
    639 		ifr = (struct ifreq *)ifrbuf;
    640 		if (!strncmp(iname, ifr->ifr_name, sizeof(ifr->ifr_name))) {
    641 			if (ifr->ifr_addr.sa_family == af)
    642 				switch (af) {
    643 				case AF_INET:
    644 					in_alias(ifr);
    645 					break;
    646 				default:
    647 					/*none*/
    648 				}
    649 			continue;
    650 		}
    651 	}
    652 }
    653 
    654 void
    655 printall()
    656 {
    657 	char inbuf[8192];
    658 	const struct sockaddr_dl *sdl = NULL;
    659 	struct ifconf ifc;
    660 	struct ifreq ifreq, *ifr;
    661 	int i, siz, idx;
    662 	char ifrbuf[8192];
    663 
    664 	ifc.ifc_len = sizeof(inbuf);
    665 	ifc.ifc_buf = inbuf;
    666 	getsock(af);
    667 	if (s < 0)
    668 		err(1, "socket");
    669 	if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
    670 		err(1, "SIOCGIFCONF");
    671 	ifr = ifc.ifc_req;
    672 	ifreq.ifr_name[0] = '\0';
    673 	for (i = 0, idx = 0; i < ifc.ifc_len; ) {
    674 		ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
    675 		siz = sizeof(ifr->ifr_name) +
    676 			(ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
    677 				? ifr->ifr_addr.sa_len
    678 				: sizeof(struct sockaddr));
    679 		i += siz;
    680 		/* avoid alignment issue */
    681 		if (sizeof(ifrbuf) < siz)
    682 			errx(1, "ifr too big");
    683 		memcpy(ifrbuf, ifr, siz);
    684 		ifr = (struct ifreq *)ifrbuf;
    685 		if (ifr->ifr_addr.sa_family == AF_LINK)
    686 			sdl = (const struct sockaddr_dl *) &ifr->ifr_addr;
    687 		if (!strncmp(ifreq.ifr_name, ifr->ifr_name,
    688 			     sizeof(ifr->ifr_name))) {
    689 			if (Aflag && ifr->ifr_addr.sa_family == AF_INET)
    690 				in_alias(ifr);
    691 			continue;
    692 		}
    693 		(void) strncpy(name, ifr->ifr_name, sizeof(ifr->ifr_name));
    694 		ifreq = *ifr;
    695 
    696 		if (getinfo(&ifreq) < 0)
    697 			continue;
    698 		if (bflag && (flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))
    699 			continue;
    700 		if (dflag && (flags & IFF_UP) != 0)
    701 			continue;
    702 		if (uflag && (flags & IFF_UP) == 0)
    703 			continue;
    704 
    705 		if (sflag && carrier())
    706 			continue;
    707 		idx++;
    708 		/*
    709 		 * Are we just listing the interfaces?
    710 		 */
    711 		if (lflag) {
    712 			if (idx > 1)
    713 				putchar(' ');
    714 			fputs(name, stdout);
    715 			continue;
    716 		}
    717 
    718 		if (sdl == NULL) {
    719 			status(NULL, 0);
    720 		} else {
    721 			status(LLADDR(sdl), sdl->sdl_alen);
    722 			sdl = NULL;
    723 		}
    724 	}
    725 	if (lflag)
    726 		putchar('\n');
    727 }
    728 
    729 #define RIDADDR 0
    730 #define ADDR	1
    731 #define MASK	2
    732 #define DSTADDR	3
    733 
    734 /*ARGSUSED*/
    735 void
    736 setifaddr(addr, param)
    737 	char *addr;
    738 	int param;
    739 {
    740 	/*
    741 	 * Delay the ioctl to set the interface addr until flags are all set.
    742 	 * The address interpretation may depend on the flags,
    743 	 * and the flags may change when the address is set.
    744 	 */
    745 	setaddr++;
    746 	if (newaddr == -1)
    747 		newaddr = 1;
    748 	if (doalias == 0)
    749 		clearaddr = 1;
    750 	(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
    751 }
    752 
    753 void
    754 setifnetmask(addr, d)
    755 	char *addr;
    756 	int d;
    757 {
    758 	(*afp->af_getaddr)(addr, MASK);
    759 }
    760 
    761 void
    762 setifbroadaddr(addr, d)
    763 	char *addr;
    764 	int d;
    765 {
    766 	(*afp->af_getaddr)(addr, DSTADDR);
    767 }
    768 
    769 void
    770 setifipdst(addr, d)
    771 	char *addr;
    772 	int d;
    773 {
    774 	in_getaddr(addr, DSTADDR);
    775 	setipdst++;
    776 	clearaddr = 0;
    777 	newaddr = 0;
    778 }
    779 
    780 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
    781 /*ARGSUSED*/
    782 void
    783 notealias(addr, param)
    784 	char *addr;
    785 	int param;
    786 {
    787 	if (setaddr && doalias == 0 && param < 0)
    788 		(void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
    789 		    rqtosa(af_addreq)->sa_len);
    790 	doalias = param;
    791 	if (param < 0) {
    792 		clearaddr = 1;
    793 		newaddr = 0;
    794 	} else
    795 		clearaddr = 0;
    796 }
    797 
    798 /*ARGSUSED*/
    799 void
    800 notrailers(vname, value)
    801 	char *vname;
    802 	int value;
    803 {
    804 	puts("Note: trailers are no longer sent, but always received");
    805 }
    806 
    807 /*ARGSUSED*/
    808 void
    809 setifdstaddr(addr, param)
    810 	char *addr;
    811 	int param;
    812 {
    813 	(*afp->af_getaddr)(addr, DSTADDR);
    814 }
    815 
    816 void
    817 setifflags(vname, value)
    818 	char *vname;
    819 	int value;
    820 {
    821  	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0)
    822 		err(1, "SIOCGIFFLAGS");
    823 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
    824  	flags = ifr.ifr_flags;
    825 
    826 	if (value < 0) {
    827 		value = -value;
    828 		flags &= ~value;
    829 	} else
    830 		flags |= value;
    831 	ifr.ifr_flags = flags;
    832 	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
    833 		err(1, "SIOCSIFFLAGS");
    834 
    835 	reset_if_flags = 1;
    836 }
    837 
    838 #ifdef INET6
    839 void
    840 setia6flags(vname, value)
    841 	char *vname;
    842 	int value;
    843 {
    844 	if (value < 0) {
    845 		value = -value;
    846 		in6_addreq.ifra_flags &= ~value;
    847 	} else
    848 		in6_addreq.ifra_flags |= value;
    849 }
    850 
    851 void
    852 setia6pltime(val, d)
    853 	char *val;
    854 	int d;
    855 {
    856 	setia6lifetime("pltime", val);
    857 }
    858 
    859 void
    860 setia6vltime(val, d)
    861 	char *val;
    862 	int d;
    863 {
    864 	setia6lifetime("vltime", val);
    865 }
    866 
    867 void
    868 setia6lifetime(cmd, val)
    869 	char *cmd;
    870 	char *val;
    871 {
    872 	time_t newval, t;
    873 	char *ep;
    874 
    875 	t = time(NULL);
    876 	newval = (time_t)strtoul(val, &ep, 0);
    877 	if (val == ep)
    878 		errx(1, "invalid %s", cmd);
    879 	if (afp->af_af != AF_INET6)
    880 		errx(1, "%s not allowed for the AF", cmd);
    881 	if (strcmp(cmd, "vltime") == 0) {
    882 		in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
    883 		in6_addreq.ifra_lifetime.ia6t_vltime = newval;
    884 	} else if (strcmp(cmd, "pltime") == 0) {
    885 		in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
    886 		in6_addreq.ifra_lifetime.ia6t_pltime = newval;
    887 	}
    888 }
    889 #endif
    890 
    891 void
    892 setifmetric(val, d)
    893 	char *val;
    894 	int d;
    895 {
    896 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
    897 	ifr.ifr_metric = atoi(val);
    898 	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
    899 		warn("SIOCSIFMETRIC");
    900 }
    901 
    902 void
    903 setifmtu(val, d)
    904 	char *val;
    905 	int d;
    906 {
    907 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    908 	ifr.ifr_mtu = atoi(val);
    909 	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
    910 		warn("SIOCSIFMTU");
    911 }
    912 
    913 void
    914 init_current_media()
    915 {
    916 	struct ifmediareq ifmr;
    917 
    918 	/*
    919 	 * If we have not yet done so, grab the currently-selected
    920 	 * media.
    921 	 */
    922 	if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
    923 		(void) memset(&ifmr, 0, sizeof(ifmr));
    924 		(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
    925 
    926 		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
    927 			/*
    928 			 * If we get E2BIG, the kernel is telling us
    929 			 * that there are more, so we can ignore it.
    930 			 */
    931 			if (errno != E2BIG)
    932 				err(1, "SGIOCGIFMEDIA");
    933 		}
    934 
    935 		media_current = ifmr.ifm_current;
    936 	}
    937 
    938 	/* Sanity. */
    939 	if (IFM_TYPE(media_current) == 0)
    940 		errx(1, "%s: no link type?", name);
    941 }
    942 
    943 void
    944 process_media_commands()
    945 {
    946 
    947 	if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
    948 		/* Nothing to do. */
    949 		return;
    950 	}
    951 
    952 	/*
    953 	 * Media already set up, and commands sanity-checked.  Set/clear
    954 	 * any options, and we're ready to go.
    955 	 */
    956 	media_current |= mediaopt_set;
    957 	media_current &= ~mediaopt_clear;
    958 
    959 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    960 	ifr.ifr_media = media_current;
    961 
    962 	if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
    963 		err(1, "SIOCSIFMEDIA");
    964 }
    965 
    966 void
    967 setmedia(val, d)
    968 	char *val;
    969 	int d;
    970 {
    971 	int type, subtype, inst;
    972 
    973 	init_current_media();
    974 
    975 	/* Only one media command may be given. */
    976 	if (actions & A_MEDIA)
    977 		errx(1, "only one `media' command may be issued");
    978 
    979 	/* Must not come after mediaopt commands */
    980 	if (actions & A_MEDIAOPT)
    981 		errx(1, "may not issue `media' after `mediaopt' commands");
    982 
    983 	/*
    984 	 * No need to check if `instance' has been issued; setmediainst()
    985 	 * craps out if `media' has not been specified.
    986 	 */
    987 
    988 	type = IFM_TYPE(media_current);
    989 	inst = IFM_INST(media_current);
    990 
    991 	/* Look up the subtype. */
    992 	subtype = get_media_subtype(type, val);
    993 
    994 	/* Build the new current media word. */
    995 	media_current = IFM_MAKEWORD(type, subtype, 0, inst);
    996 
    997 	/* Media will be set after other processing is complete. */
    998 }
    999 
   1000 void
   1001 setmediaopt(val, d)
   1002 	char *val;
   1003 	int d;
   1004 {
   1005 
   1006 	init_current_media();
   1007 
   1008 	/* Can only issue `mediaopt' once. */
   1009 	if (actions & A_MEDIAOPTSET)
   1010 		errx(1, "only one `mediaopt' command may be issued");
   1011 
   1012 	/* Can't issue `mediaopt' if `instance' has already been issued. */
   1013 	if (actions & A_MEDIAINST)
   1014 		errx(1, "may not issue `mediaopt' after `instance'");
   1015 
   1016 	mediaopt_set = get_media_options(IFM_TYPE(media_current), val);
   1017 
   1018 	/* Media will be set after other processing is complete. */
   1019 }
   1020 
   1021 void
   1022 unsetmediaopt(val, d)
   1023 	char *val;
   1024 	int d;
   1025 {
   1026 
   1027 	init_current_media();
   1028 
   1029 	/* Can only issue `-mediaopt' once. */
   1030 	if (actions & A_MEDIAOPTCLR)
   1031 		errx(1, "only one `-mediaopt' command may be issued");
   1032 
   1033 	/* May not issue `media' and `-mediaopt'. */
   1034 	if (actions & A_MEDIA)
   1035 		errx(1, "may not issue both `media' and `-mediaopt'");
   1036 
   1037 	/*
   1038 	 * No need to check for A_MEDIAINST, since the test for A_MEDIA
   1039 	 * implicitly checks for A_MEDIAINST.
   1040 	 */
   1041 
   1042 	mediaopt_clear = get_media_options(IFM_TYPE(media_current), val);
   1043 
   1044 	/* Media will be set after other processing is complete. */
   1045 }
   1046 
   1047 void
   1048 setmediainst(val, d)
   1049 	char *val;
   1050 	int d;
   1051 {
   1052 	int type, subtype, options, inst;
   1053 
   1054 	init_current_media();
   1055 
   1056 	/* Can only issue `instance' once. */
   1057 	if (actions & A_MEDIAINST)
   1058 		errx(1, "only one `instance' command may be issued");
   1059 
   1060 	/* Must have already specified `media' */
   1061 	if ((actions & A_MEDIA) == 0)
   1062 		errx(1, "must specify `media' before `instance'");
   1063 
   1064 	type = IFM_TYPE(media_current);
   1065 	subtype = IFM_SUBTYPE(media_current);
   1066 	options = IFM_OPTIONS(media_current);
   1067 
   1068 	inst = atoi(val);
   1069 	if (inst < 0 || inst > IFM_INST_MAX)
   1070 		errx(1, "invalid media instance: %s", val);
   1071 
   1072 	media_current = IFM_MAKEWORD(type, subtype, options, inst);
   1073 
   1074 	/* Media will be set after other processing is complete. */
   1075 }
   1076 
   1077 struct ifmedia_description ifm_type_descriptions[] =
   1078     IFM_TYPE_DESCRIPTIONS;
   1079 
   1080 struct ifmedia_description ifm_subtype_descriptions[] =
   1081     IFM_SUBTYPE_DESCRIPTIONS;
   1082 
   1083 struct ifmedia_description ifm_option_descriptions[] =
   1084     IFM_OPTION_DESCRIPTIONS;
   1085 
   1086 const char *
   1087 get_media_type_string(mword)
   1088 	int mword;
   1089 {
   1090 	struct ifmedia_description *desc;
   1091 
   1092 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
   1093 	     desc++) {
   1094 		if (IFM_TYPE(mword) == desc->ifmt_word)
   1095 			return (desc->ifmt_string);
   1096 	}
   1097 	return ("<unknown type>");
   1098 }
   1099 
   1100 const char *
   1101 get_media_subtype_string(mword)
   1102 	int mword;
   1103 {
   1104 	struct ifmedia_description *desc;
   1105 
   1106 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
   1107 	     desc++) {
   1108 		if (IFM_TYPE_MATCH(desc->ifmt_word, mword) &&
   1109 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword))
   1110 			return (desc->ifmt_string);
   1111 	}
   1112 	return ("<unknown subtype>");
   1113 }
   1114 
   1115 int
   1116 get_media_subtype(type, val)
   1117 	int type;
   1118 	const char *val;
   1119 {
   1120 	int rval;
   1121 
   1122 	rval = lookup_media_word(ifm_subtype_descriptions, type, val);
   1123 	if (rval == -1)
   1124 		errx(1, "unknown %s media subtype: %s",
   1125 		    get_media_type_string(type), val);
   1126 
   1127 	return (rval);
   1128 }
   1129 
   1130 int
   1131 get_media_options(type, val)
   1132 	int type;
   1133 	const char *val;
   1134 {
   1135 	char *optlist, *str;
   1136 	int option, rval = 0;
   1137 
   1138 	/* We muck with the string, so copy it. */
   1139 	optlist = strdup(val);
   1140 	if (optlist == NULL)
   1141 		err(1, "strdup");
   1142 	str = optlist;
   1143 
   1144 	/*
   1145 	 * Look up the options in the user-provided comma-separated list.
   1146 	 */
   1147 	for (; (str = strtok(str, ",")) != NULL; str = NULL) {
   1148 		option = lookup_media_word(ifm_option_descriptions, type, str);
   1149 		if (option == -1)
   1150 			errx(1, "unknown %s media option: %s",
   1151 			    get_media_type_string(type), str);
   1152 		rval |= option;
   1153 	}
   1154 
   1155 	free(optlist);
   1156 	return (rval);
   1157 }
   1158 
   1159 int
   1160 lookup_media_word(desc, type, val)
   1161 	struct ifmedia_description *desc;
   1162 	int type;
   1163 	const char *val;
   1164 {
   1165 
   1166 	for (; desc->ifmt_string != NULL; desc++) {
   1167 		if (IFM_TYPE_MATCH(desc->ifmt_word, type) &&
   1168 		    strcasecmp(desc->ifmt_string, val) == 0)
   1169 			return (desc->ifmt_word);
   1170 	}
   1171 	return (-1);
   1172 }
   1173 
   1174 void
   1175 print_media_word(ifmw, print_type, as_syntax)
   1176 	int ifmw, print_type, as_syntax;
   1177 {
   1178 	struct ifmedia_description *desc;
   1179 	int seen_option = 0;
   1180 
   1181 	if (print_type)
   1182 		printf("%s ", get_media_type_string(ifmw));
   1183 	printf("%s%s", as_syntax ? "media " : "",
   1184 	    get_media_subtype_string(ifmw));
   1185 
   1186 	/* Find options. */
   1187 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
   1188 	     desc++) {
   1189 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
   1190 		    (ifmw & desc->ifmt_word) != 0 &&
   1191 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
   1192 			if (seen_option == 0)
   1193 				printf(" %s", as_syntax ? "mediaopt " : "");
   1194 			printf("%s%s", seen_option ? "," : "",
   1195 			    desc->ifmt_string);
   1196 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
   1197 		}
   1198 	}
   1199 	if (IFM_INST(ifmw) != 0)
   1200 		printf(" instance %d", IFM_INST(ifmw));
   1201 }
   1202 
   1203 int carrier()
   1204 {
   1205 	struct ifmediareq ifmr;
   1206 
   1207 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1208 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1209 
   1210 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1211 		/*
   1212 		 * Interface doesn't support SIOC{G,S}IFMEDIA;
   1213 		 * assume ok.
   1214 		 */
   1215 		return 0;
   1216 	}
   1217 	if ((ifmr.ifm_status & IFM_AVALID) == 0) {
   1218 		/*
   1219 		 * Interface doesn't report media-valid status.
   1220 		 * assume ok.
   1221 		 */
   1222 		return 0;
   1223 	}
   1224 	/* otherwise, return ok for active, not-ok if not active. */
   1225 	return !(ifmr.ifm_status & IFM_ACTIVE);
   1226 }
   1227 
   1228 
   1229 #define	IFFBITS \
   1230 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
   1231 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
   1232 
   1233 /*
   1234  * Print the status of the interface.  If an address family was
   1235  * specified, show it and it only; otherwise, show them all.
   1236  */
   1237 void
   1238 status(ap, alen)
   1239 	const u_int8_t *ap;
   1240 	int alen;
   1241 {
   1242 	struct afswtch *p = afp;
   1243 	struct ifmediareq ifmr;
   1244 	int *media_list, i;
   1245 
   1246 	printf("%s: ", name);
   1247 	printb("flags", flags, IFFBITS);
   1248 	if (metric)
   1249 		printf(" metric %d", metric);
   1250 	if (mtu)
   1251 		printf(" mtu %d", mtu);
   1252 	putchar('\n');
   1253 	if (ap && alen > 0) {
   1254 		printf("\taddress:");
   1255 		for (i = 0; i < alen; i++, ap++)
   1256 			printf("%c%02x", i > 0 ? ':' : ' ', *ap);
   1257 		putchar('\n');
   1258 	}
   1259 
   1260 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1261 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1262 
   1263 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1264 		/*
   1265 		 * Interface doesn't support SIOC{G,S}IFMEDIA.
   1266 		 */
   1267 		goto proto_status;
   1268 	}
   1269 
   1270 	if (ifmr.ifm_count == 0) {
   1271 		warnx("%s: no media types?", name);
   1272 		goto proto_status;
   1273 	}
   1274 
   1275 	media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
   1276 	if (media_list == NULL)
   1277 		err(1, "malloc");
   1278 	ifmr.ifm_ulist = media_list;
   1279 
   1280 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
   1281 		err(1, "SIOCGIFMEDIA");
   1282 
   1283 	printf("\tmedia: ");
   1284 	print_media_word(ifmr.ifm_current, 1, 0);
   1285 	if (ifmr.ifm_active != ifmr.ifm_current) {
   1286 		putchar(' ');
   1287 		putchar('(');
   1288 		print_media_word(ifmr.ifm_active, 0, 0);
   1289 		putchar(')');
   1290 	}
   1291 	putchar('\n');
   1292 
   1293 	if (ifmr.ifm_status & IFM_AVALID) {
   1294 		printf("\tstatus: ");
   1295 		switch (IFM_TYPE(ifmr.ifm_active)) {
   1296 		case IFM_ETHER:
   1297 			if (ifmr.ifm_status & IFM_ACTIVE)
   1298 				printf("active");
   1299 			else
   1300 				printf("no carrier");
   1301 			break;
   1302 
   1303 		case IFM_FDDI:
   1304 		case IFM_TOKEN:
   1305 			if (ifmr.ifm_status & IFM_ACTIVE)
   1306 				printf("inserted");
   1307 			else
   1308 				printf("no ring");
   1309 			break;
   1310 		default:
   1311 			printf("unknown");
   1312 		}
   1313 		putchar('\n');
   1314 	}
   1315 
   1316 	if (mflag) {
   1317 		int type, printed_type;
   1318 
   1319 		for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
   1320 			for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
   1321 				if (IFM_TYPE(media_list[i]) == type) {
   1322 					if (printed_type == 0) {
   1323 					    printf("\tsupported %s media:\n",
   1324 					      get_media_type_string(type));
   1325 					    printed_type = 1;
   1326 					}
   1327 					printf("\t\t");
   1328 					print_media_word(media_list[i], 0, 1);
   1329 					printf("\n");
   1330 				}
   1331 			}
   1332 		}
   1333 	}
   1334 
   1335 	free(media_list);
   1336 
   1337  proto_status:
   1338 	if ((p = afp) != NULL) {
   1339 		(*p->af_status)(1);
   1340 		if (Aflag & !aflag)
   1341 			printalias(name, p->af_af);
   1342 	} else for (p = afs; p->af_name; p++) {
   1343 		ifr.ifr_addr.sa_family = p->af_af;
   1344 		(*p->af_status)(0);
   1345 		if (Aflag & !aflag && p->af_af == AF_INET)
   1346 			printalias(name, p->af_af);
   1347 	}
   1348 }
   1349 
   1350 void
   1351 in_alias(creq)
   1352 	struct ifreq *creq;
   1353 {
   1354 	struct sockaddr_in *sin;
   1355 
   1356 	if (lflag)
   1357 		return;
   1358 
   1359 	/* Get the non-alias address for this interface. */
   1360 	getsock(AF_INET);
   1361 	if (s < 0) {
   1362 		if (errno == EPROTONOSUPPORT)
   1363 			return;
   1364 		err(1, "socket");
   1365 	}
   1366 	(void) memset(&ifr, 0, sizeof(ifr));
   1367 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1368 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   1369 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1370 			return;
   1371 		} else
   1372 			warn("SIOCGIFADDR");
   1373 	}
   1374 	/* If creq and ifr are the same address, this is not an alias. */
   1375 	if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
   1376 		   sizeof(creq->ifr_addr)) == 0)
   1377 		return;
   1378 	(void) memset(&addreq, 0, sizeof(addreq));
   1379 	(void) strncpy(addreq.ifra_name, name, sizeof(addreq.ifra_name));
   1380 	addreq.ifra_addr = creq->ifr_addr;
   1381 	if (ioctl(s, SIOCGIFALIAS, (caddr_t)&addreq) < 0) {
   1382 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1383 			return;
   1384 		} else
   1385 			warn("SIOCGIFALIAS");
   1386 	}
   1387 
   1388 	sin = (struct sockaddr_in *)&addreq.ifra_addr;
   1389 	printf("\tinet alias %s", inet_ntoa(sin->sin_addr));
   1390 
   1391 	if (flags & IFF_POINTOPOINT) {
   1392 		sin = (struct sockaddr_in *)&addreq.ifra_dstaddr;
   1393 		printf(" -> %s", inet_ntoa(sin->sin_addr));
   1394 	}
   1395 
   1396 	sin = (struct sockaddr_in *)&addreq.ifra_mask;
   1397 	printf(" netmask 0x%x", ntohl(sin->sin_addr.s_addr));
   1398 
   1399 	if (flags & IFF_BROADCAST) {
   1400 		sin = (struct sockaddr_in *)&addreq.ifra_broadaddr;
   1401 		printf(" broadcast %s", inet_ntoa(sin->sin_addr));
   1402 	}
   1403 	printf("\n");
   1404 }
   1405 
   1406 void
   1407 in_status(force)
   1408 	int force;
   1409 {
   1410 	struct sockaddr_in *sin;
   1411 
   1412 	getsock(AF_INET);
   1413 	if (s < 0) {
   1414 		if (errno == EPROTONOSUPPORT)
   1415 			return;
   1416 		err(1, "socket");
   1417 	}
   1418 	(void) memset(&ifr, 0, sizeof(ifr));
   1419 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1420 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   1421 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1422 			if (!force)
   1423 				return;
   1424 			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1425 		} else
   1426 			warn("SIOCGIFADDR");
   1427 	}
   1428 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1429 	sin = (struct sockaddr_in *)&ifr.ifr_addr;
   1430 	printf("\tinet %s ", inet_ntoa(sin->sin_addr));
   1431 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1432 	if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
   1433 		if (errno != EADDRNOTAVAIL)
   1434 			warn("SIOCGIFNETMASK");
   1435 		(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1436 	} else
   1437 		netmask.sin_addr =
   1438 		    ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
   1439 	if (flags & IFF_POINTOPOINT) {
   1440 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   1441 			if (errno == EADDRNOTAVAIL)
   1442 			    (void) memset(&ifr.ifr_addr, 0,
   1443 				sizeof(ifr.ifr_addr));
   1444 			else
   1445 			    warn("SIOCGIFDSTADDR");
   1446 		}
   1447 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1448 		sin = (struct sockaddr_in *)&ifr.ifr_dstaddr;
   1449 		printf("--> %s ", inet_ntoa(sin->sin_addr));
   1450 	}
   1451 	printf("netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
   1452 	if (flags & IFF_BROADCAST) {
   1453 		if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
   1454 			if (errno == EADDRNOTAVAIL)
   1455 				(void) memset(&ifr.ifr_addr, 0,
   1456 				    sizeof(ifr.ifr_addr));
   1457 			else
   1458 			    warn("SIOCGIFBRDADDR");
   1459 		}
   1460 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1461 		sin = (struct sockaddr_in *)&ifr.ifr_addr;
   1462 		if (sin->sin_addr.s_addr != 0)
   1463 			printf("broadcast %s", inet_ntoa(sin->sin_addr));
   1464 	}
   1465 	putchar('\n');
   1466 }
   1467 
   1468 void
   1469 setifprefixlen(addr, d)
   1470 	char *addr;
   1471 	int d;
   1472 {
   1473 	if (*afp->af_getprefix)
   1474 		(*afp->af_getprefix)(addr, MASK);
   1475 	explicit_prefix = 1;
   1476 }
   1477 
   1478 #ifdef INET6
   1479 /* XXX not really an alias */
   1480 void
   1481 in6_alias(creq)
   1482 	struct in6_ifreq *creq;
   1483 {
   1484 	struct sockaddr_in6 *sin6;
   1485 
   1486 	/* Get the non-alias address for this interface. */
   1487 	getsock(AF_INET6);
   1488 	if (s < 0) {
   1489 		if (errno == EPROTONOSUPPORT)
   1490 			return;
   1491 		err(1, "socket");
   1492 	}
   1493 
   1494 	sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
   1495 
   1496 	printf("\tinet6 %s", inet_ntop(AF_INET6, &sin6->sin6_addr,
   1497 				       ntop_buf, sizeof(ntop_buf)));
   1498 
   1499 	if (flags & IFF_POINTOPOINT) {
   1500 		(void) memset(&ifr6, 0, sizeof(ifr6));
   1501 		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   1502 		ifr6.ifr_addr = creq->ifr_addr;
   1503 		if (ioctl(s, SIOCGIFDSTADDR_IN6, (caddr_t)&ifr6) < 0) {
   1504 			if (errno != EADDRNOTAVAIL)
   1505 				warn("SIOCGIFDSTADDR_IN6");
   1506 			(void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
   1507 			ifr6.ifr_addr.sin6_family = AF_INET6;
   1508 			ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
   1509 		}
   1510 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
   1511 		printf(" -> %s", inet_ntop(AF_INET6, &sin6->sin6_addr,
   1512 					   ntop_buf, sizeof(ntop_buf)));
   1513 	}
   1514 
   1515 	(void) memset(&ifr6, 0, sizeof(ifr6));
   1516 	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   1517 	ifr6.ifr_addr = creq->ifr_addr;
   1518 	if (ioctl(s, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) {
   1519 		if (errno != EADDRNOTAVAIL)
   1520 			warn("SIOCGIFNETMASK_IN6");
   1521 	} else {
   1522 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
   1523 		printf(" prefixlen %d", prefix(&sin6->sin6_addr,
   1524 					       sizeof(struct in6_addr)));
   1525 	}
   1526 
   1527 	(void) memset(&ifr6, 0, sizeof(ifr6));
   1528 	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   1529 	ifr6.ifr_addr = creq->ifr_addr;
   1530 	if (ioctl(s, SIOCGIFAFLAG_IN6, (caddr_t)&ifr6) < 0) {
   1531 		if (errno != EADDRNOTAVAIL)
   1532 			warn("SIOCGIFAFLAG_IN6");
   1533 	} else {
   1534 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
   1535 			printf(" anycast");
   1536 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
   1537 			printf(" tentative");
   1538 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
   1539 			printf(" duplicated");
   1540 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
   1541 			printf(" detached");
   1542 	}
   1543 
   1544 	if (Lflag) {
   1545 		struct in6_addrlifetime *lifetime;
   1546 		(void) memset(&ifr6, 0, sizeof(ifr6));
   1547 		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   1548 		ifr6.ifr_addr = creq->ifr_addr;
   1549 		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
   1550 		if (ioctl(s, SIOCGIFALIFETIME_IN6, (caddr_t)&ifr6) < 0) {
   1551 			if (errno != EADDRNOTAVAIL)
   1552 				warn("SIOCGIFALIFETIME_IN6");
   1553 		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
   1554 			time_t t = time(NULL);
   1555 			printf(" pltime ");
   1556 			if (lifetime->ia6t_preferred) {
   1557 				printf("%s", lifetime->ia6t_preferred < t
   1558 					? "0"
   1559 					: sec2str(lifetime->ia6t_preferred - t));
   1560 			} else
   1561 				printf("infty");
   1562 
   1563 			printf(" vltime ");
   1564 			if (lifetime->ia6t_expire) {
   1565 				printf("%s", lifetime->ia6t_expire < t
   1566 					? "0"
   1567 					: sec2str(lifetime->ia6t_expire - t));
   1568 			} else
   1569 				printf("infty");
   1570 		}
   1571 	}
   1572 
   1573 	printf("\n");
   1574 }
   1575 
   1576 void
   1577 in6_status(force)
   1578 	int force;
   1579 {
   1580 	char inbuf[8192];
   1581 	struct ifconf ifc;
   1582 	struct ifreq *ifr;
   1583 	int i, siz;
   1584 	char ifrbuf[8192];
   1585 
   1586 	ifc.ifc_len = sizeof(inbuf);
   1587 	ifc.ifc_buf = inbuf;
   1588 	getsock(af);
   1589 	if (s < 0)
   1590 		err(1, "socket");
   1591 	if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
   1592 		err(1, "SIOCGIFCONF");
   1593 	ifr = ifc.ifc_req;
   1594 	for (i = 0; i < ifc.ifc_len; ) {
   1595 		ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
   1596 		siz = sizeof(ifr->ifr_name) +
   1597 			(ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
   1598 				? ifr->ifr_addr.sa_len
   1599 				: sizeof(struct sockaddr));
   1600 		i += siz;
   1601 		/* avoid alignment issue */
   1602 		if (sizeof(ifrbuf) < siz)
   1603 			errx(1, "ifr too big");
   1604 		memcpy(ifrbuf, ifr, siz);
   1605 		ifr = (struct ifreq *)ifrbuf;
   1606 		if (!strncmp(name, ifr->ifr_name, sizeof(ifr->ifr_name))) {
   1607 			if (ifr->ifr_addr.sa_family == AF_INET6)
   1608 				in6_alias((struct in6_ifreq *)ifr);
   1609 		}
   1610 	}
   1611 }
   1612 #endif /*INET6*/
   1613 
   1614 #ifndef INET_ONLY
   1615 
   1616 void
   1617 at_status(force)
   1618 	int force;
   1619 {
   1620 	struct sockaddr_at *sat, null_sat;
   1621 	struct netrange *nr;
   1622 
   1623 	getsock(AF_APPLETALK);
   1624 	if (s < 0) {
   1625 		if (errno == EPROTONOSUPPORT)
   1626 			return;
   1627 		err(1, "socket");
   1628 	}
   1629 	(void) memset(&ifr, 0, sizeof(ifr));
   1630 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1631 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   1632 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1633 			if (!force)
   1634 				return;
   1635 			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1636 		} else
   1637 			warn("SIOCGIFADDR");
   1638 	}
   1639 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   1640 	sat = (struct sockaddr_at *)&ifr.ifr_addr;
   1641 
   1642 	(void) memset(&null_sat, 0, sizeof(null_sat));
   1643 
   1644 	nr = (struct netrange *) &sat->sat_zero;
   1645 	printf("\tatalk %d.%d range %d-%d phase %d",
   1646 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
   1647 	    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
   1648 	if (flags & IFF_POINTOPOINT) {
   1649 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   1650 			if (errno == EADDRNOTAVAIL)
   1651 			    (void) memset(&ifr.ifr_addr, 0,
   1652 				sizeof(ifr.ifr_addr));
   1653 			else
   1654 			    warn("SIOCGIFDSTADDR");
   1655 		}
   1656 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1657 		sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
   1658 		if (!sat)
   1659 			sat = &null_sat;
   1660 		printf("--> %d.%d",
   1661 		    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
   1662 	}
   1663 	if (flags & IFF_BROADCAST) {
   1664 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
   1665 		sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
   1666 		if (sat)
   1667 			printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
   1668 			    sat->sat_addr.s_node);
   1669 	}
   1670 	putchar('\n');
   1671 }
   1672 
   1673 void
   1674 xns_status(force)
   1675 	int force;
   1676 {
   1677 	struct sockaddr_ns *sns;
   1678 
   1679 	getsock(AF_NS);
   1680 	if (s < 0) {
   1681 		if (errno == EPROTONOSUPPORT)
   1682 			return;
   1683 		err(1, "socket");
   1684 	}
   1685 	(void) memset(&ifr, 0, sizeof(ifr));
   1686 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1687 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   1688 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1689 			if (!force)
   1690 				return;
   1691 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1692 		} else
   1693 			warn("SIOCGIFADDR");
   1694 	}
   1695 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   1696 	sns = (struct sockaddr_ns *)&ifr.ifr_addr;
   1697 	printf("\tns %s ", ns_ntoa(sns->sns_addr));
   1698 	if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
   1699 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   1700 			if (errno == EADDRNOTAVAIL)
   1701 			    memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1702 			else
   1703 			    warn("SIOCGIFDSTADDR");
   1704 		}
   1705 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1706 		sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
   1707 		printf("--> %s ", ns_ntoa(sns->sns_addr));
   1708 	}
   1709 	putchar('\n');
   1710 }
   1711 
   1712 void
   1713 iso_status(force)
   1714 	int force;
   1715 {
   1716 	struct sockaddr_iso *siso;
   1717 	struct iso_ifreq ifr;
   1718 
   1719 	getsock(AF_ISO);
   1720 	if (s < 0) {
   1721 		if (errno == EPROTONOSUPPORT)
   1722 			return;
   1723 		err(1, "socket");
   1724 	}
   1725 	(void) memset(&ifr, 0, sizeof(ifr));
   1726 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1727 	if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&ifr) < 0) {
   1728 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1729 			if (!force)
   1730 				return;
   1731 			(void) memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   1732 		} else
   1733 			warn("SIOCGIFADDR_ISO");
   1734 	}
   1735 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   1736 	siso = &ifr.ifr_Addr;
   1737 	printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
   1738 	if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&ifr) < 0) {
   1739 		if (errno == EADDRNOTAVAIL)
   1740 			memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   1741 		else
   1742 			warn("SIOCGIFNETMASK_ISO");
   1743 	} else {
   1744 		if (siso->siso_len > offsetof(struct sockaddr_iso, siso_addr))
   1745 			siso->siso_addr.isoa_len = siso->siso_len
   1746 			    - offsetof(struct sockaddr_iso, siso_addr);
   1747 		printf("\n\t\tnetmask %s ", iso_ntoa(&siso->siso_addr));
   1748 	}
   1749 	if (flags & IFF_POINTOPOINT) {
   1750 		if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&ifr) < 0) {
   1751 			if (errno == EADDRNOTAVAIL)
   1752 			    memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   1753 			else
   1754 			    warn("SIOCGIFDSTADDR_ISO");
   1755 		}
   1756 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1757 		siso = &ifr.ifr_Addr;
   1758 		printf("--> %s ", iso_ntoa(&siso->siso_addr));
   1759 	}
   1760 	putchar('\n');
   1761 }
   1762 
   1763 #endif	/* INET_ONLY */
   1764 
   1765 #define SIN(x) ((struct sockaddr_in *) &(x))
   1766 struct sockaddr_in *sintab[] = {
   1767 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
   1768 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
   1769 
   1770 void
   1771 in_getaddr(s, which)
   1772 	char *s;
   1773 	int which;
   1774 {
   1775 	struct sockaddr_in *sin = sintab[which];
   1776 	struct hostent *hp;
   1777 	struct netent *np;
   1778 
   1779 	sin->sin_len = sizeof(*sin);
   1780 	if (which != MASK)
   1781 		sin->sin_family = AF_INET;
   1782 
   1783 	if (inet_aton(s, &sin->sin_addr) == 0) {
   1784 		if ((hp = gethostbyname(s)) != NULL)
   1785 			(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
   1786 		else if ((np = getnetbyname(s)) != NULL)
   1787 			sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
   1788 		else
   1789 			errx(1, "%s: bad value", s);
   1790 	}
   1791 }
   1792 
   1793 /*
   1794  * Print a value a la the %b format of the kernel's printf
   1795  */
   1796 void
   1797 printb(s, v, bits)
   1798 	char *s;
   1799 	char *bits;
   1800 	unsigned short v;
   1801 {
   1802 	int i, any = 0;
   1803 	char c;
   1804 
   1805 	if (bits && *bits == 8)
   1806 		printf("%s=%o", s, v);
   1807 	else
   1808 		printf("%s=%x", s, v);
   1809 	bits++;
   1810 	if (bits) {
   1811 		putchar('<');
   1812 		while ((i = *bits++) != 0) {
   1813 			if (v & (1 << (i-1))) {
   1814 				if (any)
   1815 					putchar(',');
   1816 				any = 1;
   1817 				for (; (c = *bits) > 32; bits++)
   1818 					putchar(c);
   1819 			} else
   1820 				for (; *bits > 32; bits++)
   1821 					;
   1822 		}
   1823 		putchar('>');
   1824 	}
   1825 }
   1826 
   1827 #ifdef INET6
   1828 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
   1829 struct sockaddr_in6 *sin6tab[] = {
   1830 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
   1831 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
   1832 
   1833 void
   1834 in6_getaddr(s, which)
   1835 	char *s;
   1836 	int which;
   1837 {
   1838 	struct sockaddr_in6 *sin = sin6tab[which];
   1839 
   1840 	sin->sin6_len = sizeof(*sin);
   1841 	if (which != MASK)
   1842 		sin->sin6_family = AF_INET6;
   1843 
   1844 	if (inet_pton(AF_INET6, s, &sin->sin6_addr) <= 0)
   1845 		errx(1, "%s: bad value", s);
   1846 }
   1847 
   1848 void
   1849 in6_getprefix(plen, which)
   1850 	char *plen;
   1851 	int which;
   1852 {
   1853 	register struct sockaddr_in6 *sin = sin6tab[which];
   1854 	register u_char *cp;
   1855 	int len = strtol(plen, (char **)NULL, 10);
   1856 
   1857 	if ((len < 0) || (len > 128))
   1858 		errx(1, "%s: bad value", plen);
   1859 	sin->sin6_len = sizeof(*sin);
   1860 	if (which != MASK)
   1861 		sin->sin6_family = AF_INET6;
   1862 	if ((len == 0) || (len == 128)) {
   1863 		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
   1864 		return;
   1865 	}
   1866 	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
   1867 	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
   1868 		*cp++ = 0xff;
   1869 	*cp = 0xff << (8 - len);
   1870 }
   1871 
   1872 int
   1873 prefix(val, size)
   1874 	void *val;
   1875 	int size;
   1876 {
   1877 	register u_char *name = (u_char *)val;
   1878 	register int byte, bit, plen = 0;
   1879 
   1880 	for (byte = 0; byte < size; byte++, plen += 8)
   1881 		if (name[byte] != 0xff)
   1882 			break;
   1883 	if (byte == size)
   1884 		return (plen);
   1885 	for (bit = 7; bit != 0; bit--, plen++)
   1886 		if (!(name[byte] & (1 << bit)))
   1887 			break;
   1888 	for (; bit != 0; bit--)
   1889 		if (name[byte] & (1 << bit))
   1890 			return(0);
   1891 	byte++;
   1892 	for (; byte < size; byte++)
   1893 		if (name[byte])
   1894 			return(0);
   1895 	return (plen);
   1896 }
   1897 #endif /*INET6*/
   1898 
   1899 #ifndef INET_ONLY
   1900 void
   1901 at_getaddr(addr, which)
   1902 	char *addr;
   1903 	int which;
   1904 {
   1905 	struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
   1906 	u_int net, node;
   1907 
   1908 	sat->sat_family = AF_APPLETALK;
   1909 	sat->sat_len = sizeof(*sat);
   1910 	if (which == MASK)
   1911 		errx(1, "AppleTalk does not use netmasks\n");
   1912 	if (sscanf(addr, "%u.%u", &net, &node) != 2
   1913 	    || net == 0 || net > 0xffff || node == 0 || node > 0xfe)
   1914 		errx(1, "%s: illegal address", addr);
   1915 	sat->sat_addr.s_net = htons(net);
   1916 	sat->sat_addr.s_node = node;
   1917 }
   1918 
   1919 void
   1920 setatrange(range, d)
   1921 	char *range;
   1922 	int d;
   1923 {
   1924 	u_short	first = 123, last = 123;
   1925 
   1926 	if (sscanf(range, "%hu-%hu", &first, &last) != 2
   1927 	    || first == 0 || first > 0xffff
   1928 	    || last == 0 || last > 0xffff || first > last)
   1929 		errx(1, "%s: illegal net range: %u-%u", range, first, last);
   1930 	at_nr.nr_firstnet = htons(first);
   1931 	at_nr.nr_lastnet = htons(last);
   1932 }
   1933 
   1934 void
   1935 setatphase(phase, d)
   1936 	char *phase;
   1937 	int d;
   1938 {
   1939 	if (!strcmp(phase, "1"))
   1940 		at_nr.nr_phase = 1;
   1941 	else if (!strcmp(phase, "2"))
   1942 		at_nr.nr_phase = 2;
   1943 	else
   1944 		errx(1, "%s: illegal phase", phase);
   1945 }
   1946 
   1947 void
   1948 checkatrange(sat)
   1949 	struct sockaddr_at *sat;
   1950 {
   1951 	if (at_nr.nr_phase == 0)
   1952 		at_nr.nr_phase = 2;	/* Default phase 2 */
   1953 	if (at_nr.nr_firstnet == 0)
   1954 		at_nr.nr_firstnet =	/* Default range of one */
   1955 		at_nr.nr_lastnet = sat->sat_addr.s_net;
   1956 	printf("\tatalk %d.%d range %d-%d phase %d\n",
   1957 	ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
   1958 	ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
   1959 	if ((u_short) ntohs(at_nr.nr_firstnet) >
   1960 			(u_short) ntohs(sat->sat_addr.s_net)
   1961 		    || (u_short) ntohs(at_nr.nr_lastnet) <
   1962 			(u_short) ntohs(sat->sat_addr.s_net))
   1963 		errx(1, "AppleTalk address is not in range");
   1964 	*((struct netrange *) &sat->sat_zero) = at_nr;
   1965 }
   1966 
   1967 #define SNS(x) ((struct sockaddr_ns *) &(x))
   1968 struct sockaddr_ns *snstab[] = {
   1969 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
   1970 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
   1971 
   1972 void
   1973 xns_getaddr(addr, which)
   1974 	char *addr;
   1975 	int which;
   1976 {
   1977 	struct sockaddr_ns *sns = snstab[which];
   1978 
   1979 	sns->sns_family = AF_NS;
   1980 	sns->sns_len = sizeof(*sns);
   1981 	sns->sns_addr = ns_addr(addr);
   1982 	if (which == MASK)
   1983 		puts("Attempt to set XNS netmask will be ineffectual");
   1984 }
   1985 
   1986 #define SISO(x) ((struct sockaddr_iso *) &(x))
   1987 struct sockaddr_iso *sisotab[] = {
   1988 SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
   1989 SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
   1990 
   1991 void
   1992 iso_getaddr(addr, which)
   1993 	char *addr;
   1994 	int which;
   1995 {
   1996 	struct sockaddr_iso *siso = sisotab[which];
   1997 	siso->siso_addr = *iso_addr(addr);
   1998 
   1999 	if (which == MASK) {
   2000 		siso->siso_len = TSEL(siso) - (caddr_t)(siso);
   2001 		siso->siso_nlen = 0;
   2002 	} else {
   2003 		siso->siso_len = sizeof(*siso);
   2004 		siso->siso_family = AF_ISO;
   2005 	}
   2006 }
   2007 
   2008 void
   2009 setsnpaoffset(val, d)
   2010 	char *val;
   2011 	int d;
   2012 {
   2013 	iso_addreq.ifra_snpaoffset = atoi(val);
   2014 }
   2015 
   2016 void
   2017 setnsellength(val, d)
   2018 	char *val;
   2019 	int d;
   2020 {
   2021 	nsellength = atoi(val);
   2022 	if (nsellength < 0)
   2023 		errx(1, "Negative NSEL length is absurd");
   2024 	if (afp == 0 || afp->af_af != AF_ISO)
   2025 		errx(1, "Setting NSEL length valid only for iso");
   2026 }
   2027 
   2028 void
   2029 fixnsel(s)
   2030 	struct sockaddr_iso *s;
   2031 {
   2032 	if (s->siso_family == 0)
   2033 		return;
   2034 	s->siso_tlen = nsellength;
   2035 }
   2036 
   2037 void
   2038 adjust_nsellength()
   2039 {
   2040 	fixnsel(sisotab[RIDADDR]);
   2041 	fixnsel(sisotab[ADDR]);
   2042 	fixnsel(sisotab[DSTADDR]);
   2043 }
   2044 
   2045 #endif	/* INET_ONLY */
   2046 
   2047 void
   2048 usage()
   2049 {
   2050 	fprintf(stderr,
   2051 	    "usage: ifconfig [ -m ] [ -A ] %s interface\n%s%s%s%s%s%s%s%s%s%s%s%s",
   2052 #ifdef INET6
   2053 		"[ -L ]",
   2054 #else
   2055 		"",
   2056 #endif
   2057 		"\t[ af [ address [ dest_addr ] ] [ up ] [ down ] ",
   2058 		"[ netmask mask ] ]\n",
   2059 		"\t[ metric n ]\n",
   2060 		"\t[ mtu n ]\n",
   2061 		"\t[ arp | -arp ]\n",
   2062 		"\t[ media mtype ]\n",
   2063 		"\t[ mediaopt mopts ]\n",
   2064 		"\t[ -mediaopt mopts ]\n",
   2065 		"\t[ instance minst ]\n",
   2066 		"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n",
   2067 		"       ifconfig -a [ -A ] [ -m ] [ -d ] [ -u ] [ af ]\n",
   2068 		"       ifconfig -l [ -d ] [ -u ]\n");
   2069 	exit(1);
   2070 }
   2071 
   2072 #ifdef INET6
   2073 char *
   2074 sec2str(total)
   2075 	time_t total;
   2076 {
   2077 	static char result[256];
   2078 	int days, hours, mins, secs;
   2079 	int first = 1;
   2080 	char *p = result;
   2081 
   2082 	if (0) {	/*XXX*/
   2083 		days = total / 3600 / 24;
   2084 		hours = (total / 3600) % 24;
   2085 		mins = (total / 60) % 60;
   2086 		secs = total % 60;
   2087 
   2088 		if (days) {
   2089 			first = 0;
   2090 			p += sprintf(p, "%dd", days);
   2091 		}
   2092 		if (!first || hours) {
   2093 			first = 0;
   2094 			p += sprintf(p, "%dh", hours);
   2095 		}
   2096 		if (!first || mins) {
   2097 			first = 0;
   2098 			p += sprintf(p, "%dm", mins);
   2099 		}
   2100 		sprintf(p, "%ds", secs);
   2101 	} else
   2102 		sprintf(p, "%lu", (u_long)total);
   2103 
   2104 	return(result);
   2105 }
   2106 #endif
   2107