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