1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 static const char copyright[] = 32 "@(#) Copyright (c) 1983, 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34 #endif /* not lint */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94"; 39 #endif 40 static const char rcsid[] = 41 "$FreeBSD$"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/ioctl.h> 46 #include <sys/socket.h> 47 #include <sys/time.h> 48 #include <sys/module.h> 49 #include <sys/linker.h> 50 #include <sys/queue.h> 51 52 #include <net/ethernet.h> 53 #include <net/if.h> 54 #include <net/if_dl.h> 55 #include <net/if_types.h> 56 #include <net/route.h> 57 58 /* IP */ 59 #include <netinet/in.h> 60 #include <netinet/in_var.h> 61 #include <arpa/inet.h> 62 #include <netdb.h> 63 64 #include <ifaddrs.h> 65 #include <ctype.h> 66 #include <err.h> 67 #include <errno.h> 68 #include <fcntl.h> 69 #ifdef JAIL 70 #include <jail.h> 71 #endif 72 #include <stdio.h> 73 #include <stdlib.h> 74 #include <string.h> 75 #include <unistd.h> 76 77 #include "ifconfig.h" 78 79 /* 80 * Since "struct ifreq" is composed of various union members, callers 81 * should pay special attention to interpret the value. 82 * (.e.g. little/big endian difference in the structure.) 83 */ 84 struct ifreq ifr; 85 86 char name[IFNAMSIZ]; 87 char *descr = NULL; 88 size_t descrlen = 64; 89 int setaddr; 90 int setmask; 91 int doalias; 92 int clearaddr; 93 int newaddr = 1; 94 int verbose; 95 int noload; 96 97 int supmedia = 0; 98 int printkeys = 0; /* Print keying material for interfaces. */ 99 100 static int ifconfig(int argc, char *const *argv, int iscreate, 101 const struct afswtch *afp); 102 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl, 103 struct ifaddrs *ifa); 104 static void tunnel_status(int s); 105 static void usage(void); 106 107 static struct afswtch *af_getbyname(const char *name); 108 static struct afswtch *af_getbyfamily(int af); 109 static void af_other_status(int); 110 111 static struct option *opts = NULL; 112 113 struct ifa_order_elt { 114 int if_order; 115 int af_orders[255]; 116 struct ifaddrs *ifa; 117 TAILQ_ENTRY(ifa_order_elt) link; 118 }; 119 120 TAILQ_HEAD(ifa_queue, ifa_order_elt); 121 122 void 123 opt_register(struct option *p) 124 { 125 p->next = opts; 126 opts = p; 127 } 128 129 static void 130 usage(void) 131 { 132 char options[1024]; 133 struct option *p; 134 135 /* XXX not right but close enough for now */ 136 options[0] = '\0'; 137 for (p = opts; p != NULL; p = p->next) { 138 strlcat(options, p->opt_usage, sizeof(options)); 139 strlcat(options, " ", sizeof(options)); 140 } 141 142 fprintf(stderr, 143 "usage: ifconfig %sinterface address_family [address [dest_address]]\n" 144 " [parameters]\n" 145 " ifconfig interface create\n" 146 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n" 147 " ifconfig -l [-d] [-u] [address_family]\n" 148 " ifconfig %s[-d] [-m] [-u] [-v]\n", 149 options, options, options); 150 exit(1); 151 } 152 153 #define ORDERS_SIZE(x) sizeof(x) / sizeof(x[0]) 154 155 static int 156 calcorders(struct ifaddrs *ifa, struct ifa_queue *q) 157 { 158 struct ifaddrs *prev; 159 struct ifa_order_elt *cur; 160 unsigned int ord, af, ifa_ord; 161 162 prev = NULL; 163 cur = NULL; 164 ord = 0; 165 ifa_ord = 0; 166 167 while (ifa != NULL) { 168 if (prev == NULL || 169 strcmp(ifa->ifa_name, prev->ifa_name) != 0) { 170 cur = calloc(1, sizeof(*cur)); 171 172 if (cur == NULL) 173 return (-1); 174 175 TAILQ_INSERT_TAIL(q, cur, link); 176 cur->if_order = ifa_ord ++; 177 cur->ifa = ifa; 178 ord = 0; 179 } 180 181 if (ifa->ifa_addr) { 182 af = ifa->ifa_addr->sa_family; 183 184 if (af < ORDERS_SIZE(cur->af_orders) && 185 cur->af_orders[af] == 0) 186 cur->af_orders[af] = ++ord; 187 } 188 prev = ifa; 189 ifa = ifa->ifa_next; 190 } 191 192 return (0); 193 } 194 195 static int 196 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q) 197 { 198 struct ifa_order_elt *cur, *e1, *e2; 199 unsigned int af1, af2; 200 int ret; 201 202 e1 = e2 = NULL; 203 204 ret = strcmp(a->ifa_name, b->ifa_name); 205 if (ret != 0) { 206 TAILQ_FOREACH(cur, q, link) { 207 if (e1 && e2) 208 break; 209 210 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) 211 e1 = cur; 212 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0) 213 e2 = cur; 214 } 215 216 if (!e1 || !e2) 217 return (0); 218 else 219 return (e1->if_order - e2->if_order); 220 221 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) { 222 TAILQ_FOREACH(cur, q, link) { 223 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) { 224 e1 = cur; 225 break; 226 } 227 } 228 229 if (!e1) 230 return (0); 231 232 af1 = a->ifa_addr->sa_family; 233 af2 = b->ifa_addr->sa_family; 234 235 if (af1 < ORDERS_SIZE(e1->af_orders) && 236 af2 < ORDERS_SIZE(e1->af_orders)) 237 return (e1->af_orders[af1] - e1->af_orders[af2]); 238 } 239 240 return (0); 241 } 242 243 #undef ORDERS_SIZE 244 245 static struct ifaddrs * 246 sortifaddrs(struct ifaddrs *list, 247 int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *), 248 struct ifa_queue *q) 249 { 250 struct ifaddrs *right, *temp, *last, *result, *next, *tail; 251 252 right = list; 253 temp = list; 254 last = list; 255 result = NULL; 256 next = NULL; 257 tail = NULL; 258 259 if (!list || !list->ifa_next) 260 return (list); 261 262 while (temp && temp->ifa_next) { 263 last = right; 264 right = right->ifa_next; 265 temp = temp->ifa_next->ifa_next; 266 } 267 268 last->ifa_next = NULL; 269 270 list = sortifaddrs(list, compare, q); 271 right = sortifaddrs(right, compare, q); 272 273 while (list || right) { 274 275 if (!right) { 276 next = list; 277 list = list->ifa_next; 278 } else if (!list) { 279 next = right; 280 right = right->ifa_next; 281 } else if (compare(list, right, q) <= 0) { 282 next = list; 283 list = list->ifa_next; 284 } else { 285 next = right; 286 right = right->ifa_next; 287 } 288 289 if (!result) 290 result = next; 291 else 292 tail->ifa_next = next; 293 294 tail = next; 295 } 296 297 return (result); 298 } 299 300 int 301 main(int argc, char *argv[]) 302 { 303 int c, all, namesonly, downonly, uponly; 304 const struct afswtch *afp = NULL; 305 int ifindex; 306 struct ifaddrs *ifap, *sifap, *ifa; 307 struct ifreq paifr; 308 const struct sockaddr_dl *sdl; 309 char options[1024], *cp, *namecp = NULL; 310 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q); 311 struct ifa_order_elt *cur, *tmp; 312 const char *ifname; 313 struct option *p; 314 size_t iflen; 315 316 all = downonly = uponly = namesonly = noload = verbose = 0; 317 318 /* Parse leading line options */ 319 strlcpy(options, "adklmnuv", sizeof(options)); 320 for (p = opts; p != NULL; p = p->next) 321 strlcat(options, p->opt, sizeof(options)); 322 while ((c = getopt(argc, argv, options)) != -1) { 323 switch (c) { 324 case 'a': /* scan all interfaces */ 325 all++; 326 break; 327 case 'd': /* restrict scan to "down" interfaces */ 328 downonly++; 329 break; 330 case 'k': 331 printkeys++; 332 break; 333 case 'l': /* scan interface names only */ 334 namesonly++; 335 break; 336 case 'm': /* show media choices in status */ 337 supmedia = 1; 338 break; 339 case 'n': /* suppress module loading */ 340 noload++; 341 break; 342 case 'u': /* restrict scan to "up" interfaces */ 343 uponly++; 344 break; 345 case 'v': 346 verbose++; 347 break; 348 default: 349 for (p = opts; p != NULL; p = p->next) 350 if (p->opt[0] == c) { 351 p->cb(optarg); 352 break; 353 } 354 if (p == NULL) 355 usage(); 356 break; 357 } 358 } 359 argc -= optind; 360 argv += optind; 361 362 /* -l cannot be used with -a or -m */ 363 if (namesonly && (all || supmedia)) 364 usage(); 365 366 /* nonsense.. */ 367 if (uponly && downonly) 368 usage(); 369 370 /* no arguments is equivalent to '-a' */ 371 if (!namesonly && argc < 1) 372 all = 1; 373 374 /* -a and -l allow an address family arg to limit the output */ 375 if (all || namesonly) { 376 if (argc > 1) 377 usage(); 378 379 ifname = NULL; 380 ifindex = 0; 381 if (argc == 1) { 382 afp = af_getbyname(*argv); 383 if (afp == NULL) { 384 warnx("Address family '%s' unknown.", *argv); 385 usage(); 386 } 387 if (afp->af_name != NULL) 388 argc--, argv++; 389 /* leave with afp non-zero */ 390 } 391 } else { 392 /* not listing, need an argument */ 393 if (argc < 1) 394 usage(); 395 396 ifname = *argv; 397 argc--, argv++; 398 399 /* check and maybe load support for this interface */ 400 ifmaybeload(ifname); 401 402 ifindex = if_nametoindex(ifname); 403 if (ifindex == 0) { 404 /* 405 * NOTE: We must special-case the `create' command 406 * right here as we would otherwise fail when trying 407 * to find the interface. 408 */ 409 if (argc > 0 && (strcmp(argv[0], "create") == 0 || 410 strcmp(argv[0], "plumb") == 0)) { 411 iflen = strlcpy(name, ifname, sizeof(name)); 412 if (iflen >= sizeof(name)) 413 errx(1, "%s: cloning name too long", 414 ifname); 415 ifconfig(argc, argv, 1, NULL); 416 exit(0); 417 } 418 #ifdef JAIL 419 /* 420 * NOTE: We have to special-case the `-vnet' command 421 * right here as we would otherwise fail when trying 422 * to find the interface as it lives in another vnet. 423 */ 424 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) { 425 iflen = strlcpy(name, ifname, sizeof(name)); 426 if (iflen >= sizeof(name)) 427 errx(1, "%s: interface name too long", 428 ifname); 429 ifconfig(argc, argv, 0, NULL); 430 exit(0); 431 } 432 #endif 433 errx(1, "interface %s does not exist", ifname); 434 } 435 } 436 437 /* Check for address family */ 438 if (argc > 0) { 439 afp = af_getbyname(*argv); 440 if (afp != NULL) 441 argc--, argv++; 442 } 443 444 if (getifaddrs(&ifap) != 0) 445 err(EXIT_FAILURE, "getifaddrs"); 446 447 cp = NULL; 448 449 if (calcorders(ifap, &q) != 0) 450 err(EXIT_FAILURE, "calcorders"); 451 452 sifap = sortifaddrs(ifap, cmpifaddrs, &q); 453 454 TAILQ_FOREACH_SAFE(cur, &q, link, tmp) 455 free(cur); 456 457 ifindex = 0; 458 for (ifa = sifap; ifa; ifa = ifa->ifa_next) { 459 memset(&paifr, 0, sizeof(paifr)); 460 strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name)); 461 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) { 462 memcpy(&paifr.ifr_addr, ifa->ifa_addr, 463 ifa->ifa_addr->sa_len); 464 } 465 466 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0) 467 continue; 468 if (ifa->ifa_addr->sa_family == AF_LINK) 469 sdl = (const struct sockaddr_dl *) ifa->ifa_addr; 470 else 471 sdl = NULL; 472 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly) 473 continue; 474 iflen = strlcpy(name, ifa->ifa_name, sizeof(name)); 475 if (iflen >= sizeof(name)) { 476 warnx("%s: interface name too long, skipping", 477 ifa->ifa_name); 478 continue; 479 } 480 cp = ifa->ifa_name; 481 482 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0) 483 continue; 484 if (downonly && (ifa->ifa_flags & IFF_UP) != 0) 485 continue; 486 if (uponly && (ifa->ifa_flags & IFF_UP) == 0) 487 continue; 488 /* 489 * Are we just listing the interfaces? 490 */ 491 if (namesonly) { 492 if (namecp == cp) 493 continue; 494 if (afp != NULL) { 495 /* special case for "ether" address family */ 496 if (!strcmp(afp->af_name, "ether")) { 497 if (sdl == NULL || 498 (sdl->sdl_type != IFT_ETHER && 499 sdl->sdl_type != IFT_L2VLAN && 500 sdl->sdl_type != IFT_BRIDGE) || 501 sdl->sdl_alen != ETHER_ADDR_LEN) 502 continue; 503 } else { 504 if (ifa->ifa_addr->sa_family 505 != afp->af_af) 506 continue; 507 } 508 } 509 namecp = cp; 510 ifindex++; 511 if (ifindex > 1) 512 printf(" "); 513 fputs(name, stdout); 514 continue; 515 } 516 ifindex++; 517 518 if (argc > 0) 519 ifconfig(argc, argv, 0, afp); 520 else 521 status(afp, sdl, ifa); 522 } 523 if (namesonly) 524 printf("\n"); 525 freeifaddrs(ifap); 526 527 exit(0); 528 } 529 530 static struct afswtch *afs = NULL; 531 532 void 533 af_register(struct afswtch *p) 534 { 535 p->af_next = afs; 536 afs = p; 537 } 538 539 static struct afswtch * 540 af_getbyname(const char *name) 541 { 542 struct afswtch *afp; 543 544 for (afp = afs; afp != NULL; afp = afp->af_next) 545 if (strcmp(afp->af_name, name) == 0) 546 return afp; 547 return NULL; 548 } 549 550 static struct afswtch * 551 af_getbyfamily(int af) 552 { 553 struct afswtch *afp; 554 555 for (afp = afs; afp != NULL; afp = afp->af_next) 556 if (afp->af_af == af) 557 return afp; 558 return NULL; 559 } 560 561 static void 562 af_other_status(int s) 563 { 564 struct afswtch *afp; 565 uint8_t afmask[howmany(AF_MAX, NBBY)]; 566 567 memset(afmask, 0, sizeof(afmask)); 568 for (afp = afs; afp != NULL; afp = afp->af_next) { 569 if (afp->af_other_status == NULL) 570 continue; 571 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) 572 continue; 573 afp->af_other_status(s); 574 setbit(afmask, afp->af_af); 575 } 576 } 577 578 static void 579 af_all_tunnel_status(int s) 580 { 581 struct afswtch *afp; 582 uint8_t afmask[howmany(AF_MAX, NBBY)]; 583 584 memset(afmask, 0, sizeof(afmask)); 585 for (afp = afs; afp != NULL; afp = afp->af_next) { 586 if (afp->af_status_tunnel == NULL) 587 continue; 588 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) 589 continue; 590 afp->af_status_tunnel(s); 591 setbit(afmask, afp->af_af); 592 } 593 } 594 595 static struct cmd *cmds = NULL; 596 597 void 598 cmd_register(struct cmd *p) 599 { 600 p->c_next = cmds; 601 cmds = p; 602 } 603 604 static const struct cmd * 605 cmd_lookup(const char *name, int iscreate) 606 { 607 #define N(a) (sizeof(a)/sizeof(a[0])) 608 const struct cmd *p; 609 610 for (p = cmds; p != NULL; p = p->c_next) 611 if (strcmp(name, p->c_name) == 0) { 612 if (iscreate) { 613 if (p->c_iscloneop) 614 return p; 615 } else { 616 if (!p->c_iscloneop) 617 return p; 618 } 619 } 620 return NULL; 621 #undef N 622 } 623 624 struct callback { 625 callback_func *cb_func; 626 void *cb_arg; 627 struct callback *cb_next; 628 }; 629 static struct callback *callbacks = NULL; 630 631 void 632 callback_register(callback_func *func, void *arg) 633 { 634 struct callback *cb; 635 636 cb = malloc(sizeof(struct callback)); 637 if (cb == NULL) 638 errx(1, "unable to allocate memory for callback"); 639 cb->cb_func = func; 640 cb->cb_arg = arg; 641 cb->cb_next = callbacks; 642 callbacks = cb; 643 } 644 645 /* specially-handled commands */ 646 static void setifaddr(const char *, int, int, const struct afswtch *); 647 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr); 648 649 static void setifdstaddr(const char *, int, int, const struct afswtch *); 650 static const struct cmd setifdstaddr_cmd = 651 DEF_CMD("ifdstaddr", 0, setifdstaddr); 652 653 static int 654 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp) 655 { 656 const struct afswtch *afp, *nafp; 657 const struct cmd *p; 658 struct callback *cb; 659 int s; 660 661 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 662 afp = NULL; 663 if (uafp != NULL) 664 afp = uafp; 665 /* 666 * This is the historical "accident" allowing users to configure IPv4 667 * addresses without the "inet" keyword which while a nice feature has 668 * proven to complicate other things. We cannot remove this but only 669 * make sure we will never have a similar implicit default for IPv6 or 670 * any other address familiy. We need a fallback though for 671 * ifconfig IF up/down etc. to work without INET support as people 672 * never used ifconfig IF link up/down, etc. either. 673 */ 674 #ifndef RESCUE 675 #ifdef INET 676 if (afp == NULL && feature_present("inet")) 677 afp = af_getbyname("inet"); 678 #endif 679 #endif 680 if (afp == NULL) 681 afp = af_getbyname("link"); 682 if (afp == NULL) { 683 warnx("Please specify an address_family."); 684 usage(); 685 } 686 top: 687 ifr.ifr_addr.sa_family = 688 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ? 689 AF_LOCAL : afp->af_af; 690 691 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 && 692 (uafp != NULL || errno != EAFNOSUPPORT || 693 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)) 694 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family); 695 696 while (argc > 0) { 697 p = cmd_lookup(*argv, iscreate); 698 if (iscreate && p == NULL) { 699 /* 700 * Push the clone create callback so the new 701 * device is created and can be used for any 702 * remaining arguments. 703 */ 704 cb = callbacks; 705 if (cb == NULL) 706 errx(1, "internal error, no callback"); 707 callbacks = cb->cb_next; 708 cb->cb_func(s, cb->cb_arg); 709 iscreate = 0; 710 /* 711 * Handle any address family spec that 712 * immediately follows and potentially 713 * recreate the socket. 714 */ 715 nafp = af_getbyname(*argv); 716 if (nafp != NULL) { 717 argc--, argv++; 718 if (nafp != afp) { 719 close(s); 720 afp = nafp; 721 goto top; 722 } 723 } 724 /* 725 * Look for a normal parameter. 726 */ 727 continue; 728 } 729 if (p == NULL) { 730 /* 731 * Not a recognized command, choose between setting 732 * the interface address and the dst address. 733 */ 734 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd); 735 } 736 if (p->c_u.c_func || p->c_u.c_func2) { 737 if (p->c_parameter == NEXTARG) { 738 if (argv[1] == NULL) 739 errx(1, "'%s' requires argument", 740 p->c_name); 741 p->c_u.c_func(argv[1], 0, s, afp); 742 argc--, argv++; 743 } else if (p->c_parameter == OPTARG) { 744 p->c_u.c_func(argv[1], 0, s, afp); 745 if (argv[1] != NULL) 746 argc--, argv++; 747 } else if (p->c_parameter == NEXTARG2) { 748 if (argc < 3) 749 errx(1, "'%s' requires 2 arguments", 750 p->c_name); 751 p->c_u.c_func2(argv[1], argv[2], s, afp); 752 argc -= 2, argv += 2; 753 } else 754 p->c_u.c_func(*argv, p->c_parameter, s, afp); 755 } 756 argc--, argv++; 757 } 758 759 /* 760 * Do any post argument processing required by the address family. 761 */ 762 if (afp->af_postproc != NULL) 763 afp->af_postproc(s, afp); 764 /* 765 * Do deferred callbacks registered while processing 766 * command-line arguments. 767 */ 768 for (cb = callbacks; cb != NULL; cb = cb->cb_next) 769 cb->cb_func(s, cb->cb_arg); 770 /* 771 * Do deferred operations. 772 */ 773 if (clearaddr) { 774 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) { 775 warnx("interface %s cannot change %s addresses!", 776 name, afp->af_name); 777 clearaddr = 0; 778 } 779 } 780 if (clearaddr) { 781 int ret; 782 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name); 783 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq); 784 if (ret < 0) { 785 if (errno == EADDRNOTAVAIL && (doalias >= 0)) { 786 /* means no previous address for interface */ 787 } else 788 Perror("ioctl (SIOCDIFADDR)"); 789 } 790 } 791 if (newaddr) { 792 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) { 793 warnx("interface %s cannot change %s addresses!", 794 name, afp->af_name); 795 newaddr = 0; 796 } 797 } 798 if (newaddr && (setaddr || setmask)) { 799 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name); 800 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0) 801 Perror("ioctl (SIOCAIFADDR)"); 802 } 803 804 close(s); 805 return(0); 806 } 807 808 /*ARGSUSED*/ 809 static void 810 setifaddr(const char *addr, int param, int s, const struct afswtch *afp) 811 { 812 if (afp->af_getaddr == NULL) 813 return; 814 /* 815 * Delay the ioctl to set the interface addr until flags are all set. 816 * The address interpretation may depend on the flags, 817 * and the flags may change when the address is set. 818 */ 819 setaddr++; 820 if (doalias == 0 && afp->af_af != AF_LINK) 821 clearaddr = 1; 822 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR)); 823 } 824 825 static void 826 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp) 827 { 828 struct addrinfo *srcres, *dstres; 829 int ecode; 830 831 if (afp->af_settunnel == NULL) { 832 warn("address family %s does not support tunnel setup", 833 afp->af_name); 834 return; 835 } 836 837 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0) 838 errx(1, "error in parsing address string: %s", 839 gai_strerror(ecode)); 840 841 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0) 842 errx(1, "error in parsing address string: %s", 843 gai_strerror(ecode)); 844 845 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family) 846 errx(1, 847 "source and destination address families do not match"); 848 849 afp->af_settunnel(s, srcres, dstres); 850 851 freeaddrinfo(srcres); 852 freeaddrinfo(dstres); 853 } 854 855 /* ARGSUSED */ 856 static void 857 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp) 858 { 859 860 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0) 861 err(1, "SIOCDIFPHYADDR"); 862 } 863 864 #ifdef JAIL 865 static void 866 setifvnet(const char *jname, int dummy __unused, int s, 867 const struct afswtch *afp) 868 { 869 struct ifreq my_ifr; 870 871 memcpy(&my_ifr, &ifr, sizeof(my_ifr)); 872 my_ifr.ifr_jid = jail_getid(jname); 873 if (my_ifr.ifr_jid < 0) 874 errx(1, "%s", jail_errmsg); 875 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0) 876 err(1, "SIOCSIFVNET"); 877 } 878 879 static void 880 setifrvnet(const char *jname, int dummy __unused, int s, 881 const struct afswtch *afp) 882 { 883 struct ifreq my_ifr; 884 885 memcpy(&my_ifr, &ifr, sizeof(my_ifr)); 886 my_ifr.ifr_jid = jail_getid(jname); 887 if (my_ifr.ifr_jid < 0) 888 errx(1, "%s", jail_errmsg); 889 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0) 890 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name); 891 } 892 #endif 893 894 static void 895 setifnetmask(const char *addr, int dummy __unused, int s, 896 const struct afswtch *afp) 897 { 898 if (afp->af_getaddr != NULL) { 899 setmask++; 900 afp->af_getaddr(addr, MASK); 901 } 902 } 903 904 static void 905 setifbroadaddr(const char *addr, int dummy __unused, int s, 906 const struct afswtch *afp) 907 { 908 if (afp->af_getaddr != NULL) 909 afp->af_getaddr(addr, DSTADDR); 910 } 911 912 static void 913 notealias(const char *addr, int param, int s, const struct afswtch *afp) 914 { 915 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr)) 916 if (setaddr && doalias == 0 && param < 0) 917 if (afp->af_addreq != NULL && afp->af_ridreq != NULL) 918 bcopy((caddr_t)rqtosa(af_addreq), 919 (caddr_t)rqtosa(af_ridreq), 920 rqtosa(af_addreq)->sa_len); 921 doalias = param; 922 if (param < 0) { 923 clearaddr = 1; 924 newaddr = 0; 925 } else 926 clearaddr = 0; 927 #undef rqtosa 928 } 929 930 /*ARGSUSED*/ 931 static void 932 setifdstaddr(const char *addr, int param __unused, int s, 933 const struct afswtch *afp) 934 { 935 if (afp->af_getaddr != NULL) 936 afp->af_getaddr(addr, DSTADDR); 937 } 938 939 /* 940 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion 941 * of the ifreq structure, which may confuse other parts of ifconfig. 942 * Make a private copy so we can avoid that. 943 */ 944 static void 945 setifflags(const char *vname, int value, int s, const struct afswtch *afp) 946 { 947 struct ifreq my_ifr; 948 int flags; 949 950 memset(&my_ifr, 0, sizeof(my_ifr)); 951 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name)); 952 953 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) { 954 Perror("ioctl (SIOCGIFFLAGS)"); 955 exit(1); 956 } 957 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16); 958 959 if (value < 0) { 960 value = -value; 961 flags &= ~value; 962 } else 963 flags |= value; 964 my_ifr.ifr_flags = flags & 0xffff; 965 my_ifr.ifr_flagshigh = flags >> 16; 966 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0) 967 Perror(vname); 968 } 969 970 void 971 setifcap(const char *vname, int value, int s, const struct afswtch *afp) 972 { 973 int flags; 974 975 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) { 976 Perror("ioctl (SIOCGIFCAP)"); 977 exit(1); 978 } 979 flags = ifr.ifr_curcap; 980 if (value < 0) { 981 value = -value; 982 flags &= ~value; 983 } else 984 flags |= value; 985 flags &= ifr.ifr_reqcap; 986 ifr.ifr_reqcap = flags; 987 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0) 988 Perror(vname); 989 } 990 991 static void 992 setifmetric(const char *val, int dummy __unused, int s, 993 const struct afswtch *afp) 994 { 995 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 996 ifr.ifr_metric = atoi(val); 997 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0) 998 err(1, "ioctl SIOCSIFMETRIC (set metric)"); 999 } 1000 1001 static void 1002 setifmtu(const char *val, int dummy __unused, int s, 1003 const struct afswtch *afp) 1004 { 1005 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1006 ifr.ifr_mtu = atoi(val); 1007 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0) 1008 err(1, "ioctl SIOCSIFMTU (set mtu)"); 1009 } 1010 1011 static void 1012 setifname(const char *val, int dummy __unused, int s, 1013 const struct afswtch *afp) 1014 { 1015 char *newname; 1016 1017 newname = strdup(val); 1018 if (newname == NULL) 1019 err(1, "no memory to set ifname"); 1020 ifr.ifr_data = newname; 1021 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) { 1022 free(newname); 1023 err(1, "ioctl SIOCSIFNAME (set name)"); 1024 } 1025 strlcpy(name, newname, sizeof(name)); 1026 free(newname); 1027 } 1028 1029 /* ARGSUSED */ 1030 static void 1031 setifdescr(const char *val, int dummy __unused, int s, 1032 const struct afswtch *afp) 1033 { 1034 char *newdescr; 1035 1036 ifr.ifr_buffer.length = strlen(val) + 1; 1037 if (ifr.ifr_buffer.length == 1) { 1038 ifr.ifr_buffer.buffer = newdescr = NULL; 1039 ifr.ifr_buffer.length = 0; 1040 } else { 1041 newdescr = strdup(val); 1042 ifr.ifr_buffer.buffer = newdescr; 1043 if (newdescr == NULL) { 1044 warn("no memory to set ifdescr"); 1045 return; 1046 } 1047 } 1048 1049 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) 1050 err(1, "ioctl SIOCSIFDESCR (set descr)"); 1051 1052 free(newdescr); 1053 } 1054 1055 /* ARGSUSED */ 1056 static void 1057 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp) 1058 { 1059 1060 setifdescr("", 0, s, 0); 1061 } 1062 1063 #define IFFBITS \ 1064 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \ 1065 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \ 1066 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP" 1067 1068 #define IFCAPBITS \ 1069 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \ 1070 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \ 1071 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \ 1072 "\26RXCSUM_IPV6\27TXCSUM_IPV6" 1073 1074 /* 1075 * Print the status of the interface. If an address family was 1076 * specified, show only it; otherwise, show them all. 1077 */ 1078 static void 1079 status(const struct afswtch *afp, const struct sockaddr_dl *sdl, 1080 struct ifaddrs *ifa) 1081 { 1082 struct ifaddrs *ift; 1083 int allfamilies, s; 1084 struct ifstat ifs; 1085 1086 if (afp == NULL) { 1087 allfamilies = 1; 1088 ifr.ifr_addr.sa_family = AF_LOCAL; 1089 } else { 1090 allfamilies = 0; 1091 ifr.ifr_addr.sa_family = 1092 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af; 1093 } 1094 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1095 1096 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0); 1097 if (s < 0) 1098 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family); 1099 1100 printf("%s: ", name); 1101 printb("flags", ifa->ifa_flags, IFFBITS); 1102 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1) 1103 printf(" metric %d", ifr.ifr_metric); 1104 if (ioctl(s, SIOCGIFMTU, &ifr) != -1) 1105 printf(" mtu %d", ifr.ifr_mtu); 1106 putchar('\n'); 1107 1108 for (;;) { 1109 if ((descr = reallocf(descr, descrlen)) != NULL) { 1110 ifr.ifr_buffer.buffer = descr; 1111 ifr.ifr_buffer.length = descrlen; 1112 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) { 1113 if (ifr.ifr_buffer.buffer == descr) { 1114 if (strlen(descr) > 0) 1115 printf("\tdescription: %s\n", 1116 descr); 1117 } else if (ifr.ifr_buffer.length > descrlen) { 1118 descrlen = ifr.ifr_buffer.length; 1119 continue; 1120 } 1121 } 1122 } else 1123 warn("unable to allocate memory for interface" 1124 "description"); 1125 break; 1126 } 1127 1128 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) { 1129 if (ifr.ifr_curcap != 0) { 1130 printb("\toptions", ifr.ifr_curcap, IFCAPBITS); 1131 putchar('\n'); 1132 } 1133 if (supmedia && ifr.ifr_reqcap != 0) { 1134 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS); 1135 putchar('\n'); 1136 } 1137 } 1138 1139 tunnel_status(s); 1140 1141 for (ift = ifa; ift != NULL; ift = ift->ifa_next) { 1142 if (ift->ifa_addr == NULL) 1143 continue; 1144 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0) 1145 continue; 1146 if (allfamilies) { 1147 const struct afswtch *p; 1148 p = af_getbyfamily(ift->ifa_addr->sa_family); 1149 if (p != NULL && p->af_status != NULL) 1150 p->af_status(s, ift); 1151 } else if (afp->af_af == ift->ifa_addr->sa_family) 1152 afp->af_status(s, ift); 1153 } 1154 #if 0 1155 if (allfamilies || afp->af_af == AF_LINK) { 1156 const struct afswtch *lafp; 1157 1158 /* 1159 * Hack; the link level address is received separately 1160 * from the routing information so any address is not 1161 * handled above. Cobble together an entry and invoke 1162 * the status method specially. 1163 */ 1164 lafp = af_getbyname("lladdr"); 1165 if (lafp != NULL) { 1166 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl; 1167 lafp->af_status(s, &info); 1168 } 1169 } 1170 #endif 1171 if (allfamilies) 1172 af_other_status(s); 1173 else if (afp->af_other_status != NULL) 1174 afp->af_other_status(s); 1175 1176 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name); 1177 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) 1178 printf("%s", ifs.ascii); 1179 1180 if (verbose > 0) 1181 sfp_status(s, &ifr, verbose); 1182 1183 close(s); 1184 return; 1185 } 1186 1187 static void 1188 tunnel_status(int s) 1189 { 1190 af_all_tunnel_status(s); 1191 } 1192 1193 void 1194 Perror(const char *cmd) 1195 { 1196 switch (errno) { 1197 1198 case ENXIO: 1199 errx(1, "%s: no such interface", cmd); 1200 break; 1201 1202 case EPERM: 1203 errx(1, "%s: permission denied", cmd); 1204 break; 1205 1206 default: 1207 err(1, "%s", cmd); 1208 } 1209 } 1210 1211 /* 1212 * Print a value a la the %b format of the kernel's printf 1213 */ 1214 void 1215 printb(const char *s, unsigned v, const char *bits) 1216 { 1217 int i, any = 0; 1218 char c; 1219 1220 if (bits && *bits == 8) 1221 printf("%s=%o", s, v); 1222 else 1223 printf("%s=%x", s, v); 1224 bits++; 1225 if (bits) { 1226 putchar('<'); 1227 while ((i = *bits++) != '\0') { 1228 if (v & (1 << (i-1))) { 1229 if (any) 1230 putchar(','); 1231 any = 1; 1232 for (; (c = *bits) > 32; bits++) 1233 putchar(c); 1234 } else 1235 for (; *bits > 32; bits++) 1236 ; 1237 } 1238 putchar('>'); 1239 } 1240 } 1241 1242 void 1243 print_vhid(const struct ifaddrs *ifa, const char *s) 1244 { 1245 struct if_data *ifd; 1246 1247 if (ifa->ifa_data == NULL) 1248 return; 1249 1250 ifd = ifa->ifa_data; 1251 if (ifd->ifi_vhid == 0) 1252 return; 1253 1254 printf("vhid %d ", ifd->ifi_vhid); 1255 } 1256 1257 void 1258 ifmaybeload(const char *name) 1259 { 1260 #define MOD_PREFIX_LEN 3 /* "if_" */ 1261 struct module_stat mstat; 1262 int fileid, modid; 1263 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp; 1264 const char *cp; 1265 1266 /* loading suppressed by the user */ 1267 if (noload) 1268 return; 1269 1270 /* trim the interface number off the end */ 1271 strlcpy(ifname, name, sizeof(ifname)); 1272 for (dp = ifname; *dp != 0; dp++) 1273 if (isdigit(*dp)) { 1274 *dp = 0; 1275 break; 1276 } 1277 1278 /* turn interface and unit into module name */ 1279 strlcpy(ifkind, "if_", sizeof(ifkind)); 1280 strlcat(ifkind, ifname, sizeof(ifkind)); 1281 1282 /* scan files in kernel */ 1283 mstat.version = sizeof(struct module_stat); 1284 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { 1285 /* scan modules in file */ 1286 for (modid = kldfirstmod(fileid); modid > 0; 1287 modid = modfnext(modid)) { 1288 if (modstat(modid, &mstat) < 0) 1289 continue; 1290 /* strip bus name if present */ 1291 if ((cp = strchr(mstat.name, '/')) != NULL) { 1292 cp++; 1293 } else { 1294 cp = mstat.name; 1295 } 1296 /* already loaded? */ 1297 if (strcmp(ifname, cp) == 0 || 1298 strcmp(ifkind, cp) == 0) 1299 return; 1300 } 1301 } 1302 1303 /* not present, we should try to load it */ 1304 kldload(ifkind); 1305 } 1306 1307 static struct cmd basic_cmds[] = { 1308 DEF_CMD("up", IFF_UP, setifflags), 1309 DEF_CMD("down", -IFF_UP, setifflags), 1310 DEF_CMD("arp", -IFF_NOARP, setifflags), 1311 DEF_CMD("-arp", IFF_NOARP, setifflags), 1312 DEF_CMD("debug", IFF_DEBUG, setifflags), 1313 DEF_CMD("-debug", -IFF_DEBUG, setifflags), 1314 DEF_CMD_ARG("description", setifdescr), 1315 DEF_CMD_ARG("descr", setifdescr), 1316 DEF_CMD("-description", 0, unsetifdescr), 1317 DEF_CMD("-descr", 0, unsetifdescr), 1318 DEF_CMD("promisc", IFF_PPROMISC, setifflags), 1319 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags), 1320 DEF_CMD("add", IFF_UP, notealias), 1321 DEF_CMD("alias", IFF_UP, notealias), 1322 DEF_CMD("-alias", -IFF_UP, notealias), 1323 DEF_CMD("delete", -IFF_UP, notealias), 1324 DEF_CMD("remove", -IFF_UP, notealias), 1325 #ifdef notdef 1326 #define EN_SWABIPS 0x1000 1327 DEF_CMD("swabips", EN_SWABIPS, setifflags), 1328 DEF_CMD("-swabips", -EN_SWABIPS, setifflags), 1329 #endif 1330 DEF_CMD_ARG("netmask", setifnetmask), 1331 DEF_CMD_ARG("metric", setifmetric), 1332 DEF_CMD_ARG("broadcast", setifbroadaddr), 1333 DEF_CMD_ARG2("tunnel", settunnel), 1334 DEF_CMD("-tunnel", 0, deletetunnel), 1335 DEF_CMD("deletetunnel", 0, deletetunnel), 1336 #ifdef JAIL 1337 DEF_CMD_ARG("vnet", setifvnet), 1338 DEF_CMD_ARG("-vnet", setifrvnet), 1339 #endif 1340 DEF_CMD("link0", IFF_LINK0, setifflags), 1341 DEF_CMD("-link0", -IFF_LINK0, setifflags), 1342 DEF_CMD("link1", IFF_LINK1, setifflags), 1343 DEF_CMD("-link1", -IFF_LINK1, setifflags), 1344 DEF_CMD("link2", IFF_LINK2, setifflags), 1345 DEF_CMD("-link2", -IFF_LINK2, setifflags), 1346 DEF_CMD("monitor", IFF_MONITOR, setifflags), 1347 DEF_CMD("-monitor", -IFF_MONITOR, setifflags), 1348 DEF_CMD("staticarp", IFF_STATICARP, setifflags), 1349 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags), 1350 DEF_CMD("rxcsum6", IFCAP_RXCSUM_IPV6, setifcap), 1351 DEF_CMD("-rxcsum6", -IFCAP_RXCSUM_IPV6, setifcap), 1352 DEF_CMD("txcsum6", IFCAP_TXCSUM_IPV6, setifcap), 1353 DEF_CMD("-txcsum6", -IFCAP_TXCSUM_IPV6, setifcap), 1354 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap), 1355 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap), 1356 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap), 1357 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap), 1358 DEF_CMD("netcons", IFCAP_NETCONS, setifcap), 1359 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap), 1360 DEF_CMD("polling", IFCAP_POLLING, setifcap), 1361 DEF_CMD("-polling", -IFCAP_POLLING, setifcap), 1362 DEF_CMD("tso6", IFCAP_TSO6, setifcap), 1363 DEF_CMD("-tso6", -IFCAP_TSO6, setifcap), 1364 DEF_CMD("tso4", IFCAP_TSO4, setifcap), 1365 DEF_CMD("-tso4", -IFCAP_TSO4, setifcap), 1366 DEF_CMD("tso", IFCAP_TSO, setifcap), 1367 DEF_CMD("-tso", -IFCAP_TSO, setifcap), 1368 DEF_CMD("toe", IFCAP_TOE, setifcap), 1369 DEF_CMD("-toe", -IFCAP_TOE, setifcap), 1370 DEF_CMD("lro", IFCAP_LRO, setifcap), 1371 DEF_CMD("-lro", -IFCAP_LRO, setifcap), 1372 DEF_CMD("wol", IFCAP_WOL, setifcap), 1373 DEF_CMD("-wol", -IFCAP_WOL, setifcap), 1374 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap), 1375 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap), 1376 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap), 1377 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap), 1378 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap), 1379 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap), 1380 DEF_CMD("normal", -IFF_LINK0, setifflags), 1381 DEF_CMD("compress", IFF_LINK0, setifflags), 1382 DEF_CMD("noicmp", IFF_LINK1, setifflags), 1383 DEF_CMD_ARG("mtu", setifmtu), 1384 DEF_CMD_ARG("name", setifname), 1385 }; 1386 1387 static __constructor void 1388 ifconfig_ctor(void) 1389 { 1390 #define N(a) (sizeof(a) / sizeof(a[0])) 1391 size_t i; 1392 1393 for (i = 0; i < N(basic_cmds); i++) 1394 cmd_register(&basic_cmds[i]); 1395 #undef N 1396 } 1397