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 14b6459415SJakub Kicinski #include <linux/compat.h> 159409f38aSHerbert Xu #include <linux/crypto.h> 161da177e4SLinus Torvalds #include <linux/module.h> 171da177e4SLinus Torvalds #include <linux/kernel.h> 181da177e4SLinus Torvalds #include <linux/types.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/socket.h> 211da177e4SLinus Torvalds #include <linux/string.h> 221da177e4SLinus Torvalds #include <linux/net.h> 231da177e4SLinus Torvalds #include <linux/skbuff.h> 241da177e4SLinus Torvalds #include <linux/pfkeyv2.h> 251da177e4SLinus Torvalds #include <linux/ipsec.h> 261da177e4SLinus Torvalds #include <linux/init.h> 271da177e4SLinus Torvalds #include <linux/security.h> 281da177e4SLinus Torvalds #include <net/sock.h> 291da177e4SLinus Torvalds #include <net/xfrm.h> 3088fc2c84SThomas Graf #include <net/netlink.h> 31fa6dd8a2SNicolas Dichtel #include <net/ah.h> 327c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 33dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 34e23c7194SMasahide NAKAMURA #include <linux/in6.h> 35e23c7194SMasahide NAKAMURA #endif 36e33d4f13SSowmini Varadhan #include <asm/unaligned.h> 371da177e4SLinus Torvalds 381fc8fde5SSabrina Dubroca static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type, 391fc8fde5SSabrina Dubroca struct netlink_ext_ack *extack) 401da177e4SLinus Torvalds { 415424f32eSThomas Graf struct nlattr *rt = attrs[type]; 421da177e4SLinus Torvalds struct xfrm_algo *algp; 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds if (!rt) 451da177e4SLinus Torvalds return 0; 461da177e4SLinus Torvalds 475424f32eSThomas Graf algp = nla_data(rt); 481fc8fde5SSabrina Dubroca if (nla_len(rt) < (int)xfrm_alg_len(algp)) { 491fc8fde5SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid AUTH/CRYPT/COMP attribute length"); 5031c26852SHerbert Xu return -EINVAL; 511fc8fde5SSabrina Dubroca } 5231c26852SHerbert Xu 531da177e4SLinus Torvalds switch (type) { 541da177e4SLinus Torvalds case XFRMA_ALG_AUTH: 551da177e4SLinus Torvalds case XFRMA_ALG_CRYPT: 561da177e4SLinus Torvalds case XFRMA_ALG_COMP: 571da177e4SLinus Torvalds break; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds default: 601fc8fde5SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid algorithm attribute type"); 611da177e4SLinus Torvalds return -EINVAL; 623ff50b79SStephen Hemminger } 631da177e4SLinus Torvalds 64633439f5SHerbert Xu algp->alg_name[sizeof(algp->alg_name) - 1] = '\0'; 651da177e4SLinus Torvalds return 0; 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 681fc8fde5SSabrina Dubroca static int verify_auth_trunc(struct nlattr **attrs, 691fc8fde5SSabrina Dubroca struct netlink_ext_ack *extack) 704447bb33SMartin Willi { 714447bb33SMartin Willi struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC]; 724447bb33SMartin Willi struct xfrm_algo_auth *algp; 734447bb33SMartin Willi 744447bb33SMartin Willi if (!rt) 754447bb33SMartin Willi return 0; 764447bb33SMartin Willi 774447bb33SMartin Willi algp = nla_data(rt); 781fc8fde5SSabrina Dubroca if (nla_len(rt) < (int)xfrm_alg_auth_len(algp)) { 791fc8fde5SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid AUTH_TRUNC attribute length"); 804447bb33SMartin Willi return -EINVAL; 811fc8fde5SSabrina Dubroca } 824447bb33SMartin Willi 83633439f5SHerbert Xu algp->alg_name[sizeof(algp->alg_name) - 1] = '\0'; 844447bb33SMartin Willi return 0; 854447bb33SMartin Willi } 864447bb33SMartin Willi 871fc8fde5SSabrina Dubroca static int verify_aead(struct nlattr **attrs, struct netlink_ext_ack *extack) 881a6509d9SHerbert Xu { 891a6509d9SHerbert Xu struct nlattr *rt = attrs[XFRMA_ALG_AEAD]; 901a6509d9SHerbert Xu struct xfrm_algo_aead *algp; 911a6509d9SHerbert Xu 921a6509d9SHerbert Xu if (!rt) 931a6509d9SHerbert Xu return 0; 941a6509d9SHerbert Xu 951a6509d9SHerbert Xu algp = nla_data(rt); 961fc8fde5SSabrina Dubroca if (nla_len(rt) < (int)aead_len(algp)) { 971fc8fde5SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid AEAD attribute length"); 981a6509d9SHerbert Xu return -EINVAL; 991fc8fde5SSabrina Dubroca } 1001a6509d9SHerbert Xu 101633439f5SHerbert Xu algp->alg_name[sizeof(algp->alg_name) - 1] = '\0'; 1021a6509d9SHerbert Xu return 0; 1031a6509d9SHerbert Xu } 1041a6509d9SHerbert Xu 1055424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type, 106eb2971b6SMasahide NAKAMURA xfrm_address_t **addrp) 107eb2971b6SMasahide NAKAMURA { 1085424f32eSThomas Graf struct nlattr *rt = attrs[type]; 109eb2971b6SMasahide NAKAMURA 110cf5cb79fSThomas Graf if (rt && addrp) 1115424f32eSThomas Graf *addrp = nla_data(rt); 112eb2971b6SMasahide NAKAMURA } 113df71837dSTrent Jaeger 11408a717e4SSabrina Dubroca static inline int verify_sec_ctx_len(struct nlattr **attrs, struct netlink_ext_ack *extack) 115df71837dSTrent Jaeger { 1165424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 117df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx; 118df71837dSTrent Jaeger 119df71837dSTrent Jaeger if (!rt) 120df71837dSTrent Jaeger return 0; 121df71837dSTrent Jaeger 1225424f32eSThomas Graf uctx = nla_data(rt); 123171d449aSXin Long if (uctx->len > nla_len(rt) || 12408a717e4SSabrina Dubroca uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) { 12508a717e4SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid security context length"); 126df71837dSTrent Jaeger return -EINVAL; 12708a717e4SSabrina Dubroca } 128df71837dSTrent Jaeger 129df71837dSTrent Jaeger return 0; 130df71837dSTrent Jaeger } 131df71837dSTrent Jaeger 132d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p, 133785b87b2SSabrina Dubroca struct nlattr **attrs, 134785b87b2SSabrina Dubroca struct netlink_ext_ack *extack) 135d8647b79SSteffen Klassert { 136d8647b79SSteffen Klassert struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL]; 137ecd79187SMathias Krause struct xfrm_replay_state_esn *rs; 138d8647b79SSteffen Klassert 139785b87b2SSabrina Dubroca if (!rt) { 140785b87b2SSabrina Dubroca if (p->flags & XFRM_STATE_ESN) { 141785b87b2SSabrina Dubroca NL_SET_ERR_MSG(extack, "Missing required attribute for ESN"); 142785b87b2SSabrina Dubroca return -EINVAL; 143785b87b2SSabrina Dubroca } 144785b87b2SSabrina Dubroca return 0; 145785b87b2SSabrina Dubroca } 1467833aa05SSteffen Klassert 147ecd79187SMathias Krause rs = nla_data(rt); 148ecd79187SMathias Krause 149785b87b2SSabrina Dubroca if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8) { 150785b87b2SSabrina Dubroca NL_SET_ERR_MSG(extack, "ESN bitmap length must be <= 128"); 151ecd79187SMathias Krause return -EINVAL; 152785b87b2SSabrina Dubroca } 153ecd79187SMathias Krause 1545e708e47SAlexey Dobriyan if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) && 155785b87b2SSabrina Dubroca nla_len(rt) != sizeof(*rs)) { 156785b87b2SSabrina Dubroca NL_SET_ERR_MSG(extack, "ESN attribute is too short to fit the full bitmap length"); 157ecd79187SMathias Krause return -EINVAL; 158785b87b2SSabrina Dubroca } 159d8647b79SSteffen Klassert 16001714109SFan Du /* As only ESP and AH support ESN feature. */ 161785b87b2SSabrina Dubroca if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH)) { 162785b87b2SSabrina Dubroca NL_SET_ERR_MSG(extack, "ESN only supported for ESP and AH"); 16302aadf72SSteffen Klassert return -EINVAL; 164785b87b2SSabrina Dubroca } 16502aadf72SSteffen Klassert 166785b87b2SSabrina Dubroca if (p->replay_window != 0) { 167785b87b2SSabrina Dubroca NL_SET_ERR_MSG(extack, "ESN not compatible with legacy replay_window"); 168d8647b79SSteffen Klassert return -EINVAL; 169785b87b2SSabrina Dubroca } 170d8647b79SSteffen Klassert 171d8647b79SSteffen Klassert return 0; 172d8647b79SSteffen Klassert } 173df71837dSTrent Jaeger 1741da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p, 1756999aae1SSabrina Dubroca struct nlattr **attrs, 1766999aae1SSabrina Dubroca struct netlink_ext_ack *extack) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds int err; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds err = -EINVAL; 1811da177e4SLinus Torvalds switch (p->family) { 1821da177e4SLinus Torvalds case AF_INET: 183b38ff407SAnirudh Gupta break; 184b38ff407SAnirudh Gupta 185b38ff407SAnirudh Gupta case AF_INET6: 186b38ff407SAnirudh Gupta #if IS_ENABLED(CONFIG_IPV6) 187b38ff407SAnirudh Gupta break; 188b38ff407SAnirudh Gupta #else 189b38ff407SAnirudh Gupta err = -EAFNOSUPPORT; 1906999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "IPv6 support disabled"); 191b38ff407SAnirudh Gupta goto out; 192b38ff407SAnirudh Gupta #endif 193b38ff407SAnirudh Gupta 194b38ff407SAnirudh Gupta default: 1956999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid address family"); 196b38ff407SAnirudh Gupta goto out; 197b38ff407SAnirudh Gupta } 198b38ff407SAnirudh Gupta 199b38ff407SAnirudh Gupta switch (p->sel.family) { 200b8d6d007SNicolas Dichtel case AF_UNSPEC: 201b8d6d007SNicolas Dichtel break; 202b8d6d007SNicolas Dichtel 203b38ff407SAnirudh Gupta case AF_INET: 2046999aae1SSabrina Dubroca if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) { 2056999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)"); 20607bf7908SSteffen Klassert goto out; 2076999aae1SSabrina Dubroca } 20807bf7908SSteffen Klassert 2091da177e4SLinus Torvalds break; 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds case AF_INET6: 212dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 2136999aae1SSabrina Dubroca if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) { 2146999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)"); 21507bf7908SSteffen Klassert goto out; 2166999aae1SSabrina Dubroca } 21707bf7908SSteffen Klassert 2181da177e4SLinus Torvalds break; 2191da177e4SLinus Torvalds #else 2206999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "IPv6 support disabled"); 2211da177e4SLinus Torvalds err = -EAFNOSUPPORT; 2221da177e4SLinus Torvalds goto out; 2231da177e4SLinus Torvalds #endif 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds default: 2266999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid address family in selector"); 2271da177e4SLinus Torvalds goto out; 2283ff50b79SStephen Hemminger } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds err = -EINVAL; 2311da177e4SLinus Torvalds switch (p->id.proto) { 2321da177e4SLinus Torvalds case IPPROTO_AH: 2336999aae1SSabrina Dubroca if (!attrs[XFRMA_ALG_AUTH] && 2346999aae1SSabrina Dubroca !attrs[XFRMA_ALG_AUTH_TRUNC]) { 2356999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Missing required attribute for AH: AUTH_TRUNC or AUTH"); 2366999aae1SSabrina Dubroca goto out; 2376999aae1SSabrina Dubroca } 2386999aae1SSabrina Dubroca 2396999aae1SSabrina Dubroca if (attrs[XFRMA_ALG_AEAD] || 24035a7aa08SThomas Graf attrs[XFRMA_ALG_CRYPT] || 24135d2856bSMartin Willi attrs[XFRMA_ALG_COMP] || 2426999aae1SSabrina Dubroca attrs[XFRMA_TFCPAD]) { 2436999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid attributes for AH: AEAD, CRYPT, COMP, TFCPAD"); 2441da177e4SLinus Torvalds goto out; 2456999aae1SSabrina Dubroca } 2461da177e4SLinus Torvalds break; 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds case IPPROTO_ESP: 2496999aae1SSabrina Dubroca if (attrs[XFRMA_ALG_COMP]) { 2506999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid attribute for ESP: COMP"); 2511a6509d9SHerbert Xu goto out; 2526999aae1SSabrina Dubroca } 2536999aae1SSabrina Dubroca 2541a6509d9SHerbert Xu if (!attrs[XFRMA_ALG_AUTH] && 2554447bb33SMartin Willi !attrs[XFRMA_ALG_AUTH_TRUNC] && 2561a6509d9SHerbert Xu !attrs[XFRMA_ALG_CRYPT] && 2576999aae1SSabrina Dubroca !attrs[XFRMA_ALG_AEAD]) { 2586999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Missing required attribute for ESP: at least one of AUTH, AUTH_TRUNC, CRYPT, AEAD"); 2591a6509d9SHerbert Xu goto out; 2606999aae1SSabrina Dubroca } 2616999aae1SSabrina Dubroca 2621a6509d9SHerbert Xu if ((attrs[XFRMA_ALG_AUTH] || 2634447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 2641a6509d9SHerbert Xu attrs[XFRMA_ALG_CRYPT]) && 2656999aae1SSabrina Dubroca attrs[XFRMA_ALG_AEAD]) { 2666999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid attribute combination for ESP: AEAD can't be used with AUTH, AUTH_TRUNC, CRYPT"); 2671da177e4SLinus Torvalds goto out; 2686999aae1SSabrina Dubroca } 2696999aae1SSabrina Dubroca 27035d2856bSMartin Willi if (attrs[XFRMA_TFCPAD] && 2716999aae1SSabrina Dubroca p->mode != XFRM_MODE_TUNNEL) { 2726999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "TFC padding can only be used in tunnel mode"); 27335d2856bSMartin Willi goto out; 2746999aae1SSabrina Dubroca } 2751da177e4SLinus Torvalds break; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds case IPPROTO_COMP: 2786999aae1SSabrina Dubroca if (!attrs[XFRMA_ALG_COMP]) { 2796999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Missing required attribute for COMP: COMP"); 2806999aae1SSabrina Dubroca goto out; 2816999aae1SSabrina Dubroca } 2826999aae1SSabrina Dubroca 2836999aae1SSabrina Dubroca if (attrs[XFRMA_ALG_AEAD] || 28435a7aa08SThomas Graf attrs[XFRMA_ALG_AUTH] || 2854447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 28635d2856bSMartin Willi attrs[XFRMA_ALG_CRYPT] || 2876999aae1SSabrina Dubroca attrs[XFRMA_TFCPAD]) { 2886999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid attributes for COMP: AEAD, AUTH, AUTH_TRUNC, CRYPT, TFCPAD"); 2891da177e4SLinus Torvalds goto out; 2906999aae1SSabrina Dubroca } 2916999aae1SSabrina Dubroca 2926999aae1SSabrina Dubroca if (ntohl(p->id.spi) >= 0x10000) { 2936999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "SPI is too large for COMP (must be < 0x10000)"); 2946999aae1SSabrina Dubroca goto out; 2956999aae1SSabrina Dubroca } 2961da177e4SLinus Torvalds break; 2971da177e4SLinus Torvalds 298dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 299e23c7194SMasahide NAKAMURA case IPPROTO_DSTOPTS: 300e23c7194SMasahide NAKAMURA case IPPROTO_ROUTING: 30135a7aa08SThomas Graf if (attrs[XFRMA_ALG_COMP] || 30235a7aa08SThomas Graf attrs[XFRMA_ALG_AUTH] || 3034447bb33SMartin Willi attrs[XFRMA_ALG_AUTH_TRUNC] || 3041a6509d9SHerbert Xu attrs[XFRMA_ALG_AEAD] || 30535a7aa08SThomas Graf attrs[XFRMA_ALG_CRYPT] || 30635a7aa08SThomas Graf attrs[XFRMA_ENCAP] || 30735a7aa08SThomas Graf attrs[XFRMA_SEC_CTX] || 3086999aae1SSabrina Dubroca attrs[XFRMA_TFCPAD]) { 3096999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid attributes for DSTOPTS/ROUTING"); 310e23c7194SMasahide NAKAMURA goto out; 3116999aae1SSabrina Dubroca } 3126999aae1SSabrina Dubroca 3136999aae1SSabrina Dubroca if (!attrs[XFRMA_COADDR]) { 3146999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Missing required COADDR attribute for DSTOPTS/ROUTING"); 3156999aae1SSabrina Dubroca goto out; 3166999aae1SSabrina Dubroca } 317e23c7194SMasahide NAKAMURA break; 318e23c7194SMasahide NAKAMURA #endif 319e23c7194SMasahide NAKAMURA 3201da177e4SLinus Torvalds default: 3216999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Unsupported protocol"); 3221da177e4SLinus Torvalds goto out; 3233ff50b79SStephen Hemminger } 3241da177e4SLinus Torvalds 3251fc8fde5SSabrina Dubroca if ((err = verify_aead(attrs, extack))) 3261a6509d9SHerbert Xu goto out; 3271fc8fde5SSabrina Dubroca if ((err = verify_auth_trunc(attrs, extack))) 3284447bb33SMartin Willi goto out; 3291fc8fde5SSabrina Dubroca if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH, extack))) 3301da177e4SLinus Torvalds goto out; 3311fc8fde5SSabrina Dubroca if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT, extack))) 3321da177e4SLinus Torvalds goto out; 3331fc8fde5SSabrina Dubroca if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP, extack))) 3341da177e4SLinus Torvalds goto out; 3356999aae1SSabrina Dubroca if ((err = verify_sec_ctx_len(attrs, extack))) 336df71837dSTrent Jaeger goto out; 337785b87b2SSabrina Dubroca if ((err = verify_replay(p, attrs, extack))) 338d8647b79SSteffen Klassert goto out; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds err = -EINVAL; 3411da177e4SLinus Torvalds switch (p->mode) { 3427e49e6deSMasahide NAKAMURA case XFRM_MODE_TRANSPORT: 3437e49e6deSMasahide NAKAMURA case XFRM_MODE_TUNNEL: 344060f02a3SNoriaki TAKAMIYA case XFRM_MODE_ROUTEOPTIMIZATION: 3450a69452cSDiego Beltrami case XFRM_MODE_BEET: 3461da177e4SLinus Torvalds break; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds default: 3496999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "Unsupported mode"); 3501da177e4SLinus Torvalds goto out; 3513ff50b79SStephen Hemminger } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds err = 0; 3541da177e4SLinus Torvalds 3556999aae1SSabrina Dubroca if (attrs[XFRMA_MTIMER_THRESH]) { 3566999aae1SSabrina Dubroca if (!attrs[XFRMA_ENCAP]) { 3576999aae1SSabrina Dubroca NL_SET_ERR_MSG(extack, "MTIMER_THRESH attribute can only be set on ENCAP states"); 3584e484b3eSAntony Antony err = -EINVAL; 3596999aae1SSabrina Dubroca goto out; 3606999aae1SSabrina Dubroca } 3616999aae1SSabrina Dubroca } 3624e484b3eSAntony Antony 3631da177e4SLinus Torvalds out: 3641da177e4SLinus Torvalds return err; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props, 3686f2f19edSDavid S. Miller struct xfrm_algo_desc *(*get_byname)(const char *, int), 3692b916826SSabrina Dubroca struct nlattr *rta, struct netlink_ext_ack *extack) 3701da177e4SLinus Torvalds { 3711da177e4SLinus Torvalds struct xfrm_algo *p, *ualg; 3721da177e4SLinus Torvalds struct xfrm_algo_desc *algo; 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds if (!rta) 3751da177e4SLinus Torvalds return 0; 3761da177e4SLinus Torvalds 3775424f32eSThomas Graf ualg = nla_data(rta); 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds algo = get_byname(ualg->alg_name, 1); 3802b916826SSabrina Dubroca if (!algo) { 3812b916826SSabrina Dubroca NL_SET_ERR_MSG(extack, "Requested COMP algorithm not found"); 3821da177e4SLinus Torvalds return -ENOSYS; 3832b916826SSabrina Dubroca } 3841da177e4SLinus Torvalds *props = algo->desc.sadb_alg_id; 3851da177e4SLinus Torvalds 3860f99be0dSEric Dumazet p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL); 3871da177e4SLinus Torvalds if (!p) 3881da177e4SLinus Torvalds return -ENOMEM; 3891da177e4SLinus Torvalds 39004ff1260SHerbert Xu strcpy(p->alg_name, algo->name); 3911da177e4SLinus Torvalds *algpp = p; 3921da177e4SLinus Torvalds return 0; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3952b916826SSabrina Dubroca static int attach_crypt(struct xfrm_state *x, struct nlattr *rta, 3962b916826SSabrina Dubroca struct netlink_ext_ack *extack) 39769b0137fSHerbert Xu { 39869b0137fSHerbert Xu struct xfrm_algo *p, *ualg; 39969b0137fSHerbert Xu struct xfrm_algo_desc *algo; 40069b0137fSHerbert Xu 40169b0137fSHerbert Xu if (!rta) 40269b0137fSHerbert Xu return 0; 40369b0137fSHerbert Xu 40469b0137fSHerbert Xu ualg = nla_data(rta); 40569b0137fSHerbert Xu 40669b0137fSHerbert Xu algo = xfrm_ealg_get_byname(ualg->alg_name, 1); 4072b916826SSabrina Dubroca if (!algo) { 4082b916826SSabrina Dubroca NL_SET_ERR_MSG(extack, "Requested CRYPT algorithm not found"); 40969b0137fSHerbert Xu return -ENOSYS; 4102b916826SSabrina Dubroca } 41169b0137fSHerbert Xu x->props.ealgo = algo->desc.sadb_alg_id; 41269b0137fSHerbert Xu 41369b0137fSHerbert Xu p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL); 41469b0137fSHerbert Xu if (!p) 41569b0137fSHerbert Xu return -ENOMEM; 41669b0137fSHerbert Xu 41769b0137fSHerbert Xu strcpy(p->alg_name, algo->name); 41869b0137fSHerbert Xu x->ealg = p; 41969b0137fSHerbert Xu x->geniv = algo->uinfo.encr.geniv; 42069b0137fSHerbert Xu return 0; 42169b0137fSHerbert Xu } 42269b0137fSHerbert Xu 4234447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props, 4242b916826SSabrina Dubroca struct nlattr *rta, struct netlink_ext_ack *extack) 4254447bb33SMartin Willi { 4264447bb33SMartin Willi struct xfrm_algo *ualg; 4274447bb33SMartin Willi struct xfrm_algo_auth *p; 4284447bb33SMartin Willi struct xfrm_algo_desc *algo; 4294447bb33SMartin Willi 4304447bb33SMartin Willi if (!rta) 4314447bb33SMartin Willi return 0; 4324447bb33SMartin Willi 4334447bb33SMartin Willi ualg = nla_data(rta); 4344447bb33SMartin Willi 4354447bb33SMartin Willi algo = xfrm_aalg_get_byname(ualg->alg_name, 1); 4362b916826SSabrina Dubroca if (!algo) { 4372b916826SSabrina Dubroca NL_SET_ERR_MSG(extack, "Requested AUTH algorithm not found"); 4384447bb33SMartin Willi return -ENOSYS; 4392b916826SSabrina Dubroca } 4404447bb33SMartin Willi *props = algo->desc.sadb_alg_id; 4414447bb33SMartin Willi 4424447bb33SMartin Willi p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL); 4434447bb33SMartin Willi if (!p) 4444447bb33SMartin Willi return -ENOMEM; 4454447bb33SMartin Willi 4464447bb33SMartin Willi strcpy(p->alg_name, algo->name); 4474447bb33SMartin Willi p->alg_key_len = ualg->alg_key_len; 4484447bb33SMartin Willi p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; 4494447bb33SMartin Willi memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8); 4504447bb33SMartin Willi 4514447bb33SMartin Willi *algpp = p; 4524447bb33SMartin Willi return 0; 4534447bb33SMartin Willi } 4544447bb33SMartin Willi 4554447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props, 4562b916826SSabrina Dubroca struct nlattr *rta, struct netlink_ext_ack *extack) 4574447bb33SMartin Willi { 4584447bb33SMartin Willi struct xfrm_algo_auth *p, *ualg; 4594447bb33SMartin Willi struct xfrm_algo_desc *algo; 4604447bb33SMartin Willi 4614447bb33SMartin Willi if (!rta) 4624447bb33SMartin Willi return 0; 4634447bb33SMartin Willi 4644447bb33SMartin Willi ualg = nla_data(rta); 4654447bb33SMartin Willi 4664447bb33SMartin Willi algo = xfrm_aalg_get_byname(ualg->alg_name, 1); 4672b916826SSabrina Dubroca if (!algo) { 4682b916826SSabrina Dubroca NL_SET_ERR_MSG(extack, "Requested AUTH_TRUNC algorithm not found"); 4694447bb33SMartin Willi return -ENOSYS; 4702b916826SSabrina Dubroca } 4712b916826SSabrina Dubroca if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) { 4722b916826SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid length requested for truncated ICV"); 4734447bb33SMartin Willi return -EINVAL; 4742b916826SSabrina Dubroca } 4754447bb33SMartin Willi *props = algo->desc.sadb_alg_id; 4764447bb33SMartin Willi 4774447bb33SMartin Willi p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL); 4784447bb33SMartin Willi if (!p) 4794447bb33SMartin Willi return -ENOMEM; 4804447bb33SMartin Willi 4814447bb33SMartin Willi strcpy(p->alg_name, algo->name); 4824447bb33SMartin Willi if (!p->alg_trunc_len) 4834447bb33SMartin Willi p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; 4844447bb33SMartin Willi 4854447bb33SMartin Willi *algpp = p; 4864447bb33SMartin Willi return 0; 4874447bb33SMartin Willi } 4884447bb33SMartin Willi 4892b916826SSabrina Dubroca static int attach_aead(struct xfrm_state *x, struct nlattr *rta, 4902b916826SSabrina Dubroca struct netlink_ext_ack *extack) 4911a6509d9SHerbert Xu { 4921a6509d9SHerbert Xu struct xfrm_algo_aead *p, *ualg; 4931a6509d9SHerbert Xu struct xfrm_algo_desc *algo; 4941a6509d9SHerbert Xu 4951a6509d9SHerbert Xu if (!rta) 4961a6509d9SHerbert Xu return 0; 4971a6509d9SHerbert Xu 4981a6509d9SHerbert Xu ualg = nla_data(rta); 4991a6509d9SHerbert Xu 5001a6509d9SHerbert Xu algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1); 5012b916826SSabrina Dubroca if (!algo) { 5022b916826SSabrina Dubroca NL_SET_ERR_MSG(extack, "Requested AEAD algorithm not found"); 5031a6509d9SHerbert Xu return -ENOSYS; 5042b916826SSabrina Dubroca } 50569b0137fSHerbert Xu x->props.ealgo = algo->desc.sadb_alg_id; 5061a6509d9SHerbert Xu 5071a6509d9SHerbert Xu p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL); 5081a6509d9SHerbert Xu if (!p) 5091a6509d9SHerbert Xu return -ENOMEM; 5101a6509d9SHerbert Xu 5111a6509d9SHerbert Xu strcpy(p->alg_name, algo->name); 51269b0137fSHerbert Xu x->aead = p; 51369b0137fSHerbert Xu x->geniv = algo->uinfo.aead.geniv; 5141a6509d9SHerbert Xu return 0; 5151a6509d9SHerbert Xu } 5161a6509d9SHerbert Xu 517e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn, 518e2b19125SSteffen Klassert struct nlattr *rp) 519e2b19125SSteffen Klassert { 520e2b19125SSteffen Klassert struct xfrm_replay_state_esn *up; 5215e708e47SAlexey Dobriyan unsigned int ulen; 522e2b19125SSteffen Klassert 523e2b19125SSteffen Klassert if (!replay_esn || !rp) 524e2b19125SSteffen Klassert return 0; 525e2b19125SSteffen Klassert 526e2b19125SSteffen Klassert up = nla_data(rp); 527ecd79187SMathias Krause ulen = xfrm_replay_state_esn_len(up); 528e2b19125SSteffen Klassert 529f843ee6dSAndy Whitcroft /* Check the overall length and the internal bitmap length to avoid 530f843ee6dSAndy Whitcroft * potential overflow. */ 5315e708e47SAlexey Dobriyan if (nla_len(rp) < (int)ulen || 532f843ee6dSAndy Whitcroft xfrm_replay_state_esn_len(replay_esn) != ulen || 533f843ee6dSAndy Whitcroft replay_esn->bmp_len != up->bmp_len) 534e2b19125SSteffen Klassert return -EINVAL; 535e2b19125SSteffen Klassert 536677e806dSAndy Whitcroft if (up->replay_window > up->bmp_len * sizeof(__u32) * 8) 537677e806dSAndy Whitcroft return -EINVAL; 538677e806dSAndy Whitcroft 539e2b19125SSteffen Klassert return 0; 540e2b19125SSteffen Klassert } 541e2b19125SSteffen Klassert 542d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn, 543d8647b79SSteffen Klassert struct xfrm_replay_state_esn **preplay_esn, 544d8647b79SSteffen Klassert struct nlattr *rta) 545d8647b79SSteffen Klassert { 546d8647b79SSteffen Klassert struct xfrm_replay_state_esn *p, *pp, *up; 5475e708e47SAlexey Dobriyan unsigned int klen, ulen; 548d8647b79SSteffen Klassert 549d8647b79SSteffen Klassert if (!rta) 550d8647b79SSteffen Klassert return 0; 551d8647b79SSteffen Klassert 552d8647b79SSteffen Klassert up = nla_data(rta); 553ecd79187SMathias Krause klen = xfrm_replay_state_esn_len(up); 5545e708e47SAlexey Dobriyan ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up); 555d8647b79SSteffen Klassert 556ecd79187SMathias Krause p = kzalloc(klen, GFP_KERNEL); 557d8647b79SSteffen Klassert if (!p) 558d8647b79SSteffen Klassert return -ENOMEM; 559d8647b79SSteffen Klassert 560ecd79187SMathias Krause pp = kzalloc(klen, GFP_KERNEL); 561d8647b79SSteffen Klassert if (!pp) { 562d8647b79SSteffen Klassert kfree(p); 563d8647b79SSteffen Klassert return -ENOMEM; 564d8647b79SSteffen Klassert } 565d8647b79SSteffen Klassert 566ecd79187SMathias Krause memcpy(p, up, ulen); 567ecd79187SMathias Krause memcpy(pp, up, ulen); 568ecd79187SMathias Krause 569d8647b79SSteffen Klassert *replay_esn = p; 570d8647b79SSteffen Klassert *preplay_esn = pp; 571d8647b79SSteffen Klassert 572d8647b79SSteffen Klassert return 0; 573d8647b79SSteffen Klassert } 574d8647b79SSteffen Klassert 575a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx) 576df71837dSTrent Jaeger { 577a1b831f2SAlexey Dobriyan unsigned int len = 0; 578df71837dSTrent Jaeger 579df71837dSTrent Jaeger if (xfrm_ctx) { 580df71837dSTrent Jaeger len += sizeof(struct xfrm_user_sec_ctx); 581df71837dSTrent Jaeger len += xfrm_ctx->ctx_len; 582df71837dSTrent Jaeger } 583df71837dSTrent Jaeger return len; 584df71837dSTrent Jaeger } 585df71837dSTrent Jaeger 5861da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 5871da177e4SLinus Torvalds { 5881da177e4SLinus Torvalds memcpy(&x->id, &p->id, sizeof(x->id)); 5891da177e4SLinus Torvalds memcpy(&x->sel, &p->sel, sizeof(x->sel)); 5901da177e4SLinus Torvalds memcpy(&x->lft, &p->lft, sizeof(x->lft)); 5911da177e4SLinus Torvalds x->props.mode = p->mode; 59233fce60dSFan Du x->props.replay_window = min_t(unsigned int, p->replay_window, 59333fce60dSFan Du sizeof(x->replay.bitmap) * 8); 5941da177e4SLinus Torvalds x->props.reqid = p->reqid; 5951da177e4SLinus Torvalds x->props.family = p->family; 59654489c14SDavid S. Miller memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr)); 5971da177e4SLinus Torvalds x->props.flags = p->flags; 598196b0036SHerbert Xu 599ccf9b3b8SSteffen Klassert if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC)) 600196b0036SHerbert Xu x->sel.family = p->family; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 603d51d081dSJamal Hadi Salim /* 604d51d081dSJamal Hadi Salim * someday when pfkey also has support, we could have the code 605d51d081dSJamal Hadi Salim * somehow made shareable and move it to xfrm_state.c - JHS 606d51d081dSJamal Hadi Salim * 607d51d081dSJamal Hadi Salim */ 608e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs, 609e3ac104dSMathias Krause int update_esn) 610d51d081dSJamal Hadi Salim { 6115424f32eSThomas Graf struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 612e3ac104dSMathias Krause struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL; 6135424f32eSThomas Graf struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 6145424f32eSThomas Graf struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; 6155424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; 6164e484b3eSAntony Antony struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH]; 617d51d081dSJamal Hadi Salim 618d8647b79SSteffen Klassert if (re) { 619d8647b79SSteffen Klassert struct xfrm_replay_state_esn *replay_esn; 620d8647b79SSteffen Klassert replay_esn = nla_data(re); 621d8647b79SSteffen Klassert memcpy(x->replay_esn, replay_esn, 622d8647b79SSteffen Klassert xfrm_replay_state_esn_len(replay_esn)); 623d8647b79SSteffen Klassert memcpy(x->preplay_esn, replay_esn, 624d8647b79SSteffen Klassert xfrm_replay_state_esn_len(replay_esn)); 625d8647b79SSteffen Klassert } 626d8647b79SSteffen Klassert 627d51d081dSJamal Hadi Salim if (rp) { 628d51d081dSJamal Hadi Salim struct xfrm_replay_state *replay; 6295424f32eSThomas Graf replay = nla_data(rp); 630d51d081dSJamal Hadi Salim memcpy(&x->replay, replay, sizeof(*replay)); 631d51d081dSJamal Hadi Salim memcpy(&x->preplay, replay, sizeof(*replay)); 632d51d081dSJamal Hadi Salim } 633d51d081dSJamal Hadi Salim 634d51d081dSJamal Hadi Salim if (lt) { 635d51d081dSJamal Hadi Salim struct xfrm_lifetime_cur *ltime; 6365424f32eSThomas Graf ltime = nla_data(lt); 637d51d081dSJamal Hadi Salim x->curlft.bytes = ltime->bytes; 638d51d081dSJamal Hadi Salim x->curlft.packets = ltime->packets; 639d51d081dSJamal Hadi Salim x->curlft.add_time = ltime->add_time; 640d51d081dSJamal Hadi Salim x->curlft.use_time = ltime->use_time; 641d51d081dSJamal Hadi Salim } 642d51d081dSJamal Hadi Salim 643cf5cb79fSThomas Graf if (et) 6445424f32eSThomas Graf x->replay_maxage = nla_get_u32(et); 645d51d081dSJamal Hadi Salim 646cf5cb79fSThomas Graf if (rt) 6475424f32eSThomas Graf x->replay_maxdiff = nla_get_u32(rt); 6484e484b3eSAntony Antony 6494e484b3eSAntony Antony if (mt) 6504e484b3eSAntony Antony x->mapping_maxage = nla_get_u32(mt); 651d51d081dSJamal Hadi Salim } 652d51d081dSJamal Hadi Salim 6539b42c1f1SSteffen Klassert static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m) 6549b42c1f1SSteffen Klassert { 6559b42c1f1SSteffen Klassert if (attrs[XFRMA_SET_MARK]) { 6569b42c1f1SSteffen Klassert m->v = nla_get_u32(attrs[XFRMA_SET_MARK]); 6579b42c1f1SSteffen Klassert if (attrs[XFRMA_SET_MARK_MASK]) 6589b42c1f1SSteffen Klassert m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]); 6599b42c1f1SSteffen Klassert else 6609b42c1f1SSteffen Klassert m->m = 0xffffffff; 6619b42c1f1SSteffen Klassert } else { 6629b42c1f1SSteffen Klassert m->v = m->m = 0; 6639b42c1f1SSteffen Klassert } 6649b42c1f1SSteffen Klassert } 6659b42c1f1SSteffen Klassert 666fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net, 667fc34acd3SAlexey Dobriyan struct xfrm_usersa_info *p, 6685424f32eSThomas Graf struct nlattr **attrs, 669adb5c33eSSabrina Dubroca int *errp, 670adb5c33eSSabrina Dubroca struct netlink_ext_ack *extack) 6711da177e4SLinus Torvalds { 672fc34acd3SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 6731da177e4SLinus Torvalds int err = -ENOMEM; 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds if (!x) 6761da177e4SLinus Torvalds goto error_no_put; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds copy_from_user_state(x, p); 6791da177e4SLinus Torvalds 6806fd06963SSteffen Klassert if (attrs[XFRMA_ENCAP]) { 6816fd06963SSteffen Klassert x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), 6826fd06963SSteffen Klassert sizeof(*x->encap), GFP_KERNEL); 6836fd06963SSteffen Klassert if (x->encap == NULL) 6846fd06963SSteffen Klassert goto error; 6856fd06963SSteffen Klassert } 6866fd06963SSteffen Klassert 6876fd06963SSteffen Klassert if (attrs[XFRMA_COADDR]) { 6886fd06963SSteffen Klassert x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]), 6896fd06963SSteffen Klassert sizeof(*x->coaddr), GFP_KERNEL); 6906fd06963SSteffen Klassert if (x->coaddr == NULL) 6916fd06963SSteffen Klassert goto error; 6926fd06963SSteffen Klassert } 6936fd06963SSteffen Klassert 694a947b0a9SNicolas Dichtel if (attrs[XFRMA_SA_EXTRA_FLAGS]) 695a947b0a9SNicolas Dichtel x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]); 696a947b0a9SNicolas Dichtel 6972b916826SSabrina Dubroca if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD], extack))) 6981a6509d9SHerbert Xu goto error; 6994447bb33SMartin Willi if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo, 7002b916826SSabrina Dubroca attrs[XFRMA_ALG_AUTH_TRUNC], extack))) 7014447bb33SMartin Willi goto error; 7024447bb33SMartin Willi if (!x->props.aalgo) { 7034447bb33SMartin Willi if ((err = attach_auth(&x->aalg, &x->props.aalgo, 7042b916826SSabrina Dubroca attrs[XFRMA_ALG_AUTH], extack))) 7051da177e4SLinus Torvalds goto error; 7064447bb33SMartin Willi } 7072b916826SSabrina Dubroca if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT], extack))) 7081da177e4SLinus Torvalds goto error; 7091da177e4SLinus Torvalds if ((err = attach_one_algo(&x->calg, &x->props.calgo, 7101da177e4SLinus Torvalds xfrm_calg_get_byname, 7112b916826SSabrina Dubroca attrs[XFRMA_ALG_COMP], extack))) 7121da177e4SLinus Torvalds goto error; 713fd21150aSThomas Graf 71435d2856bSMartin Willi if (attrs[XFRMA_TFCPAD]) 71535d2856bSMartin Willi x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]); 71635d2856bSMartin Willi 7176f26b61eSJamal Hadi Salim xfrm_mark_get(attrs, &x->mark); 7186f26b61eSJamal Hadi Salim 7199b42c1f1SSteffen Klassert xfrm_smark_init(attrs, &x->props.smark); 720077fbac4SLorenzo Colitti 721a3d9001bSKai Lueke if (attrs[XFRMA_IF_ID]) 7227e652640SSteffen Klassert x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 723fd21150aSThomas Graf 724741f9a10SSabrina Dubroca err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV], extack); 7251da177e4SLinus Torvalds if (err) 7261da177e4SLinus Torvalds goto error; 7271da177e4SLinus Torvalds 7282f30ea50SMathias Krause if (attrs[XFRMA_SEC_CTX]) { 7292f30ea50SMathias Krause err = security_xfrm_state_alloc(x, 7302f30ea50SMathias Krause nla_data(attrs[XFRMA_SEC_CTX])); 7312f30ea50SMathias Krause if (err) 732df71837dSTrent Jaeger goto error; 7332f30ea50SMathias Krause } 734df71837dSTrent Jaeger 735d8647b79SSteffen Klassert if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn, 736d8647b79SSteffen Klassert attrs[XFRMA_REPLAY_ESN_VAL]))) 737d8647b79SSteffen Klassert goto error; 738d8647b79SSteffen Klassert 7391da177e4SLinus Torvalds x->km.seq = p->seq; 740b27aeadbSAlexey Dobriyan x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth; 741d51d081dSJamal Hadi Salim /* sysctl_xfrm_aevent_etime is in 100ms units */ 742b27aeadbSAlexey Dobriyan x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M; 743d51d081dSJamal Hadi Salim 7441cf9a3aeSSabrina Dubroca if ((err = xfrm_init_replay(x, extack))) 7459fdc4883SSteffen Klassert goto error; 746d51d081dSJamal Hadi Salim 7479fdc4883SSteffen Klassert /* override default values from above */ 748e3ac104dSMathias Krause xfrm_update_ae_params(x, attrs, 0); 7491da177e4SLinus Torvalds 750cc01572eSYossi Kuperman /* configure the hardware if offload is requested */ 751cc01572eSYossi Kuperman if (attrs[XFRMA_OFFLOAD_DEV]) { 752cc01572eSYossi Kuperman err = xfrm_dev_state_add(net, x, 753adb5c33eSSabrina Dubroca nla_data(attrs[XFRMA_OFFLOAD_DEV]), 754adb5c33eSSabrina Dubroca extack); 755cc01572eSYossi Kuperman if (err) 756cc01572eSYossi Kuperman goto error; 757cc01572eSYossi Kuperman } 758cc01572eSYossi Kuperman 7591da177e4SLinus Torvalds return x; 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds error: 7621da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 7631da177e4SLinus Torvalds xfrm_state_put(x); 7641da177e4SLinus Torvalds error_no_put: 7651da177e4SLinus Torvalds *errp = err; 7661da177e4SLinus Torvalds return NULL; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 76922e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 7703bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 7711da177e4SLinus Torvalds { 772fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 7737b67c857SThomas Graf struct xfrm_usersa_info *p = nlmsg_data(nlh); 7741da177e4SLinus Torvalds struct xfrm_state *x; 7751da177e4SLinus Torvalds int err; 77626b15dadSJamal Hadi Salim struct km_event c; 7771da177e4SLinus Torvalds 7786999aae1SSabrina Dubroca err = verify_newsa_info(p, attrs, extack); 7791da177e4SLinus Torvalds if (err) 7801da177e4SLinus Torvalds return err; 7811da177e4SLinus Torvalds 782adb5c33eSSabrina Dubroca x = xfrm_state_construct(net, p, attrs, &err, extack); 7831da177e4SLinus Torvalds if (!x) 7841da177e4SLinus Torvalds return err; 7851da177e4SLinus Torvalds 78626b15dadSJamal Hadi Salim xfrm_state_hold(x); 7871da177e4SLinus Torvalds if (nlh->nlmsg_type == XFRM_MSG_NEWSA) 7881da177e4SLinus Torvalds err = xfrm_state_add(x); 7891da177e4SLinus Torvalds else 7901da177e4SLinus Torvalds err = xfrm_state_update(x); 7911da177e4SLinus Torvalds 7922e71029eSTetsuo Handa xfrm_audit_state_add(x, err ? 0 : 1, true); 793161a09e7SJoy Latten 7941da177e4SLinus Torvalds if (err < 0) { 7951da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 796c5d4d7d8SSteffen Klassert xfrm_dev_state_delete(x); 79721380b81SHerbert Xu __xfrm_state_put(x); 7987d6dfe1fSPatrick McHardy goto out; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 801cc01572eSYossi Kuperman if (x->km.state == XFRM_STATE_VOID) 802cc01572eSYossi Kuperman x->km.state = XFRM_STATE_VALID; 803cc01572eSYossi Kuperman 80426b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 80515e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 806f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 80726b15dadSJamal Hadi Salim 80826b15dadSJamal Hadi Salim km_state_notify(x, &c); 8097d6dfe1fSPatrick McHardy out: 81026b15dadSJamal Hadi Salim xfrm_state_put(x); 8111da177e4SLinus Torvalds return err; 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 814fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net, 815fc34acd3SAlexey Dobriyan struct xfrm_usersa_id *p, 8165424f32eSThomas Graf struct nlattr **attrs, 817eb2971b6SMasahide NAKAMURA int *errp) 818eb2971b6SMasahide NAKAMURA { 819eb2971b6SMasahide NAKAMURA struct xfrm_state *x = NULL; 8206f26b61eSJamal Hadi Salim struct xfrm_mark m; 821eb2971b6SMasahide NAKAMURA int err; 8226f26b61eSJamal Hadi Salim u32 mark = xfrm_mark_get(attrs, &m); 823eb2971b6SMasahide NAKAMURA 824eb2971b6SMasahide NAKAMURA if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { 825eb2971b6SMasahide NAKAMURA err = -ESRCH; 8266f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family); 827eb2971b6SMasahide NAKAMURA } else { 828eb2971b6SMasahide NAKAMURA xfrm_address_t *saddr = NULL; 829eb2971b6SMasahide NAKAMURA 83035a7aa08SThomas Graf verify_one_addr(attrs, XFRMA_SRCADDR, &saddr); 831eb2971b6SMasahide NAKAMURA if (!saddr) { 832eb2971b6SMasahide NAKAMURA err = -EINVAL; 833eb2971b6SMasahide NAKAMURA goto out; 834eb2971b6SMasahide NAKAMURA } 835eb2971b6SMasahide NAKAMURA 8369abbffeeSMasahide NAKAMURA err = -ESRCH; 8376f26b61eSJamal Hadi Salim x = xfrm_state_lookup_byaddr(net, mark, 8386f26b61eSJamal Hadi Salim &p->daddr, saddr, 839221df1edSAlexey Dobriyan p->proto, p->family); 840eb2971b6SMasahide NAKAMURA } 841eb2971b6SMasahide NAKAMURA 842eb2971b6SMasahide NAKAMURA out: 843eb2971b6SMasahide NAKAMURA if (!x && errp) 844eb2971b6SMasahide NAKAMURA *errp = err; 845eb2971b6SMasahide NAKAMURA return x; 846eb2971b6SMasahide NAKAMURA } 847eb2971b6SMasahide NAKAMURA 84822e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 8493bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 8501da177e4SLinus Torvalds { 851fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 8521da177e4SLinus Torvalds struct xfrm_state *x; 853eb2971b6SMasahide NAKAMURA int err = -ESRCH; 85426b15dadSJamal Hadi Salim struct km_event c; 8557b67c857SThomas Graf struct xfrm_usersa_id *p = nlmsg_data(nlh); 8561da177e4SLinus Torvalds 857fc34acd3SAlexey Dobriyan x = xfrm_user_state_lookup(net, p, attrs, &err); 8581da177e4SLinus Torvalds if (x == NULL) 859eb2971b6SMasahide NAKAMURA return err; 8601da177e4SLinus Torvalds 8616f68dc37SDavid S. Miller if ((err = security_xfrm_state_delete(x)) != 0) 862c8c05a8eSCatherine Zhang goto out; 863c8c05a8eSCatherine Zhang 8641da177e4SLinus Torvalds if (xfrm_state_kern(x)) { 865c8c05a8eSCatherine Zhang err = -EPERM; 866c8c05a8eSCatherine Zhang goto out; 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 86926b15dadSJamal Hadi Salim err = xfrm_state_delete(x); 870161a09e7SJoy Latten 871c8c05a8eSCatherine Zhang if (err < 0) 872c8c05a8eSCatherine Zhang goto out; 87326b15dadSJamal Hadi Salim 87426b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 87515e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 876f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 87726b15dadSJamal Hadi Salim km_state_notify(x, &c); 8781da177e4SLinus Torvalds 879c8c05a8eSCatherine Zhang out: 8802e71029eSTetsuo Handa xfrm_audit_state_delete(x, err ? 0 : 1, true); 881c8c05a8eSCatherine Zhang xfrm_state_put(x); 88226b15dadSJamal Hadi Salim return err; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 8861da177e4SLinus Torvalds { 887f778a636SMathias Krause memset(p, 0, sizeof(*p)); 8881da177e4SLinus Torvalds memcpy(&p->id, &x->id, sizeof(p->id)); 8891da177e4SLinus Torvalds memcpy(&p->sel, &x->sel, sizeof(p->sel)); 8901da177e4SLinus Torvalds memcpy(&p->lft, &x->lft, sizeof(p->lft)); 8911da177e4SLinus Torvalds memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); 892e33d4f13SSowmini Varadhan put_unaligned(x->stats.replay_window, &p->stats.replay_window); 893e33d4f13SSowmini Varadhan put_unaligned(x->stats.replay, &p->stats.replay); 894e33d4f13SSowmini Varadhan put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed); 89554489c14SDavid S. Miller memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); 8961da177e4SLinus Torvalds p->mode = x->props.mode; 8971da177e4SLinus Torvalds p->replay_window = x->props.replay_window; 8981da177e4SLinus Torvalds p->reqid = x->props.reqid; 8991da177e4SLinus Torvalds p->family = x->props.family; 9001da177e4SLinus Torvalds p->flags = x->props.flags; 9011da177e4SLinus Torvalds p->seq = x->km.seq; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds struct xfrm_dump_info { 9051da177e4SLinus Torvalds struct sk_buff *in_skb; 9061da177e4SLinus Torvalds struct sk_buff *out_skb; 9071da177e4SLinus Torvalds u32 nlmsg_seq; 9081da177e4SLinus Torvalds u16 nlmsg_flags; 9091da177e4SLinus Torvalds }; 9101da177e4SLinus Torvalds 911c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) 912c0144beaSThomas Graf { 913c0144beaSThomas Graf struct xfrm_user_sec_ctx *uctx; 914c0144beaSThomas Graf struct nlattr *attr; 91568325d3bSHerbert Xu int ctx_size = sizeof(*uctx) + s->ctx_len; 916c0144beaSThomas Graf 917c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size); 918c0144beaSThomas Graf if (attr == NULL) 919c0144beaSThomas Graf return -EMSGSIZE; 920c0144beaSThomas Graf 921c0144beaSThomas Graf uctx = nla_data(attr); 922c0144beaSThomas Graf uctx->exttype = XFRMA_SEC_CTX; 923c0144beaSThomas Graf uctx->len = ctx_size; 924c0144beaSThomas Graf uctx->ctx_doi = s->ctx_doi; 925c0144beaSThomas Graf uctx->ctx_alg = s->ctx_alg; 926c0144beaSThomas Graf uctx->ctx_len = s->ctx_len; 927c0144beaSThomas Graf memcpy(uctx + 1, s->ctx_str, s->ctx_len); 928c0144beaSThomas Graf 929c0144beaSThomas Graf return 0; 930c0144beaSThomas Graf } 931c0144beaSThomas Graf 93287e0a94eSLeon Romanovsky static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb) 933d77e38e6SSteffen Klassert { 934d77e38e6SSteffen Klassert struct xfrm_user_offload *xuo; 935d77e38e6SSteffen Klassert struct nlattr *attr; 936d77e38e6SSteffen Klassert 937d77e38e6SSteffen Klassert attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo)); 938d77e38e6SSteffen Klassert if (attr == NULL) 939d77e38e6SSteffen Klassert return -EMSGSIZE; 940d77e38e6SSteffen Klassert 941d77e38e6SSteffen Klassert xuo = nla_data(attr); 9425fe0d4bdSMathias Krause memset(xuo, 0, sizeof(*xuo)); 943d77e38e6SSteffen Klassert xuo->ifindex = xso->dev->ifindex; 944482db2f1SLeon Romanovsky if (xso->dir == XFRM_DEV_OFFLOAD_IN) 945482db2f1SLeon Romanovsky xuo->flags = XFRM_OFFLOAD_INBOUND; 946d77e38e6SSteffen Klassert 947d77e38e6SSteffen Klassert return 0; 948d77e38e6SSteffen Klassert } 949d77e38e6SSteffen Klassert 950c7a5899eSAntony Antony static bool xfrm_redact(void) 951c7a5899eSAntony Antony { 952c7a5899eSAntony Antony return IS_ENABLED(CONFIG_SECURITY) && 953c7a5899eSAntony Antony security_locked_down(LOCKDOWN_XFRM_SECRET); 954c7a5899eSAntony Antony } 955c7a5899eSAntony Antony 9564447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb) 9574447bb33SMartin Willi { 9584447bb33SMartin Willi struct xfrm_algo *algo; 959c7a5899eSAntony Antony struct xfrm_algo_auth *ap; 9604447bb33SMartin Willi struct nlattr *nla; 961c7a5899eSAntony Antony bool redact_secret = xfrm_redact(); 9624447bb33SMartin Willi 9634447bb33SMartin Willi nla = nla_reserve(skb, XFRMA_ALG_AUTH, 9644447bb33SMartin Willi sizeof(*algo) + (auth->alg_key_len + 7) / 8); 9654447bb33SMartin Willi if (!nla) 9664447bb33SMartin Willi return -EMSGSIZE; 9674447bb33SMartin Willi algo = nla_data(nla); 9684c87308bSMathias Krause strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name)); 969c7a5899eSAntony Antony 970c7a5899eSAntony Antony if (redact_secret && auth->alg_key_len) 971c7a5899eSAntony Antony memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8); 972c7a5899eSAntony Antony else 973c7a5899eSAntony Antony memcpy(algo->alg_key, auth->alg_key, 974c7a5899eSAntony Antony (auth->alg_key_len + 7) / 8); 9754447bb33SMartin Willi algo->alg_key_len = auth->alg_key_len; 9764447bb33SMartin Willi 977c7a5899eSAntony Antony nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth)); 978c7a5899eSAntony Antony if (!nla) 979c7a5899eSAntony Antony return -EMSGSIZE; 980c7a5899eSAntony Antony ap = nla_data(nla); 981c7a5899eSAntony Antony memcpy(ap, auth, sizeof(struct xfrm_algo_auth)); 982c7a5899eSAntony Antony if (redact_secret && auth->alg_key_len) 983c7a5899eSAntony Antony memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8); 984c7a5899eSAntony Antony else 985c7a5899eSAntony Antony memcpy(ap->alg_key, auth->alg_key, 986c7a5899eSAntony Antony (auth->alg_key_len + 7) / 8); 987c7a5899eSAntony Antony return 0; 988c7a5899eSAntony Antony } 989c7a5899eSAntony Antony 990c7a5899eSAntony Antony static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb) 991c7a5899eSAntony Antony { 992c7a5899eSAntony Antony struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead)); 993c7a5899eSAntony Antony struct xfrm_algo_aead *ap; 994c7a5899eSAntony Antony bool redact_secret = xfrm_redact(); 995c7a5899eSAntony Antony 996c7a5899eSAntony Antony if (!nla) 997c7a5899eSAntony Antony return -EMSGSIZE; 998c7a5899eSAntony Antony 999c7a5899eSAntony Antony ap = nla_data(nla); 1000c7a5899eSAntony Antony memcpy(ap, aead, sizeof(*aead)); 1001c7a5899eSAntony Antony 1002c7a5899eSAntony Antony if (redact_secret && aead->alg_key_len) 1003c7a5899eSAntony Antony memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8); 1004c7a5899eSAntony Antony else 1005c7a5899eSAntony Antony memcpy(ap->alg_key, aead->alg_key, 1006c7a5899eSAntony Antony (aead->alg_key_len + 7) / 8); 1007c7a5899eSAntony Antony return 0; 1008c7a5899eSAntony Antony } 1009c7a5899eSAntony Antony 1010c7a5899eSAntony Antony static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb) 1011c7a5899eSAntony Antony { 1012c7a5899eSAntony Antony struct xfrm_algo *ap; 1013c7a5899eSAntony Antony bool redact_secret = xfrm_redact(); 1014c7a5899eSAntony Antony struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT, 1015c7a5899eSAntony Antony xfrm_alg_len(ealg)); 1016c7a5899eSAntony Antony if (!nla) 1017c7a5899eSAntony Antony return -EMSGSIZE; 1018c7a5899eSAntony Antony 1019c7a5899eSAntony Antony ap = nla_data(nla); 1020c7a5899eSAntony Antony memcpy(ap, ealg, sizeof(*ealg)); 1021c7a5899eSAntony Antony 1022c7a5899eSAntony Antony if (redact_secret && ealg->alg_key_len) 1023c7a5899eSAntony Antony memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8); 1024c7a5899eSAntony Antony else 1025c7a5899eSAntony Antony memcpy(ap->alg_key, ealg->alg_key, 1026c7a5899eSAntony Antony (ealg->alg_key_len + 7) / 8); 1027c7a5899eSAntony Antony 10284447bb33SMartin Willi return 0; 10294447bb33SMartin Willi } 10304447bb33SMartin Willi 10319b42c1f1SSteffen Klassert static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m) 10329b42c1f1SSteffen Klassert { 10339b42c1f1SSteffen Klassert int ret = 0; 10349b42c1f1SSteffen Klassert 10359b42c1f1SSteffen Klassert if (m->v | m->m) { 10369b42c1f1SSteffen Klassert ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v); 10379b42c1f1SSteffen Klassert if (!ret) 10389b42c1f1SSteffen Klassert ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m); 10399b42c1f1SSteffen Klassert } 10409b42c1f1SSteffen Klassert return ret; 10419b42c1f1SSteffen Klassert } 10429b42c1f1SSteffen Klassert 104368325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */ 104468325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x, 104568325d3bSHerbert Xu struct xfrm_usersa_info *p, 104668325d3bSHerbert Xu struct sk_buff *skb) 10471da177e4SLinus Torvalds { 10481d1e34ddSDavid S. Miller int ret = 0; 10491d1e34ddSDavid S. Miller 10501da177e4SLinus Torvalds copy_to_user_state(x, p); 10511da177e4SLinus Torvalds 1052a947b0a9SNicolas Dichtel if (x->props.extra_flags) { 1053a947b0a9SNicolas Dichtel ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS, 1054a947b0a9SNicolas Dichtel x->props.extra_flags); 1055a947b0a9SNicolas Dichtel if (ret) 1056a947b0a9SNicolas Dichtel goto out; 1057a947b0a9SNicolas Dichtel } 1058a947b0a9SNicolas Dichtel 10591d1e34ddSDavid S. Miller if (x->coaddr) { 10601d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); 10611d1e34ddSDavid S. Miller if (ret) 10621d1e34ddSDavid S. Miller goto out; 10631d1e34ddSDavid S. Miller } 10641d1e34ddSDavid S. Miller if (x->lastused) { 1065de95c4a4SNicolas Dichtel ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused, 1066de95c4a4SNicolas Dichtel XFRMA_PAD); 10671d1e34ddSDavid S. Miller if (ret) 10681d1e34ddSDavid S. Miller goto out; 10691d1e34ddSDavid S. Miller } 10701d1e34ddSDavid S. Miller if (x->aead) { 1071c7a5899eSAntony Antony ret = copy_to_user_aead(x->aead, skb); 10721d1e34ddSDavid S. Miller if (ret) 10731d1e34ddSDavid S. Miller goto out; 10741d1e34ddSDavid S. Miller } 10751d1e34ddSDavid S. Miller if (x->aalg) { 10761d1e34ddSDavid S. Miller ret = copy_to_user_auth(x->aalg, skb); 10771d1e34ddSDavid S. Miller if (ret) 10781d1e34ddSDavid S. Miller goto out; 10791d1e34ddSDavid S. Miller } 10801d1e34ddSDavid S. Miller if (x->ealg) { 1081c7a5899eSAntony Antony ret = copy_to_user_ealg(x->ealg, skb); 10821d1e34ddSDavid S. Miller if (ret) 10831d1e34ddSDavid S. Miller goto out; 10841d1e34ddSDavid S. Miller } 10851d1e34ddSDavid S. Miller if (x->calg) { 10861d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); 10871d1e34ddSDavid S. Miller if (ret) 10881d1e34ddSDavid S. Miller goto out; 10891d1e34ddSDavid S. Miller } 10901d1e34ddSDavid S. Miller if (x->encap) { 10911d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); 10921d1e34ddSDavid S. Miller if (ret) 10931d1e34ddSDavid S. Miller goto out; 10941d1e34ddSDavid S. Miller } 10951d1e34ddSDavid S. Miller if (x->tfcpad) { 10961d1e34ddSDavid S. Miller ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad); 10971d1e34ddSDavid S. Miller if (ret) 10981d1e34ddSDavid S. Miller goto out; 10991d1e34ddSDavid S. Miller } 11001d1e34ddSDavid S. Miller ret = xfrm_mark_put(skb, &x->mark); 11011d1e34ddSDavid S. Miller if (ret) 11021d1e34ddSDavid S. Miller goto out; 11039b42c1f1SSteffen Klassert 11049b42c1f1SSteffen Klassert ret = xfrm_smark_put(skb, &x->props.smark); 11059b42c1f1SSteffen Klassert if (ret) 11069b42c1f1SSteffen Klassert goto out; 11079b42c1f1SSteffen Klassert 1108f293a5e3Sdingzhi if (x->replay_esn) 11091d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 1110d0fde795SDavid S. Miller xfrm_replay_state_esn_len(x->replay_esn), 11111d1e34ddSDavid S. Miller x->replay_esn); 1112f293a5e3Sdingzhi else 1113f293a5e3Sdingzhi ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 1114f293a5e3Sdingzhi &x->replay); 11151d1e34ddSDavid S. Miller if (ret) 11161d1e34ddSDavid S. Miller goto out; 1117d77e38e6SSteffen Klassert if(x->xso.dev) 1118d77e38e6SSteffen Klassert ret = copy_user_offload(&x->xso, skb); 1119d77e38e6SSteffen Klassert if (ret) 1120d77e38e6SSteffen Klassert goto out; 11217e652640SSteffen Klassert if (x->if_id) { 11227e652640SSteffen Klassert ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id); 1123077fbac4SLorenzo Colitti if (ret) 1124077fbac4SLorenzo Colitti goto out; 1125077fbac4SLorenzo Colitti } 11264e484b3eSAntony Antony if (x->security) { 11278598112dSSteffen Klassert ret = copy_sec_ctx(x->security, skb); 11284e484b3eSAntony Antony if (ret) 11294e484b3eSAntony Antony goto out; 11304e484b3eSAntony Antony } 11314e484b3eSAntony Antony if (x->mapping_maxage) 11324e484b3eSAntony Antony ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage); 11331d1e34ddSDavid S. Miller out: 11341d1e34ddSDavid S. Miller return ret; 113568325d3bSHerbert Xu } 113668325d3bSHerbert Xu 113768325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr) 113868325d3bSHerbert Xu { 113968325d3bSHerbert Xu struct xfrm_dump_info *sp = ptr; 114068325d3bSHerbert Xu struct sk_buff *in_skb = sp->in_skb; 114168325d3bSHerbert Xu struct sk_buff *skb = sp->out_skb; 11425f3eea6bSDmitry Safonov struct xfrm_translator *xtr; 114368325d3bSHerbert Xu struct xfrm_usersa_info *p; 114468325d3bSHerbert Xu struct nlmsghdr *nlh; 114568325d3bSHerbert Xu int err; 114668325d3bSHerbert Xu 114715e47304SEric W. Biederman nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq, 114868325d3bSHerbert Xu XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags); 114968325d3bSHerbert Xu if (nlh == NULL) 115068325d3bSHerbert Xu return -EMSGSIZE; 115168325d3bSHerbert Xu 115268325d3bSHerbert Xu p = nlmsg_data(nlh); 115368325d3bSHerbert Xu 115468325d3bSHerbert Xu err = copy_to_user_state_extra(x, p, skb); 11551d1e34ddSDavid S. Miller if (err) { 11569825069dSThomas Graf nlmsg_cancel(skb, nlh); 115768325d3bSHerbert Xu return err; 11581da177e4SLinus Torvalds } 11591d1e34ddSDavid S. Miller nlmsg_end(skb, nlh); 11605f3eea6bSDmitry Safonov 11615f3eea6bSDmitry Safonov xtr = xfrm_get_translator(); 11625f3eea6bSDmitry Safonov if (xtr) { 11635f3eea6bSDmitry Safonov err = xtr->alloc_compat(skb, nlh); 11645f3eea6bSDmitry Safonov 11655f3eea6bSDmitry Safonov xfrm_put_translator(xtr); 11665f3eea6bSDmitry Safonov if (err) { 11675f3eea6bSDmitry Safonov nlmsg_cancel(skb, nlh); 11685f3eea6bSDmitry Safonov return err; 11695f3eea6bSDmitry Safonov } 11705f3eea6bSDmitry Safonov } 11715f3eea6bSDmitry Safonov 11721d1e34ddSDavid S. Miller return 0; 11731d1e34ddSDavid S. Miller } 11741da177e4SLinus Torvalds 11754c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb) 11764c563f76STimo Teras { 11774c563f76STimo Teras struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 1178283bc9f3SFan Du struct sock *sk = cb->skb->sk; 1179283bc9f3SFan Du struct net *net = sock_net(sk); 1180283bc9f3SFan Du 11811ba5bf99SVegard Nossum if (cb->args[0]) 1182283bc9f3SFan Du xfrm_state_walk_done(walk, net); 11834c563f76STimo Teras return 0; 11844c563f76STimo Teras } 11854c563f76STimo Teras 11861da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) 11871da177e4SLinus Torvalds { 1188fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 11894c563f76STimo Teras struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 11901da177e4SLinus Torvalds struct xfrm_dump_info info; 11911da177e4SLinus Torvalds 11924c563f76STimo Teras BUILD_BUG_ON(sizeof(struct xfrm_state_walk) > 11934c563f76STimo Teras sizeof(cb->args) - sizeof(cb->args[0])); 11944c563f76STimo Teras 11951da177e4SLinus Torvalds info.in_skb = cb->skb; 11961da177e4SLinus Torvalds info.out_skb = skb; 11971da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 11981da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 11994c563f76STimo Teras 12004c563f76STimo Teras if (!cb->args[0]) { 1201d3623099SNicolas Dichtel struct nlattr *attrs[XFRMA_MAX+1]; 1202870a2df4SNicolas Dichtel struct xfrm_address_filter *filter = NULL; 1203d3623099SNicolas Dichtel u8 proto = 0; 1204d3623099SNicolas Dichtel int err; 1205d3623099SNicolas Dichtel 12068cb08174SJohannes Berg err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX, 12078cb08174SJohannes Berg xfrma_policy, cb->extack); 1208d3623099SNicolas Dichtel if (err < 0) 1209d3623099SNicolas Dichtel return err; 1210d3623099SNicolas Dichtel 1211870a2df4SNicolas Dichtel if (attrs[XFRMA_ADDRESS_FILTER]) { 1212df367561SAndrzej Hajda filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]), 1213df367561SAndrzej Hajda sizeof(*filter), GFP_KERNEL); 1214d3623099SNicolas Dichtel if (filter == NULL) 1215d3623099SNicolas Dichtel return -ENOMEM; 1216d3623099SNicolas Dichtel } 1217d3623099SNicolas Dichtel 1218d3623099SNicolas Dichtel if (attrs[XFRMA_PROTO]) 1219d3623099SNicolas Dichtel proto = nla_get_u8(attrs[XFRMA_PROTO]); 1220d3623099SNicolas Dichtel 1221d3623099SNicolas Dichtel xfrm_state_walk_init(walk, proto, filter); 12221ba5bf99SVegard Nossum cb->args[0] = 1; 12234c563f76STimo Teras } 12244c563f76STimo Teras 1225fc34acd3SAlexey Dobriyan (void) xfrm_state_walk(net, walk, dump_one_state, &info); 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds return skb->len; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, 12311da177e4SLinus Torvalds struct xfrm_state *x, u32 seq) 12321da177e4SLinus Torvalds { 12331da177e4SLinus Torvalds struct xfrm_dump_info info; 12341da177e4SLinus Torvalds struct sk_buff *skb; 1235864745d2SMathias Krause int err; 12361da177e4SLinus Torvalds 12377deb2264SThomas Graf skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 12381da177e4SLinus Torvalds if (!skb) 12391da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds info.in_skb = in_skb; 12421da177e4SLinus Torvalds info.out_skb = skb; 12431da177e4SLinus Torvalds info.nlmsg_seq = seq; 12441da177e4SLinus Torvalds info.nlmsg_flags = 0; 12451da177e4SLinus Torvalds 1246864745d2SMathias Krause err = dump_one_state(x, 0, &info); 1247864745d2SMathias Krause if (err) { 12481da177e4SLinus Torvalds kfree_skb(skb); 1249864745d2SMathias Krause return ERR_PTR(err); 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds return skb; 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 125521ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available. 125621ee543eSMichal Kubecek * Must be called with RCU read lock. 125721ee543eSMichal Kubecek */ 125821ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb, 125921ee543eSMichal Kubecek u32 pid, unsigned int group) 126021ee543eSMichal Kubecek { 126121ee543eSMichal Kubecek struct sock *nlsk = rcu_dereference(net->xfrm.nlsk); 12625461fc0cSDmitry Safonov struct xfrm_translator *xtr; 126321ee543eSMichal Kubecek 126486126b77SFlorian Westphal if (!nlsk) { 126586126b77SFlorian Westphal kfree_skb(skb); 126686126b77SFlorian Westphal return -EPIPE; 126786126b77SFlorian Westphal } 126886126b77SFlorian Westphal 12695461fc0cSDmitry Safonov xtr = xfrm_get_translator(); 12705461fc0cSDmitry Safonov if (xtr) { 12715461fc0cSDmitry Safonov int err = xtr->alloc_compat(skb, nlmsg_hdr(skb)); 12725461fc0cSDmitry Safonov 12735461fc0cSDmitry Safonov xfrm_put_translator(xtr); 12745461fc0cSDmitry Safonov if (err) { 12755461fc0cSDmitry Safonov kfree_skb(skb); 12765461fc0cSDmitry Safonov return err; 12775461fc0cSDmitry Safonov } 12785461fc0cSDmitry Safonov } 12795461fc0cSDmitry Safonov 128021ee543eSMichal Kubecek return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC); 128121ee543eSMichal Kubecek } 128221ee543eSMichal Kubecek 1283a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void) 12847deb2264SThomas Graf { 12857deb2264SThomas Graf return NLMSG_ALIGN(4) 12867deb2264SThomas Graf + nla_total_size(sizeof(struct xfrmu_spdinfo)) 1287880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhinfo)) 1288880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhthresh)) 1289880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhthresh)); 12907deb2264SThomas Graf } 12917deb2264SThomas Graf 1292e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net, 129315e47304SEric W. Biederman u32 portid, u32 seq, u32 flags) 1294ecfd6b18SJamal Hadi Salim { 12955a6d3416SJamal Hadi Salim struct xfrmk_spdinfo si; 12965a6d3416SJamal Hadi Salim struct xfrmu_spdinfo spc; 12975a6d3416SJamal Hadi Salim struct xfrmu_spdhinfo sph; 1298880a6fabSChristophe Gouault struct xfrmu_spdhthresh spt4, spt6; 1299ecfd6b18SJamal Hadi Salim struct nlmsghdr *nlh; 13001d1e34ddSDavid S. Miller int err; 1301ecfd6b18SJamal Hadi Salim u32 *f; 1302880a6fabSChristophe Gouault unsigned lseq; 1303ecfd6b18SJamal Hadi Salim 130415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); 130525985edcSLucas De Marchi if (nlh == NULL) /* shouldn't really happen ... */ 1306ecfd6b18SJamal Hadi Salim return -EMSGSIZE; 1307ecfd6b18SJamal Hadi Salim 1308ecfd6b18SJamal Hadi Salim f = nlmsg_data(nlh); 1309ecfd6b18SJamal Hadi Salim *f = flags; 1310e071041bSAlexey Dobriyan xfrm_spd_getinfo(net, &si); 13115a6d3416SJamal Hadi Salim spc.incnt = si.incnt; 13125a6d3416SJamal Hadi Salim spc.outcnt = si.outcnt; 13135a6d3416SJamal Hadi Salim spc.fwdcnt = si.fwdcnt; 13145a6d3416SJamal Hadi Salim spc.inscnt = si.inscnt; 13155a6d3416SJamal Hadi Salim spc.outscnt = si.outscnt; 13165a6d3416SJamal Hadi Salim spc.fwdscnt = si.fwdscnt; 13175a6d3416SJamal Hadi Salim sph.spdhcnt = si.spdhcnt; 13185a6d3416SJamal Hadi Salim sph.spdhmcnt = si.spdhmcnt; 1319ecfd6b18SJamal Hadi Salim 1320880a6fabSChristophe Gouault do { 1321880a6fabSChristophe Gouault lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock); 1322880a6fabSChristophe Gouault 1323880a6fabSChristophe Gouault spt4.lbits = net->xfrm.policy_hthresh.lbits4; 1324880a6fabSChristophe Gouault spt4.rbits = net->xfrm.policy_hthresh.rbits4; 1325880a6fabSChristophe Gouault spt6.lbits = net->xfrm.policy_hthresh.lbits6; 1326880a6fabSChristophe Gouault spt6.rbits = net->xfrm.policy_hthresh.rbits6; 1327880a6fabSChristophe Gouault } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq)); 1328880a6fabSChristophe Gouault 13291d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc); 13301d1e34ddSDavid S. Miller if (!err) 13311d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph); 1332880a6fabSChristophe Gouault if (!err) 1333880a6fabSChristophe Gouault err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4); 1334880a6fabSChristophe Gouault if (!err) 1335880a6fabSChristophe Gouault err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6); 13361d1e34ddSDavid S. Miller if (err) { 13371d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 13381d1e34ddSDavid S. Miller return err; 13391d1e34ddSDavid S. Miller } 1340ecfd6b18SJamal Hadi Salim 1341053c095aSJohannes Berg nlmsg_end(skb, nlh); 1342053c095aSJohannes Berg return 0; 1343ecfd6b18SJamal Hadi Salim } 1344ecfd6b18SJamal Hadi Salim 1345880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 13463bec6c3eSSabrina Dubroca struct nlattr **attrs, 13473bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 1348880a6fabSChristophe Gouault { 1349880a6fabSChristophe Gouault struct net *net = sock_net(skb->sk); 1350880a6fabSChristophe Gouault struct xfrmu_spdhthresh *thresh4 = NULL; 1351880a6fabSChristophe Gouault struct xfrmu_spdhthresh *thresh6 = NULL; 1352880a6fabSChristophe Gouault 1353880a6fabSChristophe Gouault /* selector prefixlen thresholds to hash policies */ 1354880a6fabSChristophe Gouault if (attrs[XFRMA_SPD_IPV4_HTHRESH]) { 1355880a6fabSChristophe Gouault struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH]; 1356880a6fabSChristophe Gouault 1357880a6fabSChristophe Gouault if (nla_len(rta) < sizeof(*thresh4)) 1358880a6fabSChristophe Gouault return -EINVAL; 1359880a6fabSChristophe Gouault thresh4 = nla_data(rta); 1360880a6fabSChristophe Gouault if (thresh4->lbits > 32 || thresh4->rbits > 32) 1361880a6fabSChristophe Gouault return -EINVAL; 1362880a6fabSChristophe Gouault } 1363880a6fabSChristophe Gouault if (attrs[XFRMA_SPD_IPV6_HTHRESH]) { 1364880a6fabSChristophe Gouault struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH]; 1365880a6fabSChristophe Gouault 1366880a6fabSChristophe Gouault if (nla_len(rta) < sizeof(*thresh6)) 1367880a6fabSChristophe Gouault return -EINVAL; 1368880a6fabSChristophe Gouault thresh6 = nla_data(rta); 1369880a6fabSChristophe Gouault if (thresh6->lbits > 128 || thresh6->rbits > 128) 1370880a6fabSChristophe Gouault return -EINVAL; 1371880a6fabSChristophe Gouault } 1372880a6fabSChristophe Gouault 1373880a6fabSChristophe Gouault if (thresh4 || thresh6) { 1374880a6fabSChristophe Gouault write_seqlock(&net->xfrm.policy_hthresh.lock); 1375880a6fabSChristophe Gouault if (thresh4) { 1376880a6fabSChristophe Gouault net->xfrm.policy_hthresh.lbits4 = thresh4->lbits; 1377880a6fabSChristophe Gouault net->xfrm.policy_hthresh.rbits4 = thresh4->rbits; 1378880a6fabSChristophe Gouault } 1379880a6fabSChristophe Gouault if (thresh6) { 1380880a6fabSChristophe Gouault net->xfrm.policy_hthresh.lbits6 = thresh6->lbits; 1381880a6fabSChristophe Gouault net->xfrm.policy_hthresh.rbits6 = thresh6->rbits; 1382880a6fabSChristophe Gouault } 1383880a6fabSChristophe Gouault write_sequnlock(&net->xfrm.policy_hthresh.lock); 1384880a6fabSChristophe Gouault 1385880a6fabSChristophe Gouault xfrm_policy_hash_rebuild(net); 1386880a6fabSChristophe Gouault } 1387880a6fabSChristophe Gouault 1388880a6fabSChristophe Gouault return 0; 1389880a6fabSChristophe Gouault } 1390880a6fabSChristophe Gouault 1391ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 13923bec6c3eSSabrina Dubroca struct nlattr **attrs, 13933bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 1394ecfd6b18SJamal Hadi Salim { 1395a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 1396ecfd6b18SJamal Hadi Salim struct sk_buff *r_skb; 13977b67c857SThomas Graf u32 *flags = nlmsg_data(nlh); 139815e47304SEric W. Biederman u32 sportid = NETLINK_CB(skb).portid; 1399ecfd6b18SJamal Hadi Salim u32 seq = nlh->nlmsg_seq; 14002fc5f83bSGustavo A. R. Silva int err; 1401ecfd6b18SJamal Hadi Salim 14027deb2264SThomas Graf r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); 1403ecfd6b18SJamal Hadi Salim if (r_skb == NULL) 1404ecfd6b18SJamal Hadi Salim return -ENOMEM; 1405ecfd6b18SJamal Hadi Salim 14062fc5f83bSGustavo A. R. Silva err = build_spdinfo(r_skb, net, sportid, seq, *flags); 14072fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 1408ecfd6b18SJamal Hadi Salim 140915e47304SEric W. Biederman return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 1410ecfd6b18SJamal Hadi Salim } 1411ecfd6b18SJamal Hadi Salim 1412a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void) 14137deb2264SThomas Graf { 14147deb2264SThomas Graf return NLMSG_ALIGN(4) 14157deb2264SThomas Graf + nla_total_size(sizeof(struct xfrmu_sadhinfo)) 14167deb2264SThomas Graf + nla_total_size(4); /* XFRMA_SAD_CNT */ 14177deb2264SThomas Graf } 14187deb2264SThomas Graf 1419e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net, 142015e47304SEric W. Biederman u32 portid, u32 seq, u32 flags) 142128d8909bSJamal Hadi Salim { 1422af11e316SJamal Hadi Salim struct xfrmk_sadinfo si; 1423af11e316SJamal Hadi Salim struct xfrmu_sadhinfo sh; 142428d8909bSJamal Hadi Salim struct nlmsghdr *nlh; 14251d1e34ddSDavid S. Miller int err; 142628d8909bSJamal Hadi Salim u32 *f; 142728d8909bSJamal Hadi Salim 142815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); 142925985edcSLucas De Marchi if (nlh == NULL) /* shouldn't really happen ... */ 143028d8909bSJamal Hadi Salim return -EMSGSIZE; 143128d8909bSJamal Hadi Salim 143228d8909bSJamal Hadi Salim f = nlmsg_data(nlh); 143328d8909bSJamal Hadi Salim *f = flags; 1434e071041bSAlexey Dobriyan xfrm_sad_getinfo(net, &si); 143528d8909bSJamal Hadi Salim 1436af11e316SJamal Hadi Salim sh.sadhmcnt = si.sadhmcnt; 1437af11e316SJamal Hadi Salim sh.sadhcnt = si.sadhcnt; 1438af11e316SJamal Hadi Salim 14391d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt); 14401d1e34ddSDavid S. Miller if (!err) 14411d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); 14421d1e34ddSDavid S. Miller if (err) { 14431d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 14441d1e34ddSDavid S. Miller return err; 14451d1e34ddSDavid S. Miller } 144628d8909bSJamal Hadi Salim 1447053c095aSJohannes Berg nlmsg_end(skb, nlh); 1448053c095aSJohannes Berg return 0; 144928d8909bSJamal Hadi Salim } 145028d8909bSJamal Hadi Salim 145128d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 14523bec6c3eSSabrina Dubroca struct nlattr **attrs, 14533bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 145428d8909bSJamal Hadi Salim { 1455a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 145628d8909bSJamal Hadi Salim struct sk_buff *r_skb; 14577b67c857SThomas Graf u32 *flags = nlmsg_data(nlh); 145815e47304SEric W. Biederman u32 sportid = NETLINK_CB(skb).portid; 145928d8909bSJamal Hadi Salim u32 seq = nlh->nlmsg_seq; 14602fc5f83bSGustavo A. R. Silva int err; 146128d8909bSJamal Hadi Salim 14627deb2264SThomas Graf r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); 146328d8909bSJamal Hadi Salim if (r_skb == NULL) 146428d8909bSJamal Hadi Salim return -ENOMEM; 146528d8909bSJamal Hadi Salim 14662fc5f83bSGustavo A. R. Silva err = build_sadinfo(r_skb, net, sportid, seq, *flags); 14672fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 146828d8909bSJamal Hadi Salim 146915e47304SEric W. Biederman return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 147028d8909bSJamal Hadi Salim } 147128d8909bSJamal Hadi Salim 147222e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 14733bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 14741da177e4SLinus Torvalds { 1475fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 14767b67c857SThomas Graf struct xfrm_usersa_id *p = nlmsg_data(nlh); 14771da177e4SLinus Torvalds struct xfrm_state *x; 14781da177e4SLinus Torvalds struct sk_buff *resp_skb; 1479eb2971b6SMasahide NAKAMURA int err = -ESRCH; 14801da177e4SLinus Torvalds 1481fc34acd3SAlexey Dobriyan x = xfrm_user_state_lookup(net, p, attrs, &err); 14821da177e4SLinus Torvalds if (x == NULL) 14831da177e4SLinus Torvalds goto out_noput; 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 14861da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 14871da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 14881da177e4SLinus Torvalds } else { 148915e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds xfrm_state_put(x); 14921da177e4SLinus Torvalds out_noput: 14931da177e4SLinus Torvalds return err; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds 149622e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, 14973bec6c3eSSabrina Dubroca struct nlattr **attrs, 14983bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 14991da177e4SLinus Torvalds { 1500fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 15011da177e4SLinus Torvalds struct xfrm_state *x; 15021da177e4SLinus Torvalds struct xfrm_userspi_info *p; 15035f3eea6bSDmitry Safonov struct xfrm_translator *xtr; 15041da177e4SLinus Torvalds struct sk_buff *resp_skb; 15051da177e4SLinus Torvalds xfrm_address_t *daddr; 15061da177e4SLinus Torvalds int family; 15071da177e4SLinus Torvalds int err; 15086f26b61eSJamal Hadi Salim u32 mark; 15096f26b61eSJamal Hadi Salim struct xfrm_mark m; 15107e652640SSteffen Klassert u32 if_id = 0; 15111da177e4SLinus Torvalds 15127b67c857SThomas Graf p = nlmsg_data(nlh); 1513776e9dd9SFan Du err = verify_spi_info(p->info.id.proto, p->min, p->max); 15141da177e4SLinus Torvalds if (err) 15151da177e4SLinus Torvalds goto out_noput; 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds family = p->info.family; 15181da177e4SLinus Torvalds daddr = &p->info.id.daddr; 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds x = NULL; 15216f26b61eSJamal Hadi Salim 15226f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 15237e652640SSteffen Klassert 1524a3d9001bSKai Lueke if (attrs[XFRMA_IF_ID]) 15257e652640SSteffen Klassert if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 15267e652640SSteffen Klassert 15271da177e4SLinus Torvalds if (p->info.seq) { 15286f26b61eSJamal Hadi Salim x = xfrm_find_acq_byseq(net, mark, p->info.seq); 152970e94e66SYOSHIFUJI Hideaki / 吉藤英明 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) { 15301da177e4SLinus Torvalds xfrm_state_put(x); 15311da177e4SLinus Torvalds x = NULL; 15321da177e4SLinus Torvalds } 15331da177e4SLinus Torvalds } 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds if (!x) 15366f26b61eSJamal Hadi Salim x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid, 15377e652640SSteffen Klassert if_id, p->info.id.proto, daddr, 15381da177e4SLinus Torvalds &p->info.saddr, 1, 15391da177e4SLinus Torvalds family); 15401da177e4SLinus Torvalds err = -ENOENT; 1541f157c416SSabrina Dubroca if (!x) 15421da177e4SLinus Torvalds goto out_noput; 15431da177e4SLinus Torvalds 1544658b219eSHerbert Xu err = xfrm_alloc_spi(x, p->min, p->max); 1545658b219eSHerbert Xu if (err) 1546658b219eSHerbert Xu goto out; 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 15491da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 15501da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 15511da177e4SLinus Torvalds goto out; 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds 15545f3eea6bSDmitry Safonov xtr = xfrm_get_translator(); 15555f3eea6bSDmitry Safonov if (xtr) { 15565f3eea6bSDmitry Safonov err = xtr->alloc_compat(skb, nlmsg_hdr(skb)); 15575f3eea6bSDmitry Safonov 15585f3eea6bSDmitry Safonov xfrm_put_translator(xtr); 15595f3eea6bSDmitry Safonov if (err) { 15605f3eea6bSDmitry Safonov kfree_skb(resp_skb); 15615f3eea6bSDmitry Safonov goto out; 15625f3eea6bSDmitry Safonov } 15635f3eea6bSDmitry Safonov } 15645f3eea6bSDmitry Safonov 156515e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds out: 15681da177e4SLinus Torvalds xfrm_state_put(x); 15691da177e4SLinus Torvalds out_noput: 15701da177e4SLinus Torvalds return err; 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds 157324fc544fSSabrina Dubroca static int verify_policy_dir(u8 dir, struct netlink_ext_ack *extack) 15741da177e4SLinus Torvalds { 15751da177e4SLinus Torvalds switch (dir) { 15761da177e4SLinus Torvalds case XFRM_POLICY_IN: 15771da177e4SLinus Torvalds case XFRM_POLICY_OUT: 15781da177e4SLinus Torvalds case XFRM_POLICY_FWD: 15791da177e4SLinus Torvalds break; 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds default: 158224fc544fSSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid policy direction"); 15831da177e4SLinus Torvalds return -EINVAL; 15843ff50b79SStephen Hemminger } 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds return 0; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 1589fb7deabaSSabrina Dubroca static int verify_policy_type(u8 type, struct netlink_ext_ack *extack) 1590f7b6983fSMasahide NAKAMURA { 1591f7b6983fSMasahide NAKAMURA switch (type) { 1592f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_MAIN: 1593f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 1594f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_SUB: 1595f7b6983fSMasahide NAKAMURA #endif 1596f7b6983fSMasahide NAKAMURA break; 1597f7b6983fSMasahide NAKAMURA 1598f7b6983fSMasahide NAKAMURA default: 1599fb7deabaSSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid policy type"); 1600f7b6983fSMasahide NAKAMURA return -EINVAL; 16013ff50b79SStephen Hemminger } 1602f7b6983fSMasahide NAKAMURA 1603f7b6983fSMasahide NAKAMURA return 0; 1604f7b6983fSMasahide NAKAMURA } 1605f7b6983fSMasahide NAKAMURA 1606ec2b4f01SSabrina Dubroca static int verify_newpolicy_info(struct xfrm_userpolicy_info *p, 1607ec2b4f01SSabrina Dubroca struct netlink_ext_ack *extack) 16081da177e4SLinus Torvalds { 1609e682adf0SFan Du int ret; 1610e682adf0SFan Du 16111da177e4SLinus Torvalds switch (p->share) { 16121da177e4SLinus Torvalds case XFRM_SHARE_ANY: 16131da177e4SLinus Torvalds case XFRM_SHARE_SESSION: 16141da177e4SLinus Torvalds case XFRM_SHARE_USER: 16151da177e4SLinus Torvalds case XFRM_SHARE_UNIQUE: 16161da177e4SLinus Torvalds break; 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds default: 1619ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid policy share"); 16201da177e4SLinus Torvalds return -EINVAL; 16213ff50b79SStephen Hemminger } 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds switch (p->action) { 16241da177e4SLinus Torvalds case XFRM_POLICY_ALLOW: 16251da177e4SLinus Torvalds case XFRM_POLICY_BLOCK: 16261da177e4SLinus Torvalds break; 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds default: 1629ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid policy action"); 16301da177e4SLinus Torvalds return -EINVAL; 16313ff50b79SStephen Hemminger } 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds switch (p->sel.family) { 16341da177e4SLinus Torvalds case AF_INET: 1635ec2b4f01SSabrina Dubroca if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) { 1636ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)"); 163707bf7908SSteffen Klassert return -EINVAL; 1638ec2b4f01SSabrina Dubroca } 163907bf7908SSteffen Klassert 16401da177e4SLinus Torvalds break; 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds case AF_INET6: 1643dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1644ec2b4f01SSabrina Dubroca if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) { 1645ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)"); 164607bf7908SSteffen Klassert return -EINVAL; 1647ec2b4f01SSabrina Dubroca } 164807bf7908SSteffen Klassert 16491da177e4SLinus Torvalds break; 16501da177e4SLinus Torvalds #else 1651ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "IPv6 support disabled"); 16521da177e4SLinus Torvalds return -EAFNOSUPPORT; 16531da177e4SLinus Torvalds #endif 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds default: 1656ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid selector family"); 16571da177e4SLinus Torvalds return -EINVAL; 16583ff50b79SStephen Hemminger } 16591da177e4SLinus Torvalds 166024fc544fSSabrina Dubroca ret = verify_policy_dir(p->dir, extack); 1661e682adf0SFan Du if (ret) 1662e682adf0SFan Du return ret; 1663ec2b4f01SSabrina Dubroca if (p->index && (xfrm_policy_id2dir(p->index) != p->dir)) { 1664ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Policy index doesn't match direction"); 1665e682adf0SFan Du return -EINVAL; 1666ec2b4f01SSabrina Dubroca } 1667e682adf0SFan Du 1668e682adf0SFan Du return 0; 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds 16715424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) 1672df71837dSTrent Jaeger { 16735424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1674df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx; 1675df71837dSTrent Jaeger 1676df71837dSTrent Jaeger if (!rt) 1677df71837dSTrent Jaeger return 0; 1678df71837dSTrent Jaeger 16795424f32eSThomas Graf uctx = nla_data(rt); 168052a4c640SNikolay Aleksandrov return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL); 1681df71837dSTrent Jaeger } 1682df71837dSTrent Jaeger 16831da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, 16841da177e4SLinus Torvalds int nr) 16851da177e4SLinus Torvalds { 16861da177e4SLinus Torvalds int i; 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds xp->xfrm_nr = nr; 16891da177e4SLinus Torvalds for (i = 0; i < nr; i++, ut++) { 16901da177e4SLinus Torvalds struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); 16931da177e4SLinus Torvalds memcpy(&t->saddr, &ut->saddr, 16941da177e4SLinus Torvalds sizeof(xfrm_address_t)); 16951da177e4SLinus Torvalds t->reqid = ut->reqid; 16961da177e4SLinus Torvalds t->mode = ut->mode; 16971da177e4SLinus Torvalds t->share = ut->share; 16981da177e4SLinus Torvalds t->optional = ut->optional; 16991da177e4SLinus Torvalds t->aalgos = ut->aalgos; 17001da177e4SLinus Torvalds t->ealgos = ut->ealgos; 17011da177e4SLinus Torvalds t->calgos = ut->calgos; 1702c5d18e98SHerbert Xu /* If all masks are ~0, then we allow all algorithms. */ 1703c5d18e98SHerbert Xu t->allalgs = !~(t->aalgos & t->ealgos & t->calgos); 17048511d01dSMiika Komu t->encap_family = ut->family; 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds 1708d37bed89SSabrina Dubroca static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family, 1709d37bed89SSabrina Dubroca struct netlink_ext_ack *extack) 1710b4ad86bfSDavid S. Miller { 1711732706afSSteffen Klassert u16 prev_family; 1712b4ad86bfSDavid S. Miller int i; 1713b4ad86bfSDavid S. Miller 1714d37bed89SSabrina Dubroca if (nr > XFRM_MAX_DEPTH) { 1715d37bed89SSabrina Dubroca NL_SET_ERR_MSG(extack, "Template count must be <= XFRM_MAX_DEPTH (" __stringify(XFRM_MAX_DEPTH) ")"); 1716b4ad86bfSDavid S. Miller return -EINVAL; 1717d37bed89SSabrina Dubroca } 1718b4ad86bfSDavid S. Miller 1719732706afSSteffen Klassert prev_family = family; 1720732706afSSteffen Klassert 1721b4ad86bfSDavid S. Miller for (i = 0; i < nr; i++) { 1722b4ad86bfSDavid S. Miller /* We never validated the ut->family value, so many 1723b4ad86bfSDavid S. Miller * applications simply leave it at zero. The check was 1724b4ad86bfSDavid S. Miller * never made and ut->family was ignored because all 1725b4ad86bfSDavid S. Miller * templates could be assumed to have the same family as 1726b4ad86bfSDavid S. Miller * the policy itself. Now that we will have ipv4-in-ipv6 1727b4ad86bfSDavid S. Miller * and ipv6-in-ipv4 tunnels, this is no longer true. 1728b4ad86bfSDavid S. Miller */ 1729b4ad86bfSDavid S. Miller if (!ut[i].family) 1730b4ad86bfSDavid S. Miller ut[i].family = family; 1731b4ad86bfSDavid S. Miller 173235e61038SFlorian Westphal switch (ut[i].mode) { 173335e61038SFlorian Westphal case XFRM_MODE_TUNNEL: 173435e61038SFlorian Westphal case XFRM_MODE_BEET: 173535e61038SFlorian Westphal break; 173635e61038SFlorian Westphal default: 1737d37bed89SSabrina Dubroca if (ut[i].family != prev_family) { 1738d37bed89SSabrina Dubroca NL_SET_ERR_MSG(extack, "Mode in template doesn't support a family change"); 1739732706afSSteffen Klassert return -EINVAL; 1740d37bed89SSabrina Dubroca } 174135e61038SFlorian Westphal break; 174235e61038SFlorian Westphal } 1743d37bed89SSabrina Dubroca if (ut[i].mode >= XFRM_MODE_MAX) { 1744d37bed89SSabrina Dubroca NL_SET_ERR_MSG(extack, "Mode in template must be < XFRM_MODE_MAX (" __stringify(XFRM_MODE_MAX) ")"); 174532bf94fbSSean Tranchetti return -EINVAL; 1746d37bed89SSabrina Dubroca } 174732bf94fbSSean Tranchetti 1748732706afSSteffen Klassert prev_family = ut[i].family; 1749732706afSSteffen Klassert 1750b4ad86bfSDavid S. Miller switch (ut[i].family) { 1751b4ad86bfSDavid S. Miller case AF_INET: 1752b4ad86bfSDavid S. Miller break; 1753dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1754b4ad86bfSDavid S. Miller case AF_INET6: 1755b4ad86bfSDavid S. Miller break; 1756b4ad86bfSDavid S. Miller #endif 1757b4ad86bfSDavid S. Miller default: 1758d37bed89SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid family in template"); 1759b4ad86bfSDavid S. Miller return -EINVAL; 17603ff50b79SStephen Hemminger } 17616a53b759SCong Wang 1762d37bed89SSabrina Dubroca if (!xfrm_id_proto_valid(ut[i].id.proto)) { 1763d37bed89SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid XFRM protocol in template"); 17646a53b759SCong Wang return -EINVAL; 17656a53b759SCong Wang } 1766d37bed89SSabrina Dubroca } 17676a53b759SCong Wang 1768b4ad86bfSDavid S. Miller return 0; 1769b4ad86bfSDavid S. Miller } 1770b4ad86bfSDavid S. Miller 1771d37bed89SSabrina Dubroca static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs, 1772d37bed89SSabrina Dubroca struct netlink_ext_ack *extack) 17731da177e4SLinus Torvalds { 17745424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_TMPL]; 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds if (!rt) { 17771da177e4SLinus Torvalds pol->xfrm_nr = 0; 17781da177e4SLinus Torvalds } else { 17795424f32eSThomas Graf struct xfrm_user_tmpl *utmpl = nla_data(rt); 17805424f32eSThomas Graf int nr = nla_len(rt) / sizeof(*utmpl); 1781b4ad86bfSDavid S. Miller int err; 17821da177e4SLinus Torvalds 1783d37bed89SSabrina Dubroca err = validate_tmpl(nr, utmpl, pol->family, extack); 1784b4ad86bfSDavid S. Miller if (err) 1785b4ad86bfSDavid S. Miller return err; 17861da177e4SLinus Torvalds 17875424f32eSThomas Graf copy_templates(pol, utmpl, nr); 17881da177e4SLinus Torvalds } 17891da177e4SLinus Torvalds return 0; 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds 1792fb7deabaSSabrina Dubroca static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs, 1793fb7deabaSSabrina Dubroca struct netlink_ext_ack *extack) 1794f7b6983fSMasahide NAKAMURA { 17955424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; 1796f7b6983fSMasahide NAKAMURA struct xfrm_userpolicy_type *upt; 1797b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 1798f7b6983fSMasahide NAKAMURA int err; 1799f7b6983fSMasahide NAKAMURA 1800f7b6983fSMasahide NAKAMURA if (rt) { 18015424f32eSThomas Graf upt = nla_data(rt); 1802f7b6983fSMasahide NAKAMURA type = upt->type; 1803f7b6983fSMasahide NAKAMURA } 1804f7b6983fSMasahide NAKAMURA 1805fb7deabaSSabrina Dubroca err = verify_policy_type(type, extack); 1806f7b6983fSMasahide NAKAMURA if (err) 1807f7b6983fSMasahide NAKAMURA return err; 1808f7b6983fSMasahide NAKAMURA 1809f7b6983fSMasahide NAKAMURA *tp = type; 1810f7b6983fSMasahide NAKAMURA return 0; 1811f7b6983fSMasahide NAKAMURA } 1812f7b6983fSMasahide NAKAMURA 18131da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) 18141da177e4SLinus Torvalds { 18151da177e4SLinus Torvalds xp->priority = p->priority; 18161da177e4SLinus Torvalds xp->index = p->index; 18171da177e4SLinus Torvalds memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); 18181da177e4SLinus Torvalds memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); 18191da177e4SLinus Torvalds xp->action = p->action; 18201da177e4SLinus Torvalds xp->flags = p->flags; 18211da177e4SLinus Torvalds xp->family = p->sel.family; 18221da177e4SLinus Torvalds /* XXX xp->share = p->share; */ 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) 18261da177e4SLinus Torvalds { 18277b789836SMathias Krause memset(p, 0, sizeof(*p)); 18281da177e4SLinus Torvalds memcpy(&p->sel, &xp->selector, sizeof(p->sel)); 18291da177e4SLinus Torvalds memcpy(&p->lft, &xp->lft, sizeof(p->lft)); 18301da177e4SLinus Torvalds memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); 18311da177e4SLinus Torvalds p->priority = xp->priority; 18321da177e4SLinus Torvalds p->index = xp->index; 18331da177e4SLinus Torvalds p->sel.family = xp->family; 18341da177e4SLinus Torvalds p->dir = dir; 18351da177e4SLinus Torvalds p->action = xp->action; 18361da177e4SLinus Torvalds p->flags = xp->flags; 18371da177e4SLinus Torvalds p->share = XFRM_SHARE_ANY; /* XXX xp->share */ 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 1840fb7deabaSSabrina Dubroca static struct xfrm_policy *xfrm_policy_construct(struct net *net, 1841fb7deabaSSabrina Dubroca struct xfrm_userpolicy_info *p, 1842fb7deabaSSabrina Dubroca struct nlattr **attrs, 1843fb7deabaSSabrina Dubroca int *errp, 1844fb7deabaSSabrina Dubroca struct netlink_ext_ack *extack) 18451da177e4SLinus Torvalds { 1846fc34acd3SAlexey Dobriyan struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL); 18471da177e4SLinus Torvalds int err; 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds if (!xp) { 18501da177e4SLinus Torvalds *errp = -ENOMEM; 18511da177e4SLinus Torvalds return NULL; 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds copy_from_user_policy(xp, p); 1855df71837dSTrent Jaeger 1856fb7deabaSSabrina Dubroca err = copy_from_user_policy_type(&xp->type, attrs, extack); 1857f7b6983fSMasahide NAKAMURA if (err) 1858f7b6983fSMasahide NAKAMURA goto error; 1859f7b6983fSMasahide NAKAMURA 1860d37bed89SSabrina Dubroca if (!(err = copy_from_user_tmpl(xp, attrs, extack))) 186135a7aa08SThomas Graf err = copy_from_user_sec_ctx(xp, attrs); 1862f7b6983fSMasahide NAKAMURA if (err) 1863f7b6983fSMasahide NAKAMURA goto error; 18641da177e4SLinus Torvalds 1865295fae56SJamal Hadi Salim xfrm_mark_get(attrs, &xp->mark); 1866295fae56SJamal Hadi Salim 1867a3d9001bSKai Lueke if (attrs[XFRMA_IF_ID]) 18687e652640SSteffen Klassert xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 18697e652640SSteffen Klassert 18701da177e4SLinus Torvalds return xp; 1871f7b6983fSMasahide NAKAMURA error: 1872f7b6983fSMasahide NAKAMURA *errp = err; 187312a169e7SHerbert Xu xp->walk.dead = 1; 187464c31b3fSWANG Cong xfrm_policy_destroy(xp); 1875f7b6983fSMasahide NAKAMURA return NULL; 18761da177e4SLinus Torvalds } 18771da177e4SLinus Torvalds 187822e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 18793bec6c3eSSabrina Dubroca struct nlattr **attrs, 18803bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 18811da177e4SLinus Torvalds { 1882fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 18837b67c857SThomas Graf struct xfrm_userpolicy_info *p = nlmsg_data(nlh); 18841da177e4SLinus Torvalds struct xfrm_policy *xp; 188526b15dadSJamal Hadi Salim struct km_event c; 18861da177e4SLinus Torvalds int err; 18871da177e4SLinus Torvalds int excl; 18881da177e4SLinus Torvalds 1889ec2b4f01SSabrina Dubroca err = verify_newpolicy_info(p, extack); 18901da177e4SLinus Torvalds if (err) 18911da177e4SLinus Torvalds return err; 189208a717e4SSabrina Dubroca err = verify_sec_ctx_len(attrs, extack); 1893df71837dSTrent Jaeger if (err) 1894df71837dSTrent Jaeger return err; 18951da177e4SLinus Torvalds 1896fb7deabaSSabrina Dubroca xp = xfrm_policy_construct(net, p, attrs, &err, extack); 18971da177e4SLinus Torvalds if (!xp) 18981da177e4SLinus Torvalds return err; 18991da177e4SLinus Torvalds 190025985edcSLucas De Marchi /* shouldn't excl be based on nlh flags?? 190126b15dadSJamal Hadi Salim * Aha! this is anti-netlink really i.e more pfkey derived 1902a7fd0e6dSBhaskar Chowdhury * in netlink excl is a flag and you wouldn't need 190326b15dadSJamal Hadi Salim * a type XFRM_MSG_UPDPOLICY - JHS */ 19041da177e4SLinus Torvalds excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; 19051da177e4SLinus Torvalds err = xfrm_policy_insert(p->dir, xp, excl); 19062e71029eSTetsuo Handa xfrm_audit_policy_add(xp, err ? 0 : 1, true); 1907161a09e7SJoy Latten 19081da177e4SLinus Torvalds if (err) { 190903e1ad7bSPaul Moore security_xfrm_policy_free(xp->security); 19101da177e4SLinus Torvalds kfree(xp); 19111da177e4SLinus Torvalds return err; 19121da177e4SLinus Torvalds } 19131da177e4SLinus Torvalds 1914f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 191526b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 191615e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 191726b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 191826b15dadSJamal Hadi Salim 19191da177e4SLinus Torvalds xfrm_pol_put(xp); 19201da177e4SLinus Torvalds 19211da177e4SLinus Torvalds return 0; 19221da177e4SLinus Torvalds } 19231da177e4SLinus Torvalds 19241da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) 19251da177e4SLinus Torvalds { 19261da177e4SLinus Torvalds struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; 19271da177e4SLinus Torvalds int i; 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds if (xp->xfrm_nr == 0) 19301da177e4SLinus Torvalds return 0; 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds for (i = 0; i < xp->xfrm_nr; i++) { 19331da177e4SLinus Torvalds struct xfrm_user_tmpl *up = &vec[i]; 19341da177e4SLinus Torvalds struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; 19351da177e4SLinus Torvalds 19361f86840fSMathias Krause memset(up, 0, sizeof(*up)); 19371da177e4SLinus Torvalds memcpy(&up->id, &kp->id, sizeof(up->id)); 19388511d01dSMiika Komu up->family = kp->encap_family; 19391da177e4SLinus Torvalds memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); 19401da177e4SLinus Torvalds up->reqid = kp->reqid; 19411da177e4SLinus Torvalds up->mode = kp->mode; 19421da177e4SLinus Torvalds up->share = kp->share; 19431da177e4SLinus Torvalds up->optional = kp->optional; 19441da177e4SLinus Torvalds up->aalgos = kp->aalgos; 19451da177e4SLinus Torvalds up->ealgos = kp->ealgos; 19461da177e4SLinus Torvalds up->calgos = kp->calgos; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 1949c0144beaSThomas Graf return nla_put(skb, XFRMA_TMPL, 1950c0144beaSThomas Graf sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); 1951df71837dSTrent Jaeger } 1952df71837dSTrent Jaeger 19530d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) 19540d681623SSerge Hallyn { 19550d681623SSerge Hallyn if (x->security) { 19560d681623SSerge Hallyn return copy_sec_ctx(x->security, skb); 19570d681623SSerge Hallyn } 19580d681623SSerge Hallyn return 0; 19590d681623SSerge Hallyn } 19600d681623SSerge Hallyn 19610d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) 19620d681623SSerge Hallyn { 19631d1e34ddSDavid S. Miller if (xp->security) 19640d681623SSerge Hallyn return copy_sec_ctx(xp->security, skb); 19650d681623SSerge Hallyn return 0; 19660d681623SSerge Hallyn } 1967a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void) 1968cfbfd45aSThomas Graf { 1969cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY 1970cfbfd45aSThomas Graf return nla_total_size(sizeof(struct xfrm_userpolicy_type)); 1971cfbfd45aSThomas Graf #else 1972cfbfd45aSThomas Graf return 0; 1973cfbfd45aSThomas Graf #endif 1974cfbfd45aSThomas Graf } 19750d681623SSerge Hallyn 1976f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 1977b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1978f7b6983fSMasahide NAKAMURA { 197945c180bcSEric Dumazet struct xfrm_userpolicy_type upt; 198045c180bcSEric Dumazet 198145c180bcSEric Dumazet /* Sadly there are two holes in struct xfrm_userpolicy_type */ 198245c180bcSEric Dumazet memset(&upt, 0, sizeof(upt)); 198345c180bcSEric Dumazet upt.type = type; 1984f7b6983fSMasahide NAKAMURA 1985c0144beaSThomas Graf return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); 1986f7b6983fSMasahide NAKAMURA } 1987f7b6983fSMasahide NAKAMURA 1988f7b6983fSMasahide NAKAMURA #else 1989b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1990f7b6983fSMasahide NAKAMURA { 1991f7b6983fSMasahide NAKAMURA return 0; 1992f7b6983fSMasahide NAKAMURA } 1993f7b6983fSMasahide NAKAMURA #endif 1994f7b6983fSMasahide NAKAMURA 19951da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) 19961da177e4SLinus Torvalds { 19971da177e4SLinus Torvalds struct xfrm_dump_info *sp = ptr; 19981da177e4SLinus Torvalds struct xfrm_userpolicy_info *p; 19991da177e4SLinus Torvalds struct sk_buff *in_skb = sp->in_skb; 20001da177e4SLinus Torvalds struct sk_buff *skb = sp->out_skb; 20015f3eea6bSDmitry Safonov struct xfrm_translator *xtr; 20021da177e4SLinus Torvalds struct nlmsghdr *nlh; 20031d1e34ddSDavid S. Miller int err; 20041da177e4SLinus Torvalds 200515e47304SEric W. Biederman nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq, 200679b8b7f4SThomas Graf XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); 200779b8b7f4SThomas Graf if (nlh == NULL) 200879b8b7f4SThomas Graf return -EMSGSIZE; 20091da177e4SLinus Torvalds 20107b67c857SThomas Graf p = nlmsg_data(nlh); 20111da177e4SLinus Torvalds copy_to_user_policy(xp, p, dir); 20121d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 20131d1e34ddSDavid S. Miller if (!err) 20141d1e34ddSDavid S. Miller err = copy_to_user_sec_ctx(xp, skb); 20151d1e34ddSDavid S. Miller if (!err) 20161d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 20171d1e34ddSDavid S. Miller if (!err) 20181d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 20197e652640SSteffen Klassert if (!err) 20207e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 20211d1e34ddSDavid S. Miller if (err) { 20221d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 20231d1e34ddSDavid S. Miller return err; 20241d1e34ddSDavid S. Miller } 20259825069dSThomas Graf nlmsg_end(skb, nlh); 20265f3eea6bSDmitry Safonov 20275f3eea6bSDmitry Safonov xtr = xfrm_get_translator(); 20285f3eea6bSDmitry Safonov if (xtr) { 20295f3eea6bSDmitry Safonov err = xtr->alloc_compat(skb, nlh); 20305f3eea6bSDmitry Safonov 20315f3eea6bSDmitry Safonov xfrm_put_translator(xtr); 20325f3eea6bSDmitry Safonov if (err) { 20335f3eea6bSDmitry Safonov nlmsg_cancel(skb, nlh); 20345f3eea6bSDmitry Safonov return err; 20355f3eea6bSDmitry Safonov } 20365f3eea6bSDmitry Safonov } 20375f3eea6bSDmitry Safonov 20381da177e4SLinus Torvalds return 0; 20391da177e4SLinus Torvalds } 20401da177e4SLinus Torvalds 20414c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb) 20424c563f76STimo Teras { 20431137b5e2SHerbert Xu struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 2044283bc9f3SFan Du struct net *net = sock_net(cb->skb->sk); 20454c563f76STimo Teras 2046283bc9f3SFan Du xfrm_policy_walk_done(walk, net); 20474c563f76STimo Teras return 0; 20484c563f76STimo Teras } 20494c563f76STimo Teras 20501137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb) 20511137b5e2SHerbert Xu { 20521137b5e2SHerbert Xu struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 20531137b5e2SHerbert Xu 20541137b5e2SHerbert Xu BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args)); 20551137b5e2SHerbert Xu 20561137b5e2SHerbert Xu xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); 20571137b5e2SHerbert Xu return 0; 20581137b5e2SHerbert Xu } 20591137b5e2SHerbert Xu 20601da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) 20611da177e4SLinus Torvalds { 2062fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 20631137b5e2SHerbert Xu struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 20641da177e4SLinus Torvalds struct xfrm_dump_info info; 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds info.in_skb = cb->skb; 20671da177e4SLinus Torvalds info.out_skb = skb; 20681da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 20691da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 20704c563f76STimo Teras 2071fc34acd3SAlexey Dobriyan (void) xfrm_policy_walk(net, walk, dump_one_policy, &info); 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds return skb->len; 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds 20761da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, 20771da177e4SLinus Torvalds struct xfrm_policy *xp, 20781da177e4SLinus Torvalds int dir, u32 seq) 20791da177e4SLinus Torvalds { 20801da177e4SLinus Torvalds struct xfrm_dump_info info; 20811da177e4SLinus Torvalds struct sk_buff *skb; 2082c2546372SMathias Krause int err; 20831da177e4SLinus Torvalds 20847deb2264SThomas Graf skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 20851da177e4SLinus Torvalds if (!skb) 20861da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 20871da177e4SLinus Torvalds 20881da177e4SLinus Torvalds info.in_skb = in_skb; 20891da177e4SLinus Torvalds info.out_skb = skb; 20901da177e4SLinus Torvalds info.nlmsg_seq = seq; 20911da177e4SLinus Torvalds info.nlmsg_flags = 0; 20921da177e4SLinus Torvalds 2093c2546372SMathias Krause err = dump_one_policy(xp, dir, 0, &info); 2094c2546372SMathias Krause if (err) { 20951da177e4SLinus Torvalds kfree_skb(skb); 2096c2546372SMathias Krause return ERR_PTR(err); 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds return skb; 21001da177e4SLinus Torvalds } 21011da177e4SLinus Torvalds 210288d0adb5SNicolas Dichtel static int xfrm_notify_userpolicy(struct net *net) 210388d0adb5SNicolas Dichtel { 210488d0adb5SNicolas Dichtel struct xfrm_userpolicy_default *up; 210588d0adb5SNicolas Dichtel int len = NLMSG_ALIGN(sizeof(*up)); 210688d0adb5SNicolas Dichtel struct nlmsghdr *nlh; 210788d0adb5SNicolas Dichtel struct sk_buff *skb; 210893ec1320SNicolas Dichtel int err; 210988d0adb5SNicolas Dichtel 211088d0adb5SNicolas Dichtel skb = nlmsg_new(len, GFP_ATOMIC); 211188d0adb5SNicolas Dichtel if (skb == NULL) 211288d0adb5SNicolas Dichtel return -ENOMEM; 211388d0adb5SNicolas Dichtel 211488d0adb5SNicolas Dichtel nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0); 211588d0adb5SNicolas Dichtel if (nlh == NULL) { 211688d0adb5SNicolas Dichtel kfree_skb(skb); 211788d0adb5SNicolas Dichtel return -EMSGSIZE; 211888d0adb5SNicolas Dichtel } 211988d0adb5SNicolas Dichtel 212088d0adb5SNicolas Dichtel up = nlmsg_data(nlh); 2121b58b1f56SNicolas Dichtel up->in = net->xfrm.policy_default[XFRM_POLICY_IN]; 2122b58b1f56SNicolas Dichtel up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD]; 2123b58b1f56SNicolas Dichtel up->out = net->xfrm.policy_default[XFRM_POLICY_OUT]; 212488d0adb5SNicolas Dichtel 212588d0adb5SNicolas Dichtel nlmsg_end(skb, nlh); 212688d0adb5SNicolas Dichtel 212793ec1320SNicolas Dichtel rcu_read_lock(); 212893ec1320SNicolas Dichtel err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 212993ec1320SNicolas Dichtel rcu_read_unlock(); 213093ec1320SNicolas Dichtel 213193ec1320SNicolas Dichtel return err; 213288d0adb5SNicolas Dichtel } 213388d0adb5SNicolas Dichtel 2134b58b1f56SNicolas Dichtel static bool xfrm_userpolicy_is_valid(__u8 policy) 2135b58b1f56SNicolas Dichtel { 2136b58b1f56SNicolas Dichtel return policy == XFRM_USERPOLICY_BLOCK || 2137b58b1f56SNicolas Dichtel policy == XFRM_USERPOLICY_ACCEPT; 2138b58b1f56SNicolas Dichtel } 2139b58b1f56SNicolas Dichtel 21402d151d39SSteffen Klassert static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh, 21413bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 21422d151d39SSteffen Klassert { 21432d151d39SSteffen Klassert struct net *net = sock_net(skb->sk); 21442d151d39SSteffen Klassert struct xfrm_userpolicy_default *up = nlmsg_data(nlh); 21452d151d39SSteffen Klassert 2146b58b1f56SNicolas Dichtel if (xfrm_userpolicy_is_valid(up->in)) 2147b58b1f56SNicolas Dichtel net->xfrm.policy_default[XFRM_POLICY_IN] = up->in; 21485d8dbb7fSPavel Skripkin 2149b58b1f56SNicolas Dichtel if (xfrm_userpolicy_is_valid(up->fwd)) 2150b58b1f56SNicolas Dichtel net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd; 21515d8dbb7fSPavel Skripkin 2152b58b1f56SNicolas Dichtel if (xfrm_userpolicy_is_valid(up->out)) 2153b58b1f56SNicolas Dichtel net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out; 21542d151d39SSteffen Klassert 21552d151d39SSteffen Klassert rt_genid_bump_all(net); 21562d151d39SSteffen Klassert 215788d0adb5SNicolas Dichtel xfrm_notify_userpolicy(net); 21582d151d39SSteffen Klassert return 0; 21592d151d39SSteffen Klassert } 21602d151d39SSteffen Klassert 21612d151d39SSteffen Klassert static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh, 21623bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 21632d151d39SSteffen Klassert { 21642d151d39SSteffen Klassert struct sk_buff *r_skb; 21652d151d39SSteffen Klassert struct nlmsghdr *r_nlh; 21662d151d39SSteffen Klassert struct net *net = sock_net(skb->sk); 2167f8d858e6SNicolas Dichtel struct xfrm_userpolicy_default *r_up; 21682d151d39SSteffen Klassert int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default)); 21692d151d39SSteffen Klassert u32 portid = NETLINK_CB(skb).portid; 21702d151d39SSteffen Klassert u32 seq = nlh->nlmsg_seq; 21712d151d39SSteffen Klassert 21722d151d39SSteffen Klassert r_skb = nlmsg_new(len, GFP_ATOMIC); 21732d151d39SSteffen Klassert if (!r_skb) 21742d151d39SSteffen Klassert return -ENOMEM; 21752d151d39SSteffen Klassert 21762d151d39SSteffen Klassert r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0); 21772d151d39SSteffen Klassert if (!r_nlh) { 21782d151d39SSteffen Klassert kfree_skb(r_skb); 21792d151d39SSteffen Klassert return -EMSGSIZE; 21802d151d39SSteffen Klassert } 21812d151d39SSteffen Klassert 21822d151d39SSteffen Klassert r_up = nlmsg_data(r_nlh); 2183b58b1f56SNicolas Dichtel r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN]; 2184b58b1f56SNicolas Dichtel r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD]; 2185b58b1f56SNicolas Dichtel r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT]; 21862d151d39SSteffen Klassert nlmsg_end(r_skb, r_nlh); 21872d151d39SSteffen Klassert 21882d151d39SSteffen Klassert return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid); 21892d151d39SSteffen Klassert } 21902d151d39SSteffen Klassert 219122e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 21923bec6c3eSSabrina Dubroca struct nlattr **attrs, 21933bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 21941da177e4SLinus Torvalds { 2195fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 21961da177e4SLinus Torvalds struct xfrm_policy *xp; 21971da177e4SLinus Torvalds struct xfrm_userpolicy_id *p; 2198b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 21991da177e4SLinus Torvalds int err; 220026b15dadSJamal Hadi Salim struct km_event c; 22011da177e4SLinus Torvalds int delete; 2202295fae56SJamal Hadi Salim struct xfrm_mark m; 22037e652640SSteffen Klassert u32 if_id = 0; 22041da177e4SLinus Torvalds 22057b67c857SThomas Graf p = nlmsg_data(nlh); 22061da177e4SLinus Torvalds delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; 22071da177e4SLinus Torvalds 2208fb7deabaSSabrina Dubroca err = copy_from_user_policy_type(&type, attrs, extack); 2209f7b6983fSMasahide NAKAMURA if (err) 2210f7b6983fSMasahide NAKAMURA return err; 2211f7b6983fSMasahide NAKAMURA 221224fc544fSSabrina Dubroca err = verify_policy_dir(p->dir, extack); 22131da177e4SLinus Torvalds if (err) 22141da177e4SLinus Torvalds return err; 22151da177e4SLinus Torvalds 22167e652640SSteffen Klassert if (attrs[XFRMA_IF_ID]) 22177e652640SSteffen Klassert if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 22187e652640SSteffen Klassert 22194f47e8abSXin Long xfrm_mark_get(attrs, &m); 22204f47e8abSXin Long 22211da177e4SLinus Torvalds if (p->index) 22224f47e8abSXin Long xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, 22234f47e8abSXin Long p->index, delete, &err); 2224df71837dSTrent Jaeger else { 22255424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 222603e1ad7bSPaul Moore struct xfrm_sec_ctx *ctx; 2227df71837dSTrent Jaeger 222808a717e4SSabrina Dubroca err = verify_sec_ctx_len(attrs, extack); 2229df71837dSTrent Jaeger if (err) 2230df71837dSTrent Jaeger return err; 2231df71837dSTrent Jaeger 22322c8dd116SDenis V. Lunev ctx = NULL; 2233df71837dSTrent Jaeger if (rt) { 22345424f32eSThomas Graf struct xfrm_user_sec_ctx *uctx = nla_data(rt); 2235df71837dSTrent Jaeger 223652a4c640SNikolay Aleksandrov err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 223703e1ad7bSPaul Moore if (err) 2238df71837dSTrent Jaeger return err; 22392c8dd116SDenis V. Lunev } 22404f47e8abSXin Long xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir, 22414f47e8abSXin Long &p->sel, ctx, delete, &err); 224203e1ad7bSPaul Moore security_xfrm_policy_free(ctx); 2243df71837dSTrent Jaeger } 22441da177e4SLinus Torvalds if (xp == NULL) 22451da177e4SLinus Torvalds return -ENOENT; 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds if (!delete) { 22481da177e4SLinus Torvalds struct sk_buff *resp_skb; 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); 22511da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 22521da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 22531da177e4SLinus Torvalds } else { 2254a6483b79SAlexey Dobriyan err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, 225515e47304SEric W. Biederman NETLINK_CB(skb).portid); 22561da177e4SLinus Torvalds } 225726b15dadSJamal Hadi Salim } else { 22582e71029eSTetsuo Handa xfrm_audit_policy_delete(xp, err ? 0 : 1, true); 225913fcfbb0SDavid S. Miller 226013fcfbb0SDavid S. Miller if (err != 0) 2261c8c05a8eSCatherine Zhang goto out; 226213fcfbb0SDavid S. Miller 2263e7443892SHerbert Xu c.data.byid = p->index; 2264f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 226526b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 226615e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 226726b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 2270c8c05a8eSCatherine Zhang out: 2271ef41aaa0SEric Paris xfrm_pol_put(xp); 22721da177e4SLinus Torvalds return err; 22731da177e4SLinus Torvalds } 22741da177e4SLinus Torvalds 227522e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 22763bec6c3eSSabrina Dubroca struct nlattr **attrs, 22773bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 22781da177e4SLinus Torvalds { 2279fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 228026b15dadSJamal Hadi Salim struct km_event c; 22817b67c857SThomas Graf struct xfrm_usersa_flush *p = nlmsg_data(nlh); 22824aa2e62cSJoy Latten int err; 22831da177e4SLinus Torvalds 2284f75a2804SCong Wang err = xfrm_state_flush(net, p->proto, true, false); 22859e64cc95SJamal Hadi Salim if (err) { 22869e64cc95SJamal Hadi Salim if (err == -ESRCH) /* empty table */ 22879e64cc95SJamal Hadi Salim return 0; 2288069c474eSDavid S. Miller return err; 22899e64cc95SJamal Hadi Salim } 2290bf08867fSHerbert Xu c.data.proto = p->proto; 2291f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 229226b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 229315e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 22947067802eSAlexey Dobriyan c.net = net; 229526b15dadSJamal Hadi Salim km_state_notify(NULL, &c); 229626b15dadSJamal Hadi Salim 22971da177e4SLinus Torvalds return 0; 22981da177e4SLinus Torvalds } 22991da177e4SLinus Torvalds 2300a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x) 23017deb2264SThomas Graf { 2302a1b831f2SAlexey Dobriyan unsigned int replay_size = x->replay_esn ? 2303d8647b79SSteffen Klassert xfrm_replay_state_esn_len(x->replay_esn) : 2304d8647b79SSteffen Klassert sizeof(struct xfrm_replay_state); 2305d8647b79SSteffen Klassert 23067deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) 2307d8647b79SSteffen Klassert + nla_total_size(replay_size) 2308de95c4a4SNicolas Dichtel + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur)) 23096f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 23107deb2264SThomas Graf + nla_total_size(4) /* XFRM_AE_RTHR */ 23117deb2264SThomas Graf + nla_total_size(4); /* XFRM_AE_ETHR */ 23127deb2264SThomas Graf } 2313d51d081dSJamal Hadi Salim 2314214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 2315d51d081dSJamal Hadi Salim { 2316d51d081dSJamal Hadi Salim struct xfrm_aevent_id *id; 2317d51d081dSJamal Hadi Salim struct nlmsghdr *nlh; 23181d1e34ddSDavid S. Miller int err; 2319d51d081dSJamal Hadi Salim 232015e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); 232179b8b7f4SThomas Graf if (nlh == NULL) 232279b8b7f4SThomas Graf return -EMSGSIZE; 2323d51d081dSJamal Hadi Salim 23247b67c857SThomas Graf id = nlmsg_data(nlh); 2325931e79d7SMathias Krause memset(&id->sa_id, 0, sizeof(id->sa_id)); 23262b5f6dccSJamal Hadi Salim memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr)); 2327d51d081dSJamal Hadi Salim id->sa_id.spi = x->id.spi; 2328d51d081dSJamal Hadi Salim id->sa_id.family = x->props.family; 2329d51d081dSJamal Hadi Salim id->sa_id.proto = x->id.proto; 23302b5f6dccSJamal Hadi Salim memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr)); 23312b5f6dccSJamal Hadi Salim id->reqid = x->props.reqid; 2332d51d081dSJamal Hadi Salim id->flags = c->data.aevent; 2333d51d081dSJamal Hadi Salim 2334d0fde795SDavid S. Miller if (x->replay_esn) { 23351d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 2336d8647b79SSteffen Klassert xfrm_replay_state_esn_len(x->replay_esn), 23371d1e34ddSDavid S. Miller x->replay_esn); 2338d0fde795SDavid S. Miller } else { 23391d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 23401d1e34ddSDavid S. Miller &x->replay); 2341d0fde795SDavid S. Miller } 23421d1e34ddSDavid S. Miller if (err) 23431d1e34ddSDavid S. Miller goto out_cancel; 2344de95c4a4SNicolas Dichtel err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft, 2345de95c4a4SNicolas Dichtel XFRMA_PAD); 23461d1e34ddSDavid S. Miller if (err) 23471d1e34ddSDavid S. Miller goto out_cancel; 2348d8647b79SSteffen Klassert 23491d1e34ddSDavid S. Miller if (id->flags & XFRM_AE_RTHR) { 23501d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); 23511d1e34ddSDavid S. Miller if (err) 23521d1e34ddSDavid S. Miller goto out_cancel; 23531d1e34ddSDavid S. Miller } 23541d1e34ddSDavid S. Miller if (id->flags & XFRM_AE_ETHR) { 23551d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_ETIMER_THRESH, 23561d1e34ddSDavid S. Miller x->replay_maxage * 10 / HZ); 23571d1e34ddSDavid S. Miller if (err) 23581d1e34ddSDavid S. Miller goto out_cancel; 23591d1e34ddSDavid S. Miller } 23601d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &x->mark); 23611d1e34ddSDavid S. Miller if (err) 23621d1e34ddSDavid S. Miller goto out_cancel; 23636f26b61eSJamal Hadi Salim 23647e652640SSteffen Klassert err = xfrm_if_id_put(skb, x->if_id); 23657e652640SSteffen Klassert if (err) 23667e652640SSteffen Klassert goto out_cancel; 23677e652640SSteffen Klassert 2368053c095aSJohannes Berg nlmsg_end(skb, nlh); 2369053c095aSJohannes Berg return 0; 2370d51d081dSJamal Hadi Salim 23711d1e34ddSDavid S. Miller out_cancel: 23729825069dSThomas Graf nlmsg_cancel(skb, nlh); 23731d1e34ddSDavid S. Miller return err; 2374d51d081dSJamal Hadi Salim } 2375d51d081dSJamal Hadi Salim 237622e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 23773bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 2378d51d081dSJamal Hadi Salim { 2379fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 2380d51d081dSJamal Hadi Salim struct xfrm_state *x; 2381d51d081dSJamal Hadi Salim struct sk_buff *r_skb; 2382d51d081dSJamal Hadi Salim int err; 2383d51d081dSJamal Hadi Salim struct km_event c; 23846f26b61eSJamal Hadi Salim u32 mark; 23856f26b61eSJamal Hadi Salim struct xfrm_mark m; 23867b67c857SThomas Graf struct xfrm_aevent_id *p = nlmsg_data(nlh); 2387d51d081dSJamal Hadi Salim struct xfrm_usersa_id *id = &p->sa_id; 2388d51d081dSJamal Hadi Salim 23896f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 23906f26b61eSJamal Hadi Salim 23916f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family); 2392d8647b79SSteffen Klassert if (x == NULL) 2393d51d081dSJamal Hadi Salim return -ESRCH; 2394d8647b79SSteffen Klassert 2395d8647b79SSteffen Klassert r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 2396d8647b79SSteffen Klassert if (r_skb == NULL) { 2397d8647b79SSteffen Klassert xfrm_state_put(x); 2398d8647b79SSteffen Klassert return -ENOMEM; 2399d51d081dSJamal Hadi Salim } 2400d51d081dSJamal Hadi Salim 2401d51d081dSJamal Hadi Salim /* 2402d51d081dSJamal Hadi Salim * XXX: is this lock really needed - none of the other 2403d51d081dSJamal Hadi Salim * gets lock (the concern is things getting updated 2404d51d081dSJamal Hadi Salim * while we are still reading) - jhs 2405d51d081dSJamal Hadi Salim */ 2406d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 2407d51d081dSJamal Hadi Salim c.data.aevent = p->flags; 2408d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 240915e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 2410d51d081dSJamal Hadi Salim 24112fc5f83bSGustavo A. R. Silva err = build_aevent(r_skb, x, &c); 24122fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 24132fc5f83bSGustavo A. R. Silva 241415e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid); 2415d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 2416d51d081dSJamal Hadi Salim xfrm_state_put(x); 2417d51d081dSJamal Hadi Salim return err; 2418d51d081dSJamal Hadi Salim } 2419d51d081dSJamal Hadi Salim 242022e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 24213bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 2422d51d081dSJamal Hadi Salim { 2423fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 2424d51d081dSJamal Hadi Salim struct xfrm_state *x; 2425d51d081dSJamal Hadi Salim struct km_event c; 2426d51d081dSJamal Hadi Salim int err = -EINVAL; 24276f26b61eSJamal Hadi Salim u32 mark = 0; 24286f26b61eSJamal Hadi Salim struct xfrm_mark m; 24297b67c857SThomas Graf struct xfrm_aevent_id *p = nlmsg_data(nlh); 24305424f32eSThomas Graf struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 2431d8647b79SSteffen Klassert struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL]; 24325424f32eSThomas Graf struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 24334e077237SMichael Rossberg struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; 24344e077237SMichael Rossberg struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; 2435d51d081dSJamal Hadi Salim 24364e077237SMichael Rossberg if (!lt && !rp && !re && !et && !rt) 2437d51d081dSJamal Hadi Salim return err; 2438d51d081dSJamal Hadi Salim 2439d51d081dSJamal Hadi Salim /* pedantic mode - thou shalt sayeth replaceth */ 2440d51d081dSJamal Hadi Salim if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) 2441d51d081dSJamal Hadi Salim return err; 2442d51d081dSJamal Hadi Salim 24436f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 24446f26b61eSJamal Hadi Salim 24456f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); 2446d51d081dSJamal Hadi Salim if (x == NULL) 2447d51d081dSJamal Hadi Salim return -ESRCH; 2448d51d081dSJamal Hadi Salim 2449d51d081dSJamal Hadi Salim if (x->km.state != XFRM_STATE_VALID) 2450d51d081dSJamal Hadi Salim goto out; 2451d51d081dSJamal Hadi Salim 24524479ff76SSteffen Klassert err = xfrm_replay_verify_len(x->replay_esn, re); 2453e2b19125SSteffen Klassert if (err) 2454e2b19125SSteffen Klassert goto out; 2455e2b19125SSteffen Klassert 2456d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 2457e3ac104dSMathias Krause xfrm_update_ae_params(x, attrs, 1); 2458d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 2459d51d081dSJamal Hadi Salim 2460d51d081dSJamal Hadi Salim c.event = nlh->nlmsg_type; 2461d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 246215e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 2463d51d081dSJamal Hadi Salim c.data.aevent = XFRM_AE_CU; 2464d51d081dSJamal Hadi Salim km_state_notify(x, &c); 2465d51d081dSJamal Hadi Salim err = 0; 2466d51d081dSJamal Hadi Salim out: 2467d51d081dSJamal Hadi Salim xfrm_state_put(x); 2468d51d081dSJamal Hadi Salim return err; 2469d51d081dSJamal Hadi Salim } 2470d51d081dSJamal Hadi Salim 247122e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 24723bec6c3eSSabrina Dubroca struct nlattr **attrs, 24733bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 24741da177e4SLinus Torvalds { 2475fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 247626b15dadSJamal Hadi Salim struct km_event c; 2477b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 2478f7b6983fSMasahide NAKAMURA int err; 247926b15dadSJamal Hadi Salim 2480fb7deabaSSabrina Dubroca err = copy_from_user_policy_type(&type, attrs, extack); 2481f7b6983fSMasahide NAKAMURA if (err) 2482f7b6983fSMasahide NAKAMURA return err; 2483f7b6983fSMasahide NAKAMURA 24842e71029eSTetsuo Handa err = xfrm_policy_flush(net, type, true); 24852f1eb65fSJamal Hadi Salim if (err) { 24862f1eb65fSJamal Hadi Salim if (err == -ESRCH) /* empty table */ 24872f1eb65fSJamal Hadi Salim return 0; 2488069c474eSDavid S. Miller return err; 24892f1eb65fSJamal Hadi Salim } 24902f1eb65fSJamal Hadi Salim 2491f7b6983fSMasahide NAKAMURA c.data.type = type; 2492f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 249326b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 249415e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 24957067802eSAlexey Dobriyan c.net = net; 249626b15dadSJamal Hadi Salim km_policy_notify(NULL, 0, &c); 24971da177e4SLinus Torvalds return 0; 24981da177e4SLinus Torvalds } 24991da177e4SLinus Torvalds 250022e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 25013bec6c3eSSabrina Dubroca struct nlattr **attrs, 25023bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 25036c5c8ca7SJamal Hadi Salim { 2504fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 25056c5c8ca7SJamal Hadi Salim struct xfrm_policy *xp; 25067b67c857SThomas Graf struct xfrm_user_polexpire *up = nlmsg_data(nlh); 25076c5c8ca7SJamal Hadi Salim struct xfrm_userpolicy_info *p = &up->pol; 2508b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 25096c5c8ca7SJamal Hadi Salim int err = -ENOENT; 2510295fae56SJamal Hadi Salim struct xfrm_mark m; 25117e652640SSteffen Klassert u32 if_id = 0; 25126c5c8ca7SJamal Hadi Salim 2513fb7deabaSSabrina Dubroca err = copy_from_user_policy_type(&type, attrs, extack); 2514f7b6983fSMasahide NAKAMURA if (err) 2515f7b6983fSMasahide NAKAMURA return err; 2516f7b6983fSMasahide NAKAMURA 251724fc544fSSabrina Dubroca err = verify_policy_dir(p->dir, extack); 2518c8bf4d04STimo Teräs if (err) 2519c8bf4d04STimo Teräs return err; 2520c8bf4d04STimo Teräs 25217e652640SSteffen Klassert if (attrs[XFRMA_IF_ID]) 25227e652640SSteffen Klassert if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 25237e652640SSteffen Klassert 25244f47e8abSXin Long xfrm_mark_get(attrs, &m); 25254f47e8abSXin Long 25266c5c8ca7SJamal Hadi Salim if (p->index) 25274f47e8abSXin Long xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index, 25284f47e8abSXin Long 0, &err); 25296c5c8ca7SJamal Hadi Salim else { 25305424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 253103e1ad7bSPaul Moore struct xfrm_sec_ctx *ctx; 25326c5c8ca7SJamal Hadi Salim 253308a717e4SSabrina Dubroca err = verify_sec_ctx_len(attrs, extack); 25346c5c8ca7SJamal Hadi Salim if (err) 25356c5c8ca7SJamal Hadi Salim return err; 25366c5c8ca7SJamal Hadi Salim 25372c8dd116SDenis V. Lunev ctx = NULL; 25386c5c8ca7SJamal Hadi Salim if (rt) { 25395424f32eSThomas Graf struct xfrm_user_sec_ctx *uctx = nla_data(rt); 25406c5c8ca7SJamal Hadi Salim 254152a4c640SNikolay Aleksandrov err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 254203e1ad7bSPaul Moore if (err) 25436c5c8ca7SJamal Hadi Salim return err; 25442c8dd116SDenis V. Lunev } 25454f47e8abSXin Long xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir, 25466f26b61eSJamal Hadi Salim &p->sel, ctx, 0, &err); 254703e1ad7bSPaul Moore security_xfrm_policy_free(ctx); 25486c5c8ca7SJamal Hadi Salim } 25496c5c8ca7SJamal Hadi Salim if (xp == NULL) 2550ef41aaa0SEric Paris return -ENOENT; 255103e1ad7bSPaul Moore 2552ea2dea9dSTimo Teräs if (unlikely(xp->walk.dead)) 25536c5c8ca7SJamal Hadi Salim goto out; 25546c5c8ca7SJamal Hadi Salim 25556c5c8ca7SJamal Hadi Salim err = 0; 25566c5c8ca7SJamal Hadi Salim if (up->hard) { 25576c5c8ca7SJamal Hadi Salim xfrm_policy_delete(xp, p->dir); 25582e71029eSTetsuo Handa xfrm_audit_policy_delete(xp, 1, true); 25596c5c8ca7SJamal Hadi Salim } 2560c6bb8136SEric W. Biederman km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid); 25616c5c8ca7SJamal Hadi Salim 25626c5c8ca7SJamal Hadi Salim out: 25636c5c8ca7SJamal Hadi Salim xfrm_pol_put(xp); 25646c5c8ca7SJamal Hadi Salim return err; 25656c5c8ca7SJamal Hadi Salim } 25666c5c8ca7SJamal Hadi Salim 256722e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 25683bec6c3eSSabrina Dubroca struct nlattr **attrs, 25693bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 257053bc6b4dSJamal Hadi Salim { 2571fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 257253bc6b4dSJamal Hadi Salim struct xfrm_state *x; 257353bc6b4dSJamal Hadi Salim int err; 25747b67c857SThomas Graf struct xfrm_user_expire *ue = nlmsg_data(nlh); 257553bc6b4dSJamal Hadi Salim struct xfrm_usersa_info *p = &ue->state; 25766f26b61eSJamal Hadi Salim struct xfrm_mark m; 2577928497f0SNicolas Dichtel u32 mark = xfrm_mark_get(attrs, &m); 257853bc6b4dSJamal Hadi Salim 25796f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family); 258053bc6b4dSJamal Hadi Salim 25813a765aa5SDavid S. Miller err = -ENOENT; 258253bc6b4dSJamal Hadi Salim if (x == NULL) 258353bc6b4dSJamal Hadi Salim return err; 258453bc6b4dSJamal Hadi Salim 258553bc6b4dSJamal Hadi Salim spin_lock_bh(&x->lock); 25863a765aa5SDavid S. Miller err = -EINVAL; 2587*a25b19f3SSabrina Dubroca if (x->km.state != XFRM_STATE_VALID) { 2588*a25b19f3SSabrina Dubroca NL_SET_ERR_MSG(extack, "SA must be in VALID state"); 258953bc6b4dSJamal Hadi Salim goto out; 2590*a25b19f3SSabrina Dubroca } 2591*a25b19f3SSabrina Dubroca 2592c6bb8136SEric W. Biederman km_state_expired(x, ue->hard, nlh->nlmsg_pid); 259353bc6b4dSJamal Hadi Salim 2594161a09e7SJoy Latten if (ue->hard) { 259553bc6b4dSJamal Hadi Salim __xfrm_state_delete(x); 25962e71029eSTetsuo Handa xfrm_audit_state_delete(x, 1, true); 2597161a09e7SJoy Latten } 25983a765aa5SDavid S. Miller err = 0; 259953bc6b4dSJamal Hadi Salim out: 260053bc6b4dSJamal Hadi Salim spin_unlock_bh(&x->lock); 260153bc6b4dSJamal Hadi Salim xfrm_state_put(x); 260253bc6b4dSJamal Hadi Salim return err; 260353bc6b4dSJamal Hadi Salim } 260453bc6b4dSJamal Hadi Salim 260522e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, 26063bec6c3eSSabrina Dubroca struct nlattr **attrs, 26073bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 2608980ebd25SJamal Hadi Salim { 2609fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 2610980ebd25SJamal Hadi Salim struct xfrm_policy *xp; 2611980ebd25SJamal Hadi Salim struct xfrm_user_tmpl *ut; 2612980ebd25SJamal Hadi Salim int i; 26135424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_TMPL]; 26146f26b61eSJamal Hadi Salim struct xfrm_mark mark; 2615980ebd25SJamal Hadi Salim 26167b67c857SThomas Graf struct xfrm_user_acquire *ua = nlmsg_data(nlh); 2617fc34acd3SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 2618980ebd25SJamal Hadi Salim int err = -ENOMEM; 2619980ebd25SJamal Hadi Salim 2620980ebd25SJamal Hadi Salim if (!x) 2621d8eb9307SIlpo Järvinen goto nomem; 2622980ebd25SJamal Hadi Salim 26236f26b61eSJamal Hadi Salim xfrm_mark_get(attrs, &mark); 26246f26b61eSJamal Hadi Salim 2625ec2b4f01SSabrina Dubroca err = verify_newpolicy_info(&ua->policy, extack); 2626d8eb9307SIlpo Järvinen if (err) 262773efc324SVegard Nossum goto free_state; 262808a717e4SSabrina Dubroca err = verify_sec_ctx_len(attrs, extack); 2629a1a7e3a3SXin Long if (err) 2630a1a7e3a3SXin Long goto free_state; 2631980ebd25SJamal Hadi Salim 2632980ebd25SJamal Hadi Salim /* build an XP */ 2633fb7deabaSSabrina Dubroca xp = xfrm_policy_construct(net, &ua->policy, attrs, &err, extack); 2634d8eb9307SIlpo Järvinen if (!xp) 2635d8eb9307SIlpo Järvinen goto free_state; 2636980ebd25SJamal Hadi Salim 2637980ebd25SJamal Hadi Salim memcpy(&x->id, &ua->id, sizeof(ua->id)); 2638980ebd25SJamal Hadi Salim memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); 2639980ebd25SJamal Hadi Salim memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); 26406f26b61eSJamal Hadi Salim xp->mark.m = x->mark.m = mark.m; 26416f26b61eSJamal Hadi Salim xp->mark.v = x->mark.v = mark.v; 26425424f32eSThomas Graf ut = nla_data(rt); 2643980ebd25SJamal Hadi Salim /* extract the templates and for each call km_key */ 2644980ebd25SJamal Hadi Salim for (i = 0; i < xp->xfrm_nr; i++, ut++) { 2645980ebd25SJamal Hadi Salim struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 2646980ebd25SJamal Hadi Salim memcpy(&x->id, &t->id, sizeof(x->id)); 2647980ebd25SJamal Hadi Salim x->props.mode = t->mode; 2648980ebd25SJamal Hadi Salim x->props.reqid = t->reqid; 2649980ebd25SJamal Hadi Salim x->props.family = ut->family; 2650980ebd25SJamal Hadi Salim t->aalgos = ua->aalgos; 2651980ebd25SJamal Hadi Salim t->ealgos = ua->ealgos; 2652980ebd25SJamal Hadi Salim t->calgos = ua->calgos; 2653980ebd25SJamal Hadi Salim err = km_query(x, t, xp); 2654980ebd25SJamal Hadi Salim 2655980ebd25SJamal Hadi Salim } 2656980ebd25SJamal Hadi Salim 26574a135e53SMathias Krause xfrm_state_free(x); 2658980ebd25SJamal Hadi Salim kfree(xp); 2659980ebd25SJamal Hadi Salim 2660980ebd25SJamal Hadi Salim return 0; 2661d8eb9307SIlpo Järvinen 2662d8eb9307SIlpo Järvinen free_state: 26634a135e53SMathias Krause xfrm_state_free(x); 2664d8eb9307SIlpo Järvinen nomem: 2665d8eb9307SIlpo Järvinen return err; 2666980ebd25SJamal Hadi Salim } 2667980ebd25SJamal Hadi Salim 26685c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 26695c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma, 267013c1d189SArnaud Ebalard struct xfrm_kmaddress *k, 26715424f32eSThomas Graf struct nlattr **attrs, int *num) 26725c79de6eSShinta Sugimoto { 26735424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_MIGRATE]; 26745c79de6eSShinta Sugimoto struct xfrm_user_migrate *um; 26755c79de6eSShinta Sugimoto int i, num_migrate; 26765c79de6eSShinta Sugimoto 267713c1d189SArnaud Ebalard if (k != NULL) { 267813c1d189SArnaud Ebalard struct xfrm_user_kmaddress *uk; 267913c1d189SArnaud Ebalard 268013c1d189SArnaud Ebalard uk = nla_data(attrs[XFRMA_KMADDRESS]); 268113c1d189SArnaud Ebalard memcpy(&k->local, &uk->local, sizeof(k->local)); 268213c1d189SArnaud Ebalard memcpy(&k->remote, &uk->remote, sizeof(k->remote)); 268313c1d189SArnaud Ebalard k->family = uk->family; 268413c1d189SArnaud Ebalard k->reserved = uk->reserved; 268513c1d189SArnaud Ebalard } 268613c1d189SArnaud Ebalard 26875424f32eSThomas Graf um = nla_data(rt); 26885424f32eSThomas Graf num_migrate = nla_len(rt) / sizeof(*um); 26895c79de6eSShinta Sugimoto 26905c79de6eSShinta Sugimoto if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) 26915c79de6eSShinta Sugimoto return -EINVAL; 26925c79de6eSShinta Sugimoto 26935c79de6eSShinta Sugimoto for (i = 0; i < num_migrate; i++, um++, ma++) { 26945c79de6eSShinta Sugimoto memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); 26955c79de6eSShinta Sugimoto memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); 26965c79de6eSShinta Sugimoto memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); 26975c79de6eSShinta Sugimoto memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); 26985c79de6eSShinta Sugimoto 26995c79de6eSShinta Sugimoto ma->proto = um->proto; 27005c79de6eSShinta Sugimoto ma->mode = um->mode; 27015c79de6eSShinta Sugimoto ma->reqid = um->reqid; 27025c79de6eSShinta Sugimoto 27035c79de6eSShinta Sugimoto ma->old_family = um->old_family; 27045c79de6eSShinta Sugimoto ma->new_family = um->new_family; 27055c79de6eSShinta Sugimoto } 27065c79de6eSShinta Sugimoto 27075c79de6eSShinta Sugimoto *num = i; 27085c79de6eSShinta Sugimoto return 0; 27095c79de6eSShinta Sugimoto } 27105c79de6eSShinta Sugimoto 27115c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 27123bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 27135c79de6eSShinta Sugimoto { 27147b67c857SThomas Graf struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); 27155c79de6eSShinta Sugimoto struct xfrm_migrate m[XFRM_MAX_DEPTH]; 271613c1d189SArnaud Ebalard struct xfrm_kmaddress km, *kmp; 27175c79de6eSShinta Sugimoto u8 type; 27185c79de6eSShinta Sugimoto int err; 27195c79de6eSShinta Sugimoto int n = 0; 27208d549c4fSFan Du struct net *net = sock_net(skb->sk); 27214ab47d47SAntony Antony struct xfrm_encap_tmpl *encap = NULL; 2722c1aca308SYan Yan u32 if_id = 0; 27235c79de6eSShinta Sugimoto 2724f157c416SSabrina Dubroca if (!attrs[XFRMA_MIGRATE]) 2725cf5cb79fSThomas Graf return -EINVAL; 27265c79de6eSShinta Sugimoto 272713c1d189SArnaud Ebalard kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL; 272813c1d189SArnaud Ebalard 2729fb7deabaSSabrina Dubroca err = copy_from_user_policy_type(&type, attrs, extack); 27305c79de6eSShinta Sugimoto if (err) 27315c79de6eSShinta Sugimoto return err; 27325c79de6eSShinta Sugimoto 2733f157c416SSabrina Dubroca err = copy_from_user_migrate(m, kmp, attrs, &n); 27345c79de6eSShinta Sugimoto if (err) 27355c79de6eSShinta Sugimoto return err; 27365c79de6eSShinta Sugimoto 27375c79de6eSShinta Sugimoto if (!n) 27385c79de6eSShinta Sugimoto return 0; 27395c79de6eSShinta Sugimoto 27404ab47d47SAntony Antony if (attrs[XFRMA_ENCAP]) { 27414ab47d47SAntony Antony encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), 27424ab47d47SAntony Antony sizeof(*encap), GFP_KERNEL); 27434ab47d47SAntony Antony if (!encap) 27444ac7a6eeSZheng Yongjun return -ENOMEM; 27455c79de6eSShinta Sugimoto } 27464ab47d47SAntony Antony 2747c1aca308SYan Yan if (attrs[XFRMA_IF_ID]) 2748c1aca308SYan Yan if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 2749c1aca308SYan Yan 2750c1aca308SYan Yan err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, if_id); 27514ab47d47SAntony Antony 27524ab47d47SAntony Antony kfree(encap); 27534ab47d47SAntony Antony 27544ab47d47SAntony Antony return err; 27554ab47d47SAntony Antony } 27565c79de6eSShinta Sugimoto #else 27575c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 27583bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 27595c79de6eSShinta Sugimoto { 27605c79de6eSShinta Sugimoto return -ENOPROTOOPT; 27615c79de6eSShinta Sugimoto } 27625c79de6eSShinta Sugimoto #endif 27635c79de6eSShinta Sugimoto 27645c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 2765183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb) 27665c79de6eSShinta Sugimoto { 27675c79de6eSShinta Sugimoto struct xfrm_user_migrate um; 27685c79de6eSShinta Sugimoto 27695c79de6eSShinta Sugimoto memset(&um, 0, sizeof(um)); 27705c79de6eSShinta Sugimoto um.proto = m->proto; 27715c79de6eSShinta Sugimoto um.mode = m->mode; 27725c79de6eSShinta Sugimoto um.reqid = m->reqid; 27735c79de6eSShinta Sugimoto um.old_family = m->old_family; 27745c79de6eSShinta Sugimoto memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); 27755c79de6eSShinta Sugimoto memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); 27765c79de6eSShinta Sugimoto um.new_family = m->new_family; 27775c79de6eSShinta Sugimoto memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); 27785c79de6eSShinta Sugimoto memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); 27795c79de6eSShinta Sugimoto 2780c0144beaSThomas Graf return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); 27815c79de6eSShinta Sugimoto } 27825c79de6eSShinta Sugimoto 2783183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb) 278413c1d189SArnaud Ebalard { 278513c1d189SArnaud Ebalard struct xfrm_user_kmaddress uk; 278613c1d189SArnaud Ebalard 278713c1d189SArnaud Ebalard memset(&uk, 0, sizeof(uk)); 278813c1d189SArnaud Ebalard uk.family = k->family; 278913c1d189SArnaud Ebalard uk.reserved = k->reserved; 279013c1d189SArnaud Ebalard memcpy(&uk.local, &k->local, sizeof(uk.local)); 2791a1caa322SArnaud Ebalard memcpy(&uk.remote, &k->remote, sizeof(uk.remote)); 279213c1d189SArnaud Ebalard 279313c1d189SArnaud Ebalard return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk); 279413c1d189SArnaud Ebalard } 279513c1d189SArnaud Ebalard 2796a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma, 27978bafd730SAntony Antony int with_encp) 27987deb2264SThomas Graf { 27997deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) 280013c1d189SArnaud Ebalard + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0) 28018bafd730SAntony Antony + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0) 28027deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) 28037deb2264SThomas Graf + userpolicy_type_attrsize(); 28047deb2264SThomas Graf } 28057deb2264SThomas Graf 2806183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m, 2807183cad12SDavid S. Miller int num_migrate, const struct xfrm_kmaddress *k, 28088bafd730SAntony Antony const struct xfrm_selector *sel, 28098bafd730SAntony Antony const struct xfrm_encap_tmpl *encap, u8 dir, u8 type) 28105c79de6eSShinta Sugimoto { 2811183cad12SDavid S. Miller const struct xfrm_migrate *mp; 28125c79de6eSShinta Sugimoto struct xfrm_userpolicy_id *pol_id; 28135c79de6eSShinta Sugimoto struct nlmsghdr *nlh; 28141d1e34ddSDavid S. Miller int i, err; 28155c79de6eSShinta Sugimoto 281679b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); 281779b8b7f4SThomas Graf if (nlh == NULL) 281879b8b7f4SThomas Graf return -EMSGSIZE; 28195c79de6eSShinta Sugimoto 28207b67c857SThomas Graf pol_id = nlmsg_data(nlh); 28215c79de6eSShinta Sugimoto /* copy data from selector, dir, and type to the pol_id */ 28225c79de6eSShinta Sugimoto memset(pol_id, 0, sizeof(*pol_id)); 28235c79de6eSShinta Sugimoto memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); 28245c79de6eSShinta Sugimoto pol_id->dir = dir; 28255c79de6eSShinta Sugimoto 28261d1e34ddSDavid S. Miller if (k != NULL) { 28271d1e34ddSDavid S. Miller err = copy_to_user_kmaddress(k, skb); 28281d1e34ddSDavid S. Miller if (err) 28291d1e34ddSDavid S. Miller goto out_cancel; 28301d1e34ddSDavid S. Miller } 28318bafd730SAntony Antony if (encap) { 28328bafd730SAntony Antony err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap); 28338bafd730SAntony Antony if (err) 28348bafd730SAntony Antony goto out_cancel; 28358bafd730SAntony Antony } 28361d1e34ddSDavid S. Miller err = copy_to_user_policy_type(type, skb); 28371d1e34ddSDavid S. Miller if (err) 28381d1e34ddSDavid S. Miller goto out_cancel; 28395c79de6eSShinta Sugimoto for (i = 0, mp = m ; i < num_migrate; i++, mp++) { 28401d1e34ddSDavid S. Miller err = copy_to_user_migrate(mp, skb); 28411d1e34ddSDavid S. Miller if (err) 28421d1e34ddSDavid S. Miller goto out_cancel; 28435c79de6eSShinta Sugimoto } 28445c79de6eSShinta Sugimoto 2845053c095aSJohannes Berg nlmsg_end(skb, nlh); 2846053c095aSJohannes Berg return 0; 28471d1e34ddSDavid S. Miller 28481d1e34ddSDavid S. Miller out_cancel: 28499825069dSThomas Graf nlmsg_cancel(skb, nlh); 28501d1e34ddSDavid S. Miller return err; 28515c79de6eSShinta Sugimoto } 28525c79de6eSShinta Sugimoto 2853183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2854183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 28558bafd730SAntony Antony const struct xfrm_kmaddress *k, 28568bafd730SAntony Antony const struct xfrm_encap_tmpl *encap) 28575c79de6eSShinta Sugimoto { 2858a6483b79SAlexey Dobriyan struct net *net = &init_net; 28595c79de6eSShinta Sugimoto struct sk_buff *skb; 28602fc5f83bSGustavo A. R. Silva int err; 28615c79de6eSShinta Sugimoto 28628bafd730SAntony Antony skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap), 28638bafd730SAntony Antony GFP_ATOMIC); 28645c79de6eSShinta Sugimoto if (skb == NULL) 28655c79de6eSShinta Sugimoto return -ENOMEM; 28665c79de6eSShinta Sugimoto 28675c79de6eSShinta Sugimoto /* build migrate */ 28682fc5f83bSGustavo A. R. Silva err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type); 28692fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 28705c79de6eSShinta Sugimoto 287121ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE); 28725c79de6eSShinta Sugimoto } 28735c79de6eSShinta Sugimoto #else 2874183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2875183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 28768bafd730SAntony Antony const struct xfrm_kmaddress *k, 28778bafd730SAntony Antony const struct xfrm_encap_tmpl *encap) 28785c79de6eSShinta Sugimoto { 28795c79de6eSShinta Sugimoto return -ENOPROTOOPT; 28805c79de6eSShinta Sugimoto } 28815c79de6eSShinta Sugimoto #endif 2882d51d081dSJamal Hadi Salim 2883a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type) 2884492b558bSThomas Graf 28855461fc0cSDmitry Safonov const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { 288666f9a259SDavid S. Miller [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2887492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2888492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2889492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2890492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2891492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2892492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), 2893980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), 289453bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), 2895492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 289666f9a259SDavid S. Miller [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 28976c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), 2898492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), 2899a7bd9a45SThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, 2900d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2901d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 290297a64b45SMasahide NAKAMURA [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), 29035c79de6eSShinta Sugimoto [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2904a7bd9a45SThomas Graf [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), 2905880a6fabSChristophe Gouault [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 2906a7bd9a45SThomas Graf [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 29072d151d39SSteffen Klassert [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default), 29082d151d39SSteffen Klassert [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default), 29091da177e4SLinus Torvalds }; 29105461fc0cSDmitry Safonov EXPORT_SYMBOL_GPL(xfrm_msg_min); 29111da177e4SLinus Torvalds 2912492b558bSThomas Graf #undef XMSGSIZE 2913492b558bSThomas Graf 29145106f4a8SDmitry Safonov const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { 2915c28e9304Sjamal [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)}, 2916c28e9304Sjamal [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)}, 2917c28e9304Sjamal [XFRMA_LASTUSED] = { .type = NLA_U64}, 2918c28e9304Sjamal [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)}, 29191a6509d9SHerbert Xu [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, 2920cf5cb79fSThomas Graf [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, 2921cf5cb79fSThomas Graf [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, 2922cf5cb79fSThomas Graf [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, 2923cf5cb79fSThomas Graf [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, 2924cf5cb79fSThomas Graf [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, 2925cf5cb79fSThomas Graf [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, 2926cf5cb79fSThomas Graf [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, 2927cf5cb79fSThomas Graf [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, 2928cf5cb79fSThomas Graf [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, 2929cf5cb79fSThomas Graf [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, 2930cf5cb79fSThomas Graf [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, 2931cf5cb79fSThomas Graf [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, 2932cf5cb79fSThomas Graf [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, 2933cf5cb79fSThomas Graf [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, 293413c1d189SArnaud Ebalard [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) }, 29356f26b61eSJamal Hadi Salim [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) }, 293635d2856bSMartin Willi [XFRMA_TFCPAD] = { .type = NLA_U32 }, 2937d8647b79SSteffen Klassert [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) }, 2938a947b0a9SNicolas Dichtel [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 }, 2939d3623099SNicolas Dichtel [XFRMA_PROTO] = { .type = NLA_U8 }, 2940870a2df4SNicolas Dichtel [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) }, 2941d77e38e6SSteffen Klassert [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) }, 29429b42c1f1SSteffen Klassert [XFRMA_SET_MARK] = { .type = NLA_U32 }, 29439b42c1f1SSteffen Klassert [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 }, 29447e652640SSteffen Klassert [XFRMA_IF_ID] = { .type = NLA_U32 }, 2945cf5cb79fSThomas Graf }; 29465106f4a8SDmitry Safonov EXPORT_SYMBOL_GPL(xfrma_policy); 2947cf5cb79fSThomas Graf 2948880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = { 2949880a6fabSChristophe Gouault [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2950880a6fabSChristophe Gouault [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2951880a6fabSChristophe Gouault }; 2952880a6fabSChristophe Gouault 295305600a79SMathias Krause static const struct xfrm_link { 29543bec6c3eSSabrina Dubroca int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **, 29553bec6c3eSSabrina Dubroca struct netlink_ext_ack *); 29561137b5e2SHerbert Xu int (*start)(struct netlink_callback *); 29571da177e4SLinus Torvalds int (*dump)(struct sk_buff *, struct netlink_callback *); 29584c563f76STimo Teras int (*done)(struct netlink_callback *); 2959880a6fabSChristophe Gouault const struct nla_policy *nla_pol; 2960880a6fabSChristophe Gouault int nla_max; 2961492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = { 2962492b558bSThomas Graf [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 2963492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, 2964492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, 29654c563f76STimo Teras .dump = xfrm_dump_sa, 29664c563f76STimo Teras .done = xfrm_dump_sa_done }, 2967492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2968492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, 2969492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, 29701137b5e2SHerbert Xu .start = xfrm_dump_policy_start, 29714c563f76STimo Teras .dump = xfrm_dump_policy, 29724c563f76STimo Teras .done = xfrm_dump_policy_done }, 2973492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, 2974980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, 297553bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, 2976492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2977492b558bSThomas Graf [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 29786c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, 2979492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, 2980492b558bSThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, 2981d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, 2982d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, 29835c79de6eSShinta Sugimoto [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, 298428d8909bSJamal Hadi Salim [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, 2985880a6fabSChristophe Gouault [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo, 2986880a6fabSChristophe Gouault .nla_pol = xfrma_spd_policy, 2987880a6fabSChristophe Gouault .nla_max = XFRMA_SPD_MAX }, 2988ecfd6b18SJamal Hadi Salim [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, 29892d151d39SSteffen Klassert [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default }, 29902d151d39SSteffen Klassert [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default }, 29911da177e4SLinus Torvalds }; 29921da177e4SLinus Torvalds 29932d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 29942d4bc933SJohannes Berg struct netlink_ext_ack *extack) 29951da177e4SLinus Torvalds { 2996a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 299735a7aa08SThomas Graf struct nlattr *attrs[XFRMA_MAX+1]; 299805600a79SMathias Krause const struct xfrm_link *link; 29995106f4a8SDmitry Safonov struct nlmsghdr *nlh64 = NULL; 3000a7bd9a45SThomas Graf int type, err; 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds type = nlh->nlmsg_type; 30031da177e4SLinus Torvalds if (type > XFRM_MSG_MAX) 30041d00a4ebSThomas Graf return -EINVAL; 30051da177e4SLinus Torvalds 30061da177e4SLinus Torvalds type -= XFRM_MSG_BASE; 30071da177e4SLinus Torvalds link = &xfrm_dispatch[type]; 30081da177e4SLinus Torvalds 30091da177e4SLinus Torvalds /* All operations require privileges, even GET */ 301090f62cf3SEric W. Biederman if (!netlink_net_capable(skb, CAP_NET_ADMIN)) 30111d00a4ebSThomas Graf return -EPERM; 30121da177e4SLinus Torvalds 30135106f4a8SDmitry Safonov if (in_compat_syscall()) { 30145106f4a8SDmitry Safonov struct xfrm_translator *xtr = xfrm_get_translator(); 30155106f4a8SDmitry Safonov 30165106f4a8SDmitry Safonov if (!xtr) 30175106f4a8SDmitry Safonov return -EOPNOTSUPP; 30185106f4a8SDmitry Safonov 30195106f4a8SDmitry Safonov nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max, 30205106f4a8SDmitry Safonov link->nla_pol, extack); 30215106f4a8SDmitry Safonov xfrm_put_translator(xtr); 30225106f4a8SDmitry Safonov if (IS_ERR(nlh64)) 30235106f4a8SDmitry Safonov return PTR_ERR(nlh64); 30245106f4a8SDmitry Safonov if (nlh64) 30255106f4a8SDmitry Safonov nlh = nlh64; 30265106f4a8SDmitry Safonov } 30275106f4a8SDmitry Safonov 3028492b558bSThomas Graf if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || 3029492b558bSThomas Graf type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && 3030b8f3ab42SDavid S. Miller (nlh->nlmsg_flags & NLM_F_DUMP)) { 303180d326faSPablo Neira Ayuso struct netlink_dump_control c = { 30321137b5e2SHerbert Xu .start = link->start, 303380d326faSPablo Neira Ayuso .dump = link->dump, 303480d326faSPablo Neira Ayuso .done = link->done, 303580d326faSPablo Neira Ayuso }; 30365106f4a8SDmitry Safonov 30375106f4a8SDmitry Safonov if (link->dump == NULL) { 30385106f4a8SDmitry Safonov err = -EINVAL; 30395106f4a8SDmitry Safonov goto err; 304080d326faSPablo Neira Ayuso } 30415106f4a8SDmitry Safonov 30425106f4a8SDmitry Safonov err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c); 30435106f4a8SDmitry Safonov goto err; 30441da177e4SLinus Torvalds } 30451da177e4SLinus Torvalds 30468cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs, 3047880a6fabSChristophe Gouault link->nla_max ? : XFRMA_MAX, 3048fe52145fSJohannes Berg link->nla_pol ? : xfrma_policy, extack); 3049a7bd9a45SThomas Graf if (err < 0) 30505106f4a8SDmitry Safonov goto err; 30515106f4a8SDmitry Safonov 30525106f4a8SDmitry Safonov if (link->doit == NULL) { 30535106f4a8SDmitry Safonov err = -EINVAL; 30545106f4a8SDmitry Safonov goto err; 30555106f4a8SDmitry Safonov } 30565106f4a8SDmitry Safonov 30573bec6c3eSSabrina Dubroca err = link->doit(skb, nlh, attrs, extack); 30585106f4a8SDmitry Safonov 30597c1a80e8SPavel Skripkin /* We need to free skb allocated in xfrm_alloc_compat() before 30607c1a80e8SPavel Skripkin * returning from this function, because consume_skb() won't take 30617c1a80e8SPavel Skripkin * care of frag_list since netlink destructor sets 30627c1a80e8SPavel Skripkin * sbk->head to NULL. (see netlink_skb_destructor()) 30637c1a80e8SPavel Skripkin */ 30647c1a80e8SPavel Skripkin if (skb_has_frag_list(skb)) { 30657c1a80e8SPavel Skripkin kfree_skb(skb_shinfo(skb)->frag_list); 30667c1a80e8SPavel Skripkin skb_shinfo(skb)->frag_list = NULL; 30677c1a80e8SPavel Skripkin } 30687c1a80e8SPavel Skripkin 30695106f4a8SDmitry Safonov err: 30705106f4a8SDmitry Safonov kvfree(nlh64); 3071a7bd9a45SThomas Graf return err; 30721da177e4SLinus Torvalds } 30731da177e4SLinus Torvalds 3074cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb) 30751da177e4SLinus Torvalds { 3076283bc9f3SFan Du struct net *net = sock_net(skb->sk); 3077283bc9f3SFan Du 3078283bc9f3SFan Du mutex_lock(&net->xfrm.xfrm_cfg_mutex); 3079cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &xfrm_user_rcv_msg); 3080283bc9f3SFan Du mutex_unlock(&net->xfrm.xfrm_cfg_mutex); 30811da177e4SLinus Torvalds } 30821da177e4SLinus Torvalds 3083a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void) 30847deb2264SThomas Graf { 30856f26b61eSJamal Hadi Salim return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) 30866f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)); 30877deb2264SThomas Graf } 30887deb2264SThomas Graf 3089214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 30901da177e4SLinus Torvalds { 30911da177e4SLinus Torvalds struct xfrm_user_expire *ue; 30921da177e4SLinus Torvalds struct nlmsghdr *nlh; 30931d1e34ddSDavid S. Miller int err; 30941da177e4SLinus Torvalds 309515e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); 309679b8b7f4SThomas Graf if (nlh == NULL) 309779b8b7f4SThomas Graf return -EMSGSIZE; 30981da177e4SLinus Torvalds 30997b67c857SThomas Graf ue = nlmsg_data(nlh); 31001da177e4SLinus Torvalds copy_to_user_state(x, &ue->state); 3101d51d081dSJamal Hadi Salim ue->hard = (c->data.hard != 0) ? 1 : 0; 3102e3e5fc16SMathias Krause /* clear the padding bytes */ 3103caf283d0SKees Cook memset_after(ue, 0, hard); 31041da177e4SLinus Torvalds 31051d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &x->mark); 31061d1e34ddSDavid S. Miller if (err) 31071d1e34ddSDavid S. Miller return err; 31086f26b61eSJamal Hadi Salim 31097e652640SSteffen Klassert err = xfrm_if_id_put(skb, x->if_id); 31107e652640SSteffen Klassert if (err) 31117e652640SSteffen Klassert return err; 31127e652640SSteffen Klassert 3113053c095aSJohannes Berg nlmsg_end(skb, nlh); 3114053c095aSJohannes Berg return 0; 31151da177e4SLinus Torvalds } 31161da177e4SLinus Torvalds 3117214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c) 31181da177e4SLinus Torvalds { 3119fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 31201da177e4SLinus Torvalds struct sk_buff *skb; 31211da177e4SLinus Torvalds 31227deb2264SThomas Graf skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); 31231da177e4SLinus Torvalds if (skb == NULL) 31241da177e4SLinus Torvalds return -ENOMEM; 31251da177e4SLinus Torvalds 31266f26b61eSJamal Hadi Salim if (build_expire(skb, x, c) < 0) { 31276f26b61eSJamal Hadi Salim kfree_skb(skb); 31286f26b61eSJamal Hadi Salim return -EMSGSIZE; 31296f26b61eSJamal Hadi Salim } 31301da177e4SLinus Torvalds 313121ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 31321da177e4SLinus Torvalds } 31331da177e4SLinus Torvalds 3134214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c) 3135d51d081dSJamal Hadi Salim { 3136fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 3137d51d081dSJamal Hadi Salim struct sk_buff *skb; 31382fc5f83bSGustavo A. R. Silva int err; 3139d51d081dSJamal Hadi Salim 3140d8647b79SSteffen Klassert skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 3141d51d081dSJamal Hadi Salim if (skb == NULL) 3142d51d081dSJamal Hadi Salim return -ENOMEM; 3143d51d081dSJamal Hadi Salim 31442fc5f83bSGustavo A. R. Silva err = build_aevent(skb, x, c); 31452fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 3146d51d081dSJamal Hadi Salim 314721ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS); 3148d51d081dSJamal Hadi Salim } 3149d51d081dSJamal Hadi Salim 3150214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c) 315126b15dadSJamal Hadi Salim { 31527067802eSAlexey Dobriyan struct net *net = c->net; 315326b15dadSJamal Hadi Salim struct xfrm_usersa_flush *p; 315426b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 315526b15dadSJamal Hadi Salim struct sk_buff *skb; 31567deb2264SThomas Graf int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); 315726b15dadSJamal Hadi Salim 31587deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 315926b15dadSJamal Hadi Salim if (skb == NULL) 316026b15dadSJamal Hadi Salim return -ENOMEM; 316126b15dadSJamal Hadi Salim 316215e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); 316379b8b7f4SThomas Graf if (nlh == NULL) { 316479b8b7f4SThomas Graf kfree_skb(skb); 316579b8b7f4SThomas Graf return -EMSGSIZE; 316679b8b7f4SThomas Graf } 316726b15dadSJamal Hadi Salim 31687b67c857SThomas Graf p = nlmsg_data(nlh); 3169bf08867fSHerbert Xu p->proto = c->data.proto; 317026b15dadSJamal Hadi Salim 31719825069dSThomas Graf nlmsg_end(skb, nlh); 317226b15dadSJamal Hadi Salim 317321ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 317426b15dadSJamal Hadi Salim } 317526b15dadSJamal Hadi Salim 3176a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x) 317726b15dadSJamal Hadi Salim { 3178a1b831f2SAlexey Dobriyan unsigned int l = 0; 31791a6509d9SHerbert Xu if (x->aead) 31801a6509d9SHerbert Xu l += nla_total_size(aead_len(x->aead)); 31814447bb33SMartin Willi if (x->aalg) { 31824447bb33SMartin Willi l += nla_total_size(sizeof(struct xfrm_algo) + 31834447bb33SMartin Willi (x->aalg->alg_key_len + 7) / 8); 31844447bb33SMartin Willi l += nla_total_size(xfrm_alg_auth_len(x->aalg)); 31854447bb33SMartin Willi } 318626b15dadSJamal Hadi Salim if (x->ealg) 31870f99be0dSEric Dumazet l += nla_total_size(xfrm_alg_len(x->ealg)); 318826b15dadSJamal Hadi Salim if (x->calg) 31897deb2264SThomas Graf l += nla_total_size(sizeof(*x->calg)); 319026b15dadSJamal Hadi Salim if (x->encap) 31917deb2264SThomas Graf l += nla_total_size(sizeof(*x->encap)); 319235d2856bSMartin Willi if (x->tfcpad) 319335d2856bSMartin Willi l += nla_total_size(sizeof(x->tfcpad)); 3194d8647b79SSteffen Klassert if (x->replay_esn) 3195d8647b79SSteffen Klassert l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn)); 3196f293a5e3Sdingzhi else 3197f293a5e3Sdingzhi l += nla_total_size(sizeof(struct xfrm_replay_state)); 319868325d3bSHerbert Xu if (x->security) 319968325d3bSHerbert Xu l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + 320068325d3bSHerbert Xu x->security->ctx_len); 320168325d3bSHerbert Xu if (x->coaddr) 320268325d3bSHerbert Xu l += nla_total_size(sizeof(*x->coaddr)); 3203a947b0a9SNicolas Dichtel if (x->props.extra_flags) 3204a947b0a9SNicolas Dichtel l += nla_total_size(sizeof(x->props.extra_flags)); 3205d77e38e6SSteffen Klassert if (x->xso.dev) 32067770a39dSEric Dumazet l += nla_total_size(sizeof(struct xfrm_user_offload)); 32079b42c1f1SSteffen Klassert if (x->props.smark.v | x->props.smark.m) { 32089b42c1f1SSteffen Klassert l += nla_total_size(sizeof(x->props.smark.v)); 32099b42c1f1SSteffen Klassert l += nla_total_size(sizeof(x->props.smark.m)); 32109b42c1f1SSteffen Klassert } 32117e652640SSteffen Klassert if (x->if_id) 32127e652640SSteffen Klassert l += nla_total_size(sizeof(x->if_id)); 321368325d3bSHerbert Xu 3214d26f3984SHerbert Xu /* Must count x->lastused as it may become non-zero behind our back. */ 3215de95c4a4SNicolas Dichtel l += nla_total_size_64bit(sizeof(u64)); 321626b15dadSJamal Hadi Salim 32174e484b3eSAntony Antony if (x->mapping_maxage) 32184e484b3eSAntony Antony l += nla_total_size(sizeof(x->mapping_maxage)); 32194e484b3eSAntony Antony 322026b15dadSJamal Hadi Salim return l; 322126b15dadSJamal Hadi Salim } 322226b15dadSJamal Hadi Salim 3223214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c) 322426b15dadSJamal Hadi Salim { 3225fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 322626b15dadSJamal Hadi Salim struct xfrm_usersa_info *p; 32270603eac0SHerbert Xu struct xfrm_usersa_id *id; 322826b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 322926b15dadSJamal Hadi Salim struct sk_buff *skb; 3230a1b831f2SAlexey Dobriyan unsigned int len = xfrm_sa_len(x); 3231a1b831f2SAlexey Dobriyan unsigned int headlen; 3232a1b831f2SAlexey Dobriyan int err; 32330603eac0SHerbert Xu 32340603eac0SHerbert Xu headlen = sizeof(*p); 32350603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 32367deb2264SThomas Graf len += nla_total_size(headlen); 32370603eac0SHerbert Xu headlen = sizeof(*id); 32386f26b61eSJamal Hadi Salim len += nla_total_size(sizeof(struct xfrm_mark)); 32390603eac0SHerbert Xu } 32407deb2264SThomas Graf len += NLMSG_ALIGN(headlen); 324126b15dadSJamal Hadi Salim 32427deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 324326b15dadSJamal Hadi Salim if (skb == NULL) 324426b15dadSJamal Hadi Salim return -ENOMEM; 324526b15dadSJamal Hadi Salim 324615e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 32471d1e34ddSDavid S. Miller err = -EMSGSIZE; 324879b8b7f4SThomas Graf if (nlh == NULL) 32491d1e34ddSDavid S. Miller goto out_free_skb; 325026b15dadSJamal Hadi Salim 32517b67c857SThomas Graf p = nlmsg_data(nlh); 32520603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 3253c0144beaSThomas Graf struct nlattr *attr; 3254c0144beaSThomas Graf 32557b67c857SThomas Graf id = nlmsg_data(nlh); 325650329c8aSMathias Krause memset(id, 0, sizeof(*id)); 32570603eac0SHerbert Xu memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); 32580603eac0SHerbert Xu id->spi = x->id.spi; 32590603eac0SHerbert Xu id->family = x->props.family; 32600603eac0SHerbert Xu id->proto = x->id.proto; 32610603eac0SHerbert Xu 3262c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); 32631d1e34ddSDavid S. Miller err = -EMSGSIZE; 3264c0144beaSThomas Graf if (attr == NULL) 32651d1e34ddSDavid S. Miller goto out_free_skb; 3266c0144beaSThomas Graf 3267c0144beaSThomas Graf p = nla_data(attr); 32680603eac0SHerbert Xu } 32691d1e34ddSDavid S. Miller err = copy_to_user_state_extra(x, p, skb); 32701d1e34ddSDavid S. Miller if (err) 32711d1e34ddSDavid S. Miller goto out_free_skb; 327226b15dadSJamal Hadi Salim 32739825069dSThomas Graf nlmsg_end(skb, nlh); 327426b15dadSJamal Hadi Salim 327521ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 327626b15dadSJamal Hadi Salim 32771d1e34ddSDavid S. Miller out_free_skb: 327826b15dadSJamal Hadi Salim kfree_skb(skb); 32791d1e34ddSDavid S. Miller return err; 328026b15dadSJamal Hadi Salim } 328126b15dadSJamal Hadi Salim 3282214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c) 328326b15dadSJamal Hadi Salim { 328426b15dadSJamal Hadi Salim 328526b15dadSJamal Hadi Salim switch (c->event) { 3286f60f6b8fSHerbert Xu case XFRM_MSG_EXPIRE: 328726b15dadSJamal Hadi Salim return xfrm_exp_state_notify(x, c); 3288d51d081dSJamal Hadi Salim case XFRM_MSG_NEWAE: 3289d51d081dSJamal Hadi Salim return xfrm_aevent_state_notify(x, c); 3290f60f6b8fSHerbert Xu case XFRM_MSG_DELSA: 3291f60f6b8fSHerbert Xu case XFRM_MSG_UPDSA: 3292f60f6b8fSHerbert Xu case XFRM_MSG_NEWSA: 329326b15dadSJamal Hadi Salim return xfrm_notify_sa(x, c); 3294f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHSA: 329526b15dadSJamal Hadi Salim return xfrm_notify_sa_flush(c); 329626b15dadSJamal Hadi Salim default: 329762db5cfdSstephen hemminger printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n", 329862db5cfdSstephen hemminger c->event); 329926b15dadSJamal Hadi Salim break; 330026b15dadSJamal Hadi Salim } 330126b15dadSJamal Hadi Salim 330226b15dadSJamal Hadi Salim return 0; 330326b15dadSJamal Hadi Salim 330426b15dadSJamal Hadi Salim } 330526b15dadSJamal Hadi Salim 3306a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x, 33077deb2264SThomas Graf struct xfrm_policy *xp) 33087deb2264SThomas Graf { 33097deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) 33107deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 33116f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 33127deb2264SThomas Graf + nla_total_size(xfrm_user_sec_ctx_size(x->security)) 33137deb2264SThomas Graf + userpolicy_type_attrsize(); 33147deb2264SThomas Graf } 33157deb2264SThomas Graf 33161da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, 331765e0736bSFan Du struct xfrm_tmpl *xt, struct xfrm_policy *xp) 33181da177e4SLinus Torvalds { 33191d1e34ddSDavid S. Miller __u32 seq = xfrm_get_acqseq(); 33201da177e4SLinus Torvalds struct xfrm_user_acquire *ua; 33211da177e4SLinus Torvalds struct nlmsghdr *nlh; 33221d1e34ddSDavid S. Miller int err; 33231da177e4SLinus Torvalds 332479b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); 332579b8b7f4SThomas Graf if (nlh == NULL) 332679b8b7f4SThomas Graf return -EMSGSIZE; 33271da177e4SLinus Torvalds 33287b67c857SThomas Graf ua = nlmsg_data(nlh); 33291da177e4SLinus Torvalds memcpy(&ua->id, &x->id, sizeof(ua->id)); 33301da177e4SLinus Torvalds memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); 33311da177e4SLinus Torvalds memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); 333265e0736bSFan Du copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT); 33331da177e4SLinus Torvalds ua->aalgos = xt->aalgos; 33341da177e4SLinus Torvalds ua->ealgos = xt->ealgos; 33351da177e4SLinus Torvalds ua->calgos = xt->calgos; 33361da177e4SLinus Torvalds ua->seq = x->km.seq = seq; 33371da177e4SLinus Torvalds 33381d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 33391d1e34ddSDavid S. Miller if (!err) 33401d1e34ddSDavid S. Miller err = copy_to_user_state_sec_ctx(x, skb); 33411d1e34ddSDavid S. Miller if (!err) 33421d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 33431d1e34ddSDavid S. Miller if (!err) 33441d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 33457e652640SSteffen Klassert if (!err) 33467e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 33471d1e34ddSDavid S. Miller if (err) { 33481d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 33491d1e34ddSDavid S. Miller return err; 33501d1e34ddSDavid S. Miller } 33511da177e4SLinus Torvalds 3352053c095aSJohannes Berg nlmsg_end(skb, nlh); 3353053c095aSJohannes Berg return 0; 33541da177e4SLinus Torvalds } 33551da177e4SLinus Torvalds 33561da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, 335765e0736bSFan Du struct xfrm_policy *xp) 33581da177e4SLinus Torvalds { 3359a6483b79SAlexey Dobriyan struct net *net = xs_net(x); 33601da177e4SLinus Torvalds struct sk_buff *skb; 33612fc5f83bSGustavo A. R. Silva int err; 33621da177e4SLinus Torvalds 33637deb2264SThomas Graf skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); 33641da177e4SLinus Torvalds if (skb == NULL) 33651da177e4SLinus Torvalds return -ENOMEM; 33661da177e4SLinus Torvalds 33672fc5f83bSGustavo A. R. Silva err = build_acquire(skb, x, xt, xp); 33682fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 33691da177e4SLinus Torvalds 337021ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE); 33711da177e4SLinus Torvalds } 33721da177e4SLinus Torvalds 33731da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0 33741da177e4SLinus Torvalds * or more templates. 33751da177e4SLinus Torvalds */ 3376cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, 33771da177e4SLinus Torvalds u8 *data, int len, int *dir) 33781da177e4SLinus Torvalds { 3379fc34acd3SAlexey Dobriyan struct net *net = sock_net(sk); 33801da177e4SLinus Torvalds struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; 33811da177e4SLinus Torvalds struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); 33821da177e4SLinus Torvalds struct xfrm_policy *xp; 33831da177e4SLinus Torvalds int nr; 33841da177e4SLinus Torvalds 3385cb969f07SVenkat Yekkirala switch (sk->sk_family) { 33861da177e4SLinus Torvalds case AF_INET: 33871da177e4SLinus Torvalds if (opt != IP_XFRM_POLICY) { 33881da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 33891da177e4SLinus Torvalds return NULL; 33901da177e4SLinus Torvalds } 33911da177e4SLinus Torvalds break; 3392dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 33931da177e4SLinus Torvalds case AF_INET6: 33941da177e4SLinus Torvalds if (opt != IPV6_XFRM_POLICY) { 33951da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 33961da177e4SLinus Torvalds return NULL; 33971da177e4SLinus Torvalds } 33981da177e4SLinus Torvalds break; 33991da177e4SLinus Torvalds #endif 34001da177e4SLinus Torvalds default: 34011da177e4SLinus Torvalds *dir = -EINVAL; 34021da177e4SLinus Torvalds return NULL; 34031da177e4SLinus Torvalds } 34041da177e4SLinus Torvalds 34051da177e4SLinus Torvalds *dir = -EINVAL; 34061da177e4SLinus Torvalds 34071da177e4SLinus Torvalds if (len < sizeof(*p) || 3408ec2b4f01SSabrina Dubroca verify_newpolicy_info(p, NULL)) 34091da177e4SLinus Torvalds return NULL; 34101da177e4SLinus Torvalds 34111da177e4SLinus Torvalds nr = ((len - sizeof(*p)) / sizeof(*ut)); 3412d37bed89SSabrina Dubroca if (validate_tmpl(nr, ut, p->sel.family, NULL)) 34131da177e4SLinus Torvalds return NULL; 34141da177e4SLinus Torvalds 3415a4f1bac6SHerbert Xu if (p->dir > XFRM_POLICY_OUT) 3416a4f1bac6SHerbert Xu return NULL; 3417a4f1bac6SHerbert Xu 34182f09a4d5SHerbert Xu xp = xfrm_policy_alloc(net, GFP_ATOMIC); 34191da177e4SLinus Torvalds if (xp == NULL) { 34201da177e4SLinus Torvalds *dir = -ENOBUFS; 34211da177e4SLinus Torvalds return NULL; 34221da177e4SLinus Torvalds } 34231da177e4SLinus Torvalds 34241da177e4SLinus Torvalds copy_from_user_policy(xp, p); 3425f7b6983fSMasahide NAKAMURA xp->type = XFRM_POLICY_TYPE_MAIN; 34261da177e4SLinus Torvalds copy_templates(xp, ut, nr); 34271da177e4SLinus Torvalds 34281da177e4SLinus Torvalds *dir = p->dir; 34291da177e4SLinus Torvalds 34301da177e4SLinus Torvalds return xp; 34311da177e4SLinus Torvalds } 34321da177e4SLinus Torvalds 3433a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp) 34347deb2264SThomas Graf { 34357deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) 34367deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 34377deb2264SThomas Graf + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) 3438295fae56SJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 34397deb2264SThomas Graf + userpolicy_type_attrsize(); 34407deb2264SThomas Graf } 34417deb2264SThomas Graf 34421da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, 3443214e005bSDavid S. Miller int dir, const struct km_event *c) 34441da177e4SLinus Torvalds { 34451da177e4SLinus Torvalds struct xfrm_user_polexpire *upe; 3446d51d081dSJamal Hadi Salim int hard = c->data.hard; 34471d1e34ddSDavid S. Miller struct nlmsghdr *nlh; 34481d1e34ddSDavid S. Miller int err; 34491da177e4SLinus Torvalds 345015e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); 345179b8b7f4SThomas Graf if (nlh == NULL) 345279b8b7f4SThomas Graf return -EMSGSIZE; 34531da177e4SLinus Torvalds 34547b67c857SThomas Graf upe = nlmsg_data(nlh); 34551da177e4SLinus Torvalds copy_to_user_policy(xp, &upe->pol, dir); 34561d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 34571d1e34ddSDavid S. Miller if (!err) 34581d1e34ddSDavid S. Miller err = copy_to_user_sec_ctx(xp, skb); 34591d1e34ddSDavid S. Miller if (!err) 34601d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 34611d1e34ddSDavid S. Miller if (!err) 34621d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 34637e652640SSteffen Klassert if (!err) 34647e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 34651d1e34ddSDavid S. Miller if (err) { 34661d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 34671d1e34ddSDavid S. Miller return err; 34681d1e34ddSDavid S. Miller } 34691da177e4SLinus Torvalds upe->hard = !!hard; 34701da177e4SLinus Torvalds 3471053c095aSJohannes Berg nlmsg_end(skb, nlh); 3472053c095aSJohannes Berg return 0; 34731da177e4SLinus Torvalds } 34741da177e4SLinus Torvalds 3475214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 34761da177e4SLinus Torvalds { 3477fc34acd3SAlexey Dobriyan struct net *net = xp_net(xp); 34781da177e4SLinus Torvalds struct sk_buff *skb; 34792fc5f83bSGustavo A. R. Silva int err; 34801da177e4SLinus Torvalds 34817deb2264SThomas Graf skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); 34821da177e4SLinus Torvalds if (skb == NULL) 34831da177e4SLinus Torvalds return -ENOMEM; 34841da177e4SLinus Torvalds 34852fc5f83bSGustavo A. R. Silva err = build_polexpire(skb, xp, dir, c); 34862fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 34871da177e4SLinus Torvalds 348821ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 34891da177e4SLinus Torvalds } 34901da177e4SLinus Torvalds 3491214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c) 349226b15dadSJamal Hadi Salim { 3493a1b831f2SAlexey Dobriyan unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 3494fc34acd3SAlexey Dobriyan struct net *net = xp_net(xp); 349526b15dadSJamal Hadi Salim struct xfrm_userpolicy_info *p; 34960603eac0SHerbert Xu struct xfrm_userpolicy_id *id; 349726b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 349826b15dadSJamal Hadi Salim struct sk_buff *skb; 3499a1b831f2SAlexey Dobriyan unsigned int headlen; 3500a1b831f2SAlexey Dobriyan int err; 35010603eac0SHerbert Xu 35020603eac0SHerbert Xu headlen = sizeof(*p); 35030603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 35047deb2264SThomas Graf len += nla_total_size(headlen); 35050603eac0SHerbert Xu headlen = sizeof(*id); 35060603eac0SHerbert Xu } 3507cfbfd45aSThomas Graf len += userpolicy_type_attrsize(); 3508295fae56SJamal Hadi Salim len += nla_total_size(sizeof(struct xfrm_mark)); 35097deb2264SThomas Graf len += NLMSG_ALIGN(headlen); 351026b15dadSJamal Hadi Salim 35117deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 351226b15dadSJamal Hadi Salim if (skb == NULL) 351326b15dadSJamal Hadi Salim return -ENOMEM; 351426b15dadSJamal Hadi Salim 351515e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 35161d1e34ddSDavid S. Miller err = -EMSGSIZE; 351779b8b7f4SThomas Graf if (nlh == NULL) 35181d1e34ddSDavid S. Miller goto out_free_skb; 351926b15dadSJamal Hadi Salim 35207b67c857SThomas Graf p = nlmsg_data(nlh); 35210603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 3522c0144beaSThomas Graf struct nlattr *attr; 3523c0144beaSThomas Graf 35247b67c857SThomas Graf id = nlmsg_data(nlh); 35250603eac0SHerbert Xu memset(id, 0, sizeof(*id)); 35260603eac0SHerbert Xu id->dir = dir; 35270603eac0SHerbert Xu if (c->data.byid) 35280603eac0SHerbert Xu id->index = xp->index; 35290603eac0SHerbert Xu else 35300603eac0SHerbert Xu memcpy(&id->sel, &xp->selector, sizeof(id->sel)); 35310603eac0SHerbert Xu 3532c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); 35331d1e34ddSDavid S. Miller err = -EMSGSIZE; 3534c0144beaSThomas Graf if (attr == NULL) 35351d1e34ddSDavid S. Miller goto out_free_skb; 3536c0144beaSThomas Graf 3537c0144beaSThomas Graf p = nla_data(attr); 35380603eac0SHerbert Xu } 353926b15dadSJamal Hadi Salim 354026b15dadSJamal Hadi Salim copy_to_user_policy(xp, p, dir); 35411d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 35421d1e34ddSDavid S. Miller if (!err) 35431d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 35441d1e34ddSDavid S. Miller if (!err) 35451d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 35467e652640SSteffen Klassert if (!err) 35477e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 35481d1e34ddSDavid S. Miller if (err) 35491d1e34ddSDavid S. Miller goto out_free_skb; 3550295fae56SJamal Hadi Salim 35519825069dSThomas Graf nlmsg_end(skb, nlh); 355226b15dadSJamal Hadi Salim 355321ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 355426b15dadSJamal Hadi Salim 35551d1e34ddSDavid S. Miller out_free_skb: 355626b15dadSJamal Hadi Salim kfree_skb(skb); 35571d1e34ddSDavid S. Miller return err; 355826b15dadSJamal Hadi Salim } 355926b15dadSJamal Hadi Salim 3560214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c) 356126b15dadSJamal Hadi Salim { 35627067802eSAlexey Dobriyan struct net *net = c->net; 356326b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 356426b15dadSJamal Hadi Salim struct sk_buff *skb; 35651d1e34ddSDavid S. Miller int err; 356626b15dadSJamal Hadi Salim 35677deb2264SThomas Graf skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); 356826b15dadSJamal Hadi Salim if (skb == NULL) 356926b15dadSJamal Hadi Salim return -ENOMEM; 357026b15dadSJamal Hadi Salim 357115e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); 35721d1e34ddSDavid S. Miller err = -EMSGSIZE; 357379b8b7f4SThomas Graf if (nlh == NULL) 35741d1e34ddSDavid S. Miller goto out_free_skb; 35751d1e34ddSDavid S. Miller err = copy_to_user_policy_type(c->data.type, skb); 35761d1e34ddSDavid S. Miller if (err) 35771d1e34ddSDavid S. Miller goto out_free_skb; 357826b15dadSJamal Hadi Salim 35799825069dSThomas Graf nlmsg_end(skb, nlh); 358026b15dadSJamal Hadi Salim 358121ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 358226b15dadSJamal Hadi Salim 35831d1e34ddSDavid S. Miller out_free_skb: 358426b15dadSJamal Hadi Salim kfree_skb(skb); 35851d1e34ddSDavid S. Miller return err; 358626b15dadSJamal Hadi Salim } 358726b15dadSJamal Hadi Salim 3588214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 358926b15dadSJamal Hadi Salim { 359026b15dadSJamal Hadi Salim 359126b15dadSJamal Hadi Salim switch (c->event) { 3592f60f6b8fSHerbert Xu case XFRM_MSG_NEWPOLICY: 3593f60f6b8fSHerbert Xu case XFRM_MSG_UPDPOLICY: 3594f60f6b8fSHerbert Xu case XFRM_MSG_DELPOLICY: 359526b15dadSJamal Hadi Salim return xfrm_notify_policy(xp, dir, c); 3596f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHPOLICY: 359726b15dadSJamal Hadi Salim return xfrm_notify_policy_flush(c); 3598f60f6b8fSHerbert Xu case XFRM_MSG_POLEXPIRE: 359926b15dadSJamal Hadi Salim return xfrm_exp_policy_notify(xp, dir, c); 360026b15dadSJamal Hadi Salim default: 360162db5cfdSstephen hemminger printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n", 360262db5cfdSstephen hemminger c->event); 360326b15dadSJamal Hadi Salim } 360426b15dadSJamal Hadi Salim 360526b15dadSJamal Hadi Salim return 0; 360626b15dadSJamal Hadi Salim 360726b15dadSJamal Hadi Salim } 360826b15dadSJamal Hadi Salim 3609a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void) 36107deb2264SThomas Graf { 36117deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); 36127deb2264SThomas Graf } 36137deb2264SThomas Graf 361497a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto, 361597a64b45SMasahide NAKAMURA struct xfrm_selector *sel, xfrm_address_t *addr) 361697a64b45SMasahide NAKAMURA { 361797a64b45SMasahide NAKAMURA struct xfrm_user_report *ur; 361897a64b45SMasahide NAKAMURA struct nlmsghdr *nlh; 361997a64b45SMasahide NAKAMURA 362079b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); 362179b8b7f4SThomas Graf if (nlh == NULL) 362279b8b7f4SThomas Graf return -EMSGSIZE; 362397a64b45SMasahide NAKAMURA 36247b67c857SThomas Graf ur = nlmsg_data(nlh); 362597a64b45SMasahide NAKAMURA ur->proto = proto; 362697a64b45SMasahide NAKAMURA memcpy(&ur->sel, sel, sizeof(ur->sel)); 362797a64b45SMasahide NAKAMURA 36281d1e34ddSDavid S. Miller if (addr) { 36291d1e34ddSDavid S. Miller int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr); 36301d1e34ddSDavid S. Miller if (err) { 36319825069dSThomas Graf nlmsg_cancel(skb, nlh); 36321d1e34ddSDavid S. Miller return err; 36331d1e34ddSDavid S. Miller } 36341d1e34ddSDavid S. Miller } 3635053c095aSJohannes Berg nlmsg_end(skb, nlh); 3636053c095aSJohannes Berg return 0; 363797a64b45SMasahide NAKAMURA } 363897a64b45SMasahide NAKAMURA 3639db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto, 3640db983c11SAlexey Dobriyan struct xfrm_selector *sel, xfrm_address_t *addr) 364197a64b45SMasahide NAKAMURA { 364297a64b45SMasahide NAKAMURA struct sk_buff *skb; 36432fc5f83bSGustavo A. R. Silva int err; 364497a64b45SMasahide NAKAMURA 36457deb2264SThomas Graf skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); 364697a64b45SMasahide NAKAMURA if (skb == NULL) 364797a64b45SMasahide NAKAMURA return -ENOMEM; 364897a64b45SMasahide NAKAMURA 36492fc5f83bSGustavo A. R. Silva err = build_report(skb, proto, sel, addr); 36502fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 365197a64b45SMasahide NAKAMURA 365221ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT); 365397a64b45SMasahide NAKAMURA } 365497a64b45SMasahide NAKAMURA 3655a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void) 36563a2dfbe8SMartin Willi { 36573a2dfbe8SMartin Willi return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping)); 36583a2dfbe8SMartin Willi } 36593a2dfbe8SMartin Willi 36603a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x, 36613a2dfbe8SMartin Willi xfrm_address_t *new_saddr, __be16 new_sport) 36623a2dfbe8SMartin Willi { 36633a2dfbe8SMartin Willi struct xfrm_user_mapping *um; 36643a2dfbe8SMartin Willi struct nlmsghdr *nlh; 36653a2dfbe8SMartin Willi 36663a2dfbe8SMartin Willi nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0); 36673a2dfbe8SMartin Willi if (nlh == NULL) 36683a2dfbe8SMartin Willi return -EMSGSIZE; 36693a2dfbe8SMartin Willi 36703a2dfbe8SMartin Willi um = nlmsg_data(nlh); 36713a2dfbe8SMartin Willi 36723a2dfbe8SMartin Willi memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr)); 36733a2dfbe8SMartin Willi um->id.spi = x->id.spi; 36743a2dfbe8SMartin Willi um->id.family = x->props.family; 36753a2dfbe8SMartin Willi um->id.proto = x->id.proto; 36763a2dfbe8SMartin Willi memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr)); 36773a2dfbe8SMartin Willi memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr)); 36783a2dfbe8SMartin Willi um->new_sport = new_sport; 36793a2dfbe8SMartin Willi um->old_sport = x->encap->encap_sport; 36803a2dfbe8SMartin Willi um->reqid = x->props.reqid; 36813a2dfbe8SMartin Willi 3682053c095aSJohannes Berg nlmsg_end(skb, nlh); 3683053c095aSJohannes Berg return 0; 36843a2dfbe8SMartin Willi } 36853a2dfbe8SMartin Willi 36863a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, 36873a2dfbe8SMartin Willi __be16 sport) 36883a2dfbe8SMartin Willi { 3689a6483b79SAlexey Dobriyan struct net *net = xs_net(x); 36903a2dfbe8SMartin Willi struct sk_buff *skb; 36912fc5f83bSGustavo A. R. Silva int err; 36923a2dfbe8SMartin Willi 36933a2dfbe8SMartin Willi if (x->id.proto != IPPROTO_ESP) 36943a2dfbe8SMartin Willi return -EINVAL; 36953a2dfbe8SMartin Willi 36963a2dfbe8SMartin Willi if (!x->encap) 36973a2dfbe8SMartin Willi return -EINVAL; 36983a2dfbe8SMartin Willi 36993a2dfbe8SMartin Willi skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC); 37003a2dfbe8SMartin Willi if (skb == NULL) 37013a2dfbe8SMartin Willi return -ENOMEM; 37023a2dfbe8SMartin Willi 37032fc5f83bSGustavo A. R. Silva err = build_mapping(skb, x, ipaddr, sport); 37042fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 37053a2dfbe8SMartin Willi 370621ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING); 37073a2dfbe8SMartin Willi } 37083a2dfbe8SMartin Willi 37090f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c) 37100f24558eSHoria Geanta { 37110f24558eSHoria Geanta return (bool)xfrm_acquire_is_on(c->net); 37120f24558eSHoria Geanta } 37130f24558eSHoria Geanta 37141da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = { 37151da177e4SLinus Torvalds .notify = xfrm_send_state_notify, 37161da177e4SLinus Torvalds .acquire = xfrm_send_acquire, 37171da177e4SLinus Torvalds .compile_policy = xfrm_compile_policy, 37181da177e4SLinus Torvalds .notify_policy = xfrm_send_policy_notify, 371997a64b45SMasahide NAKAMURA .report = xfrm_send_report, 37205c79de6eSShinta Sugimoto .migrate = xfrm_send_migrate, 37213a2dfbe8SMartin Willi .new_mapping = xfrm_send_mapping, 37220f24558eSHoria Geanta .is_alive = xfrm_is_alive, 37231da177e4SLinus Torvalds }; 37241da177e4SLinus Torvalds 3725a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net) 37261da177e4SLinus Torvalds { 3727be33690dSPatrick McHardy struct sock *nlsk; 3728a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 3729a31f2d17SPablo Neira Ayuso .groups = XFRMNLGRP_MAX, 3730a31f2d17SPablo Neira Ayuso .input = xfrm_netlink_rcv, 3731a31f2d17SPablo Neira Ayuso }; 3732be33690dSPatrick McHardy 37339f00d977SPablo Neira Ayuso nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg); 3734be33690dSPatrick McHardy if (nlsk == NULL) 37351da177e4SLinus Torvalds return -ENOMEM; 3736d79d792eSEric W. Biederman net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */ 3737cf778b00SEric Dumazet rcu_assign_pointer(net->xfrm.nlsk, nlsk); 37381da177e4SLinus Torvalds return 0; 37391da177e4SLinus Torvalds } 37401da177e4SLinus Torvalds 37416218fe18SFlorian Westphal static void __net_exit xfrm_user_net_pre_exit(struct net *net) 37426218fe18SFlorian Westphal { 37436218fe18SFlorian Westphal RCU_INIT_POINTER(net->xfrm.nlsk, NULL); 37446218fe18SFlorian Westphal } 37456218fe18SFlorian Westphal 3746d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list) 3747a6483b79SAlexey Dobriyan { 3748d79d792eSEric W. Biederman struct net *net; 37496218fe18SFlorian Westphal 3750d79d792eSEric W. Biederman list_for_each_entry(net, net_exit_list, exit_list) 3751d79d792eSEric W. Biederman netlink_kernel_release(net->xfrm.nlsk_stash); 3752a6483b79SAlexey Dobriyan } 3753a6483b79SAlexey Dobriyan 3754a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = { 3755a6483b79SAlexey Dobriyan .init = xfrm_user_net_init, 37566218fe18SFlorian Westphal .pre_exit = xfrm_user_net_pre_exit, 3757d79d792eSEric W. Biederman .exit_batch = xfrm_user_net_exit, 3758a6483b79SAlexey Dobriyan }; 3759a6483b79SAlexey Dobriyan 3760a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void) 3761a6483b79SAlexey Dobriyan { 3762a6483b79SAlexey Dobriyan int rv; 3763a6483b79SAlexey Dobriyan 3764a6483b79SAlexey Dobriyan printk(KERN_INFO "Initializing XFRM netlink socket\n"); 3765a6483b79SAlexey Dobriyan 3766a6483b79SAlexey Dobriyan rv = register_pernet_subsys(&xfrm_user_net_ops); 3767a6483b79SAlexey Dobriyan if (rv < 0) 3768a6483b79SAlexey Dobriyan return rv; 3769f41b284aSZhengchao Shao xfrm_register_km(&netlink_mgr); 3770f41b284aSZhengchao Shao return 0; 3771a6483b79SAlexey Dobriyan } 3772a6483b79SAlexey Dobriyan 37731da177e4SLinus Torvalds static void __exit xfrm_user_exit(void) 37741da177e4SLinus Torvalds { 37751da177e4SLinus Torvalds xfrm_unregister_km(&netlink_mgr); 3776a6483b79SAlexey Dobriyan unregister_pernet_subsys(&xfrm_user_net_ops); 37771da177e4SLinus Torvalds } 37781da177e4SLinus Torvalds 37791da177e4SLinus Torvalds module_init(xfrm_user_init); 37801da177e4SLinus Torvalds module_exit(xfrm_user_exit); 37811da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 37824fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); 3783