ip (iproute2) (3/6)
This part of ip covers the routing table (ip route add/change/replace/append/del, route types, show selectors, encapsulation and SRv6) and the link-level port options: bridge_slave, bond_slave/dsa, XDP programs, SR-IOV VFs and ip link show/xstats/property.
19.1 Lab: without and with a route
- Namespaces in one Docker container: laptop h1 (192.168.39.10) -> bridge br0 (switch) -> router r1 (192.168.39.1) -> server srv 10.10.0.20.
- h1 only knows its own LAN (proto kernel route), so ping 10.10.0.20 = Network unreachable.
- After adding 10.10.0.0/24 via 192.168.39.1 the reply comes back with ttl=63: one router reduced the TTL by 1.
- Step 1
1.h1 looks up 10.10.0.20: matches 10.10.0.0/24 via 192.168.39.1
- Step 2
2.Frame goes to br0 (switch), port pa -> pr
- Step 3
3.Router r1 receives it and forwards out eth1
- Step 4
4.Server 10.10.0.20 replies
- Step 5
5.h1 gets the reply with ttl=63
ip routeping -c 1 10.10.0.20ip route add 10.10.0.0/24 via 192.168.39.1ip routeping -c 2 10.10.0.20h1$ ip route192.168.39.0/24 dev eth0 proto kernel scope link src 192.168.39.10h1$ ping -c 1 10.10.0.20ping: connect: Network unreachableh1$ ip route add 10.10.0.0/24 via 192.168.39.1h1$ ip route10.10.0.0/24 via 192.168.39.1 dev eth0192.168.39.0/24 dev eth0 proto kernel scope link src 192.168.39.10h1$ ping -c 2 10.10.0.2064 bytes from 10.10.0.20: icmp_seq=1 ttl=63 time=0.270 ms64 bytes from 10.10.0.20: icmp_seq=2 ttl=63 time=0.105 ms19.2 change, replace, append, del
| Command | Route exists | Route missing |
|---|---|---|
| add | Error: File exists | Creates it |
| change | Updates it | Error |
| replace | Updates it | Creates it (safest in scripts) |
| append | Adds one more route for the same prefix | Creates it |
| del | Removes one route | No such process |
ip route change 10.10.0.0/24 via 192.168.39.1 mtu 1400ip route replace 10.20.0.0/24 via 192.168.39.1ip route append 10.20.0.0/24 via 192.168.39.2ip route add default via 192.168.39.1 metric 100ip routeip route del 10.20.0.0/24ip route del 10.20.0.0/24ip route del 10.20.0.0/24h1$ ip routedefault via 192.168.39.1 dev eth0 metric 10010.10.0.0/24 via 192.168.39.1 dev eth0 mtu 140010.20.0.0/24 via 192.168.39.1 dev eth010.20.0.0/24 via 192.168.39.2 dev eth0192.168.39.0/24 dev eth0 proto kernel scope link src 192.168.39.10h1$ ip route del 10.20.0.0/24h1$ ip route del 10.20.0.0/24h1$ ip route del 10.20.0.0/24RTNETLINK answers: No such process- Most used options: via, dev, metric (lower = preferred); table/vrf choose the table; nexthop ... weight builds multipath; mtu, advmss, initcwnd etc. tune path MTU and TCP.
to PREFIX | default destinationvia ADDR | dev NAME next hop, interfacemetric N lower = preferredtable ID | vrf NAME which tablesrc ADDR preferred source IPmtu [lock] N | advmss path MTU, MSSproto | scope who added it, scopenexthop via .. weight N multipath routeonlink | nhid ID on-link GW, NH objecttos | realm | as TOS key, realm, NATwindow rtt rttvar TCP estimatesrto_min ssthresh cwnd TCP tuninginitcwnd initrwnd initial windowscongctl quickack ecn .. TCP optionspref | expires (IPv6) RA preference, timerrealm REALMID route realm (number or name)quickack BOOL TCP quick ack: 1 on, 0 offscope SCOPE_VAL host, link, global or numberpervasive nexthop flag (NHFLAGS)19.3 Route types
- Step 1
1.h1 pings 10.30.0.5: r1 blackhole route drops silently, no reply
- Step 2
2.h1 pings 10.40.0.5: r1 unreachable route drops and sends ICMP
- Step 3
3.h1 sees: From 192.168.39.1 Destination Host Unreachable
- Step 4
4.h1 pings 10.50.0.5: r1 prohibit route drops and sends ICMP admin prohibited
- Step 5
5.h1 sees: Packet filtered
Blackhole drops attack traffic cheaply; unreachable gives users a clear error
# r1 (router)ip route add blackhole 10.30.0.0/24ip route add unreachable 10.40.0.0/24ip route add prohibit 10.50.0.0/24# h1 (laptop)ping -c 2 -W 1 10.30.0.5ping -c 2 -W 1 10.40.0.5ping -c 2 -W 1 10.50.0.5h1$ ping -c 2 -W 1 10.30.0.5PING 10.30.0.5 (10.30.0.5) 56(84) bytes of data.2 packets transmitted, 0 received, 100% packet lossh1$ ping -c 2 -W 1 10.40.0.5From 192.168.39.1 icmp_seq=1 Destination Host UnreachableFrom 192.168.39.1 icmp_seq=2 Destination Host Unreachableh1$ ping -c 2 -W 1 10.50.0.5From 192.168.39.1 icmp_seq=1 Packet filtered| Type | What happens | Sender sees |
|---|---|---|
| unicast | Normal route (default) | Normal reply |
| blackhole | Silent drop | No reply, 100% loss |
| unreachable | Drop + ICMP host unreachable | Destination Host Unreachable |
| prohibit | Drop + ICMP admin prohibited | Packet filtered |
| local / broadcast | Own address / link broadcast (table local, added by kernel) | - |
| throw | Stop lookup in this table, try next policy rule | - |
| nat / anycast / multicast | Old / not implemented / multicast routing | - |
r1$ ip route add throw 10.60.0.0/24 table 100r1$ ip route show table 100throw 10.60.0.0/2419.4 ip route show (selectors)
- proto kernel = routes the kernel created from interface IPs; root PREFIX = everything inside; match ADDR = the route covering it.
- table local holds local and broadcast routes; table all shows every table; via/dev filter by gateway or interface.
- cached / table cache are for the old routing cache (empty for IPv4 on modern kernels).
ip route showip route show proto kernelip route show type blackholeip route show root 10.0.0.0/8ip route show match 10.10.0.20ip route show dev eth1ip route show table localr1$ ip route show10.10.0.0/24 dev eth1 proto kernel scope link src 10.10.0.1blackhole 10.30.0.0/24unreachable 10.40.0.0/24prohibit 10.50.0.0/24192.168.39.0/24 dev eth0 proto kernel scope link src 192.168.39.1r1$ ip route show proto kernel10.10.0.0/24 dev eth1 scope link src 10.10.0.1192.168.39.0/24 dev eth0 scope link src 192.168.39.1r1$ ip route show match 10.10.0.2010.10.0.0/24 dev eth1 proto kernel scope link src 10.10.0.1r1$ ip route show table locallocal 10.10.0.1 dev eth1 proto kernel scope host src 10.10.0.1broadcast 10.10.0.255 dev eth1 proto kernel scope link src 10.10.0.1local 192.168.39.1 dev eth0 proto kernel scope host src 192.168.39.1broadcast 192.168.39.255 dev eth0 proto kernel scope link src 192.168.39.1h1$ ip route show via 192.168.39.1default dev eth0 metric 10010.10.0.0/24 dev eth0 mtu 1400r1$ ip route list table all type unreachableunreachable 10.40.0.0/24show | list main tableto [exact] PREFIX this prefixroot PREFIX everything insidematch PREFIX covering routesexact PREFIX exact prefix onlytable ID | all | cache which tablecached [from SEL] cloned routesvrf NAME VRF tableproto RTPROTO who added itscope link | host scope filtertype TYPE blackhole, unreachabledev NAME | via ADDR interface or gatewaysrc ADDR | tos | realm source, TOS, realmrealm REALMID only routes of this realmrealms FROM/TO routes with from/to realms19.5 Route encapsulation and SRv6
- encap adds an extra header per route: mpls pushes a label, seg6 adds an IPv6 Segment Routing header, ip/bpf attach tunnel metadata or a BPF program.
- seg6local actions run when a packet reaches this router as a segment: End (go to next segment), End.X, End.DT4/DT6 (decap into a VRF).
- The lab's WSL kernel has LWTUNNEL disabled, so the real output is an error.
encap mpls 200/300 MPLS labels pushencap mpls 200 ttl 10 MPLS TTLencap ip id N dst IP tunnel metadataencap bpf in|out|xmit BPF programseg6 mode inline SRH after headerseg6 mode encap segs .. outer IPv6 + SRHencap.red | l2encap reduced, L2 frameseg6 ... hmac KEYID HMAC addioam6 trace prealloc IOAM trace dataencap xfrm if_id N XFRM (IPsec)encap rpl segs .. RPL source routingr1$ ip route add 10.70.0.0/24 encap mpls 100 via 10.10.0.20Error: CONFIG_LWTUNNEL is not enabled in this kernel.r1$ ip route help 2>&1 | grep -A2 ENCAPTYPEENCAPTYPE := [ mpls | ip | ip6 | seg6 | seg6local | rpl | ioam6 | xfrm ]r1$ ip -6 route add 2001:db8:39::100/128 encap seg6local action End dev eth1Error: CONFIG_LWTUNNEL is not enabled in this kernel.End normal SRv6 endpointEnd.X nh6 ADDR endpoint + next hopEnd.T table ID lookup in a tableEnd.DX2 oif DEV L2 frame decapEnd.DX4 | End.DX6 inner IPv4 | IPv6 decapEnd.DT4 | DT6 | DT46 decap + VRF lookupEnd.B6 srh segs insert a new SRHEnd.B6.Encaps outer IPv6 + new SRHEnd.BPF endpoint BPF program as actionflavors next-csid compressed SIDs19.6 Bridge port options (bridge_slave)
- Port pa faces the laptop. state 2 (learning) learns MACs but does not forward, so the ping fails; state 3 (forwarding) works again.
- cost, priority are STP values; learning off stops MAC learning; guard/root_block protect STP; isolated ports cannot talk to each other; locked allows only known MACs.
ip -br link show master br0ip link set pa type bridge_slave state 2bridge link show dev paping -c 2 -W 1 192.168.39.1ip link set pa type bridge_slave state 3ping -c 2 -W 1 192.168.39.1ip link set pa type bridge_slave cost 100ip link set pa type bridge_slave priority 32ip link set pa type bridge_slave learning offbridge -d link show dev pa$ ip link set pa type bridge_slave state 2$ bridge link show dev pa10: pa@br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state learning priority 32 cost 2h1$ ping -c 2 -W 1 192.168.39.12 packets transmitted, 0 received, 100% packet loss$ ip link set pa type bridge_slave state 3h1$ ping -c 2 -W 1 192.168.39.164 bytes from 192.168.39.1: icmp_seq=1 ttl=64 time=0.446 ms64 bytes from 192.168.39.1: icmp_seq=2 ttl=64 time=0.100 ms$ bridge -d link show dev pa10: pa@br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 100 hairpin off guard off root_block off fastleave off learning off flood on mcast_flood on bcast_flood onstate 0..4 disabled .. blockingpriority N | cost N STP port priority, costguard | root_block BPDU guard, root blockhairpin on back out same portlearning | flood learning, unknown floodbcast_flood | mcast_flood bcast, mcastmcast_router 0..3 multicast router modemcast_to_unicast multicast as unicastproxy_arp | proxy_arp_wifi proxy ARPneigh_suppress | vlan_tunnel EVPN, VXLANisolated | locked isolation, known MAC onlybackup_port DEV | nobackup_portfdb_flush dynamic FDB clear19.7 bond_slave, dsa, XDP, SR-IOV VFs
- bond_slave: queue_id and prio (which slave becomes active first in failover); DSA conduit is for routers with switch chips (dummy says Not supported).
ip link add bond0 type bond mode active-backupip link add d0 type dummyip link add d1 type dummyip link set d0 master bond0ip link set d1 master bond0ip link set d1 type bond_slave prio 10ip -d -j link show d1 | jq .[0].linkinfo.info_slave_dataip link set d1 type dsa conduit d0b$ ip -d -j link show d1 | jq .[0].linkinfo.info_slave_data{ "state": "ACTIVE", "mii_status": "UP", "queue_id": 0, "prio": 10}b$ ip link set d1 type dsa conduit d0RTNETLINK answers: Not supported- XDP (eXpress Data Path): a small BPF program at driver level that decides on every packet before routing and firewall.
- drop.o in xdpgeneric mode drops everything at port pa; native (xdpdrv) and generic cannot be active together; xdpgeneric off detaches it.
- xdpoffload runs on smart-NIC hardware; -force replaces atomically.
- SR-IOV splits one NIC into virtual functions (VFs) passed to VMs; vf options set MAC, VLAN, rate limits (veth says Not supported).
- Step 1
1.Attach drop.o: ip link set dev pa xdpgeneric obj /lab/drop.o
- Step 2
2.ip link show pa now has a prog/xdp line
- Step 3
3.h1 ping reaches port pa: XDP_DROP before the kernel stack
- Step 4
4.100% packet loss
- Step 5
5.xdpgeneric off: program detached, ping works again
ip link set dev pa xdpgeneric obj /lab/drop.oip link show dev paping -c 2 -W 1 192.168.39.1ip link set dev pa xdpdrv obj /lab/drop.oip link set dev pa xdpgeneric offping -c 2 -W 1 192.168.39.1$ ip link show dev pa10: pa@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 xdpgeneric qdisc noqueue master br0 prog/xdp id 680 name tag b11459a0e11ca14c jitedh1$ ping -c 2 -W 1 192.168.39.12 packets transmitted, 0 received, 100% packet loss$ ip link set dev pa xdpdrv obj /lab/drop.oError: Native and generic XDP can't be active at the same time.$ ip link set dev pa xdpgeneric offh1$ ping -c 2 -W 1 192.168.39.164 bytes from 192.168.39.1: icmp_seq=1 ttl=64 time=0.084 ms2 packets transmitted, 2 received, 0% packet lossxdp obj FILE native, else genericxdpgeneric obj FILE works on any NICxdpdrv obj FILE native driver onlyxdpoffload obj FILE on NIC hardwarepinned FILE bpffs pinned program-force ... xdp atomic replacexdp off | none detachvf N mac ADDR MAC of the VFvf N vlan ID VLAN tag (0 = off)vf N max_tx_rate N max TX Mbpsvf N min_tx_rate N guaranteed TX Mbpsvf N spoofchk on|off spoof checkvf N state auto|enable|disable link statevf N trust on|off trusted VF $ ip link set dev pa vf 0 mac 02:00:00:00:00:10RTNETLINK answers: Not supported19.8 ip link show / xstats / afstats / property / help
- Filters: type veth, master br0, up, group, nomaster.
- xstats type bridge shows IGMP/MLD counters; afstats per-family stats (empty here).
- property add ... altname gives an interface an alternate name; help TYPE prints that link type's options.
ip -br link show type vethip -br link show master br0ip link xstats type bridgeip link afstats dev paip link property add dev pa altname to-h1ip link show to-h1ip link help bridge_slave 2>&1 | head -6$ ip -br link show master br0pa@if9 UP 4a:33:0a:a7:71:1f <BROADCAST,MULTICAST,UP,LOWER_UP>pr@if9 UP 9e:e0:87:9f:5d:d5 <BROADCAST,MULTICAST,UP,LOWER_UP>$ ip link xstats type bridgebr0 IGMP queries: RX: v1 0 v2 0 v3 0 MLD queries: RX: v1 0 v2 0$ ip link afstats dev pa$ ip link property add dev pa altname to-h1$ ip link show to-h110: pa@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 altname to-h1$ ip link help bridge_slave 2>&1 | head -6Usage: ... bridge_slave [ fdb_flush ] [ state STATE ]19.9 Common mistakes
- Thinking the lowest metric always wins: longest prefix match comes first; metric only breaks ties between identical prefixes (a /24 beats default).
- Using add in scripts and failing on 'File exists': use replace.
- Expecting a blackhole to return an error: it is silent; use unreachable/prohibit if users need feedback.
- Forgetting that all these changes are lost after a reboot.
19.10 Interview tips
- add = new (File exists on duplicate), change = existing only, replace = either (idempotent).
- blackhole = silent drop, unreachable = ICMP host unreachable, prohibit = ICMP admin prohibited.
- Route selection: longest prefix match, then metric.
- XDP runs before the kernel stack, the fastest place to drop DDoS traffic.