go-linux

Routing Netlink decisions

Part of the accepted-design index.

Typed facilities over the shared exchange

The base Netlink package already owns sender validation, sequence matching, multipart completion, memory bounds, diagnostics, polling, and descriptor lifetime. Routing depends on that package rather than reimplementing its exchange or adding protocol-specific branches to the transport. Request and multicast sockets remain separate; multiplexing them would require a receive loop, pending-request registry, buffering policy, and cancellation model without a current requirement. Standard net supplies network I/O and basic interface inspection, but does not express Linux configuration, table IDs, or change notifications.

Fixed headers use native-endian fields and maintained x/sys/unix sizes/constants, with the Linux 7.0 routing UAPI, link UAPI, and address UAPI as the baseline. There are no unsafe structure casts or copied syscall tables. netip preserves IPv4/IPv6 identity and address prefix host bits. Linux indices, scopes, masks, table IDs, and installer protocols retain their native meanings instead of being coerced into superficially similar standard-library flags.

Observation and request boundaries

Observed records preserve opaque attributes and unknown output bits so a newer kernel or advanced route does not become an inaccurate simplified record. Interpretation validates only known singleton fields; unknown nests are not recursively validated. Parsers are public because base Netlink consumers can decode the same records without opening another socket. List operations discard partial results on failure: a typed list must not look like a successful inventory after interruption. Applications needing partial wire diagnostics can use the base exchange directly.

Mutations have separate configs. Reusing observations as requests would risk dropping opaque multipath, encapsulation, lifetimes, or source selectors while claiming to replace a complete object. A caller explicitly chooses an IP family, masked route destination, mutable link mask, and sufficiently precise deletion identity. These choices avoid implicit cross-family queries and accidental whole-record rewrites.

Configure private virtual networks on the existing connection

Creating a veth pair in one request gives the kernel responsibility for registering both endpoints and cleaning up a failed creation. A bridge uses the same simple creation path as a dummy link. Master changes have dedicated methods so clearing membership is explicit and an ordinary LinkChange leaves it alone. The kernel can detach an existing master before a replacement fails. A namespace move can likewise complete before a requested rename is rejected. Preserving these effects and the original ACK lets callers reconcile against kernel state after an error. Linux 7.0 veth implementation, Linux 7.0 routing implementation.

The move targets a borrowed namespace.Handle. Routing and mount share the module-private namespace reference representation. Routing validates the type recorded at adoption, so a closed connection still reports closure when the caller’s thread policy denies namespace inspection. Routing duplicates the validated handle with close-on-exec while the original is protected, then holds that duplicate through the request. A concurrent caller close can release its handle while the request still has a stable namespace reference. The operation needs no thread transition and the namespace package stays independent of routing.

The vishvananda/netlink v1.3.1 link helpers cover veth, bridges, masters, and descriptor-based moves, but execute on that library’s own routing sockets. Using them here would split namespace capture, deadlines, and structured ACK handling across two transports. Its attribute encoder would still leave the typed validation and lifetime work in this package. These operations need a small set of link attributes on the existing exchange, so no additional backend or dependency is needed. Upstream link implementation, upstream exchange implementation.

Neighbors and policy rules use native matching

The neighbor cache uses the interface index and family-specific address as its key. Exclusive creation and change-only replacement map directly to kernel requests. The routing-rule handler implements exclusive insertion and first-match deletion. Priorities can repeat, and deletion uses wildcard fields and a different predicate from duplicate detection. Requiring a priority and action makes the deletion boundary visible, while unique priorities remain an application choice. A userspace replacement sequence would expose an intermediate policy and could yield two rules or no rule after failure. Linux 7.0 neighbor implementation, Linux 7.0 rule implementation.

The observed rule keeps raw attributes so a controller can detect configuration outside its typed mutation surface. The listener shares the kernel neighbor group with the bridge forwarding database and distinguishes these records by address family. Forwarding database entries use link-layer identity and remain outside the IP neighbor API. Loss or truncation still surfaces from the underlying socket before decoding.

The vishvananda/netlink v1.3.1 neighbor and rule methods run on their own handles and request exchange. Reusing those methods would split namespace capture, deadlines, and structured ACK/loss behavior. The existing transport and attribute codec already cover the wire framing. The typed facade therefore adds only the family layouts and matching inputs. Upstream neighbor implementation, upstream rule implementation.

Preserve kernel application semantics

Native NEW/DEL operations with explicit exclusive-create or replace flags retain errno and kernel matching semantics. Preflight existence checks and userspace delete/recreate would add races and could not promise rollback. Address replacement is particularly nonuniform: the Linux 7.0 IPv4 handler and IPv6 handler can create an absent address even without CREATE, and IPv4 replacement retains several properties. The usage contract exposes this restriction instead of inventing a racy change-only guarantee. A multi-property link request likewise need not apply atomically.

Kernel objects persist independently of the configuration connection. Local socket cleanup is unconditional on acquisition failure, while a transport failure after mutation cannot establish which kernel effects occurred. Automatic retries, rollback, and transactions would imply guarantees Linux does not supply. Lossy notifications similarly require consumer reconciliation over a separate connection; an atomic state-plus-subscription abstraction would need a substantially stronger protocol than a dump followed by reads.

Distinguish path resolution from route configuration

The kernel can resolve a flow to one interface and gateway even when the installed route has a different prefix or several paths. LookupRoute returns the resolved flow path and MatchRoute requests the installed entry with FIB matching. IPv4 requests include RTM_F_LOOKUP_TABLE to recover the selected table. IPv6 reports its table directly and rejects that flag under strict validation. Both use the connection’s existing exchange and namespace. Linux 7.0 IPv4 lookup, Linux 7.0 IPv6 lookup.

The existing route observation keeps all of its attributes. Decoding extended paths on demand preserves the ordinary dump and notification boundary. The optional inline view of a nexthop object has an eight-bit weight field, while object groups carry a two-byte effective weight. Callers managing shared objects read the object by ID to recover that weight and preserve the group’s identity. Inline paths and object references use an explicit path configuration alongside the existing route identity. Encapsulation, metrics, and resilient group state stay observable as raw attributes. IPv6 applies some inline mutations per path, with partial effects and a kernel acknowledgement flag rewrite described in the Netlink exchange decision. Linux 7.0 route UAPI, Linux 7.0 nexthop UAPI.

The vishvananda/netlink v1.3.1 route helpers resolve lookups and encode inline multipath through their own handle and transport. The release does not supply the nexthop object lifecycle. Using its helpers would split deadline enforcement, namespace capture, and ACK handling. The existing Go exchange, attribute codec, and x/sys/unix layouts provide the shared implementation, and this package owns the additional routing layouts. Upstream lookup and route handling, upstream exchange.