ipcomp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 void 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;
  36. case ICMP_REDIRECT:
  37. break;
  38. default:
  39. return;
  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;
  46. if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH) {
  47. atomic_inc(&flow_cache_genid);
  48. rt_genid_bump(net);
  49. ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_COMP, 0);
  50. } else
  51. ipv4_redirect(skb, net, 0, 0, IPPROTO_COMP, 0);
  52. xfrm_state_put(x);
  53. }
  54. /* We always hold one tunnel user reference to indicate a tunnel */
  55. static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
  56. {
  57. struct net *net = xs_net(x);
  58. struct xfrm_state *t;
  59. t = xfrm_state_alloc(net);
  60. if (t == NULL)
  61. goto out;
  62. t->id.proto = IPPROTO_IPIP;
  63. t->id.spi = x->props.saddr.a4;
  64. t->id.daddr.a4 = x->id.daddr.a4;
  65. memcpy(&t->sel, &x->sel, sizeof(t->sel));
  66. t->props.family = AF_INET;
  67. t->props.mode = x->props.mode;
  68. t->props.saddr.a4 = x->props.saddr.a4;
  69. t->props.flags = x->props.flags;
  70. t->props.extra_flags = x->props.extra_flags;
  71. memcpy(&t->mark, &x->mark, sizeof(t->mark));
  72. if (xfrm_init_state(t))
  73. goto error;
  74. atomic_set(&t->tunnel_users, 1);
  75. out:
  76. return t;
  77. error:
  78. t->km.state = XFRM_STATE_DEAD;
  79. xfrm_state_put(t);
  80. t = NULL;
  81. goto out;
  82. }
  83. /*
  84. * Must be protected by xfrm_cfg_mutex. State and tunnel user references are
  85. * always incremented on success.
  86. */
  87. static int ipcomp_tunnel_attach(struct xfrm_state *x)
  88. {
  89. struct net *net = xs_net(x);
  90. int err = 0;
  91. struct xfrm_state *t;
  92. u32 mark = x->mark.v & x->mark.m;
  93. t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr.a4,
  94. x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
  95. if (!t) {
  96. t = ipcomp_tunnel_create(x);
  97. if (!t) {
  98. err = -EINVAL;
  99. goto out;
  100. }
  101. xfrm_state_insert(t);
  102. xfrm_state_hold(t);
  103. }
  104. x->tunnel = t;
  105. atomic_inc(&t->tunnel_users);
  106. out:
  107. return err;
  108. }
  109. static int ipcomp4_init_state(struct xfrm_state *x)
  110. {
  111. int err = -EINVAL;
  112. x->props.header_len = 0;
  113. switch (x->props.mode) {
  114. case XFRM_MODE_TRANSPORT:
  115. break;
  116. case XFRM_MODE_TUNNEL:
  117. x->props.header_len += sizeof(struct iphdr);
  118. break;
  119. default:
  120. goto out;
  121. }
  122. err = ipcomp_init_state(x);
  123. if (err)
  124. goto out;
  125. if (x->props.mode == XFRM_MODE_TUNNEL) {
  126. err = ipcomp_tunnel_attach(x);
  127. if (err)
  128. goto out;
  129. }
  130. err = 0;
  131. out:
  132. return err;
  133. }
  134. static const struct xfrm_type ipcomp_type = {
  135. .description = "IPCOMP4",
  136. .owner = THIS_MODULE,
  137. .proto = IPPROTO_COMP,
  138. .init_state = ipcomp4_init_state,
  139. .destructor = ipcomp_destroy,
  140. .input = ipcomp_input,
  141. .output = ipcomp_output
  142. };
  143. static const struct net_protocol ipcomp4_protocol = {
  144. .handler = xfrm4_rcv,
  145. .err_handler = ipcomp4_err,
  146. .no_policy = 1,
  147. .netns_ok = 1,
  148. };
  149. static int __init ipcomp4_init(void)
  150. {
  151. if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
  152. pr_info("%s: can't add xfrm type\n", __func__);
  153. return -EAGAIN;
  154. }
  155. if (inet_add_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
  156. pr_info("%s: can't add protocol\n", __func__);
  157. xfrm_unregister_type(&ipcomp_type, AF_INET);
  158. return -EAGAIN;
  159. }
  160. return 0;
  161. }
  162. static void __exit ipcomp4_fini(void)
  163. {
  164. if (inet_del_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0)
  165. pr_info("%s: can't remove protocol\n", __func__);
  166. if (xfrm_unregister_type(&ipcomp_type, AF_INET) < 0)
  167. pr_info("%s: can't remove xfrm type\n", __func__);
  168. }
  169. module_init(ipcomp4_init);
  170. module_exit(ipcomp4_fini);
  171. MODULE_LICENSE("GPL");
  172. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp/IPv4) - RFC3173");
  173. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
  174. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_COMP);