xref: /f-stack/tools/ifconfig/ifconfig.c (revision 80a6164c)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Copyright (C) 2017 THL A29 Limited, a Tencent company.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * Derived from FreeBSD's usr/sbin/ifconfig/ifconfig.c.
33  */
34 
35 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1983, 1993\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
44 #endif
45 static const char rcsid[] =
46   "$FreeBSD$";
47 #endif /* not lint */
48 
49 #ifndef FSTACK
50 #include <sys/param.h>
51 #include <sys/ioctl.h>
52 #include <sys/module.h>
53 #include <sys/linker.h>
54 #include <sys/queue.h>
55 #include <sys/socket.h>
56 #include <sys/time.h>
57 
58 #include <net/ethernet.h>
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
63 
64 /* IP */
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <arpa/inet.h>
68 #include <netdb.h>
69 
70 #include <ifaddrs.h>
71 #include <ctype.h>
72 #include <err.h>
73 #include <errno.h>
74 #include <fcntl.h>
75 #ifdef JAIL
76 #include <jail.h>
77 #endif
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <unistd.h>
82 
83 #else
84 #include <ctype.h>
85 #include <err.h>
86 #include <errno.h>
87 #include <fcntl.h>
88 #include <stdint.h>
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <string.h>
92 #include <unistd.h>
93 #include <sys/param.h>
94 
95 #include "net/ethernet.h"
96 #include "net/if.h"
97 #include "net/if_dl.h"
98 #include "net/if_types.h"
99 #include "net/route.h"
100 
101 #include "sys/queue.h"
102 #include "sys/ioctl.h"
103 #include "sys/socket.h"
104 #include "sys/sysctl.h"
105 
106 #include "ifaddrs.h"
107 #include "netdb.h"
108 
109 #include "ff_ipc.h"
110 
111 #endif
112 
113 #include "ifconfig.h"
114 
115 /*
116  * Since "struct ifreq" is composed of various union members, callers
117  * should pay special attention to interpret the value.
118  * (.e.g. little/big endian difference in the structure.)
119  */
120 struct	ifreq ifr;
121 
122 char	name[IFNAMSIZ];
123 char	*descr = NULL;
124 size_t	descrlen = 64;
125 int	setaddr;
126 int	setmask;
127 int	doalias;
128 int	clearaddr;
129 int	newaddr = 1;
130 int	verbose;
131 int	noload;
132 int	printifname = 0;
133 
134 int	supmedia = 0;
135 int	printkeys = 0;		/* Print keying material for interfaces. */
136 
137 /* Formatter Strings */
138 char	*f_inet, *f_inet6, *f_ether, *f_addr;
139 
140 static	int ifconfig(int argc, char *const *argv, int iscreate,
141 		const struct afswtch *afp);
142 static	void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
143 		struct ifaddrs *ifa);
144 static	void tunnel_status(int s);
145 static	void usage(void);
146 
147 static struct afswtch *af_getbyname(const char *name);
148 static struct afswtch *af_getbyfamily(int af);
149 static void af_other_status(int);
150 
151 void printifnamemaybe(void);
152 
153 static struct option *opts = NULL;
154 
155 struct ifa_order_elt {
156 	int if_order;
157 	int af_orders[255];
158 	struct ifaddrs *ifa;
159 	TAILQ_ENTRY(ifa_order_elt) link;
160 };
161 
162 TAILQ_HEAD(ifa_queue, ifa_order_elt);
163 
164 #ifdef FSTACK
165 int
166 fake_socket(int domain, int type, int protocol)
167 {
168 	return 0;
169 }
170 
171 int
172 fake_close(int fd)
173 {
174 	return 0;
175 }
176 #endif
177 
178 void
179 opt_register(struct option *p)
180 {
181 	p->next = opts;
182 	opts = p;
183 }
184 
185 static void
186 usage(void)
187 {
188 	char options[1024];
189 	struct option *p;
190 
191 	/* XXX not right but close enough for now */
192 	options[0] = '\0';
193 	for (p = opts; p != NULL; p = p->next) {
194 		strlcat(options, p->opt_usage, sizeof(options));
195 		strlcat(options, " ", sizeof(options));
196 	}
197 
198 	fprintf(stderr,
199 #ifndef FSTACK
200 	"usage: ifconfig [-f type:format] %sinterface address_family\n"
201 	"                [address [dest_address]] [parameters]\n"
202 	"       ifconfig interface create\n"
203 	"       ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
204 	"       ifconfig -l [-d] [-u] [address_family]\n"
205 	"       ifconfig %s[-d] [-m] [-u] [-v]\n",
206 #else
207 	"usage: ifconfig -p <f-stack proc_id> [-f type:format] %sinterface address_family\n"
208 	"                [address [dest_address]] [parameters]\n"
209 	"       ifconfig -p <f-stack proc_id> interface create\n"
210 	"       ifconfig -p <f-stack proc_id> -a %s[-d] [-m] [-u] [-v] [address_family]\n"
211 	"       ifconfig -p <f-stack proc_id> -l [-d] [-u] [address_family]\n"
212 	"       ifconfig -p <f-stack proc_id> %s[-d] [-m] [-u] [-v]\n",
213 #endif
214 		options, options, options);
215 	exit(1);
216 }
217 
218 #define ORDERS_SIZE(x) sizeof(x) / sizeof(x[0])
219 
220 static int
221 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
222 {
223 	struct ifaddrs *prev;
224 	struct ifa_order_elt *cur;
225 	unsigned int ord, af, ifa_ord;
226 
227 	prev = NULL;
228 	cur = NULL;
229 	ord = 0;
230 	ifa_ord = 0;
231 
232 	while (ifa != NULL) {
233 		if (prev == NULL ||
234 		    strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
235 			cur = calloc(1, sizeof(*cur));
236 
237 			if (cur == NULL)
238 				return (-1);
239 
240 			TAILQ_INSERT_TAIL(q, cur, link);
241 			cur->if_order = ifa_ord ++;
242 			cur->ifa = ifa;
243 			ord = 0;
244 		}
245 
246 		if (ifa->ifa_addr) {
247 			af = ifa->ifa_addr->sa_family;
248 
249 			if (af < ORDERS_SIZE(cur->af_orders) &&
250 			    cur->af_orders[af] == 0)
251 				cur->af_orders[af] = ++ord;
252 		}
253 		prev = ifa;
254 		ifa = ifa->ifa_next;
255 	}
256 
257 	return (0);
258 }
259 
260 static int
261 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
262 {
263 	struct ifa_order_elt *cur, *e1, *e2;
264 	unsigned int af1, af2;
265 	int ret;
266 
267 	e1 = e2 = NULL;
268 
269 	ret = strcmp(a->ifa_name, b->ifa_name);
270 	if (ret != 0) {
271 		TAILQ_FOREACH(cur, q, link) {
272 			if (e1 && e2)
273 				break;
274 
275 			if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
276 				e1 = cur;
277 			else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
278 				e2 = cur;
279 		}
280 
281 		if (!e1 || !e2)
282 			return (0);
283 		else
284 			return (e1->if_order - e2->if_order);
285 
286 	} else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
287 		TAILQ_FOREACH(cur, q, link) {
288 			if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
289 				e1 = cur;
290 				break;
291 			}
292 		}
293 
294 		if (!e1)
295 			return (0);
296 
297 		af1 = a->ifa_addr->sa_family;
298 		af2 = b->ifa_addr->sa_family;
299 
300 		if (af1 < ORDERS_SIZE(e1->af_orders) &&
301 		    af2 < ORDERS_SIZE(e1->af_orders))
302 			return (e1->af_orders[af1] - e1->af_orders[af2]);
303 	}
304 
305 	return (0);
306 }
307 
308 static void freeformat(void)
309 {
310 
311 	if (f_inet != NULL)
312 		free(f_inet);
313 	if (f_inet6 != NULL)
314 		free(f_inet6);
315 	if (f_ether != NULL)
316 		free(f_ether);
317 	if (f_addr != NULL)
318 		free(f_addr);
319 }
320 
321 static void setformat(char *input)
322 {
323 	char	*formatstr, *category, *modifier;
324 
325 	formatstr = strdup(input);
326 	while ((category = strsep(&formatstr, ",")) != NULL) {
327 		modifier = strchr(category, ':');
328 		if (modifier == NULL || modifier[1] == '\0') {
329 			warnx("Skipping invalid format specification: %s\n",
330 			    category);
331 			continue;
332 		}
333 
334 		/* Split the string on the separator, then seek past it */
335 		modifier[0] = '\0';
336 		modifier++;
337 
338 		if (strcmp(category, "addr") == 0)
339 			f_addr = strdup(modifier);
340 		else if (strcmp(category, "ether") == 0)
341 			f_ether = strdup(modifier);
342 		else if (strcmp(category, "inet") == 0)
343 			f_inet = strdup(modifier);
344 		else if (strcmp(category, "inet6") == 0)
345 			f_inet6 = strdup(modifier);
346 	}
347 	free(formatstr);
348 }
349 
350 #undef ORDERS_SIZE
351 
352 static struct ifaddrs *
353 sortifaddrs(struct ifaddrs *list,
354     int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *),
355     struct ifa_queue *q)
356 {
357 	struct ifaddrs *right, *temp, *last, *result, *next, *tail;
358 
359 	right = list;
360 	temp = list;
361 	last = list;
362 	result = NULL;
363 	next = NULL;
364 	tail = NULL;
365 
366 	if (!list || !list->ifa_next)
367 		return (list);
368 
369 	while (temp && temp->ifa_next) {
370 		last = right;
371 		right = right->ifa_next;
372 		temp = temp->ifa_next->ifa_next;
373 	}
374 
375 	last->ifa_next = NULL;
376 
377 	list = sortifaddrs(list, compare, q);
378 	right = sortifaddrs(right, compare, q);
379 
380 	while (list || right) {
381 
382 		if (!right) {
383 			next = list;
384 			list = list->ifa_next;
385 		} else if (!list) {
386 			next = right;
387 			right = right->ifa_next;
388 		} else if (compare(list, right, q) <= 0) {
389 			next = list;
390 			list = list->ifa_next;
391 		} else {
392 			next = right;
393 			right = right->ifa_next;
394 		}
395 
396 		if (!result)
397 			result = next;
398 		else
399 			tail->ifa_next = next;
400 
401 		tail = next;
402 	}
403 
404 	return (result);
405 }
406 
407 void printifnamemaybe()
408 {
409 	if (printifname)
410 		printf("%s\n", name);
411 }
412 
413 int
414 main(int argc, char *argv[])
415 {
416 	int c, all, namesonly, downonly, uponly;
417 	const struct afswtch *afp = NULL;
418 	int ifindex;
419 	struct ifaddrs *ifap, *sifap, *ifa;
420 	struct ifreq paifr;
421 	const struct sockaddr_dl *sdl;
422 	char options[1024], *cp, *envformat, *namecp = NULL;
423 	struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
424 	struct ifa_order_elt *cur, *tmp;
425 	const char *ifname;
426 	struct option *p;
427 	size_t iflen;
428 
429 	ff_ipc_init();
430 
431 	all = downonly = uponly = namesonly = noload = verbose = 0;
432 	f_inet = f_inet6 = f_ether = f_addr = NULL;
433 
434 	envformat = getenv("IFCONFIG_FORMAT");
435 	if (envformat != NULL)
436 		setformat(envformat);
437 
438 	/*
439 	 * Ensure we print interface name when expected to,
440 	 * even if we terminate early due to error.
441 	 */
442 	atexit(printifnamemaybe);
443 
444 	/* Parse leading line options */
445 #ifndef FSTACK
446 	strlcpy(options, "f:adklmnuv", sizeof(options));
447 #else
448 	strlcpy(options, "p:f:adklmnuv", sizeof(options));
449 #endif
450 	for (p = opts; p != NULL; p = p->next)
451 		strlcat(options, p->opt, sizeof(options));
452 	while ((c = getopt(argc, argv, options)) != -1) {
453 		switch (c) {
454 #ifdef FSTACK
455 		case 'p':
456 			ff_set_proc_id(atoi(optarg));
457 			break;
458 #endif
459 		case 'a':	/* scan all interfaces */
460 			all++;
461 			break;
462 		case 'd':	/* restrict scan to "down" interfaces */
463 			downonly++;
464 			break;
465 		case 'f':
466 			if (optarg == NULL)
467 				usage();
468 			setformat(optarg);
469 			break;
470 		case 'k':
471 			printkeys++;
472 			break;
473 		case 'l':	/* scan interface names only */
474 			namesonly++;
475 			break;
476 		case 'm':	/* show media choices in status */
477 			supmedia = 1;
478 			break;
479 		case 'n':	/* suppress module loading */
480 			noload++;
481 			break;
482 		case 'u':	/* restrict scan to "up" interfaces */
483 			uponly++;
484 			break;
485 		case 'v':
486 			verbose++;
487 			break;
488 		default:
489 			for (p = opts; p != NULL; p = p->next)
490 				if (p->opt[0] == c) {
491 					p->cb(optarg);
492 					break;
493 				}
494 			if (p == NULL)
495 				usage();
496 			break;
497 		}
498 	}
499 	argc -= optind;
500 	argv += optind;
501 
502 	/* -l cannot be used with -a or -m */
503 	if (namesonly && (all || supmedia))
504 		usage();
505 
506 	/* nonsense.. */
507 	if (uponly && downonly)
508 		usage();
509 
510 	/* no arguments is equivalent to '-a' */
511 	if (!namesonly && argc < 1)
512 		all = 1;
513 
514 	/* -a and -l allow an address family arg to limit the output */
515 	if (all || namesonly) {
516 		if (argc > 1)
517 			usage();
518 
519 		ifname = NULL;
520 		ifindex = 0;
521 		if (argc == 1) {
522 			afp = af_getbyname(*argv);
523 			if (afp == NULL) {
524 				warnx("Address family '%s' unknown.", *argv);
525 				usage();
526 			}
527 			if (afp->af_name != NULL)
528 				argc--, argv++;
529 			/* leave with afp non-zero */
530 		}
531 	} else {
532 		/* not listing, need an argument */
533 		if (argc < 1)
534 			usage();
535 
536 		ifname = *argv;
537 		argc--, argv++;
538 
539 		/* check and maybe load support for this interface */
540 		ifmaybeload(ifname);
541 
542 		ifindex = if_nametoindex(ifname);
543 		if (ifindex == 0) {
544 			/*
545 			 * NOTE:  We must special-case the `create' command
546 			 * right here as we would otherwise fail when trying
547 			 * to find the interface.
548 			 */
549 			if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
550 			    strcmp(argv[0], "plumb") == 0)) {
551 				iflen = strlcpy(name, ifname, sizeof(name));
552 				if (iflen >= sizeof(name))
553 					errx(1, "%s: cloning name too long",
554 					    ifname);
555 				ifconfig(argc, argv, 1, NULL);
556 				exit(0);
557 			}
558 #ifdef JAIL
559 			/*
560 			 * NOTE:  We have to special-case the `-vnet' command
561 			 * right here as we would otherwise fail when trying
562 			 * to find the interface as it lives in another vnet.
563 			 */
564 			if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
565 				iflen = strlcpy(name, ifname, sizeof(name));
566 				if (iflen >= sizeof(name))
567 					errx(1, "%s: interface name too long",
568 					    ifname);
569 				ifconfig(argc, argv, 0, NULL);
570 				exit(0);
571 			}
572 #endif
573 			errx(1, "interface %s does not exist", ifname);
574 		}
575 	}
576 
577 	/* Check for address family */
578 	if (argc > 0) {
579 		afp = af_getbyname(*argv);
580 		if (afp != NULL)
581 			argc--, argv++;
582 	}
583 
584 	if (getifaddrs(&ifap) != 0)
585 		err(EXIT_FAILURE, "getifaddrs");
586 
587 	cp = NULL;
588 
589 	if (calcorders(ifap, &q) != 0)
590 		err(EXIT_FAILURE, "calcorders");
591 
592 	sifap = sortifaddrs(ifap, cmpifaddrs, &q);
593 
594 	TAILQ_FOREACH_SAFE(cur, &q, link, tmp)
595 		free(cur);
596 
597 	ifindex = 0;
598 	for (ifa = sifap; ifa; ifa = ifa->ifa_next) {
599 		memset(&paifr, 0, sizeof(paifr));
600 		strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
601 		if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
602 			memcpy(&paifr.ifr_addr, ifa->ifa_addr,
603 			    ifa->ifa_addr->sa_len);
604 		}
605 
606 		if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
607 			continue;
608 		if (ifa->ifa_addr->sa_family == AF_LINK)
609 			sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
610 		else
611 			sdl = NULL;
612 		if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
613 			continue;
614 		iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
615 		if (iflen >= sizeof(name)) {
616 			warnx("%s: interface name too long, skipping",
617 			    ifa->ifa_name);
618 			continue;
619 		}
620 		cp = ifa->ifa_name;
621 
622 		if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
623 			continue;
624 		if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
625 			continue;
626 		if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
627 			continue;
628 		/*
629 		 * Are we just listing the interfaces?
630 		 */
631 		if (namesonly) {
632 			if (namecp == cp)
633 				continue;
634 			if (afp != NULL) {
635 				/* special case for "ether" address family */
636 				if (!strcmp(afp->af_name, "ether")) {
637 					if (sdl == NULL ||
638 					    (sdl->sdl_type != IFT_ETHER &&
639 					    sdl->sdl_type != IFT_L2VLAN &&
640 					    sdl->sdl_type != IFT_BRIDGE) ||
641 					    sdl->sdl_alen != ETHER_ADDR_LEN)
642 						continue;
643 				} else {
644 					if (ifa->ifa_addr->sa_family
645 					    != afp->af_af)
646 						continue;
647 				}
648 			}
649 			namecp = cp;
650 			ifindex++;
651 			if (ifindex > 1)
652 				printf(" ");
653 			fputs(name, stdout);
654 			continue;
655 		}
656 		ifindex++;
657 
658 		if (argc > 0)
659 			ifconfig(argc, argv, 0, afp);
660 		else
661 			status(afp, sdl, ifa);
662 	}
663 	if (namesonly)
664 		printf("\n");
665 	freeifaddrs(ifap);
666 
667 	freeformat();
668 	exit(0);
669 }
670 
671 static struct afswtch *afs = NULL;
672 
673 void
674 af_register(struct afswtch *p)
675 {
676 	p->af_next = afs;
677 	afs = p;
678 }
679 
680 static struct afswtch *
681 af_getbyname(const char *name)
682 {
683 	struct afswtch *afp;
684 
685 	for (afp = afs; afp !=  NULL; afp = afp->af_next)
686 		if (strcmp(afp->af_name, name) == 0)
687 			return afp;
688 	return NULL;
689 }
690 
691 static struct afswtch *
692 af_getbyfamily(int af)
693 {
694 	struct afswtch *afp;
695 
696 	for (afp = afs; afp != NULL; afp = afp->af_next)
697 		if (afp->af_af == af)
698 			return afp;
699 	return NULL;
700 }
701 
702 static void
703 af_other_status(int s)
704 {
705 	struct afswtch *afp;
706 	uint8_t afmask[howmany(AF_MAX, NBBY)];
707 
708 	memset(afmask, 0, sizeof(afmask));
709 	for (afp = afs; afp != NULL; afp = afp->af_next) {
710 		if (afp->af_other_status == NULL)
711 			continue;
712 		if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
713 			continue;
714 		afp->af_other_status(s);
715 		setbit(afmask, afp->af_af);
716 	}
717 }
718 
719 static void
720 af_all_tunnel_status(int s)
721 {
722 	struct afswtch *afp;
723 	uint8_t afmask[howmany(AF_MAX, NBBY)];
724 
725 	memset(afmask, 0, sizeof(afmask));
726 	for (afp = afs; afp != NULL; afp = afp->af_next) {
727 		if (afp->af_status_tunnel == NULL)
728 			continue;
729 		if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
730 			continue;
731 		afp->af_status_tunnel(s);
732 		setbit(afmask, afp->af_af);
733 	}
734 }
735 
736 static struct cmd *cmds = NULL;
737 
738 void
739 cmd_register(struct cmd *p)
740 {
741 	p->c_next = cmds;
742 	cmds = p;
743 }
744 
745 static const struct cmd *
746 cmd_lookup(const char *name, int iscreate)
747 {
748 	const struct cmd *p;
749 
750 	for (p = cmds; p != NULL; p = p->c_next)
751 		if (strcmp(name, p->c_name) == 0) {
752 			if (iscreate) {
753 				if (p->c_iscloneop)
754 					return p;
755 			} else {
756 				if (!p->c_iscloneop)
757 					return p;
758 			}
759 		}
760 	return NULL;
761 }
762 
763 struct callback {
764 	callback_func *cb_func;
765 	void	*cb_arg;
766 	struct callback *cb_next;
767 };
768 static struct callback *callbacks = NULL;
769 
770 void
771 callback_register(callback_func *func, void *arg)
772 {
773 	struct callback *cb;
774 
775 	cb = malloc(sizeof(struct callback));
776 	if (cb == NULL)
777 		errx(1, "unable to allocate memory for callback");
778 	cb->cb_func = func;
779 	cb->cb_arg = arg;
780 	cb->cb_next = callbacks;
781 	callbacks = cb;
782 }
783 
784 /* specially-handled commands */
785 static void setifaddr(const char *, int, int, const struct afswtch *);
786 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
787 
788 static void setifdstaddr(const char *, int, int, const struct afswtch *);
789 static const struct cmd setifdstaddr_cmd =
790 	DEF_CMD("ifdstaddr", 0, setifdstaddr);
791 
792 static int
793 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
794 {
795 	const struct afswtch *afp, *nafp;
796 	const struct cmd *p;
797 	struct callback *cb;
798 	int s;
799 
800 	strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
801 	afp = NULL;
802 	if (uafp != NULL)
803 		afp = uafp;
804 	/*
805 	 * This is the historical "accident" allowing users to configure IPv4
806 	 * addresses without the "inet" keyword which while a nice feature has
807 	 * proven to complicate other things.  We cannot remove this but only
808 	 * make sure we will never have a similar implicit default for IPv6 or
809 	 * any other address familiy.  We need a fallback though for
810 	 * ifconfig IF up/down etc. to work without INET support as people
811 	 * never used ifconfig IF link up/down, etc. either.
812 	 */
813 #ifndef RESCUE
814 #ifdef INET
815 	if (afp == NULL && feature_present("inet"))
816 		afp = af_getbyname("inet");
817 #endif
818 #endif
819 	if (afp == NULL)
820 		afp = af_getbyname("link");
821 	if (afp == NULL) {
822 		warnx("Please specify an address_family.");
823 		usage();
824 	}
825 top:
826 	ifr.ifr_addr.sa_family =
827 		afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
828 		AF_LOCAL : afp->af_af;
829 
830 	if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
831 	    (uafp != NULL || errno != EAFNOSUPPORT ||
832 	     (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
833 		err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
834 
835 	while (argc > 0) {
836 		p = cmd_lookup(*argv, iscreate);
837 		if (iscreate && p == NULL) {
838 			/*
839 			 * Push the clone create callback so the new
840 			 * device is created and can be used for any
841 			 * remaining arguments.
842 			 */
843 			cb = callbacks;
844 			if (cb == NULL)
845 				errx(1, "internal error, no callback");
846 			callbacks = cb->cb_next;
847 			cb->cb_func(s, cb->cb_arg);
848 			iscreate = 0;
849 			/*
850 			 * Handle any address family spec that
851 			 * immediately follows and potentially
852 			 * recreate the socket.
853 			 */
854 			nafp = af_getbyname(*argv);
855 			if (nafp != NULL) {
856 				argc--, argv++;
857 				if (nafp != afp) {
858 					close(s);
859 					afp = nafp;
860 					goto top;
861 				}
862 			}
863 			/*
864 			 * Look for a normal parameter.
865 			 */
866 			continue;
867 		}
868 		if (p == NULL) {
869 			/*
870 			 * Not a recognized command, choose between setting
871 			 * the interface address and the dst address.
872 			 */
873 			p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
874 		}
875 		if (p->c_u.c_func || p->c_u.c_func2) {
876 			if (p->c_parameter == NEXTARG) {
877 				if (argv[1] == NULL)
878 					errx(1, "'%s' requires argument",
879 					    p->c_name);
880 				p->c_u.c_func(argv[1], 0, s, afp);
881 				argc--, argv++;
882 			} else if (p->c_parameter == OPTARG) {
883 				p->c_u.c_func(argv[1], 0, s, afp);
884 				if (argv[1] != NULL)
885 					argc--, argv++;
886 			} else if (p->c_parameter == NEXTARG2) {
887 				if (argc < 3)
888 					errx(1, "'%s' requires 2 arguments",
889 					    p->c_name);
890 				p->c_u.c_func2(argv[1], argv[2], s, afp);
891 				argc -= 2, argv += 2;
892 			} else
893 				p->c_u.c_func(*argv, p->c_parameter, s, afp);
894 		}
895 		argc--, argv++;
896 	}
897 
898 	/*
899 	 * Do any post argument processing required by the address family.
900 	 */
901 	if (afp->af_postproc != NULL)
902 		afp->af_postproc(s, afp);
903 	/*
904 	 * Do deferred callbacks registered while processing
905 	 * command-line arguments.
906 	 */
907 	for (cb = callbacks; cb != NULL; cb = cb->cb_next)
908 		cb->cb_func(s, cb->cb_arg);
909 	/*
910 	 * Do deferred operations.
911 	 */
912 	if (clearaddr) {
913 		if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
914 			warnx("interface %s cannot change %s addresses!",
915 			      name, afp->af_name);
916 			clearaddr = 0;
917 		}
918 	}
919 	if (clearaddr) {
920 		int ret;
921 		strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
922 			sizeof ifr.ifr_name);
923 		ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
924 		if (ret < 0) {
925 			if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
926 				/* means no previous address for interface */
927 			} else
928 				Perror("ioctl (SIOCDIFADDR)");
929 		}
930 	}
931 	if (newaddr) {
932 		if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
933 			warnx("interface %s cannot change %s addresses!",
934 			      name, afp->af_name);
935 			newaddr = 0;
936 		}
937 	}
938 	if (newaddr && (setaddr || setmask)) {
939 		strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
940 			sizeof ifr.ifr_name);
941 		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
942 			Perror("ioctl (SIOCAIFADDR)");
943 	}
944 
945 	close(s);
946 	return(0);
947 }
948 
949 /*ARGSUSED*/
950 static void
951 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
952 {
953 	if (afp->af_getaddr == NULL)
954 		return;
955 	/*
956 	 * Delay the ioctl to set the interface addr until flags are all set.
957 	 * The address interpretation may depend on the flags,
958 	 * and the flags may change when the address is set.
959 	 */
960 	setaddr++;
961 	if (doalias == 0 && afp->af_af != AF_LINK)
962 		clearaddr = 1;
963 	afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
964 }
965 
966 static void
967 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
968 {
969 	struct addrinfo *srcres, *dstres;
970 	int ecode;
971 
972 	if (afp->af_settunnel == NULL) {
973 		warn("address family %s does not support tunnel setup",
974 			afp->af_name);
975 		return;
976 	}
977 
978 	if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
979 		errx(1, "error in parsing address string: %s",
980 		    gai_strerror(ecode));
981 
982 	if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
983 		errx(1, "error in parsing address string: %s",
984 		    gai_strerror(ecode));
985 
986 	if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
987 		errx(1,
988 		    "source and destination address families do not match");
989 
990 	afp->af_settunnel(s, srcres, dstres);
991 
992 	freeaddrinfo(srcres);
993 	freeaddrinfo(dstres);
994 }
995 
996 /* ARGSUSED */
997 static void
998 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
999 {
1000 
1001 	if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
1002 		err(1, "SIOCDIFPHYADDR");
1003 }
1004 
1005 #ifdef JAIL
1006 static void
1007 setifvnet(const char *jname, int dummy __unused, int s,
1008     const struct afswtch *afp)
1009 {
1010 	struct ifreq my_ifr;
1011 
1012 	memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1013 	my_ifr.ifr_jid = jail_getid(jname);
1014 	if (my_ifr.ifr_jid < 0)
1015 		errx(1, "%s", jail_errmsg);
1016 	if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
1017 		err(1, "SIOCSIFVNET");
1018 }
1019 
1020 static void
1021 setifrvnet(const char *jname, int dummy __unused, int s,
1022     const struct afswtch *afp)
1023 {
1024 	struct ifreq my_ifr;
1025 
1026 	memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1027 	my_ifr.ifr_jid = jail_getid(jname);
1028 	if (my_ifr.ifr_jid < 0)
1029 		errx(1, "%s", jail_errmsg);
1030 	if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
1031 		err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
1032 }
1033 #endif
1034 
1035 static void
1036 setifnetmask(const char *addr, int dummy __unused, int s,
1037     const struct afswtch *afp)
1038 {
1039 	if (afp->af_getaddr != NULL) {
1040 		setmask++;
1041 		afp->af_getaddr(addr, MASK);
1042 	}
1043 }
1044 
1045 static void
1046 setifbroadaddr(const char *addr, int dummy __unused, int s,
1047     const struct afswtch *afp)
1048 {
1049 	if (afp->af_getaddr != NULL)
1050 		afp->af_getaddr(addr, DSTADDR);
1051 }
1052 
1053 static void
1054 notealias(const char *addr, int param, int s, const struct afswtch *afp)
1055 {
1056 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1057 	if (setaddr && doalias == 0 && param < 0)
1058 		if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1059 			bcopy((caddr_t)rqtosa(af_addreq),
1060 			      (caddr_t)rqtosa(af_ridreq),
1061 			      rqtosa(af_addreq)->sa_len);
1062 	doalias = param;
1063 	if (param < 0) {
1064 		clearaddr = 1;
1065 		newaddr = 0;
1066 	} else
1067 		clearaddr = 0;
1068 #undef rqtosa
1069 }
1070 
1071 /*ARGSUSED*/
1072 static void
1073 setifdstaddr(const char *addr, int param __unused, int s,
1074     const struct afswtch *afp)
1075 {
1076 	if (afp->af_getaddr != NULL)
1077 		afp->af_getaddr(addr, DSTADDR);
1078 }
1079 
1080 /*
1081  * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1082  * of the ifreq structure, which may confuse other parts of ifconfig.
1083  * Make a private copy so we can avoid that.
1084  */
1085 static void
1086 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1087 {
1088 	struct ifreq		my_ifr;
1089 	int flags;
1090 
1091 	memset(&my_ifr, 0, sizeof(my_ifr));
1092 	(void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1093 
1094  	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1095  		Perror("ioctl (SIOCGIFFLAGS)");
1096  		exit(1);
1097  	}
1098 	flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1099 
1100 	if (value < 0) {
1101 		value = -value;
1102 		flags &= ~value;
1103 	} else
1104 		flags |= value;
1105 	my_ifr.ifr_flags = flags & 0xffff;
1106 	my_ifr.ifr_flagshigh = flags >> 16;
1107 	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1108 		Perror(vname);
1109 }
1110 
1111 void
1112 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1113 {
1114 	int flags;
1115 
1116  	if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1117  		Perror("ioctl (SIOCGIFCAP)");
1118  		exit(1);
1119  	}
1120 	flags = ifr.ifr_curcap;
1121 	if (value < 0) {
1122 		value = -value;
1123 		flags &= ~value;
1124 	} else
1125 		flags |= value;
1126 	flags &= ifr.ifr_reqcap;
1127 	ifr.ifr_reqcap = flags;
1128 	if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1129 		Perror(vname);
1130 }
1131 
1132 static void
1133 setifmetric(const char *val, int dummy __unused, int s,
1134     const struct afswtch *afp)
1135 {
1136 	strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1137 	ifr.ifr_metric = atoi(val);
1138 	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1139 		err(1, "ioctl SIOCSIFMETRIC (set metric)");
1140 }
1141 
1142 static void
1143 setifmtu(const char *val, int dummy __unused, int s,
1144     const struct afswtch *afp)
1145 {
1146 	strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1147 	ifr.ifr_mtu = atoi(val);
1148 	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1149 		err(1, "ioctl SIOCSIFMTU (set mtu)");
1150 }
1151 
1152 static void
1153 setifname(const char *val, int dummy __unused, int s,
1154     const struct afswtch *afp)
1155 {
1156 	char *newname;
1157 
1158 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1159 
1160 	newname = strdup(val);
1161 	if (newname == NULL)
1162 		err(1, "no memory to set ifname");
1163 	ifr.ifr_data = newname;
1164 #ifndef FSTACK
1165 	if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1166 #else
1167 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
1168 	size_t clen = strlen(newname);
1169 	if (ioctl_va(s, SIOCSIFNAME, (caddr_t)&ifr, 3, offset, newname, clen) < 0) {
1170 #endif
1171 		free(newname);
1172 		err(1, "ioctl SIOCSIFNAME (set name)");
1173 	}
1174 	printifname = 1;
1175 	strlcpy(name, newname, sizeof(name));
1176 	free(newname);
1177 }
1178 
1179 /* ARGSUSED */
1180 static void
1181 setifdescr(const char *val, int dummy __unused, int s,
1182     const struct afswtch *afp)
1183 {
1184 	char *newdescr;
1185 
1186 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1187 
1188 	ifr.ifr_buffer.length = strlen(val) + 1;
1189 	if (ifr.ifr_buffer.length == 1) {
1190 		ifr.ifr_buffer.buffer = newdescr = NULL;
1191 		ifr.ifr_buffer.length = 0;
1192 	} else {
1193 		newdescr = strdup(val);
1194 		ifr.ifr_buffer.buffer = newdescr;
1195 		if (newdescr == NULL) {
1196 			warn("no memory to set ifdescr");
1197 			return;
1198 		}
1199 	}
1200 
1201 #ifdef FSTACK
1202 	if (ifr.ifr_buffer.buffer != NULL) {
1203 		size_t offset = (char *)&(ifr.ifr_buffer.buffer) - (char *)&(ifr);
1204 		if (ioctl_va(s, SIOCSIFDESCR, (caddr_t)&ifr, 3, offset,
1205 			ifr.ifr_buffer.buffer, ifr.ifr_buffer.length) < 0)
1206 			err(1, "ioctl SIOCSIFDESCR (set descr)");
1207 	} else
1208 #endif
1209 		if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1210 			err(1, "ioctl SIOCSIFDESCR (set descr)");
1211 
1212 	free(newdescr);
1213 }
1214 
1215 /* ARGSUSED */
1216 static void
1217 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
1218 {
1219 
1220 	setifdescr("", 0, s, 0);
1221 }
1222 
1223 #define	IFFBITS \
1224 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
1225 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1226 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
1227 
1228 #define	IFCAPBITS \
1229 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1230 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1231 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1232 "\26RXCSUM_IPV6\27TXCSUM_IPV6"
1233 
1234 /*
1235  * Print the status of the interface.  If an address family was
1236  * specified, show only it; otherwise, show them all.
1237  */
1238 static void
1239 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1240 	struct ifaddrs *ifa)
1241 {
1242 	struct ifaddrs *ift;
1243 	int allfamilies, s;
1244 	struct ifstat ifs;
1245 
1246 	if (afp == NULL) {
1247 		allfamilies = 1;
1248 		ifr.ifr_addr.sa_family = AF_LOCAL;
1249 	} else {
1250 		allfamilies = 0;
1251 		ifr.ifr_addr.sa_family =
1252 		    afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1253 	}
1254 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1255 
1256 	s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1257 	if (s < 0)
1258 		err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1259 
1260 	printf("%s: ", name);
1261 	printb("flags", ifa->ifa_flags, IFFBITS);
1262 	if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1263 		printf(" metric %d", ifr.ifr_metric);
1264 	if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1265 		printf(" mtu %d", ifr.ifr_mtu);
1266 	putchar('\n');
1267 
1268 	for (;;) {
1269 		if ((descr = reallocf(descr, descrlen)) != NULL) {
1270 			ifr.ifr_buffer.buffer = descr;
1271 			ifr.ifr_buffer.length = descrlen;
1272 #ifndef FSTACK
1273 			if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1274 #else
1275 			size_t offset = (char *)&(ifr.ifr_buffer.buffer) - (char *)&(ifr);
1276 			if (ioctl_va(s, SIOCGIFDESCR, &ifr, 3, offset, descr, descrlen) == 0) {
1277 #endif
1278 				if (ifr.ifr_buffer.buffer == descr) {
1279 					if (strlen(descr) > 0)
1280 						printf("\tdescription: %s\n",
1281 						    descr);
1282 				} else if (ifr.ifr_buffer.length > descrlen) {
1283 					descrlen = ifr.ifr_buffer.length;
1284 					continue;
1285 				}
1286 			}
1287 		} else
1288 			warn("unable to allocate memory for interface"
1289 			    "description");
1290 		break;
1291 	}
1292 
1293 	if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1294 		if (ifr.ifr_curcap != 0) {
1295 			printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1296 			putchar('\n');
1297 		}
1298 		if (supmedia && ifr.ifr_reqcap != 0) {
1299 			printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1300 			putchar('\n');
1301 		}
1302 	}
1303 
1304 	tunnel_status(s);
1305 
1306 	for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1307 		if (ift->ifa_addr == NULL)
1308 			continue;
1309 		if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1310 			continue;
1311 		if (allfamilies) {
1312 			const struct afswtch *p;
1313 			p = af_getbyfamily(ift->ifa_addr->sa_family);
1314 			if (p != NULL && p->af_status != NULL)
1315 				p->af_status(s, ift);
1316 		} else if (afp->af_af == ift->ifa_addr->sa_family)
1317 			afp->af_status(s, ift);
1318 	}
1319 #if 0
1320 	if (allfamilies || afp->af_af == AF_LINK) {
1321 		const struct afswtch *lafp;
1322 
1323 		/*
1324 		 * Hack; the link level address is received separately
1325 		 * from the routing information so any address is not
1326 		 * handled above.  Cobble together an entry and invoke
1327 		 * the status method specially.
1328 		 */
1329 		lafp = af_getbyname("lladdr");
1330 		if (lafp != NULL) {
1331 			info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1332 			lafp->af_status(s, &info);
1333 		}
1334 	}
1335 #endif
1336 	if (allfamilies)
1337 		af_other_status(s);
1338 	else if (afp->af_other_status != NULL)
1339 		afp->af_other_status(s);
1340 
1341 	strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1342 	if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1343 		printf("%s", ifs.ascii);
1344 
1345 #ifndef FSTACK
1346 	if (verbose > 0)
1347 		sfp_status(s, &ifr, verbose);
1348 #endif
1349 
1350 	close(s);
1351 	return;
1352 }
1353 
1354 static void
1355 tunnel_status(int s)
1356 {
1357 	af_all_tunnel_status(s);
1358 }
1359 
1360 void
1361 Perror(const char *cmd)
1362 {
1363 	switch (errno) {
1364 
1365 	case ENXIO:
1366 		errx(1, "%s: no such interface", cmd);
1367 		break;
1368 
1369 	case EPERM:
1370 		errx(1, "%s: permission denied", cmd);
1371 		break;
1372 
1373 	default:
1374 		err(1, "%s", cmd);
1375 	}
1376 }
1377 
1378 /*
1379  * Print a value a la the %b format of the kernel's printf
1380  */
1381 void
1382 printb(const char *s, unsigned v, const char *bits)
1383 {
1384 	int i, any = 0;
1385 	char c;
1386 
1387 	if (bits && *bits == 8)
1388 		printf("%s=%o", s, v);
1389 	else
1390 		printf("%s=%x", s, v);
1391 	bits++;
1392 	if (bits) {
1393 		putchar('<');
1394 		while ((i = *bits++) != '\0') {
1395 			if (v & (1 << (i-1))) {
1396 				if (any)
1397 					putchar(',');
1398 				any = 1;
1399 				for (; (c = *bits) > 32; bits++)
1400 					putchar(c);
1401 			} else
1402 				for (; *bits > 32; bits++)
1403 					;
1404 		}
1405 		putchar('>');
1406 	}
1407 }
1408 
1409 void
1410 print_vhid(const struct ifaddrs *ifa, const char *s)
1411 {
1412 	struct if_data *ifd;
1413 
1414 	if (ifa->ifa_data == NULL)
1415 		return;
1416 
1417 	ifd = ifa->ifa_data;
1418 	if (ifd->ifi_vhid == 0)
1419 		return;
1420 
1421 	printf("vhid %d ", ifd->ifi_vhid);
1422 }
1423 
1424 void
1425 ifmaybeload(const char *name)
1426 {
1427 #ifndef FSTACK
1428 #define MOD_PREFIX_LEN		3	/* "if_" */
1429 	struct module_stat mstat;
1430 	int fileid, modid;
1431 	char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1432 	const char *cp;
1433 
1434 	/* loading suppressed by the user */
1435 	if (noload)
1436 		return;
1437 
1438 	/* trim the interface number off the end */
1439 	strlcpy(ifname, name, sizeof(ifname));
1440 	for (dp = ifname; *dp != 0; dp++)
1441 		if (isdigit(*dp)) {
1442 			*dp = 0;
1443 			break;
1444 		}
1445 
1446 	/* turn interface and unit into module name */
1447 	strlcpy(ifkind, "if_", sizeof(ifkind));
1448 	strlcat(ifkind, ifname, sizeof(ifkind));
1449 
1450 	/* scan files in kernel */
1451 	mstat.version = sizeof(struct module_stat);
1452 	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1453 		/* scan modules in file */
1454 		for (modid = kldfirstmod(fileid); modid > 0;
1455 		     modid = modfnext(modid)) {
1456 			if (modstat(modid, &mstat) < 0)
1457 				continue;
1458 			/* strip bus name if present */
1459 			if ((cp = strchr(mstat.name, '/')) != NULL) {
1460 				cp++;
1461 			} else {
1462 				cp = mstat.name;
1463 			}
1464 			/* already loaded? */
1465 			if (strcmp(ifname, cp) == 0 ||
1466 			    strcmp(ifkind, cp) == 0)
1467 				return;
1468 		}
1469 	}
1470 
1471 	/* not present, we should try to load it */
1472 	kldload(ifkind);
1473 #endif
1474 }
1475 
1476 static struct cmd basic_cmds[] = {
1477 	DEF_CMD("up",		IFF_UP,		setifflags),
1478 	DEF_CMD("down",		-IFF_UP,	setifflags),
1479 	DEF_CMD("arp",		-IFF_NOARP,	setifflags),
1480 	DEF_CMD("-arp",		IFF_NOARP,	setifflags),
1481 	DEF_CMD("debug",	IFF_DEBUG,	setifflags),
1482 	DEF_CMD("-debug",	-IFF_DEBUG,	setifflags),
1483 	DEF_CMD_ARG("description",		setifdescr),
1484 	DEF_CMD_ARG("descr",			setifdescr),
1485 	DEF_CMD("-description",	0,		unsetifdescr),
1486 	DEF_CMD("-descr",	0,		unsetifdescr),
1487 	DEF_CMD("promisc",	IFF_PPROMISC,	setifflags),
1488 	DEF_CMD("-promisc",	-IFF_PPROMISC,	setifflags),
1489 	DEF_CMD("add",		IFF_UP,		notealias),
1490 	DEF_CMD("alias",	IFF_UP,		notealias),
1491 	DEF_CMD("-alias",	-IFF_UP,	notealias),
1492 	DEF_CMD("delete",	-IFF_UP,	notealias),
1493 	DEF_CMD("remove",	-IFF_UP,	notealias),
1494 #ifdef notdef
1495 #define	EN_SWABIPS	0x1000
1496 	DEF_CMD("swabips",	EN_SWABIPS,	setifflags),
1497 	DEF_CMD("-swabips",	-EN_SWABIPS,	setifflags),
1498 #endif
1499 	DEF_CMD_ARG("netmask",			setifnetmask),
1500 	DEF_CMD_ARG("metric",			setifmetric),
1501 	DEF_CMD_ARG("broadcast",		setifbroadaddr),
1502 	DEF_CMD_ARG2("tunnel",			settunnel),
1503 	DEF_CMD("-tunnel", 0,			deletetunnel),
1504 	DEF_CMD("deletetunnel", 0,		deletetunnel),
1505 #ifdef JAIL
1506 	DEF_CMD_ARG("vnet",			setifvnet),
1507 	DEF_CMD_ARG("-vnet",			setifrvnet),
1508 #endif
1509 	DEF_CMD("link0",	IFF_LINK0,	setifflags),
1510 	DEF_CMD("-link0",	-IFF_LINK0,	setifflags),
1511 	DEF_CMD("link1",	IFF_LINK1,	setifflags),
1512 	DEF_CMD("-link1",	-IFF_LINK1,	setifflags),
1513 	DEF_CMD("link2",	IFF_LINK2,	setifflags),
1514 	DEF_CMD("-link2",	-IFF_LINK2,	setifflags),
1515 	DEF_CMD("monitor",	IFF_MONITOR,	setifflags),
1516 	DEF_CMD("-monitor",	-IFF_MONITOR,	setifflags),
1517 	DEF_CMD("staticarp",	IFF_STATICARP,	setifflags),
1518 	DEF_CMD("-staticarp",	-IFF_STATICARP,	setifflags),
1519 	DEF_CMD("rxcsum6",	IFCAP_RXCSUM_IPV6,	setifcap),
1520 	DEF_CMD("-rxcsum6",	-IFCAP_RXCSUM_IPV6,	setifcap),
1521 	DEF_CMD("txcsum6",	IFCAP_TXCSUM_IPV6,	setifcap),
1522 	DEF_CMD("-txcsum6",	-IFCAP_TXCSUM_IPV6,	setifcap),
1523 	DEF_CMD("rxcsum",	IFCAP_RXCSUM,	setifcap),
1524 	DEF_CMD("-rxcsum",	-IFCAP_RXCSUM,	setifcap),
1525 	DEF_CMD("txcsum",	IFCAP_TXCSUM,	setifcap),
1526 	DEF_CMD("-txcsum",	-IFCAP_TXCSUM,	setifcap),
1527 	DEF_CMD("netcons",	IFCAP_NETCONS,	setifcap),
1528 	DEF_CMD("-netcons",	-IFCAP_NETCONS,	setifcap),
1529 	DEF_CMD("polling",	IFCAP_POLLING,	setifcap),
1530 	DEF_CMD("-polling",	-IFCAP_POLLING,	setifcap),
1531 	DEF_CMD("tso6",		IFCAP_TSO6,	setifcap),
1532 	DEF_CMD("-tso6",	-IFCAP_TSO6,	setifcap),
1533 	DEF_CMD("tso4",		IFCAP_TSO4,	setifcap),
1534 	DEF_CMD("-tso4",	-IFCAP_TSO4,	setifcap),
1535 	DEF_CMD("tso",		IFCAP_TSO,	setifcap),
1536 	DEF_CMD("-tso",		-IFCAP_TSO,	setifcap),
1537 	DEF_CMD("toe",		IFCAP_TOE,	setifcap),
1538 	DEF_CMD("-toe",		-IFCAP_TOE,	setifcap),
1539 	DEF_CMD("lro",		IFCAP_LRO,	setifcap),
1540 	DEF_CMD("-lro",		-IFCAP_LRO,	setifcap),
1541 	DEF_CMD("wol",		IFCAP_WOL,	setifcap),
1542 	DEF_CMD("-wol",		-IFCAP_WOL,	setifcap),
1543 	DEF_CMD("wol_ucast",	IFCAP_WOL_UCAST,	setifcap),
1544 	DEF_CMD("-wol_ucast",	-IFCAP_WOL_UCAST,	setifcap),
1545 	DEF_CMD("wol_mcast",	IFCAP_WOL_MCAST,	setifcap),
1546 	DEF_CMD("-wol_mcast",	-IFCAP_WOL_MCAST,	setifcap),
1547 	DEF_CMD("wol_magic",	IFCAP_WOL_MAGIC,	setifcap),
1548 	DEF_CMD("-wol_magic",	-IFCAP_WOL_MAGIC,	setifcap),
1549 	DEF_CMD("normal",	-IFF_LINK0,	setifflags),
1550 	DEF_CMD("compress",	IFF_LINK0,	setifflags),
1551 	DEF_CMD("noicmp",	IFF_LINK1,	setifflags),
1552 	DEF_CMD_ARG("mtu",			setifmtu),
1553 	DEF_CMD_ARG("name",			setifname),
1554 };
1555 
1556 static __constructor void
1557 ifconfig_ctor(void)
1558 {
1559 	size_t i;
1560 
1561 	for (i = 0; i < nitems(basic_cmds);  i++)
1562 		cmd_register(&basic_cmds[i]);
1563 }
1564