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