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, 518643bc1a2SSabrina Dubroca struct nlattr *rp, 519643bc1a2SSabrina Dubroca struct netlink_ext_ack *extack) 520e2b19125SSteffen Klassert { 521e2b19125SSteffen Klassert struct xfrm_replay_state_esn *up; 5225e708e47SAlexey Dobriyan unsigned int ulen; 523e2b19125SSteffen Klassert 524e2b19125SSteffen Klassert if (!replay_esn || !rp) 525e2b19125SSteffen Klassert return 0; 526e2b19125SSteffen Klassert 527e2b19125SSteffen Klassert up = nla_data(rp); 528ecd79187SMathias Krause ulen = xfrm_replay_state_esn_len(up); 529e2b19125SSteffen Klassert 530f843ee6dSAndy Whitcroft /* Check the overall length and the internal bitmap length to avoid 531f843ee6dSAndy Whitcroft * potential overflow. */ 532643bc1a2SSabrina Dubroca if (nla_len(rp) < (int)ulen) { 533643bc1a2SSabrina Dubroca NL_SET_ERR_MSG(extack, "ESN attribute is too short"); 534e2b19125SSteffen Klassert return -EINVAL; 535643bc1a2SSabrina Dubroca } 536e2b19125SSteffen Klassert 537643bc1a2SSabrina Dubroca if (xfrm_replay_state_esn_len(replay_esn) != ulen) { 538643bc1a2SSabrina Dubroca NL_SET_ERR_MSG(extack, "New ESN size doesn't match the existing SA's ESN size"); 539677e806dSAndy Whitcroft return -EINVAL; 540643bc1a2SSabrina Dubroca } 541643bc1a2SSabrina Dubroca 542643bc1a2SSabrina Dubroca if (replay_esn->bmp_len != up->bmp_len) { 543643bc1a2SSabrina Dubroca NL_SET_ERR_MSG(extack, "New ESN bitmap size doesn't match the existing SA's ESN bitmap"); 544643bc1a2SSabrina Dubroca return -EINVAL; 545643bc1a2SSabrina Dubroca } 546643bc1a2SSabrina Dubroca 547643bc1a2SSabrina Dubroca if (up->replay_window > up->bmp_len * sizeof(__u32) * 8) { 548643bc1a2SSabrina Dubroca NL_SET_ERR_MSG(extack, "ESN replay window is longer than the bitmap"); 549643bc1a2SSabrina Dubroca return -EINVAL; 550643bc1a2SSabrina Dubroca } 551677e806dSAndy Whitcroft 552e2b19125SSteffen Klassert return 0; 553e2b19125SSteffen Klassert } 554e2b19125SSteffen Klassert 555d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn, 556d8647b79SSteffen Klassert struct xfrm_replay_state_esn **preplay_esn, 557d8647b79SSteffen Klassert struct nlattr *rta) 558d8647b79SSteffen Klassert { 559d8647b79SSteffen Klassert struct xfrm_replay_state_esn *p, *pp, *up; 5605e708e47SAlexey Dobriyan unsigned int klen, ulen; 561d8647b79SSteffen Klassert 562d8647b79SSteffen Klassert if (!rta) 563d8647b79SSteffen Klassert return 0; 564d8647b79SSteffen Klassert 565d8647b79SSteffen Klassert up = nla_data(rta); 566ecd79187SMathias Krause klen = xfrm_replay_state_esn_len(up); 5675e708e47SAlexey Dobriyan ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up); 568d8647b79SSteffen Klassert 569ecd79187SMathias Krause p = kzalloc(klen, GFP_KERNEL); 570d8647b79SSteffen Klassert if (!p) 571d8647b79SSteffen Klassert return -ENOMEM; 572d8647b79SSteffen Klassert 573ecd79187SMathias Krause pp = kzalloc(klen, GFP_KERNEL); 574d8647b79SSteffen Klassert if (!pp) { 575d8647b79SSteffen Klassert kfree(p); 576d8647b79SSteffen Klassert return -ENOMEM; 577d8647b79SSteffen Klassert } 578d8647b79SSteffen Klassert 579ecd79187SMathias Krause memcpy(p, up, ulen); 580ecd79187SMathias Krause memcpy(pp, up, ulen); 581ecd79187SMathias Krause 582d8647b79SSteffen Klassert *replay_esn = p; 583d8647b79SSteffen Klassert *preplay_esn = pp; 584d8647b79SSteffen Klassert 585d8647b79SSteffen Klassert return 0; 586d8647b79SSteffen Klassert } 587d8647b79SSteffen Klassert 588a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx) 589df71837dSTrent Jaeger { 590a1b831f2SAlexey Dobriyan unsigned int len = 0; 591df71837dSTrent Jaeger 592df71837dSTrent Jaeger if (xfrm_ctx) { 593df71837dSTrent Jaeger len += sizeof(struct xfrm_user_sec_ctx); 594df71837dSTrent Jaeger len += xfrm_ctx->ctx_len; 595df71837dSTrent Jaeger } 596df71837dSTrent Jaeger return len; 597df71837dSTrent Jaeger } 598df71837dSTrent Jaeger 5991da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 6001da177e4SLinus Torvalds { 6011da177e4SLinus Torvalds memcpy(&x->id, &p->id, sizeof(x->id)); 6021da177e4SLinus Torvalds memcpy(&x->sel, &p->sel, sizeof(x->sel)); 6031da177e4SLinus Torvalds memcpy(&x->lft, &p->lft, sizeof(x->lft)); 6041da177e4SLinus Torvalds x->props.mode = p->mode; 60533fce60dSFan Du x->props.replay_window = min_t(unsigned int, p->replay_window, 60633fce60dSFan Du sizeof(x->replay.bitmap) * 8); 6071da177e4SLinus Torvalds x->props.reqid = p->reqid; 6081da177e4SLinus Torvalds x->props.family = p->family; 60954489c14SDavid S. Miller memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr)); 6101da177e4SLinus Torvalds x->props.flags = p->flags; 611196b0036SHerbert Xu 612ccf9b3b8SSteffen Klassert if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC)) 613196b0036SHerbert Xu x->sel.family = p->family; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds 616d51d081dSJamal Hadi Salim /* 617d51d081dSJamal Hadi Salim * someday when pfkey also has support, we could have the code 618d51d081dSJamal Hadi Salim * somehow made shareable and move it to xfrm_state.c - JHS 619d51d081dSJamal Hadi Salim * 620d51d081dSJamal Hadi Salim */ 621e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs, 622e3ac104dSMathias Krause int update_esn) 623d51d081dSJamal Hadi Salim { 6245424f32eSThomas Graf struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 625e3ac104dSMathias Krause struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL; 6265424f32eSThomas Graf struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 6275424f32eSThomas Graf struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; 6285424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; 6294e484b3eSAntony Antony struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH]; 630d51d081dSJamal Hadi Salim 631d8647b79SSteffen Klassert if (re) { 632d8647b79SSteffen Klassert struct xfrm_replay_state_esn *replay_esn; 633d8647b79SSteffen Klassert replay_esn = nla_data(re); 634d8647b79SSteffen Klassert memcpy(x->replay_esn, replay_esn, 635d8647b79SSteffen Klassert xfrm_replay_state_esn_len(replay_esn)); 636d8647b79SSteffen Klassert memcpy(x->preplay_esn, replay_esn, 637d8647b79SSteffen Klassert xfrm_replay_state_esn_len(replay_esn)); 638d8647b79SSteffen Klassert } 639d8647b79SSteffen Klassert 640d51d081dSJamal Hadi Salim if (rp) { 641d51d081dSJamal Hadi Salim struct xfrm_replay_state *replay; 6425424f32eSThomas Graf replay = nla_data(rp); 643d51d081dSJamal Hadi Salim memcpy(&x->replay, replay, sizeof(*replay)); 644d51d081dSJamal Hadi Salim memcpy(&x->preplay, replay, sizeof(*replay)); 645d51d081dSJamal Hadi Salim } 646d51d081dSJamal Hadi Salim 647d51d081dSJamal Hadi Salim if (lt) { 648d51d081dSJamal Hadi Salim struct xfrm_lifetime_cur *ltime; 6495424f32eSThomas Graf ltime = nla_data(lt); 650d51d081dSJamal Hadi Salim x->curlft.bytes = ltime->bytes; 651d51d081dSJamal Hadi Salim x->curlft.packets = ltime->packets; 652d51d081dSJamal Hadi Salim x->curlft.add_time = ltime->add_time; 653d51d081dSJamal Hadi Salim x->curlft.use_time = ltime->use_time; 654d51d081dSJamal Hadi Salim } 655d51d081dSJamal Hadi Salim 656cf5cb79fSThomas Graf if (et) 6575424f32eSThomas Graf x->replay_maxage = nla_get_u32(et); 658d51d081dSJamal Hadi Salim 659cf5cb79fSThomas Graf if (rt) 6605424f32eSThomas Graf x->replay_maxdiff = nla_get_u32(rt); 6614e484b3eSAntony Antony 6624e484b3eSAntony Antony if (mt) 6634e484b3eSAntony Antony x->mapping_maxage = nla_get_u32(mt); 664d51d081dSJamal Hadi Salim } 665d51d081dSJamal Hadi Salim 6669b42c1f1SSteffen Klassert static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m) 6679b42c1f1SSteffen Klassert { 6689b42c1f1SSteffen Klassert if (attrs[XFRMA_SET_MARK]) { 6699b42c1f1SSteffen Klassert m->v = nla_get_u32(attrs[XFRMA_SET_MARK]); 6709b42c1f1SSteffen Klassert if (attrs[XFRMA_SET_MARK_MASK]) 6719b42c1f1SSteffen Klassert m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]); 6729b42c1f1SSteffen Klassert else 6739b42c1f1SSteffen Klassert m->m = 0xffffffff; 6749b42c1f1SSteffen Klassert } else { 6759b42c1f1SSteffen Klassert m->v = m->m = 0; 6769b42c1f1SSteffen Klassert } 6779b42c1f1SSteffen Klassert } 6789b42c1f1SSteffen Klassert 679fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net, 680fc34acd3SAlexey Dobriyan struct xfrm_usersa_info *p, 6815424f32eSThomas Graf struct nlattr **attrs, 682adb5c33eSSabrina Dubroca int *errp, 683adb5c33eSSabrina Dubroca struct netlink_ext_ack *extack) 6841da177e4SLinus Torvalds { 685fc34acd3SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 6861da177e4SLinus Torvalds int err = -ENOMEM; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds if (!x) 6891da177e4SLinus Torvalds goto error_no_put; 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds copy_from_user_state(x, p); 6921da177e4SLinus Torvalds 6936fd06963SSteffen Klassert if (attrs[XFRMA_ENCAP]) { 6946fd06963SSteffen Klassert x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), 6956fd06963SSteffen Klassert sizeof(*x->encap), GFP_KERNEL); 6966fd06963SSteffen Klassert if (x->encap == NULL) 6976fd06963SSteffen Klassert goto error; 6986fd06963SSteffen Klassert } 6996fd06963SSteffen Klassert 7006fd06963SSteffen Klassert if (attrs[XFRMA_COADDR]) { 7016fd06963SSteffen Klassert x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]), 7026fd06963SSteffen Klassert sizeof(*x->coaddr), GFP_KERNEL); 7036fd06963SSteffen Klassert if (x->coaddr == NULL) 7046fd06963SSteffen Klassert goto error; 7056fd06963SSteffen Klassert } 7066fd06963SSteffen Klassert 707a947b0a9SNicolas Dichtel if (attrs[XFRMA_SA_EXTRA_FLAGS]) 708a947b0a9SNicolas Dichtel x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]); 709a947b0a9SNicolas Dichtel 7102b916826SSabrina Dubroca if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD], extack))) 7111a6509d9SHerbert Xu goto error; 7124447bb33SMartin Willi if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo, 7132b916826SSabrina Dubroca attrs[XFRMA_ALG_AUTH_TRUNC], extack))) 7144447bb33SMartin Willi goto error; 7154447bb33SMartin Willi if (!x->props.aalgo) { 7164447bb33SMartin Willi if ((err = attach_auth(&x->aalg, &x->props.aalgo, 7172b916826SSabrina Dubroca attrs[XFRMA_ALG_AUTH], extack))) 7181da177e4SLinus Torvalds goto error; 7194447bb33SMartin Willi } 7202b916826SSabrina Dubroca if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT], extack))) 7211da177e4SLinus Torvalds goto error; 7221da177e4SLinus Torvalds if ((err = attach_one_algo(&x->calg, &x->props.calgo, 7231da177e4SLinus Torvalds xfrm_calg_get_byname, 7242b916826SSabrina Dubroca attrs[XFRMA_ALG_COMP], extack))) 7251da177e4SLinus Torvalds goto error; 726fd21150aSThomas Graf 72735d2856bSMartin Willi if (attrs[XFRMA_TFCPAD]) 72835d2856bSMartin Willi x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]); 72935d2856bSMartin Willi 7306f26b61eSJamal Hadi Salim xfrm_mark_get(attrs, &x->mark); 7316f26b61eSJamal Hadi Salim 7329b42c1f1SSteffen Klassert xfrm_smark_init(attrs, &x->props.smark); 733077fbac4SLorenzo Colitti 734a3d9001bSKai Lueke if (attrs[XFRMA_IF_ID]) 7357e652640SSteffen Klassert x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 736fd21150aSThomas Graf 737741f9a10SSabrina Dubroca err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV], extack); 7381da177e4SLinus Torvalds if (err) 7391da177e4SLinus Torvalds goto error; 7401da177e4SLinus Torvalds 7412f30ea50SMathias Krause if (attrs[XFRMA_SEC_CTX]) { 7422f30ea50SMathias Krause err = security_xfrm_state_alloc(x, 7432f30ea50SMathias Krause nla_data(attrs[XFRMA_SEC_CTX])); 7442f30ea50SMathias Krause if (err) 745df71837dSTrent Jaeger goto error; 7462f30ea50SMathias Krause } 747df71837dSTrent Jaeger 748d8647b79SSteffen Klassert if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn, 749d8647b79SSteffen Klassert attrs[XFRMA_REPLAY_ESN_VAL]))) 750d8647b79SSteffen Klassert goto error; 751d8647b79SSteffen Klassert 7521da177e4SLinus Torvalds x->km.seq = p->seq; 753b27aeadbSAlexey Dobriyan x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth; 754d51d081dSJamal Hadi Salim /* sysctl_xfrm_aevent_etime is in 100ms units */ 755b27aeadbSAlexey Dobriyan x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M; 756d51d081dSJamal Hadi Salim 7571cf9a3aeSSabrina Dubroca if ((err = xfrm_init_replay(x, extack))) 7589fdc4883SSteffen Klassert goto error; 759d51d081dSJamal Hadi Salim 7609fdc4883SSteffen Klassert /* override default values from above */ 761e3ac104dSMathias Krause xfrm_update_ae_params(x, attrs, 0); 7621da177e4SLinus Torvalds 763cc01572eSYossi Kuperman /* configure the hardware if offload is requested */ 764cc01572eSYossi Kuperman if (attrs[XFRMA_OFFLOAD_DEV]) { 765cc01572eSYossi Kuperman err = xfrm_dev_state_add(net, x, 766adb5c33eSSabrina Dubroca nla_data(attrs[XFRMA_OFFLOAD_DEV]), 767adb5c33eSSabrina Dubroca extack); 768cc01572eSYossi Kuperman if (err) 769cc01572eSYossi Kuperman goto error; 770cc01572eSYossi Kuperman } 771cc01572eSYossi Kuperman 7721da177e4SLinus Torvalds return x; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds error: 7751da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 7761da177e4SLinus Torvalds xfrm_state_put(x); 7771da177e4SLinus Torvalds error_no_put: 7781da177e4SLinus Torvalds *errp = err; 7791da177e4SLinus Torvalds return NULL; 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 78222e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 7833bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 7841da177e4SLinus Torvalds { 785fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 7867b67c857SThomas Graf struct xfrm_usersa_info *p = nlmsg_data(nlh); 7871da177e4SLinus Torvalds struct xfrm_state *x; 7881da177e4SLinus Torvalds int err; 78926b15dadSJamal Hadi Salim struct km_event c; 7901da177e4SLinus Torvalds 7916999aae1SSabrina Dubroca err = verify_newsa_info(p, attrs, extack); 7921da177e4SLinus Torvalds if (err) 7931da177e4SLinus Torvalds return err; 7941da177e4SLinus Torvalds 795adb5c33eSSabrina Dubroca x = xfrm_state_construct(net, p, attrs, &err, extack); 7961da177e4SLinus Torvalds if (!x) 7971da177e4SLinus Torvalds return err; 7981da177e4SLinus Torvalds 79926b15dadSJamal Hadi Salim xfrm_state_hold(x); 8001da177e4SLinus Torvalds if (nlh->nlmsg_type == XFRM_MSG_NEWSA) 8011da177e4SLinus Torvalds err = xfrm_state_add(x); 8021da177e4SLinus Torvalds else 8031da177e4SLinus Torvalds err = xfrm_state_update(x); 8041da177e4SLinus Torvalds 8052e71029eSTetsuo Handa xfrm_audit_state_add(x, err ? 0 : 1, true); 806161a09e7SJoy Latten 8071da177e4SLinus Torvalds if (err < 0) { 8081da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 809c5d4d7d8SSteffen Klassert xfrm_dev_state_delete(x); 81021380b81SHerbert Xu __xfrm_state_put(x); 8117d6dfe1fSPatrick McHardy goto out; 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 814cc01572eSYossi Kuperman if (x->km.state == XFRM_STATE_VOID) 815cc01572eSYossi Kuperman x->km.state = XFRM_STATE_VALID; 816cc01572eSYossi Kuperman 81726b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 81815e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 819f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 82026b15dadSJamal Hadi Salim 82126b15dadSJamal Hadi Salim km_state_notify(x, &c); 8227d6dfe1fSPatrick McHardy out: 82326b15dadSJamal Hadi Salim xfrm_state_put(x); 8241da177e4SLinus Torvalds return err; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 827fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net, 828fc34acd3SAlexey Dobriyan struct xfrm_usersa_id *p, 8295424f32eSThomas Graf struct nlattr **attrs, 830eb2971b6SMasahide NAKAMURA int *errp) 831eb2971b6SMasahide NAKAMURA { 832eb2971b6SMasahide NAKAMURA struct xfrm_state *x = NULL; 8336f26b61eSJamal Hadi Salim struct xfrm_mark m; 834eb2971b6SMasahide NAKAMURA int err; 8356f26b61eSJamal Hadi Salim u32 mark = xfrm_mark_get(attrs, &m); 836eb2971b6SMasahide NAKAMURA 837eb2971b6SMasahide NAKAMURA if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { 838eb2971b6SMasahide NAKAMURA err = -ESRCH; 8396f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family); 840eb2971b6SMasahide NAKAMURA } else { 841eb2971b6SMasahide NAKAMURA xfrm_address_t *saddr = NULL; 842eb2971b6SMasahide NAKAMURA 84335a7aa08SThomas Graf verify_one_addr(attrs, XFRMA_SRCADDR, &saddr); 844eb2971b6SMasahide NAKAMURA if (!saddr) { 845eb2971b6SMasahide NAKAMURA err = -EINVAL; 846eb2971b6SMasahide NAKAMURA goto out; 847eb2971b6SMasahide NAKAMURA } 848eb2971b6SMasahide NAKAMURA 8499abbffeeSMasahide NAKAMURA err = -ESRCH; 8506f26b61eSJamal Hadi Salim x = xfrm_state_lookup_byaddr(net, mark, 8516f26b61eSJamal Hadi Salim &p->daddr, saddr, 852221df1edSAlexey Dobriyan p->proto, p->family); 853eb2971b6SMasahide NAKAMURA } 854eb2971b6SMasahide NAKAMURA 855eb2971b6SMasahide NAKAMURA out: 856eb2971b6SMasahide NAKAMURA if (!x && errp) 857eb2971b6SMasahide NAKAMURA *errp = err; 858eb2971b6SMasahide NAKAMURA return x; 859eb2971b6SMasahide NAKAMURA } 860eb2971b6SMasahide NAKAMURA 86122e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 8623bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 8631da177e4SLinus Torvalds { 864fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 8651da177e4SLinus Torvalds struct xfrm_state *x; 866eb2971b6SMasahide NAKAMURA int err = -ESRCH; 86726b15dadSJamal Hadi Salim struct km_event c; 8687b67c857SThomas Graf struct xfrm_usersa_id *p = nlmsg_data(nlh); 8691da177e4SLinus Torvalds 870fc34acd3SAlexey Dobriyan x = xfrm_user_state_lookup(net, p, attrs, &err); 8711da177e4SLinus Torvalds if (x == NULL) 872eb2971b6SMasahide NAKAMURA return err; 8731da177e4SLinus Torvalds 8746f68dc37SDavid S. Miller if ((err = security_xfrm_state_delete(x)) != 0) 875c8c05a8eSCatherine Zhang goto out; 876c8c05a8eSCatherine Zhang 8771da177e4SLinus Torvalds if (xfrm_state_kern(x)) { 878880e475dSSabrina Dubroca NL_SET_ERR_MSG(extack, "SA is in use by tunnels"); 879c8c05a8eSCatherine Zhang err = -EPERM; 880c8c05a8eSCatherine Zhang goto out; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 88326b15dadSJamal Hadi Salim err = xfrm_state_delete(x); 884c8c05a8eSCatherine Zhang if (err < 0) 885c8c05a8eSCatherine Zhang goto out; 88626b15dadSJamal Hadi Salim 88726b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 88815e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 889f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 89026b15dadSJamal Hadi Salim km_state_notify(x, &c); 8911da177e4SLinus Torvalds 892c8c05a8eSCatherine Zhang out: 8932e71029eSTetsuo Handa xfrm_audit_state_delete(x, err ? 0 : 1, true); 894c8c05a8eSCatherine Zhang xfrm_state_put(x); 89526b15dadSJamal Hadi Salim return err; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 8991da177e4SLinus Torvalds { 900f778a636SMathias Krause memset(p, 0, sizeof(*p)); 9011da177e4SLinus Torvalds memcpy(&p->id, &x->id, sizeof(p->id)); 9021da177e4SLinus Torvalds memcpy(&p->sel, &x->sel, sizeof(p->sel)); 9031da177e4SLinus Torvalds memcpy(&p->lft, &x->lft, sizeof(p->lft)); 9041da177e4SLinus Torvalds memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); 905e33d4f13SSowmini Varadhan put_unaligned(x->stats.replay_window, &p->stats.replay_window); 906e33d4f13SSowmini Varadhan put_unaligned(x->stats.replay, &p->stats.replay); 907e33d4f13SSowmini Varadhan put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed); 90854489c14SDavid S. Miller memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); 9091da177e4SLinus Torvalds p->mode = x->props.mode; 9101da177e4SLinus Torvalds p->replay_window = x->props.replay_window; 9111da177e4SLinus Torvalds p->reqid = x->props.reqid; 9121da177e4SLinus Torvalds p->family = x->props.family; 9131da177e4SLinus Torvalds p->flags = x->props.flags; 9141da177e4SLinus Torvalds p->seq = x->km.seq; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds struct xfrm_dump_info { 9181da177e4SLinus Torvalds struct sk_buff *in_skb; 9191da177e4SLinus Torvalds struct sk_buff *out_skb; 9201da177e4SLinus Torvalds u32 nlmsg_seq; 9211da177e4SLinus Torvalds u16 nlmsg_flags; 9221da177e4SLinus Torvalds }; 9231da177e4SLinus Torvalds 924c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) 925c0144beaSThomas Graf { 926c0144beaSThomas Graf struct xfrm_user_sec_ctx *uctx; 927c0144beaSThomas Graf struct nlattr *attr; 92868325d3bSHerbert Xu int ctx_size = sizeof(*uctx) + s->ctx_len; 929c0144beaSThomas Graf 930c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size); 931c0144beaSThomas Graf if (attr == NULL) 932c0144beaSThomas Graf return -EMSGSIZE; 933c0144beaSThomas Graf 934c0144beaSThomas Graf uctx = nla_data(attr); 935c0144beaSThomas Graf uctx->exttype = XFRMA_SEC_CTX; 936c0144beaSThomas Graf uctx->len = ctx_size; 937c0144beaSThomas Graf uctx->ctx_doi = s->ctx_doi; 938c0144beaSThomas Graf uctx->ctx_alg = s->ctx_alg; 939c0144beaSThomas Graf uctx->ctx_len = s->ctx_len; 940c0144beaSThomas Graf memcpy(uctx + 1, s->ctx_str, s->ctx_len); 941c0144beaSThomas Graf 942c0144beaSThomas Graf return 0; 943c0144beaSThomas Graf } 944c0144beaSThomas Graf 94587e0a94eSLeon Romanovsky static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb) 946d77e38e6SSteffen Klassert { 947d77e38e6SSteffen Klassert struct xfrm_user_offload *xuo; 948d77e38e6SSteffen Klassert struct nlattr *attr; 949d77e38e6SSteffen Klassert 950d77e38e6SSteffen Klassert attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo)); 951d77e38e6SSteffen Klassert if (attr == NULL) 952d77e38e6SSteffen Klassert return -EMSGSIZE; 953d77e38e6SSteffen Klassert 954d77e38e6SSteffen Klassert xuo = nla_data(attr); 9555fe0d4bdSMathias Krause memset(xuo, 0, sizeof(*xuo)); 956d77e38e6SSteffen Klassert xuo->ifindex = xso->dev->ifindex; 957482db2f1SLeon Romanovsky if (xso->dir == XFRM_DEV_OFFLOAD_IN) 958482db2f1SLeon Romanovsky xuo->flags = XFRM_OFFLOAD_INBOUND; 959d14f28b8SLeon Romanovsky if (xso->type == XFRM_DEV_OFFLOAD_PACKET) 960d14f28b8SLeon Romanovsky xuo->flags |= XFRM_OFFLOAD_PACKET; 961d77e38e6SSteffen Klassert 962d77e38e6SSteffen Klassert return 0; 963d77e38e6SSteffen Klassert } 964d77e38e6SSteffen Klassert 965c7a5899eSAntony Antony static bool xfrm_redact(void) 966c7a5899eSAntony Antony { 967c7a5899eSAntony Antony return IS_ENABLED(CONFIG_SECURITY) && 968c7a5899eSAntony Antony security_locked_down(LOCKDOWN_XFRM_SECRET); 969c7a5899eSAntony Antony } 970c7a5899eSAntony Antony 9714447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb) 9724447bb33SMartin Willi { 9734447bb33SMartin Willi struct xfrm_algo *algo; 974c7a5899eSAntony Antony struct xfrm_algo_auth *ap; 9754447bb33SMartin Willi struct nlattr *nla; 976c7a5899eSAntony Antony bool redact_secret = xfrm_redact(); 9774447bb33SMartin Willi 9784447bb33SMartin Willi nla = nla_reserve(skb, XFRMA_ALG_AUTH, 9794447bb33SMartin Willi sizeof(*algo) + (auth->alg_key_len + 7) / 8); 9804447bb33SMartin Willi if (!nla) 9814447bb33SMartin Willi return -EMSGSIZE; 9824447bb33SMartin Willi algo = nla_data(nla); 9834c87308bSMathias Krause strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name)); 984c7a5899eSAntony Antony 985c7a5899eSAntony Antony if (redact_secret && auth->alg_key_len) 986c7a5899eSAntony Antony memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8); 987c7a5899eSAntony Antony else 988c7a5899eSAntony Antony memcpy(algo->alg_key, auth->alg_key, 989c7a5899eSAntony Antony (auth->alg_key_len + 7) / 8); 9904447bb33SMartin Willi algo->alg_key_len = auth->alg_key_len; 9914447bb33SMartin Willi 992c7a5899eSAntony Antony nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth)); 993c7a5899eSAntony Antony if (!nla) 994c7a5899eSAntony Antony return -EMSGSIZE; 995c7a5899eSAntony Antony ap = nla_data(nla); 996c7a5899eSAntony Antony memcpy(ap, auth, sizeof(struct xfrm_algo_auth)); 997c7a5899eSAntony Antony if (redact_secret && auth->alg_key_len) 998c7a5899eSAntony Antony memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8); 999c7a5899eSAntony Antony else 1000c7a5899eSAntony Antony memcpy(ap->alg_key, auth->alg_key, 1001c7a5899eSAntony Antony (auth->alg_key_len + 7) / 8); 1002c7a5899eSAntony Antony return 0; 1003c7a5899eSAntony Antony } 1004c7a5899eSAntony Antony 1005c7a5899eSAntony Antony static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb) 1006c7a5899eSAntony Antony { 1007c7a5899eSAntony Antony struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead)); 1008c7a5899eSAntony Antony struct xfrm_algo_aead *ap; 1009c7a5899eSAntony Antony bool redact_secret = xfrm_redact(); 1010c7a5899eSAntony Antony 1011c7a5899eSAntony Antony if (!nla) 1012c7a5899eSAntony Antony return -EMSGSIZE; 1013c7a5899eSAntony Antony 1014c7a5899eSAntony Antony ap = nla_data(nla); 1015*8222d591SHerbert Xu strscpy_pad(ap->alg_name, aead->alg_name, sizeof(ap->alg_name)); 1016*8222d591SHerbert Xu ap->alg_key_len = aead->alg_key_len; 1017*8222d591SHerbert Xu ap->alg_icv_len = aead->alg_icv_len; 1018c7a5899eSAntony Antony 1019c7a5899eSAntony Antony if (redact_secret && aead->alg_key_len) 1020c7a5899eSAntony Antony memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8); 1021c7a5899eSAntony Antony else 1022c7a5899eSAntony Antony memcpy(ap->alg_key, aead->alg_key, 1023c7a5899eSAntony Antony (aead->alg_key_len + 7) / 8); 1024c7a5899eSAntony Antony return 0; 1025c7a5899eSAntony Antony } 1026c7a5899eSAntony Antony 1027c7a5899eSAntony Antony static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb) 1028c7a5899eSAntony Antony { 1029c7a5899eSAntony Antony struct xfrm_algo *ap; 1030c7a5899eSAntony Antony bool redact_secret = xfrm_redact(); 1031c7a5899eSAntony Antony struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT, 1032c7a5899eSAntony Antony xfrm_alg_len(ealg)); 1033c7a5899eSAntony Antony if (!nla) 1034c7a5899eSAntony Antony return -EMSGSIZE; 1035c7a5899eSAntony Antony 1036c7a5899eSAntony Antony ap = nla_data(nla); 1037*8222d591SHerbert Xu strscpy_pad(ap->alg_name, ealg->alg_name, sizeof(ap->alg_name)); 1038*8222d591SHerbert Xu ap->alg_key_len = ealg->alg_key_len; 1039c7a5899eSAntony Antony 1040c7a5899eSAntony Antony if (redact_secret && ealg->alg_key_len) 1041c7a5899eSAntony Antony memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8); 1042c7a5899eSAntony Antony else 1043c7a5899eSAntony Antony memcpy(ap->alg_key, ealg->alg_key, 1044c7a5899eSAntony Antony (ealg->alg_key_len + 7) / 8); 1045c7a5899eSAntony Antony 10464447bb33SMartin Willi return 0; 10474447bb33SMartin Willi } 10484447bb33SMartin Willi 1049*8222d591SHerbert Xu static int copy_to_user_calg(struct xfrm_algo *calg, struct sk_buff *skb) 1050*8222d591SHerbert Xu { 1051*8222d591SHerbert Xu struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_COMP, sizeof(*calg)); 1052*8222d591SHerbert Xu struct xfrm_algo *ap; 1053*8222d591SHerbert Xu 1054*8222d591SHerbert Xu if (!nla) 1055*8222d591SHerbert Xu return -EMSGSIZE; 1056*8222d591SHerbert Xu 1057*8222d591SHerbert Xu ap = nla_data(nla); 1058*8222d591SHerbert Xu strscpy_pad(ap->alg_name, calg->alg_name, sizeof(ap->alg_name)); 1059*8222d591SHerbert Xu ap->alg_key_len = 0; 1060*8222d591SHerbert Xu 1061*8222d591SHerbert Xu return 0; 1062*8222d591SHerbert Xu } 1063*8222d591SHerbert Xu 1064*8222d591SHerbert Xu static int copy_to_user_encap(struct xfrm_encap_tmpl *ep, struct sk_buff *skb) 1065*8222d591SHerbert Xu { 1066*8222d591SHerbert Xu struct nlattr *nla = nla_reserve(skb, XFRMA_ENCAP, sizeof(*ep)); 1067*8222d591SHerbert Xu struct xfrm_encap_tmpl *uep; 1068*8222d591SHerbert Xu 1069*8222d591SHerbert Xu if (!nla) 1070*8222d591SHerbert Xu return -EMSGSIZE; 1071*8222d591SHerbert Xu 1072*8222d591SHerbert Xu uep = nla_data(nla); 1073*8222d591SHerbert Xu memset(uep, 0, sizeof(*uep)); 1074*8222d591SHerbert Xu 1075*8222d591SHerbert Xu uep->encap_type = ep->encap_type; 1076*8222d591SHerbert Xu uep->encap_sport = ep->encap_sport; 1077*8222d591SHerbert Xu uep->encap_dport = ep->encap_dport; 1078*8222d591SHerbert Xu uep->encap_oa = ep->encap_oa; 1079*8222d591SHerbert Xu 1080*8222d591SHerbert Xu return 0; 1081*8222d591SHerbert Xu } 1082*8222d591SHerbert Xu 10839b42c1f1SSteffen Klassert static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m) 10849b42c1f1SSteffen Klassert { 10859b42c1f1SSteffen Klassert int ret = 0; 10869b42c1f1SSteffen Klassert 10879b42c1f1SSteffen Klassert if (m->v | m->m) { 10889b42c1f1SSteffen Klassert ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v); 10899b42c1f1SSteffen Klassert if (!ret) 10909b42c1f1SSteffen Klassert ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m); 10919b42c1f1SSteffen Klassert } 10929b42c1f1SSteffen Klassert return ret; 10939b42c1f1SSteffen Klassert } 10949b42c1f1SSteffen Klassert 109568325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */ 109668325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x, 109768325d3bSHerbert Xu struct xfrm_usersa_info *p, 109868325d3bSHerbert Xu struct sk_buff *skb) 10991da177e4SLinus Torvalds { 11001d1e34ddSDavid S. Miller int ret = 0; 11011d1e34ddSDavid S. Miller 11021da177e4SLinus Torvalds copy_to_user_state(x, p); 11031da177e4SLinus Torvalds 1104a947b0a9SNicolas Dichtel if (x->props.extra_flags) { 1105a947b0a9SNicolas Dichtel ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS, 1106a947b0a9SNicolas Dichtel x->props.extra_flags); 1107a947b0a9SNicolas Dichtel if (ret) 1108a947b0a9SNicolas Dichtel goto out; 1109a947b0a9SNicolas Dichtel } 1110a947b0a9SNicolas Dichtel 11111d1e34ddSDavid S. Miller if (x->coaddr) { 11121d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); 11131d1e34ddSDavid S. Miller if (ret) 11141d1e34ddSDavid S. Miller goto out; 11151d1e34ddSDavid S. Miller } 11161d1e34ddSDavid S. Miller if (x->lastused) { 1117de95c4a4SNicolas Dichtel ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused, 1118de95c4a4SNicolas Dichtel XFRMA_PAD); 11191d1e34ddSDavid S. Miller if (ret) 11201d1e34ddSDavid S. Miller goto out; 11211d1e34ddSDavid S. Miller } 11221d1e34ddSDavid S. Miller if (x->aead) { 1123c7a5899eSAntony Antony ret = copy_to_user_aead(x->aead, skb); 11241d1e34ddSDavid S. Miller if (ret) 11251d1e34ddSDavid S. Miller goto out; 11261d1e34ddSDavid S. Miller } 11271d1e34ddSDavid S. Miller if (x->aalg) { 11281d1e34ddSDavid S. Miller ret = copy_to_user_auth(x->aalg, skb); 11291d1e34ddSDavid S. Miller if (ret) 11301d1e34ddSDavid S. Miller goto out; 11311d1e34ddSDavid S. Miller } 11321d1e34ddSDavid S. Miller if (x->ealg) { 1133c7a5899eSAntony Antony ret = copy_to_user_ealg(x->ealg, skb); 11341d1e34ddSDavid S. Miller if (ret) 11351d1e34ddSDavid S. Miller goto out; 11361d1e34ddSDavid S. Miller } 11371d1e34ddSDavid S. Miller if (x->calg) { 1138*8222d591SHerbert Xu ret = copy_to_user_calg(x->calg, skb); 11391d1e34ddSDavid S. Miller if (ret) 11401d1e34ddSDavid S. Miller goto out; 11411d1e34ddSDavid S. Miller } 11421d1e34ddSDavid S. Miller if (x->encap) { 1143*8222d591SHerbert Xu ret = copy_to_user_encap(x->encap, skb); 11441d1e34ddSDavid S. Miller if (ret) 11451d1e34ddSDavid S. Miller goto out; 11461d1e34ddSDavid S. Miller } 11471d1e34ddSDavid S. Miller if (x->tfcpad) { 11481d1e34ddSDavid S. Miller ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad); 11491d1e34ddSDavid S. Miller if (ret) 11501d1e34ddSDavid S. Miller goto out; 11511d1e34ddSDavid S. Miller } 11521d1e34ddSDavid S. Miller ret = xfrm_mark_put(skb, &x->mark); 11531d1e34ddSDavid S. Miller if (ret) 11541d1e34ddSDavid S. Miller goto out; 11559b42c1f1SSteffen Klassert 11569b42c1f1SSteffen Klassert ret = xfrm_smark_put(skb, &x->props.smark); 11579b42c1f1SSteffen Klassert if (ret) 11589b42c1f1SSteffen Klassert goto out; 11599b42c1f1SSteffen Klassert 1160f293a5e3Sdingzhi if (x->replay_esn) 11611d1e34ddSDavid S. Miller ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 1162d0fde795SDavid S. Miller xfrm_replay_state_esn_len(x->replay_esn), 11631d1e34ddSDavid S. Miller x->replay_esn); 1164f293a5e3Sdingzhi else 1165f293a5e3Sdingzhi ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 1166f293a5e3Sdingzhi &x->replay); 11671d1e34ddSDavid S. Miller if (ret) 11681d1e34ddSDavid S. Miller goto out; 1169d77e38e6SSteffen Klassert if(x->xso.dev) 1170d77e38e6SSteffen Klassert ret = copy_user_offload(&x->xso, skb); 1171d77e38e6SSteffen Klassert if (ret) 1172d77e38e6SSteffen Klassert goto out; 11737e652640SSteffen Klassert if (x->if_id) { 11747e652640SSteffen Klassert ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id); 1175077fbac4SLorenzo Colitti if (ret) 1176077fbac4SLorenzo Colitti goto out; 1177077fbac4SLorenzo Colitti } 11784e484b3eSAntony Antony if (x->security) { 11798598112dSSteffen Klassert ret = copy_sec_ctx(x->security, skb); 11804e484b3eSAntony Antony if (ret) 11814e484b3eSAntony Antony goto out; 11824e484b3eSAntony Antony } 11834e484b3eSAntony Antony if (x->mapping_maxage) 11844e484b3eSAntony Antony ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage); 11851d1e34ddSDavid S. Miller out: 11861d1e34ddSDavid S. Miller return ret; 118768325d3bSHerbert Xu } 118868325d3bSHerbert Xu 118968325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr) 119068325d3bSHerbert Xu { 119168325d3bSHerbert Xu struct xfrm_dump_info *sp = ptr; 119268325d3bSHerbert Xu struct sk_buff *in_skb = sp->in_skb; 119368325d3bSHerbert Xu struct sk_buff *skb = sp->out_skb; 11945f3eea6bSDmitry Safonov struct xfrm_translator *xtr; 119568325d3bSHerbert Xu struct xfrm_usersa_info *p; 119668325d3bSHerbert Xu struct nlmsghdr *nlh; 119768325d3bSHerbert Xu int err; 119868325d3bSHerbert Xu 119915e47304SEric W. Biederman nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq, 120068325d3bSHerbert Xu XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags); 120168325d3bSHerbert Xu if (nlh == NULL) 120268325d3bSHerbert Xu return -EMSGSIZE; 120368325d3bSHerbert Xu 120468325d3bSHerbert Xu p = nlmsg_data(nlh); 120568325d3bSHerbert Xu 120668325d3bSHerbert Xu err = copy_to_user_state_extra(x, p, skb); 12071d1e34ddSDavid S. Miller if (err) { 12089825069dSThomas Graf nlmsg_cancel(skb, nlh); 120968325d3bSHerbert Xu return err; 12101da177e4SLinus Torvalds } 12111d1e34ddSDavid S. Miller nlmsg_end(skb, nlh); 12125f3eea6bSDmitry Safonov 12135f3eea6bSDmitry Safonov xtr = xfrm_get_translator(); 12145f3eea6bSDmitry Safonov if (xtr) { 12155f3eea6bSDmitry Safonov err = xtr->alloc_compat(skb, nlh); 12165f3eea6bSDmitry Safonov 12175f3eea6bSDmitry Safonov xfrm_put_translator(xtr); 12185f3eea6bSDmitry Safonov if (err) { 12195f3eea6bSDmitry Safonov nlmsg_cancel(skb, nlh); 12205f3eea6bSDmitry Safonov return err; 12215f3eea6bSDmitry Safonov } 12225f3eea6bSDmitry Safonov } 12235f3eea6bSDmitry Safonov 12241d1e34ddSDavid S. Miller return 0; 12251d1e34ddSDavid S. Miller } 12261da177e4SLinus Torvalds 12274c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb) 12284c563f76STimo Teras { 12294c563f76STimo Teras struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 1230283bc9f3SFan Du struct sock *sk = cb->skb->sk; 1231283bc9f3SFan Du struct net *net = sock_net(sk); 1232283bc9f3SFan Du 12331ba5bf99SVegard Nossum if (cb->args[0]) 1234283bc9f3SFan Du xfrm_state_walk_done(walk, net); 12354c563f76STimo Teras return 0; 12364c563f76STimo Teras } 12374c563f76STimo Teras 12381da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) 12391da177e4SLinus Torvalds { 1240fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 12414c563f76STimo Teras struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 12421da177e4SLinus Torvalds struct xfrm_dump_info info; 12431da177e4SLinus Torvalds 12444c563f76STimo Teras BUILD_BUG_ON(sizeof(struct xfrm_state_walk) > 12454c563f76STimo Teras sizeof(cb->args) - sizeof(cb->args[0])); 12464c563f76STimo Teras 12471da177e4SLinus Torvalds info.in_skb = cb->skb; 12481da177e4SLinus Torvalds info.out_skb = skb; 12491da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 12501da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 12514c563f76STimo Teras 12524c563f76STimo Teras if (!cb->args[0]) { 1253d3623099SNicolas Dichtel struct nlattr *attrs[XFRMA_MAX+1]; 1254870a2df4SNicolas Dichtel struct xfrm_address_filter *filter = NULL; 1255d3623099SNicolas Dichtel u8 proto = 0; 1256d3623099SNicolas Dichtel int err; 1257d3623099SNicolas Dichtel 12588cb08174SJohannes Berg err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX, 12598cb08174SJohannes Berg xfrma_policy, cb->extack); 1260d3623099SNicolas Dichtel if (err < 0) 1261d3623099SNicolas Dichtel return err; 1262d3623099SNicolas Dichtel 1263870a2df4SNicolas Dichtel if (attrs[XFRMA_ADDRESS_FILTER]) { 1264df367561SAndrzej Hajda filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]), 1265df367561SAndrzej Hajda sizeof(*filter), GFP_KERNEL); 1266d3623099SNicolas Dichtel if (filter == NULL) 1267d3623099SNicolas Dichtel return -ENOMEM; 1268d3623099SNicolas Dichtel } 1269d3623099SNicolas Dichtel 1270d3623099SNicolas Dichtel if (attrs[XFRMA_PROTO]) 1271d3623099SNicolas Dichtel proto = nla_get_u8(attrs[XFRMA_PROTO]); 1272d3623099SNicolas Dichtel 1273d3623099SNicolas Dichtel xfrm_state_walk_init(walk, proto, filter); 12741ba5bf99SVegard Nossum cb->args[0] = 1; 12754c563f76STimo Teras } 12764c563f76STimo Teras 1277fc34acd3SAlexey Dobriyan (void) xfrm_state_walk(net, walk, dump_one_state, &info); 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds return skb->len; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, 12831da177e4SLinus Torvalds struct xfrm_state *x, u32 seq) 12841da177e4SLinus Torvalds { 12851da177e4SLinus Torvalds struct xfrm_dump_info info; 12861da177e4SLinus Torvalds struct sk_buff *skb; 1287864745d2SMathias Krause int err; 12881da177e4SLinus Torvalds 12897deb2264SThomas Graf skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 12901da177e4SLinus Torvalds if (!skb) 12911da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds info.in_skb = in_skb; 12941da177e4SLinus Torvalds info.out_skb = skb; 12951da177e4SLinus Torvalds info.nlmsg_seq = seq; 12961da177e4SLinus Torvalds info.nlmsg_flags = 0; 12971da177e4SLinus Torvalds 1298864745d2SMathias Krause err = dump_one_state(x, 0, &info); 1299864745d2SMathias Krause if (err) { 13001da177e4SLinus Torvalds kfree_skb(skb); 1301864745d2SMathias Krause return ERR_PTR(err); 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds return skb; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds 130721ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available. 130821ee543eSMichal Kubecek * Must be called with RCU read lock. 130921ee543eSMichal Kubecek */ 131021ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb, 131121ee543eSMichal Kubecek u32 pid, unsigned int group) 131221ee543eSMichal Kubecek { 131321ee543eSMichal Kubecek struct sock *nlsk = rcu_dereference(net->xfrm.nlsk); 13145461fc0cSDmitry Safonov struct xfrm_translator *xtr; 131521ee543eSMichal Kubecek 131686126b77SFlorian Westphal if (!nlsk) { 131786126b77SFlorian Westphal kfree_skb(skb); 131886126b77SFlorian Westphal return -EPIPE; 131986126b77SFlorian Westphal } 132086126b77SFlorian Westphal 13215461fc0cSDmitry Safonov xtr = xfrm_get_translator(); 13225461fc0cSDmitry Safonov if (xtr) { 13235461fc0cSDmitry Safonov int err = xtr->alloc_compat(skb, nlmsg_hdr(skb)); 13245461fc0cSDmitry Safonov 13255461fc0cSDmitry Safonov xfrm_put_translator(xtr); 13265461fc0cSDmitry Safonov if (err) { 13275461fc0cSDmitry Safonov kfree_skb(skb); 13285461fc0cSDmitry Safonov return err; 13295461fc0cSDmitry Safonov } 13305461fc0cSDmitry Safonov } 13315461fc0cSDmitry Safonov 133221ee543eSMichal Kubecek return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC); 133321ee543eSMichal Kubecek } 133421ee543eSMichal Kubecek 1335a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void) 13367deb2264SThomas Graf { 13377deb2264SThomas Graf return NLMSG_ALIGN(4) 13387deb2264SThomas Graf + nla_total_size(sizeof(struct xfrmu_spdinfo)) 1339880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhinfo)) 1340880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhthresh)) 1341880a6fabSChristophe Gouault + nla_total_size(sizeof(struct xfrmu_spdhthresh)); 13427deb2264SThomas Graf } 13437deb2264SThomas Graf 1344e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net, 134515e47304SEric W. Biederman u32 portid, u32 seq, u32 flags) 1346ecfd6b18SJamal Hadi Salim { 13475a6d3416SJamal Hadi Salim struct xfrmk_spdinfo si; 13485a6d3416SJamal Hadi Salim struct xfrmu_spdinfo spc; 13495a6d3416SJamal Hadi Salim struct xfrmu_spdhinfo sph; 1350880a6fabSChristophe Gouault struct xfrmu_spdhthresh spt4, spt6; 1351ecfd6b18SJamal Hadi Salim struct nlmsghdr *nlh; 13521d1e34ddSDavid S. Miller int err; 1353ecfd6b18SJamal Hadi Salim u32 *f; 1354880a6fabSChristophe Gouault unsigned lseq; 1355ecfd6b18SJamal Hadi Salim 135615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); 135725985edcSLucas De Marchi if (nlh == NULL) /* shouldn't really happen ... */ 1358ecfd6b18SJamal Hadi Salim return -EMSGSIZE; 1359ecfd6b18SJamal Hadi Salim 1360ecfd6b18SJamal Hadi Salim f = nlmsg_data(nlh); 1361ecfd6b18SJamal Hadi Salim *f = flags; 1362e071041bSAlexey Dobriyan xfrm_spd_getinfo(net, &si); 13635a6d3416SJamal Hadi Salim spc.incnt = si.incnt; 13645a6d3416SJamal Hadi Salim spc.outcnt = si.outcnt; 13655a6d3416SJamal Hadi Salim spc.fwdcnt = si.fwdcnt; 13665a6d3416SJamal Hadi Salim spc.inscnt = si.inscnt; 13675a6d3416SJamal Hadi Salim spc.outscnt = si.outscnt; 13685a6d3416SJamal Hadi Salim spc.fwdscnt = si.fwdscnt; 13695a6d3416SJamal Hadi Salim sph.spdhcnt = si.spdhcnt; 13705a6d3416SJamal Hadi Salim sph.spdhmcnt = si.spdhmcnt; 1371ecfd6b18SJamal Hadi Salim 1372880a6fabSChristophe Gouault do { 1373880a6fabSChristophe Gouault lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock); 1374880a6fabSChristophe Gouault 1375880a6fabSChristophe Gouault spt4.lbits = net->xfrm.policy_hthresh.lbits4; 1376880a6fabSChristophe Gouault spt4.rbits = net->xfrm.policy_hthresh.rbits4; 1377880a6fabSChristophe Gouault spt6.lbits = net->xfrm.policy_hthresh.lbits6; 1378880a6fabSChristophe Gouault spt6.rbits = net->xfrm.policy_hthresh.rbits6; 1379880a6fabSChristophe Gouault } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq)); 1380880a6fabSChristophe Gouault 13811d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc); 13821d1e34ddSDavid S. Miller if (!err) 13831d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph); 1384880a6fabSChristophe Gouault if (!err) 1385880a6fabSChristophe Gouault err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4); 1386880a6fabSChristophe Gouault if (!err) 1387880a6fabSChristophe Gouault err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6); 13881d1e34ddSDavid S. Miller if (err) { 13891d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 13901d1e34ddSDavid S. Miller return err; 13911d1e34ddSDavid S. Miller } 1392ecfd6b18SJamal Hadi Salim 1393053c095aSJohannes Berg nlmsg_end(skb, nlh); 1394053c095aSJohannes Berg return 0; 1395ecfd6b18SJamal Hadi Salim } 1396ecfd6b18SJamal Hadi Salim 1397880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 13983bec6c3eSSabrina Dubroca struct nlattr **attrs, 13993bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 1400880a6fabSChristophe Gouault { 1401880a6fabSChristophe Gouault struct net *net = sock_net(skb->sk); 1402880a6fabSChristophe Gouault struct xfrmu_spdhthresh *thresh4 = NULL; 1403880a6fabSChristophe Gouault struct xfrmu_spdhthresh *thresh6 = NULL; 1404880a6fabSChristophe Gouault 1405880a6fabSChristophe Gouault /* selector prefixlen thresholds to hash policies */ 1406880a6fabSChristophe Gouault if (attrs[XFRMA_SPD_IPV4_HTHRESH]) { 1407880a6fabSChristophe Gouault struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH]; 1408880a6fabSChristophe Gouault 1409a7417216SSabrina Dubroca if (nla_len(rta) < sizeof(*thresh4)) { 1410a7417216SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid SPD_IPV4_HTHRESH attribute length"); 1411880a6fabSChristophe Gouault return -EINVAL; 1412a7417216SSabrina Dubroca } 1413880a6fabSChristophe Gouault thresh4 = nla_data(rta); 1414a7417216SSabrina Dubroca if (thresh4->lbits > 32 || thresh4->rbits > 32) { 1415a7417216SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 32 for IPv4)"); 1416880a6fabSChristophe Gouault return -EINVAL; 1417880a6fabSChristophe Gouault } 1418a7417216SSabrina Dubroca } 1419880a6fabSChristophe Gouault if (attrs[XFRMA_SPD_IPV6_HTHRESH]) { 1420880a6fabSChristophe Gouault struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH]; 1421880a6fabSChristophe Gouault 1422a7417216SSabrina Dubroca if (nla_len(rta) < sizeof(*thresh6)) { 1423a7417216SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid SPD_IPV6_HTHRESH attribute length"); 1424880a6fabSChristophe Gouault return -EINVAL; 1425a7417216SSabrina Dubroca } 1426880a6fabSChristophe Gouault thresh6 = nla_data(rta); 1427a7417216SSabrina Dubroca if (thresh6->lbits > 128 || thresh6->rbits > 128) { 1428a7417216SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 128 for IPv6)"); 1429880a6fabSChristophe Gouault return -EINVAL; 1430880a6fabSChristophe Gouault } 1431a7417216SSabrina Dubroca } 1432880a6fabSChristophe Gouault 1433880a6fabSChristophe Gouault if (thresh4 || thresh6) { 1434880a6fabSChristophe Gouault write_seqlock(&net->xfrm.policy_hthresh.lock); 1435880a6fabSChristophe Gouault if (thresh4) { 1436880a6fabSChristophe Gouault net->xfrm.policy_hthresh.lbits4 = thresh4->lbits; 1437880a6fabSChristophe Gouault net->xfrm.policy_hthresh.rbits4 = thresh4->rbits; 1438880a6fabSChristophe Gouault } 1439880a6fabSChristophe Gouault if (thresh6) { 1440880a6fabSChristophe Gouault net->xfrm.policy_hthresh.lbits6 = thresh6->lbits; 1441880a6fabSChristophe Gouault net->xfrm.policy_hthresh.rbits6 = thresh6->rbits; 1442880a6fabSChristophe Gouault } 1443880a6fabSChristophe Gouault write_sequnlock(&net->xfrm.policy_hthresh.lock); 1444880a6fabSChristophe Gouault 1445880a6fabSChristophe Gouault xfrm_policy_hash_rebuild(net); 1446880a6fabSChristophe Gouault } 1447880a6fabSChristophe Gouault 1448880a6fabSChristophe Gouault return 0; 1449880a6fabSChristophe Gouault } 1450880a6fabSChristophe Gouault 1451ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 14523bec6c3eSSabrina Dubroca struct nlattr **attrs, 14533bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 1454ecfd6b18SJamal Hadi Salim { 1455a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 1456ecfd6b18SJamal Hadi Salim struct sk_buff *r_skb; 14577b67c857SThomas Graf u32 *flags = nlmsg_data(nlh); 145815e47304SEric W. Biederman u32 sportid = NETLINK_CB(skb).portid; 1459ecfd6b18SJamal Hadi Salim u32 seq = nlh->nlmsg_seq; 14602fc5f83bSGustavo A. R. Silva int err; 1461ecfd6b18SJamal Hadi Salim 14627deb2264SThomas Graf r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); 1463ecfd6b18SJamal Hadi Salim if (r_skb == NULL) 1464ecfd6b18SJamal Hadi Salim return -ENOMEM; 1465ecfd6b18SJamal Hadi Salim 14662fc5f83bSGustavo A. R. Silva err = build_spdinfo(r_skb, net, sportid, seq, *flags); 14672fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 1468ecfd6b18SJamal Hadi Salim 146915e47304SEric W. Biederman return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 1470ecfd6b18SJamal Hadi Salim } 1471ecfd6b18SJamal Hadi Salim 1472a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void) 14737deb2264SThomas Graf { 14747deb2264SThomas Graf return NLMSG_ALIGN(4) 14757deb2264SThomas Graf + nla_total_size(sizeof(struct xfrmu_sadhinfo)) 14767deb2264SThomas Graf + nla_total_size(4); /* XFRMA_SAD_CNT */ 14777deb2264SThomas Graf } 14787deb2264SThomas Graf 1479e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net, 148015e47304SEric W. Biederman u32 portid, u32 seq, u32 flags) 148128d8909bSJamal Hadi Salim { 1482af11e316SJamal Hadi Salim struct xfrmk_sadinfo si; 1483af11e316SJamal Hadi Salim struct xfrmu_sadhinfo sh; 148428d8909bSJamal Hadi Salim struct nlmsghdr *nlh; 14851d1e34ddSDavid S. Miller int err; 148628d8909bSJamal Hadi Salim u32 *f; 148728d8909bSJamal Hadi Salim 148815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); 148925985edcSLucas De Marchi if (nlh == NULL) /* shouldn't really happen ... */ 149028d8909bSJamal Hadi Salim return -EMSGSIZE; 149128d8909bSJamal Hadi Salim 149228d8909bSJamal Hadi Salim f = nlmsg_data(nlh); 149328d8909bSJamal Hadi Salim *f = flags; 1494e071041bSAlexey Dobriyan xfrm_sad_getinfo(net, &si); 149528d8909bSJamal Hadi Salim 1496af11e316SJamal Hadi Salim sh.sadhmcnt = si.sadhmcnt; 1497af11e316SJamal Hadi Salim sh.sadhcnt = si.sadhcnt; 1498af11e316SJamal Hadi Salim 14991d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt); 15001d1e34ddSDavid S. Miller if (!err) 15011d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); 15021d1e34ddSDavid S. Miller if (err) { 15031d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 15041d1e34ddSDavid S. Miller return err; 15051d1e34ddSDavid S. Miller } 150628d8909bSJamal Hadi Salim 1507053c095aSJohannes Berg nlmsg_end(skb, nlh); 1508053c095aSJohannes Berg return 0; 150928d8909bSJamal Hadi Salim } 151028d8909bSJamal Hadi Salim 151128d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 15123bec6c3eSSabrina Dubroca struct nlattr **attrs, 15133bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 151428d8909bSJamal Hadi Salim { 1515a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 151628d8909bSJamal Hadi Salim struct sk_buff *r_skb; 15177b67c857SThomas Graf u32 *flags = nlmsg_data(nlh); 151815e47304SEric W. Biederman u32 sportid = NETLINK_CB(skb).portid; 151928d8909bSJamal Hadi Salim u32 seq = nlh->nlmsg_seq; 15202fc5f83bSGustavo A. R. Silva int err; 152128d8909bSJamal Hadi Salim 15227deb2264SThomas Graf r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); 152328d8909bSJamal Hadi Salim if (r_skb == NULL) 152428d8909bSJamal Hadi Salim return -ENOMEM; 152528d8909bSJamal Hadi Salim 15262fc5f83bSGustavo A. R. Silva err = build_sadinfo(r_skb, net, sportid, seq, *flags); 15272fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 152828d8909bSJamal Hadi Salim 152915e47304SEric W. Biederman return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 153028d8909bSJamal Hadi Salim } 153128d8909bSJamal Hadi Salim 153222e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 15333bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 15341da177e4SLinus Torvalds { 1535fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 15367b67c857SThomas Graf struct xfrm_usersa_id *p = nlmsg_data(nlh); 15371da177e4SLinus Torvalds struct xfrm_state *x; 15381da177e4SLinus Torvalds struct sk_buff *resp_skb; 1539eb2971b6SMasahide NAKAMURA int err = -ESRCH; 15401da177e4SLinus Torvalds 1541fc34acd3SAlexey Dobriyan x = xfrm_user_state_lookup(net, p, attrs, &err); 15421da177e4SLinus Torvalds if (x == NULL) 15431da177e4SLinus Torvalds goto out_noput; 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 15461da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 15471da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 15481da177e4SLinus Torvalds } else { 154915e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds xfrm_state_put(x); 15521da177e4SLinus Torvalds out_noput: 15531da177e4SLinus Torvalds return err; 15541da177e4SLinus Torvalds } 15551da177e4SLinus Torvalds 155622e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, 15573bec6c3eSSabrina Dubroca struct nlattr **attrs, 15583bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 15591da177e4SLinus Torvalds { 1560fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 15611da177e4SLinus Torvalds struct xfrm_state *x; 15621da177e4SLinus Torvalds struct xfrm_userspi_info *p; 15635f3eea6bSDmitry Safonov struct xfrm_translator *xtr; 15641da177e4SLinus Torvalds struct sk_buff *resp_skb; 15651da177e4SLinus Torvalds xfrm_address_t *daddr; 15661da177e4SLinus Torvalds int family; 15671da177e4SLinus Torvalds int err; 15686f26b61eSJamal Hadi Salim u32 mark; 15696f26b61eSJamal Hadi Salim struct xfrm_mark m; 15707e652640SSteffen Klassert u32 if_id = 0; 15711da177e4SLinus Torvalds 15727b67c857SThomas Graf p = nlmsg_data(nlh); 1573c2dad11eSSabrina Dubroca err = verify_spi_info(p->info.id.proto, p->min, p->max, extack); 15741da177e4SLinus Torvalds if (err) 15751da177e4SLinus Torvalds goto out_noput; 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds family = p->info.family; 15781da177e4SLinus Torvalds daddr = &p->info.id.daddr; 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds x = NULL; 15816f26b61eSJamal Hadi Salim 15826f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 15837e652640SSteffen Klassert 1584a3d9001bSKai Lueke if (attrs[XFRMA_IF_ID]) 15857e652640SSteffen Klassert if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 15867e652640SSteffen Klassert 15871da177e4SLinus Torvalds if (p->info.seq) { 15886f26b61eSJamal Hadi Salim x = xfrm_find_acq_byseq(net, mark, p->info.seq); 158970e94e66SYOSHIFUJI Hideaki / 吉藤英明 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) { 15901da177e4SLinus Torvalds xfrm_state_put(x); 15911da177e4SLinus Torvalds x = NULL; 15921da177e4SLinus Torvalds } 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds if (!x) 15966f26b61eSJamal Hadi Salim x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid, 15977e652640SSteffen Klassert if_id, p->info.id.proto, daddr, 15981da177e4SLinus Torvalds &p->info.saddr, 1, 15991da177e4SLinus Torvalds family); 16001da177e4SLinus Torvalds err = -ENOENT; 1601c2dad11eSSabrina Dubroca if (!x) { 1602c2dad11eSSabrina Dubroca NL_SET_ERR_MSG(extack, "Target ACQUIRE not found"); 16031da177e4SLinus Torvalds goto out_noput; 1604c2dad11eSSabrina Dubroca } 16051da177e4SLinus Torvalds 1606c2dad11eSSabrina Dubroca err = xfrm_alloc_spi(x, p->min, p->max, extack); 1607658b219eSHerbert Xu if (err) 1608658b219eSHerbert Xu goto out; 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 16111da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 16121da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 16131da177e4SLinus Torvalds goto out; 16141da177e4SLinus Torvalds } 16151da177e4SLinus Torvalds 16165f3eea6bSDmitry Safonov xtr = xfrm_get_translator(); 16175f3eea6bSDmitry Safonov if (xtr) { 16185f3eea6bSDmitry Safonov err = xtr->alloc_compat(skb, nlmsg_hdr(skb)); 16195f3eea6bSDmitry Safonov 16205f3eea6bSDmitry Safonov xfrm_put_translator(xtr); 16215f3eea6bSDmitry Safonov if (err) { 16225f3eea6bSDmitry Safonov kfree_skb(resp_skb); 16235f3eea6bSDmitry Safonov goto out; 16245f3eea6bSDmitry Safonov } 16255f3eea6bSDmitry Safonov } 16265f3eea6bSDmitry Safonov 162715e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds out: 16301da177e4SLinus Torvalds xfrm_state_put(x); 16311da177e4SLinus Torvalds out_noput: 16321da177e4SLinus Torvalds return err; 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 163524fc544fSSabrina Dubroca static int verify_policy_dir(u8 dir, struct netlink_ext_ack *extack) 16361da177e4SLinus Torvalds { 16371da177e4SLinus Torvalds switch (dir) { 16381da177e4SLinus Torvalds case XFRM_POLICY_IN: 16391da177e4SLinus Torvalds case XFRM_POLICY_OUT: 16401da177e4SLinus Torvalds case XFRM_POLICY_FWD: 16411da177e4SLinus Torvalds break; 16421da177e4SLinus Torvalds 16431da177e4SLinus Torvalds default: 164424fc544fSSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid policy direction"); 16451da177e4SLinus Torvalds return -EINVAL; 16463ff50b79SStephen Hemminger } 16471da177e4SLinus Torvalds 16481da177e4SLinus Torvalds return 0; 16491da177e4SLinus Torvalds } 16501da177e4SLinus Torvalds 1651fb7deabaSSabrina Dubroca static int verify_policy_type(u8 type, struct netlink_ext_ack *extack) 1652f7b6983fSMasahide NAKAMURA { 1653f7b6983fSMasahide NAKAMURA switch (type) { 1654f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_MAIN: 1655f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 1656f7b6983fSMasahide NAKAMURA case XFRM_POLICY_TYPE_SUB: 1657f7b6983fSMasahide NAKAMURA #endif 1658f7b6983fSMasahide NAKAMURA break; 1659f7b6983fSMasahide NAKAMURA 1660f7b6983fSMasahide NAKAMURA default: 1661fb7deabaSSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid policy type"); 1662f7b6983fSMasahide NAKAMURA return -EINVAL; 16633ff50b79SStephen Hemminger } 1664f7b6983fSMasahide NAKAMURA 1665f7b6983fSMasahide NAKAMURA return 0; 1666f7b6983fSMasahide NAKAMURA } 1667f7b6983fSMasahide NAKAMURA 1668ec2b4f01SSabrina Dubroca static int verify_newpolicy_info(struct xfrm_userpolicy_info *p, 1669ec2b4f01SSabrina Dubroca struct netlink_ext_ack *extack) 16701da177e4SLinus Torvalds { 1671e682adf0SFan Du int ret; 1672e682adf0SFan Du 16731da177e4SLinus Torvalds switch (p->share) { 16741da177e4SLinus Torvalds case XFRM_SHARE_ANY: 16751da177e4SLinus Torvalds case XFRM_SHARE_SESSION: 16761da177e4SLinus Torvalds case XFRM_SHARE_USER: 16771da177e4SLinus Torvalds case XFRM_SHARE_UNIQUE: 16781da177e4SLinus Torvalds break; 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds default: 1681ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid policy share"); 16821da177e4SLinus Torvalds return -EINVAL; 16833ff50b79SStephen Hemminger } 16841da177e4SLinus Torvalds 16851da177e4SLinus Torvalds switch (p->action) { 16861da177e4SLinus Torvalds case XFRM_POLICY_ALLOW: 16871da177e4SLinus Torvalds case XFRM_POLICY_BLOCK: 16881da177e4SLinus Torvalds break; 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds default: 1691ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid policy action"); 16921da177e4SLinus Torvalds return -EINVAL; 16933ff50b79SStephen Hemminger } 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds switch (p->sel.family) { 16961da177e4SLinus Torvalds case AF_INET: 1697ec2b4f01SSabrina Dubroca if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) { 1698ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)"); 169907bf7908SSteffen Klassert return -EINVAL; 1700ec2b4f01SSabrina Dubroca } 170107bf7908SSteffen Klassert 17021da177e4SLinus Torvalds break; 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds case AF_INET6: 1705dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1706ec2b4f01SSabrina Dubroca if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) { 1707ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)"); 170807bf7908SSteffen Klassert return -EINVAL; 1709ec2b4f01SSabrina Dubroca } 171007bf7908SSteffen Klassert 17111da177e4SLinus Torvalds break; 17121da177e4SLinus Torvalds #else 1713ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "IPv6 support disabled"); 17141da177e4SLinus Torvalds return -EAFNOSUPPORT; 17151da177e4SLinus Torvalds #endif 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds default: 1718ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid selector family"); 17191da177e4SLinus Torvalds return -EINVAL; 17203ff50b79SStephen Hemminger } 17211da177e4SLinus Torvalds 172224fc544fSSabrina Dubroca ret = verify_policy_dir(p->dir, extack); 1723e682adf0SFan Du if (ret) 1724e682adf0SFan Du return ret; 1725ec2b4f01SSabrina Dubroca if (p->index && (xfrm_policy_id2dir(p->index) != p->dir)) { 1726ec2b4f01SSabrina Dubroca NL_SET_ERR_MSG(extack, "Policy index doesn't match direction"); 1727e682adf0SFan Du return -EINVAL; 1728ec2b4f01SSabrina Dubroca } 1729e682adf0SFan Du 1730e682adf0SFan Du return 0; 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds 17335424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) 1734df71837dSTrent Jaeger { 17355424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1736df71837dSTrent Jaeger struct xfrm_user_sec_ctx *uctx; 1737df71837dSTrent Jaeger 1738df71837dSTrent Jaeger if (!rt) 1739df71837dSTrent Jaeger return 0; 1740df71837dSTrent Jaeger 17415424f32eSThomas Graf uctx = nla_data(rt); 174252a4c640SNikolay Aleksandrov return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL); 1743df71837dSTrent Jaeger } 1744df71837dSTrent Jaeger 17451da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, 17461da177e4SLinus Torvalds int nr) 17471da177e4SLinus Torvalds { 17481da177e4SLinus Torvalds int i; 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds xp->xfrm_nr = nr; 17511da177e4SLinus Torvalds for (i = 0; i < nr; i++, ut++) { 17521da177e4SLinus Torvalds struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); 17551da177e4SLinus Torvalds memcpy(&t->saddr, &ut->saddr, 17561da177e4SLinus Torvalds sizeof(xfrm_address_t)); 17571da177e4SLinus Torvalds t->reqid = ut->reqid; 17581da177e4SLinus Torvalds t->mode = ut->mode; 17591da177e4SLinus Torvalds t->share = ut->share; 17601da177e4SLinus Torvalds t->optional = ut->optional; 17611da177e4SLinus Torvalds t->aalgos = ut->aalgos; 17621da177e4SLinus Torvalds t->ealgos = ut->ealgos; 17631da177e4SLinus Torvalds t->calgos = ut->calgos; 1764c5d18e98SHerbert Xu /* If all masks are ~0, then we allow all algorithms. */ 1765c5d18e98SHerbert Xu t->allalgs = !~(t->aalgos & t->ealgos & t->calgos); 17668511d01dSMiika Komu t->encap_family = ut->family; 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds 1770d37bed89SSabrina Dubroca static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family, 1771d37bed89SSabrina Dubroca struct netlink_ext_ack *extack) 1772b4ad86bfSDavid S. Miller { 1773732706afSSteffen Klassert u16 prev_family; 1774b4ad86bfSDavid S. Miller int i; 1775b4ad86bfSDavid S. Miller 1776d37bed89SSabrina Dubroca if (nr > XFRM_MAX_DEPTH) { 1777d37bed89SSabrina Dubroca NL_SET_ERR_MSG(extack, "Template count must be <= XFRM_MAX_DEPTH (" __stringify(XFRM_MAX_DEPTH) ")"); 1778b4ad86bfSDavid S. Miller return -EINVAL; 1779d37bed89SSabrina Dubroca } 1780b4ad86bfSDavid S. Miller 1781732706afSSteffen Klassert prev_family = family; 1782732706afSSteffen Klassert 1783b4ad86bfSDavid S. Miller for (i = 0; i < nr; i++) { 1784b4ad86bfSDavid S. Miller /* We never validated the ut->family value, so many 1785b4ad86bfSDavid S. Miller * applications simply leave it at zero. The check was 1786b4ad86bfSDavid S. Miller * never made and ut->family was ignored because all 1787b4ad86bfSDavid S. Miller * templates could be assumed to have the same family as 1788b4ad86bfSDavid S. Miller * the policy itself. Now that we will have ipv4-in-ipv6 1789b4ad86bfSDavid S. Miller * and ipv6-in-ipv4 tunnels, this is no longer true. 1790b4ad86bfSDavid S. Miller */ 1791b4ad86bfSDavid S. Miller if (!ut[i].family) 1792b4ad86bfSDavid S. Miller ut[i].family = family; 1793b4ad86bfSDavid S. Miller 179435e61038SFlorian Westphal switch (ut[i].mode) { 179535e61038SFlorian Westphal case XFRM_MODE_TUNNEL: 179635e61038SFlorian Westphal case XFRM_MODE_BEET: 179735e61038SFlorian Westphal break; 179835e61038SFlorian Westphal default: 1799d37bed89SSabrina Dubroca if (ut[i].family != prev_family) { 1800d37bed89SSabrina Dubroca NL_SET_ERR_MSG(extack, "Mode in template doesn't support a family change"); 1801732706afSSteffen Klassert return -EINVAL; 1802d37bed89SSabrina Dubroca } 180335e61038SFlorian Westphal break; 180435e61038SFlorian Westphal } 1805d37bed89SSabrina Dubroca if (ut[i].mode >= XFRM_MODE_MAX) { 1806d37bed89SSabrina Dubroca NL_SET_ERR_MSG(extack, "Mode in template must be < XFRM_MODE_MAX (" __stringify(XFRM_MODE_MAX) ")"); 180732bf94fbSSean Tranchetti return -EINVAL; 1808d37bed89SSabrina Dubroca } 180932bf94fbSSean Tranchetti 1810732706afSSteffen Klassert prev_family = ut[i].family; 1811732706afSSteffen Klassert 1812b4ad86bfSDavid S. Miller switch (ut[i].family) { 1813b4ad86bfSDavid S. Miller case AF_INET: 1814b4ad86bfSDavid S. Miller break; 1815dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1816b4ad86bfSDavid S. Miller case AF_INET6: 1817b4ad86bfSDavid S. Miller break; 1818b4ad86bfSDavid S. Miller #endif 1819b4ad86bfSDavid S. Miller default: 1820d37bed89SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid family in template"); 1821b4ad86bfSDavid S. Miller return -EINVAL; 18223ff50b79SStephen Hemminger } 18236a53b759SCong Wang 1824d37bed89SSabrina Dubroca if (!xfrm_id_proto_valid(ut[i].id.proto)) { 1825d37bed89SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid XFRM protocol in template"); 18266a53b759SCong Wang return -EINVAL; 18276a53b759SCong Wang } 1828d37bed89SSabrina Dubroca } 18296a53b759SCong Wang 1830b4ad86bfSDavid S. Miller return 0; 1831b4ad86bfSDavid S. Miller } 1832b4ad86bfSDavid S. Miller 1833d37bed89SSabrina Dubroca static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs, 1834d37bed89SSabrina Dubroca struct netlink_ext_ack *extack) 18351da177e4SLinus Torvalds { 18365424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_TMPL]; 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds if (!rt) { 18391da177e4SLinus Torvalds pol->xfrm_nr = 0; 18401da177e4SLinus Torvalds } else { 18415424f32eSThomas Graf struct xfrm_user_tmpl *utmpl = nla_data(rt); 18425424f32eSThomas Graf int nr = nla_len(rt) / sizeof(*utmpl); 1843b4ad86bfSDavid S. Miller int err; 18441da177e4SLinus Torvalds 1845d37bed89SSabrina Dubroca err = validate_tmpl(nr, utmpl, pol->family, extack); 1846b4ad86bfSDavid S. Miller if (err) 1847b4ad86bfSDavid S. Miller return err; 18481da177e4SLinus Torvalds 18495424f32eSThomas Graf copy_templates(pol, utmpl, nr); 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds return 0; 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds 1854fb7deabaSSabrina Dubroca static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs, 1855fb7deabaSSabrina Dubroca struct netlink_ext_ack *extack) 1856f7b6983fSMasahide NAKAMURA { 18575424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; 1858f7b6983fSMasahide NAKAMURA struct xfrm_userpolicy_type *upt; 1859b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 1860f7b6983fSMasahide NAKAMURA int err; 1861f7b6983fSMasahide NAKAMURA 1862f7b6983fSMasahide NAKAMURA if (rt) { 18635424f32eSThomas Graf upt = nla_data(rt); 1864f7b6983fSMasahide NAKAMURA type = upt->type; 1865f7b6983fSMasahide NAKAMURA } 1866f7b6983fSMasahide NAKAMURA 1867fb7deabaSSabrina Dubroca err = verify_policy_type(type, extack); 1868f7b6983fSMasahide NAKAMURA if (err) 1869f7b6983fSMasahide NAKAMURA return err; 1870f7b6983fSMasahide NAKAMURA 1871f7b6983fSMasahide NAKAMURA *tp = type; 1872f7b6983fSMasahide NAKAMURA return 0; 1873f7b6983fSMasahide NAKAMURA } 1874f7b6983fSMasahide NAKAMURA 18751da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) 18761da177e4SLinus Torvalds { 18771da177e4SLinus Torvalds xp->priority = p->priority; 18781da177e4SLinus Torvalds xp->index = p->index; 18791da177e4SLinus Torvalds memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); 18801da177e4SLinus Torvalds memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); 18811da177e4SLinus Torvalds xp->action = p->action; 18821da177e4SLinus Torvalds xp->flags = p->flags; 18831da177e4SLinus Torvalds xp->family = p->sel.family; 18841da177e4SLinus Torvalds /* XXX xp->share = p->share; */ 18851da177e4SLinus Torvalds } 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) 18881da177e4SLinus Torvalds { 18897b789836SMathias Krause memset(p, 0, sizeof(*p)); 18901da177e4SLinus Torvalds memcpy(&p->sel, &xp->selector, sizeof(p->sel)); 18911da177e4SLinus Torvalds memcpy(&p->lft, &xp->lft, sizeof(p->lft)); 18921da177e4SLinus Torvalds memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); 18931da177e4SLinus Torvalds p->priority = xp->priority; 18941da177e4SLinus Torvalds p->index = xp->index; 18951da177e4SLinus Torvalds p->sel.family = xp->family; 18961da177e4SLinus Torvalds p->dir = dir; 18971da177e4SLinus Torvalds p->action = xp->action; 18981da177e4SLinus Torvalds p->flags = xp->flags; 18991da177e4SLinus Torvalds p->share = XFRM_SHARE_ANY; /* XXX xp->share */ 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds 1902fb7deabaSSabrina Dubroca static struct xfrm_policy *xfrm_policy_construct(struct net *net, 1903fb7deabaSSabrina Dubroca struct xfrm_userpolicy_info *p, 1904fb7deabaSSabrina Dubroca struct nlattr **attrs, 1905fb7deabaSSabrina Dubroca int *errp, 1906fb7deabaSSabrina Dubroca struct netlink_ext_ack *extack) 19071da177e4SLinus Torvalds { 1908fc34acd3SAlexey Dobriyan struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL); 19091da177e4SLinus Torvalds int err; 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds if (!xp) { 19121da177e4SLinus Torvalds *errp = -ENOMEM; 19131da177e4SLinus Torvalds return NULL; 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds copy_from_user_policy(xp, p); 1917df71837dSTrent Jaeger 1918fb7deabaSSabrina Dubroca err = copy_from_user_policy_type(&xp->type, attrs, extack); 1919f7b6983fSMasahide NAKAMURA if (err) 1920f7b6983fSMasahide NAKAMURA goto error; 1921f7b6983fSMasahide NAKAMURA 1922d37bed89SSabrina Dubroca if (!(err = copy_from_user_tmpl(xp, attrs, extack))) 192335a7aa08SThomas Graf err = copy_from_user_sec_ctx(xp, attrs); 1924f7b6983fSMasahide NAKAMURA if (err) 1925f7b6983fSMasahide NAKAMURA goto error; 19261da177e4SLinus Torvalds 1927295fae56SJamal Hadi Salim xfrm_mark_get(attrs, &xp->mark); 1928295fae56SJamal Hadi Salim 1929a3d9001bSKai Lueke if (attrs[XFRMA_IF_ID]) 19307e652640SSteffen Klassert xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 19317e652640SSteffen Klassert 1932919e43faSLeon Romanovsky /* configure the hardware if offload is requested */ 1933919e43faSLeon Romanovsky if (attrs[XFRMA_OFFLOAD_DEV]) { 1934919e43faSLeon Romanovsky err = xfrm_dev_policy_add(net, xp, 1935919e43faSLeon Romanovsky nla_data(attrs[XFRMA_OFFLOAD_DEV]), 1936919e43faSLeon Romanovsky p->dir, extack); 1937919e43faSLeon Romanovsky if (err) 1938919e43faSLeon Romanovsky goto error; 1939919e43faSLeon Romanovsky } 1940919e43faSLeon Romanovsky 19411da177e4SLinus Torvalds return xp; 1942f7b6983fSMasahide NAKAMURA error: 1943f7b6983fSMasahide NAKAMURA *errp = err; 194412a169e7SHerbert Xu xp->walk.dead = 1; 194564c31b3fSWANG Cong xfrm_policy_destroy(xp); 1946f7b6983fSMasahide NAKAMURA return NULL; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 194922e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 19503bec6c3eSSabrina Dubroca struct nlattr **attrs, 19513bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 19521da177e4SLinus Torvalds { 1953fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 19547b67c857SThomas Graf struct xfrm_userpolicy_info *p = nlmsg_data(nlh); 19551da177e4SLinus Torvalds struct xfrm_policy *xp; 195626b15dadSJamal Hadi Salim struct km_event c; 19571da177e4SLinus Torvalds int err; 19581da177e4SLinus Torvalds int excl; 19591da177e4SLinus Torvalds 1960ec2b4f01SSabrina Dubroca err = verify_newpolicy_info(p, extack); 19611da177e4SLinus Torvalds if (err) 19621da177e4SLinus Torvalds return err; 196308a717e4SSabrina Dubroca err = verify_sec_ctx_len(attrs, extack); 1964df71837dSTrent Jaeger if (err) 1965df71837dSTrent Jaeger return err; 19661da177e4SLinus Torvalds 1967fb7deabaSSabrina Dubroca xp = xfrm_policy_construct(net, p, attrs, &err, extack); 19681da177e4SLinus Torvalds if (!xp) 19691da177e4SLinus Torvalds return err; 19701da177e4SLinus Torvalds 197125985edcSLucas De Marchi /* shouldn't excl be based on nlh flags?? 197226b15dadSJamal Hadi Salim * Aha! this is anti-netlink really i.e more pfkey derived 1973a7fd0e6dSBhaskar Chowdhury * in netlink excl is a flag and you wouldn't need 197426b15dadSJamal Hadi Salim * a type XFRM_MSG_UPDPOLICY - JHS */ 19751da177e4SLinus Torvalds excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; 19761da177e4SLinus Torvalds err = xfrm_policy_insert(p->dir, xp, excl); 19772e71029eSTetsuo Handa xfrm_audit_policy_add(xp, err ? 0 : 1, true); 1978161a09e7SJoy Latten 19791da177e4SLinus Torvalds if (err) { 1980919e43faSLeon Romanovsky xfrm_dev_policy_delete(xp); 198103e1ad7bSPaul Moore security_xfrm_policy_free(xp->security); 19821da177e4SLinus Torvalds kfree(xp); 19831da177e4SLinus Torvalds return err; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds 1986f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 198726b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 198815e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 198926b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 199026b15dadSJamal Hadi Salim 19911da177e4SLinus Torvalds xfrm_pol_put(xp); 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds return 0; 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) 19971da177e4SLinus Torvalds { 19981da177e4SLinus Torvalds struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; 19991da177e4SLinus Torvalds int i; 20001da177e4SLinus Torvalds 20011da177e4SLinus Torvalds if (xp->xfrm_nr == 0) 20021da177e4SLinus Torvalds return 0; 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds for (i = 0; i < xp->xfrm_nr; i++) { 20051da177e4SLinus Torvalds struct xfrm_user_tmpl *up = &vec[i]; 20061da177e4SLinus Torvalds struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; 20071da177e4SLinus Torvalds 20081f86840fSMathias Krause memset(up, 0, sizeof(*up)); 20091da177e4SLinus Torvalds memcpy(&up->id, &kp->id, sizeof(up->id)); 20108511d01dSMiika Komu up->family = kp->encap_family; 20111da177e4SLinus Torvalds memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); 20121da177e4SLinus Torvalds up->reqid = kp->reqid; 20131da177e4SLinus Torvalds up->mode = kp->mode; 20141da177e4SLinus Torvalds up->share = kp->share; 20151da177e4SLinus Torvalds up->optional = kp->optional; 20161da177e4SLinus Torvalds up->aalgos = kp->aalgos; 20171da177e4SLinus Torvalds up->ealgos = kp->ealgos; 20181da177e4SLinus Torvalds up->calgos = kp->calgos; 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds 2021c0144beaSThomas Graf return nla_put(skb, XFRMA_TMPL, 2022c0144beaSThomas Graf sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); 2023df71837dSTrent Jaeger } 2024df71837dSTrent Jaeger 20250d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) 20260d681623SSerge Hallyn { 20270d681623SSerge Hallyn if (x->security) { 20280d681623SSerge Hallyn return copy_sec_ctx(x->security, skb); 20290d681623SSerge Hallyn } 20300d681623SSerge Hallyn return 0; 20310d681623SSerge Hallyn } 20320d681623SSerge Hallyn 20330d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) 20340d681623SSerge Hallyn { 20351d1e34ddSDavid S. Miller if (xp->security) 20360d681623SSerge Hallyn return copy_sec_ctx(xp->security, skb); 20370d681623SSerge Hallyn return 0; 20380d681623SSerge Hallyn } 2039a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void) 2040cfbfd45aSThomas Graf { 2041cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY 2042cfbfd45aSThomas Graf return nla_total_size(sizeof(struct xfrm_userpolicy_type)); 2043cfbfd45aSThomas Graf #else 2044cfbfd45aSThomas Graf return 0; 2045cfbfd45aSThomas Graf #endif 2046cfbfd45aSThomas Graf } 20470d681623SSerge Hallyn 2048f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 2049b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 2050f7b6983fSMasahide NAKAMURA { 205145c180bcSEric Dumazet struct xfrm_userpolicy_type upt; 205245c180bcSEric Dumazet 205345c180bcSEric Dumazet /* Sadly there are two holes in struct xfrm_userpolicy_type */ 205445c180bcSEric Dumazet memset(&upt, 0, sizeof(upt)); 205545c180bcSEric Dumazet upt.type = type; 2056f7b6983fSMasahide NAKAMURA 2057c0144beaSThomas Graf return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); 2058f7b6983fSMasahide NAKAMURA } 2059f7b6983fSMasahide NAKAMURA 2060f7b6983fSMasahide NAKAMURA #else 2061b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 2062f7b6983fSMasahide NAKAMURA { 2063f7b6983fSMasahide NAKAMURA return 0; 2064f7b6983fSMasahide NAKAMURA } 2065f7b6983fSMasahide NAKAMURA #endif 2066f7b6983fSMasahide NAKAMURA 20671da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) 20681da177e4SLinus Torvalds { 20691da177e4SLinus Torvalds struct xfrm_dump_info *sp = ptr; 20701da177e4SLinus Torvalds struct xfrm_userpolicy_info *p; 20711da177e4SLinus Torvalds struct sk_buff *in_skb = sp->in_skb; 20721da177e4SLinus Torvalds struct sk_buff *skb = sp->out_skb; 20735f3eea6bSDmitry Safonov struct xfrm_translator *xtr; 20741da177e4SLinus Torvalds struct nlmsghdr *nlh; 20751d1e34ddSDavid S. Miller int err; 20761da177e4SLinus Torvalds 207715e47304SEric W. Biederman nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq, 207879b8b7f4SThomas Graf XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); 207979b8b7f4SThomas Graf if (nlh == NULL) 208079b8b7f4SThomas Graf return -EMSGSIZE; 20811da177e4SLinus Torvalds 20827b67c857SThomas Graf p = nlmsg_data(nlh); 20831da177e4SLinus Torvalds copy_to_user_policy(xp, p, dir); 20841d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 20851d1e34ddSDavid S. Miller if (!err) 20861d1e34ddSDavid S. Miller err = copy_to_user_sec_ctx(xp, skb); 20871d1e34ddSDavid S. Miller if (!err) 20881d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 20891d1e34ddSDavid S. Miller if (!err) 20901d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 20917e652640SSteffen Klassert if (!err) 20927e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 2093919e43faSLeon Romanovsky if (!err && xp->xdo.dev) 2094919e43faSLeon Romanovsky err = copy_user_offload(&xp->xdo, skb); 20951d1e34ddSDavid S. Miller if (err) { 20961d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 20971d1e34ddSDavid S. Miller return err; 20981d1e34ddSDavid S. Miller } 20999825069dSThomas Graf nlmsg_end(skb, nlh); 21005f3eea6bSDmitry Safonov 21015f3eea6bSDmitry Safonov xtr = xfrm_get_translator(); 21025f3eea6bSDmitry Safonov if (xtr) { 21035f3eea6bSDmitry Safonov err = xtr->alloc_compat(skb, nlh); 21045f3eea6bSDmitry Safonov 21055f3eea6bSDmitry Safonov xfrm_put_translator(xtr); 21065f3eea6bSDmitry Safonov if (err) { 21075f3eea6bSDmitry Safonov nlmsg_cancel(skb, nlh); 21085f3eea6bSDmitry Safonov return err; 21095f3eea6bSDmitry Safonov } 21105f3eea6bSDmitry Safonov } 21115f3eea6bSDmitry Safonov 21121da177e4SLinus Torvalds return 0; 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 21154c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb) 21164c563f76STimo Teras { 21171137b5e2SHerbert Xu struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 2118283bc9f3SFan Du struct net *net = sock_net(cb->skb->sk); 21194c563f76STimo Teras 2120283bc9f3SFan Du xfrm_policy_walk_done(walk, net); 21214c563f76STimo Teras return 0; 21224c563f76STimo Teras } 21234c563f76STimo Teras 21241137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb) 21251137b5e2SHerbert Xu { 21261137b5e2SHerbert Xu struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 21271137b5e2SHerbert Xu 21281137b5e2SHerbert Xu BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args)); 21291137b5e2SHerbert Xu 21301137b5e2SHerbert Xu xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); 21311137b5e2SHerbert Xu return 0; 21321137b5e2SHerbert Xu } 21331137b5e2SHerbert Xu 21341da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) 21351da177e4SLinus Torvalds { 2136fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 21371137b5e2SHerbert Xu struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 21381da177e4SLinus Torvalds struct xfrm_dump_info info; 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds info.in_skb = cb->skb; 21411da177e4SLinus Torvalds info.out_skb = skb; 21421da177e4SLinus Torvalds info.nlmsg_seq = cb->nlh->nlmsg_seq; 21431da177e4SLinus Torvalds info.nlmsg_flags = NLM_F_MULTI; 21444c563f76STimo Teras 2145fc34acd3SAlexey Dobriyan (void) xfrm_policy_walk(net, walk, dump_one_policy, &info); 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds return skb->len; 21481da177e4SLinus Torvalds } 21491da177e4SLinus Torvalds 21501da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, 21511da177e4SLinus Torvalds struct xfrm_policy *xp, 21521da177e4SLinus Torvalds int dir, u32 seq) 21531da177e4SLinus Torvalds { 21541da177e4SLinus Torvalds struct xfrm_dump_info info; 21551da177e4SLinus Torvalds struct sk_buff *skb; 2156c2546372SMathias Krause int err; 21571da177e4SLinus Torvalds 21587deb2264SThomas Graf skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 21591da177e4SLinus Torvalds if (!skb) 21601da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds info.in_skb = in_skb; 21631da177e4SLinus Torvalds info.out_skb = skb; 21641da177e4SLinus Torvalds info.nlmsg_seq = seq; 21651da177e4SLinus Torvalds info.nlmsg_flags = 0; 21661da177e4SLinus Torvalds 2167c2546372SMathias Krause err = dump_one_policy(xp, dir, 0, &info); 2168c2546372SMathias Krause if (err) { 21691da177e4SLinus Torvalds kfree_skb(skb); 2170c2546372SMathias Krause return ERR_PTR(err); 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds 21731da177e4SLinus Torvalds return skb; 21741da177e4SLinus Torvalds } 21751da177e4SLinus Torvalds 217688d0adb5SNicolas Dichtel static int xfrm_notify_userpolicy(struct net *net) 217788d0adb5SNicolas Dichtel { 217888d0adb5SNicolas Dichtel struct xfrm_userpolicy_default *up; 217988d0adb5SNicolas Dichtel int len = NLMSG_ALIGN(sizeof(*up)); 218088d0adb5SNicolas Dichtel struct nlmsghdr *nlh; 218188d0adb5SNicolas Dichtel struct sk_buff *skb; 218293ec1320SNicolas Dichtel int err; 218388d0adb5SNicolas Dichtel 218488d0adb5SNicolas Dichtel skb = nlmsg_new(len, GFP_ATOMIC); 218588d0adb5SNicolas Dichtel if (skb == NULL) 218688d0adb5SNicolas Dichtel return -ENOMEM; 218788d0adb5SNicolas Dichtel 218888d0adb5SNicolas Dichtel nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0); 218988d0adb5SNicolas Dichtel if (nlh == NULL) { 219088d0adb5SNicolas Dichtel kfree_skb(skb); 219188d0adb5SNicolas Dichtel return -EMSGSIZE; 219288d0adb5SNicolas Dichtel } 219388d0adb5SNicolas Dichtel 219488d0adb5SNicolas Dichtel up = nlmsg_data(nlh); 2195b58b1f56SNicolas Dichtel up->in = net->xfrm.policy_default[XFRM_POLICY_IN]; 2196b58b1f56SNicolas Dichtel up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD]; 2197b58b1f56SNicolas Dichtel up->out = net->xfrm.policy_default[XFRM_POLICY_OUT]; 219888d0adb5SNicolas Dichtel 219988d0adb5SNicolas Dichtel nlmsg_end(skb, nlh); 220088d0adb5SNicolas Dichtel 220193ec1320SNicolas Dichtel rcu_read_lock(); 220293ec1320SNicolas Dichtel err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 220393ec1320SNicolas Dichtel rcu_read_unlock(); 220493ec1320SNicolas Dichtel 220593ec1320SNicolas Dichtel return err; 220688d0adb5SNicolas Dichtel } 220788d0adb5SNicolas Dichtel 2208b58b1f56SNicolas Dichtel static bool xfrm_userpolicy_is_valid(__u8 policy) 2209b58b1f56SNicolas Dichtel { 2210b58b1f56SNicolas Dichtel return policy == XFRM_USERPOLICY_BLOCK || 2211b58b1f56SNicolas Dichtel policy == XFRM_USERPOLICY_ACCEPT; 2212b58b1f56SNicolas Dichtel } 2213b58b1f56SNicolas Dichtel 22142d151d39SSteffen Klassert static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh, 22153bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 22162d151d39SSteffen Klassert { 22172d151d39SSteffen Klassert struct net *net = sock_net(skb->sk); 22182d151d39SSteffen Klassert struct xfrm_userpolicy_default *up = nlmsg_data(nlh); 22192d151d39SSteffen Klassert 2220b58b1f56SNicolas Dichtel if (xfrm_userpolicy_is_valid(up->in)) 2221b58b1f56SNicolas Dichtel net->xfrm.policy_default[XFRM_POLICY_IN] = up->in; 22225d8dbb7fSPavel Skripkin 2223b58b1f56SNicolas Dichtel if (xfrm_userpolicy_is_valid(up->fwd)) 2224b58b1f56SNicolas Dichtel net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd; 22255d8dbb7fSPavel Skripkin 2226b58b1f56SNicolas Dichtel if (xfrm_userpolicy_is_valid(up->out)) 2227b58b1f56SNicolas Dichtel net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out; 22282d151d39SSteffen Klassert 22292d151d39SSteffen Klassert rt_genid_bump_all(net); 22302d151d39SSteffen Klassert 223188d0adb5SNicolas Dichtel xfrm_notify_userpolicy(net); 22322d151d39SSteffen Klassert return 0; 22332d151d39SSteffen Klassert } 22342d151d39SSteffen Klassert 22352d151d39SSteffen Klassert static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh, 22363bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 22372d151d39SSteffen Klassert { 22382d151d39SSteffen Klassert struct sk_buff *r_skb; 22392d151d39SSteffen Klassert struct nlmsghdr *r_nlh; 22402d151d39SSteffen Klassert struct net *net = sock_net(skb->sk); 2241f8d858e6SNicolas Dichtel struct xfrm_userpolicy_default *r_up; 22422d151d39SSteffen Klassert int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default)); 22432d151d39SSteffen Klassert u32 portid = NETLINK_CB(skb).portid; 22442d151d39SSteffen Klassert u32 seq = nlh->nlmsg_seq; 22452d151d39SSteffen Klassert 22462d151d39SSteffen Klassert r_skb = nlmsg_new(len, GFP_ATOMIC); 22472d151d39SSteffen Klassert if (!r_skb) 22482d151d39SSteffen Klassert return -ENOMEM; 22492d151d39SSteffen Klassert 22502d151d39SSteffen Klassert r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0); 22512d151d39SSteffen Klassert if (!r_nlh) { 22522d151d39SSteffen Klassert kfree_skb(r_skb); 22532d151d39SSteffen Klassert return -EMSGSIZE; 22542d151d39SSteffen Klassert } 22552d151d39SSteffen Klassert 22562d151d39SSteffen Klassert r_up = nlmsg_data(r_nlh); 2257b58b1f56SNicolas Dichtel r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN]; 2258b58b1f56SNicolas Dichtel r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD]; 2259b58b1f56SNicolas Dichtel r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT]; 22602d151d39SSteffen Klassert nlmsg_end(r_skb, r_nlh); 22612d151d39SSteffen Klassert 22622d151d39SSteffen Klassert return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid); 22632d151d39SSteffen Klassert } 22642d151d39SSteffen Klassert 226522e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 22663bec6c3eSSabrina Dubroca struct nlattr **attrs, 22673bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 22681da177e4SLinus Torvalds { 2269fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 22701da177e4SLinus Torvalds struct xfrm_policy *xp; 22711da177e4SLinus Torvalds struct xfrm_userpolicy_id *p; 2272b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 22731da177e4SLinus Torvalds int err; 227426b15dadSJamal Hadi Salim struct km_event c; 22751da177e4SLinus Torvalds int delete; 2276295fae56SJamal Hadi Salim struct xfrm_mark m; 22777e652640SSteffen Klassert u32 if_id = 0; 22781da177e4SLinus Torvalds 22797b67c857SThomas Graf p = nlmsg_data(nlh); 22801da177e4SLinus Torvalds delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; 22811da177e4SLinus Torvalds 2282fb7deabaSSabrina Dubroca err = copy_from_user_policy_type(&type, attrs, extack); 2283f7b6983fSMasahide NAKAMURA if (err) 2284f7b6983fSMasahide NAKAMURA return err; 2285f7b6983fSMasahide NAKAMURA 228624fc544fSSabrina Dubroca err = verify_policy_dir(p->dir, extack); 22871da177e4SLinus Torvalds if (err) 22881da177e4SLinus Torvalds return err; 22891da177e4SLinus Torvalds 22907e652640SSteffen Klassert if (attrs[XFRMA_IF_ID]) 22917e652640SSteffen Klassert if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 22927e652640SSteffen Klassert 22934f47e8abSXin Long xfrm_mark_get(attrs, &m); 22944f47e8abSXin Long 22951da177e4SLinus Torvalds if (p->index) 22964f47e8abSXin Long xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, 22974f47e8abSXin Long p->index, delete, &err); 2298df71837dSTrent Jaeger else { 22995424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 230003e1ad7bSPaul Moore struct xfrm_sec_ctx *ctx; 2301df71837dSTrent Jaeger 230208a717e4SSabrina Dubroca err = verify_sec_ctx_len(attrs, extack); 2303df71837dSTrent Jaeger if (err) 2304df71837dSTrent Jaeger return err; 2305df71837dSTrent Jaeger 23062c8dd116SDenis V. Lunev ctx = NULL; 2307df71837dSTrent Jaeger if (rt) { 23085424f32eSThomas Graf struct xfrm_user_sec_ctx *uctx = nla_data(rt); 2309df71837dSTrent Jaeger 231052a4c640SNikolay Aleksandrov err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 231103e1ad7bSPaul Moore if (err) 2312df71837dSTrent Jaeger return err; 23132c8dd116SDenis V. Lunev } 23144f47e8abSXin Long xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir, 23154f47e8abSXin Long &p->sel, ctx, delete, &err); 231603e1ad7bSPaul Moore security_xfrm_policy_free(ctx); 2317df71837dSTrent Jaeger } 23181da177e4SLinus Torvalds if (xp == NULL) 23191da177e4SLinus Torvalds return -ENOENT; 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds if (!delete) { 23221da177e4SLinus Torvalds struct sk_buff *resp_skb; 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); 23251da177e4SLinus Torvalds if (IS_ERR(resp_skb)) { 23261da177e4SLinus Torvalds err = PTR_ERR(resp_skb); 23271da177e4SLinus Torvalds } else { 2328a6483b79SAlexey Dobriyan err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, 232915e47304SEric W. Biederman NETLINK_CB(skb).portid); 23301da177e4SLinus Torvalds } 233126b15dadSJamal Hadi Salim } else { 23322e71029eSTetsuo Handa xfrm_audit_policy_delete(xp, err ? 0 : 1, true); 233313fcfbb0SDavid S. Miller 233413fcfbb0SDavid S. Miller if (err != 0) 2335c8c05a8eSCatherine Zhang goto out; 233613fcfbb0SDavid S. Miller 2337e7443892SHerbert Xu c.data.byid = p->index; 2338f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 233926b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 234015e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 234126b15dadSJamal Hadi Salim km_policy_notify(xp, p->dir, &c); 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds 2344c8c05a8eSCatherine Zhang out: 2345ef41aaa0SEric Paris xfrm_pol_put(xp); 23461da177e4SLinus Torvalds return err; 23471da177e4SLinus Torvalds } 23481da177e4SLinus Torvalds 234922e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 23503bec6c3eSSabrina Dubroca struct nlattr **attrs, 23513bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 23521da177e4SLinus Torvalds { 2353fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 235426b15dadSJamal Hadi Salim struct km_event c; 23557b67c857SThomas Graf struct xfrm_usersa_flush *p = nlmsg_data(nlh); 23564aa2e62cSJoy Latten int err; 23571da177e4SLinus Torvalds 2358f75a2804SCong Wang err = xfrm_state_flush(net, p->proto, true, false); 23599e64cc95SJamal Hadi Salim if (err) { 23609e64cc95SJamal Hadi Salim if (err == -ESRCH) /* empty table */ 23619e64cc95SJamal Hadi Salim return 0; 2362069c474eSDavid S. Miller return err; 23639e64cc95SJamal Hadi Salim } 2364bf08867fSHerbert Xu c.data.proto = p->proto; 2365f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 236626b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 236715e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 23687067802eSAlexey Dobriyan c.net = net; 236926b15dadSJamal Hadi Salim km_state_notify(NULL, &c); 237026b15dadSJamal Hadi Salim 23711da177e4SLinus Torvalds return 0; 23721da177e4SLinus Torvalds } 23731da177e4SLinus Torvalds 2374a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x) 23757deb2264SThomas Graf { 2376a1b831f2SAlexey Dobriyan unsigned int replay_size = x->replay_esn ? 2377d8647b79SSteffen Klassert xfrm_replay_state_esn_len(x->replay_esn) : 2378d8647b79SSteffen Klassert sizeof(struct xfrm_replay_state); 2379d8647b79SSteffen Klassert 23807deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) 2381d8647b79SSteffen Klassert + nla_total_size(replay_size) 2382de95c4a4SNicolas Dichtel + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur)) 23836f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 23847deb2264SThomas Graf + nla_total_size(4) /* XFRM_AE_RTHR */ 23857deb2264SThomas Graf + nla_total_size(4); /* XFRM_AE_ETHR */ 23867deb2264SThomas Graf } 2387d51d081dSJamal Hadi Salim 2388214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 2389d51d081dSJamal Hadi Salim { 2390d51d081dSJamal Hadi Salim struct xfrm_aevent_id *id; 2391d51d081dSJamal Hadi Salim struct nlmsghdr *nlh; 23921d1e34ddSDavid S. Miller int err; 2393d51d081dSJamal Hadi Salim 239415e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); 239579b8b7f4SThomas Graf if (nlh == NULL) 239679b8b7f4SThomas Graf return -EMSGSIZE; 2397d51d081dSJamal Hadi Salim 23987b67c857SThomas Graf id = nlmsg_data(nlh); 2399931e79d7SMathias Krause memset(&id->sa_id, 0, sizeof(id->sa_id)); 24002b5f6dccSJamal Hadi Salim memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr)); 2401d51d081dSJamal Hadi Salim id->sa_id.spi = x->id.spi; 2402d51d081dSJamal Hadi Salim id->sa_id.family = x->props.family; 2403d51d081dSJamal Hadi Salim id->sa_id.proto = x->id.proto; 24042b5f6dccSJamal Hadi Salim memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr)); 24052b5f6dccSJamal Hadi Salim id->reqid = x->props.reqid; 2406d51d081dSJamal Hadi Salim id->flags = c->data.aevent; 2407d51d081dSJamal Hadi Salim 2408d0fde795SDavid S. Miller if (x->replay_esn) { 24091d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 2410d8647b79SSteffen Klassert xfrm_replay_state_esn_len(x->replay_esn), 24111d1e34ddSDavid S. Miller x->replay_esn); 2412d0fde795SDavid S. Miller } else { 24131d1e34ddSDavid S. Miller err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 24141d1e34ddSDavid S. Miller &x->replay); 2415d0fde795SDavid S. Miller } 24161d1e34ddSDavid S. Miller if (err) 24171d1e34ddSDavid S. Miller goto out_cancel; 2418de95c4a4SNicolas Dichtel err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft, 2419de95c4a4SNicolas Dichtel XFRMA_PAD); 24201d1e34ddSDavid S. Miller if (err) 24211d1e34ddSDavid S. Miller goto out_cancel; 2422d8647b79SSteffen Klassert 24231d1e34ddSDavid S. Miller if (id->flags & XFRM_AE_RTHR) { 24241d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); 24251d1e34ddSDavid S. Miller if (err) 24261d1e34ddSDavid S. Miller goto out_cancel; 24271d1e34ddSDavid S. Miller } 24281d1e34ddSDavid S. Miller if (id->flags & XFRM_AE_ETHR) { 24291d1e34ddSDavid S. Miller err = nla_put_u32(skb, XFRMA_ETIMER_THRESH, 24301d1e34ddSDavid S. Miller x->replay_maxage * 10 / HZ); 24311d1e34ddSDavid S. Miller if (err) 24321d1e34ddSDavid S. Miller goto out_cancel; 24331d1e34ddSDavid S. Miller } 24341d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &x->mark); 24351d1e34ddSDavid S. Miller if (err) 24361d1e34ddSDavid S. Miller goto out_cancel; 24376f26b61eSJamal Hadi Salim 24387e652640SSteffen Klassert err = xfrm_if_id_put(skb, x->if_id); 24397e652640SSteffen Klassert if (err) 24407e652640SSteffen Klassert goto out_cancel; 24417e652640SSteffen Klassert 2442053c095aSJohannes Berg nlmsg_end(skb, nlh); 2443053c095aSJohannes Berg return 0; 2444d51d081dSJamal Hadi Salim 24451d1e34ddSDavid S. Miller out_cancel: 24469825069dSThomas Graf nlmsg_cancel(skb, nlh); 24471d1e34ddSDavid S. Miller return err; 2448d51d081dSJamal Hadi Salim } 2449d51d081dSJamal Hadi Salim 245022e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 24513bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 2452d51d081dSJamal Hadi Salim { 2453fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 2454d51d081dSJamal Hadi Salim struct xfrm_state *x; 2455d51d081dSJamal Hadi Salim struct sk_buff *r_skb; 2456d51d081dSJamal Hadi Salim int err; 2457d51d081dSJamal Hadi Salim struct km_event c; 24586f26b61eSJamal Hadi Salim u32 mark; 24596f26b61eSJamal Hadi Salim struct xfrm_mark m; 24607b67c857SThomas Graf struct xfrm_aevent_id *p = nlmsg_data(nlh); 2461d51d081dSJamal Hadi Salim struct xfrm_usersa_id *id = &p->sa_id; 2462d51d081dSJamal Hadi Salim 24636f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 24646f26b61eSJamal Hadi Salim 24656f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family); 2466d8647b79SSteffen Klassert if (x == NULL) 2467d51d081dSJamal Hadi Salim return -ESRCH; 2468d8647b79SSteffen Klassert 2469d8647b79SSteffen Klassert r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 2470d8647b79SSteffen Klassert if (r_skb == NULL) { 2471d8647b79SSteffen Klassert xfrm_state_put(x); 2472d8647b79SSteffen Klassert return -ENOMEM; 2473d51d081dSJamal Hadi Salim } 2474d51d081dSJamal Hadi Salim 2475d51d081dSJamal Hadi Salim /* 2476d51d081dSJamal Hadi Salim * XXX: is this lock really needed - none of the other 2477d51d081dSJamal Hadi Salim * gets lock (the concern is things getting updated 2478d51d081dSJamal Hadi Salim * while we are still reading) - jhs 2479d51d081dSJamal Hadi Salim */ 2480d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 2481d51d081dSJamal Hadi Salim c.data.aevent = p->flags; 2482d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 248315e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 2484d51d081dSJamal Hadi Salim 24852fc5f83bSGustavo A. R. Silva err = build_aevent(r_skb, x, &c); 24862fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 24872fc5f83bSGustavo A. R. Silva 248815e47304SEric W. Biederman err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid); 2489d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 2490d51d081dSJamal Hadi Salim xfrm_state_put(x); 2491d51d081dSJamal Hadi Salim return err; 2492d51d081dSJamal Hadi Salim } 2493d51d081dSJamal Hadi Salim 249422e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 24953bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 2496d51d081dSJamal Hadi Salim { 2497fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 2498d51d081dSJamal Hadi Salim struct xfrm_state *x; 2499d51d081dSJamal Hadi Salim struct km_event c; 2500d51d081dSJamal Hadi Salim int err = -EINVAL; 25016f26b61eSJamal Hadi Salim u32 mark = 0; 25026f26b61eSJamal Hadi Salim struct xfrm_mark m; 25037b67c857SThomas Graf struct xfrm_aevent_id *p = nlmsg_data(nlh); 25045424f32eSThomas Graf struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 2505d8647b79SSteffen Klassert struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL]; 25065424f32eSThomas Graf struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 25074e077237SMichael Rossberg struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; 25084e077237SMichael Rossberg struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; 2509d51d081dSJamal Hadi Salim 2510643bc1a2SSabrina Dubroca if (!lt && !rp && !re && !et && !rt) { 2511643bc1a2SSabrina Dubroca NL_SET_ERR_MSG(extack, "Missing required attribute for AE"); 2512d51d081dSJamal Hadi Salim return err; 2513643bc1a2SSabrina Dubroca } 2514d51d081dSJamal Hadi Salim 2515d51d081dSJamal Hadi Salim /* pedantic mode - thou shalt sayeth replaceth */ 2516643bc1a2SSabrina Dubroca if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) { 2517643bc1a2SSabrina Dubroca NL_SET_ERR_MSG(extack, "NLM_F_REPLACE flag is required"); 2518d51d081dSJamal Hadi Salim return err; 2519643bc1a2SSabrina Dubroca } 2520d51d081dSJamal Hadi Salim 25216f26b61eSJamal Hadi Salim mark = xfrm_mark_get(attrs, &m); 25226f26b61eSJamal Hadi Salim 25236f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); 2524d51d081dSJamal Hadi Salim if (x == NULL) 2525d51d081dSJamal Hadi Salim return -ESRCH; 2526d51d081dSJamal Hadi Salim 2527643bc1a2SSabrina Dubroca if (x->km.state != XFRM_STATE_VALID) { 2528643bc1a2SSabrina Dubroca NL_SET_ERR_MSG(extack, "SA must be in VALID state"); 2529d51d081dSJamal Hadi Salim goto out; 2530643bc1a2SSabrina Dubroca } 2531d51d081dSJamal Hadi Salim 2532643bc1a2SSabrina Dubroca err = xfrm_replay_verify_len(x->replay_esn, re, extack); 2533e2b19125SSteffen Klassert if (err) 2534e2b19125SSteffen Klassert goto out; 2535e2b19125SSteffen Klassert 2536d51d081dSJamal Hadi Salim spin_lock_bh(&x->lock); 2537e3ac104dSMathias Krause xfrm_update_ae_params(x, attrs, 1); 2538d51d081dSJamal Hadi Salim spin_unlock_bh(&x->lock); 2539d51d081dSJamal Hadi Salim 2540d51d081dSJamal Hadi Salim c.event = nlh->nlmsg_type; 2541d51d081dSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 254215e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 2543d51d081dSJamal Hadi Salim c.data.aevent = XFRM_AE_CU; 2544d51d081dSJamal Hadi Salim km_state_notify(x, &c); 2545d51d081dSJamal Hadi Salim err = 0; 2546d51d081dSJamal Hadi Salim out: 2547d51d081dSJamal Hadi Salim xfrm_state_put(x); 2548d51d081dSJamal Hadi Salim return err; 2549d51d081dSJamal Hadi Salim } 2550d51d081dSJamal Hadi Salim 255122e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 25523bec6c3eSSabrina Dubroca struct nlattr **attrs, 25533bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 25541da177e4SLinus Torvalds { 2555fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 255626b15dadSJamal Hadi Salim struct km_event c; 2557b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 2558f7b6983fSMasahide NAKAMURA int err; 255926b15dadSJamal Hadi Salim 2560fb7deabaSSabrina Dubroca err = copy_from_user_policy_type(&type, attrs, extack); 2561f7b6983fSMasahide NAKAMURA if (err) 2562f7b6983fSMasahide NAKAMURA return err; 2563f7b6983fSMasahide NAKAMURA 25642e71029eSTetsuo Handa err = xfrm_policy_flush(net, type, true); 25652f1eb65fSJamal Hadi Salim if (err) { 25662f1eb65fSJamal Hadi Salim if (err == -ESRCH) /* empty table */ 25672f1eb65fSJamal Hadi Salim return 0; 2568069c474eSDavid S. Miller return err; 25692f1eb65fSJamal Hadi Salim } 25702f1eb65fSJamal Hadi Salim 2571f7b6983fSMasahide NAKAMURA c.data.type = type; 2572f60f6b8fSHerbert Xu c.event = nlh->nlmsg_type; 257326b15dadSJamal Hadi Salim c.seq = nlh->nlmsg_seq; 257415e47304SEric W. Biederman c.portid = nlh->nlmsg_pid; 25757067802eSAlexey Dobriyan c.net = net; 257626b15dadSJamal Hadi Salim km_policy_notify(NULL, 0, &c); 25771da177e4SLinus Torvalds return 0; 25781da177e4SLinus Torvalds } 25791da177e4SLinus Torvalds 258022e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 25813bec6c3eSSabrina Dubroca struct nlattr **attrs, 25823bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 25836c5c8ca7SJamal Hadi Salim { 2584fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 25856c5c8ca7SJamal Hadi Salim struct xfrm_policy *xp; 25867b67c857SThomas Graf struct xfrm_user_polexpire *up = nlmsg_data(nlh); 25876c5c8ca7SJamal Hadi Salim struct xfrm_userpolicy_info *p = &up->pol; 2588b798a9edSJamal Hadi Salim u8 type = XFRM_POLICY_TYPE_MAIN; 25896c5c8ca7SJamal Hadi Salim int err = -ENOENT; 2590295fae56SJamal Hadi Salim struct xfrm_mark m; 25917e652640SSteffen Klassert u32 if_id = 0; 25926c5c8ca7SJamal Hadi Salim 2593fb7deabaSSabrina Dubroca err = copy_from_user_policy_type(&type, attrs, extack); 2594f7b6983fSMasahide NAKAMURA if (err) 2595f7b6983fSMasahide NAKAMURA return err; 2596f7b6983fSMasahide NAKAMURA 259724fc544fSSabrina Dubroca err = verify_policy_dir(p->dir, extack); 2598c8bf4d04STimo Teräs if (err) 2599c8bf4d04STimo Teräs return err; 2600c8bf4d04STimo Teräs 26017e652640SSteffen Klassert if (attrs[XFRMA_IF_ID]) 26027e652640SSteffen Klassert if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 26037e652640SSteffen Klassert 26044f47e8abSXin Long xfrm_mark_get(attrs, &m); 26054f47e8abSXin Long 26066c5c8ca7SJamal Hadi Salim if (p->index) 26074f47e8abSXin Long xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index, 26084f47e8abSXin Long 0, &err); 26096c5c8ca7SJamal Hadi Salim else { 26105424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 261103e1ad7bSPaul Moore struct xfrm_sec_ctx *ctx; 26126c5c8ca7SJamal Hadi Salim 261308a717e4SSabrina Dubroca err = verify_sec_ctx_len(attrs, extack); 26146c5c8ca7SJamal Hadi Salim if (err) 26156c5c8ca7SJamal Hadi Salim return err; 26166c5c8ca7SJamal Hadi Salim 26172c8dd116SDenis V. Lunev ctx = NULL; 26186c5c8ca7SJamal Hadi Salim if (rt) { 26195424f32eSThomas Graf struct xfrm_user_sec_ctx *uctx = nla_data(rt); 26206c5c8ca7SJamal Hadi Salim 262152a4c640SNikolay Aleksandrov err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 262203e1ad7bSPaul Moore if (err) 26236c5c8ca7SJamal Hadi Salim return err; 26242c8dd116SDenis V. Lunev } 26254f47e8abSXin Long xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir, 26266f26b61eSJamal Hadi Salim &p->sel, ctx, 0, &err); 262703e1ad7bSPaul Moore security_xfrm_policy_free(ctx); 26286c5c8ca7SJamal Hadi Salim } 26296c5c8ca7SJamal Hadi Salim if (xp == NULL) 2630ef41aaa0SEric Paris return -ENOENT; 263103e1ad7bSPaul Moore 2632ea2dea9dSTimo Teräs if (unlikely(xp->walk.dead)) 26336c5c8ca7SJamal Hadi Salim goto out; 26346c5c8ca7SJamal Hadi Salim 26356c5c8ca7SJamal Hadi Salim err = 0; 26366c5c8ca7SJamal Hadi Salim if (up->hard) { 26376c5c8ca7SJamal Hadi Salim xfrm_policy_delete(xp, p->dir); 26382e71029eSTetsuo Handa xfrm_audit_policy_delete(xp, 1, true); 26396c5c8ca7SJamal Hadi Salim } 2640c6bb8136SEric W. Biederman km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid); 26416c5c8ca7SJamal Hadi Salim 26426c5c8ca7SJamal Hadi Salim out: 26436c5c8ca7SJamal Hadi Salim xfrm_pol_put(xp); 26446c5c8ca7SJamal Hadi Salim return err; 26456c5c8ca7SJamal Hadi Salim } 26466c5c8ca7SJamal Hadi Salim 264722e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 26483bec6c3eSSabrina Dubroca struct nlattr **attrs, 26493bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 265053bc6b4dSJamal Hadi Salim { 2651fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 265253bc6b4dSJamal Hadi Salim struct xfrm_state *x; 265353bc6b4dSJamal Hadi Salim int err; 26547b67c857SThomas Graf struct xfrm_user_expire *ue = nlmsg_data(nlh); 265553bc6b4dSJamal Hadi Salim struct xfrm_usersa_info *p = &ue->state; 26566f26b61eSJamal Hadi Salim struct xfrm_mark m; 2657928497f0SNicolas Dichtel u32 mark = xfrm_mark_get(attrs, &m); 265853bc6b4dSJamal Hadi Salim 26596f26b61eSJamal Hadi Salim x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family); 266053bc6b4dSJamal Hadi Salim 26613a765aa5SDavid S. Miller err = -ENOENT; 266253bc6b4dSJamal Hadi Salim if (x == NULL) 266353bc6b4dSJamal Hadi Salim return err; 266453bc6b4dSJamal Hadi Salim 266553bc6b4dSJamal Hadi Salim spin_lock_bh(&x->lock); 26663a765aa5SDavid S. Miller err = -EINVAL; 2667a25b19f3SSabrina Dubroca if (x->km.state != XFRM_STATE_VALID) { 2668a25b19f3SSabrina Dubroca NL_SET_ERR_MSG(extack, "SA must be in VALID state"); 266953bc6b4dSJamal Hadi Salim goto out; 2670a25b19f3SSabrina Dubroca } 2671a25b19f3SSabrina Dubroca 2672c6bb8136SEric W. Biederman km_state_expired(x, ue->hard, nlh->nlmsg_pid); 267353bc6b4dSJamal Hadi Salim 2674161a09e7SJoy Latten if (ue->hard) { 267553bc6b4dSJamal Hadi Salim __xfrm_state_delete(x); 26762e71029eSTetsuo Handa xfrm_audit_state_delete(x, 1, true); 2677161a09e7SJoy Latten } 26783a765aa5SDavid S. Miller err = 0; 267953bc6b4dSJamal Hadi Salim out: 268053bc6b4dSJamal Hadi Salim spin_unlock_bh(&x->lock); 268153bc6b4dSJamal Hadi Salim xfrm_state_put(x); 268253bc6b4dSJamal Hadi Salim return err; 268353bc6b4dSJamal Hadi Salim } 268453bc6b4dSJamal Hadi Salim 268522e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, 26863bec6c3eSSabrina Dubroca struct nlattr **attrs, 26873bec6c3eSSabrina Dubroca struct netlink_ext_ack *extack) 2688980ebd25SJamal Hadi Salim { 2689fc34acd3SAlexey Dobriyan struct net *net = sock_net(skb->sk); 2690980ebd25SJamal Hadi Salim struct xfrm_policy *xp; 2691980ebd25SJamal Hadi Salim struct xfrm_user_tmpl *ut; 2692980ebd25SJamal Hadi Salim int i; 26935424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_TMPL]; 26946f26b61eSJamal Hadi Salim struct xfrm_mark mark; 2695980ebd25SJamal Hadi Salim 26967b67c857SThomas Graf struct xfrm_user_acquire *ua = nlmsg_data(nlh); 2697fc34acd3SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 2698980ebd25SJamal Hadi Salim int err = -ENOMEM; 2699980ebd25SJamal Hadi Salim 2700980ebd25SJamal Hadi Salim if (!x) 2701d8eb9307SIlpo Järvinen goto nomem; 2702980ebd25SJamal Hadi Salim 27036f26b61eSJamal Hadi Salim xfrm_mark_get(attrs, &mark); 27046f26b61eSJamal Hadi Salim 2705ec2b4f01SSabrina Dubroca err = verify_newpolicy_info(&ua->policy, extack); 2706d8eb9307SIlpo Järvinen if (err) 270773efc324SVegard Nossum goto free_state; 270808a717e4SSabrina Dubroca err = verify_sec_ctx_len(attrs, extack); 2709a1a7e3a3SXin Long if (err) 2710a1a7e3a3SXin Long goto free_state; 2711980ebd25SJamal Hadi Salim 2712980ebd25SJamal Hadi Salim /* build an XP */ 2713fb7deabaSSabrina Dubroca xp = xfrm_policy_construct(net, &ua->policy, attrs, &err, extack); 2714d8eb9307SIlpo Järvinen if (!xp) 2715d8eb9307SIlpo Järvinen goto free_state; 2716980ebd25SJamal Hadi Salim 2717980ebd25SJamal Hadi Salim memcpy(&x->id, &ua->id, sizeof(ua->id)); 2718980ebd25SJamal Hadi Salim memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); 2719980ebd25SJamal Hadi Salim memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); 27206f26b61eSJamal Hadi Salim xp->mark.m = x->mark.m = mark.m; 27216f26b61eSJamal Hadi Salim xp->mark.v = x->mark.v = mark.v; 27225424f32eSThomas Graf ut = nla_data(rt); 2723980ebd25SJamal Hadi Salim /* extract the templates and for each call km_key */ 2724980ebd25SJamal Hadi Salim for (i = 0; i < xp->xfrm_nr; i++, ut++) { 2725980ebd25SJamal Hadi Salim struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 2726980ebd25SJamal Hadi Salim memcpy(&x->id, &t->id, sizeof(x->id)); 2727980ebd25SJamal Hadi Salim x->props.mode = t->mode; 2728980ebd25SJamal Hadi Salim x->props.reqid = t->reqid; 2729980ebd25SJamal Hadi Salim x->props.family = ut->family; 2730980ebd25SJamal Hadi Salim t->aalgos = ua->aalgos; 2731980ebd25SJamal Hadi Salim t->ealgos = ua->ealgos; 2732980ebd25SJamal Hadi Salim t->calgos = ua->calgos; 2733980ebd25SJamal Hadi Salim err = km_query(x, t, xp); 2734980ebd25SJamal Hadi Salim 2735980ebd25SJamal Hadi Salim } 2736980ebd25SJamal Hadi Salim 27374a135e53SMathias Krause xfrm_state_free(x); 2738980ebd25SJamal Hadi Salim kfree(xp); 2739980ebd25SJamal Hadi Salim 2740980ebd25SJamal Hadi Salim return 0; 2741d8eb9307SIlpo Järvinen 2742d8eb9307SIlpo Järvinen free_state: 27434a135e53SMathias Krause xfrm_state_free(x); 2744d8eb9307SIlpo Järvinen nomem: 2745d8eb9307SIlpo Järvinen return err; 2746980ebd25SJamal Hadi Salim } 2747980ebd25SJamal Hadi Salim 27485c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 27495c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma, 275013c1d189SArnaud Ebalard struct xfrm_kmaddress *k, 2751bd122403SSabrina Dubroca struct nlattr **attrs, int *num, 2752bd122403SSabrina Dubroca struct netlink_ext_ack *extack) 27535c79de6eSShinta Sugimoto { 27545424f32eSThomas Graf struct nlattr *rt = attrs[XFRMA_MIGRATE]; 27555c79de6eSShinta Sugimoto struct xfrm_user_migrate *um; 27565c79de6eSShinta Sugimoto int i, num_migrate; 27575c79de6eSShinta Sugimoto 275813c1d189SArnaud Ebalard if (k != NULL) { 275913c1d189SArnaud Ebalard struct xfrm_user_kmaddress *uk; 276013c1d189SArnaud Ebalard 276113c1d189SArnaud Ebalard uk = nla_data(attrs[XFRMA_KMADDRESS]); 276213c1d189SArnaud Ebalard memcpy(&k->local, &uk->local, sizeof(k->local)); 276313c1d189SArnaud Ebalard memcpy(&k->remote, &uk->remote, sizeof(k->remote)); 276413c1d189SArnaud Ebalard k->family = uk->family; 276513c1d189SArnaud Ebalard k->reserved = uk->reserved; 276613c1d189SArnaud Ebalard } 276713c1d189SArnaud Ebalard 27685424f32eSThomas Graf um = nla_data(rt); 27695424f32eSThomas Graf num_migrate = nla_len(rt) / sizeof(*um); 27705c79de6eSShinta Sugimoto 2771bd122403SSabrina Dubroca if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) { 2772bd122403SSabrina Dubroca NL_SET_ERR_MSG(extack, "Invalid number of SAs to migrate, must be 0 < num <= XFRM_MAX_DEPTH (6)"); 27735c79de6eSShinta Sugimoto return -EINVAL; 2774bd122403SSabrina Dubroca } 27755c79de6eSShinta Sugimoto 27765c79de6eSShinta Sugimoto for (i = 0; i < num_migrate; i++, um++, ma++) { 27775c79de6eSShinta Sugimoto memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); 27785c79de6eSShinta Sugimoto memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); 27795c79de6eSShinta Sugimoto memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); 27805c79de6eSShinta Sugimoto memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); 27815c79de6eSShinta Sugimoto 27825c79de6eSShinta Sugimoto ma->proto = um->proto; 27835c79de6eSShinta Sugimoto ma->mode = um->mode; 27845c79de6eSShinta Sugimoto ma->reqid = um->reqid; 27855c79de6eSShinta Sugimoto 27865c79de6eSShinta Sugimoto ma->old_family = um->old_family; 27875c79de6eSShinta Sugimoto ma->new_family = um->new_family; 27885c79de6eSShinta Sugimoto } 27895c79de6eSShinta Sugimoto 27905c79de6eSShinta Sugimoto *num = i; 27915c79de6eSShinta Sugimoto return 0; 27925c79de6eSShinta Sugimoto } 27935c79de6eSShinta Sugimoto 27945c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 27953bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 27965c79de6eSShinta Sugimoto { 27977b67c857SThomas Graf struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); 27985c79de6eSShinta Sugimoto struct xfrm_migrate m[XFRM_MAX_DEPTH]; 279913c1d189SArnaud Ebalard struct xfrm_kmaddress km, *kmp; 28005c79de6eSShinta Sugimoto u8 type; 28015c79de6eSShinta Sugimoto int err; 28025c79de6eSShinta Sugimoto int n = 0; 28038d549c4fSFan Du struct net *net = sock_net(skb->sk); 28044ab47d47SAntony Antony struct xfrm_encap_tmpl *encap = NULL; 2805c1aca308SYan Yan u32 if_id = 0; 28065c79de6eSShinta Sugimoto 2807bd122403SSabrina Dubroca if (!attrs[XFRMA_MIGRATE]) { 2808bd122403SSabrina Dubroca NL_SET_ERR_MSG(extack, "Missing required MIGRATE attribute"); 2809cf5cb79fSThomas Graf return -EINVAL; 2810bd122403SSabrina Dubroca } 28115c79de6eSShinta Sugimoto 281213c1d189SArnaud Ebalard kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL; 281313c1d189SArnaud Ebalard 2814fb7deabaSSabrina Dubroca err = copy_from_user_policy_type(&type, attrs, extack); 28155c79de6eSShinta Sugimoto if (err) 28165c79de6eSShinta Sugimoto return err; 28175c79de6eSShinta Sugimoto 2818bd122403SSabrina Dubroca err = copy_from_user_migrate(m, kmp, attrs, &n, extack); 28195c79de6eSShinta Sugimoto if (err) 28205c79de6eSShinta Sugimoto return err; 28215c79de6eSShinta Sugimoto 28225c79de6eSShinta Sugimoto if (!n) 28235c79de6eSShinta Sugimoto return 0; 28245c79de6eSShinta Sugimoto 28254ab47d47SAntony Antony if (attrs[XFRMA_ENCAP]) { 28264ab47d47SAntony Antony encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), 28274ab47d47SAntony Antony sizeof(*encap), GFP_KERNEL); 28284ab47d47SAntony Antony if (!encap) 28294ac7a6eeSZheng Yongjun return -ENOMEM; 28305c79de6eSShinta Sugimoto } 28314ab47d47SAntony Antony 2832c1aca308SYan Yan if (attrs[XFRMA_IF_ID]) 2833c1aca308SYan Yan if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 2834c1aca308SYan Yan 2835bd122403SSabrina Dubroca err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, 2836bd122403SSabrina Dubroca if_id, extack); 28374ab47d47SAntony Antony 28384ab47d47SAntony Antony kfree(encap); 28394ab47d47SAntony Antony 28404ab47d47SAntony Antony return err; 28414ab47d47SAntony Antony } 28425c79de6eSShinta Sugimoto #else 28435c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 28443bec6c3eSSabrina Dubroca struct nlattr **attrs, struct netlink_ext_ack *extack) 28455c79de6eSShinta Sugimoto { 28465c79de6eSShinta Sugimoto return -ENOPROTOOPT; 28475c79de6eSShinta Sugimoto } 28485c79de6eSShinta Sugimoto #endif 28495c79de6eSShinta Sugimoto 28505c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 2851183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb) 28525c79de6eSShinta Sugimoto { 28535c79de6eSShinta Sugimoto struct xfrm_user_migrate um; 28545c79de6eSShinta Sugimoto 28555c79de6eSShinta Sugimoto memset(&um, 0, sizeof(um)); 28565c79de6eSShinta Sugimoto um.proto = m->proto; 28575c79de6eSShinta Sugimoto um.mode = m->mode; 28585c79de6eSShinta Sugimoto um.reqid = m->reqid; 28595c79de6eSShinta Sugimoto um.old_family = m->old_family; 28605c79de6eSShinta Sugimoto memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); 28615c79de6eSShinta Sugimoto memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); 28625c79de6eSShinta Sugimoto um.new_family = m->new_family; 28635c79de6eSShinta Sugimoto memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); 28645c79de6eSShinta Sugimoto memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); 28655c79de6eSShinta Sugimoto 2866c0144beaSThomas Graf return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); 28675c79de6eSShinta Sugimoto } 28685c79de6eSShinta Sugimoto 2869183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb) 287013c1d189SArnaud Ebalard { 287113c1d189SArnaud Ebalard struct xfrm_user_kmaddress uk; 287213c1d189SArnaud Ebalard 287313c1d189SArnaud Ebalard memset(&uk, 0, sizeof(uk)); 287413c1d189SArnaud Ebalard uk.family = k->family; 287513c1d189SArnaud Ebalard uk.reserved = k->reserved; 287613c1d189SArnaud Ebalard memcpy(&uk.local, &k->local, sizeof(uk.local)); 2877a1caa322SArnaud Ebalard memcpy(&uk.remote, &k->remote, sizeof(uk.remote)); 287813c1d189SArnaud Ebalard 287913c1d189SArnaud Ebalard return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk); 288013c1d189SArnaud Ebalard } 288113c1d189SArnaud Ebalard 2882a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma, 28838bafd730SAntony Antony int with_encp) 28847deb2264SThomas Graf { 28857deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) 288613c1d189SArnaud Ebalard + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0) 28878bafd730SAntony Antony + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0) 28887deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) 28897deb2264SThomas Graf + userpolicy_type_attrsize(); 28907deb2264SThomas Graf } 28917deb2264SThomas Graf 2892183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m, 2893183cad12SDavid S. Miller int num_migrate, const struct xfrm_kmaddress *k, 28948bafd730SAntony Antony const struct xfrm_selector *sel, 28958bafd730SAntony Antony const struct xfrm_encap_tmpl *encap, u8 dir, u8 type) 28965c79de6eSShinta Sugimoto { 2897183cad12SDavid S. Miller const struct xfrm_migrate *mp; 28985c79de6eSShinta Sugimoto struct xfrm_userpolicy_id *pol_id; 28995c79de6eSShinta Sugimoto struct nlmsghdr *nlh; 29001d1e34ddSDavid S. Miller int i, err; 29015c79de6eSShinta Sugimoto 290279b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); 290379b8b7f4SThomas Graf if (nlh == NULL) 290479b8b7f4SThomas Graf return -EMSGSIZE; 29055c79de6eSShinta Sugimoto 29067b67c857SThomas Graf pol_id = nlmsg_data(nlh); 29075c79de6eSShinta Sugimoto /* copy data from selector, dir, and type to the pol_id */ 29085c79de6eSShinta Sugimoto memset(pol_id, 0, sizeof(*pol_id)); 29095c79de6eSShinta Sugimoto memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); 29105c79de6eSShinta Sugimoto pol_id->dir = dir; 29115c79de6eSShinta Sugimoto 29121d1e34ddSDavid S. Miller if (k != NULL) { 29131d1e34ddSDavid S. Miller err = copy_to_user_kmaddress(k, skb); 29141d1e34ddSDavid S. Miller if (err) 29151d1e34ddSDavid S. Miller goto out_cancel; 29161d1e34ddSDavid S. Miller } 29178bafd730SAntony Antony if (encap) { 29188bafd730SAntony Antony err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap); 29198bafd730SAntony Antony if (err) 29208bafd730SAntony Antony goto out_cancel; 29218bafd730SAntony Antony } 29221d1e34ddSDavid S. Miller err = copy_to_user_policy_type(type, skb); 29231d1e34ddSDavid S. Miller if (err) 29241d1e34ddSDavid S. Miller goto out_cancel; 29255c79de6eSShinta Sugimoto for (i = 0, mp = m ; i < num_migrate; i++, mp++) { 29261d1e34ddSDavid S. Miller err = copy_to_user_migrate(mp, skb); 29271d1e34ddSDavid S. Miller if (err) 29281d1e34ddSDavid S. Miller goto out_cancel; 29295c79de6eSShinta Sugimoto } 29305c79de6eSShinta Sugimoto 2931053c095aSJohannes Berg nlmsg_end(skb, nlh); 2932053c095aSJohannes Berg return 0; 29331d1e34ddSDavid S. Miller 29341d1e34ddSDavid S. Miller out_cancel: 29359825069dSThomas Graf nlmsg_cancel(skb, nlh); 29361d1e34ddSDavid S. Miller return err; 29375c79de6eSShinta Sugimoto } 29385c79de6eSShinta Sugimoto 2939183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2940183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 29418bafd730SAntony Antony const struct xfrm_kmaddress *k, 29428bafd730SAntony Antony const struct xfrm_encap_tmpl *encap) 29435c79de6eSShinta Sugimoto { 2944a6483b79SAlexey Dobriyan struct net *net = &init_net; 29455c79de6eSShinta Sugimoto struct sk_buff *skb; 29462fc5f83bSGustavo A. R. Silva int err; 29475c79de6eSShinta Sugimoto 29488bafd730SAntony Antony skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap), 29498bafd730SAntony Antony GFP_ATOMIC); 29505c79de6eSShinta Sugimoto if (skb == NULL) 29515c79de6eSShinta Sugimoto return -ENOMEM; 29525c79de6eSShinta Sugimoto 29535c79de6eSShinta Sugimoto /* build migrate */ 29542fc5f83bSGustavo A. R. Silva err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type); 29552fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 29565c79de6eSShinta Sugimoto 295721ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE); 29585c79de6eSShinta Sugimoto } 29595c79de6eSShinta Sugimoto #else 2960183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2961183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 29628bafd730SAntony Antony const struct xfrm_kmaddress *k, 29638bafd730SAntony Antony const struct xfrm_encap_tmpl *encap) 29645c79de6eSShinta Sugimoto { 29655c79de6eSShinta Sugimoto return -ENOPROTOOPT; 29665c79de6eSShinta Sugimoto } 29675c79de6eSShinta Sugimoto #endif 2968d51d081dSJamal Hadi Salim 2969a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type) 2970492b558bSThomas Graf 29715461fc0cSDmitry Safonov const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { 297266f9a259SDavid S. Miller [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2973492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2974492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2975492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2976492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2977492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2978492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), 2979980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), 298053bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), 2981492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 298266f9a259SDavid S. Miller [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 29836c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), 2984492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), 2985a7bd9a45SThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, 2986d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2987d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 298897a64b45SMasahide NAKAMURA [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), 29895c79de6eSShinta Sugimoto [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2990a7bd9a45SThomas Graf [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), 2991880a6fabSChristophe Gouault [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 2992a7bd9a45SThomas Graf [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 29932d151d39SSteffen Klassert [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default), 29942d151d39SSteffen Klassert [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default), 29951da177e4SLinus Torvalds }; 29965461fc0cSDmitry Safonov EXPORT_SYMBOL_GPL(xfrm_msg_min); 29971da177e4SLinus Torvalds 2998492b558bSThomas Graf #undef XMSGSIZE 2999492b558bSThomas Graf 30005106f4a8SDmitry Safonov const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { 3001c28e9304Sjamal [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)}, 3002c28e9304Sjamal [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)}, 3003c28e9304Sjamal [XFRMA_LASTUSED] = { .type = NLA_U64}, 3004c28e9304Sjamal [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)}, 30051a6509d9SHerbert Xu [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, 3006cf5cb79fSThomas Graf [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, 3007cf5cb79fSThomas Graf [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, 3008cf5cb79fSThomas Graf [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, 3009cf5cb79fSThomas Graf [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, 3010cf5cb79fSThomas Graf [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, 3011cf5cb79fSThomas Graf [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, 3012cf5cb79fSThomas Graf [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, 3013cf5cb79fSThomas Graf [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, 3014cf5cb79fSThomas Graf [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, 3015cf5cb79fSThomas Graf [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, 3016cf5cb79fSThomas Graf [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, 3017cf5cb79fSThomas Graf [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, 3018cf5cb79fSThomas Graf [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, 3019cf5cb79fSThomas Graf [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, 302013c1d189SArnaud Ebalard [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) }, 30216f26b61eSJamal Hadi Salim [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) }, 302235d2856bSMartin Willi [XFRMA_TFCPAD] = { .type = NLA_U32 }, 3023d8647b79SSteffen Klassert [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) }, 3024a947b0a9SNicolas Dichtel [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 }, 3025d3623099SNicolas Dichtel [XFRMA_PROTO] = { .type = NLA_U8 }, 3026870a2df4SNicolas Dichtel [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) }, 3027d77e38e6SSteffen Klassert [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) }, 30289b42c1f1SSteffen Klassert [XFRMA_SET_MARK] = { .type = NLA_U32 }, 30299b42c1f1SSteffen Klassert [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 }, 30307e652640SSteffen Klassert [XFRMA_IF_ID] = { .type = NLA_U32 }, 3031cf5cb79fSThomas Graf }; 30325106f4a8SDmitry Safonov EXPORT_SYMBOL_GPL(xfrma_policy); 3033cf5cb79fSThomas Graf 3034880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = { 3035880a6fabSChristophe Gouault [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 3036880a6fabSChristophe Gouault [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 3037880a6fabSChristophe Gouault }; 3038880a6fabSChristophe Gouault 303905600a79SMathias Krause static const struct xfrm_link { 30403bec6c3eSSabrina Dubroca int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **, 30413bec6c3eSSabrina Dubroca struct netlink_ext_ack *); 30421137b5e2SHerbert Xu int (*start)(struct netlink_callback *); 30431da177e4SLinus Torvalds int (*dump)(struct sk_buff *, struct netlink_callback *); 30444c563f76STimo Teras int (*done)(struct netlink_callback *); 3045880a6fabSChristophe Gouault const struct nla_policy *nla_pol; 3046880a6fabSChristophe Gouault int nla_max; 3047492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = { 3048492b558bSThomas Graf [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 3049492b558bSThomas Graf [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, 3050492b558bSThomas Graf [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, 30514c563f76STimo Teras .dump = xfrm_dump_sa, 30524c563f76STimo Teras .done = xfrm_dump_sa_done }, 3053492b558bSThomas Graf [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 3054492b558bSThomas Graf [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, 3055492b558bSThomas Graf [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, 30561137b5e2SHerbert Xu .start = xfrm_dump_policy_start, 30574c563f76STimo Teras .dump = xfrm_dump_policy, 30584c563f76STimo Teras .done = xfrm_dump_policy_done }, 3059492b558bSThomas Graf [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, 3060980ebd25SJamal Hadi Salim [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, 306153bc6b4dSJamal Hadi Salim [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, 3062492b558bSThomas Graf [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 3063492b558bSThomas Graf [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 30646c5c8ca7SJamal Hadi Salim [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, 3065492b558bSThomas Graf [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, 3066492b558bSThomas Graf [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, 3067d51d081dSJamal Hadi Salim [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, 3068d51d081dSJamal Hadi Salim [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, 30695c79de6eSShinta Sugimoto [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, 307028d8909bSJamal Hadi Salim [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, 3071880a6fabSChristophe Gouault [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo, 3072880a6fabSChristophe Gouault .nla_pol = xfrma_spd_policy, 3073880a6fabSChristophe Gouault .nla_max = XFRMA_SPD_MAX }, 3074ecfd6b18SJamal Hadi Salim [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, 30752d151d39SSteffen Klassert [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default }, 30762d151d39SSteffen Klassert [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default }, 30771da177e4SLinus Torvalds }; 30781da177e4SLinus Torvalds 30792d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 30802d4bc933SJohannes Berg struct netlink_ext_ack *extack) 30811da177e4SLinus Torvalds { 3082a6483b79SAlexey Dobriyan struct net *net = sock_net(skb->sk); 308335a7aa08SThomas Graf struct nlattr *attrs[XFRMA_MAX+1]; 308405600a79SMathias Krause const struct xfrm_link *link; 30855106f4a8SDmitry Safonov struct nlmsghdr *nlh64 = NULL; 3086a7bd9a45SThomas Graf int type, err; 30871da177e4SLinus Torvalds 30881da177e4SLinus Torvalds type = nlh->nlmsg_type; 30891da177e4SLinus Torvalds if (type > XFRM_MSG_MAX) 30901d00a4ebSThomas Graf return -EINVAL; 30911da177e4SLinus Torvalds 30921da177e4SLinus Torvalds type -= XFRM_MSG_BASE; 30931da177e4SLinus Torvalds link = &xfrm_dispatch[type]; 30941da177e4SLinus Torvalds 30951da177e4SLinus Torvalds /* All operations require privileges, even GET */ 309690f62cf3SEric W. Biederman if (!netlink_net_capable(skb, CAP_NET_ADMIN)) 30971d00a4ebSThomas Graf return -EPERM; 30981da177e4SLinus Torvalds 30995106f4a8SDmitry Safonov if (in_compat_syscall()) { 31005106f4a8SDmitry Safonov struct xfrm_translator *xtr = xfrm_get_translator(); 31015106f4a8SDmitry Safonov 31025106f4a8SDmitry Safonov if (!xtr) 31035106f4a8SDmitry Safonov return -EOPNOTSUPP; 31045106f4a8SDmitry Safonov 31055106f4a8SDmitry Safonov nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max, 31065106f4a8SDmitry Safonov link->nla_pol, extack); 31075106f4a8SDmitry Safonov xfrm_put_translator(xtr); 31085106f4a8SDmitry Safonov if (IS_ERR(nlh64)) 31095106f4a8SDmitry Safonov return PTR_ERR(nlh64); 31105106f4a8SDmitry Safonov if (nlh64) 31115106f4a8SDmitry Safonov nlh = nlh64; 31125106f4a8SDmitry Safonov } 31135106f4a8SDmitry Safonov 3114492b558bSThomas Graf if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || 3115492b558bSThomas Graf type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && 3116b8f3ab42SDavid S. Miller (nlh->nlmsg_flags & NLM_F_DUMP)) { 311780d326faSPablo Neira Ayuso struct netlink_dump_control c = { 31181137b5e2SHerbert Xu .start = link->start, 311980d326faSPablo Neira Ayuso .dump = link->dump, 312080d326faSPablo Neira Ayuso .done = link->done, 312180d326faSPablo Neira Ayuso }; 31225106f4a8SDmitry Safonov 31235106f4a8SDmitry Safonov if (link->dump == NULL) { 31245106f4a8SDmitry Safonov err = -EINVAL; 31255106f4a8SDmitry Safonov goto err; 312680d326faSPablo Neira Ayuso } 31275106f4a8SDmitry Safonov 31285106f4a8SDmitry Safonov err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c); 31295106f4a8SDmitry Safonov goto err; 31301da177e4SLinus Torvalds } 31311da177e4SLinus Torvalds 31328cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs, 3133880a6fabSChristophe Gouault link->nla_max ? : XFRMA_MAX, 3134fe52145fSJohannes Berg link->nla_pol ? : xfrma_policy, extack); 3135a7bd9a45SThomas Graf if (err < 0) 31365106f4a8SDmitry Safonov goto err; 31375106f4a8SDmitry Safonov 31385106f4a8SDmitry Safonov if (link->doit == NULL) { 31395106f4a8SDmitry Safonov err = -EINVAL; 31405106f4a8SDmitry Safonov goto err; 31415106f4a8SDmitry Safonov } 31425106f4a8SDmitry Safonov 31433bec6c3eSSabrina Dubroca err = link->doit(skb, nlh, attrs, extack); 31445106f4a8SDmitry Safonov 31457c1a80e8SPavel Skripkin /* We need to free skb allocated in xfrm_alloc_compat() before 31467c1a80e8SPavel Skripkin * returning from this function, because consume_skb() won't take 31477c1a80e8SPavel Skripkin * care of frag_list since netlink destructor sets 31487c1a80e8SPavel Skripkin * sbk->head to NULL. (see netlink_skb_destructor()) 31497c1a80e8SPavel Skripkin */ 31507c1a80e8SPavel Skripkin if (skb_has_frag_list(skb)) { 31517c1a80e8SPavel Skripkin kfree_skb(skb_shinfo(skb)->frag_list); 31527c1a80e8SPavel Skripkin skb_shinfo(skb)->frag_list = NULL; 31537c1a80e8SPavel Skripkin } 31547c1a80e8SPavel Skripkin 31555106f4a8SDmitry Safonov err: 31565106f4a8SDmitry Safonov kvfree(nlh64); 3157a7bd9a45SThomas Graf return err; 31581da177e4SLinus Torvalds } 31591da177e4SLinus Torvalds 3160cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb) 31611da177e4SLinus Torvalds { 3162283bc9f3SFan Du struct net *net = sock_net(skb->sk); 3163283bc9f3SFan Du 3164283bc9f3SFan Du mutex_lock(&net->xfrm.xfrm_cfg_mutex); 3165cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &xfrm_user_rcv_msg); 3166283bc9f3SFan Du mutex_unlock(&net->xfrm.xfrm_cfg_mutex); 31671da177e4SLinus Torvalds } 31681da177e4SLinus Torvalds 3169a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void) 31707deb2264SThomas Graf { 31716f26b61eSJamal Hadi Salim return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) 31726f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)); 31737deb2264SThomas Graf } 31747deb2264SThomas Graf 3175214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 31761da177e4SLinus Torvalds { 31771da177e4SLinus Torvalds struct xfrm_user_expire *ue; 31781da177e4SLinus Torvalds struct nlmsghdr *nlh; 31791d1e34ddSDavid S. Miller int err; 31801da177e4SLinus Torvalds 318115e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); 318279b8b7f4SThomas Graf if (nlh == NULL) 318379b8b7f4SThomas Graf return -EMSGSIZE; 31841da177e4SLinus Torvalds 31857b67c857SThomas Graf ue = nlmsg_data(nlh); 31861da177e4SLinus Torvalds copy_to_user_state(x, &ue->state); 3187d51d081dSJamal Hadi Salim ue->hard = (c->data.hard != 0) ? 1 : 0; 3188e3e5fc16SMathias Krause /* clear the padding bytes */ 3189caf283d0SKees Cook memset_after(ue, 0, hard); 31901da177e4SLinus Torvalds 31911d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &x->mark); 31921d1e34ddSDavid S. Miller if (err) 31931d1e34ddSDavid S. Miller return err; 31946f26b61eSJamal Hadi Salim 31957e652640SSteffen Klassert err = xfrm_if_id_put(skb, x->if_id); 31967e652640SSteffen Klassert if (err) 31977e652640SSteffen Klassert return err; 31987e652640SSteffen Klassert 3199053c095aSJohannes Berg nlmsg_end(skb, nlh); 3200053c095aSJohannes Berg return 0; 32011da177e4SLinus Torvalds } 32021da177e4SLinus Torvalds 3203214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c) 32041da177e4SLinus Torvalds { 3205fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 32061da177e4SLinus Torvalds struct sk_buff *skb; 32071da177e4SLinus Torvalds 32087deb2264SThomas Graf skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); 32091da177e4SLinus Torvalds if (skb == NULL) 32101da177e4SLinus Torvalds return -ENOMEM; 32111da177e4SLinus Torvalds 32126f26b61eSJamal Hadi Salim if (build_expire(skb, x, c) < 0) { 32136f26b61eSJamal Hadi Salim kfree_skb(skb); 32146f26b61eSJamal Hadi Salim return -EMSGSIZE; 32156f26b61eSJamal Hadi Salim } 32161da177e4SLinus Torvalds 321721ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 32181da177e4SLinus Torvalds } 32191da177e4SLinus Torvalds 3220214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c) 3221d51d081dSJamal Hadi Salim { 3222fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 3223d51d081dSJamal Hadi Salim struct sk_buff *skb; 32242fc5f83bSGustavo A. R. Silva int err; 3225d51d081dSJamal Hadi Salim 3226d8647b79SSteffen Klassert skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 3227d51d081dSJamal Hadi Salim if (skb == NULL) 3228d51d081dSJamal Hadi Salim return -ENOMEM; 3229d51d081dSJamal Hadi Salim 32302fc5f83bSGustavo A. R. Silva err = build_aevent(skb, x, c); 32312fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 3232d51d081dSJamal Hadi Salim 323321ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS); 3234d51d081dSJamal Hadi Salim } 3235d51d081dSJamal Hadi Salim 3236214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c) 323726b15dadSJamal Hadi Salim { 32387067802eSAlexey Dobriyan struct net *net = c->net; 323926b15dadSJamal Hadi Salim struct xfrm_usersa_flush *p; 324026b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 324126b15dadSJamal Hadi Salim struct sk_buff *skb; 32427deb2264SThomas Graf int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); 324326b15dadSJamal Hadi Salim 32447deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 324526b15dadSJamal Hadi Salim if (skb == NULL) 324626b15dadSJamal Hadi Salim return -ENOMEM; 324726b15dadSJamal Hadi Salim 324815e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); 324979b8b7f4SThomas Graf if (nlh == NULL) { 325079b8b7f4SThomas Graf kfree_skb(skb); 325179b8b7f4SThomas Graf return -EMSGSIZE; 325279b8b7f4SThomas Graf } 325326b15dadSJamal Hadi Salim 32547b67c857SThomas Graf p = nlmsg_data(nlh); 3255bf08867fSHerbert Xu p->proto = c->data.proto; 325626b15dadSJamal Hadi Salim 32579825069dSThomas Graf nlmsg_end(skb, nlh); 325826b15dadSJamal Hadi Salim 325921ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 326026b15dadSJamal Hadi Salim } 326126b15dadSJamal Hadi Salim 3262a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x) 326326b15dadSJamal Hadi Salim { 3264a1b831f2SAlexey Dobriyan unsigned int l = 0; 32651a6509d9SHerbert Xu if (x->aead) 32661a6509d9SHerbert Xu l += nla_total_size(aead_len(x->aead)); 32674447bb33SMartin Willi if (x->aalg) { 32684447bb33SMartin Willi l += nla_total_size(sizeof(struct xfrm_algo) + 32694447bb33SMartin Willi (x->aalg->alg_key_len + 7) / 8); 32704447bb33SMartin Willi l += nla_total_size(xfrm_alg_auth_len(x->aalg)); 32714447bb33SMartin Willi } 327226b15dadSJamal Hadi Salim if (x->ealg) 32730f99be0dSEric Dumazet l += nla_total_size(xfrm_alg_len(x->ealg)); 327426b15dadSJamal Hadi Salim if (x->calg) 32757deb2264SThomas Graf l += nla_total_size(sizeof(*x->calg)); 327626b15dadSJamal Hadi Salim if (x->encap) 32777deb2264SThomas Graf l += nla_total_size(sizeof(*x->encap)); 327835d2856bSMartin Willi if (x->tfcpad) 327935d2856bSMartin Willi l += nla_total_size(sizeof(x->tfcpad)); 3280d8647b79SSteffen Klassert if (x->replay_esn) 3281d8647b79SSteffen Klassert l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn)); 3282f293a5e3Sdingzhi else 3283f293a5e3Sdingzhi l += nla_total_size(sizeof(struct xfrm_replay_state)); 328468325d3bSHerbert Xu if (x->security) 328568325d3bSHerbert Xu l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + 328668325d3bSHerbert Xu x->security->ctx_len); 328768325d3bSHerbert Xu if (x->coaddr) 328868325d3bSHerbert Xu l += nla_total_size(sizeof(*x->coaddr)); 3289a947b0a9SNicolas Dichtel if (x->props.extra_flags) 3290a947b0a9SNicolas Dichtel l += nla_total_size(sizeof(x->props.extra_flags)); 3291d77e38e6SSteffen Klassert if (x->xso.dev) 32927770a39dSEric Dumazet l += nla_total_size(sizeof(struct xfrm_user_offload)); 32939b42c1f1SSteffen Klassert if (x->props.smark.v | x->props.smark.m) { 32949b42c1f1SSteffen Klassert l += nla_total_size(sizeof(x->props.smark.v)); 32959b42c1f1SSteffen Klassert l += nla_total_size(sizeof(x->props.smark.m)); 32969b42c1f1SSteffen Klassert } 32977e652640SSteffen Klassert if (x->if_id) 32987e652640SSteffen Klassert l += nla_total_size(sizeof(x->if_id)); 329968325d3bSHerbert Xu 3300d26f3984SHerbert Xu /* Must count x->lastused as it may become non-zero behind our back. */ 3301de95c4a4SNicolas Dichtel l += nla_total_size_64bit(sizeof(u64)); 330226b15dadSJamal Hadi Salim 33034e484b3eSAntony Antony if (x->mapping_maxage) 33044e484b3eSAntony Antony l += nla_total_size(sizeof(x->mapping_maxage)); 33054e484b3eSAntony Antony 330626b15dadSJamal Hadi Salim return l; 330726b15dadSJamal Hadi Salim } 330826b15dadSJamal Hadi Salim 3309214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c) 331026b15dadSJamal Hadi Salim { 3311fc34acd3SAlexey Dobriyan struct net *net = xs_net(x); 331226b15dadSJamal Hadi Salim struct xfrm_usersa_info *p; 33130603eac0SHerbert Xu struct xfrm_usersa_id *id; 331426b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 331526b15dadSJamal Hadi Salim struct sk_buff *skb; 3316a1b831f2SAlexey Dobriyan unsigned int len = xfrm_sa_len(x); 3317a1b831f2SAlexey Dobriyan unsigned int headlen; 3318a1b831f2SAlexey Dobriyan int err; 33190603eac0SHerbert Xu 33200603eac0SHerbert Xu headlen = sizeof(*p); 33210603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 33227deb2264SThomas Graf len += nla_total_size(headlen); 33230603eac0SHerbert Xu headlen = sizeof(*id); 33246f26b61eSJamal Hadi Salim len += nla_total_size(sizeof(struct xfrm_mark)); 33250603eac0SHerbert Xu } 33267deb2264SThomas Graf len += NLMSG_ALIGN(headlen); 332726b15dadSJamal Hadi Salim 33287deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 332926b15dadSJamal Hadi Salim if (skb == NULL) 333026b15dadSJamal Hadi Salim return -ENOMEM; 333126b15dadSJamal Hadi Salim 333215e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 33331d1e34ddSDavid S. Miller err = -EMSGSIZE; 333479b8b7f4SThomas Graf if (nlh == NULL) 33351d1e34ddSDavid S. Miller goto out_free_skb; 333626b15dadSJamal Hadi Salim 33377b67c857SThomas Graf p = nlmsg_data(nlh); 33380603eac0SHerbert Xu if (c->event == XFRM_MSG_DELSA) { 3339c0144beaSThomas Graf struct nlattr *attr; 3340c0144beaSThomas Graf 33417b67c857SThomas Graf id = nlmsg_data(nlh); 334250329c8aSMathias Krause memset(id, 0, sizeof(*id)); 33430603eac0SHerbert Xu memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); 33440603eac0SHerbert Xu id->spi = x->id.spi; 33450603eac0SHerbert Xu id->family = x->props.family; 33460603eac0SHerbert Xu id->proto = x->id.proto; 33470603eac0SHerbert Xu 3348c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); 33491d1e34ddSDavid S. Miller err = -EMSGSIZE; 3350c0144beaSThomas Graf if (attr == NULL) 33511d1e34ddSDavid S. Miller goto out_free_skb; 3352c0144beaSThomas Graf 3353c0144beaSThomas Graf p = nla_data(attr); 33540603eac0SHerbert Xu } 33551d1e34ddSDavid S. Miller err = copy_to_user_state_extra(x, p, skb); 33561d1e34ddSDavid S. Miller if (err) 33571d1e34ddSDavid S. Miller goto out_free_skb; 335826b15dadSJamal Hadi Salim 33599825069dSThomas Graf nlmsg_end(skb, nlh); 336026b15dadSJamal Hadi Salim 336121ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 336226b15dadSJamal Hadi Salim 33631d1e34ddSDavid S. Miller out_free_skb: 336426b15dadSJamal Hadi Salim kfree_skb(skb); 33651d1e34ddSDavid S. Miller return err; 336626b15dadSJamal Hadi Salim } 336726b15dadSJamal Hadi Salim 3368214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c) 336926b15dadSJamal Hadi Salim { 337026b15dadSJamal Hadi Salim 337126b15dadSJamal Hadi Salim switch (c->event) { 3372f60f6b8fSHerbert Xu case XFRM_MSG_EXPIRE: 337326b15dadSJamal Hadi Salim return xfrm_exp_state_notify(x, c); 3374d51d081dSJamal Hadi Salim case XFRM_MSG_NEWAE: 3375d51d081dSJamal Hadi Salim return xfrm_aevent_state_notify(x, c); 3376f60f6b8fSHerbert Xu case XFRM_MSG_DELSA: 3377f60f6b8fSHerbert Xu case XFRM_MSG_UPDSA: 3378f60f6b8fSHerbert Xu case XFRM_MSG_NEWSA: 337926b15dadSJamal Hadi Salim return xfrm_notify_sa(x, c); 3380f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHSA: 338126b15dadSJamal Hadi Salim return xfrm_notify_sa_flush(c); 338226b15dadSJamal Hadi Salim default: 338362db5cfdSstephen hemminger printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n", 338462db5cfdSstephen hemminger c->event); 338526b15dadSJamal Hadi Salim break; 338626b15dadSJamal Hadi Salim } 338726b15dadSJamal Hadi Salim 338826b15dadSJamal Hadi Salim return 0; 338926b15dadSJamal Hadi Salim 339026b15dadSJamal Hadi Salim } 339126b15dadSJamal Hadi Salim 3392a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x, 33937deb2264SThomas Graf struct xfrm_policy *xp) 33947deb2264SThomas Graf { 33957deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) 33967deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 33976f26b61eSJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 33987deb2264SThomas Graf + nla_total_size(xfrm_user_sec_ctx_size(x->security)) 33997deb2264SThomas Graf + userpolicy_type_attrsize(); 34007deb2264SThomas Graf } 34017deb2264SThomas Graf 34021da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, 340365e0736bSFan Du struct xfrm_tmpl *xt, struct xfrm_policy *xp) 34041da177e4SLinus Torvalds { 34051d1e34ddSDavid S. Miller __u32 seq = xfrm_get_acqseq(); 34061da177e4SLinus Torvalds struct xfrm_user_acquire *ua; 34071da177e4SLinus Torvalds struct nlmsghdr *nlh; 34081d1e34ddSDavid S. Miller int err; 34091da177e4SLinus Torvalds 341079b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); 341179b8b7f4SThomas Graf if (nlh == NULL) 341279b8b7f4SThomas Graf return -EMSGSIZE; 34131da177e4SLinus Torvalds 34147b67c857SThomas Graf ua = nlmsg_data(nlh); 34151da177e4SLinus Torvalds memcpy(&ua->id, &x->id, sizeof(ua->id)); 34161da177e4SLinus Torvalds memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); 34171da177e4SLinus Torvalds memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); 341865e0736bSFan Du copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT); 34191da177e4SLinus Torvalds ua->aalgos = xt->aalgos; 34201da177e4SLinus Torvalds ua->ealgos = xt->ealgos; 34211da177e4SLinus Torvalds ua->calgos = xt->calgos; 34221da177e4SLinus Torvalds ua->seq = x->km.seq = seq; 34231da177e4SLinus Torvalds 34241d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 34251d1e34ddSDavid S. Miller if (!err) 34261d1e34ddSDavid S. Miller err = copy_to_user_state_sec_ctx(x, skb); 34271d1e34ddSDavid S. Miller if (!err) 34281d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 34291d1e34ddSDavid S. Miller if (!err) 34301d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 34317e652640SSteffen Klassert if (!err) 34327e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 3433919e43faSLeon Romanovsky if (!err && xp->xdo.dev) 3434919e43faSLeon Romanovsky err = copy_user_offload(&xp->xdo, skb); 34351d1e34ddSDavid S. Miller if (err) { 34361d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 34371d1e34ddSDavid S. Miller return err; 34381d1e34ddSDavid S. Miller } 34391da177e4SLinus Torvalds 3440053c095aSJohannes Berg nlmsg_end(skb, nlh); 3441053c095aSJohannes Berg return 0; 34421da177e4SLinus Torvalds } 34431da177e4SLinus Torvalds 34441da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, 344565e0736bSFan Du struct xfrm_policy *xp) 34461da177e4SLinus Torvalds { 3447a6483b79SAlexey Dobriyan struct net *net = xs_net(x); 34481da177e4SLinus Torvalds struct sk_buff *skb; 34492fc5f83bSGustavo A. R. Silva int err; 34501da177e4SLinus Torvalds 34517deb2264SThomas Graf skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); 34521da177e4SLinus Torvalds if (skb == NULL) 34531da177e4SLinus Torvalds return -ENOMEM; 34541da177e4SLinus Torvalds 34552fc5f83bSGustavo A. R. Silva err = build_acquire(skb, x, xt, xp); 34562fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 34571da177e4SLinus Torvalds 345821ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE); 34591da177e4SLinus Torvalds } 34601da177e4SLinus Torvalds 34611da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0 34621da177e4SLinus Torvalds * or more templates. 34631da177e4SLinus Torvalds */ 3464cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, 34651da177e4SLinus Torvalds u8 *data, int len, int *dir) 34661da177e4SLinus Torvalds { 3467fc34acd3SAlexey Dobriyan struct net *net = sock_net(sk); 34681da177e4SLinus Torvalds struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; 34691da177e4SLinus Torvalds struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); 34701da177e4SLinus Torvalds struct xfrm_policy *xp; 34711da177e4SLinus Torvalds int nr; 34721da177e4SLinus Torvalds 3473cb969f07SVenkat Yekkirala switch (sk->sk_family) { 34741da177e4SLinus Torvalds case AF_INET: 34751da177e4SLinus Torvalds if (opt != IP_XFRM_POLICY) { 34761da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 34771da177e4SLinus Torvalds return NULL; 34781da177e4SLinus Torvalds } 34791da177e4SLinus Torvalds break; 3480dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 34811da177e4SLinus Torvalds case AF_INET6: 34821da177e4SLinus Torvalds if (opt != IPV6_XFRM_POLICY) { 34831da177e4SLinus Torvalds *dir = -EOPNOTSUPP; 34841da177e4SLinus Torvalds return NULL; 34851da177e4SLinus Torvalds } 34861da177e4SLinus Torvalds break; 34871da177e4SLinus Torvalds #endif 34881da177e4SLinus Torvalds default: 34891da177e4SLinus Torvalds *dir = -EINVAL; 34901da177e4SLinus Torvalds return NULL; 34911da177e4SLinus Torvalds } 34921da177e4SLinus Torvalds 34931da177e4SLinus Torvalds *dir = -EINVAL; 34941da177e4SLinus Torvalds 34951da177e4SLinus Torvalds if (len < sizeof(*p) || 3496ec2b4f01SSabrina Dubroca verify_newpolicy_info(p, NULL)) 34971da177e4SLinus Torvalds return NULL; 34981da177e4SLinus Torvalds 34991da177e4SLinus Torvalds nr = ((len - sizeof(*p)) / sizeof(*ut)); 3500d37bed89SSabrina Dubroca if (validate_tmpl(nr, ut, p->sel.family, NULL)) 35011da177e4SLinus Torvalds return NULL; 35021da177e4SLinus Torvalds 3503a4f1bac6SHerbert Xu if (p->dir > XFRM_POLICY_OUT) 3504a4f1bac6SHerbert Xu return NULL; 3505a4f1bac6SHerbert Xu 35062f09a4d5SHerbert Xu xp = xfrm_policy_alloc(net, GFP_ATOMIC); 35071da177e4SLinus Torvalds if (xp == NULL) { 35081da177e4SLinus Torvalds *dir = -ENOBUFS; 35091da177e4SLinus Torvalds return NULL; 35101da177e4SLinus Torvalds } 35111da177e4SLinus Torvalds 35121da177e4SLinus Torvalds copy_from_user_policy(xp, p); 3513f7b6983fSMasahide NAKAMURA xp->type = XFRM_POLICY_TYPE_MAIN; 35141da177e4SLinus Torvalds copy_templates(xp, ut, nr); 35151da177e4SLinus Torvalds 35161da177e4SLinus Torvalds *dir = p->dir; 35171da177e4SLinus Torvalds 35181da177e4SLinus Torvalds return xp; 35191da177e4SLinus Torvalds } 35201da177e4SLinus Torvalds 3521a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp) 35227deb2264SThomas Graf { 35237deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) 35247deb2264SThomas Graf + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 35257deb2264SThomas Graf + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) 3526295fae56SJamal Hadi Salim + nla_total_size(sizeof(struct xfrm_mark)) 35277deb2264SThomas Graf + userpolicy_type_attrsize(); 35287deb2264SThomas Graf } 35297deb2264SThomas Graf 35301da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, 3531214e005bSDavid S. Miller int dir, const struct km_event *c) 35321da177e4SLinus Torvalds { 35331da177e4SLinus Torvalds struct xfrm_user_polexpire *upe; 3534d51d081dSJamal Hadi Salim int hard = c->data.hard; 35351d1e34ddSDavid S. Miller struct nlmsghdr *nlh; 35361d1e34ddSDavid S. Miller int err; 35371da177e4SLinus Torvalds 353815e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); 353979b8b7f4SThomas Graf if (nlh == NULL) 354079b8b7f4SThomas Graf return -EMSGSIZE; 35411da177e4SLinus Torvalds 35427b67c857SThomas Graf upe = nlmsg_data(nlh); 35431da177e4SLinus Torvalds copy_to_user_policy(xp, &upe->pol, dir); 35441d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 35451d1e34ddSDavid S. Miller if (!err) 35461d1e34ddSDavid S. Miller err = copy_to_user_sec_ctx(xp, skb); 35471d1e34ddSDavid S. Miller if (!err) 35481d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 35491d1e34ddSDavid S. Miller if (!err) 35501d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 35517e652640SSteffen Klassert if (!err) 35527e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 3553919e43faSLeon Romanovsky if (!err && xp->xdo.dev) 3554919e43faSLeon Romanovsky err = copy_user_offload(&xp->xdo, skb); 35551d1e34ddSDavid S. Miller if (err) { 35561d1e34ddSDavid S. Miller nlmsg_cancel(skb, nlh); 35571d1e34ddSDavid S. Miller return err; 35581d1e34ddSDavid S. Miller } 35591da177e4SLinus Torvalds upe->hard = !!hard; 35601da177e4SLinus Torvalds 3561053c095aSJohannes Berg nlmsg_end(skb, nlh); 3562053c095aSJohannes Berg return 0; 35631da177e4SLinus Torvalds } 35641da177e4SLinus Torvalds 3565214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 35661da177e4SLinus Torvalds { 3567fc34acd3SAlexey Dobriyan struct net *net = xp_net(xp); 35681da177e4SLinus Torvalds struct sk_buff *skb; 35692fc5f83bSGustavo A. R. Silva int err; 35701da177e4SLinus Torvalds 35717deb2264SThomas Graf skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); 35721da177e4SLinus Torvalds if (skb == NULL) 35731da177e4SLinus Torvalds return -ENOMEM; 35741da177e4SLinus Torvalds 35752fc5f83bSGustavo A. R. Silva err = build_polexpire(skb, xp, dir, c); 35762fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 35771da177e4SLinus Torvalds 357821ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 35791da177e4SLinus Torvalds } 35801da177e4SLinus Torvalds 3581214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c) 358226b15dadSJamal Hadi Salim { 3583a1b831f2SAlexey Dobriyan unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 3584fc34acd3SAlexey Dobriyan struct net *net = xp_net(xp); 358526b15dadSJamal Hadi Salim struct xfrm_userpolicy_info *p; 35860603eac0SHerbert Xu struct xfrm_userpolicy_id *id; 358726b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 358826b15dadSJamal Hadi Salim struct sk_buff *skb; 3589a1b831f2SAlexey Dobriyan unsigned int headlen; 3590a1b831f2SAlexey Dobriyan int err; 35910603eac0SHerbert Xu 35920603eac0SHerbert Xu headlen = sizeof(*p); 35930603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 35947deb2264SThomas Graf len += nla_total_size(headlen); 35950603eac0SHerbert Xu headlen = sizeof(*id); 35960603eac0SHerbert Xu } 3597cfbfd45aSThomas Graf len += userpolicy_type_attrsize(); 3598295fae56SJamal Hadi Salim len += nla_total_size(sizeof(struct xfrm_mark)); 35997deb2264SThomas Graf len += NLMSG_ALIGN(headlen); 360026b15dadSJamal Hadi Salim 36017deb2264SThomas Graf skb = nlmsg_new(len, GFP_ATOMIC); 360226b15dadSJamal Hadi Salim if (skb == NULL) 360326b15dadSJamal Hadi Salim return -ENOMEM; 360426b15dadSJamal Hadi Salim 360515e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 36061d1e34ddSDavid S. Miller err = -EMSGSIZE; 360779b8b7f4SThomas Graf if (nlh == NULL) 36081d1e34ddSDavid S. Miller goto out_free_skb; 360926b15dadSJamal Hadi Salim 36107b67c857SThomas Graf p = nlmsg_data(nlh); 36110603eac0SHerbert Xu if (c->event == XFRM_MSG_DELPOLICY) { 3612c0144beaSThomas Graf struct nlattr *attr; 3613c0144beaSThomas Graf 36147b67c857SThomas Graf id = nlmsg_data(nlh); 36150603eac0SHerbert Xu memset(id, 0, sizeof(*id)); 36160603eac0SHerbert Xu id->dir = dir; 36170603eac0SHerbert Xu if (c->data.byid) 36180603eac0SHerbert Xu id->index = xp->index; 36190603eac0SHerbert Xu else 36200603eac0SHerbert Xu memcpy(&id->sel, &xp->selector, sizeof(id->sel)); 36210603eac0SHerbert Xu 3622c0144beaSThomas Graf attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); 36231d1e34ddSDavid S. Miller err = -EMSGSIZE; 3624c0144beaSThomas Graf if (attr == NULL) 36251d1e34ddSDavid S. Miller goto out_free_skb; 3626c0144beaSThomas Graf 3627c0144beaSThomas Graf p = nla_data(attr); 36280603eac0SHerbert Xu } 362926b15dadSJamal Hadi Salim 363026b15dadSJamal Hadi Salim copy_to_user_policy(xp, p, dir); 36311d1e34ddSDavid S. Miller err = copy_to_user_tmpl(xp, skb); 36321d1e34ddSDavid S. Miller if (!err) 36331d1e34ddSDavid S. Miller err = copy_to_user_policy_type(xp->type, skb); 36341d1e34ddSDavid S. Miller if (!err) 36351d1e34ddSDavid S. Miller err = xfrm_mark_put(skb, &xp->mark); 36367e652640SSteffen Klassert if (!err) 36377e652640SSteffen Klassert err = xfrm_if_id_put(skb, xp->if_id); 3638919e43faSLeon Romanovsky if (!err && xp->xdo.dev) 3639919e43faSLeon Romanovsky err = copy_user_offload(&xp->xdo, skb); 36401d1e34ddSDavid S. Miller if (err) 36411d1e34ddSDavid S. Miller goto out_free_skb; 3642295fae56SJamal Hadi Salim 36439825069dSThomas Graf nlmsg_end(skb, nlh); 364426b15dadSJamal Hadi Salim 364521ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 364626b15dadSJamal Hadi Salim 36471d1e34ddSDavid S. Miller out_free_skb: 364826b15dadSJamal Hadi Salim kfree_skb(skb); 36491d1e34ddSDavid S. Miller return err; 365026b15dadSJamal Hadi Salim } 365126b15dadSJamal Hadi Salim 3652214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c) 365326b15dadSJamal Hadi Salim { 36547067802eSAlexey Dobriyan struct net *net = c->net; 365526b15dadSJamal Hadi Salim struct nlmsghdr *nlh; 365626b15dadSJamal Hadi Salim struct sk_buff *skb; 36571d1e34ddSDavid S. Miller int err; 365826b15dadSJamal Hadi Salim 36597deb2264SThomas Graf skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); 366026b15dadSJamal Hadi Salim if (skb == NULL) 366126b15dadSJamal Hadi Salim return -ENOMEM; 366226b15dadSJamal Hadi Salim 366315e47304SEric W. Biederman nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); 36641d1e34ddSDavid S. Miller err = -EMSGSIZE; 366579b8b7f4SThomas Graf if (nlh == NULL) 36661d1e34ddSDavid S. Miller goto out_free_skb; 36671d1e34ddSDavid S. Miller err = copy_to_user_policy_type(c->data.type, skb); 36681d1e34ddSDavid S. Miller if (err) 36691d1e34ddSDavid S. Miller goto out_free_skb; 367026b15dadSJamal Hadi Salim 36719825069dSThomas Graf nlmsg_end(skb, nlh); 367226b15dadSJamal Hadi Salim 367321ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 367426b15dadSJamal Hadi Salim 36751d1e34ddSDavid S. Miller out_free_skb: 367626b15dadSJamal Hadi Salim kfree_skb(skb); 36771d1e34ddSDavid S. Miller return err; 367826b15dadSJamal Hadi Salim } 367926b15dadSJamal Hadi Salim 3680214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 368126b15dadSJamal Hadi Salim { 368226b15dadSJamal Hadi Salim 368326b15dadSJamal Hadi Salim switch (c->event) { 3684f60f6b8fSHerbert Xu case XFRM_MSG_NEWPOLICY: 3685f60f6b8fSHerbert Xu case XFRM_MSG_UPDPOLICY: 3686f60f6b8fSHerbert Xu case XFRM_MSG_DELPOLICY: 368726b15dadSJamal Hadi Salim return xfrm_notify_policy(xp, dir, c); 3688f60f6b8fSHerbert Xu case XFRM_MSG_FLUSHPOLICY: 368926b15dadSJamal Hadi Salim return xfrm_notify_policy_flush(c); 3690f60f6b8fSHerbert Xu case XFRM_MSG_POLEXPIRE: 369126b15dadSJamal Hadi Salim return xfrm_exp_policy_notify(xp, dir, c); 369226b15dadSJamal Hadi Salim default: 369362db5cfdSstephen hemminger printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n", 369462db5cfdSstephen hemminger c->event); 369526b15dadSJamal Hadi Salim } 369626b15dadSJamal Hadi Salim 369726b15dadSJamal Hadi Salim return 0; 369826b15dadSJamal Hadi Salim 369926b15dadSJamal Hadi Salim } 370026b15dadSJamal Hadi Salim 3701a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void) 37027deb2264SThomas Graf { 37037deb2264SThomas Graf return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); 37047deb2264SThomas Graf } 37057deb2264SThomas Graf 370697a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto, 370797a64b45SMasahide NAKAMURA struct xfrm_selector *sel, xfrm_address_t *addr) 370897a64b45SMasahide NAKAMURA { 370997a64b45SMasahide NAKAMURA struct xfrm_user_report *ur; 371097a64b45SMasahide NAKAMURA struct nlmsghdr *nlh; 371197a64b45SMasahide NAKAMURA 371279b8b7f4SThomas Graf nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); 371379b8b7f4SThomas Graf if (nlh == NULL) 371479b8b7f4SThomas Graf return -EMSGSIZE; 371597a64b45SMasahide NAKAMURA 37167b67c857SThomas Graf ur = nlmsg_data(nlh); 371797a64b45SMasahide NAKAMURA ur->proto = proto; 371897a64b45SMasahide NAKAMURA memcpy(&ur->sel, sel, sizeof(ur->sel)); 371997a64b45SMasahide NAKAMURA 37201d1e34ddSDavid S. Miller if (addr) { 37211d1e34ddSDavid S. Miller int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr); 37221d1e34ddSDavid S. Miller if (err) { 37239825069dSThomas Graf nlmsg_cancel(skb, nlh); 37241d1e34ddSDavid S. Miller return err; 37251d1e34ddSDavid S. Miller } 37261d1e34ddSDavid S. Miller } 3727053c095aSJohannes Berg nlmsg_end(skb, nlh); 3728053c095aSJohannes Berg return 0; 372997a64b45SMasahide NAKAMURA } 373097a64b45SMasahide NAKAMURA 3731db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto, 3732db983c11SAlexey Dobriyan struct xfrm_selector *sel, xfrm_address_t *addr) 373397a64b45SMasahide NAKAMURA { 373497a64b45SMasahide NAKAMURA struct sk_buff *skb; 37352fc5f83bSGustavo A. R. Silva int err; 373697a64b45SMasahide NAKAMURA 37377deb2264SThomas Graf skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); 373897a64b45SMasahide NAKAMURA if (skb == NULL) 373997a64b45SMasahide NAKAMURA return -ENOMEM; 374097a64b45SMasahide NAKAMURA 37412fc5f83bSGustavo A. R. Silva err = build_report(skb, proto, sel, addr); 37422fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 374397a64b45SMasahide NAKAMURA 374421ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT); 374597a64b45SMasahide NAKAMURA } 374697a64b45SMasahide NAKAMURA 3747a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void) 37483a2dfbe8SMartin Willi { 37493a2dfbe8SMartin Willi return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping)); 37503a2dfbe8SMartin Willi } 37513a2dfbe8SMartin Willi 37523a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x, 37533a2dfbe8SMartin Willi xfrm_address_t *new_saddr, __be16 new_sport) 37543a2dfbe8SMartin Willi { 37553a2dfbe8SMartin Willi struct xfrm_user_mapping *um; 37563a2dfbe8SMartin Willi struct nlmsghdr *nlh; 37573a2dfbe8SMartin Willi 37583a2dfbe8SMartin Willi nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0); 37593a2dfbe8SMartin Willi if (nlh == NULL) 37603a2dfbe8SMartin Willi return -EMSGSIZE; 37613a2dfbe8SMartin Willi 37623a2dfbe8SMartin Willi um = nlmsg_data(nlh); 37633a2dfbe8SMartin Willi 37643a2dfbe8SMartin Willi memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr)); 37653a2dfbe8SMartin Willi um->id.spi = x->id.spi; 37663a2dfbe8SMartin Willi um->id.family = x->props.family; 37673a2dfbe8SMartin Willi um->id.proto = x->id.proto; 37683a2dfbe8SMartin Willi memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr)); 37693a2dfbe8SMartin Willi memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr)); 37703a2dfbe8SMartin Willi um->new_sport = new_sport; 37713a2dfbe8SMartin Willi um->old_sport = x->encap->encap_sport; 37723a2dfbe8SMartin Willi um->reqid = x->props.reqid; 37733a2dfbe8SMartin Willi 3774053c095aSJohannes Berg nlmsg_end(skb, nlh); 3775053c095aSJohannes Berg return 0; 37763a2dfbe8SMartin Willi } 37773a2dfbe8SMartin Willi 37783a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, 37793a2dfbe8SMartin Willi __be16 sport) 37803a2dfbe8SMartin Willi { 3781a6483b79SAlexey Dobriyan struct net *net = xs_net(x); 37823a2dfbe8SMartin Willi struct sk_buff *skb; 37832fc5f83bSGustavo A. R. Silva int err; 37843a2dfbe8SMartin Willi 37853a2dfbe8SMartin Willi if (x->id.proto != IPPROTO_ESP) 37863a2dfbe8SMartin Willi return -EINVAL; 37873a2dfbe8SMartin Willi 37883a2dfbe8SMartin Willi if (!x->encap) 37893a2dfbe8SMartin Willi return -EINVAL; 37903a2dfbe8SMartin Willi 37913a2dfbe8SMartin Willi skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC); 37923a2dfbe8SMartin Willi if (skb == NULL) 37933a2dfbe8SMartin Willi return -ENOMEM; 37943a2dfbe8SMartin Willi 37952fc5f83bSGustavo A. R. Silva err = build_mapping(skb, x, ipaddr, sport); 37962fc5f83bSGustavo A. R. Silva BUG_ON(err < 0); 37973a2dfbe8SMartin Willi 379821ee543eSMichal Kubecek return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING); 37993a2dfbe8SMartin Willi } 38003a2dfbe8SMartin Willi 38010f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c) 38020f24558eSHoria Geanta { 38030f24558eSHoria Geanta return (bool)xfrm_acquire_is_on(c->net); 38040f24558eSHoria Geanta } 38050f24558eSHoria Geanta 38061da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = { 38071da177e4SLinus Torvalds .notify = xfrm_send_state_notify, 38081da177e4SLinus Torvalds .acquire = xfrm_send_acquire, 38091da177e4SLinus Torvalds .compile_policy = xfrm_compile_policy, 38101da177e4SLinus Torvalds .notify_policy = xfrm_send_policy_notify, 381197a64b45SMasahide NAKAMURA .report = xfrm_send_report, 38125c79de6eSShinta Sugimoto .migrate = xfrm_send_migrate, 38133a2dfbe8SMartin Willi .new_mapping = xfrm_send_mapping, 38140f24558eSHoria Geanta .is_alive = xfrm_is_alive, 38151da177e4SLinus Torvalds }; 38161da177e4SLinus Torvalds 3817a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net) 38181da177e4SLinus Torvalds { 3819be33690dSPatrick McHardy struct sock *nlsk; 3820a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 3821a31f2d17SPablo Neira Ayuso .groups = XFRMNLGRP_MAX, 3822a31f2d17SPablo Neira Ayuso .input = xfrm_netlink_rcv, 3823a31f2d17SPablo Neira Ayuso }; 3824be33690dSPatrick McHardy 38259f00d977SPablo Neira Ayuso nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg); 3826be33690dSPatrick McHardy if (nlsk == NULL) 38271da177e4SLinus Torvalds return -ENOMEM; 3828d79d792eSEric W. Biederman net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */ 3829cf778b00SEric Dumazet rcu_assign_pointer(net->xfrm.nlsk, nlsk); 38301da177e4SLinus Torvalds return 0; 38311da177e4SLinus Torvalds } 38321da177e4SLinus Torvalds 38336218fe18SFlorian Westphal static void __net_exit xfrm_user_net_pre_exit(struct net *net) 38346218fe18SFlorian Westphal { 38356218fe18SFlorian Westphal RCU_INIT_POINTER(net->xfrm.nlsk, NULL); 38366218fe18SFlorian Westphal } 38376218fe18SFlorian Westphal 3838d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list) 3839a6483b79SAlexey Dobriyan { 3840d79d792eSEric W. Biederman struct net *net; 38416218fe18SFlorian Westphal 3842d79d792eSEric W. Biederman list_for_each_entry(net, net_exit_list, exit_list) 3843d79d792eSEric W. Biederman netlink_kernel_release(net->xfrm.nlsk_stash); 3844a6483b79SAlexey Dobriyan } 3845a6483b79SAlexey Dobriyan 3846a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = { 3847a6483b79SAlexey Dobriyan .init = xfrm_user_net_init, 38486218fe18SFlorian Westphal .pre_exit = xfrm_user_net_pre_exit, 3849d79d792eSEric W. Biederman .exit_batch = xfrm_user_net_exit, 3850a6483b79SAlexey Dobriyan }; 3851a6483b79SAlexey Dobriyan 3852a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void) 3853a6483b79SAlexey Dobriyan { 3854a6483b79SAlexey Dobriyan int rv; 3855a6483b79SAlexey Dobriyan 3856a6483b79SAlexey Dobriyan printk(KERN_INFO "Initializing XFRM netlink socket\n"); 3857a6483b79SAlexey Dobriyan 3858a6483b79SAlexey Dobriyan rv = register_pernet_subsys(&xfrm_user_net_ops); 3859a6483b79SAlexey Dobriyan if (rv < 0) 3860a6483b79SAlexey Dobriyan return rv; 3861f41b284aSZhengchao Shao xfrm_register_km(&netlink_mgr); 3862f41b284aSZhengchao Shao return 0; 3863a6483b79SAlexey Dobriyan } 3864a6483b79SAlexey Dobriyan 38651da177e4SLinus Torvalds static void __exit xfrm_user_exit(void) 38661da177e4SLinus Torvalds { 38671da177e4SLinus Torvalds xfrm_unregister_km(&netlink_mgr); 3868a6483b79SAlexey Dobriyan unregister_pernet_subsys(&xfrm_user_net_ops); 38691da177e4SLinus Torvalds } 38701da177e4SLinus Torvalds 38711da177e4SLinus Torvalds module_init(xfrm_user_init); 38721da177e4SLinus Torvalds module_exit(xfrm_user_exit); 38731da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 38744fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); 3875