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