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 ? 10 : 8) /* 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 ? 10 : 8)) 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 if ((buf = malloc(needed)) == NULL) 593 errx(2, "malloc(%lu)", (unsigned long)needed); 594 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) 595 err(1, "sysctl: net.route.0.%d.dump.%d", af, fibnum); 596 lim = buf + needed; 597 for (next = buf; next < lim; next += rtm->rtm_msglen) { 598 rtm = (struct rt_msghdr *)next; 599 if (rtm->rtm_version != RTM_VERSION) 600 continue; 601 /* 602 * Peek inside header to determine AF 603 */ 604 sa = (struct sockaddr *)(rtm + 1); 605 if (fam != sa->sa_family) { 606 fam = sa->sa_family; 607 size_cols(fam, NULL); 608 pr_family(fam); 609 pr_rthdr(fam); 610 } 611 p_rtentry_sysctl(rtm); 612 } 613 free(buf); 614 } 615 616 static void 617 p_rtentry_sysctl(struct rt_msghdr *rtm) 618 { 619 struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 620 char buffer[128]; 621 char prettyname[128]; 622 sa_u addr, mask, gw; 623 unsigned int l; 624 625 #define GETSA(_s, _f) { \ 626 bzero(&(_s), sizeof(_s)); \ 627 if (rtm->rtm_addrs & _f) { \ 628 l = roundup(sa->sa_len, sizeof(long)); \ 629 memcpy(&(_s), sa, (l > sizeof(_s)) ? sizeof(_s) : l); \ 630 sa = (struct sockaddr *)((char *)sa + l); \ 631 } \ 632 } 633 634 GETSA(addr, RTA_DST); 635 GETSA(gw, RTA_GATEWAY); 636 GETSA(mask, RTA_NETMASK); 637 p_sockaddr(&addr.u_sa, &mask.u_sa, rtm->rtm_flags, wid_dst); 638 p_sockaddr(&gw.u_sa, NULL, RTF_HOST, wid_gw); 639 640 snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags); 641 p_flags(rtm->rtm_flags, buffer); 642 if (Wflag) { 643 printf("%*lu ", wid_pksent, rtm->rtm_rmx.rmx_pksent); 644 645 if (rtm->rtm_rmx.rmx_mtu != 0) 646 printf("%*lu ", wid_mtu, rtm->rtm_rmx.rmx_mtu); 647 else 648 printf("%*s ", wid_mtu, ""); 649 } 650 651 memset(prettyname, 0, sizeof(prettyname)); 652 if (rtm->rtm_index < ifmap_size) { 653 strlcpy(prettyname, ifmap[rtm->rtm_index].ifname, 654 sizeof(prettyname)); 655 if (*prettyname == '\0') 656 strlcpy(prettyname, "---", sizeof(prettyname)); 657 } 658 659 printf("%*.*s", wid_if, wid_if, prettyname); 660 if (rtm->rtm_rmx.rmx_expire) { 661 time_t expire_time; 662 663 if ((expire_time = 664 rtm->rtm_rmx.rmx_expire - uptime.tv_sec) > 0) 665 printf(" %*d", wid_expire, (int)expire_time); 666 } 667 668 putchar('\n'); 669 } 670 671 static void 672 p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width) 673 { 674 const char *cp; 675 676 cp = fmt_sockaddr(sa, mask, flags); 677 678 if (width < 0 ) 679 printf("%s ", cp); 680 else { 681 if (numeric_addr) 682 printf("%-*s ", width, cp); 683 else 684 printf("%-*.*s ", width, width, cp); 685 } 686 } 687 688 static const char * 689 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags) 690 { 691 static char workbuf[128]; 692 const char *cp; 693 694 if (sa == NULL) 695 return ("null"); 696 697 switch(sa->sa_family) { 698 case AF_INET: 699 { 700 struct sockaddr_in *sockin = (struct sockaddr_in *)sa; 701 702 if ((sockin->sin_addr.s_addr == INADDR_ANY) && 703 mask && 704 ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr) 705 ==0L) 706 cp = "default" ; 707 else if (flags & RTF_HOST) 708 cp = routename(sockin->sin_addr.s_addr); 709 else if (mask) 710 cp = netname(sockin->sin_addr.s_addr, 711 ((struct sockaddr_in *)mask)->sin_addr.s_addr); 712 else 713 cp = netname(sockin->sin_addr.s_addr, INADDR_ANY); 714 break; 715 } 716 717 #ifdef INET6 718 case AF_INET6: 719 { 720 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; 721 722 /* 723 * The sa6->sin6_scope_id must be filled here because 724 * this sockaddr is extracted from kmem(4) directly 725 * and has KAME-specific embedded scope id in 726 * sa6->sin6_addr.s6_addr[2]. 727 */ 728 in6_fillscopeid(sa6); 729 730 if (flags & RTF_HOST) 731 cp = routename6(sa6); 732 else if (mask) 733 cp = netname6(sa6, 734 &((struct sockaddr_in6 *)mask)->sin6_addr); 735 else { 736 cp = netname6(sa6, NULL); 737 } 738 break; 739 } 740 #endif /*INET6*/ 741 742 case AF_NETGRAPH: 743 { 744 strlcpy(workbuf, ((struct sockaddr_ng *)sa)->sg_data, 745 sizeof(workbuf)); 746 cp = workbuf; 747 break; 748 } 749 750 case AF_LINK: 751 { 752 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; 753 754 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && 755 sdl->sdl_slen == 0) { 756 (void) sprintf(workbuf, "link#%d", sdl->sdl_index); 757 cp = workbuf; 758 } else 759 switch (sdl->sdl_type) { 760 761 case IFT_ETHER: 762 case IFT_L2VLAN: 763 case IFT_BRIDGE: 764 if (sdl->sdl_alen == ETHER_ADDR_LEN) { 765 cp = ether_ntoa((struct ether_addr *) 766 (sdl->sdl_data + sdl->sdl_nlen)); 767 break; 768 } 769 /* FALLTHROUGH */ 770 default: 771 cp = link_ntoa(sdl); 772 break; 773 } 774 break; 775 } 776 777 default: 778 { 779 u_char *s = (u_char *)sa->sa_data, *slim; 780 char *cq, *cqlim; 781 782 cq = workbuf; 783 slim = sa->sa_len + (u_char *) sa; 784 cqlim = cq + sizeof(workbuf) - 6; 785 cq += sprintf(cq, "(%d)", sa->sa_family); 786 while (s < slim && cq < cqlim) { 787 cq += sprintf(cq, " %02x", *s++); 788 if (s < slim) 789 cq += sprintf(cq, "%02x", *s++); 790 } 791 cp = workbuf; 792 } 793 } 794 795 return (cp); 796 } 797 798 static void 799 p_flags(int f, const char *format) 800 { 801 printf(format, fmt_flags(f)); 802 } 803 804 static const char * 805 fmt_flags(int f) 806 { 807 static char name[33]; 808 char *flags; 809 struct bits *p = bits; 810 811 for (flags = name; p->b_mask; p++) 812 if (p->b_mask & f) 813 *flags++ = p->b_val; 814 *flags = '\0'; 815 return (name); 816 } 817 818 static void 819 p_rtentry_kvm(struct rtentry *rt) 820 { 821 static struct ifnet ifnet, *lastif; 822 static char buffer[128]; 823 static char prettyname[128]; 824 struct sockaddr *sa; 825 sa_u addr, mask; 826 827 bzero(&addr, sizeof(addr)); 828 if ((sa = kgetsa(rt_key(rt)))) 829 bcopy(sa, &addr, sa->sa_len); 830 bzero(&mask, sizeof(mask)); 831 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt)))) 832 bcopy(sa, &mask, sa->sa_len); 833 p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, wid_dst); 834 p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, wid_gw); 835 snprintf(buffer, sizeof(buffer), "%%-%d.%ds ", wid_flags, wid_flags); 836 p_flags(rt->rt_flags, buffer); 837 if (Wflag) { 838 printf("%*ju ", wid_pksent, 839 (uintmax_t )kread_counter((u_long )rt->rt_pksent)); 840 841 if (rt->rt_mtu != 0) 842 printf("%*lu ", wid_mtu, rt->rt_mtu); 843 else 844 printf("%*s ", wid_mtu, ""); 845 } 846 if (rt->rt_ifp) { 847 if (rt->rt_ifp != lastif) { 848 if (kget(rt->rt_ifp, ifnet) == 0) 849 strlcpy(prettyname, ifnet.if_xname, 850 sizeof(prettyname)); 851 else 852 strlcpy(prettyname, "---", sizeof(prettyname)); 853 lastif = rt->rt_ifp; 854 } 855 printf("%*.*s", wid_if, wid_if, prettyname); 856 if (rt->rt_expire) { 857 time_t expire_time; 858 859 if ((expire_time = 860 rt->rt_expire - uptime.tv_sec) > 0) 861 printf(" %*d", wid_expire, (int)expire_time); 862 } 863 if (rt->rt_nodes[0].rn_dupedkey) 864 printf(" =>"); 865 } 866 putchar('\n'); 867 } 868 869 char * 870 routename(in_addr_t in) 871 { 872 char *cp; 873 static char line[MAXHOSTNAMELEN]; 874 struct hostent *hp; 875 876 cp = 0; 877 if (!numeric_addr) { 878 hp = gethostbyaddr(&in, sizeof (struct in_addr), AF_INET); 879 if (hp) { 880 cp = hp->h_name; 881 trimdomain(cp, strlen(cp)); 882 } 883 } 884 if (cp) { 885 strlcpy(line, cp, sizeof(line)); 886 } else { 887 #define C(x) ((x) & 0xff) 888 in = ntohl(in); 889 sprintf(line, "%u.%u.%u.%u", 890 C(in >> 24), C(in >> 16), C(in >> 8), C(in)); 891 } 892 return (line); 893 } 894 895 #define NSHIFT(m) ( \ 896 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \ 897 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \ 898 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \ 899 0) 900 901 static void 902 domask(char *dst, in_addr_t addr __unused, u_long mask) 903 { 904 int b, i; 905 906 if (mask == 0 || (!numeric_addr && NSHIFT(mask) != 0)) { 907 *dst = '\0'; 908 return; 909 } 910 i = 0; 911 for (b = 0; b < 32; b++) 912 if (mask & (1 << b)) { 913 int bb; 914 915 i = b; 916 for (bb = b+1; bb < 32; bb++) 917 if (!(mask & (1 << bb))) { 918 i = -1; /* noncontig */ 919 break; 920 } 921 break; 922 } 923 if (i == -1) 924 sprintf(dst, "&0x%lx", mask); 925 else 926 sprintf(dst, "/%d", 32-i); 927 } 928 929 /* 930 * Return the name of the network whose address is given. 931 */ 932 char * 933 netname(in_addr_t in, in_addr_t mask) 934 { 935 char *cp = 0; 936 static char line[MAXHOSTNAMELEN]; 937 struct netent *np = 0; 938 in_addr_t i; 939 940 /* It is ok to supply host address. */ 941 in &= mask; 942 943 i = ntohl(in); 944 if (!numeric_addr && i) { 945 np = getnetbyaddr(i >> NSHIFT(ntohl(mask)), AF_INET); 946 if (np != NULL) { 947 cp = np->n_name; 948 trimdomain(cp, strlen(cp)); 949 } 950 } 951 if (cp != NULL) { 952 strlcpy(line, cp, sizeof(line)); 953 } else { 954 inet_ntop(AF_INET, &in, line, sizeof(line) - 1); 955 } 956 domask(line + strlen(line), i, ntohl(mask)); 957 return (line); 958 } 959 960 #undef NSHIFT 961 962 #ifdef INET6 963 void 964 in6_fillscopeid(struct sockaddr_in6 *sa6) 965 { 966 #if defined(__KAME__) 967 /* 968 * XXX: This is a special workaround for KAME kernels. 969 * sin6_scope_id field of SA should be set in the future. 970 */ 971 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) || 972 IN6_IS_ADDR_MC_NODELOCAL(&sa6->sin6_addr) || 973 IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) { 974 if (sa6->sin6_scope_id == 0) 975 sa6->sin6_scope_id = 976 ntohs(*(u_int16_t *)&sa6->sin6_addr.s6_addr[2]); 977 sa6->sin6_addr.s6_addr[2] = sa6->sin6_addr.s6_addr[3] = 0; 978 } 979 #endif 980 } 981 982 const char * 983 netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask) 984 { 985 static char line[MAXHOSTNAMELEN]; 986 u_char *p = (u_char *)mask; 987 u_char *lim; 988 int masklen, illegal = 0, flag = 0; 989 990 if (mask) { 991 for (masklen = 0, lim = p + 16; p < lim; p++) { 992 switch (*p) { 993 case 0xff: 994 masklen += 8; 995 break; 996 case 0xfe: 997 masklen += 7; 998 break; 999 case 0xfc: 1000 masklen += 6; 1001 break; 1002 case 0xf8: 1003 masklen += 5; 1004 break; 1005 case 0xf0: 1006 masklen += 4; 1007 break; 1008 case 0xe0: 1009 masklen += 3; 1010 break; 1011 case 0xc0: 1012 masklen += 2; 1013 break; 1014 case 0x80: 1015 masklen += 1; 1016 break; 1017 case 0x00: 1018 break; 1019 default: 1020 illegal ++; 1021 break; 1022 } 1023 } 1024 if (illegal) 1025 fprintf(stderr, "illegal prefixlen\n"); 1026 } 1027 else 1028 masklen = 128; 1029 1030 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr)) 1031 return("default"); 1032 1033 if (numeric_addr) 1034 flag |= NI_NUMERICHOST; 1035 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line), 1036 NULL, 0, flag); 1037 1038 if (numeric_addr) 1039 sprintf(&line[strlen(line)], "/%d", masklen); 1040 1041 return line; 1042 } 1043 1044 char * 1045 routename6(struct sockaddr_in6 *sa6) 1046 { 1047 static char line[MAXHOSTNAMELEN]; 1048 int flag = 0; 1049 /* use local variable for safety */ 1050 struct sockaddr_in6 sa6_local; 1051 1052 sa6_local.sin6_family = AF_INET6; 1053 sa6_local.sin6_len = sizeof(sa6_local); 1054 sa6_local.sin6_addr = sa6->sin6_addr; 1055 sa6_local.sin6_scope_id = sa6->sin6_scope_id; 1056 1057 if (numeric_addr) 1058 flag |= NI_NUMERICHOST; 1059 1060 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len, 1061 line, sizeof(line), NULL, 0, flag); 1062 1063 return line; 1064 } 1065 #endif /*INET6*/ 1066 1067 /* 1068 * Print routing statistics 1069 */ 1070 void 1071 rt_stats(void) 1072 { 1073 struct rtstat rtstat; 1074 u_long rtsaddr, rttaddr; 1075 int rttrash; 1076 1077 kresolve_list(rl); 1078 1079 if ((rtsaddr = rl[N_RTSTAT].n_value) == 0) { 1080 printf("rtstat: symbol not in namelist\n"); 1081 return; 1082 } 1083 if ((rttaddr = rl[N_RTTRASH].n_value) == 0) { 1084 printf("rttrash: symbol not in namelist\n"); 1085 return; 1086 } 1087 kread(rtsaddr, (char *)&rtstat, sizeof (rtstat)); 1088 kread(rttaddr, (char *)&rttrash, sizeof (rttrash)); 1089 printf("routing:\n"); 1090 1091 #define p(f, m) if (rtstat.f || sflag <= 1) \ 1092 printf(m, rtstat.f, plural(rtstat.f)) 1093 1094 p(rts_badredirect, "\t%hu bad routing redirect%s\n"); 1095 p(rts_dynamic, "\t%hu dynamically created route%s\n"); 1096 p(rts_newgateway, "\t%hu new gateway%s due to redirects\n"); 1097 p(rts_unreach, "\t%hu destination%s found unreachable\n"); 1098 p(rts_wildcard, "\t%hu use%s of a wildcard route\n"); 1099 #undef p 1100 1101 if (rttrash || sflag <= 1) 1102 printf("\t%u route%s not in table but not freed\n", 1103 rttrash, plural(rttrash)); 1104 } 1105