1 /* $USAGI: $ */ 2 3 /* 4 * Copyright (C)2004 USAGI/WIDE Project 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses>. 18 */ 19 /* 20 * based on ip.c, iproute.c 21 */ 22 /* 23 * Authors: 24 * Masahide NAKAMURA @USAGI 25 */ 26 27 #include <alloca.h> 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <string.h> 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <time.h> 34 #include <netdb.h> 35 #include <linux/netlink.h> 36 #include <linux/rtnetlink.h> 37 38 #include "utils.h" 39 #include "xfrm.h" 40 #include "ip_common.h" 41 42 #define STRBUF_SIZE (128) 43 44 struct xfrm_filter filter; 45 46 static void usage(void) __attribute__((noreturn)); 47 48 static void usage(void) 49 { 50 fprintf(stderr, 51 "Usage: ip xfrm XFRM-OBJECT { COMMAND | help }\n" 52 "where XFRM-OBJECT := state | policy | monitor\n"); 53 exit(-1); 54 } 55 56 /* This is based on utils.c(inet_addr_match) */ 57 int xfrm_addr_match(xfrm_address_t *x1, xfrm_address_t *x2, int bits) 58 { 59 __u32 *a1 = (__u32 *)x1; 60 __u32 *a2 = (__u32 *)x2; 61 int words = bits >> 0x05; 62 63 bits &= 0x1f; 64 65 if (words) 66 if (memcmp(a1, a2, words << 2)) 67 return -1; 68 69 if (bits) { 70 __u32 w1, w2; 71 __u32 mask; 72 73 w1 = a1[words]; 74 w2 = a2[words]; 75 76 mask = htonl((0xffffffff) << (0x20 - bits)); 77 78 if ((w1 ^ w2) & mask) 79 return 1; 80 } 81 82 return 0; 83 } 84 85 int xfrm_xfrmproto_is_ipsec(__u8 proto) 86 { 87 return (proto == IPPROTO_ESP || 88 proto == IPPROTO_AH || 89 proto == IPPROTO_COMP); 90 } 91 92 int xfrm_xfrmproto_is_ro(__u8 proto) 93 { 94 return (proto == IPPROTO_ROUTING || 95 proto == IPPROTO_DSTOPTS); 96 } 97 98 struct typeent { 99 const char *t_name; 100 int t_type; 101 }; 102 103 static const struct typeent xfrmproto_types[] = { 104 { "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP }, 105 { "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS }, 106 { "ipsec-any", IPSEC_PROTO_ANY }, 107 { NULL, -1 } 108 }; 109 110 int xfrm_xfrmproto_getbyname(char *name) 111 { 112 int i; 113 114 for (i = 0; ; i++) { 115 const struct typeent *t = &xfrmproto_types[i]; 116 117 if (!t->t_name || t->t_type == -1) 118 break; 119 120 if (strcmp(t->t_name, name) == 0) 121 return t->t_type; 122 } 123 124 return -1; 125 } 126 127 const char *strxf_xfrmproto(__u8 proto) 128 { 129 static char str[16]; 130 int i; 131 132 for (i = 0; ; i++) { 133 const struct typeent *t = &xfrmproto_types[i]; 134 135 if (!t->t_name || t->t_type == -1) 136 break; 137 138 if (t->t_type == proto) 139 return t->t_name; 140 } 141 142 sprintf(str, "%u", proto); 143 return str; 144 } 145 146 static const struct typeent algo_types[] = { 147 { "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH }, 148 { "comp", XFRMA_ALG_COMP }, { "aead", XFRMA_ALG_AEAD }, 149 { "auth-trunc", XFRMA_ALG_AUTH_TRUNC }, 150 { NULL, -1 } 151 }; 152 153 int xfrm_algotype_getbyname(char *name) 154 { 155 int i; 156 157 for (i = 0; ; i++) { 158 const struct typeent *t = &algo_types[i]; 159 160 if (!t->t_name || t->t_type == -1) 161 break; 162 163 if (strcmp(t->t_name, name) == 0) 164 return t->t_type; 165 } 166 167 return -1; 168 } 169 170 const char *strxf_algotype(int type) 171 { 172 static char str[32]; 173 int i; 174 175 for (i = 0; ; i++) { 176 const struct typeent *t = &algo_types[i]; 177 178 if (!t->t_name || t->t_type == -1) 179 break; 180 181 if (t->t_type == type) 182 return t->t_name; 183 } 184 185 sprintf(str, "%d", type); 186 return str; 187 } 188 189 const char *strxf_mask8(__u8 mask) 190 { 191 static char str[16]; 192 const int sn = sizeof(mask) * 8 - 1; 193 __u8 b; 194 int i = 0; 195 196 for (b = (1 << sn); b > 0; b >>= 1) 197 str[i++] = ((b & mask) ? '1' : '0'); 198 str[i] = '\0'; 199 200 return str; 201 } 202 203 const char *strxf_mask32(__u32 mask) 204 { 205 static char str[16]; 206 207 sprintf(str, "%.8x", mask); 208 209 return str; 210 } 211 212 const char *strxf_share(__u8 share) 213 { 214 static char str[32]; 215 216 switch (share) { 217 case XFRM_SHARE_ANY: 218 strcpy(str, "any"); 219 break; 220 case XFRM_SHARE_SESSION: 221 strcpy(str, "session"); 222 break; 223 case XFRM_SHARE_USER: 224 strcpy(str, "user"); 225 break; 226 case XFRM_SHARE_UNIQUE: 227 strcpy(str, "unique"); 228 break; 229 default: 230 sprintf(str, "%u", share); 231 break; 232 } 233 234 return str; 235 } 236 237 const char *strxf_proto(__u8 proto) 238 { 239 static char buf[32]; 240 struct protoent *pp; 241 const char *p; 242 243 pp = getprotobynumber(proto); 244 if (pp) 245 p = pp->p_name; 246 else { 247 sprintf(buf, "%u", proto); 248 p = buf; 249 } 250 251 return p; 252 } 253 254 const char *strxf_ptype(__u8 ptype) 255 { 256 static char str[16]; 257 258 switch (ptype) { 259 case XFRM_POLICY_TYPE_MAIN: 260 strcpy(str, "main"); 261 break; 262 case XFRM_POLICY_TYPE_SUB: 263 strcpy(str, "sub"); 264 break; 265 default: 266 sprintf(str, "%u", ptype); 267 break; 268 } 269 270 return str; 271 } 272 273 void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id, 274 __u8 mode, __u32 reqid, __u16 family, int force_spi, 275 FILE *fp, const char *prefix, const char *title) 276 { 277 if (title) 278 fputs(title, fp); 279 280 fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr), saddr)); 281 fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr), &id->daddr)); 282 fprintf(fp, "%s", _SL_); 283 284 if (prefix) 285 fputs(prefix, fp); 286 fprintf(fp, "\t"); 287 288 fprintf(fp, "proto %s ", strxf_xfrmproto(id->proto)); 289 290 if (show_stats > 0 || force_spi || id->spi) { 291 __u32 spi = ntohl(id->spi); 292 293 fprintf(fp, "spi 0x%08x", spi); 294 if (show_stats > 0) 295 fprintf(fp, "(%u)", spi); 296 fprintf(fp, " "); 297 } 298 299 fprintf(fp, "reqid %u", reqid); 300 if (show_stats > 0) 301 fprintf(fp, "(0x%08x)", reqid); 302 fprintf(fp, " "); 303 304 fprintf(fp, "mode "); 305 switch (mode) { 306 case XFRM_MODE_TRANSPORT: 307 fprintf(fp, "transport"); 308 break; 309 case XFRM_MODE_TUNNEL: 310 fprintf(fp, "tunnel"); 311 break; 312 case XFRM_MODE_ROUTEOPTIMIZATION: 313 fprintf(fp, "ro"); 314 break; 315 case XFRM_MODE_IN_TRIGGER: 316 fprintf(fp, "in_trigger"); 317 break; 318 case XFRM_MODE_BEET: 319 fprintf(fp, "beet"); 320 break; 321 default: 322 fprintf(fp, "%u", mode); 323 break; 324 } 325 fprintf(fp, "%s", _SL_); 326 } 327 328 static const char *strxf_limit(__u64 limit) 329 { 330 static char str[32]; 331 332 if (limit == XFRM_INF) 333 strcpy(str, "(INF)"); 334 else 335 sprintf(str, "%llu", (unsigned long long) limit); 336 337 return str; 338 } 339 340 void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix) 341 { 342 if (prefix) 343 fputs(prefix, fp); 344 fprintf(fp, "stats:%s", _SL_); 345 346 if (prefix) 347 fputs(prefix, fp); 348 fprintf(fp, " replay-window %u replay %u failed %u%s", 349 s->replay_window, s->replay, s->integrity_failed, _SL_); 350 } 351 352 static const char *strxf_time(__u64 time) 353 { 354 static char str[32]; 355 356 if (time == 0) 357 strcpy(str, "-"); 358 else { 359 time_t t; 360 struct tm *tp; 361 362 /* XXX: treat time in the same manner of kernel's 363 * net/xfrm/xfrm_{user,state}.c 364 */ 365 t = (long)time; 366 tp = localtime(&t); 367 368 strftime(str, sizeof(str), "%Y-%m-%d %T", tp); 369 } 370 371 return str; 372 } 373 374 void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg, 375 struct xfrm_lifetime_cur *cur, 376 FILE *fp, const char *prefix) 377 { 378 if (cfg) { 379 if (prefix) 380 fputs(prefix, fp); 381 fprintf(fp, "lifetime config:%s", _SL_); 382 383 if (prefix) 384 fputs(prefix, fp); 385 fprintf(fp, " limit: soft %s(bytes),", 386 strxf_limit(cfg->soft_byte_limit)); 387 fprintf(fp, " hard %s(bytes)%s", 388 strxf_limit(cfg->hard_byte_limit), _SL_); 389 390 if (prefix) 391 fputs(prefix, fp); 392 fprintf(fp, " limit: soft %s(packets),", 393 strxf_limit(cfg->soft_packet_limit)); 394 fprintf(fp, " hard %s(packets)%s", 395 strxf_limit(cfg->hard_packet_limit), _SL_); 396 397 if (prefix) 398 fputs(prefix, fp); 399 fprintf(fp, " expire add: soft %llu(sec), hard %llu(sec)%s", 400 (unsigned long long) cfg->soft_add_expires_seconds, 401 (unsigned long long) cfg->hard_add_expires_seconds, 402 _SL_); 403 404 if (prefix) 405 fputs(prefix, fp); 406 fprintf(fp, " expire use: soft %llu(sec), hard %llu(sec)%s", 407 (unsigned long long) cfg->soft_use_expires_seconds, 408 (unsigned long long) cfg->hard_use_expires_seconds, 409 _SL_); 410 } 411 if (cur) { 412 if (prefix) 413 fputs(prefix, fp); 414 fprintf(fp, "lifetime current:%s", _SL_); 415 416 if (prefix) 417 fputs(prefix, fp); 418 fprintf(fp, " %llu(bytes), %llu(packets)%s", 419 (unsigned long long) cur->bytes, 420 (unsigned long long) cur->packets, 421 _SL_); 422 423 if (prefix) 424 fputs(prefix, fp); 425 fprintf(fp, " add %s ", strxf_time(cur->add_time)); 426 fprintf(fp, "use %s%s", strxf_time(cur->use_time), _SL_); 427 } 428 } 429 430 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family, 431 FILE *fp, const char *prefix) 432 { 433 __u16 f; 434 435 f = sel->family; 436 if (f == AF_UNSPEC) 437 f = family; 438 if (f == AF_UNSPEC) 439 f = preferred_family; 440 441 if (prefix) 442 fputs(prefix, fp); 443 444 fprintf(fp, "src %s/%u ", 445 rt_addr_n2a(f, sizeof(sel->saddr), &sel->saddr), 446 sel->prefixlen_s); 447 448 fprintf(fp, "dst %s/%u ", 449 rt_addr_n2a(f, sizeof(sel->daddr), &sel->daddr), 450 sel->prefixlen_d); 451 452 if (sel->proto) 453 fprintf(fp, "proto %s ", strxf_proto(sel->proto)); 454 switch (sel->proto) { 455 case IPPROTO_TCP: 456 case IPPROTO_UDP: 457 case IPPROTO_SCTP: 458 case IPPROTO_DCCP: 459 default: /* XXX */ 460 if (sel->sport_mask) 461 fprintf(fp, "sport %u ", ntohs(sel->sport)); 462 if (sel->dport_mask) 463 fprintf(fp, "dport %u ", ntohs(sel->dport)); 464 break; 465 case IPPROTO_ICMP: 466 case IPPROTO_ICMPV6: 467 /* type/code is stored at sport/dport in selector */ 468 if (sel->sport_mask) 469 fprintf(fp, "type %u ", ntohs(sel->sport)); 470 if (sel->dport_mask) 471 fprintf(fp, "code %u ", ntohs(sel->dport)); 472 break; 473 case IPPROTO_GRE: 474 if (sel->sport_mask || sel->dport_mask) 475 fprintf(fp, "key %u ", 476 (((__u32)ntohs(sel->sport)) << 16) + 477 ntohs(sel->dport)); 478 break; 479 case IPPROTO_MH: 480 if (sel->sport_mask) 481 fprintf(fp, "type %u ", ntohs(sel->sport)); 482 if (sel->dport_mask) { 483 if (show_stats > 0) 484 fprintf(fp, "(dport) 0x%.4x ", sel->dport); 485 } 486 break; 487 } 488 489 if (sel->ifindex > 0) 490 fprintf(fp, "dev %s ", ll_index_to_name(sel->ifindex)); 491 492 if (show_stats > 0) 493 fprintf(fp, "uid %u", sel->user); 494 495 fprintf(fp, "%s", _SL_); 496 } 497 498 static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len, 499 FILE *fp, const char *prefix, int newline) 500 { 501 int keylen; 502 int i; 503 504 if (prefix) 505 fputs(prefix, fp); 506 507 fprintf(fp, "%s ", strxf_algotype(type)); 508 509 if (len < sizeof(*algo)) { 510 fprintf(fp, "(ERROR truncated)"); 511 goto fin; 512 } 513 len -= sizeof(*algo); 514 515 fprintf(fp, "%s ", algo->alg_name); 516 517 keylen = algo->alg_key_len / 8; 518 if (len < keylen) { 519 fprintf(fp, "(ERROR truncated)"); 520 goto fin; 521 } 522 523 if (keylen > 0) { 524 fprintf(fp, "0x"); 525 for (i = 0; i < keylen; i++) 526 fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]); 527 528 if (show_stats > 0) 529 fprintf(fp, " (%d bits)", algo->alg_key_len); 530 } 531 532 fin: 533 if (newline) 534 fprintf(fp, "%s", _SL_); 535 } 536 537 static inline void xfrm_algo_print(struct xfrm_algo *algo, int type, int len, 538 FILE *fp, const char *prefix) 539 { 540 return __xfrm_algo_print(algo, type, len, fp, prefix, 1); 541 } 542 543 static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len, 544 FILE *fp, const char *prefix) 545 { 546 struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8); 547 548 memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name)); 549 base_algo->alg_key_len = algo->alg_key_len; 550 memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8); 551 552 __xfrm_algo_print(base_algo, XFRMA_ALG_AEAD, len, fp, prefix, 0); 553 554 fprintf(fp, " %d", algo->alg_icv_len); 555 556 fprintf(fp, "%s", _SL_); 557 } 558 559 static void xfrm_auth_trunc_print(struct xfrm_algo_auth *algo, int len, 560 FILE *fp, const char *prefix) 561 { 562 struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8); 563 564 memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name)); 565 base_algo->alg_key_len = algo->alg_key_len; 566 memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8); 567 568 __xfrm_algo_print(base_algo, XFRMA_ALG_AUTH_TRUNC, len, fp, prefix, 0); 569 570 fprintf(fp, " %d", algo->alg_trunc_len); 571 572 fprintf(fp, "%s", _SL_); 573 } 574 575 static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len, 576 FILE *fp, const char *prefix) 577 { 578 int ntmpls = len / sizeof(struct xfrm_user_tmpl); 579 int i; 580 581 if (ntmpls <= 0) { 582 if (prefix) 583 fputs(prefix, fp); 584 fprintf(fp, "(ERROR \"tmpl\" truncated)"); 585 fprintf(fp, "%s", _SL_); 586 return; 587 } 588 589 for (i = 0; i < ntmpls; i++) { 590 struct xfrm_user_tmpl *tmpl = &tmpls[i]; 591 592 if (prefix) 593 fputs(prefix, fp); 594 595 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode, 596 tmpl->reqid, tmpl->family, 0, fp, prefix, "tmpl "); 597 598 if (show_stats > 0 || tmpl->optional) { 599 if (prefix) 600 fputs(prefix, fp); 601 fprintf(fp, "\t"); 602 switch (tmpl->optional) { 603 case 0: 604 if (show_stats > 0) 605 fprintf(fp, "level required "); 606 break; 607 case 1: 608 fprintf(fp, "level use "); 609 break; 610 default: 611 fprintf(fp, "level %u ", tmpl->optional); 612 break; 613 } 614 615 if (show_stats > 0) 616 fprintf(fp, "share %s ", strxf_share(tmpl->share)); 617 618 fprintf(fp, "%s", _SL_); 619 } 620 621 if (show_stats > 0) { 622 if (prefix) 623 fputs(prefix, fp); 624 fprintf(fp, "\t"); 625 fprintf(fp, "%s-mask %s ", 626 strxf_algotype(XFRMA_ALG_CRYPT), 627 strxf_mask32(tmpl->ealgos)); 628 fprintf(fp, "%s-mask %s ", 629 strxf_algotype(XFRMA_ALG_AUTH), 630 strxf_mask32(tmpl->aalgos)); 631 fprintf(fp, "%s-mask %s", 632 strxf_algotype(XFRMA_ALG_COMP), 633 strxf_mask32(tmpl->calgos)); 634 635 fprintf(fp, "%s", _SL_); 636 } 637 } 638 } 639 640 int xfrm_parse_mark(struct xfrm_mark *mark, int *argcp, char ***argvp) 641 { 642 int argc = *argcp; 643 char **argv = *argvp; 644 645 NEXT_ARG(); 646 if (get_u32(&mark->v, *argv, 0)) { 647 invarg("MARK value is invalid\n", *argv); 648 } 649 if (argc > 1) 650 NEXT_ARG(); 651 else { /* last entry on parse line */ 652 mark->m = 0xffffffff; 653 goto done; 654 } 655 656 if (strcmp(*argv, "mask") == 0) { 657 NEXT_ARG(); 658 if (get_u32(&mark->m, *argv, 0)) { 659 invarg("MASK value is invalid\n", *argv); 660 } 661 } else { 662 mark->m = 0xffffffff; 663 PREV_ARG(); 664 } 665 666 done: 667 *argcp = argc; 668 *argvp = argv; 669 670 return 0; 671 } 672 673 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family, 674 FILE *fp, const char *prefix) 675 { 676 if (tb[XFRMA_MARK]) { 677 struct rtattr *rta = tb[XFRMA_MARK]; 678 struct xfrm_mark *m = RTA_DATA(rta); 679 680 fprintf(fp, "\tmark %#x/%#x", m->v, m->m); 681 fprintf(fp, "%s", _SL_); 682 } 683 684 if (tb[XFRMA_ALG_AUTH] && !tb[XFRMA_ALG_AUTH_TRUNC]) { 685 struct rtattr *rta = tb[XFRMA_ALG_AUTH]; 686 687 xfrm_algo_print(RTA_DATA(rta), 688 XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix); 689 } 690 691 if (tb[XFRMA_ALG_AUTH_TRUNC]) { 692 struct rtattr *rta = tb[XFRMA_ALG_AUTH_TRUNC]; 693 694 xfrm_auth_trunc_print(RTA_DATA(rta), 695 RTA_PAYLOAD(rta), fp, prefix); 696 } 697 698 if (tb[XFRMA_ALG_AEAD]) { 699 struct rtattr *rta = tb[XFRMA_ALG_AEAD]; 700 701 xfrm_aead_print(RTA_DATA(rta), 702 RTA_PAYLOAD(rta), fp, prefix); 703 } 704 705 if (tb[XFRMA_ALG_CRYPT]) { 706 struct rtattr *rta = tb[XFRMA_ALG_CRYPT]; 707 708 xfrm_algo_print(RTA_DATA(rta), 709 XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix); 710 } 711 712 if (tb[XFRMA_ALG_COMP]) { 713 struct rtattr *rta = tb[XFRMA_ALG_COMP]; 714 715 xfrm_algo_print(RTA_DATA(rta), 716 XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix); 717 } 718 719 if (tb[XFRMA_ENCAP]) { 720 struct xfrm_encap_tmpl *e; 721 722 if (prefix) 723 fputs(prefix, fp); 724 fprintf(fp, "encap "); 725 726 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) { 727 fprintf(fp, "(ERROR truncated)"); 728 fprintf(fp, "%s", _SL_); 729 return; 730 } 731 e = RTA_DATA(tb[XFRMA_ENCAP]); 732 733 fprintf(fp, "type "); 734 switch (e->encap_type) { 735 case 1: 736 fprintf(fp, "espinudp-nonike "); 737 break; 738 case 2: 739 fprintf(fp, "espinudp "); 740 break; 741 default: 742 fprintf(fp, "%u ", e->encap_type); 743 break; 744 } 745 fprintf(fp, "sport %u ", ntohs(e->encap_sport)); 746 fprintf(fp, "dport %u ", ntohs(e->encap_dport)); 747 748 fprintf(fp, "addr %s", 749 rt_addr_n2a(family, sizeof(e->encap_oa), &e->encap_oa)); 750 fprintf(fp, "%s", _SL_); 751 } 752 753 if (tb[XFRMA_TMPL]) { 754 struct rtattr *rta = tb[XFRMA_TMPL]; 755 756 xfrm_tmpl_print(RTA_DATA(rta), 757 RTA_PAYLOAD(rta), fp, prefix); 758 } 759 760 if (tb[XFRMA_COADDR]) { 761 const xfrm_address_t *coa; 762 763 if (prefix) 764 fputs(prefix, fp); 765 fprintf(fp, "coa "); 766 767 coa = RTA_DATA(tb[XFRMA_COADDR]); 768 if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) { 769 fprintf(fp, "(ERROR truncated)"); 770 fprintf(fp, "%s", _SL_); 771 return; 772 } 773 774 fprintf(fp, "%s", 775 rt_addr_n2a(family, sizeof(*coa), coa)); 776 fprintf(fp, "%s", _SL_); 777 } 778 779 if (tb[XFRMA_LASTUSED]) { 780 __u64 lastused; 781 782 if (prefix) 783 fputs(prefix, fp); 784 fprintf(fp, "lastused "); 785 786 if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) { 787 fprintf(fp, "(ERROR truncated)"); 788 fprintf(fp, "%s", _SL_); 789 return; 790 } 791 792 lastused = rta_getattr_u64(tb[XFRMA_LASTUSED]); 793 794 fprintf(fp, "%s", strxf_time(lastused)); 795 fprintf(fp, "%s", _SL_); 796 } 797 798 if (tb[XFRMA_REPLAY_VAL]) { 799 struct xfrm_replay_state *replay; 800 801 if (prefix) 802 fputs(prefix, fp); 803 fprintf(fp, "anti-replay context: "); 804 805 if (RTA_PAYLOAD(tb[XFRMA_REPLAY_VAL]) < sizeof(*replay)) { 806 fprintf(fp, "(ERROR truncated)"); 807 fprintf(fp, "%s", _SL_); 808 return; 809 } 810 811 replay = RTA_DATA(tb[XFRMA_REPLAY_VAL]); 812 fprintf(fp, "seq 0x%x, oseq 0x%x, bitmap 0x%08x", 813 replay->seq, replay->oseq, replay->bitmap); 814 fprintf(fp, "%s", _SL_); 815 } 816 817 if (tb[XFRMA_REPLAY_ESN_VAL]) { 818 struct xfrm_replay_state_esn *replay; 819 unsigned int i, j; 820 821 if (prefix) 822 fputs(prefix, fp); 823 fprintf(fp, "anti-replay esn context:"); 824 825 if (RTA_PAYLOAD(tb[XFRMA_REPLAY_ESN_VAL]) < sizeof(*replay)) { 826 fprintf(fp, "(ERROR truncated)"); 827 fprintf(fp, "%s", _SL_); 828 return; 829 } 830 fprintf(fp, "%s", _SL_); 831 832 replay = RTA_DATA(tb[XFRMA_REPLAY_ESN_VAL]); 833 if (prefix) 834 fputs(prefix, fp); 835 fprintf(fp, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x", 836 replay->seq_hi, replay->seq, replay->oseq_hi, 837 replay->oseq); 838 fprintf(fp, "%s", _SL_); 839 if (prefix) 840 fputs(prefix, fp); 841 fprintf(fp, " replay_window %u, bitmap-length %u", 842 replay->replay_window, replay->bmp_len); 843 for (i = replay->bmp_len, j = 0; i; i--) { 844 if (j++ % 8 == 0) { 845 fprintf(fp, "%s", _SL_); 846 if (prefix) 847 fputs(prefix, fp); 848 fprintf(fp, " "); 849 } 850 fprintf(fp, "%08x ", replay->bmp[i - 1]); 851 } 852 fprintf(fp, "%s", _SL_); 853 } 854 if (tb[XFRMA_OFFLOAD_DEV]) { 855 struct xfrm_user_offload *xuo; 856 857 if (prefix) 858 fputs(prefix, fp); 859 fprintf(fp, "crypto offload parameters: "); 860 861 if (RTA_PAYLOAD(tb[XFRMA_OFFLOAD_DEV]) < sizeof(*xuo)) { 862 fprintf(fp, "(ERROR truncated)"); 863 fprintf(fp, "%s", _SL_); 864 return; 865 } 866 867 xuo = (struct xfrm_user_offload *) 868 RTA_DATA(tb[XFRMA_OFFLOAD_DEV]); 869 fprintf(fp, "dev %s dir %s", ll_index_to_name(xuo->ifindex), 870 (xuo->flags & XFRM_OFFLOAD_INBOUND) ? "in" : "out"); 871 fprintf(fp, "%s", _SL_); 872 } 873 } 874 875 static int xfrm_selector_iszero(struct xfrm_selector *s) 876 { 877 struct xfrm_selector s0 = {}; 878 879 return (memcmp(&s0, s, sizeof(s0)) == 0); 880 } 881 882 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo, 883 struct rtattr *tb[], FILE *fp, const char *prefix, 884 const char *title) 885 { 886 char buf[STRBUF_SIZE] = {}; 887 int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto); 888 889 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode, 890 xsinfo->reqid, xsinfo->family, force_spi, fp, 891 prefix, title); 892 893 if (prefix) 894 strlcat(buf, prefix, sizeof(buf)); 895 strlcat(buf, "\t", sizeof(buf)); 896 897 fputs(buf, fp); 898 fprintf(fp, "replay-window %u ", xsinfo->replay_window); 899 if (show_stats > 0) 900 fprintf(fp, "seq 0x%08u ", xsinfo->seq); 901 if (show_stats > 0 || xsinfo->flags) { 902 __u8 flags = xsinfo->flags; 903 904 fprintf(fp, "flag "); 905 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn"); 906 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp"); 907 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc"); 908 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv"); 909 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ICMP, "icmp"); 910 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_AF_UNSPEC, "af-unspec"); 911 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ALIGN4, "align4"); 912 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ESN, "esn"); 913 if (flags) 914 fprintf(fp, "%x", flags); 915 } 916 if (show_stats > 0 && tb[XFRMA_SA_EXTRA_FLAGS]) { 917 __u32 extra_flags = rta_getattr_u32(tb[XFRMA_SA_EXTRA_FLAGS]); 918 919 fprintf(fp, "extra_flag "); 920 XFRM_FLAG_PRINT(fp, extra_flags, 921 XFRM_SA_XFLAG_DONT_ENCAP_DSCP, 922 "dont-encap-dscp"); 923 if (extra_flags) 924 fprintf(fp, "%x", extra_flags); 925 } 926 if (show_stats > 0) 927 fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags)); 928 fprintf(fp, "%s", _SL_); 929 930 xfrm_xfrma_print(tb, xsinfo->family, fp, buf); 931 932 if (!xfrm_selector_iszero(&xsinfo->sel)) { 933 char sbuf[STRBUF_SIZE]; 934 935 memcpy(sbuf, buf, sizeof(sbuf)); 936 strlcat(sbuf, "sel ", sizeof(sbuf)); 937 938 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf); 939 } 940 941 if (show_stats > 0) { 942 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf); 943 xfrm_stats_print(&xsinfo->stats, fp, buf); 944 } 945 946 if (tb[XFRMA_SEC_CTX]) { 947 struct xfrm_user_sec_ctx *sctx; 948 949 fprintf(fp, "\tsecurity context "); 950 951 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx)) 952 fprintf(fp, "(ERROR truncated)"); 953 954 sctx = RTA_DATA(tb[XFRMA_SEC_CTX]); 955 956 fprintf(fp, "%s %s", (char *)(sctx + 1), _SL_); 957 } 958 959 } 960 961 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo, 962 struct rtattr *tb[], FILE *fp, const char *prefix, 963 const char *title) 964 { 965 char buf[STRBUF_SIZE] = {}; 966 967 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title); 968 969 if (tb[XFRMA_SEC_CTX]) { 970 struct xfrm_user_sec_ctx *sctx; 971 972 fprintf(fp, "\tsecurity context "); 973 974 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx)) 975 fprintf(fp, "(ERROR truncated)"); 976 977 sctx = RTA_DATA(tb[XFRMA_SEC_CTX]); 978 979 fprintf(fp, "%s ", (char *)(sctx + 1)); 980 fprintf(fp, "%s", _SL_); 981 } 982 983 if (prefix) 984 strlcat(buf, prefix, sizeof(buf)); 985 strlcat(buf, "\t", sizeof(buf)); 986 987 fputs(buf, fp); 988 if (xpinfo->dir >= XFRM_POLICY_MAX) { 989 xpinfo->dir -= XFRM_POLICY_MAX; 990 fprintf(fp, "socket "); 991 } else 992 fprintf(fp, "dir "); 993 994 switch (xpinfo->dir) { 995 case XFRM_POLICY_IN: 996 fprintf(fp, "in"); 997 break; 998 case XFRM_POLICY_OUT: 999 fprintf(fp, "out"); 1000 break; 1001 case XFRM_POLICY_FWD: 1002 fprintf(fp, "fwd"); 1003 break; 1004 default: 1005 fprintf(fp, "%u", xpinfo->dir); 1006 break; 1007 } 1008 fprintf(fp, " "); 1009 1010 switch (xpinfo->action) { 1011 case XFRM_POLICY_ALLOW: 1012 if (show_stats > 0) 1013 fprintf(fp, "action allow "); 1014 break; 1015 case XFRM_POLICY_BLOCK: 1016 fprintf(fp, "action block "); 1017 break; 1018 default: 1019 fprintf(fp, "action %u ", xpinfo->action); 1020 break; 1021 } 1022 1023 if (show_stats) 1024 fprintf(fp, "index %u ", xpinfo->index); 1025 fprintf(fp, "priority %u ", xpinfo->priority); 1026 1027 if (tb[XFRMA_POLICY_TYPE]) { 1028 struct xfrm_userpolicy_type *upt; 1029 1030 fprintf(fp, "ptype "); 1031 1032 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) 1033 fprintf(fp, "(ERROR truncated)"); 1034 1035 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]); 1036 fprintf(fp, "%s ", strxf_ptype(upt->type)); 1037 } 1038 1039 if (show_stats > 0) 1040 fprintf(fp, "share %s ", strxf_share(xpinfo->share)); 1041 1042 if (show_stats > 0 || xpinfo->flags) { 1043 __u8 flags = xpinfo->flags; 1044 1045 fprintf(fp, "flag "); 1046 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok"); 1047 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_ICMP, "icmp"); 1048 if (flags) 1049 fprintf(fp, "%x", flags); 1050 } 1051 if (show_stats > 0) 1052 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags)); 1053 fprintf(fp, "%s", _SL_); 1054 1055 if (show_stats > 0) 1056 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf); 1057 1058 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf); 1059 } 1060 1061 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family, 1062 int loose, int *argcp, char ***argvp) 1063 { 1064 int argc = *argcp; 1065 char **argv = *argvp; 1066 inet_prefix dst = {}; 1067 inet_prefix src = {}; 1068 1069 while (1) { 1070 if (strcmp(*argv, "src") == 0) { 1071 NEXT_ARG(); 1072 1073 get_prefix(&src, *argv, preferred_family); 1074 if (src.family == AF_UNSPEC) 1075 invarg("value after \"src\" has an unrecognized address family", *argv); 1076 if (family) 1077 *family = src.family; 1078 1079 memcpy(saddr, &src.data, sizeof(*saddr)); 1080 1081 filter.id_src_mask = src.bitlen; 1082 1083 } else if (strcmp(*argv, "dst") == 0) { 1084 NEXT_ARG(); 1085 1086 get_prefix(&dst, *argv, preferred_family); 1087 if (dst.family == AF_UNSPEC) 1088 invarg("value after \"dst\" has an unrecognized address family", *argv); 1089 if (family) 1090 *family = dst.family; 1091 1092 memcpy(&id->daddr, &dst.data, sizeof(id->daddr)); 1093 1094 filter.id_dst_mask = dst.bitlen; 1095 1096 } else if (strcmp(*argv, "proto") == 0) { 1097 int ret; 1098 1099 NEXT_ARG(); 1100 1101 ret = xfrm_xfrmproto_getbyname(*argv); 1102 if (ret < 0) 1103 invarg("XFRM-PROTO value is invalid", *argv); 1104 1105 id->proto = (__u8)ret; 1106 1107 filter.id_proto_mask = XFRM_FILTER_MASK_FULL; 1108 1109 } else if (strcmp(*argv, "spi") == 0) { 1110 NEXT_ARG(); 1111 if (get_be32(&id->spi, *argv, 0)) 1112 invarg("SPI value is invalid", *argv); 1113 1114 filter.id_spi_mask = XFRM_FILTER_MASK_FULL; 1115 1116 } else { 1117 PREV_ARG(); /* back track */ 1118 break; 1119 } 1120 1121 if (!NEXT_ARG_OK()) 1122 break; 1123 NEXT_ARG(); 1124 } 1125 1126 if (src.family && dst.family && (src.family != dst.family)) 1127 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv); 1128 1129 if (id->spi && id->proto) { 1130 if (xfrm_xfrmproto_is_ro(id->proto)) { 1131 fprintf(stderr, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n", 1132 strxf_xfrmproto(id->proto)); 1133 exit(1); 1134 } else if (id->proto == IPPROTO_COMP && ntohl(id->spi) >= 0x10000) { 1135 fprintf(stderr, "SPI value is too large with XFRM-PROTO value \"%s\"\n", 1136 strxf_xfrmproto(id->proto)); 1137 exit(1); 1138 } 1139 } 1140 1141 if (loose == 0 && id->proto == 0) 1142 missarg("XFRM-PROTO"); 1143 if (argc == *argcp) 1144 missarg("ID"); 1145 1146 *argcp = argc; 1147 *argvp = argv; 1148 1149 return 0; 1150 } 1151 1152 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp) 1153 { 1154 int argc = *argcp; 1155 char **argv = *argvp; 1156 1157 if (matches(*argv, "transport") == 0) 1158 *mode = XFRM_MODE_TRANSPORT; 1159 else if (matches(*argv, "tunnel") == 0) 1160 *mode = XFRM_MODE_TUNNEL; 1161 else if (matches(*argv, "ro") == 0) 1162 *mode = XFRM_MODE_ROUTEOPTIMIZATION; 1163 else if (matches(*argv, "in_trigger") == 0) 1164 *mode = XFRM_MODE_IN_TRIGGER; 1165 else if (matches(*argv, "beet") == 0) 1166 *mode = XFRM_MODE_BEET; 1167 else 1168 invarg("MODE value is invalid", *argv); 1169 1170 *argcp = argc; 1171 *argvp = argv; 1172 1173 return 0; 1174 } 1175 1176 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp) 1177 { 1178 int argc = *argcp; 1179 char **argv = *argvp; 1180 1181 if (strcmp(*argv, "espinudp-nonike") == 0) 1182 *type = 1; 1183 else if (strcmp(*argv, "espinudp") == 0) 1184 *type = 2; 1185 else 1186 invarg("ENCAP-TYPE value is invalid", *argv); 1187 1188 *argcp = argc; 1189 *argvp = argv; 1190 1191 return 0; 1192 } 1193 1194 /* NOTE: reqid is used by host-byte order */ 1195 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp) 1196 { 1197 int argc = *argcp; 1198 char **argv = *argvp; 1199 1200 if (get_u32(reqid, *argv, 0)) 1201 invarg("REQID value is invalid", *argv); 1202 1203 *argcp = argc; 1204 *argvp = argv; 1205 1206 return 0; 1207 } 1208 1209 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel, 1210 int *argcp, char ***argvp) 1211 { 1212 int argc = *argcp; 1213 char **argv = *argvp; 1214 char *sportp = NULL; 1215 char *dportp = NULL; 1216 char *typep = NULL; 1217 char *codep = NULL; 1218 char *grekey = NULL; 1219 1220 while (1) { 1221 if (strcmp(*argv, "proto") == 0) { 1222 __u8 upspec; 1223 1224 NEXT_ARG(); 1225 1226 if (strcmp(*argv, "any") == 0) 1227 upspec = 0; 1228 else { 1229 struct protoent *pp; 1230 1231 pp = getprotobyname(*argv); 1232 if (pp) 1233 upspec = pp->p_proto; 1234 else { 1235 if (get_u8(&upspec, *argv, 0)) 1236 invarg("PROTO value is invalid", *argv); 1237 } 1238 } 1239 sel->proto = upspec; 1240 1241 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL; 1242 1243 } else if (strcmp(*argv, "sport") == 0) { 1244 sportp = *argv; 1245 1246 NEXT_ARG(); 1247 1248 if (get_be16(&sel->sport, *argv, 0)) 1249 invarg("value after \"sport\" is invalid", *argv); 1250 if (sel->sport) 1251 sel->sport_mask = ~((__u16)0); 1252 1253 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL; 1254 1255 } else if (strcmp(*argv, "dport") == 0) { 1256 dportp = *argv; 1257 1258 NEXT_ARG(); 1259 1260 if (get_be16(&sel->dport, *argv, 0)) 1261 invarg("value after \"dport\" is invalid", *argv); 1262 if (sel->dport) 1263 sel->dport_mask = ~((__u16)0); 1264 1265 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL; 1266 1267 } else if (strcmp(*argv, "type") == 0) { 1268 typep = *argv; 1269 1270 NEXT_ARG(); 1271 1272 if (get_u16(&sel->sport, *argv, 0) || 1273 (sel->sport & ~((__u16)0xff))) 1274 invarg("value after \"type\" is invalid", *argv); 1275 sel->sport = htons(sel->sport); 1276 sel->sport_mask = ~((__u16)0); 1277 1278 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL; 1279 1280 1281 } else if (strcmp(*argv, "code") == 0) { 1282 codep = *argv; 1283 1284 NEXT_ARG(); 1285 1286 if (get_u16(&sel->dport, *argv, 0) || 1287 (sel->dport & ~((__u16)0xff))) 1288 invarg("value after \"code\" is invalid", *argv); 1289 sel->dport = htons(sel->dport); 1290 sel->dport_mask = ~((__u16)0); 1291 1292 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL; 1293 1294 } else if (strcmp(*argv, "key") == 0) { 1295 unsigned int uval; 1296 1297 grekey = *argv; 1298 1299 NEXT_ARG(); 1300 1301 if (strchr(*argv, '.')) 1302 uval = htonl(get_addr32(*argv)); 1303 else { 1304 if (get_unsigned(&uval, *argv, 0) < 0) { 1305 fprintf(stderr, "value after \"key\" is invalid\n"); 1306 exit(-1); 1307 } 1308 } 1309 1310 sel->sport = htons(uval >> 16); 1311 sel->dport = htons(uval & 0xffff); 1312 sel->sport_mask = ~((__u16)0); 1313 sel->dport_mask = ~((__u16)0); 1314 1315 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL; 1316 1317 } else { 1318 PREV_ARG(); /* back track */ 1319 break; 1320 } 1321 1322 if (!NEXT_ARG_OK()) 1323 break; 1324 NEXT_ARG(); 1325 } 1326 if (argc == *argcp) 1327 missarg("UPSPEC"); 1328 if (sportp || dportp) { 1329 switch (sel->proto) { 1330 case IPPROTO_TCP: 1331 case IPPROTO_UDP: 1332 case IPPROTO_SCTP: 1333 case IPPROTO_DCCP: 1334 case IPPROTO_IP: /* to allow shared SA for different protocols */ 1335 break; 1336 default: 1337 fprintf(stderr, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto)); 1338 exit(1); 1339 } 1340 } 1341 if (typep || codep) { 1342 switch (sel->proto) { 1343 case IPPROTO_ICMP: 1344 case IPPROTO_ICMPV6: 1345 case IPPROTO_MH: 1346 break; 1347 default: 1348 fprintf(stderr, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto)); 1349 exit(1); 1350 } 1351 } 1352 if (grekey) { 1353 switch (sel->proto) { 1354 case IPPROTO_GRE: 1355 break; 1356 default: 1357 fprintf(stderr, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto)); 1358 exit(1); 1359 } 1360 } 1361 1362 *argcp = argc; 1363 *argvp = argv; 1364 1365 return 0; 1366 } 1367 1368 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp) 1369 { 1370 int argc = *argcp; 1371 char **argv = *argvp; 1372 inet_prefix dst = {}; 1373 inet_prefix src = {}; 1374 char *upspecp = NULL; 1375 1376 while (1) { 1377 if (strcmp(*argv, "src") == 0) { 1378 NEXT_ARG(); 1379 1380 get_prefix(&src, *argv, preferred_family); 1381 if (src.family == AF_UNSPEC) 1382 invarg("value after \"src\" has an unrecognized address family", *argv); 1383 sel->family = src.family; 1384 1385 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr)); 1386 sel->prefixlen_s = src.bitlen; 1387 1388 filter.sel_src_mask = src.bitlen; 1389 1390 } else if (strcmp(*argv, "dst") == 0) { 1391 NEXT_ARG(); 1392 1393 get_prefix(&dst, *argv, preferred_family); 1394 if (dst.family == AF_UNSPEC) 1395 invarg("value after \"dst\" has an unrecognized address family", *argv); 1396 sel->family = dst.family; 1397 1398 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr)); 1399 sel->prefixlen_d = dst.bitlen; 1400 1401 filter.sel_dst_mask = dst.bitlen; 1402 1403 } else if (strcmp(*argv, "dev") == 0) { 1404 int ifindex; 1405 1406 NEXT_ARG(); 1407 1408 if (strcmp(*argv, "none") == 0) 1409 ifindex = 0; 1410 else { 1411 ifindex = ll_name_to_index(*argv); 1412 if (ifindex <= 0) 1413 invarg("DEV value is invalid", *argv); 1414 } 1415 sel->ifindex = ifindex; 1416 1417 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL; 1418 1419 } else { 1420 if (upspecp) { 1421 PREV_ARG(); /* back track */ 1422 break; 1423 } else { 1424 upspecp = *argv; 1425 xfrm_selector_upspec_parse(sel, &argc, &argv); 1426 } 1427 } 1428 1429 if (!NEXT_ARG_OK()) 1430 break; 1431 1432 NEXT_ARG(); 1433 } 1434 1435 if (src.family && dst.family && (src.family != dst.family)) 1436 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv); 1437 1438 if (argc == *argcp) 1439 missarg("SELECTOR"); 1440 1441 *argcp = argc; 1442 *argvp = argv; 1443 1444 return 0; 1445 } 1446 1447 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft, 1448 int *argcp, char ***argvp) 1449 { 1450 int argc = *argcp; 1451 char **argv = *argvp; 1452 int ret; 1453 1454 if (strcmp(*argv, "time-soft") == 0) { 1455 NEXT_ARG(); 1456 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0); 1457 if (ret) 1458 invarg("value after \"time-soft\" is invalid", *argv); 1459 } else if (strcmp(*argv, "time-hard") == 0) { 1460 NEXT_ARG(); 1461 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0); 1462 if (ret) 1463 invarg("value after \"time-hard\" is invalid", *argv); 1464 } else if (strcmp(*argv, "time-use-soft") == 0) { 1465 NEXT_ARG(); 1466 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0); 1467 if (ret) 1468 invarg("value after \"time-use-soft\" is invalid", *argv); 1469 } else if (strcmp(*argv, "time-use-hard") == 0) { 1470 NEXT_ARG(); 1471 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0); 1472 if (ret) 1473 invarg("value after \"time-use-hard\" is invalid", *argv); 1474 } else if (strcmp(*argv, "byte-soft") == 0) { 1475 NEXT_ARG(); 1476 ret = get_u64(&lft->soft_byte_limit, *argv, 0); 1477 if (ret) 1478 invarg("value after \"byte-soft\" is invalid", *argv); 1479 } else if (strcmp(*argv, "byte-hard") == 0) { 1480 NEXT_ARG(); 1481 ret = get_u64(&lft->hard_byte_limit, *argv, 0); 1482 if (ret) 1483 invarg("value after \"byte-hard\" is invalid", *argv); 1484 } else if (strcmp(*argv, "packet-soft") == 0) { 1485 NEXT_ARG(); 1486 ret = get_u64(&lft->soft_packet_limit, *argv, 0); 1487 if (ret) 1488 invarg("value after \"packet-soft\" is invalid", *argv); 1489 } else if (strcmp(*argv, "packet-hard") == 0) { 1490 NEXT_ARG(); 1491 ret = get_u64(&lft->hard_packet_limit, *argv, 0); 1492 if (ret) 1493 invarg("value after \"packet-hard\" is invalid", *argv); 1494 } else 1495 invarg("LIMIT value is invalid", *argv); 1496 1497 *argcp = argc; 1498 *argvp = argv; 1499 1500 return 0; 1501 } 1502 1503 int do_xfrm(int argc, char **argv) 1504 { 1505 memset(&filter, 0, sizeof(filter)); 1506 1507 if (argc < 1) 1508 usage(); 1509 1510 if (matches(*argv, "state") == 0 || 1511 matches(*argv, "sa") == 0) 1512 return do_xfrm_state(argc-1, argv+1); 1513 else if (matches(*argv, "policy") == 0) 1514 return do_xfrm_policy(argc-1, argv+1); 1515 else if (matches(*argv, "monitor") == 0) 1516 return do_xfrm_monitor(argc-1, argv+1); 1517 else if (matches(*argv, "help") == 0) { 1518 usage(); 1519 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv); 1520 exit(-1); 1521 } 1522 usage(); 1523 } 1524