Skip to content

fix(netlink): request unique port identifiers - #910

Open
stephenlclarke wants to merge 1 commit into
apple:mainfrom
stephenlclarke:upstream/netlink-port-identifiers
Open

stephenlclarke wants to merge 1 commit into
apple:mainfrom
stephenlclarke:upstream/netlink-port-identifiers

Conversation

@stephenlclarke

Copy link
Copy Markdown
Contributor

Summary

Allow multiple Netlink route sockets in one process by requesting kernel-assigned port identifiers.

Binding every socket with nl_pid = getpid() makes the second concurrent socket collide with the first. Bind with port identifier zero, read the unique assigned identifier back with getsockname, and keep exposing it through the existing pid property.

The initialization path also closes the descriptor on every post-socket() failure and reports a dedicated socket-name error when the assigned address cannot be read or validated.

Testing

Added a Linux-only regression that opens eight concurrent route sockets and verifies every assigned port identifier is non-zero and distinct.

The installed macOS cross-compilation SDK is not compatible with the active Apple Swift compiler, so the Linux test is intentionally delegated to this pull request's native Linux CI rather than treated as locally executed evidence. git diff --check and focused Swift formatting of the added code pass.

Compatibility

The public property name remains pid for source compatibility, but its documentation now reflects Netlink semantics: it is a socket port identifier, not necessarily the process ID.

@stephenlclarke
stephenlclarke force-pushed the upstream/netlink-port-identifiers branch from 2ee4d23 to ed533ab Compare September 28, 2026 08:03
guard let ptr = buffer.bind(as: sockaddr.self, size: buffer.count) else {
throw NetlinkSocketError.bindFailure(rc: 0)
}
guard osBind(socketFD, ptr, UInt32(buffer.count)) >= 0 else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use socklen_t throughout if you can.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in d35cb51acce19b7df5ea5d0cf75e88e88a303331 and rebased onto current upstream. Both the bind buffer length and returned-address length comparison now use socklen_t; only the kernel netlink port identifier remains UInt32. The current ContainerizationNetlink production target cross-compiles successfully for aarch64 Linux/musl with Swift 6.3 and warnings as errors. This is compilation proof; I have not relabelled the earlier VM test failures as passes.

@stephenlclarke
stephenlclarke force-pushed the upstream/netlink-port-identifiers branch from ed533ab to d35cb51 Compare October 19, 2026 07:28

This branch has not been deployed

No deployments
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