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