xref: /linux/net/xfrm/xfrm_user.c (revision 171d449a028573b2f0acdc7f31ecbb045391b320)
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);
113*171d449aSXin Long 	if (uctx->len > nla_len(rt) ||
114*171d449aSXin 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);
108621ee543eSMichal Kubecek 
108786126b77SFlorian Westphal 	if (!nlsk) {
108886126b77SFlorian Westphal 		kfree_skb(skb);
108986126b77SFlorian Westphal 		return -EPIPE;
109086126b77SFlorian Westphal 	}
109186126b77SFlorian Westphal 
109221ee543eSMichal Kubecek 	return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
109321ee543eSMichal Kubecek }
109421ee543eSMichal Kubecek 
1095a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_spdinfo_msgsize(void)
10967deb2264SThomas Graf {
10977deb2264SThomas Graf 	return NLMSG_ALIGN(4)
10987deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1099880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1100880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1101880a6fabSChristophe Gouault 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
11027deb2264SThomas Graf }
11037deb2264SThomas Graf 
1104e071041bSAlexey Dobriyan static int build_spdinfo(struct sk_buff *skb, struct net *net,
110515e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
1106ecfd6b18SJamal Hadi Salim {
11075a6d3416SJamal Hadi Salim 	struct xfrmk_spdinfo si;
11085a6d3416SJamal Hadi Salim 	struct xfrmu_spdinfo spc;
11095a6d3416SJamal Hadi Salim 	struct xfrmu_spdhinfo sph;
1110880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh spt4, spt6;
1111ecfd6b18SJamal Hadi Salim 	struct nlmsghdr *nlh;
11121d1e34ddSDavid S. Miller 	int err;
1113ecfd6b18SJamal Hadi Salim 	u32 *f;
1114880a6fabSChristophe Gouault 	unsigned lseq;
1115ecfd6b18SJamal Hadi Salim 
111615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
111725985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
1118ecfd6b18SJamal Hadi Salim 		return -EMSGSIZE;
1119ecfd6b18SJamal Hadi Salim 
1120ecfd6b18SJamal Hadi Salim 	f = nlmsg_data(nlh);
1121ecfd6b18SJamal Hadi Salim 	*f = flags;
1122e071041bSAlexey Dobriyan 	xfrm_spd_getinfo(net, &si);
11235a6d3416SJamal Hadi Salim 	spc.incnt = si.incnt;
11245a6d3416SJamal Hadi Salim 	spc.outcnt = si.outcnt;
11255a6d3416SJamal Hadi Salim 	spc.fwdcnt = si.fwdcnt;
11265a6d3416SJamal Hadi Salim 	spc.inscnt = si.inscnt;
11275a6d3416SJamal Hadi Salim 	spc.outscnt = si.outscnt;
11285a6d3416SJamal Hadi Salim 	spc.fwdscnt = si.fwdscnt;
11295a6d3416SJamal Hadi Salim 	sph.spdhcnt = si.spdhcnt;
11305a6d3416SJamal Hadi Salim 	sph.spdhmcnt = si.spdhmcnt;
1131ecfd6b18SJamal Hadi Salim 
1132880a6fabSChristophe Gouault 	do {
1133880a6fabSChristophe Gouault 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1134880a6fabSChristophe Gouault 
1135880a6fabSChristophe Gouault 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1136880a6fabSChristophe Gouault 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1137880a6fabSChristophe Gouault 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1138880a6fabSChristophe Gouault 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1139880a6fabSChristophe Gouault 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1140880a6fabSChristophe Gouault 
11411d1e34ddSDavid S. Miller 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
11421d1e34ddSDavid S. Miller 	if (!err)
11431d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1144880a6fabSChristophe Gouault 	if (!err)
1145880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1146880a6fabSChristophe Gouault 	if (!err)
1147880a6fabSChristophe Gouault 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
11481d1e34ddSDavid S. Miller 	if (err) {
11491d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
11501d1e34ddSDavid S. Miller 		return err;
11511d1e34ddSDavid S. Miller 	}
1152ecfd6b18SJamal Hadi Salim 
1153053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1154053c095aSJohannes Berg 	return 0;
1155ecfd6b18SJamal Hadi Salim }
1156ecfd6b18SJamal Hadi Salim 
1157880a6fabSChristophe Gouault static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1158880a6fabSChristophe Gouault 			    struct nlattr **attrs)
1159880a6fabSChristophe Gouault {
1160880a6fabSChristophe Gouault 	struct net *net = sock_net(skb->sk);
1161880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh4 = NULL;
1162880a6fabSChristophe Gouault 	struct xfrmu_spdhthresh *thresh6 = NULL;
1163880a6fabSChristophe Gouault 
1164880a6fabSChristophe Gouault 	/* selector prefixlen thresholds to hash policies */
1165880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1166880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1167880a6fabSChristophe Gouault 
1168880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh4))
1169880a6fabSChristophe Gouault 			return -EINVAL;
1170880a6fabSChristophe Gouault 		thresh4 = nla_data(rta);
1171880a6fabSChristophe Gouault 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1172880a6fabSChristophe Gouault 			return -EINVAL;
1173880a6fabSChristophe Gouault 	}
1174880a6fabSChristophe Gouault 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1175880a6fabSChristophe Gouault 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1176880a6fabSChristophe Gouault 
1177880a6fabSChristophe Gouault 		if (nla_len(rta) < sizeof(*thresh6))
1178880a6fabSChristophe Gouault 			return -EINVAL;
1179880a6fabSChristophe Gouault 		thresh6 = nla_data(rta);
1180880a6fabSChristophe Gouault 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1181880a6fabSChristophe Gouault 			return -EINVAL;
1182880a6fabSChristophe Gouault 	}
1183880a6fabSChristophe Gouault 
1184880a6fabSChristophe Gouault 	if (thresh4 || thresh6) {
1185880a6fabSChristophe Gouault 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1186880a6fabSChristophe Gouault 		if (thresh4) {
1187880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1188880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1189880a6fabSChristophe Gouault 		}
1190880a6fabSChristophe Gouault 		if (thresh6) {
1191880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1192880a6fabSChristophe Gouault 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1193880a6fabSChristophe Gouault 		}
1194880a6fabSChristophe Gouault 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1195880a6fabSChristophe Gouault 
1196880a6fabSChristophe Gouault 		xfrm_policy_hash_rebuild(net);
1197880a6fabSChristophe Gouault 	}
1198880a6fabSChristophe Gouault 
1199880a6fabSChristophe Gouault 	return 0;
1200880a6fabSChristophe Gouault }
1201880a6fabSChristophe Gouault 
1202ecfd6b18SJamal Hadi Salim static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12035424f32eSThomas Graf 		struct nlattr **attrs)
1204ecfd6b18SJamal Hadi Salim {
1205a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
1206ecfd6b18SJamal Hadi Salim 	struct sk_buff *r_skb;
12077b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
120815e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
1209ecfd6b18SJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
12102fc5f83bSGustavo A. R. Silva 	int err;
1211ecfd6b18SJamal Hadi Salim 
12127deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1213ecfd6b18SJamal Hadi Salim 	if (r_skb == NULL)
1214ecfd6b18SJamal Hadi Salim 		return -ENOMEM;
1215ecfd6b18SJamal Hadi Salim 
12162fc5f83bSGustavo A. R. Silva 	err = build_spdinfo(r_skb, net, sportid, seq, *flags);
12172fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
1218ecfd6b18SJamal Hadi Salim 
121915e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1220ecfd6b18SJamal Hadi Salim }
1221ecfd6b18SJamal Hadi Salim 
1222a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sadinfo_msgsize(void)
12237deb2264SThomas Graf {
12247deb2264SThomas Graf 	return NLMSG_ALIGN(4)
12257deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
12267deb2264SThomas Graf 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
12277deb2264SThomas Graf }
12287deb2264SThomas Graf 
1229e071041bSAlexey Dobriyan static int build_sadinfo(struct sk_buff *skb, struct net *net,
123015e47304SEric W. Biederman 			 u32 portid, u32 seq, u32 flags)
123128d8909bSJamal Hadi Salim {
1232af11e316SJamal Hadi Salim 	struct xfrmk_sadinfo si;
1233af11e316SJamal Hadi Salim 	struct xfrmu_sadhinfo sh;
123428d8909bSJamal Hadi Salim 	struct nlmsghdr *nlh;
12351d1e34ddSDavid S. Miller 	int err;
123628d8909bSJamal Hadi Salim 	u32 *f;
123728d8909bSJamal Hadi Salim 
123815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
123925985edcSLucas De Marchi 	if (nlh == NULL) /* shouldn't really happen ... */
124028d8909bSJamal Hadi Salim 		return -EMSGSIZE;
124128d8909bSJamal Hadi Salim 
124228d8909bSJamal Hadi Salim 	f = nlmsg_data(nlh);
124328d8909bSJamal Hadi Salim 	*f = flags;
1244e071041bSAlexey Dobriyan 	xfrm_sad_getinfo(net, &si);
124528d8909bSJamal Hadi Salim 
1246af11e316SJamal Hadi Salim 	sh.sadhmcnt = si.sadhmcnt;
1247af11e316SJamal Hadi Salim 	sh.sadhcnt = si.sadhcnt;
1248af11e316SJamal Hadi Salim 
12491d1e34ddSDavid S. Miller 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
12501d1e34ddSDavid S. Miller 	if (!err)
12511d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
12521d1e34ddSDavid S. Miller 	if (err) {
12531d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
12541d1e34ddSDavid S. Miller 		return err;
12551d1e34ddSDavid S. Miller 	}
125628d8909bSJamal Hadi Salim 
1257053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1258053c095aSJohannes Berg 	return 0;
125928d8909bSJamal Hadi Salim }
126028d8909bSJamal Hadi Salim 
126128d8909bSJamal Hadi Salim static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
12625424f32eSThomas Graf 		struct nlattr **attrs)
126328d8909bSJamal Hadi Salim {
1264a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
126528d8909bSJamal Hadi Salim 	struct sk_buff *r_skb;
12667b67c857SThomas Graf 	u32 *flags = nlmsg_data(nlh);
126715e47304SEric W. Biederman 	u32 sportid = NETLINK_CB(skb).portid;
126828d8909bSJamal Hadi Salim 	u32 seq = nlh->nlmsg_seq;
12692fc5f83bSGustavo A. R. Silva 	int err;
127028d8909bSJamal Hadi Salim 
12717deb2264SThomas Graf 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
127228d8909bSJamal Hadi Salim 	if (r_skb == NULL)
127328d8909bSJamal Hadi Salim 		return -ENOMEM;
127428d8909bSJamal Hadi Salim 
12752fc5f83bSGustavo A. R. Silva 	err = build_sadinfo(r_skb, net, sportid, seq, *flags);
12762fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
127728d8909bSJamal Hadi Salim 
127815e47304SEric W. Biederman 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
127928d8909bSJamal Hadi Salim }
128028d8909bSJamal Hadi Salim 
128122e70050SChristoph Hellwig static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
12825424f32eSThomas Graf 		struct nlattr **attrs)
12831da177e4SLinus Torvalds {
1284fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
12857b67c857SThomas Graf 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
12861da177e4SLinus Torvalds 	struct xfrm_state *x;
12871da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
1288eb2971b6SMasahide NAKAMURA 	int err = -ESRCH;
12891da177e4SLinus Torvalds 
1290fc34acd3SAlexey Dobriyan 	x = xfrm_user_state_lookup(net, p, attrs, &err);
12911da177e4SLinus Torvalds 	if (x == NULL)
12921da177e4SLinus Torvalds 		goto out_noput;
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
12951da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
12961da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
12971da177e4SLinus Torvalds 	} else {
129815e47304SEric W. Biederman 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
12991da177e4SLinus Torvalds 	}
13001da177e4SLinus Torvalds 	xfrm_state_put(x);
13011da177e4SLinus Torvalds out_noput:
13021da177e4SLinus Torvalds 	return err;
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
130522e70050SChristoph Hellwig static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
13065424f32eSThomas Graf 		struct nlattr **attrs)
13071da177e4SLinus Torvalds {
1308fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
13091da177e4SLinus Torvalds 	struct xfrm_state *x;
13101da177e4SLinus Torvalds 	struct xfrm_userspi_info *p;
13111da177e4SLinus Torvalds 	struct sk_buff *resp_skb;
13121da177e4SLinus Torvalds 	xfrm_address_t *daddr;
13131da177e4SLinus Torvalds 	int family;
13141da177e4SLinus Torvalds 	int err;
13156f26b61eSJamal Hadi Salim 	u32 mark;
13166f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
13177e652640SSteffen Klassert 	u32 if_id = 0;
13181da177e4SLinus Torvalds 
13197b67c857SThomas Graf 	p = nlmsg_data(nlh);
1320776e9dd9SFan Du 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
13211da177e4SLinus Torvalds 	if (err)
13221da177e4SLinus Torvalds 		goto out_noput;
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	family = p->info.family;
13251da177e4SLinus Torvalds 	daddr = &p->info.id.daddr;
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	x = NULL;
13286f26b61eSJamal Hadi Salim 
13296f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
13307e652640SSteffen Klassert 
13317e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
13327e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
13337e652640SSteffen Klassert 
13341da177e4SLinus Torvalds 	if (p->info.seq) {
13356f26b61eSJamal Hadi Salim 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
133670e94e66SYOSHIFUJI Hideaki / 吉藤英明 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
13371da177e4SLinus Torvalds 			xfrm_state_put(x);
13381da177e4SLinus Torvalds 			x = NULL;
13391da177e4SLinus Torvalds 		}
13401da177e4SLinus Torvalds 	}
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	if (!x)
13436f26b61eSJamal Hadi Salim 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
13447e652640SSteffen Klassert 				  if_id, p->info.id.proto, daddr,
13451da177e4SLinus Torvalds 				  &p->info.saddr, 1,
13461da177e4SLinus Torvalds 				  family);
13471da177e4SLinus Torvalds 	err = -ENOENT;
13481da177e4SLinus Torvalds 	if (x == NULL)
13491da177e4SLinus Torvalds 		goto out_noput;
13501da177e4SLinus Torvalds 
1351658b219eSHerbert Xu 	err = xfrm_alloc_spi(x, p->min, p->max);
1352658b219eSHerbert Xu 	if (err)
1353658b219eSHerbert Xu 		goto out;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
13561da177e4SLinus Torvalds 	if (IS_ERR(resp_skb)) {
13571da177e4SLinus Torvalds 		err = PTR_ERR(resp_skb);
13581da177e4SLinus Torvalds 		goto out;
13591da177e4SLinus Torvalds 	}
13601da177e4SLinus Torvalds 
136115e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds out:
13641da177e4SLinus Torvalds 	xfrm_state_put(x);
13651da177e4SLinus Torvalds out_noput:
13661da177e4SLinus Torvalds 	return err;
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
1369b798a9edSJamal Hadi Salim static int verify_policy_dir(u8 dir)
13701da177e4SLinus Torvalds {
13711da177e4SLinus Torvalds 	switch (dir) {
13721da177e4SLinus Torvalds 	case XFRM_POLICY_IN:
13731da177e4SLinus Torvalds 	case XFRM_POLICY_OUT:
13741da177e4SLinus Torvalds 	case XFRM_POLICY_FWD:
13751da177e4SLinus Torvalds 		break;
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	default:
13781da177e4SLinus Torvalds 		return -EINVAL;
13793ff50b79SStephen Hemminger 	}
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	return 0;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
1384b798a9edSJamal Hadi Salim static int verify_policy_type(u8 type)
1385f7b6983fSMasahide NAKAMURA {
1386f7b6983fSMasahide NAKAMURA 	switch (type) {
1387f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_MAIN:
1388f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1389f7b6983fSMasahide NAKAMURA 	case XFRM_POLICY_TYPE_SUB:
1390f7b6983fSMasahide NAKAMURA #endif
1391f7b6983fSMasahide NAKAMURA 		break;
1392f7b6983fSMasahide NAKAMURA 
1393f7b6983fSMasahide NAKAMURA 	default:
1394f7b6983fSMasahide NAKAMURA 		return -EINVAL;
13953ff50b79SStephen Hemminger 	}
1396f7b6983fSMasahide NAKAMURA 
1397f7b6983fSMasahide NAKAMURA 	return 0;
1398f7b6983fSMasahide NAKAMURA }
1399f7b6983fSMasahide NAKAMURA 
14001da177e4SLinus Torvalds static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
14011da177e4SLinus Torvalds {
1402e682adf0SFan Du 	int ret;
1403e682adf0SFan Du 
14041da177e4SLinus Torvalds 	switch (p->share) {
14051da177e4SLinus Torvalds 	case XFRM_SHARE_ANY:
14061da177e4SLinus Torvalds 	case XFRM_SHARE_SESSION:
14071da177e4SLinus Torvalds 	case XFRM_SHARE_USER:
14081da177e4SLinus Torvalds 	case XFRM_SHARE_UNIQUE:
14091da177e4SLinus Torvalds 		break;
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	default:
14121da177e4SLinus Torvalds 		return -EINVAL;
14133ff50b79SStephen Hemminger 	}
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	switch (p->action) {
14161da177e4SLinus Torvalds 	case XFRM_POLICY_ALLOW:
14171da177e4SLinus Torvalds 	case XFRM_POLICY_BLOCK:
14181da177e4SLinus Torvalds 		break;
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 	default:
14211da177e4SLinus Torvalds 		return -EINVAL;
14223ff50b79SStephen Hemminger 	}
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 	switch (p->sel.family) {
14251da177e4SLinus Torvalds 	case AF_INET:
142607bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
142707bf7908SSteffen Klassert 			return -EINVAL;
142807bf7908SSteffen Klassert 
14291da177e4SLinus Torvalds 		break;
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds 	case AF_INET6:
1432dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
143307bf7908SSteffen Klassert 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
143407bf7908SSteffen Klassert 			return -EINVAL;
143507bf7908SSteffen Klassert 
14361da177e4SLinus Torvalds 		break;
14371da177e4SLinus Torvalds #else
14381da177e4SLinus Torvalds 		return  -EAFNOSUPPORT;
14391da177e4SLinus Torvalds #endif
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	default:
14421da177e4SLinus Torvalds 		return -EINVAL;
14433ff50b79SStephen Hemminger 	}
14441da177e4SLinus Torvalds 
1445e682adf0SFan Du 	ret = verify_policy_dir(p->dir);
1446e682adf0SFan Du 	if (ret)
1447e682adf0SFan Du 		return ret;
1448b805d78dSYueHaibing 	if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1449e682adf0SFan Du 		return -EINVAL;
1450e682adf0SFan Du 
1451e682adf0SFan Du 	return 0;
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds 
14545424f32eSThomas Graf static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1455df71837dSTrent Jaeger {
14565424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1457df71837dSTrent Jaeger 	struct xfrm_user_sec_ctx *uctx;
1458df71837dSTrent Jaeger 
1459df71837dSTrent Jaeger 	if (!rt)
1460df71837dSTrent Jaeger 		return 0;
1461df71837dSTrent Jaeger 
14625424f32eSThomas Graf 	uctx = nla_data(rt);
146352a4c640SNikolay Aleksandrov 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1464df71837dSTrent Jaeger }
1465df71837dSTrent Jaeger 
14661da177e4SLinus Torvalds static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
14671da177e4SLinus Torvalds 			   int nr)
14681da177e4SLinus Torvalds {
14691da177e4SLinus Torvalds 	int i;
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	xp->xfrm_nr = nr;
14721da177e4SLinus Torvalds 	for (i = 0; i < nr; i++, ut++) {
14731da177e4SLinus Torvalds 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
14761da177e4SLinus Torvalds 		memcpy(&t->saddr, &ut->saddr,
14771da177e4SLinus Torvalds 		       sizeof(xfrm_address_t));
14781da177e4SLinus Torvalds 		t->reqid = ut->reqid;
14791da177e4SLinus Torvalds 		t->mode = ut->mode;
14801da177e4SLinus Torvalds 		t->share = ut->share;
14811da177e4SLinus Torvalds 		t->optional = ut->optional;
14821da177e4SLinus Torvalds 		t->aalgos = ut->aalgos;
14831da177e4SLinus Torvalds 		t->ealgos = ut->ealgos;
14841da177e4SLinus Torvalds 		t->calgos = ut->calgos;
1485c5d18e98SHerbert Xu 		/* If all masks are ~0, then we allow all algorithms. */
1486c5d18e98SHerbert Xu 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
14878511d01dSMiika Komu 		t->encap_family = ut->family;
14881da177e4SLinus Torvalds 	}
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds 
1491b4ad86bfSDavid S. Miller static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1492b4ad86bfSDavid S. Miller {
1493732706afSSteffen Klassert 	u16 prev_family;
1494b4ad86bfSDavid S. Miller 	int i;
1495b4ad86bfSDavid S. Miller 
1496b4ad86bfSDavid S. Miller 	if (nr > XFRM_MAX_DEPTH)
1497b4ad86bfSDavid S. Miller 		return -EINVAL;
1498b4ad86bfSDavid S. Miller 
1499732706afSSteffen Klassert 	prev_family = family;
1500732706afSSteffen Klassert 
1501b4ad86bfSDavid S. Miller 	for (i = 0; i < nr; i++) {
1502b4ad86bfSDavid S. Miller 		/* We never validated the ut->family value, so many
1503b4ad86bfSDavid S. Miller 		 * applications simply leave it at zero.  The check was
1504b4ad86bfSDavid S. Miller 		 * never made and ut->family was ignored because all
1505b4ad86bfSDavid S. Miller 		 * templates could be assumed to have the same family as
1506b4ad86bfSDavid S. Miller 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1507b4ad86bfSDavid S. Miller 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1508b4ad86bfSDavid S. Miller 		 */
1509b4ad86bfSDavid S. Miller 		if (!ut[i].family)
1510b4ad86bfSDavid S. Miller 			ut[i].family = family;
1511b4ad86bfSDavid S. Miller 
151235e61038SFlorian Westphal 		switch (ut[i].mode) {
151335e61038SFlorian Westphal 		case XFRM_MODE_TUNNEL:
151435e61038SFlorian Westphal 		case XFRM_MODE_BEET:
151535e61038SFlorian Westphal 			break;
151635e61038SFlorian Westphal 		default:
151735e61038SFlorian Westphal 			if (ut[i].family != prev_family)
1518732706afSSteffen Klassert 				return -EINVAL;
151935e61038SFlorian Westphal 			break;
152035e61038SFlorian Westphal 		}
152132bf94fbSSean Tranchetti 		if (ut[i].mode >= XFRM_MODE_MAX)
152232bf94fbSSean Tranchetti 			return -EINVAL;
152332bf94fbSSean Tranchetti 
1524732706afSSteffen Klassert 		prev_family = ut[i].family;
1525732706afSSteffen Klassert 
1526b4ad86bfSDavid S. Miller 		switch (ut[i].family) {
1527b4ad86bfSDavid S. Miller 		case AF_INET:
1528b4ad86bfSDavid S. Miller 			break;
1529dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1530b4ad86bfSDavid S. Miller 		case AF_INET6:
1531b4ad86bfSDavid S. Miller 			break;
1532b4ad86bfSDavid S. Miller #endif
1533b4ad86bfSDavid S. Miller 		default:
1534b4ad86bfSDavid S. Miller 			return -EINVAL;
15353ff50b79SStephen Hemminger 		}
15366a53b759SCong Wang 
1537dbb2483bSCong Wang 		if (!xfrm_id_proto_valid(ut[i].id.proto))
15386a53b759SCong Wang 			return -EINVAL;
15396a53b759SCong Wang 	}
15406a53b759SCong Wang 
1541b4ad86bfSDavid S. Miller 	return 0;
1542b4ad86bfSDavid S. Miller }
1543b4ad86bfSDavid S. Miller 
15445424f32eSThomas Graf static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
15451da177e4SLinus Torvalds {
15465424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	if (!rt) {
15491da177e4SLinus Torvalds 		pol->xfrm_nr = 0;
15501da177e4SLinus Torvalds 	} else {
15515424f32eSThomas Graf 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
15525424f32eSThomas Graf 		int nr = nla_len(rt) / sizeof(*utmpl);
1553b4ad86bfSDavid S. Miller 		int err;
15541da177e4SLinus Torvalds 
1555b4ad86bfSDavid S. Miller 		err = validate_tmpl(nr, utmpl, pol->family);
1556b4ad86bfSDavid S. Miller 		if (err)
1557b4ad86bfSDavid S. Miller 			return err;
15581da177e4SLinus Torvalds 
15595424f32eSThomas Graf 		copy_templates(pol, utmpl, nr);
15601da177e4SLinus Torvalds 	}
15611da177e4SLinus Torvalds 	return 0;
15621da177e4SLinus Torvalds }
15631da177e4SLinus Torvalds 
15645424f32eSThomas Graf static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1565f7b6983fSMasahide NAKAMURA {
15665424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1567f7b6983fSMasahide NAKAMURA 	struct xfrm_userpolicy_type *upt;
1568b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
1569f7b6983fSMasahide NAKAMURA 	int err;
1570f7b6983fSMasahide NAKAMURA 
1571f7b6983fSMasahide NAKAMURA 	if (rt) {
15725424f32eSThomas Graf 		upt = nla_data(rt);
1573f7b6983fSMasahide NAKAMURA 		type = upt->type;
1574f7b6983fSMasahide NAKAMURA 	}
1575f7b6983fSMasahide NAKAMURA 
1576f7b6983fSMasahide NAKAMURA 	err = verify_policy_type(type);
1577f7b6983fSMasahide NAKAMURA 	if (err)
1578f7b6983fSMasahide NAKAMURA 		return err;
1579f7b6983fSMasahide NAKAMURA 
1580f7b6983fSMasahide NAKAMURA 	*tp = type;
1581f7b6983fSMasahide NAKAMURA 	return 0;
1582f7b6983fSMasahide NAKAMURA }
1583f7b6983fSMasahide NAKAMURA 
15841da177e4SLinus Torvalds static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
15851da177e4SLinus Torvalds {
15861da177e4SLinus Torvalds 	xp->priority = p->priority;
15871da177e4SLinus Torvalds 	xp->index = p->index;
15881da177e4SLinus Torvalds 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
15891da177e4SLinus Torvalds 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
15901da177e4SLinus Torvalds 	xp->action = p->action;
15911da177e4SLinus Torvalds 	xp->flags = p->flags;
15921da177e4SLinus Torvalds 	xp->family = p->sel.family;
15931da177e4SLinus Torvalds 	/* XXX xp->share = p->share; */
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
15971da177e4SLinus Torvalds {
15987b789836SMathias Krause 	memset(p, 0, sizeof(*p));
15991da177e4SLinus Torvalds 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
16001da177e4SLinus Torvalds 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
16011da177e4SLinus Torvalds 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
16021da177e4SLinus Torvalds 	p->priority = xp->priority;
16031da177e4SLinus Torvalds 	p->index = xp->index;
16041da177e4SLinus Torvalds 	p->sel.family = xp->family;
16051da177e4SLinus Torvalds 	p->dir = dir;
16061da177e4SLinus Torvalds 	p->action = xp->action;
16071da177e4SLinus Torvalds 	p->flags = xp->flags;
16081da177e4SLinus Torvalds 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
16091da177e4SLinus Torvalds }
16101da177e4SLinus Torvalds 
1611fc34acd3SAlexey Dobriyan static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
16121da177e4SLinus Torvalds {
1613fc34acd3SAlexey Dobriyan 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
16141da177e4SLinus Torvalds 	int err;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	if (!xp) {
16171da177e4SLinus Torvalds 		*errp = -ENOMEM;
16181da177e4SLinus Torvalds 		return NULL;
16191da177e4SLinus Torvalds 	}
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
1622df71837dSTrent Jaeger 
162335a7aa08SThomas Graf 	err = copy_from_user_policy_type(&xp->type, attrs);
1624f7b6983fSMasahide NAKAMURA 	if (err)
1625f7b6983fSMasahide NAKAMURA 		goto error;
1626f7b6983fSMasahide NAKAMURA 
162735a7aa08SThomas Graf 	if (!(err = copy_from_user_tmpl(xp, attrs)))
162835a7aa08SThomas Graf 		err = copy_from_user_sec_ctx(xp, attrs);
1629f7b6983fSMasahide NAKAMURA 	if (err)
1630f7b6983fSMasahide NAKAMURA 		goto error;
16311da177e4SLinus Torvalds 
1632295fae56SJamal Hadi Salim 	xfrm_mark_get(attrs, &xp->mark);
1633295fae56SJamal Hadi Salim 
16347e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
16357e652640SSteffen Klassert 		xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
16367e652640SSteffen Klassert 
16371da177e4SLinus Torvalds 	return xp;
1638f7b6983fSMasahide NAKAMURA  error:
1639f7b6983fSMasahide NAKAMURA 	*errp = err;
164012a169e7SHerbert Xu 	xp->walk.dead = 1;
164164c31b3fSWANG Cong 	xfrm_policy_destroy(xp);
1642f7b6983fSMasahide NAKAMURA 	return NULL;
16431da177e4SLinus Torvalds }
16441da177e4SLinus Torvalds 
164522e70050SChristoph Hellwig static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
16465424f32eSThomas Graf 		struct nlattr **attrs)
16471da177e4SLinus Torvalds {
1648fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
16497b67c857SThomas Graf 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
16501da177e4SLinus Torvalds 	struct xfrm_policy *xp;
165126b15dadSJamal Hadi Salim 	struct km_event c;
16521da177e4SLinus Torvalds 	int err;
16531da177e4SLinus Torvalds 	int excl;
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 	err = verify_newpolicy_info(p);
16561da177e4SLinus Torvalds 	if (err)
16571da177e4SLinus Torvalds 		return err;
165835a7aa08SThomas Graf 	err = verify_sec_ctx_len(attrs);
1659df71837dSTrent Jaeger 	if (err)
1660df71837dSTrent Jaeger 		return err;
16611da177e4SLinus Torvalds 
1662fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, p, attrs, &err);
16631da177e4SLinus Torvalds 	if (!xp)
16641da177e4SLinus Torvalds 		return err;
16651da177e4SLinus Torvalds 
166625985edcSLucas De Marchi 	/* shouldn't excl be based on nlh flags??
166726b15dadSJamal Hadi Salim 	 * Aha! this is anti-netlink really i.e  more pfkey derived
166826b15dadSJamal Hadi Salim 	 * in netlink excl is a flag and you wouldnt need
166926b15dadSJamal Hadi Salim 	 * a type XFRM_MSG_UPDPOLICY - JHS */
16701da177e4SLinus Torvalds 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
16711da177e4SLinus Torvalds 	err = xfrm_policy_insert(p->dir, xp, excl);
16722e71029eSTetsuo Handa 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1673161a09e7SJoy Latten 
16741da177e4SLinus Torvalds 	if (err) {
167503e1ad7bSPaul Moore 		security_xfrm_policy_free(xp->security);
16761da177e4SLinus Torvalds 		kfree(xp);
16771da177e4SLinus Torvalds 		return err;
16781da177e4SLinus Torvalds 	}
16791da177e4SLinus Torvalds 
1680f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
168126b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
168215e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
168326b15dadSJamal Hadi Salim 	km_policy_notify(xp, p->dir, &c);
168426b15dadSJamal Hadi Salim 
16851da177e4SLinus Torvalds 	xfrm_pol_put(xp);
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 	return 0;
16881da177e4SLinus Torvalds }
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
16911da177e4SLinus Torvalds {
16921da177e4SLinus Torvalds 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
16931da177e4SLinus Torvalds 	int i;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	if (xp->xfrm_nr == 0)
16961da177e4SLinus Torvalds 		return 0;
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds 	for (i = 0; i < xp->xfrm_nr; i++) {
16991da177e4SLinus Torvalds 		struct xfrm_user_tmpl *up = &vec[i];
17001da177e4SLinus Torvalds 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
17011da177e4SLinus Torvalds 
17021f86840fSMathias Krause 		memset(up, 0, sizeof(*up));
17031da177e4SLinus Torvalds 		memcpy(&up->id, &kp->id, sizeof(up->id));
17048511d01dSMiika Komu 		up->family = kp->encap_family;
17051da177e4SLinus Torvalds 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
17061da177e4SLinus Torvalds 		up->reqid = kp->reqid;
17071da177e4SLinus Torvalds 		up->mode = kp->mode;
17081da177e4SLinus Torvalds 		up->share = kp->share;
17091da177e4SLinus Torvalds 		up->optional = kp->optional;
17101da177e4SLinus Torvalds 		up->aalgos = kp->aalgos;
17111da177e4SLinus Torvalds 		up->ealgos = kp->ealgos;
17121da177e4SLinus Torvalds 		up->calgos = kp->calgos;
17131da177e4SLinus Torvalds 	}
17141da177e4SLinus Torvalds 
1715c0144beaSThomas Graf 	return nla_put(skb, XFRMA_TMPL,
1716c0144beaSThomas Graf 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1717df71837dSTrent Jaeger }
1718df71837dSTrent Jaeger 
17190d681623SSerge Hallyn static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
17200d681623SSerge Hallyn {
17210d681623SSerge Hallyn 	if (x->security) {
17220d681623SSerge Hallyn 		return copy_sec_ctx(x->security, skb);
17230d681623SSerge Hallyn 	}
17240d681623SSerge Hallyn 	return 0;
17250d681623SSerge Hallyn }
17260d681623SSerge Hallyn 
17270d681623SSerge Hallyn static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
17280d681623SSerge Hallyn {
17291d1e34ddSDavid S. Miller 	if (xp->security)
17300d681623SSerge Hallyn 		return copy_sec_ctx(xp->security, skb);
17310d681623SSerge Hallyn 	return 0;
17320d681623SSerge Hallyn }
1733a1b831f2SAlexey Dobriyan static inline unsigned int userpolicy_type_attrsize(void)
1734cfbfd45aSThomas Graf {
1735cfbfd45aSThomas Graf #ifdef CONFIG_XFRM_SUB_POLICY
1736cfbfd45aSThomas Graf 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1737cfbfd45aSThomas Graf #else
1738cfbfd45aSThomas Graf 	return 0;
1739cfbfd45aSThomas Graf #endif
1740cfbfd45aSThomas Graf }
17410d681623SSerge Hallyn 
1742f7b6983fSMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1743b798a9edSJamal Hadi Salim static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1744f7b6983fSMasahide NAKAMURA {
174545c180bcSEric Dumazet 	struct xfrm_userpolicy_type upt;
174645c180bcSEric Dumazet 
174745c180bcSEric Dumazet 	/* Sadly there are two holes in struct xfrm_userpolicy_type */
174845c180bcSEric Dumazet 	memset(&upt, 0, sizeof(upt));
174945c180bcSEric Dumazet 	upt.type = type;
1750f7b6983fSMasahide NAKAMURA 
1751c0144beaSThomas Graf 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1752f7b6983fSMasahide NAKAMURA }
1753f7b6983fSMasahide NAKAMURA 
1754f7b6983fSMasahide NAKAMURA #else
1755b798a9edSJamal Hadi Salim static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1756f7b6983fSMasahide NAKAMURA {
1757f7b6983fSMasahide NAKAMURA 	return 0;
1758f7b6983fSMasahide NAKAMURA }
1759f7b6983fSMasahide NAKAMURA #endif
1760f7b6983fSMasahide NAKAMURA 
17611da177e4SLinus Torvalds static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
17621da177e4SLinus Torvalds {
17631da177e4SLinus Torvalds 	struct xfrm_dump_info *sp = ptr;
17641da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p;
17651da177e4SLinus Torvalds 	struct sk_buff *in_skb = sp->in_skb;
17661da177e4SLinus Torvalds 	struct sk_buff *skb = sp->out_skb;
17671da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
17681d1e34ddSDavid S. Miller 	int err;
17691da177e4SLinus Torvalds 
177015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
177179b8b7f4SThomas Graf 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
177279b8b7f4SThomas Graf 	if (nlh == NULL)
177379b8b7f4SThomas Graf 		return -EMSGSIZE;
17741da177e4SLinus Torvalds 
17757b67c857SThomas Graf 	p = nlmsg_data(nlh);
17761da177e4SLinus Torvalds 	copy_to_user_policy(xp, p, dir);
17771d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
17781d1e34ddSDavid S. Miller 	if (!err)
17791d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
17801d1e34ddSDavid S. Miller 	if (!err)
17811d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
17821d1e34ddSDavid S. Miller 	if (!err)
17831d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
17847e652640SSteffen Klassert 	if (!err)
17857e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
17861d1e34ddSDavid S. Miller 	if (err) {
17871d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
17881d1e34ddSDavid S. Miller 		return err;
17891d1e34ddSDavid S. Miller 	}
17909825069dSThomas Graf 	nlmsg_end(skb, nlh);
17911da177e4SLinus Torvalds 	return 0;
17921da177e4SLinus Torvalds }
17931da177e4SLinus Torvalds 
17944c563f76STimo Teras static int xfrm_dump_policy_done(struct netlink_callback *cb)
17954c563f76STimo Teras {
17961137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1797283bc9f3SFan Du 	struct net *net = sock_net(cb->skb->sk);
17984c563f76STimo Teras 
1799283bc9f3SFan Du 	xfrm_policy_walk_done(walk, net);
18004c563f76STimo Teras 	return 0;
18014c563f76STimo Teras }
18024c563f76STimo Teras 
18031137b5e2SHerbert Xu static int xfrm_dump_policy_start(struct netlink_callback *cb)
18041137b5e2SHerbert Xu {
18051137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
18061137b5e2SHerbert Xu 
18071137b5e2SHerbert Xu 	BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
18081137b5e2SHerbert Xu 
18091137b5e2SHerbert Xu 	xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
18101137b5e2SHerbert Xu 	return 0;
18111137b5e2SHerbert Xu }
18121137b5e2SHerbert Xu 
18131da177e4SLinus Torvalds static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
18141da177e4SLinus Torvalds {
1815fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
18161137b5e2SHerbert Xu 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
18171da177e4SLinus Torvalds 	struct xfrm_dump_info info;
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	info.in_skb = cb->skb;
18201da177e4SLinus Torvalds 	info.out_skb = skb;
18211da177e4SLinus Torvalds 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
18221da177e4SLinus Torvalds 	info.nlmsg_flags = NLM_F_MULTI;
18234c563f76STimo Teras 
1824fc34acd3SAlexey Dobriyan 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 	return skb->len;
18271da177e4SLinus Torvalds }
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
18301da177e4SLinus Torvalds 					  struct xfrm_policy *xp,
18311da177e4SLinus Torvalds 					  int dir, u32 seq)
18321da177e4SLinus Torvalds {
18331da177e4SLinus Torvalds 	struct xfrm_dump_info info;
18341da177e4SLinus Torvalds 	struct sk_buff *skb;
1835c2546372SMathias Krause 	int err;
18361da177e4SLinus Torvalds 
18377deb2264SThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18381da177e4SLinus Torvalds 	if (!skb)
18391da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	info.in_skb = in_skb;
18421da177e4SLinus Torvalds 	info.out_skb = skb;
18431da177e4SLinus Torvalds 	info.nlmsg_seq = seq;
18441da177e4SLinus Torvalds 	info.nlmsg_flags = 0;
18451da177e4SLinus Torvalds 
1846c2546372SMathias Krause 	err = dump_one_policy(xp, dir, 0, &info);
1847c2546372SMathias Krause 	if (err) {
18481da177e4SLinus Torvalds 		kfree_skb(skb);
1849c2546372SMathias Krause 		return ERR_PTR(err);
18501da177e4SLinus Torvalds 	}
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 	return skb;
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
185522e70050SChristoph Hellwig static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
18565424f32eSThomas Graf 		struct nlattr **attrs)
18571da177e4SLinus Torvalds {
1858fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
18591da177e4SLinus Torvalds 	struct xfrm_policy *xp;
18601da177e4SLinus Torvalds 	struct xfrm_userpolicy_id *p;
1861b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
18621da177e4SLinus Torvalds 	int err;
186326b15dadSJamal Hadi Salim 	struct km_event c;
18641da177e4SLinus Torvalds 	int delete;
1865295fae56SJamal Hadi Salim 	struct xfrm_mark m;
1866295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
18677e652640SSteffen Klassert 	u32 if_id = 0;
18681da177e4SLinus Torvalds 
18697b67c857SThomas Graf 	p = nlmsg_data(nlh);
18701da177e4SLinus Torvalds 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
18711da177e4SLinus Torvalds 
187235a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
1873f7b6983fSMasahide NAKAMURA 	if (err)
1874f7b6983fSMasahide NAKAMURA 		return err;
1875f7b6983fSMasahide NAKAMURA 
18761da177e4SLinus Torvalds 	err = verify_policy_dir(p->dir);
18771da177e4SLinus Torvalds 	if (err)
18781da177e4SLinus Torvalds 		return err;
18791da177e4SLinus Torvalds 
18807e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
18817e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
18827e652640SSteffen Klassert 
18831da177e4SLinus Torvalds 	if (p->index)
18847e652640SSteffen Klassert 		xp = xfrm_policy_byid(net, mark, if_id, type, p->dir, p->index, delete, &err);
1885df71837dSTrent Jaeger 	else {
18865424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
188703e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
1888df71837dSTrent Jaeger 
188935a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
1890df71837dSTrent Jaeger 		if (err)
1891df71837dSTrent Jaeger 			return err;
1892df71837dSTrent Jaeger 
18932c8dd116SDenis V. Lunev 		ctx = NULL;
1894df71837dSTrent Jaeger 		if (rt) {
18955424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1896df71837dSTrent Jaeger 
189752a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
189803e1ad7bSPaul Moore 			if (err)
1899df71837dSTrent Jaeger 				return err;
19002c8dd116SDenis V. Lunev 		}
19017e652640SSteffen Klassert 		xp = xfrm_policy_bysel_ctx(net, mark, if_id, type, p->dir, &p->sel,
19026f26b61eSJamal Hadi Salim 					   ctx, delete, &err);
190303e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
1904df71837dSTrent Jaeger 	}
19051da177e4SLinus Torvalds 	if (xp == NULL)
19061da177e4SLinus Torvalds 		return -ENOENT;
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds 	if (!delete) {
19091da177e4SLinus Torvalds 		struct sk_buff *resp_skb;
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
19121da177e4SLinus Torvalds 		if (IS_ERR(resp_skb)) {
19131da177e4SLinus Torvalds 			err = PTR_ERR(resp_skb);
19141da177e4SLinus Torvalds 		} else {
1915a6483b79SAlexey Dobriyan 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
191615e47304SEric W. Biederman 					    NETLINK_CB(skb).portid);
19171da177e4SLinus Torvalds 		}
191826b15dadSJamal Hadi Salim 	} else {
19192e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
192013fcfbb0SDavid S. Miller 
192113fcfbb0SDavid S. Miller 		if (err != 0)
1922c8c05a8eSCatherine Zhang 			goto out;
192313fcfbb0SDavid S. Miller 
1924e7443892SHerbert Xu 		c.data.byid = p->index;
1925f60f6b8fSHerbert Xu 		c.event = nlh->nlmsg_type;
192626b15dadSJamal Hadi Salim 		c.seq = nlh->nlmsg_seq;
192715e47304SEric W. Biederman 		c.portid = nlh->nlmsg_pid;
192826b15dadSJamal Hadi Salim 		km_policy_notify(xp, p->dir, &c);
19291da177e4SLinus Torvalds 	}
19301da177e4SLinus Torvalds 
1931c8c05a8eSCatherine Zhang out:
1932ef41aaa0SEric Paris 	xfrm_pol_put(xp);
19331da177e4SLinus Torvalds 	return err;
19341da177e4SLinus Torvalds }
19351da177e4SLinus Torvalds 
193622e70050SChristoph Hellwig static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
19375424f32eSThomas Graf 		struct nlattr **attrs)
19381da177e4SLinus Torvalds {
1939fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
194026b15dadSJamal Hadi Salim 	struct km_event c;
19417b67c857SThomas Graf 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
19424aa2e62cSJoy Latten 	int err;
19431da177e4SLinus Torvalds 
1944f75a2804SCong Wang 	err = xfrm_state_flush(net, p->proto, true, false);
19459e64cc95SJamal Hadi Salim 	if (err) {
19469e64cc95SJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
19479e64cc95SJamal Hadi Salim 			return 0;
1948069c474eSDavid S. Miller 		return err;
19499e64cc95SJamal Hadi Salim 	}
1950bf08867fSHerbert Xu 	c.data.proto = p->proto;
1951f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
195226b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
195315e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
19547067802eSAlexey Dobriyan 	c.net = net;
195526b15dadSJamal Hadi Salim 	km_state_notify(NULL, &c);
195626b15dadSJamal Hadi Salim 
19571da177e4SLinus Torvalds 	return 0;
19581da177e4SLinus Torvalds }
19591da177e4SLinus Torvalds 
1960a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
19617deb2264SThomas Graf {
1962a1b831f2SAlexey Dobriyan 	unsigned int replay_size = x->replay_esn ?
1963d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn) :
1964d8647b79SSteffen Klassert 			      sizeof(struct xfrm_replay_state);
1965d8647b79SSteffen Klassert 
19667deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1967d8647b79SSteffen Klassert 	       + nla_total_size(replay_size)
1968de95c4a4SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
19696f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
19707deb2264SThomas Graf 	       + nla_total_size(4) /* XFRM_AE_RTHR */
19717deb2264SThomas Graf 	       + nla_total_size(4); /* XFRM_AE_ETHR */
19727deb2264SThomas Graf }
1973d51d081dSJamal Hadi Salim 
1974214e005bSDavid S. Miller static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1975d51d081dSJamal Hadi Salim {
1976d51d081dSJamal Hadi Salim 	struct xfrm_aevent_id *id;
1977d51d081dSJamal Hadi Salim 	struct nlmsghdr *nlh;
19781d1e34ddSDavid S. Miller 	int err;
1979d51d081dSJamal Hadi Salim 
198015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
198179b8b7f4SThomas Graf 	if (nlh == NULL)
198279b8b7f4SThomas Graf 		return -EMSGSIZE;
1983d51d081dSJamal Hadi Salim 
19847b67c857SThomas Graf 	id = nlmsg_data(nlh);
1985931e79d7SMathias Krause 	memset(&id->sa_id, 0, sizeof(id->sa_id));
19862b5f6dccSJamal Hadi Salim 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1987d51d081dSJamal Hadi Salim 	id->sa_id.spi = x->id.spi;
1988d51d081dSJamal Hadi Salim 	id->sa_id.family = x->props.family;
1989d51d081dSJamal Hadi Salim 	id->sa_id.proto = x->id.proto;
19902b5f6dccSJamal Hadi Salim 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
19912b5f6dccSJamal Hadi Salim 	id->reqid = x->props.reqid;
1992d51d081dSJamal Hadi Salim 	id->flags = c->data.aevent;
1993d51d081dSJamal Hadi Salim 
1994d0fde795SDavid S. Miller 	if (x->replay_esn) {
19951d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1996d8647b79SSteffen Klassert 			      xfrm_replay_state_esn_len(x->replay_esn),
19971d1e34ddSDavid S. Miller 			      x->replay_esn);
1998d0fde795SDavid S. Miller 	} else {
19991d1e34ddSDavid S. Miller 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
20001d1e34ddSDavid S. Miller 			      &x->replay);
2001d0fde795SDavid S. Miller 	}
20021d1e34ddSDavid S. Miller 	if (err)
20031d1e34ddSDavid S. Miller 		goto out_cancel;
2004de95c4a4SNicolas Dichtel 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2005de95c4a4SNicolas Dichtel 			    XFRMA_PAD);
20061d1e34ddSDavid S. Miller 	if (err)
20071d1e34ddSDavid S. Miller 		goto out_cancel;
2008d8647b79SSteffen Klassert 
20091d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_RTHR) {
20101d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
20111d1e34ddSDavid S. Miller 		if (err)
20121d1e34ddSDavid S. Miller 			goto out_cancel;
20131d1e34ddSDavid S. Miller 	}
20141d1e34ddSDavid S. Miller 	if (id->flags & XFRM_AE_ETHR) {
20151d1e34ddSDavid S. Miller 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
20161d1e34ddSDavid S. Miller 				  x->replay_maxage * 10 / HZ);
20171d1e34ddSDavid S. Miller 		if (err)
20181d1e34ddSDavid S. Miller 			goto out_cancel;
20191d1e34ddSDavid S. Miller 	}
20201d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
20211d1e34ddSDavid S. Miller 	if (err)
20221d1e34ddSDavid S. Miller 		goto out_cancel;
20236f26b61eSJamal Hadi Salim 
20247e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
20257e652640SSteffen Klassert 	if (err)
20267e652640SSteffen Klassert 		goto out_cancel;
20277e652640SSteffen Klassert 
2028053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2029053c095aSJohannes Berg 	return 0;
2030d51d081dSJamal Hadi Salim 
20311d1e34ddSDavid S. Miller out_cancel:
20329825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
20331d1e34ddSDavid S. Miller 	return err;
2034d51d081dSJamal Hadi Salim }
2035d51d081dSJamal Hadi Salim 
203622e70050SChristoph Hellwig static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
20375424f32eSThomas Graf 		struct nlattr **attrs)
2038d51d081dSJamal Hadi Salim {
2039fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2040d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2041d51d081dSJamal Hadi Salim 	struct sk_buff *r_skb;
2042d51d081dSJamal Hadi Salim 	int err;
2043d51d081dSJamal Hadi Salim 	struct km_event c;
20446f26b61eSJamal Hadi Salim 	u32 mark;
20456f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
20467b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
2047d51d081dSJamal Hadi Salim 	struct xfrm_usersa_id *id = &p->sa_id;
2048d51d081dSJamal Hadi Salim 
20496f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
20506f26b61eSJamal Hadi Salim 
20516f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2052d8647b79SSteffen Klassert 	if (x == NULL)
2053d51d081dSJamal Hadi Salim 		return -ESRCH;
2054d8647b79SSteffen Klassert 
2055d8647b79SSteffen Klassert 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2056d8647b79SSteffen Klassert 	if (r_skb == NULL) {
2057d8647b79SSteffen Klassert 		xfrm_state_put(x);
2058d8647b79SSteffen Klassert 		return -ENOMEM;
2059d51d081dSJamal Hadi Salim 	}
2060d51d081dSJamal Hadi Salim 
2061d51d081dSJamal Hadi Salim 	/*
2062d51d081dSJamal Hadi Salim 	 * XXX: is this lock really needed - none of the other
2063d51d081dSJamal Hadi Salim 	 * gets lock (the concern is things getting updated
2064d51d081dSJamal Hadi Salim 	 * while we are still reading) - jhs
2065d51d081dSJamal Hadi Salim 	*/
2066d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2067d51d081dSJamal Hadi Salim 	c.data.aevent = p->flags;
2068d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
206915e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2070d51d081dSJamal Hadi Salim 
20712fc5f83bSGustavo A. R. Silva 	err = build_aevent(r_skb, x, &c);
20722fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
20732fc5f83bSGustavo A. R. Silva 
207415e47304SEric W. Biederman 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2075d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2076d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2077d51d081dSJamal Hadi Salim 	return err;
2078d51d081dSJamal Hadi Salim }
2079d51d081dSJamal Hadi Salim 
208022e70050SChristoph Hellwig static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
20815424f32eSThomas Graf 		struct nlattr **attrs)
2082d51d081dSJamal Hadi Salim {
2083fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2084d51d081dSJamal Hadi Salim 	struct xfrm_state *x;
2085d51d081dSJamal Hadi Salim 	struct km_event c;
2086d51d081dSJamal Hadi Salim 	int err = -EINVAL;
20876f26b61eSJamal Hadi Salim 	u32 mark = 0;
20886f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
20897b67c857SThomas Graf 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
20905424f32eSThomas Graf 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2091d8647b79SSteffen Klassert 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
20925424f32eSThomas Graf 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
20934e077237SMichael Rossberg 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
20944e077237SMichael Rossberg 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2095d51d081dSJamal Hadi Salim 
20964e077237SMichael Rossberg 	if (!lt && !rp && !re && !et && !rt)
2097d51d081dSJamal Hadi Salim 		return err;
2098d51d081dSJamal Hadi Salim 
2099d51d081dSJamal Hadi Salim 	/* pedantic mode - thou shalt sayeth replaceth */
2100d51d081dSJamal Hadi Salim 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2101d51d081dSJamal Hadi Salim 		return err;
2102d51d081dSJamal Hadi Salim 
21036f26b61eSJamal Hadi Salim 	mark = xfrm_mark_get(attrs, &m);
21046f26b61eSJamal Hadi Salim 
21056f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2106d51d081dSJamal Hadi Salim 	if (x == NULL)
2107d51d081dSJamal Hadi Salim 		return -ESRCH;
2108d51d081dSJamal Hadi Salim 
2109d51d081dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
2110d51d081dSJamal Hadi Salim 		goto out;
2111d51d081dSJamal Hadi Salim 
21124479ff76SSteffen Klassert 	err = xfrm_replay_verify_len(x->replay_esn, re);
2113e2b19125SSteffen Klassert 	if (err)
2114e2b19125SSteffen Klassert 		goto out;
2115e2b19125SSteffen Klassert 
2116d51d081dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
2117e3ac104dSMathias Krause 	xfrm_update_ae_params(x, attrs, 1);
2118d51d081dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
2119d51d081dSJamal Hadi Salim 
2120d51d081dSJamal Hadi Salim 	c.event = nlh->nlmsg_type;
2121d51d081dSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
212215e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
2123d51d081dSJamal Hadi Salim 	c.data.aevent = XFRM_AE_CU;
2124d51d081dSJamal Hadi Salim 	km_state_notify(x, &c);
2125d51d081dSJamal Hadi Salim 	err = 0;
2126d51d081dSJamal Hadi Salim out:
2127d51d081dSJamal Hadi Salim 	xfrm_state_put(x);
2128d51d081dSJamal Hadi Salim 	return err;
2129d51d081dSJamal Hadi Salim }
2130d51d081dSJamal Hadi Salim 
213122e70050SChristoph Hellwig static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
21325424f32eSThomas Graf 		struct nlattr **attrs)
21331da177e4SLinus Torvalds {
2134fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
213526b15dadSJamal Hadi Salim 	struct km_event c;
2136b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
2137f7b6983fSMasahide NAKAMURA 	int err;
213826b15dadSJamal Hadi Salim 
213935a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2140f7b6983fSMasahide NAKAMURA 	if (err)
2141f7b6983fSMasahide NAKAMURA 		return err;
2142f7b6983fSMasahide NAKAMURA 
21432e71029eSTetsuo Handa 	err = xfrm_policy_flush(net, type, true);
21442f1eb65fSJamal Hadi Salim 	if (err) {
21452f1eb65fSJamal Hadi Salim 		if (err == -ESRCH) /* empty table */
21462f1eb65fSJamal Hadi Salim 			return 0;
2147069c474eSDavid S. Miller 		return err;
21482f1eb65fSJamal Hadi Salim 	}
21492f1eb65fSJamal Hadi Salim 
2150f7b6983fSMasahide NAKAMURA 	c.data.type = type;
2151f60f6b8fSHerbert Xu 	c.event = nlh->nlmsg_type;
215226b15dadSJamal Hadi Salim 	c.seq = nlh->nlmsg_seq;
215315e47304SEric W. Biederman 	c.portid = nlh->nlmsg_pid;
21547067802eSAlexey Dobriyan 	c.net = net;
215526b15dadSJamal Hadi Salim 	km_policy_notify(NULL, 0, &c);
21561da177e4SLinus Torvalds 	return 0;
21571da177e4SLinus Torvalds }
21581da177e4SLinus Torvalds 
215922e70050SChristoph Hellwig static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
21605424f32eSThomas Graf 		struct nlattr **attrs)
21616c5c8ca7SJamal Hadi Salim {
2162fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
21636c5c8ca7SJamal Hadi Salim 	struct xfrm_policy *xp;
21647b67c857SThomas Graf 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
21656c5c8ca7SJamal Hadi Salim 	struct xfrm_userpolicy_info *p = &up->pol;
2166b798a9edSJamal Hadi Salim 	u8 type = XFRM_POLICY_TYPE_MAIN;
21676c5c8ca7SJamal Hadi Salim 	int err = -ENOENT;
2168295fae56SJamal Hadi Salim 	struct xfrm_mark m;
2169295fae56SJamal Hadi Salim 	u32 mark = xfrm_mark_get(attrs, &m);
21707e652640SSteffen Klassert 	u32 if_id = 0;
21716c5c8ca7SJamal Hadi Salim 
217235a7aa08SThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
2173f7b6983fSMasahide NAKAMURA 	if (err)
2174f7b6983fSMasahide NAKAMURA 		return err;
2175f7b6983fSMasahide NAKAMURA 
2176c8bf4d04STimo Teräs 	err = verify_policy_dir(p->dir);
2177c8bf4d04STimo Teräs 	if (err)
2178c8bf4d04STimo Teräs 		return err;
2179c8bf4d04STimo Teräs 
21807e652640SSteffen Klassert 	if (attrs[XFRMA_IF_ID])
21817e652640SSteffen Klassert 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
21827e652640SSteffen Klassert 
21836c5c8ca7SJamal Hadi Salim 	if (p->index)
21847e652640SSteffen Klassert 		xp = xfrm_policy_byid(net, mark, if_id, type, p->dir, p->index, 0, &err);
21856c5c8ca7SJamal Hadi Salim 	else {
21865424f32eSThomas Graf 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
218703e1ad7bSPaul Moore 		struct xfrm_sec_ctx *ctx;
21886c5c8ca7SJamal Hadi Salim 
218935a7aa08SThomas Graf 		err = verify_sec_ctx_len(attrs);
21906c5c8ca7SJamal Hadi Salim 		if (err)
21916c5c8ca7SJamal Hadi Salim 			return err;
21926c5c8ca7SJamal Hadi Salim 
21932c8dd116SDenis V. Lunev 		ctx = NULL;
21946c5c8ca7SJamal Hadi Salim 		if (rt) {
21955424f32eSThomas Graf 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
21966c5c8ca7SJamal Hadi Salim 
219752a4c640SNikolay Aleksandrov 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
219803e1ad7bSPaul Moore 			if (err)
21996c5c8ca7SJamal Hadi Salim 				return err;
22002c8dd116SDenis V. Lunev 		}
22017e652640SSteffen Klassert 		xp = xfrm_policy_bysel_ctx(net, mark, if_id, type, p->dir,
22026f26b61eSJamal Hadi Salim 					   &p->sel, ctx, 0, &err);
220303e1ad7bSPaul Moore 		security_xfrm_policy_free(ctx);
22046c5c8ca7SJamal Hadi Salim 	}
22056c5c8ca7SJamal Hadi Salim 	if (xp == NULL)
2206ef41aaa0SEric Paris 		return -ENOENT;
220703e1ad7bSPaul Moore 
2208ea2dea9dSTimo Teräs 	if (unlikely(xp->walk.dead))
22096c5c8ca7SJamal Hadi Salim 		goto out;
22106c5c8ca7SJamal Hadi Salim 
22116c5c8ca7SJamal Hadi Salim 	err = 0;
22126c5c8ca7SJamal Hadi Salim 	if (up->hard) {
22136c5c8ca7SJamal Hadi Salim 		xfrm_policy_delete(xp, p->dir);
22142e71029eSTetsuo Handa 		xfrm_audit_policy_delete(xp, 1, true);
22156c5c8ca7SJamal Hadi Salim 	}
2216c6bb8136SEric W. Biederman 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
22176c5c8ca7SJamal Hadi Salim 
22186c5c8ca7SJamal Hadi Salim out:
22196c5c8ca7SJamal Hadi Salim 	xfrm_pol_put(xp);
22206c5c8ca7SJamal Hadi Salim 	return err;
22216c5c8ca7SJamal Hadi Salim }
22226c5c8ca7SJamal Hadi Salim 
222322e70050SChristoph Hellwig static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
22245424f32eSThomas Graf 		struct nlattr **attrs)
222553bc6b4dSJamal Hadi Salim {
2226fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
222753bc6b4dSJamal Hadi Salim 	struct xfrm_state *x;
222853bc6b4dSJamal Hadi Salim 	int err;
22297b67c857SThomas Graf 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
223053bc6b4dSJamal Hadi Salim 	struct xfrm_usersa_info *p = &ue->state;
22316f26b61eSJamal Hadi Salim 	struct xfrm_mark m;
2232928497f0SNicolas Dichtel 	u32 mark = xfrm_mark_get(attrs, &m);
223353bc6b4dSJamal Hadi Salim 
22346f26b61eSJamal Hadi Salim 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
223553bc6b4dSJamal Hadi Salim 
22363a765aa5SDavid S. Miller 	err = -ENOENT;
223753bc6b4dSJamal Hadi Salim 	if (x == NULL)
223853bc6b4dSJamal Hadi Salim 		return err;
223953bc6b4dSJamal Hadi Salim 
224053bc6b4dSJamal Hadi Salim 	spin_lock_bh(&x->lock);
22413a765aa5SDavid S. Miller 	err = -EINVAL;
224253bc6b4dSJamal Hadi Salim 	if (x->km.state != XFRM_STATE_VALID)
224353bc6b4dSJamal Hadi Salim 		goto out;
2244c6bb8136SEric W. Biederman 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
224553bc6b4dSJamal Hadi Salim 
2246161a09e7SJoy Latten 	if (ue->hard) {
224753bc6b4dSJamal Hadi Salim 		__xfrm_state_delete(x);
22482e71029eSTetsuo Handa 		xfrm_audit_state_delete(x, 1, true);
2249161a09e7SJoy Latten 	}
22503a765aa5SDavid S. Miller 	err = 0;
225153bc6b4dSJamal Hadi Salim out:
225253bc6b4dSJamal Hadi Salim 	spin_unlock_bh(&x->lock);
225353bc6b4dSJamal Hadi Salim 	xfrm_state_put(x);
225453bc6b4dSJamal Hadi Salim 	return err;
225553bc6b4dSJamal Hadi Salim }
225653bc6b4dSJamal Hadi Salim 
225722e70050SChristoph Hellwig static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
22585424f32eSThomas Graf 		struct nlattr **attrs)
2259980ebd25SJamal Hadi Salim {
2260fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
2261980ebd25SJamal Hadi Salim 	struct xfrm_policy *xp;
2262980ebd25SJamal Hadi Salim 	struct xfrm_user_tmpl *ut;
2263980ebd25SJamal Hadi Salim 	int i;
22645424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_TMPL];
22656f26b61eSJamal Hadi Salim 	struct xfrm_mark mark;
2266980ebd25SJamal Hadi Salim 
22677b67c857SThomas Graf 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2268fc34acd3SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
2269980ebd25SJamal Hadi Salim 	int err = -ENOMEM;
2270980ebd25SJamal Hadi Salim 
2271980ebd25SJamal Hadi Salim 	if (!x)
2272d8eb9307SIlpo Järvinen 		goto nomem;
2273980ebd25SJamal Hadi Salim 
22746f26b61eSJamal Hadi Salim 	xfrm_mark_get(attrs, &mark);
22756f26b61eSJamal Hadi Salim 
2276980ebd25SJamal Hadi Salim 	err = verify_newpolicy_info(&ua->policy);
2277d8eb9307SIlpo Järvinen 	if (err)
227873efc324SVegard Nossum 		goto free_state;
2279980ebd25SJamal Hadi Salim 
2280980ebd25SJamal Hadi Salim 	/*   build an XP */
2281fc34acd3SAlexey Dobriyan 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2282d8eb9307SIlpo Järvinen 	if (!xp)
2283d8eb9307SIlpo Järvinen 		goto free_state;
2284980ebd25SJamal Hadi Salim 
2285980ebd25SJamal Hadi Salim 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2286980ebd25SJamal Hadi Salim 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2287980ebd25SJamal Hadi Salim 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
22886f26b61eSJamal Hadi Salim 	xp->mark.m = x->mark.m = mark.m;
22896f26b61eSJamal Hadi Salim 	xp->mark.v = x->mark.v = mark.v;
22905424f32eSThomas Graf 	ut = nla_data(rt);
2291980ebd25SJamal Hadi Salim 	/* extract the templates and for each call km_key */
2292980ebd25SJamal Hadi Salim 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2293980ebd25SJamal Hadi Salim 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2294980ebd25SJamal Hadi Salim 		memcpy(&x->id, &t->id, sizeof(x->id));
2295980ebd25SJamal Hadi Salim 		x->props.mode = t->mode;
2296980ebd25SJamal Hadi Salim 		x->props.reqid = t->reqid;
2297980ebd25SJamal Hadi Salim 		x->props.family = ut->family;
2298980ebd25SJamal Hadi Salim 		t->aalgos = ua->aalgos;
2299980ebd25SJamal Hadi Salim 		t->ealgos = ua->ealgos;
2300980ebd25SJamal Hadi Salim 		t->calgos = ua->calgos;
2301980ebd25SJamal Hadi Salim 		err = km_query(x, t, xp);
2302980ebd25SJamal Hadi Salim 
2303980ebd25SJamal Hadi Salim 	}
2304980ebd25SJamal Hadi Salim 
23054a135e53SMathias Krause 	xfrm_state_free(x);
2306980ebd25SJamal Hadi Salim 	kfree(xp);
2307980ebd25SJamal Hadi Salim 
2308980ebd25SJamal Hadi Salim 	return 0;
2309d8eb9307SIlpo Järvinen 
2310d8eb9307SIlpo Järvinen free_state:
23114a135e53SMathias Krause 	xfrm_state_free(x);
2312d8eb9307SIlpo Järvinen nomem:
2313d8eb9307SIlpo Järvinen 	return err;
2314980ebd25SJamal Hadi Salim }
2315980ebd25SJamal Hadi Salim 
23165c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
23175c79de6eSShinta Sugimoto static int copy_from_user_migrate(struct xfrm_migrate *ma,
231813c1d189SArnaud Ebalard 				  struct xfrm_kmaddress *k,
23195424f32eSThomas Graf 				  struct nlattr **attrs, int *num)
23205c79de6eSShinta Sugimoto {
23215424f32eSThomas Graf 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
23225c79de6eSShinta Sugimoto 	struct xfrm_user_migrate *um;
23235c79de6eSShinta Sugimoto 	int i, num_migrate;
23245c79de6eSShinta Sugimoto 
232513c1d189SArnaud Ebalard 	if (k != NULL) {
232613c1d189SArnaud Ebalard 		struct xfrm_user_kmaddress *uk;
232713c1d189SArnaud Ebalard 
232813c1d189SArnaud Ebalard 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
232913c1d189SArnaud Ebalard 		memcpy(&k->local, &uk->local, sizeof(k->local));
233013c1d189SArnaud Ebalard 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
233113c1d189SArnaud Ebalard 		k->family = uk->family;
233213c1d189SArnaud Ebalard 		k->reserved = uk->reserved;
233313c1d189SArnaud Ebalard 	}
233413c1d189SArnaud Ebalard 
23355424f32eSThomas Graf 	um = nla_data(rt);
23365424f32eSThomas Graf 	num_migrate = nla_len(rt) / sizeof(*um);
23375c79de6eSShinta Sugimoto 
23385c79de6eSShinta Sugimoto 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
23395c79de6eSShinta Sugimoto 		return -EINVAL;
23405c79de6eSShinta Sugimoto 
23415c79de6eSShinta Sugimoto 	for (i = 0; i < num_migrate; i++, um++, ma++) {
23425c79de6eSShinta Sugimoto 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
23435c79de6eSShinta Sugimoto 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
23445c79de6eSShinta Sugimoto 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
23455c79de6eSShinta Sugimoto 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
23465c79de6eSShinta Sugimoto 
23475c79de6eSShinta Sugimoto 		ma->proto = um->proto;
23485c79de6eSShinta Sugimoto 		ma->mode = um->mode;
23495c79de6eSShinta Sugimoto 		ma->reqid = um->reqid;
23505c79de6eSShinta Sugimoto 
23515c79de6eSShinta Sugimoto 		ma->old_family = um->old_family;
23525c79de6eSShinta Sugimoto 		ma->new_family = um->new_family;
23535c79de6eSShinta Sugimoto 	}
23545c79de6eSShinta Sugimoto 
23555c79de6eSShinta Sugimoto 	*num = i;
23565c79de6eSShinta Sugimoto 	return 0;
23575c79de6eSShinta Sugimoto }
23585c79de6eSShinta Sugimoto 
23595c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
23605424f32eSThomas Graf 			   struct nlattr **attrs)
23615c79de6eSShinta Sugimoto {
23627b67c857SThomas Graf 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
23635c79de6eSShinta Sugimoto 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
236413c1d189SArnaud Ebalard 	struct xfrm_kmaddress km, *kmp;
23655c79de6eSShinta Sugimoto 	u8 type;
23665c79de6eSShinta Sugimoto 	int err;
23675c79de6eSShinta Sugimoto 	int n = 0;
23688d549c4fSFan Du 	struct net *net = sock_net(skb->sk);
23694ab47d47SAntony Antony 	struct xfrm_encap_tmpl  *encap = NULL;
23705c79de6eSShinta Sugimoto 
237135a7aa08SThomas Graf 	if (attrs[XFRMA_MIGRATE] == NULL)
2372cf5cb79fSThomas Graf 		return -EINVAL;
23735c79de6eSShinta Sugimoto 
237413c1d189SArnaud Ebalard 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
237513c1d189SArnaud Ebalard 
23765424f32eSThomas Graf 	err = copy_from_user_policy_type(&type, attrs);
23775c79de6eSShinta Sugimoto 	if (err)
23785c79de6eSShinta Sugimoto 		return err;
23795c79de6eSShinta Sugimoto 
238013c1d189SArnaud Ebalard 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
23815c79de6eSShinta Sugimoto 	if (err)
23825c79de6eSShinta Sugimoto 		return err;
23835c79de6eSShinta Sugimoto 
23845c79de6eSShinta Sugimoto 	if (!n)
23855c79de6eSShinta Sugimoto 		return 0;
23865c79de6eSShinta Sugimoto 
23874ab47d47SAntony Antony 	if (attrs[XFRMA_ENCAP]) {
23884ab47d47SAntony Antony 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
23894ab47d47SAntony Antony 				sizeof(*encap), GFP_KERNEL);
23904ab47d47SAntony Antony 		if (!encap)
23915c79de6eSShinta Sugimoto 			return 0;
23925c79de6eSShinta Sugimoto 	}
23934ab47d47SAntony Antony 
23944ab47d47SAntony Antony 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
23954ab47d47SAntony Antony 
23964ab47d47SAntony Antony 	kfree(encap);
23974ab47d47SAntony Antony 
23984ab47d47SAntony Antony 	return err;
23994ab47d47SAntony Antony }
24005c79de6eSShinta Sugimoto #else
24015c79de6eSShinta Sugimoto static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
24025424f32eSThomas Graf 			   struct nlattr **attrs)
24035c79de6eSShinta Sugimoto {
24045c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
24055c79de6eSShinta Sugimoto }
24065c79de6eSShinta Sugimoto #endif
24075c79de6eSShinta Sugimoto 
24085c79de6eSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
2409183cad12SDavid S. Miller static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
24105c79de6eSShinta Sugimoto {
24115c79de6eSShinta Sugimoto 	struct xfrm_user_migrate um;
24125c79de6eSShinta Sugimoto 
24135c79de6eSShinta Sugimoto 	memset(&um, 0, sizeof(um));
24145c79de6eSShinta Sugimoto 	um.proto = m->proto;
24155c79de6eSShinta Sugimoto 	um.mode = m->mode;
24165c79de6eSShinta Sugimoto 	um.reqid = m->reqid;
24175c79de6eSShinta Sugimoto 	um.old_family = m->old_family;
24185c79de6eSShinta Sugimoto 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
24195c79de6eSShinta Sugimoto 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
24205c79de6eSShinta Sugimoto 	um.new_family = m->new_family;
24215c79de6eSShinta Sugimoto 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
24225c79de6eSShinta Sugimoto 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
24235c79de6eSShinta Sugimoto 
2424c0144beaSThomas Graf 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
24255c79de6eSShinta Sugimoto }
24265c79de6eSShinta Sugimoto 
2427183cad12SDavid S. Miller static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
242813c1d189SArnaud Ebalard {
242913c1d189SArnaud Ebalard 	struct xfrm_user_kmaddress uk;
243013c1d189SArnaud Ebalard 
243113c1d189SArnaud Ebalard 	memset(&uk, 0, sizeof(uk));
243213c1d189SArnaud Ebalard 	uk.family = k->family;
243313c1d189SArnaud Ebalard 	uk.reserved = k->reserved;
243413c1d189SArnaud Ebalard 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2435a1caa322SArnaud Ebalard 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
243613c1d189SArnaud Ebalard 
243713c1d189SArnaud Ebalard 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
243813c1d189SArnaud Ebalard }
243913c1d189SArnaud Ebalard 
2440a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
24418bafd730SAntony Antony 						int with_encp)
24427deb2264SThomas Graf {
24437deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
244413c1d189SArnaud Ebalard 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
24458bafd730SAntony Antony 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
24467deb2264SThomas Graf 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
24477deb2264SThomas Graf 	      + userpolicy_type_attrsize();
24487deb2264SThomas Graf }
24497deb2264SThomas Graf 
2450183cad12SDavid S. Miller static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2451183cad12SDavid S. Miller 			 int num_migrate, const struct xfrm_kmaddress *k,
24528bafd730SAntony Antony 			 const struct xfrm_selector *sel,
24538bafd730SAntony Antony 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
24545c79de6eSShinta Sugimoto {
2455183cad12SDavid S. Miller 	const struct xfrm_migrate *mp;
24565c79de6eSShinta Sugimoto 	struct xfrm_userpolicy_id *pol_id;
24575c79de6eSShinta Sugimoto 	struct nlmsghdr *nlh;
24581d1e34ddSDavid S. Miller 	int i, err;
24595c79de6eSShinta Sugimoto 
246079b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
246179b8b7f4SThomas Graf 	if (nlh == NULL)
246279b8b7f4SThomas Graf 		return -EMSGSIZE;
24635c79de6eSShinta Sugimoto 
24647b67c857SThomas Graf 	pol_id = nlmsg_data(nlh);
24655c79de6eSShinta Sugimoto 	/* copy data from selector, dir, and type to the pol_id */
24665c79de6eSShinta Sugimoto 	memset(pol_id, 0, sizeof(*pol_id));
24675c79de6eSShinta Sugimoto 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
24685c79de6eSShinta Sugimoto 	pol_id->dir = dir;
24695c79de6eSShinta Sugimoto 
24701d1e34ddSDavid S. Miller 	if (k != NULL) {
24711d1e34ddSDavid S. Miller 		err = copy_to_user_kmaddress(k, skb);
24721d1e34ddSDavid S. Miller 		if (err)
24731d1e34ddSDavid S. Miller 			goto out_cancel;
24741d1e34ddSDavid S. Miller 	}
24758bafd730SAntony Antony 	if (encap) {
24768bafd730SAntony Antony 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
24778bafd730SAntony Antony 		if (err)
24788bafd730SAntony Antony 			goto out_cancel;
24798bafd730SAntony Antony 	}
24801d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(type, skb);
24811d1e34ddSDavid S. Miller 	if (err)
24821d1e34ddSDavid S. Miller 		goto out_cancel;
24835c79de6eSShinta Sugimoto 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
24841d1e34ddSDavid S. Miller 		err = copy_to_user_migrate(mp, skb);
24851d1e34ddSDavid S. Miller 		if (err)
24861d1e34ddSDavid S. Miller 			goto out_cancel;
24875c79de6eSShinta Sugimoto 	}
24885c79de6eSShinta Sugimoto 
2489053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2490053c095aSJohannes Berg 	return 0;
24911d1e34ddSDavid S. Miller 
24921d1e34ddSDavid S. Miller out_cancel:
24939825069dSThomas Graf 	nlmsg_cancel(skb, nlh);
24941d1e34ddSDavid S. Miller 	return err;
24955c79de6eSShinta Sugimoto }
24965c79de6eSShinta Sugimoto 
2497183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2498183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
24998bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
25008bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
25015c79de6eSShinta Sugimoto {
2502a6483b79SAlexey Dobriyan 	struct net *net = &init_net;
25035c79de6eSShinta Sugimoto 	struct sk_buff *skb;
25042fc5f83bSGustavo A. R. Silva 	int err;
25055c79de6eSShinta Sugimoto 
25068bafd730SAntony Antony 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
25078bafd730SAntony Antony 			GFP_ATOMIC);
25085c79de6eSShinta Sugimoto 	if (skb == NULL)
25095c79de6eSShinta Sugimoto 		return -ENOMEM;
25105c79de6eSShinta Sugimoto 
25115c79de6eSShinta Sugimoto 	/* build migrate */
25122fc5f83bSGustavo A. R. Silva 	err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
25132fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
25145c79de6eSShinta Sugimoto 
251521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
25165c79de6eSShinta Sugimoto }
25175c79de6eSShinta Sugimoto #else
2518183cad12SDavid S. Miller static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2519183cad12SDavid S. Miller 			     const struct xfrm_migrate *m, int num_migrate,
25208bafd730SAntony Antony 			     const struct xfrm_kmaddress *k,
25218bafd730SAntony Antony 			     const struct xfrm_encap_tmpl *encap)
25225c79de6eSShinta Sugimoto {
25235c79de6eSShinta Sugimoto 	return -ENOPROTOOPT;
25245c79de6eSShinta Sugimoto }
25255c79de6eSShinta Sugimoto #endif
2526d51d081dSJamal Hadi Salim 
2527a7bd9a45SThomas Graf #define XMSGSIZE(type) sizeof(struct type)
2528492b558bSThomas Graf 
2529492b558bSThomas Graf static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
253066f9a259SDavid S. Miller 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2531492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2532492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2533492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2534492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2535492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2536492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2537980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
253853bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2539492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
254066f9a259SDavid S. Miller 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
25416c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2542492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2543a7bd9a45SThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2544d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2545d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
254697a64b45SMasahide NAKAMURA 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
25475c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2548a7bd9a45SThomas Graf 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2549880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2550a7bd9a45SThomas Graf 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
25511da177e4SLinus Torvalds };
25521da177e4SLinus Torvalds 
2553492b558bSThomas Graf #undef XMSGSIZE
2554492b558bSThomas Graf 
2555cf5cb79fSThomas Graf static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2556c28e9304Sjamal 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2557c28e9304Sjamal 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2558c28e9304Sjamal 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2559c28e9304Sjamal 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
25601a6509d9SHerbert Xu 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2561cf5cb79fSThomas Graf 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2562cf5cb79fSThomas Graf 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2563cf5cb79fSThomas Graf 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2564cf5cb79fSThomas Graf 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2565cf5cb79fSThomas Graf 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2566cf5cb79fSThomas Graf 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2567cf5cb79fSThomas Graf 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2568cf5cb79fSThomas Graf 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2569cf5cb79fSThomas Graf 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2570cf5cb79fSThomas Graf 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2571cf5cb79fSThomas Graf 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2572cf5cb79fSThomas Graf 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2573cf5cb79fSThomas Graf 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2574cf5cb79fSThomas Graf 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
257513c1d189SArnaud Ebalard 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
25766f26b61eSJamal Hadi Salim 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
257735d2856bSMartin Willi 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2578d8647b79SSteffen Klassert 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2579a947b0a9SNicolas Dichtel 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2580d3623099SNicolas Dichtel 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2581870a2df4SNicolas Dichtel 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2582d77e38e6SSteffen Klassert 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
25839b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK]	= { .type = NLA_U32 },
25849b42c1f1SSteffen Klassert 	[XFRMA_SET_MARK_MASK]	= { .type = NLA_U32 },
25857e652640SSteffen Klassert 	[XFRMA_IF_ID]		= { .type = NLA_U32 },
2586cf5cb79fSThomas Graf };
2587cf5cb79fSThomas Graf 
2588880a6fabSChristophe Gouault static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2589880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2590880a6fabSChristophe Gouault 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2591880a6fabSChristophe Gouault };
2592880a6fabSChristophe Gouault 
259305600a79SMathias Krause static const struct xfrm_link {
25945424f32eSThomas Graf 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
25951137b5e2SHerbert Xu 	int (*start)(struct netlink_callback *);
25961da177e4SLinus Torvalds 	int (*dump)(struct sk_buff *, struct netlink_callback *);
25974c563f76STimo Teras 	int (*done)(struct netlink_callback *);
2598880a6fabSChristophe Gouault 	const struct nla_policy *nla_pol;
2599880a6fabSChristophe Gouault 	int nla_max;
2600492b558bSThomas Graf } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2601492b558bSThomas Graf 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2602492b558bSThomas Graf 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2603492b558bSThomas Graf 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
26044c563f76STimo Teras 						   .dump = xfrm_dump_sa,
26054c563f76STimo Teras 						   .done = xfrm_dump_sa_done  },
2606492b558bSThomas Graf 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2607492b558bSThomas Graf 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2608492b558bSThomas Graf 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
26091137b5e2SHerbert Xu 						   .start = xfrm_dump_policy_start,
26104c563f76STimo Teras 						   .dump = xfrm_dump_policy,
26114c563f76STimo Teras 						   .done = xfrm_dump_policy_done },
2612492b558bSThomas Graf 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2613980ebd25SJamal Hadi Salim 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
261453bc6b4dSJamal Hadi Salim 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2615492b558bSThomas Graf 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2616492b558bSThomas Graf 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
26176c5c8ca7SJamal Hadi Salim 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2618492b558bSThomas Graf 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2619492b558bSThomas Graf 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2620d51d081dSJamal Hadi Salim 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2621d51d081dSJamal Hadi Salim 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
26225c79de6eSShinta Sugimoto 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
262328d8909bSJamal Hadi Salim 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2624880a6fabSChristophe Gouault 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2625880a6fabSChristophe Gouault 						   .nla_pol = xfrma_spd_policy,
2626880a6fabSChristophe Gouault 						   .nla_max = XFRMA_SPD_MAX },
2627ecfd6b18SJamal Hadi Salim 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
26281da177e4SLinus Torvalds };
26291da177e4SLinus Torvalds 
26302d4bc933SJohannes Berg static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
26312d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
26321da177e4SLinus Torvalds {
2633a6483b79SAlexey Dobriyan 	struct net *net = sock_net(skb->sk);
263435a7aa08SThomas Graf 	struct nlattr *attrs[XFRMA_MAX+1];
263505600a79SMathias Krause 	const struct xfrm_link *link;
2636a7bd9a45SThomas Graf 	int type, err;
26371da177e4SLinus Torvalds 
26382bf8c476SAndy Lutomirski 	if (in_compat_syscall())
263983e2d058SYi Zhao 		return -EOPNOTSUPP;
264074005991SFan Du 
26411da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
26421da177e4SLinus Torvalds 	if (type > XFRM_MSG_MAX)
26431d00a4ebSThomas Graf 		return -EINVAL;
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds 	type -= XFRM_MSG_BASE;
26461da177e4SLinus Torvalds 	link = &xfrm_dispatch[type];
26471da177e4SLinus Torvalds 
26481da177e4SLinus Torvalds 	/* All operations require privileges, even GET */
264990f62cf3SEric W. Biederman 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
26501d00a4ebSThomas Graf 		return -EPERM;
26511da177e4SLinus Torvalds 
2652492b558bSThomas Graf 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2653492b558bSThomas Graf 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2654b8f3ab42SDavid S. Miller 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
26551da177e4SLinus Torvalds 		if (link->dump == NULL)
26561d00a4ebSThomas Graf 			return -EINVAL;
26571da177e4SLinus Torvalds 
265880d326faSPablo Neira Ayuso 		{
265980d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
26601137b5e2SHerbert Xu 				.start = link->start,
266180d326faSPablo Neira Ayuso 				.dump = link->dump,
266280d326faSPablo Neira Ayuso 				.done = link->done,
266380d326faSPablo Neira Ayuso 			};
266480d326faSPablo Neira Ayuso 			return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
266580d326faSPablo Neira Ayuso 		}
26661da177e4SLinus Torvalds 	}
26671da177e4SLinus Torvalds 
26688cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2669880a6fabSChristophe Gouault 				     link->nla_max ? : XFRMA_MAX,
2670fe52145fSJohannes Berg 				     link->nla_pol ? : xfrma_policy, extack);
2671a7bd9a45SThomas Graf 	if (err < 0)
2672a7bd9a45SThomas Graf 		return err;
26731da177e4SLinus Torvalds 
26741da177e4SLinus Torvalds 	if (link->doit == NULL)
26751d00a4ebSThomas Graf 		return -EINVAL;
26761da177e4SLinus Torvalds 
26775424f32eSThomas Graf 	return link->doit(skb, nlh, attrs);
26781da177e4SLinus Torvalds }
26791da177e4SLinus Torvalds 
2680cd40b7d3SDenis V. Lunev static void xfrm_netlink_rcv(struct sk_buff *skb)
26811da177e4SLinus Torvalds {
2682283bc9f3SFan Du 	struct net *net = sock_net(skb->sk);
2683283bc9f3SFan Du 
2684283bc9f3SFan Du 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2685cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2686283bc9f3SFan Du 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
26871da177e4SLinus Torvalds }
26881da177e4SLinus Torvalds 
2689a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_expire_msgsize(void)
26907deb2264SThomas Graf {
26916f26b61eSJamal Hadi Salim 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
26926f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark));
26937deb2264SThomas Graf }
26947deb2264SThomas Graf 
2695214e005bSDavid S. Miller static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
26961da177e4SLinus Torvalds {
26971da177e4SLinus Torvalds 	struct xfrm_user_expire *ue;
26981da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
26991d1e34ddSDavid S. Miller 	int err;
27001da177e4SLinus Torvalds 
270115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
270279b8b7f4SThomas Graf 	if (nlh == NULL)
270379b8b7f4SThomas Graf 		return -EMSGSIZE;
27041da177e4SLinus Torvalds 
27057b67c857SThomas Graf 	ue = nlmsg_data(nlh);
27061da177e4SLinus Torvalds 	copy_to_user_state(x, &ue->state);
2707d51d081dSJamal Hadi Salim 	ue->hard = (c->data.hard != 0) ? 1 : 0;
2708e3e5fc16SMathias Krause 	/* clear the padding bytes */
2709e3e5fc16SMathias Krause 	memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
27101da177e4SLinus Torvalds 
27111d1e34ddSDavid S. Miller 	err = xfrm_mark_put(skb, &x->mark);
27121d1e34ddSDavid S. Miller 	if (err)
27131d1e34ddSDavid S. Miller 		return err;
27146f26b61eSJamal Hadi Salim 
27157e652640SSteffen Klassert 	err = xfrm_if_id_put(skb, x->if_id);
27167e652640SSteffen Klassert 	if (err)
27177e652640SSteffen Klassert 		return err;
27187e652640SSteffen Klassert 
2719053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2720053c095aSJohannes Berg 	return 0;
27211da177e4SLinus Torvalds }
27221da177e4SLinus Torvalds 
2723214e005bSDavid S. Miller static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
27241da177e4SLinus Torvalds {
2725fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
27261da177e4SLinus Torvalds 	struct sk_buff *skb;
27271da177e4SLinus Torvalds 
27287deb2264SThomas Graf 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
27291da177e4SLinus Torvalds 	if (skb == NULL)
27301da177e4SLinus Torvalds 		return -ENOMEM;
27311da177e4SLinus Torvalds 
27326f26b61eSJamal Hadi Salim 	if (build_expire(skb, x, c) < 0) {
27336f26b61eSJamal Hadi Salim 		kfree_skb(skb);
27346f26b61eSJamal Hadi Salim 		return -EMSGSIZE;
27356f26b61eSJamal Hadi Salim 	}
27361da177e4SLinus Torvalds 
273721ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds 
2740214e005bSDavid S. Miller static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2741d51d081dSJamal Hadi Salim {
2742fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
2743d51d081dSJamal Hadi Salim 	struct sk_buff *skb;
27442fc5f83bSGustavo A. R. Silva 	int err;
2745d51d081dSJamal Hadi Salim 
2746d8647b79SSteffen Klassert 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2747d51d081dSJamal Hadi Salim 	if (skb == NULL)
2748d51d081dSJamal Hadi Salim 		return -ENOMEM;
2749d51d081dSJamal Hadi Salim 
27502fc5f83bSGustavo A. R. Silva 	err = build_aevent(skb, x, c);
27512fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
2752d51d081dSJamal Hadi Salim 
275321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2754d51d081dSJamal Hadi Salim }
2755d51d081dSJamal Hadi Salim 
2756214e005bSDavid S. Miller static int xfrm_notify_sa_flush(const struct km_event *c)
275726b15dadSJamal Hadi Salim {
27587067802eSAlexey Dobriyan 	struct net *net = c->net;
275926b15dadSJamal Hadi Salim 	struct xfrm_usersa_flush *p;
276026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
276126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
27627deb2264SThomas Graf 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
276326b15dadSJamal Hadi Salim 
27647deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
276526b15dadSJamal Hadi Salim 	if (skb == NULL)
276626b15dadSJamal Hadi Salim 		return -ENOMEM;
276726b15dadSJamal Hadi Salim 
276815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
276979b8b7f4SThomas Graf 	if (nlh == NULL) {
277079b8b7f4SThomas Graf 		kfree_skb(skb);
277179b8b7f4SThomas Graf 		return -EMSGSIZE;
277279b8b7f4SThomas Graf 	}
277326b15dadSJamal Hadi Salim 
27747b67c857SThomas Graf 	p = nlmsg_data(nlh);
2775bf08867fSHerbert Xu 	p->proto = c->data.proto;
277626b15dadSJamal Hadi Salim 
27779825069dSThomas Graf 	nlmsg_end(skb, nlh);
277826b15dadSJamal Hadi Salim 
277921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
278026b15dadSJamal Hadi Salim }
278126b15dadSJamal Hadi Salim 
2782a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
278326b15dadSJamal Hadi Salim {
2784a1b831f2SAlexey Dobriyan 	unsigned int l = 0;
27851a6509d9SHerbert Xu 	if (x->aead)
27861a6509d9SHerbert Xu 		l += nla_total_size(aead_len(x->aead));
27874447bb33SMartin Willi 	if (x->aalg) {
27884447bb33SMartin Willi 		l += nla_total_size(sizeof(struct xfrm_algo) +
27894447bb33SMartin Willi 				    (x->aalg->alg_key_len + 7) / 8);
27904447bb33SMartin Willi 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
27914447bb33SMartin Willi 	}
279226b15dadSJamal Hadi Salim 	if (x->ealg)
27930f99be0dSEric Dumazet 		l += nla_total_size(xfrm_alg_len(x->ealg));
279426b15dadSJamal Hadi Salim 	if (x->calg)
27957deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->calg));
279626b15dadSJamal Hadi Salim 	if (x->encap)
27977deb2264SThomas Graf 		l += nla_total_size(sizeof(*x->encap));
279835d2856bSMartin Willi 	if (x->tfcpad)
279935d2856bSMartin Willi 		l += nla_total_size(sizeof(x->tfcpad));
2800d8647b79SSteffen Klassert 	if (x->replay_esn)
2801d8647b79SSteffen Klassert 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2802f293a5e3Sdingzhi 	else
2803f293a5e3Sdingzhi 		l += nla_total_size(sizeof(struct xfrm_replay_state));
280468325d3bSHerbert Xu 	if (x->security)
280568325d3bSHerbert Xu 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
280668325d3bSHerbert Xu 				    x->security->ctx_len);
280768325d3bSHerbert Xu 	if (x->coaddr)
280868325d3bSHerbert Xu 		l += nla_total_size(sizeof(*x->coaddr));
2809a947b0a9SNicolas Dichtel 	if (x->props.extra_flags)
2810a947b0a9SNicolas Dichtel 		l += nla_total_size(sizeof(x->props.extra_flags));
2811d77e38e6SSteffen Klassert 	if (x->xso.dev)
2812d77e38e6SSteffen Klassert 		 l += nla_total_size(sizeof(x->xso));
28139b42c1f1SSteffen Klassert 	if (x->props.smark.v | x->props.smark.m) {
28149b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.v));
28159b42c1f1SSteffen Klassert 		l += nla_total_size(sizeof(x->props.smark.m));
28169b42c1f1SSteffen Klassert 	}
28177e652640SSteffen Klassert 	if (x->if_id)
28187e652640SSteffen Klassert 		l += nla_total_size(sizeof(x->if_id));
281968325d3bSHerbert Xu 
2820d26f3984SHerbert Xu 	/* Must count x->lastused as it may become non-zero behind our back. */
2821de95c4a4SNicolas Dichtel 	l += nla_total_size_64bit(sizeof(u64));
282226b15dadSJamal Hadi Salim 
282326b15dadSJamal Hadi Salim 	return l;
282426b15dadSJamal Hadi Salim }
282526b15dadSJamal Hadi Salim 
2826214e005bSDavid S. Miller static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
282726b15dadSJamal Hadi Salim {
2828fc34acd3SAlexey Dobriyan 	struct net *net = xs_net(x);
282926b15dadSJamal Hadi Salim 	struct xfrm_usersa_info *p;
28300603eac0SHerbert Xu 	struct xfrm_usersa_id *id;
283126b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
283226b15dadSJamal Hadi Salim 	struct sk_buff *skb;
2833a1b831f2SAlexey Dobriyan 	unsigned int len = xfrm_sa_len(x);
2834a1b831f2SAlexey Dobriyan 	unsigned int headlen;
2835a1b831f2SAlexey Dobriyan 	int err;
28360603eac0SHerbert Xu 
28370603eac0SHerbert Xu 	headlen = sizeof(*p);
28380603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
28397deb2264SThomas Graf 		len += nla_total_size(headlen);
28400603eac0SHerbert Xu 		headlen = sizeof(*id);
28416f26b61eSJamal Hadi Salim 		len += nla_total_size(sizeof(struct xfrm_mark));
28420603eac0SHerbert Xu 	}
28437deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
284426b15dadSJamal Hadi Salim 
28457deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
284626b15dadSJamal Hadi Salim 	if (skb == NULL)
284726b15dadSJamal Hadi Salim 		return -ENOMEM;
284826b15dadSJamal Hadi Salim 
284915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
28501d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
285179b8b7f4SThomas Graf 	if (nlh == NULL)
28521d1e34ddSDavid S. Miller 		goto out_free_skb;
285326b15dadSJamal Hadi Salim 
28547b67c857SThomas Graf 	p = nlmsg_data(nlh);
28550603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELSA) {
2856c0144beaSThomas Graf 		struct nlattr *attr;
2857c0144beaSThomas Graf 
28587b67c857SThomas Graf 		id = nlmsg_data(nlh);
285950329c8aSMathias Krause 		memset(id, 0, sizeof(*id));
28600603eac0SHerbert Xu 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
28610603eac0SHerbert Xu 		id->spi = x->id.spi;
28620603eac0SHerbert Xu 		id->family = x->props.family;
28630603eac0SHerbert Xu 		id->proto = x->id.proto;
28640603eac0SHerbert Xu 
2865c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
28661d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
2867c0144beaSThomas Graf 		if (attr == NULL)
28681d1e34ddSDavid S. Miller 			goto out_free_skb;
2869c0144beaSThomas Graf 
2870c0144beaSThomas Graf 		p = nla_data(attr);
28710603eac0SHerbert Xu 	}
28721d1e34ddSDavid S. Miller 	err = copy_to_user_state_extra(x, p, skb);
28731d1e34ddSDavid S. Miller 	if (err)
28741d1e34ddSDavid S. Miller 		goto out_free_skb;
287526b15dadSJamal Hadi Salim 
28769825069dSThomas Graf 	nlmsg_end(skb, nlh);
287726b15dadSJamal Hadi Salim 
287821ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
287926b15dadSJamal Hadi Salim 
28801d1e34ddSDavid S. Miller out_free_skb:
288126b15dadSJamal Hadi Salim 	kfree_skb(skb);
28821d1e34ddSDavid S. Miller 	return err;
288326b15dadSJamal Hadi Salim }
288426b15dadSJamal Hadi Salim 
2885214e005bSDavid S. Miller static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
288626b15dadSJamal Hadi Salim {
288726b15dadSJamal Hadi Salim 
288826b15dadSJamal Hadi Salim 	switch (c->event) {
2889f60f6b8fSHerbert Xu 	case XFRM_MSG_EXPIRE:
289026b15dadSJamal Hadi Salim 		return xfrm_exp_state_notify(x, c);
2891d51d081dSJamal Hadi Salim 	case XFRM_MSG_NEWAE:
2892d51d081dSJamal Hadi Salim 		return xfrm_aevent_state_notify(x, c);
2893f60f6b8fSHerbert Xu 	case XFRM_MSG_DELSA:
2894f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDSA:
2895f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWSA:
289626b15dadSJamal Hadi Salim 		return xfrm_notify_sa(x, c);
2897f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHSA:
289826b15dadSJamal Hadi Salim 		return xfrm_notify_sa_flush(c);
289926b15dadSJamal Hadi Salim 	default:
290062db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
290162db5cfdSstephen hemminger 		       c->event);
290226b15dadSJamal Hadi Salim 		break;
290326b15dadSJamal Hadi Salim 	}
290426b15dadSJamal Hadi Salim 
290526b15dadSJamal Hadi Salim 	return 0;
290626b15dadSJamal Hadi Salim 
290726b15dadSJamal Hadi Salim }
290826b15dadSJamal Hadi Salim 
2909a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
29107deb2264SThomas Graf 						struct xfrm_policy *xp)
29117deb2264SThomas Graf {
29127deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
29137deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
29146f26b61eSJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
29157deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
29167deb2264SThomas Graf 	       + userpolicy_type_attrsize();
29177deb2264SThomas Graf }
29187deb2264SThomas Graf 
29191da177e4SLinus Torvalds static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
292065e0736bSFan Du 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
29211da177e4SLinus Torvalds {
29221d1e34ddSDavid S. Miller 	__u32 seq = xfrm_get_acqseq();
29231da177e4SLinus Torvalds 	struct xfrm_user_acquire *ua;
29241da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
29251d1e34ddSDavid S. Miller 	int err;
29261da177e4SLinus Torvalds 
292779b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
292879b8b7f4SThomas Graf 	if (nlh == NULL)
292979b8b7f4SThomas Graf 		return -EMSGSIZE;
29301da177e4SLinus Torvalds 
29317b67c857SThomas Graf 	ua = nlmsg_data(nlh);
29321da177e4SLinus Torvalds 	memcpy(&ua->id, &x->id, sizeof(ua->id));
29331da177e4SLinus Torvalds 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
29341da177e4SLinus Torvalds 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
293565e0736bSFan Du 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
29361da177e4SLinus Torvalds 	ua->aalgos = xt->aalgos;
29371da177e4SLinus Torvalds 	ua->ealgos = xt->ealgos;
29381da177e4SLinus Torvalds 	ua->calgos = xt->calgos;
29391da177e4SLinus Torvalds 	ua->seq = x->km.seq = seq;
29401da177e4SLinus Torvalds 
29411d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
29421d1e34ddSDavid S. Miller 	if (!err)
29431d1e34ddSDavid S. Miller 		err = copy_to_user_state_sec_ctx(x, skb);
29441d1e34ddSDavid S. Miller 	if (!err)
29451d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
29461d1e34ddSDavid S. Miller 	if (!err)
29471d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
29487e652640SSteffen Klassert 	if (!err)
29497e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
29501d1e34ddSDavid S. Miller 	if (err) {
29511d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
29521d1e34ddSDavid S. Miller 		return err;
29531d1e34ddSDavid S. Miller 	}
29541da177e4SLinus Torvalds 
2955053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2956053c095aSJohannes Berg 	return 0;
29571da177e4SLinus Torvalds }
29581da177e4SLinus Torvalds 
29591da177e4SLinus Torvalds static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
296065e0736bSFan Du 			     struct xfrm_policy *xp)
29611da177e4SLinus Torvalds {
2962a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
29631da177e4SLinus Torvalds 	struct sk_buff *skb;
29642fc5f83bSGustavo A. R. Silva 	int err;
29651da177e4SLinus Torvalds 
29667deb2264SThomas Graf 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
29671da177e4SLinus Torvalds 	if (skb == NULL)
29681da177e4SLinus Torvalds 		return -ENOMEM;
29691da177e4SLinus Torvalds 
29702fc5f83bSGustavo A. R. Silva 	err = build_acquire(skb, x, xt, xp);
29712fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
29721da177e4SLinus Torvalds 
297321ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
29741da177e4SLinus Torvalds }
29751da177e4SLinus Torvalds 
29761da177e4SLinus Torvalds /* User gives us xfrm_user_policy_info followed by an array of 0
29771da177e4SLinus Torvalds  * or more templates.
29781da177e4SLinus Torvalds  */
2979cb969f07SVenkat Yekkirala static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
29801da177e4SLinus Torvalds 					       u8 *data, int len, int *dir)
29811da177e4SLinus Torvalds {
2982fc34acd3SAlexey Dobriyan 	struct net *net = sock_net(sk);
29831da177e4SLinus Torvalds 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
29841da177e4SLinus Torvalds 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
29851da177e4SLinus Torvalds 	struct xfrm_policy *xp;
29861da177e4SLinus Torvalds 	int nr;
29871da177e4SLinus Torvalds 
2988cb969f07SVenkat Yekkirala 	switch (sk->sk_family) {
29891da177e4SLinus Torvalds 	case AF_INET:
29901da177e4SLinus Torvalds 		if (opt != IP_XFRM_POLICY) {
29911da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
29921da177e4SLinus Torvalds 			return NULL;
29931da177e4SLinus Torvalds 		}
29941da177e4SLinus Torvalds 		break;
2995dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
29961da177e4SLinus Torvalds 	case AF_INET6:
29971da177e4SLinus Torvalds 		if (opt != IPV6_XFRM_POLICY) {
29981da177e4SLinus Torvalds 			*dir = -EOPNOTSUPP;
29991da177e4SLinus Torvalds 			return NULL;
30001da177e4SLinus Torvalds 		}
30011da177e4SLinus Torvalds 		break;
30021da177e4SLinus Torvalds #endif
30031da177e4SLinus Torvalds 	default:
30041da177e4SLinus Torvalds 		*dir = -EINVAL;
30051da177e4SLinus Torvalds 		return NULL;
30061da177e4SLinus Torvalds 	}
30071da177e4SLinus Torvalds 
30081da177e4SLinus Torvalds 	*dir = -EINVAL;
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds 	if (len < sizeof(*p) ||
30111da177e4SLinus Torvalds 	    verify_newpolicy_info(p))
30121da177e4SLinus Torvalds 		return NULL;
30131da177e4SLinus Torvalds 
30141da177e4SLinus Torvalds 	nr = ((len - sizeof(*p)) / sizeof(*ut));
3015b4ad86bfSDavid S. Miller 	if (validate_tmpl(nr, ut, p->sel.family))
30161da177e4SLinus Torvalds 		return NULL;
30171da177e4SLinus Torvalds 
3018a4f1bac6SHerbert Xu 	if (p->dir > XFRM_POLICY_OUT)
3019a4f1bac6SHerbert Xu 		return NULL;
3020a4f1bac6SHerbert Xu 
30212f09a4d5SHerbert Xu 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
30221da177e4SLinus Torvalds 	if (xp == NULL) {
30231da177e4SLinus Torvalds 		*dir = -ENOBUFS;
30241da177e4SLinus Torvalds 		return NULL;
30251da177e4SLinus Torvalds 	}
30261da177e4SLinus Torvalds 
30271da177e4SLinus Torvalds 	copy_from_user_policy(xp, p);
3028f7b6983fSMasahide NAKAMURA 	xp->type = XFRM_POLICY_TYPE_MAIN;
30291da177e4SLinus Torvalds 	copy_templates(xp, ut, nr);
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds 	*dir = p->dir;
30321da177e4SLinus Torvalds 
30331da177e4SLinus Torvalds 	return xp;
30341da177e4SLinus Torvalds }
30351da177e4SLinus Torvalds 
3036a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
30377deb2264SThomas Graf {
30387deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
30397deb2264SThomas Graf 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
30407deb2264SThomas Graf 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3041295fae56SJamal Hadi Salim 	       + nla_total_size(sizeof(struct xfrm_mark))
30427deb2264SThomas Graf 	       + userpolicy_type_attrsize();
30437deb2264SThomas Graf }
30447deb2264SThomas Graf 
30451da177e4SLinus Torvalds static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3046214e005bSDavid S. Miller 			   int dir, const struct km_event *c)
30471da177e4SLinus Torvalds {
30481da177e4SLinus Torvalds 	struct xfrm_user_polexpire *upe;
3049d51d081dSJamal Hadi Salim 	int hard = c->data.hard;
30501d1e34ddSDavid S. Miller 	struct nlmsghdr *nlh;
30511d1e34ddSDavid S. Miller 	int err;
30521da177e4SLinus Torvalds 
305315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
305479b8b7f4SThomas Graf 	if (nlh == NULL)
305579b8b7f4SThomas Graf 		return -EMSGSIZE;
30561da177e4SLinus Torvalds 
30577b67c857SThomas Graf 	upe = nlmsg_data(nlh);
30581da177e4SLinus Torvalds 	copy_to_user_policy(xp, &upe->pol, dir);
30591d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
30601d1e34ddSDavid S. Miller 	if (!err)
30611d1e34ddSDavid S. Miller 		err = copy_to_user_sec_ctx(xp, skb);
30621d1e34ddSDavid S. Miller 	if (!err)
30631d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
30641d1e34ddSDavid S. Miller 	if (!err)
30651d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
30667e652640SSteffen Klassert 	if (!err)
30677e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
30681d1e34ddSDavid S. Miller 	if (err) {
30691d1e34ddSDavid S. Miller 		nlmsg_cancel(skb, nlh);
30701d1e34ddSDavid S. Miller 		return err;
30711d1e34ddSDavid S. Miller 	}
30721da177e4SLinus Torvalds 	upe->hard = !!hard;
30731da177e4SLinus Torvalds 
3074053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3075053c095aSJohannes Berg 	return 0;
30761da177e4SLinus Torvalds }
30771da177e4SLinus Torvalds 
3078214e005bSDavid S. Miller static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
30791da177e4SLinus Torvalds {
3080fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
30811da177e4SLinus Torvalds 	struct sk_buff *skb;
30822fc5f83bSGustavo A. R. Silva 	int err;
30831da177e4SLinus Torvalds 
30847deb2264SThomas Graf 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
30851da177e4SLinus Torvalds 	if (skb == NULL)
30861da177e4SLinus Torvalds 		return -ENOMEM;
30871da177e4SLinus Torvalds 
30882fc5f83bSGustavo A. R. Silva 	err = build_polexpire(skb, xp, dir, c);
30892fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
30901da177e4SLinus Torvalds 
309121ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
30921da177e4SLinus Torvalds }
30931da177e4SLinus Torvalds 
3094214e005bSDavid S. Miller static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
309526b15dadSJamal Hadi Salim {
3096a1b831f2SAlexey Dobriyan 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3097fc34acd3SAlexey Dobriyan 	struct net *net = xp_net(xp);
309826b15dadSJamal Hadi Salim 	struct xfrm_userpolicy_info *p;
30990603eac0SHerbert Xu 	struct xfrm_userpolicy_id *id;
310026b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
310126b15dadSJamal Hadi Salim 	struct sk_buff *skb;
3102a1b831f2SAlexey Dobriyan 	unsigned int headlen;
3103a1b831f2SAlexey Dobriyan 	int err;
31040603eac0SHerbert Xu 
31050603eac0SHerbert Xu 	headlen = sizeof(*p);
31060603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
31077deb2264SThomas Graf 		len += nla_total_size(headlen);
31080603eac0SHerbert Xu 		headlen = sizeof(*id);
31090603eac0SHerbert Xu 	}
3110cfbfd45aSThomas Graf 	len += userpolicy_type_attrsize();
3111295fae56SJamal Hadi Salim 	len += nla_total_size(sizeof(struct xfrm_mark));
31127deb2264SThomas Graf 	len += NLMSG_ALIGN(headlen);
311326b15dadSJamal Hadi Salim 
31147deb2264SThomas Graf 	skb = nlmsg_new(len, GFP_ATOMIC);
311526b15dadSJamal Hadi Salim 	if (skb == NULL)
311626b15dadSJamal Hadi Salim 		return -ENOMEM;
311726b15dadSJamal Hadi Salim 
311815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
31191d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
312079b8b7f4SThomas Graf 	if (nlh == NULL)
31211d1e34ddSDavid S. Miller 		goto out_free_skb;
312226b15dadSJamal Hadi Salim 
31237b67c857SThomas Graf 	p = nlmsg_data(nlh);
31240603eac0SHerbert Xu 	if (c->event == XFRM_MSG_DELPOLICY) {
3125c0144beaSThomas Graf 		struct nlattr *attr;
3126c0144beaSThomas Graf 
31277b67c857SThomas Graf 		id = nlmsg_data(nlh);
31280603eac0SHerbert Xu 		memset(id, 0, sizeof(*id));
31290603eac0SHerbert Xu 		id->dir = dir;
31300603eac0SHerbert Xu 		if (c->data.byid)
31310603eac0SHerbert Xu 			id->index = xp->index;
31320603eac0SHerbert Xu 		else
31330603eac0SHerbert Xu 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
31340603eac0SHerbert Xu 
3135c0144beaSThomas Graf 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
31361d1e34ddSDavid S. Miller 		err = -EMSGSIZE;
3137c0144beaSThomas Graf 		if (attr == NULL)
31381d1e34ddSDavid S. Miller 			goto out_free_skb;
3139c0144beaSThomas Graf 
3140c0144beaSThomas Graf 		p = nla_data(attr);
31410603eac0SHerbert Xu 	}
314226b15dadSJamal Hadi Salim 
314326b15dadSJamal Hadi Salim 	copy_to_user_policy(xp, p, dir);
31441d1e34ddSDavid S. Miller 	err = copy_to_user_tmpl(xp, skb);
31451d1e34ddSDavid S. Miller 	if (!err)
31461d1e34ddSDavid S. Miller 		err = copy_to_user_policy_type(xp->type, skb);
31471d1e34ddSDavid S. Miller 	if (!err)
31481d1e34ddSDavid S. Miller 		err = xfrm_mark_put(skb, &xp->mark);
31497e652640SSteffen Klassert 	if (!err)
31507e652640SSteffen Klassert 		err = xfrm_if_id_put(skb, xp->if_id);
31511d1e34ddSDavid S. Miller 	if (err)
31521d1e34ddSDavid S. Miller 		goto out_free_skb;
3153295fae56SJamal Hadi Salim 
31549825069dSThomas Graf 	nlmsg_end(skb, nlh);
315526b15dadSJamal Hadi Salim 
315621ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
315726b15dadSJamal Hadi Salim 
31581d1e34ddSDavid S. Miller out_free_skb:
315926b15dadSJamal Hadi Salim 	kfree_skb(skb);
31601d1e34ddSDavid S. Miller 	return err;
316126b15dadSJamal Hadi Salim }
316226b15dadSJamal Hadi Salim 
3163214e005bSDavid S. Miller static int xfrm_notify_policy_flush(const struct km_event *c)
316426b15dadSJamal Hadi Salim {
31657067802eSAlexey Dobriyan 	struct net *net = c->net;
316626b15dadSJamal Hadi Salim 	struct nlmsghdr *nlh;
316726b15dadSJamal Hadi Salim 	struct sk_buff *skb;
31681d1e34ddSDavid S. Miller 	int err;
316926b15dadSJamal Hadi Salim 
31707deb2264SThomas Graf 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
317126b15dadSJamal Hadi Salim 	if (skb == NULL)
317226b15dadSJamal Hadi Salim 		return -ENOMEM;
317326b15dadSJamal Hadi Salim 
317415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
31751d1e34ddSDavid S. Miller 	err = -EMSGSIZE;
317679b8b7f4SThomas Graf 	if (nlh == NULL)
31771d1e34ddSDavid S. Miller 		goto out_free_skb;
31781d1e34ddSDavid S. Miller 	err = copy_to_user_policy_type(c->data.type, skb);
31791d1e34ddSDavid S. Miller 	if (err)
31801d1e34ddSDavid S. Miller 		goto out_free_skb;
318126b15dadSJamal Hadi Salim 
31829825069dSThomas Graf 	nlmsg_end(skb, nlh);
318326b15dadSJamal Hadi Salim 
318421ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
318526b15dadSJamal Hadi Salim 
31861d1e34ddSDavid S. Miller out_free_skb:
318726b15dadSJamal Hadi Salim 	kfree_skb(skb);
31881d1e34ddSDavid S. Miller 	return err;
318926b15dadSJamal Hadi Salim }
319026b15dadSJamal Hadi Salim 
3191214e005bSDavid S. Miller static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
319226b15dadSJamal Hadi Salim {
319326b15dadSJamal Hadi Salim 
319426b15dadSJamal Hadi Salim 	switch (c->event) {
3195f60f6b8fSHerbert Xu 	case XFRM_MSG_NEWPOLICY:
3196f60f6b8fSHerbert Xu 	case XFRM_MSG_UPDPOLICY:
3197f60f6b8fSHerbert Xu 	case XFRM_MSG_DELPOLICY:
319826b15dadSJamal Hadi Salim 		return xfrm_notify_policy(xp, dir, c);
3199f60f6b8fSHerbert Xu 	case XFRM_MSG_FLUSHPOLICY:
320026b15dadSJamal Hadi Salim 		return xfrm_notify_policy_flush(c);
3201f60f6b8fSHerbert Xu 	case XFRM_MSG_POLEXPIRE:
320226b15dadSJamal Hadi Salim 		return xfrm_exp_policy_notify(xp, dir, c);
320326b15dadSJamal Hadi Salim 	default:
320462db5cfdSstephen hemminger 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
320562db5cfdSstephen hemminger 		       c->event);
320626b15dadSJamal Hadi Salim 	}
320726b15dadSJamal Hadi Salim 
320826b15dadSJamal Hadi Salim 	return 0;
320926b15dadSJamal Hadi Salim 
321026b15dadSJamal Hadi Salim }
321126b15dadSJamal Hadi Salim 
3212a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_report_msgsize(void)
32137deb2264SThomas Graf {
32147deb2264SThomas Graf 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
32157deb2264SThomas Graf }
32167deb2264SThomas Graf 
321797a64b45SMasahide NAKAMURA static int build_report(struct sk_buff *skb, u8 proto,
321897a64b45SMasahide NAKAMURA 			struct xfrm_selector *sel, xfrm_address_t *addr)
321997a64b45SMasahide NAKAMURA {
322097a64b45SMasahide NAKAMURA 	struct xfrm_user_report *ur;
322197a64b45SMasahide NAKAMURA 	struct nlmsghdr *nlh;
322297a64b45SMasahide NAKAMURA 
322379b8b7f4SThomas Graf 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
322479b8b7f4SThomas Graf 	if (nlh == NULL)
322579b8b7f4SThomas Graf 		return -EMSGSIZE;
322697a64b45SMasahide NAKAMURA 
32277b67c857SThomas Graf 	ur = nlmsg_data(nlh);
322897a64b45SMasahide NAKAMURA 	ur->proto = proto;
322997a64b45SMasahide NAKAMURA 	memcpy(&ur->sel, sel, sizeof(ur->sel));
323097a64b45SMasahide NAKAMURA 
32311d1e34ddSDavid S. Miller 	if (addr) {
32321d1e34ddSDavid S. Miller 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
32331d1e34ddSDavid S. Miller 		if (err) {
32349825069dSThomas Graf 			nlmsg_cancel(skb, nlh);
32351d1e34ddSDavid S. Miller 			return err;
32361d1e34ddSDavid S. Miller 		}
32371d1e34ddSDavid S. Miller 	}
3238053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3239053c095aSJohannes Berg 	return 0;
324097a64b45SMasahide NAKAMURA }
324197a64b45SMasahide NAKAMURA 
3242db983c11SAlexey Dobriyan static int xfrm_send_report(struct net *net, u8 proto,
3243db983c11SAlexey Dobriyan 			    struct xfrm_selector *sel, xfrm_address_t *addr)
324497a64b45SMasahide NAKAMURA {
324597a64b45SMasahide NAKAMURA 	struct sk_buff *skb;
32462fc5f83bSGustavo A. R. Silva 	int err;
324797a64b45SMasahide NAKAMURA 
32487deb2264SThomas Graf 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
324997a64b45SMasahide NAKAMURA 	if (skb == NULL)
325097a64b45SMasahide NAKAMURA 		return -ENOMEM;
325197a64b45SMasahide NAKAMURA 
32522fc5f83bSGustavo A. R. Silva 	err = build_report(skb, proto, sel, addr);
32532fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
325497a64b45SMasahide NAKAMURA 
325521ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
325697a64b45SMasahide NAKAMURA }
325797a64b45SMasahide NAKAMURA 
3258a1b831f2SAlexey Dobriyan static inline unsigned int xfrm_mapping_msgsize(void)
32593a2dfbe8SMartin Willi {
32603a2dfbe8SMartin Willi 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
32613a2dfbe8SMartin Willi }
32623a2dfbe8SMartin Willi 
32633a2dfbe8SMartin Willi static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
32643a2dfbe8SMartin Willi 			 xfrm_address_t *new_saddr, __be16 new_sport)
32653a2dfbe8SMartin Willi {
32663a2dfbe8SMartin Willi 	struct xfrm_user_mapping *um;
32673a2dfbe8SMartin Willi 	struct nlmsghdr *nlh;
32683a2dfbe8SMartin Willi 
32693a2dfbe8SMartin Willi 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
32703a2dfbe8SMartin Willi 	if (nlh == NULL)
32713a2dfbe8SMartin Willi 		return -EMSGSIZE;
32723a2dfbe8SMartin Willi 
32733a2dfbe8SMartin Willi 	um = nlmsg_data(nlh);
32743a2dfbe8SMartin Willi 
32753a2dfbe8SMartin Willi 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
32763a2dfbe8SMartin Willi 	um->id.spi = x->id.spi;
32773a2dfbe8SMartin Willi 	um->id.family = x->props.family;
32783a2dfbe8SMartin Willi 	um->id.proto = x->id.proto;
32793a2dfbe8SMartin Willi 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
32803a2dfbe8SMartin Willi 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
32813a2dfbe8SMartin Willi 	um->new_sport = new_sport;
32823a2dfbe8SMartin Willi 	um->old_sport = x->encap->encap_sport;
32833a2dfbe8SMartin Willi 	um->reqid = x->props.reqid;
32843a2dfbe8SMartin Willi 
3285053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3286053c095aSJohannes Berg 	return 0;
32873a2dfbe8SMartin Willi }
32883a2dfbe8SMartin Willi 
32893a2dfbe8SMartin Willi static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
32903a2dfbe8SMartin Willi 			     __be16 sport)
32913a2dfbe8SMartin Willi {
3292a6483b79SAlexey Dobriyan 	struct net *net = xs_net(x);
32933a2dfbe8SMartin Willi 	struct sk_buff *skb;
32942fc5f83bSGustavo A. R. Silva 	int err;
32953a2dfbe8SMartin Willi 
32963a2dfbe8SMartin Willi 	if (x->id.proto != IPPROTO_ESP)
32973a2dfbe8SMartin Willi 		return -EINVAL;
32983a2dfbe8SMartin Willi 
32993a2dfbe8SMartin Willi 	if (!x->encap)
33003a2dfbe8SMartin Willi 		return -EINVAL;
33013a2dfbe8SMartin Willi 
33023a2dfbe8SMartin Willi 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
33033a2dfbe8SMartin Willi 	if (skb == NULL)
33043a2dfbe8SMartin Willi 		return -ENOMEM;
33053a2dfbe8SMartin Willi 
33062fc5f83bSGustavo A. R. Silva 	err = build_mapping(skb, x, ipaddr, sport);
33072fc5f83bSGustavo A. R. Silva 	BUG_ON(err < 0);
33083a2dfbe8SMartin Willi 
330921ee543eSMichal Kubecek 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
33103a2dfbe8SMartin Willi }
33113a2dfbe8SMartin Willi 
33120f24558eSHoria Geanta static bool xfrm_is_alive(const struct km_event *c)
33130f24558eSHoria Geanta {
33140f24558eSHoria Geanta 	return (bool)xfrm_acquire_is_on(c->net);
33150f24558eSHoria Geanta }
33160f24558eSHoria Geanta 
33171da177e4SLinus Torvalds static struct xfrm_mgr netlink_mgr = {
33181da177e4SLinus Torvalds 	.notify		= xfrm_send_state_notify,
33191da177e4SLinus Torvalds 	.acquire	= xfrm_send_acquire,
33201da177e4SLinus Torvalds 	.compile_policy	= xfrm_compile_policy,
33211da177e4SLinus Torvalds 	.notify_policy	= xfrm_send_policy_notify,
332297a64b45SMasahide NAKAMURA 	.report		= xfrm_send_report,
33235c79de6eSShinta Sugimoto 	.migrate	= xfrm_send_migrate,
33243a2dfbe8SMartin Willi 	.new_mapping	= xfrm_send_mapping,
33250f24558eSHoria Geanta 	.is_alive	= xfrm_is_alive,
33261da177e4SLinus Torvalds };
33271da177e4SLinus Torvalds 
3328a6483b79SAlexey Dobriyan static int __net_init xfrm_user_net_init(struct net *net)
33291da177e4SLinus Torvalds {
3330be33690dSPatrick McHardy 	struct sock *nlsk;
3331a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
3332a31f2d17SPablo Neira Ayuso 		.groups	= XFRMNLGRP_MAX,
3333a31f2d17SPablo Neira Ayuso 		.input	= xfrm_netlink_rcv,
3334a31f2d17SPablo Neira Ayuso 	};
3335be33690dSPatrick McHardy 
33369f00d977SPablo Neira Ayuso 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3337be33690dSPatrick McHardy 	if (nlsk == NULL)
33381da177e4SLinus Torvalds 		return -ENOMEM;
3339d79d792eSEric W. Biederman 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3340cf778b00SEric Dumazet 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
33411da177e4SLinus Torvalds 	return 0;
33421da177e4SLinus Torvalds }
33431da177e4SLinus Torvalds 
3344d79d792eSEric W. Biederman static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3345a6483b79SAlexey Dobriyan {
3346d79d792eSEric W. Biederman 	struct net *net;
3347d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3348a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3349d79d792eSEric W. Biederman 	synchronize_net();
3350d79d792eSEric W. Biederman 	list_for_each_entry(net, net_exit_list, exit_list)
3351d79d792eSEric W. Biederman 		netlink_kernel_release(net->xfrm.nlsk_stash);
3352a6483b79SAlexey Dobriyan }
3353a6483b79SAlexey Dobriyan 
3354a6483b79SAlexey Dobriyan static struct pernet_operations xfrm_user_net_ops = {
3355a6483b79SAlexey Dobriyan 	.init	    = xfrm_user_net_init,
3356d79d792eSEric W. Biederman 	.exit_batch = xfrm_user_net_exit,
3357a6483b79SAlexey Dobriyan };
3358a6483b79SAlexey Dobriyan 
3359a6483b79SAlexey Dobriyan static int __init xfrm_user_init(void)
3360a6483b79SAlexey Dobriyan {
3361a6483b79SAlexey Dobriyan 	int rv;
3362a6483b79SAlexey Dobriyan 
3363a6483b79SAlexey Dobriyan 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3364a6483b79SAlexey Dobriyan 
3365a6483b79SAlexey Dobriyan 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3366a6483b79SAlexey Dobriyan 	if (rv < 0)
3367a6483b79SAlexey Dobriyan 		return rv;
3368a6483b79SAlexey Dobriyan 	rv = xfrm_register_km(&netlink_mgr);
3369a6483b79SAlexey Dobriyan 	if (rv < 0)
3370a6483b79SAlexey Dobriyan 		unregister_pernet_subsys(&xfrm_user_net_ops);
3371a6483b79SAlexey Dobriyan 	return rv;
3372a6483b79SAlexey Dobriyan }
3373a6483b79SAlexey Dobriyan 
33741da177e4SLinus Torvalds static void __exit xfrm_user_exit(void)
33751da177e4SLinus Torvalds {
33761da177e4SLinus Torvalds 	xfrm_unregister_km(&netlink_mgr);
3377a6483b79SAlexey Dobriyan 	unregister_pernet_subsys(&xfrm_user_net_ops);
33781da177e4SLinus Torvalds }
33791da177e4SLinus Torvalds 
33801da177e4SLinus Torvalds module_init(xfrm_user_init);
33811da177e4SLinus Torvalds module_exit(xfrm_user_exit);
33821da177e4SLinus Torvalds MODULE_LICENSE("GPL");
33834fdb3bb7SHarald Welte MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3384