xref: /linux/net/xfrm/xfrm_user.c (revision b6459415b384cb829f0b2a4268f211c789f6cf0b)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /* xfrm_user.c: User interface to configure xfrm engine.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Changes:
71da177e4SLinus Torvalds  *	Mitsuru KANDA @USAGI
81da177e4SLinus Torvalds  * 	Kazunori MIYAZAWA @USAGI
91da177e4SLinus Torvalds  * 	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
101da177e4SLinus Torvalds  * 		IPv6 support
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
14*b6459415SJakub Kicinski #include <linux/compat.h>
159409f38aSHerbert Xu #include <linux/crypto.h>
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/socket.h>
211da177e4SLinus Torvalds #include <linux/string.h>
221da177e4SLinus Torvalds #include <linux/net.h>
231da177e4SLinus Torvalds #include <linux/skbuff.h>
241da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
251da177e4SLinus Torvalds #include <linux/ipsec.h>
261da177e4SLinus Torvalds #include <linux/init.h>
271da177e4SLinus Torvalds #include <linux/security.h>
281da177e4SLinus Torvalds #include <net/sock.h>
291da177e4SLinus Torvalds #include <net/xfrm.h>
3088fc2c84SThomas Graf #include <net/netlink.h>
31fa6dd8a2SNicolas Dichtel #include <net/ah.h>
327c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
33dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
34e23c7194SMasahide NAKAMURA #include <linux/in6.h>
35e23c7194SMasahide NAKAMURA #endif
36e33d4f13SSowmini Varadhan #include <asm/unaligned.h>
371da177e4SLinus Torvalds 
385424f32eSThomas Graf static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
391da177e4SLinus Torvalds {
405424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
411da177e4SLinus Torvalds 	struct xfrm_algo *algp;
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	if (!rt)
441da177e4SLinus Torvalds 		return 0;
451da177e4SLinus Torvalds 
465424f32eSThomas Graf 	algp = nla_data(rt);
4706cd22f8SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_alg_len(algp))
4831c26852SHerbert Xu 		return -EINVAL;
4931c26852SHerbert Xu 
501da177e4SLinus Torvalds 	switch (type) {
511da177e4SLinus Torvalds 	case XFRMA_ALG_AUTH:
521da177e4SLinus Torvalds 	case XFRMA_ALG_CRYPT:
531da177e4SLinus Torvalds 	case XFRMA_ALG_COMP:
541da177e4SLinus Torvalds 		break;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	default:
571da177e4SLinus Torvalds 		return -EINVAL;
583ff50b79SStephen Hemminger 	}
591da177e4SLinus Torvalds 
60633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
611da177e4SLinus Torvalds 	return 0;
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
644447bb33SMartin Willi static int verify_auth_trunc(struct nlattr **attrs)
654447bb33SMartin Willi {
664447bb33SMartin Willi 	struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
674447bb33SMartin Willi 	struct xfrm_algo_auth *algp;
684447bb33SMartin Willi 
694447bb33SMartin Willi 	if (!rt)
704447bb33SMartin Willi 		return 0;
714447bb33SMartin Willi 
724447bb33SMartin Willi 	algp = nla_data(rt);
731bd963a7SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
744447bb33SMartin Willi 		return -EINVAL;
754447bb33SMartin Willi 
76633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
774447bb33SMartin Willi 	return 0;
784447bb33SMartin Willi }
794447bb33SMartin Willi 
801a6509d9SHerbert Xu static int verify_aead(struct nlattr **attrs)
811a6509d9SHerbert Xu {
821a6509d9SHerbert Xu 	struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
831a6509d9SHerbert Xu 	struct xfrm_algo_aead *algp;
841a6509d9SHerbert Xu 
851a6509d9SHerbert Xu 	if (!rt)
861a6509d9SHerbert Xu 		return 0;
871a6509d9SHerbert Xu 
881a6509d9SHerbert Xu 	algp = nla_data(rt);
89373b8eebSAlexey Dobriyan 	if (nla_len(rt) < (int)aead_len(algp))
901a6509d9SHerbert Xu 		return -EINVAL;
911a6509d9SHerbert Xu 
92633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
931a6509d9SHerbert Xu 	return 0;
941a6509d9SHerbert Xu }
951a6509d9SHerbert Xu 
965424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
97eb2971b6SMasahide NAKAMURA 			   xfrm_address_t **addrp)
98eb2971b6SMasahide NAKAMURA {
995424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
100eb2971b6SMasahide NAKAMURA 
101cf5cb79fSThomas Graf 	if (rt && addrp)
1025424f32eSThomas Graf 		*addrp = nla_data(rt);
103eb2971b6SMasahide NAKAMURA }
104df71837dSTrent Jaeger 
1055424f32eSThomas Graf static inline int verify_sec_ctx_len(struct nlattr **attrs)
106df71837dSTrent Jaeger {
1075424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
108df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
109df71837dSTrent Jaeger 
110df71837dSTrent Jaeger 	if (!rt)
111df71837dSTrent Jaeger 		return 0;
112df71837dSTrent Jaeger 
1135424f32eSThomas Graf 	uctx = nla_data(rt);
114171d449aSXin Long 	if (uctx->len > nla_len(rt) ||
115171d449aSXin Long 	    uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
116df71837dSTrent Jaeger 		return -EINVAL;
117df71837dSTrent Jaeger 
118df71837dSTrent Jaeger 	return 0;
119df71837dSTrent Jaeger }
120df71837dSTrent Jaeger 
121d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p,
122d8647b79SSteffen Klassert 				struct nlattr **attrs)
123d8647b79SSteffen Klassert {
124d8647b79SSteffen Klassert 	struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
125ecd79187SMathias Krause 	struct xfrm_replay_state_esn *rs;
126d8647b79SSteffen Klassert 
127ecd79187SMathias Krause 	if (!rt)
128d97ca5d7SFlorian Westphal 		return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
1297833aa05SSteffen Klassert 
130ecd79187SMathias Krause 	rs = nla_data(rt);
131ecd79187SMathias Krause 
132ecd79187SMathias Krause 	if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
133ecd79187SMathias Krause 		return -EINVAL;
134ecd79187SMathias Krause 
1355e708e47SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
136ecd79187SMathias Krause 	    nla_len(rt) != sizeof(*rs))
137ecd79187SMathias Krause 		return -EINVAL;
138d8647b79SSteffen Klassert 
13901714109SFan Du 	/* As only ESP and AH support ESN feature. */
14001714109SFan Du 	if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
14102aadf72SSteffen Klassert 		return -EINVAL;
14202aadf72SSteffen Klassert 
143d8647b79SSteffen Klassert 	if (p->replay_window != 0)
144d8647b79SSteffen Klassert 		return -EINVAL;
145d8647b79SSteffen Klassert 
146d8647b79SSteffen Klassert 	return 0;
147d8647b79SSteffen Klassert }
148df71837dSTrent Jaeger 
1491da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p,
1505424f32eSThomas Graf 			     struct nlattr **attrs)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	int err;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	err = -EINVAL;
1551da177e4SLinus Torvalds 	switch (p->family) {
1561da177e4SLinus Torvalds 	case AF_INET:
157b38ff407SAnirudh Gupta 		break;
158b38ff407SAnirudh Gupta 
159b38ff407SAnirudh Gupta 	case AF_INET6:
160b38ff407SAnirudh Gupta #if IS_ENABLED(CONFIG_IPV6)
161b38ff407SAnirudh Gupta 		break;
162b38ff407SAnirudh Gupta #else
163b38ff407SAnirudh Gupta 		err = -EAFNOSUPPORT;
164b38ff407SAnirudh Gupta 		goto out;
165b38ff407SAnirudh Gupta #endif
166b38ff407SAnirudh Gupta 
167b38ff407SAnirudh Gupta 	default:
168b38ff407SAnirudh Gupta 		goto out;
169b38ff407SAnirudh Gupta 	}
170b38ff407SAnirudh Gupta 
171b38ff407SAnirudh Gupta 	switch (p->sel.family) {
172b8d6d007SNicolas Dichtel 	case AF_UNSPEC:
173b8d6d007SNicolas Dichtel 		break;
174b8d6d007SNicolas Dichtel 
175b38ff407SAnirudh Gupta 	case AF_INET:
17607bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
17707bf7908SSteffen Klassert 			goto out;
17807bf7908SSteffen Klassert 
1791da177e4SLinus Torvalds 		break;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	case AF_INET6:
182dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
18307bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
18407bf7908SSteffen Klassert 			goto out;
18507bf7908SSteffen Klassert 
1861da177e4SLinus Torvalds 		break;
1871da177e4SLinus Torvalds #else
1881da177e4SLinus Torvalds 		err = -EAFNOSUPPORT;
1891da177e4SLinus Torvalds 		goto out;
1901da177e4SLinus Torvalds #endif
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	default:
1931da177e4SLinus Torvalds 		goto out;
1943ff50b79SStephen Hemminger 	}
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	err = -EINVAL;
1971da177e4SLinus Torvalds 	switch (p->id.proto) {
1981da177e4SLinus Torvalds 	case IPPROTO_AH:
1994447bb33SMartin Willi 		if ((!attrs[XFRMA_ALG_AUTH]	&&
2004447bb33SMartin Willi 		     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
2011a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
20235a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
20335d2856bSMartin Willi 		    attrs[XFRMA_ALG_COMP]	||
204a0e5ef53STobias Brunner 		    attrs[XFRMA_TFCPAD])
2051da177e4SLinus Torvalds 			goto out;
2061da177e4SLinus Torvalds 		break;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	case IPPROTO_ESP:
2091a6509d9SHerbert Xu 		if (attrs[XFRMA_ALG_COMP])
2101a6509d9SHerbert Xu 			goto out;
2111a6509d9SHerbert Xu 		if (!attrs[XFRMA_ALG_AUTH] &&
2124447bb33SMartin Willi 		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
2131a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_CRYPT] &&
2141a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_AEAD])
2151a6509d9SHerbert Xu 			goto out;
2161a6509d9SHerbert Xu 		if ((attrs[XFRMA_ALG_AUTH] ||
2174447bb33SMartin Willi 		     attrs[XFRMA_ALG_AUTH_TRUNC] ||
2181a6509d9SHerbert Xu 		     attrs[XFRMA_ALG_CRYPT]) &&
2191a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD])
2201da177e4SLinus Torvalds 			goto out;
22135d2856bSMartin Willi 		if (attrs[XFRMA_TFCPAD] &&
22235d2856bSMartin Willi 		    p->mode != XFRM_MODE_TUNNEL)
22335d2856bSMartin Willi 			goto out;
2241da177e4SLinus Torvalds 		break;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	case IPPROTO_COMP:
22735a7aa08SThomas Graf 		if (!attrs[XFRMA_ALG_COMP]	||
2281a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
22935a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2304447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
23135d2856bSMartin Willi 		    attrs[XFRMA_ALG_CRYPT]	||
232a0e5ef53STobias Brunner 		    attrs[XFRMA_TFCPAD]		||
233a0e5ef53STobias Brunner 		    (ntohl(p->id.spi) >= 0x10000))
2341da177e4SLinus Torvalds 			goto out;
2351da177e4SLinus Torvalds 		break;
2361da177e4SLinus Torvalds 
237dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
238e23c7194SMasahide NAKAMURA 	case IPPROTO_DSTOPTS:
239e23c7194SMasahide NAKAMURA 	case IPPROTO_ROUTING:
24035a7aa08SThomas Graf 		if (attrs[XFRMA_ALG_COMP]	||
24135a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2424447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
2431a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
24435a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
24535a7aa08SThomas Graf 		    attrs[XFRMA_ENCAP]		||
24635a7aa08SThomas Graf 		    attrs[XFRMA_SEC_CTX]	||
24735d2856bSMartin Willi 		    attrs[XFRMA_TFCPAD]		||
24835a7aa08SThomas Graf 		    !attrs[XFRMA_COADDR])
249e23c7194SMasahide NAKAMURA 			goto out;
250e23c7194SMasahide NAKAMURA 		break;
251e23c7194SMasahide NAKAMURA #endif
252e23c7194SMasahide NAKAMURA 
2531da177e4SLinus Torvalds 	default:
2541da177e4SLinus Torvalds 		goto out;
2553ff50b79SStephen Hemminger 	}
2561da177e4SLinus Torvalds 
2571a6509d9SHerbert Xu 	if ((err = verify_aead(attrs)))
2581a6509d9SHerbert Xu 		goto out;
2594447bb33SMartin Willi 	if ((err = verify_auth_trunc(attrs)))
2604447bb33SMartin Willi 		goto out;
26135a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
2621da177e4SLinus Torvalds 		goto out;
26335a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
2641da177e4SLinus Torvalds 		goto out;
26535a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
2661da177e4SLinus Torvalds 		goto out;
26735a7aa08SThomas Graf 	if ((err = verify_sec_ctx_len(attrs)))
268df71837dSTrent Jaeger 		goto out;
269d8647b79SSteffen Klassert 	if ((err = verify_replay(p, attrs)))
270d8647b79SSteffen Klassert 		goto out;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	err = -EINVAL;
2731da177e4SLinus Torvalds 	switch (p->mode) {
2747e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TRANSPORT:
2757e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TUNNEL:
276060f02a3SNoriaki TAKAMIYA 	case XFRM_MODE_ROUTEOPTIMIZATION:
2770a69452cSDiego Beltrami 	case XFRM_MODE_BEET:
2781da177e4SLinus Torvalds 		break;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	default:
2811da177e4SLinus Torvalds 		goto out;
2823ff50b79SStephen Hemminger 	}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	err = 0;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds out:
2871da177e4SLinus Torvalds 	return err;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
2916f2f19edSDavid S. Miller 			   struct xfrm_algo_desc *(*get_byname)(const char *, int),
2925424f32eSThomas Graf 			   struct nlattr *rta)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	struct xfrm_algo *p, *ualg;
2951da177e4SLinus Torvalds 	struct xfrm_algo_desc *algo;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	if (!rta)
2981da177e4SLinus Torvalds 		return 0;
2991da177e4SLinus Torvalds 
3005424f32eSThomas Graf 	ualg = nla_data(rta);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	algo = get_byname(ualg->alg_name, 1);
3031da177e4SLinus Torvalds 	if (!algo)
3041da177e4SLinus Torvalds 		return -ENOSYS;
3051da177e4SLinus Torvalds 	*props = algo->desc.sadb_alg_id;
3061da177e4SLinus Torvalds 
3070f99be0dSEric Dumazet 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
3081da177e4SLinus Torvalds 	if (!p)
3091da177e4SLinus Torvalds 		return -ENOMEM;
3101da177e4SLinus Torvalds 
31104ff1260SHerbert Xu 	strcpy(p->alg_name, algo->name);
3121da177e4SLinus Torvalds 	*algpp = p;
3131da177e4SLinus Torvalds 	return 0;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
31669b0137fSHerbert Xu static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
31769b0137fSHerbert Xu {
31869b0137fSHerbert Xu 	struct xfrm_algo *p, *ualg;
31969b0137fSHerbert Xu 	struct xfrm_algo_desc *algo;
32069b0137fSHerbert Xu 
32169b0137fSHerbert Xu 	if (!rta)
32269b0137fSHerbert Xu 		return 0;
32369b0137fSHerbert Xu 
32469b0137fSHerbert Xu 	ualg = nla_data(rta);
32569b0137fSHerbert Xu 
32669b0137fSHerbert Xu 	algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
32769b0137fSHerbert Xu 	if (!algo)
32869b0137fSHerbert Xu 		return -ENOSYS;
32969b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
33069b0137fSHerbert Xu 
33169b0137fSHerbert Xu 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
33269b0137fSHerbert Xu 	if (!p)
33369b0137fSHerbert Xu 		return -ENOMEM;
33469b0137fSHerbert Xu 
33569b0137fSHerbert Xu 	strcpy(p->alg_name, algo->name);
33669b0137fSHerbert Xu 	x->ealg = p;
33769b0137fSHerbert Xu 	x->geniv = algo->uinfo.encr.geniv;
33869b0137fSHerbert Xu 	return 0;
33969b0137fSHerbert Xu }
34069b0137fSHerbert Xu 
3414447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
3424447bb33SMartin Willi 		       struct nlattr *rta)
3434447bb33SMartin Willi {
3444447bb33SMartin Willi 	struct xfrm_algo *ualg;
3454447bb33SMartin Willi 	struct xfrm_algo_auth *p;
3464447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3474447bb33SMartin Willi 
3484447bb33SMartin Willi 	if (!rta)
3494447bb33SMartin Willi 		return 0;
3504447bb33SMartin Willi 
3514447bb33SMartin Willi 	ualg = nla_data(rta);
3524447bb33SMartin Willi 
3534447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3544447bb33SMartin Willi 	if (!algo)
3554447bb33SMartin Willi 		return -ENOSYS;
3564447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3574447bb33SMartin Willi 
3584447bb33SMartin Willi 	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
3594447bb33SMartin Willi 	if (!p)
3604447bb33SMartin Willi 		return -ENOMEM;
3614447bb33SMartin Willi 
3624447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3634447bb33SMartin Willi 	p->alg_key_len = ualg->alg_key_len;
3644447bb33SMartin Willi 	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3654447bb33SMartin Willi 	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
3664447bb33SMartin Willi 
3674447bb33SMartin Willi 	*algpp = p;
3684447bb33SMartin Willi 	return 0;
3694447bb33SMartin Willi }
3704447bb33SMartin Willi 
3714447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
3724447bb33SMartin Willi 			     struct nlattr *rta)
3734447bb33SMartin Willi {
3744447bb33SMartin Willi 	struct xfrm_algo_auth *p, *ualg;
3754447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3764447bb33SMartin Willi 
3774447bb33SMartin Willi 	if (!rta)
3784447bb33SMartin Willi 		return 0;
3794447bb33SMartin Willi 
3804447bb33SMartin Willi 	ualg = nla_data(rta);
3814447bb33SMartin Willi 
3824447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3834447bb33SMartin Willi 	if (!algo)
3844447bb33SMartin Willi 		return -ENOSYS;
385689f1c9dSHerbert Xu 	if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
3864447bb33SMartin Willi 		return -EINVAL;
3874447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3884447bb33SMartin Willi 
3894447bb33SMartin Willi 	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
3904447bb33SMartin Willi 	if (!p)
3914447bb33SMartin Willi 		return -ENOMEM;
3924447bb33SMartin Willi 
3934447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3944447bb33SMartin Willi 	if (!p->alg_trunc_len)
3954447bb33SMartin Willi 		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3964447bb33SMartin Willi 
3974447bb33SMartin Willi 	*algpp = p;
3984447bb33SMartin Willi 	return 0;
3994447bb33SMartin Willi }
4004447bb33SMartin Willi 
40169b0137fSHerbert Xu static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
4021a6509d9SHerbert Xu {
4031a6509d9SHerbert Xu 	struct xfrm_algo_aead *p, *ualg;
4041a6509d9SHerbert Xu 	struct xfrm_algo_desc *algo;
4051a6509d9SHerbert Xu 
4061a6509d9SHerbert Xu 	if (!rta)
4071a6509d9SHerbert Xu 		return 0;
4081a6509d9SHerbert Xu 
4091a6509d9SHerbert Xu 	ualg = nla_data(rta);
4101a6509d9SHerbert Xu 
4111a6509d9SHerbert Xu 	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
4121a6509d9SHerbert Xu 	if (!algo)
4131a6509d9SHerbert Xu 		return -ENOSYS;
41469b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
4151a6509d9SHerbert Xu 
4161a6509d9SHerbert Xu 	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
4171a6509d9SHerbert Xu 	if (!p)
4181a6509d9SHerbert Xu 		return -ENOMEM;
4191a6509d9SHerbert Xu 
4201a6509d9SHerbert Xu 	strcpy(p->alg_name, algo->name);
42169b0137fSHerbert Xu 	x->aead = p;
42269b0137fSHerbert Xu 	x->geniv = algo->uinfo.aead.geniv;
4231a6509d9SHerbert Xu 	return 0;
4241a6509d9SHerbert Xu }
4251a6509d9SHerbert Xu 
426e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
427e2b19125SSteffen Klassert 					 struct nlattr *rp)
428e2b19125SSteffen Klassert {
429e2b19125SSteffen Klassert 	struct xfrm_replay_state_esn *up;
4305e708e47SAlexey Dobriyan 	unsigned int ulen;
431e2b19125SSteffen Klassert 
432e2b19125SSteffen Klassert 	if (!replay_esn || !rp)
433e2b19125SSteffen Klassert 		return 0;
434e2b19125SSteffen Klassert 
435e2b19125SSteffen Klassert 	up = nla_data(rp);
436ecd79187SMathias Krause 	ulen = xfrm_replay_state_esn_len(up);
437e2b19125SSteffen Klassert 
438f843ee6dSAndy Whitcroft 	/* Check the overall length and the internal bitmap length to avoid
439f843ee6dSAndy Whitcroft 	 * potential overflow. */
4405e708e47SAlexey Dobriyan 	if (nla_len(rp) < (int)ulen ||
441f843ee6dSAndy Whitcroft 	    xfrm_replay_state_esn_len(replay_esn) != ulen ||
442f843ee6dSAndy Whitcroft 	    replay_esn->bmp_len != up->bmp_len)
443e2b19125SSteffen Klassert 		return -EINVAL;
444e2b19125SSteffen Klassert 
445677e806dSAndy Whitcroft 	if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
446677e806dSAndy Whitcroft 		return -EINVAL;
447677e806dSAndy Whitcroft 
448e2b19125SSteffen Klassert 	return 0;
449e2b19125SSteffen Klassert }
450e2b19125SSteffen Klassert 
451d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
452d8647b79SSteffen Klassert 				       struct xfrm_replay_state_esn **preplay_esn,
453d8647b79SSteffen Klassert 				       struct nlattr *rta)
454d8647b79SSteffen Klassert {
455d8647b79SSteffen Klassert 	struct xfrm_replay_state_esn *p, *pp, *up;
4565e708e47SAlexey Dobriyan 	unsigned int klen, ulen;
457d8647b79SSteffen Klassert 
458d8647b79SSteffen Klassert 	if (!rta)
459d8647b79SSteffen Klassert 		return 0;
460d8647b79SSteffen Klassert 
461d8647b79SSteffen Klassert 	up = nla_data(rta);
462ecd79187SMathias Krause 	klen = xfrm_replay_state_esn_len(up);
4635e708e47SAlexey Dobriyan 	ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
464d8647b79SSteffen Klassert 
465ecd79187SMathias Krause 	p = kzalloc(klen, GFP_KERNEL);
466d8647b79SSteffen Klassert 	if (!p)
467d8647b79SSteffen Klassert 		return -ENOMEM;
468d8647b79SSteffen Klassert 
469ecd79187SMathias Krause 	pp = kzalloc(klen, GFP_KERNEL);
470d8647b79SSteffen Klassert 	if (!pp) {
471d8647b79SSteffen Klassert 		kfree(p);
472d8647b79SSteffen Klassert 		return -ENOMEM;
473d8647b79SSteffen Klassert 	}
474d8647b79SSteffen Klassert 
475ecd79187SMathias Krause 	memcpy(p, up, ulen);
476ecd79187SMathias Krause 	memcpy(pp, up, ulen);
477ecd79187SMathias Krause 
478d8647b79SSteffen Klassert 	*replay_esn = p;
479d8647b79SSteffen Klassert 	*preplay_esn = pp;
480d8647b79SSteffen Klassert 
481d8647b79SSteffen Klassert 	return 0;
482d8647b79SSteffen Klassert }
483d8647b79SSteffen Klassert 
484a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
485df71837dSTrent Jaeger {
486a1b831f2SAlexey Dobriyan 	unsigned int len = 0;
487df71837dSTrent Jaeger 
488df71837dSTrent Jaeger 	if (xfrm_ctx) {
489df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
490df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
491df71837dSTrent Jaeger 	}
492df71837dSTrent Jaeger 	return len;
493df71837dSTrent Jaeger }
494df71837dSTrent Jaeger 
4951da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
4961da177e4SLinus Torvalds {
4971da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
4981da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
4991da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
5001da177e4SLinus Torvalds 	x->props.mode = p->mode;
50133fce60dSFan Du 	x->props.replay_window = min_t(unsigned int, p->replay_window,
50233fce60dSFan Du 					sizeof(x->replay.bitmap) * 8);
5031da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
5041da177e4SLinus Torvalds 	x->props.family = p->family;
50554489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
5061da177e4SLinus Torvalds 	x->props.flags = p->flags;
507196b0036SHerbert Xu 
508ccf9b3b8SSteffen Klassert 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
509196b0036SHerbert Xu 		x->sel.family = p->family;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
512d51d081dSJamal Hadi Salim /*
513d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
514d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
515d51d081dSJamal Hadi Salim  *
516d51d081dSJamal Hadi Salim */
517e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
518e3ac104dSMathias Krause 				  int update_esn)
519d51d081dSJamal Hadi Salim {
5205424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
521e3ac104dSMathias Krause 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
5225424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
5235424f32eSThomas Graf 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
5245424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
525d51d081dSJamal Hadi Salim 
526d8647b79SSteffen Klassert 	if (re) {
527d8647b79SSteffen Klassert 		struct xfrm_replay_state_esn *replay_esn;
528d8647b79SSteffen Klassert 		replay_esn = nla_data(re);
529d8647b79SSteffen Klassert 		memcpy(x->replay_esn, replay_esn,
530d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
531d8647b79SSteffen Klassert 		memcpy(x->preplay_esn, replay_esn,
532d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
533d8647b79SSteffen Klassert 	}
534d8647b79SSteffen Klassert 
535d51d081dSJamal Hadi Salim 	if (rp) {
536d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
5375424f32eSThomas Graf 		replay = nla_data(rp);
538d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
539d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
540d51d081dSJamal Hadi Salim 	}
541d51d081dSJamal Hadi Salim 
542d51d081dSJamal Hadi Salim 	if (lt) {
543d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
5445424f32eSThomas Graf 		ltime = nla_data(lt);
545d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
546d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
547d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
548d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
549d51d081dSJamal Hadi Salim 	}
550d51d081dSJamal Hadi Salim 
551cf5cb79fSThomas Graf 	if (et)
5525424f32eSThomas Graf 		x->replay_maxage = nla_get_u32(et);
553d51d081dSJamal Hadi Salim 
554cf5cb79fSThomas Graf 	if (rt)
5555424f32eSThomas Graf 		x->replay_maxdiff = nla_get_u32(rt);
556d51d081dSJamal Hadi Salim }
557d51d081dSJamal Hadi Salim 
5589b42c1f1SSteffen Klassert static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
5599b42c1f1SSteffen Klassert {
5609b42c1f1SSteffen Klassert 	if (attrs[XFRMA_SET_MARK]) {
5619b42c1f1SSteffen Klassert 		m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
5629b42c1f1SSteffen Klassert 		if (attrs[XFRMA_SET_MARK_MASK])
5639b42c1f1SSteffen Klassert 			m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
5649b42c1f1SSteffen Klassert 		else
5659b42c1f1SSteffen Klassert 			m->m = 0xffffffff;
5669b42c1f1SSteffen Klassert 	} else {
5679b42c1f1SSteffen Klassert 		m->v = m->m = 0;
5689b42c1f1SSteffen Klassert 	}
5699b42c1f1SSteffen Klassert }
5709b42c1f1SSteffen Klassert 
571fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
572fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
5735424f32eSThomas Graf 					       struct nlattr **attrs,
5741da177e4SLinus Torvalds 					       int *errp)
5751da177e4SLinus Torvalds {
576fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
5771da177e4SLinus Torvalds 	int err = -ENOMEM;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	if (!x)
5801da177e4SLinus Torvalds 		goto error_no_put;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	copy_from_user_state(x, p);
5831da177e4SLinus Torvalds 
5846fd06963SSteffen Klassert 	if (attrs[XFRMA_ENCAP]) {
5856fd06963SSteffen Klassert 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
5866fd06963SSteffen Klassert 				   sizeof(*x->encap), GFP_KERNEL);
5876fd06963SSteffen Klassert 		if (x->encap == NULL)
5886fd06963SSteffen Klassert 			goto error;
5896fd06963SSteffen Klassert 	}
5906fd06963SSteffen Klassert 
5916fd06963SSteffen Klassert 	if (attrs[XFRMA_COADDR]) {
5926fd06963SSteffen Klassert 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
5936fd06963SSteffen Klassert 				    sizeof(*x->coaddr), GFP_KERNEL);
5946fd06963SSteffen Klassert 		if (x->coaddr == NULL)
5956fd06963SSteffen Klassert 			goto error;
5966fd06963SSteffen Klassert 	}
5976fd06963SSteffen Klassert 
598a947b0a9SNicolas Dichtel 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
599a947b0a9SNicolas Dichtel 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
600a947b0a9SNicolas Dichtel 
60169b0137fSHerbert Xu 	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
6021a6509d9SHerbert Xu 		goto error;
6034447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
6044447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
6054447bb33SMartin Willi 		goto error;
6064447bb33SMartin Willi 	if (!x->props.aalgo) {
6074447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
60835a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
6091da177e4SLinus Torvalds 			goto error;
6104447bb33SMartin Willi 	}
61169b0137fSHerbert Xu 	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
6121da177e4SLinus Torvalds 		goto error;
6131da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
6141da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
61535a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
6161da177e4SLinus Torvalds 		goto error;
617fd21150aSThomas Graf 
61835d2856bSMartin Willi 	if (attrs[XFRMA_TFCPAD])
61935d2856bSMartin Willi 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
62035d2856bSMartin Willi 
6216f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &x->mark);
6226f26b61eSJamal Hadi Salim 
6239b42c1f1SSteffen Klassert 	xfrm_smark_init(attrs, &x->props.smark);
624077fbac4SLorenzo Colitti 
6257e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
6267e652640SSteffen Klassert 		x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
627fd21150aSThomas Graf 
628ffdb5211SIlan Tayari 	err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
6291da177e4SLinus Torvalds 	if (err)
6301da177e4SLinus Torvalds 		goto error;
6311da177e4SLinus Torvalds 
6322f30ea50SMathias Krause 	if (attrs[XFRMA_SEC_CTX]) {
6332f30ea50SMathias Krause 		err = security_xfrm_state_alloc(x,
6342f30ea50SMathias Krause 						nla_data(attrs[XFRMA_SEC_CTX]));
6352f30ea50SMathias Krause 		if (err)
636df71837dSTrent Jaeger 			goto error;
6372f30ea50SMathias Krause 	}
638df71837dSTrent Jaeger 
639d8647b79SSteffen Klassert 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
640d8647b79SSteffen Klassert 					       attrs[XFRMA_REPLAY_ESN_VAL])))
641d8647b79SSteffen Klassert 		goto error;
642d8647b79SSteffen Klassert 
6431da177e4SLinus Torvalds 	x->km.seq = p->seq;
644b27aeadbSAlexey Dobriyan 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
645d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
646b27aeadbSAlexey Dobriyan 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
647d51d081dSJamal Hadi Salim 
6489fdc4883SSteffen Klassert 	if ((err = xfrm_init_replay(x)))
6499fdc4883SSteffen Klassert 		goto error;
650d51d081dSJamal Hadi Salim 
6519fdc4883SSteffen Klassert 	/* override default values from above */
652e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 0);
6531da177e4SLinus Torvalds 
654cc01572eSYossi Kuperman 	/* configure the hardware if offload is requested */
655cc01572eSYossi Kuperman 	if (attrs[XFRMA_OFFLOAD_DEV]) {
656cc01572eSYossi Kuperman 		err = xfrm_dev_state_add(net, x,
657cc01572eSYossi Kuperman 					 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
658cc01572eSYossi Kuperman 		if (err)
659cc01572eSYossi Kuperman 			goto error;
660cc01572eSYossi Kuperman 	}
661cc01572eSYossi Kuperman 
6621da177e4SLinus Torvalds 	return x;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds error:
6651da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
6661da177e4SLinus Torvalds 	xfrm_state_put(x);
6671da177e4SLinus Torvalds error_no_put:
6681da177e4SLinus Torvalds 	*errp = err;
6691da177e4SLinus Torvalds 	return NULL;
6701da177e4SLinus Torvalds }
6711da177e4SLinus Torvalds 
67222e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
6735424f32eSThomas Graf 		struct nlattr **attrs)
6741da177e4SLinus Torvalds {
675fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6767b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
6771da177e4SLinus Torvalds 	struct xfrm_state *x;
6781da177e4SLinus Torvalds 	int err;
67926b15dadSJamal Hadi Salim 	struct km_event c;
6801da177e4SLinus Torvalds 
68135a7aa08SThomas Graf 	err = verify_newsa_info(p, attrs);
6821da177e4SLinus Torvalds 	if (err)
6831da177e4SLinus Torvalds 		return err;
6841da177e4SLinus Torvalds 
685fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
6861da177e4SLinus Torvalds 	if (!x)
6871da177e4SLinus Torvalds 		return err;
6881da177e4SLinus Torvalds 
68926b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
6901da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
6911da177e4SLinus Torvalds 		err = xfrm_state_add(x);
6921da177e4SLinus Torvalds 	else
6931da177e4SLinus Torvalds 		err = xfrm_state_update(x);
6941da177e4SLinus Torvalds 
6952e71029eSTetsuo Handa 	xfrm_audit_state_add(x, err ? 0 : 1, true);
696161a09e7SJoy Latten 
6971da177e4SLinus Torvalds 	if (err < 0) {
6981da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
699c5d4d7d8SSteffen Klassert 		xfrm_dev_state_delete(x);
70021380b81SHerbert Xu 		__xfrm_state_put(x);
7017d6dfe1fSPatrick McHardy 		goto out;
7021da177e4SLinus Torvalds 	}
7031da177e4SLinus Torvalds 
704cc01572eSYossi Kuperman 	if (x->km.state == XFRM_STATE_VOID)
705cc01572eSYossi Kuperman 		x->km.state = XFRM_STATE_VALID;
706cc01572eSYossi Kuperman 
70726b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
70815e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
709f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
71026b15dadSJamal Hadi Salim 
71126b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7127d6dfe1fSPatrick McHardy out:
71326b15dadSJamal Hadi Salim 	xfrm_state_put(x);
7141da177e4SLinus Torvalds 	return err;
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
717fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
718fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
7195424f32eSThomas Graf 						 struct nlattr **attrs,
720eb2971b6SMasahide NAKAMURA 						 int *errp)
721eb2971b6SMasahide NAKAMURA {
722eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
7236f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
724eb2971b6SMasahide NAKAMURA 	int err;
7256f26b61eSJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
726eb2971b6SMasahide NAKAMURA 
727eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
728eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
7296f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
730eb2971b6SMasahide NAKAMURA 	} else {
731eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
732eb2971b6SMasahide NAKAMURA 
73335a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
734eb2971b6SMasahide NAKAMURA 		if (!saddr) {
735eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
736eb2971b6SMasahide NAKAMURA 			goto out;
737eb2971b6SMasahide NAKAMURA 		}
738eb2971b6SMasahide NAKAMURA 
7399abbffeeSMasahide NAKAMURA 		err = -ESRCH;
7406f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup_byaddr(net, mark,
7416f26b61eSJamal Hadi Salim 					     &p->daddr, saddr,
742221df1edSAlexey Dobriyan 					     p->proto, p->family);
743eb2971b6SMasahide NAKAMURA 	}
744eb2971b6SMasahide NAKAMURA 
745eb2971b6SMasahide NAKAMURA  out:
746eb2971b6SMasahide NAKAMURA 	if (!x && errp)
747eb2971b6SMasahide NAKAMURA 		*errp = err;
748eb2971b6SMasahide NAKAMURA 	return x;
749eb2971b6SMasahide NAKAMURA }
750eb2971b6SMasahide NAKAMURA 
75122e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
7525424f32eSThomas Graf 		struct nlattr **attrs)
7531da177e4SLinus Torvalds {
754fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
7551da177e4SLinus Torvalds 	struct xfrm_state *x;
756eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
75726b15dadSJamal Hadi Salim 	struct km_event c;
7587b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
7591da177e4SLinus Torvalds 
760fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
7611da177e4SLinus Torvalds 	if (x == NULL)
762eb2971b6SMasahide NAKAMURA 		return err;
7631da177e4SLinus Torvalds 
7646f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
765c8c05a8eSCatherine Zhang 		goto out;
766c8c05a8eSCatherine Zhang 
7671da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
768c8c05a8eSCatherine Zhang 		err = -EPERM;
769c8c05a8eSCatherine Zhang 		goto out;
7701da177e4SLinus Torvalds 	}
7711da177e4SLinus Torvalds 
77226b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
773161a09e7SJoy Latten 
774c8c05a8eSCatherine Zhang 	if (err < 0)
775c8c05a8eSCatherine Zhang 		goto out;
77626b15dadSJamal Hadi Salim 
77726b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
77815e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
779f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
78026b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7811da177e4SLinus Torvalds 
782c8c05a8eSCatherine Zhang out:
7832e71029eSTetsuo Handa 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
784c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
78526b15dadSJamal Hadi Salim 	return err;
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
7891da177e4SLinus Torvalds {
790f778a636SMathias Krause 	memset(p, 0, sizeof(*p));
7911da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
7921da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
7931da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
7941da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
795e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
796e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay, &p->stats.replay);
797e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
79854489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
7991da177e4SLinus Torvalds 	p->mode = x->props.mode;
8001da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
8011da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
8021da177e4SLinus Torvalds 	p->family = x->props.family;
8031da177e4SLinus Torvalds 	p->flags = x->props.flags;
8041da177e4SLinus Torvalds 	p->seq = x->km.seq;
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds struct xfrm_dump_info {
8081da177e4SLinus Torvalds 	struct sk_buff *in_skb;
8091da177e4SLinus Torvalds 	struct sk_buff *out_skb;
8101da177e4SLinus Torvalds 	u32 nlmsg_seq;
8111da177e4SLinus Torvalds 	u16 nlmsg_flags;
8121da177e4SLinus Torvalds };
8131da177e4SLinus Torvalds 
814c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
815c0144beaSThomas Graf {
816c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
817c0144beaSThomas Graf 	struct nlattr *attr;
81868325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
819c0144beaSThomas Graf 
820c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
821c0144beaSThomas Graf 	if (attr == NULL)
822c0144beaSThomas Graf 		return -EMSGSIZE;
823c0144beaSThomas Graf 
824c0144beaSThomas Graf 	uctx = nla_data(attr);
825c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
826c0144beaSThomas Graf 	uctx->len = ctx_size;
827c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
828c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
829c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
830c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
831c0144beaSThomas Graf 
832c0144beaSThomas Graf 	return 0;
833c0144beaSThomas Graf }
834c0144beaSThomas Graf 
835d77e38e6SSteffen Klassert static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
836d77e38e6SSteffen Klassert {
837d77e38e6SSteffen Klassert 	struct xfrm_user_offload *xuo;
838d77e38e6SSteffen Klassert 	struct nlattr *attr;
839d77e38e6SSteffen Klassert 
840d77e38e6SSteffen Klassert 	attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
841d77e38e6SSteffen Klassert 	if (attr == NULL)
842d77e38e6SSteffen Klassert 		return -EMSGSIZE;
843d77e38e6SSteffen Klassert 
844d77e38e6SSteffen Klassert 	xuo = nla_data(attr);
8455fe0d4bdSMathias Krause 	memset(xuo, 0, sizeof(*xuo));
846d77e38e6SSteffen Klassert 	xuo->ifindex = xso->dev->ifindex;
847d77e38e6SSteffen Klassert 	xuo->flags = xso->flags;
848d77e38e6SSteffen Klassert 
849d77e38e6SSteffen Klassert 	return 0;
850d77e38e6SSteffen Klassert }
851d77e38e6SSteffen Klassert 
852c7a5899eSAntony Antony static bool xfrm_redact(void)
853c7a5899eSAntony Antony {
854c7a5899eSAntony Antony 	return IS_ENABLED(CONFIG_SECURITY) &&
855c7a5899eSAntony Antony 		security_locked_down(LOCKDOWN_XFRM_SECRET);
856c7a5899eSAntony Antony }
857c7a5899eSAntony Antony 
8584447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
8594447bb33SMartin Willi {
8604447bb33SMartin Willi 	struct xfrm_algo *algo;
861c7a5899eSAntony Antony 	struct xfrm_algo_auth *ap;
8624447bb33SMartin Willi 	struct nlattr *nla;
863c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
8644447bb33SMartin Willi 
8654447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
8664447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
8674447bb33SMartin Willi 	if (!nla)
8684447bb33SMartin Willi 		return -EMSGSIZE;
8694447bb33SMartin Willi 	algo = nla_data(nla);
8704c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
871c7a5899eSAntony Antony 
872c7a5899eSAntony Antony 	if (redact_secret && auth->alg_key_len)
873c7a5899eSAntony Antony 		memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
874c7a5899eSAntony Antony 	else
875c7a5899eSAntony Antony 		memcpy(algo->alg_key, auth->alg_key,
876c7a5899eSAntony Antony 		       (auth->alg_key_len + 7) / 8);
8774447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
8784447bb33SMartin Willi 
879c7a5899eSAntony Antony 	nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
880c7a5899eSAntony Antony 	if (!nla)
881c7a5899eSAntony Antony 		return -EMSGSIZE;
882c7a5899eSAntony Antony 	ap = nla_data(nla);
883c7a5899eSAntony Antony 	memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
884c7a5899eSAntony Antony 	if (redact_secret && auth->alg_key_len)
885c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
886c7a5899eSAntony Antony 	else
887c7a5899eSAntony Antony 		memcpy(ap->alg_key, auth->alg_key,
888c7a5899eSAntony Antony 		       (auth->alg_key_len + 7) / 8);
889c7a5899eSAntony Antony 	return 0;
890c7a5899eSAntony Antony }
891c7a5899eSAntony Antony 
892c7a5899eSAntony Antony static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
893c7a5899eSAntony Antony {
894c7a5899eSAntony Antony 	struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
895c7a5899eSAntony Antony 	struct xfrm_algo_aead *ap;
896c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
897c7a5899eSAntony Antony 
898c7a5899eSAntony Antony 	if (!nla)
899c7a5899eSAntony Antony 		return -EMSGSIZE;
900c7a5899eSAntony Antony 
901c7a5899eSAntony Antony 	ap = nla_data(nla);
902c7a5899eSAntony Antony 	memcpy(ap, aead, sizeof(*aead));
903c7a5899eSAntony Antony 
904c7a5899eSAntony Antony 	if (redact_secret && aead->alg_key_len)
905c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
906c7a5899eSAntony Antony 	else
907c7a5899eSAntony Antony 		memcpy(ap->alg_key, aead->alg_key,
908c7a5899eSAntony Antony 		       (aead->alg_key_len + 7) / 8);
909c7a5899eSAntony Antony 	return 0;
910c7a5899eSAntony Antony }
911c7a5899eSAntony Antony 
912c7a5899eSAntony Antony static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
913c7a5899eSAntony Antony {
914c7a5899eSAntony Antony 	struct xfrm_algo *ap;
915c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
916c7a5899eSAntony Antony 	struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
917c7a5899eSAntony Antony 					 xfrm_alg_len(ealg));
918c7a5899eSAntony Antony 	if (!nla)
919c7a5899eSAntony Antony 		return -EMSGSIZE;
920c7a5899eSAntony Antony 
921c7a5899eSAntony Antony 	ap = nla_data(nla);
922c7a5899eSAntony Antony 	memcpy(ap, ealg, sizeof(*ealg));
923c7a5899eSAntony Antony 
924c7a5899eSAntony Antony 	if (redact_secret && ealg->alg_key_len)
925c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
926c7a5899eSAntony Antony 	else
927c7a5899eSAntony Antony 		memcpy(ap->alg_key, ealg->alg_key,
928c7a5899eSAntony Antony 		       (ealg->alg_key_len + 7) / 8);
929c7a5899eSAntony Antony 
9304447bb33SMartin Willi 	return 0;
9314447bb33SMartin Willi }
9324447bb33SMartin Willi 
9339b42c1f1SSteffen Klassert static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
9349b42c1f1SSteffen Klassert {
9359b42c1f1SSteffen Klassert 	int ret = 0;
9369b42c1f1SSteffen Klassert 
9379b42c1f1SSteffen Klassert 	if (m->v | m->m) {
9389b42c1f1SSteffen Klassert 		ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
9399b42c1f1SSteffen Klassert 		if (!ret)
9409b42c1f1SSteffen Klassert 			ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
9419b42c1f1SSteffen Klassert 	}
9429b42c1f1SSteffen Klassert 	return ret;
9439b42c1f1SSteffen Klassert }
9449b42c1f1SSteffen Klassert 
94568325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
94668325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
94768325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
94868325d3bSHerbert Xu 				    struct sk_buff *skb)
9491da177e4SLinus Torvalds {
9501d1e34ddSDavid S. Miller 	int ret = 0;
9511d1e34ddSDavid S. Miller 
9521da177e4SLinus Torvalds 	copy_to_user_state(x, p);
9531da177e4SLinus Torvalds 
954a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
955a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
956a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
957a947b0a9SNicolas Dichtel 		if (ret)
958a947b0a9SNicolas Dichtel 			goto out;
959a947b0a9SNicolas Dichtel 	}
960a947b0a9SNicolas Dichtel 
9611d1e34ddSDavid S. Miller 	if (x->coaddr) {
9621d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
9631d1e34ddSDavid S. Miller 		if (ret)
9641d1e34ddSDavid S. Miller 			goto out;
9651d1e34ddSDavid S. Miller 	}
9661d1e34ddSDavid S. Miller 	if (x->lastused) {
967de95c4a4SNicolas Dichtel 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
968de95c4a4SNicolas Dichtel 					XFRMA_PAD);
9691d1e34ddSDavid S. Miller 		if (ret)
9701d1e34ddSDavid S. Miller 			goto out;
9711d1e34ddSDavid S. Miller 	}
9721d1e34ddSDavid S. Miller 	if (x->aead) {
973c7a5899eSAntony Antony 		ret = copy_to_user_aead(x->aead, skb);
9741d1e34ddSDavid S. Miller 		if (ret)
9751d1e34ddSDavid S. Miller 			goto out;
9761d1e34ddSDavid S. Miller 	}
9771d1e34ddSDavid S. Miller 	if (x->aalg) {
9781d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
9791d1e34ddSDavid S. Miller 		if (ret)
9801d1e34ddSDavid S. Miller 			goto out;
9811d1e34ddSDavid S. Miller 	}
9821d1e34ddSDavid S. Miller 	if (x->ealg) {
983c7a5899eSAntony Antony 		ret = copy_to_user_ealg(x->ealg, skb);
9841d1e34ddSDavid S. Miller 		if (ret)
9851d1e34ddSDavid S. Miller 			goto out;
9861d1e34ddSDavid S. Miller 	}
9871d1e34ddSDavid S. Miller 	if (x->calg) {
9881d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
9891d1e34ddSDavid S. Miller 		if (ret)
9901d1e34ddSDavid S. Miller 			goto out;
9911d1e34ddSDavid S. Miller 	}
9921d1e34ddSDavid S. Miller 	if (x->encap) {
9931d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
9941d1e34ddSDavid S. Miller 		if (ret)
9951d1e34ddSDavid S. Miller 			goto out;
9961d1e34ddSDavid S. Miller 	}
9971d1e34ddSDavid S. Miller 	if (x->tfcpad) {
9981d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
9991d1e34ddSDavid S. Miller 		if (ret)
10001d1e34ddSDavid S. Miller 			goto out;
10011d1e34ddSDavid S. Miller 	}
10021d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
10031d1e34ddSDavid S. Miller 	if (ret)
10041d1e34ddSDavid S. Miller 		goto out;
10059b42c1f1SSteffen Klassert 
10069b42c1f1SSteffen Klassert 	ret = xfrm_smark_put(skb, &x->props.smark);
10079b42c1f1SSteffen Klassert 	if (ret)
10089b42c1f1SSteffen Klassert 		goto out;
10099b42c1f1SSteffen Klassert 
1010f293a5e3Sdingzhi 	if (x->replay_esn)
10111d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1012d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
10131d1e34ddSDavid S. Miller 			      x->replay_esn);
1014f293a5e3Sdingzhi 	else
1015f293a5e3Sdingzhi 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1016f293a5e3Sdingzhi 			      &x->replay);
10171d1e34ddSDavid S. Miller 	if (ret)
10181d1e34ddSDavid S. Miller 		goto out;
1019d77e38e6SSteffen Klassert 	if(x->xso.dev)
1020d77e38e6SSteffen Klassert 		ret = copy_user_offload(&x->xso, skb);
1021d77e38e6SSteffen Klassert 	if (ret)
1022d77e38e6SSteffen Klassert 		goto out;
10237e652640SSteffen Klassert 	if (x->if_id) {
10247e652640SSteffen Klassert 		ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1025077fbac4SLorenzo Colitti 		if (ret)
1026077fbac4SLorenzo Colitti 			goto out;
1027077fbac4SLorenzo Colitti 	}
10288598112dSSteffen Klassert 	if (x->security)
10298598112dSSteffen Klassert 		ret = copy_sec_ctx(x->security, skb);
10301d1e34ddSDavid S. Miller out:
10311d1e34ddSDavid S. Miller 	return ret;
103268325d3bSHerbert Xu }
103368325d3bSHerbert Xu 
103468325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
103568325d3bSHerbert Xu {
103668325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
103768325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
103868325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
10395f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
104068325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
104168325d3bSHerbert Xu 	struct nlmsghdr *nlh;
104268325d3bSHerbert Xu 	int err;
104368325d3bSHerbert Xu 
104415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
104568325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
104668325d3bSHerbert Xu 	if (nlh == NULL)
104768325d3bSHerbert Xu 		return -EMSGSIZE;
104868325d3bSHerbert Xu 
104968325d3bSHerbert Xu 	p = nlmsg_data(nlh);
105068325d3bSHerbert Xu 
105168325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
10521d1e34ddSDavid S. Miller 	if (err) {
10539825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
105468325d3bSHerbert Xu 		return err;
10551da177e4SLinus Torvalds 	}
10561d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
10575f3eea6bSDmitry Safonov 
10585f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
10595f3eea6bSDmitry Safonov 	if (xtr) {
10605f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlh);
10615f3eea6bSDmitry Safonov 
10625f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
10635f3eea6bSDmitry Safonov 		if (err) {
10645f3eea6bSDmitry Safonov 			nlmsg_cancel(skb, nlh);
10655f3eea6bSDmitry Safonov 			return err;
10665f3eea6bSDmitry Safonov 		}
10675f3eea6bSDmitry Safonov 	}
10685f3eea6bSDmitry Safonov 
10691d1e34ddSDavid S. Miller 	return 0;
10701d1e34ddSDavid S. Miller }
10711da177e4SLinus Torvalds 
10724c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
10734c563f76STimo Teras {
10744c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1075283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
1076283bc9f3SFan Du 	struct net *net = sock_net(sk);
1077283bc9f3SFan Du 
10781ba5bf99SVegard Nossum 	if (cb->args[0])
1079283bc9f3SFan Du 		xfrm_state_walk_done(walk, net);
10804c563f76STimo Teras 	return 0;
10814c563f76STimo Teras }
10824c563f76STimo Teras 
10831da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
10841da177e4SLinus Torvalds {
1085fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
10864c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
10871da177e4SLinus Torvalds 	struct xfrm_dump_info info;
10881da177e4SLinus Torvalds 
10894c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
10904c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
10914c563f76STimo Teras 
10921da177e4SLinus Torvalds 	info.in_skb = cb->skb;
10931da177e4SLinus Torvalds 	info.out_skb = skb;
10941da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
10951da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
10964c563f76STimo Teras 
10974c563f76STimo Teras 	if (!cb->args[0]) {
1098d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
1099870a2df4SNicolas Dichtel 		struct xfrm_address_filter *filter = NULL;
1100d3623099SNicolas Dichtel 		u8 proto = 0;
1101d3623099SNicolas Dichtel 		int err;
1102d3623099SNicolas Dichtel 
11038cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
11048cb08174SJohannes Berg 					     xfrma_policy, cb->extack);
1105d3623099SNicolas Dichtel 		if (err < 0)
1106d3623099SNicolas Dichtel 			return err;
1107d3623099SNicolas Dichtel 
1108870a2df4SNicolas Dichtel 		if (attrs[XFRMA_ADDRESS_FILTER]) {
1109df367561SAndrzej Hajda 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1110df367561SAndrzej Hajda 					 sizeof(*filter), GFP_KERNEL);
1111d3623099SNicolas Dichtel 			if (filter == NULL)
1112d3623099SNicolas Dichtel 				return -ENOMEM;
1113d3623099SNicolas Dichtel 		}
1114d3623099SNicolas Dichtel 
1115d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
1116d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
1117d3623099SNicolas Dichtel 
1118d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
11191ba5bf99SVegard Nossum 		cb->args[0] = 1;
11204c563f76STimo Teras 	}
11214c563f76STimo Teras 
1122fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 	return skb->len;
11251da177e4SLinus Torvalds }
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
11281da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
11291da177e4SLinus Torvalds {
11301da177e4SLinus Torvalds 	struct xfrm_dump_info info;
11311da177e4SLinus Torvalds 	struct sk_buff *skb;
1132864745d2SMathias Krause 	int err;
11331da177e4SLinus Torvalds 
11347deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
11351da177e4SLinus Torvalds 	if (!skb)
11361da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	info.in_skb = in_skb;
11391da177e4SLinus Torvalds 	info.out_skb = skb;
11401da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
11411da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
11421da177e4SLinus Torvalds 
1143864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
1144864745d2SMathias Krause 	if (err) {
11451da177e4SLinus Torvalds 		kfree_skb(skb);
1146864745d2SMathias Krause 		return ERR_PTR(err);
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	return skb;
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds 
115221ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
115321ee543eSMichal Kubecek  * Must be called with RCU read lock.
115421ee543eSMichal Kubecek  */
115521ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
115621ee543eSMichal Kubecek 				       u32 pid, unsigned int group)
115721ee543eSMichal Kubecek {
115821ee543eSMichal Kubecek 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
11595461fc0cSDmitry Safonov 	struct xfrm_translator *xtr;
116021ee543eSMichal Kubecek 
116186126b77SFlorian Westphal 	if (!nlsk) {
116286126b77SFlorian Westphal 		kfree_skb(skb);
116386126b77SFlorian Westphal 		return -EPIPE;
116486126b77SFlorian Westphal 	}
116586126b77SFlorian Westphal 
11665461fc0cSDmitry Safonov 	xtr = xfrm_get_translator();
11675461fc0cSDmitry Safonov 	if (xtr) {
11685461fc0cSDmitry Safonov 		int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
11695461fc0cSDmitry Safonov 
11705461fc0cSDmitry Safonov 		xfrm_put_translator(xtr);
11715461fc0cSDmitry Safonov 		if (err) {
11725461fc0cSDmitry Safonov 			kfree_skb(skb);
11735461fc0cSDmitry Safonov 			return err;
11745461fc0cSDmitry Safonov 		}
11755461fc0cSDmitry Safonov 	}
11765461fc0cSDmitry Safonov 
117721ee543eSMichal Kubecek 	return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
117821ee543eSMichal Kubecek }
117921ee543eSMichal Kubecek 
1180a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void)
11817deb2264SThomas Graf {
11827deb2264SThomas Graf 	return NLMSG_ALIGN(4)
11837deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1184880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1185880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1186880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
11877deb2264SThomas Graf }
11887deb2264SThomas Graf 
1189e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
119015e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
1191ecfd6b18SJamal Hadi Salim {
11925a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
11935a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
11945a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1195880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1196ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
11971d1e34ddSDavid S. Miller 	int err;
1198ecfd6b18SJamal Hadi Salim 	u32 *f;
1199880a6fabSChristophe Gouault 	unsigned lseq;
1200ecfd6b18SJamal Hadi Salim 
120115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
120225985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1203ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1204ecfd6b18SJamal Hadi Salim 
1205ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1206ecfd6b18SJamal Hadi Salim 	*f = flags;
1207e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
12085a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
12095a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
12105a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
12115a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
12125a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
12135a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
12145a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
12155a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1216ecfd6b18SJamal Hadi Salim 
1217880a6fabSChristophe Gouault 	do {
1218880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1219880a6fabSChristophe Gouault 
1220880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1221880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1222880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1223880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1224880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1225880a6fabSChristophe Gouault 
12261d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
12271d1e34ddSDavid S. Miller 	if (!err)
12281d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1229880a6fabSChristophe Gouault 	if (!err)
1230880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1231880a6fabSChristophe Gouault 	if (!err)
1232880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
12331d1e34ddSDavid S. Miller 	if (err) {
12341d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
12351d1e34ddSDavid S. Miller 		return err;
12361d1e34ddSDavid S. Miller 	}
1237ecfd6b18SJamal Hadi Salim 
1238053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1239053c095aSJohannes Berg 	return 0;
1240ecfd6b18SJamal Hadi Salim }
1241ecfd6b18SJamal Hadi Salim 
1242880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1243880a6fabSChristophe Gouault 			    struct nlattr **attrs)
1244880a6fabSChristophe Gouault {
1245880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1246880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1247880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1248880a6fabSChristophe Gouault 
1249880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1250880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1251880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1252880a6fabSChristophe Gouault 
1253880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1254880a6fabSChristophe Gouault 			return -EINVAL;
1255880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1256880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1257880a6fabSChristophe Gouault 			return -EINVAL;
1258880a6fabSChristophe Gouault 	}
1259880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1260880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1261880a6fabSChristophe Gouault 
1262880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1263880a6fabSChristophe Gouault 			return -EINVAL;
1264880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1265880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1266880a6fabSChristophe Gouault 			return -EINVAL;
1267880a6fabSChristophe Gouault 	}
1268880a6fabSChristophe Gouault 
1269880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1270880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1271880a6fabSChristophe Gouault 		if (thresh4) {
1272880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1273880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1274880a6fabSChristophe Gouault 		}
1275880a6fabSChristophe Gouault 		if (thresh6) {
1276880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1277880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1278880a6fabSChristophe Gouault 		}
1279880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1280880a6fabSChristophe Gouault 
1281880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1282880a6fabSChristophe Gouault 	}
1283880a6fabSChristophe Gouault 
1284880a6fabSChristophe Gouault 	return 0;
1285880a6fabSChristophe Gouault }
1286880a6fabSChristophe Gouault 
1287ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12885424f32eSThomas Graf 		struct nlattr **attrs)
1289ecfd6b18SJamal Hadi Salim {
1290a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1291ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
12927b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
129315e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1294ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
12952fc5f83bSGustavo A. R. Silva 	int err;
1296ecfd6b18SJamal Hadi Salim 
12977deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1298ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1299ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1300ecfd6b18SJamal Hadi Salim 
13012fc5f83bSGustavo A. R. Silva 	err = build_spdinfo(r_skb, net, sportid, seq, *flags);
13022fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
1303ecfd6b18SJamal Hadi Salim 
130415e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1305ecfd6b18SJamal Hadi Salim }
1306ecfd6b18SJamal Hadi Salim 
1307a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void)
13087deb2264SThomas Graf {
13097deb2264SThomas Graf 	return NLMSG_ALIGN(4)
13107deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
13117deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
13127deb2264SThomas Graf }
13137deb2264SThomas Graf 
1314e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
131515e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
131628d8909bSJamal Hadi Salim {
1317af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1318af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
131928d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
13201d1e34ddSDavid S. Miller 	int err;
132128d8909bSJamal Hadi Salim 	u32 *f;
132228d8909bSJamal Hadi Salim 
132315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
132425985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
132528d8909bSJamal Hadi Salim 		return -EMSGSIZE;
132628d8909bSJamal Hadi Salim 
132728d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
132828d8909bSJamal Hadi Salim 	*f = flags;
1329e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
133028d8909bSJamal Hadi Salim 
1331af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1332af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1333af11e316SJamal Hadi Salim 
13341d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
13351d1e34ddSDavid S. Miller 	if (!err)
13361d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
13371d1e34ddSDavid S. Miller 	if (err) {
13381d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
13391d1e34ddSDavid S. Miller 		return err;
13401d1e34ddSDavid S. Miller 	}
134128d8909bSJamal Hadi Salim 
1342053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1343053c095aSJohannes Berg 	return 0;
134428d8909bSJamal Hadi Salim }
134528d8909bSJamal Hadi Salim 
134628d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
13475424f32eSThomas Graf 		struct nlattr **attrs)
134828d8909bSJamal Hadi Salim {
1349a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
135028d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
13517b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
135215e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
135328d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
13542fc5f83bSGustavo A. R. Silva 	int err;
135528d8909bSJamal Hadi Salim 
13567deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
135728d8909bSJamal Hadi Salim 	if (r_skb == NULL)
135828d8909bSJamal Hadi Salim 		return -ENOMEM;
135928d8909bSJamal Hadi Salim 
13602fc5f83bSGustavo A. R. Silva 	err = build_sadinfo(r_skb, net, sportid, seq, *flags);
13612fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
136228d8909bSJamal Hadi Salim 
136315e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
136428d8909bSJamal Hadi Salim }
136528d8909bSJamal Hadi Salim 
136622e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
13675424f32eSThomas Graf 		struct nlattr **attrs)
13681da177e4SLinus Torvalds {
1369fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
13707b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
13711da177e4SLinus Torvalds 	struct xfrm_state *x;
13721da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1373eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
13741da177e4SLinus Torvalds 
1375fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
13761da177e4SLinus Torvalds 	if (x == NULL)
13771da177e4SLinus Torvalds 		goto out_noput;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
13801da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
13811da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
13821da177e4SLinus Torvalds 	} else {
138315e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
13841da177e4SLinus Torvalds 	}
13851da177e4SLinus Torvalds 	xfrm_state_put(x);
13861da177e4SLinus Torvalds out_noput:
13871da177e4SLinus Torvalds 	return err;
13881da177e4SLinus Torvalds }
13891da177e4SLinus Torvalds 
139022e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
13915424f32eSThomas Graf 		struct nlattr **attrs)
13921da177e4SLinus Torvalds {
1393fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
13941da177e4SLinus Torvalds 	struct xfrm_state *x;
13951da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
13965f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
13971da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
13981da177e4SLinus Torvalds 	xfrm_address_t *daddr;
13991da177e4SLinus Torvalds 	int family;
14001da177e4SLinus Torvalds 	int err;
14016f26b61eSJamal Hadi Salim 	u32 mark;
14026f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
14037e652640SSteffen Klassert 	u32 if_id = 0;
14041da177e4SLinus Torvalds 
14057b67c857SThomas Graf 	p = nlmsg_data(nlh);
1406776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
14071da177e4SLinus Torvalds 	if (err)
14081da177e4SLinus Torvalds 		goto out_noput;
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 	family = p->info.family;
14111da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 	x = NULL;
14146f26b61eSJamal Hadi Salim 
14156f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
14167e652640SSteffen Klassert 
14177e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
14187e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
14197e652640SSteffen Klassert 
14201da177e4SLinus Torvalds 	if (p->info.seq) {
14216f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
142270e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
14231da177e4SLinus Torvalds 			xfrm_state_put(x);
14241da177e4SLinus Torvalds 			x = NULL;
14251da177e4SLinus Torvalds 		}
14261da177e4SLinus Torvalds 	}
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	if (!x)
14296f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
14307e652640SSteffen Klassert 				  if_id, p->info.id.proto, daddr,
14311da177e4SLinus Torvalds 				  &p->info.saddr, 1,
14321da177e4SLinus Torvalds 				  family);
14331da177e4SLinus Torvalds 	err = -ENOENT;
14341da177e4SLinus Torvalds 	if (x == NULL)
14351da177e4SLinus Torvalds 		goto out_noput;
14361da177e4SLinus Torvalds 
1437658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1438658b219eSHerbert Xu 	if (err)
1439658b219eSHerbert Xu 		goto out;
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
14421da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
14431da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
14441da177e4SLinus Torvalds 		goto out;
14451da177e4SLinus Torvalds 	}
14461da177e4SLinus Torvalds 
14475f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
14485f3eea6bSDmitry Safonov 	if (xtr) {
14495f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
14505f3eea6bSDmitry Safonov 
14515f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
14525f3eea6bSDmitry Safonov 		if (err) {
14535f3eea6bSDmitry Safonov 			kfree_skb(resp_skb);
14545f3eea6bSDmitry Safonov 			goto out;
14555f3eea6bSDmitry Safonov 		}
14565f3eea6bSDmitry Safonov 	}
14575f3eea6bSDmitry Safonov 
145815e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds out:
14611da177e4SLinus Torvalds 	xfrm_state_put(x);
14621da177e4SLinus Torvalds out_noput:
14631da177e4SLinus Torvalds 	return err;
14641da177e4SLinus Torvalds }
14651da177e4SLinus Torvalds 
1466b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
14671da177e4SLinus Torvalds {
14681da177e4SLinus Torvalds 	switch (dir) {
14691da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
14701da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
14711da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
14721da177e4SLinus Torvalds 		break;
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	default:
14751da177e4SLinus Torvalds 		return -EINVAL;
14763ff50b79SStephen Hemminger 	}
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	return 0;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
1481b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1482f7b6983fSMasahide NAKAMURA {
1483f7b6983fSMasahide NAKAMURA 	switch (type) {
1484f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1485f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1486f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1487f7b6983fSMasahide NAKAMURA #endif
1488f7b6983fSMasahide NAKAMURA 		break;
1489f7b6983fSMasahide NAKAMURA 
1490f7b6983fSMasahide NAKAMURA 	default:
1491f7b6983fSMasahide NAKAMURA 		return -EINVAL;
14923ff50b79SStephen Hemminger 	}
1493f7b6983fSMasahide NAKAMURA 
1494f7b6983fSMasahide NAKAMURA 	return 0;
1495f7b6983fSMasahide NAKAMURA }
1496f7b6983fSMasahide NAKAMURA 
14971da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
14981da177e4SLinus Torvalds {
1499e682adf0SFan Du 	int ret;
1500e682adf0SFan Du 
15011da177e4SLinus Torvalds 	switch (p->share) {
15021da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
15031da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
15041da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
15051da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
15061da177e4SLinus Torvalds 		break;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	default:
15091da177e4SLinus Torvalds 		return -EINVAL;
15103ff50b79SStephen Hemminger 	}
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	switch (p->action) {
15131da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
15141da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
15151da177e4SLinus Torvalds 		break;
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	default:
15181da177e4SLinus Torvalds 		return -EINVAL;
15193ff50b79SStephen Hemminger 	}
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	switch (p->sel.family) {
15221da177e4SLinus Torvalds 	case AF_INET:
152307bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
152407bf7908SSteffen Klassert 			return -EINVAL;
152507bf7908SSteffen Klassert 
15261da177e4SLinus Torvalds 		break;
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 	case AF_INET6:
1529dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
153007bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
153107bf7908SSteffen Klassert 			return -EINVAL;
153207bf7908SSteffen Klassert 
15331da177e4SLinus Torvalds 		break;
15341da177e4SLinus Torvalds #else
15351da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
15361da177e4SLinus Torvalds #endif
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	default:
15391da177e4SLinus Torvalds 		return -EINVAL;
15403ff50b79SStephen Hemminger 	}
15411da177e4SLinus Torvalds 
1542e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1543e682adf0SFan Du 	if (ret)
1544e682adf0SFan Du 		return ret;
1545b805d78dSYueHaibing 	if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1546e682adf0SFan Du 		return -EINVAL;
1547e682adf0SFan Du 
1548e682adf0SFan Du 	return 0;
15491da177e4SLinus Torvalds }
15501da177e4SLinus Torvalds 
15515424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1552df71837dSTrent Jaeger {
15535424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1554df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1555df71837dSTrent Jaeger 
1556df71837dSTrent Jaeger 	if (!rt)
1557df71837dSTrent Jaeger 		return 0;
1558df71837dSTrent Jaeger 
15595424f32eSThomas Graf 	uctx = nla_data(rt);
156052a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1561df71837dSTrent Jaeger }
1562df71837dSTrent Jaeger 
15631da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
15641da177e4SLinus Torvalds 			   int nr)
15651da177e4SLinus Torvalds {
15661da177e4SLinus Torvalds 	int i;
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
15691da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
15701da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
15731da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
15741da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
15751da177e4SLinus Torvalds 		t->reqid = ut->reqid;
15761da177e4SLinus Torvalds 		t->mode = ut->mode;
15771da177e4SLinus Torvalds 		t->share = ut->share;
15781da177e4SLinus Torvalds 		t->optional = ut->optional;
15791da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
15801da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
15811da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1582c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1583c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
15848511d01dSMiika Komu 		t->encap_family = ut->family;
15851da177e4SLinus Torvalds 	}
15861da177e4SLinus Torvalds }
15871da177e4SLinus Torvalds 
1588b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1589b4ad86bfSDavid S. Miller {
1590732706afSSteffen Klassert 	u16 prev_family;
1591b4ad86bfSDavid S. Miller 	int i;
1592b4ad86bfSDavid S. Miller 
1593b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1594b4ad86bfSDavid S. Miller 		return -EINVAL;
1595b4ad86bfSDavid S. Miller 
1596732706afSSteffen Klassert 	prev_family = family;
1597732706afSSteffen Klassert 
1598b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1599b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1600b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1601b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1602b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1603b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1604b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1605b4ad86bfSDavid S. Miller 		 */
1606b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1607b4ad86bfSDavid S. Miller 			ut[i].family = family;
1608b4ad86bfSDavid S. Miller 
160935e61038SFlorian Westphal 		switch (ut[i].mode) {
161035e61038SFlorian Westphal 		case XFRM_MODE_TUNNEL:
161135e61038SFlorian Westphal 		case XFRM_MODE_BEET:
161235e61038SFlorian Westphal 			break;
161335e61038SFlorian Westphal 		default:
161435e61038SFlorian Westphal 			if (ut[i].family != prev_family)
1615732706afSSteffen Klassert 				return -EINVAL;
161635e61038SFlorian Westphal 			break;
161735e61038SFlorian Westphal 		}
161832bf94fbSSean Tranchetti 		if (ut[i].mode >= XFRM_MODE_MAX)
161932bf94fbSSean Tranchetti 			return -EINVAL;
162032bf94fbSSean Tranchetti 
1621732706afSSteffen Klassert 		prev_family = ut[i].family;
1622732706afSSteffen Klassert 
1623b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1624b4ad86bfSDavid S. Miller 		case AF_INET:
1625b4ad86bfSDavid S. Miller 			break;
1626dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1627b4ad86bfSDavid S. Miller 		case AF_INET6:
1628b4ad86bfSDavid S. Miller 			break;
1629b4ad86bfSDavid S. Miller #endif
1630b4ad86bfSDavid S. Miller 		default:
1631b4ad86bfSDavid S. Miller 			return -EINVAL;
16323ff50b79SStephen Hemminger 		}
16336a53b759SCong Wang 
1634dbb2483bSCong Wang 		if (!xfrm_id_proto_valid(ut[i].id.proto))
16356a53b759SCong Wang 			return -EINVAL;
16366a53b759SCong Wang 	}
16376a53b759SCong Wang 
1638b4ad86bfSDavid S. Miller 	return 0;
1639b4ad86bfSDavid S. Miller }
1640b4ad86bfSDavid S. Miller 
16415424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
16421da177e4SLinus Torvalds {
16435424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	if (!rt) {
16461da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
16471da177e4SLinus Torvalds 	} else {
16485424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
16495424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1650b4ad86bfSDavid S. Miller 		int err;
16511da177e4SLinus Torvalds 
1652b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1653b4ad86bfSDavid S. Miller 		if (err)
1654b4ad86bfSDavid S. Miller 			return err;
16551da177e4SLinus Torvalds 
16565424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
16571da177e4SLinus Torvalds 	}
16581da177e4SLinus Torvalds 	return 0;
16591da177e4SLinus Torvalds }
16601da177e4SLinus Torvalds 
16615424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1662f7b6983fSMasahide NAKAMURA {
16635424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1664f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1665b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1666f7b6983fSMasahide NAKAMURA 	int err;
1667f7b6983fSMasahide NAKAMURA 
1668f7b6983fSMasahide NAKAMURA 	if (rt) {
16695424f32eSThomas Graf 		upt = nla_data(rt);
1670f7b6983fSMasahide NAKAMURA 		type = upt->type;
1671f7b6983fSMasahide NAKAMURA 	}
1672f7b6983fSMasahide NAKAMURA 
1673f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1674f7b6983fSMasahide NAKAMURA 	if (err)
1675f7b6983fSMasahide NAKAMURA 		return err;
1676f7b6983fSMasahide NAKAMURA 
1677f7b6983fSMasahide NAKAMURA 	*tp = type;
1678f7b6983fSMasahide NAKAMURA 	return 0;
1679f7b6983fSMasahide NAKAMURA }
1680f7b6983fSMasahide NAKAMURA 
16811da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
16821da177e4SLinus Torvalds {
16831da177e4SLinus Torvalds 	xp->priority = p->priority;
16841da177e4SLinus Torvalds 	xp->index = p->index;
16851da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
16861da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
16871da177e4SLinus Torvalds 	xp->action = p->action;
16881da177e4SLinus Torvalds 	xp->flags = p->flags;
16891da177e4SLinus Torvalds 	xp->family = p->sel.family;
16901da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
16911da177e4SLinus Torvalds }
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
16941da177e4SLinus Torvalds {
16957b789836SMathias Krause 	memset(p, 0, sizeof(*p));
16961da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
16971da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
16981da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
16991da177e4SLinus Torvalds 	p->priority = xp->priority;
17001da177e4SLinus Torvalds 	p->index = xp->index;
17011da177e4SLinus Torvalds 	p->sel.family = xp->family;
17021da177e4SLinus Torvalds 	p->dir = dir;
17031da177e4SLinus Torvalds 	p->action = xp->action;
17041da177e4SLinus Torvalds 	p->flags = xp->flags;
17051da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
17061da177e4SLinus Torvalds }
17071da177e4SLinus Torvalds 
1708fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
17091da177e4SLinus Torvalds {
1710fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
17111da177e4SLinus Torvalds 	int err;
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 	if (!xp) {
17141da177e4SLinus Torvalds 		*errp = -ENOMEM;
17151da177e4SLinus Torvalds 		return NULL;
17161da177e4SLinus Torvalds 	}
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1719df71837dSTrent Jaeger 
172035a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1721f7b6983fSMasahide NAKAMURA 	if (err)
1722f7b6983fSMasahide NAKAMURA 		goto error;
1723f7b6983fSMasahide NAKAMURA 
172435a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
172535a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1726f7b6983fSMasahide NAKAMURA 	if (err)
1727f7b6983fSMasahide NAKAMURA 		goto error;
17281da177e4SLinus Torvalds 
1729295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1730295fae56SJamal Hadi Salim 
17317e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
17327e652640SSteffen Klassert 		xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
17337e652640SSteffen Klassert 
17341da177e4SLinus Torvalds 	return xp;
1735f7b6983fSMasahide NAKAMURA  error:
1736f7b6983fSMasahide NAKAMURA 	*errp = err;
173712a169e7SHerbert Xu 	xp->walk.dead = 1;
173864c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1739f7b6983fSMasahide NAKAMURA 	return NULL;
17401da177e4SLinus Torvalds }
17411da177e4SLinus Torvalds 
174222e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
17435424f32eSThomas Graf 		struct nlattr **attrs)
17441da177e4SLinus Torvalds {
1745fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
17467b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
17471da177e4SLinus Torvalds 	struct xfrm_policy *xp;
174826b15dadSJamal Hadi Salim 	struct km_event c;
17491da177e4SLinus Torvalds 	int err;
17501da177e4SLinus Torvalds 	int excl;
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
17531da177e4SLinus Torvalds 	if (err)
17541da177e4SLinus Torvalds 		return err;
175535a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1756df71837dSTrent Jaeger 	if (err)
1757df71837dSTrent Jaeger 		return err;
17581da177e4SLinus Torvalds 
1759fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
17601da177e4SLinus Torvalds 	if (!xp)
17611da177e4SLinus Torvalds 		return err;
17621da177e4SLinus Torvalds 
176325985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
176426b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
1765a7fd0e6dSBhaskar Chowdhury 	 * in netlink excl is a flag and you wouldn't need
176626b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
17671da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
17681da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
17692e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1770161a09e7SJoy Latten 
17711da177e4SLinus Torvalds 	if (err) {
177203e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
17731da177e4SLinus Torvalds 		kfree(xp);
17741da177e4SLinus Torvalds 		return err;
17751da177e4SLinus Torvalds 	}
17761da177e4SLinus Torvalds 
1777f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
177826b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
177915e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
178026b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
178126b15dadSJamal Hadi Salim 
17821da177e4SLinus Torvalds 	xfrm_pol_put(xp);
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	return 0;
17851da177e4SLinus Torvalds }
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
17881da177e4SLinus Torvalds {
17891da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
17901da177e4SLinus Torvalds 	int i;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
17931da177e4SLinus Torvalds 		return 0;
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
17961da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
17971da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
17981da177e4SLinus Torvalds 
17991f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
18001da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
18018511d01dSMiika Komu 		up->family = kp->encap_family;
18021da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
18031da177e4SLinus Torvalds 		up->reqid = kp->reqid;
18041da177e4SLinus Torvalds 		up->mode = kp->mode;
18051da177e4SLinus Torvalds 		up->share = kp->share;
18061da177e4SLinus Torvalds 		up->optional = kp->optional;
18071da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
18081da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
18091da177e4SLinus Torvalds 		up->calgos = kp->calgos;
18101da177e4SLinus Torvalds 	}
18111da177e4SLinus Torvalds 
1812c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1813c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1814df71837dSTrent Jaeger }
1815df71837dSTrent Jaeger 
18160d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
18170d681623SSerge Hallyn {
18180d681623SSerge Hallyn 	if (x->security) {
18190d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
18200d681623SSerge Hallyn 	}
18210d681623SSerge Hallyn 	return 0;
18220d681623SSerge Hallyn }
18230d681623SSerge Hallyn 
18240d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
18250d681623SSerge Hallyn {
18261d1e34ddSDavid S. Miller 	if (xp->security)
18270d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
18280d681623SSerge Hallyn 	return 0;
18290d681623SSerge Hallyn }
1830a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void)
1831cfbfd45aSThomas Graf {
1832cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1833cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1834cfbfd45aSThomas Graf #else
1835cfbfd45aSThomas Graf 	return 0;
1836cfbfd45aSThomas Graf #endif
1837cfbfd45aSThomas Graf }
18380d681623SSerge Hallyn 
1839f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1840b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1841f7b6983fSMasahide NAKAMURA {
184245c180bcSEric Dumazet 	struct xfrm_userpolicy_type upt;
184345c180bcSEric Dumazet 
184445c180bcSEric Dumazet 	/* Sadly there are two holes in struct xfrm_userpolicy_type */
184545c180bcSEric Dumazet 	memset(&upt, 0, sizeof(upt));
184645c180bcSEric Dumazet 	upt.type = type;
1847f7b6983fSMasahide NAKAMURA 
1848c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1849f7b6983fSMasahide NAKAMURA }
1850f7b6983fSMasahide NAKAMURA 
1851f7b6983fSMasahide NAKAMURA #else
1852b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1853f7b6983fSMasahide NAKAMURA {
1854f7b6983fSMasahide NAKAMURA 	return 0;
1855f7b6983fSMasahide NAKAMURA }
1856f7b6983fSMasahide NAKAMURA #endif
1857f7b6983fSMasahide NAKAMURA 
18581da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
18591da177e4SLinus Torvalds {
18601da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
18611da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
18621da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
18631da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
18645f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
18651da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
18661d1e34ddSDavid S. Miller 	int err;
18671da177e4SLinus Torvalds 
186815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
186979b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
187079b8b7f4SThomas Graf 	if (nlh == NULL)
187179b8b7f4SThomas Graf 		return -EMSGSIZE;
18721da177e4SLinus Torvalds 
18737b67c857SThomas Graf 	p = nlmsg_data(nlh);
18741da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
18751d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
18761d1e34ddSDavid S. Miller 	if (!err)
18771d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
18781d1e34ddSDavid S. Miller 	if (!err)
18791d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
18801d1e34ddSDavid S. Miller 	if (!err)
18811d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
18827e652640SSteffen Klassert 	if (!err)
18837e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
18841d1e34ddSDavid S. Miller 	if (err) {
18851d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
18861d1e34ddSDavid S. Miller 		return err;
18871d1e34ddSDavid S. Miller 	}
18889825069dSThomas Graf 	nlmsg_end(skb, nlh);
18895f3eea6bSDmitry Safonov 
18905f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
18915f3eea6bSDmitry Safonov 	if (xtr) {
18925f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlh);
18935f3eea6bSDmitry Safonov 
18945f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
18955f3eea6bSDmitry Safonov 		if (err) {
18965f3eea6bSDmitry Safonov 			nlmsg_cancel(skb, nlh);
18975f3eea6bSDmitry Safonov 			return err;
18985f3eea6bSDmitry Safonov 		}
18995f3eea6bSDmitry Safonov 	}
19005f3eea6bSDmitry Safonov 
19011da177e4SLinus Torvalds 	return 0;
19021da177e4SLinus Torvalds }
19031da177e4SLinus Torvalds 
19044c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
19054c563f76STimo Teras {
19061137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1907283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
19084c563f76STimo Teras 
1909283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
19104c563f76STimo Teras 	return 0;
19114c563f76STimo Teras }
19124c563f76STimo Teras 
19131137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb)
19141137b5e2SHerbert Xu {
19151137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
19161137b5e2SHerbert Xu 
19171137b5e2SHerbert Xu 	BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
19181137b5e2SHerbert Xu 
19191137b5e2SHerbert Xu 	xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
19201137b5e2SHerbert Xu 	return 0;
19211137b5e2SHerbert Xu }
19221137b5e2SHerbert Xu 
19231da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
19241da177e4SLinus Torvalds {
1925fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
19261137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
19271da177e4SLinus Torvalds 	struct xfrm_dump_info info;
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds 	info.in_skb = cb->skb;
19301da177e4SLinus Torvalds 	info.out_skb = skb;
19311da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
19321da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
19334c563f76STimo Teras 
1934fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 	return skb->len;
19371da177e4SLinus Torvalds }
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
19401da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
19411da177e4SLinus Torvalds 					  int dir, u32 seq)
19421da177e4SLinus Torvalds {
19431da177e4SLinus Torvalds 	struct xfrm_dump_info info;
19441da177e4SLinus Torvalds 	struct sk_buff *skb;
1945c2546372SMathias Krause 	int err;
19461da177e4SLinus Torvalds 
19477deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19481da177e4SLinus Torvalds 	if (!skb)
19491da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	info.in_skb = in_skb;
19521da177e4SLinus Torvalds 	info.out_skb = skb;
19531da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
19541da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
19551da177e4SLinus Torvalds 
1956c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1957c2546372SMathias Krause 	if (err) {
19581da177e4SLinus Torvalds 		kfree_skb(skb);
1959c2546372SMathias Krause 		return ERR_PTR(err);
19601da177e4SLinus Torvalds 	}
19611da177e4SLinus Torvalds 
19621da177e4SLinus Torvalds 	return skb;
19631da177e4SLinus Torvalds }
19641da177e4SLinus Torvalds 
196588d0adb5SNicolas Dichtel static int xfrm_notify_userpolicy(struct net *net)
196688d0adb5SNicolas Dichtel {
196788d0adb5SNicolas Dichtel 	struct xfrm_userpolicy_default *up;
196888d0adb5SNicolas Dichtel 	int len = NLMSG_ALIGN(sizeof(*up));
196988d0adb5SNicolas Dichtel 	struct nlmsghdr *nlh;
197088d0adb5SNicolas Dichtel 	struct sk_buff *skb;
197193ec1320SNicolas Dichtel 	int err;
197288d0adb5SNicolas Dichtel 
197388d0adb5SNicolas Dichtel 	skb = nlmsg_new(len, GFP_ATOMIC);
197488d0adb5SNicolas Dichtel 	if (skb == NULL)
197588d0adb5SNicolas Dichtel 		return -ENOMEM;
197688d0adb5SNicolas Dichtel 
197788d0adb5SNicolas Dichtel 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
197888d0adb5SNicolas Dichtel 	if (nlh == NULL) {
197988d0adb5SNicolas Dichtel 		kfree_skb(skb);
198088d0adb5SNicolas Dichtel 		return -EMSGSIZE;
198188d0adb5SNicolas Dichtel 	}
198288d0adb5SNicolas Dichtel 
198388d0adb5SNicolas Dichtel 	up = nlmsg_data(nlh);
198488d0adb5SNicolas Dichtel 	up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
198588d0adb5SNicolas Dichtel 			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
198688d0adb5SNicolas Dichtel 	up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
198788d0adb5SNicolas Dichtel 			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
198888d0adb5SNicolas Dichtel 	up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
198988d0adb5SNicolas Dichtel 			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
199088d0adb5SNicolas Dichtel 
199188d0adb5SNicolas Dichtel 	nlmsg_end(skb, nlh);
199288d0adb5SNicolas Dichtel 
199393ec1320SNicolas Dichtel 	rcu_read_lock();
199493ec1320SNicolas Dichtel 	err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
199593ec1320SNicolas Dichtel 	rcu_read_unlock();
199693ec1320SNicolas Dichtel 
199793ec1320SNicolas Dichtel 	return err;
199888d0adb5SNicolas Dichtel }
199988d0adb5SNicolas Dichtel 
20002d151d39SSteffen Klassert static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
20012d151d39SSteffen Klassert 			    struct nlattr **attrs)
20022d151d39SSteffen Klassert {
20032d151d39SSteffen Klassert 	struct net *net = sock_net(skb->sk);
20042d151d39SSteffen Klassert 	struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
20052d151d39SSteffen Klassert 
2006f8d858e6SNicolas Dichtel 	if (up->in == XFRM_USERPOLICY_BLOCK)
2007f8d858e6SNicolas Dichtel 		net->xfrm.policy_default |= XFRM_POL_DEFAULT_IN;
2008f8d858e6SNicolas Dichtel 	else if (up->in == XFRM_USERPOLICY_ACCEPT)
2009f8d858e6SNicolas Dichtel 		net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_IN;
20105d8dbb7fSPavel Skripkin 
2011f8d858e6SNicolas Dichtel 	if (up->fwd == XFRM_USERPOLICY_BLOCK)
2012f8d858e6SNicolas Dichtel 		net->xfrm.policy_default |= XFRM_POL_DEFAULT_FWD;
2013f8d858e6SNicolas Dichtel 	else if (up->fwd == XFRM_USERPOLICY_ACCEPT)
2014f8d858e6SNicolas Dichtel 		net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_FWD;
20155d8dbb7fSPavel Skripkin 
2016f8d858e6SNicolas Dichtel 	if (up->out == XFRM_USERPOLICY_BLOCK)
2017f8d858e6SNicolas Dichtel 		net->xfrm.policy_default |= XFRM_POL_DEFAULT_OUT;
2018f8d858e6SNicolas Dichtel 	else if (up->out == XFRM_USERPOLICY_ACCEPT)
2019f8d858e6SNicolas Dichtel 		net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_OUT;
20202d151d39SSteffen Klassert 
20212d151d39SSteffen Klassert 	rt_genid_bump_all(net);
20222d151d39SSteffen Klassert 
202388d0adb5SNicolas Dichtel 	xfrm_notify_userpolicy(net);
20242d151d39SSteffen Klassert 	return 0;
20252d151d39SSteffen Klassert }
20262d151d39SSteffen Klassert 
20272d151d39SSteffen Klassert static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
20282d151d39SSteffen Klassert 			    struct nlattr **attrs)
20292d151d39SSteffen Klassert {
20302d151d39SSteffen Klassert 	struct sk_buff *r_skb;
20312d151d39SSteffen Klassert 	struct nlmsghdr *r_nlh;
20322d151d39SSteffen Klassert 	struct net *net = sock_net(skb->sk);
2033f8d858e6SNicolas Dichtel 	struct xfrm_userpolicy_default *r_up;
20342d151d39SSteffen Klassert 	int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
20352d151d39SSteffen Klassert 	u32 portid = NETLINK_CB(skb).portid;
20362d151d39SSteffen Klassert 	u32 seq = nlh->nlmsg_seq;
20372d151d39SSteffen Klassert 
20382d151d39SSteffen Klassert 	r_skb = nlmsg_new(len, GFP_ATOMIC);
20392d151d39SSteffen Klassert 	if (!r_skb)
20402d151d39SSteffen Klassert 		return -ENOMEM;
20412d151d39SSteffen Klassert 
20422d151d39SSteffen Klassert 	r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
20432d151d39SSteffen Klassert 	if (!r_nlh) {
20442d151d39SSteffen Klassert 		kfree_skb(r_skb);
20452d151d39SSteffen Klassert 		return -EMSGSIZE;
20462d151d39SSteffen Klassert 	}
20472d151d39SSteffen Klassert 
20482d151d39SSteffen Klassert 	r_up = nlmsg_data(r_nlh);
20492d151d39SSteffen Klassert 
2050f8d858e6SNicolas Dichtel 	r_up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
2051f8d858e6SNicolas Dichtel 			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2052f8d858e6SNicolas Dichtel 	r_up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
2053f8d858e6SNicolas Dichtel 			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2054f8d858e6SNicolas Dichtel 	r_up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
2055f8d858e6SNicolas Dichtel 			XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
20562d151d39SSteffen Klassert 	nlmsg_end(r_skb, r_nlh);
20572d151d39SSteffen Klassert 
20582d151d39SSteffen Klassert 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
20592d151d39SSteffen Klassert }
20602d151d39SSteffen Klassert 
206122e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
20625424f32eSThomas Graf 		struct nlattr **attrs)
20631da177e4SLinus Torvalds {
2064fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
20651da177e4SLinus Torvalds 	struct xfrm_policy *xp;
20661da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
2067b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
20681da177e4SLinus Torvalds 	int err;
206926b15dadSJamal Hadi Salim 	struct km_event c;
20701da177e4SLinus Torvalds 	int delete;
2071295fae56SJamal Hadi Salim 	struct xfrm_mark m;
20727e652640SSteffen Klassert 	u32 if_id = 0;
20731da177e4SLinus Torvalds 
20747b67c857SThomas Graf 	p = nlmsg_data(nlh);
20751da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
20761da177e4SLinus Torvalds 
207735a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2078f7b6983fSMasahide NAKAMURA 	if (err)
2079f7b6983fSMasahide NAKAMURA 		return err;
2080f7b6983fSMasahide NAKAMURA 
20811da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
20821da177e4SLinus Torvalds 	if (err)
20831da177e4SLinus Torvalds 		return err;
20841da177e4SLinus Torvalds 
20857e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
20867e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
20877e652640SSteffen Klassert 
20884f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
20894f47e8abSXin Long 
20901da177e4SLinus Torvalds 	if (p->index)
20914f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
20924f47e8abSXin Long 				      p->index, delete, &err);
2093df71837dSTrent Jaeger 	else {
20945424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
209503e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
2096df71837dSTrent Jaeger 
209735a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
2098df71837dSTrent Jaeger 		if (err)
2099df71837dSTrent Jaeger 			return err;
2100df71837dSTrent Jaeger 
21012c8dd116SDenis V. Lunev 		ctx = NULL;
2102df71837dSTrent Jaeger 		if (rt) {
21035424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2104df71837dSTrent Jaeger 
210552a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
210603e1ad7bSPaul Moore 			if (err)
2107df71837dSTrent Jaeger 				return err;
21082c8dd116SDenis V. Lunev 		}
21094f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
21104f47e8abSXin Long 					   &p->sel, ctx, delete, &err);
211103e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
2112df71837dSTrent Jaeger 	}
21131da177e4SLinus Torvalds 	if (xp == NULL)
21141da177e4SLinus Torvalds 		return -ENOENT;
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	if (!delete) {
21171da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
21201da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
21211da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
21221da177e4SLinus Torvalds 		} else {
2123a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
212415e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
21251da177e4SLinus Torvalds 		}
212626b15dadSJamal Hadi Salim 	} else {
21272e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
212813fcfbb0SDavid S. Miller 
212913fcfbb0SDavid S. Miller 		if (err != 0)
2130c8c05a8eSCatherine Zhang 			goto out;
213113fcfbb0SDavid S. Miller 
2132e7443892SHerbert Xu 		c.data.byid = p->index;
2133f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
213426b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
213515e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
213626b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
21371da177e4SLinus Torvalds 	}
21381da177e4SLinus Torvalds 
2139c8c05a8eSCatherine Zhang out:
2140ef41aaa0SEric Paris 	xfrm_pol_put(xp);
21411da177e4SLinus Torvalds 	return err;
21421da177e4SLinus Torvalds }
21431da177e4SLinus Torvalds 
214422e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
21455424f32eSThomas Graf 		struct nlattr **attrs)
21461da177e4SLinus Torvalds {
2147fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
214826b15dadSJamal Hadi Salim 	struct km_event c;
21497b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
21504aa2e62cSJoy Latten 	int err;
21511da177e4SLinus Torvalds 
2152f75a2804SCong Wang 	err = xfrm_state_flush(net, p->proto, true, false);
21539e64cc95SJamal Hadi Salim 	if (err) {
21549e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
21559e64cc95SJamal Hadi Salim 			return 0;
2156069c474eSDavid S. Miller 		return err;
21579e64cc95SJamal Hadi Salim 	}
2158bf08867fSHerbert Xu 	c.data.proto = p->proto;
2159f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
216026b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
216115e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
21627067802eSAlexey Dobriyan 	c.net = net;
216326b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
216426b15dadSJamal Hadi Salim 
21651da177e4SLinus Torvalds 	return 0;
21661da177e4SLinus Torvalds }
21671da177e4SLinus Torvalds 
2168a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
21697deb2264SThomas Graf {
2170a1b831f2SAlexey Dobriyan 	unsigned int replay_size = x->replay_esn ?
2171d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
2172d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
2173d8647b79SSteffen Klassert 
21747deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2175d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
2176de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
21776f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
21787deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
21797deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
21807deb2264SThomas Graf }
2181d51d081dSJamal Hadi Salim 
2182214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2183d51d081dSJamal Hadi Salim {
2184d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
2185d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
21861d1e34ddSDavid S. Miller 	int err;
2187d51d081dSJamal Hadi Salim 
218815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
218979b8b7f4SThomas Graf 	if (nlh == NULL)
219079b8b7f4SThomas Graf 		return -EMSGSIZE;
2191d51d081dSJamal Hadi Salim 
21927b67c857SThomas Graf 	id = nlmsg_data(nlh);
2193931e79d7SMathias Krause 	memset(&id->sa_id, 0, sizeof(id->sa_id));
21942b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2195d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
2196d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
2197d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
21982b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
21992b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
2200d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
2201d51d081dSJamal Hadi Salim 
2202d0fde795SDavid S. Miller 	if (x->replay_esn) {
22031d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2204d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
22051d1e34ddSDavid S. Miller 			      x->replay_esn);
2206d0fde795SDavid S. Miller 	} else {
22071d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
22081d1e34ddSDavid S. Miller 			      &x->replay);
2209d0fde795SDavid S. Miller 	}
22101d1e34ddSDavid S. Miller 	if (err)
22111d1e34ddSDavid S. Miller 		goto out_cancel;
2212de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2213de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
22141d1e34ddSDavid S. Miller 	if (err)
22151d1e34ddSDavid S. Miller 		goto out_cancel;
2216d8647b79SSteffen Klassert 
22171d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
22181d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
22191d1e34ddSDavid S. Miller 		if (err)
22201d1e34ddSDavid S. Miller 			goto out_cancel;
22211d1e34ddSDavid S. Miller 	}
22221d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
22231d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
22241d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
22251d1e34ddSDavid S. Miller 		if (err)
22261d1e34ddSDavid S. Miller 			goto out_cancel;
22271d1e34ddSDavid S. Miller 	}
22281d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
22291d1e34ddSDavid S. Miller 	if (err)
22301d1e34ddSDavid S. Miller 		goto out_cancel;
22316f26b61eSJamal Hadi Salim 
22327e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
22337e652640SSteffen Klassert 	if (err)
22347e652640SSteffen Klassert 		goto out_cancel;
22357e652640SSteffen Klassert 
2236053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2237053c095aSJohannes Berg 	return 0;
2238d51d081dSJamal Hadi Salim 
22391d1e34ddSDavid S. Miller out_cancel:
22409825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
22411d1e34ddSDavid S. Miller 	return err;
2242d51d081dSJamal Hadi Salim }
2243d51d081dSJamal Hadi Salim 
224422e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
22455424f32eSThomas Graf 		struct nlattr **attrs)
2246d51d081dSJamal Hadi Salim {
2247fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2248d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2249d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
2250d51d081dSJamal Hadi Salim 	int err;
2251d51d081dSJamal Hadi Salim 	struct km_event c;
22526f26b61eSJamal Hadi Salim 	u32 mark;
22536f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
22547b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
2255d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
2256d51d081dSJamal Hadi Salim 
22576f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
22586f26b61eSJamal Hadi Salim 
22596f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2260d8647b79SSteffen Klassert 	if (x == NULL)
2261d51d081dSJamal Hadi Salim 		return -ESRCH;
2262d8647b79SSteffen Klassert 
2263d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2264d8647b79SSteffen Klassert 	if (r_skb == NULL) {
2265d8647b79SSteffen Klassert 		xfrm_state_put(x);
2266d8647b79SSteffen Klassert 		return -ENOMEM;
2267d51d081dSJamal Hadi Salim 	}
2268d51d081dSJamal Hadi Salim 
2269d51d081dSJamal Hadi Salim 	/*
2270d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
2271d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
2272d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
2273d51d081dSJamal Hadi Salim 	*/
2274d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2275d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
2276d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
227715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2278d51d081dSJamal Hadi Salim 
22792fc5f83bSGustavo A. R. Silva 	err = build_aevent(r_skb, x, &c);
22802fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
22812fc5f83bSGustavo A. R. Silva 
228215e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2283d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2284d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2285d51d081dSJamal Hadi Salim 	return err;
2286d51d081dSJamal Hadi Salim }
2287d51d081dSJamal Hadi Salim 
228822e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
22895424f32eSThomas Graf 		struct nlattr **attrs)
2290d51d081dSJamal Hadi Salim {
2291fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2292d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2293d51d081dSJamal Hadi Salim 	struct km_event c;
2294d51d081dSJamal Hadi Salim 	int err = -EINVAL;
22956f26b61eSJamal Hadi Salim 	u32 mark = 0;
22966f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
22977b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
22985424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2299d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
23005424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
23014e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
23024e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2303d51d081dSJamal Hadi Salim 
23044e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
2305d51d081dSJamal Hadi Salim 		return err;
2306d51d081dSJamal Hadi Salim 
2307d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
2308d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2309d51d081dSJamal Hadi Salim 		return err;
2310d51d081dSJamal Hadi Salim 
23116f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
23126f26b61eSJamal Hadi Salim 
23136f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2314d51d081dSJamal Hadi Salim 	if (x == NULL)
2315d51d081dSJamal Hadi Salim 		return -ESRCH;
2316d51d081dSJamal Hadi Salim 
2317d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
2318d51d081dSJamal Hadi Salim 		goto out;
2319d51d081dSJamal Hadi Salim 
23204479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
2321e2b19125SSteffen Klassert 	if (err)
2322e2b19125SSteffen Klassert 		goto out;
2323e2b19125SSteffen Klassert 
2324d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2325e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
2326d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2327d51d081dSJamal Hadi Salim 
2328d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
2329d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
233015e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2331d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
2332d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
2333d51d081dSJamal Hadi Salim 	err = 0;
2334d51d081dSJamal Hadi Salim out:
2335d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2336d51d081dSJamal Hadi Salim 	return err;
2337d51d081dSJamal Hadi Salim }
2338d51d081dSJamal Hadi Salim 
233922e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
23405424f32eSThomas Graf 		struct nlattr **attrs)
23411da177e4SLinus Torvalds {
2342fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
234326b15dadSJamal Hadi Salim 	struct km_event c;
2344b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
2345f7b6983fSMasahide NAKAMURA 	int err;
234626b15dadSJamal Hadi Salim 
234735a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2348f7b6983fSMasahide NAKAMURA 	if (err)
2349f7b6983fSMasahide NAKAMURA 		return err;
2350f7b6983fSMasahide NAKAMURA 
23512e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
23522f1eb65fSJamal Hadi Salim 	if (err) {
23532f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
23542f1eb65fSJamal Hadi Salim 			return 0;
2355069c474eSDavid S. Miller 		return err;
23562f1eb65fSJamal Hadi Salim 	}
23572f1eb65fSJamal Hadi Salim 
2358f7b6983fSMasahide NAKAMURA 	c.data.type = type;
2359f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
236026b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
236115e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
23627067802eSAlexey Dobriyan 	c.net = net;
236326b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
23641da177e4SLinus Torvalds 	return 0;
23651da177e4SLinus Torvalds }
23661da177e4SLinus Torvalds 
236722e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
23685424f32eSThomas Graf 		struct nlattr **attrs)
23696c5c8ca7SJamal Hadi Salim {
2370fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
23716c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
23727b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
23736c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2374b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
23756c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2376295fae56SJamal Hadi Salim 	struct xfrm_mark m;
23777e652640SSteffen Klassert 	u32 if_id = 0;
23786c5c8ca7SJamal Hadi Salim 
237935a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2380f7b6983fSMasahide NAKAMURA 	if (err)
2381f7b6983fSMasahide NAKAMURA 		return err;
2382f7b6983fSMasahide NAKAMURA 
2383c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
2384c8bf4d04STimo Teräs 	if (err)
2385c8bf4d04STimo Teräs 		return err;
2386c8bf4d04STimo Teräs 
23877e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
23887e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
23897e652640SSteffen Klassert 
23904f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
23914f47e8abSXin Long 
23926c5c8ca7SJamal Hadi Salim 	if (p->index)
23934f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
23944f47e8abSXin Long 				      0, &err);
23956c5c8ca7SJamal Hadi Salim 	else {
23965424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
239703e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
23986c5c8ca7SJamal Hadi Salim 
239935a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
24006c5c8ca7SJamal Hadi Salim 		if (err)
24016c5c8ca7SJamal Hadi Salim 			return err;
24026c5c8ca7SJamal Hadi Salim 
24032c8dd116SDenis V. Lunev 		ctx = NULL;
24046c5c8ca7SJamal Hadi Salim 		if (rt) {
24055424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
24066c5c8ca7SJamal Hadi Salim 
240752a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
240803e1ad7bSPaul Moore 			if (err)
24096c5c8ca7SJamal Hadi Salim 				return err;
24102c8dd116SDenis V. Lunev 		}
24114f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
24126f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
241303e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
24146c5c8ca7SJamal Hadi Salim 	}
24156c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2416ef41aaa0SEric Paris 		return -ENOENT;
241703e1ad7bSPaul Moore 
2418ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
24196c5c8ca7SJamal Hadi Salim 		goto out;
24206c5c8ca7SJamal Hadi Salim 
24216c5c8ca7SJamal Hadi Salim 	err = 0;
24226c5c8ca7SJamal Hadi Salim 	if (up->hard) {
24236c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
24242e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
24256c5c8ca7SJamal Hadi Salim 	}
2426c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
24276c5c8ca7SJamal Hadi Salim 
24286c5c8ca7SJamal Hadi Salim out:
24296c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
24306c5c8ca7SJamal Hadi Salim 	return err;
24316c5c8ca7SJamal Hadi Salim }
24326c5c8ca7SJamal Hadi Salim 
243322e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
24345424f32eSThomas Graf 		struct nlattr **attrs)
243553bc6b4dSJamal Hadi Salim {
2436fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
243753bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
243853bc6b4dSJamal Hadi Salim 	int err;
24397b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
244053bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
24416f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2442928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
244353bc6b4dSJamal Hadi Salim 
24446f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
244553bc6b4dSJamal Hadi Salim 
24463a765aa5SDavid S. Miller 	err = -ENOENT;
244753bc6b4dSJamal Hadi Salim 	if (x == NULL)
244853bc6b4dSJamal Hadi Salim 		return err;
244953bc6b4dSJamal Hadi Salim 
245053bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
24513a765aa5SDavid S. Miller 	err = -EINVAL;
245253bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
245353bc6b4dSJamal Hadi Salim 		goto out;
2454c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
245553bc6b4dSJamal Hadi Salim 
2456161a09e7SJoy Latten 	if (ue->hard) {
245753bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
24582e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2459161a09e7SJoy Latten 	}
24603a765aa5SDavid S. Miller 	err = 0;
246153bc6b4dSJamal Hadi Salim out:
246253bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
246353bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
246453bc6b4dSJamal Hadi Salim 	return err;
246553bc6b4dSJamal Hadi Salim }
246653bc6b4dSJamal Hadi Salim 
246722e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
24685424f32eSThomas Graf 		struct nlattr **attrs)
2469980ebd25SJamal Hadi Salim {
2470fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2471980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2472980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2473980ebd25SJamal Hadi Salim 	int i;
24745424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
24756f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2476980ebd25SJamal Hadi Salim 
24777b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2478fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2479980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2480980ebd25SJamal Hadi Salim 
2481980ebd25SJamal Hadi Salim 	if (!x)
2482d8eb9307SIlpo Järvinen 		goto nomem;
2483980ebd25SJamal Hadi Salim 
24846f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
24856f26b61eSJamal Hadi Salim 
2486980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2487d8eb9307SIlpo Järvinen 	if (err)
248873efc324SVegard Nossum 		goto free_state;
2489a1a7e3a3SXin Long 	err = verify_sec_ctx_len(attrs);
2490a1a7e3a3SXin Long 	if (err)
2491a1a7e3a3SXin Long 		goto free_state;
2492980ebd25SJamal Hadi Salim 
2493980ebd25SJamal Hadi Salim 	/*   build an XP */
2494fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2495d8eb9307SIlpo Järvinen 	if (!xp)
2496d8eb9307SIlpo Järvinen 		goto free_state;
2497980ebd25SJamal Hadi Salim 
2498980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2499980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2500980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
25016f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
25026f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
25035424f32eSThomas Graf 	ut = nla_data(rt);
2504980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2505980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2506980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2507980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2508980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2509980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2510980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2511980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2512980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2513980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2514980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2515980ebd25SJamal Hadi Salim 
2516980ebd25SJamal Hadi Salim 	}
2517980ebd25SJamal Hadi Salim 
25184a135e53SMathias Krause 	xfrm_state_free(x);
2519980ebd25SJamal Hadi Salim 	kfree(xp);
2520980ebd25SJamal Hadi Salim 
2521980ebd25SJamal Hadi Salim 	return 0;
2522d8eb9307SIlpo Järvinen 
2523d8eb9307SIlpo Järvinen free_state:
25244a135e53SMathias Krause 	xfrm_state_free(x);
2525d8eb9307SIlpo Järvinen nomem:
2526d8eb9307SIlpo Järvinen 	return err;
2527980ebd25SJamal Hadi Salim }
2528980ebd25SJamal Hadi Salim 
25295c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
25305c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
253113c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
25325424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
25335c79de6eSShinta Sugimoto {
25345424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
25355c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
25365c79de6eSShinta Sugimoto 	int i, num_migrate;
25375c79de6eSShinta Sugimoto 
253813c1d189SArnaud Ebalard 	if (k != NULL) {
253913c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
254013c1d189SArnaud Ebalard 
254113c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
254213c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
254313c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
254413c1d189SArnaud Ebalard 		k->family = uk->family;
254513c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
254613c1d189SArnaud Ebalard 	}
254713c1d189SArnaud Ebalard 
25485424f32eSThomas Graf 	um = nla_data(rt);
25495424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
25505c79de6eSShinta Sugimoto 
25515c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
25525c79de6eSShinta Sugimoto 		return -EINVAL;
25535c79de6eSShinta Sugimoto 
25545c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
25555c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
25565c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
25575c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
25585c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
25595c79de6eSShinta Sugimoto 
25605c79de6eSShinta Sugimoto 		ma->proto = um->proto;
25615c79de6eSShinta Sugimoto 		ma->mode = um->mode;
25625c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
25635c79de6eSShinta Sugimoto 
25645c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
25655c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
25665c79de6eSShinta Sugimoto 	}
25675c79de6eSShinta Sugimoto 
25685c79de6eSShinta Sugimoto 	*num = i;
25695c79de6eSShinta Sugimoto 	return 0;
25705c79de6eSShinta Sugimoto }
25715c79de6eSShinta Sugimoto 
25725c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
25735424f32eSThomas Graf 			   struct nlattr **attrs)
25745c79de6eSShinta Sugimoto {
25757b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
25765c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
257713c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
25785c79de6eSShinta Sugimoto 	u8 type;
25795c79de6eSShinta Sugimoto 	int err;
25805c79de6eSShinta Sugimoto 	int n = 0;
25818d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
25824ab47d47SAntony Antony 	struct xfrm_encap_tmpl  *encap = NULL;
25835c79de6eSShinta Sugimoto 
258435a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2585cf5cb79fSThomas Graf 		return -EINVAL;
25865c79de6eSShinta Sugimoto 
258713c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
258813c1d189SArnaud Ebalard 
25895424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
25905c79de6eSShinta Sugimoto 	if (err)
25915c79de6eSShinta Sugimoto 		return err;
25925c79de6eSShinta Sugimoto 
259313c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
25945c79de6eSShinta Sugimoto 	if (err)
25955c79de6eSShinta Sugimoto 		return err;
25965c79de6eSShinta Sugimoto 
25975c79de6eSShinta Sugimoto 	if (!n)
25985c79de6eSShinta Sugimoto 		return 0;
25995c79de6eSShinta Sugimoto 
26004ab47d47SAntony Antony 	if (attrs[XFRMA_ENCAP]) {
26014ab47d47SAntony Antony 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
26024ab47d47SAntony Antony 				sizeof(*encap), GFP_KERNEL);
26034ab47d47SAntony Antony 		if (!encap)
26044ac7a6eeSZheng Yongjun 			return -ENOMEM;
26055c79de6eSShinta Sugimoto 	}
26064ab47d47SAntony Antony 
26074ab47d47SAntony Antony 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
26084ab47d47SAntony Antony 
26094ab47d47SAntony Antony 	kfree(encap);
26104ab47d47SAntony Antony 
26114ab47d47SAntony Antony 	return err;
26124ab47d47SAntony Antony }
26135c79de6eSShinta Sugimoto #else
26145c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
26155424f32eSThomas Graf 			   struct nlattr **attrs)
26165c79de6eSShinta Sugimoto {
26175c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
26185c79de6eSShinta Sugimoto }
26195c79de6eSShinta Sugimoto #endif
26205c79de6eSShinta Sugimoto 
26215c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2622183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
26235c79de6eSShinta Sugimoto {
26245c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
26255c79de6eSShinta Sugimoto 
26265c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
26275c79de6eSShinta Sugimoto 	um.proto = m->proto;
26285c79de6eSShinta Sugimoto 	um.mode = m->mode;
26295c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
26305c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
26315c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
26325c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
26335c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
26345c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
26355c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
26365c79de6eSShinta Sugimoto 
2637c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
26385c79de6eSShinta Sugimoto }
26395c79de6eSShinta Sugimoto 
2640183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
264113c1d189SArnaud Ebalard {
264213c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
264313c1d189SArnaud Ebalard 
264413c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
264513c1d189SArnaud Ebalard 	uk.family = k->family;
264613c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
264713c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2648a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
264913c1d189SArnaud Ebalard 
265013c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
265113c1d189SArnaud Ebalard }
265213c1d189SArnaud Ebalard 
2653a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
26548bafd730SAntony Antony 						int with_encp)
26557deb2264SThomas Graf {
26567deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
265713c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
26588bafd730SAntony Antony 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
26597deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
26607deb2264SThomas Graf 	      + userpolicy_type_attrsize();
26617deb2264SThomas Graf }
26627deb2264SThomas Graf 
2663183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2664183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
26658bafd730SAntony Antony 			 const struct xfrm_selector *sel,
26668bafd730SAntony Antony 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
26675c79de6eSShinta Sugimoto {
2668183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
26695c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
26705c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
26711d1e34ddSDavid S. Miller 	int i, err;
26725c79de6eSShinta Sugimoto 
267379b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
267479b8b7f4SThomas Graf 	if (nlh == NULL)
267579b8b7f4SThomas Graf 		return -EMSGSIZE;
26765c79de6eSShinta Sugimoto 
26777b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
26785c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
26795c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
26805c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
26815c79de6eSShinta Sugimoto 	pol_id->dir = dir;
26825c79de6eSShinta Sugimoto 
26831d1e34ddSDavid S. Miller 	if (k != NULL) {
26841d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
26851d1e34ddSDavid S. Miller 		if (err)
26861d1e34ddSDavid S. Miller 			goto out_cancel;
26871d1e34ddSDavid S. Miller 	}
26888bafd730SAntony Antony 	if (encap) {
26898bafd730SAntony Antony 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
26908bafd730SAntony Antony 		if (err)
26918bafd730SAntony Antony 			goto out_cancel;
26928bafd730SAntony Antony 	}
26931d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
26941d1e34ddSDavid S. Miller 	if (err)
26951d1e34ddSDavid S. Miller 		goto out_cancel;
26965c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
26971d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
26981d1e34ddSDavid S. Miller 		if (err)
26991d1e34ddSDavid S. Miller 			goto out_cancel;
27005c79de6eSShinta Sugimoto 	}
27015c79de6eSShinta Sugimoto 
2702053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2703053c095aSJohannes Berg 	return 0;
27041d1e34ddSDavid S. Miller 
27051d1e34ddSDavid S. Miller out_cancel:
27069825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
27071d1e34ddSDavid S. Miller 	return err;
27085c79de6eSShinta Sugimoto }
27095c79de6eSShinta Sugimoto 
2710183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2711183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
27128bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
27138bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
27145c79de6eSShinta Sugimoto {
2715a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
27165c79de6eSShinta Sugimoto 	struct sk_buff *skb;
27172fc5f83bSGustavo A. R. Silva 	int err;
27185c79de6eSShinta Sugimoto 
27198bafd730SAntony Antony 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
27208bafd730SAntony Antony 			GFP_ATOMIC);
27215c79de6eSShinta Sugimoto 	if (skb == NULL)
27225c79de6eSShinta Sugimoto 		return -ENOMEM;
27235c79de6eSShinta Sugimoto 
27245c79de6eSShinta Sugimoto 	/* build migrate */
27252fc5f83bSGustavo A. R. Silva 	err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
27262fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
27275c79de6eSShinta Sugimoto 
272821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
27295c79de6eSShinta Sugimoto }
27305c79de6eSShinta Sugimoto #else
2731183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2732183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
27338bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
27348bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
27355c79de6eSShinta Sugimoto {
27365c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
27375c79de6eSShinta Sugimoto }
27385c79de6eSShinta Sugimoto #endif
2739d51d081dSJamal Hadi Salim 
2740a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2741492b558bSThomas Graf 
27425461fc0cSDmitry Safonov const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
274366f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2744492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2745492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2746492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2747492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2748492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2749492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2750980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
275153bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2752492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
275366f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
27546c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2755492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2756a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2757d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2758d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
275997a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
27605c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2761a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2762880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2763a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
27642d151d39SSteffen Klassert 	[XFRM_MSG_SETDEFAULT  - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
27652d151d39SSteffen Klassert 	[XFRM_MSG_GETDEFAULT  - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
27661da177e4SLinus Torvalds };
27675461fc0cSDmitry Safonov EXPORT_SYMBOL_GPL(xfrm_msg_min);
27681da177e4SLinus Torvalds 
2769492b558bSThomas Graf #undef XMSGSIZE
2770492b558bSThomas Graf 
27715106f4a8SDmitry Safonov const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2772c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2773c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2774c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2775c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
27761a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2777cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2778cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2779cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2780cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2781cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2782cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2783cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2784cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2785cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2786cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2787cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2788cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2789cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2790cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
279113c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
27926f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
279335d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2794d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2795a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2796d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2797870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2798d77e38e6SSteffen Klassert 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
27999b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK]	= { .type = NLA_U32 },
28009b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK_MASK]	= { .type = NLA_U32 },
28017e652640SSteffen Klassert 	[XFRMA_IF_ID]		= { .type = NLA_U32 },
2802cf5cb79fSThomas Graf };
28035106f4a8SDmitry Safonov EXPORT_SYMBOL_GPL(xfrma_policy);
2804cf5cb79fSThomas Graf 
2805880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2806880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2807880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2808880a6fabSChristophe Gouault };
2809880a6fabSChristophe Gouault 
281005600a79SMathias Krause static const struct xfrm_link {
28115424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
28121137b5e2SHerbert Xu 	int (*start)(struct netlink_callback *);
28131da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
28144c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2815880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2816880a6fabSChristophe Gouault 	int nla_max;
2817492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2818492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2819492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2820492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
28214c563f76STimo Teras 						   .dump = xfrm_dump_sa,
28224c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2823492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2824492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2825492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
28261137b5e2SHerbert Xu 						   .start = xfrm_dump_policy_start,
28274c563f76STimo Teras 						   .dump = xfrm_dump_policy,
28284c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2829492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2830980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
283153bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2832492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2833492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
28346c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2835492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2836492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2837d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2838d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
28395c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
284028d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2841880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2842880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2843880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2844ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
28452d151d39SSteffen Klassert 	[XFRM_MSG_SETDEFAULT  - XFRM_MSG_BASE] = { .doit = xfrm_set_default   },
28462d151d39SSteffen Klassert 	[XFRM_MSG_GETDEFAULT  - XFRM_MSG_BASE] = { .doit = xfrm_get_default   },
28471da177e4SLinus Torvalds };
28481da177e4SLinus Torvalds 
28492d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
28502d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
28511da177e4SLinus Torvalds {
2852a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
285335a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
285405600a79SMathias Krause 	const struct xfrm_link *link;
28555106f4a8SDmitry Safonov 	struct nlmsghdr *nlh64 = NULL;
2856a7bd9a45SThomas Graf 	int type, err;
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
28591da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
28601d00a4ebSThomas Graf 		return -EINVAL;
28611da177e4SLinus Torvalds 
28621da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
28631da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
28641da177e4SLinus Torvalds 
28651da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
286690f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
28671d00a4ebSThomas Graf 		return -EPERM;
28681da177e4SLinus Torvalds 
28695106f4a8SDmitry Safonov 	if (in_compat_syscall()) {
28705106f4a8SDmitry Safonov 		struct xfrm_translator *xtr = xfrm_get_translator();
28715106f4a8SDmitry Safonov 
28725106f4a8SDmitry Safonov 		if (!xtr)
28735106f4a8SDmitry Safonov 			return -EOPNOTSUPP;
28745106f4a8SDmitry Safonov 
28755106f4a8SDmitry Safonov 		nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
28765106f4a8SDmitry Safonov 					    link->nla_pol, extack);
28775106f4a8SDmitry Safonov 		xfrm_put_translator(xtr);
28785106f4a8SDmitry Safonov 		if (IS_ERR(nlh64))
28795106f4a8SDmitry Safonov 			return PTR_ERR(nlh64);
28805106f4a8SDmitry Safonov 		if (nlh64)
28815106f4a8SDmitry Safonov 			nlh = nlh64;
28825106f4a8SDmitry Safonov 	}
28835106f4a8SDmitry Safonov 
2884492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2885492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2886b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
288780d326faSPablo Neira Ayuso 		struct netlink_dump_control c = {
28881137b5e2SHerbert Xu 			.start = link->start,
288980d326faSPablo Neira Ayuso 			.dump = link->dump,
289080d326faSPablo Neira Ayuso 			.done = link->done,
289180d326faSPablo Neira Ayuso 		};
28925106f4a8SDmitry Safonov 
28935106f4a8SDmitry Safonov 		if (link->dump == NULL) {
28945106f4a8SDmitry Safonov 			err = -EINVAL;
28955106f4a8SDmitry Safonov 			goto err;
289680d326faSPablo Neira Ayuso 		}
28975106f4a8SDmitry Safonov 
28985106f4a8SDmitry Safonov 		err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
28995106f4a8SDmitry Safonov 		goto err;
29001da177e4SLinus Torvalds 	}
29011da177e4SLinus Torvalds 
29028cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2903880a6fabSChristophe Gouault 				     link->nla_max ? : XFRMA_MAX,
2904fe52145fSJohannes Berg 				     link->nla_pol ? : xfrma_policy, extack);
2905a7bd9a45SThomas Graf 	if (err < 0)
29065106f4a8SDmitry Safonov 		goto err;
29075106f4a8SDmitry Safonov 
29085106f4a8SDmitry Safonov 	if (link->doit == NULL) {
29095106f4a8SDmitry Safonov 		err = -EINVAL;
29105106f4a8SDmitry Safonov 		goto err;
29115106f4a8SDmitry Safonov 	}
29125106f4a8SDmitry Safonov 
29135106f4a8SDmitry Safonov 	err = link->doit(skb, nlh, attrs);
29145106f4a8SDmitry Safonov 
29157c1a80e8SPavel Skripkin 	/* We need to free skb allocated in xfrm_alloc_compat() before
29167c1a80e8SPavel Skripkin 	 * returning from this function, because consume_skb() won't take
29177c1a80e8SPavel Skripkin 	 * care of frag_list since netlink destructor sets
29187c1a80e8SPavel Skripkin 	 * sbk->head to NULL. (see netlink_skb_destructor())
29197c1a80e8SPavel Skripkin 	 */
29207c1a80e8SPavel Skripkin 	if (skb_has_frag_list(skb)) {
29217c1a80e8SPavel Skripkin 		kfree_skb(skb_shinfo(skb)->frag_list);
29227c1a80e8SPavel Skripkin 		skb_shinfo(skb)->frag_list = NULL;
29237c1a80e8SPavel Skripkin 	}
29247c1a80e8SPavel Skripkin 
29255106f4a8SDmitry Safonov err:
29265106f4a8SDmitry Safonov 	kvfree(nlh64);
2927a7bd9a45SThomas Graf 	return err;
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds 
2930cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
29311da177e4SLinus Torvalds {
2932283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2933283bc9f3SFan Du 
2934283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2935cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2936283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
29371da177e4SLinus Torvalds }
29381da177e4SLinus Torvalds 
2939a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void)
29407deb2264SThomas Graf {
29416f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
29426f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
29437deb2264SThomas Graf }
29447deb2264SThomas Graf 
2945214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
29461da177e4SLinus Torvalds {
29471da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
29481da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
29491d1e34ddSDavid S. Miller 	int err;
29501da177e4SLinus Torvalds 
295115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
295279b8b7f4SThomas Graf 	if (nlh == NULL)
295379b8b7f4SThomas Graf 		return -EMSGSIZE;
29541da177e4SLinus Torvalds 
29557b67c857SThomas Graf 	ue = nlmsg_data(nlh);
29561da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2957d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
2958e3e5fc16SMathias Krause 	/* clear the padding bytes */
2959caf283d0SKees Cook 	memset_after(ue, 0, hard);
29601da177e4SLinus Torvalds 
29611d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
29621d1e34ddSDavid S. Miller 	if (err)
29631d1e34ddSDavid S. Miller 		return err;
29646f26b61eSJamal Hadi Salim 
29657e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
29667e652640SSteffen Klassert 	if (err)
29677e652640SSteffen Klassert 		return err;
29687e652640SSteffen Klassert 
2969053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2970053c095aSJohannes Berg 	return 0;
29711da177e4SLinus Torvalds }
29721da177e4SLinus Torvalds 
2973214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
29741da177e4SLinus Torvalds {
2975fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
29761da177e4SLinus Torvalds 	struct sk_buff *skb;
29771da177e4SLinus Torvalds 
29787deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
29791da177e4SLinus Torvalds 	if (skb == NULL)
29801da177e4SLinus Torvalds 		return -ENOMEM;
29811da177e4SLinus Torvalds 
29826f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
29836f26b61eSJamal Hadi Salim 		kfree_skb(skb);
29846f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
29856f26b61eSJamal Hadi Salim 	}
29861da177e4SLinus Torvalds 
298721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
29881da177e4SLinus Torvalds }
29891da177e4SLinus Torvalds 
2990214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2991d51d081dSJamal Hadi Salim {
2992fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2993d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
29942fc5f83bSGustavo A. R. Silva 	int err;
2995d51d081dSJamal Hadi Salim 
2996d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2997d51d081dSJamal Hadi Salim 	if (skb == NULL)
2998d51d081dSJamal Hadi Salim 		return -ENOMEM;
2999d51d081dSJamal Hadi Salim 
30002fc5f83bSGustavo A. R. Silva 	err = build_aevent(skb, x, c);
30012fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
3002d51d081dSJamal Hadi Salim 
300321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
3004d51d081dSJamal Hadi Salim }
3005d51d081dSJamal Hadi Salim 
3006214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
300726b15dadSJamal Hadi Salim {
30087067802eSAlexey Dobriyan 	struct net *net = c->net;
300926b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
301026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
301126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
30127deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
301326b15dadSJamal Hadi Salim 
30147deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
301526b15dadSJamal Hadi Salim 	if (skb == NULL)
301626b15dadSJamal Hadi Salim 		return -ENOMEM;
301726b15dadSJamal Hadi Salim 
301815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
301979b8b7f4SThomas Graf 	if (nlh == NULL) {
302079b8b7f4SThomas Graf 		kfree_skb(skb);
302179b8b7f4SThomas Graf 		return -EMSGSIZE;
302279b8b7f4SThomas Graf 	}
302326b15dadSJamal Hadi Salim 
30247b67c857SThomas Graf 	p = nlmsg_data(nlh);
3025bf08867fSHerbert Xu 	p->proto = c->data.proto;
302626b15dadSJamal Hadi Salim 
30279825069dSThomas Graf 	nlmsg_end(skb, nlh);
302826b15dadSJamal Hadi Salim 
302921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
303026b15dadSJamal Hadi Salim }
303126b15dadSJamal Hadi Salim 
3032a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
303326b15dadSJamal Hadi Salim {
3034a1b831f2SAlexey Dobriyan 	unsigned int l = 0;
30351a6509d9SHerbert Xu 	if (x->aead)
30361a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
30374447bb33SMartin Willi 	if (x->aalg) {
30384447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
30394447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
30404447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
30414447bb33SMartin Willi 	}
304226b15dadSJamal Hadi Salim 	if (x->ealg)
30430f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
304426b15dadSJamal Hadi Salim 	if (x->calg)
30457deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
304626b15dadSJamal Hadi Salim 	if (x->encap)
30477deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
304835d2856bSMartin Willi 	if (x->tfcpad)
304935d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
3050d8647b79SSteffen Klassert 	if (x->replay_esn)
3051d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
3052f293a5e3Sdingzhi 	else
3053f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
305468325d3bSHerbert Xu 	if (x->security)
305568325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
305668325d3bSHerbert Xu 				    x->security->ctx_len);
305768325d3bSHerbert Xu 	if (x->coaddr)
305868325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
3059a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
3060a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
3061d77e38e6SSteffen Klassert 	if (x->xso.dev)
3062d77e38e6SSteffen Klassert 		 l += nla_total_size(sizeof(x->xso));
30639b42c1f1SSteffen Klassert 	if (x->props.smark.v | x->props.smark.m) {
30649b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.v));
30659b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.m));
30669b42c1f1SSteffen Klassert 	}
30677e652640SSteffen Klassert 	if (x->if_id)
30687e652640SSteffen Klassert 		l += nla_total_size(sizeof(x->if_id));
306968325d3bSHerbert Xu 
3070d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
3071de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
307226b15dadSJamal Hadi Salim 
307326b15dadSJamal Hadi Salim 	return l;
307426b15dadSJamal Hadi Salim }
307526b15dadSJamal Hadi Salim 
3076214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
307726b15dadSJamal Hadi Salim {
3078fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
307926b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
30800603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
308126b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
308226b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3083a1b831f2SAlexey Dobriyan 	unsigned int len = xfrm_sa_len(x);
3084a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3085a1b831f2SAlexey Dobriyan 	int err;
30860603eac0SHerbert Xu 
30870603eac0SHerbert Xu 	headlen = sizeof(*p);
30880603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
30897deb2264SThomas Graf 		len += nla_total_size(headlen);
30900603eac0SHerbert Xu 		headlen = sizeof(*id);
30916f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
30920603eac0SHerbert Xu 	}
30937deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
309426b15dadSJamal Hadi Salim 
30957deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
309626b15dadSJamal Hadi Salim 	if (skb == NULL)
309726b15dadSJamal Hadi Salim 		return -ENOMEM;
309826b15dadSJamal Hadi Salim 
309915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
31001d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
310179b8b7f4SThomas Graf 	if (nlh == NULL)
31021d1e34ddSDavid S. Miller 		goto out_free_skb;
310326b15dadSJamal Hadi Salim 
31047b67c857SThomas Graf 	p = nlmsg_data(nlh);
31050603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
3106c0144beaSThomas Graf 		struct nlattr *attr;
3107c0144beaSThomas Graf 
31087b67c857SThomas Graf 		id = nlmsg_data(nlh);
310950329c8aSMathias Krause 		memset(id, 0, sizeof(*id));
31100603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
31110603eac0SHerbert Xu 		id->spi = x->id.spi;
31120603eac0SHerbert Xu 		id->family = x->props.family;
31130603eac0SHerbert Xu 		id->proto = x->id.proto;
31140603eac0SHerbert Xu 
3115c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
31161d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3117c0144beaSThomas Graf 		if (attr == NULL)
31181d1e34ddSDavid S. Miller 			goto out_free_skb;
3119c0144beaSThomas Graf 
3120c0144beaSThomas Graf 		p = nla_data(attr);
31210603eac0SHerbert Xu 	}
31221d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
31231d1e34ddSDavid S. Miller 	if (err)
31241d1e34ddSDavid S. Miller 		goto out_free_skb;
312526b15dadSJamal Hadi Salim 
31269825069dSThomas Graf 	nlmsg_end(skb, nlh);
312726b15dadSJamal Hadi Salim 
312821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
312926b15dadSJamal Hadi Salim 
31301d1e34ddSDavid S. Miller out_free_skb:
313126b15dadSJamal Hadi Salim 	kfree_skb(skb);
31321d1e34ddSDavid S. Miller 	return err;
313326b15dadSJamal Hadi Salim }
313426b15dadSJamal Hadi Salim 
3135214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
313626b15dadSJamal Hadi Salim {
313726b15dadSJamal Hadi Salim 
313826b15dadSJamal Hadi Salim 	switch (c->event) {
3139f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
314026b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
3141d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
3142d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
3143f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
3144f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
3145f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
314626b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
3147f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
314826b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
314926b15dadSJamal Hadi Salim 	default:
315062db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
315162db5cfdSstephen hemminger 		       c->event);
315226b15dadSJamal Hadi Salim 		break;
315326b15dadSJamal Hadi Salim 	}
315426b15dadSJamal Hadi Salim 
315526b15dadSJamal Hadi Salim 	return 0;
315626b15dadSJamal Hadi Salim 
315726b15dadSJamal Hadi Salim }
315826b15dadSJamal Hadi Salim 
3159a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
31607deb2264SThomas Graf 						struct xfrm_policy *xp)
31617deb2264SThomas Graf {
31627deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
31637deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
31646f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
31657deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
31667deb2264SThomas Graf 	       + userpolicy_type_attrsize();
31677deb2264SThomas Graf }
31687deb2264SThomas Graf 
31691da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
317065e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
31711da177e4SLinus Torvalds {
31721d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
31731da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
31741da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
31751d1e34ddSDavid S. Miller 	int err;
31761da177e4SLinus Torvalds 
317779b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
317879b8b7f4SThomas Graf 	if (nlh == NULL)
317979b8b7f4SThomas Graf 		return -EMSGSIZE;
31801da177e4SLinus Torvalds 
31817b67c857SThomas Graf 	ua = nlmsg_data(nlh);
31821da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
31831da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
31841da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
318565e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
31861da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
31871da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
31881da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
31891da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
31901da177e4SLinus Torvalds 
31911d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
31921d1e34ddSDavid S. Miller 	if (!err)
31931d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
31941d1e34ddSDavid S. Miller 	if (!err)
31951d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
31961d1e34ddSDavid S. Miller 	if (!err)
31971d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
31987e652640SSteffen Klassert 	if (!err)
31997e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
32001d1e34ddSDavid S. Miller 	if (err) {
32011d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
32021d1e34ddSDavid S. Miller 		return err;
32031d1e34ddSDavid S. Miller 	}
32041da177e4SLinus Torvalds 
3205053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3206053c095aSJohannes Berg 	return 0;
32071da177e4SLinus Torvalds }
32081da177e4SLinus Torvalds 
32091da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
321065e0736bSFan Du 			     struct xfrm_policy *xp)
32111da177e4SLinus Torvalds {
3212a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
32131da177e4SLinus Torvalds 	struct sk_buff *skb;
32142fc5f83bSGustavo A. R. Silva 	int err;
32151da177e4SLinus Torvalds 
32167deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
32171da177e4SLinus Torvalds 	if (skb == NULL)
32181da177e4SLinus Torvalds 		return -ENOMEM;
32191da177e4SLinus Torvalds 
32202fc5f83bSGustavo A. R. Silva 	err = build_acquire(skb, x, xt, xp);
32212fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
32221da177e4SLinus Torvalds 
322321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
32241da177e4SLinus Torvalds }
32251da177e4SLinus Torvalds 
32261da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
32271da177e4SLinus Torvalds  * or more templates.
32281da177e4SLinus Torvalds  */
3229cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
32301da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
32311da177e4SLinus Torvalds {
3232fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
32331da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
32341da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
32351da177e4SLinus Torvalds 	struct xfrm_policy *xp;
32361da177e4SLinus Torvalds 	int nr;
32371da177e4SLinus Torvalds 
3238cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
32391da177e4SLinus Torvalds 	case AF_INET:
32401da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
32411da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
32421da177e4SLinus Torvalds 			return NULL;
32431da177e4SLinus Torvalds 		}
32441da177e4SLinus Torvalds 		break;
3245dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
32461da177e4SLinus Torvalds 	case AF_INET6:
32471da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
32481da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
32491da177e4SLinus Torvalds 			return NULL;
32501da177e4SLinus Torvalds 		}
32511da177e4SLinus Torvalds 		break;
32521da177e4SLinus Torvalds #endif
32531da177e4SLinus Torvalds 	default:
32541da177e4SLinus Torvalds 		*dir = -EINVAL;
32551da177e4SLinus Torvalds 		return NULL;
32561da177e4SLinus Torvalds 	}
32571da177e4SLinus Torvalds 
32581da177e4SLinus Torvalds 	*dir = -EINVAL;
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
32611da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
32621da177e4SLinus Torvalds 		return NULL;
32631da177e4SLinus Torvalds 
32641da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
3265b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
32661da177e4SLinus Torvalds 		return NULL;
32671da177e4SLinus Torvalds 
3268a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
3269a4f1bac6SHerbert Xu 		return NULL;
3270a4f1bac6SHerbert Xu 
32712f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
32721da177e4SLinus Torvalds 	if (xp == NULL) {
32731da177e4SLinus Torvalds 		*dir = -ENOBUFS;
32741da177e4SLinus Torvalds 		return NULL;
32751da177e4SLinus Torvalds 	}
32761da177e4SLinus Torvalds 
32771da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
3278f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
32791da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
32801da177e4SLinus Torvalds 
32811da177e4SLinus Torvalds 	*dir = p->dir;
32821da177e4SLinus Torvalds 
32831da177e4SLinus Torvalds 	return xp;
32841da177e4SLinus Torvalds }
32851da177e4SLinus Torvalds 
3286a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
32877deb2264SThomas Graf {
32887deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
32897deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
32907deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3291295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
32927deb2264SThomas Graf 	       + userpolicy_type_attrsize();
32937deb2264SThomas Graf }
32947deb2264SThomas Graf 
32951da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3296214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
32971da177e4SLinus Torvalds {
32981da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
3299d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
33001d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
33011d1e34ddSDavid S. Miller 	int err;
33021da177e4SLinus Torvalds 
330315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
330479b8b7f4SThomas Graf 	if (nlh == NULL)
330579b8b7f4SThomas Graf 		return -EMSGSIZE;
33061da177e4SLinus Torvalds 
33077b67c857SThomas Graf 	upe = nlmsg_data(nlh);
33081da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
33091d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
33101d1e34ddSDavid S. Miller 	if (!err)
33111d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
33121d1e34ddSDavid S. Miller 	if (!err)
33131d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
33141d1e34ddSDavid S. Miller 	if (!err)
33151d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
33167e652640SSteffen Klassert 	if (!err)
33177e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
33181d1e34ddSDavid S. Miller 	if (err) {
33191d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
33201d1e34ddSDavid S. Miller 		return err;
33211d1e34ddSDavid S. Miller 	}
33221da177e4SLinus Torvalds 	upe->hard = !!hard;
33231da177e4SLinus Torvalds 
3324053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3325053c095aSJohannes Berg 	return 0;
33261da177e4SLinus Torvalds }
33271da177e4SLinus Torvalds 
3328214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
33291da177e4SLinus Torvalds {
3330fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
33311da177e4SLinus Torvalds 	struct sk_buff *skb;
33322fc5f83bSGustavo A. R. Silva 	int err;
33331da177e4SLinus Torvalds 
33347deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
33351da177e4SLinus Torvalds 	if (skb == NULL)
33361da177e4SLinus Torvalds 		return -ENOMEM;
33371da177e4SLinus Torvalds 
33382fc5f83bSGustavo A. R. Silva 	err = build_polexpire(skb, xp, dir, c);
33392fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
33401da177e4SLinus Torvalds 
334121ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
33421da177e4SLinus Torvalds }
33431da177e4SLinus Torvalds 
3344214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
334526b15dadSJamal Hadi Salim {
3346a1b831f2SAlexey Dobriyan 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3347fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
334826b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
33490603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
335026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
335126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3352a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3353a1b831f2SAlexey Dobriyan 	int err;
33540603eac0SHerbert Xu 
33550603eac0SHerbert Xu 	headlen = sizeof(*p);
33560603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
33577deb2264SThomas Graf 		len += nla_total_size(headlen);
33580603eac0SHerbert Xu 		headlen = sizeof(*id);
33590603eac0SHerbert Xu 	}
3360cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
3361295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
33627deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
336326b15dadSJamal Hadi Salim 
33647deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
336526b15dadSJamal Hadi Salim 	if (skb == NULL)
336626b15dadSJamal Hadi Salim 		return -ENOMEM;
336726b15dadSJamal Hadi Salim 
336815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
33691d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
337079b8b7f4SThomas Graf 	if (nlh == NULL)
33711d1e34ddSDavid S. Miller 		goto out_free_skb;
337226b15dadSJamal Hadi Salim 
33737b67c857SThomas Graf 	p = nlmsg_data(nlh);
33740603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
3375c0144beaSThomas Graf 		struct nlattr *attr;
3376c0144beaSThomas Graf 
33777b67c857SThomas Graf 		id = nlmsg_data(nlh);
33780603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
33790603eac0SHerbert Xu 		id->dir = dir;
33800603eac0SHerbert Xu 		if (c->data.byid)
33810603eac0SHerbert Xu 			id->index = xp->index;
33820603eac0SHerbert Xu 		else
33830603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
33840603eac0SHerbert Xu 
3385c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
33861d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3387c0144beaSThomas Graf 		if (attr == NULL)
33881d1e34ddSDavid S. Miller 			goto out_free_skb;
3389c0144beaSThomas Graf 
3390c0144beaSThomas Graf 		p = nla_data(attr);
33910603eac0SHerbert Xu 	}
339226b15dadSJamal Hadi Salim 
339326b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
33941d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
33951d1e34ddSDavid S. Miller 	if (!err)
33961d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
33971d1e34ddSDavid S. Miller 	if (!err)
33981d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
33997e652640SSteffen Klassert 	if (!err)
34007e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
34011d1e34ddSDavid S. Miller 	if (err)
34021d1e34ddSDavid S. Miller 		goto out_free_skb;
3403295fae56SJamal Hadi Salim 
34049825069dSThomas Graf 	nlmsg_end(skb, nlh);
340526b15dadSJamal Hadi Salim 
340621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
340726b15dadSJamal Hadi Salim 
34081d1e34ddSDavid S. Miller out_free_skb:
340926b15dadSJamal Hadi Salim 	kfree_skb(skb);
34101d1e34ddSDavid S. Miller 	return err;
341126b15dadSJamal Hadi Salim }
341226b15dadSJamal Hadi Salim 
3413214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
341426b15dadSJamal Hadi Salim {
34157067802eSAlexey Dobriyan 	struct net *net = c->net;
341626b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
341726b15dadSJamal Hadi Salim 	struct sk_buff *skb;
34181d1e34ddSDavid S. Miller 	int err;
341926b15dadSJamal Hadi Salim 
34207deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
342126b15dadSJamal Hadi Salim 	if (skb == NULL)
342226b15dadSJamal Hadi Salim 		return -ENOMEM;
342326b15dadSJamal Hadi Salim 
342415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
34251d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
342679b8b7f4SThomas Graf 	if (nlh == NULL)
34271d1e34ddSDavid S. Miller 		goto out_free_skb;
34281d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
34291d1e34ddSDavid S. Miller 	if (err)
34301d1e34ddSDavid S. Miller 		goto out_free_skb;
343126b15dadSJamal Hadi Salim 
34329825069dSThomas Graf 	nlmsg_end(skb, nlh);
343326b15dadSJamal Hadi Salim 
343421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
343526b15dadSJamal Hadi Salim 
34361d1e34ddSDavid S. Miller out_free_skb:
343726b15dadSJamal Hadi Salim 	kfree_skb(skb);
34381d1e34ddSDavid S. Miller 	return err;
343926b15dadSJamal Hadi Salim }
344026b15dadSJamal Hadi Salim 
3441214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
344226b15dadSJamal Hadi Salim {
344326b15dadSJamal Hadi Salim 
344426b15dadSJamal Hadi Salim 	switch (c->event) {
3445f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
3446f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
3447f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
344826b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
3449f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
345026b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
3451f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
345226b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
345326b15dadSJamal Hadi Salim 	default:
345462db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
345562db5cfdSstephen hemminger 		       c->event);
345626b15dadSJamal Hadi Salim 	}
345726b15dadSJamal Hadi Salim 
345826b15dadSJamal Hadi Salim 	return 0;
345926b15dadSJamal Hadi Salim 
346026b15dadSJamal Hadi Salim }
346126b15dadSJamal Hadi Salim 
3462a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void)
34637deb2264SThomas Graf {
34647deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
34657deb2264SThomas Graf }
34667deb2264SThomas Graf 
346797a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
346897a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
346997a64b45SMasahide NAKAMURA {
347097a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
347197a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
347297a64b45SMasahide NAKAMURA 
347379b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
347479b8b7f4SThomas Graf 	if (nlh == NULL)
347579b8b7f4SThomas Graf 		return -EMSGSIZE;
347697a64b45SMasahide NAKAMURA 
34777b67c857SThomas Graf 	ur = nlmsg_data(nlh);
347897a64b45SMasahide NAKAMURA 	ur->proto = proto;
347997a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
348097a64b45SMasahide NAKAMURA 
34811d1e34ddSDavid S. Miller 	if (addr) {
34821d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
34831d1e34ddSDavid S. Miller 		if (err) {
34849825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
34851d1e34ddSDavid S. Miller 			return err;
34861d1e34ddSDavid S. Miller 		}
34871d1e34ddSDavid S. Miller 	}
3488053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3489053c095aSJohannes Berg 	return 0;
349097a64b45SMasahide NAKAMURA }
349197a64b45SMasahide NAKAMURA 
3492db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3493db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
349497a64b45SMasahide NAKAMURA {
349597a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
34962fc5f83bSGustavo A. R. Silva 	int err;
349797a64b45SMasahide NAKAMURA 
34987deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
349997a64b45SMasahide NAKAMURA 	if (skb == NULL)
350097a64b45SMasahide NAKAMURA 		return -ENOMEM;
350197a64b45SMasahide NAKAMURA 
35022fc5f83bSGustavo A. R. Silva 	err = build_report(skb, proto, sel, addr);
35032fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
350497a64b45SMasahide NAKAMURA 
350521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
350697a64b45SMasahide NAKAMURA }
350797a64b45SMasahide NAKAMURA 
3508a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void)
35093a2dfbe8SMartin Willi {
35103a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
35113a2dfbe8SMartin Willi }
35123a2dfbe8SMartin Willi 
35133a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
35143a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
35153a2dfbe8SMartin Willi {
35163a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
35173a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
35183a2dfbe8SMartin Willi 
35193a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
35203a2dfbe8SMartin Willi 	if (nlh == NULL)
35213a2dfbe8SMartin Willi 		return -EMSGSIZE;
35223a2dfbe8SMartin Willi 
35233a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
35243a2dfbe8SMartin Willi 
35253a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
35263a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
35273a2dfbe8SMartin Willi 	um->id.family = x->props.family;
35283a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
35293a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
35303a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
35313a2dfbe8SMartin Willi 	um->new_sport = new_sport;
35323a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
35333a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
35343a2dfbe8SMartin Willi 
3535053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3536053c095aSJohannes Berg 	return 0;
35373a2dfbe8SMartin Willi }
35383a2dfbe8SMartin Willi 
35393a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
35403a2dfbe8SMartin Willi 			     __be16 sport)
35413a2dfbe8SMartin Willi {
3542a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
35433a2dfbe8SMartin Willi 	struct sk_buff *skb;
35442fc5f83bSGustavo A. R. Silva 	int err;
35453a2dfbe8SMartin Willi 
35463a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
35473a2dfbe8SMartin Willi 		return -EINVAL;
35483a2dfbe8SMartin Willi 
35493a2dfbe8SMartin Willi 	if (!x->encap)
35503a2dfbe8SMartin Willi 		return -EINVAL;
35513a2dfbe8SMartin Willi 
35523a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
35533a2dfbe8SMartin Willi 	if (skb == NULL)
35543a2dfbe8SMartin Willi 		return -ENOMEM;
35553a2dfbe8SMartin Willi 
35562fc5f83bSGustavo A. R. Silva 	err = build_mapping(skb, x, ipaddr, sport);
35572fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
35583a2dfbe8SMartin Willi 
355921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
35603a2dfbe8SMartin Willi }
35613a2dfbe8SMartin Willi 
35620f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
35630f24558eSHoria Geanta {
35640f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
35650f24558eSHoria Geanta }
35660f24558eSHoria Geanta 
35671da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
35681da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
35691da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
35701da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
35711da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
357297a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
35735c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
35743a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
35750f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
35761da177e4SLinus Torvalds };
35771da177e4SLinus Torvalds 
3578a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
35791da177e4SLinus Torvalds {
3580be33690dSPatrick McHardy 	struct sock *nlsk;
3581a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3582a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3583a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3584a31f2d17SPablo Neira Ayuso 	};
3585be33690dSPatrick McHardy 
35869f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3587be33690dSPatrick McHardy 	if (nlsk == NULL)
35881da177e4SLinus Torvalds 		return -ENOMEM;
3589d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3590cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
35911da177e4SLinus Torvalds 	return 0;
35921da177e4SLinus Torvalds }
35931da177e4SLinus Torvalds 
35946218fe18SFlorian Westphal static void __net_exit xfrm_user_net_pre_exit(struct net *net)
35956218fe18SFlorian Westphal {
35966218fe18SFlorian Westphal 	RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
35976218fe18SFlorian Westphal }
35986218fe18SFlorian Westphal 
3599d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3600a6483b79SAlexey Dobriyan {
3601d79d792eSEric W. Biederman 	struct net *net;
36026218fe18SFlorian Westphal 
3603d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3604d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3605a6483b79SAlexey Dobriyan }
3606a6483b79SAlexey Dobriyan 
3607a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3608a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
36096218fe18SFlorian Westphal 	.pre_exit   = xfrm_user_net_pre_exit,
3610d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3611a6483b79SAlexey Dobriyan };
3612a6483b79SAlexey Dobriyan 
3613a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3614a6483b79SAlexey Dobriyan {
3615a6483b79SAlexey Dobriyan 	int rv;
3616a6483b79SAlexey Dobriyan 
3617a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3618a6483b79SAlexey Dobriyan 
3619a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3620a6483b79SAlexey Dobriyan 	if (rv < 0)
3621a6483b79SAlexey Dobriyan 		return rv;
3622a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3623a6483b79SAlexey Dobriyan 	if (rv < 0)
3624a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3625a6483b79SAlexey Dobriyan 	return rv;
3626a6483b79SAlexey Dobriyan }
3627a6483b79SAlexey Dobriyan 
36281da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
36291da177e4SLinus Torvalds {
36301da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3631a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
36321da177e4SLinus Torvalds }
36331da177e4SLinus Torvalds 
36341da177e4SLinus Torvalds module_init(xfrm_user_init);
36351da177e4SLinus Torvalds module_exit(xfrm_user_exit);
36361da177e4SLinus Torvalds MODULE_LICENSE("GPL");
36374fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3638