1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/kernel.h>
19#include <linux/ip.h>
20#include <linux/tcp.h>
21#include <net/ip.h>
22#include <net/tcp.h>
23#include <linux/netfilter.h>
24#include <linux/netfilter_ipv4.h>
25
26#include <net/ip_vs.h>
27
28
29static struct ip_vs_conn *
30tcp_conn_in_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
31 const struct iphdr *iph, unsigned int proto_off, int inverse)
32{
33 __be16 _ports[2], *pptr;
34
35 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
36 if (pptr == NULL)
37 return NULL;
38
39 if (likely(!inverse)) {
40 return ip_vs_conn_in_get(iph->protocol,
41 iph->saddr, pptr[0],
42 iph->daddr, pptr[1]);
43 } else {
44 return ip_vs_conn_in_get(iph->protocol,
45 iph->daddr, pptr[1],
46 iph->saddr, pptr[0]);
47 }
48}
49
50static struct ip_vs_conn *
51tcp_conn_out_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
52 const struct iphdr *iph, unsigned int proto_off, 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, pptr[0],
63 iph->daddr, pptr[1]);
64 } else {
65 return ip_vs_conn_out_get(iph->protocol,
66 iph->daddr, pptr[1],
67 iph->saddr, pptr[0]);
68 }
69}
70
71
72static int
73tcp_conn_schedule(struct sk_buff *skb,
74 struct ip_vs_protocol *pp,
75 int *verdict, struct ip_vs_conn **cpp)
76{
77 struct ip_vs_service *svc;
78 struct tcphdr _tcph, *th;
79
80 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
81 if (th == NULL) {
82 *verdict = NF_DROP;
83 return 0;
84 }
85
86 if (th->syn &&
87 (svc = ip_vs_service_get(skb->mark, ip_hdr(skb)->protocol,
88 ip_hdr(skb)->daddr, th->dest))) {
89 if (ip_vs_todrop()) {
90
91
92
93
94 ip_vs_service_put(svc);
95 *verdict = NF_DROP;
96 return 0;
97 }
98
99
100
101
102
103 *cpp = ip_vs_schedule(svc, skb);
104 if (!*cpp) {
105 *verdict = ip_vs_leave(svc, skb, pp);
106 return 0;
107 }
108 ip_vs_service_put(svc);
109 }
110 return 1;
111}
112
113
114static inline void
115tcp_fast_csum_update(struct tcphdr *tcph, __be32 oldip, __be32 newip,
116 __be16 oldport, __be16 newport)
117{
118 tcph->check =
119 csum_fold(ip_vs_check_diff4(oldip, newip,
120 ip_vs_check_diff2(oldport, newport,
121 ~csum_unfold(tcph->check))));
122}
123
124
125static int
126tcp_snat_handler(struct sk_buff *skb,
127 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
128{
129 struct tcphdr *tcph;
130 const unsigned int tcphoff = ip_hdrlen(skb);
131
132
133 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
134 return 0;
135
136 if (unlikely(cp->app != NULL)) {
137
138 if (pp->csum_check && !pp->csum_check(skb, pp))
139 return 0;
140
141
142 if (!ip_vs_app_pkt_out(cp, skb))
143 return 0;
144 }
145
146 tcph = (void *)ip_hdr(skb) + tcphoff;
147 tcph->source = cp->vport;
148
149
150 if (!cp->app) {
151
152 tcp_fast_csum_update(tcph, cp->daddr, cp->vaddr,
153 cp->dport, cp->vport);
154 if (skb->ip_summed == CHECKSUM_COMPLETE)
155 skb->ip_summed = CHECKSUM_NONE;
156 } else {
157
158 tcph->check = 0;
159 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
160 tcph->check = csum_tcpudp_magic(cp->vaddr, cp->caddr,
161 skb->len - tcphoff,
162 cp->protocol, skb->csum);
163 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
164 pp->name, tcph->check,
165 (char*)&(tcph->check) - (char*)tcph);
166 }
167 return 1;
168}
169
170
171static int
172tcp_dnat_handler(struct sk_buff *skb,
173 struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
174{
175 struct tcphdr *tcph;
176 const unsigned int tcphoff = ip_hdrlen(skb);
177
178
179 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
180 return 0;
181
182 if (unlikely(cp->app != NULL)) {
183
184 if (pp->csum_check && !pp->csum_check(skb, pp))
185 return 0;
186
187
188
189
190
191 if (!ip_vs_app_pkt_in(cp, skb))
192 return 0;
193 }
194
195 tcph = (void *)ip_hdr(skb) + tcphoff;
196 tcph->dest = cp->dport;
197
198
199
200
201 if (!cp->app) {
202
203 tcp_fast_csum_update(tcph, cp->vaddr, cp->daddr,
204 cp->vport, cp->dport);
205 if (skb->ip_summed == CHECKSUM_COMPLETE)
206 skb->ip_summed = CHECKSUM_NONE;
207 } else {
208
209 tcph->check = 0;
210 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
211 tcph->check = csum_tcpudp_magic(cp->caddr, cp->daddr,
212 skb->len - tcphoff,
213 cp->protocol, skb->csum);
214 skb->ip_summed = CHECKSUM_UNNECESSARY;
215 }
216 return 1;
217}
218
219
220static int
221tcp_csum_check(struct sk_buff *skb, struct ip_vs_protocol *pp)
222{
223 const unsigned int tcphoff = ip_hdrlen(skb);
224
225 switch (skb->ip_summed) {
226 case CHECKSUM_NONE:
227 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
228 case CHECKSUM_COMPLETE:
229 if (csum_tcpudp_magic(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
230 skb->len - tcphoff,
231 ip_hdr(skb)->protocol, skb->csum)) {
232 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
233 "Failed checksum for");
234 return 0;
235 }
236 break;
237 default:
238
239 break;
240 }
241
242 return 1;
243}
244
245
246#define TCP_DIR_INPUT 0
247#define TCP_DIR_OUTPUT 4
248#define TCP_DIR_INPUT_ONLY 8
249
250static const int tcp_state_off[IP_VS_DIR_LAST] = {
251 [IP_VS_DIR_INPUT] = TCP_DIR_INPUT,
252 [IP_VS_DIR_OUTPUT] = TCP_DIR_OUTPUT,
253 [IP_VS_DIR_INPUT_ONLY] = TCP_DIR_INPUT_ONLY,
254};
255
256
257
258
259static int tcp_timeouts[IP_VS_TCP_S_LAST+1] = {
260 [IP_VS_TCP_S_NONE] = 2*HZ,
261 [IP_VS_TCP_S_ESTABLISHED] = 15*60*HZ,
262 [IP_VS_TCP_S_SYN_SENT] = 2*60*HZ,
263 [IP_VS_TCP_S_SYN_RECV] = 1*60*HZ,
264 [IP_VS_TCP_S_FIN_WAIT] = 2*60*HZ,
265 [IP_VS_TCP_S_TIME_WAIT] = 2*60*HZ,
266 [IP_VS_TCP_S_CLOSE] = 10*HZ,
267 [IP_VS_TCP_S_CLOSE_WAIT] = 60*HZ,
268 [IP_VS_TCP_S_LAST_ACK] = 30*HZ,
269 [IP_VS_TCP_S_LISTEN] = 2*60*HZ,
270 [IP_VS_TCP_S_SYNACK] = 120*HZ,
271 [IP_VS_TCP_S_LAST] = 2*HZ,
272};
273
274static char * tcp_state_name_table[IP_VS_TCP_S_LAST+1] = {
275 [IP_VS_TCP_S_NONE] = "NONE",
276 [IP_VS_TCP_S_ESTABLISHED] = "ESTABLISHED",
277 [IP_VS_TCP_S_SYN_SENT] = "SYN_SENT",
278 [IP_VS_TCP_S_SYN_RECV] = "SYN_RECV",
279 [IP_VS_TCP_S_FIN_WAIT] = "FIN_WAIT",
280 [IP_VS_TCP_S_TIME_WAIT] = "TIME_WAIT",
281 [IP_VS_TCP_S_CLOSE] = "CLOSE",
282 [IP_VS_TCP_S_CLOSE_WAIT] = "CLOSE_WAIT",
283 [IP_VS_TCP_S_LAST_ACK] = "LAST_ACK",
284 [IP_VS_TCP_S_LISTEN] = "LISTEN",
285 [IP_VS_TCP_S_SYNACK] = "SYNACK",
286 [IP_VS_TCP_S_LAST] = "BUG!",
287};
288
289#define sNO IP_VS_TCP_S_NONE
290#define sES IP_VS_TCP_S_ESTABLISHED
291#define sSS IP_VS_TCP_S_SYN_SENT
292#define sSR IP_VS_TCP_S_SYN_RECV
293#define sFW IP_VS_TCP_S_FIN_WAIT
294#define sTW IP_VS_TCP_S_TIME_WAIT
295#define sCL IP_VS_TCP_S_CLOSE
296#define sCW IP_VS_TCP_S_CLOSE_WAIT
297#define sLA IP_VS_TCP_S_LAST_ACK
298#define sLI IP_VS_TCP_S_LISTEN
299#define sSA IP_VS_TCP_S_SYNACK
300
301struct tcp_states_t {
302 int next_state[IP_VS_TCP_S_LAST];
303};
304
305static const char * tcp_state_name(int state)
306{
307 if (state >= IP_VS_TCP_S_LAST)
308 return "ERR!";
309 return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?";
310}
311
312static struct tcp_states_t tcp_states [] = {
313
314
315 {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
316 {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW }},
317 {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
318 {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR }},
319
320
321
322 {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR }},
323 {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
324 {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
325 {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
326
327
328
329 {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
330 {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
331 {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
332 {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
333};
334
335static struct tcp_states_t tcp_states_dos [] = {
336
337
338 {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSA }},
339 {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sSA }},
340 {{sCL, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }},
341 {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
342
343
344
345 {{sSS, sES, sSS, sSA, sSS, sSS, sSS, sSS, sSS, sLI, sSA }},
346 {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
347 {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
348 {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
349
350
351
352 {{sSA, sES, sES, sSR, sSA, sSA, sSA, sSA, sSA, sSA, sSA }},
353 {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
354 {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
355 {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
356};
357
358static struct tcp_states_t *tcp_state_table = tcp_states;
359
360
361static void tcp_timeout_change(struct ip_vs_protocol *pp, int flags)
362{
363 int on = (flags & 1);
364
365
366
367
368
369
370
371 tcp_state_table = (on? tcp_states_dos : tcp_states);
372}
373
374static int
375tcp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
376{
377 return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_TCP_S_LAST,
378 tcp_state_name_table, sname, to);
379}
380
381static inline int tcp_state_idx(struct tcphdr *th)
382{
383 if (th->rst)
384 return 3;
385 if (th->syn)
386 return 0;
387 if (th->fin)
388 return 1;
389 if (th->ack)
390 return 2;
391 return -1;
392}
393
394static inline void
395set_tcp_state(struct ip_vs_protocol *pp, struct ip_vs_conn *cp,
396 int direction, struct tcphdr *th)
397{
398 int state_idx;
399 int new_state = IP_VS_TCP_S_CLOSE;
400 int state_off = tcp_state_off[direction];
401
402
403
404
405
406 if (cp->flags & IP_VS_CONN_F_NOOUTPUT) {
407 if (state_off == TCP_DIR_OUTPUT)
408 cp->flags &= ~IP_VS_CONN_F_NOOUTPUT;
409 else
410 state_off = TCP_DIR_INPUT_ONLY;
411 }
412
413 if ((state_idx = tcp_state_idx(th)) < 0) {
414 IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx);
415 goto tcp_state_out;
416 }
417
418 new_state = tcp_state_table[state_off+state_idx].next_state[cp->state];
419
420 tcp_state_out:
421 if (new_state != cp->state) {
422 struct ip_vs_dest *dest = cp->dest;
423
424 IP_VS_DBG(8, "%s %s [%c%c%c%c] %u.%u.%u.%u:%d->"
425 "%u.%u.%u.%u:%d state: %s->%s conn->refcnt:%d\n",
426 pp->name,
427 (state_off==TCP_DIR_OUTPUT)?"output ":"input ",
428 th->syn? 'S' : '.',
429 th->fin? 'F' : '.',
430 th->ack? 'A' : '.',
431 th->rst? 'R' : '.',
432 NIPQUAD(cp->daddr), ntohs(cp->dport),
433 NIPQUAD(cp->caddr), ntohs(cp->cport),
434 tcp_state_name(cp->state),
435 tcp_state_name(new_state),
436 atomic_read(&cp->refcnt));
437 if (dest) {
438 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
439 (new_state != IP_VS_TCP_S_ESTABLISHED)) {
440 atomic_dec(&dest->activeconns);
441 atomic_inc(&dest->inactconns);
442 cp->flags |= IP_VS_CONN_F_INACTIVE;
443 } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
444 (new_state == IP_VS_TCP_S_ESTABLISHED)) {
445 atomic_inc(&dest->activeconns);
446 atomic_dec(&dest->inactconns);
447 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
448 }
449 }
450 }
451
452 cp->timeout = pp->timeout_table[cp->state = new_state];
453}
454
455
456
457
458
459static int
460tcp_state_transition(struct ip_vs_conn *cp, int direction,
461 const struct sk_buff *skb,
462 struct ip_vs_protocol *pp)
463{
464 struct tcphdr _tcph, *th;
465
466 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
467 if (th == NULL)
468 return 0;
469
470 spin_lock(&cp->lock);
471 set_tcp_state(pp, cp, direction, th);
472 spin_unlock(&cp->lock);
473
474 return 1;
475}
476
477
478
479
480
481#define TCP_APP_TAB_BITS 4
482#define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
483#define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
484
485static struct list_head tcp_apps[TCP_APP_TAB_SIZE];
486static DEFINE_SPINLOCK(tcp_app_lock);
487
488static inline __u16 tcp_app_hashkey(__be16 port)
489{
490 return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port)
491 & TCP_APP_TAB_MASK;
492}
493
494
495static int tcp_register_app(struct ip_vs_app *inc)
496{
497 struct ip_vs_app *i;
498 __u16 hash;
499 __be16 port = inc->port;
500 int ret = 0;
501
502 hash = tcp_app_hashkey(port);
503
504 spin_lock_bh(&tcp_app_lock);
505 list_for_each_entry(i, &tcp_apps[hash], p_list) {
506 if (i->port == port) {
507 ret = -EEXIST;
508 goto out;
509 }
510 }
511 list_add(&inc->p_list, &tcp_apps[hash]);
512 atomic_inc(&ip_vs_protocol_tcp.appcnt);
513
514 out:
515 spin_unlock_bh(&tcp_app_lock);
516 return ret;
517}
518
519
520static void
521tcp_unregister_app(struct ip_vs_app *inc)
522{
523 spin_lock_bh(&tcp_app_lock);
524 atomic_dec(&ip_vs_protocol_tcp.appcnt);
525 list_del(&inc->p_list);
526 spin_unlock_bh(&tcp_app_lock);
527}
528
529
530static int
531tcp_app_conn_bind(struct ip_vs_conn *cp)
532{
533 int hash;
534 struct ip_vs_app *inc;
535 int result = 0;
536
537
538 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
539 return 0;
540
541
542 hash = tcp_app_hashkey(cp->vport);
543
544 spin_lock(&tcp_app_lock);
545 list_for_each_entry(inc, &tcp_apps[hash], p_list) {
546 if (inc->port == cp->vport) {
547 if (unlikely(!ip_vs_app_inc_get(inc)))
548 break;
549 spin_unlock(&tcp_app_lock);
550
551 IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->"
552 "%u.%u.%u.%u:%u to app %s on port %u\n",
553 __func__,
554 NIPQUAD(cp->caddr), ntohs(cp->cport),
555 NIPQUAD(cp->vaddr), ntohs(cp->vport),
556 inc->name, ntohs(inc->port));
557 cp->app = inc;
558 if (inc->init_conn)
559 result = inc->init_conn(inc, cp);
560 goto out;
561 }
562 }
563 spin_unlock(&tcp_app_lock);
564
565 out:
566 return result;
567}
568
569
570
571
572
573void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp)
574{
575 spin_lock(&cp->lock);
576 cp->state = IP_VS_TCP_S_LISTEN;
577 cp->timeout = ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_LISTEN];
578 spin_unlock(&cp->lock);
579}
580
581
582static void ip_vs_tcp_init(struct ip_vs_protocol *pp)
583{
584 IP_VS_INIT_HASH_TABLE(tcp_apps);
585 pp->timeout_table = tcp_timeouts;
586}
587
588
589static void ip_vs_tcp_exit(struct ip_vs_protocol *pp)
590{
591}
592
593
594struct ip_vs_protocol ip_vs_protocol_tcp = {
595 .name = "TCP",
596 .protocol = IPPROTO_TCP,
597 .num_states = IP_VS_TCP_S_LAST,
598 .dont_defrag = 0,
599 .appcnt = ATOMIC_INIT(0),
600 .init = ip_vs_tcp_init,
601 .exit = ip_vs_tcp_exit,
602 .register_app = tcp_register_app,
603 .unregister_app = tcp_unregister_app,
604 .conn_schedule = tcp_conn_schedule,
605 .conn_in_get = tcp_conn_in_get,
606 .conn_out_get = tcp_conn_out_get,
607 .snat_handler = tcp_snat_handler,
608 .dnat_handler = tcp_dnat_handler,
609 .csum_check = tcp_csum_check,
610 .state_name = tcp_state_name,
611 .state_transition = tcp_state_transition,
612 .app_conn_bind = tcp_app_conn_bind,
613 .debug_packet = ip_vs_tcpudp_debug_packet,
614 .timeout_change = tcp_timeout_change,
615 .set_state_timeout = tcp_set_state_timeout,
616};
617