ah4.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. skb_to_sgvec_nomark(skb, sg, 0, skb->len);
  187. if (x->props.flags & XFRM_STATE_ESN) {
  188. /* Attach seqhi sg right after packet payload */
  189. *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
  190. sg_set_buf(seqhisg, seqhi, seqhi_len);
  191. }
  192. ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
  193. ahash_request_set_callback(req, 0, ah_output_done, skb);
  194. AH_SKB_CB(skb)->tmp = iph;
  195. err = crypto_ahash_digest(req);
  196. if (err) {
  197. if (err == -EINPROGRESS)
  198. goto out;
  199. if (err == -EBUSY)
  200. err = NET_XMIT_DROP;
  201. goto out_free;
  202. }
  203. memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
  204. top_iph->tos = iph->tos;
  205. top_iph->ttl = iph->ttl;
  206. top_iph->frag_off = iph->frag_off;
  207. if (top_iph->ihl != 5) {
  208. top_iph->daddr = iph->daddr;
  209. memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  210. }
  211. out_free:
  212. kfree(iph);
  213. out:
  214. return err;
  215. }
  216. static void ah_input_done(struct crypto_async_request *base, int err)
  217. {
  218. u8 *auth_data;
  219. u8 *icv;
  220. struct iphdr *work_iph;
  221. struct sk_buff *skb = base->data;
  222. struct xfrm_state *x = xfrm_input_state(skb);
  223. struct ah_data *ahp = x->data;
  224. struct ip_auth_hdr *ah = ip_auth_hdr(skb);
  225. int ihl = ip_hdrlen(skb);
  226. int ah_hlen = (ah->hdrlen + 2) << 2;
  227. work_iph = AH_SKB_CB(skb)->tmp;
  228. auth_data = ah_tmp_auth(work_iph, ihl);
  229. icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
  230. err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
  231. if (err)
  232. goto out;
  233. err = ah->nexthdr;
  234. skb->network_header += ah_hlen;
  235. memcpy(skb_network_header(skb), work_iph, ihl);
  236. __skb_pull(skb, ah_hlen + ihl);
  237. if (x->props.mode == XFRM_MODE_TUNNEL)
  238. skb_reset_transport_header(skb);
  239. else
  240. skb_set_transport_header(skb, -ihl);
  241. out:
  242. kfree(AH_SKB_CB(skb)->tmp);
  243. xfrm_input_resume(skb, err);
  244. }
  245. static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
  246. {
  247. int ah_hlen;
  248. int ihl;
  249. int nexthdr;
  250. int nfrags;
  251. u8 *auth_data;
  252. u8 *icv;
  253. struct sk_buff *trailer;
  254. struct crypto_ahash *ahash;
  255. struct ahash_request *req;
  256. struct scatterlist *sg;
  257. struct iphdr *iph, *work_iph;
  258. struct ip_auth_hdr *ah;
  259. struct ah_data *ahp;
  260. int err = -ENOMEM;
  261. int seqhi_len = 0;
  262. __be32 *seqhi;
  263. int sglists = 0;
  264. struct scatterlist *seqhisg;
  265. if (!pskb_may_pull(skb, sizeof(*ah)))
  266. goto out;
  267. ah = (struct ip_auth_hdr *)skb->data;
  268. ahp = x->data;
  269. ahash = ahp->ahash;
  270. nexthdr = ah->nexthdr;
  271. ah_hlen = (ah->hdrlen + 2) << 2;
  272. if (x->props.flags & XFRM_STATE_ALIGN4) {
  273. if (ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_full_len) &&
  274. ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len))
  275. goto out;
  276. } else {
  277. if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
  278. ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
  279. goto out;
  280. }
  281. if (!pskb_may_pull(skb, ah_hlen))
  282. goto out;
  283. /* We are going to _remove_ AH header to keep sockets happy,
  284. * so... Later this can change. */
  285. if (skb_unclone(skb, GFP_ATOMIC))
  286. goto out;
  287. skb->ip_summed = CHECKSUM_NONE;
  288. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  289. goto out;
  290. nfrags = err;
  291. ah = (struct ip_auth_hdr *)skb->data;
  292. iph = ip_hdr(skb);
  293. ihl = ip_hdrlen(skb);
  294. if (x->props.flags & XFRM_STATE_ESN) {
  295. sglists = 1;
  296. seqhi_len = sizeof(*seqhi);
  297. }
  298. work_iph = ah_alloc_tmp(ahash, nfrags + sglists, ihl +
  299. ahp->icv_trunc_len + seqhi_len);
  300. if (!work_iph) {
  301. err = -ENOMEM;
  302. goto out;
  303. }
  304. seqhi = (__be32 *)((char *)work_iph + ihl);
  305. auth_data = ah_tmp_auth(seqhi, seqhi_len);
  306. icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len);
  307. req = ah_tmp_req(ahash, icv);
  308. sg = ah_req_sg(ahash, req);
  309. seqhisg = sg + nfrags;
  310. memcpy(work_iph, iph, ihl);
  311. memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
  312. memset(ah->auth_data, 0, ahp->icv_trunc_len);
  313. iph->ttl = 0;
  314. iph->tos = 0;
  315. iph->frag_off = 0;
  316. iph->check = 0;
  317. if (ihl > sizeof(*iph)) {
  318. __be32 dummy;
  319. err = ip_clear_mutable_options(iph, &dummy);
  320. if (err)
  321. goto out_free;
  322. }
  323. skb_push(skb, ihl);
  324. sg_init_table(sg, nfrags + sglists);
  325. skb_to_sgvec_nomark(skb, sg, 0, skb->len);
  326. if (x->props.flags & XFRM_STATE_ESN) {
  327. /* Attach seqhi sg right after packet payload */
  328. *seqhi = XFRM_SKB_CB(skb)->seq.input.hi;
  329. sg_set_buf(seqhisg, seqhi, seqhi_len);
  330. }
  331. ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
  332. ahash_request_set_callback(req, 0, ah_input_done, skb);
  333. AH_SKB_CB(skb)->tmp = work_iph;
  334. err = crypto_ahash_digest(req);
  335. if (err) {
  336. if (err == -EINPROGRESS)
  337. goto out;
  338. goto out_free;
  339. }
  340. err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
  341. if (err)
  342. goto out_free;
  343. skb->network_header += ah_hlen;
  344. memcpy(skb_network_header(skb), work_iph, ihl);
  345. __skb_pull(skb, ah_hlen + ihl);
  346. if (x->props.mode == XFRM_MODE_TUNNEL)
  347. skb_reset_transport_header(skb);
  348. else
  349. skb_set_transport_header(skb, -ihl);
  350. err = nexthdr;
  351. out_free:
  352. kfree (work_iph);
  353. out:
  354. return err;
  355. }
  356. static int ah4_err(struct sk_buff *skb, u32 info)
  357. {
  358. struct net *net = dev_net(skb->dev);
  359. const struct iphdr *iph = (const struct iphdr *)skb->data;
  360. struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
  361. struct xfrm_state *x;
  362. switch (icmp_hdr(skb)->type) {
  363. case ICMP_DEST_UNREACH:
  364. if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  365. return 0;
  366. case ICMP_REDIRECT:
  367. break;
  368. default:
  369. return 0;
  370. }
  371. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  372. ah->spi, IPPROTO_AH, AF_INET);
  373. if (!x)
  374. return 0;
  375. if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
  376. ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_AH, 0);
  377. else
  378. ipv4_redirect(skb, net, 0, 0, IPPROTO_AH, 0);
  379. xfrm_state_put(x);
  380. return 0;
  381. }
  382. static int ah_init_state(struct xfrm_state *x)
  383. {
  384. struct ah_data *ahp = NULL;
  385. struct xfrm_algo_desc *aalg_desc;
  386. struct crypto_ahash *ahash;
  387. if (!x->aalg)
  388. goto error;
  389. if (x->encap)
  390. goto error;
  391. ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
  392. if (!ahp)
  393. return -ENOMEM;
  394. ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
  395. if (IS_ERR(ahash))
  396. goto error;
  397. ahp->ahash = ahash;
  398. if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
  399. (x->aalg->alg_key_len + 7) / 8))
  400. goto error;
  401. /*
  402. * Lookup the algorithm description maintained by xfrm_algo,
  403. * verify crypto transform properties, and store information
  404. * we need for AH processing. This lookup cannot fail here
  405. * after a successful crypto_alloc_ahash().
  406. */
  407. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  408. BUG_ON(!aalg_desc);
  409. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  410. crypto_ahash_digestsize(ahash)) {
  411. pr_info("%s: %s digestsize %u != %hu\n",
  412. __func__, x->aalg->alg_name,
  413. crypto_ahash_digestsize(ahash),
  414. aalg_desc->uinfo.auth.icv_fullbits / 8);
  415. goto error;
  416. }
  417. ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  418. ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
  419. if (x->props.flags & XFRM_STATE_ALIGN4)
  420. x->props.header_len = XFRM_ALIGN4(sizeof(struct ip_auth_hdr) +
  421. ahp->icv_trunc_len);
  422. else
  423. x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
  424. ahp->icv_trunc_len);
  425. if (x->props.mode == XFRM_MODE_TUNNEL)
  426. x->props.header_len += sizeof(struct iphdr);
  427. x->data = ahp;
  428. return 0;
  429. error:
  430. if (ahp) {
  431. crypto_free_ahash(ahp->ahash);
  432. kfree(ahp);
  433. }
  434. return -EINVAL;
  435. }
  436. static void ah_destroy(struct xfrm_state *x)
  437. {
  438. struct ah_data *ahp = x->data;
  439. if (!ahp)
  440. return;
  441. crypto_free_ahash(ahp->ahash);
  442. kfree(ahp);
  443. }
  444. static int ah4_rcv_cb(struct sk_buff *skb, int err)
  445. {
  446. return 0;
  447. }
  448. static const struct xfrm_type ah_type =
  449. {
  450. .description = "AH4",
  451. .owner = THIS_MODULE,
  452. .proto = IPPROTO_AH,
  453. .flags = XFRM_TYPE_REPLAY_PROT,
  454. .init_state = ah_init_state,
  455. .destructor = ah_destroy,
  456. .input = ah_input,
  457. .output = ah_output
  458. };
  459. static struct xfrm4_protocol ah4_protocol = {
  460. .handler = xfrm4_rcv,
  461. .input_handler = xfrm_input,
  462. .cb_handler = ah4_rcv_cb,
  463. .err_handler = ah4_err,
  464. .priority = 0,
  465. };
  466. static int __init ah4_init(void)
  467. {
  468. if (xfrm_register_type(&ah_type, AF_INET) < 0) {
  469. pr_info("%s: can't add xfrm type\n", __func__);
  470. return -EAGAIN;
  471. }
  472. if (xfrm4_protocol_register(&ah4_protocol, IPPROTO_AH) < 0) {
  473. pr_info("%s: can't add protocol\n", __func__);
  474. xfrm_unregister_type(&ah_type, AF_INET);
  475. return -EAGAIN;
  476. }
  477. return 0;
  478. }
  479. static void __exit ah4_fini(void)
  480. {
  481. if (xfrm4_protocol_deregister(&ah4_protocol, IPPROTO_AH) < 0)
  482. pr_info("%s: can't remove protocol\n", __func__);
  483. if (xfrm_unregister_type(&ah_type, AF_INET) < 0)
  484. pr_info("%s: can't remove xfrm type\n", __func__);
  485. }
  486. module_init(ah4_init);
  487. module_exit(ah4_fini);
  488. MODULE_LICENSE("GPL");
  489. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH);