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(const struct sk_buff *skb, struct ip_vs_protocol *pp,
29 const struct iphdr *iph, unsigned int proto_off, int inverse)
31 __be16 _ports[2], *pptr;
33 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
37 if (likely(!inverse)) {
38 return ip_vs_conn_in_get(iph->protocol,
42 return ip_vs_conn_in_get(iph->protocol,
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)
52 __be16 _ports[2], *pptr;
54 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
58 if (likely(!inverse)) {
59 return ip_vs_conn_out_get(iph->protocol,
63 return ip_vs_conn_out_get(iph->protocol,
71 tcp_conn_schedule(struct sk_buff *skb,
72 struct ip_vs_protocol *pp,
73 int *verdict, struct ip_vs_conn **cpp)
75 struct ip_vs_service *svc;
76 struct tcphdr _tcph, *th;
77 struct ip_vs_iphdr iph;
79 ip_vs_fill_iphdr(AF_INET, skb_network_header(skb), &iph);
81 th = skb_header_pointer(skb, iph.len, sizeof(_tcph), &_tcph);
88 (svc = ip_vs_service_get(AF_INET, skb->mark, iph.protocol,
89 &iph.daddr, th->dest))) {
92 * It seems that we are very loaded.
93 * We have to drop this packet :(
95 ip_vs_service_put(svc);
101 * Let the virtual server select a real server for the
102 * incoming connection, and create a connection entry.
104 *cpp = ip_vs_schedule(svc, skb);
106 *verdict = ip_vs_leave(svc, skb, pp);
109 ip_vs_service_put(svc);
116 tcp_fast_csum_update(struct tcphdr *tcph, __be32 oldip, __be32 newip,
117 __be16 oldport, __be16 newport)
120 csum_fold(ip_vs_check_diff4(oldip, newip,
121 ip_vs_check_diff2(oldport, newport,
122 ~csum_unfold(tcph->check))));
127 tcp_snat_handler(struct sk_buff *skb,
128 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
131 const unsigned int tcphoff = ip_hdrlen(skb);
133 /* csum_check requires unshared skb */
134 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
137 if (unlikely(cp->app != NULL)) {
138 /* Some checks before mangling */
139 if (pp->csum_check && !pp->csum_check(skb, pp))
142 /* Call application helper if needed */
143 if (!ip_vs_app_pkt_out(cp, skb))
147 tcph = (void *)ip_hdr(skb) + tcphoff;
148 tcph->source = cp->vport;
150 /* Adjust TCP checksums */
152 /* Only port and addr are changed, do fast csum update */
153 tcp_fast_csum_update(tcph, cp->daddr.ip, cp->vaddr.ip,
154 cp->dport, cp->vport);
155 if (skb->ip_summed == CHECKSUM_COMPLETE)
156 skb->ip_summed = CHECKSUM_NONE;
158 /* full checksum calculation */
160 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
161 tcph->check = csum_tcpudp_magic(cp->vaddr.ip, cp->caddr.ip,
163 cp->protocol, skb->csum);
164 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
165 pp->name, tcph->check,
166 (char*)&(tcph->check) - (char*)tcph);
173 tcp_dnat_handler(struct sk_buff *skb,
174 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
177 const unsigned int tcphoff = ip_hdrlen(skb);
179 /* csum_check requires unshared skb */
180 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
183 if (unlikely(cp->app != NULL)) {
184 /* Some checks before mangling */
185 if (pp->csum_check && !pp->csum_check(skb, pp))
189 * Attempt ip_vs_app call.
190 * It will fix ip_vs_conn and iph ack_seq stuff
192 if (!ip_vs_app_pkt_in(cp, skb))
196 tcph = (void *)ip_hdr(skb) + tcphoff;
197 tcph->dest = cp->dport;
200 * Adjust TCP checksums
203 /* Only port and addr are changed, do fast csum update */
204 tcp_fast_csum_update(tcph, cp->vaddr.ip, cp->daddr.ip,
205 cp->vport, cp->dport);
206 if (skb->ip_summed == CHECKSUM_COMPLETE)
207 skb->ip_summed = CHECKSUM_NONE;
209 /* full checksum calculation */
211 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
212 tcph->check = csum_tcpudp_magic(cp->caddr.ip, cp->daddr.ip,
214 cp->protocol, skb->csum);
215 skb->ip_summed = CHECKSUM_UNNECESSARY;
222 tcp_csum_check(struct sk_buff *skb, struct ip_vs_protocol *pp)
224 const unsigned int tcphoff = ip_hdrlen(skb);
226 switch (skb->ip_summed) {
228 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
229 case CHECKSUM_COMPLETE:
230 if (csum_tcpudp_magic(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
232 ip_hdr(skb)->protocol, skb->csum)) {
233 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
234 "Failed checksum for");
239 /* No need to checksum. */
247 #define TCP_DIR_INPUT 0
248 #define TCP_DIR_OUTPUT 4
249 #define TCP_DIR_INPUT_ONLY 8
251 static const int tcp_state_off[IP_VS_DIR_LAST] = {
252 [IP_VS_DIR_INPUT] = TCP_DIR_INPUT,
253 [IP_VS_DIR_OUTPUT] = TCP_DIR_OUTPUT,
254 [IP_VS_DIR_INPUT_ONLY] = TCP_DIR_INPUT_ONLY,
258 * Timeout table[state]
260 static int tcp_timeouts[IP_VS_TCP_S_LAST+1] = {
261 [IP_VS_TCP_S_NONE] = 2*HZ,
262 [IP_VS_TCP_S_ESTABLISHED] = 15*60*HZ,
263 [IP_VS_TCP_S_SYN_SENT] = 2*60*HZ,
264 [IP_VS_TCP_S_SYN_RECV] = 1*60*HZ,
265 [IP_VS_TCP_S_FIN_WAIT] = 2*60*HZ,
266 [IP_VS_TCP_S_TIME_WAIT] = 2*60*HZ,
267 [IP_VS_TCP_S_CLOSE] = 10*HZ,
268 [IP_VS_TCP_S_CLOSE_WAIT] = 60*HZ,
269 [IP_VS_TCP_S_LAST_ACK] = 30*HZ,
270 [IP_VS_TCP_S_LISTEN] = 2*60*HZ,
271 [IP_VS_TCP_S_SYNACK] = 120*HZ,
272 [IP_VS_TCP_S_LAST] = 2*HZ,
275 static char * tcp_state_name_table[IP_VS_TCP_S_LAST+1] = {
276 [IP_VS_TCP_S_NONE] = "NONE",
277 [IP_VS_TCP_S_ESTABLISHED] = "ESTABLISHED",
278 [IP_VS_TCP_S_SYN_SENT] = "SYN_SENT",
279 [IP_VS_TCP_S_SYN_RECV] = "SYN_RECV",
280 [IP_VS_TCP_S_FIN_WAIT] = "FIN_WAIT",
281 [IP_VS_TCP_S_TIME_WAIT] = "TIME_WAIT",
282 [IP_VS_TCP_S_CLOSE] = "CLOSE",
283 [IP_VS_TCP_S_CLOSE_WAIT] = "CLOSE_WAIT",
284 [IP_VS_TCP_S_LAST_ACK] = "LAST_ACK",
285 [IP_VS_TCP_S_LISTEN] = "LISTEN",
286 [IP_VS_TCP_S_SYNACK] = "SYNACK",
287 [IP_VS_TCP_S_LAST] = "BUG!",
290 #define sNO IP_VS_TCP_S_NONE
291 #define sES IP_VS_TCP_S_ESTABLISHED
292 #define sSS IP_VS_TCP_S_SYN_SENT
293 #define sSR IP_VS_TCP_S_SYN_RECV
294 #define sFW IP_VS_TCP_S_FIN_WAIT
295 #define sTW IP_VS_TCP_S_TIME_WAIT
296 #define sCL IP_VS_TCP_S_CLOSE
297 #define sCW IP_VS_TCP_S_CLOSE_WAIT
298 #define sLA IP_VS_TCP_S_LAST_ACK
299 #define sLI IP_VS_TCP_S_LISTEN
300 #define sSA IP_VS_TCP_S_SYNACK
302 struct tcp_states_t {
303 int next_state[IP_VS_TCP_S_LAST];
306 static const char * tcp_state_name(int state)
308 if (state >= IP_VS_TCP_S_LAST)
310 return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?";
313 static struct tcp_states_t tcp_states [] = {
315 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
316 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
317 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW }},
318 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
319 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR }},
322 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
323 /*syn*/ {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR }},
324 /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
325 /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
326 /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
329 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
330 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
331 /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
332 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
333 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
336 static struct tcp_states_t tcp_states_dos [] = {
338 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
339 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSA }},
340 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sSA }},
341 /*ack*/ {{sCL, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }},
342 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
345 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
346 /*syn*/ {{sSS, sES, sSS, sSA, sSS, sSS, sSS, sSS, sSS, sLI, sSA }},
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*/ {{sSA, sES, sES, sSR, sSA, sSA, sSA, sSA, sSA, sSA, sSA }},
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_state_table = tcp_states;
362 static void tcp_timeout_change(struct ip_vs_protocol *pp, int flags)
364 int on = (flags & 1); /* secure_tcp */
367 ** FIXME: change secure_tcp to independent sysctl var
368 ** or make it per-service or per-app because it is valid
369 ** for most if not for all of the applications. Something
370 ** like "capabilities" (flags) for each object.
372 tcp_state_table = (on? tcp_states_dos : tcp_states);
376 tcp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
378 return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_TCP_S_LAST,
379 tcp_state_name_table, sname, to);
382 static inline int tcp_state_idx(struct tcphdr *th)
396 set_tcp_state(struct ip_vs_protocol *pp, struct ip_vs_conn *cp,
397 int direction, struct tcphdr *th)
400 int new_state = IP_VS_TCP_S_CLOSE;
401 int state_off = tcp_state_off[direction];
404 * Update state offset to INPUT_ONLY if necessary
405 * or delete NO_OUTPUT flag if output packet detected
407 if (cp->flags & IP_VS_CONN_F_NOOUTPUT) {
408 if (state_off == TCP_DIR_OUTPUT)
409 cp->flags &= ~IP_VS_CONN_F_NOOUTPUT;
411 state_off = TCP_DIR_INPUT_ONLY;
414 if ((state_idx = tcp_state_idx(th)) < 0) {
415 IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx);
419 new_state = tcp_state_table[state_off+state_idx].next_state[cp->state];
422 if (new_state != cp->state) {
423 struct ip_vs_dest *dest = cp->dest;
425 IP_VS_DBG(8, "%s %s [%c%c%c%c] %u.%u.%u.%u:%d->"
426 "%u.%u.%u.%u:%d state: %s->%s conn->refcnt:%d\n",
428 (state_off==TCP_DIR_OUTPUT)?"output ":"input ",
433 NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
434 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
435 tcp_state_name(cp->state),
436 tcp_state_name(new_state),
437 atomic_read(&cp->refcnt));
439 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
440 (new_state != IP_VS_TCP_S_ESTABLISHED)) {
441 atomic_dec(&dest->activeconns);
442 atomic_inc(&dest->inactconns);
443 cp->flags |= IP_VS_CONN_F_INACTIVE;
444 } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
445 (new_state == IP_VS_TCP_S_ESTABLISHED)) {
446 atomic_inc(&dest->activeconns);
447 atomic_dec(&dest->inactconns);
448 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
453 cp->timeout = pp->timeout_table[cp->state = new_state];
458 * Handle state transitions
461 tcp_state_transition(struct ip_vs_conn *cp, int direction,
462 const struct sk_buff *skb,
463 struct ip_vs_protocol *pp)
465 struct tcphdr _tcph, *th;
467 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
471 spin_lock(&cp->lock);
472 set_tcp_state(pp, cp, direction, th);
473 spin_unlock(&cp->lock);
480 * Hash table for TCP application incarnations
482 #define TCP_APP_TAB_BITS 4
483 #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
484 #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
486 static struct list_head tcp_apps[TCP_APP_TAB_SIZE];
487 static DEFINE_SPINLOCK(tcp_app_lock);
489 static inline __u16 tcp_app_hashkey(__be16 port)
491 return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port)
496 static int tcp_register_app(struct ip_vs_app *inc)
500 __be16 port = inc->port;
503 hash = tcp_app_hashkey(port);
505 spin_lock_bh(&tcp_app_lock);
506 list_for_each_entry(i, &tcp_apps[hash], p_list) {
507 if (i->port == port) {
512 list_add(&inc->p_list, &tcp_apps[hash]);
513 atomic_inc(&ip_vs_protocol_tcp.appcnt);
516 spin_unlock_bh(&tcp_app_lock);
522 tcp_unregister_app(struct ip_vs_app *inc)
524 spin_lock_bh(&tcp_app_lock);
525 atomic_dec(&ip_vs_protocol_tcp.appcnt);
526 list_del(&inc->p_list);
527 spin_unlock_bh(&tcp_app_lock);
532 tcp_app_conn_bind(struct ip_vs_conn *cp)
535 struct ip_vs_app *inc;
538 /* Default binding: bind app only for NAT */
539 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
542 /* Lookup application incarnations and bind the right one */
543 hash = tcp_app_hashkey(cp->vport);
545 spin_lock(&tcp_app_lock);
546 list_for_each_entry(inc, &tcp_apps[hash], p_list) {
547 if (inc->port == cp->vport) {
548 if (unlikely(!ip_vs_app_inc_get(inc)))
550 spin_unlock(&tcp_app_lock);
552 IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->"
553 "%u.%u.%u.%u:%u to app %s on port %u\n",
555 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
556 NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
557 inc->name, ntohs(inc->port));
560 result = inc->init_conn(inc, cp);
564 spin_unlock(&tcp_app_lock);
572 * Set LISTEN timeout. (ip_vs_conn_put will setup timer)
574 void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp)
576 spin_lock(&cp->lock);
577 cp->state = IP_VS_TCP_S_LISTEN;
578 cp->timeout = ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_LISTEN];
579 spin_unlock(&cp->lock);
583 static void ip_vs_tcp_init(struct ip_vs_protocol *pp)
585 IP_VS_INIT_HASH_TABLE(tcp_apps);
586 pp->timeout_table = tcp_timeouts;
590 static void ip_vs_tcp_exit(struct ip_vs_protocol *pp)
595 struct ip_vs_protocol ip_vs_protocol_tcp = {
597 .protocol = IPPROTO_TCP,
598 .num_states = IP_VS_TCP_S_LAST,
600 .appcnt = ATOMIC_INIT(0),
601 .init = ip_vs_tcp_init,
602 .exit = ip_vs_tcp_exit,
603 .register_app = tcp_register_app,
604 .unregister_app = tcp_unregister_app,
605 .conn_schedule = tcp_conn_schedule,
606 .conn_in_get = tcp_conn_in_get,
607 .conn_out_get = tcp_conn_out_get,
608 .snat_handler = tcp_snat_handler,
609 .dnat_handler = tcp_dnat_handler,
610 .csum_check = tcp_csum_check,
611 .state_name = tcp_state_name,
612 .state_transition = tcp_state_transition,
613 .app_conn_bind = tcp_app_conn_bind,
614 .debug_packet = ip_vs_tcpudp_debug_packet,
615 .timeout_change = tcp_timeout_change,
616 .set_state_timeout = tcp_set_state_timeout,