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
|
// Copyright 2020 The gVisor Authors.
//
// 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.
package stack
import (
"encoding/binary"
"sync"
"time"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/buffer"
"gvisor.dev/gvisor/pkg/tcpip/hash/jenkins"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/transport/tcpconntrack"
)
// Connection tracking is used to track and manipulate packets for NAT rules.
// The connection is created for a packet if it does not exist. Every connection
// contains two tuples (original and reply). The tuples are manipulated if there
// is a matching NAT rule. The packet is modified by looking at the tuples in the
// Prerouting and Output hooks.
// Direction of the tuple.
type ctDirection int
const (
dirOriginal ctDirection = iota
dirReply
)
// Status of connection.
// TODO(gvisor.dev/issue/170): Add other states of connection.
type connStatus int
const (
connNew connStatus = iota
connEstablished
)
// Manipulation type for the connection.
type manipType int
const (
manipDstPrerouting manipType = iota
manipDstOutput
)
// connTrackMutable is the manipulatable part of the tuple.
type connTrackMutable struct {
// addr is source address of the tuple.
addr tcpip.Address
// port is source port of the tuple.
port uint16
// protocol is network layer protocol.
protocol tcpip.NetworkProtocolNumber
}
// connTrackImmutable is the non-manipulatable part of the tuple.
type connTrackImmutable struct {
// addr is destination address of the tuple.
addr tcpip.Address
// direction is direction (original or reply) of the tuple.
direction ctDirection
// port is destination port of the tuple.
port uint16
// protocol is transport layer protocol.
protocol tcpip.TransportProtocolNumber
}
// connTrackTuple represents the tuple which is created from the
// packet.
type connTrackTuple struct {
// dst is non-manipulatable part of the tuple.
dst connTrackImmutable
// src is manipulatable part of the tuple.
src connTrackMutable
}
// connTrackTupleHolder is the container of tuple and connection.
type ConnTrackTupleHolder struct {
// conn is pointer to the connection tracking entry.
conn *connTrack
// tuple is original or reply tuple.
tuple connTrackTuple
}
// connTrack is the connection.
type connTrack struct {
// originalTupleHolder contains tuple in original direction.
originalTupleHolder ConnTrackTupleHolder
// replyTupleHolder contains tuple in reply direction.
replyTupleHolder ConnTrackTupleHolder
// status indicates connection is new or established.
status connStatus
// timeout indicates the time connection should be active.
timeout time.Duration
// manip indicates if the packet should be manipulated.
manip manipType
// tcb is TCB control block. It is used to keep track of states
// of tcp connection.
tcb tcpconntrack.TCB
// tcbHook indicates if the packet is inbound or outbound to
// update the state of tcb.
tcbHook Hook
}
// ConnTrackTable contains a map of all existing connections created for
// NAT rules.
type ConnTrackTable struct {
// connMu protects connTrackTable.
connMu sync.RWMutex
// connTrackTable maintains a map of tuples needed for connection tracking
// for iptables NAT rules. The key for the map is an integer calculated
// using seed, source address, destination address, source port and
// destination port.
CtMap map[uint32]ConnTrackTupleHolder
// seed is a one-time random value initialized at stack startup
// and is used in calculation of hash key for connection tracking
// table.
Seed uint32
}
// parseHeaders sets headers in the packet.
func parseHeaders(pkt *PacketBuffer) {
newPkt := pkt.Clone()
// Set network header.
hdr, ok := newPkt.Data.PullUp(header.IPv4MinimumSize)
if !ok {
return
}
netHeader := header.IPv4(hdr)
newPkt.NetworkHeader = hdr
length := int(netHeader.HeaderLength())
// TODO(gvisor.dev/issue/170): Need to support for other
// protocols as well.
// Set transport header.
switch protocol := netHeader.TransportProtocol(); protocol {
case header.UDPProtocolNumber:
if newPkt.TransportHeader == nil {
h, ok := newPkt.Data.PullUp(length + header.UDPMinimumSize)
if !ok {
return
}
newPkt.TransportHeader = buffer.View(header.UDP(h[length:]))
}
case header.TCPProtocolNumber:
if newPkt.TransportHeader == nil {
h, ok := newPkt.Data.PullUp(length + header.TCPMinimumSize)
if !ok {
return
}
newPkt.TransportHeader = buffer.View(header.TCP(h[length:]))
}
}
pkt.NetworkHeader = newPkt.NetworkHeader
pkt.TransportHeader = newPkt.TransportHeader
}
// packetToTuple converts packet to a tuple in original direction.
func packetToTuple(pkt PacketBuffer, hook Hook) (connTrackTuple, *tcpip.Error) {
var tuple connTrackTuple
netHeader := header.IPv4(pkt.NetworkHeader)
// TODO(gvisor.dev/issue/170): Need to support for other
// protocols as well.
if netHeader == nil || netHeader.TransportProtocol() != header.TCPProtocolNumber {
return tuple, tcpip.ErrUnknownProtocol
}
tcpHeader := header.TCP(pkt.TransportHeader)
if tcpHeader == nil {
return tuple, tcpip.ErrUnknownProtocol
}
tuple.src.addr = netHeader.SourceAddress()
tuple.src.port = tcpHeader.SourcePort()
tuple.src.protocol = header.IPv4ProtocolNumber
tuple.dst.addr = netHeader.DestinationAddress()
tuple.dst.port = tcpHeader.DestinationPort()
tuple.dst.protocol = netHeader.TransportProtocol()
return tuple, nil
}
// getReplyTuple creates reply tuple for the given tuple.
func getReplyTuple(tuple connTrackTuple) connTrackTuple {
var replyTuple connTrackTuple
replyTuple.src.addr = tuple.dst.addr
replyTuple.src.port = tuple.dst.port
replyTuple.src.protocol = tuple.src.protocol
replyTuple.dst.addr = tuple.src.addr
replyTuple.dst.port = tuple.src.port
replyTuple.dst.protocol = tuple.dst.protocol
replyTuple.dst.direction = dirReply
return replyTuple
}
// makeNewConn creates new connection.
func makeNewConn(tuple, replyTuple connTrackTuple) connTrack {
var conn connTrack
conn.status = connNew
conn.originalTupleHolder.tuple = tuple
conn.originalTupleHolder.conn = &conn
conn.replyTupleHolder.tuple = replyTuple
conn.replyTupleHolder.conn = &conn
return conn
}
// getTupleHash returns hash of the tuple. The fields used for
// generating hash are seed (generated once for stack), source address,
// destination address, source port and destination ports.
func (ct *ConnTrackTable) getTupleHash(tuple connTrackTuple) uint32 {
h := jenkins.Sum32(ct.Seed)
h.Write([]byte(tuple.src.addr))
h.Write([]byte(tuple.dst.addr))
portBuf := make([]byte, 2)
binary.LittleEndian.PutUint16(portBuf, tuple.src.port)
h.Write([]byte(portBuf))
binary.LittleEndian.PutUint16(portBuf, tuple.dst.port)
h.Write([]byte(portBuf))
return h.Sum32()
}
// connTrackForPacket returns connTrack for packet.
// TODO(gvisor.dev/issue/170): Only TCP packets are supported. Need to support other
// transport protocols.
func (ct *ConnTrackTable) connTrackForPacket(pkt *PacketBuffer, hook Hook, createConn bool) (*connTrack, ctDirection) {
if hook == Prerouting {
// Headers will not be set in Prerouting.
// TODO(gvisor.dev/issue/170): Change this after parsing headers
// code is added.
parseHeaders(pkt)
}
var dir ctDirection
tuple, err := packetToTuple(*pkt, hook)
if err != nil {
return nil, dir
}
ct.connMu.Lock()
defer ct.connMu.Unlock()
connTrackTable := ct.CtMap
hash := ct.getTupleHash(tuple)
var conn *connTrack
switch createConn {
case true:
// If connection does not exist for the hash, create a new
// connection.
replyTuple := getReplyTuple(tuple)
replyHash := ct.getTupleHash(replyTuple)
newConn := makeNewConn(tuple, replyTuple)
conn = &newConn
// Add tupleHolders to the map.
// TODO(gvisor.dev/issue/170): Need to support collisions using linked list.
ct.CtMap[hash] = conn.originalTupleHolder
ct.CtMap[replyHash] = conn.replyTupleHolder
default:
tupleHolder, ok := connTrackTable[hash]
if !ok {
return nil, dir
}
// If this is the reply of new connection, set the connection
// status as ESTABLISHED.
conn = tupleHolder.conn
if conn.status == connNew && tupleHolder.tuple.dst.direction == dirReply {
conn.status = connEstablished
}
if tupleHolder.conn == nil {
panic("tupleHolder has null connection tracking entry")
}
dir = tupleHolder.tuple.dst.direction
}
return conn, dir
}
// SetNatInfo will manipulate the tuples according to iptables NAT rules.
func (ct *ConnTrackTable) SetNatInfo(pkt *PacketBuffer, rt RedirectTarget, hook Hook) {
// Get the connection. Connection is always created before this
// function is called.
conn, _ := ct.connTrackForPacket(pkt, hook, false)
if conn == nil {
panic("connection should be created to manipulate tuples.")
}
replyTuple := conn.replyTupleHolder.tuple
replyHash := ct.getTupleHash(replyTuple)
// TODO(gvisor.dev/issue/170): Support only redirect of ports. Need to
// support changing of address for Prerouting.
// Change the port as per the iptables rule. This tuple will be used
// to manipulate the packet in HandlePacket.
conn.replyTupleHolder.tuple.src.addr = rt.MinIP
conn.replyTupleHolder.tuple.src.port = rt.MinPort
newHash := ct.getTupleHash(conn.replyTupleHolder.tuple)
// Add the changed tuple to the map.
ct.connMu.Lock()
defer ct.connMu.Unlock()
ct.CtMap[newHash] = conn.replyTupleHolder
if hook == Output {
conn.replyTupleHolder.conn.manip = manipDstOutput
}
// Delete the old tuple.
delete(ct.CtMap, replyHash)
}
// handlePacketPrerouting manipulates ports for packets in Prerouting hook.
// TODO(gvisor.dev/issue/170): Change address for Prerouting hook..
func handlePacketPrerouting(pkt *PacketBuffer, conn *connTrack, dir ctDirection) {
netHeader := header.IPv4(pkt.NetworkHeader)
tcpHeader := header.TCP(pkt.TransportHeader)
// For prerouting redirection, packets going in the original direction
// have their destinations modified and replies have their sources
// modified.
switch dir {
case dirOriginal:
port := conn.replyTupleHolder.tuple.src.port
tcpHeader.SetDestinationPort(port)
netHeader.SetDestinationAddress(conn.replyTupleHolder.tuple.src.addr)
case dirReply:
port := conn.originalTupleHolder.tuple.dst.port
tcpHeader.SetSourcePort(port)
netHeader.SetSourceAddress(conn.originalTupleHolder.tuple.dst.addr)
}
netHeader.SetChecksum(0)
netHeader.SetChecksum(^netHeader.CalculateChecksum())
}
// handlePacketOutput manipulates ports for packets in Output hook.
func handlePacketOutput(pkt *PacketBuffer, conn *connTrack, gso *GSO, r *Route, dir ctDirection) {
netHeader := header.IPv4(pkt.NetworkHeader)
tcpHeader := header.TCP(pkt.TransportHeader)
// For output redirection, packets going in the original direction
// have their destinations modified and replies have their sources
// modified. For prerouting redirection, we only reach this point
// when replying, so packet sources are modified.
if conn.manip == manipDstOutput && dir == dirOriginal {
port := conn.replyTupleHolder.tuple.src.port
tcpHeader.SetDestinationPort(port)
netHeader.SetDestinationAddress(conn.replyTupleHolder.tuple.src.addr)
} else {
port := conn.originalTupleHolder.tuple.dst.port
tcpHeader.SetSourcePort(port)
netHeader.SetSourceAddress(conn.originalTupleHolder.tuple.dst.addr)
}
// Calculate the TCP checksum and set it.
tcpHeader.SetChecksum(0)
hdr := &pkt.Header
length := uint16(pkt.Data.Size()+hdr.UsedLength()) - uint16(netHeader.HeaderLength())
xsum := r.PseudoHeaderChecksum(header.TCPProtocolNumber, length)
if gso != nil && gso.NeedsCsum {
tcpHeader.SetChecksum(xsum)
} else if r.Capabilities()&CapabilityTXChecksumOffload == 0 {
xsum = header.ChecksumVVWithOffset(pkt.Data, xsum, int(tcpHeader.DataOffset()), pkt.Data.Size())
tcpHeader.SetChecksum(^tcpHeader.CalculateChecksum(xsum))
}
netHeader.SetChecksum(0)
netHeader.SetChecksum(^netHeader.CalculateChecksum())
}
// HandlePacket will manipulate the port and address of the packet if the
// connection exists.
func (ct *ConnTrackTable) HandlePacket(pkt *PacketBuffer, hook Hook, gso *GSO, r *Route) {
if pkt.NatDone {
return
}
if hook != Prerouting && hook != Output {
return
}
conn, dir := ct.connTrackForPacket(pkt, hook, false)
// Connection or Rule not found for the packet.
if conn == nil {
return
}
netHeader := header.IPv4(pkt.NetworkHeader)
// TODO(gvisor.dev/issue/170): Need to support for other transport
// protocols as well.
if netHeader == nil || netHeader.TransportProtocol() != header.TCPProtocolNumber {
return
}
tcpHeader := header.TCP(pkt.TransportHeader)
if tcpHeader == nil {
return
}
switch hook {
case Prerouting:
handlePacketPrerouting(pkt, conn, dir)
case Output:
handlePacketOutput(pkt, conn, gso, r, dir)
}
pkt.NatDone = true
// Update the state of tcb.
// TODO(gvisor.dev/issue/170): Add support in tcpcontrack to handle
// other tcp states.
var st tcpconntrack.Result
if conn.tcb.IsEmpty() {
conn.tcb.Init(tcpHeader)
conn.tcbHook = hook
} else {
switch hook {
case conn.tcbHook:
st = conn.tcb.UpdateStateOutbound(tcpHeader)
default:
st = conn.tcb.UpdateStateInbound(tcpHeader)
}
}
// Delete conntrack if tcp connection is closed.
if st == tcpconntrack.ResultClosedByPeer || st == tcpconntrack.ResultClosedBySelf || st == tcpconntrack.ResultReset {
ct.deleteConnTrack(conn)
}
}
// deleteConnTrack deletes the connection.
func (ct *ConnTrackTable) deleteConnTrack(conn *connTrack) {
if conn == nil {
return
}
tuple := conn.originalTupleHolder.tuple
hash := ct.getTupleHash(tuple)
replyTuple := conn.replyTupleHolder.tuple
replyHash := ct.getTupleHash(replyTuple)
ct.connMu.Lock()
defer ct.connMu.Unlock()
delete(ct.CtMap, hash)
delete(ct.CtMap, replyHash)
}
|