Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to record IPv4 encapsulated packets #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions pkg/flowctl/data/filter.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,14 @@ static inline void record(const struct tcphdr *th, const struct iphdr *ih,
}

static inline int
process_ipv4_tcp(struct __sk_buff *skb)
process_ipv4_tcp(struct __sk_buff *skb, __u64 offset)
{
__u64 data = skb->data;
__u64 data_end = skb->data_end;
__u64 pkt_len = 0;
data += offset;

struct iphdr *ih = (struct iphdr *)(data + sizeof(struct ethhdr));
assert_len(ih, data_end);
pkt_len = data_end - data;

__u8 hdr_len = ih->ihl * 4;
struct tcphdr *th = (struct tcphdr *)((char *)ih + hdr_len);
Expand All @@ -233,19 +232,48 @@ process_ipv4_tcp(struct __sk_buff *skb)
}

static inline int
process_ipv4_icmp(struct __sk_buff *skb)
process_ipv4_icmp(struct __sk_buff *skb, __u64 offset)
{
// bpf_printk("icmp packet");
return TC_ACT_OK;
}

static inline int
process_ipv4_udp(struct __sk_buff *skb)
process_ipv4_udp(struct __sk_buff *skb, __u64 offset)
{
// bpf_printk("udp packet");
return TC_ACT_OK;
}

static inline int
process_ipv4_ipip(struct __sk_buff *skb)
{
__u64 data = skb->data;
__u64 data_end = skb->data_end;
__u64 pkt_len = 0;

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

struct iphdr *inner_ih = (struct iphdr *)((char *)outer_ih + outer_ih_len);
assert_len(inner_ih, data_end);

if (inner_ih->ihl < 5)
return TC_ACT_SHOT;
Comment on lines +262 to +263
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the IP header has options, inner_ih->ihl will be greater than 5, right? If you add the IP header (20) length in process_ipv4_tcp, you may need to implement it like the following?

Suggested change
if (inner_ih->ihl < 5)
return TC_ACT_SHOT;
if (inner_ih->ihl != 5)
return TC_ACT_SHOT;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or you need to calculate correct offset with ih->ihl.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this tool supports IP option (ihl > 5) but current this PR doesn't calculate offset of inner IP with correct ihl. So let me fix it.


switch (inner_ih->protocol) {
case IPPROTO_ICMP:
return process_ipv4_icmp(skb, outer_ih_len);
case IPPROTO_TCP:
return process_ipv4_tcp(skb, outer_ih_len);
case IPPROTO_UDP:
return process_ipv4_udp(skb, outer_ih_len);
default:
return TC_ACT_OK;
}
}

static inline int
process_ipv4(struct __sk_buff *skb)
{
Expand All @@ -262,11 +290,14 @@ process_ipv4(struct __sk_buff *skb)

switch (ih->protocol) {
case IPPROTO_ICMP:
return process_ipv4_icmp(skb);
return process_ipv4_icmp(skb, 0);
case IPPROTO_TCP:
return process_ipv4_tcp(skb);
return process_ipv4_tcp(skb, 0);
case IPPROTO_UDP:
return process_ipv4_udp(skb);
return process_ipv4_udp(skb, 0);
/* encapsulated packets */
case IPPROTO_IPIP:
return process_ipv4_ipip(skb);
default:
return TC_ACT_OK;
}
Expand Down