xref: /OK3568_Linux_fs/kernel/net/xfrm/xfrm_compat.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * XFRM compat layer
4*4882a593Smuzhiyun  * Author: Dmitry Safonov <dima@arista.com>
5*4882a593Smuzhiyun  * Based on code and translator idea by: Florian Westphal <fw@strlen.de>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #include <linux/compat.h>
8*4882a593Smuzhiyun #include <linux/xfrm.h>
9*4882a593Smuzhiyun #include <net/xfrm.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun struct compat_xfrm_lifetime_cfg {
12*4882a593Smuzhiyun 	compat_u64 soft_byte_limit, hard_byte_limit;
13*4882a593Smuzhiyun 	compat_u64 soft_packet_limit, hard_packet_limit;
14*4882a593Smuzhiyun 	compat_u64 soft_add_expires_seconds, hard_add_expires_seconds;
15*4882a593Smuzhiyun 	compat_u64 soft_use_expires_seconds, hard_use_expires_seconds;
16*4882a593Smuzhiyun }; /* same size on 32bit, but only 4 byte alignment required */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun struct compat_xfrm_lifetime_cur {
19*4882a593Smuzhiyun 	compat_u64 bytes, packets, add_time, use_time;
20*4882a593Smuzhiyun }; /* same size on 32bit, but only 4 byte alignment required */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun struct compat_xfrm_userpolicy_info {
23*4882a593Smuzhiyun 	struct xfrm_selector sel;
24*4882a593Smuzhiyun 	struct compat_xfrm_lifetime_cfg lft;
25*4882a593Smuzhiyun 	struct compat_xfrm_lifetime_cur curlft;
26*4882a593Smuzhiyun 	__u32 priority, index;
27*4882a593Smuzhiyun 	u8 dir, action, flags, share;
28*4882a593Smuzhiyun 	/* 4 bytes additional padding on 64bit */
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun struct compat_xfrm_usersa_info {
32*4882a593Smuzhiyun 	struct xfrm_selector sel;
33*4882a593Smuzhiyun 	struct xfrm_id id;
34*4882a593Smuzhiyun 	xfrm_address_t saddr;
35*4882a593Smuzhiyun 	struct compat_xfrm_lifetime_cfg lft;
36*4882a593Smuzhiyun 	struct compat_xfrm_lifetime_cur curlft;
37*4882a593Smuzhiyun 	struct xfrm_stats stats;
38*4882a593Smuzhiyun 	__u32 seq, reqid;
39*4882a593Smuzhiyun 	u16 family;
40*4882a593Smuzhiyun 	u8 mode, replay_window, flags;
41*4882a593Smuzhiyun 	/* 4 bytes additional padding on 64bit */
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun struct compat_xfrm_user_acquire {
45*4882a593Smuzhiyun 	struct xfrm_id id;
46*4882a593Smuzhiyun 	xfrm_address_t saddr;
47*4882a593Smuzhiyun 	struct xfrm_selector sel;
48*4882a593Smuzhiyun 	struct compat_xfrm_userpolicy_info policy;
49*4882a593Smuzhiyun 	/* 4 bytes additional padding on 64bit */
50*4882a593Smuzhiyun 	__u32 aalgos, ealgos, calgos, seq;
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun struct compat_xfrm_userspi_info {
54*4882a593Smuzhiyun 	struct compat_xfrm_usersa_info info;
55*4882a593Smuzhiyun 	/* 4 bytes additional padding on 64bit */
56*4882a593Smuzhiyun 	__u32 min, max;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun struct compat_xfrm_user_expire {
60*4882a593Smuzhiyun 	struct compat_xfrm_usersa_info state;
61*4882a593Smuzhiyun 	/* 8 bytes additional padding on 64bit */
62*4882a593Smuzhiyun 	u8 hard;
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun struct compat_xfrm_user_polexpire {
66*4882a593Smuzhiyun 	struct compat_xfrm_userpolicy_info pol;
67*4882a593Smuzhiyun 	/* 8 bytes additional padding on 64bit */
68*4882a593Smuzhiyun 	u8 hard;
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun #define XMSGSIZE(type) sizeof(struct type)
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun static const int compat_msg_min[XFRM_NR_MSGTYPES] = {
74*4882a593Smuzhiyun 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_usersa_info),
75*4882a593Smuzhiyun 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
76*4882a593Smuzhiyun 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
77*4882a593Smuzhiyun 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_userpolicy_info),
78*4882a593Smuzhiyun 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
79*4882a593Smuzhiyun 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
80*4882a593Smuzhiyun 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_userspi_info),
81*4882a593Smuzhiyun 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_user_acquire),
82*4882a593Smuzhiyun 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_user_expire),
83*4882a593Smuzhiyun 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_userpolicy_info),
84*4882a593Smuzhiyun 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_usersa_info),
85*4882a593Smuzhiyun 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_user_polexpire),
86*4882a593Smuzhiyun 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
87*4882a593Smuzhiyun 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
88*4882a593Smuzhiyun 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
89*4882a593Smuzhiyun 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
90*4882a593Smuzhiyun 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
91*4882a593Smuzhiyun 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
92*4882a593Smuzhiyun 	[XFRM_MSG_NEWSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
93*4882a593Smuzhiyun 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
94*4882a593Smuzhiyun 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
95*4882a593Smuzhiyun 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
96*4882a593Smuzhiyun 	[XFRM_MSG_MAPPING     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_mapping)
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun static const struct nla_policy compat_policy[XFRMA_MAX+1] = {
100*4882a593Smuzhiyun 	[XFRMA_SA]		= { .len = XMSGSIZE(compat_xfrm_usersa_info)},
101*4882a593Smuzhiyun 	[XFRMA_POLICY]		= { .len = XMSGSIZE(compat_xfrm_userpolicy_info)},
102*4882a593Smuzhiyun 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
103*4882a593Smuzhiyun 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
104*4882a593Smuzhiyun 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
105*4882a593Smuzhiyun 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
106*4882a593Smuzhiyun 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
107*4882a593Smuzhiyun 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
108*4882a593Smuzhiyun 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
109*4882a593Smuzhiyun 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
110*4882a593Smuzhiyun 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
111*4882a593Smuzhiyun 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
112*4882a593Smuzhiyun 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
113*4882a593Smuzhiyun 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
114*4882a593Smuzhiyun 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
115*4882a593Smuzhiyun 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
116*4882a593Smuzhiyun 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
117*4882a593Smuzhiyun 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
118*4882a593Smuzhiyun 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
119*4882a593Smuzhiyun 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
120*4882a593Smuzhiyun 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
121*4882a593Smuzhiyun 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
122*4882a593Smuzhiyun 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
123*4882a593Smuzhiyun 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
124*4882a593Smuzhiyun 	[XFRMA_PROTO]		= { .type = NLA_U8 },
125*4882a593Smuzhiyun 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
126*4882a593Smuzhiyun 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
127*4882a593Smuzhiyun 	[XFRMA_SET_MARK]	= { .type = NLA_U32 },
128*4882a593Smuzhiyun 	[XFRMA_SET_MARK_MASK]	= { .type = NLA_U32 },
129*4882a593Smuzhiyun 	[XFRMA_IF_ID]		= { .type = NLA_U32 },
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun 
xfrm_nlmsg_put_compat(struct sk_buff * skb,const struct nlmsghdr * nlh_src,u16 type)132*4882a593Smuzhiyun static struct nlmsghdr *xfrm_nlmsg_put_compat(struct sk_buff *skb,
133*4882a593Smuzhiyun 			const struct nlmsghdr *nlh_src, u16 type)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	int payload = compat_msg_min[type];
136*4882a593Smuzhiyun 	int src_len = xfrm_msg_min[type];
137*4882a593Smuzhiyun 	struct nlmsghdr *nlh_dst;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/* Compat messages are shorter or equal to native (+padding) */
140*4882a593Smuzhiyun 	if (WARN_ON_ONCE(src_len < payload))
141*4882a593Smuzhiyun 		return ERR_PTR(-EMSGSIZE);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	nlh_dst = nlmsg_put(skb, nlh_src->nlmsg_pid, nlh_src->nlmsg_seq,
144*4882a593Smuzhiyun 			    nlh_src->nlmsg_type, payload, nlh_src->nlmsg_flags);
145*4882a593Smuzhiyun 	if (!nlh_dst)
146*4882a593Smuzhiyun 		return ERR_PTR(-EMSGSIZE);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	memset(nlmsg_data(nlh_dst), 0, payload);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	switch (nlh_src->nlmsg_type) {
151*4882a593Smuzhiyun 	/* Compat message has the same layout as native */
152*4882a593Smuzhiyun 	case XFRM_MSG_DELSA:
153*4882a593Smuzhiyun 	case XFRM_MSG_DELPOLICY:
154*4882a593Smuzhiyun 	case XFRM_MSG_FLUSHSA:
155*4882a593Smuzhiyun 	case XFRM_MSG_FLUSHPOLICY:
156*4882a593Smuzhiyun 	case XFRM_MSG_NEWAE:
157*4882a593Smuzhiyun 	case XFRM_MSG_REPORT:
158*4882a593Smuzhiyun 	case XFRM_MSG_MIGRATE:
159*4882a593Smuzhiyun 	case XFRM_MSG_NEWSADINFO:
160*4882a593Smuzhiyun 	case XFRM_MSG_NEWSPDINFO:
161*4882a593Smuzhiyun 	case XFRM_MSG_MAPPING:
162*4882a593Smuzhiyun 		WARN_ON_ONCE(src_len != payload);
163*4882a593Smuzhiyun 		memcpy(nlmsg_data(nlh_dst), nlmsg_data(nlh_src), src_len);
164*4882a593Smuzhiyun 		break;
165*4882a593Smuzhiyun 	/* 4 byte alignment for trailing u64 on native, but not on compat */
166*4882a593Smuzhiyun 	case XFRM_MSG_NEWSA:
167*4882a593Smuzhiyun 	case XFRM_MSG_NEWPOLICY:
168*4882a593Smuzhiyun 	case XFRM_MSG_UPDSA:
169*4882a593Smuzhiyun 	case XFRM_MSG_UPDPOLICY:
170*4882a593Smuzhiyun 		WARN_ON_ONCE(src_len != payload + 4);
171*4882a593Smuzhiyun 		memcpy(nlmsg_data(nlh_dst), nlmsg_data(nlh_src), payload);
172*4882a593Smuzhiyun 		break;
173*4882a593Smuzhiyun 	case XFRM_MSG_EXPIRE: {
174*4882a593Smuzhiyun 		const struct xfrm_user_expire *src_ue  = nlmsg_data(nlh_src);
175*4882a593Smuzhiyun 		struct compat_xfrm_user_expire *dst_ue = nlmsg_data(nlh_dst);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 		/* compat_xfrm_user_expire has 4-byte smaller state */
178*4882a593Smuzhiyun 		memcpy(dst_ue, src_ue, sizeof(dst_ue->state));
179*4882a593Smuzhiyun 		dst_ue->hard = src_ue->hard;
180*4882a593Smuzhiyun 		break;
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 	case XFRM_MSG_ACQUIRE: {
183*4882a593Smuzhiyun 		const struct xfrm_user_acquire *src_ua  = nlmsg_data(nlh_src);
184*4882a593Smuzhiyun 		struct compat_xfrm_user_acquire *dst_ua = nlmsg_data(nlh_dst);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 		memcpy(dst_ua, src_ua, offsetof(struct compat_xfrm_user_acquire, aalgos));
187*4882a593Smuzhiyun 		dst_ua->aalgos = src_ua->aalgos;
188*4882a593Smuzhiyun 		dst_ua->ealgos = src_ua->ealgos;
189*4882a593Smuzhiyun 		dst_ua->calgos = src_ua->calgos;
190*4882a593Smuzhiyun 		dst_ua->seq    = src_ua->seq;
191*4882a593Smuzhiyun 		break;
192*4882a593Smuzhiyun 	}
193*4882a593Smuzhiyun 	case XFRM_MSG_POLEXPIRE: {
194*4882a593Smuzhiyun 		const struct xfrm_user_polexpire *src_upe  = nlmsg_data(nlh_src);
195*4882a593Smuzhiyun 		struct compat_xfrm_user_polexpire *dst_upe = nlmsg_data(nlh_dst);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 		/* compat_xfrm_user_polexpire has 4-byte smaller state */
198*4882a593Smuzhiyun 		memcpy(dst_upe, src_upe, sizeof(dst_upe->pol));
199*4882a593Smuzhiyun 		dst_upe->hard = src_upe->hard;
200*4882a593Smuzhiyun 		break;
201*4882a593Smuzhiyun 	}
202*4882a593Smuzhiyun 	case XFRM_MSG_ALLOCSPI: {
203*4882a593Smuzhiyun 		const struct xfrm_userspi_info *src_usi = nlmsg_data(nlh_src);
204*4882a593Smuzhiyun 		struct compat_xfrm_userspi_info *dst_usi = nlmsg_data(nlh_dst);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 		/* compat_xfrm_user_polexpire has 4-byte smaller state */
207*4882a593Smuzhiyun 		memcpy(dst_usi, src_usi, sizeof(src_usi->info));
208*4882a593Smuzhiyun 		dst_usi->min = src_usi->min;
209*4882a593Smuzhiyun 		dst_usi->max = src_usi->max;
210*4882a593Smuzhiyun 		break;
211*4882a593Smuzhiyun 	}
212*4882a593Smuzhiyun 	/* Not being sent by kernel */
213*4882a593Smuzhiyun 	case XFRM_MSG_GETSA:
214*4882a593Smuzhiyun 	case XFRM_MSG_GETPOLICY:
215*4882a593Smuzhiyun 	case XFRM_MSG_GETAE:
216*4882a593Smuzhiyun 	case XFRM_MSG_GETSADINFO:
217*4882a593Smuzhiyun 	case XFRM_MSG_GETSPDINFO:
218*4882a593Smuzhiyun 	default:
219*4882a593Smuzhiyun 		pr_warn_once("unsupported nlmsg_type %d\n", nlh_src->nlmsg_type);
220*4882a593Smuzhiyun 		return ERR_PTR(-EOPNOTSUPP);
221*4882a593Smuzhiyun 	}
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	return nlh_dst;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
xfrm_nla_cpy(struct sk_buff * dst,const struct nlattr * src,int len)226*4882a593Smuzhiyun static int xfrm_nla_cpy(struct sk_buff *dst, const struct nlattr *src, int len)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	return nla_put(dst, src->nla_type, len, nla_data(src));
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun 
xfrm_xlate64_attr(struct sk_buff * dst,const struct nlattr * src)231*4882a593Smuzhiyun static int xfrm_xlate64_attr(struct sk_buff *dst, const struct nlattr *src)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun 	switch (src->nla_type) {
234*4882a593Smuzhiyun 	case XFRMA_PAD:
235*4882a593Smuzhiyun 		/* Ignore */
236*4882a593Smuzhiyun 		return 0;
237*4882a593Smuzhiyun 	case XFRMA_UNSPEC:
238*4882a593Smuzhiyun 	case XFRMA_ALG_AUTH:
239*4882a593Smuzhiyun 	case XFRMA_ALG_CRYPT:
240*4882a593Smuzhiyun 	case XFRMA_ALG_COMP:
241*4882a593Smuzhiyun 	case XFRMA_ENCAP:
242*4882a593Smuzhiyun 	case XFRMA_TMPL:
243*4882a593Smuzhiyun 		return xfrm_nla_cpy(dst, src, nla_len(src));
244*4882a593Smuzhiyun 	case XFRMA_SA:
245*4882a593Smuzhiyun 		return xfrm_nla_cpy(dst, src, XMSGSIZE(compat_xfrm_usersa_info));
246*4882a593Smuzhiyun 	case XFRMA_POLICY:
247*4882a593Smuzhiyun 		return xfrm_nla_cpy(dst, src, XMSGSIZE(compat_xfrm_userpolicy_info));
248*4882a593Smuzhiyun 	case XFRMA_SEC_CTX:
249*4882a593Smuzhiyun 		return xfrm_nla_cpy(dst, src, nla_len(src));
250*4882a593Smuzhiyun 	case XFRMA_LTIME_VAL:
251*4882a593Smuzhiyun 		return nla_put_64bit(dst, src->nla_type, nla_len(src),
252*4882a593Smuzhiyun 			nla_data(src), XFRMA_PAD);
253*4882a593Smuzhiyun 	case XFRMA_REPLAY_VAL:
254*4882a593Smuzhiyun 	case XFRMA_REPLAY_THRESH:
255*4882a593Smuzhiyun 	case XFRMA_ETIMER_THRESH:
256*4882a593Smuzhiyun 	case XFRMA_SRCADDR:
257*4882a593Smuzhiyun 	case XFRMA_COADDR:
258*4882a593Smuzhiyun 		return xfrm_nla_cpy(dst, src, nla_len(src));
259*4882a593Smuzhiyun 	case XFRMA_LASTUSED:
260*4882a593Smuzhiyun 		return nla_put_64bit(dst, src->nla_type, nla_len(src),
261*4882a593Smuzhiyun 			nla_data(src), XFRMA_PAD);
262*4882a593Smuzhiyun 	case XFRMA_POLICY_TYPE:
263*4882a593Smuzhiyun 	case XFRMA_MIGRATE:
264*4882a593Smuzhiyun 	case XFRMA_ALG_AEAD:
265*4882a593Smuzhiyun 	case XFRMA_KMADDRESS:
266*4882a593Smuzhiyun 	case XFRMA_ALG_AUTH_TRUNC:
267*4882a593Smuzhiyun 	case XFRMA_MARK:
268*4882a593Smuzhiyun 	case XFRMA_TFCPAD:
269*4882a593Smuzhiyun 	case XFRMA_REPLAY_ESN_VAL:
270*4882a593Smuzhiyun 	case XFRMA_SA_EXTRA_FLAGS:
271*4882a593Smuzhiyun 	case XFRMA_PROTO:
272*4882a593Smuzhiyun 	case XFRMA_ADDRESS_FILTER:
273*4882a593Smuzhiyun 	case XFRMA_OFFLOAD_DEV:
274*4882a593Smuzhiyun 	case XFRMA_SET_MARK:
275*4882a593Smuzhiyun 	case XFRMA_SET_MARK_MASK:
276*4882a593Smuzhiyun 	case XFRMA_IF_ID:
277*4882a593Smuzhiyun 		return xfrm_nla_cpy(dst, src, nla_len(src));
278*4882a593Smuzhiyun 	default:
279*4882a593Smuzhiyun 		BUILD_BUG_ON(XFRMA_MAX != XFRMA_IF_ID);
280*4882a593Smuzhiyun 		pr_warn_once("unsupported nla_type %d\n", src->nla_type);
281*4882a593Smuzhiyun 		return -EOPNOTSUPP;
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun /* Take kernel-built (64bit layout) and create 32bit layout for userspace */
xfrm_xlate64(struct sk_buff * dst,const struct nlmsghdr * nlh_src)286*4882a593Smuzhiyun static int xfrm_xlate64(struct sk_buff *dst, const struct nlmsghdr *nlh_src)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun 	u16 type = nlh_src->nlmsg_type - XFRM_MSG_BASE;
289*4882a593Smuzhiyun 	const struct nlattr *nla, *attrs;
290*4882a593Smuzhiyun 	struct nlmsghdr *nlh_dst;
291*4882a593Smuzhiyun 	int len, remaining;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	nlh_dst = xfrm_nlmsg_put_compat(dst, nlh_src, type);
294*4882a593Smuzhiyun 	if (IS_ERR(nlh_dst))
295*4882a593Smuzhiyun 		return PTR_ERR(nlh_dst);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	attrs = nlmsg_attrdata(nlh_src, xfrm_msg_min[type]);
298*4882a593Smuzhiyun 	len = nlmsg_attrlen(nlh_src, xfrm_msg_min[type]);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	nla_for_each_attr(nla, attrs, len, remaining) {
301*4882a593Smuzhiyun 		int err;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 		switch (type) {
304*4882a593Smuzhiyun 		case XFRM_MSG_NEWSPDINFO:
305*4882a593Smuzhiyun 			err = xfrm_nla_cpy(dst, nla, nla_len(nla));
306*4882a593Smuzhiyun 			break;
307*4882a593Smuzhiyun 		default:
308*4882a593Smuzhiyun 			err = xfrm_xlate64_attr(dst, nla);
309*4882a593Smuzhiyun 			break;
310*4882a593Smuzhiyun 		}
311*4882a593Smuzhiyun 		if (err)
312*4882a593Smuzhiyun 			return err;
313*4882a593Smuzhiyun 	}
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	nlmsg_end(dst, nlh_dst);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	return 0;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun 
xfrm_alloc_compat(struct sk_buff * skb,const struct nlmsghdr * nlh_src)320*4882a593Smuzhiyun static int xfrm_alloc_compat(struct sk_buff *skb, const struct nlmsghdr *nlh_src)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	u16 type = nlh_src->nlmsg_type - XFRM_MSG_BASE;
323*4882a593Smuzhiyun 	struct sk_buff *new = NULL;
324*4882a593Smuzhiyun 	int err;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	if (type >= ARRAY_SIZE(xfrm_msg_min)) {
327*4882a593Smuzhiyun 		pr_warn_once("unsupported nlmsg_type %d\n", nlh_src->nlmsg_type);
328*4882a593Smuzhiyun 		return -EOPNOTSUPP;
329*4882a593Smuzhiyun 	}
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	if (skb_shinfo(skb)->frag_list == NULL) {
332*4882a593Smuzhiyun 		new = alloc_skb(skb->len + skb_tailroom(skb), GFP_ATOMIC);
333*4882a593Smuzhiyun 		if (!new)
334*4882a593Smuzhiyun 			return -ENOMEM;
335*4882a593Smuzhiyun 		skb_shinfo(skb)->frag_list = new;
336*4882a593Smuzhiyun 	}
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	err = xfrm_xlate64(skb_shinfo(skb)->frag_list, nlh_src);
339*4882a593Smuzhiyun 	if (err) {
340*4882a593Smuzhiyun 		if (new) {
341*4882a593Smuzhiyun 			kfree_skb(new);
342*4882a593Smuzhiyun 			skb_shinfo(skb)->frag_list = NULL;
343*4882a593Smuzhiyun 		}
344*4882a593Smuzhiyun 		return err;
345*4882a593Smuzhiyun 	}
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	return 0;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun /* Calculates len of translated 64-bit message. */
xfrm_user_rcv_calculate_len64(const struct nlmsghdr * src,struct nlattr * attrs[XFRMA_MAX+1],int maxtype)351*4882a593Smuzhiyun static size_t xfrm_user_rcv_calculate_len64(const struct nlmsghdr *src,
352*4882a593Smuzhiyun 					    struct nlattr *attrs[XFRMA_MAX + 1],
353*4882a593Smuzhiyun 					    int maxtype)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	size_t len = nlmsg_len(src);
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	switch (src->nlmsg_type) {
358*4882a593Smuzhiyun 	case XFRM_MSG_NEWSA:
359*4882a593Smuzhiyun 	case XFRM_MSG_NEWPOLICY:
360*4882a593Smuzhiyun 	case XFRM_MSG_ALLOCSPI:
361*4882a593Smuzhiyun 	case XFRM_MSG_ACQUIRE:
362*4882a593Smuzhiyun 	case XFRM_MSG_UPDPOLICY:
363*4882a593Smuzhiyun 	case XFRM_MSG_UPDSA:
364*4882a593Smuzhiyun 		len += 4;
365*4882a593Smuzhiyun 		break;
366*4882a593Smuzhiyun 	case XFRM_MSG_EXPIRE:
367*4882a593Smuzhiyun 	case XFRM_MSG_POLEXPIRE:
368*4882a593Smuzhiyun 		len += 8;
369*4882a593Smuzhiyun 		break;
370*4882a593Smuzhiyun 	case XFRM_MSG_NEWSPDINFO:
371*4882a593Smuzhiyun 		/* attirbutes are xfrm_spdattr_type_t, not xfrm_attr_type_t */
372*4882a593Smuzhiyun 		return len;
373*4882a593Smuzhiyun 	default:
374*4882a593Smuzhiyun 		break;
375*4882a593Smuzhiyun 	}
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	/* Unexpected for anything, but XFRM_MSG_NEWSPDINFO, please
378*4882a593Smuzhiyun 	 * correct both 64=>32-bit and 32=>64-bit translators to copy
379*4882a593Smuzhiyun 	 * new attributes.
380*4882a593Smuzhiyun 	 */
381*4882a593Smuzhiyun 	if (WARN_ON_ONCE(maxtype))
382*4882a593Smuzhiyun 		return len;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	if (attrs[XFRMA_SA])
385*4882a593Smuzhiyun 		len += 4;
386*4882a593Smuzhiyun 	if (attrs[XFRMA_POLICY])
387*4882a593Smuzhiyun 		len += 4;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	/* XXX: some attrs may need to be realigned
390*4882a593Smuzhiyun 	 * if !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
391*4882a593Smuzhiyun 	 */
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	return len;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun 
xfrm_attr_cpy32(void * dst,size_t * pos,const struct nlattr * src,size_t size,int copy_len,int payload)396*4882a593Smuzhiyun static int xfrm_attr_cpy32(void *dst, size_t *pos, const struct nlattr *src,
397*4882a593Smuzhiyun 			   size_t size, int copy_len, int payload)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun 	struct nlmsghdr *nlmsg = dst;
400*4882a593Smuzhiyun 	struct nlattr *nla;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	/* xfrm_user_rcv_msg_compat() relies on fact that 32-bit messages
403*4882a593Smuzhiyun 	 * have the same len or shorted than 64-bit ones.
404*4882a593Smuzhiyun 	 * 32-bit translation that is bigger than 64-bit original is unexpected.
405*4882a593Smuzhiyun 	 */
406*4882a593Smuzhiyun 	if (WARN_ON_ONCE(copy_len > payload))
407*4882a593Smuzhiyun 		copy_len = payload;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	if (size - *pos < nla_attr_size(payload))
410*4882a593Smuzhiyun 		return -ENOBUFS;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	nla = dst + *pos;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	memcpy(nla, src, nla_attr_size(copy_len));
415*4882a593Smuzhiyun 	nla->nla_len = nla_attr_size(payload);
416*4882a593Smuzhiyun 	*pos += nla_attr_size(copy_len);
417*4882a593Smuzhiyun 	nlmsg->nlmsg_len += nla->nla_len;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	memset(dst + *pos, 0, payload - copy_len);
420*4882a593Smuzhiyun 	*pos += payload - copy_len;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	return 0;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
xfrm_xlate32_attr(void * dst,const struct nlattr * nla,size_t * pos,size_t size,struct netlink_ext_ack * extack)425*4882a593Smuzhiyun static int xfrm_xlate32_attr(void *dst, const struct nlattr *nla,
426*4882a593Smuzhiyun 			     size_t *pos, size_t size,
427*4882a593Smuzhiyun 			     struct netlink_ext_ack *extack)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun 	int type = nla_type(nla);
430*4882a593Smuzhiyun 	u16 pol_len32, pol_len64;
431*4882a593Smuzhiyun 	int err;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	if (type > XFRMA_MAX) {
434*4882a593Smuzhiyun 		BUILD_BUG_ON(XFRMA_MAX != XFRMA_IF_ID);
435*4882a593Smuzhiyun 		NL_SET_ERR_MSG(extack, "Bad attribute");
436*4882a593Smuzhiyun 		return -EOPNOTSUPP;
437*4882a593Smuzhiyun 	}
438*4882a593Smuzhiyun 	if (nla_len(nla) < compat_policy[type].len) {
439*4882a593Smuzhiyun 		NL_SET_ERR_MSG(extack, "Attribute bad length");
440*4882a593Smuzhiyun 		return -EOPNOTSUPP;
441*4882a593Smuzhiyun 	}
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	pol_len32 = compat_policy[type].len;
444*4882a593Smuzhiyun 	pol_len64 = xfrma_policy[type].len;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	/* XFRMA_SA and XFRMA_POLICY - need to know how-to translate */
447*4882a593Smuzhiyun 	if (pol_len32 != pol_len64) {
448*4882a593Smuzhiyun 		if (nla_len(nla) != compat_policy[type].len) {
449*4882a593Smuzhiyun 			NL_SET_ERR_MSG(extack, "Attribute bad length");
450*4882a593Smuzhiyun 			return -EOPNOTSUPP;
451*4882a593Smuzhiyun 		}
452*4882a593Smuzhiyun 		err = xfrm_attr_cpy32(dst, pos, nla, size, pol_len32, pol_len64);
453*4882a593Smuzhiyun 		if (err)
454*4882a593Smuzhiyun 			return err;
455*4882a593Smuzhiyun 	}
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	return xfrm_attr_cpy32(dst, pos, nla, size, nla_len(nla), nla_len(nla));
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun 
xfrm_xlate32(struct nlmsghdr * dst,const struct nlmsghdr * src,struct nlattr * attrs[XFRMA_MAX+1],size_t size,u8 type,int maxtype,struct netlink_ext_ack * extack)460*4882a593Smuzhiyun static int xfrm_xlate32(struct nlmsghdr *dst, const struct nlmsghdr *src,
461*4882a593Smuzhiyun 			struct nlattr *attrs[XFRMA_MAX+1],
462*4882a593Smuzhiyun 			size_t size, u8 type, int maxtype,
463*4882a593Smuzhiyun 			struct netlink_ext_ack *extack)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun 	size_t pos;
466*4882a593Smuzhiyun 	int i;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	memcpy(dst, src, NLMSG_HDRLEN);
469*4882a593Smuzhiyun 	dst->nlmsg_len = NLMSG_HDRLEN + xfrm_msg_min[type];
470*4882a593Smuzhiyun 	memset(nlmsg_data(dst), 0, xfrm_msg_min[type]);
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	switch (src->nlmsg_type) {
473*4882a593Smuzhiyun 	/* Compat message has the same layout as native */
474*4882a593Smuzhiyun 	case XFRM_MSG_DELSA:
475*4882a593Smuzhiyun 	case XFRM_MSG_GETSA:
476*4882a593Smuzhiyun 	case XFRM_MSG_DELPOLICY:
477*4882a593Smuzhiyun 	case XFRM_MSG_GETPOLICY:
478*4882a593Smuzhiyun 	case XFRM_MSG_FLUSHSA:
479*4882a593Smuzhiyun 	case XFRM_MSG_FLUSHPOLICY:
480*4882a593Smuzhiyun 	case XFRM_MSG_NEWAE:
481*4882a593Smuzhiyun 	case XFRM_MSG_GETAE:
482*4882a593Smuzhiyun 	case XFRM_MSG_REPORT:
483*4882a593Smuzhiyun 	case XFRM_MSG_MIGRATE:
484*4882a593Smuzhiyun 	case XFRM_MSG_NEWSADINFO:
485*4882a593Smuzhiyun 	case XFRM_MSG_GETSADINFO:
486*4882a593Smuzhiyun 	case XFRM_MSG_NEWSPDINFO:
487*4882a593Smuzhiyun 	case XFRM_MSG_GETSPDINFO:
488*4882a593Smuzhiyun 	case XFRM_MSG_MAPPING:
489*4882a593Smuzhiyun 		memcpy(nlmsg_data(dst), nlmsg_data(src), compat_msg_min[type]);
490*4882a593Smuzhiyun 		break;
491*4882a593Smuzhiyun 	/* 4 byte alignment for trailing u64 on native, but not on compat */
492*4882a593Smuzhiyun 	case XFRM_MSG_NEWSA:
493*4882a593Smuzhiyun 	case XFRM_MSG_NEWPOLICY:
494*4882a593Smuzhiyun 	case XFRM_MSG_UPDSA:
495*4882a593Smuzhiyun 	case XFRM_MSG_UPDPOLICY:
496*4882a593Smuzhiyun 		memcpy(nlmsg_data(dst), nlmsg_data(src), compat_msg_min[type]);
497*4882a593Smuzhiyun 		break;
498*4882a593Smuzhiyun 	case XFRM_MSG_EXPIRE: {
499*4882a593Smuzhiyun 		const struct compat_xfrm_user_expire *src_ue = nlmsg_data(src);
500*4882a593Smuzhiyun 		struct xfrm_user_expire *dst_ue = nlmsg_data(dst);
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 		/* compat_xfrm_user_expire has 4-byte smaller state */
503*4882a593Smuzhiyun 		memcpy(dst_ue, src_ue, sizeof(src_ue->state));
504*4882a593Smuzhiyun 		dst_ue->hard = src_ue->hard;
505*4882a593Smuzhiyun 		break;
506*4882a593Smuzhiyun 	}
507*4882a593Smuzhiyun 	case XFRM_MSG_ACQUIRE: {
508*4882a593Smuzhiyun 		const struct compat_xfrm_user_acquire *src_ua = nlmsg_data(src);
509*4882a593Smuzhiyun 		struct xfrm_user_acquire *dst_ua = nlmsg_data(dst);
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 		memcpy(dst_ua, src_ua, offsetof(struct compat_xfrm_user_acquire, aalgos));
512*4882a593Smuzhiyun 		dst_ua->aalgos = src_ua->aalgos;
513*4882a593Smuzhiyun 		dst_ua->ealgos = src_ua->ealgos;
514*4882a593Smuzhiyun 		dst_ua->calgos = src_ua->calgos;
515*4882a593Smuzhiyun 		dst_ua->seq    = src_ua->seq;
516*4882a593Smuzhiyun 		break;
517*4882a593Smuzhiyun 	}
518*4882a593Smuzhiyun 	case XFRM_MSG_POLEXPIRE: {
519*4882a593Smuzhiyun 		const struct compat_xfrm_user_polexpire *src_upe = nlmsg_data(src);
520*4882a593Smuzhiyun 		struct xfrm_user_polexpire *dst_upe = nlmsg_data(dst);
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 		/* compat_xfrm_user_polexpire has 4-byte smaller state */
523*4882a593Smuzhiyun 		memcpy(dst_upe, src_upe, sizeof(src_upe->pol));
524*4882a593Smuzhiyun 		dst_upe->hard = src_upe->hard;
525*4882a593Smuzhiyun 		break;
526*4882a593Smuzhiyun 	}
527*4882a593Smuzhiyun 	case XFRM_MSG_ALLOCSPI: {
528*4882a593Smuzhiyun 		const struct compat_xfrm_userspi_info *src_usi = nlmsg_data(src);
529*4882a593Smuzhiyun 		struct xfrm_userspi_info *dst_usi = nlmsg_data(dst);
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 		/* compat_xfrm_user_polexpire has 4-byte smaller state */
532*4882a593Smuzhiyun 		memcpy(dst_usi, src_usi, sizeof(src_usi->info));
533*4882a593Smuzhiyun 		dst_usi->min = src_usi->min;
534*4882a593Smuzhiyun 		dst_usi->max = src_usi->max;
535*4882a593Smuzhiyun 		break;
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun 	default:
538*4882a593Smuzhiyun 		NL_SET_ERR_MSG(extack, "Unsupported message type");
539*4882a593Smuzhiyun 		return -EOPNOTSUPP;
540*4882a593Smuzhiyun 	}
541*4882a593Smuzhiyun 	pos = dst->nlmsg_len;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	if (maxtype) {
544*4882a593Smuzhiyun 		/* attirbutes are xfrm_spdattr_type_t, not xfrm_attr_type_t */
545*4882a593Smuzhiyun 		WARN_ON_ONCE(src->nlmsg_type != XFRM_MSG_NEWSPDINFO);
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 		for (i = 1; i <= maxtype; i++) {
548*4882a593Smuzhiyun 			int err;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 			if (!attrs[i])
551*4882a593Smuzhiyun 				continue;
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 			/* just copy - no need for translation */
554*4882a593Smuzhiyun 			err = xfrm_attr_cpy32(dst, &pos, attrs[i], size,
555*4882a593Smuzhiyun 					nla_len(attrs[i]), nla_len(attrs[i]));
556*4882a593Smuzhiyun 			if (err)
557*4882a593Smuzhiyun 				return err;
558*4882a593Smuzhiyun 		}
559*4882a593Smuzhiyun 		return 0;
560*4882a593Smuzhiyun 	}
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	for (i = 1; i < XFRMA_MAX + 1; i++) {
563*4882a593Smuzhiyun 		int err;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 		if (i == XFRMA_PAD)
566*4882a593Smuzhiyun 			continue;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 		if (!attrs[i])
569*4882a593Smuzhiyun 			continue;
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 		err = xfrm_xlate32_attr(dst, attrs[i], &pos, size, extack);
572*4882a593Smuzhiyun 		if (err)
573*4882a593Smuzhiyun 			return err;
574*4882a593Smuzhiyun 	}
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	return 0;
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun 
xfrm_user_rcv_msg_compat(const struct nlmsghdr * h32,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)579*4882a593Smuzhiyun static struct nlmsghdr *xfrm_user_rcv_msg_compat(const struct nlmsghdr *h32,
580*4882a593Smuzhiyun 			int maxtype, const struct nla_policy *policy,
581*4882a593Smuzhiyun 			struct netlink_ext_ack *extack)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun 	/* netlink_rcv_skb() checks if a message has full (struct nlmsghdr) */
584*4882a593Smuzhiyun 	u16 type = h32->nlmsg_type - XFRM_MSG_BASE;
585*4882a593Smuzhiyun 	struct nlattr *attrs[XFRMA_MAX+1];
586*4882a593Smuzhiyun 	struct nlmsghdr *h64;
587*4882a593Smuzhiyun 	size_t len;
588*4882a593Smuzhiyun 	int err;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	BUILD_BUG_ON(ARRAY_SIZE(xfrm_msg_min) != ARRAY_SIZE(compat_msg_min));
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	if (type >= ARRAY_SIZE(xfrm_msg_min))
593*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	/* Don't call parse: the message might have only nlmsg header */
596*4882a593Smuzhiyun 	if ((h32->nlmsg_type == XFRM_MSG_GETSA ||
597*4882a593Smuzhiyun 	     h32->nlmsg_type == XFRM_MSG_GETPOLICY) &&
598*4882a593Smuzhiyun 	    (h32->nlmsg_flags & NLM_F_DUMP))
599*4882a593Smuzhiyun 		return NULL;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	err = nlmsg_parse_deprecated(h32, compat_msg_min[type], attrs,
602*4882a593Smuzhiyun 			maxtype ? : XFRMA_MAX, policy ? : compat_policy, extack);
603*4882a593Smuzhiyun 	if (err < 0)
604*4882a593Smuzhiyun 		return ERR_PTR(err);
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	len = xfrm_user_rcv_calculate_len64(h32, attrs, maxtype);
607*4882a593Smuzhiyun 	/* The message doesn't need translation */
608*4882a593Smuzhiyun 	if (len == nlmsg_len(h32))
609*4882a593Smuzhiyun 		return NULL;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	len += NLMSG_HDRLEN;
612*4882a593Smuzhiyun 	h64 = kvmalloc(len, GFP_KERNEL);
613*4882a593Smuzhiyun 	if (!h64)
614*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	err = xfrm_xlate32(h64, h32, attrs, len, type, maxtype, extack);
617*4882a593Smuzhiyun 	if (err < 0) {
618*4882a593Smuzhiyun 		kvfree(h64);
619*4882a593Smuzhiyun 		return ERR_PTR(err);
620*4882a593Smuzhiyun 	}
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	return h64;
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
xfrm_user_policy_compat(u8 ** pdata32,int optlen)625*4882a593Smuzhiyun static int xfrm_user_policy_compat(u8 **pdata32, int optlen)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	struct compat_xfrm_userpolicy_info *p = (void *)*pdata32;
628*4882a593Smuzhiyun 	u8 *src_templates, *dst_templates;
629*4882a593Smuzhiyun 	u8 *data64;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	if (optlen < sizeof(*p))
632*4882a593Smuzhiyun 		return -EINVAL;
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	data64 = kmalloc_track_caller(optlen + 4, GFP_USER | __GFP_NOWARN);
635*4882a593Smuzhiyun 	if (!data64)
636*4882a593Smuzhiyun 		return -ENOMEM;
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	memcpy(data64, *pdata32, sizeof(*p));
639*4882a593Smuzhiyun 	memset(data64 + sizeof(*p), 0, 4);
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	src_templates = *pdata32 + sizeof(*p);
642*4882a593Smuzhiyun 	dst_templates = data64 + sizeof(*p) + 4;
643*4882a593Smuzhiyun 	memcpy(dst_templates, src_templates, optlen - sizeof(*p));
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	kfree(*pdata32);
646*4882a593Smuzhiyun 	*pdata32 = data64;
647*4882a593Smuzhiyun 	return 0;
648*4882a593Smuzhiyun }
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun static struct xfrm_translator xfrm_translator = {
651*4882a593Smuzhiyun 	.owner				= THIS_MODULE,
652*4882a593Smuzhiyun 	.alloc_compat			= xfrm_alloc_compat,
653*4882a593Smuzhiyun 	.rcv_msg_compat			= xfrm_user_rcv_msg_compat,
654*4882a593Smuzhiyun 	.xlate_user_policy_sockptr	= xfrm_user_policy_compat,
655*4882a593Smuzhiyun };
656*4882a593Smuzhiyun 
xfrm_compat_init(void)657*4882a593Smuzhiyun static int __init xfrm_compat_init(void)
658*4882a593Smuzhiyun {
659*4882a593Smuzhiyun 	return xfrm_register_translator(&xfrm_translator);
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun 
xfrm_compat_exit(void)662*4882a593Smuzhiyun static void __exit xfrm_compat_exit(void)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun 	xfrm_unregister_translator(&xfrm_translator);
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun module_init(xfrm_compat_init);
668*4882a593Smuzhiyun module_exit(xfrm_compat_exit);
669*4882a593Smuzhiyun MODULE_LICENSE("GPL");
670*4882a593Smuzhiyun MODULE_AUTHOR("Dmitry Safonov");
671*4882a593Smuzhiyun MODULE_DESCRIPTION("XFRM 32-bit compatibility layer");
672