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