forked from aporeto-inc/netlink-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conntrack.go
executable file
·261 lines (203 loc) · 9.25 KB
/
conntrack.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
// +build linux !darwin
package conntrack
import (
"fmt"
"net"
"syscall"
"go.aporeto.io/netlink-go/common"
"go.aporeto.io/netlink-go/common/syscallwrappers"
"github.com/vishvananda/netlink"
)
// NewHandle which returns interface which implements Conntrack table get/set/flush
func NewHandle() Conntrack {
return &Handles{Syscalls: syscallwrappers.NewSyscalls()}
}
// ConntrackTableList retrieves entries from Conntract table and parse it in the conntrack flow struct
// Using vishvananda/netlink and nl packages for parsing
// returns an array of ConntrackFlow with 4 tuples, protocol and mark
func (h *Handles) ConntrackTableList(table netlink.ConntrackTableType) ([]*netlink.ConntrackFlow, error) {
result, err := netlink.ConntrackTableList(table, syscall.AF_INET)
if result == nil || err != nil {
return nil, fmt.Errorf("Empty table")
}
return result, nil
}
// ConntrackTableFlush will flush the Conntrack table entries
// Using vishvananda/netlink and nl packages for flushing entries
func (h *Handles) ConntrackTableFlush(table netlink.ConntrackTableType) error {
return netlink.ConntrackTableFlush(table)
}
// ConntrackTableUpdateMarkForAvailableFlow will update conntrack table mark attribute only if the flow is present
// Also returns number of entries updated
func (h *Handles) ConntrackTableUpdateMarkForAvailableFlow(flows []*netlink.ConntrackFlow, ipSrc, ipDst string, protonum uint8, srcport, dstport uint16, newmark uint32) (int, error) {
var entriesUpdated int
for i := range flows {
isEntryPresent := checkTuplesInFlow(flows[i], ipSrc, ipDst, protonum, srcport, dstport)
if isEntryPresent && newmark != 0 {
err := h.ConntrackTableUpdateMark(ipSrc, ipDst, protonum, srcport, dstport, newmark)
if err != nil {
return 0, fmt.Errorf("Error %v", err)
}
entriesUpdated++
}
}
if entriesUpdated >= 0 {
return entriesUpdated, nil
}
return -1, fmt.Errorf("Entry not present")
}
// ConntrackTableUpdateMark will update conntrack table mark attribute
func (h *Handles) ConntrackTableUpdateMark(ipSrc, ipDst string, protonum uint8, srcport, dstport uint16, newmark uint32) error {
hdr, data := buildConntrackUpdateRequest(ipSrc, ipDst, protonum, srcport, dstport)
mark := common.NfValue32{}
mark.Set32Value(newmark)
data = append(data, appendMark(mark, hdr)...)
return h.sendMessage(hdr, data)
}
// ConntrackTableUpdateLabel will update conntrack table label attribute
// Specific to protocol (TCP or UDP)
// Also returns number of entries updated
func (h *Handles) ConntrackTableUpdateLabel(table netlink.ConntrackTableType, flows []*netlink.ConntrackFlow, ipSrc, ipDst string, protonum uint8, srcport, dstport uint16, newlabels uint32) (int, error) {
var entriesUpdated int
var labels common.NfValue32
for i := range flows {
isEntryPresent := checkTuplesInFlow(flows[i], ipSrc, ipDst, protonum, srcport, dstport)
if isEntryPresent {
hdr, data := buildConntrackUpdateRequest(ipSrc, ipDst, protonum, srcport, dstport)
if protonum == common.TCP_PROTO {
data = append(data, appendProtoInfo(hdr)...)
}
labels.Set32Value(newlabels)
data = append(data, appendLabel(labels, hdr)...)
err := h.sendMessage(hdr, data)
if err != nil {
return 0, err
}
entriesUpdated++
}
}
if entriesUpdated >= 0 {
return entriesUpdated, nil
}
return 0, fmt.Errorf("Entry not present")
}
// checkTuplesInFlow will check the flow with the given parameters (4 tuples and protocol)
// returns true if the table has the given flow, false otherwise
func checkTuplesInFlow(flow *netlink.ConntrackFlow, ipSrc, ipDst string, protonum uint8, srcport, dstport uint16) bool {
var isSrcIPPresent, isDstIPPresent, isProtoPresent, isSrcPortPresent, isDstPortPresent bool
if common.IP2int(flow.Forward.SrcIP) == common.IP2int(net.ParseIP(ipSrc)) && common.IP2int(flow.Reverse.SrcIP) == common.IP2int(net.ParseIP(ipDst)) {
isSrcIPPresent = true
} else {
isSrcIPPresent = false
}
if common.IP2int(flow.Forward.DstIP) == common.IP2int(net.ParseIP(ipDst)) && common.IP2int(flow.Reverse.DstIP) == common.IP2int(net.ParseIP(ipSrc)) {
isDstIPPresent = true
} else {
isDstIPPresent = false
}
if flow.Forward.Protocol == protonum && flow.Reverse.Protocol == protonum {
isProtoPresent = true
} else {
isProtoPresent = false
}
if flow.Forward.SrcPort == srcport && flow.Reverse.SrcPort == dstport {
isSrcPortPresent = true
} else {
isSrcPortPresent = false
}
if flow.Forward.DstPort == dstport && flow.Reverse.DstPort == srcport {
isDstPortPresent = true
} else {
isDstPortPresent = false
}
if isSrcIPPresent && isDstIPPresent && isSrcPortPresent && isDstPortPresent && isProtoPresent {
return true
}
return false
}
// buildConntrackUpdateRequest is generic for all conntrack attribute updates
// returns bytes till dstport from the table, if the flow is present
// to update other attributes, it is highly recommended to check the length of the NESTED attributes
func buildConntrackUpdateRequest(ipSrc, ipDst string, protonum uint8, srcport, dstport uint16) (*syscall.NlMsghdr, []byte) {
var ipv4ValueSrc, ipv4ValueDst common.NfValue32
var protoNum common.NfValue8
var srcPort, dstPort common.NfValue16
ipv4ValueSrc.Set32Value(common.IP2int(net.ParseIP(ipSrc)))
ipv4ValueDst.Set32Value(common.IP2int(net.ParseIP(ipDst)))
protoNum.Set8Value(protonum)
srcPort.Set16Value(srcport)
dstPort.Set16Value(dstport)
hdr := common.BuildNlMsgHeader(common.NfnlConntrackTable, common.NlmFRequest|common.NlmFAck, 0)
nfgen := common.BuildNfgenMsg(syscall.AF_INET, common.NFNetlinkV0, 0, hdr)
nfgenTupleOrigAttr := common.BuildNfAttrMsg(NLA_F_NESTED|CTA_TUPLE_ORIG, hdr, SizeOfNestedTupleOrig)
nfgenTupleIPAttr := common.BuildNfNestedAttrMsg(NLA_F_NESTED|CTA_TUPLE_IP, int(SizeOfNestedTupleIP))
nfgenTupleIPV4SrcAttr := common.BuildNfNestedAttrMsg(CTA_IP_V4_SRC, int(ipv4ValueSrc.Length()))
nfgenTupleIPV4DstAttr := common.BuildNfNestedAttrMsg(CTA_IP_V4_DST, int(ipv4ValueDst.Length()))
nfgenTupleProto := common.BuildNfNestedAttrMsg(NLA_F_NESTED|CTA_TUPLE_PROTO, int(SizeOfNestedTupleProto))
nfgenTupleProtoNum := common.BuildNfAttrWithPaddingMsg(CTA_PROTO_NUM, PROTO_NUM_LEN)
nfgenTupleSrcPort := common.BuildNfAttrWithPaddingMsg(CTA_PROTO_SRC_PORT, PROTO_SRC_PORT_LEN)
nfgenTupleDstPort := common.BuildNfAttrWithPaddingMsg(CTA_PROTO_DST_PORT, PROTO_DST_PORT_LEN)
buf := make([]byte, 3*int(common.SizeofNfAttr)+int(common.SizeofNfGenMsg)+2*int(common.NfaLength(uint16(common.SizeOfValue32)))+2*int(common.NfaLength(uint16(common.SizeOfValue16)))+int(common.NfaLength(uint16(common.SizeOfValue8))))
copyIndex := nfgen.ToWireFormatBuf(buf)
copyIndex += nfgenTupleOrigAttr.ToWireFormatBuf(buf[copyIndex:])
copyIndex += nfgenTupleIPAttr.ToWireFormatBuf(buf[copyIndex:])
copyIndex += nfgenTupleIPV4SrcAttr.ToWireFormatBuf(buf[copyIndex:])
copyIndex += ipv4ValueSrc.ToWireFormatBuf(buf[copyIndex:])
copyIndex += nfgenTupleIPV4DstAttr.ToWireFormatBuf(buf[copyIndex:])
copyIndex += ipv4ValueDst.ToWireFormatBuf(buf[copyIndex:])
copyIndex += nfgenTupleProto.ToWireFormatBuf(buf[copyIndex:])
copyIndex += nfgenTupleProtoNum.ToWireFormatBuf(buf[copyIndex:])
copyIndex += protoNum.ToWireFormatBuf(buf[copyIndex:])
copyIndex += nfgenTupleSrcPort.ToWireFormatBuf(buf[copyIndex:])
copyIndex += srcPort.ToWireFormatBuf(buf[copyIndex:])
copyIndex += nfgenTupleDstPort.ToWireFormatBuf(buf[copyIndex:])
dstPort.ToWireFormatBuf(buf[copyIndex:])
return hdr, buf
}
// appendMark will add the given mark to the flows
func appendMark(mark common.NfValue32, hdr *syscall.NlMsghdr) []byte {
nfgenMark := common.BuildNfAttrMsg(CTA_MARK, hdr, mark.Length())
markData := nfgenMark.ToWireFormat()
markData = append(markData, mark.ToWireFormat()...)
return markData
}
// appendLabel will add the given label to the flows
func appendLabel(label common.NfValue32, hdr *syscall.NlMsghdr) []byte {
nfgenLabel := common.BuildNfAttrMsg(CTA_LABELS, hdr, label.Length())
buf := make([]byte, int(common.SizeOfValue32))
common.NativeEndian().PutUint32(buf, label.Get32Value())
labelData := nfgenLabel.ToWireFormat()
labelData = append(labelData, buf...)
return labelData
}
// appendProtoInfo will add protocolinfo to the bytes
// only if the protocol is TCP
func appendProtoInfo(hdr *syscall.NlMsghdr) []byte {
var flagsOrig, flagsReply common.NfValue16
var data []byte
flagsOrig.Set16Value(uint16(2570))
flagsReply.Set16Value(uint16(2570))
protoData := common.BuildNfAttrMsg(NLA_F_NESTED|CTA_PROTOINFO, hdr, SizeofNestedProtoInfo)
protoTCPData := common.BuildNfNestedAttrMsg(NLA_F_NESTED|CTA_PROTOINFO_TCP, int(SizeofNestedProtoInfoTCP))
protoOriginal := common.BuildNfAttrWithPaddingMsg(CTA_PROTOINFO_TCP_FLAGS_ORIGINAL, int(flagsOrig.Length()))
protoReply := common.BuildNfAttrWithPaddingMsg(CTA_PROTOINFO_TCP_FLAGS_REPLY, int(flagsReply.Length()))
data = append(data, protoData.ToWireFormat()...)
data = append(data, protoTCPData.ToWireFormat()...)
data = append(data, protoOriginal.ToWireFormat()...)
data = append(data, flagsOrig.ToWireFormat()...)
data = append(data, protoReply.ToWireFormat()...)
data = append(data, flagsReply.ToWireFormat()...)
return data
}
func (h *Handles) sendMessage(hdr *syscall.NlMsghdr, data []byte) error {
sh, err := h.open()
defer sh.close()
if err != nil {
return err
}
netlinkMsg := &syscall.NetlinkMessage{
Header: *hdr,
Data: data,
}
return sh.query(netlinkMsg)
}