blob: 939f7fbe9b46d832ffab36569e7e4b67f3a0bc68 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ip_vs_proto.c: transport protocol load balancing support for IPVS
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Hannes Eder9aada7a2009-07-30 14:29:44 -070016#define KMSG_COMPONENT "IPVS"
17#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/in.h>
24#include <linux/ip.h>
25#include <net/protocol.h>
26#include <net/tcp.h>
27#include <net/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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
43static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
44
45
46/*
47 * register an ipvs protocol
48 */
Sven Wegener048cf482008-08-10 18:24:35 +000049static int __used __init register_ip_vs_protocol(struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Eric Dumazet95c96172012-04-15 05:58:06 +000051 unsigned int hash = IP_VS_PROTO_HASH(pp->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
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
Hans Schillstrom252c6412011-01-03 14:44:46 +010062/*
63 * register an ipvs protocols netns related data
64 */
65static int
66register_ip_vs_proto_netns(struct net *net, struct ip_vs_protocol *pp)
67{
68 struct netns_ipvs *ipvs = net_ipvs(net);
Eric Dumazet95c96172012-04-15 05:58:06 +000069 unsigned int hash = IP_VS_PROTO_HASH(pp->protocol);
Hans Schillstrom252c6412011-01-03 14:44:46 +010070 struct ip_vs_proto_data *pd =
Sasha Levin9615e61e2012-04-14 12:37:47 -040071 kzalloc(sizeof(struct ip_vs_proto_data), GFP_KERNEL);
Hans Schillstrom252c6412011-01-03 14:44:46 +010072
Joe Perches0a9ee812011-08-29 14:17:25 -070073 if (!pd)
Hans Schillstrom252c6412011-01-03 14:44:46 +010074 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -070075
Hans Schillstrom252c6412011-01-03 14:44:46 +010076 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
Hans Schillstrom582b8e32012-04-26 09:45:35 +020081 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 }
Hans Schillstrom252c6412011-01-03 14:44:46 +010090
91 return 0;
92}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * unregister an ipvs protocol
96 */
97static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
98{
99 struct ip_vs_protocol **pp_p;
Eric Dumazet95c96172012-04-15 05:58:06 +0000100 unsigned int hash = IP_VS_PROTO_HASH(pp->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
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
Hans Schillstrom252c6412011-01-03 14:44:46 +0100115/*
116 * unregister an ipvs protocols netns data
117 */
118static int
119unregister_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;
Eric Dumazet95c96172012-04-15 05:58:06 +0000123 unsigned int hash = IP_VS_PROTO_HASH(pd->pp->protocol);
Hans Schillstrom252c6412011-01-03 14:44:46 +0100124
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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/*
140 * get ip_vs_protocol object by its proto.
141 */
142struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
143{
144 struct ip_vs_protocol *pp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000145 unsigned int hash = IP_VS_PROTO_HASH(proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
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}
Hannes Eder9c3e1c32010-07-23 12:42:58 +0200154EXPORT_SYMBOL(ip_vs_proto_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Hans Schillstrom252c6412011-01-03 14:44:46 +0100156/*
157 * get ip_vs_protocol object data by netns and proto
158 */
H Hartley Sweeten068d5222012-04-26 11:26:15 -0700159static struct ip_vs_proto_data *
Hans Schillstrom93304192011-01-03 14:44:51 +0100160__ipvs_proto_data_get(struct netns_ipvs *ipvs, unsigned short proto)
Hans Schillstrom252c6412011-01-03 14:44:46 +0100161{
Hans Schillstrom252c6412011-01-03 14:44:46 +0100162 struct ip_vs_proto_data *pd;
Eric Dumazet95c96172012-04-15 05:58:06 +0000163 unsigned int hash = IP_VS_PROTO_HASH(proto);
Hans Schillstrom252c6412011-01-03 14:44:46 +0100164
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}
Hans Schillstrom93304192011-01-03 14:44:51 +0100172
173struct ip_vs_proto_data *
174ip_vs_proto_data_get(struct net *net, unsigned short proto)
175{
176 struct netns_ipvs *ipvs = net_ipvs(net);
177
178 return __ipvs_proto_data_get(ipvs, proto);
179}
Hans Schillstrom252c6412011-01-03 14:44:46 +0100180EXPORT_SYMBOL(ip_vs_proto_data_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/*
183 * Propagate event for state change to all protocols
184 */
Hans Schillstrom93304192011-01-03 14:44:51 +0100185void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Hans Schillstrom93304192011-01-03 14:44:51 +0100187 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int i;
189
190 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
Hans Schillstrom93304192011-01-03 14:44:51 +0100191 for (pd = ipvs->proto_data_table[i]; pd; pd = pd->next) {
192 if (pd->pp->timeout_change)
193 pd->pp->timeout_change(pd, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 }
196}
197
198
199int *
200ip_vs_create_timeout_table(int *table, int size)
201{
Julian Anastasov41cff6d2012-04-13 16:49:37 +0300202 return kmemdup(table, size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205
206/*
207 * Set timeout value for state specified by name
208 */
209int
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700210ip_vs_set_state_timeout(int *table, int num, const char *const *names,
211 const char *name, int to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 int i;
214
215 if (!table || !name || !to)
216 return -EINVAL;
217
218 for (i = 0; i < num; i++) {
219 if (strcmp(names[i], name))
220 continue;
221 table[i] = to * HZ;
222 return 0;
223 }
224 return -ENOENT;
225}
226
227
228const char * ip_vs_state_name(__u16 proto, int state)
229{
230 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
231
232 if (pp == NULL || pp->state_name == NULL)
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700233 return (IPPROTO_IP == proto) ? "NONE" : "ERR!";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return pp->state_name(state);
235}
236
237
Sven Wegener77eb8512008-09-05 14:43:00 +0200238static void
Julius Volz3b047d92008-09-02 15:55:41 +0200239ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol *pp,
240 const struct sk_buff *skb,
241 int offset,
242 const char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 char buf[128];
245 struct iphdr _iph, *ih;
246
247 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
248 if (ih == NULL)
Patrick McHardy3d91c1a2010-04-08 13:35:47 +0200249 sprintf(buf, "TRUNCATED");
YOSHIFUJI Hideaki5661df72007-12-12 03:53:11 +0900250 else if (ih->frag_off & htons(IP_OFFSET))
Patrick McHardy3d91c1a2010-04-08 13:35:47 +0200251 sprintf(buf, "%pI4->%pI4 frag", &ih->saddr, &ih->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 else {
Julian Anastasov0d796412010-10-17 16:46:17 +0300253 __be16 _ports[2], *pptr;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 pptr = skb_header_pointer(skb, offset + ih->ihl*4,
256 sizeof(_ports), _ports);
257 if (pptr == NULL)
Patrick McHardy3d91c1a2010-04-08 13:35:47 +0200258 sprintf(buf, "TRUNCATED %pI4->%pI4",
259 &ih->saddr, &ih->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 else
Patrick McHardy3d91c1a2010-04-08 13:35:47 +0200261 sprintf(buf, "%pI4:%u->%pI4:%u",
Harvey Harrison14d5e832008-10-31 00:54:29 -0700262 &ih->saddr, ntohs(pptr[0]),
263 &ih->daddr, ntohs(pptr[1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
Patrick McHardy3d91c1a2010-04-08 13:35:47 +0200266 pr_debug("%s: %s %s\n", msg, pp->name, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Julius Volz3b047d92008-09-02 15:55:41 +0200269#ifdef CONFIG_IP_VS_IPV6
Sven Wegener77eb8512008-09-05 14:43:00 +0200270static void
Julius Volz3b047d92008-09-02 15:55:41 +0200271ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol *pp,
272 const struct sk_buff *skb,
273 int offset,
274 const char *msg)
275{
276 char buf[192];
277 struct ipv6hdr _iph, *ih;
278
279 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
280 if (ih == NULL)
Patrick McHardy3d91c1a2010-04-08 13:35:47 +0200281 sprintf(buf, "TRUNCATED");
Julius Volz3b047d92008-09-02 15:55:41 +0200282 else if (ih->nexthdr == IPPROTO_FRAGMENT)
Jesper Dangaard Brouer120b9c12012-09-26 14:05:53 +0200283 sprintf(buf, "%pI6c->%pI6c frag", &ih->saddr, &ih->daddr);
Julius Volz3b047d92008-09-02 15:55:41 +0200284 else {
285 __be16 _ports[2], *pptr;
286
287 pptr = skb_header_pointer(skb, offset + sizeof(struct ipv6hdr),
288 sizeof(_ports), _ports);
289 if (pptr == NULL)
Jesper Dangaard Brouer120b9c12012-09-26 14:05:53 +0200290 sprintf(buf, "TRUNCATED %pI6c->%pI6c",
Patrick McHardy3d91c1a2010-04-08 13:35:47 +0200291 &ih->saddr, &ih->daddr);
Julius Volz3b047d92008-09-02 15:55:41 +0200292 else
Jesper Dangaard Brouer120b9c12012-09-26 14:05:53 +0200293 sprintf(buf, "%pI6c:%u->%pI6c:%u",
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700294 &ih->saddr, ntohs(pptr[0]),
295 &ih->daddr, ntohs(pptr[1]));
Julius Volz3b047d92008-09-02 15:55:41 +0200296 }
297
Patrick McHardy3d91c1a2010-04-08 13:35:47 +0200298 pr_debug("%s: %s %s\n", msg, pp->name, buf);
Julius Volz3b047d92008-09-02 15:55:41 +0200299}
300#endif
301
302
303void
Julian Anastasov0d796412010-10-17 16:46:17 +0300304ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
Julius Volz3b047d92008-09-02 15:55:41 +0200305 const struct sk_buff *skb,
306 int offset,
307 const char *msg)
308{
309#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov0d796412010-10-17 16:46:17 +0300310 if (af == AF_INET6)
Julius Volz3b047d92008-09-02 15:55:41 +0200311 ip_vs_tcpudp_debug_packet_v6(pp, skb, offset, msg);
312 else
313#endif
314 ip_vs_tcpudp_debug_packet_v4(pp, skb, offset, msg);
315}
316
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100317/*
318 * per network name-space init
319 */
Hans Schillstrom503cf152011-05-01 18:50:16 +0200320int __net_init ip_vs_protocol_net_init(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100321{
Sasha Levin7118c072012-04-14 12:37:46 -0400322 int i, ret;
323 static struct ip_vs_protocol *protos[] = {
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100324#ifdef CONFIG_IP_VS_PROTO_TCP
Sasha Levin7118c072012-04-14 12:37:46 -0400325 &ip_vs_protocol_tcp,
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100326#endif
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100327#ifdef CONFIG_IP_VS_PROTO_UDP
Sasha Levin7118c072012-04-14 12:37:46 -0400328 &ip_vs_protocol_udp,
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100329#endif
Hans Schillstrom9d934872011-01-03 14:44:49 +0100330#ifdef CONFIG_IP_VS_PROTO_SCTP
Sasha Levin7118c072012-04-14 12:37:46 -0400331 &ip_vs_protocol_sctp,
Hans Schillstrom9d934872011-01-03 14:44:49 +0100332#endif
Hans Schillstrom88fe2d32011-01-03 14:44:50 +0100333#ifdef CONFIG_IP_VS_PROTO_AH
Sasha Levin7118c072012-04-14 12:37:46 -0400334 &ip_vs_protocol_ah,
Hans Schillstrom88fe2d32011-01-03 14:44:50 +0100335#endif
336#ifdef CONFIG_IP_VS_PROTO_ESP
Sasha Levin7118c072012-04-14 12:37:46 -0400337 &ip_vs_protocol_esp,
Hans Schillstrom88fe2d32011-01-03 14:44:50 +0100338#endif
Sasha Levin7118c072012-04-14 12:37:46 -0400339 };
340
341 for (i = 0; i < ARRAY_SIZE(protos); i++) {
342 ret = register_ip_vs_proto_netns(net, protos[i]);
343 if (ret < 0)
344 goto cleanup;
345 }
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100346 return 0;
Sasha Levin7118c072012-04-14 12:37:46 -0400347
348cleanup:
349 ip_vs_protocol_net_cleanup(net);
350 return ret;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100351}
352
Hans Schillstrom503cf152011-05-01 18:50:16 +0200353void __net_exit ip_vs_protocol_net_cleanup(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100354{
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100355 struct netns_ipvs *ipvs = net_ipvs(net);
356 struct ip_vs_proto_data *pd;
357 int i;
358
359 /* unregister all the ipvs proto data for this netns */
360 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
361 while ((pd = ipvs->proto_data_table[i]) != NULL)
362 unregister_ip_vs_proto_netns(net, pd);
363 }
Hans Schillstrom61b1ab42011-01-03 14:44:42 +0100364}
365
Sven Wegener048cf482008-08-10 18:24:35 +0000366int __init ip_vs_protocol_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 char protocols[64];
369#define REGISTER_PROTOCOL(p) \
370 do { \
371 register_ip_vs_protocol(p); \
372 strcat(protocols, ", "); \
373 strcat(protocols, (p)->name); \
374 } while (0)
375
376 protocols[0] = '\0';
377 protocols[2] = '\0';
378#ifdef CONFIG_IP_VS_PROTO_TCP
379 REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
380#endif
381#ifdef CONFIG_IP_VS_PROTO_UDP
382 REGISTER_PROTOCOL(&ip_vs_protocol_udp);
383#endif
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100384#ifdef CONFIG_IP_VS_PROTO_SCTP
385 REGISTER_PROTOCOL(&ip_vs_protocol_sctp);
386#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387#ifdef CONFIG_IP_VS_PROTO_AH
388 REGISTER_PROTOCOL(&ip_vs_protocol_ah);
389#endif
390#ifdef CONFIG_IP_VS_PROTO_ESP
391 REGISTER_PROTOCOL(&ip_vs_protocol_esp);
392#endif
Hannes Eder1e3e2382009-08-02 11:05:41 +0000393 pr_info("Registered protocols (%s)\n", &protocols[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 return 0;
396}
397
398
399void ip_vs_protocol_cleanup(void)
400{
401 struct ip_vs_protocol *pp;
402 int i;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* unregister all the ipvs protocols */
405 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
406 while ((pp = ip_vs_proto_table[i]) != NULL)
407 unregister_ip_vs_protocol(pp);
408 }
409}