Programmable Flow Protection (Beta)

Last updated Oct 21, 2026 Programmable Flow Protection is a DDoS protection system that protects against DDoS attacks over custom or standardized Layer 7 UDP-based protocols, such as gaming protocols, financial services protocols, VoIP, telecom, and streaming. In terms of topology, it supports both asymmetric and symmetric configurations, but it will only inspect ingress traffic.

Programmable Flow Protection is currently in closed beta and available as an add-on for the Magic Transit (BYOIP or Cloudflare-leased IPs) service only. If you would like to enable the system, contact your account team or fill out this form ↗︎.

How it works

The Programmable Flow Protection system allows you to write and run your own packet-layer stateful program in C across Cloudflare's global anycast network as extended Berkeley Packet Filter (eBPF) programs running in the user space. An eBPF program ↗︎ is a packet filter system that allows a developer to write performant custom networking logic.

Programmable Flow Protection inspects and parses your UDP-based application's protocols (deep packet inspection) and determines the outcome of the packets based on your program. Using your custom program's logic, you can permit authorized users while actively blocking attacks.

The system is built on top of the flowtrackd platform, Cloudflare's stateful mitigation platform. The Programmable Flow Protection system relies on the DDoS Advanced Protection system's general settings to operate. It respects the prefixes that you have selected to route through the Advanced Protection systems, as well as the allowlist. The Advanced DDoS Protection system should be enabled for the Programmable Flow Protection system to operate.

While in beta, Cloudflare will assist and provide guidance to users to write their own code. Out-of-the-box code snippets (templates) for popular gaming protocols and VoIP protocols may be provided later on.


Get started

After Programmable Flow Protection has been enabled to your account, go to Networking > L3/4 DDoS Protection > Advanced Protection in the Cloudflare dashboard. Within the Programmable Flow Protection tab:

  1. Upload your eBPF program written in C.

The program is validated by the system and stored in your account. The API compiles the program, then runs a verifier against the compiled program to enforce memory checks and verify program termination. If the program fails compilation or verification, the Cloudflare dashboard will return a detailed error message. 2. Create a rule 3. To observe the program's behavior, check the Network Analytics dashboard and select the Programmable Flow Protection tab.

You can create additional rules with different rule settings scoped to various regions and Cloudflare locations to change the mode (Mitigation or Monitoring) to accommodate for your traffic patterns and business use cases.

The Programmable Flow Protection system supports the Data Localization suite.

Beta functionality limitations

For more information on beta services, refer to section 2.6 in the Enterprise Terms of Service ↗︎.

Write a basic program

The steps below write a sample program that drops all User Datagram Protocol (UDP) traffic with an IPv6 header. It also drops traffic destined to port 66, as well as traffic that does not have some custom specific application header value in the UDP payload.

  1. Add a define directive to specify the versioned helper functions in use.

As Cloudflare adds more features to the Programmable Flow Protection API, we will publish new versions of its API. Versions are guaranteed to be backwards compatible.

   #define CF_EBPF_HELPER_V0
   ```

2. Include the Cloudflare eBPF header files.

These files have [helper functions](#helper-functions) to parse the input packet data to the BPF program.

```c
   #include <cf_ebpf_defs.h>
   #include <cf_ebpf_helper.h>
   ```

3. Define the entry function for packet processing.

Your program must have the exact function signature below to properly pass Cloudflare's program verification.

The return type `uint64_t` dictates whether Cloudflare will pass or drop a packet. The function name `cf_ebpf_main` is used as the entrypoint to the program. The argument `void *state` refers to the data Cloudflare provides as input to your BPF program.

```c
   uint64_t cf_ebpf_main(void *state)
   ```

4. Cast the input argument into usable structs.

Convert the input data into `cf_ebpf_generic_ctx`, which tells Cloudflare the data boundaries in the memory that we are reading.

Then, declare variables for data parsing. `cf_ebpf_parsed_headers` will contain the IPv4, IPv6, and UDP headers. `cf_ebpf_packet_data` will hold a copy of the original IP packet that Cloudflare received (maximum 1,500 bytes), as well as the packet length and IP header length.

```c
   struct cf_ebpf_generic_ctx *ctx = state;
   struct cf_ebpf_parsed_headers headers;
   struct cf_ebpf_packet_data *p;
   ```

5. Fill variables by calling the helper function.

You must fill in the variables by calling the helper function `parse_packet_data`, which Cloudflare has provided in a header file included in step 2.

The `parse_packet_data` function performs the memory checks required to pass the program verifier. The `parse_packet_data` function returns `0` on success. If it is successful, the input parameters are correctly populated. The `parse_packet_data` function returns `1` on failure. If `parse_packet_data` fails, The program must return `CF_EBPF_DROP` to drop the packet in order to pass the verifier.

```c
   if (parse_packet_data(ctx, &p, &headers) != 0) {
       return CF_EBPF_DROP;
   }
   ```

Available values after successful parsing:

```c
   struct cf_ebpf_packet_data {
        /* Total length of the packet. */
        size_t   total_packet_length;
        /* Size of the IP header. Supports IPv4 (including options) and IPv6. */
        size_t   ip_header_length;
        /* Bytes of the packet, starting with the IP header. */
        uint8_t  packet_buffer[1500];
   };

struct cf_ebpf_parsed_headers {
        /* Pointer to the parsed IPv4 header, if present (otherwise null). */
        struct iphdr   *ipv4;
        /* Pointer to the parsed IPv6 header, if present (otherwise null). */
        struct ipv6hdr *ipv6;
        /* Pointer to the parsed UDP header. */
        struct udphdr  *udp;
        /* Raw pointer to the last valid byte of the packet context data. */
        uint8_t        *data_end;
   };
   ```

For a full definition of helper functions and structures, refer to [BPF helper functions and structures](#bpf-helper-functions-and-structures).

Note

The UDP header size is fixed at 8 bytes, but the IP header size varies: IPv4 headers range from 20 to 60 bytes depending on options, and IPv6 headers are 40 bytes. Use the `parse_packet_data` function to retrieve UDP payload contents instead of calculating fixed-length offsets. The parsed `udp` pointer in `cf_ebpf_parsed_headers` points directly to the UDP header regardless of IP version or options.
6. Write your custom logic.

Prior steps have established the code that should be the same for any program that you write, regardless of its logic.

Now, you can write your own custom logic.

Note

Programmable Flow Protection will only give UDP packets to a BPF program.

In the example snippet below, the program will drop any packet where the IPv6 header exists or where the UDP destination port is 66.

It will then check the application header value in the UDP payload and verify its last byte is a fixed value `0xCF`.

```c
    struct ipv6hdr *ipv6_hdr;
    struct udphdr *udp_hdr;
    ipv6_hdr = (struct ipv6hdr *)headers.ipv6;
    if (ipv6_hdr != NULL) {
      return CF_EBPF_DROP;
    }
    udp_hdr = (struct udphdr *)headers.udp;
    if (ntohs(udp_hdr->dest) == 66) {
        return CF_EBPF_DROP;
    }

struct apphdr *app = (struct apphdr *)(udp_hdr + 1);
    if ((uint8_t *)(app + 1) > headers.data_end) {
        return CF_EBPF_DROP;
    }

// The verifier has a special limit that it will not allow offsets
    // beyond 65535. We need this check (token_len > 64000) in order
    // to satisfy that, even though it is not possible.
    uint16_t token_len = app->length;
    if (token_len > 64000) {
        return CF_EBPF_DROP;
    }

if ((uint8_t *)(app->token + token_len) > headers.data_end) {
        return CF_EBPF_DROP;
    }

uint8_t *last_byte = app->token + token_len - 1;
    if (*last_byte != 0xCF) {
        return CF_EBPF_DROP;
    }
   ```

7. Pass any packets that did not get dropped by program logic by returning `CF_EBPF_PASS`.

The currently supported return values are:
   - `CF_EBPF_PASS = return value 0`
   - `CF_EBPF_DROP = return value 1`

The verifier, which runs when you upload a program to the API, will enforce that the program returns only known value types.

```c
   return CF_EBPF_PASS;
   ```

For reference, the example below is the basic program in its entirety:

```c
#define CF_EBPF_HELPER_V0

