This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
securitycritical PriorityID: ebpf-xdp-smartnic-syn-cookie-offload-standard

eBPF XDP SmartNIC Stateless SYN-Cookie Offload Standard

Enforces completely stateless L4 SYN flood mitigation executed directly in SmartNIC hardware (XDP offload mode) or native driver space prior to kernel SKB allocation. Prohibits stateful map lookups or allocations during TCP SYN ingestion, mandating deterministic cookie generation and verification via cryptographic helpers with zero host memory overhead.

Prohibited Anti-Pattern: Allocating BPF map state on inbound TCP SYN packets before cookie verification

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: eBPF XDP SmartNIC Stateless SYN-Cookie Offload Standard - Enforces completely stateless L4 SYN flood mitigation executed directly in SmartNIC hardware (XDP offload mode) or native driver space prior to kernel SKB allocation. Prohibits stateful map lookups or allocations during TCP SYN ingestion, mandating deterministic cookie generation and verification via cryptographic helpers with zero host memory overhead.
globs: **/*.bpf.c, **/*.bpf.h, src/**/*.bpf.c, bpf/**/*.c, xdp/**/*.c
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/ebpf-xdp-smartnic-syn-cookie-offload-standard"
---

# eBPF XDP SmartNIC Stateless SYN-Cookie Offload Standard

> **Severity**: CRITICAL | **Category**: security
> **Anti-Pattern Warning**: Allocating BPF map state on inbound TCP SYN packets before cookie verification

## System Constraints
You are a Principal Systems and Kernel Security Architect enforcing strict stateless SYN-cookie handling in eBPF/XDP programs targeted for SmartNIC hardware offload. Your mission is to prevent host memory exhaustion (sk_buff allocations and BPF hash table explosion) during multi-terabit volumetric L4 attacks by strictly mandating stateless cryptographic SYN verification at the NIC ingress layer.

## Strict Guidelines
- Engineers MUST ALWAYS process TCP SYN and ACK validation statelessly using hardware-compatible crypto primitives or standard BPF helpers (e.g., bpf_tcp_raw_gen_syncookie_ipv4/bpf_tcp_raw_check_syncookie_ipv4) without creating map entries during the SYN phase.
- Engineers MUST NOT invoke bpf_map_update_elem or allocate memory structures upon receiving unverified TCP SYN packets, preventing BPF map saturation and NIC SRAM exhaustion.
- Code MUST strictly verify packet bounds against ctx->data and ctx->data_end prior to any header pointer dereferencing to satisfy the eBPF verifier and SmartNIC JIT compiler constraints.
- Services MUST emit SYN-ACK responses directly out of ingress interfaces using XDP_TX after swapping L2 MAC addresses, L3 IP addresses, and recalculating L3/L4 checksums incrementally.
- Components NEVER drop or pass valid three-way handshakes to the host kernel without validating the cookie timestamp, MSS encoding, and cryptographically signed sequence number in the final ACK.
- Code SHOULD enforce dual-stack IPv4 and IPv6 handling, ensuring IPv6 flow labels and extension headers are parsed deterministically without unbounded loops.

