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