From patchwork Mon Oct 22 18:04:45 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Haley X-Patchwork-Id: 193251 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id E5C812C0123 for ; Tue, 23 Oct 2012 05:04:52 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932096Ab2JVSEv (ORCPT ); Mon, 22 Oct 2012 14:04:51 -0400 Received: from g1t0027.austin.hp.com ([15.216.28.34]:26871 "EHLO g1t0027.austin.hp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932076Ab2JVSEu (ORCPT ); Mon, 22 Oct 2012 14:04:50 -0400 Received: from g1t0039.austin.hp.com (g1t0039.austin.hp.com [16.236.32.45]) by g1t0027.austin.hp.com (Postfix) with ESMTP id 869EA38259; Mon, 22 Oct 2012 18:04:49 +0000 (UTC) Received: from [10.152.0.30] (openvpn.lnx.usa.hp.com [16.125.113.33]) by g1t0039.austin.hp.com (Postfix) with ESMTP id B3C0434050; Mon, 22 Oct 2012 18:04:46 +0000 (UTC) Message-ID: <50858ABD.2000206@hp.com> Date: Mon, 22 Oct 2012 14:04:45 -0400 From: Brian Haley Organization: HP Cloud Services User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: Pavel Emelyanov CC: Linux Netdev List , David Miller Subject: Re: [PATCH net-next] sockopt: Make SO_BINDTODEVICE readable References: <508123AC.5080208@parallels.com> In-Reply-To: <508123AC.5080208@parallels.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On 10/19/2012 05:55 AM, Pavel Emelyanov wrote: > The SO_BINDTODEVICE option is the only SOL_SOCKET one that can be set, but > cannot be get via sockopt API. The only way we can find the device id a > socket is bound to is via sock-diag interface. But the diag works only on > hashed sockets, while the opt in question can be set for yet unhashed one. > > That said, in order to know what device a socket is bound to (we do want > to know this in checkpoint-restore project) I propose to make this option > getsockopt-able and report the respective device index. > > Another solution to the problem might be to teach the sock-diag reporting > info on unhashed sockets. Should I go this way instead? > > Signed-off-by: Pavel Emelyanov > > --- > > diff --git a/net/core/sock.c b/net/core/sock.c > index 8a146cf..c49412c 100644 > --- a/net/core/sock.c > +++ b/net/core/sock.c > @@ -1074,6 +1074,9 @@ int sock_getsockopt(struct socket *sock, int level, int optname, > case SO_NOFCS: > v.val = sock_flag(sk, SOCK_NOFCS); > break; > + case SO_BINDTODEVICE: > + v.val = sk->sk_bound_dev_if; > + break; > default: > return -ENOPROTOOPT; > } Doesn't this make the set and get non-symmetrical? For example, setsockopt() would take "eth0", but getsockopt() would return 2. The following patch would return a string, or -ENODEV if not set. -Brian --- Change getsockopt(SO_BINDTODEVICE) to be symmetrical with setsockopt() by returning the interface name as a string. Signed-off-by: Brian Haley } -- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/net/core/sock.c b/net/core/sock.c index c49412c..69b9d92 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -505,7 +505,8 @@ struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie) } EXPORT_SYMBOL(sk_dst_check); -static int sock_bindtodevice(struct sock *sk, char __user *optval, int optlen) +static int sock_setbindtodevice(struct sock *sk, char __user *optval, + int optlen) { int ret = -ENOPROTOOPT; #ifdef CONFIG_NETDEVICES @@ -562,6 +563,49 @@ out: return ret; } +static int sock_getbindtodevice(struct sock *sk, char __user *optval, + int __user *optlen, int len) +{ + int ret = -ENOPROTOOPT; +#ifdef CONFIG_NETDEVICES + struct net *net = sock_net(sk); + struct net_device *dev; + char devname[IFNAMSIZ]; + + ret = 0; + if (sk->sk_bound_dev_if == 0) + goto out; + + ret = -EINVAL; + if (len < IFNAMSIZ) + goto out; + if (len > IFNAMSIZ) + len = IFNAMSIZ; + + rcu_read_lock(); + dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if); + if (dev) + strcpy(dev->name, devname); + rcu_read_unlock(); + ret = -ENODEV; + if (!dev) + goto out; + + ret = -EFAULT; + if (copy_to_user(optval, devname, len)) + goto out; + + if (put_user(len, optlen)) + goto out; + + ret = 0; + +out: +#endif + + return ret; +} + static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool) { if (valbool) @@ -589,7 +633,7 @@ int sock_setsockopt(struct socket *sock, int level, int optname, */ if (optname == SO_BINDTODEVICE) - return sock_bindtodevice(sk, optval, optlen); + return sock_setbindtodevice(sk, optval, optlen); if (optlen < sizeof(int)) return -EINVAL; @@ -1074,9 +1118,10 @@ int sock_getsockopt(struct socket *sock, int level, int optname, case SO_NOFCS: v.val = sock_flag(sk, SOCK_NOFCS); break; + case SO_BINDTODEVICE: - v.val = sk->sk_bound_dev_if; - break; + return sock_getbindtodevice(sk, optval, optlen, len); + default: return -ENOPROTOOPT;