blob: d3a1b1f2d10d89122c2330e2d42d37cdf5e04792 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ip_vs_proto_udp.c: UDP 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
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020016#include <linux/in.h>
17#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
Herbert Xuaf1e1cf2007-10-14 00:39:33 -070019#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/netfilter_ipv4.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020021#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <net/ip_vs.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030024#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static struct ip_vs_conn *
Julius Volz51ef3482008-09-02 15:55:40 +020027udp_conn_in_get(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp,
28 const struct ip_vs_iphdr *iph, unsigned int proto_off,
29 int inverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 struct ip_vs_conn *cp;
Al Viro014d7302006-09-28 14:29:52 -070032 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
35 if (pptr == NULL)
36 return NULL;
37
38 if (likely(!inverse)) {
39 cp = ip_vs_conn_in_get(iph->protocol,
Julius Volz51ef3482008-09-02 15:55:40 +020040 iph->saddr.ip, pptr[0],
41 iph->daddr.ip, pptr[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 } else {
43 cp = ip_vs_conn_in_get(iph->protocol,
Julius Volz51ef3482008-09-02 15:55:40 +020044 iph->daddr.ip, pptr[1],
45 iph->saddr.ip, pptr[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
47
48 return cp;
49}
50
51
52static struct ip_vs_conn *
Julius Volz51ef3482008-09-02 15:55:40 +020053udp_conn_out_get(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp,
54 const struct ip_vs_iphdr *iph, unsigned int proto_off,
55 int inverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct ip_vs_conn *cp;
Al Viro014d7302006-09-28 14:29:52 -070058 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Julius Volz51ef3482008-09-02 15:55:40 +020060 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (pptr == NULL)
62 return NULL;
63
64 if (likely(!inverse)) {
65 cp = ip_vs_conn_out_get(iph->protocol,
Julius Volz51ef3482008-09-02 15:55:40 +020066 iph->saddr.ip, pptr[0],
67 iph->daddr.ip, pptr[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 } else {
69 cp = ip_vs_conn_out_get(iph->protocol,
Julius Volz51ef3482008-09-02 15:55:40 +020070 iph->daddr.ip, pptr[1],
71 iph->saddr.ip, pptr[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73
74 return cp;
75}
76
77
78static int
Julius Volz51ef3482008-09-02 15:55:40 +020079udp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int *verdict, struct ip_vs_conn **cpp)
81{
82 struct ip_vs_service *svc;
83 struct udphdr _udph, *uh;
Julius Volz3c2e0502008-09-02 15:55:38 +020084 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Julius Volz51ef3482008-09-02 15:55:40 +020086 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
Julius Volz3c2e0502008-09-02 15:55:38 +020087
88 uh = skb_header_pointer(skb, iph.len, sizeof(_udph), &_udph);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (uh == NULL) {
90 *verdict = NF_DROP;
91 return 0;
92 }
93
Julius Volz51ef3482008-09-02 15:55:40 +020094 svc = ip_vs_service_get(af, skb->mark, iph.protocol,
Julius Volz3c2e0502008-09-02 15:55:38 +020095 &iph.daddr, uh->dest);
96 if (svc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (ip_vs_todrop()) {
98 /*
99 * It seems that we are very loaded.
100 * We have to drop this packet :(
101 */
102 ip_vs_service_put(svc);
103 *verdict = NF_DROP;
104 return 0;
105 }
106
107 /*
108 * Let the virtual server select a real server for the
109 * incoming connection, and create a connection entry.
110 */
111 *cpp = ip_vs_schedule(svc, skb);
112 if (!*cpp) {
113 *verdict = ip_vs_leave(svc, skb, pp);
114 return 0;
115 }
116 ip_vs_service_put(svc);
117 }
118 return 1;
119}
120
121
122static inline void
Al Viro014d7302006-09-28 14:29:52 -0700123udp_fast_csum_update(struct udphdr *uhdr, __be32 oldip, __be32 newip,
124 __be16 oldport, __be16 newport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 uhdr->check =
Al Virof9214b22006-11-16 02:41:18 -0800127 csum_fold(ip_vs_check_diff4(oldip, newip,
128 ip_vs_check_diff2(oldport, newport,
129 ~csum_unfold(uhdr->check))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (!uhdr->check)
Al Virof6ab0282006-11-16 02:36:50 -0800131 uhdr->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700135udp_snat_handler(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
137{
138 struct udphdr *udph;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700139 const unsigned int udphoff = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* csum_check requires unshared skb */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700142 if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return 0;
144
145 if (unlikely(cp->app != NULL)) {
146 /* Some checks before mangling */
Julius Volz51ef3482008-09-02 15:55:40 +0200147 if (pp->csum_check && !pp->csum_check(AF_INET, skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
149
150 /*
151 * Call application helper if needed
152 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700153 if (!ip_vs_app_pkt_out(cp, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return 0;
155 }
156
Herbert Xu3db05fe2007-10-15 00:53:15 -0700157 udph = (void *)ip_hdr(skb) + udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 udph->source = cp->vport;
159
160 /*
161 * Adjust UDP checksums
162 */
163 if (!cp->app && (udph->check != 0)) {
164 /* Only port and addr are changed, do fast csum update */
Julius Volze7ade462008-09-02 15:55:33 +0200165 udp_fast_csum_update(udph, cp->daddr.ip, cp->vaddr.ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 cp->dport, cp->vport);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700167 if (skb->ip_summed == CHECKSUM_COMPLETE)
168 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 } else {
170 /* full checksum calculation */
171 udph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700172 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
Julius Volze7ade462008-09-02 15:55:33 +0200173 udph->check = csum_tcpudp_magic(cp->vaddr.ip, cp->caddr.ip,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700174 skb->len - udphoff,
175 cp->protocol, skb->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (udph->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800177 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
179 pp->name, udph->check,
180 (char*)&(udph->check) - (char*)udph);
181 }
182 return 1;
183}
184
185
186static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700187udp_dnat_handler(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
189{
190 struct udphdr *udph;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700191 unsigned int udphoff = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /* csum_check requires unshared skb */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700194 if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return 0;
196
197 if (unlikely(cp->app != NULL)) {
198 /* Some checks before mangling */
Julius Volz51ef3482008-09-02 15:55:40 +0200199 if (pp->csum_check && !pp->csum_check(AF_INET, skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 0;
201
202 /*
203 * Attempt ip_vs_app call.
204 * It will fix ip_vs_conn
205 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700206 if (!ip_vs_app_pkt_in(cp, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 0;
208 }
209
Herbert Xu3db05fe2007-10-15 00:53:15 -0700210 udph = (void *)ip_hdr(skb) + udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 udph->dest = cp->dport;
212
213 /*
214 * Adjust UDP checksums
215 */
216 if (!cp->app && (udph->check != 0)) {
217 /* Only port and addr are changed, do fast csum update */
Julius Volze7ade462008-09-02 15:55:33 +0200218 udp_fast_csum_update(udph, cp->vaddr.ip, cp->daddr.ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 cp->vport, cp->dport);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700220 if (skb->ip_summed == CHECKSUM_COMPLETE)
221 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 } else {
223 /* full checksum calculation */
224 udph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700225 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
Julius Volze7ade462008-09-02 15:55:33 +0200226 udph->check = csum_tcpudp_magic(cp->caddr.ip, cp->daddr.ip,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700227 skb->len - udphoff,
228 cp->protocol, skb->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (udph->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800230 udph->check = CSUM_MANGLED_0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700231 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 return 1;
234}
235
236
237static int
Julius Volz51ef3482008-09-02 15:55:40 +0200238udp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct udphdr _udph, *uh;
Julius Volz51ef3482008-09-02 15:55:40 +0200241 unsigned int udphoff;
242
243#ifdef CONFIG_IP_VS_IPV6
244 if (af == AF_INET6)
245 udphoff = sizeof(struct ipv6hdr);
246 else
247#endif
248 udphoff = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph);
251 if (uh == NULL)
252 return 0;
253
254 if (uh->check != 0) {
255 switch (skb->ip_summed) {
256 case CHECKSUM_NONE:
257 skb->csum = skb_checksum(skb, udphoff,
258 skb->len - udphoff, 0);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700259 case CHECKSUM_COMPLETE:
Julius Volz51ef3482008-09-02 15:55:40 +0200260#ifdef CONFIG_IP_VS_IPV6
261 if (af == AF_INET6) {
262 if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
263 &ipv6_hdr(skb)->daddr,
264 skb->len - udphoff,
265 ipv6_hdr(skb)->nexthdr,
266 skb->csum)) {
267 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
268 "Failed checksum for");
269 return 0;
270 }
271 } else
272#endif
273 if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
274 ip_hdr(skb)->daddr,
275 skb->len - udphoff,
276 ip_hdr(skb)->protocol,
277 skb->csum)) {
278 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
279 "Failed checksum for");
280 return 0;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 default:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700284 /* No need to checksum. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286 }
287 }
288 return 1;
289}
290
291
292/*
293 * Note: the caller guarantees that only one of register_app,
294 * unregister_app or app_conn_bind is called each time.
295 */
296
297#define UDP_APP_TAB_BITS 4
298#define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
299#define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
300
301static struct list_head udp_apps[UDP_APP_TAB_SIZE];
302static DEFINE_SPINLOCK(udp_app_lock);
303
Al Viro75e7ce62006-11-14 21:13:28 -0800304static inline __u16 udp_app_hashkey(__be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Al Viro75e7ce62006-11-14 21:13:28 -0800306 return (((__force u16)port >> UDP_APP_TAB_BITS) ^ (__force u16)port)
307 & UDP_APP_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310
311static int udp_register_app(struct ip_vs_app *inc)
312{
313 struct ip_vs_app *i;
Al Viro75e7ce62006-11-14 21:13:28 -0800314 __u16 hash;
315 __be16 port = inc->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int ret = 0;
317
318 hash = udp_app_hashkey(port);
319
320
321 spin_lock_bh(&udp_app_lock);
322 list_for_each_entry(i, &udp_apps[hash], p_list) {
323 if (i->port == port) {
324 ret = -EEXIST;
325 goto out;
326 }
327 }
328 list_add(&inc->p_list, &udp_apps[hash]);
329 atomic_inc(&ip_vs_protocol_udp.appcnt);
330
331 out:
332 spin_unlock_bh(&udp_app_lock);
333 return ret;
334}
335
336
337static void
338udp_unregister_app(struct ip_vs_app *inc)
339{
340 spin_lock_bh(&udp_app_lock);
341 atomic_dec(&ip_vs_protocol_udp.appcnt);
342 list_del(&inc->p_list);
343 spin_unlock_bh(&udp_app_lock);
344}
345
346
347static int udp_app_conn_bind(struct ip_vs_conn *cp)
348{
349 int hash;
350 struct ip_vs_app *inc;
351 int result = 0;
352
353 /* Default binding: bind app only for NAT */
354 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
355 return 0;
356
357 /* Lookup application incarnations and bind the right one */
358 hash = udp_app_hashkey(cp->vport);
359
360 spin_lock(&udp_app_lock);
361 list_for_each_entry(inc, &udp_apps[hash], p_list) {
362 if (inc->port == cp->vport) {
363 if (unlikely(!ip_vs_app_inc_get(inc)))
364 break;
365 spin_unlock(&udp_app_lock);
366
367 IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->"
368 "%u.%u.%u.%u:%u to app %s on port %u\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800369 __func__,
Julius Volze7ade462008-09-02 15:55:33 +0200370 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
371 NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 inc->name, ntohs(inc->port));
373 cp->app = inc;
374 if (inc->init_conn)
375 result = inc->init_conn(inc, cp);
376 goto out;
377 }
378 }
379 spin_unlock(&udp_app_lock);
380
381 out:
382 return result;
383}
384
385
386static int udp_timeouts[IP_VS_UDP_S_LAST+1] = {
387 [IP_VS_UDP_S_NORMAL] = 5*60*HZ,
388 [IP_VS_UDP_S_LAST] = 2*HZ,
389};
390
391static char * udp_state_name_table[IP_VS_UDP_S_LAST+1] = {
392 [IP_VS_UDP_S_NORMAL] = "UDP",
393 [IP_VS_UDP_S_LAST] = "BUG!",
394};
395
396
397static int
398udp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
399{
400 return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_UDP_S_LAST,
401 udp_state_name_table, sname, to);
402}
403
404static const char * udp_state_name(int state)
405{
406 if (state >= IP_VS_UDP_S_LAST)
407 return "ERR!";
408 return udp_state_name_table[state] ? udp_state_name_table[state] : "?";
409}
410
411static int
412udp_state_transition(struct ip_vs_conn *cp, int direction,
413 const struct sk_buff *skb,
414 struct ip_vs_protocol *pp)
415{
416 cp->timeout = pp->timeout_table[IP_VS_UDP_S_NORMAL];
417 return 1;
418}
419
420static void udp_init(struct ip_vs_protocol *pp)
421{
422 IP_VS_INIT_HASH_TABLE(udp_apps);
423 pp->timeout_table = udp_timeouts;
424}
425
426static void udp_exit(struct ip_vs_protocol *pp)
427{
428}
429
430
431struct ip_vs_protocol ip_vs_protocol_udp = {
432 .name = "UDP",
433 .protocol = IPPROTO_UDP,
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700434 .num_states = IP_VS_UDP_S_LAST,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 .dont_defrag = 0,
436 .init = udp_init,
437 .exit = udp_exit,
438 .conn_schedule = udp_conn_schedule,
439 .conn_in_get = udp_conn_in_get,
440 .conn_out_get = udp_conn_out_get,
441 .snat_handler = udp_snat_handler,
442 .dnat_handler = udp_dnat_handler,
443 .csum_check = udp_csum_check,
444 .state_transition = udp_state_transition,
445 .state_name = udp_state_name,
446 .register_app = udp_register_app,
447 .unregister_app = udp_unregister_app,
448 .app_conn_bind = udp_app_conn_bind,
449 .debug_packet = ip_vs_tcpudp_debug_packet,
450 .timeout_change = NULL,
451 .set_state_timeout = udp_set_state_timeout,
452};