| 1 | /* $FreeBSD: src/lib/libipsec/pfkey_dump.c,v 1.1.2.4 2003/04/27 00:03:36 sumikawa Exp $ */ |
| 2 | /* $DragonFly: src/lib/libipsec/pfkey_dump.c,v 1.3 2003/11/12 20:21:30 eirikn Exp $ */ |
| 3 | /* $KAME: pfkey_dump.c,v 1.28 2001/06/27 10:46:51 sakane Exp $ */ |
| 4 | |
| 5 | /* |
| 6 | * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the project nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/param.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <netinet6/ipsec.h> |
| 38 | #include <net/pfkeyv2.h> |
| 39 | #include <netkey/key_var.h> |
| 40 | #include <netkey/key_debug.h> |
| 41 | |
| 42 | #include <netinet/in.h> |
| 43 | #include <netinet6/ipsec.h> |
| 44 | #include <arpa/inet.h> |
| 45 | |
| 46 | #include <stdlib.h> |
| 47 | #include <unistd.h> |
| 48 | #include <stdio.h> |
| 49 | #include <string.h> |
| 50 | #include <time.h> |
| 51 | #include <netdb.h> |
| 52 | |
| 53 | #include "ipsec_strerror.h" |
| 54 | #include "libpfkey.h" |
| 55 | |
| 56 | /* cope with old kame headers - ugly */ |
| 57 | #ifndef SADB_X_AALG_MD5 |
| 58 | #define SADB_X_AALG_MD5 SADB_AALG_MD5 |
| 59 | #endif |
| 60 | #ifndef SADB_X_AALG_SHA |
| 61 | #define SADB_X_AALG_SHA SADB_AALG_SHA |
| 62 | #endif |
| 63 | #ifndef SADB_X_AALG_NULL |
| 64 | #define SADB_X_AALG_NULL SADB_AALG_NULL |
| 65 | #endif |
| 66 | |
| 67 | #ifndef SADB_X_EALG_BLOWFISHCBC |
| 68 | #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC |
| 69 | #endif |
| 70 | #ifndef SADB_X_EALG_CAST128CBC |
| 71 | #define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC |
| 72 | #endif |
| 73 | #ifndef SADB_X_EALG_RC5CBC |
| 74 | #ifdef SADB_EALG_RC5CBC |
| 75 | #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC |
| 76 | #endif |
| 77 | #endif |
| 78 | |
| 79 | #define GETMSGSTR(str, num) \ |
| 80 | do { \ |
| 81 | if (sizeof((str)[0]) == 0 \ |
| 82 | || num >= sizeof(str)/sizeof((str)[0])) \ |
| 83 | printf("%d ", (num)); \ |
| 84 | else if (strlen((str)[(num)]) == 0) \ |
| 85 | printf("%d ", (num)); \ |
| 86 | else \ |
| 87 | printf("%s ", (str)[(num)]); \ |
| 88 | } while (0) |
| 89 | |
| 90 | #define GETMSGV2S(v2s, num) \ |
| 91 | do { \ |
| 92 | struct val2str *p; \ |
| 93 | for (p = (v2s); p && p->str; p++) { \ |
| 94 | if (p->val == (num)) \ |
| 95 | break; \ |
| 96 | } \ |
| 97 | if (p && p->str) \ |
| 98 | printf("%s ", p->str); \ |
| 99 | else \ |
| 100 | printf("%d ", (num)); \ |
| 101 | } while (0) |
| 102 | |
| 103 | static char *str_ipaddr (struct sockaddr *); |
| 104 | static char *str_prefport (u_int, u_int, u_int); |
| 105 | static char *str_time (time_t); |
| 106 | static void str_lifetime_byte (struct sadb_lifetime *, char *); |
| 107 | |
| 108 | struct val2str { |
| 109 | int val; |
| 110 | const char *str; |
| 111 | }; |
| 112 | |
| 113 | /* |
| 114 | * Must to be re-written about following strings. |
| 115 | */ |
| 116 | static char *str_satype[] = { |
| 117 | "unspec", |
| 118 | "unknown", |
| 119 | "ah", |
| 120 | "esp", |
| 121 | "unknown", |
| 122 | "rsvp", |
| 123 | "ospfv2", |
| 124 | "ripv2", |
| 125 | "mip", |
| 126 | "ipcomp", |
| 127 | "policy", |
| 128 | "tcp", |
| 129 | }; |
| 130 | |
| 131 | static char *str_mode[] = { |
| 132 | "any", |
| 133 | "transport", |
| 134 | "tunnel", |
| 135 | }; |
| 136 | |
| 137 | static char *str_upper[] = { |
| 138 | /*0*/ "ip", "icmp", "igmp", "ggp", "ip4", |
| 139 | "", "tcp", "", "egp", "", |
| 140 | /*10*/ "", "", "", "", "", |
| 141 | "", "", "udp", "", "", |
| 142 | /*20*/ "", "", "idp", "", "", |
| 143 | "", "", "", "", "tp", |
| 144 | /*30*/ "", "", "", "", "", |
| 145 | "", "", "", "", "", |
| 146 | /*40*/ "", "ip6", "", "rt6", "frag6", |
| 147 | "", "rsvp", "gre", "", "", |
| 148 | /*50*/ "esp", "ah", "", "", "", |
| 149 | "", "", "", "icmp6", "none", |
| 150 | /*60*/ "dst6", |
| 151 | }; |
| 152 | |
| 153 | static char *str_state[] = { |
| 154 | "larval", |
| 155 | "mature", |
| 156 | "dying", |
| 157 | "dead", |
| 158 | }; |
| 159 | |
| 160 | static struct val2str str_alg_auth[] = { |
| 161 | { SADB_AALG_NONE, "none", }, |
| 162 | { SADB_AALG_MD5HMAC, "hmac-md5", }, |
| 163 | { SADB_AALG_SHA1HMAC, "hmac-sha1", }, |
| 164 | { SADB_X_AALG_MD5, "md5", }, |
| 165 | { SADB_X_AALG_SHA, "sha", }, |
| 166 | { SADB_X_AALG_NULL, "null", }, |
| 167 | { SADB_X_AALG_TCP_MD5, "tcp-md5", }, |
| 168 | #ifdef SADB_X_AALG_SHA2_256 |
| 169 | { SADB_X_AALG_SHA2_256, "hmac-sha2-256", }, |
| 170 | #endif |
| 171 | #ifdef SADB_X_AALG_SHA2_384 |
| 172 | { SADB_X_AALG_SHA2_384, "hmac-sha2-384", }, |
| 173 | #endif |
| 174 | #ifdef SADB_X_AALG_SHA2_512 |
| 175 | { SADB_X_AALG_SHA2_512, "hmac-sha2-512", }, |
| 176 | #endif |
| 177 | { -1, NULL, }, |
| 178 | }; |
| 179 | |
| 180 | static struct val2str str_alg_enc[] = { |
| 181 | { SADB_EALG_NONE, "none", }, |
| 182 | { SADB_EALG_DESCBC, "des-cbc", }, |
| 183 | { SADB_EALG_3DESCBC, "3des-cbc", }, |
| 184 | { SADB_EALG_NULL, "null", }, |
| 185 | #ifdef SADB_X_EALG_RC5CBC |
| 186 | { SADB_X_EALG_RC5CBC, "rc5-cbc", }, |
| 187 | #endif |
| 188 | { SADB_X_EALG_CAST128CBC, "cast128-cbc", }, |
| 189 | { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", }, |
| 190 | #ifdef SADB_X_EALG_RIJNDAELCBC |
| 191 | { SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", }, |
| 192 | #endif |
| 193 | #ifdef SADB_X_EALG_TWOFISHCBC |
| 194 | { SADB_X_EALG_TWOFISHCBC, "twofish-cbc", }, |
| 195 | #endif |
| 196 | { -1, NULL, }, |
| 197 | }; |
| 198 | |
| 199 | static struct val2str str_alg_comp[] = { |
| 200 | { SADB_X_CALG_NONE, "none", }, |
| 201 | { SADB_X_CALG_OUI, "oui", }, |
| 202 | { SADB_X_CALG_DEFLATE, "deflate", }, |
| 203 | { SADB_X_CALG_LZS, "lzs", }, |
| 204 | { -1, NULL, }, |
| 205 | }; |
| 206 | |
| 207 | /* |
| 208 | * dump SADB_MSG formated. For debugging, you should use kdebug_sadb(). |
| 209 | */ |
| 210 | void |
| 211 | pfkey_sadump(m) |
| 212 | struct sadb_msg *m; |
| 213 | { |
| 214 | caddr_t mhp[SADB_EXT_MAX + 1]; |
| 215 | struct sadb_sa *m_sa; |
| 216 | struct sadb_x_sa2 *m_sa2; |
| 217 | struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts; |
| 218 | struct sadb_address *m_saddr, *m_daddr, *m_paddr; |
| 219 | struct sadb_key *m_auth, *m_enc; |
| 220 | struct sadb_ident *m_sid, *m_did; |
| 221 | struct sadb_sens *m_sens; |
| 222 | |
| 223 | /* check pfkey message. */ |
| 224 | if (pfkey_align(m, mhp)) { |
| 225 | printf("%s\n", ipsec_strerror()); |
| 226 | return; |
| 227 | } |
| 228 | if (pfkey_check(mhp)) { |
| 229 | printf("%s\n", ipsec_strerror()); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA]; |
| 234 | m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2]; |
| 235 | m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT]; |
| 236 | m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; |
| 237 | m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT]; |
| 238 | m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]; |
| 239 | m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]; |
| 240 | m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY]; |
| 241 | m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH]; |
| 242 | m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT]; |
| 243 | m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC]; |
| 244 | m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST]; |
| 245 | m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY]; |
| 246 | |
| 247 | /* source address */ |
| 248 | if (m_saddr == NULL) { |
| 249 | printf("no ADDRESS_SRC extension.\n"); |
| 250 | return; |
| 251 | } |
| 252 | printf("%s ", str_ipaddr((struct sockaddr *)(m_saddr + 1))); |
| 253 | |
| 254 | /* destination address */ |
| 255 | if (m_daddr == NULL) { |
| 256 | printf("no ADDRESS_DST extension.\n"); |
| 257 | return; |
| 258 | } |
| 259 | printf("%s ", str_ipaddr((struct sockaddr *)(m_daddr + 1))); |
| 260 | |
| 261 | /* SA type */ |
| 262 | if (m_sa == NULL) { |
| 263 | printf("no SA extension.\n"); |
| 264 | return; |
| 265 | } |
| 266 | if (m_sa2 == NULL) { |
| 267 | printf("no SA2 extension.\n"); |
| 268 | return; |
| 269 | } |
| 270 | printf("\n\t"); |
| 271 | |
| 272 | GETMSGSTR(str_satype, m->sadb_msg_satype); |
| 273 | |
| 274 | printf("mode="); |
| 275 | GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode); |
| 276 | |
| 277 | printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n", |
| 278 | (u_int32_t)ntohl(m_sa->sadb_sa_spi), |
| 279 | (u_int32_t)ntohl(m_sa->sadb_sa_spi), |
| 280 | (u_int32_t)m_sa2->sadb_x_sa2_reqid, |
| 281 | (u_int32_t)m_sa2->sadb_x_sa2_reqid); |
| 282 | |
| 283 | /* encryption key */ |
| 284 | if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) { |
| 285 | printf("\tC: "); |
| 286 | GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt); |
| 287 | } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) { |
| 288 | if (m_enc != NULL) { |
| 289 | printf("\tE: "); |
| 290 | GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt); |
| 291 | ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc), |
| 292 | m_enc->sadb_key_bits / 8); |
| 293 | printf("\n"); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* authentication key */ |
| 298 | if (m_auth != NULL) { |
| 299 | printf("\tA: "); |
| 300 | GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth); |
| 301 | ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth), |
| 302 | m_auth->sadb_key_bits / 8); |
| 303 | printf("\n"); |
| 304 | } |
| 305 | |
| 306 | /* replay windoe size & flags */ |
| 307 | printf("\tseq=0x%08x replay=%u flags=0x%08x ", |
| 308 | m_sa2->sadb_x_sa2_sequence, |
| 309 | m_sa->sadb_sa_replay, |
| 310 | m_sa->sadb_sa_flags); |
| 311 | |
| 312 | /* state */ |
| 313 | printf("state="); |
| 314 | GETMSGSTR(str_state, m_sa->sadb_sa_state); |
| 315 | printf("\n"); |
| 316 | |
| 317 | /* lifetime */ |
| 318 | if (m_lftc != NULL) { |
| 319 | time_t tmp_time = time(0); |
| 320 | |
| 321 | printf("\tcreated: %s", |
| 322 | str_time(m_lftc->sadb_lifetime_addtime)); |
| 323 | printf("\tcurrent: %s\n", str_time(tmp_time)); |
| 324 | printf("\tdiff: %lu(s)", |
| 325 | (u_long)(m_lftc->sadb_lifetime_addtime == 0 ? |
| 326 | 0 : (tmp_time - m_lftc->sadb_lifetime_addtime))); |
| 327 | |
| 328 | printf("\thard: %lu(s)", |
| 329 | (u_long)(m_lfth == NULL ? |
| 330 | 0 : m_lfth->sadb_lifetime_addtime)); |
| 331 | printf("\tsoft: %lu(s)\n", |
| 332 | (u_long)(m_lfts == NULL ? |
| 333 | 0 : m_lfts->sadb_lifetime_addtime)); |
| 334 | |
| 335 | printf("\tlast: %s", |
| 336 | str_time(m_lftc->sadb_lifetime_usetime)); |
| 337 | printf("\thard: %lu(s)", |
| 338 | (u_long)(m_lfth == NULL ? |
| 339 | 0 : m_lfth->sadb_lifetime_usetime)); |
| 340 | printf("\tsoft: %lu(s)\n", |
| 341 | (u_long)(m_lfts == NULL ? |
| 342 | 0 : m_lfts->sadb_lifetime_usetime)); |
| 343 | |
| 344 | str_lifetime_byte(m_lftc, "current"); |
| 345 | str_lifetime_byte(m_lfth, "hard"); |
| 346 | str_lifetime_byte(m_lfts, "soft"); |
| 347 | printf("\n"); |
| 348 | |
| 349 | printf("\tallocated: %lu", |
| 350 | (unsigned long)m_lftc->sadb_lifetime_allocations); |
| 351 | printf("\thard: %lu", |
| 352 | (u_long)(m_lfth == NULL ? |
| 353 | 0 : m_lfth->sadb_lifetime_allocations)); |
| 354 | printf("\tsoft: %lu\n", |
| 355 | (u_long)(m_lfts == NULL ? |
| 356 | 0 : m_lfts->sadb_lifetime_allocations)); |
| 357 | } |
| 358 | |
| 359 | printf("\tsadb_seq=%lu pid=%lu ", |
| 360 | (u_long)m->sadb_msg_seq, |
| 361 | (u_long)m->sadb_msg_pid); |
| 362 | |
| 363 | /* XXX DEBUG */ |
| 364 | printf("refcnt=%u\n", m->sadb_msg_reserved); |
| 365 | |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | void |
| 370 | pfkey_spdump(m) |
| 371 | struct sadb_msg *m; |
| 372 | { |
| 373 | char pbuf[NI_MAXSERV]; |
| 374 | caddr_t mhp[SADB_EXT_MAX + 1]; |
| 375 | struct sadb_address *m_saddr, *m_daddr; |
| 376 | struct sadb_x_policy *m_xpl; |
| 377 | struct sadb_lifetime *m_lft = NULL; |
| 378 | struct sockaddr *sa; |
| 379 | u_int16_t port; |
| 380 | |
| 381 | /* check pfkey message. */ |
| 382 | if (pfkey_align(m, mhp)) { |
| 383 | printf("%s\n", ipsec_strerror()); |
| 384 | return; |
| 385 | } |
| 386 | if (pfkey_check(mhp)) { |
| 387 | printf("%s\n", ipsec_strerror()); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]; |
| 392 | m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]; |
| 393 | m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY]; |
| 394 | m_lft = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; |
| 395 | |
| 396 | /* source address */ |
| 397 | if (m_saddr == NULL) { |
| 398 | printf("no ADDRESS_SRC extension.\n"); |
| 399 | return; |
| 400 | } |
| 401 | sa = (struct sockaddr *)(m_saddr + 1); |
| 402 | switch (sa->sa_family) { |
| 403 | case AF_INET: |
| 404 | case AF_INET6: |
| 405 | if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf), |
| 406 | NI_NUMERICSERV) != 0) |
| 407 | port = 0; /*XXX*/ |
| 408 | else |
| 409 | port = atoi(pbuf); |
| 410 | printf("%s%s ", str_ipaddr(sa), |
| 411 | str_prefport(sa->sa_family, |
| 412 | m_saddr->sadb_address_prefixlen, port)); |
| 413 | break; |
| 414 | default: |
| 415 | printf("unknown-af "); |
| 416 | break; |
| 417 | } |
| 418 | |
| 419 | /* destination address */ |
| 420 | if (m_daddr == NULL) { |
| 421 | printf("no ADDRESS_DST extension.\n"); |
| 422 | return; |
| 423 | } |
| 424 | sa = (struct sockaddr *)(m_daddr + 1); |
| 425 | switch (sa->sa_family) { |
| 426 | case AF_INET: |
| 427 | case AF_INET6: |
| 428 | if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf), |
| 429 | NI_NUMERICSERV) != 0) |
| 430 | port = 0; /*XXX*/ |
| 431 | else |
| 432 | port = atoi(pbuf); |
| 433 | printf("%s%s ", str_ipaddr(sa), |
| 434 | str_prefport(sa->sa_family, |
| 435 | m_daddr->sadb_address_prefixlen, port)); |
| 436 | break; |
| 437 | default: |
| 438 | printf("unknown-af "); |
| 439 | break; |
| 440 | } |
| 441 | |
| 442 | /* upper layer protocol */ |
| 443 | if (m_saddr->sadb_address_proto != m_daddr->sadb_address_proto) { |
| 444 | printf("upper layer protocol mismatched.\n"); |
| 445 | return; |
| 446 | } |
| 447 | if (m_saddr->sadb_address_proto == IPSEC_ULPROTO_ANY) |
| 448 | printf("any"); |
| 449 | else |
| 450 | GETMSGSTR(str_upper, m_saddr->sadb_address_proto); |
| 451 | |
| 452 | /* policy */ |
| 453 | { |
| 454 | char *d_xpl; |
| 455 | |
| 456 | if (m_xpl == NULL) { |
| 457 | printf("no X_POLICY extension.\n"); |
| 458 | return; |
| 459 | } |
| 460 | d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t"); |
| 461 | |
| 462 | /* dump SPD */ |
| 463 | printf("\n\t%s\n", d_xpl); |
| 464 | free(d_xpl); |
| 465 | } |
| 466 | |
| 467 | /* lifetime */ |
| 468 | if (m_lft) { |
| 469 | printf("\tlifetime:%lu validtime:%lu\n", |
| 470 | (u_long)m_lft->sadb_lifetime_addtime, |
| 471 | (u_long)m_lft->sadb_lifetime_usetime); |
| 472 | } |
| 473 | |
| 474 | printf("\tspid=%ld seq=%ld pid=%ld\n", |
| 475 | (u_long)m_xpl->sadb_x_policy_id, |
| 476 | (u_long)m->sadb_msg_seq, |
| 477 | (u_long)m->sadb_msg_pid); |
| 478 | |
| 479 | /* XXX TEST */ |
| 480 | printf("\trefcnt=%u\n", m->sadb_msg_reserved); |
| 481 | |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * set "ipaddress" to buffer. |
| 487 | */ |
| 488 | static char * |
| 489 | str_ipaddr(sa) |
| 490 | struct sockaddr *sa; |
| 491 | { |
| 492 | static char buf[NI_MAXHOST]; |
| 493 | #ifdef NI_WITHSCOPEID |
| 494 | const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID; |
| 495 | #else |
| 496 | const int niflag = NI_NUMERICHOST; |
| 497 | #endif |
| 498 | |
| 499 | if (sa == NULL) |
| 500 | return ""; |
| 501 | |
| 502 | if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0) |
| 503 | return buf; |
| 504 | return NULL; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * set "/prefix[port number]" to buffer. |
| 509 | */ |
| 510 | static char * |
| 511 | str_prefport(family, pref, port) |
| 512 | u_int family, pref, port; |
| 513 | { |
| 514 | static char buf[128]; |
| 515 | char prefbuf[128]; |
| 516 | char portbuf[128]; |
| 517 | int plen; |
| 518 | |
| 519 | switch (family) { |
| 520 | case AF_INET: |
| 521 | plen = sizeof(struct in_addr) << 3; |
| 522 | break; |
| 523 | case AF_INET6: |
| 524 | plen = sizeof(struct in6_addr) << 3; |
| 525 | break; |
| 526 | default: |
| 527 | return "?"; |
| 528 | } |
| 529 | |
| 530 | if (pref == plen) |
| 531 | prefbuf[0] = '\0'; |
| 532 | else |
| 533 | snprintf(prefbuf, sizeof(prefbuf), "/%u", pref); |
| 534 | |
| 535 | if (port == IPSEC_PORT_ANY) |
| 536 | snprintf(portbuf, sizeof(portbuf), "[%s]", "any"); |
| 537 | else |
| 538 | snprintf(portbuf, sizeof(portbuf), "[%u]", port); |
| 539 | |
| 540 | snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf); |
| 541 | |
| 542 | return buf; |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * set "Mon Day Time Year" to buffer |
| 547 | */ |
| 548 | static char * |
| 549 | str_time(t) |
| 550 | time_t t; |
| 551 | { |
| 552 | static char buf[128]; |
| 553 | |
| 554 | if (t == 0) { |
| 555 | int i = 0; |
| 556 | for (;i < 20;) buf[i++] = ' '; |
| 557 | } else { |
| 558 | char *t0; |
| 559 | t0 = ctime(&t); |
| 560 | memcpy(buf, t0 + 4, 20); |
| 561 | } |
| 562 | |
| 563 | buf[20] = '\0'; |
| 564 | |
| 565 | return(buf); |
| 566 | } |
| 567 | |
| 568 | static void |
| 569 | str_lifetime_byte(x, str) |
| 570 | struct sadb_lifetime *x; |
| 571 | char *str; |
| 572 | { |
| 573 | double y; |
| 574 | char *unit; |
| 575 | int w; |
| 576 | |
| 577 | if (x == NULL) { |
| 578 | printf("\t%s: 0(bytes)", str); |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | #if 0 |
| 583 | if ((x->sadb_lifetime_bytes) / 1024 / 1024) { |
| 584 | y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024; |
| 585 | unit = "M"; |
| 586 | w = 1; |
| 587 | } else if ((x->sadb_lifetime_bytes) / 1024) { |
| 588 | y = (x->sadb_lifetime_bytes) * 1.0 / 1024; |
| 589 | unit = "K"; |
| 590 | w = 1; |
| 591 | } else { |
| 592 | y = (x->sadb_lifetime_bytes) * 1.0; |
| 593 | unit = ""; |
| 594 | w = 0; |
| 595 | } |
| 596 | #else |
| 597 | y = (x->sadb_lifetime_bytes) * 1.0; |
| 598 | unit = ""; |
| 599 | w = 0; |
| 600 | #endif |
| 601 | printf("\t%s: %.*f(%sbytes)", str, w, y, unit); |
| 602 | } |