ipvs: Verify that IP_VS protocol has been registered
[linux-flexiantxendom0-3.2.10.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 <linux/stat.h>
29 #include <linux/proc_fs.h>
30
31 #include <net/ip_vs.h>
32
33
34 /*
35  * IPVS protocols can only be registered/unregistered when the ipvs
36  * module is loaded/unloaded, so no lock is needed in accessing the
37  * ipvs protocol table.
38  */
39
40 #define IP_VS_PROTO_TAB_SIZE            32      /* must be power of 2 */
41 #define IP_VS_PROTO_HASH(proto)         ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
42
43 static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
44
45
46 /*
47  *      register an ipvs protocol
48  */
49 static int __used __init register_ip_vs_protocol(struct ip_vs_protocol *pp)
50 {
51         unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
52
53         pp->next = ip_vs_proto_table[hash];
54         ip_vs_proto_table[hash] = pp;
55
56         if (pp->init != NULL)
57                 pp->init(pp);
58
59         return 0;
60 }
61
62 /*
63  *      register an ipvs protocols netns related data
64  */
65 static int
66 register_ip_vs_proto_netns(struct net *net, struct ip_vs_protocol *pp)
67 {
68         struct netns_ipvs *ipvs = net_ipvs(net);
69         unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
70         struct ip_vs_proto_data *pd =
71                         kzalloc(sizeof(struct ip_vs_proto_data), GFP_ATOMIC);
72
73         if (!pd)
74                 return -ENOMEM;
75
76         pd->pp = pp;    /* For speed issues */
77         pd->next = ipvs->proto_data_table[hash];
78         ipvs->proto_data_table[hash] = pd;
79         atomic_set(&pd->appcnt, 0);     /* Init app counter */
80
81         if (pp->init_netns != NULL)
82                 pp->init_netns(net, pd);
83
84         return 0;
85 }
86
87 /*
88  *      unregister an ipvs protocol
89  */
90 static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
91 {
92         struct ip_vs_protocol **pp_p;
93         unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
94
95         pp_p = &ip_vs_proto_table[hash];
96         for (; *pp_p; pp_p = &(*pp_p)->next) {
97                 if (*pp_p == pp) {
98                         *pp_p = pp->next;
99                         if (pp->exit != NULL)
100                                 pp->exit(pp);
101                         return 0;
102                 }
103         }
104
105         return -ESRCH;
106 }
107
108 /*
109  *      unregister an ipvs protocols netns data
110  */
111 static int
112 unregister_ip_vs_proto_netns(struct net *net, struct ip_vs_proto_data *pd)
113 {
114         struct netns_ipvs *ipvs = net_ipvs(net);
115         struct ip_vs_proto_data **pd_p;
116         unsigned hash = IP_VS_PROTO_HASH(pd->pp->protocol);
117
118         pd_p = &ipvs->proto_data_table[hash];
119         for (; *pd_p; pd_p = &(*pd_p)->next) {
120                 if (*pd_p == pd) {
121                         *pd_p = pd->next;
122                         if (pd->pp->exit_netns != NULL)
123                                 pd->pp->exit_netns(net, pd);
124                         kfree(pd);
125                         return 0;
126                 }
127         }
128
129         return -ESRCH;
130 }
131
132 /*
133  *      get ip_vs_protocol object by its proto.
134  */
135 struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
136 {
137         struct ip_vs_protocol *pp;
138         unsigned hash = IP_VS_PROTO_HASH(proto);
139
140         for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
141                 if (pp->protocol == proto)
142                         return pp;
143         }
144
145         return NULL;
146 }
147 EXPORT_SYMBOL(ip_vs_proto_get);
148
149 /*
150  *      get ip_vs_protocol object data by netns and proto
151  */
152 struct ip_vs_proto_data *
153 __ipvs_proto_data_get(struct netns_ipvs *ipvs, unsigned short proto)
154 {
155         struct ip_vs_proto_data *pd;
156         unsigned hash = IP_VS_PROTO_HASH(proto);
157
158         for (pd = ipvs->proto_data_table[hash]; pd; pd = pd->next) {
159                 if (pd->pp->protocol == proto)
160                         return pd;
161         }
162
163         return NULL;
164 }
165
166 struct ip_vs_proto_data *
167 ip_vs_proto_data_get(struct net *net, unsigned short proto)
168 {
169         struct netns_ipvs *ipvs = net_ipvs(net);
170
171         return __ipvs_proto_data_get(ipvs, proto);
172 }
173 EXPORT_SYMBOL(ip_vs_proto_data_get);
174
175 /*
176  *      Propagate event for state change to all protocols
177  */
178 void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags)
179 {
180         struct ip_vs_proto_data *pd;
181         int i;
182
183         for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
184                 for (pd = ipvs->proto_data_table[i]; pd; pd = pd->next) {
185                         if (pd->pp->timeout_change)
186                                 pd->pp->timeout_change(pd, flags);
187                 }
188         }
189 }
190
191
192 int *
193 ip_vs_create_timeout_table(int *table, int size)
194 {
195         return kmemdup(table, size, GFP_ATOMIC);
196 }
197
198
199 /*
200  *      Set timeout value for state specified by name
201  */
202 int
203 ip_vs_set_state_timeout(int *table, int num, const char *const *names,
204                         const char *name, int to)
205 {
206         int i;
207
208         if (!table || !name || !to)
209                 return -EINVAL;
210
211         for (i = 0; i < num; i++) {
212                 if (strcmp(names[i], name))
213                         continue;
214                 table[i] = to * HZ;
215                 return 0;
216         }
217         return -ENOENT;
218 }
219
220
221 const char * ip_vs_state_name(__u16 proto, int state)
222 {
223         struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
224
225         if (pp == NULL || pp->state_name == NULL)
226                 return (IPPROTO_IP == proto) ? "NONE" : "ERR!";
227         return pp->state_name(state);
228 }
229
230
231 static void
232 ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol *pp,
233                              const struct sk_buff *skb,
234                              int offset,
235                              const char *msg)
236 {
237         char buf[128];
238         struct iphdr _iph, *ih;
239
240         ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
241         if (ih == NULL)
242                 sprintf(buf, "TRUNCATED");
243         else if (ih->frag_off & htons(IP_OFFSET))
244                 sprintf(buf, "%pI4->%pI4 frag", &ih->saddr, &ih->daddr);
245         else {
246                 __be16 _ports[2], *pptr;
247
248                 pptr = skb_header_pointer(skb, offset + ih->ihl*4,
249                                           sizeof(_ports), _ports);
250                 if (pptr == NULL)
251                         sprintf(buf, "TRUNCATED %pI4->%pI4",
252                                 &ih->saddr, &ih->daddr);
253                 else
254                         sprintf(buf, "%pI4:%u->%pI4:%u",
255                                 &ih->saddr, ntohs(pptr[0]),
256                                 &ih->daddr, ntohs(pptr[1]));
257         }
258
259         pr_debug("%s: %s %s\n", msg, pp->name, buf);
260 }
261
262 #ifdef CONFIG_IP_VS_IPV6
263 static void
264 ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol *pp,
265                              const struct sk_buff *skb,
266                              int offset,
267                              const char *msg)
268 {
269         char buf[192];
270         struct ipv6hdr _iph, *ih;
271
272         ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
273         if (ih == NULL)
274                 sprintf(buf, "TRUNCATED");
275         else if (ih->nexthdr == IPPROTO_FRAGMENT)
276                 sprintf(buf, "%pI6->%pI6 frag", &ih->saddr, &ih->daddr);
277         else {
278                 __be16 _ports[2], *pptr;
279
280                 pptr = skb_header_pointer(skb, offset + sizeof(struct ipv6hdr),
281                                           sizeof(_ports), _ports);
282                 if (pptr == NULL)
283                         sprintf(buf, "TRUNCATED %pI6->%pI6",
284                                 &ih->saddr, &ih->daddr);
285                 else
286                         sprintf(buf, "%pI6:%u->%pI6:%u",
287                                 &ih->saddr, ntohs(pptr[0]),
288                                 &ih->daddr, ntohs(pptr[1]));
289         }
290
291         pr_debug("%s: %s %s\n", msg, pp->name, buf);
292 }
293 #endif
294
295
296 void
297 ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
298                           const struct sk_buff *skb,
299                           int offset,
300                           const char *msg)
301 {
302 #ifdef CONFIG_IP_VS_IPV6
303         if (af == AF_INET6)
304                 ip_vs_tcpudp_debug_packet_v6(pp, skb, offset, msg);
305         else
306 #endif
307                 ip_vs_tcpudp_debug_packet_v4(pp, skb, offset, msg);
308 }
309
310 /*
311  * per network name-space init
312  */
313 int __net_init ip_vs_protocol_net_init(struct net *net)
314 {
315         int i, ret;
316         static struct ip_vs_protocol *protos[] = {
317 #ifdef CONFIG_IP_VS_PROTO_TCP
318         &ip_vs_protocol_tcp,
319 #endif
320 #ifdef CONFIG_IP_VS_PROTO_UDP
321         &ip_vs_protocol_udp,
322 #endif
323 #ifdef CONFIG_IP_VS_PROTO_SCTP
324         &ip_vs_protocol_sctp,
325 #endif
326 #ifdef CONFIG_IP_VS_PROTO_AH
327         &ip_vs_protocol_ah,
328 #endif
329 #ifdef CONFIG_IP_VS_PROTO_ESP
330         &ip_vs_protocol_esp,
331 #endif
332         };
333
334         for (i = 0; i < ARRAY_SIZE(protos); i++) {
335                 ret = register_ip_vs_proto_netns(net, protos[i]);
336                 if (ret < 0)
337                         goto cleanup;
338         }
339         return 0;
340
341 cleanup:
342         ip_vs_protocol_net_cleanup(net);
343         return ret;
344 }
345
346 void __net_exit ip_vs_protocol_net_cleanup(struct net *net)
347 {
348         struct netns_ipvs *ipvs = net_ipvs(net);
349         struct ip_vs_proto_data *pd;
350         int i;
351
352         /* unregister all the ipvs proto data for this netns */
353         for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
354                 while ((pd = ipvs->proto_data_table[i]) != NULL)
355                         unregister_ip_vs_proto_netns(net, pd);
356         }
357 }
358
359 int __init ip_vs_protocol_init(void)
360 {
361         char protocols[64];
362 #define REGISTER_PROTOCOL(p)                    \
363         do {                                    \
364                 register_ip_vs_protocol(p);     \
365                 strcat(protocols, ", ");        \
366                 strcat(protocols, (p)->name);   \
367         } while (0)
368
369         protocols[0] = '\0';
370         protocols[2] = '\0';
371 #ifdef CONFIG_IP_VS_PROTO_TCP
372         REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
373 #endif
374 #ifdef CONFIG_IP_VS_PROTO_UDP
375         REGISTER_PROTOCOL(&ip_vs_protocol_udp);
376 #endif
377 #ifdef CONFIG_IP_VS_PROTO_SCTP
378         REGISTER_PROTOCOL(&ip_vs_protocol_sctp);
379 #endif
380 #ifdef CONFIG_IP_VS_PROTO_AH
381         REGISTER_PROTOCOL(&ip_vs_protocol_ah);
382 #endif
383 #ifdef CONFIG_IP_VS_PROTO_ESP
384         REGISTER_PROTOCOL(&ip_vs_protocol_esp);
385 #endif
386         pr_info("Registered protocols (%s)\n", &protocols[2]);
387
388         return 0;
389 }
390
391
392 void ip_vs_protocol_cleanup(void)
393 {
394         struct ip_vs_protocol *pp;
395         int i;
396
397         /* unregister all the ipvs protocols */
398         for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
399                 while ((pp = ip_vs_proto_table[i]) != NULL)
400                         unregister_ip_vs_protocol(pp);
401         }
402 }