xref: /freebsd/sbin/ifconfig/ifconfig.c (revision df7f5d4de4592a8948a25ce01e5bddfbb7ce39dc)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*
42 static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
43 */
44 static const char rcsid[] =
45 	"$Id$";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/ioctl.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
52 #include <sys/time.h>
53 
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59 
60 /* IP */
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <arpa/inet.h>
64 #include <netdb.h>
65 
66 /* IPX */
67 #define	IPXIP
68 #define IPTUNNEL
69 #include <netipx/ipx.h>
70 #include <netipx/ipx_if.h>
71 
72 /* Appletalk */
73 #include <netatalk/at.h>
74 
75 /* XNS */
76 #ifdef NS
77 #define	NSIP
78 #include <netns/ns.h>
79 #include <netns/ns_if.h>
80 #endif
81 
82 /* OSI */
83 #ifdef ISO
84 #define EON
85 #include <netiso/iso.h>
86 #include <netiso/iso_var.h>
87 #endif
88 
89 #include <ctype.h>
90 #include <err.h>
91 #include <errno.h>
92 #include <fcntl.h>
93 #include <stdio.h>
94 #include <stdlib.h>
95 #include <string.h>
96 #include <unistd.h>
97 
98 struct	ifreq		ifr, ridreq;
99 struct	ifaliasreq	addreq;
100 #ifdef ISO
101 struct	iso_ifreq	iso_ridreq;
102 struct	iso_aliasreq	iso_addreq;
103 #endif
104 struct	sockaddr_in	netmask;
105 struct	netrange	at_nr;		/* AppleTalk net range */
106 
107 char	name[32];
108 int	flags;
109 int	metric;
110 int	mtu;
111 #ifdef ISO
112 int	nsellength = 1;
113 #endif
114 int	setaddr;
115 int	setipdst;
116 int	doalias;
117 int	clearaddr;
118 int	newaddr = 1;
119 int	s;
120 
121 struct	afswtch;
122 
123 void	Perror __P((const char *cmd));
124 void	checkatrange __P((struct sockaddr_at *));
125 int	ifconfig __P((int argc, char *const *argv, int af,
126 		      const struct afswtch *rafp));
127 void	notealias __P((const char *, int));
128 void	printb __P((const char *s, unsigned value, const char *bits));
129 void	rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
130 void	status __P((void));
131 
132 typedef	void c_func __P((const char *cmd, int arg));
133 c_func	setatphase, setatrange;
134 c_func	setifaddr, setifbroadaddr, setifdstaddr, setifnetmask;
135 c_func	setifipdst;
136 c_func	setifflags, setifmetric, setifmtu;
137 
138 #ifdef ISO
139 c_func	setsnpaoffset, setnsellength;
140 #endif
141 
142 #define	NEXTARG		0xffffff
143 
144 const
145 struct	cmd {
146 	const	char *c_name;
147 	int	c_parameter;		/* NEXTARG means next argv */
148 	void	(*c_func) __P((const char *, int));
149 } cmds[] = {
150 	{ "up",		IFF_UP,		setifflags } ,
151 	{ "down",	-IFF_UP,	setifflags },
152 	{ "arp",	-IFF_NOARP,	setifflags },
153 	{ "-arp",	IFF_NOARP,	setifflags },
154 	{ "debug",	IFF_DEBUG,	setifflags },
155 	{ "-debug",	-IFF_DEBUG,	setifflags },
156 	{ "alias",	IFF_UP,		notealias },
157 	{ "-alias",	-IFF_UP,	notealias },
158 	{ "delete",	-IFF_UP,	notealias },
159 #ifdef notdef
160 #define	EN_SWABIPS	0x1000
161 	{ "swabips",	EN_SWABIPS,	setifflags },
162 	{ "-swabips",	-EN_SWABIPS,	setifflags },
163 #endif
164 	{ "netmask",	NEXTARG,	setifnetmask },
165 	{ "range",	NEXTARG,	setatrange },
166 	{ "phase",	NEXTARG,	setatphase },
167 	{ "metric",	NEXTARG,	setifmetric },
168 	{ "broadcast",	NEXTARG,	setifbroadaddr },
169 	{ "ipdst",	NEXTARG,	setifipdst },
170 #ifdef ISO
171 	{ "snpaoffset",	NEXTARG,	setsnpaoffset },
172 	{ "nsellength",	NEXTARG,	setnsellength },
173 #endif
174 	{ "link0",	IFF_LINK0,	setifflags },
175 	{ "-link0",	-IFF_LINK0,	setifflags },
176 	{ "link1",	IFF_LINK1,	setifflags },
177 	{ "-link1",	-IFF_LINK1,	setifflags },
178 	{ "link2",	IFF_LINK2,	setifflags },
179 	{ "-link2",	-IFF_LINK2,	setifflags },
180 	{ "normal",	-IFF_LINK0,	setifflags },
181 	{ "compress",	IFF_LINK0,	setifflags },
182 	{ "noicmp",	IFF_LINK1,	setifflags },
183 	{ "mtu",	NEXTARG,	setifmtu },
184 	{ 0,		0,		setifaddr },
185 	{ 0,		0,		setifdstaddr },
186 };
187 
188 /*
189  * XNS support liberally adapted from code written at the University of
190  * Maryland principally by James O'Toole and Chris Torek.
191  */
192 typedef	void af_status __P((int));
193 typedef	void af_getaddr __P((const char *, int));
194 
195 af_status	in_status, ipx_status, at_status, ether_status;
196 af_getaddr	in_getaddr, ipx_getaddr, at_getaddr;
197 
198 #ifdef NS
199 af_status	xns_status;
200 af_getaddr	xns_getaddr;
201 #endif
202 #ifdef ISO
203 af_status	iso_status;
204 af_getaddr	iso_getaddr;
205 #endif
206 
207 /* Known address families */
208 const
209 struct	afswtch {
210 	const char *af_name;
211 	short af_af;
212 	af_status *af_status;
213 	af_getaddr *af_getaddr;
214 	int af_difaddr;
215 	int af_aifaddr;
216 	caddr_t af_ridreq;
217 	caddr_t af_addreq;
218 } afs[] = {
219 #define C(x) ((caddr_t) &x)
220 	{ "inet", AF_INET, in_status, in_getaddr,
221 	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
222 	{ "ipx", AF_IPX, ipx_status, ipx_getaddr,
223 	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
224 	{ "atalk", AF_APPLETALK, at_status, at_getaddr,
225 	     SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
226 #ifdef NS
227 	{ "ns", AF_NS, xns_status, xns_getaddr,
228 	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
229 #endif
230 #ifdef ISO
231 	{ "iso", AF_ISO, iso_status, iso_getaddr,
232 	     SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, C(iso_ridreq), C(iso_addreq) },
233 #endif
234 	{ "ether", AF_INET, ether_status, NULL },	/* XXX not real!! */
235 	{ 0,	0,	    0,		0 }
236 };
237 
238 const struct afswtch *afp;	/*the address family being set or asked about*/
239 
240 /*
241  * Expand the compacted form of addresses as returned via the
242  * configuration read via sysctl().
243  */
244 
245 #define ROUNDUP(a) \
246 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
247 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
248 
249 void
250 rt_xaddrs(cp, cplim, rtinfo)
251 	caddr_t cp, cplim;
252 	struct rt_addrinfo *rtinfo;
253 {
254 	struct sockaddr *sa;
255 	int i;
256 
257 	memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
258 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
259 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
260 			continue;
261 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
262 		ADVANCE(cp, sa);
263 	}
264 }
265 
266 
267 /*
268  * Grunge for new-style sysctl() decoding.. :-(
269  * Apologies to the world for committing gross things like this in 1996..
270  */
271 struct if_msghdr *ifm;
272 struct ifa_msghdr *ifam;
273 struct sockaddr_dl *sdl;
274 struct rt_addrinfo info;
275 char *buf, *lim, *next;
276 
277 int
278 main(argc, argv)
279 	int argc;
280 	char *const *argv;
281 {
282 	int af = AF_INET;
283 	const struct afswtch *rafp;
284 
285 	size_t needed;
286 	int mib[6];
287 	int all;
288 
289 	rafp = 0;
290 
291 	if (argc < 2) {
292 		fprintf(stderr, "usage: ifconfig interface\n%s%s%s%s%s%s%s",
293 		    "\t[ af [ address [ dest_addr ] ] [ up ] [ down ]",
294 			    "[ netmask mask ] ]\n",
295 		    "\t[ metric n ]\n",
296 		    "\t[ mtu n ]\n",
297 		    "\t[ arp | -arp ]\n",
298 		    "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ] \n",
299 		    "\t[ -a ] [ -ad ] [ -au ]\n");
300 		exit(1);
301 	}
302 	argc--, argv++;
303 	strncpy(name, *argv, sizeof(name));
304 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
305 	argc--, argv++;
306 	if (argc > 0) {
307 		for (afp = rafp = afs; rafp->af_name; rafp++)
308 			if (strcmp(rafp->af_name, *argv) == 0) {
309 				afp = rafp; argc--; argv++;
310 				break;
311 			}
312 		rafp = afp;
313 		af = ifr.ifr_addr.sa_family = rafp->af_af;
314 	}
315 
316 	mib[0] = CTL_NET;
317 	mib[1] = PF_ROUTE;
318 	mib[2] = 0;
319 	mib[3] = 0;	/* address family */
320 	mib[4] = NET_RT_IFLIST;
321 	mib[5] = 0;
322 
323 	/* if particular family specified, only ask about it */
324 	if (afp) {
325 		mib[3] = afp->af_af;
326 	}
327 
328 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
329 		errx(1, "iflist-sysctl-estimate");
330 	if ((buf = malloc(needed)) == NULL)
331 		errx(1, "malloc");
332 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
333 		errx(1, "actual retrieval of interface table");
334 	lim = buf + needed;
335 
336 	all = 0;
337 	if (strcmp(name, "-a") == 0)
338 		all = 1;	/* All interfaces */
339 	else if (strcmp(name, "-au") == 0)
340 		all = 2;	/* All IFF_UPinterfaces */
341 	else if (strcmp(name, "-ad") == 0)
342 		all = 3;	/* All !IFF_UP interfaces */
343 
344 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
345 
346 		ifm = (struct if_msghdr *)next;
347 
348 		/* XXX: Swallow up leftover NEWADDR messages */
349 		if (ifm->ifm_type == RTM_NEWADDR)
350 			continue;
351 
352 		if (ifm->ifm_type == RTM_IFINFO) {
353 			sdl = (struct sockaddr_dl *)(ifm + 1);
354 			flags = ifm->ifm_flags;
355 		} else {
356 			errx(1, "out of sync parsing NET_RT_IFLIST");
357 		}
358 
359 		switch(all) {
360 		case -1:
361 		case 0:
362 			if (strlen(name) != sdl->sdl_nlen)
363 				continue; /* not same len */
364 			if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
365 				continue; /* not same name */
366 			break;
367 		case 1:
368 			break;	/* always do it */
369 		case 2:
370 			if ((flags & IFF_UP) == 0)
371 				continue; /* not up */
372 			break;
373 		case 3:
374 			if (flags & IFF_UP)
375 				continue; /* not down */
376 			break;
377 		}
378 
379 		if (all > 0) {
380 			strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
381 			name[sdl->sdl_nlen] = '\0';
382 		}
383 
384 		if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
385 			perror("ifconfig: socket");
386 			exit(1);
387 		}
388 
389 		ifconfig(argc, argv, af, rafp);
390 
391 		close(s);
392 
393 		if (all == 0) {
394 			all = -1; /* flag it as 'done' */
395 			break;
396 		}
397 	}
398 	free(buf);
399 
400 	if (all == 0)
401 		errx(1, "interface %s does not exist", name);
402 
403 
404 	exit (0);
405 }
406 
407 
408 
409 int
410 ifconfig(argc,argv,af,rafp)
411 	int argc;
412 	char *const *argv;
413 	int af;
414 	const struct afswtch *rafp;
415 {
416 
417 	strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
418 
419 	if (ioctl(s, SIOCGIFMETRIC, (caddr_t)&ifr) < 0)
420 		perror("ioctl (SIOCGIFMETRIC)");
421 	else
422 		metric = ifr.ifr_metric;
423 
424 	if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0)
425 		perror("ioctl (SIOCGIFMTU)");
426 	else
427 		mtu = ifr.ifr_mtu;
428 
429 	if (argc == 0) {
430 		status();
431 		return(0);
432 	}
433 
434 	while (argc > 0) {
435 		register const struct cmd *p;
436 
437 		for (p = cmds; p->c_name; p++)
438 			if (strcmp(*argv, p->c_name) == 0)
439 				break;
440 		if (p->c_name == 0 && setaddr)
441 			p++;	/* got src, do dst */
442 		if (p->c_func) {
443 			if (p->c_parameter == NEXTARG) {
444 				if (argv[1] == NULL)
445 					errx(1, "'%s' requires argument",
446 					    p->c_name);
447 				(*p->c_func)(argv[1], 0);
448 				argc--, argv++;
449 			} else
450 				(*p->c_func)(*argv, p->c_parameter);
451 		}
452 		argc--, argv++;
453 	}
454 #ifdef ISO
455 	if (af == AF_ISO)
456 		adjust_nsellength();
457 #endif
458 	if (setipdst && af==AF_IPX) {
459 		struct ipxip_req rq;
460 		int size = sizeof(rq);
461 
462 		rq.rq_ipx = addreq.ifra_addr;
463 		rq.rq_ip = addreq.ifra_dstaddr;
464 
465 		if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
466 			Perror("Encapsulation Routing");
467 	}
468 	if (af == AF_APPLETALK)
469 		checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
470 #ifdef NS
471 	if (setipdst && af==AF_NS) {
472 		struct nsip_req rq;
473 		int size = sizeof(rq);
474 
475 		rq.rq_ns = addreq.ifra_addr;
476 		rq.rq_ip = addreq.ifra_dstaddr;
477 
478 		if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
479 			Perror("Encapsulation Routing");
480 	}
481 #endif
482 	if (clearaddr) {
483 		if (rafp->af_ridreq == NULL || rafp->af_difaddr == 0) {
484 			warnx("interface %s cannot change %s addresses!",
485 			      name, rafp->af_name);
486 			clearaddr = NULL;
487 		}
488 	}
489 	if (clearaddr) {
490 		int ret;
491 		strncpy(rafp->af_ridreq, name, sizeof ifr.ifr_name);
492 		if ((ret = ioctl(s, rafp->af_difaddr, rafp->af_ridreq)) < 0) {
493 			if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
494 				/* means no previous address for interface */
495 			} else
496 				Perror("ioctl (SIOCDIFADDR)");
497 		}
498 	}
499 	if (newaddr) {
500 		if (rafp->af_ridreq == NULL || rafp->af_difaddr == 0) {
501 			warnx("interface %s cannot change %s addresses!",
502 			      name, rafp->af_name);
503 			newaddr = NULL;
504 		}
505 	}
506 	if (newaddr) {
507 		strncpy(rafp->af_addreq, name, sizeof ifr.ifr_name);
508 		if (ioctl(s, rafp->af_aifaddr, rafp->af_addreq) < 0)
509 			Perror("ioctl (SIOCAIFADDR)");
510 	}
511 	return(0);
512 }
513 #define RIDADDR 0
514 #define ADDR	1
515 #define MASK	2
516 #define DSTADDR	3
517 
518 /*ARGSUSED*/
519 void
520 setifaddr(addr, param)
521 	const char *addr;
522 	int param;
523 {
524 	/*
525 	 * Delay the ioctl to set the interface addr until flags are all set.
526 	 * The address interpretation may depend on the flags,
527 	 * and the flags may change when the address is set.
528 	 */
529 	setaddr++;
530 	if (doalias == 0)
531 		clearaddr = 1;
532 	(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
533 }
534 
535 void
536 setifnetmask(addr, dummy)
537 	const char *addr;
538 	int dummy __unused;
539 {
540 	(*afp->af_getaddr)(addr, MASK);
541 }
542 
543 void
544 setifbroadaddr(addr, dummy)
545 	const char *addr;
546 	int dummy __unused;
547 {
548 	(*afp->af_getaddr)(addr, DSTADDR);
549 }
550 
551 void
552 setifipdst(addr, dummy)
553 	const char *addr;
554 	int dummy __unused;
555 {
556 	in_getaddr(addr, DSTADDR);
557 	setipdst++;
558 	clearaddr = 0;
559 	newaddr = 0;
560 }
561 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
562 
563 void
564 notealias(addr, param)
565 	const char *addr;
566 	int param;
567 {
568 	if (setaddr && doalias == 0 && param < 0)
569 		bcopy((caddr_t)rqtosa(af_addreq),
570 		      (caddr_t)rqtosa(af_ridreq),
571 		      rqtosa(af_addreq)->sa_len);
572 	doalias = param;
573 	if (param < 0) {
574 		clearaddr = 1;
575 		newaddr = 0;
576 	} else
577 		clearaddr = 0;
578 }
579 
580 /*ARGSUSED*/
581 void
582 setifdstaddr(addr, param)
583 	const char *addr;
584 	int param __unused;
585 {
586 	(*afp->af_getaddr)(addr, DSTADDR);
587 }
588 
589 void
590 setifflags(vname, value)
591 	const char *vname;
592 	int value;
593 {
594  	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
595  		Perror("ioctl (SIOCGIFFLAGS)");
596  		exit(1);
597  	}
598 	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
599  	flags = ifr.ifr_flags;
600 
601 	if (value < 0) {
602 		value = -value;
603 		flags &= ~value;
604 	} else
605 		flags |= value;
606 	ifr.ifr_flags = flags;
607 	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
608 		Perror(vname);
609 }
610 
611 void
612 setifmetric(val, dummy)
613 	const char *val;
614 	int dummy __unused;
615 {
616 	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
617 	ifr.ifr_metric = atoi(val);
618 	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
619 		perror("ioctl (set metric)");
620 }
621 
622 void
623 setifmtu(val, dummy)
624 	const char *val;
625 	int dummy __unused;
626 {
627 	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
628 	ifr.ifr_mtu = atoi(val);
629 	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
630 		perror("ioctl (set mtu)");
631 }
632 
633 #ifdef ISO
634 void
635 setsnpaoffset(val, dummy)
636 	char *val;
637 	int dummy __unused;
638 {
639 	iso_addreq.ifra_snpaoffset = atoi(val);
640 }
641 #endif
642 
643 #define	IFFBITS \
644 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6b6\7RUNNING" \
645 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
646 "\20MULTICAST"
647 
648 /*
649  * Print the status of the interface.  If an address family was
650  * specified, show it and it only; otherwise, show them all.
651  */
652 void
653 status()
654 {
655 	const struct afswtch *p = NULL;
656 	char *mynext;
657 	struct if_msghdr *myifm;
658 
659 	printf("%s: ", name);
660 	printb("flags", flags, IFFBITS);
661 	if (metric)
662 		printf(" metric %d", metric);
663 	if (mtu)
664 		printf(" mtu %d", mtu);
665 	putchar('\n');
666 
667 	/*
668 	 * XXX: Sigh. This is bad, I know.  At this point, we may have
669 	 * *zero* RTM_NEWADDR's, so we have to "feel the water" before
670 	 * incrementing the loop.  One day, I might feel inspired enough
671 	 * to get the top level loop to pass a count down here so we
672 	 * dont have to mess with this.  -Peter
673 	 */
674 	myifm = ifm;
675 
676 	while (1) {
677 
678 		mynext = next + ifm->ifm_msglen;
679 
680 		if (mynext >= lim)
681 			break;
682 
683 		myifm = (struct if_msghdr *)mynext;
684 
685 		if (myifm->ifm_type != RTM_NEWADDR)
686 			break;
687 
688 		next = mynext;
689 
690 		ifm = (struct if_msghdr *)next;
691 
692 		ifam = (struct ifa_msghdr *)myifm;
693 		info.rti_addrs = ifam->ifam_addrs;
694 
695 		/* Expand the compacted addresses */
696 		rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
697 			  &info);
698 
699 		if (afp) {
700 			if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family &&
701 			    afp->af_status != ether_status) {
702 				p = afp;
703 				if (p->af_status != ether_status)
704 					(*p->af_status)(1);
705 			}
706 		} else for (p = afs; p->af_name; p++) {
707 			if (p->af_af == info.rti_info[RTAX_IFA]->sa_family &&
708 			    p->af_status != ether_status)
709 				(*p->af_status)(0);
710 		}
711 	}
712 	if (afp == NULL || afp->af_status == ether_status)
713 		ether_status(0);
714 	else if (afp && !p) {
715 		warnx("%s has no %s IFA address!", name, afp->af_name);
716 	}
717 }
718 
719 void
720 in_status(force)
721 	int force;
722 {
723 	struct sockaddr_in *sin, null_sin;
724 
725 	memset(&null_sin, 0, sizeof(null_sin));
726 
727 	sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
728 	if (!sin || sin->sin_family != AF_INET) {
729 		if (!force)
730 			return;
731 		/* warnx("%s has no AF_INET IFA address!", name); */
732 		sin = &null_sin;
733 	}
734 	printf("\tinet %s ", inet_ntoa(sin->sin_addr));
735 
736 	if (flags & IFF_POINTOPOINT) {
737 		/* note RTAX_BRD overlap with IFF_BROADCAST */
738 		sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
739 		if (!sin)
740 			sin = &null_sin;
741 		printf("--> %s ", inet_ntoa(sin->sin_addr));
742 	}
743 
744 	sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
745 	if (!sin)
746 		sin = &null_sin;
747 	printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
748 
749 	if (flags & IFF_BROADCAST) {
750 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
751 		sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
752 		if (sin && sin->sin_addr.s_addr != 0)
753 			printf("broadcast %s", inet_ntoa(sin->sin_addr));
754 	}
755 	putchar('\n');
756 }
757 
758 void
759 ipx_status(force)
760 	int force;
761 {
762 	struct sockaddr_ipx *sipx, null_sipx;
763 
764 	close(s);
765 	s = socket(AF_IPX, SOCK_DGRAM, 0);
766 	if (s < 0) {
767 		if (errno == EPROTONOSUPPORT)
768 			return;
769 		perror("ifconfig: socket");
770 		exit(1);
771 	}
772 
773 	memset(&null_sipx, 0, sizeof(null_sipx));
774 
775 	sipx = (struct sockaddr_ipx *)info.rti_info[RTAX_IFA];
776 	if (!sipx || sipx->sipx_family != AF_IPX) {
777 		if (!force)
778 			return;
779 		warnx("%s has no AF_IPX IFA address!", name);
780 		sipx = &null_sipx;
781 	}
782 	printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
783 
784 	if (flags & IFF_POINTOPOINT) {
785 		sipx = (struct sockaddr_ipx *)info.rti_info[RTAX_BRD];
786 		if (!sipx)
787 			sipx = &null_sipx;
788 		printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
789 	}
790 	putchar('\n');
791 
792 }
793 
794 void
795 at_status(force)
796 	int force;
797 {
798 	struct sockaddr_at *sat, null_sat;
799 	struct netrange *nr;
800 
801 	memset(&null_sat, 0, sizeof(null_sat));
802 
803 	sat = (struct sockaddr_at *)info.rti_info[RTAX_IFA];
804 	if (!sat || sat->sat_family != AF_APPLETALK) {
805 		if (!force)
806 			return;
807 		sat = &null_sat;
808 	}
809 	nr = &sat->sat_range.r_netrange;
810 	printf("\tatalk %d.%d range %d-%d phase %d",
811 		ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
812 		ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
813 	if (flags & IFF_POINTOPOINT) {
814 		/* note RTAX_BRD overlap with IFF_BROADCAST */
815 		sat = (struct sockaddr_at *)info.rti_info[RTAX_BRD];
816 		if (!sat)
817 			sat = &null_sat;
818 		printf("--> %d.%d",
819 			ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
820 	}
821 	if (flags & IFF_BROADCAST) {
822 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
823 		sat = (struct sockaddr_at *)info.rti_info[RTAX_BRD];
824 		if (sat)
825 			printf(" broadcast %d.%d",
826 				ntohs(sat->sat_addr.s_net),
827 				sat->sat_addr.s_node);
828 	}
829 
830 	putchar('\n');
831 }
832 
833 #ifdef NS
834 void
835 xns_status(force)
836 	int force;
837 {
838 	struct sockaddr_ns *sns, null_sns;
839 
840 	close(s);
841 	s = socket(AF_NS, SOCK_DGRAM, 0);
842 	if (s < 0) {
843 		if (errno == EPROTONOSUPPORT)
844 			return;
845 		perror("ifconfig: socket");
846 		exit(1);
847 	}
848 	memset(&null_sns, 0, sizeof(null_sns));
849 
850 	sns = (struct sockaddr_ns *)info.rti_info[RTAX_IFA];
851 	if (!sns || sns->sns_family != AF_NS) {
852 		if (!force)
853 			return;
854 		/* warnx("%s has no AF_NS IFA address!", name); */
855 		sns = &null_sns;
856 	}
857 	printf("\tns %s ", ns_ntoa(sns->sns_addr));
858 
859 	if (flags & IFF_POINTOPOINT) {
860 		sns = (struct sockaddr_ns *)info.rti_info[RTAX_BRD];
861 		if (!sns)
862 			sns = &null_sns;
863 		printf("--> %s ", ns_ntoa(sns->sns_addr));
864 	}
865 
866 	putchar('\n');
867 }
868 #endif
869 
870 #ifdef ISO
871 void
872 iso_status(force)
873 	int force;
874 {
875 	struct sockaddr_iso *siso, null_siso;
876 
877 	close(s);
878 	s = socket(AF_ISO, SOCK_DGRAM, 0);
879 	if (s < 0) {
880 		if (errno == EPROTONOSUPPORT)
881 			return;
882 		perror("ifconfig: socket");
883 		exit(1);
884 	}
885 
886 	memset(&null_siso, 0, sizeof(null_siso));
887 
888 	siso = (struct sockaddr_iso *)info.rti_info[RTAX_IFA];
889 	if (!siso || siso->siso_family != AF_ISO) {
890 		if (!force)
891 			return;
892 		/* warnx("%s has no AF_ISO IFA address!", name); */
893 		siso = &null_siso;
894 	}
895 	printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
896 
897 	/* XXX: is this right? is the ISO netmask meant to be before P2P? */
898 	siso = (struct sockaddr_iso *)info.rti_info[RTAX_NETMASK];
899 	if (siso)
900 		printf(" netmask %s ", iso_ntoa(&siso->siso_addr));
901 
902 	if (flags & IFF_POINTOPOINT) {
903 		siso = (struct sockaddr_iso *)info.rti_info[RTAX_BRD];
904 		if (!siso)
905 			siso = &null_siso;
906 		printf("--> %s ", iso_ntoa(&siso->siso_addr));
907 	}
908 
909 	putchar('\n');
910 }
911 #endif
912 
913 void
914 ether_status(force)
915 	int force __unused;
916 {
917 	char *cp;
918 	int n;
919 
920 	cp = (char *)LLADDR(sdl);
921 	if ((n = sdl->sdl_alen) > 0) {
922 		if (sdl->sdl_type == IFT_ETHER)
923 			printf ("\tether ");
924 		else
925 			printf ("\tlladdr ");
926              	while (--n >= 0)
927 			printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' ');
928 		putchar('\n');
929 	}
930 }
931 
932 void
933 Perror(cmd)
934 	const char *cmd;
935 {
936 	switch (errno) {
937 
938 	case ENXIO:
939 		errx(1, "%s: no such interface", cmd);
940 		break;
941 
942 	case EPERM:
943 		errx(1, "%s: permission denied", cmd);
944 		break;
945 
946 	default:
947 		err(1, "%s", cmd);
948 	}
949 }
950 
951 #define SIN(x) ((struct sockaddr_in *) &(x))
952 struct sockaddr_in *sintab[] = {
953 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
954 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
955 
956 void
957 in_getaddr(s, which)
958 	const char *s;
959 	int which;
960 {
961 	register struct sockaddr_in *sin = sintab[which];
962 	struct hostent *hp;
963 	struct netent *np;
964 
965 	sin->sin_len = sizeof(*sin);
966 	if (which != MASK)
967 		sin->sin_family = AF_INET;
968 
969 	if (inet_aton(s, &sin->sin_addr))
970 		return;
971 	if ((hp = gethostbyname(s)) != 0)
972 		bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length);
973 	else if ((np = getnetbyname(s)) != 0)
974 		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
975 	else
976 		errx(1, "%s: bad value", s);
977 }
978 
979 /*
980  * Print a value a la the %b format of the kernel's printf
981  */
982 void
983 printb(s, v, bits)
984 	const char *s;
985 	register unsigned v;
986 	register const char *bits;
987 {
988 	register int i, any = 0;
989 	register char c;
990 
991 	if (bits && *bits == 8)
992 		printf("%s=%o", s, v);
993 	else
994 		printf("%s=%x", s, v);
995 	bits++;
996 	if (bits) {
997 		putchar('<');
998 		while ((i = *bits++) != '\0') {
999 			if (v & (1 << (i-1))) {
1000 				if (any)
1001 					putchar(',');
1002 				any = 1;
1003 				for (; (c = *bits) > 32; bits++)
1004 					putchar(c);
1005 			} else
1006 				for (; *bits > 32; bits++)
1007 					;
1008 		}
1009 		putchar('>');
1010 	}
1011 }
1012 
1013 #define SIPX(x) ((struct sockaddr_ipx *) &(x))
1014 struct sockaddr_ipx *sipxtab[] = {
1015 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1016 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1017 
1018 void
1019 ipx_getaddr(addr, which)
1020 	const char *addr;
1021 	int which;
1022 {
1023 	struct sockaddr_ipx *sipx = sipxtab[which];
1024 
1025 	sipx->sipx_family = AF_IPX;
1026 	sipx->sipx_len = sizeof(*sipx);
1027 	sipx->sipx_addr = ipx_addr(addr);
1028 	if (which == MASK)
1029 		printf("Attempt to set IPX netmask will be ineffectual\n");
1030 }
1031 
1032 void
1033 at_getaddr(addr, which)
1034 	const char *addr;
1035 	int which;
1036 {
1037 	struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1038 	u_int net, node;
1039 
1040 	sat->sat_family = AF_APPLETALK;
1041 	sat->sat_len = sizeof(*sat);
1042 	if (which == MASK)
1043 		errx(1, "AppleTalk does not use netmasks\n");
1044 	if (sscanf(addr, "%u.%u", &net, &node) != 2
1045 	    || net > 0xffff || node > 0xfe)
1046 		errx(1, "%s: illegal address", addr);
1047 	sat->sat_addr.s_net = htons(net);
1048 	sat->sat_addr.s_node = node;
1049 }
1050 
1051 /* XXX  FIXME -- should use strtoul for better parsing. */
1052 void
1053 setatrange(range, dummy)
1054 	const char *range;
1055 	int dummy __unused;
1056 {
1057 	u_short	first = 123, last = 123;
1058 
1059 	if (sscanf(range, "%hu-%hu", &first, &last) != 2
1060 	    || first == 0 || first > 0xffff
1061 	    || last == 0 || last > 0xffff || first > last)
1062 		errx(1, "%s: illegal net range: %u-%u", range, first, last);
1063 	at_nr.nr_firstnet = htons(first);
1064 	at_nr.nr_lastnet = htons(last);
1065 }
1066 
1067 void
1068 setatphase(phase, dummy)
1069 	const char *phase;
1070 	int dummy __unused;
1071 {
1072 	if (!strcmp(phase, "1"))
1073 		at_nr.nr_phase = 1;
1074 	else if (!strcmp(phase, "2"))
1075 		at_nr.nr_phase = 2;
1076 	else
1077 		errx(1, "%s: illegal phase", phase);
1078 }
1079 
1080 void
1081 checkatrange(struct sockaddr_at *sat)
1082 {
1083 	if (at_nr.nr_phase == 0)
1084 		at_nr.nr_phase = 2;	/* Default phase 2 */
1085 	if (at_nr.nr_firstnet == 0)
1086 		at_nr.nr_firstnet =	/* Default range of one */
1087 		at_nr.nr_lastnet = sat->sat_addr.s_net;
1088 printf("\tatalk %d.%d range %d-%d phase %d\n",
1089 	ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1090 	ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1091 	if ((u_short) ntohs(at_nr.nr_firstnet) >
1092 			(u_short) ntohs(sat->sat_addr.s_net)
1093 		    || (u_short) ntohs(at_nr.nr_lastnet) <
1094 			(u_short) ntohs(sat->sat_addr.s_net))
1095 		errx(1, "AppleTalk address is not in range");
1096 	sat->sat_range.r_netrange = at_nr;
1097 }
1098 
1099 #ifdef NS
1100 #define SNS(x) ((struct sockaddr_ns *) &(x))
1101 struct sockaddr_ns *snstab[] = {
1102 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1103 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1104 
1105 void
1106 xns_getaddr(addr, which)
1107 	const char *addr;
1108 	int which;
1109 {
1110 	struct sockaddr_ns *sns = snstab[which];
1111 
1112 	sns->sns_family = AF_NS;
1113 	sns->sns_len = sizeof(*sns);
1114 	sns->sns_addr = ns_addr(addr);
1115 	if (which == MASK)
1116 		printf("Attempt to set XNS netmask will be ineffectual\n");
1117 }
1118 #endif
1119 
1120 #ifdef ISO
1121 #define SISO(x) ((struct sockaddr_iso *) &(x))
1122 struct sockaddr_iso *sisotab[] = {
1123 SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
1124 SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
1125 
1126 void
1127 iso_getaddr(addr, which)
1128 char *addr;
1129 {
1130 	register struct sockaddr_iso *siso = sisotab[which];
1131 	struct iso_addr *iso_addr();
1132 	siso->siso_addr = *iso_addr(addr);
1133 
1134 	if (which == MASK) {
1135 		siso->siso_len = TSEL(siso) - (caddr_t)(siso);
1136 		siso->siso_nlen = 0;
1137 	} else {
1138 		siso->siso_len = sizeof(*siso);
1139 		siso->siso_family = AF_ISO;
1140 	}
1141 }
1142 
1143 void
1144 setnsellength(val)
1145 	char *val;
1146 {
1147 	nsellength = atoi(val);
1148 	if (nsellength < 0)
1149 		errx(1, "Negative NSEL length is absurd");
1150 	if (afp == 0 || afp->af_af != AF_ISO)
1151 		errx(1, "Setting NSEL length valid only for iso");
1152 }
1153 
1154 void
1155 fixnsel(s)
1156 register struct sockaddr_iso *s;
1157 {
1158 	if (s->siso_family == 0)
1159 		return;
1160 	s->siso_tlen = nsellength;
1161 }
1162 
1163 void
1164 adjust_nsellength()
1165 {
1166 	fixnsel(sisotab[RIDADDR]);
1167 	fixnsel(sisotab[ADDR]);
1168 	fixnsel(sisotab[DSTADDR]);
1169 }
1170 #endif
1171