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