-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtypes.go
416 lines (361 loc) · 13.4 KB
/
types.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
package conntrack
import (
"context"
"errors"
"fmt"
"log"
"net"
"time"
"github.com/florianl/go-conntrack/internal/unix"
"github.com/mdlayher/netlink"
)
// Config contains options for a Conn.
type Config struct {
// NetNS specifies the network namespace the Conn will operate in.
//
// If set (non-zero), Conn will enter the specified network namespace and
// an error will occur in Dial if the operation fails.
//
// If not set (zero), a best-effort attempt will be made to enter the
// network namespace of the calling thread: this means that any changes made
// to the calling thread's network namespace will also be reflected in Conn.
// If this operation fails (due to lack of permissions or because network
// namespaces are disabled by kernel configuration), Dial will not return
// an error, and the Conn will operate in the default network namespace of
// the process. This enables non-privileged use of Conn in applications
// which do not require elevated privileges.
NetNS int
// Time till a read action times out - only available for Go >= 1.12
//
// Deprecated: Cancel the context passed to RegisterFiltered() or Register()
// to remove the conntrack gracefully. Setting this value does no longer
// have an effect on conntrack.
ReadTimeout time.Duration
// Time till a write action times out - only available for Go >= 1.12
WriteTimeout time.Duration
// Interface to log internals.
Logger *log.Logger
// DisableNSLockThread disables package netlink's default goroutine thread
// locking behavior.
//
// By default, the library will lock the processing goroutine to its
// corresponding OS thread in order to enable communication over netlink to
// a different network namespace.
//
// If the caller already knows that the netlink socket is in the same
// namespace as the calling thread, this can introduce a performance
// impact. This option disables the OS thread locking behavior if
// performance considerations are of interest.
//
// If disabled, it is the responsibility of the caller to make sure that all
// threads are running in the correct namespace.
//
// When DisableNSLockThread is set, the caller cannot set the NetNS value.
DisableNSLockThread bool
// AddConntrackInformation enriches Con and provides additional information of
// the Netlink/Conntrack origin.
AddConntrackInformation bool
}
// Nfct represents a conntrack handler
type Nfct struct {
// Con is the pure representation of a netlink socket
Con *netlink.Conn
logger *log.Logger
errChan chan error
debug bool
setWriteTimeout func() error
ctx context.Context
ctxCancel context.CancelFunc
shutdown chan struct{}
addConntrackInformation bool
}
// adjust the WriteTimeout (mostly for testing)
func adjustWriteTimeout(nfct *Nfct, fn func() error) {
nfct.setWriteTimeout = fn
}
// SecCtx contains additional information about the security context
type SecCtx struct {
Name *string
}
// Timestamp contains start and/or stop times
type Timestamp struct {
Start *time.Time
Stop *time.Time
}
// TCPFlags contains additional information for TCP flags
type TCPFlags struct {
Flags *uint8
Mask *uint8
}
// TCPInfo contains additional information for TCP sessions
type TCPInfo struct {
State *uint8
WScaleOrig *uint8
WScaleRepl *uint8
FlagsOrig *TCPFlags
FlagsReply *TCPFlags
}
// DCCPInfo contains additional information for DCCP sessions
type DCCPInfo struct {
State *uint8
Role *uint8
HandshakeSeq *uint64
}
// SCTPInfo contains additional information for SCTP sessions
type SCTPInfo struct {
State *uint8
VTagOriginal *uint32
VTagReply *uint32
}
// Helper contains additional information
type Helper struct {
Name *string
Info *string
}
// SeqAdj contains additional information about corrections
type SeqAdj struct {
CorrectionPos *uint32
OffsetBefore *uint32
OffsetAfter *uint32
}
// Counter contains additional information about the traffic
type Counter struct {
Packets *uint64
Bytes *uint64
Packets32 *uint32
Bytes32 *uint32
}
// ProtoInfo contains additional information to certain protocols
type ProtoInfo struct {
TCP *TCPInfo
DCCP *DCCPInfo
SCTP *SCTPInfo
}
// ProtoTuple contains information about the used protocol
type ProtoTuple struct {
Number *uint8
SrcPort *uint16
DstPort *uint16
IcmpID *uint16
IcmpType *uint8
IcmpCode *uint8
Icmpv6ID *uint16
Icmpv6Type *uint8
Icmpv6Code *uint8
}
// IPTuple contains the source and destination IP
type IPTuple struct {
Src *net.IP
Dst *net.IP
Proto *ProtoTuple
Zone *uint16
}
// NatInfo contains addition NAT information of a connection
type NatInfo struct {
Dir *uint32
Tuple *IPTuple
}
// Exp extends the information of a connection by information from the expected table
type Exp struct {
Master *IPTuple
Tuple *IPTuple
Mask *IPTuple
Flags *uint32
Class *uint32
ID *uint32
Timeout *uint32
Zone *uint16
HelperName *string
Fn *string
Nat *NatInfo
}
// Nat contains information for source/destination NAT
type Nat struct {
IPMin *net.IP
IPMax *net.IP
Proto *ProtoTuple
}
// Con contains all the information of a connection
type Con struct {
Info *InfoSource
Origin *IPTuple
Reply *IPTuple
ProtoInfo *ProtoInfo
CounterOrigin *Counter
CounterReply *Counter
Helper *Helper
NatSrc *Nat
SeqAdjOrig *SeqAdj
SeqAdjRepl *SeqAdj
ID *uint32
Status *uint32
StatusMask *uint32
Use *uint32
Mark *uint32
MarkMask *uint32
Timeout *uint32
Zone *uint16
Timestamp *Timestamp
SecCtx *SecCtx
Exp *Exp
}
// InfoSource provides further information from Netlink about a connection.
type InfoSource struct {
// Conntrack table this information originates from.
Table Table
// NetlinkGroup this information originates from.
NetlinkGroup NetlinkGroup
}
// CPUStat contains various conntrack related per CPU statistics
type CPUStat struct {
// ID of the CPU
ID uint32
// Values from the conntrack table
Found *uint32
Invalid *uint32
Ignore *uint32
Insert *uint32
InsertFailed *uint32
Drop *uint32
EarlyDrop *uint32
Error *uint32
SearchRestart *uint32
// Values from the expect table
ExpNew *uint32
ExpCreate *uint32
ExpDelete *uint32
}
// Table specifies the subsystem of conntrack
type Table int
// NetlinkGroup represents a Netlink multicast group
type NetlinkGroup uint32
// Supported Netlink groups
const (
NetlinkCtNew NetlinkGroup = 1 << iota
NetlinkCtUpdate NetlinkGroup = 1 << iota
NetlinkCtDestroy NetlinkGroup = 1 << iota
NetlinkCtExpectedNew NetlinkGroup = 1 << iota
NetlinkCtExpectedUpdate NetlinkGroup = 1 << iota
NetlinkCtExpectedDestroy NetlinkGroup = 1 << iota
)
// Family specifies the network family
type Family uint8
// Supported family types
const (
IPv6 Family = unix.AF_INET6
IPv4 Family = unix.AF_INET
)
// FilterAttr represents a very basic filter
type FilterAttr struct {
Mark, MarkMask []byte
}
// ConnAttr represents the type and value of a attribute of a connection
type ConnAttr struct {
Type ConnAttrType
Data []byte
// Mask is required for specific attributes, if you want to filter for them
Mask []byte
// Negates this attribute for filtering
Negate bool
}
func (ca ConnAttr) String() string {
return fmt.Sprintf("Type: %2d - Data: [%v] - Mask: [%v] - Negate: %t\n", ca.Type, ca.Data, ca.Mask, ca.Negate)
}
// ConnAttrType specifies the attribute of a connection
type ConnAttrType uint16
// Attributes of a connection
// based on libnetfilter_conntrack.h
const (
AttrOrigIPv4Src ConnAttrType = iota /* u32 bits, requires a mask if applied as filter */
AttrOrigIPv4Dst ConnAttrType = iota /* u32 bits, requires a mask if applied as filter */
AttrReplIPv4Src ConnAttrType = iota /* u32 bits, requires a mask if applied as filter */
AttrReplIPv4Dst ConnAttrType = iota /* u32 bits, requires a mask if applied as filter */
AttrOrigIPv6Src ConnAttrType = iota /* u128 bits */
AttrOrigIPv6Dst ConnAttrType = iota /* u128 bits */
AttrReplIPv6Src ConnAttrType = iota /* u128 bits */
AttrReplIPv6Dst ConnAttrType = iota /* u128 bits */
AttrOrigPortSrc ConnAttrType = iota /* u16 bits */
AttrOrigPortDst ConnAttrType = iota /* u16 bits */
AttrReplPortSrc ConnAttrType = iota /* u16 bits */
AttrReplPortDst ConnAttrType = iota /* u16 bits */
AttrIcmpType ConnAttrType = iota /* u8 bits */
AttrIcmpCode ConnAttrType = iota /* u8 bits */
AttrIcmpID ConnAttrType = iota /* u16 bits */
AttrOrigL3Proto ConnAttrType = iota /* u8 bits */
AttrReplL3Proto ConnAttrType = iota /* u8 bits */
AttrOrigL4Proto ConnAttrType = iota /* u8 bits */
AttrReplL4Proto ConnAttrType = iota /* u8 bits */
AttrTCPState ConnAttrType = iota /* u8 bits */
AttrSNatIPv4 ConnAttrType = iota /* u32 bits */
AttrDNatIPv4 ConnAttrType = iota /* u32 bits */
AttrSNatPort ConnAttrType = iota /* u16 bits */
AttrDNatPort ConnAttrType = iota /* u16 bits */
AttrTimeout ConnAttrType = iota /* u32 bits */
AttrMark ConnAttrType = iota /* u32 bits, requires a mask if applied as filter */
AttrMarkMask ConnAttrType = iota /* u32 bits */
AttrOrigCounterPackets ConnAttrType = iota /* u64 bits */
AttrReplCounterPackets ConnAttrType = iota /* u64 bits */
AttrOrigCounterBytes ConnAttrType = iota /* u64 bits */
AttrReplCounterBytes ConnAttrType = iota /* u64 bits */
AttrUse ConnAttrType = iota /* u32 bits */
AttrID ConnAttrType = iota /* u32 bits */
AttrStatus ConnAttrType = iota /* u32 bits */
AttrTCPFlagsOrig ConnAttrType = iota /* u8 bits */
AttrTCPFlagsRepl ConnAttrType = iota /* u8 bits */
AttrTCPMaskOrig ConnAttrType = iota /* u8 bits */
AttrTCPMaskRepl ConnAttrType = iota /* u8 bits */
AttrMasterIPv4Src ConnAttrType = iota /* u32 bits */
AttrMasterIPv4Dst ConnAttrType = iota /* u32 bits */
AttrMasterIPv6Src ConnAttrType = iota /* u128 bits */
AttrMasterIPv6Dst ConnAttrType = iota /* u128 bits */
AttrMasterPortSrc ConnAttrType = iota /* u16 bits */
AttrMasterPortDst ConnAttrType = iota /* u16 bits */
AttrMasterL3Proto ConnAttrType = iota /* u8 bits */
AttrMasterL4Proto ConnAttrType = iota /* u8 bits */
AttrSecmark ConnAttrType = iota /* u32 bits */
AttrOrigNatSeqCorrectionPos ConnAttrType = iota /* u32 bits */
AttrOrigNatSeqOffsetBefore ConnAttrType = iota /* u32 bits */
AttrOrigNatSeqOffsetAfter ConnAttrType = iota /* u32 bits */
AttrReplNatSeqCorrectionPos ConnAttrType = iota /* u32 bits */
AttrReplNatSeqOffsetBefore ConnAttrType = iota /* u32 bits */
AttrReplNatSeqOffsetAfter ConnAttrType = iota /* u32 bits */
AttrSctpState ConnAttrType = iota /* u8 bits */
AttrSctpVtagOrig ConnAttrType = iota /* u32 bits */
AttrSctpVtagRepl ConnAttrType = iota /* u32 bits */
AttrHelperName ConnAttrType = iota /* string (30 bytes max) */
AttrDccpState ConnAttrType = iota /* u8 bits */
AttrDccpRole ConnAttrType = iota /* u8 bits */
AttrDccpHandshakeSeq ConnAttrType = iota /* u64 bits */
AttrTCPWScaleOrig ConnAttrType = iota /* u8 bits */
AttrTCPWScaleRepl ConnAttrType = iota /* u8 bits */
AttrZone ConnAttrType = iota /* u16 bits */
AttrSecCtx ConnAttrType = iota /* string */
AttrTimestampStart ConnAttrType = iota /* u64 bits linux >= 2.6.38 */
AttrTimestampStop ConnAttrType = iota /* u64 bits linux >= 2.6.38 */
AttrHelperInfo ConnAttrType = iota /* variable length */
AttrConnlabels ConnAttrType = iota /* variable length */
AttrConnlabelsMask ConnAttrType = iota /* variable length */
AttrOrigzone ConnAttrType = iota /* u16 bits */
AttrReplzone ConnAttrType = iota /* u16 bits */
AttrSNatIPv6 ConnAttrType = iota /* u128 bits */
AttrDNatIPv6 ConnAttrType = iota /* u128 bits */
AttrIcmpv6Type ConnAttrType = iota /* u8 bits */
AttrIcmpv6Code ConnAttrType = iota /* u8 bits */
AttrIcmpv6ID ConnAttrType = iota /* u16 bits */
attrMax ConnAttrType = iota /* This is for internal use only */
AttrExpID ConnAttrType = iota /* u32 bits */
AttrExpFlags ConnAttrType = iota /* u32 bits */
AttrExpClass ConnAttrType = iota /* u32 bits */
AttrExpNATDir ConnAttrType = iota /* u32 bits */
// for internal use only
attrUnspec ConnAttrType = iota
)
// Various errors which may occur when processing attributes
var (
ErrAttrLength = errors.New("incorrect length of attribute")
ErrAttrNotImplemented = errors.New("attribute not implemented")
ErrAttrNotExist = errors.New("type of attribute does not exist")
ErrDataLength = errors.New("incorrect length of provided data")
)
// ErrUnknownCtTable will be return, if the function can not be performed on this subsystem
var ErrUnknownCtTable = errors.New("not supported for this subsystem")