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