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