1 /* $NetBSD: inet.c,v 1.35.2.1 1999/04/29 14:57:08 perry Exp $ */ 2 /* $KAME: ipsec.c,v 1.25 2001/03/12 09:04:39 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1983, 1988, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 */ 65 66 #include <sys/cdefs.h> 67 #ifndef lint 68 #if 0 69 static char sccsid[] = "from: @(#)inet.c 8.4 (Berkeley) 4/20/94"; 70 #else 71 static const char rcsid[] = 72 "$FreeBSD$"; 73 #endif 74 #endif /* not lint */ 75 76 #include <sys/param.h> 77 #include <sys/queue.h> 78 #include <sys/socket.h> 79 80 #include <netinet/in.h> 81 82 #ifdef IPSEC 83 #include <netinet6/ipsec.h> 84 #include <netkey/keysock.h> 85 #endif 86 87 #include <stdio.h> 88 #include <string.h> 89 #include <unistd.h> 90 #include "netstat.h" 91 92 #ifdef IPSEC 93 struct val2str { 94 int val; 95 const char *str; 96 }; 97 98 static struct val2str ipsec_ahnames[] = { 99 { SADB_AALG_NONE, "none", }, 100 { SADB_AALG_MD5HMAC, "hmac-md5", }, 101 { SADB_AALG_SHA1HMAC, "hmac-sha1", }, 102 { SADB_X_AALG_MD5, "md5", }, 103 { SADB_X_AALG_SHA, "sha", }, 104 { SADB_X_AALG_NULL, "null", }, 105 #ifdef SADB_X_AALG_SHA2_256 106 { SADB_X_AALG_SHA2_256, "hmac-sha2-256", }, 107 #endif 108 #ifdef SADB_X_AALG_SHA2_384 109 { SADB_X_AALG_SHA2_384, "hmac-sha2-384", }, 110 #endif 111 #ifdef SADB_X_AALG_SHA2_512 112 { SADB_X_AALG_SHA2_512, "hmac-sha2-512", }, 113 #endif 114 { -1, NULL }, 115 }; 116 117 static struct val2str ipsec_espnames[] = { 118 { SADB_EALG_NONE, "none", }, 119 { SADB_EALG_DESCBC, "des-cbc", }, 120 { SADB_EALG_3DESCBC, "3des-cbc", }, 121 { SADB_EALG_NULL, "null", }, 122 { SADB_X_EALG_CAST128CBC, "cast128-cbc", }, 123 { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", }, 124 #ifdef SADB_X_EALG_RIJNDAELCBC 125 { SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", }, 126 #endif 127 { -1, NULL }, 128 }; 129 130 static struct val2str ipsec_compnames[] = { 131 { SADB_X_CALG_NONE, "none", }, 132 { SADB_X_CALG_OUI, "oui", }, 133 { SADB_X_CALG_DEFLATE, "deflate", }, 134 { SADB_X_CALG_LZS, "lzs", }, 135 { -1, NULL }, 136 }; 137 138 static const char *pfkey_msgtypenames[] = { 139 "reserved", "getspi", "update", "add", "delete", 140 "get", "acquire", "register", "expire", "flush", 141 "dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd", 142 "x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush", 143 "x_spdsetidx", "x_spdexpire", "x_spddelete2" 144 }; 145 146 static struct ipsecstat ipsecstat; 147 148 static void print_ipsecstats (void); 149 static const char *pfkey_msgtype_names (int); 150 static void ipsec_hist (const u_quad_t *, size_t, const struct val2str *, 151 const char *); 152 153 /* 154 * Dump IPSEC statistics structure. 155 */ 156 static void 157 ipsec_hist(const u_quad_t *hist, size_t histmax, const struct val2str *name, 158 const char *title) 159 { 160 int first; 161 size_t proto; 162 const struct val2str *p; 163 164 first = 1; 165 for (proto = 0; proto < histmax; proto++) { 166 if (hist[proto] <= 0) 167 continue; 168 if (first) { 169 printf("\t%s histogram:\n", title); 170 first = 0; 171 } 172 for (p = name; p && p->str; p++) { 173 if (p->val == (int)proto) 174 break; 175 } 176 if (p && p->str) { 177 printf("\t\t%s: %llu\n", p->str, (unsigned long long)hist[proto]); 178 } else { 179 printf("\t\t#%ld: %llu\n", (long)proto, 180 (unsigned long long)hist[proto]); 181 } 182 } 183 } 184 185 static void 186 print_ipsecstats(void) 187 { 188 #define p(f, m) if (ipsecstat.f || sflag <= 1) \ 189 printf(m, (unsigned long long)ipsecstat.f, plural(ipsecstat.f)) 190 #define hist(f, n, t) \ 191 ipsec_hist((f), sizeof(f)/sizeof(f[0]), (n), (t)); 192 193 p(in_success, "\t%llu inbound packet%s processed successfully\n"); 194 p(in_polvio, "\t%llu inbound packet%s violated process security " 195 "policy\n"); 196 p(in_nosa, "\t%llu inbound packet%s with no SA available\n"); 197 p(in_inval, "\t%llu invalid inbound packet%s\n"); 198 p(in_nomem, "\t%llu inbound packet%s failed due to insufficient memory\n"); 199 p(in_badspi, "\t%llu inbound packet%s failed getting SPI\n"); 200 p(in_ahreplay, "\t%llu inbound packet%s failed on AH replay check\n"); 201 p(in_espreplay, "\t%llu inbound packet%s failed on ESP replay check\n"); 202 p(in_ahauthsucc, "\t%llu inbound packet%s considered authentic\n"); 203 p(in_ahauthfail, "\t%llu inbound packet%s failed on authentication\n"); 204 hist(ipsecstat.in_ahhist, ipsec_ahnames, "AH input"); 205 hist(ipsecstat.in_esphist, ipsec_espnames, "ESP input"); 206 hist(ipsecstat.in_comphist, ipsec_compnames, "IPComp input"); 207 208 p(out_success, "\t%llu outbound packet%s processed successfully\n"); 209 p(out_polvio, "\t%llu outbound packet%s violated process security " 210 "policy\n"); 211 p(out_nosa, "\t%llu outbound packet%s with no SA available\n"); 212 p(out_inval, "\t%llu invalid outbound packet%s\n"); 213 p(out_nomem, "\t%llu outbound packet%s failed due to insufficient memory\n"); 214 p(out_noroute, "\t%llu outbound packet%s with no route\n"); 215 hist(ipsecstat.out_ahhist, ipsec_ahnames, "AH output"); 216 hist(ipsecstat.out_esphist, ipsec_espnames, "ESP output"); 217 hist(ipsecstat.out_comphist, ipsec_compnames, "IPComp output"); 218 #undef p 219 #undef hist 220 } 221 222 void 223 ipsec_stats(u_long off __unused, const char *name, int af1 __unused) 224 { 225 if (off == 0) 226 return; 227 printf ("%s:\n", name); 228 kread(off, (char *)&ipsecstat, sizeof (ipsecstat)); 229 230 print_ipsecstats(); 231 } 232 233 static const char * 234 pfkey_msgtype_names(int x) 235 { 236 const int max = 237 sizeof(pfkey_msgtypenames)/sizeof(pfkey_msgtypenames[0]); 238 static char buf[20]; 239 240 if (x < max && pfkey_msgtypenames[x]) 241 return pfkey_msgtypenames[x]; 242 snprintf(buf, sizeof(buf), "#%d", x); 243 return buf; 244 } 245 246 void 247 pfkey_stats(u_long off __unused, const char *name, int af1 __unused) 248 { 249 struct pfkeystat pfkeystat; 250 unsigned first, type; 251 252 if (off == 0) 253 return; 254 printf ("%s:\n", name); 255 kread(off, (char *)&pfkeystat, sizeof(pfkeystat)); 256 257 #define p(f, m) if (pfkeystat.f || sflag <= 1) \ 258 printf(m, (unsigned long long)pfkeystat.f, plural(pfkeystat.f)) 259 260 /* userland -> kernel */ 261 p(out_total, "\t%llu request%s sent from userland\n"); 262 p(out_bytes, "\t%llu byte%s sent from userland\n"); 263 for (first = 1, type = 0; 264 type < sizeof(pfkeystat.out_msgtype)/sizeof(pfkeystat.out_msgtype[0]); 265 type++) { 266 if (pfkeystat.out_msgtype[type] <= 0) 267 continue; 268 if (first) { 269 printf("\thistogram by message type:\n"); 270 first = 0; 271 } 272 printf("\t\t%s: %llu\n", pfkey_msgtype_names(type), 273 (unsigned long long)pfkeystat.out_msgtype[type]); 274 } 275 p(out_invlen, "\t%llu message%s with invalid length field\n"); 276 p(out_invver, "\t%llu message%s with invalid version field\n"); 277 p(out_invmsgtype, "\t%llu message%s with invalid message type field\n"); 278 p(out_tooshort, "\t%llu message%s too short\n"); 279 p(out_nomem, "\t%llu message%s with memory allocation failure\n"); 280 p(out_dupext, "\t%llu message%s with duplicate extension\n"); 281 p(out_invexttype, "\t%llu message%s with invalid extension type\n"); 282 p(out_invsatype, "\t%llu message%s with invalid sa type\n"); 283 p(out_invaddr, "\t%llu message%s with invalid address extension\n"); 284 285 /* kernel -> userland */ 286 p(in_total, "\t%llu request%s sent to userland\n"); 287 p(in_bytes, "\t%llu byte%s sent to userland\n"); 288 for (first = 1, type = 0; 289 type < sizeof(pfkeystat.in_msgtype)/sizeof(pfkeystat.in_msgtype[0]); 290 type++) { 291 if (pfkeystat.in_msgtype[type] <= 0) 292 continue; 293 if (first) { 294 printf("\thistogram by message type:\n"); 295 first = 0; 296 } 297 printf("\t\t%s: %llu\n", pfkey_msgtype_names(type), 298 (unsigned long long)pfkeystat.in_msgtype[type]); 299 } 300 p(in_msgtarget[KEY_SENDUP_ONE], 301 "\t%llu message%s toward single socket\n"); 302 p(in_msgtarget[KEY_SENDUP_ALL], 303 "\t%llu message%s toward all sockets\n"); 304 p(in_msgtarget[KEY_SENDUP_REGISTERED], 305 "\t%llu message%s toward registered sockets\n"); 306 p(in_nomem, "\t%llu message%s with memory allocation failure\n"); 307 #undef p 308 } 309 #endif /*IPSEC*/ 310