1153809Skbyanc/*	$NetBSD: inet.c,v 1.35.2.1 1999/04/29 14:57:08 perry Exp $	*/
2153809Skbyanc/*	$KAME: ipsec.c,v 1.25 2001/03/12 09:04:39 itojun Exp $	*/
3175061Sobrien/*-
4153809Skbyanc * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
5153809Skbyanc * All rights reserved.
6175061Sobrien *
7153809Skbyanc * Redistribution and use in source and binary forms, with or without
8153809Skbyanc * modification, are permitted provided that the following conditions
9153809Skbyanc * are met:
10153809Skbyanc * 1. Redistributions of source code must retain the above copyright
11153809Skbyanc *    notice, this list of conditions and the following disclaimer.
12153809Skbyanc * 2. Redistributions in binary form must reproduce the above copyright
13153809Skbyanc *    notice, this list of conditions and the following disclaimer in the
14153809Skbyanc *    documentation and/or other materials provided with the distribution.
15153809Skbyanc * 3. Neither the name of the project nor the names of its contributors
16153809Skbyanc *    may be used to endorse or promote products derived from this software
17153809Skbyanc *    without specific prior written permission.
18175061Sobrien *
19153809Skbyanc * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20153809Skbyanc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21153809Skbyanc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22153809Skbyanc * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23153809Skbyanc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24153809Skbyanc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25153809Skbyanc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26153809Skbyanc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27153809Skbyanc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28153809Skbyanc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29153809Skbyanc * SUCH DAMAGE.
30153809Skbyanc */
31175061Sobrien/*-
32153809Skbyanc * Copyright (c) 1983, 1988, 1993
33153809Skbyanc *	The Regents of the University of California.  All rights reserved.
34153809Skbyanc *
35153809Skbyanc * Redistribution and use in source and binary forms, with or without
36153809Skbyanc * modification, are permitted provided that the following conditions
37153809Skbyanc * are met:
38153809Skbyanc * 1. Redistributions of source code must retain the above copyright
39153809Skbyanc *    notice, this list of conditions and the following disclaimer.
40153809Skbyanc * 2. Redistributions in binary form must reproduce the above copyright
41153809Skbyanc *    notice, this list of conditions and the following disclaimer in the
42153809Skbyanc *    documentation and/or other materials provided with the distribution.
43153809Skbyanc * 4. Neither the name of the University nor the names of its contributors
44153809Skbyanc *    may be used to endorse or promote products derived from this software
45153809Skbyanc *    without specific prior written permission.
46153809Skbyanc *
47153809Skbyanc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48153809Skbyanc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49153809Skbyanc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50153809Skbyanc * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51153809Skbyanc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52153809Skbyanc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53153809Skbyanc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54153809Skbyanc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55153809Skbyanc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56153809Skbyanc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57153809Skbyanc * SUCH DAMAGE.
58153809Skbyanc */
59153809Skbyanc
60175061Sobrien#if 0
61153809Skbyanc#ifndef lint
62153809Skbyancstatic char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
63153809Skbyanc#endif /* not lint */
64175061Sobrien#endif
65153809Skbyanc
66175061Sobrien#include <sys/cdefs.h>
67175061Sobrien__FBSDID("$FreeBSD$");
68175061Sobrien
69153809Skbyanc#include <sys/param.h>
70153809Skbyanc#include <sys/queue.h>
71153809Skbyanc#include <sys/socket.h>
72171465Sjhb#include <sys/socketvar.h>
73153809Skbyanc
74153809Skbyanc#include <netinet/in.h>
75153809Skbyanc
76171135Sgnn#ifdef IPSEC
77153809Skbyanc#include <netipsec/keysock.h>
78153809Skbyanc#endif
79153809Skbyanc
80160787Syar#include <stdint.h>
81153809Skbyanc#include <stdio.h>
82200462Sdelphij#include <string.h>
83200462Sdelphij#include <unistd.h>
84153809Skbyanc#include "netstat.h"
85153809Skbyanc
86153809Skbyanc#ifdef IPSEC
87153809Skbyanc
88153809Skbyancstatic const char *pfkey_msgtypenames[] = {
89153809Skbyanc	"reserved", "getspi", "update", "add", "delete",
90153809Skbyanc	"get", "acquire", "register", "expire", "flush",
91153809Skbyanc	"dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd",
92153809Skbyanc	"x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush",
93153809Skbyanc	"x_spdsetidx", "x_spdexpire", "x_spddelete2"
94153809Skbyanc};
95153809Skbyanc
96153809Skbyancstatic const char *pfkey_msgtype_names (int);
97153809Skbyanc
98153809Skbyanc
99153809Skbyancstatic const char *
100153809Skbyancpfkey_msgtype_names(int x)
101153809Skbyanc{
102153809Skbyanc	const int max =
103153809Skbyanc	    sizeof(pfkey_msgtypenames)/sizeof(pfkey_msgtypenames[0]);
104153809Skbyanc	static char buf[20];
105153809Skbyanc
106153809Skbyanc	if (x < max && pfkey_msgtypenames[x])
107153809Skbyanc		return pfkey_msgtypenames[x];
108153809Skbyanc	snprintf(buf, sizeof(buf), "#%d", x);
109153809Skbyanc	return buf;
110153809Skbyanc}
111153809Skbyanc
112153809Skbyancvoid
113171465Sjhbpfkey_stats(u_long off, const char *name, int family __unused,
114171465Sjhb    int proto __unused)
115153809Skbyanc{
116153809Skbyanc	struct pfkeystat pfkeystat;
117153809Skbyanc	unsigned first, type;
118153809Skbyanc
119153809Skbyanc	if (off == 0)
120153809Skbyanc		return;
121153809Skbyanc	printf ("%s:\n", name);
122253088Sae	kread_counters(off, (char *)&pfkeystat, sizeof(pfkeystat));
123153809Skbyanc
124153809Skbyanc#define	p(f, m) if (pfkeystat.f || sflag <= 1) \
125160787Syar    printf(m, (uintmax_t)pfkeystat.f, plural(pfkeystat.f))
126153809Skbyanc
127153809Skbyanc	/* userland -> kernel */
128160787Syar	p(out_total, "\t%ju request%s sent from userland\n");
129160787Syar	p(out_bytes, "\t%ju byte%s sent from userland\n");
130153809Skbyanc	for (first = 1, type = 0;
131153809Skbyanc	     type < sizeof(pfkeystat.out_msgtype)/sizeof(pfkeystat.out_msgtype[0]);
132153809Skbyanc	     type++) {
133153809Skbyanc		if (pfkeystat.out_msgtype[type] <= 0)
134153809Skbyanc			continue;
135153809Skbyanc		if (first) {
136153809Skbyanc			printf("\thistogram by message type:\n");
137153809Skbyanc			first = 0;
138153809Skbyanc		}
139160787Syar		printf("\t\t%s: %ju\n", pfkey_msgtype_names(type),
140160787Syar			(uintmax_t)pfkeystat.out_msgtype[type]);
141153809Skbyanc	}
142160787Syar	p(out_invlen, "\t%ju message%s with invalid length field\n");
143160787Syar	p(out_invver, "\t%ju message%s with invalid version field\n");
144160787Syar	p(out_invmsgtype, "\t%ju message%s with invalid message type field\n");
145160787Syar	p(out_tooshort, "\t%ju message%s too short\n");
146160787Syar	p(out_nomem, "\t%ju message%s with memory allocation failure\n");
147160787Syar	p(out_dupext, "\t%ju message%s with duplicate extension\n");
148160787Syar	p(out_invexttype, "\t%ju message%s with invalid extension type\n");
149160787Syar	p(out_invsatype, "\t%ju message%s with invalid sa type\n");
150160787Syar	p(out_invaddr, "\t%ju message%s with invalid address extension\n");
151153809Skbyanc
152153809Skbyanc	/* kernel -> userland */
153160787Syar	p(in_total, "\t%ju request%s sent to userland\n");
154160787Syar	p(in_bytes, "\t%ju byte%s sent to userland\n");
155153809Skbyanc	for (first = 1, type = 0;
156153809Skbyanc	     type < sizeof(pfkeystat.in_msgtype)/sizeof(pfkeystat.in_msgtype[0]);
157153809Skbyanc	     type++) {
158153809Skbyanc		if (pfkeystat.in_msgtype[type] <= 0)
159153809Skbyanc			continue;
160153809Skbyanc		if (first) {
161153809Skbyanc			printf("\thistogram by message type:\n");
162153809Skbyanc			first = 0;
163153809Skbyanc		}
164160787Syar		printf("\t\t%s: %ju\n", pfkey_msgtype_names(type),
165160787Syar			(uintmax_t)pfkeystat.in_msgtype[type]);
166153809Skbyanc	}
167153809Skbyanc	p(in_msgtarget[KEY_SENDUP_ONE],
168160787Syar	    "\t%ju message%s toward single socket\n");
169153809Skbyanc	p(in_msgtarget[KEY_SENDUP_ALL],
170160787Syar	    "\t%ju message%s toward all sockets\n");
171153809Skbyanc	p(in_msgtarget[KEY_SENDUP_REGISTERED],
172160787Syar	    "\t%ju message%s toward registered sockets\n");
173160787Syar	p(in_nomem, "\t%ju message%s with memory allocation failure\n");
174153809Skbyanc#undef p
175153809Skbyanc}
176153809Skbyanc#endif /* IPSEC */
177