ah4.c 7.5 KB

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