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