ah4.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #define pr_fmt(fmt) "IPsec: " fmt
  3. #include <crypto/hash.h>
  4. #include <crypto/utils.h>
  5. #include <linux/err.h>
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <net/ip.h>
  9. #include <net/xfrm.h>
  10. #include <net/ah.h>
  11. #include <linux/crypto.h>
  12. #include <linux/pfkeyv2.h>
  13. #include <linux/scatterlist.h>
  14. #include <net/icmp.h>
  15. #include <net/protocol.h>
  16. struct ah_skb_cb {
  17. struct xfrm_skb_cb xfrm;
  18. void *tmp;
  19. };
  20. #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
  21. static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
  22. unsigned int size)
  23. {
  24. unsigned int len;
  25. len = size + crypto_ahash_digestsize(ahash);
  26. len = ALIGN(len, crypto_tfm_ctx_alignment());
  27. len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
  28. len = ALIGN(len, __alignof__(struct scatterlist));
  29. len += sizeof(struct scatterlist) * nfrags;
  30. return kmalloc(len, GFP_ATOMIC);
  31. }
  32. static inline u8 *ah_tmp_auth(void *tmp, unsigned int offset)
  33. {
  34. return tmp + offset;
  35. }
  36. static inline u8 *ah_tmp_icv(void *tmp, unsigned int offset)
  37. {
  38. return tmp + offset;
  39. }
  40. static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
  41. u8 *icv)
  42. {
  43. struct ahash_request *req;
  44. req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
  45. crypto_tfm_ctx_alignment());
  46. ahash_request_set_tfm(req, ahash);
  47. return req;
  48. }
  49. static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
  50. struct ahash_request *req)
  51. {
  52. return (void *)ALIGN((unsigned long)(req + 1) +
  53. crypto_ahash_reqsize(ahash),
  54. __alignof__(struct scatterlist));
  55. }
  56. /* Clear mutable options and find final destination to substitute
  57. * into IP header for icv calculation. Options are already checked
  58. * for validity, so paranoia is not required. */
  59. static int ip_clear_mutable_options(const struct iphdr *iph, __be32 *daddr)
  60. {
  61. unsigned char *optptr = (unsigned char *)(iph+1);
  62. int l = iph->ihl*4 - sizeof(struct iphdr);
  63. int optlen;
  64. while (l > 0) {
  65. switch (*optptr) {
  66. case IPOPT_END:
  67. return 0;
  68. case IPOPT_NOOP:
  69. l--;
  70. optptr++;
  71. continue;
  72. }
  73. optlen = optptr[1];
  74. if (optlen<2 || optlen>l)
  75. return -EINVAL;
  76. switch (*optptr) {
  77. case IPOPT_SEC:
  78. case 0x85: /* Some "Extended Security" crap. */
  79. case IPOPT_CIPSO:
  80. case IPOPT_RA:
  81. case 0x80|21: /* RFC1770 */
  82. break;
  83. case IPOPT_LSRR:
  84. case IPOPT_SSRR:
  85. if (optlen < 6)
  86. return -EINVAL;
  87. memcpy(daddr, optptr+optlen-4, 4);
  88. fallthrough;
  89. default:
  90. memset(optptr, 0, optlen);
  91. }
  92. l -= optlen;
  93. optptr += optlen;
  94. }
  95. return 0;
  96. }
  97. static void ah_output_done(void *data, int err)
  98. {
  99. u8 *icv;
  100. struct iphdr *iph;
  101. struct sk_buff *skb = data;
  102. struct xfrm_state *x = skb_dst(skb)->xfrm;
  103. struct ah_data *ahp = x->data;
  104. struct iphdr *top_iph = ip_hdr(skb);
  105. struct ip_auth_hdr *ah = ip_auth_hdr(skb);
  106. int ihl = ip_hdrlen(skb);
  107. iph = AH_SKB_CB(skb)->tmp;
  108. icv = ah_tmp_icv(iph, ihl);
  109. memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
  110. top_iph->tos = iph->tos;
  111. top_iph->ttl = iph->ttl;
  112. top_iph->frag_off = iph->frag_off;
  113. if (top_iph->ihl != 5) {
  114. top_iph->daddr = iph->daddr;
  115. memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  116. }
  117. kfree(AH_SKB_CB(skb)->tmp);
  118. xfrm_output_resume(skb->sk, skb, err);
  119. }
  120. static int ah_output(struct xfrm_state *x, struct sk_buff *skb)
  121. {
  122. int err;
  123. int nfrags;
  124. int ihl;
  125. u8 *icv;
  126. struct sk_buff *trailer;
  127. struct crypto_ahash *ahash;
  128. struct ahash_request *req;
  129. struct scatterlist *sg;
  130. struct iphdr *iph, *top_iph;
  131. struct ip_auth_hdr *ah;
  132. struct ah_data *ahp;
  133. int seqhi_len = 0;
  134. __be32 *seqhi;
  135. int sglists = 0;
  136. struct scatterlist *seqhisg;
  137. ahp = x->data;
  138. ahash = ahp->ahash;
  139. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  140. goto out;
  141. nfrags = err;
  142. skb_push(skb, -skb_network_offset(skb));
  143. ah = ip_auth_hdr(skb);
  144. ihl = ip_hdrlen(skb);
  145. if (x->props.flags & XFRM_STATE_ESN) {
  146. sglists = 1;
  147. seqhi_len = sizeof(*seqhi);
  148. }
  149. err = -ENOMEM;
  150. iph = ah_alloc_tmp(ahash, nfrags + sglists, ihl + seqhi_len);
  151. if (!iph)
  152. goto out;
  153. seqhi = (__be32 *)((char *)iph + ihl);
  154. icv = ah_tmp_icv(seqhi, seqhi_len);
  155. req = ah_tmp_req(ahash, icv);
  156. sg = ah_req_sg(ahash, req);
  157. seqhisg = sg + nfrags;
  158. memset(ah->auth_data, 0, ahp->icv_trunc_len);
  159. top_iph = ip_hdr(skb);
  160. iph->tos = top_iph->tos;
  161. iph->ttl = top_iph->ttl;
  162. iph->frag_off = top_iph->frag_off;
  163. if (top_iph->ihl != 5) {
  164. iph->daddr = top_iph->daddr;
  165. memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  166. err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
  167. if (err)
  168. goto out_free;
  169. }
  170. ah->nexthdr = *skb_mac_header(skb);
  171. *skb_mac_header(skb) = IPPROTO_AH;
  172. top_iph->tos = 0;
  173. top_iph->tot_len = htons(skb->len);
  174. top_iph->frag_off = 0;
  175. top_iph->ttl = 0;
  176. top_iph->check = 0;
  177. if (x->props.flags & XFRM_STATE_ALIGN4)
  178. ah->hdrlen = (XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
  179. else
  180. ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
  181. ah->reserved = 0;
  182. ah->spi = x->id.spi;
  183. ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  184. sg_init_table(sg, nfrags + sglists);
  185. err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
  186. if (unlikely(err < 0))
  187. goto out_free;
  188. if (x->props.flags & XFRM_STATE_ESN) {
  189. /* Attach seqhi sg right after packet payload */
  190. *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
  191. sg_set_buf(seqhisg, seqhi, seqhi_len);
  192. }
  193. ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
  194. ahash_request_set_callback(req, 0, ah_output_done, skb);
  195. AH_SKB_CB(skb)->tmp = iph;
  196. err = crypto_ahash_digest(req);
  197. if (err) {
  198. if (err == -EINPROGRESS)
  199. goto out;
  200. if (err == -ENOSPC)
  201. err = NET_XMIT_DROP;
  202. goto out_free;
  203. }
  204. memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
  205. top_iph->tos = iph->tos;
  206. top_iph->ttl = iph->ttl;
  207. top_iph->frag_off = iph->frag_off;
  208. if (top_iph->ihl != 5) {
  209. top_iph->daddr = iph->daddr;
  210. memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  211. }
  212. out_free:
  213. kfree(iph);
  214. out:
  215. return err;
  216. }
  217. static void ah_input_done(void *data, int err)
  218. {
  219. u8 *auth_data;
  220. u8 *icv;
  221. struct iphdr *work_iph;
  222. struct sk_buff *skb = data;
  223. struct xfrm_state *x = xfrm_input_state(skb);
  224. struct ah_data *ahp = x->data;
  225. struct ip_auth_hdr *ah = ip_auth_hdr(skb);
  226. int ihl = ip_hdrlen(skb);
  227. int ah_hlen = (ah->hdrlen + 2) << 2;
  228. if (err)
  229. goto out;
  230. work_iph = AH_SKB_CB(skb)->tmp;
  231. auth_data = ah_tmp_auth(work_iph, ihl);
  232. icv = ah_tmp_icv(auth_data, ahp->icv_trunc_len);
  233. err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
  234. if (err)
  235. goto out;
  236. err = ah->nexthdr;
  237. skb->network_header += ah_hlen;
  238. memcpy(skb_network_header(skb), work_iph, ihl);
  239. __skb_pull(skb, ah_hlen + ihl);
  240. if (x->props.mode == XFRM_MODE_TUNNEL)
  241. skb_reset_transport_header(skb);
  242. else
  243. skb_set_transport_header(skb, -ihl);
  244. out:
  245. kfree(AH_SKB_CB(skb)->tmp);
  246. xfrm_input_resume(skb, err);
  247. }
  248. static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
  249. {
  250. int ah_hlen;
  251. int ihl;
  252. int nexthdr;
  253. int nfrags;
  254. u8 *auth_data;
  255. u8 *icv;
  256. struct sk_buff *trailer;
  257. struct crypto_ahash *ahash;
  258. struct ahash_request *req;
  259. struct scatterlist *sg;
  260. struct iphdr *iph, *work_iph;
  261. struct ip_auth_hdr *ah;
  262. struct ah_data *ahp;
  263. int err = -ENOMEM;
  264. int seqhi_len = 0;
  265. __be32 *seqhi;
  266. int sglists = 0;
  267. struct scatterlist *seqhisg;
  268. if (!pskb_may_pull(skb, sizeof(*ah)))
  269. goto out;
  270. ah = (struct ip_auth_hdr *)skb->data;
  271. ahp = x->data;
  272. ahash = ahp->ahash;
  273. nexthdr = ah->nexthdr;
  274. ah_hlen = (ah->hdrlen + 2) << 2;
  275. if (x->props.flags & XFRM_STATE_ALIGN4) {
  276. if (ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_full_len) &&
  277. ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len))
  278. goto out;
  279. } else {
  280. if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
  281. ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
  282. goto out;
  283. }
  284. if (!pskb_may_pull(skb, ah_hlen))
  285. goto out;
  286. /* We are going to _remove_ AH header to keep sockets happy,
  287. * so... Later this can change. */
  288. if (skb_unclone(skb, GFP_ATOMIC))
  289. goto out;
  290. skb->ip_summed = CHECKSUM_NONE;
  291. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  292. goto out;
  293. nfrags = err;
  294. ah = (struct ip_auth_hdr *)skb->data;
  295. iph = ip_hdr(skb);
  296. ihl = ip_hdrlen(skb);
  297. if (x->props.flags & XFRM_STATE_ESN) {
  298. sglists = 1;
  299. seqhi_len = sizeof(*seqhi);
  300. }
  301. work_iph = ah_alloc_tmp(ahash, nfrags + sglists, ihl +
  302. ahp->icv_trunc_len + seqhi_len);
  303. if (!work_iph) {
  304. err = -ENOMEM;
  305. goto out;
  306. }
  307. seqhi = (__be32 *)((char *)work_iph + ihl);
  308. auth_data = ah_tmp_auth(seqhi, seqhi_len);
  309. icv = ah_tmp_icv(auth_data, ahp->icv_trunc_len);
  310. req = ah_tmp_req(ahash, icv);
  311. sg = ah_req_sg(ahash, req);
  312. seqhisg = sg + nfrags;
  313. memcpy(work_iph, iph, ihl);
  314. memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
  315. memset(ah->auth_data, 0, ahp->icv_trunc_len);
  316. iph->ttl = 0;
  317. iph->tos = 0;
  318. iph->frag_off = 0;
  319. iph->check = 0;
  320. if (ihl > sizeof(*iph)) {
  321. __be32 dummy;
  322. err = ip_clear_mutable_options(iph, &dummy);
  323. if (err)
  324. goto out_free;
  325. }
  326. skb_push(skb, ihl);
  327. sg_init_table(sg, nfrags + sglists);
  328. err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
  329. if (unlikely(err < 0))
  330. goto out_free;
  331. if (x->props.flags & XFRM_STATE_ESN) {
  332. /* Attach seqhi sg right after packet payload */
  333. *seqhi = XFRM_SKB_CB(skb)->seq.input.hi;
  334. sg_set_buf(seqhisg, seqhi, seqhi_len);
  335. }
  336. ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
  337. ahash_request_set_callback(req, 0, ah_input_done, skb);
  338. AH_SKB_CB(skb)->tmp = work_iph;
  339. err = crypto_ahash_digest(req);
  340. if (err) {
  341. if (err == -EINPROGRESS)
  342. goto out;
  343. goto out_free;
  344. }
  345. err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
  346. if (err)
  347. goto out_free;
  348. skb->network_header += ah_hlen;
  349. memcpy(skb_network_header(skb), work_iph, ihl);
  350. __skb_pull(skb, ah_hlen + ihl);
  351. if (x->props.mode == XFRM_MODE_TUNNEL)
  352. skb_reset_transport_header(skb);
  353. else
  354. skb_set_transport_header(skb, -ihl);
  355. err = nexthdr;
  356. out_free:
  357. kfree (work_iph);
  358. out:
  359. return err;
  360. }
  361. static int ah4_err(struct sk_buff *skb, u32 info)
  362. {
  363. struct net *net = dev_net(skb->dev);
  364. const struct iphdr *iph = (const struct iphdr *)skb->data;
  365. struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
  366. struct xfrm_state *x;
  367. switch (icmp_hdr(skb)->type) {
  368. case ICMP_DEST_UNREACH:
  369. if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  370. return 0;
  371. break;
  372. case ICMP_REDIRECT:
  373. break;
  374. default:
  375. return 0;
  376. }
  377. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  378. ah->spi, IPPROTO_AH, AF_INET);
  379. if (!x)
  380. return 0;
  381. if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
  382. ipv4_update_pmtu(skb, net, info, 0, IPPROTO_AH);
  383. else
  384. ipv4_redirect(skb, net, 0, IPPROTO_AH);
  385. xfrm_state_put(x);
  386. return 0;
  387. }
  388. static int ah_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
  389. {
  390. struct ah_data *ahp = NULL;
  391. struct xfrm_algo_desc *aalg_desc;
  392. struct crypto_ahash *ahash;
  393. if (!x->aalg) {
  394. NL_SET_ERR_MSG(extack, "AH requires a state with an AUTH algorithm");
  395. goto error;
  396. }
  397. if (x->encap) {
  398. NL_SET_ERR_MSG(extack, "AH is not compatible with encapsulation");
  399. goto error;
  400. }
  401. ahp = kzalloc_obj(*ahp);
  402. if (!ahp)
  403. return -ENOMEM;
  404. ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
  405. if (IS_ERR(ahash)) {
  406. NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
  407. goto error;
  408. }
  409. ahp->ahash = ahash;
  410. if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
  411. (x->aalg->alg_key_len + 7) / 8)) {
  412. NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
  413. goto error;
  414. }
  415. /*
  416. * Lookup the algorithm description maintained by xfrm_algo,
  417. * verify crypto transform properties, and store information
  418. * we need for AH processing. This lookup cannot fail here
  419. * after a successful crypto_alloc_ahash().
  420. */
  421. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  422. BUG_ON(!aalg_desc);
  423. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  424. crypto_ahash_digestsize(ahash)) {
  425. NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
  426. goto error;
  427. }
  428. ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  429. ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
  430. if (x->props.flags & XFRM_STATE_ALIGN4)
  431. x->props.header_len = XFRM_ALIGN4(sizeof(struct ip_auth_hdr) +
  432. ahp->icv_trunc_len);
  433. else
  434. x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
  435. ahp->icv_trunc_len);
  436. if (x->props.mode == XFRM_MODE_TUNNEL)
  437. x->props.header_len += sizeof(struct iphdr);
  438. x->data = ahp;
  439. return 0;
  440. error:
  441. if (ahp) {
  442. crypto_free_ahash(ahp->ahash);
  443. kfree(ahp);
  444. }
  445. return -EINVAL;
  446. }
  447. static void ah_destroy(struct xfrm_state *x)
  448. {
  449. struct ah_data *ahp = x->data;
  450. if (!ahp)
  451. return;
  452. crypto_free_ahash(ahp->ahash);
  453. kfree(ahp);
  454. }
  455. static int ah4_rcv_cb(struct sk_buff *skb, int err)
  456. {
  457. return 0;
  458. }
  459. static const struct xfrm_type ah_type =
  460. {
  461. .owner = THIS_MODULE,
  462. .proto = IPPROTO_AH,
  463. .flags = XFRM_TYPE_REPLAY_PROT,
  464. .init_state = ah_init_state,
  465. .destructor = ah_destroy,
  466. .input = ah_input,
  467. .output = ah_output
  468. };
  469. static struct xfrm4_protocol ah4_protocol = {
  470. .handler = xfrm4_rcv,
  471. .input_handler = xfrm_input,
  472. .cb_handler = ah4_rcv_cb,
  473. .err_handler = ah4_err,
  474. .priority = 0,
  475. };
  476. static int __init ah4_init(void)
  477. {
  478. if (xfrm_register_type(&ah_type, AF_INET) < 0) {
  479. pr_info("%s: can't add xfrm type\n", __func__);
  480. return -EAGAIN;
  481. }
  482. if (xfrm4_protocol_register(&ah4_protocol, IPPROTO_AH) < 0) {
  483. pr_info("%s: can't add protocol\n", __func__);
  484. xfrm_unregister_type(&ah_type, AF_INET);
  485. return -EAGAIN;
  486. }
  487. return 0;
  488. }
  489. static void __exit ah4_fini(void)
  490. {
  491. if (xfrm4_protocol_deregister(&ah4_protocol, IPPROTO_AH) < 0)
  492. pr_info("%s: can't remove protocol\n", __func__);
  493. xfrm_unregister_type(&ah_type, AF_INET);
  494. }
  495. module_init(ah4_init);
  496. module_exit(ah4_fini);
  497. MODULE_DESCRIPTION("IPv4 AH transformation library");
  498. MODULE_LICENSE("GPL");
  499. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH);