net: remove CONFIG_NETDEVICES requirement for SO_BINDTODEVICE and SO_BINDTOIFINDEX
Subsystems (3)
PATCH 1/1 net: remove CONFIG_NETDEVICES requirement for SO_BINDTODEVICE and SO_BINDTOIFINDEX
The SO_BINDTODEVICE and SO_BINDTOIFINDEX sockopts return -ENOPROTOOPT if CONFIG_NETDEVICES is disabled, even if the lo interface exists. I believe it would be prudent to allow for valid bindings even if CONFIG_NETDEVICES were disabled, as an error here would be inconsistent.
--- net/core/sock.c | 56 +++++++++++++------------------------------------ 1 file changed, 15 insertions(+), 41 deletions(-)
net/core/sock.c +15 -41
@@ -634,18 +634,14 @@ EXPORT_SYMBOL(sk_dst_check);static int sock_bindtoindex_locked (struct sock *sk,int ifindex) { -int ret = -ENOPROTOOPT ; -#ifdef CONFIG_NETDEVICES struct net *net =sock_net (sk);/* Sorry... */ - ret = -EPERM ;if (sk->sk_bound_dev_if && !ns_capable (net->user_ns,CAP_NET_RAW )) - goto out; +return -EPERM ; - ret = -EINVAL ;if (ifindex <0 ) - goto out; +return -EINVAL ;/* Paired with all READ_ONCE() done locklessly. */ WRITE_ONCE (sk->sk_bound_dev_if, ifindex);
@@ -654,12 +650,7 @@ static int sock_bindtoindex_locked(struct sock *sk, int ifindex) sk->sk_prot->rehash (sk);sk_dst_reset (sk); - ret =0 ; - -out: -#endif - -return ret; +return 0 ; }int sock_bindtoindex (struct sock *sk,int ifindex,bool lock_sk)
@@ -678,15 +669,12 @@ EXPORT_SYMBOL(sock_bindtoindex);static int sock_setbindtodevice (struct sock *sk,sockptr_t optval,int optlen) { -int ret = -ENOPROTOOPT ; -#ifdef CONFIG_NETDEVICES +int ret, index;struct net *net =sock_net (sk);char devname[IFNAMSIZ ]; -int index; - ret = -EINVAL ;if (optlen <0 ) - goto out; +return -EINVAL ;/* Bind this socket to a particular device like "eth0", * as specified in the passed interface name. If the
@@ -697,9 +685,8 @@ static int sock_setbindtodevice(struct sock *sk, sockptr_t optval, int optlen) optlen =IFNAMSIZ -1 ;memset (devname,0 ,sizeof (devname)); - ret = -EFAULT ;if (copy_from_sockptr (devname, optval, optlen)) - goto out; +return -EFAULT ; index =0 ;if (devname[0 ] !='\0' ) {
@@ -710,16 +697,13 @@ static int sock_setbindtodevice(struct sock *sk, sockptr_t optval, int optlen)if (dev) index = dev->ifindex;rcu_read_unlock (); - ret = -ENODEV ;if (!dev) - goto out; +return -ENODEV ; }sockopt_lock_sock (sk); ret =sock_bindtoindex_locked (sk, index);sockopt_release_sock (sk); -out: -#endif return ret; }
@@ -727,42 +711,32 @@ static int sock_setbindtodevice(struct sock *sk, sockptr_t optval, int optlen)static int sock_getbindtodevice (struct sock *sk,sockptr_t optval,sockptr_t optlen,int len) { -int ret = -ENOPROTOOPT ; -#ifdef CONFIG_NETDEVICES -int bound_dev_if =READ_ONCE (sk->sk_bound_dev_if); +int ret, bound_dev_if =READ_ONCE (sk->sk_bound_dev_if);struct net *net =sock_net (sk);char devname[IFNAMSIZ ];if (bound_dev_if ==0 ) { len =0 ; - goto zero; + goto copy_len; } - ret = -EINVAL ;if (len <IFNAMSIZ ) - goto out; +return -EINVAL ; ret =netdev_get_name (net, devname, bound_dev_if);if (ret) - goto out; +return ret; len =strlen (devname) +1 ; - ret = -EFAULT ;if (copy_to_sockptr (optval, devname, len)) - goto out; +return -EFAULT ; -zero: - ret = -EFAULT ; +copy_len:if (copy_to_sockptr (optlen, &len,sizeof (int ))) - goto out; - - ret =0 ; - -out: -#endif +return -EFAULT ; -return ret; +return 0 ; }bool sk_mc_loop (const struct sock *sk)
Signature · 1 line
2.56.0
Kuniyuki Iwashima
maintainerQuoted 51 lines · Ethan Ferguson
The
SO_BINDTODEVICEandSO_BINDTOIFINDEXsockopts return-ENOPROTOOPTifCONFIG_NETDEVICESis disabled, even if the lo interface exists. I believe it would be prudent to allow for valid bindings even ifCONFIG_NETDEVICESwere disabled, as an error here would be inconsistent.Signed-off-by Ethan Ferguson <[email protected]>--- net/core/sock.c | 56 +++++++++++++------------------------------------ 1 file changed, 15 insertions(+), 41 deletions(-)net/core/sock.c +3
-12@@ -634,18 +634,14 @@ EXPORT_SYMBOL(sk_dst_check);static int sock_bindtoindex_locked (struct sock *sk,int ifindex) { -int ret = -ENOPROTOOPT ; -#ifdef CONFIG_NETDEVICES struct net *net =sock_net (sk);/* Sorry... */ - ret = -EPERM ;if (sk->sk_bound_dev_if && !ns_capable (net->user_ns,CAP_NET_RAW )) - goto out; +return -EPERM ; - ret = -EINVAL ;if (ifindex <0 ) - goto out; +return -EINVAL ;/* Paired with all READ_ONCE() done locklessly. */ WRITE_ONCE (sk->sk_bound_dev_if, ifindex);@@ -654,12 +650,7 @@ static int sock_bindtoindex_locked(struct sock *sk, int ifindex) sk->sk_prot->rehash (sk);sk_dst_reset (sk); - ret =0 ; - -out: -#endif - -return ret; +return 0 ; }int sock_bindtoindex (struct sock *sk,int ifindex,bool lock_sk)
net/core/sock.c EXPORT_SYMBOL(sock_bindtoindex); · line 685
Quoted 9 lines
net/core/sock.c +1
-3@@ -678,15 +669,12 @@ EXPORT_SYMBOL(sock_bindtoindex);static int sock_setbindtodevice (struct sock *sk,sockptr_t optval,int optlen) { -int ret = -ENOPROTOOPT ; -#ifdef CONFIG_NETDEVICES +int ret, index;struct net *net =sock_net (sk);char devname[IFNAMSIZ ]; -int index;
nit: keep the reverse xmas tree order.
Also, please specify the target tree in the Subject.
[PATCH net-next v2] ....
See Documentation/process/maintainer-netdev.rst
---
pw-bot: cr