xref: /linux/net/xfrm/xfrm_user.c (revision c5d4d7d83165ae863954b113c7f403d8b58febed)
11da177e4SLinus Torvalds /* xfrm_user.c: User interface to configure xfrm engine.
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Changes:
61da177e4SLinus Torvalds  *	Mitsuru KANDA @USAGI
71da177e4SLinus Torvalds  * 	Kazunori MIYAZAWA @USAGI
81da177e4SLinus Torvalds  * 	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
91da177e4SLinus Torvalds  * 		IPv6 support
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
139409f38aSHerbert Xu #include <linux/crypto.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/types.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/socket.h>
191da177e4SLinus Torvalds #include <linux/string.h>
201da177e4SLinus Torvalds #include <linux/net.h>
211da177e4SLinus Torvalds #include <linux/skbuff.h>
221da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
231da177e4SLinus Torvalds #include <linux/ipsec.h>
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/security.h>
261da177e4SLinus Torvalds #include <net/sock.h>
271da177e4SLinus Torvalds #include <net/xfrm.h>
2888fc2c84SThomas Graf #include <net/netlink.h>
29fa6dd8a2SNicolas Dichtel #include <net/ah.h>
307c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
31dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
32e23c7194SMasahide NAKAMURA #include <linux/in6.h>
33e23c7194SMasahide NAKAMURA #endif
34e33d4f13SSowmini Varadhan #include <asm/unaligned.h>
351da177e4SLinus Torvalds 
365424f32eSThomas Graf static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
371da177e4SLinus Torvalds {
385424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
391da177e4SLinus Torvalds 	struct xfrm_algo *algp;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	if (!rt)
421da177e4SLinus Torvalds 		return 0;
431da177e4SLinus Torvalds 
445424f32eSThomas Graf 	algp = nla_data(rt);
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 
58633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
591da177e4SLinus Torvalds 	return 0;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
624447bb33SMartin Willi static int verify_auth_trunc(struct nlattr **attrs)
634447bb33SMartin Willi {
644447bb33SMartin Willi 	struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
654447bb33SMartin Willi 	struct xfrm_algo_auth *algp;
664447bb33SMartin Willi 
674447bb33SMartin Willi 	if (!rt)
684447bb33SMartin Willi 		return 0;
694447bb33SMartin Willi 
704447bb33SMartin Willi 	algp = nla_data(rt);
714447bb33SMartin Willi 	if (nla_len(rt) < xfrm_alg_auth_len(algp))
724447bb33SMartin Willi 		return -EINVAL;
734447bb33SMartin Willi 
74633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
754447bb33SMartin Willi 	return 0;
764447bb33SMartin Willi }
774447bb33SMartin Willi 
781a6509d9SHerbert Xu static int verify_aead(struct nlattr **attrs)
791a6509d9SHerbert Xu {
801a6509d9SHerbert Xu 	struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
811a6509d9SHerbert Xu 	struct xfrm_algo_aead *algp;
821a6509d9SHerbert Xu 
831a6509d9SHerbert Xu 	if (!rt)
841a6509d9SHerbert Xu 		return 0;
851a6509d9SHerbert Xu 
861a6509d9SHerbert Xu 	algp = nla_data(rt);
871a6509d9SHerbert Xu 	if (nla_len(rt) < aead_len(algp))
881a6509d9SHerbert Xu 		return -EINVAL;
891a6509d9SHerbert Xu 
90633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
911a6509d9SHerbert Xu 	return 0;
921a6509d9SHerbert Xu }
931a6509d9SHerbert Xu 
945424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
95eb2971b6SMasahide NAKAMURA 			   xfrm_address_t **addrp)
96eb2971b6SMasahide NAKAMURA {
975424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
98eb2971b6SMasahide NAKAMURA 
99cf5cb79fSThomas Graf 	if (rt && addrp)
1005424f32eSThomas Graf 		*addrp = nla_data(rt);
101eb2971b6SMasahide NAKAMURA }
102df71837dSTrent Jaeger 
1035424f32eSThomas Graf static inline int verify_sec_ctx_len(struct nlattr **attrs)
104df71837dSTrent Jaeger {
1055424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
106df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
107df71837dSTrent Jaeger 
108df71837dSTrent Jaeger 	if (!rt)
109df71837dSTrent Jaeger 		return 0;
110df71837dSTrent Jaeger 
1115424f32eSThomas Graf 	uctx = nla_data(rt);
112cf5cb79fSThomas Graf 	if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
113df71837dSTrent Jaeger 		return -EINVAL;
114df71837dSTrent Jaeger 
115df71837dSTrent Jaeger 	return 0;
116df71837dSTrent Jaeger }
117df71837dSTrent Jaeger 
118d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p,
119d8647b79SSteffen Klassert 				struct nlattr **attrs)
120d8647b79SSteffen Klassert {
121d8647b79SSteffen Klassert 	struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
122ecd79187SMathias Krause 	struct xfrm_replay_state_esn *rs;
123d8647b79SSteffen Klassert 
124ecd79187SMathias Krause 	if (p->flags & XFRM_STATE_ESN) {
125ecd79187SMathias Krause 		if (!rt)
1267833aa05SSteffen Klassert 			return -EINVAL;
1277833aa05SSteffen Klassert 
128ecd79187SMathias Krause 		rs = nla_data(rt);
129ecd79187SMathias Krause 
130ecd79187SMathias Krause 		if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
131ecd79187SMathias Krause 			return -EINVAL;
132ecd79187SMathias Krause 
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 
415f843ee6dSAndy Whitcroft 	/* Check the overall length and the internal bitmap length to avoid
416f843ee6dSAndy Whitcroft 	 * potential overflow. */
417f843ee6dSAndy Whitcroft 	if (nla_len(rp) < ulen ||
418f843ee6dSAndy Whitcroft 	    xfrm_replay_state_esn_len(replay_esn) != ulen ||
419f843ee6dSAndy Whitcroft 	    replay_esn->bmp_len != up->bmp_len)
420e2b19125SSteffen Klassert 		return -EINVAL;
421e2b19125SSteffen Klassert 
422677e806dSAndy Whitcroft 	if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
423677e806dSAndy Whitcroft 		return -EINVAL;
424677e806dSAndy Whitcroft 
425e2b19125SSteffen Klassert 	return 0;
426e2b19125SSteffen Klassert }
427e2b19125SSteffen Klassert 
428d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
429d8647b79SSteffen Klassert 				       struct xfrm_replay_state_esn **preplay_esn,
430d8647b79SSteffen Klassert 				       struct nlattr *rta)
431d8647b79SSteffen Klassert {
432d8647b79SSteffen Klassert 	struct xfrm_replay_state_esn *p, *pp, *up;
433ecd79187SMathias Krause 	int klen, ulen;
434d8647b79SSteffen Klassert 
435d8647b79SSteffen Klassert 	if (!rta)
436d8647b79SSteffen Klassert 		return 0;
437d8647b79SSteffen Klassert 
438d8647b79SSteffen Klassert 	up = nla_data(rta);
439ecd79187SMathias Krause 	klen = xfrm_replay_state_esn_len(up);
440ecd79187SMathias Krause 	ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
441d8647b79SSteffen Klassert 
442ecd79187SMathias Krause 	p = kzalloc(klen, GFP_KERNEL);
443d8647b79SSteffen Klassert 	if (!p)
444d8647b79SSteffen Klassert 		return -ENOMEM;
445d8647b79SSteffen Klassert 
446ecd79187SMathias Krause 	pp = kzalloc(klen, GFP_KERNEL);
447d8647b79SSteffen Klassert 	if (!pp) {
448d8647b79SSteffen Klassert 		kfree(p);
449d8647b79SSteffen Klassert 		return -ENOMEM;
450d8647b79SSteffen Klassert 	}
451d8647b79SSteffen Klassert 
452ecd79187SMathias Krause 	memcpy(p, up, ulen);
453ecd79187SMathias Krause 	memcpy(pp, up, ulen);
454ecd79187SMathias Krause 
455d8647b79SSteffen Klassert 	*replay_esn = p;
456d8647b79SSteffen Klassert 	*preplay_esn = pp;
457d8647b79SSteffen Klassert 
458d8647b79SSteffen Klassert 	return 0;
459d8647b79SSteffen Klassert }
460d8647b79SSteffen Klassert 
461661697f7SJoy Latten static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
462df71837dSTrent Jaeger {
463df71837dSTrent Jaeger 	int len = 0;
464df71837dSTrent Jaeger 
465df71837dSTrent Jaeger 	if (xfrm_ctx) {
466df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
467df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
468df71837dSTrent Jaeger 	}
469df71837dSTrent Jaeger 	return len;
470df71837dSTrent Jaeger }
471df71837dSTrent Jaeger 
4721da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
4731da177e4SLinus Torvalds {
4741da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
4751da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
4761da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
4771da177e4SLinus Torvalds 	x->props.mode = p->mode;
47833fce60dSFan Du 	x->props.replay_window = min_t(unsigned int, p->replay_window,
47933fce60dSFan Du 					sizeof(x->replay.bitmap) * 8);
4801da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
4811da177e4SLinus Torvalds 	x->props.family = p->family;
48254489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
4831da177e4SLinus Torvalds 	x->props.flags = p->flags;
484196b0036SHerbert Xu 
485ccf9b3b8SSteffen Klassert 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
486196b0036SHerbert Xu 		x->sel.family = p->family;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
489d51d081dSJamal Hadi Salim /*
490d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
491d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
492d51d081dSJamal Hadi Salim  *
493d51d081dSJamal Hadi Salim */
494e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
495e3ac104dSMathias Krause 				  int update_esn)
496d51d081dSJamal Hadi Salim {
4975424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
498e3ac104dSMathias Krause 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
4995424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
5005424f32eSThomas Graf 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
5015424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
502d51d081dSJamal Hadi Salim 
503d8647b79SSteffen Klassert 	if (re) {
504d8647b79SSteffen Klassert 		struct xfrm_replay_state_esn *replay_esn;
505d8647b79SSteffen Klassert 		replay_esn = nla_data(re);
506d8647b79SSteffen Klassert 		memcpy(x->replay_esn, replay_esn,
507d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
508d8647b79SSteffen Klassert 		memcpy(x->preplay_esn, replay_esn,
509d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
510d8647b79SSteffen Klassert 	}
511d8647b79SSteffen Klassert 
512d51d081dSJamal Hadi Salim 	if (rp) {
513d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
5145424f32eSThomas Graf 		replay = nla_data(rp);
515d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
516d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
517d51d081dSJamal Hadi Salim 	}
518d51d081dSJamal Hadi Salim 
519d51d081dSJamal Hadi Salim 	if (lt) {
520d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
5215424f32eSThomas Graf 		ltime = nla_data(lt);
522d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
523d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
524d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
525d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
526d51d081dSJamal Hadi Salim 	}
527d51d081dSJamal Hadi Salim 
528cf5cb79fSThomas Graf 	if (et)
5295424f32eSThomas Graf 		x->replay_maxage = nla_get_u32(et);
530d51d081dSJamal Hadi Salim 
531cf5cb79fSThomas Graf 	if (rt)
5325424f32eSThomas Graf 		x->replay_maxdiff = nla_get_u32(rt);
533d51d081dSJamal Hadi Salim }
534d51d081dSJamal Hadi Salim 
535fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
536fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
5375424f32eSThomas Graf 					       struct nlattr **attrs,
5381da177e4SLinus Torvalds 					       int *errp)
5391da177e4SLinus Torvalds {
540fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
5411da177e4SLinus Torvalds 	int err = -ENOMEM;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	if (!x)
5441da177e4SLinus Torvalds 		goto error_no_put;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	copy_from_user_state(x, p);
5471da177e4SLinus Torvalds 
548a947b0a9SNicolas Dichtel 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
549a947b0a9SNicolas Dichtel 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
550a947b0a9SNicolas Dichtel 
55169b0137fSHerbert Xu 	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
5521a6509d9SHerbert Xu 		goto error;
5534447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
5544447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
5554447bb33SMartin Willi 		goto error;
5564447bb33SMartin Willi 	if (!x->props.aalgo) {
5574447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
55835a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
5591da177e4SLinus Torvalds 			goto error;
5604447bb33SMartin Willi 	}
56169b0137fSHerbert Xu 	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
5621da177e4SLinus Torvalds 		goto error;
5631da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
5641da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
56535a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
5661da177e4SLinus Torvalds 		goto error;
567fd21150aSThomas Graf 
568fd21150aSThomas Graf 	if (attrs[XFRMA_ENCAP]) {
569fd21150aSThomas Graf 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
570fd21150aSThomas Graf 				   sizeof(*x->encap), GFP_KERNEL);
571fd21150aSThomas Graf 		if (x->encap == NULL)
5721da177e4SLinus Torvalds 			goto error;
573fd21150aSThomas Graf 	}
574fd21150aSThomas Graf 
57535d2856bSMartin Willi 	if (attrs[XFRMA_TFCPAD])
57635d2856bSMartin Willi 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
57735d2856bSMartin Willi 
578fd21150aSThomas Graf 	if (attrs[XFRMA_COADDR]) {
579fd21150aSThomas Graf 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
580fd21150aSThomas Graf 				    sizeof(*x->coaddr), GFP_KERNEL);
581fd21150aSThomas Graf 		if (x->coaddr == NULL)
582060f02a3SNoriaki TAKAMIYA 			goto error;
583fd21150aSThomas Graf 	}
584fd21150aSThomas Graf 
5856f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &x->mark);
5866f26b61eSJamal Hadi Salim 
587077fbac4SLorenzo Colitti 	if (attrs[XFRMA_OUTPUT_MARK])
588077fbac4SLorenzo Colitti 		x->props.output_mark = nla_get_u32(attrs[XFRMA_OUTPUT_MARK]);
589077fbac4SLorenzo Colitti 
590ffdb5211SIlan Tayari 	err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
5911da177e4SLinus Torvalds 	if (err)
5921da177e4SLinus Torvalds 		goto error;
5931da177e4SLinus Torvalds 
5942f30ea50SMathias Krause 	if (attrs[XFRMA_SEC_CTX]) {
5952f30ea50SMathias Krause 		err = security_xfrm_state_alloc(x,
5962f30ea50SMathias Krause 						nla_data(attrs[XFRMA_SEC_CTX]));
5972f30ea50SMathias Krause 		if (err)
598df71837dSTrent Jaeger 			goto error;
5992f30ea50SMathias Krause 	}
600df71837dSTrent Jaeger 
601152afb9bSIlan Tayari 	if (attrs[XFRMA_OFFLOAD_DEV]) {
602152afb9bSIlan Tayari 		err = xfrm_dev_state_add(net, x,
603152afb9bSIlan Tayari 					 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
604152afb9bSIlan Tayari 		if (err)
605d77e38e6SSteffen Klassert 			goto error;
606152afb9bSIlan Tayari 	}
607d77e38e6SSteffen Klassert 
608d8647b79SSteffen Klassert 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
609d8647b79SSteffen Klassert 					       attrs[XFRMA_REPLAY_ESN_VAL])))
610d8647b79SSteffen Klassert 		goto error;
611d8647b79SSteffen Klassert 
6121da177e4SLinus Torvalds 	x->km.seq = p->seq;
613b27aeadbSAlexey Dobriyan 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
614d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
615b27aeadbSAlexey Dobriyan 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
616d51d081dSJamal Hadi Salim 
6179fdc4883SSteffen Klassert 	if ((err = xfrm_init_replay(x)))
6189fdc4883SSteffen Klassert 		goto error;
619d51d081dSJamal Hadi Salim 
6209fdc4883SSteffen Klassert 	/* override default values from above */
621e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 0);
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	return x;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds error:
6261da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
6271da177e4SLinus Torvalds 	xfrm_state_put(x);
6281da177e4SLinus Torvalds error_no_put:
6291da177e4SLinus Torvalds 	*errp = err;
6301da177e4SLinus Torvalds 	return NULL;
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
63322e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
6345424f32eSThomas Graf 		struct nlattr **attrs)
6351da177e4SLinus Torvalds {
636fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6377b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
6381da177e4SLinus Torvalds 	struct xfrm_state *x;
6391da177e4SLinus Torvalds 	int err;
64026b15dadSJamal Hadi Salim 	struct km_event c;
6411da177e4SLinus Torvalds 
64235a7aa08SThomas Graf 	err = verify_newsa_info(p, attrs);
6431da177e4SLinus Torvalds 	if (err)
6441da177e4SLinus Torvalds 		return err;
6451da177e4SLinus Torvalds 
646fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
6471da177e4SLinus Torvalds 	if (!x)
6481da177e4SLinus Torvalds 		return err;
6491da177e4SLinus Torvalds 
65026b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
6511da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
6521da177e4SLinus Torvalds 		err = xfrm_state_add(x);
6531da177e4SLinus Torvalds 	else
6541da177e4SLinus Torvalds 		err = xfrm_state_update(x);
6551da177e4SLinus Torvalds 
6562e71029eSTetsuo Handa 	xfrm_audit_state_add(x, err ? 0 : 1, true);
657161a09e7SJoy Latten 
6581da177e4SLinus Torvalds 	if (err < 0) {
6591da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
660*c5d4d7d8SSteffen Klassert 		xfrm_dev_state_delete(x);
66121380b81SHerbert Xu 		__xfrm_state_put(x);
6627d6dfe1fSPatrick McHardy 		goto out;
6631da177e4SLinus Torvalds 	}
6641da177e4SLinus Torvalds 
66526b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
66615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
667f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
66826b15dadSJamal Hadi Salim 
66926b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
6707d6dfe1fSPatrick McHardy out:
67126b15dadSJamal Hadi Salim 	xfrm_state_put(x);
6721da177e4SLinus Torvalds 	return err;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
675fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
676fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
6775424f32eSThomas Graf 						 struct nlattr **attrs,
678eb2971b6SMasahide NAKAMURA 						 int *errp)
679eb2971b6SMasahide NAKAMURA {
680eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
6816f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
682eb2971b6SMasahide NAKAMURA 	int err;
6836f26b61eSJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
684eb2971b6SMasahide NAKAMURA 
685eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
686eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
6876f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
688eb2971b6SMasahide NAKAMURA 	} else {
689eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
690eb2971b6SMasahide NAKAMURA 
69135a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
692eb2971b6SMasahide NAKAMURA 		if (!saddr) {
693eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
694eb2971b6SMasahide NAKAMURA 			goto out;
695eb2971b6SMasahide NAKAMURA 		}
696eb2971b6SMasahide NAKAMURA 
6979abbffeeSMasahide NAKAMURA 		err = -ESRCH;
6986f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup_byaddr(net, mark,
6996f26b61eSJamal Hadi Salim 					     &p->daddr, saddr,
700221df1edSAlexey Dobriyan 					     p->proto, p->family);
701eb2971b6SMasahide NAKAMURA 	}
702eb2971b6SMasahide NAKAMURA 
703eb2971b6SMasahide NAKAMURA  out:
704eb2971b6SMasahide NAKAMURA 	if (!x && errp)
705eb2971b6SMasahide NAKAMURA 		*errp = err;
706eb2971b6SMasahide NAKAMURA 	return x;
707eb2971b6SMasahide NAKAMURA }
708eb2971b6SMasahide NAKAMURA 
70922e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
7105424f32eSThomas Graf 		struct nlattr **attrs)
7111da177e4SLinus Torvalds {
712fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
7131da177e4SLinus Torvalds 	struct xfrm_state *x;
714eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
71526b15dadSJamal Hadi Salim 	struct km_event c;
7167b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
7171da177e4SLinus Torvalds 
718fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
7191da177e4SLinus Torvalds 	if (x == NULL)
720eb2971b6SMasahide NAKAMURA 		return err;
7211da177e4SLinus Torvalds 
7226f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
723c8c05a8eSCatherine Zhang 		goto out;
724c8c05a8eSCatherine Zhang 
7251da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
726c8c05a8eSCatherine Zhang 		err = -EPERM;
727c8c05a8eSCatherine Zhang 		goto out;
7281da177e4SLinus Torvalds 	}
7291da177e4SLinus Torvalds 
73026b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
731161a09e7SJoy Latten 
732c8c05a8eSCatherine Zhang 	if (err < 0)
733c8c05a8eSCatherine Zhang 		goto out;
73426b15dadSJamal Hadi Salim 
73526b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
73615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
737f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
73826b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7391da177e4SLinus Torvalds 
740c8c05a8eSCatherine Zhang out:
7412e71029eSTetsuo Handa 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
742c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
74326b15dadSJamal Hadi Salim 	return err;
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
7471da177e4SLinus Torvalds {
748f778a636SMathias Krause 	memset(p, 0, sizeof(*p));
7491da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
7501da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
7511da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
7521da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
753e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
754e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay, &p->stats.replay);
755e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
75654489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
7571da177e4SLinus Torvalds 	p->mode = x->props.mode;
7581da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
7591da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
7601da177e4SLinus Torvalds 	p->family = x->props.family;
7611da177e4SLinus Torvalds 	p->flags = x->props.flags;
7621da177e4SLinus Torvalds 	p->seq = x->km.seq;
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds struct xfrm_dump_info {
7661da177e4SLinus Torvalds 	struct sk_buff *in_skb;
7671da177e4SLinus Torvalds 	struct sk_buff *out_skb;
7681da177e4SLinus Torvalds 	u32 nlmsg_seq;
7691da177e4SLinus Torvalds 	u16 nlmsg_flags;
7701da177e4SLinus Torvalds };
7711da177e4SLinus Torvalds 
772c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
773c0144beaSThomas Graf {
774c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
775c0144beaSThomas Graf 	struct nlattr *attr;
77668325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
777c0144beaSThomas Graf 
778c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
779c0144beaSThomas Graf 	if (attr == NULL)
780c0144beaSThomas Graf 		return -EMSGSIZE;
781c0144beaSThomas Graf 
782c0144beaSThomas Graf 	uctx = nla_data(attr);
783c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
784c0144beaSThomas Graf 	uctx->len = ctx_size;
785c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
786c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
787c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
788c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
789c0144beaSThomas Graf 
790c0144beaSThomas Graf 	return 0;
791c0144beaSThomas Graf }
792c0144beaSThomas Graf 
793d77e38e6SSteffen Klassert static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
794d77e38e6SSteffen Klassert {
795d77e38e6SSteffen Klassert 	struct xfrm_user_offload *xuo;
796d77e38e6SSteffen Klassert 	struct nlattr *attr;
797d77e38e6SSteffen Klassert 
798d77e38e6SSteffen Klassert 	attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
799d77e38e6SSteffen Klassert 	if (attr == NULL)
800d77e38e6SSteffen Klassert 		return -EMSGSIZE;
801d77e38e6SSteffen Klassert 
802d77e38e6SSteffen Klassert 	xuo = nla_data(attr);
8035fe0d4bdSMathias Krause 	memset(xuo, 0, sizeof(*xuo));
804d77e38e6SSteffen Klassert 	xuo->ifindex = xso->dev->ifindex;
805d77e38e6SSteffen Klassert 	xuo->flags = xso->flags;
806d77e38e6SSteffen Klassert 
807d77e38e6SSteffen Klassert 	return 0;
808d77e38e6SSteffen Klassert }
809d77e38e6SSteffen Klassert 
8104447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
8114447bb33SMartin Willi {
8124447bb33SMartin Willi 	struct xfrm_algo *algo;
8134447bb33SMartin Willi 	struct nlattr *nla;
8144447bb33SMartin Willi 
8154447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
8164447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
8174447bb33SMartin Willi 	if (!nla)
8184447bb33SMartin Willi 		return -EMSGSIZE;
8194447bb33SMartin Willi 
8204447bb33SMartin Willi 	algo = nla_data(nla);
8214c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
8224447bb33SMartin Willi 	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
8234447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
8244447bb33SMartin Willi 
8254447bb33SMartin Willi 	return 0;
8264447bb33SMartin Willi }
8274447bb33SMartin Willi 
82868325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
82968325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
83068325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
83168325d3bSHerbert Xu 				    struct sk_buff *skb)
8321da177e4SLinus Torvalds {
8331d1e34ddSDavid S. Miller 	int ret = 0;
8341d1e34ddSDavid S. Miller 
8351da177e4SLinus Torvalds 	copy_to_user_state(x, p);
8361da177e4SLinus Torvalds 
837a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
838a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
839a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
840a947b0a9SNicolas Dichtel 		if (ret)
841a947b0a9SNicolas Dichtel 			goto out;
842a947b0a9SNicolas Dichtel 	}
843a947b0a9SNicolas Dichtel 
8441d1e34ddSDavid S. Miller 	if (x->coaddr) {
8451d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
8461d1e34ddSDavid S. Miller 		if (ret)
8471d1e34ddSDavid S. Miller 			goto out;
8481d1e34ddSDavid S. Miller 	}
8491d1e34ddSDavid S. Miller 	if (x->lastused) {
850de95c4a4SNicolas Dichtel 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
851de95c4a4SNicolas Dichtel 					XFRMA_PAD);
8521d1e34ddSDavid S. Miller 		if (ret)
8531d1e34ddSDavid S. Miller 			goto out;
8541d1e34ddSDavid S. Miller 	}
8551d1e34ddSDavid S. Miller 	if (x->aead) {
8561d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
8571d1e34ddSDavid S. Miller 		if (ret)
8581d1e34ddSDavid S. Miller 			goto out;
8591d1e34ddSDavid S. Miller 	}
8601d1e34ddSDavid S. Miller 	if (x->aalg) {
8611d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
8621d1e34ddSDavid S. Miller 		if (!ret)
8631d1e34ddSDavid S. Miller 			ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
8641d1e34ddSDavid S. Miller 				      xfrm_alg_auth_len(x->aalg), x->aalg);
8651d1e34ddSDavid S. Miller 		if (ret)
8661d1e34ddSDavid S. Miller 			goto out;
8671d1e34ddSDavid S. Miller 	}
8681d1e34ddSDavid S. Miller 	if (x->ealg) {
8691d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
8701d1e34ddSDavid S. Miller 		if (ret)
8711d1e34ddSDavid S. Miller 			goto out;
8721d1e34ddSDavid S. Miller 	}
8731d1e34ddSDavid S. Miller 	if (x->calg) {
8741d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
8751d1e34ddSDavid S. Miller 		if (ret)
8761d1e34ddSDavid S. Miller 			goto out;
8771d1e34ddSDavid S. Miller 	}
8781d1e34ddSDavid S. Miller 	if (x->encap) {
8791d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
8801d1e34ddSDavid S. Miller 		if (ret)
8811d1e34ddSDavid S. Miller 			goto out;
8821d1e34ddSDavid S. Miller 	}
8831d1e34ddSDavid S. Miller 	if (x->tfcpad) {
8841d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
8851d1e34ddSDavid S. Miller 		if (ret)
8861d1e34ddSDavid S. Miller 			goto out;
8871d1e34ddSDavid S. Miller 	}
8881d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
8891d1e34ddSDavid S. Miller 	if (ret)
8901d1e34ddSDavid S. Miller 		goto out;
891f293a5e3Sdingzhi 	if (x->replay_esn)
8921d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
893d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
8941d1e34ddSDavid S. Miller 			      x->replay_esn);
895f293a5e3Sdingzhi 	else
896f293a5e3Sdingzhi 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
897f293a5e3Sdingzhi 			      &x->replay);
8981d1e34ddSDavid S. Miller 	if (ret)
8991d1e34ddSDavid S. Miller 		goto out;
900d77e38e6SSteffen Klassert 	if(x->xso.dev)
901d77e38e6SSteffen Klassert 		ret = copy_user_offload(&x->xso, skb);
902d77e38e6SSteffen Klassert 	if (ret)
903d77e38e6SSteffen Klassert 		goto out;
904077fbac4SLorenzo Colitti 	if (x->props.output_mark) {
905077fbac4SLorenzo Colitti 		ret = nla_put_u32(skb, XFRMA_OUTPUT_MARK, x->props.output_mark);
906077fbac4SLorenzo Colitti 		if (ret)
907077fbac4SLorenzo Colitti 			goto out;
908077fbac4SLorenzo Colitti 	}
9098598112dSSteffen Klassert 	if (x->security)
9108598112dSSteffen Klassert 		ret = copy_sec_ctx(x->security, skb);
9111d1e34ddSDavid S. Miller out:
9121d1e34ddSDavid S. Miller 	return ret;
91368325d3bSHerbert Xu }
91468325d3bSHerbert Xu 
91568325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
91668325d3bSHerbert Xu {
91768325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
91868325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
91968325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
92068325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
92168325d3bSHerbert Xu 	struct nlmsghdr *nlh;
92268325d3bSHerbert Xu 	int err;
92368325d3bSHerbert Xu 
92415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
92568325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
92668325d3bSHerbert Xu 	if (nlh == NULL)
92768325d3bSHerbert Xu 		return -EMSGSIZE;
92868325d3bSHerbert Xu 
92968325d3bSHerbert Xu 	p = nlmsg_data(nlh);
93068325d3bSHerbert Xu 
93168325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
9321d1e34ddSDavid S. Miller 	if (err) {
9339825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
93468325d3bSHerbert Xu 		return err;
9351da177e4SLinus Torvalds 	}
9361d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
9371d1e34ddSDavid S. Miller 	return 0;
9381d1e34ddSDavid S. Miller }
9391da177e4SLinus Torvalds 
9404c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
9414c563f76STimo Teras {
9424c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
943283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
944283bc9f3SFan Du 	struct net *net = sock_net(sk);
945283bc9f3SFan Du 
9461ba5bf99SVegard Nossum 	if (cb->args[0])
947283bc9f3SFan Du 		xfrm_state_walk_done(walk, net);
9484c563f76STimo Teras 	return 0;
9494c563f76STimo Teras }
9504c563f76STimo Teras 
951d3623099SNicolas Dichtel static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
9521da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
9531da177e4SLinus Torvalds {
954fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
9554c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
9561da177e4SLinus Torvalds 	struct xfrm_dump_info info;
9571da177e4SLinus Torvalds 
9584c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
9594c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
9604c563f76STimo Teras 
9611da177e4SLinus Torvalds 	info.in_skb = cb->skb;
9621da177e4SLinus Torvalds 	info.out_skb = skb;
9631da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
9641da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
9654c563f76STimo Teras 
9664c563f76STimo Teras 	if (!cb->args[0]) {
967d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
968870a2df4SNicolas Dichtel 		struct xfrm_address_filter *filter = NULL;
969d3623099SNicolas Dichtel 		u8 proto = 0;
970d3623099SNicolas Dichtel 		int err;
971d3623099SNicolas Dichtel 
972fceb6435SJohannes Berg 		err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX, xfrma_policy,
973fceb6435SJohannes Berg 				  NULL);
974d3623099SNicolas Dichtel 		if (err < 0)
975d3623099SNicolas Dichtel 			return err;
976d3623099SNicolas Dichtel 
977870a2df4SNicolas Dichtel 		if (attrs[XFRMA_ADDRESS_FILTER]) {
978df367561SAndrzej Hajda 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
979df367561SAndrzej Hajda 					 sizeof(*filter), GFP_KERNEL);
980d3623099SNicolas Dichtel 			if (filter == NULL)
981d3623099SNicolas Dichtel 				return -ENOMEM;
982d3623099SNicolas Dichtel 		}
983d3623099SNicolas Dichtel 
984d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
985d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
986d3623099SNicolas Dichtel 
987d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
9881ba5bf99SVegard Nossum 		cb->args[0] = 1;
9894c563f76STimo Teras 	}
9904c563f76STimo Teras 
991fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	return skb->len;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
9971da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
9981da177e4SLinus Torvalds {
9991da177e4SLinus Torvalds 	struct xfrm_dump_info info;
10001da177e4SLinus Torvalds 	struct sk_buff *skb;
1001864745d2SMathias Krause 	int err;
10021da177e4SLinus Torvalds 
10037deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
10041da177e4SLinus Torvalds 	if (!skb)
10051da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	info.in_skb = in_skb;
10081da177e4SLinus Torvalds 	info.out_skb = skb;
10091da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
10101da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
10111da177e4SLinus Torvalds 
1012864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
1013864745d2SMathias Krause 	if (err) {
10141da177e4SLinus Torvalds 		kfree_skb(skb);
1015864745d2SMathias Krause 		return ERR_PTR(err);
10161da177e4SLinus Torvalds 	}
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	return skb;
10191da177e4SLinus Torvalds }
10201da177e4SLinus Torvalds 
102121ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
102221ee543eSMichal Kubecek  * Must be called with RCU read lock.
102321ee543eSMichal Kubecek  */
102421ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
102521ee543eSMichal Kubecek 				       u32 pid, unsigned int group)
102621ee543eSMichal Kubecek {
102721ee543eSMichal Kubecek 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
102821ee543eSMichal Kubecek 
102921ee543eSMichal Kubecek 	if (nlsk)
103021ee543eSMichal Kubecek 		return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
103121ee543eSMichal Kubecek 	else
103221ee543eSMichal Kubecek 		return -1;
103321ee543eSMichal Kubecek }
103421ee543eSMichal Kubecek 
10357deb2264SThomas Graf static inline size_t xfrm_spdinfo_msgsize(void)
10367deb2264SThomas Graf {
10377deb2264SThomas Graf 	return NLMSG_ALIGN(4)
10387deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1039880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1040880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1041880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
10427deb2264SThomas Graf }
10437deb2264SThomas Graf 
1044e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
104515e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
1046ecfd6b18SJamal Hadi Salim {
10475a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
10485a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
10495a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1050880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1051ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
10521d1e34ddSDavid S. Miller 	int err;
1053ecfd6b18SJamal Hadi Salim 	u32 *f;
1054880a6fabSChristophe Gouault 	unsigned lseq;
1055ecfd6b18SJamal Hadi Salim 
105615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
105725985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1058ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1059ecfd6b18SJamal Hadi Salim 
1060ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1061ecfd6b18SJamal Hadi Salim 	*f = flags;
1062e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
10635a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
10645a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
10655a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
10665a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
10675a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
10685a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
10695a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
10705a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1071ecfd6b18SJamal Hadi Salim 
1072880a6fabSChristophe Gouault 	do {
1073880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1074880a6fabSChristophe Gouault 
1075880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1076880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1077880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1078880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1079880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1080880a6fabSChristophe Gouault 
10811d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
10821d1e34ddSDavid S. Miller 	if (!err)
10831d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1084880a6fabSChristophe Gouault 	if (!err)
1085880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1086880a6fabSChristophe Gouault 	if (!err)
1087880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
10881d1e34ddSDavid S. Miller 	if (err) {
10891d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
10901d1e34ddSDavid S. Miller 		return err;
10911d1e34ddSDavid S. Miller 	}
1092ecfd6b18SJamal Hadi Salim 
1093053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1094053c095aSJohannes Berg 	return 0;
1095ecfd6b18SJamal Hadi Salim }
1096ecfd6b18SJamal Hadi Salim 
1097880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1098880a6fabSChristophe Gouault 			    struct nlattr **attrs)
1099880a6fabSChristophe Gouault {
1100880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1101880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1102880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1103880a6fabSChristophe Gouault 
1104880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1105880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1106880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1107880a6fabSChristophe Gouault 
1108880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1109880a6fabSChristophe Gouault 			return -EINVAL;
1110880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1111880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1112880a6fabSChristophe Gouault 			return -EINVAL;
1113880a6fabSChristophe Gouault 	}
1114880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1115880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1116880a6fabSChristophe Gouault 
1117880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1118880a6fabSChristophe Gouault 			return -EINVAL;
1119880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1120880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1121880a6fabSChristophe Gouault 			return -EINVAL;
1122880a6fabSChristophe Gouault 	}
1123880a6fabSChristophe Gouault 
1124880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1125880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1126880a6fabSChristophe Gouault 		if (thresh4) {
1127880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1128880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1129880a6fabSChristophe Gouault 		}
1130880a6fabSChristophe Gouault 		if (thresh6) {
1131880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1132880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1133880a6fabSChristophe Gouault 		}
1134880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1135880a6fabSChristophe Gouault 
1136880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1137880a6fabSChristophe Gouault 	}
1138880a6fabSChristophe Gouault 
1139880a6fabSChristophe Gouault 	return 0;
1140880a6fabSChristophe Gouault }
1141880a6fabSChristophe Gouault 
1142ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
11435424f32eSThomas Graf 		struct nlattr **attrs)
1144ecfd6b18SJamal Hadi Salim {
1145a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1146ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
11477b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
114815e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1149ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
1150ecfd6b18SJamal Hadi Salim 
11517deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1152ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1153ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1154ecfd6b18SJamal Hadi Salim 
115515e47304SEric W. Biederman 	if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1156ecfd6b18SJamal Hadi Salim 		BUG();
1157ecfd6b18SJamal Hadi Salim 
115815e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1159ecfd6b18SJamal Hadi Salim }
1160ecfd6b18SJamal Hadi Salim 
11617deb2264SThomas Graf static inline size_t xfrm_sadinfo_msgsize(void)
11627deb2264SThomas Graf {
11637deb2264SThomas Graf 	return NLMSG_ALIGN(4)
11647deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
11657deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
11667deb2264SThomas Graf }
11677deb2264SThomas Graf 
1168e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
116915e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
117028d8909bSJamal Hadi Salim {
1171af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1172af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
117328d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
11741d1e34ddSDavid S. Miller 	int err;
117528d8909bSJamal Hadi Salim 	u32 *f;
117628d8909bSJamal Hadi Salim 
117715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
117825985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
117928d8909bSJamal Hadi Salim 		return -EMSGSIZE;
118028d8909bSJamal Hadi Salim 
118128d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
118228d8909bSJamal Hadi Salim 	*f = flags;
1183e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
118428d8909bSJamal Hadi Salim 
1185af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1186af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1187af11e316SJamal Hadi Salim 
11881d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
11891d1e34ddSDavid S. Miller 	if (!err)
11901d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
11911d1e34ddSDavid S. Miller 	if (err) {
11921d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
11931d1e34ddSDavid S. Miller 		return err;
11941d1e34ddSDavid S. Miller 	}
119528d8909bSJamal Hadi Salim 
1196053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1197053c095aSJohannes Berg 	return 0;
119828d8909bSJamal Hadi Salim }
119928d8909bSJamal Hadi Salim 
120028d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12015424f32eSThomas Graf 		struct nlattr **attrs)
120228d8909bSJamal Hadi Salim {
1203a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
120428d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
12057b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
120615e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
120728d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
120828d8909bSJamal Hadi Salim 
12097deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
121028d8909bSJamal Hadi Salim 	if (r_skb == NULL)
121128d8909bSJamal Hadi Salim 		return -ENOMEM;
121228d8909bSJamal Hadi Salim 
121315e47304SEric W. Biederman 	if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
121428d8909bSJamal Hadi Salim 		BUG();
121528d8909bSJamal Hadi Salim 
121615e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
121728d8909bSJamal Hadi Salim }
121828d8909bSJamal Hadi Salim 
121922e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
12205424f32eSThomas Graf 		struct nlattr **attrs)
12211da177e4SLinus Torvalds {
1222fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12237b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
12241da177e4SLinus Torvalds 	struct xfrm_state *x;
12251da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1226eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
12271da177e4SLinus Torvalds 
1228fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
12291da177e4SLinus Torvalds 	if (x == NULL)
12301da177e4SLinus Torvalds 		goto out_noput;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
12331da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
12341da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
12351da177e4SLinus Torvalds 	} else {
123615e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
12371da177e4SLinus Torvalds 	}
12381da177e4SLinus Torvalds 	xfrm_state_put(x);
12391da177e4SLinus Torvalds out_noput:
12401da177e4SLinus Torvalds 	return err;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
124322e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
12445424f32eSThomas Graf 		struct nlattr **attrs)
12451da177e4SLinus Torvalds {
1246fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12471da177e4SLinus Torvalds 	struct xfrm_state *x;
12481da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
12491da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
12501da177e4SLinus Torvalds 	xfrm_address_t *daddr;
12511da177e4SLinus Torvalds 	int family;
12521da177e4SLinus Torvalds 	int err;
12536f26b61eSJamal Hadi Salim 	u32 mark;
12546f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
12551da177e4SLinus Torvalds 
12567b67c857SThomas Graf 	p = nlmsg_data(nlh);
1257776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
12581da177e4SLinus Torvalds 	if (err)
12591da177e4SLinus Torvalds 		goto out_noput;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	family = p->info.family;
12621da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	x = NULL;
12656f26b61eSJamal Hadi Salim 
12666f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
12671da177e4SLinus Torvalds 	if (p->info.seq) {
12686f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
126970e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
12701da177e4SLinus Torvalds 			xfrm_state_put(x);
12711da177e4SLinus Torvalds 			x = NULL;
12721da177e4SLinus Torvalds 		}
12731da177e4SLinus Torvalds 	}
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	if (!x)
12766f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
12771da177e4SLinus Torvalds 				  p->info.id.proto, daddr,
12781da177e4SLinus Torvalds 				  &p->info.saddr, 1,
12791da177e4SLinus Torvalds 				  family);
12801da177e4SLinus Torvalds 	err = -ENOENT;
12811da177e4SLinus Torvalds 	if (x == NULL)
12821da177e4SLinus Torvalds 		goto out_noput;
12831da177e4SLinus Torvalds 
1284658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1285658b219eSHerbert Xu 	if (err)
1286658b219eSHerbert Xu 		goto out;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
12891da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
12901da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
12911da177e4SLinus Torvalds 		goto out;
12921da177e4SLinus Torvalds 	}
12931da177e4SLinus Torvalds 
129415e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds out:
12971da177e4SLinus Torvalds 	xfrm_state_put(x);
12981da177e4SLinus Torvalds out_noput:
12991da177e4SLinus Torvalds 	return err;
13001da177e4SLinus Torvalds }
13011da177e4SLinus Torvalds 
1302b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
13031da177e4SLinus Torvalds {
13041da177e4SLinus Torvalds 	switch (dir) {
13051da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
13061da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
13071da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
13081da177e4SLinus Torvalds 		break;
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	default:
13111da177e4SLinus Torvalds 		return -EINVAL;
13123ff50b79SStephen Hemminger 	}
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	return 0;
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds 
1317b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1318f7b6983fSMasahide NAKAMURA {
1319f7b6983fSMasahide NAKAMURA 	switch (type) {
1320f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1321f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1322f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1323f7b6983fSMasahide NAKAMURA #endif
1324f7b6983fSMasahide NAKAMURA 		break;
1325f7b6983fSMasahide NAKAMURA 
1326f7b6983fSMasahide NAKAMURA 	default:
1327f7b6983fSMasahide NAKAMURA 		return -EINVAL;
13283ff50b79SStephen Hemminger 	}
1329f7b6983fSMasahide NAKAMURA 
1330f7b6983fSMasahide NAKAMURA 	return 0;
1331f7b6983fSMasahide NAKAMURA }
1332f7b6983fSMasahide NAKAMURA 
13331da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
13341da177e4SLinus Torvalds {
1335e682adf0SFan Du 	int ret;
1336e682adf0SFan Du 
13371da177e4SLinus Torvalds 	switch (p->share) {
13381da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
13391da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
13401da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
13411da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
13421da177e4SLinus Torvalds 		break;
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	default:
13451da177e4SLinus Torvalds 		return -EINVAL;
13463ff50b79SStephen Hemminger 	}
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	switch (p->action) {
13491da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
13501da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
13511da177e4SLinus Torvalds 		break;
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	default:
13541da177e4SLinus Torvalds 		return -EINVAL;
13553ff50b79SStephen Hemminger 	}
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	switch (p->sel.family) {
13581da177e4SLinus Torvalds 	case AF_INET:
13591da177e4SLinus Torvalds 		break;
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	case AF_INET6:
1362dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
13631da177e4SLinus Torvalds 		break;
13641da177e4SLinus Torvalds #else
13651da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
13661da177e4SLinus Torvalds #endif
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	default:
13691da177e4SLinus Torvalds 		return -EINVAL;
13703ff50b79SStephen Hemminger 	}
13711da177e4SLinus Torvalds 
1372e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1373e682adf0SFan Du 	if (ret)
1374e682adf0SFan Du 		return ret;
1375e682adf0SFan Du 	if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1376e682adf0SFan Du 		return -EINVAL;
1377e682adf0SFan Du 
1378e682adf0SFan Du 	return 0;
13791da177e4SLinus Torvalds }
13801da177e4SLinus Torvalds 
13815424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1382df71837dSTrent Jaeger {
13835424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1384df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1385df71837dSTrent Jaeger 
1386df71837dSTrent Jaeger 	if (!rt)
1387df71837dSTrent Jaeger 		return 0;
1388df71837dSTrent Jaeger 
13895424f32eSThomas Graf 	uctx = nla_data(rt);
139052a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1391df71837dSTrent Jaeger }
1392df71837dSTrent Jaeger 
13931da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
13941da177e4SLinus Torvalds 			   int nr)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	int i;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
13991da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
14001da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
14031da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
14041da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
14051da177e4SLinus Torvalds 		t->reqid = ut->reqid;
14061da177e4SLinus Torvalds 		t->mode = ut->mode;
14071da177e4SLinus Torvalds 		t->share = ut->share;
14081da177e4SLinus Torvalds 		t->optional = ut->optional;
14091da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
14101da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
14111da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1412c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1413c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
14148511d01dSMiika Komu 		t->encap_family = ut->family;
14151da177e4SLinus Torvalds 	}
14161da177e4SLinus Torvalds }
14171da177e4SLinus Torvalds 
1418b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1419b4ad86bfSDavid S. Miller {
1420b4ad86bfSDavid S. Miller 	int i;
1421b4ad86bfSDavid S. Miller 
1422b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1423b4ad86bfSDavid S. Miller 		return -EINVAL;
1424b4ad86bfSDavid S. Miller 
1425b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1426b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1427b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1428b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1429b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1430b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1431b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1432b4ad86bfSDavid S. Miller 		 */
1433b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1434b4ad86bfSDavid S. Miller 			ut[i].family = family;
1435b4ad86bfSDavid S. Miller 
1436b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1437b4ad86bfSDavid S. Miller 		case AF_INET:
1438b4ad86bfSDavid S. Miller 			break;
1439dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1440b4ad86bfSDavid S. Miller 		case AF_INET6:
1441b4ad86bfSDavid S. Miller 			break;
1442b4ad86bfSDavid S. Miller #endif
1443b4ad86bfSDavid S. Miller 		default:
1444b4ad86bfSDavid S. Miller 			return -EINVAL;
14453ff50b79SStephen Hemminger 		}
1446b4ad86bfSDavid S. Miller 	}
1447b4ad86bfSDavid S. Miller 
1448b4ad86bfSDavid S. Miller 	return 0;
1449b4ad86bfSDavid S. Miller }
1450b4ad86bfSDavid S. Miller 
14515424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
14521da177e4SLinus Torvalds {
14535424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	if (!rt) {
14561da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
14571da177e4SLinus Torvalds 	} else {
14585424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
14595424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1460b4ad86bfSDavid S. Miller 		int err;
14611da177e4SLinus Torvalds 
1462b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1463b4ad86bfSDavid S. Miller 		if (err)
1464b4ad86bfSDavid S. Miller 			return err;
14651da177e4SLinus Torvalds 
14665424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
14671da177e4SLinus Torvalds 	}
14681da177e4SLinus Torvalds 	return 0;
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
14715424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1472f7b6983fSMasahide NAKAMURA {
14735424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1474f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1475b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1476f7b6983fSMasahide NAKAMURA 	int err;
1477f7b6983fSMasahide NAKAMURA 
1478f7b6983fSMasahide NAKAMURA 	if (rt) {
14795424f32eSThomas Graf 		upt = nla_data(rt);
1480f7b6983fSMasahide NAKAMURA 		type = upt->type;
1481f7b6983fSMasahide NAKAMURA 	}
1482f7b6983fSMasahide NAKAMURA 
1483f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1484f7b6983fSMasahide NAKAMURA 	if (err)
1485f7b6983fSMasahide NAKAMURA 		return err;
1486f7b6983fSMasahide NAKAMURA 
1487f7b6983fSMasahide NAKAMURA 	*tp = type;
1488f7b6983fSMasahide NAKAMURA 	return 0;
1489f7b6983fSMasahide NAKAMURA }
1490f7b6983fSMasahide NAKAMURA 
14911da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
14921da177e4SLinus Torvalds {
14931da177e4SLinus Torvalds 	xp->priority = p->priority;
14941da177e4SLinus Torvalds 	xp->index = p->index;
14951da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
14961da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
14971da177e4SLinus Torvalds 	xp->action = p->action;
14981da177e4SLinus Torvalds 	xp->flags = p->flags;
14991da177e4SLinus Torvalds 	xp->family = p->sel.family;
15001da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
15041da177e4SLinus Torvalds {
15057b789836SMathias Krause 	memset(p, 0, sizeof(*p));
15061da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
15071da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
15081da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
15091da177e4SLinus Torvalds 	p->priority = xp->priority;
15101da177e4SLinus Torvalds 	p->index = xp->index;
15111da177e4SLinus Torvalds 	p->sel.family = xp->family;
15121da177e4SLinus Torvalds 	p->dir = dir;
15131da177e4SLinus Torvalds 	p->action = xp->action;
15141da177e4SLinus Torvalds 	p->flags = xp->flags;
15151da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
1518fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
15191da177e4SLinus Torvalds {
1520fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
15211da177e4SLinus Torvalds 	int err;
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 	if (!xp) {
15241da177e4SLinus Torvalds 		*errp = -ENOMEM;
15251da177e4SLinus Torvalds 		return NULL;
15261da177e4SLinus Torvalds 	}
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1529df71837dSTrent Jaeger 
153035a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1531f7b6983fSMasahide NAKAMURA 	if (err)
1532f7b6983fSMasahide NAKAMURA 		goto error;
1533f7b6983fSMasahide NAKAMURA 
153435a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
153535a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1536f7b6983fSMasahide NAKAMURA 	if (err)
1537f7b6983fSMasahide NAKAMURA 		goto error;
15381da177e4SLinus Torvalds 
1539295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1540295fae56SJamal Hadi Salim 
15411da177e4SLinus Torvalds 	return xp;
1542f7b6983fSMasahide NAKAMURA  error:
1543f7b6983fSMasahide NAKAMURA 	*errp = err;
154412a169e7SHerbert Xu 	xp->walk.dead = 1;
154564c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1546f7b6983fSMasahide NAKAMURA 	return NULL;
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
154922e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
15505424f32eSThomas Graf 		struct nlattr **attrs)
15511da177e4SLinus Torvalds {
1552fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
15537b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
15541da177e4SLinus Torvalds 	struct xfrm_policy *xp;
155526b15dadSJamal Hadi Salim 	struct km_event c;
15561da177e4SLinus Torvalds 	int err;
15571da177e4SLinus Torvalds 	int excl;
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
15601da177e4SLinus Torvalds 	if (err)
15611da177e4SLinus Torvalds 		return err;
156235a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1563df71837dSTrent Jaeger 	if (err)
1564df71837dSTrent Jaeger 		return err;
15651da177e4SLinus Torvalds 
1566fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
15671da177e4SLinus Torvalds 	if (!xp)
15681da177e4SLinus Torvalds 		return err;
15691da177e4SLinus Torvalds 
157025985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
157126b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
157226b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
157326b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
15741da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
15751da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
15762e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1577161a09e7SJoy Latten 
15781da177e4SLinus Torvalds 	if (err) {
157903e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
15801da177e4SLinus Torvalds 		kfree(xp);
15811da177e4SLinus Torvalds 		return err;
15821da177e4SLinus Torvalds 	}
15831da177e4SLinus Torvalds 
1584f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
158526b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
158615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
158726b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
158826b15dadSJamal Hadi Salim 
15891da177e4SLinus Torvalds 	xfrm_pol_put(xp);
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	return 0;
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
15951da177e4SLinus Torvalds {
15961da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
15971da177e4SLinus Torvalds 	int i;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
16001da177e4SLinus Torvalds 		return 0;
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
16031da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
16041da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
16051da177e4SLinus Torvalds 
16061f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
16071da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
16088511d01dSMiika Komu 		up->family = kp->encap_family;
16091da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
16101da177e4SLinus Torvalds 		up->reqid = kp->reqid;
16111da177e4SLinus Torvalds 		up->mode = kp->mode;
16121da177e4SLinus Torvalds 		up->share = kp->share;
16131da177e4SLinus Torvalds 		up->optional = kp->optional;
16141da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
16151da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
16161da177e4SLinus Torvalds 		up->calgos = kp->calgos;
16171da177e4SLinus Torvalds 	}
16181da177e4SLinus Torvalds 
1619c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1620c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1621df71837dSTrent Jaeger }
1622df71837dSTrent Jaeger 
16230d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
16240d681623SSerge Hallyn {
16250d681623SSerge Hallyn 	if (x->security) {
16260d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
16270d681623SSerge Hallyn 	}
16280d681623SSerge Hallyn 	return 0;
16290d681623SSerge Hallyn }
16300d681623SSerge Hallyn 
16310d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
16320d681623SSerge Hallyn {
16331d1e34ddSDavid S. Miller 	if (xp->security)
16340d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
16350d681623SSerge Hallyn 	return 0;
16360d681623SSerge Hallyn }
1637cfbfd45aSThomas Graf static inline size_t userpolicy_type_attrsize(void)
1638cfbfd45aSThomas Graf {
1639cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1640cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1641cfbfd45aSThomas Graf #else
1642cfbfd45aSThomas Graf 	return 0;
1643cfbfd45aSThomas Graf #endif
1644cfbfd45aSThomas Graf }
16450d681623SSerge Hallyn 
1646f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1647b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1648f7b6983fSMasahide NAKAMURA {
1649c0144beaSThomas Graf 	struct xfrm_userpolicy_type upt = {
1650c0144beaSThomas Graf 		.type = type,
1651c0144beaSThomas Graf 	};
1652f7b6983fSMasahide NAKAMURA 
1653c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1654f7b6983fSMasahide NAKAMURA }
1655f7b6983fSMasahide NAKAMURA 
1656f7b6983fSMasahide NAKAMURA #else
1657b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1658f7b6983fSMasahide NAKAMURA {
1659f7b6983fSMasahide NAKAMURA 	return 0;
1660f7b6983fSMasahide NAKAMURA }
1661f7b6983fSMasahide NAKAMURA #endif
1662f7b6983fSMasahide NAKAMURA 
16631da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
16641da177e4SLinus Torvalds {
16651da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
16661da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
16671da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
16681da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
16691da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
16701d1e34ddSDavid S. Miller 	int err;
16711da177e4SLinus Torvalds 
167215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
167379b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
167479b8b7f4SThomas Graf 	if (nlh == NULL)
167579b8b7f4SThomas Graf 		return -EMSGSIZE;
16761da177e4SLinus Torvalds 
16777b67c857SThomas Graf 	p = nlmsg_data(nlh);
16781da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
16791d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
16801d1e34ddSDavid S. Miller 	if (!err)
16811d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
16821d1e34ddSDavid S. Miller 	if (!err)
16831d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
16841d1e34ddSDavid S. Miller 	if (!err)
16851d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
16861d1e34ddSDavid S. Miller 	if (err) {
16871d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
16881d1e34ddSDavid S. Miller 		return err;
16891d1e34ddSDavid S. Miller 	}
16909825069dSThomas Graf 	nlmsg_end(skb, nlh);
16911da177e4SLinus Torvalds 	return 0;
16921da177e4SLinus Torvalds }
16931da177e4SLinus Torvalds 
16944c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
16954c563f76STimo Teras {
16964c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1697283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
16984c563f76STimo Teras 
1699283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
17004c563f76STimo Teras 	return 0;
17014c563f76STimo Teras }
17024c563f76STimo Teras 
17031da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
17041da177e4SLinus Torvalds {
1705fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
17064c563f76STimo Teras 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
17071da177e4SLinus Torvalds 	struct xfrm_dump_info info;
17081da177e4SLinus Torvalds 
17094c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
17104c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
17114c563f76STimo Teras 
17121da177e4SLinus Torvalds 	info.in_skb = cb->skb;
17131da177e4SLinus Torvalds 	info.out_skb = skb;
17141da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
17151da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
17164c563f76STimo Teras 
17174c563f76STimo Teras 	if (!cb->args[0]) {
17184c563f76STimo Teras 		cb->args[0] = 1;
17194c563f76STimo Teras 		xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
17204c563f76STimo Teras 	}
17214c563f76STimo Teras 
1722fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	return skb->len;
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
17281da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
17291da177e4SLinus Torvalds 					  int dir, u32 seq)
17301da177e4SLinus Torvalds {
17311da177e4SLinus Torvalds 	struct xfrm_dump_info info;
17321da177e4SLinus Torvalds 	struct sk_buff *skb;
1733c2546372SMathias Krause 	int err;
17341da177e4SLinus Torvalds 
17357deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17361da177e4SLinus Torvalds 	if (!skb)
17371da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds 	info.in_skb = in_skb;
17401da177e4SLinus Torvalds 	info.out_skb = skb;
17411da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
17421da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
17431da177e4SLinus Torvalds 
1744c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1745c2546372SMathias Krause 	if (err) {
17461da177e4SLinus Torvalds 		kfree_skb(skb);
1747c2546372SMathias Krause 		return ERR_PTR(err);
17481da177e4SLinus Torvalds 	}
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds 	return skb;
17511da177e4SLinus Torvalds }
17521da177e4SLinus Torvalds 
175322e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
17545424f32eSThomas Graf 		struct nlattr **attrs)
17551da177e4SLinus Torvalds {
1756fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
17571da177e4SLinus Torvalds 	struct xfrm_policy *xp;
17581da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1759b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
17601da177e4SLinus Torvalds 	int err;
176126b15dadSJamal Hadi Salim 	struct km_event c;
17621da177e4SLinus Torvalds 	int delete;
1763295fae56SJamal Hadi Salim 	struct xfrm_mark m;
1764295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
17651da177e4SLinus Torvalds 
17667b67c857SThomas Graf 	p = nlmsg_data(nlh);
17671da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
17681da177e4SLinus Torvalds 
176935a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1770f7b6983fSMasahide NAKAMURA 	if (err)
1771f7b6983fSMasahide NAKAMURA 		return err;
1772f7b6983fSMasahide NAKAMURA 
17731da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
17741da177e4SLinus Torvalds 	if (err)
17751da177e4SLinus Torvalds 		return err;
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	if (p->index)
1778295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1779df71837dSTrent Jaeger 	else {
17805424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
178103e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1782df71837dSTrent Jaeger 
178335a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
1784df71837dSTrent Jaeger 		if (err)
1785df71837dSTrent Jaeger 			return err;
1786df71837dSTrent Jaeger 
17872c8dd116SDenis V. Lunev 		ctx = NULL;
1788df71837dSTrent Jaeger 		if (rt) {
17895424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1790df71837dSTrent Jaeger 
179152a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
179203e1ad7bSPaul Moore 			if (err)
1793df71837dSTrent Jaeger 				return err;
17942c8dd116SDenis V. Lunev 		}
1795295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
17966f26b61eSJamal Hadi Salim 					   ctx, delete, &err);
179703e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
1798df71837dSTrent Jaeger 	}
17991da177e4SLinus Torvalds 	if (xp == NULL)
18001da177e4SLinus Torvalds 		return -ENOENT;
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	if (!delete) {
18031da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
18061da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
18071da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
18081da177e4SLinus Torvalds 		} else {
1809a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
181015e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
18111da177e4SLinus Torvalds 		}
181226b15dadSJamal Hadi Salim 	} else {
18132e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
181413fcfbb0SDavid S. Miller 
181513fcfbb0SDavid S. Miller 		if (err != 0)
1816c8c05a8eSCatherine Zhang 			goto out;
181713fcfbb0SDavid S. Miller 
1818e7443892SHerbert Xu 		c.data.byid = p->index;
1819f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
182026b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
182115e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
182226b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
18231da177e4SLinus Torvalds 	}
18241da177e4SLinus Torvalds 
1825c8c05a8eSCatherine Zhang out:
1826ef41aaa0SEric Paris 	xfrm_pol_put(xp);
18271da177e4SLinus Torvalds 	return err;
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds 
183022e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
18315424f32eSThomas Graf 		struct nlattr **attrs)
18321da177e4SLinus Torvalds {
1833fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
183426b15dadSJamal Hadi Salim 	struct km_event c;
18357b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
18364aa2e62cSJoy Latten 	int err;
18371da177e4SLinus Torvalds 
18382e71029eSTetsuo Handa 	err = xfrm_state_flush(net, p->proto, true);
18399e64cc95SJamal Hadi Salim 	if (err) {
18409e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
18419e64cc95SJamal Hadi Salim 			return 0;
1842069c474eSDavid S. Miller 		return err;
18439e64cc95SJamal Hadi Salim 	}
1844bf08867fSHerbert Xu 	c.data.proto = p->proto;
1845f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
184626b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
184715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
18487067802eSAlexey Dobriyan 	c.net = net;
184926b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
185026b15dadSJamal Hadi Salim 
18511da177e4SLinus Torvalds 	return 0;
18521da177e4SLinus Torvalds }
18531da177e4SLinus Torvalds 
1854d8647b79SSteffen Klassert static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
18557deb2264SThomas Graf {
1856d8647b79SSteffen Klassert 	size_t replay_size = x->replay_esn ?
1857d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
1858d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
1859d8647b79SSteffen Klassert 
18607deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1861d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
1862de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
18636f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
18647deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
18657deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
18667deb2264SThomas Graf }
1867d51d081dSJamal Hadi Salim 
1868214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1869d51d081dSJamal Hadi Salim {
1870d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1871d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
18721d1e34ddSDavid S. Miller 	int err;
1873d51d081dSJamal Hadi Salim 
187415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
187579b8b7f4SThomas Graf 	if (nlh == NULL)
187679b8b7f4SThomas Graf 		return -EMSGSIZE;
1877d51d081dSJamal Hadi Salim 
18787b67c857SThomas Graf 	id = nlmsg_data(nlh);
1879931e79d7SMathias Krause 	memset(&id->sa_id, 0, sizeof(id->sa_id));
18802b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1881d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
1882d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
1883d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
18842b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
18852b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
1886d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
1887d51d081dSJamal Hadi Salim 
1888d0fde795SDavid S. Miller 	if (x->replay_esn) {
18891d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1890d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
18911d1e34ddSDavid S. Miller 			      x->replay_esn);
1892d0fde795SDavid S. Miller 	} else {
18931d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
18941d1e34ddSDavid S. Miller 			      &x->replay);
1895d0fde795SDavid S. Miller 	}
18961d1e34ddSDavid S. Miller 	if (err)
18971d1e34ddSDavid S. Miller 		goto out_cancel;
1898de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1899de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
19001d1e34ddSDavid S. Miller 	if (err)
19011d1e34ddSDavid S. Miller 		goto out_cancel;
1902d8647b79SSteffen Klassert 
19031d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
19041d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
19051d1e34ddSDavid S. Miller 		if (err)
19061d1e34ddSDavid S. Miller 			goto out_cancel;
19071d1e34ddSDavid S. Miller 	}
19081d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
19091d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
19101d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
19111d1e34ddSDavid S. Miller 		if (err)
19121d1e34ddSDavid S. Miller 			goto out_cancel;
19131d1e34ddSDavid S. Miller 	}
19141d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
19151d1e34ddSDavid S. Miller 	if (err)
19161d1e34ddSDavid S. Miller 		goto out_cancel;
19176f26b61eSJamal Hadi Salim 
1918053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1919053c095aSJohannes Berg 	return 0;
1920d51d081dSJamal Hadi Salim 
19211d1e34ddSDavid S. Miller out_cancel:
19229825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
19231d1e34ddSDavid S. Miller 	return err;
1924d51d081dSJamal Hadi Salim }
1925d51d081dSJamal Hadi Salim 
192622e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
19275424f32eSThomas Graf 		struct nlattr **attrs)
1928d51d081dSJamal Hadi Salim {
1929fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1930d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1931d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
1932d51d081dSJamal Hadi Salim 	int err;
1933d51d081dSJamal Hadi Salim 	struct km_event c;
19346f26b61eSJamal Hadi Salim 	u32 mark;
19356f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
19367b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
1937d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
1938d51d081dSJamal Hadi Salim 
19396f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
19406f26b61eSJamal Hadi Salim 
19416f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1942d8647b79SSteffen Klassert 	if (x == NULL)
1943d51d081dSJamal Hadi Salim 		return -ESRCH;
1944d8647b79SSteffen Klassert 
1945d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1946d8647b79SSteffen Klassert 	if (r_skb == NULL) {
1947d8647b79SSteffen Klassert 		xfrm_state_put(x);
1948d8647b79SSteffen Klassert 		return -ENOMEM;
1949d51d081dSJamal Hadi Salim 	}
1950d51d081dSJamal Hadi Salim 
1951d51d081dSJamal Hadi Salim 	/*
1952d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
1953d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
1954d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
1955d51d081dSJamal Hadi Salim 	*/
1956d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1957d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
1958d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
195915e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
1960d51d081dSJamal Hadi Salim 
1961d51d081dSJamal Hadi Salim 	if (build_aevent(r_skb, x, &c) < 0)
1962d51d081dSJamal Hadi Salim 		BUG();
196315e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1964d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1965d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1966d51d081dSJamal Hadi Salim 	return err;
1967d51d081dSJamal Hadi Salim }
1968d51d081dSJamal Hadi Salim 
196922e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
19705424f32eSThomas Graf 		struct nlattr **attrs)
1971d51d081dSJamal Hadi Salim {
1972fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1973d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1974d51d081dSJamal Hadi Salim 	struct km_event c;
1975d51d081dSJamal Hadi Salim 	int err = -EINVAL;
19766f26b61eSJamal Hadi Salim 	u32 mark = 0;
19776f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
19787b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
19795424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1980d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
19815424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
19824e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
19834e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
1984d51d081dSJamal Hadi Salim 
19854e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
1986d51d081dSJamal Hadi Salim 		return err;
1987d51d081dSJamal Hadi Salim 
1988d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
1989d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1990d51d081dSJamal Hadi Salim 		return err;
1991d51d081dSJamal Hadi Salim 
19926f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
19936f26b61eSJamal Hadi Salim 
19946f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1995d51d081dSJamal Hadi Salim 	if (x == NULL)
1996d51d081dSJamal Hadi Salim 		return -ESRCH;
1997d51d081dSJamal Hadi Salim 
1998d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
1999d51d081dSJamal Hadi Salim 		goto out;
2000d51d081dSJamal Hadi Salim 
20014479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
2002e2b19125SSteffen Klassert 	if (err)
2003e2b19125SSteffen Klassert 		goto out;
2004e2b19125SSteffen Klassert 
2005d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2006e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
2007d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2008d51d081dSJamal Hadi Salim 
2009d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
2010d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
201115e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2012d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
2013d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
2014d51d081dSJamal Hadi Salim 	err = 0;
2015d51d081dSJamal Hadi Salim out:
2016d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2017d51d081dSJamal Hadi Salim 	return err;
2018d51d081dSJamal Hadi Salim }
2019d51d081dSJamal Hadi Salim 
202022e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
20215424f32eSThomas Graf 		struct nlattr **attrs)
20221da177e4SLinus Torvalds {
2023fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
202426b15dadSJamal Hadi Salim 	struct km_event c;
2025b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
2026f7b6983fSMasahide NAKAMURA 	int err;
202726b15dadSJamal Hadi Salim 
202835a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2029f7b6983fSMasahide NAKAMURA 	if (err)
2030f7b6983fSMasahide NAKAMURA 		return err;
2031f7b6983fSMasahide NAKAMURA 
20322e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
20332f1eb65fSJamal Hadi Salim 	if (err) {
20342f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
20352f1eb65fSJamal Hadi Salim 			return 0;
2036069c474eSDavid S. Miller 		return err;
20372f1eb65fSJamal Hadi Salim 	}
20382f1eb65fSJamal Hadi Salim 
2039f7b6983fSMasahide NAKAMURA 	c.data.type = type;
2040f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
204126b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
204215e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
20437067802eSAlexey Dobriyan 	c.net = net;
204426b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
20451da177e4SLinus Torvalds 	return 0;
20461da177e4SLinus Torvalds }
20471da177e4SLinus Torvalds 
204822e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
20495424f32eSThomas Graf 		struct nlattr **attrs)
20506c5c8ca7SJamal Hadi Salim {
2051fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
20526c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
20537b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
20546c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2055b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
20566c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2057295fae56SJamal Hadi Salim 	struct xfrm_mark m;
2058295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
20596c5c8ca7SJamal Hadi Salim 
206035a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2061f7b6983fSMasahide NAKAMURA 	if (err)
2062f7b6983fSMasahide NAKAMURA 		return err;
2063f7b6983fSMasahide NAKAMURA 
2064c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
2065c8bf4d04STimo Teräs 	if (err)
2066c8bf4d04STimo Teräs 		return err;
2067c8bf4d04STimo Teräs 
20686c5c8ca7SJamal Hadi Salim 	if (p->index)
2069295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
20706c5c8ca7SJamal Hadi Salim 	else {
20715424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
207203e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
20736c5c8ca7SJamal Hadi Salim 
207435a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
20756c5c8ca7SJamal Hadi Salim 		if (err)
20766c5c8ca7SJamal Hadi Salim 			return err;
20776c5c8ca7SJamal Hadi Salim 
20782c8dd116SDenis V. Lunev 		ctx = NULL;
20796c5c8ca7SJamal Hadi Salim 		if (rt) {
20805424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
20816c5c8ca7SJamal Hadi Salim 
208252a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
208303e1ad7bSPaul Moore 			if (err)
20846c5c8ca7SJamal Hadi Salim 				return err;
20852c8dd116SDenis V. Lunev 		}
2086295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
20876f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
208803e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
20896c5c8ca7SJamal Hadi Salim 	}
20906c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2091ef41aaa0SEric Paris 		return -ENOENT;
209203e1ad7bSPaul Moore 
2093ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
20946c5c8ca7SJamal Hadi Salim 		goto out;
20956c5c8ca7SJamal Hadi Salim 
20966c5c8ca7SJamal Hadi Salim 	err = 0;
20976c5c8ca7SJamal Hadi Salim 	if (up->hard) {
20986c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
20992e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
21006c5c8ca7SJamal Hadi Salim 	}
2101c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
21026c5c8ca7SJamal Hadi Salim 
21036c5c8ca7SJamal Hadi Salim out:
21046c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
21056c5c8ca7SJamal Hadi Salim 	return err;
21066c5c8ca7SJamal Hadi Salim }
21076c5c8ca7SJamal Hadi Salim 
210822e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
21095424f32eSThomas Graf 		struct nlattr **attrs)
211053bc6b4dSJamal Hadi Salim {
2111fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
211253bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
211353bc6b4dSJamal Hadi Salim 	int err;
21147b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
211553bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
21166f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2117928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
211853bc6b4dSJamal Hadi Salim 
21196f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
212053bc6b4dSJamal Hadi Salim 
21213a765aa5SDavid S. Miller 	err = -ENOENT;
212253bc6b4dSJamal Hadi Salim 	if (x == NULL)
212353bc6b4dSJamal Hadi Salim 		return err;
212453bc6b4dSJamal Hadi Salim 
212553bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
21263a765aa5SDavid S. Miller 	err = -EINVAL;
212753bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
212853bc6b4dSJamal Hadi Salim 		goto out;
2129c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
213053bc6b4dSJamal Hadi Salim 
2131161a09e7SJoy Latten 	if (ue->hard) {
213253bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
21332e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2134161a09e7SJoy Latten 	}
21353a765aa5SDavid S. Miller 	err = 0;
213653bc6b4dSJamal Hadi Salim out:
213753bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
213853bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
213953bc6b4dSJamal Hadi Salim 	return err;
214053bc6b4dSJamal Hadi Salim }
214153bc6b4dSJamal Hadi Salim 
214222e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
21435424f32eSThomas Graf 		struct nlattr **attrs)
2144980ebd25SJamal Hadi Salim {
2145fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2146980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2147980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2148980ebd25SJamal Hadi Salim 	int i;
21495424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
21506f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2151980ebd25SJamal Hadi Salim 
21527b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2153fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2154980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2155980ebd25SJamal Hadi Salim 
2156980ebd25SJamal Hadi Salim 	if (!x)
2157d8eb9307SIlpo Järvinen 		goto nomem;
2158980ebd25SJamal Hadi Salim 
21596f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
21606f26b61eSJamal Hadi Salim 
2161980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2162d8eb9307SIlpo Järvinen 	if (err)
216373efc324SVegard Nossum 		goto free_state;
2164980ebd25SJamal Hadi Salim 
2165980ebd25SJamal Hadi Salim 	/*   build an XP */
2166fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2167d8eb9307SIlpo Järvinen 	if (!xp)
2168d8eb9307SIlpo Järvinen 		goto free_state;
2169980ebd25SJamal Hadi Salim 
2170980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2171980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2172980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
21736f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
21746f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
21755424f32eSThomas Graf 	ut = nla_data(rt);
2176980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2177980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2178980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2179980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2180980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2181980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2182980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2183980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2184980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2185980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2186980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2187980ebd25SJamal Hadi Salim 
2188980ebd25SJamal Hadi Salim 	}
2189980ebd25SJamal Hadi Salim 
2190980ebd25SJamal Hadi Salim 	kfree(x);
2191980ebd25SJamal Hadi Salim 	kfree(xp);
2192980ebd25SJamal Hadi Salim 
2193980ebd25SJamal Hadi Salim 	return 0;
2194d8eb9307SIlpo Järvinen 
2195d8eb9307SIlpo Järvinen free_state:
2196d8eb9307SIlpo Järvinen 	kfree(x);
2197d8eb9307SIlpo Järvinen nomem:
2198d8eb9307SIlpo Järvinen 	return err;
2199980ebd25SJamal Hadi Salim }
2200980ebd25SJamal Hadi Salim 
22015c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
22025c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
220313c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
22045424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
22055c79de6eSShinta Sugimoto {
22065424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
22075c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
22085c79de6eSShinta Sugimoto 	int i, num_migrate;
22095c79de6eSShinta Sugimoto 
221013c1d189SArnaud Ebalard 	if (k != NULL) {
221113c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
221213c1d189SArnaud Ebalard 
221313c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
221413c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
221513c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
221613c1d189SArnaud Ebalard 		k->family = uk->family;
221713c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
221813c1d189SArnaud Ebalard 	}
221913c1d189SArnaud Ebalard 
22205424f32eSThomas Graf 	um = nla_data(rt);
22215424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
22225c79de6eSShinta Sugimoto 
22235c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
22245c79de6eSShinta Sugimoto 		return -EINVAL;
22255c79de6eSShinta Sugimoto 
22265c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
22275c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
22285c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
22295c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
22305c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
22315c79de6eSShinta Sugimoto 
22325c79de6eSShinta Sugimoto 		ma->proto = um->proto;
22335c79de6eSShinta Sugimoto 		ma->mode = um->mode;
22345c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
22355c79de6eSShinta Sugimoto 
22365c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
22375c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
22385c79de6eSShinta Sugimoto 	}
22395c79de6eSShinta Sugimoto 
22405c79de6eSShinta Sugimoto 	*num = i;
22415c79de6eSShinta Sugimoto 	return 0;
22425c79de6eSShinta Sugimoto }
22435c79de6eSShinta Sugimoto 
22445c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
22455424f32eSThomas Graf 			   struct nlattr **attrs)
22465c79de6eSShinta Sugimoto {
22477b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
22485c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
224913c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
22505c79de6eSShinta Sugimoto 	u8 type;
22515c79de6eSShinta Sugimoto 	int err;
22525c79de6eSShinta Sugimoto 	int n = 0;
22538d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
22544ab47d47SAntony Antony 	struct xfrm_encap_tmpl  *encap = NULL;
22555c79de6eSShinta Sugimoto 
225635a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2257cf5cb79fSThomas Graf 		return -EINVAL;
22585c79de6eSShinta Sugimoto 
225913c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
226013c1d189SArnaud Ebalard 
22615424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
22625c79de6eSShinta Sugimoto 	if (err)
22635c79de6eSShinta Sugimoto 		return err;
22645c79de6eSShinta Sugimoto 
226513c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
22665c79de6eSShinta Sugimoto 	if (err)
22675c79de6eSShinta Sugimoto 		return err;
22685c79de6eSShinta Sugimoto 
22695c79de6eSShinta Sugimoto 	if (!n)
22705c79de6eSShinta Sugimoto 		return 0;
22715c79de6eSShinta Sugimoto 
22724ab47d47SAntony Antony 	if (attrs[XFRMA_ENCAP]) {
22734ab47d47SAntony Antony 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
22744ab47d47SAntony Antony 				sizeof(*encap), GFP_KERNEL);
22754ab47d47SAntony Antony 		if (!encap)
22765c79de6eSShinta Sugimoto 			return 0;
22775c79de6eSShinta Sugimoto 	}
22784ab47d47SAntony Antony 
22794ab47d47SAntony Antony 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
22804ab47d47SAntony Antony 
22814ab47d47SAntony Antony 	kfree(encap);
22824ab47d47SAntony Antony 
22834ab47d47SAntony Antony 	return err;
22844ab47d47SAntony Antony }
22855c79de6eSShinta Sugimoto #else
22865c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
22875424f32eSThomas Graf 			   struct nlattr **attrs)
22885c79de6eSShinta Sugimoto {
22895c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
22905c79de6eSShinta Sugimoto }
22915c79de6eSShinta Sugimoto #endif
22925c79de6eSShinta Sugimoto 
22935c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2294183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
22955c79de6eSShinta Sugimoto {
22965c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
22975c79de6eSShinta Sugimoto 
22985c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
22995c79de6eSShinta Sugimoto 	um.proto = m->proto;
23005c79de6eSShinta Sugimoto 	um.mode = m->mode;
23015c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
23025c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
23035c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
23045c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
23055c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
23065c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
23075c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
23085c79de6eSShinta Sugimoto 
2309c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
23105c79de6eSShinta Sugimoto }
23115c79de6eSShinta Sugimoto 
2312183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
231313c1d189SArnaud Ebalard {
231413c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
231513c1d189SArnaud Ebalard 
231613c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
231713c1d189SArnaud Ebalard 	uk.family = k->family;
231813c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
231913c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2320a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
232113c1d189SArnaud Ebalard 
232213c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
232313c1d189SArnaud Ebalard }
232413c1d189SArnaud Ebalard 
23258bafd730SAntony Antony static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma,
23268bafd730SAntony Antony 					  int with_encp)
23277deb2264SThomas Graf {
23287deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
232913c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
23308bafd730SAntony Antony 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
23317deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
23327deb2264SThomas Graf 	      + userpolicy_type_attrsize();
23337deb2264SThomas Graf }
23347deb2264SThomas Graf 
2335183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2336183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
23378bafd730SAntony Antony 			 const struct xfrm_selector *sel,
23388bafd730SAntony Antony 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
23395c79de6eSShinta Sugimoto {
2340183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
23415c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
23425c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
23431d1e34ddSDavid S. Miller 	int i, err;
23445c79de6eSShinta Sugimoto 
234579b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
234679b8b7f4SThomas Graf 	if (nlh == NULL)
234779b8b7f4SThomas Graf 		return -EMSGSIZE;
23485c79de6eSShinta Sugimoto 
23497b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
23505c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
23515c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
23525c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
23535c79de6eSShinta Sugimoto 	pol_id->dir = dir;
23545c79de6eSShinta Sugimoto 
23551d1e34ddSDavid S. Miller 	if (k != NULL) {
23561d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
23571d1e34ddSDavid S. Miller 		if (err)
23581d1e34ddSDavid S. Miller 			goto out_cancel;
23591d1e34ddSDavid S. Miller 	}
23608bafd730SAntony Antony 	if (encap) {
23618bafd730SAntony Antony 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
23628bafd730SAntony Antony 		if (err)
23638bafd730SAntony Antony 			goto out_cancel;
23648bafd730SAntony Antony 	}
23651d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
23661d1e34ddSDavid S. Miller 	if (err)
23671d1e34ddSDavid S. Miller 		goto out_cancel;
23685c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
23691d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
23701d1e34ddSDavid S. Miller 		if (err)
23711d1e34ddSDavid S. Miller 			goto out_cancel;
23725c79de6eSShinta Sugimoto 	}
23735c79de6eSShinta Sugimoto 
2374053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2375053c095aSJohannes Berg 	return 0;
23761d1e34ddSDavid S. Miller 
23771d1e34ddSDavid S. Miller out_cancel:
23789825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
23791d1e34ddSDavid S. Miller 	return err;
23805c79de6eSShinta Sugimoto }
23815c79de6eSShinta Sugimoto 
2382183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2383183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
23848bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
23858bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
23865c79de6eSShinta Sugimoto {
2387a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
23885c79de6eSShinta Sugimoto 	struct sk_buff *skb;
23895c79de6eSShinta Sugimoto 
23908bafd730SAntony Antony 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
23918bafd730SAntony Antony 			GFP_ATOMIC);
23925c79de6eSShinta Sugimoto 	if (skb == NULL)
23935c79de6eSShinta Sugimoto 		return -ENOMEM;
23945c79de6eSShinta Sugimoto 
23955c79de6eSShinta Sugimoto 	/* build migrate */
23968bafd730SAntony Antony 	if (build_migrate(skb, m, num_migrate, k, sel, encap, dir, type) < 0)
23975c79de6eSShinta Sugimoto 		BUG();
23985c79de6eSShinta Sugimoto 
239921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
24005c79de6eSShinta Sugimoto }
24015c79de6eSShinta Sugimoto #else
2402183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2403183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
24048bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
24058bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
24065c79de6eSShinta Sugimoto {
24075c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
24085c79de6eSShinta Sugimoto }
24095c79de6eSShinta Sugimoto #endif
2410d51d081dSJamal Hadi Salim 
2411a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2412492b558bSThomas Graf 
2413492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
241466f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2415492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2416492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2417492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2418492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2419492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2420492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2421980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
242253bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2423492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
242466f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
24256c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2426492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2427a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2428d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2429d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
243097a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
24315c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2432a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2433880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2434a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
24351da177e4SLinus Torvalds };
24361da177e4SLinus Torvalds 
2437492b558bSThomas Graf #undef XMSGSIZE
2438492b558bSThomas Graf 
2439cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2440c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2441c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2442c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2443c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
24441a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2445cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2446cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2447cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2448cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2449cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2450cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2451cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2452cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2453cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2454cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2455cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2456cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2457cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2458cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
245913c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
24606f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
246135d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2462d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2463a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2464d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2465870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2466d77e38e6SSteffen Klassert 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
2467077fbac4SLorenzo Colitti 	[XFRMA_OUTPUT_MARK]	= { .len = NLA_U32 },
2468cf5cb79fSThomas Graf };
2469cf5cb79fSThomas Graf 
2470880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2471880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2472880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2473880a6fabSChristophe Gouault };
2474880a6fabSChristophe Gouault 
247505600a79SMathias Krause static const struct xfrm_link {
24765424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
24771da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
24784c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2479880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2480880a6fabSChristophe Gouault 	int nla_max;
2481492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2482492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2483492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2484492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
24854c563f76STimo Teras 						   .dump = xfrm_dump_sa,
24864c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2487492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2488492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2489492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
24904c563f76STimo Teras 						   .dump = xfrm_dump_policy,
24914c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2492492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2493980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
249453bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2495492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2496492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
24976c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2498492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2499492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2500d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2501d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
25025c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
250328d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2504880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2505880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2506880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2507ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
25081da177e4SLinus Torvalds };
25091da177e4SLinus Torvalds 
25102d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
25112d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
25121da177e4SLinus Torvalds {
2513a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
251435a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
251505600a79SMathias Krause 	const struct xfrm_link *link;
2516a7bd9a45SThomas Graf 	int type, err;
25171da177e4SLinus Torvalds 
251874005991SFan Du #ifdef CONFIG_COMPAT
25192bf8c476SAndy Lutomirski 	if (in_compat_syscall())
252083e2d058SYi Zhao 		return -EOPNOTSUPP;
252174005991SFan Du #endif
252274005991SFan Du 
25231da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
25241da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
25251d00a4ebSThomas Graf 		return -EINVAL;
25261da177e4SLinus Torvalds 
25271da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
25281da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
253190f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
25321d00a4ebSThomas Graf 		return -EPERM;
25331da177e4SLinus Torvalds 
2534492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2535492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2536b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
25371da177e4SLinus Torvalds 		if (link->dump == NULL)
25381d00a4ebSThomas Graf 			return -EINVAL;
25391da177e4SLinus Torvalds 
254080d326faSPablo Neira Ayuso 		{
254180d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
254280d326faSPablo Neira Ayuso 				.dump = link->dump,
254380d326faSPablo Neira Ayuso 				.done = link->done,
254480d326faSPablo Neira Ayuso 			};
254580d326faSPablo Neira Ayuso 			return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
254680d326faSPablo Neira Ayuso 		}
25471da177e4SLinus Torvalds 	}
25481da177e4SLinus Torvalds 
2549880a6fabSChristophe Gouault 	err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2550880a6fabSChristophe Gouault 			  link->nla_max ? : XFRMA_MAX,
2551fe52145fSJohannes Berg 			  link->nla_pol ? : xfrma_policy, extack);
2552a7bd9a45SThomas Graf 	if (err < 0)
2553a7bd9a45SThomas Graf 		return err;
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds 	if (link->doit == NULL)
25561d00a4ebSThomas Graf 		return -EINVAL;
25571da177e4SLinus Torvalds 
25585424f32eSThomas Graf 	return link->doit(skb, nlh, attrs);
25591da177e4SLinus Torvalds }
25601da177e4SLinus Torvalds 
2561cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
25621da177e4SLinus Torvalds {
2563283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2564283bc9f3SFan Du 
2565283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2566cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2567283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
25681da177e4SLinus Torvalds }
25691da177e4SLinus Torvalds 
25707deb2264SThomas Graf static inline size_t xfrm_expire_msgsize(void)
25717deb2264SThomas Graf {
25726f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
25736f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
25747deb2264SThomas Graf }
25757deb2264SThomas Graf 
2576214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
25771da177e4SLinus Torvalds {
25781da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
25791da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
25801d1e34ddSDavid S. Miller 	int err;
25811da177e4SLinus Torvalds 
258215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
258379b8b7f4SThomas Graf 	if (nlh == NULL)
258479b8b7f4SThomas Graf 		return -EMSGSIZE;
25851da177e4SLinus Torvalds 
25867b67c857SThomas Graf 	ue = nlmsg_data(nlh);
25871da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2588d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
2589e3e5fc16SMathias Krause 	/* clear the padding bytes */
2590e3e5fc16SMathias Krause 	memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
25911da177e4SLinus Torvalds 
25921d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
25931d1e34ddSDavid S. Miller 	if (err)
25941d1e34ddSDavid S. Miller 		return err;
25956f26b61eSJamal Hadi Salim 
2596053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2597053c095aSJohannes Berg 	return 0;
25981da177e4SLinus Torvalds }
25991da177e4SLinus Torvalds 
2600214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
26011da177e4SLinus Torvalds {
2602fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
26031da177e4SLinus Torvalds 	struct sk_buff *skb;
26041da177e4SLinus Torvalds 
26057deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
26061da177e4SLinus Torvalds 	if (skb == NULL)
26071da177e4SLinus Torvalds 		return -ENOMEM;
26081da177e4SLinus Torvalds 
26096f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
26106f26b61eSJamal Hadi Salim 		kfree_skb(skb);
26116f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
26126f26b61eSJamal Hadi Salim 	}
26131da177e4SLinus Torvalds 
261421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
26151da177e4SLinus Torvalds }
26161da177e4SLinus Torvalds 
2617214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2618d51d081dSJamal Hadi Salim {
2619fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2620d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
2621d51d081dSJamal Hadi Salim 
2622d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2623d51d081dSJamal Hadi Salim 	if (skb == NULL)
2624d51d081dSJamal Hadi Salim 		return -ENOMEM;
2625d51d081dSJamal Hadi Salim 
2626d51d081dSJamal Hadi Salim 	if (build_aevent(skb, x, c) < 0)
2627d51d081dSJamal Hadi Salim 		BUG();
2628d51d081dSJamal Hadi Salim 
262921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2630d51d081dSJamal Hadi Salim }
2631d51d081dSJamal Hadi Salim 
2632214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
263326b15dadSJamal Hadi Salim {
26347067802eSAlexey Dobriyan 	struct net *net = c->net;
263526b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
263626b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
263726b15dadSJamal Hadi Salim 	struct sk_buff *skb;
26387deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
263926b15dadSJamal Hadi Salim 
26407deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
264126b15dadSJamal Hadi Salim 	if (skb == NULL)
264226b15dadSJamal Hadi Salim 		return -ENOMEM;
264326b15dadSJamal Hadi Salim 
264415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
264579b8b7f4SThomas Graf 	if (nlh == NULL) {
264679b8b7f4SThomas Graf 		kfree_skb(skb);
264779b8b7f4SThomas Graf 		return -EMSGSIZE;
264879b8b7f4SThomas Graf 	}
264926b15dadSJamal Hadi Salim 
26507b67c857SThomas Graf 	p = nlmsg_data(nlh);
2651bf08867fSHerbert Xu 	p->proto = c->data.proto;
265226b15dadSJamal Hadi Salim 
26539825069dSThomas Graf 	nlmsg_end(skb, nlh);
265426b15dadSJamal Hadi Salim 
265521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
265626b15dadSJamal Hadi Salim }
265726b15dadSJamal Hadi Salim 
26587deb2264SThomas Graf static inline size_t xfrm_sa_len(struct xfrm_state *x)
265926b15dadSJamal Hadi Salim {
26607deb2264SThomas Graf 	size_t l = 0;
26611a6509d9SHerbert Xu 	if (x->aead)
26621a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
26634447bb33SMartin Willi 	if (x->aalg) {
26644447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
26654447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
26664447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
26674447bb33SMartin Willi 	}
266826b15dadSJamal Hadi Salim 	if (x->ealg)
26690f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
267026b15dadSJamal Hadi Salim 	if (x->calg)
26717deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
267226b15dadSJamal Hadi Salim 	if (x->encap)
26737deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
267435d2856bSMartin Willi 	if (x->tfcpad)
267535d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
2676d8647b79SSteffen Klassert 	if (x->replay_esn)
2677d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2678f293a5e3Sdingzhi 	else
2679f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
268068325d3bSHerbert Xu 	if (x->security)
268168325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
268268325d3bSHerbert Xu 				    x->security->ctx_len);
268368325d3bSHerbert Xu 	if (x->coaddr)
268468325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
2685a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
2686a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
2687d77e38e6SSteffen Klassert 	if (x->xso.dev)
2688d77e38e6SSteffen Klassert 		 l += nla_total_size(sizeof(x->xso));
2689077fbac4SLorenzo Colitti 	if (x->props.output_mark)
2690077fbac4SLorenzo Colitti 		l += nla_total_size(sizeof(x->props.output_mark));
269168325d3bSHerbert Xu 
2692d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2693de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
269426b15dadSJamal Hadi Salim 
269526b15dadSJamal Hadi Salim 	return l;
269626b15dadSJamal Hadi Salim }
269726b15dadSJamal Hadi Salim 
2698214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
269926b15dadSJamal Hadi Salim {
2700fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
270126b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
27020603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
270326b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
270426b15dadSJamal Hadi Salim 	struct sk_buff *skb;
270526b15dadSJamal Hadi Salim 	int len = xfrm_sa_len(x);
27061d1e34ddSDavid S. Miller 	int headlen, err;
27070603eac0SHerbert Xu 
27080603eac0SHerbert Xu 	headlen = sizeof(*p);
27090603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
27107deb2264SThomas Graf 		len += nla_total_size(headlen);
27110603eac0SHerbert Xu 		headlen = sizeof(*id);
27126f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
27130603eac0SHerbert Xu 	}
27147deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
271526b15dadSJamal Hadi Salim 
27167deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
271726b15dadSJamal Hadi Salim 	if (skb == NULL)
271826b15dadSJamal Hadi Salim 		return -ENOMEM;
271926b15dadSJamal Hadi Salim 
272015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
27211d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
272279b8b7f4SThomas Graf 	if (nlh == NULL)
27231d1e34ddSDavid S. Miller 		goto out_free_skb;
272426b15dadSJamal Hadi Salim 
27257b67c857SThomas Graf 	p = nlmsg_data(nlh);
27260603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2727c0144beaSThomas Graf 		struct nlattr *attr;
2728c0144beaSThomas Graf 
27297b67c857SThomas Graf 		id = nlmsg_data(nlh);
273050329c8aSMathias Krause 		memset(id, 0, sizeof(*id));
27310603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
27320603eac0SHerbert Xu 		id->spi = x->id.spi;
27330603eac0SHerbert Xu 		id->family = x->props.family;
27340603eac0SHerbert Xu 		id->proto = x->id.proto;
27350603eac0SHerbert Xu 
2736c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
27371d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2738c0144beaSThomas Graf 		if (attr == NULL)
27391d1e34ddSDavid S. Miller 			goto out_free_skb;
2740c0144beaSThomas Graf 
2741c0144beaSThomas Graf 		p = nla_data(attr);
27420603eac0SHerbert Xu 	}
27431d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
27441d1e34ddSDavid S. Miller 	if (err)
27451d1e34ddSDavid S. Miller 		goto out_free_skb;
274626b15dadSJamal Hadi Salim 
27479825069dSThomas Graf 	nlmsg_end(skb, nlh);
274826b15dadSJamal Hadi Salim 
274921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
275026b15dadSJamal Hadi Salim 
27511d1e34ddSDavid S. Miller out_free_skb:
275226b15dadSJamal Hadi Salim 	kfree_skb(skb);
27531d1e34ddSDavid S. Miller 	return err;
275426b15dadSJamal Hadi Salim }
275526b15dadSJamal Hadi Salim 
2756214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
275726b15dadSJamal Hadi Salim {
275826b15dadSJamal Hadi Salim 
275926b15dadSJamal Hadi Salim 	switch (c->event) {
2760f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
276126b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2762d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2763d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2764f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2765f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2766f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
276726b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2768f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
276926b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
277026b15dadSJamal Hadi Salim 	default:
277162db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
277262db5cfdSstephen hemminger 		       c->event);
277326b15dadSJamal Hadi Salim 		break;
277426b15dadSJamal Hadi Salim 	}
277526b15dadSJamal Hadi Salim 
277626b15dadSJamal Hadi Salim 	return 0;
277726b15dadSJamal Hadi Salim 
277826b15dadSJamal Hadi Salim }
277926b15dadSJamal Hadi Salim 
27807deb2264SThomas Graf static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
27817deb2264SThomas Graf 					  struct xfrm_policy *xp)
27827deb2264SThomas Graf {
27837deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
27847deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
27856f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
27867deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
27877deb2264SThomas Graf 	       + userpolicy_type_attrsize();
27887deb2264SThomas Graf }
27897deb2264SThomas Graf 
27901da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
279165e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
27921da177e4SLinus Torvalds {
27931d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
27941da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
27951da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
27961d1e34ddSDavid S. Miller 	int err;
27971da177e4SLinus Torvalds 
279879b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
279979b8b7f4SThomas Graf 	if (nlh == NULL)
280079b8b7f4SThomas Graf 		return -EMSGSIZE;
28011da177e4SLinus Torvalds 
28027b67c857SThomas Graf 	ua = nlmsg_data(nlh);
28031da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
28041da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
28051da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
280665e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
28071da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
28081da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
28091da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
28101da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
28111da177e4SLinus Torvalds 
28121d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
28131d1e34ddSDavid S. Miller 	if (!err)
28141d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
28151d1e34ddSDavid S. Miller 	if (!err)
28161d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
28171d1e34ddSDavid S. Miller 	if (!err)
28181d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
28191d1e34ddSDavid S. Miller 	if (err) {
28201d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
28211d1e34ddSDavid S. Miller 		return err;
28221d1e34ddSDavid S. Miller 	}
28231da177e4SLinus Torvalds 
2824053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2825053c095aSJohannes Berg 	return 0;
28261da177e4SLinus Torvalds }
28271da177e4SLinus Torvalds 
28281da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
282965e0736bSFan Du 			     struct xfrm_policy *xp)
28301da177e4SLinus Torvalds {
2831a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
28321da177e4SLinus Torvalds 	struct sk_buff *skb;
28331da177e4SLinus Torvalds 
28347deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
28351da177e4SLinus Torvalds 	if (skb == NULL)
28361da177e4SLinus Torvalds 		return -ENOMEM;
28371da177e4SLinus Torvalds 
283865e0736bSFan Du 	if (build_acquire(skb, x, xt, xp) < 0)
28391da177e4SLinus Torvalds 		BUG();
28401da177e4SLinus Torvalds 
284121ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
28421da177e4SLinus Torvalds }
28431da177e4SLinus Torvalds 
28441da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
28451da177e4SLinus Torvalds  * or more templates.
28461da177e4SLinus Torvalds  */
2847cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
28481da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
28491da177e4SLinus Torvalds {
2850fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
28511da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
28521da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
28531da177e4SLinus Torvalds 	struct xfrm_policy *xp;
28541da177e4SLinus Torvalds 	int nr;
28551da177e4SLinus Torvalds 
2856cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
28571da177e4SLinus Torvalds 	case AF_INET:
28581da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
28591da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
28601da177e4SLinus Torvalds 			return NULL;
28611da177e4SLinus Torvalds 		}
28621da177e4SLinus Torvalds 		break;
2863dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
28641da177e4SLinus Torvalds 	case AF_INET6:
28651da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
28661da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
28671da177e4SLinus Torvalds 			return NULL;
28681da177e4SLinus Torvalds 		}
28691da177e4SLinus Torvalds 		break;
28701da177e4SLinus Torvalds #endif
28711da177e4SLinus Torvalds 	default:
28721da177e4SLinus Torvalds 		*dir = -EINVAL;
28731da177e4SLinus Torvalds 		return NULL;
28741da177e4SLinus Torvalds 	}
28751da177e4SLinus Torvalds 
28761da177e4SLinus Torvalds 	*dir = -EINVAL;
28771da177e4SLinus Torvalds 
28781da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
28791da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
28801da177e4SLinus Torvalds 		return NULL;
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
2883b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
28841da177e4SLinus Torvalds 		return NULL;
28851da177e4SLinus Torvalds 
2886a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
2887a4f1bac6SHerbert Xu 		return NULL;
2888a4f1bac6SHerbert Xu 
28892f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
28901da177e4SLinus Torvalds 	if (xp == NULL) {
28911da177e4SLinus Torvalds 		*dir = -ENOBUFS;
28921da177e4SLinus Torvalds 		return NULL;
28931da177e4SLinus Torvalds 	}
28941da177e4SLinus Torvalds 
28951da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
2896f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
28971da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
28981da177e4SLinus Torvalds 
28991da177e4SLinus Torvalds 	*dir = p->dir;
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 	return xp;
29021da177e4SLinus Torvalds }
29031da177e4SLinus Torvalds 
29047deb2264SThomas Graf static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
29057deb2264SThomas Graf {
29067deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
29077deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
29087deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2909295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
29107deb2264SThomas Graf 	       + userpolicy_type_attrsize();
29117deb2264SThomas Graf }
29127deb2264SThomas Graf 
29131da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2914214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
29151da177e4SLinus Torvalds {
29161da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
2917d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
29181d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
29191d1e34ddSDavid S. Miller 	int err;
29201da177e4SLinus Torvalds 
292115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
292279b8b7f4SThomas Graf 	if (nlh == NULL)
292379b8b7f4SThomas Graf 		return -EMSGSIZE;
29241da177e4SLinus Torvalds 
29257b67c857SThomas Graf 	upe = nlmsg_data(nlh);
29261da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
29271d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
29281d1e34ddSDavid S. Miller 	if (!err)
29291d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
29301d1e34ddSDavid S. Miller 	if (!err)
29311d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
29321d1e34ddSDavid S. Miller 	if (!err)
29331d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
29341d1e34ddSDavid S. Miller 	if (err) {
29351d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
29361d1e34ddSDavid S. Miller 		return err;
29371d1e34ddSDavid S. Miller 	}
29381da177e4SLinus Torvalds 	upe->hard = !!hard;
29391da177e4SLinus Torvalds 
2940053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2941053c095aSJohannes Berg 	return 0;
29421da177e4SLinus Torvalds }
29431da177e4SLinus Torvalds 
2944214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
29451da177e4SLinus Torvalds {
2946fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
29471da177e4SLinus Torvalds 	struct sk_buff *skb;
29481da177e4SLinus Torvalds 
29497deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
29501da177e4SLinus Torvalds 	if (skb == NULL)
29511da177e4SLinus Torvalds 		return -ENOMEM;
29521da177e4SLinus Torvalds 
2953d51d081dSJamal Hadi Salim 	if (build_polexpire(skb, xp, dir, c) < 0)
29541da177e4SLinus Torvalds 		BUG();
29551da177e4SLinus Torvalds 
295621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
29571da177e4SLinus Torvalds }
29581da177e4SLinus Torvalds 
2959214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
296026b15dadSJamal Hadi Salim {
29611d1e34ddSDavid S. Miller 	int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2962fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
296326b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
29640603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
296526b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
296626b15dadSJamal Hadi Salim 	struct sk_buff *skb;
29671d1e34ddSDavid S. Miller 	int headlen, err;
29680603eac0SHerbert Xu 
29690603eac0SHerbert Xu 	headlen = sizeof(*p);
29700603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
29717deb2264SThomas Graf 		len += nla_total_size(headlen);
29720603eac0SHerbert Xu 		headlen = sizeof(*id);
29730603eac0SHerbert Xu 	}
2974cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
2975295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
29767deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
297726b15dadSJamal Hadi Salim 
29787deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
297926b15dadSJamal Hadi Salim 	if (skb == NULL)
298026b15dadSJamal Hadi Salim 		return -ENOMEM;
298126b15dadSJamal Hadi Salim 
298215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
29831d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
298479b8b7f4SThomas Graf 	if (nlh == NULL)
29851d1e34ddSDavid S. Miller 		goto out_free_skb;
298626b15dadSJamal Hadi Salim 
29877b67c857SThomas Graf 	p = nlmsg_data(nlh);
29880603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
2989c0144beaSThomas Graf 		struct nlattr *attr;
2990c0144beaSThomas Graf 
29917b67c857SThomas Graf 		id = nlmsg_data(nlh);
29920603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
29930603eac0SHerbert Xu 		id->dir = dir;
29940603eac0SHerbert Xu 		if (c->data.byid)
29950603eac0SHerbert Xu 			id->index = xp->index;
29960603eac0SHerbert Xu 		else
29970603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
29980603eac0SHerbert Xu 
2999c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
30001d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3001c0144beaSThomas Graf 		if (attr == NULL)
30021d1e34ddSDavid S. Miller 			goto out_free_skb;
3003c0144beaSThomas Graf 
3004c0144beaSThomas Graf 		p = nla_data(attr);
30050603eac0SHerbert Xu 	}
300626b15dadSJamal Hadi Salim 
300726b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
30081d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
30091d1e34ddSDavid S. Miller 	if (!err)
30101d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
30111d1e34ddSDavid S. Miller 	if (!err)
30121d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
30131d1e34ddSDavid S. Miller 	if (err)
30141d1e34ddSDavid S. Miller 		goto out_free_skb;
3015295fae56SJamal Hadi Salim 
30169825069dSThomas Graf 	nlmsg_end(skb, nlh);
301726b15dadSJamal Hadi Salim 
301821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
301926b15dadSJamal Hadi Salim 
30201d1e34ddSDavid S. Miller out_free_skb:
302126b15dadSJamal Hadi Salim 	kfree_skb(skb);
30221d1e34ddSDavid S. Miller 	return err;
302326b15dadSJamal Hadi Salim }
302426b15dadSJamal Hadi Salim 
3025214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
302626b15dadSJamal Hadi Salim {
30277067802eSAlexey Dobriyan 	struct net *net = c->net;
302826b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
302926b15dadSJamal Hadi Salim 	struct sk_buff *skb;
30301d1e34ddSDavid S. Miller 	int err;
303126b15dadSJamal Hadi Salim 
30327deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
303326b15dadSJamal Hadi Salim 	if (skb == NULL)
303426b15dadSJamal Hadi Salim 		return -ENOMEM;
303526b15dadSJamal Hadi Salim 
303615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
30371d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
303879b8b7f4SThomas Graf 	if (nlh == NULL)
30391d1e34ddSDavid S. Miller 		goto out_free_skb;
30401d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
30411d1e34ddSDavid S. Miller 	if (err)
30421d1e34ddSDavid S. Miller 		goto out_free_skb;
304326b15dadSJamal Hadi Salim 
30449825069dSThomas Graf 	nlmsg_end(skb, nlh);
304526b15dadSJamal Hadi Salim 
304621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
304726b15dadSJamal Hadi Salim 
30481d1e34ddSDavid S. Miller out_free_skb:
304926b15dadSJamal Hadi Salim 	kfree_skb(skb);
30501d1e34ddSDavid S. Miller 	return err;
305126b15dadSJamal Hadi Salim }
305226b15dadSJamal Hadi Salim 
3053214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
305426b15dadSJamal Hadi Salim {
305526b15dadSJamal Hadi Salim 
305626b15dadSJamal Hadi Salim 	switch (c->event) {
3057f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
3058f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
3059f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
306026b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
3061f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
306226b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
3063f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
306426b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
306526b15dadSJamal Hadi Salim 	default:
306662db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
306762db5cfdSstephen hemminger 		       c->event);
306826b15dadSJamal Hadi Salim 	}
306926b15dadSJamal Hadi Salim 
307026b15dadSJamal Hadi Salim 	return 0;
307126b15dadSJamal Hadi Salim 
307226b15dadSJamal Hadi Salim }
307326b15dadSJamal Hadi Salim 
30747deb2264SThomas Graf static inline size_t xfrm_report_msgsize(void)
30757deb2264SThomas Graf {
30767deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
30777deb2264SThomas Graf }
30787deb2264SThomas Graf 
307997a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
308097a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
308197a64b45SMasahide NAKAMURA {
308297a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
308397a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
308497a64b45SMasahide NAKAMURA 
308579b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
308679b8b7f4SThomas Graf 	if (nlh == NULL)
308779b8b7f4SThomas Graf 		return -EMSGSIZE;
308897a64b45SMasahide NAKAMURA 
30897b67c857SThomas Graf 	ur = nlmsg_data(nlh);
309097a64b45SMasahide NAKAMURA 	ur->proto = proto;
309197a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
309297a64b45SMasahide NAKAMURA 
30931d1e34ddSDavid S. Miller 	if (addr) {
30941d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
30951d1e34ddSDavid S. Miller 		if (err) {
30969825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
30971d1e34ddSDavid S. Miller 			return err;
30981d1e34ddSDavid S. Miller 		}
30991d1e34ddSDavid S. Miller 	}
3100053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3101053c095aSJohannes Berg 	return 0;
310297a64b45SMasahide NAKAMURA }
310397a64b45SMasahide NAKAMURA 
3104db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3105db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
310697a64b45SMasahide NAKAMURA {
310797a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
310897a64b45SMasahide NAKAMURA 
31097deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
311097a64b45SMasahide NAKAMURA 	if (skb == NULL)
311197a64b45SMasahide NAKAMURA 		return -ENOMEM;
311297a64b45SMasahide NAKAMURA 
311397a64b45SMasahide NAKAMURA 	if (build_report(skb, proto, sel, addr) < 0)
311497a64b45SMasahide NAKAMURA 		BUG();
311597a64b45SMasahide NAKAMURA 
311621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
311797a64b45SMasahide NAKAMURA }
311897a64b45SMasahide NAKAMURA 
31193a2dfbe8SMartin Willi static inline size_t xfrm_mapping_msgsize(void)
31203a2dfbe8SMartin Willi {
31213a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
31223a2dfbe8SMartin Willi }
31233a2dfbe8SMartin Willi 
31243a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
31253a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
31263a2dfbe8SMartin Willi {
31273a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
31283a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
31293a2dfbe8SMartin Willi 
31303a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
31313a2dfbe8SMartin Willi 	if (nlh == NULL)
31323a2dfbe8SMartin Willi 		return -EMSGSIZE;
31333a2dfbe8SMartin Willi 
31343a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
31353a2dfbe8SMartin Willi 
31363a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
31373a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
31383a2dfbe8SMartin Willi 	um->id.family = x->props.family;
31393a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
31403a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
31413a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
31423a2dfbe8SMartin Willi 	um->new_sport = new_sport;
31433a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
31443a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
31453a2dfbe8SMartin Willi 
3146053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3147053c095aSJohannes Berg 	return 0;
31483a2dfbe8SMartin Willi }
31493a2dfbe8SMartin Willi 
31503a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
31513a2dfbe8SMartin Willi 			     __be16 sport)
31523a2dfbe8SMartin Willi {
3153a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
31543a2dfbe8SMartin Willi 	struct sk_buff *skb;
31553a2dfbe8SMartin Willi 
31563a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
31573a2dfbe8SMartin Willi 		return -EINVAL;
31583a2dfbe8SMartin Willi 
31593a2dfbe8SMartin Willi 	if (!x->encap)
31603a2dfbe8SMartin Willi 		return -EINVAL;
31613a2dfbe8SMartin Willi 
31623a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
31633a2dfbe8SMartin Willi 	if (skb == NULL)
31643a2dfbe8SMartin Willi 		return -ENOMEM;
31653a2dfbe8SMartin Willi 
31663a2dfbe8SMartin Willi 	if (build_mapping(skb, x, ipaddr, sport) < 0)
31673a2dfbe8SMartin Willi 		BUG();
31683a2dfbe8SMartin Willi 
316921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
31703a2dfbe8SMartin Willi }
31713a2dfbe8SMartin Willi 
31720f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
31730f24558eSHoria Geanta {
31740f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
31750f24558eSHoria Geanta }
31760f24558eSHoria Geanta 
31771da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
31781da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
31791da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
31801da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
31811da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
318297a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
31835c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
31843a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
31850f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
31861da177e4SLinus Torvalds };
31871da177e4SLinus Torvalds 
3188a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
31891da177e4SLinus Torvalds {
3190be33690dSPatrick McHardy 	struct sock *nlsk;
3191a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3192a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3193a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3194a31f2d17SPablo Neira Ayuso 	};
3195be33690dSPatrick McHardy 
31969f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3197be33690dSPatrick McHardy 	if (nlsk == NULL)
31981da177e4SLinus Torvalds 		return -ENOMEM;
3199d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3200cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
32011da177e4SLinus Torvalds 	return 0;
32021da177e4SLinus Torvalds }
32031da177e4SLinus Torvalds 
3204d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3205a6483b79SAlexey Dobriyan {
3206d79d792eSEric W. Biederman 	struct net *net;
3207d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3208a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3209d79d792eSEric W. Biederman 	synchronize_net();
3210d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3211d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3212a6483b79SAlexey Dobriyan }
3213a6483b79SAlexey Dobriyan 
3214a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3215a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
3216d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3217a6483b79SAlexey Dobriyan };
3218a6483b79SAlexey Dobriyan 
3219a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3220a6483b79SAlexey Dobriyan {
3221a6483b79SAlexey Dobriyan 	int rv;
3222a6483b79SAlexey Dobriyan 
3223a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3224a6483b79SAlexey Dobriyan 
3225a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3226a6483b79SAlexey Dobriyan 	if (rv < 0)
3227a6483b79SAlexey Dobriyan 		return rv;
3228a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3229a6483b79SAlexey Dobriyan 	if (rv < 0)
3230a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3231a6483b79SAlexey Dobriyan 	return rv;
3232a6483b79SAlexey Dobriyan }
3233a6483b79SAlexey Dobriyan 
32341da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
32351da177e4SLinus Torvalds {
32361da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3237a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
32381da177e4SLinus Torvalds }
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds module_init(xfrm_user_init);
32411da177e4SLinus Torvalds module_exit(xfrm_user_exit);
32421da177e4SLinus Torvalds MODULE_LICENSE("GPL");
32434fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3244f8cd5488SJamal Hadi Salim 
3245