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
|
// 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 (
"sync"
"gvisor.dev/gvisor/pkg/tcpip"
"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.
//
// Currently, only TCP tracking is supported.
// Direction of the tuple.
type direction int
const (
dirOriginal direction = iota
dirReply
)
// Manipulation type for the connection.
type manipType int
const (
manipDstPrerouting manipType = iota
manipDstOutput
)
// tuple holds a connection's identifying and manipulating data in one
// direction. It is immutable.
type tuple struct {
tupleID
// conn is the connection tracking entry this tuple belongs to.
conn *conn
// direction is the direction of the tuple.
direction direction
}
// tupleID uniquely identifies a connection in one direction. It currently
// contains enough information to distinguish between any TCP or UDP
// connection, and will need to be extended to support other protocols.
type tupleID struct {
srcAddr tcpip.Address
srcPort uint16
dstAddr tcpip.Address
dstPort uint16
transProto tcpip.TransportProtocolNumber
netProto tcpip.NetworkProtocolNumber
}
// reply creates the reply tupleID.
func (ti tupleID) reply() tupleID {
return tupleID{
srcAddr: ti.dstAddr,
srcPort: ti.dstPort,
dstAddr: ti.srcAddr,
dstPort: ti.srcPort,
transProto: ti.transProto,
netProto: ti.netProto,
}
}
// conn is a tracked connection.
type conn struct {
// original is the tuple in original direction. It is immutable.
original tuple
// reply is the tuple in reply direction. It is immutable.
reply tuple
// manip indicates if the packet should be manipulated. It is immutable.
manip manipType
// tcbHook indicates if the packet is inbound or outbound to
// update the state of tcb. It is immutable.
tcbHook Hook
// mu protects tcb.
mu sync.Mutex
// tcb is TCB control block. It is used to keep track of states
// of tcp connection and is protected by mu.
tcb tcpconntrack.TCB
}
// ConnTrack tracks all connections created for NAT rules. Most users are
// expected to only call handlePacket and createConnFor.
type ConnTrack struct {
// mu protects conns.
mu sync.RWMutex
// conns maintains a map of tuples needed for connection tracking for
// iptables NAT rules. It is protected by mu.
conns map[tupleID]tuple
}
// packetToTupleID converts packet to a tuple ID. It fails when pkt lacks a valid
// TCP header.
func packetToTupleID(pkt *PacketBuffer) (tupleID, *tcpip.Error) {
// TODO(gvisor.dev/issue/170): Need to support for other
// protocols as well.
netHeader := header.IPv4(pkt.NetworkHeader)
if netHeader == nil || netHeader.TransportProtocol() != header.TCPProtocolNumber {
return tupleID{}, tcpip.ErrUnknownProtocol
}
tcpHeader := header.TCP(pkt.TransportHeader)
if tcpHeader == nil {
return tupleID{}, tcpip.ErrUnknownProtocol
}
return tupleID{
srcAddr: netHeader.SourceAddress(),
srcPort: tcpHeader.SourcePort(),
dstAddr: netHeader.DestinationAddress(),
dstPort: tcpHeader.DestinationPort(),
transProto: netHeader.TransportProtocol(),
netProto: header.IPv4ProtocolNumber,
}, nil
}
// newConn creates new connection.
func newConn(orig, reply tupleID, manip manipType, hook Hook) *conn {
conn := conn{
manip: manip,
tcbHook: hook,
}
conn.original = tuple{conn: &conn, tupleID: orig}
conn.reply = tuple{conn: &conn, tupleID: reply, direction: dirReply}
return &conn
}
// connFor gets the conn for pkt if it exists, or returns nil
// if it does not. It returns an error when pkt does not contain a valid TCP
// header.
// TODO(gvisor.dev/issue/170): Only TCP packets are supported. Need to support
// other transport protocols.
func (ct *ConnTrack) connFor(pkt *PacketBuffer) (*conn, direction) {
tid, err := packetToTupleID(pkt)
if err != nil {
return nil, dirOriginal
}
ct.mu.Lock()
defer ct.mu.Unlock()
tuple, ok := ct.conns[tid]
if !ok {
return nil, dirOriginal
}
return tuple.conn, tuple.direction
}
// createConnFor creates a new conn for pkt.
func (ct *ConnTrack) createConnFor(pkt *PacketBuffer, hook Hook, rt RedirectTarget) *conn {
tid, err := packetToTupleID(pkt)
if err != nil {
return nil
}
if hook != Prerouting && hook != Output {
return nil
}
// Create a new connection and change the port as per the iptables
// rule. This tuple will be used to manipulate the packet in
// handlePacket.
replyTID := tid.reply()
replyTID.srcAddr = rt.MinIP
replyTID.srcPort = rt.MinPort
var manip manipType
switch hook {
case Prerouting:
manip = manipDstPrerouting
case Output:
manip = manipDstOutput
}
conn := newConn(tid, replyTID, manip, hook)
// Add the changed tuple to the map.
// TODO(gvisor.dev/issue/170): Need to support collisions using linked
// list.
ct.mu.Lock()
defer ct.mu.Unlock()
ct.conns[tid] = conn.original
ct.conns[replyTID] = conn.reply
return conn
}
// handlePacketPrerouting manipulates ports for packets in Prerouting hook.
// TODO(gvisor.dev/issue/170): Change address for Prerouting hook.
func handlePacketPrerouting(pkt *PacketBuffer, conn *conn, dir direction) {
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.reply.srcPort
tcpHeader.SetDestinationPort(port)
netHeader.SetDestinationAddress(conn.reply.srcAddr)
case dirReply:
port := conn.original.dstPort
tcpHeader.SetSourcePort(port)
netHeader.SetSourceAddress(conn.original.dstAddr)
}
netHeader.SetChecksum(0)
netHeader.SetChecksum(^netHeader.CalculateChecksum())
}
// handlePacketOutput manipulates ports for packets in Output hook.
func handlePacketOutput(pkt *PacketBuffer, conn *conn, gso *GSO, r *Route, dir direction) {
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.reply.srcPort
tcpHeader.SetDestinationPort(port)
netHeader.SetDestinationAddress(conn.reply.srcAddr)
} else {
port := conn.original.dstPort
tcpHeader.SetSourcePort(port)
netHeader.SetSourceAddress(conn.original.dstAddr)
}
// 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 *ConnTrack) handlePacket(pkt *PacketBuffer, hook Hook, gso *GSO, r *Route) {
if pkt.NatDone {
return
}
if hook != Prerouting && hook != Output {
return
}
conn, dir := ct.connFor(pkt)
if conn == nil {
// Connection not found for the packet or the packet is invalid.
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.
conn.mu.Lock()
defer conn.mu.Unlock()
var st tcpconntrack.Result
tcpHeader := header.TCP(pkt.TransportHeader)
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 conn if tcp connection is closed.
if st == tcpconntrack.ResultClosedByPeer || st == tcpconntrack.ResultClosedBySelf || st == tcpconntrack.ResultReset {
ct.deleteConn(conn)
}
}
// deleteConn deletes the connection.
func (ct *ConnTrack) deleteConn(conn *conn) {
if conn == nil {
return
}
ct.mu.Lock()
defer ct.mu.Unlock()
delete(ct.conns, conn.original.tupleID)
delete(ct.conns, conn.reply.tupleID)
}
|