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 * 3. 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 * $FreeBSD: src/sbin/ifconfig/ifconfig.c,v 1.113.2.4 2006/02/09 10:48:43 yar Exp $ 30 */ 31 32 #include <sys/param.h> 33 #include <sys/ioctl.h> 34 #include <sys/socket.h> 35 #include <sys/sysctl.h> 36 #include <sys/time.h> 37 #include <sys/module.h> 38 #include <sys/linker.h> 39 #include <sys/cdefs.h> 40 41 #include <net/ethernet.h> 42 #include <net/if.h> 43 #include <net/if_var.h> 44 #include <net/if_dl.h> 45 #include <net/if_types.h> 46 #include <net/route.h> 47 48 /* IP */ 49 #include <netinet/in.h> 50 #include <netinet/in_var.h> 51 #include <arpa/inet.h> 52 #include <netdb.h> 53 54 #include <ctype.h> 55 #include <err.h> 56 #include <errno.h> 57 #include <fcntl.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 63 #include "ifconfig.h" 64 65 /* 66 * Since "struct ifreq" is composed of various union members, callers 67 * should pay special attention to interpret the value. 68 * (.e.g. little/big endian difference in the structure.) 69 */ 70 struct ifreq ifr; 71 72 char name[IFNAMSIZ]; 73 int flags; 74 int setaddr; 75 int setmask; 76 int doalias; 77 int clearaddr; 78 int newaddr = 1; 79 int verbose; 80 81 int supmedia = 0; 82 int printkeys = 0; /* Print keying material for interfaces. */ 83 int printifname = 0; /* Print the name of the created interface. */ 84 85 static int ifconfig(int argc, char *const *argv, int iscreate, 86 const struct afswtch *afp); 87 static void status(const struct afswtch *afp, int addrcount, 88 struct sockaddr_dl *sdl, struct if_msghdr *ifm, 89 struct ifa_msghdr *ifam); 90 static void tunnel_status(int s); 91 static void usage(void) __dead2; 92 93 static struct afswtch *af_getbyname(const char *name); 94 static struct afswtch *af_getbyfamily(int af); 95 static void af_other_status(int); 96 static void printifnamemaybe(void); 97 98 static struct option *opts = NULL; 99 100 void 101 opt_register(struct option *p) 102 { 103 p->next = opts; 104 opts = p; 105 } 106 107 static void 108 usage(void) 109 { 110 char options[1024]; 111 struct option *p; 112 113 /* XXX not right but close enough for now */ 114 options[0] = '\0'; 115 for (p = opts; p != NULL; p = p->next) { 116 strlcat(options, p->opt_usage, sizeof(options)); 117 strlcat(options, " ", sizeof(options)); 118 } 119 120 fprintf(stderr, 121 "usage: ifconfig %sinterface address_family [address [dest_address]]\n" 122 " [parameters]\n" 123 " ifconfig interface create\n" 124 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n" 125 " ifconfig -l [-d] [-u] [address_family]\n" 126 " ifconfig %s[-d] [-m] [-u] [-v]\n", 127 options, options, options); 128 exit(1); 129 } 130 131 static void printifnamemaybe(void) 132 { 133 if (printifname) 134 printf("%s\n", name); 135 } 136 137 int 138 main(int argc, char *argv[]) 139 { 140 int c, all, namesonly, downonly, uponly; 141 int need_nl = 0, count = 0; 142 const struct afswtch *afp = NULL; 143 int addrcount, ifindex; 144 struct if_msghdr *ifm, *nextifm; 145 struct ifa_msghdr *ifam; 146 struct sockaddr_dl *sdl; 147 char *buf, *lim, *next; 148 size_t needed; 149 int mib[6]; 150 char options[1024]; 151 const char *ifname; 152 struct option *p; 153 size_t iflen; 154 155 all = downonly = uponly = namesonly = verbose = 0; 156 157 /* 158 * Ensure we print interface name when expected to, 159 * even if we terminate early due to error. 160 */ 161 atexit(printifnamemaybe); 162 163 /* Parse leading line options */ 164 strlcpy(options, "adklmuv", sizeof(options)); 165 for (p = opts; p != NULL; p = p->next) 166 strlcat(options, p->opt, sizeof(options)); 167 while ((c = getopt(argc, argv, options)) != -1) { 168 switch (c) { 169 case 'a': /* scan all interfaces */ 170 all++; 171 break; 172 case 'd': /* restrict scan to "down" interfaces */ 173 downonly++; 174 break; 175 case 'k': 176 printkeys++; 177 break; 178 case 'l': /* scan interface names only */ 179 namesonly++; 180 break; 181 case 'm': /* show media choices in status */ 182 supmedia = 1; 183 break; 184 case 'u': /* restrict scan to "up" interfaces */ 185 uponly++; 186 break; 187 case 'v': 188 verbose++; 189 break; 190 default: 191 for (p = opts; p != NULL; p = p->next) 192 if (p->opt[0] == c) { 193 p->cb(optarg); 194 break; 195 } 196 if (p == NULL) 197 usage(); 198 break; 199 } 200 } 201 argc -= optind; 202 argv += optind; 203 204 /* -l cannot be used with -a or -m */ 205 if (namesonly && (all || supmedia)) 206 usage(); 207 208 /* nonsense.. */ 209 if (uponly && downonly) 210 usage(); 211 212 /* no arguments is equivalent to '-a' */ 213 if (!namesonly && argc < 1) 214 all = 1; 215 216 /* -a and -l allow an address family arg to limit the output */ 217 if (all || namesonly) { 218 if (argc > 1) 219 usage(); 220 221 ifname = NULL; 222 ifindex = 0; 223 if (argc == 1) { 224 afp = af_getbyname(*argv); 225 if (afp == NULL) 226 usage(); 227 if (afp->af_name != NULL) 228 argc--, argv++; 229 /* leave with afp non-zero */ 230 } 231 } else { 232 /* not listing, need an argument */ 233 if (argc < 1) 234 usage(); 235 236 ifname = *argv; 237 argc--, argv++; 238 239 /* check and maybe load support for this interface */ 240 ifmaybeload(ifname); 241 242 ifindex = if_nametoindex(ifname); 243 if (ifindex == 0) { 244 /* 245 * NOTE: We must special-case the `create' command 246 * right here as we would otherwise fail when trying 247 * to find the interface. 248 */ 249 if (argc > 0 && (strcmp(argv[0], "create") == 0 || 250 strcmp(argv[0], "plumb") == 0)) { 251 iflen = strlcpy(name, ifname, sizeof(name)); 252 if (iflen >= sizeof(name)) 253 errx(1, "%s: cloning name too long", 254 ifname); 255 ifconfig(argc, argv, 1, NULL); 256 return (0); 257 } 258 errx(1, "interface %s does not exist", ifname); 259 } 260 } 261 262 /* Check for address family */ 263 if (argc > 0) { 264 afp = af_getbyname(*argv); 265 if (afp != NULL) 266 argc--, argv++; 267 } 268 269 retry: 270 mib[0] = CTL_NET; 271 mib[1] = PF_ROUTE; 272 mib[2] = 0; 273 mib[3] = 0; /* address family */ 274 mib[4] = NET_RT_IFLIST; 275 mib[5] = ifindex; /* interface index */ 276 277 /* if particular family specified, only ask about it */ 278 if (afp != NULL) 279 mib[3] = afp->af_af; 280 281 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 282 errx(1, "iflist-sysctl-estimate"); 283 if ((buf = malloc(needed)) == NULL) 284 errx(1, "malloc"); 285 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 286 if (errno == ENOMEM && count++ < 10) { 287 warnx("Routing table grew, retrying"); 288 free(buf); 289 sleep(1); 290 goto retry; 291 } 292 errx(1, "actual retrieval of interface table"); 293 } 294 lim = buf + needed; 295 296 next = buf; 297 while (next < lim) { 298 int name_len; 299 300 ifm = (struct if_msghdr *)next; 301 302 if (ifm->ifm_type == RTM_IFINFO) { 303 #ifdef notyet 304 if (ifm->ifm_data.ifi_datalen == 0) 305 ifm->ifm_data.ifi_datalen = sizeof(struct if_data); 306 sdl = (struct sockaddr_dl *)((char *)ifm + sizeof(struct if_msghdr) - 307 sizeof(struct if_data) + ifm->ifm_data.ifi_datalen); 308 #else 309 sdl = (struct sockaddr_dl *)(ifm + 1); 310 #endif 311 flags = ifm->ifm_flags; 312 } else { 313 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n"); 314 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO, 315 ifm->ifm_type); 316 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen); 317 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next, 318 lim); 319 exit(1); 320 } 321 322 next += ifm->ifm_msglen; 323 ifam = NULL; 324 addrcount = 0; 325 while (next < lim) { 326 nextifm = (struct if_msghdr *)next; 327 328 if (nextifm->ifm_type != RTM_NEWADDR) 329 break; 330 331 if (ifam == NULL) 332 ifam = (struct ifa_msghdr *)nextifm; 333 334 addrcount++; 335 next += nextifm->ifm_msglen; 336 } 337 338 if (sizeof(name) <= sdl->sdl_nlen) 339 name_len = sizeof(name) - 1; 340 else 341 name_len = sdl->sdl_nlen; 342 343 memcpy(name, sdl->sdl_data, name_len); 344 name[name_len] = '\0'; 345 346 if (all || namesonly) { 347 if (uponly) 348 if ((flags & IFF_UP) == 0) 349 continue; /* not up */ 350 if (downonly) 351 if (flags & IFF_UP) 352 continue; /* not down */ 353 if (namesonly) { 354 if (afp == NULL || afp->af_af != AF_LINK || 355 sdl->sdl_type == IFT_ETHER) { 356 if (need_nl) 357 putchar(' '); 358 fputs(name, stdout); 359 need_nl++; 360 } 361 continue; 362 } 363 } 364 365 if (argc > 0) 366 ifconfig(argc, argv, 0, afp); 367 else 368 status(afp, addrcount, sdl, ifm, ifam); 369 } 370 free(buf); 371 372 if (namesonly && need_nl > 0) 373 putchar('\n'); 374 375 return (0); 376 } 377 378 static struct afswtch *afs = NULL; 379 380 void 381 af_register(struct afswtch *p) 382 { 383 p->af_next = afs; 384 afs = p; 385 } 386 387 static struct afswtch * 388 af_getbyname(const char *name) 389 { 390 struct afswtch *afp; 391 392 for (afp = afs; afp != NULL; afp = afp->af_next) 393 if (strcmp(afp->af_name, name) == 0) 394 return afp; 395 return NULL; 396 } 397 398 static struct afswtch * 399 af_getbyfamily(int af) 400 { 401 struct afswtch *afp; 402 403 for (afp = afs; afp != NULL; afp = afp->af_next) 404 if (afp->af_af == af) 405 return afp; 406 return NULL; 407 } 408 409 static void 410 af_other_status(int s) 411 { 412 struct afswtch *afp; 413 uint8_t afmask[howmany(AF_MAX, NBBY)]; 414 415 memset(afmask, 0, sizeof(afmask)); 416 for (afp = afs; afp != NULL; afp = afp->af_next) { 417 if (afp->af_other_status == NULL) 418 continue; 419 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) 420 continue; 421 afp->af_other_status(s); 422 setbit(afmask, afp->af_af); 423 } 424 } 425 426 static void 427 af_all_tunnel_status(int s) 428 { 429 struct afswtch *afp; 430 uint8_t afmask[howmany(AF_MAX, NBBY)]; 431 432 memset(afmask, 0, sizeof(afmask)); 433 for (afp = afs; afp != NULL; afp = afp->af_next) { 434 if (afp->af_status_tunnel == NULL) 435 continue; 436 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) 437 continue; 438 afp->af_status_tunnel(s); 439 setbit(afmask, afp->af_af); 440 } 441 } 442 443 static struct cmd *cmds = NULL; 444 445 void 446 cmd_register(struct cmd *p) 447 { 448 p->c_next = cmds; 449 cmds = p; 450 } 451 452 static const struct cmd * 453 cmd_lookup(const char *name, int iscreate) 454 { 455 const struct cmd *p; 456 457 for (p = cmds; p != NULL; p = p->c_next) 458 if (strcmp(name, p->c_name) == 0) { 459 if (iscreate) { 460 if (p->c_iscloneop) 461 return p; 462 } else { 463 if (!p->c_iscloneop) 464 return p; 465 } 466 } 467 return NULL; 468 } 469 470 struct callback { 471 callback_func *cb_func; 472 void *cb_arg; 473 struct callback *cb_next; 474 }; 475 static struct callback *callbacks = NULL; 476 477 void 478 callback_register(callback_func *func, void *arg) 479 { 480 struct callback *cb; 481 482 cb = malloc(sizeof(struct callback)); 483 if (cb == NULL) 484 errx(1, "unable to allocate memory for callback"); 485 cb->cb_func = func; 486 cb->cb_arg = arg; 487 cb->cb_next = callbacks; 488 callbacks = cb; 489 } 490 491 /* specially-handled commands */ 492 static void setifaddr(const char *, int, int, const struct afswtch *); 493 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr); 494 495 static void setifdstaddr(const char *, int, int, const struct afswtch *); 496 static const struct cmd setifdstaddr_cmd = 497 DEF_CMD("ifdstaddr", 0, setifdstaddr); 498 499 static int 500 ifconfig(int argc, char *const *argv, int iscreate, 501 const struct afswtch *uafp) 502 { 503 const struct afswtch *afp, *nafp; 504 struct callback *cb; 505 int s; 506 507 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 508 afp = uafp != NULL ? uafp : af_getbyname("inet"); 509 top: 510 ifr.ifr_addr.sa_family = 511 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ? 512 AF_LOCAL : afp->af_af; 513 514 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 && 515 (uafp != NULL || errno != EAFNOSUPPORT || 516 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)) 517 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family); 518 519 while (argc > 0) { 520 const struct cmd *p; 521 522 p = cmd_lookup(*argv, iscreate); 523 524 if (iscreate && p == NULL) { 525 /* 526 * Push the clone create callback so the new 527 * device is created and can be used for any 528 * remaining arguments. 529 */ 530 cb = callbacks; 531 if (cb == NULL) 532 errx(1, "internal error, no callback"); 533 callbacks = cb->cb_next; 534 cb->cb_func(s, cb->cb_arg); 535 iscreate = 0; 536 537 /* 538 * After cloning, make sure we have an up-to-date name 539 * in ifr_name. 540 */ 541 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 542 543 /* 544 * Handle any address family spec that 545 * immediately follows and potentially 546 * recreate the socket. 547 */ 548 nafp = af_getbyname(*argv); 549 if (nafp != NULL) { 550 argc--, argv++; 551 if (nafp != afp) { 552 close(s); 553 afp = nafp; 554 goto top; 555 } 556 } 557 /* 558 * Look for a normal parameter. 559 */ 560 continue; 561 } 562 if (p == NULL) { 563 /* 564 * Not a recognized command, choose between setting 565 * the interface address and the dst address. 566 */ 567 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd); 568 } 569 if (p->c_u.c_func || p->c_u.c_func2) { 570 if (p->c_parameter == NEXTARG) { 571 if (argv[1] == NULL) 572 errx(1, "'%s' requires argument", 573 p->c_name); 574 p->c_u.c_func(argv[1], 0, s, afp); 575 argc--, argv++; 576 } else if (p->c_parameter == OPTARG) { 577 p->c_u.c_func(argv[1], 0, s, afp); 578 if (argv[1] != NULL) 579 argc--, argv++; 580 } else if (p->c_parameter == NEXTARG2) { 581 if (argc < 3) 582 errx(1, "'%s' requires 2 arguments", 583 p->c_name); 584 p->c_u.c_func2(argv[1], argv[2], s, afp); 585 argc -= 2, argv += 2; 586 } else 587 p->c_u.c_func(*argv, p->c_parameter, s, afp); 588 } 589 argc--, argv++; 590 } 591 592 /* 593 * Do any post argument processing required by the address family. 594 */ 595 if (afp->af_postproc != NULL) 596 afp->af_postproc(s, afp); 597 /* 598 * Do deferred callbacks registered while processing 599 * command-line arguments. 600 */ 601 for (cb = callbacks; cb != NULL; cb = cb->cb_next) 602 cb->cb_func(s, cb->cb_arg); 603 /* 604 * Do deferred operations. 605 */ 606 if (clearaddr) { 607 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) { 608 warnx("interface %s cannot change %s addresses!", 609 name, afp->af_name); 610 clearaddr = 0; 611 } 612 } 613 if (clearaddr) { 614 int ret; 615 strlcpy(afp->af_ridreq, name, sizeof ifr.ifr_name); 616 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq); 617 if (ret < 0) { 618 if (errno == EADDRNOTAVAIL && (doalias >= 0)) { 619 /* means no previous address for interface */ 620 } else 621 Perror("ioctl (SIOCDIFADDR)"); 622 } 623 } 624 if (newaddr) { 625 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) { 626 warnx("interface %s cannot change %s addresses!", 627 name, afp->af_name); 628 newaddr = 0; 629 } 630 } 631 if (newaddr && (setaddr || setmask)) { 632 strlcpy(afp->af_addreq, name, sizeof ifr.ifr_name); 633 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0) 634 Perror("ioctl (SIOCAIFADDR)"); 635 } 636 637 close(s); 638 return (0); 639 } 640 641 /*ARGSUSED*/ 642 static void 643 setifaddr(const char *addr, int param, int s, const struct afswtch *afp) 644 { 645 if (afp->af_getaddr == NULL) 646 return; 647 /* 648 * Delay the ioctl to set the interface addr until flags are all set. 649 * The address interpretation may depend on the flags, 650 * and the flags may change when the address is set. 651 */ 652 setaddr++; 653 if (doalias == 0 && afp->af_af != AF_LINK) 654 clearaddr = 1; 655 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR)); 656 } 657 658 static void 659 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp) 660 { 661 struct addrinfo *srcres, *dstres; 662 int ecode; 663 664 if (afp->af_settunnel == NULL) { 665 warn("address family %s does not support tunnel setup", 666 afp->af_name); 667 return; 668 } 669 670 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0) 671 errx(1, "error in parsing address string: %s", 672 gai_strerror(ecode)); 673 674 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0) 675 errx(1, "error in parsing address string: %s", 676 gai_strerror(ecode)); 677 678 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family) 679 errx(1, 680 "source and destination address families do not match"); 681 682 afp->af_settunnel(s, srcres, dstres); 683 684 freeaddrinfo(srcres); 685 freeaddrinfo(dstres); 686 } 687 688 /* ARGSUSED */ 689 static void 690 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp) 691 { 692 693 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0) 694 err(1, "SIOCDIFPHYADDR"); 695 } 696 697 static void 698 setifnetmask(const char *addr, int dummy __unused, int s, 699 const struct afswtch *afp) 700 { 701 if (afp->af_getaddr != NULL) { 702 setmask++; 703 afp->af_getaddr(addr, MASK); 704 } 705 } 706 707 static void 708 setifbroadaddr(const char *addr, int dummy __unused, int s, 709 const struct afswtch *afp) 710 { 711 if (afp->af_getaddr != NULL) 712 afp->af_getaddr(addr, DSTADDR); 713 } 714 715 static void 716 notealias(const char *addr, int param, int s, const struct afswtch *afp) 717 { 718 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr)) 719 if (setaddr && doalias == 0 && param < 0) 720 if (afp->af_addreq != NULL && afp->af_ridreq != NULL) 721 bcopy((caddr_t)rqtosa(af_addreq), 722 (caddr_t)rqtosa(af_ridreq), 723 rqtosa(af_addreq)->sa_len); 724 doalias = param; 725 if (param < 0) { 726 clearaddr = 1; 727 newaddr = 0; 728 } else 729 clearaddr = 0; 730 #undef rqtosa 731 } 732 733 /*ARGSUSED*/ 734 static void 735 setifdstaddr(const char *addr, int param __unused, int s, 736 const struct afswtch *afp) 737 { 738 if (afp->af_getaddr != NULL) 739 afp->af_getaddr(addr, DSTADDR); 740 } 741 742 /* 743 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion 744 * of the ifreq structure, which may confuse other parts of ifconfig. 745 * Make a private copy so we can avoid that. 746 */ 747 static void 748 setifflags(const char *vname, int value, int s, const struct afswtch *afp) 749 { 750 struct ifreq my_ifr; 751 752 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq)); 753 754 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) { 755 Perror("ioctl (SIOCGIFFLAGS)"); 756 } 757 strlcpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name)); 758 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16); 759 760 if (value < 0) { 761 value = -value; 762 flags &= ~value; 763 } else 764 flags |= value; 765 my_ifr.ifr_flags = flags & 0xffff; 766 my_ifr.ifr_flagshigh = flags >> 16; 767 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0) 768 Perror(vname); 769 } 770 771 void 772 setifcap(const char *vname, int value, int s, const struct afswtch *afp) 773 { 774 775 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) { 776 Perror("ioctl (SIOCGIFCAP)"); 777 } 778 flags = ifr.ifr_curcap; 779 if (value < 0) { 780 value = -value; 781 flags &= ~value; 782 } else 783 flags |= value; 784 ifr.ifr_reqcap = flags; 785 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0) 786 Perror(vname); 787 } 788 789 static void 790 setifmetric(const char *val, int dummy __unused, int s, 791 const struct afswtch *afp) 792 { 793 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 794 ifr.ifr_metric = atoi(val); 795 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0) 796 err(1, "ioctl SIOCSIFMETRIC (set metric)"); 797 } 798 799 static void 800 setifmtu(const char *val, int dummy __unused, int s, 801 const struct afswtch *afp) 802 { 803 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 804 ifr.ifr_mtu = atoi(val); 805 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0) 806 err(1, "ioctl SIOCSIFMTU (set mtu)"); 807 } 808 809 static void 810 setiftsolen(const char *val, int dummy __unused, int s, 811 const struct afswtch *afp) 812 { 813 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 814 ifr.ifr_tsolen = atoi(val); 815 if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0) 816 err(1, "ioctl SIOCSIFTSOLEN (set tsolen)"); 817 } 818 819 static void 820 setifname(const char *val, int dummy __unused, int s, 821 const struct afswtch *afp) 822 { 823 char *newname; 824 825 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 826 827 newname = strdup(val); 828 if (newname == NULL) 829 err(1, "no memory to set ifname"); 830 ifr.ifr_data = newname; 831 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) { 832 free(newname); 833 err(1, "ioctl SIOCSIFNAME (set name)"); 834 } 835 printifname = 1; 836 strlcpy(name, newname, sizeof(name)); 837 free(newname); 838 } 839 840 static void 841 setifpollcpu(const char *val, int dummy __unused, int s, 842 const struct afswtch *afp) 843 { 844 warnx("pollcpu is deprecated, use polling or npolling instead"); 845 setifflags("npolling", IFF_NPOLLING, s, afp); 846 } 847 848 /* 849 * Expand the compacted form of addresses as returned via the 850 * configuration read via sysctl(). 851 */ 852 static void 853 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 854 { 855 struct sockaddr *sa; 856 int i; 857 858 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 859 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 860 if ((rtinfo->rti_addrs & (1 << i)) == 0) 861 continue; 862 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 863 RT_ADVANCE(cp, sa); 864 } 865 } 866 867 #define IFFBITS \ 868 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \ 869 "\10NOARP\11PROMISC\12ALLMULTI\14SIMPLEX\15LINK0\16LINK1\17LINK2" \ 870 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NPOLLING\26IDIRECT" 871 872 #define IFCAPBITS \ 873 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7RSS" \ 874 "\10VLAN_HWCSUM\11TSO" 875 876 /* 877 * Print the status of the interface. If an address family was 878 * specified, show only it; otherwise, show them all. 879 */ 880 static void 881 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl, 882 struct if_msghdr *ifm, struct ifa_msghdr *ifam) 883 { 884 struct rt_addrinfo info; 885 int allfamilies, s; 886 struct ifstat ifs; 887 888 if (afp == NULL) { 889 allfamilies = 1; 890 ifr.ifr_addr.sa_family = AF_LOCAL; 891 } else { 892 allfamilies = 0; 893 ifr.ifr_addr.sa_family = 894 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af; 895 } 896 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 897 898 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0); 899 if (s < 0) 900 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family); 901 902 printf("%s: ", name); 903 printb("flags", flags, IFFBITS); 904 if (ifm->ifm_data.ifi_metric) 905 printf(" metric %ld", ifm->ifm_data.ifi_metric); 906 if (ifm->ifm_data.ifi_mtu) 907 printf(" mtu %ld", ifm->ifm_data.ifi_mtu); 908 putchar('\n'); 909 910 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) { 911 if (ifr.ifr_curcap != 0) { 912 printb("\toptions", ifr.ifr_curcap, IFCAPBITS); 913 putchar('\n'); 914 } 915 if (supmedia && ifr.ifr_reqcap != 0) { 916 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS); 917 putchar('\n'); 918 if (ifr.ifr_reqcap & IFCAP_TSO) { 919 if (ioctl(s, SIOCGIFTSOLEN, 920 (caddr_t)&ifr) == 0) { 921 printf("\ttsolen %d", ifr.ifr_tsolen); 922 putchar('\n'); 923 } 924 } 925 } 926 } 927 928 tunnel_status(s); 929 930 while (addrcount > 0) { 931 info.rti_addrs = ifam->ifam_addrs; 932 /* Expand the compacted addresses */ 933 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 934 &info); 935 936 if (allfamilies) { 937 const struct afswtch *p; 938 p = af_getbyfamily(info.rti_info[RTAX_IFA]->sa_family); 939 if (p != NULL && p->af_status != NULL) 940 p->af_status(s, &info); 941 } else if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) 942 afp->af_status(s, &info); 943 addrcount--; 944 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen); 945 } 946 if (allfamilies || afp->af_af == AF_LINK) { 947 const struct afswtch *lafp; 948 949 /* 950 * Hack; the link level address is received separately 951 * from the routing information so any address is not 952 * handled above. Cobble together an entry and invoke 953 * the status method specially. 954 */ 955 lafp = af_getbyname("lladdr"); 956 if (lafp != NULL) { 957 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl; 958 lafp->af_status(s, &info); 959 } 960 } 961 if (allfamilies) 962 af_other_status(s); 963 else if (afp->af_other_status != NULL) 964 afp->af_other_status(s); 965 966 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name); 967 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) 968 printf("%s", ifs.ascii); 969 970 close(s); 971 return; 972 } 973 974 static void 975 tunnel_status(int s) 976 { 977 af_all_tunnel_status(s); 978 } 979 980 void 981 Perror(const char *cmd) 982 { 983 switch (errno) { 984 985 case ENXIO: 986 errx(1, "%s: no such interface", cmd); 987 break; 988 989 case EPERM: 990 errx(1, "%s: permission denied", cmd); 991 break; 992 993 default: 994 err(1, "%s", cmd); 995 } 996 } 997 998 /* 999 * Print a value a la the %pb%i format of the kernel's kprintf() 1000 */ 1001 void 1002 printb(const char *s, unsigned v, const char *bits) 1003 { 1004 int i, any = 0; 1005 char c; 1006 1007 if (bits && *bits == 8) 1008 printf("%s=%o", s, v); 1009 else 1010 printf("%s=%x", s, v); 1011 bits++; 1012 if (bits) { 1013 putchar('<'); 1014 while ((i = *bits++) != '\0') { 1015 if (v & (1 << (i-1))) { 1016 if (any) 1017 putchar(','); 1018 any = 1; 1019 for (; (c = *bits) > 32; bits++) 1020 putchar(c); 1021 } else 1022 for (; *bits > 32; bits++) 1023 ; 1024 } 1025 putchar('>'); 1026 } 1027 } 1028 1029 void 1030 ifmaybeload(const char *name) 1031 { 1032 #define MOD_PREFIX_LEN 3 /* "if_" */ 1033 struct module_stat mstat; 1034 int fileid, modid; 1035 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp; 1036 const char *cp; 1037 1038 /* trim the interface number off the end */ 1039 strlcpy(ifname, name, sizeof(ifname)); 1040 for (dp = ifname; *dp != 0; dp++) 1041 if (isdigit(*dp)) { 1042 *dp = 0; 1043 break; 1044 } 1045 1046 /* turn interface and unit into module name */ 1047 strlcpy(ifkind, "if_", sizeof(ifkind)); 1048 strlcat(ifkind, ifname, sizeof(ifkind)); 1049 1050 /* scan files in kernel */ 1051 mstat.version = sizeof(struct module_stat); 1052 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { 1053 /* scan modules in file */ 1054 for (modid = kldfirstmod(fileid); modid > 0; 1055 modid = modfnext(modid)) { 1056 if (modstat(modid, &mstat) < 0) 1057 continue; 1058 /* strip bus name if present */ 1059 if ((cp = strchr(mstat.name, '/')) != NULL) { 1060 cp++; 1061 } else { 1062 cp = mstat.name; 1063 } 1064 /* already loaded? */ 1065 if (strcmp(ifname, cp) == 0 || 1066 strcmp(ifkind, cp) == 0) 1067 return; 1068 } 1069 } 1070 1071 /* not present, we should try to load it */ 1072 kldload(ifkind); 1073 } 1074 1075 static struct cmd basic_cmds[] = { 1076 DEF_CMD("up", IFF_UP, setifflags), 1077 DEF_CMD("down", -IFF_UP, setifflags), 1078 DEF_CMD("arp", -IFF_NOARP, setifflags), 1079 DEF_CMD("-arp", IFF_NOARP, setifflags), 1080 DEF_CMD("debug", IFF_DEBUG, setifflags), 1081 DEF_CMD("-debug", -IFF_DEBUG, setifflags), 1082 DEF_CMD("promisc", IFF_PPROMISC, setifflags), 1083 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags), 1084 DEF_CMD("add", IFF_UP, notealias), 1085 DEF_CMD("alias", IFF_UP, notealias), 1086 DEF_CMD("-alias", -IFF_UP, notealias), 1087 DEF_CMD("delete", -IFF_UP, notealias), 1088 DEF_CMD("remove", -IFF_UP, notealias), 1089 #ifdef notdef 1090 #define EN_SWABIPS 0x1000 1091 DEF_CMD("swabips", EN_SWABIPS, setifflags), 1092 DEF_CMD("-swabips", -EN_SWABIPS, setifflags), 1093 #endif 1094 DEF_CMD_ARG("netmask", setifnetmask), 1095 DEF_CMD_ARG("metric", setifmetric), 1096 DEF_CMD_ARG("broadcast", setifbroadaddr), 1097 DEF_CMD_ARG2("tunnel", settunnel), 1098 DEF_CMD("-tunnel", 0, deletetunnel), 1099 DEF_CMD("deletetunnel", 0, deletetunnel), 1100 DEF_CMD("link0", IFF_LINK0, setifflags), 1101 DEF_CMD("-link0", -IFF_LINK0, setifflags), 1102 DEF_CMD("link1", IFF_LINK1, setifflags), 1103 DEF_CMD("-link1", -IFF_LINK1, setifflags), 1104 DEF_CMD("link2", IFF_LINK2, setifflags), 1105 DEF_CMD("-link2", -IFF_LINK2, setifflags), 1106 DEF_CMD("monitor", IFF_MONITOR, setifflags), 1107 DEF_CMD("-monitor", -IFF_MONITOR, setifflags), 1108 DEF_CMD("staticarp", IFF_STATICARP, setifflags), 1109 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags), 1110 DEF_CMD("polling", IFF_NPOLLING, setifflags), 1111 DEF_CMD("-polling", -IFF_NPOLLING, setifflags), 1112 DEF_CMD("npolling", IFF_NPOLLING, setifflags), 1113 DEF_CMD("-npolling", -IFF_NPOLLING, setifflags), 1114 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap), 1115 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap), 1116 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap), 1117 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap), 1118 DEF_CMD("netcons", IFCAP_NETCONS, setifcap), 1119 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap), 1120 DEF_CMD("rss", IFCAP_RSS, setifcap), 1121 DEF_CMD("-rss", -IFCAP_RSS, setifcap), 1122 DEF_CMD("tso", IFCAP_TSO, setifcap), 1123 DEF_CMD("-tso", -IFCAP_TSO, setifcap), 1124 DEF_CMD("normal", -IFF_LINK0, setifflags), 1125 DEF_CMD("compress", IFF_LINK0, setifflags), 1126 DEF_CMD("noicmp", IFF_LINK1, setifflags), 1127 DEF_CMD_ARG("mtu", setifmtu), 1128 DEF_CMD_ARG("name", setifname), 1129 DEF_CMD_ARG("pollcpu", setifpollcpu), 1130 DEF_CMD_ARG("tsolen", setiftsolen) 1131 }; 1132 1133 static __constructor(101) void 1134 ifconfig_ctor(void) 1135 { 1136 size_t i; 1137 1138 for (i = 0; i < nitems(basic_cmds); i++) 1139 cmd_register(&basic_cmds[i]); 1140 } 1141