xref: /linux/net/xfrm/xfrm_user.c (revision 785b87b220859170d8ca0e95c6396c7cfadce627)
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 
385424f32eSThomas Graf static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
391da177e4SLinus Torvalds {
405424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
411da177e4SLinus Torvalds 	struct xfrm_algo *algp;
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	if (!rt)
441da177e4SLinus Torvalds 		return 0;
451da177e4SLinus Torvalds 
465424f32eSThomas Graf 	algp = nla_data(rt);
4706cd22f8SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_alg_len(algp))
4831c26852SHerbert Xu 		return -EINVAL;
4931c26852SHerbert Xu 
501da177e4SLinus Torvalds 	switch (type) {
511da177e4SLinus Torvalds 	case XFRMA_ALG_AUTH:
521da177e4SLinus Torvalds 	case XFRMA_ALG_CRYPT:
531da177e4SLinus Torvalds 	case XFRMA_ALG_COMP:
541da177e4SLinus Torvalds 		break;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	default:
571da177e4SLinus Torvalds 		return -EINVAL;
583ff50b79SStephen Hemminger 	}
591da177e4SLinus Torvalds 
60633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
611da177e4SLinus Torvalds 	return 0;
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
644447bb33SMartin Willi static int verify_auth_trunc(struct nlattr **attrs)
654447bb33SMartin Willi {
664447bb33SMartin Willi 	struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
674447bb33SMartin Willi 	struct xfrm_algo_auth *algp;
684447bb33SMartin Willi 
694447bb33SMartin Willi 	if (!rt)
704447bb33SMartin Willi 		return 0;
714447bb33SMartin Willi 
724447bb33SMartin Willi 	algp = nla_data(rt);
731bd963a7SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
744447bb33SMartin Willi 		return -EINVAL;
754447bb33SMartin Willi 
76633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
774447bb33SMartin Willi 	return 0;
784447bb33SMartin Willi }
794447bb33SMartin Willi 
801a6509d9SHerbert Xu static int verify_aead(struct nlattr **attrs)
811a6509d9SHerbert Xu {
821a6509d9SHerbert Xu 	struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
831a6509d9SHerbert Xu 	struct xfrm_algo_aead *algp;
841a6509d9SHerbert Xu 
851a6509d9SHerbert Xu 	if (!rt)
861a6509d9SHerbert Xu 		return 0;
871a6509d9SHerbert Xu 
881a6509d9SHerbert Xu 	algp = nla_data(rt);
89373b8eebSAlexey Dobriyan 	if (nla_len(rt) < (int)aead_len(algp))
901a6509d9SHerbert Xu 		return -EINVAL;
911a6509d9SHerbert Xu 
92633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
931a6509d9SHerbert Xu 	return 0;
941a6509d9SHerbert Xu }
951a6509d9SHerbert Xu 
965424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
97eb2971b6SMasahide NAKAMURA 			   xfrm_address_t **addrp)
98eb2971b6SMasahide NAKAMURA {
995424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
100eb2971b6SMasahide NAKAMURA 
101cf5cb79fSThomas Graf 	if (rt && addrp)
1025424f32eSThomas Graf 		*addrp = nla_data(rt);
103eb2971b6SMasahide NAKAMURA }
104df71837dSTrent Jaeger 
10508a717e4SSabrina Dubroca static inline int verify_sec_ctx_len(struct nlattr **attrs, struct netlink_ext_ack *extack)
106df71837dSTrent Jaeger {
1075424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
108df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
109df71837dSTrent Jaeger 
110df71837dSTrent Jaeger 	if (!rt)
111df71837dSTrent Jaeger 		return 0;
112df71837dSTrent Jaeger 
1135424f32eSThomas Graf 	uctx = nla_data(rt);
114171d449aSXin Long 	if (uctx->len > nla_len(rt) ||
11508a717e4SSabrina Dubroca 	    uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) {
11608a717e4SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Invalid security context length");
117df71837dSTrent Jaeger 		return -EINVAL;
11808a717e4SSabrina Dubroca 	}
119df71837dSTrent Jaeger 
120df71837dSTrent Jaeger 	return 0;
121df71837dSTrent Jaeger }
122df71837dSTrent Jaeger 
123d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p,
124*785b87b2SSabrina Dubroca 				struct nlattr **attrs,
125*785b87b2SSabrina Dubroca 				struct netlink_ext_ack *extack)
126d8647b79SSteffen Klassert {
127d8647b79SSteffen Klassert 	struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
128ecd79187SMathias Krause 	struct xfrm_replay_state_esn *rs;
129d8647b79SSteffen Klassert 
130*785b87b2SSabrina Dubroca 	if (!rt) {
131*785b87b2SSabrina Dubroca 		if (p->flags & XFRM_STATE_ESN) {
132*785b87b2SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Missing required attribute for ESN");
133*785b87b2SSabrina Dubroca 			return -EINVAL;
134*785b87b2SSabrina Dubroca 		}
135*785b87b2SSabrina Dubroca 		return 0;
136*785b87b2SSabrina Dubroca 	}
1377833aa05SSteffen Klassert 
138ecd79187SMathias Krause 	rs = nla_data(rt);
139ecd79187SMathias Krause 
140*785b87b2SSabrina Dubroca 	if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8) {
141*785b87b2SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "ESN bitmap length must be <= 128");
142ecd79187SMathias Krause 		return -EINVAL;
143*785b87b2SSabrina Dubroca 	}
144ecd79187SMathias Krause 
1455e708e47SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
146*785b87b2SSabrina Dubroca 	    nla_len(rt) != sizeof(*rs)) {
147*785b87b2SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "ESN attribute is too short to fit the full bitmap length");
148ecd79187SMathias Krause 		return -EINVAL;
149*785b87b2SSabrina Dubroca 	}
150d8647b79SSteffen Klassert 
15101714109SFan Du 	/* As only ESP and AH support ESN feature. */
152*785b87b2SSabrina Dubroca 	if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH)) {
153*785b87b2SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "ESN only supported for ESP and AH");
15402aadf72SSteffen Klassert 		return -EINVAL;
155*785b87b2SSabrina Dubroca 	}
15602aadf72SSteffen Klassert 
157*785b87b2SSabrina Dubroca 	if (p->replay_window != 0) {
158*785b87b2SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "ESN not compatible with legacy replay_window");
159d8647b79SSteffen Klassert 		return -EINVAL;
160*785b87b2SSabrina Dubroca 	}
161d8647b79SSteffen Klassert 
162d8647b79SSteffen Klassert 	return 0;
163d8647b79SSteffen Klassert }
164df71837dSTrent Jaeger 
1651da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p,
1666999aae1SSabrina Dubroca 			     struct nlattr **attrs,
1676999aae1SSabrina Dubroca 			     struct netlink_ext_ack *extack)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	int err;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	err = -EINVAL;
1721da177e4SLinus Torvalds 	switch (p->family) {
1731da177e4SLinus Torvalds 	case AF_INET:
174b38ff407SAnirudh Gupta 		break;
175b38ff407SAnirudh Gupta 
176b38ff407SAnirudh Gupta 	case AF_INET6:
177b38ff407SAnirudh Gupta #if IS_ENABLED(CONFIG_IPV6)
178b38ff407SAnirudh Gupta 		break;
179b38ff407SAnirudh Gupta #else
180b38ff407SAnirudh Gupta 		err = -EAFNOSUPPORT;
1816999aae1SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "IPv6 support disabled");
182b38ff407SAnirudh Gupta 		goto out;
183b38ff407SAnirudh Gupta #endif
184b38ff407SAnirudh Gupta 
185b38ff407SAnirudh Gupta 	default:
1866999aae1SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Invalid address family");
187b38ff407SAnirudh Gupta 		goto out;
188b38ff407SAnirudh Gupta 	}
189b38ff407SAnirudh Gupta 
190b38ff407SAnirudh Gupta 	switch (p->sel.family) {
191b8d6d007SNicolas Dichtel 	case AF_UNSPEC:
192b8d6d007SNicolas Dichtel 		break;
193b8d6d007SNicolas Dichtel 
194b38ff407SAnirudh Gupta 	case AF_INET:
1956999aae1SSabrina Dubroca 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
1966999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
19707bf7908SSteffen Klassert 			goto out;
1986999aae1SSabrina Dubroca 		}
19907bf7908SSteffen Klassert 
2001da177e4SLinus Torvalds 		break;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	case AF_INET6:
203dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
2046999aae1SSabrina Dubroca 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
2056999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
20607bf7908SSteffen Klassert 			goto out;
2076999aae1SSabrina Dubroca 		}
20807bf7908SSteffen Klassert 
2091da177e4SLinus Torvalds 		break;
2101da177e4SLinus Torvalds #else
2116999aae1SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "IPv6 support disabled");
2121da177e4SLinus Torvalds 		err = -EAFNOSUPPORT;
2131da177e4SLinus Torvalds 		goto out;
2141da177e4SLinus Torvalds #endif
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	default:
2176999aae1SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Invalid address family in selector");
2181da177e4SLinus Torvalds 		goto out;
2193ff50b79SStephen Hemminger 	}
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	err = -EINVAL;
2221da177e4SLinus Torvalds 	switch (p->id.proto) {
2231da177e4SLinus Torvalds 	case IPPROTO_AH:
2246999aae1SSabrina Dubroca 		if (!attrs[XFRMA_ALG_AUTH]	&&
2256999aae1SSabrina Dubroca 		    !attrs[XFRMA_ALG_AUTH_TRUNC]) {
2266999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Missing required attribute for AH: AUTH_TRUNC or AUTH");
2276999aae1SSabrina Dubroca 			goto out;
2286999aae1SSabrina Dubroca 		}
2296999aae1SSabrina Dubroca 
2306999aae1SSabrina Dubroca 		if (attrs[XFRMA_ALG_AEAD]	||
23135a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
23235d2856bSMartin Willi 		    attrs[XFRMA_ALG_COMP]	||
2336999aae1SSabrina Dubroca 		    attrs[XFRMA_TFCPAD]) {
2346999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Invalid attributes for AH: AEAD, CRYPT, COMP, TFCPAD");
2351da177e4SLinus Torvalds 			goto out;
2366999aae1SSabrina Dubroca 		}
2371da177e4SLinus Torvalds 		break;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	case IPPROTO_ESP:
2406999aae1SSabrina Dubroca 		if (attrs[XFRMA_ALG_COMP]) {
2416999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Invalid attribute for ESP: COMP");
2421a6509d9SHerbert Xu 			goto out;
2436999aae1SSabrina Dubroca 		}
2446999aae1SSabrina Dubroca 
2451a6509d9SHerbert Xu 		if (!attrs[XFRMA_ALG_AUTH] &&
2464447bb33SMartin Willi 		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
2471a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_CRYPT] &&
2486999aae1SSabrina Dubroca 		    !attrs[XFRMA_ALG_AEAD]) {
2496999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Missing required attribute for ESP: at least one of AUTH, AUTH_TRUNC, CRYPT, AEAD");
2501a6509d9SHerbert Xu 			goto out;
2516999aae1SSabrina Dubroca 		}
2526999aae1SSabrina Dubroca 
2531a6509d9SHerbert Xu 		if ((attrs[XFRMA_ALG_AUTH] ||
2544447bb33SMartin Willi 		     attrs[XFRMA_ALG_AUTH_TRUNC] ||
2551a6509d9SHerbert Xu 		     attrs[XFRMA_ALG_CRYPT]) &&
2566999aae1SSabrina Dubroca 		    attrs[XFRMA_ALG_AEAD]) {
2576999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Invalid attribute combination for ESP: AEAD can't be used with AUTH, AUTH_TRUNC, CRYPT");
2581da177e4SLinus Torvalds 			goto out;
2596999aae1SSabrina Dubroca 		}
2606999aae1SSabrina Dubroca 
26135d2856bSMartin Willi 		if (attrs[XFRMA_TFCPAD] &&
2626999aae1SSabrina Dubroca 		    p->mode != XFRM_MODE_TUNNEL) {
2636999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "TFC padding can only be used in tunnel mode");
26435d2856bSMartin Willi 			goto out;
2656999aae1SSabrina Dubroca 		}
2661da177e4SLinus Torvalds 		break;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	case IPPROTO_COMP:
2696999aae1SSabrina Dubroca 		if (!attrs[XFRMA_ALG_COMP]) {
2706999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Missing required attribute for COMP: COMP");
2716999aae1SSabrina Dubroca 			goto out;
2726999aae1SSabrina Dubroca 		}
2736999aae1SSabrina Dubroca 
2746999aae1SSabrina Dubroca 		if (attrs[XFRMA_ALG_AEAD]	||
27535a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2764447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
27735d2856bSMartin Willi 		    attrs[XFRMA_ALG_CRYPT]	||
2786999aae1SSabrina Dubroca 		    attrs[XFRMA_TFCPAD]) {
2796999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Invalid attributes for COMP: AEAD, AUTH, AUTH_TRUNC, CRYPT, TFCPAD");
2801da177e4SLinus Torvalds 			goto out;
2816999aae1SSabrina Dubroca 		}
2826999aae1SSabrina Dubroca 
2836999aae1SSabrina Dubroca 		if (ntohl(p->id.spi) >= 0x10000) {
2846999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "SPI is too large for COMP (must be < 0x10000)");
2856999aae1SSabrina Dubroca 			goto out;
2866999aae1SSabrina Dubroca 		}
2871da177e4SLinus Torvalds 		break;
2881da177e4SLinus Torvalds 
289dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
290e23c7194SMasahide NAKAMURA 	case IPPROTO_DSTOPTS:
291e23c7194SMasahide NAKAMURA 	case IPPROTO_ROUTING:
29235a7aa08SThomas Graf 		if (attrs[XFRMA_ALG_COMP]	||
29335a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2944447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
2951a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
29635a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
29735a7aa08SThomas Graf 		    attrs[XFRMA_ENCAP]		||
29835a7aa08SThomas Graf 		    attrs[XFRMA_SEC_CTX]	||
2996999aae1SSabrina Dubroca 		    attrs[XFRMA_TFCPAD]) {
3006999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Invalid attributes for DSTOPTS/ROUTING");
301e23c7194SMasahide NAKAMURA 			goto out;
3026999aae1SSabrina Dubroca 		}
3036999aae1SSabrina Dubroca 
3046999aae1SSabrina Dubroca 		if (!attrs[XFRMA_COADDR]) {
3056999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Missing required COADDR attribute for DSTOPTS/ROUTING");
3066999aae1SSabrina Dubroca 			goto out;
3076999aae1SSabrina Dubroca 		}
308e23c7194SMasahide NAKAMURA 		break;
309e23c7194SMasahide NAKAMURA #endif
310e23c7194SMasahide NAKAMURA 
3111da177e4SLinus Torvalds 	default:
3126999aae1SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Unsupported protocol");
3131da177e4SLinus Torvalds 		goto out;
3143ff50b79SStephen Hemminger 	}
3151da177e4SLinus Torvalds 
3161a6509d9SHerbert Xu 	if ((err = verify_aead(attrs)))
3171a6509d9SHerbert Xu 		goto out;
3184447bb33SMartin Willi 	if ((err = verify_auth_trunc(attrs)))
3194447bb33SMartin Willi 		goto out;
32035a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
3211da177e4SLinus Torvalds 		goto out;
32235a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
3231da177e4SLinus Torvalds 		goto out;
32435a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
3251da177e4SLinus Torvalds 		goto out;
3266999aae1SSabrina Dubroca 	if ((err = verify_sec_ctx_len(attrs, extack)))
327df71837dSTrent Jaeger 		goto out;
328*785b87b2SSabrina Dubroca 	if ((err = verify_replay(p, attrs, extack)))
329d8647b79SSteffen Klassert 		goto out;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	err = -EINVAL;
3321da177e4SLinus Torvalds 	switch (p->mode) {
3337e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TRANSPORT:
3347e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TUNNEL:
335060f02a3SNoriaki TAKAMIYA 	case XFRM_MODE_ROUTEOPTIMIZATION:
3360a69452cSDiego Beltrami 	case XFRM_MODE_BEET:
3371da177e4SLinus Torvalds 		break;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	default:
3406999aae1SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Unsupported mode");
3411da177e4SLinus Torvalds 		goto out;
3423ff50b79SStephen Hemminger 	}
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	err = 0;
3451da177e4SLinus Torvalds 
3466999aae1SSabrina Dubroca 	if (attrs[XFRMA_MTIMER_THRESH]) {
3476999aae1SSabrina Dubroca 		if (!attrs[XFRMA_ENCAP]) {
3486999aae1SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "MTIMER_THRESH attribute can only be set on ENCAP states");
3494e484b3eSAntony Antony 			err = -EINVAL;
3506999aae1SSabrina Dubroca 			goto out;
3516999aae1SSabrina Dubroca 		}
3526999aae1SSabrina Dubroca 	}
3534e484b3eSAntony Antony 
3541da177e4SLinus Torvalds out:
3551da177e4SLinus Torvalds 	return err;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
3596f2f19edSDavid S. Miller 			   struct xfrm_algo_desc *(*get_byname)(const char *, int),
3605424f32eSThomas Graf 			   struct nlattr *rta)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	struct xfrm_algo *p, *ualg;
3631da177e4SLinus Torvalds 	struct xfrm_algo_desc *algo;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	if (!rta)
3661da177e4SLinus Torvalds 		return 0;
3671da177e4SLinus Torvalds 
3685424f32eSThomas Graf 	ualg = nla_data(rta);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	algo = get_byname(ualg->alg_name, 1);
3711da177e4SLinus Torvalds 	if (!algo)
3721da177e4SLinus Torvalds 		return -ENOSYS;
3731da177e4SLinus Torvalds 	*props = algo->desc.sadb_alg_id;
3741da177e4SLinus Torvalds 
3750f99be0dSEric Dumazet 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
3761da177e4SLinus Torvalds 	if (!p)
3771da177e4SLinus Torvalds 		return -ENOMEM;
3781da177e4SLinus Torvalds 
37904ff1260SHerbert Xu 	strcpy(p->alg_name, algo->name);
3801da177e4SLinus Torvalds 	*algpp = p;
3811da177e4SLinus Torvalds 	return 0;
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
38469b0137fSHerbert Xu static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
38569b0137fSHerbert Xu {
38669b0137fSHerbert Xu 	struct xfrm_algo *p, *ualg;
38769b0137fSHerbert Xu 	struct xfrm_algo_desc *algo;
38869b0137fSHerbert Xu 
38969b0137fSHerbert Xu 	if (!rta)
39069b0137fSHerbert Xu 		return 0;
39169b0137fSHerbert Xu 
39269b0137fSHerbert Xu 	ualg = nla_data(rta);
39369b0137fSHerbert Xu 
39469b0137fSHerbert Xu 	algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
39569b0137fSHerbert Xu 	if (!algo)
39669b0137fSHerbert Xu 		return -ENOSYS;
39769b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
39869b0137fSHerbert Xu 
39969b0137fSHerbert Xu 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
40069b0137fSHerbert Xu 	if (!p)
40169b0137fSHerbert Xu 		return -ENOMEM;
40269b0137fSHerbert Xu 
40369b0137fSHerbert Xu 	strcpy(p->alg_name, algo->name);
40469b0137fSHerbert Xu 	x->ealg = p;
40569b0137fSHerbert Xu 	x->geniv = algo->uinfo.encr.geniv;
40669b0137fSHerbert Xu 	return 0;
40769b0137fSHerbert Xu }
40869b0137fSHerbert Xu 
4094447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
4104447bb33SMartin Willi 		       struct nlattr *rta)
4114447bb33SMartin Willi {
4124447bb33SMartin Willi 	struct xfrm_algo *ualg;
4134447bb33SMartin Willi 	struct xfrm_algo_auth *p;
4144447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
4154447bb33SMartin Willi 
4164447bb33SMartin Willi 	if (!rta)
4174447bb33SMartin Willi 		return 0;
4184447bb33SMartin Willi 
4194447bb33SMartin Willi 	ualg = nla_data(rta);
4204447bb33SMartin Willi 
4214447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
4224447bb33SMartin Willi 	if (!algo)
4234447bb33SMartin Willi 		return -ENOSYS;
4244447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
4254447bb33SMartin Willi 
4264447bb33SMartin Willi 	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
4274447bb33SMartin Willi 	if (!p)
4284447bb33SMartin Willi 		return -ENOMEM;
4294447bb33SMartin Willi 
4304447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
4314447bb33SMartin Willi 	p->alg_key_len = ualg->alg_key_len;
4324447bb33SMartin Willi 	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
4334447bb33SMartin Willi 	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
4344447bb33SMartin Willi 
4354447bb33SMartin Willi 	*algpp = p;
4364447bb33SMartin Willi 	return 0;
4374447bb33SMartin Willi }
4384447bb33SMartin Willi 
4394447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
4404447bb33SMartin Willi 			     struct nlattr *rta)
4414447bb33SMartin Willi {
4424447bb33SMartin Willi 	struct xfrm_algo_auth *p, *ualg;
4434447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
4444447bb33SMartin Willi 
4454447bb33SMartin Willi 	if (!rta)
4464447bb33SMartin Willi 		return 0;
4474447bb33SMartin Willi 
4484447bb33SMartin Willi 	ualg = nla_data(rta);
4494447bb33SMartin Willi 
4504447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
4514447bb33SMartin Willi 	if (!algo)
4524447bb33SMartin Willi 		return -ENOSYS;
453689f1c9dSHerbert Xu 	if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
4544447bb33SMartin Willi 		return -EINVAL;
4554447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
4564447bb33SMartin Willi 
4574447bb33SMartin Willi 	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
4584447bb33SMartin Willi 	if (!p)
4594447bb33SMartin Willi 		return -ENOMEM;
4604447bb33SMartin Willi 
4614447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
4624447bb33SMartin Willi 	if (!p->alg_trunc_len)
4634447bb33SMartin Willi 		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
4644447bb33SMartin Willi 
4654447bb33SMartin Willi 	*algpp = p;
4664447bb33SMartin Willi 	return 0;
4674447bb33SMartin Willi }
4684447bb33SMartin Willi 
46969b0137fSHerbert Xu static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
4701a6509d9SHerbert Xu {
4711a6509d9SHerbert Xu 	struct xfrm_algo_aead *p, *ualg;
4721a6509d9SHerbert Xu 	struct xfrm_algo_desc *algo;
4731a6509d9SHerbert Xu 
4741a6509d9SHerbert Xu 	if (!rta)
4751a6509d9SHerbert Xu 		return 0;
4761a6509d9SHerbert Xu 
4771a6509d9SHerbert Xu 	ualg = nla_data(rta);
4781a6509d9SHerbert Xu 
4791a6509d9SHerbert Xu 	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
4801a6509d9SHerbert Xu 	if (!algo)
4811a6509d9SHerbert Xu 		return -ENOSYS;
48269b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
4831a6509d9SHerbert Xu 
4841a6509d9SHerbert Xu 	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
4851a6509d9SHerbert Xu 	if (!p)
4861a6509d9SHerbert Xu 		return -ENOMEM;
4871a6509d9SHerbert Xu 
4881a6509d9SHerbert Xu 	strcpy(p->alg_name, algo->name);
48969b0137fSHerbert Xu 	x->aead = p;
49069b0137fSHerbert Xu 	x->geniv = algo->uinfo.aead.geniv;
4911a6509d9SHerbert Xu 	return 0;
4921a6509d9SHerbert Xu }
4931a6509d9SHerbert Xu 
494e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
495e2b19125SSteffen Klassert 					 struct nlattr *rp)
496e2b19125SSteffen Klassert {
497e2b19125SSteffen Klassert 	struct xfrm_replay_state_esn *up;
4985e708e47SAlexey Dobriyan 	unsigned int ulen;
499e2b19125SSteffen Klassert 
500e2b19125SSteffen Klassert 	if (!replay_esn || !rp)
501e2b19125SSteffen Klassert 		return 0;
502e2b19125SSteffen Klassert 
503e2b19125SSteffen Klassert 	up = nla_data(rp);
504ecd79187SMathias Krause 	ulen = xfrm_replay_state_esn_len(up);
505e2b19125SSteffen Klassert 
506f843ee6dSAndy Whitcroft 	/* Check the overall length and the internal bitmap length to avoid
507f843ee6dSAndy Whitcroft 	 * potential overflow. */
5085e708e47SAlexey Dobriyan 	if (nla_len(rp) < (int)ulen ||
509f843ee6dSAndy Whitcroft 	    xfrm_replay_state_esn_len(replay_esn) != ulen ||
510f843ee6dSAndy Whitcroft 	    replay_esn->bmp_len != up->bmp_len)
511e2b19125SSteffen Klassert 		return -EINVAL;
512e2b19125SSteffen Klassert 
513677e806dSAndy Whitcroft 	if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
514677e806dSAndy Whitcroft 		return -EINVAL;
515677e806dSAndy Whitcroft 
516e2b19125SSteffen Klassert 	return 0;
517e2b19125SSteffen Klassert }
518e2b19125SSteffen Klassert 
519d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
520d8647b79SSteffen Klassert 				       struct xfrm_replay_state_esn **preplay_esn,
521d8647b79SSteffen Klassert 				       struct nlattr *rta)
522d8647b79SSteffen Klassert {
523d8647b79SSteffen Klassert 	struct xfrm_replay_state_esn *p, *pp, *up;
5245e708e47SAlexey Dobriyan 	unsigned int klen, ulen;
525d8647b79SSteffen Klassert 
526d8647b79SSteffen Klassert 	if (!rta)
527d8647b79SSteffen Klassert 		return 0;
528d8647b79SSteffen Klassert 
529d8647b79SSteffen Klassert 	up = nla_data(rta);
530ecd79187SMathias Krause 	klen = xfrm_replay_state_esn_len(up);
5315e708e47SAlexey Dobriyan 	ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
532d8647b79SSteffen Klassert 
533ecd79187SMathias Krause 	p = kzalloc(klen, GFP_KERNEL);
534d8647b79SSteffen Klassert 	if (!p)
535d8647b79SSteffen Klassert 		return -ENOMEM;
536d8647b79SSteffen Klassert 
537ecd79187SMathias Krause 	pp = kzalloc(klen, GFP_KERNEL);
538d8647b79SSteffen Klassert 	if (!pp) {
539d8647b79SSteffen Klassert 		kfree(p);
540d8647b79SSteffen Klassert 		return -ENOMEM;
541d8647b79SSteffen Klassert 	}
542d8647b79SSteffen Klassert 
543ecd79187SMathias Krause 	memcpy(p, up, ulen);
544ecd79187SMathias Krause 	memcpy(pp, up, ulen);
545ecd79187SMathias Krause 
546d8647b79SSteffen Klassert 	*replay_esn = p;
547d8647b79SSteffen Klassert 	*preplay_esn = pp;
548d8647b79SSteffen Klassert 
549d8647b79SSteffen Klassert 	return 0;
550d8647b79SSteffen Klassert }
551d8647b79SSteffen Klassert 
552a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
553df71837dSTrent Jaeger {
554a1b831f2SAlexey Dobriyan 	unsigned int len = 0;
555df71837dSTrent Jaeger 
556df71837dSTrent Jaeger 	if (xfrm_ctx) {
557df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
558df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
559df71837dSTrent Jaeger 	}
560df71837dSTrent Jaeger 	return len;
561df71837dSTrent Jaeger }
562df71837dSTrent Jaeger 
5631da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
5661da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
5671da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
5681da177e4SLinus Torvalds 	x->props.mode = p->mode;
56933fce60dSFan Du 	x->props.replay_window = min_t(unsigned int, p->replay_window,
57033fce60dSFan Du 					sizeof(x->replay.bitmap) * 8);
5711da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
5721da177e4SLinus Torvalds 	x->props.family = p->family;
57354489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
5741da177e4SLinus Torvalds 	x->props.flags = p->flags;
575196b0036SHerbert Xu 
576ccf9b3b8SSteffen Klassert 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
577196b0036SHerbert Xu 		x->sel.family = p->family;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
580d51d081dSJamal Hadi Salim /*
581d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
582d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
583d51d081dSJamal Hadi Salim  *
584d51d081dSJamal Hadi Salim */
585e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
586e3ac104dSMathias Krause 				  int update_esn)
587d51d081dSJamal Hadi Salim {
5885424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
589e3ac104dSMathias Krause 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
5905424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
5915424f32eSThomas Graf 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
5925424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
5934e484b3eSAntony Antony 	struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
594d51d081dSJamal Hadi Salim 
595d8647b79SSteffen Klassert 	if (re) {
596d8647b79SSteffen Klassert 		struct xfrm_replay_state_esn *replay_esn;
597d8647b79SSteffen Klassert 		replay_esn = nla_data(re);
598d8647b79SSteffen Klassert 		memcpy(x->replay_esn, replay_esn,
599d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
600d8647b79SSteffen Klassert 		memcpy(x->preplay_esn, replay_esn,
601d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
602d8647b79SSteffen Klassert 	}
603d8647b79SSteffen Klassert 
604d51d081dSJamal Hadi Salim 	if (rp) {
605d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
6065424f32eSThomas Graf 		replay = nla_data(rp);
607d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
608d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
609d51d081dSJamal Hadi Salim 	}
610d51d081dSJamal Hadi Salim 
611d51d081dSJamal Hadi Salim 	if (lt) {
612d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
6135424f32eSThomas Graf 		ltime = nla_data(lt);
614d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
615d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
616d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
617d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
618d51d081dSJamal Hadi Salim 	}
619d51d081dSJamal Hadi Salim 
620cf5cb79fSThomas Graf 	if (et)
6215424f32eSThomas Graf 		x->replay_maxage = nla_get_u32(et);
622d51d081dSJamal Hadi Salim 
623cf5cb79fSThomas Graf 	if (rt)
6245424f32eSThomas Graf 		x->replay_maxdiff = nla_get_u32(rt);
6254e484b3eSAntony Antony 
6264e484b3eSAntony Antony 	if (mt)
6274e484b3eSAntony Antony 		x->mapping_maxage = nla_get_u32(mt);
628d51d081dSJamal Hadi Salim }
629d51d081dSJamal Hadi Salim 
6309b42c1f1SSteffen Klassert static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
6319b42c1f1SSteffen Klassert {
6329b42c1f1SSteffen Klassert 	if (attrs[XFRMA_SET_MARK]) {
6339b42c1f1SSteffen Klassert 		m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
6349b42c1f1SSteffen Klassert 		if (attrs[XFRMA_SET_MARK_MASK])
6359b42c1f1SSteffen Klassert 			m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
6369b42c1f1SSteffen Klassert 		else
6379b42c1f1SSteffen Klassert 			m->m = 0xffffffff;
6389b42c1f1SSteffen Klassert 	} else {
6399b42c1f1SSteffen Klassert 		m->v = m->m = 0;
6409b42c1f1SSteffen Klassert 	}
6419b42c1f1SSteffen Klassert }
6429b42c1f1SSteffen Klassert 
643fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
644fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
6455424f32eSThomas Graf 					       struct nlattr **attrs,
6461da177e4SLinus Torvalds 					       int *errp)
6471da177e4SLinus Torvalds {
648fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
6491da177e4SLinus Torvalds 	int err = -ENOMEM;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	if (!x)
6521da177e4SLinus Torvalds 		goto error_no_put;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	copy_from_user_state(x, p);
6551da177e4SLinus Torvalds 
6566fd06963SSteffen Klassert 	if (attrs[XFRMA_ENCAP]) {
6576fd06963SSteffen Klassert 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
6586fd06963SSteffen Klassert 				   sizeof(*x->encap), GFP_KERNEL);
6596fd06963SSteffen Klassert 		if (x->encap == NULL)
6606fd06963SSteffen Klassert 			goto error;
6616fd06963SSteffen Klassert 	}
6626fd06963SSteffen Klassert 
6636fd06963SSteffen Klassert 	if (attrs[XFRMA_COADDR]) {
6646fd06963SSteffen Klassert 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
6656fd06963SSteffen Klassert 				    sizeof(*x->coaddr), GFP_KERNEL);
6666fd06963SSteffen Klassert 		if (x->coaddr == NULL)
6676fd06963SSteffen Klassert 			goto error;
6686fd06963SSteffen Klassert 	}
6696fd06963SSteffen Klassert 
670a947b0a9SNicolas Dichtel 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
671a947b0a9SNicolas Dichtel 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
672a947b0a9SNicolas Dichtel 
67369b0137fSHerbert Xu 	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
6741a6509d9SHerbert Xu 		goto error;
6754447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
6764447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
6774447bb33SMartin Willi 		goto error;
6784447bb33SMartin Willi 	if (!x->props.aalgo) {
6794447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
68035a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
6811da177e4SLinus Torvalds 			goto error;
6824447bb33SMartin Willi 	}
68369b0137fSHerbert Xu 	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
6841da177e4SLinus Torvalds 		goto error;
6851da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
6861da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
68735a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
6881da177e4SLinus Torvalds 		goto error;
689fd21150aSThomas Graf 
69035d2856bSMartin Willi 	if (attrs[XFRMA_TFCPAD])
69135d2856bSMartin Willi 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
69235d2856bSMartin Willi 
6936f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &x->mark);
6946f26b61eSJamal Hadi Salim 
6959b42c1f1SSteffen Klassert 	xfrm_smark_init(attrs, &x->props.smark);
696077fbac4SLorenzo Colitti 
697a3d9001bSKai Lueke 	if (attrs[XFRMA_IF_ID])
6987e652640SSteffen Klassert 		x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
699fd21150aSThomas Graf 
700ffdb5211SIlan Tayari 	err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
7011da177e4SLinus Torvalds 	if (err)
7021da177e4SLinus Torvalds 		goto error;
7031da177e4SLinus Torvalds 
7042f30ea50SMathias Krause 	if (attrs[XFRMA_SEC_CTX]) {
7052f30ea50SMathias Krause 		err = security_xfrm_state_alloc(x,
7062f30ea50SMathias Krause 						nla_data(attrs[XFRMA_SEC_CTX]));
7072f30ea50SMathias Krause 		if (err)
708df71837dSTrent Jaeger 			goto error;
7092f30ea50SMathias Krause 	}
710df71837dSTrent Jaeger 
711d8647b79SSteffen Klassert 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
712d8647b79SSteffen Klassert 					       attrs[XFRMA_REPLAY_ESN_VAL])))
713d8647b79SSteffen Klassert 		goto error;
714d8647b79SSteffen Klassert 
7151da177e4SLinus Torvalds 	x->km.seq = p->seq;
716b27aeadbSAlexey Dobriyan 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
717d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
718b27aeadbSAlexey Dobriyan 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
719d51d081dSJamal Hadi Salim 
7209fdc4883SSteffen Klassert 	if ((err = xfrm_init_replay(x)))
7219fdc4883SSteffen Klassert 		goto error;
722d51d081dSJamal Hadi Salim 
7239fdc4883SSteffen Klassert 	/* override default values from above */
724e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 0);
7251da177e4SLinus Torvalds 
726cc01572eSYossi Kuperman 	/* configure the hardware if offload is requested */
727cc01572eSYossi Kuperman 	if (attrs[XFRMA_OFFLOAD_DEV]) {
728cc01572eSYossi Kuperman 		err = xfrm_dev_state_add(net, x,
729cc01572eSYossi Kuperman 					 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
730cc01572eSYossi Kuperman 		if (err)
731cc01572eSYossi Kuperman 			goto error;
732cc01572eSYossi Kuperman 	}
733cc01572eSYossi Kuperman 
7341da177e4SLinus Torvalds 	return x;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds error:
7371da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
7381da177e4SLinus Torvalds 	xfrm_state_put(x);
7391da177e4SLinus Torvalds error_no_put:
7401da177e4SLinus Torvalds 	*errp = err;
7411da177e4SLinus Torvalds 	return NULL;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
74422e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
7453bec6c3eSSabrina Dubroca 		       struct nlattr **attrs, struct netlink_ext_ack *extack)
7461da177e4SLinus Torvalds {
747fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
7487b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
7491da177e4SLinus Torvalds 	struct xfrm_state *x;
7501da177e4SLinus Torvalds 	int err;
75126b15dadSJamal Hadi Salim 	struct km_event c;
7521da177e4SLinus Torvalds 
7536999aae1SSabrina Dubroca 	err = verify_newsa_info(p, attrs, extack);
7541da177e4SLinus Torvalds 	if (err)
7551da177e4SLinus Torvalds 		return err;
7561da177e4SLinus Torvalds 
757fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
7581da177e4SLinus Torvalds 	if (!x)
7591da177e4SLinus Torvalds 		return err;
7601da177e4SLinus Torvalds 
76126b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
7621da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
7631da177e4SLinus Torvalds 		err = xfrm_state_add(x);
7641da177e4SLinus Torvalds 	else
7651da177e4SLinus Torvalds 		err = xfrm_state_update(x);
7661da177e4SLinus Torvalds 
7672e71029eSTetsuo Handa 	xfrm_audit_state_add(x, err ? 0 : 1, true);
768161a09e7SJoy Latten 
7691da177e4SLinus Torvalds 	if (err < 0) {
7701da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
771c5d4d7d8SSteffen Klassert 		xfrm_dev_state_delete(x);
77221380b81SHerbert Xu 		__xfrm_state_put(x);
7737d6dfe1fSPatrick McHardy 		goto out;
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 
776cc01572eSYossi Kuperman 	if (x->km.state == XFRM_STATE_VOID)
777cc01572eSYossi Kuperman 		x->km.state = XFRM_STATE_VALID;
778cc01572eSYossi Kuperman 
77926b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
78015e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
781f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
78226b15dadSJamal Hadi Salim 
78326b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7847d6dfe1fSPatrick McHardy out:
78526b15dadSJamal Hadi Salim 	xfrm_state_put(x);
7861da177e4SLinus Torvalds 	return err;
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
789fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
790fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
7915424f32eSThomas Graf 						 struct nlattr **attrs,
792eb2971b6SMasahide NAKAMURA 						 int *errp)
793eb2971b6SMasahide NAKAMURA {
794eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
7956f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
796eb2971b6SMasahide NAKAMURA 	int err;
7976f26b61eSJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
798eb2971b6SMasahide NAKAMURA 
799eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
800eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
8016f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
802eb2971b6SMasahide NAKAMURA 	} else {
803eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
804eb2971b6SMasahide NAKAMURA 
80535a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
806eb2971b6SMasahide NAKAMURA 		if (!saddr) {
807eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
808eb2971b6SMasahide NAKAMURA 			goto out;
809eb2971b6SMasahide NAKAMURA 		}
810eb2971b6SMasahide NAKAMURA 
8119abbffeeSMasahide NAKAMURA 		err = -ESRCH;
8126f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup_byaddr(net, mark,
8136f26b61eSJamal Hadi Salim 					     &p->daddr, saddr,
814221df1edSAlexey Dobriyan 					     p->proto, p->family);
815eb2971b6SMasahide NAKAMURA 	}
816eb2971b6SMasahide NAKAMURA 
817eb2971b6SMasahide NAKAMURA  out:
818eb2971b6SMasahide NAKAMURA 	if (!x && errp)
819eb2971b6SMasahide NAKAMURA 		*errp = err;
820eb2971b6SMasahide NAKAMURA 	return x;
821eb2971b6SMasahide NAKAMURA }
822eb2971b6SMasahide NAKAMURA 
82322e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
8243bec6c3eSSabrina Dubroca 		       struct nlattr **attrs, struct netlink_ext_ack *extack)
8251da177e4SLinus Torvalds {
826fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
8271da177e4SLinus Torvalds 	struct xfrm_state *x;
828eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
82926b15dadSJamal Hadi Salim 	struct km_event c;
8307b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
8311da177e4SLinus Torvalds 
832fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
8331da177e4SLinus Torvalds 	if (x == NULL)
834eb2971b6SMasahide NAKAMURA 		return err;
8351da177e4SLinus Torvalds 
8366f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
837c8c05a8eSCatherine Zhang 		goto out;
838c8c05a8eSCatherine Zhang 
8391da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
840c8c05a8eSCatherine Zhang 		err = -EPERM;
841c8c05a8eSCatherine Zhang 		goto out;
8421da177e4SLinus Torvalds 	}
8431da177e4SLinus Torvalds 
84426b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
845161a09e7SJoy Latten 
846c8c05a8eSCatherine Zhang 	if (err < 0)
847c8c05a8eSCatherine Zhang 		goto out;
84826b15dadSJamal Hadi Salim 
84926b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
85015e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
851f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
85226b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
8531da177e4SLinus Torvalds 
854c8c05a8eSCatherine Zhang out:
8552e71029eSTetsuo Handa 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
856c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
85726b15dadSJamal Hadi Salim 	return err;
8581da177e4SLinus Torvalds }
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
8611da177e4SLinus Torvalds {
862f778a636SMathias Krause 	memset(p, 0, sizeof(*p));
8631da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
8641da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
8651da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
8661da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
867e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
868e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay, &p->stats.replay);
869e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
87054489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
8711da177e4SLinus Torvalds 	p->mode = x->props.mode;
8721da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
8731da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
8741da177e4SLinus Torvalds 	p->family = x->props.family;
8751da177e4SLinus Torvalds 	p->flags = x->props.flags;
8761da177e4SLinus Torvalds 	p->seq = x->km.seq;
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds struct xfrm_dump_info {
8801da177e4SLinus Torvalds 	struct sk_buff *in_skb;
8811da177e4SLinus Torvalds 	struct sk_buff *out_skb;
8821da177e4SLinus Torvalds 	u32 nlmsg_seq;
8831da177e4SLinus Torvalds 	u16 nlmsg_flags;
8841da177e4SLinus Torvalds };
8851da177e4SLinus Torvalds 
886c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
887c0144beaSThomas Graf {
888c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
889c0144beaSThomas Graf 	struct nlattr *attr;
89068325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
891c0144beaSThomas Graf 
892c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
893c0144beaSThomas Graf 	if (attr == NULL)
894c0144beaSThomas Graf 		return -EMSGSIZE;
895c0144beaSThomas Graf 
896c0144beaSThomas Graf 	uctx = nla_data(attr);
897c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
898c0144beaSThomas Graf 	uctx->len = ctx_size;
899c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
900c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
901c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
902c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
903c0144beaSThomas Graf 
904c0144beaSThomas Graf 	return 0;
905c0144beaSThomas Graf }
906c0144beaSThomas Graf 
90787e0a94eSLeon Romanovsky static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb)
908d77e38e6SSteffen Klassert {
909d77e38e6SSteffen Klassert 	struct xfrm_user_offload *xuo;
910d77e38e6SSteffen Klassert 	struct nlattr *attr;
911d77e38e6SSteffen Klassert 
912d77e38e6SSteffen Klassert 	attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
913d77e38e6SSteffen Klassert 	if (attr == NULL)
914d77e38e6SSteffen Klassert 		return -EMSGSIZE;
915d77e38e6SSteffen Klassert 
916d77e38e6SSteffen Klassert 	xuo = nla_data(attr);
9175fe0d4bdSMathias Krause 	memset(xuo, 0, sizeof(*xuo));
918d77e38e6SSteffen Klassert 	xuo->ifindex = xso->dev->ifindex;
919482db2f1SLeon Romanovsky 	if (xso->dir == XFRM_DEV_OFFLOAD_IN)
920482db2f1SLeon Romanovsky 		xuo->flags = XFRM_OFFLOAD_INBOUND;
921d77e38e6SSteffen Klassert 
922d77e38e6SSteffen Klassert 	return 0;
923d77e38e6SSteffen Klassert }
924d77e38e6SSteffen Klassert 
925c7a5899eSAntony Antony static bool xfrm_redact(void)
926c7a5899eSAntony Antony {
927c7a5899eSAntony Antony 	return IS_ENABLED(CONFIG_SECURITY) &&
928c7a5899eSAntony Antony 		security_locked_down(LOCKDOWN_XFRM_SECRET);
929c7a5899eSAntony Antony }
930c7a5899eSAntony Antony 
9314447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
9324447bb33SMartin Willi {
9334447bb33SMartin Willi 	struct xfrm_algo *algo;
934c7a5899eSAntony Antony 	struct xfrm_algo_auth *ap;
9354447bb33SMartin Willi 	struct nlattr *nla;
936c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
9374447bb33SMartin Willi 
9384447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
9394447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
9404447bb33SMartin Willi 	if (!nla)
9414447bb33SMartin Willi 		return -EMSGSIZE;
9424447bb33SMartin Willi 	algo = nla_data(nla);
9434c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
944c7a5899eSAntony Antony 
945c7a5899eSAntony Antony 	if (redact_secret && auth->alg_key_len)
946c7a5899eSAntony Antony 		memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
947c7a5899eSAntony Antony 	else
948c7a5899eSAntony Antony 		memcpy(algo->alg_key, auth->alg_key,
949c7a5899eSAntony Antony 		       (auth->alg_key_len + 7) / 8);
9504447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
9514447bb33SMartin Willi 
952c7a5899eSAntony Antony 	nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
953c7a5899eSAntony Antony 	if (!nla)
954c7a5899eSAntony Antony 		return -EMSGSIZE;
955c7a5899eSAntony Antony 	ap = nla_data(nla);
956c7a5899eSAntony Antony 	memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
957c7a5899eSAntony Antony 	if (redact_secret && auth->alg_key_len)
958c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
959c7a5899eSAntony Antony 	else
960c7a5899eSAntony Antony 		memcpy(ap->alg_key, auth->alg_key,
961c7a5899eSAntony Antony 		       (auth->alg_key_len + 7) / 8);
962c7a5899eSAntony Antony 	return 0;
963c7a5899eSAntony Antony }
964c7a5899eSAntony Antony 
965c7a5899eSAntony Antony static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
966c7a5899eSAntony Antony {
967c7a5899eSAntony Antony 	struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
968c7a5899eSAntony Antony 	struct xfrm_algo_aead *ap;
969c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
970c7a5899eSAntony Antony 
971c7a5899eSAntony Antony 	if (!nla)
972c7a5899eSAntony Antony 		return -EMSGSIZE;
973c7a5899eSAntony Antony 
974c7a5899eSAntony Antony 	ap = nla_data(nla);
975c7a5899eSAntony Antony 	memcpy(ap, aead, sizeof(*aead));
976c7a5899eSAntony Antony 
977c7a5899eSAntony Antony 	if (redact_secret && aead->alg_key_len)
978c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
979c7a5899eSAntony Antony 	else
980c7a5899eSAntony Antony 		memcpy(ap->alg_key, aead->alg_key,
981c7a5899eSAntony Antony 		       (aead->alg_key_len + 7) / 8);
982c7a5899eSAntony Antony 	return 0;
983c7a5899eSAntony Antony }
984c7a5899eSAntony Antony 
985c7a5899eSAntony Antony static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
986c7a5899eSAntony Antony {
987c7a5899eSAntony Antony 	struct xfrm_algo *ap;
988c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
989c7a5899eSAntony Antony 	struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
990c7a5899eSAntony Antony 					 xfrm_alg_len(ealg));
991c7a5899eSAntony Antony 	if (!nla)
992c7a5899eSAntony Antony 		return -EMSGSIZE;
993c7a5899eSAntony Antony 
994c7a5899eSAntony Antony 	ap = nla_data(nla);
995c7a5899eSAntony Antony 	memcpy(ap, ealg, sizeof(*ealg));
996c7a5899eSAntony Antony 
997c7a5899eSAntony Antony 	if (redact_secret && ealg->alg_key_len)
998c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
999c7a5899eSAntony Antony 	else
1000c7a5899eSAntony Antony 		memcpy(ap->alg_key, ealg->alg_key,
1001c7a5899eSAntony Antony 		       (ealg->alg_key_len + 7) / 8);
1002c7a5899eSAntony Antony 
10034447bb33SMartin Willi 	return 0;
10044447bb33SMartin Willi }
10054447bb33SMartin Willi 
10069b42c1f1SSteffen Klassert static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
10079b42c1f1SSteffen Klassert {
10089b42c1f1SSteffen Klassert 	int ret = 0;
10099b42c1f1SSteffen Klassert 
10109b42c1f1SSteffen Klassert 	if (m->v | m->m) {
10119b42c1f1SSteffen Klassert 		ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
10129b42c1f1SSteffen Klassert 		if (!ret)
10139b42c1f1SSteffen Klassert 			ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
10149b42c1f1SSteffen Klassert 	}
10159b42c1f1SSteffen Klassert 	return ret;
10169b42c1f1SSteffen Klassert }
10179b42c1f1SSteffen Klassert 
101868325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
101968325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
102068325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
102168325d3bSHerbert Xu 				    struct sk_buff *skb)
10221da177e4SLinus Torvalds {
10231d1e34ddSDavid S. Miller 	int ret = 0;
10241d1e34ddSDavid S. Miller 
10251da177e4SLinus Torvalds 	copy_to_user_state(x, p);
10261da177e4SLinus Torvalds 
1027a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
1028a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
1029a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
1030a947b0a9SNicolas Dichtel 		if (ret)
1031a947b0a9SNicolas Dichtel 			goto out;
1032a947b0a9SNicolas Dichtel 	}
1033a947b0a9SNicolas Dichtel 
10341d1e34ddSDavid S. Miller 	if (x->coaddr) {
10351d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
10361d1e34ddSDavid S. Miller 		if (ret)
10371d1e34ddSDavid S. Miller 			goto out;
10381d1e34ddSDavid S. Miller 	}
10391d1e34ddSDavid S. Miller 	if (x->lastused) {
1040de95c4a4SNicolas Dichtel 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
1041de95c4a4SNicolas Dichtel 					XFRMA_PAD);
10421d1e34ddSDavid S. Miller 		if (ret)
10431d1e34ddSDavid S. Miller 			goto out;
10441d1e34ddSDavid S. Miller 	}
10451d1e34ddSDavid S. Miller 	if (x->aead) {
1046c7a5899eSAntony Antony 		ret = copy_to_user_aead(x->aead, skb);
10471d1e34ddSDavid S. Miller 		if (ret)
10481d1e34ddSDavid S. Miller 			goto out;
10491d1e34ddSDavid S. Miller 	}
10501d1e34ddSDavid S. Miller 	if (x->aalg) {
10511d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
10521d1e34ddSDavid S. Miller 		if (ret)
10531d1e34ddSDavid S. Miller 			goto out;
10541d1e34ddSDavid S. Miller 	}
10551d1e34ddSDavid S. Miller 	if (x->ealg) {
1056c7a5899eSAntony Antony 		ret = copy_to_user_ealg(x->ealg, skb);
10571d1e34ddSDavid S. Miller 		if (ret)
10581d1e34ddSDavid S. Miller 			goto out;
10591d1e34ddSDavid S. Miller 	}
10601d1e34ddSDavid S. Miller 	if (x->calg) {
10611d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
10621d1e34ddSDavid S. Miller 		if (ret)
10631d1e34ddSDavid S. Miller 			goto out;
10641d1e34ddSDavid S. Miller 	}
10651d1e34ddSDavid S. Miller 	if (x->encap) {
10661d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
10671d1e34ddSDavid S. Miller 		if (ret)
10681d1e34ddSDavid S. Miller 			goto out;
10691d1e34ddSDavid S. Miller 	}
10701d1e34ddSDavid S. Miller 	if (x->tfcpad) {
10711d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
10721d1e34ddSDavid S. Miller 		if (ret)
10731d1e34ddSDavid S. Miller 			goto out;
10741d1e34ddSDavid S. Miller 	}
10751d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
10761d1e34ddSDavid S. Miller 	if (ret)
10771d1e34ddSDavid S. Miller 		goto out;
10789b42c1f1SSteffen Klassert 
10799b42c1f1SSteffen Klassert 	ret = xfrm_smark_put(skb, &x->props.smark);
10809b42c1f1SSteffen Klassert 	if (ret)
10819b42c1f1SSteffen Klassert 		goto out;
10829b42c1f1SSteffen Klassert 
1083f293a5e3Sdingzhi 	if (x->replay_esn)
10841d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1085d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
10861d1e34ddSDavid S. Miller 			      x->replay_esn);
1087f293a5e3Sdingzhi 	else
1088f293a5e3Sdingzhi 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1089f293a5e3Sdingzhi 			      &x->replay);
10901d1e34ddSDavid S. Miller 	if (ret)
10911d1e34ddSDavid S. Miller 		goto out;
1092d77e38e6SSteffen Klassert 	if(x->xso.dev)
1093d77e38e6SSteffen Klassert 		ret = copy_user_offload(&x->xso, skb);
1094d77e38e6SSteffen Klassert 	if (ret)
1095d77e38e6SSteffen Klassert 		goto out;
10967e652640SSteffen Klassert 	if (x->if_id) {
10977e652640SSteffen Klassert 		ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1098077fbac4SLorenzo Colitti 		if (ret)
1099077fbac4SLorenzo Colitti 			goto out;
1100077fbac4SLorenzo Colitti 	}
11014e484b3eSAntony Antony 	if (x->security) {
11028598112dSSteffen Klassert 		ret = copy_sec_ctx(x->security, skb);
11034e484b3eSAntony Antony 		if (ret)
11044e484b3eSAntony Antony 			goto out;
11054e484b3eSAntony Antony 	}
11064e484b3eSAntony Antony 	if (x->mapping_maxage)
11074e484b3eSAntony Antony 		ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
11081d1e34ddSDavid S. Miller out:
11091d1e34ddSDavid S. Miller 	return ret;
111068325d3bSHerbert Xu }
111168325d3bSHerbert Xu 
111268325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
111368325d3bSHerbert Xu {
111468325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
111568325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
111668325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
11175f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
111868325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
111968325d3bSHerbert Xu 	struct nlmsghdr *nlh;
112068325d3bSHerbert Xu 	int err;
112168325d3bSHerbert Xu 
112215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
112368325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
112468325d3bSHerbert Xu 	if (nlh == NULL)
112568325d3bSHerbert Xu 		return -EMSGSIZE;
112668325d3bSHerbert Xu 
112768325d3bSHerbert Xu 	p = nlmsg_data(nlh);
112868325d3bSHerbert Xu 
112968325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
11301d1e34ddSDavid S. Miller 	if (err) {
11319825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
113268325d3bSHerbert Xu 		return err;
11331da177e4SLinus Torvalds 	}
11341d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
11355f3eea6bSDmitry Safonov 
11365f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
11375f3eea6bSDmitry Safonov 	if (xtr) {
11385f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlh);
11395f3eea6bSDmitry Safonov 
11405f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
11415f3eea6bSDmitry Safonov 		if (err) {
11425f3eea6bSDmitry Safonov 			nlmsg_cancel(skb, nlh);
11435f3eea6bSDmitry Safonov 			return err;
11445f3eea6bSDmitry Safonov 		}
11455f3eea6bSDmitry Safonov 	}
11465f3eea6bSDmitry Safonov 
11471d1e34ddSDavid S. Miller 	return 0;
11481d1e34ddSDavid S. Miller }
11491da177e4SLinus Torvalds 
11504c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
11514c563f76STimo Teras {
11524c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1153283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
1154283bc9f3SFan Du 	struct net *net = sock_net(sk);
1155283bc9f3SFan Du 
11561ba5bf99SVegard Nossum 	if (cb->args[0])
1157283bc9f3SFan Du 		xfrm_state_walk_done(walk, net);
11584c563f76STimo Teras 	return 0;
11594c563f76STimo Teras }
11604c563f76STimo Teras 
11611da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
11621da177e4SLinus Torvalds {
1163fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
11644c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
11651da177e4SLinus Torvalds 	struct xfrm_dump_info info;
11661da177e4SLinus Torvalds 
11674c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
11684c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
11694c563f76STimo Teras 
11701da177e4SLinus Torvalds 	info.in_skb = cb->skb;
11711da177e4SLinus Torvalds 	info.out_skb = skb;
11721da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
11731da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
11744c563f76STimo Teras 
11754c563f76STimo Teras 	if (!cb->args[0]) {
1176d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
1177870a2df4SNicolas Dichtel 		struct xfrm_address_filter *filter = NULL;
1178d3623099SNicolas Dichtel 		u8 proto = 0;
1179d3623099SNicolas Dichtel 		int err;
1180d3623099SNicolas Dichtel 
11818cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
11828cb08174SJohannes Berg 					     xfrma_policy, cb->extack);
1183d3623099SNicolas Dichtel 		if (err < 0)
1184d3623099SNicolas Dichtel 			return err;
1185d3623099SNicolas Dichtel 
1186870a2df4SNicolas Dichtel 		if (attrs[XFRMA_ADDRESS_FILTER]) {
1187df367561SAndrzej Hajda 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1188df367561SAndrzej Hajda 					 sizeof(*filter), GFP_KERNEL);
1189d3623099SNicolas Dichtel 			if (filter == NULL)
1190d3623099SNicolas Dichtel 				return -ENOMEM;
1191d3623099SNicolas Dichtel 		}
1192d3623099SNicolas Dichtel 
1193d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
1194d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
1195d3623099SNicolas Dichtel 
1196d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
11971ba5bf99SVegard Nossum 		cb->args[0] = 1;
11984c563f76STimo Teras 	}
11994c563f76STimo Teras 
1200fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	return skb->len;
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
12061da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
12071da177e4SLinus Torvalds {
12081da177e4SLinus Torvalds 	struct xfrm_dump_info info;
12091da177e4SLinus Torvalds 	struct sk_buff *skb;
1210864745d2SMathias Krause 	int err;
12111da177e4SLinus Torvalds 
12127deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
12131da177e4SLinus Torvalds 	if (!skb)
12141da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	info.in_skb = in_skb;
12171da177e4SLinus Torvalds 	info.out_skb = skb;
12181da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
12191da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
12201da177e4SLinus Torvalds 
1221864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
1222864745d2SMathias Krause 	if (err) {
12231da177e4SLinus Torvalds 		kfree_skb(skb);
1224864745d2SMathias Krause 		return ERR_PTR(err);
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	return skb;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
123021ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
123121ee543eSMichal Kubecek  * Must be called with RCU read lock.
123221ee543eSMichal Kubecek  */
123321ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
123421ee543eSMichal Kubecek 				       u32 pid, unsigned int group)
123521ee543eSMichal Kubecek {
123621ee543eSMichal Kubecek 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
12375461fc0cSDmitry Safonov 	struct xfrm_translator *xtr;
123821ee543eSMichal Kubecek 
123986126b77SFlorian Westphal 	if (!nlsk) {
124086126b77SFlorian Westphal 		kfree_skb(skb);
124186126b77SFlorian Westphal 		return -EPIPE;
124286126b77SFlorian Westphal 	}
124386126b77SFlorian Westphal 
12445461fc0cSDmitry Safonov 	xtr = xfrm_get_translator();
12455461fc0cSDmitry Safonov 	if (xtr) {
12465461fc0cSDmitry Safonov 		int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
12475461fc0cSDmitry Safonov 
12485461fc0cSDmitry Safonov 		xfrm_put_translator(xtr);
12495461fc0cSDmitry Safonov 		if (err) {
12505461fc0cSDmitry Safonov 			kfree_skb(skb);
12515461fc0cSDmitry Safonov 			return err;
12525461fc0cSDmitry Safonov 		}
12535461fc0cSDmitry Safonov 	}
12545461fc0cSDmitry Safonov 
125521ee543eSMichal Kubecek 	return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
125621ee543eSMichal Kubecek }
125721ee543eSMichal Kubecek 
1258a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void)
12597deb2264SThomas Graf {
12607deb2264SThomas Graf 	return NLMSG_ALIGN(4)
12617deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1262880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1263880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1264880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
12657deb2264SThomas Graf }
12667deb2264SThomas Graf 
1267e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
126815e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
1269ecfd6b18SJamal Hadi Salim {
12705a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
12715a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
12725a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1273880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1274ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
12751d1e34ddSDavid S. Miller 	int err;
1276ecfd6b18SJamal Hadi Salim 	u32 *f;
1277880a6fabSChristophe Gouault 	unsigned lseq;
1278ecfd6b18SJamal Hadi Salim 
127915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
128025985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1281ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1282ecfd6b18SJamal Hadi Salim 
1283ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1284ecfd6b18SJamal Hadi Salim 	*f = flags;
1285e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
12865a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
12875a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
12885a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
12895a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
12905a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
12915a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
12925a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
12935a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1294ecfd6b18SJamal Hadi Salim 
1295880a6fabSChristophe Gouault 	do {
1296880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1297880a6fabSChristophe Gouault 
1298880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1299880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1300880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1301880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1302880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1303880a6fabSChristophe Gouault 
13041d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
13051d1e34ddSDavid S. Miller 	if (!err)
13061d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1307880a6fabSChristophe Gouault 	if (!err)
1308880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1309880a6fabSChristophe Gouault 	if (!err)
1310880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
13111d1e34ddSDavid S. Miller 	if (err) {
13121d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
13131d1e34ddSDavid S. Miller 		return err;
13141d1e34ddSDavid S. Miller 	}
1315ecfd6b18SJamal Hadi Salim 
1316053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1317053c095aSJohannes Berg 	return 0;
1318ecfd6b18SJamal Hadi Salim }
1319ecfd6b18SJamal Hadi Salim 
1320880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
13213bec6c3eSSabrina Dubroca 			    struct nlattr **attrs,
13223bec6c3eSSabrina Dubroca 			    struct netlink_ext_ack *extack)
1323880a6fabSChristophe Gouault {
1324880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1325880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1326880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1327880a6fabSChristophe Gouault 
1328880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1329880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1330880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1331880a6fabSChristophe Gouault 
1332880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1333880a6fabSChristophe Gouault 			return -EINVAL;
1334880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1335880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1336880a6fabSChristophe Gouault 			return -EINVAL;
1337880a6fabSChristophe Gouault 	}
1338880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1339880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1340880a6fabSChristophe Gouault 
1341880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1342880a6fabSChristophe Gouault 			return -EINVAL;
1343880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1344880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1345880a6fabSChristophe Gouault 			return -EINVAL;
1346880a6fabSChristophe Gouault 	}
1347880a6fabSChristophe Gouault 
1348880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1349880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1350880a6fabSChristophe Gouault 		if (thresh4) {
1351880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1352880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1353880a6fabSChristophe Gouault 		}
1354880a6fabSChristophe Gouault 		if (thresh6) {
1355880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1356880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1357880a6fabSChristophe Gouault 		}
1358880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1359880a6fabSChristophe Gouault 
1360880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1361880a6fabSChristophe Gouault 	}
1362880a6fabSChristophe Gouault 
1363880a6fabSChristophe Gouault 	return 0;
1364880a6fabSChristophe Gouault }
1365880a6fabSChristophe Gouault 
1366ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
13673bec6c3eSSabrina Dubroca 			    struct nlattr **attrs,
13683bec6c3eSSabrina Dubroca 			    struct netlink_ext_ack *extack)
1369ecfd6b18SJamal Hadi Salim {
1370a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1371ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
13727b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
137315e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1374ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
13752fc5f83bSGustavo A. R. Silva 	int err;
1376ecfd6b18SJamal Hadi Salim 
13777deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1378ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1379ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1380ecfd6b18SJamal Hadi Salim 
13812fc5f83bSGustavo A. R. Silva 	err = build_spdinfo(r_skb, net, sportid, seq, *flags);
13822fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
1383ecfd6b18SJamal Hadi Salim 
138415e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1385ecfd6b18SJamal Hadi Salim }
1386ecfd6b18SJamal Hadi Salim 
1387a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void)
13887deb2264SThomas Graf {
13897deb2264SThomas Graf 	return NLMSG_ALIGN(4)
13907deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
13917deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
13927deb2264SThomas Graf }
13937deb2264SThomas Graf 
1394e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
139515e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
139628d8909bSJamal Hadi Salim {
1397af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1398af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
139928d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
14001d1e34ddSDavid S. Miller 	int err;
140128d8909bSJamal Hadi Salim 	u32 *f;
140228d8909bSJamal Hadi Salim 
140315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
140425985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
140528d8909bSJamal Hadi Salim 		return -EMSGSIZE;
140628d8909bSJamal Hadi Salim 
140728d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
140828d8909bSJamal Hadi Salim 	*f = flags;
1409e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
141028d8909bSJamal Hadi Salim 
1411af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1412af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1413af11e316SJamal Hadi Salim 
14141d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
14151d1e34ddSDavid S. Miller 	if (!err)
14161d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
14171d1e34ddSDavid S. Miller 	if (err) {
14181d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
14191d1e34ddSDavid S. Miller 		return err;
14201d1e34ddSDavid S. Miller 	}
142128d8909bSJamal Hadi Salim 
1422053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1423053c095aSJohannes Berg 	return 0;
142428d8909bSJamal Hadi Salim }
142528d8909bSJamal Hadi Salim 
142628d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
14273bec6c3eSSabrina Dubroca 			    struct nlattr **attrs,
14283bec6c3eSSabrina Dubroca 			    struct netlink_ext_ack *extack)
142928d8909bSJamal Hadi Salim {
1430a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
143128d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
14327b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
143315e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
143428d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
14352fc5f83bSGustavo A. R. Silva 	int err;
143628d8909bSJamal Hadi Salim 
14377deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
143828d8909bSJamal Hadi Salim 	if (r_skb == NULL)
143928d8909bSJamal Hadi Salim 		return -ENOMEM;
144028d8909bSJamal Hadi Salim 
14412fc5f83bSGustavo A. R. Silva 	err = build_sadinfo(r_skb, net, sportid, seq, *flags);
14422fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
144328d8909bSJamal Hadi Salim 
144415e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
144528d8909bSJamal Hadi Salim }
144628d8909bSJamal Hadi Salim 
144722e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
14483bec6c3eSSabrina Dubroca 		       struct nlattr **attrs, struct netlink_ext_ack *extack)
14491da177e4SLinus Torvalds {
1450fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
14517b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
14521da177e4SLinus Torvalds 	struct xfrm_state *x;
14531da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1454eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
14551da177e4SLinus Torvalds 
1456fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
14571da177e4SLinus Torvalds 	if (x == NULL)
14581da177e4SLinus Torvalds 		goto out_noput;
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
14611da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
14621da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
14631da177e4SLinus Torvalds 	} else {
146415e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
14651da177e4SLinus Torvalds 	}
14661da177e4SLinus Torvalds 	xfrm_state_put(x);
14671da177e4SLinus Torvalds out_noput:
14681da177e4SLinus Torvalds 	return err;
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
147122e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
14723bec6c3eSSabrina Dubroca 			      struct nlattr **attrs,
14733bec6c3eSSabrina Dubroca 			      struct netlink_ext_ack *extack)
14741da177e4SLinus Torvalds {
1475fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
14761da177e4SLinus Torvalds 	struct xfrm_state *x;
14771da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
14785f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
14791da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
14801da177e4SLinus Torvalds 	xfrm_address_t *daddr;
14811da177e4SLinus Torvalds 	int family;
14821da177e4SLinus Torvalds 	int err;
14836f26b61eSJamal Hadi Salim 	u32 mark;
14846f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
14857e652640SSteffen Klassert 	u32 if_id = 0;
14861da177e4SLinus Torvalds 
14877b67c857SThomas Graf 	p = nlmsg_data(nlh);
1488776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
14891da177e4SLinus Torvalds 	if (err)
14901da177e4SLinus Torvalds 		goto out_noput;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	family = p->info.family;
14931da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	x = NULL;
14966f26b61eSJamal Hadi Salim 
14976f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
14987e652640SSteffen Klassert 
1499a3d9001bSKai Lueke 	if (attrs[XFRMA_IF_ID])
15007e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
15017e652640SSteffen Klassert 
15021da177e4SLinus Torvalds 	if (p->info.seq) {
15036f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
150470e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
15051da177e4SLinus Torvalds 			xfrm_state_put(x);
15061da177e4SLinus Torvalds 			x = NULL;
15071da177e4SLinus Torvalds 		}
15081da177e4SLinus Torvalds 	}
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	if (!x)
15116f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
15127e652640SSteffen Klassert 				  if_id, p->info.id.proto, daddr,
15131da177e4SLinus Torvalds 				  &p->info.saddr, 1,
15141da177e4SLinus Torvalds 				  family);
15151da177e4SLinus Torvalds 	err = -ENOENT;
15161da177e4SLinus Torvalds 	if (x == NULL)
15171da177e4SLinus Torvalds 		goto out_noput;
15181da177e4SLinus Torvalds 
1519658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1520658b219eSHerbert Xu 	if (err)
1521658b219eSHerbert Xu 		goto out;
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
15241da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
15251da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
15261da177e4SLinus Torvalds 		goto out;
15271da177e4SLinus Torvalds 	}
15281da177e4SLinus Torvalds 
15295f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
15305f3eea6bSDmitry Safonov 	if (xtr) {
15315f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
15325f3eea6bSDmitry Safonov 
15335f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
15345f3eea6bSDmitry Safonov 		if (err) {
15355f3eea6bSDmitry Safonov 			kfree_skb(resp_skb);
15365f3eea6bSDmitry Safonov 			goto out;
15375f3eea6bSDmitry Safonov 		}
15385f3eea6bSDmitry Safonov 	}
15395f3eea6bSDmitry Safonov 
154015e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds out:
15431da177e4SLinus Torvalds 	xfrm_state_put(x);
15441da177e4SLinus Torvalds out_noput:
15451da177e4SLinus Torvalds 	return err;
15461da177e4SLinus Torvalds }
15471da177e4SLinus Torvalds 
154824fc544fSSabrina Dubroca static int verify_policy_dir(u8 dir, struct netlink_ext_ack *extack)
15491da177e4SLinus Torvalds {
15501da177e4SLinus Torvalds 	switch (dir) {
15511da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
15521da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
15531da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
15541da177e4SLinus Torvalds 		break;
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	default:
155724fc544fSSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Invalid policy direction");
15581da177e4SLinus Torvalds 		return -EINVAL;
15593ff50b79SStephen Hemminger 	}
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	return 0;
15621da177e4SLinus Torvalds }
15631da177e4SLinus Torvalds 
1564fb7deabaSSabrina Dubroca static int verify_policy_type(u8 type, struct netlink_ext_ack *extack)
1565f7b6983fSMasahide NAKAMURA {
1566f7b6983fSMasahide NAKAMURA 	switch (type) {
1567f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1568f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1569f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1570f7b6983fSMasahide NAKAMURA #endif
1571f7b6983fSMasahide NAKAMURA 		break;
1572f7b6983fSMasahide NAKAMURA 
1573f7b6983fSMasahide NAKAMURA 	default:
1574fb7deabaSSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Invalid policy type");
1575f7b6983fSMasahide NAKAMURA 		return -EINVAL;
15763ff50b79SStephen Hemminger 	}
1577f7b6983fSMasahide NAKAMURA 
1578f7b6983fSMasahide NAKAMURA 	return 0;
1579f7b6983fSMasahide NAKAMURA }
1580f7b6983fSMasahide NAKAMURA 
1581ec2b4f01SSabrina Dubroca static int verify_newpolicy_info(struct xfrm_userpolicy_info *p,
1582ec2b4f01SSabrina Dubroca 				 struct netlink_ext_ack *extack)
15831da177e4SLinus Torvalds {
1584e682adf0SFan Du 	int ret;
1585e682adf0SFan Du 
15861da177e4SLinus Torvalds 	switch (p->share) {
15871da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
15881da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
15891da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
15901da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
15911da177e4SLinus Torvalds 		break;
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	default:
1594ec2b4f01SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Invalid policy share");
15951da177e4SLinus Torvalds 		return -EINVAL;
15963ff50b79SStephen Hemminger 	}
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	switch (p->action) {
15991da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
16001da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
16011da177e4SLinus Torvalds 		break;
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	default:
1604ec2b4f01SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Invalid policy action");
16051da177e4SLinus Torvalds 		return -EINVAL;
16063ff50b79SStephen Hemminger 	}
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 	switch (p->sel.family) {
16091da177e4SLinus Torvalds 	case AF_INET:
1610ec2b4f01SSabrina Dubroca 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
1611ec2b4f01SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
161207bf7908SSteffen Klassert 			return -EINVAL;
1613ec2b4f01SSabrina Dubroca 		}
161407bf7908SSteffen Klassert 
16151da177e4SLinus Torvalds 		break;
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	case AF_INET6:
1618dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1619ec2b4f01SSabrina Dubroca 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
1620ec2b4f01SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
162107bf7908SSteffen Klassert 			return -EINVAL;
1622ec2b4f01SSabrina Dubroca 		}
162307bf7908SSteffen Klassert 
16241da177e4SLinus Torvalds 		break;
16251da177e4SLinus Torvalds #else
1626ec2b4f01SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "IPv6 support disabled");
16271da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
16281da177e4SLinus Torvalds #endif
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	default:
1631ec2b4f01SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Invalid selector family");
16321da177e4SLinus Torvalds 		return -EINVAL;
16333ff50b79SStephen Hemminger 	}
16341da177e4SLinus Torvalds 
163524fc544fSSabrina Dubroca 	ret = verify_policy_dir(p->dir, extack);
1636e682adf0SFan Du 	if (ret)
1637e682adf0SFan Du 		return ret;
1638ec2b4f01SSabrina Dubroca 	if (p->index && (xfrm_policy_id2dir(p->index) != p->dir)) {
1639ec2b4f01SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Policy index doesn't match direction");
1640e682adf0SFan Du 		return -EINVAL;
1641ec2b4f01SSabrina Dubroca 	}
1642e682adf0SFan Du 
1643e682adf0SFan Du 	return 0;
16441da177e4SLinus Torvalds }
16451da177e4SLinus Torvalds 
16465424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1647df71837dSTrent Jaeger {
16485424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1649df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1650df71837dSTrent Jaeger 
1651df71837dSTrent Jaeger 	if (!rt)
1652df71837dSTrent Jaeger 		return 0;
1653df71837dSTrent Jaeger 
16545424f32eSThomas Graf 	uctx = nla_data(rt);
165552a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1656df71837dSTrent Jaeger }
1657df71837dSTrent Jaeger 
16581da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
16591da177e4SLinus Torvalds 			   int nr)
16601da177e4SLinus Torvalds {
16611da177e4SLinus Torvalds 	int i;
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
16641da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
16651da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
16681da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
16691da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
16701da177e4SLinus Torvalds 		t->reqid = ut->reqid;
16711da177e4SLinus Torvalds 		t->mode = ut->mode;
16721da177e4SLinus Torvalds 		t->share = ut->share;
16731da177e4SLinus Torvalds 		t->optional = ut->optional;
16741da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
16751da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
16761da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1677c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1678c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
16798511d01dSMiika Komu 		t->encap_family = ut->family;
16801da177e4SLinus Torvalds 	}
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds 
1683d37bed89SSabrina Dubroca static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family,
1684d37bed89SSabrina Dubroca 			 struct netlink_ext_ack *extack)
1685b4ad86bfSDavid S. Miller {
1686732706afSSteffen Klassert 	u16 prev_family;
1687b4ad86bfSDavid S. Miller 	int i;
1688b4ad86bfSDavid S. Miller 
1689d37bed89SSabrina Dubroca 	if (nr > XFRM_MAX_DEPTH) {
1690d37bed89SSabrina Dubroca 		NL_SET_ERR_MSG(extack, "Template count must be <= XFRM_MAX_DEPTH (" __stringify(XFRM_MAX_DEPTH) ")");
1691b4ad86bfSDavid S. Miller 		return -EINVAL;
1692d37bed89SSabrina Dubroca 	}
1693b4ad86bfSDavid S. Miller 
1694732706afSSteffen Klassert 	prev_family = family;
1695732706afSSteffen Klassert 
1696b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1697b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1698b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1699b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1700b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1701b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1702b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1703b4ad86bfSDavid S. Miller 		 */
1704b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1705b4ad86bfSDavid S. Miller 			ut[i].family = family;
1706b4ad86bfSDavid S. Miller 
170735e61038SFlorian Westphal 		switch (ut[i].mode) {
170835e61038SFlorian Westphal 		case XFRM_MODE_TUNNEL:
170935e61038SFlorian Westphal 		case XFRM_MODE_BEET:
171035e61038SFlorian Westphal 			break;
171135e61038SFlorian Westphal 		default:
1712d37bed89SSabrina Dubroca 			if (ut[i].family != prev_family) {
1713d37bed89SSabrina Dubroca 				NL_SET_ERR_MSG(extack, "Mode in template doesn't support a family change");
1714732706afSSteffen Klassert 				return -EINVAL;
1715d37bed89SSabrina Dubroca 			}
171635e61038SFlorian Westphal 			break;
171735e61038SFlorian Westphal 		}
1718d37bed89SSabrina Dubroca 		if (ut[i].mode >= XFRM_MODE_MAX) {
1719d37bed89SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Mode in template must be < XFRM_MODE_MAX (" __stringify(XFRM_MODE_MAX) ")");
172032bf94fbSSean Tranchetti 			return -EINVAL;
1721d37bed89SSabrina Dubroca 		}
172232bf94fbSSean Tranchetti 
1723732706afSSteffen Klassert 		prev_family = ut[i].family;
1724732706afSSteffen Klassert 
1725b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1726b4ad86bfSDavid S. Miller 		case AF_INET:
1727b4ad86bfSDavid S. Miller 			break;
1728dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1729b4ad86bfSDavid S. Miller 		case AF_INET6:
1730b4ad86bfSDavid S. Miller 			break;
1731b4ad86bfSDavid S. Miller #endif
1732b4ad86bfSDavid S. Miller 		default:
1733d37bed89SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Invalid family in template");
1734b4ad86bfSDavid S. Miller 			return -EINVAL;
17353ff50b79SStephen Hemminger 		}
17366a53b759SCong Wang 
1737d37bed89SSabrina Dubroca 		if (!xfrm_id_proto_valid(ut[i].id.proto)) {
1738d37bed89SSabrina Dubroca 			NL_SET_ERR_MSG(extack, "Invalid XFRM protocol in template");
17396a53b759SCong Wang 			return -EINVAL;
17406a53b759SCong Wang 		}
1741d37bed89SSabrina Dubroca 	}
17426a53b759SCong Wang 
1743b4ad86bfSDavid S. Miller 	return 0;
1744b4ad86bfSDavid S. Miller }
1745b4ad86bfSDavid S. Miller 
1746d37bed89SSabrina Dubroca static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs,
1747d37bed89SSabrina Dubroca 			       struct netlink_ext_ack *extack)
17481da177e4SLinus Torvalds {
17495424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	if (!rt) {
17521da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
17531da177e4SLinus Torvalds 	} else {
17545424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
17555424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1756b4ad86bfSDavid S. Miller 		int err;
17571da177e4SLinus Torvalds 
1758d37bed89SSabrina Dubroca 		err = validate_tmpl(nr, utmpl, pol->family, extack);
1759b4ad86bfSDavid S. Miller 		if (err)
1760b4ad86bfSDavid S. Miller 			return err;
17611da177e4SLinus Torvalds 
17625424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
17631da177e4SLinus Torvalds 	}
17641da177e4SLinus Torvalds 	return 0;
17651da177e4SLinus Torvalds }
17661da177e4SLinus Torvalds 
1767fb7deabaSSabrina Dubroca static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs,
1768fb7deabaSSabrina Dubroca 				      struct netlink_ext_ack *extack)
1769f7b6983fSMasahide NAKAMURA {
17705424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1771f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1772b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1773f7b6983fSMasahide NAKAMURA 	int err;
1774f7b6983fSMasahide NAKAMURA 
1775f7b6983fSMasahide NAKAMURA 	if (rt) {
17765424f32eSThomas Graf 		upt = nla_data(rt);
1777f7b6983fSMasahide NAKAMURA 		type = upt->type;
1778f7b6983fSMasahide NAKAMURA 	}
1779f7b6983fSMasahide NAKAMURA 
1780fb7deabaSSabrina Dubroca 	err = verify_policy_type(type, extack);
1781f7b6983fSMasahide NAKAMURA 	if (err)
1782f7b6983fSMasahide NAKAMURA 		return err;
1783f7b6983fSMasahide NAKAMURA 
1784f7b6983fSMasahide NAKAMURA 	*tp = type;
1785f7b6983fSMasahide NAKAMURA 	return 0;
1786f7b6983fSMasahide NAKAMURA }
1787f7b6983fSMasahide NAKAMURA 
17881da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
17891da177e4SLinus Torvalds {
17901da177e4SLinus Torvalds 	xp->priority = p->priority;
17911da177e4SLinus Torvalds 	xp->index = p->index;
17921da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
17931da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
17941da177e4SLinus Torvalds 	xp->action = p->action;
17951da177e4SLinus Torvalds 	xp->flags = p->flags;
17961da177e4SLinus Torvalds 	xp->family = p->sel.family;
17971da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
17981da177e4SLinus Torvalds }
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
18011da177e4SLinus Torvalds {
18027b789836SMathias Krause 	memset(p, 0, sizeof(*p));
18031da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
18041da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
18051da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
18061da177e4SLinus Torvalds 	p->priority = xp->priority;
18071da177e4SLinus Torvalds 	p->index = xp->index;
18081da177e4SLinus Torvalds 	p->sel.family = xp->family;
18091da177e4SLinus Torvalds 	p->dir = dir;
18101da177e4SLinus Torvalds 	p->action = xp->action;
18111da177e4SLinus Torvalds 	p->flags = xp->flags;
18121da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
18131da177e4SLinus Torvalds }
18141da177e4SLinus Torvalds 
1815fb7deabaSSabrina Dubroca static struct xfrm_policy *xfrm_policy_construct(struct net *net,
1816fb7deabaSSabrina Dubroca 						 struct xfrm_userpolicy_info *p,
1817fb7deabaSSabrina Dubroca 						 struct nlattr **attrs,
1818fb7deabaSSabrina Dubroca 						 int *errp,
1819fb7deabaSSabrina Dubroca 						 struct netlink_ext_ack *extack)
18201da177e4SLinus Torvalds {
1821fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
18221da177e4SLinus Torvalds 	int err;
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds 	if (!xp) {
18251da177e4SLinus Torvalds 		*errp = -ENOMEM;
18261da177e4SLinus Torvalds 		return NULL;
18271da177e4SLinus Torvalds 	}
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1830df71837dSTrent Jaeger 
1831fb7deabaSSabrina Dubroca 	err = copy_from_user_policy_type(&xp->type, attrs, extack);
1832f7b6983fSMasahide NAKAMURA 	if (err)
1833f7b6983fSMasahide NAKAMURA 		goto error;
1834f7b6983fSMasahide NAKAMURA 
1835d37bed89SSabrina Dubroca 	if (!(err = copy_from_user_tmpl(xp, attrs, extack)))
183635a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1837f7b6983fSMasahide NAKAMURA 	if (err)
1838f7b6983fSMasahide NAKAMURA 		goto error;
18391da177e4SLinus Torvalds 
1840295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1841295fae56SJamal Hadi Salim 
1842a3d9001bSKai Lueke 	if (attrs[XFRMA_IF_ID])
18437e652640SSteffen Klassert 		xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
18447e652640SSteffen Klassert 
18451da177e4SLinus Torvalds 	return xp;
1846f7b6983fSMasahide NAKAMURA  error:
1847f7b6983fSMasahide NAKAMURA 	*errp = err;
184812a169e7SHerbert Xu 	xp->walk.dead = 1;
184964c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1850f7b6983fSMasahide NAKAMURA 	return NULL;
18511da177e4SLinus Torvalds }
18521da177e4SLinus Torvalds 
185322e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
18543bec6c3eSSabrina Dubroca 			   struct nlattr **attrs,
18553bec6c3eSSabrina Dubroca 			   struct netlink_ext_ack *extack)
18561da177e4SLinus Torvalds {
1857fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
18587b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
18591da177e4SLinus Torvalds 	struct xfrm_policy *xp;
186026b15dadSJamal Hadi Salim 	struct km_event c;
18611da177e4SLinus Torvalds 	int err;
18621da177e4SLinus Torvalds 	int excl;
18631da177e4SLinus Torvalds 
1864ec2b4f01SSabrina Dubroca 	err = verify_newpolicy_info(p, extack);
18651da177e4SLinus Torvalds 	if (err)
18661da177e4SLinus Torvalds 		return err;
186708a717e4SSabrina Dubroca 	err = verify_sec_ctx_len(attrs, extack);
1868df71837dSTrent Jaeger 	if (err)
1869df71837dSTrent Jaeger 		return err;
18701da177e4SLinus Torvalds 
1871fb7deabaSSabrina Dubroca 	xp = xfrm_policy_construct(net, p, attrs, &err, extack);
18721da177e4SLinus Torvalds 	if (!xp)
18731da177e4SLinus Torvalds 		return err;
18741da177e4SLinus Torvalds 
187525985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
187626b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
1877a7fd0e6dSBhaskar Chowdhury 	 * in netlink excl is a flag and you wouldn't need
187826b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
18791da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
18801da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
18812e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1882161a09e7SJoy Latten 
18831da177e4SLinus Torvalds 	if (err) {
188403e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
18851da177e4SLinus Torvalds 		kfree(xp);
18861da177e4SLinus Torvalds 		return err;
18871da177e4SLinus Torvalds 	}
18881da177e4SLinus Torvalds 
1889f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
189026b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
189115e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
189226b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
189326b15dadSJamal Hadi Salim 
18941da177e4SLinus Torvalds 	xfrm_pol_put(xp);
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 	return 0;
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
19001da177e4SLinus Torvalds {
19011da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
19021da177e4SLinus Torvalds 	int i;
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
19051da177e4SLinus Torvalds 		return 0;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
19081da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
19091da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
19101da177e4SLinus Torvalds 
19111f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
19121da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
19138511d01dSMiika Komu 		up->family = kp->encap_family;
19141da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
19151da177e4SLinus Torvalds 		up->reqid = kp->reqid;
19161da177e4SLinus Torvalds 		up->mode = kp->mode;
19171da177e4SLinus Torvalds 		up->share = kp->share;
19181da177e4SLinus Torvalds 		up->optional = kp->optional;
19191da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
19201da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
19211da177e4SLinus Torvalds 		up->calgos = kp->calgos;
19221da177e4SLinus Torvalds 	}
19231da177e4SLinus Torvalds 
1924c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1925c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1926df71837dSTrent Jaeger }
1927df71837dSTrent Jaeger 
19280d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
19290d681623SSerge Hallyn {
19300d681623SSerge Hallyn 	if (x->security) {
19310d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
19320d681623SSerge Hallyn 	}
19330d681623SSerge Hallyn 	return 0;
19340d681623SSerge Hallyn }
19350d681623SSerge Hallyn 
19360d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
19370d681623SSerge Hallyn {
19381d1e34ddSDavid S. Miller 	if (xp->security)
19390d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
19400d681623SSerge Hallyn 	return 0;
19410d681623SSerge Hallyn }
1942a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void)
1943cfbfd45aSThomas Graf {
1944cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1945cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1946cfbfd45aSThomas Graf #else
1947cfbfd45aSThomas Graf 	return 0;
1948cfbfd45aSThomas Graf #endif
1949cfbfd45aSThomas Graf }
19500d681623SSerge Hallyn 
1951f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1952b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1953f7b6983fSMasahide NAKAMURA {
195445c180bcSEric Dumazet 	struct xfrm_userpolicy_type upt;
195545c180bcSEric Dumazet 
195645c180bcSEric Dumazet 	/* Sadly there are two holes in struct xfrm_userpolicy_type */
195745c180bcSEric Dumazet 	memset(&upt, 0, sizeof(upt));
195845c180bcSEric Dumazet 	upt.type = type;
1959f7b6983fSMasahide NAKAMURA 
1960c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1961f7b6983fSMasahide NAKAMURA }
1962f7b6983fSMasahide NAKAMURA 
1963f7b6983fSMasahide NAKAMURA #else
1964b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1965f7b6983fSMasahide NAKAMURA {
1966f7b6983fSMasahide NAKAMURA 	return 0;
1967f7b6983fSMasahide NAKAMURA }
1968f7b6983fSMasahide NAKAMURA #endif
1969f7b6983fSMasahide NAKAMURA 
19701da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
19711da177e4SLinus Torvalds {
19721da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
19731da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
19741da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
19751da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
19765f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
19771da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
19781d1e34ddSDavid S. Miller 	int err;
19791da177e4SLinus Torvalds 
198015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
198179b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
198279b8b7f4SThomas Graf 	if (nlh == NULL)
198379b8b7f4SThomas Graf 		return -EMSGSIZE;
19841da177e4SLinus Torvalds 
19857b67c857SThomas Graf 	p = nlmsg_data(nlh);
19861da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
19871d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
19881d1e34ddSDavid S. Miller 	if (!err)
19891d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
19901d1e34ddSDavid S. Miller 	if (!err)
19911d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
19921d1e34ddSDavid S. Miller 	if (!err)
19931d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
19947e652640SSteffen Klassert 	if (!err)
19957e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
19961d1e34ddSDavid S. Miller 	if (err) {
19971d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
19981d1e34ddSDavid S. Miller 		return err;
19991d1e34ddSDavid S. Miller 	}
20009825069dSThomas Graf 	nlmsg_end(skb, nlh);
20015f3eea6bSDmitry Safonov 
20025f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
20035f3eea6bSDmitry Safonov 	if (xtr) {
20045f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlh);
20055f3eea6bSDmitry Safonov 
20065f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
20075f3eea6bSDmitry Safonov 		if (err) {
20085f3eea6bSDmitry Safonov 			nlmsg_cancel(skb, nlh);
20095f3eea6bSDmitry Safonov 			return err;
20105f3eea6bSDmitry Safonov 		}
20115f3eea6bSDmitry Safonov 	}
20125f3eea6bSDmitry Safonov 
20131da177e4SLinus Torvalds 	return 0;
20141da177e4SLinus Torvalds }
20151da177e4SLinus Torvalds 
20164c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
20174c563f76STimo Teras {
20181137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
2019283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
20204c563f76STimo Teras 
2021283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
20224c563f76STimo Teras 	return 0;
20234c563f76STimo Teras }
20244c563f76STimo Teras 
20251137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb)
20261137b5e2SHerbert Xu {
20271137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
20281137b5e2SHerbert Xu 
20291137b5e2SHerbert Xu 	BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
20301137b5e2SHerbert Xu 
20311137b5e2SHerbert Xu 	xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
20321137b5e2SHerbert Xu 	return 0;
20331137b5e2SHerbert Xu }
20341137b5e2SHerbert Xu 
20351da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
20361da177e4SLinus Torvalds {
2037fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
20381137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
20391da177e4SLinus Torvalds 	struct xfrm_dump_info info;
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds 	info.in_skb = cb->skb;
20421da177e4SLinus Torvalds 	info.out_skb = skb;
20431da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
20441da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
20454c563f76STimo Teras 
2046fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	return skb->len;
20491da177e4SLinus Torvalds }
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
20521da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
20531da177e4SLinus Torvalds 					  int dir, u32 seq)
20541da177e4SLinus Torvalds {
20551da177e4SLinus Torvalds 	struct xfrm_dump_info info;
20561da177e4SLinus Torvalds 	struct sk_buff *skb;
2057c2546372SMathias Krause 	int err;
20581da177e4SLinus Torvalds 
20597deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
20601da177e4SLinus Torvalds 	if (!skb)
20611da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	info.in_skb = in_skb;
20641da177e4SLinus Torvalds 	info.out_skb = skb;
20651da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
20661da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
20671da177e4SLinus Torvalds 
2068c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
2069c2546372SMathias Krause 	if (err) {
20701da177e4SLinus Torvalds 		kfree_skb(skb);
2071c2546372SMathias Krause 		return ERR_PTR(err);
20721da177e4SLinus Torvalds 	}
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	return skb;
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds 
207788d0adb5SNicolas Dichtel static int xfrm_notify_userpolicy(struct net *net)
207888d0adb5SNicolas Dichtel {
207988d0adb5SNicolas Dichtel 	struct xfrm_userpolicy_default *up;
208088d0adb5SNicolas Dichtel 	int len = NLMSG_ALIGN(sizeof(*up));
208188d0adb5SNicolas Dichtel 	struct nlmsghdr *nlh;
208288d0adb5SNicolas Dichtel 	struct sk_buff *skb;
208393ec1320SNicolas Dichtel 	int err;
208488d0adb5SNicolas Dichtel 
208588d0adb5SNicolas Dichtel 	skb = nlmsg_new(len, GFP_ATOMIC);
208688d0adb5SNicolas Dichtel 	if (skb == NULL)
208788d0adb5SNicolas Dichtel 		return -ENOMEM;
208888d0adb5SNicolas Dichtel 
208988d0adb5SNicolas Dichtel 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
209088d0adb5SNicolas Dichtel 	if (nlh == NULL) {
209188d0adb5SNicolas Dichtel 		kfree_skb(skb);
209288d0adb5SNicolas Dichtel 		return -EMSGSIZE;
209388d0adb5SNicolas Dichtel 	}
209488d0adb5SNicolas Dichtel 
209588d0adb5SNicolas Dichtel 	up = nlmsg_data(nlh);
2096b58b1f56SNicolas Dichtel 	up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2097b58b1f56SNicolas Dichtel 	up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2098b58b1f56SNicolas Dichtel 	up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
209988d0adb5SNicolas Dichtel 
210088d0adb5SNicolas Dichtel 	nlmsg_end(skb, nlh);
210188d0adb5SNicolas Dichtel 
210293ec1320SNicolas Dichtel 	rcu_read_lock();
210393ec1320SNicolas Dichtel 	err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
210493ec1320SNicolas Dichtel 	rcu_read_unlock();
210593ec1320SNicolas Dichtel 
210693ec1320SNicolas Dichtel 	return err;
210788d0adb5SNicolas Dichtel }
210888d0adb5SNicolas Dichtel 
2109b58b1f56SNicolas Dichtel static bool xfrm_userpolicy_is_valid(__u8 policy)
2110b58b1f56SNicolas Dichtel {
2111b58b1f56SNicolas Dichtel 	return policy == XFRM_USERPOLICY_BLOCK ||
2112b58b1f56SNicolas Dichtel 	       policy == XFRM_USERPOLICY_ACCEPT;
2113b58b1f56SNicolas Dichtel }
2114b58b1f56SNicolas Dichtel 
21152d151d39SSteffen Klassert static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
21163bec6c3eSSabrina Dubroca 			    struct nlattr **attrs, struct netlink_ext_ack *extack)
21172d151d39SSteffen Klassert {
21182d151d39SSteffen Klassert 	struct net *net = sock_net(skb->sk);
21192d151d39SSteffen Klassert 	struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
21202d151d39SSteffen Klassert 
2121b58b1f56SNicolas Dichtel 	if (xfrm_userpolicy_is_valid(up->in))
2122b58b1f56SNicolas Dichtel 		net->xfrm.policy_default[XFRM_POLICY_IN] = up->in;
21235d8dbb7fSPavel Skripkin 
2124b58b1f56SNicolas Dichtel 	if (xfrm_userpolicy_is_valid(up->fwd))
2125b58b1f56SNicolas Dichtel 		net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd;
21265d8dbb7fSPavel Skripkin 
2127b58b1f56SNicolas Dichtel 	if (xfrm_userpolicy_is_valid(up->out))
2128b58b1f56SNicolas Dichtel 		net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out;
21292d151d39SSteffen Klassert 
21302d151d39SSteffen Klassert 	rt_genid_bump_all(net);
21312d151d39SSteffen Klassert 
213288d0adb5SNicolas Dichtel 	xfrm_notify_userpolicy(net);
21332d151d39SSteffen Klassert 	return 0;
21342d151d39SSteffen Klassert }
21352d151d39SSteffen Klassert 
21362d151d39SSteffen Klassert static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
21373bec6c3eSSabrina Dubroca 			    struct nlattr **attrs, struct netlink_ext_ack *extack)
21382d151d39SSteffen Klassert {
21392d151d39SSteffen Klassert 	struct sk_buff *r_skb;
21402d151d39SSteffen Klassert 	struct nlmsghdr *r_nlh;
21412d151d39SSteffen Klassert 	struct net *net = sock_net(skb->sk);
2142f8d858e6SNicolas Dichtel 	struct xfrm_userpolicy_default *r_up;
21432d151d39SSteffen Klassert 	int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
21442d151d39SSteffen Klassert 	u32 portid = NETLINK_CB(skb).portid;
21452d151d39SSteffen Klassert 	u32 seq = nlh->nlmsg_seq;
21462d151d39SSteffen Klassert 
21472d151d39SSteffen Klassert 	r_skb = nlmsg_new(len, GFP_ATOMIC);
21482d151d39SSteffen Klassert 	if (!r_skb)
21492d151d39SSteffen Klassert 		return -ENOMEM;
21502d151d39SSteffen Klassert 
21512d151d39SSteffen Klassert 	r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
21522d151d39SSteffen Klassert 	if (!r_nlh) {
21532d151d39SSteffen Klassert 		kfree_skb(r_skb);
21542d151d39SSteffen Klassert 		return -EMSGSIZE;
21552d151d39SSteffen Klassert 	}
21562d151d39SSteffen Klassert 
21572d151d39SSteffen Klassert 	r_up = nlmsg_data(r_nlh);
2158b58b1f56SNicolas Dichtel 	r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2159b58b1f56SNicolas Dichtel 	r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2160b58b1f56SNicolas Dichtel 	r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
21612d151d39SSteffen Klassert 	nlmsg_end(r_skb, r_nlh);
21622d151d39SSteffen Klassert 
21632d151d39SSteffen Klassert 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
21642d151d39SSteffen Klassert }
21652d151d39SSteffen Klassert 
216622e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
21673bec6c3eSSabrina Dubroca 			   struct nlattr **attrs,
21683bec6c3eSSabrina Dubroca 			   struct netlink_ext_ack *extack)
21691da177e4SLinus Torvalds {
2170fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
21711da177e4SLinus Torvalds 	struct xfrm_policy *xp;
21721da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
2173b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
21741da177e4SLinus Torvalds 	int err;
217526b15dadSJamal Hadi Salim 	struct km_event c;
21761da177e4SLinus Torvalds 	int delete;
2177295fae56SJamal Hadi Salim 	struct xfrm_mark m;
21787e652640SSteffen Klassert 	u32 if_id = 0;
21791da177e4SLinus Torvalds 
21807b67c857SThomas Graf 	p = nlmsg_data(nlh);
21811da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
21821da177e4SLinus Torvalds 
2183fb7deabaSSabrina Dubroca 	err = copy_from_user_policy_type(&type, attrs, extack);
2184f7b6983fSMasahide NAKAMURA 	if (err)
2185f7b6983fSMasahide NAKAMURA 		return err;
2186f7b6983fSMasahide NAKAMURA 
218724fc544fSSabrina Dubroca 	err = verify_policy_dir(p->dir, extack);
21881da177e4SLinus Torvalds 	if (err)
21891da177e4SLinus Torvalds 		return err;
21901da177e4SLinus Torvalds 
21917e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
21927e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
21937e652640SSteffen Klassert 
21944f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
21954f47e8abSXin Long 
21961da177e4SLinus Torvalds 	if (p->index)
21974f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
21984f47e8abSXin Long 				      p->index, delete, &err);
2199df71837dSTrent Jaeger 	else {
22005424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
220103e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
2202df71837dSTrent Jaeger 
220308a717e4SSabrina Dubroca 		err = verify_sec_ctx_len(attrs, extack);
2204df71837dSTrent Jaeger 		if (err)
2205df71837dSTrent Jaeger 			return err;
2206df71837dSTrent Jaeger 
22072c8dd116SDenis V. Lunev 		ctx = NULL;
2208df71837dSTrent Jaeger 		if (rt) {
22095424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2210df71837dSTrent Jaeger 
221152a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
221203e1ad7bSPaul Moore 			if (err)
2213df71837dSTrent Jaeger 				return err;
22142c8dd116SDenis V. Lunev 		}
22154f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
22164f47e8abSXin Long 					   &p->sel, ctx, delete, &err);
221703e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
2218df71837dSTrent Jaeger 	}
22191da177e4SLinus Torvalds 	if (xp == NULL)
22201da177e4SLinus Torvalds 		return -ENOENT;
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds 	if (!delete) {
22231da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
22261da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
22271da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
22281da177e4SLinus Torvalds 		} else {
2229a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
223015e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
22311da177e4SLinus Torvalds 		}
223226b15dadSJamal Hadi Salim 	} else {
22332e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
223413fcfbb0SDavid S. Miller 
223513fcfbb0SDavid S. Miller 		if (err != 0)
2236c8c05a8eSCatherine Zhang 			goto out;
223713fcfbb0SDavid S. Miller 
2238e7443892SHerbert Xu 		c.data.byid = p->index;
2239f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
224026b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
224115e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
224226b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
22431da177e4SLinus Torvalds 	}
22441da177e4SLinus Torvalds 
2245c8c05a8eSCatherine Zhang out:
2246ef41aaa0SEric Paris 	xfrm_pol_put(xp);
22471da177e4SLinus Torvalds 	return err;
22481da177e4SLinus Torvalds }
22491da177e4SLinus Torvalds 
225022e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
22513bec6c3eSSabrina Dubroca 			 struct nlattr **attrs,
22523bec6c3eSSabrina Dubroca 			 struct netlink_ext_ack *extack)
22531da177e4SLinus Torvalds {
2254fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
225526b15dadSJamal Hadi Salim 	struct km_event c;
22567b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
22574aa2e62cSJoy Latten 	int err;
22581da177e4SLinus Torvalds 
2259f75a2804SCong Wang 	err = xfrm_state_flush(net, p->proto, true, false);
22609e64cc95SJamal Hadi Salim 	if (err) {
22619e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
22629e64cc95SJamal Hadi Salim 			return 0;
2263069c474eSDavid S. Miller 		return err;
22649e64cc95SJamal Hadi Salim 	}
2265bf08867fSHerbert Xu 	c.data.proto = p->proto;
2266f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
226726b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
226815e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
22697067802eSAlexey Dobriyan 	c.net = net;
227026b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
227126b15dadSJamal Hadi Salim 
22721da177e4SLinus Torvalds 	return 0;
22731da177e4SLinus Torvalds }
22741da177e4SLinus Torvalds 
2275a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
22767deb2264SThomas Graf {
2277a1b831f2SAlexey Dobriyan 	unsigned int replay_size = x->replay_esn ?
2278d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
2279d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
2280d8647b79SSteffen Klassert 
22817deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2282d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
2283de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
22846f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
22857deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
22867deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
22877deb2264SThomas Graf }
2288d51d081dSJamal Hadi Salim 
2289214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2290d51d081dSJamal Hadi Salim {
2291d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
2292d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
22931d1e34ddSDavid S. Miller 	int err;
2294d51d081dSJamal Hadi Salim 
229515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
229679b8b7f4SThomas Graf 	if (nlh == NULL)
229779b8b7f4SThomas Graf 		return -EMSGSIZE;
2298d51d081dSJamal Hadi Salim 
22997b67c857SThomas Graf 	id = nlmsg_data(nlh);
2300931e79d7SMathias Krause 	memset(&id->sa_id, 0, sizeof(id->sa_id));
23012b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2302d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
2303d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
2304d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
23052b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
23062b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
2307d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
2308d51d081dSJamal Hadi Salim 
2309d0fde795SDavid S. Miller 	if (x->replay_esn) {
23101d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2311d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
23121d1e34ddSDavid S. Miller 			      x->replay_esn);
2313d0fde795SDavid S. Miller 	} else {
23141d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
23151d1e34ddSDavid S. Miller 			      &x->replay);
2316d0fde795SDavid S. Miller 	}
23171d1e34ddSDavid S. Miller 	if (err)
23181d1e34ddSDavid S. Miller 		goto out_cancel;
2319de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2320de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
23211d1e34ddSDavid S. Miller 	if (err)
23221d1e34ddSDavid S. Miller 		goto out_cancel;
2323d8647b79SSteffen Klassert 
23241d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
23251d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
23261d1e34ddSDavid S. Miller 		if (err)
23271d1e34ddSDavid S. Miller 			goto out_cancel;
23281d1e34ddSDavid S. Miller 	}
23291d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
23301d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
23311d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
23321d1e34ddSDavid S. Miller 		if (err)
23331d1e34ddSDavid S. Miller 			goto out_cancel;
23341d1e34ddSDavid S. Miller 	}
23351d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
23361d1e34ddSDavid S. Miller 	if (err)
23371d1e34ddSDavid S. Miller 		goto out_cancel;
23386f26b61eSJamal Hadi Salim 
23397e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
23407e652640SSteffen Klassert 	if (err)
23417e652640SSteffen Klassert 		goto out_cancel;
23427e652640SSteffen Klassert 
2343053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2344053c095aSJohannes Berg 	return 0;
2345d51d081dSJamal Hadi Salim 
23461d1e34ddSDavid S. Miller out_cancel:
23479825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
23481d1e34ddSDavid S. Miller 	return err;
2349d51d081dSJamal Hadi Salim }
2350d51d081dSJamal Hadi Salim 
235122e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
23523bec6c3eSSabrina Dubroca 		       struct nlattr **attrs, struct netlink_ext_ack *extack)
2353d51d081dSJamal Hadi Salim {
2354fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2355d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2356d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
2357d51d081dSJamal Hadi Salim 	int err;
2358d51d081dSJamal Hadi Salim 	struct km_event c;
23596f26b61eSJamal Hadi Salim 	u32 mark;
23606f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
23617b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
2362d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
2363d51d081dSJamal Hadi Salim 
23646f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
23656f26b61eSJamal Hadi Salim 
23666f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2367d8647b79SSteffen Klassert 	if (x == NULL)
2368d51d081dSJamal Hadi Salim 		return -ESRCH;
2369d8647b79SSteffen Klassert 
2370d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2371d8647b79SSteffen Klassert 	if (r_skb == NULL) {
2372d8647b79SSteffen Klassert 		xfrm_state_put(x);
2373d8647b79SSteffen Klassert 		return -ENOMEM;
2374d51d081dSJamal Hadi Salim 	}
2375d51d081dSJamal Hadi Salim 
2376d51d081dSJamal Hadi Salim 	/*
2377d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
2378d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
2379d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
2380d51d081dSJamal Hadi Salim 	*/
2381d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2382d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
2383d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
238415e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2385d51d081dSJamal Hadi Salim 
23862fc5f83bSGustavo A. R. Silva 	err = build_aevent(r_skb, x, &c);
23872fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
23882fc5f83bSGustavo A. R. Silva 
238915e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2390d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2391d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2392d51d081dSJamal Hadi Salim 	return err;
2393d51d081dSJamal Hadi Salim }
2394d51d081dSJamal Hadi Salim 
239522e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
23963bec6c3eSSabrina Dubroca 		       struct nlattr **attrs, struct netlink_ext_ack *extack)
2397d51d081dSJamal Hadi Salim {
2398fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2399d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2400d51d081dSJamal Hadi Salim 	struct km_event c;
2401d51d081dSJamal Hadi Salim 	int err = -EINVAL;
24026f26b61eSJamal Hadi Salim 	u32 mark = 0;
24036f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
24047b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
24055424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2406d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
24075424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
24084e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
24094e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2410d51d081dSJamal Hadi Salim 
24114e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
2412d51d081dSJamal Hadi Salim 		return err;
2413d51d081dSJamal Hadi Salim 
2414d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
2415d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2416d51d081dSJamal Hadi Salim 		return err;
2417d51d081dSJamal Hadi Salim 
24186f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
24196f26b61eSJamal Hadi Salim 
24206f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2421d51d081dSJamal Hadi Salim 	if (x == NULL)
2422d51d081dSJamal Hadi Salim 		return -ESRCH;
2423d51d081dSJamal Hadi Salim 
2424d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
2425d51d081dSJamal Hadi Salim 		goto out;
2426d51d081dSJamal Hadi Salim 
24274479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
2428e2b19125SSteffen Klassert 	if (err)
2429e2b19125SSteffen Klassert 		goto out;
2430e2b19125SSteffen Klassert 
2431d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2432e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
2433d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2434d51d081dSJamal Hadi Salim 
2435d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
2436d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
243715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2438d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
2439d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
2440d51d081dSJamal Hadi Salim 	err = 0;
2441d51d081dSJamal Hadi Salim out:
2442d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2443d51d081dSJamal Hadi Salim 	return err;
2444d51d081dSJamal Hadi Salim }
2445d51d081dSJamal Hadi Salim 
244622e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
24473bec6c3eSSabrina Dubroca 			     struct nlattr **attrs,
24483bec6c3eSSabrina Dubroca 			     struct netlink_ext_ack *extack)
24491da177e4SLinus Torvalds {
2450fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
245126b15dadSJamal Hadi Salim 	struct km_event c;
2452b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
2453f7b6983fSMasahide NAKAMURA 	int err;
245426b15dadSJamal Hadi Salim 
2455fb7deabaSSabrina Dubroca 	err = copy_from_user_policy_type(&type, attrs, extack);
2456f7b6983fSMasahide NAKAMURA 	if (err)
2457f7b6983fSMasahide NAKAMURA 		return err;
2458f7b6983fSMasahide NAKAMURA 
24592e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
24602f1eb65fSJamal Hadi Salim 	if (err) {
24612f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
24622f1eb65fSJamal Hadi Salim 			return 0;
2463069c474eSDavid S. Miller 		return err;
24642f1eb65fSJamal Hadi Salim 	}
24652f1eb65fSJamal Hadi Salim 
2466f7b6983fSMasahide NAKAMURA 	c.data.type = type;
2467f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
246826b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
246915e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
24707067802eSAlexey Dobriyan 	c.net = net;
247126b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
24721da177e4SLinus Torvalds 	return 0;
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds 
247522e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
24763bec6c3eSSabrina Dubroca 			       struct nlattr **attrs,
24773bec6c3eSSabrina Dubroca 			       struct netlink_ext_ack *extack)
24786c5c8ca7SJamal Hadi Salim {
2479fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
24806c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
24817b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
24826c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2483b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
24846c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2485295fae56SJamal Hadi Salim 	struct xfrm_mark m;
24867e652640SSteffen Klassert 	u32 if_id = 0;
24876c5c8ca7SJamal Hadi Salim 
2488fb7deabaSSabrina Dubroca 	err = copy_from_user_policy_type(&type, attrs, extack);
2489f7b6983fSMasahide NAKAMURA 	if (err)
2490f7b6983fSMasahide NAKAMURA 		return err;
2491f7b6983fSMasahide NAKAMURA 
249224fc544fSSabrina Dubroca 	err = verify_policy_dir(p->dir, extack);
2493c8bf4d04STimo Teräs 	if (err)
2494c8bf4d04STimo Teräs 		return err;
2495c8bf4d04STimo Teräs 
24967e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
24977e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
24987e652640SSteffen Klassert 
24994f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
25004f47e8abSXin Long 
25016c5c8ca7SJamal Hadi Salim 	if (p->index)
25024f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
25034f47e8abSXin Long 				      0, &err);
25046c5c8ca7SJamal Hadi Salim 	else {
25055424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
250603e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
25076c5c8ca7SJamal Hadi Salim 
250808a717e4SSabrina Dubroca 		err = verify_sec_ctx_len(attrs, extack);
25096c5c8ca7SJamal Hadi Salim 		if (err)
25106c5c8ca7SJamal Hadi Salim 			return err;
25116c5c8ca7SJamal Hadi Salim 
25122c8dd116SDenis V. Lunev 		ctx = NULL;
25136c5c8ca7SJamal Hadi Salim 		if (rt) {
25145424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
25156c5c8ca7SJamal Hadi Salim 
251652a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
251703e1ad7bSPaul Moore 			if (err)
25186c5c8ca7SJamal Hadi Salim 				return err;
25192c8dd116SDenis V. Lunev 		}
25204f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
25216f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
252203e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
25236c5c8ca7SJamal Hadi Salim 	}
25246c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2525ef41aaa0SEric Paris 		return -ENOENT;
252603e1ad7bSPaul Moore 
2527ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
25286c5c8ca7SJamal Hadi Salim 		goto out;
25296c5c8ca7SJamal Hadi Salim 
25306c5c8ca7SJamal Hadi Salim 	err = 0;
25316c5c8ca7SJamal Hadi Salim 	if (up->hard) {
25326c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
25332e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
25346c5c8ca7SJamal Hadi Salim 	}
2535c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
25366c5c8ca7SJamal Hadi Salim 
25376c5c8ca7SJamal Hadi Salim out:
25386c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
25396c5c8ca7SJamal Hadi Salim 	return err;
25406c5c8ca7SJamal Hadi Salim }
25416c5c8ca7SJamal Hadi Salim 
254222e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
25433bec6c3eSSabrina Dubroca 			      struct nlattr **attrs,
25443bec6c3eSSabrina Dubroca 			      struct netlink_ext_ack *extack)
254553bc6b4dSJamal Hadi Salim {
2546fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
254753bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
254853bc6b4dSJamal Hadi Salim 	int err;
25497b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
255053bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
25516f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2552928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
255353bc6b4dSJamal Hadi Salim 
25546f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
255553bc6b4dSJamal Hadi Salim 
25563a765aa5SDavid S. Miller 	err = -ENOENT;
255753bc6b4dSJamal Hadi Salim 	if (x == NULL)
255853bc6b4dSJamal Hadi Salim 		return err;
255953bc6b4dSJamal Hadi Salim 
256053bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
25613a765aa5SDavid S. Miller 	err = -EINVAL;
256253bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
256353bc6b4dSJamal Hadi Salim 		goto out;
2564c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
256553bc6b4dSJamal Hadi Salim 
2566161a09e7SJoy Latten 	if (ue->hard) {
256753bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
25682e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2569161a09e7SJoy Latten 	}
25703a765aa5SDavid S. Miller 	err = 0;
257153bc6b4dSJamal Hadi Salim out:
257253bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
257353bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
257453bc6b4dSJamal Hadi Salim 	return err;
257553bc6b4dSJamal Hadi Salim }
257653bc6b4dSJamal Hadi Salim 
257722e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
25783bec6c3eSSabrina Dubroca 			    struct nlattr **attrs,
25793bec6c3eSSabrina Dubroca 			    struct netlink_ext_ack *extack)
2580980ebd25SJamal Hadi Salim {
2581fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2582980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2583980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2584980ebd25SJamal Hadi Salim 	int i;
25855424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
25866f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2587980ebd25SJamal Hadi Salim 
25887b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2589fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2590980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2591980ebd25SJamal Hadi Salim 
2592980ebd25SJamal Hadi Salim 	if (!x)
2593d8eb9307SIlpo Järvinen 		goto nomem;
2594980ebd25SJamal Hadi Salim 
25956f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
25966f26b61eSJamal Hadi Salim 
2597ec2b4f01SSabrina Dubroca 	err = verify_newpolicy_info(&ua->policy, extack);
2598d8eb9307SIlpo Järvinen 	if (err)
259973efc324SVegard Nossum 		goto free_state;
260008a717e4SSabrina Dubroca 	err = verify_sec_ctx_len(attrs, extack);
2601a1a7e3a3SXin Long 	if (err)
2602a1a7e3a3SXin Long 		goto free_state;
2603980ebd25SJamal Hadi Salim 
2604980ebd25SJamal Hadi Salim 	/*   build an XP */
2605fb7deabaSSabrina Dubroca 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err, extack);
2606d8eb9307SIlpo Järvinen 	if (!xp)
2607d8eb9307SIlpo Järvinen 		goto free_state;
2608980ebd25SJamal Hadi Salim 
2609980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2610980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2611980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
26126f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
26136f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
26145424f32eSThomas Graf 	ut = nla_data(rt);
2615980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2616980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2617980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2618980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2619980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2620980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2621980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2622980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2623980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2624980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2625980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2626980ebd25SJamal Hadi Salim 
2627980ebd25SJamal Hadi Salim 	}
2628980ebd25SJamal Hadi Salim 
26294a135e53SMathias Krause 	xfrm_state_free(x);
2630980ebd25SJamal Hadi Salim 	kfree(xp);
2631980ebd25SJamal Hadi Salim 
2632980ebd25SJamal Hadi Salim 	return 0;
2633d8eb9307SIlpo Järvinen 
2634d8eb9307SIlpo Järvinen free_state:
26354a135e53SMathias Krause 	xfrm_state_free(x);
2636d8eb9307SIlpo Järvinen nomem:
2637d8eb9307SIlpo Järvinen 	return err;
2638980ebd25SJamal Hadi Salim }
2639980ebd25SJamal Hadi Salim 
26405c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
26415c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
264213c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
26435424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
26445c79de6eSShinta Sugimoto {
26455424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
26465c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
26475c79de6eSShinta Sugimoto 	int i, num_migrate;
26485c79de6eSShinta Sugimoto 
264913c1d189SArnaud Ebalard 	if (k != NULL) {
265013c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
265113c1d189SArnaud Ebalard 
265213c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
265313c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
265413c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
265513c1d189SArnaud Ebalard 		k->family = uk->family;
265613c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
265713c1d189SArnaud Ebalard 	}
265813c1d189SArnaud Ebalard 
26595424f32eSThomas Graf 	um = nla_data(rt);
26605424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
26615c79de6eSShinta Sugimoto 
26625c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
26635c79de6eSShinta Sugimoto 		return -EINVAL;
26645c79de6eSShinta Sugimoto 
26655c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
26665c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
26675c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
26685c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
26695c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
26705c79de6eSShinta Sugimoto 
26715c79de6eSShinta Sugimoto 		ma->proto = um->proto;
26725c79de6eSShinta Sugimoto 		ma->mode = um->mode;
26735c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
26745c79de6eSShinta Sugimoto 
26755c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
26765c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
26775c79de6eSShinta Sugimoto 	}
26785c79de6eSShinta Sugimoto 
26795c79de6eSShinta Sugimoto 	*num = i;
26805c79de6eSShinta Sugimoto 	return 0;
26815c79de6eSShinta Sugimoto }
26825c79de6eSShinta Sugimoto 
26835c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
26843bec6c3eSSabrina Dubroca 			   struct nlattr **attrs, struct netlink_ext_ack *extack)
26855c79de6eSShinta Sugimoto {
26867b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
26875c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
268813c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
26895c79de6eSShinta Sugimoto 	u8 type;
26905c79de6eSShinta Sugimoto 	int err;
26915c79de6eSShinta Sugimoto 	int n = 0;
26928d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
26934ab47d47SAntony Antony 	struct xfrm_encap_tmpl  *encap = NULL;
2694c1aca308SYan Yan 	u32 if_id = 0;
26955c79de6eSShinta Sugimoto 
269635a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2697cf5cb79fSThomas Graf 		return -EINVAL;
26985c79de6eSShinta Sugimoto 
269913c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
270013c1d189SArnaud Ebalard 
2701fb7deabaSSabrina Dubroca 	err = copy_from_user_policy_type(&type, attrs, extack);
27025c79de6eSShinta Sugimoto 	if (err)
27035c79de6eSShinta Sugimoto 		return err;
27045c79de6eSShinta Sugimoto 
270513c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
27065c79de6eSShinta Sugimoto 	if (err)
27075c79de6eSShinta Sugimoto 		return err;
27085c79de6eSShinta Sugimoto 
27095c79de6eSShinta Sugimoto 	if (!n)
27105c79de6eSShinta Sugimoto 		return 0;
27115c79de6eSShinta Sugimoto 
27124ab47d47SAntony Antony 	if (attrs[XFRMA_ENCAP]) {
27134ab47d47SAntony Antony 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
27144ab47d47SAntony Antony 				sizeof(*encap), GFP_KERNEL);
27154ab47d47SAntony Antony 		if (!encap)
27164ac7a6eeSZheng Yongjun 			return -ENOMEM;
27175c79de6eSShinta Sugimoto 	}
27184ab47d47SAntony Antony 
2719c1aca308SYan Yan 	if (attrs[XFRMA_IF_ID])
2720c1aca308SYan Yan 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2721c1aca308SYan Yan 
2722c1aca308SYan Yan 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, if_id);
27234ab47d47SAntony Antony 
27244ab47d47SAntony Antony 	kfree(encap);
27254ab47d47SAntony Antony 
27264ab47d47SAntony Antony 	return err;
27274ab47d47SAntony Antony }
27285c79de6eSShinta Sugimoto #else
27295c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
27303bec6c3eSSabrina Dubroca 			   struct nlattr **attrs, struct netlink_ext_ack *extack)
27315c79de6eSShinta Sugimoto {
27325c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
27335c79de6eSShinta Sugimoto }
27345c79de6eSShinta Sugimoto #endif
27355c79de6eSShinta Sugimoto 
27365c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2737183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
27385c79de6eSShinta Sugimoto {
27395c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
27405c79de6eSShinta Sugimoto 
27415c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
27425c79de6eSShinta Sugimoto 	um.proto = m->proto;
27435c79de6eSShinta Sugimoto 	um.mode = m->mode;
27445c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
27455c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
27465c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
27475c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
27485c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
27495c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
27505c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
27515c79de6eSShinta Sugimoto 
2752c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
27535c79de6eSShinta Sugimoto }
27545c79de6eSShinta Sugimoto 
2755183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
275613c1d189SArnaud Ebalard {
275713c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
275813c1d189SArnaud Ebalard 
275913c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
276013c1d189SArnaud Ebalard 	uk.family = k->family;
276113c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
276213c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2763a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
276413c1d189SArnaud Ebalard 
276513c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
276613c1d189SArnaud Ebalard }
276713c1d189SArnaud Ebalard 
2768a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
27698bafd730SAntony Antony 						int with_encp)
27707deb2264SThomas Graf {
27717deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
277213c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
27738bafd730SAntony Antony 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
27747deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
27757deb2264SThomas Graf 	      + userpolicy_type_attrsize();
27767deb2264SThomas Graf }
27777deb2264SThomas Graf 
2778183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2779183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
27808bafd730SAntony Antony 			 const struct xfrm_selector *sel,
27818bafd730SAntony Antony 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
27825c79de6eSShinta Sugimoto {
2783183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
27845c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
27855c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
27861d1e34ddSDavid S. Miller 	int i, err;
27875c79de6eSShinta Sugimoto 
278879b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
278979b8b7f4SThomas Graf 	if (nlh == NULL)
279079b8b7f4SThomas Graf 		return -EMSGSIZE;
27915c79de6eSShinta Sugimoto 
27927b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
27935c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
27945c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
27955c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
27965c79de6eSShinta Sugimoto 	pol_id->dir = dir;
27975c79de6eSShinta Sugimoto 
27981d1e34ddSDavid S. Miller 	if (k != NULL) {
27991d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
28001d1e34ddSDavid S. Miller 		if (err)
28011d1e34ddSDavid S. Miller 			goto out_cancel;
28021d1e34ddSDavid S. Miller 	}
28038bafd730SAntony Antony 	if (encap) {
28048bafd730SAntony Antony 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
28058bafd730SAntony Antony 		if (err)
28068bafd730SAntony Antony 			goto out_cancel;
28078bafd730SAntony Antony 	}
28081d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
28091d1e34ddSDavid S. Miller 	if (err)
28101d1e34ddSDavid S. Miller 		goto out_cancel;
28115c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
28121d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
28131d1e34ddSDavid S. Miller 		if (err)
28141d1e34ddSDavid S. Miller 			goto out_cancel;
28155c79de6eSShinta Sugimoto 	}
28165c79de6eSShinta Sugimoto 
2817053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2818053c095aSJohannes Berg 	return 0;
28191d1e34ddSDavid S. Miller 
28201d1e34ddSDavid S. Miller out_cancel:
28219825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
28221d1e34ddSDavid S. Miller 	return err;
28235c79de6eSShinta Sugimoto }
28245c79de6eSShinta Sugimoto 
2825183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2826183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
28278bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
28288bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
28295c79de6eSShinta Sugimoto {
2830a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
28315c79de6eSShinta Sugimoto 	struct sk_buff *skb;
28322fc5f83bSGustavo A. R. Silva 	int err;
28335c79de6eSShinta Sugimoto 
28348bafd730SAntony Antony 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
28358bafd730SAntony Antony 			GFP_ATOMIC);
28365c79de6eSShinta Sugimoto 	if (skb == NULL)
28375c79de6eSShinta Sugimoto 		return -ENOMEM;
28385c79de6eSShinta Sugimoto 
28395c79de6eSShinta Sugimoto 	/* build migrate */
28402fc5f83bSGustavo A. R. Silva 	err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
28412fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
28425c79de6eSShinta Sugimoto 
284321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
28445c79de6eSShinta Sugimoto }
28455c79de6eSShinta Sugimoto #else
2846183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2847183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
28488bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
28498bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
28505c79de6eSShinta Sugimoto {
28515c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
28525c79de6eSShinta Sugimoto }
28535c79de6eSShinta Sugimoto #endif
2854d51d081dSJamal Hadi Salim 
2855a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2856492b558bSThomas Graf 
28575461fc0cSDmitry Safonov const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
285866f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2859492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2860492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2861492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2862492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2863492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2864492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2865980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
286653bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2867492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
286866f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
28696c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2870492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2871a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2872d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2873d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
287497a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
28755c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2876a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2877880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2878a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
28792d151d39SSteffen Klassert 	[XFRM_MSG_SETDEFAULT  - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
28802d151d39SSteffen Klassert 	[XFRM_MSG_GETDEFAULT  - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
28811da177e4SLinus Torvalds };
28825461fc0cSDmitry Safonov EXPORT_SYMBOL_GPL(xfrm_msg_min);
28831da177e4SLinus Torvalds 
2884492b558bSThomas Graf #undef XMSGSIZE
2885492b558bSThomas Graf 
28865106f4a8SDmitry Safonov const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2887c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2888c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2889c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2890c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
28911a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2892cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2893cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2894cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2895cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2896cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2897cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2898cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2899cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2900cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2901cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2902cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2903cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2904cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2905cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
290613c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
29076f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
290835d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2909d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2910a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2911d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2912870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2913d77e38e6SSteffen Klassert 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
29149b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK]	= { .type = NLA_U32 },
29159b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK_MASK]	= { .type = NLA_U32 },
29167e652640SSteffen Klassert 	[XFRMA_IF_ID]		= { .type = NLA_U32 },
2917cf5cb79fSThomas Graf };
29185106f4a8SDmitry Safonov EXPORT_SYMBOL_GPL(xfrma_policy);
2919cf5cb79fSThomas Graf 
2920880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2921880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2922880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2923880a6fabSChristophe Gouault };
2924880a6fabSChristophe Gouault 
292505600a79SMathias Krause static const struct xfrm_link {
29263bec6c3eSSabrina Dubroca 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **,
29273bec6c3eSSabrina Dubroca 		    struct netlink_ext_ack *);
29281137b5e2SHerbert Xu 	int (*start)(struct netlink_callback *);
29291da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
29304c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2931880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2932880a6fabSChristophe Gouault 	int nla_max;
2933492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2934492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2935492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2936492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
29374c563f76STimo Teras 						   .dump = xfrm_dump_sa,
29384c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2939492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2940492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2941492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
29421137b5e2SHerbert Xu 						   .start = xfrm_dump_policy_start,
29434c563f76STimo Teras 						   .dump = xfrm_dump_policy,
29444c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2945492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2946980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
294753bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2948492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2949492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
29506c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2951492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2952492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2953d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2954d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
29555c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
295628d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2957880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2958880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2959880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2960ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
29612d151d39SSteffen Klassert 	[XFRM_MSG_SETDEFAULT  - XFRM_MSG_BASE] = { .doit = xfrm_set_default   },
29622d151d39SSteffen Klassert 	[XFRM_MSG_GETDEFAULT  - XFRM_MSG_BASE] = { .doit = xfrm_get_default   },
29631da177e4SLinus Torvalds };
29641da177e4SLinus Torvalds 
29652d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
29662d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
29671da177e4SLinus Torvalds {
2968a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
296935a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
297005600a79SMathias Krause 	const struct xfrm_link *link;
29715106f4a8SDmitry Safonov 	struct nlmsghdr *nlh64 = NULL;
2972a7bd9a45SThomas Graf 	int type, err;
29731da177e4SLinus Torvalds 
29741da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
29751da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
29761d00a4ebSThomas Graf 		return -EINVAL;
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
29791da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
29801da177e4SLinus Torvalds 
29811da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
298290f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
29831d00a4ebSThomas Graf 		return -EPERM;
29841da177e4SLinus Torvalds 
29855106f4a8SDmitry Safonov 	if (in_compat_syscall()) {
29865106f4a8SDmitry Safonov 		struct xfrm_translator *xtr = xfrm_get_translator();
29875106f4a8SDmitry Safonov 
29885106f4a8SDmitry Safonov 		if (!xtr)
29895106f4a8SDmitry Safonov 			return -EOPNOTSUPP;
29905106f4a8SDmitry Safonov 
29915106f4a8SDmitry Safonov 		nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
29925106f4a8SDmitry Safonov 					    link->nla_pol, extack);
29935106f4a8SDmitry Safonov 		xfrm_put_translator(xtr);
29945106f4a8SDmitry Safonov 		if (IS_ERR(nlh64))
29955106f4a8SDmitry Safonov 			return PTR_ERR(nlh64);
29965106f4a8SDmitry Safonov 		if (nlh64)
29975106f4a8SDmitry Safonov 			nlh = nlh64;
29985106f4a8SDmitry Safonov 	}
29995106f4a8SDmitry Safonov 
3000492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
3001492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
3002b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
300380d326faSPablo Neira Ayuso 		struct netlink_dump_control c = {
30041137b5e2SHerbert Xu 			.start = link->start,
300580d326faSPablo Neira Ayuso 			.dump = link->dump,
300680d326faSPablo Neira Ayuso 			.done = link->done,
300780d326faSPablo Neira Ayuso 		};
30085106f4a8SDmitry Safonov 
30095106f4a8SDmitry Safonov 		if (link->dump == NULL) {
30105106f4a8SDmitry Safonov 			err = -EINVAL;
30115106f4a8SDmitry Safonov 			goto err;
301280d326faSPablo Neira Ayuso 		}
30135106f4a8SDmitry Safonov 
30145106f4a8SDmitry Safonov 		err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
30155106f4a8SDmitry Safonov 		goto err;
30161da177e4SLinus Torvalds 	}
30171da177e4SLinus Torvalds 
30188cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
3019880a6fabSChristophe Gouault 				     link->nla_max ? : XFRMA_MAX,
3020fe52145fSJohannes Berg 				     link->nla_pol ? : xfrma_policy, extack);
3021a7bd9a45SThomas Graf 	if (err < 0)
30225106f4a8SDmitry Safonov 		goto err;
30235106f4a8SDmitry Safonov 
30245106f4a8SDmitry Safonov 	if (link->doit == NULL) {
30255106f4a8SDmitry Safonov 		err = -EINVAL;
30265106f4a8SDmitry Safonov 		goto err;
30275106f4a8SDmitry Safonov 	}
30285106f4a8SDmitry Safonov 
30293bec6c3eSSabrina Dubroca 	err = link->doit(skb, nlh, attrs, extack);
30305106f4a8SDmitry Safonov 
30317c1a80e8SPavel Skripkin 	/* We need to free skb allocated in xfrm_alloc_compat() before
30327c1a80e8SPavel Skripkin 	 * returning from this function, because consume_skb() won't take
30337c1a80e8SPavel Skripkin 	 * care of frag_list since netlink destructor sets
30347c1a80e8SPavel Skripkin 	 * sbk->head to NULL. (see netlink_skb_destructor())
30357c1a80e8SPavel Skripkin 	 */
30367c1a80e8SPavel Skripkin 	if (skb_has_frag_list(skb)) {
30377c1a80e8SPavel Skripkin 		kfree_skb(skb_shinfo(skb)->frag_list);
30387c1a80e8SPavel Skripkin 		skb_shinfo(skb)->frag_list = NULL;
30397c1a80e8SPavel Skripkin 	}
30407c1a80e8SPavel Skripkin 
30415106f4a8SDmitry Safonov err:
30425106f4a8SDmitry Safonov 	kvfree(nlh64);
3043a7bd9a45SThomas Graf 	return err;
30441da177e4SLinus Torvalds }
30451da177e4SLinus Torvalds 
3046cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
30471da177e4SLinus Torvalds {
3048283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
3049283bc9f3SFan Du 
3050283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
3051cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
3052283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
30531da177e4SLinus Torvalds }
30541da177e4SLinus Torvalds 
3055a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void)
30567deb2264SThomas Graf {
30576f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
30586f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
30597deb2264SThomas Graf }
30607deb2264SThomas Graf 
3061214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
30621da177e4SLinus Torvalds {
30631da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
30641da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
30651d1e34ddSDavid S. Miller 	int err;
30661da177e4SLinus Torvalds 
306715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
306879b8b7f4SThomas Graf 	if (nlh == NULL)
306979b8b7f4SThomas Graf 		return -EMSGSIZE;
30701da177e4SLinus Torvalds 
30717b67c857SThomas Graf 	ue = nlmsg_data(nlh);
30721da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
3073d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
3074e3e5fc16SMathias Krause 	/* clear the padding bytes */
3075caf283d0SKees Cook 	memset_after(ue, 0, hard);
30761da177e4SLinus Torvalds 
30771d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
30781d1e34ddSDavid S. Miller 	if (err)
30791d1e34ddSDavid S. Miller 		return err;
30806f26b61eSJamal Hadi Salim 
30817e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
30827e652640SSteffen Klassert 	if (err)
30837e652640SSteffen Klassert 		return err;
30847e652640SSteffen Klassert 
3085053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3086053c095aSJohannes Berg 	return 0;
30871da177e4SLinus Torvalds }
30881da177e4SLinus Torvalds 
3089214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
30901da177e4SLinus Torvalds {
3091fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
30921da177e4SLinus Torvalds 	struct sk_buff *skb;
30931da177e4SLinus Torvalds 
30947deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
30951da177e4SLinus Torvalds 	if (skb == NULL)
30961da177e4SLinus Torvalds 		return -ENOMEM;
30971da177e4SLinus Torvalds 
30986f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
30996f26b61eSJamal Hadi Salim 		kfree_skb(skb);
31006f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
31016f26b61eSJamal Hadi Salim 	}
31021da177e4SLinus Torvalds 
310321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
31041da177e4SLinus Torvalds }
31051da177e4SLinus Torvalds 
3106214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
3107d51d081dSJamal Hadi Salim {
3108fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
3109d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
31102fc5f83bSGustavo A. R. Silva 	int err;
3111d51d081dSJamal Hadi Salim 
3112d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
3113d51d081dSJamal Hadi Salim 	if (skb == NULL)
3114d51d081dSJamal Hadi Salim 		return -ENOMEM;
3115d51d081dSJamal Hadi Salim 
31162fc5f83bSGustavo A. R. Silva 	err = build_aevent(skb, x, c);
31172fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
3118d51d081dSJamal Hadi Salim 
311921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
3120d51d081dSJamal Hadi Salim }
3121d51d081dSJamal Hadi Salim 
3122214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
312326b15dadSJamal Hadi Salim {
31247067802eSAlexey Dobriyan 	struct net *net = c->net;
312526b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
312626b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
312726b15dadSJamal Hadi Salim 	struct sk_buff *skb;
31287deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
312926b15dadSJamal Hadi Salim 
31307deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
313126b15dadSJamal Hadi Salim 	if (skb == NULL)
313226b15dadSJamal Hadi Salim 		return -ENOMEM;
313326b15dadSJamal Hadi Salim 
313415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
313579b8b7f4SThomas Graf 	if (nlh == NULL) {
313679b8b7f4SThomas Graf 		kfree_skb(skb);
313779b8b7f4SThomas Graf 		return -EMSGSIZE;
313879b8b7f4SThomas Graf 	}
313926b15dadSJamal Hadi Salim 
31407b67c857SThomas Graf 	p = nlmsg_data(nlh);
3141bf08867fSHerbert Xu 	p->proto = c->data.proto;
314226b15dadSJamal Hadi Salim 
31439825069dSThomas Graf 	nlmsg_end(skb, nlh);
314426b15dadSJamal Hadi Salim 
314521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
314626b15dadSJamal Hadi Salim }
314726b15dadSJamal Hadi Salim 
3148a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
314926b15dadSJamal Hadi Salim {
3150a1b831f2SAlexey Dobriyan 	unsigned int l = 0;
31511a6509d9SHerbert Xu 	if (x->aead)
31521a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
31534447bb33SMartin Willi 	if (x->aalg) {
31544447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
31554447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
31564447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
31574447bb33SMartin Willi 	}
315826b15dadSJamal Hadi Salim 	if (x->ealg)
31590f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
316026b15dadSJamal Hadi Salim 	if (x->calg)
31617deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
316226b15dadSJamal Hadi Salim 	if (x->encap)
31637deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
316435d2856bSMartin Willi 	if (x->tfcpad)
316535d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
3166d8647b79SSteffen Klassert 	if (x->replay_esn)
3167d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
3168f293a5e3Sdingzhi 	else
3169f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
317068325d3bSHerbert Xu 	if (x->security)
317168325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
317268325d3bSHerbert Xu 				    x->security->ctx_len);
317368325d3bSHerbert Xu 	if (x->coaddr)
317468325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
3175a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
3176a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
3177d77e38e6SSteffen Klassert 	if (x->xso.dev)
31787770a39dSEric Dumazet 		 l += nla_total_size(sizeof(struct xfrm_user_offload));
31799b42c1f1SSteffen Klassert 	if (x->props.smark.v | x->props.smark.m) {
31809b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.v));
31819b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.m));
31829b42c1f1SSteffen Klassert 	}
31837e652640SSteffen Klassert 	if (x->if_id)
31847e652640SSteffen Klassert 		l += nla_total_size(sizeof(x->if_id));
318568325d3bSHerbert Xu 
3186d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
3187de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
318826b15dadSJamal Hadi Salim 
31894e484b3eSAntony Antony 	if (x->mapping_maxage)
31904e484b3eSAntony Antony 		l += nla_total_size(sizeof(x->mapping_maxage));
31914e484b3eSAntony Antony 
319226b15dadSJamal Hadi Salim 	return l;
319326b15dadSJamal Hadi Salim }
319426b15dadSJamal Hadi Salim 
3195214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
319626b15dadSJamal Hadi Salim {
3197fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
319826b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
31990603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
320026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
320126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3202a1b831f2SAlexey Dobriyan 	unsigned int len = xfrm_sa_len(x);
3203a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3204a1b831f2SAlexey Dobriyan 	int err;
32050603eac0SHerbert Xu 
32060603eac0SHerbert Xu 	headlen = sizeof(*p);
32070603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
32087deb2264SThomas Graf 		len += nla_total_size(headlen);
32090603eac0SHerbert Xu 		headlen = sizeof(*id);
32106f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
32110603eac0SHerbert Xu 	}
32127deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
321326b15dadSJamal Hadi Salim 
32147deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
321526b15dadSJamal Hadi Salim 	if (skb == NULL)
321626b15dadSJamal Hadi Salim 		return -ENOMEM;
321726b15dadSJamal Hadi Salim 
321815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
32191d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
322079b8b7f4SThomas Graf 	if (nlh == NULL)
32211d1e34ddSDavid S. Miller 		goto out_free_skb;
322226b15dadSJamal Hadi Salim 
32237b67c857SThomas Graf 	p = nlmsg_data(nlh);
32240603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
3225c0144beaSThomas Graf 		struct nlattr *attr;
3226c0144beaSThomas Graf 
32277b67c857SThomas Graf 		id = nlmsg_data(nlh);
322850329c8aSMathias Krause 		memset(id, 0, sizeof(*id));
32290603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
32300603eac0SHerbert Xu 		id->spi = x->id.spi;
32310603eac0SHerbert Xu 		id->family = x->props.family;
32320603eac0SHerbert Xu 		id->proto = x->id.proto;
32330603eac0SHerbert Xu 
3234c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
32351d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3236c0144beaSThomas Graf 		if (attr == NULL)
32371d1e34ddSDavid S. Miller 			goto out_free_skb;
3238c0144beaSThomas Graf 
3239c0144beaSThomas Graf 		p = nla_data(attr);
32400603eac0SHerbert Xu 	}
32411d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
32421d1e34ddSDavid S. Miller 	if (err)
32431d1e34ddSDavid S. Miller 		goto out_free_skb;
324426b15dadSJamal Hadi Salim 
32459825069dSThomas Graf 	nlmsg_end(skb, nlh);
324626b15dadSJamal Hadi Salim 
324721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
324826b15dadSJamal Hadi Salim 
32491d1e34ddSDavid S. Miller out_free_skb:
325026b15dadSJamal Hadi Salim 	kfree_skb(skb);
32511d1e34ddSDavid S. Miller 	return err;
325226b15dadSJamal Hadi Salim }
325326b15dadSJamal Hadi Salim 
3254214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
325526b15dadSJamal Hadi Salim {
325626b15dadSJamal Hadi Salim 
325726b15dadSJamal Hadi Salim 	switch (c->event) {
3258f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
325926b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
3260d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
3261d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
3262f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
3263f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
3264f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
326526b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
3266f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
326726b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
326826b15dadSJamal Hadi Salim 	default:
326962db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
327062db5cfdSstephen hemminger 		       c->event);
327126b15dadSJamal Hadi Salim 		break;
327226b15dadSJamal Hadi Salim 	}
327326b15dadSJamal Hadi Salim 
327426b15dadSJamal Hadi Salim 	return 0;
327526b15dadSJamal Hadi Salim 
327626b15dadSJamal Hadi Salim }
327726b15dadSJamal Hadi Salim 
3278a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
32797deb2264SThomas Graf 						struct xfrm_policy *xp)
32807deb2264SThomas Graf {
32817deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
32827deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
32836f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
32847deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
32857deb2264SThomas Graf 	       + userpolicy_type_attrsize();
32867deb2264SThomas Graf }
32877deb2264SThomas Graf 
32881da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
328965e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
32901da177e4SLinus Torvalds {
32911d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
32921da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
32931da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
32941d1e34ddSDavid S. Miller 	int err;
32951da177e4SLinus Torvalds 
329679b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
329779b8b7f4SThomas Graf 	if (nlh == NULL)
329879b8b7f4SThomas Graf 		return -EMSGSIZE;
32991da177e4SLinus Torvalds 
33007b67c857SThomas Graf 	ua = nlmsg_data(nlh);
33011da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
33021da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
33031da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
330465e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
33051da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
33061da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
33071da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
33081da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
33091da177e4SLinus Torvalds 
33101d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
33111d1e34ddSDavid S. Miller 	if (!err)
33121d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
33131d1e34ddSDavid S. Miller 	if (!err)
33141d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
33151d1e34ddSDavid S. Miller 	if (!err)
33161d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
33177e652640SSteffen Klassert 	if (!err)
33187e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
33191d1e34ddSDavid S. Miller 	if (err) {
33201d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
33211d1e34ddSDavid S. Miller 		return err;
33221d1e34ddSDavid S. Miller 	}
33231da177e4SLinus Torvalds 
3324053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3325053c095aSJohannes Berg 	return 0;
33261da177e4SLinus Torvalds }
33271da177e4SLinus Torvalds 
33281da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
332965e0736bSFan Du 			     struct xfrm_policy *xp)
33301da177e4SLinus Torvalds {
3331a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
33321da177e4SLinus Torvalds 	struct sk_buff *skb;
33332fc5f83bSGustavo A. R. Silva 	int err;
33341da177e4SLinus Torvalds 
33357deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
33361da177e4SLinus Torvalds 	if (skb == NULL)
33371da177e4SLinus Torvalds 		return -ENOMEM;
33381da177e4SLinus Torvalds 
33392fc5f83bSGustavo A. R. Silva 	err = build_acquire(skb, x, xt, xp);
33402fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
33411da177e4SLinus Torvalds 
334221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
33431da177e4SLinus Torvalds }
33441da177e4SLinus Torvalds 
33451da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
33461da177e4SLinus Torvalds  * or more templates.
33471da177e4SLinus Torvalds  */
3348cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
33491da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
33501da177e4SLinus Torvalds {
3351fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
33521da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
33531da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
33541da177e4SLinus Torvalds 	struct xfrm_policy *xp;
33551da177e4SLinus Torvalds 	int nr;
33561da177e4SLinus Torvalds 
3357cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
33581da177e4SLinus Torvalds 	case AF_INET:
33591da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
33601da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
33611da177e4SLinus Torvalds 			return NULL;
33621da177e4SLinus Torvalds 		}
33631da177e4SLinus Torvalds 		break;
3364dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
33651da177e4SLinus Torvalds 	case AF_INET6:
33661da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
33671da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
33681da177e4SLinus Torvalds 			return NULL;
33691da177e4SLinus Torvalds 		}
33701da177e4SLinus Torvalds 		break;
33711da177e4SLinus Torvalds #endif
33721da177e4SLinus Torvalds 	default:
33731da177e4SLinus Torvalds 		*dir = -EINVAL;
33741da177e4SLinus Torvalds 		return NULL;
33751da177e4SLinus Torvalds 	}
33761da177e4SLinus Torvalds 
33771da177e4SLinus Torvalds 	*dir = -EINVAL;
33781da177e4SLinus Torvalds 
33791da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
3380ec2b4f01SSabrina Dubroca 	    verify_newpolicy_info(p, NULL))
33811da177e4SLinus Torvalds 		return NULL;
33821da177e4SLinus Torvalds 
33831da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
3384d37bed89SSabrina Dubroca 	if (validate_tmpl(nr, ut, p->sel.family, NULL))
33851da177e4SLinus Torvalds 		return NULL;
33861da177e4SLinus Torvalds 
3387a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
3388a4f1bac6SHerbert Xu 		return NULL;
3389a4f1bac6SHerbert Xu 
33902f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
33911da177e4SLinus Torvalds 	if (xp == NULL) {
33921da177e4SLinus Torvalds 		*dir = -ENOBUFS;
33931da177e4SLinus Torvalds 		return NULL;
33941da177e4SLinus Torvalds 	}
33951da177e4SLinus Torvalds 
33961da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
3397f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
33981da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
33991da177e4SLinus Torvalds 
34001da177e4SLinus Torvalds 	*dir = p->dir;
34011da177e4SLinus Torvalds 
34021da177e4SLinus Torvalds 	return xp;
34031da177e4SLinus Torvalds }
34041da177e4SLinus Torvalds 
3405a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
34067deb2264SThomas Graf {
34077deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
34087deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
34097deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3410295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
34117deb2264SThomas Graf 	       + userpolicy_type_attrsize();
34127deb2264SThomas Graf }
34137deb2264SThomas Graf 
34141da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3415214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
34161da177e4SLinus Torvalds {
34171da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
3418d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
34191d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
34201d1e34ddSDavid S. Miller 	int err;
34211da177e4SLinus Torvalds 
342215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
342379b8b7f4SThomas Graf 	if (nlh == NULL)
342479b8b7f4SThomas Graf 		return -EMSGSIZE;
34251da177e4SLinus Torvalds 
34267b67c857SThomas Graf 	upe = nlmsg_data(nlh);
34271da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
34281d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
34291d1e34ddSDavid S. Miller 	if (!err)
34301d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
34311d1e34ddSDavid S. Miller 	if (!err)
34321d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
34331d1e34ddSDavid S. Miller 	if (!err)
34341d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
34357e652640SSteffen Klassert 	if (!err)
34367e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
34371d1e34ddSDavid S. Miller 	if (err) {
34381d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
34391d1e34ddSDavid S. Miller 		return err;
34401d1e34ddSDavid S. Miller 	}
34411da177e4SLinus Torvalds 	upe->hard = !!hard;
34421da177e4SLinus Torvalds 
3443053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3444053c095aSJohannes Berg 	return 0;
34451da177e4SLinus Torvalds }
34461da177e4SLinus Torvalds 
3447214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
34481da177e4SLinus Torvalds {
3449fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
34501da177e4SLinus Torvalds 	struct sk_buff *skb;
34512fc5f83bSGustavo A. R. Silva 	int err;
34521da177e4SLinus Torvalds 
34537deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
34541da177e4SLinus Torvalds 	if (skb == NULL)
34551da177e4SLinus Torvalds 		return -ENOMEM;
34561da177e4SLinus Torvalds 
34572fc5f83bSGustavo A. R. Silva 	err = build_polexpire(skb, xp, dir, c);
34582fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
34591da177e4SLinus Torvalds 
346021ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
34611da177e4SLinus Torvalds }
34621da177e4SLinus Torvalds 
3463214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
346426b15dadSJamal Hadi Salim {
3465a1b831f2SAlexey Dobriyan 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3466fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
346726b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
34680603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
346926b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
347026b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3471a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3472a1b831f2SAlexey Dobriyan 	int err;
34730603eac0SHerbert Xu 
34740603eac0SHerbert Xu 	headlen = sizeof(*p);
34750603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
34767deb2264SThomas Graf 		len += nla_total_size(headlen);
34770603eac0SHerbert Xu 		headlen = sizeof(*id);
34780603eac0SHerbert Xu 	}
3479cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
3480295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
34817deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
348226b15dadSJamal Hadi Salim 
34837deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
348426b15dadSJamal Hadi Salim 	if (skb == NULL)
348526b15dadSJamal Hadi Salim 		return -ENOMEM;
348626b15dadSJamal Hadi Salim 
348715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
34881d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
348979b8b7f4SThomas Graf 	if (nlh == NULL)
34901d1e34ddSDavid S. Miller 		goto out_free_skb;
349126b15dadSJamal Hadi Salim 
34927b67c857SThomas Graf 	p = nlmsg_data(nlh);
34930603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
3494c0144beaSThomas Graf 		struct nlattr *attr;
3495c0144beaSThomas Graf 
34967b67c857SThomas Graf 		id = nlmsg_data(nlh);
34970603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
34980603eac0SHerbert Xu 		id->dir = dir;
34990603eac0SHerbert Xu 		if (c->data.byid)
35000603eac0SHerbert Xu 			id->index = xp->index;
35010603eac0SHerbert Xu 		else
35020603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
35030603eac0SHerbert Xu 
3504c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
35051d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3506c0144beaSThomas Graf 		if (attr == NULL)
35071d1e34ddSDavid S. Miller 			goto out_free_skb;
3508c0144beaSThomas Graf 
3509c0144beaSThomas Graf 		p = nla_data(attr);
35100603eac0SHerbert Xu 	}
351126b15dadSJamal Hadi Salim 
351226b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
35131d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
35141d1e34ddSDavid S. Miller 	if (!err)
35151d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
35161d1e34ddSDavid S. Miller 	if (!err)
35171d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
35187e652640SSteffen Klassert 	if (!err)
35197e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
35201d1e34ddSDavid S. Miller 	if (err)
35211d1e34ddSDavid S. Miller 		goto out_free_skb;
3522295fae56SJamal Hadi Salim 
35239825069dSThomas Graf 	nlmsg_end(skb, nlh);
352426b15dadSJamal Hadi Salim 
352521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
352626b15dadSJamal Hadi Salim 
35271d1e34ddSDavid S. Miller out_free_skb:
352826b15dadSJamal Hadi Salim 	kfree_skb(skb);
35291d1e34ddSDavid S. Miller 	return err;
353026b15dadSJamal Hadi Salim }
353126b15dadSJamal Hadi Salim 
3532214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
353326b15dadSJamal Hadi Salim {
35347067802eSAlexey Dobriyan 	struct net *net = c->net;
353526b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
353626b15dadSJamal Hadi Salim 	struct sk_buff *skb;
35371d1e34ddSDavid S. Miller 	int err;
353826b15dadSJamal Hadi Salim 
35397deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
354026b15dadSJamal Hadi Salim 	if (skb == NULL)
354126b15dadSJamal Hadi Salim 		return -ENOMEM;
354226b15dadSJamal Hadi Salim 
354315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
35441d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
354579b8b7f4SThomas Graf 	if (nlh == NULL)
35461d1e34ddSDavid S. Miller 		goto out_free_skb;
35471d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
35481d1e34ddSDavid S. Miller 	if (err)
35491d1e34ddSDavid S. Miller 		goto out_free_skb;
355026b15dadSJamal Hadi Salim 
35519825069dSThomas Graf 	nlmsg_end(skb, nlh);
355226b15dadSJamal Hadi Salim 
355321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
355426b15dadSJamal Hadi Salim 
35551d1e34ddSDavid S. Miller out_free_skb:
355626b15dadSJamal Hadi Salim 	kfree_skb(skb);
35571d1e34ddSDavid S. Miller 	return err;
355826b15dadSJamal Hadi Salim }
355926b15dadSJamal Hadi Salim 
3560214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
356126b15dadSJamal Hadi Salim {
356226b15dadSJamal Hadi Salim 
356326b15dadSJamal Hadi Salim 	switch (c->event) {
3564f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
3565f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
3566f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
356726b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
3568f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
356926b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
3570f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
357126b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
357226b15dadSJamal Hadi Salim 	default:
357362db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
357462db5cfdSstephen hemminger 		       c->event);
357526b15dadSJamal Hadi Salim 	}
357626b15dadSJamal Hadi Salim 
357726b15dadSJamal Hadi Salim 	return 0;
357826b15dadSJamal Hadi Salim 
357926b15dadSJamal Hadi Salim }
358026b15dadSJamal Hadi Salim 
3581a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void)
35827deb2264SThomas Graf {
35837deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
35847deb2264SThomas Graf }
35857deb2264SThomas Graf 
358697a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
358797a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
358897a64b45SMasahide NAKAMURA {
358997a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
359097a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
359197a64b45SMasahide NAKAMURA 
359279b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
359379b8b7f4SThomas Graf 	if (nlh == NULL)
359479b8b7f4SThomas Graf 		return -EMSGSIZE;
359597a64b45SMasahide NAKAMURA 
35967b67c857SThomas Graf 	ur = nlmsg_data(nlh);
359797a64b45SMasahide NAKAMURA 	ur->proto = proto;
359897a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
359997a64b45SMasahide NAKAMURA 
36001d1e34ddSDavid S. Miller 	if (addr) {
36011d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
36021d1e34ddSDavid S. Miller 		if (err) {
36039825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
36041d1e34ddSDavid S. Miller 			return err;
36051d1e34ddSDavid S. Miller 		}
36061d1e34ddSDavid S. Miller 	}
3607053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3608053c095aSJohannes Berg 	return 0;
360997a64b45SMasahide NAKAMURA }
361097a64b45SMasahide NAKAMURA 
3611db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3612db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
361397a64b45SMasahide NAKAMURA {
361497a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
36152fc5f83bSGustavo A. R. Silva 	int err;
361697a64b45SMasahide NAKAMURA 
36177deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
361897a64b45SMasahide NAKAMURA 	if (skb == NULL)
361997a64b45SMasahide NAKAMURA 		return -ENOMEM;
362097a64b45SMasahide NAKAMURA 
36212fc5f83bSGustavo A. R. Silva 	err = build_report(skb, proto, sel, addr);
36222fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
362397a64b45SMasahide NAKAMURA 
362421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
362597a64b45SMasahide NAKAMURA }
362697a64b45SMasahide NAKAMURA 
3627a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void)
36283a2dfbe8SMartin Willi {
36293a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
36303a2dfbe8SMartin Willi }
36313a2dfbe8SMartin Willi 
36323a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
36333a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
36343a2dfbe8SMartin Willi {
36353a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
36363a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
36373a2dfbe8SMartin Willi 
36383a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
36393a2dfbe8SMartin Willi 	if (nlh == NULL)
36403a2dfbe8SMartin Willi 		return -EMSGSIZE;
36413a2dfbe8SMartin Willi 
36423a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
36433a2dfbe8SMartin Willi 
36443a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
36453a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
36463a2dfbe8SMartin Willi 	um->id.family = x->props.family;
36473a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
36483a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
36493a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
36503a2dfbe8SMartin Willi 	um->new_sport = new_sport;
36513a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
36523a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
36533a2dfbe8SMartin Willi 
3654053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3655053c095aSJohannes Berg 	return 0;
36563a2dfbe8SMartin Willi }
36573a2dfbe8SMartin Willi 
36583a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
36593a2dfbe8SMartin Willi 			     __be16 sport)
36603a2dfbe8SMartin Willi {
3661a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
36623a2dfbe8SMartin Willi 	struct sk_buff *skb;
36632fc5f83bSGustavo A. R. Silva 	int err;
36643a2dfbe8SMartin Willi 
36653a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
36663a2dfbe8SMartin Willi 		return -EINVAL;
36673a2dfbe8SMartin Willi 
36683a2dfbe8SMartin Willi 	if (!x->encap)
36693a2dfbe8SMartin Willi 		return -EINVAL;
36703a2dfbe8SMartin Willi 
36713a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
36723a2dfbe8SMartin Willi 	if (skb == NULL)
36733a2dfbe8SMartin Willi 		return -ENOMEM;
36743a2dfbe8SMartin Willi 
36752fc5f83bSGustavo A. R. Silva 	err = build_mapping(skb, x, ipaddr, sport);
36762fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
36773a2dfbe8SMartin Willi 
367821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
36793a2dfbe8SMartin Willi }
36803a2dfbe8SMartin Willi 
36810f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
36820f24558eSHoria Geanta {
36830f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
36840f24558eSHoria Geanta }
36850f24558eSHoria Geanta 
36861da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
36871da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
36881da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
36891da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
36901da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
369197a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
36925c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
36933a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
36940f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
36951da177e4SLinus Torvalds };
36961da177e4SLinus Torvalds 
3697a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
36981da177e4SLinus Torvalds {
3699be33690dSPatrick McHardy 	struct sock *nlsk;
3700a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3701a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3702a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3703a31f2d17SPablo Neira Ayuso 	};
3704be33690dSPatrick McHardy 
37059f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3706be33690dSPatrick McHardy 	if (nlsk == NULL)
37071da177e4SLinus Torvalds 		return -ENOMEM;
3708d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3709cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
37101da177e4SLinus Torvalds 	return 0;
37111da177e4SLinus Torvalds }
37121da177e4SLinus Torvalds 
37136218fe18SFlorian Westphal static void __net_exit xfrm_user_net_pre_exit(struct net *net)
37146218fe18SFlorian Westphal {
37156218fe18SFlorian Westphal 	RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
37166218fe18SFlorian Westphal }
37176218fe18SFlorian Westphal 
3718d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3719a6483b79SAlexey Dobriyan {
3720d79d792eSEric W. Biederman 	struct net *net;
37216218fe18SFlorian Westphal 
3722d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3723d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3724a6483b79SAlexey Dobriyan }
3725a6483b79SAlexey Dobriyan 
3726a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3727a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
37286218fe18SFlorian Westphal 	.pre_exit   = xfrm_user_net_pre_exit,
3729d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3730a6483b79SAlexey Dobriyan };
3731a6483b79SAlexey Dobriyan 
3732a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3733a6483b79SAlexey Dobriyan {
3734a6483b79SAlexey Dobriyan 	int rv;
3735a6483b79SAlexey Dobriyan 
3736a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3737a6483b79SAlexey Dobriyan 
3738a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3739a6483b79SAlexey Dobriyan 	if (rv < 0)
3740a6483b79SAlexey Dobriyan 		return rv;
3741f41b284aSZhengchao Shao 	xfrm_register_km(&netlink_mgr);
3742f41b284aSZhengchao Shao 	return 0;
3743a6483b79SAlexey Dobriyan }
3744a6483b79SAlexey Dobriyan 
37451da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
37461da177e4SLinus Torvalds {
37471da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3748a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
37491da177e4SLinus Torvalds }
37501da177e4SLinus Torvalds 
37511da177e4SLinus Torvalds module_init(xfrm_user_init);
37521da177e4SLinus Torvalds module_exit(xfrm_user_exit);
37531da177e4SLinus Torvalds MODULE_LICENSE("GPL");
37544fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3755