This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: cilium-ebpf-sockops-tcp-quarantine

Cilium eBPF Sockops Kernel-Level TCP Half-Open Connection Quarantine

Mandates kernel-level interception of TCP socket state transitions via Cilium eBPF sockops programs to detect and terminate orphaned SYN-ACK handshakes in the kernel. This prevents upstream Envoy and ingress proxies from exhausting epoll file descriptors and listen backlogs under connection churn and SYN-flood anomalies.

Prohibited Anti-Pattern: Passive Userspace Handshake Timeout Reliance Leading to Ingress Epoll Starvation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Cilium eBPF Sockops Kernel-Level TCP Half-Open Connection Quarantine - Mandates kernel-level interception of TCP socket state transitions via Cilium eBPF sockops programs to detect and terminate orphaned SYN-ACK handshakes in the kernel. This prevents upstream Envoy and ingress proxies from exhausting epoll file descriptors and listen backlogs under connection churn and SYN-flood anomalies.
globs: bpf/**/*.c, bpf/**/*.h, pkg/ebpf/**/*.go, install/kubernetes/**/*.yaml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/cilium-ebpf-sockops-tcp-quarantine"
---

# Cilium eBPF Sockops Kernel-Level TCP Half-Open Connection Quarantine

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Passive Userspace Handshake Timeout Reliance Leading to Ingress Epoll Starvation

## System Constraints
You are a Principal Systems Architect and Linux Kernel/eBPF Specialist enforcing Cilium eBPF Socket-Level TCP Half-Open Connection Quarantine. You must enforce that TCP connection establishment states are monitored directly inside the kernel via BPF sockops hooks. You strictly prohibit delegating half-open connection timeouts to user-space L7 proxies (e.g., Envoy, HAProxy) which risk epoll file descriptor exhaustion and latency spikes under SYN floods or unacknowledged SYN-ACK states.

## Strict Guidelines
- Engineers MUST attach eBPF sockops programs to cgroups using BPF_PROG_TYPE_SOCK_OPS and handle BPF_SOCK_OPS_STATE_CB callbacks explicitly.
- Engineers MUST NOT rely on userspace proxy idle timeouts or TCP keepalives to prune half-open connections that never complete the 3-way handshake.
- Code MUST track SYN_RECV socket dwell times within a kernel BPF_MAP_TYPE_LRU_HASH map storing high-resolution timestamps (bpf_ktime_get_ns).
- Services MUST inject an active TCP reset (RST) or purge orphaned connections via sockops flags (e.g., BPF_SOCK_OPS_RST_EBPF_FLAG) before handshakes propagate to upstream epoll runloops.
- Components NEVER allow unbounded eBPF map sizing; all quarantine and socket tracking structures MUST specify explicit LRU bounds verified by the kernel verifier.

## Bad vs Good Implementations
### Kernel eBPF Sockops TCP Handshake State Interception

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Failing to intercept state transitions into TCP_SYN_RECV allows orphaned handshakes to linger during asymmetric network failures or SYN attacks. Upstream proxy processes accumulate pending connection handles in listen queues and epoll structures, leading to EMFILE errors and CPU thrashing.
```c
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

// ANTI-PATTERN: Passive monitoring that ignores SYN-ACK dwell
// Handshakes linger in TCP_SYN_RECV until kernel tcp_synack_retries
// upstream proxy epoll file descriptors exhaust under high churn
SEC("sockops")
int handle_sock_ops(struct bpf_sock_ops *skops) {
    __u32 op = skops->op;

    if (op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
        op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) {
        bpf_printk("TCP connection established: %d", skops->remote_port);
    }

    // Omits BPF_SOCK_OPS_STATE_CB tracking for TCP_SYN_RECV
    return 1;
}

char _license[] SEC("license") = "GPL";
```

