xref: /original-bsd/usr.bin/netstat/ns.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)ns.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/socket.h>
14 #include <sys/socketvar.h>
15 #include <sys/mbuf.h>
16 #include <sys/protosw.h>
17 
18 #include <net/route.h>
19 #include <net/if.h>
20 
21 #include <netinet/tcp_fsm.h>
22 
23 #include <netns/ns.h>
24 #include <netns/ns_pcb.h>
25 #include <netns/idp.h>
26 #include <netns/idp_var.h>
27 #include <netns/ns_error.h>
28 #include <netns/sp.h>
29 #include <netns/spidp.h>
30 #include <netns/spp_timer.h>
31 #include <netns/spp_var.h>
32 #define SANAMES
33 #include <netns/spp_debug.h>
34 
35 #include <nlist.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include "netstat.h"
40 
41 struct	nspcb nspcb;
42 struct	sppcb sppcb;
43 struct	socket sockb;
44 
45 static char *ns_prpr __P((struct ns_addr *));
46 static void ns_erputil __P((int, int));
47 
48 static	int first = 1;
49 
50 /*
51  * Print a summary of connections related to a Network Systems
52  * protocol.  For SPP, also give state of connection.
53  * Listening processes (aflag) are suppressed unless the
54  * -a (all) flag is specified.
55  */
56 
57 void
58 nsprotopr(off, name)
59 	u_long off;
60 	char *name;
61 {
62 	struct nspcb cb;
63 	register struct nspcb *prev, *next;
64 	int isspp;
65 
66 	if (off == 0)
67 		return;
68 	isspp = strcmp(name, "spp") == 0;
69 	kread(off, (char *)&cb, sizeof (struct nspcb));
70 	nspcb = cb;
71 	prev = (struct nspcb *)off;
72 	if (nspcb.nsp_next == (struct nspcb *)off)
73 		return;
74 	for (;nspcb.nsp_next != (struct nspcb *)off; prev = next) {
75 		u_long ppcb;
76 
77 		next = nspcb.nsp_next;
78 		kread((u_long)next, (char *)&nspcb, sizeof (nspcb));
79 		if (nspcb.nsp_prev != prev) {
80 			printf("???\n");
81 			break;
82 		}
83 		if (!aflag && ns_nullhost(nspcb.nsp_faddr) ) {
84 			continue;
85 		}
86 		kread((u_long)nspcb.nsp_socket,
87 				(char *)&sockb, sizeof (sockb));
88 		ppcb = (u_long) nspcb.nsp_pcb;
89 		if (ppcb) {
90 			if (isspp) {
91 				kread(ppcb, (char *)&sppcb, sizeof (sppcb));
92 			} else continue;
93 		} else
94 			if (isspp) continue;
95 		if (first) {
96 			printf("Active NS connections");
97 			if (aflag)
98 				printf(" (including servers)");
99 			putchar('\n');
100 			if (Aflag)
101 				printf("%-8.8s ", "PCB");
102 			printf(Aflag ?
103 				"%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
104 				"%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
105 				"Proto", "Recv-Q", "Send-Q",
106 				"Local Address", "Foreign Address", "(state)");
107 			first = 0;
108 		}
109 		if (Aflag)
110 			printf("%8x ", ppcb);
111 		printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
112 			sockb.so_snd.sb_cc);
113 		printf("  %-22.22s", ns_prpr(&nspcb.nsp_laddr));
114 		printf(" %-22.22s", ns_prpr(&nspcb.nsp_faddr));
115 		if (isspp) {
116 			extern char *tcpstates[];
117 			if (sppcb.s_state >= TCP_NSTATES)
118 				printf(" %d", sppcb.s_state);
119 			else
120 				printf(" %s", tcpstates[sppcb.s_state]);
121 		}
122 		putchar('\n');
123 		prev = next;
124 	}
125 }
126 #define ANY(x,y,z) \
127 	((x) ? printf("\t%d %s%s%s -- %s\n",x,y,plural(x),z,"x") : 0)
128 
129 /*
130  * Dump SPP statistics structure.
131  */
132 void
133 spp_stats(off, name)
134 	u_long off;
135 	char *name;
136 {
137 	struct spp_istat spp_istat;
138 #define sppstat spp_istat.newstats
139 
140 	if (off == 0)
141 		return;
142 	kread(off, (char *)&spp_istat, sizeof (spp_istat));
143 	printf("%s:\n", name);
144 	ANY(spp_istat.nonucn, "connection", " dropped due to no new sockets ");
145 	ANY(spp_istat.gonawy, "connection", " terminated due to our end dying");
146 	ANY(spp_istat.nonucn, "connection",
147 	    " dropped due to inability to connect");
148 	ANY(spp_istat.noconn, "connection",
149 	    " dropped due to inability to connect");
150 	ANY(spp_istat.notme, "connection",
151 	    " incompleted due to mismatched id's");
152 	ANY(spp_istat.wrncon, "connection", " dropped due to mismatched id's");
153 	ANY(spp_istat.bdreas, "packet", " dropped out of sequence");
154 	ANY(spp_istat.lstdup, "packet", " duplicating the highest packet");
155 	ANY(spp_istat.notyet, "packet", " refused as exceeding allocation");
156 	ANY(sppstat.spps_connattempt, "connection", " initiated");
157 	ANY(sppstat.spps_accepts, "connection", " accepted");
158 	ANY(sppstat.spps_connects, "connection", " established");
159 	ANY(sppstat.spps_drops, "connection", " dropped");
160 	ANY(sppstat.spps_conndrops, "embryonic connection", " dropped");
161 	ANY(sppstat.spps_closed, "connection", " closed (includes drops)");
162 	ANY(sppstat.spps_segstimed, "packet", " where we tried to get rtt");
163 	ANY(sppstat.spps_rttupdated, "time", " we got rtt");
164 	ANY(sppstat.spps_delack, "delayed ack", " sent");
165 	ANY(sppstat.spps_timeoutdrop, "connection", " dropped in rxmt timeout");
166 	ANY(sppstat.spps_rexmttimeo, "retransmit timeout", "");
167 	ANY(sppstat.spps_persisttimeo, "persist timeout", "");
168 	ANY(sppstat.spps_keeptimeo, "keepalive timeout", "");
169 	ANY(sppstat.spps_keepprobe, "keepalive probe", " sent");
170 	ANY(sppstat.spps_keepdrops, "connection", " dropped in keepalive");
171 	ANY(sppstat.spps_sndtotal, "total packet", " sent");
172 	ANY(sppstat.spps_sndpack, "data packet", " sent");
173 	ANY(sppstat.spps_sndbyte, "data byte", " sent");
174 	ANY(sppstat.spps_sndrexmitpack, "data packet", " retransmitted");
175 	ANY(sppstat.spps_sndrexmitbyte, "data byte", " retransmitted");
176 	ANY(sppstat.spps_sndacks, "ack-only packet", " sent");
177 	ANY(sppstat.spps_sndprobe, "window probe", " sent");
178 	ANY(sppstat.spps_sndurg, "packet", " sent with URG only");
179 	ANY(sppstat.spps_sndwinup, "window update-only packet", " sent");
180 	ANY(sppstat.spps_sndctrl, "control (SYN|FIN|RST) packet", " sent");
181 	ANY(sppstat.spps_sndvoid, "request", " to send a non-existant packet");
182 	ANY(sppstat.spps_rcvtotal, "total packet", " received");
183 	ANY(sppstat.spps_rcvpack, "packet", " received in sequence");
184 	ANY(sppstat.spps_rcvbyte, "byte", " received in sequence");
185 	ANY(sppstat.spps_rcvbadsum, "packet", " received with ccksum errs");
186 	ANY(sppstat.spps_rcvbadoff, "packet", " received with bad offset");
187 	ANY(sppstat.spps_rcvshort, "packet", " received too short");
188 	ANY(sppstat.spps_rcvduppack, "duplicate-only packet", " received");
189 	ANY(sppstat.spps_rcvdupbyte, "duplicate-only byte", " received");
190 	ANY(sppstat.spps_rcvpartduppack, "packet", " with some duplicate data");
191 	ANY(sppstat.spps_rcvpartdupbyte, "dup. byte", " in part-dup. packet");
192 	ANY(sppstat.spps_rcvoopack, "out-of-order packet", " received");
193 	ANY(sppstat.spps_rcvoobyte, "out-of-order byte", " received");
194 	ANY(sppstat.spps_rcvpackafterwin, "packet", " with data after window");
195 	ANY(sppstat.spps_rcvbyteafterwin, "byte", " rcvd after window");
196 	ANY(sppstat.spps_rcvafterclose, "packet", " rcvd after 'close'");
197 	ANY(sppstat.spps_rcvwinprobe, "rcvd window probe packet", "");
198 	ANY(sppstat.spps_rcvdupack, "rcvd duplicate ack", "");
199 	ANY(sppstat.spps_rcvacktoomuch, "rcvd ack", " for unsent data");
200 	ANY(sppstat.spps_rcvackpack, "rcvd ack packet", "");
201 	ANY(sppstat.spps_rcvackbyte, "byte", " acked by rcvd acks");
202 	ANY(sppstat.spps_rcvwinupd, "rcvd window update packet", "");
203 }
204 #undef ANY
205 #define ANY(x,y,z)  ((x) ? printf("\t%d %s%s%s\n",x,y,plural(x),z) : 0)
206 
207 /*
208  * Dump IDP statistics structure.
209  */
210 void
211 idp_stats(off, name)
212 	u_long off;
213 	char *name;
214 {
215 	struct idpstat idpstat;
216 
217 	if (off == 0)
218 		return;
219 	kread(off, (char *)&idpstat, sizeof (idpstat));
220 	printf("%s:\n", name);
221 	ANY(idpstat.idps_toosmall, "packet", " smaller than a header");
222 	ANY(idpstat.idps_tooshort, "packet", " smaller than advertised");
223 	ANY(idpstat.idps_badsum, "packet", " with bad checksums");
224 }
225 
226 static	struct {
227 	u_short code;
228 	char *name;
229 	char *where;
230 } ns_errnames[] = {
231 	{0, "Unspecified Error", " at Destination"},
232 	{1, "Bad Checksum", " at Destination"},
233 	{2, "No Listener", " at Socket"},
234 	{3, "Packet", " Refused due to lack of space at Destination"},
235 	{01000, "Unspecified Error", " while gatewayed"},
236 	{01001, "Bad Checksum", " while gatewayed"},
237 	{01002, "Packet", " forwarded too many times"},
238 	{01003, "Packet", " too large to be forwarded"},
239 	{-1, 0, 0},
240 };
241 
242 /*
243  * Dump NS Error statistics structure.
244  */
245 /*ARGSUSED*/
246 void
247 nserr_stats(off, name)
248 	u_long off;
249 	char *name;
250 {
251 	struct ns_errstat ns_errstat;
252 	register int j;
253 	register int histoprint = 1;
254 	int z;
255 
256 	if (off == 0)
257 		return;
258 	kread(off, (char *)&ns_errstat, sizeof (ns_errstat));
259 	printf("NS error statistics:\n");
260 	ANY(ns_errstat.ns_es_error, "call", " to ns_error");
261 	ANY(ns_errstat.ns_es_oldshort, "error",
262 		" ignored due to insufficient addressing");
263 	ANY(ns_errstat.ns_es_oldns_err, "error request",
264 		" in response to error packets");
265 	ANY(ns_errstat.ns_es_tooshort, "error packet",
266 		" received incomplete");
267 	ANY(ns_errstat.ns_es_badcode, "error packet",
268 		" received of unknown type");
269 	for(j = 0; j < NS_ERR_MAX; j ++) {
270 		z = ns_errstat.ns_es_outhist[j];
271 		if (z && histoprint) {
272 			printf("Output Error Histogram:\n");
273 			histoprint = 0;
274 		}
275 		ns_erputil(z, ns_errstat.ns_es_codes[j]);
276 
277 	}
278 	histoprint = 1;
279 	for(j = 0; j < NS_ERR_MAX; j ++) {
280 		z = ns_errstat.ns_es_inhist[j];
281 		if (z && histoprint) {
282 			printf("Input Error Histogram:\n");
283 			histoprint = 0;
284 		}
285 		ns_erputil(z, ns_errstat.ns_es_codes[j]);
286 	}
287 }
288 
289 static void
290 ns_erputil(z, c)
291 	int z, c;
292 {
293 	int j;
294 	char codebuf[30];
295 	char *name, *where;
296 
297 	for(j = 0;; j ++) {
298 		if ((name = ns_errnames[j].name) == 0)
299 			break;
300 		if (ns_errnames[j].code == c)
301 			break;
302 	}
303 	if (name == 0)  {
304 		if (c > 01000)
305 			where = "in transit";
306 		else
307 			where = "at destination";
308 		sprintf(codebuf, "Unknown XNS error code 0%o", c);
309 		name = codebuf;
310 	} else
311 		where =  ns_errnames[j].where;
312 	ANY(z, name, where);
313 }
314 
315 static struct sockaddr_ns ssns = {AF_NS};
316 
317 static
318 char *ns_prpr(x)
319 	struct ns_addr *x;
320 {
321 	struct sockaddr_ns *sns = &ssns;
322 
323 	sns->sns_addr = *x;
324 	return(ns_print((struct sockaddr *)sns));
325 }
326