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