SkillPilotAI

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.
Packet path after ip route add
  1. 1.h1 looks up 10.10.0.20: matches 10.10.0.0/24 via 192.168.39.1

  2. 2.Frame goes to br0 (switch), port pa -> pr

  3. 3.Router r1 receives it and forwards out eth1

  4. 4.Server 10.10.0.20 replies

  5. 5.h1 gets the reply with ttl=63

Terminal
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.20
Output
h1$ 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 ms

19.2 change, replace, append, del

CommandRoute existsRoute missing
addError: File existsCreates it
changeUpdates itError
replaceUpdates itCreates it (safest in scripts)
appendAdds one more route for the same prefixCreates it
delRemoves one routeNo such process
Terminal
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/24
Output
h1$ 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.
Output
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

blackhole vs unreachable vs prohibit on r1
  1. 1.h1 pings 10.30.0.5: r1 blackhole route drops silently, no reply

  2. 2.h1 pings 10.40.0.5: r1 unreachable route drops and sends ICMP

  3. 3.h1 sees: From 192.168.39.1 Destination Host Unreachable

  4. 4.h1 pings 10.50.0.5: r1 prohibit route drops and sends ICMP admin prohibited

  5. 5.h1 sees: Packet filtered

Blackhole drops attack traffic cheaply; unreachable gives users a clear error

Terminal
# 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.5
Output
h1$ 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
TypeWhat happensSender sees
unicastNormal route (default)Normal reply
blackholeSilent dropNo reply, 100% loss
unreachableDrop + ICMP host unreachableDestination Host Unreachable
prohibitDrop + ICMP admin prohibitedPacket filtered
local / broadcastOwn address / link broadcast (table local, added by kernel)-
throwStop lookup in this table, try next policy rule-
nat / anycast / multicastOld / not implemented / multicast routing-
Output
r1$ ip route add throw 10.60.0.0/24 table 100r1$ ip route show table 100throw 10.60.0.0/24

19.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).
Terminal
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 local
Output
r1$ 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/24
Output
show | 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 realms

19.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.
Output
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 routing
Output
r1$ 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.
Output
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 SIDs

19.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.
Terminal
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
Output
$ 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 on
Output
state 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 clear

19.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).
Terminal
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 d0
Output
b$ 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).
XDP drop at the switch port
  1. 1.Attach drop.o: ip link set dev pa xdpgeneric obj /lab/drop.o

  2. 2.ip link show pa now has a prog/xdp line

  3. 3.h1 ping reaches port pa: XDP_DROP before the kernel stack

  4. 4.100% packet loss

  5. 5.xdpgeneric off: program detached, ping works again

Terminal
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
Output
$ 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 loss
Output
xdp 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 supported

19.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.
Terminal
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
Output
$ 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.