ip_vs_proto_esp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/netfilter.h>
  17. #include <linux/netfilter_ipv4.h>
  18. #include <net/ip_vs.h>
  19. /* TODO:
  20. struct isakmp_hdr {
  21. __u8 icookie[8];
  22. __u8 rcookie[8];
  23. __u8 np;
  24. __u8 version;
  25. __u8 xchgtype;
  26. __u8 flags;
  27. __u32 msgid;
  28. __u32 length;
  29. };
  30. */
  31. #define PORT_ISAKMP 500
  32. static struct ip_vs_conn *
  33. esp_conn_in_get(const struct sk_buff *skb,
  34. struct ip_vs_protocol *pp,
  35. const struct iphdr *iph,
  36. unsigned int proto_off,
  37. int inverse)
  38. {
  39. struct ip_vs_conn *cp;
  40. if (likely(!inverse)) {
  41. cp = ip_vs_conn_in_get(IPPROTO_UDP,
  42. iph->saddr,
  43. __constant_htons(PORT_ISAKMP),
  44. iph->daddr,
  45. __constant_htons(PORT_ISAKMP));
  46. } else {
  47. cp = ip_vs_conn_in_get(IPPROTO_UDP,
  48. iph->daddr,
  49. __constant_htons(PORT_ISAKMP),
  50. iph->saddr,
  51. __constant_htons(PORT_ISAKMP));
  52. }
  53. if (!cp) {
  54. /*
  55. * We are not sure if the packet is from our
  56. * service, so our conn_schedule hook should return NF_ACCEPT
  57. */
  58. IP_VS_DBG(12, "Unknown ISAKMP entry for outin packet "
  59. "%s%s %u.%u.%u.%u->%u.%u.%u.%u\n",
  60. inverse ? "ICMP+" : "",
  61. pp->name,
  62. NIPQUAD(iph->saddr),
  63. NIPQUAD(iph->daddr));
  64. }
  65. return cp;
  66. }
  67. static struct ip_vs_conn *
  68. esp_conn_out_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
  69. const struct iphdr *iph, unsigned int proto_off, int inverse)
  70. {
  71. struct ip_vs_conn *cp;
  72. if (likely(!inverse)) {
  73. cp = ip_vs_conn_out_get(IPPROTO_UDP,
  74. iph->saddr,
  75. __constant_htons(PORT_ISAKMP),
  76. iph->daddr,
  77. __constant_htons(PORT_ISAKMP));
  78. } else {
  79. cp = ip_vs_conn_out_get(IPPROTO_UDP,
  80. iph->daddr,
  81. __constant_htons(PORT_ISAKMP),
  82. iph->saddr,
  83. __constant_htons(PORT_ISAKMP));
  84. }
  85. if (!cp) {
  86. IP_VS_DBG(12, "Unknown ISAKMP entry for inout packet "
  87. "%s%s %u.%u.%u.%u->%u.%u.%u.%u\n",
  88. inverse ? "ICMP+" : "",
  89. pp->name,
  90. NIPQUAD(iph->saddr),
  91. NIPQUAD(iph->daddr));
  92. }
  93. return cp;
  94. }
  95. static int
  96. esp_conn_schedule(struct sk_buff *skb, struct ip_vs_protocol *pp,
  97. int *verdict, struct ip_vs_conn **cpp)
  98. {
  99. /*
  100. * ESP is only related traffic. Pass the packet to IP stack.
  101. */
  102. *verdict = NF_ACCEPT;
  103. return 0;
  104. }
  105. static void
  106. esp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
  107. int offset, const char *msg)
  108. {
  109. char buf[256];
  110. struct iphdr _iph, *ih;
  111. ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
  112. if (ih == NULL)
  113. sprintf(buf, "%s TRUNCATED", pp->name);
  114. else
  115. sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u",
  116. pp->name, NIPQUAD(ih->saddr),
  117. NIPQUAD(ih->daddr));
  118. printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
  119. }
  120. static void esp_init(struct ip_vs_protocol *pp)
  121. {
  122. /* nothing to do now */
  123. }
  124. static void esp_exit(struct ip_vs_protocol *pp)
  125. {
  126. /* nothing to do now */
  127. }
  128. struct ip_vs_protocol ip_vs_protocol_esp = {
  129. .name = "ESP",
  130. .protocol = IPPROTO_ESP,
  131. .dont_defrag = 1,
  132. .init = esp_init,
  133. .exit = esp_exit,
  134. .conn_schedule = esp_conn_schedule,
  135. .conn_in_get = esp_conn_in_get,
  136. .conn_out_get = esp_conn_out_get,
  137. .snat_handler = NULL,
  138. .dnat_handler = NULL,
  139. .csum_check = NULL,
  140. .state_transition = NULL,
  141. .register_app = NULL,
  142. .unregister_app = NULL,
  143. .app_conn_bind = NULL,
  144. .debug_packet = esp_debug_packet,
  145. .timeout_change = NULL, /* ISAKMP */
  146. };