Skip to content
Snippets Groups Projects
Commit 73df7414 authored by Maciej Żenczykowski's avatar Maciej Żenczykowski
Browse files

BpfClassic.h - more helpers


Test: TreeHugger
Signed-off-by: default avatarMaciej Żenczykowski <maze@google.com>
Change-Id: Ie0eb8895c2d887b71ba886735ad106c1118ebeac
parent b900201f
No related branches found
No related tags found
No related merge requests found
...@@ -108,3 +108,55 @@ ...@@ -108,3 +108,55 @@
_Static_assert(field_sizeof(struct ipv6hdr, field) == 4, "field of wrong size"); \ _Static_assert(field_sizeof(struct ipv6hdr, field) == 4, "field of wrong size"); \
offsetof(ipv6hdr, field); \ 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment