-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathllb_kern_ct.c
1848 lines (1600 loc) · 44.9 KB
/
llb_kern_ct.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
/*
* llb_kern_ct.c: Loxilb kernel eBPF ConnTracking Implementation
* Copyright (C) 2022, NetLOX <www.netlox.io>
*
* SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
*/
#ifdef HAVE_LEGACY_BPF_MAPS
struct bpf_map_def SEC("maps") ct_ctr = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(__u32),
.value_size = sizeof(struct dp_ct_ctrtact),
.max_entries = 1
};
#else
struct ct_ctr_d {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32);
__type(value, struct dp_ct_ctrtact);
__uint(max_entries, 1);
} ct_ctr SEC(".maps");
#endif
#define CT_KEY_GEN(k, xf) \
do { \
(k)->daddr[0] = xf->l34m.daddr[0]; \
(k)->daddr[1] = xf->l34m.daddr[1]; \
(k)->daddr[2] = xf->l34m.daddr[2]; \
(k)->daddr[3] = xf->l34m.daddr[3]; \
(k)->saddr[0] = xf->l34m.saddr[0]; \
(k)->saddr[1] = xf->l34m.saddr[1]; \
(k)->saddr[2] = xf->l34m.saddr[2]; \
(k)->saddr[3] = xf->l34m.saddr[3]; \
(k)->sport = xf->l34m.source; \
(k)->dport = xf->l34m.dest; \
(k)->l4proto = xf->l34m.nw_proto; \
(k)->zone = xf->pm.zone; \
(k)->v6 = xf->l2m.dl_type == bpf_ntohs(ETH_P_IPV6) ? 1: 0; \
(k)->ident = xf->tm.tun_decap ? 0 : xf->tm.tunnel_id; \
(k)->type = xf->tm.tun_decap ? 0 : xf->tm.tun_type; \
}while(0)
#define dp_run_ctact_helper(x, a) \
do { \
switch ((a)->ca.act_type) { \
case DP_SET_NOP: \
case DP_SET_SNAT: \
case DP_SET_DNAT: \
(a)->ctd.pi.t.tcp_cts[CT_DIR_IN].pseq = (x)->l34m.seq; \
(a)->ctd.pi.t.tcp_cts[CT_DIR_IN].pack = (x)->l34m.ack; \
break; \
default: \
break; \
} \
} while(0)
static int __always_inline
dp_run_ct_helper(struct xfi *xf)
{
struct dp_ct_key key;
struct dp_ct_tact *act;
CT_KEY_GEN(&key, xf);
act = bpf_map_lookup_elem(&ct_map, &key);
if (!act) {
LL_DBG_PRINTK("[FCH4] miss");
return -1;
}
/* We dont do much strict tracking after EST state.
* But need to maintain minimal ctinfo
*/
dp_run_ctact_helper(xf, act);
return 0;
}
static void __always_inline
dp_ct_related_fc_rm(struct dp_ct_key *ctk)
{
struct dp_fcv4_key key;
if (ctk->v6 || ctk->ident || ctk->type) {
return;
}
key.daddr = ctk->daddr4;
key.saddr = ctk->saddr4;
key.sport = ctk->sport;
key.dport = ctk->dport;
key.l4proto = ctk->l4proto;
key.pad = 0;
key.in_port = 0;
bpf_map_delete_elem(&fc_v4_map, &key);
return;
}
#ifdef HAVE_DP_EXTCT
#define DP_RUN_CT_HELPER(x) \
do { \
if ((x)->l34m.nw_proto == IPPROTO_TCP) { \
dp_run_ct_helper(x); \
} \
} while(0)
#else
#define DP_RUN_CT_HELPER(x)
#endif
static __u32 __always_inline
dp_ct_get_newctr(__u32 *nid)
{
__u32 k = 0;
__u32 v = 0;
struct dp_ct_ctrtact *ctr;
ctr = bpf_map_lookup_elem(&ct_ctr, &k);
if (ctr == NULL) {
return 0;
}
*nid = ctr->start;
/* FIXME - We can potentially do a percpu array and do away
* with the locking here
*/
bpf_spin_lock(&ctr->lock);
v = ctr->counter;
ctr->counter += 2;
if (ctr->counter >= ctr->entries) {
ctr->counter = ctr->start;
}
bpf_spin_unlock(&ctr->lock);
return v;
}
static int __always_inline
dp_ct_proto_xfk_init(struct dp_ct_key *key,
nxfrm_inf_t *xi,
struct dp_ct_key *xkey,
nxfrm_inf_t *xxi)
{
DP_XADDR_CP(xkey->daddr, key->saddr);
DP_XADDR_CP(xkey->saddr, key->daddr);
xkey->sport = key->dport;
xkey->dport = key->sport;
xkey->l4proto = key->l4proto;
xkey->zone = key->zone;
xkey->v6 = key->v6;
xkey->ident = key->ident;
xkey->type = key->type;
if (xi->dsr) {
if (xi->nat_flags & LLB_NAT_DST) {
xxi->nat_flags = LLB_NAT_SRC;
DP_XADDR_CP(xxi->nat_xip, key->daddr);
xxi->nat_xport = key->dport;
xxi->nv6 = xi->nv6;
}
xxi->dsr = xi->dsr;
return 0;
}
/* Apply NAT xfrm if needed */
if (xi->nat_flags & LLB_NAT_DST) {
xkey->v6 = (__u8)(xi->nv6);
DP_XADDR_CP(xkey->saddr, xi->nat_xip);
if (!DP_XADDR_ISZR(xi->nat_rip)) {
DP_XADDR_CP(xkey->daddr, xi->nat_rip);
DP_XADDR_CP(xxi->nat_rip, key->saddr);
}
if (key->l4proto != IPPROTO_ICMP) {
if (xi->nat_xport)
xkey->sport = xi->nat_xport;
else
xi->nat_xport = key->dport;
}
xxi->nat_flags = LLB_NAT_SRC;
xxi->nv6 = key->v6;
DP_XADDR_CP(xxi->nat_xip, key->daddr);
if (key->l4proto != IPPROTO_ICMP)
xxi->nat_xport = key->dport;
}
if (xi->nat_flags & LLB_NAT_SRC) {
xkey->v6 = xi->nv6;
DP_XADDR_CP(xkey->daddr, xi->nat_xip);
if (!DP_XADDR_ISZR(xi->nat_rip)) {
DP_XADDR_CP(xkey->saddr, xi->nat_rip);
DP_XADDR_CP(xxi->nat_rip, key->daddr);
}
if (key->l4proto != IPPROTO_ICMP) {
if (xi->nat_xport)
xkey->dport = xi->nat_xport;
else
xi->nat_xport = key->sport;
}
xxi->nat_flags = LLB_NAT_DST;
xxi->nv6 = key->v6;
DP_XADDR_CP(xxi->nat_xip, key->saddr);
if (key->l4proto != IPPROTO_ICMP)
xxi->nat_xport = key->sport;
}
if (xi->nat_flags & LLB_NAT_HDST) {
DP_XADDR_CP(xkey->saddr, key->saddr);
DP_XADDR_CP(xkey->daddr, key->daddr);
if (key->l4proto != IPPROTO_ICMP) {
if (xi->nat_xport)
xkey->sport = xi->nat_xport;
else
xi->nat_xport = key->dport;
}
xxi->nat_flags = LLB_NAT_HSRC;
xxi->nv6 = key->v6;
DP_XADDR_SETZR(xxi->nat_xip);
DP_XADDR_SETZR(xi->nat_xip);
if (key->l4proto != IPPROTO_ICMP)
xxi->nat_xport = key->dport;
}
if (xi->nat_flags & LLB_NAT_HSRC) {
DP_XADDR_CP(xkey->saddr, key->saddr);
DP_XADDR_CP(xkey->daddr, key->daddr);
if (key->l4proto != IPPROTO_ICMP) {
if (xi->nat_xport)
xkey->dport = xi->nat_xport;
else
xi->nat_xport = key->sport;
}
xxi->nat_flags = LLB_NAT_HDST;
xxi->nv6 = key->v6;
DP_XADDR_SETZR(xxi->nat_xip);
DP_XADDR_SETZR(xi->nat_xip);
if (key->l4proto != IPPROTO_ICMP)
xxi->nat_xport = key->sport;
}
return 0;
}
static int __always_inline
dp_ct3_sm(struct dp_ct_dat *tdat,
struct dp_ct_dat *xtdat,
ct_dir_t dir)
{
ct_state_t new_state = tdat->pi.l3i.state;
switch (tdat->pi.l3i.state) {
case CT_STATE_NONE:
if (dir == CT_DIR_IN) {
new_state = CT_STATE_REQ;
} else {
return -1;
}
break;
case CT_STATE_REQ:
if (dir == CT_DIR_OUT) {
new_state = CT_STATE_REP;
}
break;
case CT_STATE_REP:
if (dir == CT_DIR_IN) {
new_state = CT_STATE_EST;
}
break;
default:
break;
}
tdat->pi.l3i.state = new_state;
if (new_state == CT_STATE_EST) {
return 1;
}
return 0;
}
static int __always_inline
dp_ct_tcp_sm(void *ctx, struct xfi *xf,
struct dp_ct_tact *atdat,
struct dp_ct_tact *axtdat,
ct_dir_t dir)
{
struct dp_ct_dat *tdat = &atdat->ctd;
struct dp_ct_dat *xtdat = &axtdat->ctd;
ct_tcp_pinf_t *ts = &tdat->pi.t;
ct_tcp_pinf_t *rts = &xtdat->pi.t;
void *dend = DP_TC_PTR(DP_PDATA_END(ctx));
struct tcphdr *t = DP_ADD_PTR(DP_PDATA(ctx), xf->pm.l4_off);
uint8_t tcp_flags = xf->pm.tcp_flags;
ct_tcp_pinfd_t *td = &ts->tcp_cts[dir];
ct_tcp_pinfd_t *rtd;
uint32_t seq;
uint32_t ack;
uint32_t nstate = 0;
if (t + 1 > dend) {
LLBS_PPLN_DROPC(xf, LLB_PIPE_RC_PLCT_ERR);
return -1;
}
seq = bpf_ntohl(t->seq);
ack = bpf_ntohl(t->ack_seq);
bpf_spin_lock(&atdat->lock);
if (dir == CT_DIR_IN) {
tdat->pi.t.tcp_cts[0].pseq = t->seq;
tdat->pi.t.tcp_cts[0].pack = t->ack_seq;
tdat->pb.bytes += xf->pm.l3_len;
tdat->pb.packets += 1;
} else {
xtdat->pi.t.tcp_cts[0].pseq = t->seq;
xtdat->pi.t.tcp_cts[0].pack = t->ack_seq;
xtdat->pb.bytes += xf->pm.l3_len;
xtdat->pb.packets += 1;
}
rtd = &ts->tcp_cts[dir == CT_DIR_IN ? CT_DIR_OUT:CT_DIR_IN];
if (dir == CT_DIR_IN) {
if (td->ppv2) {
xf->pm.oppv2 = 1;
}
} else {
if (td->ppv2) {
xf->pm.ippv2 = 1;
}
}
if (tcp_flags & LLB_TCP_RST) {
nstate = CT_TCP_CW;
goto end;
}
switch (ts->state) {
case CT_TCP_CLOSED:
if (xf->nm.dsr) {
nstate = CT_TCP_EST;
goto end;
}
/* If DP starts after TCP was established
* we need to somehow handle this particular case
*/
if (tcp_flags & LLB_TCP_ACK) {
td->seq = seq;
if (td->init_acks) {
if (ack > rtd->seq + 2) {
nstate = CT_TCP_ERR;
goto end;
}
}
td->init_acks++;
if (td->init_acks >= CT_TCP_INIT_ACK_THRESHOLD &&
rtd->init_acks >= CT_TCP_INIT_ACK_THRESHOLD) {
nstate = CT_TCP_EST;
break;
}
nstate = CT_TCP_ERR;
goto end;
}
if ((tcp_flags & LLB_TCP_SYN) != LLB_TCP_SYN) {
nstate = CT_TCP_ERR;
goto end;
}
/* SYN sent with ack 0 */
if (ack != 0 && dir != CT_DIR_IN) {
nstate = CT_TCP_ERR;
goto end;
}
td->seq = seq;
nstate = CT_TCP_SS;
break;
case CT_TCP_SS:
if (dir != CT_DIR_OUT) {
if ((tcp_flags & LLB_TCP_SYN) == LLB_TCP_SYN) {
td->seq = seq;
nstate = CT_TCP_SS;
} else {
nstate = CT_TCP_ERR;
}
goto end;
}
if ((tcp_flags & (LLB_TCP_SYN|LLB_TCP_ACK)) !=
(LLB_TCP_SYN|LLB_TCP_ACK)) {
nstate = CT_TCP_ERR;
goto end;
}
if (ack != rtd->seq + 1) {
nstate = CT_TCP_ERR;
goto end;
}
td->seq = seq;
nstate = CT_TCP_SA;
break;
case CT_TCP_SA:
if (dir != CT_DIR_IN) {
if ((tcp_flags & (LLB_TCP_SYN|LLB_TCP_ACK)) !=
(LLB_TCP_SYN|LLB_TCP_ACK)) {
nstate = CT_TCP_ERR;
goto end;
}
if (ack != rtd->seq + 1) {
nstate = CT_TCP_ERR;
goto end;
}
nstate = CT_TCP_SA;
goto end;
}
if ((tcp_flags & LLB_TCP_SYN) == LLB_TCP_SYN) {
td->seq = seq;
nstate = CT_TCP_SS;
goto end;
}
if ((tcp_flags & LLB_TCP_ACK) != LLB_TCP_ACK) {
nstate = CT_TCP_ERR;
goto end;
}
if (ack != rtd->seq + 1) {
nstate = CT_TCP_ERR;
goto end;
}
td->seq = seq;
if (xf->nm.ppv2)
nstate = CT_TCP_PEST;
else
nstate = CT_TCP_EST;
break;
case CT_TCP_PEST:
if (tcp_flags & LLB_TCP_FIN) {
ts->fndir = dir;
nstate = CT_TCP_FINI;
td->seq = seq;
} else {
nstate = CT_TCP_PEST;
if (dir == CT_DIR_IN) {
if (td->ppv2 == 0) {
xf->pm.ppv2 = 1;
td->ppv2 = 1;
rtd->ppv2 = 1;
}
}
}
break;
case CT_TCP_EST:
if (tcp_flags & LLB_TCP_FIN) {
ts->fndir = dir;
nstate = CT_TCP_FINI;
td->seq = seq;
} else {
nstate = CT_TCP_EST;
}
break;
case CT_TCP_FINI:
if (ts->fndir != dir) {
if ((tcp_flags & (LLB_TCP_FIN|LLB_TCP_ACK)) ==
(LLB_TCP_FIN|LLB_TCP_ACK)) {
nstate = CT_TCP_FINI3;
td->seq = seq;
} else if (tcp_flags & LLB_TCP_ACK) {
nstate = CT_TCP_FINI2;
td->seq = seq;
}
}
break;
case CT_TCP_FINI2:
if (ts->fndir != dir) {
if (tcp_flags & LLB_TCP_FIN) {
nstate = CT_TCP_FINI3;
td->seq = seq;
}
}
break;
case CT_TCP_FINI3:
if (ts->fndir == dir) {
if (tcp_flags & LLB_TCP_ACK) {
nstate = CT_TCP_CW;
}
}
break;
default:
break;
}
end:
ts->state = nstate;
rts->state = nstate;
if (nstate != CT_TCP_ERR && dir == CT_DIR_OUT) {
xtdat->pi.t.tcp_cts[0].seq = seq;
}
bpf_spin_unlock(&atdat->lock);
if (nstate == CT_TCP_EST) {
return CT_SMR_EST;
} else if (nstate & CT_TCP_CW) {
return CT_SMR_CTD;
} else if (nstate & CT_TCP_ERR) {
return CT_SMR_ERR;
} else if (nstate & CT_TCP_FIN_MASK) {
return CT_SMR_FIN;
}
return CT_SMR_INPROG;
}
static int __always_inline
dp_ct_udp_sm(void *ctx, struct xfi *xf,
struct dp_ct_tact *atdat,
struct dp_ct_tact *axtdat,
ct_dir_t dir)
{
struct dp_ct_dat *tdat = &atdat->ctd;
struct dp_ct_dat *xtdat = &axtdat->ctd;
ct_udp_pinf_t *us = &tdat->pi.u;
ct_udp_pinf_t *xus = &xtdat->pi.u;
uint32_t nstate = us->state;
bpf_spin_lock(&atdat->lock);
if (dir == CT_DIR_IN) {
tdat->pb.bytes += xf->pm.l3_len;
tdat->pb.packets += 1;
us->pkts_seen++;
} else {
xtdat->pb.bytes += xf->pm.l3_len;
xtdat->pb.packets += 1;
us->rpkts_seen++;
}
switch (us->state) {
case CT_UDP_CNI:
if (xf->nm.dsr || xf->l2m.ssnid) {
nstate = CT_UDP_EST;
break;
}
if (us->pkts_seen && us->rpkts_seen) {
nstate = CT_UDP_EST;
} else if (us->pkts_seen > CT_UDP_CONN_THRESHOLD) {
nstate = CT_UDP_UEST;
}
break;
case CT_UDP_UEST:
if (us->rpkts_seen || us->pkts_seen > 2*CT_UDP_CONN_THRESHOLD)
nstate = CT_UDP_EST;
break;
case CT_UDP_EST:
if (xf->pm.l4fin) {
nstate = CT_UDP_FINI;
us->fndir = dir;
}
break;
case CT_UDP_FINI:
if (xf->pm.l4fin && us->fndir != dir) {
nstate = CT_UDP_CW;
}
break;
default:
break;
}
us->state = nstate;
xus->state = nstate;
bpf_spin_unlock(&atdat->lock);
if (nstate == CT_UDP_UEST)
return CT_SMR_UEST;
else if (nstate == CT_UDP_EST)
return CT_SMR_EST;
else if (nstate & CT_UDP_CW)
return CT_SMR_CTD;
else if (nstate & CT_UDP_FIN_MASK)
return CT_SMR_FIN;
return CT_SMR_INPROG;
}
static int __always_inline
dp_ct_icmp6_sm(void *ctx, struct xfi *xf,
struct dp_ct_tact *atdat,
struct dp_ct_tact *axtdat,
ct_dir_t dir)
{
struct dp_ct_dat *tdat = &atdat->ctd;
struct dp_ct_dat *xtdat = &axtdat->ctd;
ct_icmp_pinf_t *is = &tdat->pi.i;
ct_icmp_pinf_t *xis = &xtdat->pi.i;
void *dend = DP_TC_PTR(DP_PDATA_END(ctx));
struct icmp6hdr *i = DP_ADD_PTR(DP_PDATA(ctx), xf->pm.l4_off);
uint32_t nstate;
uint16_t seq;
if (i + 1 > dend) {
LLBS_PPLN_DROPC(xf, LLB_PIPE_RC_PLCT_ERR);
return -1;
}
/* We fetch the sequence number even if icmp may not be
* echo type because we can't call another fn holding
* spinlock
*/
seq = bpf_ntohs(i->icmp6_dataun.u_echo.sequence);
bpf_spin_lock(&atdat->lock);
if (dir == CT_DIR_IN) {
tdat->pb.bytes += xf->pm.l3_len;
tdat->pb.packets += 1;
} else {
xtdat->pb.bytes += xf->pm.l3_len;
xtdat->pb.packets += 1;
}
nstate = is->state;
switch (i->icmp6_type) {
case ICMPV6_DEST_UNREACH:
is->state |= CT_ICMP_DUNR;
goto end;
case ICMPV6_TIME_EXCEED:
is->state |= CT_ICMP_TTL;
goto end;
case ICMPV6_ECHO_REPLY:
case ICMPV6_ECHO_REQUEST:
/* Further state-machine processing */
break;
default:
is->state |= CT_ICMP_UNK;
goto end;
}
switch (is->state) {
case CT_ICMP_CLOSED:
if (xf->nm.dsr) {
nstate = CT_ICMP_REPS;
goto end;
}
if (i->icmp6_type != ICMPV6_ECHO_REQUEST) {
is->errs = 1;
goto end;
}
nstate = CT_ICMP_REQS;
is->lseq = seq;
break;
case CT_ICMP_REQS:
if (i->icmp6_type == ICMPV6_ECHO_REQUEST) {
is->lseq = seq;
} else if (i->icmp6_type == ICMPV6_ECHO_REPLY) {
if (is->lseq != seq) {
is->errs = 1;
goto end;
}
nstate = CT_ICMP_REPS;
is->lseq = seq;
}
break;
case CT_ICMP_REPS:
/* Connection is tracked now */
default:
break;
}
end:
is->state = nstate;
xis->state = nstate;
bpf_spin_unlock(&atdat->lock);
if (nstate == CT_ICMP_REPS)
return CT_SMR_EST;
return CT_SMR_INPROG;
}
static int __always_inline
dp_ct_icmp_sm(void *ctx, struct xfi *xf,
struct dp_ct_tact *atdat,
struct dp_ct_tact *axtdat,
ct_dir_t dir)
{
struct dp_ct_dat *tdat = &atdat->ctd;
struct dp_ct_dat *xtdat = &axtdat->ctd;
ct_icmp_pinf_t *is = &tdat->pi.i;
ct_icmp_pinf_t *xis = &xtdat->pi.i;
void *dend = DP_TC_PTR(DP_PDATA_END(ctx));
struct icmphdr *i = DP_ADD_PTR(DP_PDATA(ctx), xf->pm.l4_off);
uint32_t nstate;
uint16_t seq;
if (i + 1 > dend) {
LLBS_PPLN_DROPC(xf, LLB_PIPE_RC_PLCT_ERR);
return -1;
}
/* We fetch the sequence number even if icmp may not be
* echo type because we can't call another fn holding
* spinlock
*/
seq = bpf_ntohs(i->un.echo.sequence);
bpf_spin_lock(&atdat->lock);
if (dir == CT_DIR_IN) {
tdat->pb.bytes += xf->pm.l3_len;
tdat->pb.packets += 1;
} else {
xtdat->pb.bytes += xf->pm.l3_len;
xtdat->pb.packets += 1;
}
nstate = is->state;
switch (i->type) {
case ICMP_DEST_UNREACH:
is->state |= CT_ICMP_DUNR;
goto end;
case ICMP_TIME_EXCEEDED:
is->state |= CT_ICMP_TTL;
goto end;
case ICMP_REDIRECT:
is->state |= CT_ICMP_RDR;
goto end;
case ICMP_ECHOREPLY:
case ICMP_ECHO:
/* Further state-machine processing */
break;
default:
is->state |= CT_ICMP_UNK;
goto end;
}
switch (is->state) {
case CT_ICMP_CLOSED:
if (xf->nm.dsr) {
nstate = CT_ICMP_REPS;
goto end;
}
if (i->type != ICMP_ECHO) {
is->errs = 1;
goto end;
}
nstate = CT_ICMP_REQS;
is->lseq = seq;
break;
case CT_ICMP_REQS:
if (i->type == ICMP_ECHO) {
is->lseq = seq;
} else if (i->type == ICMP_ECHOREPLY) {
if (is->lseq != seq) {
is->errs = 1;
goto end;
}
nstate = CT_ICMP_REPS;
is->lseq = seq;
}
break;
case CT_ICMP_REPS:
/* Connection is tracked now */
default:
break;
}
end:
is->state = nstate;
xis->state = nstate;
bpf_spin_unlock(&atdat->lock);
if (nstate == CT_ICMP_REPS)
return CT_SMR_EST;
return CT_SMR_INPROG;
}
static int __always_inline
dp_ct_sctp_sm(void *ctx, struct xfi *xf,
struct dp_ct_tact *atdat,
struct dp_ct_tact *axtdat,
ct_dir_t dir)
{
struct dp_ct_dat *tdat = &atdat->ctd;
struct dp_ct_dat *xtdat = &axtdat->ctd;
ct_sctp_pinf_t *ss = &tdat->pi.s;
ct_sctp_pinf_t *xss = &xtdat->pi.s;
ct_sctp_pinfd_t *pss = &ss->sctp_cts[CT_DIR_IN];
ct_sctp_pinfd_t *pxss = &ss->sctp_cts[CT_DIR_OUT];
uint32_t nstate = 0;
uint32_t npmhh = tdat->pi.npmhh;
void *dend = DP_TC_PTR(DP_PDATA_END(ctx));
struct sctphdr *s = DP_ADD_PTR(DP_PDATA(ctx), xf->pm.l4_off);
struct sctp_dch *c;
struct sctp_init_ch *ic;
struct sctp_cookie *ck;
struct sctp_param *pm;
uint16_t poff = 0;
uint32_t nh = 0;
int i = 0;
if (s + 1 > dend) {
LLBS_PPLN_DROPC(xf, LLB_PIPE_RC_PLCT_ERR);
return -1;
}
c = DP_TC_PTR(DP_ADD_PTR(s, sizeof(*s)));
if (c + 1 > dend) {
LLBS_PPLN_DROPC(xf, LLB_PIPE_RC_PLCT_ERR);
return -1;
}
poff = xf->pm.l4_off + sizeof(*s);
nstate = ss->state;
bpf_spin_lock(&atdat->lock);
if (dir == CT_DIR_IN) {
atdat->ctd.pb.bytes += xf->pm.l3_len;
atdat->ctd.pb.packets += 1;
} else {
axtdat->ctd.pb.bytes += xf->pm.l3_len;
axtdat->ctd.pb.packets += 1;
}
switch (c->type) {
case SCTP_ERROR:
nstate = CT_SCTP_ERR;
goto end;
case SCTP_SHUT:
nstate = CT_SCTP_SHUT;
goto end;
case SCTP_ABORT:
nstate = CT_SCTP_ABRT;
goto end;
}
switch (ss->state) {
case CT_SCTP_CLOSED:
if (xf->nm.dsr) {
nstate = CT_SCTP_EST;
goto end;
}
if (c->type != SCTP_INIT_CHUNK || dir != CT_DIR_IN) {
nstate = CT_SCTP_ERR;
goto end;
}
ic = DP_TC_PTR(DP_ADD_PTR(c, sizeof(*c)));
if (ic + 1 > dend) {
goto end;
}
poff += sizeof(*c);
ss->itag = ic->tag;
nstate = CT_SCTP_INIT;
pm = DP_TC_PTR(DP_ADD_PTR(ic, sizeof(*ic)));
if (pm + 1 > dend) {
goto add_nph0;
}
poff += sizeof(*ic);
if (xf->l2m.dl_type != bpf_ntohs(ETH_P_IP) || !tdat->xi.nat_flags) {
break;
}
pss->mh_host[0] = xf->l34m.saddr[0];
pss->nh = 1;
pss->odst = xf->l34m.daddr[0];
pss->osrc = xf->l34m.saddr[0];
nh = 1;
for (i = 0; i < LLB_MAX_SCTP_CHUNKS_INIT; i++) {
uint16_t csz = 0;
if (poff >= 4096) {
bpf_spin_unlock(&atdat->lock);
LLBS_PPLN_DROPC(xf, LLB_PIPE_RC_PLCT_ERR);
return -1;
}
pm = DP_TC_PTR(DP_ADD_PTR(DP_PDATA(ctx), poff));
dend = DP_TC_PTR(DP_PDATA_END(ctx));
if (pm + 1 > dend) {
goto add_nph0;
}
if (pm->type == bpf_htons(SCTP_IPV4_ADDR_PARAM)) {
__be32 *ip = DP_TC_PTR(DP_ADD_PTR(pm, sizeof(*pm)));
if (ip + 1 > dend) {
break;
}
if (nh <= LLB_MAX_MHOSTS && *ip != pss->osrc) {
pss->mh_host[nh] = *ip;
pss->nh++;
nh++;
}
if (!atdat->nat_act.nv6) {
/* Checksum to be taken care of at a later stage */
if (nh-1 < LLB_MAX_MHOSTS && atdat->ctd.pi.pmhh[nh-1] != 0) {
*ip = atdat->ctd.pi.pmhh[nh-1];
} else if (atdat->ctd.pi.pmhh[0] != 0) {
*ip = atdat->ctd.pi.pmhh[0];
} else if (atdat->nat_act.rip[0] != 0) {
*ip = atdat->nat_act.rip[0];
}
}
}
csz = bpf_ntohs(pm->len);
poff += (csz + 3) & ~0x3;
}
add_nph0:
if ((pss->nh - 1) < npmhh) {
int grow;
int diff = npmhh - pss->nh + 1;
grow = ((diff)*(sizeof(*pm)+sizeof(__u32)));
poff = (((struct __sk_buff *)ctx)->len);
bpf_spin_unlock(&atdat->lock);
if (dp_pktbuf_expand_tail(ctx, grow + poff) < 0) {
LLBS_PPLN_DROPC(xf, LLB_PIPE_RC_PLCT_ERR);
bpf_spin_lock(&atdat->lock);
break;
}
bpf_spin_lock(&atdat->lock);
pm = DP_TC_PTR(DP_PDATA(ctx));
dend = DP_TC_PTR(DP_PDATA_END(ctx));
if (pm + 1 > dend) {
break;
}
for (i = 0; i < diff; i++) {
if (i >= LLB_MAX_MHOSTS) break;
/* Keep the verifier happy */
if (poff > SCTP_MAX_INIT_ACK_SZ) {
break;
}
pm = DP_ADD_PTR(pm, poff);
if (pm + 1 > dend) {
break;
}
pm->type = bpf_htons(SCTP_IPV4_ADDR_PARAM);
pm->len = bpf_htons(sizeof(*pm) + sizeof(__u32));
__be32 *ip = DP_TC_PTR(DP_ADD_PTR(pm, sizeof(*pm)));
if (ip + 1 > dend) {
break;
}
if (!atdat->nat_act.nv6) {
/* Checksum to be taken care of at a later stage */
if (i < LLB_MAX_MHOSTS && atdat->ctd.pi.pmhh[i] != 0) {
*ip = atdat->ctd.pi.pmhh[i];
} else if (atdat->ctd.pi.pmhh[0] != 0) {
*ip = atdat->ctd.pi.pmhh[0];
} else if (atdat->nat_act.rip[0] != 0) {
*ip = atdat->nat_act.rip[0];