xref: /original-bsd/usr.bin/netstat/ns.c (revision 74d2773b)
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.13 (Berkeley) 03/01/91";
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 
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) \
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 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 	kvm_read(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 idp_stats(off, name)
210 	off_t off;
211 	char *name;
212 {
213 	struct idpstat idpstat;
214 
215 	if (off == 0)
216 		return;
217 	kvm_read(off, (char *)&idpstat, sizeof (idpstat));
218 	printf("%s:\n", name);
219 	ANY(idpstat.idps_toosmall, "packet", " smaller than a header");
220 	ANY(idpstat.idps_tooshort, "packet", " smaller than advertised");
221 	ANY(idpstat.idps_badsum, "packet", " with bad checksums");
222 }
223 
224 static	struct {
225 	u_short code;
226 	char *name;
227 	char *where;
228 } ns_errnames[] = {
229 	{0, "Unspecified Error", " at Destination"},
230 	{1, "Bad Checksum", " at Destination"},
231 	{2, "No Listener", " at Socket"},
232 	{3, "Packet", " Refused due to lack of space at Destination"},
233 	{01000, "Unspecified Error", " while gatewayed"},
234 	{01001, "Bad Checksum", " while gatewayed"},
235 	{01002, "Packet", " forwarded too many times"},
236 	{01003, "Packet", " too large to be forwarded"},
237 	{-1, 0, 0},
238 };
239 
240 /*
241  * Dump NS Error statistics structure.
242  */
243 /*ARGSUSED*/
244 nserr_stats(off, name)
245 	off_t off;
246 	char *name;
247 {
248 	struct ns_errstat ns_errstat;
249 	register int j;
250 	register int histoprint = 1;
251 	int z;
252 
253 	if (off == 0)
254 		return;
255 	kvm_read(off, (char *)&ns_errstat, sizeof (ns_errstat));
256 	printf("NS error statistics:\n");
257 	ANY(ns_errstat.ns_es_error, "call", " to ns_error");
258 	ANY(ns_errstat.ns_es_oldshort, "error",
259 		" ignored due to insufficient addressing");
260 	ANY(ns_errstat.ns_es_oldns_err, "error request",
261 		" in response to error packets");
262 	ANY(ns_errstat.ns_es_tooshort, "error packet",
263 		" received incomplete");
264 	ANY(ns_errstat.ns_es_badcode, "error packet",
265 		" received of unknown type");
266 	for(j = 0; j < NS_ERR_MAX; j ++) {
267 		z = ns_errstat.ns_es_outhist[j];
268 		if (z && histoprint) {
269 			printf("Output Error Histogram:\n");
270 			histoprint = 0;
271 		}
272 		ns_erputil(z, ns_errstat.ns_es_codes[j]);
273 
274 	}
275 	histoprint = 1;
276 	for(j = 0; j < NS_ERR_MAX; j ++) {
277 		z = ns_errstat.ns_es_inhist[j];
278 		if (z && histoprint) {
279 			printf("Input Error Histogram:\n");
280 			histoprint = 0;
281 		}
282 		ns_erputil(z, ns_errstat.ns_es_codes[j]);
283 	}
284 }
285 
286 ns_erputil(z, c)
287 {
288 	int j;
289 	char codebuf[30];
290 	char *name, *where;
291 
292 	for(j = 0;; j ++) {
293 		if ((name = ns_errnames[j].name) == 0)
294 			break;
295 		if (ns_errnames[j].code == c)
296 			break;
297 	}
298 	if (name == 0)  {
299 		if (c > 01000)
300 			where = "in transit";
301 		else
302 			where = "at destination";
303 		sprintf(codebuf, "Unknown XNS error code 0%o", c);
304 		name = codebuf;
305 	} else
306 		where =  ns_errnames[j].where;
307 	ANY(z, name, where);
308 }
309 
310 static struct sockaddr_ns ssns = {AF_NS};
311 
312 char *ns_prpr(x)
313 	struct ns_addr *x;
314 {
315 	struct sockaddr_ns *sns = &ssns;
316 	extern char *ns_print();
317 
318 	sns->sns_addr = *x;
319 	return(ns_print(sns));
320 }
321