ip_vs_proto.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * ip_vs_proto.c: transport protocol load balancing support for IPVS
  3. *
  4. * Version: $Id: ip_vs_proto.c,v 1.2 2003/04/18 09:03:16 wensong Exp $
  5. *
  6. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  7. * Julian Anastasov <ja@ssi.bg>
  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. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Changes:
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/in.h>
  21. #include <linux/ip.h>
  22. #include <net/protocol.h>
  23. #include <net/tcp.h>
  24. #include <net/udp.h>
  25. #include <asm/system.h>
  26. #include <linux/stat.h>
  27. #include <linux/proc_fs.h>
  28. #include <net/ip_vs.h>
  29. /*
  30. * IPVS protocols can only be registered/unregistered when the ipvs
  31. * module is loaded/unloaded, so no lock is needed in accessing the
  32. * ipvs protocol table.
  33. */
  34. #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
  35. #define IP_VS_PROTO_HASH(proto) ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
  36. static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
  37. /*
  38. * register an ipvs protocol
  39. */
  40. static int __used register_ip_vs_protocol(struct ip_vs_protocol *pp)
  41. {
  42. unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
  43. pp->next = ip_vs_proto_table[hash];
  44. ip_vs_proto_table[hash] = pp;
  45. if (pp->init != NULL)
  46. pp->init(pp);
  47. return 0;
  48. }
  49. /*
  50. * unregister an ipvs protocol
  51. */
  52. static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
  53. {
  54. struct ip_vs_protocol **pp_p;
  55. unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
  56. pp_p = &ip_vs_proto_table[hash];
  57. for (; *pp_p; pp_p = &(*pp_p)->next) {
  58. if (*pp_p == pp) {
  59. *pp_p = pp->next;
  60. if (pp->exit != NULL)
  61. pp->exit(pp);
  62. return 0;
  63. }
  64. }
  65. return -ESRCH;
  66. }
  67. /*
  68. * get ip_vs_protocol object by its proto.
  69. */
  70. struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
  71. {
  72. struct ip_vs_protocol *pp;
  73. unsigned hash = IP_VS_PROTO_HASH(proto);
  74. for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
  75. if (pp->protocol == proto)
  76. return pp;
  77. }
  78. return NULL;
  79. }
  80. /*
  81. * Propagate event for state change to all protocols
  82. */
  83. void ip_vs_protocol_timeout_change(int flags)
  84. {
  85. struct ip_vs_protocol *pp;
  86. int i;
  87. for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
  88. for (pp = ip_vs_proto_table[i]; pp; pp = pp->next) {
  89. if (pp->timeout_change)
  90. pp->timeout_change(pp, flags);
  91. }
  92. }
  93. }
  94. int *
  95. ip_vs_create_timeout_table(int *table, int size)
  96. {
  97. return kmemdup(table, size, GFP_ATOMIC);
  98. }
  99. /*
  100. * Set timeout value for state specified by name
  101. */
  102. int
  103. ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to)
  104. {
  105. int i;
  106. if (!table || !name || !to)
  107. return -EINVAL;
  108. for (i = 0; i < num; i++) {
  109. if (strcmp(names[i], name))
  110. continue;
  111. table[i] = to * HZ;
  112. return 0;
  113. }
  114. return -ENOENT;
  115. }
  116. const char * ip_vs_state_name(__u16 proto, int state)
  117. {
  118. struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
  119. if (pp == NULL || pp->state_name == NULL)
  120. return "ERR!";
  121. return pp->state_name(state);
  122. }
  123. void
  124. ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp,
  125. const struct sk_buff *skb,
  126. int offset,
  127. const char *msg)
  128. {
  129. char buf[128];
  130. struct iphdr _iph, *ih;
  131. ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
  132. if (ih == NULL)
  133. sprintf(buf, "%s TRUNCATED", pp->name);
  134. else if (ih->frag_off & htons(IP_OFFSET))
  135. sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u frag",
  136. pp->name, NIPQUAD(ih->saddr),
  137. NIPQUAD(ih->daddr));
  138. else {
  139. __be16 _ports[2], *pptr
  140. ;
  141. pptr = skb_header_pointer(skb, offset + ih->ihl*4,
  142. sizeof(_ports), _ports);
  143. if (pptr == NULL)
  144. sprintf(buf, "%s TRUNCATED %u.%u.%u.%u->%u.%u.%u.%u",
  145. pp->name,
  146. NIPQUAD(ih->saddr),
  147. NIPQUAD(ih->daddr));
  148. else
  149. sprintf(buf, "%s %u.%u.%u.%u:%u->%u.%u.%u.%u:%u",
  150. pp->name,
  151. NIPQUAD(ih->saddr),
  152. ntohs(pptr[0]),
  153. NIPQUAD(ih->daddr),
  154. ntohs(pptr[1]));
  155. }
  156. printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
  157. }
  158. int ip_vs_protocol_init(void)
  159. {
  160. char protocols[64];
  161. #define REGISTER_PROTOCOL(p) \
  162. do { \
  163. register_ip_vs_protocol(p); \
  164. strcat(protocols, ", "); \
  165. strcat(protocols, (p)->name); \
  166. } while (0)
  167. protocols[0] = '\0';
  168. protocols[2] = '\0';
  169. #ifdef CONFIG_IP_VS_PROTO_TCP
  170. REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
  171. #endif
  172. #ifdef CONFIG_IP_VS_PROTO_UDP
  173. REGISTER_PROTOCOL(&ip_vs_protocol_udp);
  174. #endif
  175. #ifdef CONFIG_IP_VS_PROTO_AH
  176. REGISTER_PROTOCOL(&ip_vs_protocol_ah);
  177. #endif
  178. #ifdef CONFIG_IP_VS_PROTO_ESP
  179. REGISTER_PROTOCOL(&ip_vs_protocol_esp);
  180. #endif
  181. IP_VS_INFO("Registered protocols (%s)\n", &protocols[2]);
  182. return 0;
  183. }
  184. void ip_vs_protocol_cleanup(void)
  185. {
  186. struct ip_vs_protocol *pp;
  187. int i;
  188. /* unregister all the ipvs protocols */
  189. for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
  190. while ((pp = ip_vs_proto_table[i]) != NULL)
  191. unregister_ip_vs_protocol(pp);
  192. }
  193. }