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