ah6.c 13 KB

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