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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95"; 33 #endif /* not lint */ 34 #endif 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/protosw.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/time.h> 44 45 #include <net/ethernet.h> 46 #include <net/if.h> 47 #include <net/if_var.h> 48 #include <net/if_dl.h> 49 #include <net/if_types.h> 50 #include <net/radix.h> 51 #define _WANT_RTENTRY 52 #include <net/route.h> 53 54 #include <netinet/in.h> 55 #include <netgraph/ng_socket.h> 56 57 #include <sys/sysctl.h> 58 59 #include <arpa/inet.h> 60 #include <ifaddrs.h> 61 #include <libutil.h> 62 #include <netdb.h> 63 #include <nlist.h> 64 #include <stdint.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <sysexits.h> 69 #include <unistd.h> 70 #include <err.h> 71 #include "netstat.h" 72 73 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d))) 74 75 /* 76 * Definitions for showing gateway flags. 77 */ 78 struct bits { 79 u_long b_mask; 80 char b_val; 81 } bits[] = { 82 { RTF_UP, 'U' }, 83 { RTF_GATEWAY, 'G' }, 84 { RTF_HOST, 'H' }, 85 { RTF_REJECT, 'R' }, 86 { RTF_DYNAMIC, 'D' }, 87 { RTF_MODIFIED, 'M' }, 88 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */ 89 { RTF_XRESOLVE, 'X' }, 90 { RTF_STATIC, 'S' }, 91 { RTF_PROTO1, '1' }, 92 { RTF_PROTO2, '2' }, 93 { RTF_PROTO3, '3' }, 94 { RTF_BLACKHOLE,'B' }, 95 { RTF_BROADCAST,'b' }, 96 #ifdef RTF_LLINFO 97 { RTF_LLINFO, 'L' }, 98 #endif 99 { 0 , 0 } 100 }; 101 102 /* 103 * kvm(3) bindings for every needed symbol 104 */ 105 static struct nlist rl[] = { 106 #define N_RTSTAT 0 107 { .n_name = "_rtstat" }, 108 #define N_RTREE 1 109 { .n_name = "_rt_tables"}, 110 #define N_RTTRASH 2 111 { .n_name = "_rttrash" }, 112 { .n_name = NULL }, 113 }; 114 115 typedef union { 116 long dummy; /* Helps align structure. */ 117 struct sockaddr u_sa; 118 u_short u_data[128]; 119 } sa_u; 120 121 static sa_u pt_u; 122 123 struct ifmap_entry { 124 char ifname[IFNAMSIZ]; 125 }; 126 127 static struct ifmap_entry *ifmap; 128 static int ifmap_size; 129 130 int do_rtent = 0; 131 struct rtentry rtentry; 132 struct radix_node rnode; 133 struct radix_mask rmask; 134 135 int NewTree = 1; 136 137 struct timespec uptime; 138 139 static struct sockaddr *kgetsa(struct sockaddr *); 140 static void size_cols(int ef, struct radix_node *rn); 141 static void size_cols_tree(struct radix_node *rn); 142 static void size_cols_rtentry(struct rtentry *rt); 143 static void p_rtnode_kvm(void); 144 static void p_rtable_sysctl(int, int); 145 static void p_rtable_kvm(int, int ); 146 static void p_rtree_kvm(struct radix_node *); 147 static void p_rtentry_sysctl(struct rt_msghdr *); 148 static void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int); 149 static const char *fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, 150 int flags); 151 static void p_flags(int, const char *); 152 static const char *fmt_flags(int f); 153 static void p_rtentry_kvm(struct rtentry *); 154 static void domask(char *, in_addr_t, u_long); 155 156 /* 157 * Print routing tables. 158 */ 159 void 160 routepr(int fibnum, int af) 161 { 162 size_t intsize; 163 int numfibs; 164 165 intsize = sizeof(int); 166 if (fibnum == -1 && 167 sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1) 168 fibnum = 0; 169 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1) 170 numfibs = 1; 171 if (fibnum < 0 || fibnum > numfibs - 1) 172 errx(EX_USAGE, "%d: invalid fib", fibnum); 173 /* 174 * Since kernel & userland use different timebase 175 * (time_uptime vs time_second) and we are reading kernel memory 176 * directly we should do rt_expire --> expire_time conversion. 177 */ 178 if (clock_gettime(CLOCK_UPTIME, &uptime) < 0) 179 err(EX_OSERR, "clock_gettime() failed"); 180 181 printf("Routing tables"); 182 if (fibnum) 183 printf(" (fib: %d)", fibnum); 184 printf("\n"); 185 186 if (Aflag == 0 && live != 0 && NewTree) 187 p_rtable_sysctl(fibnum, af); 188 else 189 p_rtable_kvm(fibnum, af); 190 } 191 192 193 /* 194 * Print address family header before a section of the routing table. 195 */ 196 void 197 pr_family(int af1) 198 { 199 const char *afname; 200 201 switch (af1) { 202 case AF_INET: 203 afname = "Internet"; 204 break; 205 #ifdef INET6 206 case AF_INET6: 207 afname = "Internet6"; 208 break; 209 #endif /*INET6*/ 210 case AF_ISO: 211 afname = "ISO"; 212 break; 213 case AF_CCITT: 214 afname = "X.25"; 215 break; 216 case AF_NETGRAPH: 217 afname = "Netgraph"; 218 break; 219 default: 220 afname = NULL; 221 break; 222 } 223 if (afname) 224 printf("\n%s:\n", afname); 225 else 226 printf("\nProtocol Family %d:\n", af1); 227 } 228 229 /* column widths; each followed by one space */ 230 #ifndef INET6 231 #define WID_DST_DEFAULT(af) 18 /* width of destination column */ 232 #define WID_GW_DEFAULT(af) 18 /* width of gateway column */ 233 #define WID_IF_DEFAULT(af) (Wflag ? 8 : 6) /* width of netif column */ 234 #else 235 #define WID_DST_DEFAULT(af) \ 236 ((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18) 237 #define WID_GW_DEFAULT(af) \ 238 ((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18) 239 #define WID_IF_DEFAULT(af) ((af) == AF_INET6 ? 8 : (Wflag ? 8 : 6)) 240 #endif /*INET6*/ 241 242 static int wid_dst; 243 static int wid_gw; 244 static int wid_flags; 245 static int wid_pksent; 246 static int wid_mtu; 247 static int wid_if; 248 static int wid_expire; 249 250 static void 251 size_cols(int ef, struct radix_node *rn) 252 { 253 wid_dst = WID_DST_DEFAULT(ef); 254 wid_gw = WID_GW_DEFAULT(ef); 255 wid_flags = 6; 256 wid_pksent = 8; 257 wid_mtu = 6; 258 wid_if = WID_IF_DEFAULT(ef); 259 wid_expire = 6; 260 261 if (Wflag && rn != NULL) 262 size_cols_tree(rn); 263 } 264 265 static void 266 size_cols_tree(struct radix_node *rn) 267 { 268 again: 269 if (kget(rn, rnode) != 0) 270 return; 271 if (!(rnode.rn_flags & RNF_ACTIVE)) 272 return; 273 if (rnode.rn_bit < 0) { 274 if ((rnode.rn_flags & RNF_ROOT) == 0) { 275 if (kget(rn, rtentry) != 0) 276 return; 277 size_cols_rtentry(&rtentry); 278 } 279 if ((rn = rnode.rn_dupedkey)) 280 goto again; 281 } else { 282 rn = rnode.rn_right; 283 size_cols_tree(rnode.rn_left); 284 size_cols_tree(rn); 285 } 286 } 287 288 static void 289 size_cols_rtentry(struct rtentry *rt) 290 { 291 static struct ifnet ifnet, *lastif; 292 static char buffer[100]; 293 const char *bp; 294 struct sockaddr *sa; 295 sa_u addr, mask; 296 int len; 297 298 bzero(&addr, sizeof(addr)); 299 if ((sa = kgetsa(rt_key(rt)))) 300 bcopy(sa, &addr, sa->sa_len); 301 bzero(&mask, sizeof(mask)); 302 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt)))) 303 bcopy(sa, &mask, sa->sa_len); 304 bp = fmt_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags); 305 len = strlen(bp); 306 wid_dst = MAX(len, wid_dst); 307 308 bp = fmt_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST); 309 len = strlen(bp); 310 wid_gw = MAX(len, wid_gw); 311 312 bp = fmt_flags(rt->rt_flags); 313 len = strlen(bp); 314 wid_flags = MAX(len, wid_flags); 315 316 if (Wflag) { 317 len = snprintf(buffer, sizeof(buffer), "%ju", 318 (uintmax_t )kread_counter((u_long )rt->rt_pksent)); 319 wid_pksent = MAX(len, wid_pksent); 320 } 321 if (rt->rt_ifp) { 322 if (rt->rt_ifp != lastif) { 323 if (kget(rt->rt_ifp, ifnet) == 0) 324 len = strlen(ifnet.if_xname); 325 else 326 len = strlen("---"); 327 lastif = rt->rt_ifp; 328 wid_if = MAX(len, wid_if); 329 } 330 if (rt->rt_expire) { 331 time_t expire_time; 332 333 if ((expire_time = 334 rt->rt_expire - uptime.tv_sec) > 0) { 335 len = snprintf(buffer, sizeof(buffer), "%d", 336 (int)expire_time); 337 wid_expire = MAX(len, wid_expire); 338 } 339 } 340 } 341 } 342 343 344 /* 345 * Print header for routing table columns. 346 */ 347 void 348 pr_rthdr(int af1) 349 { 350 351 if (Aflag) 352 printf("%-8.8s ","Address"); 353 if (Wflag) { 354 printf("%-*.*s %-*.*s %-*.*s %*.*s %*.*s %*.*s %*s\n", 355 wid_dst, wid_dst, "Destination", 356 wid_gw, wid_gw, "Gateway", 357 wid_flags, wid_flags, "Flags", 358 wid_pksent, wid_pksent, "Use", 359 wid_mtu, wid_mtu, "Mtu", 360 wid_if, wid_if, "Netif", 361 wid_expire, "Expire"); 362 } else { 363 printf("%-*.*s %-*.*s %-*.*s %*.*s %*s\n", 364 wid_dst, wid_dst, "Destination", 365 wid_gw, wid_gw, "Gateway", 366 wid_flags, wid_flags, "Flags", 367 wid_if, wid_if, "Netif", 368 wid_expire, "Expire"); 369 } 370 } 371 372 static struct sockaddr * 373 kgetsa(struct sockaddr *dst) 374 { 375 376 if (kget(dst, pt_u.u_sa) != 0) 377 return (NULL); 378 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa)) 379 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len); 380 return (&pt_u.u_sa); 381 } 382 383 /* 384 * Print kernel routing tables for given fib 385 * using debugging kvm(3) interface. 386 */ 387 static void 388 p_rtable_kvm(int fibnum, int af) 389 { 390 struct radix_node_head **rnhp, *rnh, head; 391 struct radix_node_head **rt_tables; 392 u_long rtree; 393 int fam, af_size; 394 395 kresolve_list(rl); 396 if ((rtree = rl[N_RTREE].n_value) == 0) { 397 printf("rt_tables: symbol not in namelist\n"); 398 return; 399 } 400 401 af_size = (AF_MAX + 1) * sizeof(struct radix_node_head *); 402 rt_tables = calloc(1, af_size); 403 if (rt_tables == NULL) 404 err(EX_OSERR, "memory allocation failed"); 405 406 if (kread((u_long)(rtree), (char *)(rt_tables) + fibnum * af_size, 407 af_size) != 0) 408 err(EX_OSERR, "error retrieving radix pointers"); 409 for (fam = 0; fam <= AF_MAX; fam++) { 410 int tmpfib; 411 412 switch (fam) { 413 case AF_INET6: 414 case AF_INET: 415 tmpfib = fibnum; 416 break; 417 default: 418 tmpfib = 0; 419 } 420 rnhp = (struct radix_node_head **)*rt_tables; 421 /* Calculate the in-kernel address. */ 422 rnhp += tmpfib * (AF_MAX + 1) + fam; 423 /* Read the in kernel rhn pointer. */ 424 if (kget(rnhp, rnh) != 0) 425 continue; 426 if (rnh == NULL) 427 continue; 428 /* Read the rnh data. */ 429 if (kget(rnh, head) != 0) 430 continue; 431 if (fam == AF_UNSPEC) { 432 if (Aflag && af == 0) { 433 printf("Netmasks:\n"); 434 p_rtree_kvm(head.rnh_treetop); 435 } 436 } else if (af == AF_UNSPEC || af == fam) { 437 size_cols(fam, head.rnh_treetop); 438 pr_family(fam); 439 do_rtent = 1; 440 pr_rthdr(fam); 441 p_rtree_kvm(head.rnh_treetop); 442 } 443 } 444 445 free(rt_tables); 446 } 447 448 /* 449 * Print given kernel radix tree using 450 * debugging kvm(3) interface. 451 */ 452 static void 453 p_rtree_kvm(struct radix_node *rn) 454 { 455 456 again: 457 if (kget(rn, rnode) != 0) 458 return; 459 if (!(rnode.rn_flags & RNF_ACTIVE)) 460 return; 461 if (rnode.rn_bit < 0) { 462 if (Aflag) 463 printf("%-8.8lx ", (u_long)rn); 464 if (rnode.rn_flags & RNF_ROOT) { 465 if (Aflag) 466 printf("(root node)%s", 467 rnode.rn_dupedkey ? " =>\n" : "\n"); 468 } else if (do_rtent) { 469 if (kget(rn, rtentry) == 0) { 470 p_rtentry_kvm(&rtentry); 471 if (Aflag) 472 p_rtnode_kvm(); 473 } 474 } else { 475 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key), 476 NULL, 0, 44); 477 putchar('\n'); 478 } 479 if ((rn = rnode.rn_dupedkey)) 480 goto again; 481 } else { 482 if (Aflag && do_rtent) { 483 printf("%-8.8lx ", (u_long)rn); 484 p_rtnode_kvm(); 485 } 486 rn = rnode.rn_right; 487 p_rtree_kvm(rnode.rn_left); 488 p_rtree_kvm(rn); 489 } 490 } 491 492 char nbuf[20]; 493 494 static void 495 p_rtnode_kvm(void) 496 { 497 struct radix_mask *rm = rnode.rn_mklist; 498 499 if (rnode.rn_bit < 0) { 500 if (rnode.rn_mask) { 501 printf("\t mask "); 502 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask), 503 NULL, 0, -1); 504 } else if (rm == 0) 505 return; 506 } else { 507 sprintf(nbuf, "(%d)", rnode.rn_bit); 508 printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right); 509 } 510 while (rm) { 511 if (kget(rm, rmask) != 0) 512 break; 513 sprintf(nbuf, " %d refs, ", rmask.rm_refs); 514 printf(" mk = %8.8lx {(%d),%s", 515 (u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " "); 516 if (rmask.rm_flags & RNF_NORMAL) { 517 struct radix_node rnode_aux; 518 printf(" <normal>, "); 519 if (kget(rmask.rm_leaf, rnode_aux) == 0) 520 p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask), 521 NULL, 0, -1); 522 else 523 p_sockaddr(NULL, NULL, 0, -1); 524 } else 525 p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), 526 NULL, 0, -1); 527 putchar('}'); 528 if ((rm = rmask.rm_mklist)) 529 printf(" ->"); 530 } 531 putchar('\n'); 532 } 533 534 static void 535 p_rtable_sysctl(int fibnum, int af) 536 { 537 size_t needed; 538 int mib[7]; 539 char *buf, *next, *lim; 540 struct rt_msghdr *rtm; 541 struct sockaddr *sa; 542 int fam = 0, ifindex = 0, size; 543 544 struct ifaddrs *ifap, *ifa; 545 struct sockaddr_dl *sdl; 546 547 /* 548 * Retrieve interface list at first 549 * since we need #ifindex -> if_xname match 550 */ 551 if (getifaddrs(&ifap) != 0) 552 err(EX_OSERR, "getifaddrs"); 553 554 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 555 556 if (ifa->ifa_addr->sa_family != AF_LINK) 557 continue; 558 559 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 560 ifindex = sdl->sdl_index; 561 562 if (ifindex >= ifmap_size) { 563 size = roundup(ifindex + 1, 32) * 564 sizeof(struct ifmap_entry); 565 if ((ifmap = realloc(ifmap, size)) == NULL) 566 errx(2, "realloc(%d) failed", size); 567 memset(&ifmap[ifmap_size], 0, 568 size - ifmap_size * 569 sizeof(struct ifmap_entry)); 570 571 ifmap_size = roundup(ifindex + 1, 32); 572 } 573 574 if (*ifmap[ifindex].ifname != '\0') 575 continue; 576 577 strlcpy(ifmap[ifindex].ifname, ifa->ifa_name, IFNAMSIZ); 578 } 579 580 freeifaddrs(ifap); 581 582 mib[0] = CTL_NET; 583 mib[1] = PF_ROUTE; 584 mib[2] = 0; 585 mib[3] = af; 586 mib[4] = NET_RT_DUMP; 587 mib[5] = 0; 588 mib[6] = fibnum; 589 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) { 590 err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af, 591 fibnum); 592 } 593 594 if ((buf = malloc(needed)) == 0) { 595 errx(2, "malloc(%lu)", (unsigned long)needed); 596 } 597 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 598 err(1, "sysctl: net.route.0.%d.dump.%d", af, fibnum); 599 } 600 lim = buf + needed; 601 for (next = buf; next < lim; next += rtm->rtm_msglen) { 602 rtm = (struct rt_msghdr *)next; 603 if (rtm->rtm_version != RTM_VERSION) 604 continue; 605 /* 606 * Peek inside header to determine AF 607 */ 608 sa = (struct sockaddr *)(rtm + 1); 609 if (fam != sa->sa_family) { 610 fam = sa->sa_family; 611 size_cols(fam, NULL); 612 pr_family(fam); 613 pr_rthdr(fam); 614 } 615 p_rtentry_sysctl(rtm); 616 } 617 free(buf); 618 } 619 620 static void 621 p_rtentry_sysctl(struct rt_msghdr *rtm) 622 { 623 struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 624 char buffer[128]; 625 char prettyname[128]; 626 sa_u addr, mask, gw; 627 unsigned int l; 628 629 #define GETSA(_s, _f) { \ 630 bzero(&(_s), sizeof(_s)); \ 631 if (rtm->rtm_addrs & _f) { \ 632 l = roundup(sa->sa_len, sizeof(long)); \ 633 memcpy(&(_s), sa, (l > sizeof(_s)) ? sizeof(_s) : l); \ 634 sa = (struct sockaddr *)((char *)sa + l); \ 635 } \ 636 } 637 638 GETSA(addr, RTA_DST); 639 GETSA(gw, RTA_GATEWAY); 640 GETSA(mask, RTA_NETMASK); 641 p_sockaddr(&addr.u_sa, &mask.u_sa, rtm->rtm_flags, wid_dst); 642 p_sockaddr(&gw.u_sa, NULL, RTF_HOST, wid_gw); 643 644 snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags); 645 p_flags(rtm->rtm_flags, buffer); 646 if (Wflag) { 647 printf("%*lu ", wid_pksent, rtm->rtm_rmx.rmx_pksent); 648 649 if (rtm->rtm_rmx.rmx_mtu != 0) 650 printf("%*lu ", wid_mtu, rtm->rtm_rmx.rmx_mtu); 651 else 652 printf("%*s ", wid_mtu, ""); 653 } 654 655 memset(prettyname, 0, sizeof(prettyname)); 656 if (rtm->rtm_index < ifmap_size) { 657 strlcpy(prettyname, ifmap[rtm->rtm_index].ifname, 658 sizeof(prettyname)); 659 if (*prettyname == '\0') 660 strlcpy(prettyname, "---", sizeof(prettyname)); 661 } 662 663 printf("%*.*s", wid_if, wid_if, prettyname); 664 if (rtm->rtm_rmx.rmx_expire) { 665 time_t expire_time; 666 667 if ((expire_time = 668 rtm->rtm_rmx.rmx_expire - uptime.tv_sec) > 0) 669 printf(" %*d", wid_expire, (int)expire_time); 670 } 671 672 putchar('\n'); 673 } 674 675 static void 676 p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width) 677 { 678 const char *cp; 679 680 cp = fmt_sockaddr(sa, mask, flags); 681 682 if (width < 0 ) 683 printf("%s ", cp); 684 else { 685 if (numeric_addr) 686 printf("%-*s ", width, cp); 687 else 688 printf("%-*.*s ", width, width, cp); 689 } 690 } 691 692 static const char * 693 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags) 694 { 695 static char workbuf[128]; 696 const char *cp; 697 698 if (sa == NULL) 699 return ("null"); 700 701 switch(sa->sa_family) { 702 case AF_INET: 703 { 704 struct sockaddr_in *sockin = (struct sockaddr_in *)sa; 705 706 if ((sockin->sin_addr.s_addr == INADDR_ANY) && 707 mask && 708 ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr) 709 ==0L) 710 cp = "default" ; 711 else if (flags & RTF_HOST) 712 cp = routename(sockin->sin_addr.s_addr); 713 else if (mask) 714 cp = netname(sockin->sin_addr.s_addr, 715 ((struct sockaddr_in *)mask)->sin_addr.s_addr); 716 else 717 cp = netname(sockin->sin_addr.s_addr, INADDR_ANY); 718 break; 719 } 720 721 #ifdef INET6 722 case AF_INET6: 723 { 724 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; 725 726 /* 727 * The sa6->sin6_scope_id must be filled here because 728 * this sockaddr is extracted from kmem(4) directly 729 * and has KAME-specific embedded scope id in 730 * sa6->sin6_addr.s6_addr[2]. 731 */ 732 in6_fillscopeid(sa6); 733 734 if (flags & RTF_HOST) 735 cp = routename6(sa6); 736 else if (mask) 737 cp = netname6(sa6, 738 &((struct sockaddr_in6 *)mask)->sin6_addr); 739 else { 740 cp = netname6(sa6, NULL); 741 } 742 break; 743 } 744 #endif /*INET6*/ 745 746 case AF_NETGRAPH: 747 { 748 strlcpy(workbuf, ((struct sockaddr_ng *)sa)->sg_data, 749 sizeof(workbuf)); 750 cp = workbuf; 751 break; 752 } 753 754 case AF_LINK: 755 { 756 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; 757 758 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && 759 sdl->sdl_slen == 0) { 760 (void) sprintf(workbuf, "link#%d", sdl->sdl_index); 761 cp = workbuf; 762 } else 763 switch (sdl->sdl_type) { 764 765 case IFT_ETHER: 766 case IFT_L2VLAN: 767 case IFT_BRIDGE: 768 if (sdl->sdl_alen == ETHER_ADDR_LEN) { 769 cp = ether_ntoa((struct ether_addr *) 770 (sdl->sdl_data + sdl->sdl_nlen)); 771 break; 772 } 773 /* FALLTHROUGH */ 774 default: 775 cp = link_ntoa(sdl); 776 break; 777 } 778 break; 779 } 780 781 default: 782 { 783 u_char *s = (u_char *)sa->sa_data, *slim; 784 char *cq, *cqlim; 785 786 cq = workbuf; 787 slim = sa->sa_len + (u_char *) sa; 788 cqlim = cq + sizeof(workbuf) - 6; 789 cq += sprintf(cq, "(%d)", sa->sa_family); 790 while (s < slim && cq < cqlim) { 791 cq += sprintf(cq, " %02x", *s++); 792 if (s < slim) 793 cq += sprintf(cq, "%02x", *s++); 794 } 795 cp = workbuf; 796 } 797 } 798 799 return (cp); 800 } 801 802 static void 803 p_flags(int f, const char *format) 804 { 805 printf(format, fmt_flags(f)); 806 } 807 808 static const char * 809 fmt_flags(int f) 810 { 811 static char name[33]; 812 char *flags; 813 struct bits *p = bits; 814 815 for (flags = name; p->b_mask; p++) 816 if (p->b_mask & f) 817 *flags++ = p->b_val; 818 *flags = '\0'; 819 return (name); 820 } 821 822 static void 823 p_rtentry_kvm(struct rtentry *rt) 824 { 825 static struct ifnet ifnet, *lastif; 826 static char buffer[128]; 827 static char prettyname[128]; 828 struct sockaddr *sa; 829 sa_u addr, mask; 830 831 bzero(&addr, sizeof(addr)); 832 if ((sa = kgetsa(rt_key(rt)))) 833 bcopy(sa, &addr, sa->sa_len); 834 bzero(&mask, sizeof(mask)); 835 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt)))) 836 bcopy(sa, &mask, sa->sa_len); 837 p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, wid_dst); 838 p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, wid_gw); 839 snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags); 840 p_flags(rt->rt_flags, buffer); 841 if (Wflag) { 842 printf("%*ju ", wid_pksent, 843 (uintmax_t )kread_counter((u_long )rt->rt_pksent)); 844 845 if (rt->rt_mtu != 0) 846 printf("%*lu ", wid_mtu, rt->rt_mtu); 847 else 848 printf("%*s ", wid_mtu, ""); 849 } 850 if (rt->rt_ifp) { 851 if (rt->rt_ifp != lastif) { 852 if (kget(rt->rt_ifp, ifnet) == 0) 853 strlcpy(prettyname, ifnet.if_xname, 854 sizeof(prettyname)); 855 else 856 strlcpy(prettyname, "---", sizeof(prettyname)); 857 lastif = rt->rt_ifp; 858 } 859 printf("%*.*s", wid_if, wid_if, prettyname); 860 if (rt->rt_expire) { 861 time_t expire_time; 862 863 if ((expire_time = 864 rt->rt_expire - uptime.tv_sec) > 0) 865 printf(" %*d", wid_expire, (int)expire_time); 866 } 867 if (rt->rt_nodes[0].rn_dupedkey) 868 printf(" =>"); 869 } 870 putchar('\n'); 871 } 872 873 char * 874 routename(in_addr_t in) 875 { 876 char *cp; 877 static char line[MAXHOSTNAMELEN]; 878 struct hostent *hp; 879 880 cp = 0; 881 if (!numeric_addr) { 882 hp = gethostbyaddr(&in, sizeof (struct in_addr), AF_INET); 883 if (hp) { 884 cp = hp->h_name; 885 trimdomain(cp, strlen(cp)); 886 } 887 } 888 if (cp) { 889 strlcpy(line, cp, sizeof(line)); 890 } else { 891 #define C(x) ((x) & 0xff) 892 in = ntohl(in); 893 sprintf(line, "%u.%u.%u.%u", 894 C(in >> 24), C(in >> 16), C(in >> 8), C(in)); 895 } 896 return (line); 897 } 898 899 #define NSHIFT(m) ( \ 900 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \ 901 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \ 902 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \ 903 0) 904 905 static void 906 domask(char *dst, in_addr_t addr __unused, u_long mask) 907 { 908 int b, i; 909 910 if (mask == 0 || (!numeric_addr && NSHIFT(mask) != 0)) { 911 *dst = '\0'; 912 return; 913 } 914 i = 0; 915 for (b = 0; b < 32; b++) 916 if (mask & (1 << b)) { 917 int bb; 918 919 i = b; 920 for (bb = b+1; bb < 32; bb++) 921 if (!(mask & (1 << bb))) { 922 i = -1; /* noncontig */ 923 break; 924 } 925 break; 926 } 927 if (i == -1) 928 sprintf(dst, "&0x%lx", mask); 929 else 930 sprintf(dst, "/%d", 32-i); 931 } 932 933 /* 934 * Return the name of the network whose address is given. 935 */ 936 char * 937 netname(in_addr_t in, in_addr_t mask) 938 { 939 char *cp = 0; 940 static char line[MAXHOSTNAMELEN]; 941 struct netent *np = 0; 942 in_addr_t i; 943 944 /* It is ok to supply host address. */ 945 in &= mask; 946 947 i = ntohl(in); 948 if (!numeric_addr && i) { 949 np = getnetbyaddr(i >> NSHIFT(ntohl(mask)), AF_INET); 950 if (np != NULL) { 951 cp = np->n_name; 952 trimdomain(cp, strlen(cp)); 953 } 954 } 955 if (cp != NULL) { 956 strlcpy(line, cp, sizeof(line)); 957 } else { 958 inet_ntop(AF_INET, &in, line, sizeof(line) - 1); 959 } 960 domask(line + strlen(line), i, ntohl(mask)); 961 return (line); 962 } 963 964 #undef NSHIFT 965 966 #ifdef INET6 967 void 968 in6_fillscopeid(struct sockaddr_in6 *sa6) 969 { 970 #if defined(__KAME__) 971 /* 972 * XXX: This is a special workaround for KAME kernels. 973 * sin6_scope_id field of SA should be set in the future. 974 */ 975 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) || 976 IN6_IS_ADDR_MC_NODELOCAL(&sa6->sin6_addr) || 977 IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) { 978 if (sa6->sin6_scope_id == 0) 979 sa6->sin6_scope_id = 980 ntohs(*(u_int16_t *)&sa6->sin6_addr.s6_addr[2]); 981 sa6->sin6_addr.s6_addr[2] = sa6->sin6_addr.s6_addr[3] = 0; 982 } 983 #endif 984 } 985 986 const char * 987 netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask) 988 { 989 static char line[MAXHOSTNAMELEN]; 990 u_char *p = (u_char *)mask; 991 u_char *lim; 992 int masklen, illegal = 0, flag = 0; 993 994 if (mask) { 995 for (masklen = 0, lim = p + 16; p < lim; p++) { 996 switch (*p) { 997 case 0xff: 998 masklen += 8; 999 break; 1000 case 0xfe: 1001 masklen += 7; 1002 break; 1003 case 0xfc: 1004 masklen += 6; 1005 break; 1006 case 0xf8: 1007 masklen += 5; 1008 break; 1009 case 0xf0: 1010 masklen += 4; 1011 break; 1012 case 0xe0: 1013 masklen += 3; 1014 break; 1015 case 0xc0: 1016 masklen += 2; 1017 break; 1018 case 0x80: 1019 masklen += 1; 1020 break; 1021 case 0x00: 1022 break; 1023 default: 1024 illegal ++; 1025 break; 1026 } 1027 } 1028 if (illegal) 1029 fprintf(stderr, "illegal prefixlen\n"); 1030 } 1031 else 1032 masklen = 128; 1033 1034 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr)) 1035 return("default"); 1036 1037 if (numeric_addr) 1038 flag |= NI_NUMERICHOST; 1039 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line), 1040 NULL, 0, flag); 1041 1042 if (numeric_addr) 1043 sprintf(&line[strlen(line)], "/%d", masklen); 1044 1045 return line; 1046 } 1047 1048 char * 1049 routename6(struct sockaddr_in6 *sa6) 1050 { 1051 static char line[MAXHOSTNAMELEN]; 1052 int flag = 0; 1053 /* use local variable for safety */ 1054 struct sockaddr_in6 sa6_local; 1055 1056 sa6_local.sin6_family = AF_INET6; 1057 sa6_local.sin6_len = sizeof(sa6_local); 1058 sa6_local.sin6_addr = sa6->sin6_addr; 1059 sa6_local.sin6_scope_id = sa6->sin6_scope_id; 1060 1061 if (numeric_addr) 1062 flag |= NI_NUMERICHOST; 1063 1064 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len, 1065 line, sizeof(line), NULL, 0, flag); 1066 1067 return line; 1068 } 1069 #endif /*INET6*/ 1070 1071 /* 1072 * Print routing statistics 1073 */ 1074 void 1075 rt_stats(void) 1076 { 1077 struct rtstat rtstat; 1078 u_long rtsaddr, rttaddr; 1079 int rttrash; 1080 1081 kresolve_list(rl); 1082 1083 if ((rtsaddr = rl[N_RTSTAT].n_value) == 0) { 1084 printf("rtstat: symbol not in namelist\n"); 1085 return; 1086 } 1087 if ((rttaddr = rl[N_RTTRASH].n_value) == 0) { 1088 printf("rttrash: symbol not in namelist\n"); 1089 return; 1090 } 1091 kread(rtsaddr, (char *)&rtstat, sizeof (rtstat)); 1092 kread(rttaddr, (char *)&rttrash, sizeof (rttrash)); 1093 printf("routing:\n"); 1094 1095 #define p(f, m) if (rtstat.f || sflag <= 1) \ 1096 printf(m, rtstat.f, plural(rtstat.f)) 1097 1098 p(rts_badredirect, "\t%hu bad routing redirect%s\n"); 1099 p(rts_dynamic, "\t%hu dynamically created route%s\n"); 1100 p(rts_newgateway, "\t%hu new gateway%s due to redirects\n"); 1101 p(rts_unreach, "\t%hu destination%s found unreachable\n"); 1102 p(rts_wildcard, "\t%hu use%s of a wildcard route\n"); 1103 #undef p 1104 1105 if (rttrash || sflag <= 1) 1106 printf("\t%u route%s not in table but not freed\n", 1107 rttrash, plural(rttrash)); 1108 } 1109