ip_vs_proto_ah_esp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #define KMSG_COMPONENT "IPVS"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  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 void
  35. ah_esp_conn_fill_param_proto(struct netns_ipvs *ipvs, int af,
  36. const struct ip_vs_iphdr *iph,
  37. struct ip_vs_conn_param *p)
  38. {
  39. if (likely(!ip_vs_iph_inverse(iph)))
  40. ip_vs_conn_fill_param(ipvs, af, IPPROTO_UDP,
  41. &iph->saddr, htons(PORT_ISAKMP),
  42. &iph->daddr, htons(PORT_ISAKMP), p);
  43. else
  44. ip_vs_conn_fill_param(ipvs, af, IPPROTO_UDP,
  45. &iph->daddr, htons(PORT_ISAKMP),
  46. &iph->saddr, htons(PORT_ISAKMP), p);
  47. }
  48. static struct ip_vs_conn *
  49. ah_esp_conn_in_get(struct netns_ipvs *ipvs, int af, const struct sk_buff *skb,
  50. const struct ip_vs_iphdr *iph)
  51. {
  52. struct ip_vs_conn *cp;
  53. struct ip_vs_conn_param p;
  54. ah_esp_conn_fill_param_proto(ipvs, af, iph, &p);
  55. cp = ip_vs_conn_in_get(&p);
  56. if (!cp) {
  57. /*
  58. * We are not sure if the packet is from our
  59. * service, so our conn_schedule hook should return NF_ACCEPT
  60. */
  61. IP_VS_DBG_BUF(12, "Unknown ISAKMP entry for outin packet "
  62. "%s%s %s->%s\n",
  63. ip_vs_iph_icmp(iph) ? "ICMP+" : "",
  64. ip_vs_proto_get(iph->protocol)->name,
  65. IP_VS_DBG_ADDR(af, &iph->saddr),
  66. IP_VS_DBG_ADDR(af, &iph->daddr));
  67. }
  68. return cp;
  69. }
  70. static struct ip_vs_conn *
  71. ah_esp_conn_out_get(struct netns_ipvs *ipvs, int af, const struct sk_buff *skb,
  72. const struct ip_vs_iphdr *iph)
  73. {
  74. struct ip_vs_conn *cp;
  75. struct ip_vs_conn_param p;
  76. ah_esp_conn_fill_param_proto(ipvs, af, iph, &p);
  77. cp = ip_vs_conn_out_get(&p);
  78. if (!cp) {
  79. IP_VS_DBG_BUF(12, "Unknown ISAKMP entry for inout packet "
  80. "%s%s %s->%s\n",
  81. ip_vs_iph_icmp(iph) ? "ICMP+" : "",
  82. ip_vs_proto_get(iph->protocol)->name,
  83. IP_VS_DBG_ADDR(af, &iph->saddr),
  84. IP_VS_DBG_ADDR(af, &iph->daddr));
  85. }
  86. return cp;
  87. }
  88. static int
  89. ah_esp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
  90. struct ip_vs_proto_data *pd,
  91. int *verdict, struct ip_vs_conn **cpp,
  92. struct ip_vs_iphdr *iph)
  93. {
  94. /*
  95. * AH/ESP is only related traffic. Pass the packet to IP stack.
  96. */
  97. *verdict = NF_ACCEPT;
  98. return 0;
  99. }
  100. #ifdef CONFIG_IP_VS_PROTO_AH
  101. struct ip_vs_protocol ip_vs_protocol_ah = {
  102. .name = "AH",
  103. .protocol = IPPROTO_AH,
  104. .num_states = 1,
  105. .dont_defrag = 1,
  106. .init = NULL,
  107. .exit = NULL,
  108. .conn_schedule = ah_esp_conn_schedule,
  109. .conn_in_get = ah_esp_conn_in_get,
  110. .conn_out_get = ah_esp_conn_out_get,
  111. .snat_handler = NULL,
  112. .dnat_handler = NULL,
  113. .csum_check = NULL,
  114. .state_transition = NULL,
  115. .register_app = NULL,
  116. .unregister_app = NULL,
  117. .app_conn_bind = NULL,
  118. .debug_packet = ip_vs_tcpudp_debug_packet,
  119. .timeout_change = NULL, /* ISAKMP */
  120. };
  121. #endif
  122. #ifdef CONFIG_IP_VS_PROTO_ESP
  123. struct ip_vs_protocol ip_vs_protocol_esp = {
  124. .name = "ESP",
  125. .protocol = IPPROTO_ESP,
  126. .num_states = 1,
  127. .dont_defrag = 1,
  128. .init = NULL,
  129. .exit = NULL,
  130. .conn_schedule = ah_esp_conn_schedule,
  131. .conn_in_get = ah_esp_conn_in_get,
  132. .conn_out_get = ah_esp_conn_out_get,
  133. .snat_handler = NULL,
  134. .dnat_handler = NULL,
  135. .csum_check = NULL,
  136. .state_transition = NULL,
  137. .register_app = NULL,
  138. .unregister_app = NULL,
  139. .app_conn_bind = NULL,
  140. .debug_packet = ip_vs_tcpudp_debug_packet,
  141. .timeout_change = NULL, /* ISAKMP */
  142. };
  143. #endif