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