linux/net/xfrm/xfrm_user.c
<<
>>
Prefs
   1/* xfrm_user.c: User interface to configure xfrm engine.
   2 *
   3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
   4 *
   5 * Changes:
   6 *      Mitsuru KANDA @USAGI
   7 *      Kazunori MIYAZAWA @USAGI
   8 *      Kunihiro Ishiguro <kunihiro@ipinfusion.com>
   9 *              IPv6 support
  10 *
  11 */
  12
  13#include <linux/crypto.h>
  14#include <linux/module.h>
  15#include <linux/kernel.h>
  16#include <linux/types.h>
  17#include <linux/slab.h>
  18#include <linux/socket.h>
  19#include <linux/string.h>
  20#include <linux/net.h>
  21#include <linux/skbuff.h>
  22#include <linux/pfkeyv2.h>
  23#include <linux/ipsec.h>
  24#include <linux/init.h>
  25#include <linux/security.h>
  26#include <net/sock.h>
  27#include <net/xfrm.h>
  28#include <net/netlink.h>
  29#include <net/ah.h>
  30#include <linux/uaccess.h>
  31#if IS_ENABLED(CONFIG_IPV6)
  32#include <linux/in6.h>
  33#endif
  34#include <asm/unaligned.h>
  35
  36static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
  37{
  38        struct nlattr *rt = attrs[type];
  39        struct xfrm_algo *algp;
  40
  41        if (!rt)
  42                return 0;
  43
  44        algp = nla_data(rt);
  45        if (nla_len(rt) < (int)xfrm_alg_len(algp))
  46                return -EINVAL;
  47
  48        switch (type) {
  49        case XFRMA_ALG_AUTH:
  50        case XFRMA_ALG_CRYPT:
  51        case XFRMA_ALG_COMP:
  52                break;
  53
  54        default:
  55                return -EINVAL;
  56        }
  57
  58        algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  59        return 0;
  60}
  61
  62static int verify_auth_trunc(struct nlattr **attrs)
  63{
  64        struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
  65        struct xfrm_algo_auth *algp;
  66
  67        if (!rt)
  68                return 0;
  69
  70        algp = nla_data(rt);
  71        if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
  72                return -EINVAL;
  73
  74        algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  75        return 0;
  76}
  77
  78static int verify_aead(struct nlattr **attrs)
  79{
  80        struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
  81        struct xfrm_algo_aead *algp;
  82
  83        if (!rt)
  84                return 0;
  85
  86        algp = nla_data(rt);
  87        if (nla_len(rt) < (int)aead_len(algp))
  88                return -EINVAL;
  89
  90        algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  91        return 0;
  92}
  93
  94static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
  95                           xfrm_address_t **addrp)
  96{
  97        struct nlattr *rt = attrs[type];
  98
  99        if (rt && addrp)
 100                *addrp = nla_data(rt);
 101}
 102
 103static inline int verify_sec_ctx_len(struct nlattr **attrs)
 104{
 105        struct nlattr *rt = attrs[XFRMA_SEC_CTX];
 106        struct xfrm_user_sec_ctx *uctx;
 107
 108        if (!rt)
 109                return 0;
 110
 111        uctx = nla_data(rt);
 112        if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
 113                return -EINVAL;
 114
 115        return 0;
 116}
 117
 118static inline int verify_replay(struct xfrm_usersa_info *p,
 119                                struct nlattr **attrs)
 120{
 121        struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
 122        struct xfrm_replay_state_esn *rs;
 123
 124        if (!rt)
 125                return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
 126
 127        rs = nla_data(rt);
 128
 129        if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
 130                return -EINVAL;
 131
 132        if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
 133            nla_len(rt) != sizeof(*rs))
 134                return -EINVAL;
 135
 136        /* As only ESP and AH support ESN feature. */
 137        if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
 138                return -EINVAL;
 139
 140        if (p->replay_window != 0)
 141                return -EINVAL;
 142
 143        return 0;
 144}
 145
 146static int verify_newsa_info(struct xfrm_usersa_info *p,
 147                             struct nlattr **attrs)
 148{
 149        int err;
 150
 151        err = -EINVAL;
 152        switch (p->family) {
 153        case AF_INET:
 154                break;
 155
 156        case AF_INET6:
 157#if IS_ENABLED(CONFIG_IPV6)
 158                break;
 159#else
 160                err = -EAFNOSUPPORT;
 161                goto out;
 162#endif
 163
 164        default:
 165                goto out;
 166        }
 167
 168        err = -EINVAL;
 169        switch (p->id.proto) {
 170        case IPPROTO_AH:
 171                if ((!attrs[XFRMA_ALG_AUTH]     &&
 172                     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
 173                    attrs[XFRMA_ALG_AEAD]       ||
 174                    attrs[XFRMA_ALG_CRYPT]      ||
 175                    attrs[XFRMA_ALG_COMP]       ||
 176                    attrs[XFRMA_TFCPAD])
 177                        goto out;
 178                break;
 179
 180        case IPPROTO_ESP:
 181                if (attrs[XFRMA_ALG_COMP])
 182                        goto out;
 183                if (!attrs[XFRMA_ALG_AUTH] &&
 184                    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
 185                    !attrs[XFRMA_ALG_CRYPT] &&
 186                    !attrs[XFRMA_ALG_AEAD])
 187                        goto out;
 188                if ((attrs[XFRMA_ALG_AUTH] ||
 189                     attrs[XFRMA_ALG_AUTH_TRUNC] ||
 190                     attrs[XFRMA_ALG_CRYPT]) &&
 191                    attrs[XFRMA_ALG_AEAD])
 192                        goto out;
 193                if (attrs[XFRMA_TFCPAD] &&
 194                    p->mode != XFRM_MODE_TUNNEL)
 195                        goto out;
 196                break;
 197
 198        case IPPROTO_COMP:
 199                if (!attrs[XFRMA_ALG_COMP]      ||
 200                    attrs[XFRMA_ALG_AEAD]       ||
 201                    attrs[XFRMA_ALG_AUTH]       ||
 202                    attrs[XFRMA_ALG_AUTH_TRUNC] ||
 203                    attrs[XFRMA_ALG_CRYPT]      ||
 204                    attrs[XFRMA_TFCPAD]         ||
 205                    (ntohl(p->id.spi) >= 0x10000))
 206                        goto out;
 207                break;
 208
 209#if IS_ENABLED(CONFIG_IPV6)
 210        case IPPROTO_DSTOPTS:
 211        case IPPROTO_ROUTING:
 212                if (attrs[XFRMA_ALG_COMP]       ||
 213                    attrs[XFRMA_ALG_AUTH]       ||
 214                    attrs[XFRMA_ALG_AUTH_TRUNC] ||
 215                    attrs[XFRMA_ALG_AEAD]       ||
 216                    attrs[XFRMA_ALG_CRYPT]      ||
 217                    attrs[XFRMA_ENCAP]          ||
 218                    attrs[XFRMA_SEC_CTX]        ||
 219                    attrs[XFRMA_TFCPAD]         ||
 220                    !attrs[XFRMA_COADDR])
 221                        goto out;
 222                break;
 223#endif
 224
 225        default:
 226                goto out;
 227        }
 228
 229        if ((err = verify_aead(attrs)))
 230                goto out;
 231        if ((err = verify_auth_trunc(attrs)))
 232                goto out;
 233        if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
 234                goto out;
 235        if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
 236                goto out;
 237        if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
 238                goto out;
 239        if ((err = verify_sec_ctx_len(attrs)))
 240                goto out;
 241        if ((err = verify_replay(p, attrs)))
 242                goto out;
 243
 244        err = -EINVAL;
 245        switch (p->mode) {
 246        case XFRM_MODE_TRANSPORT:
 247        case XFRM_MODE_TUNNEL:
 248        case XFRM_MODE_ROUTEOPTIMIZATION:
 249        case XFRM_MODE_BEET:
 250                break;
 251
 252        default:
 253                goto out;
 254        }
 255
 256        err = 0;
 257
 258out:
 259        return err;
 260}
 261
 262static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
 263                           struct xfrm_algo_desc *(*get_byname)(const char *, int),
 264                           struct nlattr *rta)
 265{
 266        struct xfrm_algo *p, *ualg;
 267        struct xfrm_algo_desc *algo;
 268
 269        if (!rta)
 270                return 0;
 271
 272        ualg = nla_data(rta);
 273
 274        algo = get_byname(ualg->alg_name, 1);
 275        if (!algo)
 276                return -ENOSYS;
 277        *props = algo->desc.sadb_alg_id;
 278
 279        p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
 280        if (!p)
 281                return -ENOMEM;
 282
 283        strcpy(p->alg_name, algo->name);
 284        *algpp = p;
 285        return 0;
 286}
 287
 288static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
 289{
 290        struct xfrm_algo *p, *ualg;
 291        struct xfrm_algo_desc *algo;
 292
 293        if (!rta)
 294                return 0;
 295
 296        ualg = nla_data(rta);
 297
 298        algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
 299        if (!algo)
 300                return -ENOSYS;
 301        x->props.ealgo = algo->desc.sadb_alg_id;
 302
 303        p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
 304        if (!p)
 305                return -ENOMEM;
 306
 307        strcpy(p->alg_name, algo->name);
 308        x->ealg = p;
 309        x->geniv = algo->uinfo.encr.geniv;
 310        return 0;
 311}
 312
 313static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
 314                       struct nlattr *rta)
 315{
 316        struct xfrm_algo *ualg;
 317        struct xfrm_algo_auth *p;
 318        struct xfrm_algo_desc *algo;
 319
 320        if (!rta)
 321                return 0;
 322
 323        ualg = nla_data(rta);
 324
 325        algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
 326        if (!algo)
 327                return -ENOSYS;
 328        *props = algo->desc.sadb_alg_id;
 329
 330        p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
 331        if (!p)
 332                return -ENOMEM;
 333
 334        strcpy(p->alg_name, algo->name);
 335        p->alg_key_len = ualg->alg_key_len;
 336        p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
 337        memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
 338
 339        *algpp = p;
 340        return 0;
 341}
 342
 343static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
 344                             struct nlattr *rta)
 345{
 346        struct xfrm_algo_auth *p, *ualg;
 347        struct xfrm_algo_desc *algo;
 348
 349        if (!rta)
 350                return 0;
 351
 352        ualg = nla_data(rta);
 353
 354        algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
 355        if (!algo)
 356                return -ENOSYS;
 357        if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
 358                return -EINVAL;
 359        *props = algo->desc.sadb_alg_id;
 360
 361        p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
 362        if (!p)
 363                return -ENOMEM;
 364
 365        strcpy(p->alg_name, algo->name);
 366        if (!p->alg_trunc_len)
 367                p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
 368
 369        *algpp = p;
 370        return 0;
 371}
 372
 373static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
 374{
 375        struct xfrm_algo_aead *p, *ualg;
 376        struct xfrm_algo_desc *algo;
 377
 378        if (!rta)
 379                return 0;
 380
 381        ualg = nla_data(rta);
 382
 383        algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
 384        if (!algo)
 385                return -ENOSYS;
 386        x->props.ealgo = algo->desc.sadb_alg_id;
 387
 388        p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
 389        if (!p)
 390                return -ENOMEM;
 391
 392        strcpy(p->alg_name, algo->name);
 393        x->aead = p;
 394        x->geniv = algo->uinfo.aead.geniv;
 395        return 0;
 396}
 397
 398static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
 399                                         struct nlattr *rp)
 400{
 401        struct xfrm_replay_state_esn *up;
 402        unsigned int ulen;
 403
 404        if (!replay_esn || !rp)
 405                return 0;
 406
 407        up = nla_data(rp);
 408        ulen = xfrm_replay_state_esn_len(up);
 409
 410        /* Check the overall length and the internal bitmap length to avoid
 411         * potential overflow. */
 412        if (nla_len(rp) < (int)ulen ||
 413            xfrm_replay_state_esn_len(replay_esn) != ulen ||
 414            replay_esn->bmp_len != up->bmp_len)
 415                return -EINVAL;
 416
 417        if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
 418                return -EINVAL;
 419
 420        return 0;
 421}
 422
 423static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
 424                                       struct xfrm_replay_state_esn **preplay_esn,
 425                                       struct nlattr *rta)
 426{
 427        struct xfrm_replay_state_esn *p, *pp, *up;
 428        unsigned int klen, ulen;
 429
 430        if (!rta)
 431                return 0;
 432
 433        up = nla_data(rta);
 434        klen = xfrm_replay_state_esn_len(up);
 435        ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
 436
 437        p = kzalloc(klen, GFP_KERNEL);
 438        if (!p)
 439                return -ENOMEM;
 440
 441        pp = kzalloc(klen, GFP_KERNEL);
 442        if (!pp) {
 443                kfree(p);
 444                return -ENOMEM;
 445        }
 446
 447        memcpy(p, up, ulen);
 448        memcpy(pp, up, ulen);
 449
 450        *replay_esn = p;
 451        *preplay_esn = pp;
 452
 453        return 0;
 454}
 455
 456static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
 457{
 458        unsigned int len = 0;
 459
 460        if (xfrm_ctx) {
 461                len += sizeof(struct xfrm_user_sec_ctx);
 462                len += xfrm_ctx->ctx_len;
 463        }
 464        return len;
 465}
 466
 467static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
 468{
 469        memcpy(&x->id, &p->id, sizeof(x->id));
 470        memcpy(&x->sel, &p->sel, sizeof(x->sel));
 471        memcpy(&x->lft, &p->lft, sizeof(x->lft));
 472        x->props.mode = p->mode;
 473        x->props.replay_window = min_t(unsigned int, p->replay_window,
 474                                        sizeof(x->replay.bitmap) * 8);
 475        x->props.reqid = p->reqid;
 476        x->props.family = p->family;
 477        memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
 478        x->props.flags = p->flags;
 479
 480        if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
 481                x->sel.family = p->family;
 482}
 483
 484/*
 485 * someday when pfkey also has support, we could have the code
 486 * somehow made shareable and move it to xfrm_state.c - JHS
 487 *
 488*/
 489static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
 490                                  int update_esn)
 491{
 492        struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
 493        struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
 494        struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
 495        struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
 496        struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
 497
 498        if (re) {
 499                struct xfrm_replay_state_esn *replay_esn;
 500                replay_esn = nla_data(re);
 501                memcpy(x->replay_esn, replay_esn,
 502                       xfrm_replay_state_esn_len(replay_esn));
 503                memcpy(x->preplay_esn, replay_esn,
 504                       xfrm_replay_state_esn_len(replay_esn));
 505        }
 506
 507        if (rp) {
 508                struct xfrm_replay_state *replay;
 509                replay = nla_data(rp);
 510                memcpy(&x->replay, replay, sizeof(*replay));
 511                memcpy(&x->preplay, replay, sizeof(*replay));
 512        }
 513
 514        if (lt) {
 515                struct xfrm_lifetime_cur *ltime;
 516                ltime = nla_data(lt);
 517                x->curlft.bytes = ltime->bytes;
 518                x->curlft.packets = ltime->packets;
 519                x->curlft.add_time = ltime->add_time;
 520                x->curlft.use_time = ltime->use_time;
 521        }
 522
 523        if (et)
 524                x->replay_maxage = nla_get_u32(et);
 525
 526        if (rt)
 527                x->replay_maxdiff = nla_get_u32(rt);
 528}
 529
 530static struct xfrm_state *xfrm_state_construct(struct net *net,
 531                                               struct xfrm_usersa_info *p,
 532                                               struct nlattr **attrs,
 533                                               int *errp)
 534{
 535        struct xfrm_state *x = xfrm_state_alloc(net);
 536        int err = -ENOMEM;
 537
 538        if (!x)
 539                goto error_no_put;
 540
 541        copy_from_user_state(x, p);
 542
 543        if (attrs[XFRMA_SA_EXTRA_FLAGS])
 544                x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
 545
 546        if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
 547                goto error;
 548        if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
 549                                     attrs[XFRMA_ALG_AUTH_TRUNC])))
 550                goto error;
 551        if (!x->props.aalgo) {
 552                if ((err = attach_auth(&x->aalg, &x->props.aalgo,
 553                                       attrs[XFRMA_ALG_AUTH])))
 554                        goto error;
 555        }
 556        if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
 557                goto error;
 558        if ((err = attach_one_algo(&x->calg, &x->props.calgo,
 559                                   xfrm_calg_get_byname,
 560                                   attrs[XFRMA_ALG_COMP])))
 561                goto error;
 562
 563        if (attrs[XFRMA_ENCAP]) {
 564                x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
 565                                   sizeof(*x->encap), GFP_KERNEL);
 566                if (x->encap == NULL)
 567                        goto error;
 568        }
 569
 570        if (attrs[XFRMA_TFCPAD])
 571                x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
 572
 573        if (attrs[XFRMA_COADDR]) {
 574                x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
 575                                    sizeof(*x->coaddr), GFP_KERNEL);
 576                if (x->coaddr == NULL)
 577                        goto error;
 578        }
 579
 580        xfrm_mark_get(attrs, &x->mark);
 581
 582        if (attrs[XFRMA_OUTPUT_MARK])
 583                x->props.output_mark = nla_get_u32(attrs[XFRMA_OUTPUT_MARK]);
 584
 585        err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
 586        if (err)
 587                goto error;
 588
 589        if (attrs[XFRMA_SEC_CTX]) {
 590                err = security_xfrm_state_alloc(x,
 591                                                nla_data(attrs[XFRMA_SEC_CTX]));
 592                if (err)
 593                        goto error;
 594        }
 595
 596        if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
 597                                               attrs[XFRMA_REPLAY_ESN_VAL])))
 598                goto error;
 599
 600        x->km.seq = p->seq;
 601        x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
 602        /* sysctl_xfrm_aevent_etime is in 100ms units */
 603        x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
 604
 605        if ((err = xfrm_init_replay(x)))
 606                goto error;
 607
 608        /* override default values from above */
 609        xfrm_update_ae_params(x, attrs, 0);
 610
 611        /* configure the hardware if offload is requested */
 612        if (attrs[XFRMA_OFFLOAD_DEV]) {
 613                err = xfrm_dev_state_add(net, x,
 614                                         nla_data(attrs[XFRMA_OFFLOAD_DEV]));
 615                if (err)
 616                        goto error;
 617        }
 618
 619        return x;
 620
 621error:
 622        x->km.state = XFRM_STATE_DEAD;
 623        xfrm_state_put(x);
 624error_no_put:
 625        *errp = err;
 626        return NULL;
 627}
 628
 629static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
 630                struct nlattr **attrs)
 631{
 632        struct net *net = sock_net(skb->sk);
 633        struct xfrm_usersa_info *p = nlmsg_data(nlh);
 634        struct xfrm_state *x;
 635        int err;
 636        struct km_event c;
 637
 638        err = verify_newsa_info(p, attrs);
 639        if (err)
 640                return err;
 641
 642        x = xfrm_state_construct(net, p, attrs, &err);
 643        if (!x)
 644                return err;
 645
 646        xfrm_state_hold(x);
 647        if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
 648                err = xfrm_state_add(x);
 649        else
 650                err = xfrm_state_update(x);
 651
 652        xfrm_audit_state_add(x, err ? 0 : 1, true);
 653
 654        if (err < 0) {
 655                x->km.state = XFRM_STATE_DEAD;
 656                xfrm_dev_state_delete(x);
 657                __xfrm_state_put(x);
 658                goto out;
 659        }
 660
 661        if (x->km.state == XFRM_STATE_VOID)
 662                x->km.state = XFRM_STATE_VALID;
 663
 664        c.seq = nlh->nlmsg_seq;
 665        c.portid = nlh->nlmsg_pid;
 666        c.event = nlh->nlmsg_type;
 667
 668        km_state_notify(x, &c);
 669out:
 670        xfrm_state_put(x);
 671        return err;
 672}
 673
 674static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
 675                                                 struct xfrm_usersa_id *p,
 676                                                 struct nlattr **attrs,
 677                                                 int *errp)
 678{
 679        struct xfrm_state *x = NULL;
 680        struct xfrm_mark m;
 681        int err;
 682        u32 mark = xfrm_mark_get(attrs, &m);
 683
 684        if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
 685                err = -ESRCH;
 686                x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
 687        } else {
 688                xfrm_address_t *saddr = NULL;
 689
 690                verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
 691                if (!saddr) {
 692                        err = -EINVAL;
 693                        goto out;
 694                }
 695
 696                err = -ESRCH;
 697                x = xfrm_state_lookup_byaddr(net, mark,
 698                                             &p->daddr, saddr,
 699                                             p->proto, p->family);
 700        }
 701
 702 out:
 703        if (!x && errp)
 704                *errp = err;
 705        return x;
 706}
 707
 708static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
 709                struct nlattr **attrs)
 710{
 711        struct net *net = sock_net(skb->sk);
 712        struct xfrm_state *x;
 713        int err = -ESRCH;
 714        struct km_event c;
 715        struct xfrm_usersa_id *p = nlmsg_data(nlh);
 716
 717        x = xfrm_user_state_lookup(net, p, attrs, &err);
 718        if (x == NULL)
 719                return err;
 720
 721        if ((err = security_xfrm_state_delete(x)) != 0)
 722                goto out;
 723
 724        if (xfrm_state_kern(x)) {
 725                err = -EPERM;
 726                goto out;
 727        }
 728
 729        err = xfrm_state_delete(x);
 730
 731        if (err < 0)
 732                goto out;
 733
 734        c.seq = nlh->nlmsg_seq;
 735        c.portid = nlh->nlmsg_pid;
 736        c.event = nlh->nlmsg_type;
 737        km_state_notify(x, &c);
 738
 739out:
 740        xfrm_audit_state_delete(x, err ? 0 : 1, true);
 741        xfrm_state_put(x);
 742        return err;
 743}
 744
 745static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
 746{
 747        memset(p, 0, sizeof(*p));
 748        memcpy(&p->id, &x->id, sizeof(p->id));
 749        memcpy(&p->sel, &x->sel, sizeof(p->sel));
 750        memcpy(&p->lft, &x->lft, sizeof(p->lft));
 751        memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
 752        put_unaligned(x->stats.replay_window, &p->stats.replay_window);
 753        put_unaligned(x->stats.replay, &p->stats.replay);
 754        put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
 755        memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
 756        p->mode = x->props.mode;
 757        p->replay_window = x->props.replay_window;
 758        p->reqid = x->props.reqid;
 759        p->family = x->props.family;
 760        p->flags = x->props.flags;
 761        p->seq = x->km.seq;
 762}
 763
 764struct xfrm_dump_info {
 765        struct sk_buff *in_skb;
 766        struct sk_buff *out_skb;
 767        u32 nlmsg_seq;
 768        u16 nlmsg_flags;
 769};
 770
 771static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
 772{
 773        struct xfrm_user_sec_ctx *uctx;
 774        struct nlattr *attr;
 775        int ctx_size = sizeof(*uctx) + s->ctx_len;
 776
 777        attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
 778        if (attr == NULL)
 779                return -EMSGSIZE;
 780
 781        uctx = nla_data(attr);
 782        uctx->exttype = XFRMA_SEC_CTX;
 783        uctx->len = ctx_size;
 784        uctx->ctx_doi = s->ctx_doi;
 785        uctx->ctx_alg = s->ctx_alg;
 786        uctx->ctx_len = s->ctx_len;
 787        memcpy(uctx + 1, s->ctx_str, s->ctx_len);
 788
 789        return 0;
 790}
 791
 792static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
 793{
 794        struct xfrm_user_offload *xuo;
 795        struct nlattr *attr;
 796
 797        attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
 798        if (attr == NULL)
 799                return -EMSGSIZE;
 800
 801        xuo = nla_data(attr);
 802        memset(xuo, 0, sizeof(*xuo));
 803        xuo->ifindex = xso->dev->ifindex;
 804        xuo->flags = xso->flags;
 805
 806        return 0;
 807}
 808
 809static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
 810{
 811        struct xfrm_algo *algo;
 812        struct nlattr *nla;
 813
 814        nla = nla_reserve(skb, XFRMA_ALG_AUTH,
 815                          sizeof(*algo) + (auth->alg_key_len + 7) / 8);
 816        if (!nla)
 817                return -EMSGSIZE;
 818
 819        algo = nla_data(nla);
 820        strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
 821        memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
 822        algo->alg_key_len = auth->alg_key_len;
 823
 824        return 0;
 825}
 826
 827/* Don't change this without updating xfrm_sa_len! */
 828static int copy_to_user_state_extra(struct xfrm_state *x,
 829                                    struct xfrm_usersa_info *p,
 830                                    struct sk_buff *skb)
 831{
 832        int ret = 0;
 833
 834        copy_to_user_state(x, p);
 835
 836        if (x->props.extra_flags) {
 837                ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
 838                                  x->props.extra_flags);
 839                if (ret)
 840                        goto out;
 841        }
 842
 843        if (x->coaddr) {
 844                ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
 845                if (ret)
 846                        goto out;
 847        }
 848        if (x->lastused) {
 849                ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
 850                                        XFRMA_PAD);
 851                if (ret)
 852                        goto out;
 853        }
 854        if (x->aead) {
 855                ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
 856                if (ret)
 857                        goto out;
 858        }
 859        if (x->aalg) {
 860                ret = copy_to_user_auth(x->aalg, skb);
 861                if (!ret)
 862                        ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
 863                                      xfrm_alg_auth_len(x->aalg), x->aalg);
 864                if (ret)
 865                        goto out;
 866        }
 867        if (x->ealg) {
 868                ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
 869                if (ret)
 870                        goto out;
 871        }
 872        if (x->calg) {
 873                ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
 874                if (ret)
 875                        goto out;
 876        }
 877        if (x->encap) {
 878                ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
 879                if (ret)
 880                        goto out;
 881        }
 882        if (x->tfcpad) {
 883                ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
 884                if (ret)
 885                        goto out;
 886        }
 887        ret = xfrm_mark_put(skb, &x->mark);
 888        if (ret)
 889                goto out;
 890        if (x->replay_esn)
 891                ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
 892                              xfrm_replay_state_esn_len(x->replay_esn),
 893                              x->replay_esn);
 894        else
 895                ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
 896                              &x->replay);
 897        if (ret)
 898                goto out;
 899        if(x->xso.dev)
 900                ret = copy_user_offload(&x->xso, skb);
 901        if (ret)
 902                goto out;
 903        if (x->props.output_mark) {
 904                ret = nla_put_u32(skb, XFRMA_OUTPUT_MARK, x->props.output_mark);
 905                if (ret)
 906                        goto out;
 907        }
 908        if (x->security)
 909                ret = copy_sec_ctx(x->security, skb);
 910out:
 911        return ret;
 912}
 913
 914static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
 915{
 916        struct xfrm_dump_info *sp = ptr;
 917        struct sk_buff *in_skb = sp->in_skb;
 918        struct sk_buff *skb = sp->out_skb;
 919        struct xfrm_usersa_info *p;
 920        struct nlmsghdr *nlh;
 921        int err;
 922
 923        nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
 924                        XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
 925        if (nlh == NULL)
 926                return -EMSGSIZE;
 927
 928        p = nlmsg_data(nlh);
 929
 930        err = copy_to_user_state_extra(x, p, skb);
 931        if (err) {
 932                nlmsg_cancel(skb, nlh);
 933                return err;
 934        }
 935        nlmsg_end(skb, nlh);
 936        return 0;
 937}
 938
 939static int xfrm_dump_sa_done(struct netlink_callback *cb)
 940{
 941        struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
 942        struct sock *sk = cb->skb->sk;
 943        struct net *net = sock_net(sk);
 944
 945        if (cb->args[0])
 946                xfrm_state_walk_done(walk, net);
 947        return 0;
 948}
 949
 950static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
 951static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
 952{
 953        struct net *net = sock_net(skb->sk);
 954        struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
 955        struct xfrm_dump_info info;
 956
 957        BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
 958                     sizeof(cb->args) - sizeof(cb->args[0]));
 959
 960        info.in_skb = cb->skb;
 961        info.out_skb = skb;
 962        info.nlmsg_seq = cb->nlh->nlmsg_seq;
 963        info.nlmsg_flags = NLM_F_MULTI;
 964
 965        if (!cb->args[0]) {
 966                struct nlattr *attrs[XFRMA_MAX+1];
 967                struct xfrm_address_filter *filter = NULL;
 968                u8 proto = 0;
 969                int err;
 970
 971                err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX, xfrma_policy,
 972                                  NULL);
 973                if (err < 0)
 974                        return err;
 975
 976                if (attrs[XFRMA_ADDRESS_FILTER]) {
 977                        filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
 978                                         sizeof(*filter), GFP_KERNEL);
 979                        if (filter == NULL)
 980                                return -ENOMEM;
 981                }
 982
 983                if (attrs[XFRMA_PROTO])
 984                        proto = nla_get_u8(attrs[XFRMA_PROTO]);
 985
 986                xfrm_state_walk_init(walk, proto, filter);
 987                cb->args[0] = 1;
 988        }
 989
 990        (void) xfrm_state_walk(net, walk, dump_one_state, &info);
 991
 992        return skb->len;
 993}
 994
 995static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
 996                                          struct xfrm_state *x, u32 seq)
 997{
 998        struct xfrm_dump_info info;
 999        struct sk_buff *skb;
1000        int err;
1001
1002        skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1003        if (!skb)
1004                return ERR_PTR(-ENOMEM);
1005
1006        info.in_skb = in_skb;
1007        info.out_skb = skb;
1008        info.nlmsg_seq = seq;
1009        info.nlmsg_flags = 0;
1010
1011        err = dump_one_state(x, 0, &info);
1012        if (err) {
1013                kfree_skb(skb);
1014                return ERR_PTR(err);
1015        }
1016
1017        return skb;
1018}
1019
1020/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1021 * Must be called with RCU read lock.
1022 */
1023static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1024                                       u32 pid, unsigned int group)
1025{
1026        struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1027
1028        if (!nlsk) {
1029                kfree_skb(skb);
1030                return -EPIPE;
1031        }
1032
1033        return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1034}
1035
1036static inline unsigned int xfrm_spdinfo_msgsize(void)
1037{
1038        return NLMSG_ALIGN(4)
1039               + nla_total_size(sizeof(struct xfrmu_spdinfo))
1040               + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1041               + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1042               + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1043}
1044
1045static int build_spdinfo(struct sk_buff *skb, struct net *net,
1046                         u32 portid, u32 seq, u32 flags)
1047{
1048        struct xfrmk_spdinfo si;
1049        struct xfrmu_spdinfo spc;
1050        struct xfrmu_spdhinfo sph;
1051        struct xfrmu_spdhthresh spt4, spt6;
1052        struct nlmsghdr *nlh;
1053        int err;
1054        u32 *f;
1055        unsigned lseq;
1056
1057        nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1058        if (nlh == NULL) /* shouldn't really happen ... */
1059                return -EMSGSIZE;
1060
1061        f = nlmsg_data(nlh);
1062        *f = flags;
1063        xfrm_spd_getinfo(net, &si);
1064        spc.incnt = si.incnt;
1065        spc.outcnt = si.outcnt;
1066        spc.fwdcnt = si.fwdcnt;
1067        spc.inscnt = si.inscnt;
1068        spc.outscnt = si.outscnt;
1069        spc.fwdscnt = si.fwdscnt;
1070        sph.spdhcnt = si.spdhcnt;
1071        sph.spdhmcnt = si.spdhmcnt;
1072
1073        do {
1074                lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1075
1076                spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1077                spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1078                spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1079                spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1080        } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1081
1082        err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1083        if (!err)
1084                err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1085        if (!err)
1086                err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1087        if (!err)
1088                err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1089        if (err) {
1090                nlmsg_cancel(skb, nlh);
1091                return err;
1092        }
1093
1094        nlmsg_end(skb, nlh);
1095        return 0;
1096}
1097
1098static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1099                            struct nlattr **attrs)
1100{
1101        struct net *net = sock_net(skb->sk);
1102        struct xfrmu_spdhthresh *thresh4 = NULL;
1103        struct xfrmu_spdhthresh *thresh6 = NULL;
1104
1105        /* selector prefixlen thresholds to hash policies */
1106        if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1107                struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1108
1109                if (nla_len(rta) < sizeof(*thresh4))
1110                        return -EINVAL;
1111                thresh4 = nla_data(rta);
1112                if (thresh4->lbits > 32 || thresh4->rbits > 32)
1113                        return -EINVAL;
1114        }
1115        if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1116                struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1117
1118                if (nla_len(rta) < sizeof(*thresh6))
1119                        return -EINVAL;
1120                thresh6 = nla_data(rta);
1121                if (thresh6->lbits > 128 || thresh6->rbits > 128)
1122                        return -EINVAL;
1123        }
1124
1125        if (thresh4 || thresh6) {
1126                write_seqlock(&net->xfrm.policy_hthresh.lock);
1127                if (thresh4) {
1128                        net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1129                        net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1130                }
1131                if (thresh6) {
1132                        net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1133                        net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1134                }
1135                write_sequnlock(&net->xfrm.policy_hthresh.lock);
1136
1137                xfrm_policy_hash_rebuild(net);
1138        }
1139
1140        return 0;
1141}
1142
1143static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1144                struct nlattr **attrs)
1145{
1146        struct net *net = sock_net(skb->sk);
1147        struct sk_buff *r_skb;
1148        u32 *flags = nlmsg_data(nlh);
1149        u32 sportid = NETLINK_CB(skb).portid;
1150        u32 seq = nlh->nlmsg_seq;
1151        int err;
1152
1153        r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1154        if (r_skb == NULL)
1155                return -ENOMEM;
1156
1157        err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1158        BUG_ON(err < 0);
1159
1160        return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1161}
1162
1163static inline unsigned int xfrm_sadinfo_msgsize(void)
1164{
1165        return NLMSG_ALIGN(4)
1166               + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1167               + nla_total_size(4); /* XFRMA_SAD_CNT */
1168}
1169
1170static int build_sadinfo(struct sk_buff *skb, struct net *net,
1171                         u32 portid, u32 seq, u32 flags)
1172{
1173        struct xfrmk_sadinfo si;
1174        struct xfrmu_sadhinfo sh;
1175        struct nlmsghdr *nlh;
1176        int err;
1177        u32 *f;
1178
1179        nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1180        if (nlh == NULL) /* shouldn't really happen ... */
1181                return -EMSGSIZE;
1182
1183        f = nlmsg_data(nlh);
1184        *f = flags;
1185        xfrm_sad_getinfo(net, &si);
1186
1187        sh.sadhmcnt = si.sadhmcnt;
1188        sh.sadhcnt = si.sadhcnt;
1189
1190        err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1191        if (!err)
1192                err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1193        if (err) {
1194                nlmsg_cancel(skb, nlh);
1195                return err;
1196        }
1197
1198        nlmsg_end(skb, nlh);
1199        return 0;
1200}
1201
1202static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1203                struct nlattr **attrs)
1204{
1205        struct net *net = sock_net(skb->sk);
1206        struct sk_buff *r_skb;
1207        u32 *flags = nlmsg_data(nlh);
1208        u32 sportid = NETLINK_CB(skb).portid;
1209        u32 seq = nlh->nlmsg_seq;
1210        int err;
1211
1212        r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1213        if (r_skb == NULL)
1214                return -ENOMEM;
1215
1216        err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1217        BUG_ON(err < 0);
1218
1219        return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1220}
1221
1222static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1223                struct nlattr **attrs)
1224{
1225        struct net *net = sock_net(skb->sk);
1226        struct xfrm_usersa_id *p = nlmsg_data(nlh);
1227        struct xfrm_state *x;
1228        struct sk_buff *resp_skb;
1229        int err = -ESRCH;
1230
1231        x = xfrm_user_state_lookup(net, p, attrs, &err);
1232        if (x == NULL)
1233                goto out_noput;
1234
1235        resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1236        if (IS_ERR(resp_skb)) {
1237                err = PTR_ERR(resp_skb);
1238        } else {
1239                err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1240        }
1241        xfrm_state_put(x);
1242out_noput:
1243        return err;
1244}
1245
1246static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1247                struct nlattr **attrs)
1248{
1249        struct net *net = sock_net(skb->sk);
1250        struct xfrm_state *x;
1251        struct xfrm_userspi_info *p;
1252        struct sk_buff *resp_skb;
1253        xfrm_address_t *daddr;
1254        int family;
1255        int err;
1256        u32 mark;
1257        struct xfrm_mark m;
1258
1259        p = nlmsg_data(nlh);
1260        err = verify_spi_info(p->info.id.proto, p->min, p->max);
1261        if (err)
1262                goto out_noput;
1263
1264        family = p->info.family;
1265        daddr = &p->info.id.daddr;
1266
1267        x = NULL;
1268
1269        mark = xfrm_mark_get(attrs, &m);
1270        if (p->info.seq) {
1271                x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1272                if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1273                        xfrm_state_put(x);
1274                        x = NULL;
1275                }
1276        }
1277
1278        if (!x)
1279                x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1280                                  p->info.id.proto, daddr,
1281                                  &p->info.saddr, 1,
1282                                  family);
1283        err = -ENOENT;
1284        if (x == NULL)
1285                goto out_noput;
1286
1287        err = xfrm_alloc_spi(x, p->min, p->max);
1288        if (err)
1289                goto out;
1290
1291        resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1292        if (IS_ERR(resp_skb)) {
1293                err = PTR_ERR(resp_skb);
1294                goto out;
1295        }
1296
1297        err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1298
1299out:
1300        xfrm_state_put(x);
1301out_noput:
1302        return err;
1303}
1304
1305static int verify_policy_dir(u8 dir)
1306{
1307        switch (dir) {
1308        case XFRM_POLICY_IN:
1309        case XFRM_POLICY_OUT:
1310        case XFRM_POLICY_FWD:
1311                break;
1312
1313        default:
1314                return -EINVAL;
1315        }
1316
1317        return 0;
1318}
1319
1320static int verify_policy_type(u8 type)
1321{
1322        switch (type) {
1323        case XFRM_POLICY_TYPE_MAIN:
1324#ifdef CONFIG_XFRM_SUB_POLICY
1325        case XFRM_POLICY_TYPE_SUB:
1326#endif
1327                break;
1328
1329        default:
1330                return -EINVAL;
1331        }
1332
1333        return 0;
1334}
1335
1336static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1337{
1338        int ret;
1339
1340        switch (p->share) {
1341        case XFRM_SHARE_ANY:
1342        case XFRM_SHARE_SESSION:
1343        case XFRM_SHARE_USER:
1344        case XFRM_SHARE_UNIQUE:
1345                break;
1346
1347        default:
1348                return -EINVAL;
1349        }
1350
1351        switch (p->action) {
1352        case XFRM_POLICY_ALLOW:
1353        case XFRM_POLICY_BLOCK:
1354                break;
1355
1356        default:
1357                return -EINVAL;
1358        }
1359
1360        switch (p->sel.family) {
1361        case AF_INET:
1362                break;
1363
1364        case AF_INET6:
1365#if IS_ENABLED(CONFIG_IPV6)
1366                break;
1367#else
1368                return  -EAFNOSUPPORT;
1369#endif
1370
1371        default:
1372                return -EINVAL;
1373        }
1374
1375        ret = verify_policy_dir(p->dir);
1376        if (ret)
1377                return ret;
1378        if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1379                return -EINVAL;
1380
1381        return 0;
1382}
1383
1384static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1385{
1386        struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1387        struct xfrm_user_sec_ctx *uctx;
1388
1389        if (!rt)
1390                return 0;
1391
1392        uctx = nla_data(rt);
1393        return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1394}
1395
1396static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1397                           int nr)
1398{
1399        int i;
1400
1401        xp->xfrm_nr = nr;
1402        for (i = 0; i < nr; i++, ut++) {
1403                struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1404
1405                memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1406                memcpy(&t->saddr, &ut->saddr,
1407                       sizeof(xfrm_address_t));
1408                t->reqid = ut->reqid;
1409                t->mode = ut->mode;
1410                t->share = ut->share;
1411                t->optional = ut->optional;
1412                t->aalgos = ut->aalgos;
1413                t->ealgos = ut->ealgos;
1414                t->calgos = ut->calgos;
1415                /* If all masks are ~0, then we allow all algorithms. */
1416                t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1417                t->encap_family = ut->family;
1418        }
1419}
1420
1421static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1422{
1423        u16 prev_family;
1424        int i;
1425
1426        if (nr > XFRM_MAX_DEPTH)
1427                return -EINVAL;
1428
1429        prev_family = family;
1430
1431        for (i = 0; i < nr; i++) {
1432                /* We never validated the ut->family value, so many
1433                 * applications simply leave it at zero.  The check was
1434                 * never made and ut->family was ignored because all
1435                 * templates could be assumed to have the same family as
1436                 * the policy itself.  Now that we will have ipv4-in-ipv6
1437                 * and ipv6-in-ipv4 tunnels, this is no longer true.
1438                 */
1439                if (!ut[i].family)
1440                        ut[i].family = family;
1441
1442                if ((ut[i].mode == XFRM_MODE_TRANSPORT) &&
1443                    (ut[i].family != prev_family))
1444                        return -EINVAL;
1445
1446                prev_family = ut[i].family;
1447
1448                switch (ut[i].family) {
1449                case AF_INET:
1450                        break;
1451#if IS_ENABLED(CONFIG_IPV6)
1452                case AF_INET6:
1453                        break;
1454#endif
1455                default:
1456                        return -EINVAL;
1457                }
1458
1459                switch (ut[i].id.proto) {
1460                case IPPROTO_AH:
1461                case IPPROTO_ESP:
1462                case IPPROTO_COMP:
1463#if IS_ENABLED(CONFIG_IPV6)
1464                case IPPROTO_ROUTING:
1465                case IPPROTO_DSTOPTS:
1466#endif
1467                case IPSEC_PROTO_ANY:
1468                        break;
1469                default:
1470                        return -EINVAL;
1471                }
1472
1473        }
1474
1475        return 0;
1476}
1477
1478static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1479{
1480        struct nlattr *rt = attrs[XFRMA_TMPL];
1481
1482        if (!rt) {
1483                pol->xfrm_nr = 0;
1484        } else {
1485                struct xfrm_user_tmpl *utmpl = nla_data(rt);
1486                int nr = nla_len(rt) / sizeof(*utmpl);
1487                int err;
1488
1489                err = validate_tmpl(nr, utmpl, pol->family);
1490                if (err)
1491                        return err;
1492
1493                copy_templates(pol, utmpl, nr);
1494        }
1495        return 0;
1496}
1497
1498static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1499{
1500        struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1501        struct xfrm_userpolicy_type *upt;
1502        u8 type = XFRM_POLICY_TYPE_MAIN;
1503        int err;
1504
1505        if (rt) {
1506                upt = nla_data(rt);
1507                type = upt->type;
1508        }
1509
1510        err = verify_policy_type(type);
1511        if (err)
1512                return err;
1513
1514        *tp = type;
1515        return 0;
1516}
1517
1518static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1519{
1520        xp->priority = p->priority;
1521        xp->index = p->index;
1522        memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1523        memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1524        xp->action = p->action;
1525        xp->flags = p->flags;
1526        xp->family = p->sel.family;
1527        /* XXX xp->share = p->share; */
1528}
1529
1530static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1531{
1532        memset(p, 0, sizeof(*p));
1533        memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1534        memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1535        memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1536        p->priority = xp->priority;
1537        p->index = xp->index;
1538        p->sel.family = xp->family;
1539        p->dir = dir;
1540        p->action = xp->action;
1541        p->flags = xp->flags;
1542        p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1543}
1544
1545static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1546{
1547        struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1548        int err;
1549
1550        if (!xp) {
1551                *errp = -ENOMEM;
1552                return NULL;
1553        }
1554
1555        copy_from_user_policy(xp, p);
1556
1557        err = copy_from_user_policy_type(&xp->type, attrs);
1558        if (err)
1559                goto error;
1560
1561        if (!(err = copy_from_user_tmpl(xp, attrs)))
1562                err = copy_from_user_sec_ctx(xp, attrs);
1563        if (err)
1564                goto error;
1565
1566        xfrm_mark_get(attrs, &xp->mark);
1567
1568        return xp;
1569 error:
1570        *errp = err;
1571        xp->walk.dead = 1;
1572        xfrm_policy_destroy(xp);
1573        return NULL;
1574}
1575
1576static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1577                struct nlattr **attrs)
1578{
1579        struct net *net = sock_net(skb->sk);
1580        struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1581        struct xfrm_policy *xp;
1582        struct km_event c;
1583        int err;
1584        int excl;
1585
1586        err = verify_newpolicy_info(p);
1587        if (err)
1588                return err;
1589        err = verify_sec_ctx_len(attrs);
1590        if (err)
1591                return err;
1592
1593        xp = xfrm_policy_construct(net, p, attrs, &err);
1594        if (!xp)
1595                return err;
1596
1597        /* shouldn't excl be based on nlh flags??
1598         * Aha! this is anti-netlink really i.e  more pfkey derived
1599         * in netlink excl is a flag and you wouldnt need
1600         * a type XFRM_MSG_UPDPOLICY - JHS */
1601        excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1602        err = xfrm_policy_insert(p->dir, xp, excl);
1603        xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1604
1605        if (err) {
1606                security_xfrm_policy_free(xp->security);
1607                kfree(xp);
1608                return err;
1609        }
1610
1611        c.event = nlh->nlmsg_type;
1612        c.seq = nlh->nlmsg_seq;
1613        c.portid = nlh->nlmsg_pid;
1614        km_policy_notify(xp, p->dir, &c);
1615
1616        xfrm_pol_put(xp);
1617
1618        return 0;
1619}
1620
1621static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1622{
1623        struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1624        int i;
1625
1626        if (xp->xfrm_nr == 0)
1627                return 0;
1628
1629        for (i = 0; i < xp->xfrm_nr; i++) {
1630                struct xfrm_user_tmpl *up = &vec[i];
1631                struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1632
1633                memset(up, 0, sizeof(*up));
1634                memcpy(&up->id, &kp->id, sizeof(up->id));
1635                up->family = kp->encap_family;
1636                memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1637                up->reqid = kp->reqid;
1638                up->mode = kp->mode;
1639                up->share = kp->share;
1640                up->optional = kp->optional;
1641                up->aalgos = kp->aalgos;
1642                up->ealgos = kp->ealgos;
1643                up->calgos = kp->calgos;
1644        }
1645
1646        return nla_put(skb, XFRMA_TMPL,
1647                       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1648}
1649
1650static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1651{
1652        if (x->security) {
1653                return copy_sec_ctx(x->security, skb);
1654        }
1655        return 0;
1656}
1657
1658static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1659{
1660        if (xp->security)
1661                return copy_sec_ctx(xp->security, skb);
1662        return 0;
1663}
1664static inline unsigned int userpolicy_type_attrsize(void)
1665{
1666#ifdef CONFIG_XFRM_SUB_POLICY
1667        return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1668#else
1669        return 0;
1670#endif
1671}
1672
1673#ifdef CONFIG_XFRM_SUB_POLICY
1674static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1675{
1676        struct xfrm_userpolicy_type upt;
1677
1678        /* Sadly there are two holes in struct xfrm_userpolicy_type */
1679        memset(&upt, 0, sizeof(upt));
1680        upt.type = type;
1681
1682        return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1683}
1684
1685#else
1686static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1687{
1688        return 0;
1689}
1690#endif
1691
1692static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1693{
1694        struct xfrm_dump_info *sp = ptr;
1695        struct xfrm_userpolicy_info *p;
1696        struct sk_buff *in_skb = sp->in_skb;
1697        struct sk_buff *skb = sp->out_skb;
1698        struct nlmsghdr *nlh;
1699        int err;
1700
1701        nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1702                        XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1703        if (nlh == NULL)
1704                return -EMSGSIZE;
1705
1706        p = nlmsg_data(nlh);
1707        copy_to_user_policy(xp, p, dir);
1708        err = copy_to_user_tmpl(xp, skb);
1709        if (!err)
1710                err = copy_to_user_sec_ctx(xp, skb);
1711        if (!err)
1712                err = copy_to_user_policy_type(xp->type, skb);
1713        if (!err)
1714                err = xfrm_mark_put(skb, &xp->mark);
1715        if (err) {
1716                nlmsg_cancel(skb, nlh);
1717                return err;
1718        }
1719        nlmsg_end(skb, nlh);
1720        return 0;
1721}
1722
1723static int xfrm_dump_policy_done(struct netlink_callback *cb)
1724{
1725        struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1726        struct net *net = sock_net(cb->skb->sk);
1727
1728        xfrm_policy_walk_done(walk, net);
1729        return 0;
1730}
1731
1732static int xfrm_dump_policy_start(struct netlink_callback *cb)
1733{
1734        struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1735
1736        BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1737
1738        xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1739        return 0;
1740}
1741
1742static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1743{
1744        struct net *net = sock_net(skb->sk);
1745        struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1746        struct xfrm_dump_info info;
1747
1748        info.in_skb = cb->skb;
1749        info.out_skb = skb;
1750        info.nlmsg_seq = cb->nlh->nlmsg_seq;
1751        info.nlmsg_flags = NLM_F_MULTI;
1752
1753        (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1754
1755        return skb->len;
1756}
1757
1758static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1759                                          struct xfrm_policy *xp,
1760                                          int dir, u32 seq)
1761{
1762        struct xfrm_dump_info info;
1763        struct sk_buff *skb;
1764        int err;
1765
1766        skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1767        if (!skb)
1768                return ERR_PTR(-ENOMEM);
1769
1770        info.in_skb = in_skb;
1771        info.out_skb = skb;
1772        info.nlmsg_seq = seq;
1773        info.nlmsg_flags = 0;
1774
1775        err = dump_one_policy(xp, dir, 0, &info);
1776        if (err) {
1777                kfree_skb(skb);
1778                return ERR_PTR(err);
1779        }
1780
1781        return skb;
1782}
1783
1784static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1785                struct nlattr **attrs)
1786{
1787        struct net *net = sock_net(skb->sk);
1788        struct xfrm_policy *xp;
1789        struct xfrm_userpolicy_id *p;
1790        u8 type = XFRM_POLICY_TYPE_MAIN;
1791        int err;
1792        struct km_event c;
1793        int delete;
1794        struct xfrm_mark m;
1795        u32 mark = xfrm_mark_get(attrs, &m);
1796
1797        p = nlmsg_data(nlh);
1798        delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1799
1800        err = copy_from_user_policy_type(&type, attrs);
1801        if (err)
1802                return err;
1803
1804        err = verify_policy_dir(p->dir);
1805        if (err)
1806                return err;
1807
1808        if (p->index)
1809                xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1810        else {
1811                struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1812                struct xfrm_sec_ctx *ctx;
1813
1814                err = verify_sec_ctx_len(attrs);
1815                if (err)
1816                        return err;
1817
1818                ctx = NULL;
1819                if (rt) {
1820                        struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1821
1822                        err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1823                        if (err)
1824                                return err;
1825                }
1826                xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1827                                           ctx, delete, &err);
1828                security_xfrm_policy_free(ctx);
1829        }
1830        if (xp == NULL)
1831                return -ENOENT;
1832
1833        if (!delete) {
1834                struct sk_buff *resp_skb;
1835
1836                resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1837                if (IS_ERR(resp_skb)) {
1838                        err = PTR_ERR(resp_skb);
1839                } else {
1840                        err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1841                                            NETLINK_CB(skb).portid);
1842                }
1843        } else {
1844                xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1845
1846                if (err != 0)
1847                        goto out;
1848
1849                c.data.byid = p->index;
1850                c.event = nlh->nlmsg_type;
1851                c.seq = nlh->nlmsg_seq;
1852                c.portid = nlh->nlmsg_pid;
1853                km_policy_notify(xp, p->dir, &c);
1854        }
1855
1856out:
1857        xfrm_pol_put(xp);
1858        return err;
1859}
1860
1861static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1862                struct nlattr **attrs)
1863{
1864        struct net *net = sock_net(skb->sk);
1865        struct km_event c;
1866        struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1867        int err;
1868
1869        err = xfrm_state_flush(net, p->proto, true);
1870        if (err) {
1871                if (err == -ESRCH) /* empty table */
1872                        return 0;
1873                return err;
1874        }
1875        c.data.proto = p->proto;
1876        c.event = nlh->nlmsg_type;
1877        c.seq = nlh->nlmsg_seq;
1878        c.portid = nlh->nlmsg_pid;
1879        c.net = net;
1880        km_state_notify(NULL, &c);
1881
1882        return 0;
1883}
1884
1885static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
1886{
1887        unsigned int replay_size = x->replay_esn ?
1888                              xfrm_replay_state_esn_len(x->replay_esn) :
1889                              sizeof(struct xfrm_replay_state);
1890
1891        return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1892               + nla_total_size(replay_size)
1893               + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
1894               + nla_total_size(sizeof(struct xfrm_mark))
1895               + nla_total_size(4) /* XFRM_AE_RTHR */
1896               + nla_total_size(4); /* XFRM_AE_ETHR */
1897}
1898
1899static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1900{
1901        struct xfrm_aevent_id *id;
1902        struct nlmsghdr *nlh;
1903        int err;
1904
1905        nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1906        if (nlh == NULL)
1907                return -EMSGSIZE;
1908
1909        id = nlmsg_data(nlh);
1910        memset(&id->sa_id, 0, sizeof(id->sa_id));
1911        memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1912        id->sa_id.spi = x->id.spi;
1913        id->sa_id.family = x->props.family;
1914        id->sa_id.proto = x->id.proto;
1915        memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1916        id->reqid = x->props.reqid;
1917        id->flags = c->data.aevent;
1918
1919        if (x->replay_esn) {
1920                err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1921                              xfrm_replay_state_esn_len(x->replay_esn),
1922                              x->replay_esn);
1923        } else {
1924                err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1925                              &x->replay);
1926        }
1927        if (err)
1928                goto out_cancel;
1929        err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1930                            XFRMA_PAD);
1931        if (err)
1932                goto out_cancel;
1933
1934        if (id->flags & XFRM_AE_RTHR) {
1935                err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1936                if (err)
1937                        goto out_cancel;
1938        }
1939        if (id->flags & XFRM_AE_ETHR) {
1940                err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1941                                  x->replay_maxage * 10 / HZ);
1942                if (err)
1943                        goto out_cancel;
1944        }
1945        err = xfrm_mark_put(skb, &x->mark);
1946        if (err)
1947                goto out_cancel;
1948
1949        nlmsg_end(skb, nlh);
1950        return 0;
1951
1952out_cancel:
1953        nlmsg_cancel(skb, nlh);
1954        return err;
1955}
1956
1957static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1958                struct nlattr **attrs)
1959{
1960        struct net *net = sock_net(skb->sk);
1961        struct xfrm_state *x;
1962        struct sk_buff *r_skb;
1963        int err;
1964        struct km_event c;
1965        u32 mark;
1966        struct xfrm_mark m;
1967        struct xfrm_aevent_id *p = nlmsg_data(nlh);
1968        struct xfrm_usersa_id *id = &p->sa_id;
1969
1970        mark = xfrm_mark_get(attrs, &m);
1971
1972        x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1973        if (x == NULL)
1974                return -ESRCH;
1975
1976        r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1977        if (r_skb == NULL) {
1978                xfrm_state_put(x);
1979                return -ENOMEM;
1980        }
1981
1982        /*
1983         * XXX: is this lock really needed - none of the other
1984         * gets lock (the concern is things getting updated
1985         * while we are still reading) - jhs
1986        */
1987        spin_lock_bh(&x->lock);
1988        c.data.aevent = p->flags;
1989        c.seq = nlh->nlmsg_seq;
1990        c.portid = nlh->nlmsg_pid;
1991
1992        err = build_aevent(r_skb, x, &c);
1993        BUG_ON(err < 0);
1994
1995        err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1996        spin_unlock_bh(&x->lock);
1997        xfrm_state_put(x);
1998        return err;
1999}
2000
2001static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2002                struct nlattr **attrs)
2003{
2004        struct net *net = sock_net(skb->sk);
2005        struct xfrm_state *x;
2006        struct km_event c;
2007        int err = -EINVAL;
2008        u32 mark = 0;
2009        struct xfrm_mark m;
2010        struct xfrm_aevent_id *p = nlmsg_data(nlh);
2011        struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2012        struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2013        struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2014        struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2015        struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2016
2017        if (!lt && !rp && !re && !et && !rt)
2018                return err;
2019
2020        /* pedantic mode - thou shalt sayeth replaceth */
2021        if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2022                return err;
2023
2024        mark = xfrm_mark_get(attrs, &m);
2025
2026        x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2027        if (x == NULL)
2028                return -ESRCH;
2029
2030        if (x->km.state != XFRM_STATE_VALID)
2031                goto out;
2032
2033        err = xfrm_replay_verify_len(x->replay_esn, re);
2034        if (err)
2035                goto out;
2036
2037        spin_lock_bh(&x->lock);
2038        xfrm_update_ae_params(x, attrs, 1);
2039        spin_unlock_bh(&x->lock);
2040
2041        c.event = nlh->nlmsg_type;
2042        c.seq = nlh->nlmsg_seq;
2043        c.portid = nlh->nlmsg_pid;
2044        c.data.aevent = XFRM_AE_CU;
2045        km_state_notify(x, &c);
2046        err = 0;
2047out:
2048        xfrm_state_put(x);
2049        return err;
2050}
2051
2052static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2053                struct nlattr **attrs)
2054{
2055        struct net *net = sock_net(skb->sk);
2056        struct km_event c;
2057        u8 type = XFRM_POLICY_TYPE_MAIN;
2058        int err;
2059
2060        err = copy_from_user_policy_type(&type, attrs);
2061        if (err)
2062                return err;
2063
2064        err = xfrm_policy_flush(net, type, true);
2065        if (err) {
2066                if (err == -ESRCH) /* empty table */
2067                        return 0;
2068                return err;
2069        }
2070
2071        c.data.type = type;
2072        c.event = nlh->nlmsg_type;
2073        c.seq = nlh->nlmsg_seq;
2074        c.portid = nlh->nlmsg_pid;
2075        c.net = net;
2076        km_policy_notify(NULL, 0, &c);
2077        return 0;
2078}
2079
2080static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2081                struct nlattr **attrs)
2082{
2083        struct net *net = sock_net(skb->sk);
2084        struct xfrm_policy *xp;
2085        struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2086        struct xfrm_userpolicy_info *p = &up->pol;
2087        u8 type = XFRM_POLICY_TYPE_MAIN;
2088        int err = -ENOENT;
2089        struct xfrm_mark m;
2090        u32 mark = xfrm_mark_get(attrs, &m);
2091
2092        err = copy_from_user_policy_type(&type, attrs);
2093        if (err)
2094                return err;
2095
2096        err = verify_policy_dir(p->dir);
2097        if (err)
2098                return err;
2099
2100        if (p->index)
2101                xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
2102        else {
2103                struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2104                struct xfrm_sec_ctx *ctx;
2105
2106                err = verify_sec_ctx_len(attrs);
2107                if (err)
2108                        return err;
2109
2110                ctx = NULL;
2111                if (rt) {
2112                        struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2113
2114                        err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2115                        if (err)
2116                                return err;
2117                }
2118                xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
2119                                           &p->sel, ctx, 0, &err);
2120                security_xfrm_policy_free(ctx);
2121        }
2122        if (xp == NULL)
2123                return -ENOENT;
2124
2125        if (unlikely(xp->walk.dead))
2126                goto out;
2127
2128        err = 0;
2129        if (up->hard) {
2130                xfrm_policy_delete(xp, p->dir);
2131                xfrm_audit_policy_delete(xp, 1, true);
2132        }
2133        km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2134
2135out:
2136        xfrm_pol_put(xp);
2137        return err;
2138}
2139
2140static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2141                struct nlattr **attrs)
2142{
2143        struct net *net = sock_net(skb->sk);
2144        struct xfrm_state *x;
2145        int err;
2146        struct xfrm_user_expire *ue = nlmsg_data(nlh);
2147        struct xfrm_usersa_info *p = &ue->state;
2148        struct xfrm_mark m;
2149        u32 mark = xfrm_mark_get(attrs, &m);
2150
2151        x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2152
2153        err = -ENOENT;
2154        if (x == NULL)
2155                return err;
2156
2157        spin_lock_bh(&x->lock);
2158        err = -EINVAL;
2159        if (x->km.state != XFRM_STATE_VALID)
2160                goto out;
2161        km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2162
2163        if (ue->hard) {
2164                __xfrm_state_delete(x);
2165                xfrm_audit_state_delete(x, 1, true);
2166        }
2167        err = 0;
2168out:
2169        spin_unlock_bh(&x->lock);
2170        xfrm_state_put(x);
2171        return err;
2172}
2173
2174static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2175                struct nlattr **attrs)
2176{
2177        struct net *net = sock_net(skb->sk);
2178        struct xfrm_policy *xp;
2179        struct xfrm_user_tmpl *ut;
2180        int i;
2181        struct nlattr *rt = attrs[XFRMA_TMPL];
2182        struct xfrm_mark mark;
2183
2184        struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2185        struct xfrm_state *x = xfrm_state_alloc(net);
2186        int err = -ENOMEM;
2187
2188        if (!x)
2189                goto nomem;
2190
2191        xfrm_mark_get(attrs, &mark);
2192
2193        err = verify_newpolicy_info(&ua->policy);
2194        if (err)
2195                goto free_state;
2196
2197        /*   build an XP */
2198        xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2199        if (!xp)
2200                goto free_state;
2201
2202        memcpy(&x->id, &ua->id, sizeof(ua->id));
2203        memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2204        memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2205        xp->mark.m = x->mark.m = mark.m;
2206        xp->mark.v = x->mark.v = mark.v;
2207        ut = nla_data(rt);
2208        /* extract the templates and for each call km_key */
2209        for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2210                struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2211                memcpy(&x->id, &t->id, sizeof(x->id));
2212                x->props.mode = t->mode;
2213                x->props.reqid = t->reqid;
2214                x->props.family = ut->family;
2215                t->aalgos = ua->aalgos;
2216                t->ealgos = ua->ealgos;
2217                t->calgos = ua->calgos;
2218                err = km_query(x, t, xp);
2219
2220        }
2221
2222        kfree(x);
2223        kfree(xp);
2224
2225        return 0;
2226
2227free_state:
2228        kfree(x);
2229nomem:
2230        return err;
2231}
2232
2233#ifdef CONFIG_XFRM_MIGRATE
2234static int copy_from_user_migrate(struct xfrm_migrate *ma,
2235                                  struct xfrm_kmaddress *k,
2236                                  struct nlattr **attrs, int *num)
2237{
2238        struct nlattr *rt = attrs[XFRMA_MIGRATE];
2239        struct xfrm_user_migrate *um;
2240        int i, num_migrate;
2241
2242        if (k != NULL) {
2243                struct xfrm_user_kmaddress *uk;
2244
2245                uk = nla_data(attrs[XFRMA_KMADDRESS]);
2246                memcpy(&k->local, &uk->local, sizeof(k->local));
2247                memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2248                k->family = uk->family;
2249                k->reserved = uk->reserved;
2250        }
2251
2252        um = nla_data(rt);
2253        num_migrate = nla_len(rt) / sizeof(*um);
2254
2255        if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2256                return -EINVAL;
2257
2258        for (i = 0; i < num_migrate; i++, um++, ma++) {
2259                memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2260                memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2261                memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2262                memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2263
2264                ma->proto = um->proto;
2265                ma->mode = um->mode;
2266                ma->reqid = um->reqid;
2267
2268                ma->old_family = um->old_family;
2269                ma->new_family = um->new_family;
2270        }
2271
2272        *num = i;
2273        return 0;
2274}
2275
2276static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2277                           struct nlattr **attrs)
2278{
2279        struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2280        struct xfrm_migrate m[XFRM_MAX_DEPTH];
2281        struct xfrm_kmaddress km, *kmp;
2282        u8 type;
2283        int err;
2284        int n = 0;
2285        struct net *net = sock_net(skb->sk);
2286        struct xfrm_encap_tmpl  *encap = NULL;
2287
2288        if (attrs[XFRMA_MIGRATE] == NULL)
2289                return -EINVAL;
2290
2291        kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2292
2293        err = copy_from_user_policy_type(&type, attrs);
2294        if (err)
2295                return err;
2296
2297        err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2298        if (err)
2299                return err;
2300
2301        if (!n)
2302                return 0;
2303
2304        if (attrs[XFRMA_ENCAP]) {
2305                encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2306                                sizeof(*encap), GFP_KERNEL);
2307                if (!encap)
2308                        return 0;
2309        }
2310
2311        err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
2312
2313        kfree(encap);
2314
2315        return err;
2316}
2317#else
2318static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2319                           struct nlattr **attrs)
2320{
2321        return -ENOPROTOOPT;
2322}
2323#endif
2324
2325#ifdef CONFIG_XFRM_MIGRATE
2326static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2327{
2328        struct xfrm_user_migrate um;
2329
2330        memset(&um, 0, sizeof(um));
2331        um.proto = m->proto;
2332        um.mode = m->mode;
2333        um.reqid = m->reqid;
2334        um.old_family = m->old_family;
2335        memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2336        memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2337        um.new_family = m->new_family;
2338        memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2339        memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2340
2341        return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2342}
2343
2344static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2345{
2346        struct xfrm_user_kmaddress uk;
2347
2348        memset(&uk, 0, sizeof(uk));
2349        uk.family = k->family;
2350        uk.reserved = k->reserved;
2351        memcpy(&uk.local, &k->local, sizeof(uk.local));
2352        memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2353
2354        return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2355}
2356
2357static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2358                                                int with_encp)
2359{
2360        return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2361              + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2362              + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2363              + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2364              + userpolicy_type_attrsize();
2365}
2366
2367static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2368                         int num_migrate, const struct xfrm_kmaddress *k,
2369                         const struct xfrm_selector *sel,
2370                         const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2371{
2372        const struct xfrm_migrate *mp;
2373        struct xfrm_userpolicy_id *pol_id;
2374        struct nlmsghdr *nlh;
2375        int i, err;
2376
2377        nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2378        if (nlh == NULL)
2379                return -EMSGSIZE;
2380
2381        pol_id = nlmsg_data(nlh);
2382        /* copy data from selector, dir, and type to the pol_id */
2383        memset(pol_id, 0, sizeof(*pol_id));
2384        memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2385        pol_id->dir = dir;
2386
2387        if (k != NULL) {
2388                err = copy_to_user_kmaddress(k, skb);
2389                if (err)
2390                        goto out_cancel;
2391        }
2392        if (encap) {
2393                err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2394                if (err)
2395                        goto out_cancel;
2396        }
2397        err = copy_to_user_policy_type(type, skb);
2398        if (err)
2399                goto out_cancel;
2400        for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2401                err = copy_to_user_migrate(mp, skb);
2402                if (err)
2403                        goto out_cancel;
2404        }
2405
2406        nlmsg_end(skb, nlh);
2407        return 0;
2408
2409out_cancel:
2410        nlmsg_cancel(skb, nlh);
2411        return err;
2412}
2413
2414static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2415                             const struct xfrm_migrate *m, int num_migrate,
2416                             const struct xfrm_kmaddress *k,
2417                             const struct xfrm_encap_tmpl *encap)
2418{
2419        struct net *net = &init_net;
2420        struct sk_buff *skb;
2421        int err;
2422
2423        skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2424                        GFP_ATOMIC);
2425        if (skb == NULL)
2426                return -ENOMEM;
2427
2428        /* build migrate */
2429        err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2430        BUG_ON(err < 0);
2431
2432        return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2433}
2434#else
2435static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2436                             const struct xfrm_migrate *m, int num_migrate,
2437                             const struct xfrm_kmaddress *k,
2438                             const struct xfrm_encap_tmpl *encap)
2439{
2440        return -ENOPROTOOPT;
2441}
2442#endif
2443
2444#define XMSGSIZE(type) sizeof(struct type)
2445
2446static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2447        [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2448        [XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2449        [XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2450        [XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2451        [XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2452        [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2453        [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2454        [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2455        [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2456        [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2457        [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2458        [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2459        [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2460        [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2461        [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2462        [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2463        [XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2464        [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2465        [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2466        [XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2467        [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2468};
2469
2470#undef XMSGSIZE
2471
2472static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2473        [XFRMA_SA]              = { .len = sizeof(struct xfrm_usersa_info)},
2474        [XFRMA_POLICY]          = { .len = sizeof(struct xfrm_userpolicy_info)},
2475        [XFRMA_LASTUSED]        = { .type = NLA_U64},
2476        [XFRMA_ALG_AUTH_TRUNC]  = { .len = sizeof(struct xfrm_algo_auth)},
2477        [XFRMA_ALG_AEAD]        = { .len = sizeof(struct xfrm_algo_aead) },
2478        [XFRMA_ALG_AUTH]        = { .len = sizeof(struct xfrm_algo) },
2479        [XFRMA_ALG_CRYPT]       = { .len = sizeof(struct xfrm_algo) },
2480        [XFRMA_ALG_COMP]        = { .len = sizeof(struct xfrm_algo) },
2481        [XFRMA_ENCAP]           = { .len = sizeof(struct xfrm_encap_tmpl) },
2482        [XFRMA_TMPL]            = { .len = sizeof(struct xfrm_user_tmpl) },
2483        [XFRMA_SEC_CTX]         = { .len = sizeof(struct xfrm_sec_ctx) },
2484        [XFRMA_LTIME_VAL]       = { .len = sizeof(struct xfrm_lifetime_cur) },
2485        [XFRMA_REPLAY_VAL]      = { .len = sizeof(struct xfrm_replay_state) },
2486        [XFRMA_REPLAY_THRESH]   = { .type = NLA_U32 },
2487        [XFRMA_ETIMER_THRESH]   = { .type = NLA_U32 },
2488        [XFRMA_SRCADDR]         = { .len = sizeof(xfrm_address_t) },
2489        [XFRMA_COADDR]          = { .len = sizeof(xfrm_address_t) },
2490        [XFRMA_POLICY_TYPE]     = { .len = sizeof(struct xfrm_userpolicy_type)},
2491        [XFRMA_MIGRATE]         = { .len = sizeof(struct xfrm_user_migrate) },
2492        [XFRMA_KMADDRESS]       = { .len = sizeof(struct xfrm_user_kmaddress) },
2493        [XFRMA_MARK]            = { .len = sizeof(struct xfrm_mark) },
2494        [XFRMA_TFCPAD]          = { .type = NLA_U32 },
2495        [XFRMA_REPLAY_ESN_VAL]  = { .len = sizeof(struct xfrm_replay_state_esn) },
2496        [XFRMA_SA_EXTRA_FLAGS]  = { .type = NLA_U32 },
2497        [XFRMA_PROTO]           = { .type = NLA_U8 },
2498        [XFRMA_ADDRESS_FILTER]  = { .len = sizeof(struct xfrm_address_filter) },
2499        [XFRMA_OFFLOAD_DEV]     = { .len = sizeof(struct xfrm_user_offload) },
2500        [XFRMA_OUTPUT_MARK]     = { .type = NLA_U32 },
2501};
2502
2503static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2504        [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2505        [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2506};
2507
2508static const struct xfrm_link {
2509        int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2510        int (*start)(struct netlink_callback *);
2511        int (*dump)(struct sk_buff *, struct netlink_callback *);
2512        int (*done)(struct netlink_callback *);
2513        const struct nla_policy *nla_pol;
2514        int nla_max;
2515} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2516        [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2517        [XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2518        [XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2519                                                   .dump = xfrm_dump_sa,
2520                                                   .done = xfrm_dump_sa_done  },
2521        [XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2522        [XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2523        [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2524                                                   .start = xfrm_dump_policy_start,
2525                                                   .dump = xfrm_dump_policy,
2526                                                   .done = xfrm_dump_policy_done },
2527        [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2528        [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
2529        [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2530        [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2531        [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2532        [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2533        [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2534        [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2535        [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2536        [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
2537        [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
2538        [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2539        [XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2540                                                   .nla_pol = xfrma_spd_policy,
2541                                                   .nla_max = XFRMA_SPD_MAX },
2542        [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
2543};
2544
2545static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2546                             struct netlink_ext_ack *extack)
2547{
2548        struct net *net = sock_net(skb->sk);
2549        struct nlattr *attrs[XFRMA_MAX+1];
2550        const struct xfrm_link *link;
2551        int type, err;
2552
2553#ifdef CONFIG_COMPAT
2554        if (in_compat_syscall())
2555                return -EOPNOTSUPP;
2556#endif
2557
2558        type = nlh->nlmsg_type;
2559        if (type > XFRM_MSG_MAX)
2560                return -EINVAL;
2561
2562        type -= XFRM_MSG_BASE;
2563        link = &xfrm_dispatch[type];
2564
2565        /* All operations require privileges, even GET */
2566        if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2567                return -EPERM;
2568
2569        if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2570             type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2571            (nlh->nlmsg_flags & NLM_F_DUMP)) {
2572                if (link->dump == NULL)
2573                        return -EINVAL;
2574
2575                {
2576                        struct netlink_dump_control c = {
2577                                .start = link->start,
2578                                .dump = link->dump,
2579                                .done = link->done,
2580                        };
2581                        return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2582                }
2583        }
2584
2585        err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2586                          link->nla_max ? : XFRMA_MAX,
2587                          link->nla_pol ? : xfrma_policy, extack);
2588        if (err < 0)
2589                return err;
2590
2591        if (link->doit == NULL)
2592                return -EINVAL;
2593
2594        return link->doit(skb, nlh, attrs);
2595}
2596
2597static void xfrm_netlink_rcv(struct sk_buff *skb)
2598{
2599        struct net *net = sock_net(skb->sk);
2600
2601        mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2602        netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2603        mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2604}
2605
2606static inline unsigned int xfrm_expire_msgsize(void)
2607{
2608        return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2609               + nla_total_size(sizeof(struct xfrm_mark));
2610}
2611
2612static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2613{
2614        struct xfrm_user_expire *ue;
2615        struct nlmsghdr *nlh;
2616        int err;
2617
2618        nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2619        if (nlh == NULL)
2620                return -EMSGSIZE;
2621
2622        ue = nlmsg_data(nlh);
2623        copy_to_user_state(x, &ue->state);
2624        ue->hard = (c->data.hard != 0) ? 1 : 0;
2625        /* clear the padding bytes */
2626        memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
2627
2628        err = xfrm_mark_put(skb, &x->mark);
2629        if (err)
2630                return err;
2631
2632        nlmsg_end(skb, nlh);
2633        return 0;
2634}
2635
2636static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2637{
2638        struct net *net = xs_net(x);
2639        struct sk_buff *skb;
2640
2641        skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2642        if (skb == NULL)
2643                return -ENOMEM;
2644
2645        if (build_expire(skb, x, c) < 0) {
2646                kfree_skb(skb);
2647                return -EMSGSIZE;
2648        }
2649
2650        return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2651}
2652
2653static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2654{
2655        struct net *net = xs_net(x);
2656        struct sk_buff *skb;
2657        int err;
2658
2659        skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2660        if (skb == NULL)
2661                return -ENOMEM;
2662
2663        err = build_aevent(skb, x, c);
2664        BUG_ON(err < 0);
2665
2666        return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2667}
2668
2669static int xfrm_notify_sa_flush(const struct km_event *c)
2670{
2671        struct net *net = c->net;
2672        struct xfrm_usersa_flush *p;
2673        struct nlmsghdr *nlh;
2674        struct sk_buff *skb;
2675        int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2676
2677        skb = nlmsg_new(len, GFP_ATOMIC);
2678        if (skb == NULL)
2679                return -ENOMEM;
2680
2681        nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2682        if (nlh == NULL) {
2683                kfree_skb(skb);
2684                return -EMSGSIZE;
2685        }
2686
2687        p = nlmsg_data(nlh);
2688        p->proto = c->data.proto;
2689
2690        nlmsg_end(skb, nlh);
2691
2692        return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2693}
2694
2695static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
2696{
2697        unsigned int l = 0;
2698        if (x->aead)
2699                l += nla_total_size(aead_len(x->aead));
2700        if (x->aalg) {
2701                l += nla_total_size(sizeof(struct xfrm_algo) +
2702                                    (x->aalg->alg_key_len + 7) / 8);
2703                l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2704        }
2705        if (x->ealg)
2706                l += nla_total_size(xfrm_alg_len(x->ealg));
2707        if (x->calg)
2708                l += nla_total_size(sizeof(*x->calg));
2709        if (x->encap)
2710                l += nla_total_size(sizeof(*x->encap));
2711        if (x->tfcpad)
2712                l += nla_total_size(sizeof(x->tfcpad));
2713        if (x->replay_esn)
2714                l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2715        else
2716                l += nla_total_size(sizeof(struct xfrm_replay_state));
2717        if (x->security)
2718                l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2719                                    x->security->ctx_len);
2720        if (x->coaddr)
2721                l += nla_total_size(sizeof(*x->coaddr));
2722        if (x->props.extra_flags)
2723                l += nla_total_size(sizeof(x->props.extra_flags));
2724        if (x->xso.dev)
2725                 l += nla_total_size(sizeof(x->xso));
2726        if (x->props.output_mark)
2727                l += nla_total_size(sizeof(x->props.output_mark));
2728
2729        /* Must count x->lastused as it may become non-zero behind our back. */
2730        l += nla_total_size_64bit(sizeof(u64));
2731
2732        return l;
2733}
2734
2735static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2736{
2737        struct net *net = xs_net(x);
2738        struct xfrm_usersa_info *p;
2739        struct xfrm_usersa_id *id;
2740        struct nlmsghdr *nlh;
2741        struct sk_buff *skb;
2742        unsigned int len = xfrm_sa_len(x);
2743        unsigned int headlen;
2744        int err;
2745
2746        headlen = sizeof(*p);
2747        if (c->event == XFRM_MSG_DELSA) {
2748                len += nla_total_size(headlen);
2749                headlen = sizeof(*id);
2750                len += nla_total_size(sizeof(struct xfrm_mark));
2751        }
2752        len += NLMSG_ALIGN(headlen);
2753
2754        skb = nlmsg_new(len, GFP_ATOMIC);
2755        if (skb == NULL)
2756                return -ENOMEM;
2757
2758        nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2759        err = -EMSGSIZE;
2760        if (nlh == NULL)
2761                goto out_free_skb;
2762
2763        p = nlmsg_data(nlh);
2764        if (c->event == XFRM_MSG_DELSA) {
2765                struct nlattr *attr;
2766
2767                id = nlmsg_data(nlh);
2768                memset(id, 0, sizeof(*id));
2769                memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2770                id->spi = x->id.spi;
2771                id->family = x->props.family;
2772                id->proto = x->id.proto;
2773
2774                attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2775                err = -EMSGSIZE;
2776                if (attr == NULL)
2777                        goto out_free_skb;
2778
2779                p = nla_data(attr);
2780        }
2781        err = copy_to_user_state_extra(x, p, skb);
2782        if (err)
2783                goto out_free_skb;
2784
2785        nlmsg_end(skb, nlh);
2786
2787        return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2788
2789out_free_skb:
2790        kfree_skb(skb);
2791        return err;
2792}
2793
2794static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2795{
2796
2797        switch (c->event) {
2798        case XFRM_MSG_EXPIRE:
2799                return xfrm_exp_state_notify(x, c);
2800        case XFRM_MSG_NEWAE:
2801                return xfrm_aevent_state_notify(x, c);
2802        case XFRM_MSG_DELSA:
2803        case XFRM_MSG_UPDSA:
2804        case XFRM_MSG_NEWSA:
2805                return xfrm_notify_sa(x, c);
2806        case XFRM_MSG_FLUSHSA:
2807                return xfrm_notify_sa_flush(c);
2808        default:
2809                printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2810                       c->event);
2811                break;
2812        }
2813
2814        return 0;
2815
2816}
2817
2818static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
2819                                                struct xfrm_policy *xp)
2820{
2821        return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2822               + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2823               + nla_total_size(sizeof(struct xfrm_mark))
2824               + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2825               + userpolicy_type_attrsize();
2826}
2827
2828static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2829                         struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2830{
2831        __u32 seq = xfrm_get_acqseq();
2832        struct xfrm_user_acquire *ua;
2833        struct nlmsghdr *nlh;
2834        int err;
2835
2836        nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2837        if (nlh == NULL)
2838                return -EMSGSIZE;
2839
2840        ua = nlmsg_data(nlh);
2841        memcpy(&ua->id, &x->id, sizeof(ua->id));
2842        memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2843        memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2844        copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2845        ua->aalgos = xt->aalgos;
2846        ua->ealgos = xt->ealgos;
2847        ua->calgos = xt->calgos;
2848        ua->seq = x->km.seq = seq;
2849
2850        err = copy_to_user_tmpl(xp, skb);
2851        if (!err)
2852                err = copy_to_user_state_sec_ctx(x, skb);
2853        if (!err)
2854                err = copy_to_user_policy_type(xp->type, skb);
2855        if (!err)
2856                err = xfrm_mark_put(skb, &xp->mark);
2857        if (err) {
2858                nlmsg_cancel(skb, nlh);
2859                return err;
2860        }
2861
2862        nlmsg_end(skb, nlh);
2863        return 0;
2864}
2865
2866static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2867                             struct xfrm_policy *xp)
2868{
2869        struct net *net = xs_net(x);
2870        struct sk_buff *skb;
2871        int err;
2872
2873        skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2874        if (skb == NULL)
2875                return -ENOMEM;
2876
2877        err = build_acquire(skb, x, xt, xp);
2878        BUG_ON(err < 0);
2879
2880        return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2881}
2882
2883/* User gives us xfrm_user_policy_info followed by an array of 0
2884 * or more templates.
2885 */
2886static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2887                                               u8 *data, int len, int *dir)
2888{
2889        struct net *net = sock_net(sk);
2890        struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2891        struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2892        struct xfrm_policy *xp;
2893        int nr;
2894
2895        switch (sk->sk_family) {
2896        case AF_INET:
2897                if (opt != IP_XFRM_POLICY) {
2898                        *dir = -EOPNOTSUPP;
2899                        return NULL;
2900                }
2901                break;
2902#if IS_ENABLED(CONFIG_IPV6)
2903        case AF_INET6:
2904                if (opt != IPV6_XFRM_POLICY) {
2905                        *dir = -EOPNOTSUPP;
2906                        return NULL;
2907                }
2908                break;
2909#endif
2910        default:
2911                *dir = -EINVAL;
2912                return NULL;
2913        }
2914
2915        *dir = -EINVAL;
2916
2917        if (len < sizeof(*p) ||
2918            verify_newpolicy_info(p))
2919                return NULL;
2920
2921        nr = ((len - sizeof(*p)) / sizeof(*ut));
2922        if (validate_tmpl(nr, ut, p->sel.family))
2923                return NULL;
2924
2925        if (p->dir > XFRM_POLICY_OUT)
2926                return NULL;
2927
2928        xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2929        if (xp == NULL) {
2930                *dir = -ENOBUFS;
2931                return NULL;
2932        }
2933
2934        copy_from_user_policy(xp, p);
2935        xp->type = XFRM_POLICY_TYPE_MAIN;
2936        copy_templates(xp, ut, nr);
2937
2938        *dir = p->dir;
2939
2940        return xp;
2941}
2942
2943static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2944{
2945        return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2946               + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2947               + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2948               + nla_total_size(sizeof(struct xfrm_mark))
2949               + userpolicy_type_attrsize();
2950}
2951
2952static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2953                           int dir, const struct km_event *c)
2954{
2955        struct xfrm_user_polexpire *upe;
2956        int hard = c->data.hard;
2957        struct nlmsghdr *nlh;
2958        int err;
2959
2960        nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2961        if (nlh == NULL)
2962                return -EMSGSIZE;
2963
2964        upe = nlmsg_data(nlh);
2965        copy_to_user_policy(xp, &upe->pol, dir);
2966        err = copy_to_user_tmpl(xp, skb);
2967        if (!err)
2968                err = copy_to_user_sec_ctx(xp, skb);
2969        if (!err)
2970                err = copy_to_user_policy_type(xp->type, skb);
2971        if (!err)
2972                err = xfrm_mark_put(skb, &xp->mark);
2973        if (err) {
2974                nlmsg_cancel(skb, nlh);
2975                return err;
2976        }
2977        upe->hard = !!hard;
2978
2979        nlmsg_end(skb, nlh);
2980        return 0;
2981}
2982
2983static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2984{
2985        struct net *net = xp_net(xp);
2986        struct sk_buff *skb;
2987        int err;
2988
2989        skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2990        if (skb == NULL)
2991                return -ENOMEM;
2992
2993        err = build_polexpire(skb, xp, dir, c);
2994        BUG_ON(err < 0);
2995
2996        return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2997}
2998
2999static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3000{
3001        unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3002        struct net *net = xp_net(xp);
3003        struct xfrm_userpolicy_info *p;
3004        struct xfrm_userpolicy_id *id;
3005        struct nlmsghdr *nlh;
3006        struct sk_buff *skb;
3007        unsigned int headlen;
3008        int err;
3009
3010        headlen = sizeof(*p);
3011        if (c->event == XFRM_MSG_DELPOLICY) {
3012                len += nla_total_size(headlen);
3013                headlen = sizeof(*id);
3014        }
3015        len += userpolicy_type_attrsize();
3016        len += nla_total_size(sizeof(struct xfrm_mark));
3017        len += NLMSG_ALIGN(headlen);
3018
3019        skb = nlmsg_new(len, GFP_ATOMIC);
3020        if (skb == NULL)
3021                return -ENOMEM;
3022
3023        nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3024        err = -EMSGSIZE;
3025        if (nlh == NULL)
3026                goto out_free_skb;
3027
3028        p = nlmsg_data(nlh);
3029        if (c->event == XFRM_MSG_DELPOLICY) {
3030                struct nlattr *attr;
3031
3032                id = nlmsg_data(nlh);
3033                memset(id, 0, sizeof(*id));
3034                id->dir = dir;
3035                if (c->data.byid)
3036                        id->index = xp->index;
3037                else
3038                        memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3039
3040                attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3041                err = -EMSGSIZE;
3042                if (attr == NULL)
3043                        goto out_free_skb;
3044
3045                p = nla_data(attr);
3046        }
3047
3048        copy_to_user_policy(xp, p, dir);
3049        err = copy_to_user_tmpl(xp, skb);
3050        if (!err)
3051                err = copy_to_user_policy_type(xp->type, skb);
3052        if (!err)
3053                err = xfrm_mark_put(skb, &xp->mark);
3054        if (err)
3055                goto out_free_skb;
3056
3057        nlmsg_end(skb, nlh);
3058
3059        return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3060
3061out_free_skb:
3062        kfree_skb(skb);
3063        return err;
3064}
3065
3066static int xfrm_notify_policy_flush(const struct km_event *c)
3067{
3068        struct net *net = c->net;
3069        struct nlmsghdr *nlh;
3070        struct sk_buff *skb;
3071        int err;
3072
3073        skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3074        if (skb == NULL)
3075                return -ENOMEM;
3076
3077        nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3078        err = -EMSGSIZE;
3079        if (nlh == NULL)
3080                goto out_free_skb;
3081        err = copy_to_user_policy_type(c->data.type, skb);
3082        if (err)
3083                goto out_free_skb;
3084
3085        nlmsg_end(skb, nlh);
3086
3087        return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3088
3089out_free_skb:
3090        kfree_skb(skb);
3091        return err;
3092}
3093
3094static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3095{
3096
3097        switch (c->event) {
3098        case XFRM_MSG_NEWPOLICY:
3099        case XFRM_MSG_UPDPOLICY:
3100        case XFRM_MSG_DELPOLICY:
3101                return xfrm_notify_policy(xp, dir, c);
3102        case XFRM_MSG_FLUSHPOLICY:
3103                return xfrm_notify_policy_flush(c);
3104        case XFRM_MSG_POLEXPIRE:
3105                return xfrm_exp_policy_notify(xp, dir, c);
3106        default:
3107                printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3108                       c->event);
3109        }
3110
3111        return 0;
3112
3113}
3114
3115static inline unsigned int xfrm_report_msgsize(void)
3116{
3117        return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3118}
3119
3120static int build_report(struct sk_buff *skb, u8 proto,
3121                        struct xfrm_selector *sel, xfrm_address_t *addr)
3122{
3123        struct xfrm_user_report *ur;
3124        struct nlmsghdr *nlh;
3125
3126        nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3127        if (nlh == NULL)
3128                return -EMSGSIZE;
3129
3130        ur = nlmsg_data(nlh);
3131        ur->proto = proto;
3132        memcpy(&ur->sel, sel, sizeof(ur->sel));
3133
3134        if (addr) {
3135                int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3136                if (err) {
3137                        nlmsg_cancel(skb, nlh);
3138                        return err;
3139                }
3140        }
3141        nlmsg_end(skb, nlh);
3142        return 0;
3143}
3144
3145static int xfrm_send_report(struct net *net, u8 proto,
3146                            struct xfrm_selector *sel, xfrm_address_t *addr)
3147{
3148        struct sk_buff *skb;
3149        int err;
3150
3151        skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3152        if (skb == NULL)
3153                return -ENOMEM;
3154
3155        err = build_report(skb, proto, sel, addr);
3156        BUG_ON(err < 0);
3157
3158        return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3159}
3160
3161static inline unsigned int xfrm_mapping_msgsize(void)
3162{
3163        return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3164}
3165
3166static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3167                         xfrm_address_t *new_saddr, __be16 new_sport)
3168{
3169        struct xfrm_user_mapping *um;
3170        struct nlmsghdr *nlh;
3171
3172        nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3173        if (nlh == NULL)
3174                return -EMSGSIZE;
3175
3176        um = nlmsg_data(nlh);
3177
3178        memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3179        um->id.spi = x->id.spi;
3180        um->id.family = x->props.family;
3181        um->id.proto = x->id.proto;
3182        memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3183        memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3184        um->new_sport = new_sport;
3185        um->old_sport = x->encap->encap_sport;
3186        um->reqid = x->props.reqid;
3187
3188        nlmsg_end(skb, nlh);
3189        return 0;
3190}
3191
3192static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3193                             __be16 sport)
3194{
3195        struct net *net = xs_net(x);
3196        struct sk_buff *skb;
3197        int err;
3198
3199        if (x->id.proto != IPPROTO_ESP)
3200                return -EINVAL;
3201
3202        if (!x->encap)
3203                return -EINVAL;
3204
3205        skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3206        if (skb == NULL)
3207                return -ENOMEM;
3208
3209        err = build_mapping(skb, x, ipaddr, sport);
3210        BUG_ON(err < 0);
3211
3212        return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3213}
3214
3215static bool xfrm_is_alive(const struct km_event *c)
3216{
3217        return (bool)xfrm_acquire_is_on(c->net);
3218}
3219
3220static struct xfrm_mgr netlink_mgr = {
3221        .notify         = xfrm_send_state_notify,
3222        .acquire        = xfrm_send_acquire,
3223        .compile_policy = xfrm_compile_policy,
3224        .notify_policy  = xfrm_send_policy_notify,
3225        .report         = xfrm_send_report,
3226        .migrate        = xfrm_send_migrate,
3227        .new_mapping    = xfrm_send_mapping,
3228        .is_alive       = xfrm_is_alive,
3229};
3230
3231static int __net_init xfrm_user_net_init(struct net *net)
3232{
3233        struct sock *nlsk;
3234        struct netlink_kernel_cfg cfg = {
3235                .groups = XFRMNLGRP_MAX,
3236                .input  = xfrm_netlink_rcv,
3237        };
3238
3239        nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3240        if (nlsk == NULL)
3241                return -ENOMEM;
3242        net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3243        rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3244        return 0;
3245}
3246
3247static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3248{
3249        struct net *net;
3250        list_for_each_entry(net, net_exit_list, exit_list)
3251                RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3252        synchronize_net();
3253        list_for_each_entry(net, net_exit_list, exit_list)
3254                netlink_kernel_release(net->xfrm.nlsk_stash);
3255}
3256
3257static struct pernet_operations xfrm_user_net_ops = {
3258        .init       = xfrm_user_net_init,
3259        .exit_batch = xfrm_user_net_exit,
3260};
3261
3262static int __init xfrm_user_init(void)
3263{
3264        int rv;
3265
3266        printk(KERN_INFO "Initializing XFRM netlink socket\n");
3267
3268        rv = register_pernet_subsys(&xfrm_user_net_ops);
3269        if (rv < 0)
3270                return rv;
3271        rv = xfrm_register_km(&netlink_mgr);
3272        if (rv < 0)
3273                unregister_pernet_subsys(&xfrm_user_net_ops);
3274        return rv;
3275}
3276
3277static void __exit xfrm_user_exit(void)
3278{
3279        xfrm_unregister_km(&netlink_mgr);
3280        unregister_pernet_subsys(&xfrm_user_net_ops);
3281}
3282
3283module_init(xfrm_user_init);
3284module_exit(xfrm_user_exit);
3285MODULE_LICENSE("GPL");
3286MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3287
3288