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