-
Notifications
You must be signed in to change notification settings - Fork 51
/
connection.go
360 lines (273 loc) · 9.17 KB
/
connection.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
package connection
import (
"fmt"
"sync"
"time"
"go.uber.org/zap"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/nfqdatapath/afinetrawsocket"
"go.aporeto.io/trireme-lib/controller/pkg/packet"
"go.aporeto.io/trireme-lib/controller/pkg/pucontext"
"go.aporeto.io/trireme-lib/policy"
"go.aporeto.io/trireme-lib/utils/cache"
"go.aporeto.io/trireme-lib/utils/crypto"
)
// TCPFlowState identifies the constants of the state of a TCP connectioncon
type TCPFlowState int
// UDPFlowState identifies the constants of the state of a UDP connection.
type UDPFlowState int
// ProxyConnState identifies the constants of the state of a proxied connection
type ProxyConnState int
const (
// TCPSynSend is the state where the Syn packets has been send, but no response has been received
TCPSynSend TCPFlowState = iota
// TCPSynReceived indicates that the syn packet has been received
TCPSynReceived
// TCPSynAckSend indicates that the SynAck packet has been send
TCPSynAckSend
// TCPSynAckReceived is the state where the SynAck has been received
TCPSynAckReceived
// TCPAckSend indicates that the ack packets has been sent
TCPAckSend
// TCPAckProcessed is the state that the negotiation has been completed
TCPAckProcessed
// TCPData indicates that the packets are now data packets
TCPData
// UnknownState indicates that this an existing connection in the uknown state.
UnknownState
)
const (
// ClientTokenSend Init token send for client
ClientTokenSend ProxyConnState = iota
// ServerReceivePeerToken -- waiting to receive peer token
ServerReceivePeerToken
// ServerSendToken -- Send our own token and the client tokens
ServerSendToken
// ClientPeerTokenReceive -- Receive signed tokens from server
ClientPeerTokenReceive
// ClientSendSignedPair -- Sign the (token/nonce pair) and send
ClientSendSignedPair
// ServerAuthenticatePair -- Authenticate pair of tokens
ServerAuthenticatePair
)
const (
// UDPSynStart is the state where a syn will be sent.
UDPSynStart UDPFlowState = iota
// UDPSynSend is the state where a syn has been sent.
UDPSynSend
// UDPSynReceived is the state where a syn packet has been received.
UDPSynReceived
// UDPAckProcessed is the state that the negotiation has been completed.
UDPAckProcessed
// UDPSynAckReceived is the state where a syn ack packet has been received.
UDPSynAckReceived
// UDPSynAckSent is the state where syn ack packet has been sent.
UDPSynAckSent
// UDPAckReceived is the state where udp ack packet is received.
UDPAckReceived
)
// MaximumUDPQueueLen is the maximum number of UDP packets buffered.
const MaximumUDPQueueLen = 50
const (
// RejectReported represents that flow was reported as rejected
RejectReported bool = true
// AcceptReported represents that flow was reported as accepted
AcceptReported bool = false
)
// AuthInfo keeps authentication information about a connection
type AuthInfo struct {
LocalContext []byte
RemoteContext []byte
RemoteContextID string
RemotePublicKey interface{}
RemoteIP string
RemotePort string
LocalServiceContext []byte
RemoteServiceContext []byte
}
// TCPConnection is information regarding TCP Connection
type TCPConnection struct {
sync.RWMutex
state TCPFlowState
Auth AuthInfo
// Debugging Information
flowReported int
// ServiceData allows services to associate state with a connection
ServiceData interface{}
// Context is the pucontext.PUContext that is associated with this connection
// Minimizes the number of caches and lookups
Context *pucontext.PUContext
// TimeOut signals the timeout to be used by the state machines
TimeOut time.Duration
// Debugging information - pushed to the end for compact structure
flowLastReporting bool
// ServiceConnection indicates that this connection is handled by a service
ServiceConnection bool
// ReportFlowPolicy holds the last matched observed policy
ReportFlowPolicy *policy.FlowPolicy
// PacketFlowPolicy holds the last matched actual policy
PacketFlowPolicy *policy.FlowPolicy
}
// TCPConnectionExpirationNotifier handles processing the expiration of an element
func TCPConnectionExpirationNotifier(c cache.DataStore, id interface{}, item interface{}) {
if conn, ok := item.(*TCPConnection); ok {
conn.Cleanup(true)
}
}
// String returns a printable version of connection
func (c *TCPConnection) String() string {
return fmt.Sprintf("state:%d auth: %+v", c.state, c.Auth)
}
// GetState is used to return the state
func (c *TCPConnection) GetState() TCPFlowState {
return c.state
}
// SetState is used to setup the state for the TCP connection
func (c *TCPConnection) SetState(state TCPFlowState) {
c.state = state
}
// SetReported is used to track if a flow is reported
func (c *TCPConnection) SetReported(flowState bool) {
c.flowReported++
if c.flowReported > 1 && c.flowLastReporting != flowState {
zap.L().Info("Connection reported multiple times",
zap.Int("report count", c.flowReported),
zap.Bool("previous", c.flowLastReporting),
zap.Bool("next", flowState),
)
}
c.flowLastReporting = flowState
}
// Cleanup will provide information when a connection is removed by a timer.
func (c *TCPConnection) Cleanup(expiration bool) {
// Logging information
if c.flowReported == 0 {
zap.L().Error("Connection not reported",
zap.String("connection", c.String()))
}
}
// NewTCPConnection returns a TCPConnection information struct
func NewTCPConnection(context *pucontext.PUContext) *TCPConnection {
nonce, err := crypto.GenerateRandomBytes(16)
if err != nil {
return nil
}
return &TCPConnection{
state: TCPSynSend,
Context: context,
Auth: AuthInfo{
LocalContext: nonce,
},
}
}
// ProxyConnection is a record to keep state of proxy auth
type ProxyConnection struct {
sync.Mutex
state ProxyConnState
Auth AuthInfo
ReportFlowPolicy *policy.FlowPolicy
PacketFlowPolicy *policy.FlowPolicy
reported bool
}
// NewProxyConnection returns a new Proxy Connection
func NewProxyConnection() *ProxyConnection {
nonce, err := crypto.GenerateRandomBytes(16)
if err != nil {
return nil
}
return &ProxyConnection{
state: ClientTokenSend,
Auth: AuthInfo{
LocalContext: nonce,
},
}
}
// GetState returns the state of a proxy connection
func (c *ProxyConnection) GetState() ProxyConnState {
return c.state
}
// SetState is used to setup the state for the Proxy Connection
func (c *ProxyConnection) SetState(state ProxyConnState) {
c.state = state
}
// SetReported sets the flag to reported when the conn is reported
func (c *ProxyConnection) SetReported(reported bool) {
c.reported = reported
}
// UDPConnection is information regarding UDP connection.
type UDPConnection struct {
sync.RWMutex
state UDPFlowState
Context *pucontext.PUContext
Auth AuthInfo
// Debugging Information
flowReported int
ReportFlowPolicy *policy.FlowPolicy
PacketFlowPolicy *policy.FlowPolicy
// ServiceData allows services to associate state with a connection
ServiceData interface{}
// PacketQueue indicates app UDP packets queued while authorization is in progress.
PacketQueue []*packet.Packet
Writer afinetrawsocket.SocketWriter
// Debugging information - pushed to the end for compact structure
flowLastReporting bool
reported bool
// ServiceConnection indicates that this connection is handled by a service
ServiceConnection bool
}
// NewUDPConnection returns UDPConnection struct.
func NewUDPConnection(context *pucontext.PUContext, writer afinetrawsocket.SocketWriter) *UDPConnection {
nonce, err := crypto.GenerateRandomBytes(16)
if err != nil {
return nil
}
return &UDPConnection{
state: UDPSynStart,
Context: context,
PacketQueue: []*packet.Packet{},
Writer: writer,
Auth: AuthInfo{
LocalContext: nonce,
},
}
}
// GetState is used to get state of UDP Connection.
func (c *UDPConnection) GetState() UDPFlowState {
return c.state
}
// SetState is used to setup the state for the UDP Connection.
func (c *UDPConnection) SetState(state UDPFlowState) {
c.state = state
}
// QueuePackets queues UDP packets till the flow is authenticated.
func (c *UDPConnection) QueuePackets(udpPacket *packet.Packet) (err error) {
qlen := len(c.PacketQueue)
// only queue first 50 packets.
if qlen > MaximumUDPQueueLen {
zap.L().Info("Reached Maximum queue length, Dropping packet", zap.String("flow", udpPacket.L4FlowHash()))
return nil
}
buffer := make([]byte, len(udpPacket.Buffer))
copy(buffer, udpPacket.Buffer)
copyPacket, err := packet.New(packet.PacketTypeApplication, buffer, udpPacket.Mark, true)
if err != nil {
return fmt.Errorf("Unable to copy packets to queue:%s", err)
}
c.PacketQueue = append(c.PacketQueue, copyPacket)
return nil
}
// DropPackets drops packets on errors during Authorization.
func (c *UDPConnection) DropPackets() {
c.PacketQueue = []*packet.Packet{}
}
// SetReported is used to track if a flow is reported
func (c *UDPConnection) SetReported(flowState bool) {
c.flowReported++
if c.flowReported > 1 && c.flowLastReporting != flowState {
zap.L().Info("Connection reported multiple times",
zap.Int("report count", c.flowReported),
zap.Bool("previous", c.flowLastReporting),
zap.Bool("next", flowState),
)
}
c.flowLastReporting = flowState
}