xref: /freebsd/usr.bin/netstat/pfkey.c (revision ade9ccfe211ac8b27c58a4dc11a6cd1d33767ce0)
1100b98dbSKelly Yancey /*	$NetBSD: inet.c,v 1.35.2.1 1999/04/29 14:57:08 perry Exp $	*/
2100b98dbSKelly Yancey /*	$KAME: ipsec.c,v 1.25 2001/03/12 09:04:39 itojun Exp $	*/
365475bc8SDavid E. O'Brien /*-
4100b98dbSKelly Yancey  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
5100b98dbSKelly Yancey  * All rights reserved.
6100b98dbSKelly Yancey  *
7100b98dbSKelly Yancey  * Redistribution and use in source and binary forms, with or without
8100b98dbSKelly Yancey  * modification, are permitted provided that the following conditions
9100b98dbSKelly Yancey  * are met:
10100b98dbSKelly Yancey  * 1. Redistributions of source code must retain the above copyright
11100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer.
12100b98dbSKelly Yancey  * 2. Redistributions in binary form must reproduce the above copyright
13100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer in the
14100b98dbSKelly Yancey  *    documentation and/or other materials provided with the distribution.
15100b98dbSKelly Yancey  * 3. Neither the name of the project nor the names of its contributors
16100b98dbSKelly Yancey  *    may be used to endorse or promote products derived from this software
17100b98dbSKelly Yancey  *    without specific prior written permission.
18100b98dbSKelly Yancey  *
19100b98dbSKelly Yancey  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20100b98dbSKelly Yancey  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21100b98dbSKelly Yancey  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22100b98dbSKelly Yancey  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23100b98dbSKelly Yancey  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24100b98dbSKelly Yancey  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25100b98dbSKelly Yancey  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26100b98dbSKelly Yancey  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27100b98dbSKelly Yancey  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28100b98dbSKelly Yancey  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29100b98dbSKelly Yancey  * SUCH DAMAGE.
30100b98dbSKelly Yancey  */
3165475bc8SDavid E. O'Brien /*-
32100b98dbSKelly Yancey  * Copyright (c) 1983, 1988, 1993
33100b98dbSKelly Yancey  *	The Regents of the University of California.  All rights reserved.
34100b98dbSKelly Yancey  *
35100b98dbSKelly Yancey  * Redistribution and use in source and binary forms, with or without
36100b98dbSKelly Yancey  * modification, are permitted provided that the following conditions
37100b98dbSKelly Yancey  * are met:
38100b98dbSKelly Yancey  * 1. Redistributions of source code must retain the above copyright
39100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer.
40100b98dbSKelly Yancey  * 2. Redistributions in binary form must reproduce the above copyright
41100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer in the
42100b98dbSKelly Yancey  *    documentation and/or other materials provided with the distribution.
43100b98dbSKelly Yancey  * 4. Neither the name of the University nor the names of its contributors
44100b98dbSKelly Yancey  *    may be used to endorse or promote products derived from this software
45100b98dbSKelly Yancey  *    without specific prior written permission.
46100b98dbSKelly Yancey  *
47100b98dbSKelly Yancey  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48100b98dbSKelly Yancey  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49100b98dbSKelly Yancey  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50100b98dbSKelly Yancey  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51100b98dbSKelly Yancey  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52100b98dbSKelly Yancey  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53100b98dbSKelly Yancey  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54100b98dbSKelly Yancey  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55100b98dbSKelly Yancey  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56100b98dbSKelly Yancey  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57100b98dbSKelly Yancey  * SUCH DAMAGE.
58100b98dbSKelly Yancey  */
59100b98dbSKelly Yancey 
6065475bc8SDavid E. O'Brien #if 0
61100b98dbSKelly Yancey #ifndef lint
62100b98dbSKelly Yancey static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
63100b98dbSKelly Yancey #endif /* not lint */
6465475bc8SDavid E. O'Brien #endif
6565475bc8SDavid E. O'Brien 
6665475bc8SDavid E. O'Brien #include <sys/cdefs.h>
6765475bc8SDavid E. O'Brien __FBSDID("$FreeBSD$");
68100b98dbSKelly Yancey 
69100b98dbSKelly Yancey #include <sys/param.h>
70100b98dbSKelly Yancey #include <sys/queue.h>
71100b98dbSKelly Yancey #include <sys/socket.h>
72feda1a43SJohn Baldwin #include <sys/socketvar.h>
73100b98dbSKelly Yancey 
74100b98dbSKelly Yancey #include <netinet/in.h>
75100b98dbSKelly Yancey 
768409aedfSGeorge V. Neville-Neil #ifdef IPSEC
77100b98dbSKelly Yancey #include <netipsec/keysock.h>
78100b98dbSKelly Yancey #endif
79100b98dbSKelly Yancey 
807b95a1ebSYaroslav Tykhiy #include <stdint.h>
81100b98dbSKelly Yancey #include <stdio.h>
82821df508SXin LI #include <string.h>
83821df508SXin LI #include <unistd.h>
84*ade9ccfeSMarcel Moolenaar #include <stdbool.h>
85*ade9ccfeSMarcel Moolenaar #include <libxo/xo.h>
86100b98dbSKelly Yancey #include "netstat.h"
87100b98dbSKelly Yancey 
88100b98dbSKelly Yancey #ifdef IPSEC
89100b98dbSKelly Yancey 
90100b98dbSKelly Yancey static const char *pfkey_msgtypenames[] = {
91100b98dbSKelly Yancey 	"reserved", "getspi", "update", "add", "delete",
92100b98dbSKelly Yancey 	"get", "acquire", "register", "expire", "flush",
93100b98dbSKelly Yancey 	"dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd",
94100b98dbSKelly Yancey 	"x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush",
95100b98dbSKelly Yancey 	"x_spdsetidx", "x_spdexpire", "x_spddelete2"
96100b98dbSKelly Yancey };
97100b98dbSKelly Yancey 
98100b98dbSKelly Yancey static const char *pfkey_msgtype_names (int);
99100b98dbSKelly Yancey 
100100b98dbSKelly Yancey 
101100b98dbSKelly Yancey static const char *
102100b98dbSKelly Yancey pfkey_msgtype_names(int x)
103100b98dbSKelly Yancey {
104100b98dbSKelly Yancey 	const int max =
105100b98dbSKelly Yancey 	    sizeof(pfkey_msgtypenames)/sizeof(pfkey_msgtypenames[0]);
106100b98dbSKelly Yancey 	static char buf[20];
107100b98dbSKelly Yancey 
108100b98dbSKelly Yancey 	if (x < max && pfkey_msgtypenames[x])
109100b98dbSKelly Yancey 		return pfkey_msgtypenames[x];
110100b98dbSKelly Yancey 	snprintf(buf, sizeof(buf), "#%d", x);
111100b98dbSKelly Yancey 	return buf;
112100b98dbSKelly Yancey }
113100b98dbSKelly Yancey 
114100b98dbSKelly Yancey void
115feda1a43SJohn Baldwin pfkey_stats(u_long off, const char *name, int family __unused,
116feda1a43SJohn Baldwin     int proto __unused)
117100b98dbSKelly Yancey {
118100b98dbSKelly Yancey 	struct pfkeystat pfkeystat;
119100b98dbSKelly Yancey 	unsigned first, type;
120100b98dbSKelly Yancey 
121100b98dbSKelly Yancey 	if (off == 0)
122100b98dbSKelly Yancey 		return;
123*ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
124*ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
125db8c0879SAndrey V. Elsukov 	kread_counters(off, (char *)&pfkeystat, sizeof(pfkeystat));
126100b98dbSKelly Yancey 
127100b98dbSKelly Yancey #define	p(f, m) if (pfkeystat.f || sflag <= 1) \
128*ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)pfkeystat.f, plural(pfkeystat.f))
129100b98dbSKelly Yancey 
130100b98dbSKelly Yancey 	/* userland -> kernel */
131*ade9ccfeSMarcel Moolenaar 	p(out_total, "\t{:sent-requests//%ju} "
132*ade9ccfeSMarcel Moolenaar 	    "{N:/request%s sent from userland}\n");
133*ade9ccfeSMarcel Moolenaar 	p(out_bytes, "\t{:sent-bytes/%ju} "
134*ade9ccfeSMarcel Moolenaar 	    "{N:/byte%s sent from userland}\n");
135100b98dbSKelly Yancey 	for (first = 1, type = 0;
136100b98dbSKelly Yancey 	    type<sizeof(pfkeystat.out_msgtype)/sizeof(pfkeystat.out_msgtype[0]);
137100b98dbSKelly Yancey 	    type++) {
138100b98dbSKelly Yancey 		if (pfkeystat.out_msgtype[type] <= 0)
139100b98dbSKelly Yancey 			continue;
140100b98dbSKelly Yancey 		if (first) {
141*ade9ccfeSMarcel Moolenaar 			xo_open_list("output-histogram");
142*ade9ccfeSMarcel Moolenaar 			xo_emit("\t{T:histogram by message type}:\n");
143100b98dbSKelly Yancey 			first = 0;
144100b98dbSKelly Yancey 		}
145*ade9ccfeSMarcel Moolenaar 		xo_open_instance("output-histogram");
146*ade9ccfeSMarcel Moolenaar 		xo_emit("\t\t{k::type/%s}: {:count/%ju}\n",
147*ade9ccfeSMarcel Moolenaar 		    pfkey_msgtype_names(type),
1487b95a1ebSYaroslav Tykhiy 		    (uintmax_t)pfkeystat.out_msgtype[type]);
149*ade9ccfeSMarcel Moolenaar 		xo_close_instance("output-histogram");
150100b98dbSKelly Yancey 	}
151*ade9ccfeSMarcel Moolenaar 	if (!first)
152*ade9ccfeSMarcel Moolenaar 		xo_close_list("output-histogram");
153*ade9ccfeSMarcel Moolenaar 
154*ade9ccfeSMarcel Moolenaar 	p(out_invlen, "\t{:dropped-bad-length/%ju} "
155*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid length field}\n");
156*ade9ccfeSMarcel Moolenaar 	p(out_invver, "\t{:dropped-bad-version/%ju} "
157*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid version field}\n");
158*ade9ccfeSMarcel Moolenaar 	p(out_invmsgtype, "\t{:dropped-bad-type/%ju} "
159*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid message type field}\n");
160*ade9ccfeSMarcel Moolenaar 	p(out_tooshort, "\t{:dropped-too-short/%ju} "
161*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s too short}\n");
162*ade9ccfeSMarcel Moolenaar 	p(out_nomem, "\t{:dropped-no-memory/%ju} "
163*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with memory allocation failure}\n");
164*ade9ccfeSMarcel Moolenaar 	p(out_dupext, "\t{:dropped-duplicate-extension/%ju} "
165*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with duplicate extension}\n");
166*ade9ccfeSMarcel Moolenaar 	p(out_invexttype, "\t{:dropped-bad-extension/%ju} "
167*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid extension type}\n");
168*ade9ccfeSMarcel Moolenaar 	p(out_invsatype, "\t:dropped-bad-sa-type/%ju} "
169*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid sa type}\n");
170*ade9ccfeSMarcel Moolenaar 	p(out_invaddr, "\t{:dropped-bad-address-extension/%ju} "
171*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid address extension}\n");
172100b98dbSKelly Yancey 
173100b98dbSKelly Yancey 	/* kernel -> userland */
174*ade9ccfeSMarcel Moolenaar 	p(in_total, "\t{:received-requests/%ju} "
175*ade9ccfeSMarcel Moolenaar 	    "{N:/request%s sent to userland}\n");
176*ade9ccfeSMarcel Moolenaar 	p(in_bytes, "\t{:received-bytes/%ju} "
177*ade9ccfeSMarcel Moolenaar 	    "{N:/byte%s sent to userland}\n");
178100b98dbSKelly Yancey 	for (first = 1, type = 0;
179100b98dbSKelly Yancey 	    type < sizeof(pfkeystat.in_msgtype)/sizeof(pfkeystat.in_msgtype[0]);
180100b98dbSKelly Yancey 	    type++) {
181100b98dbSKelly Yancey 		if (pfkeystat.in_msgtype[type] <= 0)
182100b98dbSKelly Yancey 			continue;
183100b98dbSKelly Yancey 		if (first) {
184*ade9ccfeSMarcel Moolenaar 			xo_open_list("input-histogram");
185*ade9ccfeSMarcel Moolenaar 			xo_emit("\t{T:histogram by message type}:\n");
186100b98dbSKelly Yancey 			first = 0;
187100b98dbSKelly Yancey 		}
188*ade9ccfeSMarcel Moolenaar 		xo_open_instance("input-histogram");
189*ade9ccfeSMarcel Moolenaar 		xo_emit("\t\t{k:type/%s}: {:count/%ju}\n",
190*ade9ccfeSMarcel Moolenaar 		    pfkey_msgtype_names(type),
1917b95a1ebSYaroslav Tykhiy 		    (uintmax_t)pfkeystat.in_msgtype[type]);
192*ade9ccfeSMarcel Moolenaar 		xo_close_instance("input-histogram");
193100b98dbSKelly Yancey 	}
194*ade9ccfeSMarcel Moolenaar 	if (!first)
195*ade9ccfeSMarcel Moolenaar 		xo_close_list("input-histogram");
196*ade9ccfeSMarcel Moolenaar 	p(in_msgtarget[KEY_SENDUP_ONE], "\t{:received-one-socket/%ju} "
197*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s toward single socket}\n");
198*ade9ccfeSMarcel Moolenaar 	p(in_msgtarget[KEY_SENDUP_ALL], "\t{:received-all-sockets/%ju} "
199*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s toward all sockets}\n");
200100b98dbSKelly Yancey 	p(in_msgtarget[KEY_SENDUP_REGISTERED],
201*ade9ccfeSMarcel Moolenaar 	    "\t{:received-registered-sockets/%ju} "
202*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s toward registered sockets}\n");
203*ade9ccfeSMarcel Moolenaar 	p(in_nomem, "\t{:discarded-no-memory/%ju} "
204*ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with memory allocation failure}\n");
205100b98dbSKelly Yancey #undef p
206*ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
207100b98dbSKelly Yancey }
208100b98dbSKelly Yancey #endif /* IPSEC */
209