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