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