xref: /linux/net/xfrm/xfrm_user.c (revision 73efc3245fd3edb3632d82a3a9c5d5d975a02efc)
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>
301da177e4SLinus Torvalds #include <asm/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);
450f99be0dSEric Dumazet 	if (nla_len(rt) < 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 
581da177e4SLinus Torvalds 	algp->alg_name[CRYPTO_MAX_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);
714447bb33SMartin Willi 	if (nla_len(rt) < xfrm_alg_auth_len(algp))
724447bb33SMartin Willi 		return -EINVAL;
734447bb33SMartin Willi 
744447bb33SMartin Willi 	algp->alg_name[CRYPTO_MAX_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);
871a6509d9SHerbert Xu 	if (nla_len(rt) < aead_len(algp))
881a6509d9SHerbert Xu 		return -EINVAL;
891a6509d9SHerbert Xu 
901a6509d9SHerbert Xu 	algp->alg_name[CRYPTO_MAX_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 
133ecd79187SMathias Krause 		if (nla_len(rt) < 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;
407ecd79187SMathias Krause 	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 
415ecd79187SMathias Krause 	if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
416e2b19125SSteffen Klassert 		return -EINVAL;
417e2b19125SSteffen Klassert 
418e2b19125SSteffen Klassert 	return 0;
419e2b19125SSteffen Klassert }
420e2b19125SSteffen Klassert 
421d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
422d8647b79SSteffen Klassert 				       struct xfrm_replay_state_esn **preplay_esn,
423d8647b79SSteffen Klassert 				       struct nlattr *rta)
424d8647b79SSteffen Klassert {
425d8647b79SSteffen Klassert 	struct xfrm_replay_state_esn *p, *pp, *up;
426ecd79187SMathias Krause 	int klen, ulen;
427d8647b79SSteffen Klassert 
428d8647b79SSteffen Klassert 	if (!rta)
429d8647b79SSteffen Klassert 		return 0;
430d8647b79SSteffen Klassert 
431d8647b79SSteffen Klassert 	up = nla_data(rta);
432ecd79187SMathias Krause 	klen = xfrm_replay_state_esn_len(up);
433ecd79187SMathias Krause 	ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
434d8647b79SSteffen Klassert 
435ecd79187SMathias Krause 	p = kzalloc(klen, GFP_KERNEL);
436d8647b79SSteffen Klassert 	if (!p)
437d8647b79SSteffen Klassert 		return -ENOMEM;
438d8647b79SSteffen Klassert 
439ecd79187SMathias Krause 	pp = kzalloc(klen, GFP_KERNEL);
440d8647b79SSteffen Klassert 	if (!pp) {
441d8647b79SSteffen Klassert 		kfree(p);
442d8647b79SSteffen Klassert 		return -ENOMEM;
443d8647b79SSteffen Klassert 	}
444d8647b79SSteffen Klassert 
445ecd79187SMathias Krause 	memcpy(p, up, ulen);
446ecd79187SMathias Krause 	memcpy(pp, up, ulen);
447ecd79187SMathias Krause 
448d8647b79SSteffen Klassert 	*replay_esn = p;
449d8647b79SSteffen Klassert 	*preplay_esn = pp;
450d8647b79SSteffen Klassert 
451d8647b79SSteffen Klassert 	return 0;
452d8647b79SSteffen Klassert }
453d8647b79SSteffen Klassert 
454661697f7SJoy Latten static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
455df71837dSTrent Jaeger {
456df71837dSTrent Jaeger 	int len = 0;
457df71837dSTrent Jaeger 
458df71837dSTrent Jaeger 	if (xfrm_ctx) {
459df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
460df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
461df71837dSTrent Jaeger 	}
462df71837dSTrent Jaeger 	return len;
463df71837dSTrent Jaeger }
464df71837dSTrent Jaeger 
4651da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
4681da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
4691da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
4701da177e4SLinus Torvalds 	x->props.mode = p->mode;
47133fce60dSFan Du 	x->props.replay_window = min_t(unsigned int, p->replay_window,
47233fce60dSFan Du 					sizeof(x->replay.bitmap) * 8);
4731da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
4741da177e4SLinus Torvalds 	x->props.family = p->family;
47554489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
4761da177e4SLinus Torvalds 	x->props.flags = p->flags;
477196b0036SHerbert Xu 
478ccf9b3b8SSteffen Klassert 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
479196b0036SHerbert Xu 		x->sel.family = p->family;
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
482d51d081dSJamal Hadi Salim /*
483d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
484d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
485d51d081dSJamal Hadi Salim  *
486d51d081dSJamal Hadi Salim */
487e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
488e3ac104dSMathias Krause 				  int update_esn)
489d51d081dSJamal Hadi Salim {
4905424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
491e3ac104dSMathias Krause 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
4925424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
4935424f32eSThomas Graf 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
4945424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
495d51d081dSJamal Hadi Salim 
496d8647b79SSteffen Klassert 	if (re) {
497d8647b79SSteffen Klassert 		struct xfrm_replay_state_esn *replay_esn;
498d8647b79SSteffen Klassert 		replay_esn = nla_data(re);
499d8647b79SSteffen Klassert 		memcpy(x->replay_esn, replay_esn,
500d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
501d8647b79SSteffen Klassert 		memcpy(x->preplay_esn, replay_esn,
502d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
503d8647b79SSteffen Klassert 	}
504d8647b79SSteffen Klassert 
505d51d081dSJamal Hadi Salim 	if (rp) {
506d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
5075424f32eSThomas Graf 		replay = nla_data(rp);
508d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
509d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
510d51d081dSJamal Hadi Salim 	}
511d51d081dSJamal Hadi Salim 
512d51d081dSJamal Hadi Salim 	if (lt) {
513d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
5145424f32eSThomas Graf 		ltime = nla_data(lt);
515d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
516d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
517d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
518d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
519d51d081dSJamal Hadi Salim 	}
520d51d081dSJamal Hadi Salim 
521cf5cb79fSThomas Graf 	if (et)
5225424f32eSThomas Graf 		x->replay_maxage = nla_get_u32(et);
523d51d081dSJamal Hadi Salim 
524cf5cb79fSThomas Graf 	if (rt)
5255424f32eSThomas Graf 		x->replay_maxdiff = nla_get_u32(rt);
526d51d081dSJamal Hadi Salim }
527d51d081dSJamal Hadi Salim 
528fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
529fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
5305424f32eSThomas Graf 					       struct nlattr **attrs,
5311da177e4SLinus Torvalds 					       int *errp)
5321da177e4SLinus Torvalds {
533fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
5341da177e4SLinus Torvalds 	int err = -ENOMEM;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	if (!x)
5371da177e4SLinus Torvalds 		goto error_no_put;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	copy_from_user_state(x, p);
5401da177e4SLinus Torvalds 
541a947b0a9SNicolas Dichtel 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
542a947b0a9SNicolas Dichtel 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
543a947b0a9SNicolas Dichtel 
54469b0137fSHerbert Xu 	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
5451a6509d9SHerbert Xu 		goto error;
5464447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
5474447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
5484447bb33SMartin Willi 		goto error;
5494447bb33SMartin Willi 	if (!x->props.aalgo) {
5504447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
55135a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
5521da177e4SLinus Torvalds 			goto error;
5534447bb33SMartin Willi 	}
55469b0137fSHerbert Xu 	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
5551da177e4SLinus Torvalds 		goto error;
5561da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
5571da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
55835a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
5591da177e4SLinus Torvalds 		goto error;
560fd21150aSThomas Graf 
561fd21150aSThomas Graf 	if (attrs[XFRMA_ENCAP]) {
562fd21150aSThomas Graf 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
563fd21150aSThomas Graf 				   sizeof(*x->encap), GFP_KERNEL);
564fd21150aSThomas Graf 		if (x->encap == NULL)
5651da177e4SLinus Torvalds 			goto error;
566fd21150aSThomas Graf 	}
567fd21150aSThomas Graf 
56835d2856bSMartin Willi 	if (attrs[XFRMA_TFCPAD])
56935d2856bSMartin Willi 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
57035d2856bSMartin Willi 
571fd21150aSThomas Graf 	if (attrs[XFRMA_COADDR]) {
572fd21150aSThomas Graf 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
573fd21150aSThomas Graf 				    sizeof(*x->coaddr), GFP_KERNEL);
574fd21150aSThomas Graf 		if (x->coaddr == NULL)
575060f02a3SNoriaki TAKAMIYA 			goto error;
576fd21150aSThomas Graf 	}
577fd21150aSThomas Graf 
5786f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &x->mark);
5796f26b61eSJamal Hadi Salim 
580a454f0ccSWei Yongjun 	err = __xfrm_init_state(x, false);
5811da177e4SLinus Torvalds 	if (err)
5821da177e4SLinus Torvalds 		goto error;
5831da177e4SLinus Torvalds 
584fd21150aSThomas Graf 	if (attrs[XFRMA_SEC_CTX] &&
585fd21150aSThomas Graf 	    security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
586df71837dSTrent Jaeger 		goto error;
587df71837dSTrent Jaeger 
588d8647b79SSteffen Klassert 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
589d8647b79SSteffen Klassert 					       attrs[XFRMA_REPLAY_ESN_VAL])))
590d8647b79SSteffen Klassert 		goto error;
591d8647b79SSteffen Klassert 
5921da177e4SLinus Torvalds 	x->km.seq = p->seq;
593b27aeadbSAlexey Dobriyan 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
594d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
595b27aeadbSAlexey Dobriyan 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
596d51d081dSJamal Hadi Salim 
5979fdc4883SSteffen Klassert 	if ((err = xfrm_init_replay(x)))
5989fdc4883SSteffen Klassert 		goto error;
599d51d081dSJamal Hadi Salim 
6009fdc4883SSteffen Klassert 	/* override default values from above */
601e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 0);
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	return x;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds error:
6061da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
6071da177e4SLinus Torvalds 	xfrm_state_put(x);
6081da177e4SLinus Torvalds error_no_put:
6091da177e4SLinus Torvalds 	*errp = err;
6101da177e4SLinus Torvalds 	return NULL;
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
61322e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
6145424f32eSThomas Graf 		struct nlattr **attrs)
6151da177e4SLinus Torvalds {
616fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6177b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
6181da177e4SLinus Torvalds 	struct xfrm_state *x;
6191da177e4SLinus Torvalds 	int err;
62026b15dadSJamal Hadi Salim 	struct km_event c;
6211da177e4SLinus Torvalds 
62235a7aa08SThomas Graf 	err = verify_newsa_info(p, attrs);
6231da177e4SLinus Torvalds 	if (err)
6241da177e4SLinus Torvalds 		return err;
6251da177e4SLinus Torvalds 
626fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
6271da177e4SLinus Torvalds 	if (!x)
6281da177e4SLinus Torvalds 		return err;
6291da177e4SLinus Torvalds 
63026b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
6311da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
6321da177e4SLinus Torvalds 		err = xfrm_state_add(x);
6331da177e4SLinus Torvalds 	else
6341da177e4SLinus Torvalds 		err = xfrm_state_update(x);
6351da177e4SLinus Torvalds 
6362e71029eSTetsuo Handa 	xfrm_audit_state_add(x, err ? 0 : 1, true);
637161a09e7SJoy Latten 
6381da177e4SLinus Torvalds 	if (err < 0) {
6391da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
64021380b81SHerbert Xu 		__xfrm_state_put(x);
6417d6dfe1fSPatrick McHardy 		goto out;
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 
64426b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
64515e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
646f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
64726b15dadSJamal Hadi Salim 
64826b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
6497d6dfe1fSPatrick McHardy out:
65026b15dadSJamal Hadi Salim 	xfrm_state_put(x);
6511da177e4SLinus Torvalds 	return err;
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
654fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
655fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
6565424f32eSThomas Graf 						 struct nlattr **attrs,
657eb2971b6SMasahide NAKAMURA 						 int *errp)
658eb2971b6SMasahide NAKAMURA {
659eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
6606f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
661eb2971b6SMasahide NAKAMURA 	int err;
6626f26b61eSJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
663eb2971b6SMasahide NAKAMURA 
664eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
665eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
6666f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
667eb2971b6SMasahide NAKAMURA 	} else {
668eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
669eb2971b6SMasahide NAKAMURA 
67035a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
671eb2971b6SMasahide NAKAMURA 		if (!saddr) {
672eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
673eb2971b6SMasahide NAKAMURA 			goto out;
674eb2971b6SMasahide NAKAMURA 		}
675eb2971b6SMasahide NAKAMURA 
6769abbffeeSMasahide NAKAMURA 		err = -ESRCH;
6776f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup_byaddr(net, mark,
6786f26b61eSJamal Hadi Salim 					     &p->daddr, saddr,
679221df1edSAlexey Dobriyan 					     p->proto, p->family);
680eb2971b6SMasahide NAKAMURA 	}
681eb2971b6SMasahide NAKAMURA 
682eb2971b6SMasahide NAKAMURA  out:
683eb2971b6SMasahide NAKAMURA 	if (!x && errp)
684eb2971b6SMasahide NAKAMURA 		*errp = err;
685eb2971b6SMasahide NAKAMURA 	return x;
686eb2971b6SMasahide NAKAMURA }
687eb2971b6SMasahide NAKAMURA 
68822e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
6895424f32eSThomas Graf 		struct nlattr **attrs)
6901da177e4SLinus Torvalds {
691fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6921da177e4SLinus Torvalds 	struct xfrm_state *x;
693eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
69426b15dadSJamal Hadi Salim 	struct km_event c;
6957b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
6961da177e4SLinus Torvalds 
697fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
6981da177e4SLinus Torvalds 	if (x == NULL)
699eb2971b6SMasahide NAKAMURA 		return err;
7001da177e4SLinus Torvalds 
7016f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
702c8c05a8eSCatherine Zhang 		goto out;
703c8c05a8eSCatherine Zhang 
7041da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
705c8c05a8eSCatherine Zhang 		err = -EPERM;
706c8c05a8eSCatherine Zhang 		goto out;
7071da177e4SLinus Torvalds 	}
7081da177e4SLinus Torvalds 
70926b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
710161a09e7SJoy Latten 
711c8c05a8eSCatherine Zhang 	if (err < 0)
712c8c05a8eSCatherine Zhang 		goto out;
71326b15dadSJamal Hadi Salim 
71426b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
71515e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
716f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
71726b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7181da177e4SLinus Torvalds 
719c8c05a8eSCatherine Zhang out:
7202e71029eSTetsuo Handa 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
721c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
72226b15dadSJamal Hadi Salim 	return err;
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
7261da177e4SLinus Torvalds {
727f778a636SMathias Krause 	memset(p, 0, sizeof(*p));
7281da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
7291da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
7301da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
7311da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
732e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
733e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay, &p->stats.replay);
734e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
73554489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
7361da177e4SLinus Torvalds 	p->mode = x->props.mode;
7371da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
7381da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
7391da177e4SLinus Torvalds 	p->family = x->props.family;
7401da177e4SLinus Torvalds 	p->flags = x->props.flags;
7411da177e4SLinus Torvalds 	p->seq = x->km.seq;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds struct xfrm_dump_info {
7451da177e4SLinus Torvalds 	struct sk_buff *in_skb;
7461da177e4SLinus Torvalds 	struct sk_buff *out_skb;
7471da177e4SLinus Torvalds 	u32 nlmsg_seq;
7481da177e4SLinus Torvalds 	u16 nlmsg_flags;
7491da177e4SLinus Torvalds };
7501da177e4SLinus Torvalds 
751c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
752c0144beaSThomas Graf {
753c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
754c0144beaSThomas Graf 	struct nlattr *attr;
75568325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
756c0144beaSThomas Graf 
757c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
758c0144beaSThomas Graf 	if (attr == NULL)
759c0144beaSThomas Graf 		return -EMSGSIZE;
760c0144beaSThomas Graf 
761c0144beaSThomas Graf 	uctx = nla_data(attr);
762c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
763c0144beaSThomas Graf 	uctx->len = ctx_size;
764c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
765c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
766c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
767c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
768c0144beaSThomas Graf 
769c0144beaSThomas Graf 	return 0;
770c0144beaSThomas Graf }
771c0144beaSThomas Graf 
7724447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
7734447bb33SMartin Willi {
7744447bb33SMartin Willi 	struct xfrm_algo *algo;
7754447bb33SMartin Willi 	struct nlattr *nla;
7764447bb33SMartin Willi 
7774447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
7784447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
7794447bb33SMartin Willi 	if (!nla)
7804447bb33SMartin Willi 		return -EMSGSIZE;
7814447bb33SMartin Willi 
7824447bb33SMartin Willi 	algo = nla_data(nla);
7834c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
7844447bb33SMartin Willi 	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
7854447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
7864447bb33SMartin Willi 
7874447bb33SMartin Willi 	return 0;
7884447bb33SMartin Willi }
7894447bb33SMartin Willi 
79068325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
79168325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
79268325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
79368325d3bSHerbert Xu 				    struct sk_buff *skb)
7941da177e4SLinus Torvalds {
7951d1e34ddSDavid S. Miller 	int ret = 0;
7961d1e34ddSDavid S. Miller 
7971da177e4SLinus Torvalds 	copy_to_user_state(x, p);
7981da177e4SLinus Torvalds 
799a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
800a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
801a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
802a947b0a9SNicolas Dichtel 		if (ret)
803a947b0a9SNicolas Dichtel 			goto out;
804a947b0a9SNicolas Dichtel 	}
805a947b0a9SNicolas Dichtel 
8061d1e34ddSDavid S. Miller 	if (x->coaddr) {
8071d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
8081d1e34ddSDavid S. Miller 		if (ret)
8091d1e34ddSDavid S. Miller 			goto out;
8101d1e34ddSDavid S. Miller 	}
8111d1e34ddSDavid S. Miller 	if (x->lastused) {
812de95c4a4SNicolas Dichtel 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
813de95c4a4SNicolas Dichtel 					XFRMA_PAD);
8141d1e34ddSDavid S. Miller 		if (ret)
8151d1e34ddSDavid S. Miller 			goto out;
8161d1e34ddSDavid S. Miller 	}
8171d1e34ddSDavid S. Miller 	if (x->aead) {
8181d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
8191d1e34ddSDavid S. Miller 		if (ret)
8201d1e34ddSDavid S. Miller 			goto out;
8211d1e34ddSDavid S. Miller 	}
8221d1e34ddSDavid S. Miller 	if (x->aalg) {
8231d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
8241d1e34ddSDavid S. Miller 		if (!ret)
8251d1e34ddSDavid S. Miller 			ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
8261d1e34ddSDavid S. Miller 				      xfrm_alg_auth_len(x->aalg), x->aalg);
8271d1e34ddSDavid S. Miller 		if (ret)
8281d1e34ddSDavid S. Miller 			goto out;
8291d1e34ddSDavid S. Miller 	}
8301d1e34ddSDavid S. Miller 	if (x->ealg) {
8311d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
8321d1e34ddSDavid S. Miller 		if (ret)
8331d1e34ddSDavid S. Miller 			goto out;
8341d1e34ddSDavid S. Miller 	}
8351d1e34ddSDavid S. Miller 	if (x->calg) {
8361d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
8371d1e34ddSDavid S. Miller 		if (ret)
8381d1e34ddSDavid S. Miller 			goto out;
8391d1e34ddSDavid S. Miller 	}
8401d1e34ddSDavid S. Miller 	if (x->encap) {
8411d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
8421d1e34ddSDavid S. Miller 		if (ret)
8431d1e34ddSDavid S. Miller 			goto out;
8441d1e34ddSDavid S. Miller 	}
8451d1e34ddSDavid S. Miller 	if (x->tfcpad) {
8461d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
8471d1e34ddSDavid S. Miller 		if (ret)
8481d1e34ddSDavid S. Miller 			goto out;
8491d1e34ddSDavid S. Miller 	}
8501d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
8511d1e34ddSDavid S. Miller 	if (ret)
8521d1e34ddSDavid S. Miller 		goto out;
853f293a5e3Sdingzhi 	if (x->replay_esn)
8541d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
855d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
8561d1e34ddSDavid S. Miller 			      x->replay_esn);
857f293a5e3Sdingzhi 	else
858f293a5e3Sdingzhi 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
859f293a5e3Sdingzhi 			      &x->replay);
8601d1e34ddSDavid S. Miller 	if (ret)
8611d1e34ddSDavid S. Miller 		goto out;
8621d1e34ddSDavid S. Miller 	if (x->security)
8631d1e34ddSDavid S. Miller 		ret = copy_sec_ctx(x->security, skb);
8641d1e34ddSDavid S. Miller out:
8651d1e34ddSDavid S. Miller 	return ret;
86668325d3bSHerbert Xu }
86768325d3bSHerbert Xu 
86868325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
86968325d3bSHerbert Xu {
87068325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
87168325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
87268325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
87368325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
87468325d3bSHerbert Xu 	struct nlmsghdr *nlh;
87568325d3bSHerbert Xu 	int err;
87668325d3bSHerbert Xu 
87715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
87868325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
87968325d3bSHerbert Xu 	if (nlh == NULL)
88068325d3bSHerbert Xu 		return -EMSGSIZE;
88168325d3bSHerbert Xu 
88268325d3bSHerbert Xu 	p = nlmsg_data(nlh);
88368325d3bSHerbert Xu 
88468325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
8851d1e34ddSDavid S. Miller 	if (err) {
8869825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
88768325d3bSHerbert Xu 		return err;
8881da177e4SLinus Torvalds 	}
8891d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
8901d1e34ddSDavid S. Miller 	return 0;
8911d1e34ddSDavid S. Miller }
8921da177e4SLinus Torvalds 
8934c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
8944c563f76STimo Teras {
8954c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
896283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
897283bc9f3SFan Du 	struct net *net = sock_net(sk);
898283bc9f3SFan Du 
8991ba5bf99SVegard Nossum 	if (cb->args[0])
900283bc9f3SFan Du 		xfrm_state_walk_done(walk, net);
9014c563f76STimo Teras 	return 0;
9024c563f76STimo Teras }
9034c563f76STimo Teras 
904d3623099SNicolas Dichtel static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
9051da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
9061da177e4SLinus Torvalds {
907fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
9084c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
9091da177e4SLinus Torvalds 	struct xfrm_dump_info info;
9101da177e4SLinus Torvalds 
9114c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
9124c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
9134c563f76STimo Teras 
9141da177e4SLinus Torvalds 	info.in_skb = cb->skb;
9151da177e4SLinus Torvalds 	info.out_skb = skb;
9161da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
9171da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
9184c563f76STimo Teras 
9194c563f76STimo Teras 	if (!cb->args[0]) {
920d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
921870a2df4SNicolas Dichtel 		struct xfrm_address_filter *filter = NULL;
922d3623099SNicolas Dichtel 		u8 proto = 0;
923d3623099SNicolas Dichtel 		int err;
924d3623099SNicolas Dichtel 
925d3623099SNicolas Dichtel 		err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
926d3623099SNicolas Dichtel 				  xfrma_policy);
927d3623099SNicolas Dichtel 		if (err < 0)
928d3623099SNicolas Dichtel 			return err;
929d3623099SNicolas Dichtel 
930870a2df4SNicolas Dichtel 		if (attrs[XFRMA_ADDRESS_FILTER]) {
931df367561SAndrzej Hajda 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
932df367561SAndrzej Hajda 					 sizeof(*filter), GFP_KERNEL);
933d3623099SNicolas Dichtel 			if (filter == NULL)
934d3623099SNicolas Dichtel 				return -ENOMEM;
935d3623099SNicolas Dichtel 		}
936d3623099SNicolas Dichtel 
937d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
938d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
939d3623099SNicolas Dichtel 
940d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
9411ba5bf99SVegard Nossum 		cb->args[0] = 1;
9424c563f76STimo Teras 	}
9434c563f76STimo Teras 
944fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	return skb->len;
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
9501da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
9511da177e4SLinus Torvalds {
9521da177e4SLinus Torvalds 	struct xfrm_dump_info info;
9531da177e4SLinus Torvalds 	struct sk_buff *skb;
954864745d2SMathias Krause 	int err;
9551da177e4SLinus Torvalds 
9567deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
9571da177e4SLinus Torvalds 	if (!skb)
9581da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	info.in_skb = in_skb;
9611da177e4SLinus Torvalds 	info.out_skb = skb;
9621da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
9631da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
9641da177e4SLinus Torvalds 
965864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
966864745d2SMathias Krause 	if (err) {
9671da177e4SLinus Torvalds 		kfree_skb(skb);
968864745d2SMathias Krause 		return ERR_PTR(err);
9691da177e4SLinus Torvalds 	}
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	return skb;
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
97421ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
97521ee543eSMichal Kubecek  * Must be called with RCU read lock.
97621ee543eSMichal Kubecek  */
97721ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
97821ee543eSMichal Kubecek 				       u32 pid, unsigned int group)
97921ee543eSMichal Kubecek {
98021ee543eSMichal Kubecek 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
98121ee543eSMichal Kubecek 
98221ee543eSMichal Kubecek 	if (nlsk)
98321ee543eSMichal Kubecek 		return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
98421ee543eSMichal Kubecek 	else
98521ee543eSMichal Kubecek 		return -1;
98621ee543eSMichal Kubecek }
98721ee543eSMichal Kubecek 
9887deb2264SThomas Graf static inline size_t xfrm_spdinfo_msgsize(void)
9897deb2264SThomas Graf {
9907deb2264SThomas Graf 	return NLMSG_ALIGN(4)
9917deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
992880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
993880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
994880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
9957deb2264SThomas Graf }
9967deb2264SThomas Graf 
997e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
99815e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
999ecfd6b18SJamal Hadi Salim {
10005a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
10015a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
10025a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1003880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1004ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
10051d1e34ddSDavid S. Miller 	int err;
1006ecfd6b18SJamal Hadi Salim 	u32 *f;
1007880a6fabSChristophe Gouault 	unsigned lseq;
1008ecfd6b18SJamal Hadi Salim 
100915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
101025985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1011ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1012ecfd6b18SJamal Hadi Salim 
1013ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1014ecfd6b18SJamal Hadi Salim 	*f = flags;
1015e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
10165a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
10175a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
10185a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
10195a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
10205a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
10215a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
10225a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
10235a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1024ecfd6b18SJamal Hadi Salim 
1025880a6fabSChristophe Gouault 	do {
1026880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1027880a6fabSChristophe Gouault 
1028880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1029880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1030880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1031880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1032880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1033880a6fabSChristophe Gouault 
10341d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
10351d1e34ddSDavid S. Miller 	if (!err)
10361d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1037880a6fabSChristophe Gouault 	if (!err)
1038880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1039880a6fabSChristophe Gouault 	if (!err)
1040880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
10411d1e34ddSDavid S. Miller 	if (err) {
10421d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
10431d1e34ddSDavid S. Miller 		return err;
10441d1e34ddSDavid S. Miller 	}
1045ecfd6b18SJamal Hadi Salim 
1046053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1047053c095aSJohannes Berg 	return 0;
1048ecfd6b18SJamal Hadi Salim }
1049ecfd6b18SJamal Hadi Salim 
1050880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1051880a6fabSChristophe Gouault 			    struct nlattr **attrs)
1052880a6fabSChristophe Gouault {
1053880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1054880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1055880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1056880a6fabSChristophe Gouault 
1057880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1058880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1059880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1060880a6fabSChristophe Gouault 
1061880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1062880a6fabSChristophe Gouault 			return -EINVAL;
1063880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1064880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1065880a6fabSChristophe Gouault 			return -EINVAL;
1066880a6fabSChristophe Gouault 	}
1067880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1068880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1069880a6fabSChristophe Gouault 
1070880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1071880a6fabSChristophe Gouault 			return -EINVAL;
1072880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1073880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1074880a6fabSChristophe Gouault 			return -EINVAL;
1075880a6fabSChristophe Gouault 	}
1076880a6fabSChristophe Gouault 
1077880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1078880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1079880a6fabSChristophe Gouault 		if (thresh4) {
1080880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1081880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1082880a6fabSChristophe Gouault 		}
1083880a6fabSChristophe Gouault 		if (thresh6) {
1084880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1085880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1086880a6fabSChristophe Gouault 		}
1087880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1088880a6fabSChristophe Gouault 
1089880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1090880a6fabSChristophe Gouault 	}
1091880a6fabSChristophe Gouault 
1092880a6fabSChristophe Gouault 	return 0;
1093880a6fabSChristophe Gouault }
1094880a6fabSChristophe Gouault 
1095ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
10965424f32eSThomas Graf 		struct nlattr **attrs)
1097ecfd6b18SJamal Hadi Salim {
1098a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1099ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
11007b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
110115e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1102ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
1103ecfd6b18SJamal Hadi Salim 
11047deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1105ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1106ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1107ecfd6b18SJamal Hadi Salim 
110815e47304SEric W. Biederman 	if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1109ecfd6b18SJamal Hadi Salim 		BUG();
1110ecfd6b18SJamal Hadi Salim 
111115e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1112ecfd6b18SJamal Hadi Salim }
1113ecfd6b18SJamal Hadi Salim 
11147deb2264SThomas Graf static inline size_t xfrm_sadinfo_msgsize(void)
11157deb2264SThomas Graf {
11167deb2264SThomas Graf 	return NLMSG_ALIGN(4)
11177deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
11187deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
11197deb2264SThomas Graf }
11207deb2264SThomas Graf 
1121e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
112215e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
112328d8909bSJamal Hadi Salim {
1124af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1125af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
112628d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
11271d1e34ddSDavid S. Miller 	int err;
112828d8909bSJamal Hadi Salim 	u32 *f;
112928d8909bSJamal Hadi Salim 
113015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
113125985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
113228d8909bSJamal Hadi Salim 		return -EMSGSIZE;
113328d8909bSJamal Hadi Salim 
113428d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
113528d8909bSJamal Hadi Salim 	*f = flags;
1136e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
113728d8909bSJamal Hadi Salim 
1138af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1139af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1140af11e316SJamal Hadi Salim 
11411d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
11421d1e34ddSDavid S. Miller 	if (!err)
11431d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
11441d1e34ddSDavid S. Miller 	if (err) {
11451d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
11461d1e34ddSDavid S. Miller 		return err;
11471d1e34ddSDavid S. Miller 	}
114828d8909bSJamal Hadi Salim 
1149053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1150053c095aSJohannes Berg 	return 0;
115128d8909bSJamal Hadi Salim }
115228d8909bSJamal Hadi Salim 
115328d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
11545424f32eSThomas Graf 		struct nlattr **attrs)
115528d8909bSJamal Hadi Salim {
1156a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
115728d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
11587b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
115915e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
116028d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
116128d8909bSJamal Hadi Salim 
11627deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
116328d8909bSJamal Hadi Salim 	if (r_skb == NULL)
116428d8909bSJamal Hadi Salim 		return -ENOMEM;
116528d8909bSJamal Hadi Salim 
116615e47304SEric W. Biederman 	if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
116728d8909bSJamal Hadi Salim 		BUG();
116828d8909bSJamal Hadi Salim 
116915e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
117028d8909bSJamal Hadi Salim }
117128d8909bSJamal Hadi Salim 
117222e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
11735424f32eSThomas Graf 		struct nlattr **attrs)
11741da177e4SLinus Torvalds {
1175fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
11767b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
11771da177e4SLinus Torvalds 	struct xfrm_state *x;
11781da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1179eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
11801da177e4SLinus Torvalds 
1181fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
11821da177e4SLinus Torvalds 	if (x == NULL)
11831da177e4SLinus Torvalds 		goto out_noput;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
11861da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
11871da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
11881da177e4SLinus Torvalds 	} else {
118915e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
11901da177e4SLinus Torvalds 	}
11911da177e4SLinus Torvalds 	xfrm_state_put(x);
11921da177e4SLinus Torvalds out_noput:
11931da177e4SLinus Torvalds 	return err;
11941da177e4SLinus Torvalds }
11951da177e4SLinus Torvalds 
119622e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
11975424f32eSThomas Graf 		struct nlattr **attrs)
11981da177e4SLinus Torvalds {
1199fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12001da177e4SLinus Torvalds 	struct xfrm_state *x;
12011da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
12021da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
12031da177e4SLinus Torvalds 	xfrm_address_t *daddr;
12041da177e4SLinus Torvalds 	int family;
12051da177e4SLinus Torvalds 	int err;
12066f26b61eSJamal Hadi Salim 	u32 mark;
12076f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
12081da177e4SLinus Torvalds 
12097b67c857SThomas Graf 	p = nlmsg_data(nlh);
1210776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
12111da177e4SLinus Torvalds 	if (err)
12121da177e4SLinus Torvalds 		goto out_noput;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	family = p->info.family;
12151da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	x = NULL;
12186f26b61eSJamal Hadi Salim 
12196f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
12201da177e4SLinus Torvalds 	if (p->info.seq) {
12216f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
122270e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
12231da177e4SLinus Torvalds 			xfrm_state_put(x);
12241da177e4SLinus Torvalds 			x = NULL;
12251da177e4SLinus Torvalds 		}
12261da177e4SLinus Torvalds 	}
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	if (!x)
12296f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
12301da177e4SLinus Torvalds 				  p->info.id.proto, daddr,
12311da177e4SLinus Torvalds 				  &p->info.saddr, 1,
12321da177e4SLinus Torvalds 				  family);
12331da177e4SLinus Torvalds 	err = -ENOENT;
12341da177e4SLinus Torvalds 	if (x == NULL)
12351da177e4SLinus Torvalds 		goto out_noput;
12361da177e4SLinus Torvalds 
1237658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1238658b219eSHerbert Xu 	if (err)
1239658b219eSHerbert Xu 		goto out;
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
12421da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
12431da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
12441da177e4SLinus Torvalds 		goto out;
12451da177e4SLinus Torvalds 	}
12461da177e4SLinus Torvalds 
124715e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds out:
12501da177e4SLinus Torvalds 	xfrm_state_put(x);
12511da177e4SLinus Torvalds out_noput:
12521da177e4SLinus Torvalds 	return err;
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
1255b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
12561da177e4SLinus Torvalds {
12571da177e4SLinus Torvalds 	switch (dir) {
12581da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
12591da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
12601da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
12611da177e4SLinus Torvalds 		break;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	default:
12641da177e4SLinus Torvalds 		return -EINVAL;
12653ff50b79SStephen Hemminger 	}
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	return 0;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
1270b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1271f7b6983fSMasahide NAKAMURA {
1272f7b6983fSMasahide NAKAMURA 	switch (type) {
1273f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1274f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1275f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1276f7b6983fSMasahide NAKAMURA #endif
1277f7b6983fSMasahide NAKAMURA 		break;
1278f7b6983fSMasahide NAKAMURA 
1279f7b6983fSMasahide NAKAMURA 	default:
1280f7b6983fSMasahide NAKAMURA 		return -EINVAL;
12813ff50b79SStephen Hemminger 	}
1282f7b6983fSMasahide NAKAMURA 
1283f7b6983fSMasahide NAKAMURA 	return 0;
1284f7b6983fSMasahide NAKAMURA }
1285f7b6983fSMasahide NAKAMURA 
12861da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
12871da177e4SLinus Torvalds {
1288e682adf0SFan Du 	int ret;
1289e682adf0SFan Du 
12901da177e4SLinus Torvalds 	switch (p->share) {
12911da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
12921da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
12931da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
12941da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
12951da177e4SLinus Torvalds 		break;
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	default:
12981da177e4SLinus Torvalds 		return -EINVAL;
12993ff50b79SStephen Hemminger 	}
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	switch (p->action) {
13021da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
13031da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
13041da177e4SLinus Torvalds 		break;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	default:
13071da177e4SLinus Torvalds 		return -EINVAL;
13083ff50b79SStephen Hemminger 	}
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	switch (p->sel.family) {
13111da177e4SLinus Torvalds 	case AF_INET:
13121da177e4SLinus Torvalds 		break;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	case AF_INET6:
1315dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
13161da177e4SLinus Torvalds 		break;
13171da177e4SLinus Torvalds #else
13181da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
13191da177e4SLinus Torvalds #endif
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	default:
13221da177e4SLinus Torvalds 		return -EINVAL;
13233ff50b79SStephen Hemminger 	}
13241da177e4SLinus Torvalds 
1325e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1326e682adf0SFan Du 	if (ret)
1327e682adf0SFan Du 		return ret;
1328e682adf0SFan Du 	if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1329e682adf0SFan Du 		return -EINVAL;
1330e682adf0SFan Du 
1331e682adf0SFan Du 	return 0;
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
13345424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1335df71837dSTrent Jaeger {
13365424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1337df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1338df71837dSTrent Jaeger 
1339df71837dSTrent Jaeger 	if (!rt)
1340df71837dSTrent Jaeger 		return 0;
1341df71837dSTrent Jaeger 
13425424f32eSThomas Graf 	uctx = nla_data(rt);
134352a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1344df71837dSTrent Jaeger }
1345df71837dSTrent Jaeger 
13461da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
13471da177e4SLinus Torvalds 			   int nr)
13481da177e4SLinus Torvalds {
13491da177e4SLinus Torvalds 	int i;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
13521da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
13531da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
13561da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
13571da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
13581da177e4SLinus Torvalds 		t->reqid = ut->reqid;
13591da177e4SLinus Torvalds 		t->mode = ut->mode;
13601da177e4SLinus Torvalds 		t->share = ut->share;
13611da177e4SLinus Torvalds 		t->optional = ut->optional;
13621da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
13631da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
13641da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1365c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1366c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
13678511d01dSMiika Komu 		t->encap_family = ut->family;
13681da177e4SLinus Torvalds 	}
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
1371b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1372b4ad86bfSDavid S. Miller {
1373b4ad86bfSDavid S. Miller 	int i;
1374b4ad86bfSDavid S. Miller 
1375b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1376b4ad86bfSDavid S. Miller 		return -EINVAL;
1377b4ad86bfSDavid S. Miller 
1378b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1379b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1380b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1381b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1382b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1383b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1384b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1385b4ad86bfSDavid S. Miller 		 */
1386b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1387b4ad86bfSDavid S. Miller 			ut[i].family = family;
1388b4ad86bfSDavid S. Miller 
1389b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1390b4ad86bfSDavid S. Miller 		case AF_INET:
1391b4ad86bfSDavid S. Miller 			break;
1392dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1393b4ad86bfSDavid S. Miller 		case AF_INET6:
1394b4ad86bfSDavid S. Miller 			break;
1395b4ad86bfSDavid S. Miller #endif
1396b4ad86bfSDavid S. Miller 		default:
1397b4ad86bfSDavid S. Miller 			return -EINVAL;
13983ff50b79SStephen Hemminger 		}
1399b4ad86bfSDavid S. Miller 	}
1400b4ad86bfSDavid S. Miller 
1401b4ad86bfSDavid S. Miller 	return 0;
1402b4ad86bfSDavid S. Miller }
1403b4ad86bfSDavid S. Miller 
14045424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
14051da177e4SLinus Torvalds {
14065424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	if (!rt) {
14091da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
14101da177e4SLinus Torvalds 	} else {
14115424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
14125424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1413b4ad86bfSDavid S. Miller 		int err;
14141da177e4SLinus Torvalds 
1415b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1416b4ad86bfSDavid S. Miller 		if (err)
1417b4ad86bfSDavid S. Miller 			return err;
14181da177e4SLinus Torvalds 
14195424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
14201da177e4SLinus Torvalds 	}
14211da177e4SLinus Torvalds 	return 0;
14221da177e4SLinus Torvalds }
14231da177e4SLinus Torvalds 
14245424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1425f7b6983fSMasahide NAKAMURA {
14265424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1427f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1428b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1429f7b6983fSMasahide NAKAMURA 	int err;
1430f7b6983fSMasahide NAKAMURA 
1431f7b6983fSMasahide NAKAMURA 	if (rt) {
14325424f32eSThomas Graf 		upt = nla_data(rt);
1433f7b6983fSMasahide NAKAMURA 		type = upt->type;
1434f7b6983fSMasahide NAKAMURA 	}
1435f7b6983fSMasahide NAKAMURA 
1436f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1437f7b6983fSMasahide NAKAMURA 	if (err)
1438f7b6983fSMasahide NAKAMURA 		return err;
1439f7b6983fSMasahide NAKAMURA 
1440f7b6983fSMasahide NAKAMURA 	*tp = type;
1441f7b6983fSMasahide NAKAMURA 	return 0;
1442f7b6983fSMasahide NAKAMURA }
1443f7b6983fSMasahide NAKAMURA 
14441da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
14451da177e4SLinus Torvalds {
14461da177e4SLinus Torvalds 	xp->priority = p->priority;
14471da177e4SLinus Torvalds 	xp->index = p->index;
14481da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
14491da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
14501da177e4SLinus Torvalds 	xp->action = p->action;
14511da177e4SLinus Torvalds 	xp->flags = p->flags;
14521da177e4SLinus Torvalds 	xp->family = p->sel.family;
14531da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
14541da177e4SLinus Torvalds }
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
14571da177e4SLinus Torvalds {
14587b789836SMathias Krause 	memset(p, 0, sizeof(*p));
14591da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
14601da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
14611da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
14621da177e4SLinus Torvalds 	p->priority = xp->priority;
14631da177e4SLinus Torvalds 	p->index = xp->index;
14641da177e4SLinus Torvalds 	p->sel.family = xp->family;
14651da177e4SLinus Torvalds 	p->dir = dir;
14661da177e4SLinus Torvalds 	p->action = xp->action;
14671da177e4SLinus Torvalds 	p->flags = xp->flags;
14681da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
1471fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
14721da177e4SLinus Torvalds {
1473fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
14741da177e4SLinus Torvalds 	int err;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	if (!xp) {
14771da177e4SLinus Torvalds 		*errp = -ENOMEM;
14781da177e4SLinus Torvalds 		return NULL;
14791da177e4SLinus Torvalds 	}
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1482df71837dSTrent Jaeger 
148335a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1484f7b6983fSMasahide NAKAMURA 	if (err)
1485f7b6983fSMasahide NAKAMURA 		goto error;
1486f7b6983fSMasahide NAKAMURA 
148735a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
148835a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1489f7b6983fSMasahide NAKAMURA 	if (err)
1490f7b6983fSMasahide NAKAMURA 		goto error;
14911da177e4SLinus Torvalds 
1492295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1493295fae56SJamal Hadi Salim 
14941da177e4SLinus Torvalds 	return xp;
1495f7b6983fSMasahide NAKAMURA  error:
1496f7b6983fSMasahide NAKAMURA 	*errp = err;
149712a169e7SHerbert Xu 	xp->walk.dead = 1;
149864c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1499f7b6983fSMasahide NAKAMURA 	return NULL;
15001da177e4SLinus Torvalds }
15011da177e4SLinus Torvalds 
150222e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
15035424f32eSThomas Graf 		struct nlattr **attrs)
15041da177e4SLinus Torvalds {
1505fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
15067b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
15071da177e4SLinus Torvalds 	struct xfrm_policy *xp;
150826b15dadSJamal Hadi Salim 	struct km_event c;
15091da177e4SLinus Torvalds 	int err;
15101da177e4SLinus Torvalds 	int excl;
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
15131da177e4SLinus Torvalds 	if (err)
15141da177e4SLinus Torvalds 		return err;
151535a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1516df71837dSTrent Jaeger 	if (err)
1517df71837dSTrent Jaeger 		return err;
15181da177e4SLinus Torvalds 
1519fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
15201da177e4SLinus Torvalds 	if (!xp)
15211da177e4SLinus Torvalds 		return err;
15221da177e4SLinus Torvalds 
152325985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
152426b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
152526b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
152626b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
15271da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
15281da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
15292e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1530161a09e7SJoy Latten 
15311da177e4SLinus Torvalds 	if (err) {
153203e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
15331da177e4SLinus Torvalds 		kfree(xp);
15341da177e4SLinus Torvalds 		return err;
15351da177e4SLinus Torvalds 	}
15361da177e4SLinus Torvalds 
1537f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
153826b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
153915e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
154026b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
154126b15dadSJamal Hadi Salim 
15421da177e4SLinus Torvalds 	xfrm_pol_put(xp);
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	return 0;
15451da177e4SLinus Torvalds }
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
15481da177e4SLinus Torvalds {
15491da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
15501da177e4SLinus Torvalds 	int i;
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
15531da177e4SLinus Torvalds 		return 0;
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
15561da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
15571da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
15581da177e4SLinus Torvalds 
15591f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
15601da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
15618511d01dSMiika Komu 		up->family = kp->encap_family;
15621da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
15631da177e4SLinus Torvalds 		up->reqid = kp->reqid;
15641da177e4SLinus Torvalds 		up->mode = kp->mode;
15651da177e4SLinus Torvalds 		up->share = kp->share;
15661da177e4SLinus Torvalds 		up->optional = kp->optional;
15671da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
15681da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
15691da177e4SLinus Torvalds 		up->calgos = kp->calgos;
15701da177e4SLinus Torvalds 	}
15711da177e4SLinus Torvalds 
1572c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1573c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1574df71837dSTrent Jaeger }
1575df71837dSTrent Jaeger 
15760d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
15770d681623SSerge Hallyn {
15780d681623SSerge Hallyn 	if (x->security) {
15790d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
15800d681623SSerge Hallyn 	}
15810d681623SSerge Hallyn 	return 0;
15820d681623SSerge Hallyn }
15830d681623SSerge Hallyn 
15840d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
15850d681623SSerge Hallyn {
15861d1e34ddSDavid S. Miller 	if (xp->security)
15870d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
15880d681623SSerge Hallyn 	return 0;
15890d681623SSerge Hallyn }
1590cfbfd45aSThomas Graf static inline size_t userpolicy_type_attrsize(void)
1591cfbfd45aSThomas Graf {
1592cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1593cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1594cfbfd45aSThomas Graf #else
1595cfbfd45aSThomas Graf 	return 0;
1596cfbfd45aSThomas Graf #endif
1597cfbfd45aSThomas Graf }
15980d681623SSerge Hallyn 
1599f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1600b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1601f7b6983fSMasahide NAKAMURA {
1602c0144beaSThomas Graf 	struct xfrm_userpolicy_type upt = {
1603c0144beaSThomas Graf 		.type = type,
1604c0144beaSThomas Graf 	};
1605f7b6983fSMasahide NAKAMURA 
1606c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1607f7b6983fSMasahide NAKAMURA }
1608f7b6983fSMasahide NAKAMURA 
1609f7b6983fSMasahide NAKAMURA #else
1610b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1611f7b6983fSMasahide NAKAMURA {
1612f7b6983fSMasahide NAKAMURA 	return 0;
1613f7b6983fSMasahide NAKAMURA }
1614f7b6983fSMasahide NAKAMURA #endif
1615f7b6983fSMasahide NAKAMURA 
16161da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
16171da177e4SLinus Torvalds {
16181da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
16191da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
16201da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
16211da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
16221da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
16231d1e34ddSDavid S. Miller 	int err;
16241da177e4SLinus Torvalds 
162515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
162679b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
162779b8b7f4SThomas Graf 	if (nlh == NULL)
162879b8b7f4SThomas Graf 		return -EMSGSIZE;
16291da177e4SLinus Torvalds 
16307b67c857SThomas Graf 	p = nlmsg_data(nlh);
16311da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
16321d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
16331d1e34ddSDavid S. Miller 	if (!err)
16341d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
16351d1e34ddSDavid S. Miller 	if (!err)
16361d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
16371d1e34ddSDavid S. Miller 	if (!err)
16381d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
16391d1e34ddSDavid S. Miller 	if (err) {
16401d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
16411d1e34ddSDavid S. Miller 		return err;
16421d1e34ddSDavid S. Miller 	}
16439825069dSThomas Graf 	nlmsg_end(skb, nlh);
16441da177e4SLinus Torvalds 	return 0;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
16474c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
16484c563f76STimo Teras {
16494c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1650283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
16514c563f76STimo Teras 
1652283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
16534c563f76STimo Teras 	return 0;
16544c563f76STimo Teras }
16554c563f76STimo Teras 
16561da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
16571da177e4SLinus Torvalds {
1658fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
16594c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
16601da177e4SLinus Torvalds 	struct xfrm_dump_info info;
16611da177e4SLinus Torvalds 
16624c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
16634c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
16644c563f76STimo Teras 
16651da177e4SLinus Torvalds 	info.in_skb = cb->skb;
16661da177e4SLinus Torvalds 	info.out_skb = skb;
16671da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
16681da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
16694c563f76STimo Teras 
16704c563f76STimo Teras 	if (!cb->args[0]) {
16714c563f76STimo Teras 		cb->args[0] = 1;
16724c563f76STimo Teras 		xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
16734c563f76STimo Teras 	}
16744c563f76STimo Teras 
1675fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	return skb->len;
16781da177e4SLinus Torvalds }
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
16811da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
16821da177e4SLinus Torvalds 					  int dir, u32 seq)
16831da177e4SLinus Torvalds {
16841da177e4SLinus Torvalds 	struct xfrm_dump_info info;
16851da177e4SLinus Torvalds 	struct sk_buff *skb;
1686c2546372SMathias Krause 	int err;
16871da177e4SLinus Torvalds 
16887deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16891da177e4SLinus Torvalds 	if (!skb)
16901da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	info.in_skb = in_skb;
16931da177e4SLinus Torvalds 	info.out_skb = skb;
16941da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
16951da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
16961da177e4SLinus Torvalds 
1697c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1698c2546372SMathias Krause 	if (err) {
16991da177e4SLinus Torvalds 		kfree_skb(skb);
1700c2546372SMathias Krause 		return ERR_PTR(err);
17011da177e4SLinus Torvalds 	}
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	return skb;
17041da177e4SLinus Torvalds }
17051da177e4SLinus Torvalds 
170622e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
17075424f32eSThomas Graf 		struct nlattr **attrs)
17081da177e4SLinus Torvalds {
1709fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
17101da177e4SLinus Torvalds 	struct xfrm_policy *xp;
17111da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1712b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
17131da177e4SLinus Torvalds 	int err;
171426b15dadSJamal Hadi Salim 	struct km_event c;
17151da177e4SLinus Torvalds 	int delete;
1716295fae56SJamal Hadi Salim 	struct xfrm_mark m;
1717295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
17181da177e4SLinus Torvalds 
17197b67c857SThomas Graf 	p = nlmsg_data(nlh);
17201da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
17211da177e4SLinus Torvalds 
172235a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1723f7b6983fSMasahide NAKAMURA 	if (err)
1724f7b6983fSMasahide NAKAMURA 		return err;
1725f7b6983fSMasahide NAKAMURA 
17261da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
17271da177e4SLinus Torvalds 	if (err)
17281da177e4SLinus Torvalds 		return err;
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	if (p->index)
1731295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1732df71837dSTrent Jaeger 	else {
17335424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
173403e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1735df71837dSTrent Jaeger 
173635a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
1737df71837dSTrent Jaeger 		if (err)
1738df71837dSTrent Jaeger 			return err;
1739df71837dSTrent Jaeger 
17402c8dd116SDenis V. Lunev 		ctx = NULL;
1741df71837dSTrent Jaeger 		if (rt) {
17425424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1743df71837dSTrent Jaeger 
174452a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
174503e1ad7bSPaul Moore 			if (err)
1746df71837dSTrent Jaeger 				return err;
17472c8dd116SDenis V. Lunev 		}
1748295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
17496f26b61eSJamal Hadi Salim 					   ctx, delete, &err);
175003e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
1751df71837dSTrent Jaeger 	}
17521da177e4SLinus Torvalds 	if (xp == NULL)
17531da177e4SLinus Torvalds 		return -ENOENT;
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 	if (!delete) {
17561da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
17591da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
17601da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
17611da177e4SLinus Torvalds 		} else {
1762a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
176315e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
17641da177e4SLinus Torvalds 		}
176526b15dadSJamal Hadi Salim 	} else {
17662e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
176713fcfbb0SDavid S. Miller 
176813fcfbb0SDavid S. Miller 		if (err != 0)
1769c8c05a8eSCatherine Zhang 			goto out;
177013fcfbb0SDavid S. Miller 
1771e7443892SHerbert Xu 		c.data.byid = p->index;
1772f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
177326b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
177415e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
177526b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
17761da177e4SLinus Torvalds 	}
17771da177e4SLinus Torvalds 
1778c8c05a8eSCatherine Zhang out:
1779ef41aaa0SEric Paris 	xfrm_pol_put(xp);
1780e4c17216SPaul Moore 	if (delete && err == 0)
1781e4c17216SPaul Moore 		xfrm_garbage_collect(net);
17821da177e4SLinus Torvalds 	return err;
17831da177e4SLinus Torvalds }
17841da177e4SLinus Torvalds 
178522e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
17865424f32eSThomas Graf 		struct nlattr **attrs)
17871da177e4SLinus Torvalds {
1788fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
178926b15dadSJamal Hadi Salim 	struct km_event c;
17907b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
17914aa2e62cSJoy Latten 	int err;
17921da177e4SLinus Torvalds 
17932e71029eSTetsuo Handa 	err = xfrm_state_flush(net, p->proto, true);
17949e64cc95SJamal Hadi Salim 	if (err) {
17959e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
17969e64cc95SJamal Hadi Salim 			return 0;
1797069c474eSDavid S. Miller 		return err;
17989e64cc95SJamal Hadi Salim 	}
1799bf08867fSHerbert Xu 	c.data.proto = p->proto;
1800f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
180126b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
180215e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
18037067802eSAlexey Dobriyan 	c.net = net;
180426b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
180526b15dadSJamal Hadi Salim 
18061da177e4SLinus Torvalds 	return 0;
18071da177e4SLinus Torvalds }
18081da177e4SLinus Torvalds 
1809d8647b79SSteffen Klassert static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
18107deb2264SThomas Graf {
1811d8647b79SSteffen Klassert 	size_t replay_size = x->replay_esn ?
1812d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
1813d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
1814d8647b79SSteffen Klassert 
18157deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1816d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
1817de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
18186f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
18197deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
18207deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
18217deb2264SThomas Graf }
1822d51d081dSJamal Hadi Salim 
1823214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1824d51d081dSJamal Hadi Salim {
1825d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1826d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
18271d1e34ddSDavid S. Miller 	int err;
1828d51d081dSJamal Hadi Salim 
182915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
183079b8b7f4SThomas Graf 	if (nlh == NULL)
183179b8b7f4SThomas Graf 		return -EMSGSIZE;
1832d51d081dSJamal Hadi Salim 
18337b67c857SThomas Graf 	id = nlmsg_data(nlh);
18342b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1835d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
1836d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
1837d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
18382b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
18392b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
1840d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
1841d51d081dSJamal Hadi Salim 
1842d0fde795SDavid S. Miller 	if (x->replay_esn) {
18431d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1844d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
18451d1e34ddSDavid S. Miller 			      x->replay_esn);
1846d0fde795SDavid S. Miller 	} else {
18471d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
18481d1e34ddSDavid S. Miller 			      &x->replay);
1849d0fde795SDavid S. Miller 	}
18501d1e34ddSDavid S. Miller 	if (err)
18511d1e34ddSDavid S. Miller 		goto out_cancel;
1852de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1853de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
18541d1e34ddSDavid S. Miller 	if (err)
18551d1e34ddSDavid S. Miller 		goto out_cancel;
1856d8647b79SSteffen Klassert 
18571d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
18581d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
18591d1e34ddSDavid S. Miller 		if (err)
18601d1e34ddSDavid S. Miller 			goto out_cancel;
18611d1e34ddSDavid S. Miller 	}
18621d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
18631d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
18641d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
18651d1e34ddSDavid S. Miller 		if (err)
18661d1e34ddSDavid S. Miller 			goto out_cancel;
18671d1e34ddSDavid S. Miller 	}
18681d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
18691d1e34ddSDavid S. Miller 	if (err)
18701d1e34ddSDavid S. Miller 		goto out_cancel;
18716f26b61eSJamal Hadi Salim 
1872053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1873053c095aSJohannes Berg 	return 0;
1874d51d081dSJamal Hadi Salim 
18751d1e34ddSDavid S. Miller out_cancel:
18769825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
18771d1e34ddSDavid S. Miller 	return err;
1878d51d081dSJamal Hadi Salim }
1879d51d081dSJamal Hadi Salim 
188022e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
18815424f32eSThomas Graf 		struct nlattr **attrs)
1882d51d081dSJamal Hadi Salim {
1883fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1884d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1885d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
1886d51d081dSJamal Hadi Salim 	int err;
1887d51d081dSJamal Hadi Salim 	struct km_event c;
18886f26b61eSJamal Hadi Salim 	u32 mark;
18896f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
18907b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
1891d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
1892d51d081dSJamal Hadi Salim 
18936f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
18946f26b61eSJamal Hadi Salim 
18956f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1896d8647b79SSteffen Klassert 	if (x == NULL)
1897d51d081dSJamal Hadi Salim 		return -ESRCH;
1898d8647b79SSteffen Klassert 
1899d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1900d8647b79SSteffen Klassert 	if (r_skb == NULL) {
1901d8647b79SSteffen Klassert 		xfrm_state_put(x);
1902d8647b79SSteffen Klassert 		return -ENOMEM;
1903d51d081dSJamal Hadi Salim 	}
1904d51d081dSJamal Hadi Salim 
1905d51d081dSJamal Hadi Salim 	/*
1906d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
1907d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
1908d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
1909d51d081dSJamal Hadi Salim 	*/
1910d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1911d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
1912d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
191315e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
1914d51d081dSJamal Hadi Salim 
1915d51d081dSJamal Hadi Salim 	if (build_aevent(r_skb, x, &c) < 0)
1916d51d081dSJamal Hadi Salim 		BUG();
191715e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1918d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1919d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1920d51d081dSJamal Hadi Salim 	return err;
1921d51d081dSJamal Hadi Salim }
1922d51d081dSJamal Hadi Salim 
192322e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
19245424f32eSThomas Graf 		struct nlattr **attrs)
1925d51d081dSJamal Hadi Salim {
1926fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1927d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1928d51d081dSJamal Hadi Salim 	struct km_event c;
1929d51d081dSJamal Hadi Salim 	int err = -EINVAL;
19306f26b61eSJamal Hadi Salim 	u32 mark = 0;
19316f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
19327b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
19335424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1934d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
19355424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
19364e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
19374e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
1938d51d081dSJamal Hadi Salim 
19394e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
1940d51d081dSJamal Hadi Salim 		return err;
1941d51d081dSJamal Hadi Salim 
1942d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
1943d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1944d51d081dSJamal Hadi Salim 		return err;
1945d51d081dSJamal Hadi Salim 
19466f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
19476f26b61eSJamal Hadi Salim 
19486f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1949d51d081dSJamal Hadi Salim 	if (x == NULL)
1950d51d081dSJamal Hadi Salim 		return -ESRCH;
1951d51d081dSJamal Hadi Salim 
1952d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
1953d51d081dSJamal Hadi Salim 		goto out;
1954d51d081dSJamal Hadi Salim 
19554479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
1956e2b19125SSteffen Klassert 	if (err)
1957e2b19125SSteffen Klassert 		goto out;
1958e2b19125SSteffen Klassert 
1959d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1960e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
1961d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1962d51d081dSJamal Hadi Salim 
1963d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
1964d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
196515e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
1966d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
1967d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
1968d51d081dSJamal Hadi Salim 	err = 0;
1969d51d081dSJamal Hadi Salim out:
1970d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1971d51d081dSJamal Hadi Salim 	return err;
1972d51d081dSJamal Hadi Salim }
1973d51d081dSJamal Hadi Salim 
197422e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
19755424f32eSThomas Graf 		struct nlattr **attrs)
19761da177e4SLinus Torvalds {
1977fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
197826b15dadSJamal Hadi Salim 	struct km_event c;
1979b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1980f7b6983fSMasahide NAKAMURA 	int err;
198126b15dadSJamal Hadi Salim 
198235a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1983f7b6983fSMasahide NAKAMURA 	if (err)
1984f7b6983fSMasahide NAKAMURA 		return err;
1985f7b6983fSMasahide NAKAMURA 
19862e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
19872f1eb65fSJamal Hadi Salim 	if (err) {
19882f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
19892f1eb65fSJamal Hadi Salim 			return 0;
1990069c474eSDavid S. Miller 		return err;
19912f1eb65fSJamal Hadi Salim 	}
19922f1eb65fSJamal Hadi Salim 
1993f7b6983fSMasahide NAKAMURA 	c.data.type = type;
1994f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
199526b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
199615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
19977067802eSAlexey Dobriyan 	c.net = net;
199826b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
19991da177e4SLinus Torvalds 	return 0;
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds 
200222e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
20035424f32eSThomas Graf 		struct nlattr **attrs)
20046c5c8ca7SJamal Hadi Salim {
2005fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
20066c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
20077b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
20086c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2009b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
20106c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2011295fae56SJamal Hadi Salim 	struct xfrm_mark m;
2012295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
20136c5c8ca7SJamal Hadi Salim 
201435a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2015f7b6983fSMasahide NAKAMURA 	if (err)
2016f7b6983fSMasahide NAKAMURA 		return err;
2017f7b6983fSMasahide NAKAMURA 
2018c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
2019c8bf4d04STimo Teräs 	if (err)
2020c8bf4d04STimo Teräs 		return err;
2021c8bf4d04STimo Teräs 
20226c5c8ca7SJamal Hadi Salim 	if (p->index)
2023295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
20246c5c8ca7SJamal Hadi Salim 	else {
20255424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
202603e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
20276c5c8ca7SJamal Hadi Salim 
202835a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
20296c5c8ca7SJamal Hadi Salim 		if (err)
20306c5c8ca7SJamal Hadi Salim 			return err;
20316c5c8ca7SJamal Hadi Salim 
20322c8dd116SDenis V. Lunev 		ctx = NULL;
20336c5c8ca7SJamal Hadi Salim 		if (rt) {
20345424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
20356c5c8ca7SJamal Hadi Salim 
203652a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
203703e1ad7bSPaul Moore 			if (err)
20386c5c8ca7SJamal Hadi Salim 				return err;
20392c8dd116SDenis V. Lunev 		}
2040295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
20416f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
204203e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
20436c5c8ca7SJamal Hadi Salim 	}
20446c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2045ef41aaa0SEric Paris 		return -ENOENT;
204603e1ad7bSPaul Moore 
2047ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
20486c5c8ca7SJamal Hadi Salim 		goto out;
20496c5c8ca7SJamal Hadi Salim 
20506c5c8ca7SJamal Hadi Salim 	err = 0;
20516c5c8ca7SJamal Hadi Salim 	if (up->hard) {
20526c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
20532e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
20546c5c8ca7SJamal Hadi Salim 	} else {
20556c5c8ca7SJamal Hadi Salim 		// reset the timers here?
20560c199a90SJakub Wilk 		WARN(1, "Don't know what to do with soft policy expire\n");
20576c5c8ca7SJamal Hadi Salim 	}
2058c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
20596c5c8ca7SJamal Hadi Salim 
20606c5c8ca7SJamal Hadi Salim out:
20616c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
20626c5c8ca7SJamal Hadi Salim 	return err;
20636c5c8ca7SJamal Hadi Salim }
20646c5c8ca7SJamal Hadi Salim 
206522e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
20665424f32eSThomas Graf 		struct nlattr **attrs)
206753bc6b4dSJamal Hadi Salim {
2068fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
206953bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
207053bc6b4dSJamal Hadi Salim 	int err;
20717b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
207253bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
20736f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2074928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
207553bc6b4dSJamal Hadi Salim 
20766f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
207753bc6b4dSJamal Hadi Salim 
20783a765aa5SDavid S. Miller 	err = -ENOENT;
207953bc6b4dSJamal Hadi Salim 	if (x == NULL)
208053bc6b4dSJamal Hadi Salim 		return err;
208153bc6b4dSJamal Hadi Salim 
208253bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
20833a765aa5SDavid S. Miller 	err = -EINVAL;
208453bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
208553bc6b4dSJamal Hadi Salim 		goto out;
2086c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
208753bc6b4dSJamal Hadi Salim 
2088161a09e7SJoy Latten 	if (ue->hard) {
208953bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
20902e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2091161a09e7SJoy Latten 	}
20923a765aa5SDavid S. Miller 	err = 0;
209353bc6b4dSJamal Hadi Salim out:
209453bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
209553bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
209653bc6b4dSJamal Hadi Salim 	return err;
209753bc6b4dSJamal Hadi Salim }
209853bc6b4dSJamal Hadi Salim 
209922e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
21005424f32eSThomas Graf 		struct nlattr **attrs)
2101980ebd25SJamal Hadi Salim {
2102fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2103980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2104980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2105980ebd25SJamal Hadi Salim 	int i;
21065424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
21076f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2108980ebd25SJamal Hadi Salim 
21097b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2110fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2111980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2112980ebd25SJamal Hadi Salim 
2113980ebd25SJamal Hadi Salim 	if (!x)
2114d8eb9307SIlpo Järvinen 		goto nomem;
2115980ebd25SJamal Hadi Salim 
21166f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
21176f26b61eSJamal Hadi Salim 
2118980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2119d8eb9307SIlpo Järvinen 	if (err)
2120*73efc324SVegard Nossum 		goto free_state;
2121980ebd25SJamal Hadi Salim 
2122980ebd25SJamal Hadi Salim 	/*   build an XP */
2123fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2124d8eb9307SIlpo Järvinen 	if (!xp)
2125d8eb9307SIlpo Järvinen 		goto free_state;
2126980ebd25SJamal Hadi Salim 
2127980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2128980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2129980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
21306f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
21316f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
21325424f32eSThomas Graf 	ut = nla_data(rt);
2133980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2134980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2135980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2136980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2137980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2138980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2139980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2140980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2141980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2142980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2143980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2144980ebd25SJamal Hadi Salim 
2145980ebd25SJamal Hadi Salim 	}
2146980ebd25SJamal Hadi Salim 
2147980ebd25SJamal Hadi Salim 	kfree(x);
2148980ebd25SJamal Hadi Salim 	kfree(xp);
2149980ebd25SJamal Hadi Salim 
2150980ebd25SJamal Hadi Salim 	return 0;
2151d8eb9307SIlpo Järvinen 
2152d8eb9307SIlpo Järvinen free_state:
2153d8eb9307SIlpo Järvinen 	kfree(x);
2154d8eb9307SIlpo Järvinen nomem:
2155d8eb9307SIlpo Järvinen 	return err;
2156980ebd25SJamal Hadi Salim }
2157980ebd25SJamal Hadi Salim 
21585c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
21595c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
216013c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
21615424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
21625c79de6eSShinta Sugimoto {
21635424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
21645c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
21655c79de6eSShinta Sugimoto 	int i, num_migrate;
21665c79de6eSShinta Sugimoto 
216713c1d189SArnaud Ebalard 	if (k != NULL) {
216813c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
216913c1d189SArnaud Ebalard 
217013c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
217113c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
217213c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
217313c1d189SArnaud Ebalard 		k->family = uk->family;
217413c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
217513c1d189SArnaud Ebalard 	}
217613c1d189SArnaud Ebalard 
21775424f32eSThomas Graf 	um = nla_data(rt);
21785424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
21795c79de6eSShinta Sugimoto 
21805c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
21815c79de6eSShinta Sugimoto 		return -EINVAL;
21825c79de6eSShinta Sugimoto 
21835c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
21845c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
21855c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
21865c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
21875c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
21885c79de6eSShinta Sugimoto 
21895c79de6eSShinta Sugimoto 		ma->proto = um->proto;
21905c79de6eSShinta Sugimoto 		ma->mode = um->mode;
21915c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
21925c79de6eSShinta Sugimoto 
21935c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
21945c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
21955c79de6eSShinta Sugimoto 	}
21965c79de6eSShinta Sugimoto 
21975c79de6eSShinta Sugimoto 	*num = i;
21985c79de6eSShinta Sugimoto 	return 0;
21995c79de6eSShinta Sugimoto }
22005c79de6eSShinta Sugimoto 
22015c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
22025424f32eSThomas Graf 			   struct nlattr **attrs)
22035c79de6eSShinta Sugimoto {
22047b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
22055c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
220613c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
22075c79de6eSShinta Sugimoto 	u8 type;
22085c79de6eSShinta Sugimoto 	int err;
22095c79de6eSShinta Sugimoto 	int n = 0;
22108d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
22115c79de6eSShinta Sugimoto 
221235a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2213cf5cb79fSThomas Graf 		return -EINVAL;
22145c79de6eSShinta Sugimoto 
221513c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
221613c1d189SArnaud Ebalard 
22175424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
22185c79de6eSShinta Sugimoto 	if (err)
22195c79de6eSShinta Sugimoto 		return err;
22205c79de6eSShinta Sugimoto 
222113c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
22225c79de6eSShinta Sugimoto 	if (err)
22235c79de6eSShinta Sugimoto 		return err;
22245c79de6eSShinta Sugimoto 
22255c79de6eSShinta Sugimoto 	if (!n)
22265c79de6eSShinta Sugimoto 		return 0;
22275c79de6eSShinta Sugimoto 
22288d549c4fSFan Du 	xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
22295c79de6eSShinta Sugimoto 
22305c79de6eSShinta Sugimoto 	return 0;
22315c79de6eSShinta Sugimoto }
22325c79de6eSShinta Sugimoto #else
22335c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
22345424f32eSThomas Graf 			   struct nlattr **attrs)
22355c79de6eSShinta Sugimoto {
22365c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
22375c79de6eSShinta Sugimoto }
22385c79de6eSShinta Sugimoto #endif
22395c79de6eSShinta Sugimoto 
22405c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2241183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
22425c79de6eSShinta Sugimoto {
22435c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
22445c79de6eSShinta Sugimoto 
22455c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
22465c79de6eSShinta Sugimoto 	um.proto = m->proto;
22475c79de6eSShinta Sugimoto 	um.mode = m->mode;
22485c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
22495c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
22505c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
22515c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
22525c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
22535c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
22545c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
22555c79de6eSShinta Sugimoto 
2256c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
22575c79de6eSShinta Sugimoto }
22585c79de6eSShinta Sugimoto 
2259183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
226013c1d189SArnaud Ebalard {
226113c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
226213c1d189SArnaud Ebalard 
226313c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
226413c1d189SArnaud Ebalard 	uk.family = k->family;
226513c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
226613c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2267a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
226813c1d189SArnaud Ebalard 
226913c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
227013c1d189SArnaud Ebalard }
227113c1d189SArnaud Ebalard 
227213c1d189SArnaud Ebalard static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
22737deb2264SThomas Graf {
22747deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
227513c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
22767deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
22777deb2264SThomas Graf 	      + userpolicy_type_attrsize();
22787deb2264SThomas Graf }
22797deb2264SThomas Graf 
2280183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2281183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
2282183cad12SDavid S. Miller 			 const struct xfrm_selector *sel, u8 dir, u8 type)
22835c79de6eSShinta Sugimoto {
2284183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
22855c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
22865c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
22871d1e34ddSDavid S. Miller 	int i, err;
22885c79de6eSShinta Sugimoto 
228979b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
229079b8b7f4SThomas Graf 	if (nlh == NULL)
229179b8b7f4SThomas Graf 		return -EMSGSIZE;
22925c79de6eSShinta Sugimoto 
22937b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
22945c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
22955c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
22965c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
22975c79de6eSShinta Sugimoto 	pol_id->dir = dir;
22985c79de6eSShinta Sugimoto 
22991d1e34ddSDavid S. Miller 	if (k != NULL) {
23001d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
23011d1e34ddSDavid S. Miller 		if (err)
23021d1e34ddSDavid S. Miller 			goto out_cancel;
23031d1e34ddSDavid S. Miller 	}
23041d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
23051d1e34ddSDavid S. Miller 	if (err)
23061d1e34ddSDavid S. Miller 		goto out_cancel;
23075c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
23081d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
23091d1e34ddSDavid S. Miller 		if (err)
23101d1e34ddSDavid S. Miller 			goto out_cancel;
23115c79de6eSShinta Sugimoto 	}
23125c79de6eSShinta Sugimoto 
2313053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2314053c095aSJohannes Berg 	return 0;
23151d1e34ddSDavid S. Miller 
23161d1e34ddSDavid S. Miller out_cancel:
23179825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
23181d1e34ddSDavid S. Miller 	return err;
23195c79de6eSShinta Sugimoto }
23205c79de6eSShinta Sugimoto 
2321183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2322183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
2323183cad12SDavid S. Miller 			     const struct xfrm_kmaddress *k)
23245c79de6eSShinta Sugimoto {
2325a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
23265c79de6eSShinta Sugimoto 	struct sk_buff *skb;
23275c79de6eSShinta Sugimoto 
232813c1d189SArnaud Ebalard 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
23295c79de6eSShinta Sugimoto 	if (skb == NULL)
23305c79de6eSShinta Sugimoto 		return -ENOMEM;
23315c79de6eSShinta Sugimoto 
23325c79de6eSShinta Sugimoto 	/* build migrate */
233313c1d189SArnaud Ebalard 	if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
23345c79de6eSShinta Sugimoto 		BUG();
23355c79de6eSShinta Sugimoto 
233621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
23375c79de6eSShinta Sugimoto }
23385c79de6eSShinta Sugimoto #else
2339183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2340183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
2341183cad12SDavid S. Miller 			     const struct xfrm_kmaddress *k)
23425c79de6eSShinta Sugimoto {
23435c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
23445c79de6eSShinta Sugimoto }
23455c79de6eSShinta Sugimoto #endif
2346d51d081dSJamal Hadi Salim 
2347a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2348492b558bSThomas Graf 
2349492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
235066f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2351492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2352492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2353492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2354492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2355492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2356492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2357980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
235853bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2359492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
236066f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
23616c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2362492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2363a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2364d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2365d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
236697a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
23675c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2368a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2369880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2370a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
23711da177e4SLinus Torvalds };
23721da177e4SLinus Torvalds 
2373492b558bSThomas Graf #undef XMSGSIZE
2374492b558bSThomas Graf 
2375cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2376c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2377c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2378c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2379c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
23801a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2381cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2382cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2383cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2384cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2385cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2386cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2387cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2388cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2389cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2390cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2391cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2392cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2393cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2394cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
239513c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
23966f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
239735d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2398d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2399a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2400d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2401870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2402cf5cb79fSThomas Graf };
2403cf5cb79fSThomas Graf 
2404880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2405880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2406880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2407880a6fabSChristophe Gouault };
2408880a6fabSChristophe Gouault 
240905600a79SMathias Krause static const struct xfrm_link {
24105424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
24111da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
24124c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2413880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2414880a6fabSChristophe Gouault 	int nla_max;
2415492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2416492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2417492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2418492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
24194c563f76STimo Teras 						   .dump = xfrm_dump_sa,
24204c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2421492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2422492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2423492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
24244c563f76STimo Teras 						   .dump = xfrm_dump_policy,
24254c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2426492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2427980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
242853bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2429492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2430492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
24316c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2432492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2433492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2434d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2435d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
24365c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
243728d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2438880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2439880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2440880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2441ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
24421da177e4SLinus Torvalds };
24431da177e4SLinus Torvalds 
24441d00a4ebSThomas Graf static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
24451da177e4SLinus Torvalds {
2446a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
244735a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
244805600a79SMathias Krause 	const struct xfrm_link *link;
2449a7bd9a45SThomas Graf 	int type, err;
24501da177e4SLinus Torvalds 
245174005991SFan Du #ifdef CONFIG_COMPAT
24522bf8c476SAndy Lutomirski 	if (in_compat_syscall())
245374005991SFan Du 		return -ENOTSUPP;
245474005991SFan Du #endif
245574005991SFan Du 
24561da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
24571da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
24581d00a4ebSThomas Graf 		return -EINVAL;
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
24611da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
246490f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
24651d00a4ebSThomas Graf 		return -EPERM;
24661da177e4SLinus Torvalds 
2467492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2468492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2469b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
24701da177e4SLinus Torvalds 		if (link->dump == NULL)
24711d00a4ebSThomas Graf 			return -EINVAL;
24721da177e4SLinus Torvalds 
247380d326faSPablo Neira Ayuso 		{
247480d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
247580d326faSPablo Neira Ayuso 				.dump = link->dump,
247680d326faSPablo Neira Ayuso 				.done = link->done,
247780d326faSPablo Neira Ayuso 			};
247880d326faSPablo Neira Ayuso 			return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
247980d326faSPablo Neira Ayuso 		}
24801da177e4SLinus Torvalds 	}
24811da177e4SLinus Torvalds 
2482880a6fabSChristophe Gouault 	err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2483880a6fabSChristophe Gouault 			  link->nla_max ? : XFRMA_MAX,
2484880a6fabSChristophe Gouault 			  link->nla_pol ? : xfrma_policy);
2485a7bd9a45SThomas Graf 	if (err < 0)
2486a7bd9a45SThomas Graf 		return err;
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	if (link->doit == NULL)
24891d00a4ebSThomas Graf 		return -EINVAL;
24901da177e4SLinus Torvalds 
24915424f32eSThomas Graf 	return link->doit(skb, nlh, attrs);
24921da177e4SLinus Torvalds }
24931da177e4SLinus Torvalds 
2494cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
24951da177e4SLinus Torvalds {
2496283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2497283bc9f3SFan Du 
2498283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2499cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2500283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
25011da177e4SLinus Torvalds }
25021da177e4SLinus Torvalds 
25037deb2264SThomas Graf static inline size_t xfrm_expire_msgsize(void)
25047deb2264SThomas Graf {
25056f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
25066f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
25077deb2264SThomas Graf }
25087deb2264SThomas Graf 
2509214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
25101da177e4SLinus Torvalds {
25111da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
25121da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
25131d1e34ddSDavid S. Miller 	int err;
25141da177e4SLinus Torvalds 
251515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
251679b8b7f4SThomas Graf 	if (nlh == NULL)
251779b8b7f4SThomas Graf 		return -EMSGSIZE;
25181da177e4SLinus Torvalds 
25197b67c857SThomas Graf 	ue = nlmsg_data(nlh);
25201da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2521d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
25221da177e4SLinus Torvalds 
25231d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
25241d1e34ddSDavid S. Miller 	if (err)
25251d1e34ddSDavid S. Miller 		return err;
25266f26b61eSJamal Hadi Salim 
2527053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2528053c095aSJohannes Berg 	return 0;
25291da177e4SLinus Torvalds }
25301da177e4SLinus Torvalds 
2531214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
25321da177e4SLinus Torvalds {
2533fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
25341da177e4SLinus Torvalds 	struct sk_buff *skb;
25351da177e4SLinus Torvalds 
25367deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
25371da177e4SLinus Torvalds 	if (skb == NULL)
25381da177e4SLinus Torvalds 		return -ENOMEM;
25391da177e4SLinus Torvalds 
25406f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
25416f26b61eSJamal Hadi Salim 		kfree_skb(skb);
25426f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
25436f26b61eSJamal Hadi Salim 	}
25441da177e4SLinus Torvalds 
254521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
25461da177e4SLinus Torvalds }
25471da177e4SLinus Torvalds 
2548214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2549d51d081dSJamal Hadi Salim {
2550fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2551d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
2552d51d081dSJamal Hadi Salim 
2553d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2554d51d081dSJamal Hadi Salim 	if (skb == NULL)
2555d51d081dSJamal Hadi Salim 		return -ENOMEM;
2556d51d081dSJamal Hadi Salim 
2557d51d081dSJamal Hadi Salim 	if (build_aevent(skb, x, c) < 0)
2558d51d081dSJamal Hadi Salim 		BUG();
2559d51d081dSJamal Hadi Salim 
256021ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2561d51d081dSJamal Hadi Salim }
2562d51d081dSJamal Hadi Salim 
2563214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
256426b15dadSJamal Hadi Salim {
25657067802eSAlexey Dobriyan 	struct net *net = c->net;
256626b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
256726b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
256826b15dadSJamal Hadi Salim 	struct sk_buff *skb;
25697deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
257026b15dadSJamal Hadi Salim 
25717deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
257226b15dadSJamal Hadi Salim 	if (skb == NULL)
257326b15dadSJamal Hadi Salim 		return -ENOMEM;
257426b15dadSJamal Hadi Salim 
257515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
257679b8b7f4SThomas Graf 	if (nlh == NULL) {
257779b8b7f4SThomas Graf 		kfree_skb(skb);
257879b8b7f4SThomas Graf 		return -EMSGSIZE;
257979b8b7f4SThomas Graf 	}
258026b15dadSJamal Hadi Salim 
25817b67c857SThomas Graf 	p = nlmsg_data(nlh);
2582bf08867fSHerbert Xu 	p->proto = c->data.proto;
258326b15dadSJamal Hadi Salim 
25849825069dSThomas Graf 	nlmsg_end(skb, nlh);
258526b15dadSJamal Hadi Salim 
258621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
258726b15dadSJamal Hadi Salim }
258826b15dadSJamal Hadi Salim 
25897deb2264SThomas Graf static inline size_t xfrm_sa_len(struct xfrm_state *x)
259026b15dadSJamal Hadi Salim {
25917deb2264SThomas Graf 	size_t l = 0;
25921a6509d9SHerbert Xu 	if (x->aead)
25931a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
25944447bb33SMartin Willi 	if (x->aalg) {
25954447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
25964447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
25974447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
25984447bb33SMartin Willi 	}
259926b15dadSJamal Hadi Salim 	if (x->ealg)
26000f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
260126b15dadSJamal Hadi Salim 	if (x->calg)
26027deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
260326b15dadSJamal Hadi Salim 	if (x->encap)
26047deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
260535d2856bSMartin Willi 	if (x->tfcpad)
260635d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
2607d8647b79SSteffen Klassert 	if (x->replay_esn)
2608d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2609f293a5e3Sdingzhi 	else
2610f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
261168325d3bSHerbert Xu 	if (x->security)
261268325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
261368325d3bSHerbert Xu 				    x->security->ctx_len);
261468325d3bSHerbert Xu 	if (x->coaddr)
261568325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
2616a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
2617a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
261868325d3bSHerbert Xu 
2619d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2620de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
262126b15dadSJamal Hadi Salim 
262226b15dadSJamal Hadi Salim 	return l;
262326b15dadSJamal Hadi Salim }
262426b15dadSJamal Hadi Salim 
2625214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
262626b15dadSJamal Hadi Salim {
2627fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
262826b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
26290603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
263026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
263126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
263226b15dadSJamal Hadi Salim 	int len = xfrm_sa_len(x);
26331d1e34ddSDavid S. Miller 	int headlen, err;
26340603eac0SHerbert Xu 
26350603eac0SHerbert Xu 	headlen = sizeof(*p);
26360603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
26377deb2264SThomas Graf 		len += nla_total_size(headlen);
26380603eac0SHerbert Xu 		headlen = sizeof(*id);
26396f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
26400603eac0SHerbert Xu 	}
26417deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
264226b15dadSJamal Hadi Salim 
26437deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
264426b15dadSJamal Hadi Salim 	if (skb == NULL)
264526b15dadSJamal Hadi Salim 		return -ENOMEM;
264626b15dadSJamal Hadi Salim 
264715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
26481d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
264979b8b7f4SThomas Graf 	if (nlh == NULL)
26501d1e34ddSDavid S. Miller 		goto out_free_skb;
265126b15dadSJamal Hadi Salim 
26527b67c857SThomas Graf 	p = nlmsg_data(nlh);
26530603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2654c0144beaSThomas Graf 		struct nlattr *attr;
2655c0144beaSThomas Graf 
26567b67c857SThomas Graf 		id = nlmsg_data(nlh);
26570603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
26580603eac0SHerbert Xu 		id->spi = x->id.spi;
26590603eac0SHerbert Xu 		id->family = x->props.family;
26600603eac0SHerbert Xu 		id->proto = x->id.proto;
26610603eac0SHerbert Xu 
2662c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
26631d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2664c0144beaSThomas Graf 		if (attr == NULL)
26651d1e34ddSDavid S. Miller 			goto out_free_skb;
2666c0144beaSThomas Graf 
2667c0144beaSThomas Graf 		p = nla_data(attr);
26680603eac0SHerbert Xu 	}
26691d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
26701d1e34ddSDavid S. Miller 	if (err)
26711d1e34ddSDavid S. Miller 		goto out_free_skb;
267226b15dadSJamal Hadi Salim 
26739825069dSThomas Graf 	nlmsg_end(skb, nlh);
267426b15dadSJamal Hadi Salim 
267521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
267626b15dadSJamal Hadi Salim 
26771d1e34ddSDavid S. Miller out_free_skb:
267826b15dadSJamal Hadi Salim 	kfree_skb(skb);
26791d1e34ddSDavid S. Miller 	return err;
268026b15dadSJamal Hadi Salim }
268126b15dadSJamal Hadi Salim 
2682214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
268326b15dadSJamal Hadi Salim {
268426b15dadSJamal Hadi Salim 
268526b15dadSJamal Hadi Salim 	switch (c->event) {
2686f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
268726b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2688d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2689d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2690f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2691f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2692f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
269326b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2694f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
269526b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
269626b15dadSJamal Hadi Salim 	default:
269762db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
269862db5cfdSstephen hemminger 		       c->event);
269926b15dadSJamal Hadi Salim 		break;
270026b15dadSJamal Hadi Salim 	}
270126b15dadSJamal Hadi Salim 
270226b15dadSJamal Hadi Salim 	return 0;
270326b15dadSJamal Hadi Salim 
270426b15dadSJamal Hadi Salim }
270526b15dadSJamal Hadi Salim 
27067deb2264SThomas Graf static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
27077deb2264SThomas Graf 					  struct xfrm_policy *xp)
27087deb2264SThomas Graf {
27097deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
27107deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
27116f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
27127deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
27137deb2264SThomas Graf 	       + userpolicy_type_attrsize();
27147deb2264SThomas Graf }
27157deb2264SThomas Graf 
27161da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
271765e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
27181da177e4SLinus Torvalds {
27191d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
27201da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
27211da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
27221d1e34ddSDavid S. Miller 	int err;
27231da177e4SLinus Torvalds 
272479b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
272579b8b7f4SThomas Graf 	if (nlh == NULL)
272679b8b7f4SThomas Graf 		return -EMSGSIZE;
27271da177e4SLinus Torvalds 
27287b67c857SThomas Graf 	ua = nlmsg_data(nlh);
27291da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
27301da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
27311da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
273265e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
27331da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
27341da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
27351da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
27361da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
27371da177e4SLinus Torvalds 
27381d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
27391d1e34ddSDavid S. Miller 	if (!err)
27401d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
27411d1e34ddSDavid S. Miller 	if (!err)
27421d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
27431d1e34ddSDavid S. Miller 	if (!err)
27441d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
27451d1e34ddSDavid S. Miller 	if (err) {
27461d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
27471d1e34ddSDavid S. Miller 		return err;
27481d1e34ddSDavid S. Miller 	}
27491da177e4SLinus Torvalds 
2750053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2751053c095aSJohannes Berg 	return 0;
27521da177e4SLinus Torvalds }
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
275565e0736bSFan Du 			     struct xfrm_policy *xp)
27561da177e4SLinus Torvalds {
2757a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
27581da177e4SLinus Torvalds 	struct sk_buff *skb;
27591da177e4SLinus Torvalds 
27607deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
27611da177e4SLinus Torvalds 	if (skb == NULL)
27621da177e4SLinus Torvalds 		return -ENOMEM;
27631da177e4SLinus Torvalds 
276465e0736bSFan Du 	if (build_acquire(skb, x, xt, xp) < 0)
27651da177e4SLinus Torvalds 		BUG();
27661da177e4SLinus Torvalds 
276721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
27681da177e4SLinus Torvalds }
27691da177e4SLinus Torvalds 
27701da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
27711da177e4SLinus Torvalds  * or more templates.
27721da177e4SLinus Torvalds  */
2773cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
27741da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
27751da177e4SLinus Torvalds {
2776fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
27771da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
27781da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
27791da177e4SLinus Torvalds 	struct xfrm_policy *xp;
27801da177e4SLinus Torvalds 	int nr;
27811da177e4SLinus Torvalds 
2782cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
27831da177e4SLinus Torvalds 	case AF_INET:
27841da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
27851da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
27861da177e4SLinus Torvalds 			return NULL;
27871da177e4SLinus Torvalds 		}
27881da177e4SLinus Torvalds 		break;
2789dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
27901da177e4SLinus Torvalds 	case AF_INET6:
27911da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
27921da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
27931da177e4SLinus Torvalds 			return NULL;
27941da177e4SLinus Torvalds 		}
27951da177e4SLinus Torvalds 		break;
27961da177e4SLinus Torvalds #endif
27971da177e4SLinus Torvalds 	default:
27981da177e4SLinus Torvalds 		*dir = -EINVAL;
27991da177e4SLinus Torvalds 		return NULL;
28001da177e4SLinus Torvalds 	}
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds 	*dir = -EINVAL;
28031da177e4SLinus Torvalds 
28041da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
28051da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
28061da177e4SLinus Torvalds 		return NULL;
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
2809b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
28101da177e4SLinus Torvalds 		return NULL;
28111da177e4SLinus Torvalds 
2812a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
2813a4f1bac6SHerbert Xu 		return NULL;
2814a4f1bac6SHerbert Xu 
28152f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
28161da177e4SLinus Torvalds 	if (xp == NULL) {
28171da177e4SLinus Torvalds 		*dir = -ENOBUFS;
28181da177e4SLinus Torvalds 		return NULL;
28191da177e4SLinus Torvalds 	}
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
2822f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
28231da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
28241da177e4SLinus Torvalds 
28251da177e4SLinus Torvalds 	*dir = p->dir;
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds 	return xp;
28281da177e4SLinus Torvalds }
28291da177e4SLinus Torvalds 
28307deb2264SThomas Graf static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
28317deb2264SThomas Graf {
28327deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
28337deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
28347deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2835295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
28367deb2264SThomas Graf 	       + userpolicy_type_attrsize();
28377deb2264SThomas Graf }
28387deb2264SThomas Graf 
28391da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2840214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
28411da177e4SLinus Torvalds {
28421da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
2843d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
28441d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
28451d1e34ddSDavid S. Miller 	int err;
28461da177e4SLinus Torvalds 
284715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
284879b8b7f4SThomas Graf 	if (nlh == NULL)
284979b8b7f4SThomas Graf 		return -EMSGSIZE;
28501da177e4SLinus Torvalds 
28517b67c857SThomas Graf 	upe = nlmsg_data(nlh);
28521da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
28531d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
28541d1e34ddSDavid S. Miller 	if (!err)
28551d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
28561d1e34ddSDavid S. Miller 	if (!err)
28571d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
28581d1e34ddSDavid S. Miller 	if (!err)
28591d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
28601d1e34ddSDavid S. Miller 	if (err) {
28611d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
28621d1e34ddSDavid S. Miller 		return err;
28631d1e34ddSDavid S. Miller 	}
28641da177e4SLinus Torvalds 	upe->hard = !!hard;
28651da177e4SLinus Torvalds 
2866053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2867053c095aSJohannes Berg 	return 0;
28681da177e4SLinus Torvalds }
28691da177e4SLinus Torvalds 
2870214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
28711da177e4SLinus Torvalds {
2872fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
28731da177e4SLinus Torvalds 	struct sk_buff *skb;
28741da177e4SLinus Torvalds 
28757deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
28761da177e4SLinus Torvalds 	if (skb == NULL)
28771da177e4SLinus Torvalds 		return -ENOMEM;
28781da177e4SLinus Torvalds 
2879d51d081dSJamal Hadi Salim 	if (build_polexpire(skb, xp, dir, c) < 0)
28801da177e4SLinus Torvalds 		BUG();
28811da177e4SLinus Torvalds 
288221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
28831da177e4SLinus Torvalds }
28841da177e4SLinus Torvalds 
2885214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
288626b15dadSJamal Hadi Salim {
28871d1e34ddSDavid S. Miller 	int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2888fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
288926b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
28900603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
289126b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
289226b15dadSJamal Hadi Salim 	struct sk_buff *skb;
28931d1e34ddSDavid S. Miller 	int headlen, err;
28940603eac0SHerbert Xu 
28950603eac0SHerbert Xu 	headlen = sizeof(*p);
28960603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
28977deb2264SThomas Graf 		len += nla_total_size(headlen);
28980603eac0SHerbert Xu 		headlen = sizeof(*id);
28990603eac0SHerbert Xu 	}
2900cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
2901295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
29027deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
290326b15dadSJamal Hadi Salim 
29047deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
290526b15dadSJamal Hadi Salim 	if (skb == NULL)
290626b15dadSJamal Hadi Salim 		return -ENOMEM;
290726b15dadSJamal Hadi Salim 
290815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
29091d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
291079b8b7f4SThomas Graf 	if (nlh == NULL)
29111d1e34ddSDavid S. Miller 		goto out_free_skb;
291226b15dadSJamal Hadi Salim 
29137b67c857SThomas Graf 	p = nlmsg_data(nlh);
29140603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
2915c0144beaSThomas Graf 		struct nlattr *attr;
2916c0144beaSThomas Graf 
29177b67c857SThomas Graf 		id = nlmsg_data(nlh);
29180603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
29190603eac0SHerbert Xu 		id->dir = dir;
29200603eac0SHerbert Xu 		if (c->data.byid)
29210603eac0SHerbert Xu 			id->index = xp->index;
29220603eac0SHerbert Xu 		else
29230603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
29240603eac0SHerbert Xu 
2925c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
29261d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2927c0144beaSThomas Graf 		if (attr == NULL)
29281d1e34ddSDavid S. Miller 			goto out_free_skb;
2929c0144beaSThomas Graf 
2930c0144beaSThomas Graf 		p = nla_data(attr);
29310603eac0SHerbert Xu 	}
293226b15dadSJamal Hadi Salim 
293326b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
29341d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
29351d1e34ddSDavid S. Miller 	if (!err)
29361d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
29371d1e34ddSDavid S. Miller 	if (!err)
29381d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
29391d1e34ddSDavid S. Miller 	if (err)
29401d1e34ddSDavid S. Miller 		goto out_free_skb;
2941295fae56SJamal Hadi Salim 
29429825069dSThomas Graf 	nlmsg_end(skb, nlh);
294326b15dadSJamal Hadi Salim 
294421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
294526b15dadSJamal Hadi Salim 
29461d1e34ddSDavid S. Miller out_free_skb:
294726b15dadSJamal Hadi Salim 	kfree_skb(skb);
29481d1e34ddSDavid S. Miller 	return err;
294926b15dadSJamal Hadi Salim }
295026b15dadSJamal Hadi Salim 
2951214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
295226b15dadSJamal Hadi Salim {
29537067802eSAlexey Dobriyan 	struct net *net = c->net;
295426b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
295526b15dadSJamal Hadi Salim 	struct sk_buff *skb;
29561d1e34ddSDavid S. Miller 	int err;
295726b15dadSJamal Hadi Salim 
29587deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
295926b15dadSJamal Hadi Salim 	if (skb == NULL)
296026b15dadSJamal Hadi Salim 		return -ENOMEM;
296126b15dadSJamal Hadi Salim 
296215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
29631d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
296479b8b7f4SThomas Graf 	if (nlh == NULL)
29651d1e34ddSDavid S. Miller 		goto out_free_skb;
29661d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
29671d1e34ddSDavid S. Miller 	if (err)
29681d1e34ddSDavid S. Miller 		goto out_free_skb;
296926b15dadSJamal Hadi Salim 
29709825069dSThomas Graf 	nlmsg_end(skb, nlh);
297126b15dadSJamal Hadi Salim 
297221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
297326b15dadSJamal Hadi Salim 
29741d1e34ddSDavid S. Miller out_free_skb:
297526b15dadSJamal Hadi Salim 	kfree_skb(skb);
29761d1e34ddSDavid S. Miller 	return err;
297726b15dadSJamal Hadi Salim }
297826b15dadSJamal Hadi Salim 
2979214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
298026b15dadSJamal Hadi Salim {
298126b15dadSJamal Hadi Salim 
298226b15dadSJamal Hadi Salim 	switch (c->event) {
2983f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
2984f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
2985f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
298626b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
2987f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
298826b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
2989f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
299026b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
299126b15dadSJamal Hadi Salim 	default:
299262db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
299362db5cfdSstephen hemminger 		       c->event);
299426b15dadSJamal Hadi Salim 	}
299526b15dadSJamal Hadi Salim 
299626b15dadSJamal Hadi Salim 	return 0;
299726b15dadSJamal Hadi Salim 
299826b15dadSJamal Hadi Salim }
299926b15dadSJamal Hadi Salim 
30007deb2264SThomas Graf static inline size_t xfrm_report_msgsize(void)
30017deb2264SThomas Graf {
30027deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
30037deb2264SThomas Graf }
30047deb2264SThomas Graf 
300597a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
300697a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
300797a64b45SMasahide NAKAMURA {
300897a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
300997a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
301097a64b45SMasahide NAKAMURA 
301179b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
301279b8b7f4SThomas Graf 	if (nlh == NULL)
301379b8b7f4SThomas Graf 		return -EMSGSIZE;
301497a64b45SMasahide NAKAMURA 
30157b67c857SThomas Graf 	ur = nlmsg_data(nlh);
301697a64b45SMasahide NAKAMURA 	ur->proto = proto;
301797a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
301897a64b45SMasahide NAKAMURA 
30191d1e34ddSDavid S. Miller 	if (addr) {
30201d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
30211d1e34ddSDavid S. Miller 		if (err) {
30229825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
30231d1e34ddSDavid S. Miller 			return err;
30241d1e34ddSDavid S. Miller 		}
30251d1e34ddSDavid S. Miller 	}
3026053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3027053c095aSJohannes Berg 	return 0;
302897a64b45SMasahide NAKAMURA }
302997a64b45SMasahide NAKAMURA 
3030db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3031db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
303297a64b45SMasahide NAKAMURA {
303397a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
303497a64b45SMasahide NAKAMURA 
30357deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
303697a64b45SMasahide NAKAMURA 	if (skb == NULL)
303797a64b45SMasahide NAKAMURA 		return -ENOMEM;
303897a64b45SMasahide NAKAMURA 
303997a64b45SMasahide NAKAMURA 	if (build_report(skb, proto, sel, addr) < 0)
304097a64b45SMasahide NAKAMURA 		BUG();
304197a64b45SMasahide NAKAMURA 
304221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
304397a64b45SMasahide NAKAMURA }
304497a64b45SMasahide NAKAMURA 
30453a2dfbe8SMartin Willi static inline size_t xfrm_mapping_msgsize(void)
30463a2dfbe8SMartin Willi {
30473a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
30483a2dfbe8SMartin Willi }
30493a2dfbe8SMartin Willi 
30503a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
30513a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
30523a2dfbe8SMartin Willi {
30533a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
30543a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
30553a2dfbe8SMartin Willi 
30563a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
30573a2dfbe8SMartin Willi 	if (nlh == NULL)
30583a2dfbe8SMartin Willi 		return -EMSGSIZE;
30593a2dfbe8SMartin Willi 
30603a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
30613a2dfbe8SMartin Willi 
30623a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
30633a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
30643a2dfbe8SMartin Willi 	um->id.family = x->props.family;
30653a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
30663a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
30673a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
30683a2dfbe8SMartin Willi 	um->new_sport = new_sport;
30693a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
30703a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
30713a2dfbe8SMartin Willi 
3072053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3073053c095aSJohannes Berg 	return 0;
30743a2dfbe8SMartin Willi }
30753a2dfbe8SMartin Willi 
30763a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
30773a2dfbe8SMartin Willi 			     __be16 sport)
30783a2dfbe8SMartin Willi {
3079a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
30803a2dfbe8SMartin Willi 	struct sk_buff *skb;
30813a2dfbe8SMartin Willi 
30823a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
30833a2dfbe8SMartin Willi 		return -EINVAL;
30843a2dfbe8SMartin Willi 
30853a2dfbe8SMartin Willi 	if (!x->encap)
30863a2dfbe8SMartin Willi 		return -EINVAL;
30873a2dfbe8SMartin Willi 
30883a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
30893a2dfbe8SMartin Willi 	if (skb == NULL)
30903a2dfbe8SMartin Willi 		return -ENOMEM;
30913a2dfbe8SMartin Willi 
30923a2dfbe8SMartin Willi 	if (build_mapping(skb, x, ipaddr, sport) < 0)
30933a2dfbe8SMartin Willi 		BUG();
30943a2dfbe8SMartin Willi 
309521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
30963a2dfbe8SMartin Willi }
30973a2dfbe8SMartin Willi 
30980f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
30990f24558eSHoria Geanta {
31000f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
31010f24558eSHoria Geanta }
31020f24558eSHoria Geanta 
31031da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
31041da177e4SLinus Torvalds 	.id		= "netlink",
31051da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
31061da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
31071da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
31081da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
310997a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
31105c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
31113a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
31120f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
31131da177e4SLinus Torvalds };
31141da177e4SLinus Torvalds 
3115a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
31161da177e4SLinus Torvalds {
3117be33690dSPatrick McHardy 	struct sock *nlsk;
3118a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3119a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3120a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3121a31f2d17SPablo Neira Ayuso 	};
3122be33690dSPatrick McHardy 
31239f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3124be33690dSPatrick McHardy 	if (nlsk == NULL)
31251da177e4SLinus Torvalds 		return -ENOMEM;
3126d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3127cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
31281da177e4SLinus Torvalds 	return 0;
31291da177e4SLinus Torvalds }
31301da177e4SLinus Torvalds 
3131d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3132a6483b79SAlexey Dobriyan {
3133d79d792eSEric W. Biederman 	struct net *net;
3134d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3135a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3136d79d792eSEric W. Biederman 	synchronize_net();
3137d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3138d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3139a6483b79SAlexey Dobriyan }
3140a6483b79SAlexey Dobriyan 
3141a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3142a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
3143d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3144a6483b79SAlexey Dobriyan };
3145a6483b79SAlexey Dobriyan 
3146a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3147a6483b79SAlexey Dobriyan {
3148a6483b79SAlexey Dobriyan 	int rv;
3149a6483b79SAlexey Dobriyan 
3150a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3151a6483b79SAlexey Dobriyan 
3152a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3153a6483b79SAlexey Dobriyan 	if (rv < 0)
3154a6483b79SAlexey Dobriyan 		return rv;
3155a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3156a6483b79SAlexey Dobriyan 	if (rv < 0)
3157a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3158a6483b79SAlexey Dobriyan 	return rv;
3159a6483b79SAlexey Dobriyan }
3160a6483b79SAlexey Dobriyan 
31611da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
31621da177e4SLinus Torvalds {
31631da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3164a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
31651da177e4SLinus Torvalds }
31661da177e4SLinus Torvalds 
31671da177e4SLinus Torvalds module_init(xfrm_user_init);
31681da177e4SLinus Torvalds module_exit(xfrm_user_exit);
31691da177e4SLinus Torvalds MODULE_LICENSE("GPL");
31704fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3171f8cd5488SJamal Hadi Salim 
3172