1 /* $FreeBSD: src/usr.sbin/gifconfig/gifconfig.c,v 1.2.2.4 2002/08/30 14:23:39 sobomax Exp $ */ 2 /* $KAME: gifconfig.c,v 1.14 2001/01/01 04:04:56 jinmei Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * gifconfig, derived from ifconfig 35 * 36 * @(#) Copyright (c) 1983, 1993\n\ 37 * The Regents of the University of California. All rights reserved.\n 38 * 39 * @(#)ifconfig.c 8.2 (Berkeley) 2/16/94 40 */ 41 42 /* 43 * 951109 - Andrew@pubnix.net - Changed to iterative buffer growing mechanism 44 * for ifconfig -a so all interfaces are queried. 45 * 46 * 960101 - peter@freebsd.org - Blow away the SIOCGIFCONF code and use 47 * sysctl() to get the structured interface conf 48 * and parse the messages in there. REALLY UGLY! 49 */ 50 51 #include <sys/param.h> 52 #include <sys/socket.h> 53 #include <sys/ioctl.h> 54 #include <sys/sysctl.h> 55 56 #include <net/if.h> 57 #if defined(__DragonFly__) 58 #include <net/if_var.h> 59 #endif /* __DragonFly__ */ 60 #include <net/if_dl.h> 61 #include <net/if_types.h> 62 #include <net/route.h> 63 #include <netinet/in.h> 64 #include <netinet/in_var.h> 65 #include <arpa/inet.h> 66 #include <netdb.h> 67 68 #include <sys/protosw.h> 69 70 #include <ctype.h> 71 #include <err.h> 72 #include <errno.h> 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <unistd.h> 77 #include <nlist.h> 78 #include <kvm.h> 79 #include <fcntl.h> 80 81 static struct ifreq ifr; 82 static struct ifaliasreq addreq; 83 #ifdef INET6 84 static struct in6_ifreq in6_ifr; 85 static struct in6_aliasreq in6_addreq; 86 #endif 87 88 static char name[32]; 89 static int flags; 90 static int metric; 91 static int mtu; 92 static int setpsrc = 0; 93 static int newaddr = 0; 94 static int s; 95 96 static void setifpsrc(char *, int); 97 static void setifpdst(char *, int); 98 static void setifflags(char *, int); 99 #ifdef SIOCDIFPHYADDR 100 static void delifaddrs(char *, int); 101 #endif 102 103 #define NEXTARG 0xffffff 104 105 static struct cmd { 106 char *c_name; 107 int c_parameter; /* NEXTARG means next argv */ 108 void (*c_func)(char *, int); 109 } cmds[] = { 110 { "up", IFF_UP, setifflags } , 111 { "down", -IFF_UP, setifflags }, 112 #ifdef SIOCDIFPHYADDR 113 { "delete", 0, delifaddrs }, 114 #endif 115 { 0, 0, setifpsrc }, 116 { 0, 0, setifpdst }, 117 }; 118 119 /* 120 * XNS support liberally adapted from code written at the University of 121 * Maryland principally by James O'Toole and Chris Torek. 122 */ 123 static void status(void); 124 static void phys_status(int); 125 static void in_status(int); 126 #ifdef INET6 127 static void in6_status(int); 128 #endif 129 static void ether_status(int); 130 static void Perror(char *) __dead2; 131 static void in_getaddr(char *, int); 132 #ifdef INET6 133 static void in6_getaddr(char *, int); 134 static void in6_getprefix(char *, int); 135 #endif 136 static void printb(char *, unsigned int, char *); 137 static int prefix(void *, int); 138 139 /* Known address families */ 140 static struct afswtch { 141 char *af_name; 142 short af_af; 143 void (*af_status)(int); 144 void (*af_getaddr)(char *, int); 145 void (*af_getprefix)(char *, int); 146 u_long af_pifaddr; 147 caddr_t af_addreq; 148 caddr_t af_req; 149 } afs[] = { 150 #define C(x) ((caddr_t) &x) 151 { "inet", AF_INET, in_status, in_getaddr, 0, 152 SIOCSIFPHYADDR, C(addreq), C(ifr) }, 153 #ifdef INET6 154 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix, 155 SIOCSIFPHYADDR_IN6, C(in6_addreq), C(in6_ifr) }, 156 #endif 157 { "ether", AF_INET, ether_status, NULL, NULL }, /* XXX not real!! */ 158 { 0, 0, 0, 0, 0 } 159 }; 160 161 static struct afswtch *afp = NULL; /*the address family being set or asked about*/ 162 163 static void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *); 164 static int ifconfig(int argc, char *argv[], int af, struct afswtch *rafp); 165 166 static void 167 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 168 { 169 struct sockaddr *sa; 170 int i; 171 172 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 173 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 174 if ((rtinfo->rti_addrs & (1 << i)) == 0) 175 continue; 176 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 177 RT_ADVANCE(cp, sa); 178 } 179 } 180 181 182 /* 183 * Grunge for new-style sysctl() decoding.. :-( 184 * Apologies to the world for committing gross things like this in 1996.. 185 */ 186 static struct if_msghdr *ifm; 187 static struct ifa_msghdr *ifam; 188 static struct sockaddr_dl *sdl; 189 static struct rt_addrinfo info; 190 static char *buf, *lim, *next; 191 192 193 int 194 main(int argc, char **argv) 195 { 196 int af = AF_INET; 197 struct afswtch *rafp = NULL; 198 size_t needed; 199 int mib[6]; 200 int all; 201 202 if (argc < 2) { 203 fprintf(stderr, 204 "usage: gifconfig interface [af] [physsrc physdst]\n"); 205 #ifdef SIOCDIFPHYADDR 206 fprintf(stderr, 207 " gifconfig interface delete\n"); 208 #endif 209 fprintf(stderr, 210 " gifconfig -a\n"); 211 exit(1); 212 } 213 argc--, argv++; 214 strncpy(name, *argv, sizeof(name)); 215 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 216 argc--, argv++; 217 if (argc > 0) { 218 for (afp = rafp = afs; rafp->af_name; rafp++) 219 if (strcmp(rafp->af_name, *argv) == 0) { 220 afp = rafp; argc--; argv++; 221 break; 222 } 223 rafp = afp; 224 af = ifr.ifr_addr.sa_family = rafp->af_af; 225 } 226 227 mib[0] = CTL_NET; 228 mib[1] = PF_ROUTE; 229 mib[2] = 0; 230 mib[3] = 0; /* address family */ 231 mib[4] = NET_RT_IFLIST; 232 mib[5] = 0; 233 234 /* if particular family specified, only ask about it */ 235 if (afp) { 236 mib[3] = afp->af_af; 237 } 238 239 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 240 errx(1, "iflist-sysctl-estimate"); 241 if ((buf = malloc(needed)) == NULL) 242 errx(1, "malloc"); 243 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 244 errx(1, "actual retrieval of interface table"); 245 lim = buf + needed; 246 247 all = 0; 248 if (strcmp(name, "-a") == 0) 249 all = 1; /* All interfaces */ 250 else if (strcmp(name, "-au") == 0) 251 all = 2; /* All IFF_UPinterfaces */ 252 else if (strcmp(name, "-ad") == 0) 253 all = 3; /* All !IFF_UP interfaces */ 254 255 for (next = buf; next < lim; next += ifm->ifm_msglen) { 256 257 ifm = (struct if_msghdr *)next; 258 259 /* XXX: Swallow up leftover NEWADDR messages */ 260 if (ifm->ifm_type == RTM_NEWADDR) 261 continue; 262 263 if (ifm->ifm_type == RTM_IFINFO) { 264 sdl = (struct sockaddr_dl *)(ifm + 1); 265 flags = ifm->ifm_flags; 266 } else { 267 errx(1, "out of sync parsing NET_RT_IFLIST"); 268 } 269 270 switch(all) { 271 case -1: 272 case 0: 273 if (strlen(name) != sdl->sdl_nlen) 274 continue; /* not same len */ 275 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0) 276 continue; /* not same name */ 277 break; 278 case 1: 279 break; /* always do it */ 280 case 2: 281 if ((flags & IFF_UP) == 0) 282 continue; /* not up */ 283 break; 284 case 3: 285 if (flags & IFF_UP) 286 continue; /* not down */ 287 break; 288 } 289 290 /* 291 * Let's just do it for gif only 292 */ 293 if (sdl->sdl_type != IFT_GIF) { 294 if (all != 0) 295 continue; 296 297 fprintf(stderr, "gifconfig: %s is not gif.\n", 298 ifr.ifr_name); 299 exit(1); 300 } 301 302 if (all > 0) { 303 strncpy(name, sdl->sdl_data, sdl->sdl_nlen); 304 name[sdl->sdl_nlen] = '\0'; 305 } 306 307 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) { 308 perror("gifconfig: socket"); 309 exit(1); 310 } 311 312 ifconfig(argc,argv,af,rafp); 313 314 close(s); 315 316 if (all == 0) { 317 all = -1; /* flag it as 'done' */ 318 break; 319 } 320 } 321 free(buf); 322 323 if (all == 0) 324 errx(1, "interface %s does not exist", name); 325 326 327 exit (0); 328 } 329 330 331 static int 332 ifconfig(int argc, char **argv, int af, struct afswtch *rafp) 333 { 334 335 af = 0; /*fool gcc*/ 336 337 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 338 #ifdef INET6 339 strncpy(in6_ifr.ifr_name, name, sizeof in6_ifr.ifr_name); 340 #endif /* INET6 */ 341 342 if (ioctl(s, SIOCGIFMETRIC, (caddr_t)&ifr) < 0) 343 perror("ioctl (SIOCGIFMETRIC)"); 344 else 345 metric = ifr.ifr_metric; 346 347 #if defined(SIOCGIFMTU) && !defined(__OpenBSD__) 348 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) 349 perror("ioctl (SIOCGIFMTU)"); 350 else 351 mtu = ifr.ifr_mtu; 352 #else 353 mtu = 0; 354 #endif 355 356 if (argc == 0) { 357 status(); 358 return(0); 359 } 360 361 while (argc > 0) { 362 struct cmd *p; 363 364 for (p = cmds; p->c_name; p++) 365 if (strcmp(*argv, p->c_name) == 0) 366 break; 367 if (p->c_name == NULL && setpsrc) 368 p++; /* got src, do dst */ 369 if (p->c_func) { 370 if (p->c_parameter == NEXTARG) { 371 if (argv[1] == NULL) 372 errx(1, "'%s' requires argument", 373 p->c_name); 374 (*p->c_func)(argv[1], 0); 375 argc--, argv++; 376 } else 377 (*p->c_func)(*argv, p->c_parameter); 378 } 379 argc--, argv++; 380 } 381 if (newaddr) { 382 strncpy(rafp->af_addreq, name, sizeof ifr.ifr_name); 383 if (ioctl(s, rafp->af_pifaddr, rafp->af_addreq) < 0) 384 Perror("ioctl (SIOCSIFPHYADDR)"); 385 } 386 else if (setpsrc) { 387 errx(1, "destination is not specified"); 388 } 389 return(0); 390 } 391 #define PSRC 0 392 #define PDST 1 393 394 /*ARGSUSED*/ 395 static void 396 setifpsrc(char *addr, int param) 397 { 398 param = 0; /*fool gcc*/ 399 (*afp->af_getaddr)(addr, PSRC); 400 setpsrc = 1; 401 } 402 403 /*ARGSUSED*/ 404 static void 405 setifpdst(char *addr, int param) 406 { 407 param = 0; /*fool gcc*/ 408 (*afp->af_getaddr)(addr, PDST); 409 newaddr = 1; 410 } 411 412 static void 413 setifflags(char *vname, int value) 414 { 415 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { 416 Perror("ioctl (SIOCGIFFLAGS)"); 417 exit(1); 418 } 419 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 420 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16); 421 422 if (value < 0) { 423 value = -value; 424 flags &= ~value; 425 } else 426 flags |= value; 427 ifr.ifr_flags = flags & 0xffff; 428 ifr.ifr_flagshigh = flags >> 16; 429 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) 430 Perror(vname); 431 } 432 433 #ifdef SIOCDIFPHYADDR 434 /* ARGSUSED */ 435 static void 436 delifaddrs(char *vname, int param) 437 { 438 param = 0; /* fool gcc */ 439 vname = NULL; /* ditto */ 440 441 if (ioctl(s, SIOCDIFPHYADDR, (caddr_t)&ifr) < 0) 442 err(1, "ioctl(SIOCDIFPHYADDR)"); 443 } 444 #endif 445 446 #define IFFBITS \ 447 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\ 448 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST" 449 450 /* 451 * Print the status of the interface. If an address family was 452 * specified, show it and it only; otherwise, show them all. 453 */ 454 static void 455 status(void) 456 { 457 struct afswtch *p = NULL; 458 char *mynext; 459 struct if_msghdr *myifm; 460 461 printf("%s: ", name); 462 printb("flags", flags, IFFBITS); 463 if (metric) 464 printf(" metric %d", metric); 465 if (mtu) 466 printf(" mtu %d", mtu); 467 putchar('\n'); 468 469 /* 470 * XXX: Sigh. This is bad, I know. At this point, we may have 471 * *zero* RTM_NEWADDR's, so we have to "feel the water" before 472 * incrementing the loop. One day, I might feel inspired enough 473 * to get the top level loop to pass a count down here so we 474 * dont have to mess with this. -Peter 475 */ 476 myifm = ifm; 477 478 while (1) { 479 480 mynext = next + ifm->ifm_msglen; 481 482 if (mynext >= lim) 483 break; 484 485 myifm = (struct if_msghdr *)mynext; 486 487 if (myifm->ifm_type != RTM_NEWADDR) 488 break; 489 490 next = mynext; 491 492 ifm = (struct if_msghdr *)next; 493 494 ifam = (struct ifa_msghdr *)myifm; 495 info.rti_addrs = ifam->ifam_addrs; 496 497 /* Expand the compacted addresses */ 498 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 499 &info); 500 501 if (afp) { 502 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family && 503 afp->af_status != ether_status) { 504 p = afp; 505 if (p->af_status != ether_status) 506 (*p->af_status)(1); 507 } 508 } else for (p = afs; p->af_name; p++) { 509 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family && 510 p->af_status != ether_status) 511 (*p->af_status)(0); 512 } 513 } 514 if (afp == NULL || afp->af_status == ether_status) 515 ether_status(0); 516 else if (afp && !p) { 517 warnx("%s has no %s IFA address!", name, afp->af_name); 518 } 519 520 phys_status(0); 521 } 522 523 static void 524 phys_status(int force) 525 { 526 char psrcaddr[256]; 527 char pdstaddr[256]; 528 int flags = NI_NUMERICHOST; 529 char *af; 530 #ifndef SIOCGLIFPHYADDR 531 u_long srccmd, dstcmd; 532 struct ifreq *ifrp; 533 #ifdef INET6 534 int s6; 535 #endif 536 537 force = 0; /*fool gcc*/ 538 539 psrcaddr[0] = pdstaddr[0] = '\0'; 540 541 #ifdef INET6 542 s6 = socket(AF_INET6, SOCK_DGRAM, 0); 543 if (s6 < 0) { 544 ifrp = 𝔦 545 srccmd = SIOCGIFPSRCADDR; 546 dstcmd = SIOCGIFPDSTADDR; 547 } else { 548 close(s6); 549 srccmd = SIOCGIFPSRCADDR_IN6; 550 dstcmd = SIOCGIFPDSTADDR_IN6; 551 ifrp = (struct ifreq *)&in6_ifr; 552 } 553 #else /* INET6 */ 554 ifrp = 𝔦 555 srccmd = SIOCGIFPSRCADDR; 556 dstcmd = SIOCGIFPDSTADDR; 557 #endif /* INET6 */ 558 559 if (0 <= ioctl(s, srccmd, (caddr_t)ifrp)) { 560 #ifdef INET6 561 if (ifrp->ifr_addr.sa_family == AF_INET6) 562 af = "inet6"; 563 else 564 af = "inet"; 565 #else 566 af = "inet"; 567 #endif /* INET6 */ 568 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len, 569 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0) 570 psrcaddr[0] = '\0'; 571 } 572 if (0 <= ioctl(s, dstcmd, (caddr_t)ifrp)) { 573 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len, 574 pdstaddr, sizeof(pdstaddr), 0, 0, flags) != 0) 575 pdstaddr[0] = '\0'; 576 } 577 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr); 578 #else 579 struct if_laddrreq iflr; 580 581 force = 0; /*fool gcc*/ 582 583 psrcaddr[0] = pdstaddr[0] = '\0'; 584 585 memset(&iflr, 0, sizeof(iflr)); 586 memcpy(iflr.iflr_name, ifr.ifr_name, sizeof(iflr.iflr_name)); 587 588 if (0 <= ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&iflr)) { 589 switch (iflr.addr.ss_family) { 590 case AF_INET: 591 af = "inet"; 592 break; 593 #ifdef INET6 594 case AF_INET6: 595 af = "inet6"; 596 break; 597 #endif /* INET6 */ 598 } 599 if (getnameinfo((struct sockaddr *)&iflr.addr, iflr.addr.ss_len, 600 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0) 601 psrcaddr[0] = '\0'; 602 if (getnameinfo((struct sockaddr *)&iflr.dstaddr, 603 iflr.dstaddr.ss_len, pdstaddr, sizeof(pdstaddr), 0, 0, 604 flags) != 0) 605 pdstaddr[0] = '\0'; 606 } 607 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr); 608 #endif 609 } 610 611 static void 612 in_status(int force) 613 { 614 struct sockaddr_in *sin, null_sin; 615 #if 0 616 char *inet_ntoa(); 617 #endif 618 619 memset(&null_sin, 0, sizeof(null_sin)); 620 621 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA]; 622 if (!sin || sin->sin_family != AF_INET) { 623 if (!force) 624 return; 625 /* warnx("%s has no AF_INET IFA address!", name); */ 626 sin = &null_sin; 627 } 628 printf("\tinet %s ", inet_ntoa(sin->sin_addr)); 629 630 if (flags & IFF_POINTOPOINT) { 631 /* note RTAX_BRD overlap with IFF_BROADCAST */ 632 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD]; 633 if (!sin) 634 sin = &null_sin; 635 printf("--> %s ", inet_ntoa(sin->sin_addr)); 636 } 637 638 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK]; 639 if (!sin) 640 sin = &null_sin; 641 printf("netmask 0x%x ", (u_int32_t)ntohl(sin->sin_addr.s_addr)); 642 643 if (flags & IFF_BROADCAST) { 644 /* note RTAX_BRD overlap with IFF_POINTOPOINT */ 645 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD]; 646 if (sin && sin->sin_addr.s_addr != 0) 647 printf("broadcast %s", inet_ntoa(sin->sin_addr)); 648 } 649 putchar('\n'); 650 } 651 652 #ifdef INET6 653 static void 654 in6_status(int force) 655 { 656 struct sockaddr_in6 *sin, null_sin; 657 char hostname[NI_MAXHOST]; 658 int niflags = NI_NUMERICHOST; 659 660 memset(&null_sin, 0, sizeof(null_sin)); 661 #ifdef NI_WITHSCOPEID 662 niflags |= NI_WITHSCOPEID; 663 #endif 664 665 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA]; 666 if (!sin || sin->sin6_family != AF_INET6) { 667 if (!force) 668 return; 669 /* warnx("%s has no AF_INET6 IFA address!", name); */ 670 sin = &null_sin; 671 } 672 #ifdef __KAME__ 673 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 674 sin->sin6_scope_id = 675 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]); 676 sin->sin6_addr.s6_addr[2] = 0; 677 sin->sin6_addr.s6_addr[3] = 0; 678 } 679 #endif 680 getnameinfo((struct sockaddr *)sin, sin->sin6_len, 681 hostname, sizeof(hostname), 0, 0, niflags); 682 printf("\tinet6 %s ", hostname); 683 684 if (flags & IFF_POINTOPOINT) { 685 /* note RTAX_BRD overlap with IFF_BROADCAST */ 686 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_BRD]; 687 /* 688 * some of ther interfaces do not have valid destination 689 * address. 690 */ 691 if (sin->sin6_family == AF_INET6) { 692 #ifdef __KAME__ 693 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 694 sin->sin6_scope_id = 695 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]); 696 sin->sin6_addr.s6_addr[2] = 0; 697 sin->sin6_addr.s6_addr[3] = 0; 698 } 699 #endif 700 getnameinfo((struct sockaddr *)sin, sin->sin6_len, 701 hostname, sizeof(hostname), 0, 0, niflags); 702 printf("--> %s ", hostname); 703 } 704 } 705 706 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_NETMASK]; 707 if (!sin) 708 sin = &null_sin; 709 printf(" prefixlen %d ", prefix(&sin->sin6_addr, 710 sizeof(struct in6_addr))); 711 712 putchar('\n'); 713 } 714 #endif /*INET6*/ 715 716 /*ARGSUSED*/ 717 static void 718 ether_status(int dummy) 719 { 720 char *cp; 721 int n; 722 723 dummy = 0; /*fool gcc*/ 724 725 cp = (char *)LLADDR(sdl); 726 if ((n = sdl->sdl_alen) > 0) { 727 if (sdl->sdl_type == IFT_ETHER) 728 printf ("\tether "); 729 else 730 printf ("\tlladdr "); 731 while (--n >= 0) 732 printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' '); 733 putchar('\n'); 734 } 735 } 736 737 static void 738 Perror(char *cmd) 739 { 740 switch (errno) { 741 742 case ENXIO: 743 errx(1, "%s: no such interface", cmd); 744 break; 745 746 case EPERM: 747 errx(1, "%s: permission denied", cmd); 748 break; 749 750 default: 751 err(1, "%s", cmd); 752 } 753 } 754 755 #define SIN(x) ((struct sockaddr_in *) &(x)) 756 static struct sockaddr_in *sintab[] = { 757 SIN(addreq.ifra_addr), SIN(addreq.ifra_dstaddr)}; 758 759 static void 760 in_getaddr(char *s, int which) 761 { 762 struct sockaddr_in *sin = sintab[which]; 763 struct hostent *hp; 764 struct netent *np; 765 766 sin->sin_len = sizeof(*sin); 767 sin->sin_family = AF_INET; 768 769 if (inet_aton(s, &sin->sin_addr)) 770 ; 771 else if ((hp = gethostbyname(s)) != NULL) 772 bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length); 773 else if ((np = getnetbyname(s)) != NULL) 774 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 775 else 776 errx(1, "%s: bad value", s); 777 } 778 779 #ifdef INET6 780 #define SIN6(x) ((struct sockaddr_in6 *) &(x)) 781 static struct sockaddr_in6 *sin6tab[] = { 782 SIN6(in6_addreq.ifra_addr), SIN6(in6_addreq.ifra_dstaddr)}; 783 784 static void 785 in6_getaddr(char *s, int which) 786 { 787 struct sockaddr_in6 *sin = sin6tab[which]; 788 789 sin->sin6_len = sizeof(*sin); 790 sin->sin6_family = AF_INET6; 791 792 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1) 793 errx(1, "%s: bad value", s); 794 } 795 796 static void 797 in6_getprefix(char *plen, int which) 798 { 799 struct sockaddr_in6 *sin = sin6tab[which]; 800 u_char *cp; 801 int len = atoi(plen); 802 803 if ((len < 0) || (len > 128)) 804 errx(1, "%s: bad value", plen); 805 sin->sin6_len = sizeof(*sin); 806 sin->sin6_family = AF_INET6; 807 if ((len == 0) || (len == 128)) { 808 memset(&sin->sin6_addr, -1, sizeof(struct in6_addr)); 809 return; 810 } 811 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8) 812 *cp++ = -1; 813 *cp = (-1) << (8 - len); 814 } 815 #endif 816 817 /* 818 * Print a value a la the %b format of the kernel's printf 819 */ 820 static void 821 printb(char *s, unsigned int v, char *bits) 822 { 823 int i, any = 0; 824 char c; 825 826 if (bits && *bits == 8) 827 printf("%s=%o", s, v & 0xffff); 828 else 829 printf("%s=%x", s, v & 0xffff); 830 bits++; 831 if (bits) { 832 putchar('<'); 833 while ((i = *bits++) != 0) { 834 if ((v & (1 << (i-1))) != 0) { 835 if (any) 836 putchar(','); 837 any = 1; 838 for (; (c = *bits) > 32; bits++) 839 putchar(c); 840 } else 841 for (; *bits > 32; bits++) 842 ; 843 } 844 putchar('>'); 845 } 846 } 847 848 #ifdef INET6 849 static int 850 prefix(void *val, int size) 851 { 852 u_char *name = (u_char *)val; 853 int byte, bit, plen = 0; 854 855 for (byte = 0; byte < size; byte++, plen += 8) 856 if (name[byte] != 0xff) 857 break; 858 if (byte == size) 859 return (plen); 860 for (bit = 7; bit != 0; bit--, plen++) 861 if (!(name[byte] & (1 << bit))) 862 break; 863 for (; bit != 0; bit--) 864 if (name[byte] & (1 << bit)) 865 return(0); 866 byte++; 867 for (; byte < size; byte++) 868 if (name[byte]) 869 return(0); 870 return (plen); 871 } 872 #endif /*INET6*/ 873