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