2 * ip_vs_proto_udp.c: UDP load balancing support for IPVS
4 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
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.
18 #include <linux/kernel.h>
19 #include <linux/netfilter.h>
20 #include <linux/netfilter_ipv4.h>
21 #include <linux/udp.h>
23 #include <net/ip_vs.h>
25 #include <net/ip6_checksum.h>
27 static struct ip_vs_conn *
28 udp_conn_in_get(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp,
29 const struct ip_vs_iphdr *iph, unsigned int proto_off,
32 struct ip_vs_conn *cp;
33 __be16 _ports[2], *pptr;
35 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
39 if (likely(!inverse)) {
40 cp = ip_vs_conn_in_get(af, iph->protocol,
42 &iph->daddr, pptr[1]);
44 cp = ip_vs_conn_in_get(af, iph->protocol,
46 &iph->saddr, pptr[0]);
53 static struct ip_vs_conn *
54 udp_conn_out_get(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp,
55 const struct ip_vs_iphdr *iph, unsigned int proto_off,
58 struct ip_vs_conn *cp;
59 __be16 _ports[2], *pptr;
61 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
65 if (likely(!inverse)) {
66 cp = ip_vs_conn_out_get(af, iph->protocol,
68 &iph->daddr, pptr[1]);
70 cp = ip_vs_conn_out_get(af, iph->protocol,
72 &iph->saddr, pptr[0]);
80 udp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
81 int *verdict, struct ip_vs_conn **cpp)
83 struct ip_vs_service *svc;
84 struct udphdr _udph, *uh;
85 struct ip_vs_iphdr iph;
87 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
89 uh = skb_header_pointer(skb, iph.len, sizeof(_udph), &_udph);
95 svc = ip_vs_service_get(af, skb->mark, iph.protocol,
96 &iph.daddr, uh->dest);
100 * It seems that we are very loaded.
101 * We have to drop this packet :(
103 ip_vs_service_put(svc);
109 * Let the virtual server select a real server for the
110 * incoming connection, and create a connection entry.
112 *cpp = ip_vs_schedule(svc, skb);
114 *verdict = ip_vs_leave(svc, skb, pp);
117 ip_vs_service_put(svc);
124 udp_fast_csum_update(int af, struct udphdr *uhdr,
125 const union nf_inet_addr *oldip,
126 const union nf_inet_addr *newip,
127 __be16 oldport, __be16 newport)
129 #ifdef CONFIG_IP_VS_IPV6
132 csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
133 ip_vs_check_diff2(oldport, newport,
134 ~csum_unfold(uhdr->check))));
138 csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
139 ip_vs_check_diff2(oldport, newport,
140 ~csum_unfold(uhdr->check))));
142 uhdr->check = CSUM_MANGLED_0;
146 udp_partial_csum_update(int af, struct udphdr *uhdr,
147 const union nf_inet_addr *oldip,
148 const union nf_inet_addr *newip,
149 __be16 oldlen, __be16 newlen)
151 #ifdef CONFIG_IP_VS_IPV6
154 csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
155 ip_vs_check_diff2(oldlen, newlen,
156 ~csum_unfold(uhdr->check))));
160 csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
161 ip_vs_check_diff2(oldlen, newlen,
162 ~csum_unfold(uhdr->check))));
167 udp_snat_handler(struct sk_buff *skb,
168 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
171 unsigned int udphoff;
174 #ifdef CONFIG_IP_VS_IPV6
175 if (cp->af == AF_INET6)
176 udphoff = sizeof(struct ipv6hdr);
179 udphoff = ip_hdrlen(skb);
180 oldlen = skb->len - udphoff;
182 /* csum_check requires unshared skb */
183 if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
186 if (unlikely(cp->app != NULL)) {
187 /* Some checks before mangling */
188 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
192 * Call application helper if needed
194 if (!ip_vs_app_pkt_out(cp, skb))
198 udph = (void *)skb_network_header(skb) + udphoff;
199 udph->source = cp->vport;
202 * Adjust UDP checksums
204 if (skb->ip_summed == CHECKSUM_PARTIAL) {
205 udp_partial_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
207 htons(skb->len - udphoff));
208 } else if (!cp->app && (udph->check != 0)) {
209 /* Only port and addr are changed, do fast csum update */
210 udp_fast_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
211 cp->dport, cp->vport);
212 if (skb->ip_summed == CHECKSUM_COMPLETE)
213 skb->ip_summed = CHECKSUM_NONE;
215 /* full checksum calculation */
217 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
218 #ifdef CONFIG_IP_VS_IPV6
219 if (cp->af == AF_INET6)
220 udph->check = csum_ipv6_magic(&cp->vaddr.in6,
223 cp->protocol, skb->csum);
226 udph->check = csum_tcpudp_magic(cp->vaddr.ip,
231 if (udph->check == 0)
232 udph->check = CSUM_MANGLED_0;
233 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
234 pp->name, udph->check,
235 (char*)&(udph->check) - (char*)udph);
242 udp_dnat_handler(struct sk_buff *skb,
243 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
246 unsigned int udphoff;
249 #ifdef CONFIG_IP_VS_IPV6
250 if (cp->af == AF_INET6)
251 udphoff = sizeof(struct ipv6hdr);
254 udphoff = ip_hdrlen(skb);
255 oldlen = skb->len - udphoff;
257 /* csum_check requires unshared skb */
258 if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
261 if (unlikely(cp->app != NULL)) {
262 /* Some checks before mangling */
263 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
267 * Attempt ip_vs_app call.
268 * It will fix ip_vs_conn
270 if (!ip_vs_app_pkt_in(cp, skb))
274 udph = (void *)skb_network_header(skb) + udphoff;
275 udph->dest = cp->dport;
278 * Adjust UDP checksums
280 if (skb->ip_summed == CHECKSUM_PARTIAL) {
281 udp_partial_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
283 htons(skb->len - udphoff));
284 } else if (!cp->app && (udph->check != 0)) {
285 /* Only port and addr are changed, do fast csum update */
286 udp_fast_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr,
287 cp->vport, cp->dport);
288 if (skb->ip_summed == CHECKSUM_COMPLETE)
289 skb->ip_summed = CHECKSUM_NONE;
291 /* full checksum calculation */
293 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
294 #ifdef CONFIG_IP_VS_IPV6
295 if (cp->af == AF_INET6)
296 udph->check = csum_ipv6_magic(&cp->caddr.in6,
299 cp->protocol, skb->csum);
302 udph->check = csum_tcpudp_magic(cp->caddr.ip,
307 if (udph->check == 0)
308 udph->check = CSUM_MANGLED_0;
309 skb->ip_summed = CHECKSUM_UNNECESSARY;
316 udp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
318 struct udphdr _udph, *uh;
319 unsigned int udphoff;
321 #ifdef CONFIG_IP_VS_IPV6
323 udphoff = sizeof(struct ipv6hdr);
326 udphoff = ip_hdrlen(skb);
328 uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph);
332 if (uh->check != 0) {
333 switch (skb->ip_summed) {
335 skb->csum = skb_checksum(skb, udphoff,
336 skb->len - udphoff, 0);
337 case CHECKSUM_COMPLETE:
338 #ifdef CONFIG_IP_VS_IPV6
339 if (af == AF_INET6) {
340 if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
341 &ipv6_hdr(skb)->daddr,
343 ipv6_hdr(skb)->nexthdr,
345 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
346 "Failed checksum for");
351 if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
354 ip_hdr(skb)->protocol,
356 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
357 "Failed checksum for");
362 /* No need to checksum. */
371 * Note: the caller guarantees that only one of register_app,
372 * unregister_app or app_conn_bind is called each time.
375 #define UDP_APP_TAB_BITS 4
376 #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
377 #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
379 static struct list_head udp_apps[UDP_APP_TAB_SIZE];
380 static DEFINE_SPINLOCK(udp_app_lock);
382 static inline __u16 udp_app_hashkey(__be16 port)
384 return (((__force u16)port >> UDP_APP_TAB_BITS) ^ (__force u16)port)
389 static int udp_register_app(struct ip_vs_app *inc)
393 __be16 port = inc->port;
396 hash = udp_app_hashkey(port);
399 spin_lock_bh(&udp_app_lock);
400 list_for_each_entry(i, &udp_apps[hash], p_list) {
401 if (i->port == port) {
406 list_add(&inc->p_list, &udp_apps[hash]);
407 atomic_inc(&ip_vs_protocol_udp.appcnt);
410 spin_unlock_bh(&udp_app_lock);
416 udp_unregister_app(struct ip_vs_app *inc)
418 spin_lock_bh(&udp_app_lock);
419 atomic_dec(&ip_vs_protocol_udp.appcnt);
420 list_del(&inc->p_list);
421 spin_unlock_bh(&udp_app_lock);
425 static int udp_app_conn_bind(struct ip_vs_conn *cp)
428 struct ip_vs_app *inc;
431 /* Default binding: bind app only for NAT */
432 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
435 /* Lookup application incarnations and bind the right one */
436 hash = udp_app_hashkey(cp->vport);
438 spin_lock(&udp_app_lock);
439 list_for_each_entry(inc, &udp_apps[hash], p_list) {
440 if (inc->port == cp->vport) {
441 if (unlikely(!ip_vs_app_inc_get(inc)))
443 spin_unlock(&udp_app_lock);
445 IP_VS_DBG_BUF(9, "%s: Binding conn %s:%u->"
446 "%s:%u to app %s on port %u\n",
448 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
450 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
452 inc->name, ntohs(inc->port));
456 result = inc->init_conn(inc, cp);
460 spin_unlock(&udp_app_lock);
467 static int udp_timeouts[IP_VS_UDP_S_LAST+1] = {
468 [IP_VS_UDP_S_NORMAL] = 5*60*HZ,
469 [IP_VS_UDP_S_LAST] = 2*HZ,
472 static char * udp_state_name_table[IP_VS_UDP_S_LAST+1] = {
473 [IP_VS_UDP_S_NORMAL] = "UDP",
474 [IP_VS_UDP_S_LAST] = "BUG!",
479 udp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
481 return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_UDP_S_LAST,
482 udp_state_name_table, sname, to);
485 static const char * udp_state_name(int state)
487 if (state >= IP_VS_UDP_S_LAST)
489 return udp_state_name_table[state] ? udp_state_name_table[state] : "?";
493 udp_state_transition(struct ip_vs_conn *cp, int direction,
494 const struct sk_buff *skb,
495 struct ip_vs_protocol *pp)
497 cp->timeout = pp->timeout_table[IP_VS_UDP_S_NORMAL];
501 static void udp_init(struct ip_vs_protocol *pp)
503 IP_VS_INIT_HASH_TABLE(udp_apps);
504 pp->timeout_table = udp_timeouts;
507 static void udp_exit(struct ip_vs_protocol *pp)
512 struct ip_vs_protocol ip_vs_protocol_udp = {
514 .protocol = IPPROTO_UDP,
515 .num_states = IP_VS_UDP_S_LAST,
519 .conn_schedule = udp_conn_schedule,
520 .conn_in_get = udp_conn_in_get,
521 .conn_out_get = udp_conn_out_get,
522 .snat_handler = udp_snat_handler,
523 .dnat_handler = udp_dnat_handler,
524 .csum_check = udp_csum_check,
525 .state_transition = udp_state_transition,
526 .state_name = udp_state_name,
527 .register_app = udp_register_app,
528 .unregister_app = udp_unregister_app,
529 .app_conn_bind = udp_app_conn_bind,
530 .debug_packet = ip_vs_tcpudp_debug_packet,
531 .timeout_change = NULL,
532 .set_state_timeout = udp_set_state_timeout,