Annotation of src/usr.bin/netstat/ns.c, Revision 1.10
1.10 ! deraadt 1: /* $OpenBSD: ns.c,v 1.9 2002/06/09 04:07:10 jsyn 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.10 ! deraadt 41: static char *rcsid = "$OpenBSD: ns.c,v 1.9 2002/06/09 04:07:10 jsyn 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
1.10 ! deraadt 92: nsprotopr(u_long off, char *name)
1.1 deraadt 93: {
94: struct nspcb cb;
1.6 mpech 95: struct nspcb *prev, *next;
1.1 deraadt 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;
1.8 deraadt 125: } else if (isspp)
126: continue;
1.1 deraadt 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 ?
1.10 ! deraadt 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)");
1.1 deraadt 139: first = 0;
140: }
141: if (Aflag)
1.3 millert 142: printf("%8lx ", ppcb);
143: printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
1.10 ! deraadt 144: sockb.so_snd.sb_cc);
1.1 deraadt 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) \
1.3 millert 159: ((x) ? printf("\t%ld %s%s%s -- %s\n",x,y,plural(x),z,"x") : 0)
1.1 deraadt 160:
161: /*
162: * Dump SPP statistics structure.
163: */
164: void
1.10 ! deraadt 165: spp_stats(u_long off, char *name)
1.1 deraadt 166: {
167: struct spp_istat spp_istat;
168: #define sppstat spp_istat.newstats
169:
170: if (off == 0)
171: return;
172: kread(off, (char *)&spp_istat, sizeof (spp_istat));
173: printf("%s:\n", name);
1.3 millert 174: ANY((long)spp_istat.nonucn, "connection",
175: " dropped due to no new sockets ");
176: ANY((long)spp_istat.gonawy, "connection",
177: " terminated due to our end dying");
178: ANY((long)spp_istat.nonucn, "connection",
1.1 deraadt 179: " dropped due to inability to connect");
1.3 millert 180: ANY((long)spp_istat.noconn, "connection",
1.1 deraadt 181: " dropped due to inability to connect");
1.3 millert 182: ANY((long)spp_istat.notme, "connection",
1.1 deraadt 183: " incompleted due to mismatched id's");
1.3 millert 184: ANY((long)spp_istat.wrncon, "connection",
185: " dropped due to mismatched id's");
186: ANY((long)spp_istat.bdreas, "packet", " dropped out of sequence");
187: ANY((long)spp_istat.lstdup, "packet",
188: " duplicating the highest packet");
189: ANY((long)spp_istat.notyet, "packet",
190: " refused as exceeding allocation");
1.1 deraadt 191: ANY(sppstat.spps_connattempt, "connection", " initiated");
192: ANY(sppstat.spps_accepts, "connection", " accepted");
193: ANY(sppstat.spps_connects, "connection", " established");
194: ANY(sppstat.spps_drops, "connection", " dropped");
195: ANY(sppstat.spps_conndrops, "embryonic connection", " dropped");
196: ANY(sppstat.spps_closed, "connection", " closed (includes drops)");
197: ANY(sppstat.spps_segstimed, "packet", " where we tried to get rtt");
198: ANY(sppstat.spps_rttupdated, "time", " we got rtt");
199: ANY(sppstat.spps_delack, "delayed ack", " sent");
200: ANY(sppstat.spps_timeoutdrop, "connection", " dropped in rxmt timeout");
201: ANY(sppstat.spps_rexmttimeo, "retransmit timeout", "");
202: ANY(sppstat.spps_persisttimeo, "persist timeout", "");
203: ANY(sppstat.spps_keeptimeo, "keepalive timeout", "");
204: ANY(sppstat.spps_keepprobe, "keepalive probe", " sent");
205: ANY(sppstat.spps_keepdrops, "connection", " dropped in keepalive");
206: ANY(sppstat.spps_sndtotal, "total packet", " sent");
207: ANY(sppstat.spps_sndpack, "data packet", " sent");
208: ANY(sppstat.spps_sndbyte, "data byte", " sent");
209: ANY(sppstat.spps_sndrexmitpack, "data packet", " retransmitted");
210: ANY(sppstat.spps_sndrexmitbyte, "data byte", " retransmitted");
211: ANY(sppstat.spps_sndacks, "ack-only packet", " sent");
212: ANY(sppstat.spps_sndprobe, "window probe", " sent");
213: ANY(sppstat.spps_sndurg, "packet", " sent with URG only");
214: ANY(sppstat.spps_sndwinup, "window update-only packet", " sent");
215: ANY(sppstat.spps_sndctrl, "control (SYN|FIN|RST) packet", " sent");
216: ANY(sppstat.spps_sndvoid, "request", " to send a non-existant packet");
217: ANY(sppstat.spps_rcvtotal, "total packet", " received");
218: ANY(sppstat.spps_rcvpack, "packet", " received in sequence");
219: ANY(sppstat.spps_rcvbyte, "byte", " received in sequence");
220: ANY(sppstat.spps_rcvbadsum, "packet", " received with ccksum errs");
221: ANY(sppstat.spps_rcvbadoff, "packet", " received with bad offset");
222: ANY(sppstat.spps_rcvshort, "packet", " received too short");
223: ANY(sppstat.spps_rcvduppack, "duplicate-only packet", " received");
224: ANY(sppstat.spps_rcvdupbyte, "duplicate-only byte", " received");
225: ANY(sppstat.spps_rcvpartduppack, "packet", " with some duplicate data");
1.9 jsyn 226: ANY(sppstat.spps_rcvpartdupbyte, "duplicate byte", " in part-duplicate packet");
1.1 deraadt 227: ANY(sppstat.spps_rcvoopack, "out-of-order packet", " received");
228: ANY(sppstat.spps_rcvoobyte, "out-of-order byte", " received");
229: ANY(sppstat.spps_rcvpackafterwin, "packet", " with data after window");
230: ANY(sppstat.spps_rcvbyteafterwin, "byte", " rcvd after window");
231: ANY(sppstat.spps_rcvafterclose, "packet", " rcvd after 'close'");
232: ANY(sppstat.spps_rcvwinprobe, "rcvd window probe packet", "");
233: ANY(sppstat.spps_rcvdupack, "rcvd duplicate ack", "");
234: ANY(sppstat.spps_rcvacktoomuch, "rcvd ack", " for unsent data");
235: ANY(sppstat.spps_rcvackpack, "rcvd ack packet", "");
236: ANY(sppstat.spps_rcvackbyte, "byte", " acked by rcvd acks");
237: ANY(sppstat.spps_rcvwinupd, "rcvd window update packet", "");
238: }
239: #undef ANY
1.8 deraadt 240: #define ANY(x,y,z) ((x) ? printf("\t%d %s%s%s\n",x,y,plural(x),z) : 0)
1.1 deraadt 241:
242: /*
243: * Dump IDP statistics structure.
244: */
245: void
1.10 ! deraadt 246: idp_stats(u_long off, char *name)
1.1 deraadt 247: {
248: struct idpstat idpstat;
249:
250: if (off == 0)
251: return;
252: kread(off, (char *)&idpstat, sizeof (idpstat));
253: printf("%s:\n", name);
254: ANY(idpstat.idps_toosmall, "packet", " smaller than a header");
255: ANY(idpstat.idps_tooshort, "packet", " smaller than advertised");
256: ANY(idpstat.idps_badsum, "packet", " with bad checksums");
257: }
258:
259: static struct {
260: u_short code;
261: char *name;
262: char *where;
263: } ns_errnames[] = {
264: {0, "Unspecified Error", " at Destination"},
265: {1, "Bad Checksum", " at Destination"},
266: {2, "No Listener", " at Socket"},
267: {3, "Packet", " Refused due to lack of space at Destination"},
268: {01000, "Unspecified Error", " while gatewayed"},
269: {01001, "Bad Checksum", " while gatewayed"},
270: {01002, "Packet", " forwarded too many times"},
271: {01003, "Packet", " too large to be forwarded"},
272: {-1, 0, 0},
273: };
274:
275: /*
276: * Dump NS Error statistics structure.
277: */
278: /*ARGSUSED*/
279: void
1.10 ! deraadt 280: nserr_stats(u_long off, char *name)
1.1 deraadt 281: {
282: struct ns_errstat ns_errstat;
1.6 mpech 283: int j;
284: int histoprint = 1;
1.1 deraadt 285: int z;
286:
287: if (off == 0)
288: return;
289: kread(off, (char *)&ns_errstat, sizeof (ns_errstat));
290: printf("NS error statistics:\n");
291: ANY(ns_errstat.ns_es_error, "call", " to ns_error");
292: ANY(ns_errstat.ns_es_oldshort, "error",
293: " ignored due to insufficient addressing");
294: ANY(ns_errstat.ns_es_oldns_err, "error request",
295: " in response to error packets");
296: ANY(ns_errstat.ns_es_tooshort, "error packet",
297: " received incomplete");
298: ANY(ns_errstat.ns_es_badcode, "error packet",
299: " received of unknown type");
1.8 deraadt 300: for (j = 0; j < NS_ERR_MAX; j ++) {
1.1 deraadt 301: z = ns_errstat.ns_es_outhist[j];
302: if (z && histoprint) {
303: printf("Output Error Histogram:\n");
304: histoprint = 0;
305: }
306: ns_erputil(z, ns_errstat.ns_es_codes[j]);
307:
308: }
309: histoprint = 1;
1.8 deraadt 310: for (j = 0; j < NS_ERR_MAX; j ++) {
1.1 deraadt 311: z = ns_errstat.ns_es_inhist[j];
312: if (z && histoprint) {
313: printf("Input Error Histogram:\n");
314: histoprint = 0;
315: }
316: ns_erputil(z, ns_errstat.ns_es_codes[j]);
317: }
318: }
319:
320: static void
1.10 ! deraadt 321: ns_erputil(int z, int c)
1.1 deraadt 322: {
1.10 ! deraadt 323: char *name, *where;
! 324: char codebuf[30];
1.1 deraadt 325: int j;
326:
1.8 deraadt 327: for (j = 0;; j ++) {
1.1 deraadt 328: if ((name = ns_errnames[j].name) == 0)
329: break;
330: if (ns_errnames[j].code == c)
331: break;
332: }
1.8 deraadt 333: if (name == 0) {
1.1 deraadt 334: if (c > 01000)
335: where = "in transit";
336: else
337: where = "at destination";
1.5 deraadt 338: snprintf(codebuf, sizeof codebuf,
339: "Unknown XNS error code 0%o", c);
1.1 deraadt 340: name = codebuf;
341: } else
1.8 deraadt 342: where = ns_errnames[j].where;
1.1 deraadt 343: ANY(z, name, where);
344: }
345:
346: static struct sockaddr_ns ssns = {AF_NS};
347:
348: static
1.10 ! deraadt 349: char *ns_prpr(struct ns_addr *x)
1.1 deraadt 350: {
351: struct sockaddr_ns *sns = &ssns;
352:
353: sns->sns_addr = *x;
354: return(ns_print((struct sockaddr *)sns));
355: }