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