ntop / n2n

Peer-to-peer VPN
GNU General Public License v3.0
6.05k stars 925 forks source link

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

Open sbrodeur opened 3 months ago

sbrodeur commented 3 months ago

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 nlpid 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:

hamishcoleman commented 3 months ago

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