Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ip_vs_proto.c: transport protocol load balancing support for IPVS | |
3 | * | |
1da177e4 LT |
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 | ||
9aada7ac HE |
16 | #define KMSG_COMPONENT "IPVS" |
17 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
18 | ||
1da177e4 LT |
19 | #include <linux/module.h> |
20 | #include <linux/kernel.h> | |
21 | #include <linux/skbuff.h> | |
5a0e3ad6 | 22 | #include <linux/gfp.h> |
1da177e4 LT |
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 | */ | |
048cf48b | 50 | static int __used __init register_ip_vs_protocol(struct ip_vs_protocol *pp) |
1da177e4 LT |
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 | ||
091bb34c CG |
63 | #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP) || \ |
64 | defined(CONFIG_IP_VS_PROTO_SCTP) || defined(CONFIG_IP_VS_PROTO_AH) || \ | |
65 | defined(CONFIG_IP_VS_PROTO_ESP) | |
252c6410 HS |
66 | /* |
67 | * register an ipvs protocols netns related data | |
68 | */ | |
69 | static int | |
70 | register_ip_vs_proto_netns(struct net *net, struct ip_vs_protocol *pp) | |
71 | { | |
72 | struct netns_ipvs *ipvs = net_ipvs(net); | |
73 | unsigned hash = IP_VS_PROTO_HASH(pp->protocol); | |
74 | struct ip_vs_proto_data *pd = | |
75 | kzalloc(sizeof(struct ip_vs_proto_data), GFP_ATOMIC); | |
76 | ||
77 | if (!pd) { | |
78 | pr_err("%s(): no memory.\n", __func__); | |
79 | return -ENOMEM; | |
80 | } | |
81 | pd->pp = pp; /* For speed issues */ | |
82 | pd->next = ipvs->proto_data_table[hash]; | |
83 | ipvs->proto_data_table[hash] = pd; | |
84 | atomic_set(&pd->appcnt, 0); /* Init app counter */ | |
85 | ||
86 | if (pp->init_netns != NULL) | |
87 | pp->init_netns(net, pd); | |
88 | ||
89 | return 0; | |
90 | } | |
091bb34c | 91 | #endif |
1da177e4 LT |
92 | |
93 | /* | |
94 | * unregister an ipvs protocol | |
95 | */ | |
96 | static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp) | |
97 | { | |
98 | struct ip_vs_protocol **pp_p; | |
99 | unsigned hash = IP_VS_PROTO_HASH(pp->protocol); | |
100 | ||
101 | pp_p = &ip_vs_proto_table[hash]; | |
102 | for (; *pp_p; pp_p = &(*pp_p)->next) { | |
103 | if (*pp_p == pp) { | |
104 | *pp_p = pp->next; | |
105 | if (pp->exit != NULL) | |
106 | pp->exit(pp); | |
107 | return 0; | |
108 | } | |
109 | } | |
110 | ||
111 | return -ESRCH; | |
112 | } | |
113 | ||
252c6410 HS |
114 | /* |
115 | * unregister an ipvs protocols netns data | |
116 | */ | |
117 | static int | |
118 | unregister_ip_vs_proto_netns(struct net *net, struct ip_vs_proto_data *pd) | |
119 | { | |
120 | struct netns_ipvs *ipvs = net_ipvs(net); | |
121 | struct ip_vs_proto_data **pd_p; | |
122 | unsigned hash = IP_VS_PROTO_HASH(pd->pp->protocol); | |
123 | ||
124 | pd_p = &ipvs->proto_data_table[hash]; | |
125 | for (; *pd_p; pd_p = &(*pd_p)->next) { | |
126 | if (*pd_p == pd) { | |
127 | *pd_p = pd->next; | |
128 | if (pd->pp->exit_netns != NULL) | |
129 | pd->pp->exit_netns(net, pd); | |
130 | kfree(pd); | |
131 | return 0; | |
132 | } | |
133 | } | |
134 | ||
135 | return -ESRCH; | |
136 | } | |
1da177e4 LT |
137 | |
138 | /* | |
139 | * get ip_vs_protocol object by its proto. | |
140 | */ | |
141 | struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto) | |
142 | { | |
143 | struct ip_vs_protocol *pp; | |
144 | unsigned hash = IP_VS_PROTO_HASH(proto); | |
145 | ||
146 | for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) { | |
147 | if (pp->protocol == proto) | |
148 | return pp; | |
149 | } | |
150 | ||
151 | return NULL; | |
152 | } | |
9c3e1c39 | 153 | EXPORT_SYMBOL(ip_vs_proto_get); |
1da177e4 | 154 | |
252c6410 HS |
155 | /* |
156 | * get ip_vs_protocol object data by netns and proto | |
157 | */ | |
158 | struct ip_vs_proto_data * | |
9330419d | 159 | __ipvs_proto_data_get(struct netns_ipvs *ipvs, unsigned short proto) |
252c6410 | 160 | { |
252c6410 HS |
161 | struct ip_vs_proto_data *pd; |
162 | unsigned hash = IP_VS_PROTO_HASH(proto); | |
163 | ||
164 | for (pd = ipvs->proto_data_table[hash]; pd; pd = pd->next) { | |
165 | if (pd->pp->protocol == proto) | |
166 | return pd; | |
167 | } | |
168 | ||
169 | return NULL; | |
170 | } | |
9330419d HS |
171 | |
172 | struct ip_vs_proto_data * | |
173 | ip_vs_proto_data_get(struct net *net, unsigned short proto) | |
174 | { | |
175 | struct netns_ipvs *ipvs = net_ipvs(net); | |
176 | ||
177 | return __ipvs_proto_data_get(ipvs, proto); | |
178 | } | |
252c6410 | 179 | EXPORT_SYMBOL(ip_vs_proto_data_get); |
1da177e4 LT |
180 | |
181 | /* | |
182 | * Propagate event for state change to all protocols | |
183 | */ | |
9330419d | 184 | void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags) |
1da177e4 | 185 | { |
9330419d | 186 | struct ip_vs_proto_data *pd; |
1da177e4 LT |
187 | int i; |
188 | ||
189 | for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) { | |
9330419d HS |
190 | for (pd = ipvs->proto_data_table[i]; pd; pd = pd->next) { |
191 | if (pd->pp->timeout_change) | |
192 | pd->pp->timeout_change(pd, flags); | |
1da177e4 LT |
193 | } |
194 | } | |
195 | } | |
196 | ||
197 | ||
198 | int * | |
199 | ip_vs_create_timeout_table(int *table, int size) | |
200 | { | |
8b2ed4bb | 201 | return kmemdup(table, size, GFP_ATOMIC); |
1da177e4 LT |
202 | } |
203 | ||
204 | ||
205 | /* | |
206 | * Set timeout value for state specified by name | |
207 | */ | |
208 | int | |
36cbd3dc JE |
209 | ip_vs_set_state_timeout(int *table, int num, const char *const *names, |
210 | const char *name, int to) | |
1da177e4 LT |
211 | { |
212 | int i; | |
213 | ||
214 | if (!table || !name || !to) | |
215 | return -EINVAL; | |
216 | ||
217 | for (i = 0; i < num; i++) { | |
218 | if (strcmp(names[i], name)) | |
219 | continue; | |
220 | table[i] = to * HZ; | |
221 | return 0; | |
222 | } | |
223 | return -ENOENT; | |
224 | } | |
225 | ||
226 | ||
227 | const char * ip_vs_state_name(__u16 proto, int state) | |
228 | { | |
229 | struct ip_vs_protocol *pp = ip_vs_proto_get(proto); | |
230 | ||
231 | if (pp == NULL || pp->state_name == NULL) | |
2ad17def | 232 | return (IPPROTO_IP == proto) ? "NONE" : "ERR!"; |
1da177e4 LT |
233 | return pp->state_name(state); |
234 | } | |
235 | ||
236 | ||
77eb8516 | 237 | static void |
3b047d9d JV |
238 | ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol *pp, |
239 | const struct sk_buff *skb, | |
240 | int offset, | |
241 | const char *msg) | |
1da177e4 LT |
242 | { |
243 | char buf[128]; | |
244 | struct iphdr _iph, *ih; | |
245 | ||
246 | ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); | |
247 | if (ih == NULL) | |
3d91c1a8 | 248 | sprintf(buf, "TRUNCATED"); |
5661df7b | 249 | else if (ih->frag_off & htons(IP_OFFSET)) |
3d91c1a8 | 250 | sprintf(buf, "%pI4->%pI4 frag", &ih->saddr, &ih->daddr); |
1da177e4 | 251 | else { |
0d79641a JA |
252 | __be16 _ports[2], *pptr; |
253 | ||
1da177e4 LT |
254 | pptr = skb_header_pointer(skb, offset + ih->ihl*4, |
255 | sizeof(_ports), _ports); | |
256 | if (pptr == NULL) | |
3d91c1a8 PM |
257 | sprintf(buf, "TRUNCATED %pI4->%pI4", |
258 | &ih->saddr, &ih->daddr); | |
1da177e4 | 259 | else |
3d91c1a8 | 260 | sprintf(buf, "%pI4:%u->%pI4:%u", |
14d5e834 HH |
261 | &ih->saddr, ntohs(pptr[0]), |
262 | &ih->daddr, ntohs(pptr[1])); | |
1da177e4 LT |
263 | } |
264 | ||
3d91c1a8 | 265 | pr_debug("%s: %s %s\n", msg, pp->name, buf); |
1da177e4 LT |
266 | } |
267 | ||
3b047d9d | 268 | #ifdef CONFIG_IP_VS_IPV6 |
77eb8516 | 269 | static void |
3b047d9d JV |
270 | ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol *pp, |
271 | const struct sk_buff *skb, | |
272 | int offset, | |
273 | const char *msg) | |
274 | { | |
275 | char buf[192]; | |
276 | struct ipv6hdr _iph, *ih; | |
277 | ||
278 | ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); | |
279 | if (ih == NULL) | |
3d91c1a8 | 280 | sprintf(buf, "TRUNCATED"); |
3b047d9d | 281 | else if (ih->nexthdr == IPPROTO_FRAGMENT) |
3d91c1a8 | 282 | sprintf(buf, "%pI6->%pI6 frag", &ih->saddr, &ih->daddr); |
3b047d9d JV |
283 | else { |
284 | __be16 _ports[2], *pptr; | |
285 | ||
286 | pptr = skb_header_pointer(skb, offset + sizeof(struct ipv6hdr), | |
287 | sizeof(_ports), _ports); | |
288 | if (pptr == NULL) | |
3d91c1a8 PM |
289 | sprintf(buf, "TRUNCATED %pI6->%pI6", |
290 | &ih->saddr, &ih->daddr); | |
3b047d9d | 291 | else |
3d91c1a8 | 292 | sprintf(buf, "%pI6:%u->%pI6:%u", |
38ff4fa4 HH |
293 | &ih->saddr, ntohs(pptr[0]), |
294 | &ih->daddr, ntohs(pptr[1])); | |
3b047d9d JV |
295 | } |
296 | ||
3d91c1a8 | 297 | pr_debug("%s: %s %s\n", msg, pp->name, buf); |
3b047d9d JV |
298 | } |
299 | #endif | |
300 | ||
301 | ||
302 | void | |
0d79641a | 303 | ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp, |
3b047d9d JV |
304 | const struct sk_buff *skb, |
305 | int offset, | |
306 | const char *msg) | |
307 | { | |
308 | #ifdef CONFIG_IP_VS_IPV6 | |
0d79641a | 309 | if (af == AF_INET6) |
3b047d9d JV |
310 | ip_vs_tcpudp_debug_packet_v6(pp, skb, offset, msg); |
311 | else | |
312 | #endif | |
313 | ip_vs_tcpudp_debug_packet_v4(pp, skb, offset, msg); | |
314 | } | |
315 | ||
61b1ab45 HS |
316 | /* |
317 | * per network name-space init | |
318 | */ | |
7a4f0761 | 319 | int __net_init __ip_vs_protocol_init(struct net *net) |
61b1ab45 | 320 | { |
4a85b96c HS |
321 | #ifdef CONFIG_IP_VS_PROTO_TCP |
322 | register_ip_vs_proto_netns(net, &ip_vs_protocol_tcp); | |
78b16bde HS |
323 | #endif |
324 | #ifdef CONFIG_IP_VS_PROTO_UDP | |
325 | register_ip_vs_proto_netns(net, &ip_vs_protocol_udp); | |
9d934878 HS |
326 | #endif |
327 | #ifdef CONFIG_IP_VS_PROTO_SCTP | |
328 | register_ip_vs_proto_netns(net, &ip_vs_protocol_sctp); | |
88fe2d37 HS |
329 | #endif |
330 | #ifdef CONFIG_IP_VS_PROTO_AH | |
331 | register_ip_vs_proto_netns(net, &ip_vs_protocol_ah); | |
332 | #endif | |
333 | #ifdef CONFIG_IP_VS_PROTO_ESP | |
334 | register_ip_vs_proto_netns(net, &ip_vs_protocol_esp); | |
4a85b96c | 335 | #endif |
61b1ab45 HS |
336 | return 0; |
337 | } | |
338 | ||
7a4f0761 | 339 | void __net_exit __ip_vs_protocol_cleanup(struct net *net) |
61b1ab45 | 340 | { |
4a85b96c HS |
341 | struct netns_ipvs *ipvs = net_ipvs(net); |
342 | struct ip_vs_proto_data *pd; | |
343 | int i; | |
344 | ||
345 | /* unregister all the ipvs proto data for this netns */ | |
346 | for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) { | |
347 | while ((pd = ipvs->proto_data_table[i]) != NULL) | |
348 | unregister_ip_vs_proto_netns(net, pd); | |
349 | } | |
61b1ab45 HS |
350 | } |
351 | ||
048cf48b | 352 | int __init ip_vs_protocol_init(void) |
1da177e4 LT |
353 | { |
354 | char protocols[64]; | |
355 | #define REGISTER_PROTOCOL(p) \ | |
356 | do { \ | |
357 | register_ip_vs_protocol(p); \ | |
358 | strcat(protocols, ", "); \ | |
359 | strcat(protocols, (p)->name); \ | |
360 | } while (0) | |
361 | ||
362 | protocols[0] = '\0'; | |
363 | protocols[2] = '\0'; | |
364 | #ifdef CONFIG_IP_VS_PROTO_TCP | |
365 | REGISTER_PROTOCOL(&ip_vs_protocol_tcp); | |
366 | #endif | |
367 | #ifdef CONFIG_IP_VS_PROTO_UDP | |
368 | REGISTER_PROTOCOL(&ip_vs_protocol_udp); | |
369 | #endif | |
2906f66a VMR |
370 | #ifdef CONFIG_IP_VS_PROTO_SCTP |
371 | REGISTER_PROTOCOL(&ip_vs_protocol_sctp); | |
372 | #endif | |
1da177e4 LT |
373 | #ifdef CONFIG_IP_VS_PROTO_AH |
374 | REGISTER_PROTOCOL(&ip_vs_protocol_ah); | |
375 | #endif | |
376 | #ifdef CONFIG_IP_VS_PROTO_ESP | |
377 | REGISTER_PROTOCOL(&ip_vs_protocol_esp); | |
378 | #endif | |
1e3e238e | 379 | pr_info("Registered protocols (%s)\n", &protocols[2]); |
1da177e4 LT |
380 | |
381 | return 0; | |
382 | } | |
383 | ||
384 | ||
385 | void ip_vs_protocol_cleanup(void) | |
386 | { | |
387 | struct ip_vs_protocol *pp; | |
388 | int i; | |
389 | ||
390 | /* unregister all the ipvs protocols */ | |
391 | for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) { | |
392 | while ((pp = ip_vs_proto_table[i]) != NULL) | |
393 | unregister_ip_vs_protocol(pp); | |
394 | } | |
395 | } |