Home | History | Annotate | Line # | Download | only in ifconfig
ifconfig.c revision 1.41
      1 /*	$NetBSD: ifconfig.c,v 1.41 1998/08/05 23:35:17 thorpej 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.41 1998/08/05 23:35:17 thorpej 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 <arpa/inet.h>
     96 
     97 #include <netatalk/at.h>
     98 
     99 #define	NSIP
    100 #include <netns/ns.h>
    101 #include <netns/ns_if.h>
    102 #include <netdb.h>
    103 
    104 #define EON
    105 #include <netiso/iso.h>
    106 #include <netiso/iso_var.h>
    107 #include <sys/protosw.h>
    108 
    109 #include <ctype.h>
    110 #include <err.h>
    111 #include <errno.h>
    112 #include <stdio.h>
    113 #include <stdlib.h>
    114 #include <string.h>
    115 #include <unistd.h>
    116 
    117 struct	ifreq		ifr, ridreq;
    118 struct	ifaliasreq	addreq __attribute__((aligned(4)));
    119 struct	iso_aliasreq	iso_addreq;
    120 struct	sockaddr_in	netmask;
    121 struct	netrange	at_nr;		/* AppleTalk net range */
    122 
    123 char	name[30];
    124 int	flags, metric, mtu, setaddr, setipdst, doalias;
    125 int	clearaddr, s;
    126 int	newaddr = -1;
    127 int	nsellength = 1;
    128 int	af;
    129 int	dflag, mflag, lflag, uflag;
    130 int	reset_if_flags;
    131 
    132 void 	notealias __P((char *, int));
    133 void 	notrailers __P((char *, int));
    134 void 	setifaddr __P((char *, int));
    135 void 	setifdstaddr __P((char *, int));
    136 void 	setifflags __P((char *, int));
    137 void 	setifbroadaddr __P((char *, int));
    138 void 	setifipdst __P((char *, int));
    139 void 	setifmetric __P((char *, int));
    140 void 	setifmtu __P((char *, int));
    141 void 	setifnetmask __P((char *, int));
    142 void 	setnsellength __P((char *, int));
    143 void 	setsnpaoffset __P((char *, int));
    144 void	setatrange __P((char *, int));
    145 void	setatphase __P((char *, int));
    146 void	checkatrange __P ((struct sockaddr_at *));
    147 void	setmedia __P((char *, int));
    148 void	setmediaopt __P((char *, int));
    149 void	unsetmediaopt __P((char *, int));
    150 void	fixnsel __P((struct sockaddr_iso *));
    151 int	main __P((int, char *[]));
    152 
    153 #define	NEXTARG		0xffffff
    154 
    155 struct	cmd {
    156 	char	*c_name;
    157 	int	c_parameter;		/* NEXTARG means next argv */
    158 	void	(*c_func) __P((char *, int));
    159 } cmds[] = {
    160 	{ "up",		IFF_UP,		setifflags } ,
    161 	{ "down",	-IFF_UP,	setifflags },
    162 	{ "trailers",	-1,		notrailers },
    163 	{ "-trailers",	1,		notrailers },
    164 	{ "arp",	-IFF_NOARP,	setifflags },
    165 	{ "-arp",	IFF_NOARP,	setifflags },
    166 	{ "debug",	IFF_DEBUG,	setifflags },
    167 	{ "-debug",	-IFF_DEBUG,	setifflags },
    168 	{ "alias",	IFF_UP,		notealias },
    169 	{ "-alias",	-IFF_UP,	notealias },
    170 	{ "delete",	-IFF_UP,	notealias },
    171 #ifdef notdef
    172 #define	EN_SWABIPS	0x1000
    173 	{ "swabips",	EN_SWABIPS,	setifflags },
    174 	{ "-swabips",	-EN_SWABIPS,	setifflags },
    175 #endif
    176 	{ "netmask",	NEXTARG,	setifnetmask },
    177 	{ "metric",	NEXTARG,	setifmetric },
    178 	{ "mtu",	NEXTARG,	setifmtu },
    179 	{ "broadcast",	NEXTARG,	setifbroadaddr },
    180 	{ "ipdst",	NEXTARG,	setifipdst },
    181 #ifndef INET_ONLY
    182 	{ "range",	NEXTARG,	setatrange },
    183 	{ "phase",	NEXTARG,	setatphase },
    184 	{ "snpaoffset",	NEXTARG,	setsnpaoffset },
    185 	{ "nsellength",	NEXTARG,	setnsellength },
    186 #endif	/* INET_ONLY */
    187 	{ "link0",	IFF_LINK0,	setifflags } ,
    188 	{ "-link0",	-IFF_LINK0,	setifflags } ,
    189 	{ "link1",	IFF_LINK1,	setifflags } ,
    190 	{ "-link1",	-IFF_LINK1,	setifflags } ,
    191 	{ "link2",	IFF_LINK2,	setifflags } ,
    192 	{ "-link2",	-IFF_LINK2,	setifflags } ,
    193 	{ "media",	NEXTARG,	setmedia },
    194 	{ "mediaopt",	NEXTARG,	setmediaopt },
    195 	{ "-mediaopt",	NEXTARG,	unsetmediaopt },
    196 	{ 0,		0,		setifaddr },
    197 	{ 0,		0,		setifdstaddr },
    198 };
    199 
    200 void 	adjust_nsellength __P((void));
    201 int	getinfo __P((struct ifreq *));
    202 void	getsock __P((int));
    203 void	printall __P((void));
    204 void 	printb __P((char *, unsigned short, char *));
    205 void 	status __P((const u_int8_t *, int));
    206 void 	usage __P((void));
    207 
    208 void	domediaopt __P((char *, int));
    209 int	get_media_subtype __P((int, char *));
    210 int	get_media_options __P((int, char *));
    211 int	lookup_media_word __P((struct ifmedia_description *, char *));
    212 void	print_media_word __P((int));
    213 
    214 /*
    215  * XNS support liberally adapted from code written at the University of
    216  * Maryland principally by James O'Toole and Chris Torek.
    217  */
    218 void	in_status __P((int));
    219 void 	in_getaddr __P((char *, int));
    220 void	at_status __P((int));
    221 void	at_getaddr __P((char *, int));
    222 void 	xns_status __P((int));
    223 void 	xns_getaddr __P((char *, int));
    224 void 	iso_status __P((int));
    225 void 	iso_getaddr __P((char *, int));
    226 
    227 /* Known address families */
    228 struct afswtch {
    229 	char *af_name;
    230 	short af_af;
    231 	void (*af_status) __P((int));
    232 	void (*af_getaddr) __P((char *, int));
    233 	u_long af_difaddr;
    234 	u_long af_aifaddr;
    235 	caddr_t af_ridreq;
    236 	caddr_t af_addreq;
    237 } afs[] = {
    238 #define C(x) ((caddr_t) &x)
    239 	{ "inet", AF_INET, in_status, in_getaddr,
    240 	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
    241 #ifndef INET_ONLY	/* small version, for boot media */
    242 	{ "atalk", AF_APPLETALK, at_status, at_getaddr,
    243 	     SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
    244 	{ "ns", AF_NS, xns_status, xns_getaddr,
    245 	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
    246 	{ "iso", AF_ISO, iso_status, iso_getaddr,
    247 	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(iso_addreq) },
    248 #endif	/* INET_ONLY */
    249 	{ 0,	0,	    0,		0 }
    250 };
    251 
    252 struct afswtch *afp;	/*the address family being set or asked about*/
    253 
    254 struct afswtch *lookup_af __P((const char *));
    255 
    256 int
    257 main(argc, argv)
    258 	int argc;
    259 	char *argv[];
    260 {
    261 	int ch, aflag;
    262 
    263 	/* Parse command-line options */
    264 	aflag = mflag = 0;
    265 	while ((ch = getopt(argc, argv, "adlmu")) != -1) {
    266 		switch (ch) {
    267 		case 'a':
    268 			aflag = 1;
    269 			break;
    270 
    271 		case 'd':
    272 			dflag = 1;
    273 			break;
    274 
    275 		case 'l':
    276 			lflag = 1;
    277 			break;
    278 
    279 		case 'm':
    280 			mflag = 1;
    281 			break;
    282 
    283 		case 'u':
    284 			uflag = 1;
    285 			break;
    286 
    287 		default:
    288 			usage();
    289 			/* NOTREACHED */
    290 		}
    291 	}
    292 	argc -= optind;
    293 	argv += optind;
    294 
    295 	/*
    296 	 * -l means "list all interfaces", and is mutally exclusive with
    297 	 * all other flags/commands.
    298 	 *
    299 	 * -a means "print status of all interfaces".
    300 	 */
    301 	if (lflag && (aflag || mflag || argc))
    302 		usage();
    303 	if (aflag || lflag) {
    304 		if (argc > 1)
    305 			usage();
    306 		else if (argc == 1) {
    307 			afp = lookup_af(argv[0]);
    308 			if (afp == NULL)
    309 				usage();
    310 		}
    311 		if (afp)
    312 			af = ifr.ifr_addr.sa_family = afp->af_af;
    313 		else
    314 			af = ifr.ifr_addr.sa_family = afs[0].af_af;
    315 		printall();
    316 		exit(0);
    317 	}
    318 
    319 	/* Make sure there's an interface name. */
    320 	if (argc < 1)
    321 		usage();
    322 	(void) strncpy(name, argv[0], sizeof(name));
    323 	argc--; argv++;
    324 
    325 	/* Check for address family. */
    326 	afp = NULL;
    327 	if (argc > 0) {
    328 		afp = lookup_af(argv[0]);
    329 		if (afp != NULL) {
    330 			argv++;
    331 			argc--;
    332 		}
    333 	}
    334 
    335 	if (afp == NULL)
    336 		afp = afs;
    337 	af = ifr.ifr_addr.sa_family = afp->af_af;
    338 
    339 	/* Get information about the interface. */
    340 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    341 	if (getinfo(&ifr) < 0)
    342 		exit(1);
    343 
    344 	/* No more arguments means interface status. */
    345 	if (argc == 0) {
    346 		status(NULL, 0);
    347 		exit(0);
    348 	}
    349 
    350 	/* Process commands. */
    351 	while (argc > 0) {
    352 		struct cmd *p;
    353 
    354 		for (p = cmds; p->c_name; p++)
    355 			if (strcmp(argv[0], p->c_name) == 0)
    356 				break;
    357 		if (p->c_name == 0 && setaddr)
    358 			p++;	/* got src, do dst */
    359 		if (p->c_func) {
    360 			if (p->c_parameter == NEXTARG) {
    361 				if (argc < 2)
    362 					errx(1, "'%s' requires argument",
    363 					    p->c_name);
    364 				(*p->c_func)(argv[1], 0);
    365 				argc--, argv++;
    366 			} else
    367 				(*p->c_func)(argv[0], p->c_parameter);
    368 		}
    369 		argc--, argv++;
    370 	}
    371 
    372 #ifndef INET_ONLY
    373 
    374 	if (af == AF_ISO)
    375 		adjust_nsellength();
    376 
    377 	if (af == AF_APPLETALK)
    378 		checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
    379 
    380 	if (setipdst && af==AF_NS) {
    381 		struct nsip_req rq;
    382 		int size = sizeof(rq);
    383 
    384 		rq.rq_ns = addreq.ifra_addr;
    385 		rq.rq_ip = addreq.ifra_dstaddr;
    386 
    387 		if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
    388 			warn("encapsulation routing");
    389 	}
    390 
    391 #endif	/* INET_ONLY */
    392 
    393 	if (clearaddr) {
    394 		int ret;
    395 		(void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
    396 		if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
    397 			if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
    398 				/* means no previous address for interface */
    399 			} else
    400 				warn("SIOCDIFADDR");
    401 		}
    402 	}
    403 	if (newaddr > 0) {
    404 		(void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
    405 		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
    406 			warn("SIOCAIFADDR");
    407 	}
    408 	if (reset_if_flags && ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
    409 		err(1, "SIOCSIFFLAGS");
    410 	exit(0);
    411 }
    412 
    413 struct afswtch *
    414 lookup_af(cp)
    415 	const char *cp;
    416 {
    417 	struct afswtch *a;
    418 
    419 	for (a = afs; a->af_name != NULL; a++)
    420 		if (strcmp(a->af_name, cp) == 0)
    421 			return (a);
    422 	return (NULL);
    423 }
    424 
    425 void
    426 getsock(naf)
    427 	int naf;
    428 {
    429 	static int oaf = -1;
    430 
    431 	if (oaf == naf)
    432 		return;
    433 	if (oaf != -1)
    434 		close(s);
    435 	s = socket(naf, SOCK_DGRAM, 0);
    436 	if (s < 0)
    437 		oaf = -1;
    438 	else
    439 		oaf = naf;
    440 }
    441 
    442 int
    443 getinfo(ifr)
    444 	struct ifreq *ifr;
    445 {
    446 
    447 	getsock(af);
    448 	if (s < 0)
    449 		err(1, "socket");
    450 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
    451 		warn("SIOCGIFFLAGS %s", ifr->ifr_name);
    452 		return (-1);
    453 	}
    454 	flags = ifr->ifr_flags;
    455 	if (ioctl(s, SIOCGIFMETRIC, (caddr_t)ifr) < 0) {
    456 		warn("SIOCGIFMETRIC %s", ifr->ifr_name);
    457 		metric = 0;
    458 	} else
    459 		metric = ifr->ifr_metric;
    460 	if (ioctl(s, SIOCGIFMTU, (caddr_t)ifr) < 0)
    461 		mtu = 0;
    462 	else
    463 		mtu = ifr->ifr_mtu;
    464 	return (0);
    465 }
    466 
    467 void
    468 printall()
    469 {
    470 	char inbuf[8192];
    471 	const struct sockaddr_dl *sdl = NULL;
    472 	struct ifconf ifc;
    473 	struct ifreq ifreq, *ifr;
    474 	int i, idx;
    475 
    476 	ifc.ifc_len = sizeof(inbuf);
    477 	ifc.ifc_buf = inbuf;
    478 	getsock(af);
    479 	if (s < 0)
    480 		err(1, "socket");
    481 	if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
    482 		err(1, "SIOCGIFCONF");
    483 	ifr = ifc.ifc_req;
    484 	ifreq.ifr_name[0] = '\0';
    485 	for (i = 0, idx = 0; i < ifc.ifc_len; ) {
    486 		ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
    487 		i += sizeof(ifr->ifr_name) +
    488 			(ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
    489 				? ifr->ifr_addr.sa_len
    490 				: sizeof(struct sockaddr));
    491 		if (ifr->ifr_addr.sa_family == AF_LINK)
    492 			sdl = (const struct sockaddr_dl *) &ifr->ifr_addr;
    493 		if (!strncmp(ifreq.ifr_name, ifr->ifr_name,
    494 			     sizeof(ifr->ifr_name)))
    495 			continue;
    496 		(void) strncpy(name, ifr->ifr_name, sizeof(ifr->ifr_name));
    497 		ifreq = *ifr;
    498 
    499 		if (getinfo(&ifreq) < 0)
    500 			continue;
    501 		if (dflag && (flags & IFF_UP) != 0)
    502 			continue;
    503 		if (uflag && (flags & IFF_UP) == 0)
    504 			continue;
    505 
    506 		idx++;
    507 		/*
    508 		 * Are we just listing the interfaces?
    509 		 */
    510 		if (lflag) {
    511 			if (idx > 1)
    512 				putchar(' ');
    513 			fputs(name, stdout);
    514 			continue;
    515 		}
    516 
    517 		if (sdl == NULL) {
    518 			status(NULL, 0);
    519 		} else {
    520 			status(LLADDR(sdl), sdl->sdl_alen);
    521 			sdl = NULL;
    522 		}
    523 	}
    524 	if (lflag)
    525 		putchar('\n');
    526 }
    527 
    528 #define RIDADDR 0
    529 #define ADDR	1
    530 #define MASK	2
    531 #define DSTADDR	3
    532 
    533 /*ARGSUSED*/
    534 void
    535 setifaddr(addr, param)
    536 	char *addr;
    537 	int param;
    538 {
    539 	/*
    540 	 * Delay the ioctl to set the interface addr until flags are all set.
    541 	 * The address interpretation may depend on the flags,
    542 	 * and the flags may change when the address is set.
    543 	 */
    544 	setaddr++;
    545 	if (newaddr == -1)
    546 		newaddr = 1;
    547 	if (doalias == 0)
    548 		clearaddr = 1;
    549 	(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
    550 }
    551 
    552 void
    553 setifnetmask(addr, d)
    554 	char *addr;
    555 	int d;
    556 {
    557 	(*afp->af_getaddr)(addr, MASK);
    558 }
    559 
    560 void
    561 setifbroadaddr(addr, d)
    562 	char *addr;
    563 	int d;
    564 {
    565 	(*afp->af_getaddr)(addr, DSTADDR);
    566 }
    567 
    568 void
    569 setifipdst(addr, d)
    570 	char *addr;
    571 	int d;
    572 {
    573 	in_getaddr(addr, DSTADDR);
    574 	setipdst++;
    575 	clearaddr = 0;
    576 	newaddr = 0;
    577 }
    578 
    579 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
    580 /*ARGSUSED*/
    581 void
    582 notealias(addr, param)
    583 	char *addr;
    584 	int param;
    585 {
    586 	if (setaddr && doalias == 0 && param < 0)
    587 		(void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
    588 		    rqtosa(af_addreq)->sa_len);
    589 	doalias = param;
    590 	if (param < 0) {
    591 		clearaddr = 1;
    592 		newaddr = 0;
    593 	} else
    594 		clearaddr = 0;
    595 }
    596 
    597 /*ARGSUSED*/
    598 void
    599 notrailers(vname, value)
    600 	char *vname;
    601 	int value;
    602 {
    603 	puts("Note: trailers are no longer sent, but always received");
    604 }
    605 
    606 /*ARGSUSED*/
    607 void
    608 setifdstaddr(addr, param)
    609 	char *addr;
    610 	int param;
    611 {
    612 	(*afp->af_getaddr)(addr, DSTADDR);
    613 }
    614 
    615 void
    616 setifflags(vname, value)
    617 	char *vname;
    618 	int value;
    619 {
    620  	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0)
    621 		err(1, "SIOCGIFFLAGS");
    622 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
    623  	flags = ifr.ifr_flags;
    624 
    625 	if (value < 0) {
    626 		value = -value;
    627 		flags &= ~value;
    628 	} else
    629 		flags |= value;
    630 	ifr.ifr_flags = flags;
    631 	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
    632 		err(1, "SIOCSIFFLAGS");
    633 
    634 	reset_if_flags = 1;
    635 }
    636 
    637 void
    638 setifmetric(val, d)
    639 	char *val;
    640 	int d;
    641 {
    642 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
    643 	ifr.ifr_metric = atoi(val);
    644 	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
    645 		warn("SIOCSIFMETRIC");
    646 }
    647 
    648 void
    649 setifmtu(val, d)
    650 	char *val;
    651 	int d;
    652 {
    653 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    654 	ifr.ifr_mtu = atoi(val);
    655 	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
    656 		warn("SIOCSIFMTU");
    657 }
    658 
    659 void
    660 setmedia(val, d)
    661 	char *val;
    662 	int d;
    663 {
    664 	struct ifmediareq ifmr;
    665 	int first_type, subtype;
    666 
    667 	(void) memset(&ifmr, 0, sizeof(ifmr));
    668 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
    669 
    670 	ifmr.ifm_count = 1;
    671 	ifmr.ifm_ulist = &first_type;
    672 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
    673 		/*
    674 		 * If we get E2BIG, the kernel is telling us
    675 		 * that there are more, so we can ignore it.
    676 		 */
    677 		if (errno != E2BIG)
    678 			err(1, "SIOCGIFMEDIA");
    679 	}
    680 
    681 	if (ifmr.ifm_count == 0)
    682 		errx(1, "%s: no media types?", name);
    683 
    684 	/*
    685 	 * We are primarily concerned with the top-level type.
    686 	 * However, "current" may be only IFM_NONE, so we just look
    687 	 * for the top-level type in the first "supported type"
    688 	 * entry.
    689 	 *
    690 	 * (I'm assuming that all supported media types for a given
    691 	 * interface will be the same top-level type..)
    692 	 */
    693 	subtype = get_media_subtype(IFM_TYPE(first_type), val);
    694 
    695 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    696 	ifr.ifr_media = (ifmr.ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
    697 	    IFM_TYPE(first_type) | subtype;
    698 
    699 	if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
    700 		err(1, "SIOCSIFMEDIA");
    701 }
    702 
    703 void
    704 setmediaopt(val, d)
    705 	char *val;
    706 	int d;
    707 {
    708 
    709 	domediaopt(val, 0);
    710 }
    711 
    712 void
    713 unsetmediaopt(val, d)
    714 	int d;
    715 	char *val;
    716 {
    717 
    718 	domediaopt(val, 1);
    719 }
    720 
    721 void
    722 domediaopt(val, clear)
    723 	char *val;
    724 	int clear;
    725 {
    726 	struct ifmediareq ifmr;
    727 	int *mwords, options;
    728 
    729 	(void) memset(&ifmr, 0, sizeof(ifmr));
    730 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
    731 
    732 	/*
    733 	 * We must go through the motions of reading all
    734 	 * supported media because we need to know both
    735 	 * the current media type and the top-level type.
    736 	 */
    737 
    738 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
    739 		err(1, "SIOCGIFMEDIA");
    740 
    741 	if (ifmr.ifm_count == 0)
    742 		errx(1, "%s: no media types?", name);
    743 
    744 	mwords = (int *)malloc(ifmr.ifm_count * sizeof(int));
    745 	if (mwords == NULL)
    746 		err(1, "malloc");
    747 
    748 	ifmr.ifm_ulist = mwords;
    749 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
    750 		err(1, "SIOCGIFMEDIA");
    751 
    752 	options = get_media_options(IFM_TYPE(mwords[0]), val);
    753 
    754 	free(mwords);
    755 
    756 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    757 	ifr.ifr_media = ifmr.ifm_current;
    758 	if (clear)
    759 		ifr.ifr_media &= ~options;
    760 	else
    761 		ifr.ifr_media |= options;
    762 
    763 	if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
    764 		err(1, "SIOCSIFMEDIA");
    765 }
    766 
    767 /**********************************************************************
    768  * A good chunk of this is duplicated from sys/net/ifmedia.c
    769  **********************************************************************/
    770 
    771 struct ifmedia_description ifm_type_descriptions[] =
    772     IFM_TYPE_DESCRIPTIONS;
    773 
    774 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
    775     IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
    776 
    777 struct ifmedia_description ifm_subtype_ethernet_aliases[] =
    778     IFM_SUBTYPE_ETHERNET_ALIASES;
    779 
    780 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
    781     IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
    782 
    783 struct ifmedia_description ifm_subtype_tokenring_descriptions[] =
    784     IFM_SUBTYPE_TOKENRING_DESCRIPTIONS;
    785 
    786 struct ifmedia_description ifm_subtype_tokenring_aliases[] =
    787     IFM_SUBTYPE_TOKENRING_ALIASES;
    788 
    789 struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] =
    790     IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS;
    791 
    792 struct ifmedia_description ifm_subtype_fddi_descriptions[] =
    793     IFM_SUBTYPE_FDDI_DESCRIPTIONS;
    794 
    795 struct ifmedia_description ifm_subtype_fddi_aliases[] =
    796     IFM_SUBTYPE_FDDI_ALIASES;
    797 
    798 struct ifmedia_description ifm_subtype_fddi_option_descriptions[] =
    799     IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS;
    800 
    801 struct ifmedia_description ifm_subtype_shared_descriptions[] =
    802     IFM_SUBTYPE_SHARED_DESCRIPTIONS;
    803 
    804 struct ifmedia_description ifm_subtype_shared_aliases[] =
    805     IFM_SUBTYPE_SHARED_ALIASES;
    806 
    807 struct ifmedia_description ifm_shared_option_descriptions[] =
    808     IFM_SHARED_OPTION_DESCRIPTIONS;
    809 
    810 struct ifmedia_type_to_subtype {
    811 	struct {
    812 		struct ifmedia_description *desc;
    813 		int alias;
    814 	} subtypes[5];
    815 	struct {
    816 		struct ifmedia_description *desc;
    817 		int alias;
    818 	} options[3];
    819 };
    820 
    821 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
    822 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
    823 	{
    824 		{
    825 			{ &ifm_subtype_shared_descriptions[0], 0 },
    826 			{ &ifm_subtype_shared_aliases[0], 1 },
    827 			{ &ifm_subtype_ethernet_descriptions[0], 0 },
    828 			{ &ifm_subtype_ethernet_aliases[0], 1 },
    829 			{ NULL, 0 },
    830 		},
    831 		{
    832 			{ &ifm_shared_option_descriptions[0], 0 },
    833 			{ &ifm_subtype_ethernet_option_descriptions[0], 1 },
    834 			{ NULL, 0 },
    835 		},
    836 	},
    837 	{
    838 		{
    839 			{ &ifm_subtype_shared_descriptions[0], 0 },
    840 			{ &ifm_subtype_shared_aliases[0], 1 },
    841 			{ &ifm_subtype_tokenring_descriptions[0], 0 },
    842 			{ &ifm_subtype_tokenring_aliases[0], 1 },
    843 			{ NULL, 0 },
    844 		},
    845 		{
    846 			{ &ifm_shared_option_descriptions[0], 0 },
    847 			{ &ifm_subtype_tokenring_option_descriptions[0], 1 },
    848 			{ NULL, 0 },
    849 		},
    850 	},
    851 	{
    852 		{
    853 			{ &ifm_subtype_shared_descriptions[0], 0 },
    854 			{ &ifm_subtype_shared_aliases[0], 1 },
    855 			{ &ifm_subtype_fddi_descriptions[0], 0 },
    856 			{ &ifm_subtype_fddi_aliases[0], 1 },
    857 			{ NULL, 0 },
    858 		},
    859 		{
    860 			{ &ifm_shared_option_descriptions[0], 0 },
    861 			{ &ifm_subtype_fddi_option_descriptions[0], 1 },
    862 			{ NULL, 0 },
    863 		},
    864 	},
    865 };
    866 
    867 int
    868 get_media_subtype(type, val)
    869 	int type;
    870 	char *val;
    871 {
    872 	struct ifmedia_description *desc;
    873 	struct ifmedia_type_to_subtype *ttos;
    874 	int rval, i;
    875 
    876 	/* Find the top-level interface type. */
    877 	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
    878 	    desc->ifmt_string != NULL; desc++, ttos++)
    879 		if (type == desc->ifmt_word)
    880 			break;
    881 	if (desc->ifmt_string == NULL)
    882 		errx(1, "unknown media type 0x%x", type);
    883 
    884 	for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
    885 		rval = lookup_media_word(ttos->subtypes[i].desc, val);
    886 		if (rval != -1)
    887 			return (rval);
    888 	}
    889 	errx(1, "unknown media subtype: %s", val);
    890 	/* NOTREACHED */
    891 }
    892 
    893 int
    894 get_media_options(type, val)
    895 	int type;
    896 	char *val;
    897 {
    898 	struct ifmedia_description *desc;
    899 	struct ifmedia_type_to_subtype *ttos;
    900 	char *optlist;
    901 	int option = 0, i, rval = 0;
    902 
    903 	/* We muck with the string, so copy it. */
    904 	optlist = strdup(val);
    905 	if (optlist == NULL)
    906 		err(1, "strdup");
    907 	val = optlist;
    908 
    909 	/* Find the top-level interface type. */
    910 	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
    911 	    desc->ifmt_string != NULL; desc++, ttos++)
    912 		if (type == desc->ifmt_word)
    913 			break;
    914 	if (desc->ifmt_string == NULL)
    915 		errx(1, "unknown media type 0x%x", type);
    916 
    917 	/*
    918 	 * Look up the options in the user-provided comma-separated
    919 	 * list.
    920 	 */
    921 	for (; (val = strtok(val, ",")) != NULL; val = NULL) {
    922 		for (i = 0; ttos->options[i].desc != NULL; i++) {
    923 			option = lookup_media_word(ttos->options[i].desc, val);
    924 			if (option != -1)
    925 				break;
    926 		}
    927 		if (option == 0)
    928 			errx(1, "unknown option: %s", val);
    929 		rval |= option;
    930 	}
    931 
    932 	free(optlist);
    933 	return (rval);
    934 }
    935 
    936 int
    937 lookup_media_word(desc, val)
    938 	struct ifmedia_description *desc;
    939 	char *val;
    940 {
    941 
    942 	for (; desc->ifmt_string != NULL; desc++)
    943 		if (strcasecmp(desc->ifmt_string, val) == 0)
    944 			return (desc->ifmt_word);
    945 
    946 	return (-1);
    947 }
    948 
    949 void
    950 print_media_word(ifmw)
    951 	int ifmw;
    952 {
    953 	struct ifmedia_description *desc;
    954 	struct ifmedia_type_to_subtype *ttos;
    955 	int seen_option = 0, i;
    956 
    957 	/* Find the top-level interface type. */
    958 	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
    959 	    desc->ifmt_string != NULL; desc++, ttos++)
    960 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
    961 			break;
    962 	if (desc->ifmt_string == NULL) {
    963 		printf("<unknown type>");
    964 		return;
    965 	}
    966 
    967 	/*
    968 	 * Don't print the top-level type; it's not like we can
    969 	 * change it, or anything.
    970 	 */
    971 
    972 	/* Find subtype. */
    973 	for (i = 0; ttos->subtypes[i].desc != NULL; i++) {
    974 		if (ttos->subtypes[i].alias)
    975 			continue;
    976 		for (desc = ttos->subtypes[i].desc;
    977 		    desc->ifmt_string != NULL; desc++) {
    978 			if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
    979 				goto got_subtype;
    980 		}
    981 	}
    982 
    983 	/* Falling to here means unknown subtype. */
    984 	printf("<unknown subtype>");
    985 	return;
    986 
    987  got_subtype:
    988 	printf("%s", desc->ifmt_string);
    989 
    990 	/* Find options. */
    991 	for (i = 0; ttos->options[i].desc != NULL; i++) {
    992 		if (ttos->options[i].alias)
    993 			continue;
    994 		for (desc = ttos->options[i].desc;
    995 		    desc->ifmt_string != NULL; desc++) {
    996 			if (ifmw & desc->ifmt_word) {
    997 				if (seen_option == 0)
    998 					printf(" <");
    999 				printf("%s%s", seen_option++ ? "," : "",
   1000 				    desc->ifmt_string);
   1001 			}
   1002 		}
   1003 	}
   1004 	printf("%s", seen_option ? ">" : "");
   1005 }
   1006 
   1007 /**********************************************************************
   1008  * ...until here.
   1009  **********************************************************************/
   1010 
   1011 #define	IFFBITS \
   1012 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
   1013 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
   1014 
   1015 /*
   1016  * Print the status of the interface.  If an address family was
   1017  * specified, show it and it only; otherwise, show them all.
   1018  */
   1019 void
   1020 status(ap, alen)
   1021 	const u_int8_t *ap;
   1022 	int alen;
   1023 {
   1024 	struct afswtch *p = afp;
   1025 	struct ifmediareq ifmr;
   1026 	int *media_list, i;
   1027 
   1028 	printf("%s: ", name);
   1029 	printb("flags", flags, IFFBITS);
   1030 	if (metric)
   1031 		printf(" metric %d", metric);
   1032 	if (mtu)
   1033 		printf(" mtu %d", mtu);
   1034 	putchar('\n');
   1035 	if (ap && alen > 0) {
   1036 		printf("\taddress:");
   1037 		for (i = 0; i < alen; i++, ap++)
   1038 			printf("%c%02x", i > 0 ? ':' : ' ', *ap);
   1039 		putchar('\n');
   1040 	}
   1041 
   1042 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1043 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1044 
   1045 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1046 		/*
   1047 		 * Interface doesn't support SIOC{G,S}IFMEDIA.
   1048 		 */
   1049 		goto proto_status;
   1050 	}
   1051 
   1052 	if (ifmr.ifm_count == 0) {
   1053 		warnx("%s: no media types?", name);
   1054 		goto proto_status;
   1055 	}
   1056 
   1057 	media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
   1058 	if (media_list == NULL)
   1059 		err(1, "malloc");
   1060 	ifmr.ifm_ulist = media_list;
   1061 
   1062 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
   1063 		err(1, "SIOCGIFMEDIA");
   1064 
   1065 	printf("\tmedia: ");
   1066 	print_media_word(ifmr.ifm_current);
   1067 	if (ifmr.ifm_active != ifmr.ifm_current) {
   1068 		putchar(' ');
   1069 		putchar('(');
   1070 		print_media_word(ifmr.ifm_active);
   1071 		putchar(')');
   1072 	}
   1073 
   1074 	if (ifmr.ifm_status & IFM_AVALID) {
   1075 		printf(" status: ");
   1076 		switch (IFM_TYPE(ifmr.ifm_active)) {
   1077 		case IFM_ETHER:
   1078 			if (ifmr.ifm_status & IFM_ACTIVE)
   1079 				printf("active");
   1080 			else
   1081 				printf("no carrier");
   1082 			break;
   1083 
   1084 		case IFM_FDDI:
   1085 		case IFM_TOKEN:
   1086 			if (ifmr.ifm_status & IFM_ACTIVE)
   1087 				printf("inserted");
   1088 			else
   1089 				printf("no ring");
   1090 			break;
   1091 		}
   1092 	}
   1093 
   1094 	putchar('\n');
   1095 
   1096 	if (mflag) {
   1097 		printf("\tsupported media:");
   1098 		for (i = 0; i < ifmr.ifm_count; i++) {
   1099 			putchar(' ');
   1100 			print_media_word(media_list[i]);
   1101 		}
   1102 		putchar('\n');
   1103 	}
   1104 
   1105 	free(media_list);
   1106 
   1107  proto_status:
   1108 	if ((p = afp) != NULL) {
   1109 		(*p->af_status)(1);
   1110 	} else for (p = afs; p->af_name; p++) {
   1111 		ifr.ifr_addr.sa_family = p->af_af;
   1112 		(*p->af_status)(0);
   1113 	}
   1114 }
   1115 
   1116 void
   1117 in_status(force)
   1118 	int force;
   1119 {
   1120 	struct sockaddr_in *sin;
   1121 
   1122 	getsock(AF_INET);
   1123 	if (s < 0) {
   1124 		if (errno == EPROTONOSUPPORT)
   1125 			return;
   1126 		err(1, "socket");
   1127 	}
   1128 	(void) memset(&ifr, 0, sizeof(ifr));
   1129 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1130 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   1131 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1132 			if (!force)
   1133 				return;
   1134 			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1135 		} else
   1136 			warn("SIOCGIFADDR");
   1137 	}
   1138 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1139 	sin = (struct sockaddr_in *)&ifr.ifr_addr;
   1140 	printf("\tinet %s ", inet_ntoa(sin->sin_addr));
   1141 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1142 	if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
   1143 		if (errno != EADDRNOTAVAIL)
   1144 			warn("SIOCGIFNETMASK");
   1145 		(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1146 	} else
   1147 		netmask.sin_addr =
   1148 		    ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
   1149 	if (flags & IFF_POINTOPOINT) {
   1150 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   1151 			if (errno == EADDRNOTAVAIL)
   1152 			    (void) memset(&ifr.ifr_addr, 0,
   1153 				sizeof(ifr.ifr_addr));
   1154 			else
   1155 			    warn("SIOCGIFDSTADDR");
   1156 		}
   1157 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1158 		sin = (struct sockaddr_in *)&ifr.ifr_dstaddr;
   1159 		printf("--> %s ", inet_ntoa(sin->sin_addr));
   1160 	}
   1161 	printf("netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
   1162 	if (flags & IFF_BROADCAST) {
   1163 		if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
   1164 			if (errno == EADDRNOTAVAIL)
   1165 				(void) memset(&ifr.ifr_addr, 0,
   1166 				    sizeof(ifr.ifr_addr));
   1167 			else
   1168 			    warn("SIOCGIFBRDADDR");
   1169 		}
   1170 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1171 		sin = (struct sockaddr_in *)&ifr.ifr_addr;
   1172 		if (sin->sin_addr.s_addr != 0)
   1173 			printf("broadcast %s", inet_ntoa(sin->sin_addr));
   1174 	}
   1175 	putchar('\n');
   1176 }
   1177 
   1178 #ifndef INET_ONLY
   1179 
   1180 void
   1181 at_status(force)
   1182 	int force;
   1183 {
   1184 	struct sockaddr_at *sat, null_sat;
   1185 	struct netrange *nr;
   1186 
   1187 	getsock(AF_APPLETALK);
   1188 	if (s < 0) {
   1189 		if (errno == EPROTONOSUPPORT)
   1190 			return;
   1191 		err(1, "socket");
   1192 	}
   1193 	(void) memset(&ifr, 0, sizeof(ifr));
   1194 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1195 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   1196 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1197 			if (!force)
   1198 				return;
   1199 			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1200 		} else
   1201 			warn("SIOCGIFADDR");
   1202 	}
   1203 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   1204 	sat = (struct sockaddr_at *)&ifr.ifr_addr;
   1205 
   1206 	(void) memset(&null_sat, 0, sizeof(null_sat));
   1207 
   1208 	nr = (struct netrange *) &sat->sat_zero;
   1209 	printf("\tatalk %d.%d range %d-%d phase %d",
   1210 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
   1211 	    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
   1212 	if (flags & IFF_POINTOPOINT) {
   1213 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   1214 			if (errno == EADDRNOTAVAIL)
   1215 			    (void) memset(&ifr.ifr_addr, 0,
   1216 				sizeof(ifr.ifr_addr));
   1217 			else
   1218 			    warn("SIOCGIFDSTADDR");
   1219 		}
   1220 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1221 		sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
   1222 		if (!sat)
   1223 			sat = &null_sat;
   1224 		printf("--> %d.%d",
   1225 		    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
   1226 	}
   1227 	if (flags & IFF_BROADCAST) {
   1228 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
   1229 		sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
   1230 		if (sat)
   1231 			printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
   1232 			    sat->sat_addr.s_node);
   1233 	}
   1234 	putchar('\n');
   1235 }
   1236 
   1237 void
   1238 xns_status(force)
   1239 	int force;
   1240 {
   1241 	struct sockaddr_ns *sns;
   1242 
   1243 	getsock(AF_NS);
   1244 	if (s < 0) {
   1245 		if (errno == EPROTONOSUPPORT)
   1246 			return;
   1247 		err(1, "socket");
   1248 	}
   1249 	(void) memset(&ifr, 0, sizeof(ifr));
   1250 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1251 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   1252 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1253 			if (!force)
   1254 				return;
   1255 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1256 		} else
   1257 			warn("SIOCGIFADDR");
   1258 	}
   1259 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   1260 	sns = (struct sockaddr_ns *)&ifr.ifr_addr;
   1261 	printf("\tns %s ", ns_ntoa(sns->sns_addr));
   1262 	if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
   1263 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   1264 			if (errno == EADDRNOTAVAIL)
   1265 			    memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1266 			else
   1267 			    warn("SIOCGIFDSTADDR");
   1268 		}
   1269 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1270 		sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
   1271 		printf("--> %s ", ns_ntoa(sns->sns_addr));
   1272 	}
   1273 	putchar('\n');
   1274 }
   1275 
   1276 void
   1277 iso_status(force)
   1278 	int force;
   1279 {
   1280 	struct sockaddr_iso *siso;
   1281 
   1282 	getsock(AF_ISO);
   1283 	if (s < 0) {
   1284 		if (errno == EPROTONOSUPPORT)
   1285 			return;
   1286 		err(1, "socket");
   1287 	}
   1288 	(void) memset(&ifr, 0, sizeof(ifr));
   1289 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1290 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   1291 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1292 			if (!force)
   1293 				return;
   1294 			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1295 		} else
   1296 			warn("SIOCGIFADDR");
   1297 	}
   1298 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   1299 	siso = (struct sockaddr_iso *)&ifr.ifr_addr;
   1300 	printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
   1301 	if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
   1302 		if (errno == EADDRNOTAVAIL)
   1303 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1304 		else
   1305 			warn("SIOCGIFNETMASK");
   1306 	} else {
   1307 		printf(" netmask %s ", iso_ntoa(&siso->siso_addr));
   1308 	}
   1309 	if (flags & IFF_POINTOPOINT) {
   1310 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   1311 			if (errno == EADDRNOTAVAIL)
   1312 			    memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   1313 			else
   1314 			    warn("SIOCGIFDSTADDR");
   1315 		}
   1316 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1317 		siso = (struct sockaddr_iso *)&ifr.ifr_addr;
   1318 		printf("--> %s ", iso_ntoa(&siso->siso_addr));
   1319 	}
   1320 	putchar('\n');
   1321 }
   1322 
   1323 #endif	/* INET_ONLY */
   1324 
   1325 #define SIN(x) ((struct sockaddr_in *) &(x))
   1326 struct sockaddr_in *sintab[] = {
   1327 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
   1328 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
   1329 
   1330 void
   1331 in_getaddr(s, which)
   1332 	char *s;
   1333 	int which;
   1334 {
   1335 	struct sockaddr_in *sin = sintab[which];
   1336 	struct hostent *hp;
   1337 	struct netent *np;
   1338 
   1339 	sin->sin_len = sizeof(*sin);
   1340 	if (which != MASK)
   1341 		sin->sin_family = AF_INET;
   1342 
   1343 	if (inet_aton(s, &sin->sin_addr) == 0) {
   1344 		if ((hp = gethostbyname(s)) != NULL)
   1345 			(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
   1346 		else if ((np = getnetbyname(s)) != NULL)
   1347 			sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
   1348 		else
   1349 			errx(1, "%s: bad value", s);
   1350 	}
   1351 }
   1352 
   1353 /*
   1354  * Print a value a la the %b format of the kernel's printf
   1355  */
   1356 void
   1357 printb(s, v, bits)
   1358 	char *s;
   1359 	char *bits;
   1360 	unsigned short v;
   1361 {
   1362 	int i, any = 0;
   1363 	char c;
   1364 
   1365 	if (bits && *bits == 8)
   1366 		printf("%s=%o", s, v);
   1367 	else
   1368 		printf("%s=%x", s, v);
   1369 	bits++;
   1370 	if (bits) {
   1371 		putchar('<');
   1372 		while ((i = *bits++) != 0) {
   1373 			if (v & (1 << (i-1))) {
   1374 				if (any)
   1375 					putchar(',');
   1376 				any = 1;
   1377 				for (; (c = *bits) > 32; bits++)
   1378 					putchar(c);
   1379 			} else
   1380 				for (; *bits > 32; bits++)
   1381 					;
   1382 		}
   1383 		putchar('>');
   1384 	}
   1385 }
   1386 
   1387 #ifndef INET_ONLY
   1388 
   1389 void
   1390 at_getaddr(addr, which)
   1391 	char *addr;
   1392 	int which;
   1393 {
   1394 	struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
   1395 	u_int net, node;
   1396 
   1397 	sat->sat_family = AF_APPLETALK;
   1398 	sat->sat_len = sizeof(*sat);
   1399 	if (which == MASK)
   1400 		errx(1, "AppleTalk does not use netmasks\n");
   1401 	if (sscanf(addr, "%u.%u", &net, &node) != 2
   1402 	    || net == 0 || net > 0xffff || node == 0 || node > 0xfe)
   1403 		errx(1, "%s: illegal address", addr);
   1404 	sat->sat_addr.s_net = htons(net);
   1405 	sat->sat_addr.s_node = node;
   1406 }
   1407 
   1408 void
   1409 setatrange(range, d)
   1410 	char *range;
   1411 	int d;
   1412 {
   1413 	u_short	first = 123, last = 123;
   1414 
   1415 	if (sscanf(range, "%hu-%hu", &first, &last) != 2
   1416 	    || first == 0 || first > 0xffff
   1417 	    || last == 0 || last > 0xffff || first > last)
   1418 		errx(1, "%s: illegal net range: %u-%u", range, first, last);
   1419 	at_nr.nr_firstnet = htons(first);
   1420 	at_nr.nr_lastnet = htons(last);
   1421 }
   1422 
   1423 void
   1424 setatphase(phase, d)
   1425 	char *phase;
   1426 	int d;
   1427 {
   1428 	if (!strcmp(phase, "1"))
   1429 		at_nr.nr_phase = 1;
   1430 	else if (!strcmp(phase, "2"))
   1431 		at_nr.nr_phase = 2;
   1432 	else
   1433 		errx(1, "%s: illegal phase", phase);
   1434 }
   1435 
   1436 void
   1437 checkatrange(sat)
   1438 	struct sockaddr_at *sat;
   1439 {
   1440 	if (at_nr.nr_phase == 0)
   1441 		at_nr.nr_phase = 2;	/* Default phase 2 */
   1442 	if (at_nr.nr_firstnet == 0)
   1443 		at_nr.nr_firstnet =	/* Default range of one */
   1444 		at_nr.nr_lastnet = sat->sat_addr.s_net;
   1445 	printf("\tatalk %d.%d range %d-%d phase %d\n",
   1446 	ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
   1447 	ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
   1448 	if ((u_short) ntohs(at_nr.nr_firstnet) >
   1449 			(u_short) ntohs(sat->sat_addr.s_net)
   1450 		    || (u_short) ntohs(at_nr.nr_lastnet) <
   1451 			(u_short) ntohs(sat->sat_addr.s_net))
   1452 		errx(1, "AppleTalk address is not in range");
   1453 	*((struct netrange *) &sat->sat_zero) = at_nr;
   1454 }
   1455 
   1456 #define SNS(x) ((struct sockaddr_ns *) &(x))
   1457 struct sockaddr_ns *snstab[] = {
   1458 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
   1459 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
   1460 
   1461 void
   1462 xns_getaddr(addr, which)
   1463 	char *addr;
   1464 	int which;
   1465 {
   1466 	struct sockaddr_ns *sns = snstab[which];
   1467 
   1468 	sns->sns_family = AF_NS;
   1469 	sns->sns_len = sizeof(*sns);
   1470 	sns->sns_addr = ns_addr(addr);
   1471 	if (which == MASK)
   1472 		puts("Attempt to set XNS netmask will be ineffectual");
   1473 }
   1474 
   1475 #define SISO(x) ((struct sockaddr_iso *) &(x))
   1476 struct sockaddr_iso *sisotab[] = {
   1477 SISO(ridreq.ifr_addr), SISO(iso_addreq.ifra_addr),
   1478 SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
   1479 
   1480 void
   1481 iso_getaddr(addr, which)
   1482 	char *addr;
   1483 	int which;
   1484 {
   1485 	struct sockaddr_iso *siso = sisotab[which];
   1486 	siso->siso_addr = *iso_addr(addr);
   1487 
   1488 	if (which == MASK) {
   1489 		siso->siso_len = TSEL(siso) - (caddr_t)(siso);
   1490 		siso->siso_nlen = 0;
   1491 	} else {
   1492 		siso->siso_len = sizeof(*siso);
   1493 		siso->siso_family = AF_ISO;
   1494 	}
   1495 }
   1496 
   1497 void
   1498 setsnpaoffset(val, d)
   1499 	char *val;
   1500 	int d;
   1501 {
   1502 	iso_addreq.ifra_snpaoffset = atoi(val);
   1503 }
   1504 
   1505 void
   1506 setnsellength(val, d)
   1507 	char *val;
   1508 	int d;
   1509 {
   1510 	nsellength = atoi(val);
   1511 	if (nsellength < 0)
   1512 		errx(1, "Negative NSEL length is absurd");
   1513 	if (afp == 0 || afp->af_af != AF_ISO)
   1514 		errx(1, "Setting NSEL length valid only for iso");
   1515 }
   1516 
   1517 void
   1518 fixnsel(s)
   1519 	struct sockaddr_iso *s;
   1520 {
   1521 	if (s->siso_family == 0)
   1522 		return;
   1523 	s->siso_tlen = nsellength;
   1524 }
   1525 
   1526 void
   1527 adjust_nsellength()
   1528 {
   1529 	fixnsel(sisotab[RIDADDR]);
   1530 	fixnsel(sisotab[ADDR]);
   1531 	fixnsel(sisotab[DSTADDR]);
   1532 }
   1533 
   1534 #endif	/* INET_ONLY */
   1535 
   1536 void
   1537 usage()
   1538 {
   1539 	fprintf(stderr,
   1540 	    "usage: ifconfig [ -m ] interface\n%s%s%s%s%s%s%s%s%s%s%s",
   1541 		"\t[ af [ address [ dest_addr ] ] [ up ] [ down ] ",
   1542 		"[ netmask mask ] ]\n",
   1543 		"\t[ metric n ]\n",
   1544 		"\t[ mtu n ]\n",
   1545 		"\t[ arp | -arp ]\n",
   1546 		"\t[ media mtype ]\n",
   1547 		"\t[ mediaopt mopts ]\n",
   1548 		"\t[ -mediaopt mopts ]\n",
   1549 		"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n",
   1550 		"       ifconfig -a [ -m ] [ -d ] [ -u ] [ af ]\n",
   1551 		"       ifconfig -l [ -d ] [ -u ]\n");
   1552 	exit(1);
   1553 }
   1554