MaeBPF.Packet (maebpf v0.1.0)

Copy Markdown View Source

Ethernet, IPv4, UDP, and TCP packet builders plus IPv4, CIDR, LPM key, and 16-byte packed RingBuf event codecs.

All network header fields use standard network byte order (big-endian) while LPM prefix lengths and RingBuf event metadata fields (plug_id, action) use little-endian bpfel encoding to match the kernel data plane.

Summary

Functions

Decodes a numeric 16-bit event action code back to an action atom.

Decodes a 16-byte packed RingBuf event binary into a map.

Returns the numeric 16-bit event action code for an action atom.

Encodes a compact 16-byte RingBuf packet event matching the kernel data plane layout: <<plug_id::16-little, action::16-little, ipv4_src::32-big, ipv4_dst::32-big, sport::16-big, dport::16-big>>.

Encodes a CIDR string or {ip, prefixlen} into the 8-byte kernel BPF_MAP_TYPE_LPM_TRIE IPv4 key: <<prefixlen::32-little, ipv4::32-big>>.

Ensures an IPv4/IPv6 packet carries a leading 14-byte Ethernet header (used when feeding raw L3 packets into Linux BPF_PROG_TEST_RUN for cgroup_skb, where the kernel calls eth_type_trans to strip 14 bytes before entering the cgroup_skb program).

Formats a 4-byte IPv4 binary as a dotted-quad string.

Builds an Ethernet + IPv4 packet binary.

Builds an Ethernet + IPv6 packet binary.

Masks a 4-byte IPv4 binary to prefixlen leading bits (0..32).

Parses an IPv4 CIDR string ("10.0.0.0/8") or {ip, prefixlen} tuple into {:ok, {masked_ipv4_bin, prefixlen}}.

Parses an IPv4 address from a dotted string, 4-tuple, 4-byte binary, or unsigned 32-bit integer into a 4-byte network-order binary.

Like parse_ipv4/1, raising ArgumentError on invalid input.

Returns a raw packet binary or constructs a zero-filled raw frame of :size bytes (optionally overriding :ethertype when size >= 14).

Strips a leading 14-byte IPv4/IPv6 Ethernet header from a frame so it can be inspected directly as an L3 packet (as seen by cgroup_skb hooks).

Builds an Ethernet + IPv6 + TCP packet binary.

Builds an Ethernet + IPv4 + TCP packet binary.

Builds an Ethernet + IPv6 + TCP packet carrying a TLS ClientHello payload.

Builds an Ethernet + IPv4 + TCP packet carrying a TLS ClientHello payload.

Builds a raw TLS ClientHello record binary for testing SNI, ECH, PQ, and segmented ClientHello handling.

Builds an Ethernet + IPv4 + UDP packet binary.

Wraps an Ethernet frame with an 802.1Q VLAN tag (tpid defaults to 0x8100).

Functions

decode_action_code(code)

Decodes a numeric 16-bit event action code back to an action atom.

decode_event(other)

Decodes a 16-byte packed RingBuf event binary into a map.

encode_action_code(code)

Returns the numeric 16-bit event action code for an action atom.

encode_event(plug_id, action, ipv4_src, ipv4_dst, sport, dport)

Encodes a compact 16-byte RingBuf packet event matching the kernel data plane layout: <<plug_id::16-little, action::16-little, ipv4_src::32-big, ipv4_dst::32-big, sport::16-big, dport::16-big>>.

encode_lpm_key(cidr_or_ip)

Encodes a CIDR string or {ip, prefixlen} into the 8-byte kernel BPF_MAP_TYPE_LPM_TRIE IPv4 key: <<prefixlen::32-little, ipv4::32-big>>.

encode_lpm_key(ip, prefix)

ensure_ethernet(bin)

Ensures an IPv4/IPv6 packet carries a leading 14-byte Ethernet header (used when feeding raw L3 packets into Linux BPF_PROG_TEST_RUN for cgroup_skb, where the kernel calls eth_type_trans to strip 14 bytes before entering the cgroup_skb program).

format_ipv4(arg)

Formats a 4-byte IPv4 binary as a dotted-quad string.

ipv4(opts \\ [])

Builds an Ethernet + IPv4 packet binary.

