323 sdl->sdl_alen != ETHER_ADDR_LEN) 324 continue; 325 } else { 326 if (ifa->ifa_addr->sa_family != afp->af_af) 327 continue; 328 } 329 } 330 namecp = cp; 331 ifindex++; 332 if (ifindex > 1) 333 printf(" "); 334 fputs(name, stdout); 335 continue; 336 } 337 ifindex++; 338 339 if (argc > 0) 340 ifconfig(argc, argv, 0, afp); 341 else 342 status(afp, sdl, ifa); 343 } 344 if (namesonly) 345 printf("\n"); 346 freeifaddrs(ifap); 347 348 exit(0); 349} 350 351static struct afswtch *afs = NULL; 352 353void 354af_register(struct afswtch *p) 355{ 356 p->af_next = afs; 357 afs = p; 358} 359 360static struct afswtch * 361af_getbyname(const char *name) 362{ 363 struct afswtch *afp; 364 365 for (afp = afs; afp != NULL; afp = afp->af_next) 366 if (strcmp(afp->af_name, name) == 0) 367 return afp; 368 return NULL; 369} 370 371static struct afswtch * 372af_getbyfamily(int af) 373{ 374 struct afswtch *afp; 375 376 for (afp = afs; afp != NULL; afp = afp->af_next) 377 if (afp->af_af == af) 378 return afp; 379 return NULL; 380} 381 382static void 383af_other_status(int s) 384{ 385 struct afswtch *afp; 386 uint8_t afmask[howmany(AF_MAX, NBBY)]; 387 388 memset(afmask, 0, sizeof(afmask)); 389 for (afp = afs; afp != NULL; afp = afp->af_next) { 390 if (afp->af_other_status == NULL) 391 continue; 392 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) 393 continue; 394 afp->af_other_status(s); 395 setbit(afmask, afp->af_af); 396 } 397} 398 399static void 400af_all_tunnel_status(int s) 401{ 402 struct afswtch *afp; 403 uint8_t afmask[howmany(AF_MAX, NBBY)]; 404 405 memset(afmask, 0, sizeof(afmask)); 406 for (afp = afs; afp != NULL; afp = afp->af_next) { 407 if (afp->af_status_tunnel == NULL) 408 continue; 409 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) 410 continue; 411 afp->af_status_tunnel(s); 412 setbit(afmask, afp->af_af); 413 } 414} 415 416static struct cmd *cmds = NULL; 417 418void 419cmd_register(struct cmd *p) 420{ 421 p->c_next = cmds; 422 cmds = p; 423} 424 425static const struct cmd * 426cmd_lookup(const char *name, int iscreate) 427{ 428#define N(a) (sizeof(a)/sizeof(a[0])) 429 const struct cmd *p; 430 431 for (p = cmds; p != NULL; p = p->c_next) 432 if (strcmp(name, p->c_name) == 0) { 433 if (iscreate) { 434 if (p->c_iscloneop) 435 return p; 436 } else { 437 if (!p->c_iscloneop) 438 return p; 439 } 440 } 441 return NULL; 442#undef N 443} 444 445struct callback { 446 callback_func *cb_func; 447 void *cb_arg; 448 struct callback *cb_next; 449}; 450static struct callback *callbacks = NULL; 451 452void 453callback_register(callback_func *func, void *arg) 454{ 455 struct callback *cb; 456 457 cb = malloc(sizeof(struct callback)); 458 if (cb == NULL) 459 errx(1, "unable to allocate memory for callback"); 460 cb->cb_func = func; 461 cb->cb_arg = arg; 462 cb->cb_next = callbacks; 463 callbacks = cb; 464} 465 466/* specially-handled commands */ 467static void setifaddr(const char *, int, int, const struct afswtch *); 468static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr); 469 470static void setifdstaddr(const char *, int, int, const struct afswtch *); 471static const struct cmd setifdstaddr_cmd = 472 DEF_CMD("ifdstaddr", 0, setifdstaddr); 473 474static int 475ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp) 476{ 477 const struct afswtch *afp, *nafp; 478 const struct cmd *p; 479 struct callback *cb; 480 int s; 481 482 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 483 afp = uafp != NULL ? uafp : af_getbyname("inet"); 484top: 485 ifr.ifr_addr.sa_family = 486 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ? 487 AF_LOCAL : afp->af_af; 488 489 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 && 490 (uafp != NULL || errno != EPROTONOSUPPORT || 491 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)) 492 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family); 493 494 while (argc > 0) { 495 p = cmd_lookup(*argv, iscreate); 496 if (iscreate && p == NULL) { 497 /* 498 * Push the clone create callback so the new 499 * device is created and can be used for any 500 * remaining arguments. 501 */ 502 cb = callbacks; 503 if (cb == NULL) 504 errx(1, "internal error, no callback"); 505 callbacks = cb->cb_next; 506 cb->cb_func(s, cb->cb_arg); 507 iscreate = 0; 508 /* 509 * Handle any address family spec that 510 * immediately follows and potentially 511 * recreate the socket. 512 */ 513 nafp = af_getbyname(*argv); 514 if (nafp != NULL) { 515 argc--, argv++; 516 if (nafp != afp) { 517 close(s); 518 afp = nafp; 519 goto top; 520 } 521 } 522 /* 523 * Look for a normal parameter. 524 */ 525 continue; 526 } 527 if (p == NULL) { 528 /* 529 * Not a recognized command, choose between setting 530 * the interface address and the dst address. 531 */ 532 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd); 533 } 534 if (p->c_u.c_func || p->c_u.c_func2) { 535 if (p->c_parameter == NEXTARG) { 536 if (argv[1] == NULL) 537 errx(1, "'%s' requires argument", 538 p->c_name); 539 p->c_u.c_func(argv[1], 0, s, afp); 540 argc--, argv++; 541 } else if (p->c_parameter == OPTARG) { 542 p->c_u.c_func(argv[1], 0, s, afp); 543 if (argv[1] != NULL) 544 argc--, argv++; 545 } else if (p->c_parameter == NEXTARG2) { 546 if (argc < 3) 547 errx(1, "'%s' requires 2 arguments", 548 p->c_name); 549 p->c_u.c_func2(argv[1], argv[2], s, afp); 550 argc -= 2, argv += 2; 551 } else 552 p->c_u.c_func(*argv, p->c_parameter, s, afp); 553 } 554 argc--, argv++; 555 } 556 557 /* 558 * Do any post argument processing required by the address family. 559 */ 560 if (afp->af_postproc != NULL) 561 afp->af_postproc(s, afp); 562 /* 563 * Do deferred callbacks registered while processing 564 * command-line arguments. 565 */ 566 for (cb = callbacks; cb != NULL; cb = cb->cb_next) 567 cb->cb_func(s, cb->cb_arg); 568 /* 569 * Do deferred operations. 570 */ 571 if (clearaddr) { 572 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) { 573 warnx("interface %s cannot change %s addresses!", 574 name, afp->af_name); 575 clearaddr = 0; 576 } 577 } 578 if (clearaddr) { 579 int ret; 580 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name); 581 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq); 582 if (ret < 0) { 583 if (errno == EADDRNOTAVAIL && (doalias >= 0)) { 584 /* means no previous address for interface */ 585 } else 586 Perror("ioctl (SIOCDIFADDR)"); 587 } 588 } 589 if (newaddr) { 590 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) { 591 warnx("interface %s cannot change %s addresses!", 592 name, afp->af_name); 593 newaddr = 0; 594 } 595 } 596 if (newaddr && (setaddr || setmask)) { 597 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name); 598 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0) 599 Perror("ioctl (SIOCAIFADDR)"); 600 } 601 602 close(s); 603 return(0); 604} 605 606/*ARGSUSED*/ 607static void 608setifaddr(const char *addr, int param, int s, const struct afswtch *afp) 609{ 610 if (afp->af_getaddr == NULL) 611 return; 612 /* 613 * Delay the ioctl to set the interface addr until flags are all set. 614 * The address interpretation may depend on the flags, 615 * and the flags may change when the address is set. 616 */ 617 setaddr++; 618 if (doalias == 0 && afp->af_af != AF_LINK) 619 clearaddr = 1; 620 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR)); 621} 622 623static void 624settunnel(const char *src, const char *dst, int s, const struct afswtch *afp) 625{ 626 struct addrinfo *srcres, *dstres; 627 int ecode; 628 629 if (afp->af_settunnel == NULL) { 630 warn("address family %s does not support tunnel setup", 631 afp->af_name); 632 return; 633 } 634 635 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0) 636 errx(1, "error in parsing address string: %s", 637 gai_strerror(ecode)); 638 639 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0) 640 errx(1, "error in parsing address string: %s", 641 gai_strerror(ecode)); 642 643 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family) 644 errx(1, 645 "source and destination address families do not match"); 646 647 afp->af_settunnel(s, srcres, dstres); 648 649 freeaddrinfo(srcres); 650 freeaddrinfo(dstres); 651} 652 653/* ARGSUSED */ 654static void 655deletetunnel(const char *vname, int param, int s, const struct afswtch *afp) 656{ 657 658 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0) 659 err(1, "SIOCDIFPHYADDR"); 660} 661 662static void 663setifvnet(const char *jname, int dummy __unused, int s, 664 const struct afswtch *afp) 665{ 666 struct ifreq my_ifr; 667 668 memcpy(&my_ifr, &ifr, sizeof(my_ifr)); 669 my_ifr.ifr_jid = jail_getid(jname); 670 if (my_ifr.ifr_jid < 0) 671 errx(1, "%s", jail_errmsg); 672 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0) 673 err(1, "SIOCSIFVNET"); 674} 675 676static void 677setifrvnet(const char *jname, int dummy __unused, int s, 678 const struct afswtch *afp) 679{ 680 struct ifreq my_ifr; 681 682 memcpy(&my_ifr, &ifr, sizeof(my_ifr)); 683 my_ifr.ifr_jid = jail_getid(jname); 684 if (my_ifr.ifr_jid < 0) 685 errx(1, "%s", jail_errmsg); 686 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0) 687 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name); 688} 689 690static void 691setifnetmask(const char *addr, int dummy __unused, int s, 692 const struct afswtch *afp) 693{ 694 if (afp->af_getaddr != NULL) { 695 setmask++; 696 afp->af_getaddr(addr, MASK); 697 } 698} 699 700static void 701setifbroadaddr(const char *addr, int dummy __unused, int s, 702 const struct afswtch *afp) 703{ 704 if (afp->af_getaddr != NULL) 705 afp->af_getaddr(addr, DSTADDR); 706} 707 708static void 709setifipdst(const char *addr, int dummy __unused, int s, 710 const struct afswtch *afp) 711{ 712 const struct afswtch *inet; 713 714 inet = af_getbyname("inet"); 715 if (inet == NULL) 716 return; 717 inet->af_getaddr(addr, DSTADDR); 718 clearaddr = 0; 719 newaddr = 0; 720} 721 722static void 723notealias(const char *addr, int param, int s, const struct afswtch *afp) 724{ 725#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr)) 726 if (setaddr && doalias == 0 && param < 0) 727 if (afp->af_addreq != NULL && afp->af_ridreq != NULL) 728 bcopy((caddr_t)rqtosa(af_addreq), 729 (caddr_t)rqtosa(af_ridreq), 730 rqtosa(af_addreq)->sa_len); 731 doalias = param; 732 if (param < 0) { 733 clearaddr = 1; 734 newaddr = 0; 735 } else 736 clearaddr = 0; 737#undef rqtosa 738} 739 740/*ARGSUSED*/ 741static void 742setifdstaddr(const char *addr, int param __unused, int s, 743 const struct afswtch *afp) 744{ 745 if (afp->af_getaddr != NULL) 746 afp->af_getaddr(addr, DSTADDR); 747} 748 749/* 750 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion 751 * of the ifreq structure, which may confuse other parts of ifconfig. 752 * Make a private copy so we can avoid that. 753 */ 754static void 755setifflags(const char *vname, int value, int s, const struct afswtch *afp) 756{ 757 struct ifreq my_ifr; 758 int flags; 759 760 memset(&my_ifr, 0, sizeof(my_ifr)); 761 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name)); 762 763 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) { 764 Perror("ioctl (SIOCGIFFLAGS)"); 765 exit(1); 766 } 767 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16); 768 769 if (value < 0) { 770 value = -value; 771 flags &= ~value; 772 } else 773 flags |= value; 774 my_ifr.ifr_flags = flags & 0xffff; 775 my_ifr.ifr_flagshigh = flags >> 16; 776 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0) 777 Perror(vname); 778} 779 780void 781setifcap(const char *vname, int value, int s, const struct afswtch *afp) 782{ 783 int flags; 784 785 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) { 786 Perror("ioctl (SIOCGIFCAP)"); 787 exit(1); 788 } 789 flags = ifr.ifr_curcap; 790 if (value < 0) { 791 value = -value; 792 flags &= ~value; 793 } else 794 flags |= value; 795 flags &= ifr.ifr_reqcap; 796 ifr.ifr_reqcap = flags; 797 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0) 798 Perror(vname); 799} 800 801static void 802setifmetric(const char *val, int dummy __unused, int s, 803 const struct afswtch *afp) 804{ 805 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 806 ifr.ifr_metric = atoi(val); 807 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0) 808 warn("ioctl (set metric)"); 809} 810 811static void 812setifmtu(const char *val, int dummy __unused, int s, 813 const struct afswtch *afp) 814{ 815 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 816 ifr.ifr_mtu = atoi(val); 817 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0) 818 warn("ioctl (set mtu)"); 819} 820 821static void 822setifname(const char *val, int dummy __unused, int s, 823 const struct afswtch *afp) 824{ 825 char *newname; 826 827 newname = strdup(val); 828 if (newname == NULL) { 829 warn("no memory to set ifname"); 830 return; 831 } 832 ifr.ifr_data = newname; 833 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) { 834 warn("ioctl (set name)"); 835 free(newname); 836 return; 837 } 838 strlcpy(name, newname, sizeof(name)); 839 free(newname); 840} 841 842/* ARGSUSED */ 843static void 844setifdescr(const char *val, int dummy __unused, int s, 845 const struct afswtch *afp) 846{ 847 char *newdescr; 848 849 ifr.ifr_buffer.length = strlen(val) + 1; 850 if (ifr.ifr_buffer.length == 1) { 851 ifr.ifr_buffer.buffer = newdescr = NULL; 852 ifr.ifr_buffer.length = 0; 853 } else { 854 newdescr = strdup(val); 855 ifr.ifr_buffer.buffer = newdescr; 856 if (newdescr == NULL) { 857 warn("no memory to set ifdescr"); 858 return; 859 } 860 } 861 862 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) 863 warn("ioctl (set descr)"); 864 865 free(newdescr); 866} 867 868/* ARGSUSED */ 869static void 870unsetifdescr(const char *val, int value, int s, const struct afswtch *afp) 871{ 872 873 setifdescr("", 0, s, 0); 874} 875 876#define IFFBITS \ 877"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \ 878"\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \ 879"\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP" 880 881#define IFCAPBITS \ 882"\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \ 883"\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \ 884"\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE" 885 886/* 887 * Print the status of the interface. If an address family was 888 * specified, show only it; otherwise, show them all. 889 */ 890static void 891status(const struct afswtch *afp, const struct sockaddr_dl *sdl, 892 struct ifaddrs *ifa) 893{ 894 struct ifaddrs *ift; 895 int allfamilies, s; 896 struct ifstat ifs; 897 898 if (afp == NULL) { 899 allfamilies = 1; 900 ifr.ifr_addr.sa_family = AF_LOCAL; 901 } else { 902 allfamilies = 0; 903 ifr.ifr_addr.sa_family = 904 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af; 905 } 906 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 907 908 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0); 909 if (s < 0) 910 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family); 911 912 printf("%s: ", name); 913 printb("flags", ifa->ifa_flags, IFFBITS); 914 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1) 915 printf(" metric %d", ifr.ifr_metric); 916 if (ioctl(s, SIOCGIFMTU, &ifr) != -1) 917 printf(" mtu %d", ifr.ifr_mtu); 918 putchar('\n'); 919 920 for (;;) { 921 if ((descr = reallocf(descr, descrlen)) != NULL) { 922 ifr.ifr_buffer.buffer = descr; 923 ifr.ifr_buffer.length = descrlen; 924 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) { 925 if (ifr.ifr_buffer.buffer == descr) { 926 if (strlen(descr) > 0) 927 printf("\tdescription: %s\n", 928 descr); 929 } else if (ifr.ifr_buffer.length > descrlen) { 930 descrlen = ifr.ifr_buffer.length; 931 continue; 932 } 933 } 934 } else 935 warn("unable to allocate memory for interface" 936 "description"); 937 break; 938 } 939 940 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) { 941 if (ifr.ifr_curcap != 0) { 942 printb("\toptions", ifr.ifr_curcap, IFCAPBITS); 943 putchar('\n'); 944 } 945 if (supmedia && ifr.ifr_reqcap != 0) { 946 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS); 947 putchar('\n'); 948 } 949 } 950 951 tunnel_status(s); 952 953 for (ift = ifa; ift != NULL; ift = ift->ifa_next) { 954 if (ift->ifa_addr == NULL) 955 continue; 956 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0) 957 continue; 958 if (allfamilies) { 959 const struct afswtch *p; 960 p = af_getbyfamily(ift->ifa_addr->sa_family); 961 if (p != NULL && p->af_status != NULL) 962 p->af_status(s, ift); 963 } else if (afp->af_af == ift->ifa_addr->sa_family) 964 afp->af_status(s, ift); 965 } 966#if 0 967 if (allfamilies || afp->af_af == AF_LINK) { 968 const struct afswtch *lafp; 969 970 /* 971 * Hack; the link level address is received separately 972 * from the routing information so any address is not 973 * handled above. Cobble together an entry and invoke 974 * the status method specially. 975 */ 976 lafp = af_getbyname("lladdr"); 977 if (lafp != NULL) { 978 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl; 979 lafp->af_status(s, &info); 980 } 981 } 982#endif 983 if (allfamilies) 984 af_other_status(s); 985 else if (afp->af_other_status != NULL) 986 afp->af_other_status(s); 987 988 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name); 989 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) 990 printf("%s", ifs.ascii); 991 992 close(s); 993 return; 994} 995 996static void 997tunnel_status(int s) 998{ 999 af_all_tunnel_status(s); 1000} 1001 1002void 1003Perror(const char *cmd) 1004{ 1005 switch (errno) { 1006 1007 case ENXIO: 1008 errx(1, "%s: no such interface", cmd); 1009 break; 1010 1011 case EPERM: 1012 errx(1, "%s: permission denied", cmd); 1013 break; 1014 1015 default: 1016 err(1, "%s", cmd); 1017 } 1018} 1019 1020/* 1021 * Print a value a la the %b format of the kernel's printf 1022 */ 1023void 1024printb(const char *s, unsigned v, const char *bits) 1025{ 1026 int i, any = 0; 1027 char c; 1028 1029 if (bits && *bits == 8) 1030 printf("%s=%o", s, v); 1031 else 1032 printf("%s=%x", s, v); 1033 bits++; 1034 if (bits) { 1035 putchar('<'); 1036 while ((i = *bits++) != '\0') { 1037 if (v & (1 << (i-1))) { 1038 if (any) 1039 putchar(','); 1040 any = 1; 1041 for (; (c = *bits) > 32; bits++) 1042 putchar(c); 1043 } else 1044 for (; *bits > 32; bits++) 1045 ; 1046 } 1047 putchar('>'); 1048 } 1049} 1050 1051void 1052ifmaybeload(const char *name) 1053{ 1054#define MOD_PREFIX_LEN 3 /* "if_" */ 1055 struct module_stat mstat; 1056 int fileid, modid; 1057 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp; 1058 const char *cp; 1059 1060 /* loading suppressed by the user */ 1061 if (noload) 1062 return; 1063 1064 /* trim the interface number off the end */ 1065 strlcpy(ifname, name, sizeof(ifname)); 1066 for (dp = ifname; *dp != 0; dp++) 1067 if (isdigit(*dp)) { 1068 *dp = 0; 1069 break; 1070 } 1071 1072 /* turn interface and unit into module name */ 1073 strcpy(ifkind, "if_"); 1074 strlcpy(ifkind + MOD_PREFIX_LEN, ifname, 1075 sizeof(ifkind) - MOD_PREFIX_LEN); 1076 1077 /* scan files in kernel */ 1078 mstat.version = sizeof(struct module_stat); 1079 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { 1080 /* scan modules in file */ 1081 for (modid = kldfirstmod(fileid); modid > 0; 1082 modid = modfnext(modid)) { 1083 if (modstat(modid, &mstat) < 0) 1084 continue; 1085 /* strip bus name if present */ 1086 if ((cp = strchr(mstat.name, '/')) != NULL) { 1087 cp++; 1088 } else { 1089 cp = mstat.name; 1090 } 1091 /* already loaded? */ 1092 if (strncmp(ifname, cp, strlen(ifname) + 1) == 0 || 1093 strncmp(ifkind, cp, strlen(ifkind) + 1) == 0) 1094 return; 1095 } 1096 } 1097 1098 /* not present, we should try to load it */ 1099 kldload(ifkind); 1100} 1101 1102static struct cmd basic_cmds[] = { 1103 DEF_CMD("up", IFF_UP, setifflags), 1104 DEF_CMD("down", -IFF_UP, setifflags), 1105 DEF_CMD("arp", -IFF_NOARP, setifflags), 1106 DEF_CMD("-arp", IFF_NOARP, setifflags), 1107 DEF_CMD("debug", IFF_DEBUG, setifflags), 1108 DEF_CMD("-debug", -IFF_DEBUG, setifflags), 1109 DEF_CMD_ARG("description", setifdescr), 1110 DEF_CMD_ARG("descr", setifdescr), 1111 DEF_CMD("-description", 0, unsetifdescr), 1112 DEF_CMD("-descr", 0, unsetifdescr), 1113 DEF_CMD("promisc", IFF_PPROMISC, setifflags), 1114 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags), 1115 DEF_CMD("add", IFF_UP, notealias), 1116 DEF_CMD("alias", IFF_UP, notealias), 1117 DEF_CMD("-alias", -IFF_UP, notealias), 1118 DEF_CMD("delete", -IFF_UP, notealias), 1119 DEF_CMD("remove", -IFF_UP, notealias), 1120#ifdef notdef 1121#define EN_SWABIPS 0x1000 1122 DEF_CMD("swabips", EN_SWABIPS, setifflags), 1123 DEF_CMD("-swabips", -EN_SWABIPS, setifflags), 1124#endif 1125 DEF_CMD_ARG("netmask", setifnetmask), 1126 DEF_CMD_ARG("metric", setifmetric), 1127 DEF_CMD_ARG("broadcast", setifbroadaddr), 1128 DEF_CMD_ARG("ipdst", setifipdst), 1129 DEF_CMD_ARG2("tunnel", settunnel), 1130 DEF_CMD("-tunnel", 0, deletetunnel), 1131 DEF_CMD("deletetunnel", 0, deletetunnel), 1132 DEF_CMD_ARG("vnet", setifvnet), 1133 DEF_CMD_ARG("-vnet", setifrvnet), 1134 DEF_CMD("link0", IFF_LINK0, setifflags), 1135 DEF_CMD("-link0", -IFF_LINK0, setifflags), 1136 DEF_CMD("link1", IFF_LINK1, setifflags), 1137 DEF_CMD("-link1", -IFF_LINK1, setifflags), 1138 DEF_CMD("link2", IFF_LINK2, setifflags), 1139 DEF_CMD("-link2", -IFF_LINK2, setifflags), 1140 DEF_CMD("monitor", IFF_MONITOR, setifflags), 1141 DEF_CMD("-monitor", -IFF_MONITOR, setifflags), 1142 DEF_CMD("staticarp", IFF_STATICARP, setifflags), 1143 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags), 1144 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap), 1145 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap), 1146 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap), 1147 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap), 1148 DEF_CMD("netcons", IFCAP_NETCONS, setifcap), 1149 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap), 1150 DEF_CMD("polling", IFCAP_POLLING, setifcap), 1151 DEF_CMD("-polling", -IFCAP_POLLING, setifcap), 1152 DEF_CMD("tso", IFCAP_TSO, setifcap), 1153 DEF_CMD("-tso", -IFCAP_TSO, setifcap), 1154 DEF_CMD("lro", IFCAP_LRO, setifcap), 1155 DEF_CMD("-lro", -IFCAP_LRO, setifcap), 1156 DEF_CMD("wol", IFCAP_WOL, setifcap), 1157 DEF_CMD("-wol", -IFCAP_WOL, setifcap), 1158 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap), 1159 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap), 1160 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap), 1161 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap), 1162 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap), 1163 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap), 1164 DEF_CMD("normal", -IFF_LINK0, setifflags), 1165 DEF_CMD("compress", IFF_LINK0, setifflags), 1166 DEF_CMD("noicmp", IFF_LINK1, setifflags), 1167 DEF_CMD_ARG("mtu", setifmtu), 1168 DEF_CMD_ARG("name", setifname), 1169}; 1170 1171static __constructor void 1172ifconfig_ctor(void) 1173{ 1174#define N(a) (sizeof(a) / sizeof(a[0])) 1175 size_t i; 1176 1177 for (i = 0; i < N(basic_cmds); i++) 1178 cmd_register(&basic_cmds[i]); 1179#undef N 1180}
| 325 sdl->sdl_alen != ETHER_ADDR_LEN) 326 continue; 327 } else { 328 if (ifa->ifa_addr->sa_family != afp->af_af) 329 continue; 330 } 331 } 332 namecp = cp; 333 ifindex++; 334 if (ifindex > 1) 335 printf(" "); 336 fputs(name, stdout); 337 continue; 338 } 339 ifindex++; 340 341 if (argc > 0) 342 ifconfig(argc, argv, 0, afp); 343 else 344 status(afp, sdl, ifa); 345 } 346 if (namesonly) 347 printf("\n"); 348 freeifaddrs(ifap); 349 350 exit(0); 351} 352 353static struct afswtch *afs = NULL; 354 355void 356af_register(struct afswtch *p) 357{ 358 p->af_next = afs; 359 afs = p; 360} 361 362static struct afswtch * 363af_getbyname(const char *name) 364{ 365 struct afswtch *afp; 366 367 for (afp = afs; afp != NULL; afp = afp->af_next) 368 if (strcmp(afp->af_name, name) == 0) 369 return afp; 370 return NULL; 371} 372 373static struct afswtch * 374af_getbyfamily(int af) 375{ 376 struct afswtch *afp; 377 378 for (afp = afs; afp != NULL; afp = afp->af_next) 379 if (afp->af_af == af) 380 return afp; 381 return NULL; 382} 383 384static void 385af_other_status(int s) 386{ 387 struct afswtch *afp; 388 uint8_t afmask[howmany(AF_MAX, NBBY)]; 389 390 memset(afmask, 0, sizeof(afmask)); 391 for (afp = afs; afp != NULL; afp = afp->af_next) { 392 if (afp->af_other_status == NULL) 393 continue; 394 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) 395 continue; 396 afp->af_other_status(s); 397 setbit(afmask, afp->af_af); 398 } 399} 400 401static void 402af_all_tunnel_status(int s) 403{ 404 struct afswtch *afp; 405 uint8_t afmask[howmany(AF_MAX, NBBY)]; 406 407 memset(afmask, 0, sizeof(afmask)); 408 for (afp = afs; afp != NULL; afp = afp->af_next) { 409 if (afp->af_status_tunnel == NULL) 410 continue; 411 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) 412 continue; 413 afp->af_status_tunnel(s); 414 setbit(afmask, afp->af_af); 415 } 416} 417 418static struct cmd *cmds = NULL; 419 420void 421cmd_register(struct cmd *p) 422{ 423 p->c_next = cmds; 424 cmds = p; 425} 426 427static const struct cmd * 428cmd_lookup(const char *name, int iscreate) 429{ 430#define N(a) (sizeof(a)/sizeof(a[0])) 431 const struct cmd *p; 432 433 for (p = cmds; p != NULL; p = p->c_next) 434 if (strcmp(name, p->c_name) == 0) { 435 if (iscreate) { 436 if (p->c_iscloneop) 437 return p; 438 } else { 439 if (!p->c_iscloneop) 440 return p; 441 } 442 } 443 return NULL; 444#undef N 445} 446 447struct callback { 448 callback_func *cb_func; 449 void *cb_arg; 450 struct callback *cb_next; 451}; 452static struct callback *callbacks = NULL; 453 454void 455callback_register(callback_func *func, void *arg) 456{ 457 struct callback *cb; 458 459 cb = malloc(sizeof(struct callback)); 460 if (cb == NULL) 461 errx(1, "unable to allocate memory for callback"); 462 cb->cb_func = func; 463 cb->cb_arg = arg; 464 cb->cb_next = callbacks; 465 callbacks = cb; 466} 467 468/* specially-handled commands */ 469static void setifaddr(const char *, int, int, const struct afswtch *); 470static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr); 471 472static void setifdstaddr(const char *, int, int, const struct afswtch *); 473static const struct cmd setifdstaddr_cmd = 474 DEF_CMD("ifdstaddr", 0, setifdstaddr); 475 476static int 477ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp) 478{ 479 const struct afswtch *afp, *nafp; 480 const struct cmd *p; 481 struct callback *cb; 482 int s; 483 484 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 485 afp = uafp != NULL ? uafp : af_getbyname("inet"); 486top: 487 ifr.ifr_addr.sa_family = 488 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ? 489 AF_LOCAL : afp->af_af; 490 491 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 && 492 (uafp != NULL || errno != EPROTONOSUPPORT || 493 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)) 494 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family); 495 496 while (argc > 0) { 497 p = cmd_lookup(*argv, iscreate); 498 if (iscreate && p == NULL) { 499 /* 500 * Push the clone create callback so the new 501 * device is created and can be used for any 502 * remaining arguments. 503 */ 504 cb = callbacks; 505 if (cb == NULL) 506 errx(1, "internal error, no callback"); 507 callbacks = cb->cb_next; 508 cb->cb_func(s, cb->cb_arg); 509 iscreate = 0; 510 /* 511 * Handle any address family spec that 512 * immediately follows and potentially 513 * recreate the socket. 514 */ 515 nafp = af_getbyname(*argv); 516 if (nafp != NULL) { 517 argc--, argv++; 518 if (nafp != afp) { 519 close(s); 520 afp = nafp; 521 goto top; 522 } 523 } 524 /* 525 * Look for a normal parameter. 526 */ 527 continue; 528 } 529 if (p == NULL) { 530 /* 531 * Not a recognized command, choose between setting 532 * the interface address and the dst address. 533 */ 534 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd); 535 } 536 if (p->c_u.c_func || p->c_u.c_func2) { 537 if (p->c_parameter == NEXTARG) { 538 if (argv[1] == NULL) 539 errx(1, "'%s' requires argument", 540 p->c_name); 541 p->c_u.c_func(argv[1], 0, s, afp); 542 argc--, argv++; 543 } else if (p->c_parameter == OPTARG) { 544 p->c_u.c_func(argv[1], 0, s, afp); 545 if (argv[1] != NULL) 546 argc--, argv++; 547 } else if (p->c_parameter == NEXTARG2) { 548 if (argc < 3) 549 errx(1, "'%s' requires 2 arguments", 550 p->c_name); 551 p->c_u.c_func2(argv[1], argv[2], s, afp); 552 argc -= 2, argv += 2; 553 } else 554 p->c_u.c_func(*argv, p->c_parameter, s, afp); 555 } 556 argc--, argv++; 557 } 558 559 /* 560 * Do any post argument processing required by the address family. 561 */ 562 if (afp->af_postproc != NULL) 563 afp->af_postproc(s, afp); 564 /* 565 * Do deferred callbacks registered while processing 566 * command-line arguments. 567 */ 568 for (cb = callbacks; cb != NULL; cb = cb->cb_next) 569 cb->cb_func(s, cb->cb_arg); 570 /* 571 * Do deferred operations. 572 */ 573 if (clearaddr) { 574 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) { 575 warnx("interface %s cannot change %s addresses!", 576 name, afp->af_name); 577 clearaddr = 0; 578 } 579 } 580 if (clearaddr) { 581 int ret; 582 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name); 583 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq); 584 if (ret < 0) { 585 if (errno == EADDRNOTAVAIL && (doalias >= 0)) { 586 /* means no previous address for interface */ 587 } else 588 Perror("ioctl (SIOCDIFADDR)"); 589 } 590 } 591 if (newaddr) { 592 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) { 593 warnx("interface %s cannot change %s addresses!", 594 name, afp->af_name); 595 newaddr = 0; 596 } 597 } 598 if (newaddr && (setaddr || setmask)) { 599 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name); 600 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0) 601 Perror("ioctl (SIOCAIFADDR)"); 602 } 603 604 close(s); 605 return(0); 606} 607 608/*ARGSUSED*/ 609static void 610setifaddr(const char *addr, int param, int s, const struct afswtch *afp) 611{ 612 if (afp->af_getaddr == NULL) 613 return; 614 /* 615 * Delay the ioctl to set the interface addr until flags are all set. 616 * The address interpretation may depend on the flags, 617 * and the flags may change when the address is set. 618 */ 619 setaddr++; 620 if (doalias == 0 && afp->af_af != AF_LINK) 621 clearaddr = 1; 622 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR)); 623} 624 625static void 626settunnel(const char *src, const char *dst, int s, const struct afswtch *afp) 627{ 628 struct addrinfo *srcres, *dstres; 629 int ecode; 630 631 if (afp->af_settunnel == NULL) { 632 warn("address family %s does not support tunnel setup", 633 afp->af_name); 634 return; 635 } 636 637 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0) 638 errx(1, "error in parsing address string: %s", 639 gai_strerror(ecode)); 640 641 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0) 642 errx(1, "error in parsing address string: %s", 643 gai_strerror(ecode)); 644 645 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family) 646 errx(1, 647 "source and destination address families do not match"); 648 649 afp->af_settunnel(s, srcres, dstres); 650 651 freeaddrinfo(srcres); 652 freeaddrinfo(dstres); 653} 654 655/* ARGSUSED */ 656static void 657deletetunnel(const char *vname, int param, int s, const struct afswtch *afp) 658{ 659 660 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0) 661 err(1, "SIOCDIFPHYADDR"); 662} 663 664static void 665setifvnet(const char *jname, int dummy __unused, int s, 666 const struct afswtch *afp) 667{ 668 struct ifreq my_ifr; 669 670 memcpy(&my_ifr, &ifr, sizeof(my_ifr)); 671 my_ifr.ifr_jid = jail_getid(jname); 672 if (my_ifr.ifr_jid < 0) 673 errx(1, "%s", jail_errmsg); 674 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0) 675 err(1, "SIOCSIFVNET"); 676} 677 678static void 679setifrvnet(const char *jname, int dummy __unused, int s, 680 const struct afswtch *afp) 681{ 682 struct ifreq my_ifr; 683 684 memcpy(&my_ifr, &ifr, sizeof(my_ifr)); 685 my_ifr.ifr_jid = jail_getid(jname); 686 if (my_ifr.ifr_jid < 0) 687 errx(1, "%s", jail_errmsg); 688 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0) 689 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name); 690} 691 692static void 693setifnetmask(const char *addr, int dummy __unused, int s, 694 const struct afswtch *afp) 695{ 696 if (afp->af_getaddr != NULL) { 697 setmask++; 698 afp->af_getaddr(addr, MASK); 699 } 700} 701 702static void 703setifbroadaddr(const char *addr, int dummy __unused, int s, 704 const struct afswtch *afp) 705{ 706 if (afp->af_getaddr != NULL) 707 afp->af_getaddr(addr, DSTADDR); 708} 709 710static void 711setifipdst(const char *addr, int dummy __unused, int s, 712 const struct afswtch *afp) 713{ 714 const struct afswtch *inet; 715 716 inet = af_getbyname("inet"); 717 if (inet == NULL) 718 return; 719 inet->af_getaddr(addr, DSTADDR); 720 clearaddr = 0; 721 newaddr = 0; 722} 723 724static void 725notealias(const char *addr, int param, int s, const struct afswtch *afp) 726{ 727#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr)) 728 if (setaddr && doalias == 0 && param < 0) 729 if (afp->af_addreq != NULL && afp->af_ridreq != NULL) 730 bcopy((caddr_t)rqtosa(af_addreq), 731 (caddr_t)rqtosa(af_ridreq), 732 rqtosa(af_addreq)->sa_len); 733 doalias = param; 734 if (param < 0) { 735 clearaddr = 1; 736 newaddr = 0; 737 } else 738 clearaddr = 0; 739#undef rqtosa 740} 741 742/*ARGSUSED*/ 743static void 744setifdstaddr(const char *addr, int param __unused, int s, 745 const struct afswtch *afp) 746{ 747 if (afp->af_getaddr != NULL) 748 afp->af_getaddr(addr, DSTADDR); 749} 750 751/* 752 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion 753 * of the ifreq structure, which may confuse other parts of ifconfig. 754 * Make a private copy so we can avoid that. 755 */ 756static void 757setifflags(const char *vname, int value, int s, const struct afswtch *afp) 758{ 759 struct ifreq my_ifr; 760 int flags; 761 762 memset(&my_ifr, 0, sizeof(my_ifr)); 763 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name)); 764 765 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) { 766 Perror("ioctl (SIOCGIFFLAGS)"); 767 exit(1); 768 } 769 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16); 770 771 if (value < 0) { 772 value = -value; 773 flags &= ~value; 774 } else 775 flags |= value; 776 my_ifr.ifr_flags = flags & 0xffff; 777 my_ifr.ifr_flagshigh = flags >> 16; 778 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0) 779 Perror(vname); 780} 781 782void 783setifcap(const char *vname, int value, int s, const struct afswtch *afp) 784{ 785 int flags; 786 787 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) { 788 Perror("ioctl (SIOCGIFCAP)"); 789 exit(1); 790 } 791 flags = ifr.ifr_curcap; 792 if (value < 0) { 793 value = -value; 794 flags &= ~value; 795 } else 796 flags |= value; 797 flags &= ifr.ifr_reqcap; 798 ifr.ifr_reqcap = flags; 799 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0) 800 Perror(vname); 801} 802 803static void 804setifmetric(const char *val, int dummy __unused, int s, 805 const struct afswtch *afp) 806{ 807 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 808 ifr.ifr_metric = atoi(val); 809 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0) 810 warn("ioctl (set metric)"); 811} 812 813static void 814setifmtu(const char *val, int dummy __unused, int s, 815 const struct afswtch *afp) 816{ 817 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 818 ifr.ifr_mtu = atoi(val); 819 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0) 820 warn("ioctl (set mtu)"); 821} 822 823static void 824setifname(const char *val, int dummy __unused, int s, 825 const struct afswtch *afp) 826{ 827 char *newname; 828 829 newname = strdup(val); 830 if (newname == NULL) { 831 warn("no memory to set ifname"); 832 return; 833 } 834 ifr.ifr_data = newname; 835 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) { 836 warn("ioctl (set name)"); 837 free(newname); 838 return; 839 } 840 strlcpy(name, newname, sizeof(name)); 841 free(newname); 842} 843 844/* ARGSUSED */ 845static void 846setifdescr(const char *val, int dummy __unused, int s, 847 const struct afswtch *afp) 848{ 849 char *newdescr; 850 851 ifr.ifr_buffer.length = strlen(val) + 1; 852 if (ifr.ifr_buffer.length == 1) { 853 ifr.ifr_buffer.buffer = newdescr = NULL; 854 ifr.ifr_buffer.length = 0; 855 } else { 856 newdescr = strdup(val); 857 ifr.ifr_buffer.buffer = newdescr; 858 if (newdescr == NULL) { 859 warn("no memory to set ifdescr"); 860 return; 861 } 862 } 863 864 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) 865 warn("ioctl (set descr)"); 866 867 free(newdescr); 868} 869 870/* ARGSUSED */ 871static void 872unsetifdescr(const char *val, int value, int s, const struct afswtch *afp) 873{ 874 875 setifdescr("", 0, s, 0); 876} 877 878#define IFFBITS \ 879"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \ 880"\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \ 881"\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP" 882 883#define IFCAPBITS \ 884"\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \ 885"\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \ 886"\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE" 887 888/* 889 * Print the status of the interface. If an address family was 890 * specified, show only it; otherwise, show them all. 891 */ 892static void 893status(const struct afswtch *afp, const struct sockaddr_dl *sdl, 894 struct ifaddrs *ifa) 895{ 896 struct ifaddrs *ift; 897 int allfamilies, s; 898 struct ifstat ifs; 899 900 if (afp == NULL) { 901 allfamilies = 1; 902 ifr.ifr_addr.sa_family = AF_LOCAL; 903 } else { 904 allfamilies = 0; 905 ifr.ifr_addr.sa_family = 906 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af; 907 } 908 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 909 910 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0); 911 if (s < 0) 912 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family); 913 914 printf("%s: ", name); 915 printb("flags", ifa->ifa_flags, IFFBITS); 916 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1) 917 printf(" metric %d", ifr.ifr_metric); 918 if (ioctl(s, SIOCGIFMTU, &ifr) != -1) 919 printf(" mtu %d", ifr.ifr_mtu); 920 putchar('\n'); 921 922 for (;;) { 923 if ((descr = reallocf(descr, descrlen)) != NULL) { 924 ifr.ifr_buffer.buffer = descr; 925 ifr.ifr_buffer.length = descrlen; 926 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) { 927 if (ifr.ifr_buffer.buffer == descr) { 928 if (strlen(descr) > 0) 929 printf("\tdescription: %s\n", 930 descr); 931 } else if (ifr.ifr_buffer.length > descrlen) { 932 descrlen = ifr.ifr_buffer.length; 933 continue; 934 } 935 } 936 } else 937 warn("unable to allocate memory for interface" 938 "description"); 939 break; 940 } 941 942 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) { 943 if (ifr.ifr_curcap != 0) { 944 printb("\toptions", ifr.ifr_curcap, IFCAPBITS); 945 putchar('\n'); 946 } 947 if (supmedia && ifr.ifr_reqcap != 0) { 948 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS); 949 putchar('\n'); 950 } 951 } 952 953 tunnel_status(s); 954 955 for (ift = ifa; ift != NULL; ift = ift->ifa_next) { 956 if (ift->ifa_addr == NULL) 957 continue; 958 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0) 959 continue; 960 if (allfamilies) { 961 const struct afswtch *p; 962 p = af_getbyfamily(ift->ifa_addr->sa_family); 963 if (p != NULL && p->af_status != NULL) 964 p->af_status(s, ift); 965 } else if (afp->af_af == ift->ifa_addr->sa_family) 966 afp->af_status(s, ift); 967 } 968#if 0 969 if (allfamilies || afp->af_af == AF_LINK) { 970 const struct afswtch *lafp; 971 972 /* 973 * Hack; the link level address is received separately 974 * from the routing information so any address is not 975 * handled above. Cobble together an entry and invoke 976 * the status method specially. 977 */ 978 lafp = af_getbyname("lladdr"); 979 if (lafp != NULL) { 980 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl; 981 lafp->af_status(s, &info); 982 } 983 } 984#endif 985 if (allfamilies) 986 af_other_status(s); 987 else if (afp->af_other_status != NULL) 988 afp->af_other_status(s); 989 990 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name); 991 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) 992 printf("%s", ifs.ascii); 993 994 close(s); 995 return; 996} 997 998static void 999tunnel_status(int s) 1000{ 1001 af_all_tunnel_status(s); 1002} 1003 1004void 1005Perror(const char *cmd) 1006{ 1007 switch (errno) { 1008 1009 case ENXIO: 1010 errx(1, "%s: no such interface", cmd); 1011 break; 1012 1013 case EPERM: 1014 errx(1, "%s: permission denied", cmd); 1015 break; 1016 1017 default: 1018 err(1, "%s", cmd); 1019 } 1020} 1021 1022/* 1023 * Print a value a la the %b format of the kernel's printf 1024 */ 1025void 1026printb(const char *s, unsigned v, const char *bits) 1027{ 1028 int i, any = 0; 1029 char c; 1030 1031 if (bits && *bits == 8) 1032 printf("%s=%o", s, v); 1033 else 1034 printf("%s=%x", s, v); 1035 bits++; 1036 if (bits) { 1037 putchar('<'); 1038 while ((i = *bits++) != '\0') { 1039 if (v & (1 << (i-1))) { 1040 if (any) 1041 putchar(','); 1042 any = 1; 1043 for (; (c = *bits) > 32; bits++) 1044 putchar(c); 1045 } else 1046 for (; *bits > 32; bits++) 1047 ; 1048 } 1049 putchar('>'); 1050 } 1051} 1052 1053void 1054ifmaybeload(const char *name) 1055{ 1056#define MOD_PREFIX_LEN 3 /* "if_" */ 1057 struct module_stat mstat; 1058 int fileid, modid; 1059 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp; 1060 const char *cp; 1061 1062 /* loading suppressed by the user */ 1063 if (noload) 1064 return; 1065 1066 /* trim the interface number off the end */ 1067 strlcpy(ifname, name, sizeof(ifname)); 1068 for (dp = ifname; *dp != 0; dp++) 1069 if (isdigit(*dp)) { 1070 *dp = 0; 1071 break; 1072 } 1073 1074 /* turn interface and unit into module name */ 1075 strcpy(ifkind, "if_"); 1076 strlcpy(ifkind + MOD_PREFIX_LEN, ifname, 1077 sizeof(ifkind) - MOD_PREFIX_LEN); 1078 1079 /* scan files in kernel */ 1080 mstat.version = sizeof(struct module_stat); 1081 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { 1082 /* scan modules in file */ 1083 for (modid = kldfirstmod(fileid); modid > 0; 1084 modid = modfnext(modid)) { 1085 if (modstat(modid, &mstat) < 0) 1086 continue; 1087 /* strip bus name if present */ 1088 if ((cp = strchr(mstat.name, '/')) != NULL) { 1089 cp++; 1090 } else { 1091 cp = mstat.name; 1092 } 1093 /* already loaded? */ 1094 if (strncmp(ifname, cp, strlen(ifname) + 1) == 0 || 1095 strncmp(ifkind, cp, strlen(ifkind) + 1) == 0) 1096 return; 1097 } 1098 } 1099 1100 /* not present, we should try to load it */ 1101 kldload(ifkind); 1102} 1103 1104static struct cmd basic_cmds[] = { 1105 DEF_CMD("up", IFF_UP, setifflags), 1106 DEF_CMD("down", -IFF_UP, setifflags), 1107 DEF_CMD("arp", -IFF_NOARP, setifflags), 1108 DEF_CMD("-arp", IFF_NOARP, setifflags), 1109 DEF_CMD("debug", IFF_DEBUG, setifflags), 1110 DEF_CMD("-debug", -IFF_DEBUG, setifflags), 1111 DEF_CMD_ARG("description", setifdescr), 1112 DEF_CMD_ARG("descr", setifdescr), 1113 DEF_CMD("-description", 0, unsetifdescr), 1114 DEF_CMD("-descr", 0, unsetifdescr), 1115 DEF_CMD("promisc", IFF_PPROMISC, setifflags), 1116 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags), 1117 DEF_CMD("add", IFF_UP, notealias), 1118 DEF_CMD("alias", IFF_UP, notealias), 1119 DEF_CMD("-alias", -IFF_UP, notealias), 1120 DEF_CMD("delete", -IFF_UP, notealias), 1121 DEF_CMD("remove", -IFF_UP, notealias), 1122#ifdef notdef 1123#define EN_SWABIPS 0x1000 1124 DEF_CMD("swabips", EN_SWABIPS, setifflags), 1125 DEF_CMD("-swabips", -EN_SWABIPS, setifflags), 1126#endif 1127 DEF_CMD_ARG("netmask", setifnetmask), 1128 DEF_CMD_ARG("metric", setifmetric), 1129 DEF_CMD_ARG("broadcast", setifbroadaddr), 1130 DEF_CMD_ARG("ipdst", setifipdst), 1131 DEF_CMD_ARG2("tunnel", settunnel), 1132 DEF_CMD("-tunnel", 0, deletetunnel), 1133 DEF_CMD("deletetunnel", 0, deletetunnel), 1134 DEF_CMD_ARG("vnet", setifvnet), 1135 DEF_CMD_ARG("-vnet", setifrvnet), 1136 DEF_CMD("link0", IFF_LINK0, setifflags), 1137 DEF_CMD("-link0", -IFF_LINK0, setifflags), 1138 DEF_CMD("link1", IFF_LINK1, setifflags), 1139 DEF_CMD("-link1", -IFF_LINK1, setifflags), 1140 DEF_CMD("link2", IFF_LINK2, setifflags), 1141 DEF_CMD("-link2", -IFF_LINK2, setifflags), 1142 DEF_CMD("monitor", IFF_MONITOR, setifflags), 1143 DEF_CMD("-monitor", -IFF_MONITOR, setifflags), 1144 DEF_CMD("staticarp", IFF_STATICARP, setifflags), 1145 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags), 1146 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap), 1147 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap), 1148 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap), 1149 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap), 1150 DEF_CMD("netcons", IFCAP_NETCONS, setifcap), 1151 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap), 1152 DEF_CMD("polling", IFCAP_POLLING, setifcap), 1153 DEF_CMD("-polling", -IFCAP_POLLING, setifcap), 1154 DEF_CMD("tso", IFCAP_TSO, setifcap), 1155 DEF_CMD("-tso", -IFCAP_TSO, setifcap), 1156 DEF_CMD("lro", IFCAP_LRO, setifcap), 1157 DEF_CMD("-lro", -IFCAP_LRO, setifcap), 1158 DEF_CMD("wol", IFCAP_WOL, setifcap), 1159 DEF_CMD("-wol", -IFCAP_WOL, setifcap), 1160 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap), 1161 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap), 1162 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap), 1163 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap), 1164 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap), 1165 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap), 1166 DEF_CMD("normal", -IFF_LINK0, setifflags), 1167 DEF_CMD("compress", IFF_LINK0, setifflags), 1168 DEF_CMD("noicmp", IFF_LINK1, setifflags), 1169 DEF_CMD_ARG("mtu", setifmtu), 1170 DEF_CMD_ARG("name", setifname), 1171}; 1172 1173static __constructor void 1174ifconfig_ctor(void) 1175{ 1176#define N(a) (sizeof(a) / sizeof(a[0])) 1177 size_t i; 1178 1179 for (i = 0; i < N(basic_cmds); i++) 1180 cmd_register(&basic_cmds[i]); 1181#undef N 1182}
|