ipcomp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IP Payload Compression Protocol (IPComp) - RFC3173.
  4. *
  5. * Copyright (c) 2003 James Morris <[email protected]>
  6. *
  7. * Todo:
  8. * - Tunable compression parameters.
  9. * - Compression stats.
  10. * - Adaptive compression.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/err.h>
  14. #include <linux/rtnetlink.h>
  15. #include <net/ip.h>
  16. #include <net/xfrm.h>
  17. #include <net/icmp.h>
  18. #include <net/ipcomp.h>
  19. #include <net/protocol.h>
  20. #include <net/sock.h>
  21. static int ipcomp4_err(struct sk_buff *skb, u32 info)
  22. {
  23. struct net *net = dev_net(skb->dev);
  24. __be32 spi;
  25. const struct iphdr *iph = (const struct iphdr *)skb->data;
  26. struct ip_comp_hdr *ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
  27. struct xfrm_state *x;
  28. switch (icmp_hdr(skb)->type) {
  29. case ICMP_DEST_UNREACH:
  30. if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  31. return 0;
  32. break;
  33. case ICMP_REDIRECT:
  34. break;
  35. default:
  36. return 0;
  37. }
  38. spi = htonl(ntohs(ipch->cpi));
  39. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  40. spi, IPPROTO_COMP, AF_INET);
  41. if (!x)
  42. return 0;
  43. if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
  44. ipv4_update_pmtu(skb, net, info, 0, IPPROTO_COMP);
  45. else
  46. ipv4_redirect(skb, net, 0, IPPROTO_COMP);
  47. xfrm_state_put(x);
  48. return 0;
  49. }
  50. /* We always hold one tunnel user reference to indicate a tunnel */
  51. static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
  52. {
  53. struct net *net = xs_net(x);
  54. struct xfrm_state *t;
  55. t = xfrm_state_alloc(net);
  56. if (!t)
  57. goto out;
  58. t->id.proto = IPPROTO_IPIP;
  59. t->id.spi = x->props.saddr.a4;
  60. t->id.daddr.a4 = x->id.daddr.a4;
  61. memcpy(&t->sel, &x->sel, sizeof(t->sel));
  62. t->props.family = AF_INET;
  63. t->props.mode = x->props.mode;
  64. t->props.saddr.a4 = x->props.saddr.a4;
  65. t->props.flags = x->props.flags;
  66. t->props.extra_flags = x->props.extra_flags;
  67. memcpy(&t->mark, &x->mark, sizeof(t->mark));
  68. t->if_id = x->if_id;
  69. if (xfrm_init_state(t))
  70. goto error;
  71. atomic_set(&t->tunnel_users, 1);
  72. out:
  73. return t;
  74. error:
  75. t->km.state = XFRM_STATE_DEAD;
  76. xfrm_state_put(t);
  77. t = NULL;
  78. goto out;
  79. }
  80. /*
  81. * Must be protected by xfrm_cfg_mutex. State and tunnel user references are
  82. * always incremented on success.
  83. */
  84. static int ipcomp_tunnel_attach(struct xfrm_state *x)
  85. {
  86. struct net *net = xs_net(x);
  87. int err = 0;
  88. struct xfrm_state *t;
  89. u32 mark = x->mark.v & x->mark.m;
  90. t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr.a4,
  91. x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
  92. if (!t) {
  93. t = ipcomp_tunnel_create(x);
  94. if (!t) {
  95. err = -EINVAL;
  96. goto out;
  97. }
  98. xfrm_state_insert(t);
  99. xfrm_state_hold(t);
  100. }
  101. x->tunnel = t;
  102. atomic_inc(&t->tunnel_users);
  103. out:
  104. return err;
  105. }
  106. static int ipcomp4_init_state(struct xfrm_state *x,
  107. struct netlink_ext_ack *extack)
  108. {
  109. int err = -EINVAL;
  110. x->props.header_len = 0;
  111. switch (x->props.mode) {
  112. case XFRM_MODE_TRANSPORT:
  113. break;
  114. case XFRM_MODE_TUNNEL:
  115. x->props.header_len += sizeof(struct iphdr);
  116. break;
  117. default:
  118. NL_SET_ERR_MSG(extack, "Unsupported XFRM mode for IPcomp");
  119. goto out;
  120. }
  121. err = ipcomp_init_state(x, extack);
  122. if (err)
  123. goto out;
  124. if (x->props.mode == XFRM_MODE_TUNNEL) {
  125. err = ipcomp_tunnel_attach(x);
  126. if (err) {
  127. NL_SET_ERR_MSG(extack, "Kernel error: failed to initialize the associated state");
  128. goto out;
  129. }
  130. }
  131. err = 0;
  132. out:
  133. return err;
  134. }
  135. static int ipcomp4_rcv_cb(struct sk_buff *skb, int err)
  136. {
  137. return 0;
  138. }
  139. static const struct xfrm_type ipcomp_type = {
  140. .owner = THIS_MODULE,
  141. .proto = IPPROTO_COMP,
  142. .init_state = ipcomp4_init_state,
  143. .destructor = ipcomp_destroy,
  144. .input = ipcomp_input,
  145. .output = ipcomp_output
  146. };
  147. static struct xfrm4_protocol ipcomp4_protocol = {
  148. .handler = xfrm4_rcv,
  149. .input_handler = xfrm_input,
  150. .cb_handler = ipcomp4_rcv_cb,
  151. .err_handler = ipcomp4_err,
  152. .priority = 0,
  153. };
  154. static int __init ipcomp4_init(void)
  155. {
  156. if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
  157. pr_info("%s: can't add xfrm type\n", __func__);
  158. return -EAGAIN;
  159. }
  160. if (xfrm4_protocol_register(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
  161. pr_info("%s: can't add protocol\n", __func__);
  162. xfrm_unregister_type(&ipcomp_type, AF_INET);
  163. return -EAGAIN;
  164. }
  165. return 0;
  166. }
  167. static void __exit ipcomp4_fini(void)
  168. {
  169. if (xfrm4_protocol_deregister(&ipcomp4_protocol, IPPROTO_COMP) < 0)
  170. pr_info("%s: can't remove protocol\n", __func__);
  171. xfrm_unregister_type(&ipcomp_type, AF_INET);
  172. }
  173. module_init(ipcomp4_init);
  174. module_exit(ipcomp4_fini);
  175. MODULE_LICENSE("GPL");
  176. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp/IPv4) - RFC3173");
  177. MODULE_AUTHOR("James Morris <[email protected]>");
  178. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_COMP);