Options:

  • :src - source IPv4 address (string, 4-tuple, 4-byte binary, or u32)
  • :dst - destination IPv4 address
  • :proto - IPv4 protocol byte (default 17 for UDP)
  • :ihl - IPv4 IHL in 32-bit words (default 5; values < 5 construct intentionally malformed headers for testing)
  • :version - IPv4 version nibble (default 4)
  • :ttl - IPv4 TTL byte (default 64)
  • :ethertype - 16-bit EtherType (default 0x0800)
  • :src_mac - 6-byte source MAC address
  • :dst_mac - 6-byte destination MAC address
  • :payload - L4 payload binary (default <<>>)

ipv6(opts \\ [])

Builds an Ethernet + IPv6 packet binary.

Options:

  • :src - source IPv6 address (string, 8-tuple, or 16-byte binary)
  • :dst - destination IPv6 address (string, 8-tuple, or 16-byte binary)
  • :proto - IPv6 Next Header byte (default 6 for TCP)
  • :hop_limit - IPv6 Hop Limit byte (default 64)
  • :vlan - optional 802.1Q VLAN ID (0..4095)
  • :payload - L4 payload binary (default <<>>)

mask_ipv4(arg, prefix)

Masks a 4-byte IPv4 binary to prefixlen leading bits (0..32).

parse_cidr(str)

Parses an IPv4 CIDR string ("10.0.0.0/8") or {ip, prefixlen} tuple into {:ok, {masked_ipv4_bin, prefixlen}}.

parse_ipv4(bin)

Parses an IPv4 address from a dotted string, 4-tuple, 4-byte binary, or unsigned 32-bit integer into a 4-byte network-order binary.

parse_ipv4!(input)

Like parse_ipv4/1, raising ArgumentError on invalid input.

raw(bin)

Returns a raw packet binary or constructs a zero-filled raw frame of :size bytes (optionally overriding :ethertype when size >= 14).

strip_ethernet(bin)

Strips a leading 14-byte IPv4/IPv6 Ethernet header from a frame so it can be inspected directly as an L3 packet (as seen by cgroup_skb hooks).

Returns bin unchanged when it is already a raw L3 IPv4/IPv6 packet or does not carry an Ethernet IPv4 (0x0800) / IPv6 (0x86DD) header.

tcp6(opts \\ [])

Builds an Ethernet + IPv6 + TCP packet binary.

tcp(opts \\ [])

Builds an Ethernet + IPv4 + TCP packet binary.

Accepts all ipv4/1 options plus:

  • :sport - source TCP port (default 54321)
  • :dport - destination TCP port (default 443)
  • :seq - TCP sequence number (default 1)
  • :ack - TCP acknowledgment number (default 0)
  • :flags - TCP flags byte (default 0x02 SYN)
  • :window - TCP window size (default 65535)
  • :payload - TCP payload binary (default <<>>)

tls_client_hello6(opts \\ [])

Builds an Ethernet + IPv6 + TCP packet carrying a TLS ClientHello payload.

tls_client_hello(opts \\ [])

Builds an Ethernet + IPv4 + TCP packet carrying a TLS ClientHello payload.

pkt = MaeBPF.Packet.tls_client_hello(sni: "evil.example.com")

Accepts all tcp/1 options (:flags defaults to 0x18 PSH+ACK, :dport defaults to 443) plus all tls_client_hello_payload/1 options (:sni, :ech, :ech_before_sni, :extra_extensions, :trailing_extensions, :split_before_sni, :truncate_to).

tls_client_hello_payload(opts \\ [])

Builds a raw TLS ClientHello record binary for testing SNI, ECH, PQ, and segmented ClientHello handling.

payload = MaeBPF.Packet.tls_client_hello_payload(sni: "evil.example.com")

Options:

  • :sni - hostname string (or nil to omit the SNI extension)
  • :ech - include TLS extension 0xFE0D (default false)
  • :ech_before_sni - place 0xFE0D before 0x0000 (default false)
  • :extra_extensions - list of {type_u16, data_bin} prepended before SNI
  • :trailing_extensions - list of {type_u16, data_bin} appended after SNI
  • :omit_extensions_header - omit the 2-byte extensions length when no extensions are present (default false)
  • :split_before_sni - truncate the TLS record just before the SNI extension while keeping the full announced TLS record length (default false)
  • :truncate_to - explicit maximum byte length of the returned TLS record

udp(opts \\ [])

Builds an Ethernet + IPv4 + UDP packet binary.

Accepts all ipv4/1 options plus:

  • :sport - source UDP port (default 54321)
  • :dport - destination UDP port (default 53)
  • :payload - UDP payload binary (default <<>>)

wrap_vlan(arg, vlan_id, tpid \\ 33024)

Wraps an Ethernet frame with an 802.1Q VLAN tag (tpid defaults to 0x8100).