1 /* $NetBSD: pfkey_dump.c,v 1.20 2012/01/09 15:25:13 drochner Exp $ */ 2 3 /* $KAME: pfkey_dump.c,v 1.45 2003/09/08 10:14:56 itojun 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 #ifdef HAVE_CONFIG_H 35 #include "config.h" 36 #endif 37 38 #include <sys/types.h> 39 #include <sys/param.h> 40 #include <sys/socket.h> 41 #include PATH_IPSEC_H 42 #include <net/pfkeyv2.h> 43 44 #include <netinet/in.h> 45 #include <arpa/inet.h> 46 47 #include <stdlib.h> 48 #include <unistd.h> 49 #include <stdio.h> 50 #include <string.h> 51 #include <time.h> 52 #include <netdb.h> 53 54 #include "ipsec_strerror.h" 55 #include "libpfkey.h" 56 57 /* cope with old kame headers - ugly */ 58 #ifndef SADB_X_AALG_MD5 59 #define SADB_X_AALG_MD5 SADB_AALG_MD5 60 #endif 61 #ifndef SADB_X_AALG_SHA 62 #define SADB_X_AALG_SHA SADB_AALG_SHA 63 #endif 64 #ifndef SADB_X_AALG_NULL 65 #define SADB_X_AALG_NULL SADB_AALG_NULL 66 #endif 67 68 #ifndef SADB_X_EALG_BLOWFISHCBC 69 #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC 70 #endif 71 #ifndef SADB_X_EALG_CAST128CBC 72 #define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC 73 #endif 74 #ifndef SADB_X_EALG_RC5CBC 75 #ifdef SADB_EALG_RC5CBC 76 #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC 77 #endif 78 #endif 79 #if defined(SADB_X_EALG_AES) && ! defined(SADB_X_EALG_AESCBC) 80 #define SADB_X_EALG_AESCBC SADB_X_EALG_AES 81 #endif 82 83 #define GETMSGSTR(str, num) \ 84 do { \ 85 /*CONSTCOND*/ \ 86 if (sizeof((str)[0]) == 0 \ 87 || num >= sizeof(str)/sizeof((str)[0])) \ 88 printf("%u ", (num)); \ 89 else if (strlen((str)[(num)]) == 0) \ 90 printf("%u ", (num)); \ 91 else \ 92 printf("%s ", (str)[(num)]); \ 93 } while (/*CONSTCOND*/0) 94 95 #define GETMSGV2S(v2s, num) \ 96 do { \ 97 struct val2str *p; \ 98 for (p = (v2s); p && p->str; p++) { \ 99 if (p->val == (num)) \ 100 break; \ 101 } \ 102 if (p && p->str) \ 103 printf("%s ", p->str); \ 104 else \ 105 printf("%u ", (num)); \ 106 } while (/*CONSTCOND*/0) 107 108 static char *str_ipaddr __P((struct sockaddr *)); 109 static char *str_ipport __P((struct sockaddr *)); 110 static char *str_prefport __P((u_int, u_int, u_int, u_int)); 111 static void str_upperspec __P((u_int, u_int, u_int)); 112 static char *str_time __P((time_t)); 113 static void str_lifetime_byte __P((struct sadb_lifetime *, char *)); 114 static void pfkey_sadump1(struct sadb_msg *, int); 115 static void pfkey_spdump1(struct sadb_msg *, int); 116 117 struct val2str { 118 int val; 119 const char *str; 120 }; 121 122 /* 123 * Must to be re-written about following strings. 124 */ 125 static char *str_satype[] = { 126 "unspec", 127 "unknown", 128 "ah", 129 "esp", 130 "unknown", 131 "rsvp", 132 "ospfv2", 133 "ripv2", 134 "mip", 135 "ipcomp", 136 "policy", 137 "tcp", 138 }; 139 140 static char *str_mode[] = { 141 "any", 142 "transport", 143 "tunnel", 144 }; 145 146 static char *str_state[] = { 147 "larval", 148 "mature", 149 "dying", 150 "dead", 151 }; 152 153 static struct val2str str_alg_auth[] = { 154 { SADB_AALG_NONE, "none", }, 155 { SADB_AALG_MD5HMAC, "hmac-md5", }, 156 { SADB_AALG_SHA1HMAC, "hmac-sha1", }, 157 { SADB_X_AALG_MD5, "md5", }, 158 { SADB_X_AALG_SHA, "sha", }, 159 { SADB_X_AALG_NULL, "null", }, 160 #ifdef SADB_X_AALG_TCP_MD5 161 { SADB_X_AALG_TCP_MD5, "tcp-md5", }, 162 #endif 163 #ifdef SADB_X_AALG_SHA2_256 164 { SADB_X_AALG_SHA2_256, "hmac-sha256", }, 165 #endif 166 #ifdef SADB_X_AALG_SHA2_384 167 { SADB_X_AALG_SHA2_384, "hmac-sha384", }, 168 #endif 169 #ifdef SADB_X_AALG_SHA2_512 170 { SADB_X_AALG_SHA2_512, "hmac-sha512", }, 171 #endif 172 #ifdef SADB_X_AALG_RIPEMD160HMAC 173 { SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", }, 174 #endif 175 #ifdef SADB_X_AALG_AES_XCBC_MAC 176 { SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", }, 177 #endif 178 { -1, NULL, }, 179 }; 180 181 static struct val2str str_alg_enc[] = { 182 { SADB_EALG_NONE, "none", }, 183 { SADB_EALG_DESCBC, "des-cbc", }, 184 { SADB_EALG_3DESCBC, "3des-cbc", }, 185 { SADB_EALG_NULL, "null", }, 186 #ifdef SADB_X_EALG_RC5CBC 187 { SADB_X_EALG_RC5CBC, "rc5-cbc", }, 188 #endif 189 { SADB_X_EALG_CAST128CBC, "cast128-cbc", }, 190 { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", }, 191 #ifdef SADB_X_EALG_AESCBC 192 { SADB_X_EALG_AESCBC, "aes-cbc", }, 193 #endif 194 #ifdef SADB_X_EALG_TWOFISHCBC 195 { SADB_X_EALG_TWOFISHCBC, "twofish-cbc", }, 196 #endif 197 #ifdef SADB_X_EALG_AESCTR 198 { SADB_X_EALG_AESCTR, "aes-ctr", }, 199 #endif 200 #ifdef SADB_X_EALG_AESGCM16 201 { SADB_X_EALG_AESGCM16, "aes-gcm-16", }, 202 #endif 203 #ifdef SADB_X_EALG_AESGMAC 204 { SADB_X_EALG_AESGMAC, "aes-gmac", }, 205 #endif 206 #ifdef SADB_X_EALG_CAMELLIACBC 207 { SADB_X_EALG_CAMELLIACBC, "camellia-cbc", }, 208 #endif 209 { -1, NULL, }, 210 }; 211 212 static struct val2str str_alg_comp[] = { 213 { SADB_X_CALG_NONE, "none", }, 214 { SADB_X_CALG_OUI, "oui", }, 215 { SADB_X_CALG_DEFLATE, "deflate", }, 216 { SADB_X_CALG_LZS, "lzs", }, 217 { -1, NULL, }, 218 }; 219 220 /* 221 * dump SADB_MSG formated. For debugging, you should use kdebug_sadb(). 222 */ 223 224 void 225 pfkey_sadump(m) 226 struct sadb_msg *m; 227 { 228 pfkey_sadump1(m, 0); 229 } 230 231 void 232 pfkey_sadump_withports(m) 233 struct sadb_msg *m; 234 { 235 pfkey_sadump1(m, 1); 236 } 237 238 void 239 pfkey_sadump1(m, withports) 240 struct sadb_msg *m; 241 int withports; 242 { 243 caddr_t mhp[SADB_EXT_MAX + 1]; 244 struct sadb_sa *m_sa; 245 struct sadb_x_sa2 *m_sa2; 246 struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts; 247 struct sadb_address *m_saddr, *m_daddr; 248 #ifdef notdef 249 struct sadb_address *m_paddr; 250 #endif 251 struct sadb_key *m_auth, *m_enc; 252 #ifdef notdef 253 struct sadb_ident *m_sid, *m_did; 254 struct sadb_sens *m_sens; 255 #endif 256 #ifdef SADB_X_EXT_SEC_CTX 257 struct sadb_x_sec_ctx *m_sec_ctx; 258 #endif 259 #ifdef SADB_X_EXT_NAT_T_TYPE 260 struct sadb_x_nat_t_type *natt_type; 261 struct sadb_x_nat_t_port *natt_sport, *natt_dport; 262 struct sadb_address *natt_oa; 263 #ifdef SADB_X_EXT_NAT_T_FRAG 264 struct sadb_x_nat_t_frag *esp_frag; 265 #endif 266 267 int use_natt = 0; 268 #endif 269 struct sockaddr *sa; 270 271 /* check pfkey message. */ 272 if (pfkey_align(m, mhp)) { 273 printf("%s\n", ipsec_strerror()); 274 return; 275 } 276 if (pfkey_check(mhp)) { 277 printf("%s\n", ipsec_strerror()); 278 return; 279 } 280 281 m_sa = (void *)mhp[SADB_EXT_SA]; 282 m_sa2 = (void *)mhp[SADB_X_EXT_SA2]; 283 m_lftc = (void *)mhp[SADB_EXT_LIFETIME_CURRENT]; 284 m_lfth = (void *)mhp[SADB_EXT_LIFETIME_HARD]; 285 m_lfts = (void *)mhp[SADB_EXT_LIFETIME_SOFT]; 286 m_saddr = (void *)mhp[SADB_EXT_ADDRESS_SRC]; 287 m_daddr = (void *)mhp[SADB_EXT_ADDRESS_DST]; 288 #ifdef notdef 289 m_paddr = (void *)mhp[SADB_EXT_ADDRESS_PROXY]; 290 #endif 291 m_auth = (void *)mhp[SADB_EXT_KEY_AUTH]; 292 m_enc = (void *)mhp[SADB_EXT_KEY_ENCRYPT]; 293 #ifdef notdef 294 m_sid = (void *)mhp[SADB_EXT_IDENTITY_SRC]; 295 m_did = (void *)mhp[SADB_EXT_IDENTITY_DST]; 296 m_sens = (void *)mhp[SADB_EXT_SENSITIVITY]; 297 #endif 298 #ifdef SADB_X_EXT_SEC_CTX 299 m_sec_ctx = (struct sadb_x_sec_ctx *)mhp[SADB_X_EXT_SEC_CTX]; 300 #endif 301 #ifdef SADB_X_EXT_NAT_T_TYPE 302 natt_type = (void *)mhp[SADB_X_EXT_NAT_T_TYPE]; 303 natt_sport = (void *)mhp[SADB_X_EXT_NAT_T_SPORT]; 304 natt_dport = (void *)mhp[SADB_X_EXT_NAT_T_DPORT]; 305 natt_oa = (void *)mhp[SADB_X_EXT_NAT_T_OA]; 306 #ifdef SADB_X_EXT_NAT_T_FRAG 307 esp_frag = (void *)mhp[SADB_X_EXT_NAT_T_FRAG]; 308 #endif 309 310 if (natt_type && natt_type->sadb_x_nat_t_type_type) 311 use_natt = 1; 312 #endif 313 /* source address */ 314 if (m_saddr == NULL) { 315 printf("no ADDRESS_SRC extension.\n"); 316 return; 317 } 318 sa = (void *)(m_saddr + 1); 319 if (withports) 320 printf("%s[%s]", str_ipaddr(sa), str_ipport(sa)); 321 else 322 printf("%s", str_ipaddr(sa)); 323 #ifdef SADB_X_EXT_NAT_T_TYPE 324 if (use_natt && natt_sport) 325 printf("[%u]", ntohs(natt_sport->sadb_x_nat_t_port_port)); 326 #endif 327 printf(" "); 328 329 /* destination address */ 330 if (m_daddr == NULL) { 331 printf(" no ADDRESS_DST extension.\n"); 332 return; 333 } 334 sa = (void *)(m_daddr + 1); 335 if (withports) 336 printf("%s[%s]", str_ipaddr(sa), str_ipport(sa)); 337 else 338 printf("%s", str_ipaddr(sa)); 339 #ifdef SADB_X_EXT_NAT_T_TYPE 340 if (use_natt && natt_dport) 341 printf("[%u]", ntohs(natt_dport->sadb_x_nat_t_port_port)); 342 #endif 343 printf(" "); 344 345 /* SA type */ 346 if (m_sa == NULL) { 347 printf("no SA extension.\n"); 348 return; 349 } 350 if (m_sa2 == NULL) { 351 printf("no SA2 extension.\n"); 352 return; 353 } 354 printf("\n\t"); 355 356 #ifdef SADB_X_EXT_NAT_T_TYPE 357 if (use_natt && m->sadb_msg_satype == SADB_SATYPE_ESP) 358 printf("esp-udp "); 359 else if (use_natt) 360 printf("natt+"); 361 362 if (!use_natt || m->sadb_msg_satype != SADB_SATYPE_ESP) 363 #endif 364 GETMSGSTR(str_satype, m->sadb_msg_satype); 365 366 printf("mode="); 367 GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode); 368 369 printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n", 370 (u_int32_t)ntohl(m_sa->sadb_sa_spi), 371 (u_int32_t)ntohl(m_sa->sadb_sa_spi), 372 (u_int32_t)m_sa2->sadb_x_sa2_reqid, 373 (u_int32_t)m_sa2->sadb_x_sa2_reqid); 374 375 #ifdef SADB_X_EXT_NAT_T_TYPE 376 /* other NAT-T information */ 377 if (use_natt && natt_oa) 378 printf("\tNAT OA=%s\n", 379 str_ipaddr((void *)(natt_oa + 1))); 380 381 #ifdef SADB_X_EXT_NAT_T_FRAG 382 if (use_natt && esp_frag && esp_frag->sadb_x_nat_t_frag_fraglen != 0) 383 printf("\tNAT-T esp_frag=%u\n", esp_frag->sadb_x_nat_t_frag_fraglen); 384 #endif 385 #endif 386 387 /* encryption key */ 388 if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) { 389 printf("\tC: "); 390 GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt); 391 } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) { 392 if (m_enc != NULL) { 393 printf("\tE: "); 394 GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt); 395 ipsec_hexdump((caddr_t)(void *)m_enc + sizeof(*m_enc), 396 m_enc->sadb_key_bits / 8); 397 printf("\n"); 398 } 399 } 400 401 /* authentication key */ 402 if (m_auth != NULL) { 403 printf("\tA: "); 404 GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth); 405 ipsec_hexdump((caddr_t)(void *)m_auth + sizeof(*m_auth), 406 m_auth->sadb_key_bits / 8); 407 printf("\n"); 408 } 409 410 /* replay windoe size & flags */ 411 printf("\tseq=0x%08x replay=%u flags=0x%08x ", 412 m_sa2->sadb_x_sa2_sequence, 413 m_sa->sadb_sa_replay, 414 m_sa->sadb_sa_flags); 415 416 /* state */ 417 printf("state="); 418 GETMSGSTR(str_state, m_sa->sadb_sa_state); 419 printf("\n"); 420 421 /* lifetime */ 422 if (m_lftc != NULL) { 423 time_t tmp_time = time(0); 424 425 printf("\tcreated: %s", 426 str_time((long)m_lftc->sadb_lifetime_addtime)); 427 printf("\tcurrent: %s\n", str_time(tmp_time)); 428 printf("\tdiff: %lu(s)", 429 (u_long)(m_lftc->sadb_lifetime_addtime == 0 ? 430 0 : (tmp_time - m_lftc->sadb_lifetime_addtime))); 431 432 printf("\thard: %lu(s)", 433 (u_long)(m_lfth == NULL ? 434 0 : m_lfth->sadb_lifetime_addtime)); 435 printf("\tsoft: %lu(s)\n", 436 (u_long)(m_lfts == NULL ? 437 0 : m_lfts->sadb_lifetime_addtime)); 438 439 printf("\tlast: %s", 440 str_time((long)m_lftc->sadb_lifetime_usetime)); 441 printf("\thard: %lu(s)", 442 (u_long)(m_lfth == NULL ? 443 0 : m_lfth->sadb_lifetime_usetime)); 444 printf("\tsoft: %lu(s)\n", 445 (u_long)(m_lfts == NULL ? 446 0 : m_lfts->sadb_lifetime_usetime)); 447 448 str_lifetime_byte(m_lftc, "current"); 449 str_lifetime_byte(m_lfth, "hard"); 450 str_lifetime_byte(m_lfts, "soft"); 451 printf("\n"); 452 453 printf("\tallocated: %lu", 454 (unsigned long)m_lftc->sadb_lifetime_allocations); 455 printf("\thard: %lu", 456 (u_long)(m_lfth == NULL ? 457 0 : m_lfth->sadb_lifetime_allocations)); 458 printf("\tsoft: %lu\n", 459 (u_long)(m_lfts == NULL ? 460 0 : m_lfts->sadb_lifetime_allocations)); 461 } 462 463 #ifdef SADB_X_EXT_SEC_CTX 464 if (m_sec_ctx != NULL) { 465 printf("\tsecurity context doi: %u\n", 466 m_sec_ctx->sadb_x_ctx_doi); 467 printf("\tsecurity context algorithm: %u\n", 468 m_sec_ctx->sadb_x_ctx_alg); 469 printf("\tsecurity context length: %u\n", 470 m_sec_ctx->sadb_x_ctx_len); 471 printf("\tsecurity context: %s\n", 472 (char *)m_sec_ctx + sizeof(struct sadb_x_sec_ctx)); 473 } 474 #endif 475 476 printf("\tsadb_seq=%lu pid=%lu ", 477 (u_long)m->sadb_msg_seq, 478 (u_long)m->sadb_msg_pid); 479 480 /* XXX DEBUG */ 481 printf("refcnt=%u\n", m->sadb_msg_reserved); 482 483 return; 484 } 485 486 void 487 pfkey_spdump(m) 488 struct sadb_msg *m; 489 { 490 pfkey_spdump1(m, 0); 491 } 492 493 void 494 pfkey_spdump_withports(m) 495 struct sadb_msg *m; 496 { 497 pfkey_spdump1(m, 1); 498 } 499 500 static void 501 pfkey_spdump1(m, withports) 502 struct sadb_msg *m; 503 int withports; 504 { 505 char pbuf[NI_MAXSERV]; 506 caddr_t mhp[SADB_EXT_MAX + 1]; 507 struct sadb_address *m_saddr, *m_daddr; 508 #ifdef SADB_X_EXT_TAG 509 struct sadb_x_tag *m_tag; 510 #endif 511 struct sadb_x_policy *m_xpl; 512 struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL; 513 #ifdef SADB_X_EXT_SEC_CTX 514 struct sadb_x_sec_ctx *m_sec_ctx; 515 #endif 516 struct sockaddr *sa; 517 u_int16_t sport = 0, dport = 0; 518 519 /* check pfkey message. */ 520 if (pfkey_align(m, mhp)) { 521 printf("%s\n", ipsec_strerror()); 522 return; 523 } 524 if (pfkey_check(mhp)) { 525 printf("%s\n", ipsec_strerror()); 526 return; 527 } 528 529 m_saddr = (void *)mhp[SADB_EXT_ADDRESS_SRC]; 530 m_daddr = (void *)mhp[SADB_EXT_ADDRESS_DST]; 531 #ifdef SADB_X_EXT_TAG 532 m_tag = (void *)mhp[SADB_X_EXT_TAG]; 533 #endif 534 m_xpl = (void *)mhp[SADB_X_EXT_POLICY]; 535 m_lftc = (void *)mhp[SADB_EXT_LIFETIME_CURRENT]; 536 m_lfth = (void *)mhp[SADB_EXT_LIFETIME_HARD]; 537 538 #ifdef SADB_X_EXT_SEC_CTX 539 m_sec_ctx = (struct sadb_x_sec_ctx *)mhp[SADB_X_EXT_SEC_CTX]; 540 #endif 541 #ifdef __linux__ 542 /* *bsd indicates per-socket policies by omiting src and dst 543 * extensions. Linux always includes them, but we can catch it 544 * by checkin for policy id. 545 */ 546 if (m_xpl->sadb_x_policy_id % 8 >= 3) { 547 printf("(per-socket policy) "); 548 } else 549 #endif 550 if (m_saddr && m_daddr) { 551 /* source address */ 552 sa = (void *)(m_saddr + 1); 553 switch (sa->sa_family) { 554 case AF_INET: 555 case AF_INET6: 556 if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), NULL, 557 0, pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0) 558 sport = 0; /*XXX*/ 559 else 560 sport = atoi(pbuf); 561 printf("%s%s ", str_ipaddr(sa), 562 str_prefport((u_int)sa->sa_family, 563 (u_int)m_saddr->sadb_address_prefixlen, 564 (u_int)sport, 565 (u_int)m_saddr->sadb_address_proto)); 566 break; 567 default: 568 printf("unknown-af "); 569 break; 570 } 571 572 /* destination address */ 573 sa = (void *)(m_daddr + 1); 574 switch (sa->sa_family) { 575 case AF_INET: 576 case AF_INET6: 577 if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), NULL, 578 0, pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0) 579 dport = 0; /*XXX*/ 580 else 581 dport = atoi(pbuf); 582 printf("%s%s ", str_ipaddr(sa), 583 str_prefport((u_int)sa->sa_family, 584 (u_int)m_daddr->sadb_address_prefixlen, 585 (u_int)dport, 586 (u_int)m_saddr->sadb_address_proto)); 587 break; 588 default: 589 printf("unknown-af "); 590 break; 591 } 592 593 /* upper layer protocol */ 594 if (m_saddr->sadb_address_proto != 595 m_daddr->sadb_address_proto) { 596 printf("upper layer protocol mismatched.\n"); 597 return; 598 } 599 str_upperspec((u_int)m_saddr->sadb_address_proto, (u_int)sport, 600 (u_int)dport); 601 } 602 #ifdef SADB_X_EXT_TAG 603 else if (m_tag) 604 printf("tagged \"%s\" ", m_tag->sadb_x_tag_name); 605 #endif 606 else 607 printf("(no selector, probably per-socket policy) "); 608 609 /* policy */ 610 { 611 char *d_xpl; 612 613 if (m_xpl == NULL) { 614 printf("no X_POLICY extension.\n"); 615 return; 616 } 617 if (withports) 618 d_xpl = ipsec_dump_policy_withports(m_xpl, "\n\t"); 619 else 620 d_xpl = ipsec_dump_policy((ipsec_policy_t)m_xpl, "\n\t"); 621 622 if (!d_xpl) 623 printf("\n\tPolicy:[%s]\n", ipsec_strerror()); 624 else { 625 /* dump SPD */ 626 printf("\n\t%s\n", d_xpl); 627 free(d_xpl); 628 } 629 } 630 631 /* lifetime */ 632 if (m_lftc) { 633 printf("\tcreated: %s ", 634 str_time((long)m_lftc->sadb_lifetime_addtime)); 635 printf("lastused: %s\n", 636 str_time((long)m_lftc->sadb_lifetime_usetime)); 637 } 638 if (m_lfth) { 639 printf("\tlifetime: %lu(s) ", 640 (u_long)m_lfth->sadb_lifetime_addtime); 641 printf("validtime: %lu(s)\n", 642 (u_long)m_lfth->sadb_lifetime_usetime); 643 } 644 645 #ifdef SADB_X_EXT_SEC_CTX 646 if (m_sec_ctx != NULL) { 647 printf("\tsecurity context doi: %u\n", 648 m_sec_ctx->sadb_x_ctx_doi); 649 printf("\tsecurity context algorithm: %u\n", 650 m_sec_ctx->sadb_x_ctx_alg); 651 printf("\tsecurity context length: %u\n", 652 m_sec_ctx->sadb_x_ctx_len); 653 printf("\tsecurity context: %s\n", 654 (char *)m_sec_ctx + sizeof(struct sadb_x_sec_ctx)); 655 } 656 #endif 657 658 printf("\tspid=%ld seq=%ld pid=%ld\n", 659 (u_long)m_xpl->sadb_x_policy_id, 660 (u_long)m->sadb_msg_seq, 661 (u_long)m->sadb_msg_pid); 662 663 /* XXX TEST */ 664 printf("\trefcnt=%u\n", m->sadb_msg_reserved); 665 666 return; 667 } 668 669 /* 670 * set "ipaddress" to buffer. 671 */ 672 static char * 673 str_ipaddr(sa) 674 struct sockaddr *sa; 675 { 676 static char buf[NI_MAXHOST]; 677 const int niflag = NI_NUMERICHOST; 678 679 if (sa == NULL) 680 return ""; 681 682 if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), buf, sizeof(buf), 683 NULL, 0, niflag) == 0) 684 return buf; 685 return NULL; 686 } 687 688 /* 689 * set "port" to buffer. 690 */ 691 static char * 692 str_ipport(sa) 693 struct sockaddr *sa; 694 { 695 static char buf[NI_MAXHOST]; 696 const int niflag = NI_NUMERICSERV; 697 698 if (sa == NULL) 699 return ""; 700 701 if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), NULL, 0, 702 buf, sizeof(buf), niflag) == 0) 703 return buf; 704 return NULL; 705 } 706 707 708 /* 709 * set "/prefix[port number]" to buffer. 710 */ 711 static char * 712 str_prefport(family, pref, port, ulp) 713 u_int family, pref, port, ulp; 714 { 715 static char buf[128]; 716 char prefbuf[128]; 717 char portbuf[128]; 718 int plen; 719 720 switch (family) { 721 case AF_INET: 722 plen = sizeof(struct in_addr) << 3; 723 break; 724 case AF_INET6: 725 plen = sizeof(struct in6_addr) << 3; 726 break; 727 default: 728 return "?"; 729 } 730 731 if (pref == plen) 732 prefbuf[0] = '\0'; 733 else 734 snprintf(prefbuf, sizeof(prefbuf), "/%u", pref); 735 736 switch (ulp) { 737 case IPPROTO_ICMP: 738 case IPPROTO_ICMPV6: 739 case IPPROTO_MH: 740 case IPPROTO_GRE: 741 memset(portbuf, 0, sizeof(portbuf)); 742 break; 743 default: 744 if (port == IPSEC_PORT_ANY) 745 strcpy(portbuf, "[any]"); 746 else 747 snprintf(portbuf, sizeof(portbuf), "[%u]", port); 748 break; 749 } 750 751 snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf); 752 753 return buf; 754 } 755 756 static void 757 str_upperspec(ulp, p1, p2) 758 u_int ulp, p1, p2; 759 { 760 struct protoent *ent; 761 762 ent = getprotobynumber((int)ulp); 763 if (ent) 764 printf("%s", ent->p_name); 765 else 766 printf("%u", ulp); 767 768 if (p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY) 769 return; 770 771 switch (ulp) { 772 case IPPROTO_ICMP: 773 case IPPROTO_ICMPV6: 774 case IPPROTO_MH: 775 printf(" %u,%u", p1, p2); 776 break; 777 case IPPROTO_GRE: 778 printf(" %u", (p1 << 16) + p2); 779 break; 780 } 781 } 782 783 /* 784 * set "Mon Day Time Year" to buffer 785 */ 786 static char * 787 str_time(t) 788 time_t t; 789 { 790 static char buf[128]; 791 792 if (t == 0) { 793 int i = 0; 794 for (;i < 20;) buf[i++] = ' '; 795 } else { 796 char *t0; 797 if ((t0 = ctime(&t)) == NULL) 798 memset(buf, '?', 20); 799 else 800 memcpy(buf, t0 + 4, 20); 801 } 802 803 buf[20] = '\0'; 804 805 return(buf); 806 } 807 808 static void 809 str_lifetime_byte(x, str) 810 struct sadb_lifetime *x; 811 char *str; 812 { 813 double y; 814 char *unit; 815 int w; 816 817 if (x == NULL) { 818 printf("\t%s: 0(bytes)", str); 819 return; 820 } 821 822 #if 0 823 if ((x->sadb_lifetime_bytes) / 1024 / 1024) { 824 y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024; 825 unit = "M"; 826 w = 1; 827 } else if ((x->sadb_lifetime_bytes) / 1024) { 828 y = (x->sadb_lifetime_bytes) * 1.0 / 1024; 829 unit = "K"; 830 w = 1; 831 } else { 832 y = (x->sadb_lifetime_bytes) * 1.0; 833 unit = ""; 834 w = 0; 835 } 836 #else 837 y = (x->sadb_lifetime_bytes) * 1.0; 838 unit = ""; 839 w = 0; 840 #endif 841 printf("\t%s: %.*f(%sbytes)", str, w, y, unit); 842 } 843