Home | History | Annotate | Line # | Download | only in netstat
inet.c revision 1.42
      1  1.42    itojun /*	$NetBSD: inet.c,v 1.42 2000/10/18 19:20:03 itojun 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.23     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.14   thorpej #if 0
     39  1.14   thorpej static char sccsid[] = "from: @(#)inet.c	8.4 (Berkeley) 4/20/94";
     40  1.14   thorpej #else
     41  1.42    itojun __RCSID("$NetBSD: inet.c,v 1.42 2000/10/18 19:20:03 itojun Exp $");
     42  1.14   thorpej #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/param.h>
     46  1.10       cgd #include <sys/queue.h>
     47   1.1       cgd #include <sys/socket.h>
     48   1.1       cgd #include <sys/socketvar.h>
     49   1.1       cgd #include <sys/mbuf.h>
     50   1.1       cgd #include <sys/protosw.h>
     51   1.1       cgd 
     52  1.41     jhawk #include <net/if_arp.h>
     53   1.1       cgd #include <net/route.h>
     54   1.1       cgd #include <netinet/in.h>
     55   1.1       cgd #include <netinet/in_systm.h>
     56   1.1       cgd #include <netinet/ip.h>
     57   1.1       cgd #include <netinet/in_pcb.h>
     58   1.1       cgd #include <netinet/ip_icmp.h>
     59  1.37    itojun 
     60  1.37    itojun #ifdef INET6
     61  1.37    itojun #include <netinet/ip6.h>
     62  1.37    itojun #endif
     63  1.37    itojun 
     64   1.1       cgd #include <netinet/icmp_var.h>
     65   1.6    brezak #include <netinet/igmp_var.h>
     66   1.1       cgd #include <netinet/ip_var.h>
     67   1.1       cgd #include <netinet/tcp.h>
     68   1.1       cgd #include <netinet/tcpip.h>
     69   1.1       cgd #include <netinet/tcp_seq.h>
     70   1.1       cgd #define TCPSTATES
     71   1.1       cgd #include <netinet/tcp_fsm.h>
     72  1.30   thorpej #define	TCPTIMERS
     73   1.1       cgd #include <netinet/tcp_timer.h>
     74   1.1       cgd #include <netinet/tcp_var.h>
     75   1.1       cgd #include <netinet/tcp_debug.h>
     76   1.1       cgd #include <netinet/udp.h>
     77   1.1       cgd #include <netinet/udp_var.h>
     78   1.1       cgd 
     79   1.9   mycroft #include <arpa/inet.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.9   mycroft #include "netstat.h"
     85   1.1       cgd 
     86   1.1       cgd struct	inpcb inpcb;
     87   1.1       cgd struct	tcpcb tcpcb;
     88   1.1       cgd struct	socket sockb;
     89   1.1       cgd 
     90   1.9   mycroft char	*inetname __P((struct in_addr *));
     91  1.28     lukem void	inetprint __P((struct in_addr *, u_int16_t, const char *, int));
     92   1.1       cgd 
     93   1.1       cgd /*
     94   1.1       cgd  * Print a summary of connections related to an Internet
     95   1.1       cgd  * protocol.  For TCP, also give state of connection.
     96   1.1       cgd  * Listening processes (aflag) are suppressed unless the
     97   1.1       cgd  * -a (all) flag is specified.
     98   1.1       cgd  */
     99  1.35     lukem static int width;
    100  1.35     lukem 
    101   1.9   mycroft void
    102   1.1       cgd protopr(off, name)
    103   1.8       cgd 	u_long off;
    104   1.1       cgd 	char *name;
    105   1.1       cgd {
    106  1.12   mycroft 	struct inpcbtable table;
    107  1.23     lukem 	struct inpcb *head, *next, *prev;
    108  1.13       cgd 	struct inpcb inpcb;
    109  1.35     lukem 	int istcp, compact;
    110   1.1       cgd 	static int first = 1;
    111  1.35     lukem 	static char *shorttcpstates[] = {
    112  1.35     lukem 		"CLOSED",	"LISTEN",	"SYNSEN",	"SYSRCV",
    113  1.35     lukem 		"ESTABL",	"CLWAIT",	"FWAIT1",	"CLOSNG",
    114  1.35     lukem 		"LASTAK",	"FWAIT2",	"TMWAIT",
    115  1.35     lukem 	};
    116   1.1       cgd 
    117   1.1       cgd 	if (off == 0)
    118   1.1       cgd 		return;
    119   1.1       cgd 	istcp = strcmp(name, "tcp") == 0;
    120  1.12   mycroft 	kread(off, (char *)&table, sizeof table);
    121  1.13       cgd 	prev = head =
    122  1.13       cgd 	    (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue.cqh_first;
    123  1.13       cgd 	next = table.inpt_queue.cqh_first;
    124  1.13       cgd 
    125  1.35     lukem 	compact = 0;
    126  1.35     lukem 	if (Aflag) {
    127  1.35     lukem 		if (!nflag)
    128  1.35     lukem 			width = 18;
    129  1.35     lukem 		else {
    130  1.35     lukem 			width = 21;
    131  1.35     lukem 			compact = 1;
    132  1.35     lukem 		}
    133  1.35     lukem 	} else
    134  1.35     lukem 		width = 22;
    135  1.13       cgd 	while (next != head) {
    136  1.12   mycroft 		kread((u_long)next, (char *)&inpcb, sizeof inpcb);
    137  1.13       cgd 		if (inpcb.inp_queue.cqe_prev != prev) {
    138   1.1       cgd 			printf("???\n");
    139   1.1       cgd 			break;
    140   1.1       cgd 		}
    141  1.13       cgd 		prev = next;
    142  1.13       cgd 		next = inpcb.inp_queue.cqe_next;
    143  1.13       cgd 
    144   1.1       cgd 		if (!aflag &&
    145  1.12   mycroft 		    inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
    146   1.1       cgd 			continue;
    147   1.9   mycroft 		kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb));
    148   1.1       cgd 		if (istcp) {
    149   1.9   mycroft 			kread((u_long)inpcb.inp_ppcb,
    150   1.9   mycroft 			    (char *)&tcpcb, sizeof (tcpcb));
    151   1.1       cgd 		}
    152   1.1       cgd 		if (first) {
    153   1.1       cgd 			printf("Active Internet connections");
    154   1.1       cgd 			if (aflag)
    155   1.1       cgd 				printf(" (including servers)");
    156   1.1       cgd 			putchar('\n');
    157   1.1       cgd 			if (Aflag)
    158   1.1       cgd 				printf("%-8.8s ", "PCB");
    159  1.35     lukem 			printf("%-5.5s %-6.6s %-6.6s %s%-*.*s %-*.*s %s\n",
    160   1.1       cgd 				"Proto", "Recv-Q", "Send-Q",
    161  1.35     lukem 				compact ? "" : " ",
    162  1.35     lukem 				width, width, "Local Address",
    163  1.35     lukem 				width, width, "Foreign Address", "State");
    164   1.1       cgd 			first = 0;
    165   1.1       cgd 		}
    166  1.34      ross 		if (Aflag) {
    167   1.1       cgd 			if (istcp)
    168  1.21  christos 				printf("%8lx ", (u_long) inpcb.inp_ppcb);
    169   1.1       cgd 			else
    170  1.21  christos 				printf("%8lx ", (u_long) prev);
    171  1.34      ross 		}
    172  1.35     lukem 		printf("%-5.5s %6ld %6ld%s", name, sockb.so_rcv.sb_cc,
    173  1.35     lukem 			sockb.so_snd.sb_cc, compact ? "" : " ");
    174  1.28     lukem 		if (nflag) {
    175  1.28     lukem 			inetprint(&inpcb.inp_laddr, inpcb.inp_lport, name, 1);
    176  1.28     lukem 			inetprint(&inpcb.inp_faddr, inpcb.inp_fport, name, 1);
    177  1.28     lukem 		} else if (inpcb.inp_flags & INP_ANONPORT) {
    178  1.28     lukem 			inetprint(&inpcb.inp_laddr, inpcb.inp_lport, name, 1);
    179  1.28     lukem 			inetprint(&inpcb.inp_faddr, inpcb.inp_fport, name, 0);
    180  1.28     lukem 		} else {
    181  1.28     lukem 			inetprint(&inpcb.inp_laddr, inpcb.inp_lport, name, 0);
    182  1.28     lukem 			inetprint(&inpcb.inp_faddr, inpcb.inp_fport, name,
    183  1.28     lukem 			    inpcb.inp_lport != inpcb.inp_fport);
    184  1.28     lukem 		}
    185   1.1       cgd 		if (istcp) {
    186   1.1       cgd 			if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
    187   1.1       cgd 				printf(" %d", tcpcb.t_state);
    188   1.1       cgd 			else
    189  1.35     lukem 				printf(" %s", compact ?
    190  1.35     lukem 				    shorttcpstates[tcpcb.t_state] :
    191  1.35     lukem 				    tcpstates[tcpcb.t_state]);
    192   1.1       cgd 		}
    193   1.1       cgd 		putchar('\n');
    194   1.1       cgd 	}
    195   1.1       cgd }
    196   1.1       cgd 
    197   1.1       cgd /*
    198   1.1       cgd  * Dump TCP statistics structure.
    199   1.1       cgd  */
    200   1.9   mycroft void
    201   1.1       cgd tcp_stats(off, name)
    202   1.8       cgd 	u_long off;
    203   1.1       cgd 	char *name;
    204   1.1       cgd {
    205   1.1       cgd 	struct tcpstat tcpstat;
    206   1.1       cgd 
    207   1.1       cgd 	if (off == 0)
    208   1.1       cgd 		return;
    209   1.1       cgd 	printf ("%s:\n", name);
    210   1.9   mycroft 	kread(off, (char *)&tcpstat, sizeof (tcpstat));
    211   1.9   mycroft 
    212  1.20  christos #define	ps(f, m) if (tcpstat.f || sflag <= 1) \
    213  1.38    bouyer     printf(m, (unsigned long long)tcpstat.f)
    214   1.9   mycroft #define	p(f, m) if (tcpstat.f || sflag <= 1) \
    215  1.38    bouyer     printf(m, (unsigned long long)tcpstat.f, plural(tcpstat.f))
    216   1.9   mycroft #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
    217  1.38    bouyer     printf(m, (unsigned long long)tcpstat.f1, plural(tcpstat.f1), \
    218  1.38    bouyer     (unsigned long long)tcpstat.f2, plural(tcpstat.f2))
    219  1.20  christos #define	p2s(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
    220  1.38    bouyer     printf(m, (unsigned long long)tcpstat.f1, plural(tcpstat.f1), \
    221  1.38    bouyer     (unsigned long long)tcpstat.f2)
    222   1.9   mycroft #define	p3(f, m) if (tcpstat.f || sflag <= 1) \
    223  1.38    bouyer     printf(m, (unsigned long long)tcpstat.f, plurales(tcpstat.f))
    224   1.1       cgd 
    225  1.38    bouyer 	p(tcps_sndtotal, "\t%llu packet%s sent\n");
    226   1.1       cgd 	p2(tcps_sndpack,tcps_sndbyte,
    227  1.38    bouyer 		"\t\t%llu data packet%s (%llu byte%s)\n");
    228   1.1       cgd 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
    229  1.38    bouyer 		"\t\t%llu data packet%s (%llu byte%s) retransmitted\n");
    230  1.20  christos 	p2s(tcps_sndacks, tcps_delack,
    231  1.38    bouyer 		"\t\t%llu ack-only packet%s (%llu delayed)\n");
    232  1.38    bouyer 	p(tcps_sndurg, "\t\t%llu URG only packet%s\n");
    233  1.38    bouyer 	p(tcps_sndprobe, "\t\t%llu window probe packet%s\n");
    234  1.38    bouyer 	p(tcps_sndwinup, "\t\t%llu window update packet%s\n");
    235  1.38    bouyer 	p(tcps_sndctrl, "\t\t%llu control packet%s\n");
    236  1.38    bouyer 	p(tcps_rcvtotal, "\t%llu packet%s received\n");
    237  1.38    bouyer 	p2(tcps_rcvackpack, tcps_rcvackbyte,
    238  1.38    bouyer 		"\t\t%llu ack%s (for %llu byte%s)\n");
    239  1.38    bouyer 	p(tcps_rcvdupack, "\t\t%llu duplicate ack%s\n");
    240  1.38    bouyer 	p(tcps_rcvacktoomuch, "\t\t%llu ack%s for unsent data\n");
    241   1.1       cgd 	p2(tcps_rcvpack, tcps_rcvbyte,
    242  1.38    bouyer 		"\t\t%llu packet%s (%llu byte%s) received in-sequence\n");
    243   1.1       cgd 	p2(tcps_rcvduppack, tcps_rcvdupbyte,
    244  1.38    bouyer 		"\t\t%llu completely duplicate packet%s (%llu byte%s)\n");
    245  1.38    bouyer 	p(tcps_pawsdrop, "\t\t%llu old duplicate packet%s\n");
    246   1.1       cgd 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
    247  1.38    bouyer 		"\t\t%llu packet%s with some dup. data (%llu byte%s duped)\n");
    248   1.1       cgd 	p2(tcps_rcvoopack, tcps_rcvoobyte,
    249  1.38    bouyer 		"\t\t%llu out-of-order packet%s (%llu byte%s)\n");
    250   1.1       cgd 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
    251  1.38    bouyer 		"\t\t%llu packet%s (%llu byte%s) of data after window\n");
    252  1.38    bouyer 	p(tcps_rcvwinprobe, "\t\t%llu window probe%s\n");
    253  1.38    bouyer 	p(tcps_rcvwinupd, "\t\t%llu window update packet%s\n");
    254  1.38    bouyer 	p(tcps_rcvafterclose, "\t\t%llu packet%s received after close\n");
    255  1.38    bouyer 	p(tcps_rcvbadsum, "\t\t%llu discarded for bad checksum%s\n");
    256  1.38    bouyer 	p(tcps_rcvbadoff, "\t\t%llu discarded for bad header offset field%s\n");
    257  1.38    bouyer 	ps(tcps_rcvshort, "\t\t%llu discarded because packet too short\n");
    258  1.38    bouyer 	p(tcps_connattempt, "\t%llu connection request%s\n");
    259  1.38    bouyer 	p(tcps_accepts, "\t%llu connection accept%s\n");
    260  1.38    bouyer 	p(tcps_connects,
    261  1.38    bouyer 		"\t%llu connection%s established (including accepts)\n");
    262   1.1       cgd 	p2(tcps_closed, tcps_drops,
    263  1.38    bouyer 		"\t%llu connection%s closed (including %llu drop%s)\n");
    264  1.38    bouyer 	p(tcps_conndrops, "\t%llu embryonic connection%s dropped\n");
    265   1.1       cgd 	p2(tcps_rttupdated, tcps_segstimed,
    266  1.38    bouyer 		"\t%llu segment%s updated rtt (of %llu attempt%s)\n");
    267  1.38    bouyer 	p(tcps_rexmttimeo, "\t%llu retransmit timeout%s\n");
    268  1.38    bouyer 	p(tcps_timeoutdrop,
    269  1.38    bouyer 		"\t\t%llu connection%s dropped by rexmit timeout\n");
    270  1.27   thorpej 	p2(tcps_persisttimeo, tcps_persistdrops,
    271  1.38    bouyer 	   "\t%llu persist timeout%s (resulting in %llu dropped "
    272  1.38    bouyer 		"connection%s)\n");
    273  1.38    bouyer 	p(tcps_keeptimeo, "\t%llu keepalive timeout%s\n");
    274  1.38    bouyer 	p(tcps_keepprobe, "\t\t%llu keepalive probe%s sent\n");
    275  1.38    bouyer 	p(tcps_keepdrops, "\t\t%llu connection%s dropped by keepalive\n");
    276  1.38    bouyer 	p(tcps_predack, "\t%llu correct ACK header prediction%s\n");
    277  1.38    bouyer 	p(tcps_preddat, "\t%llu correct data packet header prediction%s\n");
    278  1.38    bouyer 	p3(tcps_pcbhashmiss, "\t%llu PCB hash miss%s\n");
    279  1.38    bouyer 	ps(tcps_noport, "\t%llu dropped due to no socket\n");
    280  1.38    bouyer 	p(tcps_connsdrained, "\t%llu connection%s drained due to memory "
    281  1.38    bouyer 		"shortage\n");
    282  1.38    bouyer 
    283  1.38    bouyer 	p(tcps_badsyn, "\t%llu bad connection attempt%s\n");
    284  1.38    bouyer 	ps(tcps_sc_added, "\t%llu SYN cache entries added\n");
    285  1.38    bouyer 	p(tcps_sc_collisions, "\t\t%llu hash collision%s\n");
    286  1.38    bouyer 	ps(tcps_sc_completed, "\t\t%llu completed\n");
    287  1.38    bouyer 	ps(tcps_sc_aborted, "\t\t%llu aborted (no space to build PCB)\n");
    288  1.38    bouyer 	ps(tcps_sc_timed_out, "\t\t%llu timed out\n");
    289  1.38    bouyer 	ps(tcps_sc_overflowed, "\t\t%llu dropped due to overflow\n");
    290  1.38    bouyer 	ps(tcps_sc_bucketoverflow, "\t\t%llu dropped due to bucket overflow\n");
    291  1.38    bouyer 	ps(tcps_sc_reset, "\t\t%llu dropped due to RST\n");
    292  1.38    bouyer 	ps(tcps_sc_unreach, "\t\t%llu dropped due to ICMP unreachable\n");
    293  1.38    bouyer 	p(tcps_sc_retransmitted, "\t%llu SYN,ACK%s retransmitted\n");
    294  1.38    bouyer 	p(tcps_sc_dupesyn, "\t%llu duplicate SYN%s received for entries "
    295  1.38    bouyer 		"already in the cache\n");
    296  1.38    bouyer 	p(tcps_sc_dropped, "\t%llu SYN%s dropped (no route or no space)\n");
    297  1.15   mycroft 
    298   1.1       cgd #undef p
    299  1.20  christos #undef ps
    300   1.1       cgd #undef p2
    301  1.20  christos #undef p2s
    302   1.9   mycroft #undef p3
    303   1.1       cgd }
    304   1.1       cgd 
    305   1.1       cgd /*
    306   1.1       cgd  * Dump UDP statistics structure.
    307   1.1       cgd  */
    308   1.9   mycroft void
    309   1.1       cgd udp_stats(off, name)
    310   1.8       cgd 	u_long off;
    311   1.1       cgd 	char *name;
    312   1.1       cgd {
    313   1.1       cgd 	struct udpstat udpstat;
    314  1.38    bouyer 	u_quad_t delivered;
    315   1.1       cgd 
    316   1.1       cgd 	if (off == 0)
    317   1.1       cgd 		return;
    318  1.15   mycroft 	printf("%s:\n", name);
    319   1.9   mycroft 	kread(off, (char *)&udpstat, sizeof (udpstat));
    320  1.15   mycroft 
    321  1.20  christos #define	ps(f, m) if (udpstat.f || sflag <= 1) \
    322  1.38    bouyer     printf(m, (unsigned long long)udpstat.f)
    323   1.9   mycroft #define	p(f, m) if (udpstat.f || sflag <= 1) \
    324  1.38    bouyer     printf(m, (unsigned long long)udpstat.f, plural(udpstat.f))
    325  1.15   mycroft #define	p3(f, m) if (udpstat.f || sflag <= 1) \
    326  1.38    bouyer     printf(m, (unsigned long long)udpstat.f, plurales(udpstat.f))
    327  1.15   mycroft 
    328  1.38    bouyer 	p(udps_ipackets, "\t%llu datagram%s received\n");
    329  1.38    bouyer 	ps(udps_hdrops, "\t%llu with incomplete header\n");
    330  1.38    bouyer 	ps(udps_badlen, "\t%llu with bad data length field\n");
    331  1.38    bouyer 	ps(udps_badsum, "\t%llu with bad checksum\n");
    332  1.38    bouyer 	ps(udps_noport, "\t%llu dropped due to no socket\n");
    333  1.38    bouyer 	p(udps_noportbcast, "\t%llu broadcast/multicast datagram%s dropped due to no socket\n");
    334  1.38    bouyer 	ps(udps_fullsock, "\t%llu dropped due to full socket buffers\n");
    335   1.9   mycroft 	delivered = udpstat.udps_ipackets -
    336   1.9   mycroft 		    udpstat.udps_hdrops -
    337   1.9   mycroft 		    udpstat.udps_badlen -
    338   1.9   mycroft 		    udpstat.udps_badsum -
    339   1.9   mycroft 		    udpstat.udps_noport -
    340   1.9   mycroft 		    udpstat.udps_noportbcast -
    341   1.9   mycroft 		    udpstat.udps_fullsock;
    342   1.9   mycroft 	if (delivered || sflag <= 1)
    343  1.38    bouyer 		printf("\t%llu delivered\n", (unsigned long long)delivered);
    344  1.38    bouyer 	p3(udps_pcbhashmiss, "\t%llu PCB hash miss%s\n");
    345  1.38    bouyer 	p(udps_opackets, "\t%llu datagram%s output\n");
    346  1.15   mycroft 
    347  1.20  christos #undef ps
    348   1.9   mycroft #undef p
    349  1.15   mycroft #undef p3
    350   1.1       cgd }
    351   1.1       cgd 
    352   1.1       cgd /*
    353   1.1       cgd  * Dump IP statistics structure.
    354   1.1       cgd  */
    355   1.9   mycroft void
    356   1.1       cgd ip_stats(off, name)
    357   1.8       cgd 	u_long off;
    358   1.1       cgd 	char *name;
    359   1.1       cgd {
    360   1.1       cgd 	struct ipstat ipstat;
    361   1.1       cgd 
    362   1.1       cgd 	if (off == 0)
    363   1.1       cgd 		return;
    364   1.9   mycroft 	kread(off, (char *)&ipstat, sizeof (ipstat));
    365   1.9   mycroft 	printf("%s:\n", name);
    366   1.9   mycroft 
    367  1.20  christos #define	ps(f, m) if (ipstat.f || sflag <= 1) \
    368  1.38    bouyer     printf(m, (unsigned long long)ipstat.f)
    369   1.9   mycroft #define	p(f, m) if (ipstat.f || sflag <= 1) \
    370  1.38    bouyer     printf(m, (unsigned long long)ipstat.f, plural(ipstat.f))
    371   1.9   mycroft 
    372  1.38    bouyer 	p(ips_total, "\t%llu total packet%s received\n");
    373  1.38    bouyer 	p(ips_badsum, "\t%llu bad header checksum%s\n");
    374  1.38    bouyer 	ps(ips_toosmall, "\t%llu with size smaller than minimum\n");
    375  1.38    bouyer 	ps(ips_tooshort, "\t%llu with data size < data length\n");
    376  1.38    bouyer 	ps(ips_toolong, "\t%llu with length > max ip packet size\n");
    377  1.38    bouyer 	ps(ips_badhlen, "\t%llu with header length < data size\n");
    378  1.38    bouyer 	ps(ips_badlen, "\t%llu with data length < header length\n");
    379  1.38    bouyer 	ps(ips_badoptions, "\t%llu with bad options\n");
    380  1.38    bouyer 	ps(ips_badvers, "\t%llu with incorrect version number\n");
    381  1.40     enami 	p(ips_fragments, "\t%llu fragment%s received\n");
    382  1.38    bouyer 	p(ips_fragdropped, "\t%llu fragment%s dropped (dup or out of space)\n");
    383  1.38    bouyer 	p(ips_badfrags, "\t%llu malformed fragment%s dropped\n");
    384  1.38    bouyer 	p(ips_fragtimeout, "\t%llu fragment%s dropped after timeout\n");
    385  1.38    bouyer 	p(ips_reassembled, "\t%llu packet%s reassembled ok\n");
    386  1.38    bouyer 	p(ips_delivered, "\t%llu packet%s for this host\n");
    387  1.38    bouyer 	p(ips_noproto, "\t%llu packet%s for unknown/unsupported protocol\n");
    388  1.38    bouyer 	p(ips_forward, "\t%llu packet%s forwarded");
    389  1.38    bouyer 	p(ips_fastforward, " (%llu packet%s fast forwarded)");
    390  1.29      matt 	if (ipstat.ips_forward || sflag <= 1)
    391  1.29      matt 		putchar('\n');
    392  1.38    bouyer 	p(ips_cantforward, "\t%llu packet%s not forwardable\n");
    393  1.38    bouyer 	p(ips_redirectsent, "\t%llu redirect%s sent\n");
    394  1.38    bouyer 	p(ips_localout, "\t%llu packet%s sent from this host\n");
    395  1.38    bouyer 	p(ips_rawout, "\t%llu packet%s sent with fabricated ip header\n");
    396  1.38    bouyer 	p(ips_odropped, "\t%llu output packet%s dropped due to no bufs, etc.\n");
    397  1.38    bouyer 	p(ips_noroute, "\t%llu output packet%s discarded due to no route\n");
    398  1.38    bouyer 	p(ips_fragmented, "\t%llu output datagram%s fragmented\n");
    399  1.38    bouyer 	p(ips_ofragments, "\t%llu fragment%s created\n");
    400  1.38    bouyer 	p(ips_cantfrag, "\t%llu datagram%s that can't be fragmented\n");
    401  1.20  christos #undef ps
    402   1.9   mycroft #undef p
    403   1.1       cgd }
    404   1.1       cgd 
    405   1.1       cgd static	char *icmpnames[] = {
    406   1.1       cgd 	"echo reply",
    407   1.1       cgd 	"#1",
    408   1.1       cgd 	"#2",
    409   1.1       cgd 	"destination unreachable",
    410   1.1       cgd 	"source quench",
    411   1.1       cgd 	"routing redirect",
    412   1.1       cgd 	"#6",
    413   1.1       cgd 	"#7",
    414   1.1       cgd 	"echo",
    415   1.1       cgd 	"#9",
    416   1.1       cgd 	"#10",
    417   1.1       cgd 	"time exceeded",
    418   1.1       cgd 	"parameter problem",
    419   1.1       cgd 	"time stamp",
    420   1.1       cgd 	"time stamp reply",
    421   1.1       cgd 	"information request",
    422   1.1       cgd 	"information request reply",
    423   1.1       cgd 	"address mask request",
    424   1.1       cgd 	"address mask reply",
    425   1.1       cgd };
    426   1.1       cgd 
    427   1.1       cgd /*
    428   1.1       cgd  * Dump ICMP statistics.
    429   1.1       cgd  */
    430   1.9   mycroft void
    431   1.1       cgd icmp_stats(off, name)
    432   1.8       cgd 	u_long off;
    433   1.1       cgd 	char *name;
    434   1.1       cgd {
    435   1.1       cgd 	struct icmpstat icmpstat;
    436  1.23     lukem 	int i, first;
    437   1.1       cgd 
    438   1.1       cgd 	if (off == 0)
    439   1.1       cgd 		return;
    440   1.9   mycroft 	kread(off, (char *)&icmpstat, sizeof (icmpstat));
    441   1.9   mycroft 	printf("%s:\n", name);
    442   1.9   mycroft 
    443   1.9   mycroft #define	p(f, m) if (icmpstat.f || sflag <= 1) \
    444  1.38    bouyer     printf(m, (unsigned long long)icmpstat.f, plural(icmpstat.f))
    445   1.9   mycroft 
    446  1.38    bouyer 	p(icps_error, "\t%llu call%s to icmp_error\n");
    447   1.9   mycroft 	p(icps_oldicmp,
    448  1.38    bouyer 	    "\t%llu error%s not generated because old message was icmp\n");
    449   1.1       cgd 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
    450   1.1       cgd 		if (icmpstat.icps_outhist[i] != 0) {
    451   1.1       cgd 			if (first) {
    452   1.1       cgd 				printf("\tOutput histogram:\n");
    453   1.1       cgd 				first = 0;
    454   1.1       cgd 			}
    455  1.38    bouyer 			printf("\t\t%s: %llu\n", icmpnames[i],
    456  1.38    bouyer 				(unsigned long long)icmpstat.icps_outhist[i]);
    457   1.1       cgd 		}
    458  1.38    bouyer 	p(icps_badcode, "\t%llu message%s with bad code fields\n");
    459  1.38    bouyer 	p(icps_tooshort, "\t%llu message%s < minimum length\n");
    460  1.38    bouyer 	p(icps_checksum, "\t%llu bad checksum%s\n");
    461  1.38    bouyer 	p(icps_badlen, "\t%llu message%s with bad length\n");
    462   1.1       cgd 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
    463   1.1       cgd 		if (icmpstat.icps_inhist[i] != 0) {
    464   1.1       cgd 			if (first) {
    465   1.1       cgd 				printf("\tInput histogram:\n");
    466   1.1       cgd 				first = 0;
    467   1.1       cgd 			}
    468  1.38    bouyer 			printf("\t\t%s: %llu\n", icmpnames[i],
    469  1.38    bouyer 				(unsigned long long)icmpstat.icps_inhist[i]);
    470   1.1       cgd 		}
    471  1.38    bouyer 	p(icps_reflect, "\t%llu message response%s generated\n");
    472  1.42    itojun 	p(icps_pmtuchg, "\t%llu path MTU change%s\n");
    473   1.9   mycroft #undef p
    474   1.6    brezak }
    475   1.6    brezak 
    476   1.6    brezak /*
    477   1.9   mycroft  * Dump IGMP statistics structure.
    478   1.6    brezak  */
    479   1.6    brezak void
    480   1.6    brezak igmp_stats(off, name)
    481   1.8       cgd 	u_long off;
    482   1.6    brezak 	char *name;
    483   1.6    brezak {
    484   1.6    brezak 	struct igmpstat igmpstat;
    485   1.6    brezak 
    486   1.6    brezak 	if (off == 0)
    487   1.6    brezak 		return;
    488   1.9   mycroft 	kread(off, (char *)&igmpstat, sizeof (igmpstat));
    489   1.9   mycroft 	printf("%s:\n", name);
    490   1.9   mycroft 
    491   1.9   mycroft #define	p(f, m) if (igmpstat.f || sflag <= 1) \
    492  1.38    bouyer     printf(m, (unsigned long long)igmpstat.f, plural(igmpstat.f))
    493   1.9   mycroft #define	py(f, m) if (igmpstat.f || sflag <= 1) \
    494  1.38    bouyer     printf(m, (unsigned long long)igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
    495  1.38    bouyer 	p(igps_rcv_total, "\t%llu message%s received\n");
    496  1.38    bouyer         p(igps_rcv_tooshort, "\t%llu message%s received with too few bytes\n");
    497  1.38    bouyer         p(igps_rcv_badsum, "\t%llu message%s received with bad checksum\n");
    498  1.38    bouyer         py(igps_rcv_queries, "\t%llu membership quer%s received\n");
    499  1.38    bouyer         py(igps_rcv_badqueries, "\t%llu membership quer%s received with invalid field(s)\n");
    500  1.38    bouyer         p(igps_rcv_reports, "\t%llu membership report%s received\n");
    501  1.38    bouyer         p(igps_rcv_badreports, "\t%llu membership report%s received with invalid field(s)\n");
    502  1.38    bouyer         p(igps_rcv_ourreports, "\t%llu membership report%s received for groups to which we belong\n");
    503  1.38    bouyer         p(igps_snd_reports, "\t%llu membership report%s sent\n");
    504   1.9   mycroft #undef p
    505   1.9   mycroft #undef py
    506  1.41     jhawk }
    507  1.41     jhawk 
    508  1.41     jhawk /*
    509  1.41     jhawk  * Dump the ARP statistics structure.
    510  1.41     jhawk  */
    511  1.41     jhawk void
    512  1.41     jhawk arp_stats(off, name)
    513  1.41     jhawk 	u_long off;
    514  1.41     jhawk 	char *name;
    515  1.41     jhawk {
    516  1.41     jhawk 	struct arpstat arpstat;
    517  1.41     jhawk 
    518  1.41     jhawk 	if (off == 0)
    519  1.41     jhawk 		return;
    520  1.41     jhawk 	kread(off, (char *)&arpstat, sizeof (arpstat));
    521  1.41     jhawk 	printf("%s:\n", name);
    522  1.41     jhawk 
    523  1.41     jhawk #define	ps(f, m) if (arpstat.f || sflag <= 1) \
    524  1.41     jhawk     printf(m, (unsigned long long)arpstat.f)
    525  1.41     jhawk #define	p(f, m) if (arpstat.f || sflag <= 1) \
    526  1.41     jhawk     printf(m, (unsigned long long)arpstat.f, plural(arpstat.f))
    527  1.41     jhawk 
    528  1.41     jhawk 	p(as_sndtotal, "\t%llu packet%s sent\n");
    529  1.41     jhawk 	p(as_sndreply, "\t\t%llu reply packet%s\n");
    530  1.41     jhawk 	p(as_sndrequest, "\t\t%llu request packet%s\n");
    531  1.41     jhawk 
    532  1.41     jhawk 	p(as_rcvtotal, "\t%llu packet%s received\n");
    533  1.41     jhawk 	p(as_rcvreply, "\t\t%llu reply packet%s\n");
    534  1.41     jhawk 	p(as_rcvrequest, "\t\t%llu valid request packet%s\n");
    535  1.41     jhawk 	p(as_rcvmcast, "\t\t%llu broadcast/multicast packet%s\n");
    536  1.41     jhawk 	p(as_rcvbadproto, "\t\t%llu packet%s with unknown protocol type\n");
    537  1.41     jhawk 	p(as_rcvbadlen, "\t\t%llu packet%s with bad (short) length\n");
    538  1.41     jhawk 	p(as_rcvzerotpa, "\t\t%llu packet%s with null target IP address\n");
    539  1.41     jhawk 	p(as_rcvzerospa, "\t\t%llu packet%s with null source IP address\n");
    540  1.41     jhawk 	ps(as_rcvnoint, "\t\t%llu could not be mapped to an interface\n");
    541  1.41     jhawk 	p(as_rcvlocalsha, "\t\t%llu packet%s sourced from a local hardware "
    542  1.41     jhawk 	    "address\n");
    543  1.41     jhawk 	p(as_rcvbcastsha, "\t\t%llu packet%s with a broadcast "
    544  1.41     jhawk 	    "source hardware address\n");
    545  1.41     jhawk 	p(as_rcvlocalspa, "\t\t%llu duplicate%s for a local IP address\n");
    546  1.41     jhawk 	p(as_rcvoverperm, "\t\t%llu attempt%s to overwrite a static entry\n");
    547  1.41     jhawk 	p(as_rcvoverint, "\t\t%llu packet%s received on wrong interface\n");
    548  1.41     jhawk 	p(as_rcvover, "\t\t%llu entry%s overwritten\n");
    549  1.41     jhawk 	p(as_rcvlenchg, "\t\t%llu change%s in hardware address length\n");
    550  1.41     jhawk 
    551  1.41     jhawk 	p(as_dfrtotal, "\t%llu packet%s deferred pending ARP resolution\n");
    552  1.41     jhawk 	ps(as_dfrsent, "\t\t%llu sent\n");
    553  1.41     jhawk 	ps(as_dfrdropped, "\t\t%llu dropped\n");
    554  1.41     jhawk 
    555  1.41     jhawk 	p(as_allocfail, "\t%llu failure%s to allocate llinfo\n");
    556  1.41     jhawk 
    557  1.41     jhawk #undef ps
    558  1.41     jhawk #undef p
    559   1.1       cgd }
    560   1.1       cgd 
    561   1.1       cgd /*
    562   1.1       cgd  * Pretty print an Internet address (net address + port).
    563   1.1       cgd  * If the nflag was specified, use numbers instead of names.
    564   1.1       cgd  */
    565   1.9   mycroft void
    566  1.28     lukem inetprint(in, port, proto, numeric)
    567  1.23     lukem 	struct in_addr *in;
    568  1.28     lukem 	u_int16_t port;
    569  1.28     lukem 	const char *proto;
    570  1.28     lukem 	int numeric;
    571   1.1       cgd {
    572   1.1       cgd 	struct servent *sp = 0;
    573   1.9   mycroft 	char line[80], *cp;
    574  1.33  sommerfe 	size_t space;
    575   1.1       cgd 
    576  1.32       mrg 	(void)snprintf(line, sizeof line, "%.*s.",
    577  1.35     lukem 	    (Aflag && !nflag) ? 12 : 16, inetname(in));
    578  1.23     lukem 	cp = strchr(line, '\0');
    579  1.28     lukem 	if (!numeric && port)
    580   1.1       cgd 		sp = getservbyport((int)port, proto);
    581  1.33  sommerfe 	space = sizeof line - (cp-line);
    582   1.1       cgd 	if (sp || port == 0)
    583  1.33  sommerfe 		(void)snprintf(cp, space, "%.8s", sp ? sp->s_name : "*");
    584   1.1       cgd 	else
    585  1.33  sommerfe 		(void)snprintf(cp, space, "%u", ntohs(port));
    586  1.32       mrg 	(void)printf(" %-*.*s", width, width, line);
    587   1.1       cgd }
    588   1.1       cgd 
    589   1.1       cgd /*
    590   1.1       cgd  * Construct an Internet address representation.
    591   1.9   mycroft  * If the nflag has been supplied, give
    592   1.1       cgd  * numeric value, otherwise try for symbolic name.
    593   1.1       cgd  */
    594   1.1       cgd char *
    595   1.9   mycroft inetname(inp)
    596   1.9   mycroft 	struct in_addr *inp;
    597   1.1       cgd {
    598  1.23     lukem 	char *cp;
    599   1.1       cgd 	static char line[50];
    600   1.1       cgd 	struct hostent *hp;
    601   1.1       cgd 	struct netent *np;
    602   1.1       cgd 	static char domain[MAXHOSTNAMELEN + 1];
    603   1.1       cgd 	static int first = 1;
    604   1.1       cgd 
    605   1.1       cgd 	if (first && !nflag) {
    606   1.1       cgd 		first = 0;
    607  1.31       mrg 		if (gethostname(domain, sizeof domain) == 0) {
    608  1.31       mrg 			domain[sizeof(domain) - 1] = '\0';
    609  1.31       mrg 			if ((cp = strchr(domain, '.')))
    610  1.31       mrg 				(void) strcpy(domain, cp + 1);
    611  1.31       mrg 			else
    612  1.31       mrg 				domain[0] = 0;
    613  1.31       mrg 		} else
    614   1.1       cgd 			domain[0] = 0;
    615   1.1       cgd 	}
    616   1.1       cgd 	cp = 0;
    617   1.9   mycroft 	if (!nflag && inp->s_addr != INADDR_ANY) {
    618   1.9   mycroft 		int net = inet_netof(*inp);
    619   1.9   mycroft 		int lna = inet_lnaof(*inp);
    620   1.1       cgd 
    621   1.1       cgd 		if (lna == INADDR_ANY) {
    622   1.1       cgd 			np = getnetbyaddr(net, AF_INET);
    623   1.1       cgd 			if (np)
    624   1.1       cgd 				cp = np->n_name;
    625   1.1       cgd 		}
    626   1.1       cgd 		if (cp == 0) {
    627   1.9   mycroft 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
    628   1.1       cgd 			if (hp) {
    629  1.23     lukem 				if ((cp = strchr(hp->h_name, '.')) &&
    630   1.1       cgd 				    !strcmp(cp + 1, domain))
    631   1.1       cgd 					*cp = 0;
    632   1.1       cgd 				cp = hp->h_name;
    633   1.1       cgd 			}
    634   1.1       cgd 		}
    635   1.1       cgd 	}
    636   1.9   mycroft 	if (inp->s_addr == INADDR_ANY)
    637  1.32       mrg 		strncpy(line, "*", sizeof line);
    638   1.1       cgd 	else if (cp)
    639  1.32       mrg 		strncpy(line, cp, sizeof line);
    640   1.1       cgd 	else {
    641   1.9   mycroft 		inp->s_addr = ntohl(inp->s_addr);
    642   1.1       cgd #define C(x)	((x) & 0xff)
    643  1.32       mrg 		(void)snprintf(line, sizeof line, "%u.%u.%u.%u",
    644  1.32       mrg 		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
    645  1.32       mrg 		    C(inp->s_addr >> 8), C(inp->s_addr));
    646  1.32       mrg #undef C
    647   1.1       cgd 	}
    648  1.32       mrg 	line[sizeof(line) - 1] = '\0';
    649   1.1       cgd 	return (line);
    650  1.30   thorpej }
    651  1.30   thorpej 
    652  1.30   thorpej /*
    653  1.30   thorpej  * Dump the contents of a TCP PCB.
    654  1.30   thorpej  */
    655  1.30   thorpej void
    656  1.30   thorpej tcp_dump(pcbaddr)
    657  1.30   thorpej 	u_long pcbaddr;
    658  1.30   thorpej {
    659  1.30   thorpej 	struct tcpcb tcpcb;
    660  1.30   thorpej 	int i;
    661  1.30   thorpej 
    662  1.30   thorpej 	kread(pcbaddr, (char *)&tcpcb, sizeof(tcpcb));
    663  1.30   thorpej 
    664  1.30   thorpej 	printf("TCP Protocol Control Block at 0x%08lx:\n\n", pcbaddr);
    665  1.30   thorpej 
    666  1.30   thorpej 	printf("Timers:\n");
    667  1.30   thorpej 	for (i = 0; i < TCPT_NTIMERS; i++)
    668  1.30   thorpej 		printf("\t%s: %u", tcptimers[i], tcpcb.t_timer[i]);
    669  1.30   thorpej 	printf("\n\n");
    670  1.30   thorpej 
    671  1.30   thorpej 	if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
    672  1.30   thorpej 		printf("State: %d", tcpcb.t_state);
    673  1.30   thorpej 	else
    674  1.30   thorpej 		printf("State: %s", tcpstates[tcpcb.t_state]);
    675  1.30   thorpej 	printf(", flags 0x%x, inpcb 0x%lx\n\n", tcpcb.t_flags,
    676  1.30   thorpej 	    (u_long)tcpcb.t_inpcb);
    677  1.30   thorpej 
    678  1.30   thorpej 	printf("rxtshift %d, rxtcur %d, dupacks %d\n", tcpcb.t_rxtshift,
    679  1.30   thorpej 	    tcpcb.t_rxtcur, tcpcb.t_dupacks);
    680  1.30   thorpej 	printf("peermss %u, ourmss %u, segsz %u\n\n", tcpcb.t_peermss,
    681  1.30   thorpej 	    tcpcb.t_ourmss, tcpcb.t_segsz);
    682  1.30   thorpej 
    683  1.30   thorpej 	printf("snd_una %u, snd_nxt %u, snd_up %u\n",
    684  1.30   thorpej 	    tcpcb.snd_una, tcpcb.snd_nxt, tcpcb.snd_up);
    685  1.30   thorpej 	printf("snd_wl1 %u, snd_wl2 %u, iss %u, snd_wnd %lu\n\n",
    686  1.30   thorpej 	    tcpcb.snd_wl1, tcpcb.snd_wl2, tcpcb.iss, tcpcb.snd_wnd);
    687  1.30   thorpej 
    688  1.30   thorpej 	printf("rcv_wnd %lu, rcv_nxt %u, rcv_up %u, irs %u\n\n",
    689  1.30   thorpej 	    tcpcb.rcv_wnd, tcpcb.rcv_nxt, tcpcb.rcv_up, tcpcb.irs);
    690  1.30   thorpej 
    691  1.30   thorpej 	printf("rcv_adv %u, snd_max %u, snd_cwnd %lu, snd_ssthresh %lu\n",
    692  1.30   thorpej 	    tcpcb.rcv_adv, tcpcb.snd_max, tcpcb.snd_cwnd, tcpcb.snd_ssthresh);
    693  1.30   thorpej 
    694  1.30   thorpej 	printf("idle %d, rtt %d, rtseq %u, srtt %d, rttvar %d, rttmin %d, "
    695  1.30   thorpej 	    "max_sndwnd %lu\n\n", tcpcb.t_idle, tcpcb.t_rtt, tcpcb.t_rtseq,
    696  1.30   thorpej 	    tcpcb.t_srtt, tcpcb.t_rttvar, tcpcb.t_rttmin, tcpcb.max_sndwnd);
    697  1.30   thorpej 
    698  1.30   thorpej 	printf("oobflags %d, iobc %d, softerror %d\n\n", tcpcb.t_oobflags,
    699  1.30   thorpej 	    tcpcb.t_iobc, tcpcb.t_softerror);
    700  1.30   thorpej 
    701  1.30   thorpej 	printf("snd_scale %d, rcv_scale %d, req_r_scale %d, req_s_scale %d\n",
    702  1.30   thorpej 	    tcpcb.snd_scale, tcpcb.rcv_scale, tcpcb.request_r_scale,
    703  1.30   thorpej 	    tcpcb.requested_s_scale);
    704  1.30   thorpej 	printf("ts_recent %u, ts_regent_age %d, last_ack_sent %u\n",
    705  1.30   thorpej 	    tcpcb.ts_recent, tcpcb.ts_recent_age, tcpcb.last_ack_sent);
    706   1.1       cgd }
    707