1 /* 2 * Copyright (c) 1983, 1988, 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 34 #ifndef lint 35 #if 0 36 static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95"; 37 #endif 38 static const char rcsid[] = 39 "$FreeBSD$"; 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 #include <sys/protosw.h> 44 #include <sys/socket.h> 45 #include <sys/time.h> 46 47 #include <net/if.h> 48 #include <net/if_var.h> 49 #include <net/if_dl.h> 50 #include <net/if_types.h> 51 #include <net/route.h> 52 53 #include <netinet/in.h> 54 #include <netipx/ipx.h> 55 #include <netatalk/at.h> 56 #include <netgraph/ng_socket.h> 57 58 #ifdef NS 59 #include <netns/ns.h> 60 #endif 61 62 #include <sys/sysctl.h> 63 64 #include <arpa/inet.h> 65 #include <libutil.h> 66 #include <netdb.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <string.h> 70 #include <unistd.h> 71 #include <err.h> 72 #include <time.h> 73 #include "netstat.h" 74 75 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d))) 76 77 78 /* alignment constraint for routing socket */ 79 #define ROUNDUP(a) \ 80 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 81 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 82 83 /* 84 * Definitions for showing gateway flags. 85 */ 86 struct bits { 87 u_long b_mask; 88 char b_val; 89 } bits[] = { 90 { RTF_UP, 'U' }, 91 { RTF_GATEWAY, 'G' }, 92 { RTF_HOST, 'H' }, 93 { RTF_REJECT, 'R' }, 94 { RTF_DYNAMIC, 'D' }, 95 { RTF_MODIFIED, 'M' }, 96 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */ 97 { RTF_CLONING, 'C' }, 98 { RTF_XRESOLVE, 'X' }, 99 { RTF_LLINFO, 'L' }, 100 { RTF_STATIC, 'S' }, 101 { RTF_PROTO1, '1' }, 102 { RTF_PROTO2, '2' }, 103 { RTF_WASCLONED,'W' }, 104 { RTF_PRCLONING,'c' }, 105 { RTF_PROTO3, '3' }, 106 { RTF_BLACKHOLE,'B' }, 107 { RTF_BROADCAST,'b' }, 108 { 0 } 109 }; 110 111 typedef union { 112 long dummy; /* Helps align structure. */ 113 struct sockaddr u_sa; 114 u_short u_data[128]; 115 } sa_u; 116 117 static sa_u pt_u; 118 119 int do_rtent = 0; 120 struct rtentry rtentry; 121 struct radix_node rnode; 122 struct radix_mask rmask; 123 struct radix_node_head *rt_tables[AF_MAX+1]; 124 125 int NewTree = 0; 126 127 static struct sockaddr *kgetsa (struct sockaddr *); 128 static void p_tree (struct radix_node *); 129 static void p_rtnode (void); 130 static void ntreestuff (void); 131 static void np_rtentry (struct rt_msghdr *); 132 static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int); 133 static void p_flags (int, char *); 134 static void p_rtentry (struct rtentry *); 135 static u_long forgemask (u_long); 136 static void domask (char *, u_long, u_long); 137 138 /* 139 * Print routing tables. 140 */ 141 void 142 routepr(u_long rtree) 143 { 144 struct radix_node_head *rnh, head; 145 int i; 146 147 printf("Routing tables\n"); 148 149 if (Aflag == 0 && NewTree) 150 ntreestuff(); 151 else { 152 if (rtree == 0) { 153 printf("rt_tables: symbol not in namelist\n"); 154 return; 155 } 156 157 kget(rtree, rt_tables); 158 for (i = 0; i <= AF_MAX; i++) { 159 if ((rnh = rt_tables[i]) == 0) 160 continue; 161 kget(rnh, head); 162 if (i == AF_UNSPEC) { 163 if (Aflag && af == 0) { 164 printf("Netmasks:\n"); 165 p_tree(head.rnh_treetop); 166 } 167 } else if (af == AF_UNSPEC || af == i) { 168 pr_family(i); 169 do_rtent = 1; 170 pr_rthdr(i); 171 p_tree(head.rnh_treetop); 172 } 173 } 174 } 175 } 176 177 /* 178 * Print address family header before a section of the routing table. 179 */ 180 void 181 pr_family(int af) 182 { 183 char *afname; 184 185 switch (af) { 186 case AF_INET: 187 afname = "Internet"; 188 break; 189 #ifdef INET6 190 case AF_INET6: 191 afname = "Internet6"; 192 break; 193 #endif /*INET6*/ 194 case AF_IPX: 195 afname = "IPX"; 196 break; 197 #ifdef NS 198 case AF_NS: 199 afname = "XNS"; 200 break; 201 #endif 202 case AF_ISO: 203 afname = "ISO"; 204 break; 205 case AF_APPLETALK: 206 afname = "AppleTalk"; 207 break; 208 case AF_CCITT: 209 afname = "X.25"; 210 break; 211 case AF_NETGRAPH: 212 afname = "Netgraph"; 213 break; 214 default: 215 afname = NULL; 216 break; 217 } 218 if (afname) 219 printf("\n%s:\n", afname); 220 else 221 printf("\nProtocol Family %d:\n", af); 222 } 223 224 /* column widths; each followed by one space */ 225 #ifndef INET6 226 #define WID_DST(af) 18 /* width of destination column */ 227 #define WID_GW(af) 18 /* width of gateway column */ 228 #define WID_IF(af) 6 /* width of netif column */ 229 #else 230 #define WID_DST(af) \ 231 ((af) == AF_INET6 ? (Wflag ? 39 : (numeric_addr ? 33: 18)) : 18) 232 #define WID_GW(af) \ 233 ((af) == AF_INET6 ? (Wflag ? 31 : (numeric_addr ? 29 : 18)) : 18) 234 #define WID_IF(af) ((af) == AF_INET6 ? 8 : 6) 235 #endif /*INET6*/ 236 237 /* 238 * Print header for routing table columns. 239 */ 240 void 241 pr_rthdr(int af) 242 { 243 244 if (Aflag) 245 printf("%-8.8s ","Address"); 246 if (af == AF_INET || Wflag) 247 if (Wflag) 248 printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %*.*s %6s\n", 249 WID_DST(af), WID_DST(af), "Destination", 250 WID_GW(af), WID_GW(af), "Gateway", 251 "Flags", "Refs", "Use", "Mtu", 252 WID_IF(af), WID_IF(af), "Netif", "Expire"); 253 else 254 printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %*.*s %6s\n", 255 WID_DST(af), WID_DST(af), "Destination", 256 WID_GW(af), WID_GW(af), "Gateway", 257 "Flags", "Refs", "Use", 258 WID_IF(af), WID_IF(af), "Netif", "Expire"); 259 else 260 printf("%-*.*s %-*.*s %-6.6s %8.8s %6s\n", 261 WID_DST(af), WID_DST(af), "Destination", 262 WID_GW(af), WID_GW(af), "Gateway", 263 "Flags", "Netif", "Expire"); 264 } 265 266 static struct sockaddr * 267 kgetsa(struct sockaddr *dst) 268 { 269 270 kget(dst, pt_u.u_sa); 271 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa)) 272 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len); 273 return (&pt_u.u_sa); 274 } 275 276 static void 277 p_tree(struct radix_node *rn) 278 { 279 280 again: 281 kget(rn, rnode); 282 if (rnode.rn_bit < 0) { 283 if (Aflag) 284 printf("%-8.8lx ", (u_long)rn); 285 if (rnode.rn_flags & RNF_ROOT) { 286 if (Aflag) 287 printf("(root node)%s", 288 rnode.rn_dupedkey ? " =>\n" : "\n"); 289 } else if (do_rtent) { 290 kget(rn, rtentry); 291 p_rtentry(&rtentry); 292 if (Aflag) 293 p_rtnode(); 294 } else { 295 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key), 296 NULL, 0, 44); 297 putchar('\n'); 298 } 299 if ((rn = rnode.rn_dupedkey)) 300 goto again; 301 } else { 302 if (Aflag && do_rtent) { 303 printf("%-8.8lx ", (u_long)rn); 304 p_rtnode(); 305 } 306 rn = rnode.rn_right; 307 p_tree(rnode.rn_left); 308 p_tree(rn); 309 } 310 } 311 312 char nbuf[20]; 313 314 static void 315 p_rtnode(void) 316 { 317 struct radix_mask *rm = rnode.rn_mklist; 318 319 if (rnode.rn_bit < 0) { 320 if (rnode.rn_mask) { 321 printf("\t mask "); 322 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask), 323 NULL, 0, -1); 324 } else if (rm == 0) 325 return; 326 } else { 327 sprintf(nbuf, "(%d)", rnode.rn_bit); 328 printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right); 329 } 330 while (rm) { 331 kget(rm, rmask); 332 sprintf(nbuf, " %d refs, ", rmask.rm_refs); 333 printf(" mk = %8.8lx {(%d),%s", 334 (u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " "); 335 if (rmask.rm_flags & RNF_NORMAL) { 336 struct radix_node rnode_aux; 337 printf(" <normal>, "); 338 kget(rmask.rm_leaf, rnode_aux); 339 p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask), 340 NULL, 0, -1); 341 } else 342 p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), 343 NULL, 0, -1); 344 putchar('}'); 345 if ((rm = rmask.rm_mklist)) 346 printf(" ->"); 347 } 348 putchar('\n'); 349 } 350 351 static void 352 ntreestuff(void) 353 { 354 size_t needed; 355 int mib[6]; 356 char *buf, *next, *lim; 357 register struct rt_msghdr *rtm; 358 359 mib[0] = CTL_NET; 360 mib[1] = PF_ROUTE; 361 mib[2] = 0; 362 mib[3] = 0; 363 mib[4] = NET_RT_DUMP; 364 mib[5] = 0; 365 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 366 err(1, "sysctl: net.route.0.0.dump estimate"); 367 } 368 369 if ((buf = malloc(needed)) == 0) { 370 err(2, "malloc(%lu)", (unsigned long)needed); 371 } 372 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 373 err(1, "sysctl: net.route.0.0.dump"); 374 } 375 lim = buf + needed; 376 for (next = buf; next < lim; next += rtm->rtm_msglen) { 377 rtm = (struct rt_msghdr *)next; 378 np_rtentry(rtm); 379 } 380 } 381 382 static void 383 np_rtentry(struct rt_msghdr *rtm) 384 { 385 register struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 386 #ifdef notdef 387 static int masks_done, banner_printed; 388 #endif 389 static int old_af; 390 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST; 391 392 #ifdef notdef 393 /* for the moment, netmasks are skipped over */ 394 if (!banner_printed) { 395 printf("Netmasks:\n"); 396 banner_printed = 1; 397 } 398 if (masks_done == 0) { 399 if (rtm->rtm_addrs != RTA_DST ) { 400 masks_done = 1; 401 af = sa->sa_family; 402 } 403 } else 404 #endif 405 af = sa->sa_family; 406 if (af != old_af) { 407 pr_family(af); 408 old_af = af; 409 } 410 if (rtm->rtm_addrs == RTA_DST) 411 p_sockaddr(sa, NULL, 0, 36); 412 else { 413 p_sockaddr(sa, NULL, rtm->rtm_flags, 16); 414 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa); 415 p_sockaddr(sa, NULL, 0, 18); 416 } 417 p_flags(rtm->rtm_flags & interesting, "%-6.6s "); 418 putchar('\n'); 419 } 420 421 static void 422 p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width) 423 { 424 char workbuf[128], *cplim; 425 register char *cp = workbuf; 426 427 switch(sa->sa_family) { 428 case AF_INET: 429 { 430 register struct sockaddr_in *sin = (struct sockaddr_in *)sa; 431 432 if ((sin->sin_addr.s_addr == INADDR_ANY) && 433 mask && 434 ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr) 435 ==0L) 436 cp = "default" ; 437 else if (flags & RTF_HOST) 438 cp = routename(sin->sin_addr.s_addr); 439 else if (mask) 440 cp = netname(sin->sin_addr.s_addr, 441 ntohl(((struct sockaddr_in *)mask) 442 ->sin_addr.s_addr)); 443 else 444 cp = netname(sin->sin_addr.s_addr, 0L); 445 break; 446 } 447 448 #ifdef INET6 449 case AF_INET6: 450 { 451 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; 452 struct in6_addr *in6 = &sa6->sin6_addr; 453 454 /* 455 * XXX: This is a special workaround for KAME kernels. 456 * sin6_scope_id field of SA should be set in the future. 457 */ 458 if (IN6_IS_ADDR_LINKLOCAL(in6) || 459 IN6_IS_ADDR_MC_LINKLOCAL(in6)) { 460 /* XXX: override is ok? */ 461 sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]); 462 *(u_short *)&in6->s6_addr[2] = 0; 463 } 464 465 if (flags & RTF_HOST) 466 cp = routename6(sa6); 467 else if (mask) 468 cp = netname6(sa6, 469 &((struct sockaddr_in6 *)mask)->sin6_addr); 470 else { 471 cp = netname6(sa6, NULL); 472 } 473 break; 474 } 475 #endif /*INET6*/ 476 477 case AF_IPX: 478 { 479 struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr; 480 if (ipx_nullnet(satoipx_addr(work))) 481 cp = "default"; 482 else 483 cp = ipx_print(sa); 484 break; 485 } 486 case AF_APPLETALK: 487 { 488 if (!(flags & RTF_HOST) && mask) 489 cp = atalk_print2(sa,mask,9); 490 else 491 cp = atalk_print(sa,11); 492 break; 493 } 494 case AF_NETGRAPH: 495 { 496 printf("%s", ((struct sockaddr_ng *)sa)->sg_data); 497 break; 498 } 499 #ifdef NS 500 case AF_NS: 501 cp = ns_print(sa); 502 break; 503 #endif 504 505 case AF_LINK: 506 { 507 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; 508 509 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && 510 sdl->sdl_slen == 0) 511 (void) sprintf(workbuf, "link#%d", sdl->sdl_index); 512 else 513 switch (sdl->sdl_type) { 514 515 case IFT_ETHER: 516 { 517 register int i; 518 register u_char *lla = (u_char *)sdl->sdl_data + 519 sdl->sdl_nlen; 520 521 cplim = ""; 522 for (i = 0; i < sdl->sdl_alen; i++, lla++) { 523 cp += sprintf(cp, "%s%x", cplim, *lla); 524 cplim = ":"; 525 } 526 cp = workbuf; 527 break; 528 } 529 530 default: 531 cp = link_ntoa(sdl); 532 break; 533 } 534 break; 535 } 536 537 default: 538 { 539 register u_char *s = (u_char *)sa->sa_data, *slim; 540 541 slim = sa->sa_len + (u_char *) sa; 542 cplim = cp + sizeof(workbuf) - 6; 543 cp += sprintf(cp, "(%d)", sa->sa_family); 544 while (s < slim && cp < cplim) { 545 cp += sprintf(cp, " %02x", *s++); 546 if (s < slim) 547 cp += sprintf(cp, "%02x", *s++); 548 } 549 cp = workbuf; 550 } 551 } 552 if (width < 0 ) 553 printf("%s ", cp); 554 else { 555 if (numeric_addr) 556 printf("%-*s ", width, cp); 557 else 558 printf("%-*.*s ", width, width, cp); 559 } 560 } 561 562 static void 563 p_flags(int f, char *format) 564 { 565 char name[33], *flags; 566 register struct bits *p = bits; 567 568 for (flags = name; p->b_mask; p++) 569 if (p->b_mask & f) 570 *flags++ = p->b_val; 571 *flags = '\0'; 572 printf(format, name); 573 } 574 575 static void 576 p_rtentry(struct rtentry *rt) 577 { 578 static struct ifnet ifnet, *lastif; 579 struct rtentry parent; 580 static char name[16]; 581 static char prettyname[9]; 582 struct sockaddr *sa; 583 sa_u addr, mask; 584 585 /* 586 * Don't print protocol-cloned routes unless -a. 587 */ 588 if (rt->rt_flags & RTF_WASCLONED && !aflag) { 589 kget(rt->rt_parent, parent); 590 if (parent.rt_flags & RTF_PRCLONING) 591 return; 592 } 593 594 bzero(&addr, sizeof(addr)); 595 if ((sa = kgetsa(rt_key(rt)))) 596 bcopy(sa, &addr, sa->sa_len); 597 bzero(&mask, sizeof(mask)); 598 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt)))) 599 bcopy(sa, &mask, sa->sa_len); 600 p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, 601 WID_DST(addr.u_sa.sa_family)); 602 p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, 603 WID_GW(addr.u_sa.sa_family)); 604 p_flags(rt->rt_flags, "%-6.6s "); 605 if (addr.u_sa.sa_family == AF_INET || Wflag) { 606 printf("%6ld %8ld ", rt->rt_refcnt, rt->rt_use); 607 if (Wflag) { 608 if (rt->rt_rmx.rmx_mtu != 0) 609 printf("%6lu ", rt->rt_rmx.rmx_mtu); 610 else 611 printf("%6s ", ""); 612 } 613 } 614 if (rt->rt_ifp) { 615 if (rt->rt_ifp != lastif) { 616 kget(rt->rt_ifp, ifnet); 617 kread((u_long)ifnet.if_name, name, 16); 618 lastif = rt->rt_ifp; 619 snprintf(prettyname, sizeof prettyname, 620 "%s%d", name, ifnet.if_unit); 621 } 622 printf("%*.*s", WID_IF(addr.u_sa.sa_family), 623 WID_IF(addr.u_sa.sa_family), prettyname); 624 if (rt->rt_rmx.rmx_expire) { 625 time_t expire_time; 626 627 if ((expire_time = 628 rt->rt_rmx.rmx_expire - time((time_t *)0)) > 0) 629 printf(" %6d", (int)expire_time); 630 } 631 if (rt->rt_nodes[0].rn_dupedkey) 632 printf(" =>"); 633 } 634 putchar('\n'); 635 } 636 637 char * 638 routename(u_long in) 639 { 640 register char *cp; 641 static char line[MAXHOSTNAMELEN]; 642 struct hostent *hp; 643 644 cp = 0; 645 if (!numeric_addr) { 646 hp = gethostbyaddr((char *)&in, sizeof (struct in_addr), 647 AF_INET); 648 if (hp) { 649 cp = hp->h_name; 650 trimdomain(cp, strlen(cp)); 651 } 652 } 653 if (cp) { 654 strncpy(line, cp, sizeof(line) - 1); 655 line[sizeof(line) - 1] = '\0'; 656 } else { 657 #define C(x) ((x) & 0xff) 658 in = ntohl(in); 659 sprintf(line, "%lu.%lu.%lu.%lu", 660 C(in >> 24), C(in >> 16), C(in >> 8), C(in)); 661 } 662 return (line); 663 } 664 665 static u_long 666 forgemask(u_long a) 667 { 668 u_long m; 669 670 if (IN_CLASSA(a)) 671 m = IN_CLASSA_NET; 672 else if (IN_CLASSB(a)) 673 m = IN_CLASSB_NET; 674 else 675 m = IN_CLASSC_NET; 676 return (m); 677 } 678 679 static void 680 domask(char *dst, u_long addr, u_long mask) 681 { 682 register int b, i; 683 684 if (!mask || (forgemask(addr) == mask)) { 685 *dst = '\0'; 686 return; 687 } 688 i = 0; 689 for (b = 0; b < 32; b++) 690 if (mask & (1 << b)) { 691 register int bb; 692 693 i = b; 694 for (bb = b+1; bb < 32; bb++) 695 if (!(mask & (1 << bb))) { 696 i = -1; /* noncontig */ 697 break; 698 } 699 break; 700 } 701 if (i == -1) 702 sprintf(dst, "&0x%lx", mask); 703 else 704 sprintf(dst, "/%d", 32-i); 705 } 706 707 /* 708 * Return the name of the network whose address is given. 709 * The address is assumed to be that of a net or subnet, not a host. 710 */ 711 char * 712 netname(u_long in, u_long mask) 713 { 714 char *cp = 0; 715 static char line[MAXHOSTNAMELEN]; 716 struct netent *np = 0; 717 u_long dmask; 718 register u_long i; 719 720 #define NSHIFT(m) ( \ 721 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \ 722 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \ 723 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \ 724 0) 725 726 i = ntohl(in); 727 dmask = forgemask(i); 728 if (!numeric_addr && i) { 729 np = getnetbyaddr(i >> NSHIFT(mask), AF_INET); 730 if (np == NULL && mask == 0) 731 np = getnetbyaddr(i >> NSHIFT(dmask), AF_INET); 732 if (np != NULL) { 733 cp = np->n_name; 734 trimdomain(cp, strlen(cp)); 735 } 736 } 737 #undef NSHIFT 738 if (cp != NULL) { 739 strncpy(line, cp, sizeof(line) - 1); 740 line[sizeof(line) - 1] = '\0'; 741 } else { 742 switch (dmask) { 743 case IN_CLASSA_NET: 744 if ((i & IN_CLASSA_HOST) == 0) { 745 sprintf(line, "%lu", C(i >> 24)); 746 break; 747 } 748 /* FALLTHROUGH */ 749 case IN_CLASSB_NET: 750 if ((i & IN_CLASSB_HOST) == 0) { 751 sprintf(line, "%lu.%lu", 752 C(i >> 24), C(i >> 16)); 753 break; 754 } 755 /* FALLTHROUGH */ 756 case IN_CLASSC_NET: 757 if ((i & IN_CLASSC_HOST) == 0) { 758 sprintf(line, "%lu.%lu.%lu", 759 C(i >> 24), C(i >> 16), C(i >> 8)); 760 break; 761 } 762 /* FALLTHROUGH */ 763 default: 764 sprintf(line, "%lu.%lu.%lu.%lu", 765 C(i >> 24), C(i >> 16), C(i >> 8), C(i)); 766 break; 767 } 768 } 769 domask(line + strlen(line), i, mask); 770 return (line); 771 } 772 773 #ifdef INET6 774 char * 775 netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask) 776 { 777 static char line[MAXHOSTNAMELEN]; 778 u_char *p = (u_char *)mask; 779 u_char *lim; 780 int masklen, illegal = 0, flag = NI_WITHSCOPEID; 781 782 if (mask) { 783 for (masklen = 0, lim = p + 16; p < lim; p++) { 784 switch (*p) { 785 case 0xff: 786 masklen += 8; 787 break; 788 case 0xfe: 789 masklen += 7; 790 break; 791 case 0xfc: 792 masklen += 6; 793 break; 794 case 0xf8: 795 masklen += 5; 796 break; 797 case 0xf0: 798 masklen += 4; 799 break; 800 case 0xe0: 801 masklen += 3; 802 break; 803 case 0xc0: 804 masklen += 2; 805 break; 806 case 0x80: 807 masklen += 1; 808 break; 809 case 0x00: 810 break; 811 default: 812 illegal ++; 813 break; 814 } 815 } 816 if (illegal) 817 fprintf(stderr, "illegal prefixlen\n"); 818 } 819 else 820 masklen = 128; 821 822 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr)) 823 return("default"); 824 825 if (numeric_addr) 826 flag |= NI_NUMERICHOST; 827 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line), 828 NULL, 0, flag); 829 830 if (numeric_addr) 831 sprintf(&line[strlen(line)], "/%d", masklen); 832 833 return line; 834 } 835 836 char * 837 routename6(struct sockaddr_in6 *sa6) 838 { 839 static char line[MAXHOSTNAMELEN]; 840 int flag = NI_WITHSCOPEID; 841 /* use local variable for safety */ 842 struct sockaddr_in6 sa6_local = {AF_INET6, sizeof(sa6_local),}; 843 844 sa6_local.sin6_addr = sa6->sin6_addr; 845 sa6_local.sin6_scope_id = sa6->sin6_scope_id; 846 847 if (numeric_addr) 848 flag |= NI_NUMERICHOST; 849 850 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len, 851 line, sizeof(line), NULL, 0, flag); 852 853 return line; 854 } 855 #endif /*INET6*/ 856 857 /* 858 * Print routing statistics 859 */ 860 void 861 rt_stats(u_long rtsaddr, u_long rttaddr) 862 { 863 struct rtstat rtstat; 864 int rttrash; 865 866 if (rtsaddr == 0) { 867 printf("rtstat: symbol not in namelist\n"); 868 return; 869 } 870 if (rttaddr == 0) { 871 printf("rttrash: symbol not in namelist\n"); 872 return; 873 } 874 kread(rtsaddr, (char *)&rtstat, sizeof (rtstat)); 875 kread(rttaddr, (char *)&rttrash, sizeof (rttrash)); 876 printf("routing:\n"); 877 878 #define p(f, m) if (rtstat.f || sflag <= 1) \ 879 printf(m, rtstat.f, plural(rtstat.f)) 880 881 p(rts_badredirect, "\t%u bad routing redirect%s\n"); 882 p(rts_dynamic, "\t%u dynamically created route%s\n"); 883 p(rts_newgateway, "\t%u new gateway%s due to redirects\n"); 884 p(rts_unreach, "\t%u destination%s found unreachable\n"); 885 p(rts_wildcard, "\t%u use%s of a wildcard route\n"); 886 #undef p 887 888 if (rttrash || sflag <= 1) 889 printf("\t%u route%s not in table but not freed\n", 890 rttrash, plural(rttrash)); 891 } 892 893 char * 894 ipx_print(struct sockaddr *sa) 895 { 896 u_short port; 897 struct servent *sp = 0; 898 char *net = "", *host = ""; 899 register char *p; 900 register u_char *q; 901 struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr; 902 static char mybuf[50]; 903 char cport[10], chost[15], cnet[15]; 904 905 port = ntohs(work.x_port); 906 907 if (ipx_nullnet(work) && ipx_nullhost(work)) { 908 909 if (port) { 910 if (sp) 911 sprintf(mybuf, "*.%s", sp->s_name); 912 else 913 sprintf(mybuf, "*.%x", port); 914 } else 915 sprintf(mybuf, "*.*"); 916 917 return (mybuf); 918 } 919 920 if (ipx_wildnet(work)) 921 net = "any"; 922 else if (ipx_nullnet(work)) 923 net = "*"; 924 else { 925 q = work.x_net.c_net; 926 sprintf(cnet, "%02x%02x%02x%02x", 927 q[0], q[1], q[2], q[3]); 928 for (p = cnet; *p == '0' && p < cnet + 8; p++) 929 continue; 930 net = p; 931 } 932 933 if (ipx_wildhost(work)) 934 host = "any"; 935 else if (ipx_nullhost(work)) 936 host = "*"; 937 else { 938 q = work.x_host.c_host; 939 sprintf(chost, "%02x%02x%02x%02x%02x%02x", 940 q[0], q[1], q[2], q[3], q[4], q[5]); 941 for (p = chost; *p == '0' && p < chost + 12; p++) 942 continue; 943 host = p; 944 } 945 946 if (port) { 947 if (strcmp(host, "*") == 0) 948 host = ""; 949 if (sp) 950 snprintf(cport, sizeof(cport), 951 "%s%s", *host ? "." : "", sp->s_name); 952 else 953 snprintf(cport, sizeof(cport), 954 "%s%x", *host ? "." : "", port); 955 } else 956 *cport = 0; 957 958 snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport); 959 return(mybuf); 960 } 961 962 char * 963 ipx_phost(struct sockaddr *sa) 964 { 965 register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa; 966 struct sockaddr_ipx work; 967 static union ipx_net ipx_zeronet; 968 char *p; 969 struct ipx_addr in; 970 971 work = *sipx; 972 in = work.sipx_addr; 973 974 work.sipx_addr.x_port = 0; 975 work.sipx_addr.x_net = ipx_zeronet; 976 p = ipx_print((struct sockaddr *)&work); 977 if (strncmp("*.", p, 2) == 0) p += 2; 978 979 return(p); 980 } 981 982 #ifdef NS 983 short ns_nullh[] = {0,0,0}; 984 short ns_bh[] = {-1,-1,-1}; 985 986 char * 987 ns_print(struct sockaddr *sa) 988 { 989 register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa; 990 struct ns_addr work; 991 union { union ns_net net_e; u_long long_e; } net; 992 u_short port; 993 static char mybuf[50], cport[10], chost[25]; 994 char *host = ""; 995 register char *p; register u_char *q; 996 997 work = sns->sns_addr; 998 port = ntohs(work.x_port); 999 work.x_port = 0; 1000 net.net_e = work.x_net; 1001 if (ns_nullhost(work) && net.long_e == 0) { 1002 if (port ) { 1003 sprintf(mybuf, "*.%xH", port); 1004 upHex(mybuf); 1005 } else 1006 sprintf(mybuf, "*.*"); 1007 return (mybuf); 1008 } 1009 1010 if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) { 1011 host = "any"; 1012 } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) { 1013 host = "*"; 1014 } else { 1015 q = work.x_host.c_host; 1016 sprintf(chost, "%02x%02x%02x%02x%02x%02xH", 1017 q[0], q[1], q[2], q[3], q[4], q[5]); 1018 for (p = chost; *p == '0' && p < chost + 12; p++) 1019 continue; 1020 host = p; 1021 } 1022 if (port) 1023 sprintf(cport, ".%xH", htons(port)); 1024 else 1025 *cport = 0; 1026 1027 sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport); 1028 upHex(mybuf); 1029 return(mybuf); 1030 } 1031 1032 char * 1033 ns_phost(struct sockaddr *sa) 1034 { 1035 register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa; 1036 struct sockaddr_ns work; 1037 static union ns_net ns_zeronet; 1038 char *p; 1039 1040 work = *sns; 1041 work.sns_addr.x_port = 0; 1042 work.sns_addr.x_net = ns_zeronet; 1043 1044 p = ns_print((struct sockaddr *)&work); 1045 if (strncmp("0H.", p, 3) == 0) 1046 p += 3; 1047 return(p); 1048 } 1049 #endif 1050 1051 void 1052 upHex(char *p0) 1053 { 1054 register char *p = p0; 1055 1056 for (; *p; p++) 1057 switch (*p) { 1058 1059 case 'a': 1060 case 'b': 1061 case 'c': 1062 case 'd': 1063 case 'e': 1064 case 'f': 1065 *p += ('A' - 'a'); 1066 break; 1067 } 1068 } 1069