blob: d0ea467986a017e9ea45779aa7b0b75ff77b6a4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ip_vs_proto_tcp.c: TCP load balancing support for IPVS
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
6 *
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.
11 *
12 * Changes:
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/ip.h>
18#include <linux/tcp.h> /* for tcphdr */
19#include <net/ip.h>
20#include <net/tcp.h> /* for csum_tcpudp_magic */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -070021#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/netfilter_ipv4.h>
23
24#include <net/ip_vs.h>
25
26
27static struct ip_vs_conn *
28tcp_conn_in_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
29 const struct iphdr *iph, unsigned int proto_off, int inverse)
30{
Al Viro014d7302006-09-28 14:29:52 -070031 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
34 if (pptr == NULL)
35 return NULL;
36
37 if (likely(!inverse)) {
38 return ip_vs_conn_in_get(iph->protocol,
39 iph->saddr, pptr[0],
40 iph->daddr, pptr[1]);
41 } else {
42 return ip_vs_conn_in_get(iph->protocol,
43 iph->daddr, pptr[1],
44 iph->saddr, pptr[0]);
45 }
46}
47
48static struct ip_vs_conn *
49tcp_conn_out_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
50 const struct iphdr *iph, unsigned int proto_off, int inverse)
51{
Al Viro014d7302006-09-28 14:29:52 -070052 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
55 if (pptr == NULL)
56 return NULL;
57
58 if (likely(!inverse)) {
59 return ip_vs_conn_out_get(iph->protocol,
60 iph->saddr, pptr[0],
61 iph->daddr, pptr[1]);
62 } else {
63 return ip_vs_conn_out_get(iph->protocol,
64 iph->daddr, pptr[1],
65 iph->saddr, pptr[0]);
66 }
67}
68
69
70static int
71tcp_conn_schedule(struct sk_buff *skb,
72 struct ip_vs_protocol *pp,
73 int *verdict, struct ip_vs_conn **cpp)
74{
75 struct ip_vs_service *svc;
76 struct tcphdr _tcph, *th;
77
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030078 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (th == NULL) {
80 *verdict = NF_DROP;
81 return 0;
82 }
83
84 if (th->syn &&
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070085 (svc = ip_vs_service_get(skb->mark, ip_hdr(skb)->protocol,
86 ip_hdr(skb)->daddr, th->dest))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (ip_vs_todrop()) {
88 /*
89 * It seems that we are very loaded.
90 * We have to drop this packet :(
91 */
92 ip_vs_service_put(svc);
93 *verdict = NF_DROP;
94 return 0;
95 }
96
97 /*
98 * Let the virtual server select a real server for the
99 * incoming connection, and create a connection entry.
100 */
101 *cpp = ip_vs_schedule(svc, skb);
102 if (!*cpp) {
103 *verdict = ip_vs_leave(svc, skb, pp);
104 return 0;
105 }
106 ip_vs_service_put(svc);
107 }
108 return 1;
109}
110
111
112static inline void
Al Viro014d7302006-09-28 14:29:52 -0700113tcp_fast_csum_update(struct tcphdr *tcph, __be32 oldip, __be32 newip,
114 __be16 oldport, __be16 newport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 tcph->check =
Al Virof9214b22006-11-16 02:41:18 -0800117 csum_fold(ip_vs_check_diff4(oldip, newip,
118 ip_vs_check_diff2(oldport, newport,
119 ~csum_unfold(tcph->check))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122
123static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700124tcp_snat_handler(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
126{
127 struct tcphdr *tcph;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700128 const unsigned int tcphoff = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 /* csum_check requires unshared skb */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700131 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 0;
133
134 if (unlikely(cp->app != NULL)) {
135 /* Some checks before mangling */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700136 if (pp->csum_check && !pp->csum_check(skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return 0;
138
139 /* Call application helper if needed */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700140 if (!ip_vs_app_pkt_out(cp, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return 0;
142 }
143
Herbert Xu3db05fe2007-10-15 00:53:15 -0700144 tcph = (void *)ip_hdr(skb) + tcphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 tcph->source = cp->vport;
146
147 /* Adjust TCP checksums */
148 if (!cp->app) {
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);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700152 if (skb->ip_summed == CHECKSUM_COMPLETE)
153 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 } else {
155 /* full checksum calculation */
156 tcph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700157 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 tcph->check = csum_tcpudp_magic(cp->vaddr, cp->caddr,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700159 skb->len - tcphoff,
160 cp->protocol, skb->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
162 pp->name, tcph->check,
163 (char*)&(tcph->check) - (char*)tcph);
164 }
165 return 1;
166}
167
168
169static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700170tcp_dnat_handler(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
172{
173 struct tcphdr *tcph;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700174 const unsigned int tcphoff = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /* csum_check requires unshared skb */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700177 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return 0;
179
180 if (unlikely(cp->app != NULL)) {
181 /* Some checks before mangling */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700182 if (pp->csum_check && !pp->csum_check(skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return 0;
184
185 /*
186 * Attempt ip_vs_app call.
187 * It will fix ip_vs_conn and iph ack_seq stuff
188 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700189 if (!ip_vs_app_pkt_in(cp, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
191 }
192
Herbert Xu3db05fe2007-10-15 00:53:15 -0700193 tcph = (void *)ip_hdr(skb) + tcphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 tcph->dest = cp->dport;
195
196 /*
197 * Adjust TCP checksums
198 */
199 if (!cp->app) {
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);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700203 if (skb->ip_summed == CHECKSUM_COMPLETE)
204 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 } else {
206 /* full checksum calculation */
207 tcph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700208 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 tcph->check = csum_tcpudp_magic(cp->caddr, cp->daddr,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700210 skb->len - tcphoff,
211 cp->protocol, skb->csum);
212 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 return 1;
215}
216
217
218static int
219tcp_csum_check(struct sk_buff *skb, struct ip_vs_protocol *pp)
220{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300221 const unsigned int tcphoff = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 switch (skb->ip_summed) {
224 case CHECKSUM_NONE:
225 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700226 case CHECKSUM_COMPLETE:
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700227 if (csum_tcpudp_magic(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 skb->len - tcphoff,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700229 ip_hdr(skb)->protocol, skb->csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
231 "Failed checksum for");
232 return 0;
233 }
234 break;
235 default:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700236 /* No need to checksum. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 break;
238 }
239
240 return 1;
241}
242
243
244#define TCP_DIR_INPUT 0
245#define TCP_DIR_OUTPUT 4
246#define TCP_DIR_INPUT_ONLY 8
247
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -0800248static const int tcp_state_off[IP_VS_DIR_LAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 [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,
252};
253
254/*
255 * Timeout table[state]
256 */
257static 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,
270};
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static 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!",
285};
286
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
298
299struct tcp_states_t {
300 int next_state[IP_VS_TCP_S_LAST];
301};
302
303static const char * tcp_state_name(int state)
304{
305 if (state >= IP_VS_TCP_S_LAST)
306 return "ERR!";
307 return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?";
308}
309
310static struct tcp_states_t tcp_states [] = {
311/* INPUT */
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 }},
317
318/* OUTPUT */
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 }},
324
325/* INPUT-ONLY */
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 }},
331};
332
333static struct tcp_states_t tcp_states_dos [] = {
334/* INPUT */
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 }},
340
341/* OUTPUT */
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 }},
347
348/* INPUT-ONLY */
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 }},
354};
355
356static struct tcp_states_t *tcp_state_table = tcp_states;
357
358
359static void tcp_timeout_change(struct ip_vs_protocol *pp, int flags)
360{
361 int on = (flags & 1); /* secure_tcp */
362
363 /*
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.
368 */
369 tcp_state_table = (on? tcp_states_dos : tcp_states);
370}
371
372static int
373tcp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
374{
375 return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_TCP_S_LAST,
376 tcp_state_name_table, sname, to);
377}
378
379static inline int tcp_state_idx(struct tcphdr *th)
380{
381 if (th->rst)
382 return 3;
383 if (th->syn)
384 return 0;
385 if (th->fin)
386 return 1;
387 if (th->ack)
388 return 2;
389 return -1;
390}
391
392static inline void
393set_tcp_state(struct ip_vs_protocol *pp, struct ip_vs_conn *cp,
394 int direction, struct tcphdr *th)
395{
396 int state_idx;
397 int new_state = IP_VS_TCP_S_CLOSE;
398 int state_off = tcp_state_off[direction];
399
400 /*
401 * Update state offset to INPUT_ONLY if necessary
402 * or delete NO_OUTPUT flag if output packet detected
403 */
404 if (cp->flags & IP_VS_CONN_F_NOOUTPUT) {
405 if (state_off == TCP_DIR_OUTPUT)
406 cp->flags &= ~IP_VS_CONN_F_NOOUTPUT;
407 else
408 state_off = TCP_DIR_INPUT_ONLY;
409 }
410
411 if ((state_idx = tcp_state_idx(th)) < 0) {
412 IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx);
413 goto tcp_state_out;
414 }
415
416 new_state = tcp_state_table[state_off+state_idx].next_state[cp->state];
417
418 tcp_state_out:
419 if (new_state != cp->state) {
420 struct ip_vs_dest *dest = cp->dest;
421
422 IP_VS_DBG(8, "%s %s [%c%c%c%c] %u.%u.%u.%u:%d->"
Roberto Nibali4b5bdf52006-01-03 14:22:59 -0800423 "%u.%u.%u.%u:%d state: %s->%s conn->refcnt:%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 pp->name,
425 (state_off==TCP_DIR_OUTPUT)?"output ":"input ",
426 th->syn? 'S' : '.',
427 th->fin? 'F' : '.',
428 th->ack? 'A' : '.',
429 th->rst? 'R' : '.',
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));
435 if (dest) {
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;
446 }
447 }
448 }
449
450 cp->timeout = pp->timeout_table[cp->state = new_state];
451}
452
453
454/*
455 * Handle state transitions
456 */
457static int
458tcp_state_transition(struct ip_vs_conn *cp, int direction,
459 const struct sk_buff *skb,
460 struct ip_vs_protocol *pp)
461{
462 struct tcphdr _tcph, *th;
463
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300464 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (th == NULL)
466 return 0;
467
468 spin_lock(&cp->lock);
469 set_tcp_state(pp, cp, direction, th);
470 spin_unlock(&cp->lock);
471
472 return 1;
473}
474
475
476/*
477 * Hash table for TCP application incarnations
478 */
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)
482
483static struct list_head tcp_apps[TCP_APP_TAB_SIZE];
484static DEFINE_SPINLOCK(tcp_app_lock);
485
Al Viro75e7ce62006-11-14 21:13:28 -0800486static inline __u16 tcp_app_hashkey(__be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Al Viro75e7ce62006-11-14 21:13:28 -0800488 return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port)
489 & TCP_APP_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492
493static int tcp_register_app(struct ip_vs_app *inc)
494{
495 struct ip_vs_app *i;
Al Viro75e7ce62006-11-14 21:13:28 -0800496 __u16 hash;
497 __be16 port = inc->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int ret = 0;
499
500 hash = tcp_app_hashkey(port);
501
502 spin_lock_bh(&tcp_app_lock);
503 list_for_each_entry(i, &tcp_apps[hash], p_list) {
504 if (i->port == port) {
505 ret = -EEXIST;
506 goto out;
507 }
508 }
509 list_add(&inc->p_list, &tcp_apps[hash]);
510 atomic_inc(&ip_vs_protocol_tcp.appcnt);
511
512 out:
513 spin_unlock_bh(&tcp_app_lock);
514 return ret;
515}
516
517
518static void
519tcp_unregister_app(struct ip_vs_app *inc)
520{
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);
525}
526
527
528static int
529tcp_app_conn_bind(struct ip_vs_conn *cp)
530{
531 int hash;
532 struct ip_vs_app *inc;
533 int result = 0;
534
535 /* Default binding: bind app only for NAT */
536 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
537 return 0;
538
539 /* Lookup application incarnations and bind the right one */
540 hash = tcp_app_hashkey(cp->vport);
541
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)))
546 break;
547 spin_unlock(&tcp_app_lock);
548
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",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800551 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 NIPQUAD(cp->caddr), ntohs(cp->cport),
553 NIPQUAD(cp->vaddr), ntohs(cp->vport),
554 inc->name, ntohs(inc->port));
555 cp->app = inc;
556 if (inc->init_conn)
557 result = inc->init_conn(inc, cp);
558 goto out;
559 }
560 }
561 spin_unlock(&tcp_app_lock);
562
563 out:
564 return result;
565}
566
567
568/*
569 * Set LISTEN timeout. (ip_vs_conn_put will setup timer)
570 */
571void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp)
572{
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);
577}
578
579
David S. Millerba602a82005-08-16 20:50:16 -0700580static void ip_vs_tcp_init(struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 IP_VS_INIT_HASH_TABLE(tcp_apps);
583 pp->timeout_table = tcp_timeouts;
584}
585
586
David S. Millerba602a82005-08-16 20:50:16 -0700587static void ip_vs_tcp_exit(struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589}
590
591
592struct ip_vs_protocol ip_vs_protocol_tcp = {
593 .name = "TCP",
594 .protocol = IPPROTO_TCP,
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700595 .num_states = IP_VS_TCP_S_LAST,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 .dont_defrag = 0,
597 .appcnt = ATOMIC_INIT(0),
David S. Millerba602a82005-08-16 20:50:16 -0700598 .init = ip_vs_tcp_init,
599 .exit = ip_vs_tcp_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 .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,
614};