ah6.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright (C)2002 USAGI/WIDE Project
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Authors
  19. *
  20. * Mitsuru KANDA @USAGI : IPv6 Support
  21. * Kazunori MIYAZAWA @USAGI :
  22. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  23. *
  24. * This file is derived from net/ipv4/ah.c.
  25. */
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <net/ip.h>
  29. #include <net/ah.h>
  30. #include <linux/crypto.h>
  31. #include <linux/pfkeyv2.h>
  32. #include <linux/string.h>
  33. #include <net/icmp.h>
  34. #include <net/ipv6.h>
  35. #include <net/protocol.h>
  36. #include <net/xfrm.h>
  37. #include <asm/scatterlist.h>
  38. static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
  39. {
  40. u8 *opt = (u8 *)opthdr;
  41. int len = ipv6_optlen(opthdr);
  42. int off = 0;
  43. int optlen = 0;
  44. off += 2;
  45. len -= 2;
  46. while (len > 0) {
  47. switch (opt[off]) {
  48. case IPV6_TLV_PAD0:
  49. optlen = 1;
  50. break;
  51. default:
  52. if (len < 2)
  53. goto bad;
  54. optlen = opt[off+1]+2;
  55. if (len < optlen)
  56. goto bad;
  57. if (opt[off] & 0x20)
  58. memset(&opt[off+2], 0, opt[off+1]);
  59. break;
  60. }
  61. off += optlen;
  62. len -= optlen;
  63. }
  64. if (len == 0)
  65. return 1;
  66. bad:
  67. return 0;
  68. }
  69. /**
  70. * ipv6_rearrange_rthdr - rearrange IPv6 routing header
  71. * @iph: IPv6 header
  72. * @rthdr: routing header
  73. *
  74. * Rearrange the destination address in @iph and the addresses in @rthdr
  75. * so that they appear in the order they will at the final destination.
  76. * See Appendix A2 of RFC 2402 for details.
  77. */
  78. static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
  79. {
  80. int segments, segments_left;
  81. struct in6_addr *addrs;
  82. struct in6_addr final_addr;
  83. segments_left = rthdr->segments_left;
  84. if (segments_left == 0)
  85. return;
  86. rthdr->segments_left = 0;
  87. /* The value of rthdr->hdrlen has been verified either by the system
  88. * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
  89. * packets. So we can assume that it is even and that segments is
  90. * greater than or equal to segments_left.
  91. *
  92. * For the same reason we can assume that this option is of type 0.
  93. */
  94. segments = rthdr->hdrlen >> 1;
  95. addrs = ((struct rt0_hdr *)rthdr)->addr;
  96. ipv6_addr_copy(&final_addr, addrs + segments - 1);
  97. addrs += segments - segments_left;
  98. memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs));
  99. ipv6_addr_copy(addrs, &iph->daddr);
  100. ipv6_addr_copy(&iph->daddr, &final_addr);
  101. }
  102. static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len)
  103. {
  104. union {
  105. struct ipv6hdr *iph;
  106. struct ipv6_opt_hdr *opth;
  107. struct ipv6_rt_hdr *rth;
  108. char *raw;
  109. } exthdr = { .iph = iph };
  110. char *end = exthdr.raw + len;
  111. int nexthdr = iph->nexthdr;
  112. exthdr.iph++;
  113. while (exthdr.raw < end) {
  114. switch (nexthdr) {
  115. case NEXTHDR_HOP:
  116. case NEXTHDR_DEST:
  117. if (!zero_out_mutable_opts(exthdr.opth)) {
  118. LIMIT_NETDEBUG(
  119. KERN_WARNING "overrun %sopts\n",
  120. nexthdr == NEXTHDR_HOP ?
  121. "hop" : "dest");
  122. return -EINVAL;
  123. }
  124. break;
  125. case NEXTHDR_ROUTING:
  126. ipv6_rearrange_rthdr(iph, exthdr.rth);
  127. break;
  128. default :
  129. return 0;
  130. }
  131. nexthdr = exthdr.opth->nexthdr;
  132. exthdr.raw += ipv6_optlen(exthdr.opth);
  133. }
  134. return 0;
  135. }
  136. static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
  137. {
  138. int err;
  139. int extlen;
  140. struct ipv6hdr *top_iph;
  141. struct ip_auth_hdr *ah;
  142. struct ah_data *ahp;
  143. u8 nexthdr;
  144. char tmp_base[8];
  145. struct {
  146. struct in6_addr daddr;
  147. char hdrs[0];
  148. } *tmp_ext;
  149. top_iph = (struct ipv6hdr *)skb->data;
  150. top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
  151. nexthdr = *skb->nh.raw;
  152. *skb->nh.raw = IPPROTO_AH;
  153. /* When there are no extension headers, we only need to save the first
  154. * 8 bytes of the base IP header.
  155. */
  156. memcpy(tmp_base, top_iph, sizeof(tmp_base));
  157. tmp_ext = NULL;
  158. extlen = skb->h.raw - (unsigned char *)(top_iph + 1);
  159. if (extlen) {
  160. extlen += sizeof(*tmp_ext);
  161. tmp_ext = kmalloc(extlen, GFP_ATOMIC);
  162. if (!tmp_ext) {
  163. err = -ENOMEM;
  164. goto error;
  165. }
  166. memcpy(tmp_ext, &top_iph->daddr, extlen);
  167. err = ipv6_clear_mutable_options(top_iph,
  168. extlen - sizeof(*tmp_ext) +
  169. sizeof(*top_iph));
  170. if (err)
  171. goto error_free_iph;
  172. }
  173. ah = (struct ip_auth_hdr *)skb->h.raw;
  174. ah->nexthdr = nexthdr;
  175. top_iph->priority = 0;
  176. top_iph->flow_lbl[0] = 0;
  177. top_iph->flow_lbl[1] = 0;
  178. top_iph->flow_lbl[2] = 0;
  179. top_iph->hop_limit = 0;
  180. ahp = x->data;
  181. ah->hdrlen = (XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) +
  182. ahp->icv_trunc_len) >> 2) - 2;
  183. ah->reserved = 0;
  184. ah->spi = x->id.spi;
  185. ah->seq_no = htonl(++x->replay.oseq);
  186. ahp->icv(ahp, skb, ah->auth_data);
  187. err = 0;
  188. memcpy(top_iph, tmp_base, sizeof(tmp_base));
  189. if (tmp_ext) {
  190. memcpy(&top_iph->daddr, tmp_ext, extlen);
  191. error_free_iph:
  192. kfree(tmp_ext);
  193. }
  194. error:
  195. return err;
  196. }
  197. static int ah6_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
  198. {
  199. /*
  200. * Before process AH
  201. * [IPv6][Ext1][Ext2][AH][Dest][Payload]
  202. * |<-------------->| hdr_len
  203. *
  204. * To erase AH:
  205. * Keeping copy of cleared headers. After AH processing,
  206. * Moving the pointer of skb->nh.raw by using skb_pull as long as AH
  207. * header length. Then copy back the copy as long as hdr_len
  208. * If destination header following AH exists, copy it into after [Ext2].
  209. *
  210. * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
  211. * There is offset of AH before IPv6 header after the process.
  212. */
  213. struct ipv6_auth_hdr *ah;
  214. struct ah_data *ahp;
  215. unsigned char *tmp_hdr = NULL;
  216. u16 hdr_len;
  217. u16 ah_hlen;
  218. int nexthdr;
  219. if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
  220. goto out;
  221. /* We are going to _remove_ AH header to keep sockets happy,
  222. * so... Later this can change. */
  223. if (skb_cloned(skb) &&
  224. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  225. goto out;
  226. hdr_len = skb->data - skb->nh.raw;
  227. ah = (struct ipv6_auth_hdr*)skb->data;
  228. ahp = x->data;
  229. nexthdr = ah->nexthdr;
  230. ah_hlen = (ah->hdrlen + 2) << 2;
  231. if (ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_full_len) &&
  232. ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len))
  233. goto out;
  234. if (!pskb_may_pull(skb, ah_hlen))
  235. goto out;
  236. tmp_hdr = kmalloc(hdr_len, GFP_ATOMIC);
  237. if (!tmp_hdr)
  238. goto out;
  239. memcpy(tmp_hdr, skb->nh.raw, hdr_len);
  240. if (ipv6_clear_mutable_options(skb->nh.ipv6h, hdr_len))
  241. goto out;
  242. skb->nh.ipv6h->priority = 0;
  243. skb->nh.ipv6h->flow_lbl[0] = 0;
  244. skb->nh.ipv6h->flow_lbl[1] = 0;
  245. skb->nh.ipv6h->flow_lbl[2] = 0;
  246. skb->nh.ipv6h->hop_limit = 0;
  247. {
  248. u8 auth_data[MAX_AH_AUTH_LEN];
  249. memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
  250. memset(ah->auth_data, 0, ahp->icv_trunc_len);
  251. skb_push(skb, skb->data - skb->nh.raw);
  252. ahp->icv(ahp, skb, ah->auth_data);
  253. if (memcmp(ah->auth_data, auth_data, ahp->icv_trunc_len)) {
  254. LIMIT_NETDEBUG(KERN_WARNING "ipsec ah authentication error\n");
  255. x->stats.integrity_failed++;
  256. goto free_out;
  257. }
  258. }
  259. skb->nh.raw = skb_pull(skb, ah_hlen);
  260. memcpy(skb->nh.raw, tmp_hdr, hdr_len);
  261. skb->nh.ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  262. skb_pull(skb, hdr_len);
  263. skb->h.raw = skb->data;
  264. kfree(tmp_hdr);
  265. return nexthdr;
  266. free_out:
  267. kfree(tmp_hdr);
  268. out:
  269. return -EINVAL;
  270. }
  271. static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  272. int type, int code, int offset, __u32 info)
  273. {
  274. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  275. struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
  276. struct xfrm_state *x;
  277. if (type != ICMPV6_DEST_UNREACH &&
  278. type != ICMPV6_PKT_TOOBIG)
  279. return;
  280. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
  281. if (!x)
  282. return;
  283. NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/" NIP6_FMT "\n",
  284. ntohl(ah->spi), NIP6(iph->daddr));
  285. xfrm_state_put(x);
  286. }
  287. static int ah6_init_state(struct xfrm_state *x)
  288. {
  289. struct ah_data *ahp = NULL;
  290. struct xfrm_algo_desc *aalg_desc;
  291. if (!x->aalg)
  292. goto error;
  293. /* null auth can use a zero length key */
  294. if (x->aalg->alg_key_len > 512)
  295. goto error;
  296. if (x->encap)
  297. goto error;
  298. ahp = kmalloc(sizeof(*ahp), GFP_KERNEL);
  299. if (ahp == NULL)
  300. return -ENOMEM;
  301. memset(ahp, 0, sizeof(*ahp));
  302. ahp->key = x->aalg->alg_key;
  303. ahp->key_len = (x->aalg->alg_key_len+7)/8;
  304. ahp->tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
  305. if (!ahp->tfm)
  306. goto error;
  307. ahp->icv = ah_hmac_digest;
  308. /*
  309. * Lookup the algorithm description maintained by xfrm_algo,
  310. * verify crypto transform properties, and store information
  311. * we need for AH processing. This lookup cannot fail here
  312. * after a successful crypto_alloc_tfm().
  313. */
  314. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  315. BUG_ON(!aalg_desc);
  316. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  317. crypto_tfm_alg_digestsize(ahp->tfm)) {
  318. printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
  319. x->aalg->alg_name, crypto_tfm_alg_digestsize(ahp->tfm),
  320. aalg_desc->uinfo.auth.icv_fullbits/8);
  321. goto error;
  322. }
  323. ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  324. ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
  325. BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
  326. ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL);
  327. if (!ahp->work_icv)
  328. goto error;
  329. x->props.header_len = XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len);
  330. if (x->props.mode)
  331. x->props.header_len += sizeof(struct ipv6hdr);
  332. x->data = ahp;
  333. return 0;
  334. error:
  335. if (ahp) {
  336. kfree(ahp->work_icv);
  337. crypto_free_tfm(ahp->tfm);
  338. kfree(ahp);
  339. }
  340. return -EINVAL;
  341. }
  342. static void ah6_destroy(struct xfrm_state *x)
  343. {
  344. struct ah_data *ahp = x->data;
  345. if (!ahp)
  346. return;
  347. kfree(ahp->work_icv);
  348. ahp->work_icv = NULL;
  349. crypto_free_tfm(ahp->tfm);
  350. ahp->tfm = NULL;
  351. kfree(ahp);
  352. }
  353. static struct xfrm_type ah6_type =
  354. {
  355. .description = "AH6",
  356. .owner = THIS_MODULE,
  357. .proto = IPPROTO_AH,
  358. .init_state = ah6_init_state,
  359. .destructor = ah6_destroy,
  360. .input = ah6_input,
  361. .output = ah6_output
  362. };
  363. static struct inet6_protocol ah6_protocol = {
  364. .handler = xfrm6_rcv,
  365. .err_handler = ah6_err,
  366. .flags = INET6_PROTO_NOPOLICY,
  367. };
  368. static int __init ah6_init(void)
  369. {
  370. if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
  371. printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n");
  372. return -EAGAIN;
  373. }
  374. if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) {
  375. printk(KERN_INFO "ipv6 ah init: can't add protocol\n");
  376. xfrm_unregister_type(&ah6_type, AF_INET6);
  377. return -EAGAIN;
  378. }
  379. return 0;
  380. }
  381. static void __exit ah6_fini(void)
  382. {
  383. if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0)
  384. printk(KERN_INFO "ipv6 ah close: can't remove protocol\n");
  385. if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0)
  386. printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n");
  387. }
  388. module_init(ah6_init);
  389. module_exit(ah6_fini);
  390. MODULE_LICENSE("GPL");