2 * ip_vs_proto_tcp.c: TCP load balancing support for IPVS
4 * Version: $Id: ip_vs_proto_tcp.c,v 1.3 2002/11/30 01:50:35 wensong Exp $
6 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
7 * Julian Anastasov <ja@ssi.bg>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
18 #include <linux/kernel.h>
20 #include <linux/tcp.h> /* for tcphdr */
22 #include <net/tcp.h> /* for csum_tcpudp_magic */
23 #include <linux/netfilter_ipv4.h>
25 #include <net/ip_vs.h>
28 static struct ip_vs_conn *
29 tcp_conn_in_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
30 const struct iphdr *iph, unsigned int proto_off, int inverse)
34 if (skb_copy_bits(skb, proto_off, ports, sizeof(ports)) < 0)
37 if (likely(!inverse)) {
38 return ip_vs_conn_in_get(iph->protocol,
40 iph->daddr, ports[1]);
42 return ip_vs_conn_in_get(iph->protocol,
44 iph->saddr, ports[0]);
48 static struct ip_vs_conn *
49 tcp_conn_out_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
50 const struct iphdr *iph, unsigned int proto_off, int inverse)
54 if (skb_copy_bits(skb, proto_off, ports, sizeof(ports)) < 0)
57 if (likely(!inverse)) {
58 return ip_vs_conn_out_get(iph->protocol,
60 iph->daddr, ports[1]);
62 return ip_vs_conn_out_get(iph->protocol,
64 iph->saddr, ports[0]);
70 tcp_conn_schedule(struct sk_buff *skb,
71 struct ip_vs_protocol *pp,
72 int *verdict, struct ip_vs_conn **cpp)
74 struct ip_vs_service *svc;
77 if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0) {
83 (svc = ip_vs_service_get(skb->nfmark, skb->nh.iph->protocol,
84 skb->nh.iph->daddr, tcph.dest))) {
87 * It seems that we are very loaded.
88 * We have to drop this packet :(
90 ip_vs_service_put(svc);
96 * Let the virtual server select a real server for the
97 * incoming connection, and create a connection entry.
99 *cpp = ip_vs_schedule(svc, skb);
101 *verdict = ip_vs_leave(svc, skb, pp);
104 ip_vs_service_put(svc);
111 tcp_fast_csum_update(struct tcphdr *tcph, u32 oldip, u32 newip,
112 u16 oldport, u16 newport)
115 ip_vs_check_diff(~oldip, newip,
116 ip_vs_check_diff(oldport ^ 0xFFFF,
117 newport, tcph->check));
122 tcp_snat_handler(struct sk_buff **pskb,
123 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
126 unsigned int tcphoff = (*pskb)->nh.iph->ihl * 4;
128 /* csum_check requires unshared skb */
129 if (!ip_vs_make_skb_writable(pskb, tcphoff+sizeof(*tcph)))
132 if (unlikely(cp->app != NULL)) {
133 /* Some checks before mangling */
134 if (pp->csum_check && !pp->csum_check(*pskb, pp))
137 /* Call application helper if needed */
138 if (!ip_vs_app_pkt_out(cp, pskb))
142 tcph = (void *)(*pskb)->nh.iph + tcphoff;
143 tcph->source = cp->vport;
145 /* Adjust TCP checksums */
147 /* Only port and addr are changed, do fast csum update */
148 tcp_fast_csum_update(tcph, cp->daddr, cp->vaddr,
149 cp->dport, cp->vport);
150 if ((*pskb)->ip_summed == CHECKSUM_HW)
151 (*pskb)->ip_summed = CHECKSUM_NONE;
153 /* full checksum calculation */
155 (*pskb)->csum = skb_checksum(*pskb, tcphoff,
156 (*pskb)->len - tcphoff, 0);
157 tcph->check = csum_tcpudp_magic(cp->vaddr, cp->caddr,
158 (*pskb)->len - tcphoff,
161 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%d)\n",
162 pp->name, tcph->check,
163 (char*)&(tcph->check) - (char*)tcph);
170 tcp_dnat_handler(struct sk_buff **pskb,
171 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
174 unsigned int tcphoff = (*pskb)->nh.iph->ihl * 4;
176 /* csum_check requires unshared skb */
177 if (!ip_vs_make_skb_writable(pskb, tcphoff+sizeof(*tcph)))
180 if (unlikely(cp->app != NULL)) {
181 /* Some checks before mangling */
182 if (pp->csum_check && !pp->csum_check(*pskb, pp))
186 * Attempt ip_vs_app call.
187 * It will fix ip_vs_conn and iph ack_seq stuff
189 if (!ip_vs_app_pkt_in(cp, pskb))
193 tcph = (void *)(*pskb)->nh.iph + tcphoff;
194 tcph->dest = cp->dport;
197 * Adjust TCP checksums
200 /* Only port and addr are changed, do fast csum update */
201 tcp_fast_csum_update(tcph, cp->vaddr, cp->daddr,
202 cp->vport, cp->dport);
203 if ((*pskb)->ip_summed == CHECKSUM_HW)
204 (*pskb)->ip_summed = CHECKSUM_NONE;
206 /* full checksum calculation */
208 (*pskb)->csum = skb_checksum(*pskb, tcphoff,
209 (*pskb)->len - tcphoff, 0);
210 tcph->check = csum_tcpudp_magic(cp->caddr, cp->daddr,
211 (*pskb)->len - tcphoff,
214 (*pskb)->ip_summed = CHECKSUM_UNNECESSARY;
221 tcp_csum_check(struct sk_buff *skb, struct ip_vs_protocol *pp)
223 unsigned int tcphoff = skb->nh.iph->ihl*4;
225 switch (skb->ip_summed) {
227 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
229 if (csum_tcpudp_magic(skb->nh.iph->saddr, skb->nh.iph->daddr,
231 skb->nh.iph->protocol, skb->csum)) {
232 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
233 "Failed checksum for");
238 /* CHECKSUM_UNNECESSARY */
246 #define TCP_DIR_INPUT 0
247 #define TCP_DIR_OUTPUT 4
248 #define TCP_DIR_INPUT_ONLY 8
250 static int tcp_state_off[IP_VS_DIR_LAST] = {
251 [IP_VS_DIR_INPUT] = TCP_DIR_INPUT,
252 [IP_VS_DIR_OUTPUT] = TCP_DIR_OUTPUT,
253 [IP_VS_DIR_INPUT_ONLY] = TCP_DIR_INPUT_ONLY,
257 * Timeout table[state]
259 static int tcp_timeouts[IP_VS_TCP_S_LAST+1] = {
260 [IP_VS_TCP_S_NONE] = 2*HZ,
261 [IP_VS_TCP_S_ESTABLISHED] = 15*60*HZ,
262 [IP_VS_TCP_S_SYN_SENT] = 2*60*HZ,
263 [IP_VS_TCP_S_SYN_RECV] = 1*60*HZ,
264 [IP_VS_TCP_S_FIN_WAIT] = 2*60*HZ,
265 [IP_VS_TCP_S_TIME_WAIT] = 2*60*HZ,
266 [IP_VS_TCP_S_CLOSE] = 10*HZ,
267 [IP_VS_TCP_S_CLOSE_WAIT] = 60*HZ,
268 [IP_VS_TCP_S_LAST_ACK] = 30*HZ,
269 [IP_VS_TCP_S_LISTEN] = 2*60*HZ,
270 [IP_VS_TCP_S_SYNACK] = 120*HZ,
271 [IP_VS_TCP_S_LAST] = 2*HZ,
277 /* FIXME: This is going to die */
279 static int tcp_timeouts_dos[IP_VS_TCP_S_LAST+1] = {
280 [IP_VS_TCP_S_NONE] = 2*HZ,
281 [IP_VS_TCP_S_ESTABLISHED] = 8*60*HZ,
282 [IP_VS_TCP_S_SYN_SENT] = 60*HZ,
283 [IP_VS_TCP_S_SYN_RECV] = 10*HZ,
284 [IP_VS_TCP_S_FIN_WAIT] = 60*HZ,
285 [IP_VS_TCP_S_TIME_WAIT] = 60*HZ,
286 [IP_VS_TCP_S_CLOSE] = 10*HZ,
287 [IP_VS_TCP_S_CLOSE_WAIT] = 60*HZ,
288 [IP_VS_TCP_S_LAST_ACK] = 30*HZ,
289 [IP_VS_TCP_S_LISTEN] = 2*60*HZ,
290 [IP_VS_TCP_S_SYNACK] = 100*HZ,
291 [IP_VS_TCP_S_LAST] = 2*HZ,
296 static char * tcp_state_name_table[IP_VS_TCP_S_LAST+1] = {
297 [IP_VS_TCP_S_NONE] = "NONE",
298 [IP_VS_TCP_S_ESTABLISHED] = "ESTABLISHED",
299 [IP_VS_TCP_S_SYN_SENT] = "SYN_SENT",
300 [IP_VS_TCP_S_SYN_RECV] = "SYN_RECV",
301 [IP_VS_TCP_S_FIN_WAIT] = "FIN_WAIT",
302 [IP_VS_TCP_S_TIME_WAIT] = "TIME_WAIT",
303 [IP_VS_TCP_S_CLOSE] = "CLOSE",
304 [IP_VS_TCP_S_CLOSE_WAIT] = "CLOSE_WAIT",
305 [IP_VS_TCP_S_LAST_ACK] = "LAST_ACK",
306 [IP_VS_TCP_S_LISTEN] = "LISTEN",
307 [IP_VS_TCP_S_SYNACK] = "SYNACK",
308 [IP_VS_TCP_S_LAST] = "BUG!",
311 #define sNO IP_VS_TCP_S_NONE
312 #define sES IP_VS_TCP_S_ESTABLISHED
313 #define sSS IP_VS_TCP_S_SYN_SENT
314 #define sSR IP_VS_TCP_S_SYN_RECV
315 #define sFW IP_VS_TCP_S_FIN_WAIT
316 #define sTW IP_VS_TCP_S_TIME_WAIT
317 #define sCL IP_VS_TCP_S_CLOSE
318 #define sCW IP_VS_TCP_S_CLOSE_WAIT
319 #define sLA IP_VS_TCP_S_LAST_ACK
320 #define sLI IP_VS_TCP_S_LISTEN
321 #define sSA IP_VS_TCP_S_SYNACK
323 struct tcp_states_t {
324 int next_state[IP_VS_TCP_S_LAST];
327 static const char * tcp_state_name(int state)
329 if (state >= IP_VS_TCP_S_LAST)
331 return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?";
334 static struct tcp_states_t tcp_states [] = {
336 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
337 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
338 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW }},
339 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
340 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR }},
343 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
344 /*syn*/ {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR }},
345 /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
346 /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
347 /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
350 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
351 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
352 /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
353 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
354 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
357 static struct tcp_states_t tcp_states_dos [] = {
359 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
360 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSA }},
361 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sSA }},
362 /*ack*/ {{sCL, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }},
363 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
366 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
367 /*syn*/ {{sSS, sES, sSS, sSA, sSS, sSS, sSS, sSS, sSS, sLI, sSA }},
368 /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
369 /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
370 /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
373 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
374 /*syn*/ {{sSA, sES, sES, sSR, sSA, sSA, sSA, sSA, sSA, sSA, sSA }},
375 /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
376 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
377 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
380 static struct tcp_states_t *tcp_state_table = tcp_states;
383 static void tcp_timeout_change(struct ip_vs_protocol *pp, int flags)
385 int on = (flags & 1); /* secure_tcp */
388 ** FIXME: change secure_tcp to independent sysctl var
389 ** or make it per-service or per-app because it is valid
390 ** for most if not for all of the applications. Something
391 ** like "capabilities" (flags) for each object.
393 tcp_state_table = (on? tcp_states_dos : tcp_states);
397 tcp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
399 return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_TCP_S_LAST,
400 tcp_state_name_table, sname, to);
403 static inline int tcp_state_idx(struct tcphdr *th)
417 set_tcp_state(struct ip_vs_protocol *pp, struct ip_vs_conn *cp,
418 int direction, struct tcphdr *th)
421 int new_state = IP_VS_TCP_S_CLOSE;
422 int state_off = tcp_state_off[direction];
425 * Update state offset to INPUT_ONLY if necessary
426 * or delete NO_OUTPUT flag if output packet detected
428 if (cp->flags & IP_VS_CONN_F_NOOUTPUT) {
429 if (state_off == TCP_DIR_OUTPUT)
430 cp->flags &= ~IP_VS_CONN_F_NOOUTPUT;
432 state_off = TCP_DIR_INPUT_ONLY;
435 if ((state_idx = tcp_state_idx(th)) < 0) {
436 IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx);
440 new_state = tcp_state_table[state_off+state_idx].next_state[cp->state];
443 if (new_state != cp->state) {
444 struct ip_vs_dest *dest = cp->dest;
446 IP_VS_DBG(8, "%s %s [%c%c%c%c] %u.%u.%u.%u:%d->"
447 "%u.%u.%u.%u:%d state: %s->%s cnt:%d\n",
449 (state_off==TCP_DIR_OUTPUT)?"output ":"input ",
454 NIPQUAD(cp->daddr), ntohs(cp->dport),
455 NIPQUAD(cp->caddr), ntohs(cp->cport),
456 tcp_state_name(cp->state),
457 tcp_state_name(new_state),
458 atomic_read(&cp->refcnt));
460 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
461 (new_state != IP_VS_TCP_S_ESTABLISHED)) {
462 atomic_dec(&dest->activeconns);
463 atomic_inc(&dest->inactconns);
464 cp->flags |= IP_VS_CONN_F_INACTIVE;
465 } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
466 (new_state == IP_VS_TCP_S_ESTABLISHED)) {
467 atomic_inc(&dest->activeconns);
468 atomic_dec(&dest->inactconns);
469 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
474 cp->timeout = pp->timeout_table[cp->state = new_state];
479 * Handle state transitions
482 tcp_state_transition(struct ip_vs_conn *cp, int direction,
483 const struct sk_buff *skb,
484 struct ip_vs_protocol *pp)
488 if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
491 spin_lock(&cp->lock);
492 set_tcp_state(pp, cp, direction, &tcph);
493 spin_unlock(&cp->lock);
500 * Hash table for TCP application incarnations
502 #define TCP_APP_TAB_BITS 4
503 #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
504 #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
506 static struct list_head tcp_apps[TCP_APP_TAB_SIZE];
507 static spinlock_t tcp_app_lock = SPIN_LOCK_UNLOCKED;
509 static inline __u16 tcp_app_hashkey(__u16 port)
511 return ((port >> TCP_APP_TAB_BITS) ^ port) & TCP_APP_TAB_MASK;
515 static int tcp_register_app(struct ip_vs_app *inc)
518 __u16 hash, port = inc->port;
521 hash = tcp_app_hashkey(port);
523 spin_lock_bh(&tcp_app_lock);
524 list_for_each_entry(i, &tcp_apps[hash], p_list) {
525 if (i->port == port) {
530 list_add(&inc->p_list, &tcp_apps[hash]);
531 atomic_inc(&ip_vs_protocol_tcp.appcnt);
534 spin_unlock_bh(&tcp_app_lock);
540 tcp_unregister_app(struct ip_vs_app *inc)
542 spin_lock_bh(&tcp_app_lock);
543 atomic_dec(&ip_vs_protocol_tcp.appcnt);
544 list_del(&inc->p_list);
545 spin_unlock_bh(&tcp_app_lock);
550 tcp_app_conn_bind(struct ip_vs_conn *cp)
553 struct ip_vs_app *inc;
556 /* Default binding: bind app only for NAT */
557 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
560 /* Lookup application incarnations and bind the right one */
561 hash = tcp_app_hashkey(cp->vport);
563 spin_lock(&tcp_app_lock);
564 list_for_each_entry(inc, &tcp_apps[hash], p_list) {
565 if (inc->port == cp->vport) {
566 if (unlikely(!ip_vs_app_inc_get(inc)))
568 spin_unlock(&tcp_app_lock);
570 IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->"
571 "%u.%u.%u.%u:%u to app %s on port %u\n",
573 NIPQUAD(cp->caddr), ntohs(cp->cport),
574 NIPQUAD(cp->vaddr), ntohs(cp->vport),
575 inc->name, ntohs(inc->port));
578 result = inc->init_conn(inc, cp);
582 spin_unlock(&tcp_app_lock);
590 * Set LISTEN timeout. (ip_vs_conn_put will setup timer)
592 void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp)
594 spin_lock(&cp->lock);
595 cp->state = IP_VS_TCP_S_LISTEN;
596 cp->timeout = ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_LISTEN];
597 spin_unlock(&cp->lock);
601 static void tcp_init(struct ip_vs_protocol *pp)
603 IP_VS_INIT_HASH_TABLE(tcp_apps);
604 pp->timeout_table = tcp_timeouts;
608 static void tcp_exit(struct ip_vs_protocol *pp)
613 struct ip_vs_protocol ip_vs_protocol_tcp = {
615 .protocol = IPPROTO_TCP,
617 .appcnt = ATOMIC_INIT(0),
620 .register_app = tcp_register_app,
621 .unregister_app = tcp_unregister_app,
622 .conn_schedule = tcp_conn_schedule,
623 .conn_in_get = tcp_conn_in_get,
624 .conn_out_get = tcp_conn_out_get,
625 .snat_handler = tcp_snat_handler,
626 .dnat_handler = tcp_dnat_handler,
627 .csum_check = tcp_csum_check,
628 .state_name = tcp_state_name,
629 .state_transition = tcp_state_transition,
630 .app_conn_bind = tcp_app_conn_bind,
631 .debug_packet = ip_vs_tcpudp_debug_packet,
632 .timeout_change = tcp_timeout_change,
633 .set_state_timeout = tcp_set_state_timeout,