xref: /linux/net/xfrm/xfrm_user.c (revision a1b831f23a2b3306ecf275872a54abcf97b0b977)
11da177e4SLinus Torvalds /* xfrm_user.c: User interface to configure xfrm engine.
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Changes:
61da177e4SLinus Torvalds  *	Mitsuru KANDA @USAGI
71da177e4SLinus Torvalds  * 	Kazunori MIYAZAWA @USAGI
81da177e4SLinus Torvalds  * 	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
91da177e4SLinus Torvalds  * 		IPv6 support
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
139409f38aSHerbert Xu #include <linux/crypto.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/types.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/socket.h>
191da177e4SLinus Torvalds #include <linux/string.h>
201da177e4SLinus Torvalds #include <linux/net.h>
211da177e4SLinus Torvalds #include <linux/skbuff.h>
221da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
231da177e4SLinus Torvalds #include <linux/ipsec.h>
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/security.h>
261da177e4SLinus Torvalds #include <net/sock.h>
271da177e4SLinus Torvalds #include <net/xfrm.h>
2888fc2c84SThomas Graf #include <net/netlink.h>
29fa6dd8a2SNicolas Dichtel #include <net/ah.h>
307c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
31dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
32e23c7194SMasahide NAKAMURA #include <linux/in6.h>
33e23c7194SMasahide NAKAMURA #endif
34e33d4f13SSowmini Varadhan #include <asm/unaligned.h>
351da177e4SLinus Torvalds 
365424f32eSThomas Graf static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
371da177e4SLinus Torvalds {
385424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
391da177e4SLinus Torvalds 	struct xfrm_algo *algp;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	if (!rt)
421da177e4SLinus Torvalds 		return 0;
431da177e4SLinus Torvalds 
445424f32eSThomas Graf 	algp = nla_data(rt);
4506cd22f8SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_alg_len(algp))
4631c26852SHerbert Xu 		return -EINVAL;
4731c26852SHerbert Xu 
481da177e4SLinus Torvalds 	switch (type) {
491da177e4SLinus Torvalds 	case XFRMA_ALG_AUTH:
501da177e4SLinus Torvalds 	case XFRMA_ALG_CRYPT:
511da177e4SLinus Torvalds 	case XFRMA_ALG_COMP:
521da177e4SLinus Torvalds 		break;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	default:
551da177e4SLinus Torvalds 		return -EINVAL;
563ff50b79SStephen Hemminger 	}
571da177e4SLinus Torvalds 
58633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
591da177e4SLinus Torvalds 	return 0;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
624447bb33SMartin Willi static int verify_auth_trunc(struct nlattr **attrs)
634447bb33SMartin Willi {
644447bb33SMartin Willi 	struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
654447bb33SMartin Willi 	struct xfrm_algo_auth *algp;
664447bb33SMartin Willi 
674447bb33SMartin Willi 	if (!rt)
684447bb33SMartin Willi 		return 0;
694447bb33SMartin Willi 
704447bb33SMartin Willi 	algp = nla_data(rt);
711bd963a7SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
724447bb33SMartin Willi 		return -EINVAL;
734447bb33SMartin Willi 
74633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
754447bb33SMartin Willi 	return 0;
764447bb33SMartin Willi }
774447bb33SMartin Willi 
781a6509d9SHerbert Xu static int verify_aead(struct nlattr **attrs)
791a6509d9SHerbert Xu {
801a6509d9SHerbert Xu 	struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
811a6509d9SHerbert Xu 	struct xfrm_algo_aead *algp;
821a6509d9SHerbert Xu 
831a6509d9SHerbert Xu 	if (!rt)
841a6509d9SHerbert Xu 		return 0;
851a6509d9SHerbert Xu 
861a6509d9SHerbert Xu 	algp = nla_data(rt);
87373b8eebSAlexey Dobriyan 	if (nla_len(rt) < (int)aead_len(algp))
881a6509d9SHerbert Xu 		return -EINVAL;
891a6509d9SHerbert Xu 
90633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
911a6509d9SHerbert Xu 	return 0;
921a6509d9SHerbert Xu }
931a6509d9SHerbert Xu 
945424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
95eb2971b6SMasahide NAKAMURA 			   xfrm_address_t **addrp)
96eb2971b6SMasahide NAKAMURA {
975424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
98eb2971b6SMasahide NAKAMURA 
99cf5cb79fSThomas Graf 	if (rt && addrp)
1005424f32eSThomas Graf 		*addrp = nla_data(rt);
101eb2971b6SMasahide NAKAMURA }
102df71837dSTrent Jaeger 
1035424f32eSThomas Graf static inline int verify_sec_ctx_len(struct nlattr **attrs)
104df71837dSTrent Jaeger {
1055424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
106df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
107df71837dSTrent Jaeger 
108df71837dSTrent Jaeger 	if (!rt)
109df71837dSTrent Jaeger 		return 0;
110df71837dSTrent Jaeger 
1115424f32eSThomas Graf 	uctx = nla_data(rt);
112cf5cb79fSThomas Graf 	if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
113df71837dSTrent Jaeger 		return -EINVAL;
114df71837dSTrent Jaeger 
115df71837dSTrent Jaeger 	return 0;
116df71837dSTrent Jaeger }
117df71837dSTrent Jaeger 
118d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p,
119d8647b79SSteffen Klassert 				struct nlattr **attrs)
120d8647b79SSteffen Klassert {
121d8647b79SSteffen Klassert 	struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
122ecd79187SMathias Krause 	struct xfrm_replay_state_esn *rs;
123d8647b79SSteffen Klassert 
124ecd79187SMathias Krause 	if (p->flags & XFRM_STATE_ESN) {
125ecd79187SMathias Krause 		if (!rt)
1267833aa05SSteffen Klassert 			return -EINVAL;
1277833aa05SSteffen Klassert 
128ecd79187SMathias Krause 		rs = nla_data(rt);
129ecd79187SMathias Krause 
130ecd79187SMathias Krause 		if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
131ecd79187SMathias Krause 			return -EINVAL;
132ecd79187SMathias Krause 
1335e708e47SAlexey Dobriyan 		if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
134ecd79187SMathias Krause 		    nla_len(rt) != sizeof(*rs))
135ecd79187SMathias Krause 			return -EINVAL;
136ecd79187SMathias Krause 	}
137ecd79187SMathias Krause 
138d8647b79SSteffen Klassert 	if (!rt)
139d8647b79SSteffen Klassert 		return 0;
140d8647b79SSteffen Klassert 
14101714109SFan Du 	/* As only ESP and AH support ESN feature. */
14201714109SFan Du 	if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
14302aadf72SSteffen Klassert 		return -EINVAL;
14402aadf72SSteffen Klassert 
145d8647b79SSteffen Klassert 	if (p->replay_window != 0)
146d8647b79SSteffen Klassert 		return -EINVAL;
147d8647b79SSteffen Klassert 
148d8647b79SSteffen Klassert 	return 0;
149d8647b79SSteffen Klassert }
150df71837dSTrent Jaeger 
1511da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p,
1525424f32eSThomas Graf 			     struct nlattr **attrs)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	int err;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	err = -EINVAL;
1571da177e4SLinus Torvalds 	switch (p->family) {
1581da177e4SLinus Torvalds 	case AF_INET:
1591da177e4SLinus Torvalds 		break;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	case AF_INET6:
162dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1631da177e4SLinus Torvalds 		break;
1641da177e4SLinus Torvalds #else
1651da177e4SLinus Torvalds 		err = -EAFNOSUPPORT;
1661da177e4SLinus Torvalds 		goto out;
1671da177e4SLinus Torvalds #endif
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	default:
1701da177e4SLinus Torvalds 		goto out;
1713ff50b79SStephen Hemminger 	}
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	err = -EINVAL;
1741da177e4SLinus Torvalds 	switch (p->id.proto) {
1751da177e4SLinus Torvalds 	case IPPROTO_AH:
1764447bb33SMartin Willi 		if ((!attrs[XFRMA_ALG_AUTH]	&&
1774447bb33SMartin Willi 		     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
1781a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
17935a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
18035d2856bSMartin Willi 		    attrs[XFRMA_ALG_COMP]	||
181a0e5ef53STobias Brunner 		    attrs[XFRMA_TFCPAD])
1821da177e4SLinus Torvalds 			goto out;
1831da177e4SLinus Torvalds 		break;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	case IPPROTO_ESP:
1861a6509d9SHerbert Xu 		if (attrs[XFRMA_ALG_COMP])
1871a6509d9SHerbert Xu 			goto out;
1881a6509d9SHerbert Xu 		if (!attrs[XFRMA_ALG_AUTH] &&
1894447bb33SMartin Willi 		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
1901a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_CRYPT] &&
1911a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_AEAD])
1921a6509d9SHerbert Xu 			goto out;
1931a6509d9SHerbert Xu 		if ((attrs[XFRMA_ALG_AUTH] ||
1944447bb33SMartin Willi 		     attrs[XFRMA_ALG_AUTH_TRUNC] ||
1951a6509d9SHerbert Xu 		     attrs[XFRMA_ALG_CRYPT]) &&
1961a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD])
1971da177e4SLinus Torvalds 			goto out;
19835d2856bSMartin Willi 		if (attrs[XFRMA_TFCPAD] &&
19935d2856bSMartin Willi 		    p->mode != XFRM_MODE_TUNNEL)
20035d2856bSMartin Willi 			goto out;
2011da177e4SLinus Torvalds 		break;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	case IPPROTO_COMP:
20435a7aa08SThomas Graf 		if (!attrs[XFRMA_ALG_COMP]	||
2051a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
20635a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2074447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
20835d2856bSMartin Willi 		    attrs[XFRMA_ALG_CRYPT]	||
209a0e5ef53STobias Brunner 		    attrs[XFRMA_TFCPAD]		||
210a0e5ef53STobias Brunner 		    (ntohl(p->id.spi) >= 0x10000))
2111da177e4SLinus Torvalds 			goto out;
2121da177e4SLinus Torvalds 		break;
2131da177e4SLinus Torvalds 
214dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
215e23c7194SMasahide NAKAMURA 	case IPPROTO_DSTOPTS:
216e23c7194SMasahide NAKAMURA 	case IPPROTO_ROUTING:
21735a7aa08SThomas Graf 		if (attrs[XFRMA_ALG_COMP]	||
21835a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2194447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
2201a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
22135a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
22235a7aa08SThomas Graf 		    attrs[XFRMA_ENCAP]		||
22335a7aa08SThomas Graf 		    attrs[XFRMA_SEC_CTX]	||
22435d2856bSMartin Willi 		    attrs[XFRMA_TFCPAD]		||
22535a7aa08SThomas Graf 		    !attrs[XFRMA_COADDR])
226e23c7194SMasahide NAKAMURA 			goto out;
227e23c7194SMasahide NAKAMURA 		break;
228e23c7194SMasahide NAKAMURA #endif
229e23c7194SMasahide NAKAMURA 
2301da177e4SLinus Torvalds 	default:
2311da177e4SLinus Torvalds 		goto out;
2323ff50b79SStephen Hemminger 	}
2331da177e4SLinus Torvalds 
2341a6509d9SHerbert Xu 	if ((err = verify_aead(attrs)))
2351a6509d9SHerbert Xu 		goto out;
2364447bb33SMartin Willi 	if ((err = verify_auth_trunc(attrs)))
2374447bb33SMartin Willi 		goto out;
23835a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
2391da177e4SLinus Torvalds 		goto out;
24035a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
2411da177e4SLinus Torvalds 		goto out;
24235a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
2431da177e4SLinus Torvalds 		goto out;
24435a7aa08SThomas Graf 	if ((err = verify_sec_ctx_len(attrs)))
245df71837dSTrent Jaeger 		goto out;
246d8647b79SSteffen Klassert 	if ((err = verify_replay(p, attrs)))
247d8647b79SSteffen Klassert 		goto out;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	err = -EINVAL;
2501da177e4SLinus Torvalds 	switch (p->mode) {
2517e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TRANSPORT:
2527e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TUNNEL:
253060f02a3SNoriaki TAKAMIYA 	case XFRM_MODE_ROUTEOPTIMIZATION:
2540a69452cSDiego Beltrami 	case XFRM_MODE_BEET:
2551da177e4SLinus Torvalds 		break;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	default:
2581da177e4SLinus Torvalds 		goto out;
2593ff50b79SStephen Hemminger 	}
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	err = 0;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds out:
2641da177e4SLinus Torvalds 	return err;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
2686f2f19edSDavid S. Miller 			   struct xfrm_algo_desc *(*get_byname)(const char *, int),
2695424f32eSThomas Graf 			   struct nlattr *rta)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	struct xfrm_algo *p, *ualg;
2721da177e4SLinus Torvalds 	struct xfrm_algo_desc *algo;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	if (!rta)
2751da177e4SLinus Torvalds 		return 0;
2761da177e4SLinus Torvalds 
2775424f32eSThomas Graf 	ualg = nla_data(rta);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	algo = get_byname(ualg->alg_name, 1);
2801da177e4SLinus Torvalds 	if (!algo)
2811da177e4SLinus Torvalds 		return -ENOSYS;
2821da177e4SLinus Torvalds 	*props = algo->desc.sadb_alg_id;
2831da177e4SLinus Torvalds 
2840f99be0dSEric Dumazet 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
2851da177e4SLinus Torvalds 	if (!p)
2861da177e4SLinus Torvalds 		return -ENOMEM;
2871da177e4SLinus Torvalds 
28804ff1260SHerbert Xu 	strcpy(p->alg_name, algo->name);
2891da177e4SLinus Torvalds 	*algpp = p;
2901da177e4SLinus Torvalds 	return 0;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
29369b0137fSHerbert Xu static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
29469b0137fSHerbert Xu {
29569b0137fSHerbert Xu 	struct xfrm_algo *p, *ualg;
29669b0137fSHerbert Xu 	struct xfrm_algo_desc *algo;
29769b0137fSHerbert Xu 
29869b0137fSHerbert Xu 	if (!rta)
29969b0137fSHerbert Xu 		return 0;
30069b0137fSHerbert Xu 
30169b0137fSHerbert Xu 	ualg = nla_data(rta);
30269b0137fSHerbert Xu 
30369b0137fSHerbert Xu 	algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
30469b0137fSHerbert Xu 	if (!algo)
30569b0137fSHerbert Xu 		return -ENOSYS;
30669b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
30769b0137fSHerbert Xu 
30869b0137fSHerbert Xu 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
30969b0137fSHerbert Xu 	if (!p)
31069b0137fSHerbert Xu 		return -ENOMEM;
31169b0137fSHerbert Xu 
31269b0137fSHerbert Xu 	strcpy(p->alg_name, algo->name);
31369b0137fSHerbert Xu 	x->ealg = p;
31469b0137fSHerbert Xu 	x->geniv = algo->uinfo.encr.geniv;
31569b0137fSHerbert Xu 	return 0;
31669b0137fSHerbert Xu }
31769b0137fSHerbert Xu 
3184447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
3194447bb33SMartin Willi 		       struct nlattr *rta)
3204447bb33SMartin Willi {
3214447bb33SMartin Willi 	struct xfrm_algo *ualg;
3224447bb33SMartin Willi 	struct xfrm_algo_auth *p;
3234447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3244447bb33SMartin Willi 
3254447bb33SMartin Willi 	if (!rta)
3264447bb33SMartin Willi 		return 0;
3274447bb33SMartin Willi 
3284447bb33SMartin Willi 	ualg = nla_data(rta);
3294447bb33SMartin Willi 
3304447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3314447bb33SMartin Willi 	if (!algo)
3324447bb33SMartin Willi 		return -ENOSYS;
3334447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3344447bb33SMartin Willi 
3354447bb33SMartin Willi 	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
3364447bb33SMartin Willi 	if (!p)
3374447bb33SMartin Willi 		return -ENOMEM;
3384447bb33SMartin Willi 
3394447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3404447bb33SMartin Willi 	p->alg_key_len = ualg->alg_key_len;
3414447bb33SMartin Willi 	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3424447bb33SMartin Willi 	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
3434447bb33SMartin Willi 
3444447bb33SMartin Willi 	*algpp = p;
3454447bb33SMartin Willi 	return 0;
3464447bb33SMartin Willi }
3474447bb33SMartin Willi 
3484447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
3494447bb33SMartin Willi 			     struct nlattr *rta)
3504447bb33SMartin Willi {
3514447bb33SMartin Willi 	struct xfrm_algo_auth *p, *ualg;
3524447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3534447bb33SMartin Willi 
3544447bb33SMartin Willi 	if (!rta)
3554447bb33SMartin Willi 		return 0;
3564447bb33SMartin Willi 
3574447bb33SMartin Willi 	ualg = nla_data(rta);
3584447bb33SMartin Willi 
3594447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3604447bb33SMartin Willi 	if (!algo)
3614447bb33SMartin Willi 		return -ENOSYS;
362689f1c9dSHerbert Xu 	if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
3634447bb33SMartin Willi 		return -EINVAL;
3644447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3654447bb33SMartin Willi 
3664447bb33SMartin Willi 	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
3674447bb33SMartin Willi 	if (!p)
3684447bb33SMartin Willi 		return -ENOMEM;
3694447bb33SMartin Willi 
3704447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3714447bb33SMartin Willi 	if (!p->alg_trunc_len)
3724447bb33SMartin Willi 		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3734447bb33SMartin Willi 
3744447bb33SMartin Willi 	*algpp = p;
3754447bb33SMartin Willi 	return 0;
3764447bb33SMartin Willi }
3774447bb33SMartin Willi 
37869b0137fSHerbert Xu static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
3791a6509d9SHerbert Xu {
3801a6509d9SHerbert Xu 	struct xfrm_algo_aead *p, *ualg;
3811a6509d9SHerbert Xu 	struct xfrm_algo_desc *algo;
3821a6509d9SHerbert Xu 
3831a6509d9SHerbert Xu 	if (!rta)
3841a6509d9SHerbert Xu 		return 0;
3851a6509d9SHerbert Xu 
3861a6509d9SHerbert Xu 	ualg = nla_data(rta);
3871a6509d9SHerbert Xu 
3881a6509d9SHerbert Xu 	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
3891a6509d9SHerbert Xu 	if (!algo)
3901a6509d9SHerbert Xu 		return -ENOSYS;
39169b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
3921a6509d9SHerbert Xu 
3931a6509d9SHerbert Xu 	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
3941a6509d9SHerbert Xu 	if (!p)
3951a6509d9SHerbert Xu 		return -ENOMEM;
3961a6509d9SHerbert Xu 
3971a6509d9SHerbert Xu 	strcpy(p->alg_name, algo->name);
39869b0137fSHerbert Xu 	x->aead = p;
39969b0137fSHerbert Xu 	x->geniv = algo->uinfo.aead.geniv;
4001a6509d9SHerbert Xu 	return 0;
4011a6509d9SHerbert Xu }
4021a6509d9SHerbert Xu 
403e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
404e2b19125SSteffen Klassert 					 struct nlattr *rp)
405e2b19125SSteffen Klassert {
406e2b19125SSteffen Klassert 	struct xfrm_replay_state_esn *up;
4075e708e47SAlexey Dobriyan 	unsigned int ulen;
408e2b19125SSteffen Klassert 
409e2b19125SSteffen Klassert 	if (!replay_esn || !rp)
410e2b19125SSteffen Klassert 		return 0;
411e2b19125SSteffen Klassert 
412e2b19125SSteffen Klassert 	up = nla_data(rp);
413ecd79187SMathias Krause 	ulen = xfrm_replay_state_esn_len(up);
414e2b19125SSteffen Klassert 
415f843ee6dSAndy Whitcroft 	/* Check the overall length and the internal bitmap length to avoid
416f843ee6dSAndy Whitcroft 	 * potential overflow. */
4175e708e47SAlexey Dobriyan 	if (nla_len(rp) < (int)ulen ||
418f843ee6dSAndy Whitcroft 	    xfrm_replay_state_esn_len(replay_esn) != ulen ||
419f843ee6dSAndy Whitcroft 	    replay_esn->bmp_len != up->bmp_len)
420e2b19125SSteffen Klassert 		return -EINVAL;
421e2b19125SSteffen Klassert 
422677e806dSAndy Whitcroft 	if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
423677e806dSAndy Whitcroft 		return -EINVAL;
424677e806dSAndy Whitcroft 
425e2b19125SSteffen Klassert 	return 0;
426e2b19125SSteffen Klassert }
427e2b19125SSteffen Klassert 
428d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
429d8647b79SSteffen Klassert 				       struct xfrm_replay_state_esn **preplay_esn,
430d8647b79SSteffen Klassert 				       struct nlattr *rta)
431d8647b79SSteffen Klassert {
432d8647b79SSteffen Klassert 	struct xfrm_replay_state_esn *p, *pp, *up;
4335e708e47SAlexey Dobriyan 	unsigned int klen, ulen;
434d8647b79SSteffen Klassert 
435d8647b79SSteffen Klassert 	if (!rta)
436d8647b79SSteffen Klassert 		return 0;
437d8647b79SSteffen Klassert 
438d8647b79SSteffen Klassert 	up = nla_data(rta);
439ecd79187SMathias Krause 	klen = xfrm_replay_state_esn_len(up);
4405e708e47SAlexey Dobriyan 	ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
441d8647b79SSteffen Klassert 
442ecd79187SMathias Krause 	p = kzalloc(klen, GFP_KERNEL);
443d8647b79SSteffen Klassert 	if (!p)
444d8647b79SSteffen Klassert 		return -ENOMEM;
445d8647b79SSteffen Klassert 
446ecd79187SMathias Krause 	pp = kzalloc(klen, GFP_KERNEL);
447d8647b79SSteffen Klassert 	if (!pp) {
448d8647b79SSteffen Klassert 		kfree(p);
449d8647b79SSteffen Klassert 		return -ENOMEM;
450d8647b79SSteffen Klassert 	}
451d8647b79SSteffen Klassert 
452ecd79187SMathias Krause 	memcpy(p, up, ulen);
453ecd79187SMathias Krause 	memcpy(pp, up, ulen);
454ecd79187SMathias Krause 
455d8647b79SSteffen Klassert 	*replay_esn = p;
456d8647b79SSteffen Klassert 	*preplay_esn = pp;
457d8647b79SSteffen Klassert 
458d8647b79SSteffen Klassert 	return 0;
459d8647b79SSteffen Klassert }
460d8647b79SSteffen Klassert 
461*a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
462df71837dSTrent Jaeger {
463*a1b831f2SAlexey Dobriyan 	unsigned int len = 0;
464df71837dSTrent Jaeger 
465df71837dSTrent Jaeger 	if (xfrm_ctx) {
466df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
467df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
468df71837dSTrent Jaeger 	}
469df71837dSTrent Jaeger 	return len;
470df71837dSTrent Jaeger }
471df71837dSTrent Jaeger 
4721da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
4731da177e4SLinus Torvalds {
4741da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
4751da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
4761da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
4771da177e4SLinus Torvalds 	x->props.mode = p->mode;
47833fce60dSFan Du 	x->props.replay_window = min_t(unsigned int, p->replay_window,
47933fce60dSFan Du 					sizeof(x->replay.bitmap) * 8);
4801da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
4811da177e4SLinus Torvalds 	x->props.family = p->family;
48254489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
4831da177e4SLinus Torvalds 	x->props.flags = p->flags;
484196b0036SHerbert Xu 
485ccf9b3b8SSteffen Klassert 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
486196b0036SHerbert Xu 		x->sel.family = p->family;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
489d51d081dSJamal Hadi Salim /*
490d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
491d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
492d51d081dSJamal Hadi Salim  *
493d51d081dSJamal Hadi Salim */
494e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
495e3ac104dSMathias Krause 				  int update_esn)
496d51d081dSJamal Hadi Salim {
4975424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
498e3ac104dSMathias Krause 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
4995424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
5005424f32eSThomas Graf 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
5015424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
502d51d081dSJamal Hadi Salim 
503d8647b79SSteffen Klassert 	if (re) {
504d8647b79SSteffen Klassert 		struct xfrm_replay_state_esn *replay_esn;
505d8647b79SSteffen Klassert 		replay_esn = nla_data(re);
506d8647b79SSteffen Klassert 		memcpy(x->replay_esn, replay_esn,
507d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
508d8647b79SSteffen Klassert 		memcpy(x->preplay_esn, replay_esn,
509d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
510d8647b79SSteffen Klassert 	}
511d8647b79SSteffen Klassert 
512d51d081dSJamal Hadi Salim 	if (rp) {
513d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
5145424f32eSThomas Graf 		replay = nla_data(rp);
515d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
516d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
517d51d081dSJamal Hadi Salim 	}
518d51d081dSJamal Hadi Salim 
519d51d081dSJamal Hadi Salim 	if (lt) {
520d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
5215424f32eSThomas Graf 		ltime = nla_data(lt);
522d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
523d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
524d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
525d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
526d51d081dSJamal Hadi Salim 	}
527d51d081dSJamal Hadi Salim 
528cf5cb79fSThomas Graf 	if (et)
5295424f32eSThomas Graf 		x->replay_maxage = nla_get_u32(et);
530d51d081dSJamal Hadi Salim 
531cf5cb79fSThomas Graf 	if (rt)
5325424f32eSThomas Graf 		x->replay_maxdiff = nla_get_u32(rt);
533d51d081dSJamal Hadi Salim }
534d51d081dSJamal Hadi Salim 
535fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
536fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
5375424f32eSThomas Graf 					       struct nlattr **attrs,
5381da177e4SLinus Torvalds 					       int *errp)
5391da177e4SLinus Torvalds {
540fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
5411da177e4SLinus Torvalds 	int err = -ENOMEM;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	if (!x)
5441da177e4SLinus Torvalds 		goto error_no_put;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	copy_from_user_state(x, p);
5471da177e4SLinus Torvalds 
548a947b0a9SNicolas Dichtel 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
549a947b0a9SNicolas Dichtel 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
550a947b0a9SNicolas Dichtel 
55169b0137fSHerbert Xu 	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
5521a6509d9SHerbert Xu 		goto error;
5534447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
5544447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
5554447bb33SMartin Willi 		goto error;
5564447bb33SMartin Willi 	if (!x->props.aalgo) {
5574447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
55835a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
5591da177e4SLinus Torvalds 			goto error;
5604447bb33SMartin Willi 	}
56169b0137fSHerbert Xu 	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
5621da177e4SLinus Torvalds 		goto error;
5631da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
5641da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
56535a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
5661da177e4SLinus Torvalds 		goto error;
567fd21150aSThomas Graf 
568fd21150aSThomas Graf 	if (attrs[XFRMA_ENCAP]) {
569fd21150aSThomas Graf 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
570fd21150aSThomas Graf 				   sizeof(*x->encap), GFP_KERNEL);
571fd21150aSThomas Graf 		if (x->encap == NULL)
5721da177e4SLinus Torvalds 			goto error;
573fd21150aSThomas Graf 	}
574fd21150aSThomas Graf 
57535d2856bSMartin Willi 	if (attrs[XFRMA_TFCPAD])
57635d2856bSMartin Willi 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
57735d2856bSMartin Willi 
578fd21150aSThomas Graf 	if (attrs[XFRMA_COADDR]) {
579fd21150aSThomas Graf 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
580fd21150aSThomas Graf 				    sizeof(*x->coaddr), GFP_KERNEL);
581fd21150aSThomas Graf 		if (x->coaddr == NULL)
582060f02a3SNoriaki TAKAMIYA 			goto error;
583fd21150aSThomas Graf 	}
584fd21150aSThomas Graf 
5856f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &x->mark);
5866f26b61eSJamal Hadi Salim 
587077fbac4SLorenzo Colitti 	if (attrs[XFRMA_OUTPUT_MARK])
588077fbac4SLorenzo Colitti 		x->props.output_mark = nla_get_u32(attrs[XFRMA_OUTPUT_MARK]);
589077fbac4SLorenzo Colitti 
590ffdb5211SIlan Tayari 	err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
5911da177e4SLinus Torvalds 	if (err)
5921da177e4SLinus Torvalds 		goto error;
5931da177e4SLinus Torvalds 
5942f30ea50SMathias Krause 	if (attrs[XFRMA_SEC_CTX]) {
5952f30ea50SMathias Krause 		err = security_xfrm_state_alloc(x,
5962f30ea50SMathias Krause 						nla_data(attrs[XFRMA_SEC_CTX]));
5972f30ea50SMathias Krause 		if (err)
598df71837dSTrent Jaeger 			goto error;
5992f30ea50SMathias Krause 	}
600df71837dSTrent Jaeger 
601152afb9bSIlan Tayari 	if (attrs[XFRMA_OFFLOAD_DEV]) {
602152afb9bSIlan Tayari 		err = xfrm_dev_state_add(net, x,
603152afb9bSIlan Tayari 					 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
604152afb9bSIlan Tayari 		if (err)
605d77e38e6SSteffen Klassert 			goto error;
606152afb9bSIlan Tayari 	}
607d77e38e6SSteffen Klassert 
608d8647b79SSteffen Klassert 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
609d8647b79SSteffen Klassert 					       attrs[XFRMA_REPLAY_ESN_VAL])))
610d8647b79SSteffen Klassert 		goto error;
611d8647b79SSteffen Klassert 
6121da177e4SLinus Torvalds 	x->km.seq = p->seq;
613b27aeadbSAlexey Dobriyan 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
614d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
615b27aeadbSAlexey Dobriyan 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
616d51d081dSJamal Hadi Salim 
6179fdc4883SSteffen Klassert 	if ((err = xfrm_init_replay(x)))
6189fdc4883SSteffen Klassert 		goto error;
619d51d081dSJamal Hadi Salim 
6209fdc4883SSteffen Klassert 	/* override default values from above */
621e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 0);
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	return x;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds error:
6261da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
6271da177e4SLinus Torvalds 	xfrm_state_put(x);
6281da177e4SLinus Torvalds error_no_put:
6291da177e4SLinus Torvalds 	*errp = err;
6301da177e4SLinus Torvalds 	return NULL;
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
63322e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
6345424f32eSThomas Graf 		struct nlattr **attrs)
6351da177e4SLinus Torvalds {
636fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6377b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
6381da177e4SLinus Torvalds 	struct xfrm_state *x;
6391da177e4SLinus Torvalds 	int err;
64026b15dadSJamal Hadi Salim 	struct km_event c;
6411da177e4SLinus Torvalds 
64235a7aa08SThomas Graf 	err = verify_newsa_info(p, attrs);
6431da177e4SLinus Torvalds 	if (err)
6441da177e4SLinus Torvalds 		return err;
6451da177e4SLinus Torvalds 
646fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
6471da177e4SLinus Torvalds 	if (!x)
6481da177e4SLinus Torvalds 		return err;
6491da177e4SLinus Torvalds 
65026b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
6511da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
6521da177e4SLinus Torvalds 		err = xfrm_state_add(x);
6531da177e4SLinus Torvalds 	else
6541da177e4SLinus Torvalds 		err = xfrm_state_update(x);
6551da177e4SLinus Torvalds 
6562e71029eSTetsuo Handa 	xfrm_audit_state_add(x, err ? 0 : 1, true);
657161a09e7SJoy Latten 
6581da177e4SLinus Torvalds 	if (err < 0) {
6591da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
66021380b81SHerbert Xu 		__xfrm_state_put(x);
6617d6dfe1fSPatrick McHardy 		goto out;
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 
66426b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
66515e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
666f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
66726b15dadSJamal Hadi Salim 
66826b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
6697d6dfe1fSPatrick McHardy out:
67026b15dadSJamal Hadi Salim 	xfrm_state_put(x);
6711da177e4SLinus Torvalds 	return err;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
674fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
675fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
6765424f32eSThomas Graf 						 struct nlattr **attrs,
677eb2971b6SMasahide NAKAMURA 						 int *errp)
678eb2971b6SMasahide NAKAMURA {
679eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
6806f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
681eb2971b6SMasahide NAKAMURA 	int err;
6826f26b61eSJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
683eb2971b6SMasahide NAKAMURA 
684eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
685eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
6866f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
687eb2971b6SMasahide NAKAMURA 	} else {
688eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
689eb2971b6SMasahide NAKAMURA 
69035a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
691eb2971b6SMasahide NAKAMURA 		if (!saddr) {
692eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
693eb2971b6SMasahide NAKAMURA 			goto out;
694eb2971b6SMasahide NAKAMURA 		}
695eb2971b6SMasahide NAKAMURA 
6969abbffeeSMasahide NAKAMURA 		err = -ESRCH;
6976f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup_byaddr(net, mark,
6986f26b61eSJamal Hadi Salim 					     &p->daddr, saddr,
699221df1edSAlexey Dobriyan 					     p->proto, p->family);
700eb2971b6SMasahide NAKAMURA 	}
701eb2971b6SMasahide NAKAMURA 
702eb2971b6SMasahide NAKAMURA  out:
703eb2971b6SMasahide NAKAMURA 	if (!x && errp)
704eb2971b6SMasahide NAKAMURA 		*errp = err;
705eb2971b6SMasahide NAKAMURA 	return x;
706eb2971b6SMasahide NAKAMURA }
707eb2971b6SMasahide NAKAMURA 
70822e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
7095424f32eSThomas Graf 		struct nlattr **attrs)
7101da177e4SLinus Torvalds {
711fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
7121da177e4SLinus Torvalds 	struct xfrm_state *x;
713eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
71426b15dadSJamal Hadi Salim 	struct km_event c;
7157b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
7161da177e4SLinus Torvalds 
717fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
7181da177e4SLinus Torvalds 	if (x == NULL)
719eb2971b6SMasahide NAKAMURA 		return err;
7201da177e4SLinus Torvalds 
7216f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
722c8c05a8eSCatherine Zhang 		goto out;
723c8c05a8eSCatherine Zhang 
7241da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
725c8c05a8eSCatherine Zhang 		err = -EPERM;
726c8c05a8eSCatherine Zhang 		goto out;
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 
72926b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
730161a09e7SJoy Latten 
731c8c05a8eSCatherine Zhang 	if (err < 0)
732c8c05a8eSCatherine Zhang 		goto out;
73326b15dadSJamal Hadi Salim 
73426b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
73515e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
736f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
73726b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7381da177e4SLinus Torvalds 
739c8c05a8eSCatherine Zhang out:
7402e71029eSTetsuo Handa 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
741c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
74226b15dadSJamal Hadi Salim 	return err;
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
7461da177e4SLinus Torvalds {
747f778a636SMathias Krause 	memset(p, 0, sizeof(*p));
7481da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
7491da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
7501da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
7511da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
752e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
753e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay, &p->stats.replay);
754e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
75554489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
7561da177e4SLinus Torvalds 	p->mode = x->props.mode;
7571da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
7581da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
7591da177e4SLinus Torvalds 	p->family = x->props.family;
7601da177e4SLinus Torvalds 	p->flags = x->props.flags;
7611da177e4SLinus Torvalds 	p->seq = x->km.seq;
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds struct xfrm_dump_info {
7651da177e4SLinus Torvalds 	struct sk_buff *in_skb;
7661da177e4SLinus Torvalds 	struct sk_buff *out_skb;
7671da177e4SLinus Torvalds 	u32 nlmsg_seq;
7681da177e4SLinus Torvalds 	u16 nlmsg_flags;
7691da177e4SLinus Torvalds };
7701da177e4SLinus Torvalds 
771c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
772c0144beaSThomas Graf {
773c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
774c0144beaSThomas Graf 	struct nlattr *attr;
77568325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
776c0144beaSThomas Graf 
777c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
778c0144beaSThomas Graf 	if (attr == NULL)
779c0144beaSThomas Graf 		return -EMSGSIZE;
780c0144beaSThomas Graf 
781c0144beaSThomas Graf 	uctx = nla_data(attr);
782c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
783c0144beaSThomas Graf 	uctx->len = ctx_size;
784c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
785c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
786c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
787c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
788c0144beaSThomas Graf 
789c0144beaSThomas Graf 	return 0;
790c0144beaSThomas Graf }
791c0144beaSThomas Graf 
792d77e38e6SSteffen Klassert static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
793d77e38e6SSteffen Klassert {
794d77e38e6SSteffen Klassert 	struct xfrm_user_offload *xuo;
795d77e38e6SSteffen Klassert 	struct nlattr *attr;
796d77e38e6SSteffen Klassert 
797d77e38e6SSteffen Klassert 	attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
798d77e38e6SSteffen Klassert 	if (attr == NULL)
799d77e38e6SSteffen Klassert 		return -EMSGSIZE;
800d77e38e6SSteffen Klassert 
801d77e38e6SSteffen Klassert 	xuo = nla_data(attr);
8025fe0d4bdSMathias Krause 	memset(xuo, 0, sizeof(*xuo));
803d77e38e6SSteffen Klassert 	xuo->ifindex = xso->dev->ifindex;
804d77e38e6SSteffen Klassert 	xuo->flags = xso->flags;
805d77e38e6SSteffen Klassert 
806d77e38e6SSteffen Klassert 	return 0;
807d77e38e6SSteffen Klassert }
808d77e38e6SSteffen Klassert 
8094447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
8104447bb33SMartin Willi {
8114447bb33SMartin Willi 	struct xfrm_algo *algo;
8124447bb33SMartin Willi 	struct nlattr *nla;
8134447bb33SMartin Willi 
8144447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
8154447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
8164447bb33SMartin Willi 	if (!nla)
8174447bb33SMartin Willi 		return -EMSGSIZE;
8184447bb33SMartin Willi 
8194447bb33SMartin Willi 	algo = nla_data(nla);
8204c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
8214447bb33SMartin Willi 	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
8224447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
8234447bb33SMartin Willi 
8244447bb33SMartin Willi 	return 0;
8254447bb33SMartin Willi }
8264447bb33SMartin Willi 
82768325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
82868325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
82968325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
83068325d3bSHerbert Xu 				    struct sk_buff *skb)
8311da177e4SLinus Torvalds {
8321d1e34ddSDavid S. Miller 	int ret = 0;
8331d1e34ddSDavid S. Miller 
8341da177e4SLinus Torvalds 	copy_to_user_state(x, p);
8351da177e4SLinus Torvalds 
836a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
837a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
838a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
839a947b0a9SNicolas Dichtel 		if (ret)
840a947b0a9SNicolas Dichtel 			goto out;
841a947b0a9SNicolas Dichtel 	}
842a947b0a9SNicolas Dichtel 
8431d1e34ddSDavid S. Miller 	if (x->coaddr) {
8441d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
8451d1e34ddSDavid S. Miller 		if (ret)
8461d1e34ddSDavid S. Miller 			goto out;
8471d1e34ddSDavid S. Miller 	}
8481d1e34ddSDavid S. Miller 	if (x->lastused) {
849de95c4a4SNicolas Dichtel 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
850de95c4a4SNicolas Dichtel 					XFRMA_PAD);
8511d1e34ddSDavid S. Miller 		if (ret)
8521d1e34ddSDavid S. Miller 			goto out;
8531d1e34ddSDavid S. Miller 	}
8541d1e34ddSDavid S. Miller 	if (x->aead) {
8551d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
8561d1e34ddSDavid S. Miller 		if (ret)
8571d1e34ddSDavid S. Miller 			goto out;
8581d1e34ddSDavid S. Miller 	}
8591d1e34ddSDavid S. Miller 	if (x->aalg) {
8601d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
8611d1e34ddSDavid S. Miller 		if (!ret)
8621d1e34ddSDavid S. Miller 			ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
8631d1e34ddSDavid S. Miller 				      xfrm_alg_auth_len(x->aalg), x->aalg);
8641d1e34ddSDavid S. Miller 		if (ret)
8651d1e34ddSDavid S. Miller 			goto out;
8661d1e34ddSDavid S. Miller 	}
8671d1e34ddSDavid S. Miller 	if (x->ealg) {
8681d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
8691d1e34ddSDavid S. Miller 		if (ret)
8701d1e34ddSDavid S. Miller 			goto out;
8711d1e34ddSDavid S. Miller 	}
8721d1e34ddSDavid S. Miller 	if (x->calg) {
8731d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
8741d1e34ddSDavid S. Miller 		if (ret)
8751d1e34ddSDavid S. Miller 			goto out;
8761d1e34ddSDavid S. Miller 	}
8771d1e34ddSDavid S. Miller 	if (x->encap) {
8781d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
8791d1e34ddSDavid S. Miller 		if (ret)
8801d1e34ddSDavid S. Miller 			goto out;
8811d1e34ddSDavid S. Miller 	}
8821d1e34ddSDavid S. Miller 	if (x->tfcpad) {
8831d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
8841d1e34ddSDavid S. Miller 		if (ret)
8851d1e34ddSDavid S. Miller 			goto out;
8861d1e34ddSDavid S. Miller 	}
8871d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
8881d1e34ddSDavid S. Miller 	if (ret)
8891d1e34ddSDavid S. Miller 		goto out;
890f293a5e3Sdingzhi 	if (x->replay_esn)
8911d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
892d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
8931d1e34ddSDavid S. Miller 			      x->replay_esn);
894f293a5e3Sdingzhi 	else
895f293a5e3Sdingzhi 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
896f293a5e3Sdingzhi 			      &x->replay);
8971d1e34ddSDavid S. Miller 	if (ret)
8981d1e34ddSDavid S. Miller 		goto out;
899d77e38e6SSteffen Klassert 	if(x->xso.dev)
900d77e38e6SSteffen Klassert 		ret = copy_user_offload(&x->xso, skb);
901d77e38e6SSteffen Klassert 	if (ret)
902d77e38e6SSteffen Klassert 		goto out;
903077fbac4SLorenzo Colitti 	if (x->props.output_mark) {
904077fbac4SLorenzo Colitti 		ret = nla_put_u32(skb, XFRMA_OUTPUT_MARK, x->props.output_mark);
905077fbac4SLorenzo Colitti 		if (ret)
906077fbac4SLorenzo Colitti 			goto out;
907077fbac4SLorenzo Colitti 	}
9088598112dSSteffen Klassert 	if (x->security)
9098598112dSSteffen Klassert 		ret = copy_sec_ctx(x->security, skb);
9101d1e34ddSDavid S. Miller out:
9111d1e34ddSDavid S. Miller 	return ret;
91268325d3bSHerbert Xu }
91368325d3bSHerbert Xu 
91468325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
91568325d3bSHerbert Xu {
91668325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
91768325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
91868325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
91968325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
92068325d3bSHerbert Xu 	struct nlmsghdr *nlh;
92168325d3bSHerbert Xu 	int err;
92268325d3bSHerbert Xu 
92315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
92468325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
92568325d3bSHerbert Xu 	if (nlh == NULL)
92668325d3bSHerbert Xu 		return -EMSGSIZE;
92768325d3bSHerbert Xu 
92868325d3bSHerbert Xu 	p = nlmsg_data(nlh);
92968325d3bSHerbert Xu 
93068325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
9311d1e34ddSDavid S. Miller 	if (err) {
9329825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
93368325d3bSHerbert Xu 		return err;
9341da177e4SLinus Torvalds 	}
9351d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
9361d1e34ddSDavid S. Miller 	return 0;
9371d1e34ddSDavid S. Miller }
9381da177e4SLinus Torvalds 
9394c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
9404c563f76STimo Teras {
9414c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
942283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
943283bc9f3SFan Du 	struct net *net = sock_net(sk);
944283bc9f3SFan Du 
9451ba5bf99SVegard Nossum 	if (cb->args[0])
946283bc9f3SFan Du 		xfrm_state_walk_done(walk, net);
9474c563f76STimo Teras 	return 0;
9484c563f76STimo Teras }
9494c563f76STimo Teras 
950d3623099SNicolas Dichtel static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
9511da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
9521da177e4SLinus Torvalds {
953fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
9544c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
9551da177e4SLinus Torvalds 	struct xfrm_dump_info info;
9561da177e4SLinus Torvalds 
9574c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
9584c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
9594c563f76STimo Teras 
9601da177e4SLinus Torvalds 	info.in_skb = cb->skb;
9611da177e4SLinus Torvalds 	info.out_skb = skb;
9621da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
9631da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
9644c563f76STimo Teras 
9654c563f76STimo Teras 	if (!cb->args[0]) {
966d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
967870a2df4SNicolas Dichtel 		struct xfrm_address_filter *filter = NULL;
968d3623099SNicolas Dichtel 		u8 proto = 0;
969d3623099SNicolas Dichtel 		int err;
970d3623099SNicolas Dichtel 
971fceb6435SJohannes Berg 		err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX, xfrma_policy,
972fceb6435SJohannes Berg 				  NULL);
973d3623099SNicolas Dichtel 		if (err < 0)
974d3623099SNicolas Dichtel 			return err;
975d3623099SNicolas Dichtel 
976870a2df4SNicolas Dichtel 		if (attrs[XFRMA_ADDRESS_FILTER]) {
977df367561SAndrzej Hajda 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
978df367561SAndrzej Hajda 					 sizeof(*filter), GFP_KERNEL);
979d3623099SNicolas Dichtel 			if (filter == NULL)
980d3623099SNicolas Dichtel 				return -ENOMEM;
981d3623099SNicolas Dichtel 		}
982d3623099SNicolas Dichtel 
983d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
984d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
985d3623099SNicolas Dichtel 
986d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
9871ba5bf99SVegard Nossum 		cb->args[0] = 1;
9884c563f76STimo Teras 	}
9894c563f76STimo Teras 
990fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	return skb->len;
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
9961da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
9971da177e4SLinus Torvalds {
9981da177e4SLinus Torvalds 	struct xfrm_dump_info info;
9991da177e4SLinus Torvalds 	struct sk_buff *skb;
1000864745d2SMathias Krause 	int err;
10011da177e4SLinus Torvalds 
10027deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
10031da177e4SLinus Torvalds 	if (!skb)
10041da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	info.in_skb = in_skb;
10071da177e4SLinus Torvalds 	info.out_skb = skb;
10081da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
10091da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
10101da177e4SLinus Torvalds 
1011864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
1012864745d2SMathias Krause 	if (err) {
10131da177e4SLinus Torvalds 		kfree_skb(skb);
1014864745d2SMathias Krause 		return ERR_PTR(err);
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	return skb;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
102021ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
102121ee543eSMichal Kubecek  * Must be called with RCU read lock.
102221ee543eSMichal Kubecek  */
102321ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
102421ee543eSMichal Kubecek 				       u32 pid, unsigned int group)
102521ee543eSMichal Kubecek {
102621ee543eSMichal Kubecek 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
102721ee543eSMichal Kubecek 
102821ee543eSMichal Kubecek 	if (nlsk)
102921ee543eSMichal Kubecek 		return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
103021ee543eSMichal Kubecek 	else
103121ee543eSMichal Kubecek 		return -1;
103221ee543eSMichal Kubecek }
103321ee543eSMichal Kubecek 
1034*a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void)
10357deb2264SThomas Graf {
10367deb2264SThomas Graf 	return NLMSG_ALIGN(4)
10377deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1038880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1039880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1040880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
10417deb2264SThomas Graf }
10427deb2264SThomas Graf 
1043e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
104415e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
1045ecfd6b18SJamal Hadi Salim {
10465a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
10475a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
10485a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1049880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1050ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
10511d1e34ddSDavid S. Miller 	int err;
1052ecfd6b18SJamal Hadi Salim 	u32 *f;
1053880a6fabSChristophe Gouault 	unsigned lseq;
1054ecfd6b18SJamal Hadi Salim 
105515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
105625985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1057ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1058ecfd6b18SJamal Hadi Salim 
1059ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1060ecfd6b18SJamal Hadi Salim 	*f = flags;
1061e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
10625a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
10635a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
10645a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
10655a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
10665a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
10675a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
10685a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
10695a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1070ecfd6b18SJamal Hadi Salim 
1071880a6fabSChristophe Gouault 	do {
1072880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1073880a6fabSChristophe Gouault 
1074880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1075880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1076880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1077880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1078880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1079880a6fabSChristophe Gouault 
10801d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
10811d1e34ddSDavid S. Miller 	if (!err)
10821d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1083880a6fabSChristophe Gouault 	if (!err)
1084880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1085880a6fabSChristophe Gouault 	if (!err)
1086880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
10871d1e34ddSDavid S. Miller 	if (err) {
10881d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
10891d1e34ddSDavid S. Miller 		return err;
10901d1e34ddSDavid S. Miller 	}
1091ecfd6b18SJamal Hadi Salim 
1092053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1093053c095aSJohannes Berg 	return 0;
1094ecfd6b18SJamal Hadi Salim }
1095ecfd6b18SJamal Hadi Salim 
1096880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1097880a6fabSChristophe Gouault 			    struct nlattr **attrs)
1098880a6fabSChristophe Gouault {
1099880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1100880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1101880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1102880a6fabSChristophe Gouault 
1103880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1104880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1105880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1106880a6fabSChristophe Gouault 
1107880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1108880a6fabSChristophe Gouault 			return -EINVAL;
1109880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1110880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1111880a6fabSChristophe Gouault 			return -EINVAL;
1112880a6fabSChristophe Gouault 	}
1113880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1114880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1115880a6fabSChristophe Gouault 
1116880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1117880a6fabSChristophe Gouault 			return -EINVAL;
1118880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1119880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1120880a6fabSChristophe Gouault 			return -EINVAL;
1121880a6fabSChristophe Gouault 	}
1122880a6fabSChristophe Gouault 
1123880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1124880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1125880a6fabSChristophe Gouault 		if (thresh4) {
1126880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1127880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1128880a6fabSChristophe Gouault 		}
1129880a6fabSChristophe Gouault 		if (thresh6) {
1130880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1131880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1132880a6fabSChristophe Gouault 		}
1133880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1134880a6fabSChristophe Gouault 
1135880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1136880a6fabSChristophe Gouault 	}
1137880a6fabSChristophe Gouault 
1138880a6fabSChristophe Gouault 	return 0;
1139880a6fabSChristophe Gouault }
1140880a6fabSChristophe Gouault 
1141ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
11425424f32eSThomas Graf 		struct nlattr **attrs)
1143ecfd6b18SJamal Hadi Salim {
1144a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1145ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
11467b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
114715e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1148ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
1149ecfd6b18SJamal Hadi Salim 
11507deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1151ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1152ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1153ecfd6b18SJamal Hadi Salim 
115415e47304SEric W. Biederman 	if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1155ecfd6b18SJamal Hadi Salim 		BUG();
1156ecfd6b18SJamal Hadi Salim 
115715e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1158ecfd6b18SJamal Hadi Salim }
1159ecfd6b18SJamal Hadi Salim 
1160*a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void)
11617deb2264SThomas Graf {
11627deb2264SThomas Graf 	return NLMSG_ALIGN(4)
11637deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
11647deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
11657deb2264SThomas Graf }
11667deb2264SThomas Graf 
1167e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
116815e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
116928d8909bSJamal Hadi Salim {
1170af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1171af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
117228d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
11731d1e34ddSDavid S. Miller 	int err;
117428d8909bSJamal Hadi Salim 	u32 *f;
117528d8909bSJamal Hadi Salim 
117615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
117725985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
117828d8909bSJamal Hadi Salim 		return -EMSGSIZE;
117928d8909bSJamal Hadi Salim 
118028d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
118128d8909bSJamal Hadi Salim 	*f = flags;
1182e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
118328d8909bSJamal Hadi Salim 
1184af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1185af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1186af11e316SJamal Hadi Salim 
11871d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
11881d1e34ddSDavid S. Miller 	if (!err)
11891d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
11901d1e34ddSDavid S. Miller 	if (err) {
11911d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
11921d1e34ddSDavid S. Miller 		return err;
11931d1e34ddSDavid S. Miller 	}
119428d8909bSJamal Hadi Salim 
1195053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1196053c095aSJohannes Berg 	return 0;
119728d8909bSJamal Hadi Salim }
119828d8909bSJamal Hadi Salim 
119928d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12005424f32eSThomas Graf 		struct nlattr **attrs)
120128d8909bSJamal Hadi Salim {
1202a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
120328d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
12047b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
120515e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
120628d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
120728d8909bSJamal Hadi Salim 
12087deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
120928d8909bSJamal Hadi Salim 	if (r_skb == NULL)
121028d8909bSJamal Hadi Salim 		return -ENOMEM;
121128d8909bSJamal Hadi Salim 
121215e47304SEric W. Biederman 	if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
121328d8909bSJamal Hadi Salim 		BUG();
121428d8909bSJamal Hadi Salim 
121515e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
121628d8909bSJamal Hadi Salim }
121728d8909bSJamal Hadi Salim 
121822e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
12195424f32eSThomas Graf 		struct nlattr **attrs)
12201da177e4SLinus Torvalds {
1221fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12227b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
12231da177e4SLinus Torvalds 	struct xfrm_state *x;
12241da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1225eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
12261da177e4SLinus Torvalds 
1227fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
12281da177e4SLinus Torvalds 	if (x == NULL)
12291da177e4SLinus Torvalds 		goto out_noput;
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
12321da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
12331da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
12341da177e4SLinus Torvalds 	} else {
123515e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
12361da177e4SLinus Torvalds 	}
12371da177e4SLinus Torvalds 	xfrm_state_put(x);
12381da177e4SLinus Torvalds out_noput:
12391da177e4SLinus Torvalds 	return err;
12401da177e4SLinus Torvalds }
12411da177e4SLinus Torvalds 
124222e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
12435424f32eSThomas Graf 		struct nlattr **attrs)
12441da177e4SLinus Torvalds {
1245fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12461da177e4SLinus Torvalds 	struct xfrm_state *x;
12471da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
12481da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
12491da177e4SLinus Torvalds 	xfrm_address_t *daddr;
12501da177e4SLinus Torvalds 	int family;
12511da177e4SLinus Torvalds 	int err;
12526f26b61eSJamal Hadi Salim 	u32 mark;
12536f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
12541da177e4SLinus Torvalds 
12557b67c857SThomas Graf 	p = nlmsg_data(nlh);
1256776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
12571da177e4SLinus Torvalds 	if (err)
12581da177e4SLinus Torvalds 		goto out_noput;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	family = p->info.family;
12611da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	x = NULL;
12646f26b61eSJamal Hadi Salim 
12656f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
12661da177e4SLinus Torvalds 	if (p->info.seq) {
12676f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
126870e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
12691da177e4SLinus Torvalds 			xfrm_state_put(x);
12701da177e4SLinus Torvalds 			x = NULL;
12711da177e4SLinus Torvalds 		}
12721da177e4SLinus Torvalds 	}
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	if (!x)
12756f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
12761da177e4SLinus Torvalds 				  p->info.id.proto, daddr,
12771da177e4SLinus Torvalds 				  &p->info.saddr, 1,
12781da177e4SLinus Torvalds 				  family);
12791da177e4SLinus Torvalds 	err = -ENOENT;
12801da177e4SLinus Torvalds 	if (x == NULL)
12811da177e4SLinus Torvalds 		goto out_noput;
12821da177e4SLinus Torvalds 
1283658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1284658b219eSHerbert Xu 	if (err)
1285658b219eSHerbert Xu 		goto out;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
12881da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
12891da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
12901da177e4SLinus Torvalds 		goto out;
12911da177e4SLinus Torvalds 	}
12921da177e4SLinus Torvalds 
129315e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds out:
12961da177e4SLinus Torvalds 	xfrm_state_put(x);
12971da177e4SLinus Torvalds out_noput:
12981da177e4SLinus Torvalds 	return err;
12991da177e4SLinus Torvalds }
13001da177e4SLinus Torvalds 
1301b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
13021da177e4SLinus Torvalds {
13031da177e4SLinus Torvalds 	switch (dir) {
13041da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
13051da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
13061da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
13071da177e4SLinus Torvalds 		break;
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	default:
13101da177e4SLinus Torvalds 		return -EINVAL;
13113ff50b79SStephen Hemminger 	}
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	return 0;
13141da177e4SLinus Torvalds }
13151da177e4SLinus Torvalds 
1316b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1317f7b6983fSMasahide NAKAMURA {
1318f7b6983fSMasahide NAKAMURA 	switch (type) {
1319f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1320f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1321f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1322f7b6983fSMasahide NAKAMURA #endif
1323f7b6983fSMasahide NAKAMURA 		break;
1324f7b6983fSMasahide NAKAMURA 
1325f7b6983fSMasahide NAKAMURA 	default:
1326f7b6983fSMasahide NAKAMURA 		return -EINVAL;
13273ff50b79SStephen Hemminger 	}
1328f7b6983fSMasahide NAKAMURA 
1329f7b6983fSMasahide NAKAMURA 	return 0;
1330f7b6983fSMasahide NAKAMURA }
1331f7b6983fSMasahide NAKAMURA 
13321da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
13331da177e4SLinus Torvalds {
1334e682adf0SFan Du 	int ret;
1335e682adf0SFan Du 
13361da177e4SLinus Torvalds 	switch (p->share) {
13371da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
13381da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
13391da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
13401da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
13411da177e4SLinus Torvalds 		break;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	default:
13441da177e4SLinus Torvalds 		return -EINVAL;
13453ff50b79SStephen Hemminger 	}
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	switch (p->action) {
13481da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
13491da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
13501da177e4SLinus Torvalds 		break;
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 	default:
13531da177e4SLinus Torvalds 		return -EINVAL;
13543ff50b79SStephen Hemminger 	}
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	switch (p->sel.family) {
13571da177e4SLinus Torvalds 	case AF_INET:
13581da177e4SLinus Torvalds 		break;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	case AF_INET6:
1361dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
13621da177e4SLinus Torvalds 		break;
13631da177e4SLinus Torvalds #else
13641da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
13651da177e4SLinus Torvalds #endif
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	default:
13681da177e4SLinus Torvalds 		return -EINVAL;
13693ff50b79SStephen Hemminger 	}
13701da177e4SLinus Torvalds 
1371e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1372e682adf0SFan Du 	if (ret)
1373e682adf0SFan Du 		return ret;
1374e682adf0SFan Du 	if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1375e682adf0SFan Du 		return -EINVAL;
1376e682adf0SFan Du 
1377e682adf0SFan Du 	return 0;
13781da177e4SLinus Torvalds }
13791da177e4SLinus Torvalds 
13805424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1381df71837dSTrent Jaeger {
13825424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1383df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1384df71837dSTrent Jaeger 
1385df71837dSTrent Jaeger 	if (!rt)
1386df71837dSTrent Jaeger 		return 0;
1387df71837dSTrent Jaeger 
13885424f32eSThomas Graf 	uctx = nla_data(rt);
138952a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1390df71837dSTrent Jaeger }
1391df71837dSTrent Jaeger 
13921da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
13931da177e4SLinus Torvalds 			   int nr)
13941da177e4SLinus Torvalds {
13951da177e4SLinus Torvalds 	int i;
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
13981da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
13991da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
14021da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
14031da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
14041da177e4SLinus Torvalds 		t->reqid = ut->reqid;
14051da177e4SLinus Torvalds 		t->mode = ut->mode;
14061da177e4SLinus Torvalds 		t->share = ut->share;
14071da177e4SLinus Torvalds 		t->optional = ut->optional;
14081da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
14091da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
14101da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1411c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1412c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
14138511d01dSMiika Komu 		t->encap_family = ut->family;
14141da177e4SLinus Torvalds 	}
14151da177e4SLinus Torvalds }
14161da177e4SLinus Torvalds 
1417b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1418b4ad86bfSDavid S. Miller {
1419b4ad86bfSDavid S. Miller 	int i;
1420b4ad86bfSDavid S. Miller 
1421b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1422b4ad86bfSDavid S. Miller 		return -EINVAL;
1423b4ad86bfSDavid S. Miller 
1424b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1425b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1426b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1427b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1428b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1429b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1430b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1431b4ad86bfSDavid S. Miller 		 */
1432b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1433b4ad86bfSDavid S. Miller 			ut[i].family = family;
1434b4ad86bfSDavid S. Miller 
1435b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1436b4ad86bfSDavid S. Miller 		case AF_INET:
1437b4ad86bfSDavid S. Miller 			break;
1438dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1439b4ad86bfSDavid S. Miller 		case AF_INET6:
1440b4ad86bfSDavid S. Miller 			break;
1441b4ad86bfSDavid S. Miller #endif
1442b4ad86bfSDavid S. Miller 		default:
1443b4ad86bfSDavid S. Miller 			return -EINVAL;
14443ff50b79SStephen Hemminger 		}
1445b4ad86bfSDavid S. Miller 	}
1446b4ad86bfSDavid S. Miller 
1447b4ad86bfSDavid S. Miller 	return 0;
1448b4ad86bfSDavid S. Miller }
1449b4ad86bfSDavid S. Miller 
14505424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
14511da177e4SLinus Torvalds {
14525424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	if (!rt) {
14551da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
14561da177e4SLinus Torvalds 	} else {
14575424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
14585424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1459b4ad86bfSDavid S. Miller 		int err;
14601da177e4SLinus Torvalds 
1461b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1462b4ad86bfSDavid S. Miller 		if (err)
1463b4ad86bfSDavid S. Miller 			return err;
14641da177e4SLinus Torvalds 
14655424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
14661da177e4SLinus Torvalds 	}
14671da177e4SLinus Torvalds 	return 0;
14681da177e4SLinus Torvalds }
14691da177e4SLinus Torvalds 
14705424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1471f7b6983fSMasahide NAKAMURA {
14725424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1473f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1474b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1475f7b6983fSMasahide NAKAMURA 	int err;
1476f7b6983fSMasahide NAKAMURA 
1477f7b6983fSMasahide NAKAMURA 	if (rt) {
14785424f32eSThomas Graf 		upt = nla_data(rt);
1479f7b6983fSMasahide NAKAMURA 		type = upt->type;
1480f7b6983fSMasahide NAKAMURA 	}
1481f7b6983fSMasahide NAKAMURA 
1482f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1483f7b6983fSMasahide NAKAMURA 	if (err)
1484f7b6983fSMasahide NAKAMURA 		return err;
1485f7b6983fSMasahide NAKAMURA 
1486f7b6983fSMasahide NAKAMURA 	*tp = type;
1487f7b6983fSMasahide NAKAMURA 	return 0;
1488f7b6983fSMasahide NAKAMURA }
1489f7b6983fSMasahide NAKAMURA 
14901da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
14911da177e4SLinus Torvalds {
14921da177e4SLinus Torvalds 	xp->priority = p->priority;
14931da177e4SLinus Torvalds 	xp->index = p->index;
14941da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
14951da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
14961da177e4SLinus Torvalds 	xp->action = p->action;
14971da177e4SLinus Torvalds 	xp->flags = p->flags;
14981da177e4SLinus Torvalds 	xp->family = p->sel.family;
14991da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
15001da177e4SLinus Torvalds }
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
15031da177e4SLinus Torvalds {
15047b789836SMathias Krause 	memset(p, 0, sizeof(*p));
15051da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
15061da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
15071da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
15081da177e4SLinus Torvalds 	p->priority = xp->priority;
15091da177e4SLinus Torvalds 	p->index = xp->index;
15101da177e4SLinus Torvalds 	p->sel.family = xp->family;
15111da177e4SLinus Torvalds 	p->dir = dir;
15121da177e4SLinus Torvalds 	p->action = xp->action;
15131da177e4SLinus Torvalds 	p->flags = xp->flags;
15141da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
15151da177e4SLinus Torvalds }
15161da177e4SLinus Torvalds 
1517fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
15181da177e4SLinus Torvalds {
1519fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
15201da177e4SLinus Torvalds 	int err;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	if (!xp) {
15231da177e4SLinus Torvalds 		*errp = -ENOMEM;
15241da177e4SLinus Torvalds 		return NULL;
15251da177e4SLinus Torvalds 	}
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1528df71837dSTrent Jaeger 
152935a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1530f7b6983fSMasahide NAKAMURA 	if (err)
1531f7b6983fSMasahide NAKAMURA 		goto error;
1532f7b6983fSMasahide NAKAMURA 
153335a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
153435a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1535f7b6983fSMasahide NAKAMURA 	if (err)
1536f7b6983fSMasahide NAKAMURA 		goto error;
15371da177e4SLinus Torvalds 
1538295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1539295fae56SJamal Hadi Salim 
15401da177e4SLinus Torvalds 	return xp;
1541f7b6983fSMasahide NAKAMURA  error:
1542f7b6983fSMasahide NAKAMURA 	*errp = err;
154312a169e7SHerbert Xu 	xp->walk.dead = 1;
154464c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1545f7b6983fSMasahide NAKAMURA 	return NULL;
15461da177e4SLinus Torvalds }
15471da177e4SLinus Torvalds 
154822e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
15495424f32eSThomas Graf 		struct nlattr **attrs)
15501da177e4SLinus Torvalds {
1551fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
15527b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
15531da177e4SLinus Torvalds 	struct xfrm_policy *xp;
155426b15dadSJamal Hadi Salim 	struct km_event c;
15551da177e4SLinus Torvalds 	int err;
15561da177e4SLinus Torvalds 	int excl;
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
15591da177e4SLinus Torvalds 	if (err)
15601da177e4SLinus Torvalds 		return err;
156135a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1562df71837dSTrent Jaeger 	if (err)
1563df71837dSTrent Jaeger 		return err;
15641da177e4SLinus Torvalds 
1565fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
15661da177e4SLinus Torvalds 	if (!xp)
15671da177e4SLinus Torvalds 		return err;
15681da177e4SLinus Torvalds 
156925985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
157026b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
157126b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
157226b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
15731da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
15741da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
15752e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1576161a09e7SJoy Latten 
15771da177e4SLinus Torvalds 	if (err) {
157803e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
15791da177e4SLinus Torvalds 		kfree(xp);
15801da177e4SLinus Torvalds 		return err;
15811da177e4SLinus Torvalds 	}
15821da177e4SLinus Torvalds 
1583f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
158426b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
158515e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
158626b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
158726b15dadSJamal Hadi Salim 
15881da177e4SLinus Torvalds 	xfrm_pol_put(xp);
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds 	return 0;
15911da177e4SLinus Torvalds }
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
15941da177e4SLinus Torvalds {
15951da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
15961da177e4SLinus Torvalds 	int i;
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
15991da177e4SLinus Torvalds 		return 0;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
16021da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
16031da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
16041da177e4SLinus Torvalds 
16051f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
16061da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
16078511d01dSMiika Komu 		up->family = kp->encap_family;
16081da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
16091da177e4SLinus Torvalds 		up->reqid = kp->reqid;
16101da177e4SLinus Torvalds 		up->mode = kp->mode;
16111da177e4SLinus Torvalds 		up->share = kp->share;
16121da177e4SLinus Torvalds 		up->optional = kp->optional;
16131da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
16141da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
16151da177e4SLinus Torvalds 		up->calgos = kp->calgos;
16161da177e4SLinus Torvalds 	}
16171da177e4SLinus Torvalds 
1618c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1619c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1620df71837dSTrent Jaeger }
1621df71837dSTrent Jaeger 
16220d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
16230d681623SSerge Hallyn {
16240d681623SSerge Hallyn 	if (x->security) {
16250d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
16260d681623SSerge Hallyn 	}
16270d681623SSerge Hallyn 	return 0;
16280d681623SSerge Hallyn }
16290d681623SSerge Hallyn 
16300d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
16310d681623SSerge Hallyn {
16321d1e34ddSDavid S. Miller 	if (xp->security)
16330d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
16340d681623SSerge Hallyn 	return 0;
16350d681623SSerge Hallyn }
1636*a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void)
1637cfbfd45aSThomas Graf {
1638cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1639cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1640cfbfd45aSThomas Graf #else
1641cfbfd45aSThomas Graf 	return 0;
1642cfbfd45aSThomas Graf #endif
1643cfbfd45aSThomas Graf }
16440d681623SSerge Hallyn 
1645f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1646b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1647f7b6983fSMasahide NAKAMURA {
1648c0144beaSThomas Graf 	struct xfrm_userpolicy_type upt = {
1649c0144beaSThomas Graf 		.type = type,
1650c0144beaSThomas Graf 	};
1651f7b6983fSMasahide NAKAMURA 
1652c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1653f7b6983fSMasahide NAKAMURA }
1654f7b6983fSMasahide NAKAMURA 
1655f7b6983fSMasahide NAKAMURA #else
1656b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1657f7b6983fSMasahide NAKAMURA {
1658f7b6983fSMasahide NAKAMURA 	return 0;
1659f7b6983fSMasahide NAKAMURA }
1660f7b6983fSMasahide NAKAMURA #endif
1661f7b6983fSMasahide NAKAMURA 
16621da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
16631da177e4SLinus Torvalds {
16641da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
16651da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
16661da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
16671da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
16681da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
16691d1e34ddSDavid S. Miller 	int err;
16701da177e4SLinus Torvalds 
167115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
167279b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
167379b8b7f4SThomas Graf 	if (nlh == NULL)
167479b8b7f4SThomas Graf 		return -EMSGSIZE;
16751da177e4SLinus Torvalds 
16767b67c857SThomas Graf 	p = nlmsg_data(nlh);
16771da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
16781d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
16791d1e34ddSDavid S. Miller 	if (!err)
16801d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
16811d1e34ddSDavid S. Miller 	if (!err)
16821d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
16831d1e34ddSDavid S. Miller 	if (!err)
16841d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
16851d1e34ddSDavid S. Miller 	if (err) {
16861d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
16871d1e34ddSDavid S. Miller 		return err;
16881d1e34ddSDavid S. Miller 	}
16899825069dSThomas Graf 	nlmsg_end(skb, nlh);
16901da177e4SLinus Torvalds 	return 0;
16911da177e4SLinus Torvalds }
16921da177e4SLinus Torvalds 
16934c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
16944c563f76STimo Teras {
16954c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1696283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
16974c563f76STimo Teras 
1698283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
16994c563f76STimo Teras 	return 0;
17004c563f76STimo Teras }
17014c563f76STimo Teras 
17021da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
17031da177e4SLinus Torvalds {
1704fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
17054c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
17061da177e4SLinus Torvalds 	struct xfrm_dump_info info;
17071da177e4SLinus Torvalds 
17084c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
17094c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
17104c563f76STimo Teras 
17111da177e4SLinus Torvalds 	info.in_skb = cb->skb;
17121da177e4SLinus Torvalds 	info.out_skb = skb;
17131da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
17141da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
17154c563f76STimo Teras 
17164c563f76STimo Teras 	if (!cb->args[0]) {
17174c563f76STimo Teras 		cb->args[0] = 1;
17184c563f76STimo Teras 		xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
17194c563f76STimo Teras 	}
17204c563f76STimo Teras 
1721fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	return skb->len;
17241da177e4SLinus Torvalds }
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
17271da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
17281da177e4SLinus Torvalds 					  int dir, u32 seq)
17291da177e4SLinus Torvalds {
17301da177e4SLinus Torvalds 	struct xfrm_dump_info info;
17311da177e4SLinus Torvalds 	struct sk_buff *skb;
1732c2546372SMathias Krause 	int err;
17331da177e4SLinus Torvalds 
17347deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17351da177e4SLinus Torvalds 	if (!skb)
17361da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	info.in_skb = in_skb;
17391da177e4SLinus Torvalds 	info.out_skb = skb;
17401da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
17411da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
17421da177e4SLinus Torvalds 
1743c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1744c2546372SMathias Krause 	if (err) {
17451da177e4SLinus Torvalds 		kfree_skb(skb);
1746c2546372SMathias Krause 		return ERR_PTR(err);
17471da177e4SLinus Torvalds 	}
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	return skb;
17501da177e4SLinus Torvalds }
17511da177e4SLinus Torvalds 
175222e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
17535424f32eSThomas Graf 		struct nlattr **attrs)
17541da177e4SLinus Torvalds {
1755fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
17561da177e4SLinus Torvalds 	struct xfrm_policy *xp;
17571da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1758b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
17591da177e4SLinus Torvalds 	int err;
176026b15dadSJamal Hadi Salim 	struct km_event c;
17611da177e4SLinus Torvalds 	int delete;
1762295fae56SJamal Hadi Salim 	struct xfrm_mark m;
1763295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
17641da177e4SLinus Torvalds 
17657b67c857SThomas Graf 	p = nlmsg_data(nlh);
17661da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
17671da177e4SLinus Torvalds 
176835a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1769f7b6983fSMasahide NAKAMURA 	if (err)
1770f7b6983fSMasahide NAKAMURA 		return err;
1771f7b6983fSMasahide NAKAMURA 
17721da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
17731da177e4SLinus Torvalds 	if (err)
17741da177e4SLinus Torvalds 		return err;
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	if (p->index)
1777295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1778df71837dSTrent Jaeger 	else {
17795424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
178003e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1781df71837dSTrent Jaeger 
178235a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
1783df71837dSTrent Jaeger 		if (err)
1784df71837dSTrent Jaeger 			return err;
1785df71837dSTrent Jaeger 
17862c8dd116SDenis V. Lunev 		ctx = NULL;
1787df71837dSTrent Jaeger 		if (rt) {
17885424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1789df71837dSTrent Jaeger 
179052a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
179103e1ad7bSPaul Moore 			if (err)
1792df71837dSTrent Jaeger 				return err;
17932c8dd116SDenis V. Lunev 		}
1794295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
17956f26b61eSJamal Hadi Salim 					   ctx, delete, &err);
179603e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
1797df71837dSTrent Jaeger 	}
17981da177e4SLinus Torvalds 	if (xp == NULL)
17991da177e4SLinus Torvalds 		return -ENOENT;
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 	if (!delete) {
18021da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
18051da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
18061da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
18071da177e4SLinus Torvalds 		} else {
1808a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
180915e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
18101da177e4SLinus Torvalds 		}
181126b15dadSJamal Hadi Salim 	} else {
18122e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
181313fcfbb0SDavid S. Miller 
181413fcfbb0SDavid S. Miller 		if (err != 0)
1815c8c05a8eSCatherine Zhang 			goto out;
181613fcfbb0SDavid S. Miller 
1817e7443892SHerbert Xu 		c.data.byid = p->index;
1818f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
181926b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
182015e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
182126b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
18221da177e4SLinus Torvalds 	}
18231da177e4SLinus Torvalds 
1824c8c05a8eSCatherine Zhang out:
1825ef41aaa0SEric Paris 	xfrm_pol_put(xp);
18261da177e4SLinus Torvalds 	return err;
18271da177e4SLinus Torvalds }
18281da177e4SLinus Torvalds 
182922e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
18305424f32eSThomas Graf 		struct nlattr **attrs)
18311da177e4SLinus Torvalds {
1832fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
183326b15dadSJamal Hadi Salim 	struct km_event c;
18347b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
18354aa2e62cSJoy Latten 	int err;
18361da177e4SLinus Torvalds 
18372e71029eSTetsuo Handa 	err = xfrm_state_flush(net, p->proto, true);
18389e64cc95SJamal Hadi Salim 	if (err) {
18399e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
18409e64cc95SJamal Hadi Salim 			return 0;
1841069c474eSDavid S. Miller 		return err;
18429e64cc95SJamal Hadi Salim 	}
1843bf08867fSHerbert Xu 	c.data.proto = p->proto;
1844f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
184526b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
184615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
18477067802eSAlexey Dobriyan 	c.net = net;
184826b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
184926b15dadSJamal Hadi Salim 
18501da177e4SLinus Torvalds 	return 0;
18511da177e4SLinus Torvalds }
18521da177e4SLinus Torvalds 
1853*a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
18547deb2264SThomas Graf {
1855*a1b831f2SAlexey Dobriyan 	unsigned int replay_size = x->replay_esn ?
1856d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
1857d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
1858d8647b79SSteffen Klassert 
18597deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1860d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
1861de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
18626f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
18637deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
18647deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
18657deb2264SThomas Graf }
1866d51d081dSJamal Hadi Salim 
1867214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1868d51d081dSJamal Hadi Salim {
1869d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1870d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
18711d1e34ddSDavid S. Miller 	int err;
1872d51d081dSJamal Hadi Salim 
187315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
187479b8b7f4SThomas Graf 	if (nlh == NULL)
187579b8b7f4SThomas Graf 		return -EMSGSIZE;
1876d51d081dSJamal Hadi Salim 
18777b67c857SThomas Graf 	id = nlmsg_data(nlh);
1878931e79d7SMathias Krause 	memset(&id->sa_id, 0, sizeof(id->sa_id));
18792b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1880d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
1881d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
1882d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
18832b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
18842b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
1885d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
1886d51d081dSJamal Hadi Salim 
1887d0fde795SDavid S. Miller 	if (x->replay_esn) {
18881d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1889d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
18901d1e34ddSDavid S. Miller 			      x->replay_esn);
1891d0fde795SDavid S. Miller 	} else {
18921d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
18931d1e34ddSDavid S. Miller 			      &x->replay);
1894d0fde795SDavid S. Miller 	}
18951d1e34ddSDavid S. Miller 	if (err)
18961d1e34ddSDavid S. Miller 		goto out_cancel;
1897de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1898de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
18991d1e34ddSDavid S. Miller 	if (err)
19001d1e34ddSDavid S. Miller 		goto out_cancel;
1901d8647b79SSteffen Klassert 
19021d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
19031d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
19041d1e34ddSDavid S. Miller 		if (err)
19051d1e34ddSDavid S. Miller 			goto out_cancel;
19061d1e34ddSDavid S. Miller 	}
19071d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
19081d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
19091d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
19101d1e34ddSDavid S. Miller 		if (err)
19111d1e34ddSDavid S. Miller 			goto out_cancel;
19121d1e34ddSDavid S. Miller 	}
19131d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
19141d1e34ddSDavid S. Miller 	if (err)
19151d1e34ddSDavid S. Miller 		goto out_cancel;
19166f26b61eSJamal Hadi Salim 
1917053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1918053c095aSJohannes Berg 	return 0;
1919d51d081dSJamal Hadi Salim 
19201d1e34ddSDavid S. Miller out_cancel:
19219825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
19221d1e34ddSDavid S. Miller 	return err;
1923d51d081dSJamal Hadi Salim }
1924d51d081dSJamal Hadi Salim 
192522e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
19265424f32eSThomas Graf 		struct nlattr **attrs)
1927d51d081dSJamal Hadi Salim {
1928fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1929d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1930d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
1931d51d081dSJamal Hadi Salim 	int err;
1932d51d081dSJamal Hadi Salim 	struct km_event c;
19336f26b61eSJamal Hadi Salim 	u32 mark;
19346f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
19357b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
1936d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
1937d51d081dSJamal Hadi Salim 
19386f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
19396f26b61eSJamal Hadi Salim 
19406f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1941d8647b79SSteffen Klassert 	if (x == NULL)
1942d51d081dSJamal Hadi Salim 		return -ESRCH;
1943d8647b79SSteffen Klassert 
1944d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1945d8647b79SSteffen Klassert 	if (r_skb == NULL) {
1946d8647b79SSteffen Klassert 		xfrm_state_put(x);
1947d8647b79SSteffen Klassert 		return -ENOMEM;
1948d51d081dSJamal Hadi Salim 	}
1949d51d081dSJamal Hadi Salim 
1950d51d081dSJamal Hadi Salim 	/*
1951d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
1952d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
1953d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
1954d51d081dSJamal Hadi Salim 	*/
1955d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1956d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
1957d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
195815e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
1959d51d081dSJamal Hadi Salim 
1960d51d081dSJamal Hadi Salim 	if (build_aevent(r_skb, x, &c) < 0)
1961d51d081dSJamal Hadi Salim 		BUG();
196215e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1963d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1964d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1965d51d081dSJamal Hadi Salim 	return err;
1966d51d081dSJamal Hadi Salim }
1967d51d081dSJamal Hadi Salim 
196822e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
19695424f32eSThomas Graf 		struct nlattr **attrs)
1970d51d081dSJamal Hadi Salim {
1971fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1972d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1973d51d081dSJamal Hadi Salim 	struct km_event c;
1974d51d081dSJamal Hadi Salim 	int err = -EINVAL;
19756f26b61eSJamal Hadi Salim 	u32 mark = 0;
19766f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
19777b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
19785424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1979d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
19805424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
19814e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
19824e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
1983d51d081dSJamal Hadi Salim 
19844e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
1985d51d081dSJamal Hadi Salim 		return err;
1986d51d081dSJamal Hadi Salim 
1987d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
1988d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1989d51d081dSJamal Hadi Salim 		return err;
1990d51d081dSJamal Hadi Salim 
19916f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
19926f26b61eSJamal Hadi Salim 
19936f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1994d51d081dSJamal Hadi Salim 	if (x == NULL)
1995d51d081dSJamal Hadi Salim 		return -ESRCH;
1996d51d081dSJamal Hadi Salim 
1997d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
1998d51d081dSJamal Hadi Salim 		goto out;
1999d51d081dSJamal Hadi Salim 
20004479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
2001e2b19125SSteffen Klassert 	if (err)
2002e2b19125SSteffen Klassert 		goto out;
2003e2b19125SSteffen Klassert 
2004d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2005e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
2006d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2007d51d081dSJamal Hadi Salim 
2008d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
2009d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
201015e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2011d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
2012d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
2013d51d081dSJamal Hadi Salim 	err = 0;
2014d51d081dSJamal Hadi Salim out:
2015d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2016d51d081dSJamal Hadi Salim 	return err;
2017d51d081dSJamal Hadi Salim }
2018d51d081dSJamal Hadi Salim 
201922e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
20205424f32eSThomas Graf 		struct nlattr **attrs)
20211da177e4SLinus Torvalds {
2022fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
202326b15dadSJamal Hadi Salim 	struct km_event c;
2024b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
2025f7b6983fSMasahide NAKAMURA 	int err;
202626b15dadSJamal Hadi Salim 
202735a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2028f7b6983fSMasahide NAKAMURA 	if (err)
2029f7b6983fSMasahide NAKAMURA 		return err;
2030f7b6983fSMasahide NAKAMURA 
20312e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
20322f1eb65fSJamal Hadi Salim 	if (err) {
20332f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
20342f1eb65fSJamal Hadi Salim 			return 0;
2035069c474eSDavid S. Miller 		return err;
20362f1eb65fSJamal Hadi Salim 	}
20372f1eb65fSJamal Hadi Salim 
2038f7b6983fSMasahide NAKAMURA 	c.data.type = type;
2039f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
204026b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
204115e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
20427067802eSAlexey Dobriyan 	c.net = net;
204326b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
20441da177e4SLinus Torvalds 	return 0;
20451da177e4SLinus Torvalds }
20461da177e4SLinus Torvalds 
204722e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
20485424f32eSThomas Graf 		struct nlattr **attrs)
20496c5c8ca7SJamal Hadi Salim {
2050fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
20516c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
20527b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
20536c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2054b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
20556c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2056295fae56SJamal Hadi Salim 	struct xfrm_mark m;
2057295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
20586c5c8ca7SJamal Hadi Salim 
205935a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2060f7b6983fSMasahide NAKAMURA 	if (err)
2061f7b6983fSMasahide NAKAMURA 		return err;
2062f7b6983fSMasahide NAKAMURA 
2063c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
2064c8bf4d04STimo Teräs 	if (err)
2065c8bf4d04STimo Teräs 		return err;
2066c8bf4d04STimo Teräs 
20676c5c8ca7SJamal Hadi Salim 	if (p->index)
2068295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
20696c5c8ca7SJamal Hadi Salim 	else {
20705424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
207103e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
20726c5c8ca7SJamal Hadi Salim 
207335a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
20746c5c8ca7SJamal Hadi Salim 		if (err)
20756c5c8ca7SJamal Hadi Salim 			return err;
20766c5c8ca7SJamal Hadi Salim 
20772c8dd116SDenis V. Lunev 		ctx = NULL;
20786c5c8ca7SJamal Hadi Salim 		if (rt) {
20795424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
20806c5c8ca7SJamal Hadi Salim 
208152a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
208203e1ad7bSPaul Moore 			if (err)
20836c5c8ca7SJamal Hadi Salim 				return err;
20842c8dd116SDenis V. Lunev 		}
2085295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
20866f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
208703e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
20886c5c8ca7SJamal Hadi Salim 	}
20896c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2090ef41aaa0SEric Paris 		return -ENOENT;
209103e1ad7bSPaul Moore 
2092ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
20936c5c8ca7SJamal Hadi Salim 		goto out;
20946c5c8ca7SJamal Hadi Salim 
20956c5c8ca7SJamal Hadi Salim 	err = 0;
20966c5c8ca7SJamal Hadi Salim 	if (up->hard) {
20976c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
20982e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
20996c5c8ca7SJamal Hadi Salim 	}
2100c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
21016c5c8ca7SJamal Hadi Salim 
21026c5c8ca7SJamal Hadi Salim out:
21036c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
21046c5c8ca7SJamal Hadi Salim 	return err;
21056c5c8ca7SJamal Hadi Salim }
21066c5c8ca7SJamal Hadi Salim 
210722e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
21085424f32eSThomas Graf 		struct nlattr **attrs)
210953bc6b4dSJamal Hadi Salim {
2110fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
211153bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
211253bc6b4dSJamal Hadi Salim 	int err;
21137b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
211453bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
21156f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2116928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
211753bc6b4dSJamal Hadi Salim 
21186f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
211953bc6b4dSJamal Hadi Salim 
21203a765aa5SDavid S. Miller 	err = -ENOENT;
212153bc6b4dSJamal Hadi Salim 	if (x == NULL)
212253bc6b4dSJamal Hadi Salim 		return err;
212353bc6b4dSJamal Hadi Salim 
212453bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
21253a765aa5SDavid S. Miller 	err = -EINVAL;
212653bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
212753bc6b4dSJamal Hadi Salim 		goto out;
2128c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
212953bc6b4dSJamal Hadi Salim 
2130161a09e7SJoy Latten 	if (ue->hard) {
213153bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
21322e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2133161a09e7SJoy Latten 	}
21343a765aa5SDavid S. Miller 	err = 0;
213553bc6b4dSJamal Hadi Salim out:
213653bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
213753bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
213853bc6b4dSJamal Hadi Salim 	return err;
213953bc6b4dSJamal Hadi Salim }
214053bc6b4dSJamal Hadi Salim 
214122e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
21425424f32eSThomas Graf 		struct nlattr **attrs)
2143980ebd25SJamal Hadi Salim {
2144fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2145980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2146980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2147980ebd25SJamal Hadi Salim 	int i;
21485424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
21496f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2150980ebd25SJamal Hadi Salim 
21517b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2152fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2153980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2154980ebd25SJamal Hadi Salim 
2155980ebd25SJamal Hadi Salim 	if (!x)
2156d8eb9307SIlpo Järvinen 		goto nomem;
2157980ebd25SJamal Hadi Salim 
21586f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
21596f26b61eSJamal Hadi Salim 
2160980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2161d8eb9307SIlpo Järvinen 	if (err)
216273efc324SVegard Nossum 		goto free_state;
2163980ebd25SJamal Hadi Salim 
2164980ebd25SJamal Hadi Salim 	/*   build an XP */
2165fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2166d8eb9307SIlpo Järvinen 	if (!xp)
2167d8eb9307SIlpo Järvinen 		goto free_state;
2168980ebd25SJamal Hadi Salim 
2169980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2170980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2171980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
21726f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
21736f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
21745424f32eSThomas Graf 	ut = nla_data(rt);
2175980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2176980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2177980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2178980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2179980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2180980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2181980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2182980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2183980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2184980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2185980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2186980ebd25SJamal Hadi Salim 
2187980ebd25SJamal Hadi Salim 	}
2188980ebd25SJamal Hadi Salim 
2189980ebd25SJamal Hadi Salim 	kfree(x);
2190980ebd25SJamal Hadi Salim 	kfree(xp);
2191980ebd25SJamal Hadi Salim 
2192980ebd25SJamal Hadi Salim 	return 0;
2193d8eb9307SIlpo Järvinen 
2194d8eb9307SIlpo Järvinen free_state:
2195d8eb9307SIlpo Järvinen 	kfree(x);
2196d8eb9307SIlpo Järvinen nomem:
2197d8eb9307SIlpo Järvinen 	return err;
2198980ebd25SJamal Hadi Salim }
2199980ebd25SJamal Hadi Salim 
22005c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
22015c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
220213c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
22035424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
22045c79de6eSShinta Sugimoto {
22055424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
22065c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
22075c79de6eSShinta Sugimoto 	int i, num_migrate;
22085c79de6eSShinta Sugimoto 
220913c1d189SArnaud Ebalard 	if (k != NULL) {
221013c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
221113c1d189SArnaud Ebalard 
221213c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
221313c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
221413c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
221513c1d189SArnaud Ebalard 		k->family = uk->family;
221613c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
221713c1d189SArnaud Ebalard 	}
221813c1d189SArnaud Ebalard 
22195424f32eSThomas Graf 	um = nla_data(rt);
22205424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
22215c79de6eSShinta Sugimoto 
22225c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
22235c79de6eSShinta Sugimoto 		return -EINVAL;
22245c79de6eSShinta Sugimoto 
22255c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
22265c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
22275c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
22285c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
22295c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
22305c79de6eSShinta Sugimoto 
22315c79de6eSShinta Sugimoto 		ma->proto = um->proto;
22325c79de6eSShinta Sugimoto 		ma->mode = um->mode;
22335c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
22345c79de6eSShinta Sugimoto 
22355c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
22365c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
22375c79de6eSShinta Sugimoto 	}
22385c79de6eSShinta Sugimoto 
22395c79de6eSShinta Sugimoto 	*num = i;
22405c79de6eSShinta Sugimoto 	return 0;
22415c79de6eSShinta Sugimoto }
22425c79de6eSShinta Sugimoto 
22435c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
22445424f32eSThomas Graf 			   struct nlattr **attrs)
22455c79de6eSShinta Sugimoto {
22467b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
22475c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
224813c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
22495c79de6eSShinta Sugimoto 	u8 type;
22505c79de6eSShinta Sugimoto 	int err;
22515c79de6eSShinta Sugimoto 	int n = 0;
22528d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
22534ab47d47SAntony Antony 	struct xfrm_encap_tmpl  *encap = NULL;
22545c79de6eSShinta Sugimoto 
225535a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2256cf5cb79fSThomas Graf 		return -EINVAL;
22575c79de6eSShinta Sugimoto 
225813c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
225913c1d189SArnaud Ebalard 
22605424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
22615c79de6eSShinta Sugimoto 	if (err)
22625c79de6eSShinta Sugimoto 		return err;
22635c79de6eSShinta Sugimoto 
226413c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
22655c79de6eSShinta Sugimoto 	if (err)
22665c79de6eSShinta Sugimoto 		return err;
22675c79de6eSShinta Sugimoto 
22685c79de6eSShinta Sugimoto 	if (!n)
22695c79de6eSShinta Sugimoto 		return 0;
22705c79de6eSShinta Sugimoto 
22714ab47d47SAntony Antony 	if (attrs[XFRMA_ENCAP]) {
22724ab47d47SAntony Antony 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
22734ab47d47SAntony Antony 				sizeof(*encap), GFP_KERNEL);
22744ab47d47SAntony Antony 		if (!encap)
22755c79de6eSShinta Sugimoto 			return 0;
22765c79de6eSShinta Sugimoto 	}
22774ab47d47SAntony Antony 
22784ab47d47SAntony Antony 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
22794ab47d47SAntony Antony 
22804ab47d47SAntony Antony 	kfree(encap);
22814ab47d47SAntony Antony 
22824ab47d47SAntony Antony 	return err;
22834ab47d47SAntony Antony }
22845c79de6eSShinta Sugimoto #else
22855c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
22865424f32eSThomas Graf 			   struct nlattr **attrs)
22875c79de6eSShinta Sugimoto {
22885c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
22895c79de6eSShinta Sugimoto }
22905c79de6eSShinta Sugimoto #endif
22915c79de6eSShinta Sugimoto 
22925c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2293183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
22945c79de6eSShinta Sugimoto {
22955c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
22965c79de6eSShinta Sugimoto 
22975c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
22985c79de6eSShinta Sugimoto 	um.proto = m->proto;
22995c79de6eSShinta Sugimoto 	um.mode = m->mode;
23005c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
23015c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
23025c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
23035c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
23045c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
23055c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
23065c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
23075c79de6eSShinta Sugimoto 
2308c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
23095c79de6eSShinta Sugimoto }
23105c79de6eSShinta Sugimoto 
2311183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
231213c1d189SArnaud Ebalard {
231313c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
231413c1d189SArnaud Ebalard 
231513c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
231613c1d189SArnaud Ebalard 	uk.family = k->family;
231713c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
231813c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2319a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
232013c1d189SArnaud Ebalard 
232113c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
232213c1d189SArnaud Ebalard }
232313c1d189SArnaud Ebalard 
2324*a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
23258bafd730SAntony Antony 						int with_encp)
23267deb2264SThomas Graf {
23277deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
232813c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
23298bafd730SAntony Antony 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
23307deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
23317deb2264SThomas Graf 	      + userpolicy_type_attrsize();
23327deb2264SThomas Graf }
23337deb2264SThomas Graf 
2334183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2335183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
23368bafd730SAntony Antony 			 const struct xfrm_selector *sel,
23378bafd730SAntony Antony 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
23385c79de6eSShinta Sugimoto {
2339183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
23405c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
23415c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
23421d1e34ddSDavid S. Miller 	int i, err;
23435c79de6eSShinta Sugimoto 
234479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
234579b8b7f4SThomas Graf 	if (nlh == NULL)
234679b8b7f4SThomas Graf 		return -EMSGSIZE;
23475c79de6eSShinta Sugimoto 
23487b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
23495c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
23505c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
23515c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
23525c79de6eSShinta Sugimoto 	pol_id->dir = dir;
23535c79de6eSShinta Sugimoto 
23541d1e34ddSDavid S. Miller 	if (k != NULL) {
23551d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
23561d1e34ddSDavid S. Miller 		if (err)
23571d1e34ddSDavid S. Miller 			goto out_cancel;
23581d1e34ddSDavid S. Miller 	}
23598bafd730SAntony Antony 	if (encap) {
23608bafd730SAntony Antony 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
23618bafd730SAntony Antony 		if (err)
23628bafd730SAntony Antony 			goto out_cancel;
23638bafd730SAntony Antony 	}
23641d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
23651d1e34ddSDavid S. Miller 	if (err)
23661d1e34ddSDavid S. Miller 		goto out_cancel;
23675c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
23681d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
23691d1e34ddSDavid S. Miller 		if (err)
23701d1e34ddSDavid S. Miller 			goto out_cancel;
23715c79de6eSShinta Sugimoto 	}
23725c79de6eSShinta Sugimoto 
2373053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2374053c095aSJohannes Berg 	return 0;
23751d1e34ddSDavid S. Miller 
23761d1e34ddSDavid S. Miller out_cancel:
23779825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
23781d1e34ddSDavid S. Miller 	return err;
23795c79de6eSShinta Sugimoto }
23805c79de6eSShinta Sugimoto 
2381183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2382183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
23838bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
23848bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
23855c79de6eSShinta Sugimoto {
2386a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
23875c79de6eSShinta Sugimoto 	struct sk_buff *skb;
23885c79de6eSShinta Sugimoto 
23898bafd730SAntony Antony 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
23908bafd730SAntony Antony 			GFP_ATOMIC);
23915c79de6eSShinta Sugimoto 	if (skb == NULL)
23925c79de6eSShinta Sugimoto 		return -ENOMEM;
23935c79de6eSShinta Sugimoto 
23945c79de6eSShinta Sugimoto 	/* build migrate */
23958bafd730SAntony Antony 	if (build_migrate(skb, m, num_migrate, k, sel, encap, dir, type) < 0)
23965c79de6eSShinta Sugimoto 		BUG();
23975c79de6eSShinta Sugimoto 
239821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
23995c79de6eSShinta Sugimoto }
24005c79de6eSShinta Sugimoto #else
2401183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2402183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
24038bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
24048bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
24055c79de6eSShinta Sugimoto {
24065c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
24075c79de6eSShinta Sugimoto }
24085c79de6eSShinta Sugimoto #endif
2409d51d081dSJamal Hadi Salim 
2410a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2411492b558bSThomas Graf 
2412492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
241366f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2414492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2415492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2416492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2417492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2418492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2419492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2420980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
242153bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2422492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
242366f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
24246c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2425492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2426a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2427d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2428d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
242997a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
24305c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2431a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2432880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2433a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
24341da177e4SLinus Torvalds };
24351da177e4SLinus Torvalds 
2436492b558bSThomas Graf #undef XMSGSIZE
2437492b558bSThomas Graf 
2438cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2439c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2440c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2441c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2442c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
24431a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2444cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2445cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2446cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2447cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2448cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2449cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2450cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2451cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2452cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2453cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2454cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2455cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2456cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2457cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
245813c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
24596f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
246035d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2461d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2462a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2463d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2464870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2465d77e38e6SSteffen Klassert 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
2466077fbac4SLorenzo Colitti 	[XFRMA_OUTPUT_MARK]	= { .len = NLA_U32 },
2467cf5cb79fSThomas Graf };
2468cf5cb79fSThomas Graf 
2469880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2470880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2471880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2472880a6fabSChristophe Gouault };
2473880a6fabSChristophe Gouault 
247405600a79SMathias Krause static const struct xfrm_link {
24755424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
24761da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
24774c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2478880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2479880a6fabSChristophe Gouault 	int nla_max;
2480492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2481492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2482492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2483492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
24844c563f76STimo Teras 						   .dump = xfrm_dump_sa,
24854c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2486492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2487492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2488492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
24894c563f76STimo Teras 						   .dump = xfrm_dump_policy,
24904c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2491492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2492980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
249353bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2494492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2495492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
24966c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2497492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2498492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2499d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2500d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
25015c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
250228d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2503880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2504880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2505880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2506ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
25071da177e4SLinus Torvalds };
25081da177e4SLinus Torvalds 
25092d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
25102d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
25111da177e4SLinus Torvalds {
2512a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
251335a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
251405600a79SMathias Krause 	const struct xfrm_link *link;
2515a7bd9a45SThomas Graf 	int type, err;
25161da177e4SLinus Torvalds 
251774005991SFan Du #ifdef CONFIG_COMPAT
25182bf8c476SAndy Lutomirski 	if (in_compat_syscall())
251983e2d058SYi Zhao 		return -EOPNOTSUPP;
252074005991SFan Du #endif
252174005991SFan Du 
25221da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
25231da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
25241d00a4ebSThomas Graf 		return -EINVAL;
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
25271da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
253090f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
25311d00a4ebSThomas Graf 		return -EPERM;
25321da177e4SLinus Torvalds 
2533492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2534492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2535b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
25361da177e4SLinus Torvalds 		if (link->dump == NULL)
25371d00a4ebSThomas Graf 			return -EINVAL;
25381da177e4SLinus Torvalds 
253980d326faSPablo Neira Ayuso 		{
254080d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
254180d326faSPablo Neira Ayuso 				.dump = link->dump,
254280d326faSPablo Neira Ayuso 				.done = link->done,
254380d326faSPablo Neira Ayuso 			};
254480d326faSPablo Neira Ayuso 			return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
254580d326faSPablo Neira Ayuso 		}
25461da177e4SLinus Torvalds 	}
25471da177e4SLinus Torvalds 
2548880a6fabSChristophe Gouault 	err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2549880a6fabSChristophe Gouault 			  link->nla_max ? : XFRMA_MAX,
2550fe52145fSJohannes Berg 			  link->nla_pol ? : xfrma_policy, extack);
2551a7bd9a45SThomas Graf 	if (err < 0)
2552a7bd9a45SThomas Graf 		return err;
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds 	if (link->doit == NULL)
25551d00a4ebSThomas Graf 		return -EINVAL;
25561da177e4SLinus Torvalds 
25575424f32eSThomas Graf 	return link->doit(skb, nlh, attrs);
25581da177e4SLinus Torvalds }
25591da177e4SLinus Torvalds 
2560cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
25611da177e4SLinus Torvalds {
2562283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2563283bc9f3SFan Du 
2564283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2565cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2566283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
25671da177e4SLinus Torvalds }
25681da177e4SLinus Torvalds 
2569*a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void)
25707deb2264SThomas Graf {
25716f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
25726f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
25737deb2264SThomas Graf }
25747deb2264SThomas Graf 
2575214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
25761da177e4SLinus Torvalds {
25771da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
25781da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
25791d1e34ddSDavid S. Miller 	int err;
25801da177e4SLinus Torvalds 
258115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
258279b8b7f4SThomas Graf 	if (nlh == NULL)
258379b8b7f4SThomas Graf 		return -EMSGSIZE;
25841da177e4SLinus Torvalds 
25857b67c857SThomas Graf 	ue = nlmsg_data(nlh);
25861da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2587d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
2588e3e5fc16SMathias Krause 	/* clear the padding bytes */
2589e3e5fc16SMathias Krause 	memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
25901da177e4SLinus Torvalds 
25911d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
25921d1e34ddSDavid S. Miller 	if (err)
25931d1e34ddSDavid S. Miller 		return err;
25946f26b61eSJamal Hadi Salim 
2595053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2596053c095aSJohannes Berg 	return 0;
25971da177e4SLinus Torvalds }
25981da177e4SLinus Torvalds 
2599214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
26001da177e4SLinus Torvalds {
2601fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
26021da177e4SLinus Torvalds 	struct sk_buff *skb;
26031da177e4SLinus Torvalds 
26047deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
26051da177e4SLinus Torvalds 	if (skb == NULL)
26061da177e4SLinus Torvalds 		return -ENOMEM;
26071da177e4SLinus Torvalds 
26086f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
26096f26b61eSJamal Hadi Salim 		kfree_skb(skb);
26106f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
26116f26b61eSJamal Hadi Salim 	}
26121da177e4SLinus Torvalds 
261321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
26141da177e4SLinus Torvalds }
26151da177e4SLinus Torvalds 
2616214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2617d51d081dSJamal Hadi Salim {
2618fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2619d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
2620d51d081dSJamal Hadi Salim 
2621d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2622d51d081dSJamal Hadi Salim 	if (skb == NULL)
2623d51d081dSJamal Hadi Salim 		return -ENOMEM;
2624d51d081dSJamal Hadi Salim 
2625d51d081dSJamal Hadi Salim 	if (build_aevent(skb, x, c) < 0)
2626d51d081dSJamal Hadi Salim 		BUG();
2627d51d081dSJamal Hadi Salim 
262821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2629d51d081dSJamal Hadi Salim }
2630d51d081dSJamal Hadi Salim 
2631214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
263226b15dadSJamal Hadi Salim {
26337067802eSAlexey Dobriyan 	struct net *net = c->net;
263426b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
263526b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
263626b15dadSJamal Hadi Salim 	struct sk_buff *skb;
26377deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
263826b15dadSJamal Hadi Salim 
26397deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
264026b15dadSJamal Hadi Salim 	if (skb == NULL)
264126b15dadSJamal Hadi Salim 		return -ENOMEM;
264226b15dadSJamal Hadi Salim 
264315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
264479b8b7f4SThomas Graf 	if (nlh == NULL) {
264579b8b7f4SThomas Graf 		kfree_skb(skb);
264679b8b7f4SThomas Graf 		return -EMSGSIZE;
264779b8b7f4SThomas Graf 	}
264826b15dadSJamal Hadi Salim 
26497b67c857SThomas Graf 	p = nlmsg_data(nlh);
2650bf08867fSHerbert Xu 	p->proto = c->data.proto;
265126b15dadSJamal Hadi Salim 
26529825069dSThomas Graf 	nlmsg_end(skb, nlh);
265326b15dadSJamal Hadi Salim 
265421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
265526b15dadSJamal Hadi Salim }
265626b15dadSJamal Hadi Salim 
2657*a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
265826b15dadSJamal Hadi Salim {
2659*a1b831f2SAlexey Dobriyan 	unsigned int l = 0;
26601a6509d9SHerbert Xu 	if (x->aead)
26611a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
26624447bb33SMartin Willi 	if (x->aalg) {
26634447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
26644447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
26654447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
26664447bb33SMartin Willi 	}
266726b15dadSJamal Hadi Salim 	if (x->ealg)
26680f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
266926b15dadSJamal Hadi Salim 	if (x->calg)
26707deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
267126b15dadSJamal Hadi Salim 	if (x->encap)
26727deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
267335d2856bSMartin Willi 	if (x->tfcpad)
267435d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
2675d8647b79SSteffen Klassert 	if (x->replay_esn)
2676d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2677f293a5e3Sdingzhi 	else
2678f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
267968325d3bSHerbert Xu 	if (x->security)
268068325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
268168325d3bSHerbert Xu 				    x->security->ctx_len);
268268325d3bSHerbert Xu 	if (x->coaddr)
268368325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
2684a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
2685a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
2686d77e38e6SSteffen Klassert 	if (x->xso.dev)
2687d77e38e6SSteffen Klassert 		 l += nla_total_size(sizeof(x->xso));
2688077fbac4SLorenzo Colitti 	if (x->props.output_mark)
2689077fbac4SLorenzo Colitti 		l += nla_total_size(sizeof(x->props.output_mark));
269068325d3bSHerbert Xu 
2691d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2692de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
269326b15dadSJamal Hadi Salim 
269426b15dadSJamal Hadi Salim 	return l;
269526b15dadSJamal Hadi Salim }
269626b15dadSJamal Hadi Salim 
2697214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
269826b15dadSJamal Hadi Salim {
2699fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
270026b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
27010603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
270226b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
270326b15dadSJamal Hadi Salim 	struct sk_buff *skb;
2704*a1b831f2SAlexey Dobriyan 	unsigned int len = xfrm_sa_len(x);
2705*a1b831f2SAlexey Dobriyan 	unsigned int headlen;
2706*a1b831f2SAlexey Dobriyan 	int err;
27070603eac0SHerbert Xu 
27080603eac0SHerbert Xu 	headlen = sizeof(*p);
27090603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
27107deb2264SThomas Graf 		len += nla_total_size(headlen);
27110603eac0SHerbert Xu 		headlen = sizeof(*id);
27126f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
27130603eac0SHerbert Xu 	}
27147deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
271526b15dadSJamal Hadi Salim 
27167deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
271726b15dadSJamal Hadi Salim 	if (skb == NULL)
271826b15dadSJamal Hadi Salim 		return -ENOMEM;
271926b15dadSJamal Hadi Salim 
272015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
27211d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
272279b8b7f4SThomas Graf 	if (nlh == NULL)
27231d1e34ddSDavid S. Miller 		goto out_free_skb;
272426b15dadSJamal Hadi Salim 
27257b67c857SThomas Graf 	p = nlmsg_data(nlh);
27260603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2727c0144beaSThomas Graf 		struct nlattr *attr;
2728c0144beaSThomas Graf 
27297b67c857SThomas Graf 		id = nlmsg_data(nlh);
273050329c8aSMathias Krause 		memset(id, 0, sizeof(*id));
27310603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
27320603eac0SHerbert Xu 		id->spi = x->id.spi;
27330603eac0SHerbert Xu 		id->family = x->props.family;
27340603eac0SHerbert Xu 		id->proto = x->id.proto;
27350603eac0SHerbert Xu 
2736c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
27371d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2738c0144beaSThomas Graf 		if (attr == NULL)
27391d1e34ddSDavid S. Miller 			goto out_free_skb;
2740c0144beaSThomas Graf 
2741c0144beaSThomas Graf 		p = nla_data(attr);
27420603eac0SHerbert Xu 	}
27431d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
27441d1e34ddSDavid S. Miller 	if (err)
27451d1e34ddSDavid S. Miller 		goto out_free_skb;
274626b15dadSJamal Hadi Salim 
27479825069dSThomas Graf 	nlmsg_end(skb, nlh);
274826b15dadSJamal Hadi Salim 
274921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
275026b15dadSJamal Hadi Salim 
27511d1e34ddSDavid S. Miller out_free_skb:
275226b15dadSJamal Hadi Salim 	kfree_skb(skb);
27531d1e34ddSDavid S. Miller 	return err;
275426b15dadSJamal Hadi Salim }
275526b15dadSJamal Hadi Salim 
2756214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
275726b15dadSJamal Hadi Salim {
275826b15dadSJamal Hadi Salim 
275926b15dadSJamal Hadi Salim 	switch (c->event) {
2760f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
276126b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2762d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2763d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2764f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2765f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2766f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
276726b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2768f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
276926b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
277026b15dadSJamal Hadi Salim 	default:
277162db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
277262db5cfdSstephen hemminger 		       c->event);
277326b15dadSJamal Hadi Salim 		break;
277426b15dadSJamal Hadi Salim 	}
277526b15dadSJamal Hadi Salim 
277626b15dadSJamal Hadi Salim 	return 0;
277726b15dadSJamal Hadi Salim 
277826b15dadSJamal Hadi Salim }
277926b15dadSJamal Hadi Salim 
2780*a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
27817deb2264SThomas Graf 						struct xfrm_policy *xp)
27827deb2264SThomas Graf {
27837deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
27847deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
27856f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
27867deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
27877deb2264SThomas Graf 	       + userpolicy_type_attrsize();
27887deb2264SThomas Graf }
27897deb2264SThomas Graf 
27901da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
279165e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
27921da177e4SLinus Torvalds {
27931d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
27941da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
27951da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
27961d1e34ddSDavid S. Miller 	int err;
27971da177e4SLinus Torvalds 
279879b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
279979b8b7f4SThomas Graf 	if (nlh == NULL)
280079b8b7f4SThomas Graf 		return -EMSGSIZE;
28011da177e4SLinus Torvalds 
28027b67c857SThomas Graf 	ua = nlmsg_data(nlh);
28031da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
28041da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
28051da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
280665e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
28071da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
28081da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
28091da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
28101da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
28111da177e4SLinus Torvalds 
28121d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
28131d1e34ddSDavid S. Miller 	if (!err)
28141d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
28151d1e34ddSDavid S. Miller 	if (!err)
28161d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
28171d1e34ddSDavid S. Miller 	if (!err)
28181d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
28191d1e34ddSDavid S. Miller 	if (err) {
28201d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
28211d1e34ddSDavid S. Miller 		return err;
28221d1e34ddSDavid S. Miller 	}
28231da177e4SLinus Torvalds 
2824053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2825053c095aSJohannes Berg 	return 0;
28261da177e4SLinus Torvalds }
28271da177e4SLinus Torvalds 
28281da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
282965e0736bSFan Du 			     struct xfrm_policy *xp)
28301da177e4SLinus Torvalds {
2831a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
28321da177e4SLinus Torvalds 	struct sk_buff *skb;
28331da177e4SLinus Torvalds 
28347deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
28351da177e4SLinus Torvalds 	if (skb == NULL)
28361da177e4SLinus Torvalds 		return -ENOMEM;
28371da177e4SLinus Torvalds 
283865e0736bSFan Du 	if (build_acquire(skb, x, xt, xp) < 0)
28391da177e4SLinus Torvalds 		BUG();
28401da177e4SLinus Torvalds 
284121ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
28421da177e4SLinus Torvalds }
28431da177e4SLinus Torvalds 
28441da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
28451da177e4SLinus Torvalds  * or more templates.
28461da177e4SLinus Torvalds  */
2847cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
28481da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
28491da177e4SLinus Torvalds {
2850fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
28511da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
28521da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
28531da177e4SLinus Torvalds 	struct xfrm_policy *xp;
28541da177e4SLinus Torvalds 	int nr;
28551da177e4SLinus Torvalds 
2856cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
28571da177e4SLinus Torvalds 	case AF_INET:
28581da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
28591da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
28601da177e4SLinus Torvalds 			return NULL;
28611da177e4SLinus Torvalds 		}
28621da177e4SLinus Torvalds 		break;
2863dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
28641da177e4SLinus Torvalds 	case AF_INET6:
28651da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
28661da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
28671da177e4SLinus Torvalds 			return NULL;
28681da177e4SLinus Torvalds 		}
28691da177e4SLinus Torvalds 		break;
28701da177e4SLinus Torvalds #endif
28711da177e4SLinus Torvalds 	default:
28721da177e4SLinus Torvalds 		*dir = -EINVAL;
28731da177e4SLinus Torvalds 		return NULL;
28741da177e4SLinus Torvalds 	}
28751da177e4SLinus Torvalds 
28761da177e4SLinus Torvalds 	*dir = -EINVAL;
28771da177e4SLinus Torvalds 
28781da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
28791da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
28801da177e4SLinus Torvalds 		return NULL;
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
2883b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
28841da177e4SLinus Torvalds 		return NULL;
28851da177e4SLinus Torvalds 
2886a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
2887a4f1bac6SHerbert Xu 		return NULL;
2888a4f1bac6SHerbert Xu 
28892f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
28901da177e4SLinus Torvalds 	if (xp == NULL) {
28911da177e4SLinus Torvalds 		*dir = -ENOBUFS;
28921da177e4SLinus Torvalds 		return NULL;
28931da177e4SLinus Torvalds 	}
28941da177e4SLinus Torvalds 
28951da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
2896f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
28971da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
28981da177e4SLinus Torvalds 
28991da177e4SLinus Torvalds 	*dir = p->dir;
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 	return xp;
29021da177e4SLinus Torvalds }
29031da177e4SLinus Torvalds 
2904*a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
29057deb2264SThomas Graf {
29067deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
29077deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
29087deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2909295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
29107deb2264SThomas Graf 	       + userpolicy_type_attrsize();
29117deb2264SThomas Graf }
29127deb2264SThomas Graf 
29131da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2914214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
29151da177e4SLinus Torvalds {
29161da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
2917d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
29181d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
29191d1e34ddSDavid S. Miller 	int err;
29201da177e4SLinus Torvalds 
292115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
292279b8b7f4SThomas Graf 	if (nlh == NULL)
292379b8b7f4SThomas Graf 		return -EMSGSIZE;
29241da177e4SLinus Torvalds 
29257b67c857SThomas Graf 	upe = nlmsg_data(nlh);
29261da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
29271d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
29281d1e34ddSDavid S. Miller 	if (!err)
29291d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
29301d1e34ddSDavid S. Miller 	if (!err)
29311d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
29321d1e34ddSDavid S. Miller 	if (!err)
29331d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
29341d1e34ddSDavid S. Miller 	if (err) {
29351d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
29361d1e34ddSDavid S. Miller 		return err;
29371d1e34ddSDavid S. Miller 	}
29381da177e4SLinus Torvalds 	upe->hard = !!hard;
29391da177e4SLinus Torvalds 
2940053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2941053c095aSJohannes Berg 	return 0;
29421da177e4SLinus Torvalds }
29431da177e4SLinus Torvalds 
2944214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
29451da177e4SLinus Torvalds {
2946fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
29471da177e4SLinus Torvalds 	struct sk_buff *skb;
29481da177e4SLinus Torvalds 
29497deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
29501da177e4SLinus Torvalds 	if (skb == NULL)
29511da177e4SLinus Torvalds 		return -ENOMEM;
29521da177e4SLinus Torvalds 
2953d51d081dSJamal Hadi Salim 	if (build_polexpire(skb, xp, dir, c) < 0)
29541da177e4SLinus Torvalds 		BUG();
29551da177e4SLinus Torvalds 
295621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
29571da177e4SLinus Torvalds }
29581da177e4SLinus Torvalds 
2959214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
296026b15dadSJamal Hadi Salim {
2961*a1b831f2SAlexey Dobriyan 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2962fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
296326b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
29640603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
296526b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
296626b15dadSJamal Hadi Salim 	struct sk_buff *skb;
2967*a1b831f2SAlexey Dobriyan 	unsigned int headlen;
2968*a1b831f2SAlexey Dobriyan 	int err;
29690603eac0SHerbert Xu 
29700603eac0SHerbert Xu 	headlen = sizeof(*p);
29710603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
29727deb2264SThomas Graf 		len += nla_total_size(headlen);
29730603eac0SHerbert Xu 		headlen = sizeof(*id);
29740603eac0SHerbert Xu 	}
2975cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
2976295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
29777deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
297826b15dadSJamal Hadi Salim 
29797deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
298026b15dadSJamal Hadi Salim 	if (skb == NULL)
298126b15dadSJamal Hadi Salim 		return -ENOMEM;
298226b15dadSJamal Hadi Salim 
298315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
29841d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
298579b8b7f4SThomas Graf 	if (nlh == NULL)
29861d1e34ddSDavid S. Miller 		goto out_free_skb;
298726b15dadSJamal Hadi Salim 
29887b67c857SThomas Graf 	p = nlmsg_data(nlh);
29890603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
2990c0144beaSThomas Graf 		struct nlattr *attr;
2991c0144beaSThomas Graf 
29927b67c857SThomas Graf 		id = nlmsg_data(nlh);
29930603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
29940603eac0SHerbert Xu 		id->dir = dir;
29950603eac0SHerbert Xu 		if (c->data.byid)
29960603eac0SHerbert Xu 			id->index = xp->index;
29970603eac0SHerbert Xu 		else
29980603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
29990603eac0SHerbert Xu 
3000c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
30011d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3002c0144beaSThomas Graf 		if (attr == NULL)
30031d1e34ddSDavid S. Miller 			goto out_free_skb;
3004c0144beaSThomas Graf 
3005c0144beaSThomas Graf 		p = nla_data(attr);
30060603eac0SHerbert Xu 	}
300726b15dadSJamal Hadi Salim 
300826b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
30091d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
30101d1e34ddSDavid S. Miller 	if (!err)
30111d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
30121d1e34ddSDavid S. Miller 	if (!err)
30131d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
30141d1e34ddSDavid S. Miller 	if (err)
30151d1e34ddSDavid S. Miller 		goto out_free_skb;
3016295fae56SJamal Hadi Salim 
30179825069dSThomas Graf 	nlmsg_end(skb, nlh);
301826b15dadSJamal Hadi Salim 
301921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
302026b15dadSJamal Hadi Salim 
30211d1e34ddSDavid S. Miller out_free_skb:
302226b15dadSJamal Hadi Salim 	kfree_skb(skb);
30231d1e34ddSDavid S. Miller 	return err;
302426b15dadSJamal Hadi Salim }
302526b15dadSJamal Hadi Salim 
3026214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
302726b15dadSJamal Hadi Salim {
30287067802eSAlexey Dobriyan 	struct net *net = c->net;
302926b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
303026b15dadSJamal Hadi Salim 	struct sk_buff *skb;
30311d1e34ddSDavid S. Miller 	int err;
303226b15dadSJamal Hadi Salim 
30337deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
303426b15dadSJamal Hadi Salim 	if (skb == NULL)
303526b15dadSJamal Hadi Salim 		return -ENOMEM;
303626b15dadSJamal Hadi Salim 
303715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
30381d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
303979b8b7f4SThomas Graf 	if (nlh == NULL)
30401d1e34ddSDavid S. Miller 		goto out_free_skb;
30411d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
30421d1e34ddSDavid S. Miller 	if (err)
30431d1e34ddSDavid S. Miller 		goto out_free_skb;
304426b15dadSJamal Hadi Salim 
30459825069dSThomas Graf 	nlmsg_end(skb, nlh);
304626b15dadSJamal Hadi Salim 
304721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
304826b15dadSJamal Hadi Salim 
30491d1e34ddSDavid S. Miller out_free_skb:
305026b15dadSJamal Hadi Salim 	kfree_skb(skb);
30511d1e34ddSDavid S. Miller 	return err;
305226b15dadSJamal Hadi Salim }
305326b15dadSJamal Hadi Salim 
3054214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
305526b15dadSJamal Hadi Salim {
305626b15dadSJamal Hadi Salim 
305726b15dadSJamal Hadi Salim 	switch (c->event) {
3058f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
3059f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
3060f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
306126b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
3062f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
306326b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
3064f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
306526b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
306626b15dadSJamal Hadi Salim 	default:
306762db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
306862db5cfdSstephen hemminger 		       c->event);
306926b15dadSJamal Hadi Salim 	}
307026b15dadSJamal Hadi Salim 
307126b15dadSJamal Hadi Salim 	return 0;
307226b15dadSJamal Hadi Salim 
307326b15dadSJamal Hadi Salim }
307426b15dadSJamal Hadi Salim 
3075*a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void)
30767deb2264SThomas Graf {
30777deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
30787deb2264SThomas Graf }
30797deb2264SThomas Graf 
308097a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
308197a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
308297a64b45SMasahide NAKAMURA {
308397a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
308497a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
308597a64b45SMasahide NAKAMURA 
308679b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
308779b8b7f4SThomas Graf 	if (nlh == NULL)
308879b8b7f4SThomas Graf 		return -EMSGSIZE;
308997a64b45SMasahide NAKAMURA 
30907b67c857SThomas Graf 	ur = nlmsg_data(nlh);
309197a64b45SMasahide NAKAMURA 	ur->proto = proto;
309297a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
309397a64b45SMasahide NAKAMURA 
30941d1e34ddSDavid S. Miller 	if (addr) {
30951d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
30961d1e34ddSDavid S. Miller 		if (err) {
30979825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
30981d1e34ddSDavid S. Miller 			return err;
30991d1e34ddSDavid S. Miller 		}
31001d1e34ddSDavid S. Miller 	}
3101053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3102053c095aSJohannes Berg 	return 0;
310397a64b45SMasahide NAKAMURA }
310497a64b45SMasahide NAKAMURA 
3105db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3106db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
310797a64b45SMasahide NAKAMURA {
310897a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
310997a64b45SMasahide NAKAMURA 
31107deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
311197a64b45SMasahide NAKAMURA 	if (skb == NULL)
311297a64b45SMasahide NAKAMURA 		return -ENOMEM;
311397a64b45SMasahide NAKAMURA 
311497a64b45SMasahide NAKAMURA 	if (build_report(skb, proto, sel, addr) < 0)
311597a64b45SMasahide NAKAMURA 		BUG();
311697a64b45SMasahide NAKAMURA 
311721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
311897a64b45SMasahide NAKAMURA }
311997a64b45SMasahide NAKAMURA 
3120*a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void)
31213a2dfbe8SMartin Willi {
31223a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
31233a2dfbe8SMartin Willi }
31243a2dfbe8SMartin Willi 
31253a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
31263a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
31273a2dfbe8SMartin Willi {
31283a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
31293a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
31303a2dfbe8SMartin Willi 
31313a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
31323a2dfbe8SMartin Willi 	if (nlh == NULL)
31333a2dfbe8SMartin Willi 		return -EMSGSIZE;
31343a2dfbe8SMartin Willi 
31353a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
31363a2dfbe8SMartin Willi 
31373a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
31383a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
31393a2dfbe8SMartin Willi 	um->id.family = x->props.family;
31403a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
31413a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
31423a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
31433a2dfbe8SMartin Willi 	um->new_sport = new_sport;
31443a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
31453a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
31463a2dfbe8SMartin Willi 
3147053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3148053c095aSJohannes Berg 	return 0;
31493a2dfbe8SMartin Willi }
31503a2dfbe8SMartin Willi 
31513a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
31523a2dfbe8SMartin Willi 			     __be16 sport)
31533a2dfbe8SMartin Willi {
3154a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
31553a2dfbe8SMartin Willi 	struct sk_buff *skb;
31563a2dfbe8SMartin Willi 
31573a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
31583a2dfbe8SMartin Willi 		return -EINVAL;
31593a2dfbe8SMartin Willi 
31603a2dfbe8SMartin Willi 	if (!x->encap)
31613a2dfbe8SMartin Willi 		return -EINVAL;
31623a2dfbe8SMartin Willi 
31633a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
31643a2dfbe8SMartin Willi 	if (skb == NULL)
31653a2dfbe8SMartin Willi 		return -ENOMEM;
31663a2dfbe8SMartin Willi 
31673a2dfbe8SMartin Willi 	if (build_mapping(skb, x, ipaddr, sport) < 0)
31683a2dfbe8SMartin Willi 		BUG();
31693a2dfbe8SMartin Willi 
317021ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
31713a2dfbe8SMartin Willi }
31723a2dfbe8SMartin Willi 
31730f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
31740f24558eSHoria Geanta {
31750f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
31760f24558eSHoria Geanta }
31770f24558eSHoria Geanta 
31781da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
31791da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
31801da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
31811da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
31821da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
318397a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
31845c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
31853a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
31860f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
31871da177e4SLinus Torvalds };
31881da177e4SLinus Torvalds 
3189a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
31901da177e4SLinus Torvalds {
3191be33690dSPatrick McHardy 	struct sock *nlsk;
3192a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3193a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3194a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3195a31f2d17SPablo Neira Ayuso 	};
3196be33690dSPatrick McHardy 
31979f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3198be33690dSPatrick McHardy 	if (nlsk == NULL)
31991da177e4SLinus Torvalds 		return -ENOMEM;
3200d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3201cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
32021da177e4SLinus Torvalds 	return 0;
32031da177e4SLinus Torvalds }
32041da177e4SLinus Torvalds 
3205d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3206a6483b79SAlexey Dobriyan {
3207d79d792eSEric W. Biederman 	struct net *net;
3208d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3209a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3210d79d792eSEric W. Biederman 	synchronize_net();
3211d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3212d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3213a6483b79SAlexey Dobriyan }
3214a6483b79SAlexey Dobriyan 
3215a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3216a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
3217d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3218a6483b79SAlexey Dobriyan };
3219a6483b79SAlexey Dobriyan 
3220a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3221a6483b79SAlexey Dobriyan {
3222a6483b79SAlexey Dobriyan 	int rv;
3223a6483b79SAlexey Dobriyan 
3224a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3225a6483b79SAlexey Dobriyan 
3226a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3227a6483b79SAlexey Dobriyan 	if (rv < 0)
3228a6483b79SAlexey Dobriyan 		return rv;
3229a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3230a6483b79SAlexey Dobriyan 	if (rv < 0)
3231a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3232a6483b79SAlexey Dobriyan 	return rv;
3233a6483b79SAlexey Dobriyan }
3234a6483b79SAlexey Dobriyan 
32351da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
32361da177e4SLinus Torvalds {
32371da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3238a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
32391da177e4SLinus Torvalds }
32401da177e4SLinus Torvalds 
32411da177e4SLinus Torvalds module_init(xfrm_user_init);
32421da177e4SLinus Torvalds module_exit(xfrm_user_exit);
32431da177e4SLinus Torvalds MODULE_LICENSE("GPL");
32444fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3245f8cd5488SJamal Hadi Salim 
3246