Architectural Invariant Design: eBPF sock_ops L4 Direct Socket Redirection and Namespace Boundary Enforcement
eBPF sock_ops L4 Direct Socket Redirection and Namespace Boundary Enforcement defines an industrial-grade behavioral standard for devops engineering. Mandates strict network namespace (netns) cookie verification, socket state validation, and symmetric hash lifecycle cleanup in eBPF sock_ops and sk_msg programs. Prevents cross-tenant socket hijacking, policy enforcement bypasses, and kernel memory corruption during L4 TCP/IP short-circuiting in sidecarless mesh architectures. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
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 Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Blind 4-Tuple Socket Redirection Without Namespace Cookie Validation and State Cleanup | Critical | 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 Inconsistency | AI 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. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST ALWAYS validate network namespace isolation using `bpf_get_netns_cookie(skops)` before inserting sockets into `BPF_MAP_TYPE_SOCKHASH` or `BPF_MAP_TYPE_SOCKMAP`.
Engineers MUST ALWAYS validate network namespace isolation using `bpf_get_netns_cookie(skops)` before inserting sockets into `BPF_MAP_TYPE_SOCKHASH` or `BPF_MAP_TYPE_SOCKMAP`.
Engineers MUST NOT bypass the host network namespace boundary or allow unauthenticated intra-host redirection between isolated cgroups/pods.
Engineers MUST NOT bypass the host network namespace boundary or allow unauthenticated intra-host redirection between isolated cgroups/pods.
Code SHOULD include the 64-bit network namespace cookie as a composite key component alongside the 4-tuple (src_ip, dst_ip, src_port, dst_port) to prevent cross-netns hash collisions.
Code SHOULD include the 64-bit network namespace cookie as a composite key component alongside the 4-tuple (src_ip, dst_ip, src_port, dst_port) to prevent cross-netns hash collisions.
Services MUST explicitly handle `BPF_SOCK_OPS_STATE_CB` transitions and delete corresponding entries from the redirection maps upon `BPF_TCP_CLOSE` to prevent dangling references and socket reuse corruption.
Services MUST explicitly handle `BPF_SOCK_OPS_STATE_CB` transitions and delete corresponding entries from the redirection maps upon `BPF_TCP_CLOSE` to prevent dangling references and socket reuse corruption.
Components NEVER redirect raw sockets across transport boundaries without asserting `skops->family == AF_INET || skops->family == AF_INET6` and `skops->sk->sk_protocol == IPPROTO_TCP`.
Components NEVER redirect raw sockets across transport boundaries without asserting `skops->family == AF_INET || skops->family == AF_INET6` and `skops->sk->sk_protocol == IPPROTO_TCP`.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is eBPF sock_ops L4 Direct Socket Redirection and Namespace Boundary Enforcement critical for AI coding assistants?
Mandates strict network namespace (netns) cookie verification, socket state validation, and symmetric hash lifecycle cleanup in eBPF sock_ops and sk_msg programs. Prevents cross-tenant socket hijacking, policy enforcement bypasses, and kernel memory corruption during L4 TCP/IP short-circuiting in sidecarless mesh architectures. 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 (Blind 4-Tuple Socket Redirection Without Namespace Cookie Validation and State Cleanup) 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.