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