2 * ip_vs_proto_udp.c: UDP load balancing support for IPVS
4 * Version: $Id: ip_vs_proto_udp.c,v 1.3 2002/11/30 01:50:35 wensong Exp $
6 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
7 * Julian Anastasov <ja@ssi.bg>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
20 #include <linux/kernel.h>
21 #include <linux/netfilter.h>
22 #include <linux/netfilter_ipv4.h>
23 #include <linux/udp.h>
25 #include <net/ip_vs.h>
28 static struct ip_vs_conn *
29 udp_conn_in_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
30 const struct iphdr *iph, unsigned int proto_off, int inverse)
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(iph->protocol,
44 cp = ip_vs_conn_in_get(iph->protocol,
53 static struct ip_vs_conn *
54 udp_conn_out_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
55 const struct iphdr *iph, unsigned int proto_off, int inverse)
57 struct ip_vs_conn *cp;
58 __be16 _ports[2], *pptr;
60 pptr = skb_header_pointer(skb, ip_hdrlen(skb),
61 sizeof(_ports), _ports);
65 if (likely(!inverse)) {
66 cp = ip_vs_conn_out_get(iph->protocol,
70 cp = ip_vs_conn_out_get(iph->protocol,
80 udp_conn_schedule(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;
86 uh = skb_header_pointer(skb, ip_hdrlen(skb),
87 sizeof(_udph), &_udph);
93 if ((svc = ip_vs_service_get(skb->mark, ip_hdr(skb)->protocol,
94 ip_hdr(skb)->daddr, uh->dest))) {
97 * It seems that we are very loaded.
98 * We have to drop this packet :(
100 ip_vs_service_put(svc);
106 * Let the virtual server select a real server for the
107 * incoming connection, and create a connection entry.
109 *cpp = ip_vs_schedule(svc, skb);
111 *verdict = ip_vs_leave(svc, skb, pp);
114 ip_vs_service_put(svc);
121 udp_fast_csum_update(struct udphdr *uhdr, __be32 oldip, __be32 newip,
122 __be16 oldport, __be16 newport)
125 csum_fold(ip_vs_check_diff4(oldip, newip,
126 ip_vs_check_diff2(oldport, newport,
127 ~csum_unfold(uhdr->check))));
129 uhdr->check = CSUM_MANGLED_0;
133 udp_snat_handler(struct sk_buff **pskb,
134 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
137 const unsigned int udphoff = ip_hdrlen(*pskb);
139 /* csum_check requires unshared skb */
140 if (!skb_make_writable(*pskb, udphoff+sizeof(*udph)))
143 if (unlikely(cp->app != NULL)) {
144 /* Some checks before mangling */
145 if (pp->csum_check && !pp->csum_check(*pskb, pp))
149 * Call application helper if needed
151 if (!ip_vs_app_pkt_out(cp, pskb))
155 udph = (void *)ip_hdr(*pskb) + udphoff;
156 udph->source = cp->vport;
159 * Adjust UDP checksums
161 if (!cp->app && (udph->check != 0)) {
162 /* Only port and addr are changed, do fast csum update */
163 udp_fast_csum_update(udph, cp->daddr, cp->vaddr,
164 cp->dport, cp->vport);
165 if ((*pskb)->ip_summed == CHECKSUM_COMPLETE)
166 (*pskb)->ip_summed = CHECKSUM_NONE;
168 /* full checksum calculation */
170 (*pskb)->csum = skb_checksum(*pskb, udphoff,
171 (*pskb)->len - udphoff, 0);
172 udph->check = csum_tcpudp_magic(cp->vaddr, cp->caddr,
173 (*pskb)->len - udphoff,
176 if (udph->check == 0)
177 udph->check = CSUM_MANGLED_0;
178 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
179 pp->name, udph->check,
180 (char*)&(udph->check) - (char*)udph);
187 udp_dnat_handler(struct sk_buff **pskb,
188 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
191 unsigned int udphoff = ip_hdrlen(*pskb);
193 /* csum_check requires unshared skb */
194 if (!skb_make_writable(*pskb, udphoff+sizeof(*udph)))
197 if (unlikely(cp->app != NULL)) {
198 /* Some checks before mangling */
199 if (pp->csum_check && !pp->csum_check(*pskb, pp))
203 * Attempt ip_vs_app call.
204 * It will fix ip_vs_conn
206 if (!ip_vs_app_pkt_in(cp, pskb))
210 udph = (void *)ip_hdr(*pskb) + udphoff;
211 udph->dest = cp->dport;
214 * Adjust UDP checksums
216 if (!cp->app && (udph->check != 0)) {
217 /* Only port and addr are changed, do fast csum update */
218 udp_fast_csum_update(udph, cp->vaddr, cp->daddr,
219 cp->vport, cp->dport);
220 if ((*pskb)->ip_summed == CHECKSUM_COMPLETE)
221 (*pskb)->ip_summed = CHECKSUM_NONE;
223 /* full checksum calculation */
225 (*pskb)->csum = skb_checksum(*pskb, udphoff,
226 (*pskb)->len - udphoff, 0);
227 udph->check = csum_tcpudp_magic(cp->caddr, cp->daddr,
228 (*pskb)->len - udphoff,
231 if (udph->check == 0)
232 udph->check = CSUM_MANGLED_0;
233 (*pskb)->ip_summed = CHECKSUM_UNNECESSARY;
240 udp_csum_check(struct sk_buff *skb, struct ip_vs_protocol *pp)
242 struct udphdr _udph, *uh;
243 const unsigned int udphoff = ip_hdrlen(skb);
245 uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph);
249 if (uh->check != 0) {
250 switch (skb->ip_summed) {
252 skb->csum = skb_checksum(skb, udphoff,
253 skb->len - udphoff, 0);
254 case CHECKSUM_COMPLETE:
255 if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
258 ip_hdr(skb)->protocol,
260 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
261 "Failed checksum for");
266 /* No need to checksum. */
275 * Note: the caller guarantees that only one of register_app,
276 * unregister_app or app_conn_bind is called each time.
279 #define UDP_APP_TAB_BITS 4
280 #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
281 #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
283 static struct list_head udp_apps[UDP_APP_TAB_SIZE];
284 static DEFINE_SPINLOCK(udp_app_lock);
286 static inline __u16 udp_app_hashkey(__be16 port)
288 return (((__force u16)port >> UDP_APP_TAB_BITS) ^ (__force u16)port)
293 static int udp_register_app(struct ip_vs_app *inc)
297 __be16 port = inc->port;
300 hash = udp_app_hashkey(port);
303 spin_lock_bh(&udp_app_lock);
304 list_for_each_entry(i, &udp_apps[hash], p_list) {
305 if (i->port == port) {
310 list_add(&inc->p_list, &udp_apps[hash]);
311 atomic_inc(&ip_vs_protocol_udp.appcnt);
314 spin_unlock_bh(&udp_app_lock);
320 udp_unregister_app(struct ip_vs_app *inc)
322 spin_lock_bh(&udp_app_lock);
323 atomic_dec(&ip_vs_protocol_udp.appcnt);
324 list_del(&inc->p_list);
325 spin_unlock_bh(&udp_app_lock);
329 static int udp_app_conn_bind(struct ip_vs_conn *cp)
332 struct ip_vs_app *inc;
335 /* Default binding: bind app only for NAT */
336 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
339 /* Lookup application incarnations and bind the right one */
340 hash = udp_app_hashkey(cp->vport);
342 spin_lock(&udp_app_lock);
343 list_for_each_entry(inc, &udp_apps[hash], p_list) {
344 if (inc->port == cp->vport) {
345 if (unlikely(!ip_vs_app_inc_get(inc)))
347 spin_unlock(&udp_app_lock);
349 IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->"
350 "%u.%u.%u.%u:%u to app %s on port %u\n",
352 NIPQUAD(cp->caddr), ntohs(cp->cport),
353 NIPQUAD(cp->vaddr), ntohs(cp->vport),
354 inc->name, ntohs(inc->port));
357 result = inc->init_conn(inc, cp);
361 spin_unlock(&udp_app_lock);
368 static int udp_timeouts[IP_VS_UDP_S_LAST+1] = {
369 [IP_VS_UDP_S_NORMAL] = 5*60*HZ,
370 [IP_VS_UDP_S_LAST] = 2*HZ,
373 static char * udp_state_name_table[IP_VS_UDP_S_LAST+1] = {
374 [IP_VS_UDP_S_NORMAL] = "UDP",
375 [IP_VS_UDP_S_LAST] = "BUG!",
380 udp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
382 return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_UDP_S_LAST,
383 udp_state_name_table, sname, to);
386 static const char * udp_state_name(int state)
388 if (state >= IP_VS_UDP_S_LAST)
390 return udp_state_name_table[state] ? udp_state_name_table[state] : "?";
394 udp_state_transition(struct ip_vs_conn *cp, int direction,
395 const struct sk_buff *skb,
396 struct ip_vs_protocol *pp)
398 cp->timeout = pp->timeout_table[IP_VS_UDP_S_NORMAL];
402 static void udp_init(struct ip_vs_protocol *pp)
404 IP_VS_INIT_HASH_TABLE(udp_apps);
405 pp->timeout_table = udp_timeouts;
408 static void udp_exit(struct ip_vs_protocol *pp)
413 struct ip_vs_protocol ip_vs_protocol_udp = {
415 .protocol = IPPROTO_UDP,
419 .conn_schedule = udp_conn_schedule,
420 .conn_in_get = udp_conn_in_get,
421 .conn_out_get = udp_conn_out_get,
422 .snat_handler = udp_snat_handler,
423 .dnat_handler = udp_dnat_handler,
424 .csum_check = udp_csum_check,
425 .state_transition = udp_state_transition,
426 .state_name = udp_state_name,
427 .register_app = udp_register_app,
428 .unregister_app = udp_unregister_app,
429 .app_conn_bind = udp_app_conn_bind,
430 .debug_packet = ip_vs_tcpudp_debug_packet,
431 .timeout_change = NULL,
432 .set_state_timeout = udp_set_state_timeout,