ah4.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include <linux/config.h>
  2. #include <linux/module.h>
  3. #include <net/ip.h>
  4. #include <net/xfrm.h>
  5. #include <net/ah.h>
  6. #include <linux/crypto.h>
  7. #include <linux/pfkeyv2.h>
  8. #include <net/icmp.h>
  9. #include <net/protocol.h>
  10. #include <asm/scatterlist.h>
  11. /* Clear mutable options and find final destination to substitute
  12. * into IP header for icv calculation. Options are already checked
  13. * for validity, so paranoia is not required. */
  14. static int ip_clear_mutable_options(struct iphdr *iph, u32 *daddr)
  15. {
  16. unsigned char * optptr = (unsigned char*)(iph+1);
  17. int l = iph->ihl*4 - sizeof(struct iphdr);
  18. int optlen;
  19. while (l > 0) {
  20. switch (*optptr) {
  21. case IPOPT_END:
  22. return 0;
  23. case IPOPT_NOOP:
  24. l--;
  25. optptr++;
  26. continue;
  27. }
  28. optlen = optptr[1];
  29. if (optlen<2 || optlen>l)
  30. return -EINVAL;
  31. switch (*optptr) {
  32. case IPOPT_SEC:
  33. case 0x85: /* Some "Extended Security" crap. */
  34. case 0x86: /* Another "Commercial Security" crap. */
  35. case IPOPT_RA:
  36. case 0x80|21: /* RFC1770 */
  37. break;
  38. case IPOPT_LSRR:
  39. case IPOPT_SSRR:
  40. if (optlen < 6)
  41. return -EINVAL;
  42. memcpy(daddr, optptr+optlen-4, 4);
  43. /* Fall through */
  44. default:
  45. memset(optptr+2, 0, optlen-2);
  46. }
  47. l -= optlen;
  48. optptr += optlen;
  49. }
  50. return 0;
  51. }
  52. static int ah_output(struct xfrm_state *x, struct sk_buff *skb)
  53. {
  54. int err;
  55. struct iphdr *iph, *top_iph;
  56. struct ip_auth_hdr *ah;
  57. struct ah_data *ahp;
  58. union {
  59. struct iphdr iph;
  60. char buf[60];
  61. } tmp_iph;
  62. top_iph = skb->nh.iph;
  63. iph = &tmp_iph.iph;
  64. iph->tos = top_iph->tos;
  65. iph->ttl = top_iph->ttl;
  66. iph->frag_off = top_iph->frag_off;
  67. if (top_iph->ihl != 5) {
  68. iph->daddr = top_iph->daddr;
  69. memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  70. err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
  71. if (err)
  72. goto error;
  73. }
  74. ah = (struct ip_auth_hdr *)((char *)top_iph+top_iph->ihl*4);
  75. ah->nexthdr = top_iph->protocol;
  76. top_iph->tos = 0;
  77. top_iph->tot_len = htons(skb->len);
  78. top_iph->frag_off = 0;
  79. top_iph->ttl = 0;
  80. top_iph->protocol = IPPROTO_AH;
  81. top_iph->check = 0;
  82. ahp = x->data;
  83. ah->hdrlen = (XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
  84. ahp->icv_trunc_len) >> 2) - 2;
  85. ah->reserved = 0;
  86. ah->spi = x->id.spi;
  87. ah->seq_no = htonl(++x->replay.oseq);
  88. xfrm_aevent_doreplay(x);
  89. ahp->icv(ahp, skb, ah->auth_data);
  90. top_iph->tos = iph->tos;
  91. top_iph->ttl = iph->ttl;
  92. top_iph->frag_off = iph->frag_off;
  93. if (top_iph->ihl != 5) {
  94. top_iph->daddr = iph->daddr;
  95. memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  96. }
  97. ip_send_check(top_iph);
  98. err = 0;
  99. error:
  100. return err;
  101. }
  102. static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
  103. {
  104. int ah_hlen;
  105. struct iphdr *iph;
  106. struct ip_auth_hdr *ah;
  107. struct ah_data *ahp;
  108. char work_buf[60];
  109. if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
  110. goto out;
  111. ah = (struct ip_auth_hdr*)skb->data;
  112. ahp = x->data;
  113. ah_hlen = (ah->hdrlen + 2) << 2;
  114. if (ah_hlen != XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_full_len) &&
  115. ah_hlen != XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_trunc_len))
  116. goto out;
  117. if (!pskb_may_pull(skb, ah_hlen))
  118. goto out;
  119. /* We are going to _remove_ AH header to keep sockets happy,
  120. * so... Later this can change. */
  121. if (skb_cloned(skb) &&
  122. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  123. goto out;
  124. skb->ip_summed = CHECKSUM_NONE;
  125. ah = (struct ip_auth_hdr*)skb->data;
  126. iph = skb->nh.iph;
  127. memcpy(work_buf, iph, iph->ihl*4);
  128. iph->ttl = 0;
  129. iph->tos = 0;
  130. iph->frag_off = 0;
  131. iph->check = 0;
  132. if (iph->ihl != 5) {
  133. u32 dummy;
  134. if (ip_clear_mutable_options(iph, &dummy))
  135. goto out;
  136. }
  137. {
  138. u8 auth_data[MAX_AH_AUTH_LEN];
  139. memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
  140. skb_push(skb, skb->data - skb->nh.raw);
  141. ahp->icv(ahp, skb, ah->auth_data);
  142. if (memcmp(ah->auth_data, auth_data, ahp->icv_trunc_len)) {
  143. x->stats.integrity_failed++;
  144. goto out;
  145. }
  146. }
  147. ((struct iphdr*)work_buf)->protocol = ah->nexthdr;
  148. skb->nh.raw = skb_pull(skb, ah_hlen);
  149. memcpy(skb->nh.raw, work_buf, iph->ihl*4);
  150. skb->nh.iph->tot_len = htons(skb->len);
  151. skb_pull(skb, skb->nh.iph->ihl*4);
  152. skb->h.raw = skb->data;
  153. return 0;
  154. out:
  155. return -EINVAL;
  156. }
  157. static void ah4_err(struct sk_buff *skb, u32 info)
  158. {
  159. struct iphdr *iph = (struct iphdr*)skb->data;
  160. struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+(iph->ihl<<2));
  161. struct xfrm_state *x;
  162. if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
  163. skb->h.icmph->code != ICMP_FRAG_NEEDED)
  164. return;
  165. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET);
  166. if (!x)
  167. return;
  168. printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n",
  169. ntohl(ah->spi), ntohl(iph->daddr));
  170. xfrm_state_put(x);
  171. }
  172. static int ah_init_state(struct xfrm_state *x)
  173. {
  174. struct ah_data *ahp = NULL;
  175. struct xfrm_algo_desc *aalg_desc;
  176. if (!x->aalg)
  177. goto error;
  178. /* null auth can use a zero length key */
  179. if (x->aalg->alg_key_len > 512)
  180. goto error;
  181. if (x->encap)
  182. goto error;
  183. ahp = kmalloc(sizeof(*ahp), GFP_KERNEL);
  184. if (ahp == NULL)
  185. return -ENOMEM;
  186. memset(ahp, 0, sizeof(*ahp));
  187. ahp->key = x->aalg->alg_key;
  188. ahp->key_len = (x->aalg->alg_key_len+7)/8;
  189. ahp->tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
  190. if (!ahp->tfm)
  191. goto error;
  192. ahp->icv = ah_hmac_digest;
  193. /*
  194. * Lookup the algorithm description maintained by xfrm_algo,
  195. * verify crypto transform properties, and store information
  196. * we need for AH processing. This lookup cannot fail here
  197. * after a successful crypto_alloc_tfm().
  198. */
  199. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  200. BUG_ON(!aalg_desc);
  201. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  202. crypto_tfm_alg_digestsize(ahp->tfm)) {
  203. printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
  204. x->aalg->alg_name, crypto_tfm_alg_digestsize(ahp->tfm),
  205. aalg_desc->uinfo.auth.icv_fullbits/8);
  206. goto error;
  207. }
  208. ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  209. ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
  210. BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
  211. ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL);
  212. if (!ahp->work_icv)
  213. goto error;
  214. x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_trunc_len);
  215. if (x->props.mode)
  216. x->props.header_len += sizeof(struct iphdr);
  217. x->data = ahp;
  218. return 0;
  219. error:
  220. if (ahp) {
  221. kfree(ahp->work_icv);
  222. crypto_free_tfm(ahp->tfm);
  223. kfree(ahp);
  224. }
  225. return -EINVAL;
  226. }
  227. static void ah_destroy(struct xfrm_state *x)
  228. {
  229. struct ah_data *ahp = x->data;
  230. if (!ahp)
  231. return;
  232. kfree(ahp->work_icv);
  233. ahp->work_icv = NULL;
  234. crypto_free_tfm(ahp->tfm);
  235. ahp->tfm = NULL;
  236. kfree(ahp);
  237. }
  238. static struct xfrm_type ah_type =
  239. {
  240. .description = "AH4",
  241. .owner = THIS_MODULE,
  242. .proto = IPPROTO_AH,
  243. .init_state = ah_init_state,
  244. .destructor = ah_destroy,
  245. .input = ah_input,
  246. .output = ah_output
  247. };
  248. static struct net_protocol ah4_protocol = {
  249. .handler = xfrm4_rcv,
  250. .err_handler = ah4_err,
  251. .no_policy = 1,
  252. };
  253. static int __init ah4_init(void)
  254. {
  255. if (xfrm_register_type(&ah_type, AF_INET) < 0) {
  256. printk(KERN_INFO "ip ah init: can't add xfrm type\n");
  257. return -EAGAIN;
  258. }
  259. if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) {
  260. printk(KERN_INFO "ip ah init: can't add protocol\n");
  261. xfrm_unregister_type(&ah_type, AF_INET);
  262. return -EAGAIN;
  263. }
  264. return 0;
  265. }
  266. static void __exit ah4_fini(void)
  267. {
  268. if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0)
  269. printk(KERN_INFO "ip ah close: can't remove protocol\n");
  270. if (xfrm_unregister_type(&ah_type, AF_INET) < 0)
  271. printk(KERN_INFO "ip ah close: can't remove xfrm type\n");
  272. }
  273. module_init(ah4_init);
  274. module_exit(ah4_fini);
  275. MODULE_LICENSE("GPL");