xref: /linux/net/xfrm/xfrm_user.c (revision a7fd0e6d758f0f29268438287ecf7873c069a3ae)
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 
149409f38aSHerbert Xu #include <linux/crypto.h>
151da177e4SLinus Torvalds #include <linux/module.h>
161da177e4SLinus Torvalds #include <linux/kernel.h>
171da177e4SLinus Torvalds #include <linux/types.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/socket.h>
201da177e4SLinus Torvalds #include <linux/string.h>
211da177e4SLinus Torvalds #include <linux/net.h>
221da177e4SLinus Torvalds #include <linux/skbuff.h>
231da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
241da177e4SLinus Torvalds #include <linux/ipsec.h>
251da177e4SLinus Torvalds #include <linux/init.h>
261da177e4SLinus Torvalds #include <linux/security.h>
271da177e4SLinus Torvalds #include <net/sock.h>
281da177e4SLinus Torvalds #include <net/xfrm.h>
2988fc2c84SThomas Graf #include <net/netlink.h>
30fa6dd8a2SNicolas Dichtel #include <net/ah.h>
317c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
32dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
33e23c7194SMasahide NAKAMURA #include <linux/in6.h>
34e23c7194SMasahide NAKAMURA #endif
35e33d4f13SSowmini Varadhan #include <asm/unaligned.h>
361da177e4SLinus Torvalds 
375424f32eSThomas Graf static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
381da177e4SLinus Torvalds {
395424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
401da177e4SLinus Torvalds 	struct xfrm_algo *algp;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	if (!rt)
431da177e4SLinus Torvalds 		return 0;
441da177e4SLinus Torvalds 
455424f32eSThomas Graf 	algp = nla_data(rt);
4606cd22f8SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_alg_len(algp))
4731c26852SHerbert Xu 		return -EINVAL;
4831c26852SHerbert Xu 
491da177e4SLinus Torvalds 	switch (type) {
501da177e4SLinus Torvalds 	case XFRMA_ALG_AUTH:
511da177e4SLinus Torvalds 	case XFRMA_ALG_CRYPT:
521da177e4SLinus Torvalds 	case XFRMA_ALG_COMP:
531da177e4SLinus Torvalds 		break;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	default:
561da177e4SLinus Torvalds 		return -EINVAL;
573ff50b79SStephen Hemminger 	}
581da177e4SLinus Torvalds 
59633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
601da177e4SLinus Torvalds 	return 0;
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
634447bb33SMartin Willi static int verify_auth_trunc(struct nlattr **attrs)
644447bb33SMartin Willi {
654447bb33SMartin Willi 	struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
664447bb33SMartin Willi 	struct xfrm_algo_auth *algp;
674447bb33SMartin Willi 
684447bb33SMartin Willi 	if (!rt)
694447bb33SMartin Willi 		return 0;
704447bb33SMartin Willi 
714447bb33SMartin Willi 	algp = nla_data(rt);
721bd963a7SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
734447bb33SMartin Willi 		return -EINVAL;
744447bb33SMartin Willi 
75633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
764447bb33SMartin Willi 	return 0;
774447bb33SMartin Willi }
784447bb33SMartin Willi 
791a6509d9SHerbert Xu static int verify_aead(struct nlattr **attrs)
801a6509d9SHerbert Xu {
811a6509d9SHerbert Xu 	struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
821a6509d9SHerbert Xu 	struct xfrm_algo_aead *algp;
831a6509d9SHerbert Xu 
841a6509d9SHerbert Xu 	if (!rt)
851a6509d9SHerbert Xu 		return 0;
861a6509d9SHerbert Xu 
871a6509d9SHerbert Xu 	algp = nla_data(rt);
88373b8eebSAlexey Dobriyan 	if (nla_len(rt) < (int)aead_len(algp))
891a6509d9SHerbert Xu 		return -EINVAL;
901a6509d9SHerbert Xu 
91633439f5SHerbert Xu 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
921a6509d9SHerbert Xu 	return 0;
931a6509d9SHerbert Xu }
941a6509d9SHerbert Xu 
955424f32eSThomas Graf static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
96eb2971b6SMasahide NAKAMURA 			   xfrm_address_t **addrp)
97eb2971b6SMasahide NAKAMURA {
985424f32eSThomas Graf 	struct nlattr *rt = attrs[type];
99eb2971b6SMasahide NAKAMURA 
100cf5cb79fSThomas Graf 	if (rt && addrp)
1015424f32eSThomas Graf 		*addrp = nla_data(rt);
102eb2971b6SMasahide NAKAMURA }
103df71837dSTrent Jaeger 
1045424f32eSThomas Graf static inline int verify_sec_ctx_len(struct nlattr **attrs)
105df71837dSTrent Jaeger {
1065424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
107df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
108df71837dSTrent Jaeger 
109df71837dSTrent Jaeger 	if (!rt)
110df71837dSTrent Jaeger 		return 0;
111df71837dSTrent Jaeger 
1125424f32eSThomas Graf 	uctx = nla_data(rt);
113171d449aSXin Long 	if (uctx->len > nla_len(rt) ||
114171d449aSXin Long 	    uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
115df71837dSTrent Jaeger 		return -EINVAL;
116df71837dSTrent Jaeger 
117df71837dSTrent Jaeger 	return 0;
118df71837dSTrent Jaeger }
119df71837dSTrent Jaeger 
120d8647b79SSteffen Klassert static inline int verify_replay(struct xfrm_usersa_info *p,
121d8647b79SSteffen Klassert 				struct nlattr **attrs)
122d8647b79SSteffen Klassert {
123d8647b79SSteffen Klassert 	struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
124ecd79187SMathias Krause 	struct xfrm_replay_state_esn *rs;
125d8647b79SSteffen Klassert 
126ecd79187SMathias Krause 	if (!rt)
127d97ca5d7SFlorian Westphal 		return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
1287833aa05SSteffen Klassert 
129ecd79187SMathias Krause 	rs = nla_data(rt);
130ecd79187SMathias Krause 
131ecd79187SMathias Krause 	if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
132ecd79187SMathias Krause 		return -EINVAL;
133ecd79187SMathias Krause 
1345e708e47SAlexey Dobriyan 	if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
135ecd79187SMathias Krause 	    nla_len(rt) != sizeof(*rs))
136ecd79187SMathias Krause 		return -EINVAL;
137d8647b79SSteffen Klassert 
13801714109SFan Du 	/* As only ESP and AH support ESN feature. */
13901714109SFan Du 	if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
14002aadf72SSteffen Klassert 		return -EINVAL;
14102aadf72SSteffen Klassert 
142d8647b79SSteffen Klassert 	if (p->replay_window != 0)
143d8647b79SSteffen Klassert 		return -EINVAL;
144d8647b79SSteffen Klassert 
145d8647b79SSteffen Klassert 	return 0;
146d8647b79SSteffen Klassert }
147df71837dSTrent Jaeger 
1481da177e4SLinus Torvalds static int verify_newsa_info(struct xfrm_usersa_info *p,
1495424f32eSThomas Graf 			     struct nlattr **attrs)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	int err;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	err = -EINVAL;
1541da177e4SLinus Torvalds 	switch (p->family) {
1551da177e4SLinus Torvalds 	case AF_INET:
156b38ff407SAnirudh Gupta 		break;
157b38ff407SAnirudh Gupta 
158b38ff407SAnirudh Gupta 	case AF_INET6:
159b38ff407SAnirudh Gupta #if IS_ENABLED(CONFIG_IPV6)
160b38ff407SAnirudh Gupta 		break;
161b38ff407SAnirudh Gupta #else
162b38ff407SAnirudh Gupta 		err = -EAFNOSUPPORT;
163b38ff407SAnirudh Gupta 		goto out;
164b38ff407SAnirudh Gupta #endif
165b38ff407SAnirudh Gupta 
166b38ff407SAnirudh Gupta 	default:
167b38ff407SAnirudh Gupta 		goto out;
168b38ff407SAnirudh Gupta 	}
169b38ff407SAnirudh Gupta 
170b38ff407SAnirudh Gupta 	switch (p->sel.family) {
171b8d6d007SNicolas Dichtel 	case AF_UNSPEC:
172b8d6d007SNicolas Dichtel 		break;
173b8d6d007SNicolas Dichtel 
174b38ff407SAnirudh Gupta 	case AF_INET:
17507bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
17607bf7908SSteffen Klassert 			goto out;
17707bf7908SSteffen Klassert 
1781da177e4SLinus Torvalds 		break;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	case AF_INET6:
181dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
18207bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
18307bf7908SSteffen Klassert 			goto out;
18407bf7908SSteffen Klassert 
1851da177e4SLinus Torvalds 		break;
1861da177e4SLinus Torvalds #else
1871da177e4SLinus Torvalds 		err = -EAFNOSUPPORT;
1881da177e4SLinus Torvalds 		goto out;
1891da177e4SLinus Torvalds #endif
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	default:
1921da177e4SLinus Torvalds 		goto out;
1933ff50b79SStephen Hemminger 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	err = -EINVAL;
1961da177e4SLinus Torvalds 	switch (p->id.proto) {
1971da177e4SLinus Torvalds 	case IPPROTO_AH:
1984447bb33SMartin Willi 		if ((!attrs[XFRMA_ALG_AUTH]	&&
1994447bb33SMartin Willi 		     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
2001a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
20135a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
20235d2856bSMartin Willi 		    attrs[XFRMA_ALG_COMP]	||
203a0e5ef53STobias Brunner 		    attrs[XFRMA_TFCPAD])
2041da177e4SLinus Torvalds 			goto out;
2051da177e4SLinus Torvalds 		break;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	case IPPROTO_ESP:
2081a6509d9SHerbert Xu 		if (attrs[XFRMA_ALG_COMP])
2091a6509d9SHerbert Xu 			goto out;
2101a6509d9SHerbert Xu 		if (!attrs[XFRMA_ALG_AUTH] &&
2114447bb33SMartin Willi 		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
2121a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_CRYPT] &&
2131a6509d9SHerbert Xu 		    !attrs[XFRMA_ALG_AEAD])
2141a6509d9SHerbert Xu 			goto out;
2151a6509d9SHerbert Xu 		if ((attrs[XFRMA_ALG_AUTH] ||
2164447bb33SMartin Willi 		     attrs[XFRMA_ALG_AUTH_TRUNC] ||
2171a6509d9SHerbert Xu 		     attrs[XFRMA_ALG_CRYPT]) &&
2181a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD])
2191da177e4SLinus Torvalds 			goto out;
22035d2856bSMartin Willi 		if (attrs[XFRMA_TFCPAD] &&
22135d2856bSMartin Willi 		    p->mode != XFRM_MODE_TUNNEL)
22235d2856bSMartin Willi 			goto out;
2231da177e4SLinus Torvalds 		break;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	case IPPROTO_COMP:
22635a7aa08SThomas Graf 		if (!attrs[XFRMA_ALG_COMP]	||
2271a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
22835a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2294447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
23035d2856bSMartin Willi 		    attrs[XFRMA_ALG_CRYPT]	||
231a0e5ef53STobias Brunner 		    attrs[XFRMA_TFCPAD]		||
232a0e5ef53STobias Brunner 		    (ntohl(p->id.spi) >= 0x10000))
2331da177e4SLinus Torvalds 			goto out;
2341da177e4SLinus Torvalds 		break;
2351da177e4SLinus Torvalds 
236dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
237e23c7194SMasahide NAKAMURA 	case IPPROTO_DSTOPTS:
238e23c7194SMasahide NAKAMURA 	case IPPROTO_ROUTING:
23935a7aa08SThomas Graf 		if (attrs[XFRMA_ALG_COMP]	||
24035a7aa08SThomas Graf 		    attrs[XFRMA_ALG_AUTH]	||
2414447bb33SMartin Willi 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
2421a6509d9SHerbert Xu 		    attrs[XFRMA_ALG_AEAD]	||
24335a7aa08SThomas Graf 		    attrs[XFRMA_ALG_CRYPT]	||
24435a7aa08SThomas Graf 		    attrs[XFRMA_ENCAP]		||
24535a7aa08SThomas Graf 		    attrs[XFRMA_SEC_CTX]	||
24635d2856bSMartin Willi 		    attrs[XFRMA_TFCPAD]		||
24735a7aa08SThomas Graf 		    !attrs[XFRMA_COADDR])
248e23c7194SMasahide NAKAMURA 			goto out;
249e23c7194SMasahide NAKAMURA 		break;
250e23c7194SMasahide NAKAMURA #endif
251e23c7194SMasahide NAKAMURA 
2521da177e4SLinus Torvalds 	default:
2531da177e4SLinus Torvalds 		goto out;
2543ff50b79SStephen Hemminger 	}
2551da177e4SLinus Torvalds 
2561a6509d9SHerbert Xu 	if ((err = verify_aead(attrs)))
2571a6509d9SHerbert Xu 		goto out;
2584447bb33SMartin Willi 	if ((err = verify_auth_trunc(attrs)))
2594447bb33SMartin Willi 		goto out;
26035a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
2611da177e4SLinus Torvalds 		goto out;
26235a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
2631da177e4SLinus Torvalds 		goto out;
26435a7aa08SThomas Graf 	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
2651da177e4SLinus Torvalds 		goto out;
26635a7aa08SThomas Graf 	if ((err = verify_sec_ctx_len(attrs)))
267df71837dSTrent Jaeger 		goto out;
268d8647b79SSteffen Klassert 	if ((err = verify_replay(p, attrs)))
269d8647b79SSteffen Klassert 		goto out;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	err = -EINVAL;
2721da177e4SLinus Torvalds 	switch (p->mode) {
2737e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TRANSPORT:
2747e49e6deSMasahide NAKAMURA 	case XFRM_MODE_TUNNEL:
275060f02a3SNoriaki TAKAMIYA 	case XFRM_MODE_ROUTEOPTIMIZATION:
2760a69452cSDiego Beltrami 	case XFRM_MODE_BEET:
2771da177e4SLinus Torvalds 		break;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	default:
2801da177e4SLinus Torvalds 		goto out;
2813ff50b79SStephen Hemminger 	}
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	err = 0;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds out:
2861da177e4SLinus Torvalds 	return err;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
2906f2f19edSDavid S. Miller 			   struct xfrm_algo_desc *(*get_byname)(const char *, int),
2915424f32eSThomas Graf 			   struct nlattr *rta)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	struct xfrm_algo *p, *ualg;
2941da177e4SLinus Torvalds 	struct xfrm_algo_desc *algo;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	if (!rta)
2971da177e4SLinus Torvalds 		return 0;
2981da177e4SLinus Torvalds 
2995424f32eSThomas Graf 	ualg = nla_data(rta);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	algo = get_byname(ualg->alg_name, 1);
3021da177e4SLinus Torvalds 	if (!algo)
3031da177e4SLinus Torvalds 		return -ENOSYS;
3041da177e4SLinus Torvalds 	*props = algo->desc.sadb_alg_id;
3051da177e4SLinus Torvalds 
3060f99be0dSEric Dumazet 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
3071da177e4SLinus Torvalds 	if (!p)
3081da177e4SLinus Torvalds 		return -ENOMEM;
3091da177e4SLinus Torvalds 
31004ff1260SHerbert Xu 	strcpy(p->alg_name, algo->name);
3111da177e4SLinus Torvalds 	*algpp = p;
3121da177e4SLinus Torvalds 	return 0;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
31569b0137fSHerbert Xu static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
31669b0137fSHerbert Xu {
31769b0137fSHerbert Xu 	struct xfrm_algo *p, *ualg;
31869b0137fSHerbert Xu 	struct xfrm_algo_desc *algo;
31969b0137fSHerbert Xu 
32069b0137fSHerbert Xu 	if (!rta)
32169b0137fSHerbert Xu 		return 0;
32269b0137fSHerbert Xu 
32369b0137fSHerbert Xu 	ualg = nla_data(rta);
32469b0137fSHerbert Xu 
32569b0137fSHerbert Xu 	algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
32669b0137fSHerbert Xu 	if (!algo)
32769b0137fSHerbert Xu 		return -ENOSYS;
32869b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
32969b0137fSHerbert Xu 
33069b0137fSHerbert Xu 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
33169b0137fSHerbert Xu 	if (!p)
33269b0137fSHerbert Xu 		return -ENOMEM;
33369b0137fSHerbert Xu 
33469b0137fSHerbert Xu 	strcpy(p->alg_name, algo->name);
33569b0137fSHerbert Xu 	x->ealg = p;
33669b0137fSHerbert Xu 	x->geniv = algo->uinfo.encr.geniv;
33769b0137fSHerbert Xu 	return 0;
33869b0137fSHerbert Xu }
33969b0137fSHerbert Xu 
3404447bb33SMartin Willi static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
3414447bb33SMartin Willi 		       struct nlattr *rta)
3424447bb33SMartin Willi {
3434447bb33SMartin Willi 	struct xfrm_algo *ualg;
3444447bb33SMartin Willi 	struct xfrm_algo_auth *p;
3454447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3464447bb33SMartin Willi 
3474447bb33SMartin Willi 	if (!rta)
3484447bb33SMartin Willi 		return 0;
3494447bb33SMartin Willi 
3504447bb33SMartin Willi 	ualg = nla_data(rta);
3514447bb33SMartin Willi 
3524447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3534447bb33SMartin Willi 	if (!algo)
3544447bb33SMartin Willi 		return -ENOSYS;
3554447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3564447bb33SMartin Willi 
3574447bb33SMartin Willi 	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
3584447bb33SMartin Willi 	if (!p)
3594447bb33SMartin Willi 		return -ENOMEM;
3604447bb33SMartin Willi 
3614447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3624447bb33SMartin Willi 	p->alg_key_len = ualg->alg_key_len;
3634447bb33SMartin Willi 	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3644447bb33SMartin Willi 	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
3654447bb33SMartin Willi 
3664447bb33SMartin Willi 	*algpp = p;
3674447bb33SMartin Willi 	return 0;
3684447bb33SMartin Willi }
3694447bb33SMartin Willi 
3704447bb33SMartin Willi static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
3714447bb33SMartin Willi 			     struct nlattr *rta)
3724447bb33SMartin Willi {
3734447bb33SMartin Willi 	struct xfrm_algo_auth *p, *ualg;
3744447bb33SMartin Willi 	struct xfrm_algo_desc *algo;
3754447bb33SMartin Willi 
3764447bb33SMartin Willi 	if (!rta)
3774447bb33SMartin Willi 		return 0;
3784447bb33SMartin Willi 
3794447bb33SMartin Willi 	ualg = nla_data(rta);
3804447bb33SMartin Willi 
3814447bb33SMartin Willi 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
3824447bb33SMartin Willi 	if (!algo)
3834447bb33SMartin Willi 		return -ENOSYS;
384689f1c9dSHerbert Xu 	if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
3854447bb33SMartin Willi 		return -EINVAL;
3864447bb33SMartin Willi 	*props = algo->desc.sadb_alg_id;
3874447bb33SMartin Willi 
3884447bb33SMartin Willi 	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
3894447bb33SMartin Willi 	if (!p)
3904447bb33SMartin Willi 		return -ENOMEM;
3914447bb33SMartin Willi 
3924447bb33SMartin Willi 	strcpy(p->alg_name, algo->name);
3934447bb33SMartin Willi 	if (!p->alg_trunc_len)
3944447bb33SMartin Willi 		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
3954447bb33SMartin Willi 
3964447bb33SMartin Willi 	*algpp = p;
3974447bb33SMartin Willi 	return 0;
3984447bb33SMartin Willi }
3994447bb33SMartin Willi 
40069b0137fSHerbert Xu static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
4011a6509d9SHerbert Xu {
4021a6509d9SHerbert Xu 	struct xfrm_algo_aead *p, *ualg;
4031a6509d9SHerbert Xu 	struct xfrm_algo_desc *algo;
4041a6509d9SHerbert Xu 
4051a6509d9SHerbert Xu 	if (!rta)
4061a6509d9SHerbert Xu 		return 0;
4071a6509d9SHerbert Xu 
4081a6509d9SHerbert Xu 	ualg = nla_data(rta);
4091a6509d9SHerbert Xu 
4101a6509d9SHerbert Xu 	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
4111a6509d9SHerbert Xu 	if (!algo)
4121a6509d9SHerbert Xu 		return -ENOSYS;
41369b0137fSHerbert Xu 	x->props.ealgo = algo->desc.sadb_alg_id;
4141a6509d9SHerbert Xu 
4151a6509d9SHerbert Xu 	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
4161a6509d9SHerbert Xu 	if (!p)
4171a6509d9SHerbert Xu 		return -ENOMEM;
4181a6509d9SHerbert Xu 
4191a6509d9SHerbert Xu 	strcpy(p->alg_name, algo->name);
42069b0137fSHerbert Xu 	x->aead = p;
42169b0137fSHerbert Xu 	x->geniv = algo->uinfo.aead.geniv;
4221a6509d9SHerbert Xu 	return 0;
4231a6509d9SHerbert Xu }
4241a6509d9SHerbert Xu 
425e2b19125SSteffen Klassert static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
426e2b19125SSteffen Klassert 					 struct nlattr *rp)
427e2b19125SSteffen Klassert {
428e2b19125SSteffen Klassert 	struct xfrm_replay_state_esn *up;
4295e708e47SAlexey Dobriyan 	unsigned int ulen;
430e2b19125SSteffen Klassert 
431e2b19125SSteffen Klassert 	if (!replay_esn || !rp)
432e2b19125SSteffen Klassert 		return 0;
433e2b19125SSteffen Klassert 
434e2b19125SSteffen Klassert 	up = nla_data(rp);
435ecd79187SMathias Krause 	ulen = xfrm_replay_state_esn_len(up);
436e2b19125SSteffen Klassert 
437f843ee6dSAndy Whitcroft 	/* Check the overall length and the internal bitmap length to avoid
438f843ee6dSAndy Whitcroft 	 * potential overflow. */
4395e708e47SAlexey Dobriyan 	if (nla_len(rp) < (int)ulen ||
440f843ee6dSAndy Whitcroft 	    xfrm_replay_state_esn_len(replay_esn) != ulen ||
441f843ee6dSAndy Whitcroft 	    replay_esn->bmp_len != up->bmp_len)
442e2b19125SSteffen Klassert 		return -EINVAL;
443e2b19125SSteffen Klassert 
444677e806dSAndy Whitcroft 	if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
445677e806dSAndy Whitcroft 		return -EINVAL;
446677e806dSAndy Whitcroft 
447e2b19125SSteffen Klassert 	return 0;
448e2b19125SSteffen Klassert }
449e2b19125SSteffen Klassert 
450d8647b79SSteffen Klassert static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
451d8647b79SSteffen Klassert 				       struct xfrm_replay_state_esn **preplay_esn,
452d8647b79SSteffen Klassert 				       struct nlattr *rta)
453d8647b79SSteffen Klassert {
454d8647b79SSteffen Klassert 	struct xfrm_replay_state_esn *p, *pp, *up;
4555e708e47SAlexey Dobriyan 	unsigned int klen, ulen;
456d8647b79SSteffen Klassert 
457d8647b79SSteffen Klassert 	if (!rta)
458d8647b79SSteffen Klassert 		return 0;
459d8647b79SSteffen Klassert 
460d8647b79SSteffen Klassert 	up = nla_data(rta);
461ecd79187SMathias Krause 	klen = xfrm_replay_state_esn_len(up);
4625e708e47SAlexey Dobriyan 	ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
463d8647b79SSteffen Klassert 
464ecd79187SMathias Krause 	p = kzalloc(klen, GFP_KERNEL);
465d8647b79SSteffen Klassert 	if (!p)
466d8647b79SSteffen Klassert 		return -ENOMEM;
467d8647b79SSteffen Klassert 
468ecd79187SMathias Krause 	pp = kzalloc(klen, GFP_KERNEL);
469d8647b79SSteffen Klassert 	if (!pp) {
470d8647b79SSteffen Klassert 		kfree(p);
471d8647b79SSteffen Klassert 		return -ENOMEM;
472d8647b79SSteffen Klassert 	}
473d8647b79SSteffen Klassert 
474ecd79187SMathias Krause 	memcpy(p, up, ulen);
475ecd79187SMathias Krause 	memcpy(pp, up, ulen);
476ecd79187SMathias Krause 
477d8647b79SSteffen Klassert 	*replay_esn = p;
478d8647b79SSteffen Klassert 	*preplay_esn = pp;
479d8647b79SSteffen Klassert 
480d8647b79SSteffen Klassert 	return 0;
481d8647b79SSteffen Klassert }
482d8647b79SSteffen Klassert 
483a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
484df71837dSTrent Jaeger {
485a1b831f2SAlexey Dobriyan 	unsigned int len = 0;
486df71837dSTrent Jaeger 
487df71837dSTrent Jaeger 	if (xfrm_ctx) {
488df71837dSTrent Jaeger 		len += sizeof(struct xfrm_user_sec_ctx);
489df71837dSTrent Jaeger 		len += xfrm_ctx->ctx_len;
490df71837dSTrent Jaeger 	}
491df71837dSTrent Jaeger 	return len;
492df71837dSTrent Jaeger }
493df71837dSTrent Jaeger 
4941da177e4SLinus Torvalds static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
4951da177e4SLinus Torvalds {
4961da177e4SLinus Torvalds 	memcpy(&x->id, &p->id, sizeof(x->id));
4971da177e4SLinus Torvalds 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
4981da177e4SLinus Torvalds 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
4991da177e4SLinus Torvalds 	x->props.mode = p->mode;
50033fce60dSFan Du 	x->props.replay_window = min_t(unsigned int, p->replay_window,
50133fce60dSFan Du 					sizeof(x->replay.bitmap) * 8);
5021da177e4SLinus Torvalds 	x->props.reqid = p->reqid;
5031da177e4SLinus Torvalds 	x->props.family = p->family;
50454489c14SDavid S. Miller 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
5051da177e4SLinus Torvalds 	x->props.flags = p->flags;
506196b0036SHerbert Xu 
507ccf9b3b8SSteffen Klassert 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
508196b0036SHerbert Xu 		x->sel.family = p->family;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
511d51d081dSJamal Hadi Salim /*
512d51d081dSJamal Hadi Salim  * someday when pfkey also has support, we could have the code
513d51d081dSJamal Hadi Salim  * somehow made shareable and move it to xfrm_state.c - JHS
514d51d081dSJamal Hadi Salim  *
515d51d081dSJamal Hadi Salim */
516e3ac104dSMathias Krause static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
517e3ac104dSMathias Krause 				  int update_esn)
518d51d081dSJamal Hadi Salim {
5195424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
520e3ac104dSMathias Krause 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
5215424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
5225424f32eSThomas Graf 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
5235424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
524d51d081dSJamal Hadi Salim 
525d8647b79SSteffen Klassert 	if (re) {
526d8647b79SSteffen Klassert 		struct xfrm_replay_state_esn *replay_esn;
527d8647b79SSteffen Klassert 		replay_esn = nla_data(re);
528d8647b79SSteffen Klassert 		memcpy(x->replay_esn, replay_esn,
529d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
530d8647b79SSteffen Klassert 		memcpy(x->preplay_esn, replay_esn,
531d8647b79SSteffen Klassert 		       xfrm_replay_state_esn_len(replay_esn));
532d8647b79SSteffen Klassert 	}
533d8647b79SSteffen Klassert 
534d51d081dSJamal Hadi Salim 	if (rp) {
535d51d081dSJamal Hadi Salim 		struct xfrm_replay_state *replay;
5365424f32eSThomas Graf 		replay = nla_data(rp);
537d51d081dSJamal Hadi Salim 		memcpy(&x->replay, replay, sizeof(*replay));
538d51d081dSJamal Hadi Salim 		memcpy(&x->preplay, replay, sizeof(*replay));
539d51d081dSJamal Hadi Salim 	}
540d51d081dSJamal Hadi Salim 
541d51d081dSJamal Hadi Salim 	if (lt) {
542d51d081dSJamal Hadi Salim 		struct xfrm_lifetime_cur *ltime;
5435424f32eSThomas Graf 		ltime = nla_data(lt);
544d51d081dSJamal Hadi Salim 		x->curlft.bytes = ltime->bytes;
545d51d081dSJamal Hadi Salim 		x->curlft.packets = ltime->packets;
546d51d081dSJamal Hadi Salim 		x->curlft.add_time = ltime->add_time;
547d51d081dSJamal Hadi Salim 		x->curlft.use_time = ltime->use_time;
548d51d081dSJamal Hadi Salim 	}
549d51d081dSJamal Hadi Salim 
550cf5cb79fSThomas Graf 	if (et)
5515424f32eSThomas Graf 		x->replay_maxage = nla_get_u32(et);
552d51d081dSJamal Hadi Salim 
553cf5cb79fSThomas Graf 	if (rt)
5545424f32eSThomas Graf 		x->replay_maxdiff = nla_get_u32(rt);
555d51d081dSJamal Hadi Salim }
556d51d081dSJamal Hadi Salim 
5579b42c1f1SSteffen Klassert static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
5589b42c1f1SSteffen Klassert {
5599b42c1f1SSteffen Klassert 	if (attrs[XFRMA_SET_MARK]) {
5609b42c1f1SSteffen Klassert 		m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
5619b42c1f1SSteffen Klassert 		if (attrs[XFRMA_SET_MARK_MASK])
5629b42c1f1SSteffen Klassert 			m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
5639b42c1f1SSteffen Klassert 		else
5649b42c1f1SSteffen Klassert 			m->m = 0xffffffff;
5659b42c1f1SSteffen Klassert 	} else {
5669b42c1f1SSteffen Klassert 		m->v = m->m = 0;
5679b42c1f1SSteffen Klassert 	}
5689b42c1f1SSteffen Klassert }
5699b42c1f1SSteffen Klassert 
570fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_state_construct(struct net *net,
571fc34acd3SAlexey Dobriyan 					       struct xfrm_usersa_info *p,
5725424f32eSThomas Graf 					       struct nlattr **attrs,
5731da177e4SLinus Torvalds 					       int *errp)
5741da177e4SLinus Torvalds {
575fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
5761da177e4SLinus Torvalds 	int err = -ENOMEM;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	if (!x)
5791da177e4SLinus Torvalds 		goto error_no_put;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	copy_from_user_state(x, p);
5821da177e4SLinus Torvalds 
583a947b0a9SNicolas Dichtel 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
584a947b0a9SNicolas Dichtel 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
585a947b0a9SNicolas Dichtel 
58669b0137fSHerbert Xu 	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
5871a6509d9SHerbert Xu 		goto error;
5884447bb33SMartin Willi 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
5894447bb33SMartin Willi 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
5904447bb33SMartin Willi 		goto error;
5914447bb33SMartin Willi 	if (!x->props.aalgo) {
5924447bb33SMartin Willi 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
59335a7aa08SThomas Graf 				       attrs[XFRMA_ALG_AUTH])))
5941da177e4SLinus Torvalds 			goto error;
5954447bb33SMartin Willi 	}
59669b0137fSHerbert Xu 	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
5971da177e4SLinus Torvalds 		goto error;
5981da177e4SLinus Torvalds 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
5991da177e4SLinus Torvalds 				   xfrm_calg_get_byname,
60035a7aa08SThomas Graf 				   attrs[XFRMA_ALG_COMP])))
6011da177e4SLinus Torvalds 		goto error;
602fd21150aSThomas Graf 
603fd21150aSThomas Graf 	if (attrs[XFRMA_ENCAP]) {
604fd21150aSThomas Graf 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
605fd21150aSThomas Graf 				   sizeof(*x->encap), GFP_KERNEL);
606fd21150aSThomas Graf 		if (x->encap == NULL)
6071da177e4SLinus Torvalds 			goto error;
608fd21150aSThomas Graf 	}
609fd21150aSThomas Graf 
61035d2856bSMartin Willi 	if (attrs[XFRMA_TFCPAD])
61135d2856bSMartin Willi 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
61235d2856bSMartin Willi 
613fd21150aSThomas Graf 	if (attrs[XFRMA_COADDR]) {
614fd21150aSThomas Graf 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
615fd21150aSThomas Graf 				    sizeof(*x->coaddr), GFP_KERNEL);
616fd21150aSThomas Graf 		if (x->coaddr == NULL)
617060f02a3SNoriaki TAKAMIYA 			goto error;
618fd21150aSThomas Graf 	}
619fd21150aSThomas Graf 
6206f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &x->mark);
6216f26b61eSJamal Hadi Salim 
6229b42c1f1SSteffen Klassert 	xfrm_smark_init(attrs, &x->props.smark);
623077fbac4SLorenzo Colitti 
6247e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
6257e652640SSteffen Klassert 		x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
6261da177e4SLinus Torvalds 
627ffdb5211SIlan Tayari 	err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
6281da177e4SLinus Torvalds 	if (err)
6291da177e4SLinus Torvalds 		goto error;
6301da177e4SLinus Torvalds 
6312f30ea50SMathias Krause 	if (attrs[XFRMA_SEC_CTX]) {
6322f30ea50SMathias Krause 		err = security_xfrm_state_alloc(x,
6332f30ea50SMathias Krause 						nla_data(attrs[XFRMA_SEC_CTX]));
6342f30ea50SMathias Krause 		if (err)
635df71837dSTrent Jaeger 			goto error;
6362f30ea50SMathias Krause 	}
637df71837dSTrent Jaeger 
638d8647b79SSteffen Klassert 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
639d8647b79SSteffen Klassert 					       attrs[XFRMA_REPLAY_ESN_VAL])))
640d8647b79SSteffen Klassert 		goto error;
641d8647b79SSteffen Klassert 
6421da177e4SLinus Torvalds 	x->km.seq = p->seq;
643b27aeadbSAlexey Dobriyan 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
644d51d081dSJamal Hadi Salim 	/* sysctl_xfrm_aevent_etime is in 100ms units */
645b27aeadbSAlexey Dobriyan 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
646d51d081dSJamal Hadi Salim 
6479fdc4883SSteffen Klassert 	if ((err = xfrm_init_replay(x)))
6489fdc4883SSteffen Klassert 		goto error;
649d51d081dSJamal Hadi Salim 
6509fdc4883SSteffen Klassert 	/* override default values from above */
651e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 0);
6521da177e4SLinus Torvalds 
653cc01572eSYossi Kuperman 	/* configure the hardware if offload is requested */
654cc01572eSYossi Kuperman 	if (attrs[XFRMA_OFFLOAD_DEV]) {
655cc01572eSYossi Kuperman 		err = xfrm_dev_state_add(net, x,
656cc01572eSYossi Kuperman 					 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
657cc01572eSYossi Kuperman 		if (err)
658cc01572eSYossi Kuperman 			goto error;
659cc01572eSYossi Kuperman 	}
660cc01572eSYossi Kuperman 
6611da177e4SLinus Torvalds 	return x;
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds error:
6641da177e4SLinus Torvalds 	x->km.state = XFRM_STATE_DEAD;
6651da177e4SLinus Torvalds 	xfrm_state_put(x);
6661da177e4SLinus Torvalds error_no_put:
6671da177e4SLinus Torvalds 	*errp = err;
6681da177e4SLinus Torvalds 	return NULL;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
67122e70050SChristoph Hellwig static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
6725424f32eSThomas Graf 		struct nlattr **attrs)
6731da177e4SLinus Torvalds {
674fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
6757b67c857SThomas Graf 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
6761da177e4SLinus Torvalds 	struct xfrm_state *x;
6771da177e4SLinus Torvalds 	int err;
67826b15dadSJamal Hadi Salim 	struct km_event c;
6791da177e4SLinus Torvalds 
68035a7aa08SThomas Graf 	err = verify_newsa_info(p, attrs);
6811da177e4SLinus Torvalds 	if (err)
6821da177e4SLinus Torvalds 		return err;
6831da177e4SLinus Torvalds 
684fc34acd3SAlexey Dobriyan 	x = xfrm_state_construct(net, p, attrs, &err);
6851da177e4SLinus Torvalds 	if (!x)
6861da177e4SLinus Torvalds 		return err;
6871da177e4SLinus Torvalds 
68826b15dadSJamal Hadi Salim 	xfrm_state_hold(x);
6891da177e4SLinus Torvalds 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
6901da177e4SLinus Torvalds 		err = xfrm_state_add(x);
6911da177e4SLinus Torvalds 	else
6921da177e4SLinus Torvalds 		err = xfrm_state_update(x);
6931da177e4SLinus Torvalds 
6942e71029eSTetsuo Handa 	xfrm_audit_state_add(x, err ? 0 : 1, true);
695161a09e7SJoy Latten 
6961da177e4SLinus Torvalds 	if (err < 0) {
6971da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
698c5d4d7d8SSteffen Klassert 		xfrm_dev_state_delete(x);
69921380b81SHerbert Xu 		__xfrm_state_put(x);
7007d6dfe1fSPatrick McHardy 		goto out;
7011da177e4SLinus Torvalds 	}
7021da177e4SLinus Torvalds 
703cc01572eSYossi Kuperman 	if (x->km.state == XFRM_STATE_VOID)
704cc01572eSYossi Kuperman 		x->km.state = XFRM_STATE_VALID;
705cc01572eSYossi Kuperman 
70626b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
70715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
708f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
70926b15dadSJamal Hadi Salim 
71026b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7117d6dfe1fSPatrick McHardy out:
71226b15dadSJamal Hadi Salim 	xfrm_state_put(x);
7131da177e4SLinus Torvalds 	return err;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
716fc34acd3SAlexey Dobriyan static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
717fc34acd3SAlexey Dobriyan 						 struct xfrm_usersa_id *p,
7185424f32eSThomas Graf 						 struct nlattr **attrs,
719eb2971b6SMasahide NAKAMURA 						 int *errp)
720eb2971b6SMasahide NAKAMURA {
721eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x = NULL;
7226f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
723eb2971b6SMasahide NAKAMURA 	int err;
7246f26b61eSJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
725eb2971b6SMasahide NAKAMURA 
726eb2971b6SMasahide NAKAMURA 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
727eb2971b6SMasahide NAKAMURA 		err = -ESRCH;
7286f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
729eb2971b6SMasahide NAKAMURA 	} else {
730eb2971b6SMasahide NAKAMURA 		xfrm_address_t *saddr = NULL;
731eb2971b6SMasahide NAKAMURA 
73235a7aa08SThomas Graf 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
733eb2971b6SMasahide NAKAMURA 		if (!saddr) {
734eb2971b6SMasahide NAKAMURA 			err = -EINVAL;
735eb2971b6SMasahide NAKAMURA 			goto out;
736eb2971b6SMasahide NAKAMURA 		}
737eb2971b6SMasahide NAKAMURA 
7389abbffeeSMasahide NAKAMURA 		err = -ESRCH;
7396f26b61eSJamal Hadi Salim 		x = xfrm_state_lookup_byaddr(net, mark,
7406f26b61eSJamal Hadi Salim 					     &p->daddr, saddr,
741221df1edSAlexey Dobriyan 					     p->proto, p->family);
742eb2971b6SMasahide NAKAMURA 	}
743eb2971b6SMasahide NAKAMURA 
744eb2971b6SMasahide NAKAMURA  out:
745eb2971b6SMasahide NAKAMURA 	if (!x && errp)
746eb2971b6SMasahide NAKAMURA 		*errp = err;
747eb2971b6SMasahide NAKAMURA 	return x;
748eb2971b6SMasahide NAKAMURA }
749eb2971b6SMasahide NAKAMURA 
75022e70050SChristoph Hellwig static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
7515424f32eSThomas Graf 		struct nlattr **attrs)
7521da177e4SLinus Torvalds {
753fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
7541da177e4SLinus Torvalds 	struct xfrm_state *x;
755eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
75626b15dadSJamal Hadi Salim 	struct km_event c;
7577b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
7581da177e4SLinus Torvalds 
759fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
7601da177e4SLinus Torvalds 	if (x == NULL)
761eb2971b6SMasahide NAKAMURA 		return err;
7621da177e4SLinus Torvalds 
7636f68dc37SDavid S. Miller 	if ((err = security_xfrm_state_delete(x)) != 0)
764c8c05a8eSCatherine Zhang 		goto out;
765c8c05a8eSCatherine Zhang 
7661da177e4SLinus Torvalds 	if (xfrm_state_kern(x)) {
767c8c05a8eSCatherine Zhang 		err = -EPERM;
768c8c05a8eSCatherine Zhang 		goto out;
7691da177e4SLinus Torvalds 	}
7701da177e4SLinus Torvalds 
77126b15dadSJamal Hadi Salim 	err = xfrm_state_delete(x);
772161a09e7SJoy Latten 
773c8c05a8eSCatherine Zhang 	if (err < 0)
774c8c05a8eSCatherine Zhang 		goto out;
77526b15dadSJamal Hadi Salim 
77626b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
77715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
778f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
77926b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
7801da177e4SLinus Torvalds 
781c8c05a8eSCatherine Zhang out:
7822e71029eSTetsuo Handa 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
783c8c05a8eSCatherine Zhang 	xfrm_state_put(x);
78426b15dadSJamal Hadi Salim 	return err;
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
7881da177e4SLinus Torvalds {
789f778a636SMathias Krause 	memset(p, 0, sizeof(*p));
7901da177e4SLinus Torvalds 	memcpy(&p->id, &x->id, sizeof(p->id));
7911da177e4SLinus Torvalds 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
7921da177e4SLinus Torvalds 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
7931da177e4SLinus Torvalds 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
794e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
795e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.replay, &p->stats.replay);
796e33d4f13SSowmini Varadhan 	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
79754489c14SDavid S. Miller 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
7981da177e4SLinus Torvalds 	p->mode = x->props.mode;
7991da177e4SLinus Torvalds 	p->replay_window = x->props.replay_window;
8001da177e4SLinus Torvalds 	p->reqid = x->props.reqid;
8011da177e4SLinus Torvalds 	p->family = x->props.family;
8021da177e4SLinus Torvalds 	p->flags = x->props.flags;
8031da177e4SLinus Torvalds 	p->seq = x->km.seq;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds struct xfrm_dump_info {
8071da177e4SLinus Torvalds 	struct sk_buff *in_skb;
8081da177e4SLinus Torvalds 	struct sk_buff *out_skb;
8091da177e4SLinus Torvalds 	u32 nlmsg_seq;
8101da177e4SLinus Torvalds 	u16 nlmsg_flags;
8111da177e4SLinus Torvalds };
8121da177e4SLinus Torvalds 
813c0144beaSThomas Graf static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
814c0144beaSThomas Graf {
815c0144beaSThomas Graf 	struct xfrm_user_sec_ctx *uctx;
816c0144beaSThomas Graf 	struct nlattr *attr;
81768325d3bSHerbert Xu 	int ctx_size = sizeof(*uctx) + s->ctx_len;
818c0144beaSThomas Graf 
819c0144beaSThomas Graf 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
820c0144beaSThomas Graf 	if (attr == NULL)
821c0144beaSThomas Graf 		return -EMSGSIZE;
822c0144beaSThomas Graf 
823c0144beaSThomas Graf 	uctx = nla_data(attr);
824c0144beaSThomas Graf 	uctx->exttype = XFRMA_SEC_CTX;
825c0144beaSThomas Graf 	uctx->len = ctx_size;
826c0144beaSThomas Graf 	uctx->ctx_doi = s->ctx_doi;
827c0144beaSThomas Graf 	uctx->ctx_alg = s->ctx_alg;
828c0144beaSThomas Graf 	uctx->ctx_len = s->ctx_len;
829c0144beaSThomas Graf 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
830c0144beaSThomas Graf 
831c0144beaSThomas Graf 	return 0;
832c0144beaSThomas Graf }
833c0144beaSThomas Graf 
834d77e38e6SSteffen Klassert static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
835d77e38e6SSteffen Klassert {
836d77e38e6SSteffen Klassert 	struct xfrm_user_offload *xuo;
837d77e38e6SSteffen Klassert 	struct nlattr *attr;
838d77e38e6SSteffen Klassert 
839d77e38e6SSteffen Klassert 	attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
840d77e38e6SSteffen Klassert 	if (attr == NULL)
841d77e38e6SSteffen Klassert 		return -EMSGSIZE;
842d77e38e6SSteffen Klassert 
843d77e38e6SSteffen Klassert 	xuo = nla_data(attr);
8445fe0d4bdSMathias Krause 	memset(xuo, 0, sizeof(*xuo));
845d77e38e6SSteffen Klassert 	xuo->ifindex = xso->dev->ifindex;
846d77e38e6SSteffen Klassert 	xuo->flags = xso->flags;
847d77e38e6SSteffen Klassert 
848d77e38e6SSteffen Klassert 	return 0;
849d77e38e6SSteffen Klassert }
850d77e38e6SSteffen Klassert 
851c7a5899eSAntony Antony static bool xfrm_redact(void)
852c7a5899eSAntony Antony {
853c7a5899eSAntony Antony 	return IS_ENABLED(CONFIG_SECURITY) &&
854c7a5899eSAntony Antony 		security_locked_down(LOCKDOWN_XFRM_SECRET);
855c7a5899eSAntony Antony }
856c7a5899eSAntony Antony 
8574447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
8584447bb33SMartin Willi {
8594447bb33SMartin Willi 	struct xfrm_algo *algo;
860c7a5899eSAntony Antony 	struct xfrm_algo_auth *ap;
8614447bb33SMartin Willi 	struct nlattr *nla;
862c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
8634447bb33SMartin Willi 
8644447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
8654447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
8664447bb33SMartin Willi 	if (!nla)
8674447bb33SMartin Willi 		return -EMSGSIZE;
8684447bb33SMartin Willi 	algo = nla_data(nla);
8694c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
870c7a5899eSAntony Antony 
871c7a5899eSAntony Antony 	if (redact_secret && auth->alg_key_len)
872c7a5899eSAntony Antony 		memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
873c7a5899eSAntony Antony 	else
874c7a5899eSAntony Antony 		memcpy(algo->alg_key, auth->alg_key,
875c7a5899eSAntony Antony 		       (auth->alg_key_len + 7) / 8);
8764447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
8774447bb33SMartin Willi 
878c7a5899eSAntony Antony 	nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
879c7a5899eSAntony Antony 	if (!nla)
880c7a5899eSAntony Antony 		return -EMSGSIZE;
881c7a5899eSAntony Antony 	ap = nla_data(nla);
882c7a5899eSAntony Antony 	memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
883c7a5899eSAntony Antony 	if (redact_secret && auth->alg_key_len)
884c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
885c7a5899eSAntony Antony 	else
886c7a5899eSAntony Antony 		memcpy(ap->alg_key, auth->alg_key,
887c7a5899eSAntony Antony 		       (auth->alg_key_len + 7) / 8);
888c7a5899eSAntony Antony 	return 0;
889c7a5899eSAntony Antony }
890c7a5899eSAntony Antony 
891c7a5899eSAntony Antony static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
892c7a5899eSAntony Antony {
893c7a5899eSAntony Antony 	struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
894c7a5899eSAntony Antony 	struct xfrm_algo_aead *ap;
895c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
896c7a5899eSAntony Antony 
897c7a5899eSAntony Antony 	if (!nla)
898c7a5899eSAntony Antony 		return -EMSGSIZE;
899c7a5899eSAntony Antony 
900c7a5899eSAntony Antony 	ap = nla_data(nla);
901c7a5899eSAntony Antony 	memcpy(ap, aead, sizeof(*aead));
902c7a5899eSAntony Antony 
903c7a5899eSAntony Antony 	if (redact_secret && aead->alg_key_len)
904c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
905c7a5899eSAntony Antony 	else
906c7a5899eSAntony Antony 		memcpy(ap->alg_key, aead->alg_key,
907c7a5899eSAntony Antony 		       (aead->alg_key_len + 7) / 8);
908c7a5899eSAntony Antony 	return 0;
909c7a5899eSAntony Antony }
910c7a5899eSAntony Antony 
911c7a5899eSAntony Antony static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
912c7a5899eSAntony Antony {
913c7a5899eSAntony Antony 	struct xfrm_algo *ap;
914c7a5899eSAntony Antony 	bool redact_secret = xfrm_redact();
915c7a5899eSAntony Antony 	struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
916c7a5899eSAntony Antony 					 xfrm_alg_len(ealg));
917c7a5899eSAntony Antony 	if (!nla)
918c7a5899eSAntony Antony 		return -EMSGSIZE;
919c7a5899eSAntony Antony 
920c7a5899eSAntony Antony 	ap = nla_data(nla);
921c7a5899eSAntony Antony 	memcpy(ap, ealg, sizeof(*ealg));
922c7a5899eSAntony Antony 
923c7a5899eSAntony Antony 	if (redact_secret && ealg->alg_key_len)
924c7a5899eSAntony Antony 		memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
925c7a5899eSAntony Antony 	else
926c7a5899eSAntony Antony 		memcpy(ap->alg_key, ealg->alg_key,
927c7a5899eSAntony Antony 		       (ealg->alg_key_len + 7) / 8);
928c7a5899eSAntony Antony 
9294447bb33SMartin Willi 	return 0;
9304447bb33SMartin Willi }
9314447bb33SMartin Willi 
9329b42c1f1SSteffen Klassert static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
9339b42c1f1SSteffen Klassert {
9349b42c1f1SSteffen Klassert 	int ret = 0;
9359b42c1f1SSteffen Klassert 
9369b42c1f1SSteffen Klassert 	if (m->v | m->m) {
9379b42c1f1SSteffen Klassert 		ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
9389b42c1f1SSteffen Klassert 		if (!ret)
9399b42c1f1SSteffen Klassert 			ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
9409b42c1f1SSteffen Klassert 	}
9419b42c1f1SSteffen Klassert 	return ret;
9429b42c1f1SSteffen Klassert }
9439b42c1f1SSteffen Klassert 
94468325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
94568325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
94668325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
94768325d3bSHerbert Xu 				    struct sk_buff *skb)
9481da177e4SLinus Torvalds {
9491d1e34ddSDavid S. Miller 	int ret = 0;
9501d1e34ddSDavid S. Miller 
9511da177e4SLinus Torvalds 	copy_to_user_state(x, p);
9521da177e4SLinus Torvalds 
953a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
954a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
955a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
956a947b0a9SNicolas Dichtel 		if (ret)
957a947b0a9SNicolas Dichtel 			goto out;
958a947b0a9SNicolas Dichtel 	}
959a947b0a9SNicolas Dichtel 
9601d1e34ddSDavid S. Miller 	if (x->coaddr) {
9611d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
9621d1e34ddSDavid S. Miller 		if (ret)
9631d1e34ddSDavid S. Miller 			goto out;
9641d1e34ddSDavid S. Miller 	}
9651d1e34ddSDavid S. Miller 	if (x->lastused) {
966de95c4a4SNicolas Dichtel 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
967de95c4a4SNicolas Dichtel 					XFRMA_PAD);
9681d1e34ddSDavid S. Miller 		if (ret)
9691d1e34ddSDavid S. Miller 			goto out;
9701d1e34ddSDavid S. Miller 	}
9711d1e34ddSDavid S. Miller 	if (x->aead) {
972c7a5899eSAntony Antony 		ret = copy_to_user_aead(x->aead, skb);
9731d1e34ddSDavid S. Miller 		if (ret)
9741d1e34ddSDavid S. Miller 			goto out;
9751d1e34ddSDavid S. Miller 	}
9761d1e34ddSDavid S. Miller 	if (x->aalg) {
9771d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
9781d1e34ddSDavid S. Miller 		if (ret)
9791d1e34ddSDavid S. Miller 			goto out;
9801d1e34ddSDavid S. Miller 	}
9811d1e34ddSDavid S. Miller 	if (x->ealg) {
982c7a5899eSAntony Antony 		ret = copy_to_user_ealg(x->ealg, skb);
9831d1e34ddSDavid S. Miller 		if (ret)
9841d1e34ddSDavid S. Miller 			goto out;
9851d1e34ddSDavid S. Miller 	}
9861d1e34ddSDavid S. Miller 	if (x->calg) {
9871d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
9881d1e34ddSDavid S. Miller 		if (ret)
9891d1e34ddSDavid S. Miller 			goto out;
9901d1e34ddSDavid S. Miller 	}
9911d1e34ddSDavid S. Miller 	if (x->encap) {
9921d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
9931d1e34ddSDavid S. Miller 		if (ret)
9941d1e34ddSDavid S. Miller 			goto out;
9951d1e34ddSDavid S. Miller 	}
9961d1e34ddSDavid S. Miller 	if (x->tfcpad) {
9971d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
9981d1e34ddSDavid S. Miller 		if (ret)
9991d1e34ddSDavid S. Miller 			goto out;
10001d1e34ddSDavid S. Miller 	}
10011d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
10021d1e34ddSDavid S. Miller 	if (ret)
10031d1e34ddSDavid S. Miller 		goto out;
10049b42c1f1SSteffen Klassert 
10059b42c1f1SSteffen Klassert 	ret = xfrm_smark_put(skb, &x->props.smark);
10069b42c1f1SSteffen Klassert 	if (ret)
10079b42c1f1SSteffen Klassert 		goto out;
10089b42c1f1SSteffen Klassert 
1009f293a5e3Sdingzhi 	if (x->replay_esn)
10101d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1011d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
10121d1e34ddSDavid S. Miller 			      x->replay_esn);
1013f293a5e3Sdingzhi 	else
1014f293a5e3Sdingzhi 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1015f293a5e3Sdingzhi 			      &x->replay);
10161d1e34ddSDavid S. Miller 	if (ret)
10171d1e34ddSDavid S. Miller 		goto out;
1018d77e38e6SSteffen Klassert 	if(x->xso.dev)
1019d77e38e6SSteffen Klassert 		ret = copy_user_offload(&x->xso, skb);
1020d77e38e6SSteffen Klassert 	if (ret)
1021d77e38e6SSteffen Klassert 		goto out;
10227e652640SSteffen Klassert 	if (x->if_id) {
10237e652640SSteffen Klassert 		ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1024077fbac4SLorenzo Colitti 		if (ret)
1025077fbac4SLorenzo Colitti 			goto out;
1026077fbac4SLorenzo Colitti 	}
10278598112dSSteffen Klassert 	if (x->security)
10288598112dSSteffen Klassert 		ret = copy_sec_ctx(x->security, skb);
10291d1e34ddSDavid S. Miller out:
10301d1e34ddSDavid S. Miller 	return ret;
103168325d3bSHerbert Xu }
103268325d3bSHerbert Xu 
103368325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
103468325d3bSHerbert Xu {
103568325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
103668325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
103768325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
10385f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
103968325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
104068325d3bSHerbert Xu 	struct nlmsghdr *nlh;
104168325d3bSHerbert Xu 	int err;
104268325d3bSHerbert Xu 
104315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
104468325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
104568325d3bSHerbert Xu 	if (nlh == NULL)
104668325d3bSHerbert Xu 		return -EMSGSIZE;
104768325d3bSHerbert Xu 
104868325d3bSHerbert Xu 	p = nlmsg_data(nlh);
104968325d3bSHerbert Xu 
105068325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
10511d1e34ddSDavid S. Miller 	if (err) {
10529825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
105368325d3bSHerbert Xu 		return err;
10541da177e4SLinus Torvalds 	}
10551d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
10565f3eea6bSDmitry Safonov 
10575f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
10585f3eea6bSDmitry Safonov 	if (xtr) {
10595f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlh);
10605f3eea6bSDmitry Safonov 
10615f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
10625f3eea6bSDmitry Safonov 		if (err) {
10635f3eea6bSDmitry Safonov 			nlmsg_cancel(skb, nlh);
10645f3eea6bSDmitry Safonov 			return err;
10655f3eea6bSDmitry Safonov 		}
10665f3eea6bSDmitry Safonov 	}
10675f3eea6bSDmitry Safonov 
10681d1e34ddSDavid S. Miller 	return 0;
10691d1e34ddSDavid S. Miller }
10701da177e4SLinus Torvalds 
10714c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
10724c563f76STimo Teras {
10734c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1074283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
1075283bc9f3SFan Du 	struct net *net = sock_net(sk);
1076283bc9f3SFan Du 
10771ba5bf99SVegard Nossum 	if (cb->args[0])
1078283bc9f3SFan Du 		xfrm_state_walk_done(walk, net);
10794c563f76STimo Teras 	return 0;
10804c563f76STimo Teras }
10814c563f76STimo Teras 
10821da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
10831da177e4SLinus Torvalds {
1084fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
10854c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
10861da177e4SLinus Torvalds 	struct xfrm_dump_info info;
10871da177e4SLinus Torvalds 
10884c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
10894c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
10904c563f76STimo Teras 
10911da177e4SLinus Torvalds 	info.in_skb = cb->skb;
10921da177e4SLinus Torvalds 	info.out_skb = skb;
10931da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
10941da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
10954c563f76STimo Teras 
10964c563f76STimo Teras 	if (!cb->args[0]) {
1097d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
1098870a2df4SNicolas Dichtel 		struct xfrm_address_filter *filter = NULL;
1099d3623099SNicolas Dichtel 		u8 proto = 0;
1100d3623099SNicolas Dichtel 		int err;
1101d3623099SNicolas Dichtel 
11028cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
11038cb08174SJohannes Berg 					     xfrma_policy, cb->extack);
1104d3623099SNicolas Dichtel 		if (err < 0)
1105d3623099SNicolas Dichtel 			return err;
1106d3623099SNicolas Dichtel 
1107870a2df4SNicolas Dichtel 		if (attrs[XFRMA_ADDRESS_FILTER]) {
1108df367561SAndrzej Hajda 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1109df367561SAndrzej Hajda 					 sizeof(*filter), GFP_KERNEL);
1110d3623099SNicolas Dichtel 			if (filter == NULL)
1111d3623099SNicolas Dichtel 				return -ENOMEM;
1112d3623099SNicolas Dichtel 		}
1113d3623099SNicolas Dichtel 
1114d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
1115d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
1116d3623099SNicolas Dichtel 
1117d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
11181ba5bf99SVegard Nossum 		cb->args[0] = 1;
11194c563f76STimo Teras 	}
11204c563f76STimo Teras 
1121fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds 	return skb->len;
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
11271da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
11281da177e4SLinus Torvalds {
11291da177e4SLinus Torvalds 	struct xfrm_dump_info info;
11301da177e4SLinus Torvalds 	struct sk_buff *skb;
1131864745d2SMathias Krause 	int err;
11321da177e4SLinus Torvalds 
11337deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
11341da177e4SLinus Torvalds 	if (!skb)
11351da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	info.in_skb = in_skb;
11381da177e4SLinus Torvalds 	info.out_skb = skb;
11391da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
11401da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
11411da177e4SLinus Torvalds 
1142864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
1143864745d2SMathias Krause 	if (err) {
11441da177e4SLinus Torvalds 		kfree_skb(skb);
1145864745d2SMathias Krause 		return ERR_PTR(err);
11461da177e4SLinus Torvalds 	}
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	return skb;
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
115121ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
115221ee543eSMichal Kubecek  * Must be called with RCU read lock.
115321ee543eSMichal Kubecek  */
115421ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
115521ee543eSMichal Kubecek 				       u32 pid, unsigned int group)
115621ee543eSMichal Kubecek {
115721ee543eSMichal Kubecek 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
11585461fc0cSDmitry Safonov 	struct xfrm_translator *xtr;
115921ee543eSMichal Kubecek 
116086126b77SFlorian Westphal 	if (!nlsk) {
116186126b77SFlorian Westphal 		kfree_skb(skb);
116286126b77SFlorian Westphal 		return -EPIPE;
116386126b77SFlorian Westphal 	}
116486126b77SFlorian Westphal 
11655461fc0cSDmitry Safonov 	xtr = xfrm_get_translator();
11665461fc0cSDmitry Safonov 	if (xtr) {
11675461fc0cSDmitry Safonov 		int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
11685461fc0cSDmitry Safonov 
11695461fc0cSDmitry Safonov 		xfrm_put_translator(xtr);
11705461fc0cSDmitry Safonov 		if (err) {
11715461fc0cSDmitry Safonov 			kfree_skb(skb);
11725461fc0cSDmitry Safonov 			return err;
11735461fc0cSDmitry Safonov 		}
11745461fc0cSDmitry Safonov 	}
11755461fc0cSDmitry Safonov 
117621ee543eSMichal Kubecek 	return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
117721ee543eSMichal Kubecek }
117821ee543eSMichal Kubecek 
1179a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void)
11807deb2264SThomas Graf {
11817deb2264SThomas Graf 	return NLMSG_ALIGN(4)
11827deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1183880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1184880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1185880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
11867deb2264SThomas Graf }
11877deb2264SThomas Graf 
1188e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
118915e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
1190ecfd6b18SJamal Hadi Salim {
11915a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
11925a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
11935a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1194880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1195ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
11961d1e34ddSDavid S. Miller 	int err;
1197ecfd6b18SJamal Hadi Salim 	u32 *f;
1198880a6fabSChristophe Gouault 	unsigned lseq;
1199ecfd6b18SJamal Hadi Salim 
120015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
120125985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1202ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1203ecfd6b18SJamal Hadi Salim 
1204ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1205ecfd6b18SJamal Hadi Salim 	*f = flags;
1206e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
12075a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
12085a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
12095a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
12105a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
12115a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
12125a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
12135a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
12145a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1215ecfd6b18SJamal Hadi Salim 
1216880a6fabSChristophe Gouault 	do {
1217880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1218880a6fabSChristophe Gouault 
1219880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1220880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1221880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1222880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1223880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1224880a6fabSChristophe Gouault 
12251d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
12261d1e34ddSDavid S. Miller 	if (!err)
12271d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1228880a6fabSChristophe Gouault 	if (!err)
1229880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1230880a6fabSChristophe Gouault 	if (!err)
1231880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
12321d1e34ddSDavid S. Miller 	if (err) {
12331d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
12341d1e34ddSDavid S. Miller 		return err;
12351d1e34ddSDavid S. Miller 	}
1236ecfd6b18SJamal Hadi Salim 
1237053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1238053c095aSJohannes Berg 	return 0;
1239ecfd6b18SJamal Hadi Salim }
1240ecfd6b18SJamal Hadi Salim 
1241880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1242880a6fabSChristophe Gouault 			    struct nlattr **attrs)
1243880a6fabSChristophe Gouault {
1244880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1245880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1246880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1247880a6fabSChristophe Gouault 
1248880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1249880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1250880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1251880a6fabSChristophe Gouault 
1252880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1253880a6fabSChristophe Gouault 			return -EINVAL;
1254880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1255880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1256880a6fabSChristophe Gouault 			return -EINVAL;
1257880a6fabSChristophe Gouault 	}
1258880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1259880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1260880a6fabSChristophe Gouault 
1261880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1262880a6fabSChristophe Gouault 			return -EINVAL;
1263880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1264880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1265880a6fabSChristophe Gouault 			return -EINVAL;
1266880a6fabSChristophe Gouault 	}
1267880a6fabSChristophe Gouault 
1268880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1269880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1270880a6fabSChristophe Gouault 		if (thresh4) {
1271880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1272880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1273880a6fabSChristophe Gouault 		}
1274880a6fabSChristophe Gouault 		if (thresh6) {
1275880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1276880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1277880a6fabSChristophe Gouault 		}
1278880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1279880a6fabSChristophe Gouault 
1280880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1281880a6fabSChristophe Gouault 	}
1282880a6fabSChristophe Gouault 
1283880a6fabSChristophe Gouault 	return 0;
1284880a6fabSChristophe Gouault }
1285880a6fabSChristophe Gouault 
1286ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12875424f32eSThomas Graf 		struct nlattr **attrs)
1288ecfd6b18SJamal Hadi Salim {
1289a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1290ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
12917b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
129215e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1293ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
12942fc5f83bSGustavo A. R. Silva 	int err;
1295ecfd6b18SJamal Hadi Salim 
12967deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1297ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1298ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1299ecfd6b18SJamal Hadi Salim 
13002fc5f83bSGustavo A. R. Silva 	err = build_spdinfo(r_skb, net, sportid, seq, *flags);
13012fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
1302ecfd6b18SJamal Hadi Salim 
130315e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1304ecfd6b18SJamal Hadi Salim }
1305ecfd6b18SJamal Hadi Salim 
1306a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void)
13077deb2264SThomas Graf {
13087deb2264SThomas Graf 	return NLMSG_ALIGN(4)
13097deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
13107deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
13117deb2264SThomas Graf }
13127deb2264SThomas Graf 
1313e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
131415e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
131528d8909bSJamal Hadi Salim {
1316af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1317af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
131828d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
13191d1e34ddSDavid S. Miller 	int err;
132028d8909bSJamal Hadi Salim 	u32 *f;
132128d8909bSJamal Hadi Salim 
132215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
132325985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
132428d8909bSJamal Hadi Salim 		return -EMSGSIZE;
132528d8909bSJamal Hadi Salim 
132628d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
132728d8909bSJamal Hadi Salim 	*f = flags;
1328e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
132928d8909bSJamal Hadi Salim 
1330af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1331af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1332af11e316SJamal Hadi Salim 
13331d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
13341d1e34ddSDavid S. Miller 	if (!err)
13351d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
13361d1e34ddSDavid S. Miller 	if (err) {
13371d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
13381d1e34ddSDavid S. Miller 		return err;
13391d1e34ddSDavid S. Miller 	}
134028d8909bSJamal Hadi Salim 
1341053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1342053c095aSJohannes Berg 	return 0;
134328d8909bSJamal Hadi Salim }
134428d8909bSJamal Hadi Salim 
134528d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
13465424f32eSThomas Graf 		struct nlattr **attrs)
134728d8909bSJamal Hadi Salim {
1348a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
134928d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
13507b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
135115e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
135228d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
13532fc5f83bSGustavo A. R. Silva 	int err;
135428d8909bSJamal Hadi Salim 
13557deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
135628d8909bSJamal Hadi Salim 	if (r_skb == NULL)
135728d8909bSJamal Hadi Salim 		return -ENOMEM;
135828d8909bSJamal Hadi Salim 
13592fc5f83bSGustavo A. R. Silva 	err = build_sadinfo(r_skb, net, sportid, seq, *flags);
13602fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
136128d8909bSJamal Hadi Salim 
136215e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
136328d8909bSJamal Hadi Salim }
136428d8909bSJamal Hadi Salim 
136522e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
13665424f32eSThomas Graf 		struct nlattr **attrs)
13671da177e4SLinus Torvalds {
1368fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
13697b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
13701da177e4SLinus Torvalds 	struct xfrm_state *x;
13711da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1372eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
13731da177e4SLinus Torvalds 
1374fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
13751da177e4SLinus Torvalds 	if (x == NULL)
13761da177e4SLinus Torvalds 		goto out_noput;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
13791da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
13801da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
13811da177e4SLinus Torvalds 	} else {
138215e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
13831da177e4SLinus Torvalds 	}
13841da177e4SLinus Torvalds 	xfrm_state_put(x);
13851da177e4SLinus Torvalds out_noput:
13861da177e4SLinus Torvalds 	return err;
13871da177e4SLinus Torvalds }
13881da177e4SLinus Torvalds 
138922e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
13905424f32eSThomas Graf 		struct nlattr **attrs)
13911da177e4SLinus Torvalds {
1392fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
13931da177e4SLinus Torvalds 	struct xfrm_state *x;
13941da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
13955f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
13961da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
13971da177e4SLinus Torvalds 	xfrm_address_t *daddr;
13981da177e4SLinus Torvalds 	int family;
13991da177e4SLinus Torvalds 	int err;
14006f26b61eSJamal Hadi Salim 	u32 mark;
14016f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
14027e652640SSteffen Klassert 	u32 if_id = 0;
14031da177e4SLinus Torvalds 
14047b67c857SThomas Graf 	p = nlmsg_data(nlh);
1405776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
14061da177e4SLinus Torvalds 	if (err)
14071da177e4SLinus Torvalds 		goto out_noput;
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	family = p->info.family;
14101da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	x = NULL;
14136f26b61eSJamal Hadi Salim 
14146f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
14157e652640SSteffen Klassert 
14167e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
14177e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
14187e652640SSteffen Klassert 
14191da177e4SLinus Torvalds 	if (p->info.seq) {
14206f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
142170e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
14221da177e4SLinus Torvalds 			xfrm_state_put(x);
14231da177e4SLinus Torvalds 			x = NULL;
14241da177e4SLinus Torvalds 		}
14251da177e4SLinus Torvalds 	}
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	if (!x)
14286f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
14297e652640SSteffen Klassert 				  if_id, p->info.id.proto, daddr,
14301da177e4SLinus Torvalds 				  &p->info.saddr, 1,
14311da177e4SLinus Torvalds 				  family);
14321da177e4SLinus Torvalds 	err = -ENOENT;
14331da177e4SLinus Torvalds 	if (x == NULL)
14341da177e4SLinus Torvalds 		goto out_noput;
14351da177e4SLinus Torvalds 
1436658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1437658b219eSHerbert Xu 	if (err)
1438658b219eSHerbert Xu 		goto out;
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
14411da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
14421da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
14431da177e4SLinus Torvalds 		goto out;
14441da177e4SLinus Torvalds 	}
14451da177e4SLinus Torvalds 
14465f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
14475f3eea6bSDmitry Safonov 	if (xtr) {
14485f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
14495f3eea6bSDmitry Safonov 
14505f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
14515f3eea6bSDmitry Safonov 		if (err) {
14525f3eea6bSDmitry Safonov 			kfree_skb(resp_skb);
14535f3eea6bSDmitry Safonov 			goto out;
14545f3eea6bSDmitry Safonov 		}
14555f3eea6bSDmitry Safonov 	}
14565f3eea6bSDmitry Safonov 
145715e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds out:
14601da177e4SLinus Torvalds 	xfrm_state_put(x);
14611da177e4SLinus Torvalds out_noput:
14621da177e4SLinus Torvalds 	return err;
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds 
1465b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
14661da177e4SLinus Torvalds {
14671da177e4SLinus Torvalds 	switch (dir) {
14681da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
14691da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
14701da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
14711da177e4SLinus Torvalds 		break;
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 	default:
14741da177e4SLinus Torvalds 		return -EINVAL;
14753ff50b79SStephen Hemminger 	}
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	return 0;
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
1480b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1481f7b6983fSMasahide NAKAMURA {
1482f7b6983fSMasahide NAKAMURA 	switch (type) {
1483f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1484f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1485f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1486f7b6983fSMasahide NAKAMURA #endif
1487f7b6983fSMasahide NAKAMURA 		break;
1488f7b6983fSMasahide NAKAMURA 
1489f7b6983fSMasahide NAKAMURA 	default:
1490f7b6983fSMasahide NAKAMURA 		return -EINVAL;
14913ff50b79SStephen Hemminger 	}
1492f7b6983fSMasahide NAKAMURA 
1493f7b6983fSMasahide NAKAMURA 	return 0;
1494f7b6983fSMasahide NAKAMURA }
1495f7b6983fSMasahide NAKAMURA 
14961da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
14971da177e4SLinus Torvalds {
1498e682adf0SFan Du 	int ret;
1499e682adf0SFan Du 
15001da177e4SLinus Torvalds 	switch (p->share) {
15011da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
15021da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
15031da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
15041da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
15051da177e4SLinus Torvalds 		break;
15061da177e4SLinus Torvalds 
15071da177e4SLinus Torvalds 	default:
15081da177e4SLinus Torvalds 		return -EINVAL;
15093ff50b79SStephen Hemminger 	}
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	switch (p->action) {
15121da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
15131da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
15141da177e4SLinus Torvalds 		break;
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds 	default:
15171da177e4SLinus Torvalds 		return -EINVAL;
15183ff50b79SStephen Hemminger 	}
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	switch (p->sel.family) {
15211da177e4SLinus Torvalds 	case AF_INET:
152207bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
152307bf7908SSteffen Klassert 			return -EINVAL;
152407bf7908SSteffen Klassert 
15251da177e4SLinus Torvalds 		break;
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	case AF_INET6:
1528dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
152907bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
153007bf7908SSteffen Klassert 			return -EINVAL;
153107bf7908SSteffen Klassert 
15321da177e4SLinus Torvalds 		break;
15331da177e4SLinus Torvalds #else
15341da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
15351da177e4SLinus Torvalds #endif
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	default:
15381da177e4SLinus Torvalds 		return -EINVAL;
15393ff50b79SStephen Hemminger 	}
15401da177e4SLinus Torvalds 
1541e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1542e682adf0SFan Du 	if (ret)
1543e682adf0SFan Du 		return ret;
1544b805d78dSYueHaibing 	if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1545e682adf0SFan Du 		return -EINVAL;
1546e682adf0SFan Du 
1547e682adf0SFan Du 	return 0;
15481da177e4SLinus Torvalds }
15491da177e4SLinus Torvalds 
15505424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1551df71837dSTrent Jaeger {
15525424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1553df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1554df71837dSTrent Jaeger 
1555df71837dSTrent Jaeger 	if (!rt)
1556df71837dSTrent Jaeger 		return 0;
1557df71837dSTrent Jaeger 
15585424f32eSThomas Graf 	uctx = nla_data(rt);
155952a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1560df71837dSTrent Jaeger }
1561df71837dSTrent Jaeger 
15621da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
15631da177e4SLinus Torvalds 			   int nr)
15641da177e4SLinus Torvalds {
15651da177e4SLinus Torvalds 	int i;
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
15681da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
15691da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
15721da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
15731da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
15741da177e4SLinus Torvalds 		t->reqid = ut->reqid;
15751da177e4SLinus Torvalds 		t->mode = ut->mode;
15761da177e4SLinus Torvalds 		t->share = ut->share;
15771da177e4SLinus Torvalds 		t->optional = ut->optional;
15781da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
15791da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
15801da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1581c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1582c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
15838511d01dSMiika Komu 		t->encap_family = ut->family;
15841da177e4SLinus Torvalds 	}
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds 
1587b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1588b4ad86bfSDavid S. Miller {
1589732706afSSteffen Klassert 	u16 prev_family;
1590b4ad86bfSDavid S. Miller 	int i;
1591b4ad86bfSDavid S. Miller 
1592b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1593b4ad86bfSDavid S. Miller 		return -EINVAL;
1594b4ad86bfSDavid S. Miller 
1595732706afSSteffen Klassert 	prev_family = family;
1596732706afSSteffen Klassert 
1597b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1598b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1599b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1600b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1601b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1602b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1603b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1604b4ad86bfSDavid S. Miller 		 */
1605b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1606b4ad86bfSDavid S. Miller 			ut[i].family = family;
1607b4ad86bfSDavid S. Miller 
160835e61038SFlorian Westphal 		switch (ut[i].mode) {
160935e61038SFlorian Westphal 		case XFRM_MODE_TUNNEL:
161035e61038SFlorian Westphal 		case XFRM_MODE_BEET:
161135e61038SFlorian Westphal 			break;
161235e61038SFlorian Westphal 		default:
161335e61038SFlorian Westphal 			if (ut[i].family != prev_family)
1614732706afSSteffen Klassert 				return -EINVAL;
161535e61038SFlorian Westphal 			break;
161635e61038SFlorian Westphal 		}
161732bf94fbSSean Tranchetti 		if (ut[i].mode >= XFRM_MODE_MAX)
161832bf94fbSSean Tranchetti 			return -EINVAL;
161932bf94fbSSean Tranchetti 
1620732706afSSteffen Klassert 		prev_family = ut[i].family;
1621732706afSSteffen Klassert 
1622b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1623b4ad86bfSDavid S. Miller 		case AF_INET:
1624b4ad86bfSDavid S. Miller 			break;
1625dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1626b4ad86bfSDavid S. Miller 		case AF_INET6:
1627b4ad86bfSDavid S. Miller 			break;
1628b4ad86bfSDavid S. Miller #endif
1629b4ad86bfSDavid S. Miller 		default:
1630b4ad86bfSDavid S. Miller 			return -EINVAL;
16313ff50b79SStephen Hemminger 		}
16326a53b759SCong Wang 
1633dbb2483bSCong Wang 		if (!xfrm_id_proto_valid(ut[i].id.proto))
16346a53b759SCong Wang 			return -EINVAL;
16356a53b759SCong Wang 	}
16366a53b759SCong Wang 
1637b4ad86bfSDavid S. Miller 	return 0;
1638b4ad86bfSDavid S. Miller }
1639b4ad86bfSDavid S. Miller 
16405424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
16411da177e4SLinus Torvalds {
16425424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	if (!rt) {
16451da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
16461da177e4SLinus Torvalds 	} else {
16475424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
16485424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1649b4ad86bfSDavid S. Miller 		int err;
16501da177e4SLinus Torvalds 
1651b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1652b4ad86bfSDavid S. Miller 		if (err)
1653b4ad86bfSDavid S. Miller 			return err;
16541da177e4SLinus Torvalds 
16555424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
16561da177e4SLinus Torvalds 	}
16571da177e4SLinus Torvalds 	return 0;
16581da177e4SLinus Torvalds }
16591da177e4SLinus Torvalds 
16605424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1661f7b6983fSMasahide NAKAMURA {
16625424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1663f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1664b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1665f7b6983fSMasahide NAKAMURA 	int err;
1666f7b6983fSMasahide NAKAMURA 
1667f7b6983fSMasahide NAKAMURA 	if (rt) {
16685424f32eSThomas Graf 		upt = nla_data(rt);
1669f7b6983fSMasahide NAKAMURA 		type = upt->type;
1670f7b6983fSMasahide NAKAMURA 	}
1671f7b6983fSMasahide NAKAMURA 
1672f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1673f7b6983fSMasahide NAKAMURA 	if (err)
1674f7b6983fSMasahide NAKAMURA 		return err;
1675f7b6983fSMasahide NAKAMURA 
1676f7b6983fSMasahide NAKAMURA 	*tp = type;
1677f7b6983fSMasahide NAKAMURA 	return 0;
1678f7b6983fSMasahide NAKAMURA }
1679f7b6983fSMasahide NAKAMURA 
16801da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
16811da177e4SLinus Torvalds {
16821da177e4SLinus Torvalds 	xp->priority = p->priority;
16831da177e4SLinus Torvalds 	xp->index = p->index;
16841da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
16851da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
16861da177e4SLinus Torvalds 	xp->action = p->action;
16871da177e4SLinus Torvalds 	xp->flags = p->flags;
16881da177e4SLinus Torvalds 	xp->family = p->sel.family;
16891da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
16901da177e4SLinus Torvalds }
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
16931da177e4SLinus Torvalds {
16947b789836SMathias Krause 	memset(p, 0, sizeof(*p));
16951da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
16961da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
16971da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
16981da177e4SLinus Torvalds 	p->priority = xp->priority;
16991da177e4SLinus Torvalds 	p->index = xp->index;
17001da177e4SLinus Torvalds 	p->sel.family = xp->family;
17011da177e4SLinus Torvalds 	p->dir = dir;
17021da177e4SLinus Torvalds 	p->action = xp->action;
17031da177e4SLinus Torvalds 	p->flags = xp->flags;
17041da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
17051da177e4SLinus Torvalds }
17061da177e4SLinus Torvalds 
1707fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
17081da177e4SLinus Torvalds {
1709fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
17101da177e4SLinus Torvalds 	int err;
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	if (!xp) {
17131da177e4SLinus Torvalds 		*errp = -ENOMEM;
17141da177e4SLinus Torvalds 		return NULL;
17151da177e4SLinus Torvalds 	}
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1718df71837dSTrent Jaeger 
171935a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1720f7b6983fSMasahide NAKAMURA 	if (err)
1721f7b6983fSMasahide NAKAMURA 		goto error;
1722f7b6983fSMasahide NAKAMURA 
172335a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
172435a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1725f7b6983fSMasahide NAKAMURA 	if (err)
1726f7b6983fSMasahide NAKAMURA 		goto error;
17271da177e4SLinus Torvalds 
1728295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1729295fae56SJamal Hadi Salim 
17307e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
17317e652640SSteffen Klassert 		xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
17327e652640SSteffen Klassert 
17331da177e4SLinus Torvalds 	return xp;
1734f7b6983fSMasahide NAKAMURA  error:
1735f7b6983fSMasahide NAKAMURA 	*errp = err;
173612a169e7SHerbert Xu 	xp->walk.dead = 1;
173764c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1738f7b6983fSMasahide NAKAMURA 	return NULL;
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
174122e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
17425424f32eSThomas Graf 		struct nlattr **attrs)
17431da177e4SLinus Torvalds {
1744fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
17457b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
17461da177e4SLinus Torvalds 	struct xfrm_policy *xp;
174726b15dadSJamal Hadi Salim 	struct km_event c;
17481da177e4SLinus Torvalds 	int err;
17491da177e4SLinus Torvalds 	int excl;
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
17521da177e4SLinus Torvalds 	if (err)
17531da177e4SLinus Torvalds 		return err;
175435a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1755df71837dSTrent Jaeger 	if (err)
1756df71837dSTrent Jaeger 		return err;
17571da177e4SLinus Torvalds 
1758fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
17591da177e4SLinus Torvalds 	if (!xp)
17601da177e4SLinus Torvalds 		return err;
17611da177e4SLinus Torvalds 
176225985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
176326b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
1764*a7fd0e6dSBhaskar Chowdhury 	 * in netlink excl is a flag and you wouldn't need
176526b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
17661da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
17671da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
17682e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1769161a09e7SJoy Latten 
17701da177e4SLinus Torvalds 	if (err) {
177103e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
17721da177e4SLinus Torvalds 		kfree(xp);
17731da177e4SLinus Torvalds 		return err;
17741da177e4SLinus Torvalds 	}
17751da177e4SLinus Torvalds 
1776f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
177726b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
177815e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
177926b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
178026b15dadSJamal Hadi Salim 
17811da177e4SLinus Torvalds 	xfrm_pol_put(xp);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	return 0;
17841da177e4SLinus Torvalds }
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
17871da177e4SLinus Torvalds {
17881da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
17891da177e4SLinus Torvalds 	int i;
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
17921da177e4SLinus Torvalds 		return 0;
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
17951da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
17961da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
17971da177e4SLinus Torvalds 
17981f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
17991da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
18008511d01dSMiika Komu 		up->family = kp->encap_family;
18011da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
18021da177e4SLinus Torvalds 		up->reqid = kp->reqid;
18031da177e4SLinus Torvalds 		up->mode = kp->mode;
18041da177e4SLinus Torvalds 		up->share = kp->share;
18051da177e4SLinus Torvalds 		up->optional = kp->optional;
18061da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
18071da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
18081da177e4SLinus Torvalds 		up->calgos = kp->calgos;
18091da177e4SLinus Torvalds 	}
18101da177e4SLinus Torvalds 
1811c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1812c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1813df71837dSTrent Jaeger }
1814df71837dSTrent Jaeger 
18150d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
18160d681623SSerge Hallyn {
18170d681623SSerge Hallyn 	if (x->security) {
18180d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
18190d681623SSerge Hallyn 	}
18200d681623SSerge Hallyn 	return 0;
18210d681623SSerge Hallyn }
18220d681623SSerge Hallyn 
18230d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
18240d681623SSerge Hallyn {
18251d1e34ddSDavid S. Miller 	if (xp->security)
18260d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
18270d681623SSerge Hallyn 	return 0;
18280d681623SSerge Hallyn }
1829a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void)
1830cfbfd45aSThomas Graf {
1831cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1832cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1833cfbfd45aSThomas Graf #else
1834cfbfd45aSThomas Graf 	return 0;
1835cfbfd45aSThomas Graf #endif
1836cfbfd45aSThomas Graf }
18370d681623SSerge Hallyn 
1838f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1839b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1840f7b6983fSMasahide NAKAMURA {
184145c180bcSEric Dumazet 	struct xfrm_userpolicy_type upt;
184245c180bcSEric Dumazet 
184345c180bcSEric Dumazet 	/* Sadly there are two holes in struct xfrm_userpolicy_type */
184445c180bcSEric Dumazet 	memset(&upt, 0, sizeof(upt));
184545c180bcSEric Dumazet 	upt.type = type;
1846f7b6983fSMasahide NAKAMURA 
1847c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1848f7b6983fSMasahide NAKAMURA }
1849f7b6983fSMasahide NAKAMURA 
1850f7b6983fSMasahide NAKAMURA #else
1851b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1852f7b6983fSMasahide NAKAMURA {
1853f7b6983fSMasahide NAKAMURA 	return 0;
1854f7b6983fSMasahide NAKAMURA }
1855f7b6983fSMasahide NAKAMURA #endif
1856f7b6983fSMasahide NAKAMURA 
18571da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
18581da177e4SLinus Torvalds {
18591da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
18601da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
18611da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
18621da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
18635f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
18641da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
18651d1e34ddSDavid S. Miller 	int err;
18661da177e4SLinus Torvalds 
186715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
186879b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
186979b8b7f4SThomas Graf 	if (nlh == NULL)
187079b8b7f4SThomas Graf 		return -EMSGSIZE;
18711da177e4SLinus Torvalds 
18727b67c857SThomas Graf 	p = nlmsg_data(nlh);
18731da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
18741d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
18751d1e34ddSDavid S. Miller 	if (!err)
18761d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
18771d1e34ddSDavid S. Miller 	if (!err)
18781d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
18791d1e34ddSDavid S. Miller 	if (!err)
18801d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
18817e652640SSteffen Klassert 	if (!err)
18827e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
18831d1e34ddSDavid S. Miller 	if (err) {
18841d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
18851d1e34ddSDavid S. Miller 		return err;
18861d1e34ddSDavid S. Miller 	}
18879825069dSThomas Graf 	nlmsg_end(skb, nlh);
18885f3eea6bSDmitry Safonov 
18895f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
18905f3eea6bSDmitry Safonov 	if (xtr) {
18915f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlh);
18925f3eea6bSDmitry Safonov 
18935f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
18945f3eea6bSDmitry Safonov 		if (err) {
18955f3eea6bSDmitry Safonov 			nlmsg_cancel(skb, nlh);
18965f3eea6bSDmitry Safonov 			return err;
18975f3eea6bSDmitry Safonov 		}
18985f3eea6bSDmitry Safonov 	}
18995f3eea6bSDmitry Safonov 
19001da177e4SLinus Torvalds 	return 0;
19011da177e4SLinus Torvalds }
19021da177e4SLinus Torvalds 
19034c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
19044c563f76STimo Teras {
19051137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1906283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
19074c563f76STimo Teras 
1908283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
19094c563f76STimo Teras 	return 0;
19104c563f76STimo Teras }
19114c563f76STimo Teras 
19121137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb)
19131137b5e2SHerbert Xu {
19141137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
19151137b5e2SHerbert Xu 
19161137b5e2SHerbert Xu 	BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
19171137b5e2SHerbert Xu 
19181137b5e2SHerbert Xu 	xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
19191137b5e2SHerbert Xu 	return 0;
19201137b5e2SHerbert Xu }
19211137b5e2SHerbert Xu 
19221da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
19231da177e4SLinus Torvalds {
1924fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
19251137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
19261da177e4SLinus Torvalds 	struct xfrm_dump_info info;
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds 	info.in_skb = cb->skb;
19291da177e4SLinus Torvalds 	info.out_skb = skb;
19301da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
19311da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
19324c563f76STimo Teras 
1933fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	return skb->len;
19361da177e4SLinus Torvalds }
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
19391da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
19401da177e4SLinus Torvalds 					  int dir, u32 seq)
19411da177e4SLinus Torvalds {
19421da177e4SLinus Torvalds 	struct xfrm_dump_info info;
19431da177e4SLinus Torvalds 	struct sk_buff *skb;
1944c2546372SMathias Krause 	int err;
19451da177e4SLinus Torvalds 
19467deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19471da177e4SLinus Torvalds 	if (!skb)
19481da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	info.in_skb = in_skb;
19511da177e4SLinus Torvalds 	info.out_skb = skb;
19521da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
19531da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
19541da177e4SLinus Torvalds 
1955c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1956c2546372SMathias Krause 	if (err) {
19571da177e4SLinus Torvalds 		kfree_skb(skb);
1958c2546372SMathias Krause 		return ERR_PTR(err);
19591da177e4SLinus Torvalds 	}
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	return skb;
19621da177e4SLinus Torvalds }
19631da177e4SLinus Torvalds 
196422e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
19655424f32eSThomas Graf 		struct nlattr **attrs)
19661da177e4SLinus Torvalds {
1967fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
19681da177e4SLinus Torvalds 	struct xfrm_policy *xp;
19691da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1970b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
19711da177e4SLinus Torvalds 	int err;
197226b15dadSJamal Hadi Salim 	struct km_event c;
19731da177e4SLinus Torvalds 	int delete;
1974295fae56SJamal Hadi Salim 	struct xfrm_mark m;
19757e652640SSteffen Klassert 	u32 if_id = 0;
19761da177e4SLinus Torvalds 
19777b67c857SThomas Graf 	p = nlmsg_data(nlh);
19781da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
19791da177e4SLinus Torvalds 
198035a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1981f7b6983fSMasahide NAKAMURA 	if (err)
1982f7b6983fSMasahide NAKAMURA 		return err;
1983f7b6983fSMasahide NAKAMURA 
19841da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
19851da177e4SLinus Torvalds 	if (err)
19861da177e4SLinus Torvalds 		return err;
19871da177e4SLinus Torvalds 
19887e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
19897e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
19907e652640SSteffen Klassert 
19914f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
19924f47e8abSXin Long 
19931da177e4SLinus Torvalds 	if (p->index)
19944f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
19954f47e8abSXin Long 				      p->index, delete, &err);
1996df71837dSTrent Jaeger 	else {
19975424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
199803e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1999df71837dSTrent Jaeger 
200035a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
2001df71837dSTrent Jaeger 		if (err)
2002df71837dSTrent Jaeger 			return err;
2003df71837dSTrent Jaeger 
20042c8dd116SDenis V. Lunev 		ctx = NULL;
2005df71837dSTrent Jaeger 		if (rt) {
20065424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2007df71837dSTrent Jaeger 
200852a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
200903e1ad7bSPaul Moore 			if (err)
2010df71837dSTrent Jaeger 				return err;
20112c8dd116SDenis V. Lunev 		}
20124f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
20134f47e8abSXin Long 					   &p->sel, ctx, delete, &err);
201403e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
2015df71837dSTrent Jaeger 	}
20161da177e4SLinus Torvalds 	if (xp == NULL)
20171da177e4SLinus Torvalds 		return -ENOENT;
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 	if (!delete) {
20201da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
20231da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
20241da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
20251da177e4SLinus Torvalds 		} else {
2026a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
202715e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
20281da177e4SLinus Torvalds 		}
202926b15dadSJamal Hadi Salim 	} else {
20302e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
203113fcfbb0SDavid S. Miller 
203213fcfbb0SDavid S. Miller 		if (err != 0)
2033c8c05a8eSCatherine Zhang 			goto out;
203413fcfbb0SDavid S. Miller 
2035e7443892SHerbert Xu 		c.data.byid = p->index;
2036f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
203726b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
203815e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
203926b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
20401da177e4SLinus Torvalds 	}
20411da177e4SLinus Torvalds 
2042c8c05a8eSCatherine Zhang out:
2043ef41aaa0SEric Paris 	xfrm_pol_put(xp);
20441da177e4SLinus Torvalds 	return err;
20451da177e4SLinus Torvalds }
20461da177e4SLinus Torvalds 
204722e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
20485424f32eSThomas Graf 		struct nlattr **attrs)
20491da177e4SLinus Torvalds {
2050fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
205126b15dadSJamal Hadi Salim 	struct km_event c;
20527b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
20534aa2e62cSJoy Latten 	int err;
20541da177e4SLinus Torvalds 
2055f75a2804SCong Wang 	err = xfrm_state_flush(net, p->proto, true, false);
20569e64cc95SJamal Hadi Salim 	if (err) {
20579e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
20589e64cc95SJamal Hadi Salim 			return 0;
2059069c474eSDavid S. Miller 		return err;
20609e64cc95SJamal Hadi Salim 	}
2061bf08867fSHerbert Xu 	c.data.proto = p->proto;
2062f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
206326b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
206415e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
20657067802eSAlexey Dobriyan 	c.net = net;
206626b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
206726b15dadSJamal Hadi Salim 
20681da177e4SLinus Torvalds 	return 0;
20691da177e4SLinus Torvalds }
20701da177e4SLinus Torvalds 
2071a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
20727deb2264SThomas Graf {
2073a1b831f2SAlexey Dobriyan 	unsigned int replay_size = x->replay_esn ?
2074d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
2075d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
2076d8647b79SSteffen Klassert 
20777deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2078d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
2079de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
20806f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
20817deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
20827deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
20837deb2264SThomas Graf }
2084d51d081dSJamal Hadi Salim 
2085214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2086d51d081dSJamal Hadi Salim {
2087d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
2088d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
20891d1e34ddSDavid S. Miller 	int err;
2090d51d081dSJamal Hadi Salim 
209115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
209279b8b7f4SThomas Graf 	if (nlh == NULL)
209379b8b7f4SThomas Graf 		return -EMSGSIZE;
2094d51d081dSJamal Hadi Salim 
20957b67c857SThomas Graf 	id = nlmsg_data(nlh);
2096931e79d7SMathias Krause 	memset(&id->sa_id, 0, sizeof(id->sa_id));
20972b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2098d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
2099d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
2100d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
21012b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
21022b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
2103d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
2104d51d081dSJamal Hadi Salim 
2105d0fde795SDavid S. Miller 	if (x->replay_esn) {
21061d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2107d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
21081d1e34ddSDavid S. Miller 			      x->replay_esn);
2109d0fde795SDavid S. Miller 	} else {
21101d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
21111d1e34ddSDavid S. Miller 			      &x->replay);
2112d0fde795SDavid S. Miller 	}
21131d1e34ddSDavid S. Miller 	if (err)
21141d1e34ddSDavid S. Miller 		goto out_cancel;
2115de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2116de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
21171d1e34ddSDavid S. Miller 	if (err)
21181d1e34ddSDavid S. Miller 		goto out_cancel;
2119d8647b79SSteffen Klassert 
21201d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
21211d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
21221d1e34ddSDavid S. Miller 		if (err)
21231d1e34ddSDavid S. Miller 			goto out_cancel;
21241d1e34ddSDavid S. Miller 	}
21251d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
21261d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
21271d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
21281d1e34ddSDavid S. Miller 		if (err)
21291d1e34ddSDavid S. Miller 			goto out_cancel;
21301d1e34ddSDavid S. Miller 	}
21311d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
21321d1e34ddSDavid S. Miller 	if (err)
21331d1e34ddSDavid S. Miller 		goto out_cancel;
21346f26b61eSJamal Hadi Salim 
21357e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
21367e652640SSteffen Klassert 	if (err)
21377e652640SSteffen Klassert 		goto out_cancel;
21387e652640SSteffen Klassert 
2139053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2140053c095aSJohannes Berg 	return 0;
2141d51d081dSJamal Hadi Salim 
21421d1e34ddSDavid S. Miller out_cancel:
21439825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
21441d1e34ddSDavid S. Miller 	return err;
2145d51d081dSJamal Hadi Salim }
2146d51d081dSJamal Hadi Salim 
214722e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
21485424f32eSThomas Graf 		struct nlattr **attrs)
2149d51d081dSJamal Hadi Salim {
2150fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2151d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2152d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
2153d51d081dSJamal Hadi Salim 	int err;
2154d51d081dSJamal Hadi Salim 	struct km_event c;
21556f26b61eSJamal Hadi Salim 	u32 mark;
21566f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
21577b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
2158d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
2159d51d081dSJamal Hadi Salim 
21606f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
21616f26b61eSJamal Hadi Salim 
21626f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2163d8647b79SSteffen Klassert 	if (x == NULL)
2164d51d081dSJamal Hadi Salim 		return -ESRCH;
2165d8647b79SSteffen Klassert 
2166d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2167d8647b79SSteffen Klassert 	if (r_skb == NULL) {
2168d8647b79SSteffen Klassert 		xfrm_state_put(x);
2169d8647b79SSteffen Klassert 		return -ENOMEM;
2170d51d081dSJamal Hadi Salim 	}
2171d51d081dSJamal Hadi Salim 
2172d51d081dSJamal Hadi Salim 	/*
2173d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
2174d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
2175d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
2176d51d081dSJamal Hadi Salim 	*/
2177d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2178d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
2179d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
218015e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2181d51d081dSJamal Hadi Salim 
21822fc5f83bSGustavo A. R. Silva 	err = build_aevent(r_skb, x, &c);
21832fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
21842fc5f83bSGustavo A. R. Silva 
218515e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2186d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2187d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2188d51d081dSJamal Hadi Salim 	return err;
2189d51d081dSJamal Hadi Salim }
2190d51d081dSJamal Hadi Salim 
219122e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
21925424f32eSThomas Graf 		struct nlattr **attrs)
2193d51d081dSJamal Hadi Salim {
2194fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2195d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2196d51d081dSJamal Hadi Salim 	struct km_event c;
2197d51d081dSJamal Hadi Salim 	int err = -EINVAL;
21986f26b61eSJamal Hadi Salim 	u32 mark = 0;
21996f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
22007b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
22015424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2202d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
22035424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
22044e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
22054e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2206d51d081dSJamal Hadi Salim 
22074e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
2208d51d081dSJamal Hadi Salim 		return err;
2209d51d081dSJamal Hadi Salim 
2210d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
2211d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2212d51d081dSJamal Hadi Salim 		return err;
2213d51d081dSJamal Hadi Salim 
22146f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
22156f26b61eSJamal Hadi Salim 
22166f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2217d51d081dSJamal Hadi Salim 	if (x == NULL)
2218d51d081dSJamal Hadi Salim 		return -ESRCH;
2219d51d081dSJamal Hadi Salim 
2220d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
2221d51d081dSJamal Hadi Salim 		goto out;
2222d51d081dSJamal Hadi Salim 
22234479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
2224e2b19125SSteffen Klassert 	if (err)
2225e2b19125SSteffen Klassert 		goto out;
2226e2b19125SSteffen Klassert 
2227d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2228e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
2229d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2230d51d081dSJamal Hadi Salim 
2231d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
2232d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
223315e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2234d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
2235d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
2236d51d081dSJamal Hadi Salim 	err = 0;
2237d51d081dSJamal Hadi Salim out:
2238d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2239d51d081dSJamal Hadi Salim 	return err;
2240d51d081dSJamal Hadi Salim }
2241d51d081dSJamal Hadi Salim 
224222e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
22435424f32eSThomas Graf 		struct nlattr **attrs)
22441da177e4SLinus Torvalds {
2245fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
224626b15dadSJamal Hadi Salim 	struct km_event c;
2247b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
2248f7b6983fSMasahide NAKAMURA 	int err;
224926b15dadSJamal Hadi Salim 
225035a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2251f7b6983fSMasahide NAKAMURA 	if (err)
2252f7b6983fSMasahide NAKAMURA 		return err;
2253f7b6983fSMasahide NAKAMURA 
22542e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
22552f1eb65fSJamal Hadi Salim 	if (err) {
22562f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
22572f1eb65fSJamal Hadi Salim 			return 0;
2258069c474eSDavid S. Miller 		return err;
22592f1eb65fSJamal Hadi Salim 	}
22602f1eb65fSJamal Hadi Salim 
2261f7b6983fSMasahide NAKAMURA 	c.data.type = type;
2262f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
226326b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
226415e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
22657067802eSAlexey Dobriyan 	c.net = net;
226626b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
22671da177e4SLinus Torvalds 	return 0;
22681da177e4SLinus Torvalds }
22691da177e4SLinus Torvalds 
227022e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
22715424f32eSThomas Graf 		struct nlattr **attrs)
22726c5c8ca7SJamal Hadi Salim {
2273fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
22746c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
22757b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
22766c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2277b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
22786c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2279295fae56SJamal Hadi Salim 	struct xfrm_mark m;
22807e652640SSteffen Klassert 	u32 if_id = 0;
22816c5c8ca7SJamal Hadi Salim 
228235a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2283f7b6983fSMasahide NAKAMURA 	if (err)
2284f7b6983fSMasahide NAKAMURA 		return err;
2285f7b6983fSMasahide NAKAMURA 
2286c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
2287c8bf4d04STimo Teräs 	if (err)
2288c8bf4d04STimo Teräs 		return err;
2289c8bf4d04STimo Teräs 
22907e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
22917e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
22927e652640SSteffen Klassert 
22934f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
22944f47e8abSXin Long 
22956c5c8ca7SJamal Hadi Salim 	if (p->index)
22964f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
22974f47e8abSXin Long 				      0, &err);
22986c5c8ca7SJamal Hadi Salim 	else {
22995424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
230003e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
23016c5c8ca7SJamal Hadi Salim 
230235a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
23036c5c8ca7SJamal Hadi Salim 		if (err)
23046c5c8ca7SJamal Hadi Salim 			return err;
23056c5c8ca7SJamal Hadi Salim 
23062c8dd116SDenis V. Lunev 		ctx = NULL;
23076c5c8ca7SJamal Hadi Salim 		if (rt) {
23085424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
23096c5c8ca7SJamal Hadi Salim 
231052a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
231103e1ad7bSPaul Moore 			if (err)
23126c5c8ca7SJamal Hadi Salim 				return err;
23132c8dd116SDenis V. Lunev 		}
23144f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
23156f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
231603e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
23176c5c8ca7SJamal Hadi Salim 	}
23186c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2319ef41aaa0SEric Paris 		return -ENOENT;
232003e1ad7bSPaul Moore 
2321ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
23226c5c8ca7SJamal Hadi Salim 		goto out;
23236c5c8ca7SJamal Hadi Salim 
23246c5c8ca7SJamal Hadi Salim 	err = 0;
23256c5c8ca7SJamal Hadi Salim 	if (up->hard) {
23266c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
23272e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
23286c5c8ca7SJamal Hadi Salim 	}
2329c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
23306c5c8ca7SJamal Hadi Salim 
23316c5c8ca7SJamal Hadi Salim out:
23326c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
23336c5c8ca7SJamal Hadi Salim 	return err;
23346c5c8ca7SJamal Hadi Salim }
23356c5c8ca7SJamal Hadi Salim 
233622e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
23375424f32eSThomas Graf 		struct nlattr **attrs)
233853bc6b4dSJamal Hadi Salim {
2339fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
234053bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
234153bc6b4dSJamal Hadi Salim 	int err;
23427b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
234353bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
23446f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2345928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
234653bc6b4dSJamal Hadi Salim 
23476f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
234853bc6b4dSJamal Hadi Salim 
23493a765aa5SDavid S. Miller 	err = -ENOENT;
235053bc6b4dSJamal Hadi Salim 	if (x == NULL)
235153bc6b4dSJamal Hadi Salim 		return err;
235253bc6b4dSJamal Hadi Salim 
235353bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
23543a765aa5SDavid S. Miller 	err = -EINVAL;
235553bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
235653bc6b4dSJamal Hadi Salim 		goto out;
2357c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
235853bc6b4dSJamal Hadi Salim 
2359161a09e7SJoy Latten 	if (ue->hard) {
236053bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
23612e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2362161a09e7SJoy Latten 	}
23633a765aa5SDavid S. Miller 	err = 0;
236453bc6b4dSJamal Hadi Salim out:
236553bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
236653bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
236753bc6b4dSJamal Hadi Salim 	return err;
236853bc6b4dSJamal Hadi Salim }
236953bc6b4dSJamal Hadi Salim 
237022e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
23715424f32eSThomas Graf 		struct nlattr **attrs)
2372980ebd25SJamal Hadi Salim {
2373fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2374980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2375980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2376980ebd25SJamal Hadi Salim 	int i;
23775424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
23786f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2379980ebd25SJamal Hadi Salim 
23807b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2381fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2382980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2383980ebd25SJamal Hadi Salim 
2384980ebd25SJamal Hadi Salim 	if (!x)
2385d8eb9307SIlpo Järvinen 		goto nomem;
2386980ebd25SJamal Hadi Salim 
23876f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
23886f26b61eSJamal Hadi Salim 
2389980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2390d8eb9307SIlpo Järvinen 	if (err)
239173efc324SVegard Nossum 		goto free_state;
2392a1a7e3a3SXin Long 	err = verify_sec_ctx_len(attrs);
2393a1a7e3a3SXin Long 	if (err)
2394a1a7e3a3SXin Long 		goto free_state;
2395980ebd25SJamal Hadi Salim 
2396980ebd25SJamal Hadi Salim 	/*   build an XP */
2397fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2398d8eb9307SIlpo Järvinen 	if (!xp)
2399d8eb9307SIlpo Järvinen 		goto free_state;
2400980ebd25SJamal Hadi Salim 
2401980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2402980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2403980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
24046f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
24056f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
24065424f32eSThomas Graf 	ut = nla_data(rt);
2407980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2408980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2409980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2410980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2411980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2412980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2413980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2414980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2415980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2416980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2417980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2418980ebd25SJamal Hadi Salim 
2419980ebd25SJamal Hadi Salim 	}
2420980ebd25SJamal Hadi Salim 
24214a135e53SMathias Krause 	xfrm_state_free(x);
2422980ebd25SJamal Hadi Salim 	kfree(xp);
2423980ebd25SJamal Hadi Salim 
2424980ebd25SJamal Hadi Salim 	return 0;
2425d8eb9307SIlpo Järvinen 
2426d8eb9307SIlpo Järvinen free_state:
24274a135e53SMathias Krause 	xfrm_state_free(x);
2428d8eb9307SIlpo Järvinen nomem:
2429d8eb9307SIlpo Järvinen 	return err;
2430980ebd25SJamal Hadi Salim }
2431980ebd25SJamal Hadi Salim 
24325c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
24335c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
243413c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
24355424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
24365c79de6eSShinta Sugimoto {
24375424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
24385c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
24395c79de6eSShinta Sugimoto 	int i, num_migrate;
24405c79de6eSShinta Sugimoto 
244113c1d189SArnaud Ebalard 	if (k != NULL) {
244213c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
244313c1d189SArnaud Ebalard 
244413c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
244513c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
244613c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
244713c1d189SArnaud Ebalard 		k->family = uk->family;
244813c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
244913c1d189SArnaud Ebalard 	}
245013c1d189SArnaud Ebalard 
24515424f32eSThomas Graf 	um = nla_data(rt);
24525424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
24535c79de6eSShinta Sugimoto 
24545c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
24555c79de6eSShinta Sugimoto 		return -EINVAL;
24565c79de6eSShinta Sugimoto 
24575c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
24585c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
24595c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
24605c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
24615c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
24625c79de6eSShinta Sugimoto 
24635c79de6eSShinta Sugimoto 		ma->proto = um->proto;
24645c79de6eSShinta Sugimoto 		ma->mode = um->mode;
24655c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
24665c79de6eSShinta Sugimoto 
24675c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
24685c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
24695c79de6eSShinta Sugimoto 	}
24705c79de6eSShinta Sugimoto 
24715c79de6eSShinta Sugimoto 	*num = i;
24725c79de6eSShinta Sugimoto 	return 0;
24735c79de6eSShinta Sugimoto }
24745c79de6eSShinta Sugimoto 
24755c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
24765424f32eSThomas Graf 			   struct nlattr **attrs)
24775c79de6eSShinta Sugimoto {
24787b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
24795c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
248013c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
24815c79de6eSShinta Sugimoto 	u8 type;
24825c79de6eSShinta Sugimoto 	int err;
24835c79de6eSShinta Sugimoto 	int n = 0;
24848d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
24854ab47d47SAntony Antony 	struct xfrm_encap_tmpl  *encap = NULL;
24865c79de6eSShinta Sugimoto 
248735a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2488cf5cb79fSThomas Graf 		return -EINVAL;
24895c79de6eSShinta Sugimoto 
249013c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
249113c1d189SArnaud Ebalard 
24925424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
24935c79de6eSShinta Sugimoto 	if (err)
24945c79de6eSShinta Sugimoto 		return err;
24955c79de6eSShinta Sugimoto 
249613c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
24975c79de6eSShinta Sugimoto 	if (err)
24985c79de6eSShinta Sugimoto 		return err;
24995c79de6eSShinta Sugimoto 
25005c79de6eSShinta Sugimoto 	if (!n)
25015c79de6eSShinta Sugimoto 		return 0;
25025c79de6eSShinta Sugimoto 
25034ab47d47SAntony Antony 	if (attrs[XFRMA_ENCAP]) {
25044ab47d47SAntony Antony 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
25054ab47d47SAntony Antony 				sizeof(*encap), GFP_KERNEL);
25064ab47d47SAntony Antony 		if (!encap)
25074ac7a6eeSZheng Yongjun 			return -ENOMEM;
25085c79de6eSShinta Sugimoto 	}
25094ab47d47SAntony Antony 
25104ab47d47SAntony Antony 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
25114ab47d47SAntony Antony 
25124ab47d47SAntony Antony 	kfree(encap);
25134ab47d47SAntony Antony 
25144ab47d47SAntony Antony 	return err;
25154ab47d47SAntony Antony }
25165c79de6eSShinta Sugimoto #else
25175c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
25185424f32eSThomas Graf 			   struct nlattr **attrs)
25195c79de6eSShinta Sugimoto {
25205c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
25215c79de6eSShinta Sugimoto }
25225c79de6eSShinta Sugimoto #endif
25235c79de6eSShinta Sugimoto 
25245c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2525183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
25265c79de6eSShinta Sugimoto {
25275c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
25285c79de6eSShinta Sugimoto 
25295c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
25305c79de6eSShinta Sugimoto 	um.proto = m->proto;
25315c79de6eSShinta Sugimoto 	um.mode = m->mode;
25325c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
25335c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
25345c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
25355c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
25365c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
25375c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
25385c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
25395c79de6eSShinta Sugimoto 
2540c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
25415c79de6eSShinta Sugimoto }
25425c79de6eSShinta Sugimoto 
2543183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
254413c1d189SArnaud Ebalard {
254513c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
254613c1d189SArnaud Ebalard 
254713c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
254813c1d189SArnaud Ebalard 	uk.family = k->family;
254913c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
255013c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2551a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
255213c1d189SArnaud Ebalard 
255313c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
255413c1d189SArnaud Ebalard }
255513c1d189SArnaud Ebalard 
2556a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
25578bafd730SAntony Antony 						int with_encp)
25587deb2264SThomas Graf {
25597deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
256013c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
25618bafd730SAntony Antony 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
25627deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
25637deb2264SThomas Graf 	      + userpolicy_type_attrsize();
25647deb2264SThomas Graf }
25657deb2264SThomas Graf 
2566183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2567183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
25688bafd730SAntony Antony 			 const struct xfrm_selector *sel,
25698bafd730SAntony Antony 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
25705c79de6eSShinta Sugimoto {
2571183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
25725c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
25735c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
25741d1e34ddSDavid S. Miller 	int i, err;
25755c79de6eSShinta Sugimoto 
257679b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
257779b8b7f4SThomas Graf 	if (nlh == NULL)
257879b8b7f4SThomas Graf 		return -EMSGSIZE;
25795c79de6eSShinta Sugimoto 
25807b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
25815c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
25825c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
25835c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
25845c79de6eSShinta Sugimoto 	pol_id->dir = dir;
25855c79de6eSShinta Sugimoto 
25861d1e34ddSDavid S. Miller 	if (k != NULL) {
25871d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
25881d1e34ddSDavid S. Miller 		if (err)
25891d1e34ddSDavid S. Miller 			goto out_cancel;
25901d1e34ddSDavid S. Miller 	}
25918bafd730SAntony Antony 	if (encap) {
25928bafd730SAntony Antony 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
25938bafd730SAntony Antony 		if (err)
25948bafd730SAntony Antony 			goto out_cancel;
25958bafd730SAntony Antony 	}
25961d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
25971d1e34ddSDavid S. Miller 	if (err)
25981d1e34ddSDavid S. Miller 		goto out_cancel;
25995c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
26001d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
26011d1e34ddSDavid S. Miller 		if (err)
26021d1e34ddSDavid S. Miller 			goto out_cancel;
26035c79de6eSShinta Sugimoto 	}
26045c79de6eSShinta Sugimoto 
2605053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2606053c095aSJohannes Berg 	return 0;
26071d1e34ddSDavid S. Miller 
26081d1e34ddSDavid S. Miller out_cancel:
26099825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
26101d1e34ddSDavid S. Miller 	return err;
26115c79de6eSShinta Sugimoto }
26125c79de6eSShinta Sugimoto 
2613183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2614183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
26158bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
26168bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
26175c79de6eSShinta Sugimoto {
2618a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
26195c79de6eSShinta Sugimoto 	struct sk_buff *skb;
26202fc5f83bSGustavo A. R. Silva 	int err;
26215c79de6eSShinta Sugimoto 
26228bafd730SAntony Antony 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
26238bafd730SAntony Antony 			GFP_ATOMIC);
26245c79de6eSShinta Sugimoto 	if (skb == NULL)
26255c79de6eSShinta Sugimoto 		return -ENOMEM;
26265c79de6eSShinta Sugimoto 
26275c79de6eSShinta Sugimoto 	/* build migrate */
26282fc5f83bSGustavo A. R. Silva 	err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
26292fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
26305c79de6eSShinta Sugimoto 
263121ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
26325c79de6eSShinta Sugimoto }
26335c79de6eSShinta Sugimoto #else
2634183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2635183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
26368bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
26378bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
26385c79de6eSShinta Sugimoto {
26395c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
26405c79de6eSShinta Sugimoto }
26415c79de6eSShinta Sugimoto #endif
2642d51d081dSJamal Hadi Salim 
2643a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2644492b558bSThomas Graf 
26455461fc0cSDmitry Safonov const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
264666f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2647492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2648492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2649492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2650492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2651492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2652492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2653980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
265453bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2655492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
265666f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
26576c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2658492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2659a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2660d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2661d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
266297a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
26635c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2664a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2665880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2666a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
26671da177e4SLinus Torvalds };
26685461fc0cSDmitry Safonov EXPORT_SYMBOL_GPL(xfrm_msg_min);
26691da177e4SLinus Torvalds 
2670492b558bSThomas Graf #undef XMSGSIZE
2671492b558bSThomas Graf 
26725106f4a8SDmitry Safonov const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2673c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2674c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2675c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2676c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
26771a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2678cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2679cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2680cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2681cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2682cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2683cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2684cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2685cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2686cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2687cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2688cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2689cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2690cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2691cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
269213c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
26936f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
269435d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2695d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2696a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2697d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2698870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2699d77e38e6SSteffen Klassert 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
27009b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK]	= { .type = NLA_U32 },
27019b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK_MASK]	= { .type = NLA_U32 },
27027e652640SSteffen Klassert 	[XFRMA_IF_ID]		= { .type = NLA_U32 },
2703cf5cb79fSThomas Graf };
27045106f4a8SDmitry Safonov EXPORT_SYMBOL_GPL(xfrma_policy);
2705cf5cb79fSThomas Graf 
2706880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2707880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2708880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2709880a6fabSChristophe Gouault };
2710880a6fabSChristophe Gouault 
271105600a79SMathias Krause static const struct xfrm_link {
27125424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
27131137b5e2SHerbert Xu 	int (*start)(struct netlink_callback *);
27141da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
27154c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2716880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2717880a6fabSChristophe Gouault 	int nla_max;
2718492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2719492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2720492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2721492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
27224c563f76STimo Teras 						   .dump = xfrm_dump_sa,
27234c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2724492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2725492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2726492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
27271137b5e2SHerbert Xu 						   .start = xfrm_dump_policy_start,
27284c563f76STimo Teras 						   .dump = xfrm_dump_policy,
27294c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2730492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2731980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
273253bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2733492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2734492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
27356c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2736492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2737492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2738d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2739d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
27405c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
274128d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2742880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2743880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2744880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2745ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
27461da177e4SLinus Torvalds };
27471da177e4SLinus Torvalds 
27482d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
27492d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
27501da177e4SLinus Torvalds {
2751a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
275235a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
275305600a79SMathias Krause 	const struct xfrm_link *link;
27545106f4a8SDmitry Safonov 	struct nlmsghdr *nlh64 = NULL;
2755a7bd9a45SThomas Graf 	int type, err;
27561da177e4SLinus Torvalds 
27571da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
27581da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
27591d00a4ebSThomas Graf 		return -EINVAL;
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
27621da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
27631da177e4SLinus Torvalds 
27641da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
276590f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
27661d00a4ebSThomas Graf 		return -EPERM;
27671da177e4SLinus Torvalds 
27685106f4a8SDmitry Safonov 	if (in_compat_syscall()) {
27695106f4a8SDmitry Safonov 		struct xfrm_translator *xtr = xfrm_get_translator();
27705106f4a8SDmitry Safonov 
27715106f4a8SDmitry Safonov 		if (!xtr)
27725106f4a8SDmitry Safonov 			return -EOPNOTSUPP;
27735106f4a8SDmitry Safonov 
27745106f4a8SDmitry Safonov 		nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
27755106f4a8SDmitry Safonov 					    link->nla_pol, extack);
27765106f4a8SDmitry Safonov 		xfrm_put_translator(xtr);
27775106f4a8SDmitry Safonov 		if (IS_ERR(nlh64))
27785106f4a8SDmitry Safonov 			return PTR_ERR(nlh64);
27795106f4a8SDmitry Safonov 		if (nlh64)
27805106f4a8SDmitry Safonov 			nlh = nlh64;
27815106f4a8SDmitry Safonov 	}
27825106f4a8SDmitry Safonov 
2783492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2784492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2785b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
278680d326faSPablo Neira Ayuso 		struct netlink_dump_control c = {
27871137b5e2SHerbert Xu 			.start = link->start,
278880d326faSPablo Neira Ayuso 			.dump = link->dump,
278980d326faSPablo Neira Ayuso 			.done = link->done,
279080d326faSPablo Neira Ayuso 		};
27915106f4a8SDmitry Safonov 
27925106f4a8SDmitry Safonov 		if (link->dump == NULL) {
27935106f4a8SDmitry Safonov 			err = -EINVAL;
27945106f4a8SDmitry Safonov 			goto err;
279580d326faSPablo Neira Ayuso 		}
27965106f4a8SDmitry Safonov 
27975106f4a8SDmitry Safonov 		err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
27985106f4a8SDmitry Safonov 		goto err;
27991da177e4SLinus Torvalds 	}
28001da177e4SLinus Torvalds 
28018cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2802880a6fabSChristophe Gouault 				     link->nla_max ? : XFRMA_MAX,
2803fe52145fSJohannes Berg 				     link->nla_pol ? : xfrma_policy, extack);
2804a7bd9a45SThomas Graf 	if (err < 0)
28055106f4a8SDmitry Safonov 		goto err;
28065106f4a8SDmitry Safonov 
28075106f4a8SDmitry Safonov 	if (link->doit == NULL) {
28085106f4a8SDmitry Safonov 		err = -EINVAL;
28095106f4a8SDmitry Safonov 		goto err;
28105106f4a8SDmitry Safonov 	}
28115106f4a8SDmitry Safonov 
28125106f4a8SDmitry Safonov 	err = link->doit(skb, nlh, attrs);
28135106f4a8SDmitry Safonov 
28145106f4a8SDmitry Safonov err:
28155106f4a8SDmitry Safonov 	kvfree(nlh64);
2816a7bd9a45SThomas Graf 	return err;
28171da177e4SLinus Torvalds }
28181da177e4SLinus Torvalds 
2819cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
28201da177e4SLinus Torvalds {
2821283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2822283bc9f3SFan Du 
2823283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2824cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2825283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
28261da177e4SLinus Torvalds }
28271da177e4SLinus Torvalds 
2828a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void)
28297deb2264SThomas Graf {
28306f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
28316f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
28327deb2264SThomas Graf }
28337deb2264SThomas Graf 
2834214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
28351da177e4SLinus Torvalds {
28361da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
28371da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
28381d1e34ddSDavid S. Miller 	int err;
28391da177e4SLinus Torvalds 
284015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
284179b8b7f4SThomas Graf 	if (nlh == NULL)
284279b8b7f4SThomas Graf 		return -EMSGSIZE;
28431da177e4SLinus Torvalds 
28447b67c857SThomas Graf 	ue = nlmsg_data(nlh);
28451da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2846d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
2847e3e5fc16SMathias Krause 	/* clear the padding bytes */
2848e3e5fc16SMathias Krause 	memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
28491da177e4SLinus Torvalds 
28501d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
28511d1e34ddSDavid S. Miller 	if (err)
28521d1e34ddSDavid S. Miller 		return err;
28536f26b61eSJamal Hadi Salim 
28547e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
28557e652640SSteffen Klassert 	if (err)
28567e652640SSteffen Klassert 		return err;
28577e652640SSteffen Klassert 
2858053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2859053c095aSJohannes Berg 	return 0;
28601da177e4SLinus Torvalds }
28611da177e4SLinus Torvalds 
2862214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
28631da177e4SLinus Torvalds {
2864fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
28651da177e4SLinus Torvalds 	struct sk_buff *skb;
28661da177e4SLinus Torvalds 
28677deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
28681da177e4SLinus Torvalds 	if (skb == NULL)
28691da177e4SLinus Torvalds 		return -ENOMEM;
28701da177e4SLinus Torvalds 
28716f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
28726f26b61eSJamal Hadi Salim 		kfree_skb(skb);
28736f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
28746f26b61eSJamal Hadi Salim 	}
28751da177e4SLinus Torvalds 
287621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
28771da177e4SLinus Torvalds }
28781da177e4SLinus Torvalds 
2879214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2880d51d081dSJamal Hadi Salim {
2881fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2882d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
28832fc5f83bSGustavo A. R. Silva 	int err;
2884d51d081dSJamal Hadi Salim 
2885d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2886d51d081dSJamal Hadi Salim 	if (skb == NULL)
2887d51d081dSJamal Hadi Salim 		return -ENOMEM;
2888d51d081dSJamal Hadi Salim 
28892fc5f83bSGustavo A. R. Silva 	err = build_aevent(skb, x, c);
28902fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
2891d51d081dSJamal Hadi Salim 
289221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2893d51d081dSJamal Hadi Salim }
2894d51d081dSJamal Hadi Salim 
2895214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
289626b15dadSJamal Hadi Salim {
28977067802eSAlexey Dobriyan 	struct net *net = c->net;
289826b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
289926b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
290026b15dadSJamal Hadi Salim 	struct sk_buff *skb;
29017deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
290226b15dadSJamal Hadi Salim 
29037deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
290426b15dadSJamal Hadi Salim 	if (skb == NULL)
290526b15dadSJamal Hadi Salim 		return -ENOMEM;
290626b15dadSJamal Hadi Salim 
290715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
290879b8b7f4SThomas Graf 	if (nlh == NULL) {
290979b8b7f4SThomas Graf 		kfree_skb(skb);
291079b8b7f4SThomas Graf 		return -EMSGSIZE;
291179b8b7f4SThomas Graf 	}
291226b15dadSJamal Hadi Salim 
29137b67c857SThomas Graf 	p = nlmsg_data(nlh);
2914bf08867fSHerbert Xu 	p->proto = c->data.proto;
291526b15dadSJamal Hadi Salim 
29169825069dSThomas Graf 	nlmsg_end(skb, nlh);
291726b15dadSJamal Hadi Salim 
291821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
291926b15dadSJamal Hadi Salim }
292026b15dadSJamal Hadi Salim 
2921a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
292226b15dadSJamal Hadi Salim {
2923a1b831f2SAlexey Dobriyan 	unsigned int l = 0;
29241a6509d9SHerbert Xu 	if (x->aead)
29251a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
29264447bb33SMartin Willi 	if (x->aalg) {
29274447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
29284447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
29294447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
29304447bb33SMartin Willi 	}
293126b15dadSJamal Hadi Salim 	if (x->ealg)
29320f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
293326b15dadSJamal Hadi Salim 	if (x->calg)
29347deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
293526b15dadSJamal Hadi Salim 	if (x->encap)
29367deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
293735d2856bSMartin Willi 	if (x->tfcpad)
293835d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
2939d8647b79SSteffen Klassert 	if (x->replay_esn)
2940d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2941f293a5e3Sdingzhi 	else
2942f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
294368325d3bSHerbert Xu 	if (x->security)
294468325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
294568325d3bSHerbert Xu 				    x->security->ctx_len);
294668325d3bSHerbert Xu 	if (x->coaddr)
294768325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
2948a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
2949a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
2950d77e38e6SSteffen Klassert 	if (x->xso.dev)
2951d77e38e6SSteffen Klassert 		 l += nla_total_size(sizeof(x->xso));
29529b42c1f1SSteffen Klassert 	if (x->props.smark.v | x->props.smark.m) {
29539b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.v));
29549b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.m));
29559b42c1f1SSteffen Klassert 	}
29567e652640SSteffen Klassert 	if (x->if_id)
29577e652640SSteffen Klassert 		l += nla_total_size(sizeof(x->if_id));
295868325d3bSHerbert Xu 
2959d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2960de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
296126b15dadSJamal Hadi Salim 
296226b15dadSJamal Hadi Salim 	return l;
296326b15dadSJamal Hadi Salim }
296426b15dadSJamal Hadi Salim 
2965214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
296626b15dadSJamal Hadi Salim {
2967fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
296826b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
29690603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
297026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
297126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
2972a1b831f2SAlexey Dobriyan 	unsigned int len = xfrm_sa_len(x);
2973a1b831f2SAlexey Dobriyan 	unsigned int headlen;
2974a1b831f2SAlexey Dobriyan 	int err;
29750603eac0SHerbert Xu 
29760603eac0SHerbert Xu 	headlen = sizeof(*p);
29770603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
29787deb2264SThomas Graf 		len += nla_total_size(headlen);
29790603eac0SHerbert Xu 		headlen = sizeof(*id);
29806f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
29810603eac0SHerbert Xu 	}
29827deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
298326b15dadSJamal Hadi Salim 
29847deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
298526b15dadSJamal Hadi Salim 	if (skb == NULL)
298626b15dadSJamal Hadi Salim 		return -ENOMEM;
298726b15dadSJamal Hadi Salim 
298815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
29891d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
299079b8b7f4SThomas Graf 	if (nlh == NULL)
29911d1e34ddSDavid S. Miller 		goto out_free_skb;
299226b15dadSJamal Hadi Salim 
29937b67c857SThomas Graf 	p = nlmsg_data(nlh);
29940603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2995c0144beaSThomas Graf 		struct nlattr *attr;
2996c0144beaSThomas Graf 
29977b67c857SThomas Graf 		id = nlmsg_data(nlh);
299850329c8aSMathias Krause 		memset(id, 0, sizeof(*id));
29990603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
30000603eac0SHerbert Xu 		id->spi = x->id.spi;
30010603eac0SHerbert Xu 		id->family = x->props.family;
30020603eac0SHerbert Xu 		id->proto = x->id.proto;
30030603eac0SHerbert Xu 
3004c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
30051d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3006c0144beaSThomas Graf 		if (attr == NULL)
30071d1e34ddSDavid S. Miller 			goto out_free_skb;
3008c0144beaSThomas Graf 
3009c0144beaSThomas Graf 		p = nla_data(attr);
30100603eac0SHerbert Xu 	}
30111d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
30121d1e34ddSDavid S. Miller 	if (err)
30131d1e34ddSDavid S. Miller 		goto out_free_skb;
301426b15dadSJamal Hadi Salim 
30159825069dSThomas Graf 	nlmsg_end(skb, nlh);
301626b15dadSJamal Hadi Salim 
301721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
301826b15dadSJamal Hadi Salim 
30191d1e34ddSDavid S. Miller out_free_skb:
302026b15dadSJamal Hadi Salim 	kfree_skb(skb);
30211d1e34ddSDavid S. Miller 	return err;
302226b15dadSJamal Hadi Salim }
302326b15dadSJamal Hadi Salim 
3024214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
302526b15dadSJamal Hadi Salim {
302626b15dadSJamal Hadi Salim 
302726b15dadSJamal Hadi Salim 	switch (c->event) {
3028f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
302926b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
3030d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
3031d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
3032f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
3033f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
3034f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
303526b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
3036f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
303726b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
303826b15dadSJamal Hadi Salim 	default:
303962db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
304062db5cfdSstephen hemminger 		       c->event);
304126b15dadSJamal Hadi Salim 		break;
304226b15dadSJamal Hadi Salim 	}
304326b15dadSJamal Hadi Salim 
304426b15dadSJamal Hadi Salim 	return 0;
304526b15dadSJamal Hadi Salim 
304626b15dadSJamal Hadi Salim }
304726b15dadSJamal Hadi Salim 
3048a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
30497deb2264SThomas Graf 						struct xfrm_policy *xp)
30507deb2264SThomas Graf {
30517deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
30527deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
30536f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
30547deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
30557deb2264SThomas Graf 	       + userpolicy_type_attrsize();
30567deb2264SThomas Graf }
30577deb2264SThomas Graf 
30581da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
305965e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
30601da177e4SLinus Torvalds {
30611d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
30621da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
30631da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
30641d1e34ddSDavid S. Miller 	int err;
30651da177e4SLinus Torvalds 
306679b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
306779b8b7f4SThomas Graf 	if (nlh == NULL)
306879b8b7f4SThomas Graf 		return -EMSGSIZE;
30691da177e4SLinus Torvalds 
30707b67c857SThomas Graf 	ua = nlmsg_data(nlh);
30711da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
30721da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
30731da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
307465e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
30751da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
30761da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
30771da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
30781da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
30791da177e4SLinus Torvalds 
30801d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
30811d1e34ddSDavid S. Miller 	if (!err)
30821d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
30831d1e34ddSDavid S. Miller 	if (!err)
30841d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
30851d1e34ddSDavid S. Miller 	if (!err)
30861d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
30877e652640SSteffen Klassert 	if (!err)
30887e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
30891d1e34ddSDavid S. Miller 	if (err) {
30901d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
30911d1e34ddSDavid S. Miller 		return err;
30921d1e34ddSDavid S. Miller 	}
30931da177e4SLinus Torvalds 
3094053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3095053c095aSJohannes Berg 	return 0;
30961da177e4SLinus Torvalds }
30971da177e4SLinus Torvalds 
30981da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
309965e0736bSFan Du 			     struct xfrm_policy *xp)
31001da177e4SLinus Torvalds {
3101a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
31021da177e4SLinus Torvalds 	struct sk_buff *skb;
31032fc5f83bSGustavo A. R. Silva 	int err;
31041da177e4SLinus Torvalds 
31057deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
31061da177e4SLinus Torvalds 	if (skb == NULL)
31071da177e4SLinus Torvalds 		return -ENOMEM;
31081da177e4SLinus Torvalds 
31092fc5f83bSGustavo A. R. Silva 	err = build_acquire(skb, x, xt, xp);
31102fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
31111da177e4SLinus Torvalds 
311221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
31131da177e4SLinus Torvalds }
31141da177e4SLinus Torvalds 
31151da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
31161da177e4SLinus Torvalds  * or more templates.
31171da177e4SLinus Torvalds  */
3118cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
31191da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
31201da177e4SLinus Torvalds {
3121fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
31221da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
31231da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
31241da177e4SLinus Torvalds 	struct xfrm_policy *xp;
31251da177e4SLinus Torvalds 	int nr;
31261da177e4SLinus Torvalds 
3127cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
31281da177e4SLinus Torvalds 	case AF_INET:
31291da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
31301da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
31311da177e4SLinus Torvalds 			return NULL;
31321da177e4SLinus Torvalds 		}
31331da177e4SLinus Torvalds 		break;
3134dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
31351da177e4SLinus Torvalds 	case AF_INET6:
31361da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
31371da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
31381da177e4SLinus Torvalds 			return NULL;
31391da177e4SLinus Torvalds 		}
31401da177e4SLinus Torvalds 		break;
31411da177e4SLinus Torvalds #endif
31421da177e4SLinus Torvalds 	default:
31431da177e4SLinus Torvalds 		*dir = -EINVAL;
31441da177e4SLinus Torvalds 		return NULL;
31451da177e4SLinus Torvalds 	}
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds 	*dir = -EINVAL;
31481da177e4SLinus Torvalds 
31491da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
31501da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
31511da177e4SLinus Torvalds 		return NULL;
31521da177e4SLinus Torvalds 
31531da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
3154b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
31551da177e4SLinus Torvalds 		return NULL;
31561da177e4SLinus Torvalds 
3157a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
3158a4f1bac6SHerbert Xu 		return NULL;
3159a4f1bac6SHerbert Xu 
31602f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
31611da177e4SLinus Torvalds 	if (xp == NULL) {
31621da177e4SLinus Torvalds 		*dir = -ENOBUFS;
31631da177e4SLinus Torvalds 		return NULL;
31641da177e4SLinus Torvalds 	}
31651da177e4SLinus Torvalds 
31661da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
3167f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
31681da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
31691da177e4SLinus Torvalds 
31701da177e4SLinus Torvalds 	*dir = p->dir;
31711da177e4SLinus Torvalds 
31721da177e4SLinus Torvalds 	return xp;
31731da177e4SLinus Torvalds }
31741da177e4SLinus Torvalds 
3175a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
31767deb2264SThomas Graf {
31777deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
31787deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
31797deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3180295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
31817deb2264SThomas Graf 	       + userpolicy_type_attrsize();
31827deb2264SThomas Graf }
31837deb2264SThomas Graf 
31841da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3185214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
31861da177e4SLinus Torvalds {
31871da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
3188d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
31891d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
31901d1e34ddSDavid S. Miller 	int err;
31911da177e4SLinus Torvalds 
319215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
319379b8b7f4SThomas Graf 	if (nlh == NULL)
319479b8b7f4SThomas Graf 		return -EMSGSIZE;
31951da177e4SLinus Torvalds 
31967b67c857SThomas Graf 	upe = nlmsg_data(nlh);
31971da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
31981d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
31991d1e34ddSDavid S. Miller 	if (!err)
32001d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
32011d1e34ddSDavid S. Miller 	if (!err)
32021d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
32031d1e34ddSDavid S. Miller 	if (!err)
32041d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
32057e652640SSteffen Klassert 	if (!err)
32067e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
32071d1e34ddSDavid S. Miller 	if (err) {
32081d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
32091d1e34ddSDavid S. Miller 		return err;
32101d1e34ddSDavid S. Miller 	}
32111da177e4SLinus Torvalds 	upe->hard = !!hard;
32121da177e4SLinus Torvalds 
3213053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3214053c095aSJohannes Berg 	return 0;
32151da177e4SLinus Torvalds }
32161da177e4SLinus Torvalds 
3217214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
32181da177e4SLinus Torvalds {
3219fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
32201da177e4SLinus Torvalds 	struct sk_buff *skb;
32212fc5f83bSGustavo A. R. Silva 	int err;
32221da177e4SLinus Torvalds 
32237deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
32241da177e4SLinus Torvalds 	if (skb == NULL)
32251da177e4SLinus Torvalds 		return -ENOMEM;
32261da177e4SLinus Torvalds 
32272fc5f83bSGustavo A. R. Silva 	err = build_polexpire(skb, xp, dir, c);
32282fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
32291da177e4SLinus Torvalds 
323021ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
32311da177e4SLinus Torvalds }
32321da177e4SLinus Torvalds 
3233214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
323426b15dadSJamal Hadi Salim {
3235a1b831f2SAlexey Dobriyan 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3236fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
323726b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
32380603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
323926b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
324026b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3241a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3242a1b831f2SAlexey Dobriyan 	int err;
32430603eac0SHerbert Xu 
32440603eac0SHerbert Xu 	headlen = sizeof(*p);
32450603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
32467deb2264SThomas Graf 		len += nla_total_size(headlen);
32470603eac0SHerbert Xu 		headlen = sizeof(*id);
32480603eac0SHerbert Xu 	}
3249cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
3250295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
32517deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
325226b15dadSJamal Hadi Salim 
32537deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
325426b15dadSJamal Hadi Salim 	if (skb == NULL)
325526b15dadSJamal Hadi Salim 		return -ENOMEM;
325626b15dadSJamal Hadi Salim 
325715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
32581d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
325979b8b7f4SThomas Graf 	if (nlh == NULL)
32601d1e34ddSDavid S. Miller 		goto out_free_skb;
326126b15dadSJamal Hadi Salim 
32627b67c857SThomas Graf 	p = nlmsg_data(nlh);
32630603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
3264c0144beaSThomas Graf 		struct nlattr *attr;
3265c0144beaSThomas Graf 
32667b67c857SThomas Graf 		id = nlmsg_data(nlh);
32670603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
32680603eac0SHerbert Xu 		id->dir = dir;
32690603eac0SHerbert Xu 		if (c->data.byid)
32700603eac0SHerbert Xu 			id->index = xp->index;
32710603eac0SHerbert Xu 		else
32720603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
32730603eac0SHerbert Xu 
3274c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
32751d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3276c0144beaSThomas Graf 		if (attr == NULL)
32771d1e34ddSDavid S. Miller 			goto out_free_skb;
3278c0144beaSThomas Graf 
3279c0144beaSThomas Graf 		p = nla_data(attr);
32800603eac0SHerbert Xu 	}
328126b15dadSJamal Hadi Salim 
328226b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
32831d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
32841d1e34ddSDavid S. Miller 	if (!err)
32851d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
32861d1e34ddSDavid S. Miller 	if (!err)
32871d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
32887e652640SSteffen Klassert 	if (!err)
32897e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
32901d1e34ddSDavid S. Miller 	if (err)
32911d1e34ddSDavid S. Miller 		goto out_free_skb;
3292295fae56SJamal Hadi Salim 
32939825069dSThomas Graf 	nlmsg_end(skb, nlh);
329426b15dadSJamal Hadi Salim 
329521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
329626b15dadSJamal Hadi Salim 
32971d1e34ddSDavid S. Miller out_free_skb:
329826b15dadSJamal Hadi Salim 	kfree_skb(skb);
32991d1e34ddSDavid S. Miller 	return err;
330026b15dadSJamal Hadi Salim }
330126b15dadSJamal Hadi Salim 
3302214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
330326b15dadSJamal Hadi Salim {
33047067802eSAlexey Dobriyan 	struct net *net = c->net;
330526b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
330626b15dadSJamal Hadi Salim 	struct sk_buff *skb;
33071d1e34ddSDavid S. Miller 	int err;
330826b15dadSJamal Hadi Salim 
33097deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
331026b15dadSJamal Hadi Salim 	if (skb == NULL)
331126b15dadSJamal Hadi Salim 		return -ENOMEM;
331226b15dadSJamal Hadi Salim 
331315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
33141d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
331579b8b7f4SThomas Graf 	if (nlh == NULL)
33161d1e34ddSDavid S. Miller 		goto out_free_skb;
33171d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
33181d1e34ddSDavid S. Miller 	if (err)
33191d1e34ddSDavid S. Miller 		goto out_free_skb;
332026b15dadSJamal Hadi Salim 
33219825069dSThomas Graf 	nlmsg_end(skb, nlh);
332226b15dadSJamal Hadi Salim 
332321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
332426b15dadSJamal Hadi Salim 
33251d1e34ddSDavid S. Miller out_free_skb:
332626b15dadSJamal Hadi Salim 	kfree_skb(skb);
33271d1e34ddSDavid S. Miller 	return err;
332826b15dadSJamal Hadi Salim }
332926b15dadSJamal Hadi Salim 
3330214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
333126b15dadSJamal Hadi Salim {
333226b15dadSJamal Hadi Salim 
333326b15dadSJamal Hadi Salim 	switch (c->event) {
3334f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
3335f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
3336f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
333726b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
3338f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
333926b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
3340f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
334126b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
334226b15dadSJamal Hadi Salim 	default:
334362db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
334462db5cfdSstephen hemminger 		       c->event);
334526b15dadSJamal Hadi Salim 	}
334626b15dadSJamal Hadi Salim 
334726b15dadSJamal Hadi Salim 	return 0;
334826b15dadSJamal Hadi Salim 
334926b15dadSJamal Hadi Salim }
335026b15dadSJamal Hadi Salim 
3351a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void)
33527deb2264SThomas Graf {
33537deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
33547deb2264SThomas Graf }
33557deb2264SThomas Graf 
335697a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
335797a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
335897a64b45SMasahide NAKAMURA {
335997a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
336097a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
336197a64b45SMasahide NAKAMURA 
336279b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
336379b8b7f4SThomas Graf 	if (nlh == NULL)
336479b8b7f4SThomas Graf 		return -EMSGSIZE;
336597a64b45SMasahide NAKAMURA 
33667b67c857SThomas Graf 	ur = nlmsg_data(nlh);
336797a64b45SMasahide NAKAMURA 	ur->proto = proto;
336897a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
336997a64b45SMasahide NAKAMURA 
33701d1e34ddSDavid S. Miller 	if (addr) {
33711d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
33721d1e34ddSDavid S. Miller 		if (err) {
33739825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
33741d1e34ddSDavid S. Miller 			return err;
33751d1e34ddSDavid S. Miller 		}
33761d1e34ddSDavid S. Miller 	}
3377053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3378053c095aSJohannes Berg 	return 0;
337997a64b45SMasahide NAKAMURA }
338097a64b45SMasahide NAKAMURA 
3381db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3382db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
338397a64b45SMasahide NAKAMURA {
338497a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
33852fc5f83bSGustavo A. R. Silva 	int err;
338697a64b45SMasahide NAKAMURA 
33877deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
338897a64b45SMasahide NAKAMURA 	if (skb == NULL)
338997a64b45SMasahide NAKAMURA 		return -ENOMEM;
339097a64b45SMasahide NAKAMURA 
33912fc5f83bSGustavo A. R. Silva 	err = build_report(skb, proto, sel, addr);
33922fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
339397a64b45SMasahide NAKAMURA 
339421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
339597a64b45SMasahide NAKAMURA }
339697a64b45SMasahide NAKAMURA 
3397a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void)
33983a2dfbe8SMartin Willi {
33993a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
34003a2dfbe8SMartin Willi }
34013a2dfbe8SMartin Willi 
34023a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
34033a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
34043a2dfbe8SMartin Willi {
34053a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
34063a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
34073a2dfbe8SMartin Willi 
34083a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
34093a2dfbe8SMartin Willi 	if (nlh == NULL)
34103a2dfbe8SMartin Willi 		return -EMSGSIZE;
34113a2dfbe8SMartin Willi 
34123a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
34133a2dfbe8SMartin Willi 
34143a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
34153a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
34163a2dfbe8SMartin Willi 	um->id.family = x->props.family;
34173a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
34183a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
34193a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
34203a2dfbe8SMartin Willi 	um->new_sport = new_sport;
34213a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
34223a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
34233a2dfbe8SMartin Willi 
3424053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3425053c095aSJohannes Berg 	return 0;
34263a2dfbe8SMartin Willi }
34273a2dfbe8SMartin Willi 
34283a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
34293a2dfbe8SMartin Willi 			     __be16 sport)
34303a2dfbe8SMartin Willi {
3431a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
34323a2dfbe8SMartin Willi 	struct sk_buff *skb;
34332fc5f83bSGustavo A. R. Silva 	int err;
34343a2dfbe8SMartin Willi 
34353a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
34363a2dfbe8SMartin Willi 		return -EINVAL;
34373a2dfbe8SMartin Willi 
34383a2dfbe8SMartin Willi 	if (!x->encap)
34393a2dfbe8SMartin Willi 		return -EINVAL;
34403a2dfbe8SMartin Willi 
34413a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
34423a2dfbe8SMartin Willi 	if (skb == NULL)
34433a2dfbe8SMartin Willi 		return -ENOMEM;
34443a2dfbe8SMartin Willi 
34452fc5f83bSGustavo A. R. Silva 	err = build_mapping(skb, x, ipaddr, sport);
34462fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
34473a2dfbe8SMartin Willi 
344821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
34493a2dfbe8SMartin Willi }
34503a2dfbe8SMartin Willi 
34510f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
34520f24558eSHoria Geanta {
34530f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
34540f24558eSHoria Geanta }
34550f24558eSHoria Geanta 
34561da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
34571da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
34581da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
34591da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
34601da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
346197a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
34625c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
34633a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
34640f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
34651da177e4SLinus Torvalds };
34661da177e4SLinus Torvalds 
3467a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
34681da177e4SLinus Torvalds {
3469be33690dSPatrick McHardy 	struct sock *nlsk;
3470a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3471a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3472a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3473a31f2d17SPablo Neira Ayuso 	};
3474be33690dSPatrick McHardy 
34759f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3476be33690dSPatrick McHardy 	if (nlsk == NULL)
34771da177e4SLinus Torvalds 		return -ENOMEM;
3478d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3479cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
34801da177e4SLinus Torvalds 	return 0;
34811da177e4SLinus Torvalds }
34821da177e4SLinus Torvalds 
3483d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3484a6483b79SAlexey Dobriyan {
3485d79d792eSEric W. Biederman 	struct net *net;
3486d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3487a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3488d79d792eSEric W. Biederman 	synchronize_net();
3489d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3490d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3491a6483b79SAlexey Dobriyan }
3492a6483b79SAlexey Dobriyan 
3493a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3494a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
3495d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3496a6483b79SAlexey Dobriyan };
3497a6483b79SAlexey Dobriyan 
3498a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3499a6483b79SAlexey Dobriyan {
3500a6483b79SAlexey Dobriyan 	int rv;
3501a6483b79SAlexey Dobriyan 
3502a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3503a6483b79SAlexey Dobriyan 
3504a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3505a6483b79SAlexey Dobriyan 	if (rv < 0)
3506a6483b79SAlexey Dobriyan 		return rv;
3507a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3508a6483b79SAlexey Dobriyan 	if (rv < 0)
3509a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3510a6483b79SAlexey Dobriyan 	return rv;
3511a6483b79SAlexey Dobriyan }
3512a6483b79SAlexey Dobriyan 
35131da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
35141da177e4SLinus Torvalds {
35151da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3516a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
35171da177e4SLinus Torvalds }
35181da177e4SLinus Torvalds 
35191da177e4SLinus Torvalds module_init(xfrm_user_init);
35201da177e4SLinus Torvalds module_exit(xfrm_user_exit);
35211da177e4SLinus Torvalds MODULE_LICENSE("GPL");
35224fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3523