IPVS: Add 'af' args to protocol handler functions
[safe/jmp/linux-2.6] / 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(iph->protocol,
40                                          iph->saddr.ip, pptr[0],
41                                          iph->daddr.ip, pptr[1]);
42         } else {
43                 return ip_vs_conn_in_get(iph->protocol,
44                                          iph->daddr.ip, pptr[1],
45                                          iph->saddr.ip, 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(iph->protocol,
62                                           iph->saddr.ip, pptr[0],
63                                           iph->daddr.ip, pptr[1]);
64         } else {
65                 return ip_vs_conn_out_get(iph->protocol,
66                                           iph->daddr.ip, pptr[1],
67                                           iph->saddr.ip, 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(struct tcphdr *tcph, __be32 oldip, __be32 newip,
118                      __be16 oldport, __be16 newport)
119 {
120         tcph->check =
121                 csum_fold(ip_vs_check_diff4(oldip, newip,
122                                  ip_vs_check_diff2(oldport, newport,
123                                                 ~csum_unfold(tcph->check))));
124 }
125
126
127 static int
128 tcp_snat_handler(struct sk_buff *skb,
129                  struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
130 {
131         struct tcphdr *tcph;
132         const unsigned int tcphoff = ip_hdrlen(skb);
133
134         /* csum_check requires unshared skb */
135         if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
136                 return 0;
137
138         if (unlikely(cp->app != NULL)) {
139                 /* Some checks before mangling */
140                 if (pp->csum_check && !pp->csum_check(AF_INET, skb, pp))
141                         return 0;
142
143                 /* Call application helper if needed */
144                 if (!ip_vs_app_pkt_out(cp, skb))
145                         return 0;
146         }
147
148         tcph = (void *)ip_hdr(skb) + tcphoff;
149         tcph->source = cp->vport;
150
151         /* Adjust TCP checksums */
152         if (!cp->app) {
153                 /* Only port and addr are changed, do fast csum update */
154                 tcp_fast_csum_update(tcph, cp->daddr.ip, cp->vaddr.ip,
155                                      cp->dport, cp->vport);
156                 if (skb->ip_summed == CHECKSUM_COMPLETE)
157                         skb->ip_summed = CHECKSUM_NONE;
158         } else {
159                 /* full checksum calculation */
160                 tcph->check = 0;
161                 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
162                 tcph->check = csum_tcpudp_magic(cp->vaddr.ip, cp->caddr.ip,
163                                                 skb->len - tcphoff,
164                                                 cp->protocol, skb->csum);
165                 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
166                           pp->name, tcph->check,
167                           (char*)&(tcph->check) - (char*)tcph);
168         }
169         return 1;
170 }
171
172
173 static int
174 tcp_dnat_handler(struct sk_buff *skb,
175                  struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
176 {
177         struct tcphdr *tcph;
178         const unsigned int tcphoff = ip_hdrlen(skb);
179
180         /* csum_check requires unshared skb */
181         if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
182                 return 0;
183
184         if (unlikely(cp->app != NULL)) {
185                 /* Some checks before mangling */
186                 if (pp->csum_check && !pp->csum_check(AF_INET, skb, pp))
187                         return 0;
188
189                 /*
190                  *      Attempt ip_vs_app call.
191                  *      It will fix ip_vs_conn and iph ack_seq stuff
192                  */
193                 if (!ip_vs_app_pkt_in(cp, skb))
194                         return 0;
195         }
196
197         tcph = (void *)ip_hdr(skb) + tcphoff;
198         tcph->dest = cp->dport;
199
200         /*
201          *      Adjust TCP checksums
202          */
203         if (!cp->app) {
204                 /* Only port and addr are changed, do fast csum update */
205                 tcp_fast_csum_update(tcph, cp->vaddr.ip, cp->daddr.ip,
206                                      cp->vport, cp->dport);
207                 if (skb->ip_summed == CHECKSUM_COMPLETE)
208                         skb->ip_summed = CHECKSUM_NONE;
209         } else {
210                 /* full checksum calculation */
211                 tcph->check = 0;
212                 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
213                 tcph->check = csum_tcpudp_magic(cp->caddr.ip, cp->daddr.ip,
214                                                 skb->len - tcphoff,
215                                                 cp->protocol, skb->csum);
216                 skb->ip_summed = CHECKSUM_UNNECESSARY;
217         }
218         return 1;
219 }
220
221
222 static int
223 tcp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
224 {
225         unsigned int tcphoff;
226
227 #ifdef CONFIG_IP_VS_IPV6
228         if (af == AF_INET6)
229                 tcphoff = sizeof(struct ipv6hdr);
230         else
231 #endif
232                 tcphoff = ip_hdrlen(skb);
233
234         switch (skb->ip_summed) {
235         case CHECKSUM_NONE:
236                 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
237         case CHECKSUM_COMPLETE:
238 #ifdef CONFIG_IP_VS_IPV6
239                 if (af == AF_INET6) {
240                         if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
241                                             &ipv6_hdr(skb)->daddr,
242                                             skb->len - tcphoff,
243                                             ipv6_hdr(skb)->nexthdr,
244                                             skb->csum)) {
245                                 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
246                                                  "Failed checksum for");
247                                 return 0;
248                         }
249                 } else
250 #endif
251                         if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
252                                               ip_hdr(skb)->daddr,
253                                               skb->len - tcphoff,
254                                               ip_hdr(skb)->protocol,
255                                               skb->csum)) {
256                                 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
257                                                  "Failed checksum for");
258                                 return 0;
259                         }
260                 break;
261         default:
262                 /* No need to checksum. */
263                 break;
264         }
265
266         return 1;
267 }
268
269
270 #define TCP_DIR_INPUT           0
271 #define TCP_DIR_OUTPUT          4
272 #define TCP_DIR_INPUT_ONLY      8
273
274 static const int tcp_state_off[IP_VS_DIR_LAST] = {
275         [IP_VS_DIR_INPUT]               =       TCP_DIR_INPUT,
276         [IP_VS_DIR_OUTPUT]              =       TCP_DIR_OUTPUT,
277         [IP_VS_DIR_INPUT_ONLY]          =       TCP_DIR_INPUT_ONLY,
278 };
279
280 /*
281  *      Timeout table[state]
282  */
283 static int tcp_timeouts[IP_VS_TCP_S_LAST+1] = {
284         [IP_VS_TCP_S_NONE]              =       2*HZ,
285         [IP_VS_TCP_S_ESTABLISHED]       =       15*60*HZ,
286         [IP_VS_TCP_S_SYN_SENT]          =       2*60*HZ,
287         [IP_VS_TCP_S_SYN_RECV]          =       1*60*HZ,
288         [IP_VS_TCP_S_FIN_WAIT]          =       2*60*HZ,
289         [IP_VS_TCP_S_TIME_WAIT]         =       2*60*HZ,
290         [IP_VS_TCP_S_CLOSE]             =       10*HZ,
291         [IP_VS_TCP_S_CLOSE_WAIT]        =       60*HZ,
292         [IP_VS_TCP_S_LAST_ACK]          =       30*HZ,
293         [IP_VS_TCP_S_LISTEN]            =       2*60*HZ,
294         [IP_VS_TCP_S_SYNACK]            =       120*HZ,
295         [IP_VS_TCP_S_LAST]              =       2*HZ,
296 };
297
298 static char * tcp_state_name_table[IP_VS_TCP_S_LAST+1] = {
299         [IP_VS_TCP_S_NONE]              =       "NONE",
300         [IP_VS_TCP_S_ESTABLISHED]       =       "ESTABLISHED",
301         [IP_VS_TCP_S_SYN_SENT]          =       "SYN_SENT",
302         [IP_VS_TCP_S_SYN_RECV]          =       "SYN_RECV",
303         [IP_VS_TCP_S_FIN_WAIT]          =       "FIN_WAIT",
304         [IP_VS_TCP_S_TIME_WAIT]         =       "TIME_WAIT",
305         [IP_VS_TCP_S_CLOSE]             =       "CLOSE",
306         [IP_VS_TCP_S_CLOSE_WAIT]        =       "CLOSE_WAIT",
307         [IP_VS_TCP_S_LAST_ACK]          =       "LAST_ACK",
308         [IP_VS_TCP_S_LISTEN]            =       "LISTEN",
309         [IP_VS_TCP_S_SYNACK]            =       "SYNACK",
310         [IP_VS_TCP_S_LAST]              =       "BUG!",
311 };
312
313 #define sNO IP_VS_TCP_S_NONE
314 #define sES IP_VS_TCP_S_ESTABLISHED
315 #define sSS IP_VS_TCP_S_SYN_SENT
316 #define sSR IP_VS_TCP_S_SYN_RECV
317 #define sFW IP_VS_TCP_S_FIN_WAIT
318 #define sTW IP_VS_TCP_S_TIME_WAIT
319 #define sCL IP_VS_TCP_S_CLOSE
320 #define sCW IP_VS_TCP_S_CLOSE_WAIT
321 #define sLA IP_VS_TCP_S_LAST_ACK
322 #define sLI IP_VS_TCP_S_LISTEN
323 #define sSA IP_VS_TCP_S_SYNACK
324
325 struct tcp_states_t {
326         int next_state[IP_VS_TCP_S_LAST];
327 };
328
329 static const char * tcp_state_name(int state)
330 {
331         if (state >= IP_VS_TCP_S_LAST)
332                 return "ERR!";
333         return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?";
334 }
335
336 static struct tcp_states_t tcp_states [] = {
337 /*      INPUT */
338 /*        sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
339 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
340 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW }},
341 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
342 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR }},
343
344 /*      OUTPUT */
345 /*        sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
346 /*syn*/ {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR }},
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 }},
350
351 /*      INPUT-ONLY */
352 /*        sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
353 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
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 }},
357 };
358
359 static struct tcp_states_t tcp_states_dos [] = {
360 /*      INPUT */
361 /*        sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
362 /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSA }},
363 /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sSA }},
364 /*ack*/ {{sCL, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }},
365 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
366
367 /*      OUTPUT */
368 /*        sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
369 /*syn*/ {{sSS, sES, sSS, sSA, sSS, sSS, sSS, sSS, sSS, sLI, sSA }},
370 /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
371 /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
372 /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
373
374 /*      INPUT-ONLY */
375 /*        sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
376 /*syn*/ {{sSA, sES, sES, sSR, sSA, sSA, sSA, sSA, sSA, sSA, sSA }},
377 /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
378 /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
379 /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
380 };
381
382 static struct tcp_states_t *tcp_state_table = tcp_states;
383
384
385 static void tcp_timeout_change(struct ip_vs_protocol *pp, int flags)
386 {
387         int on = (flags & 1);           /* secure_tcp */
388
389         /*
390         ** FIXME: change secure_tcp to independent sysctl var
391         ** or make it per-service or per-app because it is valid
392         ** for most if not for all of the applications. Something
393         ** like "capabilities" (flags) for each object.
394         */
395         tcp_state_table = (on? tcp_states_dos : tcp_states);
396 }
397
398 static int
399 tcp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
400 {
401         return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_TCP_S_LAST,
402                                        tcp_state_name_table, sname, to);
403 }
404
405 static inline int tcp_state_idx(struct tcphdr *th)
406 {
407         if (th->rst)
408                 return 3;
409         if (th->syn)
410                 return 0;
411         if (th->fin)
412                 return 1;
413         if (th->ack)
414                 return 2;
415         return -1;
416 }
417
418 static inline void
419 set_tcp_state(struct ip_vs_protocol *pp, struct ip_vs_conn *cp,
420               int direction, struct tcphdr *th)
421 {
422         int state_idx;
423         int new_state = IP_VS_TCP_S_CLOSE;
424         int state_off = tcp_state_off[direction];
425
426         /*
427          *    Update state offset to INPUT_ONLY if necessary
428          *    or delete NO_OUTPUT flag if output packet detected
429          */
430         if (cp->flags & IP_VS_CONN_F_NOOUTPUT) {
431                 if (state_off == TCP_DIR_OUTPUT)
432                         cp->flags &= ~IP_VS_CONN_F_NOOUTPUT;
433                 else
434                         state_off = TCP_DIR_INPUT_ONLY;
435         }
436
437         if ((state_idx = tcp_state_idx(th)) < 0) {
438                 IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx);
439                 goto tcp_state_out;
440         }
441
442         new_state = tcp_state_table[state_off+state_idx].next_state[cp->state];
443
444   tcp_state_out:
445         if (new_state != cp->state) {
446                 struct ip_vs_dest *dest = cp->dest;
447
448                 IP_VS_DBG(8, "%s %s [%c%c%c%c] %u.%u.%u.%u:%d->"
449                           "%u.%u.%u.%u:%d state: %s->%s conn->refcnt:%d\n",
450                           pp->name,
451                           (state_off==TCP_DIR_OUTPUT)?"output ":"input ",
452                           th->syn? 'S' : '.',
453                           th->fin? 'F' : '.',
454                           th->ack? 'A' : '.',
455                           th->rst? 'R' : '.',
456                           NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
457                           NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
458                           tcp_state_name(cp->state),
459                           tcp_state_name(new_state),
460                           atomic_read(&cp->refcnt));
461                 if (dest) {
462                         if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
463                             (new_state != IP_VS_TCP_S_ESTABLISHED)) {
464                                 atomic_dec(&dest->activeconns);
465                                 atomic_inc(&dest->inactconns);
466                                 cp->flags |= IP_VS_CONN_F_INACTIVE;
467                         } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
468                                    (new_state == IP_VS_TCP_S_ESTABLISHED)) {
469                                 atomic_inc(&dest->activeconns);
470                                 atomic_dec(&dest->inactconns);
471                                 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
472                         }
473                 }
474         }
475
476         cp->timeout = pp->timeout_table[cp->state = new_state];
477 }
478
479
480 /*
481  *      Handle state transitions
482  */
483 static int
484 tcp_state_transition(struct ip_vs_conn *cp, int direction,
485                      const struct sk_buff *skb,
486                      struct ip_vs_protocol *pp)
487 {
488         struct tcphdr _tcph, *th;
489
490         th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
491         if (th == NULL)
492                 return 0;
493
494         spin_lock(&cp->lock);
495         set_tcp_state(pp, cp, direction, th);
496         spin_unlock(&cp->lock);
497
498         return 1;
499 }
500
501
502 /*
503  *      Hash table for TCP application incarnations
504  */
505 #define TCP_APP_TAB_BITS        4
506 #define TCP_APP_TAB_SIZE        (1 << TCP_APP_TAB_BITS)
507 #define TCP_APP_TAB_MASK        (TCP_APP_TAB_SIZE - 1)
508
509 static struct list_head tcp_apps[TCP_APP_TAB_SIZE];
510 static DEFINE_SPINLOCK(tcp_app_lock);
511
512 static inline __u16 tcp_app_hashkey(__be16 port)
513 {
514         return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port)
515                 & TCP_APP_TAB_MASK;
516 }
517
518
519 static int tcp_register_app(struct ip_vs_app *inc)
520 {
521         struct ip_vs_app *i;
522         __u16 hash;
523         __be16 port = inc->port;
524         int ret = 0;
525
526         hash = tcp_app_hashkey(port);
527
528         spin_lock_bh(&tcp_app_lock);
529         list_for_each_entry(i, &tcp_apps[hash], p_list) {
530                 if (i->port == port) {
531                         ret = -EEXIST;
532                         goto out;
533                 }
534         }
535         list_add(&inc->p_list, &tcp_apps[hash]);
536         atomic_inc(&ip_vs_protocol_tcp.appcnt);
537
538   out:
539         spin_unlock_bh(&tcp_app_lock);
540         return ret;
541 }
542
543
544 static void
545 tcp_unregister_app(struct ip_vs_app *inc)
546 {
547         spin_lock_bh(&tcp_app_lock);
548         atomic_dec(&ip_vs_protocol_tcp.appcnt);
549         list_del(&inc->p_list);
550         spin_unlock_bh(&tcp_app_lock);
551 }
552
553
554 static int
555 tcp_app_conn_bind(struct ip_vs_conn *cp)
556 {
557         int hash;
558         struct ip_vs_app *inc;
559         int result = 0;
560
561         /* Default binding: bind app only for NAT */
562         if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
563                 return 0;
564
565         /* Lookup application incarnations and bind the right one */
566         hash = tcp_app_hashkey(cp->vport);
567
568         spin_lock(&tcp_app_lock);
569         list_for_each_entry(inc, &tcp_apps[hash], p_list) {
570                 if (inc->port == cp->vport) {
571                         if (unlikely(!ip_vs_app_inc_get(inc)))
572                                 break;
573                         spin_unlock(&tcp_app_lock);
574
575                         IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->"
576                                   "%u.%u.%u.%u:%u to app %s on port %u\n",
577                                   __func__,
578                                   NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
579                                   NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
580                                   inc->name, ntohs(inc->port));
581                         cp->app = inc;
582                         if (inc->init_conn)
583                                 result = inc->init_conn(inc, cp);
584                         goto out;
585                 }
586         }
587         spin_unlock(&tcp_app_lock);
588
589   out:
590         return result;
591 }
592
593
594 /*
595  *      Set LISTEN timeout. (ip_vs_conn_put will setup timer)
596  */
597 void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp)
598 {
599         spin_lock(&cp->lock);
600         cp->state = IP_VS_TCP_S_LISTEN;
601         cp->timeout = ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_LISTEN];
602         spin_unlock(&cp->lock);
603 }
604
605
606 static void ip_vs_tcp_init(struct ip_vs_protocol *pp)
607 {
608         IP_VS_INIT_HASH_TABLE(tcp_apps);
609         pp->timeout_table = tcp_timeouts;
610 }
611
612
613 static void ip_vs_tcp_exit(struct ip_vs_protocol *pp)
614 {
615 }
616
617
618 struct ip_vs_protocol ip_vs_protocol_tcp = {
619         .name =                 "TCP",
620         .protocol =             IPPROTO_TCP,
621         .num_states =           IP_VS_TCP_S_LAST,
622         .dont_defrag =          0,
623         .appcnt =               ATOMIC_INIT(0),
624         .init =                 ip_vs_tcp_init,
625         .exit =                 ip_vs_tcp_exit,
626         .register_app =         tcp_register_app,
627         .unregister_app =       tcp_unregister_app,
628         .conn_schedule =        tcp_conn_schedule,
629         .conn_in_get =          tcp_conn_in_get,
630         .conn_out_get =         tcp_conn_out_get,
631         .snat_handler =         tcp_snat_handler,
632         .dnat_handler =         tcp_dnat_handler,
633         .csum_check =           tcp_csum_check,
634         .state_name =           tcp_state_name,
635         .state_transition =     tcp_state_transition,
636         .app_conn_bind =        tcp_app_conn_bind,
637         .debug_packet =         ip_vs_tcpudp_debug_packet,
638         .timeout_change =       tcp_timeout_change,
639         .set_state_timeout =    tcp_set_state_timeout,
640 };