-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathpacket.h
More file actions
26 lines (23 loc) · 771 Bytes
/
packet.h
File metadata and controls
26 lines (23 loc) · 771 Bytes
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
#include <linux/ip.h>
// Returns the protocol byte for an IP packet, 0 for anything else
static __always_inline u64 lookup_protocol(struct xdp_md *ctx)
{
u64 protocol = 0;
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
struct ethhdr *eth = data;
if (data + sizeof(struct ethhdr) > data_end)
return 0;
// Check that it's an IP packet
if (bpf_ntohs(eth->h_proto) == ETH_P_IP)
{
// Return the protocol of this packet
// 1 = ICMP
// 6 = TCP
// 17 = UDP
struct iphdr *iph = data + sizeof(struct ethhdr);
if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) <= data_end)
protocol = iph->protocol;
}
return protocol;
}