ipcomp6.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. sock_net_uid(net, NULL));
  73. else
  74. ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
  75. xfrm_state_put(x);
  76. return 0;
  77. }
  78. static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
  79. {
  80. struct net *net = xs_net(x);
  81. struct xfrm_state *t = NULL;
  82. t = xfrm_state_alloc(net);
  83. if (!t)
  84. goto out;
  85. t->id.proto = IPPROTO_IPV6;
  86. t->id.spi = xfrm6_tunnel_alloc_spi(net, (xfrm_address_t *)&x->props.saddr);
  87. if (!t->id.spi)
  88. goto error;
  89. memcpy(t->id.daddr.a6, x->id.daddr.a6, sizeof(struct in6_addr));
  90. memcpy(&t->sel, &x->sel, sizeof(t->sel));
  91. t->props.family = AF_INET6;
  92. t->props.mode = x->props.mode;
  93. memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr));
  94. memcpy(&t->mark, &x->mark, sizeof(t->mark));
  95. if (xfrm_init_state(t))
  96. goto error;
  97. atomic_set(&t->tunnel_users, 1);
  98. out:
  99. return t;
  100. error:
  101. t->km.state = XFRM_STATE_DEAD;
  102. xfrm_state_put(t);
  103. t = NULL;
  104. goto out;
  105. }
  106. static int ipcomp6_tunnel_attach(struct xfrm_state *x)
  107. {
  108. struct net *net = xs_net(x);
  109. int err = 0;
  110. struct xfrm_state *t = NULL;
  111. __be32 spi;
  112. u32 mark = x->mark.m & x->mark.v;
  113. spi = xfrm6_tunnel_spi_lookup(net, (xfrm_address_t *)&x->props.saddr);
  114. if (spi)
  115. t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr,
  116. spi, IPPROTO_IPV6, AF_INET6);
  117. if (!t) {
  118. t = ipcomp6_tunnel_create(x);
  119. if (!t) {
  120. err = -EINVAL;
  121. goto out;
  122. }
  123. xfrm_state_insert(t);
  124. xfrm_state_hold(t);
  125. }
  126. x->tunnel = t;
  127. atomic_inc(&t->tunnel_users);
  128. out:
  129. return err;
  130. }
  131. static int ipcomp6_init_state(struct xfrm_state *x)
  132. {
  133. int err = -EINVAL;
  134. x->props.header_len = 0;
  135. switch (x->props.mode) {
  136. case XFRM_MODE_TRANSPORT:
  137. break;
  138. case XFRM_MODE_TUNNEL:
  139. x->props.header_len += sizeof(struct ipv6hdr);
  140. break;
  141. default:
  142. goto out;
  143. }
  144. err = ipcomp_init_state(x);
  145. if (err)
  146. goto out;
  147. if (x->props.mode == XFRM_MODE_TUNNEL) {
  148. err = ipcomp6_tunnel_attach(x);
  149. if (err)
  150. goto out;
  151. }
  152. err = 0;
  153. out:
  154. return err;
  155. }
  156. static int ipcomp6_rcv_cb(struct sk_buff *skb, int err)
  157. {
  158. return 0;
  159. }
  160. static const struct xfrm_type ipcomp6_type = {
  161. .description = "IPCOMP6",
  162. .owner = THIS_MODULE,
  163. .proto = IPPROTO_COMP,
  164. .init_state = ipcomp6_init_state,
  165. .destructor = ipcomp_destroy,
  166. .input = ipcomp_input,
  167. .output = ipcomp_output,
  168. .hdr_offset = xfrm6_find_1stfragopt,
  169. };
  170. static struct xfrm6_protocol ipcomp6_protocol = {
  171. .handler = xfrm6_rcv,
  172. .cb_handler = ipcomp6_rcv_cb,
  173. .err_handler = ipcomp6_err,
  174. .priority = 0,
  175. };
  176. static int __init ipcomp6_init(void)
  177. {
  178. if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) {
  179. pr_info("%s: can't add xfrm type\n", __func__);
  180. return -EAGAIN;
  181. }
  182. if (xfrm6_protocol_register(&ipcomp6_protocol, IPPROTO_COMP) < 0) {
  183. pr_info("%s: can't add protocol\n", __func__);
  184. xfrm_unregister_type(&ipcomp6_type, AF_INET6);
  185. return -EAGAIN;
  186. }
  187. return 0;
  188. }
  189. static void __exit ipcomp6_fini(void)
  190. {
  191. if (xfrm6_protocol_deregister(&ipcomp6_protocol, IPPROTO_COMP) < 0)
  192. pr_info("%s: can't remove protocol\n", __func__);
  193. if (xfrm_unregister_type(&ipcomp6_type, AF_INET6) < 0)
  194. pr_info("%s: can't remove xfrm type\n", __func__);
  195. }
  196. module_init(ipcomp6_init);
  197. module_exit(ipcomp6_fini);
  198. MODULE_LICENSE("GPL");
  199. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173");
  200. MODULE_AUTHOR("Mitsuru KANDA <mk@linux-ipv6.org>");
  201. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_COMP);