ah6.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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/module.h>
  27. #include <net/ip.h>
  28. #include <net/ah.h>
  29. #include <linux/crypto.h>
  30. #include <linux/pfkeyv2.h>
  31. #include <linux/string.h>
  32. #include <net/icmp.h>
  33. #include <net/ipv6.h>
  34. #include <net/protocol.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(
  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. xfrm_aevent_doreplay(x);
  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 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 free_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, hdr_len);
  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->h.raw = memcpy(skb->nh.raw += ah_hlen, tmp_hdr, hdr_len);
  260. __skb_pull(skb, ah_hlen + hdr_len);
  261. kfree(tmp_hdr);
  262. return nexthdr;
  263. free_out:
  264. kfree(tmp_hdr);
  265. out:
  266. return -EINVAL;
  267. }
  268. static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  269. int type, int code, int offset, __u32 info)
  270. {
  271. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  272. struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
  273. struct xfrm_state *x;
  274. if (type != ICMPV6_DEST_UNREACH &&
  275. type != ICMPV6_PKT_TOOBIG)
  276. return;
  277. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
  278. if (!x)
  279. return;
  280. NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/" NIP6_FMT "\n",
  281. ntohl(ah->spi), NIP6(iph->daddr));
  282. xfrm_state_put(x);
  283. }
  284. static int ah6_init_state(struct xfrm_state *x)
  285. {
  286. struct ah_data *ahp = NULL;
  287. struct xfrm_algo_desc *aalg_desc;
  288. if (!x->aalg)
  289. goto error;
  290. /* null auth can use a zero length key */
  291. if (x->aalg->alg_key_len > 512)
  292. goto error;
  293. if (x->encap)
  294. goto error;
  295. ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
  296. if (ahp == NULL)
  297. return -ENOMEM;
  298. ahp->key = x->aalg->alg_key;
  299. ahp->key_len = (x->aalg->alg_key_len+7)/8;
  300. ahp->tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
  301. if (!ahp->tfm)
  302. goto error;
  303. ahp->icv = ah_hmac_digest;
  304. /*
  305. * Lookup the algorithm description maintained by xfrm_algo,
  306. * verify crypto transform properties, and store information
  307. * we need for AH processing. This lookup cannot fail here
  308. * after a successful crypto_alloc_tfm().
  309. */
  310. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  311. BUG_ON(!aalg_desc);
  312. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  313. crypto_tfm_alg_digestsize(ahp->tfm)) {
  314. printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
  315. x->aalg->alg_name, crypto_tfm_alg_digestsize(ahp->tfm),
  316. aalg_desc->uinfo.auth.icv_fullbits/8);
  317. goto error;
  318. }
  319. ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  320. ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
  321. BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
  322. ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL);
  323. if (!ahp->work_icv)
  324. goto error;
  325. x->props.header_len = XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len);
  326. if (x->props.mode)
  327. x->props.header_len += sizeof(struct ipv6hdr);
  328. x->data = ahp;
  329. return 0;
  330. error:
  331. if (ahp) {
  332. kfree(ahp->work_icv);
  333. crypto_free_tfm(ahp->tfm);
  334. kfree(ahp);
  335. }
  336. return -EINVAL;
  337. }
  338. static void ah6_destroy(struct xfrm_state *x)
  339. {
  340. struct ah_data *ahp = x->data;
  341. if (!ahp)
  342. return;
  343. kfree(ahp->work_icv);
  344. ahp->work_icv = NULL;
  345. crypto_free_tfm(ahp->tfm);
  346. ahp->tfm = NULL;
  347. kfree(ahp);
  348. }
  349. static struct xfrm_type ah6_type =
  350. {
  351. .description = "AH6",
  352. .owner = THIS_MODULE,
  353. .proto = IPPROTO_AH,
  354. .init_state = ah6_init_state,
  355. .destructor = ah6_destroy,
  356. .input = ah6_input,
  357. .output = ah6_output
  358. };
  359. static struct inet6_protocol ah6_protocol = {
  360. .handler = xfrm6_rcv,
  361. .err_handler = ah6_err,
  362. .flags = INET6_PROTO_NOPOLICY,
  363. };
  364. static int __init ah6_init(void)
  365. {
  366. if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
  367. printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n");
  368. return -EAGAIN;
  369. }
  370. if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) {
  371. printk(KERN_INFO "ipv6 ah init: can't add protocol\n");
  372. xfrm_unregister_type(&ah6_type, AF_INET6);
  373. return -EAGAIN;
  374. }
  375. return 0;
  376. }
  377. static void __exit ah6_fini(void)
  378. {
  379. if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0)
  380. printk(KERN_INFO "ipv6 ah close: can't remove protocol\n");
  381. if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0)
  382. printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n");
  383. }
  384. module_init(ah6_init);
  385. module_exit(ah6_fini);
  386. MODULE_LICENSE("GPL");