ipcomp6.c 4.9 KB

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