@@ -94,11 +94,18 @@ func calculateSkipFalse(srcPort, dstPort uint16) uint8 {
94
94
return count
95
95
}
96
96
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 {
98
99
inst := []bpf.Instruction {}
99
100
100
101
// 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 {
102
109
inst = append (inst , bpf.JumpIf {Cond : bpf .JumpEqual , Val : srcAddrVal , SkipTrue : 0 , SkipFalse : calculateSkipFalse (srcPort , dstPort )})
103
110
} else {
104
111
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
178
185
}
179
186
}
180
187
181
- // ip address and port check
182
188
inst = append (inst , loadIPv4SourceAddress )
183
189
184
190
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 )... )
186
192
} 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 )... )
188
194
} 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 )... )
191
197
}
192
198
193
199
// return (drop)
@@ -253,31 +259,31 @@ func compilePacketFilter(packetSpec *crdv1alpha1.Packet, srcIP, dstIP net.IP, di
253
259
// For simpler code generation in 'Both' direction, an extra instruction to accept the packet is added after instruction 014.
254
260
// The final instruction set looks like this:
255
261
// (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
257
263
// (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
259
265
// (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
261
267
// (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
263
269
// (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
265
271
// (010) ldxb 4*([14]&0xf) # x = IP header length
266
272
// (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
268
274
// (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
270
276
// (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
272
278
// (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
274
280
// (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
276
282
// (021) ldxb 4*([14]&0xf) # x = IP header length
277
283
// (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
279
285
// (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
281
287
// (026) ret #262144 # MATCH
282
288
// (027) ret #0 # NOMATCH
283
289
0 commit comments