ipvs: avoid indirect calls when calculating checksums
[linux-2.6-block.git] / net / netfilter / ipvs / ip_vs_proto_udp.c
1 /*
2  * ip_vs_proto_udp.c:   UDP load balancing support for IPVS
3  *
4  * 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  *
12  * Changes:     Hans Schillstrom <hans.schillstrom@ericsson.com>
13  *              Network name space (netns) aware.
14  *
15  */
16
17 #define KMSG_COMPONENT "IPVS"
18 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19
20 #include <linux/in.h>
21 #include <linux/ip.h>
22 #include <linux/kernel.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter_ipv4.h>
25 #include <linux/udp.h>
26
27 #include <net/ip_vs.h>
28 #include <net/ip.h>
29 #include <net/ip6_checksum.h>
30
31 static int
32 udp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp);
33
34 static int
35 udp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
36                   struct ip_vs_proto_data *pd,
37                   int *verdict, struct ip_vs_conn **cpp,
38                   struct ip_vs_iphdr *iph)
39 {
40         struct ip_vs_service *svc;
41         struct udphdr _udph, *uh;
42         __be16 _ports[2], *ports = NULL;
43
44         if (likely(!ip_vs_iph_icmp(iph))) {
45                 /* IPv6 fragments, only first fragment will hit this */
46                 uh = skb_header_pointer(skb, iph->len, sizeof(_udph), &_udph);
47                 if (uh)
48                         ports = &uh->source;
49         } else {
50                 ports = skb_header_pointer(
51                         skb, iph->len, sizeof(_ports), &_ports);
52         }
53
54         if (!ports) {
55                 *verdict = NF_DROP;
56                 return 0;
57         }
58
59         if (likely(!ip_vs_iph_inverse(iph)))
60                 svc = ip_vs_service_find(ipvs, af, skb->mark, iph->protocol,
61                                          &iph->daddr, ports[1]);
62         else
63                 svc = ip_vs_service_find(ipvs, af, skb->mark, iph->protocol,
64                                          &iph->saddr, ports[0]);
65
66         if (svc) {
67                 int ignored;
68
69                 if (ip_vs_todrop(ipvs)) {
70                         /*
71                          * It seems that we are very loaded.
72                          * We have to drop this packet :(
73                          */
74                         *verdict = NF_DROP;
75                         return 0;
76                 }
77
78                 /*
79                  * Let the virtual server select a real server for the
80                  * incoming connection, and create a connection entry.
81                  */
82                 *cpp = ip_vs_schedule(svc, skb, pd, &ignored, iph);
83                 if (!*cpp && ignored <= 0) {
84                         if (!ignored)
85                                 *verdict = ip_vs_leave(svc, skb, pd, iph);
86                         else
87                                 *verdict = NF_DROP;
88                         return 0;
89                 }
90         }
91         /* NF_ACCEPT */
92         return 1;
93 }
94
95
96 static inline void
97 udp_fast_csum_update(int af, struct udphdr *uhdr,
98                      const union nf_inet_addr *oldip,
99                      const union nf_inet_addr *newip,
100                      __be16 oldport, __be16 newport)
101 {
102 #ifdef CONFIG_IP_VS_IPV6
103         if (af == AF_INET6)
104                 uhdr->check =
105                         csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
106                                          ip_vs_check_diff2(oldport, newport,
107                                                 ~csum_unfold(uhdr->check))));
108         else
109 #endif
110                 uhdr->check =
111                         csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
112                                          ip_vs_check_diff2(oldport, newport,
113                                                 ~csum_unfold(uhdr->check))));
114         if (!uhdr->check)
115                 uhdr->check = CSUM_MANGLED_0;
116 }
117
118 static inline void
119 udp_partial_csum_update(int af, struct udphdr *uhdr,
120                      const union nf_inet_addr *oldip,
121                      const union nf_inet_addr *newip,
122                      __be16 oldlen, __be16 newlen)
123 {
124 #ifdef CONFIG_IP_VS_IPV6
125         if (af == AF_INET6)
126                 uhdr->check =
127                         ~csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
128                                          ip_vs_check_diff2(oldlen, newlen,
129                                                 csum_unfold(uhdr->check))));
130         else
131 #endif
132         uhdr->check =
133                 ~csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
134                                 ip_vs_check_diff2(oldlen, newlen,
135                                                 csum_unfold(uhdr->check))));
136 }
137
138
139 static int
140 udp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
141                  struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
142 {
143         struct udphdr *udph;
144         unsigned int udphoff = iph->len;
145         int oldlen;
146         int payload_csum = 0;
147
148 #ifdef CONFIG_IP_VS_IPV6
149         if (cp->af == AF_INET6 && iph->fragoffs)
150                 return 1;
151 #endif
152         oldlen = skb->len - udphoff;
153
154         /* csum_check requires unshared skb */
155         if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
156                 return 0;
157
158         if (unlikely(cp->app != NULL)) {
159                 int ret;
160
161                 /* Some checks before mangling */
162                 if (!udp_csum_check(cp->af, skb, pp))
163                         return 0;
164
165                 /*
166                  *      Call application helper if needed
167                  */
168                 if (!(ret = ip_vs_app_pkt_out(cp, skb, iph)))
169                         return 0;
170                 /* ret=2: csum update is needed after payload mangling */
171                 if (ret == 1)
172                         oldlen = skb->len - udphoff;
173                 else
174                         payload_csum = 1;
175         }
176
177         udph = (void *)skb_network_header(skb) + udphoff;
178         udph->source = cp->vport;
179
180         /*
181          *      Adjust UDP checksums
182          */
183         if (skb->ip_summed == CHECKSUM_PARTIAL) {
184                 udp_partial_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
185                                         htons(oldlen),
186                                         htons(skb->len - udphoff));
187         } else if (!payload_csum && (udph->check != 0)) {
188                 /* Only port and addr are changed, do fast csum update */
189                 udp_fast_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
190                                      cp->dport, cp->vport);
191                 if (skb->ip_summed == CHECKSUM_COMPLETE)
192                         skb->ip_summed = cp->app ?
193                                          CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
194         } else {
195                 /* full checksum calculation */
196                 udph->check = 0;
197                 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
198 #ifdef CONFIG_IP_VS_IPV6
199                 if (cp->af == AF_INET6)
200                         udph->check = csum_ipv6_magic(&cp->vaddr.in6,
201                                                       &cp->caddr.in6,
202                                                       skb->len - udphoff,
203                                                       cp->protocol, skb->csum);
204                 else
205 #endif
206                         udph->check = csum_tcpudp_magic(cp->vaddr.ip,
207                                                         cp->caddr.ip,
208                                                         skb->len - udphoff,
209                                                         cp->protocol,
210                                                         skb->csum);
211                 if (udph->check == 0)
212                         udph->check = CSUM_MANGLED_0;
213                 skb->ip_summed = CHECKSUM_UNNECESSARY;
214                 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
215                           pp->name, udph->check,
216                           (char*)&(udph->check) - (char*)udph);
217         }
218         return 1;
219 }
220
221
222 static int
223 udp_dnat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
224                  struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
225 {
226         struct udphdr *udph;
227         unsigned int udphoff = iph->len;
228         int oldlen;
229         int payload_csum = 0;
230
231 #ifdef CONFIG_IP_VS_IPV6
232         if (cp->af == AF_INET6 && iph->fragoffs)
233                 return 1;
234 #endif
235         oldlen = skb->len - udphoff;
236
237         /* csum_check requires unshared skb */
238         if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
239                 return 0;
240
241         if (unlikely(cp->app != NULL)) {
242                 int ret;
243
244                 /* Some checks before mangling */
245                 if (!udp_csum_check(cp->af, skb, pp))
246                         return 0;
247
248                 /*
249                  *      Attempt ip_vs_app call.
250                  *      It will fix ip_vs_conn
251                  */
252                 if (!(ret = ip_vs_app_pkt_in(cp, skb, iph)))
253                         return 0;
254                 /* ret=2: csum update is needed after payload mangling */
255                 if (ret == 1)
256                         oldlen = skb->len - udphoff;
257                 else
258                         payload_csum = 1;
259         }
260
261         udph = (void *)skb_network_header(skb) + udphoff;
262         udph->dest = cp->dport;
263
264         /*
265          *      Adjust UDP checksums
266          */
267         if (skb->ip_summed == CHECKSUM_PARTIAL) {
268                 udp_partial_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr,
269                                         htons(oldlen),
270                                         htons(skb->len - udphoff));
271         } else if (!payload_csum && (udph->check != 0)) {
272                 /* Only port and addr are changed, do fast csum update */
273                 udp_fast_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr,
274                                      cp->vport, cp->dport);
275                 if (skb->ip_summed == CHECKSUM_COMPLETE)
276                         skb->ip_summed = cp->app ?
277                                          CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
278         } else {
279                 /* full checksum calculation */
280                 udph->check = 0;
281                 skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
282 #ifdef CONFIG_IP_VS_IPV6
283                 if (cp->af == AF_INET6)
284                         udph->check = csum_ipv6_magic(&cp->caddr.in6,
285                                                       &cp->daddr.in6,
286                                                       skb->len - udphoff,
287                                                       cp->protocol, skb->csum);
288                 else
289 #endif
290                         udph->check = csum_tcpudp_magic(cp->caddr.ip,
291                                                         cp->daddr.ip,
292                                                         skb->len - udphoff,
293                                                         cp->protocol,
294                                                         skb->csum);
295                 if (udph->check == 0)
296                         udph->check = CSUM_MANGLED_0;
297                 skb->ip_summed = CHECKSUM_UNNECESSARY;
298         }
299         return 1;
300 }
301
302
303 static int
304 udp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
305 {
306         struct udphdr _udph, *uh;
307         unsigned int udphoff;
308
309 #ifdef CONFIG_IP_VS_IPV6
310         if (af == AF_INET6)
311                 udphoff = sizeof(struct ipv6hdr);
312         else
313 #endif
314                 udphoff = ip_hdrlen(skb);
315
316         uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph);
317         if (uh == NULL)
318                 return 0;
319
320         if (uh->check != 0) {
321                 switch (skb->ip_summed) {
322                 case CHECKSUM_NONE:
323                         skb->csum = skb_checksum(skb, udphoff,
324                                                  skb->len - udphoff, 0);
325                         /* fall through */
326                 case CHECKSUM_COMPLETE:
327 #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)) {
334                                         IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
335                                                          "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)) {
345                                         IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
346                                                          "Failed checksum for");
347                                         return 0;
348                                 }
349                         break;
350                 default:
351                         /* No need to checksum. */
352                         break;
353                 }
354         }
355         return 1;
356 }
357
358 static inline __u16 udp_app_hashkey(__be16 port)
359 {
360         return (((__force u16)port >> UDP_APP_TAB_BITS) ^ (__force u16)port)
361                 & UDP_APP_TAB_MASK;
362 }
363
364
365 static int udp_register_app(struct netns_ipvs *ipvs, struct ip_vs_app *inc)
366 {
367         struct ip_vs_app *i;
368         __u16 hash;
369         __be16 port = inc->port;
370         int ret = 0;
371         struct ip_vs_proto_data *pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
372
373         hash = udp_app_hashkey(port);
374
375         list_for_each_entry(i, &ipvs->udp_apps[hash], p_list) {
376                 if (i->port == port) {
377                         ret = -EEXIST;
378                         goto out;
379                 }
380         }
381         list_add_rcu(&inc->p_list, &ipvs->udp_apps[hash]);
382         atomic_inc(&pd->appcnt);
383
384   out:
385         return ret;
386 }
387
388
389 static void
390 udp_unregister_app(struct netns_ipvs *ipvs, struct ip_vs_app *inc)
391 {
392         struct ip_vs_proto_data *pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP);
393
394         atomic_dec(&pd->appcnt);
395         list_del_rcu(&inc->p_list);
396 }
397
398
399 static int udp_app_conn_bind(struct ip_vs_conn *cp)
400 {
401         struct netns_ipvs *ipvs = cp->ipvs;
402         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
413         list_for_each_entry_rcu(inc, &ipvs->udp_apps[hash], p_list) {
414                 if (inc->port == cp->vport) {
415                         if (unlikely(!ip_vs_app_inc_get(inc)))
416                                 break;
417
418                         IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->"
419                                       "%s:%u to app %s on port %u\n",
420                                       __func__,
421                                       IP_VS_DBG_ADDR(cp->af, &cp->caddr),
422                                       ntohs(cp->cport),
423                                       IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
424                                       ntohs(cp->vport),
425                                       inc->name, ntohs(inc->port));
426
427                         cp->app = inc;
428                         if (inc->init_conn)
429                                 result = inc->init_conn(inc, cp);
430                         break;
431                 }
432         }
433
434         return result;
435 }
436
437
438 static const int udp_timeouts[IP_VS_UDP_S_LAST+1] = {
439         [IP_VS_UDP_S_NORMAL]            =       5*60*HZ,
440         [IP_VS_UDP_S_LAST]              =       2*HZ,
441 };
442
443 static const char *const udp_state_name_table[IP_VS_UDP_S_LAST+1] = {
444         [IP_VS_UDP_S_NORMAL]            =       "UDP",
445         [IP_VS_UDP_S_LAST]              =       "BUG!",
446 };
447
448 static const char * udp_state_name(int state)
449 {
450         if (state >= IP_VS_UDP_S_LAST)
451                 return "ERR!";
452         return udp_state_name_table[state] ? udp_state_name_table[state] : "?";
453 }
454
455 static void
456 udp_state_transition(struct ip_vs_conn *cp, int direction,
457                      const struct sk_buff *skb,
458                      struct ip_vs_proto_data *pd)
459 {
460         if (unlikely(!pd)) {
461                 pr_err("UDP no ns data\n");
462                 return;
463         }
464
465         cp->timeout = pd->timeout_table[IP_VS_UDP_S_NORMAL];
466         if (direction == IP_VS_DIR_OUTPUT)
467                 ip_vs_control_assure_ct(cp);
468 }
469
470 static int __udp_init(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd)
471 {
472         ip_vs_init_hash_table(ipvs->udp_apps, UDP_APP_TAB_SIZE);
473         pd->timeout_table = ip_vs_create_timeout_table((int *)udp_timeouts,
474                                                         sizeof(udp_timeouts));
475         if (!pd->timeout_table)
476                 return -ENOMEM;
477         return 0;
478 }
479
480 static void __udp_exit(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd)
481 {
482         kfree(pd->timeout_table);
483 }
484
485
486 struct ip_vs_protocol ip_vs_protocol_udp = {
487         .name =                 "UDP",
488         .protocol =             IPPROTO_UDP,
489         .num_states =           IP_VS_UDP_S_LAST,
490         .dont_defrag =          0,
491         .init =                 NULL,
492         .exit =                 NULL,
493         .init_netns =           __udp_init,
494         .exit_netns =           __udp_exit,
495         .conn_schedule =        udp_conn_schedule,
496         .conn_in_get =          ip_vs_conn_in_get_proto,
497         .conn_out_get =         ip_vs_conn_out_get_proto,
498         .snat_handler =         udp_snat_handler,
499         .dnat_handler =         udp_dnat_handler,
500         .state_transition =     udp_state_transition,
501         .state_name =           udp_state_name,
502         .register_app =         udp_register_app,
503         .unregister_app =       udp_unregister_app,
504         .app_conn_bind =        udp_app_conn_bind,
505         .debug_packet =         ip_vs_tcpudp_debug_packet,
506         .timeout_change =       NULL,
507 };