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 351a6509d9SHerbert Xu static inline int aead_len(struct xfrm_algo_aead *alg) 361a6509d9SHerbert Xu { 371a6509d9SHerbert Xu return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); 381a6509d9SHerbert Xu } 391a6509d9SHerbert Xu 405424f32eSThomas Graf static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type) 411da177e4SLinus Torvalds { 425424f32eSThomas Graf struct nlattr *rt = attrs[type]; 431da177e4SLinus Torvalds struct xfrm_algo *algp; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds if (!rt) 461da177e4SLinus Torvalds return 0; 471da177e4SLinus Torvalds 485424f32eSThomas Graf algp = nla_data(rt); 490f99be0dSEric Dumazet if (nla_len(rt) < xfrm_alg_len(algp)) 5031c26852SHerbert Xu return -EINVAL; 5131c26852SHerbert Xu 521da177e4SLinus Torvalds switch (type) { 531da177e4SLinus Torvalds case XFRMA_ALG_AUTH: 541da177e4SLinus Torvalds case XFRMA_ALG_CRYPT: 551da177e4SLinus Torvalds case XFRMA_ALG_COMP: 561da177e4SLinus Torvalds break; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds default: 591da177e4SLinus Torvalds return -EINVAL; 603ff50b79SStephen Hemminger } 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; 631da177e4SLinus Torvalds return 0; 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 664447bb33SMartin Willi static int verify_auth_trunc(struct nlattr **attrs) 674447bb33SMartin Willi { 684447bb33SMartin Willi struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC]; 694447bb33SMartin Willi struct xfrm_algo_auth *algp; 704447bb33SMartin Willi 714447bb33SMartin Willi if (!rt) 724447bb33SMartin Willi return 0; 734447bb33SMartin Willi 744447bb33SMartin Willi algp = nla_data(rt); 754447bb33SMartin Willi if (nla_len(rt) < xfrm_alg_auth_len(algp)) 764447bb33SMartin Willi return -EINVAL; 774447bb33SMartin Willi 784447bb33SMartin Willi algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; 794447bb33SMartin Willi return 0; 804447bb33SMartin Willi } 814447bb33SMartin Willi 821a6509d9SHerbert Xu static int verify_aead(struct nlattr **attrs) 831a6509d9SHerbert Xu { 841a6509d9SHerbert Xu struct nlattr *rt = attrs[XFRMA_ALG_AEAD]; 851a6509d9SHerbert Xu struct xfrm_algo_aead *algp; 861a6509d9SHerbert Xu 871a6509d9SHerbert Xu if (!rt) 881a6509d9SHerbert Xu return 0; 891a6509d9SHerbert Xu 901a6509d9SHerbert Xu algp = nla_data(rt); 911a6509d9SHerbert Xu if (nla_len(rt) < aead_len(algp)) 921a6509d9SHerbert Xu return -EINVAL; 931a6509d9SHerbert Xu 941a6509d9SHerbert Xu algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; 951a6509d9SHerbert Xu return 0; 961a6509d9SHerbert Xu } 971a6509d9SHerbert Xu 985424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type, 99eb2971b6SMasahide NAKAMURA xfrm_address_t **addrp) 100eb2971b6SMasahide NAKAMURA { 1015424f32eSThomas Graf struct nlattr *rt = attrs[type]; 102eb2971b6SMasahide NAKAMURA 103cf5cb79fSThomas Graf if (rt && addrp) 1045424f32eSThomas Graf *addrp = nla_data(rt); 105eb2971b6SMasahide NAKAMURA } 106df71837dSTrent Jaeger 1075424f32eSThomas Graf static inline int verify_sec_ctx_len(struct nlattr **attrs) 108df71837dSTrent Jaeger { 1095424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 110df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx; 111df71837dSTrent Jaeger 112df71837dSTrent Jaeger if (!rt) 113df71837dSTrent Jaeger return 0; 114df71837dSTrent Jaeger 1155424f32eSThomas Graf uctx = nla_data(rt); 116cf5cb79fSThomas Graf if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) 117df71837dSTrent Jaeger return -EINVAL; 118df71837dSTrent Jaeger 119df71837dSTrent Jaeger return 0; 120df71837dSTrent Jaeger } 121df71837dSTrent Jaeger 122d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p, 123d8647b79SSteffen Klassert struct nlattr **attrs) 124d8647b79SSteffen Klassert { 125d8647b79SSteffen Klassert struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL]; 126*ecd79187SMathias Krause struct xfrm_replay_state_esn *rs; 127d8647b79SSteffen Klassert 128*ecd79187SMathias Krause if (p->flags & XFRM_STATE_ESN) { 129*ecd79187SMathias Krause if (!rt) 1307833aa05SSteffen Klassert return -EINVAL; 1317833aa05SSteffen Klassert 132*ecd79187SMathias Krause rs = nla_data(rt); 133*ecd79187SMathias Krause 134*ecd79187SMathias Krause if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8) 135*ecd79187SMathias Krause return -EINVAL; 136*ecd79187SMathias Krause 137*ecd79187SMathias Krause if (nla_len(rt) < xfrm_replay_state_esn_len(rs) && 138*ecd79187SMathias Krause nla_len(rt) != sizeof(*rs)) 139*ecd79187SMathias Krause return -EINVAL; 140*ecd79187SMathias Krause } 141*ecd79187SMathias Krause 142d8647b79SSteffen Klassert if (!rt) 143d8647b79SSteffen Klassert return 0; 144d8647b79SSteffen Klassert 14502aadf72SSteffen Klassert if (p->id.proto != IPPROTO_ESP) 14602aadf72SSteffen Klassert return -EINVAL; 14702aadf72SSteffen Klassert 148d8647b79SSteffen Klassert if (p->replay_window != 0) 149d8647b79SSteffen Klassert return -EINVAL; 150d8647b79SSteffen Klassert 151d8647b79SSteffen Klassert return 0; 152d8647b79SSteffen Klassert } 153df71837dSTrent Jaeger 1541da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p, 1555424f32eSThomas Graf struct nlattr **attrs) 1561da177e4SLinus Torvalds { 1571da177e4SLinus Torvalds int err; 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds err = -EINVAL; 1601da177e4SLinus Torvalds switch (p->family) { 1611da177e4SLinus Torvalds case AF_INET: 1621da177e4SLinus Torvalds break; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds case AF_INET6: 165dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1661da177e4SLinus Torvalds break; 1671da177e4SLinus Torvalds #else 1681da177e4SLinus Torvalds err = -EAFNOSUPPORT; 1691da177e4SLinus Torvalds goto out; 1701da177e4SLinus Torvalds #endif 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds default: 1731da177e4SLinus Torvalds goto out; 1743ff50b79SStephen Hemminger } 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds err = -EINVAL; 1771da177e4SLinus Torvalds switch (p->id.proto) { 1781da177e4SLinus Torvalds case IPPROTO_AH: 1794447bb33SMartin Willi if ((!attrs[XFRMA_ALG_AUTH] && 1804447bb33SMartin Willi !attrs[XFRMA_ALG_AUTH_TRUNC]) || 1811a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD] || 18235a7aa08SThomas Graf attrs[XFRMA_ALG_CRYPT] || 18335d2856bSMartin Willi attrs[XFRMA_ALG_COMP] || 18435d2856bSMartin Willi attrs[XFRMA_TFCPAD]) 1851da177e4SLinus Torvalds goto out; 1861da177e4SLinus Torvalds break; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds case IPPROTO_ESP: 1891a6509d9SHerbert Xu if (attrs[XFRMA_ALG_COMP]) 1901a6509d9SHerbert Xu goto out; 1911a6509d9SHerbert Xu if (!attrs[XFRMA_ALG_AUTH] && 1924447bb33SMartin Willi !attrs[XFRMA_ALG_AUTH_TRUNC] && 1931a6509d9SHerbert Xu !attrs[XFRMA_ALG_CRYPT] && 1941a6509d9SHerbert Xu !attrs[XFRMA_ALG_AEAD]) 1951a6509d9SHerbert Xu goto out; 1961a6509d9SHerbert Xu if ((attrs[XFRMA_ALG_AUTH] || 1974447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 1981a6509d9SHerbert Xu attrs[XFRMA_ALG_CRYPT]) && 1991a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD]) 2001da177e4SLinus Torvalds goto out; 20135d2856bSMartin Willi if (attrs[XFRMA_TFCPAD] && 20235d2856bSMartin Willi p->mode != XFRM_MODE_TUNNEL) 20335d2856bSMartin Willi goto out; 2041da177e4SLinus Torvalds break; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds case IPPROTO_COMP: 20735a7aa08SThomas Graf if (!attrs[XFRMA_ALG_COMP] || 2081a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD] || 20935a7aa08SThomas Graf attrs[XFRMA_ALG_AUTH] || 2104447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 21135d2856bSMartin Willi attrs[XFRMA_ALG_CRYPT] || 21235d2856bSMartin Willi attrs[XFRMA_TFCPAD]) 2131da177e4SLinus Torvalds goto out; 2141da177e4SLinus Torvalds break; 2151da177e4SLinus Torvalds 216dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 217e23c7194SMasahide NAKAMURA case IPPROTO_DSTOPTS: 218e23c7194SMasahide NAKAMURA case IPPROTO_ROUTING: 21935a7aa08SThomas Graf if (attrs[XFRMA_ALG_COMP] || 22035a7aa08SThomas Graf attrs[XFRMA_ALG_AUTH] || 2214447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 2221a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD] || 22335a7aa08SThomas Graf attrs[XFRMA_ALG_CRYPT] || 22435a7aa08SThomas Graf attrs[XFRMA_ENCAP] || 22535a7aa08SThomas Graf attrs[XFRMA_SEC_CTX] || 22635d2856bSMartin Willi attrs[XFRMA_TFCPAD] || 22735a7aa08SThomas Graf !attrs[XFRMA_COADDR]) 228e23c7194SMasahide NAKAMURA goto out; 229e23c7194SMasahide NAKAMURA break; 230e23c7194SMasahide NAKAMURA #endif 231e23c7194SMasahide NAKAMURA 2321da177e4SLinus Torvalds default: 2331da177e4SLinus Torvalds goto out; 2343ff50b79SStephen Hemminger } 2351da177e4SLinus Torvalds 2361a6509d9SHerbert Xu if ((err = verify_aead(attrs))) 2371a6509d9SHerbert Xu goto out; 2384447bb33SMartin Willi if ((err = verify_auth_trunc(attrs))) 2394447bb33SMartin Willi goto out; 24035a7aa08SThomas Graf if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH))) 2411da177e4SLinus Torvalds goto out; 24235a7aa08SThomas Graf if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT))) 2431da177e4SLinus Torvalds goto out; 24435a7aa08SThomas Graf if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP))) 2451da177e4SLinus Torvalds goto out; 24635a7aa08SThomas Graf if ((err = verify_sec_ctx_len(attrs))) 247df71837dSTrent Jaeger goto out; 248d8647b79SSteffen Klassert if ((err = verify_replay(p, attrs))) 249d8647b79SSteffen Klassert goto out; 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds err = -EINVAL; 2521da177e4SLinus Torvalds switch (p->mode) { 2537e49e6deSMasahide NAKAMURA case XFRM_MODE_TRANSPORT: 2547e49e6deSMasahide NAKAMURA case XFRM_MODE_TUNNEL: 255060f02a3SNoriaki TAKAMIYA case XFRM_MODE_ROUTEOPTIMIZATION: 2560a69452cSDiego Beltrami case XFRM_MODE_BEET: 2571da177e4SLinus Torvalds break; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds default: 2601da177e4SLinus Torvalds goto out; 2613ff50b79SStephen Hemminger } 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds err = 0; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds out: 2661da177e4SLinus Torvalds return err; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props, 2706f2f19edSDavid S. Miller struct xfrm_algo_desc *(*get_byname)(const char *, int), 2715424f32eSThomas Graf struct nlattr *rta) 2721da177e4SLinus Torvalds { 2731da177e4SLinus Torvalds struct xfrm_algo *p, *ualg; 2741da177e4SLinus Torvalds struct xfrm_algo_desc *algo; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds if (!rta) 2771da177e4SLinus Torvalds return 0; 2781da177e4SLinus Torvalds 2795424f32eSThomas Graf ualg = nla_data(rta); 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds algo = get_byname(ualg->alg_name, 1); 2821da177e4SLinus Torvalds if (!algo) 2831da177e4SLinus Torvalds return -ENOSYS; 2841da177e4SLinus Torvalds *props = algo->desc.sadb_alg_id; 2851da177e4SLinus Torvalds 2860f99be0dSEric Dumazet p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL); 2871da177e4SLinus Torvalds if (!p) 2881da177e4SLinus Torvalds return -ENOMEM; 2891da177e4SLinus Torvalds 29004ff1260SHerbert Xu strcpy(p->alg_name, algo->name); 2911da177e4SLinus Torvalds *algpp = p; 2921da177e4SLinus Torvalds return 0; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 2954447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props, 2964447bb33SMartin Willi struct nlattr *rta) 2974447bb33SMartin Willi { 2984447bb33SMartin Willi struct xfrm_algo *ualg; 2994447bb33SMartin Willi struct xfrm_algo_auth *p; 3004447bb33SMartin Willi struct xfrm_algo_desc *algo; 3014447bb33SMartin Willi 3024447bb33SMartin Willi if (!rta) 3034447bb33SMartin Willi return 0; 3044447bb33SMartin Willi 3054447bb33SMartin Willi ualg = nla_data(rta); 3064447bb33SMartin Willi 3074447bb33SMartin Willi algo = xfrm_aalg_get_byname(ualg->alg_name, 1); 3084447bb33SMartin Willi if (!algo) 3094447bb33SMartin Willi return -ENOSYS; 3104447bb33SMartin Willi *props = algo->desc.sadb_alg_id; 3114447bb33SMartin Willi 3124447bb33SMartin Willi p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL); 3134447bb33SMartin Willi if (!p) 3144447bb33SMartin Willi return -ENOMEM; 3154447bb33SMartin Willi 3164447bb33SMartin Willi strcpy(p->alg_name, algo->name); 3174447bb33SMartin Willi p->alg_key_len = ualg->alg_key_len; 3184447bb33SMartin Willi p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; 3194447bb33SMartin Willi memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8); 3204447bb33SMartin Willi 3214447bb33SMartin Willi *algpp = p; 3224447bb33SMartin Willi return 0; 3234447bb33SMartin Willi } 3244447bb33SMartin Willi 3254447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props, 3264447bb33SMartin Willi struct nlattr *rta) 3274447bb33SMartin Willi { 3284447bb33SMartin Willi struct xfrm_algo_auth *p, *ualg; 3294447bb33SMartin Willi struct xfrm_algo_desc *algo; 3304447bb33SMartin Willi 3314447bb33SMartin Willi if (!rta) 3324447bb33SMartin Willi return 0; 3334447bb33SMartin Willi 3344447bb33SMartin Willi ualg = nla_data(rta); 3354447bb33SMartin Willi 3364447bb33SMartin Willi algo = xfrm_aalg_get_byname(ualg->alg_name, 1); 3374447bb33SMartin Willi if (!algo) 3384447bb33SMartin Willi return -ENOSYS; 339fa6dd8a2SNicolas Dichtel if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN || 340fa6dd8a2SNicolas Dichtel ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) 3414447bb33SMartin Willi return -EINVAL; 3424447bb33SMartin Willi *props = algo->desc.sadb_alg_id; 3434447bb33SMartin Willi 3444447bb33SMartin Willi p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL); 3454447bb33SMartin Willi if (!p) 3464447bb33SMartin Willi return -ENOMEM; 3474447bb33SMartin Willi 3484447bb33SMartin Willi strcpy(p->alg_name, algo->name); 3494447bb33SMartin Willi if (!p->alg_trunc_len) 3504447bb33SMartin Willi p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; 3514447bb33SMartin Willi 3524447bb33SMartin Willi *algpp = p; 3534447bb33SMartin Willi return 0; 3544447bb33SMartin Willi } 3554447bb33SMartin Willi 3561a6509d9SHerbert Xu static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props, 3571a6509d9SHerbert Xu struct nlattr *rta) 3581a6509d9SHerbert Xu { 3591a6509d9SHerbert Xu struct xfrm_algo_aead *p, *ualg; 3601a6509d9SHerbert Xu struct xfrm_algo_desc *algo; 3611a6509d9SHerbert Xu 3621a6509d9SHerbert Xu if (!rta) 3631a6509d9SHerbert Xu return 0; 3641a6509d9SHerbert Xu 3651a6509d9SHerbert Xu ualg = nla_data(rta); 3661a6509d9SHerbert Xu 3671a6509d9SHerbert Xu algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1); 3681a6509d9SHerbert Xu if (!algo) 3691a6509d9SHerbert Xu return -ENOSYS; 3701a6509d9SHerbert Xu *props = algo->desc.sadb_alg_id; 3711a6509d9SHerbert Xu 3721a6509d9SHerbert Xu p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL); 3731a6509d9SHerbert Xu if (!p) 3741a6509d9SHerbert Xu return -ENOMEM; 3751a6509d9SHerbert Xu 3761a6509d9SHerbert Xu strcpy(p->alg_name, algo->name); 3771a6509d9SHerbert Xu *algpp = p; 3781a6509d9SHerbert Xu return 0; 3791a6509d9SHerbert Xu } 3801a6509d9SHerbert Xu 381e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn, 382e2b19125SSteffen Klassert struct nlattr *rp) 383e2b19125SSteffen Klassert { 384e2b19125SSteffen Klassert struct xfrm_replay_state_esn *up; 385*ecd79187SMathias Krause int ulen; 386e2b19125SSteffen Klassert 387e2b19125SSteffen Klassert if (!replay_esn || !rp) 388e2b19125SSteffen Klassert return 0; 389e2b19125SSteffen Klassert 390e2b19125SSteffen Klassert up = nla_data(rp); 391*ecd79187SMathias Krause ulen = xfrm_replay_state_esn_len(up); 392e2b19125SSteffen Klassert 393*ecd79187SMathias Krause if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen) 394e2b19125SSteffen Klassert return -EINVAL; 395e2b19125SSteffen Klassert 396e2b19125SSteffen Klassert return 0; 397e2b19125SSteffen Klassert } 398e2b19125SSteffen Klassert 399d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn, 400d8647b79SSteffen Klassert struct xfrm_replay_state_esn **preplay_esn, 401d8647b79SSteffen Klassert struct nlattr *rta) 402d8647b79SSteffen Klassert { 403d8647b79SSteffen Klassert struct xfrm_replay_state_esn *p, *pp, *up; 404*ecd79187SMathias Krause int klen, ulen; 405d8647b79SSteffen Klassert 406d8647b79SSteffen Klassert if (!rta) 407d8647b79SSteffen Klassert return 0; 408d8647b79SSteffen Klassert 409d8647b79SSteffen Klassert up = nla_data(rta); 410*ecd79187SMathias Krause klen = xfrm_replay_state_esn_len(up); 411*ecd79187SMathias Krause ulen = nla_len(rta) >= klen ? klen : sizeof(*up); 412d8647b79SSteffen Klassert 413*ecd79187SMathias Krause p = kzalloc(klen, GFP_KERNEL); 414d8647b79SSteffen Klassert if (!p) 415d8647b79SSteffen Klassert return -ENOMEM; 416d8647b79SSteffen Klassert 417*ecd79187SMathias Krause pp = kzalloc(klen, GFP_KERNEL); 418d8647b79SSteffen Klassert if (!pp) { 419d8647b79SSteffen Klassert kfree(p); 420d8647b79SSteffen Klassert return -ENOMEM; 421d8647b79SSteffen Klassert } 422d8647b79SSteffen Klassert 423*ecd79187SMathias Krause memcpy(p, up, ulen); 424*ecd79187SMathias Krause memcpy(pp, up, ulen); 425*ecd79187SMathias Krause 426d8647b79SSteffen Klassert *replay_esn = p; 427d8647b79SSteffen Klassert *preplay_esn = pp; 428d8647b79SSteffen Klassert 429d8647b79SSteffen Klassert return 0; 430d8647b79SSteffen Klassert } 431d8647b79SSteffen Klassert 432661697f7SJoy Latten static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx) 433df71837dSTrent Jaeger { 434df71837dSTrent Jaeger int len = 0; 435df71837dSTrent Jaeger 436df71837dSTrent Jaeger if (xfrm_ctx) { 437df71837dSTrent Jaeger len += sizeof(struct xfrm_user_sec_ctx); 438df71837dSTrent Jaeger len += xfrm_ctx->ctx_len; 439df71837dSTrent Jaeger } 440df71837dSTrent Jaeger return len; 441df71837dSTrent Jaeger } 442df71837dSTrent Jaeger 4431da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 4441da177e4SLinus Torvalds { 4451da177e4SLinus Torvalds memcpy(&x->id, &p->id, sizeof(x->id)); 4461da177e4SLinus Torvalds memcpy(&x->sel, &p->sel, sizeof(x->sel)); 4471da177e4SLinus Torvalds memcpy(&x->lft, &p->lft, sizeof(x->lft)); 4481da177e4SLinus Torvalds x->props.mode = p->mode; 4491da177e4SLinus Torvalds x->props.replay_window = p->replay_window; 4501da177e4SLinus Torvalds x->props.reqid = p->reqid; 4511da177e4SLinus Torvalds x->props.family = p->family; 45254489c14SDavid S. Miller memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr)); 4531da177e4SLinus Torvalds x->props.flags = p->flags; 454196b0036SHerbert Xu 455ccf9b3b8SSteffen Klassert if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC)) 456196b0036SHerbert Xu x->sel.family = p->family; 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 459d51d081dSJamal Hadi Salim /* 460d51d081dSJamal Hadi Salim * someday when pfkey also has support, we could have the code 461d51d081dSJamal Hadi Salim * somehow made shareable and move it to xfrm_state.c - JHS 462d51d081dSJamal Hadi Salim * 463d51d081dSJamal Hadi Salim */ 4645424f32eSThomas Graf static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs) 465d51d081dSJamal Hadi Salim { 4665424f32eSThomas Graf struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 467d8647b79SSteffen Klassert struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL]; 4685424f32eSThomas Graf struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 4695424f32eSThomas Graf struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; 4705424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; 471d51d081dSJamal Hadi Salim 472d8647b79SSteffen Klassert if (re) { 473d8647b79SSteffen Klassert struct xfrm_replay_state_esn *replay_esn; 474d8647b79SSteffen Klassert replay_esn = nla_data(re); 475d8647b79SSteffen Klassert memcpy(x->replay_esn, replay_esn, 476d8647b79SSteffen Klassert xfrm_replay_state_esn_len(replay_esn)); 477d8647b79SSteffen Klassert memcpy(x->preplay_esn, replay_esn, 478d8647b79SSteffen Klassert xfrm_replay_state_esn_len(replay_esn)); 479d8647b79SSteffen Klassert } 480d8647b79SSteffen Klassert 481d51d081dSJamal Hadi Salim if (rp) { 482d51d081dSJamal Hadi Salim struct xfrm_replay_state *replay; 4835424f32eSThomas Graf replay = nla_data(rp); 484d51d081dSJamal Hadi Salim memcpy(&x->replay, replay, sizeof(*replay)); 485d51d081dSJamal Hadi Salim memcpy(&x->preplay, replay, sizeof(*replay)); 486d51d081dSJamal Hadi Salim } 487d51d081dSJamal Hadi Salim 488d51d081dSJamal Hadi Salim if (lt) { 489d51d081dSJamal Hadi Salim struct xfrm_lifetime_cur *ltime; 4905424f32eSThomas Graf ltime = nla_data(lt); 491d51d081dSJamal Hadi Salim x->curlft.bytes = ltime->bytes; 492d51d081dSJamal Hadi Salim x->curlft.packets = ltime->packets; 493d51d081dSJamal Hadi Salim x->curlft.add_time = ltime->add_time; 494d51d081dSJamal Hadi Salim x->curlft.use_time = ltime->use_time; 495d51d081dSJamal Hadi Salim } 496d51d081dSJamal Hadi Salim 497cf5cb79fSThomas Graf if (et) 4985424f32eSThomas Graf x->replay_maxage = nla_get_u32(et); 499d51d081dSJamal Hadi Salim 500cf5cb79fSThomas Graf if (rt) 5015424f32eSThomas Graf x->replay_maxdiff = nla_get_u32(rt); 502d51d081dSJamal Hadi Salim } 503d51d081dSJamal Hadi Salim 504fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net, 505fc34acd3SAlexey Dobriyan struct xfrm_usersa_info *p, 5065424f32eSThomas Graf struct nlattr **attrs, 5071da177e4SLinus Torvalds int *errp) 5081da177e4SLinus Torvalds { 509fc34acd3SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 5101da177e4SLinus Torvalds int err = -ENOMEM; 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds if (!x) 5131da177e4SLinus Torvalds goto error_no_put; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds copy_from_user_state(x, p); 5161da177e4SLinus Torvalds 5171a6509d9SHerbert Xu if ((err = attach_aead(&x->aead, &x->props.ealgo, 5181a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD]))) 5191a6509d9SHerbert Xu goto error; 5204447bb33SMartin Willi if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo, 5214447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC]))) 5224447bb33SMartin Willi goto error; 5234447bb33SMartin Willi if (!x->props.aalgo) { 5244447bb33SMartin Willi if ((err = attach_auth(&x->aalg, &x->props.aalgo, 52535a7aa08SThomas Graf attrs[XFRMA_ALG_AUTH]))) 5261da177e4SLinus Torvalds goto error; 5274447bb33SMartin Willi } 5281da177e4SLinus Torvalds if ((err = attach_one_algo(&x->ealg, &x->props.ealgo, 5291da177e4SLinus Torvalds xfrm_ealg_get_byname, 53035a7aa08SThomas Graf attrs[XFRMA_ALG_CRYPT]))) 5311da177e4SLinus Torvalds goto error; 5321da177e4SLinus Torvalds if ((err = attach_one_algo(&x->calg, &x->props.calgo, 5331da177e4SLinus Torvalds xfrm_calg_get_byname, 53435a7aa08SThomas Graf attrs[XFRMA_ALG_COMP]))) 5351da177e4SLinus Torvalds goto error; 536fd21150aSThomas Graf 537fd21150aSThomas Graf if (attrs[XFRMA_ENCAP]) { 538fd21150aSThomas Graf x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), 539fd21150aSThomas Graf sizeof(*x->encap), GFP_KERNEL); 540fd21150aSThomas Graf if (x->encap == NULL) 5411da177e4SLinus Torvalds goto error; 542fd21150aSThomas Graf } 543fd21150aSThomas Graf 54435d2856bSMartin Willi if (attrs[XFRMA_TFCPAD]) 54535d2856bSMartin Willi x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]); 54635d2856bSMartin Willi 547fd21150aSThomas Graf if (attrs[XFRMA_COADDR]) { 548fd21150aSThomas Graf x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]), 549fd21150aSThomas Graf sizeof(*x->coaddr), GFP_KERNEL); 550fd21150aSThomas Graf if (x->coaddr == NULL) 551060f02a3SNoriaki TAKAMIYA goto error; 552fd21150aSThomas Graf } 553fd21150aSThomas Graf 5546f26b61eSJamal Hadi Salim xfrm_mark_get(attrs, &x->mark); 5556f26b61eSJamal Hadi Salim 556a454f0ccSWei Yongjun err = __xfrm_init_state(x, false); 5571da177e4SLinus Torvalds if (err) 5581da177e4SLinus Torvalds goto error; 5591da177e4SLinus Torvalds 560fd21150aSThomas Graf if (attrs[XFRMA_SEC_CTX] && 561fd21150aSThomas Graf security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX]))) 562df71837dSTrent Jaeger goto error; 563df71837dSTrent Jaeger 564d8647b79SSteffen Klassert if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn, 565d8647b79SSteffen Klassert attrs[XFRMA_REPLAY_ESN_VAL]))) 566d8647b79SSteffen Klassert goto error; 567d8647b79SSteffen Klassert 5681da177e4SLinus Torvalds x->km.seq = p->seq; 569b27aeadbSAlexey Dobriyan x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth; 570d51d081dSJamal Hadi Salim /* sysctl_xfrm_aevent_etime is in 100ms units */ 571b27aeadbSAlexey Dobriyan x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M; 572d51d081dSJamal Hadi Salim 5739fdc4883SSteffen Klassert if ((err = xfrm_init_replay(x))) 5749fdc4883SSteffen Klassert goto error; 575d51d081dSJamal Hadi Salim 5769fdc4883SSteffen Klassert /* override default values from above */ 5775424f32eSThomas Graf xfrm_update_ae_params(x, attrs); 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds return x; 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds error: 5821da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 5831da177e4SLinus Torvalds xfrm_state_put(x); 5841da177e4SLinus Torvalds error_no_put: 5851da177e4SLinus Torvalds *errp = err; 5861da177e4SLinus Torvalds return NULL; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 58922e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 5905424f32eSThomas Graf struct nlattr **attrs) 5911da177e4SLinus Torvalds { 592fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 5937b67c857SThomas Graf struct xfrm_usersa_info *p = nlmsg_data(nlh); 5941da177e4SLinus Torvalds struct xfrm_state *x; 5951da177e4SLinus Torvalds int err; 59626b15dadSJamal Hadi Salim struct km_event c; 597c53fa1edSPatrick McHardy uid_t loginuid = audit_get_loginuid(current); 598c53fa1edSPatrick McHardy u32 sessionid = audit_get_sessionid(current); 599c53fa1edSPatrick McHardy u32 sid; 6001da177e4SLinus Torvalds 60135a7aa08SThomas Graf err = verify_newsa_info(p, attrs); 6021da177e4SLinus Torvalds if (err) 6031da177e4SLinus Torvalds return err; 6041da177e4SLinus Torvalds 605fc34acd3SAlexey Dobriyan x = xfrm_state_construct(net, p, attrs, &err); 6061da177e4SLinus Torvalds if (!x) 6071da177e4SLinus Torvalds return err; 6081da177e4SLinus Torvalds 60926b15dadSJamal Hadi Salim xfrm_state_hold(x); 6101da177e4SLinus Torvalds if (nlh->nlmsg_type == XFRM_MSG_NEWSA) 6111da177e4SLinus Torvalds err = xfrm_state_add(x); 6121da177e4SLinus Torvalds else 6131da177e4SLinus Torvalds err = xfrm_state_update(x); 6141da177e4SLinus Torvalds 615c53fa1edSPatrick McHardy security_task_getsecid(current, &sid); 6162532386fSEric Paris xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid); 617161a09e7SJoy Latten 6181da177e4SLinus Torvalds if (err < 0) { 6191da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 62021380b81SHerbert Xu __xfrm_state_put(x); 6217d6dfe1fSPatrick McHardy goto out; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 62426b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 62526b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 626f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 62726b15dadSJamal Hadi Salim 62826b15dadSJamal Hadi Salim km_state_notify(x, &c); 6297d6dfe1fSPatrick McHardy out: 63026b15dadSJamal Hadi Salim xfrm_state_put(x); 6311da177e4SLinus Torvalds return err; 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds 634fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net, 635fc34acd3SAlexey Dobriyan struct xfrm_usersa_id *p, 6365424f32eSThomas Graf struct nlattr **attrs, 637eb2971b6SMasahide NAKAMURA int *errp) 638eb2971b6SMasahide NAKAMURA { 639eb2971b6SMasahide NAKAMURA struct xfrm_state *x = NULL; 6406f26b61eSJamal Hadi Salim struct xfrm_mark m; 641eb2971b6SMasahide NAKAMURA int err; 6426f26b61eSJamal Hadi Salim u32 mark = xfrm_mark_get(attrs, &m); 643eb2971b6SMasahide NAKAMURA 644eb2971b6SMasahide NAKAMURA if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { 645eb2971b6SMasahide NAKAMURA err = -ESRCH; 6466f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family); 647eb2971b6SMasahide NAKAMURA } else { 648eb2971b6SMasahide NAKAMURA xfrm_address_t *saddr = NULL; 649eb2971b6SMasahide NAKAMURA 65035a7aa08SThomas Graf verify_one_addr(attrs, XFRMA_SRCADDR, &saddr); 651eb2971b6SMasahide NAKAMURA if (!saddr) { 652eb2971b6SMasahide NAKAMURA err = -EINVAL; 653eb2971b6SMasahide NAKAMURA goto out; 654eb2971b6SMasahide NAKAMURA } 655eb2971b6SMasahide NAKAMURA 6569abbffeeSMasahide NAKAMURA err = -ESRCH; 6576f26b61eSJamal Hadi Salim x = xfrm_state_lookup_byaddr(net, mark, 6586f26b61eSJamal Hadi Salim &p->daddr, saddr, 659221df1edSAlexey Dobriyan p->proto, p->family); 660eb2971b6SMasahide NAKAMURA } 661eb2971b6SMasahide NAKAMURA 662eb2971b6SMasahide NAKAMURA out: 663eb2971b6SMasahide NAKAMURA if (!x && errp) 664eb2971b6SMasahide NAKAMURA *errp = err; 665eb2971b6SMasahide NAKAMURA return x; 666eb2971b6SMasahide NAKAMURA } 667eb2971b6SMasahide NAKAMURA 66822e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 6695424f32eSThomas Graf struct nlattr **attrs) 6701da177e4SLinus Torvalds { 671fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 6721da177e4SLinus Torvalds struct xfrm_state *x; 673eb2971b6SMasahide NAKAMURA int err = -ESRCH; 67426b15dadSJamal Hadi Salim struct km_event c; 6757b67c857SThomas Graf struct xfrm_usersa_id *p = nlmsg_data(nlh); 676c53fa1edSPatrick McHardy uid_t loginuid = audit_get_loginuid(current); 677c53fa1edSPatrick McHardy u32 sessionid = audit_get_sessionid(current); 678c53fa1edSPatrick McHardy u32 sid; 6791da177e4SLinus Torvalds 680fc34acd3SAlexey Dobriyan x = xfrm_user_state_lookup(net, p, attrs, &err); 6811da177e4SLinus Torvalds if (x == NULL) 682eb2971b6SMasahide NAKAMURA return err; 6831da177e4SLinus Torvalds 6846f68dc37SDavid S. Miller if ((err = security_xfrm_state_delete(x)) != 0) 685c8c05a8eSCatherine Zhang goto out; 686c8c05a8eSCatherine Zhang 6871da177e4SLinus Torvalds if (xfrm_state_kern(x)) { 688c8c05a8eSCatherine Zhang err = -EPERM; 689c8c05a8eSCatherine Zhang goto out; 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds 69226b15dadSJamal Hadi Salim err = xfrm_state_delete(x); 693161a09e7SJoy Latten 694c8c05a8eSCatherine Zhang if (err < 0) 695c8c05a8eSCatherine Zhang goto out; 69626b15dadSJamal Hadi Salim 69726b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 69826b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 699f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 70026b15dadSJamal Hadi Salim km_state_notify(x, &c); 7011da177e4SLinus Torvalds 702c8c05a8eSCatherine Zhang out: 703c53fa1edSPatrick McHardy security_task_getsecid(current, &sid); 7042532386fSEric Paris xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid); 705c8c05a8eSCatherine Zhang xfrm_state_put(x); 70626b15dadSJamal Hadi Salim return err; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 7101da177e4SLinus Torvalds { 711f778a636SMathias Krause memset(p, 0, sizeof(*p)); 7121da177e4SLinus Torvalds memcpy(&p->id, &x->id, sizeof(p->id)); 7131da177e4SLinus Torvalds memcpy(&p->sel, &x->sel, sizeof(p->sel)); 7141da177e4SLinus Torvalds memcpy(&p->lft, &x->lft, sizeof(p->lft)); 7151da177e4SLinus Torvalds memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); 7161da177e4SLinus Torvalds memcpy(&p->stats, &x->stats, sizeof(p->stats)); 71754489c14SDavid S. Miller memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); 7181da177e4SLinus Torvalds p->mode = x->props.mode; 7191da177e4SLinus Torvalds p->replay_window = x->props.replay_window; 7201da177e4SLinus Torvalds p->reqid = x->props.reqid; 7211da177e4SLinus Torvalds p->family = x->props.family; 7221da177e4SLinus Torvalds p->flags = x->props.flags; 7231da177e4SLinus Torvalds p->seq = x->km.seq; 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds struct xfrm_dump_info { 7271da177e4SLinus Torvalds struct sk_buff *in_skb; 7281da177e4SLinus Torvalds struct sk_buff *out_skb; 7291da177e4SLinus Torvalds u32 nlmsg_seq; 7301da177e4SLinus Torvalds u16 nlmsg_flags; 7311da177e4SLinus Torvalds }; 7321da177e4SLinus Torvalds 733c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) 734c0144beaSThomas Graf { 735c0144beaSThomas Graf struct xfrm_user_sec_ctx *uctx; 736c0144beaSThomas Graf struct nlattr *attr; 73768325d3bSHerbert Xu int ctx_size = sizeof(*uctx) + s->ctx_len; 738c0144beaSThomas Graf 739c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size); 740c0144beaSThomas Graf if (attr == NULL) 741c0144beaSThomas Graf return -EMSGSIZE; 742c0144beaSThomas Graf 743c0144beaSThomas Graf uctx = nla_data(attr); 744c0144beaSThomas Graf uctx->exttype = XFRMA_SEC_CTX; 745c0144beaSThomas Graf uctx->len = ctx_size; 746c0144beaSThomas Graf uctx->ctx_doi = s->ctx_doi; 747c0144beaSThomas Graf uctx->ctx_alg = s->ctx_alg; 748c0144beaSThomas Graf uctx->ctx_len = s->ctx_len; 749c0144beaSThomas Graf memcpy(uctx + 1, s->ctx_str, s->ctx_len); 750c0144beaSThomas Graf 751c0144beaSThomas Graf return 0; 752c0144beaSThomas Graf } 753c0144beaSThomas Graf 7544447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb) 7554447bb33SMartin Willi { 7564447bb33SMartin Willi struct xfrm_algo *algo; 7574447bb33SMartin Willi struct nlattr *nla; 7584447bb33SMartin Willi 7594447bb33SMartin Willi nla = nla_reserve(skb, XFRMA_ALG_AUTH, 7604447bb33SMartin Willi sizeof(*algo) + (auth->alg_key_len + 7) / 8); 7614447bb33SMartin Willi if (!nla) 7624447bb33SMartin Willi return -EMSGSIZE; 7634447bb33SMartin Willi 7644447bb33SMartin Willi algo = nla_data(nla); 7654c87308bSMathias Krause strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name)); 7664447bb33SMartin Willi memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8); 7674447bb33SMartin Willi algo->alg_key_len = auth->alg_key_len; 7684447bb33SMartin Willi 7694447bb33SMartin Willi return 0; 7704447bb33SMartin Willi } 7714447bb33SMartin Willi 77268325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */ 77368325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x, 77468325d3bSHerbert Xu struct xfrm_usersa_info *p, 77568325d3bSHerbert Xu struct sk_buff *skb) 7761da177e4SLinus Torvalds { 7771d1e34ddSDavid S. Miller int ret = 0; 7781d1e34ddSDavid S. Miller 7791da177e4SLinus Torvalds copy_to_user_state(x, p); 7801da177e4SLinus Torvalds 7811d1e34ddSDavid S. Miller if (x->coaddr) { 7821d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); 7831d1e34ddSDavid S. Miller if (ret) 7841d1e34ddSDavid S. Miller goto out; 7851d1e34ddSDavid S. Miller } 7861d1e34ddSDavid S. Miller if (x->lastused) { 7871d1e34ddSDavid S. Miller ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused); 7881d1e34ddSDavid S. Miller if (ret) 7891d1e34ddSDavid S. Miller goto out; 7901d1e34ddSDavid S. Miller } 7911d1e34ddSDavid S. Miller if (x->aead) { 7921d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead); 7931d1e34ddSDavid S. Miller if (ret) 7941d1e34ddSDavid S. Miller goto out; 7951d1e34ddSDavid S. Miller } 7961d1e34ddSDavid S. Miller if (x->aalg) { 7971d1e34ddSDavid S. Miller ret = copy_to_user_auth(x->aalg, skb); 7981d1e34ddSDavid S. Miller if (!ret) 7991d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC, 8001d1e34ddSDavid S. Miller xfrm_alg_auth_len(x->aalg), x->aalg); 8011d1e34ddSDavid S. Miller if (ret) 8021d1e34ddSDavid S. Miller goto out; 8031d1e34ddSDavid S. Miller } 8041d1e34ddSDavid S. Miller if (x->ealg) { 8051d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg); 8061d1e34ddSDavid S. Miller if (ret) 8071d1e34ddSDavid S. Miller goto out; 8081d1e34ddSDavid S. Miller } 8091d1e34ddSDavid S. Miller if (x->calg) { 8101d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); 8111d1e34ddSDavid S. Miller if (ret) 8121d1e34ddSDavid S. Miller goto out; 8131d1e34ddSDavid S. Miller } 8141d1e34ddSDavid S. Miller if (x->encap) { 8151d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); 8161d1e34ddSDavid S. Miller if (ret) 8171d1e34ddSDavid S. Miller goto out; 8181d1e34ddSDavid S. Miller } 8191d1e34ddSDavid S. Miller if (x->tfcpad) { 8201d1e34ddSDavid S. Miller ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad); 8211d1e34ddSDavid S. Miller if (ret) 8221d1e34ddSDavid S. Miller goto out; 8231d1e34ddSDavid S. Miller } 8241d1e34ddSDavid S. Miller ret = xfrm_mark_put(skb, &x->mark); 8251d1e34ddSDavid S. Miller if (ret) 8261d1e34ddSDavid S. Miller goto out; 8271d1e34ddSDavid S. Miller if (x->replay_esn) { 8281d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 829d0fde795SDavid S. Miller xfrm_replay_state_esn_len(x->replay_esn), 8301d1e34ddSDavid S. Miller x->replay_esn); 8311d1e34ddSDavid S. Miller if (ret) 8321d1e34ddSDavid S. Miller goto out; 8331d1e34ddSDavid S. Miller } 8341d1e34ddSDavid S. Miller if (x->security) 8351d1e34ddSDavid S. Miller ret = copy_sec_ctx(x->security, skb); 8361d1e34ddSDavid S. Miller out: 8371d1e34ddSDavid S. Miller return ret; 83868325d3bSHerbert Xu } 83968325d3bSHerbert Xu 84068325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr) 84168325d3bSHerbert Xu { 84268325d3bSHerbert Xu struct xfrm_dump_info *sp = ptr; 84368325d3bSHerbert Xu struct sk_buff *in_skb = sp->in_skb; 84468325d3bSHerbert Xu struct sk_buff *skb = sp->out_skb; 84568325d3bSHerbert Xu struct xfrm_usersa_info *p; 84668325d3bSHerbert Xu struct nlmsghdr *nlh; 84768325d3bSHerbert Xu int err; 84868325d3bSHerbert Xu 84968325d3bSHerbert Xu nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, 85068325d3bSHerbert Xu XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags); 85168325d3bSHerbert Xu if (nlh == NULL) 85268325d3bSHerbert Xu return -EMSGSIZE; 85368325d3bSHerbert Xu 85468325d3bSHerbert Xu p = nlmsg_data(nlh); 85568325d3bSHerbert Xu 85668325d3bSHerbert Xu err = copy_to_user_state_extra(x, p, skb); 8571d1e34ddSDavid S. Miller if (err) { 8589825069dSThomas Graf nlmsg_cancel(skb, nlh); 85968325d3bSHerbert Xu return err; 8601da177e4SLinus Torvalds } 8611d1e34ddSDavid S. Miller nlmsg_end(skb, nlh); 8621d1e34ddSDavid S. Miller return 0; 8631d1e34ddSDavid S. Miller } 8641da177e4SLinus Torvalds 8654c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb) 8664c563f76STimo Teras { 8674c563f76STimo Teras struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 8684c563f76STimo Teras xfrm_state_walk_done(walk); 8694c563f76STimo Teras return 0; 8704c563f76STimo Teras } 8714c563f76STimo Teras 8721da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) 8731da177e4SLinus Torvalds { 874fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 8754c563f76STimo Teras struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 8761da177e4SLinus Torvalds struct xfrm_dump_info info; 8771da177e4SLinus Torvalds 8784c563f76STimo Teras BUILD_BUG_ON(sizeof(struct xfrm_state_walk) > 8794c563f76STimo Teras sizeof(cb->args) - sizeof(cb->args[0])); 8804c563f76STimo Teras 8811da177e4SLinus Torvalds info.in_skb = cb->skb; 8821da177e4SLinus Torvalds info.out_skb = skb; 8831da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 8841da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 8854c563f76STimo Teras 8864c563f76STimo Teras if (!cb->args[0]) { 8874c563f76STimo Teras cb->args[0] = 1; 8884c563f76STimo Teras xfrm_state_walk_init(walk, 0); 8894c563f76STimo Teras } 8904c563f76STimo Teras 891fc34acd3SAlexey Dobriyan (void) xfrm_state_walk(net, walk, dump_one_state, &info); 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds return skb->len; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, 8971da177e4SLinus Torvalds struct xfrm_state *x, u32 seq) 8981da177e4SLinus Torvalds { 8991da177e4SLinus Torvalds struct xfrm_dump_info info; 9001da177e4SLinus Torvalds struct sk_buff *skb; 901864745d2SMathias Krause int err; 9021da177e4SLinus Torvalds 9037deb2264SThomas Graf skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 9041da177e4SLinus Torvalds if (!skb) 9051da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds info.in_skb = in_skb; 9081da177e4SLinus Torvalds info.out_skb = skb; 9091da177e4SLinus Torvalds info.nlmsg_seq = seq; 9101da177e4SLinus Torvalds info.nlmsg_flags = 0; 9111da177e4SLinus Torvalds 912864745d2SMathias Krause err = dump_one_state(x, 0, &info); 913864745d2SMathias Krause if (err) { 9141da177e4SLinus Torvalds kfree_skb(skb); 915864745d2SMathias Krause return ERR_PTR(err); 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds return skb; 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 9217deb2264SThomas Graf static inline size_t xfrm_spdinfo_msgsize(void) 9227deb2264SThomas Graf { 9237deb2264SThomas Graf return NLMSG_ALIGN(4) 9247deb2264SThomas Graf + nla_total_size(sizeof(struct xfrmu_spdinfo)) 9257deb2264SThomas Graf + nla_total_size(sizeof(struct xfrmu_spdhinfo)); 9267deb2264SThomas Graf } 9277deb2264SThomas Graf 928e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net, 929e071041bSAlexey Dobriyan u32 pid, u32 seq, u32 flags) 930ecfd6b18SJamal Hadi Salim { 9315a6d3416SJamal Hadi Salim struct xfrmk_spdinfo si; 9325a6d3416SJamal Hadi Salim struct xfrmu_spdinfo spc; 9335a6d3416SJamal Hadi Salim struct xfrmu_spdhinfo sph; 934ecfd6b18SJamal Hadi Salim struct nlmsghdr *nlh; 9351d1e34ddSDavid S. Miller int err; 936ecfd6b18SJamal Hadi Salim u32 *f; 937ecfd6b18SJamal Hadi Salim 938ecfd6b18SJamal Hadi Salim nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); 93925985edcSLucas De Marchi if (nlh == NULL) /* shouldn't really happen ... */ 940ecfd6b18SJamal Hadi Salim return -EMSGSIZE; 941ecfd6b18SJamal Hadi Salim 942ecfd6b18SJamal Hadi Salim f = nlmsg_data(nlh); 943ecfd6b18SJamal Hadi Salim *f = flags; 944e071041bSAlexey Dobriyan xfrm_spd_getinfo(net, &si); 9455a6d3416SJamal Hadi Salim spc.incnt = si.incnt; 9465a6d3416SJamal Hadi Salim spc.outcnt = si.outcnt; 9475a6d3416SJamal Hadi Salim spc.fwdcnt = si.fwdcnt; 9485a6d3416SJamal Hadi Salim spc.inscnt = si.inscnt; 9495a6d3416SJamal Hadi Salim spc.outscnt = si.outscnt; 9505a6d3416SJamal Hadi Salim spc.fwdscnt = si.fwdscnt; 9515a6d3416SJamal Hadi Salim sph.spdhcnt = si.spdhcnt; 9525a6d3416SJamal Hadi Salim sph.spdhmcnt = si.spdhmcnt; 953ecfd6b18SJamal Hadi Salim 9541d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc); 9551d1e34ddSDavid S. Miller if (!err) 9561d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph); 9571d1e34ddSDavid S. Miller if (err) { 9581d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 9591d1e34ddSDavid S. Miller return err; 9601d1e34ddSDavid S. Miller } 961ecfd6b18SJamal Hadi Salim 962ecfd6b18SJamal Hadi Salim return nlmsg_end(skb, nlh); 963ecfd6b18SJamal Hadi Salim } 964ecfd6b18SJamal Hadi Salim 965ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 9665424f32eSThomas Graf struct nlattr **attrs) 967ecfd6b18SJamal Hadi Salim { 968a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 969ecfd6b18SJamal Hadi Salim struct sk_buff *r_skb; 9707b67c857SThomas Graf u32 *flags = nlmsg_data(nlh); 971ecfd6b18SJamal Hadi Salim u32 spid = NETLINK_CB(skb).pid; 972ecfd6b18SJamal Hadi Salim u32 seq = nlh->nlmsg_seq; 973ecfd6b18SJamal Hadi Salim 9747deb2264SThomas Graf r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); 975ecfd6b18SJamal Hadi Salim if (r_skb == NULL) 976ecfd6b18SJamal Hadi Salim return -ENOMEM; 977ecfd6b18SJamal Hadi Salim 978e071041bSAlexey Dobriyan if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0) 979ecfd6b18SJamal Hadi Salim BUG(); 980ecfd6b18SJamal Hadi Salim 981a6483b79SAlexey Dobriyan return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid); 982ecfd6b18SJamal Hadi Salim } 983ecfd6b18SJamal Hadi Salim 9847deb2264SThomas Graf static inline size_t xfrm_sadinfo_msgsize(void) 9857deb2264SThomas Graf { 9867deb2264SThomas Graf return NLMSG_ALIGN(4) 9877deb2264SThomas Graf + nla_total_size(sizeof(struct xfrmu_sadhinfo)) 9887deb2264SThomas Graf + nla_total_size(4); /* XFRMA_SAD_CNT */ 9897deb2264SThomas Graf } 9907deb2264SThomas Graf 991e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net, 992e071041bSAlexey Dobriyan u32 pid, u32 seq, u32 flags) 99328d8909bSJamal Hadi Salim { 994af11e316SJamal Hadi Salim struct xfrmk_sadinfo si; 995af11e316SJamal Hadi Salim struct xfrmu_sadhinfo sh; 99628d8909bSJamal Hadi Salim struct nlmsghdr *nlh; 9971d1e34ddSDavid S. Miller int err; 99828d8909bSJamal Hadi Salim u32 *f; 99928d8909bSJamal Hadi Salim 100028d8909bSJamal Hadi Salim nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); 100125985edcSLucas De Marchi if (nlh == NULL) /* shouldn't really happen ... */ 100228d8909bSJamal Hadi Salim return -EMSGSIZE; 100328d8909bSJamal Hadi Salim 100428d8909bSJamal Hadi Salim f = nlmsg_data(nlh); 100528d8909bSJamal Hadi Salim *f = flags; 1006e071041bSAlexey Dobriyan xfrm_sad_getinfo(net, &si); 100728d8909bSJamal Hadi Salim 1008af11e316SJamal Hadi Salim sh.sadhmcnt = si.sadhmcnt; 1009af11e316SJamal Hadi Salim sh.sadhcnt = si.sadhcnt; 1010af11e316SJamal Hadi Salim 10111d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt); 10121d1e34ddSDavid S. Miller if (!err) 10131d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); 10141d1e34ddSDavid S. Miller if (err) { 10151d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 10161d1e34ddSDavid S. Miller return err; 10171d1e34ddSDavid S. Miller } 101828d8909bSJamal Hadi Salim 101928d8909bSJamal Hadi Salim return nlmsg_end(skb, nlh); 102028d8909bSJamal Hadi Salim } 102128d8909bSJamal Hadi Salim 102228d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 10235424f32eSThomas Graf struct nlattr **attrs) 102428d8909bSJamal Hadi Salim { 1025a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 102628d8909bSJamal Hadi Salim struct sk_buff *r_skb; 10277b67c857SThomas Graf u32 *flags = nlmsg_data(nlh); 102828d8909bSJamal Hadi Salim u32 spid = NETLINK_CB(skb).pid; 102928d8909bSJamal Hadi Salim u32 seq = nlh->nlmsg_seq; 103028d8909bSJamal Hadi Salim 10317deb2264SThomas Graf r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); 103228d8909bSJamal Hadi Salim if (r_skb == NULL) 103328d8909bSJamal Hadi Salim return -ENOMEM; 103428d8909bSJamal Hadi Salim 1035e071041bSAlexey Dobriyan if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0) 103628d8909bSJamal Hadi Salim BUG(); 103728d8909bSJamal Hadi Salim 1038a6483b79SAlexey Dobriyan return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid); 103928d8909bSJamal Hadi Salim } 104028d8909bSJamal Hadi Salim 104122e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 10425424f32eSThomas Graf struct nlattr **attrs) 10431da177e4SLinus Torvalds { 1044fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 10457b67c857SThomas Graf struct xfrm_usersa_id *p = nlmsg_data(nlh); 10461da177e4SLinus Torvalds struct xfrm_state *x; 10471da177e4SLinus Torvalds struct sk_buff *resp_skb; 1048eb2971b6SMasahide NAKAMURA int err = -ESRCH; 10491da177e4SLinus Torvalds 1050fc34acd3SAlexey Dobriyan x = xfrm_user_state_lookup(net, p, attrs, &err); 10511da177e4SLinus Torvalds if (x == NULL) 10521da177e4SLinus Torvalds goto out_noput; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 10551da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 10561da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 10571da177e4SLinus Torvalds } else { 1058a6483b79SAlexey Dobriyan err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid); 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds xfrm_state_put(x); 10611da177e4SLinus Torvalds out_noput: 10621da177e4SLinus Torvalds return err; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds static int verify_userspi_info(struct xfrm_userspi_info *p) 10661da177e4SLinus Torvalds { 10671da177e4SLinus Torvalds switch (p->info.id.proto) { 10681da177e4SLinus Torvalds case IPPROTO_AH: 10691da177e4SLinus Torvalds case IPPROTO_ESP: 10701da177e4SLinus Torvalds break; 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds case IPPROTO_COMP: 10731da177e4SLinus Torvalds /* IPCOMP spi is 16-bits. */ 10741da177e4SLinus Torvalds if (p->max >= 0x10000) 10751da177e4SLinus Torvalds return -EINVAL; 10761da177e4SLinus Torvalds break; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds default: 10791da177e4SLinus Torvalds return -EINVAL; 10803ff50b79SStephen Hemminger } 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds if (p->min > p->max) 10831da177e4SLinus Torvalds return -EINVAL; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds return 0; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 108822e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, 10895424f32eSThomas Graf struct nlattr **attrs) 10901da177e4SLinus Torvalds { 1091fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 10921da177e4SLinus Torvalds struct xfrm_state *x; 10931da177e4SLinus Torvalds struct xfrm_userspi_info *p; 10941da177e4SLinus Torvalds struct sk_buff *resp_skb; 10951da177e4SLinus Torvalds xfrm_address_t *daddr; 10961da177e4SLinus Torvalds int family; 10971da177e4SLinus Torvalds int err; 10986f26b61eSJamal Hadi Salim u32 mark; 10996f26b61eSJamal Hadi Salim struct xfrm_mark m; 11001da177e4SLinus Torvalds 11017b67c857SThomas Graf p = nlmsg_data(nlh); 11021da177e4SLinus Torvalds err = verify_userspi_info(p); 11031da177e4SLinus Torvalds if (err) 11041da177e4SLinus Torvalds goto out_noput; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds family = p->info.family; 11071da177e4SLinus Torvalds daddr = &p->info.id.daddr; 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds x = NULL; 11106f26b61eSJamal Hadi Salim 11116f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 11121da177e4SLinus Torvalds if (p->info.seq) { 11136f26b61eSJamal Hadi Salim x = xfrm_find_acq_byseq(net, mark, p->info.seq); 11141da177e4SLinus Torvalds if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) { 11151da177e4SLinus Torvalds xfrm_state_put(x); 11161da177e4SLinus Torvalds x = NULL; 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds if (!x) 11216f26b61eSJamal Hadi Salim x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid, 11221da177e4SLinus Torvalds p->info.id.proto, daddr, 11231da177e4SLinus Torvalds &p->info.saddr, 1, 11241da177e4SLinus Torvalds family); 11251da177e4SLinus Torvalds err = -ENOENT; 11261da177e4SLinus Torvalds if (x == NULL) 11271da177e4SLinus Torvalds goto out_noput; 11281da177e4SLinus Torvalds 1129658b219eSHerbert Xu err = xfrm_alloc_spi(x, p->min, p->max); 1130658b219eSHerbert Xu if (err) 1131658b219eSHerbert Xu goto out; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 11341da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 11351da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 11361da177e4SLinus Torvalds goto out; 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 1139a6483b79SAlexey Dobriyan err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid); 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds out: 11421da177e4SLinus Torvalds xfrm_state_put(x); 11431da177e4SLinus Torvalds out_noput: 11441da177e4SLinus Torvalds return err; 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 1147b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir) 11481da177e4SLinus Torvalds { 11491da177e4SLinus Torvalds switch (dir) { 11501da177e4SLinus Torvalds case XFRM_POLICY_IN: 11511da177e4SLinus Torvalds case XFRM_POLICY_OUT: 11521da177e4SLinus Torvalds case XFRM_POLICY_FWD: 11531da177e4SLinus Torvalds break; 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds default: 11561da177e4SLinus Torvalds return -EINVAL; 11573ff50b79SStephen Hemminger } 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds return 0; 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds 1162b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type) 1163f7b6983fSMasahide NAKAMURA { 1164f7b6983fSMasahide NAKAMURA switch (type) { 1165f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_MAIN: 1166f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 1167f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_SUB: 1168f7b6983fSMasahide NAKAMURA #endif 1169f7b6983fSMasahide NAKAMURA break; 1170f7b6983fSMasahide NAKAMURA 1171f7b6983fSMasahide NAKAMURA default: 1172f7b6983fSMasahide NAKAMURA return -EINVAL; 11733ff50b79SStephen Hemminger } 1174f7b6983fSMasahide NAKAMURA 1175f7b6983fSMasahide NAKAMURA return 0; 1176f7b6983fSMasahide NAKAMURA } 1177f7b6983fSMasahide NAKAMURA 11781da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) 11791da177e4SLinus Torvalds { 11801da177e4SLinus Torvalds switch (p->share) { 11811da177e4SLinus Torvalds case XFRM_SHARE_ANY: 11821da177e4SLinus Torvalds case XFRM_SHARE_SESSION: 11831da177e4SLinus Torvalds case XFRM_SHARE_USER: 11841da177e4SLinus Torvalds case XFRM_SHARE_UNIQUE: 11851da177e4SLinus Torvalds break; 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds default: 11881da177e4SLinus Torvalds return -EINVAL; 11893ff50b79SStephen Hemminger } 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds switch (p->action) { 11921da177e4SLinus Torvalds case XFRM_POLICY_ALLOW: 11931da177e4SLinus Torvalds case XFRM_POLICY_BLOCK: 11941da177e4SLinus Torvalds break; 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds default: 11971da177e4SLinus Torvalds return -EINVAL; 11983ff50b79SStephen Hemminger } 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds switch (p->sel.family) { 12011da177e4SLinus Torvalds case AF_INET: 12021da177e4SLinus Torvalds break; 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds case AF_INET6: 1205dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 12061da177e4SLinus Torvalds break; 12071da177e4SLinus Torvalds #else 12081da177e4SLinus Torvalds return -EAFNOSUPPORT; 12091da177e4SLinus Torvalds #endif 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds default: 12121da177e4SLinus Torvalds return -EINVAL; 12133ff50b79SStephen Hemminger } 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds return verify_policy_dir(p->dir); 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds 12185424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) 1219df71837dSTrent Jaeger { 12205424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1221df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx; 1222df71837dSTrent Jaeger 1223df71837dSTrent Jaeger if (!rt) 1224df71837dSTrent Jaeger return 0; 1225df71837dSTrent Jaeger 12265424f32eSThomas Graf uctx = nla_data(rt); 122703e1ad7bSPaul Moore return security_xfrm_policy_alloc(&pol->security, uctx); 1228df71837dSTrent Jaeger } 1229df71837dSTrent Jaeger 12301da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, 12311da177e4SLinus Torvalds int nr) 12321da177e4SLinus Torvalds { 12331da177e4SLinus Torvalds int i; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds xp->xfrm_nr = nr; 12361da177e4SLinus Torvalds for (i = 0; i < nr; i++, ut++) { 12371da177e4SLinus Torvalds struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); 12401da177e4SLinus Torvalds memcpy(&t->saddr, &ut->saddr, 12411da177e4SLinus Torvalds sizeof(xfrm_address_t)); 12421da177e4SLinus Torvalds t->reqid = ut->reqid; 12431da177e4SLinus Torvalds t->mode = ut->mode; 12441da177e4SLinus Torvalds t->share = ut->share; 12451da177e4SLinus Torvalds t->optional = ut->optional; 12461da177e4SLinus Torvalds t->aalgos = ut->aalgos; 12471da177e4SLinus Torvalds t->ealgos = ut->ealgos; 12481da177e4SLinus Torvalds t->calgos = ut->calgos; 1249c5d18e98SHerbert Xu /* If all masks are ~0, then we allow all algorithms. */ 1250c5d18e98SHerbert Xu t->allalgs = !~(t->aalgos & t->ealgos & t->calgos); 12518511d01dSMiika Komu t->encap_family = ut->family; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 1255b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family) 1256b4ad86bfSDavid S. Miller { 1257b4ad86bfSDavid S. Miller int i; 1258b4ad86bfSDavid S. Miller 1259b4ad86bfSDavid S. Miller if (nr > XFRM_MAX_DEPTH) 1260b4ad86bfSDavid S. Miller return -EINVAL; 1261b4ad86bfSDavid S. Miller 1262b4ad86bfSDavid S. Miller for (i = 0; i < nr; i++) { 1263b4ad86bfSDavid S. Miller /* We never validated the ut->family value, so many 1264b4ad86bfSDavid S. Miller * applications simply leave it at zero. The check was 1265b4ad86bfSDavid S. Miller * never made and ut->family was ignored because all 1266b4ad86bfSDavid S. Miller * templates could be assumed to have the same family as 1267b4ad86bfSDavid S. Miller * the policy itself. Now that we will have ipv4-in-ipv6 1268b4ad86bfSDavid S. Miller * and ipv6-in-ipv4 tunnels, this is no longer true. 1269b4ad86bfSDavid S. Miller */ 1270b4ad86bfSDavid S. Miller if (!ut[i].family) 1271b4ad86bfSDavid S. Miller ut[i].family = family; 1272b4ad86bfSDavid S. Miller 1273b4ad86bfSDavid S. Miller switch (ut[i].family) { 1274b4ad86bfSDavid S. Miller case AF_INET: 1275b4ad86bfSDavid S. Miller break; 1276dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1277b4ad86bfSDavid S. Miller case AF_INET6: 1278b4ad86bfSDavid S. Miller break; 1279b4ad86bfSDavid S. Miller #endif 1280b4ad86bfSDavid S. Miller default: 1281b4ad86bfSDavid S. Miller return -EINVAL; 12823ff50b79SStephen Hemminger } 1283b4ad86bfSDavid S. Miller } 1284b4ad86bfSDavid S. Miller 1285b4ad86bfSDavid S. Miller return 0; 1286b4ad86bfSDavid S. Miller } 1287b4ad86bfSDavid S. Miller 12885424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs) 12891da177e4SLinus Torvalds { 12905424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_TMPL]; 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds if (!rt) { 12931da177e4SLinus Torvalds pol->xfrm_nr = 0; 12941da177e4SLinus Torvalds } else { 12955424f32eSThomas Graf struct xfrm_user_tmpl *utmpl = nla_data(rt); 12965424f32eSThomas Graf int nr = nla_len(rt) / sizeof(*utmpl); 1297b4ad86bfSDavid S. Miller int err; 12981da177e4SLinus Torvalds 1299b4ad86bfSDavid S. Miller err = validate_tmpl(nr, utmpl, pol->family); 1300b4ad86bfSDavid S. Miller if (err) 1301b4ad86bfSDavid S. Miller return err; 13021da177e4SLinus Torvalds 13035424f32eSThomas Graf copy_templates(pol, utmpl, nr); 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds return 0; 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 13085424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs) 1309f7b6983fSMasahide NAKAMURA { 13105424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; 1311f7b6983fSMasahide NAKAMURA struct xfrm_userpolicy_type *upt; 1312b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 1313f7b6983fSMasahide NAKAMURA int err; 1314f7b6983fSMasahide NAKAMURA 1315f7b6983fSMasahide NAKAMURA if (rt) { 13165424f32eSThomas Graf upt = nla_data(rt); 1317f7b6983fSMasahide NAKAMURA type = upt->type; 1318f7b6983fSMasahide NAKAMURA } 1319f7b6983fSMasahide NAKAMURA 1320f7b6983fSMasahide NAKAMURA err = verify_policy_type(type); 1321f7b6983fSMasahide NAKAMURA if (err) 1322f7b6983fSMasahide NAKAMURA return err; 1323f7b6983fSMasahide NAKAMURA 1324f7b6983fSMasahide NAKAMURA *tp = type; 1325f7b6983fSMasahide NAKAMURA return 0; 1326f7b6983fSMasahide NAKAMURA } 1327f7b6983fSMasahide NAKAMURA 13281da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) 13291da177e4SLinus Torvalds { 13301da177e4SLinus Torvalds xp->priority = p->priority; 13311da177e4SLinus Torvalds xp->index = p->index; 13321da177e4SLinus Torvalds memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); 13331da177e4SLinus Torvalds memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); 13341da177e4SLinus Torvalds xp->action = p->action; 13351da177e4SLinus Torvalds xp->flags = p->flags; 13361da177e4SLinus Torvalds xp->family = p->sel.family; 13371da177e4SLinus Torvalds /* XXX xp->share = p->share; */ 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) 13411da177e4SLinus Torvalds { 13427b789836SMathias Krause memset(p, 0, sizeof(*p)); 13431da177e4SLinus Torvalds memcpy(&p->sel, &xp->selector, sizeof(p->sel)); 13441da177e4SLinus Torvalds memcpy(&p->lft, &xp->lft, sizeof(p->lft)); 13451da177e4SLinus Torvalds memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); 13461da177e4SLinus Torvalds p->priority = xp->priority; 13471da177e4SLinus Torvalds p->index = xp->index; 13481da177e4SLinus Torvalds p->sel.family = xp->family; 13491da177e4SLinus Torvalds p->dir = dir; 13501da177e4SLinus Torvalds p->action = xp->action; 13511da177e4SLinus Torvalds p->flags = xp->flags; 13521da177e4SLinus Torvalds p->share = XFRM_SHARE_ANY; /* XXX xp->share */ 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 1355fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp) 13561da177e4SLinus Torvalds { 1357fc34acd3SAlexey Dobriyan struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL); 13581da177e4SLinus Torvalds int err; 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds if (!xp) { 13611da177e4SLinus Torvalds *errp = -ENOMEM; 13621da177e4SLinus Torvalds return NULL; 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds copy_from_user_policy(xp, p); 1366df71837dSTrent Jaeger 136735a7aa08SThomas Graf err = copy_from_user_policy_type(&xp->type, attrs); 1368f7b6983fSMasahide NAKAMURA if (err) 1369f7b6983fSMasahide NAKAMURA goto error; 1370f7b6983fSMasahide NAKAMURA 137135a7aa08SThomas Graf if (!(err = copy_from_user_tmpl(xp, attrs))) 137235a7aa08SThomas Graf err = copy_from_user_sec_ctx(xp, attrs); 1373f7b6983fSMasahide NAKAMURA if (err) 1374f7b6983fSMasahide NAKAMURA goto error; 13751da177e4SLinus Torvalds 1376295fae56SJamal Hadi Salim xfrm_mark_get(attrs, &xp->mark); 1377295fae56SJamal Hadi Salim 13781da177e4SLinus Torvalds return xp; 1379f7b6983fSMasahide NAKAMURA error: 1380f7b6983fSMasahide NAKAMURA *errp = err; 138112a169e7SHerbert Xu xp->walk.dead = 1; 138264c31b3fSWANG Cong xfrm_policy_destroy(xp); 1383f7b6983fSMasahide NAKAMURA return NULL; 13841da177e4SLinus Torvalds } 13851da177e4SLinus Torvalds 138622e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 13875424f32eSThomas Graf struct nlattr **attrs) 13881da177e4SLinus Torvalds { 1389fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 13907b67c857SThomas Graf struct xfrm_userpolicy_info *p = nlmsg_data(nlh); 13911da177e4SLinus Torvalds struct xfrm_policy *xp; 139226b15dadSJamal Hadi Salim struct km_event c; 13931da177e4SLinus Torvalds int err; 13941da177e4SLinus Torvalds int excl; 1395c53fa1edSPatrick McHardy uid_t loginuid = audit_get_loginuid(current); 1396c53fa1edSPatrick McHardy u32 sessionid = audit_get_sessionid(current); 1397c53fa1edSPatrick McHardy u32 sid; 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds err = verify_newpolicy_info(p); 14001da177e4SLinus Torvalds if (err) 14011da177e4SLinus Torvalds return err; 140235a7aa08SThomas Graf err = verify_sec_ctx_len(attrs); 1403df71837dSTrent Jaeger if (err) 1404df71837dSTrent Jaeger return err; 14051da177e4SLinus Torvalds 1406fc34acd3SAlexey Dobriyan xp = xfrm_policy_construct(net, p, attrs, &err); 14071da177e4SLinus Torvalds if (!xp) 14081da177e4SLinus Torvalds return err; 14091da177e4SLinus Torvalds 141025985edcSLucas De Marchi /* shouldn't excl be based on nlh flags?? 141126b15dadSJamal Hadi Salim * Aha! this is anti-netlink really i.e more pfkey derived 141226b15dadSJamal Hadi Salim * in netlink excl is a flag and you wouldnt need 141326b15dadSJamal Hadi Salim * a type XFRM_MSG_UPDPOLICY - JHS */ 14141da177e4SLinus Torvalds excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; 14151da177e4SLinus Torvalds err = xfrm_policy_insert(p->dir, xp, excl); 1416c53fa1edSPatrick McHardy security_task_getsecid(current, &sid); 14172532386fSEric Paris xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid); 1418161a09e7SJoy Latten 14191da177e4SLinus Torvalds if (err) { 142003e1ad7bSPaul Moore security_xfrm_policy_free(xp->security); 14211da177e4SLinus Torvalds kfree(xp); 14221da177e4SLinus Torvalds return err; 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds 1425f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 142626b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 142726b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 142826b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 142926b15dadSJamal Hadi Salim 14301da177e4SLinus Torvalds xfrm_pol_put(xp); 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds return 0; 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) 14361da177e4SLinus Torvalds { 14371da177e4SLinus Torvalds struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; 14381da177e4SLinus Torvalds int i; 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds if (xp->xfrm_nr == 0) 14411da177e4SLinus Torvalds return 0; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds for (i = 0; i < xp->xfrm_nr; i++) { 14441da177e4SLinus Torvalds struct xfrm_user_tmpl *up = &vec[i]; 14451da177e4SLinus Torvalds struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; 14461da177e4SLinus Torvalds 14471f86840fSMathias Krause memset(up, 0, sizeof(*up)); 14481da177e4SLinus Torvalds memcpy(&up->id, &kp->id, sizeof(up->id)); 14498511d01dSMiika Komu up->family = kp->encap_family; 14501da177e4SLinus Torvalds memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); 14511da177e4SLinus Torvalds up->reqid = kp->reqid; 14521da177e4SLinus Torvalds up->mode = kp->mode; 14531da177e4SLinus Torvalds up->share = kp->share; 14541da177e4SLinus Torvalds up->optional = kp->optional; 14551da177e4SLinus Torvalds up->aalgos = kp->aalgos; 14561da177e4SLinus Torvalds up->ealgos = kp->ealgos; 14571da177e4SLinus Torvalds up->calgos = kp->calgos; 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds 1460c0144beaSThomas Graf return nla_put(skb, XFRMA_TMPL, 1461c0144beaSThomas Graf sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); 1462df71837dSTrent Jaeger } 1463df71837dSTrent Jaeger 14640d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) 14650d681623SSerge Hallyn { 14660d681623SSerge Hallyn if (x->security) { 14670d681623SSerge Hallyn return copy_sec_ctx(x->security, skb); 14680d681623SSerge Hallyn } 14690d681623SSerge Hallyn return 0; 14700d681623SSerge Hallyn } 14710d681623SSerge Hallyn 14720d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) 14730d681623SSerge Hallyn { 14741d1e34ddSDavid S. Miller if (xp->security) 14750d681623SSerge Hallyn return copy_sec_ctx(xp->security, skb); 14760d681623SSerge Hallyn return 0; 14770d681623SSerge Hallyn } 1478cfbfd45aSThomas Graf static inline size_t userpolicy_type_attrsize(void) 1479cfbfd45aSThomas Graf { 1480cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY 1481cfbfd45aSThomas Graf return nla_total_size(sizeof(struct xfrm_userpolicy_type)); 1482cfbfd45aSThomas Graf #else 1483cfbfd45aSThomas Graf return 0; 1484cfbfd45aSThomas Graf #endif 1485cfbfd45aSThomas Graf } 14860d681623SSerge Hallyn 1487f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 1488b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1489f7b6983fSMasahide NAKAMURA { 1490c0144beaSThomas Graf struct xfrm_userpolicy_type upt = { 1491c0144beaSThomas Graf .type = type, 1492c0144beaSThomas Graf }; 1493f7b6983fSMasahide NAKAMURA 1494c0144beaSThomas Graf return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); 1495f7b6983fSMasahide NAKAMURA } 1496f7b6983fSMasahide NAKAMURA 1497f7b6983fSMasahide NAKAMURA #else 1498b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1499f7b6983fSMasahide NAKAMURA { 1500f7b6983fSMasahide NAKAMURA return 0; 1501f7b6983fSMasahide NAKAMURA } 1502f7b6983fSMasahide NAKAMURA #endif 1503f7b6983fSMasahide NAKAMURA 15041da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) 15051da177e4SLinus Torvalds { 15061da177e4SLinus Torvalds struct xfrm_dump_info *sp = ptr; 15071da177e4SLinus Torvalds struct xfrm_userpolicy_info *p; 15081da177e4SLinus Torvalds struct sk_buff *in_skb = sp->in_skb; 15091da177e4SLinus Torvalds struct sk_buff *skb = sp->out_skb; 15101da177e4SLinus Torvalds struct nlmsghdr *nlh; 15111d1e34ddSDavid S. Miller int err; 15121da177e4SLinus Torvalds 151379b8b7f4SThomas Graf nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, 151479b8b7f4SThomas Graf XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); 151579b8b7f4SThomas Graf if (nlh == NULL) 151679b8b7f4SThomas Graf return -EMSGSIZE; 15171da177e4SLinus Torvalds 15187b67c857SThomas Graf p = nlmsg_data(nlh); 15191da177e4SLinus Torvalds copy_to_user_policy(xp, p, dir); 15201d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 15211d1e34ddSDavid S. Miller if (!err) 15221d1e34ddSDavid S. Miller err = copy_to_user_sec_ctx(xp, skb); 15231d1e34ddSDavid S. Miller if (!err) 15241d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 15251d1e34ddSDavid S. Miller if (!err) 15261d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 15271d1e34ddSDavid S. Miller if (err) { 15281d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 15291d1e34ddSDavid S. Miller return err; 15301d1e34ddSDavid S. Miller } 15319825069dSThomas Graf nlmsg_end(skb, nlh); 15321da177e4SLinus Torvalds return 0; 15331da177e4SLinus Torvalds } 15341da177e4SLinus Torvalds 15354c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb) 15364c563f76STimo Teras { 15374c563f76STimo Teras struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; 15384c563f76STimo Teras 15394c563f76STimo Teras xfrm_policy_walk_done(walk); 15404c563f76STimo Teras return 0; 15414c563f76STimo Teras } 15424c563f76STimo Teras 15431da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) 15441da177e4SLinus Torvalds { 1545fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 15464c563f76STimo Teras struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; 15471da177e4SLinus Torvalds struct xfrm_dump_info info; 15481da177e4SLinus Torvalds 15494c563f76STimo Teras BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) > 15504c563f76STimo Teras sizeof(cb->args) - sizeof(cb->args[0])); 15514c563f76STimo Teras 15521da177e4SLinus Torvalds info.in_skb = cb->skb; 15531da177e4SLinus Torvalds info.out_skb = skb; 15541da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 15551da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 15564c563f76STimo Teras 15574c563f76STimo Teras if (!cb->args[0]) { 15584c563f76STimo Teras cb->args[0] = 1; 15594c563f76STimo Teras xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); 15604c563f76STimo Teras } 15614c563f76STimo Teras 1562fc34acd3SAlexey Dobriyan (void) xfrm_policy_walk(net, walk, dump_one_policy, &info); 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds return skb->len; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, 15681da177e4SLinus Torvalds struct xfrm_policy *xp, 15691da177e4SLinus Torvalds int dir, u32 seq) 15701da177e4SLinus Torvalds { 15711da177e4SLinus Torvalds struct xfrm_dump_info info; 15721da177e4SLinus Torvalds struct sk_buff *skb; 1573c2546372SMathias Krause int err; 15741da177e4SLinus Torvalds 15757deb2264SThomas Graf skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 15761da177e4SLinus Torvalds if (!skb) 15771da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 15781da177e4SLinus Torvalds 15791da177e4SLinus Torvalds info.in_skb = in_skb; 15801da177e4SLinus Torvalds info.out_skb = skb; 15811da177e4SLinus Torvalds info.nlmsg_seq = seq; 15821da177e4SLinus Torvalds info.nlmsg_flags = 0; 15831da177e4SLinus Torvalds 1584c2546372SMathias Krause err = dump_one_policy(xp, dir, 0, &info); 1585c2546372SMathias Krause if (err) { 15861da177e4SLinus Torvalds kfree_skb(skb); 1587c2546372SMathias Krause return ERR_PTR(err); 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds return skb; 15911da177e4SLinus Torvalds } 15921da177e4SLinus Torvalds 159322e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 15945424f32eSThomas Graf struct nlattr **attrs) 15951da177e4SLinus Torvalds { 1596fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 15971da177e4SLinus Torvalds struct xfrm_policy *xp; 15981da177e4SLinus Torvalds struct xfrm_userpolicy_id *p; 1599b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 16001da177e4SLinus Torvalds int err; 160126b15dadSJamal Hadi Salim struct km_event c; 16021da177e4SLinus Torvalds int delete; 1603295fae56SJamal Hadi Salim struct xfrm_mark m; 1604295fae56SJamal Hadi Salim u32 mark = xfrm_mark_get(attrs, &m); 16051da177e4SLinus Torvalds 16067b67c857SThomas Graf p = nlmsg_data(nlh); 16071da177e4SLinus Torvalds delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; 16081da177e4SLinus Torvalds 160935a7aa08SThomas Graf err = copy_from_user_policy_type(&type, attrs); 1610f7b6983fSMasahide NAKAMURA if (err) 1611f7b6983fSMasahide NAKAMURA return err; 1612f7b6983fSMasahide NAKAMURA 16131da177e4SLinus Torvalds err = verify_policy_dir(p->dir); 16141da177e4SLinus Torvalds if (err) 16151da177e4SLinus Torvalds return err; 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds if (p->index) 1618295fae56SJamal Hadi Salim xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err); 1619df71837dSTrent Jaeger else { 16205424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 162103e1ad7bSPaul Moore struct xfrm_sec_ctx *ctx; 1622df71837dSTrent Jaeger 162335a7aa08SThomas Graf err = verify_sec_ctx_len(attrs); 1624df71837dSTrent Jaeger if (err) 1625df71837dSTrent Jaeger return err; 1626df71837dSTrent Jaeger 16272c8dd116SDenis V. Lunev ctx = NULL; 1628df71837dSTrent Jaeger if (rt) { 16295424f32eSThomas Graf struct xfrm_user_sec_ctx *uctx = nla_data(rt); 1630df71837dSTrent Jaeger 163103e1ad7bSPaul Moore err = security_xfrm_policy_alloc(&ctx, uctx); 163203e1ad7bSPaul Moore if (err) 1633df71837dSTrent Jaeger return err; 16342c8dd116SDenis V. Lunev } 1635295fae56SJamal Hadi Salim xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel, 16366f26b61eSJamal Hadi Salim ctx, delete, &err); 163703e1ad7bSPaul Moore security_xfrm_policy_free(ctx); 1638df71837dSTrent Jaeger } 16391da177e4SLinus Torvalds if (xp == NULL) 16401da177e4SLinus Torvalds return -ENOENT; 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds if (!delete) { 16431da177e4SLinus Torvalds struct sk_buff *resp_skb; 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); 16461da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 16471da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 16481da177e4SLinus Torvalds } else { 1649a6483b79SAlexey Dobriyan err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, 1650082a1ad5SThomas Graf NETLINK_CB(skb).pid); 16511da177e4SLinus Torvalds } 165226b15dadSJamal Hadi Salim } else { 1653c53fa1edSPatrick McHardy uid_t loginuid = audit_get_loginuid(current); 1654c53fa1edSPatrick McHardy u32 sessionid = audit_get_sessionid(current); 1655c53fa1edSPatrick McHardy u32 sid; 16562532386fSEric Paris 1657c53fa1edSPatrick McHardy security_task_getsecid(current, &sid); 16582532386fSEric Paris xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid, 16592532386fSEric Paris sid); 166013fcfbb0SDavid S. Miller 166113fcfbb0SDavid S. Miller if (err != 0) 1662c8c05a8eSCatherine Zhang goto out; 166313fcfbb0SDavid S. Miller 1664e7443892SHerbert Xu c.data.byid = p->index; 1665f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 166626b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 166726b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 166826b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds 1671c8c05a8eSCatherine Zhang out: 1672ef41aaa0SEric Paris xfrm_pol_put(xp); 16731da177e4SLinus Torvalds return err; 16741da177e4SLinus Torvalds } 16751da177e4SLinus Torvalds 167622e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 16775424f32eSThomas Graf struct nlattr **attrs) 16781da177e4SLinus Torvalds { 1679fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 168026b15dadSJamal Hadi Salim struct km_event c; 16817b67c857SThomas Graf struct xfrm_usersa_flush *p = nlmsg_data(nlh); 1682161a09e7SJoy Latten struct xfrm_audit audit_info; 16834aa2e62cSJoy Latten int err; 16841da177e4SLinus Torvalds 1685c53fa1edSPatrick McHardy audit_info.loginuid = audit_get_loginuid(current); 1686c53fa1edSPatrick McHardy audit_info.sessionid = audit_get_sessionid(current); 1687c53fa1edSPatrick McHardy security_task_getsecid(current, &audit_info.secid); 1688fc34acd3SAlexey Dobriyan err = xfrm_state_flush(net, p->proto, &audit_info); 16899e64cc95SJamal Hadi Salim if (err) { 16909e64cc95SJamal Hadi Salim if (err == -ESRCH) /* empty table */ 16919e64cc95SJamal Hadi Salim return 0; 1692069c474eSDavid S. Miller return err; 16939e64cc95SJamal Hadi Salim } 1694bf08867fSHerbert Xu c.data.proto = p->proto; 1695f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 169626b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 169726b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 16987067802eSAlexey Dobriyan c.net = net; 169926b15dadSJamal Hadi Salim km_state_notify(NULL, &c); 170026b15dadSJamal Hadi Salim 17011da177e4SLinus Torvalds return 0; 17021da177e4SLinus Torvalds } 17031da177e4SLinus Torvalds 1704d8647b79SSteffen Klassert static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x) 17057deb2264SThomas Graf { 1706d8647b79SSteffen Klassert size_t replay_size = x->replay_esn ? 1707d8647b79SSteffen Klassert xfrm_replay_state_esn_len(x->replay_esn) : 1708d8647b79SSteffen Klassert sizeof(struct xfrm_replay_state); 1709d8647b79SSteffen Klassert 17107deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) 1711d8647b79SSteffen Klassert + nla_total_size(replay_size) 17127deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_lifetime_cur)) 17136f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 17147deb2264SThomas Graf + nla_total_size(4) /* XFRM_AE_RTHR */ 17157deb2264SThomas Graf + nla_total_size(4); /* XFRM_AE_ETHR */ 17167deb2264SThomas Graf } 1717d51d081dSJamal Hadi Salim 1718214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 1719d51d081dSJamal Hadi Salim { 1720d51d081dSJamal Hadi Salim struct xfrm_aevent_id *id; 1721d51d081dSJamal Hadi Salim struct nlmsghdr *nlh; 17221d1e34ddSDavid S. Miller int err; 1723d51d081dSJamal Hadi Salim 172479b8b7f4SThomas Graf nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); 172579b8b7f4SThomas Graf if (nlh == NULL) 172679b8b7f4SThomas Graf return -EMSGSIZE; 1727d51d081dSJamal Hadi Salim 17287b67c857SThomas Graf id = nlmsg_data(nlh); 17292b5f6dccSJamal Hadi Salim memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr)); 1730d51d081dSJamal Hadi Salim id->sa_id.spi = x->id.spi; 1731d51d081dSJamal Hadi Salim id->sa_id.family = x->props.family; 1732d51d081dSJamal Hadi Salim id->sa_id.proto = x->id.proto; 17332b5f6dccSJamal Hadi Salim memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr)); 17342b5f6dccSJamal Hadi Salim id->reqid = x->props.reqid; 1735d51d081dSJamal Hadi Salim id->flags = c->data.aevent; 1736d51d081dSJamal Hadi Salim 1737d0fde795SDavid S. Miller if (x->replay_esn) { 17381d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 1739d8647b79SSteffen Klassert xfrm_replay_state_esn_len(x->replay_esn), 17401d1e34ddSDavid S. Miller x->replay_esn); 1741d0fde795SDavid S. Miller } else { 17421d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 17431d1e34ddSDavid S. Miller &x->replay); 1744d0fde795SDavid S. Miller } 17451d1e34ddSDavid S. Miller if (err) 17461d1e34ddSDavid S. Miller goto out_cancel; 17471d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft); 17481d1e34ddSDavid S. Miller if (err) 17491d1e34ddSDavid S. Miller goto out_cancel; 1750d8647b79SSteffen Klassert 17511d1e34ddSDavid S. Miller if (id->flags & XFRM_AE_RTHR) { 17521d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); 17531d1e34ddSDavid S. Miller if (err) 17541d1e34ddSDavid S. Miller goto out_cancel; 17551d1e34ddSDavid S. Miller } 17561d1e34ddSDavid S. Miller if (id->flags & XFRM_AE_ETHR) { 17571d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_ETIMER_THRESH, 17581d1e34ddSDavid S. Miller x->replay_maxage * 10 / HZ); 17591d1e34ddSDavid S. Miller if (err) 17601d1e34ddSDavid S. Miller goto out_cancel; 17611d1e34ddSDavid S. Miller } 17621d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &x->mark); 17631d1e34ddSDavid S. Miller if (err) 17641d1e34ddSDavid S. Miller goto out_cancel; 17656f26b61eSJamal Hadi Salim 17669825069dSThomas Graf return nlmsg_end(skb, nlh); 1767d51d081dSJamal Hadi Salim 17681d1e34ddSDavid S. Miller out_cancel: 17699825069dSThomas Graf nlmsg_cancel(skb, nlh); 17701d1e34ddSDavid S. Miller return err; 1771d51d081dSJamal Hadi Salim } 1772d51d081dSJamal Hadi Salim 177322e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 17745424f32eSThomas Graf struct nlattr **attrs) 1775d51d081dSJamal Hadi Salim { 1776fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 1777d51d081dSJamal Hadi Salim struct xfrm_state *x; 1778d51d081dSJamal Hadi Salim struct sk_buff *r_skb; 1779d51d081dSJamal Hadi Salim int err; 1780d51d081dSJamal Hadi Salim struct km_event c; 17816f26b61eSJamal Hadi Salim u32 mark; 17826f26b61eSJamal Hadi Salim struct xfrm_mark m; 17837b67c857SThomas Graf struct xfrm_aevent_id *p = nlmsg_data(nlh); 1784d51d081dSJamal Hadi Salim struct xfrm_usersa_id *id = &p->sa_id; 1785d51d081dSJamal Hadi Salim 17866f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 17876f26b61eSJamal Hadi Salim 17886f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family); 1789d8647b79SSteffen Klassert if (x == NULL) 1790d51d081dSJamal Hadi Salim return -ESRCH; 1791d8647b79SSteffen Klassert 1792d8647b79SSteffen Klassert r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 1793d8647b79SSteffen Klassert if (r_skb == NULL) { 1794d8647b79SSteffen Klassert xfrm_state_put(x); 1795d8647b79SSteffen Klassert return -ENOMEM; 1796d51d081dSJamal Hadi Salim } 1797d51d081dSJamal Hadi Salim 1798d51d081dSJamal Hadi Salim /* 1799d51d081dSJamal Hadi Salim * XXX: is this lock really needed - none of the other 1800d51d081dSJamal Hadi Salim * gets lock (the concern is things getting updated 1801d51d081dSJamal Hadi Salim * while we are still reading) - jhs 1802d51d081dSJamal Hadi Salim */ 1803d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 1804d51d081dSJamal Hadi Salim c.data.aevent = p->flags; 1805d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 1806d51d081dSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 1807d51d081dSJamal Hadi Salim 1808d51d081dSJamal Hadi Salim if (build_aevent(r_skb, x, &c) < 0) 1809d51d081dSJamal Hadi Salim BUG(); 1810a6483b79SAlexey Dobriyan err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid); 1811d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 1812d51d081dSJamal Hadi Salim xfrm_state_put(x); 1813d51d081dSJamal Hadi Salim return err; 1814d51d081dSJamal Hadi Salim } 1815d51d081dSJamal Hadi Salim 181622e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 18175424f32eSThomas Graf struct nlattr **attrs) 1818d51d081dSJamal Hadi Salim { 1819fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 1820d51d081dSJamal Hadi Salim struct xfrm_state *x; 1821d51d081dSJamal Hadi Salim struct km_event c; 1822d51d081dSJamal Hadi Salim int err = - EINVAL; 18236f26b61eSJamal Hadi Salim u32 mark = 0; 18246f26b61eSJamal Hadi Salim struct xfrm_mark m; 18257b67c857SThomas Graf struct xfrm_aevent_id *p = nlmsg_data(nlh); 18265424f32eSThomas Graf struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 1827d8647b79SSteffen Klassert struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL]; 18285424f32eSThomas Graf struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 1829d51d081dSJamal Hadi Salim 1830d8647b79SSteffen Klassert if (!lt && !rp && !re) 1831d51d081dSJamal Hadi Salim return err; 1832d51d081dSJamal Hadi Salim 1833d51d081dSJamal Hadi Salim /* pedantic mode - thou shalt sayeth replaceth */ 1834d51d081dSJamal Hadi Salim if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) 1835d51d081dSJamal Hadi Salim return err; 1836d51d081dSJamal Hadi Salim 18376f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 18386f26b61eSJamal Hadi Salim 18396f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); 1840d51d081dSJamal Hadi Salim if (x == NULL) 1841d51d081dSJamal Hadi Salim return -ESRCH; 1842d51d081dSJamal Hadi Salim 1843d51d081dSJamal Hadi Salim if (x->km.state != XFRM_STATE_VALID) 1844d51d081dSJamal Hadi Salim goto out; 1845d51d081dSJamal Hadi Salim 1846e2b19125SSteffen Klassert err = xfrm_replay_verify_len(x->replay_esn, rp); 1847e2b19125SSteffen Klassert if (err) 1848e2b19125SSteffen Klassert goto out; 1849e2b19125SSteffen Klassert 1850d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 185135a7aa08SThomas Graf xfrm_update_ae_params(x, attrs); 1852d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 1853d51d081dSJamal Hadi Salim 1854d51d081dSJamal Hadi Salim c.event = nlh->nlmsg_type; 1855d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 1856d51d081dSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 1857d51d081dSJamal Hadi Salim c.data.aevent = XFRM_AE_CU; 1858d51d081dSJamal Hadi Salim km_state_notify(x, &c); 1859d51d081dSJamal Hadi Salim err = 0; 1860d51d081dSJamal Hadi Salim out: 1861d51d081dSJamal Hadi Salim xfrm_state_put(x); 1862d51d081dSJamal Hadi Salim return err; 1863d51d081dSJamal Hadi Salim } 1864d51d081dSJamal Hadi Salim 186522e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 18665424f32eSThomas Graf struct nlattr **attrs) 18671da177e4SLinus Torvalds { 1868fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 186926b15dadSJamal Hadi Salim struct km_event c; 1870b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 1871f7b6983fSMasahide NAKAMURA int err; 1872161a09e7SJoy Latten struct xfrm_audit audit_info; 187326b15dadSJamal Hadi Salim 187435a7aa08SThomas Graf err = copy_from_user_policy_type(&type, attrs); 1875f7b6983fSMasahide NAKAMURA if (err) 1876f7b6983fSMasahide NAKAMURA return err; 1877f7b6983fSMasahide NAKAMURA 1878c53fa1edSPatrick McHardy audit_info.loginuid = audit_get_loginuid(current); 1879c53fa1edSPatrick McHardy audit_info.sessionid = audit_get_sessionid(current); 1880c53fa1edSPatrick McHardy security_task_getsecid(current, &audit_info.secid); 1881fc34acd3SAlexey Dobriyan err = xfrm_policy_flush(net, type, &audit_info); 18822f1eb65fSJamal Hadi Salim if (err) { 18832f1eb65fSJamal Hadi Salim if (err == -ESRCH) /* empty table */ 18842f1eb65fSJamal Hadi Salim return 0; 1885069c474eSDavid S. Miller return err; 18862f1eb65fSJamal Hadi Salim } 18872f1eb65fSJamal Hadi Salim 1888f7b6983fSMasahide NAKAMURA c.data.type = type; 1889f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 189026b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 189126b15dadSJamal Hadi Salim c.pid = nlh->nlmsg_pid; 18927067802eSAlexey Dobriyan c.net = net; 189326b15dadSJamal Hadi Salim km_policy_notify(NULL, 0, &c); 18941da177e4SLinus Torvalds return 0; 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 189722e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 18985424f32eSThomas Graf struct nlattr **attrs) 18996c5c8ca7SJamal Hadi Salim { 1900fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 19016c5c8ca7SJamal Hadi Salim struct xfrm_policy *xp; 19027b67c857SThomas Graf struct xfrm_user_polexpire *up = nlmsg_data(nlh); 19036c5c8ca7SJamal Hadi Salim struct xfrm_userpolicy_info *p = &up->pol; 1904b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 19056c5c8ca7SJamal Hadi Salim int err = -ENOENT; 1906295fae56SJamal Hadi Salim struct xfrm_mark m; 1907295fae56SJamal Hadi Salim u32 mark = xfrm_mark_get(attrs, &m); 19086c5c8ca7SJamal Hadi Salim 190935a7aa08SThomas Graf err = copy_from_user_policy_type(&type, attrs); 1910f7b6983fSMasahide NAKAMURA if (err) 1911f7b6983fSMasahide NAKAMURA return err; 1912f7b6983fSMasahide NAKAMURA 1913c8bf4d04STimo Teräs err = verify_policy_dir(p->dir); 1914c8bf4d04STimo Teräs if (err) 1915c8bf4d04STimo Teräs return err; 1916c8bf4d04STimo Teräs 19176c5c8ca7SJamal Hadi Salim if (p->index) 1918295fae56SJamal Hadi Salim xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err); 19196c5c8ca7SJamal Hadi Salim else { 19205424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 192103e1ad7bSPaul Moore struct xfrm_sec_ctx *ctx; 19226c5c8ca7SJamal Hadi Salim 192335a7aa08SThomas Graf err = verify_sec_ctx_len(attrs); 19246c5c8ca7SJamal Hadi Salim if (err) 19256c5c8ca7SJamal Hadi Salim return err; 19266c5c8ca7SJamal Hadi Salim 19272c8dd116SDenis V. Lunev ctx = NULL; 19286c5c8ca7SJamal Hadi Salim if (rt) { 19295424f32eSThomas Graf struct xfrm_user_sec_ctx *uctx = nla_data(rt); 19306c5c8ca7SJamal Hadi Salim 193103e1ad7bSPaul Moore err = security_xfrm_policy_alloc(&ctx, uctx); 193203e1ad7bSPaul Moore if (err) 19336c5c8ca7SJamal Hadi Salim return err; 19342c8dd116SDenis V. Lunev } 1935295fae56SJamal Hadi Salim xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, 19366f26b61eSJamal Hadi Salim &p->sel, ctx, 0, &err); 193703e1ad7bSPaul Moore security_xfrm_policy_free(ctx); 19386c5c8ca7SJamal Hadi Salim } 19396c5c8ca7SJamal Hadi Salim if (xp == NULL) 1940ef41aaa0SEric Paris return -ENOENT; 194103e1ad7bSPaul Moore 1942ea2dea9dSTimo Teräs if (unlikely(xp->walk.dead)) 19436c5c8ca7SJamal Hadi Salim goto out; 19446c5c8ca7SJamal Hadi Salim 19456c5c8ca7SJamal Hadi Salim err = 0; 19466c5c8ca7SJamal Hadi Salim if (up->hard) { 1947c53fa1edSPatrick McHardy uid_t loginuid = audit_get_loginuid(current); 1948c53fa1edSPatrick McHardy u32 sessionid = audit_get_sessionid(current); 1949c53fa1edSPatrick McHardy u32 sid; 1950c53fa1edSPatrick McHardy 1951c53fa1edSPatrick McHardy security_task_getsecid(current, &sid); 19526c5c8ca7SJamal Hadi Salim xfrm_policy_delete(xp, p->dir); 19532532386fSEric Paris xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid); 1954161a09e7SJoy Latten 19556c5c8ca7SJamal Hadi Salim } else { 19566c5c8ca7SJamal Hadi Salim // reset the timers here? 195762db5cfdSstephen hemminger WARN(1, "Dont know what to do with soft policy expire\n"); 19586c5c8ca7SJamal Hadi Salim } 19596c5c8ca7SJamal Hadi Salim km_policy_expired(xp, p->dir, up->hard, current->pid); 19606c5c8ca7SJamal Hadi Salim 19616c5c8ca7SJamal Hadi Salim out: 19626c5c8ca7SJamal Hadi Salim xfrm_pol_put(xp); 19636c5c8ca7SJamal Hadi Salim return err; 19646c5c8ca7SJamal Hadi Salim } 19656c5c8ca7SJamal Hadi Salim 196622e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 19675424f32eSThomas Graf struct nlattr **attrs) 196853bc6b4dSJamal Hadi Salim { 1969fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 197053bc6b4dSJamal Hadi Salim struct xfrm_state *x; 197153bc6b4dSJamal Hadi Salim int err; 19727b67c857SThomas Graf struct xfrm_user_expire *ue = nlmsg_data(nlh); 197353bc6b4dSJamal Hadi Salim struct xfrm_usersa_info *p = &ue->state; 19746f26b61eSJamal Hadi Salim struct xfrm_mark m; 1975928497f0SNicolas Dichtel u32 mark = xfrm_mark_get(attrs, &m); 197653bc6b4dSJamal Hadi Salim 19776f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family); 197853bc6b4dSJamal Hadi Salim 19793a765aa5SDavid S. Miller err = -ENOENT; 198053bc6b4dSJamal Hadi Salim if (x == NULL) 198153bc6b4dSJamal Hadi Salim return err; 198253bc6b4dSJamal Hadi Salim 198353bc6b4dSJamal Hadi Salim spin_lock_bh(&x->lock); 19843a765aa5SDavid S. Miller err = -EINVAL; 198553bc6b4dSJamal Hadi Salim if (x->km.state != XFRM_STATE_VALID) 198653bc6b4dSJamal Hadi Salim goto out; 198753bc6b4dSJamal Hadi Salim km_state_expired(x, ue->hard, current->pid); 198853bc6b4dSJamal Hadi Salim 1989161a09e7SJoy Latten if (ue->hard) { 1990c53fa1edSPatrick McHardy uid_t loginuid = audit_get_loginuid(current); 1991c53fa1edSPatrick McHardy u32 sessionid = audit_get_sessionid(current); 1992c53fa1edSPatrick McHardy u32 sid; 1993c53fa1edSPatrick McHardy 1994c53fa1edSPatrick McHardy security_task_getsecid(current, &sid); 199553bc6b4dSJamal Hadi Salim __xfrm_state_delete(x); 19962532386fSEric Paris xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid); 1997161a09e7SJoy Latten } 19983a765aa5SDavid S. Miller err = 0; 199953bc6b4dSJamal Hadi Salim out: 200053bc6b4dSJamal Hadi Salim spin_unlock_bh(&x->lock); 200153bc6b4dSJamal Hadi Salim xfrm_state_put(x); 200253bc6b4dSJamal Hadi Salim return err; 200353bc6b4dSJamal Hadi Salim } 200453bc6b4dSJamal Hadi Salim 200522e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, 20065424f32eSThomas Graf struct nlattr **attrs) 2007980ebd25SJamal Hadi Salim { 2008fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 2009980ebd25SJamal Hadi Salim struct xfrm_policy *xp; 2010980ebd25SJamal Hadi Salim struct xfrm_user_tmpl *ut; 2011980ebd25SJamal Hadi Salim int i; 20125424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_TMPL]; 20136f26b61eSJamal Hadi Salim struct xfrm_mark mark; 2014980ebd25SJamal Hadi Salim 20157b67c857SThomas Graf struct xfrm_user_acquire *ua = nlmsg_data(nlh); 2016fc34acd3SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 2017980ebd25SJamal Hadi Salim int err = -ENOMEM; 2018980ebd25SJamal Hadi Salim 2019980ebd25SJamal Hadi Salim if (!x) 2020d8eb9307SIlpo Järvinen goto nomem; 2021980ebd25SJamal Hadi Salim 20226f26b61eSJamal Hadi Salim xfrm_mark_get(attrs, &mark); 20236f26b61eSJamal Hadi Salim 2024980ebd25SJamal Hadi Salim err = verify_newpolicy_info(&ua->policy); 2025d8eb9307SIlpo Järvinen if (err) 2026d8eb9307SIlpo Järvinen goto bad_policy; 2027980ebd25SJamal Hadi Salim 2028980ebd25SJamal Hadi Salim /* build an XP */ 2029fc34acd3SAlexey Dobriyan xp = xfrm_policy_construct(net, &ua->policy, attrs, &err); 2030d8eb9307SIlpo Järvinen if (!xp) 2031d8eb9307SIlpo Järvinen goto free_state; 2032980ebd25SJamal Hadi Salim 2033980ebd25SJamal Hadi Salim memcpy(&x->id, &ua->id, sizeof(ua->id)); 2034980ebd25SJamal Hadi Salim memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); 2035980ebd25SJamal Hadi Salim memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); 20366f26b61eSJamal Hadi Salim xp->mark.m = x->mark.m = mark.m; 20376f26b61eSJamal Hadi Salim xp->mark.v = x->mark.v = mark.v; 20385424f32eSThomas Graf ut = nla_data(rt); 2039980ebd25SJamal Hadi Salim /* extract the templates and for each call km_key */ 2040980ebd25SJamal Hadi Salim for (i = 0; i < xp->xfrm_nr; i++, ut++) { 2041980ebd25SJamal Hadi Salim struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 2042980ebd25SJamal Hadi Salim memcpy(&x->id, &t->id, sizeof(x->id)); 2043980ebd25SJamal Hadi Salim x->props.mode = t->mode; 2044980ebd25SJamal Hadi Salim x->props.reqid = t->reqid; 2045980ebd25SJamal Hadi Salim x->props.family = ut->family; 2046980ebd25SJamal Hadi Salim t->aalgos = ua->aalgos; 2047980ebd25SJamal Hadi Salim t->ealgos = ua->ealgos; 2048980ebd25SJamal Hadi Salim t->calgos = ua->calgos; 2049980ebd25SJamal Hadi Salim err = km_query(x, t, xp); 2050980ebd25SJamal Hadi Salim 2051980ebd25SJamal Hadi Salim } 2052980ebd25SJamal Hadi Salim 2053980ebd25SJamal Hadi Salim kfree(x); 2054980ebd25SJamal Hadi Salim kfree(xp); 2055980ebd25SJamal Hadi Salim 2056980ebd25SJamal Hadi Salim return 0; 2057d8eb9307SIlpo Järvinen 2058d8eb9307SIlpo Järvinen bad_policy: 205962db5cfdSstephen hemminger WARN(1, "BAD policy passed\n"); 2060d8eb9307SIlpo Järvinen free_state: 2061d8eb9307SIlpo Järvinen kfree(x); 2062d8eb9307SIlpo Järvinen nomem: 2063d8eb9307SIlpo Järvinen return err; 2064980ebd25SJamal Hadi Salim } 2065980ebd25SJamal Hadi Salim 20665c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 20675c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma, 206813c1d189SArnaud Ebalard struct xfrm_kmaddress *k, 20695424f32eSThomas Graf struct nlattr **attrs, int *num) 20705c79de6eSShinta Sugimoto { 20715424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_MIGRATE]; 20725c79de6eSShinta Sugimoto struct xfrm_user_migrate *um; 20735c79de6eSShinta Sugimoto int i, num_migrate; 20745c79de6eSShinta Sugimoto 207513c1d189SArnaud Ebalard if (k != NULL) { 207613c1d189SArnaud Ebalard struct xfrm_user_kmaddress *uk; 207713c1d189SArnaud Ebalard 207813c1d189SArnaud Ebalard uk = nla_data(attrs[XFRMA_KMADDRESS]); 207913c1d189SArnaud Ebalard memcpy(&k->local, &uk->local, sizeof(k->local)); 208013c1d189SArnaud Ebalard memcpy(&k->remote, &uk->remote, sizeof(k->remote)); 208113c1d189SArnaud Ebalard k->family = uk->family; 208213c1d189SArnaud Ebalard k->reserved = uk->reserved; 208313c1d189SArnaud Ebalard } 208413c1d189SArnaud Ebalard 20855424f32eSThomas Graf um = nla_data(rt); 20865424f32eSThomas Graf num_migrate = nla_len(rt) / sizeof(*um); 20875c79de6eSShinta Sugimoto 20885c79de6eSShinta Sugimoto if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) 20895c79de6eSShinta Sugimoto return -EINVAL; 20905c79de6eSShinta Sugimoto 20915c79de6eSShinta Sugimoto for (i = 0; i < num_migrate; i++, um++, ma++) { 20925c79de6eSShinta Sugimoto memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); 20935c79de6eSShinta Sugimoto memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); 20945c79de6eSShinta Sugimoto memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); 20955c79de6eSShinta Sugimoto memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); 20965c79de6eSShinta Sugimoto 20975c79de6eSShinta Sugimoto ma->proto = um->proto; 20985c79de6eSShinta Sugimoto ma->mode = um->mode; 20995c79de6eSShinta Sugimoto ma->reqid = um->reqid; 21005c79de6eSShinta Sugimoto 21015c79de6eSShinta Sugimoto ma->old_family = um->old_family; 21025c79de6eSShinta Sugimoto ma->new_family = um->new_family; 21035c79de6eSShinta Sugimoto } 21045c79de6eSShinta Sugimoto 21055c79de6eSShinta Sugimoto *num = i; 21065c79de6eSShinta Sugimoto return 0; 21075c79de6eSShinta Sugimoto } 21085c79de6eSShinta Sugimoto 21095c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 21105424f32eSThomas Graf struct nlattr **attrs) 21115c79de6eSShinta Sugimoto { 21127b67c857SThomas Graf struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); 21135c79de6eSShinta Sugimoto struct xfrm_migrate m[XFRM_MAX_DEPTH]; 211413c1d189SArnaud Ebalard struct xfrm_kmaddress km, *kmp; 21155c79de6eSShinta Sugimoto u8 type; 21165c79de6eSShinta Sugimoto int err; 21175c79de6eSShinta Sugimoto int n = 0; 21185c79de6eSShinta Sugimoto 211935a7aa08SThomas Graf if (attrs[XFRMA_MIGRATE] == NULL) 2120cf5cb79fSThomas Graf return -EINVAL; 21215c79de6eSShinta Sugimoto 212213c1d189SArnaud Ebalard kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL; 212313c1d189SArnaud Ebalard 21245424f32eSThomas Graf err = copy_from_user_policy_type(&type, attrs); 21255c79de6eSShinta Sugimoto if (err) 21265c79de6eSShinta Sugimoto return err; 21275c79de6eSShinta Sugimoto 212813c1d189SArnaud Ebalard err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n); 21295c79de6eSShinta Sugimoto if (err) 21305c79de6eSShinta Sugimoto return err; 21315c79de6eSShinta Sugimoto 21325c79de6eSShinta Sugimoto if (!n) 21335c79de6eSShinta Sugimoto return 0; 21345c79de6eSShinta Sugimoto 213513c1d189SArnaud Ebalard xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp); 21365c79de6eSShinta Sugimoto 21375c79de6eSShinta Sugimoto return 0; 21385c79de6eSShinta Sugimoto } 21395c79de6eSShinta Sugimoto #else 21405c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 21415424f32eSThomas Graf struct nlattr **attrs) 21425c79de6eSShinta Sugimoto { 21435c79de6eSShinta Sugimoto return -ENOPROTOOPT; 21445c79de6eSShinta Sugimoto } 21455c79de6eSShinta Sugimoto #endif 21465c79de6eSShinta Sugimoto 21475c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 2148183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb) 21495c79de6eSShinta Sugimoto { 21505c79de6eSShinta Sugimoto struct xfrm_user_migrate um; 21515c79de6eSShinta Sugimoto 21525c79de6eSShinta Sugimoto memset(&um, 0, sizeof(um)); 21535c79de6eSShinta Sugimoto um.proto = m->proto; 21545c79de6eSShinta Sugimoto um.mode = m->mode; 21555c79de6eSShinta Sugimoto um.reqid = m->reqid; 21565c79de6eSShinta Sugimoto um.old_family = m->old_family; 21575c79de6eSShinta Sugimoto memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); 21585c79de6eSShinta Sugimoto memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); 21595c79de6eSShinta Sugimoto um.new_family = m->new_family; 21605c79de6eSShinta Sugimoto memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); 21615c79de6eSShinta Sugimoto memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); 21625c79de6eSShinta Sugimoto 2163c0144beaSThomas Graf return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); 21645c79de6eSShinta Sugimoto } 21655c79de6eSShinta Sugimoto 2166183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb) 216713c1d189SArnaud Ebalard { 216813c1d189SArnaud Ebalard struct xfrm_user_kmaddress uk; 216913c1d189SArnaud Ebalard 217013c1d189SArnaud Ebalard memset(&uk, 0, sizeof(uk)); 217113c1d189SArnaud Ebalard uk.family = k->family; 217213c1d189SArnaud Ebalard uk.reserved = k->reserved; 217313c1d189SArnaud Ebalard memcpy(&uk.local, &k->local, sizeof(uk.local)); 2174a1caa322SArnaud Ebalard memcpy(&uk.remote, &k->remote, sizeof(uk.remote)); 217513c1d189SArnaud Ebalard 217613c1d189SArnaud Ebalard return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk); 217713c1d189SArnaud Ebalard } 217813c1d189SArnaud Ebalard 217913c1d189SArnaud Ebalard static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma) 21807deb2264SThomas Graf { 21817deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) 218213c1d189SArnaud Ebalard + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0) 21837deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) 21847deb2264SThomas Graf + userpolicy_type_attrsize(); 21857deb2264SThomas Graf } 21867deb2264SThomas Graf 2187183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m, 2188183cad12SDavid S. Miller int num_migrate, const struct xfrm_kmaddress *k, 2189183cad12SDavid S. Miller const struct xfrm_selector *sel, u8 dir, u8 type) 21905c79de6eSShinta Sugimoto { 2191183cad12SDavid S. Miller const struct xfrm_migrate *mp; 21925c79de6eSShinta Sugimoto struct xfrm_userpolicy_id *pol_id; 21935c79de6eSShinta Sugimoto struct nlmsghdr *nlh; 21941d1e34ddSDavid S. Miller int i, err; 21955c79de6eSShinta Sugimoto 219679b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); 219779b8b7f4SThomas Graf if (nlh == NULL) 219879b8b7f4SThomas Graf return -EMSGSIZE; 21995c79de6eSShinta Sugimoto 22007b67c857SThomas Graf pol_id = nlmsg_data(nlh); 22015c79de6eSShinta Sugimoto /* copy data from selector, dir, and type to the pol_id */ 22025c79de6eSShinta Sugimoto memset(pol_id, 0, sizeof(*pol_id)); 22035c79de6eSShinta Sugimoto memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); 22045c79de6eSShinta Sugimoto pol_id->dir = dir; 22055c79de6eSShinta Sugimoto 22061d1e34ddSDavid S. Miller if (k != NULL) { 22071d1e34ddSDavid S. Miller err = copy_to_user_kmaddress(k, skb); 22081d1e34ddSDavid S. Miller if (err) 22091d1e34ddSDavid S. Miller goto out_cancel; 22101d1e34ddSDavid S. Miller } 22111d1e34ddSDavid S. Miller err = copy_to_user_policy_type(type, skb); 22121d1e34ddSDavid S. Miller if (err) 22131d1e34ddSDavid S. Miller goto out_cancel; 22145c79de6eSShinta Sugimoto for (i = 0, mp = m ; i < num_migrate; i++, mp++) { 22151d1e34ddSDavid S. Miller err = copy_to_user_migrate(mp, skb); 22161d1e34ddSDavid S. Miller if (err) 22171d1e34ddSDavid S. Miller goto out_cancel; 22185c79de6eSShinta Sugimoto } 22195c79de6eSShinta Sugimoto 22209825069dSThomas Graf return nlmsg_end(skb, nlh); 22211d1e34ddSDavid S. Miller 22221d1e34ddSDavid S. Miller out_cancel: 22239825069dSThomas Graf nlmsg_cancel(skb, nlh); 22241d1e34ddSDavid S. Miller return err; 22255c79de6eSShinta Sugimoto } 22265c79de6eSShinta Sugimoto 2227183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2228183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 2229183cad12SDavid S. Miller const struct xfrm_kmaddress *k) 22305c79de6eSShinta Sugimoto { 2231a6483b79SAlexey Dobriyan struct net *net = &init_net; 22325c79de6eSShinta Sugimoto struct sk_buff *skb; 22335c79de6eSShinta Sugimoto 223413c1d189SArnaud Ebalard skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC); 22355c79de6eSShinta Sugimoto if (skb == NULL) 22365c79de6eSShinta Sugimoto return -ENOMEM; 22375c79de6eSShinta Sugimoto 22385c79de6eSShinta Sugimoto /* build migrate */ 223913c1d189SArnaud Ebalard if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0) 22405c79de6eSShinta Sugimoto BUG(); 22415c79de6eSShinta Sugimoto 2242a6483b79SAlexey Dobriyan return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC); 22435c79de6eSShinta Sugimoto } 22445c79de6eSShinta Sugimoto #else 2245183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2246183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 2247183cad12SDavid S. Miller const struct xfrm_kmaddress *k) 22485c79de6eSShinta Sugimoto { 22495c79de6eSShinta Sugimoto return -ENOPROTOOPT; 22505c79de6eSShinta Sugimoto } 22515c79de6eSShinta Sugimoto #endif 2252d51d081dSJamal Hadi Salim 2253a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type) 2254492b558bSThomas Graf 2255492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { 225666f9a259SDavid S. Miller [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2257492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2258492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2259492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2260492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2261492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2262492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), 2263980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), 226453bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), 2265492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 226666f9a259SDavid S. Miller [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 22676c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), 2268492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), 2269a7bd9a45SThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, 2270d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2271d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 227297a64b45SMasahide NAKAMURA [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), 22735c79de6eSShinta Sugimoto [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2274a7bd9a45SThomas Graf [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), 2275a7bd9a45SThomas Graf [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 22761da177e4SLinus Torvalds }; 22771da177e4SLinus Torvalds 2278492b558bSThomas Graf #undef XMSGSIZE 2279492b558bSThomas Graf 2280cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { 2281c28e9304Sjamal [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)}, 2282c28e9304Sjamal [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)}, 2283c28e9304Sjamal [XFRMA_LASTUSED] = { .type = NLA_U64}, 2284c28e9304Sjamal [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)}, 22851a6509d9SHerbert Xu [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, 2286cf5cb79fSThomas Graf [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, 2287cf5cb79fSThomas Graf [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, 2288cf5cb79fSThomas Graf [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, 2289cf5cb79fSThomas Graf [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, 2290cf5cb79fSThomas Graf [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, 2291cf5cb79fSThomas Graf [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, 2292cf5cb79fSThomas Graf [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, 2293cf5cb79fSThomas Graf [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, 2294cf5cb79fSThomas Graf [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, 2295cf5cb79fSThomas Graf [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, 2296cf5cb79fSThomas Graf [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, 2297cf5cb79fSThomas Graf [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, 2298cf5cb79fSThomas Graf [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, 2299cf5cb79fSThomas Graf [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, 230013c1d189SArnaud Ebalard [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) }, 23016f26b61eSJamal Hadi Salim [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) }, 230235d2856bSMartin Willi [XFRMA_TFCPAD] = { .type = NLA_U32 }, 2303d8647b79SSteffen Klassert [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) }, 2304cf5cb79fSThomas Graf }; 2305cf5cb79fSThomas Graf 23061da177e4SLinus Torvalds static struct xfrm_link { 23075424f32eSThomas Graf int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **); 23081da177e4SLinus Torvalds int (*dump)(struct sk_buff *, struct netlink_callback *); 23094c563f76STimo Teras int (*done)(struct netlink_callback *); 2310492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = { 2311492b558bSThomas Graf [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 2312492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, 2313492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, 23144c563f76STimo Teras .dump = xfrm_dump_sa, 23154c563f76STimo Teras .done = xfrm_dump_sa_done }, 2316492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2317492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, 2318492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, 23194c563f76STimo Teras .dump = xfrm_dump_policy, 23204c563f76STimo Teras .done = xfrm_dump_policy_done }, 2321492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, 2322980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, 232353bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, 2324492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2325492b558bSThomas Graf [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 23266c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, 2327492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, 2328492b558bSThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, 2329d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, 2330d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, 23315c79de6eSShinta Sugimoto [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, 233228d8909bSJamal Hadi Salim [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, 2333ecfd6b18SJamal Hadi Salim [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, 23341da177e4SLinus Torvalds }; 23351da177e4SLinus Torvalds 23361d00a4ebSThomas Graf static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 23371da177e4SLinus Torvalds { 2338a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 233935a7aa08SThomas Graf struct nlattr *attrs[XFRMA_MAX+1]; 23401da177e4SLinus Torvalds struct xfrm_link *link; 2341a7bd9a45SThomas Graf int type, err; 23421da177e4SLinus Torvalds 23431da177e4SLinus Torvalds type = nlh->nlmsg_type; 23441da177e4SLinus Torvalds if (type > XFRM_MSG_MAX) 23451d00a4ebSThomas Graf return -EINVAL; 23461da177e4SLinus Torvalds 23471da177e4SLinus Torvalds type -= XFRM_MSG_BASE; 23481da177e4SLinus Torvalds link = &xfrm_dispatch[type]; 23491da177e4SLinus Torvalds 23501da177e4SLinus Torvalds /* All operations require privileges, even GET */ 2351fd778461SEric Paris if (!capable(CAP_NET_ADMIN)) 23521d00a4ebSThomas Graf return -EPERM; 23531da177e4SLinus Torvalds 2354492b558bSThomas Graf if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || 2355492b558bSThomas Graf type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && 2356b8f3ab42SDavid S. Miller (nlh->nlmsg_flags & NLM_F_DUMP)) { 23571da177e4SLinus Torvalds if (link->dump == NULL) 23581d00a4ebSThomas Graf return -EINVAL; 23591da177e4SLinus Torvalds 236080d326faSPablo Neira Ayuso { 236180d326faSPablo Neira Ayuso struct netlink_dump_control c = { 236280d326faSPablo Neira Ayuso .dump = link->dump, 236380d326faSPablo Neira Ayuso .done = link->done, 236480d326faSPablo Neira Ayuso }; 236580d326faSPablo Neira Ayuso return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c); 236680d326faSPablo Neira Ayuso } 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds 236935a7aa08SThomas Graf err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX, 2370cf5cb79fSThomas Graf xfrma_policy); 2371a7bd9a45SThomas Graf if (err < 0) 2372a7bd9a45SThomas Graf return err; 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds if (link->doit == NULL) 23751d00a4ebSThomas Graf return -EINVAL; 23761da177e4SLinus Torvalds 23775424f32eSThomas Graf return link->doit(skb, nlh, attrs); 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds 2380cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb) 23811da177e4SLinus Torvalds { 23824a3e2f71SArjan van de Ven mutex_lock(&xfrm_cfg_mutex); 2383cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &xfrm_user_rcv_msg); 23844a3e2f71SArjan van de Ven mutex_unlock(&xfrm_cfg_mutex); 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds 23877deb2264SThomas Graf static inline size_t xfrm_expire_msgsize(void) 23887deb2264SThomas Graf { 23896f26b61eSJamal Hadi Salim return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) 23906f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)); 23917deb2264SThomas Graf } 23927deb2264SThomas Graf 2393214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 23941da177e4SLinus Torvalds { 23951da177e4SLinus Torvalds struct xfrm_user_expire *ue; 23961da177e4SLinus Torvalds struct nlmsghdr *nlh; 23971d1e34ddSDavid S. Miller int err; 23981da177e4SLinus Torvalds 239979b8b7f4SThomas Graf nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); 240079b8b7f4SThomas Graf if (nlh == NULL) 240179b8b7f4SThomas Graf return -EMSGSIZE; 24021da177e4SLinus Torvalds 24037b67c857SThomas Graf ue = nlmsg_data(nlh); 24041da177e4SLinus Torvalds copy_to_user_state(x, &ue->state); 2405d51d081dSJamal Hadi Salim ue->hard = (c->data.hard != 0) ? 1 : 0; 24061da177e4SLinus Torvalds 24071d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &x->mark); 24081d1e34ddSDavid S. Miller if (err) 24091d1e34ddSDavid S. Miller return err; 24106f26b61eSJamal Hadi Salim 24119825069dSThomas Graf return nlmsg_end(skb, nlh); 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 2414214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c) 24151da177e4SLinus Torvalds { 2416fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 24171da177e4SLinus Torvalds struct sk_buff *skb; 24181da177e4SLinus Torvalds 24197deb2264SThomas Graf skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); 24201da177e4SLinus Torvalds if (skb == NULL) 24211da177e4SLinus Torvalds return -ENOMEM; 24221da177e4SLinus Torvalds 24236f26b61eSJamal Hadi Salim if (build_expire(skb, x, c) < 0) { 24246f26b61eSJamal Hadi Salim kfree_skb(skb); 24256f26b61eSJamal Hadi Salim return -EMSGSIZE; 24266f26b61eSJamal Hadi Salim } 24271da177e4SLinus Torvalds 2428a6483b79SAlexey Dobriyan return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); 24291da177e4SLinus Torvalds } 24301da177e4SLinus Torvalds 2431214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c) 2432d51d081dSJamal Hadi Salim { 2433fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 2434d51d081dSJamal Hadi Salim struct sk_buff *skb; 2435d51d081dSJamal Hadi Salim 2436d8647b79SSteffen Klassert skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 2437d51d081dSJamal Hadi Salim if (skb == NULL) 2438d51d081dSJamal Hadi Salim return -ENOMEM; 2439d51d081dSJamal Hadi Salim 2440d51d081dSJamal Hadi Salim if (build_aevent(skb, x, c) < 0) 2441d51d081dSJamal Hadi Salim BUG(); 2442d51d081dSJamal Hadi Salim 2443a6483b79SAlexey Dobriyan return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC); 2444d51d081dSJamal Hadi Salim } 2445d51d081dSJamal Hadi Salim 2446214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c) 244726b15dadSJamal Hadi Salim { 24487067802eSAlexey Dobriyan struct net *net = c->net; 244926b15dadSJamal Hadi Salim struct xfrm_usersa_flush *p; 245026b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 245126b15dadSJamal Hadi Salim struct sk_buff *skb; 24527deb2264SThomas Graf int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); 245326b15dadSJamal Hadi Salim 24547deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 245526b15dadSJamal Hadi Salim if (skb == NULL) 245626b15dadSJamal Hadi Salim return -ENOMEM; 245726b15dadSJamal Hadi Salim 245879b8b7f4SThomas Graf nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); 245979b8b7f4SThomas Graf if (nlh == NULL) { 246079b8b7f4SThomas Graf kfree_skb(skb); 246179b8b7f4SThomas Graf return -EMSGSIZE; 246279b8b7f4SThomas Graf } 246326b15dadSJamal Hadi Salim 24647b67c857SThomas Graf p = nlmsg_data(nlh); 2465bf08867fSHerbert Xu p->proto = c->data.proto; 246626b15dadSJamal Hadi Salim 24679825069dSThomas Graf nlmsg_end(skb, nlh); 246826b15dadSJamal Hadi Salim 2469a6483b79SAlexey Dobriyan return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); 247026b15dadSJamal Hadi Salim } 247126b15dadSJamal Hadi Salim 24727deb2264SThomas Graf static inline size_t xfrm_sa_len(struct xfrm_state *x) 247326b15dadSJamal Hadi Salim { 24747deb2264SThomas Graf size_t l = 0; 24751a6509d9SHerbert Xu if (x->aead) 24761a6509d9SHerbert Xu l += nla_total_size(aead_len(x->aead)); 24774447bb33SMartin Willi if (x->aalg) { 24784447bb33SMartin Willi l += nla_total_size(sizeof(struct xfrm_algo) + 24794447bb33SMartin Willi (x->aalg->alg_key_len + 7) / 8); 24804447bb33SMartin Willi l += nla_total_size(xfrm_alg_auth_len(x->aalg)); 24814447bb33SMartin Willi } 248226b15dadSJamal Hadi Salim if (x->ealg) 24830f99be0dSEric Dumazet l += nla_total_size(xfrm_alg_len(x->ealg)); 248426b15dadSJamal Hadi Salim if (x->calg) 24857deb2264SThomas Graf l += nla_total_size(sizeof(*x->calg)); 248626b15dadSJamal Hadi Salim if (x->encap) 24877deb2264SThomas Graf l += nla_total_size(sizeof(*x->encap)); 248835d2856bSMartin Willi if (x->tfcpad) 248935d2856bSMartin Willi l += nla_total_size(sizeof(x->tfcpad)); 2490d8647b79SSteffen Klassert if (x->replay_esn) 2491d8647b79SSteffen Klassert l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn)); 249268325d3bSHerbert Xu if (x->security) 249368325d3bSHerbert Xu l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + 249468325d3bSHerbert Xu x->security->ctx_len); 249568325d3bSHerbert Xu if (x->coaddr) 249668325d3bSHerbert Xu l += nla_total_size(sizeof(*x->coaddr)); 249768325d3bSHerbert Xu 2498d26f3984SHerbert Xu /* Must count x->lastused as it may become non-zero behind our back. */ 2499d26f3984SHerbert Xu l += nla_total_size(sizeof(u64)); 250026b15dadSJamal Hadi Salim 250126b15dadSJamal Hadi Salim return l; 250226b15dadSJamal Hadi Salim } 250326b15dadSJamal Hadi Salim 2504214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c) 250526b15dadSJamal Hadi Salim { 2506fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 250726b15dadSJamal Hadi Salim struct xfrm_usersa_info *p; 25080603eac0SHerbert Xu struct xfrm_usersa_id *id; 250926b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 251026b15dadSJamal Hadi Salim struct sk_buff *skb; 251126b15dadSJamal Hadi Salim int len = xfrm_sa_len(x); 25121d1e34ddSDavid S. Miller int headlen, err; 25130603eac0SHerbert Xu 25140603eac0SHerbert Xu headlen = sizeof(*p); 25150603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 25167deb2264SThomas Graf len += nla_total_size(headlen); 25170603eac0SHerbert Xu headlen = sizeof(*id); 25186f26b61eSJamal Hadi Salim len += nla_total_size(sizeof(struct xfrm_mark)); 25190603eac0SHerbert Xu } 25207deb2264SThomas Graf len += NLMSG_ALIGN(headlen); 252126b15dadSJamal Hadi Salim 25227deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 252326b15dadSJamal Hadi Salim if (skb == NULL) 252426b15dadSJamal Hadi Salim return -ENOMEM; 252526b15dadSJamal Hadi Salim 252679b8b7f4SThomas Graf nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); 25271d1e34ddSDavid S. Miller err = -EMSGSIZE; 252879b8b7f4SThomas Graf if (nlh == NULL) 25291d1e34ddSDavid S. Miller goto out_free_skb; 253026b15dadSJamal Hadi Salim 25317b67c857SThomas Graf p = nlmsg_data(nlh); 25320603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 2533c0144beaSThomas Graf struct nlattr *attr; 2534c0144beaSThomas Graf 25357b67c857SThomas Graf id = nlmsg_data(nlh); 25360603eac0SHerbert Xu memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); 25370603eac0SHerbert Xu id->spi = x->id.spi; 25380603eac0SHerbert Xu id->family = x->props.family; 25390603eac0SHerbert Xu id->proto = x->id.proto; 25400603eac0SHerbert Xu 2541c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); 25421d1e34ddSDavid S. Miller err = -EMSGSIZE; 2543c0144beaSThomas Graf if (attr == NULL) 25441d1e34ddSDavid S. Miller goto out_free_skb; 2545c0144beaSThomas Graf 2546c0144beaSThomas Graf p = nla_data(attr); 25470603eac0SHerbert Xu } 25481d1e34ddSDavid S. Miller err = copy_to_user_state_extra(x, p, skb); 25491d1e34ddSDavid S. Miller if (err) 25501d1e34ddSDavid S. Miller goto out_free_skb; 255126b15dadSJamal Hadi Salim 25529825069dSThomas Graf nlmsg_end(skb, nlh); 255326b15dadSJamal Hadi Salim 2554a6483b79SAlexey Dobriyan return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); 255526b15dadSJamal Hadi Salim 25561d1e34ddSDavid S. Miller out_free_skb: 255726b15dadSJamal Hadi Salim kfree_skb(skb); 25581d1e34ddSDavid S. Miller return err; 255926b15dadSJamal Hadi Salim } 256026b15dadSJamal Hadi Salim 2561214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c) 256226b15dadSJamal Hadi Salim { 256326b15dadSJamal Hadi Salim 256426b15dadSJamal Hadi Salim switch (c->event) { 2565f60f6b8fSHerbert Xu case XFRM_MSG_EXPIRE: 256626b15dadSJamal Hadi Salim return xfrm_exp_state_notify(x, c); 2567d51d081dSJamal Hadi Salim case XFRM_MSG_NEWAE: 2568d51d081dSJamal Hadi Salim return xfrm_aevent_state_notify(x, c); 2569f60f6b8fSHerbert Xu case XFRM_MSG_DELSA: 2570f60f6b8fSHerbert Xu case XFRM_MSG_UPDSA: 2571f60f6b8fSHerbert Xu case XFRM_MSG_NEWSA: 257226b15dadSJamal Hadi Salim return xfrm_notify_sa(x, c); 2573f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHSA: 257426b15dadSJamal Hadi Salim return xfrm_notify_sa_flush(c); 257526b15dadSJamal Hadi Salim default: 257662db5cfdSstephen hemminger printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n", 257762db5cfdSstephen hemminger c->event); 257826b15dadSJamal Hadi Salim break; 257926b15dadSJamal Hadi Salim } 258026b15dadSJamal Hadi Salim 258126b15dadSJamal Hadi Salim return 0; 258226b15dadSJamal Hadi Salim 258326b15dadSJamal Hadi Salim } 258426b15dadSJamal Hadi Salim 25857deb2264SThomas Graf static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x, 25867deb2264SThomas Graf struct xfrm_policy *xp) 25877deb2264SThomas Graf { 25887deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) 25897deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 25906f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 25917deb2264SThomas Graf + nla_total_size(xfrm_user_sec_ctx_size(x->security)) 25927deb2264SThomas Graf + userpolicy_type_attrsize(); 25937deb2264SThomas Graf } 25947deb2264SThomas Graf 25951da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, 25961da177e4SLinus Torvalds struct xfrm_tmpl *xt, struct xfrm_policy *xp, 25971da177e4SLinus Torvalds int dir) 25981da177e4SLinus Torvalds { 25991d1e34ddSDavid S. Miller __u32 seq = xfrm_get_acqseq(); 26001da177e4SLinus Torvalds struct xfrm_user_acquire *ua; 26011da177e4SLinus Torvalds struct nlmsghdr *nlh; 26021d1e34ddSDavid S. Miller int err; 26031da177e4SLinus Torvalds 260479b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); 260579b8b7f4SThomas Graf if (nlh == NULL) 260679b8b7f4SThomas Graf return -EMSGSIZE; 26071da177e4SLinus Torvalds 26087b67c857SThomas Graf ua = nlmsg_data(nlh); 26091da177e4SLinus Torvalds memcpy(&ua->id, &x->id, sizeof(ua->id)); 26101da177e4SLinus Torvalds memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); 26111da177e4SLinus Torvalds memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); 26121da177e4SLinus Torvalds copy_to_user_policy(xp, &ua->policy, dir); 26131da177e4SLinus Torvalds ua->aalgos = xt->aalgos; 26141da177e4SLinus Torvalds ua->ealgos = xt->ealgos; 26151da177e4SLinus Torvalds ua->calgos = xt->calgos; 26161da177e4SLinus Torvalds ua->seq = x->km.seq = seq; 26171da177e4SLinus Torvalds 26181d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 26191d1e34ddSDavid S. Miller if (!err) 26201d1e34ddSDavid S. Miller err = copy_to_user_state_sec_ctx(x, skb); 26211d1e34ddSDavid S. Miller if (!err) 26221d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 26231d1e34ddSDavid S. Miller if (!err) 26241d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 26251d1e34ddSDavid S. Miller if (err) { 26261d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 26271d1e34ddSDavid S. Miller return err; 26281d1e34ddSDavid S. Miller } 26291da177e4SLinus Torvalds 26309825069dSThomas Graf return nlmsg_end(skb, nlh); 26311da177e4SLinus Torvalds } 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, 26341da177e4SLinus Torvalds struct xfrm_policy *xp, int dir) 26351da177e4SLinus Torvalds { 2636a6483b79SAlexey Dobriyan struct net *net = xs_net(x); 26371da177e4SLinus Torvalds struct sk_buff *skb; 26381da177e4SLinus Torvalds 26397deb2264SThomas Graf skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); 26401da177e4SLinus Torvalds if (skb == NULL) 26411da177e4SLinus Torvalds return -ENOMEM; 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds if (build_acquire(skb, x, xt, xp, dir) < 0) 26441da177e4SLinus Torvalds BUG(); 26451da177e4SLinus Torvalds 2646a6483b79SAlexey Dobriyan return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC); 26471da177e4SLinus Torvalds } 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0 26501da177e4SLinus Torvalds * or more templates. 26511da177e4SLinus Torvalds */ 2652cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, 26531da177e4SLinus Torvalds u8 *data, int len, int *dir) 26541da177e4SLinus Torvalds { 2655fc34acd3SAlexey Dobriyan struct net *net = sock_net(sk); 26561da177e4SLinus Torvalds struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; 26571da177e4SLinus Torvalds struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); 26581da177e4SLinus Torvalds struct xfrm_policy *xp; 26591da177e4SLinus Torvalds int nr; 26601da177e4SLinus Torvalds 2661cb969f07SVenkat Yekkirala switch (sk->sk_family) { 26621da177e4SLinus Torvalds case AF_INET: 26631da177e4SLinus Torvalds if (opt != IP_XFRM_POLICY) { 26641da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 26651da177e4SLinus Torvalds return NULL; 26661da177e4SLinus Torvalds } 26671da177e4SLinus Torvalds break; 2668dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 26691da177e4SLinus Torvalds case AF_INET6: 26701da177e4SLinus Torvalds if (opt != IPV6_XFRM_POLICY) { 26711da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 26721da177e4SLinus Torvalds return NULL; 26731da177e4SLinus Torvalds } 26741da177e4SLinus Torvalds break; 26751da177e4SLinus Torvalds #endif 26761da177e4SLinus Torvalds default: 26771da177e4SLinus Torvalds *dir = -EINVAL; 26781da177e4SLinus Torvalds return NULL; 26791da177e4SLinus Torvalds } 26801da177e4SLinus Torvalds 26811da177e4SLinus Torvalds *dir = -EINVAL; 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds if (len < sizeof(*p) || 26841da177e4SLinus Torvalds verify_newpolicy_info(p)) 26851da177e4SLinus Torvalds return NULL; 26861da177e4SLinus Torvalds 26871da177e4SLinus Torvalds nr = ((len - sizeof(*p)) / sizeof(*ut)); 2688b4ad86bfSDavid S. Miller if (validate_tmpl(nr, ut, p->sel.family)) 26891da177e4SLinus Torvalds return NULL; 26901da177e4SLinus Torvalds 2691a4f1bac6SHerbert Xu if (p->dir > XFRM_POLICY_OUT) 2692a4f1bac6SHerbert Xu return NULL; 2693a4f1bac6SHerbert Xu 26942f09a4d5SHerbert Xu xp = xfrm_policy_alloc(net, GFP_ATOMIC); 26951da177e4SLinus Torvalds if (xp == NULL) { 26961da177e4SLinus Torvalds *dir = -ENOBUFS; 26971da177e4SLinus Torvalds return NULL; 26981da177e4SLinus Torvalds } 26991da177e4SLinus Torvalds 27001da177e4SLinus Torvalds copy_from_user_policy(xp, p); 2701f7b6983fSMasahide NAKAMURA xp->type = XFRM_POLICY_TYPE_MAIN; 27021da177e4SLinus Torvalds copy_templates(xp, ut, nr); 27031da177e4SLinus Torvalds 27041da177e4SLinus Torvalds *dir = p->dir; 27051da177e4SLinus Torvalds 27061da177e4SLinus Torvalds return xp; 27071da177e4SLinus Torvalds } 27081da177e4SLinus Torvalds 27097deb2264SThomas Graf static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp) 27107deb2264SThomas Graf { 27117deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) 27127deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 27137deb2264SThomas Graf + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) 2714295fae56SJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 27157deb2264SThomas Graf + userpolicy_type_attrsize(); 27167deb2264SThomas Graf } 27177deb2264SThomas Graf 27181da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, 2719214e005bSDavid S. Miller int dir, const struct km_event *c) 27201da177e4SLinus Torvalds { 27211da177e4SLinus Torvalds struct xfrm_user_polexpire *upe; 2722d51d081dSJamal Hadi Salim int hard = c->data.hard; 27231d1e34ddSDavid S. Miller struct nlmsghdr *nlh; 27241d1e34ddSDavid S. Miller int err; 27251da177e4SLinus Torvalds 272679b8b7f4SThomas Graf nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); 272779b8b7f4SThomas Graf if (nlh == NULL) 272879b8b7f4SThomas Graf return -EMSGSIZE; 27291da177e4SLinus Torvalds 27307b67c857SThomas Graf upe = nlmsg_data(nlh); 27311da177e4SLinus Torvalds copy_to_user_policy(xp, &upe->pol, dir); 27321d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 27331d1e34ddSDavid S. Miller if (!err) 27341d1e34ddSDavid S. Miller err = copy_to_user_sec_ctx(xp, skb); 27351d1e34ddSDavid S. Miller if (!err) 27361d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 27371d1e34ddSDavid S. Miller if (!err) 27381d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 27391d1e34ddSDavid S. Miller if (err) { 27401d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 27411d1e34ddSDavid S. Miller return err; 27421d1e34ddSDavid S. Miller } 27431da177e4SLinus Torvalds upe->hard = !!hard; 27441da177e4SLinus Torvalds 27459825069dSThomas Graf return nlmsg_end(skb, nlh); 27461da177e4SLinus Torvalds } 27471da177e4SLinus Torvalds 2748214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 27491da177e4SLinus Torvalds { 2750fc34acd3SAlexey Dobriyan struct net *net = xp_net(xp); 27511da177e4SLinus Torvalds struct sk_buff *skb; 27521da177e4SLinus Torvalds 27537deb2264SThomas Graf skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); 27541da177e4SLinus Torvalds if (skb == NULL) 27551da177e4SLinus Torvalds return -ENOMEM; 27561da177e4SLinus Torvalds 2757d51d081dSJamal Hadi Salim if (build_polexpire(skb, xp, dir, c) < 0) 27581da177e4SLinus Torvalds BUG(); 27591da177e4SLinus Torvalds 2760a6483b79SAlexey Dobriyan return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); 27611da177e4SLinus Torvalds } 27621da177e4SLinus Torvalds 2763214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c) 276426b15dadSJamal Hadi Salim { 27651d1e34ddSDavid S. Miller int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 2766fc34acd3SAlexey Dobriyan struct net *net = xp_net(xp); 276726b15dadSJamal Hadi Salim struct xfrm_userpolicy_info *p; 27680603eac0SHerbert Xu struct xfrm_userpolicy_id *id; 276926b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 277026b15dadSJamal Hadi Salim struct sk_buff *skb; 27711d1e34ddSDavid S. Miller int headlen, err; 27720603eac0SHerbert Xu 27730603eac0SHerbert Xu headlen = sizeof(*p); 27740603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 27757deb2264SThomas Graf len += nla_total_size(headlen); 27760603eac0SHerbert Xu headlen = sizeof(*id); 27770603eac0SHerbert Xu } 2778cfbfd45aSThomas Graf len += userpolicy_type_attrsize(); 2779295fae56SJamal Hadi Salim len += nla_total_size(sizeof(struct xfrm_mark)); 27807deb2264SThomas Graf len += NLMSG_ALIGN(headlen); 278126b15dadSJamal Hadi Salim 27827deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 278326b15dadSJamal Hadi Salim if (skb == NULL) 278426b15dadSJamal Hadi Salim return -ENOMEM; 278526b15dadSJamal Hadi Salim 278679b8b7f4SThomas Graf nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); 27871d1e34ddSDavid S. Miller err = -EMSGSIZE; 278879b8b7f4SThomas Graf if (nlh == NULL) 27891d1e34ddSDavid S. Miller goto out_free_skb; 279026b15dadSJamal Hadi Salim 27917b67c857SThomas Graf p = nlmsg_data(nlh); 27920603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 2793c0144beaSThomas Graf struct nlattr *attr; 2794c0144beaSThomas Graf 27957b67c857SThomas Graf id = nlmsg_data(nlh); 27960603eac0SHerbert Xu memset(id, 0, sizeof(*id)); 27970603eac0SHerbert Xu id->dir = dir; 27980603eac0SHerbert Xu if (c->data.byid) 27990603eac0SHerbert Xu id->index = xp->index; 28000603eac0SHerbert Xu else 28010603eac0SHerbert Xu memcpy(&id->sel, &xp->selector, sizeof(id->sel)); 28020603eac0SHerbert Xu 2803c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); 28041d1e34ddSDavid S. Miller err = -EMSGSIZE; 2805c0144beaSThomas Graf if (attr == NULL) 28061d1e34ddSDavid S. Miller goto out_free_skb; 2807c0144beaSThomas Graf 2808c0144beaSThomas Graf p = nla_data(attr); 28090603eac0SHerbert Xu } 281026b15dadSJamal Hadi Salim 281126b15dadSJamal Hadi Salim copy_to_user_policy(xp, p, dir); 28121d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 28131d1e34ddSDavid S. Miller if (!err) 28141d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 28151d1e34ddSDavid S. Miller if (!err) 28161d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 28171d1e34ddSDavid S. Miller if (err) 28181d1e34ddSDavid S. Miller goto out_free_skb; 2819295fae56SJamal Hadi Salim 28209825069dSThomas Graf nlmsg_end(skb, nlh); 282126b15dadSJamal Hadi Salim 2822a6483b79SAlexey Dobriyan return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); 282326b15dadSJamal Hadi Salim 28241d1e34ddSDavid S. Miller out_free_skb: 282526b15dadSJamal Hadi Salim kfree_skb(skb); 28261d1e34ddSDavid S. Miller return err; 282726b15dadSJamal Hadi Salim } 282826b15dadSJamal Hadi Salim 2829214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c) 283026b15dadSJamal Hadi Salim { 28317067802eSAlexey Dobriyan struct net *net = c->net; 283226b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 283326b15dadSJamal Hadi Salim struct sk_buff *skb; 28341d1e34ddSDavid S. Miller int err; 283526b15dadSJamal Hadi Salim 28367deb2264SThomas Graf skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); 283726b15dadSJamal Hadi Salim if (skb == NULL) 283826b15dadSJamal Hadi Salim return -ENOMEM; 283926b15dadSJamal Hadi Salim 284079b8b7f4SThomas Graf nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); 28411d1e34ddSDavid S. Miller err = -EMSGSIZE; 284279b8b7f4SThomas Graf if (nlh == NULL) 28431d1e34ddSDavid S. Miller goto out_free_skb; 28441d1e34ddSDavid S. Miller err = copy_to_user_policy_type(c->data.type, skb); 28451d1e34ddSDavid S. Miller if (err) 28461d1e34ddSDavid S. Miller goto out_free_skb; 284726b15dadSJamal Hadi Salim 28489825069dSThomas Graf nlmsg_end(skb, nlh); 284926b15dadSJamal Hadi Salim 2850a6483b79SAlexey Dobriyan return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); 285126b15dadSJamal Hadi Salim 28521d1e34ddSDavid S. Miller out_free_skb: 285326b15dadSJamal Hadi Salim kfree_skb(skb); 28541d1e34ddSDavid S. Miller return err; 285526b15dadSJamal Hadi Salim } 285626b15dadSJamal Hadi Salim 2857214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 285826b15dadSJamal Hadi Salim { 285926b15dadSJamal Hadi Salim 286026b15dadSJamal Hadi Salim switch (c->event) { 2861f60f6b8fSHerbert Xu case XFRM_MSG_NEWPOLICY: 2862f60f6b8fSHerbert Xu case XFRM_MSG_UPDPOLICY: 2863f60f6b8fSHerbert Xu case XFRM_MSG_DELPOLICY: 286426b15dadSJamal Hadi Salim return xfrm_notify_policy(xp, dir, c); 2865f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHPOLICY: 286626b15dadSJamal Hadi Salim return xfrm_notify_policy_flush(c); 2867f60f6b8fSHerbert Xu case XFRM_MSG_POLEXPIRE: 286826b15dadSJamal Hadi Salim return xfrm_exp_policy_notify(xp, dir, c); 286926b15dadSJamal Hadi Salim default: 287062db5cfdSstephen hemminger printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n", 287162db5cfdSstephen hemminger c->event); 287226b15dadSJamal Hadi Salim } 287326b15dadSJamal Hadi Salim 287426b15dadSJamal Hadi Salim return 0; 287526b15dadSJamal Hadi Salim 287626b15dadSJamal Hadi Salim } 287726b15dadSJamal Hadi Salim 28787deb2264SThomas Graf static inline size_t xfrm_report_msgsize(void) 28797deb2264SThomas Graf { 28807deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); 28817deb2264SThomas Graf } 28827deb2264SThomas Graf 288397a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto, 288497a64b45SMasahide NAKAMURA struct xfrm_selector *sel, xfrm_address_t *addr) 288597a64b45SMasahide NAKAMURA { 288697a64b45SMasahide NAKAMURA struct xfrm_user_report *ur; 288797a64b45SMasahide NAKAMURA struct nlmsghdr *nlh; 288897a64b45SMasahide NAKAMURA 288979b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); 289079b8b7f4SThomas Graf if (nlh == NULL) 289179b8b7f4SThomas Graf return -EMSGSIZE; 289297a64b45SMasahide NAKAMURA 28937b67c857SThomas Graf ur = nlmsg_data(nlh); 289497a64b45SMasahide NAKAMURA ur->proto = proto; 289597a64b45SMasahide NAKAMURA memcpy(&ur->sel, sel, sizeof(ur->sel)); 289697a64b45SMasahide NAKAMURA 28971d1e34ddSDavid S. Miller if (addr) { 28981d1e34ddSDavid S. Miller int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr); 28991d1e34ddSDavid S. Miller if (err) { 29009825069dSThomas Graf nlmsg_cancel(skb, nlh); 29011d1e34ddSDavid S. Miller return err; 29021d1e34ddSDavid S. Miller } 29031d1e34ddSDavid S. Miller } 29041d1e34ddSDavid S. Miller return nlmsg_end(skb, nlh); 290597a64b45SMasahide NAKAMURA } 290697a64b45SMasahide NAKAMURA 2907db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto, 2908db983c11SAlexey Dobriyan struct xfrm_selector *sel, xfrm_address_t *addr) 290997a64b45SMasahide NAKAMURA { 291097a64b45SMasahide NAKAMURA struct sk_buff *skb; 291197a64b45SMasahide NAKAMURA 29127deb2264SThomas Graf skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); 291397a64b45SMasahide NAKAMURA if (skb == NULL) 291497a64b45SMasahide NAKAMURA return -ENOMEM; 291597a64b45SMasahide NAKAMURA 291697a64b45SMasahide NAKAMURA if (build_report(skb, proto, sel, addr) < 0) 291797a64b45SMasahide NAKAMURA BUG(); 291897a64b45SMasahide NAKAMURA 2919a6483b79SAlexey Dobriyan return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC); 292097a64b45SMasahide NAKAMURA } 292197a64b45SMasahide NAKAMURA 29223a2dfbe8SMartin Willi static inline size_t xfrm_mapping_msgsize(void) 29233a2dfbe8SMartin Willi { 29243a2dfbe8SMartin Willi return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping)); 29253a2dfbe8SMartin Willi } 29263a2dfbe8SMartin Willi 29273a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x, 29283a2dfbe8SMartin Willi xfrm_address_t *new_saddr, __be16 new_sport) 29293a2dfbe8SMartin Willi { 29303a2dfbe8SMartin Willi struct xfrm_user_mapping *um; 29313a2dfbe8SMartin Willi struct nlmsghdr *nlh; 29323a2dfbe8SMartin Willi 29333a2dfbe8SMartin Willi nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0); 29343a2dfbe8SMartin Willi if (nlh == NULL) 29353a2dfbe8SMartin Willi return -EMSGSIZE; 29363a2dfbe8SMartin Willi 29373a2dfbe8SMartin Willi um = nlmsg_data(nlh); 29383a2dfbe8SMartin Willi 29393a2dfbe8SMartin Willi memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr)); 29403a2dfbe8SMartin Willi um->id.spi = x->id.spi; 29413a2dfbe8SMartin Willi um->id.family = x->props.family; 29423a2dfbe8SMartin Willi um->id.proto = x->id.proto; 29433a2dfbe8SMartin Willi memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr)); 29443a2dfbe8SMartin Willi memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr)); 29453a2dfbe8SMartin Willi um->new_sport = new_sport; 29463a2dfbe8SMartin Willi um->old_sport = x->encap->encap_sport; 29473a2dfbe8SMartin Willi um->reqid = x->props.reqid; 29483a2dfbe8SMartin Willi 29493a2dfbe8SMartin Willi return nlmsg_end(skb, nlh); 29503a2dfbe8SMartin Willi } 29513a2dfbe8SMartin Willi 29523a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, 29533a2dfbe8SMartin Willi __be16 sport) 29543a2dfbe8SMartin Willi { 2955a6483b79SAlexey Dobriyan struct net *net = xs_net(x); 29563a2dfbe8SMartin Willi struct sk_buff *skb; 29573a2dfbe8SMartin Willi 29583a2dfbe8SMartin Willi if (x->id.proto != IPPROTO_ESP) 29593a2dfbe8SMartin Willi return -EINVAL; 29603a2dfbe8SMartin Willi 29613a2dfbe8SMartin Willi if (!x->encap) 29623a2dfbe8SMartin Willi return -EINVAL; 29633a2dfbe8SMartin Willi 29643a2dfbe8SMartin Willi skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC); 29653a2dfbe8SMartin Willi if (skb == NULL) 29663a2dfbe8SMartin Willi return -ENOMEM; 29673a2dfbe8SMartin Willi 29683a2dfbe8SMartin Willi if (build_mapping(skb, x, ipaddr, sport) < 0) 29693a2dfbe8SMartin Willi BUG(); 29703a2dfbe8SMartin Willi 2971a6483b79SAlexey Dobriyan return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC); 29723a2dfbe8SMartin Willi } 29733a2dfbe8SMartin Willi 29741da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = { 29751da177e4SLinus Torvalds .id = "netlink", 29761da177e4SLinus Torvalds .notify = xfrm_send_state_notify, 29771da177e4SLinus Torvalds .acquire = xfrm_send_acquire, 29781da177e4SLinus Torvalds .compile_policy = xfrm_compile_policy, 29791da177e4SLinus Torvalds .notify_policy = xfrm_send_policy_notify, 298097a64b45SMasahide NAKAMURA .report = xfrm_send_report, 29815c79de6eSShinta Sugimoto .migrate = xfrm_send_migrate, 29823a2dfbe8SMartin Willi .new_mapping = xfrm_send_mapping, 29831da177e4SLinus Torvalds }; 29841da177e4SLinus Torvalds 2985a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net) 29861da177e4SLinus Torvalds { 2987be33690dSPatrick McHardy struct sock *nlsk; 2988a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 2989a31f2d17SPablo Neira Ayuso .groups = XFRMNLGRP_MAX, 2990a31f2d17SPablo Neira Ayuso .input = xfrm_netlink_rcv, 2991a31f2d17SPablo Neira Ayuso }; 2992be33690dSPatrick McHardy 2993a31f2d17SPablo Neira Ayuso nlsk = netlink_kernel_create(net, NETLINK_XFRM, THIS_MODULE, &cfg); 2994be33690dSPatrick McHardy if (nlsk == NULL) 29951da177e4SLinus Torvalds return -ENOMEM; 2996d79d792eSEric W. Biederman net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */ 2997cf778b00SEric Dumazet rcu_assign_pointer(net->xfrm.nlsk, nlsk); 29981da177e4SLinus Torvalds return 0; 29991da177e4SLinus Torvalds } 30001da177e4SLinus Torvalds 3001d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list) 3002a6483b79SAlexey Dobriyan { 3003d79d792eSEric W. Biederman struct net *net; 3004d79d792eSEric W. Biederman list_for_each_entry(net, net_exit_list, exit_list) 3005a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(net->xfrm.nlsk, NULL); 3006d79d792eSEric W. Biederman synchronize_net(); 3007d79d792eSEric W. Biederman list_for_each_entry(net, net_exit_list, exit_list) 3008d79d792eSEric W. Biederman netlink_kernel_release(net->xfrm.nlsk_stash); 3009a6483b79SAlexey Dobriyan } 3010a6483b79SAlexey Dobriyan 3011a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = { 3012a6483b79SAlexey Dobriyan .init = xfrm_user_net_init, 3013d79d792eSEric W. Biederman .exit_batch = xfrm_user_net_exit, 3014a6483b79SAlexey Dobriyan }; 3015a6483b79SAlexey Dobriyan 3016a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void) 3017a6483b79SAlexey Dobriyan { 3018a6483b79SAlexey Dobriyan int rv; 3019a6483b79SAlexey Dobriyan 3020a6483b79SAlexey Dobriyan printk(KERN_INFO "Initializing XFRM netlink socket\n"); 3021a6483b79SAlexey Dobriyan 3022a6483b79SAlexey Dobriyan rv = register_pernet_subsys(&xfrm_user_net_ops); 3023a6483b79SAlexey Dobriyan if (rv < 0) 3024a6483b79SAlexey Dobriyan return rv; 3025a6483b79SAlexey Dobriyan rv = xfrm_register_km(&netlink_mgr); 3026a6483b79SAlexey Dobriyan if (rv < 0) 3027a6483b79SAlexey Dobriyan unregister_pernet_subsys(&xfrm_user_net_ops); 3028a6483b79SAlexey Dobriyan return rv; 3029a6483b79SAlexey Dobriyan } 3030a6483b79SAlexey Dobriyan 30311da177e4SLinus Torvalds static void __exit xfrm_user_exit(void) 30321da177e4SLinus Torvalds { 30331da177e4SLinus Torvalds xfrm_unregister_km(&netlink_mgr); 3034a6483b79SAlexey Dobriyan unregister_pernet_subsys(&xfrm_user_net_ops); 30351da177e4SLinus Torvalds } 30361da177e4SLinus Torvalds 30371da177e4SLinus Torvalds module_init(xfrm_user_init); 30381da177e4SLinus Torvalds module_exit(xfrm_user_exit); 30391da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 30404fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); 3041f8cd5488SJamal Hadi Salim 3042