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