ipcomp.c 4.6 KB

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