xfrm_compat.c 19 KB

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