xref: /original-bsd/usr.bin/netstat/ns.c (revision 39f01a53)
1cc68246cSsklower /*
2*39f01a53Sbostic  * Copyright (c) 1983, 1988, 1993
3*39f01a53Sbostic  *	The Regents of the University of California.  All rights reserved.
4d8a91290Skarels  *
5eeca2472Sbostic  * %sccs.include.redist.c%
6cc68246cSsklower  */
7cc68246cSsklower 
8cc68246cSsklower #ifndef lint
9*39f01a53Sbostic static char sccsid[] = "@(#)ns.c	8.1 (Berkeley) 06/06/93";
100b25c7b4Sbostic #endif /* not lint */
11cc68246cSsklower 
12685e1f22Ssklower #include <sys/param.h>
13cc68246cSsklower #include <sys/socket.h>
14cc68246cSsklower #include <sys/socketvar.h>
15cc68246cSsklower #include <sys/mbuf.h>
16cc68246cSsklower #include <sys/protosw.h>
17cc68246cSsklower 
18cc68246cSsklower #include <net/route.h>
19cc68246cSsklower #include <net/if.h>
20cc68246cSsklower 
21cc68246cSsklower #include <netinet/tcp_fsm.h>
22cc68246cSsklower 
23cc68246cSsklower #include <netns/ns.h>
24cc68246cSsklower #include <netns/ns_pcb.h>
25cc68246cSsklower #include <netns/idp.h>
26cc68246cSsklower #include <netns/idp_var.h>
27cc68246cSsklower #include <netns/ns_error.h>
28cc68246cSsklower #include <netns/sp.h>
29cc68246cSsklower #include <netns/spidp.h>
306b86247aSkarels #include <netns/spp_timer.h>
31cc68246cSsklower #include <netns/spp_var.h>
32cc68246cSsklower #define SANAMES
33cc68246cSsklower #include <netns/spp_debug.h>
34cc68246cSsklower 
351de12f92Sbostic #include <nlist.h>
361de12f92Sbostic #include <errno.h>
371de12f92Sbostic #include <stdio.h>
381de12f92Sbostic #include <string.h>
39e8f3b79dSsklower #include "netstat.h"
40cc68246cSsklower 
41cc68246cSsklower struct	nspcb nspcb;
42cc68246cSsklower struct	sppcb sppcb;
43cc68246cSsklower struct	socket sockb;
44e8f3b79dSsklower 
45e8f3b79dSsklower static char *ns_prpr __P((struct ns_addr *));
46a898f5b2Ssklower static void ns_erputil __P((int, int));
47cc68246cSsklower 
48cc68246cSsklower static	int first = 1;
49cc68246cSsklower 
50cc68246cSsklower /*
51cc68246cSsklower  * Print a summary of connections related to a Network Systems
52cc68246cSsklower  * protocol.  For SPP, also give state of connection.
53cc68246cSsklower  * Listening processes (aflag) are suppressed unless the
54cc68246cSsklower  * -a (all) flag is specified.
55cc68246cSsklower  */
56cc68246cSsklower 
57e8f3b79dSsklower void
nsprotopr(off,name)58cc68246cSsklower nsprotopr(off, name)
5979730432Ssklower 	u_long off;
60cc68246cSsklower 	char *name;
61cc68246cSsklower {
62cc68246cSsklower 	struct nspcb cb;
63cc68246cSsklower 	register struct nspcb *prev, *next;
64cc68246cSsklower 	int isspp;
65cc68246cSsklower 
66a19b3453Skarels 	if (off == 0)
67cc68246cSsklower 		return;
68cc68246cSsklower 	isspp = strcmp(name, "spp") == 0;
69e8f3b79dSsklower 	kread(off, (char *)&cb, sizeof (struct nspcb));
70cc68246cSsklower 	nspcb = cb;
71cc68246cSsklower 	prev = (struct nspcb *)off;
72a19b3453Skarels 	if (nspcb.nsp_next == (struct nspcb *)off)
73a19b3453Skarels 		return;
74cc68246cSsklower 	for (;nspcb.nsp_next != (struct nspcb *)off; prev = next) {
7579730432Ssklower 		u_long ppcb;
76cc68246cSsklower 
77cc68246cSsklower 		next = nspcb.nsp_next;
7879730432Ssklower 		kread((u_long)next, (char *)&nspcb, sizeof (nspcb));
79cc68246cSsklower 		if (nspcb.nsp_prev != prev) {
80cc68246cSsklower 			printf("???\n");
81cc68246cSsklower 			break;
82cc68246cSsklower 		}
83cc68246cSsklower 		if (!aflag && ns_nullhost(nspcb.nsp_faddr) ) {
84cc68246cSsklower 			continue;
85cc68246cSsklower 		}
8679730432Ssklower 		kread((u_long)nspcb.nsp_socket,
8767c2c57fSsklower 				(char *)&sockb, sizeof (sockb));
8879730432Ssklower 		ppcb = (u_long) nspcb.nsp_pcb;
89cc68246cSsklower 		if (ppcb) {
90cc68246cSsklower 			if (isspp) {
91e8f3b79dSsklower 				kread(ppcb, (char *)&sppcb, sizeof (sppcb));
92cc68246cSsklower 			} else continue;
93cc68246cSsklower 		} else
94cc68246cSsklower 			if (isspp) continue;
9592d65723Skarels 		if (first) {
9692d65723Skarels 			printf("Active NS connections");
9792d65723Skarels 			if (aflag)
9892d65723Skarels 				printf(" (including servers)");
9992d65723Skarels 			putchar('\n');
10092d65723Skarels 			if (Aflag)
10192d65723Skarels 				printf("%-8.8s ", "PCB");
10292d65723Skarels 			printf(Aflag ?
10392d65723Skarels 				"%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
10492d65723Skarels 				"%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
10592d65723Skarels 				"Proto", "Recv-Q", "Send-Q",
10692d65723Skarels 				"Local Address", "Foreign Address", "(state)");
10792d65723Skarels 			first = 0;
10892d65723Skarels 		}
109cc68246cSsklower 		if (Aflag)
110cc68246cSsklower 			printf("%8x ", ppcb);
111cc68246cSsklower 		printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
112cc68246cSsklower 			sockb.so_snd.sb_cc);
113cc68246cSsklower 		printf("  %-22.22s", ns_prpr(&nspcb.nsp_laddr));
114cc68246cSsklower 		printf(" %-22.22s", ns_prpr(&nspcb.nsp_faddr));
115cc68246cSsklower 		if (isspp) {
116cc68246cSsklower 			extern char *tcpstates[];
117647b27cfSkupfer 			if (sppcb.s_state >= TCP_NSTATES)
118cc68246cSsklower 				printf(" %d", sppcb.s_state);
119cc68246cSsklower 			else
120cc68246cSsklower 				printf(" %s", tcpstates[sppcb.s_state]);
121cc68246cSsklower 		}
122cc68246cSsklower 		putchar('\n');
123cc68246cSsklower 		prev = next;
124cc68246cSsklower 	}
125cc68246cSsklower }
1261de12f92Sbostic #define ANY(x,y,z) \
1271de12f92Sbostic 	((x) ? printf("\t%d %s%s%s -- %s\n",x,y,plural(x),z,"x") : 0)
128cc68246cSsklower 
129cc68246cSsklower /*
130cc68246cSsklower  * Dump SPP statistics structure.
131cc68246cSsklower  */
132e8f3b79dSsklower void
spp_stats(off,name)133cc68246cSsklower spp_stats(off, name)
13479730432Ssklower 	u_long off;
135cc68246cSsklower 	char *name;
136cc68246cSsklower {
137cc68246cSsklower 	struct spp_istat spp_istat;
1384514e7c6Ssklower #define sppstat spp_istat.newstats
139cc68246cSsklower 
140a19b3453Skarels 	if (off == 0)
141cc68246cSsklower 		return;
142e8f3b79dSsklower 	kread(off, (char *)&spp_istat, sizeof (spp_istat));
143cc68246cSsklower 	printf("%s:\n", name);
144cc68246cSsklower 	ANY(spp_istat.nonucn, "connection", " dropped due to no new sockets ");
145cc68246cSsklower 	ANY(spp_istat.gonawy, "connection", " terminated due to our end dying");
1461de12f92Sbostic 	ANY(spp_istat.nonucn, "connection",
1471de12f92Sbostic 	    " dropped due to inability to connect");
1481de12f92Sbostic 	ANY(spp_istat.noconn, "connection",
1491de12f92Sbostic 	    " dropped due to inability to connect");
1501de12f92Sbostic 	ANY(spp_istat.notme, "connection",
1511de12f92Sbostic 	    " incompleted due to mismatched id's");
152cc68246cSsklower 	ANY(spp_istat.wrncon, "connection", " dropped due to mismatched id's");
153cc68246cSsklower 	ANY(spp_istat.bdreas, "packet", " dropped out of sequence");
154cc68246cSsklower 	ANY(spp_istat.lstdup, "packet", " duplicating the highest packet");
155cc68246cSsklower 	ANY(spp_istat.notyet, "packet", " refused as exceeding allocation");
1564514e7c6Ssklower 	ANY(sppstat.spps_connattempt, "connection", " initiated");
1574514e7c6Ssklower 	ANY(sppstat.spps_accepts, "connection", " accepted");
1584514e7c6Ssklower 	ANY(sppstat.spps_connects, "connection", " established");
1594514e7c6Ssklower 	ANY(sppstat.spps_drops, "connection", " dropped");
1604514e7c6Ssklower 	ANY(sppstat.spps_conndrops, "embryonic connection", " dropped");
1614514e7c6Ssklower 	ANY(sppstat.spps_closed, "connection", " closed (includes drops)");
1624514e7c6Ssklower 	ANY(sppstat.spps_segstimed, "packet", " where we tried to get rtt");
1634514e7c6Ssklower 	ANY(sppstat.spps_rttupdated, "time", " we got rtt");
1644514e7c6Ssklower 	ANY(sppstat.spps_delack, "delayed ack", " sent");
1654514e7c6Ssklower 	ANY(sppstat.spps_timeoutdrop, "connection", " dropped in rxmt timeout");
1664514e7c6Ssklower 	ANY(sppstat.spps_rexmttimeo, "retransmit timeout", "");
1674514e7c6Ssklower 	ANY(sppstat.spps_persisttimeo, "persist timeout", "");
1684514e7c6Ssklower 	ANY(sppstat.spps_keeptimeo, "keepalive timeout", "");
1694514e7c6Ssklower 	ANY(sppstat.spps_keepprobe, "keepalive probe", " sent");
1704514e7c6Ssklower 	ANY(sppstat.spps_keepdrops, "connection", " dropped in keepalive");
1714514e7c6Ssklower 	ANY(sppstat.spps_sndtotal, "total packet", " sent");
1724514e7c6Ssklower 	ANY(sppstat.spps_sndpack, "data packet", " sent");
1734514e7c6Ssklower 	ANY(sppstat.spps_sndbyte, "data byte", " sent");
1744514e7c6Ssklower 	ANY(sppstat.spps_sndrexmitpack, "data packet", " retransmitted");
1754514e7c6Ssklower 	ANY(sppstat.spps_sndrexmitbyte, "data byte", " retransmitted");
1764514e7c6Ssklower 	ANY(sppstat.spps_sndacks, "ack-only packet", " sent");
1774514e7c6Ssklower 	ANY(sppstat.spps_sndprobe, "window probe", " sent");
1784514e7c6Ssklower 	ANY(sppstat.spps_sndurg, "packet", " sent with URG only");
1794514e7c6Ssklower 	ANY(sppstat.spps_sndwinup, "window update-only packet", " sent");
1804514e7c6Ssklower 	ANY(sppstat.spps_sndctrl, "control (SYN|FIN|RST) packet", " sent");
1814514e7c6Ssklower 	ANY(sppstat.spps_sndvoid, "request", " to send a non-existant packet");
1824514e7c6Ssklower 	ANY(sppstat.spps_rcvtotal, "total packet", " received");
1834514e7c6Ssklower 	ANY(sppstat.spps_rcvpack, "packet", " received in sequence");
1844514e7c6Ssklower 	ANY(sppstat.spps_rcvbyte, "byte", " received in sequence");
1854514e7c6Ssklower 	ANY(sppstat.spps_rcvbadsum, "packet", " received with ccksum errs");
1864514e7c6Ssklower 	ANY(sppstat.spps_rcvbadoff, "packet", " received with bad offset");
1874514e7c6Ssklower 	ANY(sppstat.spps_rcvshort, "packet", " received too short");
1884514e7c6Ssklower 	ANY(sppstat.spps_rcvduppack, "duplicate-only packet", " received");
1894514e7c6Ssklower 	ANY(sppstat.spps_rcvdupbyte, "duplicate-only byte", " received");
1904514e7c6Ssklower 	ANY(sppstat.spps_rcvpartduppack, "packet", " with some duplicate data");
1914514e7c6Ssklower 	ANY(sppstat.spps_rcvpartdupbyte, "dup. byte", " in part-dup. packet");
1924514e7c6Ssklower 	ANY(sppstat.spps_rcvoopack, "out-of-order packet", " received");
1934514e7c6Ssklower 	ANY(sppstat.spps_rcvoobyte, "out-of-order byte", " received");
1944514e7c6Ssklower 	ANY(sppstat.spps_rcvpackafterwin, "packet", " with data after window");
1954514e7c6Ssklower 	ANY(sppstat.spps_rcvbyteafterwin, "byte", " rcvd after window");
1964514e7c6Ssklower 	ANY(sppstat.spps_rcvafterclose, "packet", " rcvd after 'close'");
1974514e7c6Ssklower 	ANY(sppstat.spps_rcvwinprobe, "rcvd window probe packet", "");
1984514e7c6Ssklower 	ANY(sppstat.spps_rcvdupack, "rcvd duplicate ack", "");
1994514e7c6Ssklower 	ANY(sppstat.spps_rcvacktoomuch, "rcvd ack", " for unsent data");
2004514e7c6Ssklower 	ANY(sppstat.spps_rcvackpack, "rcvd ack packet", "");
2014514e7c6Ssklower 	ANY(sppstat.spps_rcvackbyte, "byte", " acked by rcvd acks");
2024514e7c6Ssklower 	ANY(sppstat.spps_rcvwinupd, "rcvd window update packet", "");
203cc68246cSsklower }
2044514e7c6Ssklower #undef ANY
2054514e7c6Ssklower #define ANY(x,y,z)  ((x) ? printf("\t%d %s%s%s\n",x,y,plural(x),z) : 0)
206cc68246cSsklower 
207cc68246cSsklower /*
208cc68246cSsklower  * Dump IDP statistics structure.
209cc68246cSsklower  */
210e8f3b79dSsklower void
idp_stats(off,name)211cc68246cSsklower idp_stats(off, name)
21279730432Ssklower 	u_long off;
213cc68246cSsklower 	char *name;
214cc68246cSsklower {
215cc68246cSsklower 	struct idpstat idpstat;
216cc68246cSsklower 
217a19b3453Skarels 	if (off == 0)
218cc68246cSsklower 		return;
219e8f3b79dSsklower 	kread(off, (char *)&idpstat, sizeof (idpstat));
220647b27cfSkupfer 	printf("%s:\n", name);
221cc68246cSsklower 	ANY(idpstat.idps_toosmall, "packet", " smaller than a header");
222cc68246cSsklower 	ANY(idpstat.idps_tooshort, "packet", " smaller than advertised");
223cc68246cSsklower 	ANY(idpstat.idps_badsum, "packet", " with bad checksums");
224cc68246cSsklower }
225cc68246cSsklower 
2264514e7c6Ssklower static	struct {
2274514e7c6Ssklower 	u_short code;
2284514e7c6Ssklower 	char *name;
2294514e7c6Ssklower 	char *where;
2304514e7c6Ssklower } ns_errnames[] = {
2314514e7c6Ssklower 	{0, "Unspecified Error", " at Destination"},
2324514e7c6Ssklower 	{1, "Bad Checksum", " at Destination"},
2334514e7c6Ssklower 	{2, "No Listener", " at Socket"},
2344514e7c6Ssklower 	{3, "Packet", " Refused due to lack of space at Destination"},
2354514e7c6Ssklower 	{01000, "Unspecified Error", " while gatewayed"},
2364514e7c6Ssklower 	{01001, "Bad Checksum", " while gatewayed"},
2374514e7c6Ssklower 	{01002, "Packet", " forwarded too many times"},
2384514e7c6Ssklower 	{01003, "Packet", " too large to be forwarded"},
2394514e7c6Ssklower 	{-1, 0, 0},
240cc68246cSsklower };
241cc68246cSsklower 
242cc68246cSsklower /*
243cc68246cSsklower  * Dump NS Error statistics structure.
244cc68246cSsklower  */
245647b27cfSkupfer /*ARGSUSED*/
246e8f3b79dSsklower void
nserr_stats(off,name)247cc68246cSsklower nserr_stats(off, name)
24879730432Ssklower 	u_long off;
249cc68246cSsklower 	char *name;
250cc68246cSsklower {
251cc68246cSsklower 	struct ns_errstat ns_errstat;
252cc68246cSsklower 	register int j;
253cc68246cSsklower 	register int histoprint = 1;
254cc68246cSsklower 	int z;
255cc68246cSsklower 
256a19b3453Skarels 	if (off == 0)
257cc68246cSsklower 		return;
258e8f3b79dSsklower 	kread(off, (char *)&ns_errstat, sizeof (ns_errstat));
259cc68246cSsklower 	printf("NS error statistics:\n");
260cc68246cSsklower 	ANY(ns_errstat.ns_es_error, "call", " to ns_error");
261cc68246cSsklower 	ANY(ns_errstat.ns_es_oldshort, "error",
262cc68246cSsklower 		" ignored due to insufficient addressing");
263cc68246cSsklower 	ANY(ns_errstat.ns_es_oldns_err, "error request",
264cc68246cSsklower 		" in response to error packets");
265cc68246cSsklower 	ANY(ns_errstat.ns_es_tooshort, "error packet",
266cc68246cSsklower 		" received incomplete");
267cc68246cSsklower 	ANY(ns_errstat.ns_es_badcode, "error packet",
268cc68246cSsklower 		" received of unknown type");
269cc68246cSsklower 	for(j = 0; j < NS_ERR_MAX; j ++) {
270cc68246cSsklower 		z = ns_errstat.ns_es_outhist[j];
271cc68246cSsklower 		if (z && histoprint) {
272cc68246cSsklower 			printf("Output Error Histogram:\n");
273cc68246cSsklower 			histoprint = 0;
274cc68246cSsklower 		}
2754514e7c6Ssklower 		ns_erputil(z, ns_errstat.ns_es_codes[j]);
2764514e7c6Ssklower 
277cc68246cSsklower 	}
278cc68246cSsklower 	histoprint = 1;
279cc68246cSsklower 	for(j = 0; j < NS_ERR_MAX; j ++) {
280cc68246cSsklower 		z = ns_errstat.ns_es_inhist[j];
281cc68246cSsklower 		if (z && histoprint) {
282cc68246cSsklower 			printf("Input Error Histogram:\n");
283cc68246cSsklower 			histoprint = 0;
284cc68246cSsklower 		}
2854514e7c6Ssklower 		ns_erputil(z, ns_errstat.ns_es_codes[j]);
286cc68246cSsklower 	}
287cc68246cSsklower }
2881de12f92Sbostic 
289e8f3b79dSsklower static void
ns_erputil(z,c)2904514e7c6Ssklower ns_erputil(z, c)
291e8f3b79dSsklower 	int z, c;
2924514e7c6Ssklower {
2934514e7c6Ssklower 	int j;
2944514e7c6Ssklower 	char codebuf[30];
2954514e7c6Ssklower 	char *name, *where;
2961de12f92Sbostic 
2974514e7c6Ssklower 	for(j = 0;; j ++) {
2984514e7c6Ssklower 		if ((name = ns_errnames[j].name) == 0)
2994514e7c6Ssklower 			break;
3004514e7c6Ssklower 		if (ns_errnames[j].code == c)
3014514e7c6Ssklower 			break;
3024514e7c6Ssklower 	}
3034514e7c6Ssklower 	if (name == 0)  {
3044514e7c6Ssklower 		if (c > 01000)
3054514e7c6Ssklower 			where = "in transit";
3064514e7c6Ssklower 		else
3074514e7c6Ssklower 			where = "at destination";
3084514e7c6Ssklower 		sprintf(codebuf, "Unknown XNS error code 0%o", c);
3094514e7c6Ssklower 		name = codebuf;
3104514e7c6Ssklower 	} else
3114514e7c6Ssklower 		where =  ns_errnames[j].where;
3124514e7c6Ssklower 	ANY(z, name, where);
3134514e7c6Ssklower }
3141de12f92Sbostic 
315cc68246cSsklower static struct sockaddr_ns ssns = {AF_NS};
316cc68246cSsklower 
317e8f3b79dSsklower static
ns_prpr(x)318cc68246cSsklower char *ns_prpr(x)
319cc68246cSsklower 	struct ns_addr *x;
320cc68246cSsklower {
321cc68246cSsklower 	struct sockaddr_ns *sns = &ssns;
3221de12f92Sbostic 
323cc68246cSsklower 	sns->sns_addr = *x;
324e8f3b79dSsklower 	return(ns_print((struct sockaddr *)sns));
325cc68246cSsklower }
326