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(af, iph->protocol,
41 &iph->daddr, pptr[1]);
43 return ip_vs_conn_in_get(af, iph->protocol,
45 &iph->saddr, 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(af, iph->protocol,
63 &iph->daddr, pptr[1]);
65 return ip_vs_conn_out_get(af, iph->protocol,
67 &iph->saddr, 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(int af, struct tcphdr *tcph,
118 const union nf_inet_addr *oldip,
119 const union nf_inet_addr *newip,
120 __be16 oldport, __be16 newport)
122 #ifdef CONFIG_IP_VS_IPV6
125 csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
126 ip_vs_check_diff2(oldport, newport,
127 ~csum_unfold(tcph->check))));
131 csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
132 ip_vs_check_diff2(oldport, newport,
133 ~csum_unfold(tcph->check))));
138 tcp_snat_handler(struct sk_buff *skb,
139 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
142 unsigned int tcphoff;
144 #ifdef CONFIG_IP_VS_IPV6
145 if (cp->af == AF_INET6)
146 tcphoff = sizeof(struct ipv6hdr);
149 tcphoff = ip_hdrlen(skb);
151 /* csum_check requires unshared skb */
152 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
155 if (unlikely(cp->app != NULL)) {
156 /* Some checks before mangling */
157 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
160 /* Call application helper if needed */
161 if (!ip_vs_app_pkt_out(cp, skb))
165 tcph = (void *)skb_network_header(skb) + tcphoff;
166 tcph->source = cp->vport;
168 /* Adjust TCP checksums */
170 /* Only port and addr are changed, do fast csum update */
171 tcp_fast_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr,
172 cp->dport, cp->vport);
173 if (skb->ip_summed == CHECKSUM_COMPLETE)
174 skb->ip_summed = CHECKSUM_NONE;
176 /* full checksum calculation */
178 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
179 #ifdef CONFIG_IP_VS_IPV6
180 if (cp->af == AF_INET6)
181 tcph->check = csum_ipv6_magic(&cp->vaddr.in6,
184 cp->protocol, skb->csum);
187 tcph->check = csum_tcpudp_magic(cp->vaddr.ip,
193 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
194 pp->name, tcph->check,
195 (char*)&(tcph->check) - (char*)tcph);
202 tcp_dnat_handler(struct sk_buff *skb,
203 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
206 unsigned int tcphoff;
208 #ifdef CONFIG_IP_VS_IPV6
209 if (cp->af == AF_INET6)
210 tcphoff = sizeof(struct ipv6hdr);
213 tcphoff = ip_hdrlen(skb);
215 /* csum_check requires unshared skb */
216 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
219 if (unlikely(cp->app != NULL)) {
220 /* Some checks before mangling */
221 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
225 * Attempt ip_vs_app call.
226 * It will fix ip_vs_conn and iph ack_seq stuff
228 if (!ip_vs_app_pkt_in(cp, skb))
232 tcph = (void *)skb_network_header(skb) + tcphoff;
233 tcph->dest = cp->dport;
236 * Adjust TCP checksums
239 /* Only port and addr are changed, do fast csum update */
240 tcp_fast_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr,
241 cp->vport, cp->dport);
242 if (skb->ip_summed == CHECKSUM_COMPLETE)
243 skb->ip_summed = CHECKSUM_NONE;
245 /* full checksum calculation */
247 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
248 #ifdef CONFIG_IP_VS_IPV6
249 if (cp->af == AF_INET6)
250 tcph->check = csum_ipv6_magic(&cp->caddr.in6,
253 cp->protocol, skb->csum);
256 tcph->check = csum_tcpudp_magic(cp->caddr.ip,
261 skb->ip_summed = CHECKSUM_UNNECESSARY;
268 tcp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
270 unsigned int tcphoff;
272 #ifdef CONFIG_IP_VS_IPV6
274 tcphoff = sizeof(struct ipv6hdr);
277 tcphoff = ip_hdrlen(skb);
279 switch (skb->ip_summed) {
281 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
282 case CHECKSUM_COMPLETE:
283 #ifdef CONFIG_IP_VS_IPV6
284 if (af == AF_INET6) {
285 if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
286 &ipv6_hdr(skb)->daddr,
288 ipv6_hdr(skb)->nexthdr,
290 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
291 "Failed checksum for");
296 if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
299 ip_hdr(skb)->protocol,
301 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
302 "Failed checksum for");
307 /* No need to checksum. */
315 #define TCP_DIR_INPUT 0
316 #define TCP_DIR_OUTPUT 4
317 #define TCP_DIR_INPUT_ONLY 8
319 static const int tcp_state_off[IP_VS_DIR_LAST] = {
320 [IP_VS_DIR_INPUT] = TCP_DIR_INPUT,
321 [IP_VS_DIR_OUTPUT] = TCP_DIR_OUTPUT,
322 [IP_VS_DIR_INPUT_ONLY] = TCP_DIR_INPUT_ONLY,
326 * Timeout table[state]
328 static int tcp_timeouts[IP_VS_TCP_S_LAST+1] = {
329 [IP_VS_TCP_S_NONE] = 2*HZ,
330 [IP_VS_TCP_S_ESTABLISHED] = 15*60*HZ,
331 [IP_VS_TCP_S_SYN_SENT] = 2*60*HZ,
332 [IP_VS_TCP_S_SYN_RECV] = 1*60*HZ,
333 [IP_VS_TCP_S_FIN_WAIT] = 2*60*HZ,
334 [IP_VS_TCP_S_TIME_WAIT] = 2*60*HZ,
335 [IP_VS_TCP_S_CLOSE] = 10*HZ,
336 [IP_VS_TCP_S_CLOSE_WAIT] = 60*HZ,
337 [IP_VS_TCP_S_LAST_ACK] = 30*HZ,
338 [IP_VS_TCP_S_LISTEN] = 2*60*HZ,
339 [IP_VS_TCP_S_SYNACK] = 120*HZ,
340 [IP_VS_TCP_S_LAST] = 2*HZ,
343 static char * tcp_state_name_table[IP_VS_TCP_S_LAST+1] = {
344 [IP_VS_TCP_S_NONE] = "NONE",
345 [IP_VS_TCP_S_ESTABLISHED] = "ESTABLISHED",
346 [IP_VS_TCP_S_SYN_SENT] = "SYN_SENT",
347 [IP_VS_TCP_S_SYN_RECV] = "SYN_RECV",
348 [IP_VS_TCP_S_FIN_WAIT] = "FIN_WAIT",
349 [IP_VS_TCP_S_TIME_WAIT] = "TIME_WAIT",
350 [IP_VS_TCP_S_CLOSE] = "CLOSE",
351 [IP_VS_TCP_S_CLOSE_WAIT] = "CLOSE_WAIT",
352 [IP_VS_TCP_S_LAST_ACK] = "LAST_ACK",
353 [IP_VS_TCP_S_LISTEN] = "LISTEN",
354 [IP_VS_TCP_S_SYNACK] = "SYNACK",
355 [IP_VS_TCP_S_LAST] = "BUG!",
358 #define sNO IP_VS_TCP_S_NONE
359 #define sES IP_VS_TCP_S_ESTABLISHED
360 #define sSS IP_VS_TCP_S_SYN_SENT
361 #define sSR IP_VS_TCP_S_SYN_RECV
362 #define sFW IP_VS_TCP_S_FIN_WAIT
363 #define sTW IP_VS_TCP_S_TIME_WAIT
364 #define sCL IP_VS_TCP_S_CLOSE
365 #define sCW IP_VS_TCP_S_CLOSE_WAIT
366 #define sLA IP_VS_TCP_S_LAST_ACK
367 #define sLI IP_VS_TCP_S_LISTEN
368 #define sSA IP_VS_TCP_S_SYNACK
370 struct tcp_states_t {
371 int next_state[IP_VS_TCP_S_LAST];
374 static const char * tcp_state_name(int state)
376 if (state >= IP_VS_TCP_S_LAST)
378 return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?";
381 static struct tcp_states_t tcp_states [] = {
383 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
384 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
385 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW }},
386 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
387 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR }},
390 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
391 /*syn*/ {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR }},
392 /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
393 /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
394 /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
397 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
398 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
399 /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
400 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
401 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
404 static struct tcp_states_t tcp_states_dos [] = {
406 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
407 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSA }},
408 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sSA }},
409 /*ack*/ {{sCL, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }},
410 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
413 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
414 /*syn*/ {{sSS, sES, sSS, sSA, sSS, sSS, sSS, sSS, sSS, sLI, sSA }},
415 /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
416 /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
417 /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
420 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
421 /*syn*/ {{sSA, sES, sES, sSR, sSA, sSA, sSA, sSA, sSA, sSA, sSA }},
422 /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
423 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
424 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
427 static struct tcp_states_t *tcp_state_table = tcp_states;
430 static void tcp_timeout_change(struct ip_vs_protocol *pp, int flags)
432 int on = (flags & 1); /* secure_tcp */
435 ** FIXME: change secure_tcp to independent sysctl var
436 ** or make it per-service or per-app because it is valid
437 ** for most if not for all of the applications. Something
438 ** like "capabilities" (flags) for each object.
440 tcp_state_table = (on? tcp_states_dos : tcp_states);
444 tcp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
446 return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_TCP_S_LAST,
447 tcp_state_name_table, sname, to);
450 static inline int tcp_state_idx(struct tcphdr *th)
464 set_tcp_state(struct ip_vs_protocol *pp, struct ip_vs_conn *cp,
465 int direction, struct tcphdr *th)
468 int new_state = IP_VS_TCP_S_CLOSE;
469 int state_off = tcp_state_off[direction];
472 * Update state offset to INPUT_ONLY if necessary
473 * or delete NO_OUTPUT flag if output packet detected
475 if (cp->flags & IP_VS_CONN_F_NOOUTPUT) {
476 if (state_off == TCP_DIR_OUTPUT)
477 cp->flags &= ~IP_VS_CONN_F_NOOUTPUT;
479 state_off = TCP_DIR_INPUT_ONLY;
482 if ((state_idx = tcp_state_idx(th)) < 0) {
483 IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx);
487 new_state = tcp_state_table[state_off+state_idx].next_state[cp->state];
490 if (new_state != cp->state) {
491 struct ip_vs_dest *dest = cp->dest;
493 IP_VS_DBG(8, "%s %s [%c%c%c%c] %u.%u.%u.%u:%d->"
494 "%u.%u.%u.%u:%d state: %s->%s conn->refcnt:%d\n",
496 (state_off==TCP_DIR_OUTPUT)?"output ":"input ",
501 NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
502 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
503 tcp_state_name(cp->state),
504 tcp_state_name(new_state),
505 atomic_read(&cp->refcnt));
507 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
508 (new_state != IP_VS_TCP_S_ESTABLISHED)) {
509 atomic_dec(&dest->activeconns);
510 atomic_inc(&dest->inactconns);
511 cp->flags |= IP_VS_CONN_F_INACTIVE;
512 } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
513 (new_state == IP_VS_TCP_S_ESTABLISHED)) {
514 atomic_inc(&dest->activeconns);
515 atomic_dec(&dest->inactconns);
516 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
521 cp->timeout = pp->timeout_table[cp->state = new_state];
526 * Handle state transitions
529 tcp_state_transition(struct ip_vs_conn *cp, int direction,
530 const struct sk_buff *skb,
531 struct ip_vs_protocol *pp)
533 struct tcphdr _tcph, *th;
535 #ifdef CONFIG_IP_VS_IPV6
536 int ihl = cp->af == AF_INET ? ip_hdrlen(skb) : sizeof(struct ipv6hdr);
538 int ihl = ip_hdrlen(skb);
541 th = skb_header_pointer(skb, ihl, sizeof(_tcph), &_tcph);
545 spin_lock(&cp->lock);
546 set_tcp_state(pp, cp, direction, th);
547 spin_unlock(&cp->lock);
554 * Hash table for TCP application incarnations
556 #define TCP_APP_TAB_BITS 4
557 #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
558 #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
560 static struct list_head tcp_apps[TCP_APP_TAB_SIZE];
561 static DEFINE_SPINLOCK(tcp_app_lock);
563 static inline __u16 tcp_app_hashkey(__be16 port)
565 return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port)
570 static int tcp_register_app(struct ip_vs_app *inc)
574 __be16 port = inc->port;
577 hash = tcp_app_hashkey(port);
579 spin_lock_bh(&tcp_app_lock);
580 list_for_each_entry(i, &tcp_apps[hash], p_list) {
581 if (i->port == port) {
586 list_add(&inc->p_list, &tcp_apps[hash]);
587 atomic_inc(&ip_vs_protocol_tcp.appcnt);
590 spin_unlock_bh(&tcp_app_lock);
596 tcp_unregister_app(struct ip_vs_app *inc)
598 spin_lock_bh(&tcp_app_lock);
599 atomic_dec(&ip_vs_protocol_tcp.appcnt);
600 list_del(&inc->p_list);
601 spin_unlock_bh(&tcp_app_lock);
606 tcp_app_conn_bind(struct ip_vs_conn *cp)
609 struct ip_vs_app *inc;
612 /* Default binding: bind app only for NAT */
613 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
616 /* Lookup application incarnations and bind the right one */
617 hash = tcp_app_hashkey(cp->vport);
619 spin_lock(&tcp_app_lock);
620 list_for_each_entry(inc, &tcp_apps[hash], p_list) {
621 if (inc->port == cp->vport) {
622 if (unlikely(!ip_vs_app_inc_get(inc)))
624 spin_unlock(&tcp_app_lock);
626 IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->"
627 "%u.%u.%u.%u:%u to app %s on port %u\n",
629 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
630 NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
631 inc->name, ntohs(inc->port));
634 result = inc->init_conn(inc, cp);
638 spin_unlock(&tcp_app_lock);
646 * Set LISTEN timeout. (ip_vs_conn_put will setup timer)
648 void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp)
650 spin_lock(&cp->lock);
651 cp->state = IP_VS_TCP_S_LISTEN;
652 cp->timeout = ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_LISTEN];
653 spin_unlock(&cp->lock);
657 static void ip_vs_tcp_init(struct ip_vs_protocol *pp)
659 IP_VS_INIT_HASH_TABLE(tcp_apps);
660 pp->timeout_table = tcp_timeouts;
664 static void ip_vs_tcp_exit(struct ip_vs_protocol *pp)
669 struct ip_vs_protocol ip_vs_protocol_tcp = {
671 .protocol = IPPROTO_TCP,
672 .num_states = IP_VS_TCP_S_LAST,
674 .appcnt = ATOMIC_INIT(0),
675 .init = ip_vs_tcp_init,
676 .exit = ip_vs_tcp_exit,
677 .register_app = tcp_register_app,
678 .unregister_app = tcp_unregister_app,
679 .conn_schedule = tcp_conn_schedule,
680 .conn_in_get = tcp_conn_in_get,
681 .conn_out_get = tcp_conn_out_get,
682 .snat_handler = tcp_snat_handler,
683 .dnat_handler = tcp_dnat_handler,
684 .csum_check = tcp_csum_check,
685 .state_name = tcp_state_name,
686 .state_transition = tcp_state_transition,
687 .app_conn_bind = tcp_app_conn_bind,
688 .debug_packet = ip_vs_tcpudp_debug_packet,
689 .timeout_change = tcp_timeout_change,
690 .set_state_timeout = tcp_set_state_timeout,