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