Skip to content

Backing up the routing table with netstat --libxo

Disclosure: Drafted with AI assistance from the system’s configuration files and command output, then reviewed and edited.

Anything I route add by hand is gone after a reboot. The normal way to deal with that is rc.conf — static_routes="foo bar" and then route_foo="-net 10.0.0.0/8 192.168.1.1" and so on, one line per route. I’ve never liked it much. You add a route once with route add, then type it in a second time in a different syntax and hope the two match. Delete a route a month later and rc.conf still has it. The config file and the actual routing table drift apart, and nothing tells you.

So instead I made the routing table back itself up. netstat --libxo json dumps the live table as JSON, a jq filter turns each route back into the route add command that produced it, and I run that at boot. The table is the config — whatever is in it right now is what gets persisted, blackholes and all.

libxo

libxo is the reason this is easy. It’s a library FreeBSD lets its userland tools write output through, so the same code path can emit the human-readable table, or JSON, or XML — you just pass a flag:

netstat -nr -F 1 --libxo json

What comes back nests under statistics.route-information.route-table, grouped into rt-family blocks (Internet, Internet6), each one a list of rt-entry objects with destination, gateway, interface-name, and the field this whole script hangs on: flags_pretty, an array of lowercase names — up, gateway, host, static, blackhole, reject. Those are the same flags netstat prints as UGS/B/R letters in the normal table, just spelled out. So instead of awk-ing a column that moves around when addresses get longer, the flags are data.

The script

dump_routes doesn’t back anything up itself — it prints a script to stdout. The first line it emits is a shebang, so redirecting it produces an executable restore script:

#!/usr/bin/env bash

echo "#!/usr/bin/env bash"

for x in 0 $(ifconfig | grep fib | awk '{print $NF}' | sort | uniq); do
  netstat -nr -F $x --libxo json | jq -r --arg fib "$x" '
    .statistics."route-information"."route-table"."rt-family"[]? as $fam
    | $fam."rt-entry"[]? as $entry

    # Grab the layout flags as a single string to safely compare
    | ($entry.flags_pretty | join(",")) as $flags

    # Filter for explicitly routed entries
    | select(($flags | contains("gateway")) or ($flags | contains("blackhole")) or ($flags | contains("reject")))

    # Determine the IP framework (-inet or -inet6)
    | (if $fam."address-family" == "Internet6" then "-inet6" else "-inet" end) as $proto

    # Handle the action modifier variant logic
    | (if $flags | contains("blackhole") then
         "-blackhole "
       elif $flags | contains("reject") then
         "-reject "
       else
         ""
       end) as $modifier

    # Build target string layout
    | "route add \($proto) -fib \($fib) \($modifier)-net \($entry.destination)" + (if $modifier == "" then " \($entry.gateway)" else "" end)
  '
done

The 0 at the front of that loop is deliberate. ifconfig only prints a fib: line for interfaces on a non-default FIB — the source checks ifr.ifr_fib != RT_DEFAULT_FIB before printing anything — so grepping it alone never yields FIB 0, and the default table would silently be left out of the backup.

What the jq does, in order:

  • Walk every rt-family and every rt-entry in it.
  • Join flags_pretty into one string so it can be tested with contains.
  • Keep only entries carrying gateway, blackhole, or reject. Everything else — the link# interface routes, the UHS host routes for local addresses — is generated by the kernel when the interfaces get their addresses, so re-adding it at boot would be redundant anyway.
  • Pick -inet or -inet6 from the family name.
  • Pick the -blackhole / -reject action modifier from the flags, and append the gateway only for plain gateway routes (a blackhole route has no next hop).

Running it, ./dump_routes > routes.sh, writes something like:

#!/usr/bin/env bash
route add -inet -fib 0 -net 10.0.0.0/8 192.168.1.1
route add -inet -fib 1 -net 192.168.50.0/24 192.168.50.1
route add -inet -fib 1 -blackhole -net 192.0.2.0/24
route add -inet6 -fib 1 -net 2001:db8::/32 2001:db8::1

Restoring at boot

chmod +x routes.sh, drop it somewhere stable, and run it from /etc/rc.local — that runs after netif has brought the interfaces up, which matters, because a route add pointing at an interface with no address yet fails:

#!/bin/sh
/usr/local/bin/routes.sh

The workflow is then: add routes however you normally would (route add ..., from a script, from a moment of experimentation), and when the table looks right, regenerate routes.sh. No second declaration of the same route in rc.conf, no drift between “what’s running” and “what’s configured” — the file is a snapshot of the kernel’s actual state, blackholes and rejects and multiple FIBs included.

The caveat that comes with any snapshot: it’s a snapshot. Routes added after the last regeneration are lost on reboot, so regenerating is part of the ritual after any change to the table. I treat routes.sh like a lockfile — regenerate on change, not on a schedule.

What should really exist instead

Honestly, the whole script is a workaround. netstat --libxo xml can already dump the same table as XML, and expat — bsdxml.h — is part of the base system, so route(8) itself could just grow a flag that reads one of those dumps and restores it: FIBs, blackholes, rejects, the lot. Dump with netstat, restore with route, both in base. No jq, no generated shell script parked in /usr/local/bin, no rc.local hook — the tool that owns the routing table would be able to read it back.

Nobody’s written that, though. It’s the idea that came out of this script.

WIP: libxo for ifconfig

What I have been writing, separately, is libxo support for ifconfig — D58560, “Adding libxo support to ifconfig (XML / JSON output)”. Same motivation, different binary: right now ifconfig can’t be queried as JSON at all, so anything scriptable around interface state is back to scraping text. The current revision moves every printf in ifconfig into a 5,600-line ifconfig_output.c behind ifdefs so libxo stays optional at build time, with the rule that the plain-text output stays exactly what it was — nobody’s scripts break — and a kyua test that builds it both ways and diffs them. It’s been “Needs Review” since July, and it’s taken a few wrong turns along the way (an update that reverted most of the diff and broke some pf tests; arcanist; the rescue(8) question, which I checked — it works).

There’s one note on that review that loops back here: an XML parser to restore interface configuration from XML, tentatively. Different tool, same shape as what I want for routes — dump state out as XML, parse it back in with expat, both halves in base.

Neither the route side nor the ifconfig side is finished; both are going to take time. Until then, dump_routes.