ipcomp6.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173
  3. *
  4. * Copyright (C)2003 USAGI/WIDE Project
  5. *
  6. * Author Mitsuru KANDA <mk@linux-ipv6.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /*
  22. * [Memo]
  23. *
  24. * Outbound:
  25. * The compression of IP datagram MUST be done before AH/ESP processing,
  26. * fragmentation, and the addition of Hop-by-Hop/Routing header.
  27. *
  28. * Inbound:
  29. * The decompression of IP datagram MUST be done after the reassembly,
  30. * AH/ESP processing.
  31. */
  32. #define pr_fmt(fmt) "IPv6: " fmt
  33. #include <linux/module.h>
  34. #include <net/ip.h>
  35. #include <net/xfrm.h>
  36. #include <net/ipcomp.h>
  37. #include <linux/crypto.h>
  38. #include <linux/err.h>
  39. #include <linux/pfkeyv2.h>
  40. #include <linux/random.h>
  41. #include <linux/percpu.h>
  42. #include <linux/smp.h>
  43. #include <linux/list.h>
  44. #include <linux/vmalloc.h>
  45. #include <linux/rtnetlink.h>
  46. #include <net/ip6_route.h>
  47. #include <net/icmp.h>
  48. #include <net/ipv6.h>
  49. #include <net/protocol.h>
  50. #include <linux/ipv6.h>
  51. #include <linux/icmpv6.h>
  52. #include <linux/mutex.h>
  53. static int ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  54. u8 type, u8 code, int offset, __be32 info)
  55. {
  56. struct net *net = dev_net(skb->dev);
  57. __be32 spi;
  58. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  59. struct ip_comp_hdr *ipcomph =
  60. (struct ip_comp_hdr *)(skb->data + offset);
  61. struct xfrm_state *x;
  62. if (type != ICMPV6_PKT_TOOBIG &&
  63. type != NDISC_REDIRECT)
  64. return 0;
  65. spi = htonl(ntohs(ipcomph->cpi));
  66. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  67. spi, IPPROTO_COMP, AF_INET6);
  68. if (!x)
  69. return 0;
  70. if (type == NDISC_REDIRECT)
  71. ip6_redirect(skb, net, skb->dev->ifindex, 0);
  72. else
  73. ip6_update_pmtu(skb, net, info, 0, 0);
  74. xfrm_state_put(x);
  75. return 0;
  76. }
  77. static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
  78. {
  79. struct net *net = xs_net(x);
  80. struct xfrm_state *t = NULL;
  81. t = xfrm_state_alloc(net);
  82. if (!t)
  83. goto out;
  84. t->id.proto = IPPROTO_IPV6;
  85. t->id.spi = xfrm6_tunnel_alloc_spi(net, (xfrm_address_t *)&x->props.saddr);
  86. if (!t->id.spi)
  87. goto error;
  88. memcpy(t->id.daddr.a6, x->id.daddr.a6, sizeof(struct in6_addr));
  89. memcpy(&t->sel, &x->sel, sizeof(t->sel));
  90. t->props.family = AF_INET6;
  91. t->props.mode = x->props.mode;
  92. memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr));
  93. memcpy(&t->mark, &x->mark, sizeof(t->mark));
  94. if (xfrm_init_state(t))
  95. goto error;
  96. atomic_set(&t->tunnel_users, 1);
  97. out:
  98. return t;
  99. error:
  100. t->km.state = XFRM_STATE_DEAD;
  101. xfrm_state_put(t);
  102. t = NULL;
  103. goto out;
  104. }
  105. static int ipcomp6_tunnel_attach(struct xfrm_state *x)
  106. {
  107. struct net *net = xs_net(x);
  108. int err = 0;
  109. struct xfrm_state *t = NULL;
  110. __be32 spi;
  111. u32 mark = x->mark.m & x->mark.v;
  112. spi = xfrm6_tunnel_spi_lookup(net, (xfrm_address_t *)&x->props.saddr);
  113. if (spi)
  114. t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr,
  115. spi, IPPROTO_IPV6, AF_INET6);
  116. if (!t) {
  117. t = ipcomp6_tunnel_create(x);
  118. if (!t) {
  119. err = -EINVAL;
  120. goto out;
  121. }
  122. xfrm_state_insert(t);
  123. xfrm_state_hold(t);
  124. }
  125. x->tunnel = t;
  126. atomic_inc(&t->tunnel_users);
  127. out:
  128. return err;
  129. }
  130. static int ipcomp6_init_state(struct xfrm_state *x)
  131. {
  132. int err = -EINVAL;
  133. x->props.header_len = 0;
  134. switch (x->props.mode) {
  135. case XFRM_MODE_TRANSPORT:
  136. break;
  137. case XFRM_MODE_TUNNEL:
  138. x->props.header_len += sizeof(struct ipv6hdr);
  139. break;
  140. default:
  141. goto out;
  142. }
  143. err = ipcomp_init_state(x);
  144. if (err)
  145. goto out;
  146. if (x->props.mode == XFRM_MODE_TUNNEL) {
  147. err = ipcomp6_tunnel_attach(x);
  148. if (err)
  149. goto out;
  150. }
  151. err = 0;
  152. out:
  153. return err;
  154. }
  155. static int ipcomp6_rcv_cb(struct sk_buff *skb, int err)
  156. {
  157. return 0;
  158. }
  159. static const struct xfrm_type ipcomp6_type = {
  160. .description = "IPCOMP6",
  161. .owner = THIS_MODULE,
  162. .proto = IPPROTO_COMP,
  163. .init_state = ipcomp6_init_state,
  164. .destructor = ipcomp_destroy,
  165. .input = ipcomp_input,
  166. .output = ipcomp_output,
  167. .hdr_offset = xfrm6_find_1stfragopt,
  168. };
  169. static struct xfrm6_protocol ipcomp6_protocol = {
  170. .handler = xfrm6_rcv,
  171. .cb_handler = ipcomp6_rcv_cb,
  172. .err_handler = ipcomp6_err,
  173. .priority = 0,
  174. };
  175. static int __init ipcomp6_init(void)
  176. {
  177. if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) {
  178. pr_info("%s: can't add xfrm type\n", __func__);
  179. return -EAGAIN;
  180. }
  181. if (xfrm6_protocol_register(&ipcomp6_protocol, IPPROTO_COMP) < 0) {
  182. pr_info("%s: can't add protocol\n", __func__);
  183. xfrm_unregister_type(&ipcomp6_type, AF_INET6);
  184. return -EAGAIN;
  185. }
  186. return 0;
  187. }
  188. static void __exit ipcomp6_fini(void)
  189. {
  190. if (xfrm6_protocol_deregister(&ipcomp6_protocol, IPPROTO_COMP) < 0)
  191. pr_info("%s: can't remove protocol\n", __func__);
  192. if (xfrm_unregister_type(&ipcomp6_type, AF_INET6) < 0)
  193. pr_info("%s: can't remove xfrm type\n", __func__);
  194. }
  195. module_init(ipcomp6_init);
  196. module_exit(ipcomp6_fini);
  197. MODULE_LICENSE("GPL");
  198. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173");
  199. MODULE_AUTHOR("Mitsuru KANDA <mk@linux-ipv6.org>");
  200. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_COMP);