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.3 (Berkeley) 3/9/94"; 37 #endif 38 static const char rcsid[] = 39 "$Id: route.c,v 1.3 1995/01/23 20:19:16 wollman Exp $"; 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 #include <sys/protosw.h> 44 #include <sys/socket.h> 45 #include <sys/mbuf.h> 46 47 #include <net/if.h> 48 #include <net/if_dl.h> 49 #include <net/if_types.h> 50 #define KERNEL 51 #include <net/route.h> 52 #undef KERNEL 53 #include <netinet/in.h> 54 55 #include <netns/ns.h> 56 57 #include <sys/sysctl.h> 58 59 #include <netdb.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 #include <err.h> 65 #include <time.h> 66 #include "netstat.h" 67 68 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d))) 69 70 /* 71 * Definitions for showing gateway flags. 72 */ 73 struct bits { 74 u_long b_mask; 75 char b_val; 76 } bits[] = { 77 { RTF_UP, 'U' }, 78 { RTF_GATEWAY, 'G' }, 79 { RTF_HOST, 'H' }, 80 { RTF_REJECT, 'R' }, 81 { RTF_DYNAMIC, 'D' }, 82 { RTF_MODIFIED, 'M' }, 83 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */ 84 { RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */ 85 { RTF_CLONING, 'C' }, 86 { RTF_XRESOLVE, 'X' }, 87 { RTF_LLINFO, 'L' }, 88 { RTF_STATIC, 'S' }, 89 { RTF_PROTO1, '1' }, 90 { RTF_PROTO2, '2' }, 91 { RTF_WASCLONED,'W' }, 92 { RTF_PRCLONING,'c' }, 93 { RTF_PROTO3, '3' }, 94 { 0 } 95 }; 96 97 static union { 98 struct sockaddr u_sa; 99 u_short u_data[128]; 100 } pt_u; 101 102 int do_rtent = 0; 103 struct rtentry rtentry; 104 struct radix_node rnode; 105 struct radix_mask rmask; 106 107 int NewTree = 0; 108 109 static struct sockaddr *kgetsa __P((struct sockaddr *)); 110 static void p_tree __P((struct radix_node *)); 111 static void p_rtnode __P(()); 112 static void ntreestuff __P(()); 113 static void np_rtentry __P((struct rt_msghdr *)); 114 static void p_sockaddr __P((struct sockaddr *, int, int)); 115 static void p_flags __P((int, char *)); 116 static void p_rtentry __P((struct rtentry *)); 117 118 /* 119 * Print routing tables. 120 */ 121 void 122 routepr(rtree) 123 u_long rtree; 124 { 125 struct radix_node_head *rnh, head; 126 int i; 127 128 printf("Routing tables\n"); 129 130 if (Aflag == 0 && NewTree) 131 ntreestuff(); 132 else { 133 if (rtree == 0) { 134 printf("rt_tables: symbol not in namelist\n"); 135 return; 136 } 137 138 kget(rtree, rt_tables); 139 for (i = 0; i <= AF_MAX; i++) { 140 if ((rnh = rt_tables[i]) == 0) 141 continue; 142 kget(rnh, head); 143 if (i == AF_UNSPEC) { 144 if (Aflag && af == 0) { 145 printf("Netmasks:\n"); 146 p_tree(head.rnh_treetop); 147 } 148 } else if (af == AF_UNSPEC || af == i) { 149 pr_family(i); 150 do_rtent = 1; 151 pr_rthdr(); 152 p_tree(head.rnh_treetop); 153 } 154 } 155 } 156 } 157 158 /* 159 * Print address family header before a section of the routing table. 160 */ 161 void 162 pr_family(af) 163 int af; 164 { 165 char *afname; 166 167 switch (af) { 168 case AF_INET: 169 afname = "Internet"; 170 break; 171 case AF_NS: 172 afname = "XNS"; 173 break; 174 case AF_ISO: 175 afname = "ISO"; 176 break; 177 case AF_CCITT: 178 afname = "X.25"; 179 break; 180 default: 181 afname = NULL; 182 break; 183 } 184 if (afname) 185 printf("\n%s:\n", afname); 186 else 187 printf("\nProtocol Family %d:\n", af); 188 } 189 190 /* column widths; each followed by one space */ 191 #define WID_DST 16 /* width of destination column */ 192 #define WID_GW 18 /* width of gateway column */ 193 194 /* 195 * Print header for routing table columns. 196 */ 197 void 198 pr_rthdr() 199 { 200 201 if (Aflag) 202 printf("%-8.8s ","Address"); 203 printf("%-*.*s %-*.*s %-6.6s %6.6s%8.8s %8.8s %6s\n", 204 WID_DST, WID_DST, "Destination", 205 WID_GW, WID_GW, "Gateway", 206 "Flags", "Refs", "Use", "Netif", "Expire"); 207 } 208 209 static struct sockaddr * 210 kgetsa(dst) 211 register struct sockaddr *dst; 212 { 213 214 kget(dst, pt_u.u_sa); 215 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa)) 216 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len); 217 return (&pt_u.u_sa); 218 } 219 220 static void 221 p_tree(rn) 222 struct radix_node *rn; 223 { 224 225 again: 226 kget(rn, rnode); 227 if (rnode.rn_b < 0) { 228 if (Aflag) 229 printf("%-8.8x ", rn); 230 if (rnode.rn_flags & RNF_ROOT) { 231 if (Aflag) 232 printf("(root node)%s", 233 rnode.rn_dupedkey ? " =>\n" : "\n"); 234 } else if (do_rtent) { 235 kget(rn, rtentry); 236 p_rtentry(&rtentry); 237 if (Aflag) 238 p_rtnode(); 239 } else { 240 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key), 241 0, 44); 242 putchar('\n'); 243 } 244 if (rn = rnode.rn_dupedkey) 245 goto again; 246 } else { 247 if (Aflag && do_rtent) { 248 printf("%-8.8x ", rn); 249 p_rtnode(); 250 } 251 rn = rnode.rn_r; 252 p_tree(rnode.rn_l); 253 p_tree(rn); 254 } 255 } 256 257 char nbuf[20]; 258 259 static void 260 p_rtnode() 261 { 262 struct radix_mask *rm = rnode.rn_mklist; 263 264 if (rnode.rn_b < 0) { 265 if (rnode.rn_mask) { 266 printf("\t mask "); 267 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask), 268 0, -1); 269 } else if (rm == 0) 270 return; 271 } else { 272 sprintf(nbuf, "(%d)", rnode.rn_b); 273 printf("%6.6s %8.8x : %8.8x", nbuf, rnode.rn_l, rnode.rn_r); 274 } 275 while (rm) { 276 kget(rm, rmask); 277 sprintf(nbuf, " %d refs, ", rmask.rm_refs); 278 printf(" mk = %8.8x {(%d),%s", 279 rm, -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " "); 280 p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), 0, -1); 281 putchar('}'); 282 if (rm = rmask.rm_mklist) 283 printf(" ->"); 284 } 285 putchar('\n'); 286 } 287 288 static void 289 ntreestuff() 290 { 291 size_t needed; 292 int mib[6]; 293 char *buf, *next, *lim; 294 register struct rt_msghdr *rtm; 295 296 mib[0] = CTL_NET; 297 mib[1] = PF_ROUTE; 298 mib[2] = 0; 299 mib[3] = 0; 300 mib[4] = NET_RT_DUMP; 301 mib[5] = 0; 302 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 303 err(1, "sysctl: net.route.0.0.dump estimate"); 304 } 305 306 if ((buf = malloc(needed)) == 0) { 307 err(2, "malloc(%lu)", (unsigned long)needed); 308 } 309 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 310 err(1, "sysctl: net.route.0.0.dump"); 311 } 312 lim = buf + needed; 313 for (next = buf; next < lim; next += rtm->rtm_msglen) { 314 rtm = (struct rt_msghdr *)next; 315 np_rtentry(rtm); 316 } 317 } 318 319 static void 320 np_rtentry(rtm) 321 register struct rt_msghdr *rtm; 322 { 323 register struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 324 #ifdef notdef 325 static int masks_done, banner_printed; 326 #endif 327 static int old_af; 328 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST; 329 330 #ifdef notdef 331 /* for the moment, netmasks are skipped over */ 332 if (!banner_printed) { 333 printf("Netmasks:\n"); 334 banner_printed = 1; 335 } 336 if (masks_done == 0) { 337 if (rtm->rtm_addrs != RTA_DST ) { 338 masks_done = 1; 339 af = sa->sa_family; 340 } 341 } else 342 #endif 343 af = sa->sa_family; 344 if (af != old_af) { 345 pr_family(af); 346 old_af = af; 347 } 348 if (rtm->rtm_addrs == RTA_DST) 349 p_sockaddr(sa, 0, 36); 350 else { 351 p_sockaddr(sa, rtm->rtm_flags, 16); 352 if (sa->sa_len == 0) 353 sa->sa_len = sizeof(long); 354 sa = (struct sockaddr *)(sa->sa_len + (char *)sa); 355 p_sockaddr(sa, 0, 18); 356 } 357 p_flags(rtm->rtm_flags & interesting, "%-6.6s "); 358 putchar('\n'); 359 } 360 361 static void 362 p_sockaddr(sa, flags, width) 363 struct sockaddr *sa; 364 int flags, width; 365 { 366 char workbuf[128], *cplim; 367 register char *cp = workbuf; 368 369 switch(sa->sa_family) { 370 case AF_INET: 371 { 372 register struct sockaddr_in *sin = (struct sockaddr_in *)sa; 373 374 cp = (sin->sin_addr.s_addr == 0) ? "default" : 375 ((flags & RTF_HOST) ? 376 routename(sin->sin_addr.s_addr) : 377 netname(sin->sin_addr.s_addr, 0L)); 378 break; 379 } 380 381 case AF_NS: 382 cp = ns_print(sa); 383 break; 384 385 case AF_LINK: 386 { 387 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; 388 389 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && 390 sdl->sdl_slen == 0) 391 (void) sprintf(workbuf, "link#%d", sdl->sdl_index); 392 else switch (sdl->sdl_type) { 393 case IFT_ETHER: 394 { 395 register int i; 396 register u_char *lla = (u_char *)sdl->sdl_data + 397 sdl->sdl_nlen; 398 399 cplim = ""; 400 for (i = 0; i < sdl->sdl_alen; i++, lla++) { 401 cp += sprintf(cp, "%s%x", cplim, *lla); 402 cplim = ":"; 403 } 404 cp = workbuf; 405 break; 406 } 407 default: 408 cp = link_ntoa(sdl); 409 break; 410 } 411 break; 412 } 413 414 default: 415 { 416 register u_char *s = (u_char *)sa->sa_data, *slim; 417 418 slim = sa->sa_len + (u_char *) sa; 419 cplim = cp + sizeof(workbuf) - 6; 420 cp += sprintf(cp, "(%d)", sa->sa_family); 421 while (s < slim && cp < cplim) { 422 cp += sprintf(cp, " %02x", *s++); 423 if (s < slim) 424 cp += sprintf(cp, "%02x", *s++); 425 } 426 cp = workbuf; 427 } 428 } 429 if (width < 0 ) 430 printf("%s ", cp); 431 else { 432 if (nflag) 433 printf("%-*s ", width, cp); 434 else 435 printf("%-*.*s ", width, width, cp); 436 } 437 } 438 439 static void 440 p_flags(f, format) 441 register int f; 442 char *format; 443 { 444 char name[33], *flags; 445 register struct bits *p = bits; 446 447 for (flags = name; p->b_mask; p++) 448 if (p->b_mask & f) 449 *flags++ = p->b_val; 450 *flags = '\0'; 451 printf(format, name); 452 } 453 454 static void 455 p_rtentry(rt) 456 register struct rtentry *rt; 457 { 458 static struct ifnet ifnet, *lastif; 459 static char name[16]; 460 static char prettyname[9]; 461 462 /* 463 * Don't print protocol-cloned routes unless -a. 464 */ 465 if(rt->rt_parent && !aflag) 466 return; 467 468 p_sockaddr(kgetsa(rt_key(rt)), rt->rt_flags, WID_DST); 469 p_sockaddr(kgetsa(rt->rt_gateway), RTF_HOST, WID_GW); 470 p_flags(rt->rt_flags, "%-6.6s "); 471 printf("%6d %8d ", rt->rt_refcnt, rt->rt_use); 472 if (rt->rt_ifp) { 473 if (rt->rt_ifp != lastif) { 474 kget(rt->rt_ifp, ifnet); 475 kread((u_long)ifnet.if_name, name, 16); 476 lastif = rt->rt_ifp; 477 snprintf(prettyname, sizeof prettyname, 478 "%.6s%d", name, ifnet.if_unit); 479 } 480 if(rt->rt_rmx.rmx_expire) { 481 time_t expire_time; 482 483 if ((expire_time 484 =rt->rt_rmx.rmx_expire - time((time_t *)0)) > 0) 485 printf(" %8.8s %6d%s", prettyname, 486 expire_time, 487 rt->rt_nodes[0].rn_dupedkey ? " =>" : ""); 488 } else { 489 printf(" %8.8s%s", prettyname, 490 rt->rt_nodes[0].rn_dupedkey ? " =>" : ""); 491 } 492 493 } 494 putchar('\n'); 495 } 496 497 char * 498 routename(in) 499 u_long in; 500 { 501 register char *cp; 502 static char line[MAXHOSTNAMELEN + 1]; 503 struct hostent *hp; 504 static char domain[MAXHOSTNAMELEN + 1]; 505 static int first = 1; 506 507 if (first) { 508 first = 0; 509 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 510 (cp = index(domain, '.'))) 511 (void) strcpy(domain, cp + 1); 512 else 513 domain[0] = 0; 514 } 515 cp = 0; 516 if (!nflag) { 517 hp = gethostbyaddr((char *)&in, sizeof (struct in_addr), 518 AF_INET); 519 if (hp) { 520 if ((cp = index(hp->h_name, '.')) && 521 !strcmp(cp + 1, domain)) 522 *cp = 0; 523 cp = hp->h_name; 524 } 525 } 526 if (cp) 527 strncpy(line, cp, sizeof(line) - 1); 528 else { 529 #define C(x) ((x) & 0xff) 530 in = ntohl(in); 531 sprintf(line, "%u.%u.%u.%u", 532 C(in >> 24), C(in >> 16), C(in >> 8), C(in)); 533 } 534 return (line); 535 } 536 537 /* 538 * Return the name of the network whose address is given. 539 * The address is assumed to be that of a net or subnet, not a host. 540 */ 541 char * 542 netname(in, mask) 543 u_long in, mask; 544 { 545 char *cp = 0; 546 static char line[MAXHOSTNAMELEN + 1]; 547 struct netent *np = 0; 548 u_long net; 549 register int i; 550 int subnetshift; 551 552 i = ntohl(in); 553 if (!nflag && i) { 554 if (mask == 0) { 555 if (IN_CLASSA(i)) { 556 mask = IN_CLASSA_NET; 557 subnetshift = 8; 558 } else if (IN_CLASSB(i)) { 559 mask = IN_CLASSB_NET; 560 subnetshift = 8; 561 } else { 562 mask = IN_CLASSC_NET; 563 subnetshift = 4; 564 } 565 /* 566 * If there are more bits than the standard mask 567 * would suggest, subnets must be in use. 568 * Guess at the subnet mask, assuming reasonable 569 * width subnet fields. 570 */ 571 while (i &~ mask) 572 mask = (long)mask >> subnetshift; 573 } 574 net = i & mask; 575 while ((mask & 1) == 0) 576 mask >>= 1, net >>= 1; 577 np = getnetbyaddr(net, AF_INET); 578 if (np) 579 cp = np->n_name; 580 } 581 if (cp) 582 strncpy(line, cp, sizeof(line) - 1); 583 else if ((i & 0xffffff) == 0) 584 sprintf(line, "%u", C(i >> 24)); 585 else if ((i & 0xffff) == 0) 586 sprintf(line, "%u.%u", C(i >> 24) , C(i >> 16)); 587 else if ((i & 0xff) == 0) 588 sprintf(line, "%u.%u.%u", C(i >> 24), C(i >> 16), C(i >> 8)); 589 else 590 sprintf(line, "%u.%u.%u.%u", C(i >> 24), 591 C(i >> 16), C(i >> 8), C(i)); 592 return (line); 593 } 594 595 /* 596 * Print routing statistics 597 */ 598 void 599 rt_stats(off) 600 u_long off; 601 { 602 struct rtstat rtstat; 603 604 if (off == 0) { 605 printf("rtstat: symbol not in namelist\n"); 606 return; 607 } 608 kread(off, (char *)&rtstat, sizeof (rtstat)); 609 printf("routing:\n"); 610 printf("\t%u bad routing redirect%s\n", 611 rtstat.rts_badredirect, plural(rtstat.rts_badredirect)); 612 printf("\t%u dynamically created route%s\n", 613 rtstat.rts_dynamic, plural(rtstat.rts_dynamic)); 614 printf("\t%u new gateway%s due to redirects\n", 615 rtstat.rts_newgateway, plural(rtstat.rts_newgateway)); 616 printf("\t%u destination%s found unreachable\n", 617 rtstat.rts_unreach, plural(rtstat.rts_unreach)); 618 printf("\t%u use%s of a wildcard route\n", 619 rtstat.rts_wildcard, plural(rtstat.rts_wildcard)); 620 } 621 short ns_nullh[] = {0,0,0}; 622 short ns_bh[] = {-1,-1,-1}; 623 624 char * 625 ns_print(sa) 626 register struct sockaddr *sa; 627 { 628 register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa; 629 struct ns_addr work; 630 union { union ns_net net_e; u_long long_e; } net; 631 u_short port; 632 static char mybuf[50], cport[10], chost[25]; 633 char *host = ""; 634 register char *p; register u_char *q; 635 636 work = sns->sns_addr; 637 port = ntohs(work.x_port); 638 work.x_port = 0; 639 net.net_e = work.x_net; 640 if (ns_nullhost(work) && net.long_e == 0) { 641 if (port ) { 642 sprintf(mybuf, "*.%xH", port); 643 upHex(mybuf); 644 } else 645 sprintf(mybuf, "*.*"); 646 return (mybuf); 647 } 648 649 if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) { 650 host = "any"; 651 } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) { 652 host = "*"; 653 } else { 654 q = work.x_host.c_host; 655 sprintf(chost, "%02x%02x%02x%02x%02x%02xH", 656 q[0], q[1], q[2], q[3], q[4], q[5]); 657 for (p = chost; *p == '0' && p < chost + 12; p++) 658 continue; 659 host = p; 660 } 661 if (port) 662 sprintf(cport, ".%xH", htons(port)); 663 else 664 *cport = 0; 665 666 sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport); 667 upHex(mybuf); 668 return(mybuf); 669 } 670 671 char * 672 ns_phost(sa) 673 struct sockaddr *sa; 674 { 675 register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa; 676 struct sockaddr_ns work; 677 static union ns_net ns_zeronet; 678 char *p; 679 680 work = *sns; 681 work.sns_addr.x_port = 0; 682 work.sns_addr.x_net = ns_zeronet; 683 684 p = ns_print((struct sockaddr *)&work); 685 if (strncmp("0H.", p, 3) == 0) p += 3; 686 return(p); 687 } 688 689 void 690 upHex(p0) 691 char *p0; 692 { 693 register char *p = p0; 694 for (; *p; p++) switch (*p) { 695 696 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 697 *p += ('A' - 'a'); 698 } 699 } 700