xref: /linux/net/xfrm/xfrm_user.c (revision 5f3eea6b7e8f58cf5c8a9d4b9679dc19e9e67ba3)
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 
8514447bb33SMartin Willi static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
8524447bb33SMartin Willi {
8534447bb33SMartin Willi 	struct xfrm_algo *algo;
8544447bb33SMartin Willi 	struct nlattr *nla;
8554447bb33SMartin Willi 
8564447bb33SMartin Willi 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
8574447bb33SMartin Willi 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
8584447bb33SMartin Willi 	if (!nla)
8594447bb33SMartin Willi 		return -EMSGSIZE;
8604447bb33SMartin Willi 
8614447bb33SMartin Willi 	algo = nla_data(nla);
8624c87308bSMathias Krause 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
8634447bb33SMartin Willi 	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
8644447bb33SMartin Willi 	algo->alg_key_len = auth->alg_key_len;
8654447bb33SMartin Willi 
8664447bb33SMartin Willi 	return 0;
8674447bb33SMartin Willi }
8684447bb33SMartin Willi 
8699b42c1f1SSteffen Klassert static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
8709b42c1f1SSteffen Klassert {
8719b42c1f1SSteffen Klassert 	int ret = 0;
8729b42c1f1SSteffen Klassert 
8739b42c1f1SSteffen Klassert 	if (m->v | m->m) {
8749b42c1f1SSteffen Klassert 		ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
8759b42c1f1SSteffen Klassert 		if (!ret)
8769b42c1f1SSteffen Klassert 			ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
8779b42c1f1SSteffen Klassert 	}
8789b42c1f1SSteffen Klassert 	return ret;
8799b42c1f1SSteffen Klassert }
8809b42c1f1SSteffen Klassert 
88168325d3bSHerbert Xu /* Don't change this without updating xfrm_sa_len! */
88268325d3bSHerbert Xu static int copy_to_user_state_extra(struct xfrm_state *x,
88368325d3bSHerbert Xu 				    struct xfrm_usersa_info *p,
88468325d3bSHerbert Xu 				    struct sk_buff *skb)
8851da177e4SLinus Torvalds {
8861d1e34ddSDavid S. Miller 	int ret = 0;
8871d1e34ddSDavid S. Miller 
8881da177e4SLinus Torvalds 	copy_to_user_state(x, p);
8891da177e4SLinus Torvalds 
890a947b0a9SNicolas Dichtel 	if (x->props.extra_flags) {
891a947b0a9SNicolas Dichtel 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
892a947b0a9SNicolas Dichtel 				  x->props.extra_flags);
893a947b0a9SNicolas Dichtel 		if (ret)
894a947b0a9SNicolas Dichtel 			goto out;
895a947b0a9SNicolas Dichtel 	}
896a947b0a9SNicolas Dichtel 
8971d1e34ddSDavid S. Miller 	if (x->coaddr) {
8981d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
8991d1e34ddSDavid S. Miller 		if (ret)
9001d1e34ddSDavid S. Miller 			goto out;
9011d1e34ddSDavid S. Miller 	}
9021d1e34ddSDavid S. Miller 	if (x->lastused) {
903de95c4a4SNicolas Dichtel 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
904de95c4a4SNicolas Dichtel 					XFRMA_PAD);
9051d1e34ddSDavid S. Miller 		if (ret)
9061d1e34ddSDavid S. Miller 			goto out;
9071d1e34ddSDavid S. Miller 	}
9081d1e34ddSDavid S. Miller 	if (x->aead) {
9091d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
9101d1e34ddSDavid S. Miller 		if (ret)
9111d1e34ddSDavid S. Miller 			goto out;
9121d1e34ddSDavid S. Miller 	}
9131d1e34ddSDavid S. Miller 	if (x->aalg) {
9141d1e34ddSDavid S. Miller 		ret = copy_to_user_auth(x->aalg, skb);
9151d1e34ddSDavid S. Miller 		if (!ret)
9161d1e34ddSDavid S. Miller 			ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
9171d1e34ddSDavid S. Miller 				      xfrm_alg_auth_len(x->aalg), x->aalg);
9181d1e34ddSDavid S. Miller 		if (ret)
9191d1e34ddSDavid S. Miller 			goto out;
9201d1e34ddSDavid S. Miller 	}
9211d1e34ddSDavid S. Miller 	if (x->ealg) {
9221d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
9231d1e34ddSDavid S. Miller 		if (ret)
9241d1e34ddSDavid S. Miller 			goto out;
9251d1e34ddSDavid S. Miller 	}
9261d1e34ddSDavid S. Miller 	if (x->calg) {
9271d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
9281d1e34ddSDavid S. Miller 		if (ret)
9291d1e34ddSDavid S. Miller 			goto out;
9301d1e34ddSDavid S. Miller 	}
9311d1e34ddSDavid S. Miller 	if (x->encap) {
9321d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
9331d1e34ddSDavid S. Miller 		if (ret)
9341d1e34ddSDavid S. Miller 			goto out;
9351d1e34ddSDavid S. Miller 	}
9361d1e34ddSDavid S. Miller 	if (x->tfcpad) {
9371d1e34ddSDavid S. Miller 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
9381d1e34ddSDavid S. Miller 		if (ret)
9391d1e34ddSDavid S. Miller 			goto out;
9401d1e34ddSDavid S. Miller 	}
9411d1e34ddSDavid S. Miller 	ret = xfrm_mark_put(skb, &x->mark);
9421d1e34ddSDavid S. Miller 	if (ret)
9431d1e34ddSDavid S. Miller 		goto out;
9449b42c1f1SSteffen Klassert 
9459b42c1f1SSteffen Klassert 	ret = xfrm_smark_put(skb, &x->props.smark);
9469b42c1f1SSteffen Klassert 	if (ret)
9479b42c1f1SSteffen Klassert 		goto out;
9489b42c1f1SSteffen Klassert 
949f293a5e3Sdingzhi 	if (x->replay_esn)
9501d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
951d0fde795SDavid S. Miller 			      xfrm_replay_state_esn_len(x->replay_esn),
9521d1e34ddSDavid S. Miller 			      x->replay_esn);
953f293a5e3Sdingzhi 	else
954f293a5e3Sdingzhi 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
955f293a5e3Sdingzhi 			      &x->replay);
9561d1e34ddSDavid S. Miller 	if (ret)
9571d1e34ddSDavid S. Miller 		goto out;
958d77e38e6SSteffen Klassert 	if(x->xso.dev)
959d77e38e6SSteffen Klassert 		ret = copy_user_offload(&x->xso, skb);
960d77e38e6SSteffen Klassert 	if (ret)
961d77e38e6SSteffen Klassert 		goto out;
9627e652640SSteffen Klassert 	if (x->if_id) {
9637e652640SSteffen Klassert 		ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
964077fbac4SLorenzo Colitti 		if (ret)
965077fbac4SLorenzo Colitti 			goto out;
966077fbac4SLorenzo Colitti 	}
9678598112dSSteffen Klassert 	if (x->security)
9688598112dSSteffen Klassert 		ret = copy_sec_ctx(x->security, skb);
9691d1e34ddSDavid S. Miller out:
9701d1e34ddSDavid S. Miller 	return ret;
97168325d3bSHerbert Xu }
97268325d3bSHerbert Xu 
97368325d3bSHerbert Xu static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
97468325d3bSHerbert Xu {
97568325d3bSHerbert Xu 	struct xfrm_dump_info *sp = ptr;
97668325d3bSHerbert Xu 	struct sk_buff *in_skb = sp->in_skb;
97768325d3bSHerbert Xu 	struct sk_buff *skb = sp->out_skb;
978*5f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
97968325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
98068325d3bSHerbert Xu 	struct nlmsghdr *nlh;
98168325d3bSHerbert Xu 	int err;
98268325d3bSHerbert Xu 
98315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
98468325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
98568325d3bSHerbert Xu 	if (nlh == NULL)
98668325d3bSHerbert Xu 		return -EMSGSIZE;
98768325d3bSHerbert Xu 
98868325d3bSHerbert Xu 	p = nlmsg_data(nlh);
98968325d3bSHerbert Xu 
99068325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
9911d1e34ddSDavid S. Miller 	if (err) {
9929825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
99368325d3bSHerbert Xu 		return err;
9941da177e4SLinus Torvalds 	}
9951d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
996*5f3eea6bSDmitry Safonov 
997*5f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
998*5f3eea6bSDmitry Safonov 	if (xtr) {
999*5f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlh);
1000*5f3eea6bSDmitry Safonov 
1001*5f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
1002*5f3eea6bSDmitry Safonov 		if (err) {
1003*5f3eea6bSDmitry Safonov 			nlmsg_cancel(skb, nlh);
1004*5f3eea6bSDmitry Safonov 			return err;
1005*5f3eea6bSDmitry Safonov 		}
1006*5f3eea6bSDmitry Safonov 	}
1007*5f3eea6bSDmitry Safonov 
10081d1e34ddSDavid S. Miller 	return 0;
10091d1e34ddSDavid S. Miller }
10101da177e4SLinus Torvalds 
10114c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
10124c563f76STimo Teras {
10134c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1014283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
1015283bc9f3SFan Du 	struct net *net = sock_net(sk);
1016283bc9f3SFan Du 
10171ba5bf99SVegard Nossum 	if (cb->args[0])
1018283bc9f3SFan Du 		xfrm_state_walk_done(walk, net);
10194c563f76STimo Teras 	return 0;
10204c563f76STimo Teras }
10214c563f76STimo Teras 
1022d3623099SNicolas Dichtel static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
10231da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
10241da177e4SLinus Torvalds {
1025fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
10264c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
10271da177e4SLinus Torvalds 	struct xfrm_dump_info info;
10281da177e4SLinus Torvalds 
10294c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
10304c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
10314c563f76STimo Teras 
10321da177e4SLinus Torvalds 	info.in_skb = cb->skb;
10331da177e4SLinus Torvalds 	info.out_skb = skb;
10341da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
10351da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
10364c563f76STimo Teras 
10374c563f76STimo Teras 	if (!cb->args[0]) {
1038d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
1039870a2df4SNicolas Dichtel 		struct xfrm_address_filter *filter = NULL;
1040d3623099SNicolas Dichtel 		u8 proto = 0;
1041d3623099SNicolas Dichtel 		int err;
1042d3623099SNicolas Dichtel 
10438cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
10448cb08174SJohannes Berg 					     xfrma_policy, cb->extack);
1045d3623099SNicolas Dichtel 		if (err < 0)
1046d3623099SNicolas Dichtel 			return err;
1047d3623099SNicolas Dichtel 
1048870a2df4SNicolas Dichtel 		if (attrs[XFRMA_ADDRESS_FILTER]) {
1049df367561SAndrzej Hajda 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1050df367561SAndrzej Hajda 					 sizeof(*filter), GFP_KERNEL);
1051d3623099SNicolas Dichtel 			if (filter == NULL)
1052d3623099SNicolas Dichtel 				return -ENOMEM;
1053d3623099SNicolas Dichtel 		}
1054d3623099SNicolas Dichtel 
1055d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
1056d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
1057d3623099SNicolas Dichtel 
1058d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
10591ba5bf99SVegard Nossum 		cb->args[0] = 1;
10604c563f76STimo Teras 	}
10614c563f76STimo Teras 
1062fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 	return skb->len;
10651da177e4SLinus Torvalds }
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
10681da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
10691da177e4SLinus Torvalds {
10701da177e4SLinus Torvalds 	struct xfrm_dump_info info;
10711da177e4SLinus Torvalds 	struct sk_buff *skb;
1072864745d2SMathias Krause 	int err;
10731da177e4SLinus Torvalds 
10747deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
10751da177e4SLinus Torvalds 	if (!skb)
10761da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	info.in_skb = in_skb;
10791da177e4SLinus Torvalds 	info.out_skb = skb;
10801da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
10811da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
10821da177e4SLinus Torvalds 
1083864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
1084864745d2SMathias Krause 	if (err) {
10851da177e4SLinus Torvalds 		kfree_skb(skb);
1086864745d2SMathias Krause 		return ERR_PTR(err);
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	return skb;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
109221ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
109321ee543eSMichal Kubecek  * Must be called with RCU read lock.
109421ee543eSMichal Kubecek  */
109521ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
109621ee543eSMichal Kubecek 				       u32 pid, unsigned int group)
109721ee543eSMichal Kubecek {
109821ee543eSMichal Kubecek 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
10995461fc0cSDmitry Safonov 	struct xfrm_translator *xtr;
110021ee543eSMichal Kubecek 
110186126b77SFlorian Westphal 	if (!nlsk) {
110286126b77SFlorian Westphal 		kfree_skb(skb);
110386126b77SFlorian Westphal 		return -EPIPE;
110486126b77SFlorian Westphal 	}
110586126b77SFlorian Westphal 
11065461fc0cSDmitry Safonov 	xtr = xfrm_get_translator();
11075461fc0cSDmitry Safonov 	if (xtr) {
11085461fc0cSDmitry Safonov 		int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
11095461fc0cSDmitry Safonov 
11105461fc0cSDmitry Safonov 		xfrm_put_translator(xtr);
11115461fc0cSDmitry Safonov 		if (err) {
11125461fc0cSDmitry Safonov 			kfree_skb(skb);
11135461fc0cSDmitry Safonov 			return err;
11145461fc0cSDmitry Safonov 		}
11155461fc0cSDmitry Safonov 	}
11165461fc0cSDmitry Safonov 
111721ee543eSMichal Kubecek 	return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
111821ee543eSMichal Kubecek }
111921ee543eSMichal Kubecek 
1120a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void)
11217deb2264SThomas Graf {
11227deb2264SThomas Graf 	return NLMSG_ALIGN(4)
11237deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1124880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1125880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1126880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
11277deb2264SThomas Graf }
11287deb2264SThomas Graf 
1129e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
113015e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
1131ecfd6b18SJamal Hadi Salim {
11325a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
11335a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
11345a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1135880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1136ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
11371d1e34ddSDavid S. Miller 	int err;
1138ecfd6b18SJamal Hadi Salim 	u32 *f;
1139880a6fabSChristophe Gouault 	unsigned lseq;
1140ecfd6b18SJamal Hadi Salim 
114115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
114225985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1143ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1144ecfd6b18SJamal Hadi Salim 
1145ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1146ecfd6b18SJamal Hadi Salim 	*f = flags;
1147e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
11485a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
11495a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
11505a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
11515a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
11525a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
11535a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
11545a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
11555a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1156ecfd6b18SJamal Hadi Salim 
1157880a6fabSChristophe Gouault 	do {
1158880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1159880a6fabSChristophe Gouault 
1160880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1161880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1162880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1163880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1164880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1165880a6fabSChristophe Gouault 
11661d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
11671d1e34ddSDavid S. Miller 	if (!err)
11681d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1169880a6fabSChristophe Gouault 	if (!err)
1170880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1171880a6fabSChristophe Gouault 	if (!err)
1172880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
11731d1e34ddSDavid S. Miller 	if (err) {
11741d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
11751d1e34ddSDavid S. Miller 		return err;
11761d1e34ddSDavid S. Miller 	}
1177ecfd6b18SJamal Hadi Salim 
1178053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1179053c095aSJohannes Berg 	return 0;
1180ecfd6b18SJamal Hadi Salim }
1181ecfd6b18SJamal Hadi Salim 
1182880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1183880a6fabSChristophe Gouault 			    struct nlattr **attrs)
1184880a6fabSChristophe Gouault {
1185880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1186880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1187880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1188880a6fabSChristophe Gouault 
1189880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1190880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1191880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1192880a6fabSChristophe Gouault 
1193880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1194880a6fabSChristophe Gouault 			return -EINVAL;
1195880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1196880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1197880a6fabSChristophe Gouault 			return -EINVAL;
1198880a6fabSChristophe Gouault 	}
1199880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1200880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1201880a6fabSChristophe Gouault 
1202880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1203880a6fabSChristophe Gouault 			return -EINVAL;
1204880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1205880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1206880a6fabSChristophe Gouault 			return -EINVAL;
1207880a6fabSChristophe Gouault 	}
1208880a6fabSChristophe Gouault 
1209880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1210880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1211880a6fabSChristophe Gouault 		if (thresh4) {
1212880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1213880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1214880a6fabSChristophe Gouault 		}
1215880a6fabSChristophe Gouault 		if (thresh6) {
1216880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1217880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1218880a6fabSChristophe Gouault 		}
1219880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1220880a6fabSChristophe Gouault 
1221880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1222880a6fabSChristophe Gouault 	}
1223880a6fabSChristophe Gouault 
1224880a6fabSChristophe Gouault 	return 0;
1225880a6fabSChristophe Gouault }
1226880a6fabSChristophe Gouault 
1227ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12285424f32eSThomas Graf 		struct nlattr **attrs)
1229ecfd6b18SJamal Hadi Salim {
1230a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1231ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
12327b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
123315e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1234ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
12352fc5f83bSGustavo A. R. Silva 	int err;
1236ecfd6b18SJamal Hadi Salim 
12377deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1238ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1239ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1240ecfd6b18SJamal Hadi Salim 
12412fc5f83bSGustavo A. R. Silva 	err = build_spdinfo(r_skb, net, sportid, seq, *flags);
12422fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
1243ecfd6b18SJamal Hadi Salim 
124415e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1245ecfd6b18SJamal Hadi Salim }
1246ecfd6b18SJamal Hadi Salim 
1247a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void)
12487deb2264SThomas Graf {
12497deb2264SThomas Graf 	return NLMSG_ALIGN(4)
12507deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
12517deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
12527deb2264SThomas Graf }
12537deb2264SThomas Graf 
1254e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
125515e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
125628d8909bSJamal Hadi Salim {
1257af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1258af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
125928d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
12601d1e34ddSDavid S. Miller 	int err;
126128d8909bSJamal Hadi Salim 	u32 *f;
126228d8909bSJamal Hadi Salim 
126315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
126425985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
126528d8909bSJamal Hadi Salim 		return -EMSGSIZE;
126628d8909bSJamal Hadi Salim 
126728d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
126828d8909bSJamal Hadi Salim 	*f = flags;
1269e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
127028d8909bSJamal Hadi Salim 
1271af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1272af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1273af11e316SJamal Hadi Salim 
12741d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
12751d1e34ddSDavid S. Miller 	if (!err)
12761d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
12771d1e34ddSDavid S. Miller 	if (err) {
12781d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
12791d1e34ddSDavid S. Miller 		return err;
12801d1e34ddSDavid S. Miller 	}
128128d8909bSJamal Hadi Salim 
1282053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1283053c095aSJohannes Berg 	return 0;
128428d8909bSJamal Hadi Salim }
128528d8909bSJamal Hadi Salim 
128628d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12875424f32eSThomas Graf 		struct nlattr **attrs)
128828d8909bSJamal Hadi Salim {
1289a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
129028d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
12917b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
129215e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
129328d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
12942fc5f83bSGustavo A. R. Silva 	int err;
129528d8909bSJamal Hadi Salim 
12967deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
129728d8909bSJamal Hadi Salim 	if (r_skb == NULL)
129828d8909bSJamal Hadi Salim 		return -ENOMEM;
129928d8909bSJamal Hadi Salim 
13002fc5f83bSGustavo A. R. Silva 	err = build_sadinfo(r_skb, net, sportid, seq, *flags);
13012fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
130228d8909bSJamal Hadi Salim 
130315e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
130428d8909bSJamal Hadi Salim }
130528d8909bSJamal Hadi Salim 
130622e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
13075424f32eSThomas Graf 		struct nlattr **attrs)
13081da177e4SLinus Torvalds {
1309fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
13107b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
13111da177e4SLinus Torvalds 	struct xfrm_state *x;
13121da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1313eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
13141da177e4SLinus Torvalds 
1315fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
13161da177e4SLinus Torvalds 	if (x == NULL)
13171da177e4SLinus Torvalds 		goto out_noput;
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
13201da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
13211da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
13221da177e4SLinus Torvalds 	} else {
132315e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
13241da177e4SLinus Torvalds 	}
13251da177e4SLinus Torvalds 	xfrm_state_put(x);
13261da177e4SLinus Torvalds out_noput:
13271da177e4SLinus Torvalds 	return err;
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
133022e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
13315424f32eSThomas Graf 		struct nlattr **attrs)
13321da177e4SLinus Torvalds {
1333fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
13341da177e4SLinus Torvalds 	struct xfrm_state *x;
13351da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
1336*5f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
13371da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
13381da177e4SLinus Torvalds 	xfrm_address_t *daddr;
13391da177e4SLinus Torvalds 	int family;
13401da177e4SLinus Torvalds 	int err;
13416f26b61eSJamal Hadi Salim 	u32 mark;
13426f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
13437e652640SSteffen Klassert 	u32 if_id = 0;
13441da177e4SLinus Torvalds 
13457b67c857SThomas Graf 	p = nlmsg_data(nlh);
1346776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
13471da177e4SLinus Torvalds 	if (err)
13481da177e4SLinus Torvalds 		goto out_noput;
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	family = p->info.family;
13511da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	x = NULL;
13546f26b61eSJamal Hadi Salim 
13556f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
13567e652640SSteffen Klassert 
13577e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
13587e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
13597e652640SSteffen Klassert 
13601da177e4SLinus Torvalds 	if (p->info.seq) {
13616f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
136270e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
13631da177e4SLinus Torvalds 			xfrm_state_put(x);
13641da177e4SLinus Torvalds 			x = NULL;
13651da177e4SLinus Torvalds 		}
13661da177e4SLinus Torvalds 	}
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	if (!x)
13696f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
13707e652640SSteffen Klassert 				  if_id, p->info.id.proto, daddr,
13711da177e4SLinus Torvalds 				  &p->info.saddr, 1,
13721da177e4SLinus Torvalds 				  family);
13731da177e4SLinus Torvalds 	err = -ENOENT;
13741da177e4SLinus Torvalds 	if (x == NULL)
13751da177e4SLinus Torvalds 		goto out_noput;
13761da177e4SLinus Torvalds 
1377658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1378658b219eSHerbert Xu 	if (err)
1379658b219eSHerbert Xu 		goto out;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
13821da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
13831da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
13841da177e4SLinus Torvalds 		goto out;
13851da177e4SLinus Torvalds 	}
13861da177e4SLinus Torvalds 
1387*5f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
1388*5f3eea6bSDmitry Safonov 	if (xtr) {
1389*5f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1390*5f3eea6bSDmitry Safonov 
1391*5f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
1392*5f3eea6bSDmitry Safonov 		if (err) {
1393*5f3eea6bSDmitry Safonov 			kfree_skb(resp_skb);
1394*5f3eea6bSDmitry Safonov 			goto out;
1395*5f3eea6bSDmitry Safonov 		}
1396*5f3eea6bSDmitry Safonov 	}
1397*5f3eea6bSDmitry Safonov 
139815e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds out:
14011da177e4SLinus Torvalds 	xfrm_state_put(x);
14021da177e4SLinus Torvalds out_noput:
14031da177e4SLinus Torvalds 	return err;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
1406b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
14071da177e4SLinus Torvalds {
14081da177e4SLinus Torvalds 	switch (dir) {
14091da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
14101da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
14111da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
14121da177e4SLinus Torvalds 		break;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	default:
14151da177e4SLinus Torvalds 		return -EINVAL;
14163ff50b79SStephen Hemminger 	}
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	return 0;
14191da177e4SLinus Torvalds }
14201da177e4SLinus Torvalds 
1421b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1422f7b6983fSMasahide NAKAMURA {
1423f7b6983fSMasahide NAKAMURA 	switch (type) {
1424f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1425f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1426f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1427f7b6983fSMasahide NAKAMURA #endif
1428f7b6983fSMasahide NAKAMURA 		break;
1429f7b6983fSMasahide NAKAMURA 
1430f7b6983fSMasahide NAKAMURA 	default:
1431f7b6983fSMasahide NAKAMURA 		return -EINVAL;
14323ff50b79SStephen Hemminger 	}
1433f7b6983fSMasahide NAKAMURA 
1434f7b6983fSMasahide NAKAMURA 	return 0;
1435f7b6983fSMasahide NAKAMURA }
1436f7b6983fSMasahide NAKAMURA 
14371da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
14381da177e4SLinus Torvalds {
1439e682adf0SFan Du 	int ret;
1440e682adf0SFan Du 
14411da177e4SLinus Torvalds 	switch (p->share) {
14421da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
14431da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
14441da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
14451da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
14461da177e4SLinus Torvalds 		break;
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	default:
14491da177e4SLinus Torvalds 		return -EINVAL;
14503ff50b79SStephen Hemminger 	}
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 	switch (p->action) {
14531da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
14541da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
14551da177e4SLinus Torvalds 		break;
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	default:
14581da177e4SLinus Torvalds 		return -EINVAL;
14593ff50b79SStephen Hemminger 	}
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	switch (p->sel.family) {
14621da177e4SLinus Torvalds 	case AF_INET:
146307bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
146407bf7908SSteffen Klassert 			return -EINVAL;
146507bf7908SSteffen Klassert 
14661da177e4SLinus Torvalds 		break;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	case AF_INET6:
1469dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
147007bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
147107bf7908SSteffen Klassert 			return -EINVAL;
147207bf7908SSteffen Klassert 
14731da177e4SLinus Torvalds 		break;
14741da177e4SLinus Torvalds #else
14751da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
14761da177e4SLinus Torvalds #endif
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	default:
14791da177e4SLinus Torvalds 		return -EINVAL;
14803ff50b79SStephen Hemminger 	}
14811da177e4SLinus Torvalds 
1482e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1483e682adf0SFan Du 	if (ret)
1484e682adf0SFan Du 		return ret;
1485b805d78dSYueHaibing 	if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1486e682adf0SFan Du 		return -EINVAL;
1487e682adf0SFan Du 
1488e682adf0SFan Du 	return 0;
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds 
14915424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1492df71837dSTrent Jaeger {
14935424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1494df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1495df71837dSTrent Jaeger 
1496df71837dSTrent Jaeger 	if (!rt)
1497df71837dSTrent Jaeger 		return 0;
1498df71837dSTrent Jaeger 
14995424f32eSThomas Graf 	uctx = nla_data(rt);
150052a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1501df71837dSTrent Jaeger }
1502df71837dSTrent Jaeger 
15031da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
15041da177e4SLinus Torvalds 			   int nr)
15051da177e4SLinus Torvalds {
15061da177e4SLinus Torvalds 	int i;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
15091da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
15101da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
15131da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
15141da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
15151da177e4SLinus Torvalds 		t->reqid = ut->reqid;
15161da177e4SLinus Torvalds 		t->mode = ut->mode;
15171da177e4SLinus Torvalds 		t->share = ut->share;
15181da177e4SLinus Torvalds 		t->optional = ut->optional;
15191da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
15201da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
15211da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1522c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1523c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
15248511d01dSMiika Komu 		t->encap_family = ut->family;
15251da177e4SLinus Torvalds 	}
15261da177e4SLinus Torvalds }
15271da177e4SLinus Torvalds 
1528b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1529b4ad86bfSDavid S. Miller {
1530732706afSSteffen Klassert 	u16 prev_family;
1531b4ad86bfSDavid S. Miller 	int i;
1532b4ad86bfSDavid S. Miller 
1533b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1534b4ad86bfSDavid S. Miller 		return -EINVAL;
1535b4ad86bfSDavid S. Miller 
1536732706afSSteffen Klassert 	prev_family = family;
1537732706afSSteffen Klassert 
1538b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1539b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1540b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1541b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1542b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1543b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1544b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1545b4ad86bfSDavid S. Miller 		 */
1546b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1547b4ad86bfSDavid S. Miller 			ut[i].family = family;
1548b4ad86bfSDavid S. Miller 
154935e61038SFlorian Westphal 		switch (ut[i].mode) {
155035e61038SFlorian Westphal 		case XFRM_MODE_TUNNEL:
155135e61038SFlorian Westphal 		case XFRM_MODE_BEET:
155235e61038SFlorian Westphal 			break;
155335e61038SFlorian Westphal 		default:
155435e61038SFlorian Westphal 			if (ut[i].family != prev_family)
1555732706afSSteffen Klassert 				return -EINVAL;
155635e61038SFlorian Westphal 			break;
155735e61038SFlorian Westphal 		}
155832bf94fbSSean Tranchetti 		if (ut[i].mode >= XFRM_MODE_MAX)
155932bf94fbSSean Tranchetti 			return -EINVAL;
156032bf94fbSSean Tranchetti 
1561732706afSSteffen Klassert 		prev_family = ut[i].family;
1562732706afSSteffen Klassert 
1563b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1564b4ad86bfSDavid S. Miller 		case AF_INET:
1565b4ad86bfSDavid S. Miller 			break;
1566dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1567b4ad86bfSDavid S. Miller 		case AF_INET6:
1568b4ad86bfSDavid S. Miller 			break;
1569b4ad86bfSDavid S. Miller #endif
1570b4ad86bfSDavid S. Miller 		default:
1571b4ad86bfSDavid S. Miller 			return -EINVAL;
15723ff50b79SStephen Hemminger 		}
15736a53b759SCong Wang 
1574dbb2483bSCong Wang 		if (!xfrm_id_proto_valid(ut[i].id.proto))
15756a53b759SCong Wang 			return -EINVAL;
15766a53b759SCong Wang 	}
15776a53b759SCong Wang 
1578b4ad86bfSDavid S. Miller 	return 0;
1579b4ad86bfSDavid S. Miller }
1580b4ad86bfSDavid S. Miller 
15815424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
15821da177e4SLinus Torvalds {
15835424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 	if (!rt) {
15861da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
15871da177e4SLinus Torvalds 	} else {
15885424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
15895424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1590b4ad86bfSDavid S. Miller 		int err;
15911da177e4SLinus Torvalds 
1592b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1593b4ad86bfSDavid S. Miller 		if (err)
1594b4ad86bfSDavid S. Miller 			return err;
15951da177e4SLinus Torvalds 
15965424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
15971da177e4SLinus Torvalds 	}
15981da177e4SLinus Torvalds 	return 0;
15991da177e4SLinus Torvalds }
16001da177e4SLinus Torvalds 
16015424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1602f7b6983fSMasahide NAKAMURA {
16035424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1604f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1605b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1606f7b6983fSMasahide NAKAMURA 	int err;
1607f7b6983fSMasahide NAKAMURA 
1608f7b6983fSMasahide NAKAMURA 	if (rt) {
16095424f32eSThomas Graf 		upt = nla_data(rt);
1610f7b6983fSMasahide NAKAMURA 		type = upt->type;
1611f7b6983fSMasahide NAKAMURA 	}
1612f7b6983fSMasahide NAKAMURA 
1613f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1614f7b6983fSMasahide NAKAMURA 	if (err)
1615f7b6983fSMasahide NAKAMURA 		return err;
1616f7b6983fSMasahide NAKAMURA 
1617f7b6983fSMasahide NAKAMURA 	*tp = type;
1618f7b6983fSMasahide NAKAMURA 	return 0;
1619f7b6983fSMasahide NAKAMURA }
1620f7b6983fSMasahide NAKAMURA 
16211da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
16221da177e4SLinus Torvalds {
16231da177e4SLinus Torvalds 	xp->priority = p->priority;
16241da177e4SLinus Torvalds 	xp->index = p->index;
16251da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
16261da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
16271da177e4SLinus Torvalds 	xp->action = p->action;
16281da177e4SLinus Torvalds 	xp->flags = p->flags;
16291da177e4SLinus Torvalds 	xp->family = p->sel.family;
16301da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
16311da177e4SLinus Torvalds }
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
16341da177e4SLinus Torvalds {
16357b789836SMathias Krause 	memset(p, 0, sizeof(*p));
16361da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
16371da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
16381da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
16391da177e4SLinus Torvalds 	p->priority = xp->priority;
16401da177e4SLinus Torvalds 	p->index = xp->index;
16411da177e4SLinus Torvalds 	p->sel.family = xp->family;
16421da177e4SLinus Torvalds 	p->dir = dir;
16431da177e4SLinus Torvalds 	p->action = xp->action;
16441da177e4SLinus Torvalds 	p->flags = xp->flags;
16451da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
16461da177e4SLinus Torvalds }
16471da177e4SLinus Torvalds 
1648fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
16491da177e4SLinus Torvalds {
1650fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
16511da177e4SLinus Torvalds 	int err;
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	if (!xp) {
16541da177e4SLinus Torvalds 		*errp = -ENOMEM;
16551da177e4SLinus Torvalds 		return NULL;
16561da177e4SLinus Torvalds 	}
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1659df71837dSTrent Jaeger 
166035a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1661f7b6983fSMasahide NAKAMURA 	if (err)
1662f7b6983fSMasahide NAKAMURA 		goto error;
1663f7b6983fSMasahide NAKAMURA 
166435a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
166535a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1666f7b6983fSMasahide NAKAMURA 	if (err)
1667f7b6983fSMasahide NAKAMURA 		goto error;
16681da177e4SLinus Torvalds 
1669295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1670295fae56SJamal Hadi Salim 
16717e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
16727e652640SSteffen Klassert 		xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
16737e652640SSteffen Klassert 
16741da177e4SLinus Torvalds 	return xp;
1675f7b6983fSMasahide NAKAMURA  error:
1676f7b6983fSMasahide NAKAMURA 	*errp = err;
167712a169e7SHerbert Xu 	xp->walk.dead = 1;
167864c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1679f7b6983fSMasahide NAKAMURA 	return NULL;
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
168222e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
16835424f32eSThomas Graf 		struct nlattr **attrs)
16841da177e4SLinus Torvalds {
1685fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
16867b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
16871da177e4SLinus Torvalds 	struct xfrm_policy *xp;
168826b15dadSJamal Hadi Salim 	struct km_event c;
16891da177e4SLinus Torvalds 	int err;
16901da177e4SLinus Torvalds 	int excl;
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
16931da177e4SLinus Torvalds 	if (err)
16941da177e4SLinus Torvalds 		return err;
169535a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1696df71837dSTrent Jaeger 	if (err)
1697df71837dSTrent Jaeger 		return err;
16981da177e4SLinus Torvalds 
1699fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
17001da177e4SLinus Torvalds 	if (!xp)
17011da177e4SLinus Torvalds 		return err;
17021da177e4SLinus Torvalds 
170325985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
170426b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
170526b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
170626b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
17071da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
17081da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
17092e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1710161a09e7SJoy Latten 
17111da177e4SLinus Torvalds 	if (err) {
171203e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
17131da177e4SLinus Torvalds 		kfree(xp);
17141da177e4SLinus Torvalds 		return err;
17151da177e4SLinus Torvalds 	}
17161da177e4SLinus Torvalds 
1717f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
171826b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
171915e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
172026b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
172126b15dadSJamal Hadi Salim 
17221da177e4SLinus Torvalds 	xfrm_pol_put(xp);
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	return 0;
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
17281da177e4SLinus Torvalds {
17291da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
17301da177e4SLinus Torvalds 	int i;
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
17331da177e4SLinus Torvalds 		return 0;
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
17361da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
17371da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
17381da177e4SLinus Torvalds 
17391f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
17401da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
17418511d01dSMiika Komu 		up->family = kp->encap_family;
17421da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
17431da177e4SLinus Torvalds 		up->reqid = kp->reqid;
17441da177e4SLinus Torvalds 		up->mode = kp->mode;
17451da177e4SLinus Torvalds 		up->share = kp->share;
17461da177e4SLinus Torvalds 		up->optional = kp->optional;
17471da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
17481da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
17491da177e4SLinus Torvalds 		up->calgos = kp->calgos;
17501da177e4SLinus Torvalds 	}
17511da177e4SLinus Torvalds 
1752c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1753c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1754df71837dSTrent Jaeger }
1755df71837dSTrent Jaeger 
17560d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
17570d681623SSerge Hallyn {
17580d681623SSerge Hallyn 	if (x->security) {
17590d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
17600d681623SSerge Hallyn 	}
17610d681623SSerge Hallyn 	return 0;
17620d681623SSerge Hallyn }
17630d681623SSerge Hallyn 
17640d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
17650d681623SSerge Hallyn {
17661d1e34ddSDavid S. Miller 	if (xp->security)
17670d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
17680d681623SSerge Hallyn 	return 0;
17690d681623SSerge Hallyn }
1770a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void)
1771cfbfd45aSThomas Graf {
1772cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1773cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1774cfbfd45aSThomas Graf #else
1775cfbfd45aSThomas Graf 	return 0;
1776cfbfd45aSThomas Graf #endif
1777cfbfd45aSThomas Graf }
17780d681623SSerge Hallyn 
1779f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1780b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1781f7b6983fSMasahide NAKAMURA {
178245c180bcSEric Dumazet 	struct xfrm_userpolicy_type upt;
178345c180bcSEric Dumazet 
178445c180bcSEric Dumazet 	/* Sadly there are two holes in struct xfrm_userpolicy_type */
178545c180bcSEric Dumazet 	memset(&upt, 0, sizeof(upt));
178645c180bcSEric Dumazet 	upt.type = type;
1787f7b6983fSMasahide NAKAMURA 
1788c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1789f7b6983fSMasahide NAKAMURA }
1790f7b6983fSMasahide NAKAMURA 
1791f7b6983fSMasahide NAKAMURA #else
1792b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1793f7b6983fSMasahide NAKAMURA {
1794f7b6983fSMasahide NAKAMURA 	return 0;
1795f7b6983fSMasahide NAKAMURA }
1796f7b6983fSMasahide NAKAMURA #endif
1797f7b6983fSMasahide NAKAMURA 
17981da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
17991da177e4SLinus Torvalds {
18001da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
18011da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
18021da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
18031da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
1804*5f3eea6bSDmitry Safonov 	struct xfrm_translator *xtr;
18051da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
18061d1e34ddSDavid S. Miller 	int err;
18071da177e4SLinus Torvalds 
180815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
180979b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
181079b8b7f4SThomas Graf 	if (nlh == NULL)
181179b8b7f4SThomas Graf 		return -EMSGSIZE;
18121da177e4SLinus Torvalds 
18137b67c857SThomas Graf 	p = nlmsg_data(nlh);
18141da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
18151d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
18161d1e34ddSDavid S. Miller 	if (!err)
18171d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
18181d1e34ddSDavid S. Miller 	if (!err)
18191d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
18201d1e34ddSDavid S. Miller 	if (!err)
18211d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
18227e652640SSteffen Klassert 	if (!err)
18237e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
18241d1e34ddSDavid S. Miller 	if (err) {
18251d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
18261d1e34ddSDavid S. Miller 		return err;
18271d1e34ddSDavid S. Miller 	}
18289825069dSThomas Graf 	nlmsg_end(skb, nlh);
1829*5f3eea6bSDmitry Safonov 
1830*5f3eea6bSDmitry Safonov 	xtr = xfrm_get_translator();
1831*5f3eea6bSDmitry Safonov 	if (xtr) {
1832*5f3eea6bSDmitry Safonov 		err = xtr->alloc_compat(skb, nlh);
1833*5f3eea6bSDmitry Safonov 
1834*5f3eea6bSDmitry Safonov 		xfrm_put_translator(xtr);
1835*5f3eea6bSDmitry Safonov 		if (err) {
1836*5f3eea6bSDmitry Safonov 			nlmsg_cancel(skb, nlh);
1837*5f3eea6bSDmitry Safonov 			return err;
1838*5f3eea6bSDmitry Safonov 		}
1839*5f3eea6bSDmitry Safonov 	}
1840*5f3eea6bSDmitry Safonov 
18411da177e4SLinus Torvalds 	return 0;
18421da177e4SLinus Torvalds }
18431da177e4SLinus Torvalds 
18444c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
18454c563f76STimo Teras {
18461137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1847283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
18484c563f76STimo Teras 
1849283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
18504c563f76STimo Teras 	return 0;
18514c563f76STimo Teras }
18524c563f76STimo Teras 
18531137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb)
18541137b5e2SHerbert Xu {
18551137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
18561137b5e2SHerbert Xu 
18571137b5e2SHerbert Xu 	BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
18581137b5e2SHerbert Xu 
18591137b5e2SHerbert Xu 	xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
18601137b5e2SHerbert Xu 	return 0;
18611137b5e2SHerbert Xu }
18621137b5e2SHerbert Xu 
18631da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
18641da177e4SLinus Torvalds {
1865fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
18661137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
18671da177e4SLinus Torvalds 	struct xfrm_dump_info info;
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	info.in_skb = cb->skb;
18701da177e4SLinus Torvalds 	info.out_skb = skb;
18711da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
18721da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
18734c563f76STimo Teras 
1874fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds 	return skb->len;
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
18801da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
18811da177e4SLinus Torvalds 					  int dir, u32 seq)
18821da177e4SLinus Torvalds {
18831da177e4SLinus Torvalds 	struct xfrm_dump_info info;
18841da177e4SLinus Torvalds 	struct sk_buff *skb;
1885c2546372SMathias Krause 	int err;
18861da177e4SLinus Torvalds 
18877deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18881da177e4SLinus Torvalds 	if (!skb)
18891da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
18901da177e4SLinus Torvalds 
18911da177e4SLinus Torvalds 	info.in_skb = in_skb;
18921da177e4SLinus Torvalds 	info.out_skb = skb;
18931da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
18941da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
18951da177e4SLinus Torvalds 
1896c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1897c2546372SMathias Krause 	if (err) {
18981da177e4SLinus Torvalds 		kfree_skb(skb);
1899c2546372SMathias Krause 		return ERR_PTR(err);
19001da177e4SLinus Torvalds 	}
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 	return skb;
19031da177e4SLinus Torvalds }
19041da177e4SLinus Torvalds 
190522e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
19065424f32eSThomas Graf 		struct nlattr **attrs)
19071da177e4SLinus Torvalds {
1908fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
19091da177e4SLinus Torvalds 	struct xfrm_policy *xp;
19101da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1911b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
19121da177e4SLinus Torvalds 	int err;
191326b15dadSJamal Hadi Salim 	struct km_event c;
19141da177e4SLinus Torvalds 	int delete;
1915295fae56SJamal Hadi Salim 	struct xfrm_mark m;
19167e652640SSteffen Klassert 	u32 if_id = 0;
19171da177e4SLinus Torvalds 
19187b67c857SThomas Graf 	p = nlmsg_data(nlh);
19191da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
19201da177e4SLinus Torvalds 
192135a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1922f7b6983fSMasahide NAKAMURA 	if (err)
1923f7b6983fSMasahide NAKAMURA 		return err;
1924f7b6983fSMasahide NAKAMURA 
19251da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
19261da177e4SLinus Torvalds 	if (err)
19271da177e4SLinus Torvalds 		return err;
19281da177e4SLinus Torvalds 
19297e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
19307e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
19317e652640SSteffen Klassert 
19324f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
19334f47e8abSXin Long 
19341da177e4SLinus Torvalds 	if (p->index)
19354f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
19364f47e8abSXin Long 				      p->index, delete, &err);
1937df71837dSTrent Jaeger 	else {
19385424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
193903e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1940df71837dSTrent Jaeger 
194135a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
1942df71837dSTrent Jaeger 		if (err)
1943df71837dSTrent Jaeger 			return err;
1944df71837dSTrent Jaeger 
19452c8dd116SDenis V. Lunev 		ctx = NULL;
1946df71837dSTrent Jaeger 		if (rt) {
19475424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1948df71837dSTrent Jaeger 
194952a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
195003e1ad7bSPaul Moore 			if (err)
1951df71837dSTrent Jaeger 				return err;
19522c8dd116SDenis V. Lunev 		}
19534f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
19544f47e8abSXin Long 					   &p->sel, ctx, delete, &err);
195503e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
1956df71837dSTrent Jaeger 	}
19571da177e4SLinus Torvalds 	if (xp == NULL)
19581da177e4SLinus Torvalds 		return -ENOENT;
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	if (!delete) {
19611da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
19641da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
19651da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
19661da177e4SLinus Torvalds 		} else {
1967a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
196815e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
19691da177e4SLinus Torvalds 		}
197026b15dadSJamal Hadi Salim 	} else {
19712e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
197213fcfbb0SDavid S. Miller 
197313fcfbb0SDavid S. Miller 		if (err != 0)
1974c8c05a8eSCatherine Zhang 			goto out;
197513fcfbb0SDavid S. Miller 
1976e7443892SHerbert Xu 		c.data.byid = p->index;
1977f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
197826b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
197915e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
198026b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
19811da177e4SLinus Torvalds 	}
19821da177e4SLinus Torvalds 
1983c8c05a8eSCatherine Zhang out:
1984ef41aaa0SEric Paris 	xfrm_pol_put(xp);
19851da177e4SLinus Torvalds 	return err;
19861da177e4SLinus Torvalds }
19871da177e4SLinus Torvalds 
198822e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
19895424f32eSThomas Graf 		struct nlattr **attrs)
19901da177e4SLinus Torvalds {
1991fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
199226b15dadSJamal Hadi Salim 	struct km_event c;
19937b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
19944aa2e62cSJoy Latten 	int err;
19951da177e4SLinus Torvalds 
1996f75a2804SCong Wang 	err = xfrm_state_flush(net, p->proto, true, false);
19979e64cc95SJamal Hadi Salim 	if (err) {
19989e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
19999e64cc95SJamal Hadi Salim 			return 0;
2000069c474eSDavid S. Miller 		return err;
20019e64cc95SJamal Hadi Salim 	}
2002bf08867fSHerbert Xu 	c.data.proto = p->proto;
2003f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
200426b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
200515e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
20067067802eSAlexey Dobriyan 	c.net = net;
200726b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
200826b15dadSJamal Hadi Salim 
20091da177e4SLinus Torvalds 	return 0;
20101da177e4SLinus Torvalds }
20111da177e4SLinus Torvalds 
2012a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
20137deb2264SThomas Graf {
2014a1b831f2SAlexey Dobriyan 	unsigned int replay_size = x->replay_esn ?
2015d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
2016d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
2017d8647b79SSteffen Klassert 
20187deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2019d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
2020de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
20216f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
20227deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
20237deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
20247deb2264SThomas Graf }
2025d51d081dSJamal Hadi Salim 
2026214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2027d51d081dSJamal Hadi Salim {
2028d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
2029d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
20301d1e34ddSDavid S. Miller 	int err;
2031d51d081dSJamal Hadi Salim 
203215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
203379b8b7f4SThomas Graf 	if (nlh == NULL)
203479b8b7f4SThomas Graf 		return -EMSGSIZE;
2035d51d081dSJamal Hadi Salim 
20367b67c857SThomas Graf 	id = nlmsg_data(nlh);
2037931e79d7SMathias Krause 	memset(&id->sa_id, 0, sizeof(id->sa_id));
20382b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2039d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
2040d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
2041d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
20422b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
20432b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
2044d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
2045d51d081dSJamal Hadi Salim 
2046d0fde795SDavid S. Miller 	if (x->replay_esn) {
20471d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2048d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
20491d1e34ddSDavid S. Miller 			      x->replay_esn);
2050d0fde795SDavid S. Miller 	} else {
20511d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
20521d1e34ddSDavid S. Miller 			      &x->replay);
2053d0fde795SDavid S. Miller 	}
20541d1e34ddSDavid S. Miller 	if (err)
20551d1e34ddSDavid S. Miller 		goto out_cancel;
2056de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2057de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
20581d1e34ddSDavid S. Miller 	if (err)
20591d1e34ddSDavid S. Miller 		goto out_cancel;
2060d8647b79SSteffen Klassert 
20611d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
20621d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
20631d1e34ddSDavid S. Miller 		if (err)
20641d1e34ddSDavid S. Miller 			goto out_cancel;
20651d1e34ddSDavid S. Miller 	}
20661d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
20671d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
20681d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
20691d1e34ddSDavid S. Miller 		if (err)
20701d1e34ddSDavid S. Miller 			goto out_cancel;
20711d1e34ddSDavid S. Miller 	}
20721d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
20731d1e34ddSDavid S. Miller 	if (err)
20741d1e34ddSDavid S. Miller 		goto out_cancel;
20756f26b61eSJamal Hadi Salim 
20767e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
20777e652640SSteffen Klassert 	if (err)
20787e652640SSteffen Klassert 		goto out_cancel;
20797e652640SSteffen Klassert 
2080053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2081053c095aSJohannes Berg 	return 0;
2082d51d081dSJamal Hadi Salim 
20831d1e34ddSDavid S. Miller out_cancel:
20849825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
20851d1e34ddSDavid S. Miller 	return err;
2086d51d081dSJamal Hadi Salim }
2087d51d081dSJamal Hadi Salim 
208822e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
20895424f32eSThomas Graf 		struct nlattr **attrs)
2090d51d081dSJamal Hadi Salim {
2091fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2092d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2093d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
2094d51d081dSJamal Hadi Salim 	int err;
2095d51d081dSJamal Hadi Salim 	struct km_event c;
20966f26b61eSJamal Hadi Salim 	u32 mark;
20976f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
20987b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
2099d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
2100d51d081dSJamal Hadi Salim 
21016f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
21026f26b61eSJamal Hadi Salim 
21036f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2104d8647b79SSteffen Klassert 	if (x == NULL)
2105d51d081dSJamal Hadi Salim 		return -ESRCH;
2106d8647b79SSteffen Klassert 
2107d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2108d8647b79SSteffen Klassert 	if (r_skb == NULL) {
2109d8647b79SSteffen Klassert 		xfrm_state_put(x);
2110d8647b79SSteffen Klassert 		return -ENOMEM;
2111d51d081dSJamal Hadi Salim 	}
2112d51d081dSJamal Hadi Salim 
2113d51d081dSJamal Hadi Salim 	/*
2114d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
2115d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
2116d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
2117d51d081dSJamal Hadi Salim 	*/
2118d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2119d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
2120d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
212115e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2122d51d081dSJamal Hadi Salim 
21232fc5f83bSGustavo A. R. Silva 	err = build_aevent(r_skb, x, &c);
21242fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
21252fc5f83bSGustavo A. R. Silva 
212615e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2127d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2128d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2129d51d081dSJamal Hadi Salim 	return err;
2130d51d081dSJamal Hadi Salim }
2131d51d081dSJamal Hadi Salim 
213222e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
21335424f32eSThomas Graf 		struct nlattr **attrs)
2134d51d081dSJamal Hadi Salim {
2135fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2136d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2137d51d081dSJamal Hadi Salim 	struct km_event c;
2138d51d081dSJamal Hadi Salim 	int err = -EINVAL;
21396f26b61eSJamal Hadi Salim 	u32 mark = 0;
21406f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
21417b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
21425424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2143d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
21445424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
21454e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
21464e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2147d51d081dSJamal Hadi Salim 
21484e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
2149d51d081dSJamal Hadi Salim 		return err;
2150d51d081dSJamal Hadi Salim 
2151d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
2152d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2153d51d081dSJamal Hadi Salim 		return err;
2154d51d081dSJamal Hadi Salim 
21556f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
21566f26b61eSJamal Hadi Salim 
21576f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2158d51d081dSJamal Hadi Salim 	if (x == NULL)
2159d51d081dSJamal Hadi Salim 		return -ESRCH;
2160d51d081dSJamal Hadi Salim 
2161d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
2162d51d081dSJamal Hadi Salim 		goto out;
2163d51d081dSJamal Hadi Salim 
21644479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
2165e2b19125SSteffen Klassert 	if (err)
2166e2b19125SSteffen Klassert 		goto out;
2167e2b19125SSteffen Klassert 
2168d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2169e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
2170d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2171d51d081dSJamal Hadi Salim 
2172d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
2173d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
217415e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2175d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
2176d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
2177d51d081dSJamal Hadi Salim 	err = 0;
2178d51d081dSJamal Hadi Salim out:
2179d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2180d51d081dSJamal Hadi Salim 	return err;
2181d51d081dSJamal Hadi Salim }
2182d51d081dSJamal Hadi Salim 
218322e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
21845424f32eSThomas Graf 		struct nlattr **attrs)
21851da177e4SLinus Torvalds {
2186fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
218726b15dadSJamal Hadi Salim 	struct km_event c;
2188b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
2189f7b6983fSMasahide NAKAMURA 	int err;
219026b15dadSJamal Hadi Salim 
219135a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2192f7b6983fSMasahide NAKAMURA 	if (err)
2193f7b6983fSMasahide NAKAMURA 		return err;
2194f7b6983fSMasahide NAKAMURA 
21952e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
21962f1eb65fSJamal Hadi Salim 	if (err) {
21972f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
21982f1eb65fSJamal Hadi Salim 			return 0;
2199069c474eSDavid S. Miller 		return err;
22002f1eb65fSJamal Hadi Salim 	}
22012f1eb65fSJamal Hadi Salim 
2202f7b6983fSMasahide NAKAMURA 	c.data.type = type;
2203f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
220426b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
220515e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
22067067802eSAlexey Dobriyan 	c.net = net;
220726b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
22081da177e4SLinus Torvalds 	return 0;
22091da177e4SLinus Torvalds }
22101da177e4SLinus Torvalds 
221122e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
22125424f32eSThomas Graf 		struct nlattr **attrs)
22136c5c8ca7SJamal Hadi Salim {
2214fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
22156c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
22167b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
22176c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2218b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
22196c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2220295fae56SJamal Hadi Salim 	struct xfrm_mark m;
22217e652640SSteffen Klassert 	u32 if_id = 0;
22226c5c8ca7SJamal Hadi Salim 
222335a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2224f7b6983fSMasahide NAKAMURA 	if (err)
2225f7b6983fSMasahide NAKAMURA 		return err;
2226f7b6983fSMasahide NAKAMURA 
2227c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
2228c8bf4d04STimo Teräs 	if (err)
2229c8bf4d04STimo Teräs 		return err;
2230c8bf4d04STimo Teräs 
22317e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
22327e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
22337e652640SSteffen Klassert 
22344f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
22354f47e8abSXin Long 
22366c5c8ca7SJamal Hadi Salim 	if (p->index)
22374f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
22384f47e8abSXin Long 				      0, &err);
22396c5c8ca7SJamal Hadi Salim 	else {
22405424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
224103e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
22426c5c8ca7SJamal Hadi Salim 
224335a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
22446c5c8ca7SJamal Hadi Salim 		if (err)
22456c5c8ca7SJamal Hadi Salim 			return err;
22466c5c8ca7SJamal Hadi Salim 
22472c8dd116SDenis V. Lunev 		ctx = NULL;
22486c5c8ca7SJamal Hadi Salim 		if (rt) {
22495424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
22506c5c8ca7SJamal Hadi Salim 
225152a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
225203e1ad7bSPaul Moore 			if (err)
22536c5c8ca7SJamal Hadi Salim 				return err;
22542c8dd116SDenis V. Lunev 		}
22554f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
22566f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
225703e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
22586c5c8ca7SJamal Hadi Salim 	}
22596c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2260ef41aaa0SEric Paris 		return -ENOENT;
226103e1ad7bSPaul Moore 
2262ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
22636c5c8ca7SJamal Hadi Salim 		goto out;
22646c5c8ca7SJamal Hadi Salim 
22656c5c8ca7SJamal Hadi Salim 	err = 0;
22666c5c8ca7SJamal Hadi Salim 	if (up->hard) {
22676c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
22682e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
22696c5c8ca7SJamal Hadi Salim 	}
2270c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
22716c5c8ca7SJamal Hadi Salim 
22726c5c8ca7SJamal Hadi Salim out:
22736c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
22746c5c8ca7SJamal Hadi Salim 	return err;
22756c5c8ca7SJamal Hadi Salim }
22766c5c8ca7SJamal Hadi Salim 
227722e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
22785424f32eSThomas Graf 		struct nlattr **attrs)
227953bc6b4dSJamal Hadi Salim {
2280fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
228153bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
228253bc6b4dSJamal Hadi Salim 	int err;
22837b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
228453bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
22856f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2286928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
228753bc6b4dSJamal Hadi Salim 
22886f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
228953bc6b4dSJamal Hadi Salim 
22903a765aa5SDavid S. Miller 	err = -ENOENT;
229153bc6b4dSJamal Hadi Salim 	if (x == NULL)
229253bc6b4dSJamal Hadi Salim 		return err;
229353bc6b4dSJamal Hadi Salim 
229453bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
22953a765aa5SDavid S. Miller 	err = -EINVAL;
229653bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
229753bc6b4dSJamal Hadi Salim 		goto out;
2298c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
229953bc6b4dSJamal Hadi Salim 
2300161a09e7SJoy Latten 	if (ue->hard) {
230153bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
23022e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2303161a09e7SJoy Latten 	}
23043a765aa5SDavid S. Miller 	err = 0;
230553bc6b4dSJamal Hadi Salim out:
230653bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
230753bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
230853bc6b4dSJamal Hadi Salim 	return err;
230953bc6b4dSJamal Hadi Salim }
231053bc6b4dSJamal Hadi Salim 
231122e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
23125424f32eSThomas Graf 		struct nlattr **attrs)
2313980ebd25SJamal Hadi Salim {
2314fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2315980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2316980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2317980ebd25SJamal Hadi Salim 	int i;
23185424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
23196f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2320980ebd25SJamal Hadi Salim 
23217b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2322fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2323980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2324980ebd25SJamal Hadi Salim 
2325980ebd25SJamal Hadi Salim 	if (!x)
2326d8eb9307SIlpo Järvinen 		goto nomem;
2327980ebd25SJamal Hadi Salim 
23286f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
23296f26b61eSJamal Hadi Salim 
2330980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2331d8eb9307SIlpo Järvinen 	if (err)
233273efc324SVegard Nossum 		goto free_state;
2333a1a7e3a3SXin Long 	err = verify_sec_ctx_len(attrs);
2334a1a7e3a3SXin Long 	if (err)
2335a1a7e3a3SXin Long 		goto free_state;
2336980ebd25SJamal Hadi Salim 
2337980ebd25SJamal Hadi Salim 	/*   build an XP */
2338fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2339d8eb9307SIlpo Järvinen 	if (!xp)
2340d8eb9307SIlpo Järvinen 		goto free_state;
2341980ebd25SJamal Hadi Salim 
2342980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2343980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2344980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
23456f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
23466f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
23475424f32eSThomas Graf 	ut = nla_data(rt);
2348980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2349980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2350980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2351980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2352980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2353980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2354980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2355980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2356980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2357980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2358980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2359980ebd25SJamal Hadi Salim 
2360980ebd25SJamal Hadi Salim 	}
2361980ebd25SJamal Hadi Salim 
23624a135e53SMathias Krause 	xfrm_state_free(x);
2363980ebd25SJamal Hadi Salim 	kfree(xp);
2364980ebd25SJamal Hadi Salim 
2365980ebd25SJamal Hadi Salim 	return 0;
2366d8eb9307SIlpo Järvinen 
2367d8eb9307SIlpo Järvinen free_state:
23684a135e53SMathias Krause 	xfrm_state_free(x);
2369d8eb9307SIlpo Järvinen nomem:
2370d8eb9307SIlpo Järvinen 	return err;
2371980ebd25SJamal Hadi Salim }
2372980ebd25SJamal Hadi Salim 
23735c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
23745c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
237513c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
23765424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
23775c79de6eSShinta Sugimoto {
23785424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
23795c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
23805c79de6eSShinta Sugimoto 	int i, num_migrate;
23815c79de6eSShinta Sugimoto 
238213c1d189SArnaud Ebalard 	if (k != NULL) {
238313c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
238413c1d189SArnaud Ebalard 
238513c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
238613c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
238713c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
238813c1d189SArnaud Ebalard 		k->family = uk->family;
238913c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
239013c1d189SArnaud Ebalard 	}
239113c1d189SArnaud Ebalard 
23925424f32eSThomas Graf 	um = nla_data(rt);
23935424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
23945c79de6eSShinta Sugimoto 
23955c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
23965c79de6eSShinta Sugimoto 		return -EINVAL;
23975c79de6eSShinta Sugimoto 
23985c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
23995c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
24005c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
24015c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
24025c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
24035c79de6eSShinta Sugimoto 
24045c79de6eSShinta Sugimoto 		ma->proto = um->proto;
24055c79de6eSShinta Sugimoto 		ma->mode = um->mode;
24065c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
24075c79de6eSShinta Sugimoto 
24085c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
24095c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
24105c79de6eSShinta Sugimoto 	}
24115c79de6eSShinta Sugimoto 
24125c79de6eSShinta Sugimoto 	*num = i;
24135c79de6eSShinta Sugimoto 	return 0;
24145c79de6eSShinta Sugimoto }
24155c79de6eSShinta Sugimoto 
24165c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
24175424f32eSThomas Graf 			   struct nlattr **attrs)
24185c79de6eSShinta Sugimoto {
24197b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
24205c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
242113c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
24225c79de6eSShinta Sugimoto 	u8 type;
24235c79de6eSShinta Sugimoto 	int err;
24245c79de6eSShinta Sugimoto 	int n = 0;
24258d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
24264ab47d47SAntony Antony 	struct xfrm_encap_tmpl  *encap = NULL;
24275c79de6eSShinta Sugimoto 
242835a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2429cf5cb79fSThomas Graf 		return -EINVAL;
24305c79de6eSShinta Sugimoto 
243113c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
243213c1d189SArnaud Ebalard 
24335424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
24345c79de6eSShinta Sugimoto 	if (err)
24355c79de6eSShinta Sugimoto 		return err;
24365c79de6eSShinta Sugimoto 
243713c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
24385c79de6eSShinta Sugimoto 	if (err)
24395c79de6eSShinta Sugimoto 		return err;
24405c79de6eSShinta Sugimoto 
24415c79de6eSShinta Sugimoto 	if (!n)
24425c79de6eSShinta Sugimoto 		return 0;
24435c79de6eSShinta Sugimoto 
24444ab47d47SAntony Antony 	if (attrs[XFRMA_ENCAP]) {
24454ab47d47SAntony Antony 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
24464ab47d47SAntony Antony 				sizeof(*encap), GFP_KERNEL);
24474ab47d47SAntony Antony 		if (!encap)
24485c79de6eSShinta Sugimoto 			return 0;
24495c79de6eSShinta Sugimoto 	}
24504ab47d47SAntony Antony 
24514ab47d47SAntony Antony 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
24524ab47d47SAntony Antony 
24534ab47d47SAntony Antony 	kfree(encap);
24544ab47d47SAntony Antony 
24554ab47d47SAntony Antony 	return err;
24564ab47d47SAntony Antony }
24575c79de6eSShinta Sugimoto #else
24585c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
24595424f32eSThomas Graf 			   struct nlattr **attrs)
24605c79de6eSShinta Sugimoto {
24615c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
24625c79de6eSShinta Sugimoto }
24635c79de6eSShinta Sugimoto #endif
24645c79de6eSShinta Sugimoto 
24655c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2466183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
24675c79de6eSShinta Sugimoto {
24685c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
24695c79de6eSShinta Sugimoto 
24705c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
24715c79de6eSShinta Sugimoto 	um.proto = m->proto;
24725c79de6eSShinta Sugimoto 	um.mode = m->mode;
24735c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
24745c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
24755c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
24765c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
24775c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
24785c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
24795c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
24805c79de6eSShinta Sugimoto 
2481c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
24825c79de6eSShinta Sugimoto }
24835c79de6eSShinta Sugimoto 
2484183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
248513c1d189SArnaud Ebalard {
248613c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
248713c1d189SArnaud Ebalard 
248813c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
248913c1d189SArnaud Ebalard 	uk.family = k->family;
249013c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
249113c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2492a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
249313c1d189SArnaud Ebalard 
249413c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
249513c1d189SArnaud Ebalard }
249613c1d189SArnaud Ebalard 
2497a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
24988bafd730SAntony Antony 						int with_encp)
24997deb2264SThomas Graf {
25007deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
250113c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
25028bafd730SAntony Antony 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
25037deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
25047deb2264SThomas Graf 	      + userpolicy_type_attrsize();
25057deb2264SThomas Graf }
25067deb2264SThomas Graf 
2507183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2508183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
25098bafd730SAntony Antony 			 const struct xfrm_selector *sel,
25108bafd730SAntony Antony 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
25115c79de6eSShinta Sugimoto {
2512183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
25135c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
25145c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
25151d1e34ddSDavid S. Miller 	int i, err;
25165c79de6eSShinta Sugimoto 
251779b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
251879b8b7f4SThomas Graf 	if (nlh == NULL)
251979b8b7f4SThomas Graf 		return -EMSGSIZE;
25205c79de6eSShinta Sugimoto 
25217b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
25225c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
25235c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
25245c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
25255c79de6eSShinta Sugimoto 	pol_id->dir = dir;
25265c79de6eSShinta Sugimoto 
25271d1e34ddSDavid S. Miller 	if (k != NULL) {
25281d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
25291d1e34ddSDavid S. Miller 		if (err)
25301d1e34ddSDavid S. Miller 			goto out_cancel;
25311d1e34ddSDavid S. Miller 	}
25328bafd730SAntony Antony 	if (encap) {
25338bafd730SAntony Antony 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
25348bafd730SAntony Antony 		if (err)
25358bafd730SAntony Antony 			goto out_cancel;
25368bafd730SAntony Antony 	}
25371d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
25381d1e34ddSDavid S. Miller 	if (err)
25391d1e34ddSDavid S. Miller 		goto out_cancel;
25405c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
25411d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
25421d1e34ddSDavid S. Miller 		if (err)
25431d1e34ddSDavid S. Miller 			goto out_cancel;
25445c79de6eSShinta Sugimoto 	}
25455c79de6eSShinta Sugimoto 
2546053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2547053c095aSJohannes Berg 	return 0;
25481d1e34ddSDavid S. Miller 
25491d1e34ddSDavid S. Miller out_cancel:
25509825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
25511d1e34ddSDavid S. Miller 	return err;
25525c79de6eSShinta Sugimoto }
25535c79de6eSShinta Sugimoto 
2554183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2555183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
25568bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
25578bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
25585c79de6eSShinta Sugimoto {
2559a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
25605c79de6eSShinta Sugimoto 	struct sk_buff *skb;
25612fc5f83bSGustavo A. R. Silva 	int err;
25625c79de6eSShinta Sugimoto 
25638bafd730SAntony Antony 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
25648bafd730SAntony Antony 			GFP_ATOMIC);
25655c79de6eSShinta Sugimoto 	if (skb == NULL)
25665c79de6eSShinta Sugimoto 		return -ENOMEM;
25675c79de6eSShinta Sugimoto 
25685c79de6eSShinta Sugimoto 	/* build migrate */
25692fc5f83bSGustavo A. R. Silva 	err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
25702fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
25715c79de6eSShinta Sugimoto 
257221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
25735c79de6eSShinta Sugimoto }
25745c79de6eSShinta Sugimoto #else
2575183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2576183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
25778bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
25788bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
25795c79de6eSShinta Sugimoto {
25805c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
25815c79de6eSShinta Sugimoto }
25825c79de6eSShinta Sugimoto #endif
2583d51d081dSJamal Hadi Salim 
2584a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2585492b558bSThomas Graf 
25865461fc0cSDmitry Safonov const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
258766f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2588492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2589492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2590492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2591492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2592492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2593492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2594980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
259553bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2596492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
259766f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
25986c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2599492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2600a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2601d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2602d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
260397a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
26045c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2605a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2606880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2607a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
26081da177e4SLinus Torvalds };
26095461fc0cSDmitry Safonov EXPORT_SYMBOL_GPL(xfrm_msg_min);
26101da177e4SLinus Torvalds 
2611492b558bSThomas Graf #undef XMSGSIZE
2612492b558bSThomas Graf 
2613cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2614c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2615c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2616c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2617c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
26181a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2619cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2620cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2621cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2622cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2623cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2624cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2625cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2626cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2627cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2628cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2629cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2630cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2631cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2632cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
263313c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
26346f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
263535d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2636d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2637a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2638d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2639870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2640d77e38e6SSteffen Klassert 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
26419b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK]	= { .type = NLA_U32 },
26429b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK_MASK]	= { .type = NLA_U32 },
26437e652640SSteffen Klassert 	[XFRMA_IF_ID]		= { .type = NLA_U32 },
2644cf5cb79fSThomas Graf };
2645cf5cb79fSThomas Graf 
2646880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2647880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2648880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2649880a6fabSChristophe Gouault };
2650880a6fabSChristophe Gouault 
265105600a79SMathias Krause static const struct xfrm_link {
26525424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
26531137b5e2SHerbert Xu 	int (*start)(struct netlink_callback *);
26541da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
26554c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2656880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2657880a6fabSChristophe Gouault 	int nla_max;
2658492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2659492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2660492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2661492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
26624c563f76STimo Teras 						   .dump = xfrm_dump_sa,
26634c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2664492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2665492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2666492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
26671137b5e2SHerbert Xu 						   .start = xfrm_dump_policy_start,
26684c563f76STimo Teras 						   .dump = xfrm_dump_policy,
26694c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2670492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2671980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
267253bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2673492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2674492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
26756c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2676492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2677492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2678d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2679d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
26805c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
268128d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2682880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2683880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2684880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2685ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
26861da177e4SLinus Torvalds };
26871da177e4SLinus Torvalds 
26882d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
26892d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
26901da177e4SLinus Torvalds {
2691a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
269235a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
269305600a79SMathias Krause 	const struct xfrm_link *link;
2694a7bd9a45SThomas Graf 	int type, err;
26951da177e4SLinus Torvalds 
26962bf8c476SAndy Lutomirski 	if (in_compat_syscall())
269783e2d058SYi Zhao 		return -EOPNOTSUPP;
269874005991SFan Du 
26991da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
27001da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
27011d00a4ebSThomas Graf 		return -EINVAL;
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
27041da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
270790f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
27081d00a4ebSThomas Graf 		return -EPERM;
27091da177e4SLinus Torvalds 
2710492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2711492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2712b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
27131da177e4SLinus Torvalds 		if (link->dump == NULL)
27141d00a4ebSThomas Graf 			return -EINVAL;
27151da177e4SLinus Torvalds 
271680d326faSPablo Neira Ayuso 		{
271780d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
27181137b5e2SHerbert Xu 				.start = link->start,
271980d326faSPablo Neira Ayuso 				.dump = link->dump,
272080d326faSPablo Neira Ayuso 				.done = link->done,
272180d326faSPablo Neira Ayuso 			};
272280d326faSPablo Neira Ayuso 			return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
272380d326faSPablo Neira Ayuso 		}
27241da177e4SLinus Torvalds 	}
27251da177e4SLinus Torvalds 
27268cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2727880a6fabSChristophe Gouault 				     link->nla_max ? : XFRMA_MAX,
2728fe52145fSJohannes Berg 				     link->nla_pol ? : xfrma_policy, extack);
2729a7bd9a45SThomas Graf 	if (err < 0)
2730a7bd9a45SThomas Graf 		return err;
27311da177e4SLinus Torvalds 
27321da177e4SLinus Torvalds 	if (link->doit == NULL)
27331d00a4ebSThomas Graf 		return -EINVAL;
27341da177e4SLinus Torvalds 
27355424f32eSThomas Graf 	return link->doit(skb, nlh, attrs);
27361da177e4SLinus Torvalds }
27371da177e4SLinus Torvalds 
2738cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
27391da177e4SLinus Torvalds {
2740283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2741283bc9f3SFan Du 
2742283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2743cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2744283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
27451da177e4SLinus Torvalds }
27461da177e4SLinus Torvalds 
2747a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void)
27487deb2264SThomas Graf {
27496f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
27506f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
27517deb2264SThomas Graf }
27527deb2264SThomas Graf 
2753214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
27541da177e4SLinus Torvalds {
27551da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
27561da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
27571d1e34ddSDavid S. Miller 	int err;
27581da177e4SLinus Torvalds 
275915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
276079b8b7f4SThomas Graf 	if (nlh == NULL)
276179b8b7f4SThomas Graf 		return -EMSGSIZE;
27621da177e4SLinus Torvalds 
27637b67c857SThomas Graf 	ue = nlmsg_data(nlh);
27641da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2765d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
2766e3e5fc16SMathias Krause 	/* clear the padding bytes */
2767e3e5fc16SMathias Krause 	memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
27681da177e4SLinus Torvalds 
27691d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
27701d1e34ddSDavid S. Miller 	if (err)
27711d1e34ddSDavid S. Miller 		return err;
27726f26b61eSJamal Hadi Salim 
27737e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
27747e652640SSteffen Klassert 	if (err)
27757e652640SSteffen Klassert 		return err;
27767e652640SSteffen Klassert 
2777053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2778053c095aSJohannes Berg 	return 0;
27791da177e4SLinus Torvalds }
27801da177e4SLinus Torvalds 
2781214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
27821da177e4SLinus Torvalds {
2783fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
27841da177e4SLinus Torvalds 	struct sk_buff *skb;
27851da177e4SLinus Torvalds 
27867deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
27871da177e4SLinus Torvalds 	if (skb == NULL)
27881da177e4SLinus Torvalds 		return -ENOMEM;
27891da177e4SLinus Torvalds 
27906f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
27916f26b61eSJamal Hadi Salim 		kfree_skb(skb);
27926f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
27936f26b61eSJamal Hadi Salim 	}
27941da177e4SLinus Torvalds 
279521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
27961da177e4SLinus Torvalds }
27971da177e4SLinus Torvalds 
2798214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2799d51d081dSJamal Hadi Salim {
2800fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2801d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
28022fc5f83bSGustavo A. R. Silva 	int err;
2803d51d081dSJamal Hadi Salim 
2804d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2805d51d081dSJamal Hadi Salim 	if (skb == NULL)
2806d51d081dSJamal Hadi Salim 		return -ENOMEM;
2807d51d081dSJamal Hadi Salim 
28082fc5f83bSGustavo A. R. Silva 	err = build_aevent(skb, x, c);
28092fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
2810d51d081dSJamal Hadi Salim 
281121ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2812d51d081dSJamal Hadi Salim }
2813d51d081dSJamal Hadi Salim 
2814214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
281526b15dadSJamal Hadi Salim {
28167067802eSAlexey Dobriyan 	struct net *net = c->net;
281726b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
281826b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
281926b15dadSJamal Hadi Salim 	struct sk_buff *skb;
28207deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
282126b15dadSJamal Hadi Salim 
28227deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
282326b15dadSJamal Hadi Salim 	if (skb == NULL)
282426b15dadSJamal Hadi Salim 		return -ENOMEM;
282526b15dadSJamal Hadi Salim 
282615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
282779b8b7f4SThomas Graf 	if (nlh == NULL) {
282879b8b7f4SThomas Graf 		kfree_skb(skb);
282979b8b7f4SThomas Graf 		return -EMSGSIZE;
283079b8b7f4SThomas Graf 	}
283126b15dadSJamal Hadi Salim 
28327b67c857SThomas Graf 	p = nlmsg_data(nlh);
2833bf08867fSHerbert Xu 	p->proto = c->data.proto;
283426b15dadSJamal Hadi Salim 
28359825069dSThomas Graf 	nlmsg_end(skb, nlh);
283626b15dadSJamal Hadi Salim 
283721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
283826b15dadSJamal Hadi Salim }
283926b15dadSJamal Hadi Salim 
2840a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
284126b15dadSJamal Hadi Salim {
2842a1b831f2SAlexey Dobriyan 	unsigned int l = 0;
28431a6509d9SHerbert Xu 	if (x->aead)
28441a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
28454447bb33SMartin Willi 	if (x->aalg) {
28464447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
28474447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
28484447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
28494447bb33SMartin Willi 	}
285026b15dadSJamal Hadi Salim 	if (x->ealg)
28510f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
285226b15dadSJamal Hadi Salim 	if (x->calg)
28537deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
285426b15dadSJamal Hadi Salim 	if (x->encap)
28557deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
285635d2856bSMartin Willi 	if (x->tfcpad)
285735d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
2858d8647b79SSteffen Klassert 	if (x->replay_esn)
2859d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2860f293a5e3Sdingzhi 	else
2861f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
286268325d3bSHerbert Xu 	if (x->security)
286368325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
286468325d3bSHerbert Xu 				    x->security->ctx_len);
286568325d3bSHerbert Xu 	if (x->coaddr)
286668325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
2867a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
2868a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
2869d77e38e6SSteffen Klassert 	if (x->xso.dev)
2870d77e38e6SSteffen Klassert 		 l += nla_total_size(sizeof(x->xso));
28719b42c1f1SSteffen Klassert 	if (x->props.smark.v | x->props.smark.m) {
28729b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.v));
28739b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.m));
28749b42c1f1SSteffen Klassert 	}
28757e652640SSteffen Klassert 	if (x->if_id)
28767e652640SSteffen Klassert 		l += nla_total_size(sizeof(x->if_id));
287768325d3bSHerbert Xu 
2878d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2879de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
288026b15dadSJamal Hadi Salim 
288126b15dadSJamal Hadi Salim 	return l;
288226b15dadSJamal Hadi Salim }
288326b15dadSJamal Hadi Salim 
2884214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
288526b15dadSJamal Hadi Salim {
2886fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
288726b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
28880603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
288926b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
289026b15dadSJamal Hadi Salim 	struct sk_buff *skb;
2891a1b831f2SAlexey Dobriyan 	unsigned int len = xfrm_sa_len(x);
2892a1b831f2SAlexey Dobriyan 	unsigned int headlen;
2893a1b831f2SAlexey Dobriyan 	int err;
28940603eac0SHerbert Xu 
28950603eac0SHerbert Xu 	headlen = sizeof(*p);
28960603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
28977deb2264SThomas Graf 		len += nla_total_size(headlen);
28980603eac0SHerbert Xu 		headlen = sizeof(*id);
28996f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
29000603eac0SHerbert Xu 	}
29017deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
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, c->event, headlen, 0);
29081d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
290979b8b7f4SThomas Graf 	if (nlh == NULL)
29101d1e34ddSDavid S. Miller 		goto out_free_skb;
291126b15dadSJamal Hadi Salim 
29127b67c857SThomas Graf 	p = nlmsg_data(nlh);
29130603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2914c0144beaSThomas Graf 		struct nlattr *attr;
2915c0144beaSThomas Graf 
29167b67c857SThomas Graf 		id = nlmsg_data(nlh);
291750329c8aSMathias Krause 		memset(id, 0, sizeof(*id));
29180603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
29190603eac0SHerbert Xu 		id->spi = x->id.spi;
29200603eac0SHerbert Xu 		id->family = x->props.family;
29210603eac0SHerbert Xu 		id->proto = x->id.proto;
29220603eac0SHerbert Xu 
2923c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
29241d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2925c0144beaSThomas Graf 		if (attr == NULL)
29261d1e34ddSDavid S. Miller 			goto out_free_skb;
2927c0144beaSThomas Graf 
2928c0144beaSThomas Graf 		p = nla_data(attr);
29290603eac0SHerbert Xu 	}
29301d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
29311d1e34ddSDavid S. Miller 	if (err)
29321d1e34ddSDavid S. Miller 		goto out_free_skb;
293326b15dadSJamal Hadi Salim 
29349825069dSThomas Graf 	nlmsg_end(skb, nlh);
293526b15dadSJamal Hadi Salim 
293621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
293726b15dadSJamal Hadi Salim 
29381d1e34ddSDavid S. Miller out_free_skb:
293926b15dadSJamal Hadi Salim 	kfree_skb(skb);
29401d1e34ddSDavid S. Miller 	return err;
294126b15dadSJamal Hadi Salim }
294226b15dadSJamal Hadi Salim 
2943214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
294426b15dadSJamal Hadi Salim {
294526b15dadSJamal Hadi Salim 
294626b15dadSJamal Hadi Salim 	switch (c->event) {
2947f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
294826b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2949d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2950d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2951f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2952f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2953f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
295426b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2955f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
295626b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
295726b15dadSJamal Hadi Salim 	default:
295862db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
295962db5cfdSstephen hemminger 		       c->event);
296026b15dadSJamal Hadi Salim 		break;
296126b15dadSJamal Hadi Salim 	}
296226b15dadSJamal Hadi Salim 
296326b15dadSJamal Hadi Salim 	return 0;
296426b15dadSJamal Hadi Salim 
296526b15dadSJamal Hadi Salim }
296626b15dadSJamal Hadi Salim 
2967a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
29687deb2264SThomas Graf 						struct xfrm_policy *xp)
29697deb2264SThomas Graf {
29707deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
29717deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
29726f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
29737deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
29747deb2264SThomas Graf 	       + userpolicy_type_attrsize();
29757deb2264SThomas Graf }
29767deb2264SThomas Graf 
29771da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
297865e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
29791da177e4SLinus Torvalds {
29801d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
29811da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
29821da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
29831d1e34ddSDavid S. Miller 	int err;
29841da177e4SLinus Torvalds 
298579b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
298679b8b7f4SThomas Graf 	if (nlh == NULL)
298779b8b7f4SThomas Graf 		return -EMSGSIZE;
29881da177e4SLinus Torvalds 
29897b67c857SThomas Graf 	ua = nlmsg_data(nlh);
29901da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
29911da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
29921da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
299365e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
29941da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
29951da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
29961da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
29971da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
29981da177e4SLinus Torvalds 
29991d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
30001d1e34ddSDavid S. Miller 	if (!err)
30011d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
30021d1e34ddSDavid S. Miller 	if (!err)
30031d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
30041d1e34ddSDavid S. Miller 	if (!err)
30051d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
30067e652640SSteffen Klassert 	if (!err)
30077e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
30081d1e34ddSDavid S. Miller 	if (err) {
30091d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
30101d1e34ddSDavid S. Miller 		return err;
30111d1e34ddSDavid S. Miller 	}
30121da177e4SLinus Torvalds 
3013053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3014053c095aSJohannes Berg 	return 0;
30151da177e4SLinus Torvalds }
30161da177e4SLinus Torvalds 
30171da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
301865e0736bSFan Du 			     struct xfrm_policy *xp)
30191da177e4SLinus Torvalds {
3020a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
30211da177e4SLinus Torvalds 	struct sk_buff *skb;
30222fc5f83bSGustavo A. R. Silva 	int err;
30231da177e4SLinus Torvalds 
30247deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
30251da177e4SLinus Torvalds 	if (skb == NULL)
30261da177e4SLinus Torvalds 		return -ENOMEM;
30271da177e4SLinus Torvalds 
30282fc5f83bSGustavo A. R. Silva 	err = build_acquire(skb, x, xt, xp);
30292fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
30301da177e4SLinus Torvalds 
303121ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
30321da177e4SLinus Torvalds }
30331da177e4SLinus Torvalds 
30341da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
30351da177e4SLinus Torvalds  * or more templates.
30361da177e4SLinus Torvalds  */
3037cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
30381da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
30391da177e4SLinus Torvalds {
3040fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
30411da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
30421da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
30431da177e4SLinus Torvalds 	struct xfrm_policy *xp;
30441da177e4SLinus Torvalds 	int nr;
30451da177e4SLinus Torvalds 
3046cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
30471da177e4SLinus Torvalds 	case AF_INET:
30481da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
30491da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
30501da177e4SLinus Torvalds 			return NULL;
30511da177e4SLinus Torvalds 		}
30521da177e4SLinus Torvalds 		break;
3053dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
30541da177e4SLinus Torvalds 	case AF_INET6:
30551da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
30561da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
30571da177e4SLinus Torvalds 			return NULL;
30581da177e4SLinus Torvalds 		}
30591da177e4SLinus Torvalds 		break;
30601da177e4SLinus Torvalds #endif
30611da177e4SLinus Torvalds 	default:
30621da177e4SLinus Torvalds 		*dir = -EINVAL;
30631da177e4SLinus Torvalds 		return NULL;
30641da177e4SLinus Torvalds 	}
30651da177e4SLinus Torvalds 
30661da177e4SLinus Torvalds 	*dir = -EINVAL;
30671da177e4SLinus Torvalds 
30681da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
30691da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
30701da177e4SLinus Torvalds 		return NULL;
30711da177e4SLinus Torvalds 
30721da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
3073b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
30741da177e4SLinus Torvalds 		return NULL;
30751da177e4SLinus Torvalds 
3076a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
3077a4f1bac6SHerbert Xu 		return NULL;
3078a4f1bac6SHerbert Xu 
30792f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
30801da177e4SLinus Torvalds 	if (xp == NULL) {
30811da177e4SLinus Torvalds 		*dir = -ENOBUFS;
30821da177e4SLinus Torvalds 		return NULL;
30831da177e4SLinus Torvalds 	}
30841da177e4SLinus Torvalds 
30851da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
3086f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
30871da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
30881da177e4SLinus Torvalds 
30891da177e4SLinus Torvalds 	*dir = p->dir;
30901da177e4SLinus Torvalds 
30911da177e4SLinus Torvalds 	return xp;
30921da177e4SLinus Torvalds }
30931da177e4SLinus Torvalds 
3094a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
30957deb2264SThomas Graf {
30967deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
30977deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
30987deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3099295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
31007deb2264SThomas Graf 	       + userpolicy_type_attrsize();
31017deb2264SThomas Graf }
31027deb2264SThomas Graf 
31031da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3104214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
31051da177e4SLinus Torvalds {
31061da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
3107d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
31081d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
31091d1e34ddSDavid S. Miller 	int err;
31101da177e4SLinus Torvalds 
311115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
311279b8b7f4SThomas Graf 	if (nlh == NULL)
311379b8b7f4SThomas Graf 		return -EMSGSIZE;
31141da177e4SLinus Torvalds 
31157b67c857SThomas Graf 	upe = nlmsg_data(nlh);
31161da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
31171d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
31181d1e34ddSDavid S. Miller 	if (!err)
31191d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
31201d1e34ddSDavid S. Miller 	if (!err)
31211d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
31221d1e34ddSDavid S. Miller 	if (!err)
31231d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
31247e652640SSteffen Klassert 	if (!err)
31257e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
31261d1e34ddSDavid S. Miller 	if (err) {
31271d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
31281d1e34ddSDavid S. Miller 		return err;
31291d1e34ddSDavid S. Miller 	}
31301da177e4SLinus Torvalds 	upe->hard = !!hard;
31311da177e4SLinus Torvalds 
3132053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3133053c095aSJohannes Berg 	return 0;
31341da177e4SLinus Torvalds }
31351da177e4SLinus Torvalds 
3136214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
31371da177e4SLinus Torvalds {
3138fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
31391da177e4SLinus Torvalds 	struct sk_buff *skb;
31402fc5f83bSGustavo A. R. Silva 	int err;
31411da177e4SLinus Torvalds 
31427deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
31431da177e4SLinus Torvalds 	if (skb == NULL)
31441da177e4SLinus Torvalds 		return -ENOMEM;
31451da177e4SLinus Torvalds 
31462fc5f83bSGustavo A. R. Silva 	err = build_polexpire(skb, xp, dir, c);
31472fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
31481da177e4SLinus Torvalds 
314921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
31501da177e4SLinus Torvalds }
31511da177e4SLinus Torvalds 
3152214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
315326b15dadSJamal Hadi Salim {
3154a1b831f2SAlexey Dobriyan 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3155fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
315626b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
31570603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
315826b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
315926b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3160a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3161a1b831f2SAlexey Dobriyan 	int err;
31620603eac0SHerbert Xu 
31630603eac0SHerbert Xu 	headlen = sizeof(*p);
31640603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
31657deb2264SThomas Graf 		len += nla_total_size(headlen);
31660603eac0SHerbert Xu 		headlen = sizeof(*id);
31670603eac0SHerbert Xu 	}
3168cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
3169295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
31707deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
317126b15dadSJamal Hadi Salim 
31727deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
317326b15dadSJamal Hadi Salim 	if (skb == NULL)
317426b15dadSJamal Hadi Salim 		return -ENOMEM;
317526b15dadSJamal Hadi Salim 
317615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
31771d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
317879b8b7f4SThomas Graf 	if (nlh == NULL)
31791d1e34ddSDavid S. Miller 		goto out_free_skb;
318026b15dadSJamal Hadi Salim 
31817b67c857SThomas Graf 	p = nlmsg_data(nlh);
31820603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
3183c0144beaSThomas Graf 		struct nlattr *attr;
3184c0144beaSThomas Graf 
31857b67c857SThomas Graf 		id = nlmsg_data(nlh);
31860603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
31870603eac0SHerbert Xu 		id->dir = dir;
31880603eac0SHerbert Xu 		if (c->data.byid)
31890603eac0SHerbert Xu 			id->index = xp->index;
31900603eac0SHerbert Xu 		else
31910603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
31920603eac0SHerbert Xu 
3193c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
31941d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3195c0144beaSThomas Graf 		if (attr == NULL)
31961d1e34ddSDavid S. Miller 			goto out_free_skb;
3197c0144beaSThomas Graf 
3198c0144beaSThomas Graf 		p = nla_data(attr);
31990603eac0SHerbert Xu 	}
320026b15dadSJamal Hadi Salim 
320126b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
32021d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
32031d1e34ddSDavid S. Miller 	if (!err)
32041d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
32051d1e34ddSDavid S. Miller 	if (!err)
32061d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
32077e652640SSteffen Klassert 	if (!err)
32087e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
32091d1e34ddSDavid S. Miller 	if (err)
32101d1e34ddSDavid S. Miller 		goto out_free_skb;
3211295fae56SJamal Hadi Salim 
32129825069dSThomas Graf 	nlmsg_end(skb, nlh);
321326b15dadSJamal Hadi Salim 
321421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
321526b15dadSJamal Hadi Salim 
32161d1e34ddSDavid S. Miller out_free_skb:
321726b15dadSJamal Hadi Salim 	kfree_skb(skb);
32181d1e34ddSDavid S. Miller 	return err;
321926b15dadSJamal Hadi Salim }
322026b15dadSJamal Hadi Salim 
3221214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
322226b15dadSJamal Hadi Salim {
32237067802eSAlexey Dobriyan 	struct net *net = c->net;
322426b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
322526b15dadSJamal Hadi Salim 	struct sk_buff *skb;
32261d1e34ddSDavid S. Miller 	int err;
322726b15dadSJamal Hadi Salim 
32287deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
322926b15dadSJamal Hadi Salim 	if (skb == NULL)
323026b15dadSJamal Hadi Salim 		return -ENOMEM;
323126b15dadSJamal Hadi Salim 
323215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
32331d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
323479b8b7f4SThomas Graf 	if (nlh == NULL)
32351d1e34ddSDavid S. Miller 		goto out_free_skb;
32361d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
32371d1e34ddSDavid S. Miller 	if (err)
32381d1e34ddSDavid S. Miller 		goto out_free_skb;
323926b15dadSJamal Hadi Salim 
32409825069dSThomas Graf 	nlmsg_end(skb, nlh);
324126b15dadSJamal Hadi Salim 
324221ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
324326b15dadSJamal Hadi Salim 
32441d1e34ddSDavid S. Miller out_free_skb:
324526b15dadSJamal Hadi Salim 	kfree_skb(skb);
32461d1e34ddSDavid S. Miller 	return err;
324726b15dadSJamal Hadi Salim }
324826b15dadSJamal Hadi Salim 
3249214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
325026b15dadSJamal Hadi Salim {
325126b15dadSJamal Hadi Salim 
325226b15dadSJamal Hadi Salim 	switch (c->event) {
3253f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
3254f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
3255f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
325626b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
3257f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
325826b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
3259f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
326026b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
326126b15dadSJamal Hadi Salim 	default:
326262db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
326362db5cfdSstephen hemminger 		       c->event);
326426b15dadSJamal Hadi Salim 	}
326526b15dadSJamal Hadi Salim 
326626b15dadSJamal Hadi Salim 	return 0;
326726b15dadSJamal Hadi Salim 
326826b15dadSJamal Hadi Salim }
326926b15dadSJamal Hadi Salim 
3270a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void)
32717deb2264SThomas Graf {
32727deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
32737deb2264SThomas Graf }
32747deb2264SThomas Graf 
327597a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
327697a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
327797a64b45SMasahide NAKAMURA {
327897a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
327997a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
328097a64b45SMasahide NAKAMURA 
328179b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
328279b8b7f4SThomas Graf 	if (nlh == NULL)
328379b8b7f4SThomas Graf 		return -EMSGSIZE;
328497a64b45SMasahide NAKAMURA 
32857b67c857SThomas Graf 	ur = nlmsg_data(nlh);
328697a64b45SMasahide NAKAMURA 	ur->proto = proto;
328797a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
328897a64b45SMasahide NAKAMURA 
32891d1e34ddSDavid S. Miller 	if (addr) {
32901d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
32911d1e34ddSDavid S. Miller 		if (err) {
32929825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
32931d1e34ddSDavid S. Miller 			return err;
32941d1e34ddSDavid S. Miller 		}
32951d1e34ddSDavid S. Miller 	}
3296053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3297053c095aSJohannes Berg 	return 0;
329897a64b45SMasahide NAKAMURA }
329997a64b45SMasahide NAKAMURA 
3300db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3301db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
330297a64b45SMasahide NAKAMURA {
330397a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
33042fc5f83bSGustavo A. R. Silva 	int err;
330597a64b45SMasahide NAKAMURA 
33067deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
330797a64b45SMasahide NAKAMURA 	if (skb == NULL)
330897a64b45SMasahide NAKAMURA 		return -ENOMEM;
330997a64b45SMasahide NAKAMURA 
33102fc5f83bSGustavo A. R. Silva 	err = build_report(skb, proto, sel, addr);
33112fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
331297a64b45SMasahide NAKAMURA 
331321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
331497a64b45SMasahide NAKAMURA }
331597a64b45SMasahide NAKAMURA 
3316a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void)
33173a2dfbe8SMartin Willi {
33183a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
33193a2dfbe8SMartin Willi }
33203a2dfbe8SMartin Willi 
33213a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
33223a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
33233a2dfbe8SMartin Willi {
33243a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
33253a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
33263a2dfbe8SMartin Willi 
33273a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
33283a2dfbe8SMartin Willi 	if (nlh == NULL)
33293a2dfbe8SMartin Willi 		return -EMSGSIZE;
33303a2dfbe8SMartin Willi 
33313a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
33323a2dfbe8SMartin Willi 
33333a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
33343a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
33353a2dfbe8SMartin Willi 	um->id.family = x->props.family;
33363a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
33373a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
33383a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
33393a2dfbe8SMartin Willi 	um->new_sport = new_sport;
33403a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
33413a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
33423a2dfbe8SMartin Willi 
3343053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3344053c095aSJohannes Berg 	return 0;
33453a2dfbe8SMartin Willi }
33463a2dfbe8SMartin Willi 
33473a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
33483a2dfbe8SMartin Willi 			     __be16 sport)
33493a2dfbe8SMartin Willi {
3350a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
33513a2dfbe8SMartin Willi 	struct sk_buff *skb;
33522fc5f83bSGustavo A. R. Silva 	int err;
33533a2dfbe8SMartin Willi 
33543a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
33553a2dfbe8SMartin Willi 		return -EINVAL;
33563a2dfbe8SMartin Willi 
33573a2dfbe8SMartin Willi 	if (!x->encap)
33583a2dfbe8SMartin Willi 		return -EINVAL;
33593a2dfbe8SMartin Willi 
33603a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
33613a2dfbe8SMartin Willi 	if (skb == NULL)
33623a2dfbe8SMartin Willi 		return -ENOMEM;
33633a2dfbe8SMartin Willi 
33642fc5f83bSGustavo A. R. Silva 	err = build_mapping(skb, x, ipaddr, sport);
33652fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
33663a2dfbe8SMartin Willi 
336721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
33683a2dfbe8SMartin Willi }
33693a2dfbe8SMartin Willi 
33700f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
33710f24558eSHoria Geanta {
33720f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
33730f24558eSHoria Geanta }
33740f24558eSHoria Geanta 
33751da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
33761da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
33771da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
33781da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
33791da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
338097a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
33815c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
33823a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
33830f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
33841da177e4SLinus Torvalds };
33851da177e4SLinus Torvalds 
3386a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
33871da177e4SLinus Torvalds {
3388be33690dSPatrick McHardy 	struct sock *nlsk;
3389a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3390a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3391a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3392a31f2d17SPablo Neira Ayuso 	};
3393be33690dSPatrick McHardy 
33949f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3395be33690dSPatrick McHardy 	if (nlsk == NULL)
33961da177e4SLinus Torvalds 		return -ENOMEM;
3397d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3398cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
33991da177e4SLinus Torvalds 	return 0;
34001da177e4SLinus Torvalds }
34011da177e4SLinus Torvalds 
3402d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3403a6483b79SAlexey Dobriyan {
3404d79d792eSEric W. Biederman 	struct net *net;
3405d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3406a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3407d79d792eSEric W. Biederman 	synchronize_net();
3408d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3409d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3410a6483b79SAlexey Dobriyan }
3411a6483b79SAlexey Dobriyan 
3412a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3413a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
3414d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3415a6483b79SAlexey Dobriyan };
3416a6483b79SAlexey Dobriyan 
3417a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3418a6483b79SAlexey Dobriyan {
3419a6483b79SAlexey Dobriyan 	int rv;
3420a6483b79SAlexey Dobriyan 
3421a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3422a6483b79SAlexey Dobriyan 
3423a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3424a6483b79SAlexey Dobriyan 	if (rv < 0)
3425a6483b79SAlexey Dobriyan 		return rv;
3426a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3427a6483b79SAlexey Dobriyan 	if (rv < 0)
3428a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3429a6483b79SAlexey Dobriyan 	return rv;
3430a6483b79SAlexey Dobriyan }
3431a6483b79SAlexey Dobriyan 
34321da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
34331da177e4SLinus Torvalds {
34341da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3435a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
34361da177e4SLinus Torvalds }
34371da177e4SLinus Torvalds 
34381da177e4SLinus Torvalds module_init(xfrm_user_init);
34391da177e4SLinus Torvalds module_exit(xfrm_user_exit);
34401da177e4SLinus Torvalds MODULE_LICENSE("GPL");
34414fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3442