ah6.c 11 KB

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