ah4.c 14 KB

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