xref: /freebsd/usr.bin/netstat/pfkey.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
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 /*-
4*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
5*8a16b7a1SPedro F. Giffuni  *
6100b98dbSKelly Yancey  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
7100b98dbSKelly Yancey  * All rights reserved.
8100b98dbSKelly Yancey  *
9100b98dbSKelly Yancey  * Redistribution and use in source and binary forms, with or without
10100b98dbSKelly Yancey  * modification, are permitted provided that the following conditions
11100b98dbSKelly Yancey  * are met:
12100b98dbSKelly Yancey  * 1. Redistributions of source code must retain the above copyright
13100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer.
14100b98dbSKelly Yancey  * 2. Redistributions in binary form must reproduce the above copyright
15100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer in the
16100b98dbSKelly Yancey  *    documentation and/or other materials provided with the distribution.
17100b98dbSKelly Yancey  * 3. Neither the name of the project nor the names of its contributors
18100b98dbSKelly Yancey  *    may be used to endorse or promote products derived from this software
19100b98dbSKelly Yancey  *    without specific prior written permission.
20100b98dbSKelly Yancey  *
21100b98dbSKelly Yancey  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22100b98dbSKelly Yancey  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23100b98dbSKelly Yancey  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24100b98dbSKelly Yancey  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25100b98dbSKelly Yancey  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26100b98dbSKelly Yancey  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27100b98dbSKelly Yancey  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28100b98dbSKelly Yancey  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29100b98dbSKelly Yancey  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30100b98dbSKelly Yancey  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31100b98dbSKelly Yancey  * SUCH DAMAGE.
32100b98dbSKelly Yancey  */
3365475bc8SDavid E. O'Brien /*-
34100b98dbSKelly Yancey  * Copyright (c) 1983, 1988, 1993
35100b98dbSKelly Yancey  *	The Regents of the University of California.  All rights reserved.
36100b98dbSKelly Yancey  *
37100b98dbSKelly Yancey  * Redistribution and use in source and binary forms, with or without
38100b98dbSKelly Yancey  * modification, are permitted provided that the following conditions
39100b98dbSKelly Yancey  * are met:
40100b98dbSKelly Yancey  * 1. Redistributions of source code must retain the above copyright
41100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer.
42100b98dbSKelly Yancey  * 2. Redistributions in binary form must reproduce the above copyright
43100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer in the
44100b98dbSKelly Yancey  *    documentation and/or other materials provided with the distribution.
45fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
46100b98dbSKelly Yancey  *    may be used to endorse or promote products derived from this software
47100b98dbSKelly Yancey  *    without specific prior written permission.
48100b98dbSKelly Yancey  *
49100b98dbSKelly Yancey  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50100b98dbSKelly Yancey  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51100b98dbSKelly Yancey  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52100b98dbSKelly Yancey  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53100b98dbSKelly Yancey  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54100b98dbSKelly Yancey  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55100b98dbSKelly Yancey  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56100b98dbSKelly Yancey  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57100b98dbSKelly Yancey  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58100b98dbSKelly Yancey  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59100b98dbSKelly Yancey  * SUCH DAMAGE.
60100b98dbSKelly Yancey  */
61100b98dbSKelly Yancey 
62100b98dbSKelly Yancey #include <sys/param.h>
63100b98dbSKelly Yancey #include <sys/queue.h>
64100b98dbSKelly Yancey #include <sys/socket.h>
65feda1a43SJohn Baldwin #include <sys/socketvar.h>
66100b98dbSKelly Yancey 
67100b98dbSKelly Yancey #include <netinet/in.h>
68100b98dbSKelly Yancey 
698409aedfSGeorge V. Neville-Neil #ifdef IPSEC
70100b98dbSKelly Yancey #include <netipsec/keysock.h>
71100b98dbSKelly Yancey #endif
72100b98dbSKelly Yancey 
737b95a1ebSYaroslav Tykhiy #include <stdint.h>
74100b98dbSKelly Yancey #include <stdio.h>
75821df508SXin LI #include <string.h>
76821df508SXin LI #include <unistd.h>
77ade9ccfeSMarcel Moolenaar #include <stdbool.h>
78ade9ccfeSMarcel Moolenaar #include <libxo/xo.h>
79100b98dbSKelly Yancey #include "netstat.h"
80100b98dbSKelly Yancey 
81100b98dbSKelly Yancey #ifdef IPSEC
82100b98dbSKelly Yancey 
83100b98dbSKelly Yancey static const char *pfkey_msgtypenames[] = {
84100b98dbSKelly Yancey 	"reserved", "getspi", "update", "add", "delete",
85100b98dbSKelly Yancey 	"get", "acquire", "register", "expire", "flush",
86100b98dbSKelly Yancey 	"dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd",
87100b98dbSKelly Yancey 	"x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush",
88100b98dbSKelly Yancey 	"x_spdsetidx", "x_spdexpire", "x_spddelete2"
89100b98dbSKelly Yancey };
90100b98dbSKelly Yancey 
91100b98dbSKelly Yancey static const char *pfkey_msgtype_names (int);
92100b98dbSKelly Yancey 
93100b98dbSKelly Yancey 
94100b98dbSKelly Yancey static const char *
pfkey_msgtype_names(int x)95100b98dbSKelly Yancey pfkey_msgtype_names(int x)
96100b98dbSKelly Yancey {
97d19ba08aSMarcelo Araujo 	const int max = nitems(pfkey_msgtypenames);
98100b98dbSKelly Yancey 	static char buf[20];
99100b98dbSKelly Yancey 
100100b98dbSKelly Yancey 	if (x < max && pfkey_msgtypenames[x])
101100b98dbSKelly Yancey 		return pfkey_msgtypenames[x];
102100b98dbSKelly Yancey 	snprintf(buf, sizeof(buf), "#%d", x);
103100b98dbSKelly Yancey 	return buf;
104100b98dbSKelly Yancey }
105100b98dbSKelly Yancey 
106100b98dbSKelly Yancey void
pfkey_stats(u_long off,const char * name,int family __unused,int proto __unused)107feda1a43SJohn Baldwin pfkey_stats(u_long off, const char *name, int family __unused,
108feda1a43SJohn Baldwin     int proto __unused)
109100b98dbSKelly Yancey {
110100b98dbSKelly Yancey 	struct pfkeystat pfkeystat;
111100b98dbSKelly Yancey 	unsigned first, type;
112100b98dbSKelly Yancey 
113100b98dbSKelly Yancey 	if (off == 0)
114100b98dbSKelly Yancey 		return;
115ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
116ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
117db8c0879SAndrey V. Elsukov 	kread_counters(off, (char *)&pfkeystat, sizeof(pfkeystat));
118100b98dbSKelly Yancey 
119100b98dbSKelly Yancey #define	p(f, m) if (pfkeystat.f || sflag <= 1) \
120ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)pfkeystat.f, plural(pfkeystat.f))
121100b98dbSKelly Yancey 
122100b98dbSKelly Yancey 	/* userland -> kernel */
1234ffc6e48SMark Johnston 	p(out_total, "\t{:sent-requests/%ju} "
124ade9ccfeSMarcel Moolenaar 	    "{N:/request%s sent from userland}\n");
125ade9ccfeSMarcel Moolenaar 	p(out_bytes, "\t{:sent-bytes/%ju} "
126ade9ccfeSMarcel Moolenaar 	    "{N:/byte%s sent from userland}\n");
127100b98dbSKelly Yancey 	for (first = 1, type = 0;
128100b98dbSKelly Yancey 	    type<sizeof(pfkeystat.out_msgtype)/sizeof(pfkeystat.out_msgtype[0]);
129100b98dbSKelly Yancey 	    type++) {
130100b98dbSKelly Yancey 		if (pfkeystat.out_msgtype[type] <= 0)
131100b98dbSKelly Yancey 			continue;
132100b98dbSKelly Yancey 		if (first) {
133ade9ccfeSMarcel Moolenaar 			xo_open_list("output-histogram");
134ade9ccfeSMarcel Moolenaar 			xo_emit("\t{T:histogram by message type}:\n");
135100b98dbSKelly Yancey 			first = 0;
136100b98dbSKelly Yancey 		}
137ade9ccfeSMarcel Moolenaar 		xo_open_instance("output-histogram");
138ade9ccfeSMarcel Moolenaar 		xo_emit("\t\t{k::type/%s}: {:count/%ju}\n",
139ade9ccfeSMarcel Moolenaar 		    pfkey_msgtype_names(type),
1407b95a1ebSYaroslav Tykhiy 		    (uintmax_t)pfkeystat.out_msgtype[type]);
141ade9ccfeSMarcel Moolenaar 		xo_close_instance("output-histogram");
142100b98dbSKelly Yancey 	}
143ade9ccfeSMarcel Moolenaar 	if (!first)
144ade9ccfeSMarcel Moolenaar 		xo_close_list("output-histogram");
145ade9ccfeSMarcel Moolenaar 
146ade9ccfeSMarcel Moolenaar 	p(out_invlen, "\t{:dropped-bad-length/%ju} "
147ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid length field}\n");
148ade9ccfeSMarcel Moolenaar 	p(out_invver, "\t{:dropped-bad-version/%ju} "
149ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid version field}\n");
150ade9ccfeSMarcel Moolenaar 	p(out_invmsgtype, "\t{:dropped-bad-type/%ju} "
151ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid message type field}\n");
152ade9ccfeSMarcel Moolenaar 	p(out_tooshort, "\t{:dropped-too-short/%ju} "
153ade9ccfeSMarcel Moolenaar 	    "{N:/message%s too short}\n");
154ade9ccfeSMarcel Moolenaar 	p(out_nomem, "\t{:dropped-no-memory/%ju} "
155ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with memory allocation failure}\n");
156ade9ccfeSMarcel Moolenaar 	p(out_dupext, "\t{:dropped-duplicate-extension/%ju} "
157ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with duplicate extension}\n");
158ade9ccfeSMarcel Moolenaar 	p(out_invexttype, "\t{:dropped-bad-extension/%ju} "
159ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid extension type}\n");
1604ffc6e48SMark Johnston 	p(out_invsatype, "\t{:dropped-bad-sa-type/%ju} "
161ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid sa type}\n");
162ade9ccfeSMarcel Moolenaar 	p(out_invaddr, "\t{:dropped-bad-address-extension/%ju} "
163ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with invalid address extension}\n");
164100b98dbSKelly Yancey 
165100b98dbSKelly Yancey 	/* kernel -> userland */
166ade9ccfeSMarcel Moolenaar 	p(in_total, "\t{:received-requests/%ju} "
167ade9ccfeSMarcel Moolenaar 	    "{N:/request%s sent to userland}\n");
168ade9ccfeSMarcel Moolenaar 	p(in_bytes, "\t{:received-bytes/%ju} "
169ade9ccfeSMarcel Moolenaar 	    "{N:/byte%s sent to userland}\n");
170100b98dbSKelly Yancey 	for (first = 1, type = 0;
171100b98dbSKelly Yancey 	    type < sizeof(pfkeystat.in_msgtype)/sizeof(pfkeystat.in_msgtype[0]);
172100b98dbSKelly Yancey 	    type++) {
173100b98dbSKelly Yancey 		if (pfkeystat.in_msgtype[type] <= 0)
174100b98dbSKelly Yancey 			continue;
175100b98dbSKelly Yancey 		if (first) {
176ade9ccfeSMarcel Moolenaar 			xo_open_list("input-histogram");
177ade9ccfeSMarcel Moolenaar 			xo_emit("\t{T:histogram by message type}:\n");
178100b98dbSKelly Yancey 			first = 0;
179100b98dbSKelly Yancey 		}
180ade9ccfeSMarcel Moolenaar 		xo_open_instance("input-histogram");
181ade9ccfeSMarcel Moolenaar 		xo_emit("\t\t{k:type/%s}: {:count/%ju}\n",
182ade9ccfeSMarcel Moolenaar 		    pfkey_msgtype_names(type),
1837b95a1ebSYaroslav Tykhiy 		    (uintmax_t)pfkeystat.in_msgtype[type]);
184ade9ccfeSMarcel Moolenaar 		xo_close_instance("input-histogram");
185100b98dbSKelly Yancey 	}
186ade9ccfeSMarcel Moolenaar 	if (!first)
187ade9ccfeSMarcel Moolenaar 		xo_close_list("input-histogram");
188ade9ccfeSMarcel Moolenaar 	p(in_msgtarget[KEY_SENDUP_ONE], "\t{:received-one-socket/%ju} "
189ade9ccfeSMarcel Moolenaar 	    "{N:/message%s toward single socket}\n");
190ade9ccfeSMarcel Moolenaar 	p(in_msgtarget[KEY_SENDUP_ALL], "\t{:received-all-sockets/%ju} "
191ade9ccfeSMarcel Moolenaar 	    "{N:/message%s toward all sockets}\n");
192100b98dbSKelly Yancey 	p(in_msgtarget[KEY_SENDUP_REGISTERED],
193ade9ccfeSMarcel Moolenaar 	    "\t{:received-registered-sockets/%ju} "
194ade9ccfeSMarcel Moolenaar 	    "{N:/message%s toward registered sockets}\n");
195ade9ccfeSMarcel Moolenaar 	p(in_nomem, "\t{:discarded-no-memory/%ju} "
196ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with memory allocation failure}\n");
197100b98dbSKelly Yancey #undef p
198ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
199100b98dbSKelly Yancey }
200100b98dbSKelly Yancey #endif /* IPSEC */
201