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
;
78 th
= skb_header_pointer(skb
, ip_hdrlen(skb
), sizeof(_tcph
), &_tcph
);
85 (svc
= ip_vs_service_get(skb
->mark
, ip_hdr(skb
)->protocol
,
86 ip_hdr(skb
)->daddr
, th
->dest
))) {
89 * It seems that we are very loaded.
90 * We have to drop this packet :(
92 ip_vs_service_put(svc
);
98 * Let the virtual server select a real server for the
99 * incoming connection, and create a connection entry.
101 *cpp
= ip_vs_schedule(svc
, skb
);
103 *verdict
= ip_vs_leave(svc
, skb
, pp
);
106 ip_vs_service_put(svc
);
113 tcp_fast_csum_update(struct tcphdr
*tcph
, __be32 oldip
, __be32 newip
,
114 __be16 oldport
, __be16 newport
)
117 csum_fold(ip_vs_check_diff4(oldip
, newip
,
118 ip_vs_check_diff2(oldport
, newport
,
119 ~csum_unfold(tcph
->check
))));
124 tcp_snat_handler(struct sk_buff
*skb
,
125 struct ip_vs_protocol
*pp
, struct ip_vs_conn
*cp
)
128 const unsigned int tcphoff
= ip_hdrlen(skb
);
130 /* csum_check requires unshared skb */
131 if (!skb_make_writable(skb
, tcphoff
+sizeof(*tcph
)))
134 if (unlikely(cp
->app
!= NULL
)) {
135 /* Some checks before mangling */
136 if (pp
->csum_check
&& !pp
->csum_check(skb
, pp
))
139 /* Call application helper if needed */
140 if (!ip_vs_app_pkt_out(cp
, skb
))
144 tcph
= (void *)ip_hdr(skb
) + tcphoff
;
145 tcph
->source
= cp
->vport
;
147 /* Adjust TCP checksums */
149 /* Only port and addr are changed, do fast csum update */
150 tcp_fast_csum_update(tcph
, cp
->daddr
, cp
->vaddr
,
151 cp
->dport
, cp
->vport
);
152 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
153 skb
->ip_summed
= CHECKSUM_NONE
;
155 /* full checksum calculation */
157 skb
->csum
= skb_checksum(skb
, tcphoff
, skb
->len
- tcphoff
, 0);
158 tcph
->check
= csum_tcpudp_magic(cp
->vaddr
, cp
->caddr
,
160 cp
->protocol
, skb
->csum
);
161 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
162 pp
->name
, tcph
->check
,
163 (char*)&(tcph
->check
) - (char*)tcph
);
170 tcp_dnat_handler(struct sk_buff
*skb
,
171 struct ip_vs_protocol
*pp
, struct ip_vs_conn
*cp
)
174 const unsigned int tcphoff
= ip_hdrlen(skb
);
176 /* csum_check requires unshared skb */
177 if (!skb_make_writable(skb
, tcphoff
+sizeof(*tcph
)))
180 if (unlikely(cp
->app
!= NULL
)) {
181 /* Some checks before mangling */
182 if (pp
->csum_check
&& !pp
->csum_check(skb
, 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
, skb
))
193 tcph
= (void *)ip_hdr(skb
) + 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 (skb
->ip_summed
== CHECKSUM_COMPLETE
)
204 skb
->ip_summed
= CHECKSUM_NONE
;
206 /* full checksum calculation */
208 skb
->csum
= skb_checksum(skb
, tcphoff
, skb
->len
- tcphoff
, 0);
209 tcph
->check
= csum_tcpudp_magic(cp
->caddr
, cp
->daddr
,
211 cp
->protocol
, skb
->csum
);
212 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
219 tcp_csum_check(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
)
221 const unsigned int tcphoff
= ip_hdrlen(skb
);
223 switch (skb
->ip_summed
) {
225 skb
->csum
= skb_checksum(skb
, tcphoff
, skb
->len
- tcphoff
, 0);
226 case CHECKSUM_COMPLETE
:
227 if (csum_tcpudp_magic(ip_hdr(skb
)->saddr
, ip_hdr(skb
)->daddr
,
229 ip_hdr(skb
)->protocol
, skb
->csum
)) {
230 IP_VS_DBG_RL_PKT(0, pp
, skb
, 0,
231 "Failed checksum for");
236 /* No need to checksum. */
244 #define TCP_DIR_INPUT 0
245 #define TCP_DIR_OUTPUT 4
246 #define TCP_DIR_INPUT_ONLY 8
248 static const int tcp_state_off
[IP_VS_DIR_LAST
] = {
249 [IP_VS_DIR_INPUT
] = TCP_DIR_INPUT
,
250 [IP_VS_DIR_OUTPUT
] = TCP_DIR_OUTPUT
,
251 [IP_VS_DIR_INPUT_ONLY
] = TCP_DIR_INPUT_ONLY
,
255 * Timeout table[state]
257 static int tcp_timeouts
[IP_VS_TCP_S_LAST
+1] = {
258 [IP_VS_TCP_S_NONE
] = 2*HZ
,
259 [IP_VS_TCP_S_ESTABLISHED
] = 15*60*HZ
,
260 [IP_VS_TCP_S_SYN_SENT
] = 2*60*HZ
,
261 [IP_VS_TCP_S_SYN_RECV
] = 1*60*HZ
,
262 [IP_VS_TCP_S_FIN_WAIT
] = 2*60*HZ
,
263 [IP_VS_TCP_S_TIME_WAIT
] = 2*60*HZ
,
264 [IP_VS_TCP_S_CLOSE
] = 10*HZ
,
265 [IP_VS_TCP_S_CLOSE_WAIT
] = 60*HZ
,
266 [IP_VS_TCP_S_LAST_ACK
] = 30*HZ
,
267 [IP_VS_TCP_S_LISTEN
] = 2*60*HZ
,
268 [IP_VS_TCP_S_SYNACK
] = 120*HZ
,
269 [IP_VS_TCP_S_LAST
] = 2*HZ
,
272 static char * tcp_state_name_table
[IP_VS_TCP_S_LAST
+1] = {
273 [IP_VS_TCP_S_NONE
] = "NONE",
274 [IP_VS_TCP_S_ESTABLISHED
] = "ESTABLISHED",
275 [IP_VS_TCP_S_SYN_SENT
] = "SYN_SENT",
276 [IP_VS_TCP_S_SYN_RECV
] = "SYN_RECV",
277 [IP_VS_TCP_S_FIN_WAIT
] = "FIN_WAIT",
278 [IP_VS_TCP_S_TIME_WAIT
] = "TIME_WAIT",
279 [IP_VS_TCP_S_CLOSE
] = "CLOSE",
280 [IP_VS_TCP_S_CLOSE_WAIT
] = "CLOSE_WAIT",
281 [IP_VS_TCP_S_LAST_ACK
] = "LAST_ACK",
282 [IP_VS_TCP_S_LISTEN
] = "LISTEN",
283 [IP_VS_TCP_S_SYNACK
] = "SYNACK",
284 [IP_VS_TCP_S_LAST
] = "BUG!",
287 #define sNO IP_VS_TCP_S_NONE
288 #define sES IP_VS_TCP_S_ESTABLISHED
289 #define sSS IP_VS_TCP_S_SYN_SENT
290 #define sSR IP_VS_TCP_S_SYN_RECV
291 #define sFW IP_VS_TCP_S_FIN_WAIT
292 #define sTW IP_VS_TCP_S_TIME_WAIT
293 #define sCL IP_VS_TCP_S_CLOSE
294 #define sCW IP_VS_TCP_S_CLOSE_WAIT
295 #define sLA IP_VS_TCP_S_LAST_ACK
296 #define sLI IP_VS_TCP_S_LISTEN
297 #define sSA IP_VS_TCP_S_SYNACK
299 struct tcp_states_t
{
300 int next_state
[IP_VS_TCP_S_LAST
];
303 static const char * tcp_state_name(int state
)
305 if (state
>= IP_VS_TCP_S_LAST
)
307 return tcp_state_name_table
[state
] ? tcp_state_name_table
[state
] : "?";
310 static struct tcp_states_t tcp_states
[] = {
312 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
313 /*syn*/ {{sSR
, sES
, sES
, sSR
, sSR
, sSR
, sSR
, sSR
, sSR
, sSR
, sSR
}},
314 /*fin*/ {{sCL
, sCW
, sSS
, sTW
, sTW
, sTW
, sCL
, sCW
, sLA
, sLI
, sTW
}},
315 /*ack*/ {{sCL
, sES
, sSS
, sES
, sFW
, sTW
, sCL
, sCW
, sCL
, sLI
, sES
}},
316 /*rst*/ {{sCL
, sCL
, sCL
, sSR
, sCL
, sCL
, sCL
, sCL
, sLA
, sLI
, sSR
}},
319 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
320 /*syn*/ {{sSS
, sES
, sSS
, sSR
, sSS
, sSS
, sSS
, sSS
, sSS
, sLI
, sSR
}},
321 /*fin*/ {{sTW
, sFW
, sSS
, sTW
, sFW
, sTW
, sCL
, sTW
, sLA
, sLI
, sTW
}},
322 /*ack*/ {{sES
, sES
, sSS
, sES
, sFW
, sTW
, sCL
, sCW
, sLA
, sES
, sES
}},
323 /*rst*/ {{sCL
, sCL
, sSS
, sCL
, sCL
, sTW
, sCL
, sCL
, sCL
, sCL
, sCL
}},
326 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
327 /*syn*/ {{sSR
, sES
, sES
, sSR
, sSR
, sSR
, sSR
, sSR
, sSR
, sSR
, sSR
}},
328 /*fin*/ {{sCL
, sFW
, sSS
, sTW
, sFW
, sTW
, sCL
, sCW
, sLA
, sLI
, sTW
}},
329 /*ack*/ {{sCL
, sES
, sSS
, sES
, sFW
, sTW
, sCL
, sCW
, sCL
, sLI
, sES
}},
330 /*rst*/ {{sCL
, sCL
, sCL
, sSR
, sCL
, sCL
, sCL
, sCL
, sLA
, sLI
, sCL
}},
333 static struct tcp_states_t tcp_states_dos
[] = {
335 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
336 /*syn*/ {{sSR
, sES
, sES
, sSR
, sSR
, sSR
, sSR
, sSR
, sSR
, sSR
, sSA
}},
337 /*fin*/ {{sCL
, sCW
, sSS
, sTW
, sTW
, sTW
, sCL
, sCW
, sLA
, sLI
, sSA
}},
338 /*ack*/ {{sCL
, sES
, sSS
, sSR
, sFW
, sTW
, sCL
, sCW
, sCL
, sLI
, sSA
}},
339 /*rst*/ {{sCL
, sCL
, sCL
, sSR
, sCL
, sCL
, sCL
, sCL
, sLA
, sLI
, sCL
}},
342 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
343 /*syn*/ {{sSS
, sES
, sSS
, sSA
, sSS
, sSS
, sSS
, sSS
, sSS
, sLI
, sSA
}},
344 /*fin*/ {{sTW
, sFW
, sSS
, sTW
, sFW
, sTW
, sCL
, sTW
, sLA
, sLI
, sTW
}},
345 /*ack*/ {{sES
, sES
, sSS
, sES
, sFW
, sTW
, sCL
, sCW
, sLA
, sES
, sES
}},
346 /*rst*/ {{sCL
, sCL
, sSS
, sCL
, sCL
, sTW
, sCL
, sCL
, sCL
, sCL
, sCL
}},
349 /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
350 /*syn*/ {{sSA
, sES
, sES
, sSR
, sSA
, sSA
, sSA
, sSA
, sSA
, sSA
, sSA
}},
351 /*fin*/ {{sCL
, sFW
, sSS
, sTW
, sFW
, sTW
, sCL
, sCW
, sLA
, sLI
, sTW
}},
352 /*ack*/ {{sCL
, sES
, sSS
, sES
, sFW
, sTW
, sCL
, sCW
, sCL
, sLI
, sES
}},
353 /*rst*/ {{sCL
, sCL
, sCL
, sSR
, sCL
, sCL
, sCL
, sCL
, sLA
, sLI
, sCL
}},
356 static struct tcp_states_t
*tcp_state_table
= tcp_states
;
359 static void tcp_timeout_change(struct ip_vs_protocol
*pp
, int flags
)
361 int on
= (flags
& 1); /* secure_tcp */
364 ** FIXME: change secure_tcp to independent sysctl var
365 ** or make it per-service or per-app because it is valid
366 ** for most if not for all of the applications. Something
367 ** like "capabilities" (flags) for each object.
369 tcp_state_table
= (on
? tcp_states_dos
: tcp_states
);
373 tcp_set_state_timeout(struct ip_vs_protocol
*pp
, char *sname
, int to
)
375 return ip_vs_set_state_timeout(pp
->timeout_table
, IP_VS_TCP_S_LAST
,
376 tcp_state_name_table
, sname
, to
);
379 static inline int tcp_state_idx(struct tcphdr
*th
)
393 set_tcp_state(struct ip_vs_protocol
*pp
, struct ip_vs_conn
*cp
,
394 int direction
, struct tcphdr
*th
)
397 int new_state
= IP_VS_TCP_S_CLOSE
;
398 int state_off
= tcp_state_off
[direction
];
401 * Update state offset to INPUT_ONLY if necessary
402 * or delete NO_OUTPUT flag if output packet detected
404 if (cp
->flags
& IP_VS_CONN_F_NOOUTPUT
) {
405 if (state_off
== TCP_DIR_OUTPUT
)
406 cp
->flags
&= ~IP_VS_CONN_F_NOOUTPUT
;
408 state_off
= TCP_DIR_INPUT_ONLY
;
411 if ((state_idx
= tcp_state_idx(th
)) < 0) {
412 IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx
);
416 new_state
= tcp_state_table
[state_off
+state_idx
].next_state
[cp
->state
];
419 if (new_state
!= cp
->state
) {
420 struct ip_vs_dest
*dest
= cp
->dest
;
422 IP_VS_DBG(8, "%s %s [%c%c%c%c] %u.%u.%u.%u:%d->"
423 "%u.%u.%u.%u:%d state: %s->%s conn->refcnt:%d\n",
425 (state_off
==TCP_DIR_OUTPUT
)?"output ":"input ",
430 NIPQUAD(cp
->daddr
), ntohs(cp
->dport
),
431 NIPQUAD(cp
->caddr
), ntohs(cp
->cport
),
432 tcp_state_name(cp
->state
),
433 tcp_state_name(new_state
),
434 atomic_read(&cp
->refcnt
));
436 if (!(cp
->flags
& IP_VS_CONN_F_INACTIVE
) &&
437 (new_state
!= IP_VS_TCP_S_ESTABLISHED
)) {
438 atomic_dec(&dest
->activeconns
);
439 atomic_inc(&dest
->inactconns
);
440 cp
->flags
|= IP_VS_CONN_F_INACTIVE
;
441 } else if ((cp
->flags
& IP_VS_CONN_F_INACTIVE
) &&
442 (new_state
== IP_VS_TCP_S_ESTABLISHED
)) {
443 atomic_inc(&dest
->activeconns
);
444 atomic_dec(&dest
->inactconns
);
445 cp
->flags
&= ~IP_VS_CONN_F_INACTIVE
;
450 cp
->timeout
= pp
->timeout_table
[cp
->state
= new_state
];
455 * Handle state transitions
458 tcp_state_transition(struct ip_vs_conn
*cp
, int direction
,
459 const struct sk_buff
*skb
,
460 struct ip_vs_protocol
*pp
)
462 struct tcphdr _tcph
, *th
;
464 th
= skb_header_pointer(skb
, ip_hdrlen(skb
), sizeof(_tcph
), &_tcph
);
468 spin_lock(&cp
->lock
);
469 set_tcp_state(pp
, cp
, direction
, th
);
470 spin_unlock(&cp
->lock
);
477 * Hash table for TCP application incarnations
479 #define TCP_APP_TAB_BITS 4
480 #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
481 #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
483 static struct list_head tcp_apps
[TCP_APP_TAB_SIZE
];
484 static DEFINE_SPINLOCK(tcp_app_lock
);
486 static inline __u16
tcp_app_hashkey(__be16 port
)
488 return (((__force u16
)port
>> TCP_APP_TAB_BITS
) ^ (__force u16
)port
)
493 static int tcp_register_app(struct ip_vs_app
*inc
)
497 __be16 port
= inc
->port
;
500 hash
= tcp_app_hashkey(port
);
502 spin_lock_bh(&tcp_app_lock
);
503 list_for_each_entry(i
, &tcp_apps
[hash
], p_list
) {
504 if (i
->port
== port
) {
509 list_add(&inc
->p_list
, &tcp_apps
[hash
]);
510 atomic_inc(&ip_vs_protocol_tcp
.appcnt
);
513 spin_unlock_bh(&tcp_app_lock
);
519 tcp_unregister_app(struct ip_vs_app
*inc
)
521 spin_lock_bh(&tcp_app_lock
);
522 atomic_dec(&ip_vs_protocol_tcp
.appcnt
);
523 list_del(&inc
->p_list
);
524 spin_unlock_bh(&tcp_app_lock
);
529 tcp_app_conn_bind(struct ip_vs_conn
*cp
)
532 struct ip_vs_app
*inc
;
535 /* Default binding: bind app only for NAT */
536 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
)
539 /* Lookup application incarnations and bind the right one */
540 hash
= tcp_app_hashkey(cp
->vport
);
542 spin_lock(&tcp_app_lock
);
543 list_for_each_entry(inc
, &tcp_apps
[hash
], p_list
) {
544 if (inc
->port
== cp
->vport
) {
545 if (unlikely(!ip_vs_app_inc_get(inc
)))
547 spin_unlock(&tcp_app_lock
);
549 IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->"
550 "%u.%u.%u.%u:%u to app %s on port %u\n",
552 NIPQUAD(cp
->caddr
), ntohs(cp
->cport
),
553 NIPQUAD(cp
->vaddr
), ntohs(cp
->vport
),
554 inc
->name
, ntohs(inc
->port
));
557 result
= inc
->init_conn(inc
, cp
);
561 spin_unlock(&tcp_app_lock
);
569 * Set LISTEN timeout. (ip_vs_conn_put will setup timer)
571 void ip_vs_tcp_conn_listen(struct ip_vs_conn
*cp
)
573 spin_lock(&cp
->lock
);
574 cp
->state
= IP_VS_TCP_S_LISTEN
;
575 cp
->timeout
= ip_vs_protocol_tcp
.timeout_table
[IP_VS_TCP_S_LISTEN
];
576 spin_unlock(&cp
->lock
);
580 static void ip_vs_tcp_init(struct ip_vs_protocol
*pp
)
582 IP_VS_INIT_HASH_TABLE(tcp_apps
);
583 pp
->timeout_table
= tcp_timeouts
;
587 static void ip_vs_tcp_exit(struct ip_vs_protocol
*pp
)
592 struct ip_vs_protocol ip_vs_protocol_tcp
= {
594 .protocol
= IPPROTO_TCP
,
595 .num_states
= IP_VS_TCP_S_LAST
,
597 .appcnt
= ATOMIC_INIT(0),
598 .init
= ip_vs_tcp_init
,
599 .exit
= ip_vs_tcp_exit
,
600 .register_app
= tcp_register_app
,
601 .unregister_app
= tcp_unregister_app
,
602 .conn_schedule
= tcp_conn_schedule
,
603 .conn_in_get
= tcp_conn_in_get
,
604 .conn_out_get
= tcp_conn_out_get
,
605 .snat_handler
= tcp_snat_handler
,
606 .dnat_handler
= tcp_dnat_handler
,
607 .csum_check
= tcp_csum_check
,
608 .state_name
= tcp_state_name
,
609 .state_transition
= tcp_state_transition
,
610 .app_conn_bind
= tcp_app_conn_bind
,
611 .debug_packet
= ip_vs_tcpudp_debug_packet
,
612 .timeout_change
= tcp_timeout_change
,
613 .set_state_timeout
= tcp_set_state_timeout
,