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