Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let the kernel takes care of assigning the port ID of the netlink socket on Linux #1169

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

sbrodeur
Copy link

@sbrodeur sbrodeur commented Mar 26, 2024

Let the kernel takes care of assigning the port ID of the netlink socket on Linux.

I had a problem where I couldn't start an n2n edge node under Docker, because it happened that the netlink socket created by n2n with port ID set to the internal process ID would conflict another process on the host. I would get an error like "Could not bind NETLINK socket: Address already in use [98]". This is because the container's process has two process IDs, one that for inside the container and the another in the host. By calling getpid() from inside the container, n2n seems to get the internal process ID rather than an unique process ID on the host. The error did not occurred when running the n2n edge node on the host (i.e. without Docker), because then the process ID obtained would be unique, and so the port ID of the netlink socket.

As per the documentation from https://man7.org/linux/man-pages/man7/netlink.7.html :
If the application sets nl_pid before calling bind(2), then it is up to
the application to make sure that nl_pid is unique. If the
application sets it to 0, the kernel takes care of assigning it.
The kernel assigns the process ID to the first netlink socket the
process opens and assigns a unique nl_pid to every netlink socket
that the process subsequently creates.

It seems it would be best for the application to set the port ID to 0 and let the kernel handle this.

Please sign (check) the below before submitting the Pull Request:

Link to the related issue:

Describe changes:

  • Set nl_pid to 0, to avoid potential conflicts when running under Docker.

Let the kernel takes care of assigning the port ID of the netlink socket
hamishcoleman pushed a commit to hamishcoleman/n3n that referenced this pull request Mar 27, 2024
Let the kernel takes care of assigning the port ID of the netlink socket.

The netlink socket id uses the same namespace in all docker containers,
so if the userspace code assigns the nl_pid then there is a possiblity
of a collision, resulting in errors like:
    "Could not bind NETLINK socket: Address already in use [98]"

Cherry-picked from 0df939ed5b0a7013a73f2df78f6a805b5ec555b3 in ntop/n2n#1169

Thanks @sbrodeur
@hamishcoleman
Copy link
Collaborator

Just in case github didnt send you the update, I have merged your fix into the recently released fork n3n - Thanks!

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