ip_vs_proto_esp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * ip_vs_proto_esp.c: ESP IPSec load balancing support for IPVS
  3. *
  4. * Version: $Id: ip_vs_proto_esp.c,v 1.1 2003/07/04 15:04:37 wensong Exp $
  5. *
  6. * Authors: Julian Anastasov <ja@ssi.bg>, February 2002
  7. * Wensong Zhang <wensong@linuxvirtualserver.org>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation;
  12. *
  13. */
  14. #include <linux/in.h>
  15. #include <linux/ip.h>
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/netfilter.h>
  19. #include <linux/netfilter_ipv4.h>
  20. #include <net/ip_vs.h>
  21. /* TODO:
  22. struct isakmp_hdr {
  23. __u8 icookie[8];
  24. __u8 rcookie[8];
  25. __u8 np;
  26. __u8 version;
  27. __u8 xchgtype;
  28. __u8 flags;
  29. __u32 msgid;
  30. __u32 length;
  31. };
  32. */
  33. #define PORT_ISAKMP 500
  34. static struct ip_vs_conn *
  35. esp_conn_in_get(const struct sk_buff *skb,
  36. struct ip_vs_protocol *pp,
  37. const struct iphdr *iph,
  38. unsigned int proto_off,
  39. int inverse)
  40. {
  41. struct ip_vs_conn *cp;
  42. if (likely(!inverse)) {
  43. cp = ip_vs_conn_in_get(IPPROTO_UDP,
  44. iph->saddr,
  45. __constant_htons(PORT_ISAKMP),
  46. iph->daddr,
  47. __constant_htons(PORT_ISAKMP));
  48. } else {
  49. cp = ip_vs_conn_in_get(IPPROTO_UDP,
  50. iph->daddr,
  51. __constant_htons(PORT_ISAKMP),
  52. iph->saddr,
  53. __constant_htons(PORT_ISAKMP));
  54. }
  55. if (!cp) {
  56. /*
  57. * We are not sure if the packet is from our
  58. * service, so our conn_schedule hook should return NF_ACCEPT
  59. */
  60. IP_VS_DBG(12, "Unknown ISAKMP entry for outin packet "
  61. "%s%s %u.%u.%u.%u->%u.%u.%u.%u\n",
  62. inverse ? "ICMP+" : "",
  63. pp->name,
  64. NIPQUAD(iph->saddr),
  65. NIPQUAD(iph->daddr));
  66. }
  67. return cp;
  68. }
  69. static struct ip_vs_conn *
  70. esp_conn_out_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
  71. const struct iphdr *iph, unsigned int proto_off, int inverse)
  72. {
  73. struct ip_vs_conn *cp;
  74. if (likely(!inverse)) {
  75. cp = ip_vs_conn_out_get(IPPROTO_UDP,
  76. iph->saddr,
  77. __constant_htons(PORT_ISAKMP),
  78. iph->daddr,
  79. __constant_htons(PORT_ISAKMP));
  80. } else {
  81. cp = ip_vs_conn_out_get(IPPROTO_UDP,
  82. iph->daddr,
  83. __constant_htons(PORT_ISAKMP),
  84. iph->saddr,
  85. __constant_htons(PORT_ISAKMP));
  86. }
  87. if (!cp) {
  88. IP_VS_DBG(12, "Unknown ISAKMP entry for inout packet "
  89. "%s%s %u.%u.%u.%u->%u.%u.%u.%u\n",
  90. inverse ? "ICMP+" : "",
  91. pp->name,
  92. NIPQUAD(iph->saddr),
  93. NIPQUAD(iph->daddr));
  94. }
  95. return cp;
  96. }
  97. static int
  98. esp_conn_schedule(struct sk_buff *skb, struct ip_vs_protocol *pp,
  99. int *verdict, struct ip_vs_conn **cpp)
  100. {
  101. /*
  102. * ESP is only related traffic. Pass the packet to IP stack.
  103. */
  104. *verdict = NF_ACCEPT;
  105. return 0;
  106. }
  107. static void
  108. esp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
  109. int offset, const char *msg)
  110. {
  111. char buf[256];
  112. struct iphdr _iph, *ih;
  113. ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
  114. if (ih == NULL)
  115. sprintf(buf, "%s TRUNCATED", pp->name);
  116. else
  117. sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u",
  118. pp->name, NIPQUAD(ih->saddr),
  119. NIPQUAD(ih->daddr));
  120. printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
  121. }
  122. static void esp_init(struct ip_vs_protocol *pp)
  123. {
  124. /* nothing to do now */
  125. }
  126. static void esp_exit(struct ip_vs_protocol *pp)
  127. {
  128. /* nothing to do now */
  129. }
  130. struct ip_vs_protocol ip_vs_protocol_esp = {
  131. .name = "ESP",
  132. .protocol = IPPROTO_ESP,
  133. .dont_defrag = 1,
  134. .init = esp_init,
  135. .exit = esp_exit,
  136. .conn_schedule = esp_conn_schedule,
  137. .conn_in_get = esp_conn_in_get,
  138. .conn_out_get = esp_conn_out_get,
  139. .snat_handler = NULL,
  140. .dnat_handler = NULL,
  141. .csum_check = NULL,
  142. .state_transition = NULL,
  143. .register_app = NULL,
  144. .unregister_app = NULL,
  145. .app_conn_bind = NULL,
  146. .debug_packet = esp_debug_packet,
  147. .timeout_change = NULL, /* ISAKMP */
  148. };