ah4.c 12 KB

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