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