1bd9f52d5SHajimu UMEMOTO /* $KAME: pfkey_dump.c,v 1.45 2003/09/08 10:14:56 itojun Exp $ */ 23c62e87aSJun-ichiro itojun Hagino 38a16b7a1SPedro F. Giffuni /*- 48a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 58a16b7a1SPedro F. Giffuni * 69a4365d0SYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 79a4365d0SYoshinobu Inoue * All rights reserved. 89a4365d0SYoshinobu Inoue * 99a4365d0SYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 109a4365d0SYoshinobu Inoue * modification, are permitted provided that the following conditions 119a4365d0SYoshinobu Inoue * are met: 129a4365d0SYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 139a4365d0SYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 149a4365d0SYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 159a4365d0SYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 169a4365d0SYoshinobu Inoue * documentation and/or other materials provided with the distribution. 179a4365d0SYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 189a4365d0SYoshinobu Inoue * may be used to endorse or promote products derived from this software 199a4365d0SYoshinobu Inoue * without specific prior written permission. 209a4365d0SYoshinobu Inoue * 219a4365d0SYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 229a4365d0SYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239a4365d0SYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249a4365d0SYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 259a4365d0SYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269a4365d0SYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279a4365d0SYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289a4365d0SYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299a4365d0SYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309a4365d0SYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319a4365d0SYoshinobu Inoue * SUCH DAMAGE. 329a4365d0SYoshinobu Inoue */ 339a4365d0SYoshinobu Inoue 3446a50f4eSMatthew Dillon #include <sys/cdefs.h> 3546a50f4eSMatthew Dillon __FBSDID("$FreeBSD$"); 3646a50f4eSMatthew Dillon 379a4365d0SYoshinobu Inoue #include <sys/types.h> 389a4365d0SYoshinobu Inoue #include <sys/param.h> 399a4365d0SYoshinobu Inoue #include <sys/socket.h> 4022986c67SAndrey V. Elsukov #include <net/if.h> 419a4365d0SYoshinobu Inoue #include <net/pfkeyv2.h> 4222986c67SAndrey V. Elsukov #include <netipsec/ipsec.h> 438409aedfSGeorge V. Neville-Neil #include <netipsec/key_var.h> 448409aedfSGeorge V. Neville-Neil #include <netipsec/key_debug.h> 459a4365d0SYoshinobu Inoue 469a4365d0SYoshinobu Inoue #include <netinet/in.h> 479a4365d0SYoshinobu Inoue #include <arpa/inet.h> 489a4365d0SYoshinobu Inoue 499a4365d0SYoshinobu Inoue #include <stdlib.h> 509a4365d0SYoshinobu Inoue #include <unistd.h> 519a4365d0SYoshinobu Inoue #include <stdio.h> 529a4365d0SYoshinobu Inoue #include <string.h> 539a4365d0SYoshinobu Inoue #include <time.h> 543c62e87aSJun-ichiro itojun Hagino #include <netdb.h> 559a4365d0SYoshinobu Inoue 569a4365d0SYoshinobu Inoue #include "ipsec_strerror.h" 573c62e87aSJun-ichiro itojun Hagino #include "libpfkey.h" 589a4365d0SYoshinobu Inoue 5933841545SHajimu UMEMOTO /* cope with old kame headers - ugly */ 6033841545SHajimu UMEMOTO #ifndef SADB_X_AALG_NULL 6133841545SHajimu UMEMOTO #define SADB_X_AALG_NULL SADB_AALG_NULL 6233841545SHajimu UMEMOTO #endif 6333841545SHajimu UMEMOTO 6433841545SHajimu UMEMOTO #ifndef SADB_X_EALG_RC5CBC 6533841545SHajimu UMEMOTO #ifdef SADB_EALG_RC5CBC 6633841545SHajimu UMEMOTO #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC 6733841545SHajimu UMEMOTO #endif 6833841545SHajimu UMEMOTO #endif 6933841545SHajimu UMEMOTO 709a4365d0SYoshinobu Inoue #define GETMSGSTR(str, num) \ 713c62e87aSJun-ichiro itojun Hagino do { \ 729a4365d0SYoshinobu Inoue if (sizeof((str)[0]) == 0 \ 739a4365d0SYoshinobu Inoue || num >= sizeof(str)/sizeof((str)[0])) \ 749713f5c1SHajimu UMEMOTO printf("%u ", (num)); \ 759a4365d0SYoshinobu Inoue else if (strlen((str)[(num)]) == 0) \ 769713f5c1SHajimu UMEMOTO printf("%u ", (num)); \ 779a4365d0SYoshinobu Inoue else \ 789a4365d0SYoshinobu Inoue printf("%s ", (str)[(num)]); \ 793c62e87aSJun-ichiro itojun Hagino } while (0) 809a4365d0SYoshinobu Inoue 8133841545SHajimu UMEMOTO #define GETMSGV2S(v2s, num) \ 8233841545SHajimu UMEMOTO do { \ 8333841545SHajimu UMEMOTO struct val2str *p; \ 8433841545SHajimu UMEMOTO for (p = (v2s); p && p->str; p++) { \ 8533841545SHajimu UMEMOTO if (p->val == (num)) \ 8633841545SHajimu UMEMOTO break; \ 8733841545SHajimu UMEMOTO } \ 8833841545SHajimu UMEMOTO if (p && p->str) \ 8933841545SHajimu UMEMOTO printf("%s ", p->str); \ 9033841545SHajimu UMEMOTO else \ 919713f5c1SHajimu UMEMOTO printf("%u ", (num)); \ 9233841545SHajimu UMEMOTO } while (0) 9333841545SHajimu UMEMOTO 9469160b1eSDavid E. O'Brien static char *str_ipaddr(struct sockaddr *); 95bd9f52d5SHajimu UMEMOTO static char *str_prefport(u_int, u_int, u_int, u_int); 96bd9f52d5SHajimu UMEMOTO static void str_upperspec(u_int, u_int, u_int); 9769160b1eSDavid E. O'Brien static char *str_time(time_t); 9869160b1eSDavid E. O'Brien static void str_lifetime_byte(struct sadb_lifetime *, char *); 999a4365d0SYoshinobu Inoue 10033841545SHajimu UMEMOTO struct val2str { 10133841545SHajimu UMEMOTO int val; 10233841545SHajimu UMEMOTO const char *str; 10333841545SHajimu UMEMOTO }; 10433841545SHajimu UMEMOTO 1059a4365d0SYoshinobu Inoue /* 1069a4365d0SYoshinobu Inoue * Must to be re-written about following strings. 1079a4365d0SYoshinobu Inoue */ 10833841545SHajimu UMEMOTO static char *str_satype[] = { 1099a4365d0SYoshinobu Inoue "unspec", 1109a4365d0SYoshinobu Inoue "unknown", 1119a4365d0SYoshinobu Inoue "ah", 1129a4365d0SYoshinobu Inoue "esp", 1139a4365d0SYoshinobu Inoue "unknown", 1149a4365d0SYoshinobu Inoue "rsvp", 1159a4365d0SYoshinobu Inoue "ospfv2", 1169a4365d0SYoshinobu Inoue "ripv2", 1179a4365d0SYoshinobu Inoue "mip", 1189a4365d0SYoshinobu Inoue "ipcomp", 1191922fd12SBruce M Simpson "policy", 1201922fd12SBruce M Simpson "tcp" 1219a4365d0SYoshinobu Inoue }; 1229a4365d0SYoshinobu Inoue 12333841545SHajimu UMEMOTO static char *str_mode[] = { 1249a4365d0SYoshinobu Inoue "any", 1259a4365d0SYoshinobu Inoue "transport", 1269a4365d0SYoshinobu Inoue "tunnel", 1279a4365d0SYoshinobu Inoue }; 1289a4365d0SYoshinobu Inoue 12933841545SHajimu UMEMOTO static char *str_state[] = { 1309a4365d0SYoshinobu Inoue "larval", 1319a4365d0SYoshinobu Inoue "mature", 1329a4365d0SYoshinobu Inoue "dying", 1339a4365d0SYoshinobu Inoue "dead", 1349a4365d0SYoshinobu Inoue }; 1359a4365d0SYoshinobu Inoue 13633841545SHajimu UMEMOTO static struct val2str str_alg_auth[] = { 13733841545SHajimu UMEMOTO { SADB_AALG_NONE, "none", }, 13833841545SHajimu UMEMOTO { SADB_AALG_SHA1HMAC, "hmac-sha1", }, 13933841545SHajimu UMEMOTO { SADB_X_AALG_NULL, "null", }, 1401922fd12SBruce M Simpson { SADB_X_AALG_TCP_MD5, "tcp-md5", }, 14133841545SHajimu UMEMOTO #ifdef SADB_X_AALG_SHA2_256 14233841545SHajimu UMEMOTO { SADB_X_AALG_SHA2_256, "hmac-sha2-256", }, 14333841545SHajimu UMEMOTO #endif 14433841545SHajimu UMEMOTO #ifdef SADB_X_AALG_SHA2_384 14533841545SHajimu UMEMOTO { SADB_X_AALG_SHA2_384, "hmac-sha2-384", }, 14633841545SHajimu UMEMOTO #endif 14733841545SHajimu UMEMOTO #ifdef SADB_X_AALG_SHA2_512 14833841545SHajimu UMEMOTO { SADB_X_AALG_SHA2_512, "hmac-sha2-512", }, 14933841545SHajimu UMEMOTO #endif 150c65ee7c7SHajimu UMEMOTO #ifdef SADB_X_AALG_AES_XCBC_MAC 151c65ee7c7SHajimu UMEMOTO { SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", }, 152c65ee7c7SHajimu UMEMOTO #endif 153*9f8f3a8eSKristof Provost #ifdef SADB_X_AALG_CHACHA20POLY1305 154*9f8f3a8eSKristof Provost { SADB_X_AALG_CHACHA20POLY1305, "chacha20-poly1305", }, 155*9f8f3a8eSKristof Provost #endif 15633841545SHajimu UMEMOTO { -1, NULL, }, 1579a4365d0SYoshinobu Inoue }; 1589a4365d0SYoshinobu Inoue 15933841545SHajimu UMEMOTO static struct val2str str_alg_enc[] = { 16033841545SHajimu UMEMOTO { SADB_EALG_NONE, "none", }, 16133841545SHajimu UMEMOTO { SADB_EALG_NULL, "null", }, 16233841545SHajimu UMEMOTO #ifdef SADB_X_EALG_RC5CBC 16333841545SHajimu UMEMOTO { SADB_X_EALG_RC5CBC, "rc5-cbc", }, 16433841545SHajimu UMEMOTO #endif 16500a4311aSJohn Baldwin #ifdef SADB_X_EALG_AESCBC 16600a4311aSJohn Baldwin { SADB_X_EALG_AESCBC, "aes-cbc", }, 16733841545SHajimu UMEMOTO #endif 16833841545SHajimu UMEMOTO #ifdef SADB_X_EALG_TWOFISHCBC 16933841545SHajimu UMEMOTO { SADB_X_EALG_TWOFISHCBC, "twofish-cbc", }, 17033841545SHajimu UMEMOTO #endif 171b42ac57fSHajimu UMEMOTO #ifdef SADB_X_EALG_AESCTR 172b42ac57fSHajimu UMEMOTO { SADB_X_EALG_AESCTR, "aes-ctr", }, 173b42ac57fSHajimu UMEMOTO #endif 174987de844SGeorge V. Neville-Neil #ifdef SADB_X_EALG_AESGCM16 175987de844SGeorge V. Neville-Neil { SADB_X_EALG_AESGCM16, "aes-gcm-16", }, 176987de844SGeorge V. Neville-Neil #endif 177*9f8f3a8eSKristof Provost #ifdef SADB_X_EALG_CHACHA20POLY1305 178*9f8f3a8eSKristof Provost { SADB_X_EALG_CHACHA20POLY1305, "chacha20-poly1305", }, 179*9f8f3a8eSKristof Provost #endif 18033841545SHajimu UMEMOTO { -1, NULL, }, 1819a4365d0SYoshinobu Inoue }; 1829a4365d0SYoshinobu Inoue 18333841545SHajimu UMEMOTO static struct val2str str_alg_comp[] = { 18433841545SHajimu UMEMOTO { SADB_X_CALG_NONE, "none", }, 18533841545SHajimu UMEMOTO { SADB_X_CALG_OUI, "oui", }, 18633841545SHajimu UMEMOTO { SADB_X_CALG_DEFLATE, "deflate", }, 18733841545SHajimu UMEMOTO { SADB_X_CALG_LZS, "lzs", }, 18833841545SHajimu UMEMOTO { -1, NULL, }, 1899a4365d0SYoshinobu Inoue }; 1909a4365d0SYoshinobu Inoue 19122986c67SAndrey V. Elsukov static struct val2str str_sp_scope[] = { 19222986c67SAndrey V. Elsukov { IPSEC_POLICYSCOPE_GLOBAL, "global" }, 19322986c67SAndrey V. Elsukov { IPSEC_POLICYSCOPE_IFNET, "ifnet" }, 19422986c67SAndrey V. Elsukov { IPSEC_POLICYSCOPE_PCB, "pcb"}, 19522986c67SAndrey V. Elsukov { -1, NULL }, 19622986c67SAndrey V. Elsukov }; 19722986c67SAndrey V. Elsukov 1989a4365d0SYoshinobu Inoue /* 1997a33c6bfSGordon Bergling * dump SADB_MSG formatted. For debugging, you should use kdebug_sadb(). 2009a4365d0SYoshinobu Inoue */ 2019a4365d0SYoshinobu Inoue void 2029a4365d0SYoshinobu Inoue pfkey_sadump(m) 2039a4365d0SYoshinobu Inoue struct sadb_msg *m; 2049a4365d0SYoshinobu Inoue { 2059a4365d0SYoshinobu Inoue caddr_t mhp[SADB_EXT_MAX + 1]; 2069a4365d0SYoshinobu Inoue struct sadb_sa *m_sa; 2073c62e87aSJun-ichiro itojun Hagino struct sadb_x_sa2 *m_sa2; 2089a4365d0SYoshinobu Inoue struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts; 2099a4365d0SYoshinobu Inoue struct sadb_address *m_saddr, *m_daddr, *m_paddr; 2109a4365d0SYoshinobu Inoue struct sadb_key *m_auth, *m_enc; 2119a4365d0SYoshinobu Inoue struct sadb_ident *m_sid, *m_did; 2129a4365d0SYoshinobu Inoue struct sadb_sens *m_sens; 213bf435626SFabien Thomas struct sadb_x_sa_replay *m_sa_replay; 214fcf59617SAndrey V. Elsukov struct sadb_x_nat_t_type *natt_type; 215fcf59617SAndrey V. Elsukov struct sadb_x_nat_t_port *natt_sport, *natt_dport; 216fcf59617SAndrey V. Elsukov struct sadb_address *natt_oai, *natt_oar; 2179a4365d0SYoshinobu Inoue 2189a4365d0SYoshinobu Inoue /* check pfkey message. */ 2199a4365d0SYoshinobu Inoue if (pfkey_align(m, mhp)) { 2209a4365d0SYoshinobu Inoue printf("%s\n", ipsec_strerror()); 2219a4365d0SYoshinobu Inoue return; 2229a4365d0SYoshinobu Inoue } 2239a4365d0SYoshinobu Inoue if (pfkey_check(mhp)) { 2249a4365d0SYoshinobu Inoue printf("%s\n", ipsec_strerror()); 2259a4365d0SYoshinobu Inoue return; 2269a4365d0SYoshinobu Inoue } 2279a4365d0SYoshinobu Inoue 2289a4365d0SYoshinobu Inoue m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA]; 2293c62e87aSJun-ichiro itojun Hagino m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2]; 2309a4365d0SYoshinobu Inoue m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT]; 2319a4365d0SYoshinobu Inoue m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; 2329a4365d0SYoshinobu Inoue m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT]; 2339a4365d0SYoshinobu Inoue m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]; 2349a4365d0SYoshinobu Inoue m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]; 2359a4365d0SYoshinobu Inoue m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY]; 2369a4365d0SYoshinobu Inoue m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH]; 2379a4365d0SYoshinobu Inoue m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT]; 2389a4365d0SYoshinobu Inoue m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC]; 2393c62e87aSJun-ichiro itojun Hagino m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST]; 2409a4365d0SYoshinobu Inoue m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY]; 241bf435626SFabien Thomas m_sa_replay = (struct sadb_x_sa_replay *)mhp[SADB_X_EXT_SA_REPLAY]; 242fcf59617SAndrey V. Elsukov natt_type = (struct sadb_x_nat_t_type *)mhp[SADB_X_EXT_NAT_T_TYPE]; 243fcf59617SAndrey V. Elsukov natt_sport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_SPORT]; 244fcf59617SAndrey V. Elsukov natt_dport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_DPORT]; 245fcf59617SAndrey V. Elsukov natt_oai = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OAI]; 246fcf59617SAndrey V. Elsukov natt_oar = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OAR]; 247fcf59617SAndrey V. Elsukov 2489a4365d0SYoshinobu Inoue 2499a4365d0SYoshinobu Inoue /* source address */ 2509a4365d0SYoshinobu Inoue if (m_saddr == NULL) { 2519a4365d0SYoshinobu Inoue printf("no ADDRESS_SRC extension.\n"); 2529a4365d0SYoshinobu Inoue return; 2539a4365d0SYoshinobu Inoue } 2543c62e87aSJun-ichiro itojun Hagino printf("%s", str_ipaddr((struct sockaddr *)(m_saddr + 1))); 255fcf59617SAndrey V. Elsukov if (natt_type != NULL && natt_sport != NULL) 256fcf59617SAndrey V. Elsukov printf("[%u]", ntohs(natt_sport->sadb_x_nat_t_port_port)); 2579a4365d0SYoshinobu Inoue 2589a4365d0SYoshinobu Inoue /* destination address */ 2599a4365d0SYoshinobu Inoue if (m_daddr == NULL) { 260fcf59617SAndrey V. Elsukov printf("\nno ADDRESS_DST extension.\n"); 2619a4365d0SYoshinobu Inoue return; 2629a4365d0SYoshinobu Inoue } 2633c62e87aSJun-ichiro itojun Hagino printf(" %s", str_ipaddr((struct sockaddr *)(m_daddr + 1))); 264fcf59617SAndrey V. Elsukov if (natt_type != NULL && natt_dport != NULL) 265fcf59617SAndrey V. Elsukov printf("[%u]", ntohs(natt_dport->sadb_x_nat_t_port_port)); 2669a4365d0SYoshinobu Inoue 2679a4365d0SYoshinobu Inoue /* SA type */ 2689a4365d0SYoshinobu Inoue if (m_sa == NULL) { 269fcf59617SAndrey V. Elsukov printf("\nno SA extension.\n"); 2709a4365d0SYoshinobu Inoue return; 2719a4365d0SYoshinobu Inoue } 2723c62e87aSJun-ichiro itojun Hagino if (m_sa2 == NULL) { 273fcf59617SAndrey V. Elsukov printf("\nno SA2 extension.\n"); 2743c62e87aSJun-ichiro itojun Hagino return; 2753c62e87aSJun-ichiro itojun Hagino } 2769a4365d0SYoshinobu Inoue printf("\n\t"); 2779a4365d0SYoshinobu Inoue 278fcf59617SAndrey V. Elsukov if (m->sadb_msg_satype == SADB_SATYPE_ESP && natt_type != NULL) 279fcf59617SAndrey V. Elsukov printf("esp-udp "); 280fcf59617SAndrey V. Elsukov else 28133841545SHajimu UMEMOTO GETMSGSTR(str_satype, m->sadb_msg_satype); 2829a4365d0SYoshinobu Inoue 2839a4365d0SYoshinobu Inoue printf("mode="); 28433841545SHajimu UMEMOTO GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode); 2859a4365d0SYoshinobu Inoue 2863c62e87aSJun-ichiro itojun Hagino printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n", 2879a4365d0SYoshinobu Inoue (u_int32_t)ntohl(m_sa->sadb_sa_spi), 2889a4365d0SYoshinobu Inoue (u_int32_t)ntohl(m_sa->sadb_sa_spi), 2893c62e87aSJun-ichiro itojun Hagino (u_int32_t)m_sa2->sadb_x_sa2_reqid, 2903c62e87aSJun-ichiro itojun Hagino (u_int32_t)m_sa2->sadb_x_sa2_reqid); 2919a4365d0SYoshinobu Inoue 292fcf59617SAndrey V. Elsukov /* other NAT-T information */ 293fcf59617SAndrey V. Elsukov if (natt_type != NULL && (natt_oai != NULL || natt_oar != NULL)) { 294fcf59617SAndrey V. Elsukov printf("\tNAT:"); 295fcf59617SAndrey V. Elsukov if (natt_oai != NULL) 296fcf59617SAndrey V. Elsukov printf(" OAI=%s", 297fcf59617SAndrey V. Elsukov str_ipaddr((struct sockaddr *)(natt_oai + 1))); 298fcf59617SAndrey V. Elsukov if (natt_oar != NULL) 299fcf59617SAndrey V. Elsukov printf(" OAR=%s", 300fcf59617SAndrey V. Elsukov str_ipaddr((struct sockaddr *)(natt_oar + 1))); 301fcf59617SAndrey V. Elsukov printf("\n"); 302fcf59617SAndrey V. Elsukov } 303fcf59617SAndrey V. Elsukov 3049a4365d0SYoshinobu Inoue /* encryption key */ 3059a4365d0SYoshinobu Inoue if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) { 3069a4365d0SYoshinobu Inoue printf("\tC: "); 30733841545SHajimu UMEMOTO GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt); 3089a4365d0SYoshinobu Inoue } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) { 3099a4365d0SYoshinobu Inoue if (m_enc != NULL) { 3109a4365d0SYoshinobu Inoue printf("\tE: "); 31133841545SHajimu UMEMOTO GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt); 3129a4365d0SYoshinobu Inoue ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc), 3139a4365d0SYoshinobu Inoue m_enc->sadb_key_bits / 8); 3149a4365d0SYoshinobu Inoue printf("\n"); 3159a4365d0SYoshinobu Inoue } 3169a4365d0SYoshinobu Inoue } 3179a4365d0SYoshinobu Inoue 3189a4365d0SYoshinobu Inoue /* authentication key */ 3199a4365d0SYoshinobu Inoue if (m_auth != NULL) { 3209a4365d0SYoshinobu Inoue printf("\tA: "); 32133841545SHajimu UMEMOTO GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth); 3229a4365d0SYoshinobu Inoue ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth), 3239a4365d0SYoshinobu Inoue m_auth->sadb_key_bits / 8); 3249a4365d0SYoshinobu Inoue printf("\n"); 3259a4365d0SYoshinobu Inoue } 3269a4365d0SYoshinobu Inoue 3273c62e87aSJun-ichiro itojun Hagino /* replay windoe size & flags */ 328232bdaf6SHajimu UMEMOTO printf("\tseq=0x%08x replay=%u flags=0x%08x ", 329232bdaf6SHajimu UMEMOTO m_sa2->sadb_x_sa2_sequence, 330bf435626SFabien Thomas m_sa_replay ? (m_sa_replay->sadb_x_sa_replay_replay >> 3) : 3313c62e87aSJun-ichiro itojun Hagino m_sa->sadb_sa_replay, 3323c62e87aSJun-ichiro itojun Hagino m_sa->sadb_sa_flags); 3333c62e87aSJun-ichiro itojun Hagino 3349a4365d0SYoshinobu Inoue /* state */ 3353c62e87aSJun-ichiro itojun Hagino printf("state="); 33633841545SHajimu UMEMOTO GETMSGSTR(str_state, m_sa->sadb_sa_state); 337232bdaf6SHajimu UMEMOTO printf("\n"); 3389a4365d0SYoshinobu Inoue 3399a4365d0SYoshinobu Inoue /* lifetime */ 3409a4365d0SYoshinobu Inoue if (m_lftc != NULL) { 3419a4365d0SYoshinobu Inoue time_t tmp_time = time(0); 3429a4365d0SYoshinobu Inoue 3439a4365d0SYoshinobu Inoue printf("\tcreated: %s", 3443c62e87aSJun-ichiro itojun Hagino str_time(m_lftc->sadb_lifetime_addtime)); 3453c62e87aSJun-ichiro itojun Hagino printf("\tcurrent: %s\n", str_time(tmp_time)); 3469a4365d0SYoshinobu Inoue printf("\tdiff: %lu(s)", 3479a4365d0SYoshinobu Inoue (u_long)(m_lftc->sadb_lifetime_addtime == 0 ? 3489a4365d0SYoshinobu Inoue 0 : (tmp_time - m_lftc->sadb_lifetime_addtime))); 3499a4365d0SYoshinobu Inoue 3509a4365d0SYoshinobu Inoue printf("\thard: %lu(s)", 3519a4365d0SYoshinobu Inoue (u_long)(m_lfth == NULL ? 3529a4365d0SYoshinobu Inoue 0 : m_lfth->sadb_lifetime_addtime)); 3539a4365d0SYoshinobu Inoue printf("\tsoft: %lu(s)\n", 3549a4365d0SYoshinobu Inoue (u_long)(m_lfts == NULL ? 3559a4365d0SYoshinobu Inoue 0 : m_lfts->sadb_lifetime_addtime)); 3569a4365d0SYoshinobu Inoue 3579a4365d0SYoshinobu Inoue printf("\tlast: %s", 3583c62e87aSJun-ichiro itojun Hagino str_time(m_lftc->sadb_lifetime_usetime)); 3599a4365d0SYoshinobu Inoue printf("\thard: %lu(s)", 3609a4365d0SYoshinobu Inoue (u_long)(m_lfth == NULL ? 3619a4365d0SYoshinobu Inoue 0 : m_lfth->sadb_lifetime_usetime)); 3629a4365d0SYoshinobu Inoue printf("\tsoft: %lu(s)\n", 3639a4365d0SYoshinobu Inoue (u_long)(m_lfts == NULL ? 3649a4365d0SYoshinobu Inoue 0 : m_lfts->sadb_lifetime_usetime)); 3659a4365d0SYoshinobu Inoue 3663c62e87aSJun-ichiro itojun Hagino str_lifetime_byte(m_lftc, "current"); 3673c62e87aSJun-ichiro itojun Hagino str_lifetime_byte(m_lfth, "hard"); 3683c62e87aSJun-ichiro itojun Hagino str_lifetime_byte(m_lfts, "soft"); 3699a4365d0SYoshinobu Inoue printf("\n"); 3709a4365d0SYoshinobu Inoue 3719a4365d0SYoshinobu Inoue printf("\tallocated: %lu", 3729a4365d0SYoshinobu Inoue (unsigned long)m_lftc->sadb_lifetime_allocations); 3739a4365d0SYoshinobu Inoue printf("\thard: %lu", 3749a4365d0SYoshinobu Inoue (u_long)(m_lfth == NULL ? 3759a4365d0SYoshinobu Inoue 0 : m_lfth->sadb_lifetime_allocations)); 3769a4365d0SYoshinobu Inoue printf("\tsoft: %lu\n", 3779a4365d0SYoshinobu Inoue (u_long)(m_lfts == NULL ? 3789a4365d0SYoshinobu Inoue 0 : m_lfts->sadb_lifetime_allocations)); 3799a4365d0SYoshinobu Inoue } 3809a4365d0SYoshinobu Inoue 381232bdaf6SHajimu UMEMOTO printf("\tsadb_seq=%lu pid=%lu ", 382232bdaf6SHajimu UMEMOTO (u_long)m->sadb_msg_seq, 383232bdaf6SHajimu UMEMOTO (u_long)m->sadb_msg_pid); 384232bdaf6SHajimu UMEMOTO 3859a4365d0SYoshinobu Inoue /* XXX DEBUG */ 386232bdaf6SHajimu UMEMOTO printf("refcnt=%u\n", m->sadb_msg_reserved); 3879a4365d0SYoshinobu Inoue 3889a4365d0SYoshinobu Inoue return; 3899a4365d0SYoshinobu Inoue } 3909a4365d0SYoshinobu Inoue 3919a4365d0SYoshinobu Inoue void 39222986c67SAndrey V. Elsukov pfkey_spdump(struct sadb_msg *m) 3939a4365d0SYoshinobu Inoue { 3943c62e87aSJun-ichiro itojun Hagino char pbuf[NI_MAXSERV]; 3959a4365d0SYoshinobu Inoue caddr_t mhp[SADB_EXT_MAX + 1]; 3969a4365d0SYoshinobu Inoue struct sadb_address *m_saddr, *m_daddr; 3979a4365d0SYoshinobu Inoue struct sadb_x_policy *m_xpl; 398bd9f52d5SHajimu UMEMOTO struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL; 3993c62e87aSJun-ichiro itojun Hagino struct sockaddr *sa; 400bd9f52d5SHajimu UMEMOTO u_int16_t sport = 0, dport = 0; 4019a4365d0SYoshinobu Inoue 4029a4365d0SYoshinobu Inoue /* check pfkey message. */ 4039a4365d0SYoshinobu Inoue if (pfkey_align(m, mhp)) { 4049a4365d0SYoshinobu Inoue printf("%s\n", ipsec_strerror()); 4059a4365d0SYoshinobu Inoue return; 4069a4365d0SYoshinobu Inoue } 4079a4365d0SYoshinobu Inoue if (pfkey_check(mhp)) { 4089a4365d0SYoshinobu Inoue printf("%s\n", ipsec_strerror()); 4099a4365d0SYoshinobu Inoue return; 4109a4365d0SYoshinobu Inoue } 4119a4365d0SYoshinobu Inoue 4129a4365d0SYoshinobu Inoue m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]; 4139a4365d0SYoshinobu Inoue m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]; 4149a4365d0SYoshinobu Inoue m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY]; 415bd9f52d5SHajimu UMEMOTO m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT]; 416bd9f52d5SHajimu UMEMOTO m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; 4179a4365d0SYoshinobu Inoue 418bd9f52d5SHajimu UMEMOTO if (m_saddr && m_daddr) { 4199a4365d0SYoshinobu Inoue /* source address */ 4203c62e87aSJun-ichiro itojun Hagino sa = (struct sockaddr *)(m_saddr + 1); 4213c62e87aSJun-ichiro itojun Hagino switch (sa->sa_family) { 4223c62e87aSJun-ichiro itojun Hagino case AF_INET: 4233c62e87aSJun-ichiro itojun Hagino case AF_INET6: 424bd9f52d5SHajimu UMEMOTO if (getnameinfo(sa, sa->sa_len, NULL, 0, 425bd9f52d5SHajimu UMEMOTO pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0) 426bd9f52d5SHajimu UMEMOTO sport = 0; /*XXX*/ 4273c62e87aSJun-ichiro itojun Hagino else 428bd9f52d5SHajimu UMEMOTO sport = atoi(pbuf); 4293c62e87aSJun-ichiro itojun Hagino printf("%s%s ", str_ipaddr(sa), 4303c62e87aSJun-ichiro itojun Hagino str_prefport(sa->sa_family, 431bd9f52d5SHajimu UMEMOTO m_saddr->sadb_address_prefixlen, sport, 432bd9f52d5SHajimu UMEMOTO m_saddr->sadb_address_proto)); 4333c62e87aSJun-ichiro itojun Hagino break; 4343c62e87aSJun-ichiro itojun Hagino default: 4353c62e87aSJun-ichiro itojun Hagino printf("unknown-af "); 4363c62e87aSJun-ichiro itojun Hagino break; 4373c62e87aSJun-ichiro itojun Hagino } 4389a4365d0SYoshinobu Inoue 4399a4365d0SYoshinobu Inoue /* destination address */ 4403c62e87aSJun-ichiro itojun Hagino sa = (struct sockaddr *)(m_daddr + 1); 4413c62e87aSJun-ichiro itojun Hagino switch (sa->sa_family) { 4423c62e87aSJun-ichiro itojun Hagino case AF_INET: 4433c62e87aSJun-ichiro itojun Hagino case AF_INET6: 444bd9f52d5SHajimu UMEMOTO if (getnameinfo(sa, sa->sa_len, NULL, 0, 445bd9f52d5SHajimu UMEMOTO pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0) 446bd9f52d5SHajimu UMEMOTO dport = 0; /*XXX*/ 4473c62e87aSJun-ichiro itojun Hagino else 448bd9f52d5SHajimu UMEMOTO dport = atoi(pbuf); 4493c62e87aSJun-ichiro itojun Hagino printf("%s%s ", str_ipaddr(sa), 4503c62e87aSJun-ichiro itojun Hagino str_prefport(sa->sa_family, 451bd9f52d5SHajimu UMEMOTO m_daddr->sadb_address_prefixlen, dport, 452bd9f52d5SHajimu UMEMOTO m_saddr->sadb_address_proto)); 4533c62e87aSJun-ichiro itojun Hagino break; 4543c62e87aSJun-ichiro itojun Hagino default: 4553c62e87aSJun-ichiro itojun Hagino printf("unknown-af "); 4563c62e87aSJun-ichiro itojun Hagino break; 4573c62e87aSJun-ichiro itojun Hagino } 4589a4365d0SYoshinobu Inoue 4599a4365d0SYoshinobu Inoue /* upper layer protocol */ 460bd9f52d5SHajimu UMEMOTO if (m_saddr->sadb_address_proto != 461bd9f52d5SHajimu UMEMOTO m_daddr->sadb_address_proto) { 4629a4365d0SYoshinobu Inoue printf("upper layer protocol mismatched.\n"); 4639a4365d0SYoshinobu Inoue return; 4649a4365d0SYoshinobu Inoue } 465bd9f52d5SHajimu UMEMOTO str_upperspec(m_saddr->sadb_address_proto, sport, dport); 466bd9f52d5SHajimu UMEMOTO } 4679a4365d0SYoshinobu Inoue else 468bd9f52d5SHajimu UMEMOTO printf("(no selector, probably per-socket policy) "); 4699a4365d0SYoshinobu Inoue 4709a4365d0SYoshinobu Inoue /* policy */ 4719a4365d0SYoshinobu Inoue { 4729a4365d0SYoshinobu Inoue char *d_xpl; 4739a4365d0SYoshinobu Inoue 4749a4365d0SYoshinobu Inoue if (m_xpl == NULL) { 4759a4365d0SYoshinobu Inoue printf("no X_POLICY extension.\n"); 4769a4365d0SYoshinobu Inoue return; 4779a4365d0SYoshinobu Inoue } 4789a4365d0SYoshinobu Inoue d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t"); 4799a4365d0SYoshinobu Inoue 4809a4365d0SYoshinobu Inoue /* dump SPD */ 4819a4365d0SYoshinobu Inoue printf("\n\t%s\n", d_xpl); 4829a4365d0SYoshinobu Inoue free(d_xpl); 4839a4365d0SYoshinobu Inoue } 4849a4365d0SYoshinobu Inoue 48533841545SHajimu UMEMOTO /* lifetime */ 486bd9f52d5SHajimu UMEMOTO if (m_lftc) { 487bd9f52d5SHajimu UMEMOTO printf("\tcreated: %s ", 488bd9f52d5SHajimu UMEMOTO str_time(m_lftc->sadb_lifetime_addtime)); 489bd9f52d5SHajimu UMEMOTO printf("lastused: %s\n", 490bd9f52d5SHajimu UMEMOTO str_time(m_lftc->sadb_lifetime_usetime)); 49133841545SHajimu UMEMOTO } 492bd9f52d5SHajimu UMEMOTO if (m_lfth) { 493bd9f52d5SHajimu UMEMOTO printf("\tlifetime: %lu(s) ", 494bd9f52d5SHajimu UMEMOTO (u_long)m_lfth->sadb_lifetime_addtime); 495bd9f52d5SHajimu UMEMOTO printf("validtime: %lu(s)\n", 496bd9f52d5SHajimu UMEMOTO (u_long)m_lfth->sadb_lifetime_usetime); 497bd9f52d5SHajimu UMEMOTO } 498bd9f52d5SHajimu UMEMOTO 49933841545SHajimu UMEMOTO 50022986c67SAndrey V. Elsukov printf("\tspid=%ld seq=%ld pid=%ld scope=", 5013c62e87aSJun-ichiro itojun Hagino (u_long)m_xpl->sadb_x_policy_id, 5029a4365d0SYoshinobu Inoue (u_long)m->sadb_msg_seq, 5039a4365d0SYoshinobu Inoue (u_long)m->sadb_msg_pid); 50422986c67SAndrey V. Elsukov GETMSGV2S(str_sp_scope, m_xpl->sadb_x_policy_scope); 50522986c67SAndrey V. Elsukov if (m_xpl->sadb_x_policy_scope == IPSEC_POLICYSCOPE_IFNET && 50622986c67SAndrey V. Elsukov if_indextoname(m_xpl->sadb_x_policy_ifindex, pbuf) != NULL) 50722986c67SAndrey V. Elsukov printf("ifname=%s", pbuf); 50822986c67SAndrey V. Elsukov printf("\n"); 5099a4365d0SYoshinobu Inoue 5109a4365d0SYoshinobu Inoue /* XXX TEST */ 5113c62e87aSJun-ichiro itojun Hagino printf("\trefcnt=%u\n", m->sadb_msg_reserved); 5129a4365d0SYoshinobu Inoue 5139a4365d0SYoshinobu Inoue return; 5149a4365d0SYoshinobu Inoue } 5159a4365d0SYoshinobu Inoue 5169a4365d0SYoshinobu Inoue /* 5179a4365d0SYoshinobu Inoue * set "ipaddress" to buffer. 5189a4365d0SYoshinobu Inoue */ 5199a4365d0SYoshinobu Inoue static char * 5203c62e87aSJun-ichiro itojun Hagino str_ipaddr(sa) 5213c62e87aSJun-ichiro itojun Hagino struct sockaddr *sa; 5229a4365d0SYoshinobu Inoue { 5233c62e87aSJun-ichiro itojun Hagino static char buf[NI_MAXHOST]; 5243c62e87aSJun-ichiro itojun Hagino const int niflag = NI_NUMERICHOST; 5259a4365d0SYoshinobu Inoue 5263c62e87aSJun-ichiro itojun Hagino if (sa == NULL) 5279a4365d0SYoshinobu Inoue return ""; 5289a4365d0SYoshinobu Inoue 5293c62e87aSJun-ichiro itojun Hagino if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0) 5309a4365d0SYoshinobu Inoue return buf; 5313c62e87aSJun-ichiro itojun Hagino return NULL; 5329a4365d0SYoshinobu Inoue } 5339a4365d0SYoshinobu Inoue 5349a4365d0SYoshinobu Inoue /* 5359a4365d0SYoshinobu Inoue * set "/prefix[port number]" to buffer. 5369a4365d0SYoshinobu Inoue */ 5379a4365d0SYoshinobu Inoue static char * 538bd9f52d5SHajimu UMEMOTO str_prefport(family, pref, port, ulp) 539bd9f52d5SHajimu UMEMOTO u_int family, pref, port, ulp; 5409a4365d0SYoshinobu Inoue { 5419a4365d0SYoshinobu Inoue static char buf[128]; 542296e054fSMunechika SUMIKAWA char prefbuf[128]; 543296e054fSMunechika SUMIKAWA char portbuf[128]; 5443c62e87aSJun-ichiro itojun Hagino int plen; 5459a4365d0SYoshinobu Inoue 5463c62e87aSJun-ichiro itojun Hagino switch (family) { 5473c62e87aSJun-ichiro itojun Hagino case AF_INET: 5483c62e87aSJun-ichiro itojun Hagino plen = sizeof(struct in_addr) << 3; 5493c62e87aSJun-ichiro itojun Hagino break; 5503c62e87aSJun-ichiro itojun Hagino case AF_INET6: 5513c62e87aSJun-ichiro itojun Hagino plen = sizeof(struct in6_addr) << 3; 5523c62e87aSJun-ichiro itojun Hagino break; 5533c62e87aSJun-ichiro itojun Hagino default: 5543c62e87aSJun-ichiro itojun Hagino return "?"; 5553c62e87aSJun-ichiro itojun Hagino } 5563c62e87aSJun-ichiro itojun Hagino 5573c62e87aSJun-ichiro itojun Hagino if (pref == plen) 5589a4365d0SYoshinobu Inoue prefbuf[0] = '\0'; 5599a4365d0SYoshinobu Inoue else 5609a4365d0SYoshinobu Inoue snprintf(prefbuf, sizeof(prefbuf), "/%u", pref); 5619a4365d0SYoshinobu Inoue 562bd9f52d5SHajimu UMEMOTO if (ulp == IPPROTO_ICMPV6) 563bd9f52d5SHajimu UMEMOTO memset(portbuf, 0, sizeof(portbuf)); 564bd9f52d5SHajimu UMEMOTO else { 5659a4365d0SYoshinobu Inoue if (port == IPSEC_PORT_ANY) 5669a4365d0SYoshinobu Inoue snprintf(portbuf, sizeof(portbuf), "[%s]", "any"); 5679a4365d0SYoshinobu Inoue else 5683c62e87aSJun-ichiro itojun Hagino snprintf(portbuf, sizeof(portbuf), "[%u]", port); 569bd9f52d5SHajimu UMEMOTO } 5709a4365d0SYoshinobu Inoue 5719a4365d0SYoshinobu Inoue snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf); 5729a4365d0SYoshinobu Inoue 5739a4365d0SYoshinobu Inoue return buf; 5749a4365d0SYoshinobu Inoue } 5759a4365d0SYoshinobu Inoue 576bd9f52d5SHajimu UMEMOTO static void 577bd9f52d5SHajimu UMEMOTO str_upperspec(ulp, p1, p2) 578bd9f52d5SHajimu UMEMOTO u_int ulp, p1, p2; 579bd9f52d5SHajimu UMEMOTO { 580bd9f52d5SHajimu UMEMOTO if (ulp == IPSEC_ULPROTO_ANY) 581bd9f52d5SHajimu UMEMOTO printf("any"); 582bd9f52d5SHajimu UMEMOTO else if (ulp == IPPROTO_ICMPV6) { 583bd9f52d5SHajimu UMEMOTO printf("icmp6"); 584bd9f52d5SHajimu UMEMOTO if (!(p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY)) 585bd9f52d5SHajimu UMEMOTO printf(" %u,%u", p1, p2); 586bd9f52d5SHajimu UMEMOTO } else { 587bd9f52d5SHajimu UMEMOTO struct protoent *ent; 588bd9f52d5SHajimu UMEMOTO 589bd9f52d5SHajimu UMEMOTO switch (ulp) { 590bd9f52d5SHajimu UMEMOTO case IPPROTO_IPV4: 591bd9f52d5SHajimu UMEMOTO printf("ip4"); 592bd9f52d5SHajimu UMEMOTO break; 593bd9f52d5SHajimu UMEMOTO default: 594bd9f52d5SHajimu UMEMOTO ent = getprotobynumber(ulp); 595bd9f52d5SHajimu UMEMOTO if (ent) 596bd9f52d5SHajimu UMEMOTO printf("%s", ent->p_name); 597bd9f52d5SHajimu UMEMOTO else 598bd9f52d5SHajimu UMEMOTO printf("%u", ulp); 599bd9f52d5SHajimu UMEMOTO 600bd9f52d5SHajimu UMEMOTO endprotoent(); 601bd9f52d5SHajimu UMEMOTO break; 602bd9f52d5SHajimu UMEMOTO } 603bd9f52d5SHajimu UMEMOTO } 604bd9f52d5SHajimu UMEMOTO } 605bd9f52d5SHajimu UMEMOTO 6069a4365d0SYoshinobu Inoue /* 6079a4365d0SYoshinobu Inoue * set "Mon Day Time Year" to buffer 6089a4365d0SYoshinobu Inoue */ 6099a4365d0SYoshinobu Inoue static char * 6103c62e87aSJun-ichiro itojun Hagino str_time(t) 6119a4365d0SYoshinobu Inoue time_t t; 6129a4365d0SYoshinobu Inoue { 6139a4365d0SYoshinobu Inoue static char buf[128]; 6149a4365d0SYoshinobu Inoue 6159a4365d0SYoshinobu Inoue if (t == 0) { 6169a4365d0SYoshinobu Inoue int i = 0; 6179a4365d0SYoshinobu Inoue for (;i < 20;) buf[i++] = ' '; 6189a4365d0SYoshinobu Inoue } else { 6199a4365d0SYoshinobu Inoue char *t0; 6209a4365d0SYoshinobu Inoue t0 = ctime(&t); 6219a4365d0SYoshinobu Inoue memcpy(buf, t0 + 4, 20); 6229a4365d0SYoshinobu Inoue } 6239a4365d0SYoshinobu Inoue 6249a4365d0SYoshinobu Inoue buf[20] = '\0'; 6259a4365d0SYoshinobu Inoue 6269a4365d0SYoshinobu Inoue return(buf); 6279a4365d0SYoshinobu Inoue } 6289a4365d0SYoshinobu Inoue 6299a4365d0SYoshinobu Inoue static void 6303c62e87aSJun-ichiro itojun Hagino str_lifetime_byte(x, str) 6319a4365d0SYoshinobu Inoue struct sadb_lifetime *x; 6329a4365d0SYoshinobu Inoue char *str; 6339a4365d0SYoshinobu Inoue { 6349a4365d0SYoshinobu Inoue double y; 6359a4365d0SYoshinobu Inoue char *unit; 6369a4365d0SYoshinobu Inoue int w; 6379a4365d0SYoshinobu Inoue 6389a4365d0SYoshinobu Inoue if (x == NULL) { 6399a4365d0SYoshinobu Inoue printf("\t%s: 0(bytes)", str); 6409a4365d0SYoshinobu Inoue return; 6419a4365d0SYoshinobu Inoue } 6429a4365d0SYoshinobu Inoue 6433c62e87aSJun-ichiro itojun Hagino #if 0 6443c62e87aSJun-ichiro itojun Hagino if ((x->sadb_lifetime_bytes) / 1024 / 1024) { 6453c62e87aSJun-ichiro itojun Hagino y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024; 6463c62e87aSJun-ichiro itojun Hagino unit = "M"; 6473c62e87aSJun-ichiro itojun Hagino w = 1; 6483c62e87aSJun-ichiro itojun Hagino } else if ((x->sadb_lifetime_bytes) / 1024) { 6493c62e87aSJun-ichiro itojun Hagino y = (x->sadb_lifetime_bytes) * 1.0 / 1024; 6503c62e87aSJun-ichiro itojun Hagino unit = "K"; 6513c62e87aSJun-ichiro itojun Hagino w = 1; 6523c62e87aSJun-ichiro itojun Hagino } else { 6539a4365d0SYoshinobu Inoue y = (x->sadb_lifetime_bytes) * 1.0; 6549a4365d0SYoshinobu Inoue unit = ""; 6559a4365d0SYoshinobu Inoue w = 0; 6563c62e87aSJun-ichiro itojun Hagino } 6573c62e87aSJun-ichiro itojun Hagino #else 6583c62e87aSJun-ichiro itojun Hagino y = (x->sadb_lifetime_bytes) * 1.0; 6593c62e87aSJun-ichiro itojun Hagino unit = ""; 6603c62e87aSJun-ichiro itojun Hagino w = 0; 6613c62e87aSJun-ichiro itojun Hagino #endif 6629a4365d0SYoshinobu Inoue printf("\t%s: %.*f(%sbytes)", str, w, y, unit); 6639a4365d0SYoshinobu Inoue } 664