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