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