ah6.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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/spinlock.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. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  70. /**
  71. * ipv6_rearrange_destopt - rearrange IPv6 destination options header
  72. * @iph: IPv6 header
  73. * @destopt: destionation options header
  74. */
  75. static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt)
  76. {
  77. u8 *opt = (u8 *)destopt;
  78. int len = ipv6_optlen(destopt);
  79. int off = 0;
  80. int optlen = 0;
  81. off += 2;
  82. len -= 2;
  83. while (len > 0) {
  84. switch (opt[off]) {
  85. case IPV6_TLV_PAD0:
  86. optlen = 1;
  87. break;
  88. default:
  89. if (len < 2)
  90. goto bad;
  91. optlen = opt[off+1]+2;
  92. if (len < optlen)
  93. goto bad;
  94. /* Rearrange the source address in @iph and the
  95. * addresses in home address option for final source.
  96. * See 11.3.2 of RFC 3775 for details.
  97. */
  98. if (opt[off] == IPV6_TLV_HAO) {
  99. struct in6_addr final_addr;
  100. struct ipv6_destopt_hao *hao;
  101. hao = (struct ipv6_destopt_hao *)&opt[off];
  102. if (hao->length != sizeof(hao->addr)) {
  103. if (net_ratelimit())
  104. printk(KERN_WARNING "destopt hao: invalid header length: %u\n", hao->length);
  105. goto bad;
  106. }
  107. ipv6_addr_copy(&final_addr, &hao->addr);
  108. ipv6_addr_copy(&hao->addr, &iph->saddr);
  109. ipv6_addr_copy(&iph->saddr, &final_addr);
  110. }
  111. break;
  112. }
  113. off += optlen;
  114. len -= optlen;
  115. }
  116. /* Note: ok if len == 0 */
  117. bad:
  118. return;
  119. }
  120. #else
  121. static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {}
  122. #endif
  123. /**
  124. * ipv6_rearrange_rthdr - rearrange IPv6 routing header
  125. * @iph: IPv6 header
  126. * @rthdr: routing header
  127. *
  128. * Rearrange the destination address in @iph and the addresses in @rthdr
  129. * so that they appear in the order they will at the final destination.
  130. * See Appendix A2 of RFC 2402 for details.
  131. */
  132. static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
  133. {
  134. int segments, segments_left;
  135. struct in6_addr *addrs;
  136. struct in6_addr final_addr;
  137. segments_left = rthdr->segments_left;
  138. if (segments_left == 0)
  139. return;
  140. rthdr->segments_left = 0;
  141. /* The value of rthdr->hdrlen has been verified either by the system
  142. * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
  143. * packets. So we can assume that it is even and that segments is
  144. * greater than or equal to segments_left.
  145. *
  146. * For the same reason we can assume that this option is of type 0.
  147. */
  148. segments = rthdr->hdrlen >> 1;
  149. addrs = ((struct rt0_hdr *)rthdr)->addr;
  150. ipv6_addr_copy(&final_addr, addrs + segments - 1);
  151. addrs += segments - segments_left;
  152. memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs));
  153. ipv6_addr_copy(addrs, &iph->daddr);
  154. ipv6_addr_copy(&iph->daddr, &final_addr);
  155. }
  156. static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir)
  157. {
  158. union {
  159. struct ipv6hdr *iph;
  160. struct ipv6_opt_hdr *opth;
  161. struct ipv6_rt_hdr *rth;
  162. char *raw;
  163. } exthdr = { .iph = iph };
  164. char *end = exthdr.raw + len;
  165. int nexthdr = iph->nexthdr;
  166. exthdr.iph++;
  167. while (exthdr.raw < end) {
  168. switch (nexthdr) {
  169. case NEXTHDR_DEST:
  170. if (dir == XFRM_POLICY_OUT)
  171. ipv6_rearrange_destopt(iph, exthdr.opth);
  172. case NEXTHDR_HOP:
  173. if (!zero_out_mutable_opts(exthdr.opth)) {
  174. LIMIT_NETDEBUG(
  175. KERN_WARNING "overrun %sopts\n",
  176. nexthdr == NEXTHDR_HOP ?
  177. "hop" : "dest");
  178. return -EINVAL;
  179. }
  180. break;
  181. case NEXTHDR_ROUTING:
  182. ipv6_rearrange_rthdr(iph, exthdr.rth);
  183. break;
  184. default :
  185. return 0;
  186. }
  187. nexthdr = exthdr.opth->nexthdr;
  188. exthdr.raw += ipv6_optlen(exthdr.opth);
  189. }
  190. return 0;
  191. }
  192. static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
  193. {
  194. int err;
  195. int extlen;
  196. struct ipv6hdr *top_iph;
  197. struct ip_auth_hdr *ah;
  198. struct ah_data *ahp;
  199. u8 nexthdr;
  200. char tmp_base[8];
  201. struct {
  202. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  203. struct in6_addr saddr;
  204. #endif
  205. struct in6_addr daddr;
  206. char hdrs[0];
  207. } *tmp_ext;
  208. skb_push(skb, -skb_network_offset(skb));
  209. top_iph = ipv6_hdr(skb);
  210. top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
  211. nexthdr = *skb_mac_header(skb);
  212. *skb_mac_header(skb) = IPPROTO_AH;
  213. /* When there are no extension headers, we only need to save the first
  214. * 8 bytes of the base IP header.
  215. */
  216. memcpy(tmp_base, top_iph, sizeof(tmp_base));
  217. tmp_ext = NULL;
  218. extlen = skb_transport_offset(skb) - sizeof(struct ipv6hdr);
  219. if (extlen) {
  220. extlen += sizeof(*tmp_ext);
  221. tmp_ext = kmalloc(extlen, GFP_ATOMIC);
  222. if (!tmp_ext) {
  223. err = -ENOMEM;
  224. goto error;
  225. }
  226. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  227. memcpy(tmp_ext, &top_iph->saddr, extlen);
  228. #else
  229. memcpy(tmp_ext, &top_iph->daddr, extlen);
  230. #endif
  231. err = ipv6_clear_mutable_options(top_iph,
  232. extlen - sizeof(*tmp_ext) +
  233. sizeof(*top_iph),
  234. XFRM_POLICY_OUT);
  235. if (err)
  236. goto error_free_iph;
  237. }
  238. ah = ip_auth_hdr(skb);
  239. ah->nexthdr = nexthdr;
  240. top_iph->priority = 0;
  241. top_iph->flow_lbl[0] = 0;
  242. top_iph->flow_lbl[1] = 0;
  243. top_iph->flow_lbl[2] = 0;
  244. top_iph->hop_limit = 0;
  245. ahp = x->data;
  246. ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
  247. ah->reserved = 0;
  248. ah->spi = x->id.spi;
  249. ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
  250. spin_lock_bh(&x->lock);
  251. err = ah_mac_digest(ahp, skb, ah->auth_data);
  252. memcpy(ah->auth_data, ahp->work_icv, ahp->icv_trunc_len);
  253. spin_unlock_bh(&x->lock);
  254. if (err)
  255. goto error_free_iph;
  256. memcpy(top_iph, tmp_base, sizeof(tmp_base));
  257. if (tmp_ext) {
  258. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  259. memcpy(&top_iph->saddr, tmp_ext, extlen);
  260. #else
  261. memcpy(&top_iph->daddr, tmp_ext, extlen);
  262. #endif
  263. error_free_iph:
  264. kfree(tmp_ext);
  265. }
  266. error:
  267. return err;
  268. }
  269. static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
  270. {
  271. /*
  272. * Before process AH
  273. * [IPv6][Ext1][Ext2][AH][Dest][Payload]
  274. * |<-------------->| hdr_len
  275. *
  276. * To erase AH:
  277. * Keeping copy of cleared headers. After AH processing,
  278. * Moving the pointer of skb->network_header by using skb_pull as long
  279. * as AH header length. Then copy back the copy as long as hdr_len
  280. * If destination header following AH exists, copy it into after [Ext2].
  281. *
  282. * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
  283. * There is offset of AH before IPv6 header after the process.
  284. */
  285. struct ip_auth_hdr *ah;
  286. struct ipv6hdr *ip6h;
  287. struct ah_data *ahp;
  288. unsigned char *tmp_hdr = NULL;
  289. u16 hdr_len;
  290. u16 ah_hlen;
  291. int nexthdr;
  292. int err = -EINVAL;
  293. if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
  294. goto out;
  295. /* We are going to _remove_ AH header to keep sockets happy,
  296. * so... Later this can change. */
  297. if (skb_cloned(skb) &&
  298. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  299. goto out;
  300. hdr_len = skb->data - skb_network_header(skb);
  301. ah = (struct ip_auth_hdr *)skb->data;
  302. ahp = x->data;
  303. nexthdr = ah->nexthdr;
  304. ah_hlen = (ah->hdrlen + 2) << 2;
  305. if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
  306. ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
  307. goto out;
  308. if (!pskb_may_pull(skb, ah_hlen))
  309. goto out;
  310. tmp_hdr = kmemdup(skb_network_header(skb), hdr_len, GFP_ATOMIC);
  311. if (!tmp_hdr)
  312. goto out;
  313. ip6h = ipv6_hdr(skb);
  314. if (ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN))
  315. goto free_out;
  316. ip6h->priority = 0;
  317. ip6h->flow_lbl[0] = 0;
  318. ip6h->flow_lbl[1] = 0;
  319. ip6h->flow_lbl[2] = 0;
  320. ip6h->hop_limit = 0;
  321. {
  322. u8 auth_data[MAX_AH_AUTH_LEN];
  323. memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
  324. memset(ah->auth_data, 0, ahp->icv_trunc_len);
  325. skb_push(skb, hdr_len);
  326. err = ah_mac_digest(ahp, skb, ah->auth_data);
  327. if (err)
  328. goto free_out;
  329. err = -EINVAL;
  330. if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len)) {
  331. LIMIT_NETDEBUG(KERN_WARNING "ipsec ah authentication error\n");
  332. x->stats.integrity_failed++;
  333. goto free_out;
  334. }
  335. }
  336. skb->network_header += ah_hlen;
  337. memcpy(skb_network_header(skb), tmp_hdr, hdr_len);
  338. skb->transport_header = skb->network_header;
  339. __skb_pull(skb, ah_hlen + hdr_len);
  340. kfree(tmp_hdr);
  341. return nexthdr;
  342. free_out:
  343. kfree(tmp_hdr);
  344. out:
  345. return err;
  346. }
  347. static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  348. int type, int code, int offset, __be32 info)
  349. {
  350. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  351. struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
  352. struct xfrm_state *x;
  353. if (type != ICMPV6_DEST_UNREACH &&
  354. type != ICMPV6_PKT_TOOBIG)
  355. return;
  356. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
  357. if (!x)
  358. return;
  359. NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/" NIP6_FMT "\n",
  360. ntohl(ah->spi), NIP6(iph->daddr));
  361. xfrm_state_put(x);
  362. }
  363. static int ah6_init_state(struct xfrm_state *x)
  364. {
  365. struct ah_data *ahp = NULL;
  366. struct xfrm_algo_desc *aalg_desc;
  367. struct crypto_hash *tfm;
  368. if (!x->aalg)
  369. goto error;
  370. if (x->encap)
  371. goto error;
  372. ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
  373. if (ahp == NULL)
  374. return -ENOMEM;
  375. tfm = crypto_alloc_hash(x->aalg->alg_name, 0, CRYPTO_ALG_ASYNC);
  376. if (IS_ERR(tfm))
  377. goto error;
  378. ahp->tfm = tfm;
  379. if (crypto_hash_setkey(tfm, x->aalg->alg_key,
  380. (x->aalg->alg_key_len + 7) / 8))
  381. goto error;
  382. /*
  383. * Lookup the algorithm description maintained by xfrm_algo,
  384. * verify crypto transform properties, and store information
  385. * we need for AH processing. This lookup cannot fail here
  386. * after a successful crypto_alloc_hash().
  387. */
  388. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  389. BUG_ON(!aalg_desc);
  390. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  391. crypto_hash_digestsize(tfm)) {
  392. printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
  393. x->aalg->alg_name, crypto_hash_digestsize(tfm),
  394. aalg_desc->uinfo.auth.icv_fullbits/8);
  395. goto error;
  396. }
  397. ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  398. ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
  399. BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
  400. ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL);
  401. if (!ahp->work_icv)
  402. goto error;
  403. x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
  404. ahp->icv_trunc_len);
  405. if (x->props.mode == XFRM_MODE_TUNNEL)
  406. x->props.header_len += sizeof(struct ipv6hdr);
  407. x->data = ahp;
  408. return 0;
  409. error:
  410. if (ahp) {
  411. kfree(ahp->work_icv);
  412. crypto_free_hash(ahp->tfm);
  413. kfree(ahp);
  414. }
  415. return -EINVAL;
  416. }
  417. static void ah6_destroy(struct xfrm_state *x)
  418. {
  419. struct ah_data *ahp = x->data;
  420. if (!ahp)
  421. return;
  422. kfree(ahp->work_icv);
  423. ahp->work_icv = NULL;
  424. crypto_free_hash(ahp->tfm);
  425. ahp->tfm = NULL;
  426. kfree(ahp);
  427. }
  428. static struct xfrm_type ah6_type =
  429. {
  430. .description = "AH6",
  431. .owner = THIS_MODULE,
  432. .proto = IPPROTO_AH,
  433. .flags = XFRM_TYPE_REPLAY_PROT,
  434. .init_state = ah6_init_state,
  435. .destructor = ah6_destroy,
  436. .input = ah6_input,
  437. .output = ah6_output,
  438. .hdr_offset = xfrm6_find_1stfragopt,
  439. };
  440. static struct inet6_protocol ah6_protocol = {
  441. .handler = xfrm6_rcv,
  442. .err_handler = ah6_err,
  443. .flags = INET6_PROTO_NOPOLICY,
  444. };
  445. static int __init ah6_init(void)
  446. {
  447. if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
  448. printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n");
  449. return -EAGAIN;
  450. }
  451. if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) {
  452. printk(KERN_INFO "ipv6 ah init: can't add protocol\n");
  453. xfrm_unregister_type(&ah6_type, AF_INET6);
  454. return -EAGAIN;
  455. }
  456. return 0;
  457. }
  458. static void __exit ah6_fini(void)
  459. {
  460. if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0)
  461. printk(KERN_INFO "ipv6 ah close: can't remove protocol\n");
  462. if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0)
  463. printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n");
  464. }
  465. module_init(ah6_init);
  466. module_exit(ah6_fini);
  467. MODULE_LICENSE("GPL");
  468. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH);