Skip to content
AnswerWell geo Documentation

Research · 6 min read · Answerwell

eBPF Flow Protection State Tables, Packet Parsing, and Helper APIs

How eBPF flow protection state tables, challenges, parsing, and hash helpers work in advanced DDoS programmable programs.

This page summarises Cloudflare documentation on Programmable Flow Protection: eBPF programs that inspect and act on packets during advanced DDoS mitigation. The platform exposes state tables keyed by source IP and by connection flow, plus helper functions for parsing, cryptography, challenges, entropy, and analytics tagging.

When you need Programmable Flow Protection helpers

Section titled “When you need Programmable Flow Protection helpers”

You need these APIs when you write a custom eBPF program that must remember clients across packets, fingerprint payloads, issue challenges, or label traffic for Network Analytics—without implementing hashing or state storage yourself on the data plane.

Typical situations:

  • Rate-limit or block abusive source IPs after a challenge or verification step.
  • Track per-flow counters or flags using the 4-tuple (source IP, source port, destination IP, destination port).
  • Parse IP/UDP headers safely under the eBPF verifier.
  • Derive digests or HMACs for protocol-specific checks.

The helpers run in the context of the current packet; most return error codes when context is missing, tables are disabled, or buffers are invalid.

Source IP state table: status, expiry, and custom data

Section titled “Source IP state table: status, expiry, and custom data”

The source IP state table stores one entry per source IP with a status enum and optional user data (u64).

The source IP state table stores state keyed by source IP address. Each entry contains:

Field Type Description Status Enum The status of the source IP: None (0), Challenged (1), Verified (2), or Blocklisted (3). User data u64 A user-defined value you can set for any purpose.

The default maximum capacity is 1,000 entries.

— Cloudflare, Programmable Flow Protection

An entry appears when your program:

  • calls set_src_ip_status with Challenged, Verified, or Blocklisted;
  • calls set_src_ip_data to store custom data; or
  • calls set_challenge for a source IP that has no existing row yet.

Use get_src_ip_status / set_src_ip_status for lifecycle; use get_src_ip_data / set_src_ip_data for arbitrary u64 metadata (counters, flags, timestamps you encode yourself).

Source IP status values and set_src_ip_status

Section titled “Source IP status values and set_src_ip_status”

get_src_ip_status fills status (e.g. CF_EBPF_SRC_IP_STATUS_CHALLENGED, CF_EBPF_SRC_IP_STATUS_VERIFIED, CF_EBPF_SRC_IP_STATUS_BLOCKLISTED) and an expiry timestamp. Either pointer may be null if you only need one field.

set_src_ip_status takes expiry_secs; 0 means the status never expires.

Common return codes:

Code Typical meaning
0 Success
-1 No entry for this source IP (get_* only)
-2 No source IP context on the current packet
-5 Source IP state table not enabled

get_src_ip_status also returns -4 if both status and expiry are null.

The flow state table keys state by the full 4-tuple and stores a single u64 per flow.

The flow state table stores state keyed by the 4-tuple: source IP, source port, destination IP, and destination port. Each entry contains a u64 value you can set for any purpose.

The default maximum capacity is 10,000 entries.

— Cloudflare, Programmable Flow Protection

Entries are created when the program calls set_flow_data. Read with get_flow_data. Failures mirror the source-IP helpers: -1 no entry, -2 no flow context, -5 table disabled; get_flow_data also requires a non-null, aligned data pointer (-4).

Use the flow table for per-session logic (packet counts, phase of a handshake) and the source IP table when behaviour should apply to all traffic from that client.

parse_packet_data: safe header access in eBPF

Section titled “parse_packet_data: safe header access in eBPF”

Before reading headers manually, call parse_packet_data to build validated pointers:

Constructs cf_ebpf_parsed_headers from cf_ebpf_generic_ctx and cf_ebpf_packet_data. Performs required memory checks to pass the verifier.

— Cloudflare, Programmable Flow Protection

static inline int parse_packet_data(
struct cf_ebpf_generic_ctx *ctx,
struct cf_ebpf_packet_data **out_p,
struct cf_ebpf_parsed_headers *out_headers
);

Returns 0 on success (valid IP and UDP header pointers in out_headers), 1 on failure (e.g. packet too short or invalid length). Skipping this step often fails verification or causes unsafe reads.

set_challenge: send a challenge and affect source IP state

Section titled “set_challenge: send a challenge and affect source IP state”

set_challenge attaches challenge bytes to the response path for the current packet’s client:

