Skip to content

feat(bgp) TCP-AO - #4

Merged
digizeph merged 15 commits into
bgpkit:mainfrom
ties:feat/tcp-ao
Nov 18, 2026
Merged

digizeph merged 15 commits into
bgpkit:mainfrom
ties:feat/tcp-ao

Conversation

@ties

@ties ties commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Support TCP-AO for BGP

Change authored with Claude Code (Opus 5.5 high)

Support TCP-AO for BGP

Change authored with Claude Code (Opus 5.5 high)
@digizeph

Copy link
Copy Markdown
Member

Independent check of the TCP-AO path on a kernel that implements it, since CI was the only place it had run.

Setup: Linux 6.8 with CONFIG_TCP_AO=y, BIRD 3.4.0 from source, focl from this PR's head, both unprivileged on loopback (focld 127.0.0.1:2179, BIRD 127.0.0.2:3179).

The script in this PR passes as written: focld active with HMAC-SHA-1-96 and BIRD active with AES-128-CMAC-96, Established with a route each way.

Ten more cases, all as expected:

  • no auth (control): up
  • AO HMAC-SHA-1-96, focld active: up, auth_mode tcp-ao
  • AO AES-128-CMAC-96, BIRD active: up
  • wrong AO secret: no session, TCPAOBad +8
  • BIRD requires AO, focld has no key: no session, TCPAORequired +9
  • focld has a key, peer sends no AO: no session, TCPAOKeyNotFound +9; the kernel drops the unsigned SYN, so accept() never returns and the accept-side re-check is not exercised here
  • MD5, focld active: up, SYN carries the digest
  • MD5, [::] listener with an IPv4 peer: up, so the v4-mapped match works
  • MD5 wrong password: no session
  • AO, dual-stack listener with an IPv4 peer: up

Captures show tcp-ao keyid 1 rnextkeyid 2 mac on every segment including the SYN, and TCPAOGood moves (134 to 148) only for the authenticated session.

Two operator-relevant behaviours: one listener with one keyed and one unauthenticated peer brings both up, which is what the no-ao_required decision buys; and after BIRD restarts the AO session returns with keys.

Failure modes: on a kernel without TCP-AO (6.12, CONFIG_TCP_AO unset), focl peer show reports "failed to add TCP-AO key ... (os error 92) (TCP-AO needs Linux 6.7+ built with CONFIG_TCP_AO)", no crash, no silent downgrade. Config validation rejects password plus tcp_ao_keys, duplicate send_id, two current keys, keys on a hostname peer, and an empty secret at startup.

Not covered: live key rotation (comments say restart), passive-side rnext, IPv6 transport, BIRD 2.17, production-scale timing.

Working through the diff now; findings to follow.

digizeph and others added 14 commits November 16, 2026 13:44
…ilures

- Compare the configured and remote peer addresses after canonicalizing both,
  so a peer configured as an IPv4-mapped literal matches its own inbound
  connection again.
- Attempt every listener key of a peer and record a refused key in
  PeerInfo.last_error instead of only logging it at startup.
- Require a literal peer address for every authentication mode: the session
  path and the listener already require one.
- Redact PeerAuth in Debug and compute peer_auth once per peer runtime.
- Probe TCP-AO support through focld in the interop script; /proc/net/netstat
  lists the TCPAO counters on a kernel built without CONFIG_TCP_AO.
- Document that current applies to active sessions, and correct the TCPAO
  counter glossary for a listener without ao_required.
…utput

PeerConfig and TcpAoKeyConfig derived Debug, so printing a peer's config
with {:?} wrote the TCP-MD5 password or the TCP-AO secret in clear. Both
now print <redacted> in its place, like PeerAuth already does.

The hand-written impls destructure the struct, so a field added later is a
compile error there instead of one that silently goes unprinted.

Assisted-by: Claude Code
…ped peers as IPv4

The listener key is the only thing that authenticates an inbound TCP-MD5
connection: the kernel drops an unsigned or wrongly signed SYN because of
it and hands it on to the accepted socket. Installing the key again on the
accepted socket proved nothing, as TCP_MD5SIG installs fine on a
connection that was never signed and cannot be read back. A failed listener
install was recorded in last_error, but that peer's unsigned connection
still got a session.

- Install the keys on the bound socket before listen(), which closes the
  window in which a SYN could beat its key.
- Remember the peers the listener holds no key for and drop their inbound
  connections. A TCP-AO peer with at least one key on the listener is
  still accepted: the kernel drops its unsigned SYN, and the accepted
  connection is checked to be TCP-AO protected.
- Drop the TCP-MD5 re-install on accepted sockets.

A peer configured v4-mapped (::ffff:192.0.2.2) matched its own connections
but was keyed under that IPv6 spelling, which an IPv4 listener refuses.
The configured address is canonicalized for the listener key as well.

Linux gives an accepted TCP-AO connection the keys its SYN names and
refuses a current key on a listener, so `current` cannot take effect on a
connection the peer opens. Log a warning when such a session runs on other
keys than the one marked current, and say so in the docs.

Assisted-by: Claude Code
- BgpService::new validates its config. FoclConfig::load already did, but
  a config built in code reached peer_auth unchecked, where a peer with
  both a password and TCP-AO keys silently ran with TCP-AO only.
- [[peers]] and [[peers.tcp_ao_keys]] reject unknown keys. A misspelled
  `tcp_ao_keys` or `password` left the peer unauthenticated, and a
  misspelled `current` dropped the mark.
- Startup fails when an enabled peer has TCP-AO keys and the kernel has no
  TCP-AO, naming the peers. Such a peer could never get a session and only
  showed as a failing connect per retry or a refused listener key. The
  check lives in BgpService rather than FoclConfig::validate, which stays
  independent of the host; a disabled peer may keep its keys. The interop
  script's TCP-AO probe reads that startup error now, instead of the
  probe peer's last_error.
