Skip to content

Commit 1cf857d

Browse files
committed
support variable IHL
1 parent 90f4ba3 commit 1cf857d

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

pkg/flowctl/data/filter.bpf.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,11 @@ static inline void record(const struct tcphdr *th, const struct iphdr *ih,
214214
}
215215

216216
static inline int
217-
process_ipv4_tcp(struct __sk_buff *skb, __u8 encap_with)
217+
process_ipv4_tcp(struct __sk_buff *skb, __u64 offset)
218218
{
219219
__u64 data = skb->data;
220220
__u64 data_end = skb->data_end;
221-
222-
if (encap_with == IPPROTO_IPIP)
223-
data += sizeof(struct iphdr);
221+
data += offset;
224222

225223
struct iphdr *ih = (struct iphdr *)(data + sizeof(struct ethhdr));
226224
assert_len(ih, data_end);
@@ -234,14 +232,14 @@ process_ipv4_tcp(struct __sk_buff *skb, __u8 encap_with)
234232
}
235233

236234
static inline int
237-
process_ipv4_icmp(struct __sk_buff *skb, __u8 encap_with)
235+
process_ipv4_icmp(struct __sk_buff *skb, __u64 offset)
238236
{
239237
// bpf_printk("icmp packet");
240238
return TC_ACT_OK;
241239
}
242240

243241
static inline int
244-
process_ipv4_udp(struct __sk_buff *skb, __u8 encap_with)
242+
process_ipv4_udp(struct __sk_buff *skb, __u64 offset)
245243
{
246244
// bpf_printk("udp packet");
247245
return TC_ACT_OK;
@@ -256,17 +254,21 @@ process_ipv4_ipip(struct __sk_buff *skb)
256254

257255
struct iphdr *outer_ih = (struct iphdr *)(data + sizeof(struct ethhdr));
258256
assert_len(outer_ih, data_end);
257+
__u64 outer_ih_len = sizeof(struct iphdr) + outer_ih->ihl * 4;
259258

260-
struct iphdr *inner_ih = (struct iphdr *)((char *)outer_ih + sizeof(struct iphdr));
259+
struct iphdr *inner_ih = (struct iphdr *)((char *)outer_ih + outer_ih_len);
261260
assert_len(inner_ih, data_end);
262261

262+
if (inner_ih->ihl < 5)
263+
return TC_ACT_SHOT;
264+
263265
switch (inner_ih->protocol) {
264266
case IPPROTO_ICMP:
265-
return process_ipv4_icmp(skb, IPPROTO_IPIP);
267+
return process_ipv4_icmp(skb, outer_ih_len);
266268
case IPPROTO_TCP:
267-
return process_ipv4_tcp(skb, IPPROTO_IPIP);
269+
return process_ipv4_tcp(skb, outer_ih_len);
268270
case IPPROTO_UDP:
269-
return process_ipv4_udp(skb, IPPROTO_IPIP);
271+
return process_ipv4_udp(skb, outer_ih_len);
270272
default:
271273
return TC_ACT_OK;
272274
}

0 commit comments

Comments
 (0)