-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
464 lines (391 loc) · 13.9 KB
/
tcp.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
package main
import (
"fmt"
"io"
"net"
"strings"
"sync/atomic"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
// TCP connection state
type TCPState int
const (
StateInit TCPState = iota + 1
StateSynchronizing // means we have received a SYN and replied with a SYN+ACK, awaiting ACK from other side
StateConnected // means we have received at least one ACK from other side so can send and receive data
StateOtherSideFinished // means we have received a FIN from other side and responded with FIN+ACK
StateFinished // means we have sent our own FIN and must not send any more data
)
func (s TCPState) String() string {
switch s {
case StateInit:
return "StateInit"
case StateSynchronizing:
return "StateSynchronizing"
case StateConnected:
return "StateConnected"
case StateOtherSideFinished:
return "StateOtherSideFinished"
case StateFinished:
return "StateFinished"
default:
return fmt.Sprintf("unknown(%d)", s)
}
}
// TCP stream
type tcpStream struct {
subprocess AddrPort // address from which we originally intercepted packets generated by subprocess
world AddrPort // address to which the intercepted packets were addressed
fromSubprocess chan []byte // the stack sends packets here, we receives
toSubprocess chan []byte // we send packets here, the stack receives
serializeBuf gopacket.SerializeBuffer // used to serialize gopacket structs to wire format
state TCPState // state of the connection
seq uint32 // sequence number for packets going to the subprocess
ack uint32 // the next acknowledgement number to send
}
func newTCPStream(world AddrPort, subprocess AddrPort, out chan []byte) *tcpStream {
return &tcpStream{
world: world,
subprocess: subprocess,
state: StateInit,
fromSubprocess: make(chan []byte, 1024),
toSubprocess: out,
serializeBuf: gopacket.NewSerializeBuffer(),
}
}
// for net.Conn interface
func (s *tcpStream) SetDeadline(t time.Time) error {
verbose("SetDeadline not implemented for TCP streams, ignoring")
return nil
}
// for net.Conn interface
func (s *tcpStream) SetReadDeadline(t time.Time) error {
verbose("SetReadDeadline not implemented for TCP streams, ignoring")
return nil
}
// for net.Conn interface
func (s *tcpStream) SetWriteDeadline(t time.Time) error {
verbose("SetWriteDeadline not implemented for TCP streams, ignoring")
return nil
}
// for net.Conn interface
func (s *tcpStream) LocalAddr() net.Addr {
return &net.TCPAddr{
IP: s.world.Addr,
Port: int(s.world.Port),
}
}
// for net.Conn interface
func (s *tcpStream) RemoteAddr() net.Addr {
return &net.TCPAddr{
IP: s.subprocess.Addr,
Port: int(s.subprocess.Port),
}
}
// Read reads packets sent by the subprocess and intercepted by us
func (s *tcpStream) Read(buf []byte) (int, error) {
// read packets from the channel until we get a non-empty one
for packet := range s.fromSubprocess {
if len(packet) == 0 {
continue // we must not return zero bytes according to io.Reader interface
}
if len(packet) > len(buf) {
// TODO: deliver first part of this packet, store the rest for next Read()
return 0, fmt.Errorf("received packet with %d bytes but receive buffer only has size %d", len(packet), len(buf))
}
// copy bytes into the buffer and return
return copy(buf, packet), nil
}
// if we fell through then the channel is closed
return 0, io.EOF
}
// Write writes payloads to the subprocess as if they came from the address that the subprocess
// was trying to reach when this stream was first intercepted.
func (s *tcpStream) Write(payload []byte) (int, error) {
sz := uint32(len(payload))
replytcp := layers.TCP{
SrcPort: layers.TCPPort(s.world.Port),
DstPort: layers.TCPPort(s.subprocess.Port),
Seq: atomic.AddUint32(&s.seq, sz) - sz, // sequence number on our side
Ack: atomic.LoadUint32(&s.ack), // laste sequence number we saw on their side
ACK: true, // this indicates that we are acknolwedging some bytes
Window: 64240, // number of bytes we are willing to receive (copied from sender)
}
replyipv4 := layers.IPv4{
Version: 4, // indicates IPv4
TTL: ttl,
Protocol: layers.IPProtocolTCP,
SrcIP: s.world.Addr,
DstIP: s.subprocess.Addr,
}
err := replytcp.SetNetworkLayerForChecksum(&replyipv4)
if err != nil {
return 0, fmt.Errorf("error setting network-layer TCP checksums: %w", err)
}
// log
verbosef("sending tcp packet to subprocess: %s", summarizeTCP(&replyipv4, &replytcp, payload))
// serialize the data
packet, err := serializeTCP(&replyipv4, &replytcp, payload, s.serializeBuf)
if err != nil {
return 0, fmt.Errorf("error serializing TCP packet: %w", err)
}
// make a copy because the same buffer will be re-used
cp := make([]byte, len(packet))
copy(cp, packet)
// send to the subprocess channel non-blocking
select {
case s.toSubprocess <- cp:
default:
return 0, fmt.Errorf("channel would have blocked")
}
// return number of bytes sent to us, not number of bytes written to underlying network
return len(payload), nil
}
// Close the connection by sending a FIN packet
func (s *tcpStream) Close() error {
switch s.state {
case StateInit:
errorf("application tried tp close a TCP stream in state %v, returning error", s.state)
return fmt.Errorf("cannot close TCP stream in state %v", s.state)
case StateFinished:
verbosef("application tried tp close a TCP stream in state %v, ignoring", s.state)
return nil
}
// TODO: s.state should be protected with a mutex
s.state = StateFinished
seq := atomic.AddUint32(&s.seq, 1) - 1
// make a FIN packet to send to the subprocess
tcp := layers.TCP{
SrcPort: layers.TCPPort(s.world.Port),
DstPort: layers.TCPPort(s.subprocess.Port),
FIN: true,
ACK: true,
Seq: seq,
Ack: atomic.LoadUint32(&s.ack),
Window: 64240, // number of bytes we are willing to receive (copied from sender)
}
ipv4 := layers.IPv4{
Version: 4, // indicates IPv4
TTL: ttl,
Protocol: layers.IPProtocolTCP,
SrcIP: s.world.Addr,
DstIP: s.subprocess.Addr,
}
tcp.SetNetworkLayerForChecksum(&ipv4)
// log
verbosef("sending FIN to subprocess: %s", summarizeTCP(&ipv4, &tcp, nil))
// serialize the packet (TODO: guard serializeBuf with a mutex)
serialized, err := serializeTCP(&ipv4, &tcp, nil, s.serializeBuf)
if err != nil {
errorf("error serializing reply TCP: %v, dropping", err)
return fmt.Errorf("error serializing reply TCP: %w, dropping", err)
}
// make a copy of the data
cp := make([]byte, len(serialized))
copy(cp, serialized)
// send to the channel that goes to the subprocess
select {
case s.toSubprocess <- cp:
default:
verbosef("channel for sending to subprocess would have blocked, dropping %d bytes", len(cp))
}
return nil
}
func (s *tcpStream) deliverToApplication(payload []byte) {
// copy the payload because it may be overwritten before the write loop gets to it
cp := make([]byte, len(payload))
copy(cp, payload)
verbosef("stream enqueing %d bytes to send to world", len(payload))
// send to channel unless it would block
select {
case s.fromSubprocess <- cp:
default:
verbosef("channel to world would block, dropping %d bytes", len(payload))
}
}
// tcpStack accepts raw packets and handles TCP connections
type tcpStack struct {
streamsBySrcDst map[string]*tcpStream
toSubprocess chan []byte // data sent to this channel goes to subprocess as raw IPv4 packet
app *mux
}
func newTCPStack(app *mux, link chan []byte) *tcpStack {
return &tcpStack{
streamsBySrcDst: make(map[string]*tcpStream),
toSubprocess: link,
app: app,
}
}
func (s *tcpStack) handlePacket(ipv4 *layers.IPv4, tcp *layers.TCP, payload []byte) {
// it happens that a process will connect to the same remote service multiple times in from
// different source ports so we must key by a descriptor that includes both endpoints
dst := AddrPort{Addr: ipv4.DstIP, Port: uint16(tcp.DstPort)}
src := AddrPort{Addr: ipv4.SrcIP, Port: uint16(tcp.SrcPort)}
// put source address, source port, destination address, and destination port into a human-readable string
srcdst := src.String() + " => " + dst.String()
stream, found := s.streamsBySrcDst[srcdst]
if !found {
// create a new stream no matter what kind of packet this is
// later we will reject everything other than SYN packets sent to a fresh stream
stream = newTCPStream(dst, src, s.toSubprocess)
s.streamsBySrcDst[srcdst] = stream
s.app.notifyTCP(stream)
}
// handle connection establishment
if tcp.SYN && stream.state == StateInit {
stream.state = StateSynchronizing
seq := atomic.AddUint32(&stream.seq, 1) - 1
atomic.StoreUint32(&stream.ack, tcp.Seq+1)
verbosef("got SYN to %v:%v, now state is %v", ipv4.DstIP, tcp.DstPort, stream.state)
// reply to the subprocess as if the connection were already good to go
replytcp := layers.TCP{
SrcPort: tcp.DstPort,
DstPort: tcp.SrcPort,
SYN: true,
ACK: true,
Seq: seq,
Ack: tcp.Seq + 1,
Window: 64240, // number of bytes we are willing to receive (copied from sender)
}
replyipv4 := layers.IPv4{
Version: 4, // indicates IPv4
TTL: ttl,
Protocol: layers.IPProtocolTCP,
SrcIP: ipv4.DstIP,
DstIP: ipv4.SrcIP,
}
replytcp.SetNetworkLayerForChecksum(&replyipv4)
// log
verbosef("sending SYN+ACK to subprocess: %s", summarizeTCP(&replyipv4, &replytcp, nil))
// serialize the packet
serialized, err := serializeTCP(&replyipv4, &replytcp, nil, stream.serializeBuf)
if err != nil {
errorf("error serializing reply TCP: %v, dropping", err)
return
}
// make a copy of the data
cp := make([]byte, len(serialized))
copy(cp, serialized)
// send to the channel that goes to the subprocess
select {
case s.toSubprocess <- cp:
default:
verbosef("channel for sending to subprocess would have blocked, dropping %d bytes", len(cp))
}
}
// handle connection teardown
if tcp.FIN && stream.state != StateInit {
// according to the tcp spec we are always allowed to ack the other side's FIN, even if we have
// already sent our own FIN
stream.state = StateOtherSideFinished
seq := atomic.AddUint32(&stream.seq, 1) - 1
atomic.StoreUint32(&stream.ack, tcp.Seq+1)
verbosef("got FIN to %v:%v, now state is %v", ipv4.DstIP, tcp.DstPort, stream.state)
// make a FIN+ACK reply to send to the subprocess
replytcp := layers.TCP{
SrcPort: tcp.DstPort,
DstPort: tcp.SrcPort,
FIN: true,
ACK: true,
Seq: seq,
Ack: tcp.Seq + 1,
Window: 64240, // number of bytes we are willing to receive (copied from sender)
}
replyipv4 := layers.IPv4{
Version: 4, // indicates IPv4
TTL: ttl,
Protocol: layers.IPProtocolTCP,
SrcIP: ipv4.DstIP,
DstIP: ipv4.SrcIP,
}
replytcp.SetNetworkLayerForChecksum(&replyipv4)
// log
verbosef("sending FIN+ACK to subprocess: %s", summarizeTCP(&replyipv4, &replytcp, nil))
// serialize the packet
serialized, err := serializeTCP(&replyipv4, &replytcp, nil, stream.serializeBuf)
if err != nil {
errorf("error serializing reply TCP: %v, dropping", err)
return
}
// make a copy of the data
cp := make([]byte, len(serialized))
copy(cp, serialized)
// send to the channel that goes to the subprocess
select {
case s.toSubprocess <- cp:
default:
verbosef("channel for sending to subprocess would have blocked, dropping %d bytes", len(cp))
}
}
if tcp.ACK && stream.state == StateSynchronizing {
stream.state = StateConnected
verbosef("got ACK to %v:%v, now state is %v", ipv4.DstIP, tcp.DstPort, stream.state)
// nothing more to do here -- if there is a payload then it will be forwarded
// to the subprocess in the block below
}
// payload packets will often have ACK set, which acknowledges previously sent bytes
if !tcp.SYN && len(tcp.Payload) > 0 && stream.state == StateConnected {
verbosef("got %d tcp bytes to %v:%v, forwarding to application", len(tcp.Payload), ipv4.DstIP, tcp.DstPort)
// update TCP sequence number -- this is not an increment but an overwrite, so no race condition here
atomic.StoreUint32(&stream.ack, tcp.Seq+uint32(len(tcp.Payload)))
// deliver the payload to application-level listeners
stream.deliverToApplication(tcp.Payload)
}
}
// serializeTCP serializes a TCP packet
func serializeTCP(ipv4 *layers.IPv4, tcp *layers.TCP, payload []byte, tmp gopacket.SerializeBuffer) ([]byte, error) {
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
tmp.Clear()
// each layer is *prepended*, treating the current buffer data as payload
p, err := tmp.AppendBytes(len(payload))
if err != nil {
return nil, fmt.Errorf("error appending TCP payload to packet (%d bytes): %w", len(payload), err)
}
copy(p, payload)
err = tcp.SerializeTo(tmp, opts)
if err != nil {
return nil, fmt.Errorf("error serializing TCP part of packet: %w", err)
}
err = ipv4.SerializeTo(tmp, opts)
if err != nil {
errorf("error serializing IP part of packet: %v", err)
}
return tmp.Bytes(), nil
}
// summarizeTCP summarizes a TCP packet into a single line for logging
func summarizeTCP(ipv4 *layers.IPv4, tcp *layers.TCP, payload []byte) string {
var flags []string
if tcp.FIN {
flags = append(flags, "FIN")
}
if tcp.SYN {
flags = append(flags, "SYN")
}
if tcp.RST {
flags = append(flags, "RST")
}
if tcp.ACK {
flags = append(flags, "ACK")
}
if tcp.URG {
flags = append(flags, "URG")
}
if tcp.ECE {
flags = append(flags, "ECE")
}
if tcp.CWR {
flags = append(flags, "CWR")
}
if tcp.NS {
flags = append(flags, "NS")
}
// ignore PSH flag
flagstr := strings.Join(flags, "+")
return fmt.Sprintf("TCP %v:%d => %v:%d %s - Seq %d - Ack %d - Len %d",
ipv4.SrcIP, tcp.SrcPort, ipv4.DstIP, tcp.DstPort, flagstr, tcp.Seq, tcp.Ack, len(tcp.Payload))
}