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