net: introduce SO_BINDTOIFINDEX sockopt
Subsystems (11)
PATCH v2 1/3 net: introduce SO_BINDTOIFINDEX sockopt
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.
---
v2:
- Rename to SO_BINDTOIFINDEX from SO_BINDTOIF
- skip 0x0040 SO-value on sparc, as it is already used
arch/alpha/include/uapi/asm/socket.h | 2 ++ arch/ia64/include/uapi/asm/socket.h | 2 ++ arch/mips/include/uapi/asm/socket.h | 2 ++ arch/parisc/include/uapi/asm/socket.h | 2 ++ arch/s390/include/uapi/asm/socket.h | 2 ++ arch/sparc/include/uapi/asm/socket.h | 2 ++ arch/xtensa/include/uapi/asm/socket.h | 2 ++ include/uapi/asm-generic/socket.h | 2 ++ net/core/sock.c | 46 +++++++++++++++++++++------ 9 files changed, 52 insertions(+), 10 deletions(-)
arch/alpha/include/uapi/asm/socket.h +2 -0
@@ -115,4 +115,6 @@#define SO_TXTIME 61#define SCM_TXTIME SO_TXTIME +#define SO_BINDTOIFINDEX 62 +#endif /* _UAPI_ASM_SOCKET_H */
arch/ia64/include/uapi/asm/socket.h +2 -0
@@ -117,4 +117,6 @@#define SO_TXTIME 61#define SCM_TXTIME SO_TXTIME +#define SO_BINDTOIFINDEX 62 +#endif /* _ASM_IA64_SOCKET_H */
arch/mips/include/uapi/asm/socket.h +2 -0
@@ -126,4 +126,6 @@#define SO_TXTIME 61#define SCM_TXTIME SO_TXTIME +#define SO_BINDTOIFINDEX 62 +#endif /* _UAPI_ASM_SOCKET_H */
arch/parisc/include/uapi/asm/socket.h +2 -0
@@ -107,4 +107,6 @@#define SO_TXTIME 0x4036#define SCM_TXTIME SO_TXTIME +#define SO_BINDTOIFINDEX 0x4037 +#endif /* _UAPI_ASM_SOCKET_H */
arch/s390/include/uapi/asm/socket.h +2 -0
@@ -114,4 +114,6 @@#define SO_TXTIME 61#define SCM_TXTIME SO_TXTIME +#define SO_BINDTOIFINDEX 62 +#endif /* _ASM_SOCKET_H */
arch/sparc/include/uapi/asm/socket.h +2 -0
@@ -104,6 +104,8 @@#define SO_TXTIME 0x003f#define SCM_TXTIME SO_TXTIME +#define SO_BINDTOIFINDEX 0x0041 +/* Security levels - as per NRL IPv6 - don't actually do anything */ #define SO_SECURITY_AUTHENTICATION 0x5001#define SO_SECURITY_ENCRYPTION_TRANSPORT 0x5002
arch/xtensa/include/uapi/asm/socket.h +2 -0
@@ -119,4 +119,6 @@#define SO_TXTIME 61#define SCM_TXTIME SO_TXTIME +#define SO_BINDTOIFINDEX 62 +#endif /* _XTENSA_SOCKET_H */
include/uapi/asm-generic/socket.h +2 -0
@@ -110,4 +110,6 @@#define SO_TXTIME 61#define SCM_TXTIME SO_TXTIME +#define SO_BINDTOIFINDEX 62 +#endif /* __ASM_GENERIC_SOCKET_H */
net/core/sock.c +36 -10
82 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v2 2/3 net/ipv4/udp_tunnel: prefer SO_BINDTOIFINDEX over SO_BINDTODEVICE
The udp-tunnel setup allows binding sockets to a network device. Prefer the new SO_BINDTOIFINDEX to avoid temporarily resolving the device-name just to look it up in the ioctl again.
---
v2:
- Rename to SO_BINDTOIFINDEX from SO_BINDTOIF
net/ipv4/udp_tunnel.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)
net/ipv4/udp_tunnel.c +3 -12
@@ -21,18 +21,9 @@ int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg, goto error;if (cfg->bind_ifindex) { -struct net_device *dev; - - dev =dev_get_by_index (net, cfg->bind_ifindex); -if (!dev) { - err = -ENODEV ; - goto error; - } - - err =kernel_setsockopt (sock,SOL_SOCKET ,SO_BINDTODEVICE , - dev->name,strlen (dev->name) +1 ); -dev_put (dev); - + err =kernel_setsockopt (sock,SOL_SOCKET ,SO_BINDTOIFINDEX , + (void *)&cfg->bind_ifindex, +sizeof (cfg->bind_ifindex));if (err <0 ) goto error; }
Signature · 1 line
2.20.1
Re: [PATCH v2 2/3] net/ipv4/udp_tunnel: prefer SO_BINDTOIFINDEX over SO_BINDTODEVICE
From: David Herrmann <[email protected]>
Date: Tue, 15 Jan 2019 14:42:15 +0100
Quoted 9 lines
The udp-tunnel setup allows binding sockets to a network device. Prefer the new
SO_BINDTOIFINDEXto avoid temporarily resolving the device-name just to look it up in the ioctl again.Reviewed-by Tom Gundersen <[email protected]>Signed-off-by David Herrmann <[email protected]>---
v2:
- Rename toSO_BINDTOIFINDEXfromSO_BINDTOIF
Applied.
PATCH v2 3/3 net/ipv6/udp_tunnel: prefer SO_BINDTOIFINDEX over SO_BINDTODEVICE
In reply to David HerrmannThe udp-tunnel setup allows binding sockets to a network device. Prefer the new SO_BINDTOIFINDEX to avoid temporarily resolving the device-name just to look it up in the ioctl again.
---
v2:
- Rename to SO_BINDTOIFINDEX from SO_BINDTOIF
net/ipv6/ip6_udp_tunnel.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)
net/ipv6/ip6_udp_tunnel.c +3 -12
@@ -32,18 +32,9 @@ int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg, goto error; }if (cfg->bind_ifindex) { -struct net_device *dev; - - dev =dev_get_by_index (net, cfg->bind_ifindex); -if (!dev) { - err = -ENODEV ; - goto error; - } - - err =kernel_setsockopt (sock,SOL_SOCKET ,SO_BINDTODEVICE , - dev->name,strlen (dev->name) +1 ); -dev_put (dev); - + err =kernel_setsockopt (sock,SOL_SOCKET ,SO_BINDTOIFINDEX , + (void *)&cfg->bind_ifindex, +sizeof (cfg->bind_ifindex));if (err <0 ) goto error; }
Signature · 1 line
2.20.1
Re: [PATCH v2 3/3] net/ipv6/udp_tunnel: prefer SO_BINDTOIFINDEX over SO_BINDTODEVICE
From: David Herrmann <[email protected]>
Date: Tue, 15 Jan 2019 14:42:16 +0100
Quoted 9 lines
The udp-tunnel setup allows binding sockets to a network device. Prefer the new
SO_BINDTOIFINDEXto avoid temporarily resolving the device-name just to look it up in the ioctl again.Reviewed-by Tom Gundersen <[email protected]>Signed-off-by David Herrmann <[email protected]>---
v2:
- Rename toSO_BINDTOIFINDEXfromSO_BINDTOIF
Applied.
Quoted 33 lines · David Herrmann
This introduces a new generic SOL_SOCKET-level socket option called
SO_BINDTOIFINDEX. It behaves similar toSO_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, theSO_BINDTODEVICEmight 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.
Quoted 7 lines
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_BINDTOIFINDEXioctl plugs this race.Reviewed-by Tom Gundersen <[email protected]>Signed-off-by David Herrmann <[email protected]>
This looks great to me, thanks. For the series
From: David Herrmann <[email protected]>
Date: Tue, 15 Jan 2019 14:42:14 +0100
Quoted 37 lines
This introduces a new generic SOL_SOCKET-level socket option called
SO_BINDTOIFINDEX. It behaves similar toSO_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, theSO_BINDTODEVICEmight 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_BINDTOIFINDEXioctl plugs this race.
Quoted 6 lines
Reviewed-by Tom Gundersen <[email protected]>Signed-off-by David Herrmann <[email protected]>---
v2:
- Rename toSO_BINDTOIFINDEXfromSO_BINDTOIF
- skip0x0040SO-value on sparc, as it is already used
Applied.