#include <cf_ebpf_defs.h>
#include <cf_ebpf_helper.h>

struct apphdr {
    uint8_t       version;
    uint16_t      length;   // Length of the variable-length token
    unsigned char token[0]; // Variable-length token
} __attribute__((packed));

uint64_t
cf_ebpf_main(void *state)
{
    struct cf_ebpf_generic_ctx *ctx = state;
    struct cf_ebpf_parsed_headers headers;
    struct cf_ebpf_packet_data *p;

if (parse_packet_data(ctx, &p, &headers) != 0) {
        return CF_EBPF_DROP;
    }
    struct ipv6hdr *ipv6_hdr;
    struct udphdr *udp_hdr;
    ipv6_hdr = (struct ipv6hdr *)headers.ipv6;
    if (ipv6_hdr != NULL) {
        return CF_EBPF_DROP;
    }

udp_hdr = (struct udphdr *)headers.udp;
    if (ntohs(udp_hdr->dest) == 66) {
        return CF_EBPF_DROP;
    }

struct apphdr *app = (struct apphdr *)(udp_hdr + 1);
    if ((uint8_t *)(app + 1) > headers.data_end) {
        return CF_EBPF_DROP;
    }

// The verifier has a special limit that it will not allow offsets
    // beyond 65535. We need this check (token_len > 64000) in order
    // to satisfy that, even though it is not possible.
    uint16_t token_len = app->length;
    if (token_len > 64000) {
        return CF_EBPF_DROP;
    }

if ((uint8_t *)(app->token + token_len) > headers.data_end) {
        return CF_EBPF_DROP;
    }

uint8_t *last_byte = app->token + token_len - 1;
    if (*last_byte != 0xCF) {
        return CF_EBPF_DROP;
    }
    return CF_EBPF_PASS;
}

Write a complex program: challenge-based response

The example program below implements a UDP-based challenge-response mechanism using helper functions to maintain state between packets from the same source IP. This is useful for mitigating DDoS attacks by requiring clients to prove they can receive and respond to challenges before allowing their traffic through.

The challenge mechanism works as follows:

When a packet arrives from an unknown source IP, the program generates a challenge packet containing a random nonce and marks the source IP as "challenged" in the state table. The original packet is dropped.

If a packet arrives from a source IP that has already been challenged, the program checks if the packet contains the correct challenge response (the nonce XORed with a secret value). If the response is correct, the source IP is marked as "verified". If incorrect, the source IP is immediately blocklisted.

Packets from verified source IPs are passed through without further checks.

  1. Include the Cloudflare eBPF header files and define the helper version.

```c #define CF_EBPF_HELPER_V0

include

#include ```

  1. Define constants for the challenge-response protocol.

The challenge response is computed by XORing the nonce with a secret value. The expiry time determines how long a challenged or verified status remains valid.

c #define CHALLENGE_SECRET 0xDEADBEEFCAFEBABEULL #define CHALLENGE_EXPIRY_SECS 60 #define VERIFIED_EXPIRY_SECS 3600

  1. Define a structure for challenge packets.

The challenge packet contains the nonce that the client must respond to, and space for the client's response.

c struct challenge_packet { uint64_t nonce; // Random nonce for this challenge uint64_t response; // Expected: nonce XOR CHALLENGE_SECRET };

  1. Define the entry function and parse the packet.

```c uint64_t cf_ebpf_main(void state) { struct cf_ebpf_generic_ctx ctx = state; struct cf_ebpf_parsed_headers headers; struct cf_ebpf_packet_data *p;

if (parse_packet_data(ctx, &p, &headers) != 0) { return CF_EBPF_DROP; }

struct udphdr *udp_hdr = headers.udp; ```

  1. Check the source IP status using get_src_ip_status.

The status indicates whether this source IP is new, challenged, verified, or blocklisted. The expiry timestamp indicates when the status expires.