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

Add SO_BINDTOIFINDEX constant to socket module #104061

Closed
viccie30 opened this issue May 1, 2023 · 1 comment
Closed

Add SO_BINDTOIFINDEX constant to socket module #104061

viccie30 opened this issue May 1, 2023 · 1 comment
Labels
3.13 new features, bugs and security fixes extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@viccie30
Copy link
Contributor

viccie30 commented May 1, 2023

Feature or enhancement

Since Linux 5.0 socket option SO_BINDTOIFINDEX is available to avoid a race condition between SO_BINDTODEVICE and network interface renaming. Add this constant to Modules/socketmodule.c if available.

Pitch

I will shamelessly steal David Herrmann's reasoning in his proposal to add SO_BINDTOIFINDEX to the Linux kernel:

This introduces a new generic SOL_SOCKET-level socket option called SO_BINDTOIFINDEX. It behaves similar to SO_BINDTODEVICE, but takes a network interface index as argument, rather than the network interface name.

User-space often refers to network-interfaces via their index, but has to temporarily resolve it to a name for a call into SO_BINDTODEVICE. This might pose problems when the network-device is renamed asynchronously by other parts of the system. When this happens, the SO_BINDTODEVICE might either fail, or worse, it might bind to the wrong device.

In most cases user-space only ever operates on devices which they either manage themselves, or otherwise have a guarantee that the device name will not change (e.g., devices that are UP cannot be renamed). However, particularly in libraries this guarantee is non-obvious and it would be nice if that race-condition would simply not exist. It would make it easier for those libraries to operate even in situations where the device-name might change under the hood.

A real use-case that we recently hit is trying to start the network stack early in the initrd but make it survive into the real system. Existing distributions rename network-interfaces during the transition from initrd into the real system. This, obviously, cannot affect devices that are up and running (unless you also consider moving them between network-namespaces). However, the network manager now has to make sure its management engine for dormant devices will not run in parallel to these renames. Particularly, when you offload operations like DHCP into separate processes, these might setup their sockets early, and thus have to resolve the device-name possibly running into this race-condition.

By avoiding a call to resolve the device-name, we no longer depend on the name and can run network setup of dormant devices in parallel to the transition off the initrd. The SO_BINDTOIFINDEX ioctl plugs this race.

Previous discussion

I have not discussed this addition. It looks like similar additions, like #100813 or #91968, were not discussed outside of Github either.

Linked PRs

@viccie30 viccie30 added the type-feature A feature request or enhancement label May 1, 2023
@arhadthedev arhadthedev added the extension-modules C modules in the Modules dir label May 2, 2023
@serhiy-storchaka serhiy-storchaka added the 3.13 new features, bugs and security fixes label Feb 2, 2024
serhiy-storchaka pushed a commit that referenced this issue Feb 20, 2024
Add socket.SO_BINDTOIFINDEX constant

This socket option avoids a race condition between SO_BINDTODEVICE and network interface renaming.
@serhiy-storchaka
Copy link
Member

Thank you for your contribution @viccie30.

woodruffw pushed a commit to woodruffw-forks/cpython that referenced this issue Mar 4, 2024
Add socket.SO_BINDTOIFINDEX constant

This socket option avoids a race condition between SO_BINDTODEVICE and network interface renaming.
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
Add socket.SO_BINDTOIFINDEX constant

This socket option avoids a race condition between SO_BINDTODEVICE and network interface renaming.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 new features, bugs and security fixes extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants