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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved. 34 * @(#)ifconfig.c 8.2 (Berkeley) 2/16/94 35 * $FreeBSD: src/sbin/ifconfig/ifconfig.c,v 1.96 2004/02/27 06:43:14 kan Exp $ 36 * $DragonFly: src/sbin/ifconfig/ifconfig.c,v 1.13 2005/01/05 15:14:04 joerg Exp $ 37 */ 38 39 #include <sys/param.h> 40 #include <sys/ioctl.h> 41 #include <sys/socket.h> 42 #include <sys/sockio.h> 43 #include <sys/sysctl.h> 44 #include <sys/time.h> 45 #include <sys/module.h> 46 #include <sys/linker.h> 47 48 #include <net/ethernet.h> 49 #include <net/if.h> 50 #include <net/if_var.h> 51 #include <net/if_dl.h> 52 #include <net/if_types.h> 53 #include <net/route.h> 54 55 /* IP */ 56 #include <netinet/in.h> 57 #include <netinet/in_var.h> 58 #include <arpa/inet.h> 59 #include <netdb.h> 60 61 #ifdef INET6 62 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */ 63 #endif 64 65 #ifndef NO_IPX 66 /* IPX */ 67 #define IPXIP 68 #define IPTUNNEL 69 #include <netipx/ipx.h> 70 #include <netipx/ipx_if.h> 71 #endif 72 73 /* Appletalk */ 74 #include <netatalk/at.h> 75 76 /* XNS */ 77 #ifdef NS 78 #define NSIP 79 #include <netns/ns.h> 80 #include <netns/ns_if.h> 81 #endif 82 83 /* OSI */ 84 85 #include <ctype.h> 86 #include <err.h> 87 #include <errno.h> 88 #include <fcntl.h> 89 #include <stdio.h> 90 #include <stdlib.h> 91 #include <string.h> 92 #include <unistd.h> 93 #include <ifaddrs.h> 94 95 #include "ifconfig.h" 96 97 /* wrapper for KAME-special getnameinfo() */ 98 #ifndef NI_WITHSCOPEID 99 #define NI_WITHSCOPEID 0 100 #endif 101 102 struct ifreq ifr, ridreq; 103 struct ifaliasreq addreq; 104 #ifdef INET6 105 struct in6_ifreq in6_ridreq; 106 struct in6_aliasreq in6_addreq = 107 { { 0 }, 108 { 0 }, 109 { 0 }, 110 { 0 }, 111 0, 112 { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } }; 113 #endif 114 struct sockaddr_in netmask; 115 struct netrange at_nr; /* AppleTalk net range */ 116 117 char name[IFNAMSIZ]; 118 int flags; 119 int setaddr; 120 int setipdst; 121 int setmask; 122 int doalias; 123 int clearaddr; 124 int newaddr = 1; 125 #ifdef INET6 126 static int ip6lifetime; 127 #endif 128 129 struct afswtch; 130 131 int supmedia = 0; 132 int listcloners = 0; 133 int printname = 0; /* Print the name of the created interface. */ 134 135 #ifdef INET6 136 char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/ 137 #endif 138 139 void Perror(const char *cmd); 140 void checkatrange(struct sockaddr_at *); 141 int ifconfig(int argc, char *const *argv, const struct afswtch *afp); 142 void notealias(const char *, int, int, const struct afswtch *afp); 143 void list_cloners(void); 144 void printb(const char *s, unsigned value, const char *bits); 145 void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *); 146 void status(const struct afswtch *afp, int addrcount, 147 struct sockaddr_dl *sdl, struct if_msghdr *ifm, 148 struct ifa_msghdr *ifam); 149 void tunnel_status(int s); 150 void usage(void); 151 void ifmaybeload(char *name); 152 153 #ifdef INET6 154 void in6_fillscopeid(struct sockaddr_in6 *sin6); 155 int prefix(void *, int); 156 static char *sec2str(time_t); 157 int explicit_prefix = 0; 158 #endif 159 160 typedef void c_func(const char *cmd, int arg, int s, const struct afswtch *afp); 161 typedef void c_func2(const char *arg, const char *arg2, int s, const struct afswtch *afp); 162 c_func setatphase, setatrange; 163 c_func setifaddr, setifbroadaddr, setifdstaddr, setifnetmask; 164 c_func2 settunnel; 165 c_func deletetunnel; 166 #ifdef INET6 167 c_func setifprefixlen; 168 c_func setip6flags; 169 c_func setip6pltime; 170 c_func setip6vltime; 171 c_func2 setip6lifetime; 172 c_func setip6eui64; 173 #endif 174 c_func setifipdst; 175 c_func setifflags, setifmetric, setifmtu, setifcap; 176 c_func clone_destroy; 177 c_func setifname; 178 179 180 void clone_create(void); 181 182 183 #define NEXTARG 0xffffff 184 #define NEXTARG2 0xfffffe 185 186 const 187 struct cmd { 188 const char *c_name; 189 int c_parameter; /* NEXTARG means next argv */ 190 void (*c_func)(const char *, int, int, const struct afswtch *afp); 191 void (*c_func2)(const char *, const char *, int, const struct afswtch *afp); 192 } cmds[] = { 193 { "up", IFF_UP, setifflags } , 194 { "down", -IFF_UP, setifflags }, 195 { "arp", -IFF_NOARP, setifflags }, 196 { "-arp", IFF_NOARP, setifflags }, 197 { "debug", IFF_DEBUG, setifflags }, 198 { "-debug", -IFF_DEBUG, setifflags }, 199 { "promisc", IFF_PPROMISC, setifflags }, 200 { "-promisc", -IFF_PPROMISC, setifflags }, 201 { "add", IFF_UP, notealias }, 202 { "alias", IFF_UP, notealias }, 203 { "-alias", -IFF_UP, notealias }, 204 { "delete", -IFF_UP, notealias }, 205 { "remove", -IFF_UP, notealias }, 206 #ifdef notdef 207 #define EN_SWABIPS 0x1000 208 { "swabips", EN_SWABIPS, setifflags }, 209 { "-swabips", -EN_SWABIPS, setifflags }, 210 #endif 211 { "netmask", NEXTARG, setifnetmask }, 212 #ifdef INET6 213 { "prefixlen", NEXTARG, setifprefixlen }, 214 { "anycast", IN6_IFF_ANYCAST, setip6flags }, 215 { "tentative", IN6_IFF_TENTATIVE, setip6flags }, 216 { "-tentative", -IN6_IFF_TENTATIVE, setip6flags }, 217 { "deprecated", IN6_IFF_DEPRECATED, setip6flags }, 218 { "-deprecated", -IN6_IFF_DEPRECATED, setip6flags }, 219 { "autoconf", IN6_IFF_AUTOCONF, setip6flags }, 220 { "-autoconf", -IN6_IFF_AUTOCONF, setip6flags }, 221 { "pltime", NEXTARG, setip6pltime }, 222 { "vltime", NEXTARG, setip6vltime }, 223 { "eui64", 0, setip6eui64 }, 224 #endif 225 { "range", NEXTARG, setatrange }, 226 { "phase", NEXTARG, setatphase }, 227 { "metric", NEXTARG, setifmetric }, 228 { "broadcast", NEXTARG, setifbroadaddr }, 229 { "ipdst", NEXTARG, setifipdst }, 230 { "tunnel", NEXTARG2, NULL, settunnel }, 231 { "deletetunnel", 0, deletetunnel }, 232 { "link0", IFF_LINK0, setifflags }, 233 { "-link0", -IFF_LINK0, setifflags }, 234 { "link1", IFF_LINK1, setifflags }, 235 { "-link1", -IFF_LINK1, setifflags }, 236 { "link2", IFF_LINK2, setifflags }, 237 { "-link2", -IFF_LINK2, setifflags }, 238 #ifdef USE_IF_MEDIA 239 { "media", NEXTARG, setmedia }, 240 { "mediaopt", NEXTARG, setmediaopt }, 241 { "-mediaopt", NEXTARG, unsetmediaopt }, 242 #endif 243 #ifdef USE_VLANS 244 { "vlan", NEXTARG, setvlantag }, 245 { "vlandev", NEXTARG, setvlandev }, 246 { "-vlandev", NEXTARG, unsetvlandev }, 247 #endif 248 #if 0 249 /* XXX `create' special-cased below */ 250 {"create", 0, clone_create }, 251 {"plumb", 0, clone_create }, 252 #endif 253 {"destroy", 0, clone_destroy }, 254 {"unplumb", 0, clone_destroy }, 255 #ifdef USE_IEEE80211 256 { "ssid", NEXTARG, set80211ssid }, 257 { "nwid", NEXTARG, set80211ssid }, 258 { "stationname", NEXTARG, set80211stationname }, 259 { "station", NEXTARG, set80211stationname }, /* BSD/OS */ 260 { "channel", NEXTARG, set80211channel }, 261 { "authmode", NEXTARG, set80211authmode }, 262 { "powersavemode", NEXTARG, set80211powersavemode }, 263 { "powersave", 1, set80211powersave }, 264 { "-powersave", 0, set80211powersave }, 265 { "powersavesleep", NEXTARG, set80211powersavesleep }, 266 { "wepmode", NEXTARG, set80211wepmode }, 267 { "wep", 1, set80211wep }, 268 { "-wep", 0, set80211wep }, 269 { "weptxkey", NEXTARG, set80211weptxkey }, 270 { "wepkey", NEXTARG, set80211wepkey }, 271 { "nwkey", NEXTARG, set80211nwkey }, /* NetBSD */ 272 { "-nwkey", 0, set80211wep }, /* NetBSD */ 273 #endif 274 { "rxcsum", IFCAP_RXCSUM, setifcap }, 275 { "-rxcsum", -IFCAP_RXCSUM, setifcap }, 276 { "txcsum", IFCAP_TXCSUM, setifcap }, 277 { "-txcsum", -IFCAP_TXCSUM, setifcap }, 278 { "netcons", IFCAP_NETCONS, setifcap }, 279 { "-netcons", -IFCAP_NETCONS, setifcap }, 280 { "polling", IFCAP_POLLING, setifcap }, 281 { "-polling", -IFCAP_POLLING, setifcap }, 282 { "normal", -IFF_LINK0, setifflags }, 283 { "compress", IFF_LINK0, setifflags }, 284 { "noicmp", IFF_LINK1, setifflags }, 285 { "mtu", NEXTARG, setifmtu }, 286 { "name", NEXTARG, setifname }, 287 { 0, 0, setifaddr }, 288 { 0, 0, setifdstaddr }, 289 }; 290 291 /* 292 * XNS support liberally adapted from code written at the University of 293 * Maryland principally by James O'Toole and Chris Torek. 294 */ 295 typedef void af_status(int, struct rt_addrinfo *); 296 typedef void af_getaddr(const char *, int); 297 typedef void af_getprefix(const char *, int); 298 299 af_status in_status, at_status, link_status; 300 af_getaddr in_getaddr, at_getaddr, link_getaddr; 301 302 #ifndef NO_IPX 303 af_status ipx_status; 304 af_getaddr ipx_getaddr; 305 #endif 306 307 #ifdef INET6 308 af_status in6_status; 309 af_getaddr in6_getaddr; 310 af_getprefix in6_getprefix; 311 #endif /*INET6*/ 312 #ifdef NS 313 af_status xns_status; 314 af_getaddr xns_getaddr; 315 #endif 316 317 /* Known address families */ 318 const 319 struct afswtch { 320 const char *af_name; 321 short af_af; 322 af_status *af_status; 323 af_getaddr *af_getaddr; 324 af_getprefix *af_getprefix; 325 u_long af_difaddr; 326 u_long af_aifaddr; 327 caddr_t af_ridreq; 328 caddr_t af_addreq; 329 } afs[] = { 330 #define C(x) ((caddr_t) &x) 331 { "inet", AF_INET, in_status, in_getaddr, NULL, 332 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) }, 333 #ifdef INET6 334 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix, 335 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6, 336 C(in6_ridreq), C(in6_addreq) }, 337 #endif /*INET6*/ 338 #ifndef NO_IPX 339 { "ipx", AF_IPX, ipx_status, ipx_getaddr, NULL, 340 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) }, 341 #endif 342 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL, 343 SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) }, 344 #ifdef NS 345 { "ns", AF_NS, xns_status, xns_getaddr, NULL, 346 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) }, 347 #endif 348 { "link", AF_LINK, link_status, link_getaddr, NULL, 349 0, SIOCSIFLLADDR, NULL, C(ridreq) }, 350 { "ether", AF_LINK, link_status, link_getaddr, NULL, 351 0, SIOCSIFLLADDR, NULL, C(ridreq) }, 352 { "lladdr", AF_LINK, link_status, link_getaddr, NULL, 353 0, SIOCSIFLLADDR, NULL, C(ridreq) }, 354 #if 0 /* XXX conflicts with the media command */ 355 #ifdef USE_IF_MEDIA 356 { "media", AF_UNSPEC, media_status, NULL, NULL, }, /* XXX not real!! */ 357 #endif 358 #ifdef USE_VLANS 359 { "vlan", AF_UNSPEC, vlan_status, NULL, NULL, }, /* XXX not real!! */ 360 #endif 361 #ifdef USE_IEEE80211 362 { "ieee80211", AF_UNSPEC, ieee80211_status, NULL, NULL, }, /* XXX not real!! */ 363 #endif 364 #endif 365 { 0, 0, 0, 0 } 366 }; 367 368 /* 369 * Expand the compacted form of addresses as returned via the 370 * configuration read via sysctl(). 371 */ 372 373 #define ROUNDUP(a) \ 374 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 375 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 376 377 void 378 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 379 { 380 struct sockaddr *sa; 381 int i; 382 383 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 384 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 385 if ((rtinfo->rti_addrs & (1 << i)) == 0) 386 continue; 387 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 388 ADVANCE(cp, sa); 389 } 390 } 391 392 393 void 394 usage(void) 395 { 396 #ifndef INET6 397 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 398 "usage: ifconfig interface address_family [address [dest_address]]", 399 " [parameters]", 400 " ifconfig -C", 401 " ifconfig interface create", 402 " ifconfig -a [-d] [-m] [-u] [address_family]", 403 " ifconfig -l [-d] [-u] [address_family]", 404 " ifconfig [-d] [-m] [-u]"); 405 #else 406 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 407 "usage: ifconfig [-L] interface address_family [address [dest_address]]", 408 " [parameters]", 409 " ifconfig -C", 410 " ifconfig interface create", 411 " ifconfig -a [-L] [-d] [-m] [-u] [address_family]", 412 " ifconfig -l [-d] [-u] [address_family]", 413 " ifconfig [-L] [-d] [-m] [-u]"); 414 #endif 415 exit(1); 416 } 417 418 int 419 main(int argc, char * const *argv) 420 { 421 int c; 422 int all, namesonly, downonly, uponly; 423 int need_nl = 0; 424 const struct afswtch *afp = 0; 425 int addrcount, ifindex; 426 struct if_msghdr *ifm, *nextifm; 427 struct ifa_msghdr *ifam; 428 struct sockaddr_dl *sdl; 429 char *buf, *lim, *next; 430 431 size_t needed; 432 int mib[6]; 433 434 /* Parse leading line options */ 435 all = downonly = uponly = namesonly = 0; 436 while ((c = getopt(argc, argv, "adlmuC" 437 #ifdef INET6 438 "L" 439 #endif 440 )) != -1) { 441 switch (c) { 442 case 'a': /* scan all interfaces */ 443 all++; 444 break; 445 case 'd': /* restrict scan to "down" interfaces */ 446 downonly++; 447 break; 448 case 'l': /* scan interface names only */ 449 namesonly++; 450 break; 451 case 'm': /* show media choices in status */ 452 supmedia = 1; 453 break; 454 case 'u': /* restrict scan to "up" interfaces */ 455 uponly++; 456 break; 457 case 'C': 458 listcloners = 1; 459 break; 460 #ifdef INET6 461 case 'L': 462 ip6lifetime++; /* print IPv6 address lifetime */ 463 break; 464 #endif 465 default: 466 usage(); 467 break; 468 } 469 } 470 argc -= optind; 471 argv += optind; 472 473 if (listcloners) { 474 /* -C must be solitary */ 475 if (all || supmedia || uponly || downonly || namesonly || 476 argc > 0) 477 usage(); 478 479 list_cloners(); 480 exit(0); 481 } 482 483 /* -l cannot be used with -a or -m */ 484 if (namesonly && (all || supmedia)) 485 usage(); 486 487 /* nonsense.. */ 488 if (uponly && downonly) 489 usage(); 490 491 /* no arguments is equivalent to '-a' */ 492 if (!namesonly && argc < 1) 493 all = 1; 494 495 /* -a and -l allow an address family arg to limit the output */ 496 if (all || namesonly) { 497 if (argc > 1) 498 usage(); 499 500 ifindex = 0; 501 if (argc == 1) { 502 for (afp = afs; afp->af_name; afp++) 503 if (strcmp(afp->af_name, *argv) == 0) { 504 argc--, argv++; 505 break; 506 } 507 if (afp->af_name == NULL) 508 usage(); 509 /* leave with afp non-zero */ 510 } 511 } else { 512 /* not listing, need an argument */ 513 if (argc < 1) 514 usage(); 515 516 strncpy(name, *argv, sizeof(name)); 517 argc--, argv++; 518 519 /* check and maybe load support for this interface */ 520 ifmaybeload(name); 521 522 /* 523 * NOTE: We must special-case the `create' command right 524 * here as we would otherwise fail when trying to find 525 * the interface. 526 */ 527 if (argc > 0 && (strcmp(argv[0], "create") == 0 || 528 strcmp(argv[0], "plumb") == 0)) { 529 clone_create(); 530 argc--, argv++; 531 if (argc == 0) 532 goto end; 533 } 534 ifindex = if_nametoindex(name); 535 if (ifindex == 0) 536 errx(1, "interface %s does not exist", name); 537 } 538 539 /* Check for address family */ 540 if (argc > 0) { 541 for (afp = afs; afp->af_name; afp++) 542 if (strcmp(afp->af_name, *argv) == 0) { 543 argc--, argv++; 544 break; 545 } 546 if (afp->af_name == NULL) 547 afp = NULL; /* not a family, NULL */ 548 } 549 550 mib[0] = CTL_NET; 551 mib[1] = PF_ROUTE; 552 mib[2] = 0; 553 mib[3] = 0; /* address family */ 554 mib[4] = NET_RT_IFLIST; 555 mib[5] = ifindex; /* interface index */ 556 557 /* if particular family specified, only ask about it */ 558 if (afp) 559 mib[3] = afp->af_af; 560 561 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 562 errx(1, "iflist-sysctl-estimate"); 563 if ((buf = malloc(needed)) == NULL) 564 errx(1, "malloc"); 565 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 566 errx(1, "actual retrieval of interface table"); 567 lim = buf + needed; 568 569 next = buf; 570 while (next < lim) { 571 572 ifm = (struct if_msghdr *)next; 573 574 if (ifm->ifm_type == RTM_IFINFO) { 575 sdl = (struct sockaddr_dl *)(ifm + 1); 576 flags = ifm->ifm_flags; 577 } else { 578 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n"); 579 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO, 580 ifm->ifm_type); 581 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen); 582 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next, 583 lim); 584 exit (1); 585 } 586 587 next += ifm->ifm_msglen; 588 ifam = NULL; 589 addrcount = 0; 590 while (next < lim) { 591 592 nextifm = (struct if_msghdr *)next; 593 594 if (nextifm->ifm_type != RTM_NEWADDR) 595 break; 596 597 if (ifam == NULL) 598 ifam = (struct ifa_msghdr *)nextifm; 599 600 addrcount++; 601 next += nextifm->ifm_msglen; 602 } 603 strncpy(name, sdl->sdl_data, sdl->sdl_nlen); 604 name[sdl->sdl_nlen] = '\0'; 605 606 if (all || namesonly) { 607 int len; 608 609 /* sdl_data may not be terminated, don't use strlcpy */ 610 if ((len = sdl->sdl_nlen) > sizeof(name) - 1) 611 len = sizeof(name) - 1; 612 bcopy(sdl->sdl_data, name, len); 613 name[len] = 0; 614 615 if (uponly) 616 if ((flags & IFF_UP) == 0) 617 continue; /* not up */ 618 if (downonly) 619 if (flags & IFF_UP) 620 continue; /* not down */ 621 if (namesonly) { 622 if (afp == NULL || 623 afp->af_status != link_status || 624 sdl->sdl_type == IFT_ETHER) { 625 if (need_nl) 626 putchar(' '); 627 fputs(name, stdout); 628 need_nl++; 629 } 630 continue; 631 } 632 } 633 634 if (argc > 0) 635 ifconfig(argc, argv, afp); 636 else 637 status(afp, addrcount, sdl, ifm, ifam); 638 } 639 free(buf); 640 641 if (namesonly && need_nl > 0) 642 putchar('\n'); 643 end: 644 if (printname) 645 printf("%s\n", name); 646 647 exit (0); 648 } 649 650 651 int 652 ifconfig(int argc, char *const *argv, const struct afswtch *afp) 653 { 654 int s; 655 656 if (afp == NULL) 657 afp = &afs[0]; 658 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af; 659 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 660 661 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0) 662 err(1, "socket"); 663 664 while (argc > 0) { 665 register const struct cmd *p; 666 667 for (p = cmds; p->c_name; p++) 668 if (strcmp(*argv, p->c_name) == 0) 669 break; 670 if (p->c_name == 0 && setaddr) 671 p++; /* got src, do dst */ 672 if (p->c_func || p->c_func2) { 673 if (p->c_parameter == NEXTARG) { 674 if (argv[1] == NULL) 675 errx(1, "'%s' requires argument", 676 p->c_name); 677 (*p->c_func)(argv[1], 0, s, afp); 678 argc--, argv++; 679 } else if (p->c_parameter == NEXTARG2) { 680 if (argc < 3) 681 errx(1, "'%s' requires 2 arguments", 682 p->c_name); 683 (*p->c_func2)(argv[1], argv[2], s, afp); 684 argc -= 2, argv += 2; 685 } else 686 (*p->c_func)(*argv, p->c_parameter, s, afp); 687 } 688 argc--, argv++; 689 } 690 #ifdef INET6 691 if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) { 692 /* Aggregatable address architecture defines all prefixes 693 are 64. So, it is convenient to set prefixlen to 64 if 694 it is not specified. */ 695 setifprefixlen("64", 0, s, afp); 696 /* in6_getprefix("64", MASK) if MASK is available here... */ 697 } 698 #endif 699 #ifndef NO_IPX 700 if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) { 701 struct ipxip_req rq; 702 int size = sizeof(rq); 703 704 rq.rq_ipx = addreq.ifra_addr; 705 rq.rq_ip = addreq.ifra_dstaddr; 706 707 if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0) 708 Perror("Encapsulation Routing"); 709 } 710 #endif 711 if (ifr.ifr_addr.sa_family == AF_APPLETALK) 712 checkatrange((struct sockaddr_at *) &addreq.ifra_addr); 713 #ifdef NS 714 if (setipdst && ifr.ifr_addr.sa_family == AF_NS) { 715 struct nsip_req rq; 716 int size = sizeof(rq); 717 718 rq.rq_ns = addreq.ifra_addr; 719 rq.rq_ip = addreq.ifra_dstaddr; 720 721 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0) 722 Perror("Encapsulation Routing"); 723 } 724 #endif 725 if (clearaddr) { 726 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) { 727 warnx("interface %s cannot change %s addresses!", 728 name, afp->af_name); 729 clearaddr = NULL; 730 } 731 } 732 if (clearaddr) { 733 int ret; 734 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name); 735 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) { 736 if (errno == EADDRNOTAVAIL && (doalias >= 0)) { 737 /* means no previous address for interface */ 738 } else 739 Perror("ioctl (SIOCDIFADDR)"); 740 } 741 } 742 if (newaddr) { 743 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) { 744 warnx("interface %s cannot change %s addresses!", 745 name, afp->af_name); 746 newaddr = 0; 747 } 748 } 749 if (newaddr && (setaddr || setmask)) { 750 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name); 751 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0) 752 Perror("ioctl (SIOCAIFADDR)"); 753 } 754 close(s); 755 return(0); 756 } 757 #define RIDADDR 0 758 #define ADDR 1 759 #define MASK 2 760 #define DSTADDR 3 761 762 /*ARGSUSED*/ 763 void 764 setifaddr(const char *addr, int param, int s, const struct afswtch *afp) 765 { 766 if (*afp->af_getaddr == NULL) 767 return; 768 /* 769 * Delay the ioctl to set the interface addr until flags are all set. 770 * The address interpretation may depend on the flags, 771 * and the flags may change when the address is set. 772 */ 773 setaddr++; 774 if (doalias == 0 && afp->af_af != AF_LINK) 775 clearaddr = 1; 776 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR)); 777 } 778 779 void 780 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp) 781 { 782 struct addrinfo hints, *srcres, *dstres; 783 struct ifaliasreq addreq; 784 int ecode; 785 #ifdef INET6 786 struct in6_aliasreq in6_addreq; 787 #endif 788 789 memset(&hints, 0, sizeof(hints)); 790 hints.ai_family = afp->af_af; 791 792 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0) 793 errx(1, "error in parsing address string: %s", 794 gai_strerror(ecode)); 795 796 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0) 797 errx(1, "error in parsing address string: %s", 798 gai_strerror(ecode)); 799 800 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family) 801 errx(1, 802 "source and destination address families do not match"); 803 804 switch (srcres->ai_addr->sa_family) { 805 case AF_INET: 806 memset(&addreq, 0, sizeof(addreq)); 807 strncpy(addreq.ifra_name, name, IFNAMSIZ); 808 memcpy(&addreq.ifra_addr, srcres->ai_addr, 809 srcres->ai_addr->sa_len); 810 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, 811 dstres->ai_addr->sa_len); 812 813 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0) 814 warn("SIOCSIFPHYADDR"); 815 break; 816 817 #ifdef INET6 818 case AF_INET6: 819 memset(&in6_addreq, 0, sizeof(in6_addreq)); 820 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ); 821 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, 822 srcres->ai_addr->sa_len); 823 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr, 824 dstres->ai_addr->sa_len); 825 826 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0) 827 warn("SIOCSIFPHYADDR_IN6"); 828 break; 829 #endif /* INET6 */ 830 831 default: 832 warn("address family not supported"); 833 } 834 835 freeaddrinfo(srcres); 836 freeaddrinfo(dstres); 837 } 838 839 /* ARGSUSED */ 840 void 841 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp) 842 { 843 844 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0) 845 err(1, "SIOCDIFPHYADDR"); 846 } 847 848 void 849 setifnetmask(const char *addr, int dummy __unused, int s, 850 const struct afswtch *afp) 851 { 852 if (*afp->af_getaddr == NULL) 853 return; 854 setmask++; 855 (*afp->af_getaddr)(addr, MASK); 856 } 857 858 #ifdef INET6 859 void 860 setifprefixlen(const char *addr, int dummy __unused, int s, 861 const struct afswtch *afp) 862 { 863 if (*afp->af_getprefix) 864 (*afp->af_getprefix)(addr, MASK); 865 explicit_prefix = 1; 866 } 867 868 void 869 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused, 870 const struct afswtch *afp) 871 { 872 if (afp->af_af != AF_INET6) 873 err(1, "address flags can be set only for inet6 addresses"); 874 875 if (flag < 0) 876 in6_addreq.ifra_flags &= ~(-flag); 877 else 878 in6_addreq.ifra_flags |= flag; 879 } 880 881 void 882 setip6pltime(const char *seconds, int dummy __unused, int s, 883 const struct afswtch *afp) 884 { 885 setip6lifetime("pltime", seconds, s, afp); 886 } 887 888 void 889 setip6vltime(const char *seconds, int dummy __unused, int s, 890 const struct afswtch *afp) 891 { 892 setip6lifetime("vltime", seconds, s, afp); 893 } 894 895 void 896 setip6lifetime(const char *cmd, const char *val, int s, 897 const struct afswtch *afp) 898 { 899 time_t newval, t; 900 char *ep; 901 902 t = time(NULL); 903 newval = (time_t)strtoul(val, &ep, 0); 904 if (val == ep) 905 errx(1, "invalid %s", cmd); 906 if (afp->af_af != AF_INET6) 907 errx(1, "%s not allowed for the AF", cmd); 908 if (strcmp(cmd, "vltime") == 0) { 909 in6_addreq.ifra_lifetime.ia6t_expire = t + newval; 910 in6_addreq.ifra_lifetime.ia6t_vltime = newval; 911 } else if (strcmp(cmd, "pltime") == 0) { 912 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval; 913 in6_addreq.ifra_lifetime.ia6t_pltime = newval; 914 } 915 } 916 917 void 918 setip6eui64(const char *cmd, int dummy __unused, int s, 919 const struct afswtch *afp) 920 { 921 struct ifaddrs *ifap, *ifa; 922 const struct sockaddr_in6 *sin6 = NULL; 923 const struct in6_addr *lladdr = NULL; 924 struct in6_addr *in6; 925 926 if (afp->af_af != AF_INET6) 927 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd); 928 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr; 929 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) 930 errx(EXIT_FAILURE, "interface index is already filled"); 931 if (getifaddrs(&ifap) != 0) 932 err(EXIT_FAILURE, "getifaddrs"); 933 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 934 if (ifa->ifa_addr->sa_family == AF_INET6 && 935 strcmp(ifa->ifa_name, name) == 0) { 936 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr; 937 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 938 lladdr = &sin6->sin6_addr; 939 break; 940 } 941 } 942 } 943 if (!lladdr) 944 errx(EXIT_FAILURE, "could not determine link local address"); 945 946 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8); 947 948 freeifaddrs(ifap); 949 } 950 #endif 951 952 void 953 setifbroadaddr(const char *addr, int dummy __unused, int s, 954 const struct afswtch *afp) 955 { 956 if (*afp->af_getaddr == NULL) 957 return; 958 (*afp->af_getaddr)(addr, DSTADDR); 959 } 960 961 void 962 setifipdst(const char *addr, int dummy __unused, int s, 963 const struct afswtch *afp) 964 { 965 in_getaddr(addr, DSTADDR); 966 setipdst++; 967 clearaddr = 0; 968 newaddr = 0; 969 } 970 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr)) 971 972 void 973 notealias(const char *addr, int param, int s, const struct afswtch *afp) 974 { 975 if (setaddr && doalias == 0 && param < 0) 976 bcopy((caddr_t)rqtosa(af_addreq), 977 (caddr_t)rqtosa(af_ridreq), 978 rqtosa(af_addreq)->sa_len); 979 doalias = param; 980 if (param < 0) { 981 clearaddr = 1; 982 newaddr = 0; 983 } else 984 clearaddr = 0; 985 } 986 987 /*ARGSUSED*/ 988 void 989 setifdstaddr(const char *addr, int param __unused, int s, 990 const struct afswtch *afp) 991 { 992 if (*afp->af_getaddr == NULL) 993 return; 994 (*afp->af_getaddr)(addr, DSTADDR); 995 } 996 997 /* 998 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion 999 * of the ifreq structure, which may confuse other parts of ifconfig. 1000 * Make a private copy so we can avoid that. 1001 */ 1002 void 1003 setifflags(const char *vname, int value, int s, const struct afswtch *afp) 1004 { 1005 struct ifreq my_ifr; 1006 1007 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq)); 1008 1009 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) { 1010 Perror("ioctl (SIOCGIFFLAGS)"); 1011 exit(1); 1012 } 1013 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name)); 1014 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16); 1015 1016 if (value < 0) { 1017 value = -value; 1018 flags &= ~value; 1019 } else 1020 flags |= value; 1021 my_ifr.ifr_flags = flags & 0xffff; 1022 my_ifr.ifr_flagshigh = flags >> 16; 1023 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0) 1024 Perror(vname); 1025 } 1026 1027 void 1028 setifcap(const char *vname, int value, int s, const struct afswtch *afp) 1029 { 1030 1031 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) { 1032 Perror("ioctl (SIOCGIFCAP)"); 1033 exit(1); 1034 } 1035 flags = ifr.ifr_curcap; 1036 if (value < 0) { 1037 value = -value; 1038 flags &= ~value; 1039 } else 1040 flags |= value; 1041 ifr.ifr_reqcap = flags; 1042 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0) 1043 Perror(vname); 1044 } 1045 1046 void 1047 setifmetric(const char *val, int dummy __unused, int s, 1048 const struct afswtch *afp) 1049 { 1050 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1051 ifr.ifr_metric = atoi(val); 1052 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0) 1053 warn("ioctl (set metric)"); 1054 } 1055 1056 void 1057 setifmtu(const char *val, int dummy __unused, int s, const struct afswtch *afp) 1058 { 1059 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1060 ifr.ifr_mtu = atoi(val); 1061 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0) 1062 warn("ioctl (set mtu)"); 1063 } 1064 1065 void 1066 setifname(const char *val, int dummy __unused, int s, 1067 const struct afswtch *afp) 1068 { 1069 char *newname; 1070 1071 newname = strdup(val); 1072 1073 ifr.ifr_data = newname; 1074 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) { 1075 warn("ioctl (set name)"); 1076 free(newname); 1077 return; 1078 } 1079 strlcpy(name, newname, sizeof(name)); 1080 1081 free(newname); 1082 1083 /* 1084 * Even if we just created the interface, we don't need to print 1085 * its name because we just nailed it down separately. 1086 */ 1087 printname = 0; 1088 } 1089 1090 #define IFFBITS \ 1091 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \ 1092 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \ 1093 "\20MULTICAST" 1094 1095 #define IFCAPBITS \ 1096 "\003\1rxcsum\2txcsum\3netcons\7polling" 1097 1098 /* 1099 * Print the status of the interface. If an address family was 1100 * specified, show it and it only; otherwise, show them all. 1101 */ 1102 void 1103 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl, 1104 struct if_msghdr *ifm, struct ifa_msghdr *ifam) 1105 { 1106 const struct afswtch *p = NULL; 1107 struct rt_addrinfo info; 1108 int allfamilies, s; 1109 struct ifstat ifs; 1110 1111 if (afp == NULL) { 1112 allfamilies = 1; 1113 afp = &afs[0]; 1114 } else 1115 allfamilies = 0; 1116 1117 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af; 1118 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 1119 1120 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0) 1121 err(1, "socket"); 1122 1123 printf("%s: ", name); 1124 printb("flags", flags, IFFBITS); 1125 if (ifm->ifm_data.ifi_metric) 1126 printf(" metric %ld", ifm->ifm_data.ifi_metric); 1127 if (ifm->ifm_data.ifi_mtu) 1128 printf(" mtu %ld", ifm->ifm_data.ifi_mtu); 1129 putchar('\n'); 1130 1131 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) { 1132 if (ifr.ifr_curcap != 0) { 1133 printb("\toptions", ifr.ifr_curcap, IFCAPBITS); 1134 putchar('\n'); 1135 } 1136 if (supmedia && ifr.ifr_reqcap != 0) { 1137 printf("\tcapability list:\n"); 1138 printb("\t\t", ifr.ifr_reqcap, IFCAPBITS); 1139 putchar('\n'); 1140 } 1141 } 1142 1143 tunnel_status(s); 1144 1145 while (addrcount > 0) { 1146 1147 info.rti_addrs = ifam->ifam_addrs; 1148 1149 /* Expand the compacted addresses */ 1150 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 1151 &info); 1152 1153 if (!allfamilies) { 1154 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) { 1155 p = afp; 1156 (*p->af_status)(s, &info); 1157 } 1158 } else for (p = afs; p->af_name; p++) { 1159 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family) 1160 (*p->af_status)(s, &info); 1161 } 1162 addrcount--; 1163 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen); 1164 } 1165 if (allfamilies || afp->af_status == link_status) 1166 link_status(s, (struct rt_addrinfo *)sdl); 1167 #ifdef USE_IF_MEDIA 1168 if (allfamilies || afp->af_status == media_status) 1169 media_status(s, NULL); 1170 #endif 1171 #ifdef USE_VLANS 1172 if (allfamilies || afp->af_status == vlan_status) 1173 vlan_status(s, NULL); 1174 #endif 1175 #ifdef USE_IEEE80211 1176 if (allfamilies || afp->af_status == ieee80211_status) 1177 ieee80211_status(s, NULL); 1178 #endif 1179 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name); 1180 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) 1181 printf("%s", ifs.ascii); 1182 1183 if (!allfamilies && !p && afp->af_status != media_status && 1184 afp->af_status != link_status 1185 #ifdef USE_VLANS 1186 && afp->af_status != vlan_status 1187 #endif 1188 ) 1189 warnx("%s has no %s interface address!", name, afp->af_name); 1190 1191 close(s); 1192 return; 1193 } 1194 1195 void 1196 tunnel_status(int s) 1197 { 1198 char psrcaddr[NI_MAXHOST]; 1199 char pdstaddr[NI_MAXHOST]; 1200 u_long srccmd, dstcmd; 1201 struct ifreq *ifrp; 1202 const char *ver = ""; 1203 #ifdef NI_WITHSCOPEID 1204 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID; 1205 #else 1206 const int niflag = NI_NUMERICHOST; 1207 #endif 1208 #ifdef INET6 1209 struct in6_ifreq in6_ifr; 1210 int s6; 1211 #endif /* INET6 */ 1212 1213 psrcaddr[0] = pdstaddr[0] = '\0'; 1214 1215 #ifdef INET6 1216 memset(&in6_ifr, 0, sizeof(in6_ifr)); 1217 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ); 1218 s6 = socket(AF_INET6, SOCK_DGRAM, 0); 1219 if (s6 < 0) { 1220 srccmd = SIOCGIFPSRCADDR; 1221 dstcmd = SIOCGIFPDSTADDR; 1222 ifrp = 𝔦 1223 } else { 1224 close(s6); 1225 srccmd = SIOCGIFPSRCADDR_IN6; 1226 dstcmd = SIOCGIFPDSTADDR_IN6; 1227 ifrp = (struct ifreq *)&in6_ifr; 1228 } 1229 #else /* INET6 */ 1230 srccmd = SIOCGIFPSRCADDR; 1231 dstcmd = SIOCGIFPDSTADDR; 1232 ifrp = 𝔦 1233 #endif /* INET6 */ 1234 1235 if (ioctl(s, srccmd, (caddr_t)ifrp) < 0) 1236 return; 1237 #ifdef INET6 1238 if (ifrp->ifr_addr.sa_family == AF_INET6) 1239 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr); 1240 #endif 1241 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len, 1242 psrcaddr, sizeof(psrcaddr), 0, 0, niflag); 1243 #ifdef INET6 1244 if (ifrp->ifr_addr.sa_family == AF_INET6) 1245 ver = "6"; 1246 #endif 1247 1248 if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0) 1249 return; 1250 #ifdef INET6 1251 if (ifrp->ifr_addr.sa_family == AF_INET6) 1252 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr); 1253 #endif 1254 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len, 1255 pdstaddr, sizeof(pdstaddr), 0, 0, niflag); 1256 1257 printf("\ttunnel inet%s %s --> %s\n", ver, 1258 psrcaddr, pdstaddr); 1259 } 1260 1261 void 1262 in_status(int s __unused, struct rt_addrinfo *info) 1263 { 1264 struct sockaddr_in *sin, null_sin; 1265 1266 memset(&null_sin, 0, sizeof(null_sin)); 1267 1268 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA]; 1269 printf("\tinet %s ", inet_ntoa(sin->sin_addr)); 1270 1271 if (flags & IFF_POINTOPOINT) { 1272 /* note RTAX_BRD overlap with IFF_BROADCAST */ 1273 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD]; 1274 if (!sin) 1275 sin = &null_sin; 1276 printf("--> %s ", inet_ntoa(sin->sin_addr)); 1277 } 1278 1279 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK]; 1280 if (!sin) 1281 sin = &null_sin; 1282 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr)); 1283 1284 if (flags & IFF_BROADCAST) { 1285 /* note RTAX_BRD overlap with IFF_POINTOPOINT */ 1286 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD]; 1287 if (sin && sin->sin_addr.s_addr != 0) 1288 printf("broadcast %s", inet_ntoa(sin->sin_addr)); 1289 } 1290 putchar('\n'); 1291 } 1292 1293 #ifdef INET6 1294 void 1295 in6_fillscopeid(struct sockaddr_in6 *sin6) 1296 { 1297 #if defined(__KAME__) && defined(KAME_SCOPEID) 1298 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 1299 sin6->sin6_scope_id = 1300 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]); 1301 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0; 1302 } 1303 #endif 1304 } 1305 1306 void 1307 in6_status(int s __unused, struct rt_addrinfo *info) 1308 { 1309 struct sockaddr_in6 *sin, null_sin; 1310 struct in6_ifreq ifr6; 1311 int s6; 1312 u_int32_t flags6; 1313 struct in6_addrlifetime lifetime; 1314 time_t t = time(NULL); 1315 int error; 1316 u_int32_t scopeid; 1317 1318 memset(&null_sin, 0, sizeof(null_sin)); 1319 1320 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA]; 1321 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name)); 1322 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1323 perror("ifconfig: socket"); 1324 return; 1325 } 1326 ifr6.ifr_addr = *sin; 1327 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) { 1328 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)"); 1329 close(s6); 1330 return; 1331 } 1332 flags6 = ifr6.ifr_ifru.ifru_flags6; 1333 memset(&lifetime, 0, sizeof(lifetime)); 1334 ifr6.ifr_addr = *sin; 1335 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) { 1336 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)"); 1337 close(s6); 1338 return; 1339 } 1340 lifetime = ifr6.ifr_ifru.ifru_lifetime; 1341 close(s6); 1342 1343 /* XXX: embedded link local addr check */ 1344 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) && 1345 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) { 1346 u_short index; 1347 1348 index = *(u_short *)&sin->sin6_addr.s6_addr[2]; 1349 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0; 1350 if (sin->sin6_scope_id == 0) 1351 sin->sin6_scope_id = ntohs(index); 1352 } 1353 scopeid = sin->sin6_scope_id; 1354 1355 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf, 1356 sizeof(addr_buf), NULL, 0, 1357 NI_NUMERICHOST|NI_WITHSCOPEID); 1358 if (error != 0) 1359 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, 1360 sizeof(addr_buf)); 1361 printf("\tinet6 %s ", addr_buf); 1362 1363 if (flags & IFF_POINTOPOINT) { 1364 /* note RTAX_BRD overlap with IFF_BROADCAST */ 1365 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD]; 1366 /* 1367 * some of the interfaces do not have valid destination 1368 * address. 1369 */ 1370 if (sin && sin->sin6_family == AF_INET6) { 1371 int error; 1372 1373 /* XXX: embedded link local addr check */ 1374 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) && 1375 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) { 1376 u_short index; 1377 1378 index = *(u_short *)&sin->sin6_addr.s6_addr[2]; 1379 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0; 1380 if (sin->sin6_scope_id == 0) 1381 sin->sin6_scope_id = ntohs(index); 1382 } 1383 1384 error = getnameinfo((struct sockaddr *)sin, 1385 sin->sin6_len, addr_buf, 1386 sizeof(addr_buf), NULL, 0, 1387 NI_NUMERICHOST|NI_WITHSCOPEID); 1388 if (error != 0) 1389 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, 1390 sizeof(addr_buf)); 1391 printf("--> %s ", addr_buf); 1392 } 1393 } 1394 1395 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK]; 1396 if (!sin) 1397 sin = &null_sin; 1398 printf("prefixlen %d ", prefix(&sin->sin6_addr, 1399 sizeof(struct in6_addr))); 1400 1401 if ((flags6 & IN6_IFF_ANYCAST) != 0) 1402 printf("anycast "); 1403 if ((flags6 & IN6_IFF_TENTATIVE) != 0) 1404 printf("tentative "); 1405 if ((flags6 & IN6_IFF_DUPLICATED) != 0) 1406 printf("duplicated "); 1407 if ((flags6 & IN6_IFF_DETACHED) != 0) 1408 printf("detached "); 1409 if ((flags6 & IN6_IFF_DEPRECATED) != 0) 1410 printf("deprecated "); 1411 if ((flags6 & IN6_IFF_AUTOCONF) != 0) 1412 printf("autoconf "); 1413 if ((flags6 & IN6_IFF_TEMPORARY) != 0) 1414 printf("temporary "); 1415 1416 if (scopeid) 1417 printf("scopeid 0x%x ", scopeid); 1418 1419 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) { 1420 printf("pltime "); 1421 if (lifetime.ia6t_preferred) { 1422 printf("%s ", lifetime.ia6t_preferred < t 1423 ? "0" : sec2str(lifetime.ia6t_preferred - t)); 1424 } else 1425 printf("infty "); 1426 1427 printf("vltime "); 1428 if (lifetime.ia6t_expire) { 1429 printf("%s ", lifetime.ia6t_expire < t 1430 ? "0" : sec2str(lifetime.ia6t_expire - t)); 1431 } else 1432 printf("infty "); 1433 } 1434 1435 putchar('\n'); 1436 } 1437 #endif /*INET6*/ 1438 1439 #ifndef NO_IPX 1440 void 1441 ipx_status(int s __unused, struct rt_addrinfo *info) 1442 { 1443 struct sockaddr_ipx *sipx, null_sipx; 1444 1445 memset(&null_sipx, 0, sizeof(null_sipx)); 1446 1447 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA]; 1448 printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr)); 1449 1450 if (flags & IFF_POINTOPOINT) { 1451 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD]; 1452 if (!sipx) 1453 sipx = &null_sipx; 1454 printf("--> %s ", ipx_ntoa(sipx->sipx_addr)); 1455 } 1456 putchar('\n'); 1457 } 1458 #endif 1459 1460 void 1461 at_status(int s __unused, struct rt_addrinfo *info) 1462 { 1463 struct sockaddr_at *sat, null_sat; 1464 struct netrange *nr; 1465 1466 memset(&null_sat, 0, sizeof(null_sat)); 1467 1468 sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA]; 1469 nr = &sat->sat_range.r_netrange; 1470 printf("\tatalk %d.%d range %d-%d phase %d", 1471 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node, 1472 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase); 1473 if (flags & IFF_POINTOPOINT) { 1474 /* note RTAX_BRD overlap with IFF_BROADCAST */ 1475 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD]; 1476 if (!sat) 1477 sat = &null_sat; 1478 printf("--> %d.%d", 1479 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node); 1480 } 1481 if (flags & IFF_BROADCAST) { 1482 /* note RTAX_BRD overlap with IFF_POINTOPOINT */ 1483 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD]; 1484 if (sat) 1485 printf(" broadcast %d.%d", 1486 ntohs(sat->sat_addr.s_net), 1487 sat->sat_addr.s_node); 1488 } 1489 1490 putchar('\n'); 1491 } 1492 1493 #ifdef NS 1494 void 1495 xns_status(int s __unused, struct rt_addrinfo *info) 1496 { 1497 struct sockaddr_ns *sns, null_sns; 1498 1499 memset(&null_sns, 0, sizeof(null_sns)); 1500 1501 sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA]; 1502 printf("\tns %s ", ns_ntoa(sns->sns_addr)); 1503 1504 if (flags & IFF_POINTOPOINT) { 1505 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD]; 1506 if (!sns) 1507 sns = &null_sns; 1508 printf("--> %s ", ns_ntoa(sns->sns_addr)); 1509 } 1510 1511 putchar('\n'); 1512 close(s); 1513 } 1514 #endif 1515 1516 1517 void 1518 link_status(int s __unused, struct rt_addrinfo *info) 1519 { 1520 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info; 1521 1522 if (sdl->sdl_alen > 0) { 1523 if (sdl->sdl_type == IFT_ETHER && 1524 sdl->sdl_alen == ETHER_ADDR_LEN) 1525 printf("\tether %s\n", 1526 ether_ntoa((struct ether_addr *)LLADDR(sdl))); 1527 else { 1528 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 1529 1530 printf("\tlladdr %s\n", link_ntoa(sdl) + n); 1531 } 1532 } 1533 } 1534 1535 void 1536 Perror(const char *cmd) 1537 { 1538 switch (errno) { 1539 1540 case ENXIO: 1541 errx(1, "%s: no such interface", cmd); 1542 break; 1543 1544 case EPERM: 1545 errx(1, "%s: permission denied", cmd); 1546 break; 1547 1548 default: 1549 err(1, "%s", cmd); 1550 } 1551 } 1552 1553 #define SIN(x) ((struct sockaddr_in *) &(x)) 1554 struct sockaddr_in *sintab[] = { 1555 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr), 1556 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)}; 1557 1558 void 1559 in_getaddr(const char *s, int which) 1560 { 1561 register struct sockaddr_in *sin = sintab[which]; 1562 struct hostent *hp; 1563 struct netent *np; 1564 1565 sin->sin_len = sizeof(*sin); 1566 if (which != MASK) 1567 sin->sin_family = AF_INET; 1568 1569 if (which == ADDR) { 1570 char *p = NULL; 1571 1572 if((p = strrchr(s, '/')) != NULL) { 1573 /* address is `name/masklen' */ 1574 int masklen; 1575 int ret; 1576 struct sockaddr_in *min = sintab[MASK]; 1577 *p = '\0'; 1578 ret = sscanf(p+1, "%u", &masklen); 1579 if(ret != 1 || (masklen < 0 || masklen > 32)) { 1580 *p = '/'; 1581 errx(1, "%s: bad value", s); 1582 } 1583 min->sin_len = sizeof(*min); 1584 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) & 1585 0xffffffff); 1586 } 1587 } 1588 1589 if (inet_aton(s, &sin->sin_addr)) 1590 return; 1591 if ((hp = gethostbyname(s)) != 0) 1592 bcopy(hp->h_addr, (char *)&sin->sin_addr, 1593 MIN(hp->h_length, sizeof(sin->sin_addr))); 1594 else if ((np = getnetbyname(s)) != 0) 1595 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 1596 else 1597 errx(1, "%s: bad value", s); 1598 } 1599 1600 #ifdef INET6 1601 #define SIN6(x) ((struct sockaddr_in6 *) &(x)) 1602 struct sockaddr_in6 *sin6tab[] = { 1603 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr), 1604 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)}; 1605 1606 void 1607 in6_getaddr(const char *s, int which) 1608 { 1609 register struct sockaddr_in6 *sin = sin6tab[which]; 1610 struct addrinfo hints, *res; 1611 int error = -1; 1612 1613 newaddr &= 1; 1614 1615 sin->sin6_len = sizeof(*sin); 1616 if (which != MASK) 1617 sin->sin6_family = AF_INET6; 1618 1619 if (which == ADDR) { 1620 char *p = NULL; 1621 if((p = strrchr(s, '/')) != NULL) { 1622 *p = '\0'; 1623 in6_getprefix(p + 1, MASK); 1624 explicit_prefix = 1; 1625 } 1626 } 1627 1628 if (sin->sin6_family == AF_INET6) { 1629 bzero(&hints, sizeof(struct addrinfo)); 1630 hints.ai_family = AF_INET6; 1631 error = getaddrinfo(s, NULL, &hints, &res); 1632 } 1633 if (error != 0) { 1634 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1) 1635 errx(1, "%s: bad value", s); 1636 } else 1637 bcopy(res->ai_addr, sin, res->ai_addrlen); 1638 } 1639 1640 void 1641 in6_getprefix(const char *plen, int which) 1642 { 1643 register struct sockaddr_in6 *sin = sin6tab[which]; 1644 register u_char *cp; 1645 int len = atoi(plen); 1646 1647 if ((len < 0) || (len > 128)) 1648 errx(1, "%s: bad value", plen); 1649 sin->sin6_len = sizeof(*sin); 1650 if (which != MASK) 1651 sin->sin6_family = AF_INET6; 1652 if ((len == 0) || (len == 128)) { 1653 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr)); 1654 return; 1655 } 1656 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr)); 1657 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8) 1658 *cp++ = 0xff; 1659 *cp = 0xff << (8 - len); 1660 } 1661 #endif 1662 1663 /* 1664 * Print a value a la the %b format of the kernel's printf 1665 */ 1666 void 1667 printb(const char *s, register unsigned v, register const char *bits) 1668 { 1669 register int i, any = 0; 1670 register char c; 1671 1672 if (bits && *bits == 8) 1673 printf("%s=%o", s, v); 1674 else 1675 printf("%s=%x", s, v); 1676 bits++; 1677 if (bits) { 1678 putchar('<'); 1679 while ((i = *bits++) != '\0') { 1680 if (v & (1 << (i-1))) { 1681 if (any) 1682 putchar(','); 1683 any = 1; 1684 for (; (c = *bits) > 32; bits++) 1685 putchar(c); 1686 } else 1687 for (; *bits > 32; bits++) 1688 ; 1689 } 1690 putchar('>'); 1691 } 1692 } 1693 1694 #ifndef NO_IPX 1695 #define SIPX(x) ((struct sockaddr_ipx *) &(x)) 1696 struct sockaddr_ipx *sipxtab[] = { 1697 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr), 1698 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)}; 1699 1700 void 1701 ipx_getaddr(const char *addr, int which) 1702 { 1703 struct sockaddr_ipx *sipx = sipxtab[which]; 1704 1705 sipx->sipx_family = AF_IPX; 1706 sipx->sipx_len = sizeof(*sipx); 1707 sipx->sipx_addr = ipx_addr(addr); 1708 if (which == MASK) 1709 printf("Attempt to set IPX netmask will be ineffectual\n"); 1710 } 1711 #endif 1712 1713 void 1714 at_getaddr(const char *addr, int which) 1715 { 1716 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr; 1717 u_int net, node; 1718 1719 sat->sat_family = AF_APPLETALK; 1720 sat->sat_len = sizeof(*sat); 1721 if (which == MASK) 1722 errx(1, "AppleTalk does not use netmasks"); 1723 if (sscanf(addr, "%u.%u", &net, &node) != 2 1724 || net > 0xffff || node > 0xfe) 1725 errx(1, "%s: illegal address", addr); 1726 sat->sat_addr.s_net = htons(net); 1727 sat->sat_addr.s_node = node; 1728 } 1729 1730 void 1731 link_getaddr(const char *addr, int which) 1732 { 1733 char *temp; 1734 struct sockaddr_dl sdl; 1735 struct sockaddr *sa = &ridreq.ifr_addr; 1736 1737 if (which != ADDR) 1738 errx(1, "can't set link-level netmask or broadcast"); 1739 if ((temp = malloc(strlen(addr) + 1)) == NULL) 1740 errx(1, "malloc failed"); 1741 temp[0] = ':'; 1742 strcpy(temp + 1, addr); 1743 sdl.sdl_len = sizeof(sdl); 1744 link_addr(temp, &sdl); 1745 free(temp); 1746 if (sdl.sdl_alen > sizeof(sa->sa_data)) 1747 errx(1, "malformed link-level address"); 1748 sa->sa_family = AF_LINK; 1749 sa->sa_len = sdl.sdl_alen; 1750 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen); 1751 } 1752 1753 /* XXX FIXME -- should use strtoul for better parsing. */ 1754 void 1755 setatrange(const char *range, int dummy __unused, int s, 1756 const struct afswtch *afp) 1757 { 1758 u_short first = 123, last = 123; 1759 1760 if (sscanf(range, "%hu-%hu", &first, &last) != 2 1761 || first == 0 || first > 0xffff 1762 || last == 0 || last > 0xffff || first > last) 1763 errx(1, "%s: illegal net range: %u-%u", range, first, last); 1764 at_nr.nr_firstnet = htons(first); 1765 at_nr.nr_lastnet = htons(last); 1766 } 1767 1768 void 1769 setatphase(const char *phase, int dummy __unused, int s, 1770 const struct afswtch *afp) 1771 { 1772 if (!strcmp(phase, "1")) 1773 at_nr.nr_phase = 1; 1774 else if (!strcmp(phase, "2")) 1775 at_nr.nr_phase = 2; 1776 else 1777 errx(1, "%s: illegal phase", phase); 1778 } 1779 1780 void 1781 checkatrange(struct sockaddr_at *sat) 1782 { 1783 if (at_nr.nr_phase == 0) 1784 at_nr.nr_phase = 2; /* Default phase 2 */ 1785 if (at_nr.nr_firstnet == 0) 1786 at_nr.nr_firstnet = /* Default range of one */ 1787 at_nr.nr_lastnet = sat->sat_addr.s_net; 1788 printf("\tatalk %d.%d range %d-%d phase %d\n", 1789 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node, 1790 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase); 1791 if ((u_short) ntohs(at_nr.nr_firstnet) > 1792 (u_short) ntohs(sat->sat_addr.s_net) 1793 || (u_short) ntohs(at_nr.nr_lastnet) < 1794 (u_short) ntohs(sat->sat_addr.s_net)) 1795 errx(1, "AppleTalk address is not in range"); 1796 sat->sat_range.r_netrange = at_nr; 1797 } 1798 1799 #ifdef NS 1800 #define SNS(x) ((struct sockaddr_ns *) &(x)) 1801 struct sockaddr_ns *snstab[] = { 1802 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr), 1803 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)}; 1804 1805 void 1806 xns_getaddr(const char *addr, int which) 1807 { 1808 struct sockaddr_ns *sns = snstab[which]; 1809 1810 sns->sns_family = AF_NS; 1811 sns->sns_len = sizeof(*sns); 1812 sns->sns_addr = ns_addr(addr); 1813 if (which == MASK) 1814 printf("Attempt to set XNS netmask will be ineffectual\n"); 1815 } 1816 #endif 1817 1818 #ifdef INET6 1819 int 1820 prefix(void *val, int size) 1821 { 1822 register u_char *name = (u_char *)val; 1823 register int byte, bit, plen = 0; 1824 1825 for (byte = 0; byte < size; byte++, plen += 8) 1826 if (name[byte] != 0xff) 1827 break; 1828 if (byte == size) 1829 return (plen); 1830 for (bit = 7; bit != 0; bit--, plen++) 1831 if (!(name[byte] & (1 << bit))) 1832 break; 1833 for (; bit != 0; bit--) 1834 if (name[byte] & (1 << bit)) 1835 return(0); 1836 byte++; 1837 for (; byte < size; byte++) 1838 if (name[byte]) 1839 return(0); 1840 return (plen); 1841 } 1842 1843 static char * 1844 sec2str(time_t total) 1845 { 1846 static char result[256]; 1847 int days, hours, mins, secs; 1848 int first = 1; 1849 char *p = result; 1850 1851 if (0) { 1852 days = total / 3600 / 24; 1853 hours = (total / 3600) % 24; 1854 mins = (total / 60) % 60; 1855 secs = total % 60; 1856 1857 if (days) { 1858 first = 0; 1859 p += sprintf(p, "%dd", days); 1860 } 1861 if (!first || hours) { 1862 first = 0; 1863 p += sprintf(p, "%dh", hours); 1864 } 1865 if (!first || mins) { 1866 first = 0; 1867 p += sprintf(p, "%dm", mins); 1868 } 1869 sprintf(p, "%ds", secs); 1870 } else 1871 sprintf(result, "%lu", (unsigned long)total); 1872 1873 return(result); 1874 } 1875 #endif /*INET6*/ 1876 1877 void 1878 ifmaybeload(char *name) 1879 { 1880 struct module_stat mstat; 1881 int fileid, modid; 1882 char ifkind[35], *cp, *dp; 1883 1884 1885 /* turn interface and unit into module name */ 1886 strcpy(ifkind, "if_"); 1887 for (cp = name, dp = ifkind + 3; 1888 (*cp != 0) && !isdigit(*cp); cp++, dp++) 1889 *dp = *cp; 1890 *dp = 0; 1891 1892 /* scan files in kernel */ 1893 mstat.version = sizeof(struct module_stat); 1894 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { 1895 /* scan modules in file */ 1896 for (modid = kldfirstmod(fileid); modid > 0; 1897 modid = modfnext(modid)) { 1898 if (modstat(modid, &mstat) < 0) 1899 continue; 1900 /* strip bus name if present */ 1901 if ((cp = strchr(mstat.name, '/')) != NULL) { 1902 cp++; 1903 } else { 1904 cp = mstat.name; 1905 } 1906 /* already loaded? */ 1907 if (strncmp(name, cp, strlen(cp)) == 0 || 1908 strncmp(ifkind, cp, strlen(cp)) == 0) 1909 return; 1910 } 1911 } 1912 1913 /* not present, we should try to load it */ 1914 kldload(ifkind); 1915 } 1916 1917 void 1918 list_cloners(void) 1919 { 1920 struct if_clonereq ifcr; 1921 char *cp, *buf; 1922 int idx; 1923 int s; 1924 1925 s = socket(AF_INET, SOCK_DGRAM, 0); 1926 if (s == -1) 1927 err(1, "socket"); 1928 1929 memset(&ifcr, 0, sizeof(ifcr)); 1930 1931 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0) 1932 err(1, "SIOCIFGCLONERS for count"); 1933 1934 buf = malloc(ifcr.ifcr_total * IFNAMSIZ); 1935 if (buf == NULL) 1936 err(1, "unable to allocate cloner name buffer"); 1937 1938 ifcr.ifcr_count = ifcr.ifcr_total; 1939 ifcr.ifcr_buffer = buf; 1940 1941 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0) 1942 err(1, "SIOCIFGCLONERS for names"); 1943 1944 /* 1945 * In case some disappeared in the mean time, clamp it down. 1946 */ 1947 if (ifcr.ifcr_count > ifcr.ifcr_total) 1948 ifcr.ifcr_count = ifcr.ifcr_total; 1949 1950 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) { 1951 if (idx > 0) 1952 putchar(' '); 1953 printf("%s", cp); 1954 } 1955 1956 putchar('\n'); 1957 free(buf); 1958 } 1959 1960 void 1961 clone_create(void) 1962 { 1963 int s; 1964 1965 s = socket(AF_INET, SOCK_DGRAM, 0); 1966 if (s == -1) 1967 err(1, "socket"); 1968 1969 memset(&ifr, 0, sizeof(ifr)); 1970 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1971 if (ioctl(s, SIOCIFCREATE, &ifr) < 0) 1972 err(1, "SIOCIFCREATE"); 1973 1974 /* 1975 * If we get a different name back then we put in, we probably 1976 * want to print it out, but we might change our mind later so 1977 * we just signal our intrest and leave the printout for later. 1978 */ 1979 if (strcmp(name, ifr.ifr_name) != 0) { 1980 printname = 1; 1981 strlcpy(name, ifr.ifr_name, sizeof(name)); 1982 } 1983 1984 close(s); 1985 } 1986 1987 void 1988 clone_destroy(const char *val, int d, int s, const struct afswtch *rafp) 1989 { 1990 1991 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1992 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0) 1993 err(1, "SIOCIFDESTROY"); 1994 /* 1995 * If we create and destroy an interface in the same command, 1996 * there isn't any reason to print it's name. 1997 */ 1998 printname = 0; 1999 } 2000