forked from gao-feng/colo-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxt_PMYCOLO.c
1696 lines (1388 loc) · 41.7 KB
/
xt_PMYCOLO.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
/*
* PRIMARY side proxy module for COLO
*
* Copyright (c) 2014, 2015 Fujitsu Limited.
* Copyright (c) 2014, 2015 HUAWEI TECHNOLOGIES CO.,LTD.
* Copyright (c) 2014, 2015 Intel Corporation.
*
* Authors:
* Gao feng <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/list_sort.h>
#include <linux/freezer.h>
#include <linux/kthread.h>
#include <linux/netfilter_arp.h>
#include <linux/netfilter/xt_COLO.h>
#include <net/netfilter/nf_conntrack_core.h>
#include <net/netfilter/nf_conntrack_zones.h>
#include <net/netfilter/nf_queue.h>
#include <net/ip.h>
#include <net/arp.h>
#include <net/tcp.h>
#include <linux/icmp.h>
#include "xt_COLO.h"
#include "nf_conntrack_colo.h"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gao feng <[email protected]>");
MODULE_DESCRIPTION("Xtables: primary proxy module for colo.");
static char *sec_dev = NULL;
module_param(sec_dev, charp, 0);
MODULE_PARM_DESC(sec_dev,
"The physical nic which slaver packets coming in");
DEFINE_PER_CPU(struct nf_conn, slaver_conntrack);
static DECLARE_WAIT_QUEUE_HEAD(kcolo_wait);
static bool colo_compare_skb(struct sk_buff *skb,
struct sk_buff *skb1,
u32 dataoff, u32 dataoff1,
u32 len)
{
u32 mpos, spos, moffset, soffset;
u32 msize, ssize;
char *mbuff, *sbuff;
if (len == 0)
return true;
mpos = spos = moffset = soffset = 0;
msize = ssize = len;
mbuff = sbuff = NULL;
do {
if (moffset == 0) {
colo_get_data(skb, &mbuff, mpos + dataoff, &msize);
mpos += msize;
}
if (soffset == 0) {
colo_get_data(skb1, &sbuff, spos + dataoff1, &ssize);
spos += ssize;
}
if (mbuff == NULL || sbuff == NULL) {
pr_dbg("get mbuff %p, sbuff %p, do checkpoint\n", mbuff, sbuff);
return false;
}
if (memcmp(mbuff + moffset, sbuff + soffset,
MIN(msize - moffset, ssize - soffset))) {
int i;
pr_dbg("compare data inconsist, do checkpoint\n");
pr_dbg("master: pos %u\n", mpos);
for (i = 0; i < msize; i++)
pr_dbg("%c ", *(mbuff + i));
pr_dbg("\nslaver: pos %u\n", spos);
for (i = 0; i < ssize; i++)
pr_dbg("%c ", *(sbuff + i));
pr_dbg("\n");
return false;
}
if (msize - moffset > ssize - soffset) {
moffset += ssize - soffset;
ssize = len - spos;
soffset = 0;
continue;
} else if (ssize - soffset > msize - moffset) {
soffset += msize - moffset;
msize = len - mpos;
moffset = 0;
continue;
} else {
moffset = soffset = 0;
msize = len - mpos;
ssize = len - spos;
}
} while (mpos != len || spos != len);
return true;
}
static int colo_send_checkpoint_req(struct colo_primary *colo);
static void
__colo_compare_common(struct colo_primary *colo,
struct nf_conn_colo *conn,
bool (*compare)(struct sk_buff *,
struct sk_buff *,
u32 *, u32, u32, u32, u8 *),
bool (*pre_compare)(struct nf_conn_colo *,
struct nf_queue_entry *,
u32 *, u32))
{
struct nf_queue_entry *e, *n;
LIST_HEAD(master_head);
struct sk_buff_head slaver_head;
struct sk_buff *skb;
bool sort = false;
bool checkpoint = false;
union nf_conn_colo_tcp *p = NULL;
u32 max_ack = 0, compared_seq;
u32 swin = 0; u16 mscale = 1;
u8 free, times = 0;
/* Get checkpoint lock */
spin_lock(&conn->chk_lock);
skb_queue_head_init(&slaver_head);
if (nf_ct_protonum((struct nf_conn *)conn->nfct) == IPPROTO_TCP) {
p = (union nf_conn_colo_tcp *) conn->proto;
}
spin_lock_bh(&conn->lock);
if (list_empty(&conn->entry_list)) {
spin_unlock_bh(&conn->lock);
goto out;
}
list_splice_init(&conn->entry_list, &master_head);
if (p) {
max_ack = p->p.mack;
compared_seq = p->p.compared_seq;
mscale = p->p.mscale;
}
spin_unlock_bh(&conn->lock);
spin_lock_bh(&conn->slaver_pkt_queue.lock);
skb_queue_splice_init(&conn->slaver_pkt_queue, &slaver_head);
if (p) {
if (p->p.sack && before(p->p.sack, max_ack))
max_ack = p->p.sack;
swin = p->p.swin;
}
spin_unlock_bh(&conn->slaver_pkt_queue.lock);
restart:
skb = skb_peek(&slaver_head);
list_for_each_entry_safe(e, n, &master_head, list) {
if (times++ > 64 || need_resched()) {
/* Do not cost cpu for a long time. */
pr_dbg("times %d exceed 64 or need_resched, do schedule\n", times);
spin_lock_bh(&conn->lock);
list_splice_init(&master_head, &conn->entry_list);
if (p) {
p->p.sort = true;
}
spin_unlock_bh(&conn->lock);
spin_lock_bh(&conn->slaver_pkt_queue.lock);
skb_queue_splice_init(&slaver_head, &conn->slaver_pkt_queue);
spin_unlock_bh(&conn->slaver_pkt_queue.lock);
spin_unlock(&conn->chk_lock);
schedule();
pr_dbg("kcolo_thread come back!\n");
spin_lock(&conn->chk_lock);
spin_lock_bh(&conn->lock);
if (list_empty(&conn->entry_list)) {
spin_unlock_bh(&conn->lock);
goto out;
}
list_splice_init(&conn->entry_list, &master_head);
spin_unlock_bh(&conn->lock);
spin_lock_bh(&conn->slaver_pkt_queue.lock);
skb_queue_splice_init(&conn->slaver_pkt_queue, &slaver_head);
spin_unlock_bh(&conn->slaver_pkt_queue.lock);
times = 0;
/* restart compare this connection */
goto restart;
}
/* ugly, entry is freed & handled by pre_compare */
if (pre_compare && pre_compare(conn, e, &compared_seq, max_ack))
continue;
next:
if (skb == NULL) {
struct nf_conn *ct = (struct nf_conn *) conn->nfct;
if (test_bit(IPS_DYING_BIT, &ct->status)) {
/* timeout, it's a long time, there is something wrong
* with slaver, we should help him. */
pr_dbg("conn %p timeout, should do checkpoint\n", ct);
checkpoint = true;
}
break;
}
free = 0;
if (!compare(e->skb, skb, &compared_seq, max_ack, mscale, swin, &free)) {
/* should do checkpoint */
checkpoint = true;
break;
}
if (free & COLO_COMPARE_FREE_SLAVER) {
__skb_unlink(skb, &slaver_head);
kfree_skb(skb);
skb = skb_peek(&slaver_head);
}
if (free & COLO_COMPARE_FREE_MASTER) {
list_del_init(&e->list);
nf_reinject(e, NF_STOP);
} else if (!(free & COLO_COMPARE_NEXT_MASTER)) {
goto next;
}
}
spin_lock_bh(&conn->lock);
if (!list_empty(&master_head)) {
__list_splice(&master_head, &conn->entry_list, conn->entry_list.next);
sort = true;
}
if (p) {
if (before(p->p.compared_seq, compared_seq))
p->p.compared_seq = compared_seq;
p->p.sort = sort;
}
spin_unlock_bh(&conn->lock);
spin_lock_bh(&conn->slaver_pkt_queue.lock);
if (!skb_queue_empty(&slaver_head)) {
__skb_queue_splice(&slaver_head, (struct sk_buff *) &conn->slaver_pkt_queue,
conn->slaver_pkt_queue.next);
conn->slaver_pkt_queue.qlen += slaver_head.qlen;
}
spin_unlock_bh(&conn->slaver_pkt_queue.lock);
out:
spin_unlock(&conn->chk_lock);
if (checkpoint)
colo_send_checkpoint_req(colo);
}
static bool colo_compare_other_skb(struct sk_buff *skb,
struct sk_buff *skb1,
u32 *compared_seq,
u32 a, u32 b, u32 c,
u8 *free)
{
pr_dbg("compare other: get master skb %p, slaver skb %p\n", skb, skb1);
*free = COLO_COMPARE_FREE_MASTER | COLO_COMPARE_FREE_SLAVER;
if (skb->len != skb1->len) {
pr_dbg("master skb length %d, slaver length %d\n",
skb->len, skb1->len);
return false;
}
return colo_compare_skb(skb, skb1, 0, 0, skb->len);
}
static bool colo_compare_icmp_skb(struct sk_buff *skb,
struct sk_buff *skb1,
u32 *compared_seq,
u32 a, u32 b, u32 c,
u8 *free)
{
struct colo_icmp_cb *cb, *cb1;
*free = COLO_COMPARE_FREE_SLAVER | COLO_COMPARE_FREE_MASTER;
cb = COLO_SKB_CB(skb);
cb1 = COLO_SKB_CB(skb1);
if ((cb->type == cb1->type) && (cb->code == cb1->code)) {
if (cb->type == ICMP_REDIRECT) {
if (cb->gw != cb1->gw)
return false;
} else if ((cb->type == ICMP_DEST_UNREACH) && (cb->code == ICMP_FRAG_NEEDED)) {
if (cb->mtu != cb1->mtu)
return false;
}
} else {
pr_dbg("master type,code %u:%u, slaver %u:%u\n",
cb->type, cb->code, cb1->type, cb1->code);
return false;
}
return true;
}
static bool colo_compare_udp_skb(struct sk_buff *skb,
struct sk_buff *skb1,
u32 *compared_seq,
u32 a, u32 b, u32 c,
u8 *free)
{
struct colo_udp_cb *cb, *cb1;
pr_dbg("compare udp: get master skb %p, slaver skb %p\n", skb, skb1);
*free = COLO_COMPARE_FREE_MASTER | COLO_COMPARE_FREE_SLAVER;
if (skb->len != skb1->len) {
pr_dbg("master skb length %d, slaver length %d\n",
skb->len, skb1->len);
return false;
}
cb = COLO_SKB_CB(skb);
cb1 = COLO_SKB_CB(skb1);
if (cb->size != cb1->size) {
pr_dbg("master udp payload %u, slaver payload %u\n",
cb->size, cb1->size);
return false;
}
return colo_compare_skb(skb, skb1, cb->dataoff, cb1->dataoff, cb->size);
}
static void colo_compare_other(struct colo_primary *colo,
struct nf_conn_colo *conn)
{
return __colo_compare_common(colo, conn,
colo_compare_other_skb,
NULL);
}
static void colo_compare_icmp(struct colo_primary *colo,
struct nf_conn_colo *conn)
{
return __colo_compare_common(colo, conn,
colo_compare_icmp_skb,
NULL);
}
static void colo_compare_udp(struct colo_primary *colo,
struct nf_conn_colo *conn)
{
return __colo_compare_common(colo, conn,
colo_compare_udp_skb,
NULL);
}
/*
* return true is skb need not to compare.
* FIXME: compare ooo packet
*/
static bool colo_pre_compare_tcp_skb(struct nf_conn_colo *conn,
struct nf_queue_entry *e,
u32 *compared_seq,
u32 max_ack)
{
struct sk_buff *skb = e->skb;
struct colo_tcp_cb *cb = COLO_SKB_CB(skb);
union nf_conn_colo_tcp *p = (union nf_conn_colo_tcp *) conn->proto;
u32 win = cb->win << p->p.mscale;
pr_dbg("master skb seq %u, end %u, ack %u, max ack %u, compared_seq %u, win %u, slaver win %u\n",
cb->seq, cb->seq_end, cb->ack, max_ack, *compared_seq, win, p->p.swin);
if (unlikely(cb->syn) && (ACCESS_ONCE(conn->flags) & COLO_CONN_SYN_RECVD)) {
/* syn must be first in master and slaver. */
pr_dbg("get slaver's syn, send master's syn out\n");
*compared_seq = cb->seq_end;
list_del_init(&e->list);
nf_reinject(e, NF_STOP);
return true;
} else if (unlikely(cb->rst)) {
return false;
} else if (!after(cb->seq_end, *compared_seq)) {
if(after(cb->ack, max_ack)) {
pr_dbg("%u wait for slaver's ack %u\n",
cb->ack, max_ack);
return true;
}
if (win > p->p.swin) {
struct tcphdr *th;
u16 new_win = p->p.swin >> p->p.mscale;
if (!new_win) {
pr_dbg("slaver send zero window packet, help slaver, do checkpoint\n");
return false;
}
if (!skb_make_writable(skb, cb->thoff + sizeof(*th)))
return true;
th = (void *) skb->data + cb->thoff;
inet_proto_csum_replace4(&th->check, skb, th->window, htons(new_win), 0);
th->window = htons(new_win);
pr_dbg("change window form %u to %u & set out\n", win, ntohs(th->window));
}
pr_dbg("set out already compared data\n");
list_del_init(&e->list);
nf_reinject(e, NF_STOP);
return true;
}
return false;
}
static bool colo_compare_tcp_skb(struct sk_buff *skb,
struct sk_buff *skb1,
u32 *compared_seq,
u32 max_ack, u32 mscale,
u32 swin, u8 *free)
{
struct colo_tcp_cb *cb, *cb1;
cb = COLO_SKB_CB(skb);
cb1 = COLO_SKB_CB(skb1);
pr_dbg("cb seq %u, end %u, ack %u, cb1 seq %u, end %u, ack %u, max_ack %u\n",
cb->seq, cb->seq_end, cb->ack, cb1->seq, cb1->seq_end, cb1->ack, max_ack);
*free = 0;
/* both rst packet */
if (unlikely(cb->rst || cb1->rst)) {
if ((cb->rst ^ cb1->rst) || (cb->seq_end != cb1->seq_end)) {
pr_dbg("rst diff cb %d, seq_end %u, cb1 %d, seq_end %u\n",
cb->rst, cb->seq_end, cb1->rst, cb1->seq_end);
return false;
}
pr_dbg("send out master rst packet\n");
*free = COLO_COMPARE_FREE_MASTER | COLO_COMPARE_FREE_SLAVER;
pr_dbg("free slaver rst packet\n");
return true;
}
/* the retrans packet's seq may not be cutted,
* so cut it to the slaver seq alignment. */
if (unlikely(before(cb->seq, *compared_seq) && after(cb->seq_end, *compared_seq))) {
pr_dbg("maybe retrans, seq %u, seq_end %u, compared %u dataoff %u\n",
cb->seq, cb->seq_end, *compared_seq, cb->dataoff);
cb->dataoff += *compared_seq - cb->seq;
cb->seq = *compared_seq;
pr_dbg("after seq %u, dataoff %u\n", cb->seq, cb->dataoff);
}
/* start must be same */
if (WARN_ONCE(cb->seq != cb1->seq, "master seq %u slaver seq %u",
cb->seq, cb1->seq))
return false;
if (likely(!(cb1->fin || cb->fin)))
goto compare;
if (cb1->fin && cb->fin) {
if (cb1->seq_end != cb->seq_end) {
pr_dbg("get differenct fin seq %u %u\n",
cb->seq_end, cb1->seq_end);
return false;
}
/* pure fin packet */
pr_dbg("get slaver fin packet, cb1 seq %u,seq_end %u\n",
cb1->seq, cb1->seq_end);
pr_dbg("get master fin packet, cb seq %u,seq_end %u\n",
cb->seq, cb->seq_end);
if (--cb1->seq_end == cb1->seq) {
pr_dbg("free slaver pure fin packet\n");
/* both fin packet, at the same start and same end. */
BUG_ON(cb->seq_end -1 != cb->seq);
/* pure fin packet, send out */
/* let it go, let it go */
*free = COLO_COMPARE_FREE_SLAVER;
if (!after(cb->ack, max_ack) && ((cb->win << mscale) <= swin)) {
*free |= COLO_COMPARE_FREE_MASTER;
pr_dbg("send out master pure fin packet\n");
} else {
/* Slaver hasn't ack the data. do not send out this skb */
*free |= COLO_COMPARE_NEXT_MASTER;
pr_dbg("buffer skb, seq %u, seq_end %u, ack %u, mwin %u, swin %u\n",
cb->seq, cb->seq_end, cb->ack, cb->win << mscale, swin);
}
*compared_seq = cb->seq_end;
return true;
}
cb->seq_end -= 1;
} else if (cb1->fin && !cb->fin) {
if (!after(cb1->seq_end, cb->seq_end)) {
pr_dbg("slaver fin packet seq is %u while master packet seq %u\n",
cb1->seq_end, cb->seq_end);
return false;
}
cb1->seq_end -= 1;
} else if (!cb1->fin && cb->fin) {
if (!after(cb->seq_end, cb1->seq_end)) {
pr_dbg("master fin packet seq is %u while slaver packet seq %u\n",
cb->seq_end, cb1->seq_end);
return false;
}
cb->seq_end -= 1;
}
compare:
/* slaver is fin packet or both fin/non-fin packet */
if (!after(cb->seq_end, cb1->seq_end)) {
u32 len = cb->seq_end - cb->seq;
u32 seq_end = cb->seq_end;
if (!colo_compare_skb(skb, skb1, cb->dataoff, cb1->dataoff, len))
return false;
/* data is consist */
/* restore fin packet, get ready for next round */
cb1->seq_end += cb1->fin;
if (cb1->seq_end == seq_end + cb->fin) {
/* slaver packet has finished its jod */
pr_dbg("free slaver skb seq %u, seq_end %u, ack %u\n",
cb1->seq, cb1->seq_end, cb1->ack);
*compared_seq = cb1->seq_end;
*free = COLO_COMPARE_FREE_SLAVER;
} else {
/* one part of slaver packet still need to be compared */
cb1->dataoff += len;
*compared_seq = cb1->seq = seq_end + cb->fin;
pr_dbg("update slaver skb seq to %u\n", cb1->seq);
}
/* let it go, let it go */
if (!after(cb->ack, max_ack) && ((cb->win << mscale) <= swin)) {
pr_dbg("send out master skb, seq %u, seq_end %u, ack %u\n",
cb->seq, cb->seq_end, cb->ack);
*free |= COLO_COMPARE_FREE_MASTER;
} else {
/* Slaver hasn't ack the data. do not send out this skb */
*free |= COLO_COMPARE_NEXT_MASTER;
pr_dbg("buffer skb, seq %u, seq_end %u, ack %u, mwin %u, swin %u\n",
cb->seq, cb->seq_end, cb->ack, cb->win << mscale, swin);
}
} else {
u32 len = cb1->seq_end - cb1->seq;
if (!colo_compare_skb(skb, skb1, cb->dataoff, cb1->dataoff, len))
return false;
/* master packet is longer than skaver packet, compare the
* same part, and move the seq & dataoff */
cb->dataoff += len;
*compared_seq = cb->seq = cb1->seq_end;
/* restore fin packet, get ready for next round */
cb->seq_end += cb->fin;
*free |= COLO_COMPARE_FREE_SLAVER;
}
return true;
}
static void colo_compare_tcp(struct colo_primary *colo,
struct nf_conn_colo *conn)
{
return __colo_compare_common(colo, conn,
colo_compare_tcp_skb,
colo_pre_compare_tcp_skb);
}
static int kcolo_thread(void *dummy)
{
struct colo_primary *colo = dummy;
struct colo_node *node = container_of(colo, struct colo_node, u.p);
set_freezable();
while (!kthread_should_stop()) {
struct nf_conn_colo *conn = NULL;
DECLARE_WAITQUEUE(wait, current);
spin_lock_bh(&node->lock);
if(!list_empty(&node->list))
conn = list_first_entry(&node->list, struct nf_conn_colo, conn_list);
if (!conn || colo->checkpoint) {
add_wait_queue(&colo->wait, &wait);
set_current_state(TASK_INTERRUPTIBLE);
spin_unlock_bh(&node->lock);
pr_dbg("kcolo_thread, no conn, schedule out, chk %d\n", colo->checkpoint);
try_to_freeze();
schedule();
__set_current_state(TASK_RUNNING);
remove_wait_queue(&colo->wait, &wait);
continue;
}
pr_dbg("get conn %p\n", conn->nfct);
list_move_tail(&conn->conn_list, &node->wait_list);
spin_unlock_bh(&node->lock);
// get reference, the last nf_reinject may trigger conntrack destruction.
nf_conntrack_get(conn->nfct);
if (nf_ct_protonum((struct nf_conn *)conn->nfct) == IPPROTO_TCP) {
colo_compare_tcp(colo, conn);
} else if (nf_ct_protonum((struct nf_conn *)conn->nfct) == IPPROTO_UDP) {
colo_compare_udp(colo, conn);
} else if (nf_ct_protonum((struct nf_conn *)conn->nfct) == IPPROTO_ICMP) {
colo_compare_icmp(colo, conn);
} else {
colo_compare_other(colo, conn);
}
// release the reference, this conntrack can go now
nf_conntrack_put(conn->nfct);
}
pr_dbg("FUCKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK EXIT\n");
return 0;
}
static void colo_setup_checkpoint_by_id(u32 id);
static int colo_enqueue_icmp_packet(struct nf_conn_colo *conn,
struct nf_queue_entry *entry)
{
struct sk_buff *skb = entry->skb;
struct icmphdr *icmph, _ih;
struct colo_icmp_cb *cb = COLO_SKB_CB(skb);
icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
if (icmph == NULL) {
return -1;
}
cb->type = icmph->type;
cb->code = icmph->code;
if ((cb->type == ICMP_ECHO) || (cb->type == ICMP_ECHOREPLY)) {
cb->seq = ntohs(icmph->un.echo.sequence);
cb->id = ntohs(icmph->un.echo.id);
pr_dbg("[master]conn %p, type %u, seq %u, id %d\n",
conn->nfct, cb->type, cb->seq, cb->id);
} else if (cb->type == ICMP_REDIRECT) {
cb->gw = icmph->un.gateway;
} else if ((cb->type == ICMP_DEST_UNREACH) && (cb->code == ICMP_FRAG_NEEDED)) {
cb->mtu = icmph->un.frag.mtu;
}
list_add_tail(&entry->list, &conn->entry_list);
return 0;
}
static int colo_tcp_seq_cmp(void *p, struct list_head *a,
struct list_head *b)
{
struct nf_queue_entry *ea = list_entry(a, struct nf_queue_entry, list);
struct nf_queue_entry *eb = list_entry(b, struct nf_queue_entry, list);
struct colo_tcp_cb *cba = COLO_SKB_CB(ea->skb);
struct colo_tcp_cb *cbb = COLO_SKB_CB(eb->skb);
return after(cba->seq, cbb->seq);
}
static int colo_enqueue_tcp_packet(struct nf_conn_colo *conn,
struct nf_queue_entry *entry)
{
struct sk_buff *skb = entry->skb;
struct tcphdr *th;
union nf_conn_colo_tcp *proto = (union nf_conn_colo_tcp *) conn->proto;
struct colo_tcp_cb *cb, *cb1;
struct nf_queue_entry *e, *e_next;
cb = COLO_SKB_CB(skb);
th = colo_get_tcphdr(nf_ct_l3num((struct nf_conn*) conn->nfct),
skb, cb, &proto->p.mscale);
if (th == NULL)
return -1;
pr_dbg("DEBUG: master: enqueue skb seq %u, seq_end %u, ack %u, LEN: %u\n",
cb->seq, cb->seq_end, cb->ack, cb->seq_end - cb->seq);
/* Reopen? */
if ((proto->p.mrcv_nxt == 0) || (th->syn && cb->seq_end != proto->p.mrcv_nxt)) {
/* Init */
proto->p.srcv_nxt = proto->p.mrcv_nxt = cb->seq_end;
proto->p.sack = proto->p.mack = cb->ack;
proto->p.compared_seq = cb->seq;
pr_dbg("syn %d, seq_end %u, rcv_nxt is %u\n",
th->syn, cb->seq_end, proto->p.mrcv_nxt);
if (th->syn && (conn->flags & COLO_CONN_SYN_RECVD)) {
nf_reinject(entry, NF_STOP);
} else {
list_add_tail(&entry->list, &conn->entry_list);
}
return 0;
}
if (before(proto->p.mack, cb->ack))
proto->p.mack = cb->ack;
pr_dbg("master max ack %u, rcv_nxt %u, window size %u\n",
proto->p.mack, proto->p.mrcv_nxt, cb->win << proto->p.mscale);
if(th->rst || th->fin) {
pr_dbg("master received rst/fin %d/%d\n", th->rst, th->fin);
}
if (proto->p.sort) {
proto->p.sort = false;
list_sort(NULL, &conn->entry_list, colo_tcp_seq_cmp);
}
if (!after(cb->seq_end, proto->p.mrcv_nxt)) {
struct sk_buff *skb1;
/* we should add packet to entry_list even it is retransed,
* since this packet's ack may be bigger than max(mack, sack) */
list_for_each_entry_safe(e, e_next, &conn->entry_list, list) {
skb1 = e->skb;
cb1 = COLO_SKB_CB(skb1);
if (!after(cb->seq, cb1->seq)) {
list_add_tail(&entry->list, &e->list);
pr_dbg("insert before cb1 seq %u, seq_end %u\n",
cb1->seq, cb1->seq_end);
return 0;
}
}
list_add_tail(&entry->list, &conn->entry_list);
} else if (!after(cb->seq, proto->p.mrcv_nxt)) {
// at least part of data is new.
u32 nrcv_nxt;
cb->dataoff += (proto->p.mrcv_nxt - cb->seq);
cb->seq = proto->p.mrcv_nxt;
nrcv_nxt = proto->p.mrcv_nxt = cb->seq_end;
list_add_tail(&entry->list, &conn->entry_list);
pr_dbg("new_data:seq_valid %u, seq_valid_end %u, new rcv_nxt %u\n", cb->seq,
cb->seq_end, proto->p.mrcv_nxt);
} else {
// out-of-order packet, drop it now
return -1;
}
return 0;
}
static int colo_enqueue_udp_packet(struct nf_conn_colo *conn,
struct nf_queue_entry *entry)
{
struct sk_buff *skb = entry->skb;
if (colo_get_udphdr(nf_ct_l3num((struct nf_conn *)conn->nfct),
skb, COLO_SKB_CB(skb)) == NULL) {
return -1;
}
list_add_tail(&entry->list, &conn->entry_list);
return 0;
}
static int colo_enqueue_packet(struct nf_queue_entry *entry, unsigned int ptr)
{
enum ip_conntrack_info ctinfo;
struct nf_conn *ct;
struct nf_conn_colo *conn;
struct sk_buff *skb = entry->skb;
struct colo_node *node;
struct colo_tcp_cb *cb;
int ret = 0;
ct = nf_ct_get(skb, &ctinfo);
conn = nfct_colo(ct);
if (conn == NULL) {
pr_dbg("fuck colo_enqueue_packet colo isn't exist\n");
return -1;
}
if (skb_is_gso(skb)) {
pr_dbg("master: gso again???!!!\n");
}
if (entry->hook != NF_INET_PRE_ROUTING) {
pr_dbg("fuck packet is not on pre routing chain\n");
return -1;
}
rcu_read_lock();
node = colo_node_find(conn->index);
BUG_ON(node == NULL);
switch (entry->pf) {
case NFPROTO_IPV4:
skb->protocol = htons(ETH_P_IP);
break;
case NFPROTO_IPV6:
skb->protocol = htons(ETH_P_IPV6);
break;
}
spin_lock_bh(&conn->lock);
cb = COLO_SKB_CB(skb);
BUILD_BUG_ON(sizeof(*cb) + sizeof(struct inet_skb_parm) > 48);
memset(cb, 0, sizeof(*cb));
switch (nf_ct_protonum(ct)) {
case IPPROTO_TCP:
ret = colo_enqueue_tcp_packet(conn, entry);
break;
case IPPROTO_UDP:
ret = colo_enqueue_udp_packet(conn, entry);
break;
case IPPROTO_ICMP:
ret = colo_enqueue_icmp_packet(conn, entry);
break;
default:
list_add_tail(&entry->list, &conn->entry_list);
pr_dbg("colo_enqueue_packet add skb into ct %p colo %p queue %p\n",
ct, conn, &conn->entry_list);
}
spin_unlock_bh(&conn->lock);
if (ret < 0) {
rcu_read_unlock();
return ret;
}
spin_lock_bh(&node->lock);
list_move_tail(&conn->conn_list, &node->list);
wake_up_interruptible(&node->u.p.wait);
spin_unlock_bh(&node->lock);
rcu_read_unlock();
return 0;
}
static inline struct nf_conn *nf_ct_slaver_get(void)
{
return &__raw_get_cpu_var(slaver_conntrack);
}
static inline int nf_ct_is_colo_template(const struct nf_conn *ct)
{
return test_bit(IPS_COLO_TEMPLATE_BIT, &ct->status);
}
static unsigned int
colo_slaver_enqueue_icmp_packet(struct nf_conn_colo *conn,
struct sk_buff *skb)
{
struct icmphdr *icmph, _ih;
struct colo_icmp_cb *cb = COLO_SKB_CB(skb);
icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
if (icmph == NULL) {
return NF_DROP;
}
cb->type = icmph->type;
cb->code = icmph->code;
if ((cb->type == ICMP_ECHO) || (cb->type == ICMP_ECHOREPLY)) {
cb->seq = ntohs(icmph->un.echo.sequence);
cb->id = ntohs(icmph->un.echo.id);
pr_dbg("[slaver]conn %p, type %u, seq %u, id %d\n", conn->nfct, cb->type, cb->seq, cb->id);
} else if (cb->type == ICMP_REDIRECT) {
cb->gw = icmph->un.gateway;
} else if ((cb->type == ICMP_DEST_UNREACH) && (cb->code == ICMP_FRAG_NEEDED)) {
cb->mtu = icmph->un.frag.mtu;
}
__skb_queue_tail(&conn->slaver_pkt_queue, skb);
return NF_STOLEN;
}
static unsigned int
colo_slaver_enqueue_tcp_packet(struct nf_conn_colo *conn,
struct sk_buff *skb)
{
struct tcphdr *th;
union nf_conn_colo_tcp *proto = (union nf_conn_colo_tcp *) conn->proto;
struct colo_tcp_cb *cb;
bool stolen = false;
u32 win;
cb = COLO_SKB_CB(skb);
th = colo_get_tcphdr(nf_ct_l3num((struct nf_conn *) conn->nfct),
skb, cb, &proto->p.sscale);
if (th == NULL) {
pr_dbg("fuck! get tcphdr of slaver packet failed\n");
return NF_DROP;
}
pr_dbg("DEBUG: slaver: enqueue skb seq %u, seq_end %u, ack %u, sack %u, LEN: %u\n",
cb->seq, cb->seq_end, cb->ack, proto->p.sack, cb->seq_end - cb->seq);
if (before(proto->p.sack, cb->ack)) {
proto->p.sack = cb->ack;
stolen = true;
}
pr_dbg("slaver max ack %u, rcv_nxt is %u\n", proto->p.sack, proto->p.srcv_nxt);
if (th->syn) {
conn->flags |= COLO_CONN_SYN_RECVD;
proto->p.swin = cb->win;
pr_dbg("slaver received syn, window scale %u\n", proto->p.sscale);
/* need to return NF_STOLEN to start compare thread */
kfree_skb(skb);
goto out;
}
win = cb->win << proto->p.sscale;
if (proto->p.swin < win)
stolen = true;
proto->p.swin = win;
pr_dbg("window size %u\n", proto->p.swin);
if (unlikely(th->rst)) {
pr_dbg("slaver received rst\n");
__skb_queue_tail(&conn->slaver_pkt_queue, skb);
goto out;
}
if (!after(cb->seq_end, proto->p.srcv_nxt)) {
// retrans, don't compare
if (stolen) {
/* ack updated, should start compare thread */
pr_dbg("slaver ack updated or window size updated\n");
kfree_skb(skb);
goto out;
}
pr_dbg("slaver retrans_data or pure ack, DROP\n");
return NF_DROP;
}
if (!after(cb->seq, proto->p.srcv_nxt)) {
// at least part of data is new.
u32 nrcv_nxt;
cb->dataoff += proto->p.srcv_nxt - cb->seq;
cb->seq = proto->p.srcv_nxt;
nrcv_nxt = proto->p.srcv_nxt = cb->seq_end;
__skb_queue_tail(&conn->slaver_pkt_queue, skb);
pr_dbg("new_data:seq_valid %u, seq_valid_end %u, new rcv_nxt %u\n", cb->seq,
cb->seq_end, proto->p.srcv_nxt);
if (th->fin)
goto out;
} else {
// out-of-order packet, drop it now
return NF_DROP;
}
out: