IPVS: Add/adjust Netfilter hook functions and helpers for v6
[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(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(8, "%s %s [%c%c%c%c] %u.%u.%u.%u:%d->"
494                           "%u.%u.%u.%u:%d state: %s->%s conn->refcnt:%d\n",
495                           pp->name,
496                           (state_off==TCP_DIR_OUTPUT)?"output ":"input ",
497                           th->syn? 'S' : '.',
498                           th->fin? 'F' : '.',
499                           th->ack? 'A' : '.',
500                           th->rst? 'R' : '.',
501                           NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
502                           NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
503                           tcp_state_name(cp->state),
504                           tcp_state_name(new_state),
505                           atomic_read(&cp->refcnt));
506                 if (dest) {
507                         if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
508                             (new_state != IP_VS_TCP_S_ESTABLISHED)) {
509                                 atomic_dec(&dest->activeconns);
510                                 atomic_inc(&dest->inactconns);
511                                 cp->flags |= IP_VS_CONN_F_INACTIVE;
512                         } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
513                                    (new_state == IP_VS_TCP_S_ESTABLISHED)) {
514                                 atomic_inc(&dest->activeconns);
515                                 atomic_dec(&dest->inactconns);
516                                 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
517                         }
518                 }
519         }
520
521         cp->timeout = pp->timeout_table[cp->state = new_state];
522 }
523
524
525 /*
526  *      Handle state transitions
527  */
528 static int
529 tcp_state_transition(struct ip_vs_conn *cp, int direction,
530                      const struct sk_buff *skb,
531                      struct ip_vs_protocol *pp)
532 {
533         struct tcphdr _tcph, *th;
534
535 #ifdef CONFIG_IP_VS_IPV6
536         int ihl = cp->af == AF_INET ? ip_hdrlen(skb) : sizeof(struct ipv6hdr);
537 #else
538         int ihl = ip_hdrlen(skb);
539 #endif
540
541         th = skb_header_pointer(skb, ihl, sizeof(_tcph), &_tcph);
542         if (th == NULL)
543                 return 0;
544
545         spin_lock(&cp->lock);
546         set_tcp_state(pp, cp, direction, th);
547         spin_unlock(&cp->lock);
548
549         return 1;
550 }
551
552
553 /*
554  *      Hash table for TCP application incarnations
555  */
556 #define TCP_APP_TAB_BITS        4
557 #define TCP_APP_TAB_SIZE        (1 << TCP_APP_TAB_BITS)
558 #define TCP_APP_TAB_MASK        (TCP_APP_TAB_SIZE - 1)
559
560 static struct list_head tcp_apps[TCP_APP_TAB_SIZE];
561 static DEFINE_SPINLOCK(tcp_app_lock);
562
563 static inline __u16 tcp_app_hashkey(__be16 port)
564 {
565         return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port)
566                 & TCP_APP_TAB_MASK;
567 }
568
569
570 static int tcp_register_app(struct ip_vs_app *inc)
571 {
572         struct ip_vs_app *i;
573         __u16 hash;
574         __be16 port = inc->port;
575         int ret = 0;
576
577         hash = tcp_app_hashkey(port);
578
579         spin_lock_bh(&tcp_app_lock);
580         list_for_each_entry(i, &tcp_apps[hash], p_list) {
581                 if (i->port == port) {
582                         ret = -EEXIST;
583                         goto out;
584                 }
585         }
586         list_add(&inc->p_list, &tcp_apps[hash]);
587         atomic_inc(&ip_vs_protocol_tcp.appcnt);
588
589   out:
590         spin_unlock_bh(&tcp_app_lock);
591         return ret;
592 }
593
594
595 static void
596 tcp_unregister_app(struct ip_vs_app *inc)
597 {
598         spin_lock_bh(&tcp_app_lock);
599         atomic_dec(&ip_vs_protocol_tcp.appcnt);
600         list_del(&inc->p_list);
601         spin_unlock_bh(&tcp_app_lock);
602 }
603
604
605 static int
606 tcp_app_conn_bind(struct ip_vs_conn *cp)
607 {
608         int hash;
609         struct ip_vs_app *inc;
610         int result = 0;
611
612         /* Default binding: bind app only for NAT */
613         if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
614                 return 0;
615
616         /* Lookup application incarnations and bind the right one */
617         hash = tcp_app_hashkey(cp->vport);
618
619         spin_lock(&tcp_app_lock);
620         list_for_each_entry(inc, &tcp_apps[hash], p_list) {
621                 if (inc->port == cp->vport) {
622                         if (unlikely(!ip_vs_app_inc_get(inc)))
623                                 break;
624                         spin_unlock(&tcp_app_lock);
625
626                         IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->"
627                                   "%u.%u.%u.%u:%u to app %s on port %u\n",
628                                   __func__,
629                                   NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
630                                   NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
631                                   inc->name, ntohs(inc->port));
632                         cp->app = inc;
633                         if (inc->init_conn)
634                                 result = inc->init_conn(inc, cp);
635                         goto out;
636                 }
637         }
638         spin_unlock(&tcp_app_lock);
639
640   out:
641         return result;
642 }
643
644
645 /*
646  *      Set LISTEN timeout. (ip_vs_conn_put will setup timer)
647  */
648 void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp)
649 {
650         spin_lock(&cp->lock);
651         cp->state = IP_VS_TCP_S_LISTEN;
652         cp->timeout = ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_LISTEN];
653         spin_unlock(&cp->lock);
654 }
655
656
657 static void ip_vs_tcp_init(struct ip_vs_protocol *pp)
658 {
659         IP_VS_INIT_HASH_TABLE(tcp_apps);
660         pp->timeout_table = tcp_timeouts;
661 }
662
663
664 static void ip_vs_tcp_exit(struct ip_vs_protocol *pp)
665 {
666 }
667
668
669 struct ip_vs_protocol ip_vs_protocol_tcp = {
670         .name =                 "TCP",
671         .protocol =             IPPROTO_TCP,
672         .num_states =           IP_VS_TCP_S_LAST,
673         .dont_defrag =          0,
674         .appcnt =               ATOMIC_INIT(0),
675         .init =                 ip_vs_tcp_init,
676         .exit =                 ip_vs_tcp_exit,
677         .register_app =         tcp_register_app,
678         .unregister_app =       tcp_unregister_app,
679         .conn_schedule =        tcp_conn_schedule,
680         .conn_in_get =          tcp_conn_in_get,
681         .conn_out_get =         tcp_conn_out_get,
682         .snat_handler =         tcp_snat_handler,
683         .dnat_handler =         tcp_dnat_handler,
684         .csum_check =           tcp_csum_check,
685         .state_name =           tcp_state_name,
686         .state_transition =     tcp_state_transition,
687         .app_conn_bind =        tcp_app_conn_bind,
688         .debug_packet =         ip_vs_tcpudp_debug_packet,
689         .timeout_change =       tcp_timeout_change,
690         .set_state_timeout =    tcp_set_state_timeout,
691 };