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