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