Skip to content

Commit 70c5899

Browse files
SPYFFAlexei Starovoitov
authored andcommitted
bpf: Allow SO_BINDTODEVICE opt in bpf_setsockopt
Extending the supported sockopts in bpf_setsockopt with SO_BINDTODEVICE. We call sock_bindtoindex with parameter lock_sk = false in this context because we already owning the socket. Signed-off-by: Ferenc Fejes <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/4149e304867b8d5a606a305bc59e29b063e51f49.1590871065.git.fejes@inf.elte.hu
1 parent 8ea204c commit 70c5899

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

net/core/filter.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4248,6 +4248,9 @@ static const struct bpf_func_proto bpf_get_socket_uid_proto = {
42484248
static int _bpf_setsockopt(struct sock *sk, int level, int optname,
42494249
char *optval, int optlen, u32 flags)
42504250
{
4251+
char devname[IFNAMSIZ];
4252+
struct net *net;
4253+
int ifindex;
42514254
int ret = 0;
42524255
int val;
42534256

@@ -4257,7 +4260,7 @@ static int _bpf_setsockopt(struct sock *sk, int level, int optname,
42574260
sock_owned_by_me(sk);
42584261

42594262
if (level == SOL_SOCKET) {
4260-
if (optlen != sizeof(int))
4263+
if (optlen != sizeof(int) && optname != SO_BINDTODEVICE)
42614264
return -EINVAL;
42624265
val = *((int *)optval);
42634266

@@ -4298,6 +4301,29 @@ static int _bpf_setsockopt(struct sock *sk, int level, int optname,
42984301
sk_dst_reset(sk);
42994302
}
43004303
break;
4304+
case SO_BINDTODEVICE:
4305+
ret = -ENOPROTOOPT;
4306+
#ifdef CONFIG_NETDEVICES
4307+
optlen = min_t(long, optlen, IFNAMSIZ - 1);
4308+
strncpy(devname, optval, optlen);
4309+
devname[optlen] = 0;
4310+
4311+
ifindex = 0;
4312+
if (devname[0] != '\0') {
4313+
struct net_device *dev;
4314+
4315+
ret = -ENODEV;
4316+
4317+
net = sock_net(sk);
4318+
dev = dev_get_by_name(net, devname);
4319+
if (!dev)
4320+
break;
4321+
ifindex = dev->ifindex;
4322+
dev_put(dev);
4323+
}
4324+
ret = sock_bindtoindex(sk, ifindex, false);
4325+
#endif
4326+
break;
43014327
default:
43024328
ret = -EINVAL;
43034329
}

0 commit comments

Comments
 (0)