ipcomp.c 4.2 KB

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