ah4.c 14 KB

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