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