Home | History | Annotate | Line # | Download | only in netstat
      1 /*	$NetBSD: inet.c,v 1.119 2022/10/28 05:27:17 ozaki-r Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "from: @(#)inet.c	8.4 (Berkeley) 4/20/94";
     36 #else
     37 __RCSID("$NetBSD: inet.c,v 1.119 2022/10/28 05:27:17 ozaki-r Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #define	_CALLOUT_PRIVATE	/* for defs in sys/callout.h */
     42 
     43 #include <sys/param.h>
     44 #include <sys/queue.h>
     45 #include <sys/socket.h>
     46 #include <sys/socketvar.h>
     47 #include <sys/mbuf.h>
     48 #include <sys/protosw.h>
     49 #include <sys/sysctl.h>
     50 
     51 #include <net/if_arp.h>
     52 #include <net/route.h>
     53 #include <netinet/in.h>
     54 #include <netinet/in_systm.h>
     55 #include <netinet/ip.h>
     56 #include <netinet/in_pcb.h>
     57 #define ICMP_STRINGS
     58 #include <netinet/ip_icmp.h>
     59 
     60 #ifdef INET6
     61 #include <netinet/ip6.h>
     62 #endif
     63 
     64 #include <netinet/icmp_var.h>
     65 #include <netinet/igmp_var.h>
     66 #include <netinet/ip_var.h>
     67 #include <netinet/pim_var.h>
     68 #include <netinet/tcp.h>
     69 #include <netinet/tcp_seq.h>
     70 #define TCPSTATES
     71 #include <netinet/tcp_fsm.h>
     72 #define	TCPTIMERS
     73 #include <netinet/tcp_timer.h>
     74 #include <netinet/tcp_var.h>
     75 #include <netinet/tcp_debug.h>
     76 #include <netinet/udp.h>
     77 #include <netinet/ip_carp.h>
     78 #include <netinet/udp_var.h>
     79 #include <netinet/tcp_vtw.h>
     80 
     81 #include <arpa/inet.h>
     82 #include <kvm.h>
     83 #include <netdb.h>
     84 #include <stdio.h>
     85 #include <string.h>
     86 #include <unistd.h>
     87 #include <stdlib.h>
     88 #include <err.h>
     89 #include <util.h>
     90 #include <errno.h>
     91 
     92 #include "netstat.h"
     93 #include "vtw.h"
     94 #include "prog_ops.h"
     95 
     96 char	*inetname(struct in_addr *);
     97 void	inetprint(struct in_addr *, uint16_t, const char *, int);
     98 
     99 void	print_vtw_v4(const vtw_t *);
    100 
    101 /*
    102  * Print a summary of connections related to an Internet
    103  * protocol.  For TCP, also give state of connection.
    104  * Listening processes (aflag) are suppressed unless the
    105  * -a (all) flag is specified.
    106  */
    107 static int width;
    108 static int compact;
    109 
    110 /* VTW-related variables. */
    111 static struct timeval now;
    112 
    113 static void
    114 protoprhdr(void)
    115 {
    116 	printf("Active Internet connections");
    117 	if (aflag)
    118 		printf(" (including servers)");
    119 	putchar('\n');
    120 	if (Aflag)
    121 		printf("%-8.8s ", "PCB");
    122 	printf(
    123 	    Vflag ? "%-5.5s %-6.6s %-6.6s %s%-*.*s %-*.*s %-13.13s Expires\n"
    124 		  : "%-5.5s %-6.6s %-6.6s %s%-*.*s %-*.*s %s\n",
    125 		"Proto", "Recv-Q", "Send-Q", compact ? "" : " ",
    126 		width, width, "Local Address",
    127 		width, width, "Foreign Address",
    128 		"State");
    129 }
    130 
    131 static void
    132 protopr0(intptr_t ppcb, u_long rcv_sb_cc, u_long snd_sb_cc,
    133 	 struct in_addr *laddr, uint16_t lport,
    134 	 struct in_addr *faddr, uint16_t fport,
    135 	 short t_state, const char *name, int inp_flags,
    136 	 const struct timeval *expires)
    137 {
    138 	static const char *shorttcpstates[] = {
    139 		"CLOSED",	"LISTEN",	"SYNSEN",	"SYSRCV",
    140 		"ESTABL",	"CLWAIT",	"FWAIT1",	"CLOSNG",
    141 		"LASTAK",	"FWAIT2",	"TMWAIT"
    142 	};
    143 	int istcp;
    144 
    145 	istcp = strcmp(name, "tcp") == 0;
    146 
    147 	if (Aflag)
    148 		printf("%8" PRIxPTR " ", ppcb);
    149 
    150 	printf("%-5.5s %6ld %6ld%s", name, rcv_sb_cc, snd_sb_cc,
    151 	    compact ? "" : " ");
    152 	if (numeric_port) {
    153 		inetprint(laddr, lport, name, 1);
    154 		inetprint(faddr, fport, name, 1);
    155 	} else if (inp_flags & INP_ANONPORT) {
    156 		inetprint(laddr, lport, name, 1);
    157 		inetprint(faddr, fport, name, 0);
    158 	} else {
    159 		inetprint(laddr, lport, name, 0);
    160 		inetprint(faddr, fport, name, 0);
    161 	}
    162 	if (istcp) {
    163 		if (t_state < 0 || t_state >= TCP_NSTATES)
    164 			printf(" %d", t_state);
    165 		else
    166 			printf(" %s", compact ? shorttcpstates[t_state] :
    167 			    tcpstates[t_state]);
    168 	}
    169 	if (Vflag && expires != NULL) {
    170 		if (expires->tv_sec == 0 && expires->tv_usec == -1)
    171 			printf(" reclaimed");
    172 		else {
    173 			struct timeval delta;
    174 
    175 			timersub(expires, &now, &delta);
    176 			printf(" %.3fms",
    177 			    delta.tv_sec * 1000.0 + delta.tv_usec / 1000.0);
    178 		}
    179 	}
    180 	putchar('\n');
    181 }
    182 
    183 static void
    184 dbg_printf(const char *fmt, ...)
    185 {
    186 	return;
    187 }
    188 
    189 void
    190 print_vtw_v4(const vtw_t *vtw)
    191 {
    192 	const vtw_v4_t *v4 = (const vtw_v4_t *)vtw;
    193 	struct timeval delta;
    194 	struct in_addr la, fa;
    195 	char buf[2][32];
    196 	static const struct timeval zero = {.tv_sec = 0, .tv_usec = 0};
    197 
    198 	la.s_addr = v4->laddr;
    199 	fa.s_addr = v4->faddr;
    200 
    201 	snprintf(&buf[0][0], 32, "%s", inet_ntoa(la));
    202 	snprintf(&buf[1][0], 32, "%s", inet_ntoa(fa));
    203 
    204 	timersub(&vtw->expire, &now, &delta);
    205 
    206 	if (vtw->expire.tv_sec == 0 && vtw->expire.tv_usec == -1) {
    207 		dbg_printf("%15.15s:%d %15.15s:%d reclaimed\n",
    208 		    buf[0], ntohs(v4->lport),
    209 		    buf[1], ntohs(v4->fport));
    210 		if (!(Vflag && vflag))
    211 			return;
    212 	} else if (vtw->expire.tv_sec == 0)
    213 		return;
    214 	else if (timercmp(&delta, &zero, <) && !(Vflag && vflag)) {
    215 		dbg_printf("%15.15s:%d %15.15s:%d expired\n",
    216 		    buf[0], ntohs(v4->lport),
    217 		    buf[1], ntohs(v4->fport));
    218 		return;
    219 	} else {
    220 		dbg_printf("%15.15s:%d %15.15s:%d expires in %.3fms\n",
    221 		    buf[0], ntohs(v4->lport),
    222 		    buf[1], ntohs(v4->fport),
    223 		    delta.tv_sec * 1000.0 + delta.tv_usec / 1000.0);
    224 	}
    225 	protopr0(0, 0, 0,
    226 		 &la, v4->lport,
    227 		 &fa, v4->fport,
    228 		 TCPS_TIME_WAIT, "tcp", 0, &vtw->expire);
    229 }
    230 
    231 struct kinfo_pcb *
    232 getpcblist_sysctl(const char *name, size_t *len)
    233 {
    234 	int mib[8];
    235 	size_t namelen = 0, size = 0;
    236 	char *mibname = NULL;
    237 	struct kinfo_pcb *pcblist;
    238 
    239 	memset(mib, 0, sizeof(mib));
    240 
    241 	if (asprintf(&mibname, "net.inet%s.%s.pcblist", name + 3, name) == -1)
    242 		err(1, "asprintf");
    243 
    244 	/* get dynamic pcblist node */
    245 	if (prog_sysctlnametomib(mibname, mib, &namelen) == -1) {
    246 		if (errno == ENOENT) {
    247 			*len = 0;
    248 			return NULL;
    249 		}
    250 
    251 		err(1, "sysctlnametomib: %s", mibname);
    252 	}
    253 
    254 	free(mibname);
    255 
    256 	if (prog_sysctl(mib, __arraycount(mib), NULL, &size, NULL, 0) == -1)
    257 		err(1, "sysctl (query)");
    258 
    259 	if ((pcblist = malloc(size)) == NULL)
    260 		err(1, "malloc");
    261 	memset(pcblist, 0, size);
    262 
    263 	mib[6] = sizeof(*pcblist);
    264 	mib[7] = size / sizeof(*pcblist);
    265 
    266 	if (prog_sysctl(mib, __arraycount(mib), pcblist, &size, NULL, 0) == -1)
    267 		err(1, "sysctl (copy)");
    268 
    269 	*len = size / sizeof(*pcblist);
    270 	return pcblist;
    271 
    272 }
    273 
    274 static struct kinfo_pcb *
    275 getpcblist_kmem(u_long off, const char *name, size_t *len)
    276 {
    277 	struct inpcbtable table;
    278 	struct inpcb *next, *prev;
    279 	struct in4pcb in4pcb;
    280 	struct inpcb *inp;
    281 	struct tcpcb tcpcb;
    282 	struct socket sockb;
    283 	int istcp = strcmp(name, "tcp") == 0;
    284 	struct kinfo_pcb *pcblist;
    285 	size_t size = 100, i;
    286 	struct sockaddr_in sin;
    287 	struct inpcbqueue *head;
    288 
    289 	if (off == 0) {
    290 		*len = 0;
    291 		return NULL;
    292 	}
    293 
    294 	kread(off, (char *)&table, sizeof table);
    295 	head = &table.inpt_queue;
    296 	next = TAILQ_FIRST(head);
    297 	prev = TAILQ_END(head);
    298 
    299 	pcblist = NULL;
    300 	if (reallocarr(&pcblist, size, sizeof(*pcblist)) != 0)
    301 		err(1, "reallocarr");
    302 
    303 	i = 0;
    304 	while (next != TAILQ_END(head)) {
    305 		kread((u_long)next, (char *)&in4pcb, sizeof in4pcb);
    306 		prev = next;
    307 		inp = (struct inpcb *)&in4pcb;
    308 		next = TAILQ_NEXT(inp, inp_queue);
    309 
    310 		if (inp->inp_af != AF_INET)
    311 			continue;
    312 
    313 		kread((u_long)inp->inp_socket, (char *)&sockb, sizeof(sockb));
    314 		if (istcp) {
    315 			kread((u_long)inp->inp_ppcb,
    316 			    (char *)&tcpcb, sizeof (tcpcb));
    317 		}
    318 		pcblist[i].ki_ppcbaddr =
    319 		    istcp ? (uintptr_t) inp->inp_ppcb : (uintptr_t) prev;
    320 		pcblist[i].ki_rcvq = (uint64_t)sockb.so_rcv.sb_cc;
    321 		pcblist[i].ki_sndq = (uint64_t)sockb.so_snd.sb_cc;
    322 
    323 		sin.sin_addr = in4p_laddr(inp);
    324 		sin.sin_port = inp->inp_lport;
    325 		memcpy(&pcblist[i].ki_s, &sin, sizeof(sin));
    326 		sin.sin_addr = in4p_faddr(inp);
    327 		sin.sin_port = inp->inp_fport;
    328 		memcpy(&pcblist[i].ki_d, &sin, sizeof(sin));
    329 		pcblist[i].ki_tstate = tcpcb.t_state;
    330 		pcblist[i].ki_pflags = inp->inp_flags;
    331 		if (i++ == size) {
    332 			size += 100;
    333 			if (reallocarr(&pcblist, size, sizeof(*pcblist)) != 0)
    334 				err(1, "reallocarr");
    335 		}
    336 	}
    337 	*len = i;
    338 	return pcblist;
    339 }
    340 
    341 void
    342 protopr(u_long off, const char *name)
    343 {
    344 	static int first = 1;
    345 	struct kinfo_pcb *pcblist;
    346 	size_t i, len;
    347 
    348 	compact = 0;
    349 	if (Aflag) {
    350 		if (!numeric_addr)
    351 			width = 18;
    352 		else {
    353 			width = 21;
    354 			compact = 1;
    355 		}
    356 	} else
    357 		width = 22;
    358 
    359 	if (use_sysctl)
    360 		pcblist = getpcblist_sysctl(name, &len);
    361 	else
    362 		pcblist = getpcblist_kmem(off, name, &len);
    363 
    364 	for (i = 0; i < len; i++) {
    365 		struct sockaddr_in src, dst;
    366 
    367 		memcpy(&src, &pcblist[i].ki_s, sizeof(src));
    368 		memcpy(&dst, &pcblist[i].ki_d, sizeof(dst));
    369 
    370 		if (!aflag && (inet_lnaof(dst.sin_addr) == INADDR_ANY))
    371 			continue;
    372 
    373 		if (first) {
    374 			protoprhdr();
    375 			first = 0;
    376 		}
    377 		protopr0((intptr_t) pcblist[i].ki_ppcbaddr,
    378 			 pcblist[i].ki_rcvq, pcblist[i].ki_sndq,
    379 			 &src.sin_addr, src.sin_port,
    380 			 &dst.sin_addr, dst.sin_port,
    381 			 pcblist[i].ki_tstate, name,
    382 			 pcblist[i].ki_pflags, NULL);
    383 	}
    384 
    385 	free(pcblist);
    386 
    387 	if (strcmp(name, "tcp") == 0) {
    388 		struct timeval t;
    389 		timebase(&t);
    390 		gettimeofday(&now, NULL);
    391 		timersub(&now, &t, &now);
    392 		show_vtw_v4(print_vtw_v4);
    393 	}
    394 }
    395 
    396 /*
    397  * Dump TCP statistics structure.
    398  */
    399 void
    400 tcp_stats(u_long off, const char *name)
    401 {
    402 	uint64_t tcpstat[TCP_NSTATS];
    403 
    404 	if (use_sysctl) {
    405 		size_t size = sizeof(tcpstat);
    406 
    407 		if (prog_sysctlbyname("net.inet.tcp.stats", tcpstat, &size,
    408 				 NULL, 0) == -1 && errno != ENOMEM)
    409 			return;
    410 	} else {
    411 		warnx("%s stats not available via KVM.", name);
    412 		return;
    413 	}
    414 
    415 	printf ("%s:\n", name);
    416 
    417 #define	ps(f, m) if (tcpstat[f] || sflag <= 1)	\
    418 		printf(m, tcpstat[f])
    419 #define	p(f, m) if (tcpstat[f] || sflag <= 1)			\
    420 		printf(m, tcpstat[f], plural(tcpstat[f]))
    421 #define	p2(f1, f2, m) if (tcpstat[f1] || tcpstat[f2] || sflag <= 1)	\
    422 		printf(m, tcpstat[f1], plural(tcpstat[f1]),		\
    423 		    tcpstat[f2], plural(tcpstat[f2]))
    424 #define	p2s(f1, f2, m) if (tcpstat[f1] || tcpstat[f2] || sflag <= 1)	\
    425 		printf(m, tcpstat[f1], plural(tcpstat[f1]),		\
    426 		    tcpstat[f2])
    427 #define	p3(f, m) if (tcpstat[f] || sflag <= 1)			\
    428 		printf(m, tcpstat[f], plurales(tcpstat[f]))
    429 
    430 	p(TCP_STAT_SNDTOTAL, "\t%" PRIu64 " packet%s sent\n");
    431 	p2(TCP_STAT_SNDPACK,TCP_STAT_SNDBYTE,
    432 		"\t\t%" PRIu64 " data packet%s (%" PRIu64 " byte%s)\n");
    433 	p2(TCP_STAT_SNDREXMITPACK, TCP_STAT_SNDREXMITBYTE,
    434 		"\t\t%" PRIu64 " data packet%s (%" PRIu64 " byte%s) retransmitted\n");
    435 	p2s(TCP_STAT_SNDACKS, TCP_STAT_DELACK,
    436 		"\t\t%" PRIu64 " ack-only packet%s (%" PRIu64 " delayed)\n");
    437 	p(TCP_STAT_SNDURG, "\t\t%" PRIu64 " URG only packet%s\n");
    438 	p(TCP_STAT_SNDPROBE, "\t\t%" PRIu64 " window probe packet%s\n");
    439 	p(TCP_STAT_SNDWINUP, "\t\t%" PRIu64 " window update packet%s\n");
    440 	p(TCP_STAT_SNDCTRL, "\t\t%" PRIu64 " control packet%s\n");
    441 	p(TCP_STAT_SELFQUENCH,
    442 	    "\t\t%" PRIu64 " send attempt%s resulted in self-quench\n");
    443 	p(TCP_STAT_RCVTOTAL, "\t%" PRIu64 " packet%s received\n");
    444 	p2(TCP_STAT_RCVACKPACK, TCP_STAT_RCVACKBYTE,
    445 	    "\t\t%" PRIu64 " ack%s (for %" PRIu64 " byte%s)\n");
    446 	p(TCP_STAT_RCVDUPACK, "\t\t%" PRIu64 " duplicate ack%s\n");
    447 	p(TCP_STAT_RCVACKTOOMUCH, "\t\t%" PRIu64 " ack%s for unsent data\n");
    448 	p2(TCP_STAT_RCVPACK, TCP_STAT_RCVBYTE,
    449 	    "\t\t%" PRIu64 " packet%s (%" PRIu64 " byte%s) received in-sequence\n");
    450 	p2(TCP_STAT_RCVDUPPACK, TCP_STAT_RCVDUPBYTE,
    451 	    "\t\t%" PRIu64 " completely duplicate packet%s (%" PRIu64 " byte%s)\n");
    452 	p(TCP_STAT_PAWSDROP, "\t\t%" PRIu64 " old duplicate packet%s\n");
    453 	p2(TCP_STAT_RCVPARTDUPPACK, TCP_STAT_RCVPARTDUPBYTE,
    454 	    "\t\t%" PRIu64 " packet%s with some dup. data (%" PRIu64 " byte%s duped)\n");
    455 	p2(TCP_STAT_RCVOOPACK, TCP_STAT_RCVOOBYTE,
    456 	    "\t\t%" PRIu64 " out-of-order packet%s (%" PRIu64 " byte%s)\n");
    457 	p2(TCP_STAT_RCVPACKAFTERWIN, TCP_STAT_RCVBYTEAFTERWIN,
    458 	    "\t\t%" PRIu64 " packet%s (%" PRIu64 " byte%s) of data after window\n");
    459 	p(TCP_STAT_RCVWINPROBE, "\t\t%" PRIu64 " window probe%s\n");
    460 	p(TCP_STAT_RCVWINUPD, "\t\t%" PRIu64 " window update packet%s\n");
    461 	p(TCP_STAT_RCVAFTERCLOSE,
    462 	    "\t\t%" PRIu64 " packet%s received after close\n");
    463 	p(TCP_STAT_RCVBADSUM,
    464 	    "\t\t%" PRIu64 " discarded for bad checksum%s\n");
    465 	p(TCP_STAT_RCVBADOFF,
    466 	    "\t\t%" PRIu64 " discarded for bad header offset field%s\n");
    467 	ps(TCP_STAT_RCVSHORT,
    468 	    "\t\t%" PRIu64 " discarded because packet too short\n");
    469 	p(TCP_STAT_CONNATTEMPT, "\t%" PRIu64 " connection request%s\n");
    470 	p(TCP_STAT_ACCEPTS, "\t%" PRIu64 " connection accept%s\n");
    471 	p(TCP_STAT_CONNECTS,
    472 	    "\t%" PRIu64 " connection%s established (including accepts)\n");
    473 	p2(TCP_STAT_CLOSED, TCP_STAT_DROPS,
    474 	    "\t%" PRIu64 " connection%s closed (including %" PRIu64 " drop%s)\n");
    475 	p(TCP_STAT_CONNDROPS,
    476 	    "\t%" PRIu64 " embryonic connection%s dropped\n");
    477 	p(TCP_STAT_DELAYED_FREE, "\t%" PRIu64 " delayed free%s of tcpcb\n");
    478 	p2(TCP_STAT_RTTUPDATED, TCP_STAT_SEGSTIMED,
    479 	    "\t%" PRIu64 " segment%s updated rtt (of %" PRIu64 " attempt%s)\n");
    480 	p(TCP_STAT_REXMTTIMEO, "\t%" PRIu64 " retransmit timeout%s\n");
    481 	p(TCP_STAT_TIMEOUTDROP,
    482 	    "\t\t%" PRIu64 " connection%s dropped by rexmit timeout\n");
    483 	p2(TCP_STAT_PERSISTTIMEO, TCP_STAT_PERSISTDROPS,
    484 	   "\t%" PRIu64 " persist timeout%s (resulting in %" PRIu64 " dropped "
    485 		"connection%s)\n");
    486 	p(TCP_STAT_KEEPTIMEO, "\t%" PRIu64 " keepalive timeout%s\n");
    487 	p(TCP_STAT_KEEPPROBE, "\t\t%" PRIu64 " keepalive probe%s sent\n");
    488 	p(TCP_STAT_KEEPDROPS,
    489 	    "\t\t%" PRIu64 " connection%s dropped by keepalive\n");
    490 	p(TCP_STAT_PREDACK, "\t%" PRIu64 " correct ACK header prediction%s\n");
    491 	p(TCP_STAT_PREDDAT,
    492 	    "\t%" PRIu64 " correct data packet header prediction%s\n");
    493 	p3(TCP_STAT_PCBHASHMISS, "\t%" PRIu64 " PCB hash miss%s\n");
    494 	ps(TCP_STAT_NOPORT, "\t%" PRIu64 " dropped due to no socket\n");
    495 	p(TCP_STAT_CONNSDRAINED,
    496 	    "\t%" PRIu64 " connection%s drained due to memory shortage\n");
    497 	p(TCP_STAT_PMTUBLACKHOLE,
    498 	    "\t%" PRIu64 " PMTUD blackhole%s detected\n");
    499 
    500 	p(TCP_STAT_BADSYN, "\t%" PRIu64 " bad connection attempt%s\n");
    501 	ps(TCP_STAT_SC_ADDED, "\t%" PRIu64 " SYN cache entries added\n");
    502 	p(TCP_STAT_SC_COLLISIONS, "\t\t%" PRIu64 " hash collision%s\n");
    503 	ps(TCP_STAT_SC_COMPLETED, "\t\t%" PRIu64 " completed\n");
    504 	ps(TCP_STAT_SC_ABORTED,
    505 	    "\t\t%" PRIu64 " aborted (no space to build PCB)\n");
    506 	ps(TCP_STAT_SC_TIMED_OUT, "\t\t%" PRIu64 " timed out\n");
    507 	ps(TCP_STAT_SC_OVERFLOWED,
    508 	    "\t\t%" PRIu64 " dropped due to overflow\n");
    509 	ps(TCP_STAT_SC_BUCKETOVERFLOW,
    510 	    "\t\t%" PRIu64 " dropped due to bucket overflow\n");
    511 	ps(TCP_STAT_SC_RESET, "\t\t%" PRIu64 " dropped due to RST\n");
    512 	ps(TCP_STAT_SC_UNREACH,
    513 	    "\t\t%" PRIu64 " dropped due to ICMP unreachable\n");
    514 	ps(TCP_STAT_SC_DELAYED_FREE,
    515 	    "\t\t%" PRIu64 " delayed free of SYN cache entries\n");
    516 	p(TCP_STAT_SC_RETRANSMITTED,
    517 	    "\t%" PRIu64 " SYN,ACK%s retransmitted\n");
    518 	p(TCP_STAT_SC_DUPESYN,
    519 	    "\t%" PRIu64 " duplicate SYN%s received for entries "
    520 		"already in the cache\n");
    521 	p(TCP_STAT_SC_DROPPED,
    522 	    "\t%" PRIu64 " SYN%s dropped (no route or no space)\n");
    523 	p(TCP_STAT_BADSIG, "\t%" PRIu64 " packet%s with bad signature\n");
    524 	p(TCP_STAT_GOODSIG, "\t%" PRIu64 " packet%s with good signature\n");
    525 
    526 	p(TCP_STAT_ECN_SHS, "\t%" PRIu64 " successful ECN handshake%s\n");
    527 	p(TCP_STAT_ECN_CE, "\t%" PRIu64 " packet%s with ECN CE bit\n");
    528 	p(TCP_STAT_ECN_ECT, "\t%" PRIu64 " packet%s ECN ECT(0) bit\n");
    529 #undef p
    530 #undef ps
    531 #undef p2
    532 #undef p2s
    533 #undef p3
    534 	show_vtw_stats();
    535 }
    536 
    537 /*
    538  * Dump UDP statistics structure.
    539  */
    540 void
    541 udp_stats(u_long off, const char *name)
    542 {
    543 	uint64_t udpstat[UDP_NSTATS];
    544 	uint64_t delivered;
    545 
    546 	if (use_sysctl) {
    547 		size_t size = sizeof(udpstat);
    548 
    549 		if (prog_sysctlbyname("net.inet.udp.stats", udpstat, &size,
    550 				 NULL, 0) == -1 && errno != ENOMEM)
    551 			return;
    552 	} else {
    553 		warnx("%s stats not available via KVM.", name);
    554 		return;
    555 	}
    556 
    557 	printf ("%s:\n", name);
    558 
    559 #define	ps(f, m) if (udpstat[f] || sflag <= 1)	\
    560 		printf(m, udpstat[f])
    561 #define	p(f, m) if (udpstat[f] || sflag <= 1)			\
    562 		printf(m, udpstat[f], plural(udpstat[f]))
    563 #define	p3(f, m) if (udpstat[f] || sflag <= 1)			\
    564 		printf(m, udpstat[f], plurales(udpstat[f]))
    565 
    566 	p(UDP_STAT_IPACKETS, "\t%" PRIu64 " datagram%s received\n");
    567 	ps(UDP_STAT_HDROPS, "\t%" PRIu64 " with incomplete header\n");
    568 	ps(UDP_STAT_BADLEN, "\t%" PRIu64 " with bad data length field\n");
    569 	ps(UDP_STAT_BADSUM, "\t%" PRIu64 " with bad checksum\n");
    570 	ps(UDP_STAT_NOPORT, "\t%" PRIu64 " dropped due to no socket\n");
    571 	p(UDP_STAT_NOPORTBCAST, "\t%" PRIu64
    572 	    " broadcast/multicast datagram%s dropped due to no socket\n");
    573 	ps(UDP_STAT_FULLSOCK, "\t%" PRIu64
    574 	    " dropped due to full socket buffers\n");
    575 	delivered = udpstat[UDP_STAT_IPACKETS] -
    576 		    udpstat[UDP_STAT_HDROPS] -
    577 		    udpstat[UDP_STAT_BADLEN] -
    578 		    udpstat[UDP_STAT_BADSUM] -
    579 		    udpstat[UDP_STAT_NOPORT] -
    580 		    udpstat[UDP_STAT_NOPORTBCAST] -
    581 		    udpstat[UDP_STAT_FULLSOCK];
    582 	if (delivered || sflag <= 1)
    583 		printf("\t%" PRIu64 " delivered\n", delivered);
    584 	p3(UDP_STAT_PCBHASHMISS, "\t%" PRIu64 " PCB hash miss%s\n");
    585 	p(UDP_STAT_OPACKETS, "\t%" PRIu64 " datagram%s output\n");
    586 
    587 #undef ps
    588 #undef p
    589 #undef p3
    590 }
    591 
    592 /*
    593  * Dump IP statistics structure.
    594  */
    595 void
    596 ip_stats(u_long off, const char *name)
    597 {
    598 	uint64_t ipstat[IP_NSTATS];
    599 
    600 	if (use_sysctl) {
    601 		size_t size = sizeof(ipstat);
    602 
    603 		if (prog_sysctlbyname("net.inet.ip.stats", ipstat, &size,
    604 		    NULL, 0) == -1 && errno != ENOMEM)
    605 			return;
    606 	} else {
    607 		warnx("%s stats not available via KVM.", name);
    608 		return;
    609 	}
    610 
    611 	printf("%s:\n", name);
    612 
    613 #define	ps(f, m) if (ipstat[f] || sflag <= 1)	\
    614 		printf(m, ipstat[f])
    615 #define	p(f, m) if (ipstat[f] || sflag <= 1)		\
    616 		printf(m, ipstat[f], plural(ipstat[f]))
    617 
    618 	p(IP_STAT_TOTAL, "\t%" PRIu64 " total packet%s received\n");
    619 	p(IP_STAT_BADSUM, "\t%" PRIu64 " bad header checksum%s\n");
    620 	ps(IP_STAT_TOOSMALL, "\t%" PRIu64 " with size smaller than minimum\n");
    621 	ps(IP_STAT_TOOSHORT, "\t%" PRIu64 " with data size < data length\n");
    622 	ps(IP_STAT_TOOLONG,
    623 	    "\t%" PRIu64 " with length > max ip packet size\n");
    624 	ps(IP_STAT_BADHLEN, "\t%" PRIu64 " with header length < data size\n");
    625 	ps(IP_STAT_BADLEN, "\t%" PRIu64 " with data length < header length\n");
    626 	ps(IP_STAT_BADOPTIONS, "\t%" PRIu64 " with bad options\n");
    627 	ps(IP_STAT_BADVERS, "\t%" PRIu64 " with incorrect version number\n");
    628 	p(IP_STAT_FRAGMENTS, "\t%" PRIu64 " fragment%s received\n");
    629 	p(IP_STAT_FRAGDROPPED,
    630 	    "\t%" PRIu64 " fragment%s dropped (dup or out of space)\n");
    631 	p(IP_STAT_RCVMEMDROP,
    632 	    "\t%" PRIu64 " fragment%s dropped (out of ipqent)\n");
    633 	p(IP_STAT_BADFRAGS, "\t%" PRIu64 " malformed fragment%s dropped\n");
    634 	p(IP_STAT_FRAGTIMEOUT,
    635 	    "\t%" PRIu64 " fragment%s dropped after timeout\n");
    636 	p(IP_STAT_REASSEMBLED, "\t%" PRIu64 " packet%s reassembled ok\n");
    637 	p(IP_STAT_DELIVERED, "\t%" PRIu64 " packet%s for this host\n");
    638 	p(IP_STAT_NOPROTO,
    639 	    "\t%" PRIu64 " packet%s for unknown/unsupported protocol\n");
    640 	p(IP_STAT_FORWARD, "\t%" PRIu64 " packet%s forwarded");
    641 	p(IP_STAT_FASTFORWARD, " (%" PRIu64 " packet%s fast forwarded)");
    642 	if (ipstat[IP_STAT_FORWARD] || sflag <= 1)
    643 		putchar('\n');
    644 	p(IP_STAT_CANTFORWARD, "\t%" PRIu64 " packet%s not forwardable\n");
    645 	p(IP_STAT_REDIRECTSENT, "\t%" PRIu64 " redirect%s sent\n");
    646 	p(IP_STAT_NOGIF, "\t%" PRIu64 " packet%s no matching gif found\n");
    647 	p(IP_STAT_NOIPSEC,
    648 	    "\t%" PRIu64 " packet%s no matching ipsecif found\n");
    649 	p(IP_STAT_LOCALOUT, "\t%" PRIu64 " packet%s sent from this host\n");
    650 	p(IP_STAT_RAWOUT,
    651 	    "\t%" PRIu64 " packet%s sent with fabricated ip header\n");
    652 	p(IP_STAT_ODROPPED,
    653 	    "\t%" PRIu64 " output packet%s dropped due to no bufs, etc.\n");
    654 	p(IP_STAT_NOROUTE,
    655 	    "\t%" PRIu64 " output packet%s discarded due to no route\n");
    656 	p(IP_STAT_FRAGMENTED, "\t%" PRIu64 " output datagram%s fragmented\n");
    657 	p(IP_STAT_OFRAGMENTS, "\t%" PRIu64 " fragment%s created\n");
    658 	p(IP_STAT_CANTFRAG,
    659 	    "\t%" PRIu64 " datagram%s that can't be fragmented\n");
    660 	p(IP_STAT_BADADDR,
    661 	    "\t%" PRIu64 " datagram%s with bad address in header\n");
    662 	p(IP_STAT_PFILDROP_IN,
    663 	    "\t%" PRIu64 " input packet%s dropped by pfil\n");
    664 	p(IP_STAT_PFILDROP_OUT,
    665 	    "\t%" PRIu64 " output packet%s dropped by pfil\n");
    666 	p(IP_STAT_IPSECDROP_IN,
    667 	    "\t%" PRIu64 " input packet%s dropped by IPsec\n");
    668 	p(IP_STAT_IPSECDROP_OUT,
    669 	    "\t%" PRIu64 " output packet%s dropped by IPsec\n");
    670 	p(IP_STAT_IFDROP,
    671 	    "\t%" PRIu64 " input packet%s dropped due to interface state\n");
    672 	p(IP_STAT_TIMXCEED,
    673 	    "\t%" PRIu64 " packet%s dropped due to TTL exceeded\n");
    674 	p(IP_STAT_IFNOADDR,
    675 	    "\t%" PRIu64 " output packet%s dropped (no IP address)\n");
    676 	p(IP_STAT_RTREJECT,
    677 	    "\t%" PRIu64 " output packet%s discarded due to reject route\n");
    678 	p(IP_STAT_BCASTDENIED,
    679 	    "\t%" PRIu64 " output packet%s dropped (broadcast prohibited)\n");
    680 #undef ps
    681 #undef p
    682 }
    683 
    684 /*
    685  * Dump ICMP statistics.
    686  */
    687 void
    688 icmp_stats(u_long off, const char *name)
    689 {
    690 	uint64_t icmpstat[ICMP_NSTATS];
    691 	int i, first;
    692 
    693 	if (use_sysctl) {
    694 		size_t size = sizeof(icmpstat);
    695 
    696 		if (prog_sysctlbyname("net.inet.icmp.stats", icmpstat, &size,
    697 				 NULL, 0) == -1 && errno != ENOMEM)
    698 			return;
    699 	} else {
    700 		warnx("%s stats not available via KVM.", name);
    701 		return;
    702 	}
    703 
    704 	printf("%s:\n", name);
    705 
    706 #define	p(f, m) if (icmpstat[f] || sflag <= 1) \
    707     printf(m, icmpstat[f], plural(icmpstat[f]))
    708 
    709 	p(ICMP_STAT_ERROR, "\t%" PRIu64 " call%s to icmp_error\n");
    710 	p(ICMP_STAT_OLDICMP, "\t%" PRIu64
    711 	    " error%s not generated because old message was icmp\n");
    712 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
    713 		if (icmpstat[ICMP_STAT_OUTHIST + i] != 0) {
    714 			if (first) {
    715 				printf("\tOutput histogram:\n");
    716 				first = 0;
    717 			}
    718 			printf("\t\t%s: %" PRIu64 "\n", icmp_type[i],
    719 			   icmpstat[ICMP_STAT_OUTHIST + i]);
    720 		}
    721 	p(ICMP_STAT_BADCODE, "\t%" PRIu64 " message%s with bad code fields\n");
    722 	p(ICMP_STAT_TOOSHORT, "\t%" PRIu64 " message%s < minimum length\n");
    723 	p(ICMP_STAT_CHECKSUM, "\t%" PRIu64 " bad checksum%s\n");
    724 	p(ICMP_STAT_BADLEN, "\t%" PRIu64 " message%s with bad length\n");
    725 	p(ICMP_STAT_BMCASTECHO,
    726 	    "\t%" PRIu64 " multicast echo request%s ignored\n");
    727 	p(ICMP_STAT_BMCASTTSTAMP,
    728 	    "\t%" PRIu64 " multicast timestamp request%s ignored\n");
    729 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
    730 		if (icmpstat[ICMP_STAT_INHIST + i] != 0) {
    731 			if (first) {
    732 				printf("\tInput histogram:\n");
    733 				first = 0;
    734 			}
    735 			printf("\t\t%s: %" PRIu64 "\n", icmp_type[i],
    736 			    icmpstat[ICMP_STAT_INHIST + i]);
    737 		}
    738 	p(ICMP_STAT_REFLECT, "\t%" PRIu64 " message response%s generated\n");
    739 	p(ICMP_STAT_PMTUCHG, "\t%" PRIu64 " path MTU change%s\n");
    740 #undef p
    741 }
    742 
    743 /*
    744  * Dump IGMP statistics structure.
    745  */
    746 void
    747 igmp_stats(u_long off, const char *name)
    748 {
    749 	uint64_t igmpstat[IGMP_NSTATS];
    750 
    751 	if (use_sysctl) {
    752 		size_t size = sizeof(igmpstat);
    753 
    754 		if (prog_sysctlbyname("net.inet.igmp.stats", igmpstat, &size,
    755 		    NULL, 0) == -1 && errno != ENOMEM)
    756 			return;
    757 	} else {
    758 		warnx("%s stats not available via KVM.", name);
    759 		return;
    760 	}
    761 
    762 	printf("%s:\n", name);
    763 
    764 #define	p(f, m) if (igmpstat[f] || sflag <= 1)			\
    765 		printf(m, igmpstat[f], plural(igmpstat[f]))
    766 #define	py(f, m) if (igmpstat[f] || sflag <= 1)				\
    767 		printf(m, igmpstat[f], igmpstat[f] != 1 ? "ies" : "y")
    768 
    769 	p(IGMP_STAT_RCV_TOTAL, "\t%" PRIu64 " message%s received\n");
    770 	p(IGMP_STAT_RCV_TOOSHORT,
    771 	    "\t%" PRIu64 " message%s received with too few bytes\n");
    772 	p(IGMP_STAT_RCV_BADSUM,
    773 	    "\t%" PRIu64 " message%s received with bad checksum\n");
    774 	py(IGMP_STAT_RCV_QUERIES,
    775 	    "\t%" PRIu64 " membership quer%s received\n");
    776 	py(IGMP_STAT_RCV_BADQUERIES,
    777 	    "\t%" PRIu64 " membership quer%s received with invalid field(s)\n");
    778 	p(IGMP_STAT_RCV_REPORTS,
    779 	    "\t%" PRIu64 " membership report%s received\n");
    780 	p(IGMP_STAT_RCV_BADREPORTS, "\t%" PRIu64
    781 	    " membership report%s received with invalid field(s)\n");
    782 	p(IGMP_STAT_RCV_OURREPORTS, "\t%" PRIu64
    783 	    " membership report%s received for groups to which we belong\n");
    784 	p(IGMP_STAT_SND_REPORTS,
    785 	    "\t%" PRIu64 " membership report%s sent\n");
    786 #undef p
    787 #undef py
    788 }
    789 
    790 /*
    791  * Dump CARP statistics structure.
    792  */
    793 void
    794 carp_stats(u_long off, const char *name)
    795 {
    796 	uint64_t carpstat[CARP_NSTATS];
    797 
    798 	if (use_sysctl) {
    799 		size_t size = sizeof(carpstat);
    800 
    801 		if (prog_sysctlbyname("net.inet.carp.stats", carpstat, &size,
    802 				 NULL, 0) == -1 && errno != ENOMEM)
    803 			return;
    804 	} else {
    805 		warnx("%s stats not available via KVM.", name);
    806 		return;
    807 	}
    808 
    809 	printf("%s:\n", name);
    810 
    811 #define p(f, m) if (carpstat[f] || sflag <= 1) \
    812 	printf(m, carpstat[f], plural(carpstat[f]))
    813 #define p2(f, m) if (carpstat[f] || sflag <= 1) \
    814 	printf(m, carpstat[f])
    815 
    816 	p(CARP_STAT_IPACKETS, "\t%" PRIu64 " packet%s received (IPv4)\n");
    817 	p(CARP_STAT_IPACKETS6, "\t%" PRIu64 " packet%s received (IPv6)\n");
    818 	p(CARP_STAT_BADIF,
    819 	    "\t\t%" PRIu64 " packet%s discarded for bad interface\n");
    820 	p(CARP_STAT_BADTTL,
    821 	    "\t\t%" PRIu64 " packet%s discarded for wrong TTL\n");
    822 	p(CARP_STAT_HDROPS, "\t\t%" PRIu64 " packet%s shorter than header\n");
    823 	p(CARP_STAT_BADSUM,
    824 	    "\t\t%" PRIu64 " packet%s discarded for bad checksum\n");
    825 	p(CARP_STAT_BADVER,
    826 	    "\t\t%" PRIu64 " packet%s discarded with a bad version\n");
    827 	p2(CARP_STAT_BADLEN,
    828 	    "\t\t%" PRIu64 " discarded because packet was too short\n");
    829 	p(CARP_STAT_BADAUTH,
    830 	    "\t\t%" PRIu64 " packet%s discarded for bad authentication\n");
    831 	p(CARP_STAT_BADVHID,
    832 	    "\t\t%" PRIu64 " packet%s discarded for bad vhid\n");
    833 	p(CARP_STAT_BADADDRS, "\t\t%" PRIu64
    834 	    " packet%s discarded because of a bad address list\n");
    835 	p(CARP_STAT_OPACKETS, "\t%" PRIu64 " packet%s sent (IPv4)\n");
    836 	p(CARP_STAT_OPACKETS6, "\t%" PRIu64 " packet%s sent (IPv6)\n");
    837 	p2(CARP_STAT_ONOMEM,
    838 	    "\t\t%" PRIu64 " send failed due to mbuf memory error\n");
    839 #undef p
    840 #undef p2
    841 }
    842 
    843 /*
    844  * Dump PIM statistics structure.
    845  */
    846 void
    847 pim_stats(u_long off, const char *name)
    848 {
    849 	struct pimstat pimstat;
    850 
    851 	if (off == 0)
    852 		return;
    853 	if (kread(off, (char *)&pimstat, sizeof (pimstat)) != 0) {
    854 		/* XXX: PIM is probably not enabled in the kernel */
    855 		return;
    856 	}
    857 
    858 	printf("%s:\n", name);
    859 
    860 #define	p(f, m) if (pimstat.f || sflag <= 1) \
    861 	printf(m, pimstat.f, plural(pimstat.f))
    862 
    863 	p(pims_rcv_total_msgs, "\t%" PRIu64 " message%s received\n");
    864 	p(pims_rcv_total_bytes, "\t%" PRIu64 " byte%s received\n");
    865 	p(pims_rcv_tooshort,
    866 	    "\t%" PRIu64 " message%s received with too few bytes\n");
    867 	p(pims_rcv_badsum,
    868 	    "\t%" PRIu64 " message%s received with bad checksum\n");
    869 	p(pims_rcv_badversion,
    870 	    "\t%" PRIu64 " message%s received with bad version\n");
    871 	p(pims_rcv_registers_msgs,
    872 	    "\t%" PRIu64 " data register message%s received\n");
    873 	p(pims_rcv_registers_bytes,
    874 	    "\t%" PRIu64 " data register byte%s received\n");
    875 	p(pims_rcv_registers_wrongiif,
    876 	    "\t%" PRIu64 " data register message%s received on wrong iif\n");
    877 	p(pims_rcv_badregisters,
    878 	    "\t%" PRIu64 " bad register%s received\n");
    879 	p(pims_snd_registers_msgs,
    880 	    "\t%" PRIu64 " data register message%s sent\n");
    881 	p(pims_snd_registers_bytes,
    882 	    "\t%" PRIu64 " data register byte%s sent\n");
    883 #undef p
    884 }
    885 
    886 /*
    887  * Dump the ARP statistics structure.
    888  */
    889 void
    890 arp_stats(u_long off, const char *name)
    891 {
    892 	uint64_t arpstat[ARP_NSTATS];
    893 
    894 	if (use_sysctl) {
    895 		size_t size = sizeof(arpstat);
    896 
    897 		if (prog_sysctlbyname("net.inet.arp.stats", arpstat, &size,
    898 				 NULL, 0) == -1 && errno != ENOMEM)
    899 			return;
    900 	} else {
    901 		warnx("%s stats not available via KVM.", name);
    902 		return;
    903 	}
    904 
    905 	printf("%s:\n", name);
    906 
    907 #define	ps(f, m) if (arpstat[f] || sflag <= 1) \
    908     printf(m, arpstat[f])
    909 #define	p(f, m) if (arpstat[f] || sflag <= 1) \
    910     printf(m, arpstat[f], plural(arpstat[f]))
    911 
    912 	p(ARP_STAT_SNDTOTAL, "\t%" PRIu64 " packet%s sent\n");
    913 	p(ARP_STAT_SNDREPLY, "\t\t%" PRIu64 " reply packet%s\n");
    914 	p(ARP_STAT_SENDREQUEST, "\t\t%" PRIu64 " request packet%s\n");
    915 
    916 	p(ARP_STAT_RCVTOTAL, "\t%" PRIu64 " packet%s received\n");
    917 	p(ARP_STAT_RCVREPLY, "\t\t%" PRIu64 " reply packet%s\n");
    918 	p(ARP_STAT_RCVREQUEST, "\t\t%" PRIu64 " valid request packet%s\n");
    919 	p(ARP_STAT_RCVMCAST, "\t\t%" PRIu64 " broadcast/multicast packet%s\n");
    920 	p(ARP_STAT_RCVBADPROTO,
    921 	    "\t\t%" PRIu64 " packet%s with unknown protocol type\n");
    922 	p(ARP_STAT_RCVBADLEN,
    923 	    "\t\t%" PRIu64 " packet%s with bad (short) length\n");
    924 	p(ARP_STAT_RCVZEROTPA,
    925 	    "\t\t%" PRIu64 " packet%s with null target IP address\n");
    926 	p(ARP_STAT_RCVZEROSPA,
    927 	    "\t\t%" PRIu64 " packet%s with null source IP address\n");
    928 	ps(ARP_STAT_RCVNOINT,
    929 	    "\t\t%" PRIu64 " could not be mapped to an interface\n");
    930 	p(ARP_STAT_RCVLOCALSHA,
    931 	    "\t\t%" PRIu64 " packet%s sourced from a local hardware "
    932 	    "address\n");
    933 	p(ARP_STAT_RCVBCASTSHA, "\t\t%" PRIu64 " packet%s with a broadcast "
    934 	    "source hardware address\n");
    935 	p(ARP_STAT_RCVLOCALSPA,
    936 	    "\t\t%" PRIu64 " duplicate%s for a local IP address\n");
    937 	p(ARP_STAT_RCVOVERPERM,
    938 	    "\t\t%" PRIu64 " attempt%s to overwrite a static entry\n");
    939 	p(ARP_STAT_RCVOVERINT,
    940 	    "\t\t%" PRIu64 " packet%s received on wrong interface\n");
    941 	p(ARP_STAT_RCVOVER, "\t\t%" PRIu64 " entry%s overwritten\n");
    942 	p(ARP_STAT_RCVLENCHG,
    943 	    "\t\t%" PRIu64 " change%s in hardware address length\n");
    944 
    945 	p(ARP_STAT_DFRTOTAL,
    946 	    "\t%" PRIu64 " packet%s deferred pending ARP resolution\n");
    947 	ps(ARP_STAT_DFRSENT, "\t\t%" PRIu64 " sent\n");
    948 	ps(ARP_STAT_DFRDROPPED, "\t\t%" PRIu64 " dropped\n");
    949 
    950 	p(ARP_STAT_ALLOCFAIL, "\t%" PRIu64 " failure%s to allocate llinfo\n");
    951 
    952 #undef ps
    953 #undef p
    954 }
    955 
    956 /*
    957  * Pretty print an Internet address (net address + port).
    958  * Take numeric_addr and numeric_port into consideration.
    959  */
    960 void
    961 inetprint(struct in_addr *in, uint16_t port, const char *proto,
    962 	  int port_numeric)
    963 {
    964 	struct servent *sp = 0;
    965 	char line[80], *cp;
    966 	size_t space;
    967 
    968 	(void)snprintf(line, sizeof line, "%.*s.",
    969 	    (Aflag && !numeric_addr) ? 12 : 16, inetname(in));
    970 	cp = strchr(line, '\0');
    971 	if (!port_numeric && port)
    972 		sp = getservbyport((int)port, proto);
    973 	space = sizeof line - (cp-line);
    974 	if (sp || port == 0)
    975 		(void)snprintf(cp, space, "%s", sp ? sp->s_name : "*");
    976 	else
    977 		(void)snprintf(cp, space, "%u", ntohs(port));
    978 	(void)printf(" %-*.*s", width, width, line);
    979 }
    980 
    981 /*
    982  * Construct an Internet address representation.
    983  * If numeric_addr has been supplied, give
    984  * numeric value, otherwise try for symbolic name.
    985  */
    986 char *
    987 inetname(struct in_addr *inp)
    988 {
    989 	char *cp;
    990 	static char line[50];
    991 	struct hostent *hp;
    992 	struct netent *np;
    993 	static char domain[MAXHOSTNAMELEN + 1];
    994 	static int first = 1;
    995 
    996 	if (first && !numeric_addr) {
    997 		first = 0;
    998 		if (gethostname(domain, sizeof domain) == 0) {
    999 			domain[sizeof(domain) - 1] = '\0';
   1000 			if ((cp = strchr(domain, '.')))
   1001 				(void) strlcpy(domain, cp + 1, sizeof(domain));
   1002 			else
   1003 				domain[0] = 0;
   1004 		} else
   1005 			domain[0] = 0;
   1006 	}
   1007 	cp = 0;
   1008 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
   1009 		int net = inet_netof(*inp);
   1010 		int lna = inet_lnaof(*inp);
   1011 
   1012 		if (lna == INADDR_ANY) {
   1013 			np = getnetbyaddr(net, AF_INET);
   1014 			if (np)
   1015 				cp = np->n_name;
   1016 		}
   1017 		if (cp == 0) {
   1018 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
   1019 			if (hp) {
   1020 				if ((cp = strchr(hp->h_name, '.')) &&
   1021 				    !strcmp(cp + 1, domain))
   1022 					*cp = 0;
   1023 				cp = hp->h_name;
   1024 			}
   1025 		}
   1026 	}
   1027 	if (inp->s_addr == INADDR_ANY)
   1028 		strlcpy(line, "*", sizeof line);
   1029 	else if (cp)
   1030 		strlcpy(line, cp, sizeof line);
   1031 	else {
   1032 		inp->s_addr = ntohl(inp->s_addr);
   1033 #define C(x)	((x) & 0xff)
   1034 		(void)snprintf(line, sizeof line, "%u.%u.%u.%u",
   1035 		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
   1036 		    C(inp->s_addr >> 8), C(inp->s_addr));
   1037 #undef C
   1038 	}
   1039 	return line;
   1040 }
   1041 
   1042 /*
   1043  * Dump the contents of a TCP PCB.
   1044  */
   1045 void
   1046 tcp_dump(u_long off, const char *name, u_long pcbaddr)
   1047 {
   1048 	callout_impl_t *ci;
   1049 	struct tcpcb tcpcb;
   1050 	int i, hardticks;
   1051 	struct kinfo_pcb *pcblist;
   1052 	size_t j, len;
   1053 
   1054 	if (use_sysctl)
   1055 		pcblist = getpcblist_sysctl(name, &len);
   1056 	else
   1057 		pcblist = getpcblist_kmem(off, name, &len);
   1058 
   1059 	for (j = 0; j < len; j++)
   1060 		if (pcblist[j].ki_ppcbaddr == pcbaddr)
   1061 			break;
   1062 	free(pcblist);
   1063 
   1064 	if (j == len)
   1065 		errx(1, "0x%lx is not a valid pcb address", pcbaddr);
   1066 
   1067 	kread(pcbaddr, (char *)&tcpcb, sizeof(tcpcb));
   1068 	hardticks = get_hardticks();
   1069 
   1070 	printf("TCP Protocol Control Block at 0x%08lx:\n\n", pcbaddr);
   1071 
   1072 	printf("Timers:\n");
   1073 	for (i = 0; i < TCPT_NTIMERS; i++) {
   1074 		char buf[128];
   1075 		ci = (callout_impl_t *)&tcpcb.t_timer[i];
   1076 		snprintb(buf, sizeof(buf), CALLOUT_FMT, ci->c_flags);
   1077 		printf("\t%s\t%s", tcptimers[i], buf);
   1078 		if (ci->c_flags & CALLOUT_PENDING)
   1079 			printf("\t%d\n", ci->c_time - hardticks);
   1080 		else
   1081 			printf("\n");
   1082 	}
   1083 	printf("\n\n");
   1084 
   1085 	if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
   1086 		printf("State: %d", tcpcb.t_state);
   1087 	else
   1088 		printf("State: %s", tcpstates[tcpcb.t_state]);
   1089 	printf(", flags 0x%x, inpcb 0x%lx\n\n", tcpcb.t_flags,
   1090 	    (u_long)tcpcb.t_inpcb);
   1091 
   1092 	printf("rxtshift %d, rxtcur %d, dupacks %d\n", tcpcb.t_rxtshift,
   1093 	    tcpcb.t_rxtcur, tcpcb.t_dupacks);
   1094 	printf("peermss %u, ourmss %u, segsz %u, segqlen %u\n\n",
   1095 	    tcpcb.t_peermss, tcpcb.t_ourmss, tcpcb.t_segsz, tcpcb.t_segqlen);
   1096 
   1097 	printf("snd_una %u, snd_nxt %u, snd_up %u\n",
   1098 	    tcpcb.snd_una, tcpcb.snd_nxt, tcpcb.snd_up);
   1099 	printf("snd_wl1 %u, snd_wl2 %u, iss %u, snd_wnd %lu\n\n",
   1100 	    tcpcb.snd_wl1, tcpcb.snd_wl2, tcpcb.iss, tcpcb.snd_wnd);
   1101 
   1102 	printf("rcv_wnd %lu, rcv_nxt %u, rcv_up %u, irs %u\n\n",
   1103 	    tcpcb.rcv_wnd, tcpcb.rcv_nxt, tcpcb.rcv_up, tcpcb.irs);
   1104 
   1105 	printf("rcv_adv %u, snd_max %u, snd_cwnd %lu, snd_ssthresh %lu\n",
   1106 	    tcpcb.rcv_adv, tcpcb.snd_max, tcpcb.snd_cwnd, tcpcb.snd_ssthresh);
   1107 
   1108 	printf("rcvtime %u, rtttime %u, rtseq %u, srtt %d, rttvar %d, "
   1109 	    "rttmin %d, max_sndwnd %lu\n\n", tcpcb.t_rcvtime, tcpcb.t_rtttime,
   1110 	    tcpcb.t_rtseq, tcpcb.t_srtt, tcpcb.t_rttvar, tcpcb.t_rttmin,
   1111 	    tcpcb.max_sndwnd);
   1112 
   1113 	printf("oobflags %d, iobc %d, softerror %d\n\n", tcpcb.t_oobflags,
   1114 	    tcpcb.t_iobc, tcpcb.t_softerror);
   1115 
   1116 	printf("snd_scale %d, rcv_scale %d, req_r_scale %d, req_s_scale %d\n",
   1117 	    tcpcb.snd_scale, tcpcb.rcv_scale, tcpcb.request_r_scale,
   1118 	    tcpcb.requested_s_scale);
   1119 	printf("ts_recent %u, ts_regent_age %d, last_ack_sent %u\n",
   1120 	    tcpcb.ts_recent, tcpcb.ts_recent_age, tcpcb.last_ack_sent);
   1121 }
   1122