ip_vs_proto_ah_esp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * ip_vs_proto_ah_esp.c: AH/ESP IPSec load balancing support for IPVS
  3. *
  4. * Authors: Julian Anastasov <ja@ssi.bg>, February 2002
  5. * Wensong Zhang <wensong@linuxvirtualserver.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation;
  10. *
  11. */
  12. #include <linux/in.h>
  13. #include <linux/ip.h>
  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. ah_esp_conn_in_get(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp,
  34. const struct ip_vs_iphdr *iph, unsigned int proto_off,
  35. int inverse)
  36. {
  37. struct ip_vs_conn *cp;
  38. if (likely(!inverse)) {
  39. cp = ip_vs_conn_in_get(af, IPPROTO_UDP,
  40. &iph->saddr,
  41. htons(PORT_ISAKMP),
  42. &iph->daddr,
  43. htons(PORT_ISAKMP));
  44. } else {
  45. cp = ip_vs_conn_in_get(af, IPPROTO_UDP,
  46. &iph->daddr,
  47. htons(PORT_ISAKMP),
  48. &iph->saddr,
  49. htons(PORT_ISAKMP));
  50. }
  51. if (!cp) {
  52. /*
  53. * We are not sure if the packet is from our
  54. * service, so our conn_schedule hook should return NF_ACCEPT
  55. */
  56. IP_VS_DBG_BUF(12, "Unknown ISAKMP entry for outin packet "
  57. "%s%s %s->%s\n",
  58. inverse ? "ICMP+" : "",
  59. pp->name,
  60. IP_VS_DBG_ADDR(af, &iph->saddr),
  61. IP_VS_DBG_ADDR(af, &iph->daddr));
  62. }
  63. return cp;
  64. }
  65. static struct ip_vs_conn *
  66. ah_esp_conn_out_get(int af, const struct sk_buff *skb,
  67. struct ip_vs_protocol *pp,
  68. const struct ip_vs_iphdr *iph,
  69. unsigned int proto_off,
  70. int inverse)
  71. {
  72. struct ip_vs_conn *cp;
  73. if (likely(!inverse)) {
  74. cp = ip_vs_conn_out_get(af, IPPROTO_UDP,
  75. &iph->saddr,
  76. htons(PORT_ISAKMP),
  77. &iph->daddr,
  78. htons(PORT_ISAKMP));
  79. } else {
  80. cp = ip_vs_conn_out_get(af, IPPROTO_UDP,
  81. &iph->daddr,
  82. htons(PORT_ISAKMP),
  83. &iph->saddr,
  84. htons(PORT_ISAKMP));
  85. }
  86. if (!cp) {
  87. IP_VS_DBG_BUF(12, "Unknown ISAKMP entry for inout packet "
  88. "%s%s %s->%s\n",
  89. inverse ? "ICMP+" : "",
  90. pp->name,
  91. IP_VS_DBG_ADDR(af, &iph->saddr),
  92. IP_VS_DBG_ADDR(af, &iph->daddr));
  93. }
  94. return cp;
  95. }
  96. static int
  97. ah_esp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
  98. int *verdict, struct ip_vs_conn **cpp)
  99. {
  100. /*
  101. * AH/ESP is only related traffic. Pass the packet to IP stack.
  102. */
  103. *verdict = NF_ACCEPT;
  104. return 0;
  105. }
  106. static void
  107. ah_esp_debug_packet_v4(struct ip_vs_protocol *pp, const struct sk_buff *skb,
  108. int offset, const char *msg)
  109. {
  110. char buf[256];
  111. struct iphdr _iph, *ih;
  112. ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
  113. if (ih == NULL)
  114. sprintf(buf, "%s TRUNCATED", pp->name);
  115. else
  116. sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u",
  117. pp->name, NIPQUAD(ih->saddr),
  118. NIPQUAD(ih->daddr));
  119. printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
  120. }
  121. #ifdef CONFIG_IP_VS_IPV6
  122. static void
  123. ah_esp_debug_packet_v6(struct ip_vs_protocol *pp, const struct sk_buff *skb,
  124. int offset, const char *msg)
  125. {
  126. char buf[256];
  127. struct ipv6hdr _iph, *ih;
  128. ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
  129. if (ih == NULL)
  130. sprintf(buf, "%s TRUNCATED", pp->name);
  131. else
  132. sprintf(buf, "%s " NIP6_FMT "->" NIP6_FMT,
  133. pp->name, NIP6(ih->saddr),
  134. NIP6(ih->daddr));
  135. printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
  136. }
  137. #endif
  138. static void
  139. ah_esp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
  140. int offset, const char *msg)
  141. {
  142. #ifdef CONFIG_IP_VS_IPV6
  143. if (skb->protocol == __constant_htons(ETH_P_IPV6))
  144. ah_esp_debug_packet_v6(pp, skb, offset, msg);
  145. else
  146. #endif
  147. ah_esp_debug_packet_v4(pp, skb, offset, msg);
  148. }
  149. static void ah_esp_init(struct ip_vs_protocol *pp)
  150. {
  151. /* nothing to do now */
  152. }
  153. static void ah_esp_exit(struct ip_vs_protocol *pp)
  154. {
  155. /* nothing to do now */
  156. }
  157. #ifdef CONFIG_IP_VS_PROTO_AH
  158. struct ip_vs_protocol ip_vs_protocol_ah = {
  159. .name = "AH",
  160. .protocol = IPPROTO_AH,
  161. .num_states = 1,
  162. .dont_defrag = 1,
  163. .init = ah_esp_init,
  164. .exit = ah_esp_exit,
  165. .conn_schedule = ah_esp_conn_schedule,
  166. .conn_in_get = ah_esp_conn_in_get,
  167. .conn_out_get = ah_esp_conn_out_get,
  168. .snat_handler = NULL,
  169. .dnat_handler = NULL,
  170. .csum_check = NULL,
  171. .state_transition = NULL,
  172. .register_app = NULL,
  173. .unregister_app = NULL,
  174. .app_conn_bind = NULL,
  175. .debug_packet = ah_esp_debug_packet,
  176. .timeout_change = NULL, /* ISAKMP */
  177. .set_state_timeout = NULL,
  178. };
  179. #endif
  180. #ifdef CONFIG_IP_VS_PROTO_ESP
  181. struct ip_vs_protocol ip_vs_protocol_esp = {
  182. .name = "ESP",
  183. .protocol = IPPROTO_ESP,
  184. .num_states = 1,
  185. .dont_defrag = 1,
  186. .init = ah_esp_init,
  187. .exit = ah_esp_exit,
  188. .conn_schedule = ah_esp_conn_schedule,
  189. .conn_in_get = ah_esp_conn_in_get,
  190. .conn_out_get = ah_esp_conn_out_get,
  191. .snat_handler = NULL,
  192. .dnat_handler = NULL,
  193. .csum_check = NULL,
  194. .state_transition = NULL,
  195. .register_app = NULL,
  196. .unregister_app = NULL,
  197. .app_conn_bind = NULL,
  198. .debug_packet = ah_esp_debug_packet,
  199. .timeout_change = NULL, /* ISAKMP */
  200. };
  201. #endif