blob: d6f4eeec8b713ed2793d6bbdc88baada9c4898e9 [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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int *verdict, struct ip_vs_conn **cpp)
34{
Hans Schillstromfc723252011-01-03 14:44:43 +010035 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct ip_vs_service *svc;
37 struct udphdr _udph, *uh;
Julius Volz3c2e0502008-09-02 15:55:38 +020038 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +020040 ip_vs_fill_iph_skb(af, skb, &iph);
Julius Volz3c2e0502008-09-02 15:55:38 +020041
42 uh = skb_header_pointer(skb, iph.len, sizeof(_udph), &_udph);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 if (uh == NULL) {
44 *verdict = NF_DROP;
45 return 0;
46 }
Hans Schillstromfc723252011-01-03 14:44:43 +010047 net = skb_net(skb);
48 svc = ip_vs_service_get(net, af, skb->mark, iph.protocol,
Julius Volz3c2e0502008-09-02 15:55:38 +020049 &iph.daddr, uh->dest);
50 if (svc) {
Julian Anastasov190ecd22010-10-17 16:24:37 +030051 int ignored;
52
Hans Schillstroma0840e22011-01-03 14:44:58 +010053 if (ip_vs_todrop(net_ipvs(net))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 /*
55 * It seems that we are very loaded.
56 * We have to drop this packet :(
57 */
58 ip_vs_service_put(svc);
59 *verdict = NF_DROP;
60 return 0;
61 }
62
63 /*
64 * Let the virtual server select a real server for the
65 * incoming connection, and create a connection entry.
66 */
Hans Schillstrom93304192011-01-03 14:44:51 +010067 *cpp = ip_vs_schedule(svc, skb, pd, &ignored);
Hans Schillstroma5959d52010-11-19 14:25:10 +010068 if (!*cpp && ignored <= 0) {
69 if (!ignored)
Hans Schillstrom93304192011-01-03 14:44:51 +010070 *verdict = ip_vs_leave(svc, skb, pd);
Hans Schillstroma5959d52010-11-19 14:25:10 +010071 else {
72 ip_vs_service_put(svc);
73 *verdict = NF_DROP;
74 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return 0;
76 }
77 ip_vs_service_put(svc);
78 }
Hans Schillstroma5959d52010-11-19 14:25:10 +010079 /* NF_ACCEPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return 1;
81}
82
83
84static inline void
Julius Volz0bbdd422008-09-02 15:55:42 +020085udp_fast_csum_update(int af, struct udphdr *uhdr,
86 const union nf_inet_addr *oldip,
87 const union nf_inet_addr *newip,
Al Viro014d7302006-09-28 14:29:52 -070088 __be16 oldport, __be16 newport)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Julius Volz0bbdd422008-09-02 15:55:42 +020090#ifdef CONFIG_IP_VS_IPV6
91 if (af == AF_INET6)
92 uhdr->check =
93 csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
94 ip_vs_check_diff2(oldport, newport,
95 ~csum_unfold(uhdr->check))));
96 else
97#endif
98 uhdr->check =
99 csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
100 ip_vs_check_diff2(oldport, newport,
101 ~csum_unfold(uhdr->check))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!uhdr->check)
Al Virof6ab0282006-11-16 02:36:50 -0800103 uhdr->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Simon Horman503e81f2008-09-08 12:04:21 +1000106static inline void
107udp_partial_csum_update(int af, struct udphdr *uhdr,
108 const union nf_inet_addr *oldip,
109 const union nf_inet_addr *newip,
110 __be16 oldlen, __be16 newlen)
111{
112#ifdef CONFIG_IP_VS_IPV6
113 if (af == AF_INET6)
114 uhdr->check =
Julian Anastasov5bc90682010-10-17 16:14:31 +0300115 ~csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
Simon Horman503e81f2008-09-08 12:04:21 +1000116 ip_vs_check_diff2(oldlen, newlen,
Julian Anastasov5bc90682010-10-17 16:14:31 +0300117 csum_unfold(uhdr->check))));
Simon Horman503e81f2008-09-08 12:04:21 +1000118 else
119#endif
120 uhdr->check =
Julian Anastasov5bc90682010-10-17 16:14:31 +0300121 ~csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
Simon Horman503e81f2008-09-08 12:04:21 +1000122 ip_vs_check_diff2(oldlen, newlen,
Julian Anastasov5bc90682010-10-17 16:14:31 +0300123 csum_unfold(uhdr->check))));
Simon Horman503e81f2008-09-08 12:04:21 +1000124}
125
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700128udp_snat_handler(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
130{
131 struct udphdr *udph;
Julius Volz0bbdd422008-09-02 15:55:42 +0200132 unsigned int udphoff;
Simon Horman503e81f2008-09-08 12:04:21 +1000133 int oldlen;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300134 int payload_csum = 0;
Julius Volz0bbdd422008-09-02 15:55:42 +0200135
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200136 struct ip_vs_iphdr iph;
137 ip_vs_fill_iph_skb(cp->af, skb, &iph);
138 udphoff = iph.len;
139
Julius Volz0bbdd422008-09-02 15:55:42 +0200140#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200141 if (cp->af == AF_INET6 && iph.fragoffs)
142 return 1;
Julius Volz0bbdd422008-09-02 15:55:42 +0200143#endif
Simon Horman503e81f2008-09-08 12:04:21 +1000144 oldlen = skb->len - udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /* csum_check requires unshared skb */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700147 if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
149
150 if (unlikely(cp->app != NULL)) {
Julian Anastasov8b27b102010-10-17 16:17:20 +0300151 int ret;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* Some checks before mangling */
Julius Volz0bbdd422008-09-02 15:55:42 +0200154 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 0;
156
157 /*
158 * Call application helper if needed
159 */
Julian Anastasov8b27b102010-10-17 16:17:20 +0300160 if (!(ret = ip_vs_app_pkt_out(cp, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return 0;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300162 /* ret=2: csum update is needed after payload mangling */
163 if (ret == 1)
164 oldlen = skb->len - udphoff;
165 else
166 payload_csum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
Julius Volz0bbdd422008-09-02 15:55:42 +0200169 udph = (void *)skb_network_header(skb) + udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 udph->source = cp->vport;
171
172 /*
173 * Adjust UDP checksums
174 */
Simon Horman503e81f2008-09-08 12:04:21 +1000175 if (skb->ip_summed == CHECKSUM_PARTIAL) {
176 udp_partial_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
Harvey Harrisonca620592008-11-06 23:09:56 -0800177 htons(oldlen),
178 htons(skb->len - udphoff));
Julian Anastasov8b27b102010-10-17 16:17:20 +0300179 } else if (!payload_csum && (udph->check != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* Only port and addr are changed, do fast csum update */
Julius Volz0bbdd422008-09-02 15:55:42 +0200181 udp_fast_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 cp->dport, cp->vport);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700183 if (skb->ip_summed == CHECKSUM_COMPLETE)
Julian Anastasov8b27b102010-10-17 16:17:20 +0300184 skb->ip_summed = (cp->app && pp->csum_check) ?
185 CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 } else {
187 /* full checksum calculation */
188 udph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700189 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
Julius Volz0bbdd422008-09-02 15:55:42 +0200190#ifdef CONFIG_IP_VS_IPV6
191 if (cp->af == AF_INET6)
192 udph->check = csum_ipv6_magic(&cp->vaddr.in6,
193 &cp->caddr.in6,
194 skb->len - udphoff,
195 cp->protocol, skb->csum);
196 else
197#endif
198 udph->check = csum_tcpudp_magic(cp->vaddr.ip,
199 cp->caddr.ip,
200 skb->len - udphoff,
201 cp->protocol,
202 skb->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (udph->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800204 udph->check = CSUM_MANGLED_0;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300205 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
207 pp->name, udph->check,
208 (char*)&(udph->check) - (char*)udph);
209 }
210 return 1;
211}
212
213
214static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700215udp_dnat_handler(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
217{
218 struct udphdr *udph;
Julius Volz0bbdd422008-09-02 15:55:42 +0200219 unsigned int udphoff;
Simon Horman503e81f2008-09-08 12:04:21 +1000220 int oldlen;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300221 int payload_csum = 0;
Julius Volz0bbdd422008-09-02 15:55:42 +0200222
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200223 struct ip_vs_iphdr iph;
224 ip_vs_fill_iph_skb(cp->af, skb, &iph);
225 udphoff = iph.len;
226
Julius Volz0bbdd422008-09-02 15:55:42 +0200227#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200228 if (cp->af == AF_INET6 && iph.fragoffs)
229 return 1;
Julius Volz0bbdd422008-09-02 15:55:42 +0200230#endif
Simon Horman503e81f2008-09-08 12:04:21 +1000231 oldlen = skb->len - udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* csum_check requires unshared skb */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700234 if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236
237 if (unlikely(cp->app != NULL)) {
Julian Anastasov8b27b102010-10-17 16:17:20 +0300238 int ret;
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* Some checks before mangling */
Julius Volz0bbdd422008-09-02 15:55:42 +0200241 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243
244 /*
245 * Attempt ip_vs_app call.
246 * It will fix ip_vs_conn
247 */
Julian Anastasov8b27b102010-10-17 16:17:20 +0300248 if (!(ret = ip_vs_app_pkt_in(cp, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300250 /* ret=2: csum update is needed after payload mangling */
251 if (ret == 1)
252 oldlen = skb->len - udphoff;
253 else
254 payload_csum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256
Julius Volz0bbdd422008-09-02 15:55:42 +0200257 udph = (void *)skb_network_header(skb) + udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 udph->dest = cp->dport;
259
260 /*
261 * Adjust UDP checksums
262 */
Simon Horman503e81f2008-09-08 12:04:21 +1000263 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Julian Anastasov5bc90682010-10-17 16:14:31 +0300264 udp_partial_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr,
Harvey Harrisonca620592008-11-06 23:09:56 -0800265 htons(oldlen),
266 htons(skb->len - udphoff));
Julian Anastasov8b27b102010-10-17 16:17:20 +0300267 } else if (!payload_csum && (udph->check != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* Only port and addr are changed, do fast csum update */
Julius Volz0bbdd422008-09-02 15:55:42 +0200269 udp_fast_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 cp->vport, cp->dport);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700271 if (skb->ip_summed == CHECKSUM_COMPLETE)
Julian Anastasov8b27b102010-10-17 16:17:20 +0300272 skb->ip_summed = (cp->app && pp->csum_check) ?
273 CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 } else {
275 /* full checksum calculation */
276 udph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700277 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
Julius Volz0bbdd422008-09-02 15:55:42 +0200278#ifdef CONFIG_IP_VS_IPV6
279 if (cp->af == AF_INET6)
280 udph->check = csum_ipv6_magic(&cp->caddr.in6,
281 &cp->daddr.in6,
282 skb->len - udphoff,
283 cp->protocol, skb->csum);
284 else
285#endif
286 udph->check = csum_tcpudp_magic(cp->caddr.ip,
287 cp->daddr.ip,
288 skb->len - udphoff,
289 cp->protocol,
290 skb->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (udph->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800292 udph->check = CSUM_MANGLED_0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700293 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 return 1;
296}
297
298
299static int
Julius Volz51ef3482008-09-02 15:55:40 +0200300udp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct udphdr _udph, *uh;
Julius Volz51ef3482008-09-02 15:55:40 +0200303 unsigned int udphoff;
304
305#ifdef CONFIG_IP_VS_IPV6
306 if (af == AF_INET6)
307 udphoff = sizeof(struct ipv6hdr);
308 else
309#endif
310 udphoff = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph);
313 if (uh == NULL)
314 return 0;
315
316 if (uh->check != 0) {
317 switch (skb->ip_summed) {
318 case CHECKSUM_NONE:
319 skb->csum = skb_checksum(skb, udphoff,
320 skb->len - udphoff, 0);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700321 case CHECKSUM_COMPLETE:
Julius Volz51ef3482008-09-02 15:55:40 +0200322#ifdef CONFIG_IP_VS_IPV6
323 if (af == AF_INET6) {
324 if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
325 &ipv6_hdr(skb)->daddr,
326 skb->len - udphoff,
327 ipv6_hdr(skb)->nexthdr,
328 skb->csum)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300329 IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
Julius Volz51ef3482008-09-02 15:55:40 +0200330 "Failed checksum for");
331 return 0;
332 }
333 } else
334#endif
335 if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
336 ip_hdr(skb)->daddr,
337 skb->len - udphoff,
338 ip_hdr(skb)->protocol,
339 skb->csum)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300340 IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
Julius Volz51ef3482008-09-02 15:55:40 +0200341 "Failed checksum for");
342 return 0;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 break;
345 default:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700346 /* No need to checksum. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 break;
348 }
349 }
350 return 1;
351}
352
Al Viro75e7ce62006-11-14 21:13:28 -0800353static inline __u16 udp_app_hashkey(__be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Al Viro75e7ce62006-11-14 21:13:28 -0800355 return (((__force u16)port >> UDP_APP_TAB_BITS) ^ (__force u16)port)
356 & UDP_APP_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100360static int udp_register_app(struct net *net, struct ip_vs_app *inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct ip_vs_app *i;
Al Viro75e7ce62006-11-14 21:13:28 -0800363 __u16 hash;
364 __be16 port = inc->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int ret = 0;
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100366 struct netns_ipvs *ipvs = net_ipvs(net);
367 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 hash = udp_app_hashkey(port);
370
371
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100372 spin_lock_bh(&ipvs->udp_app_lock);
373 list_for_each_entry(i, &ipvs->udp_apps[hash], p_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (i->port == port) {
375 ret = -EEXIST;
376 goto out;
377 }
378 }
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100379 list_add(&inc->p_list, &ipvs->udp_apps[hash]);
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100380 atomic_inc(&pd->appcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 out:
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100383 spin_unlock_bh(&ipvs->udp_app_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return ret;
385}
386
387
388static void
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100389udp_unregister_app(struct net *net, struct ip_vs_app *inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100391 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_UDP);
392 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100393
394 spin_lock_bh(&ipvs->udp_app_lock);
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100395 atomic_dec(&pd->appcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 list_del(&inc->p_list);
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100397 spin_unlock_bh(&ipvs->udp_app_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400
401static int udp_app_conn_bind(struct ip_vs_conn *cp)
402{
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100403 struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(cp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int hash;
405 struct ip_vs_app *inc;
406 int result = 0;
407
408 /* Default binding: bind app only for NAT */
409 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
410 return 0;
411
412 /* Lookup application incarnations and bind the right one */
413 hash = udp_app_hashkey(cp->vport);
414
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100415 spin_lock(&ipvs->udp_app_lock);
416 list_for_each_entry(inc, &ipvs->udp_apps[hash], p_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (inc->port == cp->vport) {
418 if (unlikely(!ip_vs_app_inc_get(inc)))
419 break;
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100420 spin_unlock(&ipvs->udp_app_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Hannes Eder1e3e2382009-08-02 11:05:41 +0000422 IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->"
Julius Volzcfc78c52008-09-02 15:55:53 +0200423 "%s:%u to app %s on port %u\n",
424 __func__,
425 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
426 ntohs(cp->cport),
427 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
428 ntohs(cp->vport),
429 inc->name, ntohs(inc->port));
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 cp->app = inc;
432 if (inc->init_conn)
433 result = inc->init_conn(inc, cp);
434 goto out;
435 }
436 }
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100437 spin_unlock(&ipvs->udp_app_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 out:
440 return result;
441}
442
443
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100444static const int udp_timeouts[IP_VS_UDP_S_LAST+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 [IP_VS_UDP_S_NORMAL] = 5*60*HZ,
446 [IP_VS_UDP_S_LAST] = 2*HZ,
447};
448
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700449static const char *const udp_state_name_table[IP_VS_UDP_S_LAST+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 [IP_VS_UDP_S_NORMAL] = "UDP",
451 [IP_VS_UDP_S_LAST] = "BUG!",
452};
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static const char * udp_state_name(int state)
455{
456 if (state >= IP_VS_UDP_S_LAST)
457 return "ERR!";
458 return udp_state_name_table[state] ? udp_state_name_table[state] : "?";
459}
460
Simon Horman4a516f12011-09-16 14:11:49 +0900461static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462udp_state_transition(struct ip_vs_conn *cp, int direction,
463 const struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100464 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100466 if (unlikely(!pd)) {
467 pr_err("UDP no ns data\n");
Simon Horman4a516f12011-09-16 14:11:49 +0900468 return;
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100469 }
470
471 cp->timeout = pd->timeout_table[IP_VS_UDP_S_NORMAL];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Hans Schillstrom582b8e32012-04-26 09:45:35 +0200474static int __udp_init(struct net *net, struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100476 struct netns_ipvs *ipvs = net_ipvs(net);
477
478 ip_vs_init_hash_table(ipvs->udp_apps, UDP_APP_TAB_SIZE);
479 spin_lock_init(&ipvs->udp_app_lock);
480 pd->timeout_table = ip_vs_create_timeout_table((int *)udp_timeouts,
481 sizeof(udp_timeouts));
Hans Schillstrom582b8e32012-04-26 09:45:35 +0200482 if (!pd->timeout_table)
483 return -ENOMEM;
484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100487static void __udp_exit(struct net *net, struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100489 kfree(pd->timeout_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492
493struct ip_vs_protocol ip_vs_protocol_udp = {
494 .name = "UDP",
495 .protocol = IPPROTO_UDP,
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700496 .num_states = IP_VS_UDP_S_LAST,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 .dont_defrag = 0,
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100498 .init = NULL,
499 .exit = NULL,
500 .init_netns = __udp_init,
501 .exit_netns = __udp_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 .conn_schedule = udp_conn_schedule,
Simon Horman5c0d2372010-08-02 17:12:44 +0200503 .conn_in_get = ip_vs_conn_in_get_proto,
504 .conn_out_get = ip_vs_conn_out_get_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .snat_handler = udp_snat_handler,
506 .dnat_handler = udp_dnat_handler,
507 .csum_check = udp_csum_check,
508 .state_transition = udp_state_transition,
509 .state_name = udp_state_name,
510 .register_app = udp_register_app,
511 .unregister_app = udp_unregister_app,
512 .app_conn_bind = udp_app_conn_bind,
513 .debug_packet = ip_vs_tcpudp_debug_packet,
514 .timeout_change = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515};