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