ipcomp.c 4.4 KB

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