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 ip_vs_service
*svc
;
38 struct udphdr _udph
, *uh
;
40 /* IPv6 fragments, only first fragment will hit this */
41 uh
= skb_header_pointer(skb
, iph
->len
, sizeof(_udph
), &_udph
);
48 svc
= ip_vs_service_find(net
, af
, skb
->mark
, iph
->protocol
,
49 &iph
->daddr
, uh
->dest
);
53 if (ip_vs_todrop(net_ipvs(net
))) {
55 * It seems that we are very loaded.
56 * We have to drop this packet :(
64 * Let the virtual server select a real server for the
65 * incoming connection, and create a connection entry.
67 *cpp
= ip_vs_schedule(svc
, skb
, pd
, &ignored
, iph
);
68 if (!*cpp
&& ignored
<= 0) {
70 *verdict
= ip_vs_leave(svc
, skb
, pd
, iph
);
84 udp_fast_csum_update(int af
, struct udphdr
*uhdr
,
85 const union nf_inet_addr
*oldip
,
86 const union nf_inet_addr
*newip
,
87 __be16 oldport
, __be16 newport
)
89 #ifdef CONFIG_IP_VS_IPV6
92 csum_fold(ip_vs_check_diff16(oldip
->ip6
, newip
->ip6
,
93 ip_vs_check_diff2(oldport
, newport
,
94 ~csum_unfold(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
))));
102 uhdr
->check
= CSUM_MANGLED_0
;
106 udp_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
)
111 #ifdef CONFIG_IP_VS_IPV6
114 ~csum_fold(ip_vs_check_diff16(oldip
->ip6
, newip
->ip6
,
115 ip_vs_check_diff2(oldlen
, newlen
,
116 csum_unfold(uhdr
->check
))));
120 ~csum_fold(ip_vs_check_diff4(oldip
->ip
, newip
->ip
,
121 ip_vs_check_diff2(oldlen
, newlen
,
122 csum_unfold(uhdr
->check
))));
127 udp_snat_handler(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
128 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
)
131 unsigned int udphoff
= iph
->len
;
133 int payload_csum
= 0;
135 #ifdef CONFIG_IP_VS_IPV6
136 if (cp
->af
== AF_INET6
&& iph
->fragoffs
)
139 oldlen
= skb
->len
- udphoff
;
141 /* csum_check requires unshared skb */
142 if (!skb_make_writable(skb
, udphoff
+sizeof(*udph
)))
145 if (unlikely(cp
->app
!= NULL
)) {
148 /* Some checks before mangling */
149 if (pp
->csum_check
&& !pp
->csum_check(cp
->af
, skb
, pp
))
153 * Call application helper if needed
155 if (!(ret
= ip_vs_app_pkt_out(cp
, skb
)))
157 /* ret=2: csum update is needed after payload mangling */
159 oldlen
= skb
->len
- udphoff
;
164 udph
= (void *)skb_network_header(skb
) + udphoff
;
165 udph
->source
= cp
->vport
;
168 * Adjust UDP checksums
170 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
171 udp_partial_csum_update(cp
->af
, udph
, &cp
->daddr
, &cp
->vaddr
,
173 htons(skb
->len
- udphoff
));
174 } else if (!payload_csum
&& (udph
->check
!= 0)) {
175 /* Only port and addr are changed, do fast csum update */
176 udp_fast_csum_update(cp
->af
, udph
, &cp
->daddr
, &cp
->vaddr
,
177 cp
->dport
, cp
->vport
);
178 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
179 skb
->ip_summed
= (cp
->app
&& pp
->csum_check
) ?
180 CHECKSUM_UNNECESSARY
: CHECKSUM_NONE
;
182 /* full checksum calculation */
184 skb
->csum
= skb_checksum(skb
, udphoff
, skb
->len
- udphoff
, 0);
185 #ifdef CONFIG_IP_VS_IPV6
186 if (cp
->af
== AF_INET6
)
187 udph
->check
= csum_ipv6_magic(&cp
->vaddr
.in6
,
190 cp
->protocol
, skb
->csum
);
193 udph
->check
= csum_tcpudp_magic(cp
->vaddr
.ip
,
198 if (udph
->check
== 0)
199 udph
->check
= CSUM_MANGLED_0
;
200 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
201 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
202 pp
->name
, udph
->check
,
203 (char*)&(udph
->check
) - (char*)udph
);
210 udp_dnat_handler(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
211 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
)
214 unsigned int udphoff
= iph
->len
;
216 int payload_csum
= 0;
218 #ifdef CONFIG_IP_VS_IPV6
219 if (cp
->af
== AF_INET6
&& iph
->fragoffs
)
222 oldlen
= skb
->len
- udphoff
;
224 /* csum_check requires unshared skb */
225 if (!skb_make_writable(skb
, udphoff
+sizeof(*udph
)))
228 if (unlikely(cp
->app
!= NULL
)) {
231 /* Some checks before mangling */
232 if (pp
->csum_check
&& !pp
->csum_check(cp
->af
, skb
, pp
))
236 * Attempt ip_vs_app call.
237 * It will fix ip_vs_conn
239 if (!(ret
= ip_vs_app_pkt_in(cp
, skb
)))
241 /* ret=2: csum update is needed after payload mangling */
243 oldlen
= skb
->len
- udphoff
;
248 udph
= (void *)skb_network_header(skb
) + udphoff
;
249 udph
->dest
= cp
->dport
;
252 * Adjust UDP checksums
254 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
255 udp_partial_csum_update(cp
->af
, udph
, &cp
->vaddr
, &cp
->daddr
,
257 htons(skb
->len
- udphoff
));
258 } else if (!payload_csum
&& (udph
->check
!= 0)) {
259 /* Only port and addr are changed, do fast csum update */
260 udp_fast_csum_update(cp
->af
, udph
, &cp
->vaddr
, &cp
->daddr
,
261 cp
->vport
, cp
->dport
);
262 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
263 skb
->ip_summed
= (cp
->app
&& pp
->csum_check
) ?
264 CHECKSUM_UNNECESSARY
: CHECKSUM_NONE
;
266 /* full checksum calculation */
268 skb
->csum
= skb_checksum(skb
, udphoff
, skb
->len
- udphoff
, 0);
269 #ifdef CONFIG_IP_VS_IPV6
270 if (cp
->af
== AF_INET6
)
271 udph
->check
= csum_ipv6_magic(&cp
->caddr
.in6
,
274 cp
->protocol
, skb
->csum
);
277 udph
->check
= csum_tcpudp_magic(cp
->caddr
.ip
,
282 if (udph
->check
== 0)
283 udph
->check
= CSUM_MANGLED_0
;
284 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
291 udp_csum_check(int af
, struct sk_buff
*skb
, struct ip_vs_protocol
*pp
)
293 struct udphdr _udph
, *uh
;
294 unsigned int udphoff
;
296 #ifdef CONFIG_IP_VS_IPV6
298 udphoff
= sizeof(struct ipv6hdr
);
301 udphoff
= ip_hdrlen(skb
);
303 uh
= skb_header_pointer(skb
, udphoff
, sizeof(_udph
), &_udph
);
307 if (uh
->check
!= 0) {
308 switch (skb
->ip_summed
) {
310 skb
->csum
= skb_checksum(skb
, udphoff
,
311 skb
->len
- udphoff
, 0);
312 case CHECKSUM_COMPLETE
:
313 #ifdef CONFIG_IP_VS_IPV6
314 if (af
== AF_INET6
) {
315 if (csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
316 &ipv6_hdr(skb
)->daddr
,
318 ipv6_hdr(skb
)->nexthdr
,
320 IP_VS_DBG_RL_PKT(0, af
, pp
, skb
, 0,
321 "Failed checksum for");
326 if (csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
329 ip_hdr(skb
)->protocol
,
331 IP_VS_DBG_RL_PKT(0, af
, pp
, skb
, 0,
332 "Failed checksum for");
337 /* No need to checksum. */
344 static inline __u16
udp_app_hashkey(__be16 port
)
346 return (((__force u16
)port
>> UDP_APP_TAB_BITS
) ^ (__force u16
)port
)
351 static int udp_register_app(struct net
*net
, struct ip_vs_app
*inc
)
355 __be16 port
= inc
->port
;
357 struct netns_ipvs
*ipvs
= net_ipvs(net
);
358 struct ip_vs_proto_data
*pd
= ip_vs_proto_data_get(net
, IPPROTO_UDP
);
360 hash
= udp_app_hashkey(port
);
362 list_for_each_entry(i
, &ipvs
->udp_apps
[hash
], p_list
) {
363 if (i
->port
== port
) {
368 list_add_rcu(&inc
->p_list
, &ipvs
->udp_apps
[hash
]);
369 atomic_inc(&pd
->appcnt
);
377 udp_unregister_app(struct net
*net
, struct ip_vs_app
*inc
)
379 struct ip_vs_proto_data
*pd
= ip_vs_proto_data_get(net
, IPPROTO_UDP
);
381 atomic_dec(&pd
->appcnt
);
382 list_del_rcu(&inc
->p_list
);
386 static int udp_app_conn_bind(struct ip_vs_conn
*cp
)
388 struct netns_ipvs
*ipvs
= net_ipvs(ip_vs_conn_net(cp
));
390 struct ip_vs_app
*inc
;
393 /* Default binding: bind app only for NAT */
394 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)
397 /* Lookup application incarnations and bind the right one */
398 hash
= udp_app_hashkey(cp
->vport
);
401 list_for_each_entry_rcu(inc
, &ipvs
->udp_apps
[hash
], p_list
) {
402 if (inc
->port
== cp
->vport
) {
403 if (unlikely(!ip_vs_app_inc_get(inc
)))
407 IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->"
408 "%s:%u to app %s on port %u\n",
410 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
),
412 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
),
414 inc
->name
, ntohs(inc
->port
));
418 result
= inc
->init_conn(inc
, cp
);
429 static const int udp_timeouts
[IP_VS_UDP_S_LAST
+1] = {
430 [IP_VS_UDP_S_NORMAL
] = 5*60*HZ
,
431 [IP_VS_UDP_S_LAST
] = 2*HZ
,
434 static const char *const udp_state_name_table
[IP_VS_UDP_S_LAST
+1] = {
435 [IP_VS_UDP_S_NORMAL
] = "UDP",
436 [IP_VS_UDP_S_LAST
] = "BUG!",
439 static const char * udp_state_name(int state
)
441 if (state
>= IP_VS_UDP_S_LAST
)
443 return udp_state_name_table
[state
] ? udp_state_name_table
[state
] : "?";
447 udp_state_transition(struct ip_vs_conn
*cp
, int direction
,
448 const struct sk_buff
*skb
,
449 struct ip_vs_proto_data
*pd
)
452 pr_err("UDP no ns data\n");
456 cp
->timeout
= pd
->timeout_table
[IP_VS_UDP_S_NORMAL
];
459 static int __udp_init(struct net
*net
, struct ip_vs_proto_data
*pd
)
461 struct netns_ipvs
*ipvs
= net_ipvs(net
);
463 ip_vs_init_hash_table(ipvs
->udp_apps
, UDP_APP_TAB_SIZE
);
464 pd
->timeout_table
= ip_vs_create_timeout_table((int *)udp_timeouts
,
465 sizeof(udp_timeouts
));
466 if (!pd
->timeout_table
)
471 static void __udp_exit(struct net
*net
, struct ip_vs_proto_data
*pd
)
473 kfree(pd
->timeout_table
);
477 struct ip_vs_protocol ip_vs_protocol_udp
= {
479 .protocol
= IPPROTO_UDP
,
480 .num_states
= IP_VS_UDP_S_LAST
,
484 .init_netns
= __udp_init
,
485 .exit_netns
= __udp_exit
,
486 .conn_schedule
= udp_conn_schedule
,
487 .conn_in_get
= ip_vs_conn_in_get_proto
,
488 .conn_out_get
= ip_vs_conn_out_get_proto
,
489 .snat_handler
= udp_snat_handler
,
490 .dnat_handler
= udp_dnat_handler
,
491 .csum_check
= udp_csum_check
,
492 .state_transition
= udp_state_transition
,
493 .state_name
= udp_state_name
,
494 .register_app
= udp_register_app
,
495 .unregister_app
= udp_unregister_app
,
496 .app_conn_bind
= udp_app_conn_bind
,
497 .debug_packet
= ip_vs_tcpudp_debug_packet
,
498 .timeout_change
= NULL
,