2 * ip_vs_proto_udp.c: UDP load balancing support for IPVS
4 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
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.
12 * Changes: Hans Schillstrom <hans.schillstrom@ericsson.com>
13 * Network name space (netns) aware.
17 #define KMSG_COMPONENT "IPVS"
18 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22 #include <linux/kernel.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter_ipv4.h>
25 #include <linux/udp.h>
27 #include <net/ip_vs.h>
29 #include <net/ip6_checksum.h>
32 udp_conn_schedule(int af
, struct sk_buff
*skb
, struct ip_vs_proto_data
*pd
,
33 int *verdict
, struct ip_vs_conn
**cpp
,
34 struct ip_vs_iphdr
*iph
)
37 struct netns_ipvs
*ipvs
;
38 struct ip_vs_service
*svc
;
39 struct udphdr _udph
, *uh
;
40 __be16 _ports
[2], *ports
= NULL
;
42 if (likely(!ip_vs_iph_icmp(iph
))) {
43 /* IPv6 fragments, only first fragment will hit this */
44 uh
= skb_header_pointer(skb
, iph
->len
, sizeof(_udph
), &_udph
);
48 ports
= skb_header_pointer(
49 skb
, iph
->len
, sizeof(_ports
), &_ports
);
60 if (likely(!ip_vs_iph_inverse(iph
)))
61 svc
= ip_vs_service_find(ipvs
, af
, skb
->mark
, iph
->protocol
,
62 &iph
->daddr
, ports
[1]);
64 svc
= ip_vs_service_find(ipvs
, af
, skb
->mark
, iph
->protocol
,
65 &iph
->saddr
, ports
[0]);
70 if (ip_vs_todrop(net_ipvs(net
))) {
72 * It seems that we are very loaded.
73 * We have to drop this packet :(
81 * Let the virtual server select a real server for the
82 * incoming connection, and create a connection entry.
84 *cpp
= ip_vs_schedule(svc
, skb
, pd
, &ignored
, iph
);
85 if (!*cpp
&& ignored
<= 0) {
87 *verdict
= ip_vs_leave(svc
, skb
, pd
, iph
);
101 udp_fast_csum_update(int af
, struct udphdr
*uhdr
,
102 const union nf_inet_addr
*oldip
,
103 const union nf_inet_addr
*newip
,
104 __be16 oldport
, __be16 newport
)
106 #ifdef CONFIG_IP_VS_IPV6
109 csum_fold(ip_vs_check_diff16(oldip
->ip6
, newip
->ip6
,
110 ip_vs_check_diff2(oldport
, newport
,
111 ~csum_unfold(uhdr
->check
))));
115 csum_fold(ip_vs_check_diff4(oldip
->ip
, newip
->ip
,
116 ip_vs_check_diff2(oldport
, newport
,
117 ~csum_unfold(uhdr
->check
))));
119 uhdr
->check
= CSUM_MANGLED_0
;
123 udp_partial_csum_update(int af
, struct udphdr
*uhdr
,
124 const union nf_inet_addr
*oldip
,
125 const union nf_inet_addr
*newip
,
126 __be16 oldlen
, __be16 newlen
)
128 #ifdef CONFIG_IP_VS_IPV6
131 ~csum_fold(ip_vs_check_diff16(oldip
->ip6
, newip
->ip6
,
132 ip_vs_check_diff2(oldlen
, newlen
,
133 csum_unfold(uhdr
->check
))));
137 ~csum_fold(ip_vs_check_diff4(oldip
->ip
, newip
->ip
,
138 ip_vs_check_diff2(oldlen
, newlen
,
139 csum_unfold(uhdr
->check
))));
144 udp_snat_handler(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
145 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
)
148 unsigned int udphoff
= iph
->len
;
150 int payload_csum
= 0;
152 #ifdef CONFIG_IP_VS_IPV6
153 if (cp
->af
== AF_INET6
&& iph
->fragoffs
)
156 oldlen
= skb
->len
- udphoff
;
158 /* csum_check requires unshared skb */
159 if (!skb_make_writable(skb
, udphoff
+sizeof(*udph
)))
162 if (unlikely(cp
->app
!= NULL
)) {
165 /* Some checks before mangling */
166 if (pp
->csum_check
&& !pp
->csum_check(cp
->af
, skb
, pp
))
170 * Call application helper if needed
172 if (!(ret
= ip_vs_app_pkt_out(cp
, skb
)))
174 /* ret=2: csum update is needed after payload mangling */
176 oldlen
= skb
->len
- udphoff
;
181 udph
= (void *)skb_network_header(skb
) + udphoff
;
182 udph
->source
= cp
->vport
;
185 * Adjust UDP checksums
187 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
188 udp_partial_csum_update(cp
->af
, udph
, &cp
->daddr
, &cp
->vaddr
,
190 htons(skb
->len
- udphoff
));
191 } else if (!payload_csum
&& (udph
->check
!= 0)) {
192 /* Only port and addr are changed, do fast csum update */
193 udp_fast_csum_update(cp
->af
, udph
, &cp
->daddr
, &cp
->vaddr
,
194 cp
->dport
, cp
->vport
);
195 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
196 skb
->ip_summed
= (cp
->app
&& pp
->csum_check
) ?
197 CHECKSUM_UNNECESSARY
: CHECKSUM_NONE
;
199 /* full checksum calculation */
201 skb
->csum
= skb_checksum(skb
, udphoff
, skb
->len
- udphoff
, 0);
202 #ifdef CONFIG_IP_VS_IPV6
203 if (cp
->af
== AF_INET6
)
204 udph
->check
= csum_ipv6_magic(&cp
->vaddr
.in6
,
207 cp
->protocol
, skb
->csum
);
210 udph
->check
= csum_tcpudp_magic(cp
->vaddr
.ip
,
215 if (udph
->check
== 0)
216 udph
->check
= CSUM_MANGLED_0
;
217 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
218 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
219 pp
->name
, udph
->check
,
220 (char*)&(udph
->check
) - (char*)udph
);
227 udp_dnat_handler(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
228 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
)
231 unsigned int udphoff
= iph
->len
;
233 int payload_csum
= 0;
235 #ifdef CONFIG_IP_VS_IPV6
236 if (cp
->af
== AF_INET6
&& iph
->fragoffs
)
239 oldlen
= skb
->len
- udphoff
;
241 /* csum_check requires unshared skb */
242 if (!skb_make_writable(skb
, udphoff
+sizeof(*udph
)))
245 if (unlikely(cp
->app
!= NULL
)) {
248 /* Some checks before mangling */
249 if (pp
->csum_check
&& !pp
->csum_check(cp
->af
, skb
, pp
))
253 * Attempt ip_vs_app call.
254 * It will fix ip_vs_conn
256 if (!(ret
= ip_vs_app_pkt_in(cp
, skb
)))
258 /* ret=2: csum update is needed after payload mangling */
260 oldlen
= skb
->len
- udphoff
;
265 udph
= (void *)skb_network_header(skb
) + udphoff
;
266 udph
->dest
= cp
->dport
;
269 * Adjust UDP checksums
271 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
272 udp_partial_csum_update(cp
->af
, udph
, &cp
->vaddr
, &cp
->daddr
,
274 htons(skb
->len
- udphoff
));
275 } else if (!payload_csum
&& (udph
->check
!= 0)) {
276 /* Only port and addr are changed, do fast csum update */
277 udp_fast_csum_update(cp
->af
, udph
, &cp
->vaddr
, &cp
->daddr
,
278 cp
->vport
, cp
->dport
);
279 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
280 skb
->ip_summed
= (cp
->app
&& pp
->csum_check
) ?
281 CHECKSUM_UNNECESSARY
: CHECKSUM_NONE
;
283 /* full checksum calculation */
285 skb
->csum
= skb_checksum(skb
, udphoff
, skb
->len
- udphoff
, 0);
286 #ifdef CONFIG_IP_VS_IPV6
287 if (cp
->af
== AF_INET6
)
288 udph
->check
= csum_ipv6_magic(&cp
->caddr
.in6
,
291 cp
->protocol
, skb
->csum
);
294 udph
->check
= csum_tcpudp_magic(cp
->caddr
.ip
,
299 if (udph
->check
== 0)
300 udph
->check
= CSUM_MANGLED_0
;
301 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
308 udp_csum_check(int af
, struct sk_buff
*skb
, struct ip_vs_protocol
*pp
)
310 struct udphdr _udph
, *uh
;
311 unsigned int udphoff
;
313 #ifdef CONFIG_IP_VS_IPV6
315 udphoff
= sizeof(struct ipv6hdr
);
318 udphoff
= ip_hdrlen(skb
);
320 uh
= skb_header_pointer(skb
, udphoff
, sizeof(_udph
), &_udph
);
324 if (uh
->check
!= 0) {
325 switch (skb
->ip_summed
) {
327 skb
->csum
= skb_checksum(skb
, udphoff
,
328 skb
->len
- udphoff
, 0);
329 case CHECKSUM_COMPLETE
:
330 #ifdef CONFIG_IP_VS_IPV6
331 if (af
== AF_INET6
) {
332 if (csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
333 &ipv6_hdr(skb
)->daddr
,
335 ipv6_hdr(skb
)->nexthdr
,
337 IP_VS_DBG_RL_PKT(0, af
, pp
, skb
, 0,
338 "Failed checksum for");
343 if (csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
346 ip_hdr(skb
)->protocol
,
348 IP_VS_DBG_RL_PKT(0, af
, pp
, skb
, 0,
349 "Failed checksum for");
354 /* No need to checksum. */
361 static inline __u16
udp_app_hashkey(__be16 port
)
363 return (((__force u16
)port
>> UDP_APP_TAB_BITS
) ^ (__force u16
)port
)
368 static int udp_register_app(struct net
*net
, struct ip_vs_app
*inc
)
372 __be16 port
= inc
->port
;
374 struct netns_ipvs
*ipvs
= net_ipvs(net
);
375 struct ip_vs_proto_data
*pd
= ip_vs_proto_data_get(ipvs
, IPPROTO_UDP
);
377 hash
= udp_app_hashkey(port
);
379 list_for_each_entry(i
, &ipvs
->udp_apps
[hash
], p_list
) {
380 if (i
->port
== port
) {
385 list_add_rcu(&inc
->p_list
, &ipvs
->udp_apps
[hash
]);
386 atomic_inc(&pd
->appcnt
);
394 udp_unregister_app(struct net
*net
, struct ip_vs_app
*inc
)
396 struct ip_vs_proto_data
*pd
= ip_vs_proto_data_get(net_ipvs(net
), IPPROTO_UDP
);
398 atomic_dec(&pd
->appcnt
);
399 list_del_rcu(&inc
->p_list
);
403 static int udp_app_conn_bind(struct ip_vs_conn
*cp
)
405 struct netns_ipvs
*ipvs
= cp
->ipvs
;
407 struct ip_vs_app
*inc
;
410 /* Default binding: bind app only for NAT */
411 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)
414 /* Lookup application incarnations and bind the right one */
415 hash
= udp_app_hashkey(cp
->vport
);
418 list_for_each_entry_rcu(inc
, &ipvs
->udp_apps
[hash
], p_list
) {
419 if (inc
->port
== cp
->vport
) {
420 if (unlikely(!ip_vs_app_inc_get(inc
)))
424 IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->"
425 "%s:%u to app %s on port %u\n",
427 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
),
429 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
),
431 inc
->name
, ntohs(inc
->port
));
435 result
= inc
->init_conn(inc
, cp
);
446 static const int udp_timeouts
[IP_VS_UDP_S_LAST
+1] = {
447 [IP_VS_UDP_S_NORMAL
] = 5*60*HZ
,
448 [IP_VS_UDP_S_LAST
] = 2*HZ
,
451 static const char *const udp_state_name_table
[IP_VS_UDP_S_LAST
+1] = {
452 [IP_VS_UDP_S_NORMAL
] = "UDP",
453 [IP_VS_UDP_S_LAST
] = "BUG!",
456 static const char * udp_state_name(int state
)
458 if (state
>= IP_VS_UDP_S_LAST
)
460 return udp_state_name_table
[state
] ? udp_state_name_table
[state
] : "?";
464 udp_state_transition(struct ip_vs_conn
*cp
, int direction
,
465 const struct sk_buff
*skb
,
466 struct ip_vs_proto_data
*pd
)
469 pr_err("UDP no ns data\n");
473 cp
->timeout
= pd
->timeout_table
[IP_VS_UDP_S_NORMAL
];
476 static int __udp_init(struct net
*net
, struct ip_vs_proto_data
*pd
)
478 struct netns_ipvs
*ipvs
= net_ipvs(net
);
480 ip_vs_init_hash_table(ipvs
->udp_apps
, UDP_APP_TAB_SIZE
);
481 pd
->timeout_table
= ip_vs_create_timeout_table((int *)udp_timeouts
,
482 sizeof(udp_timeouts
));
483 if (!pd
->timeout_table
)
488 static void __udp_exit(struct net
*net
, struct ip_vs_proto_data
*pd
)
490 kfree(pd
->timeout_table
);
494 struct ip_vs_protocol ip_vs_protocol_udp
= {
496 .protocol
= IPPROTO_UDP
,
497 .num_states
= IP_VS_UDP_S_LAST
,
501 .init_netns
= __udp_init
,
502 .exit_netns
= __udp_exit
,
503 .conn_schedule
= udp_conn_schedule
,
504 .conn_in_get
= ip_vs_conn_in_get_proto
,
505 .conn_out_get
= ip_vs_conn_out_get_proto
,
506 .snat_handler
= udp_snat_handler
,
507 .dnat_handler
= udp_dnat_handler
,
508 .csum_check
= udp_csum_check
,
509 .state_transition
= udp_state_transition
,
510 .state_name
= udp_state_name
,
511 .register_app
= udp_register_app
,
512 .unregister_app
= udp_unregister_app
,
513 .app_conn_bind
= udp_app_conn_bind
,
514 .debug_packet
= ip_vs_tcpudp_debug_packet
,
515 .timeout_change
= NULL
,