11da177e4SLinus Torvalds /* xfrm_user.c: User interface to configure xfrm engine. 21da177e4SLinus Torvalds * 31da177e4SLinus Torvalds * Copyright (C) 2002 David S. Miller (davem@redhat.com) 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Changes: 61da177e4SLinus Torvalds * Mitsuru KANDA @USAGI 71da177e4SLinus Torvalds * Kazunori MIYAZAWA @USAGI 81da177e4SLinus Torvalds * Kunihiro Ishiguro <kunihiro@ipinfusion.com> 91da177e4SLinus Torvalds * IPv6 support 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 139409f38aSHerbert Xu #include <linux/crypto.h> 141da177e4SLinus Torvalds #include <linux/module.h> 151da177e4SLinus Torvalds #include <linux/kernel.h> 161da177e4SLinus Torvalds #include <linux/types.h> 171da177e4SLinus Torvalds #include <linux/slab.h> 181da177e4SLinus Torvalds #include <linux/socket.h> 191da177e4SLinus Torvalds #include <linux/string.h> 201da177e4SLinus Torvalds #include <linux/net.h> 211da177e4SLinus Torvalds #include <linux/skbuff.h> 221da177e4SLinus Torvalds #include <linux/pfkeyv2.h> 231da177e4SLinus Torvalds #include <linux/ipsec.h> 241da177e4SLinus Torvalds #include <linux/init.h> 251da177e4SLinus Torvalds #include <linux/security.h> 261da177e4SLinus Torvalds #include <net/sock.h> 271da177e4SLinus Torvalds #include <net/xfrm.h> 2888fc2c84SThomas Graf #include <net/netlink.h> 29fa6dd8a2SNicolas Dichtel #include <net/ah.h> 301da177e4SLinus Torvalds #include <asm/uaccess.h> 31dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 32e23c7194SMasahide NAKAMURA #include <linux/in6.h> 33e23c7194SMasahide NAKAMURA #endif 341da177e4SLinus Torvalds 355424f32eSThomas Graf static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type) 361da177e4SLinus Torvalds { 375424f32eSThomas Graf struct nlattr *rt = attrs[type]; 381da177e4SLinus Torvalds struct xfrm_algo *algp; 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds if (!rt) 411da177e4SLinus Torvalds return 0; 421da177e4SLinus Torvalds 435424f32eSThomas Graf algp = nla_data(rt); 440f99be0dSEric Dumazet if (nla_len(rt) < xfrm_alg_len(algp)) 4531c26852SHerbert Xu return -EINVAL; 4631c26852SHerbert Xu 471da177e4SLinus Torvalds switch (type) { 481da177e4SLinus Torvalds case XFRMA_ALG_AUTH: 491da177e4SLinus Torvalds case XFRMA_ALG_CRYPT: 501da177e4SLinus Torvalds case XFRMA_ALG_COMP: 511da177e4SLinus Torvalds break; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds default: 541da177e4SLinus Torvalds return -EINVAL; 553ff50b79SStephen Hemminger } 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; 581da177e4SLinus Torvalds return 0; 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds 614447bb33SMartin Willi static int verify_auth_trunc(struct nlattr **attrs) 624447bb33SMartin Willi { 634447bb33SMartin Willi struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC]; 644447bb33SMartin Willi struct xfrm_algo_auth *algp; 654447bb33SMartin Willi 664447bb33SMartin Willi if (!rt) 674447bb33SMartin Willi return 0; 684447bb33SMartin Willi 694447bb33SMartin Willi algp = nla_data(rt); 704447bb33SMartin Willi if (nla_len(rt) < xfrm_alg_auth_len(algp)) 714447bb33SMartin Willi return -EINVAL; 724447bb33SMartin Willi 734447bb33SMartin Willi algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; 744447bb33SMartin Willi return 0; 754447bb33SMartin Willi } 764447bb33SMartin Willi 771a6509d9SHerbert Xu static int verify_aead(struct nlattr **attrs) 781a6509d9SHerbert Xu { 791a6509d9SHerbert Xu struct nlattr *rt = attrs[XFRMA_ALG_AEAD]; 801a6509d9SHerbert Xu struct xfrm_algo_aead *algp; 811a6509d9SHerbert Xu 821a6509d9SHerbert Xu if (!rt) 831a6509d9SHerbert Xu return 0; 841a6509d9SHerbert Xu 851a6509d9SHerbert Xu algp = nla_data(rt); 861a6509d9SHerbert Xu if (nla_len(rt) < aead_len(algp)) 871a6509d9SHerbert Xu return -EINVAL; 881a6509d9SHerbert Xu 891a6509d9SHerbert Xu algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; 901a6509d9SHerbert Xu return 0; 911a6509d9SHerbert Xu } 921a6509d9SHerbert Xu 935424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type, 94eb2971b6SMasahide NAKAMURA xfrm_address_t **addrp) 95eb2971b6SMasahide NAKAMURA { 965424f32eSThomas Graf struct nlattr *rt = attrs[type]; 97eb2971b6SMasahide NAKAMURA 98cf5cb79fSThomas Graf if (rt && addrp) 995424f32eSThomas Graf *addrp = nla_data(rt); 100eb2971b6SMasahide NAKAMURA } 101df71837dSTrent Jaeger 1025424f32eSThomas Graf static inline int verify_sec_ctx_len(struct nlattr **attrs) 103df71837dSTrent Jaeger { 1045424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 105df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx; 106df71837dSTrent Jaeger 107df71837dSTrent Jaeger if (!rt) 108df71837dSTrent Jaeger return 0; 109df71837dSTrent Jaeger 1105424f32eSThomas Graf uctx = nla_data(rt); 111cf5cb79fSThomas Graf if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) 112df71837dSTrent Jaeger return -EINVAL; 113df71837dSTrent Jaeger 114df71837dSTrent Jaeger return 0; 115df71837dSTrent Jaeger } 116df71837dSTrent Jaeger 117d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p, 118d8647b79SSteffen Klassert struct nlattr **attrs) 119d8647b79SSteffen Klassert { 120d8647b79SSteffen Klassert struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL]; 121ecd79187SMathias Krause struct xfrm_replay_state_esn *rs; 122d8647b79SSteffen Klassert 123ecd79187SMathias Krause if (p->flags & XFRM_STATE_ESN) { 124ecd79187SMathias Krause if (!rt) 1257833aa05SSteffen Klassert return -EINVAL; 1267833aa05SSteffen Klassert 127ecd79187SMathias Krause rs = nla_data(rt); 128ecd79187SMathias Krause 129ecd79187SMathias Krause if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8) 130ecd79187SMathias Krause return -EINVAL; 131ecd79187SMathias Krause 132ecd79187SMathias Krause if (nla_len(rt) < xfrm_replay_state_esn_len(rs) && 133ecd79187SMathias Krause nla_len(rt) != sizeof(*rs)) 134ecd79187SMathias Krause return -EINVAL; 135ecd79187SMathias Krause } 136ecd79187SMathias Krause 137d8647b79SSteffen Klassert if (!rt) 138d8647b79SSteffen Klassert return 0; 139d8647b79SSteffen Klassert 14001714109SFan Du /* As only ESP and AH support ESN feature. */ 14101714109SFan Du if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH)) 14202aadf72SSteffen Klassert return -EINVAL; 14302aadf72SSteffen Klassert 144d8647b79SSteffen Klassert if (p->replay_window != 0) 145d8647b79SSteffen Klassert return -EINVAL; 146d8647b79SSteffen Klassert 147d8647b79SSteffen Klassert return 0; 148d8647b79SSteffen Klassert } 149df71837dSTrent Jaeger 1501da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p, 1515424f32eSThomas Graf struct nlattr **attrs) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds int err; 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds err = -EINVAL; 1561da177e4SLinus Torvalds switch (p->family) { 1571da177e4SLinus Torvalds case AF_INET: 1581da177e4SLinus Torvalds break; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds case AF_INET6: 161dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1621da177e4SLinus Torvalds break; 1631da177e4SLinus Torvalds #else 1641da177e4SLinus Torvalds err = -EAFNOSUPPORT; 1651da177e4SLinus Torvalds goto out; 1661da177e4SLinus Torvalds #endif 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds default: 1691da177e4SLinus Torvalds goto out; 1703ff50b79SStephen Hemminger } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds err = -EINVAL; 1731da177e4SLinus Torvalds switch (p->id.proto) { 1741da177e4SLinus Torvalds case IPPROTO_AH: 1754447bb33SMartin Willi if ((!attrs[XFRMA_ALG_AUTH] && 1764447bb33SMartin Willi !attrs[XFRMA_ALG_AUTH_TRUNC]) || 1771a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD] || 17835a7aa08SThomas Graf attrs[XFRMA_ALG_CRYPT] || 17935d2856bSMartin Willi attrs[XFRMA_ALG_COMP] || 180a0e5ef53STobias Brunner attrs[XFRMA_TFCPAD]) 1811da177e4SLinus Torvalds goto out; 1821da177e4SLinus Torvalds break; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds case IPPROTO_ESP: 1851a6509d9SHerbert Xu if (attrs[XFRMA_ALG_COMP]) 1861a6509d9SHerbert Xu goto out; 1871a6509d9SHerbert Xu if (!attrs[XFRMA_ALG_AUTH] && 1884447bb33SMartin Willi !attrs[XFRMA_ALG_AUTH_TRUNC] && 1891a6509d9SHerbert Xu !attrs[XFRMA_ALG_CRYPT] && 1901a6509d9SHerbert Xu !attrs[XFRMA_ALG_AEAD]) 1911a6509d9SHerbert Xu goto out; 1921a6509d9SHerbert Xu if ((attrs[XFRMA_ALG_AUTH] || 1934447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 1941a6509d9SHerbert Xu attrs[XFRMA_ALG_CRYPT]) && 1951a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD]) 1961da177e4SLinus Torvalds goto out; 19735d2856bSMartin Willi if (attrs[XFRMA_TFCPAD] && 19835d2856bSMartin Willi p->mode != XFRM_MODE_TUNNEL) 19935d2856bSMartin Willi goto out; 2001da177e4SLinus Torvalds break; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds case IPPROTO_COMP: 20335a7aa08SThomas Graf if (!attrs[XFRMA_ALG_COMP] || 2041a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD] || 20535a7aa08SThomas Graf attrs[XFRMA_ALG_AUTH] || 2064447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 20735d2856bSMartin Willi attrs[XFRMA_ALG_CRYPT] || 208a0e5ef53STobias Brunner attrs[XFRMA_TFCPAD] || 209a0e5ef53STobias Brunner (ntohl(p->id.spi) >= 0x10000)) 2101da177e4SLinus Torvalds goto out; 2111da177e4SLinus Torvalds break; 2121da177e4SLinus Torvalds 213dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 214e23c7194SMasahide NAKAMURA case IPPROTO_DSTOPTS: 215e23c7194SMasahide NAKAMURA case IPPROTO_ROUTING: 21635a7aa08SThomas Graf if (attrs[XFRMA_ALG_COMP] || 21735a7aa08SThomas Graf attrs[XFRMA_ALG_AUTH] || 2184447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 2191a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD] || 22035a7aa08SThomas Graf attrs[XFRMA_ALG_CRYPT] || 22135a7aa08SThomas Graf attrs[XFRMA_ENCAP] || 22235a7aa08SThomas Graf attrs[XFRMA_SEC_CTX] || 22335d2856bSMartin Willi attrs[XFRMA_TFCPAD] || 22435a7aa08SThomas Graf !attrs[XFRMA_COADDR]) 225e23c7194SMasahide NAKAMURA goto out; 226e23c7194SMasahide NAKAMURA break; 227e23c7194SMasahide NAKAMURA #endif 228e23c7194SMasahide NAKAMURA 2291da177e4SLinus Torvalds default: 2301da177e4SLinus Torvalds goto out; 2313ff50b79SStephen Hemminger } 2321da177e4SLinus Torvalds 2331a6509d9SHerbert Xu if ((err = verify_aead(attrs))) 2341a6509d9SHerbert Xu goto out; 2354447bb33SMartin Willi if ((err = verify_auth_trunc(attrs))) 2364447bb33SMartin Willi goto out; 23735a7aa08SThomas Graf if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH))) 2381da177e4SLinus Torvalds goto out; 23935a7aa08SThomas Graf if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT))) 2401da177e4SLinus Torvalds goto out; 24135a7aa08SThomas Graf if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP))) 2421da177e4SLinus Torvalds goto out; 24335a7aa08SThomas Graf if ((err = verify_sec_ctx_len(attrs))) 244df71837dSTrent Jaeger goto out; 245d8647b79SSteffen Klassert if ((err = verify_replay(p, attrs))) 246d8647b79SSteffen Klassert goto out; 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds err = -EINVAL; 2491da177e4SLinus Torvalds switch (p->mode) { 2507e49e6deSMasahide NAKAMURA case XFRM_MODE_TRANSPORT: 2517e49e6deSMasahide NAKAMURA case XFRM_MODE_TUNNEL: 252060f02a3SNoriaki TAKAMIYA case XFRM_MODE_ROUTEOPTIMIZATION: 2530a69452cSDiego Beltrami case XFRM_MODE_BEET: 2541da177e4SLinus Torvalds break; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds default: 2571da177e4SLinus Torvalds goto out; 2583ff50b79SStephen Hemminger } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds err = 0; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds out: 2631da177e4SLinus Torvalds return err; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props, 2676f2f19edSDavid S. Miller struct xfrm_algo_desc *(*get_byname)(const char *, int), 2685424f32eSThomas Graf struct nlattr *rta) 2691da177e4SLinus Torvalds { 2701da177e4SLinus Torvalds struct xfrm_algo *p, *ualg; 2711da177e4SLinus Torvalds struct xfrm_algo_desc *algo; 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds if (!rta) 2741da177e4SLinus Torvalds return 0; 2751da177e4SLinus Torvalds 2765424f32eSThomas Graf ualg = nla_data(rta); 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds algo = get_byname(ualg->alg_name, 1); 2791da177e4SLinus Torvalds if (!algo) 2801da177e4SLinus Torvalds return -ENOSYS; 2811da177e4SLinus Torvalds *props = algo->desc.sadb_alg_id; 2821da177e4SLinus Torvalds 2830f99be0dSEric Dumazet p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL); 2841da177e4SLinus Torvalds if (!p) 2851da177e4SLinus Torvalds return -ENOMEM; 2861da177e4SLinus Torvalds 28704ff1260SHerbert Xu strcpy(p->alg_name, algo->name); 2881da177e4SLinus Torvalds *algpp = p; 2891da177e4SLinus Torvalds return 0; 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 29269b0137fSHerbert Xu static int attach_crypt(struct xfrm_state *x, struct nlattr *rta) 29369b0137fSHerbert Xu { 29469b0137fSHerbert Xu struct xfrm_algo *p, *ualg; 29569b0137fSHerbert Xu struct xfrm_algo_desc *algo; 29669b0137fSHerbert Xu 29769b0137fSHerbert Xu if (!rta) 29869b0137fSHerbert Xu return 0; 29969b0137fSHerbert Xu 30069b0137fSHerbert Xu ualg = nla_data(rta); 30169b0137fSHerbert Xu 30269b0137fSHerbert Xu algo = xfrm_ealg_get_byname(ualg->alg_name, 1); 30369b0137fSHerbert Xu if (!algo) 30469b0137fSHerbert Xu return -ENOSYS; 30569b0137fSHerbert Xu x->props.ealgo = algo->desc.sadb_alg_id; 30669b0137fSHerbert Xu 30769b0137fSHerbert Xu p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL); 30869b0137fSHerbert Xu if (!p) 30969b0137fSHerbert Xu return -ENOMEM; 31069b0137fSHerbert Xu 31169b0137fSHerbert Xu strcpy(p->alg_name, algo->name); 31269b0137fSHerbert Xu x->ealg = p; 31369b0137fSHerbert Xu x->geniv = algo->uinfo.encr.geniv; 31469b0137fSHerbert Xu return 0; 31569b0137fSHerbert Xu } 31669b0137fSHerbert Xu 3174447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props, 3184447bb33SMartin Willi struct nlattr *rta) 3194447bb33SMartin Willi { 3204447bb33SMartin Willi struct xfrm_algo *ualg; 3214447bb33SMartin Willi struct xfrm_algo_auth *p; 3224447bb33SMartin Willi struct xfrm_algo_desc *algo; 3234447bb33SMartin Willi 3244447bb33SMartin Willi if (!rta) 3254447bb33SMartin Willi return 0; 3264447bb33SMartin Willi 3274447bb33SMartin Willi ualg = nla_data(rta); 3284447bb33SMartin Willi 3294447bb33SMartin Willi algo = xfrm_aalg_get_byname(ualg->alg_name, 1); 3304447bb33SMartin Willi if (!algo) 3314447bb33SMartin Willi return -ENOSYS; 3324447bb33SMartin Willi *props = algo->desc.sadb_alg_id; 3334447bb33SMartin Willi 3344447bb33SMartin Willi p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL); 3354447bb33SMartin Willi if (!p) 3364447bb33SMartin Willi return -ENOMEM; 3374447bb33SMartin Willi 3384447bb33SMartin Willi strcpy(p->alg_name, algo->name); 3394447bb33SMartin Willi p->alg_key_len = ualg->alg_key_len; 3404447bb33SMartin Willi p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; 3414447bb33SMartin Willi memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8); 3424447bb33SMartin Willi 3434447bb33SMartin Willi *algpp = p; 3444447bb33SMartin Willi return 0; 3454447bb33SMartin Willi } 3464447bb33SMartin Willi 3474447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props, 3484447bb33SMartin Willi struct nlattr *rta) 3494447bb33SMartin Willi { 3504447bb33SMartin Willi struct xfrm_algo_auth *p, *ualg; 3514447bb33SMartin Willi struct xfrm_algo_desc *algo; 3524447bb33SMartin Willi 3534447bb33SMartin Willi if (!rta) 3544447bb33SMartin Willi return 0; 3554447bb33SMartin Willi 3564447bb33SMartin Willi ualg = nla_data(rta); 3574447bb33SMartin Willi 3584447bb33SMartin Willi algo = xfrm_aalg_get_byname(ualg->alg_name, 1); 3594447bb33SMartin Willi if (!algo) 3604447bb33SMartin Willi return -ENOSYS; 361689f1c9dSHerbert Xu if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) 3624447bb33SMartin Willi return -EINVAL; 3634447bb33SMartin Willi *props = algo->desc.sadb_alg_id; 3644447bb33SMartin Willi 3654447bb33SMartin Willi p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL); 3664447bb33SMartin Willi if (!p) 3674447bb33SMartin Willi return -ENOMEM; 3684447bb33SMartin Willi 3694447bb33SMartin Willi strcpy(p->alg_name, algo->name); 3704447bb33SMartin Willi if (!p->alg_trunc_len) 3714447bb33SMartin Willi p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; 3724447bb33SMartin Willi 3734447bb33SMartin Willi *algpp = p; 3744447bb33SMartin Willi return 0; 3754447bb33SMartin Willi } 3764447bb33SMartin Willi 37769b0137fSHerbert Xu static int attach_aead(struct xfrm_state *x, struct nlattr *rta) 3781a6509d9SHerbert Xu { 3791a6509d9SHerbert Xu struct xfrm_algo_aead *p, *ualg; 3801a6509d9SHerbert Xu struct xfrm_algo_desc *algo; 3811a6509d9SHerbert Xu 3821a6509d9SHerbert Xu if (!rta) 3831a6509d9SHerbert Xu return 0; 3841a6509d9SHerbert Xu 3851a6509d9SHerbert Xu ualg = nla_data(rta); 3861a6509d9SHerbert Xu 3871a6509d9SHerbert Xu algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1); 3881a6509d9SHerbert Xu if (!algo) 3891a6509d9SHerbert Xu return -ENOSYS; 39069b0137fSHerbert Xu x->props.ealgo = algo->desc.sadb_alg_id; 3911a6509d9SHerbert Xu 3921a6509d9SHerbert Xu p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL); 3931a6509d9SHerbert Xu if (!p) 3941a6509d9SHerbert Xu return -ENOMEM; 3951a6509d9SHerbert Xu 3961a6509d9SHerbert Xu strcpy(p->alg_name, algo->name); 39769b0137fSHerbert Xu x->aead = p; 39869b0137fSHerbert Xu x->geniv = algo->uinfo.aead.geniv; 3991a6509d9SHerbert Xu return 0; 4001a6509d9SHerbert Xu } 4011a6509d9SHerbert Xu 402e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn, 403e2b19125SSteffen Klassert struct nlattr *rp) 404e2b19125SSteffen Klassert { 405e2b19125SSteffen Klassert struct xfrm_replay_state_esn *up; 406ecd79187SMathias Krause int ulen; 407e2b19125SSteffen Klassert 408e2b19125SSteffen Klassert if (!replay_esn || !rp) 409e2b19125SSteffen Klassert return 0; 410e2b19125SSteffen Klassert 411e2b19125SSteffen Klassert up = nla_data(rp); 412ecd79187SMathias Krause ulen = xfrm_replay_state_esn_len(up); 413e2b19125SSteffen Klassert 414ecd79187SMathias Krause if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen) 415e2b19125SSteffen Klassert return -EINVAL; 416e2b19125SSteffen Klassert 417e2b19125SSteffen Klassert return 0; 418e2b19125SSteffen Klassert } 419e2b19125SSteffen Klassert 420d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn, 421d8647b79SSteffen Klassert struct xfrm_replay_state_esn **preplay_esn, 422d8647b79SSteffen Klassert struct nlattr *rta) 423d8647b79SSteffen Klassert { 424d8647b79SSteffen Klassert struct xfrm_replay_state_esn *p, *pp, *up; 425ecd79187SMathias Krause int klen, ulen; 426d8647b79SSteffen Klassert 427d8647b79SSteffen Klassert if (!rta) 428d8647b79SSteffen Klassert return 0; 429d8647b79SSteffen Klassert 430d8647b79SSteffen Klassert up = nla_data(rta); 431ecd79187SMathias Krause klen = xfrm_replay_state_esn_len(up); 432ecd79187SMathias Krause ulen = nla_len(rta) >= klen ? klen : sizeof(*up); 433d8647b79SSteffen Klassert 434ecd79187SMathias Krause p = kzalloc(klen, GFP_KERNEL); 435d8647b79SSteffen Klassert if (!p) 436d8647b79SSteffen Klassert return -ENOMEM; 437d8647b79SSteffen Klassert 438ecd79187SMathias Krause pp = kzalloc(klen, GFP_KERNEL); 439d8647b79SSteffen Klassert if (!pp) { 440d8647b79SSteffen Klassert kfree(p); 441d8647b79SSteffen Klassert return -ENOMEM; 442d8647b79SSteffen Klassert } 443d8647b79SSteffen Klassert 444ecd79187SMathias Krause memcpy(p, up, ulen); 445ecd79187SMathias Krause memcpy(pp, up, ulen); 446ecd79187SMathias Krause 447d8647b79SSteffen Klassert *replay_esn = p; 448d8647b79SSteffen Klassert *preplay_esn = pp; 449d8647b79SSteffen Klassert 450d8647b79SSteffen Klassert return 0; 451d8647b79SSteffen Klassert } 452d8647b79SSteffen Klassert 453661697f7SJoy Latten static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx) 454df71837dSTrent Jaeger { 455df71837dSTrent Jaeger int len = 0; 456df71837dSTrent Jaeger 457df71837dSTrent Jaeger if (xfrm_ctx) { 458df71837dSTrent Jaeger len += sizeof(struct xfrm_user_sec_ctx); 459df71837dSTrent Jaeger len += xfrm_ctx->ctx_len; 460df71837dSTrent Jaeger } 461df71837dSTrent Jaeger return len; 462df71837dSTrent Jaeger } 463df71837dSTrent Jaeger 4641da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds memcpy(&x->id, &p->id, sizeof(x->id)); 4671da177e4SLinus Torvalds memcpy(&x->sel, &p->sel, sizeof(x->sel)); 4681da177e4SLinus Torvalds memcpy(&x->lft, &p->lft, sizeof(x->lft)); 4691da177e4SLinus Torvalds x->props.mode = p->mode; 47033fce60dSFan Du x->props.replay_window = min_t(unsigned int, p->replay_window, 47133fce60dSFan Du sizeof(x->replay.bitmap) * 8); 4721da177e4SLinus Torvalds x->props.reqid = p->reqid; 4731da177e4SLinus Torvalds x->props.family = p->family; 47454489c14SDavid S. Miller memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr)); 4751da177e4SLinus Torvalds x->props.flags = p->flags; 476196b0036SHerbert Xu 477ccf9b3b8SSteffen Klassert if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC)) 478196b0036SHerbert Xu x->sel.family = p->family; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 481d51d081dSJamal Hadi Salim /* 482d51d081dSJamal Hadi Salim * someday when pfkey also has support, we could have the code 483d51d081dSJamal Hadi Salim * somehow made shareable and move it to xfrm_state.c - JHS 484d51d081dSJamal Hadi Salim * 485d51d081dSJamal Hadi Salim */ 486e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs, 487e3ac104dSMathias Krause int update_esn) 488d51d081dSJamal Hadi Salim { 4895424f32eSThomas Graf struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 490e3ac104dSMathias Krause struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL; 4915424f32eSThomas Graf struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 4925424f32eSThomas Graf struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; 4935424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; 494d51d081dSJamal Hadi Salim 495d8647b79SSteffen Klassert if (re) { 496d8647b79SSteffen Klassert struct xfrm_replay_state_esn *replay_esn; 497d8647b79SSteffen Klassert replay_esn = nla_data(re); 498d8647b79SSteffen Klassert memcpy(x->replay_esn, replay_esn, 499d8647b79SSteffen Klassert xfrm_replay_state_esn_len(replay_esn)); 500d8647b79SSteffen Klassert memcpy(x->preplay_esn, replay_esn, 501d8647b79SSteffen Klassert xfrm_replay_state_esn_len(replay_esn)); 502d8647b79SSteffen Klassert } 503d8647b79SSteffen Klassert 504d51d081dSJamal Hadi Salim if (rp) { 505d51d081dSJamal Hadi Salim struct xfrm_replay_state *replay; 5065424f32eSThomas Graf replay = nla_data(rp); 507d51d081dSJamal Hadi Salim memcpy(&x->replay, replay, sizeof(*replay)); 508d51d081dSJamal Hadi Salim memcpy(&x->preplay, replay, sizeof(*replay)); 509d51d081dSJamal Hadi Salim } 510d51d081dSJamal Hadi Salim 511d51d081dSJamal Hadi Salim if (lt) { 512d51d081dSJamal Hadi Salim struct xfrm_lifetime_cur *ltime; 5135424f32eSThomas Graf ltime = nla_data(lt); 514d51d081dSJamal Hadi Salim x->curlft.bytes = ltime->bytes; 515d51d081dSJamal Hadi Salim x->curlft.packets = ltime->packets; 516d51d081dSJamal Hadi Salim x->curlft.add_time = ltime->add_time; 517d51d081dSJamal Hadi Salim x->curlft.use_time = ltime->use_time; 518d51d081dSJamal Hadi Salim } 519d51d081dSJamal Hadi Salim 520cf5cb79fSThomas Graf if (et) 5215424f32eSThomas Graf x->replay_maxage = nla_get_u32(et); 522d51d081dSJamal Hadi Salim 523cf5cb79fSThomas Graf if (rt) 5245424f32eSThomas Graf x->replay_maxdiff = nla_get_u32(rt); 525d51d081dSJamal Hadi Salim } 526d51d081dSJamal Hadi Salim 527fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net, 528fc34acd3SAlexey Dobriyan struct xfrm_usersa_info *p, 5295424f32eSThomas Graf struct nlattr **attrs, 5301da177e4SLinus Torvalds int *errp) 5311da177e4SLinus Torvalds { 532fc34acd3SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 5331da177e4SLinus Torvalds int err = -ENOMEM; 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds if (!x) 5361da177e4SLinus Torvalds goto error_no_put; 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds copy_from_user_state(x, p); 5391da177e4SLinus Torvalds 540a947b0a9SNicolas Dichtel if (attrs[XFRMA_SA_EXTRA_FLAGS]) 541a947b0a9SNicolas Dichtel x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]); 542a947b0a9SNicolas Dichtel 54369b0137fSHerbert Xu if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD]))) 5441a6509d9SHerbert Xu goto error; 5454447bb33SMartin Willi if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo, 5464447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC]))) 5474447bb33SMartin Willi goto error; 5484447bb33SMartin Willi if (!x->props.aalgo) { 5494447bb33SMartin Willi if ((err = attach_auth(&x->aalg, &x->props.aalgo, 55035a7aa08SThomas Graf attrs[XFRMA_ALG_AUTH]))) 5511da177e4SLinus Torvalds goto error; 5524447bb33SMartin Willi } 55369b0137fSHerbert Xu if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT]))) 5541da177e4SLinus Torvalds goto error; 5551da177e4SLinus Torvalds if ((err = attach_one_algo(&x->calg, &x->props.calgo, 5561da177e4SLinus Torvalds xfrm_calg_get_byname, 55735a7aa08SThomas Graf attrs[XFRMA_ALG_COMP]))) 5581da177e4SLinus Torvalds goto error; 559fd21150aSThomas Graf 560fd21150aSThomas Graf if (attrs[XFRMA_ENCAP]) { 561fd21150aSThomas Graf x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), 562fd21150aSThomas Graf sizeof(*x->encap), GFP_KERNEL); 563fd21150aSThomas Graf if (x->encap == NULL) 5641da177e4SLinus Torvalds goto error; 565fd21150aSThomas Graf } 566fd21150aSThomas Graf 56735d2856bSMartin Willi if (attrs[XFRMA_TFCPAD]) 56835d2856bSMartin Willi x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]); 56935d2856bSMartin Willi 570fd21150aSThomas Graf if (attrs[XFRMA_COADDR]) { 571fd21150aSThomas Graf x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]), 572fd21150aSThomas Graf sizeof(*x->coaddr), GFP_KERNEL); 573fd21150aSThomas Graf if (x->coaddr == NULL) 574060f02a3SNoriaki TAKAMIYA goto error; 575fd21150aSThomas Graf } 576fd21150aSThomas Graf 5776f26b61eSJamal Hadi Salim xfrm_mark_get(attrs, &x->mark); 5786f26b61eSJamal Hadi Salim 579a454f0ccSWei Yongjun err = __xfrm_init_state(x, false); 5801da177e4SLinus Torvalds if (err) 5811da177e4SLinus Torvalds goto error; 5821da177e4SLinus Torvalds 583fd21150aSThomas Graf if (attrs[XFRMA_SEC_CTX] && 584fd21150aSThomas Graf security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX]))) 585df71837dSTrent Jaeger goto error; 586df71837dSTrent Jaeger 587d8647b79SSteffen Klassert if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn, 588d8647b79SSteffen Klassert attrs[XFRMA_REPLAY_ESN_VAL]))) 589d8647b79SSteffen Klassert goto error; 590d8647b79SSteffen Klassert 5911da177e4SLinus Torvalds x->km.seq = p->seq; 592b27aeadbSAlexey Dobriyan x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth; 593d51d081dSJamal Hadi Salim /* sysctl_xfrm_aevent_etime is in 100ms units */ 594b27aeadbSAlexey Dobriyan x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M; 595d51d081dSJamal Hadi Salim 5969fdc4883SSteffen Klassert if ((err = xfrm_init_replay(x))) 5979fdc4883SSteffen Klassert goto error; 598d51d081dSJamal Hadi Salim 5999fdc4883SSteffen Klassert /* override default values from above */ 600e3ac104dSMathias Krause xfrm_update_ae_params(x, attrs, 0); 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds return x; 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds error: 6051da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 6061da177e4SLinus Torvalds xfrm_state_put(x); 6071da177e4SLinus Torvalds error_no_put: 6081da177e4SLinus Torvalds *errp = err; 6091da177e4SLinus Torvalds return NULL; 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 61222e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 6135424f32eSThomas Graf struct nlattr **attrs) 6141da177e4SLinus Torvalds { 615fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 6167b67c857SThomas Graf struct xfrm_usersa_info *p = nlmsg_data(nlh); 6171da177e4SLinus Torvalds struct xfrm_state *x; 6181da177e4SLinus Torvalds int err; 61926b15dadSJamal Hadi Salim struct km_event c; 6201da177e4SLinus Torvalds 62135a7aa08SThomas Graf err = verify_newsa_info(p, attrs); 6221da177e4SLinus Torvalds if (err) 6231da177e4SLinus Torvalds return err; 6241da177e4SLinus Torvalds 625fc34acd3SAlexey Dobriyan x = xfrm_state_construct(net, p, attrs, &err); 6261da177e4SLinus Torvalds if (!x) 6271da177e4SLinus Torvalds return err; 6281da177e4SLinus Torvalds 62926b15dadSJamal Hadi Salim xfrm_state_hold(x); 6301da177e4SLinus Torvalds if (nlh->nlmsg_type == XFRM_MSG_NEWSA) 6311da177e4SLinus Torvalds err = xfrm_state_add(x); 6321da177e4SLinus Torvalds else 6331da177e4SLinus Torvalds err = xfrm_state_update(x); 6341da177e4SLinus Torvalds 6352e71029eSTetsuo Handa xfrm_audit_state_add(x, err ? 0 : 1, true); 636161a09e7SJoy Latten 6371da177e4SLinus Torvalds if (err < 0) { 6381da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 63921380b81SHerbert Xu __xfrm_state_put(x); 6407d6dfe1fSPatrick McHardy goto out; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 64326b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 64415e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 645f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 64626b15dadSJamal Hadi Salim 64726b15dadSJamal Hadi Salim km_state_notify(x, &c); 6487d6dfe1fSPatrick McHardy out: 64926b15dadSJamal Hadi Salim xfrm_state_put(x); 6501da177e4SLinus Torvalds return err; 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds 653fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net, 654fc34acd3SAlexey Dobriyan struct xfrm_usersa_id *p, 6555424f32eSThomas Graf struct nlattr **attrs, 656eb2971b6SMasahide NAKAMURA int *errp) 657eb2971b6SMasahide NAKAMURA { 658eb2971b6SMasahide NAKAMURA struct xfrm_state *x = NULL; 6596f26b61eSJamal Hadi Salim struct xfrm_mark m; 660eb2971b6SMasahide NAKAMURA int err; 6616f26b61eSJamal Hadi Salim u32 mark = xfrm_mark_get(attrs, &m); 662eb2971b6SMasahide NAKAMURA 663eb2971b6SMasahide NAKAMURA if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { 664eb2971b6SMasahide NAKAMURA err = -ESRCH; 6656f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family); 666eb2971b6SMasahide NAKAMURA } else { 667eb2971b6SMasahide NAKAMURA xfrm_address_t *saddr = NULL; 668eb2971b6SMasahide NAKAMURA 66935a7aa08SThomas Graf verify_one_addr(attrs, XFRMA_SRCADDR, &saddr); 670eb2971b6SMasahide NAKAMURA if (!saddr) { 671eb2971b6SMasahide NAKAMURA err = -EINVAL; 672eb2971b6SMasahide NAKAMURA goto out; 673eb2971b6SMasahide NAKAMURA } 674eb2971b6SMasahide NAKAMURA 6759abbffeeSMasahide NAKAMURA err = -ESRCH; 6766f26b61eSJamal Hadi Salim x = xfrm_state_lookup_byaddr(net, mark, 6776f26b61eSJamal Hadi Salim &p->daddr, saddr, 678221df1edSAlexey Dobriyan p->proto, p->family); 679eb2971b6SMasahide NAKAMURA } 680eb2971b6SMasahide NAKAMURA 681eb2971b6SMasahide NAKAMURA out: 682eb2971b6SMasahide NAKAMURA if (!x && errp) 683eb2971b6SMasahide NAKAMURA *errp = err; 684eb2971b6SMasahide NAKAMURA return x; 685eb2971b6SMasahide NAKAMURA } 686eb2971b6SMasahide NAKAMURA 68722e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 6885424f32eSThomas Graf struct nlattr **attrs) 6891da177e4SLinus Torvalds { 690fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 6911da177e4SLinus Torvalds struct xfrm_state *x; 692eb2971b6SMasahide NAKAMURA int err = -ESRCH; 69326b15dadSJamal Hadi Salim struct km_event c; 6947b67c857SThomas Graf struct xfrm_usersa_id *p = nlmsg_data(nlh); 6951da177e4SLinus Torvalds 696fc34acd3SAlexey Dobriyan x = xfrm_user_state_lookup(net, p, attrs, &err); 6971da177e4SLinus Torvalds if (x == NULL) 698eb2971b6SMasahide NAKAMURA return err; 6991da177e4SLinus Torvalds 7006f68dc37SDavid S. Miller if ((err = security_xfrm_state_delete(x)) != 0) 701c8c05a8eSCatherine Zhang goto out; 702c8c05a8eSCatherine Zhang 7031da177e4SLinus Torvalds if (xfrm_state_kern(x)) { 704c8c05a8eSCatherine Zhang err = -EPERM; 705c8c05a8eSCatherine Zhang goto out; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 70826b15dadSJamal Hadi Salim err = xfrm_state_delete(x); 709161a09e7SJoy Latten 710c8c05a8eSCatherine Zhang if (err < 0) 711c8c05a8eSCatherine Zhang goto out; 71226b15dadSJamal Hadi Salim 71326b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 71415e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 715f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 71626b15dadSJamal Hadi Salim km_state_notify(x, &c); 7171da177e4SLinus Torvalds 718c8c05a8eSCatherine Zhang out: 7192e71029eSTetsuo Handa xfrm_audit_state_delete(x, err ? 0 : 1, true); 720c8c05a8eSCatherine Zhang xfrm_state_put(x); 72126b15dadSJamal Hadi Salim return err; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 7251da177e4SLinus Torvalds { 726f778a636SMathias Krause memset(p, 0, sizeof(*p)); 7271da177e4SLinus Torvalds memcpy(&p->id, &x->id, sizeof(p->id)); 7281da177e4SLinus Torvalds memcpy(&p->sel, &x->sel, sizeof(p->sel)); 7291da177e4SLinus Torvalds memcpy(&p->lft, &x->lft, sizeof(p->lft)); 7301da177e4SLinus Torvalds memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); 7311da177e4SLinus Torvalds memcpy(&p->stats, &x->stats, sizeof(p->stats)); 73254489c14SDavid S. Miller memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); 7331da177e4SLinus Torvalds p->mode = x->props.mode; 7341da177e4SLinus Torvalds p->replay_window = x->props.replay_window; 7351da177e4SLinus Torvalds p->reqid = x->props.reqid; 7361da177e4SLinus Torvalds p->family = x->props.family; 7371da177e4SLinus Torvalds p->flags = x->props.flags; 7381da177e4SLinus Torvalds p->seq = x->km.seq; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds struct xfrm_dump_info { 7421da177e4SLinus Torvalds struct sk_buff *in_skb; 7431da177e4SLinus Torvalds struct sk_buff *out_skb; 7441da177e4SLinus Torvalds u32 nlmsg_seq; 7451da177e4SLinus Torvalds u16 nlmsg_flags; 7461da177e4SLinus Torvalds }; 7471da177e4SLinus Torvalds 748c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) 749c0144beaSThomas Graf { 750c0144beaSThomas Graf struct xfrm_user_sec_ctx *uctx; 751c0144beaSThomas Graf struct nlattr *attr; 75268325d3bSHerbert Xu int ctx_size = sizeof(*uctx) + s->ctx_len; 753c0144beaSThomas Graf 754c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size); 755c0144beaSThomas Graf if (attr == NULL) 756c0144beaSThomas Graf return -EMSGSIZE; 757c0144beaSThomas Graf 758c0144beaSThomas Graf uctx = nla_data(attr); 759c0144beaSThomas Graf uctx->exttype = XFRMA_SEC_CTX; 760c0144beaSThomas Graf uctx->len = ctx_size; 761c0144beaSThomas Graf uctx->ctx_doi = s->ctx_doi; 762c0144beaSThomas Graf uctx->ctx_alg = s->ctx_alg; 763c0144beaSThomas Graf uctx->ctx_len = s->ctx_len; 764c0144beaSThomas Graf memcpy(uctx + 1, s->ctx_str, s->ctx_len); 765c0144beaSThomas Graf 766c0144beaSThomas Graf return 0; 767c0144beaSThomas Graf } 768c0144beaSThomas Graf 7694447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb) 7704447bb33SMartin Willi { 7714447bb33SMartin Willi struct xfrm_algo *algo; 7724447bb33SMartin Willi struct nlattr *nla; 7734447bb33SMartin Willi 7744447bb33SMartin Willi nla = nla_reserve(skb, XFRMA_ALG_AUTH, 7754447bb33SMartin Willi sizeof(*algo) + (auth->alg_key_len + 7) / 8); 7764447bb33SMartin Willi if (!nla) 7774447bb33SMartin Willi return -EMSGSIZE; 7784447bb33SMartin Willi 7794447bb33SMartin Willi algo = nla_data(nla); 7804c87308bSMathias Krause strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name)); 7814447bb33SMartin Willi memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8); 7824447bb33SMartin Willi algo->alg_key_len = auth->alg_key_len; 7834447bb33SMartin Willi 7844447bb33SMartin Willi return 0; 7854447bb33SMartin Willi } 7864447bb33SMartin Willi 78768325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */ 78868325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x, 78968325d3bSHerbert Xu struct xfrm_usersa_info *p, 79068325d3bSHerbert Xu struct sk_buff *skb) 7911da177e4SLinus Torvalds { 7921d1e34ddSDavid S. Miller int ret = 0; 7931d1e34ddSDavid S. Miller 7941da177e4SLinus Torvalds copy_to_user_state(x, p); 7951da177e4SLinus Torvalds 796a947b0a9SNicolas Dichtel if (x->props.extra_flags) { 797a947b0a9SNicolas Dichtel ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS, 798a947b0a9SNicolas Dichtel x->props.extra_flags); 799a947b0a9SNicolas Dichtel if (ret) 800a947b0a9SNicolas Dichtel goto out; 801a947b0a9SNicolas Dichtel } 802a947b0a9SNicolas Dichtel 8031d1e34ddSDavid S. Miller if (x->coaddr) { 8041d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); 8051d1e34ddSDavid S. Miller if (ret) 8061d1e34ddSDavid S. Miller goto out; 8071d1e34ddSDavid S. Miller } 8081d1e34ddSDavid S. Miller if (x->lastused) { 8091d1e34ddSDavid S. Miller ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused); 8101d1e34ddSDavid S. Miller if (ret) 8111d1e34ddSDavid S. Miller goto out; 8121d1e34ddSDavid S. Miller } 8131d1e34ddSDavid S. Miller if (x->aead) { 8141d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead); 8151d1e34ddSDavid S. Miller if (ret) 8161d1e34ddSDavid S. Miller goto out; 8171d1e34ddSDavid S. Miller } 8181d1e34ddSDavid S. Miller if (x->aalg) { 8191d1e34ddSDavid S. Miller ret = copy_to_user_auth(x->aalg, skb); 8201d1e34ddSDavid S. Miller if (!ret) 8211d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC, 8221d1e34ddSDavid S. Miller xfrm_alg_auth_len(x->aalg), x->aalg); 8231d1e34ddSDavid S. Miller if (ret) 8241d1e34ddSDavid S. Miller goto out; 8251d1e34ddSDavid S. Miller } 8261d1e34ddSDavid S. Miller if (x->ealg) { 8271d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg); 8281d1e34ddSDavid S. Miller if (ret) 8291d1e34ddSDavid S. Miller goto out; 8301d1e34ddSDavid S. Miller } 8311d1e34ddSDavid S. Miller if (x->calg) { 8321d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); 8331d1e34ddSDavid S. Miller if (ret) 8341d1e34ddSDavid S. Miller goto out; 8351d1e34ddSDavid S. Miller } 8361d1e34ddSDavid S. Miller if (x->encap) { 8371d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); 8381d1e34ddSDavid S. Miller if (ret) 8391d1e34ddSDavid S. Miller goto out; 8401d1e34ddSDavid S. Miller } 8411d1e34ddSDavid S. Miller if (x->tfcpad) { 8421d1e34ddSDavid S. Miller ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad); 8431d1e34ddSDavid S. Miller if (ret) 8441d1e34ddSDavid S. Miller goto out; 8451d1e34ddSDavid S. Miller } 8461d1e34ddSDavid S. Miller ret = xfrm_mark_put(skb, &x->mark); 8471d1e34ddSDavid S. Miller if (ret) 8481d1e34ddSDavid S. Miller goto out; 849f293a5e3Sdingzhi if (x->replay_esn) 8501d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 851d0fde795SDavid S. Miller xfrm_replay_state_esn_len(x->replay_esn), 8521d1e34ddSDavid S. Miller x->replay_esn); 853f293a5e3Sdingzhi else 854f293a5e3Sdingzhi ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 855f293a5e3Sdingzhi &x->replay); 8561d1e34ddSDavid S. Miller if (ret) 8571d1e34ddSDavid S. Miller goto out; 8581d1e34ddSDavid S. Miller if (x->security) 8591d1e34ddSDavid S. Miller ret = copy_sec_ctx(x->security, skb); 8601d1e34ddSDavid S. Miller out: 8611d1e34ddSDavid S. Miller return ret; 86268325d3bSHerbert Xu } 86368325d3bSHerbert Xu 86468325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr) 86568325d3bSHerbert Xu { 86668325d3bSHerbert Xu struct xfrm_dump_info *sp = ptr; 86768325d3bSHerbert Xu struct sk_buff *in_skb = sp->in_skb; 86868325d3bSHerbert Xu struct sk_buff *skb = sp->out_skb; 86968325d3bSHerbert Xu struct xfrm_usersa_info *p; 87068325d3bSHerbert Xu struct nlmsghdr *nlh; 87168325d3bSHerbert Xu int err; 87268325d3bSHerbert Xu 87315e47304SEric W. Biederman nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq, 87468325d3bSHerbert Xu XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags); 87568325d3bSHerbert Xu if (nlh == NULL) 87668325d3bSHerbert Xu return -EMSGSIZE; 87768325d3bSHerbert Xu 87868325d3bSHerbert Xu p = nlmsg_data(nlh); 87968325d3bSHerbert Xu 88068325d3bSHerbert Xu err = copy_to_user_state_extra(x, p, skb); 8811d1e34ddSDavid S. Miller if (err) { 8829825069dSThomas Graf nlmsg_cancel(skb, nlh); 88368325d3bSHerbert Xu return err; 8841da177e4SLinus Torvalds } 8851d1e34ddSDavid S. Miller nlmsg_end(skb, nlh); 8861d1e34ddSDavid S. Miller return 0; 8871d1e34ddSDavid S. Miller } 8881da177e4SLinus Torvalds 8894c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb) 8904c563f76STimo Teras { 8914c563f76STimo Teras struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 892283bc9f3SFan Du struct sock *sk = cb->skb->sk; 893283bc9f3SFan Du struct net *net = sock_net(sk); 894283bc9f3SFan Du 895283bc9f3SFan Du xfrm_state_walk_done(walk, net); 8964c563f76STimo Teras return 0; 8974c563f76STimo Teras } 8984c563f76STimo Teras 899d3623099SNicolas Dichtel static const struct nla_policy xfrma_policy[XFRMA_MAX+1]; 9001da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) 9011da177e4SLinus Torvalds { 902fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 9034c563f76STimo Teras struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 9041da177e4SLinus Torvalds struct xfrm_dump_info info; 9051da177e4SLinus Torvalds 9064c563f76STimo Teras BUILD_BUG_ON(sizeof(struct xfrm_state_walk) > 9074c563f76STimo Teras sizeof(cb->args) - sizeof(cb->args[0])); 9084c563f76STimo Teras 9091da177e4SLinus Torvalds info.in_skb = cb->skb; 9101da177e4SLinus Torvalds info.out_skb = skb; 9111da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 9121da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 9134c563f76STimo Teras 9144c563f76STimo Teras if (!cb->args[0]) { 915d3623099SNicolas Dichtel struct nlattr *attrs[XFRMA_MAX+1]; 916870a2df4SNicolas Dichtel struct xfrm_address_filter *filter = NULL; 917d3623099SNicolas Dichtel u8 proto = 0; 918d3623099SNicolas Dichtel int err; 919d3623099SNicolas Dichtel 9204c563f76STimo Teras cb->args[0] = 1; 921d3623099SNicolas Dichtel 922d3623099SNicolas Dichtel err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX, 923d3623099SNicolas Dichtel xfrma_policy); 924d3623099SNicolas Dichtel if (err < 0) 925d3623099SNicolas Dichtel return err; 926d3623099SNicolas Dichtel 927870a2df4SNicolas Dichtel if (attrs[XFRMA_ADDRESS_FILTER]) { 928*df367561SAndrzej Hajda filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]), 929*df367561SAndrzej Hajda sizeof(*filter), GFP_KERNEL); 930d3623099SNicolas Dichtel if (filter == NULL) 931d3623099SNicolas Dichtel return -ENOMEM; 932d3623099SNicolas Dichtel } 933d3623099SNicolas Dichtel 934d3623099SNicolas Dichtel if (attrs[XFRMA_PROTO]) 935d3623099SNicolas Dichtel proto = nla_get_u8(attrs[XFRMA_PROTO]); 936d3623099SNicolas Dichtel 937d3623099SNicolas Dichtel xfrm_state_walk_init(walk, proto, filter); 9384c563f76STimo Teras } 9394c563f76STimo Teras 940fc34acd3SAlexey Dobriyan (void) xfrm_state_walk(net, walk, dump_one_state, &info); 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds return skb->len; 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, 9461da177e4SLinus Torvalds struct xfrm_state *x, u32 seq) 9471da177e4SLinus Torvalds { 9481da177e4SLinus Torvalds struct xfrm_dump_info info; 9491da177e4SLinus Torvalds struct sk_buff *skb; 950864745d2SMathias Krause int err; 9511da177e4SLinus Torvalds 9527deb2264SThomas Graf skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 9531da177e4SLinus Torvalds if (!skb) 9541da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds info.in_skb = in_skb; 9571da177e4SLinus Torvalds info.out_skb = skb; 9581da177e4SLinus Torvalds info.nlmsg_seq = seq; 9591da177e4SLinus Torvalds info.nlmsg_flags = 0; 9601da177e4SLinus Torvalds 961864745d2SMathias Krause err = dump_one_state(x, 0, &info); 962864745d2SMathias Krause if (err) { 9631da177e4SLinus Torvalds kfree_skb(skb); 964864745d2SMathias Krause return ERR_PTR(err); 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds return skb; 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 97021ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available. 97121ee543eSMichal Kubecek * Must be called with RCU read lock. 97221ee543eSMichal Kubecek */ 97321ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb, 97421ee543eSMichal Kubecek u32 pid, unsigned int group) 97521ee543eSMichal Kubecek { 97621ee543eSMichal Kubecek struct sock *nlsk = rcu_dereference(net->xfrm.nlsk); 97721ee543eSMichal Kubecek 97821ee543eSMichal Kubecek if (nlsk) 97921ee543eSMichal Kubecek return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC); 98021ee543eSMichal Kubecek else 98121ee543eSMichal Kubecek return -1; 98221ee543eSMichal Kubecek } 98321ee543eSMichal Kubecek 9847deb2264SThomas Graf static inline size_t xfrm_spdinfo_msgsize(void) 9857deb2264SThomas Graf { 9867deb2264SThomas Graf return NLMSG_ALIGN(4) 9877deb2264SThomas Graf + nla_total_size(sizeof(struct xfrmu_spdinfo)) 988880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhinfo)) 989880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhthresh)) 990880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhthresh)); 9917deb2264SThomas Graf } 9927deb2264SThomas Graf 993e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net, 99415e47304SEric W. Biederman u32 portid, u32 seq, u32 flags) 995ecfd6b18SJamal Hadi Salim { 9965a6d3416SJamal Hadi Salim struct xfrmk_spdinfo si; 9975a6d3416SJamal Hadi Salim struct xfrmu_spdinfo spc; 9985a6d3416SJamal Hadi Salim struct xfrmu_spdhinfo sph; 999880a6fabSChristophe Gouault struct xfrmu_spdhthresh spt4, spt6; 1000ecfd6b18SJamal Hadi Salim struct nlmsghdr *nlh; 10011d1e34ddSDavid S. Miller int err; 1002ecfd6b18SJamal Hadi Salim u32 *f; 1003880a6fabSChristophe Gouault unsigned lseq; 1004ecfd6b18SJamal Hadi Salim 100515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); 100625985edcSLucas De Marchi if (nlh == NULL) /* shouldn't really happen ... */ 1007ecfd6b18SJamal Hadi Salim return -EMSGSIZE; 1008ecfd6b18SJamal Hadi Salim 1009ecfd6b18SJamal Hadi Salim f = nlmsg_data(nlh); 1010ecfd6b18SJamal Hadi Salim *f = flags; 1011e071041bSAlexey Dobriyan xfrm_spd_getinfo(net, &si); 10125a6d3416SJamal Hadi Salim spc.incnt = si.incnt; 10135a6d3416SJamal Hadi Salim spc.outcnt = si.outcnt; 10145a6d3416SJamal Hadi Salim spc.fwdcnt = si.fwdcnt; 10155a6d3416SJamal Hadi Salim spc.inscnt = si.inscnt; 10165a6d3416SJamal Hadi Salim spc.outscnt = si.outscnt; 10175a6d3416SJamal Hadi Salim spc.fwdscnt = si.fwdscnt; 10185a6d3416SJamal Hadi Salim sph.spdhcnt = si.spdhcnt; 10195a6d3416SJamal Hadi Salim sph.spdhmcnt = si.spdhmcnt; 1020ecfd6b18SJamal Hadi Salim 1021880a6fabSChristophe Gouault do { 1022880a6fabSChristophe Gouault lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock); 1023880a6fabSChristophe Gouault 1024880a6fabSChristophe Gouault spt4.lbits = net->xfrm.policy_hthresh.lbits4; 1025880a6fabSChristophe Gouault spt4.rbits = net->xfrm.policy_hthresh.rbits4; 1026880a6fabSChristophe Gouault spt6.lbits = net->xfrm.policy_hthresh.lbits6; 1027880a6fabSChristophe Gouault spt6.rbits = net->xfrm.policy_hthresh.rbits6; 1028880a6fabSChristophe Gouault } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq)); 1029880a6fabSChristophe Gouault 10301d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc); 10311d1e34ddSDavid S. Miller if (!err) 10321d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph); 1033880a6fabSChristophe Gouault if (!err) 1034880a6fabSChristophe Gouault err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4); 1035880a6fabSChristophe Gouault if (!err) 1036880a6fabSChristophe Gouault err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6); 10371d1e34ddSDavid S. Miller if (err) { 10381d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 10391d1e34ddSDavid S. Miller return err; 10401d1e34ddSDavid S. Miller } 1041ecfd6b18SJamal Hadi Salim 1042053c095aSJohannes Berg nlmsg_end(skb, nlh); 1043053c095aSJohannes Berg return 0; 1044ecfd6b18SJamal Hadi Salim } 1045ecfd6b18SJamal Hadi Salim 1046880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 1047880a6fabSChristophe Gouault struct nlattr **attrs) 1048880a6fabSChristophe Gouault { 1049880a6fabSChristophe Gouault struct net *net = sock_net(skb->sk); 1050880a6fabSChristophe Gouault struct xfrmu_spdhthresh *thresh4 = NULL; 1051880a6fabSChristophe Gouault struct xfrmu_spdhthresh *thresh6 = NULL; 1052880a6fabSChristophe Gouault 1053880a6fabSChristophe Gouault /* selector prefixlen thresholds to hash policies */ 1054880a6fabSChristophe Gouault if (attrs[XFRMA_SPD_IPV4_HTHRESH]) { 1055880a6fabSChristophe Gouault struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH]; 1056880a6fabSChristophe Gouault 1057880a6fabSChristophe Gouault if (nla_len(rta) < sizeof(*thresh4)) 1058880a6fabSChristophe Gouault return -EINVAL; 1059880a6fabSChristophe Gouault thresh4 = nla_data(rta); 1060880a6fabSChristophe Gouault if (thresh4->lbits > 32 || thresh4->rbits > 32) 1061880a6fabSChristophe Gouault return -EINVAL; 1062880a6fabSChristophe Gouault } 1063880a6fabSChristophe Gouault if (attrs[XFRMA_SPD_IPV6_HTHRESH]) { 1064880a6fabSChristophe Gouault struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH]; 1065880a6fabSChristophe Gouault 1066880a6fabSChristophe Gouault if (nla_len(rta) < sizeof(*thresh6)) 1067880a6fabSChristophe Gouault return -EINVAL; 1068880a6fabSChristophe Gouault thresh6 = nla_data(rta); 1069880a6fabSChristophe Gouault if (thresh6->lbits > 128 || thresh6->rbits > 128) 1070880a6fabSChristophe Gouault return -EINVAL; 1071880a6fabSChristophe Gouault } 1072880a6fabSChristophe Gouault 1073880a6fabSChristophe Gouault if (thresh4 || thresh6) { 1074880a6fabSChristophe Gouault write_seqlock(&net->xfrm.policy_hthresh.lock); 1075880a6fabSChristophe Gouault if (thresh4) { 1076880a6fabSChristophe Gouault net->xfrm.policy_hthresh.lbits4 = thresh4->lbits; 1077880a6fabSChristophe Gouault net->xfrm.policy_hthresh.rbits4 = thresh4->rbits; 1078880a6fabSChristophe Gouault } 1079880a6fabSChristophe Gouault if (thresh6) { 1080880a6fabSChristophe Gouault net->xfrm.policy_hthresh.lbits6 = thresh6->lbits; 1081880a6fabSChristophe Gouault net->xfrm.policy_hthresh.rbits6 = thresh6->rbits; 1082880a6fabSChristophe Gouault } 1083880a6fabSChristophe Gouault write_sequnlock(&net->xfrm.policy_hthresh.lock); 1084880a6fabSChristophe Gouault 1085880a6fabSChristophe Gouault xfrm_policy_hash_rebuild(net); 1086880a6fabSChristophe Gouault } 1087880a6fabSChristophe Gouault 1088880a6fabSChristophe Gouault return 0; 1089880a6fabSChristophe Gouault } 1090880a6fabSChristophe Gouault 1091ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 10925424f32eSThomas Graf struct nlattr **attrs) 1093ecfd6b18SJamal Hadi Salim { 1094a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 1095ecfd6b18SJamal Hadi Salim struct sk_buff *r_skb; 10967b67c857SThomas Graf u32 *flags = nlmsg_data(nlh); 109715e47304SEric W. Biederman u32 sportid = NETLINK_CB(skb).portid; 1098ecfd6b18SJamal Hadi Salim u32 seq = nlh->nlmsg_seq; 1099ecfd6b18SJamal Hadi Salim 11007deb2264SThomas Graf r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); 1101ecfd6b18SJamal Hadi Salim if (r_skb == NULL) 1102ecfd6b18SJamal Hadi Salim return -ENOMEM; 1103ecfd6b18SJamal Hadi Salim 110415e47304SEric W. Biederman if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0) 1105ecfd6b18SJamal Hadi Salim BUG(); 1106ecfd6b18SJamal Hadi Salim 110715e47304SEric W. Biederman return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 1108ecfd6b18SJamal Hadi Salim } 1109ecfd6b18SJamal Hadi Salim 11107deb2264SThomas Graf static inline size_t xfrm_sadinfo_msgsize(void) 11117deb2264SThomas Graf { 11127deb2264SThomas Graf return NLMSG_ALIGN(4) 11137deb2264SThomas Graf + nla_total_size(sizeof(struct xfrmu_sadhinfo)) 11147deb2264SThomas Graf + nla_total_size(4); /* XFRMA_SAD_CNT */ 11157deb2264SThomas Graf } 11167deb2264SThomas Graf 1117e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net, 111815e47304SEric W. Biederman u32 portid, u32 seq, u32 flags) 111928d8909bSJamal Hadi Salim { 1120af11e316SJamal Hadi Salim struct xfrmk_sadinfo si; 1121af11e316SJamal Hadi Salim struct xfrmu_sadhinfo sh; 112228d8909bSJamal Hadi Salim struct nlmsghdr *nlh; 11231d1e34ddSDavid S. Miller int err; 112428d8909bSJamal Hadi Salim u32 *f; 112528d8909bSJamal Hadi Salim 112615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); 112725985edcSLucas De Marchi if (nlh == NULL) /* shouldn't really happen ... */ 112828d8909bSJamal Hadi Salim return -EMSGSIZE; 112928d8909bSJamal Hadi Salim 113028d8909bSJamal Hadi Salim f = nlmsg_data(nlh); 113128d8909bSJamal Hadi Salim *f = flags; 1132e071041bSAlexey Dobriyan xfrm_sad_getinfo(net, &si); 113328d8909bSJamal Hadi Salim 1134af11e316SJamal Hadi Salim sh.sadhmcnt = si.sadhmcnt; 1135af11e316SJamal Hadi Salim sh.sadhcnt = si.sadhcnt; 1136af11e316SJamal Hadi Salim 11371d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt); 11381d1e34ddSDavid S. Miller if (!err) 11391d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); 11401d1e34ddSDavid S. Miller if (err) { 11411d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 11421d1e34ddSDavid S. Miller return err; 11431d1e34ddSDavid S. Miller } 114428d8909bSJamal Hadi Salim 1145053c095aSJohannes Berg nlmsg_end(skb, nlh); 1146053c095aSJohannes Berg return 0; 114728d8909bSJamal Hadi Salim } 114828d8909bSJamal Hadi Salim 114928d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 11505424f32eSThomas Graf struct nlattr **attrs) 115128d8909bSJamal Hadi Salim { 1152a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 115328d8909bSJamal Hadi Salim struct sk_buff *r_skb; 11547b67c857SThomas Graf u32 *flags = nlmsg_data(nlh); 115515e47304SEric W. Biederman u32 sportid = NETLINK_CB(skb).portid; 115628d8909bSJamal Hadi Salim u32 seq = nlh->nlmsg_seq; 115728d8909bSJamal Hadi Salim 11587deb2264SThomas Graf r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); 115928d8909bSJamal Hadi Salim if (r_skb == NULL) 116028d8909bSJamal Hadi Salim return -ENOMEM; 116128d8909bSJamal Hadi Salim 116215e47304SEric W. Biederman if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0) 116328d8909bSJamal Hadi Salim BUG(); 116428d8909bSJamal Hadi Salim 116515e47304SEric W. Biederman return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 116628d8909bSJamal Hadi Salim } 116728d8909bSJamal Hadi Salim 116822e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 11695424f32eSThomas Graf struct nlattr **attrs) 11701da177e4SLinus Torvalds { 1171fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 11727b67c857SThomas Graf struct xfrm_usersa_id *p = nlmsg_data(nlh); 11731da177e4SLinus Torvalds struct xfrm_state *x; 11741da177e4SLinus Torvalds struct sk_buff *resp_skb; 1175eb2971b6SMasahide NAKAMURA int err = -ESRCH; 11761da177e4SLinus Torvalds 1177fc34acd3SAlexey Dobriyan x = xfrm_user_state_lookup(net, p, attrs, &err); 11781da177e4SLinus Torvalds if (x == NULL) 11791da177e4SLinus Torvalds goto out_noput; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 11821da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 11831da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 11841da177e4SLinus Torvalds } else { 118515e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds xfrm_state_put(x); 11881da177e4SLinus Torvalds out_noput: 11891da177e4SLinus Torvalds return err; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 119222e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, 11935424f32eSThomas Graf struct nlattr **attrs) 11941da177e4SLinus Torvalds { 1195fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 11961da177e4SLinus Torvalds struct xfrm_state *x; 11971da177e4SLinus Torvalds struct xfrm_userspi_info *p; 11981da177e4SLinus Torvalds struct sk_buff *resp_skb; 11991da177e4SLinus Torvalds xfrm_address_t *daddr; 12001da177e4SLinus Torvalds int family; 12011da177e4SLinus Torvalds int err; 12026f26b61eSJamal Hadi Salim u32 mark; 12036f26b61eSJamal Hadi Salim struct xfrm_mark m; 12041da177e4SLinus Torvalds 12057b67c857SThomas Graf p = nlmsg_data(nlh); 1206776e9dd9SFan Du err = verify_spi_info(p->info.id.proto, p->min, p->max); 12071da177e4SLinus Torvalds if (err) 12081da177e4SLinus Torvalds goto out_noput; 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds family = p->info.family; 12111da177e4SLinus Torvalds daddr = &p->info.id.daddr; 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds x = NULL; 12146f26b61eSJamal Hadi Salim 12156f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 12161da177e4SLinus Torvalds if (p->info.seq) { 12176f26b61eSJamal Hadi Salim x = xfrm_find_acq_byseq(net, mark, p->info.seq); 121870e94e66SYOSHIFUJI Hideaki / 吉藤英明 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) { 12191da177e4SLinus Torvalds xfrm_state_put(x); 12201da177e4SLinus Torvalds x = NULL; 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds if (!x) 12256f26b61eSJamal Hadi Salim x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid, 12261da177e4SLinus Torvalds p->info.id.proto, daddr, 12271da177e4SLinus Torvalds &p->info.saddr, 1, 12281da177e4SLinus Torvalds family); 12291da177e4SLinus Torvalds err = -ENOENT; 12301da177e4SLinus Torvalds if (x == NULL) 12311da177e4SLinus Torvalds goto out_noput; 12321da177e4SLinus Torvalds 1233658b219eSHerbert Xu err = xfrm_alloc_spi(x, p->min, p->max); 1234658b219eSHerbert Xu if (err) 1235658b219eSHerbert Xu goto out; 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 12381da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 12391da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 12401da177e4SLinus Torvalds goto out; 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds 124315e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds out: 12461da177e4SLinus Torvalds xfrm_state_put(x); 12471da177e4SLinus Torvalds out_noput: 12481da177e4SLinus Torvalds return err; 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds 1251b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir) 12521da177e4SLinus Torvalds { 12531da177e4SLinus Torvalds switch (dir) { 12541da177e4SLinus Torvalds case XFRM_POLICY_IN: 12551da177e4SLinus Torvalds case XFRM_POLICY_OUT: 12561da177e4SLinus Torvalds case XFRM_POLICY_FWD: 12571da177e4SLinus Torvalds break; 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds default: 12601da177e4SLinus Torvalds return -EINVAL; 12613ff50b79SStephen Hemminger } 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds return 0; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 1266b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type) 1267f7b6983fSMasahide NAKAMURA { 1268f7b6983fSMasahide NAKAMURA switch (type) { 1269f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_MAIN: 1270f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 1271f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_SUB: 1272f7b6983fSMasahide NAKAMURA #endif 1273f7b6983fSMasahide NAKAMURA break; 1274f7b6983fSMasahide NAKAMURA 1275f7b6983fSMasahide NAKAMURA default: 1276f7b6983fSMasahide NAKAMURA return -EINVAL; 12773ff50b79SStephen Hemminger } 1278f7b6983fSMasahide NAKAMURA 1279f7b6983fSMasahide NAKAMURA return 0; 1280f7b6983fSMasahide NAKAMURA } 1281f7b6983fSMasahide NAKAMURA 12821da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) 12831da177e4SLinus Torvalds { 1284e682adf0SFan Du int ret; 1285e682adf0SFan Du 12861da177e4SLinus Torvalds switch (p->share) { 12871da177e4SLinus Torvalds case XFRM_SHARE_ANY: 12881da177e4SLinus Torvalds case XFRM_SHARE_SESSION: 12891da177e4SLinus Torvalds case XFRM_SHARE_USER: 12901da177e4SLinus Torvalds case XFRM_SHARE_UNIQUE: 12911da177e4SLinus Torvalds break; 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds default: 12941da177e4SLinus Torvalds return -EINVAL; 12953ff50b79SStephen Hemminger } 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds switch (p->action) { 12981da177e4SLinus Torvalds case XFRM_POLICY_ALLOW: 12991da177e4SLinus Torvalds case XFRM_POLICY_BLOCK: 13001da177e4SLinus Torvalds break; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds default: 13031da177e4SLinus Torvalds return -EINVAL; 13043ff50b79SStephen Hemminger } 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds switch (p->sel.family) { 13071da177e4SLinus Torvalds case AF_INET: 13081da177e4SLinus Torvalds break; 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds case AF_INET6: 1311dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 13121da177e4SLinus Torvalds break; 13131da177e4SLinus Torvalds #else 13141da177e4SLinus Torvalds return -EAFNOSUPPORT; 13151da177e4SLinus Torvalds #endif 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds default: 13181da177e4SLinus Torvalds return -EINVAL; 13193ff50b79SStephen Hemminger } 13201da177e4SLinus Torvalds 1321e682adf0SFan Du ret = verify_policy_dir(p->dir); 1322e682adf0SFan Du if (ret) 1323e682adf0SFan Du return ret; 1324e682adf0SFan Du if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir)) 1325e682adf0SFan Du return -EINVAL; 1326e682adf0SFan Du 1327e682adf0SFan Du return 0; 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds 13305424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) 1331df71837dSTrent Jaeger { 13325424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1333df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx; 1334df71837dSTrent Jaeger 1335df71837dSTrent Jaeger if (!rt) 1336df71837dSTrent Jaeger return 0; 1337df71837dSTrent Jaeger 13385424f32eSThomas Graf uctx = nla_data(rt); 133952a4c640SNikolay Aleksandrov return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL); 1340df71837dSTrent Jaeger } 1341df71837dSTrent Jaeger 13421da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, 13431da177e4SLinus Torvalds int nr) 13441da177e4SLinus Torvalds { 13451da177e4SLinus Torvalds int i; 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds xp->xfrm_nr = nr; 13481da177e4SLinus Torvalds for (i = 0; i < nr; i++, ut++) { 13491da177e4SLinus Torvalds struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); 13521da177e4SLinus Torvalds memcpy(&t->saddr, &ut->saddr, 13531da177e4SLinus Torvalds sizeof(xfrm_address_t)); 13541da177e4SLinus Torvalds t->reqid = ut->reqid; 13551da177e4SLinus Torvalds t->mode = ut->mode; 13561da177e4SLinus Torvalds t->share = ut->share; 13571da177e4SLinus Torvalds t->optional = ut->optional; 13581da177e4SLinus Torvalds t->aalgos = ut->aalgos; 13591da177e4SLinus Torvalds t->ealgos = ut->ealgos; 13601da177e4SLinus Torvalds t->calgos = ut->calgos; 1361c5d18e98SHerbert Xu /* If all masks are ~0, then we allow all algorithms. */ 1362c5d18e98SHerbert Xu t->allalgs = !~(t->aalgos & t->ealgos & t->calgos); 13638511d01dSMiika Komu t->encap_family = ut->family; 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 1367b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family) 1368b4ad86bfSDavid S. Miller { 1369b4ad86bfSDavid S. Miller int i; 1370b4ad86bfSDavid S. Miller 1371b4ad86bfSDavid S. Miller if (nr > XFRM_MAX_DEPTH) 1372b4ad86bfSDavid S. Miller return -EINVAL; 1373b4ad86bfSDavid S. Miller 1374b4ad86bfSDavid S. Miller for (i = 0; i < nr; i++) { 1375b4ad86bfSDavid S. Miller /* We never validated the ut->family value, so many 1376b4ad86bfSDavid S. Miller * applications simply leave it at zero. The check was 1377b4ad86bfSDavid S. Miller * never made and ut->family was ignored because all 1378b4ad86bfSDavid S. Miller * templates could be assumed to have the same family as 1379b4ad86bfSDavid S. Miller * the policy itself. Now that we will have ipv4-in-ipv6 1380b4ad86bfSDavid S. Miller * and ipv6-in-ipv4 tunnels, this is no longer true. 1381b4ad86bfSDavid S. Miller */ 1382b4ad86bfSDavid S. Miller if (!ut[i].family) 1383b4ad86bfSDavid S. Miller ut[i].family = family; 1384b4ad86bfSDavid S. Miller 1385b4ad86bfSDavid S. Miller switch (ut[i].family) { 1386b4ad86bfSDavid S. Miller case AF_INET: 1387b4ad86bfSDavid S. Miller break; 1388dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1389b4ad86bfSDavid S. Miller case AF_INET6: 1390b4ad86bfSDavid S. Miller break; 1391b4ad86bfSDavid S. Miller #endif 1392b4ad86bfSDavid S. Miller default: 1393b4ad86bfSDavid S. Miller return -EINVAL; 13943ff50b79SStephen Hemminger } 1395b4ad86bfSDavid S. Miller } 1396b4ad86bfSDavid S. Miller 1397b4ad86bfSDavid S. Miller return 0; 1398b4ad86bfSDavid S. Miller } 1399b4ad86bfSDavid S. Miller 14005424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs) 14011da177e4SLinus Torvalds { 14025424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_TMPL]; 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds if (!rt) { 14051da177e4SLinus Torvalds pol->xfrm_nr = 0; 14061da177e4SLinus Torvalds } else { 14075424f32eSThomas Graf struct xfrm_user_tmpl *utmpl = nla_data(rt); 14085424f32eSThomas Graf int nr = nla_len(rt) / sizeof(*utmpl); 1409b4ad86bfSDavid S. Miller int err; 14101da177e4SLinus Torvalds 1411b4ad86bfSDavid S. Miller err = validate_tmpl(nr, utmpl, pol->family); 1412b4ad86bfSDavid S. Miller if (err) 1413b4ad86bfSDavid S. Miller return err; 14141da177e4SLinus Torvalds 14155424f32eSThomas Graf copy_templates(pol, utmpl, nr); 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds return 0; 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14205424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs) 1421f7b6983fSMasahide NAKAMURA { 14225424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; 1423f7b6983fSMasahide NAKAMURA struct xfrm_userpolicy_type *upt; 1424b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 1425f7b6983fSMasahide NAKAMURA int err; 1426f7b6983fSMasahide NAKAMURA 1427f7b6983fSMasahide NAKAMURA if (rt) { 14285424f32eSThomas Graf upt = nla_data(rt); 1429f7b6983fSMasahide NAKAMURA type = upt->type; 1430f7b6983fSMasahide NAKAMURA } 1431f7b6983fSMasahide NAKAMURA 1432f7b6983fSMasahide NAKAMURA err = verify_policy_type(type); 1433f7b6983fSMasahide NAKAMURA if (err) 1434f7b6983fSMasahide NAKAMURA return err; 1435f7b6983fSMasahide NAKAMURA 1436f7b6983fSMasahide NAKAMURA *tp = type; 1437f7b6983fSMasahide NAKAMURA return 0; 1438f7b6983fSMasahide NAKAMURA } 1439f7b6983fSMasahide NAKAMURA 14401da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) 14411da177e4SLinus Torvalds { 14421da177e4SLinus Torvalds xp->priority = p->priority; 14431da177e4SLinus Torvalds xp->index = p->index; 14441da177e4SLinus Torvalds memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); 14451da177e4SLinus Torvalds memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); 14461da177e4SLinus Torvalds xp->action = p->action; 14471da177e4SLinus Torvalds xp->flags = p->flags; 14481da177e4SLinus Torvalds xp->family = p->sel.family; 14491da177e4SLinus Torvalds /* XXX xp->share = p->share; */ 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) 14531da177e4SLinus Torvalds { 14547b789836SMathias Krause memset(p, 0, sizeof(*p)); 14551da177e4SLinus Torvalds memcpy(&p->sel, &xp->selector, sizeof(p->sel)); 14561da177e4SLinus Torvalds memcpy(&p->lft, &xp->lft, sizeof(p->lft)); 14571da177e4SLinus Torvalds memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); 14581da177e4SLinus Torvalds p->priority = xp->priority; 14591da177e4SLinus Torvalds p->index = xp->index; 14601da177e4SLinus Torvalds p->sel.family = xp->family; 14611da177e4SLinus Torvalds p->dir = dir; 14621da177e4SLinus Torvalds p->action = xp->action; 14631da177e4SLinus Torvalds p->flags = xp->flags; 14641da177e4SLinus Torvalds p->share = XFRM_SHARE_ANY; /* XXX xp->share */ 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds 1467fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp) 14681da177e4SLinus Torvalds { 1469fc34acd3SAlexey Dobriyan struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL); 14701da177e4SLinus Torvalds int err; 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds if (!xp) { 14731da177e4SLinus Torvalds *errp = -ENOMEM; 14741da177e4SLinus Torvalds return NULL; 14751da177e4SLinus Torvalds } 14761da177e4SLinus Torvalds 14771da177e4SLinus Torvalds copy_from_user_policy(xp, p); 1478df71837dSTrent Jaeger 147935a7aa08SThomas Graf err = copy_from_user_policy_type(&xp->type, attrs); 1480f7b6983fSMasahide NAKAMURA if (err) 1481f7b6983fSMasahide NAKAMURA goto error; 1482f7b6983fSMasahide NAKAMURA 148335a7aa08SThomas Graf if (!(err = copy_from_user_tmpl(xp, attrs))) 148435a7aa08SThomas Graf err = copy_from_user_sec_ctx(xp, attrs); 1485f7b6983fSMasahide NAKAMURA if (err) 1486f7b6983fSMasahide NAKAMURA goto error; 14871da177e4SLinus Torvalds 1488295fae56SJamal Hadi Salim xfrm_mark_get(attrs, &xp->mark); 1489295fae56SJamal Hadi Salim 14901da177e4SLinus Torvalds return xp; 1491f7b6983fSMasahide NAKAMURA error: 1492f7b6983fSMasahide NAKAMURA *errp = err; 149312a169e7SHerbert Xu xp->walk.dead = 1; 149464c31b3fSWANG Cong xfrm_policy_destroy(xp); 1495f7b6983fSMasahide NAKAMURA return NULL; 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds 149822e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 14995424f32eSThomas Graf struct nlattr **attrs) 15001da177e4SLinus Torvalds { 1501fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 15027b67c857SThomas Graf struct xfrm_userpolicy_info *p = nlmsg_data(nlh); 15031da177e4SLinus Torvalds struct xfrm_policy *xp; 150426b15dadSJamal Hadi Salim struct km_event c; 15051da177e4SLinus Torvalds int err; 15061da177e4SLinus Torvalds int excl; 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds err = verify_newpolicy_info(p); 15091da177e4SLinus Torvalds if (err) 15101da177e4SLinus Torvalds return err; 151135a7aa08SThomas Graf err = verify_sec_ctx_len(attrs); 1512df71837dSTrent Jaeger if (err) 1513df71837dSTrent Jaeger return err; 15141da177e4SLinus Torvalds 1515fc34acd3SAlexey Dobriyan xp = xfrm_policy_construct(net, p, attrs, &err); 15161da177e4SLinus Torvalds if (!xp) 15171da177e4SLinus Torvalds return err; 15181da177e4SLinus Torvalds 151925985edcSLucas De Marchi /* shouldn't excl be based on nlh flags?? 152026b15dadSJamal Hadi Salim * Aha! this is anti-netlink really i.e more pfkey derived 152126b15dadSJamal Hadi Salim * in netlink excl is a flag and you wouldnt need 152226b15dadSJamal Hadi Salim * a type XFRM_MSG_UPDPOLICY - JHS */ 15231da177e4SLinus Torvalds excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; 15241da177e4SLinus Torvalds err = xfrm_policy_insert(p->dir, xp, excl); 15252e71029eSTetsuo Handa xfrm_audit_policy_add(xp, err ? 0 : 1, true); 1526161a09e7SJoy Latten 15271da177e4SLinus Torvalds if (err) { 152803e1ad7bSPaul Moore security_xfrm_policy_free(xp->security); 15291da177e4SLinus Torvalds kfree(xp); 15301da177e4SLinus Torvalds return err; 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds 1533f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 153426b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 153515e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 153626b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 153726b15dadSJamal Hadi Salim 15381da177e4SLinus Torvalds xfrm_pol_put(xp); 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds return 0; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) 15441da177e4SLinus Torvalds { 15451da177e4SLinus Torvalds struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; 15461da177e4SLinus Torvalds int i; 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds if (xp->xfrm_nr == 0) 15491da177e4SLinus Torvalds return 0; 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds for (i = 0; i < xp->xfrm_nr; i++) { 15521da177e4SLinus Torvalds struct xfrm_user_tmpl *up = &vec[i]; 15531da177e4SLinus Torvalds struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; 15541da177e4SLinus Torvalds 15551f86840fSMathias Krause memset(up, 0, sizeof(*up)); 15561da177e4SLinus Torvalds memcpy(&up->id, &kp->id, sizeof(up->id)); 15578511d01dSMiika Komu up->family = kp->encap_family; 15581da177e4SLinus Torvalds memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); 15591da177e4SLinus Torvalds up->reqid = kp->reqid; 15601da177e4SLinus Torvalds up->mode = kp->mode; 15611da177e4SLinus Torvalds up->share = kp->share; 15621da177e4SLinus Torvalds up->optional = kp->optional; 15631da177e4SLinus Torvalds up->aalgos = kp->aalgos; 15641da177e4SLinus Torvalds up->ealgos = kp->ealgos; 15651da177e4SLinus Torvalds up->calgos = kp->calgos; 15661da177e4SLinus Torvalds } 15671da177e4SLinus Torvalds 1568c0144beaSThomas Graf return nla_put(skb, XFRMA_TMPL, 1569c0144beaSThomas Graf sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); 1570df71837dSTrent Jaeger } 1571df71837dSTrent Jaeger 15720d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) 15730d681623SSerge Hallyn { 15740d681623SSerge Hallyn if (x->security) { 15750d681623SSerge Hallyn return copy_sec_ctx(x->security, skb); 15760d681623SSerge Hallyn } 15770d681623SSerge Hallyn return 0; 15780d681623SSerge Hallyn } 15790d681623SSerge Hallyn 15800d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) 15810d681623SSerge Hallyn { 15821d1e34ddSDavid S. Miller if (xp->security) 15830d681623SSerge Hallyn return copy_sec_ctx(xp->security, skb); 15840d681623SSerge Hallyn return 0; 15850d681623SSerge Hallyn } 1586cfbfd45aSThomas Graf static inline size_t userpolicy_type_attrsize(void) 1587cfbfd45aSThomas Graf { 1588cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY 1589cfbfd45aSThomas Graf return nla_total_size(sizeof(struct xfrm_userpolicy_type)); 1590cfbfd45aSThomas Graf #else 1591cfbfd45aSThomas Graf return 0; 1592cfbfd45aSThomas Graf #endif 1593cfbfd45aSThomas Graf } 15940d681623SSerge Hallyn 1595f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 1596b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1597f7b6983fSMasahide NAKAMURA { 1598c0144beaSThomas Graf struct xfrm_userpolicy_type upt = { 1599c0144beaSThomas Graf .type = type, 1600c0144beaSThomas Graf }; 1601f7b6983fSMasahide NAKAMURA 1602c0144beaSThomas Graf return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); 1603f7b6983fSMasahide NAKAMURA } 1604f7b6983fSMasahide NAKAMURA 1605f7b6983fSMasahide NAKAMURA #else 1606b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1607f7b6983fSMasahide NAKAMURA { 1608f7b6983fSMasahide NAKAMURA return 0; 1609f7b6983fSMasahide NAKAMURA } 1610f7b6983fSMasahide NAKAMURA #endif 1611f7b6983fSMasahide NAKAMURA 16121da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) 16131da177e4SLinus Torvalds { 16141da177e4SLinus Torvalds struct xfrm_dump_info *sp = ptr; 16151da177e4SLinus Torvalds struct xfrm_userpolicy_info *p; 16161da177e4SLinus Torvalds struct sk_buff *in_skb = sp->in_skb; 16171da177e4SLinus Torvalds struct sk_buff *skb = sp->out_skb; 16181da177e4SLinus Torvalds struct nlmsghdr *nlh; 16191d1e34ddSDavid S. Miller int err; 16201da177e4SLinus Torvalds 162115e47304SEric W. Biederman nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq, 162279b8b7f4SThomas Graf XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); 162379b8b7f4SThomas Graf if (nlh == NULL) 162479b8b7f4SThomas Graf return -EMSGSIZE; 16251da177e4SLinus Torvalds 16267b67c857SThomas Graf p = nlmsg_data(nlh); 16271da177e4SLinus Torvalds copy_to_user_policy(xp, p, dir); 16281d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 16291d1e34ddSDavid S. Miller if (!err) 16301d1e34ddSDavid S. Miller err = copy_to_user_sec_ctx(xp, skb); 16311d1e34ddSDavid S. Miller if (!err) 16321d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 16331d1e34ddSDavid S. Miller if (!err) 16341d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 16351d1e34ddSDavid S. Miller if (err) { 16361d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 16371d1e34ddSDavid S. Miller return err; 16381d1e34ddSDavid S. Miller } 16399825069dSThomas Graf nlmsg_end(skb, nlh); 16401da177e4SLinus Torvalds return 0; 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds 16434c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb) 16444c563f76STimo Teras { 16454c563f76STimo Teras struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; 1646283bc9f3SFan Du struct net *net = sock_net(cb->skb->sk); 16474c563f76STimo Teras 1648283bc9f3SFan Du xfrm_policy_walk_done(walk, net); 16494c563f76STimo Teras return 0; 16504c563f76STimo Teras } 16514c563f76STimo Teras 16521da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) 16531da177e4SLinus Torvalds { 1654fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 16554c563f76STimo Teras struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; 16561da177e4SLinus Torvalds struct xfrm_dump_info info; 16571da177e4SLinus Torvalds 16584c563f76STimo Teras BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) > 16594c563f76STimo Teras sizeof(cb->args) - sizeof(cb->args[0])); 16604c563f76STimo Teras 16611da177e4SLinus Torvalds info.in_skb = cb->skb; 16621da177e4SLinus Torvalds info.out_skb = skb; 16631da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 16641da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 16654c563f76STimo Teras 16664c563f76STimo Teras if (!cb->args[0]) { 16674c563f76STimo Teras cb->args[0] = 1; 16684c563f76STimo Teras xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); 16694c563f76STimo Teras } 16704c563f76STimo Teras 1671fc34acd3SAlexey Dobriyan (void) xfrm_policy_walk(net, walk, dump_one_policy, &info); 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds return skb->len; 16741da177e4SLinus Torvalds } 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, 16771da177e4SLinus Torvalds struct xfrm_policy *xp, 16781da177e4SLinus Torvalds int dir, u32 seq) 16791da177e4SLinus Torvalds { 16801da177e4SLinus Torvalds struct xfrm_dump_info info; 16811da177e4SLinus Torvalds struct sk_buff *skb; 1682c2546372SMathias Krause int err; 16831da177e4SLinus Torvalds 16847deb2264SThomas Graf skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 16851da177e4SLinus Torvalds if (!skb) 16861da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds info.in_skb = in_skb; 16891da177e4SLinus Torvalds info.out_skb = skb; 16901da177e4SLinus Torvalds info.nlmsg_seq = seq; 16911da177e4SLinus Torvalds info.nlmsg_flags = 0; 16921da177e4SLinus Torvalds 1693c2546372SMathias Krause err = dump_one_policy(xp, dir, 0, &info); 1694c2546372SMathias Krause if (err) { 16951da177e4SLinus Torvalds kfree_skb(skb); 1696c2546372SMathias Krause return ERR_PTR(err); 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds return skb; 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds 170222e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 17035424f32eSThomas Graf struct nlattr **attrs) 17041da177e4SLinus Torvalds { 1705fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 17061da177e4SLinus Torvalds struct xfrm_policy *xp; 17071da177e4SLinus Torvalds struct xfrm_userpolicy_id *p; 1708b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 17091da177e4SLinus Torvalds int err; 171026b15dadSJamal Hadi Salim struct km_event c; 17111da177e4SLinus Torvalds int delete; 1712295fae56SJamal Hadi Salim struct xfrm_mark m; 1713295fae56SJamal Hadi Salim u32 mark = xfrm_mark_get(attrs, &m); 17141da177e4SLinus Torvalds 17157b67c857SThomas Graf p = nlmsg_data(nlh); 17161da177e4SLinus Torvalds delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; 17171da177e4SLinus Torvalds 171835a7aa08SThomas Graf err = copy_from_user_policy_type(&type, attrs); 1719f7b6983fSMasahide NAKAMURA if (err) 1720f7b6983fSMasahide NAKAMURA return err; 1721f7b6983fSMasahide NAKAMURA 17221da177e4SLinus Torvalds err = verify_policy_dir(p->dir); 17231da177e4SLinus Torvalds if (err) 17241da177e4SLinus Torvalds return err; 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds if (p->index) 1727295fae56SJamal Hadi Salim xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err); 1728df71837dSTrent Jaeger else { 17295424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 173003e1ad7bSPaul Moore struct xfrm_sec_ctx *ctx; 1731df71837dSTrent Jaeger 173235a7aa08SThomas Graf err = verify_sec_ctx_len(attrs); 1733df71837dSTrent Jaeger if (err) 1734df71837dSTrent Jaeger return err; 1735df71837dSTrent Jaeger 17362c8dd116SDenis V. Lunev ctx = NULL; 1737df71837dSTrent Jaeger if (rt) { 17385424f32eSThomas Graf struct xfrm_user_sec_ctx *uctx = nla_data(rt); 1739df71837dSTrent Jaeger 174052a4c640SNikolay Aleksandrov err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 174103e1ad7bSPaul Moore if (err) 1742df71837dSTrent Jaeger return err; 17432c8dd116SDenis V. Lunev } 1744295fae56SJamal Hadi Salim xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel, 17456f26b61eSJamal Hadi Salim ctx, delete, &err); 174603e1ad7bSPaul Moore security_xfrm_policy_free(ctx); 1747df71837dSTrent Jaeger } 17481da177e4SLinus Torvalds if (xp == NULL) 17491da177e4SLinus Torvalds return -ENOENT; 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds if (!delete) { 17521da177e4SLinus Torvalds struct sk_buff *resp_skb; 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); 17551da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 17561da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 17571da177e4SLinus Torvalds } else { 1758a6483b79SAlexey Dobriyan err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, 175915e47304SEric W. Biederman NETLINK_CB(skb).portid); 17601da177e4SLinus Torvalds } 176126b15dadSJamal Hadi Salim } else { 17622e71029eSTetsuo Handa xfrm_audit_policy_delete(xp, err ? 0 : 1, true); 176313fcfbb0SDavid S. Miller 176413fcfbb0SDavid S. Miller if (err != 0) 1765c8c05a8eSCatherine Zhang goto out; 176613fcfbb0SDavid S. Miller 1767e7443892SHerbert Xu c.data.byid = p->index; 1768f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 176926b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 177015e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 177126b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds 1774c8c05a8eSCatherine Zhang out: 1775ef41aaa0SEric Paris xfrm_pol_put(xp); 1776e4c17216SPaul Moore if (delete && err == 0) 1777e4c17216SPaul Moore xfrm_garbage_collect(net); 17781da177e4SLinus Torvalds return err; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 178122e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 17825424f32eSThomas Graf struct nlattr **attrs) 17831da177e4SLinus Torvalds { 1784fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 178526b15dadSJamal Hadi Salim struct km_event c; 17867b67c857SThomas Graf struct xfrm_usersa_flush *p = nlmsg_data(nlh); 17874aa2e62cSJoy Latten int err; 17881da177e4SLinus Torvalds 17892e71029eSTetsuo Handa err = xfrm_state_flush(net, p->proto, true); 17909e64cc95SJamal Hadi Salim if (err) { 17919e64cc95SJamal Hadi Salim if (err == -ESRCH) /* empty table */ 17929e64cc95SJamal Hadi Salim return 0; 1793069c474eSDavid S. Miller return err; 17949e64cc95SJamal Hadi Salim } 1795bf08867fSHerbert Xu c.data.proto = p->proto; 1796f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 179726b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 179815e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 17997067802eSAlexey Dobriyan c.net = net; 180026b15dadSJamal Hadi Salim km_state_notify(NULL, &c); 180126b15dadSJamal Hadi Salim 18021da177e4SLinus Torvalds return 0; 18031da177e4SLinus Torvalds } 18041da177e4SLinus Torvalds 1805d8647b79SSteffen Klassert static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x) 18067deb2264SThomas Graf { 1807d8647b79SSteffen Klassert size_t replay_size = x->replay_esn ? 1808d8647b79SSteffen Klassert xfrm_replay_state_esn_len(x->replay_esn) : 1809d8647b79SSteffen Klassert sizeof(struct xfrm_replay_state); 1810d8647b79SSteffen Klassert 18117deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) 1812d8647b79SSteffen Klassert + nla_total_size(replay_size) 18137deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_lifetime_cur)) 18146f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 18157deb2264SThomas Graf + nla_total_size(4) /* XFRM_AE_RTHR */ 18167deb2264SThomas Graf + nla_total_size(4); /* XFRM_AE_ETHR */ 18177deb2264SThomas Graf } 1818d51d081dSJamal Hadi Salim 1819214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 1820d51d081dSJamal Hadi Salim { 1821d51d081dSJamal Hadi Salim struct xfrm_aevent_id *id; 1822d51d081dSJamal Hadi Salim struct nlmsghdr *nlh; 18231d1e34ddSDavid S. Miller int err; 1824d51d081dSJamal Hadi Salim 182515e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); 182679b8b7f4SThomas Graf if (nlh == NULL) 182779b8b7f4SThomas Graf return -EMSGSIZE; 1828d51d081dSJamal Hadi Salim 18297b67c857SThomas Graf id = nlmsg_data(nlh); 18302b5f6dccSJamal Hadi Salim memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr)); 1831d51d081dSJamal Hadi Salim id->sa_id.spi = x->id.spi; 1832d51d081dSJamal Hadi Salim id->sa_id.family = x->props.family; 1833d51d081dSJamal Hadi Salim id->sa_id.proto = x->id.proto; 18342b5f6dccSJamal Hadi Salim memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr)); 18352b5f6dccSJamal Hadi Salim id->reqid = x->props.reqid; 1836d51d081dSJamal Hadi Salim id->flags = c->data.aevent; 1837d51d081dSJamal Hadi Salim 1838d0fde795SDavid S. Miller if (x->replay_esn) { 18391d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 1840d8647b79SSteffen Klassert xfrm_replay_state_esn_len(x->replay_esn), 18411d1e34ddSDavid S. Miller x->replay_esn); 1842d0fde795SDavid S. Miller } else { 18431d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 18441d1e34ddSDavid S. Miller &x->replay); 1845d0fde795SDavid S. Miller } 18461d1e34ddSDavid S. Miller if (err) 18471d1e34ddSDavid S. Miller goto out_cancel; 18481d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft); 18491d1e34ddSDavid S. Miller if (err) 18501d1e34ddSDavid S. Miller goto out_cancel; 1851d8647b79SSteffen Klassert 18521d1e34ddSDavid S. Miller if (id->flags & XFRM_AE_RTHR) { 18531d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); 18541d1e34ddSDavid S. Miller if (err) 18551d1e34ddSDavid S. Miller goto out_cancel; 18561d1e34ddSDavid S. Miller } 18571d1e34ddSDavid S. Miller if (id->flags & XFRM_AE_ETHR) { 18581d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_ETIMER_THRESH, 18591d1e34ddSDavid S. Miller x->replay_maxage * 10 / HZ); 18601d1e34ddSDavid S. Miller if (err) 18611d1e34ddSDavid S. Miller goto out_cancel; 18621d1e34ddSDavid S. Miller } 18631d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &x->mark); 18641d1e34ddSDavid S. Miller if (err) 18651d1e34ddSDavid S. Miller goto out_cancel; 18666f26b61eSJamal Hadi Salim 1867053c095aSJohannes Berg nlmsg_end(skb, nlh); 1868053c095aSJohannes Berg return 0; 1869d51d081dSJamal Hadi Salim 18701d1e34ddSDavid S. Miller out_cancel: 18719825069dSThomas Graf nlmsg_cancel(skb, nlh); 18721d1e34ddSDavid S. Miller return err; 1873d51d081dSJamal Hadi Salim } 1874d51d081dSJamal Hadi Salim 187522e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 18765424f32eSThomas Graf struct nlattr **attrs) 1877d51d081dSJamal Hadi Salim { 1878fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 1879d51d081dSJamal Hadi Salim struct xfrm_state *x; 1880d51d081dSJamal Hadi Salim struct sk_buff *r_skb; 1881d51d081dSJamal Hadi Salim int err; 1882d51d081dSJamal Hadi Salim struct km_event c; 18836f26b61eSJamal Hadi Salim u32 mark; 18846f26b61eSJamal Hadi Salim struct xfrm_mark m; 18857b67c857SThomas Graf struct xfrm_aevent_id *p = nlmsg_data(nlh); 1886d51d081dSJamal Hadi Salim struct xfrm_usersa_id *id = &p->sa_id; 1887d51d081dSJamal Hadi Salim 18886f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 18896f26b61eSJamal Hadi Salim 18906f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family); 1891d8647b79SSteffen Klassert if (x == NULL) 1892d51d081dSJamal Hadi Salim return -ESRCH; 1893d8647b79SSteffen Klassert 1894d8647b79SSteffen Klassert r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 1895d8647b79SSteffen Klassert if (r_skb == NULL) { 1896d8647b79SSteffen Klassert xfrm_state_put(x); 1897d8647b79SSteffen Klassert return -ENOMEM; 1898d51d081dSJamal Hadi Salim } 1899d51d081dSJamal Hadi Salim 1900d51d081dSJamal Hadi Salim /* 1901d51d081dSJamal Hadi Salim * XXX: is this lock really needed - none of the other 1902d51d081dSJamal Hadi Salim * gets lock (the concern is things getting updated 1903d51d081dSJamal Hadi Salim * while we are still reading) - jhs 1904d51d081dSJamal Hadi Salim */ 1905d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 1906d51d081dSJamal Hadi Salim c.data.aevent = p->flags; 1907d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 190815e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 1909d51d081dSJamal Hadi Salim 1910d51d081dSJamal Hadi Salim if (build_aevent(r_skb, x, &c) < 0) 1911d51d081dSJamal Hadi Salim BUG(); 191215e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid); 1913d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 1914d51d081dSJamal Hadi Salim xfrm_state_put(x); 1915d51d081dSJamal Hadi Salim return err; 1916d51d081dSJamal Hadi Salim } 1917d51d081dSJamal Hadi Salim 191822e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 19195424f32eSThomas Graf struct nlattr **attrs) 1920d51d081dSJamal Hadi Salim { 1921fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 1922d51d081dSJamal Hadi Salim struct xfrm_state *x; 1923d51d081dSJamal Hadi Salim struct km_event c; 1924d51d081dSJamal Hadi Salim int err = -EINVAL; 19256f26b61eSJamal Hadi Salim u32 mark = 0; 19266f26b61eSJamal Hadi Salim struct xfrm_mark m; 19277b67c857SThomas Graf struct xfrm_aevent_id *p = nlmsg_data(nlh); 19285424f32eSThomas Graf struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 1929d8647b79SSteffen Klassert struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL]; 19305424f32eSThomas Graf struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 1931d51d081dSJamal Hadi Salim 1932d8647b79SSteffen Klassert if (!lt && !rp && !re) 1933d51d081dSJamal Hadi Salim return err; 1934d51d081dSJamal Hadi Salim 1935d51d081dSJamal Hadi Salim /* pedantic mode - thou shalt sayeth replaceth */ 1936d51d081dSJamal Hadi Salim if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) 1937d51d081dSJamal Hadi Salim return err; 1938d51d081dSJamal Hadi Salim 19396f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 19406f26b61eSJamal Hadi Salim 19416f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); 1942d51d081dSJamal Hadi Salim if (x == NULL) 1943d51d081dSJamal Hadi Salim return -ESRCH; 1944d51d081dSJamal Hadi Salim 1945d51d081dSJamal Hadi Salim if (x->km.state != XFRM_STATE_VALID) 1946d51d081dSJamal Hadi Salim goto out; 1947d51d081dSJamal Hadi Salim 19484479ff76SSteffen Klassert err = xfrm_replay_verify_len(x->replay_esn, re); 1949e2b19125SSteffen Klassert if (err) 1950e2b19125SSteffen Klassert goto out; 1951e2b19125SSteffen Klassert 1952d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 1953e3ac104dSMathias Krause xfrm_update_ae_params(x, attrs, 1); 1954d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 1955d51d081dSJamal Hadi Salim 1956d51d081dSJamal Hadi Salim c.event = nlh->nlmsg_type; 1957d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 195815e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 1959d51d081dSJamal Hadi Salim c.data.aevent = XFRM_AE_CU; 1960d51d081dSJamal Hadi Salim km_state_notify(x, &c); 1961d51d081dSJamal Hadi Salim err = 0; 1962d51d081dSJamal Hadi Salim out: 1963d51d081dSJamal Hadi Salim xfrm_state_put(x); 1964d51d081dSJamal Hadi Salim return err; 1965d51d081dSJamal Hadi Salim } 1966d51d081dSJamal Hadi Salim 196722e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 19685424f32eSThomas Graf struct nlattr **attrs) 19691da177e4SLinus Torvalds { 1970fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 197126b15dadSJamal Hadi Salim struct km_event c; 1972b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 1973f7b6983fSMasahide NAKAMURA int err; 197426b15dadSJamal Hadi Salim 197535a7aa08SThomas Graf err = copy_from_user_policy_type(&type, attrs); 1976f7b6983fSMasahide NAKAMURA if (err) 1977f7b6983fSMasahide NAKAMURA return err; 1978f7b6983fSMasahide NAKAMURA 19792e71029eSTetsuo Handa err = xfrm_policy_flush(net, type, true); 19802f1eb65fSJamal Hadi Salim if (err) { 19812f1eb65fSJamal Hadi Salim if (err == -ESRCH) /* empty table */ 19822f1eb65fSJamal Hadi Salim return 0; 1983069c474eSDavid S. Miller return err; 19842f1eb65fSJamal Hadi Salim } 19852f1eb65fSJamal Hadi Salim 1986f7b6983fSMasahide NAKAMURA c.data.type = type; 1987f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 198826b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 198915e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 19907067802eSAlexey Dobriyan c.net = net; 199126b15dadSJamal Hadi Salim km_policy_notify(NULL, 0, &c); 19921da177e4SLinus Torvalds return 0; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 199522e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 19965424f32eSThomas Graf struct nlattr **attrs) 19976c5c8ca7SJamal Hadi Salim { 1998fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 19996c5c8ca7SJamal Hadi Salim struct xfrm_policy *xp; 20007b67c857SThomas Graf struct xfrm_user_polexpire *up = nlmsg_data(nlh); 20016c5c8ca7SJamal Hadi Salim struct xfrm_userpolicy_info *p = &up->pol; 2002b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 20036c5c8ca7SJamal Hadi Salim int err = -ENOENT; 2004295fae56SJamal Hadi Salim struct xfrm_mark m; 2005295fae56SJamal Hadi Salim u32 mark = xfrm_mark_get(attrs, &m); 20066c5c8ca7SJamal Hadi Salim 200735a7aa08SThomas Graf err = copy_from_user_policy_type(&type, attrs); 2008f7b6983fSMasahide NAKAMURA if (err) 2009f7b6983fSMasahide NAKAMURA return err; 2010f7b6983fSMasahide NAKAMURA 2011c8bf4d04STimo Teräs err = verify_policy_dir(p->dir); 2012c8bf4d04STimo Teräs if (err) 2013c8bf4d04STimo Teräs return err; 2014c8bf4d04STimo Teräs 20156c5c8ca7SJamal Hadi Salim if (p->index) 2016295fae56SJamal Hadi Salim xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err); 20176c5c8ca7SJamal Hadi Salim else { 20185424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 201903e1ad7bSPaul Moore struct xfrm_sec_ctx *ctx; 20206c5c8ca7SJamal Hadi Salim 202135a7aa08SThomas Graf err = verify_sec_ctx_len(attrs); 20226c5c8ca7SJamal Hadi Salim if (err) 20236c5c8ca7SJamal Hadi Salim return err; 20246c5c8ca7SJamal Hadi Salim 20252c8dd116SDenis V. Lunev ctx = NULL; 20266c5c8ca7SJamal Hadi Salim if (rt) { 20275424f32eSThomas Graf struct xfrm_user_sec_ctx *uctx = nla_data(rt); 20286c5c8ca7SJamal Hadi Salim 202952a4c640SNikolay Aleksandrov err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 203003e1ad7bSPaul Moore if (err) 20316c5c8ca7SJamal Hadi Salim return err; 20322c8dd116SDenis V. Lunev } 2033295fae56SJamal Hadi Salim xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, 20346f26b61eSJamal Hadi Salim &p->sel, ctx, 0, &err); 203503e1ad7bSPaul Moore security_xfrm_policy_free(ctx); 20366c5c8ca7SJamal Hadi Salim } 20376c5c8ca7SJamal Hadi Salim if (xp == NULL) 2038ef41aaa0SEric Paris return -ENOENT; 203903e1ad7bSPaul Moore 2040ea2dea9dSTimo Teräs if (unlikely(xp->walk.dead)) 20416c5c8ca7SJamal Hadi Salim goto out; 20426c5c8ca7SJamal Hadi Salim 20436c5c8ca7SJamal Hadi Salim err = 0; 20446c5c8ca7SJamal Hadi Salim if (up->hard) { 20456c5c8ca7SJamal Hadi Salim xfrm_policy_delete(xp, p->dir); 20462e71029eSTetsuo Handa xfrm_audit_policy_delete(xp, 1, true); 20476c5c8ca7SJamal Hadi Salim } else { 20486c5c8ca7SJamal Hadi Salim // reset the timers here? 20490c199a90SJakub Wilk WARN(1, "Don't know what to do with soft policy expire\n"); 20506c5c8ca7SJamal Hadi Salim } 2051c6bb8136SEric W. Biederman km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid); 20526c5c8ca7SJamal Hadi Salim 20536c5c8ca7SJamal Hadi Salim out: 20546c5c8ca7SJamal Hadi Salim xfrm_pol_put(xp); 20556c5c8ca7SJamal Hadi Salim return err; 20566c5c8ca7SJamal Hadi Salim } 20576c5c8ca7SJamal Hadi Salim 205822e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 20595424f32eSThomas Graf struct nlattr **attrs) 206053bc6b4dSJamal Hadi Salim { 2061fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 206253bc6b4dSJamal Hadi Salim struct xfrm_state *x; 206353bc6b4dSJamal Hadi Salim int err; 20647b67c857SThomas Graf struct xfrm_user_expire *ue = nlmsg_data(nlh); 206553bc6b4dSJamal Hadi Salim struct xfrm_usersa_info *p = &ue->state; 20666f26b61eSJamal Hadi Salim struct xfrm_mark m; 2067928497f0SNicolas Dichtel u32 mark = xfrm_mark_get(attrs, &m); 206853bc6b4dSJamal Hadi Salim 20696f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family); 207053bc6b4dSJamal Hadi Salim 20713a765aa5SDavid S. Miller err = -ENOENT; 207253bc6b4dSJamal Hadi Salim if (x == NULL) 207353bc6b4dSJamal Hadi Salim return err; 207453bc6b4dSJamal Hadi Salim 207553bc6b4dSJamal Hadi Salim spin_lock_bh(&x->lock); 20763a765aa5SDavid S. Miller err = -EINVAL; 207753bc6b4dSJamal Hadi Salim if (x->km.state != XFRM_STATE_VALID) 207853bc6b4dSJamal Hadi Salim goto out; 2079c6bb8136SEric W. Biederman km_state_expired(x, ue->hard, nlh->nlmsg_pid); 208053bc6b4dSJamal Hadi Salim 2081161a09e7SJoy Latten if (ue->hard) { 208253bc6b4dSJamal Hadi Salim __xfrm_state_delete(x); 20832e71029eSTetsuo Handa xfrm_audit_state_delete(x, 1, true); 2084161a09e7SJoy Latten } 20853a765aa5SDavid S. Miller err = 0; 208653bc6b4dSJamal Hadi Salim out: 208753bc6b4dSJamal Hadi Salim spin_unlock_bh(&x->lock); 208853bc6b4dSJamal Hadi Salim xfrm_state_put(x); 208953bc6b4dSJamal Hadi Salim return err; 209053bc6b4dSJamal Hadi Salim } 209153bc6b4dSJamal Hadi Salim 209222e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, 20935424f32eSThomas Graf struct nlattr **attrs) 2094980ebd25SJamal Hadi Salim { 2095fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 2096980ebd25SJamal Hadi Salim struct xfrm_policy *xp; 2097980ebd25SJamal Hadi Salim struct xfrm_user_tmpl *ut; 2098980ebd25SJamal Hadi Salim int i; 20995424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_TMPL]; 21006f26b61eSJamal Hadi Salim struct xfrm_mark mark; 2101980ebd25SJamal Hadi Salim 21027b67c857SThomas Graf struct xfrm_user_acquire *ua = nlmsg_data(nlh); 2103fc34acd3SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 2104980ebd25SJamal Hadi Salim int err = -ENOMEM; 2105980ebd25SJamal Hadi Salim 2106980ebd25SJamal Hadi Salim if (!x) 2107d8eb9307SIlpo Järvinen goto nomem; 2108980ebd25SJamal Hadi Salim 21096f26b61eSJamal Hadi Salim xfrm_mark_get(attrs, &mark); 21106f26b61eSJamal Hadi Salim 2111980ebd25SJamal Hadi Salim err = verify_newpolicy_info(&ua->policy); 2112d8eb9307SIlpo Järvinen if (err) 2113d8eb9307SIlpo Järvinen goto bad_policy; 2114980ebd25SJamal Hadi Salim 2115980ebd25SJamal Hadi Salim /* build an XP */ 2116fc34acd3SAlexey Dobriyan xp = xfrm_policy_construct(net, &ua->policy, attrs, &err); 2117d8eb9307SIlpo Järvinen if (!xp) 2118d8eb9307SIlpo Järvinen goto free_state; 2119980ebd25SJamal Hadi Salim 2120980ebd25SJamal Hadi Salim memcpy(&x->id, &ua->id, sizeof(ua->id)); 2121980ebd25SJamal Hadi Salim memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); 2122980ebd25SJamal Hadi Salim memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); 21236f26b61eSJamal Hadi Salim xp->mark.m = x->mark.m = mark.m; 21246f26b61eSJamal Hadi Salim xp->mark.v = x->mark.v = mark.v; 21255424f32eSThomas Graf ut = nla_data(rt); 2126980ebd25SJamal Hadi Salim /* extract the templates and for each call km_key */ 2127980ebd25SJamal Hadi Salim for (i = 0; i < xp->xfrm_nr; i++, ut++) { 2128980ebd25SJamal Hadi Salim struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 2129980ebd25SJamal Hadi Salim memcpy(&x->id, &t->id, sizeof(x->id)); 2130980ebd25SJamal Hadi Salim x->props.mode = t->mode; 2131980ebd25SJamal Hadi Salim x->props.reqid = t->reqid; 2132980ebd25SJamal Hadi Salim x->props.family = ut->family; 2133980ebd25SJamal Hadi Salim t->aalgos = ua->aalgos; 2134980ebd25SJamal Hadi Salim t->ealgos = ua->ealgos; 2135980ebd25SJamal Hadi Salim t->calgos = ua->calgos; 2136980ebd25SJamal Hadi Salim err = km_query(x, t, xp); 2137980ebd25SJamal Hadi Salim 2138980ebd25SJamal Hadi Salim } 2139980ebd25SJamal Hadi Salim 2140980ebd25SJamal Hadi Salim kfree(x); 2141980ebd25SJamal Hadi Salim kfree(xp); 2142980ebd25SJamal Hadi Salim 2143980ebd25SJamal Hadi Salim return 0; 2144d8eb9307SIlpo Järvinen 2145d8eb9307SIlpo Järvinen bad_policy: 214662db5cfdSstephen hemminger WARN(1, "BAD policy passed\n"); 2147d8eb9307SIlpo Järvinen free_state: 2148d8eb9307SIlpo Järvinen kfree(x); 2149d8eb9307SIlpo Järvinen nomem: 2150d8eb9307SIlpo Järvinen return err; 2151980ebd25SJamal Hadi Salim } 2152980ebd25SJamal Hadi Salim 21535c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 21545c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma, 215513c1d189SArnaud Ebalard struct xfrm_kmaddress *k, 21565424f32eSThomas Graf struct nlattr **attrs, int *num) 21575c79de6eSShinta Sugimoto { 21585424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_MIGRATE]; 21595c79de6eSShinta Sugimoto struct xfrm_user_migrate *um; 21605c79de6eSShinta Sugimoto int i, num_migrate; 21615c79de6eSShinta Sugimoto 216213c1d189SArnaud Ebalard if (k != NULL) { 216313c1d189SArnaud Ebalard struct xfrm_user_kmaddress *uk; 216413c1d189SArnaud Ebalard 216513c1d189SArnaud Ebalard uk = nla_data(attrs[XFRMA_KMADDRESS]); 216613c1d189SArnaud Ebalard memcpy(&k->local, &uk->local, sizeof(k->local)); 216713c1d189SArnaud Ebalard memcpy(&k->remote, &uk->remote, sizeof(k->remote)); 216813c1d189SArnaud Ebalard k->family = uk->family; 216913c1d189SArnaud Ebalard k->reserved = uk->reserved; 217013c1d189SArnaud Ebalard } 217113c1d189SArnaud Ebalard 21725424f32eSThomas Graf um = nla_data(rt); 21735424f32eSThomas Graf num_migrate = nla_len(rt) / sizeof(*um); 21745c79de6eSShinta Sugimoto 21755c79de6eSShinta Sugimoto if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) 21765c79de6eSShinta Sugimoto return -EINVAL; 21775c79de6eSShinta Sugimoto 21785c79de6eSShinta Sugimoto for (i = 0; i < num_migrate; i++, um++, ma++) { 21795c79de6eSShinta Sugimoto memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); 21805c79de6eSShinta Sugimoto memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); 21815c79de6eSShinta Sugimoto memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); 21825c79de6eSShinta Sugimoto memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); 21835c79de6eSShinta Sugimoto 21845c79de6eSShinta Sugimoto ma->proto = um->proto; 21855c79de6eSShinta Sugimoto ma->mode = um->mode; 21865c79de6eSShinta Sugimoto ma->reqid = um->reqid; 21875c79de6eSShinta Sugimoto 21885c79de6eSShinta Sugimoto ma->old_family = um->old_family; 21895c79de6eSShinta Sugimoto ma->new_family = um->new_family; 21905c79de6eSShinta Sugimoto } 21915c79de6eSShinta Sugimoto 21925c79de6eSShinta Sugimoto *num = i; 21935c79de6eSShinta Sugimoto return 0; 21945c79de6eSShinta Sugimoto } 21955c79de6eSShinta Sugimoto 21965c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 21975424f32eSThomas Graf struct nlattr **attrs) 21985c79de6eSShinta Sugimoto { 21997b67c857SThomas Graf struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); 22005c79de6eSShinta Sugimoto struct xfrm_migrate m[XFRM_MAX_DEPTH]; 220113c1d189SArnaud Ebalard struct xfrm_kmaddress km, *kmp; 22025c79de6eSShinta Sugimoto u8 type; 22035c79de6eSShinta Sugimoto int err; 22045c79de6eSShinta Sugimoto int n = 0; 22058d549c4fSFan Du struct net *net = sock_net(skb->sk); 22065c79de6eSShinta Sugimoto 220735a7aa08SThomas Graf if (attrs[XFRMA_MIGRATE] == NULL) 2208cf5cb79fSThomas Graf return -EINVAL; 22095c79de6eSShinta Sugimoto 221013c1d189SArnaud Ebalard kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL; 221113c1d189SArnaud Ebalard 22125424f32eSThomas Graf err = copy_from_user_policy_type(&type, attrs); 22135c79de6eSShinta Sugimoto if (err) 22145c79de6eSShinta Sugimoto return err; 22155c79de6eSShinta Sugimoto 221613c1d189SArnaud Ebalard err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n); 22175c79de6eSShinta Sugimoto if (err) 22185c79de6eSShinta Sugimoto return err; 22195c79de6eSShinta Sugimoto 22205c79de6eSShinta Sugimoto if (!n) 22215c79de6eSShinta Sugimoto return 0; 22225c79de6eSShinta Sugimoto 22238d549c4fSFan Du xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net); 22245c79de6eSShinta Sugimoto 22255c79de6eSShinta Sugimoto return 0; 22265c79de6eSShinta Sugimoto } 22275c79de6eSShinta Sugimoto #else 22285c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 22295424f32eSThomas Graf struct nlattr **attrs) 22305c79de6eSShinta Sugimoto { 22315c79de6eSShinta Sugimoto return -ENOPROTOOPT; 22325c79de6eSShinta Sugimoto } 22335c79de6eSShinta Sugimoto #endif 22345c79de6eSShinta Sugimoto 22355c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 2236183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb) 22375c79de6eSShinta Sugimoto { 22385c79de6eSShinta Sugimoto struct xfrm_user_migrate um; 22395c79de6eSShinta Sugimoto 22405c79de6eSShinta Sugimoto memset(&um, 0, sizeof(um)); 22415c79de6eSShinta Sugimoto um.proto = m->proto; 22425c79de6eSShinta Sugimoto um.mode = m->mode; 22435c79de6eSShinta Sugimoto um.reqid = m->reqid; 22445c79de6eSShinta Sugimoto um.old_family = m->old_family; 22455c79de6eSShinta Sugimoto memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); 22465c79de6eSShinta Sugimoto memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); 22475c79de6eSShinta Sugimoto um.new_family = m->new_family; 22485c79de6eSShinta Sugimoto memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); 22495c79de6eSShinta Sugimoto memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); 22505c79de6eSShinta Sugimoto 2251c0144beaSThomas Graf return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); 22525c79de6eSShinta Sugimoto } 22535c79de6eSShinta Sugimoto 2254183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb) 225513c1d189SArnaud Ebalard { 225613c1d189SArnaud Ebalard struct xfrm_user_kmaddress uk; 225713c1d189SArnaud Ebalard 225813c1d189SArnaud Ebalard memset(&uk, 0, sizeof(uk)); 225913c1d189SArnaud Ebalard uk.family = k->family; 226013c1d189SArnaud Ebalard uk.reserved = k->reserved; 226113c1d189SArnaud Ebalard memcpy(&uk.local, &k->local, sizeof(uk.local)); 2262a1caa322SArnaud Ebalard memcpy(&uk.remote, &k->remote, sizeof(uk.remote)); 226313c1d189SArnaud Ebalard 226413c1d189SArnaud Ebalard return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk); 226513c1d189SArnaud Ebalard } 226613c1d189SArnaud Ebalard 226713c1d189SArnaud Ebalard static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma) 22687deb2264SThomas Graf { 22697deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) 227013c1d189SArnaud Ebalard + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0) 22717deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) 22727deb2264SThomas Graf + userpolicy_type_attrsize(); 22737deb2264SThomas Graf } 22747deb2264SThomas Graf 2275183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m, 2276183cad12SDavid S. Miller int num_migrate, const struct xfrm_kmaddress *k, 2277183cad12SDavid S. Miller const struct xfrm_selector *sel, u8 dir, u8 type) 22785c79de6eSShinta Sugimoto { 2279183cad12SDavid S. Miller const struct xfrm_migrate *mp; 22805c79de6eSShinta Sugimoto struct xfrm_userpolicy_id *pol_id; 22815c79de6eSShinta Sugimoto struct nlmsghdr *nlh; 22821d1e34ddSDavid S. Miller int i, err; 22835c79de6eSShinta Sugimoto 228479b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); 228579b8b7f4SThomas Graf if (nlh == NULL) 228679b8b7f4SThomas Graf return -EMSGSIZE; 22875c79de6eSShinta Sugimoto 22887b67c857SThomas Graf pol_id = nlmsg_data(nlh); 22895c79de6eSShinta Sugimoto /* copy data from selector, dir, and type to the pol_id */ 22905c79de6eSShinta Sugimoto memset(pol_id, 0, sizeof(*pol_id)); 22915c79de6eSShinta Sugimoto memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); 22925c79de6eSShinta Sugimoto pol_id->dir = dir; 22935c79de6eSShinta Sugimoto 22941d1e34ddSDavid S. Miller if (k != NULL) { 22951d1e34ddSDavid S. Miller err = copy_to_user_kmaddress(k, skb); 22961d1e34ddSDavid S. Miller if (err) 22971d1e34ddSDavid S. Miller goto out_cancel; 22981d1e34ddSDavid S. Miller } 22991d1e34ddSDavid S. Miller err = copy_to_user_policy_type(type, skb); 23001d1e34ddSDavid S. Miller if (err) 23011d1e34ddSDavid S. Miller goto out_cancel; 23025c79de6eSShinta Sugimoto for (i = 0, mp = m ; i < num_migrate; i++, mp++) { 23031d1e34ddSDavid S. Miller err = copy_to_user_migrate(mp, skb); 23041d1e34ddSDavid S. Miller if (err) 23051d1e34ddSDavid S. Miller goto out_cancel; 23065c79de6eSShinta Sugimoto } 23075c79de6eSShinta Sugimoto 2308053c095aSJohannes Berg nlmsg_end(skb, nlh); 2309053c095aSJohannes Berg return 0; 23101d1e34ddSDavid S. Miller 23111d1e34ddSDavid S. Miller out_cancel: 23129825069dSThomas Graf nlmsg_cancel(skb, nlh); 23131d1e34ddSDavid S. Miller return err; 23145c79de6eSShinta Sugimoto } 23155c79de6eSShinta Sugimoto 2316183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2317183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 2318183cad12SDavid S. Miller const struct xfrm_kmaddress *k) 23195c79de6eSShinta Sugimoto { 2320a6483b79SAlexey Dobriyan struct net *net = &init_net; 23215c79de6eSShinta Sugimoto struct sk_buff *skb; 23225c79de6eSShinta Sugimoto 232313c1d189SArnaud Ebalard skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC); 23245c79de6eSShinta Sugimoto if (skb == NULL) 23255c79de6eSShinta Sugimoto return -ENOMEM; 23265c79de6eSShinta Sugimoto 23275c79de6eSShinta Sugimoto /* build migrate */ 232813c1d189SArnaud Ebalard if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0) 23295c79de6eSShinta Sugimoto BUG(); 23305c79de6eSShinta Sugimoto 233121ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE); 23325c79de6eSShinta Sugimoto } 23335c79de6eSShinta Sugimoto #else 2334183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2335183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 2336183cad12SDavid S. Miller const struct xfrm_kmaddress *k) 23375c79de6eSShinta Sugimoto { 23385c79de6eSShinta Sugimoto return -ENOPROTOOPT; 23395c79de6eSShinta Sugimoto } 23405c79de6eSShinta Sugimoto #endif 2341d51d081dSJamal Hadi Salim 2342a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type) 2343492b558bSThomas Graf 2344492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { 234566f9a259SDavid S. Miller [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2346492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2347492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2348492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2349492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2350492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2351492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), 2352980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), 235353bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), 2354492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 235566f9a259SDavid S. Miller [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 23566c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), 2357492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), 2358a7bd9a45SThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, 2359d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2360d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 236197a64b45SMasahide NAKAMURA [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), 23625c79de6eSShinta Sugimoto [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2363a7bd9a45SThomas Graf [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), 2364880a6fabSChristophe Gouault [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 2365a7bd9a45SThomas Graf [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 23661da177e4SLinus Torvalds }; 23671da177e4SLinus Torvalds 2368492b558bSThomas Graf #undef XMSGSIZE 2369492b558bSThomas Graf 2370cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { 2371c28e9304Sjamal [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)}, 2372c28e9304Sjamal [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)}, 2373c28e9304Sjamal [XFRMA_LASTUSED] = { .type = NLA_U64}, 2374c28e9304Sjamal [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)}, 23751a6509d9SHerbert Xu [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, 2376cf5cb79fSThomas Graf [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, 2377cf5cb79fSThomas Graf [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, 2378cf5cb79fSThomas Graf [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, 2379cf5cb79fSThomas Graf [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, 2380cf5cb79fSThomas Graf [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, 2381cf5cb79fSThomas Graf [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, 2382cf5cb79fSThomas Graf [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, 2383cf5cb79fSThomas Graf [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, 2384cf5cb79fSThomas Graf [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, 2385cf5cb79fSThomas Graf [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, 2386cf5cb79fSThomas Graf [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, 2387cf5cb79fSThomas Graf [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, 2388cf5cb79fSThomas Graf [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, 2389cf5cb79fSThomas Graf [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, 239013c1d189SArnaud Ebalard [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) }, 23916f26b61eSJamal Hadi Salim [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) }, 239235d2856bSMartin Willi [XFRMA_TFCPAD] = { .type = NLA_U32 }, 2393d8647b79SSteffen Klassert [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) }, 2394a947b0a9SNicolas Dichtel [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 }, 2395d3623099SNicolas Dichtel [XFRMA_PROTO] = { .type = NLA_U8 }, 2396870a2df4SNicolas Dichtel [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) }, 2397cf5cb79fSThomas Graf }; 2398cf5cb79fSThomas Graf 2399880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = { 2400880a6fabSChristophe Gouault [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2401880a6fabSChristophe Gouault [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2402880a6fabSChristophe Gouault }; 2403880a6fabSChristophe Gouault 240405600a79SMathias Krause static const struct xfrm_link { 24055424f32eSThomas Graf int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **); 24061da177e4SLinus Torvalds int (*dump)(struct sk_buff *, struct netlink_callback *); 24074c563f76STimo Teras int (*done)(struct netlink_callback *); 2408880a6fabSChristophe Gouault const struct nla_policy *nla_pol; 2409880a6fabSChristophe Gouault int nla_max; 2410492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = { 2411492b558bSThomas Graf [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 2412492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, 2413492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, 24144c563f76STimo Teras .dump = xfrm_dump_sa, 24154c563f76STimo Teras .done = xfrm_dump_sa_done }, 2416492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2417492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, 2418492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, 24194c563f76STimo Teras .dump = xfrm_dump_policy, 24204c563f76STimo Teras .done = xfrm_dump_policy_done }, 2421492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, 2422980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, 242353bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, 2424492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2425492b558bSThomas Graf [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 24266c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, 2427492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, 2428492b558bSThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, 2429d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, 2430d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, 24315c79de6eSShinta Sugimoto [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, 243228d8909bSJamal Hadi Salim [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, 2433880a6fabSChristophe Gouault [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo, 2434880a6fabSChristophe Gouault .nla_pol = xfrma_spd_policy, 2435880a6fabSChristophe Gouault .nla_max = XFRMA_SPD_MAX }, 2436ecfd6b18SJamal Hadi Salim [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, 24371da177e4SLinus Torvalds }; 24381da177e4SLinus Torvalds 24391d00a4ebSThomas Graf static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 24401da177e4SLinus Torvalds { 2441a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 244235a7aa08SThomas Graf struct nlattr *attrs[XFRMA_MAX+1]; 244305600a79SMathias Krause const struct xfrm_link *link; 2444a7bd9a45SThomas Graf int type, err; 24451da177e4SLinus Torvalds 244674005991SFan Du #ifdef CONFIG_COMPAT 244774005991SFan Du if (is_compat_task()) 244874005991SFan Du return -ENOTSUPP; 244974005991SFan Du #endif 245074005991SFan Du 24511da177e4SLinus Torvalds type = nlh->nlmsg_type; 24521da177e4SLinus Torvalds if (type > XFRM_MSG_MAX) 24531d00a4ebSThomas Graf return -EINVAL; 24541da177e4SLinus Torvalds 24551da177e4SLinus Torvalds type -= XFRM_MSG_BASE; 24561da177e4SLinus Torvalds link = &xfrm_dispatch[type]; 24571da177e4SLinus Torvalds 24581da177e4SLinus Torvalds /* All operations require privileges, even GET */ 245990f62cf3SEric W. Biederman if (!netlink_net_capable(skb, CAP_NET_ADMIN)) 24601d00a4ebSThomas Graf return -EPERM; 24611da177e4SLinus Torvalds 2462492b558bSThomas Graf if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || 2463492b558bSThomas Graf type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && 2464b8f3ab42SDavid S. Miller (nlh->nlmsg_flags & NLM_F_DUMP)) { 24651da177e4SLinus Torvalds if (link->dump == NULL) 24661d00a4ebSThomas Graf return -EINVAL; 24671da177e4SLinus Torvalds 246880d326faSPablo Neira Ayuso { 246980d326faSPablo Neira Ayuso struct netlink_dump_control c = { 247080d326faSPablo Neira Ayuso .dump = link->dump, 247180d326faSPablo Neira Ayuso .done = link->done, 247280d326faSPablo Neira Ayuso }; 247380d326faSPablo Neira Ayuso return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c); 247480d326faSPablo Neira Ayuso } 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 2477880a6fabSChristophe Gouault err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, 2478880a6fabSChristophe Gouault link->nla_max ? : XFRMA_MAX, 2479880a6fabSChristophe Gouault link->nla_pol ? : xfrma_policy); 2480a7bd9a45SThomas Graf if (err < 0) 2481a7bd9a45SThomas Graf return err; 24821da177e4SLinus Torvalds 24831da177e4SLinus Torvalds if (link->doit == NULL) 24841d00a4ebSThomas Graf return -EINVAL; 24851da177e4SLinus Torvalds 24865424f32eSThomas Graf return link->doit(skb, nlh, attrs); 24871da177e4SLinus Torvalds } 24881da177e4SLinus Torvalds 2489cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb) 24901da177e4SLinus Torvalds { 2491283bc9f3SFan Du struct net *net = sock_net(skb->sk); 2492283bc9f3SFan Du 2493283bc9f3SFan Du mutex_lock(&net->xfrm.xfrm_cfg_mutex); 2494cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &xfrm_user_rcv_msg); 2495283bc9f3SFan Du mutex_unlock(&net->xfrm.xfrm_cfg_mutex); 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 24987deb2264SThomas Graf static inline size_t xfrm_expire_msgsize(void) 24997deb2264SThomas Graf { 25006f26b61eSJamal Hadi Salim return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) 25016f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)); 25027deb2264SThomas Graf } 25037deb2264SThomas Graf 2504214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 25051da177e4SLinus Torvalds { 25061da177e4SLinus Torvalds struct xfrm_user_expire *ue; 25071da177e4SLinus Torvalds struct nlmsghdr *nlh; 25081d1e34ddSDavid S. Miller int err; 25091da177e4SLinus Torvalds 251015e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); 251179b8b7f4SThomas Graf if (nlh == NULL) 251279b8b7f4SThomas Graf return -EMSGSIZE; 25131da177e4SLinus Torvalds 25147b67c857SThomas Graf ue = nlmsg_data(nlh); 25151da177e4SLinus Torvalds copy_to_user_state(x, &ue->state); 2516d51d081dSJamal Hadi Salim ue->hard = (c->data.hard != 0) ? 1 : 0; 25171da177e4SLinus Torvalds 25181d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &x->mark); 25191d1e34ddSDavid S. Miller if (err) 25201d1e34ddSDavid S. Miller return err; 25216f26b61eSJamal Hadi Salim 2522053c095aSJohannes Berg nlmsg_end(skb, nlh); 2523053c095aSJohannes Berg return 0; 25241da177e4SLinus Torvalds } 25251da177e4SLinus Torvalds 2526214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c) 25271da177e4SLinus Torvalds { 2528fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 25291da177e4SLinus Torvalds struct sk_buff *skb; 25301da177e4SLinus Torvalds 25317deb2264SThomas Graf skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); 25321da177e4SLinus Torvalds if (skb == NULL) 25331da177e4SLinus Torvalds return -ENOMEM; 25341da177e4SLinus Torvalds 25356f26b61eSJamal Hadi Salim if (build_expire(skb, x, c) < 0) { 25366f26b61eSJamal Hadi Salim kfree_skb(skb); 25376f26b61eSJamal Hadi Salim return -EMSGSIZE; 25386f26b61eSJamal Hadi Salim } 25391da177e4SLinus Torvalds 254021ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds 2543214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c) 2544d51d081dSJamal Hadi Salim { 2545fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 2546d51d081dSJamal Hadi Salim struct sk_buff *skb; 2547d51d081dSJamal Hadi Salim 2548d8647b79SSteffen Klassert skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 2549d51d081dSJamal Hadi Salim if (skb == NULL) 2550d51d081dSJamal Hadi Salim return -ENOMEM; 2551d51d081dSJamal Hadi Salim 2552d51d081dSJamal Hadi Salim if (build_aevent(skb, x, c) < 0) 2553d51d081dSJamal Hadi Salim BUG(); 2554d51d081dSJamal Hadi Salim 255521ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS); 2556d51d081dSJamal Hadi Salim } 2557d51d081dSJamal Hadi Salim 2558214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c) 255926b15dadSJamal Hadi Salim { 25607067802eSAlexey Dobriyan struct net *net = c->net; 256126b15dadSJamal Hadi Salim struct xfrm_usersa_flush *p; 256226b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 256326b15dadSJamal Hadi Salim struct sk_buff *skb; 25647deb2264SThomas Graf int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); 256526b15dadSJamal Hadi Salim 25667deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 256726b15dadSJamal Hadi Salim if (skb == NULL) 256826b15dadSJamal Hadi Salim return -ENOMEM; 256926b15dadSJamal Hadi Salim 257015e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); 257179b8b7f4SThomas Graf if (nlh == NULL) { 257279b8b7f4SThomas Graf kfree_skb(skb); 257379b8b7f4SThomas Graf return -EMSGSIZE; 257479b8b7f4SThomas Graf } 257526b15dadSJamal Hadi Salim 25767b67c857SThomas Graf p = nlmsg_data(nlh); 2577bf08867fSHerbert Xu p->proto = c->data.proto; 257826b15dadSJamal Hadi Salim 25799825069dSThomas Graf nlmsg_end(skb, nlh); 258026b15dadSJamal Hadi Salim 258121ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 258226b15dadSJamal Hadi Salim } 258326b15dadSJamal Hadi Salim 25847deb2264SThomas Graf static inline size_t xfrm_sa_len(struct xfrm_state *x) 258526b15dadSJamal Hadi Salim { 25867deb2264SThomas Graf size_t l = 0; 25871a6509d9SHerbert Xu if (x->aead) 25881a6509d9SHerbert Xu l += nla_total_size(aead_len(x->aead)); 25894447bb33SMartin Willi if (x->aalg) { 25904447bb33SMartin Willi l += nla_total_size(sizeof(struct xfrm_algo) + 25914447bb33SMartin Willi (x->aalg->alg_key_len + 7) / 8); 25924447bb33SMartin Willi l += nla_total_size(xfrm_alg_auth_len(x->aalg)); 25934447bb33SMartin Willi } 259426b15dadSJamal Hadi Salim if (x->ealg) 25950f99be0dSEric Dumazet l += nla_total_size(xfrm_alg_len(x->ealg)); 259626b15dadSJamal Hadi Salim if (x->calg) 25977deb2264SThomas Graf l += nla_total_size(sizeof(*x->calg)); 259826b15dadSJamal Hadi Salim if (x->encap) 25997deb2264SThomas Graf l += nla_total_size(sizeof(*x->encap)); 260035d2856bSMartin Willi if (x->tfcpad) 260135d2856bSMartin Willi l += nla_total_size(sizeof(x->tfcpad)); 2602d8647b79SSteffen Klassert if (x->replay_esn) 2603d8647b79SSteffen Klassert l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn)); 2604f293a5e3Sdingzhi else 2605f293a5e3Sdingzhi l += nla_total_size(sizeof(struct xfrm_replay_state)); 260668325d3bSHerbert Xu if (x->security) 260768325d3bSHerbert Xu l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + 260868325d3bSHerbert Xu x->security->ctx_len); 260968325d3bSHerbert Xu if (x->coaddr) 261068325d3bSHerbert Xu l += nla_total_size(sizeof(*x->coaddr)); 2611a947b0a9SNicolas Dichtel if (x->props.extra_flags) 2612a947b0a9SNicolas Dichtel l += nla_total_size(sizeof(x->props.extra_flags)); 261368325d3bSHerbert Xu 2614d26f3984SHerbert Xu /* Must count x->lastused as it may become non-zero behind our back. */ 2615d26f3984SHerbert Xu l += nla_total_size(sizeof(u64)); 261626b15dadSJamal Hadi Salim 261726b15dadSJamal Hadi Salim return l; 261826b15dadSJamal Hadi Salim } 261926b15dadSJamal Hadi Salim 2620214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c) 262126b15dadSJamal Hadi Salim { 2622fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 262326b15dadSJamal Hadi Salim struct xfrm_usersa_info *p; 26240603eac0SHerbert Xu struct xfrm_usersa_id *id; 262526b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 262626b15dadSJamal Hadi Salim struct sk_buff *skb; 262726b15dadSJamal Hadi Salim int len = xfrm_sa_len(x); 26281d1e34ddSDavid S. Miller int headlen, err; 26290603eac0SHerbert Xu 26300603eac0SHerbert Xu headlen = sizeof(*p); 26310603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 26327deb2264SThomas Graf len += nla_total_size(headlen); 26330603eac0SHerbert Xu headlen = sizeof(*id); 26346f26b61eSJamal Hadi Salim len += nla_total_size(sizeof(struct xfrm_mark)); 26350603eac0SHerbert Xu } 26367deb2264SThomas Graf len += NLMSG_ALIGN(headlen); 263726b15dadSJamal Hadi Salim 26387deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 263926b15dadSJamal Hadi Salim if (skb == NULL) 264026b15dadSJamal Hadi Salim return -ENOMEM; 264126b15dadSJamal Hadi Salim 264215e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 26431d1e34ddSDavid S. Miller err = -EMSGSIZE; 264479b8b7f4SThomas Graf if (nlh == NULL) 26451d1e34ddSDavid S. Miller goto out_free_skb; 264626b15dadSJamal Hadi Salim 26477b67c857SThomas Graf p = nlmsg_data(nlh); 26480603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 2649c0144beaSThomas Graf struct nlattr *attr; 2650c0144beaSThomas Graf 26517b67c857SThomas Graf id = nlmsg_data(nlh); 26520603eac0SHerbert Xu memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); 26530603eac0SHerbert Xu id->spi = x->id.spi; 26540603eac0SHerbert Xu id->family = x->props.family; 26550603eac0SHerbert Xu id->proto = x->id.proto; 26560603eac0SHerbert Xu 2657c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); 26581d1e34ddSDavid S. Miller err = -EMSGSIZE; 2659c0144beaSThomas Graf if (attr == NULL) 26601d1e34ddSDavid S. Miller goto out_free_skb; 2661c0144beaSThomas Graf 2662c0144beaSThomas Graf p = nla_data(attr); 26630603eac0SHerbert Xu } 26641d1e34ddSDavid S. Miller err = copy_to_user_state_extra(x, p, skb); 26651d1e34ddSDavid S. Miller if (err) 26661d1e34ddSDavid S. Miller goto out_free_skb; 266726b15dadSJamal Hadi Salim 26689825069dSThomas Graf nlmsg_end(skb, nlh); 266926b15dadSJamal Hadi Salim 267021ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 267126b15dadSJamal Hadi Salim 26721d1e34ddSDavid S. Miller out_free_skb: 267326b15dadSJamal Hadi Salim kfree_skb(skb); 26741d1e34ddSDavid S. Miller return err; 267526b15dadSJamal Hadi Salim } 267626b15dadSJamal Hadi Salim 2677214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c) 267826b15dadSJamal Hadi Salim { 267926b15dadSJamal Hadi Salim 268026b15dadSJamal Hadi Salim switch (c->event) { 2681f60f6b8fSHerbert Xu case XFRM_MSG_EXPIRE: 268226b15dadSJamal Hadi Salim return xfrm_exp_state_notify(x, c); 2683d51d081dSJamal Hadi Salim case XFRM_MSG_NEWAE: 2684d51d081dSJamal Hadi Salim return xfrm_aevent_state_notify(x, c); 2685f60f6b8fSHerbert Xu case XFRM_MSG_DELSA: 2686f60f6b8fSHerbert Xu case XFRM_MSG_UPDSA: 2687f60f6b8fSHerbert Xu case XFRM_MSG_NEWSA: 268826b15dadSJamal Hadi Salim return xfrm_notify_sa(x, c); 2689f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHSA: 269026b15dadSJamal Hadi Salim return xfrm_notify_sa_flush(c); 269126b15dadSJamal Hadi Salim default: 269262db5cfdSstephen hemminger printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n", 269362db5cfdSstephen hemminger c->event); 269426b15dadSJamal Hadi Salim break; 269526b15dadSJamal Hadi Salim } 269626b15dadSJamal Hadi Salim 269726b15dadSJamal Hadi Salim return 0; 269826b15dadSJamal Hadi Salim 269926b15dadSJamal Hadi Salim } 270026b15dadSJamal Hadi Salim 27017deb2264SThomas Graf static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x, 27027deb2264SThomas Graf struct xfrm_policy *xp) 27037deb2264SThomas Graf { 27047deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) 27057deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 27066f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 27077deb2264SThomas Graf + nla_total_size(xfrm_user_sec_ctx_size(x->security)) 27087deb2264SThomas Graf + userpolicy_type_attrsize(); 27097deb2264SThomas Graf } 27107deb2264SThomas Graf 27111da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, 271265e0736bSFan Du struct xfrm_tmpl *xt, struct xfrm_policy *xp) 27131da177e4SLinus Torvalds { 27141d1e34ddSDavid S. Miller __u32 seq = xfrm_get_acqseq(); 27151da177e4SLinus Torvalds struct xfrm_user_acquire *ua; 27161da177e4SLinus Torvalds struct nlmsghdr *nlh; 27171d1e34ddSDavid S. Miller int err; 27181da177e4SLinus Torvalds 271979b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); 272079b8b7f4SThomas Graf if (nlh == NULL) 272179b8b7f4SThomas Graf return -EMSGSIZE; 27221da177e4SLinus Torvalds 27237b67c857SThomas Graf ua = nlmsg_data(nlh); 27241da177e4SLinus Torvalds memcpy(&ua->id, &x->id, sizeof(ua->id)); 27251da177e4SLinus Torvalds memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); 27261da177e4SLinus Torvalds memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); 272765e0736bSFan Du copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT); 27281da177e4SLinus Torvalds ua->aalgos = xt->aalgos; 27291da177e4SLinus Torvalds ua->ealgos = xt->ealgos; 27301da177e4SLinus Torvalds ua->calgos = xt->calgos; 27311da177e4SLinus Torvalds ua->seq = x->km.seq = seq; 27321da177e4SLinus Torvalds 27331d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 27341d1e34ddSDavid S. Miller if (!err) 27351d1e34ddSDavid S. Miller err = copy_to_user_state_sec_ctx(x, skb); 27361d1e34ddSDavid S. Miller if (!err) 27371d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 27381d1e34ddSDavid S. Miller if (!err) 27391d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 27401d1e34ddSDavid S. Miller if (err) { 27411d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 27421d1e34ddSDavid S. Miller return err; 27431d1e34ddSDavid S. Miller } 27441da177e4SLinus Torvalds 2745053c095aSJohannes Berg nlmsg_end(skb, nlh); 2746053c095aSJohannes Berg return 0; 27471da177e4SLinus Torvalds } 27481da177e4SLinus Torvalds 27491da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, 275065e0736bSFan Du struct xfrm_policy *xp) 27511da177e4SLinus Torvalds { 2752a6483b79SAlexey Dobriyan struct net *net = xs_net(x); 27531da177e4SLinus Torvalds struct sk_buff *skb; 27541da177e4SLinus Torvalds 27557deb2264SThomas Graf skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); 27561da177e4SLinus Torvalds if (skb == NULL) 27571da177e4SLinus Torvalds return -ENOMEM; 27581da177e4SLinus Torvalds 275965e0736bSFan Du if (build_acquire(skb, x, xt, xp) < 0) 27601da177e4SLinus Torvalds BUG(); 27611da177e4SLinus Torvalds 276221ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE); 27631da177e4SLinus Torvalds } 27641da177e4SLinus Torvalds 27651da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0 27661da177e4SLinus Torvalds * or more templates. 27671da177e4SLinus Torvalds */ 2768cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, 27691da177e4SLinus Torvalds u8 *data, int len, int *dir) 27701da177e4SLinus Torvalds { 2771fc34acd3SAlexey Dobriyan struct net *net = sock_net(sk); 27721da177e4SLinus Torvalds struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; 27731da177e4SLinus Torvalds struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); 27741da177e4SLinus Torvalds struct xfrm_policy *xp; 27751da177e4SLinus Torvalds int nr; 27761da177e4SLinus Torvalds 2777cb969f07SVenkat Yekkirala switch (sk->sk_family) { 27781da177e4SLinus Torvalds case AF_INET: 27791da177e4SLinus Torvalds if (opt != IP_XFRM_POLICY) { 27801da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 27811da177e4SLinus Torvalds return NULL; 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds break; 2784dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 27851da177e4SLinus Torvalds case AF_INET6: 27861da177e4SLinus Torvalds if (opt != IPV6_XFRM_POLICY) { 27871da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 27881da177e4SLinus Torvalds return NULL; 27891da177e4SLinus Torvalds } 27901da177e4SLinus Torvalds break; 27911da177e4SLinus Torvalds #endif 27921da177e4SLinus Torvalds default: 27931da177e4SLinus Torvalds *dir = -EINVAL; 27941da177e4SLinus Torvalds return NULL; 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds 27971da177e4SLinus Torvalds *dir = -EINVAL; 27981da177e4SLinus Torvalds 27991da177e4SLinus Torvalds if (len < sizeof(*p) || 28001da177e4SLinus Torvalds verify_newpolicy_info(p)) 28011da177e4SLinus Torvalds return NULL; 28021da177e4SLinus Torvalds 28031da177e4SLinus Torvalds nr = ((len - sizeof(*p)) / sizeof(*ut)); 2804b4ad86bfSDavid S. Miller if (validate_tmpl(nr, ut, p->sel.family)) 28051da177e4SLinus Torvalds return NULL; 28061da177e4SLinus Torvalds 2807a4f1bac6SHerbert Xu if (p->dir > XFRM_POLICY_OUT) 2808a4f1bac6SHerbert Xu return NULL; 2809a4f1bac6SHerbert Xu 28102f09a4d5SHerbert Xu xp = xfrm_policy_alloc(net, GFP_ATOMIC); 28111da177e4SLinus Torvalds if (xp == NULL) { 28121da177e4SLinus Torvalds *dir = -ENOBUFS; 28131da177e4SLinus Torvalds return NULL; 28141da177e4SLinus Torvalds } 28151da177e4SLinus Torvalds 28161da177e4SLinus Torvalds copy_from_user_policy(xp, p); 2817f7b6983fSMasahide NAKAMURA xp->type = XFRM_POLICY_TYPE_MAIN; 28181da177e4SLinus Torvalds copy_templates(xp, ut, nr); 28191da177e4SLinus Torvalds 28201da177e4SLinus Torvalds *dir = p->dir; 28211da177e4SLinus Torvalds 28221da177e4SLinus Torvalds return xp; 28231da177e4SLinus Torvalds } 28241da177e4SLinus Torvalds 28257deb2264SThomas Graf static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp) 28267deb2264SThomas Graf { 28277deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) 28287deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 28297deb2264SThomas Graf + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) 2830295fae56SJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 28317deb2264SThomas Graf + userpolicy_type_attrsize(); 28327deb2264SThomas Graf } 28337deb2264SThomas Graf 28341da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, 2835214e005bSDavid S. Miller int dir, const struct km_event *c) 28361da177e4SLinus Torvalds { 28371da177e4SLinus Torvalds struct xfrm_user_polexpire *upe; 2838d51d081dSJamal Hadi Salim int hard = c->data.hard; 28391d1e34ddSDavid S. Miller struct nlmsghdr *nlh; 28401d1e34ddSDavid S. Miller int err; 28411da177e4SLinus Torvalds 284215e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); 284379b8b7f4SThomas Graf if (nlh == NULL) 284479b8b7f4SThomas Graf return -EMSGSIZE; 28451da177e4SLinus Torvalds 28467b67c857SThomas Graf upe = nlmsg_data(nlh); 28471da177e4SLinus Torvalds copy_to_user_policy(xp, &upe->pol, dir); 28481d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 28491d1e34ddSDavid S. Miller if (!err) 28501d1e34ddSDavid S. Miller err = copy_to_user_sec_ctx(xp, skb); 28511d1e34ddSDavid S. Miller if (!err) 28521d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 28531d1e34ddSDavid S. Miller if (!err) 28541d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 28551d1e34ddSDavid S. Miller if (err) { 28561d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 28571d1e34ddSDavid S. Miller return err; 28581d1e34ddSDavid S. Miller } 28591da177e4SLinus Torvalds upe->hard = !!hard; 28601da177e4SLinus Torvalds 2861053c095aSJohannes Berg nlmsg_end(skb, nlh); 2862053c095aSJohannes Berg return 0; 28631da177e4SLinus Torvalds } 28641da177e4SLinus Torvalds 2865214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 28661da177e4SLinus Torvalds { 2867fc34acd3SAlexey Dobriyan struct net *net = xp_net(xp); 28681da177e4SLinus Torvalds struct sk_buff *skb; 28691da177e4SLinus Torvalds 28707deb2264SThomas Graf skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); 28711da177e4SLinus Torvalds if (skb == NULL) 28721da177e4SLinus Torvalds return -ENOMEM; 28731da177e4SLinus Torvalds 2874d51d081dSJamal Hadi Salim if (build_polexpire(skb, xp, dir, c) < 0) 28751da177e4SLinus Torvalds BUG(); 28761da177e4SLinus Torvalds 287721ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 28781da177e4SLinus Torvalds } 28791da177e4SLinus Torvalds 2880214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c) 288126b15dadSJamal Hadi Salim { 28821d1e34ddSDavid S. Miller int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 2883fc34acd3SAlexey Dobriyan struct net *net = xp_net(xp); 288426b15dadSJamal Hadi Salim struct xfrm_userpolicy_info *p; 28850603eac0SHerbert Xu struct xfrm_userpolicy_id *id; 288626b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 288726b15dadSJamal Hadi Salim struct sk_buff *skb; 28881d1e34ddSDavid S. Miller int headlen, err; 28890603eac0SHerbert Xu 28900603eac0SHerbert Xu headlen = sizeof(*p); 28910603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 28927deb2264SThomas Graf len += nla_total_size(headlen); 28930603eac0SHerbert Xu headlen = sizeof(*id); 28940603eac0SHerbert Xu } 2895cfbfd45aSThomas Graf len += userpolicy_type_attrsize(); 2896295fae56SJamal Hadi Salim len += nla_total_size(sizeof(struct xfrm_mark)); 28977deb2264SThomas Graf len += NLMSG_ALIGN(headlen); 289826b15dadSJamal Hadi Salim 28997deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 290026b15dadSJamal Hadi Salim if (skb == NULL) 290126b15dadSJamal Hadi Salim return -ENOMEM; 290226b15dadSJamal Hadi Salim 290315e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 29041d1e34ddSDavid S. Miller err = -EMSGSIZE; 290579b8b7f4SThomas Graf if (nlh == NULL) 29061d1e34ddSDavid S. Miller goto out_free_skb; 290726b15dadSJamal Hadi Salim 29087b67c857SThomas Graf p = nlmsg_data(nlh); 29090603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 2910c0144beaSThomas Graf struct nlattr *attr; 2911c0144beaSThomas Graf 29127b67c857SThomas Graf id = nlmsg_data(nlh); 29130603eac0SHerbert Xu memset(id, 0, sizeof(*id)); 29140603eac0SHerbert Xu id->dir = dir; 29150603eac0SHerbert Xu if (c->data.byid) 29160603eac0SHerbert Xu id->index = xp->index; 29170603eac0SHerbert Xu else 29180603eac0SHerbert Xu memcpy(&id->sel, &xp->selector, sizeof(id->sel)); 29190603eac0SHerbert Xu 2920c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); 29211d1e34ddSDavid S. Miller err = -EMSGSIZE; 2922c0144beaSThomas Graf if (attr == NULL) 29231d1e34ddSDavid S. Miller goto out_free_skb; 2924c0144beaSThomas Graf 2925c0144beaSThomas Graf p = nla_data(attr); 29260603eac0SHerbert Xu } 292726b15dadSJamal Hadi Salim 292826b15dadSJamal Hadi Salim copy_to_user_policy(xp, p, dir); 29291d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 29301d1e34ddSDavid S. Miller if (!err) 29311d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 29321d1e34ddSDavid S. Miller if (!err) 29331d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 29341d1e34ddSDavid S. Miller if (err) 29351d1e34ddSDavid S. Miller goto out_free_skb; 2936295fae56SJamal Hadi Salim 29379825069dSThomas Graf nlmsg_end(skb, nlh); 293826b15dadSJamal Hadi Salim 293921ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 294026b15dadSJamal Hadi Salim 29411d1e34ddSDavid S. Miller out_free_skb: 294226b15dadSJamal Hadi Salim kfree_skb(skb); 29431d1e34ddSDavid S. Miller return err; 294426b15dadSJamal Hadi Salim } 294526b15dadSJamal Hadi Salim 2946214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c) 294726b15dadSJamal Hadi Salim { 29487067802eSAlexey Dobriyan struct net *net = c->net; 294926b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 295026b15dadSJamal Hadi Salim struct sk_buff *skb; 29511d1e34ddSDavid S. Miller int err; 295226b15dadSJamal Hadi Salim 29537deb2264SThomas Graf skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); 295426b15dadSJamal Hadi Salim if (skb == NULL) 295526b15dadSJamal Hadi Salim return -ENOMEM; 295626b15dadSJamal Hadi Salim 295715e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); 29581d1e34ddSDavid S. Miller err = -EMSGSIZE; 295979b8b7f4SThomas Graf if (nlh == NULL) 29601d1e34ddSDavid S. Miller goto out_free_skb; 29611d1e34ddSDavid S. Miller err = copy_to_user_policy_type(c->data.type, skb); 29621d1e34ddSDavid S. Miller if (err) 29631d1e34ddSDavid S. Miller goto out_free_skb; 296426b15dadSJamal Hadi Salim 29659825069dSThomas Graf nlmsg_end(skb, nlh); 296626b15dadSJamal Hadi Salim 296721ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 296826b15dadSJamal Hadi Salim 29691d1e34ddSDavid S. Miller out_free_skb: 297026b15dadSJamal Hadi Salim kfree_skb(skb); 29711d1e34ddSDavid S. Miller return err; 297226b15dadSJamal Hadi Salim } 297326b15dadSJamal Hadi Salim 2974214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 297526b15dadSJamal Hadi Salim { 297626b15dadSJamal Hadi Salim 297726b15dadSJamal Hadi Salim switch (c->event) { 2978f60f6b8fSHerbert Xu case XFRM_MSG_NEWPOLICY: 2979f60f6b8fSHerbert Xu case XFRM_MSG_UPDPOLICY: 2980f60f6b8fSHerbert Xu case XFRM_MSG_DELPOLICY: 298126b15dadSJamal Hadi Salim return xfrm_notify_policy(xp, dir, c); 2982f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHPOLICY: 298326b15dadSJamal Hadi Salim return xfrm_notify_policy_flush(c); 2984f60f6b8fSHerbert Xu case XFRM_MSG_POLEXPIRE: 298526b15dadSJamal Hadi Salim return xfrm_exp_policy_notify(xp, dir, c); 298626b15dadSJamal Hadi Salim default: 298762db5cfdSstephen hemminger printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n", 298862db5cfdSstephen hemminger c->event); 298926b15dadSJamal Hadi Salim } 299026b15dadSJamal Hadi Salim 299126b15dadSJamal Hadi Salim return 0; 299226b15dadSJamal Hadi Salim 299326b15dadSJamal Hadi Salim } 299426b15dadSJamal Hadi Salim 29957deb2264SThomas Graf static inline size_t xfrm_report_msgsize(void) 29967deb2264SThomas Graf { 29977deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); 29987deb2264SThomas Graf } 29997deb2264SThomas Graf 300097a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto, 300197a64b45SMasahide NAKAMURA struct xfrm_selector *sel, xfrm_address_t *addr) 300297a64b45SMasahide NAKAMURA { 300397a64b45SMasahide NAKAMURA struct xfrm_user_report *ur; 300497a64b45SMasahide NAKAMURA struct nlmsghdr *nlh; 300597a64b45SMasahide NAKAMURA 300679b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); 300779b8b7f4SThomas Graf if (nlh == NULL) 300879b8b7f4SThomas Graf return -EMSGSIZE; 300997a64b45SMasahide NAKAMURA 30107b67c857SThomas Graf ur = nlmsg_data(nlh); 301197a64b45SMasahide NAKAMURA ur->proto = proto; 301297a64b45SMasahide NAKAMURA memcpy(&ur->sel, sel, sizeof(ur->sel)); 301397a64b45SMasahide NAKAMURA 30141d1e34ddSDavid S. Miller if (addr) { 30151d1e34ddSDavid S. Miller int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr); 30161d1e34ddSDavid S. Miller if (err) { 30179825069dSThomas Graf nlmsg_cancel(skb, nlh); 30181d1e34ddSDavid S. Miller return err; 30191d1e34ddSDavid S. Miller } 30201d1e34ddSDavid S. Miller } 3021053c095aSJohannes Berg nlmsg_end(skb, nlh); 3022053c095aSJohannes Berg return 0; 302397a64b45SMasahide NAKAMURA } 302497a64b45SMasahide NAKAMURA 3025db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto, 3026db983c11SAlexey Dobriyan struct xfrm_selector *sel, xfrm_address_t *addr) 302797a64b45SMasahide NAKAMURA { 302897a64b45SMasahide NAKAMURA struct sk_buff *skb; 302997a64b45SMasahide NAKAMURA 30307deb2264SThomas Graf skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); 303197a64b45SMasahide NAKAMURA if (skb == NULL) 303297a64b45SMasahide NAKAMURA return -ENOMEM; 303397a64b45SMasahide NAKAMURA 303497a64b45SMasahide NAKAMURA if (build_report(skb, proto, sel, addr) < 0) 303597a64b45SMasahide NAKAMURA BUG(); 303697a64b45SMasahide NAKAMURA 303721ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT); 303897a64b45SMasahide NAKAMURA } 303997a64b45SMasahide NAKAMURA 30403a2dfbe8SMartin Willi static inline size_t xfrm_mapping_msgsize(void) 30413a2dfbe8SMartin Willi { 30423a2dfbe8SMartin Willi return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping)); 30433a2dfbe8SMartin Willi } 30443a2dfbe8SMartin Willi 30453a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x, 30463a2dfbe8SMartin Willi xfrm_address_t *new_saddr, __be16 new_sport) 30473a2dfbe8SMartin Willi { 30483a2dfbe8SMartin Willi struct xfrm_user_mapping *um; 30493a2dfbe8SMartin Willi struct nlmsghdr *nlh; 30503a2dfbe8SMartin Willi 30513a2dfbe8SMartin Willi nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0); 30523a2dfbe8SMartin Willi if (nlh == NULL) 30533a2dfbe8SMartin Willi return -EMSGSIZE; 30543a2dfbe8SMartin Willi 30553a2dfbe8SMartin Willi um = nlmsg_data(nlh); 30563a2dfbe8SMartin Willi 30573a2dfbe8SMartin Willi memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr)); 30583a2dfbe8SMartin Willi um->id.spi = x->id.spi; 30593a2dfbe8SMartin Willi um->id.family = x->props.family; 30603a2dfbe8SMartin Willi um->id.proto = x->id.proto; 30613a2dfbe8SMartin Willi memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr)); 30623a2dfbe8SMartin Willi memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr)); 30633a2dfbe8SMartin Willi um->new_sport = new_sport; 30643a2dfbe8SMartin Willi um->old_sport = x->encap->encap_sport; 30653a2dfbe8SMartin Willi um->reqid = x->props.reqid; 30663a2dfbe8SMartin Willi 3067053c095aSJohannes Berg nlmsg_end(skb, nlh); 3068053c095aSJohannes Berg return 0; 30693a2dfbe8SMartin Willi } 30703a2dfbe8SMartin Willi 30713a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, 30723a2dfbe8SMartin Willi __be16 sport) 30733a2dfbe8SMartin Willi { 3074a6483b79SAlexey Dobriyan struct net *net = xs_net(x); 30753a2dfbe8SMartin Willi struct sk_buff *skb; 30763a2dfbe8SMartin Willi 30773a2dfbe8SMartin Willi if (x->id.proto != IPPROTO_ESP) 30783a2dfbe8SMartin Willi return -EINVAL; 30793a2dfbe8SMartin Willi 30803a2dfbe8SMartin Willi if (!x->encap) 30813a2dfbe8SMartin Willi return -EINVAL; 30823a2dfbe8SMartin Willi 30833a2dfbe8SMartin Willi skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC); 30843a2dfbe8SMartin Willi if (skb == NULL) 30853a2dfbe8SMartin Willi return -ENOMEM; 30863a2dfbe8SMartin Willi 30873a2dfbe8SMartin Willi if (build_mapping(skb, x, ipaddr, sport) < 0) 30883a2dfbe8SMartin Willi BUG(); 30893a2dfbe8SMartin Willi 309021ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING); 30913a2dfbe8SMartin Willi } 30923a2dfbe8SMartin Willi 30930f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c) 30940f24558eSHoria Geanta { 30950f24558eSHoria Geanta return (bool)xfrm_acquire_is_on(c->net); 30960f24558eSHoria Geanta } 30970f24558eSHoria Geanta 30981da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = { 30991da177e4SLinus Torvalds .id = "netlink", 31001da177e4SLinus Torvalds .notify = xfrm_send_state_notify, 31011da177e4SLinus Torvalds .acquire = xfrm_send_acquire, 31021da177e4SLinus Torvalds .compile_policy = xfrm_compile_policy, 31031da177e4SLinus Torvalds .notify_policy = xfrm_send_policy_notify, 310497a64b45SMasahide NAKAMURA .report = xfrm_send_report, 31055c79de6eSShinta Sugimoto .migrate = xfrm_send_migrate, 31063a2dfbe8SMartin Willi .new_mapping = xfrm_send_mapping, 31070f24558eSHoria Geanta .is_alive = xfrm_is_alive, 31081da177e4SLinus Torvalds }; 31091da177e4SLinus Torvalds 3110a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net) 31111da177e4SLinus Torvalds { 3112be33690dSPatrick McHardy struct sock *nlsk; 3113a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 3114a31f2d17SPablo Neira Ayuso .groups = XFRMNLGRP_MAX, 3115a31f2d17SPablo Neira Ayuso .input = xfrm_netlink_rcv, 3116a31f2d17SPablo Neira Ayuso }; 3117be33690dSPatrick McHardy 31189f00d977SPablo Neira Ayuso nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg); 3119be33690dSPatrick McHardy if (nlsk == NULL) 31201da177e4SLinus Torvalds return -ENOMEM; 3121d79d792eSEric W. Biederman net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */ 3122cf778b00SEric Dumazet rcu_assign_pointer(net->xfrm.nlsk, nlsk); 31231da177e4SLinus Torvalds return 0; 31241da177e4SLinus Torvalds } 31251da177e4SLinus Torvalds 3126d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list) 3127a6483b79SAlexey Dobriyan { 3128d79d792eSEric W. Biederman struct net *net; 3129d79d792eSEric W. Biederman list_for_each_entry(net, net_exit_list, exit_list) 3130a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(net->xfrm.nlsk, NULL); 3131d79d792eSEric W. Biederman synchronize_net(); 3132d79d792eSEric W. Biederman list_for_each_entry(net, net_exit_list, exit_list) 3133d79d792eSEric W. Biederman netlink_kernel_release(net->xfrm.nlsk_stash); 3134a6483b79SAlexey Dobriyan } 3135a6483b79SAlexey Dobriyan 3136a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = { 3137a6483b79SAlexey Dobriyan .init = xfrm_user_net_init, 3138d79d792eSEric W. Biederman .exit_batch = xfrm_user_net_exit, 3139a6483b79SAlexey Dobriyan }; 3140a6483b79SAlexey Dobriyan 3141a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void) 3142a6483b79SAlexey Dobriyan { 3143a6483b79SAlexey Dobriyan int rv; 3144a6483b79SAlexey Dobriyan 3145a6483b79SAlexey Dobriyan printk(KERN_INFO "Initializing XFRM netlink socket\n"); 3146a6483b79SAlexey Dobriyan 3147a6483b79SAlexey Dobriyan rv = register_pernet_subsys(&xfrm_user_net_ops); 3148a6483b79SAlexey Dobriyan if (rv < 0) 3149a6483b79SAlexey Dobriyan return rv; 3150a6483b79SAlexey Dobriyan rv = xfrm_register_km(&netlink_mgr); 3151a6483b79SAlexey Dobriyan if (rv < 0) 3152a6483b79SAlexey Dobriyan unregister_pernet_subsys(&xfrm_user_net_ops); 3153a6483b79SAlexey Dobriyan return rv; 3154a6483b79SAlexey Dobriyan } 3155a6483b79SAlexey Dobriyan 31561da177e4SLinus Torvalds static void __exit xfrm_user_exit(void) 31571da177e4SLinus Torvalds { 31581da177e4SLinus Torvalds xfrm_unregister_km(&netlink_mgr); 3159a6483b79SAlexey Dobriyan unregister_pernet_subsys(&xfrm_user_net_ops); 31601da177e4SLinus Torvalds } 31611da177e4SLinus Torvalds 31621da177e4SLinus Torvalds module_init(xfrm_user_init); 31631da177e4SLinus Torvalds module_exit(xfrm_user_exit); 31641da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 31654fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); 3166f8cd5488SJamal Hadi Salim 3167