## Bad vs Good Implementations
### Stateless SYN-Cookie Generation vs. Stateful Map Tracking in XDP

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Allocates state directly in a BPF hash map upon receiving an unverified TCP SYN frame. An attacker can saturate the map capacity (1,024,000 entries) in milliseconds during a volumetric attack, causing legitimate traffic drops and depleting kernel memory. Furthermore, BPF hash maps are generally incompatible with pure SmartNIC hardware offload modes.
```c
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <bpf/bpf_helpers.h>

struct {
    __uint(type, BPF_MAP_TYPE_HASH);
    __uint(max_entries, 1024000);
    __type(key, __u64); // 4-tuple hash
    __type(value, __u32); // connection state
} conntrack_map SEC(".maps");

SEC("xdp")
int xdp_syn_filter(struct xdp_md *ctx) {
    void *data = (void *)(long)ctx->data;
    void *data_end = (void *)(long)ctx->data_end;
    struct ethhdr *eth = data;
    if ((void *)(eth + 1) > data_end)
        return XDP_PASS;

    struct iphdr *iph = (void *)(eth + 1);
    if ((void *)(iph + 1) > data_end || iph->protocol != IPPROTO_TCP)
        return XDP_PASS;

    struct tcphdr *tcp = (void *)iph + (iph->ihl * 4);
    if ((void *)(tcp + 1) > data_end)
        return XDP_PASS;

    // CRITICAL FLAW: Stateful allocation on incoming SYN packet
    // Vulnerable to map exhaustion DoS; breaks SmartNIC HW offload
    if (tcp->syn && !tcp->ack) {
        __u64 flow_id = ((__u64)iph->saddr << 32) | tcp->source;
        __u32 state = 1;
        bpf_map_update_elem(&conntrack_map, &flow_id, &state, BPF_ANY);
        return XDP_PASS;
    }
    return XDP_PASS;
}
```

#### Production Standard (Correct)
> **Standard**: Implements pure stateless defense using `bpf_tcp_raw_gen_syncookie_ipv4` and `bpf_tcp_raw_check_syncookie_ipv4`. SYN packets trigger an immediate `XDP_TX` SYN-ACK response calculated completely statelessly in-place. Connection state is strictly prohibited until a mathematically verified ACK returns, protecting host CPU and RAM from allocation exhaustion.
```c
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>

SEC("xdp")
int xdp_syn_cookie_offload(struct xdp_md *ctx) {
    void *data = (void *)(long)ctx->data;
    void *data_end = (void *)(long)ctx->data_end;

    struct ethhdr *eth = data;
    if ((void *)(eth + 1) > data_end)
        return XDP_PASS;

    if (eth->h_proto != bpf_htons(ETH_P_IP))
        return XDP_PASS;

    struct iphdr *iph = (void *)(eth + 1);
    if ((void *)(iph + 1) > data_end || iph->protocol != IPPROTO_TCP)
        return XDP_PASS;

    __u32 ip_hdr_len = iph->ihl * 4;
    if (ip_hdr_len < sizeof(struct iphdr))
        return XDP_DROP;

    struct tcphdr *tcp = (void *)iph + ip_hdr_len;
    if ((void *)(tcp + 1) > data_end)
        return XDP_PASS;

    // Stateless SYN evaluation: generate cookie without host memory allocation
    if (tcp->syn && !tcp->ack) {
        __s64 cookie = bpf_tcp_raw_gen_syncookie_ipv4(iph, tcp, sizeof(*tcp));
        if (cookie < 0)
            return XDP_DROP;

        // Reflect packet: swap L2 MACs and L3 IPs, convert to SYN-ACK, set seq = cookie
        unsigned char tmp_mac[ETH_ALEN];
        __builtin_memcpy(tmp_mac, eth->h_source, ETH_ALEN);
        __builtin_memcpy(eth->h_source, eth->h_dest, ETH_ALEN);
        __builtin_memcpy(eth->h_dest, tmp_mac, ETH_ALEN);

        __be32 tmp_ip = iph->saddr;
        iph->saddr = iph->daddr;
        iph->daddr = tmp_ip;

        tcp->ack_seq = bpf_htonl(bpf_ntohl(tcp->seq) + 1);
        tcp->seq = bpf_htonl((__u32)cookie);
        tcp->ack = 1;
        tcp->syn = 1;

        // Recalculate checksums statelessly or flag for hardware csum offload
        iph->check = 0;
        iph->check = bpf_csum_diff(0, 0, (__be32 *)iph, sizeof(*iph), 0);
        tcp->check = 0; // Offloaded or updated via pseudo-header csum

        return XDP_TX;
    }

    // Stateless ACK verification on 3-way handshake completion
    if (tcp->ack && !tcp->syn) {
        int res = bpf_tcp_raw_check_syncookie_ipv4(iph, tcp);
        if (res == 0)
            return XDP_PASS; // Valid cookie: allow host OS to create SKB and socket
        else
            return XDP_DROP; // Forged or invalid cookie: discard at NIC wire rate
    }

    return XDP_PASS;
}
```

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: eBPF XDP SmartNIC Stateless SYN-Cookie Offload Standard

