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(struct netns_ipvs
*ipvs
, int af
, struct sk_buff
*skb
,
33 struct ip_vs_proto_data
*pd
,
34 int *verdict
, struct ip_vs_conn
**cpp
,
35 struct ip_vs_iphdr
*iph
)
37 struct ip_vs_service
*svc
;
38 struct udphdr _udph
, *uh
;
39 __be16 _ports
[2], *ports
= NULL
;
41 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
);
47 ports
= skb_header_pointer(
48 skb
, iph
->len
, sizeof(_ports
), &_ports
);
57 if (likely(!ip_vs_iph_inverse(iph
)))
58 svc
= ip_vs_service_find(ipvs
, af
, skb
->mark
, iph
->protocol
,
59 &iph
->daddr
, ports
[1]);
61 svc
= ip_vs_service_find(ipvs
, af
, skb
->mark
, iph
->protocol
,
62 &iph
->saddr
, ports
[0]);
67 if (ip_vs_todrop(ipvs
)) {
69 * It seems that we are very loaded.
70 * We have to drop this packet :(
78 * Let the virtual server select a real server for the
79 * incoming connection, and create a connection entry.
81 *cpp
= ip_vs_schedule(svc
, skb
, pd
, &ignored
, iph
);
82 if (!*cpp
&& ignored
<= 0) {
84 *verdict
= ip_vs_leave(svc
, skb
, pd
, iph
);
98 udp_fast_csum_update(int af
, struct udphdr
*uhdr
,
99 const union nf_inet_addr
*oldip
,
100 const union nf_inet_addr
*newip
,
101 __be16 oldport
, __be16 newport
)
103 #ifdef CONFIG_IP_VS_IPV6
106 csum_fold(ip_vs_check_diff16(oldip
->ip6
, newip
->ip6
,
107 ip_vs_check_diff2(oldport
, newport
,
108 ~csum_unfold(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
))));
116 uhdr
->check
= CSUM_MANGLED_0
;
120 udp_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
)
125 #ifdef CONFIG_IP_VS_IPV6
128 ~csum_fold(ip_vs_check_diff16(oldip
->ip6
, newip
->ip6
,
129 ip_vs_check_diff2(oldlen
, newlen
,
130 csum_unfold(uhdr
->check
))));
134 ~csum_fold(ip_vs_check_diff4(oldip
->ip
, newip
->ip
,
135 ip_vs_check_diff2(oldlen
, newlen
,
136 csum_unfold(uhdr
->check
))));
141 udp_snat_handler(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
142 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
)
145 unsigned int udphoff
= iph
->len
;
147 int payload_csum
= 0;
149 #ifdef CONFIG_IP_VS_IPV6
150 if (cp
->af
== AF_INET6
&& iph
->fragoffs
)
153 oldlen
= skb
->len
- udphoff
;
155 /* csum_check requires unshared skb */
156 if (!skb_make_writable(skb
, udphoff
+sizeof(*udph
)))
159 if (unlikely(cp
->app
!= NULL
)) {
162 /* Some checks before mangling */
163 if (pp
->csum_check
&& !pp
->csum_check(cp
->af
, skb
, pp
))
167 * Call application helper if needed
169 if (!(ret
= ip_vs_app_pkt_out(cp
, skb
)))
171 /* ret=2: csum update is needed after payload mangling */
173 oldlen
= skb
->len
- udphoff
;
178 udph
= (void *)skb_network_header(skb
) + udphoff
;
179 udph
->source
= cp
->vport
;
182 * Adjust UDP checksums
184 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
185 udp_partial_csum_update(cp
->af
, udph
, &cp
->daddr
, &cp
->vaddr
,
187 htons(skb
->len
- udphoff
));
188 } else if (!payload_csum
&& (udph
->check
!= 0)) {
189 /* Only port and addr are changed, do fast csum update */
190 udp_fast_csum_update(cp
->af
, udph
, &cp
->daddr
, &cp
->vaddr
,
191 cp
->dport
, cp
->vport
);
192 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
193 skb
->ip_summed
= (cp
->app
&& pp
->csum_check
) ?
194 CHECKSUM_UNNECESSARY
: CHECKSUM_NONE
;
196 /* full checksum calculation */
198 skb
->csum
= skb_checksum(skb
, udphoff
, skb
->len
- udphoff
, 0);
199 #ifdef CONFIG_IP_VS_IPV6
200 if (cp
->af
== AF_INET6
)
201 udph
->check
= csum_ipv6_magic(&cp
->vaddr
.in6
,
204 cp
->protocol
, skb
->csum
);
207 udph
->check
= csum_tcpudp_magic(cp
->vaddr
.ip
,
212 if (udph
->check
== 0)
213 udph
->check
= CSUM_MANGLED_0
;
214 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
215 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
216 pp
->name
, udph
->check
,
217 (char*)&(udph
->check
) - (char*)udph
);
224 udp_dnat_handler(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
225 struct ip_vs_conn
*cp
, struct ip_vs_iphdr
*iph
)
228 unsigned int udphoff
= iph
->len
;
230 int payload_csum
= 0;
232 #ifdef CONFIG_IP_VS_IPV6
233 if (cp
->af
== AF_INET6
&& iph
->fragoffs
)
236 oldlen
= skb
->len
- udphoff
;
238 /* csum_check requires unshared skb */
239 if (!skb_make_writable(skb
, udphoff
+sizeof(*udph
)))
242 if (unlikely(cp
->app
!= NULL
)) {
245 /* Some checks before mangling */
246 if (pp
->csum_check
&& !pp
->csum_check(cp
->af
, skb
, pp
))
250 * Attempt ip_vs_app call.
251 * It will fix ip_vs_conn
253 if (!(ret
= ip_vs_app_pkt_in(cp
, skb
)))
255 /* ret=2: csum update is needed after payload mangling */
257 oldlen
= skb
->len
- udphoff
;
262 udph
= (void *)skb_network_header(skb
) + udphoff
;
263 udph
->dest
= cp
->dport
;
266 * Adjust UDP checksums
268 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
269 udp_partial_csum_update(cp
->af
, udph
, &cp
->vaddr
, &cp
->daddr
,
271 htons(skb
->len
- udphoff
));
272 } else if (!payload_csum
&& (udph
->check
!= 0)) {
273 /* Only port and addr are changed, do fast csum update */
274 udp_fast_csum_update(cp
->af
, udph
, &cp
->vaddr
, &cp
->daddr
,
275 cp
->vport
, cp
->dport
);
276 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
277 skb
->ip_summed
= (cp
->app
&& pp
->csum_check
) ?
278 CHECKSUM_UNNECESSARY
: CHECKSUM_NONE
;
280 /* full checksum calculation */
282 skb
->csum
= skb_checksum(skb
, udphoff
, skb
->len
- udphoff
, 0);
283 #ifdef CONFIG_IP_VS_IPV6
284 if (cp
->af
== AF_INET6
)
285 udph
->check
= csum_ipv6_magic(&cp
->caddr
.in6
,
288 cp
->protocol
, skb
->csum
);
291 udph
->check
= csum_tcpudp_magic(cp
->caddr
.ip
,
296 if (udph
->check
== 0)
297 udph
->check
= CSUM_MANGLED_0
;
298 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
305 udp_csum_check(int af
, struct sk_buff
*skb
, struct ip_vs_protocol
*pp
)
307 struct udphdr _udph
, *uh
;
308 unsigned int udphoff
;
310 #ifdef CONFIG_IP_VS_IPV6
312 udphoff
= sizeof(struct ipv6hdr
);
315 udphoff
= ip_hdrlen(skb
);
317 uh
= skb_header_pointer(skb
, udphoff
, sizeof(_udph
), &_udph
);
321 if (uh
->check
!= 0) {
322 switch (skb
->ip_summed
) {
324 skb
->csum
= skb_checksum(skb
, udphoff
,
325 skb
->len
- udphoff
, 0);
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
,
332 ipv6_hdr(skb
)->nexthdr
,
334 IP_VS_DBG_RL_PKT(0, af
, pp
, skb
, 0,
335 "Failed checksum for");
340 if (csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
343 ip_hdr(skb
)->protocol
,
345 IP_VS_DBG_RL_PKT(0, af
, pp
, skb
, 0,
346 "Failed checksum for");
351 /* No need to checksum. */
358 static inline __u16
udp_app_hashkey(__be16 port
)
360 return (((__force u16
)port
>> UDP_APP_TAB_BITS
) ^ (__force u16
)port
)
365 static int udp_register_app(struct netns_ipvs
*ipvs
, struct ip_vs_app
*inc
)
369 __be16 port
= inc
->port
;
371 struct ip_vs_proto_data
*pd
= ip_vs_proto_data_get(ipvs
, IPPROTO_UDP
);
373 hash
= udp_app_hashkey(port
);
375 list_for_each_entry(i
, &ipvs
->udp_apps
[hash
], p_list
) {
376 if (i
->port
== port
) {
381 list_add_rcu(&inc
->p_list
, &ipvs
->udp_apps
[hash
]);
382 atomic_inc(&pd
->appcnt
);
390 udp_unregister_app(struct netns_ipvs
*ipvs
, struct ip_vs_app
*inc
)
392 struct ip_vs_proto_data
*pd
= ip_vs_proto_data_get(ipvs
, IPPROTO_UDP
);
394 atomic_dec(&pd
->appcnt
);
395 list_del_rcu(&inc
->p_list
);
399 static int udp_app_conn_bind(struct ip_vs_conn
*cp
)
401 struct netns_ipvs
*ipvs
= cp
->ipvs
;
403 struct ip_vs_app
*inc
;
406 /* Default binding: bind app only for NAT */
407 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)
410 /* Lookup application incarnations and bind the right one */
411 hash
= udp_app_hashkey(cp
->vport
);
414 list_for_each_entry_rcu(inc
, &ipvs
->udp_apps
[hash
], p_list
) {
415 if (inc
->port
== cp
->vport
) {
416 if (unlikely(!ip_vs_app_inc_get(inc
)))
420 IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->"
421 "%s:%u to app %s on port %u\n",
423 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
),
425 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
),
427 inc
->name
, ntohs(inc
->port
));
431 result
= inc
->init_conn(inc
, cp
);
442 static const int udp_timeouts
[IP_VS_UDP_S_LAST
+1] = {
443 [IP_VS_UDP_S_NORMAL
] = 5*60*HZ
,
444 [IP_VS_UDP_S_LAST
] = 2*HZ
,
447 static const char *const udp_state_name_table
[IP_VS_UDP_S_LAST
+1] = {
448 [IP_VS_UDP_S_NORMAL
] = "UDP",
449 [IP_VS_UDP_S_LAST
] = "BUG!",
452 static const char * udp_state_name(int state
)
454 if (state
>= IP_VS_UDP_S_LAST
)
456 return udp_state_name_table
[state
] ? udp_state_name_table
[state
] : "?";
460 udp_state_transition(struct ip_vs_conn
*cp
, int direction
,
461 const struct sk_buff
*skb
,
462 struct ip_vs_proto_data
*pd
)
465 pr_err("UDP no ns data\n");
469 cp
->timeout
= pd
->timeout_table
[IP_VS_UDP_S_NORMAL
];
472 static int __udp_init(struct netns_ipvs
*ipvs
, struct ip_vs_proto_data
*pd
)
474 ip_vs_init_hash_table(ipvs
->udp_apps
, UDP_APP_TAB_SIZE
);
475 pd
->timeout_table
= ip_vs_create_timeout_table((int *)udp_timeouts
,
476 sizeof(udp_timeouts
));
477 if (!pd
->timeout_table
)
482 static void __udp_exit(struct netns_ipvs
*ipvs
, struct ip_vs_proto_data
*pd
)
484 kfree(pd
->timeout_table
);
488 struct ip_vs_protocol ip_vs_protocol_udp
= {
490 .protocol
= IPPROTO_UDP
,
491 .num_states
= IP_VS_UDP_S_LAST
,
495 .init_netns
= __udp_init
,
496 .exit_netns
= __udp_exit
,
497 .conn_schedule
= udp_conn_schedule
,
498 .conn_in_get
= ip_vs_conn_in_get_proto
,
499 .conn_out_get
= ip_vs_conn_out_get_proto
,
500 .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
,