xref: /freebsd/sbin/ifconfig/ifconfig.c (revision 9247238cc4b8835892a47701136b0fd073f8d67c)
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 #if 0
37 static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
38 #endif
39 static const char rcsid[] =
40   "$FreeBSD$";
41 #endif /* not lint */
42 
43 #include <sys/param.h>
44 #include <sys/ioctl.h>
45 #ifdef JAIL
46 #include <sys/jail.h>
47 #endif
48 #include <sys/module.h>
49 #include <sys/linker.h>
50 #include <sys/nv.h>
51 #include <sys/queue.h>
52 #include <sys/socket.h>
53 #include <sys/time.h>
54 
55 #include <net/ethernet.h>
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
60 
61 /* IP */
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <arpa/inet.h>
65 #include <netdb.h>
66 
67 #include <fnmatch.h>
68 #include <ifaddrs.h>
69 #include <ctype.h>
70 #include <err.h>
71 #include <errno.h>
72 #include <fcntl.h>
73 #ifdef JAIL
74 #include <jail.h>
75 #endif
76 #include <stdbool.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <unistd.h>
81 
82 #include <libifconfig.h>
83 
84 #include "ifconfig.h"
85 
86 ifconfig_handle_t *lifh;
87 
88 /*
89  * Since "struct ifreq" is composed of various union members, callers
90  * should pay special attention to interpret the value.
91  * (.e.g. little/big endian difference in the structure.)
92  */
93 struct	ifreq ifr;
94 
95 //char	name[IFNAMSIZ];
96 #ifdef WITHOUT_NETLINK
97 static char	*descr = NULL;
98 static size_t	descrlen = 64;
99 #endif
100 static int	setaddr;
101 static int	setmask;
102 static int	doalias;
103 static int	clearaddr;
104 int	newaddr = 1;
105 
106 int	exit_code = 0;
107 
108 static char ifname_to_print[IFNAMSIZ]; /* Helper for printifnamemaybe() */
109 
110 /* Formatter Strings */
111 char	*f_inet, *f_inet6, *f_ether, *f_addr;
112 
113 #ifdef WITHOUT_NETLINK
114 static void list_interfaces_ioctl(struct ifconfig_args *args);
115 static	void status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
116 		struct ifaddrs *ifa);
117 #endif
118 static _Noreturn void usage(void);
119 static void Perrorc(const char *cmd, int error);
120 
121 static int getifflags(const char *ifname, int us, bool err_ok);
122 
123 static struct afswtch *af_getbyname(const char *name);
124 
125 static struct option *opts = NULL;
126 
127 struct ifa_order_elt {
128 	int if_order;
129 	int af_orders[255];
130 	struct ifaddrs *ifa;
131 	TAILQ_ENTRY(ifa_order_elt) link;
132 };
133 
134 TAILQ_HEAD(ifa_queue, ifa_order_elt);
135 
136 static struct module_map_entry {
137 	const char *ifname;
138 	const char *kldname;
139 } module_map[] = {
140 	{
141 		.ifname = "tun",
142 		.kldname = "if_tuntap",
143 	},
144 	{
145 		.ifname = "tap",
146 		.kldname = "if_tuntap",
147 	},
148 	{
149 		.ifname = "vmnet",
150 		.kldname = "if_tuntap",
151 	},
152 	{
153 		.ifname = "ipsec",
154 		.kldname = "ipsec",
155 	},
156 	{
157 		/*
158 		 * This mapping exists because there is a conflicting enc module
159 		 * in CAM.  ifconfig's guessing behavior will attempt to match
160 		 * the ifname to a module as well as if_${ifname} and clash with
161 		 * CAM enc.  This is an assertion of the correct module to load.
162 		 */
163 		.ifname = "enc",
164 		.kldname = "if_enc",
165 	},
166 };
167 
168 
169 void
170 opt_register(struct option *p)
171 {
172 	p->next = opts;
173 	opts = p;
174 }
175 
176 static void
177 usage(void)
178 {
179 	char options[1024];
180 	struct option *p;
181 
182 	/* XXX not right but close enough for now */
183 	options[0] = '\0';
184 	for (p = opts; p != NULL; p = p->next) {
185 		strlcat(options, p->opt_usage, sizeof(options));
186 		strlcat(options, " ", sizeof(options));
187 	}
188 
189 	fprintf(stderr,
190 	"usage: ifconfig [-j jail] [-f type:format] %sinterface address_family\n"
191 	"                [address [dest_address]] [parameters]\n"
192 	"       ifconfig [-j jail] interface create\n"
193 	"       ifconfig [-j jail] -a %s[-d] [-m] [-u] [-v] [address_family]\n"
194 	"       ifconfig [-j jail] -l [-d] [-u] [address_family]\n"
195 	"       ifconfig [-j jail] %s[-d] [-m] [-u] [-v]\n",
196 		options, options, options);
197 	exit(1);
198 }
199 
200 static void
201 ifname_update(if_ctx *ctx, const char *name)
202 {
203 	strlcpy(ctx->_ifname_storage_ioctl, name, sizeof(ctx->_ifname_storage_ioctl));
204 	ctx->ifname = ctx->_ifname_storage_ioctl;
205 
206 	strlcpy(ifname_to_print, name, sizeof(ifname_to_print));
207 }
208 
209 void
210 ifcreate_ioctl(if_ctx *ctx, struct ifreq *ifr)
211 {
212 	char ifname_orig[IFNAMSIZ];
213 
214 	strlcpy(ifname_orig, ifr->ifr_name, sizeof(ifname_orig));
215 
216 	if (ioctl(ctx->io_s, SIOCIFCREATE2, ifr) < 0) {
217 		switch (errno) {
218 		case EEXIST:
219 			errx(1, "interface %s already exists", ifr->ifr_name);
220 		default:
221 			err(1, "SIOCIFCREATE2 (%s)", ifr->ifr_name);
222 		}
223 	}
224 
225 	if (strncmp(ifname_orig, ifr->ifr_name, sizeof(ifname_orig)) != 0)
226 		ifname_update(ctx, ifr->ifr_name);
227 }
228 
229 #ifdef WITHOUT_NETLINK
230 static int
231 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
232 {
233 	struct ifaddrs *prev;
234 	struct ifa_order_elt *cur;
235 	unsigned int ord, af, ifa_ord;
236 
237 	prev = NULL;
238 	cur = NULL;
239 	ord = 0;
240 	ifa_ord = 0;
241 
242 	while (ifa != NULL) {
243 		if (prev == NULL ||
244 		    strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
245 			cur = calloc(1, sizeof(*cur));
246 
247 			if (cur == NULL)
248 				return (-1);
249 
250 			TAILQ_INSERT_TAIL(q, cur, link);
251 			cur->if_order = ifa_ord ++;
252 			cur->ifa = ifa;
253 			ord = 0;
254 		}
255 
256 		if (ifa->ifa_addr) {
257 			af = ifa->ifa_addr->sa_family;
258 
259 			if (af < nitems(cur->af_orders) &&
260 			    cur->af_orders[af] == 0)
261 				cur->af_orders[af] = ++ord;
262 		}
263 		prev = ifa;
264 		ifa = ifa->ifa_next;
265 	}
266 
267 	return (0);
268 }
269 
270 static int
271 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
272 {
273 	struct ifa_order_elt *cur, *e1, *e2;
274 	unsigned int af1, af2;
275 	int ret;
276 
277 	e1 = e2 = NULL;
278 
279 	ret = strcmp(a->ifa_name, b->ifa_name);
280 	if (ret != 0) {
281 		TAILQ_FOREACH(cur, q, link) {
282 			if (e1 && e2)
283 				break;
284 
285 			if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
286 				e1 = cur;
287 			else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
288 				e2 = cur;
289 		}
290 
291 		if (!e1 || !e2)
292 			return (0);
293 		else
294 			return (e1->if_order - e2->if_order);
295 
296 	} else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
297 		TAILQ_FOREACH(cur, q, link) {
298 			if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
299 				e1 = cur;
300 				break;
301 			}
302 		}
303 
304 		if (!e1)
305 			return (0);
306 
307 		af1 = a->ifa_addr->sa_family;
308 		af2 = b->ifa_addr->sa_family;
309 
310 		if (af1 < nitems(e1->af_orders) && af2 < nitems(e1->af_orders))
311 			return (e1->af_orders[af1] - e1->af_orders[af2]);
312 	}
313 
314 	return (0);
315 }
316 #endif
317 
318 static void freeformat(void)
319 {
320 
321 	if (f_inet != NULL)
322 		free(f_inet);
323 	if (f_inet6 != NULL)
324 		free(f_inet6);
325 	if (f_ether != NULL)
326 		free(f_ether);
327 	if (f_addr != NULL)
328 		free(f_addr);
329 }
330 
331 static void setformat(char *input)
332 {
333 	char	*formatstr, *category, *modifier;
334 
335 	formatstr = strdup(input);
336 	while ((category = strsep(&formatstr, ",")) != NULL) {
337 		modifier = strchr(category, ':');
338 		if (modifier == NULL || modifier[1] == '\0') {
339 			warnx("Skipping invalid format specification: %s\n",
340 			    category);
341 			continue;
342 		}
343 
344 		/* Split the string on the separator, then seek past it */
345 		modifier[0] = '\0';
346 		modifier++;
347 
348 		if (strcmp(category, "addr") == 0)
349 			f_addr = strdup(modifier);
350 		else if (strcmp(category, "ether") == 0)
351 			f_ether = strdup(modifier);
352 		else if (strcmp(category, "inet") == 0)
353 			f_inet = strdup(modifier);
354 		else if (strcmp(category, "inet6") == 0)
355 			f_inet6 = strdup(modifier);
356 	}
357 	free(formatstr);
358 }
359 
360 #ifdef WITHOUT_NETLINK
361 static struct ifaddrs *
362 sortifaddrs(struct ifaddrs *list,
363     int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *),
364     struct ifa_queue *q)
365 {
366 	struct ifaddrs *right, *temp, *last, *result, *next, *tail;
367 
368 	right = list;
369 	temp = list;
370 	last = list;
371 	result = NULL;
372 	next = NULL;
373 	tail = NULL;
374 
375 	if (!list || !list->ifa_next)
376 		return (list);
377 
378 	while (temp && temp->ifa_next) {
379 		last = right;
380 		right = right->ifa_next;
381 		temp = temp->ifa_next->ifa_next;
382 	}
383 
384 	last->ifa_next = NULL;
385 
386 	list = sortifaddrs(list, compare, q);
387 	right = sortifaddrs(right, compare, q);
388 
389 	while (list || right) {
390 
391 		if (!right) {
392 			next = list;
393 			list = list->ifa_next;
394 		} else if (!list) {
395 			next = right;
396 			right = right->ifa_next;
397 		} else if (compare(list, right, q) <= 0) {
398 			next = list;
399 			list = list->ifa_next;
400 		} else {
401 			next = right;
402 			right = right->ifa_next;
403 		}
404 
405 		if (!result)
406 			result = next;
407 		else
408 			tail->ifa_next = next;
409 
410 		tail = next;
411 	}
412 
413 	return (result);
414 }
415 #endif
416 
417 static void
418 printifnamemaybe(void)
419 {
420 	if (ifname_to_print[0] != '\0')
421 		printf("%s\n", ifname_to_print);
422 }
423 
424 static void
425 list_interfaces(struct ifconfig_args *args)
426 {
427 #ifdef WITHOUT_NETLINK
428 	list_interfaces_ioctl(args);
429 #else
430 	list_interfaces_nl(args);
431 #endif
432 }
433 
434 static char *
435 args_peek(struct ifconfig_args *args)
436 {
437 	if (args->argc > 0)
438 		return (args->argv[0]);
439 	return (NULL);
440 }
441 
442 static char *
443 args_pop(struct ifconfig_args *args)
444 {
445 	if (args->argc == 0)
446 		return (NULL);
447 
448 	char *arg = args->argv[0];
449 
450 	args->argc--;
451 	args->argv++;
452 
453 	return (arg);
454 }
455 
456 static void
457 args_parse(struct ifconfig_args *args, int argc, char *argv[])
458 {
459 	char options[1024];
460 	struct option *p;
461 	int c;
462 
463 	/* Parse leading line options */
464 	strlcpy(options, "G:adf:j:klmnuv", sizeof(options));
465 	for (p = opts; p != NULL; p = p->next)
466 		strlcat(options, p->opt, sizeof(options));
467 	while ((c = getopt(argc, argv, options)) != -1) {
468 		switch (c) {
469 		case 'a':	/* scan all interfaces */
470 			args->all = true;
471 			break;
472 		case 'd':	/* restrict scan to "down" interfaces */
473 			args->downonly = true;
474 			break;
475 		case 'f':
476 			if (optarg == NULL)
477 				usage();
478 			setformat(optarg);
479 			break;
480 		case 'G':
481 			if (optarg == NULL || args->all == 0)
482 				usage();
483 			args->nogroup = optarg;
484 			break;
485 		case 'j':
486 #ifdef JAIL
487 			if (optarg == NULL)
488 				usage();
489 			args->jail_name = optarg;
490 #else
491 			Perror("not built with jail support");
492 #endif
493 			break;
494 		case 'k':
495 			args->printkeys = true;
496 			break;
497 		case 'l':	/* scan interface names only */
498 			args->namesonly++;
499 			break;
500 		case 'm':	/* show media choices in status */
501 			args->supmedia = true;
502 			break;
503 		case 'n':	/* suppress module loading */
504 			args->noload = true;
505 			break;
506 		case 'u':	/* restrict scan to "up" interfaces */
507 			args->uponly = true;
508 			break;
509 		case 'v':
510 			args->verbose++;
511 			break;
512 		case 'g':
513 			if (args->all) {
514 				if (optarg == NULL)
515 					usage();
516 				args->matchgroup = optarg;
517 				break;
518 			}
519 			/* FALLTHROUGH */
520 		default:
521 			for (p = opts; p != NULL; p = p->next)
522 				if (p->opt[0] == c) {
523 					p->cb(optarg);
524 					break;
525 				}
526 			if (p == NULL)
527 				usage();
528 			break;
529 		}
530 	}
531 	argc -= optind;
532 	argv += optind;
533 
534 	/* -l cannot be used with -a or -m */
535 	if (args->namesonly && (args->all || args->supmedia))
536 		usage();
537 
538 	/* nonsense.. */
539 	if (args->uponly && args->downonly)
540 		usage();
541 
542 	/* no arguments is equivalent to '-a' */
543 	if (!args->namesonly && argc < 1)
544 		args->all = 1;
545 
546 	/* -a and -l allow an address family arg to limit the output */
547 	if (args->all || args->namesonly) {
548 		if (argc > 1)
549 			usage();
550 
551 		if (argc == 1) {
552 			const struct afswtch *afp = af_getbyname(*argv);
553 
554 			if (afp == NULL) {
555 				warnx("Address family '%s' unknown.", *argv);
556 				usage();
557 			}
558 			if (afp->af_name != NULL)
559 				argc--, argv++;
560 			/* leave with afp non-zero */
561 			args->afp = afp;
562 		}
563 	} else {
564 		/* not listing, need an argument */
565 		if (argc < 1)
566 			usage();
567 	}
568 
569 	args->argc = argc;
570 	args->argv = argv;
571 }
572 
573 static int
574 ifconfig_wrapper(struct ifconfig_args *args, int iscreate,
575     const struct afswtch *uafp)
576 {
577 	struct ifconfig_context ctx = {
578 		.args = args,
579 		.io_s = -1,
580 		.ifname = args->ifname,
581 	};
582 
583 #ifdef WITHOUT_NETLINK
584 	return (ifconfig(&ctx, iscreate, uafp));
585 #else
586 	return (ifconfig_wrapper_nl(&ctx, iscreate, uafp));
587 #endif
588 }
589 
590 static bool
591 isargcreate(const char *arg)
592 {
593 	if (arg == NULL)
594 		return (false);
595 
596 	if (strcmp(arg, "create") == 0 || strcmp(arg, "plumb") == 0)
597 		return (true);
598 
599 	return (false);
600 }
601 
602 static bool
603 isnametoolong(const char *ifname)
604 {
605 	return (strlen(ifname) >= IFNAMSIZ);
606 }
607 
608 int
609 main(int ac, char *av[])
610 {
611 	char *envformat;
612 	int flags;
613 #ifdef JAIL
614 	int jid;
615 #endif
616 	struct ifconfig_args _args = {};
617 	struct ifconfig_args *args = &_args;
618 
619 	f_inet = f_inet6 = f_ether = f_addr = NULL;
620 
621 	lifh = ifconfig_open();
622 	if (lifh == NULL)
623 		err(EXIT_FAILURE, "ifconfig_open");
624 
625 	envformat = getenv("IFCONFIG_FORMAT");
626 	if (envformat != NULL)
627 		setformat(envformat);
628 
629 	/*
630 	 * Ensure we print interface name when expected to,
631 	 * even if we terminate early due to error.
632 	 */
633 	atexit(printifnamemaybe);
634 
635 	args_parse(args, ac, av);
636 
637 #ifdef JAIL
638 	if (args->jail_name) {
639 		jid = jail_getid(args->jail_name);
640 		if (jid == -1)
641 			Perror("jail not found");
642 		if (jail_attach(jid) != 0)
643 			Perror("cannot attach to jail");
644 	}
645 #endif
646 
647 	if (!args->all && !args->namesonly) {
648 		/* not listing, need an argument */
649 		args->ifname = args_pop(args);
650 
651 		/* check and maybe load support for this interface */
652 		ifmaybeload(args, args->ifname);
653 
654 		char *arg = args_peek(args);
655 		if (if_nametoindex(args->ifname) == 0) {
656 			/*
657 			 * NOTE:  We must special-case the `create' command
658 			 * right here as we would otherwise fail when trying
659 			 * to find the interface.
660 			 */
661 			if (isargcreate(arg)) {
662 				if (isnametoolong(args->ifname))
663 					errx(1, "%s: cloning name too long",
664 					    args->ifname);
665 				ifconfig_wrapper(args, 1, NULL);
666 				exit(exit_code);
667 			}
668 #ifdef JAIL
669 			/*
670 			 * NOTE:  We have to special-case the `-vnet' command
671 			 * right here as we would otherwise fail when trying
672 			 * to find the interface as it lives in another vnet.
673 			 */
674 			if (arg != NULL && (strcmp(arg, "-vnet") == 0)) {
675 				if (isnametoolong(args->ifname))
676 					errx(1, "%s: interface name too long",
677 					    args->ifname);
678 				ifconfig_wrapper(args, 0, NULL);
679 				exit(exit_code);
680 			}
681 #endif
682 			errx(1, "interface %s does not exist", args->ifname);
683 		} else {
684 			/*
685 			 * Do not allow use `create` command as hostname if
686 			 * address family is not specified.
687 			 */
688 			if (isargcreate(arg)) {
689 				if (args->argc == 1)
690 					errx(1, "interface %s already exists",
691 					    args->ifname);
692 				args_pop(args);
693 			}
694 		}
695 	}
696 
697 	/* Check for address family */
698 	if (args->argc > 0) {
699 		args->afp = af_getbyname(args_peek(args));
700 		if (args->afp != NULL)
701 			args_pop(args);
702 	}
703 
704 	/*
705 	 * Check for a requested configuration action on a single interface,
706 	 * which doesn't require building, sorting, and searching the entire
707 	 * system address list
708 	 */
709 	if ((args->argc > 0) && (args->ifname != NULL)) {
710 		if (isnametoolong(args->ifname))
711 			warnx("%s: interface name too long, skipping", args->ifname);
712 		else {
713 			flags = getifflags(args->ifname, -1, false);
714 			if (!(((flags & IFF_CANTCONFIG) != 0) ||
715 				(args->downonly && (flags & IFF_UP) != 0) ||
716 				(args->uponly && (flags & IFF_UP) == 0)))
717 				ifconfig_wrapper(args, 0, args->afp);
718 		}
719 		goto done;
720 	}
721 
722 	args->allfamilies = args->afp == NULL;
723 
724 	list_interfaces(args);
725 
726 done:
727 	freeformat();
728 	ifconfig_close(lifh);
729 	exit(exit_code);
730 }
731 
732 bool
733 match_ether(const struct sockaddr_dl *sdl)
734 {
735 	switch (sdl->sdl_type) {
736 		case IFT_ETHER:
737 		case IFT_L2VLAN:
738 		case IFT_BRIDGE:
739 			if (sdl->sdl_alen == ETHER_ADDR_LEN)
740 				return (true);
741 		default:
742 			return (false);
743 	}
744 }
745 
746 bool
747 match_if_flags(struct ifconfig_args *args, int if_flags)
748 {
749 	if ((if_flags & IFF_CANTCONFIG) != 0)
750 		return (false);
751 	if (args->downonly && (if_flags & IFF_UP) != 0)
752 		return (false);
753 	if (args->uponly && (if_flags & IFF_UP) == 0)
754 		return (false);
755 	return (true);
756 }
757 
758 #ifdef WITHOUT_NETLINK
759 static bool
760 match_afp(const struct afswtch *afp, int sa_family, const struct sockaddr_dl *sdl)
761 {
762 	if (afp == NULL)
763 		return (true);
764 	/* special case for "ether" address family */
765 	if (!strcmp(afp->af_name, "ether")) {
766 		if (sdl == NULL && !match_ether(sdl))
767 			return (false);
768 		return (true);
769 	}
770 	return (afp->af_af == sa_family);
771 }
772 
773 static void
774 list_interfaces_ioctl(struct ifconfig_args *args)
775 {
776 	struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
777 	struct ifaddrs *ifap, *sifap, *ifa;
778 	struct ifa_order_elt *cur, *tmp;
779 	char *namecp = NULL;
780 	int ifindex;
781 
782 	if (getifaddrs(&ifap) != 0)
783 		err(EXIT_FAILURE, "getifaddrs");
784 
785 	char *cp = NULL;
786 
787 	if (calcorders(ifap, &q) != 0)
788 		err(EXIT_FAILURE, "calcorders");
789 
790 	sifap = sortifaddrs(ifap, cmpifaddrs, &q);
791 
792 	TAILQ_FOREACH_SAFE(cur, &q, link, tmp)
793 		free(cur);
794 
795 	ifindex = 0;
796 	for (ifa = sifap; ifa; ifa = ifa->ifa_next) {
797 		struct ifreq paifr = {};
798 		const struct sockaddr_dl *sdl;
799 
800 		strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
801 		if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
802 			memcpy(&paifr.ifr_addr, ifa->ifa_addr,
803 			    ifa->ifa_addr->sa_len);
804 		}
805 
806 		if (args->ifname != NULL && strcmp(args->ifname, ifa->ifa_name) != 0)
807 			continue;
808 		if (ifa->ifa_addr->sa_family == AF_LINK)
809 			sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
810 		else
811 			sdl = NULL;
812 		if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !args->namesonly)
813 			continue;
814 		if (isnametoolong(ifa->ifa_name)) {
815 			warnx("%s: interface name too long, skipping",
816 			    ifa->ifa_name);
817 			continue;
818 		}
819 		cp = ifa->ifa_name;
820 
821 		if (!match_if_flags(args, ifa->ifa_flags))
822 			continue;
823 		if (!group_member(ifa->ifa_name, args->matchgroup, args->nogroup))
824 			continue;
825 		/*
826 		 * Are we just listing the interfaces?
827 		 */
828 		if (args->namesonly) {
829 			if (namecp == cp)
830 				continue;
831 			if (!match_afp(args->afp, ifa->ifa_addr->sa_family, sdl))
832 				continue;
833 			namecp = cp;
834 			ifindex++;
835 			if (ifindex > 1)
836 				printf(" ");
837 			fputs(cp, stdout);
838 			continue;
839 		}
840 		ifindex++;
841 
842 		if (args->argc > 0)
843 			ifconfig_wrapper(args, 0, args->afp);
844 		else
845 			status(args, sdl, ifa);
846 	}
847 	if (args->namesonly)
848 		printf("\n");
849 	freeifaddrs(ifap);
850 }
851 #endif
852 
853 /*
854  * Returns true if an interface should be listed because any its groups
855  * matches shell pattern "match" and none of groups matches pattern "nomatch".
856  * If any pattern is NULL, corresponding condition is skipped.
857  */
858 bool
859 group_member(const char *ifname, const char *match, const char *nomatch)
860 {
861 	static int		 sock = -1;
862 
863 	struct ifgroupreq	 ifgr;
864 	struct ifg_req		*ifg;
865 	unsigned int		 len;
866 	bool			 matched, nomatched;
867 
868 	/* Sanity checks. */
869 	if (match == NULL && nomatch == NULL)
870 		return (true);
871 	if (ifname == NULL)
872 		return (false);
873 
874 	memset(&ifgr, 0, sizeof(ifgr));
875 	strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ);
876 
877 	/* The socket is opened once. Let _exit() close it. */
878 	if (sock == -1) {
879 		sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
880     		if (sock == -1)
881             	    errx(1, "%s: socket(AF_LOCAL,SOCK_DGRAM)", __func__);
882 	}
883 
884 	/* Determine amount of memory for the list of groups. */
885 	if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
886 		if (errno == EINVAL || errno == ENOTTY)
887 			return (false);
888 		else
889 			errx(1, "%s: SIOCGIFGROUP", __func__);
890 	}
891 
892 	/* Obtain the list of groups. */
893 	len = ifgr.ifgr_len;
894 	ifgr.ifgr_groups =
895 	    (struct ifg_req *)calloc(len / sizeof(*ifg), sizeof(*ifg));
896 
897 	if (ifgr.ifgr_groups == NULL)
898 		errx(1, "%s: no memory", __func__);
899 	if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
900 		errx(1, "%s: SIOCGIFGROUP", __func__);
901 
902 	/* Perform matching. */
903 	matched = false;
904 	nomatched = true;
905 	for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(*ifg); ifg++) {
906 		len -= sizeof(struct ifg_req);
907 		if (match)
908 			matched |= !fnmatch(match, ifg->ifgrq_group, 0);
909 		if (nomatch)
910 			nomatched &= fnmatch(nomatch, ifg->ifgrq_group, 0);
911 	}
912 	free(ifgr.ifgr_groups);
913 
914 	if (match && !nomatch)
915 		return (matched);
916 	if (!match && nomatch)
917 		return (nomatched);
918 	return (matched && nomatched);
919 }
920 
921 static struct afswtch *afs = NULL;
922 
923 void
924 af_register(struct afswtch *p)
925 {
926 	p->af_next = afs;
927 	afs = p;
928 }
929 
930 static struct afswtch *
931 af_getbyname(const char *name)
932 {
933 	struct afswtch *afp;
934 
935 	for (afp = afs; afp !=  NULL; afp = afp->af_next)
936 		if (strcmp(afp->af_name, name) == 0)
937 			return afp;
938 	return NULL;
939 }
940 
941 struct afswtch *
942 af_getbyfamily(int af)
943 {
944 	struct afswtch *afp;
945 
946 	for (afp = afs; afp != NULL; afp = afp->af_next)
947 		if (afp->af_af == af)
948 			return afp;
949 	return NULL;
950 }
951 
952 void
953 af_other_status(if_ctx *ctx)
954 {
955 	struct afswtch *afp;
956 	uint8_t afmask[howmany(AF_MAX, NBBY)];
957 
958 	memset(afmask, 0, sizeof(afmask));
959 	for (afp = afs; afp != NULL; afp = afp->af_next) {
960 		if (afp->af_other_status == NULL)
961 			continue;
962 		if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
963 			continue;
964 		afp->af_other_status(ctx);
965 		setbit(afmask, afp->af_af);
966 	}
967 }
968 
969 static void
970 af_all_tunnel_status(if_ctx *ctx)
971 {
972 	struct afswtch *afp;
973 	uint8_t afmask[howmany(AF_MAX, NBBY)];
974 
975 	memset(afmask, 0, sizeof(afmask));
976 	for (afp = afs; afp != NULL; afp = afp->af_next) {
977 		if (afp->af_status_tunnel == NULL)
978 			continue;
979 		if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
980 			continue;
981 		afp->af_status_tunnel(ctx);
982 		setbit(afmask, afp->af_af);
983 	}
984 }
985 
986 static struct cmd *cmds = NULL;
987 
988 void
989 cmd_register(struct cmd *p)
990 {
991 	p->c_next = cmds;
992 	cmds = p;
993 }
994 
995 static const struct cmd *
996 cmd_lookup(const char *name, int iscreate)
997 {
998 	const struct cmd *p;
999 
1000 	for (p = cmds; p != NULL; p = p->c_next)
1001 		if (strcmp(name, p->c_name) == 0) {
1002 			if (iscreate) {
1003 				if (p->c_iscloneop)
1004 					return p;
1005 			} else {
1006 				if (!p->c_iscloneop)
1007 					return p;
1008 			}
1009 		}
1010 	return NULL;
1011 }
1012 
1013 struct callback {
1014 	callback_func *cb_func;
1015 	void	*cb_arg;
1016 	struct callback *cb_next;
1017 };
1018 static struct callback *callbacks = NULL;
1019 
1020 void
1021 callback_register(callback_func *func, void *arg)
1022 {
1023 	struct callback *cb;
1024 
1025 	cb = malloc(sizeof(struct callback));
1026 	if (cb == NULL)
1027 		errx(1, "unable to allocate memory for callback");
1028 	cb->cb_func = func;
1029 	cb->cb_arg = arg;
1030 	cb->cb_next = callbacks;
1031 	callbacks = cb;
1032 }
1033 
1034 /* specially-handled commands */
1035 static void setifaddr(if_ctx *ctx, const char *addr, int param);
1036 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
1037 
1038 static void setifdstaddr(if_ctx *ctx, const char *addr, int param __unused);
1039 static const struct cmd setifdstaddr_cmd =
1040 	DEF_CMD("ifdstaddr", 0, setifdstaddr);
1041 
1042 int
1043 af_exec_ioctl(if_ctx *ctx, unsigned long action, void *data)
1044 {
1045 	struct ifreq *req = (struct ifreq *)data;
1046 
1047 	strlcpy(req->ifr_name, ctx->ifname, sizeof(req->ifr_name));
1048 	if (ioctl_ctx(ctx, action, req) == 0)
1049 		return (0);
1050 	return (errno);
1051 }
1052 
1053 static void
1054 delifaddr(if_ctx *ctx, const struct afswtch *afp)
1055 {
1056 	int error;
1057 
1058 	if (afp->af_exec == NULL) {
1059 		warnx("interface %s cannot change %s addresses!",
1060 		    ctx->ifname, afp->af_name);
1061 		clearaddr = 0;
1062 		return;
1063 	}
1064 
1065 	error = afp->af_exec(ctx, afp->af_difaddr, afp->af_ridreq);
1066 	if (error != 0) {
1067 		if (error == EADDRNOTAVAIL && (doalias >= 0)) {
1068 			/* means no previous address for interface */
1069 		} else
1070 			Perrorc("ioctl (SIOCDIFADDR)", error);
1071 	}
1072 }
1073 
1074 static void
1075 addifaddr(if_ctx *ctx, const struct afswtch *afp)
1076 {
1077 	if (afp->af_exec == NULL) {
1078 		warnx("interface %s cannot change %s addresses!",
1079 		      ctx->ifname, afp->af_name);
1080 		newaddr = 0;
1081 		return;
1082 	}
1083 
1084 	if (setaddr || setmask) {
1085 		int error = afp->af_exec(ctx, afp->af_aifaddr, afp->af_addreq);
1086 		if (error != 0)
1087 			Perrorc("ioctl (SIOCAIFADDR)", error);
1088 	}
1089 }
1090 
1091 int
1092 ifconfig(if_ctx *orig_ctx, int iscreate, const struct afswtch *uafp)
1093 {
1094 	const struct afswtch *afp, *nafp;
1095 	const struct cmd *p;
1096 	struct callback *cb;
1097 	int s;
1098 	int argc = orig_ctx->args->argc;
1099 	char *const *argv = orig_ctx->args->argv;
1100 	struct ifconfig_context _ctx = {
1101 		.args = orig_ctx->args,
1102 		.io_ss = orig_ctx->io_ss,
1103 		.ifname = orig_ctx->ifname,
1104 	};
1105 	struct ifconfig_context *ctx = &_ctx;
1106 
1107 	strlcpy(ifr.ifr_name, ctx->ifname, sizeof ifr.ifr_name);
1108 	afp = NULL;
1109 	if (uafp != NULL)
1110 		afp = uafp;
1111 	/*
1112 	 * This is the historical "accident" allowing users to configure IPv4
1113 	 * addresses without the "inet" keyword which while a nice feature has
1114 	 * proven to complicate other things.  We cannot remove this but only
1115 	 * make sure we will never have a similar implicit default for IPv6 or
1116 	 * any other address familiy.  We need a fallback though for
1117 	 * ifconfig IF up/down etc. to work without INET support as people
1118 	 * never used ifconfig IF link up/down, etc. either.
1119 	 */
1120 #ifndef RESCUE
1121 #ifdef INET
1122 	if (afp == NULL && feature_present("inet"))
1123 		afp = af_getbyname("inet");
1124 #endif
1125 #endif
1126 	if (afp == NULL)
1127 		afp = af_getbyname("link");
1128 	if (afp == NULL) {
1129 		warnx("Please specify an address_family.");
1130 		usage();
1131 	}
1132 
1133 top:
1134 	ifr.ifr_addr.sa_family =
1135 		afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
1136 		AF_LOCAL : afp->af_af;
1137 
1138 	if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
1139 	    (uafp != NULL || errno != EAFNOSUPPORT ||
1140 	     (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
1141 		err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1142 
1143 	ctx->io_s = s;
1144 	ctx->afp = afp;
1145 
1146 	while (argc > 0) {
1147 		p = cmd_lookup(*argv, iscreate);
1148 		if (iscreate && p == NULL) {
1149 			/*
1150 			 * Push the clone create callback so the new
1151 			 * device is created and can be used for any
1152 			 * remaining arguments.
1153 			 */
1154 			cb = callbacks;
1155 			if (cb == NULL)
1156 				errx(1, "internal error, no callback");
1157 			callbacks = cb->cb_next;
1158 			cb->cb_func(ctx, cb->cb_arg);
1159 			iscreate = 0;
1160 			/*
1161 			 * Handle any address family spec that
1162 			 * immediately follows and potentially
1163 			 * recreate the socket.
1164 			 */
1165 			nafp = af_getbyname(*argv);
1166 			if (nafp != NULL) {
1167 				argc--, argv++;
1168 				if (nafp != afp) {
1169 					close(s);
1170 					afp = nafp;
1171 					goto top;
1172 				}
1173 			}
1174 			/*
1175 			 * Look for a normal parameter.
1176 			 */
1177 			continue;
1178 		}
1179 		if (p == NULL) {
1180 			/*
1181 			 * Not a recognized command, choose between setting
1182 			 * the interface address and the dst address.
1183 			 */
1184 			p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
1185 		}
1186 		if (p->c_parameter == NEXTARG && p->c_u.c_func) {
1187 			if (argv[1] == NULL)
1188 				errx(1, "'%s' requires argument",
1189 				    p->c_name);
1190 			p->c_u.c_func(ctx, argv[1], 0);
1191 			argc--, argv++;
1192 		} else if (p->c_parameter == OPTARG && p->c_u.c_func) {
1193 			p->c_u.c_func(ctx, argv[1], 0);
1194 			if (argv[1] != NULL)
1195 				argc--, argv++;
1196 		} else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
1197 			if (argc < 3)
1198 				errx(1, "'%s' requires 2 arguments",
1199 				    p->c_name);
1200 			p->c_u.c_func2(ctx, argv[1], argv[2]);
1201 			argc -= 2, argv += 2;
1202 		} else if (p->c_parameter == SPARAM && p->c_u.c_func3) {
1203 			p->c_u.c_func3(ctx, *argv, p->c_sparameter);
1204 		} else if (p->c_u.c_func)
1205 			p->c_u.c_func(ctx, *argv, p->c_parameter);
1206 		argc--, argv++;
1207 	}
1208 
1209 	/*
1210 	 * Do any post argument processing required by the address family.
1211 	 */
1212 	if (afp->af_postproc != NULL)
1213 		afp->af_postproc(ctx, newaddr, getifflags(ctx->ifname, s, true));
1214 	/*
1215 	 * Do deferred callbacks registered while processing
1216 	 * command-line arguments.
1217 	 */
1218 	for (cb = callbacks; cb != NULL; cb = cb->cb_next)
1219 		cb->cb_func(ctx, cb->cb_arg);
1220 	/*
1221 	 * Do deferred operations.
1222 	 */
1223 	if (clearaddr)
1224 		delifaddr(ctx, afp);
1225 	if (newaddr)
1226 		addifaddr(ctx, afp);
1227 
1228 	close(s);
1229 	return(0);
1230 }
1231 
1232 static void
1233 setifaddr(if_ctx *ctx, const char *addr, int param)
1234 {
1235 	const struct afswtch *afp = ctx->afp;
1236 
1237 	if (afp->af_getaddr == NULL)
1238 		return;
1239 	/*
1240 	 * Delay the ioctl to set the interface addr until flags are all set.
1241 	 * The address interpretation may depend on the flags,
1242 	 * and the flags may change when the address is set.
1243 	 */
1244 	setaddr++;
1245 	if (doalias == 0 && afp->af_af != AF_LINK)
1246 		clearaddr = 1;
1247 	afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
1248 }
1249 
1250 static void
1251 settunnel(if_ctx *ctx, const char *src, const char *dst)
1252 {
1253 	const struct afswtch *afp = ctx->afp;
1254 	struct addrinfo *srcres, *dstres;
1255 	int ecode;
1256 
1257 	if (afp->af_settunnel == NULL) {
1258 		warn("address family %s does not support tunnel setup",
1259 			afp->af_name);
1260 		return;
1261 	}
1262 
1263 	if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
1264 		errx(1, "error in parsing address string: %s",
1265 		    gai_strerror(ecode));
1266 
1267 	if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
1268 		errx(1, "error in parsing address string: %s",
1269 		    gai_strerror(ecode));
1270 
1271 	if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
1272 		errx(1,
1273 		    "source and destination address families do not match");
1274 
1275 	afp->af_settunnel(ctx, srcres, dstres);
1276 
1277 	freeaddrinfo(srcres);
1278 	freeaddrinfo(dstres);
1279 }
1280 
1281 static void
1282 deletetunnel(if_ctx *ctx, const char *vname, int param)
1283 {
1284 
1285 	if (ioctl(ctx->io_s, SIOCDIFPHYADDR, &ifr) < 0)
1286 		err(1, "SIOCDIFPHYADDR");
1287 }
1288 
1289 #ifdef JAIL
1290 static void
1291 setifvnet(if_ctx *ctx, const char *jname, int dummy __unused)
1292 {
1293 	struct ifreq my_ifr;
1294 
1295 	memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1296 	my_ifr.ifr_jid = jail_getid(jname);
1297 	if (my_ifr.ifr_jid < 0)
1298 		errx(1, "%s", jail_errmsg);
1299 	if (ioctl(ctx->io_s, SIOCSIFVNET, &my_ifr) < 0)
1300 		err(1, "SIOCSIFVNET");
1301 }
1302 
1303 static void
1304 setifrvnet(if_ctx *ctx, const char *jname, int dummy __unused)
1305 {
1306 	struct ifreq my_ifr;
1307 
1308 	memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1309 	my_ifr.ifr_jid = jail_getid(jname);
1310 	if (my_ifr.ifr_jid < 0)
1311 		errx(1, "%s", jail_errmsg);
1312 	if (ioctl(ctx->io_s, SIOCSIFRVNET, &my_ifr) < 0)
1313 		err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
1314 }
1315 #endif
1316 
1317 static void
1318 setifnetmask(if_ctx *ctx, const char *addr, int dummy __unused)
1319 {
1320 	const struct afswtch *afp = ctx->afp;
1321 
1322 	if (afp->af_getaddr != NULL) {
1323 		setmask++;
1324 		afp->af_getaddr(addr, MASK);
1325 	}
1326 }
1327 
1328 static void
1329 setifbroadaddr(if_ctx *ctx, const char *addr, int dummy __unused)
1330 {
1331 	const struct afswtch *afp = ctx->afp;
1332 
1333 	if (afp->af_getaddr != NULL)
1334 		afp->af_getaddr(addr, BRDADDR);
1335 }
1336 
1337 static void
1338 notealias(if_ctx *ctx, const char *addr, int param)
1339 {
1340 	const struct afswtch *afp = ctx->afp;
1341 
1342 	if (setaddr && doalias == 0 && param < 0) {
1343 		if (afp->af_copyaddr != NULL)
1344 			afp->af_copyaddr(ctx, RIDADDR, ADDR);
1345 	}
1346 	doalias = param;
1347 	if (param < 0) {
1348 		clearaddr = 1;
1349 		newaddr = 0;
1350 	} else
1351 		clearaddr = 0;
1352 }
1353 
1354 static void
1355 setifdstaddr(if_ctx *ctx, const char *addr, int param __unused)
1356 {
1357 	const struct afswtch *afp = ctx->afp;
1358 
1359 	if (afp->af_getaddr != NULL)
1360 		afp->af_getaddr(addr, DSTADDR);
1361 }
1362 
1363 static int
1364 getifflags(const char *ifname, int us, bool err_ok)
1365 {
1366 	struct ifreq my_ifr;
1367 	int s;
1368 
1369 	memset(&my_ifr, 0, sizeof(my_ifr));
1370 	(void) strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
1371 	if (us < 0) {
1372 		if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
1373 			err(1, "socket(family AF_LOCAL,SOCK_DGRAM");
1374 	} else
1375 		s = us;
1376  	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1377 		if (!err_ok) {
1378 			Perror("ioctl (SIOCGIFFLAGS)");
1379 			exit(1);
1380 		}
1381  	}
1382 	if (us < 0)
1383 		close(s);
1384 	return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
1385 }
1386 
1387 /*
1388  * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1389  * of the ifreq structure, which may confuse other parts of ifconfig.
1390  * Make a private copy so we can avoid that.
1391  */
1392 static void
1393 setifflags(if_ctx *ctx, const char *vname, int value)
1394 {
1395 	struct ifreq		my_ifr;
1396 	int flags;
1397 
1398 	flags = getifflags(ctx->ifname, ctx->io_s, false);
1399 	if (value < 0) {
1400 		value = -value;
1401 		flags &= ~value;
1402 	} else
1403 		flags |= value;
1404 	memset(&my_ifr, 0, sizeof(my_ifr));
1405 	strlcpy(my_ifr.ifr_name, ctx->ifname, sizeof(my_ifr.ifr_name));
1406 	my_ifr.ifr_flags = flags & 0xffff;
1407 	my_ifr.ifr_flagshigh = flags >> 16;
1408 	if (ioctl(ctx->io_s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1409 		Perror(vname);
1410 }
1411 
1412 void
1413 setifcap(if_ctx *ctx, const char *vname, int value)
1414 {
1415 	int flags;
1416 
1417 	if (ioctl(ctx->io_s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1418  		Perror("ioctl (SIOCGIFCAP)");
1419  		exit(1);
1420  	}
1421 	flags = ifr.ifr_curcap;
1422 	if (value < 0) {
1423 		value = -value;
1424 		flags &= ~value;
1425 	} else
1426 		flags |= value;
1427 	flags &= ifr.ifr_reqcap;
1428 	/* Check for no change in capabilities. */
1429 	if (ifr.ifr_curcap == flags)
1430 		return;
1431 	ifr.ifr_reqcap = flags;
1432 	if (ioctl(ctx->io_s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1433 		Perror(vname);
1434 }
1435 
1436 void
1437 setifcapnv(if_ctx *ctx, const char *vname, const char *arg)
1438 {
1439 	nvlist_t *nvcap;
1440 	void *buf;
1441 	char *marg, *mopt;
1442 	size_t nvbuflen;
1443 	bool neg;
1444 
1445 	if (ioctl(ctx->io_s, SIOCGIFCAP, (caddr_t)&ifr) < 0)
1446 		Perror("ioctl (SIOCGIFCAP)");
1447 	if ((ifr.ifr_curcap & IFCAP_NV) == 0) {
1448 		warnx("IFCAP_NV not supported");
1449 		return; /* Not exit() */
1450 	}
1451 
1452 	marg = strdup(arg);
1453 	if (marg == NULL)
1454 		Perror("strdup");
1455 	nvcap = nvlist_create(0);
1456 	if (nvcap == NULL)
1457 		Perror("nvlist_create");
1458 	while ((mopt = strsep(&marg, ",")) != NULL) {
1459 		neg = *mopt == '-';
1460 		if (neg)
1461 			mopt++;
1462 		if (strcmp(mopt, "rxtls") == 0) {
1463 			nvlist_add_bool(nvcap, "rxtls4", !neg);
1464 			nvlist_add_bool(nvcap, "rxtls6", !neg);
1465 		} else {
1466 			nvlist_add_bool(nvcap, mopt, !neg);
1467 		}
1468 	}
1469 	buf = nvlist_pack(nvcap, &nvbuflen);
1470 	if (buf == NULL) {
1471 		errx(1, "nvlist_pack error");
1472 		exit(1);
1473 	}
1474 	ifr.ifr_cap_nv.buf_length = ifr.ifr_cap_nv.length = nvbuflen;
1475 	ifr.ifr_cap_nv.buffer = buf;
1476 	if (ioctl(ctx->io_s, SIOCSIFCAPNV, (caddr_t)&ifr) < 0)
1477 		Perror(vname);
1478 	free(buf);
1479 	nvlist_destroy(nvcap);
1480 	free(marg);
1481 }
1482 
1483 static void
1484 setifmetric(if_ctx *ctx, const char *val, int dummy __unused)
1485 {
1486 	strlcpy(ifr.ifr_name, ctx->ifname, sizeof (ifr.ifr_name));
1487 	ifr.ifr_metric = atoi(val);
1488 	if (ioctl(ctx->io_s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1489 		err(1, "ioctl SIOCSIFMETRIC (set metric)");
1490 }
1491 
1492 static void
1493 setifmtu(if_ctx *ctx, const char *val, int dummy __unused)
1494 {
1495 	strlcpy(ifr.ifr_name, ctx->ifname, sizeof (ifr.ifr_name));
1496 	ifr.ifr_mtu = atoi(val);
1497 	if (ioctl(ctx->io_s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1498 		err(1, "ioctl SIOCSIFMTU (set mtu)");
1499 }
1500 
1501 static void
1502 setifpcp(if_ctx *ctx, const char *val, int arg __unused)
1503 {
1504 	u_long ul;
1505 	char *endp;
1506 
1507 	ul = strtoul(val, &endp, 0);
1508 	if (*endp != '\0')
1509 		errx(1, "invalid value for pcp");
1510 	if (ul > 7)
1511 		errx(1, "value for pcp out of range");
1512 	ifr.ifr_lan_pcp = ul;
1513 	if (ioctl(ctx->io_s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1514 		err(1, "SIOCSLANPCP");
1515 }
1516 
1517 static void
1518 disableifpcp(if_ctx *ctx, const char *val, int arg __unused)
1519 {
1520 
1521 	ifr.ifr_lan_pcp = IFNET_PCP_NONE;
1522 	if (ioctl(ctx->io_s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1523 		err(1, "SIOCSLANPCP");
1524 }
1525 
1526 static void
1527 setifname(if_ctx *ctx, const char *val, int dummy __unused)
1528 {
1529 	char *newname;
1530 
1531 	strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));
1532 
1533 	newname = strdup(val);
1534 	if (newname == NULL)
1535 		err(1, "no memory to set ifname");
1536 	ifr.ifr_data = newname;
1537 	if (ioctl(ctx->io_s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1538 		free(newname);
1539 		err(1, "ioctl SIOCSIFNAME (set name)");
1540 	}
1541 	ifname_update(ctx, newname);
1542 	free(newname);
1543 }
1544 
1545 static void
1546 setifdescr(if_ctx *ctx, const char *val, int dummy __unused)
1547 {
1548 	char *newdescr;
1549 
1550 	strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));
1551 
1552 	ifr.ifr_buffer.length = strlen(val) + 1;
1553 	if (ifr.ifr_buffer.length == 1) {
1554 		ifr.ifr_buffer.buffer = newdescr = NULL;
1555 		ifr.ifr_buffer.length = 0;
1556 	} else {
1557 		newdescr = strdup(val);
1558 		ifr.ifr_buffer.buffer = newdescr;
1559 		if (newdescr == NULL) {
1560 			warn("no memory to set ifdescr");
1561 			return;
1562 		}
1563 	}
1564 
1565 	if (ioctl(ctx->io_s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1566 		err(1, "ioctl SIOCSIFDESCR (set descr)");
1567 
1568 	free(newdescr);
1569 }
1570 
1571 static void
1572 unsetifdescr(if_ctx *ctx, const char *val, int value)
1573 {
1574 	setifdescr(ctx, "", 0);
1575 }
1576 
1577 #define	IFFBITS \
1578 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
1579 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1580 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25STICKYARP"
1581 
1582 #define	IFCAPBITS \
1583 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1584 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1585 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1586 "\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP\33NOMAP\34TXTLS4\35TXTLS6" \
1587 "\36VXLAN_HWCSUM\37VXLAN_HWTSO\40TXTLS_RTLMT"
1588 
1589 static void
1590 print_ifcap_nv(struct ifconfig_args *args, int s)
1591 {
1592 	nvlist_t *nvcap;
1593 	const char *nvname;
1594 	void *buf, *cookie;
1595 	bool first, val;
1596 	int type;
1597 
1598 	buf = malloc(IFR_CAP_NV_MAXBUFSIZE);
1599 	if (buf == NULL)
1600 		Perror("malloc");
1601 	ifr.ifr_cap_nv.buffer = buf;
1602 	ifr.ifr_cap_nv.buf_length = IFR_CAP_NV_MAXBUFSIZE;
1603 	if (ioctl(s, SIOCGIFCAPNV, (caddr_t)&ifr) != 0)
1604 		Perror("ioctl (SIOCGIFCAPNV)");
1605 	nvcap = nvlist_unpack(ifr.ifr_cap_nv.buffer,
1606 	    ifr.ifr_cap_nv.length, 0);
1607 	if (nvcap == NULL)
1608 		Perror("nvlist_unpack");
1609 	printf("\toptions");
1610 	cookie = NULL;
1611 	for (first = true;; first = false) {
1612 		nvname = nvlist_next(nvcap, &type, &cookie);
1613 		if (nvname == NULL) {
1614 			printf("\n");
1615 			break;
1616 		}
1617 		if (type == NV_TYPE_BOOL) {
1618 			val = nvlist_get_bool(nvcap, nvname);
1619 			if (val) {
1620 				printf("%c%s",
1621 				    first ? ' ' : ',', nvname);
1622 			}
1623 		}
1624 	}
1625 	if (args->supmedia) {
1626 		printf("\tcapabilities");
1627 		cookie = NULL;
1628 		for (first = true;; first = false) {
1629 			nvname = nvlist_next(nvcap, &type,
1630 			    &cookie);
1631 			if (nvname == NULL) {
1632 				printf("\n");
1633 				break;
1634 			}
1635 			if (type == NV_TYPE_BOOL)
1636 				printf("%c%s", first ? ' ' :
1637 				    ',', nvname);
1638 		}
1639 	}
1640 	nvlist_destroy(nvcap);
1641 	free(buf);
1642 
1643 	if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) != 0)
1644 		Perror("ioctl (SIOCGIFCAP)");
1645 }
1646 
1647 void
1648 print_ifcap(struct ifconfig_args *args, int s)
1649 {
1650 	if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) != 0)
1651 		return;
1652 
1653 	if ((ifr.ifr_curcap & IFCAP_NV) != 0)
1654 		print_ifcap_nv(args, s);
1655 	else {
1656 		printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1657 		putchar('\n');
1658 		if (args->supmedia && ifr.ifr_reqcap != 0) {
1659 			printb("\tcapabilities", ifr.ifr_reqcap,
1660 			    IFCAPBITS);
1661 			putchar('\n');
1662 		}
1663 	}
1664 }
1665 
1666 void
1667 print_ifstatus(if_ctx *ctx)
1668 {
1669 	struct ifstat ifs;
1670 
1671 	strlcpy(ifs.ifs_name, ctx->ifname, sizeof ifs.ifs_name);
1672 	if (ioctl_ctx(ctx, SIOCGIFSTATUS, &ifs) == 0)
1673 		printf("%s", ifs.ascii);
1674 }
1675 
1676 void
1677 print_metric(int s)
1678 {
1679 	if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1680 		printf(" metric %d", ifr.ifr_metric);
1681 }
1682 
1683 #ifdef WITHOUT_NETLINK
1684 static void
1685 print_mtu(int s)
1686 {
1687 	if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1688 		printf(" mtu %d", ifr.ifr_mtu);
1689 }
1690 
1691 static void
1692 print_description(int s)
1693 {
1694 	for (;;) {
1695 		if ((descr = reallocf(descr, descrlen)) != NULL) {
1696 			ifr.ifr_buffer.buffer = descr;
1697 			ifr.ifr_buffer.length = descrlen;
1698 			if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1699 				if (ifr.ifr_buffer.buffer == descr) {
1700 					if (strlen(descr) > 0)
1701 						printf("\tdescription: %s\n",
1702 						    descr);
1703 				} else if (ifr.ifr_buffer.length > descrlen) {
1704 					descrlen = ifr.ifr_buffer.length;
1705 					continue;
1706 				}
1707 			}
1708 		} else
1709 			warn("unable to allocate memory for interface"
1710 			    "description");
1711 		break;
1712 	}
1713 }
1714 
1715 /*
1716  * Print the status of the interface.  If an address family was
1717  * specified, show only it; otherwise, show them all.
1718  */
1719 static void
1720 status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
1721 	struct ifaddrs *ifa)
1722 {
1723 	struct ifaddrs *ift;
1724 	int s;
1725 	bool allfamilies = args->afp == NULL;
1726 	char *ifname = ifa->ifa_name;
1727 
1728 	if (args->afp == NULL)
1729 		ifr.ifr_addr.sa_family = AF_LOCAL;
1730 	else
1731 		ifr.ifr_addr.sa_family =
1732 		   args->afp->af_af == AF_LINK ? AF_LOCAL : args->afp->af_af;
1733 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1734 
1735 	s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1736 	if (s < 0)
1737 		err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1738 
1739 	struct ifconfig_context _ctx = {
1740 		.io_s = s,
1741 		.ifname = ifname,
1742 	};
1743 	struct ifconfig_context *ctx = &_ctx;
1744 
1745 	printf("%s: ", ifname);
1746 	printb("flags", ifa->ifa_flags, IFFBITS);
1747 	print_metric(s);
1748 	print_mtu(s);
1749 	putchar('\n');
1750 
1751 	print_description(s);
1752 
1753 	print_ifcap(args, s);
1754 
1755 	tunnel_status(ctx);
1756 
1757 	for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1758 		if (ift->ifa_addr == NULL)
1759 			continue;
1760 		if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1761 			continue;
1762 		if (allfamilies) {
1763 			const struct afswtch *p;
1764 			p = af_getbyfamily(ift->ifa_addr->sa_family);
1765 			if (p != NULL && p->af_status != NULL)
1766 				p->af_status(ctx, ift);
1767 		} else if (args->afp->af_af == ift->ifa_addr->sa_family)
1768 			args->afp->af_status(ctx, ift);
1769 	}
1770 #if 0
1771 	if (allfamilies || afp->af_af == AF_LINK) {
1772 		const struct afswtch *lafp;
1773 
1774 		/*
1775 		 * Hack; the link level address is received separately
1776 		 * from the routing information so any address is not
1777 		 * handled above.  Cobble together an entry and invoke
1778 		 * the status method specially.
1779 		 */
1780 		lafp = af_getbyname("lladdr");
1781 		if (lafp != NULL) {
1782 			info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1783 			lafp->af_status(s, &info);
1784 		}
1785 	}
1786 #endif
1787 	if (allfamilies)
1788 		af_other_status(ctx);
1789 	else if (args->afp->af_other_status != NULL)
1790 		args->afp->af_other_status(ctx);
1791 
1792 	print_ifstatus(ctx);
1793 	if (args->verbose > 0)
1794 		sfp_status(ctx);
1795 
1796 	close(s);
1797 	return;
1798 }
1799 #endif
1800 
1801 void
1802 tunnel_status(if_ctx *ctx)
1803 {
1804 	af_all_tunnel_status(ctx);
1805 }
1806 
1807 static void
1808 Perrorc(const char *cmd, int error)
1809 {
1810 	switch (errno) {
1811 
1812 	case ENXIO:
1813 		errx(1, "%s: no such interface", cmd);
1814 		break;
1815 
1816 	case EPERM:
1817 		errx(1, "%s: permission denied", cmd);
1818 		break;
1819 
1820 	default:
1821 		errc(1, error, "%s", cmd);
1822 	}
1823 }
1824 
1825 void
1826 Perror(const char *cmd)
1827 {
1828 	Perrorc(cmd, errno);
1829 }
1830 
1831 /*
1832  * Print a value a la the %b format of the kernel's printf
1833  */
1834 void
1835 printb(const char *s, unsigned v, const char *bits)
1836 {
1837 	int i, any = 0;
1838 	char c;
1839 
1840 	if (bits && *bits == 8)
1841 		printf("%s=%o", s, v);
1842 	else
1843 		printf("%s=%x", s, v);
1844 	if (bits) {
1845 		bits++;
1846 		putchar('<');
1847 		while ((i = *bits++) != '\0') {
1848 			if (v & (1u << (i-1))) {
1849 				if (any)
1850 					putchar(',');
1851 				any = 1;
1852 				for (; (c = *bits) > 32; bits++)
1853 					putchar(c);
1854 			} else
1855 				for (; *bits > 32; bits++)
1856 					;
1857 		}
1858 		putchar('>');
1859 	}
1860 }
1861 
1862 void
1863 print_vhid(const struct ifaddrs *ifa, const char *s)
1864 {
1865 	struct if_data *ifd;
1866 
1867 	if (ifa->ifa_data == NULL)
1868 		return;
1869 
1870 	ifd = ifa->ifa_data;
1871 	if (ifd->ifi_vhid == 0)
1872 		return;
1873 
1874 	printf(" vhid %d", ifd->ifi_vhid);
1875 }
1876 
1877 void
1878 ifmaybeload(struct ifconfig_args *args, const char *name)
1879 {
1880 #define MOD_PREFIX_LEN		3	/* "if_" */
1881 	struct module_stat mstat;
1882 	int fileid, modid;
1883 	char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1884 	const char *cp;
1885 	struct module_map_entry *mme;
1886 	bool found;
1887 
1888 	/* loading suppressed by the user */
1889 	if (args->noload)
1890 		return;
1891 
1892 	/* trim the interface number off the end */
1893 	strlcpy(ifname, name, sizeof(ifname));
1894 	dp = ifname + strlen(ifname) - 1;
1895 	for (; dp > ifname; dp--) {
1896 		if (isdigit(*dp))
1897 			*dp = '\0';
1898 		else
1899 			break;
1900 	}
1901 
1902 	/* Either derive it from the map or guess otherwise */
1903 	*ifkind = '\0';
1904 	found = false;
1905 	for (unsigned i = 0; i < nitems(module_map); ++i) {
1906 		mme = &module_map[i];
1907 		if (strcmp(mme->ifname, ifname) == 0) {
1908 			strlcpy(ifkind, mme->kldname, sizeof(ifkind));
1909 			found = true;
1910 			break;
1911 		}
1912 	}
1913 
1914 	/* We didn't have an alias for it... we'll guess. */
1915 	if (!found) {
1916 	    /* turn interface and unit into module name */
1917 	    strlcpy(ifkind, "if_", sizeof(ifkind));
1918 	    strlcat(ifkind, ifname, sizeof(ifkind));
1919 	}
1920 
1921 	/* scan files in kernel */
1922 	mstat.version = sizeof(struct module_stat);
1923 	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1924 		/* scan modules in file */
1925 		for (modid = kldfirstmod(fileid); modid > 0;
1926 		     modid = modfnext(modid)) {
1927 			if (modstat(modid, &mstat) < 0)
1928 				continue;
1929 			/* strip bus name if present */
1930 			if ((cp = strchr(mstat.name, '/')) != NULL) {
1931 				cp++;
1932 			} else {
1933 				cp = mstat.name;
1934 			}
1935 			/*
1936 			 * Is it already loaded?  Don't compare with ifname if
1937 			 * we were specifically told which kld to use.  Doing
1938 			 * so could lead to conflicts not trivially solved.
1939 			 */
1940 			if ((!found && strcmp(ifname, cp) == 0) ||
1941 			    strcmp(ifkind, cp) == 0)
1942 				return;
1943 		}
1944 	}
1945 
1946 	/*
1947 	 * Try to load the module.  But ignore failures, because ifconfig can't
1948 	 * infer the names of all drivers (eg mlx4en(4)).
1949 	 */
1950 	(void) kldload(ifkind);
1951 }
1952 
1953 static struct cmd basic_cmds[] = {
1954 	DEF_CMD("up",		IFF_UP,		setifflags),
1955 	DEF_CMD("down",		-IFF_UP,	setifflags),
1956 	DEF_CMD("arp",		-IFF_NOARP,	setifflags),
1957 	DEF_CMD("-arp",		IFF_NOARP,	setifflags),
1958 	DEF_CMD("debug",	IFF_DEBUG,	setifflags),
1959 	DEF_CMD("-debug",	-IFF_DEBUG,	setifflags),
1960 	DEF_CMD_ARG("description",		setifdescr),
1961 	DEF_CMD_ARG("descr",			setifdescr),
1962 	DEF_CMD("-description",	0,		unsetifdescr),
1963 	DEF_CMD("-descr",	0,		unsetifdescr),
1964 	DEF_CMD("promisc",	IFF_PPROMISC,	setifflags),
1965 	DEF_CMD("-promisc",	-IFF_PPROMISC,	setifflags),
1966 	DEF_CMD("add",		IFF_UP,		notealias),
1967 	DEF_CMD("alias",	IFF_UP,		notealias),
1968 	DEF_CMD("-alias",	-IFF_UP,	notealias),
1969 	DEF_CMD("delete",	-IFF_UP,	notealias),
1970 	DEF_CMD("remove",	-IFF_UP,	notealias),
1971 #ifdef notdef
1972 #define	EN_SWABIPS	0x1000
1973 	DEF_CMD("swabips",	EN_SWABIPS,	setifflags),
1974 	DEF_CMD("-swabips",	-EN_SWABIPS,	setifflags),
1975 #endif
1976 	DEF_CMD_ARG("netmask",			setifnetmask),
1977 	DEF_CMD_ARG("metric",			setifmetric),
1978 	DEF_CMD_ARG("broadcast",		setifbroadaddr),
1979 	DEF_CMD_ARG2("tunnel",			settunnel),
1980 	DEF_CMD("-tunnel", 0,			deletetunnel),
1981 	DEF_CMD("deletetunnel", 0,		deletetunnel),
1982 #ifdef JAIL
1983 	DEF_CMD_ARG("vnet",			setifvnet),
1984 	DEF_CMD_ARG("-vnet",			setifrvnet),
1985 #endif
1986 	DEF_CMD("link0",	IFF_LINK0,	setifflags),
1987 	DEF_CMD("-link0",	-IFF_LINK0,	setifflags),
1988 	DEF_CMD("link1",	IFF_LINK1,	setifflags),
1989 	DEF_CMD("-link1",	-IFF_LINK1,	setifflags),
1990 	DEF_CMD("link2",	IFF_LINK2,	setifflags),
1991 	DEF_CMD("-link2",	-IFF_LINK2,	setifflags),
1992 	DEF_CMD("monitor",	IFF_MONITOR,	setifflags),
1993 	DEF_CMD("-monitor",	-IFF_MONITOR,	setifflags),
1994 	DEF_CMD("mextpg",	IFCAP_MEXTPG,	setifcap),
1995 	DEF_CMD("-mextpg",	-IFCAP_MEXTPG,	setifcap),
1996 	DEF_CMD("staticarp",	IFF_STATICARP,	setifflags),
1997 	DEF_CMD("-staticarp",	-IFF_STATICARP,	setifflags),
1998 	DEF_CMD("stickyarp",	IFF_STICKYARP,	setifflags),
1999 	DEF_CMD("-stickyarp",	-IFF_STICKYARP,	setifflags),
2000 	DEF_CMD("rxcsum6",	IFCAP_RXCSUM_IPV6,	setifcap),
2001 	DEF_CMD("-rxcsum6",	-IFCAP_RXCSUM_IPV6,	setifcap),
2002 	DEF_CMD("txcsum6",	IFCAP_TXCSUM_IPV6,	setifcap),
2003 	DEF_CMD("-txcsum6",	-IFCAP_TXCSUM_IPV6,	setifcap),
2004 	DEF_CMD("rxcsum",	IFCAP_RXCSUM,	setifcap),
2005 	DEF_CMD("-rxcsum",	-IFCAP_RXCSUM,	setifcap),
2006 	DEF_CMD("txcsum",	IFCAP_TXCSUM,	setifcap),
2007 	DEF_CMD("-txcsum",	-IFCAP_TXCSUM,	setifcap),
2008 	DEF_CMD("netcons",	IFCAP_NETCONS,	setifcap),
2009 	DEF_CMD("-netcons",	-IFCAP_NETCONS,	setifcap),
2010 	DEF_CMD_ARG("pcp",			setifpcp),
2011 	DEF_CMD("-pcp", 0,			disableifpcp),
2012 	DEF_CMD("polling",	IFCAP_POLLING,	setifcap),
2013 	DEF_CMD("-polling",	-IFCAP_POLLING,	setifcap),
2014 	DEF_CMD("tso6",		IFCAP_TSO6,	setifcap),
2015 	DEF_CMD("-tso6",	-IFCAP_TSO6,	setifcap),
2016 	DEF_CMD("tso4",		IFCAP_TSO4,	setifcap),
2017 	DEF_CMD("-tso4",	-IFCAP_TSO4,	setifcap),
2018 	DEF_CMD("tso",		IFCAP_TSO,	setifcap),
2019 	DEF_CMD("-tso",		-IFCAP_TSO,	setifcap),
2020 	DEF_CMD("toe",		IFCAP_TOE,	setifcap),
2021 	DEF_CMD("-toe",		-IFCAP_TOE,	setifcap),
2022 	DEF_CMD("lro",		IFCAP_LRO,	setifcap),
2023 	DEF_CMD("-lro",		-IFCAP_LRO,	setifcap),
2024 	DEF_CMD("txtls",	IFCAP_TXTLS,	setifcap),
2025 	DEF_CMD("-txtls",	-IFCAP_TXTLS,	setifcap),
2026 	DEF_CMD_SARG("rxtls",	IFCAP2_RXTLS4_NAME "," IFCAP2_RXTLS6_NAME,
2027 	    setifcapnv),
2028 	DEF_CMD_SARG("-rxtls",	"-"IFCAP2_RXTLS4_NAME ",-" IFCAP2_RXTLS6_NAME,
2029 	    setifcapnv),
2030 	DEF_CMD("wol",		IFCAP_WOL,	setifcap),
2031 	DEF_CMD("-wol",		-IFCAP_WOL,	setifcap),
2032 	DEF_CMD("wol_ucast",	IFCAP_WOL_UCAST,	setifcap),
2033 	DEF_CMD("-wol_ucast",	-IFCAP_WOL_UCAST,	setifcap),
2034 	DEF_CMD("wol_mcast",	IFCAP_WOL_MCAST,	setifcap),
2035 	DEF_CMD("-wol_mcast",	-IFCAP_WOL_MCAST,	setifcap),
2036 	DEF_CMD("wol_magic",	IFCAP_WOL_MAGIC,	setifcap),
2037 	DEF_CMD("-wol_magic",	-IFCAP_WOL_MAGIC,	setifcap),
2038 	DEF_CMD("txrtlmt",	IFCAP_TXRTLMT,	setifcap),
2039 	DEF_CMD("-txrtlmt",	-IFCAP_TXRTLMT,	setifcap),
2040 	DEF_CMD("txtlsrtlmt",	IFCAP_TXTLS_RTLMT,	setifcap),
2041 	DEF_CMD("-txtlsrtlmt",	-IFCAP_TXTLS_RTLMT,	setifcap),
2042 	DEF_CMD("hwrxtstmp",	IFCAP_HWRXTSTMP,	setifcap),
2043 	DEF_CMD("-hwrxtstmp",	-IFCAP_HWRXTSTMP,	setifcap),
2044 	DEF_CMD("normal",	-IFF_LINK0,	setifflags),
2045 	DEF_CMD("compress",	IFF_LINK0,	setifflags),
2046 	DEF_CMD("noicmp",	IFF_LINK1,	setifflags),
2047 	DEF_CMD_ARG("mtu",			setifmtu),
2048 	DEF_CMD_ARG("name",			setifname),
2049 };
2050 
2051 static __constructor void
2052 ifconfig_ctor(void)
2053 {
2054 	size_t i;
2055 
2056 	for (i = 0; i < nitems(basic_cmds);  i++)
2057 		cmd_register(&basic_cmds[i]);
2058 }
2059