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