xref: /linux/net/xfrm/xfrm_user.c (revision 5461fc0c8d9f23956b99f5907f69726a293ccb67)
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;
97868325d3bSHerbert Xu 	struct xfrm_usersa_info *p;
97968325d3bSHerbert Xu 	struct nlmsghdr *nlh;
98068325d3bSHerbert Xu 	int err;
98168325d3bSHerbert Xu 
98215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
98368325d3bSHerbert Xu 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
98468325d3bSHerbert Xu 	if (nlh == NULL)
98568325d3bSHerbert Xu 		return -EMSGSIZE;
98668325d3bSHerbert Xu 
98768325d3bSHerbert Xu 	p = nlmsg_data(nlh);
98868325d3bSHerbert Xu 
98968325d3bSHerbert Xu 	err = copy_to_user_state_extra(x, p, skb);
9901d1e34ddSDavid S. Miller 	if (err) {
9919825069dSThomas Graf 		nlmsg_cancel(skb, nlh);
99268325d3bSHerbert Xu 		return err;
9931da177e4SLinus Torvalds 	}
9941d1e34ddSDavid S. Miller 	nlmsg_end(skb, nlh);
9951d1e34ddSDavid S. Miller 	return 0;
9961d1e34ddSDavid S. Miller }
9971da177e4SLinus Torvalds 
9984c563f76STimo Teras static int xfrm_dump_sa_done(struct netlink_callback *cb)
9994c563f76STimo Teras {
10004c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1001283bc9f3SFan Du 	struct sock *sk = cb->skb->sk;
1002283bc9f3SFan Du 	struct net *net = sock_net(sk);
1003283bc9f3SFan Du 
10041ba5bf99SVegard Nossum 	if (cb->args[0])
1005283bc9f3SFan Du 		xfrm_state_walk_done(walk, net);
10064c563f76STimo Teras 	return 0;
10074c563f76STimo Teras }
10084c563f76STimo Teras 
1009d3623099SNicolas Dichtel static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
10101da177e4SLinus Torvalds static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
10111da177e4SLinus Torvalds {
1012fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
10134c563f76STimo Teras 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
10141da177e4SLinus Torvalds 	struct xfrm_dump_info info;
10151da177e4SLinus Torvalds 
10164c563f76STimo Teras 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
10174c563f76STimo Teras 		     sizeof(cb->args) - sizeof(cb->args[0]));
10184c563f76STimo Teras 
10191da177e4SLinus Torvalds 	info.in_skb = cb->skb;
10201da177e4SLinus Torvalds 	info.out_skb = skb;
10211da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
10221da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
10234c563f76STimo Teras 
10244c563f76STimo Teras 	if (!cb->args[0]) {
1025d3623099SNicolas Dichtel 		struct nlattr *attrs[XFRMA_MAX+1];
1026870a2df4SNicolas Dichtel 		struct xfrm_address_filter *filter = NULL;
1027d3623099SNicolas Dichtel 		u8 proto = 0;
1028d3623099SNicolas Dichtel 		int err;
1029d3623099SNicolas Dichtel 
10308cb08174SJohannes Berg 		err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
10318cb08174SJohannes Berg 					     xfrma_policy, cb->extack);
1032d3623099SNicolas Dichtel 		if (err < 0)
1033d3623099SNicolas Dichtel 			return err;
1034d3623099SNicolas Dichtel 
1035870a2df4SNicolas Dichtel 		if (attrs[XFRMA_ADDRESS_FILTER]) {
1036df367561SAndrzej Hajda 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1037df367561SAndrzej Hajda 					 sizeof(*filter), GFP_KERNEL);
1038d3623099SNicolas Dichtel 			if (filter == NULL)
1039d3623099SNicolas Dichtel 				return -ENOMEM;
1040d3623099SNicolas Dichtel 		}
1041d3623099SNicolas Dichtel 
1042d3623099SNicolas Dichtel 		if (attrs[XFRMA_PROTO])
1043d3623099SNicolas Dichtel 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
1044d3623099SNicolas Dichtel 
1045d3623099SNicolas Dichtel 		xfrm_state_walk_init(walk, proto, filter);
10461ba5bf99SVegard Nossum 		cb->args[0] = 1;
10474c563f76STimo Teras 	}
10484c563f76STimo Teras 
1049fc34acd3SAlexey Dobriyan 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	return skb->len;
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
10551da177e4SLinus Torvalds 					  struct xfrm_state *x, u32 seq)
10561da177e4SLinus Torvalds {
10571da177e4SLinus Torvalds 	struct xfrm_dump_info info;
10581da177e4SLinus Torvalds 	struct sk_buff *skb;
1059864745d2SMathias Krause 	int err;
10601da177e4SLinus Torvalds 
10617deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
10621da177e4SLinus Torvalds 	if (!skb)
10631da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	info.in_skb = in_skb;
10661da177e4SLinus Torvalds 	info.out_skb = skb;
10671da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
10681da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
10691da177e4SLinus Torvalds 
1070864745d2SMathias Krause 	err = dump_one_state(x, 0, &info);
1071864745d2SMathias Krause 	if (err) {
10721da177e4SLinus Torvalds 		kfree_skb(skb);
1073864745d2SMathias Krause 		return ERR_PTR(err);
10741da177e4SLinus Torvalds 	}
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 	return skb;
10771da177e4SLinus Torvalds }
10781da177e4SLinus Torvalds 
107921ee543eSMichal Kubecek /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
108021ee543eSMichal Kubecek  * Must be called with RCU read lock.
108121ee543eSMichal Kubecek  */
108221ee543eSMichal Kubecek static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
108321ee543eSMichal Kubecek 				       u32 pid, unsigned int group)
108421ee543eSMichal Kubecek {
108521ee543eSMichal Kubecek 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1086*5461fc0cSDmitry Safonov 	struct xfrm_translator *xtr;
108721ee543eSMichal Kubecek 
108886126b77SFlorian Westphal 	if (!nlsk) {
108986126b77SFlorian Westphal 		kfree_skb(skb);
109086126b77SFlorian Westphal 		return -EPIPE;
109186126b77SFlorian Westphal 	}
109286126b77SFlorian Westphal 
1093*5461fc0cSDmitry Safonov 	xtr = xfrm_get_translator();
1094*5461fc0cSDmitry Safonov 	if (xtr) {
1095*5461fc0cSDmitry Safonov 		int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1096*5461fc0cSDmitry Safonov 
1097*5461fc0cSDmitry Safonov 		xfrm_put_translator(xtr);
1098*5461fc0cSDmitry Safonov 		if (err) {
1099*5461fc0cSDmitry Safonov 			kfree_skb(skb);
1100*5461fc0cSDmitry Safonov 			return err;
1101*5461fc0cSDmitry Safonov 		}
1102*5461fc0cSDmitry Safonov 	}
1103*5461fc0cSDmitry Safonov 
110421ee543eSMichal Kubecek 	return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
110521ee543eSMichal Kubecek }
110621ee543eSMichal Kubecek 
1107a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void)
11087deb2264SThomas Graf {
11097deb2264SThomas Graf 	return NLMSG_ALIGN(4)
11107deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1111880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1112880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1113880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
11147deb2264SThomas Graf }
11157deb2264SThomas Graf 
1116e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
111715e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
1118ecfd6b18SJamal Hadi Salim {
11195a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
11205a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
11215a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1122880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1123ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
11241d1e34ddSDavid S. Miller 	int err;
1125ecfd6b18SJamal Hadi Salim 	u32 *f;
1126880a6fabSChristophe Gouault 	unsigned lseq;
1127ecfd6b18SJamal Hadi Salim 
112815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
112925985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1130ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1131ecfd6b18SJamal Hadi Salim 
1132ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1133ecfd6b18SJamal Hadi Salim 	*f = flags;
1134e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
11355a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
11365a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
11375a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
11385a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
11395a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
11405a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
11415a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
11425a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1143ecfd6b18SJamal Hadi Salim 
1144880a6fabSChristophe Gouault 	do {
1145880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1146880a6fabSChristophe Gouault 
1147880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1148880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1149880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1150880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1151880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1152880a6fabSChristophe Gouault 
11531d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
11541d1e34ddSDavid S. Miller 	if (!err)
11551d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1156880a6fabSChristophe Gouault 	if (!err)
1157880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1158880a6fabSChristophe Gouault 	if (!err)
1159880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
11601d1e34ddSDavid S. Miller 	if (err) {
11611d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
11621d1e34ddSDavid S. Miller 		return err;
11631d1e34ddSDavid S. Miller 	}
1164ecfd6b18SJamal Hadi Salim 
1165053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1166053c095aSJohannes Berg 	return 0;
1167ecfd6b18SJamal Hadi Salim }
1168ecfd6b18SJamal Hadi Salim 
1169880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1170880a6fabSChristophe Gouault 			    struct nlattr **attrs)
1171880a6fabSChristophe Gouault {
1172880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1173880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1174880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1175880a6fabSChristophe Gouault 
1176880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1177880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1178880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1179880a6fabSChristophe Gouault 
1180880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1181880a6fabSChristophe Gouault 			return -EINVAL;
1182880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1183880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1184880a6fabSChristophe Gouault 			return -EINVAL;
1185880a6fabSChristophe Gouault 	}
1186880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1187880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1188880a6fabSChristophe Gouault 
1189880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1190880a6fabSChristophe Gouault 			return -EINVAL;
1191880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1192880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1193880a6fabSChristophe Gouault 			return -EINVAL;
1194880a6fabSChristophe Gouault 	}
1195880a6fabSChristophe Gouault 
1196880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1197880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1198880a6fabSChristophe Gouault 		if (thresh4) {
1199880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1200880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1201880a6fabSChristophe Gouault 		}
1202880a6fabSChristophe Gouault 		if (thresh6) {
1203880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1204880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1205880a6fabSChristophe Gouault 		}
1206880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1207880a6fabSChristophe Gouault 
1208880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1209880a6fabSChristophe Gouault 	}
1210880a6fabSChristophe Gouault 
1211880a6fabSChristophe Gouault 	return 0;
1212880a6fabSChristophe Gouault }
1213880a6fabSChristophe Gouault 
1214ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12155424f32eSThomas Graf 		struct nlattr **attrs)
1216ecfd6b18SJamal Hadi Salim {
1217a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1218ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
12197b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
122015e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1221ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
12222fc5f83bSGustavo A. R. Silva 	int err;
1223ecfd6b18SJamal Hadi Salim 
12247deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1225ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1226ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1227ecfd6b18SJamal Hadi Salim 
12282fc5f83bSGustavo A. R. Silva 	err = build_spdinfo(r_skb, net, sportid, seq, *flags);
12292fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
1230ecfd6b18SJamal Hadi Salim 
123115e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1232ecfd6b18SJamal Hadi Salim }
1233ecfd6b18SJamal Hadi Salim 
1234a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void)
12357deb2264SThomas Graf {
12367deb2264SThomas Graf 	return NLMSG_ALIGN(4)
12377deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
12387deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
12397deb2264SThomas Graf }
12407deb2264SThomas Graf 
1241e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
124215e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
124328d8909bSJamal Hadi Salim {
1244af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1245af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
124628d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
12471d1e34ddSDavid S. Miller 	int err;
124828d8909bSJamal Hadi Salim 	u32 *f;
124928d8909bSJamal Hadi Salim 
125015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
125125985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
125228d8909bSJamal Hadi Salim 		return -EMSGSIZE;
125328d8909bSJamal Hadi Salim 
125428d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
125528d8909bSJamal Hadi Salim 	*f = flags;
1256e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
125728d8909bSJamal Hadi Salim 
1258af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1259af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1260af11e316SJamal Hadi Salim 
12611d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
12621d1e34ddSDavid S. Miller 	if (!err)
12631d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
12641d1e34ddSDavid S. Miller 	if (err) {
12651d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
12661d1e34ddSDavid S. Miller 		return err;
12671d1e34ddSDavid S. Miller 	}
126828d8909bSJamal Hadi Salim 
1269053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1270053c095aSJohannes Berg 	return 0;
127128d8909bSJamal Hadi Salim }
127228d8909bSJamal Hadi Salim 
127328d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12745424f32eSThomas Graf 		struct nlattr **attrs)
127528d8909bSJamal Hadi Salim {
1276a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
127728d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
12787b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
127915e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
128028d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
12812fc5f83bSGustavo A. R. Silva 	int err;
128228d8909bSJamal Hadi Salim 
12837deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
128428d8909bSJamal Hadi Salim 	if (r_skb == NULL)
128528d8909bSJamal Hadi Salim 		return -ENOMEM;
128628d8909bSJamal Hadi Salim 
12872fc5f83bSGustavo A. R. Silva 	err = build_sadinfo(r_skb, net, sportid, seq, *flags);
12882fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
128928d8909bSJamal Hadi Salim 
129015e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
129128d8909bSJamal Hadi Salim }
129228d8909bSJamal Hadi Salim 
129322e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
12945424f32eSThomas Graf 		struct nlattr **attrs)
12951da177e4SLinus Torvalds {
1296fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12977b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
12981da177e4SLinus Torvalds 	struct xfrm_state *x;
12991da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1300eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
13011da177e4SLinus Torvalds 
1302fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
13031da177e4SLinus Torvalds 	if (x == NULL)
13041da177e4SLinus Torvalds 		goto out_noput;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
13071da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
13081da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
13091da177e4SLinus Torvalds 	} else {
131015e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
13111da177e4SLinus Torvalds 	}
13121da177e4SLinus Torvalds 	xfrm_state_put(x);
13131da177e4SLinus Torvalds out_noput:
13141da177e4SLinus Torvalds 	return err;
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds 
131722e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
13185424f32eSThomas Graf 		struct nlattr **attrs)
13191da177e4SLinus Torvalds {
1320fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
13211da177e4SLinus Torvalds 	struct xfrm_state *x;
13221da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
13231da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
13241da177e4SLinus Torvalds 	xfrm_address_t *daddr;
13251da177e4SLinus Torvalds 	int family;
13261da177e4SLinus Torvalds 	int err;
13276f26b61eSJamal Hadi Salim 	u32 mark;
13286f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
13297e652640SSteffen Klassert 	u32 if_id = 0;
13301da177e4SLinus Torvalds 
13317b67c857SThomas Graf 	p = nlmsg_data(nlh);
1332776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
13331da177e4SLinus Torvalds 	if (err)
13341da177e4SLinus Torvalds 		goto out_noput;
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	family = p->info.family;
13371da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 	x = NULL;
13406f26b61eSJamal Hadi Salim 
13416f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
13427e652640SSteffen Klassert 
13437e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
13447e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
13457e652640SSteffen Klassert 
13461da177e4SLinus Torvalds 	if (p->info.seq) {
13476f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
134870e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
13491da177e4SLinus Torvalds 			xfrm_state_put(x);
13501da177e4SLinus Torvalds 			x = NULL;
13511da177e4SLinus Torvalds 		}
13521da177e4SLinus Torvalds 	}
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	if (!x)
13556f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
13567e652640SSteffen Klassert 				  if_id, p->info.id.proto, daddr,
13571da177e4SLinus Torvalds 				  &p->info.saddr, 1,
13581da177e4SLinus Torvalds 				  family);
13591da177e4SLinus Torvalds 	err = -ENOENT;
13601da177e4SLinus Torvalds 	if (x == NULL)
13611da177e4SLinus Torvalds 		goto out_noput;
13621da177e4SLinus Torvalds 
1363658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1364658b219eSHerbert Xu 	if (err)
1365658b219eSHerbert Xu 		goto out;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
13681da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
13691da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
13701da177e4SLinus Torvalds 		goto out;
13711da177e4SLinus Torvalds 	}
13721da177e4SLinus Torvalds 
137315e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds out:
13761da177e4SLinus Torvalds 	xfrm_state_put(x);
13771da177e4SLinus Torvalds out_noput:
13781da177e4SLinus Torvalds 	return err;
13791da177e4SLinus Torvalds }
13801da177e4SLinus Torvalds 
1381b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
13821da177e4SLinus Torvalds {
13831da177e4SLinus Torvalds 	switch (dir) {
13841da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
13851da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
13861da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
13871da177e4SLinus Torvalds 		break;
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 	default:
13901da177e4SLinus Torvalds 		return -EINVAL;
13913ff50b79SStephen Hemminger 	}
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	return 0;
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
1396b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1397f7b6983fSMasahide NAKAMURA {
1398f7b6983fSMasahide NAKAMURA 	switch (type) {
1399f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1400f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1401f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1402f7b6983fSMasahide NAKAMURA #endif
1403f7b6983fSMasahide NAKAMURA 		break;
1404f7b6983fSMasahide NAKAMURA 
1405f7b6983fSMasahide NAKAMURA 	default:
1406f7b6983fSMasahide NAKAMURA 		return -EINVAL;
14073ff50b79SStephen Hemminger 	}
1408f7b6983fSMasahide NAKAMURA 
1409f7b6983fSMasahide NAKAMURA 	return 0;
1410f7b6983fSMasahide NAKAMURA }
1411f7b6983fSMasahide NAKAMURA 
14121da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
14131da177e4SLinus Torvalds {
1414e682adf0SFan Du 	int ret;
1415e682adf0SFan Du 
14161da177e4SLinus Torvalds 	switch (p->share) {
14171da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
14181da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
14191da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
14201da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
14211da177e4SLinus Torvalds 		break;
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	default:
14241da177e4SLinus Torvalds 		return -EINVAL;
14253ff50b79SStephen Hemminger 	}
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	switch (p->action) {
14281da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
14291da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
14301da177e4SLinus Torvalds 		break;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	default:
14331da177e4SLinus Torvalds 		return -EINVAL;
14343ff50b79SStephen Hemminger 	}
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 	switch (p->sel.family) {
14371da177e4SLinus Torvalds 	case AF_INET:
143807bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
143907bf7908SSteffen Klassert 			return -EINVAL;
144007bf7908SSteffen Klassert 
14411da177e4SLinus Torvalds 		break;
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	case AF_INET6:
1444dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
144507bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
144607bf7908SSteffen Klassert 			return -EINVAL;
144707bf7908SSteffen Klassert 
14481da177e4SLinus Torvalds 		break;
14491da177e4SLinus Torvalds #else
14501da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
14511da177e4SLinus Torvalds #endif
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 	default:
14541da177e4SLinus Torvalds 		return -EINVAL;
14553ff50b79SStephen Hemminger 	}
14561da177e4SLinus Torvalds 
1457e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1458e682adf0SFan Du 	if (ret)
1459e682adf0SFan Du 		return ret;
1460b805d78dSYueHaibing 	if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1461e682adf0SFan Du 		return -EINVAL;
1462e682adf0SFan Du 
1463e682adf0SFan Du 	return 0;
14641da177e4SLinus Torvalds }
14651da177e4SLinus Torvalds 
14665424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1467df71837dSTrent Jaeger {
14685424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1469df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1470df71837dSTrent Jaeger 
1471df71837dSTrent Jaeger 	if (!rt)
1472df71837dSTrent Jaeger 		return 0;
1473df71837dSTrent Jaeger 
14745424f32eSThomas Graf 	uctx = nla_data(rt);
147552a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1476df71837dSTrent Jaeger }
1477df71837dSTrent Jaeger 
14781da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
14791da177e4SLinus Torvalds 			   int nr)
14801da177e4SLinus Torvalds {
14811da177e4SLinus Torvalds 	int i;
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
14841da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
14851da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
14881da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
14891da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
14901da177e4SLinus Torvalds 		t->reqid = ut->reqid;
14911da177e4SLinus Torvalds 		t->mode = ut->mode;
14921da177e4SLinus Torvalds 		t->share = ut->share;
14931da177e4SLinus Torvalds 		t->optional = ut->optional;
14941da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
14951da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
14961da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1497c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1498c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
14998511d01dSMiika Komu 		t->encap_family = ut->family;
15001da177e4SLinus Torvalds 	}
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
1503b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1504b4ad86bfSDavid S. Miller {
1505732706afSSteffen Klassert 	u16 prev_family;
1506b4ad86bfSDavid S. Miller 	int i;
1507b4ad86bfSDavid S. Miller 
1508b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1509b4ad86bfSDavid S. Miller 		return -EINVAL;
1510b4ad86bfSDavid S. Miller 
1511732706afSSteffen Klassert 	prev_family = family;
1512732706afSSteffen Klassert 
1513b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1514b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1515b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1516b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1517b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1518b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1519b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1520b4ad86bfSDavid S. Miller 		 */
1521b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1522b4ad86bfSDavid S. Miller 			ut[i].family = family;
1523b4ad86bfSDavid S. Miller 
152435e61038SFlorian Westphal 		switch (ut[i].mode) {
152535e61038SFlorian Westphal 		case XFRM_MODE_TUNNEL:
152635e61038SFlorian Westphal 		case XFRM_MODE_BEET:
152735e61038SFlorian Westphal 			break;
152835e61038SFlorian Westphal 		default:
152935e61038SFlorian Westphal 			if (ut[i].family != prev_family)
1530732706afSSteffen Klassert 				return -EINVAL;
153135e61038SFlorian Westphal 			break;
153235e61038SFlorian Westphal 		}
153332bf94fbSSean Tranchetti 		if (ut[i].mode >= XFRM_MODE_MAX)
153432bf94fbSSean Tranchetti 			return -EINVAL;
153532bf94fbSSean Tranchetti 
1536732706afSSteffen Klassert 		prev_family = ut[i].family;
1537732706afSSteffen Klassert 
1538b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1539b4ad86bfSDavid S. Miller 		case AF_INET:
1540b4ad86bfSDavid S. Miller 			break;
1541dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1542b4ad86bfSDavid S. Miller 		case AF_INET6:
1543b4ad86bfSDavid S. Miller 			break;
1544b4ad86bfSDavid S. Miller #endif
1545b4ad86bfSDavid S. Miller 		default:
1546b4ad86bfSDavid S. Miller 			return -EINVAL;
15473ff50b79SStephen Hemminger 		}
15486a53b759SCong Wang 
1549dbb2483bSCong Wang 		if (!xfrm_id_proto_valid(ut[i].id.proto))
15506a53b759SCong Wang 			return -EINVAL;
15516a53b759SCong Wang 	}
15526a53b759SCong Wang 
1553b4ad86bfSDavid S. Miller 	return 0;
1554b4ad86bfSDavid S. Miller }
1555b4ad86bfSDavid S. Miller 
15565424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
15571da177e4SLinus Torvalds {
15585424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	if (!rt) {
15611da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
15621da177e4SLinus Torvalds 	} else {
15635424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
15645424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1565b4ad86bfSDavid S. Miller 		int err;
15661da177e4SLinus Torvalds 
1567b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1568b4ad86bfSDavid S. Miller 		if (err)
1569b4ad86bfSDavid S. Miller 			return err;
15701da177e4SLinus Torvalds 
15715424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
15721da177e4SLinus Torvalds 	}
15731da177e4SLinus Torvalds 	return 0;
15741da177e4SLinus Torvalds }
15751da177e4SLinus Torvalds 
15765424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1577f7b6983fSMasahide NAKAMURA {
15785424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1579f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1580b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1581f7b6983fSMasahide NAKAMURA 	int err;
1582f7b6983fSMasahide NAKAMURA 
1583f7b6983fSMasahide NAKAMURA 	if (rt) {
15845424f32eSThomas Graf 		upt = nla_data(rt);
1585f7b6983fSMasahide NAKAMURA 		type = upt->type;
1586f7b6983fSMasahide NAKAMURA 	}
1587f7b6983fSMasahide NAKAMURA 
1588f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1589f7b6983fSMasahide NAKAMURA 	if (err)
1590f7b6983fSMasahide NAKAMURA 		return err;
1591f7b6983fSMasahide NAKAMURA 
1592f7b6983fSMasahide NAKAMURA 	*tp = type;
1593f7b6983fSMasahide NAKAMURA 	return 0;
1594f7b6983fSMasahide NAKAMURA }
1595f7b6983fSMasahide NAKAMURA 
15961da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
15971da177e4SLinus Torvalds {
15981da177e4SLinus Torvalds 	xp->priority = p->priority;
15991da177e4SLinus Torvalds 	xp->index = p->index;
16001da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
16011da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
16021da177e4SLinus Torvalds 	xp->action = p->action;
16031da177e4SLinus Torvalds 	xp->flags = p->flags;
16041da177e4SLinus Torvalds 	xp->family = p->sel.family;
16051da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
16061da177e4SLinus Torvalds }
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
16091da177e4SLinus Torvalds {
16107b789836SMathias Krause 	memset(p, 0, sizeof(*p));
16111da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
16121da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
16131da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
16141da177e4SLinus Torvalds 	p->priority = xp->priority;
16151da177e4SLinus Torvalds 	p->index = xp->index;
16161da177e4SLinus Torvalds 	p->sel.family = xp->family;
16171da177e4SLinus Torvalds 	p->dir = dir;
16181da177e4SLinus Torvalds 	p->action = xp->action;
16191da177e4SLinus Torvalds 	p->flags = xp->flags;
16201da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
16211da177e4SLinus Torvalds }
16221da177e4SLinus Torvalds 
1623fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
16241da177e4SLinus Torvalds {
1625fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
16261da177e4SLinus Torvalds 	int err;
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	if (!xp) {
16291da177e4SLinus Torvalds 		*errp = -ENOMEM;
16301da177e4SLinus Torvalds 		return NULL;
16311da177e4SLinus Torvalds 	}
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1634df71837dSTrent Jaeger 
163535a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1636f7b6983fSMasahide NAKAMURA 	if (err)
1637f7b6983fSMasahide NAKAMURA 		goto error;
1638f7b6983fSMasahide NAKAMURA 
163935a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
164035a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1641f7b6983fSMasahide NAKAMURA 	if (err)
1642f7b6983fSMasahide NAKAMURA 		goto error;
16431da177e4SLinus Torvalds 
1644295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1645295fae56SJamal Hadi Salim 
16467e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
16477e652640SSteffen Klassert 		xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
16487e652640SSteffen Klassert 
16491da177e4SLinus Torvalds 	return xp;
1650f7b6983fSMasahide NAKAMURA  error:
1651f7b6983fSMasahide NAKAMURA 	*errp = err;
165212a169e7SHerbert Xu 	xp->walk.dead = 1;
165364c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1654f7b6983fSMasahide NAKAMURA 	return NULL;
16551da177e4SLinus Torvalds }
16561da177e4SLinus Torvalds 
165722e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
16585424f32eSThomas Graf 		struct nlattr **attrs)
16591da177e4SLinus Torvalds {
1660fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
16617b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
16621da177e4SLinus Torvalds 	struct xfrm_policy *xp;
166326b15dadSJamal Hadi Salim 	struct km_event c;
16641da177e4SLinus Torvalds 	int err;
16651da177e4SLinus Torvalds 	int excl;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
16681da177e4SLinus Torvalds 	if (err)
16691da177e4SLinus Torvalds 		return err;
167035a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1671df71837dSTrent Jaeger 	if (err)
1672df71837dSTrent Jaeger 		return err;
16731da177e4SLinus Torvalds 
1674fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
16751da177e4SLinus Torvalds 	if (!xp)
16761da177e4SLinus Torvalds 		return err;
16771da177e4SLinus Torvalds 
167825985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
167926b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
168026b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
168126b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
16821da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
16831da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
16842e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1685161a09e7SJoy Latten 
16861da177e4SLinus Torvalds 	if (err) {
168703e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
16881da177e4SLinus Torvalds 		kfree(xp);
16891da177e4SLinus Torvalds 		return err;
16901da177e4SLinus Torvalds 	}
16911da177e4SLinus Torvalds 
1692f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
169326b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
169415e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
169526b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
169626b15dadSJamal Hadi Salim 
16971da177e4SLinus Torvalds 	xfrm_pol_put(xp);
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	return 0;
17001da177e4SLinus Torvalds }
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
17031da177e4SLinus Torvalds {
17041da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
17051da177e4SLinus Torvalds 	int i;
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
17081da177e4SLinus Torvalds 		return 0;
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
17111da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
17121da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
17131da177e4SLinus Torvalds 
17141f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
17151da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
17168511d01dSMiika Komu 		up->family = kp->encap_family;
17171da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
17181da177e4SLinus Torvalds 		up->reqid = kp->reqid;
17191da177e4SLinus Torvalds 		up->mode = kp->mode;
17201da177e4SLinus Torvalds 		up->share = kp->share;
17211da177e4SLinus Torvalds 		up->optional = kp->optional;
17221da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
17231da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
17241da177e4SLinus Torvalds 		up->calgos = kp->calgos;
17251da177e4SLinus Torvalds 	}
17261da177e4SLinus Torvalds 
1727c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1728c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1729df71837dSTrent Jaeger }
1730df71837dSTrent Jaeger 
17310d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
17320d681623SSerge Hallyn {
17330d681623SSerge Hallyn 	if (x->security) {
17340d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
17350d681623SSerge Hallyn 	}
17360d681623SSerge Hallyn 	return 0;
17370d681623SSerge Hallyn }
17380d681623SSerge Hallyn 
17390d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
17400d681623SSerge Hallyn {
17411d1e34ddSDavid S. Miller 	if (xp->security)
17420d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
17430d681623SSerge Hallyn 	return 0;
17440d681623SSerge Hallyn }
1745a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void)
1746cfbfd45aSThomas Graf {
1747cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1748cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1749cfbfd45aSThomas Graf #else
1750cfbfd45aSThomas Graf 	return 0;
1751cfbfd45aSThomas Graf #endif
1752cfbfd45aSThomas Graf }
17530d681623SSerge Hallyn 
1754f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1755b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1756f7b6983fSMasahide NAKAMURA {
175745c180bcSEric Dumazet 	struct xfrm_userpolicy_type upt;
175845c180bcSEric Dumazet 
175945c180bcSEric Dumazet 	/* Sadly there are two holes in struct xfrm_userpolicy_type */
176045c180bcSEric Dumazet 	memset(&upt, 0, sizeof(upt));
176145c180bcSEric Dumazet 	upt.type = type;
1762f7b6983fSMasahide NAKAMURA 
1763c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1764f7b6983fSMasahide NAKAMURA }
1765f7b6983fSMasahide NAKAMURA 
1766f7b6983fSMasahide NAKAMURA #else
1767b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1768f7b6983fSMasahide NAKAMURA {
1769f7b6983fSMasahide NAKAMURA 	return 0;
1770f7b6983fSMasahide NAKAMURA }
1771f7b6983fSMasahide NAKAMURA #endif
1772f7b6983fSMasahide NAKAMURA 
17731da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
17741da177e4SLinus Torvalds {
17751da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
17761da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
17771da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
17781da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
17791da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
17801d1e34ddSDavid S. Miller 	int err;
17811da177e4SLinus Torvalds 
178215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
178379b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
178479b8b7f4SThomas Graf 	if (nlh == NULL)
178579b8b7f4SThomas Graf 		return -EMSGSIZE;
17861da177e4SLinus Torvalds 
17877b67c857SThomas Graf 	p = nlmsg_data(nlh);
17881da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
17891d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
17901d1e34ddSDavid S. Miller 	if (!err)
17911d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
17921d1e34ddSDavid S. Miller 	if (!err)
17931d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
17941d1e34ddSDavid S. Miller 	if (!err)
17951d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
17967e652640SSteffen Klassert 	if (!err)
17977e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
17981d1e34ddSDavid S. Miller 	if (err) {
17991d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
18001d1e34ddSDavid S. Miller 		return err;
18011d1e34ddSDavid S. Miller 	}
18029825069dSThomas Graf 	nlmsg_end(skb, nlh);
18031da177e4SLinus Torvalds 	return 0;
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
18064c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
18074c563f76STimo Teras {
18081137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1809283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
18104c563f76STimo Teras 
1811283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
18124c563f76STimo Teras 	return 0;
18134c563f76STimo Teras }
18144c563f76STimo Teras 
18151137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb)
18161137b5e2SHerbert Xu {
18171137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
18181137b5e2SHerbert Xu 
18191137b5e2SHerbert Xu 	BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
18201137b5e2SHerbert Xu 
18211137b5e2SHerbert Xu 	xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
18221137b5e2SHerbert Xu 	return 0;
18231137b5e2SHerbert Xu }
18241137b5e2SHerbert Xu 
18251da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
18261da177e4SLinus Torvalds {
1827fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
18281137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
18291da177e4SLinus Torvalds 	struct xfrm_dump_info info;
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 	info.in_skb = cb->skb;
18321da177e4SLinus Torvalds 	info.out_skb = skb;
18331da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
18341da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
18354c563f76STimo Teras 
1836fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds 	return skb->len;
18391da177e4SLinus Torvalds }
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
18421da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
18431da177e4SLinus Torvalds 					  int dir, u32 seq)
18441da177e4SLinus Torvalds {
18451da177e4SLinus Torvalds 	struct xfrm_dump_info info;
18461da177e4SLinus Torvalds 	struct sk_buff *skb;
1847c2546372SMathias Krause 	int err;
18481da177e4SLinus Torvalds 
18497deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18501da177e4SLinus Torvalds 	if (!skb)
18511da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 	info.in_skb = in_skb;
18541da177e4SLinus Torvalds 	info.out_skb = skb;
18551da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
18561da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
18571da177e4SLinus Torvalds 
1858c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1859c2546372SMathias Krause 	if (err) {
18601da177e4SLinus Torvalds 		kfree_skb(skb);
1861c2546372SMathias Krause 		return ERR_PTR(err);
18621da177e4SLinus Torvalds 	}
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	return skb;
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds 
186722e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
18685424f32eSThomas Graf 		struct nlattr **attrs)
18691da177e4SLinus Torvalds {
1870fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
18711da177e4SLinus Torvalds 	struct xfrm_policy *xp;
18721da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1873b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
18741da177e4SLinus Torvalds 	int err;
187526b15dadSJamal Hadi Salim 	struct km_event c;
18761da177e4SLinus Torvalds 	int delete;
1877295fae56SJamal Hadi Salim 	struct xfrm_mark m;
18787e652640SSteffen Klassert 	u32 if_id = 0;
18791da177e4SLinus Torvalds 
18807b67c857SThomas Graf 	p = nlmsg_data(nlh);
18811da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
18821da177e4SLinus Torvalds 
188335a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1884f7b6983fSMasahide NAKAMURA 	if (err)
1885f7b6983fSMasahide NAKAMURA 		return err;
1886f7b6983fSMasahide NAKAMURA 
18871da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
18881da177e4SLinus Torvalds 	if (err)
18891da177e4SLinus Torvalds 		return err;
18901da177e4SLinus Torvalds 
18917e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
18927e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
18937e652640SSteffen Klassert 
18944f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
18954f47e8abSXin Long 
18961da177e4SLinus Torvalds 	if (p->index)
18974f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
18984f47e8abSXin Long 				      p->index, delete, &err);
1899df71837dSTrent Jaeger 	else {
19005424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
190103e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1902df71837dSTrent Jaeger 
190335a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
1904df71837dSTrent Jaeger 		if (err)
1905df71837dSTrent Jaeger 			return err;
1906df71837dSTrent Jaeger 
19072c8dd116SDenis V. Lunev 		ctx = NULL;
1908df71837dSTrent Jaeger 		if (rt) {
19095424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1910df71837dSTrent Jaeger 
191152a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
191203e1ad7bSPaul Moore 			if (err)
1913df71837dSTrent Jaeger 				return err;
19142c8dd116SDenis V. Lunev 		}
19154f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
19164f47e8abSXin Long 					   &p->sel, ctx, delete, &err);
191703e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
1918df71837dSTrent Jaeger 	}
19191da177e4SLinus Torvalds 	if (xp == NULL)
19201da177e4SLinus Torvalds 		return -ENOENT;
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds 	if (!delete) {
19231da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
19261da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
19271da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
19281da177e4SLinus Torvalds 		} else {
1929a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
193015e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
19311da177e4SLinus Torvalds 		}
193226b15dadSJamal Hadi Salim 	} else {
19332e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
193413fcfbb0SDavid S. Miller 
193513fcfbb0SDavid S. Miller 		if (err != 0)
1936c8c05a8eSCatherine Zhang 			goto out;
193713fcfbb0SDavid S. Miller 
1938e7443892SHerbert Xu 		c.data.byid = p->index;
1939f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
194026b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
194115e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
194226b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
19431da177e4SLinus Torvalds 	}
19441da177e4SLinus Torvalds 
1945c8c05a8eSCatherine Zhang out:
1946ef41aaa0SEric Paris 	xfrm_pol_put(xp);
19471da177e4SLinus Torvalds 	return err;
19481da177e4SLinus Torvalds }
19491da177e4SLinus Torvalds 
195022e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
19515424f32eSThomas Graf 		struct nlattr **attrs)
19521da177e4SLinus Torvalds {
1953fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
195426b15dadSJamal Hadi Salim 	struct km_event c;
19557b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
19564aa2e62cSJoy Latten 	int err;
19571da177e4SLinus Torvalds 
1958f75a2804SCong Wang 	err = xfrm_state_flush(net, p->proto, true, false);
19599e64cc95SJamal Hadi Salim 	if (err) {
19609e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
19619e64cc95SJamal Hadi Salim 			return 0;
1962069c474eSDavid S. Miller 		return err;
19639e64cc95SJamal Hadi Salim 	}
1964bf08867fSHerbert Xu 	c.data.proto = p->proto;
1965f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
196626b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
196715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
19687067802eSAlexey Dobriyan 	c.net = net;
196926b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
197026b15dadSJamal Hadi Salim 
19711da177e4SLinus Torvalds 	return 0;
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds 
1974a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
19757deb2264SThomas Graf {
1976a1b831f2SAlexey Dobriyan 	unsigned int replay_size = x->replay_esn ?
1977d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
1978d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
1979d8647b79SSteffen Klassert 
19807deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1981d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
1982de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
19836f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
19847deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
19857deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
19867deb2264SThomas Graf }
1987d51d081dSJamal Hadi Salim 
1988214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1989d51d081dSJamal Hadi Salim {
1990d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1991d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
19921d1e34ddSDavid S. Miller 	int err;
1993d51d081dSJamal Hadi Salim 
199415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
199579b8b7f4SThomas Graf 	if (nlh == NULL)
199679b8b7f4SThomas Graf 		return -EMSGSIZE;
1997d51d081dSJamal Hadi Salim 
19987b67c857SThomas Graf 	id = nlmsg_data(nlh);
1999931e79d7SMathias Krause 	memset(&id->sa_id, 0, sizeof(id->sa_id));
20002b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2001d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
2002d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
2003d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
20042b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
20052b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
2006d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
2007d51d081dSJamal Hadi Salim 
2008d0fde795SDavid S. Miller 	if (x->replay_esn) {
20091d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2010d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
20111d1e34ddSDavid S. Miller 			      x->replay_esn);
2012d0fde795SDavid S. Miller 	} else {
20131d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
20141d1e34ddSDavid S. Miller 			      &x->replay);
2015d0fde795SDavid S. Miller 	}
20161d1e34ddSDavid S. Miller 	if (err)
20171d1e34ddSDavid S. Miller 		goto out_cancel;
2018de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2019de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
20201d1e34ddSDavid S. Miller 	if (err)
20211d1e34ddSDavid S. Miller 		goto out_cancel;
2022d8647b79SSteffen Klassert 
20231d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
20241d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
20251d1e34ddSDavid S. Miller 		if (err)
20261d1e34ddSDavid S. Miller 			goto out_cancel;
20271d1e34ddSDavid S. Miller 	}
20281d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
20291d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
20301d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
20311d1e34ddSDavid S. Miller 		if (err)
20321d1e34ddSDavid S. Miller 			goto out_cancel;
20331d1e34ddSDavid S. Miller 	}
20341d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
20351d1e34ddSDavid S. Miller 	if (err)
20361d1e34ddSDavid S. Miller 		goto out_cancel;
20376f26b61eSJamal Hadi Salim 
20387e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
20397e652640SSteffen Klassert 	if (err)
20407e652640SSteffen Klassert 		goto out_cancel;
20417e652640SSteffen Klassert 
2042053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2043053c095aSJohannes Berg 	return 0;
2044d51d081dSJamal Hadi Salim 
20451d1e34ddSDavid S. Miller out_cancel:
20469825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
20471d1e34ddSDavid S. Miller 	return err;
2048d51d081dSJamal Hadi Salim }
2049d51d081dSJamal Hadi Salim 
205022e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
20515424f32eSThomas Graf 		struct nlattr **attrs)
2052d51d081dSJamal Hadi Salim {
2053fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2054d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2055d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
2056d51d081dSJamal Hadi Salim 	int err;
2057d51d081dSJamal Hadi Salim 	struct km_event c;
20586f26b61eSJamal Hadi Salim 	u32 mark;
20596f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
20607b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
2061d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
2062d51d081dSJamal Hadi Salim 
20636f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
20646f26b61eSJamal Hadi Salim 
20656f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2066d8647b79SSteffen Klassert 	if (x == NULL)
2067d51d081dSJamal Hadi Salim 		return -ESRCH;
2068d8647b79SSteffen Klassert 
2069d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2070d8647b79SSteffen Klassert 	if (r_skb == NULL) {
2071d8647b79SSteffen Klassert 		xfrm_state_put(x);
2072d8647b79SSteffen Klassert 		return -ENOMEM;
2073d51d081dSJamal Hadi Salim 	}
2074d51d081dSJamal Hadi Salim 
2075d51d081dSJamal Hadi Salim 	/*
2076d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
2077d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
2078d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
2079d51d081dSJamal Hadi Salim 	*/
2080d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2081d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
2082d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
208315e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2084d51d081dSJamal Hadi Salim 
20852fc5f83bSGustavo A. R. Silva 	err = build_aevent(r_skb, x, &c);
20862fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
20872fc5f83bSGustavo A. R. Silva 
208815e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2089d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2090d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2091d51d081dSJamal Hadi Salim 	return err;
2092d51d081dSJamal Hadi Salim }
2093d51d081dSJamal Hadi Salim 
209422e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
20955424f32eSThomas Graf 		struct nlattr **attrs)
2096d51d081dSJamal Hadi Salim {
2097fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2098d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2099d51d081dSJamal Hadi Salim 	struct km_event c;
2100d51d081dSJamal Hadi Salim 	int err = -EINVAL;
21016f26b61eSJamal Hadi Salim 	u32 mark = 0;
21026f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
21037b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
21045424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2105d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
21065424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
21074e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
21084e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2109d51d081dSJamal Hadi Salim 
21104e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
2111d51d081dSJamal Hadi Salim 		return err;
2112d51d081dSJamal Hadi Salim 
2113d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
2114d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2115d51d081dSJamal Hadi Salim 		return err;
2116d51d081dSJamal Hadi Salim 
21176f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
21186f26b61eSJamal Hadi Salim 
21196f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2120d51d081dSJamal Hadi Salim 	if (x == NULL)
2121d51d081dSJamal Hadi Salim 		return -ESRCH;
2122d51d081dSJamal Hadi Salim 
2123d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
2124d51d081dSJamal Hadi Salim 		goto out;
2125d51d081dSJamal Hadi Salim 
21264479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
2127e2b19125SSteffen Klassert 	if (err)
2128e2b19125SSteffen Klassert 		goto out;
2129e2b19125SSteffen Klassert 
2130d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2131e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
2132d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2133d51d081dSJamal Hadi Salim 
2134d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
2135d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
213615e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2137d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
2138d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
2139d51d081dSJamal Hadi Salim 	err = 0;
2140d51d081dSJamal Hadi Salim out:
2141d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2142d51d081dSJamal Hadi Salim 	return err;
2143d51d081dSJamal Hadi Salim }
2144d51d081dSJamal Hadi Salim 
214522e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
21465424f32eSThomas Graf 		struct nlattr **attrs)
21471da177e4SLinus Torvalds {
2148fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
214926b15dadSJamal Hadi Salim 	struct km_event c;
2150b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
2151f7b6983fSMasahide NAKAMURA 	int err;
215226b15dadSJamal Hadi Salim 
215335a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2154f7b6983fSMasahide NAKAMURA 	if (err)
2155f7b6983fSMasahide NAKAMURA 		return err;
2156f7b6983fSMasahide NAKAMURA 
21572e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
21582f1eb65fSJamal Hadi Salim 	if (err) {
21592f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
21602f1eb65fSJamal Hadi Salim 			return 0;
2161069c474eSDavid S. Miller 		return err;
21622f1eb65fSJamal Hadi Salim 	}
21632f1eb65fSJamal Hadi Salim 
2164f7b6983fSMasahide NAKAMURA 	c.data.type = type;
2165f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
216626b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
216715e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
21687067802eSAlexey Dobriyan 	c.net = net;
216926b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
21701da177e4SLinus Torvalds 	return 0;
21711da177e4SLinus Torvalds }
21721da177e4SLinus Torvalds 
217322e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
21745424f32eSThomas Graf 		struct nlattr **attrs)
21756c5c8ca7SJamal Hadi Salim {
2176fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
21776c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
21787b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
21796c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2180b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
21816c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2182295fae56SJamal Hadi Salim 	struct xfrm_mark m;
21837e652640SSteffen Klassert 	u32 if_id = 0;
21846c5c8ca7SJamal Hadi Salim 
218535a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2186f7b6983fSMasahide NAKAMURA 	if (err)
2187f7b6983fSMasahide NAKAMURA 		return err;
2188f7b6983fSMasahide NAKAMURA 
2189c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
2190c8bf4d04STimo Teräs 	if (err)
2191c8bf4d04STimo Teräs 		return err;
2192c8bf4d04STimo Teräs 
21937e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
21947e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
21957e652640SSteffen Klassert 
21964f47e8abSXin Long 	xfrm_mark_get(attrs, &m);
21974f47e8abSXin Long 
21986c5c8ca7SJamal Hadi Salim 	if (p->index)
21994f47e8abSXin Long 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
22004f47e8abSXin Long 				      0, &err);
22016c5c8ca7SJamal Hadi Salim 	else {
22025424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
220303e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
22046c5c8ca7SJamal Hadi Salim 
220535a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
22066c5c8ca7SJamal Hadi Salim 		if (err)
22076c5c8ca7SJamal Hadi Salim 			return err;
22086c5c8ca7SJamal Hadi Salim 
22092c8dd116SDenis V. Lunev 		ctx = NULL;
22106c5c8ca7SJamal Hadi Salim 		if (rt) {
22115424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
22126c5c8ca7SJamal Hadi Salim 
221352a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
221403e1ad7bSPaul Moore 			if (err)
22156c5c8ca7SJamal Hadi Salim 				return err;
22162c8dd116SDenis V. Lunev 		}
22174f47e8abSXin Long 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
22186f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
221903e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
22206c5c8ca7SJamal Hadi Salim 	}
22216c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2222ef41aaa0SEric Paris 		return -ENOENT;
222303e1ad7bSPaul Moore 
2224ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
22256c5c8ca7SJamal Hadi Salim 		goto out;
22266c5c8ca7SJamal Hadi Salim 
22276c5c8ca7SJamal Hadi Salim 	err = 0;
22286c5c8ca7SJamal Hadi Salim 	if (up->hard) {
22296c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
22302e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
22316c5c8ca7SJamal Hadi Salim 	}
2232c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
22336c5c8ca7SJamal Hadi Salim 
22346c5c8ca7SJamal Hadi Salim out:
22356c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
22366c5c8ca7SJamal Hadi Salim 	return err;
22376c5c8ca7SJamal Hadi Salim }
22386c5c8ca7SJamal Hadi Salim 
223922e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
22405424f32eSThomas Graf 		struct nlattr **attrs)
224153bc6b4dSJamal Hadi Salim {
2242fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
224353bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
224453bc6b4dSJamal Hadi Salim 	int err;
22457b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
224653bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
22476f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2248928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
224953bc6b4dSJamal Hadi Salim 
22506f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
225153bc6b4dSJamal Hadi Salim 
22523a765aa5SDavid S. Miller 	err = -ENOENT;
225353bc6b4dSJamal Hadi Salim 	if (x == NULL)
225453bc6b4dSJamal Hadi Salim 		return err;
225553bc6b4dSJamal Hadi Salim 
225653bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
22573a765aa5SDavid S. Miller 	err = -EINVAL;
225853bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
225953bc6b4dSJamal Hadi Salim 		goto out;
2260c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
226153bc6b4dSJamal Hadi Salim 
2262161a09e7SJoy Latten 	if (ue->hard) {
226353bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
22642e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2265161a09e7SJoy Latten 	}
22663a765aa5SDavid S. Miller 	err = 0;
226753bc6b4dSJamal Hadi Salim out:
226853bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
226953bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
227053bc6b4dSJamal Hadi Salim 	return err;
227153bc6b4dSJamal Hadi Salim }
227253bc6b4dSJamal Hadi Salim 
227322e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
22745424f32eSThomas Graf 		struct nlattr **attrs)
2275980ebd25SJamal Hadi Salim {
2276fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2277980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2278980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2279980ebd25SJamal Hadi Salim 	int i;
22805424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
22816f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2282980ebd25SJamal Hadi Salim 
22837b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2284fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2285980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2286980ebd25SJamal Hadi Salim 
2287980ebd25SJamal Hadi Salim 	if (!x)
2288d8eb9307SIlpo Järvinen 		goto nomem;
2289980ebd25SJamal Hadi Salim 
22906f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
22916f26b61eSJamal Hadi Salim 
2292980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2293d8eb9307SIlpo Järvinen 	if (err)
229473efc324SVegard Nossum 		goto free_state;
2295a1a7e3a3SXin Long 	err = verify_sec_ctx_len(attrs);
2296a1a7e3a3SXin Long 	if (err)
2297a1a7e3a3SXin Long 		goto free_state;
2298980ebd25SJamal Hadi Salim 
2299980ebd25SJamal Hadi Salim 	/*   build an XP */
2300fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2301d8eb9307SIlpo Järvinen 	if (!xp)
2302d8eb9307SIlpo Järvinen 		goto free_state;
2303980ebd25SJamal Hadi Salim 
2304980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2305980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2306980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
23076f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
23086f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
23095424f32eSThomas Graf 	ut = nla_data(rt);
2310980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2311980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2312980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2313980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2314980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2315980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2316980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2317980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2318980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2319980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2320980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2321980ebd25SJamal Hadi Salim 
2322980ebd25SJamal Hadi Salim 	}
2323980ebd25SJamal Hadi Salim 
23244a135e53SMathias Krause 	xfrm_state_free(x);
2325980ebd25SJamal Hadi Salim 	kfree(xp);
2326980ebd25SJamal Hadi Salim 
2327980ebd25SJamal Hadi Salim 	return 0;
2328d8eb9307SIlpo Järvinen 
2329d8eb9307SIlpo Järvinen free_state:
23304a135e53SMathias Krause 	xfrm_state_free(x);
2331d8eb9307SIlpo Järvinen nomem:
2332d8eb9307SIlpo Järvinen 	return err;
2333980ebd25SJamal Hadi Salim }
2334980ebd25SJamal Hadi Salim 
23355c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
23365c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
233713c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
23385424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
23395c79de6eSShinta Sugimoto {
23405424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
23415c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
23425c79de6eSShinta Sugimoto 	int i, num_migrate;
23435c79de6eSShinta Sugimoto 
234413c1d189SArnaud Ebalard 	if (k != NULL) {
234513c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
234613c1d189SArnaud Ebalard 
234713c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
234813c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
234913c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
235013c1d189SArnaud Ebalard 		k->family = uk->family;
235113c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
235213c1d189SArnaud Ebalard 	}
235313c1d189SArnaud Ebalard 
23545424f32eSThomas Graf 	um = nla_data(rt);
23555424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
23565c79de6eSShinta Sugimoto 
23575c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
23585c79de6eSShinta Sugimoto 		return -EINVAL;
23595c79de6eSShinta Sugimoto 
23605c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
23615c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
23625c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
23635c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
23645c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
23655c79de6eSShinta Sugimoto 
23665c79de6eSShinta Sugimoto 		ma->proto = um->proto;
23675c79de6eSShinta Sugimoto 		ma->mode = um->mode;
23685c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
23695c79de6eSShinta Sugimoto 
23705c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
23715c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
23725c79de6eSShinta Sugimoto 	}
23735c79de6eSShinta Sugimoto 
23745c79de6eSShinta Sugimoto 	*num = i;
23755c79de6eSShinta Sugimoto 	return 0;
23765c79de6eSShinta Sugimoto }
23775c79de6eSShinta Sugimoto 
23785c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
23795424f32eSThomas Graf 			   struct nlattr **attrs)
23805c79de6eSShinta Sugimoto {
23817b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
23825c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
238313c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
23845c79de6eSShinta Sugimoto 	u8 type;
23855c79de6eSShinta Sugimoto 	int err;
23865c79de6eSShinta Sugimoto 	int n = 0;
23878d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
23884ab47d47SAntony Antony 	struct xfrm_encap_tmpl  *encap = NULL;
23895c79de6eSShinta Sugimoto 
239035a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2391cf5cb79fSThomas Graf 		return -EINVAL;
23925c79de6eSShinta Sugimoto 
239313c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
239413c1d189SArnaud Ebalard 
23955424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
23965c79de6eSShinta Sugimoto 	if (err)
23975c79de6eSShinta Sugimoto 		return err;
23985c79de6eSShinta Sugimoto 
239913c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
24005c79de6eSShinta Sugimoto 	if (err)
24015c79de6eSShinta Sugimoto 		return err;
24025c79de6eSShinta Sugimoto 
24035c79de6eSShinta Sugimoto 	if (!n)
24045c79de6eSShinta Sugimoto 		return 0;
24055c79de6eSShinta Sugimoto 
24064ab47d47SAntony Antony 	if (attrs[XFRMA_ENCAP]) {
24074ab47d47SAntony Antony 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
24084ab47d47SAntony Antony 				sizeof(*encap), GFP_KERNEL);
24094ab47d47SAntony Antony 		if (!encap)
24105c79de6eSShinta Sugimoto 			return 0;
24115c79de6eSShinta Sugimoto 	}
24124ab47d47SAntony Antony 
24134ab47d47SAntony Antony 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
24144ab47d47SAntony Antony 
24154ab47d47SAntony Antony 	kfree(encap);
24164ab47d47SAntony Antony 
24174ab47d47SAntony Antony 	return err;
24184ab47d47SAntony Antony }
24195c79de6eSShinta Sugimoto #else
24205c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
24215424f32eSThomas Graf 			   struct nlattr **attrs)
24225c79de6eSShinta Sugimoto {
24235c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
24245c79de6eSShinta Sugimoto }
24255c79de6eSShinta Sugimoto #endif
24265c79de6eSShinta Sugimoto 
24275c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2428183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
24295c79de6eSShinta Sugimoto {
24305c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
24315c79de6eSShinta Sugimoto 
24325c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
24335c79de6eSShinta Sugimoto 	um.proto = m->proto;
24345c79de6eSShinta Sugimoto 	um.mode = m->mode;
24355c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
24365c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
24375c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
24385c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
24395c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
24405c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
24415c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
24425c79de6eSShinta Sugimoto 
2443c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
24445c79de6eSShinta Sugimoto }
24455c79de6eSShinta Sugimoto 
2446183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
244713c1d189SArnaud Ebalard {
244813c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
244913c1d189SArnaud Ebalard 
245013c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
245113c1d189SArnaud Ebalard 	uk.family = k->family;
245213c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
245313c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2454a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
245513c1d189SArnaud Ebalard 
245613c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
245713c1d189SArnaud Ebalard }
245813c1d189SArnaud Ebalard 
2459a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
24608bafd730SAntony Antony 						int with_encp)
24617deb2264SThomas Graf {
24627deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
246313c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
24648bafd730SAntony Antony 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
24657deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
24667deb2264SThomas Graf 	      + userpolicy_type_attrsize();
24677deb2264SThomas Graf }
24687deb2264SThomas Graf 
2469183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2470183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
24718bafd730SAntony Antony 			 const struct xfrm_selector *sel,
24728bafd730SAntony Antony 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
24735c79de6eSShinta Sugimoto {
2474183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
24755c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
24765c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
24771d1e34ddSDavid S. Miller 	int i, err;
24785c79de6eSShinta Sugimoto 
247979b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
248079b8b7f4SThomas Graf 	if (nlh == NULL)
248179b8b7f4SThomas Graf 		return -EMSGSIZE;
24825c79de6eSShinta Sugimoto 
24837b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
24845c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
24855c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
24865c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
24875c79de6eSShinta Sugimoto 	pol_id->dir = dir;
24885c79de6eSShinta Sugimoto 
24891d1e34ddSDavid S. Miller 	if (k != NULL) {
24901d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
24911d1e34ddSDavid S. Miller 		if (err)
24921d1e34ddSDavid S. Miller 			goto out_cancel;
24931d1e34ddSDavid S. Miller 	}
24948bafd730SAntony Antony 	if (encap) {
24958bafd730SAntony Antony 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
24968bafd730SAntony Antony 		if (err)
24978bafd730SAntony Antony 			goto out_cancel;
24988bafd730SAntony Antony 	}
24991d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
25001d1e34ddSDavid S. Miller 	if (err)
25011d1e34ddSDavid S. Miller 		goto out_cancel;
25025c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
25031d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
25041d1e34ddSDavid S. Miller 		if (err)
25051d1e34ddSDavid S. Miller 			goto out_cancel;
25065c79de6eSShinta Sugimoto 	}
25075c79de6eSShinta Sugimoto 
2508053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2509053c095aSJohannes Berg 	return 0;
25101d1e34ddSDavid S. Miller 
25111d1e34ddSDavid S. Miller out_cancel:
25129825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
25131d1e34ddSDavid S. Miller 	return err;
25145c79de6eSShinta Sugimoto }
25155c79de6eSShinta Sugimoto 
2516183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2517183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
25188bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
25198bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
25205c79de6eSShinta Sugimoto {
2521a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
25225c79de6eSShinta Sugimoto 	struct sk_buff *skb;
25232fc5f83bSGustavo A. R. Silva 	int err;
25245c79de6eSShinta Sugimoto 
25258bafd730SAntony Antony 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
25268bafd730SAntony Antony 			GFP_ATOMIC);
25275c79de6eSShinta Sugimoto 	if (skb == NULL)
25285c79de6eSShinta Sugimoto 		return -ENOMEM;
25295c79de6eSShinta Sugimoto 
25305c79de6eSShinta Sugimoto 	/* build migrate */
25312fc5f83bSGustavo A. R. Silva 	err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
25322fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
25335c79de6eSShinta Sugimoto 
253421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
25355c79de6eSShinta Sugimoto }
25365c79de6eSShinta Sugimoto #else
2537183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2538183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
25398bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
25408bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
25415c79de6eSShinta Sugimoto {
25425c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
25435c79de6eSShinta Sugimoto }
25445c79de6eSShinta Sugimoto #endif
2545d51d081dSJamal Hadi Salim 
2546a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2547492b558bSThomas Graf 
2548*5461fc0cSDmitry Safonov const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
254966f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2550492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2551492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2552492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2553492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2554492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2555492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2556980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
255753bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2558492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
255966f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
25606c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2561492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2562a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2563d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2564d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
256597a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
25665c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2567a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2568880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2569a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
25701da177e4SLinus Torvalds };
2571*5461fc0cSDmitry Safonov EXPORT_SYMBOL_GPL(xfrm_msg_min);
25721da177e4SLinus Torvalds 
2573492b558bSThomas Graf #undef XMSGSIZE
2574492b558bSThomas Graf 
2575cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2576c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2577c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2578c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2579c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
25801a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2581cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2582cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2583cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2584cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2585cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2586cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2587cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2588cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2589cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2590cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2591cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2592cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2593cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2594cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
259513c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
25966f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
259735d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2598d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2599a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2600d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2601870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2602d77e38e6SSteffen Klassert 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
26039b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK]	= { .type = NLA_U32 },
26049b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK_MASK]	= { .type = NLA_U32 },
26057e652640SSteffen Klassert 	[XFRMA_IF_ID]		= { .type = NLA_U32 },
2606cf5cb79fSThomas Graf };
2607cf5cb79fSThomas Graf 
2608880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2609880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2610880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2611880a6fabSChristophe Gouault };
2612880a6fabSChristophe Gouault 
261305600a79SMathias Krause static const struct xfrm_link {
26145424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
26151137b5e2SHerbert Xu 	int (*start)(struct netlink_callback *);
26161da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
26174c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2618880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2619880a6fabSChristophe Gouault 	int nla_max;
2620492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2621492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2622492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2623492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
26244c563f76STimo Teras 						   .dump = xfrm_dump_sa,
26254c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2626492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2627492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2628492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
26291137b5e2SHerbert Xu 						   .start = xfrm_dump_policy_start,
26304c563f76STimo Teras 						   .dump = xfrm_dump_policy,
26314c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2632492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2633980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
263453bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2635492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2636492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
26376c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2638492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2639492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2640d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2641d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
26425c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
264328d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2644880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2645880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2646880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2647ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
26481da177e4SLinus Torvalds };
26491da177e4SLinus Torvalds 
26502d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
26512d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
26521da177e4SLinus Torvalds {
2653a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
265435a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
265505600a79SMathias Krause 	const struct xfrm_link *link;
2656a7bd9a45SThomas Graf 	int type, err;
26571da177e4SLinus Torvalds 
26582bf8c476SAndy Lutomirski 	if (in_compat_syscall())
265983e2d058SYi Zhao 		return -EOPNOTSUPP;
266074005991SFan Du 
26611da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
26621da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
26631d00a4ebSThomas Graf 		return -EINVAL;
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
26661da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
266990f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
26701d00a4ebSThomas Graf 		return -EPERM;
26711da177e4SLinus Torvalds 
2672492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2673492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2674b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
26751da177e4SLinus Torvalds 		if (link->dump == NULL)
26761d00a4ebSThomas Graf 			return -EINVAL;
26771da177e4SLinus Torvalds 
267880d326faSPablo Neira Ayuso 		{
267980d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
26801137b5e2SHerbert Xu 				.start = link->start,
268180d326faSPablo Neira Ayuso 				.dump = link->dump,
268280d326faSPablo Neira Ayuso 				.done = link->done,
268380d326faSPablo Neira Ayuso 			};
268480d326faSPablo Neira Ayuso 			return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
268580d326faSPablo Neira Ayuso 		}
26861da177e4SLinus Torvalds 	}
26871da177e4SLinus Torvalds 
26888cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2689880a6fabSChristophe Gouault 				     link->nla_max ? : XFRMA_MAX,
2690fe52145fSJohannes Berg 				     link->nla_pol ? : xfrma_policy, extack);
2691a7bd9a45SThomas Graf 	if (err < 0)
2692a7bd9a45SThomas Graf 		return err;
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds 	if (link->doit == NULL)
26951d00a4ebSThomas Graf 		return -EINVAL;
26961da177e4SLinus Torvalds 
26975424f32eSThomas Graf 	return link->doit(skb, nlh, attrs);
26981da177e4SLinus Torvalds }
26991da177e4SLinus Torvalds 
2700cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
27011da177e4SLinus Torvalds {
2702283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2703283bc9f3SFan Du 
2704283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2705cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2706283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
27071da177e4SLinus Torvalds }
27081da177e4SLinus Torvalds 
2709a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void)
27107deb2264SThomas Graf {
27116f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
27126f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
27137deb2264SThomas Graf }
27147deb2264SThomas Graf 
2715214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
27161da177e4SLinus Torvalds {
27171da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
27181da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
27191d1e34ddSDavid S. Miller 	int err;
27201da177e4SLinus Torvalds 
272115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
272279b8b7f4SThomas Graf 	if (nlh == NULL)
272379b8b7f4SThomas Graf 		return -EMSGSIZE;
27241da177e4SLinus Torvalds 
27257b67c857SThomas Graf 	ue = nlmsg_data(nlh);
27261da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2727d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
2728e3e5fc16SMathias Krause 	/* clear the padding bytes */
2729e3e5fc16SMathias Krause 	memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
27301da177e4SLinus Torvalds 
27311d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
27321d1e34ddSDavid S. Miller 	if (err)
27331d1e34ddSDavid S. Miller 		return err;
27346f26b61eSJamal Hadi Salim 
27357e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
27367e652640SSteffen Klassert 	if (err)
27377e652640SSteffen Klassert 		return err;
27387e652640SSteffen Klassert 
2739053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2740053c095aSJohannes Berg 	return 0;
27411da177e4SLinus Torvalds }
27421da177e4SLinus Torvalds 
2743214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
27441da177e4SLinus Torvalds {
2745fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
27461da177e4SLinus Torvalds 	struct sk_buff *skb;
27471da177e4SLinus Torvalds 
27487deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
27491da177e4SLinus Torvalds 	if (skb == NULL)
27501da177e4SLinus Torvalds 		return -ENOMEM;
27511da177e4SLinus Torvalds 
27526f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
27536f26b61eSJamal Hadi Salim 		kfree_skb(skb);
27546f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
27556f26b61eSJamal Hadi Salim 	}
27561da177e4SLinus Torvalds 
275721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
27581da177e4SLinus Torvalds }
27591da177e4SLinus Torvalds 
2760214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2761d51d081dSJamal Hadi Salim {
2762fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2763d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
27642fc5f83bSGustavo A. R. Silva 	int err;
2765d51d081dSJamal Hadi Salim 
2766d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2767d51d081dSJamal Hadi Salim 	if (skb == NULL)
2768d51d081dSJamal Hadi Salim 		return -ENOMEM;
2769d51d081dSJamal Hadi Salim 
27702fc5f83bSGustavo A. R. Silva 	err = build_aevent(skb, x, c);
27712fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
2772d51d081dSJamal Hadi Salim 
277321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2774d51d081dSJamal Hadi Salim }
2775d51d081dSJamal Hadi Salim 
2776214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
277726b15dadSJamal Hadi Salim {
27787067802eSAlexey Dobriyan 	struct net *net = c->net;
277926b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
278026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
278126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
27827deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
278326b15dadSJamal Hadi Salim 
27847deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
278526b15dadSJamal Hadi Salim 	if (skb == NULL)
278626b15dadSJamal Hadi Salim 		return -ENOMEM;
278726b15dadSJamal Hadi Salim 
278815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
278979b8b7f4SThomas Graf 	if (nlh == NULL) {
279079b8b7f4SThomas Graf 		kfree_skb(skb);
279179b8b7f4SThomas Graf 		return -EMSGSIZE;
279279b8b7f4SThomas Graf 	}
279326b15dadSJamal Hadi Salim 
27947b67c857SThomas Graf 	p = nlmsg_data(nlh);
2795bf08867fSHerbert Xu 	p->proto = c->data.proto;
279626b15dadSJamal Hadi Salim 
27979825069dSThomas Graf 	nlmsg_end(skb, nlh);
279826b15dadSJamal Hadi Salim 
279921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
280026b15dadSJamal Hadi Salim }
280126b15dadSJamal Hadi Salim 
2802a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
280326b15dadSJamal Hadi Salim {
2804a1b831f2SAlexey Dobriyan 	unsigned int l = 0;
28051a6509d9SHerbert Xu 	if (x->aead)
28061a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
28074447bb33SMartin Willi 	if (x->aalg) {
28084447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
28094447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
28104447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
28114447bb33SMartin Willi 	}
281226b15dadSJamal Hadi Salim 	if (x->ealg)
28130f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
281426b15dadSJamal Hadi Salim 	if (x->calg)
28157deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
281626b15dadSJamal Hadi Salim 	if (x->encap)
28177deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
281835d2856bSMartin Willi 	if (x->tfcpad)
281935d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
2820d8647b79SSteffen Klassert 	if (x->replay_esn)
2821d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2822f293a5e3Sdingzhi 	else
2823f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
282468325d3bSHerbert Xu 	if (x->security)
282568325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
282668325d3bSHerbert Xu 				    x->security->ctx_len);
282768325d3bSHerbert Xu 	if (x->coaddr)
282868325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
2829a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
2830a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
2831d77e38e6SSteffen Klassert 	if (x->xso.dev)
2832d77e38e6SSteffen Klassert 		 l += nla_total_size(sizeof(x->xso));
28339b42c1f1SSteffen Klassert 	if (x->props.smark.v | x->props.smark.m) {
28349b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.v));
28359b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.m));
28369b42c1f1SSteffen Klassert 	}
28377e652640SSteffen Klassert 	if (x->if_id)
28387e652640SSteffen Klassert 		l += nla_total_size(sizeof(x->if_id));
283968325d3bSHerbert Xu 
2840d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2841de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
284226b15dadSJamal Hadi Salim 
284326b15dadSJamal Hadi Salim 	return l;
284426b15dadSJamal Hadi Salim }
284526b15dadSJamal Hadi Salim 
2846214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
284726b15dadSJamal Hadi Salim {
2848fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
284926b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
28500603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
285126b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
285226b15dadSJamal Hadi Salim 	struct sk_buff *skb;
2853a1b831f2SAlexey Dobriyan 	unsigned int len = xfrm_sa_len(x);
2854a1b831f2SAlexey Dobriyan 	unsigned int headlen;
2855a1b831f2SAlexey Dobriyan 	int err;
28560603eac0SHerbert Xu 
28570603eac0SHerbert Xu 	headlen = sizeof(*p);
28580603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
28597deb2264SThomas Graf 		len += nla_total_size(headlen);
28600603eac0SHerbert Xu 		headlen = sizeof(*id);
28616f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
28620603eac0SHerbert Xu 	}
28637deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
286426b15dadSJamal Hadi Salim 
28657deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
286626b15dadSJamal Hadi Salim 	if (skb == NULL)
286726b15dadSJamal Hadi Salim 		return -ENOMEM;
286826b15dadSJamal Hadi Salim 
286915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
28701d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
287179b8b7f4SThomas Graf 	if (nlh == NULL)
28721d1e34ddSDavid S. Miller 		goto out_free_skb;
287326b15dadSJamal Hadi Salim 
28747b67c857SThomas Graf 	p = nlmsg_data(nlh);
28750603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2876c0144beaSThomas Graf 		struct nlattr *attr;
2877c0144beaSThomas Graf 
28787b67c857SThomas Graf 		id = nlmsg_data(nlh);
287950329c8aSMathias Krause 		memset(id, 0, sizeof(*id));
28800603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
28810603eac0SHerbert Xu 		id->spi = x->id.spi;
28820603eac0SHerbert Xu 		id->family = x->props.family;
28830603eac0SHerbert Xu 		id->proto = x->id.proto;
28840603eac0SHerbert Xu 
2885c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
28861d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2887c0144beaSThomas Graf 		if (attr == NULL)
28881d1e34ddSDavid S. Miller 			goto out_free_skb;
2889c0144beaSThomas Graf 
2890c0144beaSThomas Graf 		p = nla_data(attr);
28910603eac0SHerbert Xu 	}
28921d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
28931d1e34ddSDavid S. Miller 	if (err)
28941d1e34ddSDavid S. Miller 		goto out_free_skb;
289526b15dadSJamal Hadi Salim 
28969825069dSThomas Graf 	nlmsg_end(skb, nlh);
289726b15dadSJamal Hadi Salim 
289821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
289926b15dadSJamal Hadi Salim 
29001d1e34ddSDavid S. Miller out_free_skb:
290126b15dadSJamal Hadi Salim 	kfree_skb(skb);
29021d1e34ddSDavid S. Miller 	return err;
290326b15dadSJamal Hadi Salim }
290426b15dadSJamal Hadi Salim 
2905214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
290626b15dadSJamal Hadi Salim {
290726b15dadSJamal Hadi Salim 
290826b15dadSJamal Hadi Salim 	switch (c->event) {
2909f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
291026b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2911d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2912d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2913f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2914f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2915f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
291626b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2917f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
291826b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
291926b15dadSJamal Hadi Salim 	default:
292062db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
292162db5cfdSstephen hemminger 		       c->event);
292226b15dadSJamal Hadi Salim 		break;
292326b15dadSJamal Hadi Salim 	}
292426b15dadSJamal Hadi Salim 
292526b15dadSJamal Hadi Salim 	return 0;
292626b15dadSJamal Hadi Salim 
292726b15dadSJamal Hadi Salim }
292826b15dadSJamal Hadi Salim 
2929a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
29307deb2264SThomas Graf 						struct xfrm_policy *xp)
29317deb2264SThomas Graf {
29327deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
29337deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
29346f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
29357deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
29367deb2264SThomas Graf 	       + userpolicy_type_attrsize();
29377deb2264SThomas Graf }
29387deb2264SThomas Graf 
29391da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
294065e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
29411da177e4SLinus Torvalds {
29421d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
29431da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
29441da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
29451d1e34ddSDavid S. Miller 	int err;
29461da177e4SLinus Torvalds 
294779b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
294879b8b7f4SThomas Graf 	if (nlh == NULL)
294979b8b7f4SThomas Graf 		return -EMSGSIZE;
29501da177e4SLinus Torvalds 
29517b67c857SThomas Graf 	ua = nlmsg_data(nlh);
29521da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
29531da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
29541da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
295565e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
29561da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
29571da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
29581da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
29591da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
29601da177e4SLinus Torvalds 
29611d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
29621d1e34ddSDavid S. Miller 	if (!err)
29631d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
29641d1e34ddSDavid S. Miller 	if (!err)
29651d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
29661d1e34ddSDavid S. Miller 	if (!err)
29671d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
29687e652640SSteffen Klassert 	if (!err)
29697e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
29701d1e34ddSDavid S. Miller 	if (err) {
29711d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
29721d1e34ddSDavid S. Miller 		return err;
29731d1e34ddSDavid S. Miller 	}
29741da177e4SLinus Torvalds 
2975053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2976053c095aSJohannes Berg 	return 0;
29771da177e4SLinus Torvalds }
29781da177e4SLinus Torvalds 
29791da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
298065e0736bSFan Du 			     struct xfrm_policy *xp)
29811da177e4SLinus Torvalds {
2982a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
29831da177e4SLinus Torvalds 	struct sk_buff *skb;
29842fc5f83bSGustavo A. R. Silva 	int err;
29851da177e4SLinus Torvalds 
29867deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
29871da177e4SLinus Torvalds 	if (skb == NULL)
29881da177e4SLinus Torvalds 		return -ENOMEM;
29891da177e4SLinus Torvalds 
29902fc5f83bSGustavo A. R. Silva 	err = build_acquire(skb, x, xt, xp);
29912fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
29921da177e4SLinus Torvalds 
299321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
29941da177e4SLinus Torvalds }
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
29971da177e4SLinus Torvalds  * or more templates.
29981da177e4SLinus Torvalds  */
2999cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
30001da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
30011da177e4SLinus Torvalds {
3002fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
30031da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
30041da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
30051da177e4SLinus Torvalds 	struct xfrm_policy *xp;
30061da177e4SLinus Torvalds 	int nr;
30071da177e4SLinus Torvalds 
3008cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
30091da177e4SLinus Torvalds 	case AF_INET:
30101da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
30111da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
30121da177e4SLinus Torvalds 			return NULL;
30131da177e4SLinus Torvalds 		}
30141da177e4SLinus Torvalds 		break;
3015dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
30161da177e4SLinus Torvalds 	case AF_INET6:
30171da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
30181da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
30191da177e4SLinus Torvalds 			return NULL;
30201da177e4SLinus Torvalds 		}
30211da177e4SLinus Torvalds 		break;
30221da177e4SLinus Torvalds #endif
30231da177e4SLinus Torvalds 	default:
30241da177e4SLinus Torvalds 		*dir = -EINVAL;
30251da177e4SLinus Torvalds 		return NULL;
30261da177e4SLinus Torvalds 	}
30271da177e4SLinus Torvalds 
30281da177e4SLinus Torvalds 	*dir = -EINVAL;
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
30311da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
30321da177e4SLinus Torvalds 		return NULL;
30331da177e4SLinus Torvalds 
30341da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
3035b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
30361da177e4SLinus Torvalds 		return NULL;
30371da177e4SLinus Torvalds 
3038a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
3039a4f1bac6SHerbert Xu 		return NULL;
3040a4f1bac6SHerbert Xu 
30412f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
30421da177e4SLinus Torvalds 	if (xp == NULL) {
30431da177e4SLinus Torvalds 		*dir = -ENOBUFS;
30441da177e4SLinus Torvalds 		return NULL;
30451da177e4SLinus Torvalds 	}
30461da177e4SLinus Torvalds 
30471da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
3048f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
30491da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
30501da177e4SLinus Torvalds 
30511da177e4SLinus Torvalds 	*dir = p->dir;
30521da177e4SLinus Torvalds 
30531da177e4SLinus Torvalds 	return xp;
30541da177e4SLinus Torvalds }
30551da177e4SLinus Torvalds 
3056a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
30577deb2264SThomas Graf {
30587deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
30597deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
30607deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3061295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
30627deb2264SThomas Graf 	       + userpolicy_type_attrsize();
30637deb2264SThomas Graf }
30647deb2264SThomas Graf 
30651da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3066214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
30671da177e4SLinus Torvalds {
30681da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
3069d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
30701d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
30711d1e34ddSDavid S. Miller 	int err;
30721da177e4SLinus Torvalds 
307315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
307479b8b7f4SThomas Graf 	if (nlh == NULL)
307579b8b7f4SThomas Graf 		return -EMSGSIZE;
30761da177e4SLinus Torvalds 
30777b67c857SThomas Graf 	upe = nlmsg_data(nlh);
30781da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
30791d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
30801d1e34ddSDavid S. Miller 	if (!err)
30811d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
30821d1e34ddSDavid S. Miller 	if (!err)
30831d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
30841d1e34ddSDavid S. Miller 	if (!err)
30851d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
30867e652640SSteffen Klassert 	if (!err)
30877e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
30881d1e34ddSDavid S. Miller 	if (err) {
30891d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
30901d1e34ddSDavid S. Miller 		return err;
30911d1e34ddSDavid S. Miller 	}
30921da177e4SLinus Torvalds 	upe->hard = !!hard;
30931da177e4SLinus Torvalds 
3094053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3095053c095aSJohannes Berg 	return 0;
30961da177e4SLinus Torvalds }
30971da177e4SLinus Torvalds 
3098214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
30991da177e4SLinus Torvalds {
3100fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
31011da177e4SLinus Torvalds 	struct sk_buff *skb;
31022fc5f83bSGustavo A. R. Silva 	int err;
31031da177e4SLinus Torvalds 
31047deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
31051da177e4SLinus Torvalds 	if (skb == NULL)
31061da177e4SLinus Torvalds 		return -ENOMEM;
31071da177e4SLinus Torvalds 
31082fc5f83bSGustavo A. R. Silva 	err = build_polexpire(skb, xp, dir, c);
31092fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
31101da177e4SLinus Torvalds 
311121ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
31121da177e4SLinus Torvalds }
31131da177e4SLinus Torvalds 
3114214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
311526b15dadSJamal Hadi Salim {
3116a1b831f2SAlexey Dobriyan 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3117fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
311826b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
31190603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
312026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
312126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3122a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3123a1b831f2SAlexey Dobriyan 	int err;
31240603eac0SHerbert Xu 
31250603eac0SHerbert Xu 	headlen = sizeof(*p);
31260603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
31277deb2264SThomas Graf 		len += nla_total_size(headlen);
31280603eac0SHerbert Xu 		headlen = sizeof(*id);
31290603eac0SHerbert Xu 	}
3130cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
3131295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
31327deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
313326b15dadSJamal Hadi Salim 
31347deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
313526b15dadSJamal Hadi Salim 	if (skb == NULL)
313626b15dadSJamal Hadi Salim 		return -ENOMEM;
313726b15dadSJamal Hadi Salim 
313815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
31391d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
314079b8b7f4SThomas Graf 	if (nlh == NULL)
31411d1e34ddSDavid S. Miller 		goto out_free_skb;
314226b15dadSJamal Hadi Salim 
31437b67c857SThomas Graf 	p = nlmsg_data(nlh);
31440603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
3145c0144beaSThomas Graf 		struct nlattr *attr;
3146c0144beaSThomas Graf 
31477b67c857SThomas Graf 		id = nlmsg_data(nlh);
31480603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
31490603eac0SHerbert Xu 		id->dir = dir;
31500603eac0SHerbert Xu 		if (c->data.byid)
31510603eac0SHerbert Xu 			id->index = xp->index;
31520603eac0SHerbert Xu 		else
31530603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
31540603eac0SHerbert Xu 
3155c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
31561d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3157c0144beaSThomas Graf 		if (attr == NULL)
31581d1e34ddSDavid S. Miller 			goto out_free_skb;
3159c0144beaSThomas Graf 
3160c0144beaSThomas Graf 		p = nla_data(attr);
31610603eac0SHerbert Xu 	}
316226b15dadSJamal Hadi Salim 
316326b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
31641d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
31651d1e34ddSDavid S. Miller 	if (!err)
31661d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
31671d1e34ddSDavid S. Miller 	if (!err)
31681d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
31697e652640SSteffen Klassert 	if (!err)
31707e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
31711d1e34ddSDavid S. Miller 	if (err)
31721d1e34ddSDavid S. Miller 		goto out_free_skb;
3173295fae56SJamal Hadi Salim 
31749825069dSThomas Graf 	nlmsg_end(skb, nlh);
317526b15dadSJamal Hadi Salim 
317621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
317726b15dadSJamal Hadi Salim 
31781d1e34ddSDavid S. Miller out_free_skb:
317926b15dadSJamal Hadi Salim 	kfree_skb(skb);
31801d1e34ddSDavid S. Miller 	return err;
318126b15dadSJamal Hadi Salim }
318226b15dadSJamal Hadi Salim 
3183214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
318426b15dadSJamal Hadi Salim {
31857067802eSAlexey Dobriyan 	struct net *net = c->net;
318626b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
318726b15dadSJamal Hadi Salim 	struct sk_buff *skb;
31881d1e34ddSDavid S. Miller 	int err;
318926b15dadSJamal Hadi Salim 
31907deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
319126b15dadSJamal Hadi Salim 	if (skb == NULL)
319226b15dadSJamal Hadi Salim 		return -ENOMEM;
319326b15dadSJamal Hadi Salim 
319415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
31951d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
319679b8b7f4SThomas Graf 	if (nlh == NULL)
31971d1e34ddSDavid S. Miller 		goto out_free_skb;
31981d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
31991d1e34ddSDavid S. Miller 	if (err)
32001d1e34ddSDavid S. Miller 		goto out_free_skb;
320126b15dadSJamal Hadi Salim 
32029825069dSThomas Graf 	nlmsg_end(skb, nlh);
320326b15dadSJamal Hadi Salim 
320421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
320526b15dadSJamal Hadi Salim 
32061d1e34ddSDavid S. Miller out_free_skb:
320726b15dadSJamal Hadi Salim 	kfree_skb(skb);
32081d1e34ddSDavid S. Miller 	return err;
320926b15dadSJamal Hadi Salim }
321026b15dadSJamal Hadi Salim 
3211214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
321226b15dadSJamal Hadi Salim {
321326b15dadSJamal Hadi Salim 
321426b15dadSJamal Hadi Salim 	switch (c->event) {
3215f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
3216f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
3217f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
321826b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
3219f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
322026b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
3221f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
322226b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
322326b15dadSJamal Hadi Salim 	default:
322462db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
322562db5cfdSstephen hemminger 		       c->event);
322626b15dadSJamal Hadi Salim 	}
322726b15dadSJamal Hadi Salim 
322826b15dadSJamal Hadi Salim 	return 0;
322926b15dadSJamal Hadi Salim 
323026b15dadSJamal Hadi Salim }
323126b15dadSJamal Hadi Salim 
3232a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void)
32337deb2264SThomas Graf {
32347deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
32357deb2264SThomas Graf }
32367deb2264SThomas Graf 
323797a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
323897a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
323997a64b45SMasahide NAKAMURA {
324097a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
324197a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
324297a64b45SMasahide NAKAMURA 
324379b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
324479b8b7f4SThomas Graf 	if (nlh == NULL)
324579b8b7f4SThomas Graf 		return -EMSGSIZE;
324697a64b45SMasahide NAKAMURA 
32477b67c857SThomas Graf 	ur = nlmsg_data(nlh);
324897a64b45SMasahide NAKAMURA 	ur->proto = proto;
324997a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
325097a64b45SMasahide NAKAMURA 
32511d1e34ddSDavid S. Miller 	if (addr) {
32521d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
32531d1e34ddSDavid S. Miller 		if (err) {
32549825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
32551d1e34ddSDavid S. Miller 			return err;
32561d1e34ddSDavid S. Miller 		}
32571d1e34ddSDavid S. Miller 	}
3258053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3259053c095aSJohannes Berg 	return 0;
326097a64b45SMasahide NAKAMURA }
326197a64b45SMasahide NAKAMURA 
3262db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3263db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
326497a64b45SMasahide NAKAMURA {
326597a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
32662fc5f83bSGustavo A. R. Silva 	int err;
326797a64b45SMasahide NAKAMURA 
32687deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
326997a64b45SMasahide NAKAMURA 	if (skb == NULL)
327097a64b45SMasahide NAKAMURA 		return -ENOMEM;
327197a64b45SMasahide NAKAMURA 
32722fc5f83bSGustavo A. R. Silva 	err = build_report(skb, proto, sel, addr);
32732fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
327497a64b45SMasahide NAKAMURA 
327521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
327697a64b45SMasahide NAKAMURA }
327797a64b45SMasahide NAKAMURA 
3278a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void)
32793a2dfbe8SMartin Willi {
32803a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
32813a2dfbe8SMartin Willi }
32823a2dfbe8SMartin Willi 
32833a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
32843a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
32853a2dfbe8SMartin Willi {
32863a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
32873a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
32883a2dfbe8SMartin Willi 
32893a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
32903a2dfbe8SMartin Willi 	if (nlh == NULL)
32913a2dfbe8SMartin Willi 		return -EMSGSIZE;
32923a2dfbe8SMartin Willi 
32933a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
32943a2dfbe8SMartin Willi 
32953a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
32963a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
32973a2dfbe8SMartin Willi 	um->id.family = x->props.family;
32983a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
32993a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
33003a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
33013a2dfbe8SMartin Willi 	um->new_sport = new_sport;
33023a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
33033a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
33043a2dfbe8SMartin Willi 
3305053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3306053c095aSJohannes Berg 	return 0;
33073a2dfbe8SMartin Willi }
33083a2dfbe8SMartin Willi 
33093a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
33103a2dfbe8SMartin Willi 			     __be16 sport)
33113a2dfbe8SMartin Willi {
3312a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
33133a2dfbe8SMartin Willi 	struct sk_buff *skb;
33142fc5f83bSGustavo A. R. Silva 	int err;
33153a2dfbe8SMartin Willi 
33163a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
33173a2dfbe8SMartin Willi 		return -EINVAL;
33183a2dfbe8SMartin Willi 
33193a2dfbe8SMartin Willi 	if (!x->encap)
33203a2dfbe8SMartin Willi 		return -EINVAL;
33213a2dfbe8SMartin Willi 
33223a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
33233a2dfbe8SMartin Willi 	if (skb == NULL)
33243a2dfbe8SMartin Willi 		return -ENOMEM;
33253a2dfbe8SMartin Willi 
33262fc5f83bSGustavo A. R. Silva 	err = build_mapping(skb, x, ipaddr, sport);
33272fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
33283a2dfbe8SMartin Willi 
332921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
33303a2dfbe8SMartin Willi }
33313a2dfbe8SMartin Willi 
33320f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
33330f24558eSHoria Geanta {
33340f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
33350f24558eSHoria Geanta }
33360f24558eSHoria Geanta 
33371da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
33381da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
33391da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
33401da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
33411da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
334297a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
33435c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
33443a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
33450f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
33461da177e4SLinus Torvalds };
33471da177e4SLinus Torvalds 
3348a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
33491da177e4SLinus Torvalds {
3350be33690dSPatrick McHardy 	struct sock *nlsk;
3351a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3352a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3353a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3354a31f2d17SPablo Neira Ayuso 	};
3355be33690dSPatrick McHardy 
33569f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3357be33690dSPatrick McHardy 	if (nlsk == NULL)
33581da177e4SLinus Torvalds 		return -ENOMEM;
3359d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3360cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
33611da177e4SLinus Torvalds 	return 0;
33621da177e4SLinus Torvalds }
33631da177e4SLinus Torvalds 
3364d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3365a6483b79SAlexey Dobriyan {
3366d79d792eSEric W. Biederman 	struct net *net;
3367d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3368a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3369d79d792eSEric W. Biederman 	synchronize_net();
3370d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3371d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3372a6483b79SAlexey Dobriyan }
3373a6483b79SAlexey Dobriyan 
3374a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3375a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
3376d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3377a6483b79SAlexey Dobriyan };
3378a6483b79SAlexey Dobriyan 
3379a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3380a6483b79SAlexey Dobriyan {
3381a6483b79SAlexey Dobriyan 	int rv;
3382a6483b79SAlexey Dobriyan 
3383a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3384a6483b79SAlexey Dobriyan 
3385a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3386a6483b79SAlexey Dobriyan 	if (rv < 0)
3387a6483b79SAlexey Dobriyan 		return rv;
3388a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3389a6483b79SAlexey Dobriyan 	if (rv < 0)
3390a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3391a6483b79SAlexey Dobriyan 	return rv;
3392a6483b79SAlexey Dobriyan }
3393a6483b79SAlexey Dobriyan 
33941da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
33951da177e4SLinus Torvalds {
33961da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3397a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
33981da177e4SLinus Torvalds }
33991da177e4SLinus Torvalds 
34001da177e4SLinus Torvalds module_init(xfrm_user_init);
34011da177e4SLinus Torvalds module_exit(xfrm_user_exit);
34021da177e4SLinus Torvalds MODULE_LICENSE("GPL");
34034fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3404