Sets challenge data for the current packet. Use this to send a challenge packet back to the client.

— Cloudflare, Programmable Flow Protection

  • src_len 0 resets the challenge buffer.
  • Success is 0.
  • -4 invalid buffer or over max size; -5 challenges disabled; -6 rate limited (too recent to this source IP or global limit).

Calling set_challenge for a new source IP can create a source IP state table entry even before you set an explicit status—coordinate with set_src_ip_status so clients are not stuck in the wrong state.

Hash and HMAC helpers: buffers, sizes, and errors

Section titled “Hash and HMAC helpers: buffers, sizes, and errors”

Cloudflare exposes MD5, SHA-256, SHA-512, CRC32 (as uint64_t), BLAKE2b-512, and matching HMAC variants. The pattern is the same:

  • dest must meet minimum length (16 / 32 / 64 bytes depending on algorithm).
  • Success: positive byte count written.
  • -1 invalid source buffer; -2 null or undersized destination (CRC32: null dest pointer).

HMAC helpers (hmac_sha256, hmac_sha512, hmac_blake2b512) use a platform-configured private key:

The private key is configured at the platform level and is not exposed directly to the BPF program. The key is unique per server and per customer.

— Cloudflare, Programmable Flow Protection

You can authenticate or derive tags without embedding secrets in the program, but you cannot export or rotate the key from eBPF—design logic around “HMAC this payload” not “use my static key.”

hash_crc32 is a convenience wrapper that stores the CRC in a uint64_t (always 8 bytes written internally).

For unpredictable values (sampling, nonces), use rand(), which returns a random uint64_t.

entropy: detect uniform vs repetitive payloads

Section titled “entropy: detect uniform vs repetitive payloads”

entropy returns Shannon entropy in millibits from 0 (identical bytes) to 8000 (uniform byte distribution):

Calculates the Shannon entropy of the source buffer. The result is returned in millibits, ranging from 0 (all identical bytes) to 8000 (all 256 byte values equally distributed).

— Cloudflare, Programmable Flow Protection

Useful to flag likely encrypted or compressed payloads versus filler or reflective attack bodies; invalid buffers return -1.

set_network_analytics_tag: label samples in Network Analytics

Section titled “set_network_analytics_tag: label samples in Network Analytics”

Sets a custom tag for network analytics reporting. The tag appears along with the packet sample in the Network Analytics dashboard. By default, packets are sampled at 1/10,000 rate. Only one tag is set per program execution. If program execution calls set_network_analytics_tag multiple times, the last tag value applies to the packet sample.

— Cloudflare, Programmable Flow Protection

Tags help correlate dashboard samples with branches in your program (drop reason, challenge issued, etc.). Remember sampling: most packets never appear in the dashboard.

cf_ebpf_generic_ctx: program entry context

Section titled “cf_ebpf_generic_ctx: program entry context”

Every program receives:

struct cf_ebpf_generic_ctx {
uint64_t data; /* start of context data */
uint64_t data_end; /* end of context data */
uint64_t meta_data; /* program metadata scratch */
};

Pass this ctx into parse_packet_data and follow bounds checks implied by data / data_end.

Common mistakes when using state tables and helpers

Section titled “Common mistakes when using state tables and helpers”
  • Calling status/data getters without source IP or flow context on the packet (-2).
  • Assuming Verified persists forever when expiry_secs was meant to be non-zero, or using 0 expiry when you wanted TTL.
  • Mixing flow and source IP state for the same policy (e.g. blocking on flow count but forgetting to blocklist the IP).
  • Undersized hash output buffers (-2) or ignoring -6 on challenges and interpreting it as client malice.
  • Setting analytics tags in tight loops and expecting every packet to be visible (1/10,000 sampling).
  • Multiple set_network_analytics_tag calls expecting multiple tags—only the last applies.
  1. Confirm Programmable Flow Protection and required state tables (source IP, flow) are enabled for your advanced DDoS configuration per Programmable Flow Protection.
  2. Deploy a minimal program that sets a distinct set_network_analytics_tag value on a test branch and trigger that branch from lab traffic.
  3. In Network Analytics, look for sampled packets carrying your tag (expect sparse hits due to default 1/10,000 sampling).
  4. Exercise challenge paths: verify set_challenge success (0) and that get_src_ip_status reflects Challenged or your intended status after client response.
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page does not match the Programmable Flow Protection docs or your deployment behaviour, email yimasynthetic@gmail.com with the page URL and the exact statement you want clarified or corrected.