1 /* $NetBSD: inet.c,v 1.35.2.1 1999/04/29 14:57:08 perry Exp $ */ 2 /* $KAME: ipsec.c,v 1.33 2003/07/25 09:54:32 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 #ifdef SADB_X_AALG_RIPEMD160HMAC 115 { SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", }, 116 #endif 117 #ifdef SADB_X_AALG_AES_XCBC_MAC 118 { SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", }, 119 #endif 120 { -1, NULL }, 121 }; 122 123 static struct val2str ipsec_espnames[] = { 124 { SADB_EALG_NONE, "none", }, 125 { SADB_EALG_DESCBC, "des-cbc", }, 126 { SADB_EALG_3DESCBC, "3des-cbc", }, 127 { SADB_EALG_NULL, "null", }, 128 { SADB_X_EALG_CAST128CBC, "cast128-cbc", }, 129 { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", }, 130 #ifdef SADB_X_EALG_RIJNDAELCBC 131 { SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", }, 132 #endif 133 #ifdef SADB_X_EALG_AESCTR 134 { SADB_X_EALG_AESCTR, "aes-ctr", }, 135 #endif 136 { -1, NULL }, 137 }; 138 139 static struct val2str ipsec_compnames[] = { 140 { SADB_X_CALG_NONE, "none", }, 141 { SADB_X_CALG_OUI, "oui", }, 142 { SADB_X_CALG_DEFLATE, "deflate", }, 143 { SADB_X_CALG_LZS, "lzs", }, 144 { -1, NULL }, 145 }; 146 147 static const char *pfkey_msgtypenames[] = { 148 "reserved", "getspi", "update", "add", "delete", 149 "get", "acquire", "register", "expire", "flush", 150 "dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd", 151 "x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush", 152 "x_spdsetidx", "x_spdexpire", "x_spddelete2" 153 }; 154 155 static struct ipsecstat ipsecstat; 156 157 static void print_ipsecstats (void); 158 static const char *pfkey_msgtype_names (int); 159 static void ipsec_hist (const u_quad_t *, size_t, const struct val2str *, 160 const char *); 161 162 /* 163 * Dump IPSEC statistics structure. 164 */ 165 static void 166 ipsec_hist(const u_quad_t *hist, size_t histmax, const struct val2str *name, 167 const char *title) 168 { 169 int first; 170 size_t proto; 171 const struct val2str *p; 172 173 first = 1; 174 for (proto = 0; proto < histmax; proto++) { 175 if (hist[proto] <= 0) 176 continue; 177 if (first) { 178 printf("\t%s histogram:\n", title); 179 first = 0; 180 } 181 for (p = name; p && p->str; p++) { 182 if (p->val == (int)proto) 183 break; 184 } 185 if (p && p->str) { 186 printf("\t\t%s: %llu\n", p->str, (unsigned long long)hist[proto]); 187 } else { 188 printf("\t\t#%ld: %llu\n", (long)proto, 189 (unsigned long long)hist[proto]); 190 } 191 } 192 } 193 194 static void 195 print_ipsecstats(void) 196 { 197 #define p(f, m) if (ipsecstat.f || sflag <= 1) \ 198 printf(m, (unsigned long long)ipsecstat.f, plural(ipsecstat.f)) 199 #define pes(f, m) if (ipsecstat.f || sflag <= 1) \ 200 printf(m, (unsigned long long)ipsecstat.f, plurales(ipsecstat.f)) 201 #define hist(f, n, t) \ 202 ipsec_hist((f), sizeof(f)/sizeof(f[0]), (n), (t)); 203 204 p(in_success, "\t%llu inbound packet%s processed successfully\n"); 205 p(in_polvio, "\t%llu inbound packet%s violated process security " 206 "policy\n"); 207 p(in_nosa, "\t%llu inbound packet%s with no SA available\n"); 208 p(in_inval, "\t%llu invalid inbound packet%s\n"); 209 p(in_nomem, "\t%llu inbound packet%s failed due to insufficient memory\n"); 210 p(in_badspi, "\t%llu inbound packet%s failed getting SPI\n"); 211 p(in_ahreplay, "\t%llu inbound packet%s failed on AH replay check\n"); 212 p(in_espreplay, "\t%llu inbound packet%s failed on ESP replay check\n"); 213 p(in_ahauthsucc, "\t%llu inbound packet%s considered authentic\n"); 214 p(in_ahauthfail, "\t%llu inbound packet%s failed on authentication\n"); 215 hist(ipsecstat.in_ahhist, ipsec_ahnames, "AH input"); 216 hist(ipsecstat.in_esphist, ipsec_espnames, "ESP input"); 217 hist(ipsecstat.in_comphist, ipsec_compnames, "IPComp input"); 218 219 p(out_success, "\t%llu outbound packet%s processed successfully\n"); 220 p(out_polvio, "\t%llu outbound packet%s violated process security " 221 "policy\n"); 222 p(out_nosa, "\t%llu outbound packet%s with no SA available\n"); 223 p(out_inval, "\t%llu invalid outbound packet%s\n"); 224 p(out_nomem, "\t%llu outbound packet%s failed due to insufficient memory\n"); 225 p(out_noroute, "\t%llu outbound packet%s with no route\n"); 226 hist(ipsecstat.out_ahhist, ipsec_ahnames, "AH output"); 227 hist(ipsecstat.out_esphist, ipsec_espnames, "ESP output"); 228 hist(ipsecstat.out_comphist, ipsec_compnames, "IPComp output"); 229 p(spdcachelookup, "\t%llu SPD cache lookup%s\n"); 230 pes(spdcachemiss, "\t%llu SPD cache miss%s\n"); 231 #undef p 232 #undef pes 233 #undef hist 234 } 235 236 void 237 ipsec_stats(u_long off __unused, const char *name, int af1 __unused) 238 { 239 if (off == 0) 240 return; 241 printf ("%s:\n", name); 242 kread(off, (char *)&ipsecstat, sizeof (ipsecstat)); 243 244 print_ipsecstats(); 245 } 246 247 static const char * 248 pfkey_msgtype_names(int x) 249 { 250 const int max = 251 sizeof(pfkey_msgtypenames)/sizeof(pfkey_msgtypenames[0]); 252 static char buf[20]; 253 254 if (x < max && pfkey_msgtypenames[x]) 255 return pfkey_msgtypenames[x]; 256 snprintf(buf, sizeof(buf), "#%d", x); 257 return buf; 258 } 259 260 void 261 pfkey_stats(u_long off __unused, const char *name, int af1 __unused) 262 { 263 struct pfkeystat pfkeystat; 264 unsigned first, type; 265 266 if (off == 0) 267 return; 268 printf ("%s:\n", name); 269 kread(off, (char *)&pfkeystat, sizeof(pfkeystat)); 270 271 #define p(f, m) if (pfkeystat.f || sflag <= 1) \ 272 printf(m, (unsigned long long)pfkeystat.f, plural(pfkeystat.f)) 273 274 /* userland -> kernel */ 275 p(out_total, "\t%llu request%s sent from userland\n"); 276 p(out_bytes, "\t%llu byte%s sent from userland\n"); 277 for (first = 1, type = 0; 278 type < sizeof(pfkeystat.out_msgtype)/sizeof(pfkeystat.out_msgtype[0]); 279 type++) { 280 if (pfkeystat.out_msgtype[type] <= 0) 281 continue; 282 if (first) { 283 printf("\thistogram by message type:\n"); 284 first = 0; 285 } 286 printf("\t\t%s: %llu\n", pfkey_msgtype_names(type), 287 (unsigned long long)pfkeystat.out_msgtype[type]); 288 } 289 p(out_invlen, "\t%llu message%s with invalid length field\n"); 290 p(out_invver, "\t%llu message%s with invalid version field\n"); 291 p(out_invmsgtype, "\t%llu message%s with invalid message type field\n"); 292 p(out_tooshort, "\t%llu message%s too short\n"); 293 p(out_nomem, "\t%llu message%s with memory allocation failure\n"); 294 p(out_dupext, "\t%llu message%s with duplicate extension\n"); 295 p(out_invexttype, "\t%llu message%s with invalid extension type\n"); 296 p(out_invsatype, "\t%llu message%s with invalid sa type\n"); 297 p(out_invaddr, "\t%llu message%s with invalid address extension\n"); 298 299 /* kernel -> userland */ 300 p(in_total, "\t%llu request%s sent to userland\n"); 301 p(in_bytes, "\t%llu byte%s sent to userland\n"); 302 for (first = 1, type = 0; 303 type < sizeof(pfkeystat.in_msgtype)/sizeof(pfkeystat.in_msgtype[0]); 304 type++) { 305 if (pfkeystat.in_msgtype[type] <= 0) 306 continue; 307 if (first) { 308 printf("\thistogram by message type:\n"); 309 first = 0; 310 } 311 printf("\t\t%s: %llu\n", pfkey_msgtype_names(type), 312 (unsigned long long)pfkeystat.in_msgtype[type]); 313 } 314 p(in_msgtarget[KEY_SENDUP_ONE], 315 "\t%llu message%s toward single socket\n"); 316 p(in_msgtarget[KEY_SENDUP_ALL], 317 "\t%llu message%s toward all sockets\n"); 318 p(in_msgtarget[KEY_SENDUP_REGISTERED], 319 "\t%llu message%s toward registered sockets\n"); 320 p(in_nomem, "\t%llu message%s with memory allocation failure\n"); 321 #undef p 322 } 323 #endif /*IPSEC*/ 324