xref: /linux/net/xfrm/xfrm_user.c (revision 482db2f1dd211f73ad9d71e33ae15c1df6379982)
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 
1055424f32eSThomas Graf static inline int verify_sec_ctx_len(struct nlattr **attrs)
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) ||
115171d449aSXin Long 	    uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
116df71837dSTrent Jaeger 		return -EINVAL;
117df71837dSTrent Jaeger 
118df71837dSTrent Jaeger 	return 0;
119df71837dSTrent Jaeger }
120df71837dSTrent Jaeger 
121d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p,
122d8647b79SSteffen Klassert 				struct nlattr **attrs)
123d8647b79SSteffen Klassert {
124d8647b79SSteffen Klassert 	struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
125ecd79187SMathias Krause 	struct xfrm_replay_state_esn *rs;
126d8647b79SSteffen Klassert 
127ecd79187SMathias Krause 	if (!rt)
128d97ca5d7SFlorian Westphal 		return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
1297833aa05SSteffen Klassert 
130ecd79187SMathias Krause 	rs = nla_data(rt);
131ecd79187SMathias Krause 
132ecd79187SMathias Krause 	if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
133ecd79187SMathias Krause 		return -EINVAL;
134ecd79187SMathias Krause 
1355e708e47SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
136ecd79187SMathias Krause 	    nla_len(rt) != sizeof(*rs))
137ecd79187SMathias Krause 		return -EINVAL;
138d8647b79SSteffen Klassert 
13901714109SFan Du 	/* As only ESP and AH support ESN feature. */
14001714109SFan Du 	if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
14102aadf72SSteffen Klassert 		return -EINVAL;
14202aadf72SSteffen Klassert 
143d8647b79SSteffen Klassert 	if (p->replay_window != 0)
144d8647b79SSteffen Klassert 		return -EINVAL;
145d8647b79SSteffen Klassert 
146d8647b79SSteffen Klassert 	return 0;
147d8647b79SSteffen Klassert }
148df71837dSTrent Jaeger 
1491da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p,
1505424f32eSThomas Graf 			     struct nlattr **attrs)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	int err;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	err = -EINVAL;
1551da177e4SLinus Torvalds 	switch (p->family) {
1561da177e4SLinus Torvalds 	case AF_INET:
157b38ff407SAnirudh Gupta 		break;
158b38ff407SAnirudh Gupta 
159b38ff407SAnirudh Gupta 	case AF_INET6:
160b38ff407SAnirudh Gupta #if IS_ENABLED(CONFIG_IPV6)
161b38ff407SAnirudh Gupta 		break;
162b38ff407SAnirudh Gupta #else
163b38ff407SAnirudh Gupta 		err = -EAFNOSUPPORT;
164b38ff407SAnirudh Gupta 		goto out;
165b38ff407SAnirudh Gupta #endif
166b38ff407SAnirudh Gupta 
167b38ff407SAnirudh Gupta 	default:
168b38ff407SAnirudh Gupta 		goto out;
169b38ff407SAnirudh Gupta 	}
170b38ff407SAnirudh Gupta 
171b38ff407SAnirudh Gupta 	switch (p->sel.family) {
172b8d6d007SNicolas Dichtel 	case AF_UNSPEC:
173b8d6d007SNicolas Dichtel 		break;
174b8d6d007SNicolas Dichtel 
175b38ff407SAnirudh Gupta 	case AF_INET:
17607bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
17707bf7908SSteffen Klassert 			goto out;
17807bf7908SSteffen Klassert 
1791da177e4SLinus Torvalds 		break;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	case AF_INET6:
182dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
18307bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
18407bf7908SSteffen Klassert 			goto out;
18507bf7908SSteffen Klassert 
1861da177e4SLinus Torvalds 		break;
1871da177e4SLinus Torvalds #else
1881da177e4SLinus Torvalds 		err = -EAFNOSUPPORT;
1891da177e4SLinus Torvalds 		goto out;
1901da177e4SLinus Torvalds #endif
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	default:
1931da177e4SLinus Torvalds 		goto out;
1943ff50b79SStephen Hemminger 	}
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	err = -EINVAL;
1971da177e4SLinus Torvalds 	switch (p->id.proto) {
1981da177e4SLinus Torvalds 	case IPPROTO_AH:
1994447bb33SMartin Willi 		if ((!attrs[XFRMA_ALG_AUTH]	&&
2004447bb33SMartin Willi 		     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
2011a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
20235a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
20335d2856bSMartin Willi 		    attrs[XFRMA_ALG_COMP]	||
204a0e5ef53STobias Brunner 		    attrs[XFRMA_TFCPAD])
2051da177e4SLinus Torvalds 			goto out;
2061da177e4SLinus Torvalds 		break;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	case IPPROTO_ESP:
2091a6509d9SHerbert Xu 		if (attrs[XFRMA_ALG_COMP])
2101a6509d9SHerbert Xu 			goto out;
2111a6509d9SHerbert Xu 		if (!attrs[XFRMA_ALG_AUTH] &&
2124447bb33SMartin Willi 		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
2131a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_CRYPT] &&
2141a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_AEAD])
2151a6509d9SHerbert Xu 			goto out;
2161a6509d9SHerbert Xu 		if ((attrs[XFRMA_ALG_AUTH] ||
2174447bb33SMartin Willi 		     attrs[XFRMA_ALG_AUTH_TRUNC] ||
2181a6509d9SHerbert Xu 		     attrs[XFRMA_ALG_CRYPT]) &&
2191a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD])
2201da177e4SLinus Torvalds 			goto out;
22135d2856bSMartin Willi 		if (attrs[XFRMA_TFCPAD] &&
22235d2856bSMartin Willi 		    p->mode != XFRM_MODE_TUNNEL)
22335d2856bSMartin Willi 			goto out;
2241da177e4SLinus Torvalds 		break;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	case IPPROTO_COMP:
22735a7aa08SThomas Graf 		if (!attrs[XFRMA_ALG_COMP]	||
2281a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
22935a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2304447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
23135d2856bSMartin Willi 		    attrs[XFRMA_ALG_CRYPT]	||
232a0e5ef53STobias Brunner 		    attrs[XFRMA_TFCPAD]		||
233a0e5ef53STobias Brunner 		    (ntohl(p->id.spi) >= 0x10000))
2341da177e4SLinus Torvalds 			goto out;
2351da177e4SLinus Torvalds 		break;
2361da177e4SLinus Torvalds 
237dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
238e23c7194SMasahide NAKAMURA 	case IPPROTO_DSTOPTS:
239e23c7194SMasahide NAKAMURA 	case IPPROTO_ROUTING:
24035a7aa08SThomas Graf 		if (attrs[XFRMA_ALG_COMP]	||
24135a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2424447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
2431a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
24435a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
24535a7aa08SThomas Graf 		    attrs[XFRMA_ENCAP]		||
24635a7aa08SThomas Graf 		    attrs[XFRMA_SEC_CTX]	||
24735d2856bSMartin Willi 		    attrs[XFRMA_TFCPAD]		||
24835a7aa08SThomas Graf 		    !attrs[XFRMA_COADDR])
249e23c7194SMasahide NAKAMURA 			goto out;
250e23c7194SMasahide NAKAMURA 		break;
251e23c7194SMasahide NAKAMURA #endif
252e23c7194SMasahide NAKAMURA 
2531da177e4SLinus Torvalds 	default:
2541da177e4SLinus Torvalds 		goto out;
2553ff50b79SStephen Hemminger 	}
2561da177e4SLinus Torvalds 
2571a6509d9SHerbert Xu 	if ((err = verify_aead(attrs)))
2581a6509d9SHerbert Xu 		goto out;
2594447bb33SMartin Willi 	if ((err = verify_auth_trunc(attrs)))
2604447bb33SMartin Willi 		goto out;
26135a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
2621da177e4SLinus Torvalds 		goto out;
26335a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
2641da177e4SLinus Torvalds 		goto out;
26535a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
2661da177e4SLinus Torvalds 		goto out;
26735a7aa08SThomas Graf 	if ((err = verify_sec_ctx_len(attrs)))
268df71837dSTrent Jaeger 		goto out;
269d8647b79SSteffen Klassert 	if ((err = verify_replay(p, attrs)))
270d8647b79SSteffen Klassert 		goto out;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	err = -EINVAL;
2731da177e4SLinus Torvalds 	switch (p->mode) {
2747e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TRANSPORT:
2757e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TUNNEL:
276060f02a3SNoriaki TAKAMIYA 	case XFRM_MODE_ROUTEOPTIMIZATION:
2770a69452cSDiego Beltrami 	case XFRM_MODE_BEET:
2781da177e4SLinus Torvalds 		break;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	default:
2811da177e4SLinus Torvalds 		goto out;
2823ff50b79SStephen Hemminger 	}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	err = 0;
2851da177e4SLinus Torvalds 
2864e484b3eSAntony Antony 	if (attrs[XFRMA_MTIMER_THRESH])
2874e484b3eSAntony Antony 		if (!attrs[XFRMA_ENCAP])
2884e484b3eSAntony Antony 			err = -EINVAL;
2894e484b3eSAntony Antony 
2901da177e4SLinus Torvalds out:
2911da177e4SLinus Torvalds 	return err;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
2956f2f19edSDavid S. Miller 			   struct xfrm_algo_desc *(*get_byname)(const char *, int),
2965424f32eSThomas Graf 			   struct nlattr *rta)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	struct xfrm_algo *p, *ualg;
2991da177e4SLinus Torvalds 	struct xfrm_algo_desc *algo;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	if (!rta)
3021da177e4SLinus Torvalds 		return 0;
3031da177e4SLinus Torvalds 
3045424f32eSThomas Graf 	ualg = nla_data(rta);
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	algo = get_byname(ualg->alg_name, 1);
3071da177e4SLinus Torvalds 	if (!algo)
3081da177e4SLinus Torvalds 		return -ENOSYS;
3091da177e4SLinus Torvalds 	*props = algo->desc.sadb_alg_id;
3101da177e4SLinus Torvalds 
3110f99be0dSEric Dumazet 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
3121da177e4SLinus Torvalds 	if (!p)
3131da177e4SLinus Torvalds 		return -ENOMEM;
3141da177e4SLinus Torvalds 
31504ff1260SHerbert Xu 	strcpy(p->alg_name, algo->name);
3161da177e4SLinus Torvalds 	*algpp = p;
3171da177e4SLinus Torvalds 	return 0;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
32069b0137fSHerbert Xu static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
32169b0137fSHerbert Xu {
32269b0137fSHerbert Xu 	struct xfrm_algo *p, *ualg;
32369b0137fSHerbert Xu 	struct xfrm_algo_desc *algo;
32469b0137fSHerbert Xu 
32569b0137fSHerbert Xu 	if (!rta)
32669b0137fSHerbert Xu 		return 0;
32769b0137fSHerbert Xu 
32869b0137fSHerbert Xu 	ualg = nla_data(rta);
32969b0137fSHerbert Xu 
33069b0137fSHerbert Xu 	algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
33169b0137fSHerbert Xu 	if (!algo)
33269b0137fSHerbert Xu 		return -ENOSYS;
33369b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
33469b0137fSHerbert Xu 
33569b0137fSHerbert Xu 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
33669b0137fSHerbert Xu 	if (!p)
33769b0137fSHerbert Xu 		return -ENOMEM;
33869b0137fSHerbert Xu 
33969b0137fSHerbert Xu 	strcpy(p->alg_name, algo->name);
34069b0137fSHerbert Xu 	x->ealg = p;
34169b0137fSHerbert Xu 	x->geniv = algo->uinfo.encr.geniv;
34269b0137fSHerbert Xu 	return 0;
34369b0137fSHerbert Xu }
34469b0137fSHerbert Xu 
3454447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
3464447bb33SMartin Willi 		       struct nlattr *rta)
3474447bb33SMartin Willi {
3484447bb33SMartin Willi 	struct xfrm_algo *ualg;
3494447bb33SMartin Willi 	struct xfrm_algo_auth *p;
3504447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3514447bb33SMartin Willi 
3524447bb33SMartin Willi 	if (!rta)
3534447bb33SMartin Willi 		return 0;
3544447bb33SMartin Willi 
3554447bb33SMartin Willi 	ualg = nla_data(rta);
3564447bb33SMartin Willi 
3574447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3584447bb33SMartin Willi 	if (!algo)
3594447bb33SMartin Willi 		return -ENOSYS;
3604447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3614447bb33SMartin Willi 
3624447bb33SMartin Willi 	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
3634447bb33SMartin Willi 	if (!p)
3644447bb33SMartin Willi 		return -ENOMEM;
3654447bb33SMartin Willi 
3664447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3674447bb33SMartin Willi 	p->alg_key_len = ualg->alg_key_len;
3684447bb33SMartin Willi 	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3694447bb33SMartin Willi 	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
3704447bb33SMartin Willi 
3714447bb33SMartin Willi 	*algpp = p;
3724447bb33SMartin Willi 	return 0;
3734447bb33SMartin Willi }
3744447bb33SMartin Willi 
3754447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
3764447bb33SMartin Willi 			     struct nlattr *rta)
3774447bb33SMartin Willi {
3784447bb33SMartin Willi 	struct xfrm_algo_auth *p, *ualg;
3794447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3804447bb33SMartin Willi 
3814447bb33SMartin Willi 	if (!rta)
3824447bb33SMartin Willi 		return 0;
3834447bb33SMartin Willi 
3844447bb33SMartin Willi 	ualg = nla_data(rta);
3854447bb33SMartin Willi 
3864447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3874447bb33SMartin Willi 	if (!algo)
3884447bb33SMartin Willi 		return -ENOSYS;
389689f1c9dSHerbert Xu 	if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
3904447bb33SMartin Willi 		return -EINVAL;
3914447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3924447bb33SMartin Willi 
3934447bb33SMartin Willi 	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
3944447bb33SMartin Willi 	if (!p)
3954447bb33SMartin Willi 		return -ENOMEM;
3964447bb33SMartin Willi 
3974447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3984447bb33SMartin Willi 	if (!p->alg_trunc_len)
3994447bb33SMartin Willi 		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
4004447bb33SMartin Willi 
4014447bb33SMartin Willi 	*algpp = p;
4024447bb33SMartin Willi 	return 0;
4034447bb33SMartin Willi }
4044447bb33SMartin Willi 
40569b0137fSHerbert Xu static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
4061a6509d9SHerbert Xu {
4071a6509d9SHerbert Xu 	struct xfrm_algo_aead *p, *ualg;
4081a6509d9SHerbert Xu 	struct xfrm_algo_desc *algo;
4091a6509d9SHerbert Xu 
4101a6509d9SHerbert Xu 	if (!rta)
4111a6509d9SHerbert Xu 		return 0;
4121a6509d9SHerbert Xu 
4131a6509d9SHerbert Xu 	ualg = nla_data(rta);
4141a6509d9SHerbert Xu 
4151a6509d9SHerbert Xu 	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
4161a6509d9SHerbert Xu 	if (!algo)
4171a6509d9SHerbert Xu 		return -ENOSYS;
41869b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
4191a6509d9SHerbert Xu 
4201a6509d9SHerbert Xu 	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
4211a6509d9SHerbert Xu 	if (!p)
4221a6509d9SHerbert Xu 		return -ENOMEM;
4231a6509d9SHerbert Xu 
4241a6509d9SHerbert Xu 	strcpy(p->alg_name, algo->name);
42569b0137fSHerbert Xu 	x->aead = p;
42669b0137fSHerbert Xu 	x->geniv = algo->uinfo.aead.geniv;
4271a6509d9SHerbert Xu 	return 0;
4281a6509d9SHerbert Xu }
4291a6509d9SHerbert Xu 
430e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
431e2b19125SSteffen Klassert 					 struct nlattr *rp)
432e2b19125SSteffen Klassert {
433e2b19125SSteffen Klassert 	struct xfrm_replay_state_esn *up;
4345e708e47SAlexey Dobriyan 	unsigned int ulen;
435e2b19125SSteffen Klassert 
436e2b19125SSteffen Klassert 	if (!replay_esn || !rp)
437e2b19125SSteffen Klassert 		return 0;
438e2b19125SSteffen Klassert 
439e2b19125SSteffen Klassert 	up = nla_data(rp);
440ecd79187SMathias Krause 	ulen = xfrm_replay_state_esn_len(up);
441e2b19125SSteffen Klassert 
442f843ee6dSAndy Whitcroft 	/* Check the overall length and the internal bitmap length to avoid
443f843ee6dSAndy Whitcroft 	 * potential overflow. */
4445e708e47SAlexey Dobriyan 	if (nla_len(rp) < (int)ulen ||
445f843ee6dSAndy Whitcroft 	    xfrm_replay_state_esn_len(replay_esn) != ulen ||
446f843ee6dSAndy Whitcroft 	    replay_esn->bmp_len != up->bmp_len)
447e2b19125SSteffen Klassert 		return -EINVAL;
448e2b19125SSteffen Klassert 
449677e806dSAndy Whitcroft 	if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
450677e806dSAndy Whitcroft 		return -EINVAL;
451677e806dSAndy Whitcroft 
452e2b19125SSteffen Klassert 	return 0;
453e2b19125SSteffen Klassert }
454e2b19125SSteffen Klassert 
455d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
456d8647b79SSteffen Klassert 				       struct xfrm_replay_state_esn **preplay_esn,
457d8647b79SSteffen Klassert 				       struct nlattr *rta)
458d8647b79SSteffen Klassert {
459d8647b79SSteffen Klassert 	struct xfrm_replay_state_esn *p, *pp, *up;
4605e708e47SAlexey Dobriyan 	unsigned int klen, ulen;
461d8647b79SSteffen Klassert 
462d8647b79SSteffen Klassert 	if (!rta)
463d8647b79SSteffen Klassert 		return 0;
464d8647b79SSteffen Klassert 
465d8647b79SSteffen Klassert 	up = nla_data(rta);
466ecd79187SMathias Krause 	klen = xfrm_replay_state_esn_len(up);
4675e708e47SAlexey Dobriyan 	ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
468d8647b79SSteffen Klassert 
469ecd79187SMathias Krause 	p = kzalloc(klen, GFP_KERNEL);
470d8647b79SSteffen Klassert 	if (!p)
471d8647b79SSteffen Klassert 		return -ENOMEM;
472d8647b79SSteffen Klassert 
473ecd79187SMathias Krause 	pp = kzalloc(klen, GFP_KERNEL);
474d8647b79SSteffen Klassert 	if (!pp) {
475d8647b79SSteffen Klassert 		kfree(p);
476d8647b79SSteffen Klassert 		return -ENOMEM;
477d8647b79SSteffen Klassert 	}
478d8647b79SSteffen Klassert 
479ecd79187SMathias Krause 	memcpy(p, up, ulen);
480ecd79187SMathias Krause 	memcpy(pp, up, ulen);
481ecd79187SMathias Krause 
482d8647b79SSteffen Klassert 	*replay_esn = p;
483d8647b79SSteffen Klassert 	*preplay_esn = pp;
484d8647b79SSteffen Klassert 
485d8647b79SSteffen Klassert 	return 0;
486d8647b79SSteffen Klassert }
487d8647b79SSteffen Klassert 
488a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
489df71837dSTrent Jaeger {
490a1b831f2SAlexey Dobriyan 	unsigned int len = 0;
491df71837dSTrent Jaeger 
492df71837dSTrent Jaeger 	if (xfrm_ctx) {
493df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
494df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
495df71837dSTrent Jaeger 	}
496df71837dSTrent Jaeger 	return len;
497df71837dSTrent Jaeger }
498df71837dSTrent Jaeger 
4991da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
5021da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
5031da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
5041da177e4SLinus Torvalds 	x->props.mode = p->mode;
50533fce60dSFan Du 	x->props.replay_window = min_t(unsigned int, p->replay_window,
50633fce60dSFan Du 					sizeof(x->replay.bitmap) * 8);
5071da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
5081da177e4SLinus Torvalds 	x->props.family = p->family;
50954489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
5101da177e4SLinus Torvalds 	x->props.flags = p->flags;
511196b0036SHerbert Xu 
512ccf9b3b8SSteffen Klassert 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
513196b0036SHerbert Xu 		x->sel.family = p->family;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
516d51d081dSJamal Hadi Salim /*
517d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
518d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
519d51d081dSJamal Hadi Salim  *
520d51d081dSJamal Hadi Salim */
521e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
522e3ac104dSMathias Krause 				  int update_esn)
523d51d081dSJamal Hadi Salim {
5245424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
525e3ac104dSMathias Krause 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
5265424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
5275424f32eSThomas Graf 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
5285424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
5294e484b3eSAntony Antony 	struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
530d51d081dSJamal Hadi Salim 
531d8647b79SSteffen Klassert 	if (re) {
532d8647b79SSteffen Klassert 		struct xfrm_replay_state_esn *replay_esn;
533d8647b79SSteffen Klassert 		replay_esn = nla_data(re);
534d8647b79SSteffen Klassert 		memcpy(x->replay_esn, replay_esn,
535d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
536d8647b79SSteffen Klassert 		memcpy(x->preplay_esn, replay_esn,
537d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
538d8647b79SSteffen Klassert 	}
539d8647b79SSteffen Klassert 
540d51d081dSJamal Hadi Salim 	if (rp) {
541d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
5425424f32eSThomas Graf 		replay = nla_data(rp);
543d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
544d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
545d51d081dSJamal Hadi Salim 	}
546d51d081dSJamal Hadi Salim 
547d51d081dSJamal Hadi Salim 	if (lt) {
548d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
5495424f32eSThomas Graf 		ltime = nla_data(lt);
550d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
551d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
552d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
553d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
554d51d081dSJamal Hadi Salim 	}
555d51d081dSJamal Hadi Salim 
556cf5cb79fSThomas Graf 	if (et)
5575424f32eSThomas Graf 		x->replay_maxage = nla_get_u32(et);
558d51d081dSJamal Hadi Salim 
559cf5cb79fSThomas Graf 	if (rt)
5605424f32eSThomas Graf 		x->replay_maxdiff = nla_get_u32(rt);
5614e484b3eSAntony Antony 
5624e484b3eSAntony Antony 	if (mt)
5634e484b3eSAntony Antony 		x->mapping_maxage = nla_get_u32(mt);
564d51d081dSJamal Hadi Salim }
565d51d081dSJamal Hadi Salim 
5669b42c1f1SSteffen Klassert static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
5679b42c1f1SSteffen Klassert {
5689b42c1f1SSteffen Klassert 	if (attrs[XFRMA_SET_MARK]) {
5699b42c1f1SSteffen Klassert 		m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
5709b42c1f1SSteffen Klassert 		if (attrs[XFRMA_SET_MARK_MASK])
5719b42c1f1SSteffen Klassert 			m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
5729b42c1f1SSteffen Klassert 		else
5739b42c1f1SSteffen Klassert 			m->m = 0xffffffff;
5749b42c1f1SSteffen Klassert 	} else {
5759b42c1f1SSteffen Klassert 		m->v = m->m = 0;
5769b42c1f1SSteffen Klassert 	}
5779b42c1f1SSteffen Klassert }
5789b42c1f1SSteffen Klassert 
579fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
580fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
5815424f32eSThomas Graf 					       struct nlattr **attrs,
5821da177e4SLinus Torvalds 					       int *errp)
5831da177e4SLinus Torvalds {
584fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
5851da177e4SLinus Torvalds 	int err = -ENOMEM;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	if (!x)
5881da177e4SLinus Torvalds 		goto error_no_put;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	copy_from_user_state(x, p);
5911da177e4SLinus Torvalds 
5926fd06963SSteffen Klassert 	if (attrs[XFRMA_ENCAP]) {
5936fd06963SSteffen Klassert 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
5946fd06963SSteffen Klassert 				   sizeof(*x->encap), GFP_KERNEL);
5956fd06963SSteffen Klassert 		if (x->encap == NULL)
5966fd06963SSteffen Klassert 			goto error;
5976fd06963SSteffen Klassert 	}
5986fd06963SSteffen Klassert 
5996fd06963SSteffen Klassert 	if (attrs[XFRMA_COADDR]) {
6006fd06963SSteffen Klassert 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
6016fd06963SSteffen Klassert 				    sizeof(*x->coaddr), GFP_KERNEL);
6026fd06963SSteffen Klassert 		if (x->coaddr == NULL)
6036fd06963SSteffen Klassert 			goto error;
6046fd06963SSteffen Klassert 	}
6056fd06963SSteffen Klassert 
606a947b0a9SNicolas Dichtel 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
607a947b0a9SNicolas Dichtel 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
608a947b0a9SNicolas Dichtel 
60969b0137fSHerbert Xu 	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
6101a6509d9SHerbert Xu 		goto error;
6114447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
6124447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
6134447bb33SMartin Willi 		goto error;
6144447bb33SMartin Willi 	if (!x->props.aalgo) {
6154447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
61635a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
6171da177e4SLinus Torvalds 			goto error;
6184447bb33SMartin Willi 	}
61969b0137fSHerbert Xu 	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
6201da177e4SLinus Torvalds 		goto error;
6211da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
6221da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
62335a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
6241da177e4SLinus Torvalds 		goto error;
625fd21150aSThomas Graf 
62635d2856bSMartin Willi 	if (attrs[XFRMA_TFCPAD])
62735d2856bSMartin Willi 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
62835d2856bSMartin Willi 
6296f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &x->mark);
6306f26b61eSJamal Hadi Salim 
6319b42c1f1SSteffen Klassert 	xfrm_smark_init(attrs, &x->props.smark);
632077fbac4SLorenzo Colitti 
633a3d9001bSKai Lueke 	if (attrs[XFRMA_IF_ID])
6347e652640SSteffen Klassert 		x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
635fd21150aSThomas Graf 
636ffdb5211SIlan Tayari 	err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
6371da177e4SLinus Torvalds 	if (err)
6381da177e4SLinus Torvalds 		goto error;
6391da177e4SLinus Torvalds 
6402f30ea50SMathias Krause 	if (attrs[XFRMA_SEC_CTX]) {
6412f30ea50SMathias Krause 		err = security_xfrm_state_alloc(x,
6422f30ea50SMathias Krause 						nla_data(attrs[XFRMA_SEC_CTX]));
6432f30ea50SMathias Krause 		if (err)
644df71837dSTrent Jaeger 			goto error;
6452f30ea50SMathias Krause 	}
646df71837dSTrent Jaeger 
647d8647b79SSteffen Klassert 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
648d8647b79SSteffen Klassert 					       attrs[XFRMA_REPLAY_ESN_VAL])))
649d8647b79SSteffen Klassert 		goto error;
650d8647b79SSteffen Klassert 
6511da177e4SLinus Torvalds 	x->km.seq = p->seq;
652b27aeadbSAlexey Dobriyan 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
653d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
654b27aeadbSAlexey Dobriyan 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
655d51d081dSJamal Hadi Salim 
6569fdc4883SSteffen Klassert 	if ((err = xfrm_init_replay(x)))
6579fdc4883SSteffen Klassert 		goto error;
658d51d081dSJamal Hadi Salim 
6599fdc4883SSteffen Klassert 	/* override default values from above */
660e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 0);
6611da177e4SLinus Torvalds 
662cc01572eSYossi Kuperman 	/* configure the hardware if offload is requested */
663cc01572eSYossi Kuperman 	if (attrs[XFRMA_OFFLOAD_DEV]) {
664cc01572eSYossi Kuperman 		err = xfrm_dev_state_add(net, x,
665cc01572eSYossi Kuperman 					 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
666cc01572eSYossi Kuperman 		if (err)
667cc01572eSYossi Kuperman 			goto error;
668cc01572eSYossi Kuperman 	}
669cc01572eSYossi Kuperman 
6701da177e4SLinus Torvalds 	return x;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds error:
6731da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
6741da177e4SLinus Torvalds 	xfrm_state_put(x);
6751da177e4SLinus Torvalds error_no_put:
6761da177e4SLinus Torvalds 	*errp = err;
6771da177e4SLinus Torvalds 	return NULL;
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
68022e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
6815424f32eSThomas Graf 		struct nlattr **attrs)
6821da177e4SLinus Torvalds {
683fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6847b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
6851da177e4SLinus Torvalds 	struct xfrm_state *x;
6861da177e4SLinus Torvalds 	int err;
68726b15dadSJamal Hadi Salim 	struct km_event c;
6881da177e4SLinus Torvalds 
68935a7aa08SThomas Graf 	err = verify_newsa_info(p, attrs);
6901da177e4SLinus Torvalds 	if (err)
6911da177e4SLinus Torvalds 		return err;
6921da177e4SLinus Torvalds 
693fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
6941da177e4SLinus Torvalds 	if (!x)
6951da177e4SLinus Torvalds 		return err;
6961da177e4SLinus Torvalds 
69726b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
6981da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
6991da177e4SLinus Torvalds 		err = xfrm_state_add(x);
7001da177e4SLinus Torvalds 	else
7011da177e4SLinus Torvalds 		err = xfrm_state_update(x);
7021da177e4SLinus Torvalds 
7032e71029eSTetsuo Handa 	xfrm_audit_state_add(x, err ? 0 : 1, true);
704161a09e7SJoy Latten 
7051da177e4SLinus Torvalds 	if (err < 0) {
7061da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
707c5d4d7d8SSteffen Klassert 		xfrm_dev_state_delete(x);
70821380b81SHerbert Xu 		__xfrm_state_put(x);
7097d6dfe1fSPatrick McHardy 		goto out;
7101da177e4SLinus Torvalds 	}
7111da177e4SLinus Torvalds 
712cc01572eSYossi Kuperman 	if (x->km.state == XFRM_STATE_VOID)
713cc01572eSYossi Kuperman 		x->km.state = XFRM_STATE_VALID;
714cc01572eSYossi Kuperman 
71526b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
71615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
717f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
71826b15dadSJamal Hadi Salim 
71926b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7207d6dfe1fSPatrick McHardy out:
72126b15dadSJamal Hadi Salim 	xfrm_state_put(x);
7221da177e4SLinus Torvalds 	return err;
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
725fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
726fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
7275424f32eSThomas Graf 						 struct nlattr **attrs,
728eb2971b6SMasahide NAKAMURA 						 int *errp)
729eb2971b6SMasahide NAKAMURA {
730eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
7316f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
732eb2971b6SMasahide NAKAMURA 	int err;
7336f26b61eSJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
734eb2971b6SMasahide NAKAMURA 
735eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
736eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
7376f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
738eb2971b6SMasahide NAKAMURA 	} else {
739eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
740eb2971b6SMasahide NAKAMURA 
74135a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
742eb2971b6SMasahide NAKAMURA 		if (!saddr) {
743eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
744eb2971b6SMasahide NAKAMURA 			goto out;
745eb2971b6SMasahide NAKAMURA 		}
746eb2971b6SMasahide NAKAMURA 
7479abbffeeSMasahide NAKAMURA 		err = -ESRCH;
7486f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup_byaddr(net, mark,
7496f26b61eSJamal Hadi Salim 					     &p->daddr, saddr,
750221df1edSAlexey Dobriyan 					     p->proto, p->family);
751eb2971b6SMasahide NAKAMURA 	}
752eb2971b6SMasahide NAKAMURA 
753eb2971b6SMasahide NAKAMURA  out:
754eb2971b6SMasahide NAKAMURA 	if (!x && errp)
755eb2971b6SMasahide NAKAMURA 		*errp = err;
756eb2971b6SMasahide NAKAMURA 	return x;
757eb2971b6SMasahide NAKAMURA }
758eb2971b6SMasahide NAKAMURA 
75922e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
7605424f32eSThomas Graf 		struct nlattr **attrs)
7611da177e4SLinus Torvalds {
762fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
7631da177e4SLinus Torvalds 	struct xfrm_state *x;
764eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
76526b15dadSJamal Hadi Salim 	struct km_event c;
7667b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
7671da177e4SLinus Torvalds 
768fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
7691da177e4SLinus Torvalds 	if (x == NULL)
770eb2971b6SMasahide NAKAMURA 		return err;
7711da177e4SLinus Torvalds 
7726f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
773c8c05a8eSCatherine Zhang 		goto out;
774c8c05a8eSCatherine Zhang 
7751da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
776c8c05a8eSCatherine Zhang 		err = -EPERM;
777c8c05a8eSCatherine Zhang 		goto out;
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 
78026b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
781161a09e7SJoy Latten 
782c8c05a8eSCatherine Zhang 	if (err < 0)
783c8c05a8eSCatherine Zhang 		goto out;
78426b15dadSJamal Hadi Salim 
78526b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
78615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
787f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
78826b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7891da177e4SLinus Torvalds 
790c8c05a8eSCatherine Zhang out:
7912e71029eSTetsuo Handa 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
792c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
79326b15dadSJamal Hadi Salim 	return err;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
7971da177e4SLinus Torvalds {
798f778a636SMathias Krause 	memset(p, 0, sizeof(*p));
7991da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
8001da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
8011da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
8021da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
803e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
804e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay, &p->stats.replay);
805e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
80654489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
8071da177e4SLinus Torvalds 	p->mode = x->props.mode;
8081da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
8091da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
8101da177e4SLinus Torvalds 	p->family = x->props.family;
8111da177e4SLinus Torvalds 	p->flags = x->props.flags;
8121da177e4SLinus Torvalds 	p->seq = x->km.seq;
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds struct xfrm_dump_info {
8161da177e4SLinus Torvalds 	struct sk_buff *in_skb;
8171da177e4SLinus Torvalds 	struct sk_buff *out_skb;
8181da177e4SLinus Torvalds 	u32 nlmsg_seq;
8191da177e4SLinus Torvalds 	u16 nlmsg_flags;
8201da177e4SLinus Torvalds };
8211da177e4SLinus Torvalds 
822c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
823c0144beaSThomas Graf {
824c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
825c0144beaSThomas Graf 	struct nlattr *attr;
82668325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
827c0144beaSThomas Graf 
828c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
829c0144beaSThomas Graf 	if (attr == NULL)
830c0144beaSThomas Graf 		return -EMSGSIZE;
831c0144beaSThomas Graf 
832c0144beaSThomas Graf 	uctx = nla_data(attr);
833c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
834c0144beaSThomas Graf 	uctx->len = ctx_size;
835c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
836c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
837c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
838c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
839c0144beaSThomas Graf 
840c0144beaSThomas Graf 	return 0;
841c0144beaSThomas Graf }
842c0144beaSThomas Graf 
84387e0a94eSLeon Romanovsky static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb)
844d77e38e6SSteffen Klassert {
845d77e38e6SSteffen Klassert 	struct xfrm_user_offload *xuo;
846d77e38e6SSteffen Klassert 	struct nlattr *attr;
847d77e38e6SSteffen Klassert 
848d77e38e6SSteffen Klassert 	attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
849d77e38e6SSteffen Klassert 	if (attr == NULL)
850d77e38e6SSteffen Klassert 		return -EMSGSIZE;
851d77e38e6SSteffen Klassert 
852d77e38e6SSteffen Klassert 	xuo = nla_data(attr);
8535fe0d4bdSMathias Krause 	memset(xuo, 0, sizeof(*xuo));
854d77e38e6SSteffen Klassert 	xuo->ifindex = xso->dev->ifindex;
855*482db2f1SLeon Romanovsky 	if (xso->dir == XFRM_DEV_OFFLOAD_IN)
856*482db2f1SLeon Romanovsky 		xuo->flags = XFRM_OFFLOAD_INBOUND;
857d77e38e6SSteffen Klassert 
858d77e38e6SSteffen Klassert 	return 0;
859d77e38e6SSteffen Klassert }
860d77e38e6SSteffen Klassert 
861c7a5899eSAntony Antony static bool xfrm_redact(void)
862c7a5899eSAntony Antony {
863c7a5899eSAntony Antony 	return IS_ENABLED(CONFIG_SECURITY) &&
864c7a5899eSAntony Antony 		security_locked_down(LOCKDOWN_XFRM_SECRET);
865c7a5899eSAntony Antony }
866c7a5899eSAntony Antony 
8674447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
8684447bb33SMartin Willi {
8694447bb33SMartin Willi 	struct xfrm_algo *algo;
870c7a5899eSAntony Antony 	struct xfrm_algo_auth *ap;
8714447bb33SMartin Willi 	struct nlattr *nla;
872c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
8734447bb33SMartin Willi 
8744447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
8754447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
8764447bb33SMartin Willi 	if (!nla)
8774447bb33SMartin Willi 		return -EMSGSIZE;
8784447bb33SMartin Willi 	algo = nla_data(nla);
8794c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
880c7a5899eSAntony Antony 
881c7a5899eSAntony Antony 	if (redact_secret && auth->alg_key_len)
882c7a5899eSAntony Antony 		memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
883c7a5899eSAntony Antony 	else
884c7a5899eSAntony Antony 		memcpy(algo->alg_key, auth->alg_key,
885c7a5899eSAntony Antony 		       (auth->alg_key_len + 7) / 8);
8864447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
8874447bb33SMartin Willi 
888c7a5899eSAntony Antony 	nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
889c7a5899eSAntony Antony 	if (!nla)
890c7a5899eSAntony Antony 		return -EMSGSIZE;
891c7a5899eSAntony Antony 	ap = nla_data(nla);
892c7a5899eSAntony Antony 	memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
893c7a5899eSAntony Antony 	if (redact_secret && auth->alg_key_len)
894c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
895c7a5899eSAntony Antony 	else
896c7a5899eSAntony Antony 		memcpy(ap->alg_key, auth->alg_key,
897c7a5899eSAntony Antony 		       (auth->alg_key_len + 7) / 8);
898c7a5899eSAntony Antony 	return 0;
899c7a5899eSAntony Antony }
900c7a5899eSAntony Antony 
901c7a5899eSAntony Antony static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
902c7a5899eSAntony Antony {
903c7a5899eSAntony Antony 	struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
904c7a5899eSAntony Antony 	struct xfrm_algo_aead *ap;
905c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
906c7a5899eSAntony Antony 
907c7a5899eSAntony Antony 	if (!nla)
908c7a5899eSAntony Antony 		return -EMSGSIZE;
909c7a5899eSAntony Antony 
910c7a5899eSAntony Antony 	ap = nla_data(nla);
911c7a5899eSAntony Antony 	memcpy(ap, aead, sizeof(*aead));
912c7a5899eSAntony Antony 
913c7a5899eSAntony Antony 	if (redact_secret && aead->alg_key_len)
914c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
915c7a5899eSAntony Antony 	else
916c7a5899eSAntony Antony 		memcpy(ap->alg_key, aead->alg_key,
917c7a5899eSAntony Antony 		       (aead->alg_key_len + 7) / 8);
918c7a5899eSAntony Antony 	return 0;
919c7a5899eSAntony Antony }
920c7a5899eSAntony Antony 
921c7a5899eSAntony Antony static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
922c7a5899eSAntony Antony {
923c7a5899eSAntony Antony 	struct xfrm_algo *ap;
924c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
925c7a5899eSAntony Antony 	struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
926c7a5899eSAntony Antony 					 xfrm_alg_len(ealg));
927c7a5899eSAntony Antony 	if (!nla)
928c7a5899eSAntony Antony 		return -EMSGSIZE;
929c7a5899eSAntony Antony 
930c7a5899eSAntony Antony 	ap = nla_data(nla);
931c7a5899eSAntony Antony 	memcpy(ap, ealg, sizeof(*ealg));
932c7a5899eSAntony Antony 
933c7a5899eSAntony Antony 	if (redact_secret && ealg->alg_key_len)
934c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
935c7a5899eSAntony Antony 	else
936c7a5899eSAntony Antony 		memcpy(ap->alg_key, ealg->alg_key,
937c7a5899eSAntony Antony 		       (ealg->alg_key_len + 7) / 8);
938c7a5899eSAntony Antony 
9394447bb33SMartin Willi 	return 0;
9404447bb33SMartin Willi }
9414447bb33SMartin Willi 
9429b42c1f1SSteffen Klassert static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
9439b42c1f1SSteffen Klassert {
9449b42c1f1SSteffen Klassert 	int ret = 0;
9459b42c1f1SSteffen Klassert 
9469b42c1f1SSteffen Klassert 	if (m->v | m->m) {
9479b42c1f1SSteffen Klassert 		ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
9489b42c1f1SSteffen Klassert 		if (!ret)
9499b42c1f1SSteffen Klassert 			ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
9509b42c1f1SSteffen Klassert 	}
9519b42c1f1SSteffen Klassert 	return ret;
9529b42c1f1SSteffen Klassert }
9539b42c1f1SSteffen Klassert 
95468325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
95568325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
95668325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
95768325d3bSHerbert Xu 				    struct sk_buff *skb)
9581da177e4SLinus Torvalds {
9591d1e34ddSDavid S. Miller 	int ret = 0;
9601d1e34ddSDavid S. Miller 
9611da177e4SLinus Torvalds 	copy_to_user_state(x, p);
9621da177e4SLinus Torvalds 
963a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
964a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
965a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
966a947b0a9SNicolas Dichtel 		if (ret)
967a947b0a9SNicolas Dichtel 			goto out;
968a947b0a9SNicolas Dichtel 	}
969a947b0a9SNicolas Dichtel 
9701d1e34ddSDavid S. Miller 	if (x->coaddr) {
9711d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
9721d1e34ddSDavid S. Miller 		if (ret)
9731d1e34ddSDavid S. Miller 			goto out;
9741d1e34ddSDavid S. Miller 	}
9751d1e34ddSDavid S. Miller 	if (x->lastused) {
976de95c4a4SNicolas Dichtel 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
977de95c4a4SNicolas Dichtel 					XFRMA_PAD);
9781d1e34ddSDavid S. Miller 		if (ret)
9791d1e34ddSDavid S. Miller 			goto out;
9801d1e34ddSDavid S. Miller 	}
9811d1e34ddSDavid S. Miller 	if (x->aead) {
982c7a5899eSAntony Antony 		ret = copy_to_user_aead(x->aead, skb);
9831d1e34ddSDavid S. Miller 		if (ret)
9841d1e34ddSDavid S. Miller 			goto out;
9851d1e34ddSDavid S. Miller 	}
9861d1e34ddSDavid S. Miller 	if (x->aalg) {
9871d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
9881d1e34ddSDavid S. Miller 		if (ret)
9891d1e34ddSDavid S. Miller 			goto out;
9901d1e34ddSDavid S. Miller 	}
9911d1e34ddSDavid S. Miller 	if (x->ealg) {
992c7a5899eSAntony Antony 		ret = copy_to_user_ealg(x->ealg, skb);
9931d1e34ddSDavid S. Miller 		if (ret)
9941d1e34ddSDavid S. Miller 			goto out;
9951d1e34ddSDavid S. Miller 	}
9961d1e34ddSDavid S. Miller 	if (x->calg) {
9971d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
9981d1e34ddSDavid S. Miller 		if (ret)
9991d1e34ddSDavid S. Miller 			goto out;
10001d1e34ddSDavid S. Miller 	}
10011d1e34ddSDavid S. Miller 	if (x->encap) {
10021d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
10031d1e34ddSDavid S. Miller 		if (ret)
10041d1e34ddSDavid S. Miller 			goto out;
10051d1e34ddSDavid S. Miller 	}
10061d1e34ddSDavid S. Miller 	if (x->tfcpad) {
10071d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
10081d1e34ddSDavid S. Miller 		if (ret)
10091d1e34ddSDavid S. Miller 			goto out;
10101d1e34ddSDavid S. Miller 	}
10111d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
10121d1e34ddSDavid S. Miller 	if (ret)
10131d1e34ddSDavid S. Miller 		goto out;
10149b42c1f1SSteffen Klassert 
10159b42c1f1SSteffen Klassert 	ret = xfrm_smark_put(skb, &x->props.smark);
10169b42c1f1SSteffen Klassert 	if (ret)
10179b42c1f1SSteffen Klassert 		goto out;
10189b42c1f1SSteffen Klassert 
1019f293a5e3Sdingzhi 	if (x->replay_esn)
10201d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1021d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
10221d1e34ddSDavid S. Miller 			      x->replay_esn);
1023f293a5e3Sdingzhi 	else
1024f293a5e3Sdingzhi 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1025f293a5e3Sdingzhi 			      &x->replay);
10261d1e34ddSDavid S. Miller 	if (ret)
10271d1e34ddSDavid S. Miller 		goto out;
1028d77e38e6SSteffen Klassert 	if(x->xso.dev)
1029d77e38e6SSteffen Klassert 		ret = copy_user_offload(&x->xso, skb);
1030d77e38e6SSteffen Klassert 	if (ret)
1031d77e38e6SSteffen Klassert 		goto out;
10327e652640SSteffen Klassert 	if (x->if_id) {
10337e652640SSteffen Klassert 		ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1034077fbac4SLorenzo Colitti 		if (ret)
1035077fbac4SLorenzo Colitti 			goto out;
1036077fbac4SLorenzo Colitti 	}
10374e484b3eSAntony Antony 	if (x->security) {
10388598112dSSteffen Klassert 		ret = copy_sec_ctx(x->security, skb);
10394e484b3eSAntony Antony 		if (ret)
10404e484b3eSAntony Antony 			goto out;
10414e484b3eSAntony Antony 	}
10424e484b3eSAntony Antony 	if (x->mapping_maxage)
10434e484b3eSAntony Antony 		ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
10441d1e34ddSDavid S. Miller out:
10451d1e34ddSDavid S. Miller 	return ret;
104668325d3bSHerbert Xu }
104768325d3bSHerbert Xu 
104868325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
104968325d3bSHerbert Xu {
105068325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
105168325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
105268325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
10535f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
105468325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
105568325d3bSHerbert Xu 	struct nlmsghdr *nlh;
105668325d3bSHerbert Xu 	int err;
105768325d3bSHerbert Xu 
105815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
105968325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
106068325d3bSHerbert Xu 	if (nlh == NULL)
106168325d3bSHerbert Xu 		return -EMSGSIZE;
106268325d3bSHerbert Xu 
106368325d3bSHerbert Xu 	p = nlmsg_data(nlh);
106468325d3bSHerbert Xu 
106568325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
10661d1e34ddSDavid S. Miller 	if (err) {
10679825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
106868325d3bSHerbert Xu 		return err;
10691da177e4SLinus Torvalds 	}
10701d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
10715f3eea6bSDmitry Safonov 
10725f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
10735f3eea6bSDmitry Safonov 	if (xtr) {
10745f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlh);
10755f3eea6bSDmitry Safonov 
10765f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
10775f3eea6bSDmitry Safonov 		if (err) {
10785f3eea6bSDmitry Safonov 			nlmsg_cancel(skb, nlh);
10795f3eea6bSDmitry Safonov 			return err;
10805f3eea6bSDmitry Safonov 		}
10815f3eea6bSDmitry Safonov 	}
10825f3eea6bSDmitry Safonov 
10831d1e34ddSDavid S. Miller 	return 0;
10841d1e34ddSDavid S. Miller }
10851da177e4SLinus Torvalds 
10864c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
10874c563f76STimo Teras {
10884c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1089283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
1090283bc9f3SFan Du 	struct net *net = sock_net(sk);
1091283bc9f3SFan Du 
10921ba5bf99SVegard Nossum 	if (cb->args[0])
1093283bc9f3SFan Du 		xfrm_state_walk_done(walk, net);
10944c563f76STimo Teras 	return 0;
10954c563f76STimo Teras }
10964c563f76STimo Teras 
10971da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
10981da177e4SLinus Torvalds {
1099fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
11004c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
11011da177e4SLinus Torvalds 	struct xfrm_dump_info info;
11021da177e4SLinus Torvalds 
11034c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
11044c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
11054c563f76STimo Teras 
11061da177e4SLinus Torvalds 	info.in_skb = cb->skb;
11071da177e4SLinus Torvalds 	info.out_skb = skb;
11081da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
11091da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
11104c563f76STimo Teras 
11114c563f76STimo Teras 	if (!cb->args[0]) {
1112d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
1113870a2df4SNicolas Dichtel 		struct xfrm_address_filter *filter = NULL;
1114d3623099SNicolas Dichtel 		u8 proto = 0;
1115d3623099SNicolas Dichtel 		int err;
1116d3623099SNicolas Dichtel 
11178cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
11188cb08174SJohannes Berg 					     xfrma_policy, cb->extack);
1119d3623099SNicolas Dichtel 		if (err < 0)
1120d3623099SNicolas Dichtel 			return err;
1121d3623099SNicolas Dichtel 
1122870a2df4SNicolas Dichtel 		if (attrs[XFRMA_ADDRESS_FILTER]) {
1123df367561SAndrzej Hajda 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1124df367561SAndrzej Hajda 					 sizeof(*filter), GFP_KERNEL);
1125d3623099SNicolas Dichtel 			if (filter == NULL)
1126d3623099SNicolas Dichtel 				return -ENOMEM;
1127d3623099SNicolas Dichtel 		}
1128d3623099SNicolas Dichtel 
1129d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
1130d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
1131d3623099SNicolas Dichtel 
1132d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
11331ba5bf99SVegard Nossum 		cb->args[0] = 1;
11344c563f76STimo Teras 	}
11354c563f76STimo Teras 
1136fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	return skb->len;
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
11421da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
11431da177e4SLinus Torvalds {
11441da177e4SLinus Torvalds 	struct xfrm_dump_info info;
11451da177e4SLinus Torvalds 	struct sk_buff *skb;
1146864745d2SMathias Krause 	int err;
11471da177e4SLinus Torvalds 
11487deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
11491da177e4SLinus Torvalds 	if (!skb)
11501da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	info.in_skb = in_skb;
11531da177e4SLinus Torvalds 	info.out_skb = skb;
11541da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
11551da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
11561da177e4SLinus Torvalds 
1157864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
1158864745d2SMathias Krause 	if (err) {
11591da177e4SLinus Torvalds 		kfree_skb(skb);
1160864745d2SMathias Krause 		return ERR_PTR(err);
11611da177e4SLinus Torvalds 	}
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	return skb;
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
116621ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
116721ee543eSMichal Kubecek  * Must be called with RCU read lock.
116821ee543eSMichal Kubecek  */
116921ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
117021ee543eSMichal Kubecek 				       u32 pid, unsigned int group)
117121ee543eSMichal Kubecek {
117221ee543eSMichal Kubecek 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
11735461fc0cSDmitry Safonov 	struct xfrm_translator *xtr;
117421ee543eSMichal Kubecek 
117586126b77SFlorian Westphal 	if (!nlsk) {
117686126b77SFlorian Westphal 		kfree_skb(skb);
117786126b77SFlorian Westphal 		return -EPIPE;
117886126b77SFlorian Westphal 	}
117986126b77SFlorian Westphal 
11805461fc0cSDmitry Safonov 	xtr = xfrm_get_translator();
11815461fc0cSDmitry Safonov 	if (xtr) {
11825461fc0cSDmitry Safonov 		int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
11835461fc0cSDmitry Safonov 
11845461fc0cSDmitry Safonov 		xfrm_put_translator(xtr);
11855461fc0cSDmitry Safonov 		if (err) {
11865461fc0cSDmitry Safonov 			kfree_skb(skb);
11875461fc0cSDmitry Safonov 			return err;
11885461fc0cSDmitry Safonov 		}
11895461fc0cSDmitry Safonov 	}
11905461fc0cSDmitry Safonov 
119121ee543eSMichal Kubecek 	return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
119221ee543eSMichal Kubecek }
119321ee543eSMichal Kubecek 
1194a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void)
11957deb2264SThomas Graf {
11967deb2264SThomas Graf 	return NLMSG_ALIGN(4)
11977deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1198880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1199880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1200880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
12017deb2264SThomas Graf }
12027deb2264SThomas Graf 
1203e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
120415e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
1205ecfd6b18SJamal Hadi Salim {
12065a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
12075a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
12085a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1209880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1210ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
12111d1e34ddSDavid S. Miller 	int err;
1212ecfd6b18SJamal Hadi Salim 	u32 *f;
1213880a6fabSChristophe Gouault 	unsigned lseq;
1214ecfd6b18SJamal Hadi Salim 
121515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
121625985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1217ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1218ecfd6b18SJamal Hadi Salim 
1219ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1220ecfd6b18SJamal Hadi Salim 	*f = flags;
1221e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
12225a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
12235a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
12245a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
12255a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
12265a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
12275a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
12285a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
12295a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1230ecfd6b18SJamal Hadi Salim 
1231880a6fabSChristophe Gouault 	do {
1232880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1233880a6fabSChristophe Gouault 
1234880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1235880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1236880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1237880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1238880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1239880a6fabSChristophe Gouault 
12401d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
12411d1e34ddSDavid S. Miller 	if (!err)
12421d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1243880a6fabSChristophe Gouault 	if (!err)
1244880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1245880a6fabSChristophe Gouault 	if (!err)
1246880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
12471d1e34ddSDavid S. Miller 	if (err) {
12481d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
12491d1e34ddSDavid S. Miller 		return err;
12501d1e34ddSDavid S. Miller 	}
1251ecfd6b18SJamal Hadi Salim 
1252053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1253053c095aSJohannes Berg 	return 0;
1254ecfd6b18SJamal Hadi Salim }
1255ecfd6b18SJamal Hadi Salim 
1256880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1257880a6fabSChristophe Gouault 			    struct nlattr **attrs)
1258880a6fabSChristophe Gouault {
1259880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1260880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1261880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1262880a6fabSChristophe Gouault 
1263880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1264880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1265880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1266880a6fabSChristophe Gouault 
1267880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1268880a6fabSChristophe Gouault 			return -EINVAL;
1269880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1270880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1271880a6fabSChristophe Gouault 			return -EINVAL;
1272880a6fabSChristophe Gouault 	}
1273880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1274880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1275880a6fabSChristophe Gouault 
1276880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1277880a6fabSChristophe Gouault 			return -EINVAL;
1278880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1279880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1280880a6fabSChristophe Gouault 			return -EINVAL;
1281880a6fabSChristophe Gouault 	}
1282880a6fabSChristophe Gouault 
1283880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1284880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1285880a6fabSChristophe Gouault 		if (thresh4) {
1286880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1287880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1288880a6fabSChristophe Gouault 		}
1289880a6fabSChristophe Gouault 		if (thresh6) {
1290880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1291880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1292880a6fabSChristophe Gouault 		}
1293880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1294880a6fabSChristophe Gouault 
1295880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1296880a6fabSChristophe Gouault 	}
1297880a6fabSChristophe Gouault 
1298880a6fabSChristophe Gouault 	return 0;
1299880a6fabSChristophe Gouault }
1300880a6fabSChristophe Gouault 
1301ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
13025424f32eSThomas Graf 		struct nlattr **attrs)
1303ecfd6b18SJamal Hadi Salim {
1304a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1305ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
13067b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
130715e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1308ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
13092fc5f83bSGustavo A. R. Silva 	int err;
1310ecfd6b18SJamal Hadi Salim 
13117deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1312ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1313ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1314ecfd6b18SJamal Hadi Salim 
13152fc5f83bSGustavo A. R. Silva 	err = build_spdinfo(r_skb, net, sportid, seq, *flags);
13162fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
1317ecfd6b18SJamal Hadi Salim 
131815e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1319ecfd6b18SJamal Hadi Salim }
1320ecfd6b18SJamal Hadi Salim 
1321a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void)
13227deb2264SThomas Graf {
13237deb2264SThomas Graf 	return NLMSG_ALIGN(4)
13247deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
13257deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
13267deb2264SThomas Graf }
13277deb2264SThomas Graf 
1328e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
132915e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
133028d8909bSJamal Hadi Salim {
1331af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1332af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
133328d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
13341d1e34ddSDavid S. Miller 	int err;
133528d8909bSJamal Hadi Salim 	u32 *f;
133628d8909bSJamal Hadi Salim 
133715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
133825985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
133928d8909bSJamal Hadi Salim 		return -EMSGSIZE;
134028d8909bSJamal Hadi Salim 
134128d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
134228d8909bSJamal Hadi Salim 	*f = flags;
1343e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
134428d8909bSJamal Hadi Salim 
1345af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1346af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1347af11e316SJamal Hadi Salim 
13481d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
13491d1e34ddSDavid S. Miller 	if (!err)
13501d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
13511d1e34ddSDavid S. Miller 	if (err) {
13521d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
13531d1e34ddSDavid S. Miller 		return err;
13541d1e34ddSDavid S. Miller 	}
135528d8909bSJamal Hadi Salim 
1356053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1357053c095aSJohannes Berg 	return 0;
135828d8909bSJamal Hadi Salim }
135928d8909bSJamal Hadi Salim 
136028d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
13615424f32eSThomas Graf 		struct nlattr **attrs)
136228d8909bSJamal Hadi Salim {
1363a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
136428d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
13657b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
136615e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
136728d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
13682fc5f83bSGustavo A. R. Silva 	int err;
136928d8909bSJamal Hadi Salim 
13707deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
137128d8909bSJamal Hadi Salim 	if (r_skb == NULL)
137228d8909bSJamal Hadi Salim 		return -ENOMEM;
137328d8909bSJamal Hadi Salim 
13742fc5f83bSGustavo A. R. Silva 	err = build_sadinfo(r_skb, net, sportid, seq, *flags);
13752fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
137628d8909bSJamal Hadi Salim 
137715e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
137828d8909bSJamal Hadi Salim }
137928d8909bSJamal Hadi Salim 
138022e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
13815424f32eSThomas Graf 		struct nlattr **attrs)
13821da177e4SLinus Torvalds {
1383fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
13847b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
13851da177e4SLinus Torvalds 	struct xfrm_state *x;
13861da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1387eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
13881da177e4SLinus Torvalds 
1389fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
13901da177e4SLinus Torvalds 	if (x == NULL)
13911da177e4SLinus Torvalds 		goto out_noput;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
13941da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
13951da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
13961da177e4SLinus Torvalds 	} else {
139715e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
13981da177e4SLinus Torvalds 	}
13991da177e4SLinus Torvalds 	xfrm_state_put(x);
14001da177e4SLinus Torvalds out_noput:
14011da177e4SLinus Torvalds 	return err;
14021da177e4SLinus Torvalds }
14031da177e4SLinus Torvalds 
140422e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
14055424f32eSThomas Graf 		struct nlattr **attrs)
14061da177e4SLinus Torvalds {
1407fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
14081da177e4SLinus Torvalds 	struct xfrm_state *x;
14091da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
14105f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
14111da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
14121da177e4SLinus Torvalds 	xfrm_address_t *daddr;
14131da177e4SLinus Torvalds 	int family;
14141da177e4SLinus Torvalds 	int err;
14156f26b61eSJamal Hadi Salim 	u32 mark;
14166f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
14177e652640SSteffen Klassert 	u32 if_id = 0;
14181da177e4SLinus Torvalds 
14197b67c857SThomas Graf 	p = nlmsg_data(nlh);
1420776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
14211da177e4SLinus Torvalds 	if (err)
14221da177e4SLinus Torvalds 		goto out_noput;
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 	family = p->info.family;
14251da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	x = NULL;
14286f26b61eSJamal Hadi Salim 
14296f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
14307e652640SSteffen Klassert 
1431a3d9001bSKai Lueke 	if (attrs[XFRMA_IF_ID])
14327e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
14337e652640SSteffen Klassert 
14341da177e4SLinus Torvalds 	if (p->info.seq) {
14356f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
143670e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
14371da177e4SLinus Torvalds 			xfrm_state_put(x);
14381da177e4SLinus Torvalds 			x = NULL;
14391da177e4SLinus Torvalds 		}
14401da177e4SLinus Torvalds 	}
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	if (!x)
14436f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
14447e652640SSteffen Klassert 				  if_id, p->info.id.proto, daddr,
14451da177e4SLinus Torvalds 				  &p->info.saddr, 1,
14461da177e4SLinus Torvalds 				  family);
14471da177e4SLinus Torvalds 	err = -ENOENT;
14481da177e4SLinus Torvalds 	if (x == NULL)
14491da177e4SLinus Torvalds 		goto out_noput;
14501da177e4SLinus Torvalds 
1451658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1452658b219eSHerbert Xu 	if (err)
1453658b219eSHerbert Xu 		goto out;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
14561da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
14571da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
14581da177e4SLinus Torvalds 		goto out;
14591da177e4SLinus Torvalds 	}
14601da177e4SLinus Torvalds 
14615f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
14625f3eea6bSDmitry Safonov 	if (xtr) {
14635f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
14645f3eea6bSDmitry Safonov 
14655f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
14665f3eea6bSDmitry Safonov 		if (err) {
14675f3eea6bSDmitry Safonov 			kfree_skb(resp_skb);
14685f3eea6bSDmitry Safonov 			goto out;
14695f3eea6bSDmitry Safonov 		}
14705f3eea6bSDmitry Safonov 	}
14715f3eea6bSDmitry Safonov 
147215e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds out:
14751da177e4SLinus Torvalds 	xfrm_state_put(x);
14761da177e4SLinus Torvalds out_noput:
14771da177e4SLinus Torvalds 	return err;
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
1480b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
14811da177e4SLinus Torvalds {
14821da177e4SLinus Torvalds 	switch (dir) {
14831da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
14841da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
14851da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
14861da177e4SLinus Torvalds 		break;
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds 	default:
14891da177e4SLinus Torvalds 		return -EINVAL;
14903ff50b79SStephen Hemminger 	}
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	return 0;
14931da177e4SLinus Torvalds }
14941da177e4SLinus Torvalds 
1495b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1496f7b6983fSMasahide NAKAMURA {
1497f7b6983fSMasahide NAKAMURA 	switch (type) {
1498f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1499f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1500f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1501f7b6983fSMasahide NAKAMURA #endif
1502f7b6983fSMasahide NAKAMURA 		break;
1503f7b6983fSMasahide NAKAMURA 
1504f7b6983fSMasahide NAKAMURA 	default:
1505f7b6983fSMasahide NAKAMURA 		return -EINVAL;
15063ff50b79SStephen Hemminger 	}
1507f7b6983fSMasahide NAKAMURA 
1508f7b6983fSMasahide NAKAMURA 	return 0;
1509f7b6983fSMasahide NAKAMURA }
1510f7b6983fSMasahide NAKAMURA 
15111da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
15121da177e4SLinus Torvalds {
1513e682adf0SFan Du 	int ret;
1514e682adf0SFan Du 
15151da177e4SLinus Torvalds 	switch (p->share) {
15161da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
15171da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
15181da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
15191da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
15201da177e4SLinus Torvalds 		break;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	default:
15231da177e4SLinus Torvalds 		return -EINVAL;
15243ff50b79SStephen Hemminger 	}
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 	switch (p->action) {
15271da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
15281da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
15291da177e4SLinus Torvalds 		break;
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	default:
15321da177e4SLinus Torvalds 		return -EINVAL;
15333ff50b79SStephen Hemminger 	}
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	switch (p->sel.family) {
15361da177e4SLinus Torvalds 	case AF_INET:
153707bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
153807bf7908SSteffen Klassert 			return -EINVAL;
153907bf7908SSteffen Klassert 
15401da177e4SLinus Torvalds 		break;
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	case AF_INET6:
1543dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
154407bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
154507bf7908SSteffen Klassert 			return -EINVAL;
154607bf7908SSteffen Klassert 
15471da177e4SLinus Torvalds 		break;
15481da177e4SLinus Torvalds #else
15491da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
15501da177e4SLinus Torvalds #endif
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	default:
15531da177e4SLinus Torvalds 		return -EINVAL;
15543ff50b79SStephen Hemminger 	}
15551da177e4SLinus Torvalds 
1556e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1557e682adf0SFan Du 	if (ret)
1558e682adf0SFan Du 		return ret;
1559b805d78dSYueHaibing 	if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1560e682adf0SFan Du 		return -EINVAL;
1561e682adf0SFan Du 
1562e682adf0SFan Du 	return 0;
15631da177e4SLinus Torvalds }
15641da177e4SLinus Torvalds 
15655424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1566df71837dSTrent Jaeger {
15675424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1568df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1569df71837dSTrent Jaeger 
1570df71837dSTrent Jaeger 	if (!rt)
1571df71837dSTrent Jaeger 		return 0;
1572df71837dSTrent Jaeger 
15735424f32eSThomas Graf 	uctx = nla_data(rt);
157452a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1575df71837dSTrent Jaeger }
1576df71837dSTrent Jaeger 
15771da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
15781da177e4SLinus Torvalds 			   int nr)
15791da177e4SLinus Torvalds {
15801da177e4SLinus Torvalds 	int i;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
15831da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
15841da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
15871da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
15881da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
15891da177e4SLinus Torvalds 		t->reqid = ut->reqid;
15901da177e4SLinus Torvalds 		t->mode = ut->mode;
15911da177e4SLinus Torvalds 		t->share = ut->share;
15921da177e4SLinus Torvalds 		t->optional = ut->optional;
15931da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
15941da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
15951da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1596c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1597c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
15988511d01dSMiika Komu 		t->encap_family = ut->family;
15991da177e4SLinus Torvalds 	}
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds 
1602b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1603b4ad86bfSDavid S. Miller {
1604732706afSSteffen Klassert 	u16 prev_family;
1605b4ad86bfSDavid S. Miller 	int i;
1606b4ad86bfSDavid S. Miller 
1607b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1608b4ad86bfSDavid S. Miller 		return -EINVAL;
1609b4ad86bfSDavid S. Miller 
1610732706afSSteffen Klassert 	prev_family = family;
1611732706afSSteffen Klassert 
1612b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1613b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1614b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1615b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1616b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1617b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1618b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1619b4ad86bfSDavid S. Miller 		 */
1620b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1621b4ad86bfSDavid S. Miller 			ut[i].family = family;
1622b4ad86bfSDavid S. Miller 
162335e61038SFlorian Westphal 		switch (ut[i].mode) {
162435e61038SFlorian Westphal 		case XFRM_MODE_TUNNEL:
162535e61038SFlorian Westphal 		case XFRM_MODE_BEET:
162635e61038SFlorian Westphal 			break;
162735e61038SFlorian Westphal 		default:
162835e61038SFlorian Westphal 			if (ut[i].family != prev_family)
1629732706afSSteffen Klassert 				return -EINVAL;
163035e61038SFlorian Westphal 			break;
163135e61038SFlorian Westphal 		}
163232bf94fbSSean Tranchetti 		if (ut[i].mode >= XFRM_MODE_MAX)
163332bf94fbSSean Tranchetti 			return -EINVAL;
163432bf94fbSSean Tranchetti 
1635732706afSSteffen Klassert 		prev_family = ut[i].family;
1636732706afSSteffen Klassert 
1637b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1638b4ad86bfSDavid S. Miller 		case AF_INET:
1639b4ad86bfSDavid S. Miller 			break;
1640dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1641b4ad86bfSDavid S. Miller 		case AF_INET6:
1642b4ad86bfSDavid S. Miller 			break;
1643b4ad86bfSDavid S. Miller #endif
1644b4ad86bfSDavid S. Miller 		default:
1645b4ad86bfSDavid S. Miller 			return -EINVAL;
16463ff50b79SStephen Hemminger 		}
16476a53b759SCong Wang 
1648dbb2483bSCong Wang 		if (!xfrm_id_proto_valid(ut[i].id.proto))
16496a53b759SCong Wang 			return -EINVAL;
16506a53b759SCong Wang 	}
16516a53b759SCong Wang 
1652b4ad86bfSDavid S. Miller 	return 0;
1653b4ad86bfSDavid S. Miller }
1654b4ad86bfSDavid S. Miller 
16555424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
16561da177e4SLinus Torvalds {
16575424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 	if (!rt) {
16601da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
16611da177e4SLinus Torvalds 	} else {
16625424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
16635424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1664b4ad86bfSDavid S. Miller 		int err;
16651da177e4SLinus Torvalds 
1666b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1667b4ad86bfSDavid S. Miller 		if (err)
1668b4ad86bfSDavid S. Miller 			return err;
16691da177e4SLinus Torvalds 
16705424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
16711da177e4SLinus Torvalds 	}
16721da177e4SLinus Torvalds 	return 0;
16731da177e4SLinus Torvalds }
16741da177e4SLinus Torvalds 
16755424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1676f7b6983fSMasahide NAKAMURA {
16775424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1678f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1679b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1680f7b6983fSMasahide NAKAMURA 	int err;
1681f7b6983fSMasahide NAKAMURA 
1682f7b6983fSMasahide NAKAMURA 	if (rt) {
16835424f32eSThomas Graf 		upt = nla_data(rt);
1684f7b6983fSMasahide NAKAMURA 		type = upt->type;
1685f7b6983fSMasahide NAKAMURA 	}
1686f7b6983fSMasahide NAKAMURA 
1687f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1688f7b6983fSMasahide NAKAMURA 	if (err)
1689f7b6983fSMasahide NAKAMURA 		return err;
1690f7b6983fSMasahide NAKAMURA 
1691f7b6983fSMasahide NAKAMURA 	*tp = type;
1692f7b6983fSMasahide NAKAMURA 	return 0;
1693f7b6983fSMasahide NAKAMURA }
1694f7b6983fSMasahide NAKAMURA 
16951da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
16961da177e4SLinus Torvalds {
16971da177e4SLinus Torvalds 	xp->priority = p->priority;
16981da177e4SLinus Torvalds 	xp->index = p->index;
16991da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
17001da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
17011da177e4SLinus Torvalds 	xp->action = p->action;
17021da177e4SLinus Torvalds 	xp->flags = p->flags;
17031da177e4SLinus Torvalds 	xp->family = p->sel.family;
17041da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
17051da177e4SLinus Torvalds }
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
17081da177e4SLinus Torvalds {
17097b789836SMathias Krause 	memset(p, 0, sizeof(*p));
17101da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
17111da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
17121da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
17131da177e4SLinus Torvalds 	p->priority = xp->priority;
17141da177e4SLinus Torvalds 	p->index = xp->index;
17151da177e4SLinus Torvalds 	p->sel.family = xp->family;
17161da177e4SLinus Torvalds 	p->dir = dir;
17171da177e4SLinus Torvalds 	p->action = xp->action;
17181da177e4SLinus Torvalds 	p->flags = xp->flags;
17191da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
1722fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
17231da177e4SLinus Torvalds {
1724fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
17251da177e4SLinus Torvalds 	int err;
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds 	if (!xp) {
17281da177e4SLinus Torvalds 		*errp = -ENOMEM;
17291da177e4SLinus Torvalds 		return NULL;
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1733df71837dSTrent Jaeger 
173435a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1735f7b6983fSMasahide NAKAMURA 	if (err)
1736f7b6983fSMasahide NAKAMURA 		goto error;
1737f7b6983fSMasahide NAKAMURA 
173835a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
173935a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1740f7b6983fSMasahide NAKAMURA 	if (err)
1741f7b6983fSMasahide NAKAMURA 		goto error;
17421da177e4SLinus Torvalds 
1743295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1744295fae56SJamal Hadi Salim 
1745a3d9001bSKai Lueke 	if (attrs[XFRMA_IF_ID])
17467e652640SSteffen Klassert 		xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
17477e652640SSteffen Klassert 
17481da177e4SLinus Torvalds 	return xp;
1749f7b6983fSMasahide NAKAMURA  error:
1750f7b6983fSMasahide NAKAMURA 	*errp = err;
175112a169e7SHerbert Xu 	xp->walk.dead = 1;
175264c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1753f7b6983fSMasahide NAKAMURA 	return NULL;
17541da177e4SLinus Torvalds }
17551da177e4SLinus Torvalds 
175622e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
17575424f32eSThomas Graf 		struct nlattr **attrs)
17581da177e4SLinus Torvalds {
1759fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
17607b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
17611da177e4SLinus Torvalds 	struct xfrm_policy *xp;
176226b15dadSJamal Hadi Salim 	struct km_event c;
17631da177e4SLinus Torvalds 	int err;
17641da177e4SLinus Torvalds 	int excl;
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
17671da177e4SLinus Torvalds 	if (err)
17681da177e4SLinus Torvalds 		return err;
176935a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1770df71837dSTrent Jaeger 	if (err)
1771df71837dSTrent Jaeger 		return err;
17721da177e4SLinus Torvalds 
1773fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
17741da177e4SLinus Torvalds 	if (!xp)
17751da177e4SLinus Torvalds 		return err;
17761da177e4SLinus Torvalds 
177725985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
177826b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
1779a7fd0e6dSBhaskar Chowdhury 	 * in netlink excl is a flag and you wouldn't need
178026b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
17811da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
17821da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
17832e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1784161a09e7SJoy Latten 
17851da177e4SLinus Torvalds 	if (err) {
178603e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
17871da177e4SLinus Torvalds 		kfree(xp);
17881da177e4SLinus Torvalds 		return err;
17891da177e4SLinus Torvalds 	}
17901da177e4SLinus Torvalds 
1791f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
179226b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
179315e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
179426b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
179526b15dadSJamal Hadi Salim 
17961da177e4SLinus Torvalds 	xfrm_pol_put(xp);
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	return 0;
17991da177e4SLinus Torvalds }
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
18021da177e4SLinus Torvalds {
18031da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
18041da177e4SLinus Torvalds 	int i;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
18071da177e4SLinus Torvalds 		return 0;
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
18101da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
18111da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
18121da177e4SLinus Torvalds 
18131f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
18141da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
18158511d01dSMiika Komu 		up->family = kp->encap_family;
18161da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
18171da177e4SLinus Torvalds 		up->reqid = kp->reqid;
18181da177e4SLinus Torvalds 		up->mode = kp->mode;
18191da177e4SLinus Torvalds 		up->share = kp->share;
18201da177e4SLinus Torvalds 		up->optional = kp->optional;
18211da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
18221da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
18231da177e4SLinus Torvalds 		up->calgos = kp->calgos;
18241da177e4SLinus Torvalds 	}
18251da177e4SLinus Torvalds 
1826c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1827c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1828df71837dSTrent Jaeger }
1829df71837dSTrent Jaeger 
18300d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
18310d681623SSerge Hallyn {
18320d681623SSerge Hallyn 	if (x->security) {
18330d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
18340d681623SSerge Hallyn 	}
18350d681623SSerge Hallyn 	return 0;
18360d681623SSerge Hallyn }
18370d681623SSerge Hallyn 
18380d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
18390d681623SSerge Hallyn {
18401d1e34ddSDavid S. Miller 	if (xp->security)
18410d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
18420d681623SSerge Hallyn 	return 0;
18430d681623SSerge Hallyn }
1844a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void)
1845cfbfd45aSThomas Graf {
1846cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1847cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1848cfbfd45aSThomas Graf #else
1849cfbfd45aSThomas Graf 	return 0;
1850cfbfd45aSThomas Graf #endif
1851cfbfd45aSThomas Graf }
18520d681623SSerge Hallyn 
1853f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1854b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1855f7b6983fSMasahide NAKAMURA {
185645c180bcSEric Dumazet 	struct xfrm_userpolicy_type upt;
185745c180bcSEric Dumazet 
185845c180bcSEric Dumazet 	/* Sadly there are two holes in struct xfrm_userpolicy_type */
185945c180bcSEric Dumazet 	memset(&upt, 0, sizeof(upt));
186045c180bcSEric Dumazet 	upt.type = type;
1861f7b6983fSMasahide NAKAMURA 
1862c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1863f7b6983fSMasahide NAKAMURA }
1864f7b6983fSMasahide NAKAMURA 
1865f7b6983fSMasahide NAKAMURA #else
1866b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1867f7b6983fSMasahide NAKAMURA {
1868f7b6983fSMasahide NAKAMURA 	return 0;
1869f7b6983fSMasahide NAKAMURA }
1870f7b6983fSMasahide NAKAMURA #endif
1871f7b6983fSMasahide NAKAMURA 
18721da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
18731da177e4SLinus Torvalds {
18741da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
18751da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
18761da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
18771da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
18785f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
18791da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
18801d1e34ddSDavid S. Miller 	int err;
18811da177e4SLinus Torvalds 
188215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
188379b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
188479b8b7f4SThomas Graf 	if (nlh == NULL)
188579b8b7f4SThomas Graf 		return -EMSGSIZE;
18861da177e4SLinus Torvalds 
18877b67c857SThomas Graf 	p = nlmsg_data(nlh);
18881da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
18891d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
18901d1e34ddSDavid S. Miller 	if (!err)
18911d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
18921d1e34ddSDavid S. Miller 	if (!err)
18931d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
18941d1e34ddSDavid S. Miller 	if (!err)
18951d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
18967e652640SSteffen Klassert 	if (!err)
18977e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
18981d1e34ddSDavid S. Miller 	if (err) {
18991d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
19001d1e34ddSDavid S. Miller 		return err;
19011d1e34ddSDavid S. Miller 	}
19029825069dSThomas Graf 	nlmsg_end(skb, nlh);
19035f3eea6bSDmitry Safonov 
19045f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
19055f3eea6bSDmitry Safonov 	if (xtr) {
19065f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlh);
19075f3eea6bSDmitry Safonov 
19085f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
19095f3eea6bSDmitry Safonov 		if (err) {
19105f3eea6bSDmitry Safonov 			nlmsg_cancel(skb, nlh);
19115f3eea6bSDmitry Safonov 			return err;
19125f3eea6bSDmitry Safonov 		}
19135f3eea6bSDmitry Safonov 	}
19145f3eea6bSDmitry Safonov 
19151da177e4SLinus Torvalds 	return 0;
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
19184c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
19194c563f76STimo Teras {
19201137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1921283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
19224c563f76STimo Teras 
1923283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
19244c563f76STimo Teras 	return 0;
19254c563f76STimo Teras }
19264c563f76STimo Teras 
19271137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb)
19281137b5e2SHerbert Xu {
19291137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
19301137b5e2SHerbert Xu 
19311137b5e2SHerbert Xu 	BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
19321137b5e2SHerbert Xu 
19331137b5e2SHerbert Xu 	xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
19341137b5e2SHerbert Xu 	return 0;
19351137b5e2SHerbert Xu }
19361137b5e2SHerbert Xu 
19371da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
19381da177e4SLinus Torvalds {
1939fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
19401137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
19411da177e4SLinus Torvalds 	struct xfrm_dump_info info;
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	info.in_skb = cb->skb;
19441da177e4SLinus Torvalds 	info.out_skb = skb;
19451da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
19461da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
19474c563f76STimo Teras 
1948fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	return skb->len;
19511da177e4SLinus Torvalds }
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
19541da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
19551da177e4SLinus Torvalds 					  int dir, u32 seq)
19561da177e4SLinus Torvalds {
19571da177e4SLinus Torvalds 	struct xfrm_dump_info info;
19581da177e4SLinus Torvalds 	struct sk_buff *skb;
1959c2546372SMathias Krause 	int err;
19601da177e4SLinus Torvalds 
19617deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19621da177e4SLinus Torvalds 	if (!skb)
19631da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	info.in_skb = in_skb;
19661da177e4SLinus Torvalds 	info.out_skb = skb;
19671da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
19681da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
19691da177e4SLinus Torvalds 
1970c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1971c2546372SMathias Krause 	if (err) {
19721da177e4SLinus Torvalds 		kfree_skb(skb);
1973c2546372SMathias Krause 		return ERR_PTR(err);
19741da177e4SLinus Torvalds 	}
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 	return skb;
19771da177e4SLinus Torvalds }
19781da177e4SLinus Torvalds 
197988d0adb5SNicolas Dichtel static int xfrm_notify_userpolicy(struct net *net)
198088d0adb5SNicolas Dichtel {
198188d0adb5SNicolas Dichtel 	struct xfrm_userpolicy_default *up;
198288d0adb5SNicolas Dichtel 	int len = NLMSG_ALIGN(sizeof(*up));
198388d0adb5SNicolas Dichtel 	struct nlmsghdr *nlh;
198488d0adb5SNicolas Dichtel 	struct sk_buff *skb;
198593ec1320SNicolas Dichtel 	int err;
198688d0adb5SNicolas Dichtel 
198788d0adb5SNicolas Dichtel 	skb = nlmsg_new(len, GFP_ATOMIC);
198888d0adb5SNicolas Dichtel 	if (skb == NULL)
198988d0adb5SNicolas Dichtel 		return -ENOMEM;
199088d0adb5SNicolas Dichtel 
199188d0adb5SNicolas Dichtel 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
199288d0adb5SNicolas Dichtel 	if (nlh == NULL) {
199388d0adb5SNicolas Dichtel 		kfree_skb(skb);
199488d0adb5SNicolas Dichtel 		return -EMSGSIZE;
199588d0adb5SNicolas Dichtel 	}
199688d0adb5SNicolas Dichtel 
199788d0adb5SNicolas Dichtel 	up = nlmsg_data(nlh);
1998b58b1f56SNicolas Dichtel 	up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
1999b58b1f56SNicolas Dichtel 	up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2000b58b1f56SNicolas Dichtel 	up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
200188d0adb5SNicolas Dichtel 
200288d0adb5SNicolas Dichtel 	nlmsg_end(skb, nlh);
200388d0adb5SNicolas Dichtel 
200493ec1320SNicolas Dichtel 	rcu_read_lock();
200593ec1320SNicolas Dichtel 	err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
200693ec1320SNicolas Dichtel 	rcu_read_unlock();
200793ec1320SNicolas Dichtel 
200893ec1320SNicolas Dichtel 	return err;
200988d0adb5SNicolas Dichtel }
201088d0adb5SNicolas Dichtel 
2011b58b1f56SNicolas Dichtel static bool xfrm_userpolicy_is_valid(__u8 policy)
2012b58b1f56SNicolas Dichtel {
2013b58b1f56SNicolas Dichtel 	return policy == XFRM_USERPOLICY_BLOCK ||
2014b58b1f56SNicolas Dichtel 	       policy == XFRM_USERPOLICY_ACCEPT;
2015b58b1f56SNicolas Dichtel }
2016b58b1f56SNicolas Dichtel 
20172d151d39SSteffen Klassert static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
20182d151d39SSteffen Klassert 			    struct nlattr **attrs)
20192d151d39SSteffen Klassert {
20202d151d39SSteffen Klassert 	struct net *net = sock_net(skb->sk);
20212d151d39SSteffen Klassert 	struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
20222d151d39SSteffen Klassert 
2023b58b1f56SNicolas Dichtel 	if (xfrm_userpolicy_is_valid(up->in))
2024b58b1f56SNicolas Dichtel 		net->xfrm.policy_default[XFRM_POLICY_IN] = up->in;
20255d8dbb7fSPavel Skripkin 
2026b58b1f56SNicolas Dichtel 	if (xfrm_userpolicy_is_valid(up->fwd))
2027b58b1f56SNicolas Dichtel 		net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd;
20285d8dbb7fSPavel Skripkin 
2029b58b1f56SNicolas Dichtel 	if (xfrm_userpolicy_is_valid(up->out))
2030b58b1f56SNicolas Dichtel 		net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out;
20312d151d39SSteffen Klassert 
20322d151d39SSteffen Klassert 	rt_genid_bump_all(net);
20332d151d39SSteffen Klassert 
203488d0adb5SNicolas Dichtel 	xfrm_notify_userpolicy(net);
20352d151d39SSteffen Klassert 	return 0;
20362d151d39SSteffen Klassert }
20372d151d39SSteffen Klassert 
20382d151d39SSteffen Klassert static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
20392d151d39SSteffen Klassert 			    struct nlattr **attrs)
20402d151d39SSteffen Klassert {
20412d151d39SSteffen Klassert 	struct sk_buff *r_skb;
20422d151d39SSteffen Klassert 	struct nlmsghdr *r_nlh;
20432d151d39SSteffen Klassert 	struct net *net = sock_net(skb->sk);
2044f8d858e6SNicolas Dichtel 	struct xfrm_userpolicy_default *r_up;
20452d151d39SSteffen Klassert 	int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
20462d151d39SSteffen Klassert 	u32 portid = NETLINK_CB(skb).portid;
20472d151d39SSteffen Klassert 	u32 seq = nlh->nlmsg_seq;
20482d151d39SSteffen Klassert 
20492d151d39SSteffen Klassert 	r_skb = nlmsg_new(len, GFP_ATOMIC);
20502d151d39SSteffen Klassert 	if (!r_skb)
20512d151d39SSteffen Klassert 		return -ENOMEM;
20522d151d39SSteffen Klassert 
20532d151d39SSteffen Klassert 	r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
20542d151d39SSteffen Klassert 	if (!r_nlh) {
20552d151d39SSteffen Klassert 		kfree_skb(r_skb);
20562d151d39SSteffen Klassert 		return -EMSGSIZE;
20572d151d39SSteffen Klassert 	}
20582d151d39SSteffen Klassert 
20592d151d39SSteffen Klassert 	r_up = nlmsg_data(r_nlh);
2060b58b1f56SNicolas Dichtel 	r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2061b58b1f56SNicolas Dichtel 	r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2062b58b1f56SNicolas Dichtel 	r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
20632d151d39SSteffen Klassert 	nlmsg_end(r_skb, r_nlh);
20642d151d39SSteffen Klassert 
20652d151d39SSteffen Klassert 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
20662d151d39SSteffen Klassert }
20672d151d39SSteffen Klassert 
206822e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
20695424f32eSThomas Graf 		struct nlattr **attrs)
20701da177e4SLinus Torvalds {
2071fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
20721da177e4SLinus Torvalds 	struct xfrm_policy *xp;
20731da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
2074b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
20751da177e4SLinus Torvalds 	int err;
207626b15dadSJamal Hadi Salim 	struct km_event c;
20771da177e4SLinus Torvalds 	int delete;
2078295fae56SJamal Hadi Salim 	struct xfrm_mark m;
20797e652640SSteffen Klassert 	u32 if_id = 0;
20801da177e4SLinus Torvalds 
20817b67c857SThomas Graf 	p = nlmsg_data(nlh);
20821da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
20831da177e4SLinus Torvalds 
208435a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2085f7b6983fSMasahide NAKAMURA 	if (err)
2086f7b6983fSMasahide NAKAMURA 		return err;
2087f7b6983fSMasahide NAKAMURA 
20881da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
20891da177e4SLinus Torvalds 	if (err)
20901da177e4SLinus Torvalds 		return err;
20911da177e4SLinus Torvalds 
20927e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
20937e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
20947e652640SSteffen Klassert 
20954f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
20964f47e8abSXin Long 
20971da177e4SLinus Torvalds 	if (p->index)
20984f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
20994f47e8abSXin Long 				      p->index, delete, &err);
2100df71837dSTrent Jaeger 	else {
21015424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
210203e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
2103df71837dSTrent Jaeger 
210435a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
2105df71837dSTrent Jaeger 		if (err)
2106df71837dSTrent Jaeger 			return err;
2107df71837dSTrent Jaeger 
21082c8dd116SDenis V. Lunev 		ctx = NULL;
2109df71837dSTrent Jaeger 		if (rt) {
21105424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2111df71837dSTrent Jaeger 
211252a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
211303e1ad7bSPaul Moore 			if (err)
2114df71837dSTrent Jaeger 				return err;
21152c8dd116SDenis V. Lunev 		}
21164f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
21174f47e8abSXin Long 					   &p->sel, ctx, delete, &err);
211803e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
2119df71837dSTrent Jaeger 	}
21201da177e4SLinus Torvalds 	if (xp == NULL)
21211da177e4SLinus Torvalds 		return -ENOENT;
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 	if (!delete) {
21241da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
21251da177e4SLinus Torvalds 
21261da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
21271da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
21281da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
21291da177e4SLinus Torvalds 		} else {
2130a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
213115e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
21321da177e4SLinus Torvalds 		}
213326b15dadSJamal Hadi Salim 	} else {
21342e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
213513fcfbb0SDavid S. Miller 
213613fcfbb0SDavid S. Miller 		if (err != 0)
2137c8c05a8eSCatherine Zhang 			goto out;
213813fcfbb0SDavid S. Miller 
2139e7443892SHerbert Xu 		c.data.byid = p->index;
2140f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
214126b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
214215e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
214326b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
21441da177e4SLinus Torvalds 	}
21451da177e4SLinus Torvalds 
2146c8c05a8eSCatherine Zhang out:
2147ef41aaa0SEric Paris 	xfrm_pol_put(xp);
21481da177e4SLinus Torvalds 	return err;
21491da177e4SLinus Torvalds }
21501da177e4SLinus Torvalds 
215122e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
21525424f32eSThomas Graf 		struct nlattr **attrs)
21531da177e4SLinus Torvalds {
2154fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
215526b15dadSJamal Hadi Salim 	struct km_event c;
21567b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
21574aa2e62cSJoy Latten 	int err;
21581da177e4SLinus Torvalds 
2159f75a2804SCong Wang 	err = xfrm_state_flush(net, p->proto, true, false);
21609e64cc95SJamal Hadi Salim 	if (err) {
21619e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
21629e64cc95SJamal Hadi Salim 			return 0;
2163069c474eSDavid S. Miller 		return err;
21649e64cc95SJamal Hadi Salim 	}
2165bf08867fSHerbert Xu 	c.data.proto = p->proto;
2166f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
216726b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
216815e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
21697067802eSAlexey Dobriyan 	c.net = net;
217026b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
217126b15dadSJamal Hadi Salim 
21721da177e4SLinus Torvalds 	return 0;
21731da177e4SLinus Torvalds }
21741da177e4SLinus Torvalds 
2175a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
21767deb2264SThomas Graf {
2177a1b831f2SAlexey Dobriyan 	unsigned int replay_size = x->replay_esn ?
2178d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
2179d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
2180d8647b79SSteffen Klassert 
21817deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2182d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
2183de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
21846f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
21857deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
21867deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
21877deb2264SThomas Graf }
2188d51d081dSJamal Hadi Salim 
2189214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2190d51d081dSJamal Hadi Salim {
2191d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
2192d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
21931d1e34ddSDavid S. Miller 	int err;
2194d51d081dSJamal Hadi Salim 
219515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
219679b8b7f4SThomas Graf 	if (nlh == NULL)
219779b8b7f4SThomas Graf 		return -EMSGSIZE;
2198d51d081dSJamal Hadi Salim 
21997b67c857SThomas Graf 	id = nlmsg_data(nlh);
2200931e79d7SMathias Krause 	memset(&id->sa_id, 0, sizeof(id->sa_id));
22012b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2202d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
2203d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
2204d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
22052b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
22062b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
2207d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
2208d51d081dSJamal Hadi Salim 
2209d0fde795SDavid S. Miller 	if (x->replay_esn) {
22101d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2211d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
22121d1e34ddSDavid S. Miller 			      x->replay_esn);
2213d0fde795SDavid S. Miller 	} else {
22141d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
22151d1e34ddSDavid S. Miller 			      &x->replay);
2216d0fde795SDavid S. Miller 	}
22171d1e34ddSDavid S. Miller 	if (err)
22181d1e34ddSDavid S. Miller 		goto out_cancel;
2219de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2220de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
22211d1e34ddSDavid S. Miller 	if (err)
22221d1e34ddSDavid S. Miller 		goto out_cancel;
2223d8647b79SSteffen Klassert 
22241d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
22251d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
22261d1e34ddSDavid S. Miller 		if (err)
22271d1e34ddSDavid S. Miller 			goto out_cancel;
22281d1e34ddSDavid S. Miller 	}
22291d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
22301d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
22311d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
22321d1e34ddSDavid S. Miller 		if (err)
22331d1e34ddSDavid S. Miller 			goto out_cancel;
22341d1e34ddSDavid S. Miller 	}
22351d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
22361d1e34ddSDavid S. Miller 	if (err)
22371d1e34ddSDavid S. Miller 		goto out_cancel;
22386f26b61eSJamal Hadi Salim 
22397e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
22407e652640SSteffen Klassert 	if (err)
22417e652640SSteffen Klassert 		goto out_cancel;
22427e652640SSteffen Klassert 
2243053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2244053c095aSJohannes Berg 	return 0;
2245d51d081dSJamal Hadi Salim 
22461d1e34ddSDavid S. Miller out_cancel:
22479825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
22481d1e34ddSDavid S. Miller 	return err;
2249d51d081dSJamal Hadi Salim }
2250d51d081dSJamal Hadi Salim 
225122e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
22525424f32eSThomas Graf 		struct nlattr **attrs)
2253d51d081dSJamal Hadi Salim {
2254fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2255d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2256d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
2257d51d081dSJamal Hadi Salim 	int err;
2258d51d081dSJamal Hadi Salim 	struct km_event c;
22596f26b61eSJamal Hadi Salim 	u32 mark;
22606f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
22617b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
2262d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
2263d51d081dSJamal Hadi Salim 
22646f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
22656f26b61eSJamal Hadi Salim 
22666f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2267d8647b79SSteffen Klassert 	if (x == NULL)
2268d51d081dSJamal Hadi Salim 		return -ESRCH;
2269d8647b79SSteffen Klassert 
2270d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2271d8647b79SSteffen Klassert 	if (r_skb == NULL) {
2272d8647b79SSteffen Klassert 		xfrm_state_put(x);
2273d8647b79SSteffen Klassert 		return -ENOMEM;
2274d51d081dSJamal Hadi Salim 	}
2275d51d081dSJamal Hadi Salim 
2276d51d081dSJamal Hadi Salim 	/*
2277d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
2278d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
2279d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
2280d51d081dSJamal Hadi Salim 	*/
2281d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2282d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
2283d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
228415e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2285d51d081dSJamal Hadi Salim 
22862fc5f83bSGustavo A. R. Silva 	err = build_aevent(r_skb, x, &c);
22872fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
22882fc5f83bSGustavo A. R. Silva 
228915e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2290d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2291d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2292d51d081dSJamal Hadi Salim 	return err;
2293d51d081dSJamal Hadi Salim }
2294d51d081dSJamal Hadi Salim 
229522e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
22965424f32eSThomas Graf 		struct nlattr **attrs)
2297d51d081dSJamal Hadi Salim {
2298fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2299d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2300d51d081dSJamal Hadi Salim 	struct km_event c;
2301d51d081dSJamal Hadi Salim 	int err = -EINVAL;
23026f26b61eSJamal Hadi Salim 	u32 mark = 0;
23036f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
23047b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
23055424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2306d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
23075424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
23084e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
23094e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2310d51d081dSJamal Hadi Salim 
23114e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
2312d51d081dSJamal Hadi Salim 		return err;
2313d51d081dSJamal Hadi Salim 
2314d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
2315d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2316d51d081dSJamal Hadi Salim 		return err;
2317d51d081dSJamal Hadi Salim 
23186f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
23196f26b61eSJamal Hadi Salim 
23206f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2321d51d081dSJamal Hadi Salim 	if (x == NULL)
2322d51d081dSJamal Hadi Salim 		return -ESRCH;
2323d51d081dSJamal Hadi Salim 
2324d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
2325d51d081dSJamal Hadi Salim 		goto out;
2326d51d081dSJamal Hadi Salim 
23274479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
2328e2b19125SSteffen Klassert 	if (err)
2329e2b19125SSteffen Klassert 		goto out;
2330e2b19125SSteffen Klassert 
2331d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2332e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
2333d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2334d51d081dSJamal Hadi Salim 
2335d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
2336d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
233715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2338d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
2339d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
2340d51d081dSJamal Hadi Salim 	err = 0;
2341d51d081dSJamal Hadi Salim out:
2342d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2343d51d081dSJamal Hadi Salim 	return err;
2344d51d081dSJamal Hadi Salim }
2345d51d081dSJamal Hadi Salim 
234622e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
23475424f32eSThomas Graf 		struct nlattr **attrs)
23481da177e4SLinus Torvalds {
2349fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
235026b15dadSJamal Hadi Salim 	struct km_event c;
2351b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
2352f7b6983fSMasahide NAKAMURA 	int err;
235326b15dadSJamal Hadi Salim 
235435a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2355f7b6983fSMasahide NAKAMURA 	if (err)
2356f7b6983fSMasahide NAKAMURA 		return err;
2357f7b6983fSMasahide NAKAMURA 
23582e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
23592f1eb65fSJamal Hadi Salim 	if (err) {
23602f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
23612f1eb65fSJamal Hadi Salim 			return 0;
2362069c474eSDavid S. Miller 		return err;
23632f1eb65fSJamal Hadi Salim 	}
23642f1eb65fSJamal Hadi Salim 
2365f7b6983fSMasahide NAKAMURA 	c.data.type = type;
2366f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
236726b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
236815e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
23697067802eSAlexey Dobriyan 	c.net = net;
237026b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
23711da177e4SLinus Torvalds 	return 0;
23721da177e4SLinus Torvalds }
23731da177e4SLinus Torvalds 
237422e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
23755424f32eSThomas Graf 		struct nlattr **attrs)
23766c5c8ca7SJamal Hadi Salim {
2377fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
23786c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
23797b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
23806c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2381b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
23826c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2383295fae56SJamal Hadi Salim 	struct xfrm_mark m;
23847e652640SSteffen Klassert 	u32 if_id = 0;
23856c5c8ca7SJamal Hadi Salim 
238635a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2387f7b6983fSMasahide NAKAMURA 	if (err)
2388f7b6983fSMasahide NAKAMURA 		return err;
2389f7b6983fSMasahide NAKAMURA 
2390c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
2391c8bf4d04STimo Teräs 	if (err)
2392c8bf4d04STimo Teräs 		return err;
2393c8bf4d04STimo Teräs 
23947e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
23957e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
23967e652640SSteffen Klassert 
23974f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
23984f47e8abSXin Long 
23996c5c8ca7SJamal Hadi Salim 	if (p->index)
24004f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
24014f47e8abSXin Long 				      0, &err);
24026c5c8ca7SJamal Hadi Salim 	else {
24035424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
240403e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
24056c5c8ca7SJamal Hadi Salim 
240635a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
24076c5c8ca7SJamal Hadi Salim 		if (err)
24086c5c8ca7SJamal Hadi Salim 			return err;
24096c5c8ca7SJamal Hadi Salim 
24102c8dd116SDenis V. Lunev 		ctx = NULL;
24116c5c8ca7SJamal Hadi Salim 		if (rt) {
24125424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
24136c5c8ca7SJamal Hadi Salim 
241452a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
241503e1ad7bSPaul Moore 			if (err)
24166c5c8ca7SJamal Hadi Salim 				return err;
24172c8dd116SDenis V. Lunev 		}
24184f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
24196f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
242003e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
24216c5c8ca7SJamal Hadi Salim 	}
24226c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2423ef41aaa0SEric Paris 		return -ENOENT;
242403e1ad7bSPaul Moore 
2425ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
24266c5c8ca7SJamal Hadi Salim 		goto out;
24276c5c8ca7SJamal Hadi Salim 
24286c5c8ca7SJamal Hadi Salim 	err = 0;
24296c5c8ca7SJamal Hadi Salim 	if (up->hard) {
24306c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
24312e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
24326c5c8ca7SJamal Hadi Salim 	}
2433c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
24346c5c8ca7SJamal Hadi Salim 
24356c5c8ca7SJamal Hadi Salim out:
24366c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
24376c5c8ca7SJamal Hadi Salim 	return err;
24386c5c8ca7SJamal Hadi Salim }
24396c5c8ca7SJamal Hadi Salim 
244022e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
24415424f32eSThomas Graf 		struct nlattr **attrs)
244253bc6b4dSJamal Hadi Salim {
2443fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
244453bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
244553bc6b4dSJamal Hadi Salim 	int err;
24467b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
244753bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
24486f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2449928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
245053bc6b4dSJamal Hadi Salim 
24516f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
245253bc6b4dSJamal Hadi Salim 
24533a765aa5SDavid S. Miller 	err = -ENOENT;
245453bc6b4dSJamal Hadi Salim 	if (x == NULL)
245553bc6b4dSJamal Hadi Salim 		return err;
245653bc6b4dSJamal Hadi Salim 
245753bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
24583a765aa5SDavid S. Miller 	err = -EINVAL;
245953bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
246053bc6b4dSJamal Hadi Salim 		goto out;
2461c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
246253bc6b4dSJamal Hadi Salim 
2463161a09e7SJoy Latten 	if (ue->hard) {
246453bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
24652e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2466161a09e7SJoy Latten 	}
24673a765aa5SDavid S. Miller 	err = 0;
246853bc6b4dSJamal Hadi Salim out:
246953bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
247053bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
247153bc6b4dSJamal Hadi Salim 	return err;
247253bc6b4dSJamal Hadi Salim }
247353bc6b4dSJamal Hadi Salim 
247422e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
24755424f32eSThomas Graf 		struct nlattr **attrs)
2476980ebd25SJamal Hadi Salim {
2477fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2478980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2479980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2480980ebd25SJamal Hadi Salim 	int i;
24815424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
24826f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2483980ebd25SJamal Hadi Salim 
24847b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2485fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2486980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2487980ebd25SJamal Hadi Salim 
2488980ebd25SJamal Hadi Salim 	if (!x)
2489d8eb9307SIlpo Järvinen 		goto nomem;
2490980ebd25SJamal Hadi Salim 
24916f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
24926f26b61eSJamal Hadi Salim 
2493980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2494d8eb9307SIlpo Järvinen 	if (err)
249573efc324SVegard Nossum 		goto free_state;
2496a1a7e3a3SXin Long 	err = verify_sec_ctx_len(attrs);
2497a1a7e3a3SXin Long 	if (err)
2498a1a7e3a3SXin Long 		goto free_state;
2499980ebd25SJamal Hadi Salim 
2500980ebd25SJamal Hadi Salim 	/*   build an XP */
2501fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2502d8eb9307SIlpo Järvinen 	if (!xp)
2503d8eb9307SIlpo Järvinen 		goto free_state;
2504980ebd25SJamal Hadi Salim 
2505980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2506980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2507980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
25086f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
25096f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
25105424f32eSThomas Graf 	ut = nla_data(rt);
2511980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2512980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2513980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2514980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2515980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2516980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2517980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2518980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2519980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2520980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2521980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2522980ebd25SJamal Hadi Salim 
2523980ebd25SJamal Hadi Salim 	}
2524980ebd25SJamal Hadi Salim 
25254a135e53SMathias Krause 	xfrm_state_free(x);
2526980ebd25SJamal Hadi Salim 	kfree(xp);
2527980ebd25SJamal Hadi Salim 
2528980ebd25SJamal Hadi Salim 	return 0;
2529d8eb9307SIlpo Järvinen 
2530d8eb9307SIlpo Järvinen free_state:
25314a135e53SMathias Krause 	xfrm_state_free(x);
2532d8eb9307SIlpo Järvinen nomem:
2533d8eb9307SIlpo Järvinen 	return err;
2534980ebd25SJamal Hadi Salim }
2535980ebd25SJamal Hadi Salim 
25365c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
25375c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
253813c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
25395424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
25405c79de6eSShinta Sugimoto {
25415424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
25425c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
25435c79de6eSShinta Sugimoto 	int i, num_migrate;
25445c79de6eSShinta Sugimoto 
254513c1d189SArnaud Ebalard 	if (k != NULL) {
254613c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
254713c1d189SArnaud Ebalard 
254813c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
254913c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
255013c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
255113c1d189SArnaud Ebalard 		k->family = uk->family;
255213c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
255313c1d189SArnaud Ebalard 	}
255413c1d189SArnaud Ebalard 
25555424f32eSThomas Graf 	um = nla_data(rt);
25565424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
25575c79de6eSShinta Sugimoto 
25585c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
25595c79de6eSShinta Sugimoto 		return -EINVAL;
25605c79de6eSShinta Sugimoto 
25615c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
25625c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
25635c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
25645c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
25655c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
25665c79de6eSShinta Sugimoto 
25675c79de6eSShinta Sugimoto 		ma->proto = um->proto;
25685c79de6eSShinta Sugimoto 		ma->mode = um->mode;
25695c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
25705c79de6eSShinta Sugimoto 
25715c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
25725c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
25735c79de6eSShinta Sugimoto 	}
25745c79de6eSShinta Sugimoto 
25755c79de6eSShinta Sugimoto 	*num = i;
25765c79de6eSShinta Sugimoto 	return 0;
25775c79de6eSShinta Sugimoto }
25785c79de6eSShinta Sugimoto 
25795c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
25805424f32eSThomas Graf 			   struct nlattr **attrs)
25815c79de6eSShinta Sugimoto {
25827b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
25835c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
258413c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
25855c79de6eSShinta Sugimoto 	u8 type;
25865c79de6eSShinta Sugimoto 	int err;
25875c79de6eSShinta Sugimoto 	int n = 0;
25888d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
25894ab47d47SAntony Antony 	struct xfrm_encap_tmpl  *encap = NULL;
2590c1aca308SYan Yan 	u32 if_id = 0;
25915c79de6eSShinta Sugimoto 
259235a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2593cf5cb79fSThomas Graf 		return -EINVAL;
25945c79de6eSShinta Sugimoto 
259513c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
259613c1d189SArnaud Ebalard 
25975424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
25985c79de6eSShinta Sugimoto 	if (err)
25995c79de6eSShinta Sugimoto 		return err;
26005c79de6eSShinta Sugimoto 
260113c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
26025c79de6eSShinta Sugimoto 	if (err)
26035c79de6eSShinta Sugimoto 		return err;
26045c79de6eSShinta Sugimoto 
26055c79de6eSShinta Sugimoto 	if (!n)
26065c79de6eSShinta Sugimoto 		return 0;
26075c79de6eSShinta Sugimoto 
26084ab47d47SAntony Antony 	if (attrs[XFRMA_ENCAP]) {
26094ab47d47SAntony Antony 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
26104ab47d47SAntony Antony 				sizeof(*encap), GFP_KERNEL);
26114ab47d47SAntony Antony 		if (!encap)
26124ac7a6eeSZheng Yongjun 			return -ENOMEM;
26135c79de6eSShinta Sugimoto 	}
26144ab47d47SAntony Antony 
2615c1aca308SYan Yan 	if (attrs[XFRMA_IF_ID])
2616c1aca308SYan Yan 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2617c1aca308SYan Yan 
2618c1aca308SYan Yan 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, if_id);
26194ab47d47SAntony Antony 
26204ab47d47SAntony Antony 	kfree(encap);
26214ab47d47SAntony Antony 
26224ab47d47SAntony Antony 	return err;
26234ab47d47SAntony Antony }
26245c79de6eSShinta Sugimoto #else
26255c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
26265424f32eSThomas Graf 			   struct nlattr **attrs)
26275c79de6eSShinta Sugimoto {
26285c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
26295c79de6eSShinta Sugimoto }
26305c79de6eSShinta Sugimoto #endif
26315c79de6eSShinta Sugimoto 
26325c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2633183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
26345c79de6eSShinta Sugimoto {
26355c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
26365c79de6eSShinta Sugimoto 
26375c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
26385c79de6eSShinta Sugimoto 	um.proto = m->proto;
26395c79de6eSShinta Sugimoto 	um.mode = m->mode;
26405c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
26415c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
26425c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
26435c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
26445c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
26455c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
26465c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
26475c79de6eSShinta Sugimoto 
2648c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
26495c79de6eSShinta Sugimoto }
26505c79de6eSShinta Sugimoto 
2651183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
265213c1d189SArnaud Ebalard {
265313c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
265413c1d189SArnaud Ebalard 
265513c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
265613c1d189SArnaud Ebalard 	uk.family = k->family;
265713c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
265813c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2659a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
266013c1d189SArnaud Ebalard 
266113c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
266213c1d189SArnaud Ebalard }
266313c1d189SArnaud Ebalard 
2664a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
26658bafd730SAntony Antony 						int with_encp)
26667deb2264SThomas Graf {
26677deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
266813c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
26698bafd730SAntony Antony 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
26707deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
26717deb2264SThomas Graf 	      + userpolicy_type_attrsize();
26727deb2264SThomas Graf }
26737deb2264SThomas Graf 
2674183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2675183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
26768bafd730SAntony Antony 			 const struct xfrm_selector *sel,
26778bafd730SAntony Antony 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
26785c79de6eSShinta Sugimoto {
2679183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
26805c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
26815c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
26821d1e34ddSDavid S. Miller 	int i, err;
26835c79de6eSShinta Sugimoto 
268479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
268579b8b7f4SThomas Graf 	if (nlh == NULL)
268679b8b7f4SThomas Graf 		return -EMSGSIZE;
26875c79de6eSShinta Sugimoto 
26887b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
26895c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
26905c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
26915c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
26925c79de6eSShinta Sugimoto 	pol_id->dir = dir;
26935c79de6eSShinta Sugimoto 
26941d1e34ddSDavid S. Miller 	if (k != NULL) {
26951d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
26961d1e34ddSDavid S. Miller 		if (err)
26971d1e34ddSDavid S. Miller 			goto out_cancel;
26981d1e34ddSDavid S. Miller 	}
26998bafd730SAntony Antony 	if (encap) {
27008bafd730SAntony Antony 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
27018bafd730SAntony Antony 		if (err)
27028bafd730SAntony Antony 			goto out_cancel;
27038bafd730SAntony Antony 	}
27041d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
27051d1e34ddSDavid S. Miller 	if (err)
27061d1e34ddSDavid S. Miller 		goto out_cancel;
27075c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
27081d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
27091d1e34ddSDavid S. Miller 		if (err)
27101d1e34ddSDavid S. Miller 			goto out_cancel;
27115c79de6eSShinta Sugimoto 	}
27125c79de6eSShinta Sugimoto 
2713053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2714053c095aSJohannes Berg 	return 0;
27151d1e34ddSDavid S. Miller 
27161d1e34ddSDavid S. Miller out_cancel:
27179825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
27181d1e34ddSDavid S. Miller 	return err;
27195c79de6eSShinta Sugimoto }
27205c79de6eSShinta Sugimoto 
2721183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2722183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
27238bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
27248bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
27255c79de6eSShinta Sugimoto {
2726a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
27275c79de6eSShinta Sugimoto 	struct sk_buff *skb;
27282fc5f83bSGustavo A. R. Silva 	int err;
27295c79de6eSShinta Sugimoto 
27308bafd730SAntony Antony 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
27318bafd730SAntony Antony 			GFP_ATOMIC);
27325c79de6eSShinta Sugimoto 	if (skb == NULL)
27335c79de6eSShinta Sugimoto 		return -ENOMEM;
27345c79de6eSShinta Sugimoto 
27355c79de6eSShinta Sugimoto 	/* build migrate */
27362fc5f83bSGustavo A. R. Silva 	err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
27372fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
27385c79de6eSShinta Sugimoto 
273921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
27405c79de6eSShinta Sugimoto }
27415c79de6eSShinta Sugimoto #else
2742183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2743183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
27448bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
27458bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
27465c79de6eSShinta Sugimoto {
27475c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
27485c79de6eSShinta Sugimoto }
27495c79de6eSShinta Sugimoto #endif
2750d51d081dSJamal Hadi Salim 
2751a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2752492b558bSThomas Graf 
27535461fc0cSDmitry Safonov const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
275466f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2755492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2756492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2757492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2758492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2759492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2760492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2761980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
276253bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2763492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
276466f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
27656c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2766492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2767a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2768d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2769d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
277097a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
27715c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2772a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2773880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2774a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
27752d151d39SSteffen Klassert 	[XFRM_MSG_SETDEFAULT  - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
27762d151d39SSteffen Klassert 	[XFRM_MSG_GETDEFAULT  - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
27771da177e4SLinus Torvalds };
27785461fc0cSDmitry Safonov EXPORT_SYMBOL_GPL(xfrm_msg_min);
27791da177e4SLinus Torvalds 
2780492b558bSThomas Graf #undef XMSGSIZE
2781492b558bSThomas Graf 
27825106f4a8SDmitry Safonov const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2783c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2784c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2785c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2786c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
27871a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2788cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2789cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2790cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2791cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2792cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2793cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2794cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2795cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2796cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2797cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2798cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2799cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2800cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2801cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
280213c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
28036f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
280435d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2805d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2806a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2807d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2808870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2809d77e38e6SSteffen Klassert 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
28109b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK]	= { .type = NLA_U32 },
28119b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK_MASK]	= { .type = NLA_U32 },
28127e652640SSteffen Klassert 	[XFRMA_IF_ID]		= { .type = NLA_U32 },
2813cf5cb79fSThomas Graf };
28145106f4a8SDmitry Safonov EXPORT_SYMBOL_GPL(xfrma_policy);
2815cf5cb79fSThomas Graf 
2816880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2817880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2818880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2819880a6fabSChristophe Gouault };
2820880a6fabSChristophe Gouault 
282105600a79SMathias Krause static const struct xfrm_link {
28225424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
28231137b5e2SHerbert Xu 	int (*start)(struct netlink_callback *);
28241da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
28254c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2826880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2827880a6fabSChristophe Gouault 	int nla_max;
2828492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2829492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2830492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2831492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
28324c563f76STimo Teras 						   .dump = xfrm_dump_sa,
28334c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2834492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2835492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2836492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
28371137b5e2SHerbert Xu 						   .start = xfrm_dump_policy_start,
28384c563f76STimo Teras 						   .dump = xfrm_dump_policy,
28394c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2840492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2841980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
284253bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2843492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2844492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
28456c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2846492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2847492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2848d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2849d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
28505c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
285128d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2852880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2853880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2854880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2855ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
28562d151d39SSteffen Klassert 	[XFRM_MSG_SETDEFAULT  - XFRM_MSG_BASE] = { .doit = xfrm_set_default   },
28572d151d39SSteffen Klassert 	[XFRM_MSG_GETDEFAULT  - XFRM_MSG_BASE] = { .doit = xfrm_get_default   },
28581da177e4SLinus Torvalds };
28591da177e4SLinus Torvalds 
28602d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
28612d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
28621da177e4SLinus Torvalds {
2863a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
286435a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
286505600a79SMathias Krause 	const struct xfrm_link *link;
28665106f4a8SDmitry Safonov 	struct nlmsghdr *nlh64 = NULL;
2867a7bd9a45SThomas Graf 	int type, err;
28681da177e4SLinus Torvalds 
28691da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
28701da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
28711d00a4ebSThomas Graf 		return -EINVAL;
28721da177e4SLinus Torvalds 
28731da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
28741da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
28751da177e4SLinus Torvalds 
28761da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
287790f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
28781d00a4ebSThomas Graf 		return -EPERM;
28791da177e4SLinus Torvalds 
28805106f4a8SDmitry Safonov 	if (in_compat_syscall()) {
28815106f4a8SDmitry Safonov 		struct xfrm_translator *xtr = xfrm_get_translator();
28825106f4a8SDmitry Safonov 
28835106f4a8SDmitry Safonov 		if (!xtr)
28845106f4a8SDmitry Safonov 			return -EOPNOTSUPP;
28855106f4a8SDmitry Safonov 
28865106f4a8SDmitry Safonov 		nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
28875106f4a8SDmitry Safonov 					    link->nla_pol, extack);
28885106f4a8SDmitry Safonov 		xfrm_put_translator(xtr);
28895106f4a8SDmitry Safonov 		if (IS_ERR(nlh64))
28905106f4a8SDmitry Safonov 			return PTR_ERR(nlh64);
28915106f4a8SDmitry Safonov 		if (nlh64)
28925106f4a8SDmitry Safonov 			nlh = nlh64;
28935106f4a8SDmitry Safonov 	}
28945106f4a8SDmitry Safonov 
2895492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2896492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2897b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
289880d326faSPablo Neira Ayuso 		struct netlink_dump_control c = {
28991137b5e2SHerbert Xu 			.start = link->start,
290080d326faSPablo Neira Ayuso 			.dump = link->dump,
290180d326faSPablo Neira Ayuso 			.done = link->done,
290280d326faSPablo Neira Ayuso 		};
29035106f4a8SDmitry Safonov 
29045106f4a8SDmitry Safonov 		if (link->dump == NULL) {
29055106f4a8SDmitry Safonov 			err = -EINVAL;
29065106f4a8SDmitry Safonov 			goto err;
290780d326faSPablo Neira Ayuso 		}
29085106f4a8SDmitry Safonov 
29095106f4a8SDmitry Safonov 		err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
29105106f4a8SDmitry Safonov 		goto err;
29111da177e4SLinus Torvalds 	}
29121da177e4SLinus Torvalds 
29138cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2914880a6fabSChristophe Gouault 				     link->nla_max ? : XFRMA_MAX,
2915fe52145fSJohannes Berg 				     link->nla_pol ? : xfrma_policy, extack);
2916a7bd9a45SThomas Graf 	if (err < 0)
29175106f4a8SDmitry Safonov 		goto err;
29185106f4a8SDmitry Safonov 
29195106f4a8SDmitry Safonov 	if (link->doit == NULL) {
29205106f4a8SDmitry Safonov 		err = -EINVAL;
29215106f4a8SDmitry Safonov 		goto err;
29225106f4a8SDmitry Safonov 	}
29235106f4a8SDmitry Safonov 
29245106f4a8SDmitry Safonov 	err = link->doit(skb, nlh, attrs);
29255106f4a8SDmitry Safonov 
29267c1a80e8SPavel Skripkin 	/* We need to free skb allocated in xfrm_alloc_compat() before
29277c1a80e8SPavel Skripkin 	 * returning from this function, because consume_skb() won't take
29287c1a80e8SPavel Skripkin 	 * care of frag_list since netlink destructor sets
29297c1a80e8SPavel Skripkin 	 * sbk->head to NULL. (see netlink_skb_destructor())
29307c1a80e8SPavel Skripkin 	 */
29317c1a80e8SPavel Skripkin 	if (skb_has_frag_list(skb)) {
29327c1a80e8SPavel Skripkin 		kfree_skb(skb_shinfo(skb)->frag_list);
29337c1a80e8SPavel Skripkin 		skb_shinfo(skb)->frag_list = NULL;
29347c1a80e8SPavel Skripkin 	}
29357c1a80e8SPavel Skripkin 
29365106f4a8SDmitry Safonov err:
29375106f4a8SDmitry Safonov 	kvfree(nlh64);
2938a7bd9a45SThomas Graf 	return err;
29391da177e4SLinus Torvalds }
29401da177e4SLinus Torvalds 
2941cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
29421da177e4SLinus Torvalds {
2943283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2944283bc9f3SFan Du 
2945283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2946cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2947283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
29481da177e4SLinus Torvalds }
29491da177e4SLinus Torvalds 
2950a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void)
29517deb2264SThomas Graf {
29526f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
29536f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
29547deb2264SThomas Graf }
29557deb2264SThomas Graf 
2956214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
29571da177e4SLinus Torvalds {
29581da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
29591da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
29601d1e34ddSDavid S. Miller 	int err;
29611da177e4SLinus Torvalds 
296215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
296379b8b7f4SThomas Graf 	if (nlh == NULL)
296479b8b7f4SThomas Graf 		return -EMSGSIZE;
29651da177e4SLinus Torvalds 
29667b67c857SThomas Graf 	ue = nlmsg_data(nlh);
29671da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2968d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
2969e3e5fc16SMathias Krause 	/* clear the padding bytes */
2970caf283d0SKees Cook 	memset_after(ue, 0, hard);
29711da177e4SLinus Torvalds 
29721d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
29731d1e34ddSDavid S. Miller 	if (err)
29741d1e34ddSDavid S. Miller 		return err;
29756f26b61eSJamal Hadi Salim 
29767e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
29777e652640SSteffen Klassert 	if (err)
29787e652640SSteffen Klassert 		return err;
29797e652640SSteffen Klassert 
2980053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2981053c095aSJohannes Berg 	return 0;
29821da177e4SLinus Torvalds }
29831da177e4SLinus Torvalds 
2984214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
29851da177e4SLinus Torvalds {
2986fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
29871da177e4SLinus Torvalds 	struct sk_buff *skb;
29881da177e4SLinus Torvalds 
29897deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
29901da177e4SLinus Torvalds 	if (skb == NULL)
29911da177e4SLinus Torvalds 		return -ENOMEM;
29921da177e4SLinus Torvalds 
29936f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
29946f26b61eSJamal Hadi Salim 		kfree_skb(skb);
29956f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
29966f26b61eSJamal Hadi Salim 	}
29971da177e4SLinus Torvalds 
299821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
29991da177e4SLinus Torvalds }
30001da177e4SLinus Torvalds 
3001214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
3002d51d081dSJamal Hadi Salim {
3003fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
3004d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
30052fc5f83bSGustavo A. R. Silva 	int err;
3006d51d081dSJamal Hadi Salim 
3007d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
3008d51d081dSJamal Hadi Salim 	if (skb == NULL)
3009d51d081dSJamal Hadi Salim 		return -ENOMEM;
3010d51d081dSJamal Hadi Salim 
30112fc5f83bSGustavo A. R. Silva 	err = build_aevent(skb, x, c);
30122fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
3013d51d081dSJamal Hadi Salim 
301421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
3015d51d081dSJamal Hadi Salim }
3016d51d081dSJamal Hadi Salim 
3017214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
301826b15dadSJamal Hadi Salim {
30197067802eSAlexey Dobriyan 	struct net *net = c->net;
302026b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
302126b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
302226b15dadSJamal Hadi Salim 	struct sk_buff *skb;
30237deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
302426b15dadSJamal Hadi Salim 
30257deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
302626b15dadSJamal Hadi Salim 	if (skb == NULL)
302726b15dadSJamal Hadi Salim 		return -ENOMEM;
302826b15dadSJamal Hadi Salim 
302915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
303079b8b7f4SThomas Graf 	if (nlh == NULL) {
303179b8b7f4SThomas Graf 		kfree_skb(skb);
303279b8b7f4SThomas Graf 		return -EMSGSIZE;
303379b8b7f4SThomas Graf 	}
303426b15dadSJamal Hadi Salim 
30357b67c857SThomas Graf 	p = nlmsg_data(nlh);
3036bf08867fSHerbert Xu 	p->proto = c->data.proto;
303726b15dadSJamal Hadi Salim 
30389825069dSThomas Graf 	nlmsg_end(skb, nlh);
303926b15dadSJamal Hadi Salim 
304021ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
304126b15dadSJamal Hadi Salim }
304226b15dadSJamal Hadi Salim 
3043a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
304426b15dadSJamal Hadi Salim {
3045a1b831f2SAlexey Dobriyan 	unsigned int l = 0;
30461a6509d9SHerbert Xu 	if (x->aead)
30471a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
30484447bb33SMartin Willi 	if (x->aalg) {
30494447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
30504447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
30514447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
30524447bb33SMartin Willi 	}
305326b15dadSJamal Hadi Salim 	if (x->ealg)
30540f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
305526b15dadSJamal Hadi Salim 	if (x->calg)
30567deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
305726b15dadSJamal Hadi Salim 	if (x->encap)
30587deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
305935d2856bSMartin Willi 	if (x->tfcpad)
306035d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
3061d8647b79SSteffen Klassert 	if (x->replay_esn)
3062d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
3063f293a5e3Sdingzhi 	else
3064f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
306568325d3bSHerbert Xu 	if (x->security)
306668325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
306768325d3bSHerbert Xu 				    x->security->ctx_len);
306868325d3bSHerbert Xu 	if (x->coaddr)
306968325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
3070a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
3071a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
3072d77e38e6SSteffen Klassert 	if (x->xso.dev)
30737770a39dSEric Dumazet 		 l += nla_total_size(sizeof(struct xfrm_user_offload));
30749b42c1f1SSteffen Klassert 	if (x->props.smark.v | x->props.smark.m) {
30759b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.v));
30769b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.m));
30779b42c1f1SSteffen Klassert 	}
30787e652640SSteffen Klassert 	if (x->if_id)
30797e652640SSteffen Klassert 		l += nla_total_size(sizeof(x->if_id));
308068325d3bSHerbert Xu 
3081d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
3082de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
308326b15dadSJamal Hadi Salim 
30844e484b3eSAntony Antony 	if (x->mapping_maxage)
30854e484b3eSAntony Antony 		l += nla_total_size(sizeof(x->mapping_maxage));
30864e484b3eSAntony Antony 
308726b15dadSJamal Hadi Salim 	return l;
308826b15dadSJamal Hadi Salim }
308926b15dadSJamal Hadi Salim 
3090214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
309126b15dadSJamal Hadi Salim {
3092fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
309326b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
30940603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
309526b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
309626b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3097a1b831f2SAlexey Dobriyan 	unsigned int len = xfrm_sa_len(x);
3098a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3099a1b831f2SAlexey Dobriyan 	int err;
31000603eac0SHerbert Xu 
31010603eac0SHerbert Xu 	headlen = sizeof(*p);
31020603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
31037deb2264SThomas Graf 		len += nla_total_size(headlen);
31040603eac0SHerbert Xu 		headlen = sizeof(*id);
31056f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
31060603eac0SHerbert Xu 	}
31077deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
310826b15dadSJamal Hadi Salim 
31097deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
311026b15dadSJamal Hadi Salim 	if (skb == NULL)
311126b15dadSJamal Hadi Salim 		return -ENOMEM;
311226b15dadSJamal Hadi Salim 
311315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
31141d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
311579b8b7f4SThomas Graf 	if (nlh == NULL)
31161d1e34ddSDavid S. Miller 		goto out_free_skb;
311726b15dadSJamal Hadi Salim 
31187b67c857SThomas Graf 	p = nlmsg_data(nlh);
31190603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
3120c0144beaSThomas Graf 		struct nlattr *attr;
3121c0144beaSThomas Graf 
31227b67c857SThomas Graf 		id = nlmsg_data(nlh);
312350329c8aSMathias Krause 		memset(id, 0, sizeof(*id));
31240603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
31250603eac0SHerbert Xu 		id->spi = x->id.spi;
31260603eac0SHerbert Xu 		id->family = x->props.family;
31270603eac0SHerbert Xu 		id->proto = x->id.proto;
31280603eac0SHerbert Xu 
3129c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
31301d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3131c0144beaSThomas Graf 		if (attr == NULL)
31321d1e34ddSDavid S. Miller 			goto out_free_skb;
3133c0144beaSThomas Graf 
3134c0144beaSThomas Graf 		p = nla_data(attr);
31350603eac0SHerbert Xu 	}
31361d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
31371d1e34ddSDavid S. Miller 	if (err)
31381d1e34ddSDavid S. Miller 		goto out_free_skb;
313926b15dadSJamal Hadi Salim 
31409825069dSThomas Graf 	nlmsg_end(skb, nlh);
314126b15dadSJamal Hadi Salim 
314221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
314326b15dadSJamal Hadi Salim 
31441d1e34ddSDavid S. Miller out_free_skb:
314526b15dadSJamal Hadi Salim 	kfree_skb(skb);
31461d1e34ddSDavid S. Miller 	return err;
314726b15dadSJamal Hadi Salim }
314826b15dadSJamal Hadi Salim 
3149214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
315026b15dadSJamal Hadi Salim {
315126b15dadSJamal Hadi Salim 
315226b15dadSJamal Hadi Salim 	switch (c->event) {
3153f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
315426b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
3155d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
3156d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
3157f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
3158f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
3159f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
316026b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
3161f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
316226b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
316326b15dadSJamal Hadi Salim 	default:
316462db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
316562db5cfdSstephen hemminger 		       c->event);
316626b15dadSJamal Hadi Salim 		break;
316726b15dadSJamal Hadi Salim 	}
316826b15dadSJamal Hadi Salim 
316926b15dadSJamal Hadi Salim 	return 0;
317026b15dadSJamal Hadi Salim 
317126b15dadSJamal Hadi Salim }
317226b15dadSJamal Hadi Salim 
3173a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
31747deb2264SThomas Graf 						struct xfrm_policy *xp)
31757deb2264SThomas Graf {
31767deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
31777deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
31786f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
31797deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
31807deb2264SThomas Graf 	       + userpolicy_type_attrsize();
31817deb2264SThomas Graf }
31827deb2264SThomas Graf 
31831da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
318465e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
31851da177e4SLinus Torvalds {
31861d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
31871da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
31881da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
31891d1e34ddSDavid S. Miller 	int err;
31901da177e4SLinus Torvalds 
319179b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
319279b8b7f4SThomas Graf 	if (nlh == NULL)
319379b8b7f4SThomas Graf 		return -EMSGSIZE;
31941da177e4SLinus Torvalds 
31957b67c857SThomas Graf 	ua = nlmsg_data(nlh);
31961da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
31971da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
31981da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
319965e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
32001da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
32011da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
32021da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
32031da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
32041da177e4SLinus Torvalds 
32051d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
32061d1e34ddSDavid S. Miller 	if (!err)
32071d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
32081d1e34ddSDavid S. Miller 	if (!err)
32091d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
32101d1e34ddSDavid S. Miller 	if (!err)
32111d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
32127e652640SSteffen Klassert 	if (!err)
32137e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
32141d1e34ddSDavid S. Miller 	if (err) {
32151d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
32161d1e34ddSDavid S. Miller 		return err;
32171d1e34ddSDavid S. Miller 	}
32181da177e4SLinus Torvalds 
3219053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3220053c095aSJohannes Berg 	return 0;
32211da177e4SLinus Torvalds }
32221da177e4SLinus Torvalds 
32231da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
322465e0736bSFan Du 			     struct xfrm_policy *xp)
32251da177e4SLinus Torvalds {
3226a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
32271da177e4SLinus Torvalds 	struct sk_buff *skb;
32282fc5f83bSGustavo A. R. Silva 	int err;
32291da177e4SLinus Torvalds 
32307deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
32311da177e4SLinus Torvalds 	if (skb == NULL)
32321da177e4SLinus Torvalds 		return -ENOMEM;
32331da177e4SLinus Torvalds 
32342fc5f83bSGustavo A. R. Silva 	err = build_acquire(skb, x, xt, xp);
32352fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
32361da177e4SLinus Torvalds 
323721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
32381da177e4SLinus Torvalds }
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
32411da177e4SLinus Torvalds  * or more templates.
32421da177e4SLinus Torvalds  */
3243cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
32441da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
32451da177e4SLinus Torvalds {
3246fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
32471da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
32481da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
32491da177e4SLinus Torvalds 	struct xfrm_policy *xp;
32501da177e4SLinus Torvalds 	int nr;
32511da177e4SLinus Torvalds 
3252cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
32531da177e4SLinus Torvalds 	case AF_INET:
32541da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
32551da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
32561da177e4SLinus Torvalds 			return NULL;
32571da177e4SLinus Torvalds 		}
32581da177e4SLinus Torvalds 		break;
3259dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
32601da177e4SLinus Torvalds 	case AF_INET6:
32611da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
32621da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
32631da177e4SLinus Torvalds 			return NULL;
32641da177e4SLinus Torvalds 		}
32651da177e4SLinus Torvalds 		break;
32661da177e4SLinus Torvalds #endif
32671da177e4SLinus Torvalds 	default:
32681da177e4SLinus Torvalds 		*dir = -EINVAL;
32691da177e4SLinus Torvalds 		return NULL;
32701da177e4SLinus Torvalds 	}
32711da177e4SLinus Torvalds 
32721da177e4SLinus Torvalds 	*dir = -EINVAL;
32731da177e4SLinus Torvalds 
32741da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
32751da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
32761da177e4SLinus Torvalds 		return NULL;
32771da177e4SLinus Torvalds 
32781da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
3279b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
32801da177e4SLinus Torvalds 		return NULL;
32811da177e4SLinus Torvalds 
3282a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
3283a4f1bac6SHerbert Xu 		return NULL;
3284a4f1bac6SHerbert Xu 
32852f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
32861da177e4SLinus Torvalds 	if (xp == NULL) {
32871da177e4SLinus Torvalds 		*dir = -ENOBUFS;
32881da177e4SLinus Torvalds 		return NULL;
32891da177e4SLinus Torvalds 	}
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
3292f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
32931da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
32941da177e4SLinus Torvalds 
32951da177e4SLinus Torvalds 	*dir = p->dir;
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds 	return xp;
32981da177e4SLinus Torvalds }
32991da177e4SLinus Torvalds 
3300a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
33017deb2264SThomas Graf {
33027deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
33037deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
33047deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3305295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
33067deb2264SThomas Graf 	       + userpolicy_type_attrsize();
33077deb2264SThomas Graf }
33087deb2264SThomas Graf 
33091da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3310214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
33111da177e4SLinus Torvalds {
33121da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
3313d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
33141d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
33151d1e34ddSDavid S. Miller 	int err;
33161da177e4SLinus Torvalds 
331715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
331879b8b7f4SThomas Graf 	if (nlh == NULL)
331979b8b7f4SThomas Graf 		return -EMSGSIZE;
33201da177e4SLinus Torvalds 
33217b67c857SThomas Graf 	upe = nlmsg_data(nlh);
33221da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
33231d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
33241d1e34ddSDavid S. Miller 	if (!err)
33251d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
33261d1e34ddSDavid S. Miller 	if (!err)
33271d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
33281d1e34ddSDavid S. Miller 	if (!err)
33291d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
33307e652640SSteffen Klassert 	if (!err)
33317e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
33321d1e34ddSDavid S. Miller 	if (err) {
33331d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
33341d1e34ddSDavid S. Miller 		return err;
33351d1e34ddSDavid S. Miller 	}
33361da177e4SLinus Torvalds 	upe->hard = !!hard;
33371da177e4SLinus Torvalds 
3338053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3339053c095aSJohannes Berg 	return 0;
33401da177e4SLinus Torvalds }
33411da177e4SLinus Torvalds 
3342214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
33431da177e4SLinus Torvalds {
3344fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
33451da177e4SLinus Torvalds 	struct sk_buff *skb;
33462fc5f83bSGustavo A. R. Silva 	int err;
33471da177e4SLinus Torvalds 
33487deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
33491da177e4SLinus Torvalds 	if (skb == NULL)
33501da177e4SLinus Torvalds 		return -ENOMEM;
33511da177e4SLinus Torvalds 
33522fc5f83bSGustavo A. R. Silva 	err = build_polexpire(skb, xp, dir, c);
33532fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
33541da177e4SLinus Torvalds 
335521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
33561da177e4SLinus Torvalds }
33571da177e4SLinus Torvalds 
3358214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
335926b15dadSJamal Hadi Salim {
3360a1b831f2SAlexey Dobriyan 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3361fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
336226b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
33630603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
336426b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
336526b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3366a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3367a1b831f2SAlexey Dobriyan 	int err;
33680603eac0SHerbert Xu 
33690603eac0SHerbert Xu 	headlen = sizeof(*p);
33700603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
33717deb2264SThomas Graf 		len += nla_total_size(headlen);
33720603eac0SHerbert Xu 		headlen = sizeof(*id);
33730603eac0SHerbert Xu 	}
3374cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
3375295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
33767deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
337726b15dadSJamal Hadi Salim 
33787deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
337926b15dadSJamal Hadi Salim 	if (skb == NULL)
338026b15dadSJamal Hadi Salim 		return -ENOMEM;
338126b15dadSJamal Hadi Salim 
338215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
33831d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
338479b8b7f4SThomas Graf 	if (nlh == NULL)
33851d1e34ddSDavid S. Miller 		goto out_free_skb;
338626b15dadSJamal Hadi Salim 
33877b67c857SThomas Graf 	p = nlmsg_data(nlh);
33880603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
3389c0144beaSThomas Graf 		struct nlattr *attr;
3390c0144beaSThomas Graf 
33917b67c857SThomas Graf 		id = nlmsg_data(nlh);
33920603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
33930603eac0SHerbert Xu 		id->dir = dir;
33940603eac0SHerbert Xu 		if (c->data.byid)
33950603eac0SHerbert Xu 			id->index = xp->index;
33960603eac0SHerbert Xu 		else
33970603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
33980603eac0SHerbert Xu 
3399c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
34001d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3401c0144beaSThomas Graf 		if (attr == NULL)
34021d1e34ddSDavid S. Miller 			goto out_free_skb;
3403c0144beaSThomas Graf 
3404c0144beaSThomas Graf 		p = nla_data(attr);
34050603eac0SHerbert Xu 	}
340626b15dadSJamal Hadi Salim 
340726b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
34081d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
34091d1e34ddSDavid S. Miller 	if (!err)
34101d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
34111d1e34ddSDavid S. Miller 	if (!err)
34121d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
34137e652640SSteffen Klassert 	if (!err)
34147e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
34151d1e34ddSDavid S. Miller 	if (err)
34161d1e34ddSDavid S. Miller 		goto out_free_skb;
3417295fae56SJamal Hadi Salim 
34189825069dSThomas Graf 	nlmsg_end(skb, nlh);
341926b15dadSJamal Hadi Salim 
342021ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
342126b15dadSJamal Hadi Salim 
34221d1e34ddSDavid S. Miller out_free_skb:
342326b15dadSJamal Hadi Salim 	kfree_skb(skb);
34241d1e34ddSDavid S. Miller 	return err;
342526b15dadSJamal Hadi Salim }
342626b15dadSJamal Hadi Salim 
3427214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
342826b15dadSJamal Hadi Salim {
34297067802eSAlexey Dobriyan 	struct net *net = c->net;
343026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
343126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
34321d1e34ddSDavid S. Miller 	int err;
343326b15dadSJamal Hadi Salim 
34347deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
343526b15dadSJamal Hadi Salim 	if (skb == NULL)
343626b15dadSJamal Hadi Salim 		return -ENOMEM;
343726b15dadSJamal Hadi Salim 
343815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
34391d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
344079b8b7f4SThomas Graf 	if (nlh == NULL)
34411d1e34ddSDavid S. Miller 		goto out_free_skb;
34421d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
34431d1e34ddSDavid S. Miller 	if (err)
34441d1e34ddSDavid S. Miller 		goto out_free_skb;
344526b15dadSJamal Hadi Salim 
34469825069dSThomas Graf 	nlmsg_end(skb, nlh);
344726b15dadSJamal Hadi Salim 
344821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
344926b15dadSJamal Hadi Salim 
34501d1e34ddSDavid S. Miller out_free_skb:
345126b15dadSJamal Hadi Salim 	kfree_skb(skb);
34521d1e34ddSDavid S. Miller 	return err;
345326b15dadSJamal Hadi Salim }
345426b15dadSJamal Hadi Salim 
3455214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
345626b15dadSJamal Hadi Salim {
345726b15dadSJamal Hadi Salim 
345826b15dadSJamal Hadi Salim 	switch (c->event) {
3459f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
3460f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
3461f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
346226b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
3463f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
346426b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
3465f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
346626b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
346726b15dadSJamal Hadi Salim 	default:
346862db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
346962db5cfdSstephen hemminger 		       c->event);
347026b15dadSJamal Hadi Salim 	}
347126b15dadSJamal Hadi Salim 
347226b15dadSJamal Hadi Salim 	return 0;
347326b15dadSJamal Hadi Salim 
347426b15dadSJamal Hadi Salim }
347526b15dadSJamal Hadi Salim 
3476a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void)
34777deb2264SThomas Graf {
34787deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
34797deb2264SThomas Graf }
34807deb2264SThomas Graf 
348197a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
348297a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
348397a64b45SMasahide NAKAMURA {
348497a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
348597a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
348697a64b45SMasahide NAKAMURA 
348779b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
348879b8b7f4SThomas Graf 	if (nlh == NULL)
348979b8b7f4SThomas Graf 		return -EMSGSIZE;
349097a64b45SMasahide NAKAMURA 
34917b67c857SThomas Graf 	ur = nlmsg_data(nlh);
349297a64b45SMasahide NAKAMURA 	ur->proto = proto;
349397a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
349497a64b45SMasahide NAKAMURA 
34951d1e34ddSDavid S. Miller 	if (addr) {
34961d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
34971d1e34ddSDavid S. Miller 		if (err) {
34989825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
34991d1e34ddSDavid S. Miller 			return err;
35001d1e34ddSDavid S. Miller 		}
35011d1e34ddSDavid S. Miller 	}
3502053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3503053c095aSJohannes Berg 	return 0;
350497a64b45SMasahide NAKAMURA }
350597a64b45SMasahide NAKAMURA 
3506db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3507db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
350897a64b45SMasahide NAKAMURA {
350997a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
35102fc5f83bSGustavo A. R. Silva 	int err;
351197a64b45SMasahide NAKAMURA 
35127deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
351397a64b45SMasahide NAKAMURA 	if (skb == NULL)
351497a64b45SMasahide NAKAMURA 		return -ENOMEM;
351597a64b45SMasahide NAKAMURA 
35162fc5f83bSGustavo A. R. Silva 	err = build_report(skb, proto, sel, addr);
35172fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
351897a64b45SMasahide NAKAMURA 
351921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
352097a64b45SMasahide NAKAMURA }
352197a64b45SMasahide NAKAMURA 
3522a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void)
35233a2dfbe8SMartin Willi {
35243a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
35253a2dfbe8SMartin Willi }
35263a2dfbe8SMartin Willi 
35273a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
35283a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
35293a2dfbe8SMartin Willi {
35303a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
35313a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
35323a2dfbe8SMartin Willi 
35333a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
35343a2dfbe8SMartin Willi 	if (nlh == NULL)
35353a2dfbe8SMartin Willi 		return -EMSGSIZE;
35363a2dfbe8SMartin Willi 
35373a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
35383a2dfbe8SMartin Willi 
35393a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
35403a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
35413a2dfbe8SMartin Willi 	um->id.family = x->props.family;
35423a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
35433a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
35443a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
35453a2dfbe8SMartin Willi 	um->new_sport = new_sport;
35463a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
35473a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
35483a2dfbe8SMartin Willi 
3549053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3550053c095aSJohannes Berg 	return 0;
35513a2dfbe8SMartin Willi }
35523a2dfbe8SMartin Willi 
35533a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
35543a2dfbe8SMartin Willi 			     __be16 sport)
35553a2dfbe8SMartin Willi {
3556a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
35573a2dfbe8SMartin Willi 	struct sk_buff *skb;
35582fc5f83bSGustavo A. R. Silva 	int err;
35593a2dfbe8SMartin Willi 
35603a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
35613a2dfbe8SMartin Willi 		return -EINVAL;
35623a2dfbe8SMartin Willi 
35633a2dfbe8SMartin Willi 	if (!x->encap)
35643a2dfbe8SMartin Willi 		return -EINVAL;
35653a2dfbe8SMartin Willi 
35663a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
35673a2dfbe8SMartin Willi 	if (skb == NULL)
35683a2dfbe8SMartin Willi 		return -ENOMEM;
35693a2dfbe8SMartin Willi 
35702fc5f83bSGustavo A. R. Silva 	err = build_mapping(skb, x, ipaddr, sport);
35712fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
35723a2dfbe8SMartin Willi 
357321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
35743a2dfbe8SMartin Willi }
35753a2dfbe8SMartin Willi 
35760f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
35770f24558eSHoria Geanta {
35780f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
35790f24558eSHoria Geanta }
35800f24558eSHoria Geanta 
35811da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
35821da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
35831da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
35841da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
35851da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
358697a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
35875c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
35883a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
35890f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
35901da177e4SLinus Torvalds };
35911da177e4SLinus Torvalds 
3592a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
35931da177e4SLinus Torvalds {
3594be33690dSPatrick McHardy 	struct sock *nlsk;
3595a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3596a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3597a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3598a31f2d17SPablo Neira Ayuso 	};
3599be33690dSPatrick McHardy 
36009f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3601be33690dSPatrick McHardy 	if (nlsk == NULL)
36021da177e4SLinus Torvalds 		return -ENOMEM;
3603d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3604cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
36051da177e4SLinus Torvalds 	return 0;
36061da177e4SLinus Torvalds }
36071da177e4SLinus Torvalds 
36086218fe18SFlorian Westphal static void __net_exit xfrm_user_net_pre_exit(struct net *net)
36096218fe18SFlorian Westphal {
36106218fe18SFlorian Westphal 	RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
36116218fe18SFlorian Westphal }
36126218fe18SFlorian Westphal 
3613d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3614a6483b79SAlexey Dobriyan {
3615d79d792eSEric W. Biederman 	struct net *net;
36166218fe18SFlorian Westphal 
3617d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3618d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3619a6483b79SAlexey Dobriyan }
3620a6483b79SAlexey Dobriyan 
3621a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3622a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
36236218fe18SFlorian Westphal 	.pre_exit   = xfrm_user_net_pre_exit,
3624d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3625a6483b79SAlexey Dobriyan };
3626a6483b79SAlexey Dobriyan 
3627a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3628a6483b79SAlexey Dobriyan {
3629a6483b79SAlexey Dobriyan 	int rv;
3630a6483b79SAlexey Dobriyan 
3631a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3632a6483b79SAlexey Dobriyan 
3633a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3634a6483b79SAlexey Dobriyan 	if (rv < 0)
3635a6483b79SAlexey Dobriyan 		return rv;
3636a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3637a6483b79SAlexey Dobriyan 	if (rv < 0)
3638a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3639a6483b79SAlexey Dobriyan 	return rv;
3640a6483b79SAlexey Dobriyan }
3641a6483b79SAlexey Dobriyan 
36421da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
36431da177e4SLinus Torvalds {
36441da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3645a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
36461da177e4SLinus Torvalds }
36471da177e4SLinus Torvalds 
36481da177e4SLinus Torvalds module_init(xfrm_user_init);
36491da177e4SLinus Torvalds module_exit(xfrm_user_exit);
36501da177e4SLinus Torvalds MODULE_LICENSE("GPL");
36514fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3652