ah4.c 12 KB

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