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
)
36 struct ip_vs_service
*svc
;
37 struct udphdr _udph
, *uh
;
38 struct ip_vs_iphdr iph
;
40 ip_vs_fill_iphdr(af
, skb_network_header(skb
), &iph
);
42 uh
= skb_header_pointer(skb
, iph
.len
, sizeof(_udph
), &_udph
);
48 svc
= ip_vs_service_get(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 :(
58 ip_vs_service_put(svc
);
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
);
68 if (!*cpp
&& ignored
<= 0) {
70 *verdict
= ip_vs_leave(svc
, skb
, pd
);
72 ip_vs_service_put(svc
);
77 ip_vs_service_put(svc
);
85 udp_fast_csum_update(int af
, struct udphdr
*uhdr
,
86 const union nf_inet_addr
*oldip
,
87 const union nf_inet_addr
*newip
,
88 __be16 oldport
, __be16 newport
)
90 #ifdef CONFIG_IP_VS_IPV6
93 csum_fold(ip_vs_check_diff16(oldip
->ip6
, newip
->ip6
,
94 ip_vs_check_diff2(oldport
, newport
,
95 ~csum_unfold(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
))));
103 uhdr
->check
= CSUM_MANGLED_0
;
107 udp_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
)
112 #ifdef CONFIG_IP_VS_IPV6
115 ~csum_fold(ip_vs_check_diff16(oldip
->ip6
, newip
->ip6
,
116 ip_vs_check_diff2(oldlen
, newlen
,
117 csum_unfold(uhdr
->check
))));
121 ~csum_fold(ip_vs_check_diff4(oldip
->ip
, newip
->ip
,
122 ip_vs_check_diff2(oldlen
, newlen
,
123 csum_unfold(uhdr
->check
))));
128 udp_snat_handler(struct sk_buff
*skb
,
129 struct ip_vs_protocol
*pp
, struct ip_vs_conn
*cp
)
132 unsigned int udphoff
;
134 int payload_csum
= 0;
136 #ifdef CONFIG_IP_VS_IPV6
137 if (cp
->af
== AF_INET6
)
138 udphoff
= sizeof(struct ipv6hdr
);
141 udphoff
= ip_hdrlen(skb
);
142 oldlen
= skb
->len
- udphoff
;
144 /* csum_check requires unshared skb */
145 if (!skb_make_writable(skb
, udphoff
+sizeof(*udph
)))
148 if (unlikely(cp
->app
!= NULL
)) {
151 /* Some checks before mangling */
152 if (pp
->csum_check
&& !pp
->csum_check(cp
->af
, skb
, pp
))
156 * Call application helper if needed
158 if (!(ret
= ip_vs_app_pkt_out(cp
, skb
)))
160 /* ret=2: csum update is needed after payload mangling */
162 oldlen
= skb
->len
- udphoff
;
167 udph
= (void *)skb_network_header(skb
) + udphoff
;
168 udph
->source
= cp
->vport
;
171 * Adjust UDP checksums
173 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
174 udp_partial_csum_update(cp
->af
, udph
, &cp
->daddr
, &cp
->vaddr
,
176 htons(skb
->len
- udphoff
));
177 } else if (!payload_csum
&& (udph
->check
!= 0)) {
178 /* Only port and addr are changed, do fast csum update */
179 udp_fast_csum_update(cp
->af
, udph
, &cp
->daddr
, &cp
->vaddr
,
180 cp
->dport
, cp
->vport
);
181 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
182 skb
->ip_summed
= (cp
->app
&& pp
->csum_check
) ?
183 CHECKSUM_UNNECESSARY
: CHECKSUM_NONE
;
185 /* full checksum calculation */
187 skb
->csum
= skb_checksum(skb
, udphoff
, skb
->len
- udphoff
, 0);
188 #ifdef CONFIG_IP_VS_IPV6
189 if (cp
->af
== AF_INET6
)
190 udph
->check
= csum_ipv6_magic(&cp
->vaddr
.in6
,
193 cp
->protocol
, skb
->csum
);
196 udph
->check
= csum_tcpudp_magic(cp
->vaddr
.ip
,
201 if (udph
->check
== 0)
202 udph
->check
= CSUM_MANGLED_0
;
203 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
204 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
205 pp
->name
, udph
->check
,
206 (char*)&(udph
->check
) - (char*)udph
);
213 udp_dnat_handler(struct sk_buff
*skb
,
214 struct ip_vs_protocol
*pp
, struct ip_vs_conn
*cp
)
217 unsigned int udphoff
;
219 int payload_csum
= 0;
221 #ifdef CONFIG_IP_VS_IPV6
222 if (cp
->af
== AF_INET6
)
223 udphoff
= sizeof(struct ipv6hdr
);
226 udphoff
= ip_hdrlen(skb
);
227 oldlen
= skb
->len
- udphoff
;
229 /* csum_check requires unshared skb */
230 if (!skb_make_writable(skb
, udphoff
+sizeof(*udph
)))
233 if (unlikely(cp
->app
!= NULL
)) {
236 /* Some checks before mangling */
237 if (pp
->csum_check
&& !pp
->csum_check(cp
->af
, skb
, pp
))
241 * Attempt ip_vs_app call.
242 * It will fix ip_vs_conn
244 if (!(ret
= ip_vs_app_pkt_in(cp
, skb
)))
246 /* ret=2: csum update is needed after payload mangling */
248 oldlen
= skb
->len
- udphoff
;
253 udph
= (void *)skb_network_header(skb
) + udphoff
;
254 udph
->dest
= cp
->dport
;
257 * Adjust UDP checksums
259 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
260 udp_partial_csum_update(cp
->af
, udph
, &cp
->vaddr
, &cp
->daddr
,
262 htons(skb
->len
- udphoff
));
263 } else if (!payload_csum
&& (udph
->check
!= 0)) {
264 /* Only port and addr are changed, do fast csum update */
265 udp_fast_csum_update(cp
->af
, udph
, &cp
->vaddr
, &cp
->daddr
,
266 cp
->vport
, cp
->dport
);
267 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
268 skb
->ip_summed
= (cp
->app
&& pp
->csum_check
) ?
269 CHECKSUM_UNNECESSARY
: CHECKSUM_NONE
;
271 /* full checksum calculation */
273 skb
->csum
= skb_checksum(skb
, udphoff
, skb
->len
- udphoff
, 0);
274 #ifdef CONFIG_IP_VS_IPV6
275 if (cp
->af
== AF_INET6
)
276 udph
->check
= csum_ipv6_magic(&cp
->caddr
.in6
,
279 cp
->protocol
, skb
->csum
);
282 udph
->check
= csum_tcpudp_magic(cp
->caddr
.ip
,
287 if (udph
->check
== 0)
288 udph
->check
= CSUM_MANGLED_0
;
289 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
296 udp_csum_check(int af
, struct sk_buff
*skb
, struct ip_vs_protocol
*pp
)
298 struct udphdr _udph
, *uh
;
299 unsigned int udphoff
;
301 #ifdef CONFIG_IP_VS_IPV6
303 udphoff
= sizeof(struct ipv6hdr
);
306 udphoff
= ip_hdrlen(skb
);
308 uh
= skb_header_pointer(skb
, udphoff
, sizeof(_udph
), &_udph
);
312 if (uh
->check
!= 0) {
313 switch (skb
->ip_summed
) {
315 skb
->csum
= skb_checksum(skb
, udphoff
,
316 skb
->len
- udphoff
, 0);
317 case CHECKSUM_COMPLETE
:
318 #ifdef CONFIG_IP_VS_IPV6
319 if (af
== AF_INET6
) {
320 if (csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
321 &ipv6_hdr(skb
)->daddr
,
323 ipv6_hdr(skb
)->nexthdr
,
325 IP_VS_DBG_RL_PKT(0, af
, pp
, skb
, 0,
326 "Failed checksum for");
331 if (csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
334 ip_hdr(skb
)->protocol
,
336 IP_VS_DBG_RL_PKT(0, af
, pp
, skb
, 0,
337 "Failed checksum for");
342 /* No need to checksum. */
349 static inline __u16
udp_app_hashkey(__be16 port
)
351 return (((__force u16
)port
>> UDP_APP_TAB_BITS
) ^ (__force u16
)port
)
356 static int udp_register_app(struct net
*net
, struct ip_vs_app
*inc
)
360 __be16 port
= inc
->port
;
362 struct netns_ipvs
*ipvs
= net_ipvs(net
);
363 struct ip_vs_proto_data
*pd
= ip_vs_proto_data_get(net
, IPPROTO_UDP
);
365 hash
= udp_app_hashkey(port
);
368 spin_lock_bh(&ipvs
->udp_app_lock
);
369 list_for_each_entry(i
, &ipvs
->udp_apps
[hash
], p_list
) {
370 if (i
->port
== port
) {
375 list_add(&inc
->p_list
, &ipvs
->udp_apps
[hash
]);
376 atomic_inc(&pd
->appcnt
);
379 spin_unlock_bh(&ipvs
->udp_app_lock
);
385 udp_unregister_app(struct net
*net
, struct ip_vs_app
*inc
)
387 struct ip_vs_proto_data
*pd
= ip_vs_proto_data_get(net
, IPPROTO_UDP
);
388 struct netns_ipvs
*ipvs
= net_ipvs(net
);
390 spin_lock_bh(&ipvs
->udp_app_lock
);
391 atomic_dec(&pd
->appcnt
);
392 list_del(&inc
->p_list
);
393 spin_unlock_bh(&ipvs
->udp_app_lock
);
397 static int udp_app_conn_bind(struct ip_vs_conn
*cp
)
399 struct netns_ipvs
*ipvs
= net_ipvs(ip_vs_conn_net(cp
));
401 struct ip_vs_app
*inc
;
404 /* Default binding: bind app only for NAT */
405 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)
408 /* Lookup application incarnations and bind the right one */
409 hash
= udp_app_hashkey(cp
->vport
);
411 spin_lock(&ipvs
->udp_app_lock
);
412 list_for_each_entry(inc
, &ipvs
->udp_apps
[hash
], p_list
) {
413 if (inc
->port
== cp
->vport
) {
414 if (unlikely(!ip_vs_app_inc_get(inc
)))
416 spin_unlock(&ipvs
->udp_app_lock
);
418 IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->"
419 "%s:%u to app %s on port %u\n",
421 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
),
423 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
),
425 inc
->name
, ntohs(inc
->port
));
429 result
= inc
->init_conn(inc
, cp
);
433 spin_unlock(&ipvs
->udp_app_lock
);
440 static const int udp_timeouts
[IP_VS_UDP_S_LAST
+1] = {
441 [IP_VS_UDP_S_NORMAL
] = 5*60*HZ
,
442 [IP_VS_UDP_S_LAST
] = 2*HZ
,
445 static const char *const udp_state_name_table
[IP_VS_UDP_S_LAST
+1] = {
446 [IP_VS_UDP_S_NORMAL
] = "UDP",
447 [IP_VS_UDP_S_LAST
] = "BUG!",
450 static const char * udp_state_name(int state
)
452 if (state
>= IP_VS_UDP_S_LAST
)
454 return udp_state_name_table
[state
] ? udp_state_name_table
[state
] : "?";
458 udp_state_transition(struct ip_vs_conn
*cp
, int direction
,
459 const struct sk_buff
*skb
,
460 struct ip_vs_proto_data
*pd
)
463 pr_err("UDP no ns data\n");
467 cp
->timeout
= pd
->timeout_table
[IP_VS_UDP_S_NORMAL
];
471 static void __udp_init(struct net
*net
, struct ip_vs_proto_data
*pd
)
473 struct netns_ipvs
*ipvs
= net_ipvs(net
);
475 ip_vs_init_hash_table(ipvs
->udp_apps
, UDP_APP_TAB_SIZE
);
476 spin_lock_init(&ipvs
->udp_app_lock
);
477 pd
->timeout_table
= ip_vs_create_timeout_table((int *)udp_timeouts
,
478 sizeof(udp_timeouts
));
481 static void __udp_exit(struct net
*net
, struct ip_vs_proto_data
*pd
)
483 kfree(pd
->timeout_table
);
487 struct ip_vs_protocol ip_vs_protocol_udp
= {
489 .protocol
= IPPROTO_UDP
,
490 .num_states
= IP_VS_UDP_S_LAST
,
494 .init_netns
= __udp_init
,
495 .exit_netns
= __udp_exit
,
496 .conn_schedule
= udp_conn_schedule
,
497 .conn_in_get
= ip_vs_conn_in_get_proto
,
498 .conn_out_get
= ip_vs_conn_out_get_proto
,
499 .snat_handler
= udp_snat_handler
,
500 .dnat_handler
= udp_dnat_handler
,
501 .csum_check
= udp_csum_check
,
502 .state_transition
= udp_state_transition
,
503 .state_name
= udp_state_name
,
504 .register_app
= udp_register_app
,
505 .unregister_app
= udp_unregister_app
,
506 .app_conn_bind
= udp_app_conn_bind
,
507 .debug_packet
= ip_vs_tcpudp_debug_packet
,
508 .timeout_change
= NULL
,