eBPF XDP SmartNIC Stateless SYN-Cookie Offload Standard defines an industrial-grade behavioral standard for security engineering. Enforces completely stateless L4 SYN flood mitigation executed directly in SmartNIC hardware (XDP offload mode) or native driver space prior to kernel SKB allocation. Prohibits stateful map lookups or allocations during TCP SYN ingestion, mandating deterministic cookie generation and verification via cryptographic helpers with zero host memory overhead. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Allocating BPF map state on inbound TCP SYN packets before cookie verification Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftAllocating BPF map state on inbound TCP SYN packets before cookie verificationCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS process TCP SYN and ACK validation statelessly using hardware-compatible crypto primitives or standard BPF helpers (e.g., bpf_tcp_raw_gen_syncookie_ipv4/bpf_tcp_raw_check_syncookie_ipv4) without creating map entries during the SYN phase.

Engineers MUST ALWAYS process TCP SYN and ACK validation statelessly using hardware-compatible crypto primitives or standard BPF helpers (e.g., bpf_tcp_raw_gen_syncookie_ipv4/bpf_tcp_raw_check_syncookie_ipv4) without creating map entries during the SYN phase.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT invoke bpf_map_update_elem or allocate memory structures upon receiving unverified TCP SYN packets, preventing BPF map saturation and NIC SRAM exhaustion.

Engineers MUST NOT invoke bpf_map_update_elem or allocate memory structures upon receiving unverified TCP SYN packets, preventing BPF map saturation and NIC SRAM exhaustion.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Code MUST strictly verify packet bounds against ctx->data and ctx->data_end prior to any header pointer dereferencing to satisfy the eBPF verifier and SmartNIC JIT compiler constraints.

Code MUST strictly verify packet bounds against ctx->data and ctx->data_end prior to any header pointer dereferencing to satisfy the eBPF verifier and SmartNIC JIT compiler constraints.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST emit SYN-ACK responses directly out of ingress interfaces using XDP_TX after swapping L2 MAC addresses, L3 IP addresses, and recalculating L3/L4 checksums incrementally.

Services MUST emit SYN-ACK responses directly out of ingress interfaces using XDP_TX after swapping L2 MAC addresses, L3 IP addresses, and recalculating L3/L4 checksums incrementally.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER drop or pass valid three-way handshakes to the host kernel without validating the cookie timestamp, MSS encoding, and cryptographically signed sequence number in the final ACK.

Components NEVER drop or pass valid three-way handshakes to the host kernel without validating the cookie timestamp, MSS encoding, and cryptographically signed sequence number in the final ACK.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Code SHOULD enforce dual-stack IPv4 and IPv6 handling, ensuring IPv6 flow labels and extension headers are parsed deterministically without unbounded loops.

Code SHOULD enforce dual-stack IPv4 and IPv6 handling, ensuring IPv6 flow labels and extension headers are parsed deterministically without unbounded loops.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is eBPF XDP SmartNIC Stateless SYN-Cookie Offload Standard critical for AI coding assistants?

Enforces completely stateless L4 SYN flood mitigation executed directly in SmartNIC hardware (XDP offload mode) or native driver space prior to kernel SKB allocation. Prohibits stateful map lookups or allocations during TCP SYN ingestion, mandating deterministic cookie generation and verification via cryptographic helpers with zero host memory overhead. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Allocating BPF map state on inbound TCP SYN packets before cookie verification) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

eBPF