109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* xfrm_user.c: User interface to configure xfrm engine. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2002 David S. Miller (davem@redhat.com) 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Changes: 71da177e4SLinus Torvalds * Mitsuru KANDA @USAGI 81da177e4SLinus Torvalds * Kazunori MIYAZAWA @USAGI 91da177e4SLinus Torvalds * Kunihiro Ishiguro <kunihiro@ipinfusion.com> 101da177e4SLinus Torvalds * IPv6 support 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 149409f38aSHerbert Xu #include <linux/crypto.h> 151da177e4SLinus Torvalds #include <linux/module.h> 161da177e4SLinus Torvalds #include <linux/kernel.h> 171da177e4SLinus Torvalds #include <linux/types.h> 181da177e4SLinus Torvalds #include <linux/slab.h> 191da177e4SLinus Torvalds #include <linux/socket.h> 201da177e4SLinus Torvalds #include <linux/string.h> 211da177e4SLinus Torvalds #include <linux/net.h> 221da177e4SLinus Torvalds #include <linux/skbuff.h> 231da177e4SLinus Torvalds #include <linux/pfkeyv2.h> 241da177e4SLinus Torvalds #include <linux/ipsec.h> 251da177e4SLinus Torvalds #include <linux/init.h> 261da177e4SLinus Torvalds #include <linux/security.h> 271da177e4SLinus Torvalds #include <net/sock.h> 281da177e4SLinus Torvalds #include <net/xfrm.h> 2988fc2c84SThomas Graf #include <net/netlink.h> 30fa6dd8a2SNicolas Dichtel #include <net/ah.h> 317c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 32dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 33e23c7194SMasahide NAKAMURA #include <linux/in6.h> 34e23c7194SMasahide NAKAMURA #endif 35e33d4f13SSowmini Varadhan #include <asm/unaligned.h> 361da177e4SLinus Torvalds 375424f32eSThomas Graf static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type) 381da177e4SLinus Torvalds { 395424f32eSThomas Graf struct nlattr *rt = attrs[type]; 401da177e4SLinus Torvalds struct xfrm_algo *algp; 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds if (!rt) 431da177e4SLinus Torvalds return 0; 441da177e4SLinus Torvalds 455424f32eSThomas Graf algp = nla_data(rt); 4606cd22f8SAlexey Dobriyan if (nla_len(rt) < (int)xfrm_alg_len(algp)) 4731c26852SHerbert Xu return -EINVAL; 4831c26852SHerbert Xu 491da177e4SLinus Torvalds switch (type) { 501da177e4SLinus Torvalds case XFRMA_ALG_AUTH: 511da177e4SLinus Torvalds case XFRMA_ALG_CRYPT: 521da177e4SLinus Torvalds case XFRMA_ALG_COMP: 531da177e4SLinus Torvalds break; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds default: 561da177e4SLinus Torvalds return -EINVAL; 573ff50b79SStephen Hemminger } 581da177e4SLinus Torvalds 59633439f5SHerbert Xu algp->alg_name[sizeof(algp->alg_name) - 1] = '\0'; 601da177e4SLinus Torvalds return 0; 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds 634447bb33SMartin Willi static int verify_auth_trunc(struct nlattr **attrs) 644447bb33SMartin Willi { 654447bb33SMartin Willi struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC]; 664447bb33SMartin Willi struct xfrm_algo_auth *algp; 674447bb33SMartin Willi 684447bb33SMartin Willi if (!rt) 694447bb33SMartin Willi return 0; 704447bb33SMartin Willi 714447bb33SMartin Willi algp = nla_data(rt); 721bd963a7SAlexey Dobriyan if (nla_len(rt) < (int)xfrm_alg_auth_len(algp)) 734447bb33SMartin Willi return -EINVAL; 744447bb33SMartin Willi 75633439f5SHerbert Xu algp->alg_name[sizeof(algp->alg_name) - 1] = '\0'; 764447bb33SMartin Willi return 0; 774447bb33SMartin Willi } 784447bb33SMartin Willi 791a6509d9SHerbert Xu static int verify_aead(struct nlattr **attrs) 801a6509d9SHerbert Xu { 811a6509d9SHerbert Xu struct nlattr *rt = attrs[XFRMA_ALG_AEAD]; 821a6509d9SHerbert Xu struct xfrm_algo_aead *algp; 831a6509d9SHerbert Xu 841a6509d9SHerbert Xu if (!rt) 851a6509d9SHerbert Xu return 0; 861a6509d9SHerbert Xu 871a6509d9SHerbert Xu algp = nla_data(rt); 88373b8eebSAlexey Dobriyan if (nla_len(rt) < (int)aead_len(algp)) 891a6509d9SHerbert Xu return -EINVAL; 901a6509d9SHerbert Xu 91633439f5SHerbert Xu algp->alg_name[sizeof(algp->alg_name) - 1] = '\0'; 921a6509d9SHerbert Xu return 0; 931a6509d9SHerbert Xu } 941a6509d9SHerbert Xu 955424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type, 96eb2971b6SMasahide NAKAMURA xfrm_address_t **addrp) 97eb2971b6SMasahide NAKAMURA { 985424f32eSThomas Graf struct nlattr *rt = attrs[type]; 99eb2971b6SMasahide NAKAMURA 100cf5cb79fSThomas Graf if (rt && addrp) 1015424f32eSThomas Graf *addrp = nla_data(rt); 102eb2971b6SMasahide NAKAMURA } 103df71837dSTrent Jaeger 1045424f32eSThomas Graf static inline int verify_sec_ctx_len(struct nlattr **attrs) 105df71837dSTrent Jaeger { 1065424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 107df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx; 108df71837dSTrent Jaeger 109df71837dSTrent Jaeger if (!rt) 110df71837dSTrent Jaeger return 0; 111df71837dSTrent Jaeger 1125424f32eSThomas Graf uctx = nla_data(rt); 113171d449aSXin Long if (uctx->len > nla_len(rt) || 114171d449aSXin Long uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) 115df71837dSTrent Jaeger return -EINVAL; 116df71837dSTrent Jaeger 117df71837dSTrent Jaeger return 0; 118df71837dSTrent Jaeger } 119df71837dSTrent Jaeger 120d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p, 121d8647b79SSteffen Klassert struct nlattr **attrs) 122d8647b79SSteffen Klassert { 123d8647b79SSteffen Klassert struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL]; 124ecd79187SMathias Krause struct xfrm_replay_state_esn *rs; 125d8647b79SSteffen Klassert 126ecd79187SMathias Krause if (!rt) 127d97ca5d7SFlorian Westphal return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0; 1287833aa05SSteffen Klassert 129ecd79187SMathias Krause rs = nla_data(rt); 130ecd79187SMathias Krause 131ecd79187SMathias Krause if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8) 132ecd79187SMathias Krause return -EINVAL; 133ecd79187SMathias Krause 1345e708e47SAlexey Dobriyan if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) && 135ecd79187SMathias Krause nla_len(rt) != sizeof(*rs)) 136ecd79187SMathias Krause return -EINVAL; 137d8647b79SSteffen Klassert 13801714109SFan Du /* As only ESP and AH support ESN feature. */ 13901714109SFan Du if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH)) 14002aadf72SSteffen Klassert return -EINVAL; 14102aadf72SSteffen Klassert 142d8647b79SSteffen Klassert if (p->replay_window != 0) 143d8647b79SSteffen Klassert return -EINVAL; 144d8647b79SSteffen Klassert 145d8647b79SSteffen Klassert return 0; 146d8647b79SSteffen Klassert } 147df71837dSTrent Jaeger 1481da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p, 1495424f32eSThomas Graf struct nlattr **attrs) 1501da177e4SLinus Torvalds { 1511da177e4SLinus Torvalds int err; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds err = -EINVAL; 1541da177e4SLinus Torvalds switch (p->family) { 1551da177e4SLinus Torvalds case AF_INET: 156b38ff407SAnirudh Gupta break; 157b38ff407SAnirudh Gupta 158b38ff407SAnirudh Gupta case AF_INET6: 159b38ff407SAnirudh Gupta #if IS_ENABLED(CONFIG_IPV6) 160b38ff407SAnirudh Gupta break; 161b38ff407SAnirudh Gupta #else 162b38ff407SAnirudh Gupta err = -EAFNOSUPPORT; 163b38ff407SAnirudh Gupta goto out; 164b38ff407SAnirudh Gupta #endif 165b38ff407SAnirudh Gupta 166b38ff407SAnirudh Gupta default: 167b38ff407SAnirudh Gupta goto out; 168b38ff407SAnirudh Gupta } 169b38ff407SAnirudh Gupta 170b38ff407SAnirudh Gupta switch (p->sel.family) { 171b8d6d007SNicolas Dichtel case AF_UNSPEC: 172b8d6d007SNicolas Dichtel break; 173b8d6d007SNicolas Dichtel 174b38ff407SAnirudh Gupta case AF_INET: 17507bf7908SSteffen Klassert if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) 17607bf7908SSteffen Klassert goto out; 17707bf7908SSteffen Klassert 1781da177e4SLinus Torvalds break; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds case AF_INET6: 181dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 18207bf7908SSteffen Klassert if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) 18307bf7908SSteffen Klassert goto out; 18407bf7908SSteffen Klassert 1851da177e4SLinus Torvalds break; 1861da177e4SLinus Torvalds #else 1871da177e4SLinus Torvalds err = -EAFNOSUPPORT; 1881da177e4SLinus Torvalds goto out; 1891da177e4SLinus Torvalds #endif 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds default: 1921da177e4SLinus Torvalds goto out; 1933ff50b79SStephen Hemminger } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds err = -EINVAL; 1961da177e4SLinus Torvalds switch (p->id.proto) { 1971da177e4SLinus Torvalds case IPPROTO_AH: 1984447bb33SMartin Willi if ((!attrs[XFRMA_ALG_AUTH] && 1994447bb33SMartin Willi !attrs[XFRMA_ALG_AUTH_TRUNC]) || 2001a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD] || 20135a7aa08SThomas Graf attrs[XFRMA_ALG_CRYPT] || 20235d2856bSMartin Willi attrs[XFRMA_ALG_COMP] || 203a0e5ef53STobias Brunner attrs[XFRMA_TFCPAD]) 2041da177e4SLinus Torvalds goto out; 2051da177e4SLinus Torvalds break; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds case IPPROTO_ESP: 2081a6509d9SHerbert Xu if (attrs[XFRMA_ALG_COMP]) 2091a6509d9SHerbert Xu goto out; 2101a6509d9SHerbert Xu if (!attrs[XFRMA_ALG_AUTH] && 2114447bb33SMartin Willi !attrs[XFRMA_ALG_AUTH_TRUNC] && 2121a6509d9SHerbert Xu !attrs[XFRMA_ALG_CRYPT] && 2131a6509d9SHerbert Xu !attrs[XFRMA_ALG_AEAD]) 2141a6509d9SHerbert Xu goto out; 2151a6509d9SHerbert Xu if ((attrs[XFRMA_ALG_AUTH] || 2164447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 2171a6509d9SHerbert Xu attrs[XFRMA_ALG_CRYPT]) && 2181a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD]) 2191da177e4SLinus Torvalds goto out; 22035d2856bSMartin Willi if (attrs[XFRMA_TFCPAD] && 22135d2856bSMartin Willi p->mode != XFRM_MODE_TUNNEL) 22235d2856bSMartin Willi goto out; 2231da177e4SLinus Torvalds break; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds case IPPROTO_COMP: 22635a7aa08SThomas Graf if (!attrs[XFRMA_ALG_COMP] || 2271a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD] || 22835a7aa08SThomas Graf attrs[XFRMA_ALG_AUTH] || 2294447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 23035d2856bSMartin Willi attrs[XFRMA_ALG_CRYPT] || 231a0e5ef53STobias Brunner attrs[XFRMA_TFCPAD] || 232a0e5ef53STobias Brunner (ntohl(p->id.spi) >= 0x10000)) 2331da177e4SLinus Torvalds goto out; 2341da177e4SLinus Torvalds break; 2351da177e4SLinus Torvalds 236dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 237e23c7194SMasahide NAKAMURA case IPPROTO_DSTOPTS: 238e23c7194SMasahide NAKAMURA case IPPROTO_ROUTING: 23935a7aa08SThomas Graf if (attrs[XFRMA_ALG_COMP] || 24035a7aa08SThomas Graf attrs[XFRMA_ALG_AUTH] || 2414447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 2421a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD] || 24335a7aa08SThomas Graf attrs[XFRMA_ALG_CRYPT] || 24435a7aa08SThomas Graf attrs[XFRMA_ENCAP] || 24535a7aa08SThomas Graf attrs[XFRMA_SEC_CTX] || 24635d2856bSMartin Willi attrs[XFRMA_TFCPAD] || 24735a7aa08SThomas Graf !attrs[XFRMA_COADDR]) 248e23c7194SMasahide NAKAMURA goto out; 249e23c7194SMasahide NAKAMURA break; 250e23c7194SMasahide NAKAMURA #endif 251e23c7194SMasahide NAKAMURA 2521da177e4SLinus Torvalds default: 2531da177e4SLinus Torvalds goto out; 2543ff50b79SStephen Hemminger } 2551da177e4SLinus Torvalds 2561a6509d9SHerbert Xu if ((err = verify_aead(attrs))) 2571a6509d9SHerbert Xu goto out; 2584447bb33SMartin Willi if ((err = verify_auth_trunc(attrs))) 2594447bb33SMartin Willi goto out; 26035a7aa08SThomas Graf if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH))) 2611da177e4SLinus Torvalds goto out; 26235a7aa08SThomas Graf if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT))) 2631da177e4SLinus Torvalds goto out; 26435a7aa08SThomas Graf if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP))) 2651da177e4SLinus Torvalds goto out; 26635a7aa08SThomas Graf if ((err = verify_sec_ctx_len(attrs))) 267df71837dSTrent Jaeger goto out; 268d8647b79SSteffen Klassert if ((err = verify_replay(p, attrs))) 269d8647b79SSteffen Klassert goto out; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds err = -EINVAL; 2721da177e4SLinus Torvalds switch (p->mode) { 2737e49e6deSMasahide NAKAMURA case XFRM_MODE_TRANSPORT: 2747e49e6deSMasahide NAKAMURA case XFRM_MODE_TUNNEL: 275060f02a3SNoriaki TAKAMIYA case XFRM_MODE_ROUTEOPTIMIZATION: 2760a69452cSDiego Beltrami case XFRM_MODE_BEET: 2771da177e4SLinus Torvalds break; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds default: 2801da177e4SLinus Torvalds goto out; 2813ff50b79SStephen Hemminger } 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds err = 0; 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds out: 2861da177e4SLinus Torvalds return err; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props, 2906f2f19edSDavid S. Miller struct xfrm_algo_desc *(*get_byname)(const char *, int), 2915424f32eSThomas Graf struct nlattr *rta) 2921da177e4SLinus Torvalds { 2931da177e4SLinus Torvalds struct xfrm_algo *p, *ualg; 2941da177e4SLinus Torvalds struct xfrm_algo_desc *algo; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds if (!rta) 2971da177e4SLinus Torvalds return 0; 2981da177e4SLinus Torvalds 2995424f32eSThomas Graf ualg = nla_data(rta); 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds algo = get_byname(ualg->alg_name, 1); 3021da177e4SLinus Torvalds if (!algo) 3031da177e4SLinus Torvalds return -ENOSYS; 3041da177e4SLinus Torvalds *props = algo->desc.sadb_alg_id; 3051da177e4SLinus Torvalds 3060f99be0dSEric Dumazet p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL); 3071da177e4SLinus Torvalds if (!p) 3081da177e4SLinus Torvalds return -ENOMEM; 3091da177e4SLinus Torvalds 31004ff1260SHerbert Xu strcpy(p->alg_name, algo->name); 3111da177e4SLinus Torvalds *algpp = p; 3121da177e4SLinus Torvalds return 0; 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 31569b0137fSHerbert Xu static int attach_crypt(struct xfrm_state *x, struct nlattr *rta) 31669b0137fSHerbert Xu { 31769b0137fSHerbert Xu struct xfrm_algo *p, *ualg; 31869b0137fSHerbert Xu struct xfrm_algo_desc *algo; 31969b0137fSHerbert Xu 32069b0137fSHerbert Xu if (!rta) 32169b0137fSHerbert Xu return 0; 32269b0137fSHerbert Xu 32369b0137fSHerbert Xu ualg = nla_data(rta); 32469b0137fSHerbert Xu 32569b0137fSHerbert Xu algo = xfrm_ealg_get_byname(ualg->alg_name, 1); 32669b0137fSHerbert Xu if (!algo) 32769b0137fSHerbert Xu return -ENOSYS; 32869b0137fSHerbert Xu x->props.ealgo = algo->desc.sadb_alg_id; 32969b0137fSHerbert Xu 33069b0137fSHerbert Xu p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL); 33169b0137fSHerbert Xu if (!p) 33269b0137fSHerbert Xu return -ENOMEM; 33369b0137fSHerbert Xu 33469b0137fSHerbert Xu strcpy(p->alg_name, algo->name); 33569b0137fSHerbert Xu x->ealg = p; 33669b0137fSHerbert Xu x->geniv = algo->uinfo.encr.geniv; 33769b0137fSHerbert Xu return 0; 33869b0137fSHerbert Xu } 33969b0137fSHerbert Xu 3404447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props, 3414447bb33SMartin Willi struct nlattr *rta) 3424447bb33SMartin Willi { 3434447bb33SMartin Willi struct xfrm_algo *ualg; 3444447bb33SMartin Willi struct xfrm_algo_auth *p; 3454447bb33SMartin Willi struct xfrm_algo_desc *algo; 3464447bb33SMartin Willi 3474447bb33SMartin Willi if (!rta) 3484447bb33SMartin Willi return 0; 3494447bb33SMartin Willi 3504447bb33SMartin Willi ualg = nla_data(rta); 3514447bb33SMartin Willi 3524447bb33SMartin Willi algo = xfrm_aalg_get_byname(ualg->alg_name, 1); 3534447bb33SMartin Willi if (!algo) 3544447bb33SMartin Willi return -ENOSYS; 3554447bb33SMartin Willi *props = algo->desc.sadb_alg_id; 3564447bb33SMartin Willi 3574447bb33SMartin Willi p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL); 3584447bb33SMartin Willi if (!p) 3594447bb33SMartin Willi return -ENOMEM; 3604447bb33SMartin Willi 3614447bb33SMartin Willi strcpy(p->alg_name, algo->name); 3624447bb33SMartin Willi p->alg_key_len = ualg->alg_key_len; 3634447bb33SMartin Willi p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; 3644447bb33SMartin Willi memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8); 3654447bb33SMartin Willi 3664447bb33SMartin Willi *algpp = p; 3674447bb33SMartin Willi return 0; 3684447bb33SMartin Willi } 3694447bb33SMartin Willi 3704447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props, 3714447bb33SMartin Willi struct nlattr *rta) 3724447bb33SMartin Willi { 3734447bb33SMartin Willi struct xfrm_algo_auth *p, *ualg; 3744447bb33SMartin Willi struct xfrm_algo_desc *algo; 3754447bb33SMartin Willi 3764447bb33SMartin Willi if (!rta) 3774447bb33SMartin Willi return 0; 3784447bb33SMartin Willi 3794447bb33SMartin Willi ualg = nla_data(rta); 3804447bb33SMartin Willi 3814447bb33SMartin Willi algo = xfrm_aalg_get_byname(ualg->alg_name, 1); 3824447bb33SMartin Willi if (!algo) 3834447bb33SMartin Willi return -ENOSYS; 384689f1c9dSHerbert Xu if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) 3854447bb33SMartin Willi return -EINVAL; 3864447bb33SMartin Willi *props = algo->desc.sadb_alg_id; 3874447bb33SMartin Willi 3884447bb33SMartin Willi p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL); 3894447bb33SMartin Willi if (!p) 3904447bb33SMartin Willi return -ENOMEM; 3914447bb33SMartin Willi 3924447bb33SMartin Willi strcpy(p->alg_name, algo->name); 3934447bb33SMartin Willi if (!p->alg_trunc_len) 3944447bb33SMartin Willi p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; 3954447bb33SMartin Willi 3964447bb33SMartin Willi *algpp = p; 3974447bb33SMartin Willi return 0; 3984447bb33SMartin Willi } 3994447bb33SMartin Willi 40069b0137fSHerbert Xu static int attach_aead(struct xfrm_state *x, struct nlattr *rta) 4011a6509d9SHerbert Xu { 4021a6509d9SHerbert Xu struct xfrm_algo_aead *p, *ualg; 4031a6509d9SHerbert Xu struct xfrm_algo_desc *algo; 4041a6509d9SHerbert Xu 4051a6509d9SHerbert Xu if (!rta) 4061a6509d9SHerbert Xu return 0; 4071a6509d9SHerbert Xu 4081a6509d9SHerbert Xu ualg = nla_data(rta); 4091a6509d9SHerbert Xu 4101a6509d9SHerbert Xu algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1); 4111a6509d9SHerbert Xu if (!algo) 4121a6509d9SHerbert Xu return -ENOSYS; 41369b0137fSHerbert Xu x->props.ealgo = algo->desc.sadb_alg_id; 4141a6509d9SHerbert Xu 4151a6509d9SHerbert Xu p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL); 4161a6509d9SHerbert Xu if (!p) 4171a6509d9SHerbert Xu return -ENOMEM; 4181a6509d9SHerbert Xu 4191a6509d9SHerbert Xu strcpy(p->alg_name, algo->name); 42069b0137fSHerbert Xu x->aead = p; 42169b0137fSHerbert Xu x->geniv = algo->uinfo.aead.geniv; 4221a6509d9SHerbert Xu return 0; 4231a6509d9SHerbert Xu } 4241a6509d9SHerbert Xu 425e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn, 426e2b19125SSteffen Klassert struct nlattr *rp) 427e2b19125SSteffen Klassert { 428e2b19125SSteffen Klassert struct xfrm_replay_state_esn *up; 4295e708e47SAlexey Dobriyan unsigned int ulen; 430e2b19125SSteffen Klassert 431e2b19125SSteffen Klassert if (!replay_esn || !rp) 432e2b19125SSteffen Klassert return 0; 433e2b19125SSteffen Klassert 434e2b19125SSteffen Klassert up = nla_data(rp); 435ecd79187SMathias Krause ulen = xfrm_replay_state_esn_len(up); 436e2b19125SSteffen Klassert 437f843ee6dSAndy Whitcroft /* Check the overall length and the internal bitmap length to avoid 438f843ee6dSAndy Whitcroft * potential overflow. */ 4395e708e47SAlexey Dobriyan if (nla_len(rp) < (int)ulen || 440f843ee6dSAndy Whitcroft xfrm_replay_state_esn_len(replay_esn) != ulen || 441f843ee6dSAndy Whitcroft replay_esn->bmp_len != up->bmp_len) 442e2b19125SSteffen Klassert return -EINVAL; 443e2b19125SSteffen Klassert 444677e806dSAndy Whitcroft if (up->replay_window > up->bmp_len * sizeof(__u32) * 8) 445677e806dSAndy Whitcroft return -EINVAL; 446677e806dSAndy Whitcroft 447e2b19125SSteffen Klassert return 0; 448e2b19125SSteffen Klassert } 449e2b19125SSteffen Klassert 450d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn, 451d8647b79SSteffen Klassert struct xfrm_replay_state_esn **preplay_esn, 452d8647b79SSteffen Klassert struct nlattr *rta) 453d8647b79SSteffen Klassert { 454d8647b79SSteffen Klassert struct xfrm_replay_state_esn *p, *pp, *up; 4555e708e47SAlexey Dobriyan unsigned int klen, ulen; 456d8647b79SSteffen Klassert 457d8647b79SSteffen Klassert if (!rta) 458d8647b79SSteffen Klassert return 0; 459d8647b79SSteffen Klassert 460d8647b79SSteffen Klassert up = nla_data(rta); 461ecd79187SMathias Krause klen = xfrm_replay_state_esn_len(up); 4625e708e47SAlexey Dobriyan ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up); 463d8647b79SSteffen Klassert 464ecd79187SMathias Krause p = kzalloc(klen, GFP_KERNEL); 465d8647b79SSteffen Klassert if (!p) 466d8647b79SSteffen Klassert return -ENOMEM; 467d8647b79SSteffen Klassert 468ecd79187SMathias Krause pp = kzalloc(klen, GFP_KERNEL); 469d8647b79SSteffen Klassert if (!pp) { 470d8647b79SSteffen Klassert kfree(p); 471d8647b79SSteffen Klassert return -ENOMEM; 472d8647b79SSteffen Klassert } 473d8647b79SSteffen Klassert 474ecd79187SMathias Krause memcpy(p, up, ulen); 475ecd79187SMathias Krause memcpy(pp, up, ulen); 476ecd79187SMathias Krause 477d8647b79SSteffen Klassert *replay_esn = p; 478d8647b79SSteffen Klassert *preplay_esn = pp; 479d8647b79SSteffen Klassert 480d8647b79SSteffen Klassert return 0; 481d8647b79SSteffen Klassert } 482d8647b79SSteffen Klassert 483a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx) 484df71837dSTrent Jaeger { 485a1b831f2SAlexey Dobriyan unsigned int len = 0; 486df71837dSTrent Jaeger 487df71837dSTrent Jaeger if (xfrm_ctx) { 488df71837dSTrent Jaeger len += sizeof(struct xfrm_user_sec_ctx); 489df71837dSTrent Jaeger len += xfrm_ctx->ctx_len; 490df71837dSTrent Jaeger } 491df71837dSTrent Jaeger return len; 492df71837dSTrent Jaeger } 493df71837dSTrent Jaeger 4941da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 4951da177e4SLinus Torvalds { 4961da177e4SLinus Torvalds memcpy(&x->id, &p->id, sizeof(x->id)); 4971da177e4SLinus Torvalds memcpy(&x->sel, &p->sel, sizeof(x->sel)); 4981da177e4SLinus Torvalds memcpy(&x->lft, &p->lft, sizeof(x->lft)); 4991da177e4SLinus Torvalds x->props.mode = p->mode; 50033fce60dSFan Du x->props.replay_window = min_t(unsigned int, p->replay_window, 50133fce60dSFan Du sizeof(x->replay.bitmap) * 8); 5021da177e4SLinus Torvalds x->props.reqid = p->reqid; 5031da177e4SLinus Torvalds x->props.family = p->family; 50454489c14SDavid S. Miller memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr)); 5051da177e4SLinus Torvalds x->props.flags = p->flags; 506196b0036SHerbert Xu 507ccf9b3b8SSteffen Klassert if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC)) 508196b0036SHerbert Xu x->sel.family = p->family; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 511d51d081dSJamal Hadi Salim /* 512d51d081dSJamal Hadi Salim * someday when pfkey also has support, we could have the code 513d51d081dSJamal Hadi Salim * somehow made shareable and move it to xfrm_state.c - JHS 514d51d081dSJamal Hadi Salim * 515d51d081dSJamal Hadi Salim */ 516e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs, 517e3ac104dSMathias Krause int update_esn) 518d51d081dSJamal Hadi Salim { 5195424f32eSThomas Graf struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 520e3ac104dSMathias Krause struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL; 5215424f32eSThomas Graf struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 5225424f32eSThomas Graf struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; 5235424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; 524d51d081dSJamal Hadi Salim 525d8647b79SSteffen Klassert if (re) { 526d8647b79SSteffen Klassert struct xfrm_replay_state_esn *replay_esn; 527d8647b79SSteffen Klassert replay_esn = nla_data(re); 528d8647b79SSteffen Klassert memcpy(x->replay_esn, replay_esn, 529d8647b79SSteffen Klassert xfrm_replay_state_esn_len(replay_esn)); 530d8647b79SSteffen Klassert memcpy(x->preplay_esn, replay_esn, 531d8647b79SSteffen Klassert xfrm_replay_state_esn_len(replay_esn)); 532d8647b79SSteffen Klassert } 533d8647b79SSteffen Klassert 534d51d081dSJamal Hadi Salim if (rp) { 535d51d081dSJamal Hadi Salim struct xfrm_replay_state *replay; 5365424f32eSThomas Graf replay = nla_data(rp); 537d51d081dSJamal Hadi Salim memcpy(&x->replay, replay, sizeof(*replay)); 538d51d081dSJamal Hadi Salim memcpy(&x->preplay, replay, sizeof(*replay)); 539d51d081dSJamal Hadi Salim } 540d51d081dSJamal Hadi Salim 541d51d081dSJamal Hadi Salim if (lt) { 542d51d081dSJamal Hadi Salim struct xfrm_lifetime_cur *ltime; 5435424f32eSThomas Graf ltime = nla_data(lt); 544d51d081dSJamal Hadi Salim x->curlft.bytes = ltime->bytes; 545d51d081dSJamal Hadi Salim x->curlft.packets = ltime->packets; 546d51d081dSJamal Hadi Salim x->curlft.add_time = ltime->add_time; 547d51d081dSJamal Hadi Salim x->curlft.use_time = ltime->use_time; 548d51d081dSJamal Hadi Salim } 549d51d081dSJamal Hadi Salim 550cf5cb79fSThomas Graf if (et) 5515424f32eSThomas Graf x->replay_maxage = nla_get_u32(et); 552d51d081dSJamal Hadi Salim 553cf5cb79fSThomas Graf if (rt) 5545424f32eSThomas Graf x->replay_maxdiff = nla_get_u32(rt); 555d51d081dSJamal Hadi Salim } 556d51d081dSJamal Hadi Salim 5579b42c1f1SSteffen Klassert static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m) 5589b42c1f1SSteffen Klassert { 5599b42c1f1SSteffen Klassert if (attrs[XFRMA_SET_MARK]) { 5609b42c1f1SSteffen Klassert m->v = nla_get_u32(attrs[XFRMA_SET_MARK]); 5619b42c1f1SSteffen Klassert if (attrs[XFRMA_SET_MARK_MASK]) 5629b42c1f1SSteffen Klassert m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]); 5639b42c1f1SSteffen Klassert else 5649b42c1f1SSteffen Klassert m->m = 0xffffffff; 5659b42c1f1SSteffen Klassert } else { 5669b42c1f1SSteffen Klassert m->v = m->m = 0; 5679b42c1f1SSteffen Klassert } 5689b42c1f1SSteffen Klassert } 5699b42c1f1SSteffen Klassert 570fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net, 571fc34acd3SAlexey Dobriyan struct xfrm_usersa_info *p, 5725424f32eSThomas Graf struct nlattr **attrs, 5731da177e4SLinus Torvalds int *errp) 5741da177e4SLinus Torvalds { 575fc34acd3SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 5761da177e4SLinus Torvalds int err = -ENOMEM; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds if (!x) 5791da177e4SLinus Torvalds goto error_no_put; 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds copy_from_user_state(x, p); 5821da177e4SLinus Torvalds 5836fd06963SSteffen Klassert if (attrs[XFRMA_ENCAP]) { 5846fd06963SSteffen Klassert x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), 5856fd06963SSteffen Klassert sizeof(*x->encap), GFP_KERNEL); 5866fd06963SSteffen Klassert if (x->encap == NULL) 5876fd06963SSteffen Klassert goto error; 5886fd06963SSteffen Klassert } 5896fd06963SSteffen Klassert 5906fd06963SSteffen Klassert if (attrs[XFRMA_COADDR]) { 5916fd06963SSteffen Klassert x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]), 5926fd06963SSteffen Klassert sizeof(*x->coaddr), GFP_KERNEL); 5936fd06963SSteffen Klassert if (x->coaddr == NULL) 5946fd06963SSteffen Klassert goto error; 5956fd06963SSteffen Klassert } 5966fd06963SSteffen Klassert 597a947b0a9SNicolas Dichtel if (attrs[XFRMA_SA_EXTRA_FLAGS]) 598a947b0a9SNicolas Dichtel x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]); 599a947b0a9SNicolas Dichtel 60069b0137fSHerbert Xu if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD]))) 6011a6509d9SHerbert Xu goto error; 6024447bb33SMartin Willi if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo, 6034447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC]))) 6044447bb33SMartin Willi goto error; 6054447bb33SMartin Willi if (!x->props.aalgo) { 6064447bb33SMartin Willi if ((err = attach_auth(&x->aalg, &x->props.aalgo, 60735a7aa08SThomas Graf attrs[XFRMA_ALG_AUTH]))) 6081da177e4SLinus Torvalds goto error; 6094447bb33SMartin Willi } 61069b0137fSHerbert Xu if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT]))) 6111da177e4SLinus Torvalds goto error; 6121da177e4SLinus Torvalds if ((err = attach_one_algo(&x->calg, &x->props.calgo, 6131da177e4SLinus Torvalds xfrm_calg_get_byname, 61435a7aa08SThomas Graf attrs[XFRMA_ALG_COMP]))) 6151da177e4SLinus Torvalds goto error; 616fd21150aSThomas Graf 61735d2856bSMartin Willi if (attrs[XFRMA_TFCPAD]) 61835d2856bSMartin Willi x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]); 61935d2856bSMartin Willi 6206f26b61eSJamal Hadi Salim xfrm_mark_get(attrs, &x->mark); 6216f26b61eSJamal Hadi Salim 6229b42c1f1SSteffen Klassert xfrm_smark_init(attrs, &x->props.smark); 623077fbac4SLorenzo Colitti 6247e652640SSteffen Klassert if (attrs[XFRMA_IF_ID]) 6257e652640SSteffen Klassert x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 626fd21150aSThomas Graf 627ffdb5211SIlan Tayari err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]); 6281da177e4SLinus Torvalds if (err) 6291da177e4SLinus Torvalds goto error; 6301da177e4SLinus Torvalds 6312f30ea50SMathias Krause if (attrs[XFRMA_SEC_CTX]) { 6322f30ea50SMathias Krause err = security_xfrm_state_alloc(x, 6332f30ea50SMathias Krause nla_data(attrs[XFRMA_SEC_CTX])); 6342f30ea50SMathias Krause if (err) 635df71837dSTrent Jaeger goto error; 6362f30ea50SMathias Krause } 637df71837dSTrent Jaeger 638d8647b79SSteffen Klassert if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn, 639d8647b79SSteffen Klassert attrs[XFRMA_REPLAY_ESN_VAL]))) 640d8647b79SSteffen Klassert goto error; 641d8647b79SSteffen Klassert 6421da177e4SLinus Torvalds x->km.seq = p->seq; 643b27aeadbSAlexey Dobriyan x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth; 644d51d081dSJamal Hadi Salim /* sysctl_xfrm_aevent_etime is in 100ms units */ 645b27aeadbSAlexey Dobriyan x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M; 646d51d081dSJamal Hadi Salim 6479fdc4883SSteffen Klassert if ((err = xfrm_init_replay(x))) 6489fdc4883SSteffen Klassert goto error; 649d51d081dSJamal Hadi Salim 6509fdc4883SSteffen Klassert /* override default values from above */ 651e3ac104dSMathias Krause xfrm_update_ae_params(x, attrs, 0); 6521da177e4SLinus Torvalds 653cc01572eSYossi Kuperman /* configure the hardware if offload is requested */ 654cc01572eSYossi Kuperman if (attrs[XFRMA_OFFLOAD_DEV]) { 655cc01572eSYossi Kuperman err = xfrm_dev_state_add(net, x, 656cc01572eSYossi Kuperman nla_data(attrs[XFRMA_OFFLOAD_DEV])); 657cc01572eSYossi Kuperman if (err) 658cc01572eSYossi Kuperman goto error; 659cc01572eSYossi Kuperman } 660cc01572eSYossi Kuperman 6611da177e4SLinus Torvalds return x; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds error: 6641da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 6651da177e4SLinus Torvalds xfrm_state_put(x); 6661da177e4SLinus Torvalds error_no_put: 6671da177e4SLinus Torvalds *errp = err; 6681da177e4SLinus Torvalds return NULL; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 67122e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 6725424f32eSThomas Graf struct nlattr **attrs) 6731da177e4SLinus Torvalds { 674fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 6757b67c857SThomas Graf struct xfrm_usersa_info *p = nlmsg_data(nlh); 6761da177e4SLinus Torvalds struct xfrm_state *x; 6771da177e4SLinus Torvalds int err; 67826b15dadSJamal Hadi Salim struct km_event c; 6791da177e4SLinus Torvalds 68035a7aa08SThomas Graf err = verify_newsa_info(p, attrs); 6811da177e4SLinus Torvalds if (err) 6821da177e4SLinus Torvalds return err; 6831da177e4SLinus Torvalds 684fc34acd3SAlexey Dobriyan x = xfrm_state_construct(net, p, attrs, &err); 6851da177e4SLinus Torvalds if (!x) 6861da177e4SLinus Torvalds return err; 6871da177e4SLinus Torvalds 68826b15dadSJamal Hadi Salim xfrm_state_hold(x); 6891da177e4SLinus Torvalds if (nlh->nlmsg_type == XFRM_MSG_NEWSA) 6901da177e4SLinus Torvalds err = xfrm_state_add(x); 6911da177e4SLinus Torvalds else 6921da177e4SLinus Torvalds err = xfrm_state_update(x); 6931da177e4SLinus Torvalds 6942e71029eSTetsuo Handa xfrm_audit_state_add(x, err ? 0 : 1, true); 695161a09e7SJoy Latten 6961da177e4SLinus Torvalds if (err < 0) { 6971da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 698c5d4d7d8SSteffen Klassert xfrm_dev_state_delete(x); 69921380b81SHerbert Xu __xfrm_state_put(x); 7007d6dfe1fSPatrick McHardy goto out; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 703cc01572eSYossi Kuperman if (x->km.state == XFRM_STATE_VOID) 704cc01572eSYossi Kuperman x->km.state = XFRM_STATE_VALID; 705cc01572eSYossi Kuperman 70626b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 70715e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 708f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 70926b15dadSJamal Hadi Salim 71026b15dadSJamal Hadi Salim km_state_notify(x, &c); 7117d6dfe1fSPatrick McHardy out: 71226b15dadSJamal Hadi Salim xfrm_state_put(x); 7131da177e4SLinus Torvalds return err; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 716fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net, 717fc34acd3SAlexey Dobriyan struct xfrm_usersa_id *p, 7185424f32eSThomas Graf struct nlattr **attrs, 719eb2971b6SMasahide NAKAMURA int *errp) 720eb2971b6SMasahide NAKAMURA { 721eb2971b6SMasahide NAKAMURA struct xfrm_state *x = NULL; 7226f26b61eSJamal Hadi Salim struct xfrm_mark m; 723eb2971b6SMasahide NAKAMURA int err; 7246f26b61eSJamal Hadi Salim u32 mark = xfrm_mark_get(attrs, &m); 725eb2971b6SMasahide NAKAMURA 726eb2971b6SMasahide NAKAMURA if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { 727eb2971b6SMasahide NAKAMURA err = -ESRCH; 7286f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family); 729eb2971b6SMasahide NAKAMURA } else { 730eb2971b6SMasahide NAKAMURA xfrm_address_t *saddr = NULL; 731eb2971b6SMasahide NAKAMURA 73235a7aa08SThomas Graf verify_one_addr(attrs, XFRMA_SRCADDR, &saddr); 733eb2971b6SMasahide NAKAMURA if (!saddr) { 734eb2971b6SMasahide NAKAMURA err = -EINVAL; 735eb2971b6SMasahide NAKAMURA goto out; 736eb2971b6SMasahide NAKAMURA } 737eb2971b6SMasahide NAKAMURA 7389abbffeeSMasahide NAKAMURA err = -ESRCH; 7396f26b61eSJamal Hadi Salim x = xfrm_state_lookup_byaddr(net, mark, 7406f26b61eSJamal Hadi Salim &p->daddr, saddr, 741221df1edSAlexey Dobriyan p->proto, p->family); 742eb2971b6SMasahide NAKAMURA } 743eb2971b6SMasahide NAKAMURA 744eb2971b6SMasahide NAKAMURA out: 745eb2971b6SMasahide NAKAMURA if (!x && errp) 746eb2971b6SMasahide NAKAMURA *errp = err; 747eb2971b6SMasahide NAKAMURA return x; 748eb2971b6SMasahide NAKAMURA } 749eb2971b6SMasahide NAKAMURA 75022e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 7515424f32eSThomas Graf struct nlattr **attrs) 7521da177e4SLinus Torvalds { 753fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 7541da177e4SLinus Torvalds struct xfrm_state *x; 755eb2971b6SMasahide NAKAMURA int err = -ESRCH; 75626b15dadSJamal Hadi Salim struct km_event c; 7577b67c857SThomas Graf struct xfrm_usersa_id *p = nlmsg_data(nlh); 7581da177e4SLinus Torvalds 759fc34acd3SAlexey Dobriyan x = xfrm_user_state_lookup(net, p, attrs, &err); 7601da177e4SLinus Torvalds if (x == NULL) 761eb2971b6SMasahide NAKAMURA return err; 7621da177e4SLinus Torvalds 7636f68dc37SDavid S. Miller if ((err = security_xfrm_state_delete(x)) != 0) 764c8c05a8eSCatherine Zhang goto out; 765c8c05a8eSCatherine Zhang 7661da177e4SLinus Torvalds if (xfrm_state_kern(x)) { 767c8c05a8eSCatherine Zhang err = -EPERM; 768c8c05a8eSCatherine Zhang goto out; 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds 77126b15dadSJamal Hadi Salim err = xfrm_state_delete(x); 772161a09e7SJoy Latten 773c8c05a8eSCatherine Zhang if (err < 0) 774c8c05a8eSCatherine Zhang goto out; 77526b15dadSJamal Hadi Salim 77626b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 77715e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 778f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 77926b15dadSJamal Hadi Salim km_state_notify(x, &c); 7801da177e4SLinus Torvalds 781c8c05a8eSCatherine Zhang out: 7822e71029eSTetsuo Handa xfrm_audit_state_delete(x, err ? 0 : 1, true); 783c8c05a8eSCatherine Zhang xfrm_state_put(x); 78426b15dadSJamal Hadi Salim return err; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 7881da177e4SLinus Torvalds { 789f778a636SMathias Krause memset(p, 0, sizeof(*p)); 7901da177e4SLinus Torvalds memcpy(&p->id, &x->id, sizeof(p->id)); 7911da177e4SLinus Torvalds memcpy(&p->sel, &x->sel, sizeof(p->sel)); 7921da177e4SLinus Torvalds memcpy(&p->lft, &x->lft, sizeof(p->lft)); 7931da177e4SLinus Torvalds memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); 794e33d4f13SSowmini Varadhan put_unaligned(x->stats.replay_window, &p->stats.replay_window); 795e33d4f13SSowmini Varadhan put_unaligned(x->stats.replay, &p->stats.replay); 796e33d4f13SSowmini Varadhan put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed); 79754489c14SDavid S. Miller memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); 7981da177e4SLinus Torvalds p->mode = x->props.mode; 7991da177e4SLinus Torvalds p->replay_window = x->props.replay_window; 8001da177e4SLinus Torvalds p->reqid = x->props.reqid; 8011da177e4SLinus Torvalds p->family = x->props.family; 8021da177e4SLinus Torvalds p->flags = x->props.flags; 8031da177e4SLinus Torvalds p->seq = x->km.seq; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds struct xfrm_dump_info { 8071da177e4SLinus Torvalds struct sk_buff *in_skb; 8081da177e4SLinus Torvalds struct sk_buff *out_skb; 8091da177e4SLinus Torvalds u32 nlmsg_seq; 8101da177e4SLinus Torvalds u16 nlmsg_flags; 8111da177e4SLinus Torvalds }; 8121da177e4SLinus Torvalds 813c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) 814c0144beaSThomas Graf { 815c0144beaSThomas Graf struct xfrm_user_sec_ctx *uctx; 816c0144beaSThomas Graf struct nlattr *attr; 81768325d3bSHerbert Xu int ctx_size = sizeof(*uctx) + s->ctx_len; 818c0144beaSThomas Graf 819c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size); 820c0144beaSThomas Graf if (attr == NULL) 821c0144beaSThomas Graf return -EMSGSIZE; 822c0144beaSThomas Graf 823c0144beaSThomas Graf uctx = nla_data(attr); 824c0144beaSThomas Graf uctx->exttype = XFRMA_SEC_CTX; 825c0144beaSThomas Graf uctx->len = ctx_size; 826c0144beaSThomas Graf uctx->ctx_doi = s->ctx_doi; 827c0144beaSThomas Graf uctx->ctx_alg = s->ctx_alg; 828c0144beaSThomas Graf uctx->ctx_len = s->ctx_len; 829c0144beaSThomas Graf memcpy(uctx + 1, s->ctx_str, s->ctx_len); 830c0144beaSThomas Graf 831c0144beaSThomas Graf return 0; 832c0144beaSThomas Graf } 833c0144beaSThomas Graf 834d77e38e6SSteffen Klassert static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb) 835d77e38e6SSteffen Klassert { 836d77e38e6SSteffen Klassert struct xfrm_user_offload *xuo; 837d77e38e6SSteffen Klassert struct nlattr *attr; 838d77e38e6SSteffen Klassert 839d77e38e6SSteffen Klassert attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo)); 840d77e38e6SSteffen Klassert if (attr == NULL) 841d77e38e6SSteffen Klassert return -EMSGSIZE; 842d77e38e6SSteffen Klassert 843d77e38e6SSteffen Klassert xuo = nla_data(attr); 8445fe0d4bdSMathias Krause memset(xuo, 0, sizeof(*xuo)); 845d77e38e6SSteffen Klassert xuo->ifindex = xso->dev->ifindex; 846d77e38e6SSteffen Klassert xuo->flags = xso->flags; 847d77e38e6SSteffen Klassert 848d77e38e6SSteffen Klassert return 0; 849d77e38e6SSteffen Klassert } 850d77e38e6SSteffen Klassert 851c7a5899eSAntony Antony static bool xfrm_redact(void) 852c7a5899eSAntony Antony { 853c7a5899eSAntony Antony return IS_ENABLED(CONFIG_SECURITY) && 854c7a5899eSAntony Antony security_locked_down(LOCKDOWN_XFRM_SECRET); 855c7a5899eSAntony Antony } 856c7a5899eSAntony Antony 8574447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb) 8584447bb33SMartin Willi { 8594447bb33SMartin Willi struct xfrm_algo *algo; 860c7a5899eSAntony Antony struct xfrm_algo_auth *ap; 8614447bb33SMartin Willi struct nlattr *nla; 862c7a5899eSAntony Antony bool redact_secret = xfrm_redact(); 8634447bb33SMartin Willi 8644447bb33SMartin Willi nla = nla_reserve(skb, XFRMA_ALG_AUTH, 8654447bb33SMartin Willi sizeof(*algo) + (auth->alg_key_len + 7) / 8); 8664447bb33SMartin Willi if (!nla) 8674447bb33SMartin Willi return -EMSGSIZE; 8684447bb33SMartin Willi algo = nla_data(nla); 8694c87308bSMathias Krause strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name)); 870c7a5899eSAntony Antony 871c7a5899eSAntony Antony if (redact_secret && auth->alg_key_len) 872c7a5899eSAntony Antony memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8); 873c7a5899eSAntony Antony else 874c7a5899eSAntony Antony memcpy(algo->alg_key, auth->alg_key, 875c7a5899eSAntony Antony (auth->alg_key_len + 7) / 8); 8764447bb33SMartin Willi algo->alg_key_len = auth->alg_key_len; 8774447bb33SMartin Willi 878c7a5899eSAntony Antony nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth)); 879c7a5899eSAntony Antony if (!nla) 880c7a5899eSAntony Antony return -EMSGSIZE; 881c7a5899eSAntony Antony ap = nla_data(nla); 882c7a5899eSAntony Antony memcpy(ap, auth, sizeof(struct xfrm_algo_auth)); 883c7a5899eSAntony Antony if (redact_secret && auth->alg_key_len) 884c7a5899eSAntony Antony memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8); 885c7a5899eSAntony Antony else 886c7a5899eSAntony Antony memcpy(ap->alg_key, auth->alg_key, 887c7a5899eSAntony Antony (auth->alg_key_len + 7) / 8); 888c7a5899eSAntony Antony return 0; 889c7a5899eSAntony Antony } 890c7a5899eSAntony Antony 891c7a5899eSAntony Antony static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb) 892c7a5899eSAntony Antony { 893c7a5899eSAntony Antony struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead)); 894c7a5899eSAntony Antony struct xfrm_algo_aead *ap; 895c7a5899eSAntony Antony bool redact_secret = xfrm_redact(); 896c7a5899eSAntony Antony 897c7a5899eSAntony Antony if (!nla) 898c7a5899eSAntony Antony return -EMSGSIZE; 899c7a5899eSAntony Antony 900c7a5899eSAntony Antony ap = nla_data(nla); 901c7a5899eSAntony Antony memcpy(ap, aead, sizeof(*aead)); 902c7a5899eSAntony Antony 903c7a5899eSAntony Antony if (redact_secret && aead->alg_key_len) 904c7a5899eSAntony Antony memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8); 905c7a5899eSAntony Antony else 906c7a5899eSAntony Antony memcpy(ap->alg_key, aead->alg_key, 907c7a5899eSAntony Antony (aead->alg_key_len + 7) / 8); 908c7a5899eSAntony Antony return 0; 909c7a5899eSAntony Antony } 910c7a5899eSAntony Antony 911c7a5899eSAntony Antony static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb) 912c7a5899eSAntony Antony { 913c7a5899eSAntony Antony struct xfrm_algo *ap; 914c7a5899eSAntony Antony bool redact_secret = xfrm_redact(); 915c7a5899eSAntony Antony struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT, 916c7a5899eSAntony Antony xfrm_alg_len(ealg)); 917c7a5899eSAntony Antony if (!nla) 918c7a5899eSAntony Antony return -EMSGSIZE; 919c7a5899eSAntony Antony 920c7a5899eSAntony Antony ap = nla_data(nla); 921c7a5899eSAntony Antony memcpy(ap, ealg, sizeof(*ealg)); 922c7a5899eSAntony Antony 923c7a5899eSAntony Antony if (redact_secret && ealg->alg_key_len) 924c7a5899eSAntony Antony memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8); 925c7a5899eSAntony Antony else 926c7a5899eSAntony Antony memcpy(ap->alg_key, ealg->alg_key, 927c7a5899eSAntony Antony (ealg->alg_key_len + 7) / 8); 928c7a5899eSAntony Antony 9294447bb33SMartin Willi return 0; 9304447bb33SMartin Willi } 9314447bb33SMartin Willi 9329b42c1f1SSteffen Klassert static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m) 9339b42c1f1SSteffen Klassert { 9349b42c1f1SSteffen Klassert int ret = 0; 9359b42c1f1SSteffen Klassert 9369b42c1f1SSteffen Klassert if (m->v | m->m) { 9379b42c1f1SSteffen Klassert ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v); 9389b42c1f1SSteffen Klassert if (!ret) 9399b42c1f1SSteffen Klassert ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m); 9409b42c1f1SSteffen Klassert } 9419b42c1f1SSteffen Klassert return ret; 9429b42c1f1SSteffen Klassert } 9439b42c1f1SSteffen Klassert 94468325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */ 94568325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x, 94668325d3bSHerbert Xu struct xfrm_usersa_info *p, 94768325d3bSHerbert Xu struct sk_buff *skb) 9481da177e4SLinus Torvalds { 9491d1e34ddSDavid S. Miller int ret = 0; 9501d1e34ddSDavid S. Miller 9511da177e4SLinus Torvalds copy_to_user_state(x, p); 9521da177e4SLinus Torvalds 953a947b0a9SNicolas Dichtel if (x->props.extra_flags) { 954a947b0a9SNicolas Dichtel ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS, 955a947b0a9SNicolas Dichtel x->props.extra_flags); 956a947b0a9SNicolas Dichtel if (ret) 957a947b0a9SNicolas Dichtel goto out; 958a947b0a9SNicolas Dichtel } 959a947b0a9SNicolas Dichtel 9601d1e34ddSDavid S. Miller if (x->coaddr) { 9611d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); 9621d1e34ddSDavid S. Miller if (ret) 9631d1e34ddSDavid S. Miller goto out; 9641d1e34ddSDavid S. Miller } 9651d1e34ddSDavid S. Miller if (x->lastused) { 966de95c4a4SNicolas Dichtel ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused, 967de95c4a4SNicolas Dichtel XFRMA_PAD); 9681d1e34ddSDavid S. Miller if (ret) 9691d1e34ddSDavid S. Miller goto out; 9701d1e34ddSDavid S. Miller } 9711d1e34ddSDavid S. Miller if (x->aead) { 972c7a5899eSAntony Antony ret = copy_to_user_aead(x->aead, skb); 9731d1e34ddSDavid S. Miller if (ret) 9741d1e34ddSDavid S. Miller goto out; 9751d1e34ddSDavid S. Miller } 9761d1e34ddSDavid S. Miller if (x->aalg) { 9771d1e34ddSDavid S. Miller ret = copy_to_user_auth(x->aalg, skb); 9781d1e34ddSDavid S. Miller if (ret) 9791d1e34ddSDavid S. Miller goto out; 9801d1e34ddSDavid S. Miller } 9811d1e34ddSDavid S. Miller if (x->ealg) { 982c7a5899eSAntony Antony ret = copy_to_user_ealg(x->ealg, skb); 9831d1e34ddSDavid S. Miller if (ret) 9841d1e34ddSDavid S. Miller goto out; 9851d1e34ddSDavid S. Miller } 9861d1e34ddSDavid S. Miller if (x->calg) { 9871d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); 9881d1e34ddSDavid S. Miller if (ret) 9891d1e34ddSDavid S. Miller goto out; 9901d1e34ddSDavid S. Miller } 9911d1e34ddSDavid S. Miller if (x->encap) { 9921d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); 9931d1e34ddSDavid S. Miller if (ret) 9941d1e34ddSDavid S. Miller goto out; 9951d1e34ddSDavid S. Miller } 9961d1e34ddSDavid S. Miller if (x->tfcpad) { 9971d1e34ddSDavid S. Miller ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad); 9981d1e34ddSDavid S. Miller if (ret) 9991d1e34ddSDavid S. Miller goto out; 10001d1e34ddSDavid S. Miller } 10011d1e34ddSDavid S. Miller ret = xfrm_mark_put(skb, &x->mark); 10021d1e34ddSDavid S. Miller if (ret) 10031d1e34ddSDavid S. Miller goto out; 10049b42c1f1SSteffen Klassert 10059b42c1f1SSteffen Klassert ret = xfrm_smark_put(skb, &x->props.smark); 10069b42c1f1SSteffen Klassert if (ret) 10079b42c1f1SSteffen Klassert goto out; 10089b42c1f1SSteffen Klassert 1009f293a5e3Sdingzhi if (x->replay_esn) 10101d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 1011d0fde795SDavid S. Miller xfrm_replay_state_esn_len(x->replay_esn), 10121d1e34ddSDavid S. Miller x->replay_esn); 1013f293a5e3Sdingzhi else 1014f293a5e3Sdingzhi ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 1015f293a5e3Sdingzhi &x->replay); 10161d1e34ddSDavid S. Miller if (ret) 10171d1e34ddSDavid S. Miller goto out; 1018d77e38e6SSteffen Klassert if(x->xso.dev) 1019d77e38e6SSteffen Klassert ret = copy_user_offload(&x->xso, skb); 1020d77e38e6SSteffen Klassert if (ret) 1021d77e38e6SSteffen Klassert goto out; 10227e652640SSteffen Klassert if (x->if_id) { 10237e652640SSteffen Klassert ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id); 1024077fbac4SLorenzo Colitti if (ret) 1025077fbac4SLorenzo Colitti goto out; 1026077fbac4SLorenzo Colitti } 10278598112dSSteffen Klassert if (x->security) 10288598112dSSteffen Klassert ret = copy_sec_ctx(x->security, skb); 10291d1e34ddSDavid S. Miller out: 10301d1e34ddSDavid S. Miller return ret; 103168325d3bSHerbert Xu } 103268325d3bSHerbert Xu 103368325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr) 103468325d3bSHerbert Xu { 103568325d3bSHerbert Xu struct xfrm_dump_info *sp = ptr; 103668325d3bSHerbert Xu struct sk_buff *in_skb = sp->in_skb; 103768325d3bSHerbert Xu struct sk_buff *skb = sp->out_skb; 10385f3eea6bSDmitry Safonov struct xfrm_translator *xtr; 103968325d3bSHerbert Xu struct xfrm_usersa_info *p; 104068325d3bSHerbert Xu struct nlmsghdr *nlh; 104168325d3bSHerbert Xu int err; 104268325d3bSHerbert Xu 104315e47304SEric W. Biederman nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq, 104468325d3bSHerbert Xu XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags); 104568325d3bSHerbert Xu if (nlh == NULL) 104668325d3bSHerbert Xu return -EMSGSIZE; 104768325d3bSHerbert Xu 104868325d3bSHerbert Xu p = nlmsg_data(nlh); 104968325d3bSHerbert Xu 105068325d3bSHerbert Xu err = copy_to_user_state_extra(x, p, skb); 10511d1e34ddSDavid S. Miller if (err) { 10529825069dSThomas Graf nlmsg_cancel(skb, nlh); 105368325d3bSHerbert Xu return err; 10541da177e4SLinus Torvalds } 10551d1e34ddSDavid S. Miller nlmsg_end(skb, nlh); 10565f3eea6bSDmitry Safonov 10575f3eea6bSDmitry Safonov xtr = xfrm_get_translator(); 10585f3eea6bSDmitry Safonov if (xtr) { 10595f3eea6bSDmitry Safonov err = xtr->alloc_compat(skb, nlh); 10605f3eea6bSDmitry Safonov 10615f3eea6bSDmitry Safonov xfrm_put_translator(xtr); 10625f3eea6bSDmitry Safonov if (err) { 10635f3eea6bSDmitry Safonov nlmsg_cancel(skb, nlh); 10645f3eea6bSDmitry Safonov return err; 10655f3eea6bSDmitry Safonov } 10665f3eea6bSDmitry Safonov } 10675f3eea6bSDmitry Safonov 10681d1e34ddSDavid S. Miller return 0; 10691d1e34ddSDavid S. Miller } 10701da177e4SLinus Torvalds 10714c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb) 10724c563f76STimo Teras { 10734c563f76STimo Teras struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 1074283bc9f3SFan Du struct sock *sk = cb->skb->sk; 1075283bc9f3SFan Du struct net *net = sock_net(sk); 1076283bc9f3SFan Du 10771ba5bf99SVegard Nossum if (cb->args[0]) 1078283bc9f3SFan Du xfrm_state_walk_done(walk, net); 10794c563f76STimo Teras return 0; 10804c563f76STimo Teras } 10814c563f76STimo Teras 10821da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) 10831da177e4SLinus Torvalds { 1084fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 10854c563f76STimo Teras struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 10861da177e4SLinus Torvalds struct xfrm_dump_info info; 10871da177e4SLinus Torvalds 10884c563f76STimo Teras BUILD_BUG_ON(sizeof(struct xfrm_state_walk) > 10894c563f76STimo Teras sizeof(cb->args) - sizeof(cb->args[0])); 10904c563f76STimo Teras 10911da177e4SLinus Torvalds info.in_skb = cb->skb; 10921da177e4SLinus Torvalds info.out_skb = skb; 10931da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 10941da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 10954c563f76STimo Teras 10964c563f76STimo Teras if (!cb->args[0]) { 1097d3623099SNicolas Dichtel struct nlattr *attrs[XFRMA_MAX+1]; 1098870a2df4SNicolas Dichtel struct xfrm_address_filter *filter = NULL; 1099d3623099SNicolas Dichtel u8 proto = 0; 1100d3623099SNicolas Dichtel int err; 1101d3623099SNicolas Dichtel 11028cb08174SJohannes Berg err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX, 11038cb08174SJohannes Berg xfrma_policy, cb->extack); 1104d3623099SNicolas Dichtel if (err < 0) 1105d3623099SNicolas Dichtel return err; 1106d3623099SNicolas Dichtel 1107870a2df4SNicolas Dichtel if (attrs[XFRMA_ADDRESS_FILTER]) { 1108df367561SAndrzej Hajda filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]), 1109df367561SAndrzej Hajda sizeof(*filter), GFP_KERNEL); 1110d3623099SNicolas Dichtel if (filter == NULL) 1111d3623099SNicolas Dichtel return -ENOMEM; 1112d3623099SNicolas Dichtel } 1113d3623099SNicolas Dichtel 1114d3623099SNicolas Dichtel if (attrs[XFRMA_PROTO]) 1115d3623099SNicolas Dichtel proto = nla_get_u8(attrs[XFRMA_PROTO]); 1116d3623099SNicolas Dichtel 1117d3623099SNicolas Dichtel xfrm_state_walk_init(walk, proto, filter); 11181ba5bf99SVegard Nossum cb->args[0] = 1; 11194c563f76STimo Teras } 11204c563f76STimo Teras 1121fc34acd3SAlexey Dobriyan (void) xfrm_state_walk(net, walk, dump_one_state, &info); 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds return skb->len; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, 11271da177e4SLinus Torvalds struct xfrm_state *x, u32 seq) 11281da177e4SLinus Torvalds { 11291da177e4SLinus Torvalds struct xfrm_dump_info info; 11301da177e4SLinus Torvalds struct sk_buff *skb; 1131864745d2SMathias Krause int err; 11321da177e4SLinus Torvalds 11337deb2264SThomas Graf skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 11341da177e4SLinus Torvalds if (!skb) 11351da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds info.in_skb = in_skb; 11381da177e4SLinus Torvalds info.out_skb = skb; 11391da177e4SLinus Torvalds info.nlmsg_seq = seq; 11401da177e4SLinus Torvalds info.nlmsg_flags = 0; 11411da177e4SLinus Torvalds 1142864745d2SMathias Krause err = dump_one_state(x, 0, &info); 1143864745d2SMathias Krause if (err) { 11441da177e4SLinus Torvalds kfree_skb(skb); 1145864745d2SMathias Krause return ERR_PTR(err); 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds return skb; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 115121ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available. 115221ee543eSMichal Kubecek * Must be called with RCU read lock. 115321ee543eSMichal Kubecek */ 115421ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb, 115521ee543eSMichal Kubecek u32 pid, unsigned int group) 115621ee543eSMichal Kubecek { 115721ee543eSMichal Kubecek struct sock *nlsk = rcu_dereference(net->xfrm.nlsk); 11585461fc0cSDmitry Safonov struct xfrm_translator *xtr; 115921ee543eSMichal Kubecek 116086126b77SFlorian Westphal if (!nlsk) { 116186126b77SFlorian Westphal kfree_skb(skb); 116286126b77SFlorian Westphal return -EPIPE; 116386126b77SFlorian Westphal } 116486126b77SFlorian Westphal 11655461fc0cSDmitry Safonov xtr = xfrm_get_translator(); 11665461fc0cSDmitry Safonov if (xtr) { 11675461fc0cSDmitry Safonov int err = xtr->alloc_compat(skb, nlmsg_hdr(skb)); 11685461fc0cSDmitry Safonov 11695461fc0cSDmitry Safonov xfrm_put_translator(xtr); 11705461fc0cSDmitry Safonov if (err) { 11715461fc0cSDmitry Safonov kfree_skb(skb); 11725461fc0cSDmitry Safonov return err; 11735461fc0cSDmitry Safonov } 11745461fc0cSDmitry Safonov } 11755461fc0cSDmitry Safonov 117621ee543eSMichal Kubecek return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC); 117721ee543eSMichal Kubecek } 117821ee543eSMichal Kubecek 1179a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void) 11807deb2264SThomas Graf { 11817deb2264SThomas Graf return NLMSG_ALIGN(4) 11827deb2264SThomas Graf + nla_total_size(sizeof(struct xfrmu_spdinfo)) 1183880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhinfo)) 1184880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhthresh)) 1185880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhthresh)); 11867deb2264SThomas Graf } 11877deb2264SThomas Graf 1188e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net, 118915e47304SEric W. Biederman u32 portid, u32 seq, u32 flags) 1190ecfd6b18SJamal Hadi Salim { 11915a6d3416SJamal Hadi Salim struct xfrmk_spdinfo si; 11925a6d3416SJamal Hadi Salim struct xfrmu_spdinfo spc; 11935a6d3416SJamal Hadi Salim struct xfrmu_spdhinfo sph; 1194880a6fabSChristophe Gouault struct xfrmu_spdhthresh spt4, spt6; 1195ecfd6b18SJamal Hadi Salim struct nlmsghdr *nlh; 11961d1e34ddSDavid S. Miller int err; 1197ecfd6b18SJamal Hadi Salim u32 *f; 1198880a6fabSChristophe Gouault unsigned lseq; 1199ecfd6b18SJamal Hadi Salim 120015e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); 120125985edcSLucas De Marchi if (nlh == NULL) /* shouldn't really happen ... */ 1202ecfd6b18SJamal Hadi Salim return -EMSGSIZE; 1203ecfd6b18SJamal Hadi Salim 1204ecfd6b18SJamal Hadi Salim f = nlmsg_data(nlh); 1205ecfd6b18SJamal Hadi Salim *f = flags; 1206e071041bSAlexey Dobriyan xfrm_spd_getinfo(net, &si); 12075a6d3416SJamal Hadi Salim spc.incnt = si.incnt; 12085a6d3416SJamal Hadi Salim spc.outcnt = si.outcnt; 12095a6d3416SJamal Hadi Salim spc.fwdcnt = si.fwdcnt; 12105a6d3416SJamal Hadi Salim spc.inscnt = si.inscnt; 12115a6d3416SJamal Hadi Salim spc.outscnt = si.outscnt; 12125a6d3416SJamal Hadi Salim spc.fwdscnt = si.fwdscnt; 12135a6d3416SJamal Hadi Salim sph.spdhcnt = si.spdhcnt; 12145a6d3416SJamal Hadi Salim sph.spdhmcnt = si.spdhmcnt; 1215ecfd6b18SJamal Hadi Salim 1216880a6fabSChristophe Gouault do { 1217880a6fabSChristophe Gouault lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock); 1218880a6fabSChristophe Gouault 1219880a6fabSChristophe Gouault spt4.lbits = net->xfrm.policy_hthresh.lbits4; 1220880a6fabSChristophe Gouault spt4.rbits = net->xfrm.policy_hthresh.rbits4; 1221880a6fabSChristophe Gouault spt6.lbits = net->xfrm.policy_hthresh.lbits6; 1222880a6fabSChristophe Gouault spt6.rbits = net->xfrm.policy_hthresh.rbits6; 1223880a6fabSChristophe Gouault } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq)); 1224880a6fabSChristophe Gouault 12251d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc); 12261d1e34ddSDavid S. Miller if (!err) 12271d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph); 1228880a6fabSChristophe Gouault if (!err) 1229880a6fabSChristophe Gouault err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4); 1230880a6fabSChristophe Gouault if (!err) 1231880a6fabSChristophe Gouault err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6); 12321d1e34ddSDavid S. Miller if (err) { 12331d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 12341d1e34ddSDavid S. Miller return err; 12351d1e34ddSDavid S. Miller } 1236ecfd6b18SJamal Hadi Salim 1237053c095aSJohannes Berg nlmsg_end(skb, nlh); 1238053c095aSJohannes Berg return 0; 1239ecfd6b18SJamal Hadi Salim } 1240ecfd6b18SJamal Hadi Salim 1241880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 1242880a6fabSChristophe Gouault struct nlattr **attrs) 1243880a6fabSChristophe Gouault { 1244880a6fabSChristophe Gouault struct net *net = sock_net(skb->sk); 1245880a6fabSChristophe Gouault struct xfrmu_spdhthresh *thresh4 = NULL; 1246880a6fabSChristophe Gouault struct xfrmu_spdhthresh *thresh6 = NULL; 1247880a6fabSChristophe Gouault 1248880a6fabSChristophe Gouault /* selector prefixlen thresholds to hash policies */ 1249880a6fabSChristophe Gouault if (attrs[XFRMA_SPD_IPV4_HTHRESH]) { 1250880a6fabSChristophe Gouault struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH]; 1251880a6fabSChristophe Gouault 1252880a6fabSChristophe Gouault if (nla_len(rta) < sizeof(*thresh4)) 1253880a6fabSChristophe Gouault return -EINVAL; 1254880a6fabSChristophe Gouault thresh4 = nla_data(rta); 1255880a6fabSChristophe Gouault if (thresh4->lbits > 32 || thresh4->rbits > 32) 1256880a6fabSChristophe Gouault return -EINVAL; 1257880a6fabSChristophe Gouault } 1258880a6fabSChristophe Gouault if (attrs[XFRMA_SPD_IPV6_HTHRESH]) { 1259880a6fabSChristophe Gouault struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH]; 1260880a6fabSChristophe Gouault 1261880a6fabSChristophe Gouault if (nla_len(rta) < sizeof(*thresh6)) 1262880a6fabSChristophe Gouault return -EINVAL; 1263880a6fabSChristophe Gouault thresh6 = nla_data(rta); 1264880a6fabSChristophe Gouault if (thresh6->lbits > 128 || thresh6->rbits > 128) 1265880a6fabSChristophe Gouault return -EINVAL; 1266880a6fabSChristophe Gouault } 1267880a6fabSChristophe Gouault 1268880a6fabSChristophe Gouault if (thresh4 || thresh6) { 1269880a6fabSChristophe Gouault write_seqlock(&net->xfrm.policy_hthresh.lock); 1270880a6fabSChristophe Gouault if (thresh4) { 1271880a6fabSChristophe Gouault net->xfrm.policy_hthresh.lbits4 = thresh4->lbits; 1272880a6fabSChristophe Gouault net->xfrm.policy_hthresh.rbits4 = thresh4->rbits; 1273880a6fabSChristophe Gouault } 1274880a6fabSChristophe Gouault if (thresh6) { 1275880a6fabSChristophe Gouault net->xfrm.policy_hthresh.lbits6 = thresh6->lbits; 1276880a6fabSChristophe Gouault net->xfrm.policy_hthresh.rbits6 = thresh6->rbits; 1277880a6fabSChristophe Gouault } 1278880a6fabSChristophe Gouault write_sequnlock(&net->xfrm.policy_hthresh.lock); 1279880a6fabSChristophe Gouault 1280880a6fabSChristophe Gouault xfrm_policy_hash_rebuild(net); 1281880a6fabSChristophe Gouault } 1282880a6fabSChristophe Gouault 1283880a6fabSChristophe Gouault return 0; 1284880a6fabSChristophe Gouault } 1285880a6fabSChristophe Gouault 1286ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 12875424f32eSThomas Graf struct nlattr **attrs) 1288ecfd6b18SJamal Hadi Salim { 1289a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 1290ecfd6b18SJamal Hadi Salim struct sk_buff *r_skb; 12917b67c857SThomas Graf u32 *flags = nlmsg_data(nlh); 129215e47304SEric W. Biederman u32 sportid = NETLINK_CB(skb).portid; 1293ecfd6b18SJamal Hadi Salim u32 seq = nlh->nlmsg_seq; 12942fc5f83bSGustavo A. R. Silva int err; 1295ecfd6b18SJamal Hadi Salim 12967deb2264SThomas Graf r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); 1297ecfd6b18SJamal Hadi Salim if (r_skb == NULL) 1298ecfd6b18SJamal Hadi Salim return -ENOMEM; 1299ecfd6b18SJamal Hadi Salim 13002fc5f83bSGustavo A. R. Silva err = build_spdinfo(r_skb, net, sportid, seq, *flags); 13012fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 1302ecfd6b18SJamal Hadi Salim 130315e47304SEric W. Biederman return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 1304ecfd6b18SJamal Hadi Salim } 1305ecfd6b18SJamal Hadi Salim 1306a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void) 13077deb2264SThomas Graf { 13087deb2264SThomas Graf return NLMSG_ALIGN(4) 13097deb2264SThomas Graf + nla_total_size(sizeof(struct xfrmu_sadhinfo)) 13107deb2264SThomas Graf + nla_total_size(4); /* XFRMA_SAD_CNT */ 13117deb2264SThomas Graf } 13127deb2264SThomas Graf 1313e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net, 131415e47304SEric W. Biederman u32 portid, u32 seq, u32 flags) 131528d8909bSJamal Hadi Salim { 1316af11e316SJamal Hadi Salim struct xfrmk_sadinfo si; 1317af11e316SJamal Hadi Salim struct xfrmu_sadhinfo sh; 131828d8909bSJamal Hadi Salim struct nlmsghdr *nlh; 13191d1e34ddSDavid S. Miller int err; 132028d8909bSJamal Hadi Salim u32 *f; 132128d8909bSJamal Hadi Salim 132215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); 132325985edcSLucas De Marchi if (nlh == NULL) /* shouldn't really happen ... */ 132428d8909bSJamal Hadi Salim return -EMSGSIZE; 132528d8909bSJamal Hadi Salim 132628d8909bSJamal Hadi Salim f = nlmsg_data(nlh); 132728d8909bSJamal Hadi Salim *f = flags; 1328e071041bSAlexey Dobriyan xfrm_sad_getinfo(net, &si); 132928d8909bSJamal Hadi Salim 1330af11e316SJamal Hadi Salim sh.sadhmcnt = si.sadhmcnt; 1331af11e316SJamal Hadi Salim sh.sadhcnt = si.sadhcnt; 1332af11e316SJamal Hadi Salim 13331d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt); 13341d1e34ddSDavid S. Miller if (!err) 13351d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); 13361d1e34ddSDavid S. Miller if (err) { 13371d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 13381d1e34ddSDavid S. Miller return err; 13391d1e34ddSDavid S. Miller } 134028d8909bSJamal Hadi Salim 1341053c095aSJohannes Berg nlmsg_end(skb, nlh); 1342053c095aSJohannes Berg return 0; 134328d8909bSJamal Hadi Salim } 134428d8909bSJamal Hadi Salim 134528d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 13465424f32eSThomas Graf struct nlattr **attrs) 134728d8909bSJamal Hadi Salim { 1348a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 134928d8909bSJamal Hadi Salim struct sk_buff *r_skb; 13507b67c857SThomas Graf u32 *flags = nlmsg_data(nlh); 135115e47304SEric W. Biederman u32 sportid = NETLINK_CB(skb).portid; 135228d8909bSJamal Hadi Salim u32 seq = nlh->nlmsg_seq; 13532fc5f83bSGustavo A. R. Silva int err; 135428d8909bSJamal Hadi Salim 13557deb2264SThomas Graf r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); 135628d8909bSJamal Hadi Salim if (r_skb == NULL) 135728d8909bSJamal Hadi Salim return -ENOMEM; 135828d8909bSJamal Hadi Salim 13592fc5f83bSGustavo A. R. Silva err = build_sadinfo(r_skb, net, sportid, seq, *flags); 13602fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 136128d8909bSJamal Hadi Salim 136215e47304SEric W. Biederman return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 136328d8909bSJamal Hadi Salim } 136428d8909bSJamal Hadi Salim 136522e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 13665424f32eSThomas Graf struct nlattr **attrs) 13671da177e4SLinus Torvalds { 1368fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 13697b67c857SThomas Graf struct xfrm_usersa_id *p = nlmsg_data(nlh); 13701da177e4SLinus Torvalds struct xfrm_state *x; 13711da177e4SLinus Torvalds struct sk_buff *resp_skb; 1372eb2971b6SMasahide NAKAMURA int err = -ESRCH; 13731da177e4SLinus Torvalds 1374fc34acd3SAlexey Dobriyan x = xfrm_user_state_lookup(net, p, attrs, &err); 13751da177e4SLinus Torvalds if (x == NULL) 13761da177e4SLinus Torvalds goto out_noput; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 13791da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 13801da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 13811da177e4SLinus Torvalds } else { 138215e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds xfrm_state_put(x); 13851da177e4SLinus Torvalds out_noput: 13861da177e4SLinus Torvalds return err; 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds 138922e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, 13905424f32eSThomas Graf struct nlattr **attrs) 13911da177e4SLinus Torvalds { 1392fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 13931da177e4SLinus Torvalds struct xfrm_state *x; 13941da177e4SLinus Torvalds struct xfrm_userspi_info *p; 13955f3eea6bSDmitry Safonov struct xfrm_translator *xtr; 13961da177e4SLinus Torvalds struct sk_buff *resp_skb; 13971da177e4SLinus Torvalds xfrm_address_t *daddr; 13981da177e4SLinus Torvalds int family; 13991da177e4SLinus Torvalds int err; 14006f26b61eSJamal Hadi Salim u32 mark; 14016f26b61eSJamal Hadi Salim struct xfrm_mark m; 14027e652640SSteffen Klassert u32 if_id = 0; 14031da177e4SLinus Torvalds 14047b67c857SThomas Graf p = nlmsg_data(nlh); 1405776e9dd9SFan Du err = verify_spi_info(p->info.id.proto, p->min, p->max); 14061da177e4SLinus Torvalds if (err) 14071da177e4SLinus Torvalds goto out_noput; 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds family = p->info.family; 14101da177e4SLinus Torvalds daddr = &p->info.id.daddr; 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds x = NULL; 14136f26b61eSJamal Hadi Salim 14146f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 14157e652640SSteffen Klassert 14167e652640SSteffen Klassert if (attrs[XFRMA_IF_ID]) 14177e652640SSteffen Klassert if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 14187e652640SSteffen Klassert 14191da177e4SLinus Torvalds if (p->info.seq) { 14206f26b61eSJamal Hadi Salim x = xfrm_find_acq_byseq(net, mark, p->info.seq); 142170e94e66SYOSHIFUJI Hideaki / 吉藤英明 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) { 14221da177e4SLinus Torvalds xfrm_state_put(x); 14231da177e4SLinus Torvalds x = NULL; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds if (!x) 14286f26b61eSJamal Hadi Salim x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid, 14297e652640SSteffen Klassert if_id, p->info.id.proto, daddr, 14301da177e4SLinus Torvalds &p->info.saddr, 1, 14311da177e4SLinus Torvalds family); 14321da177e4SLinus Torvalds err = -ENOENT; 14331da177e4SLinus Torvalds if (x == NULL) 14341da177e4SLinus Torvalds goto out_noput; 14351da177e4SLinus Torvalds 1436658b219eSHerbert Xu err = xfrm_alloc_spi(x, p->min, p->max); 1437658b219eSHerbert Xu if (err) 1438658b219eSHerbert Xu goto out; 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 14411da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 14421da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 14431da177e4SLinus Torvalds goto out; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds 14465f3eea6bSDmitry Safonov xtr = xfrm_get_translator(); 14475f3eea6bSDmitry Safonov if (xtr) { 14485f3eea6bSDmitry Safonov err = xtr->alloc_compat(skb, nlmsg_hdr(skb)); 14495f3eea6bSDmitry Safonov 14505f3eea6bSDmitry Safonov xfrm_put_translator(xtr); 14515f3eea6bSDmitry Safonov if (err) { 14525f3eea6bSDmitry Safonov kfree_skb(resp_skb); 14535f3eea6bSDmitry Safonov goto out; 14545f3eea6bSDmitry Safonov } 14555f3eea6bSDmitry Safonov } 14565f3eea6bSDmitry Safonov 145715e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds out: 14601da177e4SLinus Torvalds xfrm_state_put(x); 14611da177e4SLinus Torvalds out_noput: 14621da177e4SLinus Torvalds return err; 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 1465b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir) 14661da177e4SLinus Torvalds { 14671da177e4SLinus Torvalds switch (dir) { 14681da177e4SLinus Torvalds case XFRM_POLICY_IN: 14691da177e4SLinus Torvalds case XFRM_POLICY_OUT: 14701da177e4SLinus Torvalds case XFRM_POLICY_FWD: 14711da177e4SLinus Torvalds break; 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds default: 14741da177e4SLinus Torvalds return -EINVAL; 14753ff50b79SStephen Hemminger } 14761da177e4SLinus Torvalds 14771da177e4SLinus Torvalds return 0; 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds 1480b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type) 1481f7b6983fSMasahide NAKAMURA { 1482f7b6983fSMasahide NAKAMURA switch (type) { 1483f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_MAIN: 1484f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 1485f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_SUB: 1486f7b6983fSMasahide NAKAMURA #endif 1487f7b6983fSMasahide NAKAMURA break; 1488f7b6983fSMasahide NAKAMURA 1489f7b6983fSMasahide NAKAMURA default: 1490f7b6983fSMasahide NAKAMURA return -EINVAL; 14913ff50b79SStephen Hemminger } 1492f7b6983fSMasahide NAKAMURA 1493f7b6983fSMasahide NAKAMURA return 0; 1494f7b6983fSMasahide NAKAMURA } 1495f7b6983fSMasahide NAKAMURA 14961da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) 14971da177e4SLinus Torvalds { 1498e682adf0SFan Du int ret; 1499e682adf0SFan Du 15001da177e4SLinus Torvalds switch (p->share) { 15011da177e4SLinus Torvalds case XFRM_SHARE_ANY: 15021da177e4SLinus Torvalds case XFRM_SHARE_SESSION: 15031da177e4SLinus Torvalds case XFRM_SHARE_USER: 15041da177e4SLinus Torvalds case XFRM_SHARE_UNIQUE: 15051da177e4SLinus Torvalds break; 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds default: 15081da177e4SLinus Torvalds return -EINVAL; 15093ff50b79SStephen Hemminger } 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds switch (p->action) { 15121da177e4SLinus Torvalds case XFRM_POLICY_ALLOW: 15131da177e4SLinus Torvalds case XFRM_POLICY_BLOCK: 15141da177e4SLinus Torvalds break; 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds default: 15171da177e4SLinus Torvalds return -EINVAL; 15183ff50b79SStephen Hemminger } 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds switch (p->sel.family) { 15211da177e4SLinus Torvalds case AF_INET: 152207bf7908SSteffen Klassert if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) 152307bf7908SSteffen Klassert return -EINVAL; 152407bf7908SSteffen Klassert 15251da177e4SLinus Torvalds break; 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds case AF_INET6: 1528dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 152907bf7908SSteffen Klassert if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) 153007bf7908SSteffen Klassert return -EINVAL; 153107bf7908SSteffen Klassert 15321da177e4SLinus Torvalds break; 15331da177e4SLinus Torvalds #else 15341da177e4SLinus Torvalds return -EAFNOSUPPORT; 15351da177e4SLinus Torvalds #endif 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds default: 15381da177e4SLinus Torvalds return -EINVAL; 15393ff50b79SStephen Hemminger } 15401da177e4SLinus Torvalds 1541e682adf0SFan Du ret = verify_policy_dir(p->dir); 1542e682adf0SFan Du if (ret) 1543e682adf0SFan Du return ret; 1544b805d78dSYueHaibing if (p->index && (xfrm_policy_id2dir(p->index) != p->dir)) 1545e682adf0SFan Du return -EINVAL; 1546e682adf0SFan Du 1547e682adf0SFan Du return 0; 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds 15505424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) 1551df71837dSTrent Jaeger { 15525424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1553df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx; 1554df71837dSTrent Jaeger 1555df71837dSTrent Jaeger if (!rt) 1556df71837dSTrent Jaeger return 0; 1557df71837dSTrent Jaeger 15585424f32eSThomas Graf uctx = nla_data(rt); 155952a4c640SNikolay Aleksandrov return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL); 1560df71837dSTrent Jaeger } 1561df71837dSTrent Jaeger 15621da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, 15631da177e4SLinus Torvalds int nr) 15641da177e4SLinus Torvalds { 15651da177e4SLinus Torvalds int i; 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds xp->xfrm_nr = nr; 15681da177e4SLinus Torvalds for (i = 0; i < nr; i++, ut++) { 15691da177e4SLinus Torvalds struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); 15721da177e4SLinus Torvalds memcpy(&t->saddr, &ut->saddr, 15731da177e4SLinus Torvalds sizeof(xfrm_address_t)); 15741da177e4SLinus Torvalds t->reqid = ut->reqid; 15751da177e4SLinus Torvalds t->mode = ut->mode; 15761da177e4SLinus Torvalds t->share = ut->share; 15771da177e4SLinus Torvalds t->optional = ut->optional; 15781da177e4SLinus Torvalds t->aalgos = ut->aalgos; 15791da177e4SLinus Torvalds t->ealgos = ut->ealgos; 15801da177e4SLinus Torvalds t->calgos = ut->calgos; 1581c5d18e98SHerbert Xu /* If all masks are ~0, then we allow all algorithms. */ 1582c5d18e98SHerbert Xu t->allalgs = !~(t->aalgos & t->ealgos & t->calgos); 15838511d01dSMiika Komu t->encap_family = ut->family; 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds } 15861da177e4SLinus Torvalds 1587b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family) 1588b4ad86bfSDavid S. Miller { 1589732706afSSteffen Klassert u16 prev_family; 1590b4ad86bfSDavid S. Miller int i; 1591b4ad86bfSDavid S. Miller 1592b4ad86bfSDavid S. Miller if (nr > XFRM_MAX_DEPTH) 1593b4ad86bfSDavid S. Miller return -EINVAL; 1594b4ad86bfSDavid S. Miller 1595732706afSSteffen Klassert prev_family = family; 1596732706afSSteffen Klassert 1597b4ad86bfSDavid S. Miller for (i = 0; i < nr; i++) { 1598b4ad86bfSDavid S. Miller /* We never validated the ut->family value, so many 1599b4ad86bfSDavid S. Miller * applications simply leave it at zero. The check was 1600b4ad86bfSDavid S. Miller * never made and ut->family was ignored because all 1601b4ad86bfSDavid S. Miller * templates could be assumed to have the same family as 1602b4ad86bfSDavid S. Miller * the policy itself. Now that we will have ipv4-in-ipv6 1603b4ad86bfSDavid S. Miller * and ipv6-in-ipv4 tunnels, this is no longer true. 1604b4ad86bfSDavid S. Miller */ 1605b4ad86bfSDavid S. Miller if (!ut[i].family) 1606b4ad86bfSDavid S. Miller ut[i].family = family; 1607b4ad86bfSDavid S. Miller 160835e61038SFlorian Westphal switch (ut[i].mode) { 160935e61038SFlorian Westphal case XFRM_MODE_TUNNEL: 161035e61038SFlorian Westphal case XFRM_MODE_BEET: 161135e61038SFlorian Westphal break; 161235e61038SFlorian Westphal default: 161335e61038SFlorian Westphal if (ut[i].family != prev_family) 1614732706afSSteffen Klassert return -EINVAL; 161535e61038SFlorian Westphal break; 161635e61038SFlorian Westphal } 161732bf94fbSSean Tranchetti if (ut[i].mode >= XFRM_MODE_MAX) 161832bf94fbSSean Tranchetti return -EINVAL; 161932bf94fbSSean Tranchetti 1620732706afSSteffen Klassert prev_family = ut[i].family; 1621732706afSSteffen Klassert 1622b4ad86bfSDavid S. Miller switch (ut[i].family) { 1623b4ad86bfSDavid S. Miller case AF_INET: 1624b4ad86bfSDavid S. Miller break; 1625dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1626b4ad86bfSDavid S. Miller case AF_INET6: 1627b4ad86bfSDavid S. Miller break; 1628b4ad86bfSDavid S. Miller #endif 1629b4ad86bfSDavid S. Miller default: 1630b4ad86bfSDavid S. Miller return -EINVAL; 16313ff50b79SStephen Hemminger } 16326a53b759SCong Wang 1633dbb2483bSCong Wang if (!xfrm_id_proto_valid(ut[i].id.proto)) 16346a53b759SCong Wang return -EINVAL; 16356a53b759SCong Wang } 16366a53b759SCong Wang 1637b4ad86bfSDavid S. Miller return 0; 1638b4ad86bfSDavid S. Miller } 1639b4ad86bfSDavid S. Miller 16405424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs) 16411da177e4SLinus Torvalds { 16425424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_TMPL]; 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds if (!rt) { 16451da177e4SLinus Torvalds pol->xfrm_nr = 0; 16461da177e4SLinus Torvalds } else { 16475424f32eSThomas Graf struct xfrm_user_tmpl *utmpl = nla_data(rt); 16485424f32eSThomas Graf int nr = nla_len(rt) / sizeof(*utmpl); 1649b4ad86bfSDavid S. Miller int err; 16501da177e4SLinus Torvalds 1651b4ad86bfSDavid S. Miller err = validate_tmpl(nr, utmpl, pol->family); 1652b4ad86bfSDavid S. Miller if (err) 1653b4ad86bfSDavid S. Miller return err; 16541da177e4SLinus Torvalds 16555424f32eSThomas Graf copy_templates(pol, utmpl, nr); 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds return 0; 16581da177e4SLinus Torvalds } 16591da177e4SLinus Torvalds 16605424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs) 1661f7b6983fSMasahide NAKAMURA { 16625424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; 1663f7b6983fSMasahide NAKAMURA struct xfrm_userpolicy_type *upt; 1664b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 1665f7b6983fSMasahide NAKAMURA int err; 1666f7b6983fSMasahide NAKAMURA 1667f7b6983fSMasahide NAKAMURA if (rt) { 16685424f32eSThomas Graf upt = nla_data(rt); 1669f7b6983fSMasahide NAKAMURA type = upt->type; 1670f7b6983fSMasahide NAKAMURA } 1671f7b6983fSMasahide NAKAMURA 1672f7b6983fSMasahide NAKAMURA err = verify_policy_type(type); 1673f7b6983fSMasahide NAKAMURA if (err) 1674f7b6983fSMasahide NAKAMURA return err; 1675f7b6983fSMasahide NAKAMURA 1676f7b6983fSMasahide NAKAMURA *tp = type; 1677f7b6983fSMasahide NAKAMURA return 0; 1678f7b6983fSMasahide NAKAMURA } 1679f7b6983fSMasahide NAKAMURA 16801da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) 16811da177e4SLinus Torvalds { 16821da177e4SLinus Torvalds xp->priority = p->priority; 16831da177e4SLinus Torvalds xp->index = p->index; 16841da177e4SLinus Torvalds memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); 16851da177e4SLinus Torvalds memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); 16861da177e4SLinus Torvalds xp->action = p->action; 16871da177e4SLinus Torvalds xp->flags = p->flags; 16881da177e4SLinus Torvalds xp->family = p->sel.family; 16891da177e4SLinus Torvalds /* XXX xp->share = p->share; */ 16901da177e4SLinus Torvalds } 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) 16931da177e4SLinus Torvalds { 16947b789836SMathias Krause memset(p, 0, sizeof(*p)); 16951da177e4SLinus Torvalds memcpy(&p->sel, &xp->selector, sizeof(p->sel)); 16961da177e4SLinus Torvalds memcpy(&p->lft, &xp->lft, sizeof(p->lft)); 16971da177e4SLinus Torvalds memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); 16981da177e4SLinus Torvalds p->priority = xp->priority; 16991da177e4SLinus Torvalds p->index = xp->index; 17001da177e4SLinus Torvalds p->sel.family = xp->family; 17011da177e4SLinus Torvalds p->dir = dir; 17021da177e4SLinus Torvalds p->action = xp->action; 17031da177e4SLinus Torvalds p->flags = xp->flags; 17041da177e4SLinus Torvalds p->share = XFRM_SHARE_ANY; /* XXX xp->share */ 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 1707fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp) 17081da177e4SLinus Torvalds { 1709fc34acd3SAlexey Dobriyan struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL); 17101da177e4SLinus Torvalds int err; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds if (!xp) { 17131da177e4SLinus Torvalds *errp = -ENOMEM; 17141da177e4SLinus Torvalds return NULL; 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds copy_from_user_policy(xp, p); 1718df71837dSTrent Jaeger 171935a7aa08SThomas Graf err = copy_from_user_policy_type(&xp->type, attrs); 1720f7b6983fSMasahide NAKAMURA if (err) 1721f7b6983fSMasahide NAKAMURA goto error; 1722f7b6983fSMasahide NAKAMURA 172335a7aa08SThomas Graf if (!(err = copy_from_user_tmpl(xp, attrs))) 172435a7aa08SThomas Graf err = copy_from_user_sec_ctx(xp, attrs); 1725f7b6983fSMasahide NAKAMURA if (err) 1726f7b6983fSMasahide NAKAMURA goto error; 17271da177e4SLinus Torvalds 1728295fae56SJamal Hadi Salim xfrm_mark_get(attrs, &xp->mark); 1729295fae56SJamal Hadi Salim 17307e652640SSteffen Klassert if (attrs[XFRMA_IF_ID]) 17317e652640SSteffen Klassert xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 17327e652640SSteffen Klassert 17331da177e4SLinus Torvalds return xp; 1734f7b6983fSMasahide NAKAMURA error: 1735f7b6983fSMasahide NAKAMURA *errp = err; 173612a169e7SHerbert Xu xp->walk.dead = 1; 173764c31b3fSWANG Cong xfrm_policy_destroy(xp); 1738f7b6983fSMasahide NAKAMURA return NULL; 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds 174122e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 17425424f32eSThomas Graf struct nlattr **attrs) 17431da177e4SLinus Torvalds { 1744fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 17457b67c857SThomas Graf struct xfrm_userpolicy_info *p = nlmsg_data(nlh); 17461da177e4SLinus Torvalds struct xfrm_policy *xp; 174726b15dadSJamal Hadi Salim struct km_event c; 17481da177e4SLinus Torvalds int err; 17491da177e4SLinus Torvalds int excl; 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds err = verify_newpolicy_info(p); 17521da177e4SLinus Torvalds if (err) 17531da177e4SLinus Torvalds return err; 175435a7aa08SThomas Graf err = verify_sec_ctx_len(attrs); 1755df71837dSTrent Jaeger if (err) 1756df71837dSTrent Jaeger return err; 17571da177e4SLinus Torvalds 1758fc34acd3SAlexey Dobriyan xp = xfrm_policy_construct(net, p, attrs, &err); 17591da177e4SLinus Torvalds if (!xp) 17601da177e4SLinus Torvalds return err; 17611da177e4SLinus Torvalds 176225985edcSLucas De Marchi /* shouldn't excl be based on nlh flags?? 176326b15dadSJamal Hadi Salim * Aha! this is anti-netlink really i.e more pfkey derived 1764a7fd0e6dSBhaskar Chowdhury * in netlink excl is a flag and you wouldn't need 176526b15dadSJamal Hadi Salim * a type XFRM_MSG_UPDPOLICY - JHS */ 17661da177e4SLinus Torvalds excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; 17671da177e4SLinus Torvalds err = xfrm_policy_insert(p->dir, xp, excl); 17682e71029eSTetsuo Handa xfrm_audit_policy_add(xp, err ? 0 : 1, true); 1769161a09e7SJoy Latten 17701da177e4SLinus Torvalds if (err) { 177103e1ad7bSPaul Moore security_xfrm_policy_free(xp->security); 17721da177e4SLinus Torvalds kfree(xp); 17731da177e4SLinus Torvalds return err; 17741da177e4SLinus Torvalds } 17751da177e4SLinus Torvalds 1776f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 177726b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 177815e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 177926b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 178026b15dadSJamal Hadi Salim 17811da177e4SLinus Torvalds xfrm_pol_put(xp); 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds return 0; 17841da177e4SLinus Torvalds } 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) 17871da177e4SLinus Torvalds { 17881da177e4SLinus Torvalds struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; 17891da177e4SLinus Torvalds int i; 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds if (xp->xfrm_nr == 0) 17921da177e4SLinus Torvalds return 0; 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds for (i = 0; i < xp->xfrm_nr; i++) { 17951da177e4SLinus Torvalds struct xfrm_user_tmpl *up = &vec[i]; 17961da177e4SLinus Torvalds struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; 17971da177e4SLinus Torvalds 17981f86840fSMathias Krause memset(up, 0, sizeof(*up)); 17991da177e4SLinus Torvalds memcpy(&up->id, &kp->id, sizeof(up->id)); 18008511d01dSMiika Komu up->family = kp->encap_family; 18011da177e4SLinus Torvalds memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); 18021da177e4SLinus Torvalds up->reqid = kp->reqid; 18031da177e4SLinus Torvalds up->mode = kp->mode; 18041da177e4SLinus Torvalds up->share = kp->share; 18051da177e4SLinus Torvalds up->optional = kp->optional; 18061da177e4SLinus Torvalds up->aalgos = kp->aalgos; 18071da177e4SLinus Torvalds up->ealgos = kp->ealgos; 18081da177e4SLinus Torvalds up->calgos = kp->calgos; 18091da177e4SLinus Torvalds } 18101da177e4SLinus Torvalds 1811c0144beaSThomas Graf return nla_put(skb, XFRMA_TMPL, 1812c0144beaSThomas Graf sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); 1813df71837dSTrent Jaeger } 1814df71837dSTrent Jaeger 18150d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) 18160d681623SSerge Hallyn { 18170d681623SSerge Hallyn if (x->security) { 18180d681623SSerge Hallyn return copy_sec_ctx(x->security, skb); 18190d681623SSerge Hallyn } 18200d681623SSerge Hallyn return 0; 18210d681623SSerge Hallyn } 18220d681623SSerge Hallyn 18230d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) 18240d681623SSerge Hallyn { 18251d1e34ddSDavid S. Miller if (xp->security) 18260d681623SSerge Hallyn return copy_sec_ctx(xp->security, skb); 18270d681623SSerge Hallyn return 0; 18280d681623SSerge Hallyn } 1829a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void) 1830cfbfd45aSThomas Graf { 1831cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY 1832cfbfd45aSThomas Graf return nla_total_size(sizeof(struct xfrm_userpolicy_type)); 1833cfbfd45aSThomas Graf #else 1834cfbfd45aSThomas Graf return 0; 1835cfbfd45aSThomas Graf #endif 1836cfbfd45aSThomas Graf } 18370d681623SSerge Hallyn 1838f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 1839b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1840f7b6983fSMasahide NAKAMURA { 184145c180bcSEric Dumazet struct xfrm_userpolicy_type upt; 184245c180bcSEric Dumazet 184345c180bcSEric Dumazet /* Sadly there are two holes in struct xfrm_userpolicy_type */ 184445c180bcSEric Dumazet memset(&upt, 0, sizeof(upt)); 184545c180bcSEric Dumazet upt.type = type; 1846f7b6983fSMasahide NAKAMURA 1847c0144beaSThomas Graf return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); 1848f7b6983fSMasahide NAKAMURA } 1849f7b6983fSMasahide NAKAMURA 1850f7b6983fSMasahide NAKAMURA #else 1851b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1852f7b6983fSMasahide NAKAMURA { 1853f7b6983fSMasahide NAKAMURA return 0; 1854f7b6983fSMasahide NAKAMURA } 1855f7b6983fSMasahide NAKAMURA #endif 1856f7b6983fSMasahide NAKAMURA 18571da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) 18581da177e4SLinus Torvalds { 18591da177e4SLinus Torvalds struct xfrm_dump_info *sp = ptr; 18601da177e4SLinus Torvalds struct xfrm_userpolicy_info *p; 18611da177e4SLinus Torvalds struct sk_buff *in_skb = sp->in_skb; 18621da177e4SLinus Torvalds struct sk_buff *skb = sp->out_skb; 18635f3eea6bSDmitry Safonov struct xfrm_translator *xtr; 18641da177e4SLinus Torvalds struct nlmsghdr *nlh; 18651d1e34ddSDavid S. Miller int err; 18661da177e4SLinus Torvalds 186715e47304SEric W. Biederman nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq, 186879b8b7f4SThomas Graf XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); 186979b8b7f4SThomas Graf if (nlh == NULL) 187079b8b7f4SThomas Graf return -EMSGSIZE; 18711da177e4SLinus Torvalds 18727b67c857SThomas Graf p = nlmsg_data(nlh); 18731da177e4SLinus Torvalds copy_to_user_policy(xp, p, dir); 18741d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 18751d1e34ddSDavid S. Miller if (!err) 18761d1e34ddSDavid S. Miller err = copy_to_user_sec_ctx(xp, skb); 18771d1e34ddSDavid S. Miller if (!err) 18781d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 18791d1e34ddSDavid S. Miller if (!err) 18801d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 18817e652640SSteffen Klassert if (!err) 18827e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 18831d1e34ddSDavid S. Miller if (err) { 18841d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 18851d1e34ddSDavid S. Miller return err; 18861d1e34ddSDavid S. Miller } 18879825069dSThomas Graf nlmsg_end(skb, nlh); 18885f3eea6bSDmitry Safonov 18895f3eea6bSDmitry Safonov xtr = xfrm_get_translator(); 18905f3eea6bSDmitry Safonov if (xtr) { 18915f3eea6bSDmitry Safonov err = xtr->alloc_compat(skb, nlh); 18925f3eea6bSDmitry Safonov 18935f3eea6bSDmitry Safonov xfrm_put_translator(xtr); 18945f3eea6bSDmitry Safonov if (err) { 18955f3eea6bSDmitry Safonov nlmsg_cancel(skb, nlh); 18965f3eea6bSDmitry Safonov return err; 18975f3eea6bSDmitry Safonov } 18985f3eea6bSDmitry Safonov } 18995f3eea6bSDmitry Safonov 19001da177e4SLinus Torvalds return 0; 19011da177e4SLinus Torvalds } 19021da177e4SLinus Torvalds 19034c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb) 19044c563f76STimo Teras { 19051137b5e2SHerbert Xu struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 1906283bc9f3SFan Du struct net *net = sock_net(cb->skb->sk); 19074c563f76STimo Teras 1908283bc9f3SFan Du xfrm_policy_walk_done(walk, net); 19094c563f76STimo Teras return 0; 19104c563f76STimo Teras } 19114c563f76STimo Teras 19121137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb) 19131137b5e2SHerbert Xu { 19141137b5e2SHerbert Xu struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 19151137b5e2SHerbert Xu 19161137b5e2SHerbert Xu BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args)); 19171137b5e2SHerbert Xu 19181137b5e2SHerbert Xu xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); 19191137b5e2SHerbert Xu return 0; 19201137b5e2SHerbert Xu } 19211137b5e2SHerbert Xu 19221da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) 19231da177e4SLinus Torvalds { 1924fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 19251137b5e2SHerbert Xu struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 19261da177e4SLinus Torvalds struct xfrm_dump_info info; 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds info.in_skb = cb->skb; 19291da177e4SLinus Torvalds info.out_skb = skb; 19301da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 19311da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 19324c563f76STimo Teras 1933fc34acd3SAlexey Dobriyan (void) xfrm_policy_walk(net, walk, dump_one_policy, &info); 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds return skb->len; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, 19391da177e4SLinus Torvalds struct xfrm_policy *xp, 19401da177e4SLinus Torvalds int dir, u32 seq) 19411da177e4SLinus Torvalds { 19421da177e4SLinus Torvalds struct xfrm_dump_info info; 19431da177e4SLinus Torvalds struct sk_buff *skb; 1944c2546372SMathias Krause int err; 19451da177e4SLinus Torvalds 19467deb2264SThomas Graf skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 19471da177e4SLinus Torvalds if (!skb) 19481da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds info.in_skb = in_skb; 19511da177e4SLinus Torvalds info.out_skb = skb; 19521da177e4SLinus Torvalds info.nlmsg_seq = seq; 19531da177e4SLinus Torvalds info.nlmsg_flags = 0; 19541da177e4SLinus Torvalds 1955c2546372SMathias Krause err = dump_one_policy(xp, dir, 0, &info); 1956c2546372SMathias Krause if (err) { 19571da177e4SLinus Torvalds kfree_skb(skb); 1958c2546372SMathias Krause return ERR_PTR(err); 19591da177e4SLinus Torvalds } 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds return skb; 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds 196488d0adb5SNicolas Dichtel static int xfrm_notify_userpolicy(struct net *net) 196588d0adb5SNicolas Dichtel { 196688d0adb5SNicolas Dichtel struct xfrm_userpolicy_default *up; 196788d0adb5SNicolas Dichtel int len = NLMSG_ALIGN(sizeof(*up)); 196888d0adb5SNicolas Dichtel struct nlmsghdr *nlh; 196988d0adb5SNicolas Dichtel struct sk_buff *skb; 1970*93ec1320SNicolas Dichtel int err; 197188d0adb5SNicolas Dichtel 197288d0adb5SNicolas Dichtel skb = nlmsg_new(len, GFP_ATOMIC); 197388d0adb5SNicolas Dichtel if (skb == NULL) 197488d0adb5SNicolas Dichtel return -ENOMEM; 197588d0adb5SNicolas Dichtel 197688d0adb5SNicolas Dichtel nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0); 197788d0adb5SNicolas Dichtel if (nlh == NULL) { 197888d0adb5SNicolas Dichtel kfree_skb(skb); 197988d0adb5SNicolas Dichtel return -EMSGSIZE; 198088d0adb5SNicolas Dichtel } 198188d0adb5SNicolas Dichtel 198288d0adb5SNicolas Dichtel up = nlmsg_data(nlh); 198388d0adb5SNicolas Dichtel up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ? 198488d0adb5SNicolas Dichtel XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT; 198588d0adb5SNicolas Dichtel up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ? 198688d0adb5SNicolas Dichtel XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT; 198788d0adb5SNicolas Dichtel up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ? 198888d0adb5SNicolas Dichtel XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT; 198988d0adb5SNicolas Dichtel 199088d0adb5SNicolas Dichtel nlmsg_end(skb, nlh); 199188d0adb5SNicolas Dichtel 1992*93ec1320SNicolas Dichtel rcu_read_lock(); 1993*93ec1320SNicolas Dichtel err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 1994*93ec1320SNicolas Dichtel rcu_read_unlock(); 1995*93ec1320SNicolas Dichtel 1996*93ec1320SNicolas Dichtel return err; 199788d0adb5SNicolas Dichtel } 199888d0adb5SNicolas Dichtel 19992d151d39SSteffen Klassert static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh, 20002d151d39SSteffen Klassert struct nlattr **attrs) 20012d151d39SSteffen Klassert { 20022d151d39SSteffen Klassert struct net *net = sock_net(skb->sk); 20032d151d39SSteffen Klassert struct xfrm_userpolicy_default *up = nlmsg_data(nlh); 20042d151d39SSteffen Klassert 2005f8d858e6SNicolas Dichtel if (up->in == XFRM_USERPOLICY_BLOCK) 2006f8d858e6SNicolas Dichtel net->xfrm.policy_default |= XFRM_POL_DEFAULT_IN; 2007f8d858e6SNicolas Dichtel else if (up->in == XFRM_USERPOLICY_ACCEPT) 2008f8d858e6SNicolas Dichtel net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_IN; 20095d8dbb7fSPavel Skripkin 2010f8d858e6SNicolas Dichtel if (up->fwd == XFRM_USERPOLICY_BLOCK) 2011f8d858e6SNicolas Dichtel net->xfrm.policy_default |= XFRM_POL_DEFAULT_FWD; 2012f8d858e6SNicolas Dichtel else if (up->fwd == XFRM_USERPOLICY_ACCEPT) 2013f8d858e6SNicolas Dichtel net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_FWD; 20145d8dbb7fSPavel Skripkin 2015f8d858e6SNicolas Dichtel if (up->out == XFRM_USERPOLICY_BLOCK) 2016f8d858e6SNicolas Dichtel net->xfrm.policy_default |= XFRM_POL_DEFAULT_OUT; 2017f8d858e6SNicolas Dichtel else if (up->out == XFRM_USERPOLICY_ACCEPT) 2018f8d858e6SNicolas Dichtel net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_OUT; 20192d151d39SSteffen Klassert 20202d151d39SSteffen Klassert rt_genid_bump_all(net); 20212d151d39SSteffen Klassert 202288d0adb5SNicolas Dichtel xfrm_notify_userpolicy(net); 20232d151d39SSteffen Klassert return 0; 20242d151d39SSteffen Klassert } 20252d151d39SSteffen Klassert 20262d151d39SSteffen Klassert static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh, 20272d151d39SSteffen Klassert struct nlattr **attrs) 20282d151d39SSteffen Klassert { 20292d151d39SSteffen Klassert struct sk_buff *r_skb; 20302d151d39SSteffen Klassert struct nlmsghdr *r_nlh; 20312d151d39SSteffen Klassert struct net *net = sock_net(skb->sk); 2032f8d858e6SNicolas Dichtel struct xfrm_userpolicy_default *r_up; 20332d151d39SSteffen Klassert int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default)); 20342d151d39SSteffen Klassert u32 portid = NETLINK_CB(skb).portid; 20352d151d39SSteffen Klassert u32 seq = nlh->nlmsg_seq; 20362d151d39SSteffen Klassert 20372d151d39SSteffen Klassert r_skb = nlmsg_new(len, GFP_ATOMIC); 20382d151d39SSteffen Klassert if (!r_skb) 20392d151d39SSteffen Klassert return -ENOMEM; 20402d151d39SSteffen Klassert 20412d151d39SSteffen Klassert r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0); 20422d151d39SSteffen Klassert if (!r_nlh) { 20432d151d39SSteffen Klassert kfree_skb(r_skb); 20442d151d39SSteffen Klassert return -EMSGSIZE; 20452d151d39SSteffen Klassert } 20462d151d39SSteffen Klassert 20472d151d39SSteffen Klassert r_up = nlmsg_data(r_nlh); 20482d151d39SSteffen Klassert 2049f8d858e6SNicolas Dichtel r_up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ? 2050f8d858e6SNicolas Dichtel XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT; 2051f8d858e6SNicolas Dichtel r_up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ? 2052f8d858e6SNicolas Dichtel XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT; 2053f8d858e6SNicolas Dichtel r_up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ? 2054f8d858e6SNicolas Dichtel XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT; 20552d151d39SSteffen Klassert nlmsg_end(r_skb, r_nlh); 20562d151d39SSteffen Klassert 20572d151d39SSteffen Klassert return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid); 20582d151d39SSteffen Klassert } 20592d151d39SSteffen Klassert 206022e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 20615424f32eSThomas Graf struct nlattr **attrs) 20621da177e4SLinus Torvalds { 2063fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 20641da177e4SLinus Torvalds struct xfrm_policy *xp; 20651da177e4SLinus Torvalds struct xfrm_userpolicy_id *p; 2066b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 20671da177e4SLinus Torvalds int err; 206826b15dadSJamal Hadi Salim struct km_event c; 20691da177e4SLinus Torvalds int delete; 2070295fae56SJamal Hadi Salim struct xfrm_mark m; 20717e652640SSteffen Klassert u32 if_id = 0; 20721da177e4SLinus Torvalds 20737b67c857SThomas Graf p = nlmsg_data(nlh); 20741da177e4SLinus Torvalds delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; 20751da177e4SLinus Torvalds 207635a7aa08SThomas Graf err = copy_from_user_policy_type(&type, attrs); 2077f7b6983fSMasahide NAKAMURA if (err) 2078f7b6983fSMasahide NAKAMURA return err; 2079f7b6983fSMasahide NAKAMURA 20801da177e4SLinus Torvalds err = verify_policy_dir(p->dir); 20811da177e4SLinus Torvalds if (err) 20821da177e4SLinus Torvalds return err; 20831da177e4SLinus Torvalds 20847e652640SSteffen Klassert if (attrs[XFRMA_IF_ID]) 20857e652640SSteffen Klassert if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 20867e652640SSteffen Klassert 20874f47e8abSXin Long xfrm_mark_get(attrs, &m); 20884f47e8abSXin Long 20891da177e4SLinus Torvalds if (p->index) 20904f47e8abSXin Long xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, 20914f47e8abSXin Long p->index, delete, &err); 2092df71837dSTrent Jaeger else { 20935424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 209403e1ad7bSPaul Moore struct xfrm_sec_ctx *ctx; 2095df71837dSTrent Jaeger 209635a7aa08SThomas Graf err = verify_sec_ctx_len(attrs); 2097df71837dSTrent Jaeger if (err) 2098df71837dSTrent Jaeger return err; 2099df71837dSTrent Jaeger 21002c8dd116SDenis V. Lunev ctx = NULL; 2101df71837dSTrent Jaeger if (rt) { 21025424f32eSThomas Graf struct xfrm_user_sec_ctx *uctx = nla_data(rt); 2103df71837dSTrent Jaeger 210452a4c640SNikolay Aleksandrov err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 210503e1ad7bSPaul Moore if (err) 2106df71837dSTrent Jaeger return err; 21072c8dd116SDenis V. Lunev } 21084f47e8abSXin Long xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir, 21094f47e8abSXin Long &p->sel, ctx, delete, &err); 211003e1ad7bSPaul Moore security_xfrm_policy_free(ctx); 2111df71837dSTrent Jaeger } 21121da177e4SLinus Torvalds if (xp == NULL) 21131da177e4SLinus Torvalds return -ENOENT; 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds if (!delete) { 21161da177e4SLinus Torvalds struct sk_buff *resp_skb; 21171da177e4SLinus Torvalds 21181da177e4SLinus Torvalds resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); 21191da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 21201da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 21211da177e4SLinus Torvalds } else { 2122a6483b79SAlexey Dobriyan err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, 212315e47304SEric W. Biederman NETLINK_CB(skb).portid); 21241da177e4SLinus Torvalds } 212526b15dadSJamal Hadi Salim } else { 21262e71029eSTetsuo Handa xfrm_audit_policy_delete(xp, err ? 0 : 1, true); 212713fcfbb0SDavid S. Miller 212813fcfbb0SDavid S. Miller if (err != 0) 2129c8c05a8eSCatherine Zhang goto out; 213013fcfbb0SDavid S. Miller 2131e7443892SHerbert Xu c.data.byid = p->index; 2132f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 213326b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 213415e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 213526b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 21361da177e4SLinus Torvalds } 21371da177e4SLinus Torvalds 2138c8c05a8eSCatherine Zhang out: 2139ef41aaa0SEric Paris xfrm_pol_put(xp); 21401da177e4SLinus Torvalds return err; 21411da177e4SLinus Torvalds } 21421da177e4SLinus Torvalds 214322e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 21445424f32eSThomas Graf struct nlattr **attrs) 21451da177e4SLinus Torvalds { 2146fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 214726b15dadSJamal Hadi Salim struct km_event c; 21487b67c857SThomas Graf struct xfrm_usersa_flush *p = nlmsg_data(nlh); 21494aa2e62cSJoy Latten int err; 21501da177e4SLinus Torvalds 2151f75a2804SCong Wang err = xfrm_state_flush(net, p->proto, true, false); 21529e64cc95SJamal Hadi Salim if (err) { 21539e64cc95SJamal Hadi Salim if (err == -ESRCH) /* empty table */ 21549e64cc95SJamal Hadi Salim return 0; 2155069c474eSDavid S. Miller return err; 21569e64cc95SJamal Hadi Salim } 2157bf08867fSHerbert Xu c.data.proto = p->proto; 2158f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 215926b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 216015e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 21617067802eSAlexey Dobriyan c.net = net; 216226b15dadSJamal Hadi Salim km_state_notify(NULL, &c); 216326b15dadSJamal Hadi Salim 21641da177e4SLinus Torvalds return 0; 21651da177e4SLinus Torvalds } 21661da177e4SLinus Torvalds 2167a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x) 21687deb2264SThomas Graf { 2169a1b831f2SAlexey Dobriyan unsigned int replay_size = x->replay_esn ? 2170d8647b79SSteffen Klassert xfrm_replay_state_esn_len(x->replay_esn) : 2171d8647b79SSteffen Klassert sizeof(struct xfrm_replay_state); 2172d8647b79SSteffen Klassert 21737deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) 2174d8647b79SSteffen Klassert + nla_total_size(replay_size) 2175de95c4a4SNicolas Dichtel + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur)) 21766f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 21777deb2264SThomas Graf + nla_total_size(4) /* XFRM_AE_RTHR */ 21787deb2264SThomas Graf + nla_total_size(4); /* XFRM_AE_ETHR */ 21797deb2264SThomas Graf } 2180d51d081dSJamal Hadi Salim 2181214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 2182d51d081dSJamal Hadi Salim { 2183d51d081dSJamal Hadi Salim struct xfrm_aevent_id *id; 2184d51d081dSJamal Hadi Salim struct nlmsghdr *nlh; 21851d1e34ddSDavid S. Miller int err; 2186d51d081dSJamal Hadi Salim 218715e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); 218879b8b7f4SThomas Graf if (nlh == NULL) 218979b8b7f4SThomas Graf return -EMSGSIZE; 2190d51d081dSJamal Hadi Salim 21917b67c857SThomas Graf id = nlmsg_data(nlh); 2192931e79d7SMathias Krause memset(&id->sa_id, 0, sizeof(id->sa_id)); 21932b5f6dccSJamal Hadi Salim memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr)); 2194d51d081dSJamal Hadi Salim id->sa_id.spi = x->id.spi; 2195d51d081dSJamal Hadi Salim id->sa_id.family = x->props.family; 2196d51d081dSJamal Hadi Salim id->sa_id.proto = x->id.proto; 21972b5f6dccSJamal Hadi Salim memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr)); 21982b5f6dccSJamal Hadi Salim id->reqid = x->props.reqid; 2199d51d081dSJamal Hadi Salim id->flags = c->data.aevent; 2200d51d081dSJamal Hadi Salim 2201d0fde795SDavid S. Miller if (x->replay_esn) { 22021d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 2203d8647b79SSteffen Klassert xfrm_replay_state_esn_len(x->replay_esn), 22041d1e34ddSDavid S. Miller x->replay_esn); 2205d0fde795SDavid S. Miller } else { 22061d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 22071d1e34ddSDavid S. Miller &x->replay); 2208d0fde795SDavid S. Miller } 22091d1e34ddSDavid S. Miller if (err) 22101d1e34ddSDavid S. Miller goto out_cancel; 2211de95c4a4SNicolas Dichtel err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft, 2212de95c4a4SNicolas Dichtel XFRMA_PAD); 22131d1e34ddSDavid S. Miller if (err) 22141d1e34ddSDavid S. Miller goto out_cancel; 2215d8647b79SSteffen Klassert 22161d1e34ddSDavid S. Miller if (id->flags & XFRM_AE_RTHR) { 22171d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); 22181d1e34ddSDavid S. Miller if (err) 22191d1e34ddSDavid S. Miller goto out_cancel; 22201d1e34ddSDavid S. Miller } 22211d1e34ddSDavid S. Miller if (id->flags & XFRM_AE_ETHR) { 22221d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_ETIMER_THRESH, 22231d1e34ddSDavid S. Miller x->replay_maxage * 10 / HZ); 22241d1e34ddSDavid S. Miller if (err) 22251d1e34ddSDavid S. Miller goto out_cancel; 22261d1e34ddSDavid S. Miller } 22271d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &x->mark); 22281d1e34ddSDavid S. Miller if (err) 22291d1e34ddSDavid S. Miller goto out_cancel; 22306f26b61eSJamal Hadi Salim 22317e652640SSteffen Klassert err = xfrm_if_id_put(skb, x->if_id); 22327e652640SSteffen Klassert if (err) 22337e652640SSteffen Klassert goto out_cancel; 22347e652640SSteffen Klassert 2235053c095aSJohannes Berg nlmsg_end(skb, nlh); 2236053c095aSJohannes Berg return 0; 2237d51d081dSJamal Hadi Salim 22381d1e34ddSDavid S. Miller out_cancel: 22399825069dSThomas Graf nlmsg_cancel(skb, nlh); 22401d1e34ddSDavid S. Miller return err; 2241d51d081dSJamal Hadi Salim } 2242d51d081dSJamal Hadi Salim 224322e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 22445424f32eSThomas Graf struct nlattr **attrs) 2245d51d081dSJamal Hadi Salim { 2246fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 2247d51d081dSJamal Hadi Salim struct xfrm_state *x; 2248d51d081dSJamal Hadi Salim struct sk_buff *r_skb; 2249d51d081dSJamal Hadi Salim int err; 2250d51d081dSJamal Hadi Salim struct km_event c; 22516f26b61eSJamal Hadi Salim u32 mark; 22526f26b61eSJamal Hadi Salim struct xfrm_mark m; 22537b67c857SThomas Graf struct xfrm_aevent_id *p = nlmsg_data(nlh); 2254d51d081dSJamal Hadi Salim struct xfrm_usersa_id *id = &p->sa_id; 2255d51d081dSJamal Hadi Salim 22566f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 22576f26b61eSJamal Hadi Salim 22586f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family); 2259d8647b79SSteffen Klassert if (x == NULL) 2260d51d081dSJamal Hadi Salim return -ESRCH; 2261d8647b79SSteffen Klassert 2262d8647b79SSteffen Klassert r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 2263d8647b79SSteffen Klassert if (r_skb == NULL) { 2264d8647b79SSteffen Klassert xfrm_state_put(x); 2265d8647b79SSteffen Klassert return -ENOMEM; 2266d51d081dSJamal Hadi Salim } 2267d51d081dSJamal Hadi Salim 2268d51d081dSJamal Hadi Salim /* 2269d51d081dSJamal Hadi Salim * XXX: is this lock really needed - none of the other 2270d51d081dSJamal Hadi Salim * gets lock (the concern is things getting updated 2271d51d081dSJamal Hadi Salim * while we are still reading) - jhs 2272d51d081dSJamal Hadi Salim */ 2273d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 2274d51d081dSJamal Hadi Salim c.data.aevent = p->flags; 2275d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 227615e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 2277d51d081dSJamal Hadi Salim 22782fc5f83bSGustavo A. R. Silva err = build_aevent(r_skb, x, &c); 22792fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 22802fc5f83bSGustavo A. R. Silva 228115e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid); 2282d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 2283d51d081dSJamal Hadi Salim xfrm_state_put(x); 2284d51d081dSJamal Hadi Salim return err; 2285d51d081dSJamal Hadi Salim } 2286d51d081dSJamal Hadi Salim 228722e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 22885424f32eSThomas Graf struct nlattr **attrs) 2289d51d081dSJamal Hadi Salim { 2290fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 2291d51d081dSJamal Hadi Salim struct xfrm_state *x; 2292d51d081dSJamal Hadi Salim struct km_event c; 2293d51d081dSJamal Hadi Salim int err = -EINVAL; 22946f26b61eSJamal Hadi Salim u32 mark = 0; 22956f26b61eSJamal Hadi Salim struct xfrm_mark m; 22967b67c857SThomas Graf struct xfrm_aevent_id *p = nlmsg_data(nlh); 22975424f32eSThomas Graf struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 2298d8647b79SSteffen Klassert struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL]; 22995424f32eSThomas Graf struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 23004e077237SMichael Rossberg struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; 23014e077237SMichael Rossberg struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; 2302d51d081dSJamal Hadi Salim 23034e077237SMichael Rossberg if (!lt && !rp && !re && !et && !rt) 2304d51d081dSJamal Hadi Salim return err; 2305d51d081dSJamal Hadi Salim 2306d51d081dSJamal Hadi Salim /* pedantic mode - thou shalt sayeth replaceth */ 2307d51d081dSJamal Hadi Salim if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) 2308d51d081dSJamal Hadi Salim return err; 2309d51d081dSJamal Hadi Salim 23106f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 23116f26b61eSJamal Hadi Salim 23126f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); 2313d51d081dSJamal Hadi Salim if (x == NULL) 2314d51d081dSJamal Hadi Salim return -ESRCH; 2315d51d081dSJamal Hadi Salim 2316d51d081dSJamal Hadi Salim if (x->km.state != XFRM_STATE_VALID) 2317d51d081dSJamal Hadi Salim goto out; 2318d51d081dSJamal Hadi Salim 23194479ff76SSteffen Klassert err = xfrm_replay_verify_len(x->replay_esn, re); 2320e2b19125SSteffen Klassert if (err) 2321e2b19125SSteffen Klassert goto out; 2322e2b19125SSteffen Klassert 2323d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 2324e3ac104dSMathias Krause xfrm_update_ae_params(x, attrs, 1); 2325d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 2326d51d081dSJamal Hadi Salim 2327d51d081dSJamal Hadi Salim c.event = nlh->nlmsg_type; 2328d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 232915e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 2330d51d081dSJamal Hadi Salim c.data.aevent = XFRM_AE_CU; 2331d51d081dSJamal Hadi Salim km_state_notify(x, &c); 2332d51d081dSJamal Hadi Salim err = 0; 2333d51d081dSJamal Hadi Salim out: 2334d51d081dSJamal Hadi Salim xfrm_state_put(x); 2335d51d081dSJamal Hadi Salim return err; 2336d51d081dSJamal Hadi Salim } 2337d51d081dSJamal Hadi Salim 233822e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 23395424f32eSThomas Graf struct nlattr **attrs) 23401da177e4SLinus Torvalds { 2341fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 234226b15dadSJamal Hadi Salim struct km_event c; 2343b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 2344f7b6983fSMasahide NAKAMURA int err; 234526b15dadSJamal Hadi Salim 234635a7aa08SThomas Graf err = copy_from_user_policy_type(&type, attrs); 2347f7b6983fSMasahide NAKAMURA if (err) 2348f7b6983fSMasahide NAKAMURA return err; 2349f7b6983fSMasahide NAKAMURA 23502e71029eSTetsuo Handa err = xfrm_policy_flush(net, type, true); 23512f1eb65fSJamal Hadi Salim if (err) { 23522f1eb65fSJamal Hadi Salim if (err == -ESRCH) /* empty table */ 23532f1eb65fSJamal Hadi Salim return 0; 2354069c474eSDavid S. Miller return err; 23552f1eb65fSJamal Hadi Salim } 23562f1eb65fSJamal Hadi Salim 2357f7b6983fSMasahide NAKAMURA c.data.type = type; 2358f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 235926b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 236015e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 23617067802eSAlexey Dobriyan c.net = net; 236226b15dadSJamal Hadi Salim km_policy_notify(NULL, 0, &c); 23631da177e4SLinus Torvalds return 0; 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds 236622e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 23675424f32eSThomas Graf struct nlattr **attrs) 23686c5c8ca7SJamal Hadi Salim { 2369fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 23706c5c8ca7SJamal Hadi Salim struct xfrm_policy *xp; 23717b67c857SThomas Graf struct xfrm_user_polexpire *up = nlmsg_data(nlh); 23726c5c8ca7SJamal Hadi Salim struct xfrm_userpolicy_info *p = &up->pol; 2373b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 23746c5c8ca7SJamal Hadi Salim int err = -ENOENT; 2375295fae56SJamal Hadi Salim struct xfrm_mark m; 23767e652640SSteffen Klassert u32 if_id = 0; 23776c5c8ca7SJamal Hadi Salim 237835a7aa08SThomas Graf err = copy_from_user_policy_type(&type, attrs); 2379f7b6983fSMasahide NAKAMURA if (err) 2380f7b6983fSMasahide NAKAMURA return err; 2381f7b6983fSMasahide NAKAMURA 2382c8bf4d04STimo Teräs err = verify_policy_dir(p->dir); 2383c8bf4d04STimo Teräs if (err) 2384c8bf4d04STimo Teräs return err; 2385c8bf4d04STimo Teräs 23867e652640SSteffen Klassert if (attrs[XFRMA_IF_ID]) 23877e652640SSteffen Klassert if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 23887e652640SSteffen Klassert 23894f47e8abSXin Long xfrm_mark_get(attrs, &m); 23904f47e8abSXin Long 23916c5c8ca7SJamal Hadi Salim if (p->index) 23924f47e8abSXin Long xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index, 23934f47e8abSXin Long 0, &err); 23946c5c8ca7SJamal Hadi Salim else { 23955424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 239603e1ad7bSPaul Moore struct xfrm_sec_ctx *ctx; 23976c5c8ca7SJamal Hadi Salim 239835a7aa08SThomas Graf err = verify_sec_ctx_len(attrs); 23996c5c8ca7SJamal Hadi Salim if (err) 24006c5c8ca7SJamal Hadi Salim return err; 24016c5c8ca7SJamal Hadi Salim 24022c8dd116SDenis V. Lunev ctx = NULL; 24036c5c8ca7SJamal Hadi Salim if (rt) { 24045424f32eSThomas Graf struct xfrm_user_sec_ctx *uctx = nla_data(rt); 24056c5c8ca7SJamal Hadi Salim 240652a4c640SNikolay Aleksandrov err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 240703e1ad7bSPaul Moore if (err) 24086c5c8ca7SJamal Hadi Salim return err; 24092c8dd116SDenis V. Lunev } 24104f47e8abSXin Long xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir, 24116f26b61eSJamal Hadi Salim &p->sel, ctx, 0, &err); 241203e1ad7bSPaul Moore security_xfrm_policy_free(ctx); 24136c5c8ca7SJamal Hadi Salim } 24146c5c8ca7SJamal Hadi Salim if (xp == NULL) 2415ef41aaa0SEric Paris return -ENOENT; 241603e1ad7bSPaul Moore 2417ea2dea9dSTimo Teräs if (unlikely(xp->walk.dead)) 24186c5c8ca7SJamal Hadi Salim goto out; 24196c5c8ca7SJamal Hadi Salim 24206c5c8ca7SJamal Hadi Salim err = 0; 24216c5c8ca7SJamal Hadi Salim if (up->hard) { 24226c5c8ca7SJamal Hadi Salim xfrm_policy_delete(xp, p->dir); 24232e71029eSTetsuo Handa xfrm_audit_policy_delete(xp, 1, true); 24246c5c8ca7SJamal Hadi Salim } 2425c6bb8136SEric W. Biederman km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid); 24266c5c8ca7SJamal Hadi Salim 24276c5c8ca7SJamal Hadi Salim out: 24286c5c8ca7SJamal Hadi Salim xfrm_pol_put(xp); 24296c5c8ca7SJamal Hadi Salim return err; 24306c5c8ca7SJamal Hadi Salim } 24316c5c8ca7SJamal Hadi Salim 243222e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 24335424f32eSThomas Graf struct nlattr **attrs) 243453bc6b4dSJamal Hadi Salim { 2435fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 243653bc6b4dSJamal Hadi Salim struct xfrm_state *x; 243753bc6b4dSJamal Hadi Salim int err; 24387b67c857SThomas Graf struct xfrm_user_expire *ue = nlmsg_data(nlh); 243953bc6b4dSJamal Hadi Salim struct xfrm_usersa_info *p = &ue->state; 24406f26b61eSJamal Hadi Salim struct xfrm_mark m; 2441928497f0SNicolas Dichtel u32 mark = xfrm_mark_get(attrs, &m); 244253bc6b4dSJamal Hadi Salim 24436f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family); 244453bc6b4dSJamal Hadi Salim 24453a765aa5SDavid S. Miller err = -ENOENT; 244653bc6b4dSJamal Hadi Salim if (x == NULL) 244753bc6b4dSJamal Hadi Salim return err; 244853bc6b4dSJamal Hadi Salim 244953bc6b4dSJamal Hadi Salim spin_lock_bh(&x->lock); 24503a765aa5SDavid S. Miller err = -EINVAL; 245153bc6b4dSJamal Hadi Salim if (x->km.state != XFRM_STATE_VALID) 245253bc6b4dSJamal Hadi Salim goto out; 2453c6bb8136SEric W. Biederman km_state_expired(x, ue->hard, nlh->nlmsg_pid); 245453bc6b4dSJamal Hadi Salim 2455161a09e7SJoy Latten if (ue->hard) { 245653bc6b4dSJamal Hadi Salim __xfrm_state_delete(x); 24572e71029eSTetsuo Handa xfrm_audit_state_delete(x, 1, true); 2458161a09e7SJoy Latten } 24593a765aa5SDavid S. Miller err = 0; 246053bc6b4dSJamal Hadi Salim out: 246153bc6b4dSJamal Hadi Salim spin_unlock_bh(&x->lock); 246253bc6b4dSJamal Hadi Salim xfrm_state_put(x); 246353bc6b4dSJamal Hadi Salim return err; 246453bc6b4dSJamal Hadi Salim } 246553bc6b4dSJamal Hadi Salim 246622e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, 24675424f32eSThomas Graf struct nlattr **attrs) 2468980ebd25SJamal Hadi Salim { 2469fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 2470980ebd25SJamal Hadi Salim struct xfrm_policy *xp; 2471980ebd25SJamal Hadi Salim struct xfrm_user_tmpl *ut; 2472980ebd25SJamal Hadi Salim int i; 24735424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_TMPL]; 24746f26b61eSJamal Hadi Salim struct xfrm_mark mark; 2475980ebd25SJamal Hadi Salim 24767b67c857SThomas Graf struct xfrm_user_acquire *ua = nlmsg_data(nlh); 2477fc34acd3SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 2478980ebd25SJamal Hadi Salim int err = -ENOMEM; 2479980ebd25SJamal Hadi Salim 2480980ebd25SJamal Hadi Salim if (!x) 2481d8eb9307SIlpo Järvinen goto nomem; 2482980ebd25SJamal Hadi Salim 24836f26b61eSJamal Hadi Salim xfrm_mark_get(attrs, &mark); 24846f26b61eSJamal Hadi Salim 2485980ebd25SJamal Hadi Salim err = verify_newpolicy_info(&ua->policy); 2486d8eb9307SIlpo Järvinen if (err) 248773efc324SVegard Nossum goto free_state; 2488a1a7e3a3SXin Long err = verify_sec_ctx_len(attrs); 2489a1a7e3a3SXin Long if (err) 2490a1a7e3a3SXin Long goto free_state; 2491980ebd25SJamal Hadi Salim 2492980ebd25SJamal Hadi Salim /* build an XP */ 2493fc34acd3SAlexey Dobriyan xp = xfrm_policy_construct(net, &ua->policy, attrs, &err); 2494d8eb9307SIlpo Järvinen if (!xp) 2495d8eb9307SIlpo Järvinen goto free_state; 2496980ebd25SJamal Hadi Salim 2497980ebd25SJamal Hadi Salim memcpy(&x->id, &ua->id, sizeof(ua->id)); 2498980ebd25SJamal Hadi Salim memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); 2499980ebd25SJamal Hadi Salim memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); 25006f26b61eSJamal Hadi Salim xp->mark.m = x->mark.m = mark.m; 25016f26b61eSJamal Hadi Salim xp->mark.v = x->mark.v = mark.v; 25025424f32eSThomas Graf ut = nla_data(rt); 2503980ebd25SJamal Hadi Salim /* extract the templates and for each call km_key */ 2504980ebd25SJamal Hadi Salim for (i = 0; i < xp->xfrm_nr; i++, ut++) { 2505980ebd25SJamal Hadi Salim struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 2506980ebd25SJamal Hadi Salim memcpy(&x->id, &t->id, sizeof(x->id)); 2507980ebd25SJamal Hadi Salim x->props.mode = t->mode; 2508980ebd25SJamal Hadi Salim x->props.reqid = t->reqid; 2509980ebd25SJamal Hadi Salim x->props.family = ut->family; 2510980ebd25SJamal Hadi Salim t->aalgos = ua->aalgos; 2511980ebd25SJamal Hadi Salim t->ealgos = ua->ealgos; 2512980ebd25SJamal Hadi Salim t->calgos = ua->calgos; 2513980ebd25SJamal Hadi Salim err = km_query(x, t, xp); 2514980ebd25SJamal Hadi Salim 2515980ebd25SJamal Hadi Salim } 2516980ebd25SJamal Hadi Salim 25174a135e53SMathias Krause xfrm_state_free(x); 2518980ebd25SJamal Hadi Salim kfree(xp); 2519980ebd25SJamal Hadi Salim 2520980ebd25SJamal Hadi Salim return 0; 2521d8eb9307SIlpo Järvinen 2522d8eb9307SIlpo Järvinen free_state: 25234a135e53SMathias Krause xfrm_state_free(x); 2524d8eb9307SIlpo Järvinen nomem: 2525d8eb9307SIlpo Järvinen return err; 2526980ebd25SJamal Hadi Salim } 2527980ebd25SJamal Hadi Salim 25285c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 25295c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma, 253013c1d189SArnaud Ebalard struct xfrm_kmaddress *k, 25315424f32eSThomas Graf struct nlattr **attrs, int *num) 25325c79de6eSShinta Sugimoto { 25335424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_MIGRATE]; 25345c79de6eSShinta Sugimoto struct xfrm_user_migrate *um; 25355c79de6eSShinta Sugimoto int i, num_migrate; 25365c79de6eSShinta Sugimoto 253713c1d189SArnaud Ebalard if (k != NULL) { 253813c1d189SArnaud Ebalard struct xfrm_user_kmaddress *uk; 253913c1d189SArnaud Ebalard 254013c1d189SArnaud Ebalard uk = nla_data(attrs[XFRMA_KMADDRESS]); 254113c1d189SArnaud Ebalard memcpy(&k->local, &uk->local, sizeof(k->local)); 254213c1d189SArnaud Ebalard memcpy(&k->remote, &uk->remote, sizeof(k->remote)); 254313c1d189SArnaud Ebalard k->family = uk->family; 254413c1d189SArnaud Ebalard k->reserved = uk->reserved; 254513c1d189SArnaud Ebalard } 254613c1d189SArnaud Ebalard 25475424f32eSThomas Graf um = nla_data(rt); 25485424f32eSThomas Graf num_migrate = nla_len(rt) / sizeof(*um); 25495c79de6eSShinta Sugimoto 25505c79de6eSShinta Sugimoto if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) 25515c79de6eSShinta Sugimoto return -EINVAL; 25525c79de6eSShinta Sugimoto 25535c79de6eSShinta Sugimoto for (i = 0; i < num_migrate; i++, um++, ma++) { 25545c79de6eSShinta Sugimoto memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); 25555c79de6eSShinta Sugimoto memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); 25565c79de6eSShinta Sugimoto memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); 25575c79de6eSShinta Sugimoto memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); 25585c79de6eSShinta Sugimoto 25595c79de6eSShinta Sugimoto ma->proto = um->proto; 25605c79de6eSShinta Sugimoto ma->mode = um->mode; 25615c79de6eSShinta Sugimoto ma->reqid = um->reqid; 25625c79de6eSShinta Sugimoto 25635c79de6eSShinta Sugimoto ma->old_family = um->old_family; 25645c79de6eSShinta Sugimoto ma->new_family = um->new_family; 25655c79de6eSShinta Sugimoto } 25665c79de6eSShinta Sugimoto 25675c79de6eSShinta Sugimoto *num = i; 25685c79de6eSShinta Sugimoto return 0; 25695c79de6eSShinta Sugimoto } 25705c79de6eSShinta Sugimoto 25715c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 25725424f32eSThomas Graf struct nlattr **attrs) 25735c79de6eSShinta Sugimoto { 25747b67c857SThomas Graf struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); 25755c79de6eSShinta Sugimoto struct xfrm_migrate m[XFRM_MAX_DEPTH]; 257613c1d189SArnaud Ebalard struct xfrm_kmaddress km, *kmp; 25775c79de6eSShinta Sugimoto u8 type; 25785c79de6eSShinta Sugimoto int err; 25795c79de6eSShinta Sugimoto int n = 0; 25808d549c4fSFan Du struct net *net = sock_net(skb->sk); 25814ab47d47SAntony Antony struct xfrm_encap_tmpl *encap = NULL; 25825c79de6eSShinta Sugimoto 258335a7aa08SThomas Graf if (attrs[XFRMA_MIGRATE] == NULL) 2584cf5cb79fSThomas Graf return -EINVAL; 25855c79de6eSShinta Sugimoto 258613c1d189SArnaud Ebalard kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL; 258713c1d189SArnaud Ebalard 25885424f32eSThomas Graf err = copy_from_user_policy_type(&type, attrs); 25895c79de6eSShinta Sugimoto if (err) 25905c79de6eSShinta Sugimoto return err; 25915c79de6eSShinta Sugimoto 259213c1d189SArnaud Ebalard err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n); 25935c79de6eSShinta Sugimoto if (err) 25945c79de6eSShinta Sugimoto return err; 25955c79de6eSShinta Sugimoto 25965c79de6eSShinta Sugimoto if (!n) 25975c79de6eSShinta Sugimoto return 0; 25985c79de6eSShinta Sugimoto 25994ab47d47SAntony Antony if (attrs[XFRMA_ENCAP]) { 26004ab47d47SAntony Antony encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), 26014ab47d47SAntony Antony sizeof(*encap), GFP_KERNEL); 26024ab47d47SAntony Antony if (!encap) 26034ac7a6eeSZheng Yongjun return -ENOMEM; 26045c79de6eSShinta Sugimoto } 26054ab47d47SAntony Antony 26064ab47d47SAntony Antony err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap); 26074ab47d47SAntony Antony 26084ab47d47SAntony Antony kfree(encap); 26094ab47d47SAntony Antony 26104ab47d47SAntony Antony return err; 26114ab47d47SAntony Antony } 26125c79de6eSShinta Sugimoto #else 26135c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 26145424f32eSThomas Graf struct nlattr **attrs) 26155c79de6eSShinta Sugimoto { 26165c79de6eSShinta Sugimoto return -ENOPROTOOPT; 26175c79de6eSShinta Sugimoto } 26185c79de6eSShinta Sugimoto #endif 26195c79de6eSShinta Sugimoto 26205c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 2621183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb) 26225c79de6eSShinta Sugimoto { 26235c79de6eSShinta Sugimoto struct xfrm_user_migrate um; 26245c79de6eSShinta Sugimoto 26255c79de6eSShinta Sugimoto memset(&um, 0, sizeof(um)); 26265c79de6eSShinta Sugimoto um.proto = m->proto; 26275c79de6eSShinta Sugimoto um.mode = m->mode; 26285c79de6eSShinta Sugimoto um.reqid = m->reqid; 26295c79de6eSShinta Sugimoto um.old_family = m->old_family; 26305c79de6eSShinta Sugimoto memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); 26315c79de6eSShinta Sugimoto memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); 26325c79de6eSShinta Sugimoto um.new_family = m->new_family; 26335c79de6eSShinta Sugimoto memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); 26345c79de6eSShinta Sugimoto memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); 26355c79de6eSShinta Sugimoto 2636c0144beaSThomas Graf return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); 26375c79de6eSShinta Sugimoto } 26385c79de6eSShinta Sugimoto 2639183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb) 264013c1d189SArnaud Ebalard { 264113c1d189SArnaud Ebalard struct xfrm_user_kmaddress uk; 264213c1d189SArnaud Ebalard 264313c1d189SArnaud Ebalard memset(&uk, 0, sizeof(uk)); 264413c1d189SArnaud Ebalard uk.family = k->family; 264513c1d189SArnaud Ebalard uk.reserved = k->reserved; 264613c1d189SArnaud Ebalard memcpy(&uk.local, &k->local, sizeof(uk.local)); 2647a1caa322SArnaud Ebalard memcpy(&uk.remote, &k->remote, sizeof(uk.remote)); 264813c1d189SArnaud Ebalard 264913c1d189SArnaud Ebalard return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk); 265013c1d189SArnaud Ebalard } 265113c1d189SArnaud Ebalard 2652a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma, 26538bafd730SAntony Antony int with_encp) 26547deb2264SThomas Graf { 26557deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) 265613c1d189SArnaud Ebalard + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0) 26578bafd730SAntony Antony + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0) 26587deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) 26597deb2264SThomas Graf + userpolicy_type_attrsize(); 26607deb2264SThomas Graf } 26617deb2264SThomas Graf 2662183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m, 2663183cad12SDavid S. Miller int num_migrate, const struct xfrm_kmaddress *k, 26648bafd730SAntony Antony const struct xfrm_selector *sel, 26658bafd730SAntony Antony const struct xfrm_encap_tmpl *encap, u8 dir, u8 type) 26665c79de6eSShinta Sugimoto { 2667183cad12SDavid S. Miller const struct xfrm_migrate *mp; 26685c79de6eSShinta Sugimoto struct xfrm_userpolicy_id *pol_id; 26695c79de6eSShinta Sugimoto struct nlmsghdr *nlh; 26701d1e34ddSDavid S. Miller int i, err; 26715c79de6eSShinta Sugimoto 267279b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); 267379b8b7f4SThomas Graf if (nlh == NULL) 267479b8b7f4SThomas Graf return -EMSGSIZE; 26755c79de6eSShinta Sugimoto 26767b67c857SThomas Graf pol_id = nlmsg_data(nlh); 26775c79de6eSShinta Sugimoto /* copy data from selector, dir, and type to the pol_id */ 26785c79de6eSShinta Sugimoto memset(pol_id, 0, sizeof(*pol_id)); 26795c79de6eSShinta Sugimoto memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); 26805c79de6eSShinta Sugimoto pol_id->dir = dir; 26815c79de6eSShinta Sugimoto 26821d1e34ddSDavid S. Miller if (k != NULL) { 26831d1e34ddSDavid S. Miller err = copy_to_user_kmaddress(k, skb); 26841d1e34ddSDavid S. Miller if (err) 26851d1e34ddSDavid S. Miller goto out_cancel; 26861d1e34ddSDavid S. Miller } 26878bafd730SAntony Antony if (encap) { 26888bafd730SAntony Antony err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap); 26898bafd730SAntony Antony if (err) 26908bafd730SAntony Antony goto out_cancel; 26918bafd730SAntony Antony } 26921d1e34ddSDavid S. Miller err = copy_to_user_policy_type(type, skb); 26931d1e34ddSDavid S. Miller if (err) 26941d1e34ddSDavid S. Miller goto out_cancel; 26955c79de6eSShinta Sugimoto for (i = 0, mp = m ; i < num_migrate; i++, mp++) { 26961d1e34ddSDavid S. Miller err = copy_to_user_migrate(mp, skb); 26971d1e34ddSDavid S. Miller if (err) 26981d1e34ddSDavid S. Miller goto out_cancel; 26995c79de6eSShinta Sugimoto } 27005c79de6eSShinta Sugimoto 2701053c095aSJohannes Berg nlmsg_end(skb, nlh); 2702053c095aSJohannes Berg return 0; 27031d1e34ddSDavid S. Miller 27041d1e34ddSDavid S. Miller out_cancel: 27059825069dSThomas Graf nlmsg_cancel(skb, nlh); 27061d1e34ddSDavid S. Miller return err; 27075c79de6eSShinta Sugimoto } 27085c79de6eSShinta Sugimoto 2709183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2710183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 27118bafd730SAntony Antony const struct xfrm_kmaddress *k, 27128bafd730SAntony Antony const struct xfrm_encap_tmpl *encap) 27135c79de6eSShinta Sugimoto { 2714a6483b79SAlexey Dobriyan struct net *net = &init_net; 27155c79de6eSShinta Sugimoto struct sk_buff *skb; 27162fc5f83bSGustavo A. R. Silva int err; 27175c79de6eSShinta Sugimoto 27188bafd730SAntony Antony skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap), 27198bafd730SAntony Antony GFP_ATOMIC); 27205c79de6eSShinta Sugimoto if (skb == NULL) 27215c79de6eSShinta Sugimoto return -ENOMEM; 27225c79de6eSShinta Sugimoto 27235c79de6eSShinta Sugimoto /* build migrate */ 27242fc5f83bSGustavo A. R. Silva err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type); 27252fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 27265c79de6eSShinta Sugimoto 272721ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE); 27285c79de6eSShinta Sugimoto } 27295c79de6eSShinta Sugimoto #else 2730183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2731183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 27328bafd730SAntony Antony const struct xfrm_kmaddress *k, 27338bafd730SAntony Antony const struct xfrm_encap_tmpl *encap) 27345c79de6eSShinta Sugimoto { 27355c79de6eSShinta Sugimoto return -ENOPROTOOPT; 27365c79de6eSShinta Sugimoto } 27375c79de6eSShinta Sugimoto #endif 2738d51d081dSJamal Hadi Salim 2739a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type) 2740492b558bSThomas Graf 27415461fc0cSDmitry Safonov const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { 274266f9a259SDavid S. Miller [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2743492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2744492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2745492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2746492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2747492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2748492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), 2749980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), 275053bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), 2751492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 275266f9a259SDavid S. Miller [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 27536c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), 2754492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), 2755a7bd9a45SThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, 2756d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2757d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 275897a64b45SMasahide NAKAMURA [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), 27595c79de6eSShinta Sugimoto [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2760a7bd9a45SThomas Graf [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), 2761880a6fabSChristophe Gouault [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 2762a7bd9a45SThomas Graf [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 27632d151d39SSteffen Klassert [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default), 27642d151d39SSteffen Klassert [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default), 27651da177e4SLinus Torvalds }; 27665461fc0cSDmitry Safonov EXPORT_SYMBOL_GPL(xfrm_msg_min); 27671da177e4SLinus Torvalds 2768492b558bSThomas Graf #undef XMSGSIZE 2769492b558bSThomas Graf 27705106f4a8SDmitry Safonov const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { 2771c28e9304Sjamal [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)}, 2772c28e9304Sjamal [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)}, 2773c28e9304Sjamal [XFRMA_LASTUSED] = { .type = NLA_U64}, 2774c28e9304Sjamal [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)}, 27751a6509d9SHerbert Xu [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, 2776cf5cb79fSThomas Graf [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, 2777cf5cb79fSThomas Graf [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, 2778cf5cb79fSThomas Graf [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, 2779cf5cb79fSThomas Graf [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, 2780cf5cb79fSThomas Graf [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, 2781cf5cb79fSThomas Graf [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, 2782cf5cb79fSThomas Graf [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, 2783cf5cb79fSThomas Graf [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, 2784cf5cb79fSThomas Graf [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, 2785cf5cb79fSThomas Graf [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, 2786cf5cb79fSThomas Graf [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, 2787cf5cb79fSThomas Graf [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, 2788cf5cb79fSThomas Graf [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, 2789cf5cb79fSThomas Graf [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, 279013c1d189SArnaud Ebalard [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) }, 27916f26b61eSJamal Hadi Salim [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) }, 279235d2856bSMartin Willi [XFRMA_TFCPAD] = { .type = NLA_U32 }, 2793d8647b79SSteffen Klassert [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) }, 2794a947b0a9SNicolas Dichtel [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 }, 2795d3623099SNicolas Dichtel [XFRMA_PROTO] = { .type = NLA_U8 }, 2796870a2df4SNicolas Dichtel [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) }, 2797d77e38e6SSteffen Klassert [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) }, 27989b42c1f1SSteffen Klassert [XFRMA_SET_MARK] = { .type = NLA_U32 }, 27999b42c1f1SSteffen Klassert [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 }, 28007e652640SSteffen Klassert [XFRMA_IF_ID] = { .type = NLA_U32 }, 2801cf5cb79fSThomas Graf }; 28025106f4a8SDmitry Safonov EXPORT_SYMBOL_GPL(xfrma_policy); 2803cf5cb79fSThomas Graf 2804880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = { 2805880a6fabSChristophe Gouault [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2806880a6fabSChristophe Gouault [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2807880a6fabSChristophe Gouault }; 2808880a6fabSChristophe Gouault 280905600a79SMathias Krause static const struct xfrm_link { 28105424f32eSThomas Graf int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **); 28111137b5e2SHerbert Xu int (*start)(struct netlink_callback *); 28121da177e4SLinus Torvalds int (*dump)(struct sk_buff *, struct netlink_callback *); 28134c563f76STimo Teras int (*done)(struct netlink_callback *); 2814880a6fabSChristophe Gouault const struct nla_policy *nla_pol; 2815880a6fabSChristophe Gouault int nla_max; 2816492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = { 2817492b558bSThomas Graf [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 2818492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, 2819492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, 28204c563f76STimo Teras .dump = xfrm_dump_sa, 28214c563f76STimo Teras .done = xfrm_dump_sa_done }, 2822492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2823492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, 2824492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, 28251137b5e2SHerbert Xu .start = xfrm_dump_policy_start, 28264c563f76STimo Teras .dump = xfrm_dump_policy, 28274c563f76STimo Teras .done = xfrm_dump_policy_done }, 2828492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, 2829980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, 283053bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, 2831492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2832492b558bSThomas Graf [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 28336c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, 2834492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, 2835492b558bSThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, 2836d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, 2837d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, 28385c79de6eSShinta Sugimoto [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, 283928d8909bSJamal Hadi Salim [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, 2840880a6fabSChristophe Gouault [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo, 2841880a6fabSChristophe Gouault .nla_pol = xfrma_spd_policy, 2842880a6fabSChristophe Gouault .nla_max = XFRMA_SPD_MAX }, 2843ecfd6b18SJamal Hadi Salim [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, 28442d151d39SSteffen Klassert [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default }, 28452d151d39SSteffen Klassert [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default }, 28461da177e4SLinus Torvalds }; 28471da177e4SLinus Torvalds 28482d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 28492d4bc933SJohannes Berg struct netlink_ext_ack *extack) 28501da177e4SLinus Torvalds { 2851a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 285235a7aa08SThomas Graf struct nlattr *attrs[XFRMA_MAX+1]; 285305600a79SMathias Krause const struct xfrm_link *link; 28545106f4a8SDmitry Safonov struct nlmsghdr *nlh64 = NULL; 2855a7bd9a45SThomas Graf int type, err; 28561da177e4SLinus Torvalds 28571da177e4SLinus Torvalds type = nlh->nlmsg_type; 28581da177e4SLinus Torvalds if (type > XFRM_MSG_MAX) 28591d00a4ebSThomas Graf return -EINVAL; 28601da177e4SLinus Torvalds 28611da177e4SLinus Torvalds type -= XFRM_MSG_BASE; 28621da177e4SLinus Torvalds link = &xfrm_dispatch[type]; 28631da177e4SLinus Torvalds 28641da177e4SLinus Torvalds /* All operations require privileges, even GET */ 286590f62cf3SEric W. Biederman if (!netlink_net_capable(skb, CAP_NET_ADMIN)) 28661d00a4ebSThomas Graf return -EPERM; 28671da177e4SLinus Torvalds 28685106f4a8SDmitry Safonov if (in_compat_syscall()) { 28695106f4a8SDmitry Safonov struct xfrm_translator *xtr = xfrm_get_translator(); 28705106f4a8SDmitry Safonov 28715106f4a8SDmitry Safonov if (!xtr) 28725106f4a8SDmitry Safonov return -EOPNOTSUPP; 28735106f4a8SDmitry Safonov 28745106f4a8SDmitry Safonov nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max, 28755106f4a8SDmitry Safonov link->nla_pol, extack); 28765106f4a8SDmitry Safonov xfrm_put_translator(xtr); 28775106f4a8SDmitry Safonov if (IS_ERR(nlh64)) 28785106f4a8SDmitry Safonov return PTR_ERR(nlh64); 28795106f4a8SDmitry Safonov if (nlh64) 28805106f4a8SDmitry Safonov nlh = nlh64; 28815106f4a8SDmitry Safonov } 28825106f4a8SDmitry Safonov 2883492b558bSThomas Graf if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || 2884492b558bSThomas Graf type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && 2885b8f3ab42SDavid S. Miller (nlh->nlmsg_flags & NLM_F_DUMP)) { 288680d326faSPablo Neira Ayuso struct netlink_dump_control c = { 28871137b5e2SHerbert Xu .start = link->start, 288880d326faSPablo Neira Ayuso .dump = link->dump, 288980d326faSPablo Neira Ayuso .done = link->done, 289080d326faSPablo Neira Ayuso }; 28915106f4a8SDmitry Safonov 28925106f4a8SDmitry Safonov if (link->dump == NULL) { 28935106f4a8SDmitry Safonov err = -EINVAL; 28945106f4a8SDmitry Safonov goto err; 289580d326faSPablo Neira Ayuso } 28965106f4a8SDmitry Safonov 28975106f4a8SDmitry Safonov err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c); 28985106f4a8SDmitry Safonov goto err; 28991da177e4SLinus Torvalds } 29001da177e4SLinus Torvalds 29018cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs, 2902880a6fabSChristophe Gouault link->nla_max ? : XFRMA_MAX, 2903fe52145fSJohannes Berg link->nla_pol ? : xfrma_policy, extack); 2904a7bd9a45SThomas Graf if (err < 0) 29055106f4a8SDmitry Safonov goto err; 29065106f4a8SDmitry Safonov 29075106f4a8SDmitry Safonov if (link->doit == NULL) { 29085106f4a8SDmitry Safonov err = -EINVAL; 29095106f4a8SDmitry Safonov goto err; 29105106f4a8SDmitry Safonov } 29115106f4a8SDmitry Safonov 29125106f4a8SDmitry Safonov err = link->doit(skb, nlh, attrs); 29135106f4a8SDmitry Safonov 29147c1a80e8SPavel Skripkin /* We need to free skb allocated in xfrm_alloc_compat() before 29157c1a80e8SPavel Skripkin * returning from this function, because consume_skb() won't take 29167c1a80e8SPavel Skripkin * care of frag_list since netlink destructor sets 29177c1a80e8SPavel Skripkin * sbk->head to NULL. (see netlink_skb_destructor()) 29187c1a80e8SPavel Skripkin */ 29197c1a80e8SPavel Skripkin if (skb_has_frag_list(skb)) { 29207c1a80e8SPavel Skripkin kfree_skb(skb_shinfo(skb)->frag_list); 29217c1a80e8SPavel Skripkin skb_shinfo(skb)->frag_list = NULL; 29227c1a80e8SPavel Skripkin } 29237c1a80e8SPavel Skripkin 29245106f4a8SDmitry Safonov err: 29255106f4a8SDmitry Safonov kvfree(nlh64); 2926a7bd9a45SThomas Graf return err; 29271da177e4SLinus Torvalds } 29281da177e4SLinus Torvalds 2929cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb) 29301da177e4SLinus Torvalds { 2931283bc9f3SFan Du struct net *net = sock_net(skb->sk); 2932283bc9f3SFan Du 2933283bc9f3SFan Du mutex_lock(&net->xfrm.xfrm_cfg_mutex); 2934cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &xfrm_user_rcv_msg); 2935283bc9f3SFan Du mutex_unlock(&net->xfrm.xfrm_cfg_mutex); 29361da177e4SLinus Torvalds } 29371da177e4SLinus Torvalds 2938a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void) 29397deb2264SThomas Graf { 29406f26b61eSJamal Hadi Salim return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) 29416f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)); 29427deb2264SThomas Graf } 29437deb2264SThomas Graf 2944214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 29451da177e4SLinus Torvalds { 29461da177e4SLinus Torvalds struct xfrm_user_expire *ue; 29471da177e4SLinus Torvalds struct nlmsghdr *nlh; 29481d1e34ddSDavid S. Miller int err; 29491da177e4SLinus Torvalds 295015e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); 295179b8b7f4SThomas Graf if (nlh == NULL) 295279b8b7f4SThomas Graf return -EMSGSIZE; 29531da177e4SLinus Torvalds 29547b67c857SThomas Graf ue = nlmsg_data(nlh); 29551da177e4SLinus Torvalds copy_to_user_state(x, &ue->state); 2956d51d081dSJamal Hadi Salim ue->hard = (c->data.hard != 0) ? 1 : 0; 2957e3e5fc16SMathias Krause /* clear the padding bytes */ 2958e3e5fc16SMathias Krause memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard)); 29591da177e4SLinus Torvalds 29601d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &x->mark); 29611d1e34ddSDavid S. Miller if (err) 29621d1e34ddSDavid S. Miller return err; 29636f26b61eSJamal Hadi Salim 29647e652640SSteffen Klassert err = xfrm_if_id_put(skb, x->if_id); 29657e652640SSteffen Klassert if (err) 29667e652640SSteffen Klassert return err; 29677e652640SSteffen Klassert 2968053c095aSJohannes Berg nlmsg_end(skb, nlh); 2969053c095aSJohannes Berg return 0; 29701da177e4SLinus Torvalds } 29711da177e4SLinus Torvalds 2972214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c) 29731da177e4SLinus Torvalds { 2974fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 29751da177e4SLinus Torvalds struct sk_buff *skb; 29761da177e4SLinus Torvalds 29777deb2264SThomas Graf skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); 29781da177e4SLinus Torvalds if (skb == NULL) 29791da177e4SLinus Torvalds return -ENOMEM; 29801da177e4SLinus Torvalds 29816f26b61eSJamal Hadi Salim if (build_expire(skb, x, c) < 0) { 29826f26b61eSJamal Hadi Salim kfree_skb(skb); 29836f26b61eSJamal Hadi Salim return -EMSGSIZE; 29846f26b61eSJamal Hadi Salim } 29851da177e4SLinus Torvalds 298621ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 29871da177e4SLinus Torvalds } 29881da177e4SLinus Torvalds 2989214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c) 2990d51d081dSJamal Hadi Salim { 2991fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 2992d51d081dSJamal Hadi Salim struct sk_buff *skb; 29932fc5f83bSGustavo A. R. Silva int err; 2994d51d081dSJamal Hadi Salim 2995d8647b79SSteffen Klassert skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 2996d51d081dSJamal Hadi Salim if (skb == NULL) 2997d51d081dSJamal Hadi Salim return -ENOMEM; 2998d51d081dSJamal Hadi Salim 29992fc5f83bSGustavo A. R. Silva err = build_aevent(skb, x, c); 30002fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 3001d51d081dSJamal Hadi Salim 300221ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS); 3003d51d081dSJamal Hadi Salim } 3004d51d081dSJamal Hadi Salim 3005214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c) 300626b15dadSJamal Hadi Salim { 30077067802eSAlexey Dobriyan struct net *net = c->net; 300826b15dadSJamal Hadi Salim struct xfrm_usersa_flush *p; 300926b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 301026b15dadSJamal Hadi Salim struct sk_buff *skb; 30117deb2264SThomas Graf int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); 301226b15dadSJamal Hadi Salim 30137deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 301426b15dadSJamal Hadi Salim if (skb == NULL) 301526b15dadSJamal Hadi Salim return -ENOMEM; 301626b15dadSJamal Hadi Salim 301715e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); 301879b8b7f4SThomas Graf if (nlh == NULL) { 301979b8b7f4SThomas Graf kfree_skb(skb); 302079b8b7f4SThomas Graf return -EMSGSIZE; 302179b8b7f4SThomas Graf } 302226b15dadSJamal Hadi Salim 30237b67c857SThomas Graf p = nlmsg_data(nlh); 3024bf08867fSHerbert Xu p->proto = c->data.proto; 302526b15dadSJamal Hadi Salim 30269825069dSThomas Graf nlmsg_end(skb, nlh); 302726b15dadSJamal Hadi Salim 302821ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 302926b15dadSJamal Hadi Salim } 303026b15dadSJamal Hadi Salim 3031a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x) 303226b15dadSJamal Hadi Salim { 3033a1b831f2SAlexey Dobriyan unsigned int l = 0; 30341a6509d9SHerbert Xu if (x->aead) 30351a6509d9SHerbert Xu l += nla_total_size(aead_len(x->aead)); 30364447bb33SMartin Willi if (x->aalg) { 30374447bb33SMartin Willi l += nla_total_size(sizeof(struct xfrm_algo) + 30384447bb33SMartin Willi (x->aalg->alg_key_len + 7) / 8); 30394447bb33SMartin Willi l += nla_total_size(xfrm_alg_auth_len(x->aalg)); 30404447bb33SMartin Willi } 304126b15dadSJamal Hadi Salim if (x->ealg) 30420f99be0dSEric Dumazet l += nla_total_size(xfrm_alg_len(x->ealg)); 304326b15dadSJamal Hadi Salim if (x->calg) 30447deb2264SThomas Graf l += nla_total_size(sizeof(*x->calg)); 304526b15dadSJamal Hadi Salim if (x->encap) 30467deb2264SThomas Graf l += nla_total_size(sizeof(*x->encap)); 304735d2856bSMartin Willi if (x->tfcpad) 304835d2856bSMartin Willi l += nla_total_size(sizeof(x->tfcpad)); 3049d8647b79SSteffen Klassert if (x->replay_esn) 3050d8647b79SSteffen Klassert l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn)); 3051f293a5e3Sdingzhi else 3052f293a5e3Sdingzhi l += nla_total_size(sizeof(struct xfrm_replay_state)); 305368325d3bSHerbert Xu if (x->security) 305468325d3bSHerbert Xu l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + 305568325d3bSHerbert Xu x->security->ctx_len); 305668325d3bSHerbert Xu if (x->coaddr) 305768325d3bSHerbert Xu l += nla_total_size(sizeof(*x->coaddr)); 3058a947b0a9SNicolas Dichtel if (x->props.extra_flags) 3059a947b0a9SNicolas Dichtel l += nla_total_size(sizeof(x->props.extra_flags)); 3060d77e38e6SSteffen Klassert if (x->xso.dev) 3061d77e38e6SSteffen Klassert l += nla_total_size(sizeof(x->xso)); 30629b42c1f1SSteffen Klassert if (x->props.smark.v | x->props.smark.m) { 30639b42c1f1SSteffen Klassert l += nla_total_size(sizeof(x->props.smark.v)); 30649b42c1f1SSteffen Klassert l += nla_total_size(sizeof(x->props.smark.m)); 30659b42c1f1SSteffen Klassert } 30667e652640SSteffen Klassert if (x->if_id) 30677e652640SSteffen Klassert l += nla_total_size(sizeof(x->if_id)); 306868325d3bSHerbert Xu 3069d26f3984SHerbert Xu /* Must count x->lastused as it may become non-zero behind our back. */ 3070de95c4a4SNicolas Dichtel l += nla_total_size_64bit(sizeof(u64)); 307126b15dadSJamal Hadi Salim 307226b15dadSJamal Hadi Salim return l; 307326b15dadSJamal Hadi Salim } 307426b15dadSJamal Hadi Salim 3075214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c) 307626b15dadSJamal Hadi Salim { 3077fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 307826b15dadSJamal Hadi Salim struct xfrm_usersa_info *p; 30790603eac0SHerbert Xu struct xfrm_usersa_id *id; 308026b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 308126b15dadSJamal Hadi Salim struct sk_buff *skb; 3082a1b831f2SAlexey Dobriyan unsigned int len = xfrm_sa_len(x); 3083a1b831f2SAlexey Dobriyan unsigned int headlen; 3084a1b831f2SAlexey Dobriyan int err; 30850603eac0SHerbert Xu 30860603eac0SHerbert Xu headlen = sizeof(*p); 30870603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 30887deb2264SThomas Graf len += nla_total_size(headlen); 30890603eac0SHerbert Xu headlen = sizeof(*id); 30906f26b61eSJamal Hadi Salim len += nla_total_size(sizeof(struct xfrm_mark)); 30910603eac0SHerbert Xu } 30927deb2264SThomas Graf len += NLMSG_ALIGN(headlen); 309326b15dadSJamal Hadi Salim 30947deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 309526b15dadSJamal Hadi Salim if (skb == NULL) 309626b15dadSJamal Hadi Salim return -ENOMEM; 309726b15dadSJamal Hadi Salim 309815e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 30991d1e34ddSDavid S. Miller err = -EMSGSIZE; 310079b8b7f4SThomas Graf if (nlh == NULL) 31011d1e34ddSDavid S. Miller goto out_free_skb; 310226b15dadSJamal Hadi Salim 31037b67c857SThomas Graf p = nlmsg_data(nlh); 31040603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 3105c0144beaSThomas Graf struct nlattr *attr; 3106c0144beaSThomas Graf 31077b67c857SThomas Graf id = nlmsg_data(nlh); 310850329c8aSMathias Krause memset(id, 0, sizeof(*id)); 31090603eac0SHerbert Xu memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); 31100603eac0SHerbert Xu id->spi = x->id.spi; 31110603eac0SHerbert Xu id->family = x->props.family; 31120603eac0SHerbert Xu id->proto = x->id.proto; 31130603eac0SHerbert Xu 3114c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); 31151d1e34ddSDavid S. Miller err = -EMSGSIZE; 3116c0144beaSThomas Graf if (attr == NULL) 31171d1e34ddSDavid S. Miller goto out_free_skb; 3118c0144beaSThomas Graf 3119c0144beaSThomas Graf p = nla_data(attr); 31200603eac0SHerbert Xu } 31211d1e34ddSDavid S. Miller err = copy_to_user_state_extra(x, p, skb); 31221d1e34ddSDavid S. Miller if (err) 31231d1e34ddSDavid S. Miller goto out_free_skb; 312426b15dadSJamal Hadi Salim 31259825069dSThomas Graf nlmsg_end(skb, nlh); 312626b15dadSJamal Hadi Salim 312721ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 312826b15dadSJamal Hadi Salim 31291d1e34ddSDavid S. Miller out_free_skb: 313026b15dadSJamal Hadi Salim kfree_skb(skb); 31311d1e34ddSDavid S. Miller return err; 313226b15dadSJamal Hadi Salim } 313326b15dadSJamal Hadi Salim 3134214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c) 313526b15dadSJamal Hadi Salim { 313626b15dadSJamal Hadi Salim 313726b15dadSJamal Hadi Salim switch (c->event) { 3138f60f6b8fSHerbert Xu case XFRM_MSG_EXPIRE: 313926b15dadSJamal Hadi Salim return xfrm_exp_state_notify(x, c); 3140d51d081dSJamal Hadi Salim case XFRM_MSG_NEWAE: 3141d51d081dSJamal Hadi Salim return xfrm_aevent_state_notify(x, c); 3142f60f6b8fSHerbert Xu case XFRM_MSG_DELSA: 3143f60f6b8fSHerbert Xu case XFRM_MSG_UPDSA: 3144f60f6b8fSHerbert Xu case XFRM_MSG_NEWSA: 314526b15dadSJamal Hadi Salim return xfrm_notify_sa(x, c); 3146f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHSA: 314726b15dadSJamal Hadi Salim return xfrm_notify_sa_flush(c); 314826b15dadSJamal Hadi Salim default: 314962db5cfdSstephen hemminger printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n", 315062db5cfdSstephen hemminger c->event); 315126b15dadSJamal Hadi Salim break; 315226b15dadSJamal Hadi Salim } 315326b15dadSJamal Hadi Salim 315426b15dadSJamal Hadi Salim return 0; 315526b15dadSJamal Hadi Salim 315626b15dadSJamal Hadi Salim } 315726b15dadSJamal Hadi Salim 3158a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x, 31597deb2264SThomas Graf struct xfrm_policy *xp) 31607deb2264SThomas Graf { 31617deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) 31627deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 31636f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 31647deb2264SThomas Graf + nla_total_size(xfrm_user_sec_ctx_size(x->security)) 31657deb2264SThomas Graf + userpolicy_type_attrsize(); 31667deb2264SThomas Graf } 31677deb2264SThomas Graf 31681da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, 316965e0736bSFan Du struct xfrm_tmpl *xt, struct xfrm_policy *xp) 31701da177e4SLinus Torvalds { 31711d1e34ddSDavid S. Miller __u32 seq = xfrm_get_acqseq(); 31721da177e4SLinus Torvalds struct xfrm_user_acquire *ua; 31731da177e4SLinus Torvalds struct nlmsghdr *nlh; 31741d1e34ddSDavid S. Miller int err; 31751da177e4SLinus Torvalds 317679b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); 317779b8b7f4SThomas Graf if (nlh == NULL) 317879b8b7f4SThomas Graf return -EMSGSIZE; 31791da177e4SLinus Torvalds 31807b67c857SThomas Graf ua = nlmsg_data(nlh); 31811da177e4SLinus Torvalds memcpy(&ua->id, &x->id, sizeof(ua->id)); 31821da177e4SLinus Torvalds memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); 31831da177e4SLinus Torvalds memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); 318465e0736bSFan Du copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT); 31851da177e4SLinus Torvalds ua->aalgos = xt->aalgos; 31861da177e4SLinus Torvalds ua->ealgos = xt->ealgos; 31871da177e4SLinus Torvalds ua->calgos = xt->calgos; 31881da177e4SLinus Torvalds ua->seq = x->km.seq = seq; 31891da177e4SLinus Torvalds 31901d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 31911d1e34ddSDavid S. Miller if (!err) 31921d1e34ddSDavid S. Miller err = copy_to_user_state_sec_ctx(x, skb); 31931d1e34ddSDavid S. Miller if (!err) 31941d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 31951d1e34ddSDavid S. Miller if (!err) 31961d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 31977e652640SSteffen Klassert if (!err) 31987e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 31991d1e34ddSDavid S. Miller if (err) { 32001d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 32011d1e34ddSDavid S. Miller return err; 32021d1e34ddSDavid S. Miller } 32031da177e4SLinus Torvalds 3204053c095aSJohannes Berg nlmsg_end(skb, nlh); 3205053c095aSJohannes Berg return 0; 32061da177e4SLinus Torvalds } 32071da177e4SLinus Torvalds 32081da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, 320965e0736bSFan Du struct xfrm_policy *xp) 32101da177e4SLinus Torvalds { 3211a6483b79SAlexey Dobriyan struct net *net = xs_net(x); 32121da177e4SLinus Torvalds struct sk_buff *skb; 32132fc5f83bSGustavo A. R. Silva int err; 32141da177e4SLinus Torvalds 32157deb2264SThomas Graf skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); 32161da177e4SLinus Torvalds if (skb == NULL) 32171da177e4SLinus Torvalds return -ENOMEM; 32181da177e4SLinus Torvalds 32192fc5f83bSGustavo A. R. Silva err = build_acquire(skb, x, xt, xp); 32202fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 32211da177e4SLinus Torvalds 322221ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE); 32231da177e4SLinus Torvalds } 32241da177e4SLinus Torvalds 32251da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0 32261da177e4SLinus Torvalds * or more templates. 32271da177e4SLinus Torvalds */ 3228cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, 32291da177e4SLinus Torvalds u8 *data, int len, int *dir) 32301da177e4SLinus Torvalds { 3231fc34acd3SAlexey Dobriyan struct net *net = sock_net(sk); 32321da177e4SLinus Torvalds struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; 32331da177e4SLinus Torvalds struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); 32341da177e4SLinus Torvalds struct xfrm_policy *xp; 32351da177e4SLinus Torvalds int nr; 32361da177e4SLinus Torvalds 3237cb969f07SVenkat Yekkirala switch (sk->sk_family) { 32381da177e4SLinus Torvalds case AF_INET: 32391da177e4SLinus Torvalds if (opt != IP_XFRM_POLICY) { 32401da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 32411da177e4SLinus Torvalds return NULL; 32421da177e4SLinus Torvalds } 32431da177e4SLinus Torvalds break; 3244dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 32451da177e4SLinus Torvalds case AF_INET6: 32461da177e4SLinus Torvalds if (opt != IPV6_XFRM_POLICY) { 32471da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 32481da177e4SLinus Torvalds return NULL; 32491da177e4SLinus Torvalds } 32501da177e4SLinus Torvalds break; 32511da177e4SLinus Torvalds #endif 32521da177e4SLinus Torvalds default: 32531da177e4SLinus Torvalds *dir = -EINVAL; 32541da177e4SLinus Torvalds return NULL; 32551da177e4SLinus Torvalds } 32561da177e4SLinus Torvalds 32571da177e4SLinus Torvalds *dir = -EINVAL; 32581da177e4SLinus Torvalds 32591da177e4SLinus Torvalds if (len < sizeof(*p) || 32601da177e4SLinus Torvalds verify_newpolicy_info(p)) 32611da177e4SLinus Torvalds return NULL; 32621da177e4SLinus Torvalds 32631da177e4SLinus Torvalds nr = ((len - sizeof(*p)) / sizeof(*ut)); 3264b4ad86bfSDavid S. Miller if (validate_tmpl(nr, ut, p->sel.family)) 32651da177e4SLinus Torvalds return NULL; 32661da177e4SLinus Torvalds 3267a4f1bac6SHerbert Xu if (p->dir > XFRM_POLICY_OUT) 3268a4f1bac6SHerbert Xu return NULL; 3269a4f1bac6SHerbert Xu 32702f09a4d5SHerbert Xu xp = xfrm_policy_alloc(net, GFP_ATOMIC); 32711da177e4SLinus Torvalds if (xp == NULL) { 32721da177e4SLinus Torvalds *dir = -ENOBUFS; 32731da177e4SLinus Torvalds return NULL; 32741da177e4SLinus Torvalds } 32751da177e4SLinus Torvalds 32761da177e4SLinus Torvalds copy_from_user_policy(xp, p); 3277f7b6983fSMasahide NAKAMURA xp->type = XFRM_POLICY_TYPE_MAIN; 32781da177e4SLinus Torvalds copy_templates(xp, ut, nr); 32791da177e4SLinus Torvalds 32801da177e4SLinus Torvalds *dir = p->dir; 32811da177e4SLinus Torvalds 32821da177e4SLinus Torvalds return xp; 32831da177e4SLinus Torvalds } 32841da177e4SLinus Torvalds 3285a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp) 32867deb2264SThomas Graf { 32877deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) 32887deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 32897deb2264SThomas Graf + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) 3290295fae56SJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 32917deb2264SThomas Graf + userpolicy_type_attrsize(); 32927deb2264SThomas Graf } 32937deb2264SThomas Graf 32941da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, 3295214e005bSDavid S. Miller int dir, const struct km_event *c) 32961da177e4SLinus Torvalds { 32971da177e4SLinus Torvalds struct xfrm_user_polexpire *upe; 3298d51d081dSJamal Hadi Salim int hard = c->data.hard; 32991d1e34ddSDavid S. Miller struct nlmsghdr *nlh; 33001d1e34ddSDavid S. Miller int err; 33011da177e4SLinus Torvalds 330215e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); 330379b8b7f4SThomas Graf if (nlh == NULL) 330479b8b7f4SThomas Graf return -EMSGSIZE; 33051da177e4SLinus Torvalds 33067b67c857SThomas Graf upe = nlmsg_data(nlh); 33071da177e4SLinus Torvalds copy_to_user_policy(xp, &upe->pol, dir); 33081d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 33091d1e34ddSDavid S. Miller if (!err) 33101d1e34ddSDavid S. Miller err = copy_to_user_sec_ctx(xp, skb); 33111d1e34ddSDavid S. Miller if (!err) 33121d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 33131d1e34ddSDavid S. Miller if (!err) 33141d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 33157e652640SSteffen Klassert if (!err) 33167e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 33171d1e34ddSDavid S. Miller if (err) { 33181d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 33191d1e34ddSDavid S. Miller return err; 33201d1e34ddSDavid S. Miller } 33211da177e4SLinus Torvalds upe->hard = !!hard; 33221da177e4SLinus Torvalds 3323053c095aSJohannes Berg nlmsg_end(skb, nlh); 3324053c095aSJohannes Berg return 0; 33251da177e4SLinus Torvalds } 33261da177e4SLinus Torvalds 3327214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 33281da177e4SLinus Torvalds { 3329fc34acd3SAlexey Dobriyan struct net *net = xp_net(xp); 33301da177e4SLinus Torvalds struct sk_buff *skb; 33312fc5f83bSGustavo A. R. Silva int err; 33321da177e4SLinus Torvalds 33337deb2264SThomas Graf skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); 33341da177e4SLinus Torvalds if (skb == NULL) 33351da177e4SLinus Torvalds return -ENOMEM; 33361da177e4SLinus Torvalds 33372fc5f83bSGustavo A. R. Silva err = build_polexpire(skb, xp, dir, c); 33382fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 33391da177e4SLinus Torvalds 334021ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 33411da177e4SLinus Torvalds } 33421da177e4SLinus Torvalds 3343214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c) 334426b15dadSJamal Hadi Salim { 3345a1b831f2SAlexey Dobriyan unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 3346fc34acd3SAlexey Dobriyan struct net *net = xp_net(xp); 334726b15dadSJamal Hadi Salim struct xfrm_userpolicy_info *p; 33480603eac0SHerbert Xu struct xfrm_userpolicy_id *id; 334926b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 335026b15dadSJamal Hadi Salim struct sk_buff *skb; 3351a1b831f2SAlexey Dobriyan unsigned int headlen; 3352a1b831f2SAlexey Dobriyan int err; 33530603eac0SHerbert Xu 33540603eac0SHerbert Xu headlen = sizeof(*p); 33550603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 33567deb2264SThomas Graf len += nla_total_size(headlen); 33570603eac0SHerbert Xu headlen = sizeof(*id); 33580603eac0SHerbert Xu } 3359cfbfd45aSThomas Graf len += userpolicy_type_attrsize(); 3360295fae56SJamal Hadi Salim len += nla_total_size(sizeof(struct xfrm_mark)); 33617deb2264SThomas Graf len += NLMSG_ALIGN(headlen); 336226b15dadSJamal Hadi Salim 33637deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 336426b15dadSJamal Hadi Salim if (skb == NULL) 336526b15dadSJamal Hadi Salim return -ENOMEM; 336626b15dadSJamal Hadi Salim 336715e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 33681d1e34ddSDavid S. Miller err = -EMSGSIZE; 336979b8b7f4SThomas Graf if (nlh == NULL) 33701d1e34ddSDavid S. Miller goto out_free_skb; 337126b15dadSJamal Hadi Salim 33727b67c857SThomas Graf p = nlmsg_data(nlh); 33730603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 3374c0144beaSThomas Graf struct nlattr *attr; 3375c0144beaSThomas Graf 33767b67c857SThomas Graf id = nlmsg_data(nlh); 33770603eac0SHerbert Xu memset(id, 0, sizeof(*id)); 33780603eac0SHerbert Xu id->dir = dir; 33790603eac0SHerbert Xu if (c->data.byid) 33800603eac0SHerbert Xu id->index = xp->index; 33810603eac0SHerbert Xu else 33820603eac0SHerbert Xu memcpy(&id->sel, &xp->selector, sizeof(id->sel)); 33830603eac0SHerbert Xu 3384c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); 33851d1e34ddSDavid S. Miller err = -EMSGSIZE; 3386c0144beaSThomas Graf if (attr == NULL) 33871d1e34ddSDavid S. Miller goto out_free_skb; 3388c0144beaSThomas Graf 3389c0144beaSThomas Graf p = nla_data(attr); 33900603eac0SHerbert Xu } 339126b15dadSJamal Hadi Salim 339226b15dadSJamal Hadi Salim copy_to_user_policy(xp, p, dir); 33931d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 33941d1e34ddSDavid S. Miller if (!err) 33951d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 33961d1e34ddSDavid S. Miller if (!err) 33971d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 33987e652640SSteffen Klassert if (!err) 33997e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 34001d1e34ddSDavid S. Miller if (err) 34011d1e34ddSDavid S. Miller goto out_free_skb; 3402295fae56SJamal Hadi Salim 34039825069dSThomas Graf nlmsg_end(skb, nlh); 340426b15dadSJamal Hadi Salim 340521ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 340626b15dadSJamal Hadi Salim 34071d1e34ddSDavid S. Miller out_free_skb: 340826b15dadSJamal Hadi Salim kfree_skb(skb); 34091d1e34ddSDavid S. Miller return err; 341026b15dadSJamal Hadi Salim } 341126b15dadSJamal Hadi Salim 3412214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c) 341326b15dadSJamal Hadi Salim { 34147067802eSAlexey Dobriyan struct net *net = c->net; 341526b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 341626b15dadSJamal Hadi Salim struct sk_buff *skb; 34171d1e34ddSDavid S. Miller int err; 341826b15dadSJamal Hadi Salim 34197deb2264SThomas Graf skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); 342026b15dadSJamal Hadi Salim if (skb == NULL) 342126b15dadSJamal Hadi Salim return -ENOMEM; 342226b15dadSJamal Hadi Salim 342315e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); 34241d1e34ddSDavid S. Miller err = -EMSGSIZE; 342579b8b7f4SThomas Graf if (nlh == NULL) 34261d1e34ddSDavid S. Miller goto out_free_skb; 34271d1e34ddSDavid S. Miller err = copy_to_user_policy_type(c->data.type, skb); 34281d1e34ddSDavid S. Miller if (err) 34291d1e34ddSDavid S. Miller goto out_free_skb; 343026b15dadSJamal Hadi Salim 34319825069dSThomas Graf nlmsg_end(skb, nlh); 343226b15dadSJamal Hadi Salim 343321ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 343426b15dadSJamal Hadi Salim 34351d1e34ddSDavid S. Miller out_free_skb: 343626b15dadSJamal Hadi Salim kfree_skb(skb); 34371d1e34ddSDavid S. Miller return err; 343826b15dadSJamal Hadi Salim } 343926b15dadSJamal Hadi Salim 3440214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 344126b15dadSJamal Hadi Salim { 344226b15dadSJamal Hadi Salim 344326b15dadSJamal Hadi Salim switch (c->event) { 3444f60f6b8fSHerbert Xu case XFRM_MSG_NEWPOLICY: 3445f60f6b8fSHerbert Xu case XFRM_MSG_UPDPOLICY: 3446f60f6b8fSHerbert Xu case XFRM_MSG_DELPOLICY: 344726b15dadSJamal Hadi Salim return xfrm_notify_policy(xp, dir, c); 3448f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHPOLICY: 344926b15dadSJamal Hadi Salim return xfrm_notify_policy_flush(c); 3450f60f6b8fSHerbert Xu case XFRM_MSG_POLEXPIRE: 345126b15dadSJamal Hadi Salim return xfrm_exp_policy_notify(xp, dir, c); 345226b15dadSJamal Hadi Salim default: 345362db5cfdSstephen hemminger printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n", 345462db5cfdSstephen hemminger c->event); 345526b15dadSJamal Hadi Salim } 345626b15dadSJamal Hadi Salim 345726b15dadSJamal Hadi Salim return 0; 345826b15dadSJamal Hadi Salim 345926b15dadSJamal Hadi Salim } 346026b15dadSJamal Hadi Salim 3461a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void) 34627deb2264SThomas Graf { 34637deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); 34647deb2264SThomas Graf } 34657deb2264SThomas Graf 346697a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto, 346797a64b45SMasahide NAKAMURA struct xfrm_selector *sel, xfrm_address_t *addr) 346897a64b45SMasahide NAKAMURA { 346997a64b45SMasahide NAKAMURA struct xfrm_user_report *ur; 347097a64b45SMasahide NAKAMURA struct nlmsghdr *nlh; 347197a64b45SMasahide NAKAMURA 347279b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); 347379b8b7f4SThomas Graf if (nlh == NULL) 347479b8b7f4SThomas Graf return -EMSGSIZE; 347597a64b45SMasahide NAKAMURA 34767b67c857SThomas Graf ur = nlmsg_data(nlh); 347797a64b45SMasahide NAKAMURA ur->proto = proto; 347897a64b45SMasahide NAKAMURA memcpy(&ur->sel, sel, sizeof(ur->sel)); 347997a64b45SMasahide NAKAMURA 34801d1e34ddSDavid S. Miller if (addr) { 34811d1e34ddSDavid S. Miller int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr); 34821d1e34ddSDavid S. Miller if (err) { 34839825069dSThomas Graf nlmsg_cancel(skb, nlh); 34841d1e34ddSDavid S. Miller return err; 34851d1e34ddSDavid S. Miller } 34861d1e34ddSDavid S. Miller } 3487053c095aSJohannes Berg nlmsg_end(skb, nlh); 3488053c095aSJohannes Berg return 0; 348997a64b45SMasahide NAKAMURA } 349097a64b45SMasahide NAKAMURA 3491db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto, 3492db983c11SAlexey Dobriyan struct xfrm_selector *sel, xfrm_address_t *addr) 349397a64b45SMasahide NAKAMURA { 349497a64b45SMasahide NAKAMURA struct sk_buff *skb; 34952fc5f83bSGustavo A. R. Silva int err; 349697a64b45SMasahide NAKAMURA 34977deb2264SThomas Graf skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); 349897a64b45SMasahide NAKAMURA if (skb == NULL) 349997a64b45SMasahide NAKAMURA return -ENOMEM; 350097a64b45SMasahide NAKAMURA 35012fc5f83bSGustavo A. R. Silva err = build_report(skb, proto, sel, addr); 35022fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 350397a64b45SMasahide NAKAMURA 350421ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT); 350597a64b45SMasahide NAKAMURA } 350697a64b45SMasahide NAKAMURA 3507a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void) 35083a2dfbe8SMartin Willi { 35093a2dfbe8SMartin Willi return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping)); 35103a2dfbe8SMartin Willi } 35113a2dfbe8SMartin Willi 35123a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x, 35133a2dfbe8SMartin Willi xfrm_address_t *new_saddr, __be16 new_sport) 35143a2dfbe8SMartin Willi { 35153a2dfbe8SMartin Willi struct xfrm_user_mapping *um; 35163a2dfbe8SMartin Willi struct nlmsghdr *nlh; 35173a2dfbe8SMartin Willi 35183a2dfbe8SMartin Willi nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0); 35193a2dfbe8SMartin Willi if (nlh == NULL) 35203a2dfbe8SMartin Willi return -EMSGSIZE; 35213a2dfbe8SMartin Willi 35223a2dfbe8SMartin Willi um = nlmsg_data(nlh); 35233a2dfbe8SMartin Willi 35243a2dfbe8SMartin Willi memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr)); 35253a2dfbe8SMartin Willi um->id.spi = x->id.spi; 35263a2dfbe8SMartin Willi um->id.family = x->props.family; 35273a2dfbe8SMartin Willi um->id.proto = x->id.proto; 35283a2dfbe8SMartin Willi memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr)); 35293a2dfbe8SMartin Willi memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr)); 35303a2dfbe8SMartin Willi um->new_sport = new_sport; 35313a2dfbe8SMartin Willi um->old_sport = x->encap->encap_sport; 35323a2dfbe8SMartin Willi um->reqid = x->props.reqid; 35333a2dfbe8SMartin Willi 3534053c095aSJohannes Berg nlmsg_end(skb, nlh); 3535053c095aSJohannes Berg return 0; 35363a2dfbe8SMartin Willi } 35373a2dfbe8SMartin Willi 35383a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, 35393a2dfbe8SMartin Willi __be16 sport) 35403a2dfbe8SMartin Willi { 3541a6483b79SAlexey Dobriyan struct net *net = xs_net(x); 35423a2dfbe8SMartin Willi struct sk_buff *skb; 35432fc5f83bSGustavo A. R. Silva int err; 35443a2dfbe8SMartin Willi 35453a2dfbe8SMartin Willi if (x->id.proto != IPPROTO_ESP) 35463a2dfbe8SMartin Willi return -EINVAL; 35473a2dfbe8SMartin Willi 35483a2dfbe8SMartin Willi if (!x->encap) 35493a2dfbe8SMartin Willi return -EINVAL; 35503a2dfbe8SMartin Willi 35513a2dfbe8SMartin Willi skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC); 35523a2dfbe8SMartin Willi if (skb == NULL) 35533a2dfbe8SMartin Willi return -ENOMEM; 35543a2dfbe8SMartin Willi 35552fc5f83bSGustavo A. R. Silva err = build_mapping(skb, x, ipaddr, sport); 35562fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 35573a2dfbe8SMartin Willi 355821ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING); 35593a2dfbe8SMartin Willi } 35603a2dfbe8SMartin Willi 35610f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c) 35620f24558eSHoria Geanta { 35630f24558eSHoria Geanta return (bool)xfrm_acquire_is_on(c->net); 35640f24558eSHoria Geanta } 35650f24558eSHoria Geanta 35661da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = { 35671da177e4SLinus Torvalds .notify = xfrm_send_state_notify, 35681da177e4SLinus Torvalds .acquire = xfrm_send_acquire, 35691da177e4SLinus Torvalds .compile_policy = xfrm_compile_policy, 35701da177e4SLinus Torvalds .notify_policy = xfrm_send_policy_notify, 357197a64b45SMasahide NAKAMURA .report = xfrm_send_report, 35725c79de6eSShinta Sugimoto .migrate = xfrm_send_migrate, 35733a2dfbe8SMartin Willi .new_mapping = xfrm_send_mapping, 35740f24558eSHoria Geanta .is_alive = xfrm_is_alive, 35751da177e4SLinus Torvalds }; 35761da177e4SLinus Torvalds 3577a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net) 35781da177e4SLinus Torvalds { 3579be33690dSPatrick McHardy struct sock *nlsk; 3580a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 3581a31f2d17SPablo Neira Ayuso .groups = XFRMNLGRP_MAX, 3582a31f2d17SPablo Neira Ayuso .input = xfrm_netlink_rcv, 3583a31f2d17SPablo Neira Ayuso }; 3584be33690dSPatrick McHardy 35859f00d977SPablo Neira Ayuso nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg); 3586be33690dSPatrick McHardy if (nlsk == NULL) 35871da177e4SLinus Torvalds return -ENOMEM; 3588d79d792eSEric W. Biederman net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */ 3589cf778b00SEric Dumazet rcu_assign_pointer(net->xfrm.nlsk, nlsk); 35901da177e4SLinus Torvalds return 0; 35911da177e4SLinus Torvalds } 35921da177e4SLinus Torvalds 35936218fe18SFlorian Westphal static void __net_exit xfrm_user_net_pre_exit(struct net *net) 35946218fe18SFlorian Westphal { 35956218fe18SFlorian Westphal RCU_INIT_POINTER(net->xfrm.nlsk, NULL); 35966218fe18SFlorian Westphal } 35976218fe18SFlorian Westphal 3598d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list) 3599a6483b79SAlexey Dobriyan { 3600d79d792eSEric W. Biederman struct net *net; 36016218fe18SFlorian Westphal 3602d79d792eSEric W. Biederman list_for_each_entry(net, net_exit_list, exit_list) 3603d79d792eSEric W. Biederman netlink_kernel_release(net->xfrm.nlsk_stash); 3604a6483b79SAlexey Dobriyan } 3605a6483b79SAlexey Dobriyan 3606a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = { 3607a6483b79SAlexey Dobriyan .init = xfrm_user_net_init, 36086218fe18SFlorian Westphal .pre_exit = xfrm_user_net_pre_exit, 3609d79d792eSEric W. Biederman .exit_batch = xfrm_user_net_exit, 3610a6483b79SAlexey Dobriyan }; 3611a6483b79SAlexey Dobriyan 3612a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void) 3613a6483b79SAlexey Dobriyan { 3614a6483b79SAlexey Dobriyan int rv; 3615a6483b79SAlexey Dobriyan 3616a6483b79SAlexey Dobriyan printk(KERN_INFO "Initializing XFRM netlink socket\n"); 3617a6483b79SAlexey Dobriyan 3618a6483b79SAlexey Dobriyan rv = register_pernet_subsys(&xfrm_user_net_ops); 3619a6483b79SAlexey Dobriyan if (rv < 0) 3620a6483b79SAlexey Dobriyan return rv; 3621a6483b79SAlexey Dobriyan rv = xfrm_register_km(&netlink_mgr); 3622a6483b79SAlexey Dobriyan if (rv < 0) 3623a6483b79SAlexey Dobriyan unregister_pernet_subsys(&xfrm_user_net_ops); 3624a6483b79SAlexey Dobriyan return rv; 3625a6483b79SAlexey Dobriyan } 3626a6483b79SAlexey Dobriyan 36271da177e4SLinus Torvalds static void __exit xfrm_user_exit(void) 36281da177e4SLinus Torvalds { 36291da177e4SLinus Torvalds xfrm_unregister_km(&netlink_mgr); 3630a6483b79SAlexey Dobriyan unregister_pernet_subsys(&xfrm_user_net_ops); 36311da177e4SLinus Torvalds } 36321da177e4SLinus Torvalds 36331da177e4SLinus Torvalds module_init(xfrm_user_init); 36341da177e4SLinus Torvalds module_exit(xfrm_user_exit); 36351da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 36364fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); 3637