blob: e494e9a88c7fb4d089845727bbc63778d352de4f [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
Eric W. Biedermand8f44c32015-09-21 13:02:43 -050032udp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
33 struct ip_vs_proto_data *pd,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020034 int *verdict, struct ip_vs_conn **cpp,
35 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 struct ip_vs_service *svc;
38 struct udphdr _udph, *uh;
Alex Gartrell2b0f39e2015-08-26 09:40:40 -070039 __be16 _ports[2], *ports = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Alex Gartrell2b0f39e2015-08-26 09:40:40 -070041 if (likely(!ip_vs_iph_icmp(iph))) {
42 /* IPv6 fragments, only first fragment will hit this */
43 uh = skb_header_pointer(skb, iph->len, sizeof(_udph), &_udph);
44 if (uh)
45 ports = &uh->source;
46 } else {
47 ports = skb_header_pointer(
48 skb, iph->len, sizeof(_ports), &_ports);
Alex Gartrell6044eef2015-08-26 09:40:37 -070049 }
50
Alex Gartrell2b0f39e2015-08-26 09:40:40 -070051 if (!ports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *verdict = NF_DROP;
53 return 0;
54 }
Alex Gartrell2b0f39e2015-08-26 09:40:40 -070055
Julian Anastasovceec4c32013-03-22 11:46:53 +020056 rcu_read_lock();
Alex Gartrell2b0f39e2015-08-26 09:40:40 -070057 if (likely(!ip_vs_iph_inverse(iph)))
Eric W. Biederman0a4fd6c2015-09-21 13:01:49 -050058 svc = ip_vs_service_find(ipvs, af, skb->mark, iph->protocol,
Alex Gartrell2b0f39e2015-08-26 09:40:40 -070059 &iph->daddr, ports[1]);
60 else
Eric W. Biederman0a4fd6c2015-09-21 13:01:49 -050061 svc = ip_vs_service_find(ipvs, af, skb->mark, iph->protocol,
Alex Gartrell2b0f39e2015-08-26 09:40:40 -070062 &iph->saddr, ports[0]);
63
Julius Volz3c2e0502008-09-02 15:55:38 +020064 if (svc) {
Julian Anastasov190ecd22010-10-17 16:24:37 +030065 int ignored;
66
Eric W. Biedermand8f44c32015-09-21 13:02:43 -050067 if (ip_vs_todrop(ipvs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /*
69 * It seems that we are very loaded.
70 * We have to drop this packet :(
71 */
Julian Anastasovceec4c32013-03-22 11:46:53 +020072 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *verdict = NF_DROP;
74 return 0;
75 }
76
77 /*
78 * Let the virtual server select a real server for the
79 * incoming connection, and create a connection entry.
80 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020081 *cpp = ip_vs_schedule(svc, skb, pd, &ignored, iph);
Hans Schillstroma5959d52010-11-19 14:25:10 +010082 if (!*cpp && ignored <= 0) {
83 if (!ignored)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020084 *verdict = ip_vs_leave(svc, skb, pd, iph);
Julian Anastasovceec4c32013-03-22 11:46:53 +020085 else
Hans Schillstroma5959d52010-11-19 14:25:10 +010086 *verdict = NF_DROP;
Julian Anastasovceec4c32013-03-22 11:46:53 +020087 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 0;
89 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Julian Anastasovceec4c32013-03-22 11:46:53 +020091 rcu_read_unlock();
Hans Schillstroma5959d52010-11-19 14:25:10 +010092 /* NF_ACCEPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 1;
94}
95
96
97static inline void
Julius Volz0bbdd422008-09-02 15:55:42 +020098udp_fast_csum_update(int af, struct udphdr *uhdr,
99 const union nf_inet_addr *oldip,
100 const union nf_inet_addr *newip,
Al Viro014d7302006-09-28 14:29:52 -0700101 __be16 oldport, __be16 newport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Julius Volz0bbdd422008-09-02 15:55:42 +0200103#ifdef CONFIG_IP_VS_IPV6
104 if (af == AF_INET6)
105 uhdr->check =
106 csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
107 ip_vs_check_diff2(oldport, newport,
108 ~csum_unfold(uhdr->check))));
109 else
110#endif
111 uhdr->check =
112 csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
113 ip_vs_check_diff2(oldport, newport,
114 ~csum_unfold(uhdr->check))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (!uhdr->check)
Al Virof6ab0282006-11-16 02:36:50 -0800116 uhdr->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Simon Horman503e81f2008-09-08 12:04:21 +1000119static inline void
120udp_partial_csum_update(int af, struct udphdr *uhdr,
121 const union nf_inet_addr *oldip,
122 const union nf_inet_addr *newip,
123 __be16 oldlen, __be16 newlen)
124{
125#ifdef CONFIG_IP_VS_IPV6
126 if (af == AF_INET6)
127 uhdr->check =
Julian Anastasov5bc90682010-10-17 16:14:31 +0300128 ~csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
Simon Horman503e81f2008-09-08 12:04:21 +1000129 ip_vs_check_diff2(oldlen, newlen,
Julian Anastasov5bc90682010-10-17 16:14:31 +0300130 csum_unfold(uhdr->check))));
Simon Horman503e81f2008-09-08 12:04:21 +1000131 else
132#endif
133 uhdr->check =
Julian Anastasov5bc90682010-10-17 16:14:31 +0300134 ~csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
Simon Horman503e81f2008-09-08 12:04:21 +1000135 ip_vs_check_diff2(oldlen, newlen,
Julian Anastasov5bc90682010-10-17 16:14:31 +0300136 csum_unfold(uhdr->check))));
Simon Horman503e81f2008-09-08 12:04:21 +1000137}
138
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static int
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200141udp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
142 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct udphdr *udph;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200145 unsigned int udphoff = iph->len;
Simon Horman503e81f2008-09-08 12:04:21 +1000146 int oldlen;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300147 int payload_csum = 0;
Julius Volz0bbdd422008-09-02 15:55:42 +0200148
149#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200150 if (cp->af == AF_INET6 && iph->fragoffs)
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200151 return 1;
Julius Volz0bbdd422008-09-02 15:55:42 +0200152#endif
Simon Horman503e81f2008-09-08 12:04:21 +1000153 oldlen = skb->len - udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* csum_check requires unshared skb */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700156 if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return 0;
158
159 if (unlikely(cp->app != NULL)) {
Julian Anastasov8b27b102010-10-17 16:17:20 +0300160 int ret;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /* Some checks before mangling */
Julius Volz0bbdd422008-09-02 15:55:42 +0200163 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
165
166 /*
167 * Call application helper if needed
168 */
Julian Anastasov8b27b102010-10-17 16:17:20 +0300169 if (!(ret = ip_vs_app_pkt_out(cp, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300171 /* ret=2: csum update is needed after payload mangling */
172 if (ret == 1)
173 oldlen = skb->len - udphoff;
174 else
175 payload_csum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
Julius Volz0bbdd422008-09-02 15:55:42 +0200178 udph = (void *)skb_network_header(skb) + udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 udph->source = cp->vport;
180
181 /*
182 * Adjust UDP checksums
183 */
Simon Horman503e81f2008-09-08 12:04:21 +1000184 if (skb->ip_summed == CHECKSUM_PARTIAL) {
185 udp_partial_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
Harvey Harrisonca620592008-11-06 23:09:56 -0800186 htons(oldlen),
187 htons(skb->len - udphoff));
Julian Anastasov8b27b102010-10-17 16:17:20 +0300188 } else if (!payload_csum && (udph->check != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Only port and addr are changed, do fast csum update */
Julius Volz0bbdd422008-09-02 15:55:42 +0200190 udp_fast_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 cp->dport, cp->vport);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700192 if (skb->ip_summed == CHECKSUM_COMPLETE)
Julian Anastasov8b27b102010-10-17 16:17:20 +0300193 skb->ip_summed = (cp->app && pp->csum_check) ?
194 CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 } else {
196 /* full checksum calculation */
197 udph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700198 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
Julius Volz0bbdd422008-09-02 15:55:42 +0200199#ifdef CONFIG_IP_VS_IPV6
200 if (cp->af == AF_INET6)
201 udph->check = csum_ipv6_magic(&cp->vaddr.in6,
202 &cp->caddr.in6,
203 skb->len - udphoff,
204 cp->protocol, skb->csum);
205 else
206#endif
207 udph->check = csum_tcpudp_magic(cp->vaddr.ip,
208 cp->caddr.ip,
209 skb->len - udphoff,
210 cp->protocol,
211 skb->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (udph->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800213 udph->check = CSUM_MANGLED_0;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300214 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
216 pp->name, udph->check,
217 (char*)&(udph->check) - (char*)udph);
218 }
219 return 1;
220}
221
222
223static int
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200224udp_dnat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
225 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct udphdr *udph;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200228 unsigned int udphoff = iph->len;
Simon Horman503e81f2008-09-08 12:04:21 +1000229 int oldlen;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300230 int payload_csum = 0;
Julius Volz0bbdd422008-09-02 15:55:42 +0200231
232#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200233 if (cp->af == AF_INET6 && iph->fragoffs)
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200234 return 1;
Julius Volz0bbdd422008-09-02 15:55:42 +0200235#endif
Simon Horman503e81f2008-09-08 12:04:21 +1000236 oldlen = skb->len - udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /* csum_check requires unshared skb */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700239 if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return 0;
241
242 if (unlikely(cp->app != NULL)) {
Julian Anastasov8b27b102010-10-17 16:17:20 +0300243 int ret;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* Some checks before mangling */
Julius Volz0bbdd422008-09-02 15:55:42 +0200246 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
248
249 /*
250 * Attempt ip_vs_app call.
251 * It will fix ip_vs_conn
252 */
Julian Anastasov8b27b102010-10-17 16:17:20 +0300253 if (!(ret = ip_vs_app_pkt_in(cp, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300255 /* ret=2: csum update is needed after payload mangling */
256 if (ret == 1)
257 oldlen = skb->len - udphoff;
258 else
259 payload_csum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
Julius Volz0bbdd422008-09-02 15:55:42 +0200262 udph = (void *)skb_network_header(skb) + udphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 udph->dest = cp->dport;
264
265 /*
266 * Adjust UDP checksums
267 */
Simon Horman503e81f2008-09-08 12:04:21 +1000268 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Julian Anastasov5bc90682010-10-17 16:14:31 +0300269 udp_partial_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr,
Harvey Harrisonca620592008-11-06 23:09:56 -0800270 htons(oldlen),
271 htons(skb->len - udphoff));
Julian Anastasov8b27b102010-10-17 16:17:20 +0300272 } else if (!payload_csum && (udph->check != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Only port and addr are changed, do fast csum update */
Julius Volz0bbdd422008-09-02 15:55:42 +0200274 udp_fast_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 cp->vport, cp->dport);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700276 if (skb->ip_summed == CHECKSUM_COMPLETE)
Julian Anastasov8b27b102010-10-17 16:17:20 +0300277 skb->ip_summed = (cp->app && pp->csum_check) ?
278 CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 } else {
280 /* full checksum calculation */
281 udph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700282 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
Julius Volz0bbdd422008-09-02 15:55:42 +0200283#ifdef CONFIG_IP_VS_IPV6
284 if (cp->af == AF_INET6)
285 udph->check = csum_ipv6_magic(&cp->caddr.in6,
286 &cp->daddr.in6,
287 skb->len - udphoff,
288 cp->protocol, skb->csum);
289 else
290#endif
291 udph->check = csum_tcpudp_magic(cp->caddr.ip,
292 cp->daddr.ip,
293 skb->len - udphoff,
294 cp->protocol,
295 skb->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (udph->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800297 udph->check = CSUM_MANGLED_0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700298 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 return 1;
301}
302
303
304static int
Julius Volz51ef3482008-09-02 15:55:40 +0200305udp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 struct udphdr _udph, *uh;
Julius Volz51ef3482008-09-02 15:55:40 +0200308 unsigned int udphoff;
309
310#ifdef CONFIG_IP_VS_IPV6
311 if (af == AF_INET6)
312 udphoff = sizeof(struct ipv6hdr);
313 else
314#endif
315 udphoff = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph);
318 if (uh == NULL)
319 return 0;
320
321 if (uh->check != 0) {
322 switch (skb->ip_summed) {
323 case CHECKSUM_NONE:
324 skb->csum = skb_checksum(skb, udphoff,
325 skb->len - udphoff, 0);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700326 case CHECKSUM_COMPLETE:
Julius Volz51ef3482008-09-02 15:55:40 +0200327#ifdef CONFIG_IP_VS_IPV6
328 if (af == AF_INET6) {
329 if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
330 &ipv6_hdr(skb)->daddr,
331 skb->len - udphoff,
332 ipv6_hdr(skb)->nexthdr,
333 skb->csum)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300334 IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
Julius Volz51ef3482008-09-02 15:55:40 +0200335 "Failed checksum for");
336 return 0;
337 }
338 } else
339#endif
340 if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
341 ip_hdr(skb)->daddr,
342 skb->len - udphoff,
343 ip_hdr(skb)->protocol,
344 skb->csum)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300345 IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
Julius Volz51ef3482008-09-02 15:55:40 +0200346 "Failed checksum for");
347 return 0;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 break;
350 default:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700351 /* No need to checksum. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
353 }
354 }
355 return 1;
356}
357
Al Viro75e7ce62006-11-14 21:13:28 -0800358static inline __u16 udp_app_hashkey(__be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Al Viro75e7ce62006-11-14 21:13:28 -0800360 return (((__force u16)port >> UDP_APP_TAB_BITS) ^ (__force u16)port)
361 & UDP_APP_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364
Eric W. Biederman19648912015-09-21 13:02:29 -0500365static int udp_register_app(struct netns_ipvs *ipvs, struct ip_vs_app *inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct ip_vs_app *i;
Al Viro75e7ce62006-11-14 21:13:28 -0800368 __u16 hash;
369 __be16 port = inc->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int ret = 0;
Eric W. Biederman18d6ade2015-09-21 13:02:01 -0500371 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 hash = udp_app_hashkey(port);
374
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100375 list_for_each_entry(i, &ipvs->udp_apps[hash], p_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (i->port == port) {
377 ret = -EEXIST;
378 goto out;
379 }
380 }
Julian Anastasov363c97d2013-03-21 11:58:07 +0200381 list_add_rcu(&inc->p_list, &ipvs->udp_apps[hash]);
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100382 atomic_inc(&pd->appcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return ret;
386}
387
388
389static void
Eric W. Biederman19648912015-09-21 13:02:29 -0500390udp_unregister_app(struct netns_ipvs *ipvs, struct ip_vs_app *inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Eric W. Biederman19648912015-09-21 13:02:29 -0500392 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100393
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100394 atomic_dec(&pd->appcnt);
Julian Anastasov363c97d2013-03-21 11:58:07 +0200395 list_del_rcu(&inc->p_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398
399static int udp_app_conn_bind(struct ip_vs_conn *cp)
400{
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500401 struct netns_ipvs *ipvs = cp->ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int hash;
403 struct ip_vs_app *inc;
404 int result = 0;
405
406 /* Default binding: bind app only for NAT */
407 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
408 return 0;
409
410 /* Lookup application incarnations and bind the right one */
411 hash = udp_app_hashkey(cp->vport);
412
Julian Anastasov363c97d2013-03-21 11:58:07 +0200413 rcu_read_lock();
414 list_for_each_entry_rcu(inc, &ipvs->udp_apps[hash], p_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (inc->port == cp->vport) {
416 if (unlikely(!ip_vs_app_inc_get(inc)))
417 break;
Julian Anastasov363c97d2013-03-21 11:58:07 +0200418 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Hannes Eder1e3e2382009-08-02 11:05:41 +0000420 IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->"
Julius Volzcfc78c52008-09-02 15:55:53 +0200421 "%s:%u to app %s on port %u\n",
422 __func__,
423 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
424 ntohs(cp->cport),
425 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
426 ntohs(cp->vport),
427 inc->name, ntohs(inc->port));
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 cp->app = inc;
430 if (inc->init_conn)
431 result = inc->init_conn(inc, cp);
432 goto out;
433 }
434 }
Julian Anastasov363c97d2013-03-21 11:58:07 +0200435 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 out:
438 return result;
439}
440
441
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100442static const int udp_timeouts[IP_VS_UDP_S_LAST+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 [IP_VS_UDP_S_NORMAL] = 5*60*HZ,
444 [IP_VS_UDP_S_LAST] = 2*HZ,
445};
446
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700447static const char *const udp_state_name_table[IP_VS_UDP_S_LAST+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 [IP_VS_UDP_S_NORMAL] = "UDP",
449 [IP_VS_UDP_S_LAST] = "BUG!",
450};
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452static const char * udp_state_name(int state)
453{
454 if (state >= IP_VS_UDP_S_LAST)
455 return "ERR!";
456 return udp_state_name_table[state] ? udp_state_name_table[state] : "?";
457}
458
Simon Horman4a516f12011-09-16 14:11:49 +0900459static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460udp_state_transition(struct ip_vs_conn *cp, int direction,
461 const struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100462 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100464 if (unlikely(!pd)) {
465 pr_err("UDP no ns data\n");
Simon Horman4a516f12011-09-16 14:11:49 +0900466 return;
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100467 }
468
469 cp->timeout = pd->timeout_table[IP_VS_UDP_S_NORMAL];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Eric W. Biederman1281a9c22015-09-21 13:02:36 -0500472static int __udp_init(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100474 ip_vs_init_hash_table(ipvs->udp_apps, UDP_APP_TAB_SIZE);
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100475 pd->timeout_table = ip_vs_create_timeout_table((int *)udp_timeouts,
476 sizeof(udp_timeouts));
Hans Schillstrom582b8e32012-04-26 09:45:35 +0200477 if (!pd->timeout_table)
478 return -ENOMEM;
479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Eric W. Biederman1281a9c22015-09-21 13:02:36 -0500482static void __udp_exit(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100484 kfree(pd->timeout_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487
488struct ip_vs_protocol ip_vs_protocol_udp = {
489 .name = "UDP",
490 .protocol = IPPROTO_UDP,
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700491 .num_states = IP_VS_UDP_S_LAST,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 .dont_defrag = 0,
Hans Schillstrom78b16bd2011-01-03 14:44:48 +0100493 .init = NULL,
494 .exit = NULL,
495 .init_netns = __udp_init,
496 .exit_netns = __udp_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 .conn_schedule = udp_conn_schedule,
Simon Horman5c0d2372010-08-02 17:12:44 +0200498 .conn_in_get = ip_vs_conn_in_get_proto,
499 .conn_out_get = ip_vs_conn_out_get_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 .snat_handler = udp_snat_handler,
501 .dnat_handler = udp_dnat_handler,
502 .csum_check = udp_csum_check,
503 .state_transition = udp_state_transition,
504 .state_name = udp_state_name,
505 .register_app = udp_register_app,
506 .unregister_app = udp_unregister_app,
507 .app_conn_bind = udp_app_conn_bind,
508 .debug_packet = ip_vs_tcpudp_debug_packet,
509 .timeout_change = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510};