pfkey.c revision 1.1.4.2 1 1.1.4.2 yamt /* $NetBSD: pfkey.c,v 1.1.4.2 2012/04/17 00:09:37 yamt Exp $ */
2 1.1.4.2 yamt /* $KAME: ipsec.c,v 1.33 2003/07/25 09:54:32 itojun Exp $ */
3 1.1.4.2 yamt
4 1.1.4.2 yamt /*
5 1.1.4.2 yamt * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
6 1.1.4.2 yamt * All rights reserved.
7 1.1.4.2 yamt *
8 1.1.4.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.1.4.2 yamt * modification, are permitted provided that the following conditions
10 1.1.4.2 yamt * are met:
11 1.1.4.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.1.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.1.4.2 yamt * documentation and/or other materials provided with the distribution.
16 1.1.4.2 yamt * 3. Neither the name of the project nor the names of its contributors
17 1.1.4.2 yamt * may be used to endorse or promote products derived from this software
18 1.1.4.2 yamt * without specific prior written permission.
19 1.1.4.2 yamt *
20 1.1.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.1.4.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1.4.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1.4.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.1.4.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1.4.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1.4.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1.4.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1.4.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1.4.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1.4.2 yamt * SUCH DAMAGE.
31 1.1.4.2 yamt */
32 1.1.4.2 yamt
33 1.1.4.2 yamt /*
34 1.1.4.2 yamt * Copyright (c) 1983, 1988, 1993
35 1.1.4.2 yamt * The Regents of the University of California. All rights reserved.
36 1.1.4.2 yamt *
37 1.1.4.2 yamt * Redistribution and use in source and binary forms, with or without
38 1.1.4.2 yamt * modification, are permitted provided that the following conditions
39 1.1.4.2 yamt * are met:
40 1.1.4.2 yamt * 1. Redistributions of source code must retain the above copyright
41 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer.
42 1.1.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
43 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer in the
44 1.1.4.2 yamt * documentation and/or other materials provided with the distribution.
45 1.1.4.2 yamt * 3. Neither the name of the University nor the names of its contributors
46 1.1.4.2 yamt * may be used to endorse or promote products derived from this software
47 1.1.4.2 yamt * without specific prior written permission.
48 1.1.4.2 yamt *
49 1.1.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 1.1.4.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 1.1.4.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 1.1.4.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 1.1.4.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 1.1.4.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 1.1.4.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 1.1.4.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 1.1.4.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 1.1.4.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 1.1.4.2 yamt * SUCH DAMAGE.
60 1.1.4.2 yamt */
61 1.1.4.2 yamt
62 1.1.4.2 yamt #include <sys/cdefs.h>
63 1.1.4.2 yamt #ifndef lint
64 1.1.4.2 yamt #if 0
65 1.1.4.2 yamt static char sccsid[] = "from: @(#)inet.c 8.4 (Berkeley) 4/20/94";
66 1.1.4.2 yamt #else
67 1.1.4.2 yamt #ifdef __NetBSD__
68 1.1.4.2 yamt __RCSID("$NetBSD: pfkey.c,v 1.1.4.2 2012/04/17 00:09:37 yamt Exp $");
69 1.1.4.2 yamt #endif
70 1.1.4.2 yamt #endif
71 1.1.4.2 yamt #endif /* not lint */
72 1.1.4.2 yamt
73 1.1.4.2 yamt #include <sys/param.h>
74 1.1.4.2 yamt #include <sys/queue.h>
75 1.1.4.2 yamt #include <sys/socket.h>
76 1.1.4.2 yamt #include <sys/sysctl.h>
77 1.1.4.2 yamt
78 1.1.4.2 yamt #ifdef IPSEC
79 1.1.4.2 yamt #include <netipsec/keysock.h>
80 1.1.4.2 yamt #endif
81 1.1.4.2 yamt
82 1.1.4.2 yamt #include <err.h>
83 1.1.4.2 yamt #include <stdio.h>
84 1.1.4.2 yamt #include <string.h>
85 1.1.4.2 yamt #include <unistd.h>
86 1.1.4.2 yamt #include "netstat.h"
87 1.1.4.2 yamt
88 1.1.4.2 yamt #ifdef IPSEC
89 1.1.4.2 yamt
90 1.1.4.2 yamt static const char *pfkey_msgtypenames[] = {
91 1.1.4.2 yamt "reserved", "getspi", "update", "add", "delete",
92 1.1.4.2 yamt "get", "acquire", "register", "expire", "flush",
93 1.1.4.2 yamt "dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd",
94 1.1.4.2 yamt "x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush",
95 1.1.4.2 yamt "x_spdsetidx", "x_spdexpire", "x_spddelete2"
96 1.1.4.2 yamt };
97 1.1.4.2 yamt
98 1.1.4.2 yamt static const char *pfkey_msgtype_names(int);
99 1.1.4.2 yamt
100 1.1.4.2 yamt static const char *
101 1.1.4.2 yamt pfkey_msgtype_names(int x)
102 1.1.4.2 yamt {
103 1.1.4.2 yamt const int max =
104 1.1.4.2 yamt sizeof(pfkey_msgtypenames)/sizeof(pfkey_msgtypenames[0]);
105 1.1.4.2 yamt static char buf[20];
106 1.1.4.2 yamt
107 1.1.4.2 yamt if (x < max && pfkey_msgtypenames[x])
108 1.1.4.2 yamt return pfkey_msgtypenames[x];
109 1.1.4.2 yamt snprintf(buf, sizeof(buf), "#%d", x);
110 1.1.4.2 yamt return buf;
111 1.1.4.2 yamt }
112 1.1.4.2 yamt
113 1.1.4.2 yamt void
114 1.1.4.2 yamt pfkey_stats(u_long off, const char *name)
115 1.1.4.2 yamt {
116 1.1.4.2 yamt uint64_t pfkeystat[PFKEY_NSTATS];
117 1.1.4.2 yamt int first, type;
118 1.1.4.2 yamt
119 1.1.4.2 yamt if (use_sysctl) {
120 1.1.4.2 yamt size_t size = sizeof(pfkeystat);
121 1.1.4.2 yamt
122 1.1.4.2 yamt if (sysctlbyname("net.key.stats", pfkeystat, &size,
123 1.1.4.2 yamt NULL, 0) == -1)
124 1.1.4.2 yamt return;
125 1.1.4.2 yamt } else {
126 1.1.4.2 yamt warnx("%s stats not available via KVM.", name);
127 1.1.4.2 yamt return;
128 1.1.4.2 yamt }
129 1.1.4.2 yamt
130 1.1.4.2 yamt printf ("%s:\n", name);
131 1.1.4.2 yamt
132 1.1.4.2 yamt #define p(f, m) if (pfkeystat[f] || sflag <= 1) \
133 1.1.4.2 yamt printf(m, (unsigned long long)pfkeystat[f], plural(pfkeystat[f]))
134 1.1.4.2 yamt
135 1.1.4.2 yamt /* userland -> kernel */
136 1.1.4.2 yamt p(PFKEY_STAT_OUT_TOTAL, "\t%llu request%s sent from userland\n");
137 1.1.4.2 yamt p(PFKEY_STAT_OUT_BYTES, "\t%llu byte%s sent from userland\n");
138 1.1.4.2 yamt for (first = 1, type = 0; type < 256; type++) {
139 1.1.4.2 yamt if (pfkeystat[PFKEY_STAT_OUT_MSGTYPE + type] == 0)
140 1.1.4.2 yamt continue;
141 1.1.4.2 yamt if (first) {
142 1.1.4.2 yamt printf("\thistogram by message type:\n");
143 1.1.4.2 yamt first = 0;
144 1.1.4.2 yamt }
145 1.1.4.2 yamt printf("\t\t%s: %llu\n", pfkey_msgtype_names(type),
146 1.1.4.2 yamt (unsigned long long)pfkeystat[PFKEY_STAT_OUT_MSGTYPE + type]);
147 1.1.4.2 yamt }
148 1.1.4.2 yamt p(PFKEY_STAT_OUT_INVLEN, "\t%llu message%s with invalid length field\n");
149 1.1.4.2 yamt p(PFKEY_STAT_OUT_INVVER, "\t%llu message%s with invalid version field\n");
150 1.1.4.2 yamt p(PFKEY_STAT_OUT_INVMSGTYPE, "\t%llu message%s with invalid message type field\n");
151 1.1.4.2 yamt p(PFKEY_STAT_OUT_TOOSHORT, "\t%llu message%s too short\n");
152 1.1.4.2 yamt p(PFKEY_STAT_OUT_NOMEM, "\t%llu message%s with memory allocation failure\n");
153 1.1.4.2 yamt p(PFKEY_STAT_OUT_DUPEXT, "\t%llu message%s with duplicate extension\n");
154 1.1.4.2 yamt p(PFKEY_STAT_OUT_INVEXTTYPE, "\t%llu message%s with invalid extension type\n");
155 1.1.4.2 yamt p(PFKEY_STAT_OUT_INVSATYPE, "\t%llu message%s with invalid sa type\n");
156 1.1.4.2 yamt p(PFKEY_STAT_OUT_INVADDR, "\t%llu message%s with invalid address extension\n");
157 1.1.4.2 yamt
158 1.1.4.2 yamt /* kernel -> userland */
159 1.1.4.2 yamt p(PFKEY_STAT_IN_TOTAL, "\t%llu request%s sent to userland\n");
160 1.1.4.2 yamt p(PFKEY_STAT_IN_BYTES, "\t%llu byte%s sent to userland\n");
161 1.1.4.2 yamt for (first = 1, type = 0; type < 256; type++) {
162 1.1.4.2 yamt if (pfkeystat[PFKEY_STAT_IN_MSGTYPE + type] == 0)
163 1.1.4.2 yamt continue;
164 1.1.4.2 yamt if (first) {
165 1.1.4.2 yamt printf("\thistogram by message type:\n");
166 1.1.4.2 yamt first = 0;
167 1.1.4.2 yamt }
168 1.1.4.2 yamt printf("\t\t%s: %llu\n", pfkey_msgtype_names(type),
169 1.1.4.2 yamt (unsigned long long)pfkeystat[PFKEY_STAT_IN_MSGTYPE + type]);
170 1.1.4.2 yamt }
171 1.1.4.2 yamt p(PFKEY_STAT_IN_MSGTARGET + KEY_SENDUP_ONE,
172 1.1.4.2 yamt "\t%llu message%s toward single socket\n");
173 1.1.4.2 yamt p(PFKEY_STAT_IN_MSGTARGET + KEY_SENDUP_ALL,
174 1.1.4.2 yamt "\t%llu message%s toward all sockets\n");
175 1.1.4.2 yamt p(PFKEY_STAT_IN_MSGTARGET + KEY_SENDUP_REGISTERED,
176 1.1.4.2 yamt "\t%llu message%s toward registered sockets\n");
177 1.1.4.2 yamt p(PFKEY_STAT_IN_NOMEM, "\t%llu message%s with memory allocation failure\n");
178 1.1.4.2 yamt #undef p
179 1.1.4.2 yamt }
180 1.1.4.2 yamt #endif /*IPSEC*/
181