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