- A peer whose address family the listener cannot accept is no longer
  logged as an error, nor given a last_error, per authenticated peer.
  Active peers dial out and are not affected; passive ones get one warning
  and a last_error at listener start, with or without authentication. This
  now also covers an IPv4 peer on a listener bound to one IPv6 address,
  which got a useless v4-mapped key and no log. Such a peer counts as
  unkeyed, so it is refused should it ever arrive.
- PeerAuthMode serializes in snake_case (`tcp_ao`) like PeerState.

Assisted-by: Claude Code
TCP-AO brought in linux-raw-sys 0.12 while rustix 1.1.3, which only the
tempfile dev-dependency pulls in, still held linux-raw-sys 0.11.0 in the
lockfile. rustix 1.1.6 moved to 0.12, so the tree is back to one version.

Lockfile only: the other edges, windows-sys among them, stay as they were.

Assisted-by: Claude Code
…ebug output

ArchiveDestinationConfig derived Debug, so printing an archive destination
or the whole config with {:?} wrote both in clear. They now print
<redacted>, like the TCP-MD5 password and the TCP-AO secret.

The access key id stays in the output: it names the credential in use and
is no secret, as every signed request carries it in clear.

Assisted-by: Claude Code
…secrets

peer_auth returned an owned PeerAuth, so every call copied the TCP-MD5
password or all TCP-AO keys out of the PeerConfig: once to fill in a
peer's auth_mode, once per connect attempt and once per accepted
connection. PeerAuth now borrows from the config, which stays the only
place the secrets live.

No change in behaviour.

Assisted-by: Claude Code
PeerAuth's Debug output only counted a peer's TCP-AO keys. TcpAoKeyConfig
leaves its secret out of its own Debug output, so list the keys through
it: the KeyIDs, the algorithm and the current mark show while the secrets
stay out.

Assisted-by: Claude Code
… key errors

- Reject two peers that name the same IP in different spellings: one inbound
  connection matched both, and the listener kept only one of their keys.
- Try every resolved listen_addr with socket creation and SO_REUSEADDR inside
  the attempt, so an address family this host cannot open falls through like
  TcpListener::bind instead of aborting startup.
- Report a refused listener key in PeerInfo.listener_key_error, which later
  session errors and an established session do not clear.
- Document the TCPAO counters as the kernel counts them: an unsigned segment on
  a listener that holds a key is TCPAORequired.
- Tests: duplicate spellings rejected, bind fall-through and last-error, the
  sticky listener key error, and one listener serving a keyed and an unkeyed
  peer.
…lemented key

- tcp_ao_probe() separates "the kernel has no TCP-AO" (ENOPROTOOPT) from a
  failed probe (any other errno), so startup never blames CONFIG_TCP_AO for an
  EMFILE or EPERM.
- The listener reads IPV6_V6ONLY from the bound socket: with bindv6only on, an
  IPv4 peer is unkeyable and is reported as unreachable instead of sitting idle
  without a reason.
- AO-dependent tests still skip on a kernel without TCP-AO, but refuse to skip
  when FOCL_REQUIRE_TCP_AO=1, which CI now sets; a green run can no longer hide
  an untested TCP-AO path.
- docs/examples.md stops advertising ribs_mode, which ArchiveConfig does not
  define; the sizing note marks the mode as planned.
… docs

Round-3 review of d66803f found that the new probe made every start depend on
getsockopt(TCP_AO_INFO): a host where the probe fails (EPERM, EMFILE) refused a
configuration with no TCP-AO peer. Probe only when an enabled peer has keys.

- probe_kernel_tcp_ao() skips the probe when nothing needs it and keeps the
  context on a real failure; tested for a plain config, a disabled AO peer, an
  enabled AO peer and a failing probe.
- README and CHANGELOG: an unsigned segment at a socket that requires TCP-AO is
  TCPAORequired; TCPAOKeyNotFound is a segment carrying the option whose KeyID
  matched no key.
- The IPV6_V6ONLY test sets the option on both sockets and is Linux-gated; a
  failed getsockopt now logs instead of silently assuming dual-stack.
- New acceptance case: one listener serves a TCP-AO peer and an unauthenticated
  peer (the MD5 variant covered only MD5).
- tcp_ao_probe is re-exported so the rustdoc link resolves; the last-error test
  now injects two different errors; the spliced doc comment is repaired.
- CI pins ubuntu-24.04 for the job that must have CONFIG_TCP_AO.
The lab saw a session whose peer stayed `idle` in `focl peer show` while the
kernel had it established; the case log could not say whether the active loop
ran or the update missed the peer map. Log the loop start, every state
transition, and an update that finds no peer, so the next occurrence is
diagnosable from the daemon log alone.
…docs

Round 4 found that start_peers spawned the active loop inside make_runtime,
before the runtime was in the map: the loop's first set_peer_state(Connect)
could land on a peer that was not registered yet, so the transition and its
archive record were dropped and the new diagnostic warned about a peer that
was running. Register every peer under the map lock, then spawn and store the
handle inside the same critical section.

- Test: 40 active peers on a multi-thread runtime must each report their first
  state; it fails 5/5 with the spawn-before-insert order and passes with the
  fix.
- README: TCPAORequired also covers an accepted AO session whose keys were
  copied from a keyed listener.
- CHANGELOG: the IPV6_V6ONLY read is Linux-only, and tcp_ao_probe is part of the
  documented re-export list.
- Docs: move require_tcp_ao_support's paragraph back above it.
@digizeph
digizeph merged commit 81046a7 into bgpkit:main Nov 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants