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