Home | History | Annotate | Line # | Download | only in netstat
inet.c revision 1.69
      1  1.69  liamjfoy /*	$NetBSD: inet.c,v 1.69 2006/05/18 09:05:51 liamjfoy Exp $	*/
      2  1.14   thorpej 
      3   1.1       cgd /*
      4   1.9   mycroft  * Copyright (c) 1983, 1988, 1993
      5   1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.58       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.23     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.14   thorpej #if 0
     35  1.14   thorpej static char sccsid[] = "from: @(#)inet.c	8.4 (Berkeley) 4/20/94";
     36  1.14   thorpej #else
     37  1.69  liamjfoy __RCSID("$NetBSD: inet.c,v 1.69 2006/05/18 09:05:51 liamjfoy Exp $");
     38  1.14   thorpej #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include <sys/param.h>
     42  1.10       cgd #include <sys/queue.h>
     43   1.1       cgd #include <sys/socket.h>
     44   1.1       cgd #include <sys/socketvar.h>
     45   1.1       cgd #include <sys/mbuf.h>
     46   1.1       cgd #include <sys/protosw.h>
     47  1.65      elad #include <sys/sysctl.h>
     48   1.1       cgd 
     49  1.41     jhawk #include <net/if_arp.h>
     50   1.1       cgd #include <net/route.h>
     51   1.1       cgd #include <netinet/in.h>
     52   1.1       cgd #include <netinet/in_systm.h>
     53   1.1       cgd #include <netinet/ip.h>
     54   1.1       cgd #include <netinet/in_pcb.h>
     55   1.1       cgd #include <netinet/ip_icmp.h>
     56  1.37    itojun 
     57  1.37    itojun #ifdef INET6
     58  1.37    itojun #include <netinet/ip6.h>
     59  1.37    itojun #endif
     60  1.37    itojun 
     61   1.1       cgd #include <netinet/icmp_var.h>
     62   1.6    brezak #include <netinet/igmp_var.h>
     63   1.1       cgd #include <netinet/ip_var.h>
     64  1.62      manu #include <netinet/pim_var.h>
     65   1.1       cgd #include <netinet/tcp.h>
     66   1.1       cgd #include <netinet/tcpip.h>
     67   1.1       cgd #include <netinet/tcp_seq.h>
     68   1.1       cgd #define TCPSTATES
     69   1.1       cgd #include <netinet/tcp_fsm.h>
     70  1.30   thorpej #define	TCPTIMERS
     71   1.1       cgd #include <netinet/tcp_timer.h>
     72   1.1       cgd #include <netinet/tcp_var.h>
     73   1.1       cgd #include <netinet/tcp_debug.h>
     74   1.1       cgd #include <netinet/udp.h>
     75  1.69  liamjfoy #include <netinet/ip_carp.h>
     76   1.1       cgd #include <netinet/udp_var.h>
     77   1.1       cgd 
     78   1.9   mycroft #include <arpa/inet.h>
     79  1.64    rpaulo #include <kvm.h>
     80   1.1       cgd #include <netdb.h>
     81   1.1       cgd #include <stdio.h>
     82   1.1       cgd #include <string.h>
     83   1.9   mycroft #include <unistd.h>
     84  1.66      elad #include <stdlib.h>
     85  1.65      elad #include <err.h>
     86   1.9   mycroft #include "netstat.h"
     87   1.1       cgd 
     88   1.1       cgd struct	inpcb inpcb;
     89   1.1       cgd struct	tcpcb tcpcb;
     90   1.1       cgd struct	socket sockb;
     91   1.1       cgd 
     92   1.9   mycroft char	*inetname __P((struct in_addr *));
     93  1.28     lukem void	inetprint __P((struct in_addr *, u_int16_t, const char *, int));
     94   1.1       cgd 
     95   1.1       cgd /*
     96   1.1       cgd  * Print a summary of connections related to an Internet
     97   1.1       cgd  * protocol.  For TCP, also give state of connection.
     98   1.1       cgd  * Listening processes (aflag) are suppressed unless the
     99   1.1       cgd  * -a (all) flag is specified.
    100   1.1       cgd  */
    101  1.35     lukem static int width;
    102  1.66      elad static int compact;
    103  1.66      elad 
    104  1.66      elad static void
    105  1.66      elad protoprhdr(void)
    106  1.66      elad {
    107  1.66      elad 	printf("Active Internet connections");
    108  1.66      elad 	if (aflag)
    109  1.66      elad 		printf(" (including servers)");
    110  1.66      elad 	putchar('\n');
    111  1.66      elad 	if (Aflag)
    112  1.66      elad 		printf("%-8.8s ", "PCB");
    113  1.66      elad 	printf("%-5.5s %-6.6s %-6.6s %s%-*.*s %-*.*s %s\n",
    114  1.66      elad 		"Proto", "Recv-Q", "Send-Q", compact ? "" : " ",
    115  1.66      elad 		width, width, "Local Address",
    116  1.66      elad 		width, width, "Foreign Address",
    117  1.66      elad 		"State");
    118  1.66      elad }
    119  1.66      elad 
    120  1.66      elad static void
    121  1.67        he protopr0(intptr_t ppcb, u_long rcv_sb_cc, u_long snd_sb_cc,
    122  1.66      elad 	 struct in_addr *laddr, u_int16_t lport,
    123  1.66      elad 	 struct in_addr *faddr, u_int16_t fport,
    124  1.66      elad 	 short t_state, char *name)
    125  1.66      elad {
    126  1.66      elad 	static char *shorttcpstates[] = {
    127  1.66      elad 		"CLOSED",	"LISTEN",	"SYNSEN",	"SYSRCV",
    128  1.66      elad 		"ESTABL",	"CLWAIT",	"FWAIT1",	"CLOSNG",
    129  1.66      elad 		"LASTAK",	"FWAIT2",	"TMWAIT",
    130  1.66      elad 	};
    131  1.66      elad 	int istcp;
    132  1.66      elad 
    133  1.66      elad 	istcp = strcmp(name, "tcp") == 0;
    134  1.66      elad 
    135  1.66      elad 	if (Aflag) {
    136  1.68      elad 		printf("%8" PRIxPTR " ", ppcb);
    137  1.66      elad 	}
    138  1.66      elad 	printf("%-5.5s %6ld %6ld%s", name, rcv_sb_cc, snd_sb_cc,
    139  1.66      elad 	       compact ? "" : " ");
    140  1.66      elad 	if (numeric_port) {
    141  1.66      elad 		inetprint(laddr, lport, name, 1);
    142  1.66      elad 		inetprint(faddr, fport, name, 1);
    143  1.66      elad 	} else if (inpcb.inp_flags & INP_ANONPORT) {
    144  1.66      elad 		inetprint(laddr, lport, name, 1);
    145  1.66      elad 		inetprint(faddr, fport, name, 0);
    146  1.66      elad 	} else {
    147  1.66      elad 		inetprint(laddr, lport, name, 0);
    148  1.66      elad 		inetprint(faddr, fport, name, 0);
    149  1.66      elad 	}
    150  1.66      elad 	if (istcp) {
    151  1.66      elad 		if (t_state < 0 || t_state >= TCP_NSTATES)
    152  1.66      elad 			printf(" %d", t_state);
    153  1.66      elad 		else
    154  1.66      elad 			printf(" %s", compact ? shorttcpstates[t_state] :
    155  1.66      elad 			       tcpstates[t_state]);
    156  1.66      elad 	}
    157  1.66      elad 	putchar('\n');
    158  1.66      elad }
    159  1.35     lukem 
    160   1.9   mycroft void
    161   1.1       cgd protopr(off, name)
    162   1.8       cgd 	u_long off;
    163   1.1       cgd 	char *name;
    164   1.1       cgd {
    165  1.12   mycroft 	struct inpcbtable table;
    166  1.23     lukem 	struct inpcb *head, *next, *prev;
    167  1.13       cgd 	struct inpcb inpcb;
    168  1.66      elad 	int istcp;
    169   1.1       cgd 	static int first = 1;
    170   1.1       cgd 
    171   1.1       cgd 	if (off == 0)
    172   1.1       cgd 		return;
    173   1.1       cgd 	istcp = strcmp(name, "tcp") == 0;
    174  1.12   mycroft 	kread(off, (char *)&table, sizeof table);
    175  1.13       cgd 	prev = head =
    176  1.13       cgd 	    (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue.cqh_first;
    177  1.59    itojun 	next = (struct inpcb *)table.inpt_queue.cqh_first;
    178  1.13       cgd 
    179  1.35     lukem 	compact = 0;
    180  1.35     lukem 	if (Aflag) {
    181  1.46     assar 		if (!numeric_addr)
    182  1.35     lukem 			width = 18;
    183  1.35     lukem 		else {
    184  1.35     lukem 			width = 21;
    185  1.35     lukem 			compact = 1;
    186  1.35     lukem 		}
    187  1.35     lukem 	} else
    188  1.35     lukem 		width = 22;
    189  1.66      elad 
    190  1.66      elad 	if (use_sysctl) {
    191  1.66      elad 		struct kinfo_pcb *pcblist;
    192  1.66      elad 		int mib[8];
    193  1.66      elad 		size_t namelen = 0, size = 0, i;
    194  1.66      elad 		char *mibname = NULL;
    195  1.66      elad 
    196  1.66      elad 		memset(mib, 0, sizeof(mib));
    197  1.66      elad 
    198  1.66      elad 		if (asprintf(&mibname, "net.inet.%s.pcblist", name) == -1)
    199  1.66      elad 			err(1, "asprintf");
    200  1.66      elad 
    201  1.66      elad 		/* get dynamic pcblist node */
    202  1.66      elad 		if (sysctlnametomib(mibname, mib, &namelen) == -1)
    203  1.66      elad 			err(1, "sysctlnametomib");
    204  1.66      elad 
    205  1.66      elad 		if (sysctl(mib, sizeof(mib) / sizeof(*mib), NULL, &size,
    206  1.66      elad 			   NULL, 0) == -1)
    207  1.66      elad 			err(1, "sysctl (query)");
    208  1.66      elad 
    209  1.66      elad 		pcblist = malloc(size);
    210  1.66      elad 		memset(pcblist, 0, size);
    211  1.66      elad 
    212  1.66      elad 	        mib[6] = sizeof(*pcblist);
    213  1.66      elad         	mib[7] = size / sizeof(*pcblist);
    214  1.66      elad 
    215  1.66      elad 		if (sysctl(mib, sizeof(mib) / sizeof(*mib), pcblist,
    216  1.66      elad 			   &size, NULL, 0) == -1)
    217  1.66      elad 			err(1, "sysctl (copy)");
    218  1.66      elad 
    219  1.66      elad 		for (i = 0; i < size / sizeof(*pcblist); i++) {
    220  1.66      elad 			struct sockaddr_in src, dst;
    221  1.66      elad 
    222  1.66      elad 			memcpy(&src, &pcblist[i].ki_s, sizeof(src));
    223  1.66      elad 			memcpy(&dst, &pcblist[i].ki_d, sizeof(dst));
    224  1.66      elad 
    225  1.66      elad 			if (first) {
    226  1.66      elad 				protoprhdr();
    227  1.66      elad 				first = 0;
    228  1.66      elad 			}
    229  1.66      elad 
    230  1.67        he 	                protopr0((intptr_t) pcblist[i].ki_ppcbaddr,
    231  1.66      elad 				 pcblist[i].ki_rcvq, pcblist[i].ki_sndq,
    232  1.66      elad 				 &src.sin_addr, src.sin_port,
    233  1.66      elad 				 &dst.sin_addr, dst.sin_port,
    234  1.66      elad 				 pcblist[i].ki_tstate, name);
    235  1.66      elad 		}
    236  1.66      elad 
    237  1.66      elad 		free(pcblist);
    238  1.66      elad 		return;
    239  1.66      elad 	}
    240  1.66      elad 
    241  1.13       cgd 	while (next != head) {
    242  1.12   mycroft 		kread((u_long)next, (char *)&inpcb, sizeof inpcb);
    243  1.59    itojun 		if ((struct inpcb *)inpcb.inp_queue.cqe_prev != prev) {
    244   1.1       cgd 			printf("???\n");
    245   1.1       cgd 			break;
    246   1.1       cgd 		}
    247  1.13       cgd 		prev = next;
    248  1.59    itojun 		next = (struct inpcb *)inpcb.inp_queue.cqe_next;
    249  1.59    itojun 
    250  1.59    itojun 		if (inpcb.inp_af != AF_INET)
    251  1.59    itojun 			continue;
    252  1.13       cgd 
    253   1.1       cgd 		if (!aflag &&
    254  1.12   mycroft 		    inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
    255   1.1       cgd 			continue;
    256   1.9   mycroft 		kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb));
    257   1.1       cgd 		if (istcp) {
    258   1.9   mycroft 			kread((u_long)inpcb.inp_ppcb,
    259   1.9   mycroft 			    (char *)&tcpcb, sizeof (tcpcb));
    260   1.1       cgd 		}
    261  1.66      elad 
    262   1.1       cgd 		if (first) {
    263  1.66      elad 			protoprhdr();
    264   1.1       cgd 			first = 0;
    265   1.1       cgd 		}
    266  1.66      elad 
    267  1.67        he 		protopr0(istcp ? (intptr_t) inpcb.inp_ppcb : (intptr_t) prev,
    268  1.66      elad 			 sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc,
    269  1.66      elad 			 &inpcb.inp_laddr, inpcb.inp_lport,
    270  1.66      elad 			 &inpcb.inp_faddr, inpcb.inp_fport,
    271  1.66      elad 			 tcpcb.t_state, name);
    272   1.1       cgd 	}
    273   1.1       cgd }
    274   1.1       cgd 
    275   1.1       cgd /*
    276   1.1       cgd  * Dump TCP statistics structure.
    277   1.1       cgd  */
    278   1.9   mycroft void
    279   1.1       cgd tcp_stats(off, name)
    280   1.8       cgd 	u_long off;
    281   1.1       cgd 	char *name;
    282   1.1       cgd {
    283   1.1       cgd 	struct tcpstat tcpstat;
    284   1.1       cgd 
    285  1.65      elad 	if (use_sysctl) {
    286  1.65      elad 		size_t size = sizeof(tcpstat);
    287  1.65      elad 
    288  1.65      elad 		if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &size,
    289  1.65      elad 				 NULL, 0) == -1)
    290  1.65      elad 			err(1, "net.inet.tcp.stats");
    291  1.65      elad 	} else {
    292  1.65      elad 		if (off == 0)
    293  1.65      elad 			return;
    294  1.65      elad 		kread(off, (char *)&tcpstat, sizeof (tcpstat));
    295  1.65      elad 	}
    296  1.65      elad 
    297   1.1       cgd 	printf ("%s:\n", name);
    298   1.9   mycroft 
    299  1.20  christos #define	ps(f, m) if (tcpstat.f || sflag <= 1) \
    300  1.38    bouyer     printf(m, (unsigned long long)tcpstat.f)
    301   1.9   mycroft #define	p(f, m) if (tcpstat.f || sflag <= 1) \
    302  1.38    bouyer     printf(m, (unsigned long long)tcpstat.f, plural(tcpstat.f))
    303   1.9   mycroft #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
    304  1.38    bouyer     printf(m, (unsigned long long)tcpstat.f1, plural(tcpstat.f1), \
    305  1.38    bouyer     (unsigned long long)tcpstat.f2, plural(tcpstat.f2))
    306  1.20  christos #define	p2s(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
    307  1.38    bouyer     printf(m, (unsigned long long)tcpstat.f1, plural(tcpstat.f1), \
    308  1.38    bouyer     (unsigned long long)tcpstat.f2)
    309   1.9   mycroft #define	p3(f, m) if (tcpstat.f || sflag <= 1) \
    310  1.38    bouyer     printf(m, (unsigned long long)tcpstat.f, plurales(tcpstat.f))
    311   1.1       cgd 
    312  1.38    bouyer 	p(tcps_sndtotal, "\t%llu packet%s sent\n");
    313   1.1       cgd 	p2(tcps_sndpack,tcps_sndbyte,
    314  1.38    bouyer 		"\t\t%llu data packet%s (%llu byte%s)\n");
    315   1.1       cgd 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
    316  1.38    bouyer 		"\t\t%llu data packet%s (%llu byte%s) retransmitted\n");
    317  1.20  christos 	p2s(tcps_sndacks, tcps_delack,
    318  1.38    bouyer 		"\t\t%llu ack-only packet%s (%llu delayed)\n");
    319  1.38    bouyer 	p(tcps_sndurg, "\t\t%llu URG only packet%s\n");
    320  1.38    bouyer 	p(tcps_sndprobe, "\t\t%llu window probe packet%s\n");
    321  1.38    bouyer 	p(tcps_sndwinup, "\t\t%llu window update packet%s\n");
    322  1.38    bouyer 	p(tcps_sndctrl, "\t\t%llu control packet%s\n");
    323  1.47   thorpej 	p(tcps_selfquench,
    324  1.47   thorpej 	    "\t\t%llu send attempt%s resulted in self-quench\n");
    325  1.38    bouyer 	p(tcps_rcvtotal, "\t%llu packet%s received\n");
    326  1.38    bouyer 	p2(tcps_rcvackpack, tcps_rcvackbyte,
    327  1.38    bouyer 		"\t\t%llu ack%s (for %llu byte%s)\n");
    328  1.38    bouyer 	p(tcps_rcvdupack, "\t\t%llu duplicate ack%s\n");
    329  1.38    bouyer 	p(tcps_rcvacktoomuch, "\t\t%llu ack%s for unsent data\n");
    330   1.1       cgd 	p2(tcps_rcvpack, tcps_rcvbyte,
    331  1.38    bouyer 		"\t\t%llu packet%s (%llu byte%s) received in-sequence\n");
    332   1.1       cgd 	p2(tcps_rcvduppack, tcps_rcvdupbyte,
    333  1.38    bouyer 		"\t\t%llu completely duplicate packet%s (%llu byte%s)\n");
    334  1.38    bouyer 	p(tcps_pawsdrop, "\t\t%llu old duplicate packet%s\n");
    335   1.1       cgd 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
    336  1.38    bouyer 		"\t\t%llu packet%s with some dup. data (%llu byte%s duped)\n");
    337   1.1       cgd 	p2(tcps_rcvoopack, tcps_rcvoobyte,
    338  1.38    bouyer 		"\t\t%llu out-of-order packet%s (%llu byte%s)\n");
    339   1.1       cgd 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
    340  1.38    bouyer 		"\t\t%llu packet%s (%llu byte%s) of data after window\n");
    341  1.38    bouyer 	p(tcps_rcvwinprobe, "\t\t%llu window probe%s\n");
    342  1.38    bouyer 	p(tcps_rcvwinupd, "\t\t%llu window update packet%s\n");
    343  1.38    bouyer 	p(tcps_rcvafterclose, "\t\t%llu packet%s received after close\n");
    344  1.38    bouyer 	p(tcps_rcvbadsum, "\t\t%llu discarded for bad checksum%s\n");
    345  1.38    bouyer 	p(tcps_rcvbadoff, "\t\t%llu discarded for bad header offset field%s\n");
    346  1.38    bouyer 	ps(tcps_rcvshort, "\t\t%llu discarded because packet too short\n");
    347  1.38    bouyer 	p(tcps_connattempt, "\t%llu connection request%s\n");
    348  1.38    bouyer 	p(tcps_accepts, "\t%llu connection accept%s\n");
    349  1.38    bouyer 	p(tcps_connects,
    350  1.38    bouyer 		"\t%llu connection%s established (including accepts)\n");
    351   1.1       cgd 	p2(tcps_closed, tcps_drops,
    352  1.38    bouyer 		"\t%llu connection%s closed (including %llu drop%s)\n");
    353  1.38    bouyer 	p(tcps_conndrops, "\t%llu embryonic connection%s dropped\n");
    354  1.57        he 	p(tcps_delayed_free, "\t%llu delayed free%s of tcpcb\n");
    355   1.1       cgd 	p2(tcps_rttupdated, tcps_segstimed,
    356  1.38    bouyer 		"\t%llu segment%s updated rtt (of %llu attempt%s)\n");
    357  1.38    bouyer 	p(tcps_rexmttimeo, "\t%llu retransmit timeout%s\n");
    358  1.38    bouyer 	p(tcps_timeoutdrop,
    359  1.38    bouyer 		"\t\t%llu connection%s dropped by rexmit timeout\n");
    360  1.27   thorpej 	p2(tcps_persisttimeo, tcps_persistdrops,
    361  1.38    bouyer 	   "\t%llu persist timeout%s (resulting in %llu dropped "
    362  1.38    bouyer 		"connection%s)\n");
    363  1.38    bouyer 	p(tcps_keeptimeo, "\t%llu keepalive timeout%s\n");
    364  1.38    bouyer 	p(tcps_keepprobe, "\t\t%llu keepalive probe%s sent\n");
    365  1.38    bouyer 	p(tcps_keepdrops, "\t\t%llu connection%s dropped by keepalive\n");
    366  1.38    bouyer 	p(tcps_predack, "\t%llu correct ACK header prediction%s\n");
    367  1.38    bouyer 	p(tcps_preddat, "\t%llu correct data packet header prediction%s\n");
    368  1.38    bouyer 	p3(tcps_pcbhashmiss, "\t%llu PCB hash miss%s\n");
    369  1.38    bouyer 	ps(tcps_noport, "\t%llu dropped due to no socket\n");
    370  1.38    bouyer 	p(tcps_connsdrained, "\t%llu connection%s drained due to memory "
    371  1.38    bouyer 		"shortage\n");
    372  1.52    itojun 	p(tcps_pmtublackhole, "\t%llu PMTUD blackhole%s detected\n");
    373  1.38    bouyer 
    374  1.38    bouyer 	p(tcps_badsyn, "\t%llu bad connection attempt%s\n");
    375  1.38    bouyer 	ps(tcps_sc_added, "\t%llu SYN cache entries added\n");
    376  1.38    bouyer 	p(tcps_sc_collisions, "\t\t%llu hash collision%s\n");
    377  1.38    bouyer 	ps(tcps_sc_completed, "\t\t%llu completed\n");
    378  1.38    bouyer 	ps(tcps_sc_aborted, "\t\t%llu aborted (no space to build PCB)\n");
    379  1.38    bouyer 	ps(tcps_sc_timed_out, "\t\t%llu timed out\n");
    380  1.38    bouyer 	ps(tcps_sc_overflowed, "\t\t%llu dropped due to overflow\n");
    381  1.38    bouyer 	ps(tcps_sc_bucketoverflow, "\t\t%llu dropped due to bucket overflow\n");
    382  1.38    bouyer 	ps(tcps_sc_reset, "\t\t%llu dropped due to RST\n");
    383  1.38    bouyer 	ps(tcps_sc_unreach, "\t\t%llu dropped due to ICMP unreachable\n");
    384  1.57        he 	ps(tcps_sc_delayed_free, "\t\t%llu delayed free of SYN cache "
    385  1.57        he 		"entries\n");
    386  1.38    bouyer 	p(tcps_sc_retransmitted, "\t%llu SYN,ACK%s retransmitted\n");
    387  1.38    bouyer 	p(tcps_sc_dupesyn, "\t%llu duplicate SYN%s received for entries "
    388  1.38    bouyer 		"already in the cache\n");
    389  1.38    bouyer 	p(tcps_sc_dropped, "\t%llu SYN%s dropped (no route or no space)\n");
    390  1.61    itojun 	p(tcps_badsig, "\t%llu packet%s with bad signature\n");
    391  1.61    itojun 	p(tcps_goodsig, "\t%llu packet%s with good signature\n");
    392  1.15   mycroft 
    393   1.1       cgd #undef p
    394  1.20  christos #undef ps
    395   1.1       cgd #undef p2
    396  1.20  christos #undef p2s
    397   1.9   mycroft #undef p3
    398   1.1       cgd }
    399   1.1       cgd 
    400   1.1       cgd /*
    401   1.1       cgd  * Dump UDP statistics structure.
    402   1.1       cgd  */
    403   1.9   mycroft void
    404   1.1       cgd udp_stats(off, name)
    405   1.8       cgd 	u_long off;
    406   1.1       cgd 	char *name;
    407   1.1       cgd {
    408   1.1       cgd 	struct udpstat udpstat;
    409  1.38    bouyer 	u_quad_t delivered;
    410   1.1       cgd 
    411  1.65      elad 	if (use_sysctl) {
    412  1.65      elad 		size_t size = sizeof(udpstat);
    413  1.65      elad 
    414  1.65      elad 		if (sysctlbyname("net.inet.udp.stats", &udpstat, &size,
    415  1.65      elad 				 NULL, 0) == -1)
    416  1.65      elad 			err(1, "net.inet.udp.stats");
    417  1.65      elad 	} else {
    418  1.65      elad 		if (off == 0)
    419  1.65      elad 			return;
    420  1.65      elad 		kread(off, (char *)&udpstat, sizeof (udpstat));
    421  1.65      elad 	}
    422  1.65      elad 
    423  1.65      elad 	printf ("%s:\n", name);
    424  1.15   mycroft 
    425  1.20  christos #define	ps(f, m) if (udpstat.f || sflag <= 1) \
    426  1.38    bouyer     printf(m, (unsigned long long)udpstat.f)
    427   1.9   mycroft #define	p(f, m) if (udpstat.f || sflag <= 1) \
    428  1.38    bouyer     printf(m, (unsigned long long)udpstat.f, plural(udpstat.f))
    429  1.15   mycroft #define	p3(f, m) if (udpstat.f || sflag <= 1) \
    430  1.38    bouyer     printf(m, (unsigned long long)udpstat.f, plurales(udpstat.f))
    431  1.15   mycroft 
    432  1.38    bouyer 	p(udps_ipackets, "\t%llu datagram%s received\n");
    433  1.38    bouyer 	ps(udps_hdrops, "\t%llu with incomplete header\n");
    434  1.38    bouyer 	ps(udps_badlen, "\t%llu with bad data length field\n");
    435  1.38    bouyer 	ps(udps_badsum, "\t%llu with bad checksum\n");
    436  1.38    bouyer 	ps(udps_noport, "\t%llu dropped due to no socket\n");
    437  1.38    bouyer 	p(udps_noportbcast, "\t%llu broadcast/multicast datagram%s dropped due to no socket\n");
    438  1.38    bouyer 	ps(udps_fullsock, "\t%llu dropped due to full socket buffers\n");
    439   1.9   mycroft 	delivered = udpstat.udps_ipackets -
    440   1.9   mycroft 		    udpstat.udps_hdrops -
    441   1.9   mycroft 		    udpstat.udps_badlen -
    442   1.9   mycroft 		    udpstat.udps_badsum -
    443   1.9   mycroft 		    udpstat.udps_noport -
    444   1.9   mycroft 		    udpstat.udps_noportbcast -
    445   1.9   mycroft 		    udpstat.udps_fullsock;
    446   1.9   mycroft 	if (delivered || sflag <= 1)
    447  1.38    bouyer 		printf("\t%llu delivered\n", (unsigned long long)delivered);
    448  1.38    bouyer 	p3(udps_pcbhashmiss, "\t%llu PCB hash miss%s\n");
    449  1.38    bouyer 	p(udps_opackets, "\t%llu datagram%s output\n");
    450  1.15   mycroft 
    451  1.20  christos #undef ps
    452   1.9   mycroft #undef p
    453  1.15   mycroft #undef p3
    454   1.1       cgd }
    455   1.1       cgd 
    456   1.1       cgd /*
    457   1.1       cgd  * Dump IP statistics structure.
    458   1.1       cgd  */
    459   1.9   mycroft void
    460   1.1       cgd ip_stats(off, name)
    461   1.8       cgd 	u_long off;
    462   1.1       cgd 	char *name;
    463   1.1       cgd {
    464   1.1       cgd 	struct ipstat ipstat;
    465   1.1       cgd 
    466  1.65      elad 	if (use_sysctl) {
    467  1.65      elad 		size_t size = sizeof(ipstat);
    468  1.65      elad 
    469  1.65      elad 		if (sysctlbyname("net.inet.ip.stats", &ipstat, &size,
    470  1.65      elad 				 NULL, 0) == -1)
    471  1.65      elad 			err(1, "net.inet.ip.stats");
    472  1.65      elad 	} else {
    473  1.65      elad 		if (off == 0)
    474  1.65      elad 			return;
    475  1.65      elad 		kread(off, (char *)&ipstat, sizeof (ipstat));
    476  1.65      elad 	}
    477  1.65      elad 
    478   1.9   mycroft 	printf("%s:\n", name);
    479   1.9   mycroft 
    480  1.20  christos #define	ps(f, m) if (ipstat.f || sflag <= 1) \
    481  1.38    bouyer     printf(m, (unsigned long long)ipstat.f)
    482   1.9   mycroft #define	p(f, m) if (ipstat.f || sflag <= 1) \
    483  1.38    bouyer     printf(m, (unsigned long long)ipstat.f, plural(ipstat.f))
    484   1.9   mycroft 
    485  1.38    bouyer 	p(ips_total, "\t%llu total packet%s received\n");
    486  1.38    bouyer 	p(ips_badsum, "\t%llu bad header checksum%s\n");
    487  1.38    bouyer 	ps(ips_toosmall, "\t%llu with size smaller than minimum\n");
    488  1.38    bouyer 	ps(ips_tooshort, "\t%llu with data size < data length\n");
    489  1.38    bouyer 	ps(ips_toolong, "\t%llu with length > max ip packet size\n");
    490  1.38    bouyer 	ps(ips_badhlen, "\t%llu with header length < data size\n");
    491  1.38    bouyer 	ps(ips_badlen, "\t%llu with data length < header length\n");
    492  1.38    bouyer 	ps(ips_badoptions, "\t%llu with bad options\n");
    493  1.38    bouyer 	ps(ips_badvers, "\t%llu with incorrect version number\n");
    494  1.40     enami 	p(ips_fragments, "\t%llu fragment%s received\n");
    495  1.38    bouyer 	p(ips_fragdropped, "\t%llu fragment%s dropped (dup or out of space)\n");
    496  1.60     enami 	p(ips_rcvmemdrop, "\t%llu fragment%s dropped (out of ipqent)\n");
    497  1.38    bouyer 	p(ips_badfrags, "\t%llu malformed fragment%s dropped\n");
    498  1.38    bouyer 	p(ips_fragtimeout, "\t%llu fragment%s dropped after timeout\n");
    499  1.38    bouyer 	p(ips_reassembled, "\t%llu packet%s reassembled ok\n");
    500  1.38    bouyer 	p(ips_delivered, "\t%llu packet%s for this host\n");
    501  1.38    bouyer 	p(ips_noproto, "\t%llu packet%s for unknown/unsupported protocol\n");
    502  1.38    bouyer 	p(ips_forward, "\t%llu packet%s forwarded");
    503  1.38    bouyer 	p(ips_fastforward, " (%llu packet%s fast forwarded)");
    504  1.29      matt 	if (ipstat.ips_forward || sflag <= 1)
    505  1.29      matt 		putchar('\n');
    506  1.38    bouyer 	p(ips_cantforward, "\t%llu packet%s not forwardable\n");
    507  1.38    bouyer 	p(ips_redirectsent, "\t%llu redirect%s sent\n");
    508  1.60     enami 	p(ips_nogif, "\t%llu packet%s no matching gif found\n");
    509  1.38    bouyer 	p(ips_localout, "\t%llu packet%s sent from this host\n");
    510  1.38    bouyer 	p(ips_rawout, "\t%llu packet%s sent with fabricated ip header\n");
    511  1.38    bouyer 	p(ips_odropped, "\t%llu output packet%s dropped due to no bufs, etc.\n");
    512  1.38    bouyer 	p(ips_noroute, "\t%llu output packet%s discarded due to no route\n");
    513  1.38    bouyer 	p(ips_fragmented, "\t%llu output datagram%s fragmented\n");
    514  1.38    bouyer 	p(ips_ofragments, "\t%llu fragment%s created\n");
    515  1.38    bouyer 	p(ips_cantfrag, "\t%llu datagram%s that can't be fragmented\n");
    516  1.43    itojun 	p(ips_badaddr, "\t%llu datagram%s with bad address in header\n");
    517  1.20  christos #undef ps
    518   1.9   mycroft #undef p
    519   1.1       cgd }
    520   1.1       cgd 
    521   1.1       cgd static	char *icmpnames[] = {
    522   1.1       cgd 	"echo reply",
    523   1.1       cgd 	"#1",
    524   1.1       cgd 	"#2",
    525   1.1       cgd 	"destination unreachable",
    526   1.1       cgd 	"source quench",
    527   1.1       cgd 	"routing redirect",
    528  1.44    itojun 	"alternate host address",
    529   1.1       cgd 	"#7",
    530   1.1       cgd 	"echo",
    531  1.44    itojun 	"router advertisement",
    532  1.44    itojun 	"router solicitation",
    533   1.1       cgd 	"time exceeded",
    534   1.1       cgd 	"parameter problem",
    535   1.1       cgd 	"time stamp",
    536   1.1       cgd 	"time stamp reply",
    537   1.1       cgd 	"information request",
    538   1.1       cgd 	"information request reply",
    539   1.1       cgd 	"address mask request",
    540   1.1       cgd 	"address mask reply",
    541   1.1       cgd };
    542   1.1       cgd 
    543   1.1       cgd /*
    544   1.1       cgd  * Dump ICMP statistics.
    545   1.1       cgd  */
    546   1.9   mycroft void
    547   1.1       cgd icmp_stats(off, name)
    548   1.8       cgd 	u_long off;
    549   1.1       cgd 	char *name;
    550   1.1       cgd {
    551   1.1       cgd 	struct icmpstat icmpstat;
    552  1.23     lukem 	int i, first;
    553   1.1       cgd 
    554  1.65      elad 	if (use_sysctl) {
    555  1.65      elad 		size_t size = sizeof(icmpstat);
    556  1.65      elad 
    557  1.65      elad 		if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &size,
    558  1.65      elad 				 NULL, 0) == -1)
    559  1.65      elad 			err(1, "net.inet.icmp.stats");
    560  1.65      elad 	} else {
    561  1.65      elad 		if (off == 0)
    562  1.65      elad 			return;
    563  1.65      elad 		kread(off, (char *)&icmpstat, sizeof (icmpstat));
    564  1.65      elad 	}
    565  1.65      elad 
    566   1.9   mycroft 	printf("%s:\n", name);
    567   1.9   mycroft 
    568   1.9   mycroft #define	p(f, m) if (icmpstat.f || sflag <= 1) \
    569  1.38    bouyer     printf(m, (unsigned long long)icmpstat.f, plural(icmpstat.f))
    570   1.9   mycroft 
    571  1.38    bouyer 	p(icps_error, "\t%llu call%s to icmp_error\n");
    572   1.9   mycroft 	p(icps_oldicmp,
    573  1.38    bouyer 	    "\t%llu error%s not generated because old message was icmp\n");
    574   1.1       cgd 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
    575   1.1       cgd 		if (icmpstat.icps_outhist[i] != 0) {
    576   1.1       cgd 			if (first) {
    577   1.1       cgd 				printf("\tOutput histogram:\n");
    578   1.1       cgd 				first = 0;
    579   1.1       cgd 			}
    580  1.38    bouyer 			printf("\t\t%s: %llu\n", icmpnames[i],
    581  1.38    bouyer 				(unsigned long long)icmpstat.icps_outhist[i]);
    582   1.1       cgd 		}
    583  1.38    bouyer 	p(icps_badcode, "\t%llu message%s with bad code fields\n");
    584  1.38    bouyer 	p(icps_tooshort, "\t%llu message%s < minimum length\n");
    585  1.38    bouyer 	p(icps_checksum, "\t%llu bad checksum%s\n");
    586  1.38    bouyer 	p(icps_badlen, "\t%llu message%s with bad length\n");
    587   1.1       cgd 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
    588   1.1       cgd 		if (icmpstat.icps_inhist[i] != 0) {
    589   1.1       cgd 			if (first) {
    590   1.1       cgd 				printf("\tInput histogram:\n");
    591   1.1       cgd 				first = 0;
    592   1.1       cgd 			}
    593  1.38    bouyer 			printf("\t\t%s: %llu\n", icmpnames[i],
    594  1.38    bouyer 				(unsigned long long)icmpstat.icps_inhist[i]);
    595   1.1       cgd 		}
    596  1.38    bouyer 	p(icps_reflect, "\t%llu message response%s generated\n");
    597  1.42    itojun 	p(icps_pmtuchg, "\t%llu path MTU change%s\n");
    598   1.9   mycroft #undef p
    599   1.6    brezak }
    600   1.6    brezak 
    601   1.6    brezak /*
    602   1.9   mycroft  * Dump IGMP statistics structure.
    603   1.6    brezak  */
    604   1.6    brezak void
    605   1.6    brezak igmp_stats(off, name)
    606   1.8       cgd 	u_long off;
    607   1.6    brezak 	char *name;
    608   1.6    brezak {
    609   1.6    brezak 	struct igmpstat igmpstat;
    610   1.6    brezak 
    611   1.6    brezak 	if (off == 0)
    612   1.6    brezak 		return;
    613   1.9   mycroft 	kread(off, (char *)&igmpstat, sizeof (igmpstat));
    614   1.9   mycroft 	printf("%s:\n", name);
    615   1.9   mycroft 
    616   1.9   mycroft #define	p(f, m) if (igmpstat.f || sflag <= 1) \
    617  1.38    bouyer     printf(m, (unsigned long long)igmpstat.f, plural(igmpstat.f))
    618   1.9   mycroft #define	py(f, m) if (igmpstat.f || sflag <= 1) \
    619  1.38    bouyer     printf(m, (unsigned long long)igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
    620  1.38    bouyer 	p(igps_rcv_total, "\t%llu message%s received\n");
    621  1.38    bouyer         p(igps_rcv_tooshort, "\t%llu message%s received with too few bytes\n");
    622  1.38    bouyer         p(igps_rcv_badsum, "\t%llu message%s received with bad checksum\n");
    623  1.38    bouyer         py(igps_rcv_queries, "\t%llu membership quer%s received\n");
    624  1.38    bouyer         py(igps_rcv_badqueries, "\t%llu membership quer%s received with invalid field(s)\n");
    625  1.38    bouyer         p(igps_rcv_reports, "\t%llu membership report%s received\n");
    626  1.38    bouyer         p(igps_rcv_badreports, "\t%llu membership report%s received with invalid field(s)\n");
    627  1.38    bouyer         p(igps_rcv_ourreports, "\t%llu membership report%s received for groups to which we belong\n");
    628  1.38    bouyer         p(igps_snd_reports, "\t%llu membership report%s sent\n");
    629   1.9   mycroft #undef p
    630   1.9   mycroft #undef py
    631  1.41     jhawk }
    632  1.41     jhawk 
    633  1.41     jhawk /*
    634  1.69  liamjfoy  * Dump CARP statistics structure.
    635  1.69  liamjfoy  */
    636  1.69  liamjfoy void
    637  1.69  liamjfoy carp_stats(u_long off, char *name)
    638  1.69  liamjfoy {
    639  1.69  liamjfoy 	struct carpstats carpstat;
    640  1.69  liamjfoy 
    641  1.69  liamjfoy 	if (use_sysctl) {
    642  1.69  liamjfoy 		size_t size = sizeof(carpstat);
    643  1.69  liamjfoy 
    644  1.69  liamjfoy 		if (sysctlbyname("net.inet.carp.stats", &carpstat, &size,
    645  1.69  liamjfoy 				 NULL, 0) == -1)
    646  1.69  liamjfoy 			err(1, "net.inet.carp.stats");
    647  1.69  liamjfoy 	} else {
    648  1.69  liamjfoy 		if (off == 0)
    649  1.69  liamjfoy 			return;
    650  1.69  liamjfoy 		kread(off, (char *)&carpstat, sizeof(carpstat));
    651  1.69  liamjfoy 	}
    652  1.69  liamjfoy 
    653  1.69  liamjfoy 	printf("%s:\n", name);
    654  1.69  liamjfoy 
    655  1.69  liamjfoy #define p(f, m) if (carpstat.f || sflag <= 1) \
    656  1.69  liamjfoy 	printf(m, carpstat.f, plural(carpstat.f))
    657  1.69  liamjfoy #define p2(f, m) if (carpstat.f || sflag <= 1) \
    658  1.69  liamjfoy 	printf(m, carpstat.f)
    659  1.69  liamjfoy 
    660  1.69  liamjfoy 	p(carps_ipackets, "\t%llu packet%s received (IPv4)\n");
    661  1.69  liamjfoy 	p(carps_ipackets6, "\t%llu packet%s received (IPv6)\n");
    662  1.69  liamjfoy 	p(carps_badif,
    663  1.69  liamjfoy 	    "\t\t%llu packet%s discarded for bad interface\n");
    664  1.69  liamjfoy 	p(carps_badttl,
    665  1.69  liamjfoy 	    "\t\t%llu packet%s discarded for wrong TTL\n");
    666  1.69  liamjfoy 	p(carps_hdrops, "\t\t%llu packet%s shorter than header\n");
    667  1.69  liamjfoy 	p(carps_badsum, "\t\t%llu packet%s discarded for bad checksum\n");
    668  1.69  liamjfoy 	p(carps_badver,
    669  1.69  liamjfoy 	    "\t\t%llu packet%s discarded with a bad version\n");
    670  1.69  liamjfoy 	p2(carps_badlen,
    671  1.69  liamjfoy 	    "\t\t%llu discarded because packet was too short\n");
    672  1.69  liamjfoy 	p(carps_badauth,
    673  1.69  liamjfoy 	    "\t\t%llu packet%s discarded for bad authentication\n");
    674  1.69  liamjfoy 	p(carps_badvhid, "\t\t%llu packet%s discarded for bad vhid\n");
    675  1.69  liamjfoy 	p(carps_badaddrs,
    676  1.69  liamjfoy 	    "\t\t%llu packet%s discarded because of a bad address list\n");
    677  1.69  liamjfoy 	p(carps_opackets, "\t%llu packet%s sent (IPv4)\n");
    678  1.69  liamjfoy 	p(carps_opackets6, "\t%llu packet%s sent (IPv6)\n");
    679  1.69  liamjfoy 	p2(carps_onomem,
    680  1.69  liamjfoy 	    "\t\t%llu send failed due to mbuf memory error\n");
    681  1.69  liamjfoy #undef p
    682  1.69  liamjfoy #undef p2
    683  1.69  liamjfoy }
    684  1.69  liamjfoy 
    685  1.69  liamjfoy /*
    686  1.62      manu  * Dump PIM statistics structure.
    687  1.62      manu  */
    688  1.62      manu void
    689  1.62      manu pim_stats(off, name)
    690  1.62      manu 	u_long off;
    691  1.62      manu 	char *name;
    692  1.62      manu {
    693  1.62      manu 	struct pimstat pimstat;
    694  1.62      manu 
    695  1.62      manu 	if (off == 0)
    696  1.62      manu 		return;
    697  1.62      manu 	if (kread(off, (char *)&pimstat, sizeof (pimstat)) != 0) {
    698  1.62      manu 		/* XXX: PIM is probably not enabled in the kernel */
    699  1.62      manu 		return;
    700  1.62      manu 	}
    701  1.62      manu 
    702  1.62      manu 	printf("%s:\n", name);
    703  1.62      manu 
    704  1.62      manu #define	p(f, m) if (pimstat.f || sflag <= 1) \
    705  1.63    martin 	printf(m, (unsigned long long)pimstat.f, plural(pimstat.f))
    706  1.62      manu 
    707  1.62      manu 	p(pims_rcv_total_msgs, "\t%llu message%s received\n");
    708  1.62      manu 	p(pims_rcv_total_bytes, "\t%llu byte%s received\n");
    709  1.62      manu 	p(pims_rcv_tooshort, "\t%llu message%s received with too few bytes\n");
    710  1.62      manu         p(pims_rcv_badsum, "\t%llu message%s received with bad checksum\n");
    711  1.62      manu 	p(pims_rcv_badversion, "\t%llu message%s received with bad version\n");
    712  1.62      manu 	p(pims_rcv_registers_msgs, "\t%llu data register message%s received\n");
    713  1.62      manu 	p(pims_rcv_registers_bytes, "\t%llu data register byte%s received\n");
    714  1.62      manu 	p(pims_rcv_registers_wrongiif, "\t%llu data register message%s received on wrong iif\n");
    715  1.62      manu 	p(pims_rcv_badregisters, "\t%llu bad register%s received\n");
    716  1.62      manu 	p(pims_snd_registers_msgs, "\t%llu data register message%s sent\n");
    717  1.62      manu 	p(pims_snd_registers_bytes, "\t%llu data register byte%s sent\n");
    718  1.62      manu #undef p
    719  1.62      manu }
    720  1.62      manu 
    721  1.62      manu /*
    722  1.41     jhawk  * Dump the ARP statistics structure.
    723  1.41     jhawk  */
    724  1.41     jhawk void
    725  1.41     jhawk arp_stats(off, name)
    726  1.41     jhawk 	u_long off;
    727  1.41     jhawk 	char *name;
    728  1.41     jhawk {
    729  1.41     jhawk 	struct arpstat arpstat;
    730  1.41     jhawk 
    731  1.41     jhawk 	if (off == 0)
    732  1.41     jhawk 		return;
    733  1.41     jhawk 	kread(off, (char *)&arpstat, sizeof (arpstat));
    734  1.41     jhawk 	printf("%s:\n", name);
    735  1.41     jhawk 
    736  1.41     jhawk #define	ps(f, m) if (arpstat.f || sflag <= 1) \
    737  1.41     jhawk     printf(m, (unsigned long long)arpstat.f)
    738  1.41     jhawk #define	p(f, m) if (arpstat.f || sflag <= 1) \
    739  1.41     jhawk     printf(m, (unsigned long long)arpstat.f, plural(arpstat.f))
    740  1.41     jhawk 
    741  1.41     jhawk 	p(as_sndtotal, "\t%llu packet%s sent\n");
    742  1.41     jhawk 	p(as_sndreply, "\t\t%llu reply packet%s\n");
    743  1.41     jhawk 	p(as_sndrequest, "\t\t%llu request packet%s\n");
    744  1.41     jhawk 
    745  1.41     jhawk 	p(as_rcvtotal, "\t%llu packet%s received\n");
    746  1.41     jhawk 	p(as_rcvreply, "\t\t%llu reply packet%s\n");
    747  1.41     jhawk 	p(as_rcvrequest, "\t\t%llu valid request packet%s\n");
    748  1.41     jhawk 	p(as_rcvmcast, "\t\t%llu broadcast/multicast packet%s\n");
    749  1.41     jhawk 	p(as_rcvbadproto, "\t\t%llu packet%s with unknown protocol type\n");
    750  1.41     jhawk 	p(as_rcvbadlen, "\t\t%llu packet%s with bad (short) length\n");
    751  1.41     jhawk 	p(as_rcvzerotpa, "\t\t%llu packet%s with null target IP address\n");
    752  1.41     jhawk 	p(as_rcvzerospa, "\t\t%llu packet%s with null source IP address\n");
    753  1.41     jhawk 	ps(as_rcvnoint, "\t\t%llu could not be mapped to an interface\n");
    754  1.41     jhawk 	p(as_rcvlocalsha, "\t\t%llu packet%s sourced from a local hardware "
    755  1.41     jhawk 	    "address\n");
    756  1.41     jhawk 	p(as_rcvbcastsha, "\t\t%llu packet%s with a broadcast "
    757  1.41     jhawk 	    "source hardware address\n");
    758  1.41     jhawk 	p(as_rcvlocalspa, "\t\t%llu duplicate%s for a local IP address\n");
    759  1.41     jhawk 	p(as_rcvoverperm, "\t\t%llu attempt%s to overwrite a static entry\n");
    760  1.41     jhawk 	p(as_rcvoverint, "\t\t%llu packet%s received on wrong interface\n");
    761  1.41     jhawk 	p(as_rcvover, "\t\t%llu entry%s overwritten\n");
    762  1.41     jhawk 	p(as_rcvlenchg, "\t\t%llu change%s in hardware address length\n");
    763  1.41     jhawk 
    764  1.41     jhawk 	p(as_dfrtotal, "\t%llu packet%s deferred pending ARP resolution\n");
    765  1.41     jhawk 	ps(as_dfrsent, "\t\t%llu sent\n");
    766  1.41     jhawk 	ps(as_dfrdropped, "\t\t%llu dropped\n");
    767  1.41     jhawk 
    768  1.41     jhawk 	p(as_allocfail, "\t%llu failure%s to allocate llinfo\n");
    769  1.41     jhawk 
    770  1.41     jhawk #undef ps
    771  1.41     jhawk #undef p
    772   1.1       cgd }
    773   1.1       cgd 
    774   1.1       cgd /*
    775   1.1       cgd  * Pretty print an Internet address (net address + port).
    776  1.46     assar  * Take numeric_addr and numeric_port into consideration.
    777   1.1       cgd  */
    778   1.9   mycroft void
    779  1.46     assar inetprint(in, port, proto, numeric_port)
    780  1.23     lukem 	struct in_addr *in;
    781  1.28     lukem 	u_int16_t port;
    782  1.28     lukem 	const char *proto;
    783  1.46     assar 	int numeric_port;
    784   1.1       cgd {
    785   1.1       cgd 	struct servent *sp = 0;
    786   1.9   mycroft 	char line[80], *cp;
    787  1.33  sommerfe 	size_t space;
    788   1.1       cgd 
    789  1.32       mrg 	(void)snprintf(line, sizeof line, "%.*s.",
    790  1.46     assar 	    (Aflag && !numeric_addr) ? 12 : 16, inetname(in));
    791  1.23     lukem 	cp = strchr(line, '\0');
    792  1.46     assar 	if (!numeric_port && port)
    793   1.1       cgd 		sp = getservbyport((int)port, proto);
    794  1.33  sommerfe 	space = sizeof line - (cp-line);
    795   1.1       cgd 	if (sp || port == 0)
    796  1.55  jdolecek 		(void)snprintf(cp, space, "%s", sp ? sp->s_name : "*");
    797   1.1       cgd 	else
    798  1.33  sommerfe 		(void)snprintf(cp, space, "%u", ntohs(port));
    799  1.32       mrg 	(void)printf(" %-*.*s", width, width, line);
    800   1.1       cgd }
    801   1.1       cgd 
    802   1.1       cgd /*
    803   1.1       cgd  * Construct an Internet address representation.
    804  1.46     assar  * If numeric_addr has been supplied, give
    805   1.1       cgd  * numeric value, otherwise try for symbolic name.
    806   1.1       cgd  */
    807   1.1       cgd char *
    808   1.9   mycroft inetname(inp)
    809   1.9   mycroft 	struct in_addr *inp;
    810   1.1       cgd {
    811  1.23     lukem 	char *cp;
    812   1.1       cgd 	static char line[50];
    813   1.1       cgd 	struct hostent *hp;
    814   1.1       cgd 	struct netent *np;
    815   1.1       cgd 	static char domain[MAXHOSTNAMELEN + 1];
    816   1.1       cgd 	static int first = 1;
    817   1.1       cgd 
    818  1.46     assar 	if (first && !numeric_addr) {
    819   1.1       cgd 		first = 0;
    820  1.31       mrg 		if (gethostname(domain, sizeof domain) == 0) {
    821  1.31       mrg 			domain[sizeof(domain) - 1] = '\0';
    822  1.31       mrg 			if ((cp = strchr(domain, '.')))
    823  1.45    itojun 				(void) strlcpy(domain, cp + 1, sizeof(domain));
    824  1.31       mrg 			else
    825  1.31       mrg 				domain[0] = 0;
    826  1.31       mrg 		} else
    827   1.1       cgd 			domain[0] = 0;
    828   1.1       cgd 	}
    829   1.1       cgd 	cp = 0;
    830  1.46     assar 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
    831   1.9   mycroft 		int net = inet_netof(*inp);
    832   1.9   mycroft 		int lna = inet_lnaof(*inp);
    833   1.1       cgd 
    834   1.1       cgd 		if (lna == INADDR_ANY) {
    835   1.1       cgd 			np = getnetbyaddr(net, AF_INET);
    836   1.1       cgd 			if (np)
    837   1.1       cgd 				cp = np->n_name;
    838   1.1       cgd 		}
    839   1.1       cgd 		if (cp == 0) {
    840   1.9   mycroft 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
    841   1.1       cgd 			if (hp) {
    842  1.23     lukem 				if ((cp = strchr(hp->h_name, '.')) &&
    843   1.1       cgd 				    !strcmp(cp + 1, domain))
    844   1.1       cgd 					*cp = 0;
    845   1.1       cgd 				cp = hp->h_name;
    846   1.1       cgd 			}
    847   1.1       cgd 		}
    848   1.1       cgd 	}
    849   1.9   mycroft 	if (inp->s_addr == INADDR_ANY)
    850  1.56    itojun 		strlcpy(line, "*", sizeof line);
    851   1.1       cgd 	else if (cp)
    852  1.56    itojun 		strlcpy(line, cp, sizeof line);
    853   1.1       cgd 	else {
    854   1.9   mycroft 		inp->s_addr = ntohl(inp->s_addr);
    855   1.1       cgd #define C(x)	((x) & 0xff)
    856  1.32       mrg 		(void)snprintf(line, sizeof line, "%u.%u.%u.%u",
    857  1.32       mrg 		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
    858  1.32       mrg 		    C(inp->s_addr >> 8), C(inp->s_addr));
    859  1.32       mrg #undef C
    860   1.1       cgd 	}
    861   1.1       cgd 	return (line);
    862  1.30   thorpej }
    863  1.30   thorpej 
    864  1.30   thorpej /*
    865  1.30   thorpej  * Dump the contents of a TCP PCB.
    866  1.30   thorpej  */
    867  1.30   thorpej void
    868  1.30   thorpej tcp_dump(pcbaddr)
    869  1.30   thorpej 	u_long pcbaddr;
    870  1.30   thorpej {
    871  1.30   thorpej 	struct tcpcb tcpcb;
    872  1.54   thorpej 	int i, hardticks;
    873  1.30   thorpej 
    874  1.30   thorpej 	kread(pcbaddr, (char *)&tcpcb, sizeof(tcpcb));
    875  1.54   thorpej 	hardticks = get_hardticks();
    876  1.30   thorpej 
    877  1.30   thorpej 	printf("TCP Protocol Control Block at 0x%08lx:\n\n", pcbaddr);
    878  1.30   thorpej 
    879  1.30   thorpej 	printf("Timers:\n");
    880  1.48   thorpej 	for (i = 0; i < TCPT_NTIMERS; i++) {
    881  1.54   thorpej 		printf("\t%s: %d", tcptimers[i],
    882  1.53   thorpej 		    (tcpcb.t_timer[i].c_flags & CALLOUT_PENDING) ?
    883  1.54   thorpej 		    tcpcb.t_timer[i].c_time - hardticks : 0);
    884  1.48   thorpej 	}
    885  1.30   thorpej 	printf("\n\n");
    886  1.30   thorpej 
    887  1.30   thorpej 	if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
    888  1.30   thorpej 		printf("State: %d", tcpcb.t_state);
    889  1.30   thorpej 	else
    890  1.30   thorpej 		printf("State: %s", tcpstates[tcpcb.t_state]);
    891  1.49   thorpej 	printf(", flags 0x%x, inpcb 0x%lx, in6pcb 0x%lx\n\n", tcpcb.t_flags,
    892  1.49   thorpej 	    (u_long)tcpcb.t_inpcb, (u_long)tcpcb.t_in6pcb);
    893  1.30   thorpej 
    894  1.30   thorpej 	printf("rxtshift %d, rxtcur %d, dupacks %d\n", tcpcb.t_rxtshift,
    895  1.30   thorpej 	    tcpcb.t_rxtcur, tcpcb.t_dupacks);
    896  1.30   thorpej 	printf("peermss %u, ourmss %u, segsz %u\n\n", tcpcb.t_peermss,
    897  1.30   thorpej 	    tcpcb.t_ourmss, tcpcb.t_segsz);
    898  1.30   thorpej 
    899  1.30   thorpej 	printf("snd_una %u, snd_nxt %u, snd_up %u\n",
    900  1.30   thorpej 	    tcpcb.snd_una, tcpcb.snd_nxt, tcpcb.snd_up);
    901  1.30   thorpej 	printf("snd_wl1 %u, snd_wl2 %u, iss %u, snd_wnd %lu\n\n",
    902  1.30   thorpej 	    tcpcb.snd_wl1, tcpcb.snd_wl2, tcpcb.iss, tcpcb.snd_wnd);
    903  1.30   thorpej 
    904  1.30   thorpej 	printf("rcv_wnd %lu, rcv_nxt %u, rcv_up %u, irs %u\n\n",
    905  1.30   thorpej 	    tcpcb.rcv_wnd, tcpcb.rcv_nxt, tcpcb.rcv_up, tcpcb.irs);
    906  1.30   thorpej 
    907  1.30   thorpej 	printf("rcv_adv %u, snd_max %u, snd_cwnd %lu, snd_ssthresh %lu\n",
    908  1.30   thorpej 	    tcpcb.rcv_adv, tcpcb.snd_max, tcpcb.snd_cwnd, tcpcb.snd_ssthresh);
    909  1.30   thorpej 
    910  1.47   thorpej 	printf("rcvtime %u, rtttime %u, rtseq %u, srtt %d, rttvar %d, "
    911  1.47   thorpej 	    "rttmin %d, max_sndwnd %lu\n\n", tcpcb.t_rcvtime, tcpcb.t_rtttime,
    912  1.47   thorpej 	    tcpcb.t_rtseq, tcpcb.t_srtt, tcpcb.t_rttvar, tcpcb.t_rttmin,
    913  1.47   thorpej 	    tcpcb.max_sndwnd);
    914  1.30   thorpej 
    915  1.30   thorpej 	printf("oobflags %d, iobc %d, softerror %d\n\n", tcpcb.t_oobflags,
    916  1.30   thorpej 	    tcpcb.t_iobc, tcpcb.t_softerror);
    917  1.30   thorpej 
    918  1.30   thorpej 	printf("snd_scale %d, rcv_scale %d, req_r_scale %d, req_s_scale %d\n",
    919  1.30   thorpej 	    tcpcb.snd_scale, tcpcb.rcv_scale, tcpcb.request_r_scale,
    920  1.30   thorpej 	    tcpcb.requested_s_scale);
    921  1.30   thorpej 	printf("ts_recent %u, ts_regent_age %d, last_ack_sent %u\n",
    922  1.30   thorpej 	    tcpcb.ts_recent, tcpcb.ts_recent_age, tcpcb.last_ack_sent);
    923   1.1       cgd }
    924