ah4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. #include <crypto/hash.h>
  2. #include <linux/err.h>
  3. #include <linux/module.h>
  4. #include <linux/slab.h>
  5. #include <net/ip.h>
  6. #include <net/xfrm.h>
  7. #include <net/ah.h>
  8. #include <linux/crypto.h>
  9. #include <linux/pfkeyv2.h>
  10. #include <linux/scatterlist.h>
  11. #include <net/icmp.h>
  12. #include <net/protocol.h>
  13. struct ah_skb_cb {
  14. struct xfrm_skb_cb xfrm;
  15. void *tmp;
  16. };
  17. #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
  18. static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
  19. unsigned int size)
  20. {
  21. unsigned int len;
  22. len = size + crypto_ahash_digestsize(ahash) +
  23. (crypto_ahash_alignmask(ahash) &
  24. ~(crypto_tfm_ctx_alignment() - 1));
  25. len = ALIGN(len, crypto_tfm_ctx_alignment());
  26. len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
  27. len = ALIGN(len, __alignof__(struct scatterlist));
  28. len += sizeof(struct scatterlist) * nfrags;
  29. return kmalloc(len, GFP_ATOMIC);
  30. }
  31. static inline u8 *ah_tmp_auth(void *tmp, unsigned int offset)
  32. {
  33. return tmp + offset;
  34. }
  35. static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
  36. unsigned int offset)
  37. {
  38. return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
  39. }
  40. static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
  41. u8 *icv)
  42. {
  43. struct ahash_request *req;
  44. req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
  45. crypto_tfm_ctx_alignment());
  46. ahash_request_set_tfm(req, ahash);
  47. return req;
  48. }
  49. static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
  50. struct ahash_request *req)
  51. {
  52. return (void *)ALIGN((unsigned long)(req + 1) +
  53. crypto_ahash_reqsize(ahash),
  54. __alignof__(struct scatterlist));
  55. }
  56. /* Clear mutable options and find final destination to substitute
  57. * into IP header for icv calculation. Options are already checked
  58. * for validity, so paranoia is not required. */
  59. static int ip_clear_mutable_options(struct iphdr *iph, __be32 *daddr)
  60. {
  61. unsigned char * optptr = (unsigned char*)(iph+1);
  62. int l = iph->ihl*4 - sizeof(struct iphdr);
  63. int optlen;
  64. while (l > 0) {
  65. switch (*optptr) {
  66. case IPOPT_END:
  67. return 0;
  68. case IPOPT_NOOP:
  69. l--;
  70. optptr++;
  71. continue;
  72. }
  73. optlen = optptr[1];
  74. if (optlen<2 || optlen>l)
  75. return -EINVAL;
  76. switch (*optptr) {
  77. case IPOPT_SEC:
  78. case 0x85: /* Some "Extended Security" crap. */
  79. case IPOPT_CIPSO:
  80. case IPOPT_RA:
  81. case 0x80|21: /* RFC1770 */
  82. break;
  83. case IPOPT_LSRR:
  84. case IPOPT_SSRR:
  85. if (optlen < 6)
  86. return -EINVAL;
  87. memcpy(daddr, optptr+optlen-4, 4);
  88. /* Fall through */
  89. default:
  90. memset(optptr, 0, optlen);
  91. }
  92. l -= optlen;
  93. optptr += optlen;
  94. }
  95. return 0;
  96. }
  97. static void ah_output_done(struct crypto_async_request *base, int err)
  98. {
  99. u8 *icv;
  100. struct iphdr *iph;
  101. struct sk_buff *skb = base->data;
  102. struct xfrm_state *x = skb_dst(skb)->xfrm;
  103. struct ah_data *ahp = x->data;
  104. struct iphdr *top_iph = ip_hdr(skb);
  105. struct ip_auth_hdr *ah = ip_auth_hdr(skb);
  106. int ihl = ip_hdrlen(skb);
  107. iph = AH_SKB_CB(skb)->tmp;
  108. icv = ah_tmp_icv(ahp->ahash, iph, ihl);
  109. memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
  110. top_iph->tos = iph->tos;
  111. top_iph->ttl = iph->ttl;
  112. top_iph->frag_off = iph->frag_off;
  113. if (top_iph->ihl != 5) {
  114. top_iph->daddr = iph->daddr;
  115. memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  116. }
  117. err = ah->nexthdr;
  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. ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
  169. ah->reserved = 0;
  170. ah->spi = x->id.spi;
  171. ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
  172. sg_init_table(sg, nfrags);
  173. skb_to_sgvec(skb, sg, 0, skb->len);
  174. ahash_request_set_crypt(req, sg, icv, skb->len);
  175. ahash_request_set_callback(req, 0, ah_output_done, skb);
  176. AH_SKB_CB(skb)->tmp = iph;
  177. err = crypto_ahash_digest(req);
  178. if (err) {
  179. if (err == -EINPROGRESS)
  180. goto out;
  181. if (err == -EBUSY)
  182. err = NET_XMIT_DROP;
  183. goto out_free;
  184. }
  185. memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
  186. top_iph->tos = iph->tos;
  187. top_iph->ttl = iph->ttl;
  188. top_iph->frag_off = iph->frag_off;
  189. if (top_iph->ihl != 5) {
  190. top_iph->daddr = iph->daddr;
  191. memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  192. }
  193. out_free:
  194. kfree(iph);
  195. out:
  196. return err;
  197. }
  198. static void ah_input_done(struct crypto_async_request *base, int err)
  199. {
  200. u8 *auth_data;
  201. u8 *icv;
  202. struct iphdr *work_iph;
  203. struct sk_buff *skb = base->data;
  204. struct xfrm_state *x = xfrm_input_state(skb);
  205. struct ah_data *ahp = x->data;
  206. struct ip_auth_hdr *ah = ip_auth_hdr(skb);
  207. int ihl = ip_hdrlen(skb);
  208. int ah_hlen = (ah->hdrlen + 2) << 2;
  209. work_iph = AH_SKB_CB(skb)->tmp;
  210. auth_data = ah_tmp_auth(work_iph, ihl);
  211. icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
  212. err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
  213. if (err)
  214. goto out;
  215. skb->network_header += ah_hlen;
  216. memcpy(skb_network_header(skb), work_iph, ihl);
  217. __skb_pull(skb, ah_hlen + ihl);
  218. skb_set_transport_header(skb, -ihl);
  219. err = ah->nexthdr;
  220. out:
  221. kfree(AH_SKB_CB(skb)->tmp);
  222. xfrm_input_resume(skb, err);
  223. }
  224. static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
  225. {
  226. int ah_hlen;
  227. int ihl;
  228. int nexthdr;
  229. int nfrags;
  230. u8 *auth_data;
  231. u8 *icv;
  232. struct sk_buff *trailer;
  233. struct crypto_ahash *ahash;
  234. struct ahash_request *req;
  235. struct scatterlist *sg;
  236. struct iphdr *iph, *work_iph;
  237. struct ip_auth_hdr *ah;
  238. struct ah_data *ahp;
  239. int err = -ENOMEM;
  240. if (!pskb_may_pull(skb, sizeof(*ah)))
  241. goto out;
  242. ah = (struct ip_auth_hdr *)skb->data;
  243. ahp = x->data;
  244. ahash = ahp->ahash;
  245. nexthdr = ah->nexthdr;
  246. ah_hlen = (ah->hdrlen + 2) << 2;
  247. if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
  248. ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
  249. goto out;
  250. if (!pskb_may_pull(skb, ah_hlen))
  251. goto out;
  252. /* We are going to _remove_ AH header to keep sockets happy,
  253. * so... Later this can change. */
  254. if (skb_cloned(skb) &&
  255. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  256. goto out;
  257. skb->ip_summed = CHECKSUM_NONE;
  258. ah = (struct ip_auth_hdr *)skb->data;
  259. iph = ip_hdr(skb);
  260. ihl = ip_hdrlen(skb);
  261. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  262. goto out;
  263. nfrags = err;
  264. work_iph = ah_alloc_tmp(ahash, nfrags, ihl + ahp->icv_trunc_len);
  265. if (!work_iph)
  266. goto out;
  267. auth_data = ah_tmp_auth(work_iph, ihl);
  268. icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len);
  269. req = ah_tmp_req(ahash, icv);
  270. sg = ah_req_sg(ahash, req);
  271. memcpy(work_iph, iph, ihl);
  272. memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
  273. memset(ah->auth_data, 0, ahp->icv_trunc_len);
  274. iph->ttl = 0;
  275. iph->tos = 0;
  276. iph->frag_off = 0;
  277. iph->check = 0;
  278. if (ihl > sizeof(*iph)) {
  279. __be32 dummy;
  280. err = ip_clear_mutable_options(iph, &dummy);
  281. if (err)
  282. goto out_free;
  283. }
  284. skb_push(skb, ihl);
  285. sg_init_table(sg, nfrags);
  286. skb_to_sgvec(skb, sg, 0, skb->len);
  287. ahash_request_set_crypt(req, sg, icv, skb->len);
  288. ahash_request_set_callback(req, 0, ah_input_done, skb);
  289. AH_SKB_CB(skb)->tmp = work_iph;
  290. err = crypto_ahash_digest(req);
  291. if (err) {
  292. if (err == -EINPROGRESS)
  293. goto out;
  294. if (err == -EBUSY)
  295. err = NET_XMIT_DROP;
  296. goto out_free;
  297. }
  298. err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
  299. if (err)
  300. goto out_free;
  301. skb->network_header += ah_hlen;
  302. memcpy(skb_network_header(skb), work_iph, ihl);
  303. __skb_pull(skb, ah_hlen + ihl);
  304. skb_set_transport_header(skb, -ihl);
  305. err = nexthdr;
  306. out_free:
  307. kfree (work_iph);
  308. out:
  309. return err;
  310. }
  311. static void ah4_err(struct sk_buff *skb, u32 info)
  312. {
  313. struct net *net = dev_net(skb->dev);
  314. struct iphdr *iph = (struct iphdr *)skb->data;
  315. struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
  316. struct xfrm_state *x;
  317. if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
  318. icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  319. return;
  320. x = xfrm_state_lookup(net, skb->mark, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET);
  321. if (!x)
  322. return;
  323. printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n",
  324. ntohl(ah->spi), ntohl(iph->daddr));
  325. xfrm_state_put(x);
  326. }
  327. static int ah_init_state(struct xfrm_state *x)
  328. {
  329. struct ah_data *ahp = NULL;
  330. struct xfrm_algo_desc *aalg_desc;
  331. struct crypto_ahash *ahash;
  332. if (!x->aalg)
  333. goto error;
  334. if (x->encap)
  335. goto error;
  336. ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
  337. if (!ahp)
  338. return -ENOMEM;
  339. ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
  340. if (IS_ERR(ahash))
  341. goto error;
  342. ahp->ahash = ahash;
  343. if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
  344. (x->aalg->alg_key_len + 7) / 8))
  345. goto error;
  346. /*
  347. * Lookup the algorithm description maintained by xfrm_algo,
  348. * verify crypto transform properties, and store information
  349. * we need for AH processing. This lookup cannot fail here
  350. * after a successful crypto_alloc_ahash().
  351. */
  352. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  353. BUG_ON(!aalg_desc);
  354. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  355. crypto_ahash_digestsize(ahash)) {
  356. printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
  357. x->aalg->alg_name, crypto_ahash_digestsize(ahash),
  358. aalg_desc->uinfo.auth.icv_fullbits/8);
  359. goto error;
  360. }
  361. ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  362. ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
  363. BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
  364. x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
  365. ahp->icv_trunc_len);
  366. if (x->props.mode == XFRM_MODE_TUNNEL)
  367. x->props.header_len += sizeof(struct iphdr);
  368. x->data = ahp;
  369. return 0;
  370. error:
  371. if (ahp) {
  372. crypto_free_ahash(ahp->ahash);
  373. kfree(ahp);
  374. }
  375. return -EINVAL;
  376. }
  377. static void ah_destroy(struct xfrm_state *x)
  378. {
  379. struct ah_data *ahp = x->data;
  380. if (!ahp)
  381. return;
  382. crypto_free_ahash(ahp->ahash);
  383. kfree(ahp);
  384. }
  385. static const struct xfrm_type ah_type =
  386. {
  387. .description = "AH4",
  388. .owner = THIS_MODULE,
  389. .proto = IPPROTO_AH,
  390. .flags = XFRM_TYPE_REPLAY_PROT,
  391. .init_state = ah_init_state,
  392. .destructor = ah_destroy,
  393. .input = ah_input,
  394. .output = ah_output
  395. };
  396. static const struct net_protocol ah4_protocol = {
  397. .handler = xfrm4_rcv,
  398. .err_handler = ah4_err,
  399. .no_policy = 1,
  400. .netns_ok = 1,
  401. };
  402. static int __init ah4_init(void)
  403. {
  404. if (xfrm_register_type(&ah_type, AF_INET) < 0) {
  405. printk(KERN_INFO "ip ah init: can't add xfrm type\n");
  406. return -EAGAIN;
  407. }
  408. if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) {
  409. printk(KERN_INFO "ip ah init: can't add protocol\n");
  410. xfrm_unregister_type(&ah_type, AF_INET);
  411. return -EAGAIN;
  412. }
  413. return 0;
  414. }
  415. static void __exit ah4_fini(void)
  416. {
  417. if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0)
  418. printk(KERN_INFO "ip ah close: can't remove protocol\n");
  419. if (xfrm_unregister_type(&ah_type, AF_INET) < 0)
  420. printk(KERN_INFO "ip ah close: can't remove xfrm type\n");
  421. }
  422. module_init(ah4_init);
  423. module_exit(ah4_fini);
  424. MODULE_LICENSE("GPL");
  425. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH);