forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_vs_proto_tcp.c
1114 lines (943 loc) · 33.7 KB
/
ip_vs_proto_tcp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2017 iQIYI (www.iqiyi.com).
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <assert.h>
#include <time.h>
#include "common.h"
#include "dpdk.h"
#include "ipv4.h"
#include "ipvs/ipvs.h"
#include "ipvs/proto.h"
#include "ipvs/proto_tcp.h"
#include "ipvs/conn.h"
#include "ipvs/service.h"
#include "ipvs/dest.h"
#include "ipvs/synproxy.h"
#include "parser/parser.h"
/* we need more detailed fields than dpdk tcp_hdr{},
* like tcphdr.syn, so use standard definition. */
#include <netinet/tcp.h>
#include <openssl/sha.h>
static int g_defence_tcp_drop = 0;
static int tcp_timeouts[DPVS_TCP_S_LAST + 1] = {
[DPVS_TCP_S_NONE] = 2, /* in seconds */
[DPVS_TCP_S_ESTABLISHED] = 90,
[DPVS_TCP_S_SYN_SENT] = 3,
[DPVS_TCP_S_SYN_RECV] = 30,
[DPVS_TCP_S_FIN_WAIT] = 7,
[DPVS_TCP_S_TIME_WAIT] = 7,
[DPVS_TCP_S_CLOSE] = 3,
[DPVS_TCP_S_CLOSE_WAIT] = 7,
[DPVS_TCP_S_LAST_ACK] = 7,
[DPVS_TCP_S_LISTEN] = 120,
[DPVS_TCP_S_SYNACK] = 30,
[DPVS_TCP_S_LAST] = 2
};
#ifdef CONFIG_DPVS_IPVS_DEBUG
static const char *tcp_state_names[] = {
[DPVS_TCP_S_NONE] = "NONE",
[DPVS_TCP_S_ESTABLISHED] = "ESTABLISHED",
[DPVS_TCP_S_SYN_SENT] = "SYN_SENT",
[DPVS_TCP_S_SYN_RECV] = "SYN_RECV",
[DPVS_TCP_S_FIN_WAIT] = "FIN_WAIT",
[DPVS_TCP_S_TIME_WAIT] = "TIME_WAIT",
[DPVS_TCP_S_CLOSE] = "CLOSE",
[DPVS_TCP_S_CLOSE_WAIT] = "CLOSE_WAIT",
[DPVS_TCP_S_LAST_ACK] = "LAST_ACK",
[DPVS_TCP_S_LISTEN] = "LISTEN",
[DPVS_TCP_S_SYNACK] = "SYNACK",
[DPVS_TCP_S_LAST] = "BUG!"
};
#endif
static struct tcp_state tcp_states[] = {
/* INPUT */
/* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
/*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR}},
/*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW}},
/*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES}},
/*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR}},
/* OUTPUT */
/* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
/*syn*/ {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR}},
/*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW}},
/*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES}},
/*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL}},
/* INPUT-ONLY */
/* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
/*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR}},
/*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW}},
/*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES}},
/*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL}},
};
static uint32_t tcp_secret;
/* if tcp header will be modified mbuf_header_pointer() cannot be used. */
inline struct tcphdr *tcp_hdr(const struct rte_mbuf *mbuf)
{
int ip4hlen = ip4_hdrlen(mbuf);
/* do not support frags */
if (unlikely(mbuf->data_len < ip4hlen + sizeof(struct tcphdr)))
return NULL;
return rte_pktmbuf_mtod_offset(mbuf, struct tcphdr *, ip4hlen);
}
inline void tcp4_send_csum(struct ipv4_hdr *iph, struct tcphdr *th)
{
th->check = 0;
th->check = rte_ipv4_udptcp_cksum(iph, th);
}
static inline uint32_t seq_scale(uint32_t seq)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
/* 64 ns as kernel */
return seq + ((now.tv_sec * 1000000000L + now.tv_nsec) >> 6);
}
static inline int seq_before(uint32_t seq1, uint32_t seq2)
{
return (int32_t)(seq1 - seq2) < 0;
}
static inline uint32_t tcp_secure_sequence_number(uint32_t saddr, uint32_t daddr,
uint16_t sport, uint16_t dport)
{
unsigned char hash[SHA_DIGEST_LENGTH];
uint32_t data[4], hash0;
data[0] = (uint32_t)saddr;
data[1] = (uint32_t)daddr;
data[2] = ((uint16_t)sport << 16) + (uint16_t)dport;
data[3] = tcp_secret;
SHA1((unsigned char *)data, sizeof(data), hash);
hash0 = hash[0];
return seq_scale(*(uint32_t *)&hash0);
}
static inline void tcp_in_init_seq(struct dp_vs_conn *conn,
struct rte_mbuf *mbuf, struct tcphdr *th)
{
struct dp_vs_seq *fseq = &conn->fnat_seq;
uint32_t seq = ntohl(th->seq);
if (fseq->isn != 0 && fseq->delta == fseq->isn - seq)
return; /* retransmit */
if (fseq->isn)
return;
fseq->isn = tcp_secure_sequence_number(conn->laddr.in.s_addr,
conn->daddr.in.s_addr, conn->lport, conn->dport);
fseq->delta = fseq->isn - seq;
return;
}
static inline void tcp_in_adjust_seq(struct dp_vs_conn *conn, struct tcphdr *th)
{
th->seq = htonl(ntohl(th->seq) + conn->fnat_seq.delta);
/* recalc checksum later */
/* adjust ack_seq for synproxy,including tcp hdr and sack opt */
dp_vs_synproxy_dnat_handler(th, &conn->syn_proxy_seq);
return;
}
/* use NOP option to replace timestamp opt */
static void tcp_in_remove_ts(struct tcphdr *tcph)
{
unsigned char *ptr;
int len, i;
ptr = (unsigned char *)(tcph + 1);
len = (tcph->doff << 2) - sizeof(struct tcphdr);
while (len > 0) {
int opcode = *ptr++;
int opsize;
switch (opcode) {
case TCP_OPT_EOL:
return;
case TCP_OPT_NOP:
len--;
continue;
default:
opsize = *ptr++;
if (opsize < 2) /* silly options */
return;
if (opsize > len)
return; /* partial options */
if ((opcode == TCP_OPT_TIMESTAMP)
&& (opsize == TCP_OLEN_TIMESTAMP)) {
for (i = 0; i < TCP_OLEN_TIMESTAMP; i++)
*(ptr - 2 + i) = TCP_OPT_NOP;
return;
}
ptr += opsize - 2;
len -= opsize;
break;
}
}
}
static inline int tcp_in_add_toa(struct dp_vs_conn *conn, struct rte_mbuf *mbuf,
struct tcphdr *tcph)
{
uint32_t mtu;
struct tcpopt_addr *toa;
uint8_t *p, *q, *tail;
struct route_entry *rt;
if (unlikely(conn->af != AF_INET))
return EDPVS_NOTSUPP;
/*
* check if we can add the new option
*/
/* skb length and tcp option length checking */
if ((rt = mbuf->userdata) != NULL) {
mtu = rt->mtu;
} else if (conn->in_dev) { /* no route for fast-xmit */
mtu = conn->in_dev->mtu;
} else {
RTE_LOG(DEBUG, IPVS, "add toa: MTU unknown.\n");
return EDPVS_NOROUTE;
}
if (unlikely(mbuf->pkt_len > (mtu - sizeof(struct tcpopt_addr)))) {
RTE_LOG(DEBUG, IPVS, "add toa: need fragment.\n");
return EDPVS_FRAG;
}
/* maximum TCP header is 60, and 40 for options */
if (unlikely((60 - (tcph->doff << 2)) < sizeof(struct tcpopt_addr))) {
RTE_LOG(DEBUG, IPVS, "add toa: no TCP header room.\n");
return EDPVS_NOROOM;
}
/* check tail room and expand mbuf.
* have to pull all bits in segments for later operation. */
if (unlikely(mbuf_may_pull(mbuf, mbuf->pkt_len) != 0))
return EDPVS_INVPKT;
tail = (uint8_t *)rte_pktmbuf_append(mbuf, sizeof(struct tcpopt_addr));
if (unlikely(!tail)) {
RTE_LOG(DEBUG, IPVS, "add toa: no mbuf tail room.\n");
return EDPVS_NOROOM;
}
/*
* now add address option
*/
/* move data down, including existing tcp options
* @p is last data byte,
* @q is new position of last data byte */
p = tail - 1;
q = p + sizeof(struct tcpopt_addr);
while (p >= ((uint8_t *)tcph + sizeof(struct tcphdr))) {
*q = *p;
p--, q--;
}
/* insert toa right after TCP basic header */
toa = (struct tcpopt_addr *)(tcph + 1);
toa->opcode = TCP_OPT_ADDR;
toa->opsize = TCP_OLEN_ADDR;
toa->port = conn->cport;
toa->addr = conn->caddr.in.s_addr;
/* reset tcp header length */
tcph->doff += sizeof(struct tcpopt_addr) >> 2;
/* reset ip header total length */
ip4_hdr(mbuf)->total_length =
htons(ntohs(ip4_hdr(mbuf)->total_length)
+ sizeof(struct tcpopt_addr));
/* tcp csum will be recalc later,
* so as IP hdr csum since iph.tot_len has been chagned. */
return EDPVS_OK;
}
static void tcp_out_save_seq(struct rte_mbuf *mbuf,
struct dp_vs_conn *conn, struct tcphdr *th)
{
if (th->rst)
return;
/* out of order ? */
if (seq_before(ntohl(th->ack_seq), ntohl(conn->rs_end_ack))
&& conn->rs_end_ack != 0)
return;
if (th->syn && th->ack)
conn->rs_end_seq = htonl(ntohl(th->seq) + 1);
else
conn->rs_end_seq = htonl(ntohl(th->seq) + mbuf->pkt_len
- ip4_hdrlen(mbuf) - (th->doff << 2));
conn->rs_end_ack = th->ack_seq;
}
static void tcp_out_adjust_mss(struct tcphdr *tcph)
{
unsigned char *ptr;
int length;
ptr = (unsigned char *)(tcph + 1);
length = (tcph->doff << 2) - sizeof(struct tcphdr);
while (length > 0) {
int opcode = *ptr++;
int opsize;
switch (opcode) {
case TCP_OPT_EOL:
return;
case TCP_OPT_NOP:
length--;
continue;
default:
opsize = *ptr++;
if (opsize < 2) /* "silly options" */
return;
if (opsize > length)
return; /* partial options */
if ((opcode == TCP_OPT_MSS) && (opsize == TCP_OLEN_MSS)) {
uint16_t in_mss = ntohs(*(__be16 *) ptr);
in_mss -= TCP_OLEN_ADDR;
/* set mss, 16bit */
*((uint16_t *) ptr) = htons(in_mss);
/* re-calc csum later */
return;
}
ptr += opsize - 2;
length -= opsize;
break;
}
}
}
static int tcp_out_adjust_seq(struct dp_vs_conn *conn, struct tcphdr *tcph)
{
uint8_t i;
uint8_t *ptr;
int length;
/* synproxy seq change, including tcp hdr and check ack storm */
if (dp_vs_synproxy_snat_handler(tcph, conn) == 0) {
return EDPVS_OK; // ACK storm found
}
/* adjust ack sequence */
tcph->ack_seq = htonl(ntohl(tcph->ack_seq) - conn->fnat_seq.delta);
/* adjust sack sequence */
ptr = (uint8_t *)(tcph + 1);
length = (tcph->doff << 2) - sizeof(struct tcphdr);
/* Fast path for timestamp-only option */
if (length == TCP_OLEN_TSTAMP_ALIGNED &&
*(uint32_t *)ptr == htonl((TCP_OPT_NOP << 24) |
(TCP_OPT_NOP << 16) |
(TCP_OPT_TIMESTAMP << 8) |
TCP_OLEN_TIMESTAMP))
return EDPVS_OK;
while (length > 0) {
int opcode = *ptr++;
int opsize;
switch (opcode) {
case TCP_OPT_EOL:
return EDPVS_OK;
case TCP_OPT_NOP:
length--;
continue;
default:
opsize = *ptr++;
if (opsize < 2) /* silly options */
return EDPVS_OK;
if (opsize > length)
return EDPVS_OK; /* partial options */
if ((opcode == TCP_OPT_SACK) &&
(opsize >= (TCP_OLEN_SACK_BASE + TCP_OLEN_SACK_PERBLOCK))
&& !((opsize - TCP_OLEN_SACK_BASE) %
TCP_OLEN_SACK_PERBLOCK)) {
for (i = 0; i < opsize - TCP_OLEN_SACK_BASE;
i += TCP_OLEN_SACK_PERBLOCK) {
uint32_t *tmp = (uint32_t *) (ptr + i);
*tmp = htonl(ntohl(*tmp) -
conn->fnat_seq.delta);
tmp++;
*tmp = htonl(ntohl(*tmp) -
conn->fnat_seq.delta);
}
return EDPVS_OK;
}
ptr += opsize - 2;
length -= opsize;
break;
}
}
return EDPVS_OK;
}
static void tcp_out_init_seq(struct dp_vs_conn *conn, struct tcphdr *th)
{
conn->fnat_seq.fdata_seq = ntohl(th->seq) + 1;
}
/* set @verdict if failed to schedule */
static int tcp_conn_sched(struct dp_vs_proto *proto,
const struct dp_vs_iphdr *iph,
struct rte_mbuf *mbuf,
struct dp_vs_conn **conn,
int *verdict)
{
struct tcphdr *th, _tcph;
struct dp_vs_service *svc;
assert(proto && iph && mbuf && conn && verdict);
th = mbuf_header_pointer(mbuf, iph->len, sizeof(_tcph), &_tcph);
if (unlikely(!th)) {
*verdict = INET_DROP;
return EDPVS_INVPKT;
}
/* Syn-proxy step 2 logic: receive client's 3-handshacke ack packet */
/* When synproxy disabled, only SYN packets can arrive here.
* So don't judge SYNPROXY flag here! If SYNPROXY flag judged, and syn_proxy
* got disbled and keepalived reloaded, SYN packets for RS may never be sent. */
if (dp_vs_synproxy_ack_rcv(iph->af, mbuf, th, proto, conn, iph, verdict) == 0) {
/* Attention: First ACK packet is also stored in conn->ack_mbuf */
return EDPVS_PKTSTOLEN;
}
/* only TCP-SYN without other flag can be scheduled */
if (!th->syn || th->ack || th->fin || th->rst) {
char dbuf[64], sbuf[64];
const char *daddr, *saddr;
daddr = inet_ntop(iph->af, &iph->daddr, dbuf, sizeof(dbuf)) ? dbuf : "::";
saddr = inet_ntop(iph->af, &iph->saddr, sbuf, sizeof(sbuf)) ? sbuf : "::";
RTE_LOG(DEBUG, IPVS,
"%s: [%d] try sched non-SYN packet: [%c%c%c%c] %s:%d->%s:%d\n",
__func__, rte_lcore_id(),
th->syn ? 'S' : '.', th->fin ? 'F' : '.',
th->ack ? 'A' : '.', th->rst ? 'R' : '.',
saddr, ntohs(th->source), daddr, ntohs(th->dest));
/* Drop tcp packet which is send to vip and !vport */
if (g_defence_tcp_drop &&
(svc = dp_vs_lookup_vip(iph->af, iph->proto, &iph->daddr))) {
dp_vs_estats_inc(DEFENCE_TCP_DROP);
*verdict = INET_DROP;
return EDPVS_INVPKT;
}
*verdict = INET_ACCEPT;
return EDPVS_INVAL;
}
svc = dp_vs_service_lookup(iph->af, iph->proto,
&iph->daddr, th->dest, 0, mbuf, NULL);
if (!svc) {
/* Drop tcp packet which is send to vip and !vport */
if (g_defence_tcp_drop &&
(svc = dp_vs_lookup_vip(iph->af, iph->proto, &iph->daddr))) {
dp_vs_estats_inc(DEFENCE_TCP_DROP);
*verdict = INET_DROP;
return EDPVS_INVPKT;
}
*verdict = INET_ACCEPT;
return EDPVS_NOSERV;
}
*conn = dp_vs_schedule(svc, iph, mbuf, false);
if (!*conn) {
dp_vs_service_put(svc);
*verdict = INET_DROP;
return EDPVS_RESOURCE;
}
dp_vs_service_put(svc);
return EDPVS_OK;
}
static struct dp_vs_conn *
tcp_conn_lookup(struct dp_vs_proto *proto, const struct dp_vs_iphdr *iph,
struct rte_mbuf *mbuf, int *direct, bool reverse)
{
struct tcphdr *th, _tcph;
assert(proto && iph && mbuf);
th = mbuf_header_pointer(mbuf, iph->len, sizeof(_tcph), &_tcph);
if (unlikely(!th))
return NULL;
return dp_vs_conn_get(iph->af, iph->proto,
&iph->saddr, &iph->daddr, th->source, th->dest, direct, reverse);
}
static int tcp_fnat_in_handler(struct dp_vs_proto *proto,
struct dp_vs_conn *conn, struct rte_mbuf *mbuf)
{
struct tcphdr *th;
struct route_entry *rt = mbuf->userdata;
struct netif_port *dev = NULL;
int ip4hlen = ip4_hdrlen(mbuf);
if (mbuf_may_pull(mbuf, ip4hlen + sizeof(*th)) != 0)
return EDPVS_INVPKT;
th = tcp_hdr(mbuf);
if (unlikely(!th))
return EDPVS_INVPKT;
if (mbuf_may_pull(mbuf, ip4hlen + (th->doff<<2)) != 0)
return EDPVS_INVPKT;
/*
* for SYN packet
* 1. remove tcp timestamp option
* laddress for different client have diff timestamp.
* 2. save original TCP sequence for seq-adjust later.
* since TCP option will be change.
* 3. add TOA option
* so that RS with TOA module can get real client IP.
*/
if (th->syn && !th->ack) {
tcp_in_remove_ts(th);
tcp_in_init_seq(conn, mbuf, th);
tcp_in_add_toa(conn, mbuf, th);
}
/* add toa to first data packet */
if (ntohl(th->ack_seq) == conn->fnat_seq.fdata_seq
&& !th->syn && !th->rst && !th->fin)
tcp_in_add_toa(conn, mbuf, th);
tcp_in_adjust_seq(conn, th);
/* L4 translation */
th->source = conn->lport;
th->dest = conn->dport;
if (rt && rt->port)
dev = rt->port;
else if (conn->in_dev)
dev = conn->in_dev;
if (likely(dev && (dev->flag & NETIF_PORT_FLAG_TX_TCP_CSUM_OFFLOAD))) {
mbuf->l4_len = ntohs(ip4_hdr(mbuf)->total_length) - ip4hlen;
mbuf->l3_len = ip4hlen;
mbuf->ol_flags |= (PKT_TX_TCP_CKSUM | PKT_TX_IP_CKSUM | PKT_TX_IPV4);
th->check = rte_ipv4_phdr_cksum(ip4_hdr(mbuf), mbuf->ol_flags);
} else {
if (mbuf_may_pull(mbuf, mbuf->pkt_len) != 0)
return EDPVS_INVPKT;
tcp4_send_csum(ip4_hdr(mbuf), th);
}
return EDPVS_OK;
}
static int tcp_fnat_out_handler(struct dp_vs_proto *proto,
struct dp_vs_conn *conn, struct rte_mbuf *mbuf)
{
struct tcphdr *th;
struct route_entry *rt = mbuf->userdata;
struct netif_port *dev = NULL;
int ip4hlen = ip4_hdrlen(mbuf);
if (mbuf_may_pull(mbuf, ip4hlen + sizeof(*th)) != 0)
return EDPVS_INVPKT;
th = tcp_hdr(mbuf);
if (unlikely(!th))
return EDPVS_INVPKT;
if (mbuf_may_pull(mbuf, ip4hlen + (th->doff<<2)) != 0)
return EDPVS_INVPKT;
/* save last seq/ack from RS for RST when conn expire */
tcp_out_save_seq(mbuf, conn, th);
/* L4 translation */
th->source = conn->vport;
th->dest = conn->cport;
if (th->syn && th->ack)
tcp_out_adjust_mss(th);
/* adjust ACK/SACK from RS since inbound SEQ is changed */
if (tcp_out_adjust_seq(conn, th) != EDPVS_OK)
return EDPVS_INVPKT;
if (th->syn && th->ack)
tcp_out_init_seq(conn, th);
if (rt && rt->port)
dev = rt->port;
else if (conn->out_dev)
dev = conn->out_dev;
if (likely(dev && (dev->flag & NETIF_PORT_FLAG_TX_TCP_CSUM_OFFLOAD))) {
mbuf->l4_len = ntohs(ip4_hdr(mbuf)->total_length) - ip4hlen;
mbuf->l3_len = ip4hlen;
mbuf->ol_flags |= (PKT_TX_TCP_CKSUM | PKT_TX_IP_CKSUM | PKT_TX_IPV4);
th->check = rte_ipv4_phdr_cksum(ip4_hdr(mbuf), mbuf->ol_flags);
} else {
if (mbuf_may_pull(mbuf, mbuf->pkt_len) != 0)
return EDPVS_INVPKT;
tcp4_send_csum(ip4_hdr(mbuf), th);
}
return EDPVS_OK;
}
static int tcp_snat_in_handler(struct dp_vs_proto *proto,
struct dp_vs_conn *conn, struct rte_mbuf *mbuf)
{
struct tcphdr *th;
int ip4hlen = ip4_hdrlen(mbuf);
struct netif_port *dev = NULL;
struct route_entry *rt = mbuf->userdata;
if (mbuf_may_pull(mbuf, ip4hlen + sizeof(*th)) != 0)
return EDPVS_INVPKT;
th = tcp_hdr(mbuf);
if (unlikely(!th))
return EDPVS_INVPKT;
if (mbuf_may_pull(mbuf, ip4hlen + (th->doff<<2)) != 0)
return EDPVS_INVPKT;
/* L4 translation */
th->dest = conn->dport;
/* L4 re-checksum */
if (rt && rt->port)
dev = rt->port;
/* leverage HW TX TCP csum offload if possible */
if (likely(dev && (dev->flag & NETIF_PORT_FLAG_TX_TCP_CSUM_OFFLOAD))) {
mbuf->l4_len = ntohs(ip4_hdr(mbuf)->total_length) - ip4hlen;
mbuf->l3_len = ip4hlen;
mbuf->ol_flags |= (PKT_TX_TCP_CKSUM | PKT_TX_IP_CKSUM | PKT_TX_IPV4);
th->check = rte_ipv4_phdr_cksum(ip4_hdr(mbuf), mbuf->ol_flags);
} else {
if (mbuf_may_pull(mbuf, mbuf->pkt_len) != 0)
return EDPVS_INVPKT;
tcp4_send_csum(ip4_hdr(mbuf), th);
}
return EDPVS_OK;
}
static int tcp_snat_out_handler(struct dp_vs_proto *proto,
struct dp_vs_conn *conn, struct rte_mbuf *mbuf)
{
struct tcphdr *th;
int ip4hlen = ip4_hdrlen(mbuf);
struct netif_port *dev = NULL;
struct route_entry *rt = mbuf->userdata;
if (mbuf_may_pull(mbuf, ip4hlen + sizeof(*th)) != 0)
return EDPVS_INVPKT;
th = tcp_hdr(mbuf);
if (unlikely(!th))
return EDPVS_INVPKT;
if (mbuf_may_pull(mbuf, ip4hlen + (th->doff<<2)) != 0)
return EDPVS_INVPKT;
/* L4 translation */
th->source = conn->vport;
/* L4 re-checksum */
if (rt && rt->port)
dev = rt->port;
/* leverage HW TX TCP csum offload if possible */
if (likely(dev && (dev->flag & NETIF_PORT_FLAG_TX_TCP_CSUM_OFFLOAD))) {
mbuf->l4_len = ntohs(ip4_hdr(mbuf)->total_length) - ip4hlen;
mbuf->l3_len = ip4hlen;
mbuf->ol_flags |= (PKT_TX_TCP_CKSUM | PKT_TX_IP_CKSUM | PKT_TX_IPV4);
th->check = rte_ipv4_phdr_cksum(ip4_hdr(mbuf), mbuf->ol_flags);
} else {
if (mbuf_may_pull(mbuf, mbuf->pkt_len) != 0)
return EDPVS_INVPKT;
tcp4_send_csum(ip4_hdr(mbuf), th);
}
return EDPVS_OK;
}
static inline int tcp_state_idx(struct tcphdr *th)
{
if (th->rst)
return 3;
if (th->syn)
return 0;
if (th->fin)
return 1;
if (th->ack)
return 2;
return -1;
}
#ifdef CONFIG_DPVS_IPVS_DEBUG
static const char *tcp_state_name(int state)
{
if (state >= DPVS_TCP_S_LAST)
return "ERR!";
return tcp_state_names[state] ? tcp_state_names[state] : "<Unknown>";
}
#endif
static int tcp_state_trans(struct dp_vs_proto *proto, struct dp_vs_conn *conn,
struct rte_mbuf *mbuf, int dir)
{
struct tcphdr *th, _tcph;
int idx, off;
int new_state = DPVS_TCP_S_CLOSE;
assert(proto && conn && mbuf);
struct dp_vs_dest *dest = conn->dest;
unsigned conn_timeout = 0;
#ifdef CONFIG_DPVS_IPVS_DEBUG
char dbuf[64], cbuf[64];
const char *daddr, *caddr;
#endif
th = mbuf_header_pointer(mbuf, ip4_hdrlen(mbuf), sizeof(_tcph), &_tcph);
if (unlikely(!th))
return EDPVS_INVPKT;
if (dest->fwdmode == DPVS_FWD_MODE_DR)
off = 8;
else if (dir == DPVS_CONN_DIR_INBOUND)
off = 0;
else if (dir == DPVS_CONN_DIR_OUTBOUND)
off = 4;
else
return EDPVS_NOTSUPP; /* do not support INPUT_ONLY now */
if ((idx = tcp_state_idx(th)) < 0) {
RTE_LOG(DEBUG, IPVS, "tcp_state_idx=%d !\n", idx);
goto tcp_state_out;
}
new_state = tcp_states[off + idx].next_state[conn->state];
tcp_state_out:
if (new_state == conn->state)
return EDPVS_OK;
/* state changed */
#ifdef CONFIG_DPVS_IPVS_DEBUG
daddr = inet_ntop(conn->af, &conn->daddr, dbuf, sizeof(dbuf)) ? dbuf : "::";
caddr = inet_ntop(conn->af, &conn->caddr, cbuf, sizeof(cbuf)) ? cbuf : "::";
RTE_LOG(DEBUG, IPVS, "state trans: %s %s [%c%c%c%c] %s:%u->%s:%u "
" state %s->%s conn.refcnt %d\n",
proto->name, dir == DPVS_CONN_DIR_OUTBOUND ? "out" : "in",
th->syn ? 'S' : '.', th->fin ? 'F' : '.',
th->ack ? 'A' : '.', th->rst ? 'R' : '.',
caddr, ntohs(conn->cport),
daddr, ntohs(conn->dport),
tcp_state_name(conn->state),
tcp_state_name(new_state),
rte_atomic32_read(&conn->refcnt));
#endif
conn->old_state = conn->state; // old_state called when connection reused
conn->state = new_state;
if (new_state == DPVS_TCP_S_ESTABLISHED) {
conn_timeout = dp_vs_get_conn_timeout(conn);
if (unlikely(conn_timeout > 0))
conn->timeout.tv_sec = conn_timeout;
else
conn->timeout.tv_sec = tcp_timeouts[new_state];
} else {
conn->timeout.tv_sec = tcp_timeouts[new_state];
}
if (dest) {
if (!(conn->flags & DPVS_CONN_F_INACTIVE)
&& (new_state != DPVS_TCP_S_ESTABLISHED)) {
rte_atomic32_dec(&dest->actconns);
rte_atomic32_inc(&dest->inactconns);
conn->flags |= DPVS_CONN_F_INACTIVE;
} else if ((conn->flags & DPVS_CONN_F_INACTIVE)
&& (new_state == DPVS_TCP_S_ESTABLISHED)) {
rte_atomic32_inc(&dest->actconns);
rte_atomic32_dec(&dest->inactconns);
conn->flags &= ~DPVS_CONN_F_INACTIVE;
}
}
return EDPVS_OK;
}
struct rte_mempool *get_mbuf_pool(const struct dp_vs_conn *conn, int dir)
{
struct flow4 fl4;
struct netif_port *dev;
struct route_entry *rt = NULL;
/* we need oif for correct rte_mempoll,
* most likely oif is conn->in/out_dev (fast-xmit),
* if not, determine output device by route. */
dev = ((dir == DPVS_CONN_DIR_INBOUND) ? conn->in_dev : conn->out_dev);
if (unlikely(!dev)) {
memset(&fl4, 0, sizeof(struct flow4));
if (dir == DPVS_CONN_DIR_INBOUND) {
fl4.saddr = conn->laddr.in;
fl4.daddr = conn->daddr.in;
fl4.sport = conn->lport;
fl4.dport = conn->dport;
} else {
fl4.saddr = conn->vaddr.in;
fl4.daddr = conn->caddr.in;
fl4.sport = conn->vport;
fl4.dport = conn->cport;
}
fl4.proto = IPPROTO_TCP;
if ((rt = route4_output(&fl4)) == NULL)
return NULL;
dev = rt->port;
route4_put(rt);
}
return dev->mbuf_pool;
}
static int tcp_send_rst(struct dp_vs_proto *proto,
struct dp_vs_conn *conn, int dir)
{
struct rte_mempool *pool;
struct rte_mbuf *mbuf = NULL;
struct tcphdr *th;
struct ipv4_hdr *iph;
if (conn->state != DPVS_TCP_S_ESTABLISHED) {
/* RTE_LOG(WARNING, IPVS, "%s: only RST in ESTABLISHED.\n", __func__); */
return EDPVS_OK;
}
pool = get_mbuf_pool(conn, dir);
if (!pool)
return EDPVS_NOROUTE;
mbuf = rte_pktmbuf_alloc(pool);
if (!mbuf)
return EDPVS_NOMEM;
mbuf->userdata = NULL; /* make sure "no route info" */
/*
* reserve head room ?
* mbuf has alreay configured header room
* RTE_PKTMBUF_HEADROOM for lower layer headers.
*/
assert(rte_pktmbuf_headroom(mbuf) >= 128); /* how to reserve. >_< */
/* rte_pktmbuf_mtod ? */
th = (struct tcphdr *)rte_pktmbuf_prepend(mbuf, sizeof(struct tcphdr));
if (!th) {
rte_pktmbuf_free(mbuf);
return EDPVS_NOROOM;
}
memset(th, 0, sizeof(struct tcphdr));
if (dir == DPVS_CONN_DIR_INBOUND) {
th->source = conn->cport; /* will be translated */
th->dest = conn->vport;
th->seq = conn->rs_end_ack;
if (conn->dest->fwdmode == DPVS_FWD_MODE_FNAT)
th->seq = htonl(ntohl(th->seq) - conn->fnat_seq.delta);
} else {
th->source = conn->dport;
th->dest = conn->lport;
th->seq = conn->rs_end_seq;
}
th->ack_seq = 0;
th->doff = sizeof(struct tcphdr) >> 2;
th->rst = 1;
/* IP header (before translation) */
iph = (struct ipv4_hdr *)rte_pktmbuf_prepend(mbuf, sizeof(struct ipv4_hdr));
if (!iph) {
rte_pktmbuf_free(mbuf);
return EDPVS_NOROOM;
}
iph->version_ihl = 0x45;
iph->total_length = htons(mbuf->pkt_len);
iph->packet_id = 0;
iph->fragment_offset = htons(IPV4_HDR_DF_FLAG);
iph->time_to_live = 64;
iph->next_proto_id = IPPROTO_TCP;
if (dir == DPVS_CONN_DIR_INBOUND) {
iph->src_addr = conn->caddr.in.s_addr;
iph->dst_addr = conn->vaddr.in.s_addr;
} else {
iph->src_addr = conn->daddr.in.s_addr;
iph->dst_addr = conn->laddr.in.s_addr;
}
iph->hdr_checksum = 0;
tcp4_send_csum(iph, th);
ip4_send_csum(iph);
if (dir == DPVS_CONN_DIR_INBOUND)
conn->packet_xmit(proto, conn, mbuf);
else
conn->packet_out_xmit(proto, conn, mbuf);
return EDPVS_OK;
}
static int tcp_conn_expire(struct dp_vs_proto *proto,
struct dp_vs_conn *conn)
{
int err;
assert(proto && conn && conn->dest);
if (conn->dest->fwdmode == DPVS_FWD_MODE_NAT
|| conn->dest->fwdmode == DPVS_FWD_MODE_FNAT) {
/* send RST to RS and client */
err = tcp_send_rst(proto, conn, DPVS_CONN_DIR_INBOUND);
if (err != EDPVS_OK)
RTE_LOG(WARNING, IPVS, "%s: fail RST RS.\n", __func__);
err = tcp_send_rst(proto, conn, DPVS_CONN_DIR_OUTBOUND);
if (err != EDPVS_OK)
RTE_LOG(WARNING, IPVS, "%s: fail RST Client.\n", __func__);
}
return EDPVS_OK;
}
static void defence_tcp_drop_handler(vector_t tokens)
{
RTE_LOG(INFO, IPVS, "defence_tcp_drop ON\n");
g_defence_tcp_drop = 1;
}
static inline void timeout_handler_template(vector_t tokens,
const char *tcp_state, int idx, int default_timeout)
{
char *str = set_value(tokens);
int timeout;
assert(str && idx >= DPVS_TCP_S_NONE && idx <= DPVS_TCP_S_LAST);
timeout = atoi(str);
if (timeout > IPVS_TIMEOUT_MIN && timeout < IPVS_TIMEOUT_MAX) {
RTE_LOG(INFO, IPVS, "tcp_timeout_%s = %d\n", tcp_state, timeout);
tcp_timeouts[idx] = timeout;
} else {
RTE_LOG(INFO, IPVS, "invalid tcp_timeout_%s, using default %d\n",
tcp_state, default_timeout);
tcp_timeouts[idx] = default_timeout;
}
FREE_PTR(str);
}
static void timeout_none_handler(vector_t tokens)
{
timeout_handler_template(tokens, "none", DPVS_TCP_S_NONE, 2);
}
static void timeout_established_handler(vector_t tokens)
{
timeout_handler_template(tokens, "established", DPVS_TCP_S_ESTABLISHED, 90);
}
static void timeout_syn_sent_handler(vector_t tokens)
{
timeout_handler_template(tokens, "syn_sent", DPVS_TCP_S_SYN_SENT, 3);