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