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