ip (iproute2) (6/6)
Part 6 (the last part) of the ip command: building a real IPsec tunnel with ip xfrm (policy + state), watching kernel events with ip xfrm monitor, and the remaining objects ip stats, ip tcp_metrics, ip token, ip vrf and ip sr.
22.1 Why is it used?
- xfrm is the IPsec framework of the Linux kernel; VPN daemons such as strongSwan and Libreswan run on top of it.
- ip xfrm lets you build, inspect and debug IPsec policies and SAs by hand.
- ip stats and ip tcp_metrics show interface counters and the TCP values the kernel caches per destination.
- ip vrf gives separate routing tables; ip token and ip sr are IPv6 helpers (interface IDs, Segment Routing).
- Linux only (iproute2, root). Windows and macOS manage IPsec with their own tools. Lab: Docker, two namespaces a and b.
22.2 How it works: SPD and SAD
- SPD (Security Policy Database), via ip xfrm policy: WHICH traffic must be protected (selector + direction + tmpl).
- SAD (Security Association Database), via ip xfrm state: HOW to protect it (SPI, protocol, mode, algorithm, keys).
- The policy's tmpl (src, dst, proto, reqid, mode) must match an SA, otherwise the packet is dropped and an acquire event is raised.
- Lab: Host A (192.168.42.1, LAN 10.1.0.0/24) and Host B (192.168.42.2, LAN 10.2.0.0/24) are office gateways; br0 between them is untrusted.
- Step 1
1.Host A pings 10.2.0.1 from 10.1.0.1
- Step 2
2.xfrm layer checks the SPD: dir out policy matches
- Step 3
3.Policy says: encrypt with ESP, tunnel mode, reqid 1
- Step 4
4.SAD lookup: no SA with keys yet
- Step 5
5.Packet dropped, kernel raises an acquire event
- Step 6
6.ip xfrm monitor prints 'acquire proto esp'
In production an IKE daemon listens for acquire and negotiates the SA
22.3 ip xfrm policy + monitor
ip -n a xfrm policy add \ src 10.1.0.0/24 dst 10.2.0.0/24 dir out \ tmpl src 192.168.42.1 dst 192.168.42.2 \ proto esp reqid 1 mode tunnelip netns exec a ping -c 2 -W 1 10.2.0.1# second terminal (started first):ip xfrm monitor acquire expire SA policy$ ip netns exec a ping -c 2 -W 1 10.2.0.1PING 10.2.0.1 (10.2.0.1) 56(84) bytes of data.--- 10.2.0.1 ping statistics ---2 packets transmitted, 0 received, 100% packet loss$ ip xfrm monitor acquire expire SA policyacquire proto esp policy src 10.1.0.0/24 dst 10.2.0.0/24 dir out priority 0 ptype main tmpl src 192.168.42.1 dst 192.168.42.2 proto esp reqid 1 mode tunnel22.4 ip xfrm state: adding the SA
- state add with src/dst (outer endpoints), proto esp, spi 0x1000, reqid 1 (links to the policy), mode tunnel.
- aead rfc4106(gcm(aes)) KEY 128 = AES-GCM with a 128-bit ICV (encryption + authentication in one).
- Host B needs the same SA plus a return SA (SPI 0x2000) and dir in policies on both hosts.
- On br0 only ESP is visible: SPI 0x1000 one way, 0x2000 the other, sequence numbers rising.
K1=0x00112233445566778899aabbccddeeff01020304ip -n a xfrm state add \ src 192.168.42.1 dst 192.168.42.2 \ proto esp spi 0x1000 reqid 1 mode tunnel \ aead "rfc4106(gcm(aes))" $K1 128# same SA on Host B, SPI 0x2000 for return,# dir in policies on both hostsip netns exec a ping -c 2 10.2.0.1tcpdump -ni br0 -c 4 esp$ ip netns exec a ping -c 2 10.2.0.164 bytes from 10.2.0.1: icmp_seq=1 ttl=6464 bytes from 10.2.0.1: icmp_seq=2 ttl=642 packets transmitted, 2 received, 0% packet loss$ tcpdump -ni br0 -c 4 espIP 192.168.42.1 > 192.168.42.2: ESP(spi=0x00001000,seq=0x1), length 120IP 192.168.42.2 > 192.168.42.1: ESP(spi=0x00002000,seq=0x1), length 120IP 192.168.42.1 > 192.168.42.2: ESP(spi=0x00001000,seq=0x2), length 120IP 192.168.42.2 > 192.168.42.1: ESP(spi=0x00002000,seq=0x2), length 1204 packets captured- Step 1
1.Host A ping 10.1.0.1 -> 10.2.0.1
- Step 2
2.xfrm finds policy + matching SA (SPI 0x1000)
- Step 3
3.Original packet encrypted and wrapped in ESP
- Step 4
4.New outer IP header 192.168.42.1 -> 192.168.42.2 on br0
- Step 5
5.Host B finds its SA by SPI and decrypts
- Step 6
6.Original ping reaches the app on Host B
Tunnel mode wraps the whole packet; transport mode protects only the payload
22.5 Managing SAs: list, get, count, allocspi
ip -n a xfrm state list nokeysip -n a -s xfrm state get src 192.168.42.1 \ dst 192.168.42.2 proto esp spi 0x1000ip -n a xfrm state countip -n a xfrm state allocspi \ src 192.168.42.1 dst 192.168.42.3 \ proto esp min 0x3000 max 0x3fff$ ip -n a xfrm state list nokeyssrc 192.168.42.1 dst 192.168.42.2 proto esp spi 0x00001000 reqid 1 mode tunnel aead rfc4106(gcm(aes)) <<Keys hidden>> 128$ ip -n a -s xfrm state get src 192.168.42.1 dst 192.168.42.2 proto esp spi 0x1000 lifetime config: expire add: soft 0(sec), hard 0(sec) lifetime current: 168(bytes), 2(packets)$ ip -n a xfrm state count SAD count 2$ ip -n a xfrm state allocspi src 192.168.42.1 dst 192.168.42.3 proto esp min 0x3000 max 0x3fffsrc 192.168.42.1 dst 192.168.42.3 proto esp spi 0x00003257 reqid 0 mode transport22.6 State options: algorithms, mode, replay, selector, limits
| Option | Meaning |
|---|---|
| proto esp | ah | comp | ESP = encrypt + auth, AH = authentication only, comp = IP compression |
| enc / auth / auth-trunc / aead | Cipher (cbc(aes)), HMAC (hmac(sha256)), truncated ICV, or AEAD (rfc4106(gcm(aes))) |
| mode transport | tunnel | beet | Payload only / whole packet with new IP / BEET, Mobile IPv6 modes |
| reqid N, mark M | Links the SA to a policy tmpl; matches marked traffic |
| replay-window N, flag esn | Anti-replay window; 64-bit extended sequence numbers |
| sel src A dst B | Which inner traffic this SA may carry |
| limit time-soft / time-hard | Soft = rekey trigger, hard = SA expires |
| encap espinudp | NAT-T: ESP inside UDP 4500 |
| deleteall / flush / count | Bulk delete, remove all SAs, SAD count |
ip -n a xfrm state add \ src 192.168.42.1 dst 192.168.42.9 \ proto esp spi 0x4000 mode tunnel \ aead "rfc4106(gcm(aes))" $K1 128 \ replay-window 128 flag esn mark 7 \ sel src 10.1.0.0/24 dst 10.9.0.0/24 \ limit time-soft 3000 limit time-hard 3600ip -n a -s xfrm state get src 192.168.42.1 \ dst 192.168.42.9 proto esp \ spi 0x4000 mark 7 replay-window 0 seq 0x00000000 flag esn (0x10000000) mark 0x7/0xffffffff replay_window 128, bitmap-length 4 sel src 10.1.0.0/24 dst 10.9.0.0/24 uid 0 expire add: soft 3000(sec), hard 3600(sec)22.7 Policy options and management
- dir out | in | fwd: direction; fwd is for traffic routed through the gateway.
- action allow | block, priority N (lower number first), level required | use, ptype main | sub.
- tmpl src dst proto mode reqid: the SA the traffic needs.
- setdefault DIR ACTION changes the default for traffic with no policy; getdefault shows it.
- set hthresh4 / hthresh6 tune policy hashing; ip xfrm monitor events: acquire, expire, SA, policy, aevent, report.
ip -n a xfrm policy listip -n a -s xfrm policy countip -n a xfrm policy setdefault in blockip -n a xfrm policy getdefaultip -n a xfrm policy setdefault in allow$ ip -n a xfrm policy listsrc 10.2.0.0/24 dst 10.1.0.0/24 dir in priority 0 ptype mainsrc 10.1.0.0/24 dst 10.2.0.0/24 dir out priority 0 ptype main$ ip -n a -s xfrm policy count SPD IN 1 OUT 1 FWD 0 (Sock: IN 0 OUT 0 FWD 0)$ ip -n a xfrm policy setdefault in block$ ip -n a xfrm policy getdefaultDefault policies: in: block fwd: accept out: accept22.8 ip stats and ip tcp_metrics
- ip stats show dev D group link = same counters as ip -s link; group offload, xstats (bridge stp/mcast, bond 802.3ad), afstats.
- ip tcp_metrics: per-destination cached cwnd, rtt, rttvar; new connections start from these values.
- In the lab the IPsec path (10.2.0.1) gave 80 Mbits/sec with rtt 29 ms, the direct path 2.36 Gbits/sec with rtt 5.6 ms.
- delete removes one destination, flush all clears the cache.
ip -n a stats show dev eth0 group linkip stats show dev pa group xstats_slave \ subgroup bridge suite stpip stats show dev pa group offload \ subgroup hw_stats_info$ ip -n a stats show dev eth0 group link9: eth0: group link RX: bytes packets errors dropped missed mcast 643733 7392 0 0 0 0 TX: bytes packets errors dropped carrier collsns 324327623 14526 0 0 0 0$ ip stats show dev pa group xstats_slave subgroup bridge suite stp9: pa: group xstats_slave subgroup bridge suite stp STP BPDU: RX: 0 TX: 0 STP TCN: RX: 0 TX: 0 STP Transitions: Blocked: 0 Forwarding: 0$ ip stats show dev pa group offload subgroup hw_stats_info9: pa: group offload subgroup hw_stats_info l3_stats off used offip -n a tcp_metrics showip -n a tcp_metrics show address 10.2.0.0/24ip -n a tcp_metrics delete 10.2.0.1ip -n a tcp_metrics flush allip -n a tcp_metrics show$ ip netns exec a iperf3 -c 192.168.42.2 -t 1 | tail -4[ 5] 0.00-1.06 sec 298 MBytes 2.36 Gbits/sec receiver$ ip netns exec a iperf3 -c 10.2.0.1 -B 10.1.0.1 -t 1 | tail -4[ 5] 0.00-1.02 sec 9.75 MBytes 80.0 Mbits/sec receiver$ ip -n a tcp_metrics show10.2.0.1 age 1.068sec cwnd 49 rtt 29045us rttvar 33122us source 10.1.0.1192.168.42.2 age 2.140sec cwnd 398 rtt 5670us rttvar 9431us source 192.168.42.1$ ip -n a tcp_metrics delete 10.2.0.1$ ip -n a tcp_metrics show192.168.42.2 age 2.152sec cwnd 398 rtt 5670us$ ip -n a tcp_metrics flush all$ ip -n a tcp_metrics show22.9 ip token, ip vrf, ip sr
- ip token sets a fixed IPv6 interface identifier (the low 64 bits) while the prefix still comes from router advertisements.
- ip vrf: a VRF device (type vrf table 10) gives its enslaved interfaces a separate routing table; ip vrf exec runs a command inside it.
- Outside the VRF the ping says 'Network unreachable'; inside (ip vrf exec red) it works.
- ip sr tunsrc sets the SRv6 tunnel source address; hmac manages SRv6 HMAC keys.
ip -n a token set ::1a:2b:3c:4d/64 dev eth0ip -n a token get dev eth0ip -n a token del dev eth0ip -n a token get dev eth0ip -n a token list$ ip -n a token set ::1a:2b:3c:4d/64 dev eth0$ ip -n a token get dev eth0token ::1a:2b:3c:4d dev eth0$ ip -n a token del dev eth0$ ip -n a token get dev eth0token :: dev eth0ip -n a link add red type vrf table 10ip -n a link set red upip -n a link set v1 master redip -n a vrf showip netns exec a ping -c 1 -W 1 10.42.0.2ip netns exec a ip vrf exec red \ ping -c 1 10.42.0.2ip netns exec a ip vrf exec red \ ip vrf identify$ ip -n a vrf showName Table-----------------------red 10$ ip netns exec a ping -c 1 -W 1 10.42.0.2ping: connect: Network unreachable$ ip netns exec a ip vrf exec red ping -c 1 10.42.0.264 bytes from 10.42.0.2: icmp_seq=1 ttl=641 packets transmitted, 1 received, 0% packet loss$ ip netns exec a ip vrf exec red ip vrf identifyredip -n a sr tunsrc showip -n a sr tunsrc set 2001:db8::1ip -n a sr tunsrc showip -n a sr hmac show# hmac set 42 sha256 = passphrase prompt$ ip -n a sr tunsrc showtunsrc addr ::$ ip -n a sr tunsrc set 2001:db8::1$ ip -n a sr tunsrc showtunsrc addr 2001:db8::1$ ip -n a sr hmac showRTNETLINK answers: No error informationDump terminated22.10 Common mistakes
- Policy + SA added but monitor still shows acquire: the tmpl reqid, src, dst or proto does not match the SA.
- Forgetting the return direction: Host B needs its own SA (SPI 0x2000) and dir in policies on both sides.
- Treating manual xfrm as a production VPN: manual keys never rotate and a sequence reset weakens security. Use an IKE daemon (strongSwan).
- Setting setdefault in block and locking yourself out of non-IPsec traffic.
22.11 Interview tips
- IPsec in Linux = xfrm: SPD says which traffic, SAD holds SAs with keys, the IKE daemon negotiates and installs SAs, the kernel encrypts/decrypts by policy and SPI.
- Policy = what to protect, SA = how. No SA means drop + acquire.
- ESP encrypts and authenticates, AH only authenticates; tunnel mode wraps the whole packet.
- NAT-T puts ESP in UDP 4500 (encap espinudp).
- VRF = separate routing table per tenant on one Linux box.