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