ah4.c 7.6 KB

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