basforge

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

    RU

    Routing and longest prefix match: ip route, LPM, metrics, and TTL

    Before sending any IP packet, the kernel picks a matching entry from the routing table by longest prefix match (LPM): a more specific prefix always beats a broader one, regardless of line order or metric. Covers ip route output, ip route get diagnostics, ip_forward, and the TTL mechanism behind traceroute and mtr.

    What you’ll get

    Inspect the IPv4 routing table and ask ip route get exactly where the kernel sends packets to an external server, a LAN neighbor, and a Docker container:

    ip -4 route
    # default via 10.0.0.1 dev eth0 proto static
    # 10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.11
    # 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
    
    ip route get 203.0.113.10
    # 203.0.113.10 via 10.0.0.1 dev eth0 src 10.0.0.11 uid 1000
    
    ip route get 10.0.0.12
    # 10.0.0.12 dev eth0 src 10.0.0.11 uid 1000
    
    ip route get 172.17.0.2
    # 172.17.0.2 dev docker0 src 172.17.0.1 uid 1000

    203.0.113.10 includes via 10.0.0.1 — the packet goes to gateway 10.0.0.1’s MAC. 10.0.0.12 and 172.17.0.2 have no via: the kernel delivers the frame directly over L2 after resolving the target MAC with ARP (MAC, switch, ARP, and VLAN).

    Each line in ip route is compact, and every token maps to a specific kernel decision:

    ip -4 route · Pick a segment
    Instead of eyeballing a long table, ask the kernel directly: ip route get <IP> shows the winning interface, gateway, and src.

    Longest prefix match (LPM) and the role of metrics

    On a server with multiple NICs, Docker bridges, and VPN tunnels, several routing entries often match the same destination IP. Unlike a firewall ruleset, the kernel does not read the routing table top-to-bottom — it applies two rules:

    1. Longest Prefix Match (LPM). The most specific prefix wins: /32 beats /24, /24 beats /16, and any route beats default (/0).
    2. Lowest metric (metric). Metrics are compared only when prefix lengths are equal — for instance between two default entries (metric 100 beats metric 200).

    Here is a table with overlapping prefixes where the narrower /24 deliberately carries a worse metric (500) than /16 (10):

    ip -4 route
    # default via 10.0.10.1 dev eth0 metric 100
    # default via 10.0.10.2 dev eth0 metric 200
    # 10.20.0.0/16 via 10.0.10.2 dev eth0 metric 10
    # 10.20.30.0/24 via 10.0.10.3 dev eth0 metric 500
    # 10.20.30.5 via 10.0.10.4 dev eth0
    
    ip route get 10.20.30.5      # -> via 10.0.10.4  (/32 — longest prefix)
    ip route get 10.20.30.9      # -> via 10.0.10.3  (/24 beat /16 despite metric 500 > 10)
    ip route get 10.20.50.1      # -> via 10.0.10.2  (outside /24, matched /16)
    ip route get 203.0.113.10    # -> via 10.0.10.1  (two /0 routes — lower metric 100 won)

    In ip route output, a /32 host route appears without a slash (10.20.30.5), yet the kernel treats it as a 32-bit prefix.

    VPN clients (OpenVPN, WireGuard) rely on LPM for a well-known trick: to redirect all traffic into the tunnel without deleting your existing default, they install two routes — 0.0.0.0/1 and 128.0.0.0/1. Together they cover the entire IPv4 space, and /1 beats /0, so they override default. As soon as the tunnel drops and those two entries disappear, your original default route takes over immediately.

    The same rule explains why a Docker subnet colliding with your home LAN cuts off access (Docker subnet clashes with home network): 192.168.0.0/20 dev br-… is longer than default (/0) and hijacks return packets meant for the client.

    Which gateway does the kernel choose for a packet to 10.20.30.9?
    default          via 10.0.10.1  metric 100
    10.20.0.0/16     via 10.0.10.2  metric 10
    10.20.30.0/24    via 10.0.10.3  metric 500
    Which gateway does the kernel choose for a packet to 10.20.30.9?
    Answer:
    10.20.30.9 via 10.0.10.3 dev eth0 src 10.0.10.10

    The kernel first picks the longest matching prefix (LPM) and compares metric only among entries of the same prefix length. Since 10.20.30.9 falls inside /24, the /16 entry with metric 10 is never considered.

    Forwarding (ip_forward), return routes, and ip rule

    An ordinary Linux server and a Linux router run the exact same networking stack. The key switch is net.ipv4.ip_forward:

    • 0 (default on plain Linux): the kernel accepts packets addressed to its own IPs only; transit packets for other destinations are silently dropped.
    • 1 (router, Docker host, or Proxmox NAT bridge): the kernel looks up Dst IP in the routing table, decrements TTL by 1, rewrites the Ethernet header for the next hop, and sends the packet out the egress interface (Proxmox networking).

    Each direction is routed independently. On the public internet, forward and return paths often cross different transit ISPs, yet behind NAT or stateful firewalls the return path must go back through the same edge. If a client sends SYN through a gateway but the server lacks a route back to the client’s subnet, the packet arrives and the reply vanishes — always run ip route get on both ends when debugging.

    Linux holds more than one routing table; ip rule decides which table to query:

    ip rule show
    # 0:      from all lookup local
    # 32766:  from all lookup main
    # 32767:  from all lookup default
    • local (priority 0) holds the host’s own addresses, 127.0.0.0/8, and broadcast entries — which is why they do not appear in plain ip route.
    • main (32766) is the primary table displayed by ip route.
    • Between them, you can insert policy routing rules (for example, on a dual-ISP server so replies leave through the same interface the request came in on).

    TTL, traceroute, and mtr: how routers along the path show up

    The TTL field (Time To Live, 1 byte in IPv4; called Hop Limit in IPv6) caps how many routers a packet may cross. Linux starts at 64 (sysctl net.ipv4.ip_default_ttl), Windows at 128, and many network appliances at 255.

    Every router on the path subtracts 1 from TTL. When it reaches zero, the router drops the packet and returns ICMP Time Exceeded (type 11, code 0) with its own IP as the source. Without TTL, any routing loop (router A forwards to B and B forwards back to A) would spin a packet forever.

    Watch TTL in action with ping -t 1 (sets TTL = 1):

    # Normal ping across one router (TTL 64 dropped to 63):
    ping -c 1 10.0.20.10
    # 64 bytes from 10.0.20.10: icmp_seq=1 ttl=63 time=0.050 ms
    
    # Same ping with TTL=1 — stopped at the very first router 10.0.10.1:
    ping -c 1 -t 1 10.0.20.10
    # From 10.0.10.1 icmp_seq=1 Time to live exceeded

    On the router’s ingress interface, tcpdump -nn captures that two-packet exchange:

    # 18:43:36.370928 IP 10.0.10.10 > 10.0.20.10: ICMP echo request, id 3979, seq 1, length 64
    # 18:43:36.370949 IP 10.0.10.1 > 10.0.10.10: ICMP time exceeded in-transit, length 92

    traceroute, tracepath, and mtr all build on this mechanism: send probes with TTL = 1 (hop 1 answers), then TTL = 2 (hop 2 answers), TTL = 3, and so on until the target itself replies. Ubuntu includes mtr and tracepath out of the box: mtr -rwc 20 example.org runs 20 cycles and prints per-hop loss and latency.

    1. Client (TTL 64)✓ via 10.0.10.1
    2. Router 1✓ TTL 64 > 63
    3. Router 2✓ TTL 63 > 62
    4. Server✓ reply (TTL 62)
    64 bytes from 10.0.20.10: icmp_seq=1 ttl=63 time=0.050 ms
    What happened

    Each router picked the next hop by LPM, decremented TTL by 1, rewrote MAC addresses, and forwarded the packet.

    Check with

    ip route get 10.0.20.10

    1. Client (TTL 1)✓ probe >
    2. Router 10.0.10.1✕ TTL = 0 (dropped)
    3. ICMP Time Exceeded✓ < from 10.0.10.1
    18:43:36.370928 IP 10.0.10.10 > 10.0.20.10: ICMP echo request, id 3979, seq 1, length 64
    18:43:36.370949 IP 10.0.10.1 > 10.0.10.10: ICMP time exceeded in-transit, length 92
    What happened

    The router decremented TTL to zero and returned ICMP time exceeded in-transit from its own address — that is how traceroute and mtr discover each hop.

    Check with

    ping -c 1 -t 1 10.0.20.10 or mtr -rwc 10 203.0.113.10

    1. Hop 1 (gateway)✓ Loss 0%
    2. Hop 2 (backbone)✕ Loss 60% (ICMP)
    3. Hop 3 (ISP)✓ Loss 0%
    4. Server✓ Loss 0%
    HOST: web1              Loss%   Snt   Last   Avg  Best  Wrst
    1.|-- 10.0.0.1         0.0%    20    0.2   0.2   0.1   0.4
    2.|-- 198.51.100.1    60.0%    20    4.1   4.3   3.9   5.0
    3.|-- 203.0.113.1      0.0%    20   11.2  11.4  10.9  12.1
    4.|-- 203.0.113.10     0.0%    20   12.0  12.1  11.8  12.9
    Cause

    Hop 2 rate-limits ICMP Time Exceeded replies to save CPU, yet forwards transit packets cleanly — proven by 0.0% loss at hops 3 and 4.

    Check with

    Check the loss column on the last line of mtr -rwc 20 203.0.113.10

    1. Client✓ packet >
    2. Linux gateway✕ ip_forward = 0
    # On the client:
    1 packets transmitted, 0 received, 100% packet loss
    # On the Linux gateway:
    sysctl net.ipv4.ip_forward
    net.ipv4.ip_forward = 0
    Cause

    The gateway’s Linux kernel received the packet, but forwarding is off — transit traffic is dropped silently before the firewall.

    Check with

    sysctl net.ipv4.ip_forward on the gateway

    Does this mtr report show real packet loss to the server?
    HOST: web1              Loss%   Snt   Avg
      1.|-- 10.0.0.1         0.0%    20   0.2
      2.|-- 198.51.100.1     0.0%    20   3.1
      3.|-- 198.51.100.9    45.0%    20   8.4
      4.|-- 203.0.113.1      0.0%    20  12.0
      5.|-- 203.0.113.10     0.0%    20  12.4
    Does this mtr report show real packet loss to the server?
    Answer:
    No transit loss: read the final row (0.0% Loss)

    Core routers routinely deprioritize generating ICMP Time Exceeded on their control plane so they do not waste CPU on mtr probes. Genuine link loss starts at the faulty hop and continues down every subsequent row through the final server.

    If it doesn’t work

    Added a route with a low metric (10), yet traffic still leaves via a route with metric 500

    Cause
    The route with metric 500 has a longer prefix (for example /24 vs /16 or /0). Metrics break ties only among routes with the same prefix length.
    Fix
    Check the kernel’s decision for a specific address with ip route get 10.20.30.9. To override a narrower prefix, add a route with an equal or longer mask.

    mtr shows 40–100% loss or ??? on an intermediate hop, while the final server shows 0% loss

    Cause
    Intermediate routers rate-limit ICMP Time Exceeded generation or drop probes to their control plane while forwarding transit traffic without loss.
    Fix
    Read packet loss on the final hop and the rows after the suspect hop: real loss carries all the way to the bottom, whereas loss confined to one middle row is ICMP rate limiting on that router.

    A two-interface Linux host is reachable from both networks, yet packets will not pass between them

    Cause
    IPv4 forwarding is disabled in the kernel (net.ipv4.ip_forward = 0 by default on stock Linux without Docker), or the firewall’s forward chain drops transit packets.
    Fix
    Check sysctl net.ipv4.ip_forward (must be 1 for routing; persist it in /etc/sysctl.d/) and inspect the forward rules in sudo nft list ruleset.

    Starting a VPN dropped your SSH session to the server or cut off access to the local network

    Cause
    The VPN installed 0.0.0.0/1 and 128.0.0.0/1 (together they cover all of IPv4 and beat default /0), or a route covering the client subnet sent replies into the tunnel.
    Fix
    Run ip route get <client-ip>: if replies leave via the tunnel instead of eth0, add an explicit /32 route for your management IP through the old gateway, or configure policy routing (ip rule).

    ping or curl immediately fails with Network is unreachable even though the interface is UP

    Cause
    No entry in the routing table matches the target address — usually the default (0.0.0.0/0) route is missing.
    Fix
    Check ip -4 route: if there is no default via … line, inspect DHCP/netplan settings or add the gateway with sudo ip route add default via 10.0.0.1 dev eth0.