2 * ip_vs_proto.c: transport protocol load balancing support for IPVS
4 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
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 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
16 #define KMSG_COMPONENT "IPVS"
17 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/skbuff.h>
22 #include <linux/gfp.h>
25 #include <net/protocol.h>
28 #include <asm/system.h>
29 #include <linux/stat.h>
30 #include <linux/proc_fs.h>
32 #include <net/ip_vs.h>
36 * IPVS protocols can only be registered/unregistered when the ipvs
37 * module is loaded/unloaded, so no lock is needed in accessing the
38 * ipvs protocol table.
41 #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
42 #define IP_VS_PROTO_HASH(proto) ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
44 static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
48 * register an ipvs protocol
50 static int __used __init register_ip_vs_protocol(struct ip_vs_protocol *pp)
52 unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
54 pp->next = ip_vs_proto_table[hash];
55 ip_vs_proto_table[hash] = pp;
65 * unregister an ipvs protocol
67 static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
69 struct ip_vs_protocol **pp_p;
70 unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
72 pp_p = &ip_vs_proto_table[hash];
73 for (; *pp_p; pp_p = &(*pp_p)->next) {
87 * get ip_vs_protocol object by its proto.
89 struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
91 struct ip_vs_protocol *pp;
92 unsigned hash = IP_VS_PROTO_HASH(proto);
94 for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
95 if (pp->protocol == proto)
104 * Propagate event for state change to all protocols
106 void ip_vs_protocol_timeout_change(int flags)
108 struct ip_vs_protocol *pp;
111 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
112 for (pp = ip_vs_proto_table[i]; pp; pp = pp->next) {
113 if (pp->timeout_change)
114 pp->timeout_change(pp, flags);
121 ip_vs_create_timeout_table(int *table, int size)
123 return kmemdup(table, size, GFP_ATOMIC);
128 * Set timeout value for state specified by name
131 ip_vs_set_state_timeout(int *table, int num, const char *const *names,
132 const char *name, int to)
136 if (!table || !name || !to)
139 for (i = 0; i < num; i++) {
140 if (strcmp(names[i], name))
149 const char * ip_vs_state_name(__u16 proto, int state)
151 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
153 if (pp == NULL || pp->state_name == NULL)
154 return (IPPROTO_IP == proto) ? "NONE" : "ERR!";
155 return pp->state_name(state);
160 ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol *pp,
161 const struct sk_buff *skb,
166 struct iphdr _iph, *ih;
168 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
170 sprintf(buf, "%s TRUNCATED", pp->name);
171 else if (ih->frag_off & htons(IP_OFFSET))
172 sprintf(buf, "%s %pI4->%pI4 frag",
173 pp->name, &ih->saddr, &ih->daddr);
175 __be16 _ports[2], *pptr
177 pptr = skb_header_pointer(skb, offset + ih->ihl*4,
178 sizeof(_ports), _ports);
180 sprintf(buf, "%s TRUNCATED %pI4->%pI4",
181 pp->name, &ih->saddr, &ih->daddr);
183 sprintf(buf, "%s %pI4:%u->%pI4:%u",
185 &ih->saddr, ntohs(pptr[0]),
186 &ih->daddr, ntohs(pptr[1]));
189 pr_debug("%s: %s\n", msg, buf);
192 #ifdef CONFIG_IP_VS_IPV6
194 ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol *pp,
195 const struct sk_buff *skb,
200 struct ipv6hdr _iph, *ih;
202 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
204 sprintf(buf, "%s TRUNCATED", pp->name);
205 else if (ih->nexthdr == IPPROTO_FRAGMENT)
206 sprintf(buf, "%s %pI6->%pI6 frag",
207 pp->name, &ih->saddr, &ih->daddr);
209 __be16 _ports[2], *pptr;
211 pptr = skb_header_pointer(skb, offset + sizeof(struct ipv6hdr),
212 sizeof(_ports), _ports);
214 sprintf(buf, "%s TRUNCATED %pI6->%pI6",
215 pp->name, &ih->saddr, &ih->daddr);
217 sprintf(buf, "%s %pI6:%u->%pI6:%u",
219 &ih->saddr, ntohs(pptr[0]),
220 &ih->daddr, ntohs(pptr[1]));
223 pr_debug("%s: %s\n", msg, buf);
229 ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp,
230 const struct sk_buff *skb,
234 #ifdef CONFIG_IP_VS_IPV6
235 if (skb->protocol == htons(ETH_P_IPV6))
236 ip_vs_tcpudp_debug_packet_v6(pp, skb, offset, msg);
239 ip_vs_tcpudp_debug_packet_v4(pp, skb, offset, msg);
243 int __init ip_vs_protocol_init(void)
246 #define REGISTER_PROTOCOL(p) \
248 register_ip_vs_protocol(p); \
249 strcat(protocols, ", "); \
250 strcat(protocols, (p)->name); \
255 #ifdef CONFIG_IP_VS_PROTO_TCP
256 REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
258 #ifdef CONFIG_IP_VS_PROTO_UDP
259 REGISTER_PROTOCOL(&ip_vs_protocol_udp);
261 #ifdef CONFIG_IP_VS_PROTO_SCTP
262 REGISTER_PROTOCOL(&ip_vs_protocol_sctp);
264 #ifdef CONFIG_IP_VS_PROTO_AH
265 REGISTER_PROTOCOL(&ip_vs_protocol_ah);
267 #ifdef CONFIG_IP_VS_PROTO_ESP
268 REGISTER_PROTOCOL(&ip_vs_protocol_esp);
270 pr_info("Registered protocols (%s)\n", &protocols[2]);
276 void ip_vs_protocol_cleanup(void)
278 struct ip_vs_protocol *pp;
281 /* unregister all the ipvs protocols */
282 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
283 while ((pp = ip_vs_proto_table[i]) != NULL)
284 unregister_ip_vs_protocol(pp);