/net/ipv6/ipcomp6.c

http://github.com/mirrors/linux · C · 218 lines · 169 code · 30 blank · 19 comment · 20 complexity · b870248329d7c17243957906a580f353 MD5 · raw file

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