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 #define KMSG_COMPONENT "IPVS"
17 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19 #include <linux/kernel.h>
21 #include <linux/tcp.h> /* for tcphdr */
23 #include <net/tcp.h> /* for csum_tcpudp_magic */
24 #include <net/ip6_checksum.h>
25 #include <linux/netfilter.h>
26 #include <linux/netfilter_ipv4.h>
28 #include <net/ip_vs.h>
31 tcp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
32 int *verdict, struct ip_vs_conn **cpp)
34 struct ip_vs_service *svc;
35 struct tcphdr _tcph, *th;
36 struct ip_vs_iphdr iph;
38 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
40 th = skb_header_pointer(skb, iph.len, sizeof(_tcph), &_tcph);
46 /* No !th->ack check to allow scheduling on SYN+ACK for Active FTP */
48 (svc = ip_vs_service_get(af, skb->mark, iph.protocol, &iph.daddr,
54 * It seems that we are very loaded.
55 * We have to drop this packet :(
57 ip_vs_service_put(svc);
63 * Let the virtual server select a real server for the
64 * incoming connection, and create a connection entry.
66 *cpp = ip_vs_schedule(svc, skb, pp, &ignored);
67 if (!*cpp && !ignored) {
68 *verdict = ip_vs_leave(svc, skb, pp);
71 ip_vs_service_put(svc);
78 tcp_fast_csum_update(int af, struct tcphdr *tcph,
79 const union nf_inet_addr *oldip,
80 const union nf_inet_addr *newip,
81 __be16 oldport, __be16 newport)
83 #ifdef CONFIG_IP_VS_IPV6
86 csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
87 ip_vs_check_diff2(oldport, newport,
88 ~csum_unfold(tcph->check))));
92 csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
93 ip_vs_check_diff2(oldport, newport,
94 ~csum_unfold(tcph->check))));
99 tcp_partial_csum_update(int af, struct tcphdr *tcph,
100 const union nf_inet_addr *oldip,
101 const union nf_inet_addr *newip,
102 __be16 oldlen, __be16 newlen)
104 #ifdef CONFIG_IP_VS_IPV6
107 ~csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
108 ip_vs_check_diff2(oldlen, newlen,
109 csum_unfold(tcph->check))));
113 ~csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
114 ip_vs_check_diff2(oldlen, newlen,
115 csum_unfold(tcph->check))));
120 tcp_snat_handler(struct sk_buff *skb,
121 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
124 unsigned int tcphoff;
126 int payload_csum = 0;
128 #ifdef CONFIG_IP_VS_IPV6
129 if (cp->af == AF_INET6)
130 tcphoff = sizeof(struct ipv6hdr);
133 tcphoff = ip_hdrlen(skb);
134 oldlen = skb->len - tcphoff;
136 /* csum_check requires unshared skb */
137 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
140 if (unlikely(cp->app != NULL)) {
143 /* Some checks before mangling */
144 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
147 /* Call application helper if needed */
148 if (!(ret = ip_vs_app_pkt_out(cp, skb)))
150 /* ret=2: csum update is needed after payload mangling */
152 oldlen = skb->len - tcphoff;
157 tcph = (void *)skb_network_header(skb) + tcphoff;
158 tcph->source = cp->vport;
160 /* Adjust TCP checksums */
161 if (skb->ip_summed == CHECKSUM_PARTIAL) {
162 tcp_partial_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr,
164 htons(skb->len - tcphoff));
165 } else if (!payload_csum) {
166 /* Only port and addr are changed, do fast csum update */
167 tcp_fast_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr,
168 cp->dport, cp->vport);
169 if (skb->ip_summed == CHECKSUM_COMPLETE)
170 skb->ip_summed = (cp->app && pp->csum_check) ?
171 CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
173 /* full checksum calculation */
175 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
176 #ifdef CONFIG_IP_VS_IPV6
177 if (cp->af == AF_INET6)
178 tcph->check = csum_ipv6_magic(&cp->vaddr.in6,
181 cp->protocol, skb->csum);
184 tcph->check = csum_tcpudp_magic(cp->vaddr.ip,
189 skb->ip_summed = CHECKSUM_UNNECESSARY;
191 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
192 pp->name, tcph->check,
193 (char*)&(tcph->check) - (char*)tcph);
200 tcp_dnat_handler(struct sk_buff *skb,
201 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
204 unsigned int tcphoff;
206 int payload_csum = 0;
208 #ifdef CONFIG_IP_VS_IPV6
209 if (cp->af == AF_INET6)
210 tcphoff = sizeof(struct ipv6hdr);
213 tcphoff = ip_hdrlen(skb);
214 oldlen = skb->len - tcphoff;
216 /* csum_check requires unshared skb */
217 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
220 if (unlikely(cp->app != NULL)) {
223 /* Some checks before mangling */
224 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
228 * Attempt ip_vs_app call.
229 * It will fix ip_vs_conn and iph ack_seq stuff
231 if (!(ret = ip_vs_app_pkt_in(cp, skb)))
233 /* ret=2: csum update is needed after payload mangling */
235 oldlen = skb->len - tcphoff;
240 tcph = (void *)skb_network_header(skb) + tcphoff;
241 tcph->dest = cp->dport;
244 * Adjust TCP checksums
246 if (skb->ip_summed == CHECKSUM_PARTIAL) {
247 tcp_partial_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr,
249 htons(skb->len - tcphoff));
250 } else if (!payload_csum) {
251 /* Only port and addr are changed, do fast csum update */
252 tcp_fast_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr,
253 cp->vport, cp->dport);
254 if (skb->ip_summed == CHECKSUM_COMPLETE)
255 skb->ip_summed = (cp->app && pp->csum_check) ?
256 CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
258 /* full checksum calculation */
260 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
261 #ifdef CONFIG_IP_VS_IPV6
262 if (cp->af == AF_INET6)
263 tcph->check = csum_ipv6_magic(&cp->caddr.in6,
266 cp->protocol, skb->csum);
269 tcph->check = csum_tcpudp_magic(cp->caddr.ip,
274 skb->ip_summed = CHECKSUM_UNNECESSARY;
281 tcp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
283 unsigned int tcphoff;
285 #ifdef CONFIG_IP_VS_IPV6
287 tcphoff = sizeof(struct ipv6hdr);
290 tcphoff = ip_hdrlen(skb);
292 switch (skb->ip_summed) {
294 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
295 case CHECKSUM_COMPLETE:
296 #ifdef CONFIG_IP_VS_IPV6
297 if (af == AF_INET6) {
298 if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
299 &ipv6_hdr(skb)->daddr,
301 ipv6_hdr(skb)->nexthdr,
303 IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
304 "Failed checksum for");
309 if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
312 ip_hdr(skb)->protocol,
314 IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
315 "Failed checksum for");
320 /* No need to checksum. */
328 #define TCP_DIR_INPUT 0
329 #define TCP_DIR_OUTPUT 4
330 #define TCP_DIR_INPUT_ONLY 8
332 static const int tcp_state_off[IP_VS_DIR_LAST] = {
333 [IP_VS_DIR_INPUT] = TCP_DIR_INPUT,
334 [IP_VS_DIR_OUTPUT] = TCP_DIR_OUTPUT,
335 [IP_VS_DIR_INPUT_ONLY] = TCP_DIR_INPUT_ONLY,
339 * Timeout table[state]
341 static int tcp_timeouts[IP_VS_TCP_S_LAST+1] = {
342 [IP_VS_TCP_S_NONE] = 2*HZ,
343 [IP_VS_TCP_S_ESTABLISHED] = 15*60*HZ,
344 [IP_VS_TCP_S_SYN_SENT] = 2*60*HZ,
345 [IP_VS_TCP_S_SYN_RECV] = 1*60*HZ,
346 [IP_VS_TCP_S_FIN_WAIT] = 2*60*HZ,
347 [IP_VS_TCP_S_TIME_WAIT] = 2*60*HZ,
348 [IP_VS_TCP_S_CLOSE] = 10*HZ,
349 [IP_VS_TCP_S_CLOSE_WAIT] = 60*HZ,
350 [IP_VS_TCP_S_LAST_ACK] = 30*HZ,
351 [IP_VS_TCP_S_LISTEN] = 2*60*HZ,
352 [IP_VS_TCP_S_SYNACK] = 120*HZ,
353 [IP_VS_TCP_S_LAST] = 2*HZ,
356 static const char *const tcp_state_name_table[IP_VS_TCP_S_LAST+1] = {
357 [IP_VS_TCP_S_NONE] = "NONE",
358 [IP_VS_TCP_S_ESTABLISHED] = "ESTABLISHED",
359 [IP_VS_TCP_S_SYN_SENT] = "SYN_SENT",
360 [IP_VS_TCP_S_SYN_RECV] = "SYN_RECV",
361 [IP_VS_TCP_S_FIN_WAIT] = "FIN_WAIT",
362 [IP_VS_TCP_S_TIME_WAIT] = "TIME_WAIT",
363 [IP_VS_TCP_S_CLOSE] = "CLOSE",
364 [IP_VS_TCP_S_CLOSE_WAIT] = "CLOSE_WAIT",
365 [IP_VS_TCP_S_LAST_ACK] = "LAST_ACK",
366 [IP_VS_TCP_S_LISTEN] = "LISTEN",
367 [IP_VS_TCP_S_SYNACK] = "SYNACK",
368 [IP_VS_TCP_S_LAST] = "BUG!",
371 #define sNO IP_VS_TCP_S_NONE
372 #define sES IP_VS_TCP_S_ESTABLISHED
373 #define sSS IP_VS_TCP_S_SYN_SENT
374 #define sSR IP_VS_TCP_S_SYN_RECV
375 #define sFW IP_VS_TCP_S_FIN_WAIT
376 #define sTW IP_VS_TCP_S_TIME_WAIT
377 #define sCL IP_VS_TCP_S_CLOSE
378 #define sCW IP_VS_TCP_S_CLOSE_WAIT
379 #define sLA IP_VS_TCP_S_LAST_ACK
380 #define sLI IP_VS_TCP_S_LISTEN
381 #define sSA IP_VS_TCP_S_SYNACK
383 struct tcp_states_t {
384 int next_state[IP_VS_TCP_S_LAST];
387 static const char * tcp_state_name(int state)
389 if (state >= IP_VS_TCP_S_LAST)
391 return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?";
394 static struct tcp_states_t tcp_states [] = {
396 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
397 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
398 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW }},
399 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
400 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR }},
403 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
404 /*syn*/ {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR }},
405 /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
406 /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
407 /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
410 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
411 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
412 /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
413 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
414 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
417 static struct tcp_states_t tcp_states_dos [] = {
419 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
420 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSA }},
421 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sSA }},
422 /*ack*/ {{sCL, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }},
423 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
426 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
427 /*syn*/ {{sSS, sES, sSS, sSA, sSS, sSS, sSS, sSS, sSS, sLI, sSA }},
428 /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
429 /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
430 /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
433 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
434 /*syn*/ {{sSA, sES, sES, sSR, sSA, sSA, sSA, sSA, sSA, sSA, sSA }},
435 /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
436 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
437 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
440 static struct tcp_states_t *tcp_state_table = tcp_states;
443 static void tcp_timeout_change(struct ip_vs_protocol *pp, int flags)
445 int on = (flags & 1); /* secure_tcp */
448 ** FIXME: change secure_tcp to independent sysctl var
449 ** or make it per-service or per-app because it is valid
450 ** for most if not for all of the applications. Something
451 ** like "capabilities" (flags) for each object.
453 tcp_state_table = (on? tcp_states_dos : tcp_states);
457 tcp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
459 return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_TCP_S_LAST,
460 tcp_state_name_table, sname, to);
463 static inline int tcp_state_idx(struct tcphdr *th)
477 set_tcp_state(struct ip_vs_protocol *pp, struct ip_vs_conn *cp,
478 int direction, struct tcphdr *th)
481 int new_state = IP_VS_TCP_S_CLOSE;
482 int state_off = tcp_state_off[direction];
485 * Update state offset to INPUT_ONLY if necessary
486 * or delete NO_OUTPUT flag if output packet detected
488 if (cp->flags & IP_VS_CONN_F_NOOUTPUT) {
489 if (state_off == TCP_DIR_OUTPUT)
490 cp->flags &= ~IP_VS_CONN_F_NOOUTPUT;
492 state_off = TCP_DIR_INPUT_ONLY;
495 if ((state_idx = tcp_state_idx(th)) < 0) {
496 IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx);
500 new_state = tcp_state_table[state_off+state_idx].next_state[cp->state];
503 if (new_state != cp->state) {
504 struct ip_vs_dest *dest = cp->dest;
506 IP_VS_DBG_BUF(8, "%s %s [%c%c%c%c] %s:%d->"
507 "%s:%d state: %s->%s conn->refcnt:%d\n",
509 ((state_off == TCP_DIR_OUTPUT) ?
510 "output " : "input "),
515 IP_VS_DBG_ADDR(cp->af, &cp->daddr),
517 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
519 tcp_state_name(cp->state),
520 tcp_state_name(new_state),
521 atomic_read(&cp->refcnt));
524 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
525 (new_state != IP_VS_TCP_S_ESTABLISHED)) {
526 atomic_dec(&dest->activeconns);
527 atomic_inc(&dest->inactconns);
528 cp->flags |= IP_VS_CONN_F_INACTIVE;
529 } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
530 (new_state == IP_VS_TCP_S_ESTABLISHED)) {
531 atomic_inc(&dest->activeconns);
532 atomic_dec(&dest->inactconns);
533 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
538 cp->timeout = pp->timeout_table[cp->state = new_state];
543 * Handle state transitions
546 tcp_state_transition(struct ip_vs_conn *cp, int direction,
547 const struct sk_buff *skb,
548 struct ip_vs_protocol *pp)
550 struct tcphdr _tcph, *th;
552 #ifdef CONFIG_IP_VS_IPV6
553 int ihl = cp->af == AF_INET ? ip_hdrlen(skb) : sizeof(struct ipv6hdr);
555 int ihl = ip_hdrlen(skb);
558 th = skb_header_pointer(skb, ihl, sizeof(_tcph), &_tcph);
562 spin_lock(&cp->lock);
563 set_tcp_state(pp, cp, direction, th);
564 spin_unlock(&cp->lock);
571 * Hash table for TCP application incarnations
573 #define TCP_APP_TAB_BITS 4
574 #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
575 #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
577 static struct list_head tcp_apps[TCP_APP_TAB_SIZE];
578 static DEFINE_SPINLOCK(tcp_app_lock);
580 static inline __u16 tcp_app_hashkey(__be16 port)
582 return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port)
587 static int tcp_register_app(struct ip_vs_app *inc)
591 __be16 port = inc->port;
594 hash = tcp_app_hashkey(port);
596 spin_lock_bh(&tcp_app_lock);
597 list_for_each_entry(i, &tcp_apps[hash], p_list) {
598 if (i->port == port) {
603 list_add(&inc->p_list, &tcp_apps[hash]);
604 atomic_inc(&ip_vs_protocol_tcp.appcnt);
607 spin_unlock_bh(&tcp_app_lock);
613 tcp_unregister_app(struct ip_vs_app *inc)
615 spin_lock_bh(&tcp_app_lock);
616 atomic_dec(&ip_vs_protocol_tcp.appcnt);
617 list_del(&inc->p_list);
618 spin_unlock_bh(&tcp_app_lock);
623 tcp_app_conn_bind(struct ip_vs_conn *cp)
626 struct ip_vs_app *inc;
629 /* Default binding: bind app only for NAT */
630 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
633 /* Lookup application incarnations and bind the right one */
634 hash = tcp_app_hashkey(cp->vport);
636 spin_lock(&tcp_app_lock);
637 list_for_each_entry(inc, &tcp_apps[hash], p_list) {
638 if (inc->port == cp->vport) {
639 if (unlikely(!ip_vs_app_inc_get(inc)))
641 spin_unlock(&tcp_app_lock);
643 IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->"
644 "%s:%u to app %s on port %u\n",
646 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
648 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
650 inc->name, ntohs(inc->port));
654 result = inc->init_conn(inc, cp);
658 spin_unlock(&tcp_app_lock);
666 * Set LISTEN timeout. (ip_vs_conn_put will setup timer)
668 void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp)
670 spin_lock(&cp->lock);
671 cp->state = IP_VS_TCP_S_LISTEN;
672 cp->timeout = ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_LISTEN];
673 spin_unlock(&cp->lock);
677 static void ip_vs_tcp_init(struct ip_vs_protocol *pp)
679 IP_VS_INIT_HASH_TABLE(tcp_apps);
680 pp->timeout_table = tcp_timeouts;
684 static void ip_vs_tcp_exit(struct ip_vs_protocol *pp)
689 struct ip_vs_protocol ip_vs_protocol_tcp = {
691 .protocol = IPPROTO_TCP,
692 .num_states = IP_VS_TCP_S_LAST,
694 .appcnt = ATOMIC_INIT(0),
695 .init = ip_vs_tcp_init,
696 .exit = ip_vs_tcp_exit,
697 .register_app = tcp_register_app,
698 .unregister_app = tcp_unregister_app,
699 .conn_schedule = tcp_conn_schedule,
700 .conn_in_get = ip_vs_conn_in_get_proto,
701 .conn_out_get = ip_vs_conn_out_get_proto,
702 .snat_handler = tcp_snat_handler,
703 .dnat_handler = tcp_dnat_handler,
704 .csum_check = tcp_csum_check,
705 .state_name = tcp_state_name,
706 .state_transition = tcp_state_transition,
707 .app_conn_bind = tcp_app_conn_bind,
708 .debug_packet = ip_vs_tcpudp_debug_packet,
709 .timeout_change = tcp_timeout_change,
710 .set_state_timeout = tcp_set_state_timeout,