-
Notifications
You must be signed in to change notification settings - Fork 51
/
datapath_udp.go
992 lines (848 loc) · 36.2 KB
/
datapath_udp.go
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
package nfqdatapath
// Go libraries
import (
"errors"
"fmt"
"strconv"
"time"
"go.aporeto.io/enforcerd/trireme-lib/collector"
"go.aporeto.io/enforcerd/trireme-lib/controller/constants"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/claimsheader"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/connection"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/counters"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/packet"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/pucontext"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/tokens"
markconstants "go.aporeto.io/enforcerd/trireme-lib/utils/constants"
"go.uber.org/zap"
)
const (
// Default retransmit delay for first packet
retransmitDelay = 200
// rentrasmitRetries is the number of times we will retry
retransmitRetries = 3
// ACLCheckMultipler is the multiplie on delay that is used to attempt and fallbackto acls
ACLCheckMultipler = retransmitDelay * 12
)
// DropReason is used to indicate the drop reason for a packet
type DropReason string
// DropReason is the reason a packet is dropped and fin packets are generated
const (
InvalidUDPState DropReason = "invalidUDPState"
PolicyDrop DropReason = "policyDrop"
)
var errHandshakePacket = errors.New("handshake packet")
var errDropQueuedPacket = errors.New("dropping queued packet")
func calculatedelay(retransmitDelay uint32, multiplier uint32) time.Duration {
return time.Duration(retransmitDelay * (multiplier + 1))
}
// ProcessNetworkUDPPacket processes packets arriving from network and are destined to the application.
func (d *Datapath) ProcessNetworkUDPPacket(p *packet.Packet) (conn *connection.UDPConnection, err error) {
if d.PacketLogsEnabled() {
zap.L().Debug("Processing network packet ",
zap.String("flow", p.L4FlowHash()),
)
defer zap.L().Debug("Finished Processing network packet ",
zap.String("flow", p.L4FlowHash()),
zap.Error(err),
)
}
udpPacketType := p.GetUDPType()
switch udpPacketType {
case packet.UDPSynMask:
conn, err = d.netSynUDPRetrieveState(p)
if err != nil {
if d.PacketLogsEnabled() {
zap.L().Debug("Packet rejected",
zap.String("flow", p.L4FlowHash()),
zap.Error(err),
)
}
return nil, err
}
case packet.UDPSynAckMask, packet.UDPPolicyRejectMask:
conn, err = d.netSynAckUDPRetrieveState(p)
if err != nil {
if d.PacketLogsEnabled() {
zap.L().Debug("Syn ack Packet Rejected/ignored",
zap.String("flow", p.L4FlowHash()),
)
}
return nil, err
}
case packet.UDPFinAckMask:
if err := d.processUDPFinPacket(p); err != nil {
zap.L().Debug("unable to process udp fin ack",
zap.String("flowhash", p.L4FlowHash()), zap.Error(err))
return nil, err
}
// drop control packets
return conn, fmt.Errorf("dropping udp fin ack control packet")
default:
// Process packets that don't have the control header. These are data packets.
conn, err = d.netUDPAckRetrieveState(p)
if err != nil {
// Retrieve the context from the packet information.
context, err := d.contextFromIP(false, p.Mark, p.DestPort(), packet.IPProtocolUDP)
if err != nil {
return nil, counters.CounterError(counters.ErrNonPUUDPTraffic, errNonPUUDPTraffic)
}
// Check if a network acl allows this traffic traffic coming from external network
_, packetPolicy, err := context.NetworkACLPolicy(p)
if err == nil && packetPolicy.Action.Accepted() {
context.Counters().IncrementCounter(counters.ErrSynAckToExtNetAccept)
if err = d.conntrack.UpdateApplicationFlowMark(
p.SourceAddress(),
p.DestinationAddress(),
p.IPProto(),
p.SourcePort(),
p.DestPort(),
markconstants.DefaultConnMark,
); err != nil {
zap.L().Error("Failed to update conntrack table for UDP flow at transmitter",
zap.String("net-data-acl", p.L4FlowHash()),
zap.Error(err),
)
}
return conn, nil
}
if err := d.sendUDPFinPacket(p); err != nil {
return nil, fmt.Errorf("net state not found, unable to send fin ack packets: %s", err)
}
if d.PacketLogsEnabled() {
zap.L().Debug("No connection found for the flow, Dropping it",
zap.String("flow", p.L4FlowHash()),
zap.Error(err),
)
}
return nil, err
}
}
// We are processing only one connection at a time.
conn.Lock()
defer conn.Unlock()
p.Print(packet.PacketStageIncoming, d.PacketLogsEnabled())
if d.service != nil {
if !d.service.PreProcessUDPNetPacket(p, conn.Context, conn) {
p.Print(packet.PacketFailureService, d.PacketLogsEnabled())
return conn, conn.Context.Counters().CounterError(counters.ErrUDPNetPreProcessingFailed, errors.New("pre processing failed for network packet"))
}
}
// handle handshake packets and do not deliver to application.
action, claims, err := d.processNetUDPPacket(p, conn.Context, conn)
if err != nil && err != errHandshakePacket && err != errDropQueuedPacket {
zap.L().Debug("Rejecting packet because of policy decision",
zap.String("flow", p.L4FlowHash()),
zap.Error(err),
)
return conn, fmt.Errorf("packet processing failed for network packet: %s", err)
}
// Process the packet by any external services.
if d.service != nil {
if !d.service.PostProcessUDPNetPacket(p, action, claims, conn.Context, conn) {
p.Print(packet.PacketFailureService, d.PacketLogsEnabled())
return conn, conn.Context.Counters().CounterError(counters.ErrUDPNetPostProcessingFailed, errors.New("post service processing failed for network packet"))
}
}
// If reached the final state, drain the queue.
if conn.GetState() == connection.UDPClientSendAck {
conn.SetState(connection.UDPData)
for udpPacket := conn.ReadPacket(); udpPacket != nil; udpPacket = conn.ReadPacket() {
if d.service != nil {
// PostProcessServiceInterface
// We call it for all outgoing packets.
if !d.service.PostProcessUDPAppPacket(udpPacket, nil, conn.Context, conn) {
udpPacket.Print(packet.PacketFailureService, d.PacketLogsEnabled())
zap.L().Error("Failed to encrypt queued packet")
}
}
err = d.ignoreFlow(udpPacket)
if err != nil {
zap.L().Error("Unable to ignore the flow", zap.Error(err))
}
err = d.writeUDPSocket(udpPacket.GetBuffer(0), udpPacket)
if err != nil {
zap.L().Error("Unable to transmit Queued UDP packets", zap.Error(err))
}
}
return conn, fmt.Errorf("Drop the packet")
}
if conn.GetState() != connection.UDPData {
// handshake packets are not to be delivered to application.
return conn, errHandshakePacket
}
return conn, nil
}
func (d *Datapath) netSynUDPRetrieveState(p *packet.Packet) (*connection.UDPConnection, error) {
// Retrieve the context from the packet information.
context, err := d.contextFromIP(false, p.Mark, p.DestPort(), packet.IPProtocolUDP)
if err != nil {
return nil, counters.CounterError(counters.ErrNonPUTraffic, errNonPUTraffic)
}
// Check if a connection already exists for this flow. This can happen
// in the case of retransmissions. If there is no connection, create
// a new one.
conn, cerr := d.udpNetOrigConnectionTracker.Get(p.L4FlowHash())
if cerr != nil {
conn := connection.NewUDPConnection(context, d.udpSocketWriter)
conn.Secrets, conn.Auth.LocalDatapathPrivateKey, conn.Auth.LocalDatapathPublicKeyV1, conn.Auth.LocalDatapathPublicKeySignV1, conn.Auth.LocalDatapathPublicKeyV2, conn.Auth.LocalDatapathPublicKeySignV2 = context.GetSecrets()
return conn, nil
}
return conn.(*connection.UDPConnection), nil
}
func (d *Datapath) netSynAckUDPRetrieveState(p *packet.Packet) (*connection.UDPConnection, error) {
conn, err := d.udpSourcePortConnectionCache.GetReset(p.SourcePortHash(packet.PacketTypeNetwork), 0)
if err != nil {
return nil, counters.CounterError(counters.ErrUDPSynAckNoConnection, errors.New("No connection.Drop the syn ack packet"))
}
return conn.(*connection.UDPConnection), nil
}
func (d *Datapath) netUDPAckRetrieveState(p *packet.Packet) (*connection.UDPConnection, error) {
hash := p.L4FlowHash()
conn, err := d.udpNetReplyConnectionTracker.GetReset(hash, 0)
if err != nil {
conn, err = d.udpNetOrigConnectionTracker.GetReset(hash, 0)
if err != nil {
// This might be an existing udp connection.
// Send FinAck to reauthorize the connection.
return nil, fmt.Errorf("net state not found: %s", err)
}
}
return conn.(*connection.UDPConnection), nil
}
// processNetUDPPacket processes a network UDP packet and dispatches it to different methods based on the flags.
// This applies only to control packets.
func (d *Datapath) processNetUDPPacket(udpPacket *packet.Packet, context *pucontext.PUContext, conn *connection.UDPConnection) (action interface{}, claims *tokens.ConnectionClaims, err error) {
// Extra check, just in case the caller didn't provide a connection.
if conn == nil {
return nil, nil, fmt.Errorf("no connection provided")
}
udpPacketType := udpPacket.GetUDPType()
// Update connection state in the internal state machine tracker
switch udpPacketType {
case packet.UDPSynMask:
// Parse the packet for the identity information.
action, claims, err = d.processNetworkUDPSynPacket(context, conn, udpPacket)
if err != nil {
if err = d.sendUDPRstPacket(udpPacket, conn); err != nil {
zap.L().Error("Unable to send rst packet", zap.Error(err), zap.String("FlowHash", udpPacket.L4FlowHash()))
}
return nil, nil, err
}
// Send the return packet.
if err = d.sendUDPSynAckPacket(udpPacket, context, conn); err != nil {
return nil, nil, err
}
// Mark the state that we have transmitted a SynAck packet.
conn.SetState(connection.UDPReceiverSendSynAck)
return action, claims, errHandshakePacket
case packet.UDPAckMask:
// Retrieve the header and parse the signatures.
if err = d.processNetworkUDPAckPacket(udpPacket, context, conn); err != nil {
return nil, nil, err
}
// Set the connection to
conn.SetState(connection.UDPReceiverProcessedAck)
return nil, nil, errHandshakePacket
case packet.UDPSynAckMask:
// Process the synack header and claims of the other side.
action, claims, err = d.processNetworkUDPSynAckPacket(udpPacket, context, conn)
if err != nil {
return nil, nil, err
}
// Send back the acknowledgement.
err = d.sendUDPAckPacket(udpPacket, context, conn)
if err != nil {
return nil, nil, err
}
conn.SetState(connection.UDPClientSendAck)
return action, claims, errHandshakePacket
case packet.UDPPolicyRejectMask:
if err := d.processUDPPolicyRstPacket(udpPacket, context, conn); err != nil {
zap.L().Debug("unable to process udp policy rst",
zap.String("flowhash", udpPacket.L4FlowHash()), zap.Error(err))
return conn, nil, err
}
return conn, nil, fmt.Errorf("dropping udp rst control packet")
default:
state := conn.GetState()
if state == connection.UDPReceiverProcessedAck || state == connection.UDPClientSendAck || state == connection.UDPData {
conn.SetState(connection.UDPData)
return nil, nil, nil
}
return nil, nil, fmt.Errorf("invalid packet at state: %d", state)
}
}
// ProcessApplicationUDPPacket processes packets arriving from an application and are destined to the network
func (d *Datapath) ProcessApplicationUDPPacket(p *packet.Packet) (conn *connection.UDPConnection, err error) {
if d.PacketLogsEnabled() {
zap.L().Debug("Processing application UDP packet ",
zap.String("flow", p.L4FlowHash()),
)
defer zap.L().Debug("Finished Processing UDP application packet ",
zap.String("flow", p.L4FlowHash()),
zap.Error(err),
)
}
// First retrieve the connection state.
conn, err = d.appUDPRetrieveState(p)
if err != nil {
zap.L().Debug("Connection not found", zap.Error(err))
return nil, counters.CounterError(counters.ErrNonPUTraffic, errNonPUTraffic)
}
// We are processing only one packet from a given connection at a time.
conn.Lock()
defer conn.Unlock()
// do some pre processing.
if d.service != nil {
// PreProcessServiceInterface
if !d.service.PreProcessUDPAppPacket(p, conn.Context, conn, packet.UDPSynMask) {
p.Print(packet.PacketFailureService, d.PacketLogsEnabled())
return nil, conn.Context.Counters().CounterError(counters.ErrUDPAppPreProcessingFailed, errors.New("pre service processing failed for UDP application packet"))
}
}
triggerControlProtocol := false
switch conn.GetState() {
case connection.UDPStart:
// Queue the packet. We will send it after we authorize the session.
if err = conn.QueuePackets(p); err != nil {
// unable to queue packets, perhaps queue is full. if start
// machine is still in start state, we can start authorisation
// again. A drop counter is incremented.
zap.L().Debug("udp queue full for connection", zap.String("flow", p.L4FlowHash()))
}
// Set the state indicating that we send out a Syn packet
conn.SetState(connection.UDPClientSendSyn)
// Drop the packet. We stored it in the queue.
triggerControlProtocol = true
case connection.UDPReceiverProcessedAck, connection.UDPClientSendAck, connection.UDPData:
conn.SetState(connection.UDPData)
default:
if err = conn.QueuePackets(p); err != nil {
return conn, conn.Context.Counters().CounterError(counters.ErrUDPDropQueueFull, fmt.Errorf("Unable to queue packets:%s", err))
}
return conn, conn.Context.Counters().CounterError(counters.ErrUDPDropInNfQueue, errDropQueuedPacket)
}
if d.service != nil {
// PostProcessServiceInterface
if !d.service.PostProcessUDPAppPacket(p, nil, conn.Context, conn) {
p.Print(packet.PacketFailureService, d.PacketLogsEnabled())
return conn, conn.Context.Counters().CounterError(counters.ErrUDPAppPostProcessingFailed, errors.New("Encryption failed for application packet"))
}
}
if triggerControlProtocol {
err = d.triggerNegotiation(p, conn.Context, conn)
if err != nil {
return conn, conn.Context.Counters().CounterError(counters.ErrUDPDropInNfQueue, errDropQueuedPacket)
}
return conn, errDropQueuedPacket
}
return conn, nil
}
func (d *Datapath) appUDPRetrieveState(p *packet.Packet) (*connection.UDPConnection, error) {
hash := p.L4FlowHash()
if conn, err := d.udpAppReplyConnectionTracker.GetReset(hash, 0); err == nil {
return conn.(*connection.UDPConnection), nil
}
if conn, err := d.udpAppOrigConnectionTracker.GetReset(hash, 0); err == nil {
return conn.(*connection.UDPConnection), nil
}
context, err := d.contextFromIP(true, p.Mark, p.SourcePort(), packet.IPProtocolUDP)
if err != nil {
return nil, counters.CounterError(counters.ErrNonPUTraffic, errors.New("No context in app processing"))
}
return connection.NewUDPConnection(context, d.udpSocketWriter), nil
}
// processApplicationUDPSynPacket processes a single Syn Packet
func (d *Datapath) triggerNegotiation(udpPacket *packet.Packet, context *pucontext.PUContext, conn *connection.UDPConnection) (err error) {
newPacket, err := d.clonePacketHeaders(udpPacket)
if err != nil {
return fmt.Errorf("Unable to clone packet: %s", err)
}
var udpData []byte
conn.Secrets, conn.Auth.LocalDatapathPrivateKey, udpData = context.GetSynToken(nil, conn.Auth.Nonce, nil)
udpOptions := packet.CreateUDPAuthMarker(packet.UDPSynMask, uint16(len(udpData)))
// Attach the UDP data and token
newPacket.UDPTokenAttach(udpOptions, udpData)
if udpPacket.PlatformMetadata != nil {
newPacket.PlatformMetadata = udpPacket.PlatformMetadata.Clone()
}
statusChannel := make(chan bool)
go func() {
// We started a handhsake drop reverse packets automatically
// Assert connmark before relaseing packets if response is receied
if err = d.conntrack.UpdateApplicationFlowMark(
udpPacket.SourceAddress(),
udpPacket.DestinationAddress(),
udpPacket.IPProto(),
udpPacket.SourcePort(),
udpPacket.DestPort(),
markconstants.HandshakeConnmark,
); err != nil {
zap.L().Error("Failed to update conntrack table for UDP flow at transmitter",
zap.String("app-conn", udpPacket.L4FlowHash()),
zap.String("state", fmt.Sprintf("%d", conn.GetState())),
zap.Error(err),
)
}
loop:
for {
select {
case <-statusChannel:
break loop
case <-time.After(ACLCheckMultipler * time.Millisecond):
return
}
}
conn.Lock()
defer conn.Unlock()
if conn.GetState() == connection.UDPStart {
// We did not receive any response from the remote.
// It is most likely an external network lets evaluate acls at this point to see if we are allowed to talk to this ip
report, pkt, perr := context.ApplicationACLPolicyFromAddr(udpPacket.DestinationAddress(), udpPacket.DestPort(), udpPacket.IPProto())
if perr != nil && pkt.Action.Rejected() {
d.reportExternalServiceFlow(context, report, pkt, true, udpPacket)
return
}
<-time.After(50 * time.Millisecond) //Arbitrary number to ensure last handshake packet is dropped in our tables
// Assert connmark before relaseing packets if response is receied
if err = d.conntrack.UpdateApplicationFlowMark(
udpPacket.SourceAddress(),
udpPacket.DestinationAddress(),
udpPacket.IPProto(),
udpPacket.SourcePort(),
udpPacket.DestPort(),
markconstants.DefaultExternalConnMark,
); err != nil {
zap.L().Error("Failed to update conntrack table for UDP flow at transmitter",
zap.String("app-conn", udpPacket.L4FlowHash()),
zap.String("state", fmt.Sprintf("%d", conn.GetState())),
zap.Error(err),
)
}
for udpPacket := conn.ReadPacket(); udpPacket != nil; udpPacket = conn.ReadPacket() {
if d.service != nil {
// PostProcessServiceInterface
// We call it for all outgoing packets.
if !d.service.PostProcessUDPAppPacket(udpPacket, nil, conn.Context, conn) {
udpPacket.Print(packet.PacketFailureService, d.PacketLogsEnabled())
zap.L().Error("Failed to encrypt queued packet")
}
}
err = d.ignoreFlow(udpPacket)
if err != nil {
zap.L().Error("Unable to ignore the flow", zap.Error(err))
}
err = d.writeUDPSocket(udpPacket.GetBuffer(0), udpPacket)
if err != nil {
zap.L().Error("Unable to transmit Queued UDP packets", zap.Error(err))
}
}
conn.SetState(connection.UDPData)
d.reportExternalServiceFlow(context, report, pkt, true, udpPacket)
return
}
}()
// send packet
err = d.writeWithRetransmit(newPacket, conn, conn.SynChannel(), statusChannel)
if err != nil {
zap.L().Error("Unable to send syn token on raw socket", zap.Error(err), zap.Time("time", time.Now()))
return fmt.Errorf("unable to transmit syn packet")
}
// Populate the caches to track the connection
hash := udpPacket.L4FlowHash()
d.udpAppOrigConnectionTracker.AddOrUpdate(hash, conn)
d.udpSourcePortConnectionCache.AddOrUpdate(newPacket.SourcePortHash(packet.PacketTypeApplication), conn)
return nil
}
func (d *Datapath) writeWithRetransmit(udpPacket *packet.Packet, conn *connection.UDPConnection, stop chan bool, statusChan chan bool) error {
buffer := udpPacket.GetBuffer(0)
localBuffer := make([]byte, len(buffer))
copy(localBuffer, buffer)
zap.L().Debug("TRYINGT to send control packet", zap.String("FlowHash", udpPacket.L4FlowHash()))
if err := d.writeUDPSocket(localBuffer, udpPacket); err != nil {
zap.L().Error("Failed to write control packet to socket", zap.Error(err), zap.String("FlowHash", udpPacket.L4FlowHash()))
return err
}
go func() {
for retries := 0; retries < retransmitRetries; retries++ {
delay := time.Millisecond * time.Duration((retransmitDelay * (retries + 1)))
select {
case <-stop:
return
case <-time.After(delay):
if err := d.writeUDPSocket(localBuffer, udpPacket); err != nil {
zap.L().Error("Failed to write control packet to socket", zap.Error(err), zap.String("FlowHash", udpPacket.L4FlowHash()))
}
}
}
// We did not get a synack maybe this dest is an external network
if statusChan != nil {
zap.L().Debug("Timedout should start acl")
statusChan <- true
}
// retransmits did not succeed. Reset the state machine so that
// next packet can try again.
conn.SetState(connection.UDPStart)
}()
return nil
}
func (d *Datapath) clonePacketHeaders(p *packet.Packet) (*packet.Packet, error) {
// copy the ip and udp headers.
newSize := uint16(p.IPHeaderLen() + packet.UDPDataPos)
newPacket := make([]byte, newSize)
p.FixupIPHdrOnDataModify(p.IPTotalLen(), newSize)
origBuffer := p.GetBuffer(0)
_ = copy(newPacket, origBuffer[:newSize])
return packet.New(packet.PacketTypeApplication, newPacket, p.Mark, true)
}
// sendUDPSynAckPacket processes a UDP SynAck packet
func (d *Datapath) sendUDPSynAckPacket(udpPacket *packet.Packet, context *pucontext.PUContext, conn *connection.UDPConnection) (err error) {
claimsHeader := claimsheader.NewClaimsHeader()
claims := &tokens.ConnectionClaims{
CT: context.CompressedTags(),
LCL: conn.Auth.Nonce[:],
RMT: conn.Auth.RemoteNonce,
DEKV1: conn.Auth.LocalDatapathPublicKeyV1,
SDEKV1: conn.Auth.LocalDatapathPublicKeySignV1,
DEKV2: conn.Auth.LocalDatapathPublicKeyV2,
SDEKV2: conn.Auth.LocalDatapathPublicKeySignV2,
ID: context.ManagementID(),
RemoteID: conn.Auth.RemoteContextID,
}
var udpData []byte
udpData, err = d.tokenAccessor.CreateSynAckPacketToken(conn.Auth.Proto314, claims, conn.EncodedBuf[:], conn.Auth.Nonce[:], claimsHeader, conn.Secrets, conn.Auth.SecretKey)
if err != nil {
return counters.CounterError(appUDPSynAckCounterFromError(err), err)
}
// Create UDP Option
udpPacket.CreateReverseFlowPacket()
// This for Windows and isn't necessary, but helps when driver is logging
err = d.reverseFlow(udpPacket)
if err != nil {
return counters.CounterError(appUDPSynAckCounterFromError(err), err)
}
// Create UDP Option
udpOptions := packet.CreateUDPAuthMarker(packet.UDPSynAckMask, uint16(len(udpData)))
// Attach the UDP data and token
udpPacket.UDPTokenAttach(udpOptions, udpData)
// If we have already a backgroun re-transmit session, stop it at this point. We will
// start from the beginning.
if conn.GetState() == connection.UDPReceiverSendSynAck {
conn.SynAckStop()
}
// Only start the retransmission timer once. Not on every packet.
if err := d.writeWithRetransmit(udpPacket, conn, conn.SynAckChannel(), nil); err != nil {
zap.L().Debug("Unable to send synack token on raw socket", zap.Error(err))
return err
}
return nil
}
func (d *Datapath) sendUDPAckPacket(udpPacket *packet.Packet, context *pucontext.PUContext, conn *connection.UDPConnection) (err error) {
// This for Windows and isn't necessary, but helps when driver is logging
err = d.reverseFlow(udpPacket)
if err != nil {
return counters.CounterError(appUDPAckCounterFromError(err), err)
}
udpPacket.CreateReverseFlowPacket()
claims := &tokens.ConnectionClaims{
ID: context.ManagementID(),
RMT: conn.Auth.RemoteNonce,
RemoteID: conn.Auth.RemoteContextID,
}
udpData, err := d.tokenAccessor.CreateAckPacketToken(conn.Auth.Proto314, conn.Auth.SecretKey, claims, conn.EncodedBuf[:])
if err != nil {
return counters.CounterError(appUDPAckCounterFromError(err), err)
}
// Create UDP Option
udpOptions := packet.CreateUDPAuthMarker(packet.UDPAckMask, uint16(len(udpData)))
// Attach the UDP data and token
udpPacket.UDPTokenAttach(udpOptions, udpData)
// send packet
err = d.writeUDPSocket(udpPacket.GetBuffer(0), udpPacket)
if err != nil {
return err
}
// We reached final state drain the queue here
<-time.After(40 * time.Millisecond) //Arbitrary number give receiver chance to plumb conntrack
for udpPacket := conn.ReadPacket(); udpPacket != nil; udpPacket = conn.ReadPacket() {
if d.service != nil {
// PostProcessServiceInterface
// We call it for all outgoing packets.
if !d.service.PostProcessUDPAppPacket(udpPacket, nil, conn.Context, conn) {
udpPacket.Print(packet.PacketFailureService, d.PacketLogsEnabled())
zap.L().Error("Failed to encrypt queued packet")
}
}
err = d.ignoreFlow(udpPacket)
if err != nil {
zap.L().Error("Unable to ignore the flow", zap.Error(err))
}
err = d.writeUDPSocket(udpPacket.GetBuffer(0), udpPacket)
if err != nil {
zap.L().Error("Unable to transmit Queued UDP packets", zap.Error(err))
}
}
// When server and client are the same machine, we can't ignore the
// flow until the server side receives the Ack packet
if !udpPacket.SourceAddress().Equal(udpPacket.DestinationAddress()) {
if err := d.ignoreFlow(udpPacket); err != nil {
zap.L().Error("Failed to ignore flow", zap.Error(err))
}
}
if err = d.conntrack.UpdateApplicationFlowMark(
udpPacket.SourceAddress(),
udpPacket.DestinationAddress(),
udpPacket.IPProto(),
udpPacket.SourcePort(),
udpPacket.DestPort(),
markconstants.DefaultConnMark,
); err != nil {
zap.L().Error("Failed to update conntrack table for UDP flow at transmitter",
zap.String("app-conn", udpPacket.L4FlowHash()),
zap.String("state", fmt.Sprintf("%d", conn.GetState())),
zap.Error(err),
)
return err
}
conn.SetState(connection.UDPData)
zap.L().Debug("Clearing fin packet entry in cache", zap.String("flowhash", udpPacket.L4FlowHash()))
if err := d.udpFinPacketTracker.Remove(udpPacket.L4FlowHash()); err != nil {
zap.L().Debug("Unable to remove entry from udp finack cache")
}
return nil
}
// processNetworkUDPSynPacket processes a syn packet arriving from the network
func (d *Datapath) processNetworkUDPSynPacket(context *pucontext.PUContext, conn *connection.UDPConnection, udpPacket *packet.Packet) (action interface{}, claims *tokens.ConnectionClaims, err error) {
rejected := false
networkReport, pkt, perr := context.NetworkACLPolicy(udpPacket)
if perr == nil {
rejected = pkt.Action.Rejected()
if rejected {
perr = fmt.Errorf("rejected by ACL policy %s", pkt.PolicyID)
}
} else {
// We got an error, but ensure it isn't the catch all policy
if !(pkt != nil && pkt.Action.Rejected() && pkt.PolicyID == "default") {
rejected = true
}
}
if rejected {
d.reportExternalServiceFlow(context, networkReport, pkt, false, udpPacket)
return nil, nil, context.Counters().CounterError(counters.ErrUDPSynDroppedPolicy, fmt.Errorf("packet had identity: incoming connection dropped:due to reject acl %s", perr))
}
claims = &conn.Auth.ConnectionClaims
secretKey, _, controller, remoteNonce, remoteContextID, proto314, err := d.tokenAccessor.ParsePacketToken(conn.Auth.LocalDatapathPrivateKey, udpPacket.ReadUDPToken(), conn.Secrets, claims, false)
if err != nil {
d.reportUDPRejectedFlow(udpPacket, conn, collector.DefaultEndPoint, context.ManagementID(), context, collector.InvalidToken, nil, nil, false)
return nil, nil, conn.Context.Counters().CounterError(netUDPSynCounterFromError(err), fmt.Errorf("UDP Syn packet dropped because of invalid token: %s", err))
}
if controller != nil && !controller.SameController {
conn.SourceController = controller.Controller
}
// Why is this required. Take a look.
//txLabel, _ := claims.T.Get(enforcerconstants.TransmitterLabel)
// Add the port as a label with an @ prefix. These labels are invalid otherwise
// If all policies are restricted by port numbers this will allow port-specific policies
tags := claims.T.Copy()
tags.AppendKeyValue(constants.PortNumberLabelString, fmt.Sprintf("%s/%s", constants.UDPProtoString, strconv.Itoa(int(udpPacket.DestPort()))))
// Add the controller to the claims
if controller != nil && len(controller.Controller) > 0 {
tags.AppendKeyValue(constants.ControllerLabelString, controller.Controller)
}
report, pkt := context.SearchRcvRules(tags)
if pkt.Action.Rejected() {
d.reportUDPRejectedFlow(udpPacket, conn, remoteContextID, context.ManagementID(), context, collector.PolicyDrop, report, pkt, false)
return nil, nil, conn.Context.Counters().CounterError(counters.ErrUDPSynDroppedPolicy, fmt.Errorf("connection rejected because of policy: %s", claims.T.String()))
}
hash := udpPacket.L4FlowHash()
// conntrack
d.udpNetOrigConnectionTracker.AddOrUpdate(hash, conn)
d.udpAppReplyConnectionTracker.AddOrUpdate(udpPacket.L4ReverseFlowHash(), conn)
conn.Auth.SecretKey = secretKey
conn.Auth.RemoteNonce = remoteNonce
conn.Auth.RemoteContextID = remoteContextID
conn.Auth.Proto314 = proto314
// Record actions
conn.ReportFlowPolicy = report
conn.PacketFlowPolicy = pkt
return pkt, claims, nil
}
func (d *Datapath) processNetworkUDPSynAckPacket(udpPacket *packet.Packet, context *pucontext.PUContext, conn *connection.UDPConnection) (action interface{}, claims *tokens.ConnectionClaims, err error) {
conn.SynStop()
claims = &conn.Auth.ConnectionClaims
secretKey, _, controller, remoteNonce, remoteContextID, proto314, err := d.tokenAccessor.ParsePacketToken(conn.Auth.LocalDatapathPrivateKey, udpPacket.ReadUDPToken(), conn.Secrets, claims, true)
if err != nil {
d.reportUDPRejectedFlow(udpPacket, conn, context.ManagementID(), collector.DefaultEndPoint, context, collector.MissingToken, nil, nil, true)
return nil, nil, conn.Context.Counters().CounterError(netUDPSynAckCounterFromError(err), errors.New("SynAck packet dropped because of bad claims"))
}
if controller != nil && !controller.SameController {
conn.DestinationController = controller.Controller
}
// Add the port as a label with an @ prefix. These labels are invalid otherwise
// If all policies are restricted by port numbers this will allow port-specific policies
tags := claims.T.Copy()
tags.AppendKeyValue(constants.PortNumberLabelString, fmt.Sprintf("%s/%s", constants.UDPProtoString, strconv.Itoa(int(udpPacket.SourcePort()))))
// Add the controller to the claims
if controller != nil && len(controller.Controller) > 0 {
tags.AppendKeyValue(constants.ControllerLabelString, controller.Controller)
}
report, pkt := context.SearchTxtRules(tags, !d.mutualAuthorization)
if pkt.Action.Rejected() {
d.reportUDPRejectedFlow(udpPacket, conn, remoteContextID, context.ManagementID(), context, collector.PolicyDrop, report, pkt, true)
return nil, nil, conn.Context.Counters().CounterError(counters.ErrUDPSynAckPolicy, fmt.Errorf("dropping because of reject rule on transmitter: %s", claims.T.String()))
}
// conntrack
d.udpNetReplyConnectionTracker.AddOrUpdate(udpPacket.L4FlowHash(), conn)
conn.Auth.SecretKey = secretKey
conn.Auth.RemoteNonce = remoteNonce
conn.Auth.RemoteContextID = remoteContextID
conn.Auth.Proto314 = proto314
return pkt, claims, nil
}
func (d *Datapath) processNetworkUDPAckPacket(udpPacket *packet.Packet, context *pucontext.PUContext, conn *connection.UDPConnection) (err error) {
conn.SynAckStop()
if err = d.tokenAccessor.ParseAckToken(conn.Auth.Proto314, conn.Auth.SecretKey, conn.Auth.Nonce[:], udpPacket.ReadUDPToken(), &conn.Auth.ConnectionClaims); err != nil {
d.reportUDPRejectedFlow(udpPacket, conn, conn.Auth.RemoteContextID, context.ManagementID(), context, collector.InvalidToken, conn.ReportFlowPolicy, conn.PacketFlowPolicy, false)
return conn.Context.Counters().CounterError(netUDPAckCounterFromError(err), fmt.Errorf("ack packet dropped because signature validation failed: %s", err))
}
// For Windows, we allow the flow
if err := d.setFlowState(udpPacket, true); err != nil {
zap.L().Error("Failed to ignore flow", zap.Error(err))
}
// Plumb connmark rule here.
if err := d.conntrack.UpdateNetworkFlowMark(
udpPacket.SourceAddress(),
udpPacket.DestinationAddress(),
udpPacket.IPProto(),
udpPacket.SourcePort(),
udpPacket.DestPort(),
markconstants.DefaultConnMark,
); err != nil {
zap.L().Error("Failed to update conntrack table after ack packet")
}
d.reportUDPAcceptedFlow(udpPacket, conn, conn.Auth.RemoteContextID, context.ManagementID(), context, conn.ReportFlowPolicy, conn.PacketFlowPolicy, false)
conn.Context.Counters().IncrementCounter(counters.ErrUDPConnectionsProcessed)
return nil
}
// sendUDPFinPacket sends a Fin packet to Peer.
func (d *Datapath) sendUDPFinPacket(udpPacket *packet.Packet) (err error) {
// Create UDP Option
udpOptions := packet.CreateUDPAuthMarker(packet.UDPFinAckMask, 0)
udpPacket.CreateReverseFlowPacket()
err = d.reverseFlow(udpPacket)
if err != nil {
return counters.CounterError(counters.ErrUDPDropFin, err)
}
// Attach the UDP data and token
udpPacket.UDPTokenAttach(udpOptions, []byte{})
// no need for retransmits here.
err = d.writeUDPSocket(udpPacket.GetBuffer(0), udpPacket)
if err != nil {
zap.L().Debug("Unable to send fin packet on raw socket:", zap.Error(err))
return counters.CounterError(counters.ErrUDPDropFin, fmt.Errorf("Unable to send fin packet on raw socket: %s", err.Error()))
}
return nil
}
// sendUDPRstPacket sends a rst packet to Peer.
func (d *Datapath) sendUDPRstPacket(udpPacket *packet.Packet, conn *connection.UDPConnection) (err error) {
// Create UDP Option
udpOptions := packet.CreateUDPAuthMarker(packet.UDPPolicyRejectMask, 0)
udpPacket.CreateReverseFlowPacket()
// TODO ::: Have a signed payload this packets will force remote end to process acls
// So we have to be sure someone we trust send this
err = d.reverseFlow(udpPacket)
if err != nil {
return conn.Context.Counters().CounterError(counters.ErrUDPDropRst, err)
}
// Attach the UDP data and token
udpPacket.UDPTokenAttach(udpOptions, []byte{})
// For Windows, this mark udpPacket packet so that when writeUDPSocket is called,
// it will send the packet but will drop additional packets for this flow.
if err := d.dropFlow(udpPacket); err != nil {
zap.L().Error("Failed to drop flow", zap.Error(err))
}
// no need for retransmits here.
err = d.writeUDPSocket(udpPacket.GetBuffer(0), udpPacket)
if err != nil {
zap.L().Debug("Unable to send fin packet on raw socket", zap.Error(err))
return conn.Context.Counters().CounterError(counters.ErrUDPDropRst, fmt.Errorf("Unable to send rst packet on raw socket: %s", err.Error()))
}
// conn.SynStop()
// conn.SynAckStop()
// Plumb connmark rule here. drop packet on this flow. Till we see a acceptable handshake packet again
if err := d.conntrack.UpdateNetworkFlowMark(
udpPacket.SourceAddress(),
udpPacket.DestinationAddress(),
udpPacket.IPProto(),
udpPacket.SourcePort(),
udpPacket.DestPort(),
markconstants.DropConnmark,
); err != nil {
zap.L().Error("Failed to update conntrack table after ack packet")
}
return nil
}
func (d *Datapath) processUDPPolicyRstPacket(udpPacket *packet.Packet, context *pucontext.PUContext, conn *connection.UDPConnection) (err error) { // nolint
conn.SetState(connection.UDPRST)
conn.SynStop()
conn.SynAckStop()
if err := d.udpAppOrigConnectionTracker.Remove(udpPacket.L4ReverseFlowHash()); err != nil {
zap.L().Debug("Failed to clean cache udpappOrigConnectionTracker", zap.Error(err))
}
if err := d.udpSourcePortConnectionCache.Remove(udpPacket.SourcePortHash(packet.PacketTypeNetwork)); err != nil {
zap.L().Debug("Failed to clean cache udpsourcePortConnectionCache", zap.Error(err))
}
if err := d.setFlowState(udpPacket, false); err != nil {
zap.L().Error("Failed to drop flow", zap.Error(err))
}
if err := d.conntrack.UpdateNetworkFlowMark(
udpPacket.SourceAddress(),
udpPacket.DestinationAddress(),
udpPacket.IPProto(),
udpPacket.SourcePort(),
udpPacket.DestPort(),
markconstants.DropConnmark,
); err != nil {
zap.L().Error("Failed to update conntrack table after ack packet")
}
return nil
}
// Update the udp fin cache and delete the connmark.
func (d *Datapath) processUDPFinPacket(udpPacket *packet.Packet) (err error) { // nolint
// add it to the udp fin cache. If we have already received the fin packet
// for this flow. There is no need to change the connmark label again.
if d.udpFinPacketTracker.AddOrUpdate(udpPacket.L4ReverseFlowHash(), true) {
return nil
}
// clear cache entries.
if err := d.udpAppOrigConnectionTracker.Remove(udpPacket.L4ReverseFlowHash()); err != nil {
zap.L().Debug("Failed to clean cache udpappOrigConnectionTracker", zap.Error(err))
}
if err := d.udpSourcePortConnectionCache.Remove(udpPacket.SourcePortHash(packet.PacketTypeNetwork)); err != nil {
zap.L().Debug("Failed to clean cache udpsourcePortConnectionCache", zap.Error(err))
}
if err := d.setFlowState(udpPacket, false); err != nil {
zap.L().Error("Failed to drop flow", zap.Error(err))
}
if err = d.conntrack.UpdateNetworkFlowMark(
udpPacket.SourceAddress(),
udpPacket.DestinationAddress(),
udpPacket.IPProto(),
udpPacket.SourcePort(),
udpPacket.DestPort(),
markconstants.DeleteConnmark,
); err != nil {
zap.L().Error("Failed to update conntrack table for flow to terminate connection",
zap.String("app-conn", udpPacket.L4FlowHash()),
zap.Error(err),
)
}
return nil
}
// note: for platforms that need it (Windows), please ensure that udpPacket.PlatformMetadata is set.
// thus, for any Packets created outside of the driver packet callback, the originating metadata must be
// propagated to the udpPacket argument before this call.
func (d *Datapath) writeUDPSocket(buf []byte, udpPacket *packet.Packet) error {
return d.udpSocketWriter.WriteSocket(buf, udpPacket.IPversion(), udpPacket.PlatformMetadata)
}