Skip to content

Commit 474a382

Browse files
committed
change function and param name
Signed-off-by: Aryan Bakliwal <[email protected]>
1 parent 67b6633 commit 474a382

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

pkg/agent/packetcapture/capture/bpf.go

+25-19
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,18 @@ func calculateSkipFalse(srcPort, dstPort uint16) uint8 {
9494
return count
9595
}
9696

97-
func compareIPPort(srcAddrVal, dstAddrVal uint32, size, curLen uint8, srcPort, dstPort uint16, useSkipFalse bool) []bpf.Instruction {
97+
// Generates IP address and port matching instructions
98+
func compileIPPortFilter(srcAddrVal, dstAddrVal uint32, size, curLen uint8, srcPort, dstPort uint16, skipRequestCheck bool) []bpf.Instruction {
9899
inst := []bpf.Instruction{}
99100

100101
// from here we need to check the inst length to calculate skipFalse. If no protocol is set, there will be no related bpf instructions.
101-
if useSkipFalse {
102+
103+
// In the previous instruction, we load the packet's source IP. We then compare it with the source IP from the packet spec to determine if
104+
// the packet is a request (from source to destination). When capturing packets in Both directions, if the source IPs do not match,
105+
// we need to check if the packet is a response (from destination to source). In this case, we skip to the instruction where we compare the
106+
// loaded source IP with the destination IP from the packet spec. The skipRequestCheck flag indicates whether we need to call calculateSkipFalse
107+
// to determine how many instructions to skip before checking for response packets or just skip to the last instruction (drop packet).
108+
if skipRequestCheck {
102109
inst = append(inst, bpf.JumpIf{Cond: bpf.JumpEqual, Val: srcAddrVal, SkipTrue: 0, SkipFalse: calculateSkipFalse(srcPort, dstPort)})
103110
} else {
104111
inst = append(inst, bpf.JumpIf{Cond: bpf.JumpEqual, Val: srcAddrVal, SkipTrue: 0, SkipFalse: size - curLen - uint8(len(inst)) - 2})
@@ -178,16 +185,15 @@ func compilePacketFilter(packetSpec *crdv1alpha1.Packet, srcIP, dstIP net.IP, di
178185
}
179186
}
180187

181-
// ip address and port check
182188
inst = append(inst, loadIPv4SourceAddress)
183189

184190
if direction == crdv1alpha1.CaptureDirectionSourceToDestination {
185-
inst = append(inst, compareIPPort(srcAddrVal, dstAddrVal, size, uint8(len(inst)), srcPort, dstPort, false)...)
191+
inst = append(inst, compileIPPortFilter(srcAddrVal, dstAddrVal, size, uint8(len(inst)), srcPort, dstPort, false)...)
186192
} else if direction == crdv1alpha1.CaptureDirectionDestinationToSource {
187-
inst = append(inst, compareIPPort(dstAddrVal, srcAddrVal, size, uint8(len(inst)), dstPort, srcPort, false)...)
193+
inst = append(inst, compileIPPortFilter(dstAddrVal, srcAddrVal, size, uint8(len(inst)), dstPort, srcPort, false)...)
188194
} else {
189-
inst = append(inst, compareIPPort(srcAddrVal, dstAddrVal, size, uint8(len(inst)), srcPort, dstPort, true)...)
190-
inst = append(inst, compareIPPort(dstAddrVal, srcAddrVal, size, uint8(len(inst)), dstPort, srcPort, false)...)
195+
inst = append(inst, compileIPPortFilter(srcAddrVal, dstAddrVal, size, uint8(len(inst)), srcPort, dstPort, true)...)
196+
inst = append(inst, compileIPPortFilter(dstAddrVal, srcAddrVal, size, uint8(len(inst)), dstPort, srcPort, false)...)
191197
}
192198

193199
// return (drop)
@@ -253,31 +259,31 @@ func compilePacketFilter(packetSpec *crdv1alpha1.Packet, srcIP, dstIP net.IP, di
253259
// For simpler code generation in 'Both' direction, an extra instruction to accept the packet is added after instruction 014.
254260
// The final instruction set looks like this:
255261
// (000) ldh [12] # Load 2B at 12 (Ethertype)
256-
// (001) jeq #0x800 jt 2 jf 27 # Ethertype: If IPv4, goto #2, else #26
262+
// (001) jeq #0x800 jt 2 jf 27 # Ethertype: If IPv4, goto #2, else #27
257263
// (002) ldb [23] # Load 1B at 23 (IPv4 Protocol)
258-
// (003) jeq #0x6 jt 4 jf 27 # IPv4 Protocol: If TCP, goto #4, #26
264+
// (003) jeq #0x6 jt 4 jf 27 # IPv4 Protocol: If TCP, goto #4, #27
259265
// (004) ld [26] # Load 4B at 26 (source address)
260-
// (005) jeq #0xaf40102 jt 6 jf 16 # If bytes match(10.244.1.2), goto #6, else #15
266+
// (005) jeq #0xaf40102 jt 6 jf 16 # If bytes match(10.244.1.2), goto #6, else #16
261267
// (006) ld [30] # Load 4B at 30 (dest address)
262-
// (007) jeq #0xaf40103 jt 8 jf 27 # If bytes match(10.244.1.3), goto #8, else #26
268+
// (007) jeq #0xaf40103 jt 8 jf 27 # If bytes match(10.244.1.3), goto #8, else #27
263269
// (008) ldh [20] # Load 2B at 20 (13b Fragment Offset)
264-
// (009) jset #0x1fff jt 27 jf 10 # Use 0x1fff as a mask for fragment offset; If fragment offset != 0, #10, else #26
270+
// (009) jset #0x1fff jt 27 jf 10 # Use 0x1fff as a mask for fragment offset; If fragment offset != 0, #10, else #27
265271
// (010) ldxb 4*([14]&0xf) # x = IP header length
266272
// (011) ldh [x + 14] # Load 2B at x+14 (TCP Source Port)
267-
// (012) jeq #0x7b jt 13 jf 27 # TCP Source Port: If 123, goto #13, else #26
273+
// (012) jeq #0x7b jt 13 jf 27 # TCP Source Port: If 123, goto #13, else #27
268274
// (013) ldh [x + 16] # Load 2B at x+16 (TCP dst port)
269-
// (014) jeq #0x7c jt 15 jf 27 # TCP dst port: If 123, goto #15, else #26
275+
// (014) jeq #0x7c jt 15 jf 27 # TCP dst port: If 123, goto #15, else #27
270276
// (015) ret #262144 # MATCH
271-
// (016) jeq #0xaf40103 jt 17 jf 27 # If bytes match(10.244.1.3), goto #16, else #26
277+
// (016) jeq #0xaf40103 jt 17 jf 27 # If bytes match(10.244.1.3), goto #17, else #27
272278
// (017) ld [30] # Load 4B at 30 (return traffic dest address)
273-
// (018) jeq #0xaf40102 jt 19 jf 27 # If bytes match(10.244.1.2), goto #18, else #26
279+
// (018) jeq #0xaf40102 jt 19 jf 27 # If bytes match(10.244.1.2), goto #19, else #27
274280
// (019) ldh [20] # Load 2B at 20 (13b Fragment Offset)
275-
// (020) jset #0x1fff jt 27 jf 21 # Use 0x1fff as a mask for fragment offset; If fragment offset != 0, #20, else #26
281+
// (020) jset #0x1fff jt 27 jf 21 # Use 0x1fff as a mask for fragment offset; If fragment offset != 0, #21, else #27
276282
// (021) ldxb 4*([14]&0xf) # x = IP header length
277283
// (022) ldh [x + 14] # Load 2B at x+14 (TCP Source Port)
278-
// (023) jeq #0x7c jt 24 jf 27 # TCP Source Port: If 124, goto #23, else #26
284+
// (023) jeq #0x7c jt 24 jf 27 # TCP Source Port: If 124, goto #24, else #27
279285
// (024) ldh [x + 16] # Load 2B at x+16 (TCP dst port)
280-
// (025) jeq #0x7b jt 26 jf 27 # TCP dst port: If 123, goto #25, else #26
286+
// (025) jeq #0x7b jt 26 jf 27 # TCP dst port: If 123, goto #26, else #27
281287
// (026) ret #262144 # MATCH
282288
// (027) ret #0 # NOMATCH
283289

pkg/agent/packetcapture/capture/bpf_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func TestCalculateInstructionsSize(t *testing.T) {
5151
},
5252
},
5353
count: 17,
54+
direction: crdv1alpha1.CaptureDirectionSourceToDestination,
5455
},
5556
{
5657
name: "proto and host and port and DestinationToSource",
@@ -77,7 +78,7 @@ func TestCalculateInstructionsSize(t *testing.T) {
7778
},
7879
},
7980
},
80-
count: 27,
81+
count: 28,
8182
direction: crdv1alpha1.CaptureDirectionBoth,
8283
},
8384
{
@@ -86,6 +87,7 @@ func TestCalculateInstructionsSize(t *testing.T) {
8687
Protocol: &testTCPProtocol,
8788
},
8889
count: 10,
90+
direction: crdv1alpha1.CaptureDirectionSourceToDestination,
8991
},
9092
{
9193
name: "proto with src port",
@@ -98,6 +100,7 @@ func TestCalculateInstructionsSize(t *testing.T) {
98100
},
99101
},
100102
count: 15,
103+
direction: crdv1alpha1.CaptureDirectionSourceToDestination,
101104
},
102105
{
103106
name: "proto with dst port",
@@ -110,12 +113,14 @@ func TestCalculateInstructionsSize(t *testing.T) {
110113
},
111114
},
112115
count: 15,
116+
direction: crdv1alpha1.CaptureDirectionSourceToDestination,
113117
},
114118

115119
{
116120
name: "any proto",
117121
packet: &crdv1alpha1.Packet{},
118122
count: 8,
123+
direction: crdv1alpha1.CaptureDirectionSourceToDestination,
119124
},
120125
}
121126

0 commit comments

Comments
 (0)