xref: /linux/net/xfrm/xfrm_user.c (revision 86126b77dcd551ce223e7293bb55854e3df05646)
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 
530fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
531fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
5325424f32eSThomas Graf 					       struct nlattr **attrs,
5331da177e4SLinus Torvalds 					       int *errp)
5341da177e4SLinus Torvalds {
535fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
5361da177e4SLinus Torvalds 	int err = -ENOMEM;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	if (!x)
5391da177e4SLinus Torvalds 		goto error_no_put;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	copy_from_user_state(x, p);
5421da177e4SLinus Torvalds 
543a947b0a9SNicolas Dichtel 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
544a947b0a9SNicolas Dichtel 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
545a947b0a9SNicolas Dichtel 
54669b0137fSHerbert Xu 	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
5471a6509d9SHerbert Xu 		goto error;
5484447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
5494447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
5504447bb33SMartin Willi 		goto error;
5514447bb33SMartin Willi 	if (!x->props.aalgo) {
5524447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
55335a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
5541da177e4SLinus Torvalds 			goto error;
5554447bb33SMartin Willi 	}
55669b0137fSHerbert Xu 	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
5571da177e4SLinus Torvalds 		goto error;
5581da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
5591da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
56035a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
5611da177e4SLinus Torvalds 		goto error;
562fd21150aSThomas Graf 
563fd21150aSThomas Graf 	if (attrs[XFRMA_ENCAP]) {
564fd21150aSThomas Graf 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
565fd21150aSThomas Graf 				   sizeof(*x->encap), GFP_KERNEL);
566fd21150aSThomas Graf 		if (x->encap == NULL)
5671da177e4SLinus Torvalds 			goto error;
568fd21150aSThomas Graf 	}
569fd21150aSThomas Graf 
57035d2856bSMartin Willi 	if (attrs[XFRMA_TFCPAD])
57135d2856bSMartin Willi 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
57235d2856bSMartin Willi 
573fd21150aSThomas Graf 	if (attrs[XFRMA_COADDR]) {
574fd21150aSThomas Graf 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
575fd21150aSThomas Graf 				    sizeof(*x->coaddr), GFP_KERNEL);
576fd21150aSThomas Graf 		if (x->coaddr == NULL)
577060f02a3SNoriaki TAKAMIYA 			goto error;
578fd21150aSThomas Graf 	}
579fd21150aSThomas Graf 
5806f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &x->mark);
5816f26b61eSJamal Hadi Salim 
582077fbac4SLorenzo Colitti 	if (attrs[XFRMA_OUTPUT_MARK])
583077fbac4SLorenzo Colitti 		x->props.output_mark = nla_get_u32(attrs[XFRMA_OUTPUT_MARK]);
584077fbac4SLorenzo Colitti 
585ffdb5211SIlan Tayari 	err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
5861da177e4SLinus Torvalds 	if (err)
5871da177e4SLinus Torvalds 		goto error;
5881da177e4SLinus Torvalds 
5892f30ea50SMathias Krause 	if (attrs[XFRMA_SEC_CTX]) {
5902f30ea50SMathias Krause 		err = security_xfrm_state_alloc(x,
5912f30ea50SMathias Krause 						nla_data(attrs[XFRMA_SEC_CTX]));
5922f30ea50SMathias Krause 		if (err)
593df71837dSTrent Jaeger 			goto error;
5942f30ea50SMathias Krause 	}
595df71837dSTrent Jaeger 
596d8647b79SSteffen Klassert 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
597d8647b79SSteffen Klassert 					       attrs[XFRMA_REPLAY_ESN_VAL])))
598d8647b79SSteffen Klassert 		goto error;
599d8647b79SSteffen Klassert 
6001da177e4SLinus Torvalds 	x->km.seq = p->seq;
601b27aeadbSAlexey Dobriyan 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
602d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
603b27aeadbSAlexey Dobriyan 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
604d51d081dSJamal Hadi Salim 
6059fdc4883SSteffen Klassert 	if ((err = xfrm_init_replay(x)))
6069fdc4883SSteffen Klassert 		goto error;
607d51d081dSJamal Hadi Salim 
6089fdc4883SSteffen Klassert 	/* override default values from above */
609e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 0);
6101da177e4SLinus Torvalds 
611cc01572eSYossi Kuperman 	/* configure the hardware if offload is requested */
612cc01572eSYossi Kuperman 	if (attrs[XFRMA_OFFLOAD_DEV]) {
613cc01572eSYossi Kuperman 		err = xfrm_dev_state_add(net, x,
614cc01572eSYossi Kuperman 					 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
615cc01572eSYossi Kuperman 		if (err)
616cc01572eSYossi Kuperman 			goto error;
617cc01572eSYossi Kuperman 	}
618cc01572eSYossi Kuperman 
6191da177e4SLinus Torvalds 	return x;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds error:
6221da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
6231da177e4SLinus Torvalds 	xfrm_state_put(x);
6241da177e4SLinus Torvalds error_no_put:
6251da177e4SLinus Torvalds 	*errp = err;
6261da177e4SLinus Torvalds 	return NULL;
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
62922e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
6305424f32eSThomas Graf 		struct nlattr **attrs)
6311da177e4SLinus Torvalds {
632fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6337b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
6341da177e4SLinus Torvalds 	struct xfrm_state *x;
6351da177e4SLinus Torvalds 	int err;
63626b15dadSJamal Hadi Salim 	struct km_event c;
6371da177e4SLinus Torvalds 
63835a7aa08SThomas Graf 	err = verify_newsa_info(p, attrs);
6391da177e4SLinus Torvalds 	if (err)
6401da177e4SLinus Torvalds 		return err;
6411da177e4SLinus Torvalds 
642fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
6431da177e4SLinus Torvalds 	if (!x)
6441da177e4SLinus Torvalds 		return err;
6451da177e4SLinus Torvalds 
64626b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
6471da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
6481da177e4SLinus Torvalds 		err = xfrm_state_add(x);
6491da177e4SLinus Torvalds 	else
6501da177e4SLinus Torvalds 		err = xfrm_state_update(x);
6511da177e4SLinus Torvalds 
6522e71029eSTetsuo Handa 	xfrm_audit_state_add(x, err ? 0 : 1, true);
653161a09e7SJoy Latten 
6541da177e4SLinus Torvalds 	if (err < 0) {
6551da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
656c5d4d7d8SSteffen Klassert 		xfrm_dev_state_delete(x);
65721380b81SHerbert Xu 		__xfrm_state_put(x);
6587d6dfe1fSPatrick McHardy 		goto out;
6591da177e4SLinus Torvalds 	}
6601da177e4SLinus Torvalds 
661cc01572eSYossi Kuperman 	if (x->km.state == XFRM_STATE_VOID)
662cc01572eSYossi Kuperman 		x->km.state = XFRM_STATE_VALID;
663cc01572eSYossi Kuperman 
66426b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
66515e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
666f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
66726b15dadSJamal Hadi Salim 
66826b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
6697d6dfe1fSPatrick McHardy out:
67026b15dadSJamal Hadi Salim 	xfrm_state_put(x);
6711da177e4SLinus Torvalds 	return err;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
674fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
675fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
6765424f32eSThomas Graf 						 struct nlattr **attrs,
677eb2971b6SMasahide NAKAMURA 						 int *errp)
678eb2971b6SMasahide NAKAMURA {
679eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
6806f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
681eb2971b6SMasahide NAKAMURA 	int err;
6826f26b61eSJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
683eb2971b6SMasahide NAKAMURA 
684eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
685eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
6866f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
687eb2971b6SMasahide NAKAMURA 	} else {
688eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
689eb2971b6SMasahide NAKAMURA 
69035a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
691eb2971b6SMasahide NAKAMURA 		if (!saddr) {
692eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
693eb2971b6SMasahide NAKAMURA 			goto out;
694eb2971b6SMasahide NAKAMURA 		}
695eb2971b6SMasahide NAKAMURA 
6969abbffeeSMasahide NAKAMURA 		err = -ESRCH;
6976f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup_byaddr(net, mark,
6986f26b61eSJamal Hadi Salim 					     &p->daddr, saddr,
699221df1edSAlexey Dobriyan 					     p->proto, p->family);
700eb2971b6SMasahide NAKAMURA 	}
701eb2971b6SMasahide NAKAMURA 
702eb2971b6SMasahide NAKAMURA  out:
703eb2971b6SMasahide NAKAMURA 	if (!x && errp)
704eb2971b6SMasahide NAKAMURA 		*errp = err;
705eb2971b6SMasahide NAKAMURA 	return x;
706eb2971b6SMasahide NAKAMURA }
707eb2971b6SMasahide NAKAMURA 
70822e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
7095424f32eSThomas Graf 		struct nlattr **attrs)
7101da177e4SLinus Torvalds {
711fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
7121da177e4SLinus Torvalds 	struct xfrm_state *x;
713eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
71426b15dadSJamal Hadi Salim 	struct km_event c;
7157b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
7161da177e4SLinus Torvalds 
717fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
7181da177e4SLinus Torvalds 	if (x == NULL)
719eb2971b6SMasahide NAKAMURA 		return err;
7201da177e4SLinus Torvalds 
7216f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
722c8c05a8eSCatherine Zhang 		goto out;
723c8c05a8eSCatherine Zhang 
7241da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
725c8c05a8eSCatherine Zhang 		err = -EPERM;
726c8c05a8eSCatherine Zhang 		goto out;
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 
72926b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
730161a09e7SJoy Latten 
731c8c05a8eSCatherine Zhang 	if (err < 0)
732c8c05a8eSCatherine Zhang 		goto out;
73326b15dadSJamal Hadi Salim 
73426b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
73515e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
736f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
73726b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7381da177e4SLinus Torvalds 
739c8c05a8eSCatherine Zhang out:
7402e71029eSTetsuo Handa 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
741c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
74226b15dadSJamal Hadi Salim 	return err;
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
7461da177e4SLinus Torvalds {
747f778a636SMathias Krause 	memset(p, 0, sizeof(*p));
7481da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
7491da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
7501da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
7511da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
752e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
753e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay, &p->stats.replay);
754e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
75554489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
7561da177e4SLinus Torvalds 	p->mode = x->props.mode;
7571da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
7581da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
7591da177e4SLinus Torvalds 	p->family = x->props.family;
7601da177e4SLinus Torvalds 	p->flags = x->props.flags;
7611da177e4SLinus Torvalds 	p->seq = x->km.seq;
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds struct xfrm_dump_info {
7651da177e4SLinus Torvalds 	struct sk_buff *in_skb;
7661da177e4SLinus Torvalds 	struct sk_buff *out_skb;
7671da177e4SLinus Torvalds 	u32 nlmsg_seq;
7681da177e4SLinus Torvalds 	u16 nlmsg_flags;
7691da177e4SLinus Torvalds };
7701da177e4SLinus Torvalds 
771c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
772c0144beaSThomas Graf {
773c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
774c0144beaSThomas Graf 	struct nlattr *attr;
77568325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
776c0144beaSThomas Graf 
777c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
778c0144beaSThomas Graf 	if (attr == NULL)
779c0144beaSThomas Graf 		return -EMSGSIZE;
780c0144beaSThomas Graf 
781c0144beaSThomas Graf 	uctx = nla_data(attr);
782c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
783c0144beaSThomas Graf 	uctx->len = ctx_size;
784c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
785c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
786c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
787c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
788c0144beaSThomas Graf 
789c0144beaSThomas Graf 	return 0;
790c0144beaSThomas Graf }
791c0144beaSThomas Graf 
792d77e38e6SSteffen Klassert static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
793d77e38e6SSteffen Klassert {
794d77e38e6SSteffen Klassert 	struct xfrm_user_offload *xuo;
795d77e38e6SSteffen Klassert 	struct nlattr *attr;
796d77e38e6SSteffen Klassert 
797d77e38e6SSteffen Klassert 	attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
798d77e38e6SSteffen Klassert 	if (attr == NULL)
799d77e38e6SSteffen Klassert 		return -EMSGSIZE;
800d77e38e6SSteffen Klassert 
801d77e38e6SSteffen Klassert 	xuo = nla_data(attr);
8025fe0d4bdSMathias Krause 	memset(xuo, 0, sizeof(*xuo));
803d77e38e6SSteffen Klassert 	xuo->ifindex = xso->dev->ifindex;
804d77e38e6SSteffen Klassert 	xuo->flags = xso->flags;
805d77e38e6SSteffen Klassert 
806d77e38e6SSteffen Klassert 	return 0;
807d77e38e6SSteffen Klassert }
808d77e38e6SSteffen Klassert 
8094447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
8104447bb33SMartin Willi {
8114447bb33SMartin Willi 	struct xfrm_algo *algo;
8124447bb33SMartin Willi 	struct nlattr *nla;
8134447bb33SMartin Willi 
8144447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
8154447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
8164447bb33SMartin Willi 	if (!nla)
8174447bb33SMartin Willi 		return -EMSGSIZE;
8184447bb33SMartin Willi 
8194447bb33SMartin Willi 	algo = nla_data(nla);
8204c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
8214447bb33SMartin Willi 	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
8224447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
8234447bb33SMartin Willi 
8244447bb33SMartin Willi 	return 0;
8254447bb33SMartin Willi }
8264447bb33SMartin Willi 
82768325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
82868325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
82968325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
83068325d3bSHerbert Xu 				    struct sk_buff *skb)
8311da177e4SLinus Torvalds {
8321d1e34ddSDavid S. Miller 	int ret = 0;
8331d1e34ddSDavid S. Miller 
8341da177e4SLinus Torvalds 	copy_to_user_state(x, p);
8351da177e4SLinus Torvalds 
836a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
837a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
838a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
839a947b0a9SNicolas Dichtel 		if (ret)
840a947b0a9SNicolas Dichtel 			goto out;
841a947b0a9SNicolas Dichtel 	}
842a947b0a9SNicolas Dichtel 
8431d1e34ddSDavid S. Miller 	if (x->coaddr) {
8441d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
8451d1e34ddSDavid S. Miller 		if (ret)
8461d1e34ddSDavid S. Miller 			goto out;
8471d1e34ddSDavid S. Miller 	}
8481d1e34ddSDavid S. Miller 	if (x->lastused) {
849de95c4a4SNicolas Dichtel 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
850de95c4a4SNicolas Dichtel 					XFRMA_PAD);
8511d1e34ddSDavid S. Miller 		if (ret)
8521d1e34ddSDavid S. Miller 			goto out;
8531d1e34ddSDavid S. Miller 	}
8541d1e34ddSDavid S. Miller 	if (x->aead) {
8551d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
8561d1e34ddSDavid S. Miller 		if (ret)
8571d1e34ddSDavid S. Miller 			goto out;
8581d1e34ddSDavid S. Miller 	}
8591d1e34ddSDavid S. Miller 	if (x->aalg) {
8601d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
8611d1e34ddSDavid S. Miller 		if (!ret)
8621d1e34ddSDavid S. Miller 			ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
8631d1e34ddSDavid S. Miller 				      xfrm_alg_auth_len(x->aalg), x->aalg);
8641d1e34ddSDavid S. Miller 		if (ret)
8651d1e34ddSDavid S. Miller 			goto out;
8661d1e34ddSDavid S. Miller 	}
8671d1e34ddSDavid S. Miller 	if (x->ealg) {
8681d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
8691d1e34ddSDavid S. Miller 		if (ret)
8701d1e34ddSDavid S. Miller 			goto out;
8711d1e34ddSDavid S. Miller 	}
8721d1e34ddSDavid S. Miller 	if (x->calg) {
8731d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
8741d1e34ddSDavid S. Miller 		if (ret)
8751d1e34ddSDavid S. Miller 			goto out;
8761d1e34ddSDavid S. Miller 	}
8771d1e34ddSDavid S. Miller 	if (x->encap) {
8781d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
8791d1e34ddSDavid S. Miller 		if (ret)
8801d1e34ddSDavid S. Miller 			goto out;
8811d1e34ddSDavid S. Miller 	}
8821d1e34ddSDavid S. Miller 	if (x->tfcpad) {
8831d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
8841d1e34ddSDavid S. Miller 		if (ret)
8851d1e34ddSDavid S. Miller 			goto out;
8861d1e34ddSDavid S. Miller 	}
8871d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
8881d1e34ddSDavid S. Miller 	if (ret)
8891d1e34ddSDavid S. Miller 		goto out;
890f293a5e3Sdingzhi 	if (x->replay_esn)
8911d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
892d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
8931d1e34ddSDavid S. Miller 			      x->replay_esn);
894f293a5e3Sdingzhi 	else
895f293a5e3Sdingzhi 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
896f293a5e3Sdingzhi 			      &x->replay);
8971d1e34ddSDavid S. Miller 	if (ret)
8981d1e34ddSDavid S. Miller 		goto out;
899d77e38e6SSteffen Klassert 	if(x->xso.dev)
900d77e38e6SSteffen Klassert 		ret = copy_user_offload(&x->xso, skb);
901d77e38e6SSteffen Klassert 	if (ret)
902d77e38e6SSteffen Klassert 		goto out;
903077fbac4SLorenzo Colitti 	if (x->props.output_mark) {
904077fbac4SLorenzo Colitti 		ret = nla_put_u32(skb, XFRMA_OUTPUT_MARK, x->props.output_mark);
905077fbac4SLorenzo Colitti 		if (ret)
906077fbac4SLorenzo Colitti 			goto out;
907077fbac4SLorenzo Colitti 	}
9088598112dSSteffen Klassert 	if (x->security)
9098598112dSSteffen Klassert 		ret = copy_sec_ctx(x->security, skb);
9101d1e34ddSDavid S. Miller out:
9111d1e34ddSDavid S. Miller 	return ret;
91268325d3bSHerbert Xu }
91368325d3bSHerbert Xu 
91468325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
91568325d3bSHerbert Xu {
91668325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
91768325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
91868325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
91968325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
92068325d3bSHerbert Xu 	struct nlmsghdr *nlh;
92168325d3bSHerbert Xu 	int err;
92268325d3bSHerbert Xu 
92315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
92468325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
92568325d3bSHerbert Xu 	if (nlh == NULL)
92668325d3bSHerbert Xu 		return -EMSGSIZE;
92768325d3bSHerbert Xu 
92868325d3bSHerbert Xu 	p = nlmsg_data(nlh);
92968325d3bSHerbert Xu 
93068325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
9311d1e34ddSDavid S. Miller 	if (err) {
9329825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
93368325d3bSHerbert Xu 		return err;
9341da177e4SLinus Torvalds 	}
9351d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
9361d1e34ddSDavid S. Miller 	return 0;
9371d1e34ddSDavid S. Miller }
9381da177e4SLinus Torvalds 
9394c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
9404c563f76STimo Teras {
9414c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
942283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
943283bc9f3SFan Du 	struct net *net = sock_net(sk);
944283bc9f3SFan Du 
9451ba5bf99SVegard Nossum 	if (cb->args[0])
946283bc9f3SFan Du 		xfrm_state_walk_done(walk, net);
9474c563f76STimo Teras 	return 0;
9484c563f76STimo Teras }
9494c563f76STimo Teras 
950d3623099SNicolas Dichtel static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
9511da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
9521da177e4SLinus Torvalds {
953fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
9544c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
9551da177e4SLinus Torvalds 	struct xfrm_dump_info info;
9561da177e4SLinus Torvalds 
9574c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
9584c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
9594c563f76STimo Teras 
9601da177e4SLinus Torvalds 	info.in_skb = cb->skb;
9611da177e4SLinus Torvalds 	info.out_skb = skb;
9621da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
9631da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
9644c563f76STimo Teras 
9654c563f76STimo Teras 	if (!cb->args[0]) {
966d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
967870a2df4SNicolas Dichtel 		struct xfrm_address_filter *filter = NULL;
968d3623099SNicolas Dichtel 		u8 proto = 0;
969d3623099SNicolas Dichtel 		int err;
970d3623099SNicolas Dichtel 
971fceb6435SJohannes Berg 		err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX, xfrma_policy,
972fceb6435SJohannes Berg 				  NULL);
973d3623099SNicolas Dichtel 		if (err < 0)
974d3623099SNicolas Dichtel 			return err;
975d3623099SNicolas Dichtel 
976870a2df4SNicolas Dichtel 		if (attrs[XFRMA_ADDRESS_FILTER]) {
977df367561SAndrzej Hajda 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
978df367561SAndrzej Hajda 					 sizeof(*filter), GFP_KERNEL);
979d3623099SNicolas Dichtel 			if (filter == NULL)
980d3623099SNicolas Dichtel 				return -ENOMEM;
981d3623099SNicolas Dichtel 		}
982d3623099SNicolas Dichtel 
983d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
984d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
985d3623099SNicolas Dichtel 
986d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
9871ba5bf99SVegard Nossum 		cb->args[0] = 1;
9884c563f76STimo Teras 	}
9894c563f76STimo Teras 
990fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	return skb->len;
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
9961da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
9971da177e4SLinus Torvalds {
9981da177e4SLinus Torvalds 	struct xfrm_dump_info info;
9991da177e4SLinus Torvalds 	struct sk_buff *skb;
1000864745d2SMathias Krause 	int err;
10011da177e4SLinus Torvalds 
10027deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
10031da177e4SLinus Torvalds 	if (!skb)
10041da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	info.in_skb = in_skb;
10071da177e4SLinus Torvalds 	info.out_skb = skb;
10081da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
10091da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
10101da177e4SLinus Torvalds 
1011864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
1012864745d2SMathias Krause 	if (err) {
10131da177e4SLinus Torvalds 		kfree_skb(skb);
1014864745d2SMathias Krause 		return ERR_PTR(err);
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	return skb;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
102021ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
102121ee543eSMichal Kubecek  * Must be called with RCU read lock.
102221ee543eSMichal Kubecek  */
102321ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
102421ee543eSMichal Kubecek 				       u32 pid, unsigned int group)
102521ee543eSMichal Kubecek {
102621ee543eSMichal Kubecek 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
102721ee543eSMichal Kubecek 
1028*86126b77SFlorian Westphal 	if (!nlsk) {
1029*86126b77SFlorian Westphal 		kfree_skb(skb);
1030*86126b77SFlorian Westphal 		return -EPIPE;
1031*86126b77SFlorian Westphal 	}
1032*86126b77SFlorian Westphal 
103321ee543eSMichal Kubecek 	return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
103421ee543eSMichal Kubecek }
103521ee543eSMichal Kubecek 
1036a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void)
10377deb2264SThomas Graf {
10387deb2264SThomas Graf 	return NLMSG_ALIGN(4)
10397deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1040880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1041880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1042880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
10437deb2264SThomas Graf }
10447deb2264SThomas Graf 
1045e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
104615e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
1047ecfd6b18SJamal Hadi Salim {
10485a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
10495a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
10505a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1051880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1052ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
10531d1e34ddSDavid S. Miller 	int err;
1054ecfd6b18SJamal Hadi Salim 	u32 *f;
1055880a6fabSChristophe Gouault 	unsigned lseq;
1056ecfd6b18SJamal Hadi Salim 
105715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
105825985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1059ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1060ecfd6b18SJamal Hadi Salim 
1061ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1062ecfd6b18SJamal Hadi Salim 	*f = flags;
1063e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
10645a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
10655a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
10665a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
10675a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
10685a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
10695a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
10705a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
10715a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1072ecfd6b18SJamal Hadi Salim 
1073880a6fabSChristophe Gouault 	do {
1074880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1075880a6fabSChristophe Gouault 
1076880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1077880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1078880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1079880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1080880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1081880a6fabSChristophe Gouault 
10821d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
10831d1e34ddSDavid S. Miller 	if (!err)
10841d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1085880a6fabSChristophe Gouault 	if (!err)
1086880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1087880a6fabSChristophe Gouault 	if (!err)
1088880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
10891d1e34ddSDavid S. Miller 	if (err) {
10901d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
10911d1e34ddSDavid S. Miller 		return err;
10921d1e34ddSDavid S. Miller 	}
1093ecfd6b18SJamal Hadi Salim 
1094053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1095053c095aSJohannes Berg 	return 0;
1096ecfd6b18SJamal Hadi Salim }
1097ecfd6b18SJamal Hadi Salim 
1098880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1099880a6fabSChristophe Gouault 			    struct nlattr **attrs)
1100880a6fabSChristophe Gouault {
1101880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1102880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1103880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1104880a6fabSChristophe Gouault 
1105880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1106880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1107880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1108880a6fabSChristophe Gouault 
1109880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1110880a6fabSChristophe Gouault 			return -EINVAL;
1111880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1112880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1113880a6fabSChristophe Gouault 			return -EINVAL;
1114880a6fabSChristophe Gouault 	}
1115880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1116880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1117880a6fabSChristophe Gouault 
1118880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1119880a6fabSChristophe Gouault 			return -EINVAL;
1120880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1121880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1122880a6fabSChristophe Gouault 			return -EINVAL;
1123880a6fabSChristophe Gouault 	}
1124880a6fabSChristophe Gouault 
1125880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1126880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1127880a6fabSChristophe Gouault 		if (thresh4) {
1128880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1129880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1130880a6fabSChristophe Gouault 		}
1131880a6fabSChristophe Gouault 		if (thresh6) {
1132880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1133880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1134880a6fabSChristophe Gouault 		}
1135880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1136880a6fabSChristophe Gouault 
1137880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1138880a6fabSChristophe Gouault 	}
1139880a6fabSChristophe Gouault 
1140880a6fabSChristophe Gouault 	return 0;
1141880a6fabSChristophe Gouault }
1142880a6fabSChristophe Gouault 
1143ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
11445424f32eSThomas Graf 		struct nlattr **attrs)
1145ecfd6b18SJamal Hadi Salim {
1146a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1147ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
11487b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
114915e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1150ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
11512fc5f83bSGustavo A. R. Silva 	int err;
1152ecfd6b18SJamal Hadi Salim 
11537deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1154ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1155ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1156ecfd6b18SJamal Hadi Salim 
11572fc5f83bSGustavo A. R. Silva 	err = build_spdinfo(r_skb, net, sportid, seq, *flags);
11582fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
1159ecfd6b18SJamal Hadi Salim 
116015e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1161ecfd6b18SJamal Hadi Salim }
1162ecfd6b18SJamal Hadi Salim 
1163a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void)
11647deb2264SThomas Graf {
11657deb2264SThomas Graf 	return NLMSG_ALIGN(4)
11667deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
11677deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
11687deb2264SThomas Graf }
11697deb2264SThomas Graf 
1170e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
117115e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
117228d8909bSJamal Hadi Salim {
1173af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1174af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
117528d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
11761d1e34ddSDavid S. Miller 	int err;
117728d8909bSJamal Hadi Salim 	u32 *f;
117828d8909bSJamal Hadi Salim 
117915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
118025985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
118128d8909bSJamal Hadi Salim 		return -EMSGSIZE;
118228d8909bSJamal Hadi Salim 
118328d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
118428d8909bSJamal Hadi Salim 	*f = flags;
1185e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
118628d8909bSJamal Hadi Salim 
1187af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1188af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1189af11e316SJamal Hadi Salim 
11901d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
11911d1e34ddSDavid S. Miller 	if (!err)
11921d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
11931d1e34ddSDavid S. Miller 	if (err) {
11941d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
11951d1e34ddSDavid S. Miller 		return err;
11961d1e34ddSDavid S. Miller 	}
119728d8909bSJamal Hadi Salim 
1198053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1199053c095aSJohannes Berg 	return 0;
120028d8909bSJamal Hadi Salim }
120128d8909bSJamal Hadi Salim 
120228d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12035424f32eSThomas Graf 		struct nlattr **attrs)
120428d8909bSJamal Hadi Salim {
1205a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
120628d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
12077b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
120815e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
120928d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
12102fc5f83bSGustavo A. R. Silva 	int err;
121128d8909bSJamal Hadi Salim 
12127deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
121328d8909bSJamal Hadi Salim 	if (r_skb == NULL)
121428d8909bSJamal Hadi Salim 		return -ENOMEM;
121528d8909bSJamal Hadi Salim 
12162fc5f83bSGustavo A. R. Silva 	err = build_sadinfo(r_skb, net, sportid, seq, *flags);
12172fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
121828d8909bSJamal Hadi Salim 
121915e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
122028d8909bSJamal Hadi Salim }
122128d8909bSJamal Hadi Salim 
122222e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
12235424f32eSThomas Graf 		struct nlattr **attrs)
12241da177e4SLinus Torvalds {
1225fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12267b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
12271da177e4SLinus Torvalds 	struct xfrm_state *x;
12281da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1229eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
12301da177e4SLinus Torvalds 
1231fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
12321da177e4SLinus Torvalds 	if (x == NULL)
12331da177e4SLinus Torvalds 		goto out_noput;
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
12361da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
12371da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
12381da177e4SLinus Torvalds 	} else {
123915e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 	xfrm_state_put(x);
12421da177e4SLinus Torvalds out_noput:
12431da177e4SLinus Torvalds 	return err;
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds 
124622e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
12475424f32eSThomas Graf 		struct nlattr **attrs)
12481da177e4SLinus Torvalds {
1249fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12501da177e4SLinus Torvalds 	struct xfrm_state *x;
12511da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
12521da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
12531da177e4SLinus Torvalds 	xfrm_address_t *daddr;
12541da177e4SLinus Torvalds 	int family;
12551da177e4SLinus Torvalds 	int err;
12566f26b61eSJamal Hadi Salim 	u32 mark;
12576f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
12581da177e4SLinus Torvalds 
12597b67c857SThomas Graf 	p = nlmsg_data(nlh);
1260776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
12611da177e4SLinus Torvalds 	if (err)
12621da177e4SLinus Torvalds 		goto out_noput;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	family = p->info.family;
12651da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	x = NULL;
12686f26b61eSJamal Hadi Salim 
12696f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
12701da177e4SLinus Torvalds 	if (p->info.seq) {
12716f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
127270e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
12731da177e4SLinus Torvalds 			xfrm_state_put(x);
12741da177e4SLinus Torvalds 			x = NULL;
12751da177e4SLinus Torvalds 		}
12761da177e4SLinus Torvalds 	}
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	if (!x)
12796f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
12801da177e4SLinus Torvalds 				  p->info.id.proto, daddr,
12811da177e4SLinus Torvalds 				  &p->info.saddr, 1,
12821da177e4SLinus Torvalds 				  family);
12831da177e4SLinus Torvalds 	err = -ENOENT;
12841da177e4SLinus Torvalds 	if (x == NULL)
12851da177e4SLinus Torvalds 		goto out_noput;
12861da177e4SLinus Torvalds 
1287658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1288658b219eSHerbert Xu 	if (err)
1289658b219eSHerbert Xu 		goto out;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
12921da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
12931da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
12941da177e4SLinus Torvalds 		goto out;
12951da177e4SLinus Torvalds 	}
12961da177e4SLinus Torvalds 
129715e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds out:
13001da177e4SLinus Torvalds 	xfrm_state_put(x);
13011da177e4SLinus Torvalds out_noput:
13021da177e4SLinus Torvalds 	return err;
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
1305b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
13061da177e4SLinus Torvalds {
13071da177e4SLinus Torvalds 	switch (dir) {
13081da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
13091da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
13101da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
13111da177e4SLinus Torvalds 		break;
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	default:
13141da177e4SLinus Torvalds 		return -EINVAL;
13153ff50b79SStephen Hemminger 	}
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	return 0;
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
1320b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1321f7b6983fSMasahide NAKAMURA {
1322f7b6983fSMasahide NAKAMURA 	switch (type) {
1323f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1324f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1325f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1326f7b6983fSMasahide NAKAMURA #endif
1327f7b6983fSMasahide NAKAMURA 		break;
1328f7b6983fSMasahide NAKAMURA 
1329f7b6983fSMasahide NAKAMURA 	default:
1330f7b6983fSMasahide NAKAMURA 		return -EINVAL;
13313ff50b79SStephen Hemminger 	}
1332f7b6983fSMasahide NAKAMURA 
1333f7b6983fSMasahide NAKAMURA 	return 0;
1334f7b6983fSMasahide NAKAMURA }
1335f7b6983fSMasahide NAKAMURA 
13361da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
13371da177e4SLinus Torvalds {
1338e682adf0SFan Du 	int ret;
1339e682adf0SFan Du 
13401da177e4SLinus Torvalds 	switch (p->share) {
13411da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
13421da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
13431da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
13441da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
13451da177e4SLinus Torvalds 		break;
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	default:
13481da177e4SLinus Torvalds 		return -EINVAL;
13493ff50b79SStephen Hemminger 	}
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	switch (p->action) {
13521da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
13531da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
13541da177e4SLinus Torvalds 		break;
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	default:
13571da177e4SLinus Torvalds 		return -EINVAL;
13583ff50b79SStephen Hemminger 	}
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	switch (p->sel.family) {
13611da177e4SLinus Torvalds 	case AF_INET:
13621da177e4SLinus Torvalds 		break;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	case AF_INET6:
1365dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
13661da177e4SLinus Torvalds 		break;
13671da177e4SLinus Torvalds #else
13681da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
13691da177e4SLinus Torvalds #endif
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	default:
13721da177e4SLinus Torvalds 		return -EINVAL;
13733ff50b79SStephen Hemminger 	}
13741da177e4SLinus Torvalds 
1375e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1376e682adf0SFan Du 	if (ret)
1377e682adf0SFan Du 		return ret;
1378e682adf0SFan Du 	if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1379e682adf0SFan Du 		return -EINVAL;
1380e682adf0SFan Du 
1381e682adf0SFan Du 	return 0;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
13845424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1385df71837dSTrent Jaeger {
13865424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1387df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1388df71837dSTrent Jaeger 
1389df71837dSTrent Jaeger 	if (!rt)
1390df71837dSTrent Jaeger 		return 0;
1391df71837dSTrent Jaeger 
13925424f32eSThomas Graf 	uctx = nla_data(rt);
139352a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1394df71837dSTrent Jaeger }
1395df71837dSTrent Jaeger 
13961da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
13971da177e4SLinus Torvalds 			   int nr)
13981da177e4SLinus Torvalds {
13991da177e4SLinus Torvalds 	int i;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
14021da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
14031da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
14061da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
14071da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
14081da177e4SLinus Torvalds 		t->reqid = ut->reqid;
14091da177e4SLinus Torvalds 		t->mode = ut->mode;
14101da177e4SLinus Torvalds 		t->share = ut->share;
14111da177e4SLinus Torvalds 		t->optional = ut->optional;
14121da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
14131da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
14141da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1415c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1416c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
14178511d01dSMiika Komu 		t->encap_family = ut->family;
14181da177e4SLinus Torvalds 	}
14191da177e4SLinus Torvalds }
14201da177e4SLinus Torvalds 
1421b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1422b4ad86bfSDavid S. Miller {
1423732706afSSteffen Klassert 	u16 prev_family;
1424b4ad86bfSDavid S. Miller 	int i;
1425b4ad86bfSDavid S. Miller 
1426b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1427b4ad86bfSDavid S. Miller 		return -EINVAL;
1428b4ad86bfSDavid S. Miller 
1429732706afSSteffen Klassert 	prev_family = family;
1430732706afSSteffen Klassert 
1431b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1432b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1433b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1434b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1435b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1436b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1437b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1438b4ad86bfSDavid S. Miller 		 */
1439b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1440b4ad86bfSDavid S. Miller 			ut[i].family = family;
1441b4ad86bfSDavid S. Miller 
1442732706afSSteffen Klassert 		if ((ut[i].mode == XFRM_MODE_TRANSPORT) &&
1443732706afSSteffen Klassert 		    (ut[i].family != prev_family))
1444732706afSSteffen Klassert 			return -EINVAL;
1445732706afSSteffen Klassert 
1446732706afSSteffen Klassert 		prev_family = ut[i].family;
1447732706afSSteffen Klassert 
1448b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1449b4ad86bfSDavid S. Miller 		case AF_INET:
1450b4ad86bfSDavid S. Miller 			break;
1451dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1452b4ad86bfSDavid S. Miller 		case AF_INET6:
1453b4ad86bfSDavid S. Miller 			break;
1454b4ad86bfSDavid S. Miller #endif
1455b4ad86bfSDavid S. Miller 		default:
1456b4ad86bfSDavid S. Miller 			return -EINVAL;
14573ff50b79SStephen Hemminger 		}
14586a53b759SCong Wang 
14596a53b759SCong Wang 		switch (ut[i].id.proto) {
14606a53b759SCong Wang 		case IPPROTO_AH:
14616a53b759SCong Wang 		case IPPROTO_ESP:
14626a53b759SCong Wang 		case IPPROTO_COMP:
14636a53b759SCong Wang #if IS_ENABLED(CONFIG_IPV6)
14646a53b759SCong Wang 		case IPPROTO_ROUTING:
14656a53b759SCong Wang 		case IPPROTO_DSTOPTS:
14666a53b759SCong Wang #endif
14676a53b759SCong Wang 		case IPSEC_PROTO_ANY:
14686a53b759SCong Wang 			break;
14696a53b759SCong Wang 		default:
14706a53b759SCong Wang 			return -EINVAL;
14716a53b759SCong Wang 		}
14726a53b759SCong Wang 
1473b4ad86bfSDavid S. Miller 	}
1474b4ad86bfSDavid S. Miller 
1475b4ad86bfSDavid S. Miller 	return 0;
1476b4ad86bfSDavid S. Miller }
1477b4ad86bfSDavid S. Miller 
14785424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
14791da177e4SLinus Torvalds {
14805424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	if (!rt) {
14831da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
14841da177e4SLinus Torvalds 	} else {
14855424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
14865424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1487b4ad86bfSDavid S. Miller 		int err;
14881da177e4SLinus Torvalds 
1489b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1490b4ad86bfSDavid S. Miller 		if (err)
1491b4ad86bfSDavid S. Miller 			return err;
14921da177e4SLinus Torvalds 
14935424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
14941da177e4SLinus Torvalds 	}
14951da177e4SLinus Torvalds 	return 0;
14961da177e4SLinus Torvalds }
14971da177e4SLinus Torvalds 
14985424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1499f7b6983fSMasahide NAKAMURA {
15005424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1501f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1502b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1503f7b6983fSMasahide NAKAMURA 	int err;
1504f7b6983fSMasahide NAKAMURA 
1505f7b6983fSMasahide NAKAMURA 	if (rt) {
15065424f32eSThomas Graf 		upt = nla_data(rt);
1507f7b6983fSMasahide NAKAMURA 		type = upt->type;
1508f7b6983fSMasahide NAKAMURA 	}
1509f7b6983fSMasahide NAKAMURA 
1510f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1511f7b6983fSMasahide NAKAMURA 	if (err)
1512f7b6983fSMasahide NAKAMURA 		return err;
1513f7b6983fSMasahide NAKAMURA 
1514f7b6983fSMasahide NAKAMURA 	*tp = type;
1515f7b6983fSMasahide NAKAMURA 	return 0;
1516f7b6983fSMasahide NAKAMURA }
1517f7b6983fSMasahide NAKAMURA 
15181da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
15191da177e4SLinus Torvalds {
15201da177e4SLinus Torvalds 	xp->priority = p->priority;
15211da177e4SLinus Torvalds 	xp->index = p->index;
15221da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
15231da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
15241da177e4SLinus Torvalds 	xp->action = p->action;
15251da177e4SLinus Torvalds 	xp->flags = p->flags;
15261da177e4SLinus Torvalds 	xp->family = p->sel.family;
15271da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
15311da177e4SLinus Torvalds {
15327b789836SMathias Krause 	memset(p, 0, sizeof(*p));
15331da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
15341da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
15351da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
15361da177e4SLinus Torvalds 	p->priority = xp->priority;
15371da177e4SLinus Torvalds 	p->index = xp->index;
15381da177e4SLinus Torvalds 	p->sel.family = xp->family;
15391da177e4SLinus Torvalds 	p->dir = dir;
15401da177e4SLinus Torvalds 	p->action = xp->action;
15411da177e4SLinus Torvalds 	p->flags = xp->flags;
15421da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
15431da177e4SLinus Torvalds }
15441da177e4SLinus Torvalds 
1545fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
15461da177e4SLinus Torvalds {
1547fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
15481da177e4SLinus Torvalds 	int err;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	if (!xp) {
15511da177e4SLinus Torvalds 		*errp = -ENOMEM;
15521da177e4SLinus Torvalds 		return NULL;
15531da177e4SLinus Torvalds 	}
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1556df71837dSTrent Jaeger 
155735a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1558f7b6983fSMasahide NAKAMURA 	if (err)
1559f7b6983fSMasahide NAKAMURA 		goto error;
1560f7b6983fSMasahide NAKAMURA 
156135a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
156235a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1563f7b6983fSMasahide NAKAMURA 	if (err)
1564f7b6983fSMasahide NAKAMURA 		goto error;
15651da177e4SLinus Torvalds 
1566295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1567295fae56SJamal Hadi Salim 
15681da177e4SLinus Torvalds 	return xp;
1569f7b6983fSMasahide NAKAMURA  error:
1570f7b6983fSMasahide NAKAMURA 	*errp = err;
157112a169e7SHerbert Xu 	xp->walk.dead = 1;
157264c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1573f7b6983fSMasahide NAKAMURA 	return NULL;
15741da177e4SLinus Torvalds }
15751da177e4SLinus Torvalds 
157622e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
15775424f32eSThomas Graf 		struct nlattr **attrs)
15781da177e4SLinus Torvalds {
1579fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
15807b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
15811da177e4SLinus Torvalds 	struct xfrm_policy *xp;
158226b15dadSJamal Hadi Salim 	struct km_event c;
15831da177e4SLinus Torvalds 	int err;
15841da177e4SLinus Torvalds 	int excl;
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
15871da177e4SLinus Torvalds 	if (err)
15881da177e4SLinus Torvalds 		return err;
158935a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1590df71837dSTrent Jaeger 	if (err)
1591df71837dSTrent Jaeger 		return err;
15921da177e4SLinus Torvalds 
1593fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
15941da177e4SLinus Torvalds 	if (!xp)
15951da177e4SLinus Torvalds 		return err;
15961da177e4SLinus Torvalds 
159725985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
159826b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
159926b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
160026b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
16011da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
16021da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
16032e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1604161a09e7SJoy Latten 
16051da177e4SLinus Torvalds 	if (err) {
160603e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
16071da177e4SLinus Torvalds 		kfree(xp);
16081da177e4SLinus Torvalds 		return err;
16091da177e4SLinus Torvalds 	}
16101da177e4SLinus Torvalds 
1611f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
161226b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
161315e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
161426b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
161526b15dadSJamal Hadi Salim 
16161da177e4SLinus Torvalds 	xfrm_pol_put(xp);
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	return 0;
16191da177e4SLinus Torvalds }
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
16221da177e4SLinus Torvalds {
16231da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
16241da177e4SLinus Torvalds 	int i;
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
16271da177e4SLinus Torvalds 		return 0;
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
16301da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
16311da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
16321da177e4SLinus Torvalds 
16331f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
16341da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
16358511d01dSMiika Komu 		up->family = kp->encap_family;
16361da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
16371da177e4SLinus Torvalds 		up->reqid = kp->reqid;
16381da177e4SLinus Torvalds 		up->mode = kp->mode;
16391da177e4SLinus Torvalds 		up->share = kp->share;
16401da177e4SLinus Torvalds 		up->optional = kp->optional;
16411da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
16421da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
16431da177e4SLinus Torvalds 		up->calgos = kp->calgos;
16441da177e4SLinus Torvalds 	}
16451da177e4SLinus Torvalds 
1646c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1647c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1648df71837dSTrent Jaeger }
1649df71837dSTrent Jaeger 
16500d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
16510d681623SSerge Hallyn {
16520d681623SSerge Hallyn 	if (x->security) {
16530d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
16540d681623SSerge Hallyn 	}
16550d681623SSerge Hallyn 	return 0;
16560d681623SSerge Hallyn }
16570d681623SSerge Hallyn 
16580d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
16590d681623SSerge Hallyn {
16601d1e34ddSDavid S. Miller 	if (xp->security)
16610d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
16620d681623SSerge Hallyn 	return 0;
16630d681623SSerge Hallyn }
1664a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void)
1665cfbfd45aSThomas Graf {
1666cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1667cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1668cfbfd45aSThomas Graf #else
1669cfbfd45aSThomas Graf 	return 0;
1670cfbfd45aSThomas Graf #endif
1671cfbfd45aSThomas Graf }
16720d681623SSerge Hallyn 
1673f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1674b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1675f7b6983fSMasahide NAKAMURA {
167645c180bcSEric Dumazet 	struct xfrm_userpolicy_type upt;
167745c180bcSEric Dumazet 
167845c180bcSEric Dumazet 	/* Sadly there are two holes in struct xfrm_userpolicy_type */
167945c180bcSEric Dumazet 	memset(&upt, 0, sizeof(upt));
168045c180bcSEric Dumazet 	upt.type = type;
1681f7b6983fSMasahide NAKAMURA 
1682c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1683f7b6983fSMasahide NAKAMURA }
1684f7b6983fSMasahide NAKAMURA 
1685f7b6983fSMasahide NAKAMURA #else
1686b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1687f7b6983fSMasahide NAKAMURA {
1688f7b6983fSMasahide NAKAMURA 	return 0;
1689f7b6983fSMasahide NAKAMURA }
1690f7b6983fSMasahide NAKAMURA #endif
1691f7b6983fSMasahide NAKAMURA 
16921da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
16931da177e4SLinus Torvalds {
16941da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
16951da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
16961da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
16971da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
16981da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
16991d1e34ddSDavid S. Miller 	int err;
17001da177e4SLinus Torvalds 
170115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
170279b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
170379b8b7f4SThomas Graf 	if (nlh == NULL)
170479b8b7f4SThomas Graf 		return -EMSGSIZE;
17051da177e4SLinus Torvalds 
17067b67c857SThomas Graf 	p = nlmsg_data(nlh);
17071da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
17081d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
17091d1e34ddSDavid S. Miller 	if (!err)
17101d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
17111d1e34ddSDavid S. Miller 	if (!err)
17121d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
17131d1e34ddSDavid S. Miller 	if (!err)
17141d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
17151d1e34ddSDavid S. Miller 	if (err) {
17161d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
17171d1e34ddSDavid S. Miller 		return err;
17181d1e34ddSDavid S. Miller 	}
17199825069dSThomas Graf 	nlmsg_end(skb, nlh);
17201da177e4SLinus Torvalds 	return 0;
17211da177e4SLinus Torvalds }
17221da177e4SLinus Torvalds 
17234c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
17244c563f76STimo Teras {
17251137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1726283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
17274c563f76STimo Teras 
1728283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
17294c563f76STimo Teras 	return 0;
17304c563f76STimo Teras }
17314c563f76STimo Teras 
17321137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb)
17331137b5e2SHerbert Xu {
17341137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
17351137b5e2SHerbert Xu 
17361137b5e2SHerbert Xu 	BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
17371137b5e2SHerbert Xu 
17381137b5e2SHerbert Xu 	xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
17391137b5e2SHerbert Xu 	return 0;
17401137b5e2SHerbert Xu }
17411137b5e2SHerbert Xu 
17421da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
17431da177e4SLinus Torvalds {
1744fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
17451137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
17461da177e4SLinus Torvalds 	struct xfrm_dump_info info;
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	info.in_skb = cb->skb;
17491da177e4SLinus Torvalds 	info.out_skb = skb;
17501da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
17511da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
17524c563f76STimo Teras 
1753fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 	return skb->len;
17561da177e4SLinus Torvalds }
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
17591da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
17601da177e4SLinus Torvalds 					  int dir, u32 seq)
17611da177e4SLinus Torvalds {
17621da177e4SLinus Torvalds 	struct xfrm_dump_info info;
17631da177e4SLinus Torvalds 	struct sk_buff *skb;
1764c2546372SMathias Krause 	int err;
17651da177e4SLinus Torvalds 
17667deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17671da177e4SLinus Torvalds 	if (!skb)
17681da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	info.in_skb = in_skb;
17711da177e4SLinus Torvalds 	info.out_skb = skb;
17721da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
17731da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
17741da177e4SLinus Torvalds 
1775c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1776c2546372SMathias Krause 	if (err) {
17771da177e4SLinus Torvalds 		kfree_skb(skb);
1778c2546372SMathias Krause 		return ERR_PTR(err);
17791da177e4SLinus Torvalds 	}
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	return skb;
17821da177e4SLinus Torvalds }
17831da177e4SLinus Torvalds 
178422e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
17855424f32eSThomas Graf 		struct nlattr **attrs)
17861da177e4SLinus Torvalds {
1787fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
17881da177e4SLinus Torvalds 	struct xfrm_policy *xp;
17891da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1790b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
17911da177e4SLinus Torvalds 	int err;
179226b15dadSJamal Hadi Salim 	struct km_event c;
17931da177e4SLinus Torvalds 	int delete;
1794295fae56SJamal Hadi Salim 	struct xfrm_mark m;
1795295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
17961da177e4SLinus Torvalds 
17977b67c857SThomas Graf 	p = nlmsg_data(nlh);
17981da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
17991da177e4SLinus Torvalds 
180035a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1801f7b6983fSMasahide NAKAMURA 	if (err)
1802f7b6983fSMasahide NAKAMURA 		return err;
1803f7b6983fSMasahide NAKAMURA 
18041da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
18051da177e4SLinus Torvalds 	if (err)
18061da177e4SLinus Torvalds 		return err;
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds 	if (p->index)
1809295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1810df71837dSTrent Jaeger 	else {
18115424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
181203e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1813df71837dSTrent Jaeger 
181435a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
1815df71837dSTrent Jaeger 		if (err)
1816df71837dSTrent Jaeger 			return err;
1817df71837dSTrent Jaeger 
18182c8dd116SDenis V. Lunev 		ctx = NULL;
1819df71837dSTrent Jaeger 		if (rt) {
18205424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1821df71837dSTrent Jaeger 
182252a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
182303e1ad7bSPaul Moore 			if (err)
1824df71837dSTrent Jaeger 				return err;
18252c8dd116SDenis V. Lunev 		}
1826295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
18276f26b61eSJamal Hadi Salim 					   ctx, delete, &err);
182803e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
1829df71837dSTrent Jaeger 	}
18301da177e4SLinus Torvalds 	if (xp == NULL)
18311da177e4SLinus Torvalds 		return -ENOENT;
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	if (!delete) {
18341da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
18371da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
18381da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
18391da177e4SLinus Torvalds 		} else {
1840a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
184115e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
18421da177e4SLinus Torvalds 		}
184326b15dadSJamal Hadi Salim 	} else {
18442e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
184513fcfbb0SDavid S. Miller 
184613fcfbb0SDavid S. Miller 		if (err != 0)
1847c8c05a8eSCatherine Zhang 			goto out;
184813fcfbb0SDavid S. Miller 
1849e7443892SHerbert Xu 		c.data.byid = p->index;
1850f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
185126b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
185215e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
185326b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
18541da177e4SLinus Torvalds 	}
18551da177e4SLinus Torvalds 
1856c8c05a8eSCatherine Zhang out:
1857ef41aaa0SEric Paris 	xfrm_pol_put(xp);
18581da177e4SLinus Torvalds 	return err;
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds 
186122e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
18625424f32eSThomas Graf 		struct nlattr **attrs)
18631da177e4SLinus Torvalds {
1864fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
186526b15dadSJamal Hadi Salim 	struct km_event c;
18667b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
18674aa2e62cSJoy Latten 	int err;
18681da177e4SLinus Torvalds 
18692e71029eSTetsuo Handa 	err = xfrm_state_flush(net, p->proto, true);
18709e64cc95SJamal Hadi Salim 	if (err) {
18719e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
18729e64cc95SJamal Hadi Salim 			return 0;
1873069c474eSDavid S. Miller 		return err;
18749e64cc95SJamal Hadi Salim 	}
1875bf08867fSHerbert Xu 	c.data.proto = p->proto;
1876f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
187726b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
187815e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
18797067802eSAlexey Dobriyan 	c.net = net;
188026b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
188126b15dadSJamal Hadi Salim 
18821da177e4SLinus Torvalds 	return 0;
18831da177e4SLinus Torvalds }
18841da177e4SLinus Torvalds 
1885a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
18867deb2264SThomas Graf {
1887a1b831f2SAlexey Dobriyan 	unsigned int replay_size = x->replay_esn ?
1888d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
1889d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
1890d8647b79SSteffen Klassert 
18917deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1892d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
1893de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
18946f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
18957deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
18967deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
18977deb2264SThomas Graf }
1898d51d081dSJamal Hadi Salim 
1899214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1900d51d081dSJamal Hadi Salim {
1901d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1902d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
19031d1e34ddSDavid S. Miller 	int err;
1904d51d081dSJamal Hadi Salim 
190515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
190679b8b7f4SThomas Graf 	if (nlh == NULL)
190779b8b7f4SThomas Graf 		return -EMSGSIZE;
1908d51d081dSJamal Hadi Salim 
19097b67c857SThomas Graf 	id = nlmsg_data(nlh);
1910931e79d7SMathias Krause 	memset(&id->sa_id, 0, sizeof(id->sa_id));
19112b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1912d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
1913d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
1914d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
19152b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
19162b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
1917d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
1918d51d081dSJamal Hadi Salim 
1919d0fde795SDavid S. Miller 	if (x->replay_esn) {
19201d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1921d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
19221d1e34ddSDavid S. Miller 			      x->replay_esn);
1923d0fde795SDavid S. Miller 	} else {
19241d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
19251d1e34ddSDavid S. Miller 			      &x->replay);
1926d0fde795SDavid S. Miller 	}
19271d1e34ddSDavid S. Miller 	if (err)
19281d1e34ddSDavid S. Miller 		goto out_cancel;
1929de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1930de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
19311d1e34ddSDavid S. Miller 	if (err)
19321d1e34ddSDavid S. Miller 		goto out_cancel;
1933d8647b79SSteffen Klassert 
19341d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
19351d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
19361d1e34ddSDavid S. Miller 		if (err)
19371d1e34ddSDavid S. Miller 			goto out_cancel;
19381d1e34ddSDavid S. Miller 	}
19391d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
19401d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
19411d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
19421d1e34ddSDavid S. Miller 		if (err)
19431d1e34ddSDavid S. Miller 			goto out_cancel;
19441d1e34ddSDavid S. Miller 	}
19451d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
19461d1e34ddSDavid S. Miller 	if (err)
19471d1e34ddSDavid S. Miller 		goto out_cancel;
19486f26b61eSJamal Hadi Salim 
1949053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1950053c095aSJohannes Berg 	return 0;
1951d51d081dSJamal Hadi Salim 
19521d1e34ddSDavid S. Miller out_cancel:
19539825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
19541d1e34ddSDavid S. Miller 	return err;
1955d51d081dSJamal Hadi Salim }
1956d51d081dSJamal Hadi Salim 
195722e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
19585424f32eSThomas Graf 		struct nlattr **attrs)
1959d51d081dSJamal Hadi Salim {
1960fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1961d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
1962d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
1963d51d081dSJamal Hadi Salim 	int err;
1964d51d081dSJamal Hadi Salim 	struct km_event c;
19656f26b61eSJamal Hadi Salim 	u32 mark;
19666f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
19677b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
1968d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
1969d51d081dSJamal Hadi Salim 
19706f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
19716f26b61eSJamal Hadi Salim 
19726f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1973d8647b79SSteffen Klassert 	if (x == NULL)
1974d51d081dSJamal Hadi Salim 		return -ESRCH;
1975d8647b79SSteffen Klassert 
1976d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1977d8647b79SSteffen Klassert 	if (r_skb == NULL) {
1978d8647b79SSteffen Klassert 		xfrm_state_put(x);
1979d8647b79SSteffen Klassert 		return -ENOMEM;
1980d51d081dSJamal Hadi Salim 	}
1981d51d081dSJamal Hadi Salim 
1982d51d081dSJamal Hadi Salim 	/*
1983d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
1984d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
1985d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
1986d51d081dSJamal Hadi Salim 	*/
1987d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
1988d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
1989d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
199015e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
1991d51d081dSJamal Hadi Salim 
19922fc5f83bSGustavo A. R. Silva 	err = build_aevent(r_skb, x, &c);
19932fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
19942fc5f83bSGustavo A. R. Silva 
199515e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1996d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
1997d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
1998d51d081dSJamal Hadi Salim 	return err;
1999d51d081dSJamal Hadi Salim }
2000d51d081dSJamal Hadi Salim 
200122e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
20025424f32eSThomas Graf 		struct nlattr **attrs)
2003d51d081dSJamal Hadi Salim {
2004fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2005d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2006d51d081dSJamal Hadi Salim 	struct km_event c;
2007d51d081dSJamal Hadi Salim 	int err = -EINVAL;
20086f26b61eSJamal Hadi Salim 	u32 mark = 0;
20096f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
20107b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
20115424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2012d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
20135424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
20144e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
20154e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2016d51d081dSJamal Hadi Salim 
20174e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
2018d51d081dSJamal Hadi Salim 		return err;
2019d51d081dSJamal Hadi Salim 
2020d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
2021d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2022d51d081dSJamal Hadi Salim 		return err;
2023d51d081dSJamal Hadi Salim 
20246f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
20256f26b61eSJamal Hadi Salim 
20266f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2027d51d081dSJamal Hadi Salim 	if (x == NULL)
2028d51d081dSJamal Hadi Salim 		return -ESRCH;
2029d51d081dSJamal Hadi Salim 
2030d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
2031d51d081dSJamal Hadi Salim 		goto out;
2032d51d081dSJamal Hadi Salim 
20334479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
2034e2b19125SSteffen Klassert 	if (err)
2035e2b19125SSteffen Klassert 		goto out;
2036e2b19125SSteffen Klassert 
2037d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2038e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
2039d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2040d51d081dSJamal Hadi Salim 
2041d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
2042d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
204315e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2044d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
2045d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
2046d51d081dSJamal Hadi Salim 	err = 0;
2047d51d081dSJamal Hadi Salim out:
2048d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2049d51d081dSJamal Hadi Salim 	return err;
2050d51d081dSJamal Hadi Salim }
2051d51d081dSJamal Hadi Salim 
205222e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
20535424f32eSThomas Graf 		struct nlattr **attrs)
20541da177e4SLinus Torvalds {
2055fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
205626b15dadSJamal Hadi Salim 	struct km_event c;
2057b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
2058f7b6983fSMasahide NAKAMURA 	int err;
205926b15dadSJamal Hadi Salim 
206035a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2061f7b6983fSMasahide NAKAMURA 	if (err)
2062f7b6983fSMasahide NAKAMURA 		return err;
2063f7b6983fSMasahide NAKAMURA 
20642e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
20652f1eb65fSJamal Hadi Salim 	if (err) {
20662f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
20672f1eb65fSJamal Hadi Salim 			return 0;
2068069c474eSDavid S. Miller 		return err;
20692f1eb65fSJamal Hadi Salim 	}
20702f1eb65fSJamal Hadi Salim 
2071f7b6983fSMasahide NAKAMURA 	c.data.type = type;
2072f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
207326b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
207415e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
20757067802eSAlexey Dobriyan 	c.net = net;
207626b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
20771da177e4SLinus Torvalds 	return 0;
20781da177e4SLinus Torvalds }
20791da177e4SLinus Torvalds 
208022e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
20815424f32eSThomas Graf 		struct nlattr **attrs)
20826c5c8ca7SJamal Hadi Salim {
2083fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
20846c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
20857b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
20866c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2087b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
20886c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2089295fae56SJamal Hadi Salim 	struct xfrm_mark m;
2090295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
20916c5c8ca7SJamal Hadi Salim 
209235a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2093f7b6983fSMasahide NAKAMURA 	if (err)
2094f7b6983fSMasahide NAKAMURA 		return err;
2095f7b6983fSMasahide NAKAMURA 
2096c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
2097c8bf4d04STimo Teräs 	if (err)
2098c8bf4d04STimo Teräs 		return err;
2099c8bf4d04STimo Teräs 
21006c5c8ca7SJamal Hadi Salim 	if (p->index)
2101295fae56SJamal Hadi Salim 		xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
21026c5c8ca7SJamal Hadi Salim 	else {
21035424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
210403e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
21056c5c8ca7SJamal Hadi Salim 
210635a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
21076c5c8ca7SJamal Hadi Salim 		if (err)
21086c5c8ca7SJamal Hadi Salim 			return err;
21096c5c8ca7SJamal Hadi Salim 
21102c8dd116SDenis V. Lunev 		ctx = NULL;
21116c5c8ca7SJamal Hadi Salim 		if (rt) {
21125424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
21136c5c8ca7SJamal Hadi Salim 
211452a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
211503e1ad7bSPaul Moore 			if (err)
21166c5c8ca7SJamal Hadi Salim 				return err;
21172c8dd116SDenis V. Lunev 		}
2118295fae56SJamal Hadi Salim 		xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
21196f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
212003e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
21216c5c8ca7SJamal Hadi Salim 	}
21226c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2123ef41aaa0SEric Paris 		return -ENOENT;
212403e1ad7bSPaul Moore 
2125ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
21266c5c8ca7SJamal Hadi Salim 		goto out;
21276c5c8ca7SJamal Hadi Salim 
21286c5c8ca7SJamal Hadi Salim 	err = 0;
21296c5c8ca7SJamal Hadi Salim 	if (up->hard) {
21306c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
21312e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
21326c5c8ca7SJamal Hadi Salim 	}
2133c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
21346c5c8ca7SJamal Hadi Salim 
21356c5c8ca7SJamal Hadi Salim out:
21366c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
21376c5c8ca7SJamal Hadi Salim 	return err;
21386c5c8ca7SJamal Hadi Salim }
21396c5c8ca7SJamal Hadi Salim 
214022e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
21415424f32eSThomas Graf 		struct nlattr **attrs)
214253bc6b4dSJamal Hadi Salim {
2143fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
214453bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
214553bc6b4dSJamal Hadi Salim 	int err;
21467b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
214753bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
21486f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2149928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
215053bc6b4dSJamal Hadi Salim 
21516f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
215253bc6b4dSJamal Hadi Salim 
21533a765aa5SDavid S. Miller 	err = -ENOENT;
215453bc6b4dSJamal Hadi Salim 	if (x == NULL)
215553bc6b4dSJamal Hadi Salim 		return err;
215653bc6b4dSJamal Hadi Salim 
215753bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
21583a765aa5SDavid S. Miller 	err = -EINVAL;
215953bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
216053bc6b4dSJamal Hadi Salim 		goto out;
2161c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
216253bc6b4dSJamal Hadi Salim 
2163161a09e7SJoy Latten 	if (ue->hard) {
216453bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
21652e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2166161a09e7SJoy Latten 	}
21673a765aa5SDavid S. Miller 	err = 0;
216853bc6b4dSJamal Hadi Salim out:
216953bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
217053bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
217153bc6b4dSJamal Hadi Salim 	return err;
217253bc6b4dSJamal Hadi Salim }
217353bc6b4dSJamal Hadi Salim 
217422e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
21755424f32eSThomas Graf 		struct nlattr **attrs)
2176980ebd25SJamal Hadi Salim {
2177fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2178980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2179980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2180980ebd25SJamal Hadi Salim 	int i;
21815424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
21826f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2183980ebd25SJamal Hadi Salim 
21847b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2185fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2186980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2187980ebd25SJamal Hadi Salim 
2188980ebd25SJamal Hadi Salim 	if (!x)
2189d8eb9307SIlpo Järvinen 		goto nomem;
2190980ebd25SJamal Hadi Salim 
21916f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
21926f26b61eSJamal Hadi Salim 
2193980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2194d8eb9307SIlpo Järvinen 	if (err)
219573efc324SVegard Nossum 		goto free_state;
2196980ebd25SJamal Hadi Salim 
2197980ebd25SJamal Hadi Salim 	/*   build an XP */
2198fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2199d8eb9307SIlpo Järvinen 	if (!xp)
2200d8eb9307SIlpo Järvinen 		goto free_state;
2201980ebd25SJamal Hadi Salim 
2202980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2203980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2204980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
22056f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
22066f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
22075424f32eSThomas Graf 	ut = nla_data(rt);
2208980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2209980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2210980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2211980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2212980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2213980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2214980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2215980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2216980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2217980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2218980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2219980ebd25SJamal Hadi Salim 
2220980ebd25SJamal Hadi Salim 	}
2221980ebd25SJamal Hadi Salim 
2222980ebd25SJamal Hadi Salim 	kfree(x);
2223980ebd25SJamal Hadi Salim 	kfree(xp);
2224980ebd25SJamal Hadi Salim 
2225980ebd25SJamal Hadi Salim 	return 0;
2226d8eb9307SIlpo Järvinen 
2227d8eb9307SIlpo Järvinen free_state:
2228d8eb9307SIlpo Järvinen 	kfree(x);
2229d8eb9307SIlpo Järvinen nomem:
2230d8eb9307SIlpo Järvinen 	return err;
2231980ebd25SJamal Hadi Salim }
2232980ebd25SJamal Hadi Salim 
22335c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
22345c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
223513c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
22365424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
22375c79de6eSShinta Sugimoto {
22385424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
22395c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
22405c79de6eSShinta Sugimoto 	int i, num_migrate;
22415c79de6eSShinta Sugimoto 
224213c1d189SArnaud Ebalard 	if (k != NULL) {
224313c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
224413c1d189SArnaud Ebalard 
224513c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
224613c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
224713c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
224813c1d189SArnaud Ebalard 		k->family = uk->family;
224913c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
225013c1d189SArnaud Ebalard 	}
225113c1d189SArnaud Ebalard 
22525424f32eSThomas Graf 	um = nla_data(rt);
22535424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
22545c79de6eSShinta Sugimoto 
22555c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
22565c79de6eSShinta Sugimoto 		return -EINVAL;
22575c79de6eSShinta Sugimoto 
22585c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
22595c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
22605c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
22615c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
22625c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
22635c79de6eSShinta Sugimoto 
22645c79de6eSShinta Sugimoto 		ma->proto = um->proto;
22655c79de6eSShinta Sugimoto 		ma->mode = um->mode;
22665c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
22675c79de6eSShinta Sugimoto 
22685c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
22695c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
22705c79de6eSShinta Sugimoto 	}
22715c79de6eSShinta Sugimoto 
22725c79de6eSShinta Sugimoto 	*num = i;
22735c79de6eSShinta Sugimoto 	return 0;
22745c79de6eSShinta Sugimoto }
22755c79de6eSShinta Sugimoto 
22765c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
22775424f32eSThomas Graf 			   struct nlattr **attrs)
22785c79de6eSShinta Sugimoto {
22797b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
22805c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
228113c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
22825c79de6eSShinta Sugimoto 	u8 type;
22835c79de6eSShinta Sugimoto 	int err;
22845c79de6eSShinta Sugimoto 	int n = 0;
22858d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
22864ab47d47SAntony Antony 	struct xfrm_encap_tmpl  *encap = NULL;
22875c79de6eSShinta Sugimoto 
228835a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2289cf5cb79fSThomas Graf 		return -EINVAL;
22905c79de6eSShinta Sugimoto 
229113c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
229213c1d189SArnaud Ebalard 
22935424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
22945c79de6eSShinta Sugimoto 	if (err)
22955c79de6eSShinta Sugimoto 		return err;
22965c79de6eSShinta Sugimoto 
229713c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
22985c79de6eSShinta Sugimoto 	if (err)
22995c79de6eSShinta Sugimoto 		return err;
23005c79de6eSShinta Sugimoto 
23015c79de6eSShinta Sugimoto 	if (!n)
23025c79de6eSShinta Sugimoto 		return 0;
23035c79de6eSShinta Sugimoto 
23044ab47d47SAntony Antony 	if (attrs[XFRMA_ENCAP]) {
23054ab47d47SAntony Antony 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
23064ab47d47SAntony Antony 				sizeof(*encap), GFP_KERNEL);
23074ab47d47SAntony Antony 		if (!encap)
23085c79de6eSShinta Sugimoto 			return 0;
23095c79de6eSShinta Sugimoto 	}
23104ab47d47SAntony Antony 
23114ab47d47SAntony Antony 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
23124ab47d47SAntony Antony 
23134ab47d47SAntony Antony 	kfree(encap);
23144ab47d47SAntony Antony 
23154ab47d47SAntony Antony 	return err;
23164ab47d47SAntony Antony }
23175c79de6eSShinta Sugimoto #else
23185c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
23195424f32eSThomas Graf 			   struct nlattr **attrs)
23205c79de6eSShinta Sugimoto {
23215c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
23225c79de6eSShinta Sugimoto }
23235c79de6eSShinta Sugimoto #endif
23245c79de6eSShinta Sugimoto 
23255c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2326183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
23275c79de6eSShinta Sugimoto {
23285c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
23295c79de6eSShinta Sugimoto 
23305c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
23315c79de6eSShinta Sugimoto 	um.proto = m->proto;
23325c79de6eSShinta Sugimoto 	um.mode = m->mode;
23335c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
23345c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
23355c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
23365c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
23375c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
23385c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
23395c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
23405c79de6eSShinta Sugimoto 
2341c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
23425c79de6eSShinta Sugimoto }
23435c79de6eSShinta Sugimoto 
2344183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
234513c1d189SArnaud Ebalard {
234613c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
234713c1d189SArnaud Ebalard 
234813c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
234913c1d189SArnaud Ebalard 	uk.family = k->family;
235013c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
235113c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2352a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
235313c1d189SArnaud Ebalard 
235413c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
235513c1d189SArnaud Ebalard }
235613c1d189SArnaud Ebalard 
2357a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
23588bafd730SAntony Antony 						int with_encp)
23597deb2264SThomas Graf {
23607deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
236113c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
23628bafd730SAntony Antony 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
23637deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
23647deb2264SThomas Graf 	      + userpolicy_type_attrsize();
23657deb2264SThomas Graf }
23667deb2264SThomas Graf 
2367183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2368183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
23698bafd730SAntony Antony 			 const struct xfrm_selector *sel,
23708bafd730SAntony Antony 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
23715c79de6eSShinta Sugimoto {
2372183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
23735c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
23745c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
23751d1e34ddSDavid S. Miller 	int i, err;
23765c79de6eSShinta Sugimoto 
237779b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
237879b8b7f4SThomas Graf 	if (nlh == NULL)
237979b8b7f4SThomas Graf 		return -EMSGSIZE;
23805c79de6eSShinta Sugimoto 
23817b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
23825c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
23835c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
23845c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
23855c79de6eSShinta Sugimoto 	pol_id->dir = dir;
23865c79de6eSShinta Sugimoto 
23871d1e34ddSDavid S. Miller 	if (k != NULL) {
23881d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
23891d1e34ddSDavid S. Miller 		if (err)
23901d1e34ddSDavid S. Miller 			goto out_cancel;
23911d1e34ddSDavid S. Miller 	}
23928bafd730SAntony Antony 	if (encap) {
23938bafd730SAntony Antony 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
23948bafd730SAntony Antony 		if (err)
23958bafd730SAntony Antony 			goto out_cancel;
23968bafd730SAntony Antony 	}
23971d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
23981d1e34ddSDavid S. Miller 	if (err)
23991d1e34ddSDavid S. Miller 		goto out_cancel;
24005c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
24011d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
24021d1e34ddSDavid S. Miller 		if (err)
24031d1e34ddSDavid S. Miller 			goto out_cancel;
24045c79de6eSShinta Sugimoto 	}
24055c79de6eSShinta Sugimoto 
2406053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2407053c095aSJohannes Berg 	return 0;
24081d1e34ddSDavid S. Miller 
24091d1e34ddSDavid S. Miller out_cancel:
24109825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
24111d1e34ddSDavid S. Miller 	return err;
24125c79de6eSShinta Sugimoto }
24135c79de6eSShinta Sugimoto 
2414183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2415183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
24168bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
24178bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
24185c79de6eSShinta Sugimoto {
2419a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
24205c79de6eSShinta Sugimoto 	struct sk_buff *skb;
24212fc5f83bSGustavo A. R. Silva 	int err;
24225c79de6eSShinta Sugimoto 
24238bafd730SAntony Antony 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
24248bafd730SAntony Antony 			GFP_ATOMIC);
24255c79de6eSShinta Sugimoto 	if (skb == NULL)
24265c79de6eSShinta Sugimoto 		return -ENOMEM;
24275c79de6eSShinta Sugimoto 
24285c79de6eSShinta Sugimoto 	/* build migrate */
24292fc5f83bSGustavo A. R. Silva 	err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
24302fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
24315c79de6eSShinta Sugimoto 
243221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
24335c79de6eSShinta Sugimoto }
24345c79de6eSShinta Sugimoto #else
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 {
24405c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
24415c79de6eSShinta Sugimoto }
24425c79de6eSShinta Sugimoto #endif
2443d51d081dSJamal Hadi Salim 
2444a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2445492b558bSThomas Graf 
2446492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
244766f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2448492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2449492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2450492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2451492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2452492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2453492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2454980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
245553bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2456492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
245766f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
24586c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2459492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2460a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2461d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2462d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
246397a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
24645c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2465a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2466880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2467a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
24681da177e4SLinus Torvalds };
24691da177e4SLinus Torvalds 
2470492b558bSThomas Graf #undef XMSGSIZE
2471492b558bSThomas Graf 
2472cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2473c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2474c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2475c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2476c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
24771a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2478cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2479cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2480cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2481cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2482cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2483cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2484cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2485cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2486cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2487cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2488cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2489cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2490cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2491cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
249213c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
24936f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
249435d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2495d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2496a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2497d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2498870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2499d77e38e6SSteffen Klassert 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
2500e7191358SMichal Kubecek 	[XFRMA_OUTPUT_MARK]	= { .type = NLA_U32 },
2501cf5cb79fSThomas Graf };
2502cf5cb79fSThomas Graf 
2503880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2504880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2505880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2506880a6fabSChristophe Gouault };
2507880a6fabSChristophe Gouault 
250805600a79SMathias Krause static const struct xfrm_link {
25095424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
25101137b5e2SHerbert Xu 	int (*start)(struct netlink_callback *);
25111da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
25124c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2513880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2514880a6fabSChristophe Gouault 	int nla_max;
2515492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2516492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2517492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2518492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
25194c563f76STimo Teras 						   .dump = xfrm_dump_sa,
25204c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2521492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2522492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2523492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
25241137b5e2SHerbert Xu 						   .start = xfrm_dump_policy_start,
25254c563f76STimo Teras 						   .dump = xfrm_dump_policy,
25264c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2527492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2528980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
252953bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2530492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2531492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
25326c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2533492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2534492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2535d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2536d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
25375c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
253828d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2539880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2540880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2541880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2542ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
25431da177e4SLinus Torvalds };
25441da177e4SLinus Torvalds 
25452d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
25462d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
25471da177e4SLinus Torvalds {
2548a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
254935a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
255005600a79SMathias Krause 	const struct xfrm_link *link;
2551a7bd9a45SThomas Graf 	int type, err;
25521da177e4SLinus Torvalds 
255374005991SFan Du #ifdef CONFIG_COMPAT
25542bf8c476SAndy Lutomirski 	if (in_compat_syscall())
255583e2d058SYi Zhao 		return -EOPNOTSUPP;
255674005991SFan Du #endif
255774005991SFan Du 
25581da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
25591da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
25601d00a4ebSThomas Graf 		return -EINVAL;
25611da177e4SLinus Torvalds 
25621da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
25631da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
256690f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
25671d00a4ebSThomas Graf 		return -EPERM;
25681da177e4SLinus Torvalds 
2569492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2570492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2571b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
25721da177e4SLinus Torvalds 		if (link->dump == NULL)
25731d00a4ebSThomas Graf 			return -EINVAL;
25741da177e4SLinus Torvalds 
257580d326faSPablo Neira Ayuso 		{
257680d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
25771137b5e2SHerbert Xu 				.start = link->start,
257880d326faSPablo Neira Ayuso 				.dump = link->dump,
257980d326faSPablo Neira Ayuso 				.done = link->done,
258080d326faSPablo Neira Ayuso 			};
258180d326faSPablo Neira Ayuso 			return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
258280d326faSPablo Neira Ayuso 		}
25831da177e4SLinus Torvalds 	}
25841da177e4SLinus Torvalds 
2585880a6fabSChristophe Gouault 	err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2586880a6fabSChristophe Gouault 			  link->nla_max ? : XFRMA_MAX,
2587fe52145fSJohannes Berg 			  link->nla_pol ? : xfrma_policy, extack);
2588a7bd9a45SThomas Graf 	if (err < 0)
2589a7bd9a45SThomas Graf 		return err;
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 	if (link->doit == NULL)
25921d00a4ebSThomas Graf 		return -EINVAL;
25931da177e4SLinus Torvalds 
25945424f32eSThomas Graf 	return link->doit(skb, nlh, attrs);
25951da177e4SLinus Torvalds }
25961da177e4SLinus Torvalds 
2597cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
25981da177e4SLinus Torvalds {
2599283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2600283bc9f3SFan Du 
2601283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2602cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2603283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
26041da177e4SLinus Torvalds }
26051da177e4SLinus Torvalds 
2606a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void)
26077deb2264SThomas Graf {
26086f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
26096f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
26107deb2264SThomas Graf }
26117deb2264SThomas Graf 
2612214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
26131da177e4SLinus Torvalds {
26141da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
26151da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
26161d1e34ddSDavid S. Miller 	int err;
26171da177e4SLinus Torvalds 
261815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
261979b8b7f4SThomas Graf 	if (nlh == NULL)
262079b8b7f4SThomas Graf 		return -EMSGSIZE;
26211da177e4SLinus Torvalds 
26227b67c857SThomas Graf 	ue = nlmsg_data(nlh);
26231da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2624d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
2625e3e5fc16SMathias Krause 	/* clear the padding bytes */
2626e3e5fc16SMathias Krause 	memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
26271da177e4SLinus Torvalds 
26281d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
26291d1e34ddSDavid S. Miller 	if (err)
26301d1e34ddSDavid S. Miller 		return err;
26316f26b61eSJamal Hadi Salim 
2632053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2633053c095aSJohannes Berg 	return 0;
26341da177e4SLinus Torvalds }
26351da177e4SLinus Torvalds 
2636214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
26371da177e4SLinus Torvalds {
2638fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
26391da177e4SLinus Torvalds 	struct sk_buff *skb;
26401da177e4SLinus Torvalds 
26417deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
26421da177e4SLinus Torvalds 	if (skb == NULL)
26431da177e4SLinus Torvalds 		return -ENOMEM;
26441da177e4SLinus Torvalds 
26456f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
26466f26b61eSJamal Hadi Salim 		kfree_skb(skb);
26476f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
26486f26b61eSJamal Hadi Salim 	}
26491da177e4SLinus Torvalds 
265021ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
26511da177e4SLinus Torvalds }
26521da177e4SLinus Torvalds 
2653214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2654d51d081dSJamal Hadi Salim {
2655fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2656d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
26572fc5f83bSGustavo A. R. Silva 	int err;
2658d51d081dSJamal Hadi Salim 
2659d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2660d51d081dSJamal Hadi Salim 	if (skb == NULL)
2661d51d081dSJamal Hadi Salim 		return -ENOMEM;
2662d51d081dSJamal Hadi Salim 
26632fc5f83bSGustavo A. R. Silva 	err = build_aevent(skb, x, c);
26642fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
2665d51d081dSJamal Hadi Salim 
266621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2667d51d081dSJamal Hadi Salim }
2668d51d081dSJamal Hadi Salim 
2669214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
267026b15dadSJamal Hadi Salim {
26717067802eSAlexey Dobriyan 	struct net *net = c->net;
267226b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
267326b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
267426b15dadSJamal Hadi Salim 	struct sk_buff *skb;
26757deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
267626b15dadSJamal Hadi Salim 
26777deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
267826b15dadSJamal Hadi Salim 	if (skb == NULL)
267926b15dadSJamal Hadi Salim 		return -ENOMEM;
268026b15dadSJamal Hadi Salim 
268115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
268279b8b7f4SThomas Graf 	if (nlh == NULL) {
268379b8b7f4SThomas Graf 		kfree_skb(skb);
268479b8b7f4SThomas Graf 		return -EMSGSIZE;
268579b8b7f4SThomas Graf 	}
268626b15dadSJamal Hadi Salim 
26877b67c857SThomas Graf 	p = nlmsg_data(nlh);
2688bf08867fSHerbert Xu 	p->proto = c->data.proto;
268926b15dadSJamal Hadi Salim 
26909825069dSThomas Graf 	nlmsg_end(skb, nlh);
269126b15dadSJamal Hadi Salim 
269221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
269326b15dadSJamal Hadi Salim }
269426b15dadSJamal Hadi Salim 
2695a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
269626b15dadSJamal Hadi Salim {
2697a1b831f2SAlexey Dobriyan 	unsigned int l = 0;
26981a6509d9SHerbert Xu 	if (x->aead)
26991a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
27004447bb33SMartin Willi 	if (x->aalg) {
27014447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
27024447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
27034447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
27044447bb33SMartin Willi 	}
270526b15dadSJamal Hadi Salim 	if (x->ealg)
27060f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
270726b15dadSJamal Hadi Salim 	if (x->calg)
27087deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
270926b15dadSJamal Hadi Salim 	if (x->encap)
27107deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
271135d2856bSMartin Willi 	if (x->tfcpad)
271235d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
2713d8647b79SSteffen Klassert 	if (x->replay_esn)
2714d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2715f293a5e3Sdingzhi 	else
2716f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
271768325d3bSHerbert Xu 	if (x->security)
271868325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
271968325d3bSHerbert Xu 				    x->security->ctx_len);
272068325d3bSHerbert Xu 	if (x->coaddr)
272168325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
2722a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
2723a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
2724d77e38e6SSteffen Klassert 	if (x->xso.dev)
2725d77e38e6SSteffen Klassert 		 l += nla_total_size(sizeof(x->xso));
2726077fbac4SLorenzo Colitti 	if (x->props.output_mark)
2727077fbac4SLorenzo Colitti 		l += nla_total_size(sizeof(x->props.output_mark));
272868325d3bSHerbert Xu 
2729d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2730de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
273126b15dadSJamal Hadi Salim 
273226b15dadSJamal Hadi Salim 	return l;
273326b15dadSJamal Hadi Salim }
273426b15dadSJamal Hadi Salim 
2735214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
273626b15dadSJamal Hadi Salim {
2737fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
273826b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
27390603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
274026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
274126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
2742a1b831f2SAlexey Dobriyan 	unsigned int len = xfrm_sa_len(x);
2743a1b831f2SAlexey Dobriyan 	unsigned int headlen;
2744a1b831f2SAlexey Dobriyan 	int err;
27450603eac0SHerbert Xu 
27460603eac0SHerbert Xu 	headlen = sizeof(*p);
27470603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
27487deb2264SThomas Graf 		len += nla_total_size(headlen);
27490603eac0SHerbert Xu 		headlen = sizeof(*id);
27506f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
27510603eac0SHerbert Xu 	}
27527deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
275326b15dadSJamal Hadi Salim 
27547deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
275526b15dadSJamal Hadi Salim 	if (skb == NULL)
275626b15dadSJamal Hadi Salim 		return -ENOMEM;
275726b15dadSJamal Hadi Salim 
275815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
27591d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
276079b8b7f4SThomas Graf 	if (nlh == NULL)
27611d1e34ddSDavid S. Miller 		goto out_free_skb;
276226b15dadSJamal Hadi Salim 
27637b67c857SThomas Graf 	p = nlmsg_data(nlh);
27640603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2765c0144beaSThomas Graf 		struct nlattr *attr;
2766c0144beaSThomas Graf 
27677b67c857SThomas Graf 		id = nlmsg_data(nlh);
276850329c8aSMathias Krause 		memset(id, 0, sizeof(*id));
27690603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
27700603eac0SHerbert Xu 		id->spi = x->id.spi;
27710603eac0SHerbert Xu 		id->family = x->props.family;
27720603eac0SHerbert Xu 		id->proto = x->id.proto;
27730603eac0SHerbert Xu 
2774c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
27751d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2776c0144beaSThomas Graf 		if (attr == NULL)
27771d1e34ddSDavid S. Miller 			goto out_free_skb;
2778c0144beaSThomas Graf 
2779c0144beaSThomas Graf 		p = nla_data(attr);
27800603eac0SHerbert Xu 	}
27811d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
27821d1e34ddSDavid S. Miller 	if (err)
27831d1e34ddSDavid S. Miller 		goto out_free_skb;
278426b15dadSJamal Hadi Salim 
27859825069dSThomas Graf 	nlmsg_end(skb, nlh);
278626b15dadSJamal Hadi Salim 
278721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
278826b15dadSJamal Hadi Salim 
27891d1e34ddSDavid S. Miller out_free_skb:
279026b15dadSJamal Hadi Salim 	kfree_skb(skb);
27911d1e34ddSDavid S. Miller 	return err;
279226b15dadSJamal Hadi Salim }
279326b15dadSJamal Hadi Salim 
2794214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
279526b15dadSJamal Hadi Salim {
279626b15dadSJamal Hadi Salim 
279726b15dadSJamal Hadi Salim 	switch (c->event) {
2798f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
279926b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2800d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2801d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2802f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2803f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2804f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
280526b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2806f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
280726b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
280826b15dadSJamal Hadi Salim 	default:
280962db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
281062db5cfdSstephen hemminger 		       c->event);
281126b15dadSJamal Hadi Salim 		break;
281226b15dadSJamal Hadi Salim 	}
281326b15dadSJamal Hadi Salim 
281426b15dadSJamal Hadi Salim 	return 0;
281526b15dadSJamal Hadi Salim 
281626b15dadSJamal Hadi Salim }
281726b15dadSJamal Hadi Salim 
2818a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
28197deb2264SThomas Graf 						struct xfrm_policy *xp)
28207deb2264SThomas Graf {
28217deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
28227deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
28236f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
28247deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
28257deb2264SThomas Graf 	       + userpolicy_type_attrsize();
28267deb2264SThomas Graf }
28277deb2264SThomas Graf 
28281da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
282965e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
28301da177e4SLinus Torvalds {
28311d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
28321da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
28331da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
28341d1e34ddSDavid S. Miller 	int err;
28351da177e4SLinus Torvalds 
283679b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
283779b8b7f4SThomas Graf 	if (nlh == NULL)
283879b8b7f4SThomas Graf 		return -EMSGSIZE;
28391da177e4SLinus Torvalds 
28407b67c857SThomas Graf 	ua = nlmsg_data(nlh);
28411da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
28421da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
28431da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
284465e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
28451da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
28461da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
28471da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
28481da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
28491da177e4SLinus Torvalds 
28501d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
28511d1e34ddSDavid S. Miller 	if (!err)
28521d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
28531d1e34ddSDavid S. Miller 	if (!err)
28541d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
28551d1e34ddSDavid S. Miller 	if (!err)
28561d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
28571d1e34ddSDavid S. Miller 	if (err) {
28581d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
28591d1e34ddSDavid S. Miller 		return err;
28601d1e34ddSDavid S. Miller 	}
28611da177e4SLinus Torvalds 
2862053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2863053c095aSJohannes Berg 	return 0;
28641da177e4SLinus Torvalds }
28651da177e4SLinus Torvalds 
28661da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
286765e0736bSFan Du 			     struct xfrm_policy *xp)
28681da177e4SLinus Torvalds {
2869a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
28701da177e4SLinus Torvalds 	struct sk_buff *skb;
28712fc5f83bSGustavo A. R. Silva 	int err;
28721da177e4SLinus Torvalds 
28737deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
28741da177e4SLinus Torvalds 	if (skb == NULL)
28751da177e4SLinus Torvalds 		return -ENOMEM;
28761da177e4SLinus Torvalds 
28772fc5f83bSGustavo A. R. Silva 	err = build_acquire(skb, x, xt, xp);
28782fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
28791da177e4SLinus Torvalds 
288021ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
28811da177e4SLinus Torvalds }
28821da177e4SLinus Torvalds 
28831da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
28841da177e4SLinus Torvalds  * or more templates.
28851da177e4SLinus Torvalds  */
2886cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
28871da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
28881da177e4SLinus Torvalds {
2889fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
28901da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
28911da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
28921da177e4SLinus Torvalds 	struct xfrm_policy *xp;
28931da177e4SLinus Torvalds 	int nr;
28941da177e4SLinus Torvalds 
2895cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
28961da177e4SLinus Torvalds 	case AF_INET:
28971da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
28981da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
28991da177e4SLinus Torvalds 			return NULL;
29001da177e4SLinus Torvalds 		}
29011da177e4SLinus Torvalds 		break;
2902dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
29031da177e4SLinus Torvalds 	case AF_INET6:
29041da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
29051da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
29061da177e4SLinus Torvalds 			return NULL;
29071da177e4SLinus Torvalds 		}
29081da177e4SLinus Torvalds 		break;
29091da177e4SLinus Torvalds #endif
29101da177e4SLinus Torvalds 	default:
29111da177e4SLinus Torvalds 		*dir = -EINVAL;
29121da177e4SLinus Torvalds 		return NULL;
29131da177e4SLinus Torvalds 	}
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds 	*dir = -EINVAL;
29161da177e4SLinus Torvalds 
29171da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
29181da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
29191da177e4SLinus Torvalds 		return NULL;
29201da177e4SLinus Torvalds 
29211da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
2922b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
29231da177e4SLinus Torvalds 		return NULL;
29241da177e4SLinus Torvalds 
2925a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
2926a4f1bac6SHerbert Xu 		return NULL;
2927a4f1bac6SHerbert Xu 
29282f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
29291da177e4SLinus Torvalds 	if (xp == NULL) {
29301da177e4SLinus Torvalds 		*dir = -ENOBUFS;
29311da177e4SLinus Torvalds 		return NULL;
29321da177e4SLinus Torvalds 	}
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
2935f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
29361da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
29371da177e4SLinus Torvalds 
29381da177e4SLinus Torvalds 	*dir = p->dir;
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds 	return xp;
29411da177e4SLinus Torvalds }
29421da177e4SLinus Torvalds 
2943a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
29447deb2264SThomas Graf {
29457deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
29467deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
29477deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2948295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
29497deb2264SThomas Graf 	       + userpolicy_type_attrsize();
29507deb2264SThomas Graf }
29517deb2264SThomas Graf 
29521da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2953214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
29541da177e4SLinus Torvalds {
29551da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
2956d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
29571d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
29581d1e34ddSDavid S. Miller 	int err;
29591da177e4SLinus Torvalds 
296015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
296179b8b7f4SThomas Graf 	if (nlh == NULL)
296279b8b7f4SThomas Graf 		return -EMSGSIZE;
29631da177e4SLinus Torvalds 
29647b67c857SThomas Graf 	upe = nlmsg_data(nlh);
29651da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
29661d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
29671d1e34ddSDavid S. Miller 	if (!err)
29681d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
29691d1e34ddSDavid S. Miller 	if (!err)
29701d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
29711d1e34ddSDavid S. Miller 	if (!err)
29721d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
29731d1e34ddSDavid S. Miller 	if (err) {
29741d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
29751d1e34ddSDavid S. Miller 		return err;
29761d1e34ddSDavid S. Miller 	}
29771da177e4SLinus Torvalds 	upe->hard = !!hard;
29781da177e4SLinus Torvalds 
2979053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2980053c095aSJohannes Berg 	return 0;
29811da177e4SLinus Torvalds }
29821da177e4SLinus Torvalds 
2983214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
29841da177e4SLinus Torvalds {
2985fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
29861da177e4SLinus Torvalds 	struct sk_buff *skb;
29872fc5f83bSGustavo A. R. Silva 	int err;
29881da177e4SLinus Torvalds 
29897deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
29901da177e4SLinus Torvalds 	if (skb == NULL)
29911da177e4SLinus Torvalds 		return -ENOMEM;
29921da177e4SLinus Torvalds 
29932fc5f83bSGustavo A. R. Silva 	err = build_polexpire(skb, xp, dir, c);
29942fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
29951da177e4SLinus Torvalds 
299621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
29971da177e4SLinus Torvalds }
29981da177e4SLinus Torvalds 
2999214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
300026b15dadSJamal Hadi Salim {
3001a1b831f2SAlexey Dobriyan 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3002fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
300326b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
30040603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
300526b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
300626b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3007a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3008a1b831f2SAlexey Dobriyan 	int err;
30090603eac0SHerbert Xu 
30100603eac0SHerbert Xu 	headlen = sizeof(*p);
30110603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
30127deb2264SThomas Graf 		len += nla_total_size(headlen);
30130603eac0SHerbert Xu 		headlen = sizeof(*id);
30140603eac0SHerbert Xu 	}
3015cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
3016295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
30177deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
301826b15dadSJamal Hadi Salim 
30197deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
302026b15dadSJamal Hadi Salim 	if (skb == NULL)
302126b15dadSJamal Hadi Salim 		return -ENOMEM;
302226b15dadSJamal Hadi Salim 
302315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
30241d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
302579b8b7f4SThomas Graf 	if (nlh == NULL)
30261d1e34ddSDavid S. Miller 		goto out_free_skb;
302726b15dadSJamal Hadi Salim 
30287b67c857SThomas Graf 	p = nlmsg_data(nlh);
30290603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
3030c0144beaSThomas Graf 		struct nlattr *attr;
3031c0144beaSThomas Graf 
30327b67c857SThomas Graf 		id = nlmsg_data(nlh);
30330603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
30340603eac0SHerbert Xu 		id->dir = dir;
30350603eac0SHerbert Xu 		if (c->data.byid)
30360603eac0SHerbert Xu 			id->index = xp->index;
30370603eac0SHerbert Xu 		else
30380603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
30390603eac0SHerbert Xu 
3040c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
30411d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3042c0144beaSThomas Graf 		if (attr == NULL)
30431d1e34ddSDavid S. Miller 			goto out_free_skb;
3044c0144beaSThomas Graf 
3045c0144beaSThomas Graf 		p = nla_data(attr);
30460603eac0SHerbert Xu 	}
304726b15dadSJamal Hadi Salim 
304826b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
30491d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
30501d1e34ddSDavid S. Miller 	if (!err)
30511d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
30521d1e34ddSDavid S. Miller 	if (!err)
30531d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
30541d1e34ddSDavid S. Miller 	if (err)
30551d1e34ddSDavid S. Miller 		goto out_free_skb;
3056295fae56SJamal Hadi Salim 
30579825069dSThomas Graf 	nlmsg_end(skb, nlh);
305826b15dadSJamal Hadi Salim 
305921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
306026b15dadSJamal Hadi Salim 
30611d1e34ddSDavid S. Miller out_free_skb:
306226b15dadSJamal Hadi Salim 	kfree_skb(skb);
30631d1e34ddSDavid S. Miller 	return err;
306426b15dadSJamal Hadi Salim }
306526b15dadSJamal Hadi Salim 
3066214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
306726b15dadSJamal Hadi Salim {
30687067802eSAlexey Dobriyan 	struct net *net = c->net;
306926b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
307026b15dadSJamal Hadi Salim 	struct sk_buff *skb;
30711d1e34ddSDavid S. Miller 	int err;
307226b15dadSJamal Hadi Salim 
30737deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
307426b15dadSJamal Hadi Salim 	if (skb == NULL)
307526b15dadSJamal Hadi Salim 		return -ENOMEM;
307626b15dadSJamal Hadi Salim 
307715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
30781d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
307979b8b7f4SThomas Graf 	if (nlh == NULL)
30801d1e34ddSDavid S. Miller 		goto out_free_skb;
30811d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
30821d1e34ddSDavid S. Miller 	if (err)
30831d1e34ddSDavid S. Miller 		goto out_free_skb;
308426b15dadSJamal Hadi Salim 
30859825069dSThomas Graf 	nlmsg_end(skb, nlh);
308626b15dadSJamal Hadi Salim 
308721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
308826b15dadSJamal Hadi Salim 
30891d1e34ddSDavid S. Miller out_free_skb:
309026b15dadSJamal Hadi Salim 	kfree_skb(skb);
30911d1e34ddSDavid S. Miller 	return err;
309226b15dadSJamal Hadi Salim }
309326b15dadSJamal Hadi Salim 
3094214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
309526b15dadSJamal Hadi Salim {
309626b15dadSJamal Hadi Salim 
309726b15dadSJamal Hadi Salim 	switch (c->event) {
3098f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
3099f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
3100f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
310126b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
3102f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
310326b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
3104f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
310526b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
310626b15dadSJamal Hadi Salim 	default:
310762db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
310862db5cfdSstephen hemminger 		       c->event);
310926b15dadSJamal Hadi Salim 	}
311026b15dadSJamal Hadi Salim 
311126b15dadSJamal Hadi Salim 	return 0;
311226b15dadSJamal Hadi Salim 
311326b15dadSJamal Hadi Salim }
311426b15dadSJamal Hadi Salim 
3115a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void)
31167deb2264SThomas Graf {
31177deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
31187deb2264SThomas Graf }
31197deb2264SThomas Graf 
312097a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
312197a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
312297a64b45SMasahide NAKAMURA {
312397a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
312497a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
312597a64b45SMasahide NAKAMURA 
312679b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
312779b8b7f4SThomas Graf 	if (nlh == NULL)
312879b8b7f4SThomas Graf 		return -EMSGSIZE;
312997a64b45SMasahide NAKAMURA 
31307b67c857SThomas Graf 	ur = nlmsg_data(nlh);
313197a64b45SMasahide NAKAMURA 	ur->proto = proto;
313297a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
313397a64b45SMasahide NAKAMURA 
31341d1e34ddSDavid S. Miller 	if (addr) {
31351d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
31361d1e34ddSDavid S. Miller 		if (err) {
31379825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
31381d1e34ddSDavid S. Miller 			return err;
31391d1e34ddSDavid S. Miller 		}
31401d1e34ddSDavid S. Miller 	}
3141053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3142053c095aSJohannes Berg 	return 0;
314397a64b45SMasahide NAKAMURA }
314497a64b45SMasahide NAKAMURA 
3145db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3146db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
314797a64b45SMasahide NAKAMURA {
314897a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
31492fc5f83bSGustavo A. R. Silva 	int err;
315097a64b45SMasahide NAKAMURA 
31517deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
315297a64b45SMasahide NAKAMURA 	if (skb == NULL)
315397a64b45SMasahide NAKAMURA 		return -ENOMEM;
315497a64b45SMasahide NAKAMURA 
31552fc5f83bSGustavo A. R. Silva 	err = build_report(skb, proto, sel, addr);
31562fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
315797a64b45SMasahide NAKAMURA 
315821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
315997a64b45SMasahide NAKAMURA }
316097a64b45SMasahide NAKAMURA 
3161a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void)
31623a2dfbe8SMartin Willi {
31633a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
31643a2dfbe8SMartin Willi }
31653a2dfbe8SMartin Willi 
31663a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
31673a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
31683a2dfbe8SMartin Willi {
31693a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
31703a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
31713a2dfbe8SMartin Willi 
31723a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
31733a2dfbe8SMartin Willi 	if (nlh == NULL)
31743a2dfbe8SMartin Willi 		return -EMSGSIZE;
31753a2dfbe8SMartin Willi 
31763a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
31773a2dfbe8SMartin Willi 
31783a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
31793a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
31803a2dfbe8SMartin Willi 	um->id.family = x->props.family;
31813a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
31823a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
31833a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
31843a2dfbe8SMartin Willi 	um->new_sport = new_sport;
31853a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
31863a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
31873a2dfbe8SMartin Willi 
3188053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3189053c095aSJohannes Berg 	return 0;
31903a2dfbe8SMartin Willi }
31913a2dfbe8SMartin Willi 
31923a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
31933a2dfbe8SMartin Willi 			     __be16 sport)
31943a2dfbe8SMartin Willi {
3195a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
31963a2dfbe8SMartin Willi 	struct sk_buff *skb;
31972fc5f83bSGustavo A. R. Silva 	int err;
31983a2dfbe8SMartin Willi 
31993a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
32003a2dfbe8SMartin Willi 		return -EINVAL;
32013a2dfbe8SMartin Willi 
32023a2dfbe8SMartin Willi 	if (!x->encap)
32033a2dfbe8SMartin Willi 		return -EINVAL;
32043a2dfbe8SMartin Willi 
32053a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
32063a2dfbe8SMartin Willi 	if (skb == NULL)
32073a2dfbe8SMartin Willi 		return -ENOMEM;
32083a2dfbe8SMartin Willi 
32092fc5f83bSGustavo A. R. Silva 	err = build_mapping(skb, x, ipaddr, sport);
32102fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
32113a2dfbe8SMartin Willi 
321221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
32133a2dfbe8SMartin Willi }
32143a2dfbe8SMartin Willi 
32150f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
32160f24558eSHoria Geanta {
32170f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
32180f24558eSHoria Geanta }
32190f24558eSHoria Geanta 
32201da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
32211da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
32221da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
32231da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
32241da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
322597a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
32265c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
32273a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
32280f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
32291da177e4SLinus Torvalds };
32301da177e4SLinus Torvalds 
3231a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
32321da177e4SLinus Torvalds {
3233be33690dSPatrick McHardy 	struct sock *nlsk;
3234a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3235a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3236a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3237a31f2d17SPablo Neira Ayuso 	};
3238be33690dSPatrick McHardy 
32399f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3240be33690dSPatrick McHardy 	if (nlsk == NULL)
32411da177e4SLinus Torvalds 		return -ENOMEM;
3242d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3243cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
32441da177e4SLinus Torvalds 	return 0;
32451da177e4SLinus Torvalds }
32461da177e4SLinus Torvalds 
3247d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3248a6483b79SAlexey Dobriyan {
3249d79d792eSEric W. Biederman 	struct net *net;
3250d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3251a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3252d79d792eSEric W. Biederman 	synchronize_net();
3253d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3254d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3255a6483b79SAlexey Dobriyan }
3256a6483b79SAlexey Dobriyan 
3257a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3258a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
3259d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3260a6483b79SAlexey Dobriyan };
3261a6483b79SAlexey Dobriyan 
3262a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3263a6483b79SAlexey Dobriyan {
3264a6483b79SAlexey Dobriyan 	int rv;
3265a6483b79SAlexey Dobriyan 
3266a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3267a6483b79SAlexey Dobriyan 
3268a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3269a6483b79SAlexey Dobriyan 	if (rv < 0)
3270a6483b79SAlexey Dobriyan 		return rv;
3271a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3272a6483b79SAlexey Dobriyan 	if (rv < 0)
3273a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3274a6483b79SAlexey Dobriyan 	return rv;
3275a6483b79SAlexey Dobriyan }
3276a6483b79SAlexey Dobriyan 
32771da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
32781da177e4SLinus Torvalds {
32791da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3280a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
32811da177e4SLinus Torvalds }
32821da177e4SLinus Torvalds 
32831da177e4SLinus Torvalds module_init(xfrm_user_init);
32841da177e4SLinus Torvalds module_exit(xfrm_user_exit);
32851da177e4SLinus Torvalds MODULE_LICENSE("GPL");
32864fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3287f8cd5488SJamal Hadi Salim 
3288