ah4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. ahp = x->data;
  135. ahash = ahp->ahash;
  136. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  137. goto out;
  138. nfrags = err;
  139. skb_push(skb, -skb_network_offset(skb));
  140. ah = ip_auth_hdr(skb);
  141. ihl = ip_hdrlen(skb);
  142. err = -ENOMEM;
  143. iph = ah_alloc_tmp(ahash, nfrags, ihl);
  144. if (!iph)
  145. goto out;
  146. icv = ah_tmp_icv(ahash, iph, ihl);
  147. req = ah_tmp_req(ahash, icv);
  148. sg = ah_req_sg(ahash, req);
  149. memset(ah->auth_data, 0, ahp->icv_trunc_len);
  150. top_iph = ip_hdr(skb);
  151. iph->tos = top_iph->tos;
  152. iph->ttl = top_iph->ttl;
  153. iph->frag_off = top_iph->frag_off;
  154. if (top_iph->ihl != 5) {
  155. iph->daddr = top_iph->daddr;
  156. memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  157. err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
  158. if (err)
  159. goto out_free;
  160. }
  161. ah->nexthdr = *skb_mac_header(skb);
  162. *skb_mac_header(skb) = IPPROTO_AH;
  163. top_iph->tos = 0;
  164. top_iph->tot_len = htons(skb->len);
  165. top_iph->frag_off = 0;
  166. top_iph->ttl = 0;
  167. top_iph->check = 0;
  168. if (x->props.flags & XFRM_STATE_ALIGN4)
  169. ah->hdrlen = (XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
  170. else
  171. ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
  172. ah->reserved = 0;
  173. ah->spi = x->id.spi;
  174. ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  175. sg_init_table(sg, nfrags);
  176. skb_to_sgvec(skb, sg, 0, skb->len);
  177. ahash_request_set_crypt(req, sg, icv, skb->len);
  178. ahash_request_set_callback(req, 0, ah_output_done, skb);
  179. AH_SKB_CB(skb)->tmp = iph;
  180. err = crypto_ahash_digest(req);
  181. if (err) {
  182. if (err == -EINPROGRESS)
  183. goto out;
  184. if (err == -EBUSY)
  185. err = NET_XMIT_DROP;
  186. goto out_free;
  187. }
  188. memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
  189. top_iph->tos = iph->tos;
  190. top_iph->ttl = iph->ttl;
  191. top_iph->frag_off = iph->frag_off;
  192. if (top_iph->ihl != 5) {
  193. top_iph->daddr = iph->daddr;
  194. memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  195. }
  196. out_free:
  197. kfree(iph);
  198. out:
  199. return err;
  200. }
  201. static void ah_input_done(struct crypto_async_request *base, int err)
  202. {
  203. u8 *auth_data;
  204. u8 *icv;
  205. struct iphdr *work_iph;
  206. struct sk_buff *skb = base->data;
  207. struct xfrm_state *x = xfrm_input_state(skb);
  208. struct ah_data *ahp = x->data;
  209. struct ip_auth_hdr *ah = ip_auth_hdr(skb);
  210. int ihl = ip_hdrlen(skb);
  211. int ah_hlen = (ah->hdrlen + 2) << 2;
  212. work_iph = AH_SKB_CB(skb)->tmp;
  213. auth_data = ah_tmp_auth(work_iph, ihl);
  214. icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
  215. err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
  216. if (err)
  217. goto out;
  218. err = ah->nexthdr;
  219. skb->network_header += ah_hlen;
  220. memcpy(skb_network_header(skb), work_iph, ihl);
  221. __skb_pull(skb, ah_hlen + ihl);
  222. skb_set_transport_header(skb, -ihl);
  223. out:
  224. kfree(AH_SKB_CB(skb)->tmp);
  225. xfrm_input_resume(skb, err);
  226. }
  227. static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
  228. {
  229. int ah_hlen;
  230. int ihl;
  231. int nexthdr;
  232. int nfrags;
  233. u8 *auth_data;
  234. u8 *icv;
  235. struct sk_buff *trailer;
  236. struct crypto_ahash *ahash;
  237. struct ahash_request *req;
  238. struct scatterlist *sg;
  239. struct iphdr *iph, *work_iph;
  240. struct ip_auth_hdr *ah;
  241. struct ah_data *ahp;
  242. int err = -ENOMEM;
  243. if (!pskb_may_pull(skb, sizeof(*ah)))
  244. goto out;
  245. ah = (struct ip_auth_hdr *)skb->data;
  246. ahp = x->data;
  247. ahash = ahp->ahash;
  248. nexthdr = ah->nexthdr;
  249. ah_hlen = (ah->hdrlen + 2) << 2;
  250. if (x->props.flags & XFRM_STATE_ALIGN4) {
  251. if (ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_full_len) &&
  252. ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len))
  253. goto out;
  254. } else {
  255. if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
  256. ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
  257. goto out;
  258. }
  259. if (!pskb_may_pull(skb, ah_hlen))
  260. goto out;
  261. /* We are going to _remove_ AH header to keep sockets happy,
  262. * so... Later this can change. */
  263. if (skb_cloned(skb) &&
  264. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  265. goto out;
  266. skb->ip_summed = CHECKSUM_NONE;
  267. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  268. goto out;
  269. nfrags = err;
  270. ah = (struct ip_auth_hdr *)skb->data;
  271. iph = ip_hdr(skb);
  272. ihl = ip_hdrlen(skb);
  273. work_iph = ah_alloc_tmp(ahash, nfrags, ihl + ahp->icv_trunc_len);
  274. if (!work_iph)
  275. goto out;
  276. auth_data = ah_tmp_auth(work_iph, ihl);
  277. icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len);
  278. req = ah_tmp_req(ahash, icv);
  279. sg = ah_req_sg(ahash, req);
  280. memcpy(work_iph, iph, ihl);
  281. memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
  282. memset(ah->auth_data, 0, ahp->icv_trunc_len);
  283. iph->ttl = 0;
  284. iph->tos = 0;
  285. iph->frag_off = 0;
  286. iph->check = 0;
  287. if (ihl > sizeof(*iph)) {
  288. __be32 dummy;
  289. err = ip_clear_mutable_options(iph, &dummy);
  290. if (err)
  291. goto out_free;
  292. }
  293. skb_push(skb, ihl);
  294. sg_init_table(sg, nfrags);
  295. skb_to_sgvec(skb, sg, 0, skb->len);
  296. ahash_request_set_crypt(req, sg, icv, skb->len);
  297. ahash_request_set_callback(req, 0, ah_input_done, skb);
  298. AH_SKB_CB(skb)->tmp = work_iph;
  299. err = crypto_ahash_digest(req);
  300. if (err) {
  301. if (err == -EINPROGRESS)
  302. goto out;
  303. goto out_free;
  304. }
  305. err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
  306. if (err)
  307. goto out_free;
  308. skb->network_header += ah_hlen;
  309. memcpy(skb_network_header(skb), work_iph, ihl);
  310. __skb_pull(skb, ah_hlen + ihl);
  311. skb_set_transport_header(skb, -ihl);
  312. err = nexthdr;
  313. out_free:
  314. kfree (work_iph);
  315. out:
  316. return err;
  317. }
  318. static void ah4_err(struct sk_buff *skb, u32 info)
  319. {
  320. struct net *net = dev_net(skb->dev);
  321. const struct iphdr *iph = (const struct iphdr *)skb->data;
  322. struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
  323. struct xfrm_state *x;
  324. if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
  325. icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  326. return;
  327. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  328. ah->spi, IPPROTO_AH, AF_INET);
  329. if (!x)
  330. return;
  331. printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n",
  332. ntohl(ah->spi), ntohl(iph->daddr));
  333. xfrm_state_put(x);
  334. }
  335. static int ah_init_state(struct xfrm_state *x)
  336. {
  337. struct ah_data *ahp = NULL;
  338. struct xfrm_algo_desc *aalg_desc;
  339. struct crypto_ahash *ahash;
  340. if (!x->aalg)
  341. goto error;
  342. if (x->encap)
  343. goto error;
  344. ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
  345. if (!ahp)
  346. return -ENOMEM;
  347. ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
  348. if (IS_ERR(ahash))
  349. goto error;
  350. ahp->ahash = ahash;
  351. if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
  352. (x->aalg->alg_key_len + 7) / 8))
  353. goto error;
  354. /*
  355. * Lookup the algorithm description maintained by xfrm_algo,
  356. * verify crypto transform properties, and store information
  357. * we need for AH processing. This lookup cannot fail here
  358. * after a successful crypto_alloc_ahash().
  359. */
  360. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  361. BUG_ON(!aalg_desc);
  362. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  363. crypto_ahash_digestsize(ahash)) {
  364. pr_info("%s: %s digestsize %u != %hu\n",
  365. __func__, x->aalg->alg_name,
  366. crypto_ahash_digestsize(ahash),
  367. aalg_desc->uinfo.auth.icv_fullbits / 8);
  368. goto error;
  369. }
  370. ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  371. ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
  372. BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
  373. if (x->props.flags & XFRM_STATE_ALIGN4)
  374. x->props.header_len = XFRM_ALIGN4(sizeof(struct ip_auth_hdr) +
  375. ahp->icv_trunc_len);
  376. else
  377. x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
  378. ahp->icv_trunc_len);
  379. if (x->props.mode == XFRM_MODE_TUNNEL)
  380. x->props.header_len += sizeof(struct iphdr);
  381. x->data = ahp;
  382. return 0;
  383. error:
  384. if (ahp) {
  385. crypto_free_ahash(ahp->ahash);
  386. kfree(ahp);
  387. }
  388. return -EINVAL;
  389. }
  390. static void ah_destroy(struct xfrm_state *x)
  391. {
  392. struct ah_data *ahp = x->data;
  393. if (!ahp)
  394. return;
  395. crypto_free_ahash(ahp->ahash);
  396. kfree(ahp);
  397. }
  398. static const struct xfrm_type ah_type =
  399. {
  400. .description = "AH4",
  401. .owner = THIS_MODULE,
  402. .proto = IPPROTO_AH,
  403. .flags = XFRM_TYPE_REPLAY_PROT,
  404. .init_state = ah_init_state,
  405. .destructor = ah_destroy,
  406. .input = ah_input,
  407. .output = ah_output
  408. };
  409. static const struct net_protocol ah4_protocol = {
  410. .handler = xfrm4_rcv,
  411. .err_handler = ah4_err,
  412. .no_policy = 1,
  413. .netns_ok = 1,
  414. };
  415. static int __init ah4_init(void)
  416. {
  417. if (xfrm_register_type(&ah_type, AF_INET) < 0) {
  418. pr_info("%s: can't add xfrm type\n", __func__);
  419. return -EAGAIN;
  420. }
  421. if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) {
  422. pr_info("%s: can't add protocol\n", __func__);
  423. xfrm_unregister_type(&ah_type, AF_INET);
  424. return -EAGAIN;
  425. }
  426. return 0;
  427. }
  428. static void __exit ah4_fini(void)
  429. {
  430. if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0)
  431. pr_info("%s: can't remove protocol\n", __func__);
  432. if (xfrm_unregister_type(&ah_type, AF_INET) < 0)
  433. pr_info("%s: can't remove xfrm type\n", __func__);
  434. }
  435. module_init(ah4_init);
  436. module_exit(ah4_fini);
  437. MODULE_LICENSE("GPL");
  438. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH);