ah4.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /* 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, __be32 *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 IPOPT_CIPSO:
  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, 0, optlen);
  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. skb_push(skb, -skb_network_offset(skb));
  63. top_iph = ip_hdr(skb);
  64. iph = &tmp_iph.iph;
  65. iph->tos = top_iph->tos;
  66. iph->ttl = top_iph->ttl;
  67. iph->frag_off = top_iph->frag_off;
  68. if (top_iph->ihl != 5) {
  69. iph->daddr = top_iph->daddr;
  70. memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  71. err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
  72. if (err)
  73. goto error;
  74. }
  75. ah = ip_auth_hdr(skb);
  76. ah->nexthdr = *skb_mac_header(skb);
  77. *skb_mac_header(skb) = IPPROTO_AH;
  78. top_iph->tos = 0;
  79. top_iph->tot_len = htons(skb->len);
  80. top_iph->frag_off = 0;
  81. top_iph->ttl = 0;
  82. top_iph->check = 0;
  83. ahp = x->data;
  84. ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
  85. ah->reserved = 0;
  86. ah->spi = x->id.spi;
  87. ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
  88. spin_lock_bh(&x->lock);
  89. err = ah_mac_digest(ahp, skb, ah->auth_data);
  90. memcpy(ah->auth_data, ahp->work_icv, ahp->icv_trunc_len);
  91. spin_unlock_bh(&x->lock);
  92. if (err)
  93. goto error;
  94. top_iph->tos = iph->tos;
  95. top_iph->ttl = iph->ttl;
  96. top_iph->frag_off = iph->frag_off;
  97. if (top_iph->ihl != 5) {
  98. top_iph->daddr = iph->daddr;
  99. memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
  100. }
  101. err = 0;
  102. error:
  103. return err;
  104. }
  105. static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
  106. {
  107. int ah_hlen;
  108. int ihl;
  109. int nexthdr;
  110. int err = -EINVAL;
  111. struct iphdr *iph;
  112. struct ip_auth_hdr *ah;
  113. struct ah_data *ahp;
  114. char work_buf[60];
  115. if (!pskb_may_pull(skb, sizeof(*ah)))
  116. goto out;
  117. ah = (struct ip_auth_hdr *)skb->data;
  118. ahp = x->data;
  119. nexthdr = ah->nexthdr;
  120. ah_hlen = (ah->hdrlen + 2) << 2;
  121. if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
  122. ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
  123. goto out;
  124. if (!pskb_may_pull(skb, ah_hlen))
  125. goto out;
  126. /* We are going to _remove_ AH header to keep sockets happy,
  127. * so... Later this can change. */
  128. if (skb_cloned(skb) &&
  129. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  130. goto out;
  131. skb->ip_summed = CHECKSUM_NONE;
  132. ah = (struct ip_auth_hdr *)skb->data;
  133. iph = ip_hdr(skb);
  134. ihl = skb->data - skb_network_header(skb);
  135. memcpy(work_buf, iph, ihl);
  136. iph->ttl = 0;
  137. iph->tos = 0;
  138. iph->frag_off = 0;
  139. iph->check = 0;
  140. if (ihl > sizeof(*iph)) {
  141. __be32 dummy;
  142. if (ip_clear_mutable_options(iph, &dummy))
  143. goto out;
  144. }
  145. spin_lock(&x->lock);
  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 unlock;
  153. if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len))
  154. err = -EBADMSG;
  155. }
  156. unlock:
  157. spin_unlock(&x->lock);
  158. if (err)
  159. goto out;
  160. skb->network_header += ah_hlen;
  161. memcpy(skb_network_header(skb), work_buf, ihl);
  162. skb->transport_header = skb->network_header;
  163. __skb_pull(skb, ah_hlen + ihl);
  164. return nexthdr;
  165. out:
  166. return err;
  167. }
  168. static void ah4_err(struct sk_buff *skb, u32 info)
  169. {
  170. struct iphdr *iph = (struct iphdr*)skb->data;
  171. struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+(iph->ihl<<2));
  172. struct xfrm_state *x;
  173. if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
  174. icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  175. return;
  176. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET);
  177. if (!x)
  178. return;
  179. printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n",
  180. ntohl(ah->spi), ntohl(iph->daddr));
  181. xfrm_state_put(x);
  182. }
  183. static int ah_init_state(struct xfrm_state *x)
  184. {
  185. struct ah_data *ahp = NULL;
  186. struct xfrm_algo_desc *aalg_desc;
  187. struct crypto_hash *tfm;
  188. if (!x->aalg)
  189. goto error;
  190. if (x->encap)
  191. goto error;
  192. ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
  193. if (ahp == NULL)
  194. return -ENOMEM;
  195. tfm = crypto_alloc_hash(x->aalg->alg_name, 0, CRYPTO_ALG_ASYNC);
  196. if (IS_ERR(tfm))
  197. goto error;
  198. ahp->tfm = tfm;
  199. if (crypto_hash_setkey(tfm, x->aalg->alg_key,
  200. (x->aalg->alg_key_len + 7) / 8))
  201. goto error;
  202. /*
  203. * Lookup the algorithm description maintained by xfrm_algo,
  204. * verify crypto transform properties, and store information
  205. * we need for AH processing. This lookup cannot fail here
  206. * after a successful crypto_alloc_hash().
  207. */
  208. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  209. BUG_ON(!aalg_desc);
  210. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  211. crypto_hash_digestsize(tfm)) {
  212. printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
  213. x->aalg->alg_name, crypto_hash_digestsize(tfm),
  214. aalg_desc->uinfo.auth.icv_fullbits/8);
  215. goto error;
  216. }
  217. ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  218. ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
  219. BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
  220. ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL);
  221. if (!ahp->work_icv)
  222. goto error;
  223. x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
  224. ahp->icv_trunc_len);
  225. if (x->props.mode == XFRM_MODE_TUNNEL)
  226. x->props.header_len += sizeof(struct iphdr);
  227. x->data = ahp;
  228. return 0;
  229. error:
  230. if (ahp) {
  231. kfree(ahp->work_icv);
  232. crypto_free_hash(ahp->tfm);
  233. kfree(ahp);
  234. }
  235. return -EINVAL;
  236. }
  237. static void ah_destroy(struct xfrm_state *x)
  238. {
  239. struct ah_data *ahp = x->data;
  240. if (!ahp)
  241. return;
  242. kfree(ahp->work_icv);
  243. ahp->work_icv = NULL;
  244. crypto_free_hash(ahp->tfm);
  245. ahp->tfm = NULL;
  246. kfree(ahp);
  247. }
  248. static const struct xfrm_type ah_type =
  249. {
  250. .description = "AH4",
  251. .owner = THIS_MODULE,
  252. .proto = IPPROTO_AH,
  253. .flags = XFRM_TYPE_REPLAY_PROT,
  254. .init_state = ah_init_state,
  255. .destructor = ah_destroy,
  256. .input = ah_input,
  257. .output = ah_output
  258. };
  259. static struct net_protocol ah4_protocol = {
  260. .handler = xfrm4_rcv,
  261. .err_handler = ah4_err,
  262. .no_policy = 1,
  263. };
  264. static int __init ah4_init(void)
  265. {
  266. if (xfrm_register_type(&ah_type, AF_INET) < 0) {
  267. printk(KERN_INFO "ip ah init: can't add xfrm type\n");
  268. return -EAGAIN;
  269. }
  270. if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) {
  271. printk(KERN_INFO "ip ah init: can't add protocol\n");
  272. xfrm_unregister_type(&ah_type, AF_INET);
  273. return -EAGAIN;
  274. }
  275. return 0;
  276. }
  277. static void __exit ah4_fini(void)
  278. {
  279. if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0)
  280. printk(KERN_INFO "ip ah close: can't remove protocol\n");
  281. if (xfrm_unregister_type(&ah_type, AF_INET) < 0)
  282. printk(KERN_INFO "ip ah close: can't remove xfrm type\n");
  283. }
  284. module_init(ah4_init);
  285. module_exit(ah4_fini);
  286. MODULE_LICENSE("GPL");
  287. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH);