ah4.c 14 KB

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