xref: /linux/net/xfrm/xfrm_user.c (revision 9b42c1f179a614e11893ae4619f0304a38f481ae)
11da177e4SLinus Torvalds /* xfrm_user.c: User interface to configure xfrm engine.
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Changes:
61da177e4SLinus Torvalds  *	Mitsuru KANDA @USAGI
71da177e4SLinus Torvalds  * 	Kazunori MIYAZAWA @USAGI
81da177e4SLinus Torvalds  * 	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
91da177e4SLinus Torvalds  * 		IPv6 support
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
139409f38aSHerbert Xu #include <linux/crypto.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/types.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/socket.h>
191da177e4SLinus Torvalds #include <linux/string.h>
201da177e4SLinus Torvalds #include <linux/net.h>
211da177e4SLinus Torvalds #include <linux/skbuff.h>
221da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
231da177e4SLinus Torvalds #include <linux/ipsec.h>
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/security.h>
261da177e4SLinus Torvalds #include <net/sock.h>
271da177e4SLinus Torvalds #include <net/xfrm.h>
2888fc2c84SThomas Graf #include <net/netlink.h>
29fa6dd8a2SNicolas Dichtel #include <net/ah.h>
307c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
31dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
32e23c7194SMasahide NAKAMURA #include <linux/in6.h>
33e23c7194SMasahide NAKAMURA #endif
34e33d4f13SSowmini Varadhan #include <asm/unaligned.h>
351da177e4SLinus Torvalds 
365424f32eSThomas Graf static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
371da177e4SLinus Torvalds {
385424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
391da177e4SLinus Torvalds 	struct xfrm_algo *algp;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	if (!rt)
421da177e4SLinus Torvalds 		return 0;
431da177e4SLinus Torvalds 
445424f32eSThomas Graf 	algp = nla_data(rt);
4506cd22f8SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_alg_len(algp))
4631c26852SHerbert Xu 		return -EINVAL;
4731c26852SHerbert Xu 
481da177e4SLinus Torvalds 	switch (type) {
491da177e4SLinus Torvalds 	case XFRMA_ALG_AUTH:
501da177e4SLinus Torvalds 	case XFRMA_ALG_CRYPT:
511da177e4SLinus Torvalds 	case XFRMA_ALG_COMP:
521da177e4SLinus Torvalds 		break;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	default:
551da177e4SLinus Torvalds 		return -EINVAL;
563ff50b79SStephen Hemminger 	}
571da177e4SLinus Torvalds 
58633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
591da177e4SLinus Torvalds 	return 0;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
624447bb33SMartin Willi static int verify_auth_trunc(struct nlattr **attrs)
634447bb33SMartin Willi {
644447bb33SMartin Willi 	struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
654447bb33SMartin Willi 	struct xfrm_algo_auth *algp;
664447bb33SMartin Willi 
674447bb33SMartin Willi 	if (!rt)
684447bb33SMartin Willi 		return 0;
694447bb33SMartin Willi 
704447bb33SMartin Willi 	algp = nla_data(rt);
711bd963a7SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
724447bb33SMartin Willi 		return -EINVAL;
734447bb33SMartin Willi 
74633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
754447bb33SMartin Willi 	return 0;
764447bb33SMartin Willi }
774447bb33SMartin Willi 
781a6509d9SHerbert Xu static int verify_aead(struct nlattr **attrs)
791a6509d9SHerbert Xu {
801a6509d9SHerbert Xu 	struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
811a6509d9SHerbert Xu 	struct xfrm_algo_aead *algp;
821a6509d9SHerbert Xu 
831a6509d9SHerbert Xu 	if (!rt)
841a6509d9SHerbert Xu 		return 0;
851a6509d9SHerbert Xu 
861a6509d9SHerbert Xu 	algp = nla_data(rt);
87373b8eebSAlexey Dobriyan 	if (nla_len(rt) < (int)aead_len(algp))
881a6509d9SHerbert Xu 		return -EINVAL;
891a6509d9SHerbert Xu 
90633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
911a6509d9SHerbert Xu 	return 0;
921a6509d9SHerbert Xu }
931a6509d9SHerbert Xu 
945424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
95eb2971b6SMasahide NAKAMURA 			   xfrm_address_t **addrp)
96eb2971b6SMasahide NAKAMURA {
975424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
98eb2971b6SMasahide NAKAMURA 
99cf5cb79fSThomas Graf 	if (rt && addrp)
1005424f32eSThomas Graf 		*addrp = nla_data(rt);
101eb2971b6SMasahide NAKAMURA }
102df71837dSTrent Jaeger 
1035424f32eSThomas Graf static inline int verify_sec_ctx_len(struct nlattr **attrs)
104df71837dSTrent Jaeger {
1055424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
106df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
107df71837dSTrent Jaeger 
108df71837dSTrent Jaeger 	if (!rt)
109df71837dSTrent Jaeger 		return 0;
110df71837dSTrent Jaeger 
1115424f32eSThomas Graf 	uctx = nla_data(rt);
112cf5cb79fSThomas Graf 	if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
113df71837dSTrent Jaeger 		return -EINVAL;
114df71837dSTrent Jaeger 
115df71837dSTrent Jaeger 	return 0;
116df71837dSTrent Jaeger }
117df71837dSTrent Jaeger 
118d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p,
119d8647b79SSteffen Klassert 				struct nlattr **attrs)
120d8647b79SSteffen Klassert {
121d8647b79SSteffen Klassert 	struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
122ecd79187SMathias Krause 	struct xfrm_replay_state_esn *rs;
123d8647b79SSteffen Klassert 
124ecd79187SMathias Krause 	if (!rt)
125d97ca5d7SFlorian Westphal 		return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
1267833aa05SSteffen Klassert 
127ecd79187SMathias Krause 	rs = nla_data(rt);
128ecd79187SMathias Krause 
129ecd79187SMathias Krause 	if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
130ecd79187SMathias Krause 		return -EINVAL;
131ecd79187SMathias Krause 
1325e708e47SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
133ecd79187SMathias Krause 	    nla_len(rt) != sizeof(*rs))
134ecd79187SMathias Krause 		return -EINVAL;
135d8647b79SSteffen Klassert 
13601714109SFan Du 	/* As only ESP and AH support ESN feature. */
13701714109SFan Du 	if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
13802aadf72SSteffen Klassert 		return -EINVAL;
13902aadf72SSteffen Klassert 
140d8647b79SSteffen Klassert 	if (p->replay_window != 0)
141d8647b79SSteffen Klassert 		return -EINVAL;
142d8647b79SSteffen Klassert 
143d8647b79SSteffen Klassert 	return 0;
144d8647b79SSteffen Klassert }
145df71837dSTrent Jaeger 
1461da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p,
1475424f32eSThomas Graf 			     struct nlattr **attrs)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	int err;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	err = -EINVAL;
1521da177e4SLinus Torvalds 	switch (p->family) {
1531da177e4SLinus Torvalds 	case AF_INET:
1541da177e4SLinus Torvalds 		break;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	case AF_INET6:
157dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1581da177e4SLinus Torvalds 		break;
1591da177e4SLinus Torvalds #else
1601da177e4SLinus Torvalds 		err = -EAFNOSUPPORT;
1611da177e4SLinus Torvalds 		goto out;
1621da177e4SLinus Torvalds #endif
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	default:
1651da177e4SLinus Torvalds 		goto out;
1663ff50b79SStephen Hemminger 	}
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	err = -EINVAL;
1691da177e4SLinus Torvalds 	switch (p->id.proto) {
1701da177e4SLinus Torvalds 	case IPPROTO_AH:
1714447bb33SMartin Willi 		if ((!attrs[XFRMA_ALG_AUTH]	&&
1724447bb33SMartin Willi 		     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
1731a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
17435a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
17535d2856bSMartin Willi 		    attrs[XFRMA_ALG_COMP]	||
176a0e5ef53STobias Brunner 		    attrs[XFRMA_TFCPAD])
1771da177e4SLinus Torvalds 			goto out;
1781da177e4SLinus Torvalds 		break;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	case IPPROTO_ESP:
1811a6509d9SHerbert Xu 		if (attrs[XFRMA_ALG_COMP])
1821a6509d9SHerbert Xu 			goto out;
1831a6509d9SHerbert Xu 		if (!attrs[XFRMA_ALG_AUTH] &&
1844447bb33SMartin Willi 		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
1851a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_CRYPT] &&
1861a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_AEAD])
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])
1921da177e4SLinus Torvalds 			goto out;
19335d2856bSMartin Willi 		if (attrs[XFRMA_TFCPAD] &&
19435d2856bSMartin Willi 		    p->mode != XFRM_MODE_TUNNEL)
19535d2856bSMartin Willi 			goto out;
1961da177e4SLinus Torvalds 		break;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	case IPPROTO_COMP:
19935a7aa08SThomas Graf 		if (!attrs[XFRMA_ALG_COMP]	||
2001a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
20135a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2024447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
20335d2856bSMartin Willi 		    attrs[XFRMA_ALG_CRYPT]	||
204a0e5ef53STobias Brunner 		    attrs[XFRMA_TFCPAD]		||
205a0e5ef53STobias Brunner 		    (ntohl(p->id.spi) >= 0x10000))
2061da177e4SLinus Torvalds 			goto out;
2071da177e4SLinus Torvalds 		break;
2081da177e4SLinus Torvalds 
209dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
210e23c7194SMasahide NAKAMURA 	case IPPROTO_DSTOPTS:
211e23c7194SMasahide NAKAMURA 	case IPPROTO_ROUTING:
21235a7aa08SThomas Graf 		if (attrs[XFRMA_ALG_COMP]	||
21335a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2144447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
2151a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
21635a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
21735a7aa08SThomas Graf 		    attrs[XFRMA_ENCAP]		||
21835a7aa08SThomas Graf 		    attrs[XFRMA_SEC_CTX]	||
21935d2856bSMartin Willi 		    attrs[XFRMA_TFCPAD]		||
22035a7aa08SThomas Graf 		    !attrs[XFRMA_COADDR])
221e23c7194SMasahide NAKAMURA 			goto out;
222e23c7194SMasahide NAKAMURA 		break;
223e23c7194SMasahide NAKAMURA #endif
224e23c7194SMasahide NAKAMURA 
2251da177e4SLinus Torvalds 	default:
2261da177e4SLinus Torvalds 		goto out;
2273ff50b79SStephen Hemminger 	}
2281da177e4SLinus Torvalds 
2291a6509d9SHerbert Xu 	if ((err = verify_aead(attrs)))
2301a6509d9SHerbert Xu 		goto out;
2314447bb33SMartin Willi 	if ((err = verify_auth_trunc(attrs)))
2324447bb33SMartin Willi 		goto out;
23335a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
2341da177e4SLinus Torvalds 		goto out;
23535a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
2361da177e4SLinus Torvalds 		goto out;
23735a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
2381da177e4SLinus Torvalds 		goto out;
23935a7aa08SThomas Graf 	if ((err = verify_sec_ctx_len(attrs)))
240df71837dSTrent Jaeger 		goto out;
241d8647b79SSteffen Klassert 	if ((err = verify_replay(p, attrs)))
242d8647b79SSteffen Klassert 		goto out;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	err = -EINVAL;
2451da177e4SLinus Torvalds 	switch (p->mode) {
2467e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TRANSPORT:
2477e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TUNNEL:
248060f02a3SNoriaki TAKAMIYA 	case XFRM_MODE_ROUTEOPTIMIZATION:
2490a69452cSDiego Beltrami 	case XFRM_MODE_BEET:
2501da177e4SLinus Torvalds 		break;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	default:
2531da177e4SLinus Torvalds 		goto out;
2543ff50b79SStephen Hemminger 	}
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	err = 0;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds out:
2591da177e4SLinus Torvalds 	return err;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
2636f2f19edSDavid S. Miller 			   struct xfrm_algo_desc *(*get_byname)(const char *, int),
2645424f32eSThomas Graf 			   struct nlattr *rta)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	struct xfrm_algo *p, *ualg;
2671da177e4SLinus Torvalds 	struct xfrm_algo_desc *algo;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	if (!rta)
2701da177e4SLinus Torvalds 		return 0;
2711da177e4SLinus Torvalds 
2725424f32eSThomas Graf 	ualg = nla_data(rta);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	algo = get_byname(ualg->alg_name, 1);
2751da177e4SLinus Torvalds 	if (!algo)
2761da177e4SLinus Torvalds 		return -ENOSYS;
2771da177e4SLinus Torvalds 	*props = algo->desc.sadb_alg_id;
2781da177e4SLinus Torvalds 
2790f99be0dSEric Dumazet 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
2801da177e4SLinus Torvalds 	if (!p)
2811da177e4SLinus Torvalds 		return -ENOMEM;
2821da177e4SLinus Torvalds 
28304ff1260SHerbert Xu 	strcpy(p->alg_name, algo->name);
2841da177e4SLinus Torvalds 	*algpp = p;
2851da177e4SLinus Torvalds 	return 0;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
28869b0137fSHerbert Xu static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
28969b0137fSHerbert Xu {
29069b0137fSHerbert Xu 	struct xfrm_algo *p, *ualg;
29169b0137fSHerbert Xu 	struct xfrm_algo_desc *algo;
29269b0137fSHerbert Xu 
29369b0137fSHerbert Xu 	if (!rta)
29469b0137fSHerbert Xu 		return 0;
29569b0137fSHerbert Xu 
29669b0137fSHerbert Xu 	ualg = nla_data(rta);
29769b0137fSHerbert Xu 
29869b0137fSHerbert Xu 	algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
29969b0137fSHerbert Xu 	if (!algo)
30069b0137fSHerbert Xu 		return -ENOSYS;
30169b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
30269b0137fSHerbert Xu 
30369b0137fSHerbert Xu 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
30469b0137fSHerbert Xu 	if (!p)
30569b0137fSHerbert Xu 		return -ENOMEM;
30669b0137fSHerbert Xu 
30769b0137fSHerbert Xu 	strcpy(p->alg_name, algo->name);
30869b0137fSHerbert Xu 	x->ealg = p;
30969b0137fSHerbert Xu 	x->geniv = algo->uinfo.encr.geniv;
31069b0137fSHerbert Xu 	return 0;
31169b0137fSHerbert Xu }
31269b0137fSHerbert Xu 
3134447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
3144447bb33SMartin Willi 		       struct nlattr *rta)
3154447bb33SMartin Willi {
3164447bb33SMartin Willi 	struct xfrm_algo *ualg;
3174447bb33SMartin Willi 	struct xfrm_algo_auth *p;
3184447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3194447bb33SMartin Willi 
3204447bb33SMartin Willi 	if (!rta)
3214447bb33SMartin Willi 		return 0;
3224447bb33SMartin Willi 
3234447bb33SMartin Willi 	ualg = nla_data(rta);
3244447bb33SMartin Willi 
3254447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3264447bb33SMartin Willi 	if (!algo)
3274447bb33SMartin Willi 		return -ENOSYS;
3284447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3294447bb33SMartin Willi 
3304447bb33SMartin Willi 	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
3314447bb33SMartin Willi 	if (!p)
3324447bb33SMartin Willi 		return -ENOMEM;
3334447bb33SMartin Willi 
3344447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3354447bb33SMartin Willi 	p->alg_key_len = ualg->alg_key_len;
3364447bb33SMartin Willi 	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3374447bb33SMartin Willi 	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
3384447bb33SMartin Willi 
3394447bb33SMartin Willi 	*algpp = p;
3404447bb33SMartin Willi 	return 0;
3414447bb33SMartin Willi }
3424447bb33SMartin Willi 
3434447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
3444447bb33SMartin Willi 			     struct nlattr *rta)
3454447bb33SMartin Willi {
3464447bb33SMartin Willi 	struct xfrm_algo_auth *p, *ualg;
3474447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3484447bb33SMartin Willi 
3494447bb33SMartin Willi 	if (!rta)
3504447bb33SMartin Willi 		return 0;
3514447bb33SMartin Willi 
3524447bb33SMartin Willi 	ualg = nla_data(rta);
3534447bb33SMartin Willi 
3544447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3554447bb33SMartin Willi 	if (!algo)
3564447bb33SMartin Willi 		return -ENOSYS;
357689f1c9dSHerbert Xu 	if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
3584447bb33SMartin Willi 		return -EINVAL;
3594447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3604447bb33SMartin Willi 
3614447bb33SMartin Willi 	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
3624447bb33SMartin Willi 	if (!p)
3634447bb33SMartin Willi 		return -ENOMEM;
3644447bb33SMartin Willi 
3654447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3664447bb33SMartin Willi 	if (!p->alg_trunc_len)
3674447bb33SMartin Willi 		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3684447bb33SMartin Willi 
3694447bb33SMartin Willi 	*algpp = p;
3704447bb33SMartin Willi 	return 0;
3714447bb33SMartin Willi }
3724447bb33SMartin Willi 
37369b0137fSHerbert Xu static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
3741a6509d9SHerbert Xu {
3751a6509d9SHerbert Xu 	struct xfrm_algo_aead *p, *ualg;
3761a6509d9SHerbert Xu 	struct xfrm_algo_desc *algo;
3771a6509d9SHerbert Xu 
3781a6509d9SHerbert Xu 	if (!rta)
3791a6509d9SHerbert Xu 		return 0;
3801a6509d9SHerbert Xu 
3811a6509d9SHerbert Xu 	ualg = nla_data(rta);
3821a6509d9SHerbert Xu 
3831a6509d9SHerbert Xu 	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
3841a6509d9SHerbert Xu 	if (!algo)
3851a6509d9SHerbert Xu 		return -ENOSYS;
38669b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
3871a6509d9SHerbert Xu 
3881a6509d9SHerbert Xu 	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
3891a6509d9SHerbert Xu 	if (!p)
3901a6509d9SHerbert Xu 		return -ENOMEM;
3911a6509d9SHerbert Xu 
3921a6509d9SHerbert Xu 	strcpy(p->alg_name, algo->name);
39369b0137fSHerbert Xu 	x->aead = p;
39469b0137fSHerbert Xu 	x->geniv = algo->uinfo.aead.geniv;
3951a6509d9SHerbert Xu 	return 0;
3961a6509d9SHerbert Xu }
3971a6509d9SHerbert Xu 
398e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
399e2b19125SSteffen Klassert 					 struct nlattr *rp)
400e2b19125SSteffen Klassert {
401e2b19125SSteffen Klassert 	struct xfrm_replay_state_esn *up;
4025e708e47SAlexey Dobriyan 	unsigned int ulen;
403e2b19125SSteffen Klassert 
404e2b19125SSteffen Klassert 	if (!replay_esn || !rp)
405e2b19125SSteffen Klassert 		return 0;
406e2b19125SSteffen Klassert 
407e2b19125SSteffen Klassert 	up = nla_data(rp);
408ecd79187SMathias Krause 	ulen = xfrm_replay_state_esn_len(up);
409e2b19125SSteffen Klassert 
410f843ee6dSAndy Whitcroft 	/* Check the overall length and the internal bitmap length to avoid
411f843ee6dSAndy Whitcroft 	 * potential overflow. */
4125e708e47SAlexey Dobriyan 	if (nla_len(rp) < (int)ulen ||
413f843ee6dSAndy Whitcroft 	    xfrm_replay_state_esn_len(replay_esn) != ulen ||
414f843ee6dSAndy Whitcroft 	    replay_esn->bmp_len != up->bmp_len)
415e2b19125SSteffen Klassert 		return -EINVAL;
416e2b19125SSteffen Klassert 
417677e806dSAndy Whitcroft 	if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
418677e806dSAndy Whitcroft 		return -EINVAL;
419677e806dSAndy Whitcroft 
420e2b19125SSteffen Klassert 	return 0;
421e2b19125SSteffen Klassert }
422e2b19125SSteffen Klassert 
423d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
424d8647b79SSteffen Klassert 				       struct xfrm_replay_state_esn **preplay_esn,
425d8647b79SSteffen Klassert 				       struct nlattr *rta)
426d8647b79SSteffen Klassert {
427d8647b79SSteffen Klassert 	struct xfrm_replay_state_esn *p, *pp, *up;
4285e708e47SAlexey Dobriyan 	unsigned int klen, ulen;
429d8647b79SSteffen Klassert 
430d8647b79SSteffen Klassert 	if (!rta)
431d8647b79SSteffen Klassert 		return 0;
432d8647b79SSteffen Klassert 
433d8647b79SSteffen Klassert 	up = nla_data(rta);
434ecd79187SMathias Krause 	klen = xfrm_replay_state_esn_len(up);
4355e708e47SAlexey Dobriyan 	ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
436d8647b79SSteffen Klassert 
437ecd79187SMathias Krause 	p = kzalloc(klen, GFP_KERNEL);
438d8647b79SSteffen Klassert 	if (!p)
439d8647b79SSteffen Klassert 		return -ENOMEM;
440d8647b79SSteffen Klassert 
441ecd79187SMathias Krause 	pp = kzalloc(klen, GFP_KERNEL);
442d8647b79SSteffen Klassert 	if (!pp) {
443d8647b79SSteffen Klassert 		kfree(p);
444d8647b79SSteffen Klassert 		return -ENOMEM;
445d8647b79SSteffen Klassert 	}
446d8647b79SSteffen Klassert 
447ecd79187SMathias Krause 	memcpy(p, up, ulen);
448ecd79187SMathias Krause 	memcpy(pp, up, ulen);
449ecd79187SMathias Krause 
450d8647b79SSteffen Klassert 	*replay_esn = p;
451d8647b79SSteffen Klassert 	*preplay_esn = pp;
452d8647b79SSteffen Klassert 
453d8647b79SSteffen Klassert 	return 0;
454d8647b79SSteffen Klassert }
455d8647b79SSteffen Klassert 
456a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
457df71837dSTrent Jaeger {
458a1b831f2SAlexey Dobriyan 	unsigned int len = 0;
459df71837dSTrent Jaeger 
460df71837dSTrent Jaeger 	if (xfrm_ctx) {
461df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
462df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
463df71837dSTrent Jaeger 	}
464df71837dSTrent Jaeger 	return len;
465df71837dSTrent Jaeger }
466df71837dSTrent Jaeger 
4671da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
4701da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
4711da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
4721da177e4SLinus Torvalds 	x->props.mode = p->mode;
47333fce60dSFan Du 	x->props.replay_window = min_t(unsigned int, p->replay_window,
47433fce60dSFan Du 					sizeof(x->replay.bitmap) * 8);
4751da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
4761da177e4SLinus Torvalds 	x->props.family = p->family;
47754489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
4781da177e4SLinus Torvalds 	x->props.flags = p->flags;
479196b0036SHerbert Xu 
480ccf9b3b8SSteffen Klassert 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
481196b0036SHerbert Xu 		x->sel.family = p->family;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
484d51d081dSJamal Hadi Salim /*
485d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
486d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
487d51d081dSJamal Hadi Salim  *
488d51d081dSJamal Hadi Salim */
489e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
490e3ac104dSMathias Krause 				  int update_esn)
491d51d081dSJamal Hadi Salim {
4925424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
493e3ac104dSMathias Krause 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
4945424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
4955424f32eSThomas Graf 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
4965424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
497d51d081dSJamal Hadi Salim 
498d8647b79SSteffen Klassert 	if (re) {
499d8647b79SSteffen Klassert 		struct xfrm_replay_state_esn *replay_esn;
500d8647b79SSteffen Klassert 		replay_esn = nla_data(re);
501d8647b79SSteffen Klassert 		memcpy(x->replay_esn, replay_esn,
502d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
503d8647b79SSteffen Klassert 		memcpy(x->preplay_esn, replay_esn,
504d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
505d8647b79SSteffen Klassert 	}
506d8647b79SSteffen Klassert 
507d51d081dSJamal Hadi Salim 	if (rp) {
508d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
5095424f32eSThomas Graf 		replay = nla_data(rp);
510d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
511d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
512d51d081dSJamal Hadi Salim 	}
513d51d081dSJamal Hadi Salim 
514d51d081dSJamal Hadi Salim 	if (lt) {
515d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
5165424f32eSThomas Graf 		ltime = nla_data(lt);
517d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
518d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
519d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
520d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
521d51d081dSJamal Hadi Salim 	}
522d51d081dSJamal Hadi Salim 
523cf5cb79fSThomas Graf 	if (et)
5245424f32eSThomas Graf 		x->replay_maxage = nla_get_u32(et);
525d51d081dSJamal Hadi Salim 
526cf5cb79fSThomas Graf 	if (rt)
5275424f32eSThomas Graf 		x->replay_maxdiff = nla_get_u32(rt);
528d51d081dSJamal Hadi Salim }
529d51d081dSJamal Hadi Salim 
530*9b42c1f1SSteffen Klassert static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
531*9b42c1f1SSteffen Klassert {
532*9b42c1f1SSteffen Klassert 	if (attrs[XFRMA_SET_MARK]) {
533*9b42c1f1SSteffen Klassert 		m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
534*9b42c1f1SSteffen Klassert 		if (attrs[XFRMA_SET_MARK_MASK])
535*9b42c1f1SSteffen Klassert 			m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
536*9b42c1f1SSteffen Klassert 		else
537*9b42c1f1SSteffen Klassert 			m->m = 0xffffffff;
538*9b42c1f1SSteffen Klassert 	} else {
539*9b42c1f1SSteffen Klassert 		m->v = m->m = 0;
540*9b42c1f1SSteffen Klassert 	}
541*9b42c1f1SSteffen Klassert }
542*9b42c1f1SSteffen Klassert 
543fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
544fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
5455424f32eSThomas Graf 					       struct nlattr **attrs,
5461da177e4SLinus Torvalds 					       int *errp)
5471da177e4SLinus Torvalds {
548fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
5491da177e4SLinus Torvalds 	int err = -ENOMEM;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	if (!x)
5521da177e4SLinus Torvalds 		goto error_no_put;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	copy_from_user_state(x, p);
5551da177e4SLinus Torvalds 
556a947b0a9SNicolas Dichtel 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
557a947b0a9SNicolas Dichtel 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
558a947b0a9SNicolas Dichtel 
55969b0137fSHerbert Xu 	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
5601a6509d9SHerbert Xu 		goto error;
5614447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
5624447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
5634447bb33SMartin Willi 		goto error;
5644447bb33SMartin Willi 	if (!x->props.aalgo) {
5654447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
56635a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
5671da177e4SLinus Torvalds 			goto error;
5684447bb33SMartin Willi 	}
56969b0137fSHerbert Xu 	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
5701da177e4SLinus Torvalds 		goto error;
5711da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
5721da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
57335a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
5741da177e4SLinus Torvalds 		goto error;
575fd21150aSThomas Graf 
576fd21150aSThomas Graf 	if (attrs[XFRMA_ENCAP]) {
577fd21150aSThomas Graf 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
578fd21150aSThomas Graf 				   sizeof(*x->encap), GFP_KERNEL);
579fd21150aSThomas Graf 		if (x->encap == NULL)
5801da177e4SLinus Torvalds 			goto error;
581fd21150aSThomas Graf 	}
582fd21150aSThomas Graf 
58335d2856bSMartin Willi 	if (attrs[XFRMA_TFCPAD])
58435d2856bSMartin Willi 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
58535d2856bSMartin Willi 
586fd21150aSThomas Graf 	if (attrs[XFRMA_COADDR]) {
587fd21150aSThomas Graf 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
588fd21150aSThomas Graf 				    sizeof(*x->coaddr), GFP_KERNEL);
589fd21150aSThomas Graf 		if (x->coaddr == NULL)
590060f02a3SNoriaki TAKAMIYA 			goto error;
591fd21150aSThomas Graf 	}
592fd21150aSThomas Graf 
5936f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &x->mark);
5946f26b61eSJamal Hadi Salim 
595*9b42c1f1SSteffen Klassert 	xfrm_smark_init(attrs, &x->props.smark);
596077fbac4SLorenzo Colitti 
597ffdb5211SIlan Tayari 	err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
5981da177e4SLinus Torvalds 	if (err)
5991da177e4SLinus Torvalds 		goto error;
6001da177e4SLinus Torvalds 
6012f30ea50SMathias Krause 	if (attrs[XFRMA_SEC_CTX]) {
6022f30ea50SMathias Krause 		err = security_xfrm_state_alloc(x,
6032f30ea50SMathias Krause 						nla_data(attrs[XFRMA_SEC_CTX]));
6042f30ea50SMathias Krause 		if (err)
605df71837dSTrent Jaeger 			goto error;
6062f30ea50SMathias Krause 	}
607df71837dSTrent Jaeger 
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 
623cc01572eSYossi Kuperman 	/* configure the hardware if offload is requested */
624cc01572eSYossi Kuperman 	if (attrs[XFRMA_OFFLOAD_DEV]) {
625cc01572eSYossi Kuperman 		err = xfrm_dev_state_add(net, x,
626cc01572eSYossi Kuperman 					 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
627cc01572eSYossi Kuperman 		if (err)
628cc01572eSYossi Kuperman 			goto error;
629cc01572eSYossi Kuperman 	}
630cc01572eSYossi Kuperman 
6311da177e4SLinus Torvalds 	return x;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds error:
6341da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
6351da177e4SLinus Torvalds 	xfrm_state_put(x);
6361da177e4SLinus Torvalds error_no_put:
6371da177e4SLinus Torvalds 	*errp = err;
6381da177e4SLinus Torvalds 	return NULL;
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
64122e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
6425424f32eSThomas Graf 		struct nlattr **attrs)
6431da177e4SLinus Torvalds {
644fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6457b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
6461da177e4SLinus Torvalds 	struct xfrm_state *x;
6471da177e4SLinus Torvalds 	int err;
64826b15dadSJamal Hadi Salim 	struct km_event c;
6491da177e4SLinus Torvalds 
65035a7aa08SThomas Graf 	err = verify_newsa_info(p, attrs);
6511da177e4SLinus Torvalds 	if (err)
6521da177e4SLinus Torvalds 		return err;
6531da177e4SLinus Torvalds 
654fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
6551da177e4SLinus Torvalds 	if (!x)
6561da177e4SLinus Torvalds 		return err;
6571da177e4SLinus Torvalds 
65826b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
6591da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
6601da177e4SLinus Torvalds 		err = xfrm_state_add(x);
6611da177e4SLinus Torvalds 	else
6621da177e4SLinus Torvalds 		err = xfrm_state_update(x);
6631da177e4SLinus Torvalds 
6642e71029eSTetsuo Handa 	xfrm_audit_state_add(x, err ? 0 : 1, true);
665161a09e7SJoy Latten 
6661da177e4SLinus Torvalds 	if (err < 0) {
6671da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
668c5d4d7d8SSteffen Klassert 		xfrm_dev_state_delete(x);
66921380b81SHerbert Xu 		__xfrm_state_put(x);
6707d6dfe1fSPatrick McHardy 		goto out;
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 
673cc01572eSYossi Kuperman 	if (x->km.state == XFRM_STATE_VOID)
674cc01572eSYossi Kuperman 		x->km.state = XFRM_STATE_VALID;
675cc01572eSYossi Kuperman 
67626b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
67715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
678f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
67926b15dadSJamal Hadi Salim 
68026b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
6817d6dfe1fSPatrick McHardy out:
68226b15dadSJamal Hadi Salim 	xfrm_state_put(x);
6831da177e4SLinus Torvalds 	return err;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
686fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
687fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
6885424f32eSThomas Graf 						 struct nlattr **attrs,
689eb2971b6SMasahide NAKAMURA 						 int *errp)
690eb2971b6SMasahide NAKAMURA {
691eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
6926f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
693eb2971b6SMasahide NAKAMURA 	int err;
6946f26b61eSJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
695eb2971b6SMasahide NAKAMURA 
696eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
697eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
6986f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
699eb2971b6SMasahide NAKAMURA 	} else {
700eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
701eb2971b6SMasahide NAKAMURA 
70235a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
703eb2971b6SMasahide NAKAMURA 		if (!saddr) {
704eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
705eb2971b6SMasahide NAKAMURA 			goto out;
706eb2971b6SMasahide NAKAMURA 		}
707eb2971b6SMasahide NAKAMURA 
7089abbffeeSMasahide NAKAMURA 		err = -ESRCH;
7096f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup_byaddr(net, mark,
7106f26b61eSJamal Hadi Salim 					     &p->daddr, saddr,
711221df1edSAlexey Dobriyan 					     p->proto, p->family);
712eb2971b6SMasahide NAKAMURA 	}
713eb2971b6SMasahide NAKAMURA 
714eb2971b6SMasahide NAKAMURA  out:
715eb2971b6SMasahide NAKAMURA 	if (!x && errp)
716eb2971b6SMasahide NAKAMURA 		*errp = err;
717eb2971b6SMasahide NAKAMURA 	return x;
718eb2971b6SMasahide NAKAMURA }
719eb2971b6SMasahide NAKAMURA 
72022e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
7215424f32eSThomas Graf 		struct nlattr **attrs)
7221da177e4SLinus Torvalds {
723fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
7241da177e4SLinus Torvalds 	struct xfrm_state *x;
725eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
72626b15dadSJamal Hadi Salim 	struct km_event c;
7277b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
7281da177e4SLinus Torvalds 
729fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
7301da177e4SLinus Torvalds 	if (x == NULL)
731eb2971b6SMasahide NAKAMURA 		return err;
7321da177e4SLinus Torvalds 
7336f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
734c8c05a8eSCatherine Zhang 		goto out;
735c8c05a8eSCatherine Zhang 
7361da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
737c8c05a8eSCatherine Zhang 		err = -EPERM;
738c8c05a8eSCatherine Zhang 		goto out;
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 
74126b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
742161a09e7SJoy Latten 
743c8c05a8eSCatherine Zhang 	if (err < 0)
744c8c05a8eSCatherine Zhang 		goto out;
74526b15dadSJamal Hadi Salim 
74626b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
74715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
748f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
74926b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7501da177e4SLinus Torvalds 
751c8c05a8eSCatherine Zhang out:
7522e71029eSTetsuo Handa 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
753c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
75426b15dadSJamal Hadi Salim 	return err;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
7581da177e4SLinus Torvalds {
759f778a636SMathias Krause 	memset(p, 0, sizeof(*p));
7601da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
7611da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
7621da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
7631da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
764e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
765e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay, &p->stats.replay);
766e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
76754489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
7681da177e4SLinus Torvalds 	p->mode = x->props.mode;
7691da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
7701da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
7711da177e4SLinus Torvalds 	p->family = x->props.family;
7721da177e4SLinus Torvalds 	p->flags = x->props.flags;
7731da177e4SLinus Torvalds 	p->seq = x->km.seq;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds struct xfrm_dump_info {
7771da177e4SLinus Torvalds 	struct sk_buff *in_skb;
7781da177e4SLinus Torvalds 	struct sk_buff *out_skb;
7791da177e4SLinus Torvalds 	u32 nlmsg_seq;
7801da177e4SLinus Torvalds 	u16 nlmsg_flags;
7811da177e4SLinus Torvalds };
7821da177e4SLinus Torvalds 
783c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
784c0144beaSThomas Graf {
785c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
786c0144beaSThomas Graf 	struct nlattr *attr;
78768325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
788c0144beaSThomas Graf 
789c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
790c0144beaSThomas Graf 	if (attr == NULL)
791c0144beaSThomas Graf 		return -EMSGSIZE;
792c0144beaSThomas Graf 
793c0144beaSThomas Graf 	uctx = nla_data(attr);
794c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
795c0144beaSThomas Graf 	uctx->len = ctx_size;
796c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
797c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
798c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
799c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
800c0144beaSThomas Graf 
801c0144beaSThomas Graf 	return 0;
802c0144beaSThomas Graf }
803c0144beaSThomas Graf 
804d77e38e6SSteffen Klassert static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
805d77e38e6SSteffen Klassert {
806d77e38e6SSteffen Klassert 	struct xfrm_user_offload *xuo;
807d77e38e6SSteffen Klassert 	struct nlattr *attr;
808d77e38e6SSteffen Klassert 
809d77e38e6SSteffen Klassert 	attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
810d77e38e6SSteffen Klassert 	if (attr == NULL)
811d77e38e6SSteffen Klassert 		return -EMSGSIZE;
812d77e38e6SSteffen Klassert 
813d77e38e6SSteffen Klassert 	xuo = nla_data(attr);
8145fe0d4bdSMathias Krause 	memset(xuo, 0, sizeof(*xuo));
815d77e38e6SSteffen Klassert 	xuo->ifindex = xso->dev->ifindex;
816d77e38e6SSteffen Klassert 	xuo->flags = xso->flags;
817d77e38e6SSteffen Klassert 
818d77e38e6SSteffen Klassert 	return 0;
819d77e38e6SSteffen Klassert }
820d77e38e6SSteffen Klassert 
8214447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
8224447bb33SMartin Willi {
8234447bb33SMartin Willi 	struct xfrm_algo *algo;
8244447bb33SMartin Willi 	struct nlattr *nla;
8254447bb33SMartin Willi 
8264447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
8274447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
8284447bb33SMartin Willi 	if (!nla)
8294447bb33SMartin Willi 		return -EMSGSIZE;
8304447bb33SMartin Willi 
8314447bb33SMartin Willi 	algo = nla_data(nla);
8324c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
8334447bb33SMartin Willi 	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
8344447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
8354447bb33SMartin Willi 
8364447bb33SMartin Willi 	return 0;
8374447bb33SMartin Willi }
8384447bb33SMartin Willi 
839*9b42c1f1SSteffen Klassert static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
840*9b42c1f1SSteffen Klassert {
841*9b42c1f1SSteffen Klassert 	int ret = 0;
842*9b42c1f1SSteffen Klassert 
843*9b42c1f1SSteffen Klassert 	if (m->v | m->m) {
844*9b42c1f1SSteffen Klassert 		ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
845*9b42c1f1SSteffen Klassert 		if (!ret)
846*9b42c1f1SSteffen Klassert 			ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
847*9b42c1f1SSteffen Klassert 	}
848*9b42c1f1SSteffen Klassert 	return ret;
849*9b42c1f1SSteffen Klassert }
850*9b42c1f1SSteffen Klassert 
85168325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
85268325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
85368325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
85468325d3bSHerbert Xu 				    struct sk_buff *skb)
8551da177e4SLinus Torvalds {
8561d1e34ddSDavid S. Miller 	int ret = 0;
8571d1e34ddSDavid S. Miller 
8581da177e4SLinus Torvalds 	copy_to_user_state(x, p);
8591da177e4SLinus Torvalds 
860a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
861a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
862a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
863a947b0a9SNicolas Dichtel 		if (ret)
864a947b0a9SNicolas Dichtel 			goto out;
865a947b0a9SNicolas Dichtel 	}
866a947b0a9SNicolas Dichtel 
8671d1e34ddSDavid S. Miller 	if (x->coaddr) {
8681d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
8691d1e34ddSDavid S. Miller 		if (ret)
8701d1e34ddSDavid S. Miller 			goto out;
8711d1e34ddSDavid S. Miller 	}
8721d1e34ddSDavid S. Miller 	if (x->lastused) {
873de95c4a4SNicolas Dichtel 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
874de95c4a4SNicolas Dichtel 					XFRMA_PAD);
8751d1e34ddSDavid S. Miller 		if (ret)
8761d1e34ddSDavid S. Miller 			goto out;
8771d1e34ddSDavid S. Miller 	}
8781d1e34ddSDavid S. Miller 	if (x->aead) {
8791d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
8801d1e34ddSDavid S. Miller 		if (ret)
8811d1e34ddSDavid S. Miller 			goto out;
8821d1e34ddSDavid S. Miller 	}
8831d1e34ddSDavid S. Miller 	if (x->aalg) {
8841d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
8851d1e34ddSDavid S. Miller 		if (!ret)
8861d1e34ddSDavid S. Miller 			ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
8871d1e34ddSDavid S. Miller 				      xfrm_alg_auth_len(x->aalg), x->aalg);
8881d1e34ddSDavid S. Miller 		if (ret)
8891d1e34ddSDavid S. Miller 			goto out;
8901d1e34ddSDavid S. Miller 	}
8911d1e34ddSDavid S. Miller 	if (x->ealg) {
8921d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
8931d1e34ddSDavid S. Miller 		if (ret)
8941d1e34ddSDavid S. Miller 			goto out;
8951d1e34ddSDavid S. Miller 	}
8961d1e34ddSDavid S. Miller 	if (x->calg) {
8971d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
8981d1e34ddSDavid S. Miller 		if (ret)
8991d1e34ddSDavid S. Miller 			goto out;
9001d1e34ddSDavid S. Miller 	}
9011d1e34ddSDavid S. Miller 	if (x->encap) {
9021d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
9031d1e34ddSDavid S. Miller 		if (ret)
9041d1e34ddSDavid S. Miller 			goto out;
9051d1e34ddSDavid S. Miller 	}
9061d1e34ddSDavid S. Miller 	if (x->tfcpad) {
9071d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
9081d1e34ddSDavid S. Miller 		if (ret)
9091d1e34ddSDavid S. Miller 			goto out;
9101d1e34ddSDavid S. Miller 	}
9111d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
9121d1e34ddSDavid S. Miller 	if (ret)
9131d1e34ddSDavid S. Miller 		goto out;
914*9b42c1f1SSteffen Klassert 
915*9b42c1f1SSteffen Klassert 	ret = xfrm_smark_put(skb, &x->props.smark);
916*9b42c1f1SSteffen Klassert 	if (ret)
917*9b42c1f1SSteffen Klassert 		goto out;
918*9b42c1f1SSteffen Klassert 
919f293a5e3Sdingzhi 	if (x->replay_esn)
9201d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
921d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
9221d1e34ddSDavid S. Miller 			      x->replay_esn);
923f293a5e3Sdingzhi 	else
924f293a5e3Sdingzhi 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
925f293a5e3Sdingzhi 			      &x->replay);
9261d1e34ddSDavid S. Miller 	if (ret)
9271d1e34ddSDavid S. Miller 		goto out;
928d77e38e6SSteffen Klassert 	if(x->xso.dev)
929d77e38e6SSteffen Klassert 		ret = copy_user_offload(&x->xso, skb);
930d77e38e6SSteffen Klassert 	if (ret)
931d77e38e6SSteffen Klassert 		goto out;
932*9b42c1f1SSteffen Klassert 
9338598112dSSteffen Klassert 	if (x->security)
9348598112dSSteffen Klassert 		ret = copy_sec_ctx(x->security, skb);
9351d1e34ddSDavid S. Miller out:
9361d1e34ddSDavid S. Miller 	return ret;
93768325d3bSHerbert Xu }
93868325d3bSHerbert Xu 
93968325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
94068325d3bSHerbert Xu {
94168325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
94268325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
94368325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
94468325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
94568325d3bSHerbert Xu 	struct nlmsghdr *nlh;
94668325d3bSHerbert Xu 	int err;
94768325d3bSHerbert Xu 
94815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
94968325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
95068325d3bSHerbert Xu 	if (nlh == NULL)
95168325d3bSHerbert Xu 		return -EMSGSIZE;
95268325d3bSHerbert Xu 
95368325d3bSHerbert Xu 	p = nlmsg_data(nlh);
95468325d3bSHerbert Xu 
95568325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
9561d1e34ddSDavid S. Miller 	if (err) {
9579825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
95868325d3bSHerbert Xu 		return err;
9591da177e4SLinus Torvalds 	}
9601d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
9611d1e34ddSDavid S. Miller 	return 0;
9621d1e34ddSDavid S. Miller }
9631da177e4SLinus Torvalds 
9644c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
9654c563f76STimo Teras {
9664c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
967283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
968283bc9f3SFan Du 	struct net *net = sock_net(sk);
969283bc9f3SFan Du 
9701ba5bf99SVegard Nossum 	if (cb->args[0])
971283bc9f3SFan Du 		xfrm_state_walk_done(walk, net);
9724c563f76STimo Teras 	return 0;
9734c563f76STimo Teras }
9744c563f76STimo Teras 
975d3623099SNicolas Dichtel static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
9761da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
9771da177e4SLinus Torvalds {
978fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
9794c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
9801da177e4SLinus Torvalds 	struct xfrm_dump_info info;
9811da177e4SLinus Torvalds 
9824c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
9834c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
9844c563f76STimo Teras 
9851da177e4SLinus Torvalds 	info.in_skb = cb->skb;
9861da177e4SLinus Torvalds 	info.out_skb = skb;
9871da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
9881da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
9894c563f76STimo Teras 
9904c563f76STimo Teras 	if (!cb->args[0]) {
991d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
992870a2df4SNicolas Dichtel 		struct xfrm_address_filter *filter = NULL;
993d3623099SNicolas Dichtel 		u8 proto = 0;
994d3623099SNicolas Dichtel 		int err;
995d3623099SNicolas Dichtel 
996fceb6435SJohannes Berg 		err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX, xfrma_policy,
997fceb6435SJohannes Berg 				  NULL);
998d3623099SNicolas Dichtel 		if (err < 0)
999d3623099SNicolas Dichtel 			return err;
1000d3623099SNicolas Dichtel 
1001870a2df4SNicolas Dichtel 		if (attrs[XFRMA_ADDRESS_FILTER]) {
1002df367561SAndrzej Hajda 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1003df367561SAndrzej Hajda 					 sizeof(*filter), GFP_KERNEL);
1004d3623099SNicolas Dichtel 			if (filter == NULL)
1005d3623099SNicolas Dichtel 				return -ENOMEM;
1006d3623099SNicolas Dichtel 		}
1007d3623099SNicolas Dichtel 
1008d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
1009d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
1010d3623099SNicolas Dichtel 
1011d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
10121ba5bf99SVegard Nossum 		cb->args[0] = 1;
10134c563f76STimo Teras 	}
10144c563f76STimo Teras 
1015fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	return skb->len;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
10211da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
10221da177e4SLinus Torvalds {
10231da177e4SLinus Torvalds 	struct xfrm_dump_info info;
10241da177e4SLinus Torvalds 	struct sk_buff *skb;
1025864745d2SMathias Krause 	int err;
10261da177e4SLinus Torvalds 
10277deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
10281da177e4SLinus Torvalds 	if (!skb)
10291da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	info.in_skb = in_skb;
10321da177e4SLinus Torvalds 	info.out_skb = skb;
10331da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
10341da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
10351da177e4SLinus Torvalds 
1036864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
1037864745d2SMathias Krause 	if (err) {
10381da177e4SLinus Torvalds 		kfree_skb(skb);
1039864745d2SMathias Krause 		return ERR_PTR(err);
10401da177e4SLinus Torvalds 	}
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	return skb;
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
104521ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
104621ee543eSMichal Kubecek  * Must be called with RCU read lock.
104721ee543eSMichal Kubecek  */
104821ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
104921ee543eSMichal Kubecek 				       u32 pid, unsigned int group)
105021ee543eSMichal Kubecek {
105121ee543eSMichal Kubecek 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
105221ee543eSMichal Kubecek 
105321ee543eSMichal Kubecek 	if (nlsk)
105421ee543eSMichal Kubecek 		return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
105521ee543eSMichal Kubecek 	else
105621ee543eSMichal Kubecek 		return -1;
105721ee543eSMichal Kubecek }
105821ee543eSMichal Kubecek 
1059a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void)
10607deb2264SThomas Graf {
10617deb2264SThomas Graf 	return NLMSG_ALIGN(4)
10627deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1063880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1064880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1065880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
10667deb2264SThomas Graf }
10677deb2264SThomas Graf 
1068e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
106915e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
1070ecfd6b18SJamal Hadi Salim {
10715a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
10725a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
10735a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1074880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1075ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
10761d1e34ddSDavid S. Miller 	int err;
1077ecfd6b18SJamal Hadi Salim 	u32 *f;
1078880a6fabSChristophe Gouault 	unsigned lseq;
1079ecfd6b18SJamal Hadi Salim 
108015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
108125985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1082ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1083ecfd6b18SJamal Hadi Salim 
1084ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1085ecfd6b18SJamal Hadi Salim 	*f = flags;
1086e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
10875a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
10885a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
10895a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
10905a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
10915a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
10925a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
10935a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
10945a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1095ecfd6b18SJamal Hadi Salim 
1096880a6fabSChristophe Gouault 	do {
1097880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1098880a6fabSChristophe Gouault 
1099880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1100880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1101880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1102880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1103880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1104880a6fabSChristophe Gouault 
11051d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
11061d1e34ddSDavid S. Miller 	if (!err)
11071d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1108880a6fabSChristophe Gouault 	if (!err)
1109880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1110880a6fabSChristophe Gouault 	if (!err)
1111880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
11121d1e34ddSDavid S. Miller 	if (err) {
11131d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
11141d1e34ddSDavid S. Miller 		return err;
11151d1e34ddSDavid S. Miller 	}
1116ecfd6b18SJamal Hadi Salim 
1117053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1118053c095aSJohannes Berg 	return 0;
1119ecfd6b18SJamal Hadi Salim }
1120ecfd6b18SJamal Hadi Salim 
1121880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1122880a6fabSChristophe Gouault 			    struct nlattr **attrs)
1123880a6fabSChristophe Gouault {
1124880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1125880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1126880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1127880a6fabSChristophe Gouault 
1128880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1129880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1130880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1131880a6fabSChristophe Gouault 
1132880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1133880a6fabSChristophe Gouault 			return -EINVAL;
1134880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1135880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1136880a6fabSChristophe Gouault 			return -EINVAL;
1137880a6fabSChristophe Gouault 	}
1138880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1139880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1140880a6fabSChristophe Gouault 
1141880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1142880a6fabSChristophe Gouault 			return -EINVAL;
1143880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1144880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1145880a6fabSChristophe Gouault 			return -EINVAL;
1146880a6fabSChristophe Gouault 	}
1147880a6fabSChristophe Gouault 
1148880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1149880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1150880a6fabSChristophe Gouault 		if (thresh4) {
1151880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1152880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1153880a6fabSChristophe Gouault 		}
1154880a6fabSChristophe Gouault 		if (thresh6) {
1155880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1156880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1157880a6fabSChristophe Gouault 		}
1158880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1159880a6fabSChristophe Gouault 
1160880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1161880a6fabSChristophe Gouault 	}
1162880a6fabSChristophe Gouault 
1163880a6fabSChristophe Gouault 	return 0;
1164880a6fabSChristophe Gouault }
1165880a6fabSChristophe Gouault 
1166ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
11675424f32eSThomas Graf 		struct nlattr **attrs)
1168ecfd6b18SJamal Hadi Salim {
1169a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1170ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
11717b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
117215e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1173ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
11742fc5f83bSGustavo A. R. Silva 	int err;
1175ecfd6b18SJamal Hadi Salim 
11767deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1177ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1178ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1179ecfd6b18SJamal Hadi Salim 
11802fc5f83bSGustavo A. R. Silva 	err = build_spdinfo(r_skb, net, sportid, seq, *flags);
11812fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
1182ecfd6b18SJamal Hadi Salim 
118315e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1184ecfd6b18SJamal Hadi Salim }
1185ecfd6b18SJamal Hadi Salim 
1186a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void)
11877deb2264SThomas Graf {
11887deb2264SThomas Graf 	return NLMSG_ALIGN(4)
11897deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
11907deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
11917deb2264SThomas Graf }
11927deb2264SThomas Graf 
1193e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
119415e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
119528d8909bSJamal Hadi Salim {
1196af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1197af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
119828d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
11991d1e34ddSDavid S. Miller 	int err;
120028d8909bSJamal Hadi Salim 	u32 *f;
120128d8909bSJamal Hadi Salim 
120215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
120325985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
120428d8909bSJamal Hadi Salim 		return -EMSGSIZE;
120528d8909bSJamal Hadi Salim 
120628d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
120728d8909bSJamal Hadi Salim 	*f = flags;
1208e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
120928d8909bSJamal Hadi Salim 
1210af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1211af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1212af11e316SJamal Hadi Salim 
12131d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
12141d1e34ddSDavid S. Miller 	if (!err)
12151d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
12161d1e34ddSDavid S. Miller 	if (err) {
12171d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
12181d1e34ddSDavid S. Miller 		return err;
12191d1e34ddSDavid S. Miller 	}
122028d8909bSJamal Hadi Salim 
1221053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1222053c095aSJohannes Berg 	return 0;
122328d8909bSJamal Hadi Salim }
122428d8909bSJamal Hadi Salim 
122528d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12265424f32eSThomas Graf 		struct nlattr **attrs)
122728d8909bSJamal Hadi Salim {
1228a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
122928d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
12307b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
123115e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
123228d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
12332fc5f83bSGustavo A. R. Silva 	int err;
123428d8909bSJamal Hadi Salim 
12357deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
123628d8909bSJamal Hadi Salim 	if (r_skb == NULL)
123728d8909bSJamal Hadi Salim 		return -ENOMEM;
123828d8909bSJamal Hadi Salim 
12392fc5f83bSGustavo A. R. Silva 	err = build_sadinfo(r_skb, net, sportid, seq, *flags);
12402fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
124128d8909bSJamal Hadi Salim 
124215e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
124328d8909bSJamal Hadi Salim }
124428d8909bSJamal Hadi Salim 
124522e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
12465424f32eSThomas Graf 		struct nlattr **attrs)
12471da177e4SLinus Torvalds {
1248fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12497b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
12501da177e4SLinus Torvalds 	struct xfrm_state *x;
12511da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1252eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
12531da177e4SLinus Torvalds 
1254fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
12551da177e4SLinus Torvalds 	if (x == NULL)
12561da177e4SLinus Torvalds 		goto out_noput;
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
12591da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
12601da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
12611da177e4SLinus Torvalds 	} else {
126215e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
12631da177e4SLinus Torvalds 	}
12641da177e4SLinus Torvalds 	xfrm_state_put(x);
12651da177e4SLinus Torvalds out_noput:
12661da177e4SLinus Torvalds 	return err;
12671da177e4SLinus Torvalds }
12681da177e4SLinus Torvalds 
126922e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
12705424f32eSThomas Graf 		struct nlattr **attrs)
12711da177e4SLinus Torvalds {
1272fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12731da177e4SLinus Torvalds 	struct xfrm_state *x;
12741da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
12751da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
12761da177e4SLinus Torvalds 	xfrm_address_t *daddr;
12771da177e4SLinus Torvalds 	int family;
12781da177e4SLinus Torvalds 	int err;
12796f26b61eSJamal Hadi Salim 	u32 mark;
12806f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
12811da177e4SLinus Torvalds 
12827b67c857SThomas Graf 	p = nlmsg_data(nlh);
1283776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
12841da177e4SLinus Torvalds 	if (err)
12851da177e4SLinus Torvalds 		goto out_noput;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	family = p->info.family;
12881da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	x = NULL;
12916f26b61eSJamal Hadi Salim 
12926f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
12931da177e4SLinus Torvalds 	if (p->info.seq) {
12946f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
129570e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
12961da177e4SLinus Torvalds 			xfrm_state_put(x);
12971da177e4SLinus Torvalds 			x = NULL;
12981da177e4SLinus Torvalds 		}
12991da177e4SLinus Torvalds 	}
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	if (!x)
13026f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
13031da177e4SLinus Torvalds 				  p->info.id.proto, daddr,
13041da177e4SLinus Torvalds 				  &p->info.saddr, 1,
13051da177e4SLinus Torvalds 				  family);
13061da177e4SLinus Torvalds 	err = -ENOENT;
13071da177e4SLinus Torvalds 	if (x == NULL)
13081da177e4SLinus Torvalds 		goto out_noput;
13091da177e4SLinus Torvalds 
1310658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1311658b219eSHerbert Xu 	if (err)
1312658b219eSHerbert Xu 		goto out;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
13151da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
13161da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
13171da177e4SLinus Torvalds 		goto out;
13181da177e4SLinus Torvalds 	}
13191da177e4SLinus Torvalds 
132015e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds out:
13231da177e4SLinus Torvalds 	xfrm_state_put(x);
13241da177e4SLinus Torvalds out_noput:
13251da177e4SLinus Torvalds 	return err;
13261da177e4SLinus Torvalds }
13271da177e4SLinus Torvalds 
1328b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
13291da177e4SLinus Torvalds {
13301da177e4SLinus Torvalds 	switch (dir) {
13311da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
13321da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
13331da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
13341da177e4SLinus Torvalds 		break;
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	default:
13371da177e4SLinus Torvalds 		return -EINVAL;
13383ff50b79SStephen Hemminger 	}
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	return 0;
13411da177e4SLinus Torvalds }
13421da177e4SLinus Torvalds 
1343b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1344f7b6983fSMasahide NAKAMURA {
1345f7b6983fSMasahide NAKAMURA 	switch (type) {
1346f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1347f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1348f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1349f7b6983fSMasahide NAKAMURA #endif
1350f7b6983fSMasahide NAKAMURA 		break;
1351f7b6983fSMasahide NAKAMURA 
1352f7b6983fSMasahide NAKAMURA 	default:
1353f7b6983fSMasahide NAKAMURA 		return -EINVAL;
13543ff50b79SStephen Hemminger 	}
1355f7b6983fSMasahide NAKAMURA 
1356f7b6983fSMasahide NAKAMURA 	return 0;
1357f7b6983fSMasahide NAKAMURA }
1358f7b6983fSMasahide NAKAMURA 
13591da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
13601da177e4SLinus Torvalds {
1361e682adf0SFan Du 	int ret;
1362e682adf0SFan Du 
13631da177e4SLinus Torvalds 	switch (p->share) {
13641da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
13651da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
13661da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
13671da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
13681da177e4SLinus Torvalds 		break;
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	default:
13711da177e4SLinus Torvalds 		return -EINVAL;
13723ff50b79SStephen Hemminger 	}
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	switch (p->action) {
13751da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
13761da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
13771da177e4SLinus Torvalds 		break;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	default:
13801da177e4SLinus Torvalds 		return -EINVAL;
13813ff50b79SStephen Hemminger 	}
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	switch (p->sel.family) {
13841da177e4SLinus Torvalds 	case AF_INET:
13851da177e4SLinus Torvalds 		break;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	case AF_INET6:
1388dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
13891da177e4SLinus Torvalds 		break;
13901da177e4SLinus Torvalds #else
13911da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
13921da177e4SLinus Torvalds #endif
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	default:
13951da177e4SLinus Torvalds 		return -EINVAL;
13963ff50b79SStephen Hemminger 	}
13971da177e4SLinus Torvalds 
1398e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1399e682adf0SFan Du 	if (ret)
1400e682adf0SFan Du 		return ret;
1401e682adf0SFan Du 	if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1402e682adf0SFan Du 		return -EINVAL;
1403e682adf0SFan Du 
1404e682adf0SFan Du 	return 0;
14051da177e4SLinus Torvalds }
14061da177e4SLinus Torvalds 
14075424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1408df71837dSTrent Jaeger {
14095424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1410df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1411df71837dSTrent Jaeger 
1412df71837dSTrent Jaeger 	if (!rt)
1413df71837dSTrent Jaeger 		return 0;
1414df71837dSTrent Jaeger 
14155424f32eSThomas Graf 	uctx = nla_data(rt);
141652a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1417df71837dSTrent Jaeger }
1418df71837dSTrent Jaeger 
14191da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
14201da177e4SLinus Torvalds 			   int nr)
14211da177e4SLinus Torvalds {
14221da177e4SLinus Torvalds 	int i;
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
14251da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
14261da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
14291da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
14301da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
14311da177e4SLinus Torvalds 		t->reqid = ut->reqid;
14321da177e4SLinus Torvalds 		t->mode = ut->mode;
14331da177e4SLinus Torvalds 		t->share = ut->share;
14341da177e4SLinus Torvalds 		t->optional = ut->optional;
14351da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
14361da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
14371da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1438c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1439c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
14408511d01dSMiika Komu 		t->encap_family = ut->family;
14411da177e4SLinus Torvalds 	}
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
1444b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1445b4ad86bfSDavid S. Miller {
1446732706afSSteffen Klassert 	u16 prev_family;
1447b4ad86bfSDavid S. Miller 	int i;
1448b4ad86bfSDavid S. Miller 
1449b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1450b4ad86bfSDavid S. Miller 		return -EINVAL;
1451b4ad86bfSDavid S. Miller 
1452732706afSSteffen Klassert 	prev_family = family;
1453732706afSSteffen Klassert 
1454b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1455b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1456b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1457b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1458b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1459b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1460b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1461b4ad86bfSDavid S. Miller 		 */
1462b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1463b4ad86bfSDavid S. Miller 			ut[i].family = family;
1464b4ad86bfSDavid S. Miller 
1465732706afSSteffen Klassert 		if ((ut[i].mode == XFRM_MODE_TRANSPORT) &&
1466732706afSSteffen Klassert 		    (ut[i].family != prev_family))
1467732706afSSteffen Klassert 			return -EINVAL;
1468732706afSSteffen Klassert 
1469732706afSSteffen Klassert 		prev_family = ut[i].family;
1470732706afSSteffen Klassert 
1471b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1472b4ad86bfSDavid S. Miller 		case AF_INET:
1473b4ad86bfSDavid S. Miller 			break;
1474dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1475b4ad86bfSDavid S. Miller 		case AF_INET6:
1476b4ad86bfSDavid S. Miller 			break;
1477b4ad86bfSDavid S. Miller #endif
1478b4ad86bfSDavid S. Miller 		default:
1479b4ad86bfSDavid S. Miller 			return -EINVAL;
14803ff50b79SStephen Hemminger 		}
14816a53b759SCong Wang 
14826a53b759SCong Wang 		switch (ut[i].id.proto) {
14836a53b759SCong Wang 		case IPPROTO_AH:
14846a53b759SCong Wang 		case IPPROTO_ESP:
14856a53b759SCong Wang 		case IPPROTO_COMP:
14866a53b759SCong Wang #if IS_ENABLED(CONFIG_IPV6)
14876a53b759SCong Wang 		case IPPROTO_ROUTING:
14886a53b759SCong Wang 		case IPPROTO_DSTOPTS:
14896a53b759SCong Wang #endif
14906a53b759SCong Wang 		case IPSEC_PROTO_ANY:
14916a53b759SCong Wang 			break;
14926a53b759SCong Wang 		default:
14936a53b759SCong Wang 			return -EINVAL;
14946a53b759SCong Wang 		}
14956a53b759SCong Wang 
1496b4ad86bfSDavid S. Miller 	}
1497b4ad86bfSDavid S. Miller 
1498b4ad86bfSDavid S. Miller 	return 0;
1499b4ad86bfSDavid S. Miller }
1500b4ad86bfSDavid S. Miller 
15015424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
15021da177e4SLinus Torvalds {
15035424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	if (!rt) {
15061da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
15071da177e4SLinus Torvalds 	} else {
15085424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
15095424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1510b4ad86bfSDavid S. Miller 		int err;
15111da177e4SLinus Torvalds 
1512b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1513b4ad86bfSDavid S. Miller 		if (err)
1514b4ad86bfSDavid S. Miller 			return err;
15151da177e4SLinus Torvalds 
15165424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
15171da177e4SLinus Torvalds 	}
15181da177e4SLinus Torvalds 	return 0;
15191da177e4SLinus Torvalds }
15201da177e4SLinus Torvalds 
15215424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1522f7b6983fSMasahide NAKAMURA {
15235424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1524f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1525b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1526f7b6983fSMasahide NAKAMURA 	int err;
1527f7b6983fSMasahide NAKAMURA 
1528f7b6983fSMasahide NAKAMURA 	if (rt) {
15295424f32eSThomas Graf 		upt = nla_data(rt);
1530f7b6983fSMasahide NAKAMURA 		type = upt->type;
1531f7b6983fSMasahide NAKAMURA 	}
1532f7b6983fSMasahide NAKAMURA 
1533f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1534f7b6983fSMasahide NAKAMURA 	if (err)
1535f7b6983fSMasahide NAKAMURA 		return err;
1536f7b6983fSMasahide NAKAMURA 
1537f7b6983fSMasahide NAKAMURA 	*tp = type;
1538f7b6983fSMasahide NAKAMURA 	return 0;
1539f7b6983fSMasahide NAKAMURA }
1540f7b6983fSMasahide NAKAMURA 
15411da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
15421da177e4SLinus Torvalds {
15431da177e4SLinus Torvalds 	xp->priority = p->priority;
15441da177e4SLinus Torvalds 	xp->index = p->index;
15451da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
15461da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
15471da177e4SLinus Torvalds 	xp->action = p->action;
15481da177e4SLinus Torvalds 	xp->flags = p->flags;
15491da177e4SLinus Torvalds 	xp->family = p->sel.family;
15501da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
15541da177e4SLinus Torvalds {
15557b789836SMathias Krause 	memset(p, 0, sizeof(*p));
15561da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
15571da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
15581da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
15591da177e4SLinus Torvalds 	p->priority = xp->priority;
15601da177e4SLinus Torvalds 	p->index = xp->index;
15611da177e4SLinus Torvalds 	p->sel.family = xp->family;
15621da177e4SLinus Torvalds 	p->dir = dir;
15631da177e4SLinus Torvalds 	p->action = xp->action;
15641da177e4SLinus Torvalds 	p->flags = xp->flags;
15651da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
15661da177e4SLinus Torvalds }
15671da177e4SLinus Torvalds 
1568fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
15691da177e4SLinus Torvalds {
1570fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
15711da177e4SLinus Torvalds 	int err;
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	if (!xp) {
15741da177e4SLinus Torvalds 		*errp = -ENOMEM;
15751da177e4SLinus Torvalds 		return NULL;
15761da177e4SLinus Torvalds 	}
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1579df71837dSTrent Jaeger 
158035a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1581f7b6983fSMasahide NAKAMURA 	if (err)
1582f7b6983fSMasahide NAKAMURA 		goto error;
1583f7b6983fSMasahide NAKAMURA 
158435a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
158535a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1586f7b6983fSMasahide NAKAMURA 	if (err)
1587f7b6983fSMasahide NAKAMURA 		goto error;
15881da177e4SLinus Torvalds 
1589295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1590295fae56SJamal Hadi Salim 
15911da177e4SLinus Torvalds 	return xp;
1592f7b6983fSMasahide NAKAMURA  error:
1593f7b6983fSMasahide NAKAMURA 	*errp = err;
159412a169e7SHerbert Xu 	xp->walk.dead = 1;
159564c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1596f7b6983fSMasahide NAKAMURA 	return NULL;
15971da177e4SLinus Torvalds }
15981da177e4SLinus Torvalds 
159922e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
16005424f32eSThomas Graf 		struct nlattr **attrs)
16011da177e4SLinus Torvalds {
1602fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
16037b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
16041da177e4SLinus Torvalds 	struct xfrm_policy *xp;
160526b15dadSJamal Hadi Salim 	struct km_event c;
16061da177e4SLinus Torvalds 	int err;
16071da177e4SLinus Torvalds 	int excl;
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
16101da177e4SLinus Torvalds 	if (err)
16111da177e4SLinus Torvalds 		return err;
161235a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1613df71837dSTrent Jaeger 	if (err)
1614df71837dSTrent Jaeger 		return err;
16151da177e4SLinus Torvalds 
1616fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
16171da177e4SLinus Torvalds 	if (!xp)
16181da177e4SLinus Torvalds 		return err;
16191da177e4SLinus Torvalds 
162025985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
162126b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
162226b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
162326b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
16241da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
16251da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
16262e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1627161a09e7SJoy Latten 
16281da177e4SLinus Torvalds 	if (err) {
162903e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
16301da177e4SLinus Torvalds 		kfree(xp);
16311da177e4SLinus Torvalds 		return err;
16321da177e4SLinus Torvalds 	}
16331da177e4SLinus Torvalds 
1634f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
163526b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
163615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
163726b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
163826b15dadSJamal Hadi Salim 
16391da177e4SLinus Torvalds 	xfrm_pol_put(xp);
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	return 0;
16421da177e4SLinus Torvalds }
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
16451da177e4SLinus Torvalds {
16461da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
16471da177e4SLinus Torvalds 	int i;
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
16501da177e4SLinus Torvalds 		return 0;
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
16531da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
16541da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
16551da177e4SLinus Torvalds 
16561f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
16571da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
16588511d01dSMiika Komu 		up->family = kp->encap_family;
16591da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
16601da177e4SLinus Torvalds 		up->reqid = kp->reqid;
16611da177e4SLinus Torvalds 		up->mode = kp->mode;
16621da177e4SLinus Torvalds 		up->share = kp->share;
16631da177e4SLinus Torvalds 		up->optional = kp->optional;
16641da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
16651da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
16661da177e4SLinus Torvalds 		up->calgos = kp->calgos;
16671da177e4SLinus Torvalds 	}
16681da177e4SLinus Torvalds 
1669c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1670c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1671df71837dSTrent Jaeger }
1672df71837dSTrent Jaeger 
16730d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
16740d681623SSerge Hallyn {
16750d681623SSerge Hallyn 	if (x->security) {
16760d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
16770d681623SSerge Hallyn 	}
16780d681623SSerge Hallyn 	return 0;
16790d681623SSerge Hallyn }
16800d681623SSerge Hallyn 
16810d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
16820d681623SSerge Hallyn {
16831d1e34ddSDavid S. Miller 	if (xp->security)
16840d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
16850d681623SSerge Hallyn 	return 0;
16860d681623SSerge Hallyn }
1687a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void)
1688cfbfd45aSThomas Graf {
1689cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1690cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1691cfbfd45aSThomas Graf #else
1692cfbfd45aSThomas Graf 	return 0;
1693cfbfd45aSThomas Graf #endif
1694cfbfd45aSThomas Graf }
16950d681623SSerge Hallyn 
1696f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1697b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1698f7b6983fSMasahide NAKAMURA {
1699c0144beaSThomas Graf 	struct xfrm_userpolicy_type upt = {
1700c0144beaSThomas Graf 		.type = type,
1701c0144beaSThomas Graf 	};
1702f7b6983fSMasahide NAKAMURA 
1703c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1704f7b6983fSMasahide NAKAMURA }
1705f7b6983fSMasahide NAKAMURA 
1706f7b6983fSMasahide NAKAMURA #else
1707b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1708f7b6983fSMasahide NAKAMURA {
1709f7b6983fSMasahide NAKAMURA 	return 0;
1710f7b6983fSMasahide NAKAMURA }
1711f7b6983fSMasahide NAKAMURA #endif
1712f7b6983fSMasahide NAKAMURA 
17131da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
17141da177e4SLinus Torvalds {
17151da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
17161da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
17171da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
17181da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
17191da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
17201d1e34ddSDavid S. Miller 	int err;
17211da177e4SLinus Torvalds 
172215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
172379b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
172479b8b7f4SThomas Graf 	if (nlh == NULL)
172579b8b7f4SThomas Graf 		return -EMSGSIZE;
17261da177e4SLinus Torvalds 
17277b67c857SThomas Graf 	p = nlmsg_data(nlh);
17281da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
17291d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
17301d1e34ddSDavid S. Miller 	if (!err)
17311d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
17321d1e34ddSDavid S. Miller 	if (!err)
17331d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
17341d1e34ddSDavid S. Miller 	if (!err)
17351d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
17361d1e34ddSDavid S. Miller 	if (err) {
17371d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
17381d1e34ddSDavid S. Miller 		return err;
17391d1e34ddSDavid S. Miller 	}
17409825069dSThomas Graf 	nlmsg_end(skb, nlh);
17411da177e4SLinus Torvalds 	return 0;
17421da177e4SLinus Torvalds }
17431da177e4SLinus Torvalds 
17444c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
17454c563f76STimo Teras {
17461137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1747283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
17484c563f76STimo Teras 
1749283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
17504c563f76STimo Teras 	return 0;
17514c563f76STimo Teras }
17524c563f76STimo Teras 
17531137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb)
17541137b5e2SHerbert Xu {
17551137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
17561137b5e2SHerbert Xu 
17571137b5e2SHerbert Xu 	BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
17581137b5e2SHerbert Xu 
17591137b5e2SHerbert Xu 	xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
17601137b5e2SHerbert Xu 	return 0;
17611137b5e2SHerbert Xu }
17621137b5e2SHerbert Xu 
17631da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
17641da177e4SLinus Torvalds {
1765fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
17661137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
17671da177e4SLinus Torvalds 	struct xfrm_dump_info info;
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	info.in_skb = cb->skb;
17701da177e4SLinus Torvalds 	info.out_skb = skb;
17711da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
17721da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
17734c563f76STimo Teras 
1774fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	return skb->len;
17771da177e4SLinus Torvalds }
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
17801da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
17811da177e4SLinus Torvalds 					  int dir, u32 seq)
17821da177e4SLinus Torvalds {
17831da177e4SLinus Torvalds 	struct xfrm_dump_info info;
17841da177e4SLinus Torvalds 	struct sk_buff *skb;
1785c2546372SMathias Krause 	int err;
17861da177e4SLinus Torvalds 
17877deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17881da177e4SLinus Torvalds 	if (!skb)
17891da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 	info.in_skb = in_skb;
17921da177e4SLinus Torvalds 	info.out_skb = skb;
17931da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
17941da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
17951da177e4SLinus Torvalds 
1796c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1797c2546372SMathias Krause 	if (err) {
17981da177e4SLinus Torvalds 		kfree_skb(skb);
1799c2546372SMathias Krause 		return ERR_PTR(err);
18001da177e4SLinus Torvalds 	}
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	return skb;
18031da177e4SLinus Torvalds }
18041da177e4SLinus Torvalds 
180522e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
18065424f32eSThomas Graf 		struct nlattr **attrs)
18071da177e4SLinus Torvalds {
1808fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
18091da177e4SLinus Torvalds 	struct xfrm_policy *xp;
18101da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1811b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
18121da177e4SLinus Torvalds 	int err;
181326b15dadSJamal Hadi Salim 	struct km_event c;
18141da177e4SLinus Torvalds 	int delete;
1815295fae56SJamal Hadi Salim 	struct xfrm_mark m;
1816295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
18171da177e4SLinus Torvalds 
18187b67c857SThomas Graf 	p = nlmsg_data(nlh);
18191da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
18201da177e4SLinus Torvalds 
182135a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1822f7b6983fSMasahide NAKAMURA 	if (err)
1823f7b6983fSMasahide NAKAMURA 		return err;
1824f7b6983fSMasahide NAKAMURA 
18251da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
18261da177e4SLinus Torvalds 	if (err)
18271da177e4SLinus Torvalds 		return err;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	if (p->index)
1830295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1831df71837dSTrent Jaeger 	else {
18325424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
183303e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1834df71837dSTrent Jaeger 
183535a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
1836df71837dSTrent Jaeger 		if (err)
1837df71837dSTrent Jaeger 			return err;
1838df71837dSTrent Jaeger 
18392c8dd116SDenis V. Lunev 		ctx = NULL;
1840df71837dSTrent Jaeger 		if (rt) {
18415424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1842df71837dSTrent Jaeger 
184352a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
184403e1ad7bSPaul Moore 			if (err)
1845df71837dSTrent Jaeger 				return err;
18462c8dd116SDenis V. Lunev 		}
1847295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
18486f26b61eSJamal Hadi Salim 					   ctx, delete, &err);
184903e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
1850df71837dSTrent Jaeger 	}
18511da177e4SLinus Torvalds 	if (xp == NULL)
18521da177e4SLinus Torvalds 		return -ENOENT;
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds 	if (!delete) {
18551da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
18581da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
18591da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
18601da177e4SLinus Torvalds 		} else {
1861a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
186215e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
18631da177e4SLinus Torvalds 		}
186426b15dadSJamal Hadi Salim 	} else {
18652e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
186613fcfbb0SDavid S. Miller 
186713fcfbb0SDavid S. Miller 		if (err != 0)
1868c8c05a8eSCatherine Zhang 			goto out;
186913fcfbb0SDavid S. Miller 
1870e7443892SHerbert Xu 		c.data.byid = p->index;
1871f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
187226b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
187315e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
187426b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
18751da177e4SLinus Torvalds 	}
18761da177e4SLinus Torvalds 
1877c8c05a8eSCatherine Zhang out:
1878ef41aaa0SEric Paris 	xfrm_pol_put(xp);
18791da177e4SLinus Torvalds 	return err;
18801da177e4SLinus Torvalds }
18811da177e4SLinus Torvalds 
188222e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
18835424f32eSThomas Graf 		struct nlattr **attrs)
18841da177e4SLinus Torvalds {
1885fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
188626b15dadSJamal Hadi Salim 	struct km_event c;
18877b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
18884aa2e62cSJoy Latten 	int err;
18891da177e4SLinus Torvalds 
18902e71029eSTetsuo Handa 	err = xfrm_state_flush(net, p->proto, true);
18919e64cc95SJamal Hadi Salim 	if (err) {
18929e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
18939e64cc95SJamal Hadi Salim 			return 0;
1894069c474eSDavid S. Miller 		return err;
18959e64cc95SJamal Hadi Salim 	}
1896bf08867fSHerbert Xu 	c.data.proto = p->proto;
1897f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
189826b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
189915e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
19007067802eSAlexey Dobriyan 	c.net = net;
190126b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
190226b15dadSJamal Hadi Salim 
19031da177e4SLinus Torvalds 	return 0;
19041da177e4SLinus Torvalds }
19051da177e4SLinus Torvalds 
1906a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
19077deb2264SThomas Graf {
1908a1b831f2SAlexey Dobriyan 	unsigned int replay_size = x->replay_esn ?
1909d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
1910d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
1911d8647b79SSteffen Klassert 
19127deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1913d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
1914de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
19156f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
19167deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
19177deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
19187deb2264SThomas Graf }
1919d51d081dSJamal Hadi Salim 
1920214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1921d51d081dSJamal Hadi Salim {
1922d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1923d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
19241d1e34ddSDavid S. Miller 	int err;
1925d51d081dSJamal Hadi Salim 
192615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
192779b8b7f4SThomas Graf 	if (nlh == NULL)
192879b8b7f4SThomas Graf 		return -EMSGSIZE;
1929d51d081dSJamal Hadi Salim 
19307b67c857SThomas Graf 	id = nlmsg_data(nlh);
1931931e79d7SMathias Krause 	memset(&id->sa_id, 0, sizeof(id->sa_id));
19322b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1933d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
1934d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
1935d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
19362b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
19372b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
1938d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
1939d51d081dSJamal Hadi Salim 
1940d0fde795SDavid S. Miller 	if (x->replay_esn) {
19411d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1942d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
19431d1e34ddSDavid S. Miller 			      x->replay_esn);
1944d0fde795SDavid S. Miller 	} else {
19451d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
19461d1e34ddSDavid S. Miller 			      &x->replay);
1947d0fde795SDavid S. Miller 	}
19481d1e34ddSDavid S. Miller 	if (err)
19491d1e34ddSDavid S. Miller 		goto out_cancel;
1950de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1951de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
19521d1e34ddSDavid S. Miller 	if (err)
19531d1e34ddSDavid S. Miller 		goto out_cancel;
1954d8647b79SSteffen Klassert 
19551d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
19561d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
19571d1e34ddSDavid S. Miller 		if (err)
19581d1e34ddSDavid S. Miller 			goto out_cancel;
19591d1e34ddSDavid S. Miller 	}
19601d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
19611d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
19621d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
19631d1e34ddSDavid S. Miller 		if (err)
19641d1e34ddSDavid S. Miller 			goto out_cancel;
19651d1e34ddSDavid S. Miller 	}
19661d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
19671d1e34ddSDavid S. Miller 	if (err)
19681d1e34ddSDavid S. Miller 		goto out_cancel;
19696f26b61eSJamal Hadi Salim 
1970053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1971053c095aSJohannes Berg 	return 0;
1972d51d081dSJamal Hadi Salim 
19731d1e34ddSDavid S. Miller out_cancel:
19749825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
19751d1e34ddSDavid S. Miller 	return err;
1976d51d081dSJamal Hadi Salim }
1977d51d081dSJamal Hadi Salim 
197822e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
19795424f32eSThomas Graf 		struct nlattr **attrs)
1980d51d081dSJamal Hadi Salim {
1981fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1982d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1983d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
1984d51d081dSJamal Hadi Salim 	int err;
1985d51d081dSJamal Hadi Salim 	struct km_event c;
19866f26b61eSJamal Hadi Salim 	u32 mark;
19876f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
19887b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
1989d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
1990d51d081dSJamal Hadi Salim 
19916f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
19926f26b61eSJamal Hadi Salim 
19936f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1994d8647b79SSteffen Klassert 	if (x == NULL)
1995d51d081dSJamal Hadi Salim 		return -ESRCH;
1996d8647b79SSteffen Klassert 
1997d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1998d8647b79SSteffen Klassert 	if (r_skb == NULL) {
1999d8647b79SSteffen Klassert 		xfrm_state_put(x);
2000d8647b79SSteffen Klassert 		return -ENOMEM;
2001d51d081dSJamal Hadi Salim 	}
2002d51d081dSJamal Hadi Salim 
2003d51d081dSJamal Hadi Salim 	/*
2004d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
2005d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
2006d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
2007d51d081dSJamal Hadi Salim 	*/
2008d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2009d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
2010d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
201115e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2012d51d081dSJamal Hadi Salim 
20132fc5f83bSGustavo A. R. Silva 	err = build_aevent(r_skb, x, &c);
20142fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
20152fc5f83bSGustavo A. R. Silva 
201615e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2017d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2018d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2019d51d081dSJamal Hadi Salim 	return err;
2020d51d081dSJamal Hadi Salim }
2021d51d081dSJamal Hadi Salim 
202222e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
20235424f32eSThomas Graf 		struct nlattr **attrs)
2024d51d081dSJamal Hadi Salim {
2025fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2026d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2027d51d081dSJamal Hadi Salim 	struct km_event c;
2028d51d081dSJamal Hadi Salim 	int err = -EINVAL;
20296f26b61eSJamal Hadi Salim 	u32 mark = 0;
20306f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
20317b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
20325424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2033d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
20345424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
20354e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
20364e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2037d51d081dSJamal Hadi Salim 
20384e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
2039d51d081dSJamal Hadi Salim 		return err;
2040d51d081dSJamal Hadi Salim 
2041d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
2042d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2043d51d081dSJamal Hadi Salim 		return err;
2044d51d081dSJamal Hadi Salim 
20456f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
20466f26b61eSJamal Hadi Salim 
20476f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2048d51d081dSJamal Hadi Salim 	if (x == NULL)
2049d51d081dSJamal Hadi Salim 		return -ESRCH;
2050d51d081dSJamal Hadi Salim 
2051d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
2052d51d081dSJamal Hadi Salim 		goto out;
2053d51d081dSJamal Hadi Salim 
20544479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
2055e2b19125SSteffen Klassert 	if (err)
2056e2b19125SSteffen Klassert 		goto out;
2057e2b19125SSteffen Klassert 
2058d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2059e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
2060d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2061d51d081dSJamal Hadi Salim 
2062d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
2063d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
206415e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2065d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
2066d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
2067d51d081dSJamal Hadi Salim 	err = 0;
2068d51d081dSJamal Hadi Salim out:
2069d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2070d51d081dSJamal Hadi Salim 	return err;
2071d51d081dSJamal Hadi Salim }
2072d51d081dSJamal Hadi Salim 
207322e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
20745424f32eSThomas Graf 		struct nlattr **attrs)
20751da177e4SLinus Torvalds {
2076fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
207726b15dadSJamal Hadi Salim 	struct km_event c;
2078b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
2079f7b6983fSMasahide NAKAMURA 	int err;
208026b15dadSJamal Hadi Salim 
208135a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2082f7b6983fSMasahide NAKAMURA 	if (err)
2083f7b6983fSMasahide NAKAMURA 		return err;
2084f7b6983fSMasahide NAKAMURA 
20852e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
20862f1eb65fSJamal Hadi Salim 	if (err) {
20872f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
20882f1eb65fSJamal Hadi Salim 			return 0;
2089069c474eSDavid S. Miller 		return err;
20902f1eb65fSJamal Hadi Salim 	}
20912f1eb65fSJamal Hadi Salim 
2092f7b6983fSMasahide NAKAMURA 	c.data.type = type;
2093f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
209426b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
209515e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
20967067802eSAlexey Dobriyan 	c.net = net;
209726b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
20981da177e4SLinus Torvalds 	return 0;
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds 
210122e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
21025424f32eSThomas Graf 		struct nlattr **attrs)
21036c5c8ca7SJamal Hadi Salim {
2104fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
21056c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
21067b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
21076c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2108b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
21096c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2110295fae56SJamal Hadi Salim 	struct xfrm_mark m;
2111295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
21126c5c8ca7SJamal Hadi Salim 
211335a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2114f7b6983fSMasahide NAKAMURA 	if (err)
2115f7b6983fSMasahide NAKAMURA 		return err;
2116f7b6983fSMasahide NAKAMURA 
2117c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
2118c8bf4d04STimo Teräs 	if (err)
2119c8bf4d04STimo Teräs 		return err;
2120c8bf4d04STimo Teräs 
21216c5c8ca7SJamal Hadi Salim 	if (p->index)
2122295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
21236c5c8ca7SJamal Hadi Salim 	else {
21245424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
212503e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
21266c5c8ca7SJamal Hadi Salim 
212735a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
21286c5c8ca7SJamal Hadi Salim 		if (err)
21296c5c8ca7SJamal Hadi Salim 			return err;
21306c5c8ca7SJamal Hadi Salim 
21312c8dd116SDenis V. Lunev 		ctx = NULL;
21326c5c8ca7SJamal Hadi Salim 		if (rt) {
21335424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
21346c5c8ca7SJamal Hadi Salim 
213552a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
213603e1ad7bSPaul Moore 			if (err)
21376c5c8ca7SJamal Hadi Salim 				return err;
21382c8dd116SDenis V. Lunev 		}
2139295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
21406f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
214103e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
21426c5c8ca7SJamal Hadi Salim 	}
21436c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2144ef41aaa0SEric Paris 		return -ENOENT;
214503e1ad7bSPaul Moore 
2146ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
21476c5c8ca7SJamal Hadi Salim 		goto out;
21486c5c8ca7SJamal Hadi Salim 
21496c5c8ca7SJamal Hadi Salim 	err = 0;
21506c5c8ca7SJamal Hadi Salim 	if (up->hard) {
21516c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
21522e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
21536c5c8ca7SJamal Hadi Salim 	}
2154c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
21556c5c8ca7SJamal Hadi Salim 
21566c5c8ca7SJamal Hadi Salim out:
21576c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
21586c5c8ca7SJamal Hadi Salim 	return err;
21596c5c8ca7SJamal Hadi Salim }
21606c5c8ca7SJamal Hadi Salim 
216122e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
21625424f32eSThomas Graf 		struct nlattr **attrs)
216353bc6b4dSJamal Hadi Salim {
2164fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
216553bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
216653bc6b4dSJamal Hadi Salim 	int err;
21677b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
216853bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
21696f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2170928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
217153bc6b4dSJamal Hadi Salim 
21726f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
217353bc6b4dSJamal Hadi Salim 
21743a765aa5SDavid S. Miller 	err = -ENOENT;
217553bc6b4dSJamal Hadi Salim 	if (x == NULL)
217653bc6b4dSJamal Hadi Salim 		return err;
217753bc6b4dSJamal Hadi Salim 
217853bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
21793a765aa5SDavid S. Miller 	err = -EINVAL;
218053bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
218153bc6b4dSJamal Hadi Salim 		goto out;
2182c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
218353bc6b4dSJamal Hadi Salim 
2184161a09e7SJoy Latten 	if (ue->hard) {
218553bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
21862e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2187161a09e7SJoy Latten 	}
21883a765aa5SDavid S. Miller 	err = 0;
218953bc6b4dSJamal Hadi Salim out:
219053bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
219153bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
219253bc6b4dSJamal Hadi Salim 	return err;
219353bc6b4dSJamal Hadi Salim }
219453bc6b4dSJamal Hadi Salim 
219522e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
21965424f32eSThomas Graf 		struct nlattr **attrs)
2197980ebd25SJamal Hadi Salim {
2198fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2199980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2200980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2201980ebd25SJamal Hadi Salim 	int i;
22025424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
22036f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2204980ebd25SJamal Hadi Salim 
22057b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2206fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2207980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2208980ebd25SJamal Hadi Salim 
2209980ebd25SJamal Hadi Salim 	if (!x)
2210d8eb9307SIlpo Järvinen 		goto nomem;
2211980ebd25SJamal Hadi Salim 
22126f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
22136f26b61eSJamal Hadi Salim 
2214980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2215d8eb9307SIlpo Järvinen 	if (err)
221673efc324SVegard Nossum 		goto free_state;
2217980ebd25SJamal Hadi Salim 
2218980ebd25SJamal Hadi Salim 	/*   build an XP */
2219fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2220d8eb9307SIlpo Järvinen 	if (!xp)
2221d8eb9307SIlpo Järvinen 		goto free_state;
2222980ebd25SJamal Hadi Salim 
2223980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2224980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2225980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
22266f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
22276f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
22285424f32eSThomas Graf 	ut = nla_data(rt);
2229980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2230980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2231980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2232980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2233980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2234980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2235980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2236980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2237980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2238980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2239980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2240980ebd25SJamal Hadi Salim 
2241980ebd25SJamal Hadi Salim 	}
2242980ebd25SJamal Hadi Salim 
2243980ebd25SJamal Hadi Salim 	kfree(x);
2244980ebd25SJamal Hadi Salim 	kfree(xp);
2245980ebd25SJamal Hadi Salim 
2246980ebd25SJamal Hadi Salim 	return 0;
2247d8eb9307SIlpo Järvinen 
2248d8eb9307SIlpo Järvinen free_state:
2249d8eb9307SIlpo Järvinen 	kfree(x);
2250d8eb9307SIlpo Järvinen nomem:
2251d8eb9307SIlpo Järvinen 	return err;
2252980ebd25SJamal Hadi Salim }
2253980ebd25SJamal Hadi Salim 
22545c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
22555c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
225613c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
22575424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
22585c79de6eSShinta Sugimoto {
22595424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
22605c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
22615c79de6eSShinta Sugimoto 	int i, num_migrate;
22625c79de6eSShinta Sugimoto 
226313c1d189SArnaud Ebalard 	if (k != NULL) {
226413c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
226513c1d189SArnaud Ebalard 
226613c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
226713c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
226813c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
226913c1d189SArnaud Ebalard 		k->family = uk->family;
227013c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
227113c1d189SArnaud Ebalard 	}
227213c1d189SArnaud Ebalard 
22735424f32eSThomas Graf 	um = nla_data(rt);
22745424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
22755c79de6eSShinta Sugimoto 
22765c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
22775c79de6eSShinta Sugimoto 		return -EINVAL;
22785c79de6eSShinta Sugimoto 
22795c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
22805c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
22815c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
22825c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
22835c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
22845c79de6eSShinta Sugimoto 
22855c79de6eSShinta Sugimoto 		ma->proto = um->proto;
22865c79de6eSShinta Sugimoto 		ma->mode = um->mode;
22875c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
22885c79de6eSShinta Sugimoto 
22895c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
22905c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
22915c79de6eSShinta Sugimoto 	}
22925c79de6eSShinta Sugimoto 
22935c79de6eSShinta Sugimoto 	*num = i;
22945c79de6eSShinta Sugimoto 	return 0;
22955c79de6eSShinta Sugimoto }
22965c79de6eSShinta Sugimoto 
22975c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
22985424f32eSThomas Graf 			   struct nlattr **attrs)
22995c79de6eSShinta Sugimoto {
23007b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
23015c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
230213c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
23035c79de6eSShinta Sugimoto 	u8 type;
23045c79de6eSShinta Sugimoto 	int err;
23055c79de6eSShinta Sugimoto 	int n = 0;
23068d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
23074ab47d47SAntony Antony 	struct xfrm_encap_tmpl  *encap = NULL;
23085c79de6eSShinta Sugimoto 
230935a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2310cf5cb79fSThomas Graf 		return -EINVAL;
23115c79de6eSShinta Sugimoto 
231213c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
231313c1d189SArnaud Ebalard 
23145424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
23155c79de6eSShinta Sugimoto 	if (err)
23165c79de6eSShinta Sugimoto 		return err;
23175c79de6eSShinta Sugimoto 
231813c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
23195c79de6eSShinta Sugimoto 	if (err)
23205c79de6eSShinta Sugimoto 		return err;
23215c79de6eSShinta Sugimoto 
23225c79de6eSShinta Sugimoto 	if (!n)
23235c79de6eSShinta Sugimoto 		return 0;
23245c79de6eSShinta Sugimoto 
23254ab47d47SAntony Antony 	if (attrs[XFRMA_ENCAP]) {
23264ab47d47SAntony Antony 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
23274ab47d47SAntony Antony 				sizeof(*encap), GFP_KERNEL);
23284ab47d47SAntony Antony 		if (!encap)
23295c79de6eSShinta Sugimoto 			return 0;
23305c79de6eSShinta Sugimoto 	}
23314ab47d47SAntony Antony 
23324ab47d47SAntony Antony 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
23334ab47d47SAntony Antony 
23344ab47d47SAntony Antony 	kfree(encap);
23354ab47d47SAntony Antony 
23364ab47d47SAntony Antony 	return err;
23374ab47d47SAntony Antony }
23385c79de6eSShinta Sugimoto #else
23395c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
23405424f32eSThomas Graf 			   struct nlattr **attrs)
23415c79de6eSShinta Sugimoto {
23425c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
23435c79de6eSShinta Sugimoto }
23445c79de6eSShinta Sugimoto #endif
23455c79de6eSShinta Sugimoto 
23465c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2347183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
23485c79de6eSShinta Sugimoto {
23495c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
23505c79de6eSShinta Sugimoto 
23515c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
23525c79de6eSShinta Sugimoto 	um.proto = m->proto;
23535c79de6eSShinta Sugimoto 	um.mode = m->mode;
23545c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
23555c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
23565c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
23575c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
23585c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
23595c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
23605c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
23615c79de6eSShinta Sugimoto 
2362c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
23635c79de6eSShinta Sugimoto }
23645c79de6eSShinta Sugimoto 
2365183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
236613c1d189SArnaud Ebalard {
236713c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
236813c1d189SArnaud Ebalard 
236913c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
237013c1d189SArnaud Ebalard 	uk.family = k->family;
237113c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
237213c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2373a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
237413c1d189SArnaud Ebalard 
237513c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
237613c1d189SArnaud Ebalard }
237713c1d189SArnaud Ebalard 
2378a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
23798bafd730SAntony Antony 						int with_encp)
23807deb2264SThomas Graf {
23817deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
238213c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
23838bafd730SAntony Antony 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
23847deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
23857deb2264SThomas Graf 	      + userpolicy_type_attrsize();
23867deb2264SThomas Graf }
23877deb2264SThomas Graf 
2388183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2389183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
23908bafd730SAntony Antony 			 const struct xfrm_selector *sel,
23918bafd730SAntony Antony 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
23925c79de6eSShinta Sugimoto {
2393183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
23945c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
23955c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
23961d1e34ddSDavid S. Miller 	int i, err;
23975c79de6eSShinta Sugimoto 
239879b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
239979b8b7f4SThomas Graf 	if (nlh == NULL)
240079b8b7f4SThomas Graf 		return -EMSGSIZE;
24015c79de6eSShinta Sugimoto 
24027b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
24035c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
24045c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
24055c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
24065c79de6eSShinta Sugimoto 	pol_id->dir = dir;
24075c79de6eSShinta Sugimoto 
24081d1e34ddSDavid S. Miller 	if (k != NULL) {
24091d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
24101d1e34ddSDavid S. Miller 		if (err)
24111d1e34ddSDavid S. Miller 			goto out_cancel;
24121d1e34ddSDavid S. Miller 	}
24138bafd730SAntony Antony 	if (encap) {
24148bafd730SAntony Antony 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
24158bafd730SAntony Antony 		if (err)
24168bafd730SAntony Antony 			goto out_cancel;
24178bafd730SAntony Antony 	}
24181d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
24191d1e34ddSDavid S. Miller 	if (err)
24201d1e34ddSDavid S. Miller 		goto out_cancel;
24215c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
24221d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
24231d1e34ddSDavid S. Miller 		if (err)
24241d1e34ddSDavid S. Miller 			goto out_cancel;
24255c79de6eSShinta Sugimoto 	}
24265c79de6eSShinta Sugimoto 
2427053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2428053c095aSJohannes Berg 	return 0;
24291d1e34ddSDavid S. Miller 
24301d1e34ddSDavid S. Miller out_cancel:
24319825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
24321d1e34ddSDavid S. Miller 	return err;
24335c79de6eSShinta Sugimoto }
24345c79de6eSShinta Sugimoto 
2435183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2436183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
24378bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
24388bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
24395c79de6eSShinta Sugimoto {
2440a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
24415c79de6eSShinta Sugimoto 	struct sk_buff *skb;
24422fc5f83bSGustavo A. R. Silva 	int err;
24435c79de6eSShinta Sugimoto 
24448bafd730SAntony Antony 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
24458bafd730SAntony Antony 			GFP_ATOMIC);
24465c79de6eSShinta Sugimoto 	if (skb == NULL)
24475c79de6eSShinta Sugimoto 		return -ENOMEM;
24485c79de6eSShinta Sugimoto 
24495c79de6eSShinta Sugimoto 	/* build migrate */
24502fc5f83bSGustavo A. R. Silva 	err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
24512fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
24525c79de6eSShinta Sugimoto 
245321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
24545c79de6eSShinta Sugimoto }
24555c79de6eSShinta Sugimoto #else
2456183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2457183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
24588bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
24598bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
24605c79de6eSShinta Sugimoto {
24615c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
24625c79de6eSShinta Sugimoto }
24635c79de6eSShinta Sugimoto #endif
2464d51d081dSJamal Hadi Salim 
2465a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2466492b558bSThomas Graf 
2467492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
246866f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2469492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2470492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2471492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2472492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2473492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2474492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2475980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
247653bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2477492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
247866f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
24796c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2480492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2481a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2482d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2483d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
248497a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
24855c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2486a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2487880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2488a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
24891da177e4SLinus Torvalds };
24901da177e4SLinus Torvalds 
2491492b558bSThomas Graf #undef XMSGSIZE
2492492b558bSThomas Graf 
2493cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2494c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2495c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2496c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2497c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
24981a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2499cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2500cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2501cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2502cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2503cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2504cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2505cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2506cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2507cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2508cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2509cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2510cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2511cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2512cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
251313c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
25146f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
251535d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2516d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2517a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2518d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2519870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2520d77e38e6SSteffen Klassert 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
2521*9b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK]	= { .type = NLA_U32 },
2522*9b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK_MASK]	= { .type = NLA_U32 },
2523cf5cb79fSThomas Graf };
2524cf5cb79fSThomas Graf 
2525880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2526880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2527880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2528880a6fabSChristophe Gouault };
2529880a6fabSChristophe Gouault 
253005600a79SMathias Krause static const struct xfrm_link {
25315424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
25321137b5e2SHerbert Xu 	int (*start)(struct netlink_callback *);
25331da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
25344c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2535880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2536880a6fabSChristophe Gouault 	int nla_max;
2537492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2538492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2539492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2540492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
25414c563f76STimo Teras 						   .dump = xfrm_dump_sa,
25424c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2543492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2544492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2545492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
25461137b5e2SHerbert Xu 						   .start = xfrm_dump_policy_start,
25474c563f76STimo Teras 						   .dump = xfrm_dump_policy,
25484c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2549492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2550980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
255153bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2552492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2553492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
25546c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2555492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2556492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2557d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2558d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
25595c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
256028d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2561880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2562880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2563880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2564ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
25651da177e4SLinus Torvalds };
25661da177e4SLinus Torvalds 
25672d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
25682d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
25691da177e4SLinus Torvalds {
2570a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
257135a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
257205600a79SMathias Krause 	const struct xfrm_link *link;
2573a7bd9a45SThomas Graf 	int type, err;
25741da177e4SLinus Torvalds 
257574005991SFan Du #ifdef CONFIG_COMPAT
25762bf8c476SAndy Lutomirski 	if (in_compat_syscall())
257783e2d058SYi Zhao 		return -EOPNOTSUPP;
257874005991SFan Du #endif
257974005991SFan Du 
25801da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
25811da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
25821d00a4ebSThomas Graf 		return -EINVAL;
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
25851da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
258890f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
25891d00a4ebSThomas Graf 		return -EPERM;
25901da177e4SLinus Torvalds 
2591492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2592492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2593b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
25941da177e4SLinus Torvalds 		if (link->dump == NULL)
25951d00a4ebSThomas Graf 			return -EINVAL;
25961da177e4SLinus Torvalds 
259780d326faSPablo Neira Ayuso 		{
259880d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
25991137b5e2SHerbert Xu 				.start = link->start,
260080d326faSPablo Neira Ayuso 				.dump = link->dump,
260180d326faSPablo Neira Ayuso 				.done = link->done,
260280d326faSPablo Neira Ayuso 			};
260380d326faSPablo Neira Ayuso 			return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
260480d326faSPablo Neira Ayuso 		}
26051da177e4SLinus Torvalds 	}
26061da177e4SLinus Torvalds 
2607880a6fabSChristophe Gouault 	err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2608880a6fabSChristophe Gouault 			  link->nla_max ? : XFRMA_MAX,
2609fe52145fSJohannes Berg 			  link->nla_pol ? : xfrma_policy, extack);
2610a7bd9a45SThomas Graf 	if (err < 0)
2611a7bd9a45SThomas Graf 		return err;
26121da177e4SLinus Torvalds 
26131da177e4SLinus Torvalds 	if (link->doit == NULL)
26141d00a4ebSThomas Graf 		return -EINVAL;
26151da177e4SLinus Torvalds 
26165424f32eSThomas Graf 	return link->doit(skb, nlh, attrs);
26171da177e4SLinus Torvalds }
26181da177e4SLinus Torvalds 
2619cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
26201da177e4SLinus Torvalds {
2621283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2622283bc9f3SFan Du 
2623283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2624cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2625283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
26261da177e4SLinus Torvalds }
26271da177e4SLinus Torvalds 
2628a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void)
26297deb2264SThomas Graf {
26306f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
26316f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
26327deb2264SThomas Graf }
26337deb2264SThomas Graf 
2634214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
26351da177e4SLinus Torvalds {
26361da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
26371da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
26381d1e34ddSDavid S. Miller 	int err;
26391da177e4SLinus Torvalds 
264015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
264179b8b7f4SThomas Graf 	if (nlh == NULL)
264279b8b7f4SThomas Graf 		return -EMSGSIZE;
26431da177e4SLinus Torvalds 
26447b67c857SThomas Graf 	ue = nlmsg_data(nlh);
26451da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2646d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
2647e3e5fc16SMathias Krause 	/* clear the padding bytes */
2648e3e5fc16SMathias Krause 	memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
26491da177e4SLinus Torvalds 
26501d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
26511d1e34ddSDavid S. Miller 	if (err)
26521d1e34ddSDavid S. Miller 		return err;
26536f26b61eSJamal Hadi Salim 
2654053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2655053c095aSJohannes Berg 	return 0;
26561da177e4SLinus Torvalds }
26571da177e4SLinus Torvalds 
2658214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
26591da177e4SLinus Torvalds {
2660fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
26611da177e4SLinus Torvalds 	struct sk_buff *skb;
26621da177e4SLinus Torvalds 
26637deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
26641da177e4SLinus Torvalds 	if (skb == NULL)
26651da177e4SLinus Torvalds 		return -ENOMEM;
26661da177e4SLinus Torvalds 
26676f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
26686f26b61eSJamal Hadi Salim 		kfree_skb(skb);
26696f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
26706f26b61eSJamal Hadi Salim 	}
26711da177e4SLinus Torvalds 
267221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
26731da177e4SLinus Torvalds }
26741da177e4SLinus Torvalds 
2675214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2676d51d081dSJamal Hadi Salim {
2677fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2678d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
26792fc5f83bSGustavo A. R. Silva 	int err;
2680d51d081dSJamal Hadi Salim 
2681d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2682d51d081dSJamal Hadi Salim 	if (skb == NULL)
2683d51d081dSJamal Hadi Salim 		return -ENOMEM;
2684d51d081dSJamal Hadi Salim 
26852fc5f83bSGustavo A. R. Silva 	err = build_aevent(skb, x, c);
26862fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
2687d51d081dSJamal Hadi Salim 
268821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2689d51d081dSJamal Hadi Salim }
2690d51d081dSJamal Hadi Salim 
2691214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
269226b15dadSJamal Hadi Salim {
26937067802eSAlexey Dobriyan 	struct net *net = c->net;
269426b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
269526b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
269626b15dadSJamal Hadi Salim 	struct sk_buff *skb;
26977deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
269826b15dadSJamal Hadi Salim 
26997deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
270026b15dadSJamal Hadi Salim 	if (skb == NULL)
270126b15dadSJamal Hadi Salim 		return -ENOMEM;
270226b15dadSJamal Hadi Salim 
270315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
270479b8b7f4SThomas Graf 	if (nlh == NULL) {
270579b8b7f4SThomas Graf 		kfree_skb(skb);
270679b8b7f4SThomas Graf 		return -EMSGSIZE;
270779b8b7f4SThomas Graf 	}
270826b15dadSJamal Hadi Salim 
27097b67c857SThomas Graf 	p = nlmsg_data(nlh);
2710bf08867fSHerbert Xu 	p->proto = c->data.proto;
271126b15dadSJamal Hadi Salim 
27129825069dSThomas Graf 	nlmsg_end(skb, nlh);
271326b15dadSJamal Hadi Salim 
271421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
271526b15dadSJamal Hadi Salim }
271626b15dadSJamal Hadi Salim 
2717a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
271826b15dadSJamal Hadi Salim {
2719a1b831f2SAlexey Dobriyan 	unsigned int l = 0;
27201a6509d9SHerbert Xu 	if (x->aead)
27211a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
27224447bb33SMartin Willi 	if (x->aalg) {
27234447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
27244447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
27254447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
27264447bb33SMartin Willi 	}
272726b15dadSJamal Hadi Salim 	if (x->ealg)
27280f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
272926b15dadSJamal Hadi Salim 	if (x->calg)
27307deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
273126b15dadSJamal Hadi Salim 	if (x->encap)
27327deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
273335d2856bSMartin Willi 	if (x->tfcpad)
273435d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
2735d8647b79SSteffen Klassert 	if (x->replay_esn)
2736d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2737f293a5e3Sdingzhi 	else
2738f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
273968325d3bSHerbert Xu 	if (x->security)
274068325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
274168325d3bSHerbert Xu 				    x->security->ctx_len);
274268325d3bSHerbert Xu 	if (x->coaddr)
274368325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
2744a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
2745a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
2746d77e38e6SSteffen Klassert 	if (x->xso.dev)
2747d77e38e6SSteffen Klassert 		 l += nla_total_size(sizeof(x->xso));
2748*9b42c1f1SSteffen Klassert 	if (x->props.smark.v | x->props.smark.m) {
2749*9b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.v));
2750*9b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.m));
2751*9b42c1f1SSteffen Klassert 	}
275268325d3bSHerbert Xu 
2753d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2754de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
275526b15dadSJamal Hadi Salim 
275626b15dadSJamal Hadi Salim 	return l;
275726b15dadSJamal Hadi Salim }
275826b15dadSJamal Hadi Salim 
2759214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
276026b15dadSJamal Hadi Salim {
2761fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
276226b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
27630603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
276426b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
276526b15dadSJamal Hadi Salim 	struct sk_buff *skb;
2766a1b831f2SAlexey Dobriyan 	unsigned int len = xfrm_sa_len(x);
2767a1b831f2SAlexey Dobriyan 	unsigned int headlen;
2768a1b831f2SAlexey Dobriyan 	int err;
27690603eac0SHerbert Xu 
27700603eac0SHerbert Xu 	headlen = sizeof(*p);
27710603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
27727deb2264SThomas Graf 		len += nla_total_size(headlen);
27730603eac0SHerbert Xu 		headlen = sizeof(*id);
27746f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
27750603eac0SHerbert Xu 	}
27767deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
277726b15dadSJamal Hadi Salim 
27787deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
277926b15dadSJamal Hadi Salim 	if (skb == NULL)
278026b15dadSJamal Hadi Salim 		return -ENOMEM;
278126b15dadSJamal Hadi Salim 
278215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
27831d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
278479b8b7f4SThomas Graf 	if (nlh == NULL)
27851d1e34ddSDavid S. Miller 		goto out_free_skb;
278626b15dadSJamal Hadi Salim 
27877b67c857SThomas Graf 	p = nlmsg_data(nlh);
27880603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2789c0144beaSThomas Graf 		struct nlattr *attr;
2790c0144beaSThomas Graf 
27917b67c857SThomas Graf 		id = nlmsg_data(nlh);
279250329c8aSMathias Krause 		memset(id, 0, sizeof(*id));
27930603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
27940603eac0SHerbert Xu 		id->spi = x->id.spi;
27950603eac0SHerbert Xu 		id->family = x->props.family;
27960603eac0SHerbert Xu 		id->proto = x->id.proto;
27970603eac0SHerbert Xu 
2798c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
27991d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2800c0144beaSThomas Graf 		if (attr == NULL)
28011d1e34ddSDavid S. Miller 			goto out_free_skb;
2802c0144beaSThomas Graf 
2803c0144beaSThomas Graf 		p = nla_data(attr);
28040603eac0SHerbert Xu 	}
28051d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
28061d1e34ddSDavid S. Miller 	if (err)
28071d1e34ddSDavid S. Miller 		goto out_free_skb;
280826b15dadSJamal Hadi Salim 
28099825069dSThomas Graf 	nlmsg_end(skb, nlh);
281026b15dadSJamal Hadi Salim 
281121ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
281226b15dadSJamal Hadi Salim 
28131d1e34ddSDavid S. Miller out_free_skb:
281426b15dadSJamal Hadi Salim 	kfree_skb(skb);
28151d1e34ddSDavid S. Miller 	return err;
281626b15dadSJamal Hadi Salim }
281726b15dadSJamal Hadi Salim 
2818214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
281926b15dadSJamal Hadi Salim {
282026b15dadSJamal Hadi Salim 
282126b15dadSJamal Hadi Salim 	switch (c->event) {
2822f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
282326b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2824d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2825d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2826f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2827f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2828f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
282926b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2830f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
283126b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
283226b15dadSJamal Hadi Salim 	default:
283362db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
283462db5cfdSstephen hemminger 		       c->event);
283526b15dadSJamal Hadi Salim 		break;
283626b15dadSJamal Hadi Salim 	}
283726b15dadSJamal Hadi Salim 
283826b15dadSJamal Hadi Salim 	return 0;
283926b15dadSJamal Hadi Salim 
284026b15dadSJamal Hadi Salim }
284126b15dadSJamal Hadi Salim 
2842a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
28437deb2264SThomas Graf 						struct xfrm_policy *xp)
28447deb2264SThomas Graf {
28457deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
28467deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
28476f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
28487deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
28497deb2264SThomas Graf 	       + userpolicy_type_attrsize();
28507deb2264SThomas Graf }
28517deb2264SThomas Graf 
28521da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
285365e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
28541da177e4SLinus Torvalds {
28551d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
28561da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
28571da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
28581d1e34ddSDavid S. Miller 	int err;
28591da177e4SLinus Torvalds 
286079b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
286179b8b7f4SThomas Graf 	if (nlh == NULL)
286279b8b7f4SThomas Graf 		return -EMSGSIZE;
28631da177e4SLinus Torvalds 
28647b67c857SThomas Graf 	ua = nlmsg_data(nlh);
28651da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
28661da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
28671da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
286865e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
28691da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
28701da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
28711da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
28721da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
28731da177e4SLinus Torvalds 
28741d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
28751d1e34ddSDavid S. Miller 	if (!err)
28761d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
28771d1e34ddSDavid S. Miller 	if (!err)
28781d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
28791d1e34ddSDavid S. Miller 	if (!err)
28801d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
28811d1e34ddSDavid S. Miller 	if (err) {
28821d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
28831d1e34ddSDavid S. Miller 		return err;
28841d1e34ddSDavid S. Miller 	}
28851da177e4SLinus Torvalds 
2886053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2887053c095aSJohannes Berg 	return 0;
28881da177e4SLinus Torvalds }
28891da177e4SLinus Torvalds 
28901da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
289165e0736bSFan Du 			     struct xfrm_policy *xp)
28921da177e4SLinus Torvalds {
2893a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
28941da177e4SLinus Torvalds 	struct sk_buff *skb;
28952fc5f83bSGustavo A. R. Silva 	int err;
28961da177e4SLinus Torvalds 
28977deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
28981da177e4SLinus Torvalds 	if (skb == NULL)
28991da177e4SLinus Torvalds 		return -ENOMEM;
29001da177e4SLinus Torvalds 
29012fc5f83bSGustavo A. R. Silva 	err = build_acquire(skb, x, xt, xp);
29022fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
29031da177e4SLinus Torvalds 
290421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
29051da177e4SLinus Torvalds }
29061da177e4SLinus Torvalds 
29071da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
29081da177e4SLinus Torvalds  * or more templates.
29091da177e4SLinus Torvalds  */
2910cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
29111da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
29121da177e4SLinus Torvalds {
2913fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
29141da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
29151da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
29161da177e4SLinus Torvalds 	struct xfrm_policy *xp;
29171da177e4SLinus Torvalds 	int nr;
29181da177e4SLinus Torvalds 
2919cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
29201da177e4SLinus Torvalds 	case AF_INET:
29211da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
29221da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
29231da177e4SLinus Torvalds 			return NULL;
29241da177e4SLinus Torvalds 		}
29251da177e4SLinus Torvalds 		break;
2926dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
29271da177e4SLinus Torvalds 	case AF_INET6:
29281da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
29291da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
29301da177e4SLinus Torvalds 			return NULL;
29311da177e4SLinus Torvalds 		}
29321da177e4SLinus Torvalds 		break;
29331da177e4SLinus Torvalds #endif
29341da177e4SLinus Torvalds 	default:
29351da177e4SLinus Torvalds 		*dir = -EINVAL;
29361da177e4SLinus Torvalds 		return NULL;
29371da177e4SLinus Torvalds 	}
29381da177e4SLinus Torvalds 
29391da177e4SLinus Torvalds 	*dir = -EINVAL;
29401da177e4SLinus Torvalds 
29411da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
29421da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
29431da177e4SLinus Torvalds 		return NULL;
29441da177e4SLinus Torvalds 
29451da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
2946b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
29471da177e4SLinus Torvalds 		return NULL;
29481da177e4SLinus Torvalds 
2949a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
2950a4f1bac6SHerbert Xu 		return NULL;
2951a4f1bac6SHerbert Xu 
29522f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
29531da177e4SLinus Torvalds 	if (xp == NULL) {
29541da177e4SLinus Torvalds 		*dir = -ENOBUFS;
29551da177e4SLinus Torvalds 		return NULL;
29561da177e4SLinus Torvalds 	}
29571da177e4SLinus Torvalds 
29581da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
2959f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
29601da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
29611da177e4SLinus Torvalds 
29621da177e4SLinus Torvalds 	*dir = p->dir;
29631da177e4SLinus Torvalds 
29641da177e4SLinus Torvalds 	return xp;
29651da177e4SLinus Torvalds }
29661da177e4SLinus Torvalds 
2967a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
29687deb2264SThomas Graf {
29697deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
29707deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
29717deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2972295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
29737deb2264SThomas Graf 	       + userpolicy_type_attrsize();
29747deb2264SThomas Graf }
29757deb2264SThomas Graf 
29761da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2977214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
29781da177e4SLinus Torvalds {
29791da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
2980d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
29811d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
29821d1e34ddSDavid S. Miller 	int err;
29831da177e4SLinus Torvalds 
298415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
298579b8b7f4SThomas Graf 	if (nlh == NULL)
298679b8b7f4SThomas Graf 		return -EMSGSIZE;
29871da177e4SLinus Torvalds 
29887b67c857SThomas Graf 	upe = nlmsg_data(nlh);
29891da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
29901d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
29911d1e34ddSDavid S. Miller 	if (!err)
29921d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
29931d1e34ddSDavid S. Miller 	if (!err)
29941d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
29951d1e34ddSDavid S. Miller 	if (!err)
29961d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
29971d1e34ddSDavid S. Miller 	if (err) {
29981d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
29991d1e34ddSDavid S. Miller 		return err;
30001d1e34ddSDavid S. Miller 	}
30011da177e4SLinus Torvalds 	upe->hard = !!hard;
30021da177e4SLinus Torvalds 
3003053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3004053c095aSJohannes Berg 	return 0;
30051da177e4SLinus Torvalds }
30061da177e4SLinus Torvalds 
3007214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
30081da177e4SLinus Torvalds {
3009fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
30101da177e4SLinus Torvalds 	struct sk_buff *skb;
30112fc5f83bSGustavo A. R. Silva 	int err;
30121da177e4SLinus Torvalds 
30137deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
30141da177e4SLinus Torvalds 	if (skb == NULL)
30151da177e4SLinus Torvalds 		return -ENOMEM;
30161da177e4SLinus Torvalds 
30172fc5f83bSGustavo A. R. Silva 	err = build_polexpire(skb, xp, dir, c);
30182fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
30191da177e4SLinus Torvalds 
302021ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
30211da177e4SLinus Torvalds }
30221da177e4SLinus Torvalds 
3023214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
302426b15dadSJamal Hadi Salim {
3025a1b831f2SAlexey Dobriyan 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3026fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
302726b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
30280603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
302926b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
303026b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3031a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3032a1b831f2SAlexey Dobriyan 	int err;
30330603eac0SHerbert Xu 
30340603eac0SHerbert Xu 	headlen = sizeof(*p);
30350603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
30367deb2264SThomas Graf 		len += nla_total_size(headlen);
30370603eac0SHerbert Xu 		headlen = sizeof(*id);
30380603eac0SHerbert Xu 	}
3039cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
3040295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
30417deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
304226b15dadSJamal Hadi Salim 
30437deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
304426b15dadSJamal Hadi Salim 	if (skb == NULL)
304526b15dadSJamal Hadi Salim 		return -ENOMEM;
304626b15dadSJamal Hadi Salim 
304715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
30481d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
304979b8b7f4SThomas Graf 	if (nlh == NULL)
30501d1e34ddSDavid S. Miller 		goto out_free_skb;
305126b15dadSJamal Hadi Salim 
30527b67c857SThomas Graf 	p = nlmsg_data(nlh);
30530603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
3054c0144beaSThomas Graf 		struct nlattr *attr;
3055c0144beaSThomas Graf 
30567b67c857SThomas Graf 		id = nlmsg_data(nlh);
30570603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
30580603eac0SHerbert Xu 		id->dir = dir;
30590603eac0SHerbert Xu 		if (c->data.byid)
30600603eac0SHerbert Xu 			id->index = xp->index;
30610603eac0SHerbert Xu 		else
30620603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
30630603eac0SHerbert Xu 
3064c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
30651d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3066c0144beaSThomas Graf 		if (attr == NULL)
30671d1e34ddSDavid S. Miller 			goto out_free_skb;
3068c0144beaSThomas Graf 
3069c0144beaSThomas Graf 		p = nla_data(attr);
30700603eac0SHerbert Xu 	}
307126b15dadSJamal Hadi Salim 
307226b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
30731d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
30741d1e34ddSDavid S. Miller 	if (!err)
30751d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
30761d1e34ddSDavid S. Miller 	if (!err)
30771d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
30781d1e34ddSDavid S. Miller 	if (err)
30791d1e34ddSDavid S. Miller 		goto out_free_skb;
3080295fae56SJamal Hadi Salim 
30819825069dSThomas Graf 	nlmsg_end(skb, nlh);
308226b15dadSJamal Hadi Salim 
308321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
308426b15dadSJamal Hadi Salim 
30851d1e34ddSDavid S. Miller out_free_skb:
308626b15dadSJamal Hadi Salim 	kfree_skb(skb);
30871d1e34ddSDavid S. Miller 	return err;
308826b15dadSJamal Hadi Salim }
308926b15dadSJamal Hadi Salim 
3090214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
309126b15dadSJamal Hadi Salim {
30927067802eSAlexey Dobriyan 	struct net *net = c->net;
309326b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
309426b15dadSJamal Hadi Salim 	struct sk_buff *skb;
30951d1e34ddSDavid S. Miller 	int err;
309626b15dadSJamal Hadi Salim 
30977deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
309826b15dadSJamal Hadi Salim 	if (skb == NULL)
309926b15dadSJamal Hadi Salim 		return -ENOMEM;
310026b15dadSJamal Hadi Salim 
310115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
31021d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
310379b8b7f4SThomas Graf 	if (nlh == NULL)
31041d1e34ddSDavid S. Miller 		goto out_free_skb;
31051d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
31061d1e34ddSDavid S. Miller 	if (err)
31071d1e34ddSDavid S. Miller 		goto out_free_skb;
310826b15dadSJamal Hadi Salim 
31099825069dSThomas Graf 	nlmsg_end(skb, nlh);
311026b15dadSJamal Hadi Salim 
311121ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
311226b15dadSJamal Hadi Salim 
31131d1e34ddSDavid S. Miller out_free_skb:
311426b15dadSJamal Hadi Salim 	kfree_skb(skb);
31151d1e34ddSDavid S. Miller 	return err;
311626b15dadSJamal Hadi Salim }
311726b15dadSJamal Hadi Salim 
3118214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
311926b15dadSJamal Hadi Salim {
312026b15dadSJamal Hadi Salim 
312126b15dadSJamal Hadi Salim 	switch (c->event) {
3122f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
3123f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
3124f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
312526b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
3126f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
312726b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
3128f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
312926b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
313026b15dadSJamal Hadi Salim 	default:
313162db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
313262db5cfdSstephen hemminger 		       c->event);
313326b15dadSJamal Hadi Salim 	}
313426b15dadSJamal Hadi Salim 
313526b15dadSJamal Hadi Salim 	return 0;
313626b15dadSJamal Hadi Salim 
313726b15dadSJamal Hadi Salim }
313826b15dadSJamal Hadi Salim 
3139a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void)
31407deb2264SThomas Graf {
31417deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
31427deb2264SThomas Graf }
31437deb2264SThomas Graf 
314497a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
314597a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
314697a64b45SMasahide NAKAMURA {
314797a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
314897a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
314997a64b45SMasahide NAKAMURA 
315079b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
315179b8b7f4SThomas Graf 	if (nlh == NULL)
315279b8b7f4SThomas Graf 		return -EMSGSIZE;
315397a64b45SMasahide NAKAMURA 
31547b67c857SThomas Graf 	ur = nlmsg_data(nlh);
315597a64b45SMasahide NAKAMURA 	ur->proto = proto;
315697a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
315797a64b45SMasahide NAKAMURA 
31581d1e34ddSDavid S. Miller 	if (addr) {
31591d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
31601d1e34ddSDavid S. Miller 		if (err) {
31619825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
31621d1e34ddSDavid S. Miller 			return err;
31631d1e34ddSDavid S. Miller 		}
31641d1e34ddSDavid S. Miller 	}
3165053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3166053c095aSJohannes Berg 	return 0;
316797a64b45SMasahide NAKAMURA }
316897a64b45SMasahide NAKAMURA 
3169db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3170db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
317197a64b45SMasahide NAKAMURA {
317297a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
31732fc5f83bSGustavo A. R. Silva 	int err;
317497a64b45SMasahide NAKAMURA 
31757deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
317697a64b45SMasahide NAKAMURA 	if (skb == NULL)
317797a64b45SMasahide NAKAMURA 		return -ENOMEM;
317897a64b45SMasahide NAKAMURA 
31792fc5f83bSGustavo A. R. Silva 	err = build_report(skb, proto, sel, addr);
31802fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
318197a64b45SMasahide NAKAMURA 
318221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
318397a64b45SMasahide NAKAMURA }
318497a64b45SMasahide NAKAMURA 
3185a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void)
31863a2dfbe8SMartin Willi {
31873a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
31883a2dfbe8SMartin Willi }
31893a2dfbe8SMartin Willi 
31903a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
31913a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
31923a2dfbe8SMartin Willi {
31933a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
31943a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
31953a2dfbe8SMartin Willi 
31963a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
31973a2dfbe8SMartin Willi 	if (nlh == NULL)
31983a2dfbe8SMartin Willi 		return -EMSGSIZE;
31993a2dfbe8SMartin Willi 
32003a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
32013a2dfbe8SMartin Willi 
32023a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
32033a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
32043a2dfbe8SMartin Willi 	um->id.family = x->props.family;
32053a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
32063a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
32073a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
32083a2dfbe8SMartin Willi 	um->new_sport = new_sport;
32093a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
32103a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
32113a2dfbe8SMartin Willi 
3212053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3213053c095aSJohannes Berg 	return 0;
32143a2dfbe8SMartin Willi }
32153a2dfbe8SMartin Willi 
32163a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
32173a2dfbe8SMartin Willi 			     __be16 sport)
32183a2dfbe8SMartin Willi {
3219a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
32203a2dfbe8SMartin Willi 	struct sk_buff *skb;
32212fc5f83bSGustavo A. R. Silva 	int err;
32223a2dfbe8SMartin Willi 
32233a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
32243a2dfbe8SMartin Willi 		return -EINVAL;
32253a2dfbe8SMartin Willi 
32263a2dfbe8SMartin Willi 	if (!x->encap)
32273a2dfbe8SMartin Willi 		return -EINVAL;
32283a2dfbe8SMartin Willi 
32293a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
32303a2dfbe8SMartin Willi 	if (skb == NULL)
32313a2dfbe8SMartin Willi 		return -ENOMEM;
32323a2dfbe8SMartin Willi 
32332fc5f83bSGustavo A. R. Silva 	err = build_mapping(skb, x, ipaddr, sport);
32342fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
32353a2dfbe8SMartin Willi 
323621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
32373a2dfbe8SMartin Willi }
32383a2dfbe8SMartin Willi 
32390f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
32400f24558eSHoria Geanta {
32410f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
32420f24558eSHoria Geanta }
32430f24558eSHoria Geanta 
32441da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
32451da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
32461da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
32471da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
32481da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
324997a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
32505c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
32513a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
32520f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
32531da177e4SLinus Torvalds };
32541da177e4SLinus Torvalds 
3255a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
32561da177e4SLinus Torvalds {
3257be33690dSPatrick McHardy 	struct sock *nlsk;
3258a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3259a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3260a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3261a31f2d17SPablo Neira Ayuso 	};
3262be33690dSPatrick McHardy 
32639f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3264be33690dSPatrick McHardy 	if (nlsk == NULL)
32651da177e4SLinus Torvalds 		return -ENOMEM;
3266d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3267cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
32681da177e4SLinus Torvalds 	return 0;
32691da177e4SLinus Torvalds }
32701da177e4SLinus Torvalds 
3271d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3272a6483b79SAlexey Dobriyan {
3273d79d792eSEric W. Biederman 	struct net *net;
3274d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3275a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3276d79d792eSEric W. Biederman 	synchronize_net();
3277d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3278d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3279a6483b79SAlexey Dobriyan }
3280a6483b79SAlexey Dobriyan 
3281a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3282a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
3283d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3284a6483b79SAlexey Dobriyan };
3285a6483b79SAlexey Dobriyan 
3286a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3287a6483b79SAlexey Dobriyan {
3288a6483b79SAlexey Dobriyan 	int rv;
3289a6483b79SAlexey Dobriyan 
3290a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3291a6483b79SAlexey Dobriyan 
3292a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3293a6483b79SAlexey Dobriyan 	if (rv < 0)
3294a6483b79SAlexey Dobriyan 		return rv;
3295a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3296a6483b79SAlexey Dobriyan 	if (rv < 0)
3297a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3298a6483b79SAlexey Dobriyan 	return rv;
3299a6483b79SAlexey Dobriyan }
3300a6483b79SAlexey Dobriyan 
33011da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
33021da177e4SLinus Torvalds {
33031da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3304a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
33051da177e4SLinus Torvalds }
33061da177e4SLinus Torvalds 
33071da177e4SLinus Torvalds module_init(xfrm_user_init);
33081da177e4SLinus Torvalds module_exit(xfrm_user_exit);
33091da177e4SLinus Torvalds MODULE_LICENSE("GPL");
33104fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3311f8cd5488SJamal Hadi Salim 
3312