blob: 503a842c90d271bac3a1c8386db92611f2888502 [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 *
Hans Schillstrom78b16bd2011-01-03 14:44:48 +010012 * Changes: Hans Schillstrom <hans.schillstrom@ericsson.com>
13 * Network name space (netns) aware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 */
16
Hannes Eder9aada7a2009-07-30 14:29:44 -070017#define KMSG_COMPONENT "IPVS"
18#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020020#include <linux/in.h>
21#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
Herbert Xuaf1e1cf2007-10-14 00:39:33 -070023#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/netfilter_ipv4.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020025#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <net/ip_vs.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030028#include <net/ip.h>
Stephen Rothwell63f2c042008-09-12 23:23:50 -070029#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int
Hans Schillstrom93304192011-01-03 14:44:51 +010032udp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020033 int *verdict, struct ip_vs_conn **cpp,
34 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Hans Schillstromfc723252011-01-03 14:44:43 +010036 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct ip_vs_service *svc;
38 struct udphdr _udph, *uh;
39
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020040 /* IPv6 fragments, only first fragment will hit this */
41 uh = skb_header_pointer(skb, iph->len, sizeof(_udph), &_udph);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (uh == NULL) {
43 *verdict = NF_DROP;
44 return 0;
45 }
Hans Schillstromfc723252011-01-03 14:44:43 +010046 net = skb_net(skb);
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020047 svc = ip_vs_service_get(net, af, skb->mark, iph->protocol,
48 &iph->daddr, uh->dest);
Julius Volz3c2e0502008-09-02 15:55:38 +020049 if (svc) {
Julian Anastasov190ecd22010-10-17 16:24:37 +030050 int ignored;
51
Hans Schillstroma0840e22011-01-03 14:44:58 +010052 if (ip_vs_todrop(net_ipvs(net))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 /*
54 * It seems that we are very loaded.
55 * We have to drop this packet :(
56 */
57 ip_vs_service_put(svc);
58 *verdict = NF_DROP;
59 return 0;
60 }
61
62 /*
63 * Let the virtual server select a real server for the
64 * incoming connection, and create a connection entry.
65 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020066 *cpp = ip_vs_schedule(svc, skb, pd, &ignored, iph);
Hans Schillstroma5959d52010-11-19 14:25:10 +010067 if (!*cpp && ignored <= 0) {
68 if (!ignored)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020069 *verdict = ip_vs_leave(svc, skb, pd, iph);
Hans Schillstroma5959d52010-11-19 14:25:10 +010070 else {
71 ip_vs_service_put(svc);
72 *verdict = NF_DROP;
73 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 0;
75 }
76 ip_vs_service_put(svc);
77 }
Hans Schillstroma5959d52010-11-19 14:25:10 +010078 /* NF_ACCEPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return 1;
80}
81
82
83static inline void
Julius Volz0bbdd422008-09-02 15:55:42 +020084udp_fast_csum_update(int af, struct udphdr *uhdr,
85 const union nf_inet_addr *oldip,
86 const union nf_inet_addr *newip,
Al Viro014d7302006-09-28 14:29:52 -070087 __be16 oldport, __be16 newport)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Julius Volz0bbdd422008-09-02 15:55:42 +020089#ifdef CONFIG_IP_VS_IPV6
90 if (af == AF_INET6)
91 uhdr->check =
92 csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
93 ip_vs_check_diff2(oldport, newport,
94 ~csum_unfold(uhdr->check))));
95 else
96#endif
97 uhdr->check =
98 csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
99 ip_vs_check_diff2(oldport, newport,
100 ~csum_unfold(uhdr->check))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!uhdr->check)
Al Virof6ab0282006-11-16 02:36:50 -0800102 uhdr->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Simon Horman503e81f2008-09-08 12:04:21 +1000105static inline void
106udp_partial_csum_update(int af, struct udphdr *uhdr,
107 const union nf_inet_addr *oldip,
108 const union nf_inet_addr *newip,
109 __be16 oldlen, __be16 newlen)
110{
111#ifdef CONFIG_IP_VS_IPV6
112 if (af == AF_INET6)
113 uhdr->check =
Julian Anastasov5bc90682010-10-17 16:14:31 +0300114 ~csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
Simon Horman503e81f2008-09-08 12:04:21 +1000115 ip_vs_check_diff2(oldlen, newlen,
Julian Anastasov5bc90682010-10-17 16:14:31 +0300116 csum_unfold(uhdr->check))));
Simon Horman503e81f2008-09-08 12:04:21 +1000117 else
118#endif
119 uhdr->check =
Julian Anastasov5bc90682010-10-17 16:14:31 +0300120 ~csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
Simon Horman503e81f2008-09-08 12:04:21 +1000121 ip_vs_check_diff2(oldlen, newlen,
Julian Anastasov5bc90682010-10-17 16:14:31 +0300122 csum_unfold(uhdr->check))));
Simon Horman503e81f2008-09-08 12:04:21 +1000123}
124
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static int
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200127udp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
128 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct udphdr *udph;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200131 unsigned int udphoff = iph->len;
Simon Horman503e81f2008-09-08 12:04:21 +1000132 int oldlen;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300133 int payload_csum = 0;
Julius Volz0bbdd422008-09-02 15:55:42 +0200134
135#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200136 if (cp->af == AF_INET6 && iph->fragoffs)
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200137 return 1;
Julius Volz0bbdd422008-09-02 15:55:42 +0200138#endif
Simon Horman503e81f2008-09-08 12:04:21 +1000139 oldlen = skb->len - udphoff;
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)) {
Julian Anastasov8b27b102010-10-17 16:17:20 +0300146 int ret;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* Some checks before mangling */
Julius Volz0bbdd422008-09-02 15:55:42 +0200149 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return 0;
151
152 /*
153 * Call application helper if needed
154 */
Julian Anastasov8b27b102010-10-17 16:17:20 +0300155 if (!(ret = ip_vs_app_pkt_out(cp, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return 0;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300157 /* ret=2: csum update is needed after payload mangling */
158 if (ret == 1)
159 oldlen = skb->len - udphoff;
160 else
161 payload_csum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163
Julius Volz0bbdd422008-09-02 15:55:42 +0200164 udph = (void *)skb_network_header(skb) + udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 udph->source = cp->vport;
166
167 /*
168 * Adjust UDP checksums
169 */
Simon Horman503e81f2008-09-08 12:04:21 +1000170 if (skb->ip_summed == CHECKSUM_PARTIAL) {
171 udp_partial_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
Harvey Harrisonca620592008-11-06 23:09:56 -0800172 htons(oldlen),
173 htons(skb->len - udphoff));
Julian Anastasov8b27b102010-10-17 16:17:20 +0300174 } else if (!payload_csum && (udph->check != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Only port and addr are changed, do fast csum update */
Julius Volz0bbdd422008-09-02 15:55:42 +0200176 udp_fast_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 cp->dport, cp->vport);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700178 if (skb->ip_summed == CHECKSUM_COMPLETE)
Julian Anastasov8b27b102010-10-17 16:17:20 +0300179 skb->ip_summed = (cp->app && pp->csum_check) ?
180 CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 } else {
182 /* full checksum calculation */
183 udph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700184 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
Julius Volz0bbdd422008-09-02 15:55:42 +0200185#ifdef CONFIG_IP_VS_IPV6
186 if (cp->af == AF_INET6)
187 udph->check = csum_ipv6_magic(&cp->vaddr.in6,
188 &cp->caddr.in6,
189 skb->len - udphoff,
190 cp->protocol, skb->csum);
191 else
192#endif
193 udph->check = csum_tcpudp_magic(cp->vaddr.ip,
194 cp->caddr.ip,
195 skb->len - udphoff,
196 cp->protocol,
197 skb->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (udph->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800199 udph->check = CSUM_MANGLED_0;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300200 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
202 pp->name, udph->check,
203 (char*)&(udph->check) - (char*)udph);
204 }
205 return 1;
206}
207
208
209static int
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200210udp_dnat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
211 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct udphdr *udph;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200214 unsigned int udphoff = iph->len;
Simon Horman503e81f2008-09-08 12:04:21 +1000215 int oldlen;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300216 int payload_csum = 0;
Julius Volz0bbdd422008-09-02 15:55:42 +0200217
218#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200219 if (cp->af == AF_INET6 && iph->fragoffs)
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200220 return 1;
Julius Volz0bbdd422008-09-02 15:55:42 +0200221#endif
Simon Horman503e81f2008-09-08 12:04:21 +1000222 oldlen = skb->len - udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* csum_check requires unshared skb */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700225 if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return 0;
227
228 if (unlikely(cp->app != NULL)) {
Julian Anastasov8b27b102010-10-17 16:17:20 +0300229 int ret;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Some checks before mangling */
Julius Volz0bbdd422008-09-02 15:55:42 +0200232 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return 0;
234
235 /*
236 * Attempt ip_vs_app call.
237 * It will fix ip_vs_conn
238 */
Julian Anastasov8b27b102010-10-17 16:17:20 +0300239 if (!(ret = ip_vs_app_pkt_in(cp, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return 0;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300241 /* ret=2: csum update is needed after payload mangling */
242 if (ret == 1)
243 oldlen = skb->len - udphoff;
244 else
245 payload_csum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
Julius Volz0bbdd422008-09-02 15:55:42 +0200248 udph = (void *)skb_network_header(skb) + udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 udph->dest = cp->dport;
250
251 /*
252 * Adjust UDP checksums
253 */
Simon Horman503e81f2008-09-08 12:04:21 +1000254 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Julian Anastasov5bc90682010-10-17 16:14:31 +0300255 udp_partial_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr,
Harvey Harrisonca620592008-11-06 23:09:56 -0800256 htons(oldlen),
257 htons(skb->len - udphoff));
Julian Anastasov8b27b102010-10-17 16:17:20 +0300258 } else if (!payload_csum && (udph->check != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* Only port and addr are changed, do fast csum update */
Julius Volz0bbdd422008-09-02 15:55:42 +0200260 udp_fast_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 cp->vport, cp->dport);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700262 if (skb->ip_summed == CHECKSUM_COMPLETE)
Julian Anastasov8b27b102010-10-17 16:17:20 +0300263 skb->ip_summed = (cp->app && pp->csum_check) ?
264 CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 } else {
266 /* full checksum calculation */
267 udph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700268 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
Julius Volz0bbdd422008-09-02 15:55:42 +0200269#ifdef CONFIG_IP_VS_IPV6
270 if (cp->af == AF_INET6)
271 udph->check = csum_ipv6_magic(&cp->caddr.in6,
272 &cp->daddr.in6,
273 skb->len - udphoff,
274 cp->protocol, skb->csum);
275 else
276#endif
277 udph->check = csum_tcpudp_magic(cp->caddr.ip,
278 cp->daddr.ip,
279 skb->len - udphoff,
280 cp->protocol,
281 skb->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (udph->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800283 udph->check = CSUM_MANGLED_0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700284 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286 return 1;
287}
288
289
290static int
Julius Volz51ef3482008-09-02 15:55:40 +0200291udp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct udphdr _udph, *uh;
Julius Volz51ef3482008-09-02 15:55:40 +0200294 unsigned int udphoff;
295
296#ifdef CONFIG_IP_VS_IPV6
297 if (af == AF_INET6)
298 udphoff = sizeof(struct ipv6hdr);
299 else
300#endif
301 udphoff = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph);
304 if (uh == NULL)
305 return 0;
306
307 if (uh->check != 0) {
308 switch (skb->ip_summed) {
309 case CHECKSUM_NONE:
310 skb->csum = skb_checksum(skb, udphoff,
311 skb->len - udphoff, 0);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700312 case CHECKSUM_COMPLETE:
Julius Volz51ef3482008-09-02 15:55:40 +0200313#ifdef CONFIG_IP_VS_IPV6
314 if (af == AF_INET6) {
315 if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
316 &ipv6_hdr(skb)->daddr,
317 skb->len - udphoff,
318 ipv6_hdr(skb)->nexthdr,
319 skb->csum)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300320 IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
Julius Volz51ef3482008-09-02 15:55:40 +0200321 "Failed checksum for");
322 return 0;
323 }
324 } else
325#endif
326 if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
327 ip_hdr(skb)->daddr,
328 skb->len - udphoff,
329 ip_hdr(skb)->protocol,
330 skb->csum)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300331 IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
Julius Volz51ef3482008-09-02 15:55:40 +0200332 "Failed checksum for");
333 return 0;
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 break;
336 default:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700337 /* No need to checksum. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 break;
339 }
340 }
341 return 1;
342}
343
Al Viro75e7ce62006-11-14 21:13:28 -0800344static inline __u16 udp_app_hashkey(__be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Al Viro75e7ce62006-11-14 21:13:28 -0800346 return (((__force u16)port >> UDP_APP_TAB_BITS) ^ (__force u16)port)
347 & UDP_APP_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100351static int udp_register_app(struct net *net, struct ip_vs_app *inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct ip_vs_app *i;
Al Viro75e7ce62006-11-14 21:13:28 -0800354 __u16 hash;
355 __be16 port = inc->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int ret = 0;
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100357 struct netns_ipvs *ipvs = net_ipvs(net);
358 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 hash = udp_app_hashkey(port);
361
362
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100363 spin_lock_bh(&ipvs->udp_app_lock);
364 list_for_each_entry(i, &ipvs->udp_apps[hash], p_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (i->port == port) {
366 ret = -EEXIST;
367 goto out;
368 }
369 }
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100370 list_add(&inc->p_list, &ipvs->udp_apps[hash]);
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100371 atomic_inc(&pd->appcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 out:
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100374 spin_unlock_bh(&ipvs->udp_app_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return ret;
376}
377
378
379static void
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100380udp_unregister_app(struct net *net, struct ip_vs_app *inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100382 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
383 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100384
385 spin_lock_bh(&ipvs->udp_app_lock);
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100386 atomic_dec(&pd->appcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 list_del(&inc->p_list);
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100388 spin_unlock_bh(&ipvs->udp_app_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391
392static int udp_app_conn_bind(struct ip_vs_conn *cp)
393{
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100394 struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(cp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int hash;
396 struct ip_vs_app *inc;
397 int result = 0;
398
399 /* Default binding: bind app only for NAT */
400 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
401 return 0;
402
403 /* Lookup application incarnations and bind the right one */
404 hash = udp_app_hashkey(cp->vport);
405
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100406 spin_lock(&ipvs->udp_app_lock);
407 list_for_each_entry(inc, &ipvs->udp_apps[hash], p_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (inc->port == cp->vport) {
409 if (unlikely(!ip_vs_app_inc_get(inc)))
410 break;
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100411 spin_unlock(&ipvs->udp_app_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Hannes Eder1e3e2382009-08-02 11:05:41 +0000413 IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->"
Julius Volzcfc78c52008-09-02 15:55:53 +0200414 "%s:%u to app %s on port %u\n",
415 __func__,
416 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
417 ntohs(cp->cport),
418 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
419 ntohs(cp->vport),
420 inc->name, ntohs(inc->port));
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 cp->app = inc;
423 if (inc->init_conn)
424 result = inc->init_conn(inc, cp);
425 goto out;
426 }
427 }
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100428 spin_unlock(&ipvs->udp_app_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 out:
431 return result;
432}
433
434
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100435static const int udp_timeouts[IP_VS_UDP_S_LAST+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 [IP_VS_UDP_S_NORMAL] = 5*60*HZ,
437 [IP_VS_UDP_S_LAST] = 2*HZ,
438};
439
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700440static const char *const udp_state_name_table[IP_VS_UDP_S_LAST+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 [IP_VS_UDP_S_NORMAL] = "UDP",
442 [IP_VS_UDP_S_LAST] = "BUG!",
443};
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static const char * udp_state_name(int state)
446{
447 if (state >= IP_VS_UDP_S_LAST)
448 return "ERR!";
449 return udp_state_name_table[state] ? udp_state_name_table[state] : "?";
450}
451
Simon Horman4a516f12011-09-16 14:11:49 +0900452static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453udp_state_transition(struct ip_vs_conn *cp, int direction,
454 const struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100455 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100457 if (unlikely(!pd)) {
458 pr_err("UDP no ns data\n");
Simon Horman4a516f12011-09-16 14:11:49 +0900459 return;
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100460 }
461
462 cp->timeout = pd->timeout_table[IP_VS_UDP_S_NORMAL];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Hans Schillstrom582b8e32012-04-26 09:45:35 +0200465static int __udp_init(struct net *net, struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100467 struct netns_ipvs *ipvs = net_ipvs(net);
468
469 ip_vs_init_hash_table(ipvs->udp_apps, UDP_APP_TAB_SIZE);
470 spin_lock_init(&ipvs->udp_app_lock);
471 pd->timeout_table = ip_vs_create_timeout_table((int *)udp_timeouts,
472 sizeof(udp_timeouts));
Hans Schillstrom582b8e32012-04-26 09:45:35 +0200473 if (!pd->timeout_table)
474 return -ENOMEM;
475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100478static void __udp_exit(struct net *net, struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100480 kfree(pd->timeout_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483
484struct ip_vs_protocol ip_vs_protocol_udp = {
485 .name = "UDP",
486 .protocol = IPPROTO_UDP,
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700487 .num_states = IP_VS_UDP_S_LAST,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 .dont_defrag = 0,
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100489 .init = NULL,
490 .exit = NULL,
491 .init_netns = __udp_init,
492 .exit_netns = __udp_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 .conn_schedule = udp_conn_schedule,
Simon Horman5c0d2372010-08-02 17:12:44 +0200494 .conn_in_get = ip_vs_conn_in_get_proto,
495 .conn_out_get = ip_vs_conn_out_get_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 .snat_handler = udp_snat_handler,
497 .dnat_handler = udp_dnat_handler,
498 .csum_check = udp_csum_check,
499 .state_transition = udp_state_transition,
500 .state_name = udp_state_name,
501 .register_app = udp_register_app,
502 .unregister_app = udp_unregister_app,
503 .app_conn_bind = udp_app_conn_bind,
504 .debug_packet = ip_vs_tcpudp_debug_packet,
505 .timeout_change = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506};