/
Conntrack.c
2351 lines (2127 loc) · 81.4 KB
/
Conntrack.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
/*
* Copyright (c) 2015, 2016 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Conntrack.h"
#include "IpFragment.h"
#include "Ip6Fragment.h"
#include "Jhash.h"
#include "PacketParser.h"
#include "Event.h"
#include "Conntrack-nat.h"
#include "IpHelper.h"
#pragma warning(push)
#pragma warning(disable:4311)
#define WINDOWS_TICK 10000000
#define SEC_TO_UNIX_EPOCH 11644473600LL
#define SEC_TO_NANOSEC 1000000000LL
#define CT_MAX_ZONE (UINT16_MAX + 1)
KSTART_ROUTINE OvsConntrackEntryCleaner;
static PLIST_ENTRY ovsConntrackTable;
static OVS_CT_THREAD_CTX ctThreadCtx;
static PNDIS_RW_LOCK_EX *ovsCtBucketLock = NULL;
static NDIS_SPIN_LOCK ovsCtZoneLock;
static POVS_CT_ZONE_INFO zoneInfo = NULL;
extern POVS_SWITCH_CONTEXT gOvsSwitchContext;
static ULONG ctTotalEntries;
static ULONG defaultCtLimit;
static __inline OvsCtFlush(UINT16 zone, struct ovs_key_ct_tuple_ipv4 *tuple);
static __inline NDIS_STATUS
MapNlToCtTuple(POVS_MESSAGE msgIn, PNL_ATTR attr,
struct ovs_key_ct_tuple_ipv4 *ct_tuple);
/*
*----------------------------------------------------------------------------
* OvsInitConntrack
* Initialize the components used by Connection Tracking
*----------------------------------------------------------------------------
*/
NTSTATUS
OvsInitConntrack(POVS_SWITCH_CONTEXT context)
{
NTSTATUS status = STATUS_SUCCESS;
HANDLE threadHandle = NULL;
ctTotalEntries = 0;
UINT32 numBucketLocks = CT_HASH_TABLE_SIZE;
/* Init the Hash Buffer */
ovsConntrackTable = OvsAllocateMemoryWithTag(sizeof(LIST_ENTRY)
* CT_HASH_TABLE_SIZE,
OVS_CT_POOL_TAG);
if (ovsConntrackTable == NULL) {
return STATUS_INSUFFICIENT_RESOURCES;
}
ovsCtBucketLock = OvsAllocateMemoryWithTag(sizeof(PNDIS_RW_LOCK_EX)
* CT_HASH_TABLE_SIZE,
OVS_CT_POOL_TAG);
if (ovsCtBucketLock == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto freeTable;
}
for (UINT32 i = 0; i < CT_HASH_TABLE_SIZE; i++) {
InitializeListHead(&ovsConntrackTable[i]);
ovsCtBucketLock[i] = NdisAllocateRWLock(context->NdisFilterHandle);
if (ovsCtBucketLock[i] == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
numBucketLocks = i;
goto freeBucketLock;
}
}
/* Init CT Cleaner Thread */
KeInitializeEvent(&ctThreadCtx.event, NotificationEvent, FALSE);
status = PsCreateSystemThread(&threadHandle, SYNCHRONIZE, NULL, NULL,
NULL, OvsConntrackEntryCleaner,
&ctThreadCtx);
if (status != STATUS_SUCCESS) {
goto freeBucketLock;
}
ObReferenceObjectByHandle(threadHandle, SYNCHRONIZE, NULL, KernelMode,
&ctThreadCtx.threadObject, NULL);
ZwClose(threadHandle);
threadHandle = NULL;
zoneInfo = OvsAllocateMemoryWithTag(sizeof(OVS_CT_ZONE_INFO) *
CT_MAX_ZONE, OVS_CT_POOL_TAG);
if (zoneInfo == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto freeBucketLock;
}
NdisAllocateSpinLock(&ovsCtZoneLock);
defaultCtLimit = CT_MAX_ENTRIES;
for (UINT32 i = 0; i < CT_MAX_ZONE; i++) {
zoneInfo[i].entries = 0;
zoneInfo[i].limit = defaultCtLimit;
}
status = OvsNatInit();
if (status != STATUS_SUCCESS) {
OvsCleanupConntrack();
}
return STATUS_SUCCESS;
freeBucketLock:
for (UINT32 i = 0; i < numBucketLocks; i++) {
if (ovsCtBucketLock[i] != NULL) {
NdisFreeRWLock(ovsCtBucketLock[i]);
}
}
OvsFreeMemoryWithTag(ovsCtBucketLock, OVS_CT_POOL_TAG);
ovsCtBucketLock = NULL;
freeTable:
OvsFreeMemoryWithTag(ovsConntrackTable, OVS_CT_POOL_TAG);
ovsConntrackTable = NULL;
return status;
}
/*
*----------------------------------------------------------------------------
* OvsCleanupConntrack
* Cleanup memory and thread that were spawned for Connection tracking
*----------------------------------------------------------------------------
*/
VOID
OvsCleanupConntrack(VOID)
{
ctThreadCtx.exit = 1;
KeSetEvent(&ctThreadCtx.event, 0, FALSE);
KeWaitForSingleObject(ctThreadCtx.threadObject, Executive,
KernelMode, FALSE, NULL);
ObDereferenceObject(ctThreadCtx.threadObject);
/* Force flush all entries before removing */
OvsCtFlush(0, NULL);
if (ovsConntrackTable) {
OvsFreeMemoryWithTag(ovsConntrackTable, OVS_CT_POOL_TAG);
ovsConntrackTable = NULL;
}
for (UINT32 i = 0; i < CT_HASH_TABLE_SIZE; i++) {
/* Disabling the uninitialized memory warning because it should
* always be initialized during OvsInitConntrack */
#pragma warning(suppress: 6001)
if (ovsCtBucketLock[i] != NULL) {
NdisFreeRWLock(ovsCtBucketLock[i]);
}
}
OvsFreeMemoryWithTag(ovsCtBucketLock, OVS_CT_POOL_TAG);
ovsCtBucketLock = NULL;
OvsNatCleanup();
NdisFreeSpinLock(&ovsCtZoneLock);
if (zoneInfo) {
OvsFreeMemoryWithTag(zoneInfo, OVS_CT_POOL_TAG);
}
}
VOID
OvsCtSetZoneLimit(int zone, ULONG value) {
NdisAcquireSpinLock(&ovsCtZoneLock);
if (zone == -1) {
/* Set default limit for all zones. */
defaultCtLimit = value;
for (UINT32 i = 0; i < CT_MAX_ZONE; i++) {
zoneInfo[i].limit = value;
}
} else {
zoneInfo[(UINT16)zone].limit = value;
}
NdisReleaseSpinLock(&ovsCtZoneLock);
}
static uint32_t
OvsCtEndpointHashAdd(uint32_t hash, const struct ct_endpoint *ep)
{
BUILD_ASSERT_DECL(sizeof *ep % 4 == 0);
return OvsJhashBytes((UINT32 *)ep, sizeof *ep, hash);
}
/*
*----------------------------------------------------------------------------
* OvsCtHashKey
* Compute hash using 5-tuple and zone.
*----------------------------------------------------------------------------
*/
UINT32
OvsCtHashKey(const OVS_CT_KEY *key)
{
UINT32 hsrc, hdst, hash;
hsrc = ntohl(key->src.port);
hdst = ntohl(key->dst.port);
hsrc = OvsCtEndpointHashAdd(hsrc, &key->src);
hdst = OvsCtEndpointHashAdd(hdst, &key->dst);
hash = hsrc ^ hdst; /* TO identify reverse traffic */
hash = hash | (key->zone + key->nw_proto);
hash = OvsJhashWords((uint32_t*) &hash, 1, hash);
return hash;
}
static __inline VOID
OvsCtKeyReverse(OVS_CT_KEY *key)
{
struct ct_endpoint tmp;
tmp = key->src;
key->src = key->dst;
key->dst = tmp;
}
static __inline VOID
OvsCtUpdateFlowKey(struct OvsFlowKey *key,
UINT32 state,
UINT16 zone,
UINT32 mark,
struct ovs_key_ct_labels *labels)
{
key->ct.state = state | OVS_CS_F_TRACKED;
key->ct.zone = zone;
key->ct.mark = mark;
if (labels) {
NdisMoveMemory(&key->ct.labels, labels,
sizeof(struct ovs_key_ct_labels));
} else {
memset(&key->ct.labels, 0,
sizeof(struct ovs_key_ct_labels));
}
}
/*
*----------------------------------------------------------------------------
* OvsPostCtEventEntry
* Assumes ct entry lock is acquired
* XXX Refactor OvsPostCtEvent() as it does not require ct entry lock.
*----------------------------------------------------------------------------
*/
static __inline VOID
OvsPostCtEventEntry(POVS_CT_ENTRY entry, UINT8 type)
{
OVS_CT_EVENT_ENTRY ctEventEntry = {0};
NdisMoveMemory(&ctEventEntry.entry, entry, sizeof(OVS_CT_ENTRY));
ctEventEntry.type = type;
OvsPostCtEvent(&ctEventEntry);
}
static __inline VOID
OvsCtIncrementCounters(POVS_CT_ENTRY entry, BOOLEAN reply, PNET_BUFFER_LIST nbl)
{
NdisAcquireSpinLock(&(entry->lock));
if (reply) {
entry->rev_key.byteCount+= OvsPacketLenNBL(nbl);
entry->rev_key.packetCount++;
} else {
entry->key.byteCount += OvsPacketLenNBL(nbl);
entry->key.packetCount++;
}
NdisReleaseSpinLock(&(entry->lock));
}
static __inline BOOLEAN
OvsCtAddEntry(POVS_CT_ENTRY entry,
OvsConntrackKeyLookupCtx *ctx,
PNAT_ACTION_INFO natInfo, UINT64 now)
{
LOCK_STATE_EX lockState;
NdisMoveMemory(&entry->key, &ctx->key, sizeof(OVS_CT_KEY));
NdisMoveMemory(&entry->rev_key, &ctx->key, sizeof(OVS_CT_KEY));
OvsCtKeyReverse(&entry->rev_key);
/* NatInfo is always initialized to be disabled, so that if NAT action
* fails, we will not end up deleting an non-existent NAT entry.
*/
if (natInfo == NULL) {
entry->natInfo.natAction = NAT_ACTION_NONE;
} else {
if (OvsIsForwardNat(natInfo->natAction)) {
entry->natInfo = *natInfo;
if (!OvsNatTranslateCtEntry(entry)) {
return FALSE;
}
ctx->hash = OvsCtHashKey(&entry->key);
} else {
entry->natInfo.natAction = natInfo->natAction;
}
}
entry->timestampStart = now;
NdisAllocateSpinLock(&(entry->lock));
UINT32 bucketIdx = ctx->hash & CT_HASH_TABLE_MASK;
NdisAcquireRWLockWrite(ovsCtBucketLock[bucketIdx], &lockState, 0);
InsertHeadList(&ovsConntrackTable[bucketIdx],
&entry->link);
NdisInterlockedIncrement((PLONG)&ctTotalEntries);
NdisInterlockedIncrement((PLONG)&zoneInfo[ctx->key.zone].entries);
NdisReleaseRWLock(ovsCtBucketLock[bucketIdx], &lockState);
return TRUE;
}
static __inline POVS_CT_ENTRY
OvsCtEntryCreate(OvsForwardingContext *fwdCtx,
UINT8 ipProto,
OVS_PACKET_HDR_INFO *layers,
OvsConntrackKeyLookupCtx *ctx,
OvsFlowKey *key,
PNAT_ACTION_INFO natInfo,
BOOLEAN commit,
UINT64 currentTime,
BOOLEAN *entryCreated)
{
POVS_CT_ENTRY entry = NULL;
UINT32 state = 0;
POVS_CT_ENTRY parentEntry;
PNET_BUFFER_LIST curNbl = fwdCtx->curNbl;
*entryCreated = FALSE;
state |= OVS_CS_F_NEW;
switch (ipProto) {
case IPPROTO_TCP:
{
UINT32 tcpPayloadLen;
TCPHdr tcpStorage;
const TCPHdr *tcp;
tcp = OvsGetTcpHeader(curNbl, layers, &tcpStorage, &tcpPayloadLen);
if (!OvsConntrackValidateTcpPacket(tcp)) {
state = OVS_CS_F_INVALID;
break;
}
if (commit) {
entry = OvsConntrackCreateTcpEntry(tcp, currentTime,
tcpPayloadLen);
}
break;
}
case IPPROTO_ICMPV6:
{
ICMPHdr storage;
const ICMPHdr *icmp;
icmp = OvsGetIcmp(curNbl, layers->l4Offset, &storage);
if (!OvsConntrackValidateIcmp6Packet(icmp)) {
if (icmp) {
OVS_LOG_TRACE("Invalid ICMP6 packet detected, icmp->type %u",
icmp->type);
}
state = OVS_CS_F_INVALID;
break;
}
if (commit) {
entry = OvsConntrackCreateIcmpEntry(currentTime);
}
break;
}
case IPPROTO_ICMP:
{
ICMPHdr storage;
const ICMPHdr *icmp;
icmp = OvsGetIcmp(curNbl, layers->l4Offset, &storage);
if (!OvsConntrackValidateIcmpPacket(icmp)) {
if(icmp) {
OVS_LOG_TRACE("Invalid ICMP packet detected, icmp->type %u",
icmp->type);
}
state = OVS_CS_F_INVALID;
break;
}
if (commit) {
entry = OvsConntrackCreateIcmpEntry(currentTime);
}
break;
}
case IPPROTO_UDP:
{
if (commit) {
entry = OvsConntrackCreateOtherEntry(currentTime);
}
break;
}
default:
OVS_LOG_TRACE("Invalid packet detected, protocol not supported"
" ipProto %u", ipProto);
state = OVS_CS_F_INVALID;
break;
}
parentEntry = OvsCtRelatedLookup(ctx->key, currentTime);
if (parentEntry != NULL && state != OVS_CS_F_INVALID) {
state |= OVS_CS_F_RELATED;
}
if (state != OVS_CS_F_INVALID && commit) {
if (entry) {
entry->parent = parentEntry;
if (OvsCtAddEntry(entry, ctx, natInfo, currentTime)) {
*entryCreated = TRUE;
} else {
/* Unable to add entry to the list */
OvsFreeMemoryWithTag(entry, OVS_CT_POOL_TAG);
state = OVS_CS_F_INVALID;
entry = NULL;
}
} else {
/* OvsAllocateMemoryWithTag returned NULL; treat as invalid */
state = OVS_CS_F_INVALID;
}
}
OvsCtUpdateFlowKey(key, state, ctx->key.zone, 0, NULL);
if (entry) {
OvsCtIncrementCounters(entry, ctx->reply, curNbl);
}
return entry;
}
static enum CT_UPDATE_RES
OvsCtUpdateEntry(OVS_CT_ENTRY* entry,
PNET_BUFFER_LIST nbl,
UINT8 ipProto,
OVS_PACKET_HDR_INFO *layers,
BOOLEAN reply,
UINT64 now)
{
CT_UPDATE_RES status;
switch (ipProto) {
case IPPROTO_TCP:
{
UINT32 tcpPayloadLen;
TCPHdr tcpStorage;
const TCPHdr *tcp;
tcp = OvsGetTcpHeader(nbl, layers, &tcpStorage, &tcpPayloadLen);
if (!tcp) {
status = CT_UPDATE_INVALID;
break;
}
NdisAcquireSpinLock(&(entry->lock));
status = OvsConntrackUpdateTcpEntry(entry, tcp, reply, now,
tcpPayloadLen);
NdisReleaseSpinLock(&(entry->lock));
break;
}
case IPPROTO_ICMP:
{
NdisAcquireSpinLock(&(entry->lock));
status = OvsConntrackUpdateIcmpEntry(entry, reply, now);
NdisReleaseSpinLock(&(entry->lock));
break;
}
case IPPROTO_ICMPV6:
{
NdisAcquireSpinLock(&(entry->lock));
status = OvsConntrackUpdateIcmpEntry(entry, reply, now);
NdisReleaseSpinLock(&(entry->lock));
break;
}
case IPPROTO_UDP:
{
NdisAcquireSpinLock(&(entry->lock));
status = OvsConntrackUpdateOtherEntry(entry, reply, now);
NdisReleaseSpinLock(&(entry->lock));
break;
}
default:
status = CT_UPDATE_INVALID;
break;
}
return status;
}
/*
*----------------------------------------------------------------------------
* OvsCtEntryExpired
* Assumes ct entry lock is acquired
*----------------------------------------------------------------------------
*/
static __inline BOOLEAN
OvsCtEntryExpired(POVS_CT_ENTRY entry)
{
UINT64 currentTime;
NdisGetCurrentSystemTime((LARGE_INTEGER *)¤tTime);
return entry->expiration < currentTime;
}
static __inline VOID
OvsCtEntryDelete(POVS_CT_ENTRY entry, BOOLEAN forceDelete)
{
if (entry == NULL) {
return;
}
KIRQL irql = KeGetCurrentIrql();
OVS_ACQUIRE_SPIN_LOCK(&(entry->lock), irql);
if (forceDelete || OvsCtEntryExpired(entry)) {
if (entry->natInfo.natAction) {
OvsNatDeleteKey(&entry->key);
}
NdisInterlockedDecrement((PLONG)&zoneInfo[entry->key.zone].entries);
OvsPostCtEventEntry(entry, OVS_EVENT_CT_DELETE);
RemoveEntryList(&entry->link);
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
NdisFreeSpinLock(&(entry->lock));
if (entry->helper_name) {
OvsFreeMemoryWithTag(entry->helper_name, OVS_CT_POOL_TAG);
}
OvsFreeMemoryWithTag(entry, OVS_CT_POOL_TAG);
NdisInterlockedDecrement((PLONG)&ctTotalEntries);
return;
}
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
}
static __inline NDIS_STATUS
OvsDetectCtPacket(OvsForwardingContext *fwdCtx,
OvsFlowKey *key)
{
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
OvsFlowKey newFlowKey = { 0 };
switch (ntohs(key->l2.dlType)) {
case ETH_TYPE_IPV4:
if (key->ipKey.nwFrag != OVS_FRAG_TYPE_NONE) {
status = OvsProcessIpv4Fragment(fwdCtx->switchContext,
&fwdCtx->curNbl,
fwdCtx->completionList,
fwdCtx->fwdDetail->SourcePortId,
&fwdCtx->layers,
key->tunKey.tunnelId);
if (status == NDIS_STATUS_SUCCESS) {
/* After the Ipv4 Fragment is reassembled, update flow key as
L3 and L4 headers are not correct */
status = OvsExtractFlow(fwdCtx->curNbl, fwdCtx->srcVportNo,
&newFlowKey, &fwdCtx->layers,
!OvsIphIsZero(&(fwdCtx->tunKey.dst)) ?
&(fwdCtx->tunKey) : NULL);
if (status != NDIS_STATUS_SUCCESS) {
OVS_LOG_ERROR("Extract flow failed Nbl %p", fwdCtx->curNbl);
return status;
}
*key = newFlowKey;
}
return status;
}
if (key->ipKey.nwProto == IPPROTO_TCP
|| key->ipKey.nwProto == IPPROTO_UDP
|| key->ipKey.nwProto == IPPROTO_ICMP) {
return NDIS_STATUS_SUCCESS;
}
return NDIS_STATUS_NOT_SUPPORTED;
case ETH_TYPE_IPV6:
if (key->ipv6Key.nwFrag != OVS_FRAG_TYPE_NONE) {
status = OvsProcessIpv6Fragment(fwdCtx->switchContext,
&fwdCtx->curNbl,
fwdCtx->completionList,
fwdCtx->fwdDetail->SourcePortId,
&fwdCtx->layers,
key->tunKey.tunnelId, key);
if (status == NDIS_STATUS_SUCCESS) {
status = OvsExtractFlow(fwdCtx->curNbl, fwdCtx->srcVportNo,
&newFlowKey, &fwdCtx->layers,
!OvsIphIsZero(&(fwdCtx->tunKey.dst)) ?
&(fwdCtx->tunKey) : NULL);
if (status != NDIS_STATUS_SUCCESS) {
OVS_LOG_ERROR("Extract flow for ipv6 failed Nbl %p",
fwdCtx->curNbl);
return status;
}
*key = newFlowKey;
}
return status;
}
if (key->ipv6Key.nwProto == IPPROTO_ICMPV6
|| key->ipv6Key.nwProto == IPPROTO_TCP
|| key->ipv6Key.nwProto == IPPROTO_UDP) {
return NDIS_STATUS_SUCCESS;
}
return NDIS_STATUS_NOT_SUPPORTED;
}
return NDIS_STATUS_NOT_SUPPORTED;
}
BOOLEAN
OvsCtEndpointsAreSame(OVS_CT_KEY ctxKey, OVS_CT_KEY entryKey)
{
return ((NdisEqualMemory(&ctxKey.src, &entryKey.src,
sizeof(struct ct_endpoint))) &&
(NdisEqualMemory(&ctxKey.dst, &entryKey.dst,
sizeof(struct ct_endpoint))));
}
POVS_CT_ENTRY
OvsCtLookup(OvsConntrackKeyLookupCtx *ctx)
{
PLIST_ENTRY link;
POVS_CT_ENTRY entry;
BOOLEAN reply = FALSE;
POVS_CT_ENTRY found = NULL;
LOCK_STATE_EX lockStateTable;
UINT32 bucketIdx;
if (!ctTotalEntries) {
return found;
}
/* Reverse NAT must be performed before OvsCtLookup, so here
* we simply need to flip the src and dst in key and compare
* they are equal. Note that flipped key is not equal to
* rev_key due to NAT effect.
*/
OVS_CT_KEY revCtxKey = ctx->key;
OvsCtKeyReverse(&revCtxKey);
KIRQL irql = KeGetCurrentIrql();
bucketIdx = ctx->hash & CT_HASH_TABLE_MASK;
NdisAcquireRWLockRead(ovsCtBucketLock[bucketIdx], &lockStateTable, 0);
LIST_FORALL(&ovsConntrackTable[bucketIdx], link) {
entry = CONTAINING_RECORD(link, OVS_CT_ENTRY, link);
OVS_ACQUIRE_SPIN_LOCK(&(entry->lock), irql);
if ((ctx->key.dl_type != entry->key.dl_type) ||
(ctx->key.nw_proto != entry->key.nw_proto) ||
(ctx->key.zone != entry->key.zone)) {
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
continue;
}
if (OvsCtEndpointsAreSame(ctx->key, entry->key)) {
found = entry;
reply = FALSE;
}
if (!found && OvsCtEndpointsAreSame(revCtxKey, entry->key)) {
found = entry;
reply = TRUE;
}
if (found) {
if (OvsCtEntryExpired(found)) {
found = NULL;
} else {
ctx->reply = reply;
}
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
break;
}
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
}
NdisReleaseRWLock(ovsCtBucketLock[bucketIdx], &lockStateTable);
ctx->entry = found;
return found;
}
const TCPHdr*
OvsGetTcpHeader(PNET_BUFFER_LIST nbl,
OVS_PACKET_HDR_INFO *layers,
VOID *storage,
UINT32 *tcpPayloadLen)
{
IPHdr *ipHdr;
IPv6Hdr *ipv6Hdr;
TCPHdr *tcp;
VOID *dest = storage;
uint16_t ipv6ExtLength = 0;
if (layers->isIPv6) {
ipv6Hdr = NdisGetDataBuffer(NET_BUFFER_LIST_FIRST_NB(nbl),
layers->l4Offset + sizeof(TCPHdr),
NULL, 1, 0);
if (ipv6Hdr == NULL) {
return NULL;
}
tcp = (TCPHdr *)((PCHAR)ipv6Hdr + layers->l4Offset);
ipv6Hdr = (IPv6Hdr *)((PCHAR)ipv6Hdr + layers->l3Offset);
if (tcp->doff * 4 >= sizeof *tcp) {
NdisMoveMemory(dest, tcp, sizeof(TCPHdr));
ipv6ExtLength = layers->l4Offset - layers->l3Offset - sizeof(IPv6Hdr);
*tcpPayloadLen = (ntohs(ipv6Hdr->payload_len) - ipv6ExtLength - TCP_HDR_LEN(tcp));
return storage;
}
} else {
ipHdr = NdisGetDataBuffer(NET_BUFFER_LIST_FIRST_NB(nbl),
layers->l4Offset + sizeof(TCPHdr),
NULL, 1 /*no align*/, 0);
if (ipHdr == NULL) {
return NULL;
}
ipHdr = (IPHdr *)((PCHAR)ipHdr + layers->l3Offset);
tcp = (TCPHdr *)((PCHAR)ipHdr + ipHdr->ihl * 4);
if (tcp->doff * 4 >= sizeof *tcp) {
NdisMoveMemory(dest, tcp, sizeof(TCPHdr));
*tcpPayloadLen = TCP_DATA_LENGTH(ipHdr, tcp);
return storage;
}
}
return NULL;
}
static UINT8
OvsReverseIcmpType(UINT8 type)
{
switch (type) {
case ICMP4_ECHO_REQUEST:
return ICMP4_ECHO_REPLY;
case ICMP4_ECHO_REPLY:
return ICMP4_ECHO_REQUEST;
case ICMP4_TIMESTAMP_REQUEST:
return ICMP4_TIMESTAMP_REPLY;
case ICMP4_TIMESTAMP_REPLY:
return ICMP4_TIMESTAMP_REQUEST;
case ICMP4_INFO_REQUEST:
return ICMP4_INFO_REPLY;
case ICMP4_INFO_REPLY:
return ICMP4_INFO_REQUEST;
case ICMP6_ECHO_REQUEST:
return ICMP6_ECHO_REPLY;
case ICMP6_ECHO_REPLY:
return ICMP6_ECHO_REQUEST;
default:
return 0;
}
}
static __inline void
OvsPickupCtTupleAsLookupKey(POVS_CT_KEY ctKey, UINT16 zone, OvsFlowKey *flowKey)
{
UINT32 ipAddrSrc = 0, ipAddrDst = 0;
if (!flowKey || !ctKey) return;
if (flowKey->l2.dlType == htons(ETH_TYPE_IPV4)) {
ipAddrSrc = flowKey->ct.tuple_ipv4.ipv4_src;
ipAddrDst = flowKey->ct.tuple_ipv4.ipv4_dst;
if ((ipAddrSrc > 0 && ipAddrDst > 0) &&
(zone == flowKey->ct.zone)) {
/* if the ct tuple_ipv4 in flowKey is not null and ct.zone is same with
* zone parameter pickup the tuple_ipv4 value as the lookup key
*/
ctKey->src.addr.ipv4 = flowKey->ct.tuple_ipv4.ipv4_src;
ctKey->dst.addr.ipv4 = flowKey->ct.tuple_ipv4.ipv4_dst;
ctKey->nw_proto = flowKey->ct.tuple_ipv4.ipv4_proto;
ctKey->src.port = flowKey->ct.tuple_ipv4.src_port;
ctKey->dst.port = flowKey->ct.tuple_ipv4.dst_port;
}
}
}
static __inline NDIS_STATUS
OvsCtSetupLookupCtx(OvsFlowKey *flowKey,
UINT16 zone,
OvsConntrackKeyLookupCtx *ctx,
PNET_BUFFER_LIST curNbl,
UINT32 l4Offset)
{
const OVS_NAT_ENTRY *natEntry;
ctx->key.zone = zone;
ctx->key.dl_type = flowKey->l2.dlType;
ctx->related = FALSE;
/* Extract L3 and L4*/
if (flowKey->l2.dlType == htons(ETH_TYPE_IPV4)) {
ctx->key.src.addr.ipv4 = flowKey->ipKey.nwSrc;
ctx->key.dst.addr.ipv4 = flowKey->ipKey.nwDst;
ctx->key.nw_proto = flowKey->ipKey.nwProto;
ctx->key.src.port = flowKey->ipKey.l4.tpSrc;
ctx->key.dst.port = flowKey->ipKey.l4.tpDst;
if (flowKey->ipKey.nwProto == IPPROTO_ICMP) {
ICMPHdr icmpStorage;
const ICMPHdr *icmp;
icmp = OvsGetIcmp(curNbl, l4Offset, &icmpStorage);
ASSERT(icmp);
/* Related bit is set when ICMP has an error */
/* XXX parse out the appropriate src and dst from inner pkt */
switch (icmp->type) {
case ICMP4_ECHO_REQUEST:
case ICMP4_ECHO_REPLY:
case ICMP4_TIMESTAMP_REQUEST:
case ICMP4_TIMESTAMP_REPLY:
case ICMP4_INFO_REQUEST:
case ICMP4_INFO_REPLY:
if (icmp->code != 0) {
return NDIS_STATUS_INVALID_PACKET;
}
/* Separate ICMP connection: identified using id */
ctx->key.dst.icmp_id = ntohs(icmp->fields.echo.id);
ctx->key.src.icmp_id = ntohs(icmp->fields.echo.id);
ctx->key.src.icmp_type = icmp->type;
ctx->key.dst.icmp_type = OvsReverseIcmpType(icmp->type);
break;
case ICMP4_DEST_UNREACH:
case ICMP4_TIME_EXCEEDED:
case ICMP4_PARAM_PROB:
case ICMP4_SOURCE_QUENCH:
case ICMP4_REDIRECT: {
ctx->related = TRUE;
break;
}
default:
ctx->related = FALSE;
}
}
} else if (flowKey->l2.dlType == htons(ETH_TYPE_IPV6)) {
ctx->key.src.addr.ipv6 = flowKey->ipv6Key.ipv6Src;
ctx->key.dst.addr.ipv6 = flowKey->ipv6Key.ipv6Dst;
ctx->key.nw_proto = flowKey->ipv6Key.nwProto;
ctx->key.src.port = flowKey->ipv6Key.l4.tpSrc;
ctx->key.dst.port = flowKey->ipv6Key.l4.tpDst;
if (flowKey->ipv6Key.nwProto == IPPROTO_ICMPV6) {
ICMPHdr icmpStorage;
const ICMPHdr *icmp;
icmp = OvsGetIcmp(curNbl, l4Offset, &icmpStorage);
ASSERT(icmp);
switch (icmp->type) {
case ICMP6_ECHO_REQUEST:
case ICMP6_ECHO_REPLY: {
ctx->key.dst.icmp_id = ntohs(icmp->fields.echo.id);
ctx->key.src.icmp_id = ntohs(icmp->fields.echo.id);
ctx->key.src.icmp_type = icmp->type;
ctx->key.dst.icmp_type = OvsReverseIcmpType(icmp->type);
break;
}
case ICMP6_DST_UNREACH:
case ICMP6_TIME_EXCEEDED:
case ICMP6_PARAM_PROB:
case ICMP6_PACKET_TOO_BIG: {
Ipv6Key ipv6Key;
OVS_PACKET_HDR_INFO layers;
OvsExtractLayers(curNbl, &layers);
layers.l3Offset = layers.l7Offset;
NDIS_STATUS status = OvsParseIPv6(curNbl, &ipv6Key, &layers);
if (status != NDIS_STATUS_SUCCESS) {
return NDIS_STATUS_INVALID_PACKET;
}
ctx->key.src.addr.ipv6 = ipv6Key.ipv6Src;
ctx->key.dst.addr.ipv6 = ipv6Key.ipv6Dst;
ctx->key.nw_proto = ipv6Key.nwProto;
if (ipv6Key.nwProto == SOCKET_IPPROTO_TCP) {
OvsParseTcp(curNbl, &(ipv6Key.l4), &layers);
} else if (ipv6Key.nwProto == SOCKET_IPPROTO_UDP) {
OvsParseUdp(curNbl, &(ipv6Key.l4), &layers);
} else if (ipv6Key.nwProto == SOCKET_IPPROTO_SCTP) {
OvsParseSctp(curNbl, &ipv6Key.l4, &layers);
}
ctx->key.src.port = ipv6Key.l4.tpSrc;
ctx->key.dst.port = ipv6Key.l4.tpDst;
OvsCtKeyReverse(&ctx->key);
ctx->related = TRUE;
break;
}
default:
ctx->related = FALSE;
}
}
} else {
return NDIS_STATUS_INVALID_PACKET;
}
/* It's only designed for unNat traffic, when reverse traffic comes,
* find the unNat table, if found the nat entry, based on the nat entry
* restore the conntrack, it will be stored in the ctx->key and then use the
* ctx->key lookup the conntrack table to find the corresponded
* entry with the traffic.*/
natEntry = OvsNatLookup(&ctx->key, TRUE);
if (natEntry) {
/* initial direction 20::1 -> 20::9, reverse direction 21::3 -> 20::1
* 20::9 could be regarded as nat ip, before convert, ctx->key value
* is "21::3 -> 20::1", after convert, ctx->key value is
* "20::9->20::1" */
ctx->key = natEntry->ctEntry->key;
OvsCtKeyReverse(&ctx->key);
} else {
if (OvsNatLookup(&ctx->key, FALSE)) {
/* Do nothing here, this branch here used to exclude traffic
* described in https://github.com/openvswitch/ovs-issues/issues/237
* */
} else if (flowKey->l2.dlType == htons(ETH_TYPE_IPV4)) {
OvsPickupCtTupleAsLookupKey(&(ctx->key), zone, flowKey);
}
}
ctx->hash = OvsCtHashKey(&ctx->key);
return NDIS_STATUS_SUCCESS;
}
static __inline BOOLEAN
OvsDetectFtpPacket(OvsFlowKey *key) {
return (key->ipKey.nwProto == IPPROTO_TCP &&
(ntohs(key->ipKey.l4.tpDst) == IPPORT_FTP ||
ntohs(key->ipKey.l4.tpSrc) == IPPORT_FTP));
}
static __inline BOOLEAN
OvsDetectFtp6Packet(OvsFlowKey *key) {
return (key->ipv6Key.nwProto == IPPROTO_TCP &&
(ntohs(key->ipv6Key.l4.tpDst) == IPPORT_FTP ||
ntohs(key->ipv6Key.l4.tpSrc) == IPPORT_FTP));
}
static __inline BOOLEAN
OvsDetectTftpPacket(OvsFlowKey *key) {
return (key->ipKey.nwProto == IPPROTO_UDP &&
(ntohs(key->ipKey.l4.tpDst) == IPPORT_TFTP));
}
static __inline BOOLEAN
OvsDetectTftp6Packet(OvsFlowKey *key) {
return (key->ipv6Key.nwProto == IPPROTO_UDP &&
(ntohs(key->ipv6Key.l4.tpDst) == IPPORT_TFTP));
}
/*
*----------------------------------------------------------------------------
* OvsProcessConntrackEntry
* Check the TCP flags and set the ct_state of the entry
*----------------------------------------------------------------------------
*/
static __inline POVS_CT_ENTRY
OvsProcessConntrackEntry(OvsForwardingContext *fwdCtx,
OVS_PACKET_HDR_INFO *layers,
OvsConntrackKeyLookupCtx *ctx,
OvsFlowKey *key,
UINT16 zone,
NAT_ACTION_INFO *natInfo,
BOOLEAN commit,
UINT64 currentTime,
BOOLEAN *entryCreated)
{
POVS_CT_ENTRY entry = ctx->entry;
UINT32 state = 0;
PNET_BUFFER_LIST curNbl = fwdCtx->curNbl;
LOCK_STATE_EX lockStateTable;
*entryCreated = FALSE;
/* If an entry was found, update the state based on TCP flags */
if (ctx->related) {
state |= OVS_CS_F_RELATED;
if (ctx->reply) {
state |= OVS_CS_F_REPLY_DIR;
}
} else {
CT_UPDATE_RES result;
UINT32 bucketIdx;
if (layers->isIPv6) {
result = OvsCtUpdateEntry(entry, curNbl, key->ipv6Key.nwProto, layers,
ctx->reply, currentTime);
} else {
result = OvsCtUpdateEntry(entry, curNbl, key->ipKey.nwProto, layers,
ctx->reply, currentTime);
}
switch (result) {
case CT_UPDATE_VALID:
state |= OVS_CS_F_ESTABLISHED;
/** when the ct state is established, at least
* request/reply two packets go through,
* so the ct_state shouldn't contain new.**/
state &= ~OVS_CS_F_NEW;
if (ctx->reply) {
state |= OVS_CS_F_REPLY_DIR;
}
break;
case CT_UPDATE_INVALID:
state |= OVS_CS_F_INVALID;
break;
case CT_UPDATE_NEW:
//Delete and update the Conntrack
bucketIdx = ctx->hash & CT_HASH_TABLE_MASK;
NdisAcquireRWLockWrite(ovsCtBucketLock[bucketIdx], &lockStateTable, 0);