basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    How a packet moves through Linux: netns, veth, netfilter, and why tcpdump sees inbound traffic before the firewall

    Container networking in Linux runs on three kernel primitives — network namespaces (netns), virtual cable pairs (veth), and bridges (bridge) — while packet filtering happens in netfilter hooks. Here is how packets traverse the kernel and why tcpdump via AF_PACKET sees inbound packets before a firewall drop, yet misses dropped outbound ones.

    What you’ll get

    Build a container network by hand from kernel primitives (ip netns, veth, and a br-app bridge), add nftables drop rules on the host for inbound port 8080 and outbound 9090, and see what tcpdump captures:

    # The veth pair links the br-app bridge on the host (index 42) to eth0 inside the st_app namespace (index 41):
    ip -o link show veth-app
    # 42: veth-app@if41: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br-app state UP ... link-netns st_app
    
    ip netns exec st_app ip -o link show eth0
    # 41: eth0@if42: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP ... link-netns st_host
    
    # Inbound SYN to 8080 is dropped in input, outbound to 9090 in output:
    sudo tcpdump -nn -i br-app 'tcp port 8080 or tcp port 9090'
    # 20:05:31.293244 IP 10.0.50.10.37232 > 10.0.50.1.8080: Flags [S], seq 2136655328, win 64240, length 0
    # 20:05:32.322323 IP 10.0.50.10.37232 > 10.0.50.1.8080: Flags [S], seq 2136655328, win 64240, length 0
    # (no outbound traffic to port 9090 appears in the dump, even though the output drop counter grew by 2 packets)

    A single line of ip link show lays out the entire virtual cable between the host and a container:

    ip -o link show veth-app · Pick a segment
    A Docker container uses the exact same setup: an isolated netns, a veth pair, and a br-* bridge on the host.

    How container networking is built: netns, veth, and bridge

    Docker does not invent its own network subsystem — it combines three Linux kernel mechanisms (Docker networks):

    • netns (Network Namespace) — an isolated copy of the entire network stack: its own interfaces, its own 127.0.0.1, its own routing table, its own ARP/NDP cache, and its own nftables rules. Each container gets its own netns.
    • veth (Virtual Ethernet Pair) — a virtual patch cord made of two interfaces. A packet sent into one end comes straight out of the other. One end moves into the container’s netns as eth0; the other stays on the host (veth…).
    • bridge (docker0, br-*) — a software L2 switch in the kernel (MAC, switch, ARP, and VLAN). The host ends of veth pairs for all containers on a network plug into it; the bridge itself holds the gateway IP (such as 10.0.50.1/24) through which containers reach the outside via host NAT (NAT, DNAT, and conntrack).

    Packet path through the kernel: netfilter hooks and AF_PACKET

    Every packet entering or leaving Linux follows a fixed path across five netfilter hooks (where nftables, iptables, and ufw operate):

    Incoming frame (NIC / veth)
      │
      ├──► [AF_PACKET: tcpdump sees ALL inbound traffic here — BEFORE the firewall]
      ▼
    [prerouting]  (DNAT: e.g. Docker -p 8080:80 rewrites Dst IP to the container IP)
      │
      ▼
    Routing decision (ip route): local host or forwarded?
      ├── for this host ──► [input] ──────► local socket (nginx, sshd)
      │                                            │
      │                                            ▼ (app reply)
      │                                     [output] (outbound filtering)
      │                                            │
      └── forwarded ──────► [forward] ◄────────────┘ (shared exit path)
                                │
                                ▼
                          [postrouting]  (SNAT / masquerade: rewrites Src IP)
                                │
                                ├──► [AF_PACKET: tcpdump sees outbound AFTER the firewall]
                                ▼
                      Outgoing frame (NIC / veth)

    Two facts fall straight out of this diagram and save hours during network debugging:

    1. tcpdump asymmetry. Inbound traffic hits AF_PACKET (tcpdump) before prerouting and input, while outbound traffic hits it after output and postrouting. A packet dropped in input still shows up in the capture; a packet dropped in output never does.
    2. The input vs forward split. Once prerouting applies DNAT toward a container (or the host acts as a VPN router), the packet enters forward and never touches input. Filter published container ports in forward (nftables).
    1. Container✓ SYN to :8080
    2. AF_PACKET✓ tcpdump saw SYN
    3. netfilter input✕ counter 2 · drop
    # Both SYNs appear in tcpdump on br-app:
    20:05:31.293244 IP 10.0.50.10.37232 > 10.0.50.1.8080: Flags [S], length 0
    20:05:32.322323 IP 10.0.50.10.37232 > 10.0.50.1.8080: Flags [S], length 0
    # Meanwhile the nftables input counter climbed:
    tcp dport 8080 counter packets 2 bytes 120 drop
    Cause

    The packet was copied to tcpdump first, then dropped by the input chain right after. Seeing an inbound SYN in tcpdump does not prove the firewall allowed it.

    Check with

    sudo nft list ruleset (watch counter values on drop rules)

    1. curl on host✓ SYN to :9090
    2. netfilter output✕ counter 2 · drop
    # Not a single packet to port 9090 in tcpdump on br-app!
    # Yet the nftables output counter shows where they went:
    tcp dport 9090 counter packets 2 bytes 120 drop
    Cause

    On the outbound path, the firewall runs ahead of AF_PACKET. When an app sends traffic and tcpdump stays blank, check the output chain and the routing table.

    Check with

    sudo nft list ruleset and ip route get 10.0.50.10

    1. eth0 + AF_PACKET✓ SYN to :8080
    2. prerouting DNAT✓ -> 10.0.50.10:80
    3. forward -> veth✓ reached container
    # Routing after DNAT sends the packet into forward, skipping input:
    # eth0 -> prerouting (DNAT) -> forward -> postrouting -> br-app -> veth-app -> container
    Cause

    Port 8080 blocked only in input stays open for traffic headed to a container. Filter that traffic in forward (or publish on 127.0.0.1).

    Check with

    sudo nft list ruleset (forward and DOCKER-USER chains)

    You run `sudo tcpdump -nn -i eth0 'tcp port 8080'` on a server: inbound `SYN` packets from the client arrive, but there is no `SYN-ACK` and the web server logs stay empty. Can you rule out `nftables` on this server?
    sudo tcpdump -nn -i eth0 'tcp port 8080'
    # 20:05:31.293244 IP 10.0.50.10.37232 > 10.0.50.1.8080: Flags [S], length 0
    You run `sudo tcpdump -nn -i eth0 'tcp port 8080'` on a server: inbound `SYN` packets from the client arrive, but there is no `SYN-ACK` and the web server logs stay empty. Can you rule out `nftables` on this server?
    Answer:
    Ingress: NIC -> AF_PACKET (tcpdump) -> netfilter (nftables) -> application

    This is the single most common tcpdump trap. Seeing an inbound SYN only confirms the packet reached the server’s NIC — it says nothing about what netfilter did next.

    You need to inspect a container’s sockets and traffic, but it runs a minimal image with no `ip`, `ss`, or `tcpdump`. How do you check them from the host without rebuilding the image?
    PID=$(docker inspect -f '{{.State.Pid}}' web)
    sudo nsenter -t "$PID" -n ip -br addr
    sudo nsenter -t "$PID" -n tcpdump -nn -i eth0
    You need to inspect a container’s sockets and traffic, but it runs a minimal image with no `ip`, `ss`, or `tcpdump`. How do you check them from the host without rebuilding the image?
    Answer:
    sudo nsenter -t $(docker inspect -f '{{.State.Pid}}' app) -n ss -tulnp

    Container network isolation is a Linux kernel netns. Passing -n to nsenter runs any host binary directly inside the container’s network stack.

    If it doesn’t work

    tcpdump -nn -i eth0 shows incoming SYN packets from a client, but the app stays silent and the client times out

    Cause
    tcpdump captures incoming frames via an AF_PACKET socket at the interface entrance — before netfilter chains run. The packet reached the dump, then a drop rule in nftables or ufw discarded it microseconds later.
    Fix
    Inspect rules and packet counters with sudo nft list ruleset: look for a climbing drop counter in the input and forward hooks.

    A program on the server sends packets outbound and times out, while tcpdump -nn -i eth0 shows nothing at all

    Cause
    On the outbound path, netfilter hooks (output and postrouting) run before the frame reaches the driver and AF_PACKET. A packet dropped in output or missing a route never reaches tcpdump.
    Fix
    Check the output chain in sudo nft list ruleset and verify the route with ip route get <IP>.

    Port 8080 is blocked in ufw (input), yet a Docker container with -p 8080:80 stays reachable from outside

    Cause
    In the prerouting hook, Docker rewrites the destination address to the container IP (DNAT). After routing, the packet enters the forward hook toward br-* and bypasses input completely.
    Fix
    Publish the port on 127.0.0.1:8080:80 or add a filtering rule to forward / DOCKER-USER (Docker ports bypass the input chain, nftables).

    You need tcpdump or ss inside a scratch/distroless container that has no bash or network tools

    Cause
    A container’s network stack is a regular Linux kernel netns tied to its process PID, so host tools can enter it directly.
    Fix
    Get the PID (docker inspect -f '{{.State.Pid}}' <container>) and run sudo nsenter -t <PID> -n ss -tulnp or sudo nsenter -t <PID> -n tcpdump -nn -i eth0 from the host.

    The host shows dozens of veth…@if… interfaces and you cannot tell which belongs to which container

    Cause
    Docker creates a veth pair: one end lives inside the container’s netns (eth0@if42), the other sits on the host (veth…@if41) attached to a br-* bridge.
    Fix
    Read the number after @if from ip link inside the container (nsenter -t <PID> -n ip -o link show eth0) and match that index in ip -o link on the host.

    Also in this chapter