#### Production Standard (Correct)
> **Standard**: The eBPF program hooks directly into `BPF_SOCK_OPS_STATE_CB`, recording the entry timestamp of sockets in `BPF_TCP_SYN_RECV`. By enabling `BPF_SOCK_OPS_RTO_CB_FLAG` and aborting stale handshakes directly at the first retransmit timeout in the kernel, it eliminates socket accumulation before epoll queues in userspace L7 proxies ever see the connection.
```c
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>

#define BPF_TCP_SYN_RECV 2
#define MAX_HANDSHAKE_NS 1500000000ULL // 1.5s timeout

struct handshake_key {
    __u32 src_ip;
    __u32 dst_ip;
    __u16 src_port;
    __u16 dst_port;
};

struct {
    __uint(type, BPF_MAP_TYPE_LRU_HASH);
    __uint(max_entries, 65536);
    __type(key, struct handshake_key);
    __type(value, __u64);
} syn_dwell_map SEC(".maps");

SEC("sockops")
int handle_sock_ops(struct bpf_sock_ops *skops) {
    __u32 op = skops->op;

    if (op == BPF_SOCK_OPS_STATE_CB) {
        int old_state = skops->args[0];
        int new_state = skops->args[1];

        if (new_state == BPF_TCP_SYN_RECV) {
            struct handshake_key key = {
                .src_ip   = skops->remote_ip4,
                .dst_ip   = skops->local_ip4,
                .src_port = bpf_ntohl(skops->remote_port),
                .dst_port = skops->local_port,
            };
            __u64 now = bpf_ktime_get_ns();
            bpf_map_update_elem(&syn_dwell_map, &key, &now, BPF_ANY);

            // Enable RTO and state callbacks for immediate enforcement
            bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_STATE_CB_FLAG | BPF_SOCK_OPS_RTO_CB_FLAG);
        } else if (old_state == BPF_TCP_SYN_RECV) {
            struct handshake_key key = {
                .src_ip   = skops->remote_ip4,
                .dst_ip   = skops->local_ip4,
                .src_port = bpf_ntohl(skops->remote_port),
                .dst_port = skops->local_port,
            };
            bpf_map_delete_elem(&syn_dwell_map, &key);
        }
    } else if (op == BPF_SOCK_OPS_RTO_CB) {
        // Handshake exceeded retransmit budget in half-open state; abort in-kernel
        skops->reply = -1; // Abort and purge socket before epoll exposure
        return -1;
    }

    return 1;
}

char _license[] SEC("license") = "GPL";
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Cilium eBPF Sockops Kernel-Level TCP Half-Open Connection Quarantine

Cilium eBPF Sockops Kernel-Level TCP Half-Open Connection Quarantine defines an industrial-grade behavioral standard for devops engineering. Mandates kernel-level interception of TCP socket state transitions via Cilium eBPF sockops programs to detect and terminate orphaned SYN-ACK handshakes in the kernel. This prevents upstream Envoy and ingress proxies from exhausting epoll file descriptors and listen backlogs under connection churn and SYN-flood anomalies. 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: Passive Userspace Handshake Timeout Reliance Leading to Ingress Epoll Starvation 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 DriftPassive Userspace Handshake Timeout Reliance Leading to Ingress Epoll StarvationCritical
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 attach eBPF sockops programs to cgroups using BPF_PROG_TYPE_SOCK_OPS and handle BPF_SOCK_OPS_STATE_CB callbacks explicitly.

Engineers MUST attach eBPF sockops programs to cgroups using BPF_PROG_TYPE_SOCK_OPS and handle BPF_SOCK_OPS_STATE_CB callbacks explicitly.

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 rely on userspace proxy idle timeouts or TCP keepalives to prune half-open connections that never complete the 3-way handshake.

Engineers MUST NOT rely on userspace proxy idle timeouts or TCP keepalives to prune half-open connections that never complete the 3-way handshake.

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 track SYN_RECV socket dwell times within a kernel BPF_MAP_TYPE_LRU_HASH map storing high-resolution timestamps (bpf_ktime_get_ns).

Code MUST track SYN_RECV socket dwell times within a kernel BPF_MAP_TYPE_LRU_HASH map storing high-resolution timestamps (bpf_ktime_get_ns).

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 inject an active TCP reset (RST) or purge orphaned connections via sockops flags (e.g., BPF_SOCK_OPS_RST_EBPF_FLAG) before handshakes propagate to upstream epoll runloops.

Services MUST inject an active TCP reset (RST) or purge orphaned connections via sockops flags (e.g., BPF_SOCK_OPS_RST_EBPF_FLAG) before handshakes propagate to upstream epoll runloops.

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 allow unbounded eBPF map sizing

Components NEVER allow unbounded eBPF map sizing; all quarantine and socket tracking structures MUST specify explicit LRU bounds verified by the kernel verifier.

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 Cilium eBPF Sockops Kernel-Level TCP Half-Open Connection Quarantine critical for AI coding assistants?

Mandates kernel-level interception of TCP socket state transitions via Cilium eBPF sockops programs to detect and terminate orphaned SYN-ACK handshakes in the kernel. This prevents upstream Envoy and ingress proxies from exhausting epoll file descriptors and listen backlogs under connection churn and SYN-flood anomalies. 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 (Passive Userspace Handshake Timeout Reliance Leading to Ingress Epoll Starvation) 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

Cilium