ip_vs_proto_icmp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * ip_vs_proto_icmp.c: ICMP load balancing support for IP Virtual Server
  3. *
  4. * Authors: Julian Anastasov <ja@ssi.bg>, March 2002
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation;
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/icmp.h>
  14. #include <linux/netfilter.h>
  15. #include <linux/netfilter_ipv4.h>
  16. #include <net/ip_vs.h>
  17. static int icmp_timeouts[1] = { 1*60*HZ };
  18. static char * icmp_state_name_table[1] = { "ICMP" };
  19. static struct ip_vs_conn *
  20. icmp_conn_in_get(const struct sk_buff *skb,
  21. struct ip_vs_protocol *pp,
  22. const struct iphdr *iph,
  23. unsigned int proto_off,
  24. int inverse)
  25. {
  26. #if 0
  27. struct ip_vs_conn *cp;
  28. if (likely(!inverse)) {
  29. cp = ip_vs_conn_in_get(iph->protocol,
  30. iph->saddr, 0,
  31. iph->daddr, 0);
  32. } else {
  33. cp = ip_vs_conn_in_get(iph->protocol,
  34. iph->daddr, 0,
  35. iph->saddr, 0);
  36. }
  37. return cp;
  38. #else
  39. return NULL;
  40. #endif
  41. }
  42. static struct ip_vs_conn *
  43. icmp_conn_out_get(const struct sk_buff *skb,
  44. struct ip_vs_protocol *pp,
  45. const struct iphdr *iph,
  46. unsigned int proto_off,
  47. int inverse)
  48. {
  49. #if 0
  50. struct ip_vs_conn *cp;
  51. if (likely(!inverse)) {
  52. cp = ip_vs_conn_out_get(iph->protocol,
  53. iph->saddr, 0,
  54. iph->daddr, 0);
  55. } else {
  56. cp = ip_vs_conn_out_get(IPPROTO_UDP,
  57. iph->daddr, 0,
  58. iph->saddr, 0);
  59. }
  60. return cp;
  61. #else
  62. return NULL;
  63. #endif
  64. }
  65. static int
  66. icmp_conn_schedule(struct sk_buff *skb, struct ip_vs_protocol *pp,
  67. int *verdict, struct ip_vs_conn **cpp)
  68. {
  69. *verdict = NF_ACCEPT;
  70. return 0;
  71. }
  72. static int
  73. icmp_csum_check(struct sk_buff *skb, struct ip_vs_protocol *pp)
  74. {
  75. if (!(skb->nh.iph->frag_off & __constant_htons(IP_OFFSET))) {
  76. if (skb->ip_summed != CHECKSUM_UNNECESSARY) {
  77. if (ip_vs_checksum_complete(skb, skb->nh.iph->ihl * 4)) {
  78. IP_VS_DBG_RL_PKT(0, pp, skb, 0, "Failed checksum for");
  79. return 0;
  80. }
  81. }
  82. }
  83. return 1;
  84. }
  85. static void
  86. icmp_debug_packet(struct ip_vs_protocol *pp,
  87. const struct sk_buff *skb,
  88. int offset,
  89. const char *msg)
  90. {
  91. char buf[256];
  92. struct iphdr _iph, *ih;
  93. ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
  94. if (ih == NULL)
  95. sprintf(buf, "%s TRUNCATED", pp->name);
  96. else if (ih->frag_off & __constant_htons(IP_OFFSET))
  97. sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u frag",
  98. pp->name, NIPQUAD(ih->saddr),
  99. NIPQUAD(ih->daddr));
  100. else {
  101. struct icmphdr _icmph, *ic;
  102. ic = skb_header_pointer(skb, offset + ih->ihl*4,
  103. sizeof(_icmph), &_icmph);
  104. if (ic == NULL)
  105. sprintf(buf, "%s TRUNCATED to %u bytes\n",
  106. pp->name, skb->len - offset);
  107. else
  108. sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u T:%d C:%d",
  109. pp->name, NIPQUAD(ih->saddr),
  110. NIPQUAD(ih->daddr),
  111. ic->type, ic->code);
  112. }
  113. printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
  114. }
  115. static int
  116. icmp_state_transition(struct ip_vs_conn *cp, int direction,
  117. const struct sk_buff *skb,
  118. struct ip_vs_protocol *pp)
  119. {
  120. cp->timeout = pp->timeout_table[IP_VS_ICMP_S_NORMAL];
  121. return 1;
  122. }
  123. static int
  124. icmp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
  125. {
  126. int num;
  127. char **names;
  128. num = IP_VS_ICMP_S_LAST;
  129. names = icmp_state_name_table;
  130. return ip_vs_set_state_timeout(pp->timeout_table, num, names, sname, to);
  131. }
  132. static void icmp_init(struct ip_vs_protocol *pp)
  133. {
  134. pp->timeout_table = icmp_timeouts;
  135. }
  136. static void icmp_exit(struct ip_vs_protocol *pp)
  137. {
  138. }
  139. struct ip_vs_protocol ip_vs_protocol_icmp = {
  140. .name = "ICMP",
  141. .protocol = IPPROTO_ICMP,
  142. .dont_defrag = 0,
  143. .init = icmp_init,
  144. .exit = icmp_exit,
  145. .conn_schedule = icmp_conn_schedule,
  146. .conn_in_get = icmp_conn_in_get,
  147. .conn_out_get = icmp_conn_out_get,
  148. .snat_handler = NULL,
  149. .dnat_handler = NULL,
  150. .csum_check = icmp_csum_check,
  151. .state_transition = icmp_state_transition,
  152. .register_app = NULL,
  153. .unregister_app = NULL,
  154. .app_conn_bind = NULL,
  155. .debug_packet = icmp_debug_packet,
  156. .timeout_change = NULL,
  157. .set_state_timeout = icmp_set_state_timeout,
  158. };