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