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