b06da1c3445a8d81e87c7ef995ec4ffd9e9824e7
[pandora-kernel.git] / net / ipv4 / ipvs / ip_vs_proto.c
1 /*
2  * ip_vs_proto.c: transport protocol load balancing support for IPVS
3  *
4  * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
5  *              Julian Anastasov <ja@ssi.bg>
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  *              as published by the Free Software Foundation; either version
10  *              2 of the License, or (at your option) any later version.
11  *
12  * Changes:
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/skbuff.h>
19 #include <linux/in.h>
20 #include <linux/ip.h>
21 #include <net/protocol.h>
22 #include <net/tcp.h>
23 #include <net/udp.h>
24 #include <asm/system.h>
25 #include <linux/stat.h>
26 #include <linux/proc_fs.h>
27
28 #include <net/ip_vs.h>
29
30
31 /*
32  * IPVS protocols can only be registered/unregistered when the ipvs
33  * module is loaded/unloaded, so no lock is needed in accessing the
34  * ipvs protocol table.
35  */
36
37 #define IP_VS_PROTO_TAB_SIZE            32      /* must be power of 2 */
38 #define IP_VS_PROTO_HASH(proto)         ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
39
40 static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
41
42
43 /*
44  *      register an ipvs protocol
45  */
46 static int __used __init register_ip_vs_protocol(struct ip_vs_protocol *pp)
47 {
48         unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
49
50         pp->next = ip_vs_proto_table[hash];
51         ip_vs_proto_table[hash] = pp;
52
53         if (pp->init != NULL)
54                 pp->init(pp);
55
56         return 0;
57 }
58
59
60 /*
61  *      unregister an ipvs protocol
62  */
63 static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
64 {
65         struct ip_vs_protocol **pp_p;
66         unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
67
68         pp_p = &ip_vs_proto_table[hash];
69         for (; *pp_p; pp_p = &(*pp_p)->next) {
70                 if (*pp_p == pp) {
71                         *pp_p = pp->next;
72                         if (pp->exit != NULL)
73                                 pp->exit(pp);
74                         return 0;
75                 }
76         }
77
78         return -ESRCH;
79 }
80
81
82 /*
83  *      get ip_vs_protocol object by its proto.
84  */
85 struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
86 {
87         struct ip_vs_protocol *pp;
88         unsigned hash = IP_VS_PROTO_HASH(proto);
89
90         for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
91                 if (pp->protocol == proto)
92                         return pp;
93         }
94
95         return NULL;
96 }
97
98
99 /*
100  *      Propagate event for state change to all protocols
101  */
102 void ip_vs_protocol_timeout_change(int flags)
103 {
104         struct ip_vs_protocol *pp;
105         int i;
106
107         for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
108                 for (pp = ip_vs_proto_table[i]; pp; pp = pp->next) {
109                         if (pp->timeout_change)
110                                 pp->timeout_change(pp, flags);
111                 }
112         }
113 }
114
115
116 int *
117 ip_vs_create_timeout_table(int *table, int size)
118 {
119         return kmemdup(table, size, GFP_ATOMIC);
120 }
121
122
123 /*
124  *      Set timeout value for state specified by name
125  */
126 int
127 ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to)
128 {
129         int i;
130
131         if (!table || !name || !to)
132                 return -EINVAL;
133
134         for (i = 0; i < num; i++) {
135                 if (strcmp(names[i], name))
136                         continue;
137                 table[i] = to * HZ;
138                 return 0;
139         }
140         return -ENOENT;
141 }
142
143
144 const char * ip_vs_state_name(__u16 proto, int state)
145 {
146         struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
147
148         if (pp == NULL || pp->state_name == NULL)
149                 return (IPPROTO_IP == proto) ? "NONE" : "ERR!";
150         return pp->state_name(state);
151 }
152
153
154 static void
155 ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol *pp,
156                              const struct sk_buff *skb,
157                              int offset,
158                              const char *msg)
159 {
160         char buf[128];
161         struct iphdr _iph, *ih;
162
163         ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
164         if (ih == NULL)
165                 sprintf(buf, "%s TRUNCATED", pp->name);
166         else if (ih->frag_off & htons(IP_OFFSET))
167                 sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u frag",
168                         pp->name, NIPQUAD(ih->saddr),
169                         NIPQUAD(ih->daddr));
170         else {
171                 __be16 _ports[2], *pptr
172 ;
173                 pptr = skb_header_pointer(skb, offset + ih->ihl*4,
174                                           sizeof(_ports), _ports);
175                 if (pptr == NULL)
176                         sprintf(buf, "%s TRUNCATED %u.%u.%u.%u->%u.%u.%u.%u",
177                                 pp->name,
178                                 NIPQUAD(ih->saddr),
179                                 NIPQUAD(ih->daddr));
180                 else
181                         sprintf(buf, "%s %u.%u.%u.%u:%u->%u.%u.%u.%u:%u",
182                                 pp->name,
183                                 NIPQUAD(ih->saddr),
184                                 ntohs(pptr[0]),
185                                 NIPQUAD(ih->daddr),
186                                 ntohs(pptr[1]));
187         }
188
189         printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
190 }
191
192 #ifdef CONFIG_IP_VS_IPV6
193 static void
194 ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol *pp,
195                              const struct sk_buff *skb,
196                              int offset,
197                              const char *msg)
198 {
199         char buf[192];
200         struct ipv6hdr _iph, *ih;
201
202         ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
203         if (ih == NULL)
204                 sprintf(buf, "%s TRUNCATED", pp->name);
205         else if (ih->nexthdr == IPPROTO_FRAGMENT)
206                 sprintf(buf, "%s " NIP6_FMT "->" NIP6_FMT " frag",
207                         pp->name, NIP6(ih->saddr),
208                         NIP6(ih->daddr));
209         else {
210                 __be16 _ports[2], *pptr;
211
212                 pptr = skb_header_pointer(skb, offset + sizeof(struct ipv6hdr),
213                                           sizeof(_ports), _ports);
214                 if (pptr == NULL)
215                         sprintf(buf, "%s TRUNCATED " NIP6_FMT "->" NIP6_FMT,
216                                 pp->name,
217                                 NIP6(ih->saddr),
218                                 NIP6(ih->daddr));
219                 else
220                         sprintf(buf, "%s " NIP6_FMT ":%u->" NIP6_FMT ":%u",
221                                 pp->name,
222                                 NIP6(ih->saddr),
223                                 ntohs(pptr[0]),
224                                 NIP6(ih->daddr),
225                                 ntohs(pptr[1]));
226         }
227
228         printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
229 }
230 #endif
231
232
233 void
234 ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp,
235                           const struct sk_buff *skb,
236                           int offset,
237                           const char *msg)
238 {
239 #ifdef CONFIG_IP_VS_IPV6
240         if (skb->protocol == __constant_htons(ETH_P_IPV6))
241                 ip_vs_tcpudp_debug_packet_v6(pp, skb, offset, msg);
242         else
243 #endif
244                 ip_vs_tcpudp_debug_packet_v4(pp, skb, offset, msg);
245 }
246
247
248 int __init ip_vs_protocol_init(void)
249 {
250         char protocols[64];
251 #define REGISTER_PROTOCOL(p)                    \
252         do {                                    \
253                 register_ip_vs_protocol(p);     \
254                 strcat(protocols, ", ");        \
255                 strcat(protocols, (p)->name);   \
256         } while (0)
257
258         protocols[0] = '\0';
259         protocols[2] = '\0';
260 #ifdef CONFIG_IP_VS_PROTO_TCP
261         REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
262 #endif
263 #ifdef CONFIG_IP_VS_PROTO_UDP
264         REGISTER_PROTOCOL(&ip_vs_protocol_udp);
265 #endif
266 #ifdef CONFIG_IP_VS_PROTO_AH
267         REGISTER_PROTOCOL(&ip_vs_protocol_ah);
268 #endif
269 #ifdef CONFIG_IP_VS_PROTO_ESP
270         REGISTER_PROTOCOL(&ip_vs_protocol_esp);
271 #endif
272         IP_VS_INFO("Registered protocols (%s)\n", &protocols[2]);
273
274         return 0;
275 }
276
277
278 void ip_vs_protocol_cleanup(void)
279 {
280         struct ip_vs_protocol *pp;
281         int i;
282
283         /* unregister all the ipvs protocols */
284         for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
285                 while ((pp = ip_vs_proto_table[i]) != NULL)
286                         unregister_ip_vs_protocol(pp);
287         }
288 }