diff --git a/staticlibs/native/bpf_headers/include/bpf/BpfClassic.h b/staticlibs/native/bpf_headers/include/bpf/BpfClassic.h index 034460b1dbd6594a8172f5153e3dbd1b2b53daf3..f7ffddb10101d280216dc9c0a0f189974caa2706 100644 --- a/staticlibs/native/bpf_headers/include/bpf/BpfClassic.h +++ b/staticlibs/native/bpf_headers/include/bpf/BpfClassic.h @@ -108,3 +108,55 @@ _Static_assert(field_sizeof(struct ipv6hdr, field) == 4, "field of wrong size"); \ offsetof(ipv6hdr, field); \ })) + +// Load the length of the IPv4 header into X index register. +// ie. X := 4 * IPv4.IHL, where IPv4.IHL is the bottom nibble +// of the first byte of the IPv4 (aka network layer) header. +#define BPF_LOADX_NET_RELATIVE_IPV4_HLEN \ + BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, (__u32)SKF_NET_OFF) + +// Blindly assumes no IPv6 extension headers, just does X := 40 +// You may later adjust this as you parse through IPv6 ext hdrs. +#define BPF_LOADX_CONSTANT_IPV6_HLEN \ + BPF_STMT(BPF_LDX | BPF_W | BPF_IMM, sizeof(struct ipv6hdr)) + +// NOTE: all the following require X to be setup correctly (v4: 20+, v6: 40+) + +// 8-bit load from L4 (TCP/UDP/...) header +#define BPF_LOAD_NETX_RELATIVE_L4_U8(ofs) \ + BPF_STMT(BPF_LD | BPF_B | BPF_IND, (__u32)SKF_NET_OFF + (ofs)) + +// Big/Network Endian 16-bit load from L4 (TCP/UDP/...) header +#define BPF_LOAD_NETX_RELATIVE_L4_BE16(ofs) \ + BPF_STMT(BPF_LD | BPF_H | BPF_IND, (__u32)SKF_NET_OFF + (ofs)) + +// Big/Network Endian 32-bit load from L4 (TCP/UDP/...) header +#define BPF_LOAD_NETX_RELATIVE_L4_BE32(ofs) \ + BPF_STMT(BPF_LD | BPF_W | BPF_IND, (__u32)SKF_NET_OFF + (ofs)) + +// Both ICMPv4 and ICMPv6 start with u8 type, u8 code +#define BPF_LOAD_NETX_RELATIVE_ICMP_TYPE BPF_LOAD_NETX_RELATIVE_L4_U8(0) +#define BPF_LOAD_NETX_RELATIVE_ICMP_CODE BPF_LOAD_NETX_RELATIVE_L4_U8(1) + +// IPv6 extension headers (HOPOPTS, DSTOPS, FRAG) begin with a u8 nexthdr +#define BPF_LOAD_NETX_RELATIVE_V6EXTHDR_NEXTHDR BPF_LOAD_NETX_RELATIVE_L4_U8(0) + +// IPv6 fragment header is always exactly 8 bytes long +#define BPF_LOAD_CONSTANT_V6FRAGHDR_LEN \ + BPF_STMT(BPF_LD | BPF_IMM, 8) + +// HOPOPTS/DSTOPS follow up with 'u8 len', counting 8 byte units, (0->8, 1->16) +// *THREE* instructions +#define BPF3_LOAD_NETX_RELATIVE_V6EXTHDR_LEN \ + BPF_LOAD_NETX_RELATIVE_L4_U8(1) \ + BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1) \ + BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 3) + +// *TWO* instructions: A += X; X := A +#define BPF2_ADD_A_TO_X \ + BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0) \ + BPF_STMT(BPF_MISC | BPF_TAX, 0) + +// UDP/UDPLITE/TCP/SCTP/DCCP all start with be16 srcport, dstport +#define BPF_LOAD_NETX_RELATIVE_SRC_PORT BPF_LOAD_NETX_RELATIVE_L4_BE16(0) +#define BPF_LOAD_NETX_RELATIVE_DST_PORT BPF_LOAD_NETX_RELATIVE_L4_BE16(2)