Remove advertising header from all userland binaries.
[dragonfly.git] / usr.bin / netstat / ipx.c
1 /*
2  * Copyright (c) 2004, Robert N. M. Watson
3  * Copyright (c) 1983, 1988, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * @(#)ns.c     8.1 (Berkeley) 6/6/93
31  * $FreeBSD: src/usr.bin/netstat/ipx.c,v 1.13.2.1 2001/08/10 09:07:09 ru Exp $
32  */
33
34 #define _KERNEL_STRUCTURES
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
39 #include <sys/protosw.h>
40
41 #include <net/route.h>
42
43 #include <netinet/tcp_fsm.h>
44
45 #include <netipx/ipx.h>
46 #include <netipx/ipx_pcb.h>
47 #include <netipx/ipx_var.h>
48 #ifdef IPXERRORMSGS
49 #include <netipx/ipx_error.h>
50 #endif
51 #include <netipx/spx.h>
52 #include <netipx/spx_timer.h>
53 #include <netipx/spx_var.h>
54 #define SANAMES
55 #include <netipx/spx_debug.h>
56
57 #include <nlist.h>
58 #include <errno.h>
59 #include <stdio.h>
60 #include <string.h>
61 #include "netstat.h"
62
63 extern char *tcpstates[];
64
65 static char *ipx_prpr (struct ipx_addr *);
66
67 /*
68  * Print a summary of connections related to a Network Systems
69  * protocol.  For SPX, also give state of connection.
70  * Listening processes (aflag) are suppressed unless the
71  * -a (all) flag is specified.
72  */
73
74 void
75 ipxprotopr(u_long off, const char *name, int af1 __unused)
76 {
77         struct ipxpcbhead cb;
78         struct ipxpcb *ipxp;
79         struct ipxpcb ipxpcb;
80         struct spxpcb spxpcb;
81         struct socket sockb;
82         static int first = 1;
83         int isspx;
84
85         if (off == 0)
86                 return;
87
88         isspx = strcmp(name, "spx") == 0;
89         kread(off, (char *)&cb, sizeof (struct ipxpcbhead));
90         ipxp = LIST_FIRST(&cb);
91         while (ipxp != NULL) {
92                 u_long ppcb;
93
94                 kread((u_long)ipxp, (char *)&ipxpcb, sizeof (ipxpcb));
95                 ipxp = LIST_NEXT(&ipxpcb, ipxp_list);
96
97                 if (!aflag && ipx_nullhost(ipxpcb.ipxp_faddr) ) {
98                         continue;
99                 }
100                 kread((u_long)ipxpcb.ipxp_socket,
101                                 (char *)&sockb, sizeof (sockb));
102                 ppcb = (u_long) ipxpcb.ipxp_pcb;
103                 if (ppcb) {
104                         if (isspx) {
105                                 kread(ppcb, (char *)&spxpcb, sizeof (spxpcb));
106                         } else continue;
107                 } else
108                         if (isspx) continue;
109                 if (first) {
110                         printf("Active IPX connections");
111                         if (aflag)
112                                 printf(" (including servers)");
113                         putchar('\n');
114                         if (Aflag)
115                                 printf("%-8.8s ", "PCB");
116                         printf(Aflag ?
117                                 "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
118                                 "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
119                                 "Proto", "Recv-Q", "Send-Q",
120                                 "Local Address", "Foreign Address", "(state)");
121                         first = 0;
122                 }
123                 if (Aflag)
124                         printf("%8lx ", ppcb);
125                 printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.ssb_cc,
126                         sockb.so_snd.ssb_cc);
127                 printf(Aflag?" %-18.18s":" %-22.22s", ipx_prpr(&ipxpcb.ipxp_laddr));
128                 printf(Aflag?" %-18.18s":" %-22.22s", ipx_prpr(&ipxpcb.ipxp_faddr));
129                 if (isspx) {
130                         if (spxpcb.s_state >= TCP_NSTATES)
131                                 printf(" %d", spxpcb.s_state);
132                         else
133                                 printf(" %s", tcpstates[spxpcb.s_state]);
134                 }
135                 putchar('\n');
136         }
137 }
138
139 #define ANY(x,y,z) \
140             if (x || sflag <= 1) printf("\t%u %s%s%s\n", x, y, plural(x), z)
141 #define ANYl(x,y,z) \
142             if (x || sflag <= 1) printf("\t%lu %s%s%s\n", x, y, plural(x), z)
143
144 /*
145  * Dump SPX statistics structure.
146  */
147 void
148 spx_stats(u_long off, const char *name, int af1 __unused)
149 {
150         struct spx_istat spx_istat;
151 #define spxstat spx_istat.newstats
152
153         if (off == 0)
154                 return;
155         kread(off, (char *)&spx_istat, sizeof (spx_istat));
156         printf("%s:\n", name);
157         ANY(spx_istat.nonucn, "connection", " dropped due to no new sockets ");
158         ANY(spx_istat.gonawy, "connection", " terminated due to our end dying");
159         ANY(spx_istat.nonucn, "connection",
160             " dropped due to inability to connect");
161         ANY(spx_istat.noconn, "connection",
162             " dropped due to inability to connect");
163         ANY(spx_istat.notme, "connection",
164             " incompleted due to mismatched id's");
165         ANY(spx_istat.wrncon, "connection", " dropped due to mismatched id's");
166         ANY(spx_istat.bdreas, "packet", " dropped out of sequence");
167         ANY(spx_istat.lstdup, "packet", " duplicating the highest packet");
168         ANY(spx_istat.notyet, "packet", " refused as exceeding allocation");
169         ANYl(spxstat.spxs_connattempt, "connection", " initiated");
170         ANYl(spxstat.spxs_accepts, "connection", " accepted");
171         ANYl(spxstat.spxs_connects, "connection", " established");
172         ANYl(spxstat.spxs_drops, "connection", " dropped");
173         ANYl(spxstat.spxs_conndrops, "embryonic connection", " dropped");
174         ANYl(spxstat.spxs_closed, "connection", " closed (includes drops)");
175         ANYl(spxstat.spxs_segstimed, "packet", " where we tried to get rtt");
176         ANYl(spxstat.spxs_rttupdated, "time", " we got rtt");
177         ANYl(spxstat.spxs_delack, "delayed ack", " sent");
178         ANYl(spxstat.spxs_timeoutdrop, "connection",
179             " dropped in rxmt timeout");
180         ANYl(spxstat.spxs_rexmttimeo, "retransmit timeout", "");
181         ANYl(spxstat.spxs_persisttimeo, "persist timeout", "");
182         ANYl(spxstat.spxs_keeptimeo, "keepalive timeout", "");
183         ANYl(spxstat.spxs_keepprobe, "keepalive probe", " sent");
184         ANYl(spxstat.spxs_keepdrops, "connection", " dropped in keepalive");
185         ANYl(spxstat.spxs_sndtotal, "total packet", " sent");
186         ANYl(spxstat.spxs_sndpack, "data packet", " sent");
187         ANYl(spxstat.spxs_sndbyte, "data byte", " sent");
188         ANYl(spxstat.spxs_sndrexmitpack, "data packet", " retransmitted");
189         ANYl(spxstat.spxs_sndrexmitbyte, "data byte", " retransmitted");
190         ANYl(spxstat.spxs_sndacks, "ack-only packet", " sent");
191         ANYl(spxstat.spxs_sndprobe, "window probe", " sent");
192         ANYl(spxstat.spxs_sndurg, "packet", " sent with URG only");
193         ANYl(spxstat.spxs_sndwinup, "window update-only packet", " sent");
194         ANYl(spxstat.spxs_sndctrl, "control (SYN|FIN|RST) packet", " sent");
195         ANYl(spxstat.spxs_sndvoid, "request", " to send a non-existent packet");
196         ANYl(spxstat.spxs_rcvtotal, "total packet", " received");
197         ANYl(spxstat.spxs_rcvpack, "packet", " received in sequence");
198         ANYl(spxstat.spxs_rcvbyte, "byte", " received in sequence");
199         ANYl(spxstat.spxs_rcvbadsum, "packet", " received with ccksum errs");
200         ANYl(spxstat.spxs_rcvbadoff, "packet", " received with bad offset");
201         ANYl(spxstat.spxs_rcvshort, "packet", " received too short");
202         ANYl(spxstat.spxs_rcvduppack, "duplicate-only packet", " received");
203         ANYl(spxstat.spxs_rcvdupbyte, "duplicate-only byte", " received");
204         ANYl(spxstat.spxs_rcvpartduppack, "packet",
205             " with some duplicate data");
206         ANYl(spxstat.spxs_rcvpartdupbyte, "dup. byte", " in part-dup. packet");
207         ANYl(spxstat.spxs_rcvoopack, "out-of-order packet", " received");
208         ANYl(spxstat.spxs_rcvoobyte, "out-of-order byte", " received");
209         ANYl(spxstat.spxs_rcvpackafterwin, "packet", " with data after window");
210         ANYl(spxstat.spxs_rcvbyteafterwin, "byte", " rcvd after window");
211         ANYl(spxstat.spxs_rcvafterclose, "packet", " rcvd after 'close'");
212         ANYl(spxstat.spxs_rcvwinprobe, "rcvd window probe packet", "");
213         ANYl(spxstat.spxs_rcvdupack, "rcvd duplicate ack", "");
214         ANYl(spxstat.spxs_rcvacktoomuch, "rcvd ack", " for unsent data");
215         ANYl(spxstat.spxs_rcvackpack, "rcvd ack packet", "");
216         ANYl(spxstat.spxs_rcvackbyte, "byte", " acked by rcvd acks");
217         ANYl(spxstat.spxs_rcvwinupd, "rcvd window update packet", "");
218 }
219
220 /*
221  * Dump IPX statistics structure.
222  */
223 void
224 ipx_stats(u_long off, const char *name, int af1 __unused)
225 {
226         struct ipxstat ipxstat;
227
228         if (off == 0)
229                 return;
230         kread(off, (char *)&ipxstat, sizeof (ipxstat));
231         printf("%s:\n", name);
232         ANYl(ipxstat.ipxs_total, "total packet", " received");
233         ANYl(ipxstat.ipxs_badsum, "packet", " with bad checksums");
234         ANYl(ipxstat.ipxs_tooshort, "packet", " smaller than advertised");
235         ANYl(ipxstat.ipxs_toosmall, "packet", " smaller than a header");
236         ANYl(ipxstat.ipxs_forward, "packet", " forwarded");
237         ANYl(ipxstat.ipxs_cantforward, "packet", " not forwardable");
238         ANYl(ipxstat.ipxs_delivered, "packet", " for this host");
239         ANYl(ipxstat.ipxs_localout, "packet", " sent from this host");
240         ANYl(ipxstat.ipxs_odropped, "packet", " dropped due to no bufs, etc.");
241         ANYl(ipxstat.ipxs_noroute, "packet", " discarded due to no route");
242         ANYl(ipxstat.ipxs_mtutoosmall, "packet", " too big");
243 }
244
245 #ifdef IPXERRORMSGS
246 static  struct {
247         u_short code;
248         char *name;
249         char *where;
250 } ipx_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},
260 };
261
262 /*
263  * Dump IPX Error statistics structure.
264  */
265 /*ARGSUSED*/
266 void
267 ipxerr_stats(u_long off, char *name, int af __unused)
268 {
269         struct ipx_errstat ipx_errstat;
270         int j;
271         int histoprint = 1;
272         int z;
273
274         if (off == 0)
275                 return;
276         kread(off, (char *)&ipx_errstat, sizeof (ipx_errstat));
277         printf("IPX error statistics:\n");
278         ANY(ipx_errstat.ipx_es_error, "call", " to ipx_error");
279         ANY(ipx_errstat.ipx_es_oldshort, "error",
280                 " ignored due to insufficient addressing");
281         ANY(ipx_errstat.ipx_es_oldipx_err, "error request",
282                 " in response to error packets");
283         ANY(ipx_errstat.ipx_es_tooshort, "error packet",
284                 " received incomplete");
285         ANY(ipx_errstat.ipx_es_badcode, "error packet",
286                 " received of unknown type");
287         for(j = 0; j < IPX_ERR_MAX; j ++) {
288                 z = ipx_errstat.ipx_es_outhist[j];
289                 if (z && histoprint) {
290                         printf("Output Error Histogram:\n");
291                         histoprint = 0;
292                 }
293                 ipx_erputil(z, ipx_errstat.ipx_es_codes[j]);
294         }
295         histoprint = 1;
296         for(j = 0; j < IPX_ERR_MAX; j ++) {
297                 z = ipx_errstat.ipx_es_inhist[j];
298                 if (z && histoprint) {
299                         printf("Input Error Histogram:\n");
300                         histoprint = 0;
301                 }
302                 ipx_erputil(z, ipx_errstat.ipx_es_codes[j]);
303         }
304 }
305
306 static void
307 ipx_erputil(int z, int c)
308 {
309         int j;
310         char codebuf[30];
311         char *name, *where;
312
313         for(j = 0;; j ++) {
314                 if ((name = ipx_errnames[j].name) == NULL)
315                         break;
316                 if (ipx_errnames[j].code == c)
317                         break;
318         }
319         if (name == NULL)  {
320                 if (c > 01000)
321                         where = "in transit";
322                 else
323                         where = "at destination";
324                 sprintf(codebuf, "Unknown IPX error code 0%o", c);
325                 name = codebuf;
326         } else
327                 where =  ipx_errnames[j].where;
328         ANY(z, name, where);
329 }
330 #endif /* IPXERRORMSGS */
331
332 static struct sockaddr_ipx ssipx = { .sipx_family = AF_IPX };
333
334 static
335 char *ipx_prpr(struct ipx_addr *x)
336 {
337         struct sockaddr_ipx *sipx = &ssipx;
338
339         sipx->sipx_addr = *x;
340         return(ipx_print((struct sockaddr *)sipx));
341 }