1 /* $NetBSD: inet6.c,v 1.6 1999/12/16 00:58:17 thorpej Exp $ */ 2 3 /* BSDI inet.c,v 2.3 1995/10/24 02:19:29 prb Exp */ 4 /* 5 * Copyright (c) 1983, 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)inet.c 8.4 (Berkeley) 4/20/94"; 41 #else 42 __RCSID("$NetBSD: inet6.c,v 1.6 1999/12/16 00:58:17 thorpej Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/ioctl.h> 50 #include <sys/mbuf.h> 51 #include <sys/protosw.h> 52 53 #include <net/route.h> 54 #include <net/if.h> 55 #include <netinet/in.h> 56 #include <netinet/ip6.h> 57 #include <netinet/icmp6.h> 58 #include <netinet/in_systm.h> 59 #ifndef TCP6 60 #include <netinet/ip.h> 61 #include <netinet/ip_var.h> 62 #endif 63 #include <netinet6/in6_pcb.h> 64 #include <netinet6/in6_var.h> 65 #include <netinet6/ip6_var.h> 66 #ifdef TCP6 67 #include <netinet6/tcp6.h> 68 #include <netinet6/tcp6_seq.h> 69 #define TCP6STATES 70 #include <netinet6/tcp6_fsm.h> 71 #define TCP6TIMERS 72 #include <netinet6/tcp6_timer.h> 73 #include <netinet6/tcp6_var.h> 74 #include <netinet6/tcp6_debug.h> 75 #else 76 #include <netinet/tcp.h> 77 #include <netinet/tcpip.h> 78 #include <netinet/tcp_seq.h> 79 /*#define TCPSTATES*/ 80 #include <netinet/tcp_fsm.h> 81 extern char *tcpstates[]; 82 /*#define TCPTIMERS*/ 83 #include <netinet/tcp_timer.h> 84 #include <netinet/tcp_var.h> 85 #include <netinet/tcp_debug.h> 86 #endif /*TCP6*/ 87 #include <netinet6/udp6.h> 88 #include <netinet6/udp6_var.h> 89 #include <netinet6/pim6_var.h> 90 91 #include <arpa/inet.h> 92 #if 0 93 #include "gethostbyname2.h" 94 #endif 95 #include <netdb.h> 96 97 #include <stdio.h> 98 #include <string.h> 99 #include <unistd.h> 100 #include "netstat.h" 101 102 #ifdef INET6 103 104 struct in6pcb in6pcb; 105 #ifdef TCP6 106 struct tcp6cb tcp6cb; 107 #else 108 struct tcpcb tcpcb; 109 #endif 110 struct socket sockb; 111 112 char *inet6name __P((struct in6_addr *)); 113 void inet6print __P((struct in6_addr *, int, char *)); 114 115 /* 116 * Print a summary of connections related to an Internet 117 * protocol. For TCP, also give state of connection. 118 * Listening processes (aflag) are suppressed unless the 119 * -a (all) flag is specified. 120 */ 121 void 122 ip6protopr(off, name) 123 u_long off; 124 char *name; 125 { 126 struct in6pcb cb; 127 register struct in6pcb *prev, *next; 128 int istcp; 129 static int first = 1; 130 131 if (off == 0) 132 return; 133 istcp = strcmp(name, "tcp6") == 0; 134 kread(off, (char *)&cb, sizeof (struct in6pcb)); 135 in6pcb = cb; 136 prev = (struct in6pcb *)off; 137 if (in6pcb.in6p_next == (struct in6pcb *)off) 138 return; 139 while (in6pcb.in6p_next != (struct in6pcb *)off) { 140 next = in6pcb.in6p_next; 141 kread((u_long)next, (char *)&in6pcb, sizeof (in6pcb)); 142 if (in6pcb.in6p_prev != prev) { 143 printf("???\n"); 144 break; 145 } 146 if (!aflag && IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr)) { 147 prev = next; 148 continue; 149 } 150 kread((u_long)in6pcb.in6p_socket, (char *)&sockb, sizeof (sockb)); 151 if (istcp) { 152 #ifdef TCP6 153 kread((u_long)in6pcb.in6p_ppcb, 154 (char *)&tcp6cb, sizeof (tcp6cb)); 155 #else 156 kread((u_long)in6pcb.in6p_ppcb, 157 (char *)&tcpcb, sizeof (tcpcb)); 158 #endif 159 } 160 if (first) { 161 printf("Active Internet6 connections"); 162 if (aflag) 163 printf(" (including servers)"); 164 putchar('\n'); 165 if (Aflag) 166 printf("%-8.8s ", "PCB"); 167 printf(Aflag ? 168 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" : 169 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n", 170 "Proto", "Recv-Q", "Send-Q", 171 "Local Address", "Foreign Address", "(state)"); 172 first = 0; 173 } 174 if (Aflag) { 175 if (istcp) 176 printf("%8p ", in6pcb.in6p_ppcb); 177 else 178 printf("%8p ", next); 179 } 180 printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc, 181 sockb.so_snd.sb_cc); 182 /* xxx */ 183 inet6print(&in6pcb.in6p_laddr, (int)in6pcb.in6p_lport, name); 184 inet6print(&in6pcb.in6p_faddr, (int)in6pcb.in6p_fport, name); 185 if (istcp) { 186 #ifdef TCP6 187 if (tcp6cb.t_state < 0 || tcp6cb.t_state >= TCP6_NSTATES) 188 printf(" %d", tcp6cb.t_state); 189 else 190 printf(" %s", tcp6states[tcp6cb.t_state]); 191 #else 192 if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES) 193 printf(" %d", tcpcb.t_state); 194 else 195 printf(" %s", tcpstates[tcpcb.t_state]); 196 #endif 197 } 198 putchar('\n'); 199 prev = next; 200 } 201 } 202 203 #ifdef TCP6 204 /* 205 * Dump TCP6 statistics structure. 206 */ 207 void 208 tcp6_stats(off, name) 209 u_long off; 210 char *name; 211 { 212 struct tcp6stat tcp6stat; 213 214 if (off == 0) 215 return; 216 printf ("%s:\n", name); 217 kread(off, (char *)&tcp6stat, sizeof (tcp6stat)); 218 219 #define p(f, m) if (tcp6stat.f || sflag <= 1) \ 220 printf(m, tcp6stat.f, plural(tcp6stat.f)) 221 #define p2(f1, f2, m) if (tcp6stat.f1 || tcp6stat.f2 || sflag <= 1) \ 222 printf(m, tcp6stat.f1, plural(tcp6stat.f1), tcp6stat.f2, plural(tcp6stat.f2)) 223 #define p3(f, m) if (tcp6stat.f || sflag <= 1) \ 224 printf(m, tcp6stat.f, plurales(tcp6stat.f)) 225 226 p(tcp6s_sndtotal, "\t%ld packet%s sent\n"); 227 p2(tcp6s_sndpack,tcp6s_sndbyte, 228 "\t\t%ld data packet%s (%ld byte%s)\n"); 229 p2(tcp6s_sndrexmitpack, tcp6s_sndrexmitbyte, 230 "\t\t%ld data packet%s (%ld byte%s) retransmitted\n"); 231 p2(tcp6s_sndacks, tcp6s_delack, 232 "\t\t%ld ack-only packet%s (%ld packet%s delayed)\n"); 233 p(tcp6s_sndurg, "\t\t%ld URG only packet%s\n"); 234 p(tcp6s_sndprobe, "\t\t%ld window probe packet%s\n"); 235 p(tcp6s_sndwinup, "\t\t%ld window update packet%s\n"); 236 p(tcp6s_sndctrl, "\t\t%ld control packet%s\n"); 237 p(tcp6s_rcvtotal, "\t%ld packet%s received\n"); 238 p2(tcp6s_rcvackpack, tcp6s_rcvackbyte, "\t\t%ld ack%s (for %ld byte%s)\n"); 239 p(tcp6s_rcvdupack, "\t\t%ld duplicate ack%s\n"); 240 p(tcp6s_rcvacktoomuch, "\t\t%ld ack%s for unsent data\n"); 241 p2(tcp6s_rcvpack, tcp6s_rcvbyte, 242 "\t\t%ld packet%s (%ld byte%s) received in-sequence\n"); 243 p2(tcp6s_rcvduppack, tcp6s_rcvdupbyte, 244 "\t\t%ld completely duplicate packet%s (%ld byte%s)\n"); 245 p(tcp6s_pawsdrop, "\t\t%ld old duplicate packet%s\n"); 246 p2(tcp6s_rcvpartduppack, tcp6s_rcvpartdupbyte, 247 "\t\t%ld packet%s with some dup. data (%ld byte%s duped)\n"); 248 p2(tcp6s_rcvoopack, tcp6s_rcvoobyte, 249 "\t\t%ld out-of-order packet%s (%ld byte%s)\n"); 250 p2(tcp6s_rcvpackafterwin, tcp6s_rcvbyteafterwin, 251 "\t\t%ld packet%s (%ld byte%s) of data after window\n"); 252 p(tcp6s_rcvwinprobe, "\t\t%ld window probe%s\n"); 253 p(tcp6s_rcvwinupd, "\t\t%ld window update packet%s\n"); 254 p(tcp6s_rcvafterclose, "\t\t%ld packet%s received after close\n"); 255 p(tcp6s_rcvbadsum, "\t\t%ld discarded for bad checksum%s\n"); 256 p(tcp6s_rcvbadoff, "\t\t%ld discarded for bad header offset field%s\n"); 257 p(tcp6s_rcvshort, "\t\t%ld discarded because packet%s too short\n"); 258 p(tcp6s_connattempt, "\t%ld connection request%s\n"); 259 p(tcp6s_accepts, "\t%ld connection accept%s\n"); 260 p(tcp6s_badsyn, "\t%ld bad connection attempt%s\n"); 261 p(tcp6s_connects, "\t%ld connection%s established (including accepts)\n"); 262 p2(tcp6s_closed, tcp6s_drops, 263 "\t%ld connection%s closed (including %ld drop%s)\n"); 264 p(tcp6s_conndrops, "\t%ld embryonic connection%s dropped\n"); 265 p2(tcp6s_rttupdated, tcp6s_segstimed, 266 "\t%ld segment%s updated rtt (of %ld attempt%s)\n"); 267 p(tcp6s_rexmttimeo, "\t%ld retransmit timeout%s\n"); 268 p(tcp6s_timeoutdrop, "\t\t%ld connection%s dropped by rexmit timeout\n"); 269 p(tcp6s_persisttimeo, "\t%ld persist timeout%s\n"); 270 p(tcp6s_persistdrop, "\t%ld connection%s timed out in persist\n"); 271 p(tcp6s_keeptimeo, "\t%ld keepalive timeout%s\n"); 272 p(tcp6s_keepprobe, "\t\t%ld keepalive probe%s sent\n"); 273 p(tcp6s_keepdrops, "\t\t%ld connection%s dropped by keepalive\n"); 274 p(tcp6s_predack, "\t%ld correct ACK header prediction%s\n"); 275 p(tcp6s_preddat, "\t%ld correct data packet header prediction%s\n"); 276 p3(tcp6s_pcbcachemiss, "\t%ld PCB cache miss%s\n"); 277 #undef p 278 #undef p2 279 #undef p3 280 } 281 #endif 282 283 /* 284 * Dump UDP6 statistics structure. 285 */ 286 void 287 udp6_stats(off, name) 288 u_long off; 289 char *name; 290 { 291 struct udp6stat udp6stat; 292 u_quad_t delivered; 293 294 if (off == 0) 295 return; 296 kread(off, (char *)&udp6stat, sizeof (udp6stat)); 297 printf("%s:\n", name); 298 #define p(f, m) if (udp6stat.f || sflag <= 1) \ 299 printf(m, (unsigned long long)udp6stat.f, plural(udp6stat.f)) 300 #define p1(f, m) if (udp6stat.f || sflag <= 1) \ 301 printf(m, (unsigned long long)udp6stat.f) 302 p(udp6s_ipackets, "\t%llu datagram%s received\n"); 303 p1(udp6s_hdrops, "\t%llu with incomplete header\n"); 304 p1(udp6s_badlen, "\t%llu with bad data length field\n"); 305 p1(udp6s_badsum, "\t%llu with bad checksum\n"); 306 p1(udp6s_nosum, "\t%llu with no checksum\n"); 307 p1(udp6s_noport, "\t%llu dropped due to no socket\n"); 308 p(udp6s_noportmcast, "\t%llu multicast datagram%s dropped due to no socket\n"); 309 p1(udp6s_fullsock, "\t%llu dropped due to full socket buffers\n"); 310 delivered = udp6stat.udp6s_ipackets - 311 udp6stat.udp6s_hdrops - 312 udp6stat.udp6s_badlen - 313 udp6stat.udp6s_badsum - 314 udp6stat.udp6s_noport - 315 udp6stat.udp6s_noportmcast - 316 udp6stat.udp6s_fullsock; 317 if (delivered || sflag <= 1) 318 printf("\t%llu delivered\n", (unsigned long long)delivered); 319 p(udp6s_opackets, "\t%llu datagram%s output\n"); 320 #undef p 321 #undef p1 322 } 323 324 static char *ip6nh[] = { 325 "hop by hop", 326 "ICMP", 327 "IGMP", 328 "#3", 329 "IP", 330 "#5", 331 "TCP", 332 "#7", 333 "#8", 334 "#9", 335 "#10", 336 "#11", 337 "#12", 338 "#13", 339 "#14", 340 "#15", 341 "#16", 342 "UDP", 343 "#18", 344 "#19", 345 "#20", 346 "#21", 347 "IDP", 348 "#23", 349 "#24", 350 "#25", 351 "#26", 352 "#27", 353 "#28", 354 "TP", 355 "#30", 356 "#31", 357 "#32", 358 "#33", 359 "#34", 360 "#35", 361 "#36", 362 "#37", 363 "#38", 364 "#39", 365 "#40", 366 "IP6", 367 "#42", 368 "routing", 369 "fragment", 370 "#45", 371 "#46", 372 "#47", 373 "#48", 374 "#49", 375 "ESP", 376 "AH", 377 "#52", 378 "#53", 379 "#54", 380 "#55", 381 "#56", 382 "#57", 383 "ICMP6", 384 "no next header", 385 "destination option", 386 "#61", 387 "#62", 388 "#63", 389 "#64", 390 "#65", 391 "#66", 392 "#67", 393 "#68", 394 "#69", 395 "#70", 396 "#71", 397 "#72", 398 "#73", 399 "#74", 400 "#75", 401 "#76", 402 "#77", 403 "#78", 404 "#79", 405 "ISOIP", 406 "#81", 407 "#82", 408 "#83", 409 "#84", 410 "#85", 411 "#86", 412 "#87", 413 "#88", 414 "#89", 415 "#80", 416 "#91", 417 "#92", 418 "#93", 419 "#94", 420 "#95", 421 "#96", 422 "Ethernet", 423 "#98", 424 "#99", 425 "#100", 426 "#101", 427 "#102", 428 "PIM", 429 "#104", 430 "#105", 431 "#106", 432 "#107", 433 "#108", 434 "#109", 435 "#110", 436 "#111", 437 "#112", 438 "#113", 439 "#114", 440 "#115", 441 "#116", 442 "#117", 443 "#118", 444 "#119", 445 "#120", 446 "#121", 447 "#122", 448 "#123", 449 "#124", 450 "#125", 451 "#126", 452 "#127", 453 "#128", 454 "#129", 455 "#130", 456 "#131", 457 "#132", 458 "#133", 459 "#134", 460 "#135", 461 "#136", 462 "#137", 463 "#138", 464 "#139", 465 "#140", 466 "#141", 467 "#142", 468 "#143", 469 "#144", 470 "#145", 471 "#146", 472 "#147", 473 "#148", 474 "#149", 475 "#150", 476 "#151", 477 "#152", 478 "#153", 479 "#154", 480 "#155", 481 "#156", 482 "#157", 483 "#158", 484 "#159", 485 "#160", 486 "#161", 487 "#162", 488 "#163", 489 "#164", 490 "#165", 491 "#166", 492 "#167", 493 "#168", 494 "#169", 495 "#170", 496 "#171", 497 "#172", 498 "#173", 499 "#174", 500 "#175", 501 "#176", 502 "#177", 503 "#178", 504 "#179", 505 "#180", 506 "#181", 507 "#182", 508 "#183", 509 "#184", 510 "#185", 511 "#186", 512 "#187", 513 "#188", 514 "#189", 515 "#180", 516 "#191", 517 "#192", 518 "#193", 519 "#194", 520 "#195", 521 "#196", 522 "#197", 523 "#198", 524 "#199", 525 "#200", 526 "#201", 527 "#202", 528 "#203", 529 "#204", 530 "#205", 531 "#206", 532 "#207", 533 "#208", 534 "#209", 535 "#210", 536 "#211", 537 "#212", 538 "#213", 539 "#214", 540 "#215", 541 "#216", 542 "#217", 543 "#218", 544 "#219", 545 "#220", 546 "#221", 547 "#222", 548 "#223", 549 "#224", 550 "#225", 551 "#226", 552 "#227", 553 "#228", 554 "#229", 555 "#230", 556 "#231", 557 "#232", 558 "#233", 559 "#234", 560 "#235", 561 "#236", 562 "#237", 563 "#238", 564 "#239", 565 "#240", 566 "#241", 567 "#242", 568 "#243", 569 "#244", 570 "#245", 571 "#246", 572 "#247", 573 "#248", 574 "#249", 575 "#250", 576 "#251", 577 "#252", 578 "#253", 579 "#254", 580 "#255", 581 }; 582 583 /* 584 * Dump IP6 statistics structure. 585 */ 586 void 587 ip6_stats(off, name) 588 u_long off; 589 char *name; 590 { 591 struct ip6stat ip6stat; 592 int first, i; 593 594 if (off == 0) 595 return; 596 597 kread(off, (char *)&ip6stat, sizeof (ip6stat)); 598 printf("%s:\n", name); 599 600 #define p(f, m) if (ip6stat.f || sflag <= 1) \ 601 printf(m, (unsigned long long)ip6stat.f, plural(ip6stat.f)) 602 #define p1(f, m) if (ip6stat.f || sflag <= 1) \ 603 printf(m, (unsigned long long)ip6stat.f) 604 605 p(ip6s_total, "\t%llu total packet%s received\n"); 606 p1(ip6s_toosmall, "\t%llu with size smaller than minimum\n"); 607 p1(ip6s_tooshort, "\t%llu with data size < data length\n"); 608 p1(ip6s_badoptions, "\t%llu with bad options\n"); 609 p1(ip6s_badvers, "\t%llu with incorrect version number\n"); 610 p(ip6s_fragments, "\t%llu fragment%s received\n"); 611 p(ip6s_fragdropped, 612 "\t%llu fragment%s dropped (dup or out of space)\n"); 613 p(ip6s_fragtimeout, "\t%llu fragment%s dropped after timeout\n"); 614 p(ip6s_fragoverflow, "\t%llu fragment%s that exceeded limit\n"); 615 p(ip6s_reassembled, "\t%llu packet%s reassembled ok\n"); 616 p(ip6s_delivered, "\t%llu packet%s for this host\n"); 617 p(ip6s_forward, "\t%llu packet%s forwarded\n"); 618 p(ip6s_cantforward, "\t%llu packet%s not forwardable\n"); 619 p(ip6s_redirectsent, "\t%llu redirect%s sent\n"); 620 p(ip6s_localout, "\t%llu packet%s sent from this host\n"); 621 p(ip6s_rawout, "\t%llu packet%s sent with fabricated ip header\n"); 622 p(ip6s_odropped, 623 "\t%llu output packet%s dropped due to no bufs, etc.\n"); 624 p(ip6s_noroute, "\t%llu output packet%s discarded due to no route\n"); 625 p(ip6s_fragmented, "\t%llu output datagram%s fragmented\n"); 626 p(ip6s_ofragments, "\t%llu fragment%s created\n"); 627 p(ip6s_cantfrag, "\t%llu datagram%s that can't be fragmented\n"); 628 p(ip6s_badscope, "\t%llu packet%s that violated scope rules\n"); 629 p(ip6s_notmember, "\t%llu multicast packet%s which we don't join\n"); 630 for (first = 1, i = 0; i < 256; i++) 631 if (ip6stat.ip6s_nxthist[i] != 0) { 632 if (first) { 633 printf("\tInput histogram:\n"); 634 first = 0; 635 } 636 printf("\t\t%s: %llu\n", ip6nh[i], 637 (unsigned long long)ip6stat.ip6s_nxthist[i]); 638 } 639 printf("\tMbuf statics:\n"); 640 printf("\t\t%llu one mbuf\n", (unsigned long long)ip6stat.ip6s_m1); 641 for (first = 1, i = 0; i < 32; i++) { 642 char ifbuf[IFNAMSIZ]; 643 if (ip6stat.ip6s_m2m[i] != 0) { 644 if (first) { 645 printf("\t\ttwo or more mbuf:\n"); 646 first = 0; 647 } 648 printf("\t\t\t%s = %llu\n", 649 if_indextoname(i, ifbuf), 650 (unsigned long long)ip6stat.ip6s_m2m[i]); 651 } 652 } 653 printf("\t\t%llu one ext mbuf\n", 654 (unsigned long long)ip6stat.ip6s_mext1); 655 printf("\t\t%llu two or more ext mbuf\n", 656 (unsigned long long)ip6stat.ip6s_mext2m); 657 p(ip6s_exthdrtoolong, 658 "\t%llu packet%s whose headers are not continuous\n"); 659 p(ip6s_nogif, "\t%llu tunneling packet%s that can't find gif\n"); 660 p(ip6s_toomanyhdr, "\t%qu packet%s discarded due to too many headers\n"); 661 p(ip6s_pulldown, "\t%qu call%s to m_pulldown\n"); 662 p(ip6s_pulldown_alloc, "\t%qu mbuf allocation%s in m_pulldown\n"); 663 if (ip6stat.ip6s_pulldown_copy != 1) { 664 p1(ip6s_pulldown_copy, "\t%qu mbuf copies in m_pulldown\n"); 665 } else { 666 p1(ip6s_pulldown_copy, "\t%qu mbuf copy in m_pulldown\n"); 667 } 668 #undef p 669 #undef p1 670 } 671 672 /* 673 * Dump IPv6 per-interface statistics based on RFC 2465. 674 */ 675 void 676 ip6_ifstats(ifname) 677 char *ifname; 678 { 679 struct in6_ifreq ifr; 680 int s; 681 #define p(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \ 682 printf(m, (unsigned long long)ifr.ifr_ifru.ifru_stat.f, \ 683 plural(ifr.ifr_ifru.ifru_stat.f)) 684 #define p_5(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \ 685 printf(m, ip6stat.f) 686 687 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 688 perror("Warning: socket(AF_INET6)"); 689 return; 690 } 691 692 strcpy(ifr.ifr_name, ifname); 693 printf("ip6 on %s:\n", ifr.ifr_name); 694 695 if (ioctl(s, SIOCGIFSTAT_IN6, (char *)&ifr) < 0) { 696 perror("Warning: ioctl(SIOCGIFSTAT_IN6)"); 697 goto end; 698 } 699 700 p(ifs6_in_receive, "\t%llu total input datagram%s\n"); 701 p(ifs6_in_hdrerr, "\t%llu datagram%s with invalid header received\n"); 702 p(ifs6_in_toobig, "\t%llu datagram%s exceeded MTU received\n"); 703 p(ifs6_in_noroute, "\t%llu datagram%s with no route received\n"); 704 p(ifs6_in_addrerr, "\t%llu datagram%s with invalid dst received\n"); 705 p(ifs6_in_truncated, "\t%llu truncated datagram%s received\n"); 706 p(ifs6_in_protounknown, "\t%llu datagram%s with unknown proto received\n"); 707 p(ifs6_in_discard, "\t%llu input datagram%s discarded\n"); 708 p(ifs6_in_deliver, 709 "\t%llu datagram%s delivered to an upper layer protocol\n"); 710 p(ifs6_out_forward, "\t%llu datagram%s forwarded to this interface\n"); 711 p(ifs6_out_request, 712 "\t%llu datagram%s sent from an upper layer protocol\n"); 713 p(ifs6_out_discard, "\t%llu total discarded output datagram%s\n"); 714 p(ifs6_out_fragok, "\t%llu output datagram%s fragmented\n"); 715 p(ifs6_out_fragfail, "\t%llu output datagram%s failed on fragment\n"); 716 p(ifs6_out_fragcreat, "\t%llu output datagram%s succeeded on fragment\n"); 717 p(ifs6_reass_reqd, "\t%llu incoming datagram%s fragmented\n"); 718 p(ifs6_reass_ok, "\t%llu datagram%s reassembled\n"); 719 p(ifs6_reass_fail, "\t%llu datagram%s failed on reassembling\n"); 720 p(ifs6_in_mcast, "\t%llu multicast datagram%s received\n"); 721 p(ifs6_out_mcast, "\t%llu multicast datagram%s sent\n"); 722 723 end: 724 close(s); 725 726 #undef p 727 #undef p_5 728 } 729 730 static char *icmp6names[] = { 731 "#0", 732 "unreach", 733 "packet too big", 734 "time exceed", 735 "parameter problem", 736 "#5", 737 "#6", 738 "#7", 739 "#8", 740 "#9", 741 "#10", 742 "#11", 743 "#12", 744 "#13", 745 "#14", 746 "#15", 747 "#16", 748 "#17", 749 "#18", 750 "#19", 751 "#20", 752 "#21", 753 "#22", 754 "#23", 755 "#24", 756 "#25", 757 "#26", 758 "#27", 759 "#28", 760 "#29", 761 "#30", 762 "#31", 763 "#32", 764 "#33", 765 "#34", 766 "#35", 767 "#36", 768 "#37", 769 "#38", 770 "#39", 771 "#40", 772 "#41", 773 "#42", 774 "#43", 775 "#44", 776 "#45", 777 "#46", 778 "#47", 779 "#48", 780 "#49", 781 "#50", 782 "#51", 783 "#52", 784 "#53", 785 "#54", 786 "#55", 787 "#56", 788 "#57", 789 "#58", 790 "#59", 791 "#60", 792 "#61", 793 "#62", 794 "#63", 795 "#64", 796 "#65", 797 "#66", 798 "#67", 799 "#68", 800 "#69", 801 "#70", 802 "#71", 803 "#72", 804 "#73", 805 "#74", 806 "#75", 807 "#76", 808 "#77", 809 "#78", 810 "#79", 811 "#80", 812 "#81", 813 "#82", 814 "#83", 815 "#84", 816 "#85", 817 "#86", 818 "#87", 819 "#88", 820 "#89", 821 "#80", 822 "#91", 823 "#92", 824 "#93", 825 "#94", 826 "#95", 827 "#96", 828 "#97", 829 "#98", 830 "#99", 831 "#100", 832 "#101", 833 "#102", 834 "#103", 835 "#104", 836 "#105", 837 "#106", 838 "#107", 839 "#108", 840 "#109", 841 "#110", 842 "#111", 843 "#112", 844 "#113", 845 "#114", 846 "#115", 847 "#116", 848 "#117", 849 "#118", 850 "#119", 851 "#120", 852 "#121", 853 "#122", 854 "#123", 855 "#124", 856 "#125", 857 "#126", 858 "#127", 859 "echo", 860 "echo reply", 861 "multicast listener query", 862 "multicast listener report", 863 "multicast listener done", 864 "router solicitation", 865 "router advertisment", 866 "neighbor solicitation", 867 "neighbor advertisment", 868 "redirect", 869 "router renumbering", 870 "node information request", 871 "node information reply", 872 "#141", 873 "#142", 874 "#143", 875 "#144", 876 "#145", 877 "#146", 878 "#147", 879 "#148", 880 "#149", 881 "#150", 882 "#151", 883 "#152", 884 "#153", 885 "#154", 886 "#155", 887 "#156", 888 "#157", 889 "#158", 890 "#159", 891 "#160", 892 "#161", 893 "#162", 894 "#163", 895 "#164", 896 "#165", 897 "#166", 898 "#167", 899 "#168", 900 "#169", 901 "#170", 902 "#171", 903 "#172", 904 "#173", 905 "#174", 906 "#175", 907 "#176", 908 "#177", 909 "#178", 910 "#179", 911 "#180", 912 "#181", 913 "#182", 914 "#183", 915 "#184", 916 "#185", 917 "#186", 918 "#187", 919 "#188", 920 "#189", 921 "#180", 922 "#191", 923 "#192", 924 "#193", 925 "#194", 926 "#195", 927 "#196", 928 "#197", 929 "#198", 930 "#199", 931 "#200", 932 "#201", 933 "#202", 934 "#203", 935 "#204", 936 "#205", 937 "#206", 938 "#207", 939 "#208", 940 "#209", 941 "#210", 942 "#211", 943 "#212", 944 "#213", 945 "#214", 946 "#215", 947 "#216", 948 "#217", 949 "#218", 950 "#219", 951 "#220", 952 "#221", 953 "#222", 954 "#223", 955 "#224", 956 "#225", 957 "#226", 958 "#227", 959 "#228", 960 "#229", 961 "#230", 962 "#231", 963 "#232", 964 "#233", 965 "#234", 966 "#235", 967 "#236", 968 "#237", 969 "#238", 970 "#239", 971 "#240", 972 "#241", 973 "#242", 974 "#243", 975 "#244", 976 "#245", 977 "#246", 978 "#247", 979 "#248", 980 "#249", 981 "#250", 982 "#251", 983 "#252", 984 "#253", 985 "#254", 986 "#255", 987 }; 988 989 /* 990 * Dump ICMPv6 statistics. 991 */ 992 void 993 icmp6_stats(off, name) 994 u_long off; 995 char *name; 996 { 997 struct icmp6stat icmp6stat; 998 register int i, first; 999 1000 if (off == 0) 1001 return; 1002 kread(off, (char *)&icmp6stat, sizeof (icmp6stat)); 1003 printf("%s:\n", name); 1004 1005 #define p(f, m) if (icmp6stat.f || sflag <= 1) \ 1006 printf(m, (unsigned long long)icmp6stat.f, plural(icmp6stat.f)) 1007 1008 p(icp6s_error, "\t%llu call%s to icmp_error\n"); 1009 p(icp6s_canterror, "\t%llu error%s not generated because old message " 1010 "was icmp or so\n"); 1011 for (first = 1, i = 0; i < 256; i++) 1012 if (icmp6stat.icp6s_outhist[i] != 0) { 1013 if (first) { 1014 printf("\tOutput histogram:\n"); 1015 first = 0; 1016 } 1017 printf("\t\t%s: %llu\n", icmp6names[i], 1018 (unsigned long long)icmp6stat.icp6s_outhist[i]); 1019 } 1020 p(icp6s_badcode, "\t%llu message%s with bad code fields\n"); 1021 p(icp6s_tooshort, "\t%llu message%s < minimum length\n"); 1022 p(icp6s_checksum, "\t%llu bad checksum%s\n"); 1023 p(icp6s_badlen, "\t%llu message%s with bad length\n"); 1024 for (first = 1, i = 0; i < ICMP6_MAXTYPE; i++) 1025 if (icmp6stat.icp6s_inhist[i] != 0) { 1026 if (first) { 1027 printf("\tInput histogram:\n"); 1028 first = 0; 1029 } 1030 printf("\t\t%s: %llu\n", icmp6names[i], 1031 (unsigned long long)icmp6stat.icp6s_inhist[i]); 1032 } 1033 p(icp6s_reflect, "\t%llu message response%s generated\n"); 1034 p(icp6s_nd_toomanyopt, "\t%llu message%s with too many ND options\n"); 1035 #undef p 1036 } 1037 1038 /* 1039 * Dump ICMPv6 per-interface statistics based on RFC 2466. 1040 */ 1041 void 1042 icmp6_ifstats(ifname) 1043 char *ifname; 1044 { 1045 struct in6_ifreq ifr; 1046 int s; 1047 #define p(f, m) if (ifr.ifr_ifru.ifru_icmp6stat.f || sflag <= 1) \ 1048 printf(m, (unsigned long long)ifr.ifr_ifru.ifru_icmp6stat.f, \ 1049 plural(ifr.ifr_ifru.ifru_icmp6stat.f)) 1050 1051 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1052 perror("Warning: socket(AF_INET6)"); 1053 return; 1054 } 1055 1056 strcpy(ifr.ifr_name, ifname); 1057 printf("icmp6 on %s:\n", ifr.ifr_name); 1058 1059 if (ioctl(s, SIOCGIFSTAT_ICMP6, (char *)&ifr) < 0) { 1060 perror("Warning: ioctl(SIOCGIFSTAT_ICMP6)"); 1061 goto end; 1062 } 1063 1064 p(ifs6_in_msg, "\t%llu total input message%s\n"); 1065 p(ifs6_in_error, "\t%llu total input error message%s\n"); 1066 p(ifs6_in_dstunreach, "\t%llu input destination unreachable error%s\n"); 1067 p(ifs6_in_adminprohib, "\t%llu input administratively prohibited error%s\n"); 1068 p(ifs6_in_timeexceed, "\t%llu input time exceeded error%s\n"); 1069 p(ifs6_in_paramprob, "\t%llu input parameter problem error%s\n"); 1070 p(ifs6_in_pkttoobig, "\t%llu input packet too big error%s\n"); 1071 p(ifs6_in_echo, "\t%llu input echo request%s\n"); 1072 p(ifs6_in_echoreply, "\t%llu input echo reply%s\n"); 1073 p(ifs6_in_routersolicit, "\t%llu input router solicitation%s\n"); 1074 p(ifs6_in_routeradvert, "\t%llu input router advertisement%s\n"); 1075 p(ifs6_in_neighborsolicit, "\t%llu input neighbor solicitation%s\n"); 1076 p(ifs6_in_neighboradvert, "\t%llu input neighbor advertisement%s\n"); 1077 p(ifs6_in_redirect, "\t%llu input redirect%s\n"); 1078 p(ifs6_in_mldquery, "\t%llu input MLD query%s\n"); 1079 p(ifs6_in_mldreport, "\t%llu input MLD report%s\n"); 1080 p(ifs6_in_mlddone, "\t%llu input MLD done%s\n"); 1081 1082 p(ifs6_out_msg, "\t%llu total output message%s\n"); 1083 p(ifs6_out_error, "\t%llu total output error message%s\n"); 1084 p(ifs6_out_dstunreach, "\t%llu output destination unreachable error%s\n"); 1085 p(ifs6_out_adminprohib, "\t%llu output administratively prohibited error%s\n"); 1086 p(ifs6_out_timeexceed, "\t%llu output time exceeded error%s\n"); 1087 p(ifs6_out_paramprob, "\t%llu output parameter problem error%s\n"); 1088 p(ifs6_out_pkttoobig, "\t%llu output packet too big error%s\n"); 1089 p(ifs6_out_echo, "\t%llu output echo request%s\n"); 1090 p(ifs6_out_echoreply, "\t%llu output echo reply%s\n"); 1091 p(ifs6_out_routersolicit, "\t%llu output router solicitation%s\n"); 1092 p(ifs6_out_routeradvert, "\t%llu output router advertisement%s\n"); 1093 p(ifs6_out_neighborsolicit, "\t%llu output neighbor solicitation%s\n"); 1094 p(ifs6_out_neighboradvert, "\t%llu output neighbor advertisement%s\n"); 1095 p(ifs6_out_redirect, "\t%llu output redirect%s\n"); 1096 p(ifs6_out_mldquery, "\t%llu output MLD query%s\n"); 1097 p(ifs6_out_mldreport, "\t%llu output MLD report%s\n"); 1098 p(ifs6_out_mlddone, "\t%llu output MLD done%s\n"); 1099 1100 end: 1101 close(s); 1102 #undef p 1103 } 1104 1105 /* 1106 * Dump PIM statistics structure. 1107 */ 1108 void 1109 pim6_stats(off, name) 1110 u_long off; 1111 char *name; 1112 { 1113 struct pim6stat pim6stat; 1114 1115 if (off == 0) 1116 return; 1117 kread(off, (char *)&pim6stat, sizeof(pim6stat)); 1118 printf("%s:\n", name); 1119 1120 #define p(f, m) if (pim6stat.f || sflag <= 1) \ 1121 printf(m, (unsigned long long)pim6stat.f, plural(pim6stat.f)) 1122 p(pim6s_rcv_total, "\t%llu message%s received\n"); 1123 p(pim6s_rcv_tooshort, "\t%llu message%s received with too few bytes\n"); 1124 p(pim6s_rcv_badsum, "\t%llu message%s received with bad checksum\n"); 1125 p(pim6s_rcv_badversion, "\t%llu message%s received with bad version\n"); 1126 p(pim6s_rcv_registers, "\t%llu register%s received\n"); 1127 p(pim6s_rcv_badregisters, "\t%llu bad register%s received\n"); 1128 p(pim6s_snd_registers, "\t%llu register%s sent\n"); 1129 #undef p 1130 } 1131 1132 /* 1133 * Pretty print an Internet address (net address + port). 1134 * If the nflag was specified, use numbers instead of names. 1135 */ 1136 1137 void 1138 inet6print(in6, port, proto) 1139 register struct in6_addr *in6; 1140 int port; 1141 char *proto; 1142 { 1143 #define GETSERVBYPORT6(port, proto, ret)\ 1144 do {\ 1145 if (strcmp((proto), "tcp6") == 0)\ 1146 (ret) = getservbyport((int)(port), "tcp");\ 1147 else if (strcmp((proto), "udp6") == 0)\ 1148 (ret) = getservbyport((int)(port), "udp");\ 1149 else\ 1150 (ret) = getservbyport((int)(port), (proto));\ 1151 } while (0) 1152 struct servent *sp = 0; 1153 char line[80], *cp; 1154 int width; 1155 1156 width = Aflag ? 12 : 16; 1157 if (vflag && width < strlen(inet6name(in6))) 1158 width = strlen(inet6name(in6)); 1159 sprintf(line, "%.*s.", width, inet6name(in6)); 1160 cp = index(line, '\0'); 1161 if (!nflag && port) 1162 GETSERVBYPORT6(port, proto, sp); 1163 if (sp || port == 0) 1164 sprintf(cp, "%.8s", sp ? sp->s_name : "*"); 1165 else 1166 sprintf(cp, "%d", ntohs((u_short)port)); 1167 width = Aflag ? 18 : 22; 1168 if (vflag && width < strlen(line)) 1169 width = strlen(line); 1170 printf(" %-*.*s", width, width, line); 1171 } 1172 1173 /* 1174 * Construct an Internet address representation. 1175 * If the nflag has been supplied, give 1176 * numeric value, otherwise try for symbolic name. 1177 */ 1178 1179 char * 1180 inet6name(in6p) 1181 struct in6_addr *in6p; 1182 { 1183 register char *cp; 1184 static char line[50]; 1185 struct hostent *hp; 1186 static char domain[MAXHOSTNAMELEN + 1]; 1187 static int first = 1; 1188 char hbuf[NI_MAXHOST]; 1189 struct sockaddr_in6 sin6; 1190 #ifdef KAME_SCOPEID 1191 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID; 1192 #else 1193 const int niflag = NI_NUMERICHOST; 1194 #endif 1195 1196 if (first && !nflag) { 1197 first = 0; 1198 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 1199 (cp = index(domain, '.'))) 1200 (void) strcpy(domain, cp + 1); 1201 else 1202 domain[0] = 0; 1203 } 1204 cp = 0; 1205 if (!nflag && !IN6_IS_ADDR_UNSPECIFIED(in6p)) { 1206 hp = gethostbyaddr((char *)in6p, sizeof(*in6p), AF_INET6); 1207 if (hp) { 1208 if ((cp = index(hp->h_name, '.')) && 1209 !strcmp(cp + 1, domain)) 1210 *cp = 0; 1211 cp = hp->h_name; 1212 } 1213 } 1214 if (IN6_IS_ADDR_UNSPECIFIED(in6p)) 1215 strcpy(line, "*"); 1216 else if (cp) 1217 strcpy(line, cp); 1218 else { 1219 memset(&sin6, 0, sizeof(sin6)); 1220 sin6.sin6_len = sizeof(sin6); 1221 sin6.sin6_family = AF_INET6; 1222 sin6.sin6_addr = *in6p; 1223 #ifdef KAME_SCOPEID 1224 if (IN6_IS_ADDR_LINKLOCAL(in6p)) { 1225 sin6.sin6_scope_id = 1226 ntohs(*(u_int16_t *)&in6p->s6_addr[2]); 1227 sin6.sin6_addr.s6_addr[2] = 0; 1228 sin6.sin6_addr.s6_addr[3] = 0; 1229 } 1230 #endif 1231 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 1232 hbuf, sizeof(hbuf), NULL, 0, niflag)) 1233 strcpy(hbuf, "?"); 1234 sprintf(line, "%s", hbuf); 1235 } 1236 return (line); 1237 } 1238 1239 #ifdef TCP6 1240 /* 1241 * Dump the contents of a TCP6 PCB. 1242 */ 1243 void 1244 tcp6_dump(pcbaddr) 1245 u_long pcbaddr; 1246 { 1247 struct tcp6cb tcp6cb; 1248 int i; 1249 1250 kread(pcbaddr, (char *)&tcp6cb, sizeof(tcp6cb)); 1251 1252 printf("TCP Protocol Control Block at 0x%08lx:\n\n", pcbaddr); 1253 1254 printf("Timers:\n"); 1255 for (i = 0; i < TCP6T_NTIMERS; i++) 1256 printf("\t%s: %u", tcp6timers[i], tcp6cb.t_timer[i]); 1257 printf("\n\n"); 1258 1259 if (tcp6cb.t_state < 0 || tcp6cb.t_state >= TCP6_NSTATES) 1260 printf("State: %d", tcp6cb.t_state); 1261 else 1262 printf("State: %s", tcp6states[tcp6cb.t_state]); 1263 printf(", flags 0x%x, in6pcb 0x%lx\n\n", tcp6cb.t_flags, 1264 (u_long)tcp6cb.t_in6pcb); 1265 1266 printf("rxtshift %d, rxtcur %d, dupacks %d\n", tcp6cb.t_rxtshift, 1267 tcp6cb.t_rxtcur, tcp6cb.t_dupacks); 1268 printf("peermaxseg %u, maxseg %u, force %d\n\n", tcp6cb.t_peermaxseg, 1269 tcp6cb.t_maxseg, tcp6cb.t_force); 1270 1271 printf("snd_una %u, snd_nxt %u, snd_up %u\n", 1272 tcp6cb.snd_una, tcp6cb.snd_nxt, tcp6cb.snd_up); 1273 printf("snd_wl1 %u, snd_wl2 %u, iss %u, snd_wnd %llu\n\n", 1274 tcp6cb.snd_wl1, tcp6cb.snd_wl2, tcp6cb.iss, 1275 (unsigned long long)tcp6cb.snd_wnd); 1276 1277 printf("rcv_wnd %llu, rcv_nxt %u, rcv_up %u, irs %u\n\n", 1278 (unsigned long long)cp6cb.rcv_wnd, tcp6cb.rcv_nxt, 1279 tcp6cb.rcv_up, tcp6cb.irs); 1280 1281 printf("rcv_adv %u, snd_max %u, snd_cwnd %llu, snd_ssthresh %llu\n", 1282 tcp6cb.rcv_adv, tcp6cb.snd_max, (unsigned long long)tcp6cb.snd_cwnd, 1283 (unsigned long long)tcp6cb.snd_ssthresh); 1284 1285 printf("idle %d, rtt %d, rtseq %u, srtt %d, rttvar %d, rttmin %d, " 1286 "max_sndwnd %llu\n\n", tcp6cb.t_idle, tcp6cb.t_rtt, tcp6cb.t_rtseq, 1287 tcp6cb.t_srtt, tcp6cb.t_rttvar, tcp6cb.t_rttmin, 1288 (unsigned long long)tcp6cb.max_sndwnd); 1289 1290 printf("oobflags %d, iobc %d, softerror %d\n\n", tcp6cb.t_oobflags, 1291 tcp6cb.t_iobc, tcp6cb.t_softerror); 1292 1293 printf("snd_scale %d, rcv_scale %d, req_r_scale %d, req_s_scale %d\n", 1294 tcp6cb.snd_scale, tcp6cb.rcv_scale, tcp6cb.request_r_scale, 1295 tcp6cb.requested_s_scale); 1296 printf("ts_recent %u, ts_regent_age %d, last_ack_sent %u\n", 1297 tcp6cb.ts_recent, tcp6cb.ts_recent_age, tcp6cb.last_ack_sent); 1298 } 1299 #endif 1300 1301 #endif /*INET6*/ 1302