]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv4/ipvs/ip_vs_proto_tcp.c
IPVS: Adjust various debug outputs to use new macros
[net-next-2.6.git] / net / ipv4 / ipvs / ip_vs_proto_tcp.c
1 /*
2  * ip_vs_proto_tcp.c:   TCP load balancing support for IPVS
3  *
4  * 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 */
21 #include <linux/netfilter.h>
22 #include <linux/netfilter_ipv4.h>
23
24 #include <net/ip_vs.h>
25
26
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,
30                 int inverse)
31 {
32         __be16 _ports[2], *pptr;
33
34         pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
35         if (pptr == NULL)
36                 return NULL;
37
38         if (likely(!inverse)) {
39                 return ip_vs_conn_in_get(af, iph->protocol,
40                                          &iph->saddr, pptr[0],
41                                          &iph->daddr, pptr[1]);
42         } else {
43                 return ip_vs_conn_in_get(af, iph->protocol,
44                                          &iph->daddr, pptr[1],
45                                          &iph->saddr, pptr[0]);
46         }
47 }
48
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,
52                  int inverse)
53 {
54         __be16 _ports[2], *pptr;
55
56         pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
57         if (pptr == NULL)
58                 return NULL;
59
60         if (likely(!inverse)) {
61                 return ip_vs_conn_out_get(af, iph->protocol,
62                                           &iph->saddr, pptr[0],
63                                           &iph->daddr, pptr[1]);
64         } else {
65                 return ip_vs_conn_out_get(af, iph->protocol,
66                                           &iph->daddr, pptr[1],
67                                           &iph->saddr, pptr[0]);
68         }
69 }
70
71
72 static int
73 tcp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
74                   int *verdict, struct ip_vs_conn **cpp)
75 {
76         struct ip_vs_service *svc;
77         struct tcphdr _tcph, *th;
78         struct ip_vs_iphdr iph;
79
80         ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
81
82         th = skb_header_pointer(skb, iph.len, sizeof(_tcph), &_tcph);
83         if (th == NULL) {
84                 *verdict = NF_DROP;
85                 return 0;
86         }
87
88         if (th->syn &&
89             (svc = ip_vs_service_get(af, skb->mark, iph.protocol, &iph.daddr,
90                                      th->dest))) {
91                 if (ip_vs_todrop()) {
92                         /*
93                          * It seems that we are very loaded.
94                          * We have to drop this packet :(
95                          */
96                         ip_vs_service_put(svc);
97                         *verdict = NF_DROP;
98                         return 0;
99                 }
100
101                 /*
102                  * Let the virtual server select a real server for the
103                  * incoming connection, and create a connection entry.
104                  */
105                 *cpp = ip_vs_schedule(svc, skb);
106                 if (!*cpp) {
107                         *verdict = ip_vs_leave(svc, skb, pp);
108                         return 0;
109                 }
110                 ip_vs_service_put(svc);
111         }
112         return 1;
113 }
114
115
116 static inline void
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)
121 {
122 #ifdef CONFIG_IP_VS_IPV6
123         if (af == AF_INET6)
124                 tcph->check =
125                         csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
126                                          ip_vs_check_diff2(oldport, newport,
127                                                 ~csum_unfold(tcph->check))));
128         else
129 #endif
130         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))));
134 }
135
136
137 static int
138 tcp_snat_handler(struct sk_buff *skb,
139                  struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
140 {
141         struct tcphdr *tcph;
142         unsigned int tcphoff;
143
144 #ifdef CONFIG_IP_VS_IPV6
145         if (cp->af == AF_INET6)
146                 tcphoff = sizeof(struct ipv6hdr);
147         else
148 #endif
149                 tcphoff = ip_hdrlen(skb);
150
151         /* csum_check requires unshared skb */
152         if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
153                 return 0;
154
155         if (unlikely(cp->app != NULL)) {
156                 /* Some checks before mangling */
157                 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
158                         return 0;
159
160                 /* Call application helper if needed */
161                 if (!ip_vs_app_pkt_out(cp, skb))
162                         return 0;
163         }
164
165         tcph = (void *)skb_network_header(skb) + tcphoff;
166         tcph->source = cp->vport;
167
168         /* Adjust TCP checksums */
169         if (!cp->app) {
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;
175         } else {
176                 /* full checksum calculation */
177                 tcph->check = 0;
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,
182                                                       &cp->caddr.in6,
183                                                       skb->len - tcphoff,
184                                                       cp->protocol, skb->csum);
185                 else
186 #endif
187                         tcph->check = csum_tcpudp_magic(cp->vaddr.ip,
188                                                         cp->caddr.ip,
189                                                         skb->len - tcphoff,
190                                                         cp->protocol,
191                                                         skb->csum);
192
193                 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
194                           pp->name, tcph->check,
195                           (char*)&(tcph->check) - (char*)tcph);
196         }
197         return 1;
198 }
199
200
201 static int
202 tcp_dnat_handler(struct sk_buff *skb,
203                  struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
204 {
205         struct tcphdr *tcph;
206         unsigned int tcphoff;
207
208 #ifdef CONFIG_IP_VS_IPV6
209         if (cp->af == AF_INET6)
210                 tcphoff = sizeof(struct ipv6hdr);
211         else
212 #endif
213                 tcphoff = ip_hdrlen(skb);
214
215         /* csum_check requires unshared skb */
216         if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
217                 return 0;
218
219         if (unlikely(cp->app != NULL)) {
220                 /* Some checks before mangling */
221                 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
222                         return 0;
223
224                 /*
225                  *      Attempt ip_vs_app call.
226                  *      It will fix ip_vs_conn and iph ack_seq stuff
227                  */
228                 if (!ip_vs_app_pkt_in(cp, skb))
229                         return 0;
230         }
231
232         tcph = (void *)skb_network_header(skb) + tcphoff;
233         tcph->dest = cp->dport;
234
235         /*
236          *      Adjust TCP checksums
237          */
238         if (!cp->app) {
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;
244         } else {
245                 /* full checksum calculation */
246                 tcph->check = 0;
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,
251                                                       &cp->daddr.in6,
252                                                       skb->len - tcphoff,
253                                                       cp->protocol, skb->csum);
254                 else
255 #endif
256                         tcph->check = csum_tcpudp_magic(cp->caddr.ip,
257                                                         cp->daddr.ip,
258                                                         skb->len - tcphoff,
259                                                         cp->protocol,
260                                                         skb->csum);
261                 skb->ip_summed = CHECKSUM_UNNECESSARY;
262         }
263         return 1;
264 }
265
266
267 static int
268 tcp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
269 {
270         unsigned int tcphoff;
271
272 #ifdef CONFIG_IP_VS_IPV6
273         if (af == AF_INET6)
274                 tcphoff = sizeof(struct ipv6hdr);
275         else
276 #endif
277                 tcphoff = ip_hdrlen(skb);
278
279         switch (skb->ip_summed) {
280         case CHECKSUM_NONE:
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,
287                                             skb->len - tcphoff,
288                                             ipv6_hdr(skb)->nexthdr,
289                                             skb->csum)) {
290                                 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
291                                                  "Failed checksum for");
292                                 return 0;
293                         }
294                 } else
295 #endif
296                         if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
297                                               ip_hdr(skb)->daddr,
298                                               skb->len - tcphoff,
299                                               ip_hdr(skb)->protocol,
300                                               skb->csum)) {
301                                 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
302                                                  "Failed checksum for");
303                                 return 0;
304                         }
305                 break;
306         default:
307                 /* No need to checksum. */
308                 break;
309         }
310
311         return 1;
312 }
313
314
315 #define TCP_DIR_INPUT           0
316 #define TCP_DIR_OUTPUT          4
317 #define TCP_DIR_INPUT_ONLY      8
318
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,
323 };
324
325 /*
326  *      Timeout table[state]
327  */
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,
341 };
342
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!",
356 };
357
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
369
370 struct tcp_states_t {
371         int next_state[IP_VS_TCP_S_LAST];
372 };
373
374 static const char * tcp_state_name(int state)
375 {
376         if (state >= IP_VS_TCP_S_LAST)
377                 return "ERR!";
378         return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?";
379 }
380
381 static struct tcp_states_t tcp_states [] = {
382 /*      INPUT */
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 }},
388
389 /*      OUTPUT */
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 }},
395
396 /*      INPUT-ONLY */
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 }},
402 };
403
404 static struct tcp_states_t tcp_states_dos [] = {
405 /*      INPUT */
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 }},
411
412 /*      OUTPUT */
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 }},
418
419 /*      INPUT-ONLY */
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 }},
425 };
426
427 static struct tcp_states_t *tcp_state_table = tcp_states;
428
429
430 static void tcp_timeout_change(struct ip_vs_protocol *pp, int flags)
431 {
432         int on = (flags & 1);           /* secure_tcp */
433
434         /*
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.
439         */
440         tcp_state_table = (on? tcp_states_dos : tcp_states);
441 }
442
443 static int
444 tcp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
445 {
446         return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_TCP_S_LAST,
447                                        tcp_state_name_table, sname, to);
448 }
449
450 static inline int tcp_state_idx(struct tcphdr *th)
451 {
452         if (th->rst)
453                 return 3;
454         if (th->syn)
455                 return 0;
456         if (th->fin)
457                 return 1;
458         if (th->ack)
459                 return 2;
460         return -1;
461 }
462
463 static inline void
464 set_tcp_state(struct ip_vs_protocol *pp, struct ip_vs_conn *cp,
465               int direction, struct tcphdr *th)
466 {
467         int state_idx;
468         int new_state = IP_VS_TCP_S_CLOSE;
469         int state_off = tcp_state_off[direction];
470
471         /*
472          *    Update state offset to INPUT_ONLY if necessary
473          *    or delete NO_OUTPUT flag if output packet detected
474          */
475         if (cp->flags & IP_VS_CONN_F_NOOUTPUT) {
476                 if (state_off == TCP_DIR_OUTPUT)
477                         cp->flags &= ~IP_VS_CONN_F_NOOUTPUT;
478                 else
479                         state_off = TCP_DIR_INPUT_ONLY;
480         }
481
482         if ((state_idx = tcp_state_idx(th)) < 0) {
483                 IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx);
484                 goto tcp_state_out;
485         }
486
487         new_state = tcp_state_table[state_off+state_idx].next_state[cp->state];
488
489   tcp_state_out:
490         if (new_state != cp->state) {
491                 struct ip_vs_dest *dest = cp->dest;
492
493                 IP_VS_DBG_BUF(8, "%s %s [%c%c%c%c] %s:%d->"
494                               "%s:%d state: %s->%s conn->refcnt:%d\n",
495                               pp->name,
496                               ((state_off == TCP_DIR_OUTPUT) ?
497                                "output " : "input "),
498                               th->syn ? 'S' : '.',
499                               th->fin ? 'F' : '.',
500                               th->ack ? 'A' : '.',
501                               th->rst ? 'R' : '.',
502                               IP_VS_DBG_ADDR(cp->af, &cp->daddr),
503                               ntohs(cp->dport),
504                               IP_VS_DBG_ADDR(cp->af, &cp->caddr),
505                               ntohs(cp->cport),
506                               tcp_state_name(cp->state),
507                               tcp_state_name(new_state),
508                               atomic_read(&cp->refcnt));
509
510                 if (dest) {
511                         if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
512                             (new_state != IP_VS_TCP_S_ESTABLISHED)) {
513                                 atomic_dec(&dest->activeconns);
514                                 atomic_inc(&dest->inactconns);
515                                 cp->flags |= IP_VS_CONN_F_INACTIVE;
516                         } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
517                                    (new_state == IP_VS_TCP_S_ESTABLISHED)) {
518                                 atomic_inc(&dest->activeconns);
519                                 atomic_dec(&dest->inactconns);
520                                 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
521                         }
522                 }
523         }
524
525         cp->timeout = pp->timeout_table[cp->state = new_state];
526 }
527
528
529 /*
530  *      Handle state transitions
531  */
532 static int
533 tcp_state_transition(struct ip_vs_conn *cp, int direction,
534                      const struct sk_buff *skb,
535                      struct ip_vs_protocol *pp)
536 {
537         struct tcphdr _tcph, *th;
538
539 #ifdef CONFIG_IP_VS_IPV6
540         int ihl = cp->af == AF_INET ? ip_hdrlen(skb) : sizeof(struct ipv6hdr);
541 #else
542         int ihl = ip_hdrlen(skb);
543 #endif
544
545         th = skb_header_pointer(skb, ihl, sizeof(_tcph), &_tcph);
546         if (th == NULL)
547                 return 0;
548
549         spin_lock(&cp->lock);
550         set_tcp_state(pp, cp, direction, th);
551         spin_unlock(&cp->lock);
552
553         return 1;
554 }
555
556
557 /*
558  *      Hash table for TCP application incarnations
559  */
560 #define TCP_APP_TAB_BITS        4
561 #define TCP_APP_TAB_SIZE        (1 << TCP_APP_TAB_BITS)
562 #define TCP_APP_TAB_MASK        (TCP_APP_TAB_SIZE - 1)
563
564 static struct list_head tcp_apps[TCP_APP_TAB_SIZE];
565 static DEFINE_SPINLOCK(tcp_app_lock);
566
567 static inline __u16 tcp_app_hashkey(__be16 port)
568 {
569         return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port)
570                 & TCP_APP_TAB_MASK;
571 }
572
573
574 static int tcp_register_app(struct ip_vs_app *inc)
575 {
576         struct ip_vs_app *i;
577         __u16 hash;
578         __be16 port = inc->port;
579         int ret = 0;
580
581         hash = tcp_app_hashkey(port);
582
583         spin_lock_bh(&tcp_app_lock);
584         list_for_each_entry(i, &tcp_apps[hash], p_list) {
585                 if (i->port == port) {
586                         ret = -EEXIST;
587                         goto out;
588                 }
589         }
590         list_add(&inc->p_list, &tcp_apps[hash]);
591         atomic_inc(&ip_vs_protocol_tcp.appcnt);
592
593   out:
594         spin_unlock_bh(&tcp_app_lock);
595         return ret;
596 }
597
598
599 static void
600 tcp_unregister_app(struct ip_vs_app *inc)
601 {
602         spin_lock_bh(&tcp_app_lock);
603         atomic_dec(&ip_vs_protocol_tcp.appcnt);
604         list_del(&inc->p_list);
605         spin_unlock_bh(&tcp_app_lock);
606 }
607
608
609 static int
610 tcp_app_conn_bind(struct ip_vs_conn *cp)
611 {
612         int hash;
613         struct ip_vs_app *inc;
614         int result = 0;
615
616         /* Default binding: bind app only for NAT */
617         if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
618                 return 0;
619
620         /* Lookup application incarnations and bind the right one */
621         hash = tcp_app_hashkey(cp->vport);
622
623         spin_lock(&tcp_app_lock);
624         list_for_each_entry(inc, &tcp_apps[hash], p_list) {
625                 if (inc->port == cp->vport) {
626                         if (unlikely(!ip_vs_app_inc_get(inc)))
627                                 break;
628                         spin_unlock(&tcp_app_lock);
629
630                         IP_VS_DBG_BUF(9, "%s: Binding conn %s:%u->"
631                                       "%s:%u to app %s on port %u\n",
632                                       __func__,
633                                       IP_VS_DBG_ADDR(cp->af, &cp->caddr),
634                                       ntohs(cp->cport),
635                                       IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
636                                       ntohs(cp->vport),
637                                       inc->name, ntohs(inc->port));
638
639                         cp->app = inc;
640                         if (inc->init_conn)
641                                 result = inc->init_conn(inc, cp);
642                         goto out;
643                 }
644         }
645         spin_unlock(&tcp_app_lock);
646
647   out:
648         return result;
649 }
650
651
652 /*
653  *      Set LISTEN timeout. (ip_vs_conn_put will setup timer)
654  */
655 void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp)
656 {
657         spin_lock(&cp->lock);
658         cp->state = IP_VS_TCP_S_LISTEN;
659         cp->timeout = ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_LISTEN];
660         spin_unlock(&cp->lock);
661 }
662
663
664 static void ip_vs_tcp_init(struct ip_vs_protocol *pp)
665 {
666         IP_VS_INIT_HASH_TABLE(tcp_apps);
667         pp->timeout_table = tcp_timeouts;
668 }
669
670
671 static void ip_vs_tcp_exit(struct ip_vs_protocol *pp)
672 {
673 }
674
675
676 struct ip_vs_protocol ip_vs_protocol_tcp = {
677         .name =                 "TCP",
678         .protocol =             IPPROTO_TCP,
679         .num_states =           IP_VS_TCP_S_LAST,
680         .dont_defrag =          0,
681         .appcnt =               ATOMIC_INIT(0),
682         .init =                 ip_vs_tcp_init,
683         .exit =                 ip_vs_tcp_exit,
684         .register_app =         tcp_register_app,
685         .unregister_app =       tcp_unregister_app,
686         .conn_schedule =        tcp_conn_schedule,
687         .conn_in_get =          tcp_conn_in_get,
688         .conn_out_get =         tcp_conn_out_get,
689         .snat_handler =         tcp_snat_handler,
690         .dnat_handler =         tcp_dnat_handler,
691         .csum_check =           tcp_csum_check,
692         .state_name =           tcp_state_name,
693         .state_transition =     tcp_state_transition,
694         .app_conn_bind =        tcp_app_conn_bind,
695         .debug_packet =         ip_vs_tcpudp_debug_packet,
696         .timeout_change =       tcp_timeout_change,
697         .set_state_timeout =    tcp_set_state_timeout,
698 };