forked from google/gopacket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpcheck.go
246 lines (225 loc) · 5.76 KB
/
tcpcheck.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
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package reassembly
import (
"encoding/binary"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
/*
* Check TCP packet against options (window, MSS)
*/
type tcpStreamOptions struct {
mss int
scale int
receiveWindow uint
}
// TCPOptionCheck contains options for the two directions
type TCPOptionCheck struct {
options [2]tcpStreamOptions
}
func (t *TCPOptionCheck) getOptions(dir TCPFlowDirection) *tcpStreamOptions {
if dir == TCPDirClientToServer {
return &t.options[0]
}
return &t.options[1]
}
// NewTCPOptionCheck creates default options
func NewTCPOptionCheck() TCPOptionCheck {
return TCPOptionCheck{
options: [2]tcpStreamOptions{
tcpStreamOptions{
mss: 0,
scale: -1,
receiveWindow: 0,
}, tcpStreamOptions{
mss: 0,
scale: -1,
receiveWindow: 0,
},
},
}
}
// Accept checks whether the packet should be accepted by checking TCP options
func (t *TCPOptionCheck) Accept(tcp *layers.TCP, ci gopacket.CaptureInfo, dir TCPFlowDirection, nextSeq Sequence, start *bool) error {
options := t.getOptions(dir)
if tcp.SYN {
mss := -1
scale := -1
for _, o := range tcp.Options {
// MSS
if o.OptionType == 2 {
if len(o.OptionData) != 2 {
return fmt.Errorf("MSS option data length expected 2, got %d", len(o.OptionData))
}
mss = int(binary.BigEndian.Uint16(o.OptionData[:2]))
}
// Window scaling
if o.OptionType == 3 {
if len(o.OptionData) != 1 {
return fmt.Errorf("Window scaling length expected: 1, got %d", len(o.OptionData))
}
scale = int(o.OptionData[0])
}
}
options.mss = mss
options.scale = scale
} else {
if nextSeq != invalidSequence {
revOptions := t.getOptions(dir.Reverse())
length := len(tcp.Payload)
// Check packet is in the correct window
diff := nextSeq.Difference(Sequence(tcp.Seq))
if diff == -1 && (length == 1 || length == 0) {
// This is probably a Keep-alive
// TODO: check byte is ok
} else if diff < 0 {
return fmt.Errorf("Re-emitted packet (diff:%d,seq:%d,rev-ack:%d)", diff,
tcp.Seq, nextSeq)
} else if revOptions.mss > 0 && length > revOptions.mss {
return fmt.Errorf("%d > mss (%d)", length, revOptions.mss)
} else if revOptions.receiveWindow != 0 && revOptions.scale < 0 && diff > int(revOptions.receiveWindow) {
return fmt.Errorf("%d > receiveWindow(%d)", diff, revOptions.receiveWindow)
}
}
}
// Compute receiveWindow
options.receiveWindow = uint(tcp.Window)
if options.scale > 0 {
options.receiveWindow = options.receiveWindow << (uint(options.scale))
}
return nil
}
// TCPSimpleFSM implements a very simple TCP state machine
//
// Usage:
// When implementing a Stream interface and to avoid to consider packets that
// would be rejected due to client/server's TCP stack, the Accept() can call
// TCPSimpleFSM.CheckState().
//
// Limitations:
// - packet should be received in-order.
// - no check on sequence number is performed
// - no RST
type TCPSimpleFSM struct {
dir TCPFlowDirection
state int
options TCPSimpleFSMOptions
}
// TCPSimpleFSMOptions holds options for TCPSimpleFSM
type TCPSimpleFSMOptions struct {
SupportMissingEstablishment bool // Allow missing SYN, SYN+ACK, ACK
}
// Internal values of state machine
const (
TCPStateClosed = 0
TCPStateSynSent = 1
TCPStateEstablished = 2
TCPStateCloseWait = 3
TCPStateLastAck = 4
TCPStateReset = 5
)
// NewTCPSimpleFSM creates a new TCPSimpleFSM
func NewTCPSimpleFSM(options TCPSimpleFSMOptions) *TCPSimpleFSM {
return &TCPSimpleFSM{
state: TCPStateClosed,
options: options,
}
}
func (t *TCPSimpleFSM) String() string {
switch t.state {
case TCPStateClosed:
return "Closed"
case TCPStateSynSent:
return "SynSent"
case TCPStateEstablished:
return "Established"
case TCPStateCloseWait:
return "CloseWait"
case TCPStateLastAck:
return "LastAck"
case TCPStateReset:
return "Reset"
}
return "?"
}
// CheckState returns false if tcp is invalid wrt current state or update the state machine's state
func (t *TCPSimpleFSM) CheckState(tcp *layers.TCP, dir TCPFlowDirection) bool {
if t.state == TCPStateClosed && t.options.SupportMissingEstablishment && !(tcp.SYN && !tcp.ACK) {
/* try to figure out state */
switch true {
case tcp.SYN && tcp.ACK:
t.state = TCPStateSynSent
t.dir = dir.Reverse()
case tcp.FIN && !tcp.ACK:
t.state = TCPStateEstablished
case tcp.FIN && tcp.ACK:
t.state = TCPStateCloseWait
t.dir = dir.Reverse()
default:
t.state = TCPStateEstablished
}
}
switch t.state {
/* openning connection */
case TCPStateClosed:
if tcp.SYN && !tcp.ACK {
t.dir = dir
t.state = TCPStateSynSent
return true
}
case TCPStateSynSent:
if tcp.RST {
t.state = TCPStateReset
return true
}
if tcp.SYN && tcp.ACK && dir == t.dir.Reverse() {
t.state = TCPStateEstablished
return true
}
if tcp.SYN && !tcp.ACK && dir == t.dir {
// re-transmission
return true
}
/* established */
case TCPStateEstablished:
if tcp.RST {
t.state = TCPStateReset
return true
}
if tcp.FIN {
t.state = TCPStateCloseWait
t.dir = dir
return true
}
// accept any packet
return true
/* closing connection */
case TCPStateCloseWait:
if tcp.RST {
t.state = TCPStateReset
return true
}
if tcp.FIN && tcp.ACK && dir == t.dir.Reverse() {
t.state = TCPStateLastAck
return true
}
if tcp.ACK {
return true
}
case TCPStateLastAck:
if tcp.RST {
t.state = TCPStateReset
return true
}
if tcp.ACK && t.dir == dir {
t.state = TCPStateClosed
return true
}
}
return false
}