2 * ip_vs_proto_tcp.c: TCP 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.
16 #include <linux/kernel.h>
18 #include <linux/tcp.h> /* for tcphdr */
20 #include <net/tcp.h> /* for csum_tcpudp_magic */
21 #include <linux/netfilter.h>
22 #include <linux/netfilter_ipv4.h>
24 #include <net/ip_vs.h>
27 static struct ip_vs_conn *
28 tcp_conn_in_get(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp,
29 const struct ip_vs_iphdr *iph, unsigned int proto_off,
32 __be16 _ports[2], *pptr;
34 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
38 if (likely(!inverse)) {
39 return ip_vs_conn_in_get(iph->protocol,
40 iph->saddr.ip, pptr[0],
41 iph->daddr.ip, pptr[1]);
43 return ip_vs_conn_in_get(iph->protocol,
44 iph->daddr.ip, pptr[1],
45 iph->saddr.ip, pptr[0]);
49 static struct ip_vs_conn *
50 tcp_conn_out_get(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp,
51 const struct ip_vs_iphdr *iph, unsigned int proto_off,
54 __be16 _ports[2], *pptr;
56 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
60 if (likely(!inverse)) {
61 return ip_vs_conn_out_get(iph->protocol,
62 iph->saddr.ip, pptr[0],
63 iph->daddr.ip, pptr[1]);
65 return ip_vs_conn_out_get(iph->protocol,
66 iph->daddr.ip, pptr[1],
67 iph->saddr.ip, pptr[0]);
73 tcp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
74 int *verdict, struct ip_vs_conn **cpp)
76 struct ip_vs_service *svc;
77 struct tcphdr _tcph, *th;
78 struct ip_vs_iphdr iph;
80 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
82 th = skb_header_pointer(skb, iph.len, sizeof(_tcph), &_tcph);
89 (svc = ip_vs_service_get(af, skb->mark, iph.protocol, &iph.daddr,
93 * It seems that we are very loaded.
94 * We have to drop this packet :(
96 ip_vs_service_put(svc);
102 * Let the virtual server select a real server for the
103 * incoming connection, and create a connection entry.
105 *cpp = ip_vs_schedule(svc, skb);
107 *verdict = ip_vs_leave(svc, skb, pp);
110 ip_vs_service_put(svc);
117 tcp_fast_csum_update(struct tcphdr *tcph, __be32 oldip, __be32 newip,
118 __be16 oldport, __be16 newport)
121 csum_fold(ip_vs_check_diff4(oldip, newip,
122 ip_vs_check_diff2(oldport, newport,
123 ~csum_unfold(tcph->check))));
128 tcp_snat_handler(struct sk_buff *skb,
129 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
132 const unsigned int tcphoff = ip_hdrlen(skb);
134 /* csum_check requires unshared skb */
135 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
138 if (unlikely(cp->app != NULL)) {
139 /* Some checks before mangling */
140 if (pp->csum_check && !pp->csum_check(AF_INET, skb, pp))
143 /* Call application helper if needed */
144 if (!ip_vs_app_pkt_out(cp, skb))
148 tcph = (void *)ip_hdr(skb) + tcphoff;
149 tcph->source = cp->vport;
151 /* Adjust TCP checksums */
153 /* Only port and addr are changed, do fast csum update */
154 tcp_fast_csum_update(tcph, cp->daddr.ip, cp->vaddr.ip,
155 cp->dport, cp->vport);
156 if (skb->ip_summed == CHECKSUM_COMPLETE)
157 skb->ip_summed = CHECKSUM_NONE;
159 /* full checksum calculation */
161 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
162 tcph->check = csum_tcpudp_magic(cp->vaddr.ip, cp->caddr.ip,
164 cp->protocol, skb->csum);
165 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
166 pp->name, tcph->check,
167 (char*)&(tcph->check) - (char*)tcph);
174 tcp_dnat_handler(struct sk_buff *skb,
175 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
178 const unsigned int tcphoff = ip_hdrlen(skb);
180 /* csum_check requires unshared skb */
181 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
184 if (unlikely(cp->app != NULL)) {
185 /* Some checks before mangling */
186 if (pp->csum_check && !pp->csum_check(AF_INET, skb, pp))
190 * Attempt ip_vs_app call.
191 * It will fix ip_vs_conn and iph ack_seq stuff
193 if (!ip_vs_app_pkt_in(cp, skb))
197 tcph = (void *)ip_hdr(skb) + tcphoff;
198 tcph->dest = cp->dport;
201 * Adjust TCP checksums
204 /* Only port and addr are changed, do fast csum update */
205 tcp_fast_csum_update(tcph, cp->vaddr.ip, cp->daddr.ip,
206 cp->vport, cp->dport);
207 if (skb->ip_summed == CHECKSUM_COMPLETE)
208 skb->ip_summed = CHECKSUM_NONE;
210 /* full checksum calculation */
212 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
213 tcph->check = csum_tcpudp_magic(cp->caddr.ip, cp->daddr.ip,
215 cp->protocol, skb->csum);
216 skb->ip_summed = CHECKSUM_UNNECESSARY;
223 tcp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
225 unsigned int tcphoff;
227 #ifdef CONFIG_IP_VS_IPV6
229 tcphoff = sizeof(struct ipv6hdr);
232 tcphoff = ip_hdrlen(skb);
234 switch (skb->ip_summed) {
236 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
237 case CHECKSUM_COMPLETE:
238 #ifdef CONFIG_IP_VS_IPV6
239 if (af == AF_INET6) {
240 if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
241 &ipv6_hdr(skb)->daddr,
243 ipv6_hdr(skb)->nexthdr,
245 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
246 "Failed checksum for");
251 if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
254 ip_hdr(skb)->protocol,
256 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
257 "Failed checksum for");
262 /* No need to checksum. */
270 #define TCP_DIR_INPUT 0
271 #define TCP_DIR_OUTPUT 4
272 #define TCP_DIR_INPUT_ONLY 8
274 static const int tcp_state_off[IP_VS_DIR_LAST] = {
275 [IP_VS_DIR_INPUT] = TCP_DIR_INPUT,
276 [IP_VS_DIR_OUTPUT] = TCP_DIR_OUTPUT,
277 [IP_VS_DIR_INPUT_ONLY] = TCP_DIR_INPUT_ONLY,
281 * Timeout table[state]
283 static int tcp_timeouts[IP_VS_TCP_S_LAST+1] = {
284 [IP_VS_TCP_S_NONE] = 2*HZ,
285 [IP_VS_TCP_S_ESTABLISHED] = 15*60*HZ,
286 [IP_VS_TCP_S_SYN_SENT] = 2*60*HZ,
287 [IP_VS_TCP_S_SYN_RECV] = 1*60*HZ,
288 [IP_VS_TCP_S_FIN_WAIT] = 2*60*HZ,
289 [IP_VS_TCP_S_TIME_WAIT] = 2*60*HZ,
290 [IP_VS_TCP_S_CLOSE] = 10*HZ,
291 [IP_VS_TCP_S_CLOSE_WAIT] = 60*HZ,
292 [IP_VS_TCP_S_LAST_ACK] = 30*HZ,
293 [IP_VS_TCP_S_LISTEN] = 2*60*HZ,
294 [IP_VS_TCP_S_SYNACK] = 120*HZ,
295 [IP_VS_TCP_S_LAST] = 2*HZ,
298 static char * tcp_state_name_table[IP_VS_TCP_S_LAST+1] = {
299 [IP_VS_TCP_S_NONE] = "NONE",
300 [IP_VS_TCP_S_ESTABLISHED] = "ESTABLISHED",
301 [IP_VS_TCP_S_SYN_SENT] = "SYN_SENT",
302 [IP_VS_TCP_S_SYN_RECV] = "SYN_RECV",
303 [IP_VS_TCP_S_FIN_WAIT] = "FIN_WAIT",
304 [IP_VS_TCP_S_TIME_WAIT] = "TIME_WAIT",
305 [IP_VS_TCP_S_CLOSE] = "CLOSE",
306 [IP_VS_TCP_S_CLOSE_WAIT] = "CLOSE_WAIT",
307 [IP_VS_TCP_S_LAST_ACK] = "LAST_ACK",
308 [IP_VS_TCP_S_LISTEN] = "LISTEN",
309 [IP_VS_TCP_S_SYNACK] = "SYNACK",
310 [IP_VS_TCP_S_LAST] = "BUG!",
313 #define sNO IP_VS_TCP_S_NONE
314 #define sES IP_VS_TCP_S_ESTABLISHED
315 #define sSS IP_VS_TCP_S_SYN_SENT
316 #define sSR IP_VS_TCP_S_SYN_RECV
317 #define sFW IP_VS_TCP_S_FIN_WAIT
318 #define sTW IP_VS_TCP_S_TIME_WAIT
319 #define sCL IP_VS_TCP_S_CLOSE
320 #define sCW IP_VS_TCP_S_CLOSE_WAIT
321 #define sLA IP_VS_TCP_S_LAST_ACK
322 #define sLI IP_VS_TCP_S_LISTEN
323 #define sSA IP_VS_TCP_S_SYNACK
325 struct tcp_states_t {
326 int next_state[IP_VS_TCP_S_LAST];
329 static const char * tcp_state_name(int state)
331 if (state >= IP_VS_TCP_S_LAST)
333 return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?";
336 static struct tcp_states_t tcp_states [] = {
338 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
339 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
340 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW }},
341 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
342 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR }},
345 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
346 /*syn*/ {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR }},
347 /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
348 /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
349 /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
352 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
353 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
354 /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
355 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
356 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
359 static struct tcp_states_t tcp_states_dos [] = {
361 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
362 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSA }},
363 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sSA }},
364 /*ack*/ {{sCL, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }},
365 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
368 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
369 /*syn*/ {{sSS, sES, sSS, sSA, sSS, sSS, sSS, sSS, sSS, sLI, sSA }},
370 /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
371 /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
372 /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
375 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
376 /*syn*/ {{sSA, sES, sES, sSR, sSA, sSA, sSA, sSA, sSA, sSA, sSA }},
377 /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
378 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
379 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
382 static struct tcp_states_t *tcp_state_table = tcp_states;
385 static void tcp_timeout_change(struct ip_vs_protocol *pp, int flags)
387 int on = (flags & 1); /* secure_tcp */
390 ** FIXME: change secure_tcp to independent sysctl var
391 ** or make it per-service or per-app because it is valid
392 ** for most if not for all of the applications. Something
393 ** like "capabilities" (flags) for each object.
395 tcp_state_table = (on? tcp_states_dos : tcp_states);
399 tcp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
401 return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_TCP_S_LAST,
402 tcp_state_name_table, sname, to);
405 static inline int tcp_state_idx(struct tcphdr *th)
419 set_tcp_state(struct ip_vs_protocol *pp, struct ip_vs_conn *cp,
420 int direction, struct tcphdr *th)
423 int new_state = IP_VS_TCP_S_CLOSE;
424 int state_off = tcp_state_off[direction];
427 * Update state offset to INPUT_ONLY if necessary
428 * or delete NO_OUTPUT flag if output packet detected
430 if (cp->flags & IP_VS_CONN_F_NOOUTPUT) {
431 if (state_off == TCP_DIR_OUTPUT)
432 cp->flags &= ~IP_VS_CONN_F_NOOUTPUT;
434 state_off = TCP_DIR_INPUT_ONLY;
437 if ((state_idx = tcp_state_idx(th)) < 0) {
438 IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx);
442 new_state = tcp_state_table[state_off+state_idx].next_state[cp->state];
445 if (new_state != cp->state) {
446 struct ip_vs_dest *dest = cp->dest;
448 IP_VS_DBG(8, "%s %s [%c%c%c%c] %u.%u.%u.%u:%d->"
449 "%u.%u.%u.%u:%d state: %s->%s conn->refcnt:%d\n",
451 (state_off==TCP_DIR_OUTPUT)?"output ":"input ",
456 NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
457 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
458 tcp_state_name(cp->state),
459 tcp_state_name(new_state),
460 atomic_read(&cp->refcnt));
462 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
463 (new_state != IP_VS_TCP_S_ESTABLISHED)) {
464 atomic_dec(&dest->activeconns);
465 atomic_inc(&dest->inactconns);
466 cp->flags |= IP_VS_CONN_F_INACTIVE;
467 } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
468 (new_state == IP_VS_TCP_S_ESTABLISHED)) {
469 atomic_inc(&dest->activeconns);
470 atomic_dec(&dest->inactconns);
471 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
476 cp->timeout = pp->timeout_table[cp->state = new_state];
481 * Handle state transitions
484 tcp_state_transition(struct ip_vs_conn *cp, int direction,
485 const struct sk_buff *skb,
486 struct ip_vs_protocol *pp)
488 struct tcphdr _tcph, *th;
490 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
494 spin_lock(&cp->lock);
495 set_tcp_state(pp, cp, direction, th);
496 spin_unlock(&cp->lock);
503 * Hash table for TCP application incarnations
505 #define TCP_APP_TAB_BITS 4
506 #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
507 #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
509 static struct list_head tcp_apps[TCP_APP_TAB_SIZE];
510 static DEFINE_SPINLOCK(tcp_app_lock);
512 static inline __u16 tcp_app_hashkey(__be16 port)
514 return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port)
519 static int tcp_register_app(struct ip_vs_app *inc)
523 __be16 port = inc->port;
526 hash = tcp_app_hashkey(port);
528 spin_lock_bh(&tcp_app_lock);
529 list_for_each_entry(i, &tcp_apps[hash], p_list) {
530 if (i->port == port) {
535 list_add(&inc->p_list, &tcp_apps[hash]);
536 atomic_inc(&ip_vs_protocol_tcp.appcnt);
539 spin_unlock_bh(&tcp_app_lock);
545 tcp_unregister_app(struct ip_vs_app *inc)
547 spin_lock_bh(&tcp_app_lock);
548 atomic_dec(&ip_vs_protocol_tcp.appcnt);
549 list_del(&inc->p_list);
550 spin_unlock_bh(&tcp_app_lock);
555 tcp_app_conn_bind(struct ip_vs_conn *cp)
558 struct ip_vs_app *inc;
561 /* Default binding: bind app only for NAT */
562 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
565 /* Lookup application incarnations and bind the right one */
566 hash = tcp_app_hashkey(cp->vport);
568 spin_lock(&tcp_app_lock);
569 list_for_each_entry(inc, &tcp_apps[hash], p_list) {
570 if (inc->port == cp->vport) {
571 if (unlikely(!ip_vs_app_inc_get(inc)))
573 spin_unlock(&tcp_app_lock);
575 IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->"
576 "%u.%u.%u.%u:%u to app %s on port %u\n",
578 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
579 NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
580 inc->name, ntohs(inc->port));
583 result = inc->init_conn(inc, cp);
587 spin_unlock(&tcp_app_lock);
595 * Set LISTEN timeout. (ip_vs_conn_put will setup timer)
597 void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp)
599 spin_lock(&cp->lock);
600 cp->state = IP_VS_TCP_S_LISTEN;
601 cp->timeout = ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_LISTEN];
602 spin_unlock(&cp->lock);
606 static void ip_vs_tcp_init(struct ip_vs_protocol *pp)
608 IP_VS_INIT_HASH_TABLE(tcp_apps);
609 pp->timeout_table = tcp_timeouts;
613 static void ip_vs_tcp_exit(struct ip_vs_protocol *pp)
618 struct ip_vs_protocol ip_vs_protocol_tcp = {
620 .protocol = IPPROTO_TCP,
621 .num_states = IP_VS_TCP_S_LAST,
623 .appcnt = ATOMIC_INIT(0),
624 .init = ip_vs_tcp_init,
625 .exit = ip_vs_tcp_exit,
626 .register_app = tcp_register_app,
627 .unregister_app = tcp_unregister_app,
628 .conn_schedule = tcp_conn_schedule,
629 .conn_in_get = tcp_conn_in_get,
630 .conn_out_get = tcp_conn_out_get,
631 .snat_handler = tcp_snat_handler,
632 .dnat_handler = tcp_dnat_handler,
633 .csum_check = tcp_csum_check,
634 .state_name = tcp_state_name,
635 .state_transition = tcp_state_transition,
636 .app_conn_bind = tcp_app_conn_bind,
637 .debug_packet = ip_vs_tcpudp_debug_packet,
638 .timeout_change = tcp_timeout_change,
639 .set_state_timeout = tcp_set_state_timeout,