Skip to content

Commit a540c81

Browse files
fomichevAlexei Starovoitov
authored andcommitted
selftests/bpf: Extend bind{4,6} programs with a call to bpf_setsockopt
To make sure it doesn't trigger sock_owned_by_me splat. Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20201202172516.3483656-4-sdf@google.com
1 parent 427167c commit a540c81

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

tools/testing/selftests/bpf/progs/bind4_prog.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,33 @@
1919
#define SERV4_REWRITE_IP 0x7f000001U /* 127.0.0.1 */
2020
#define SERV4_REWRITE_PORT 4444
2121

22+
#ifndef IFNAMSIZ
23+
#define IFNAMSIZ 16
24+
#endif
25+
26+
static __inline int bind_to_device(struct bpf_sock_addr *ctx)
27+
{
28+
char veth1[IFNAMSIZ] = "test_sock_addr1";
29+
char veth2[IFNAMSIZ] = "test_sock_addr2";
30+
char missing[IFNAMSIZ] = "nonexistent_dev";
31+
char del_bind[IFNAMSIZ] = "";
32+
33+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
34+
&veth1, sizeof(veth1)))
35+
return 1;
36+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
37+
&veth2, sizeof(veth2)))
38+
return 1;
39+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
40+
&missing, sizeof(missing)) != -ENODEV)
41+
return 1;
42+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
43+
&del_bind, sizeof(del_bind)))
44+
return 1;
45+
46+
return 0;
47+
}
48+
2249
SEC("cgroup/bind4")
2350
int bind_v4_prog(struct bpf_sock_addr *ctx)
2451
{
@@ -62,6 +89,10 @@ int bind_v4_prog(struct bpf_sock_addr *ctx)
6289
if (ctx->user_ip4 != user_ip4)
6390
return 0;
6491

92+
/* Bind to device and unbind it. */
93+
if (bind_to_device(ctx))
94+
return 0;
95+
6596
ctx->user_ip4 = bpf_htonl(SERV4_REWRITE_IP);
6697
ctx->user_port = bpf_htons(SERV4_REWRITE_PORT);
6798

tools/testing/selftests/bpf/progs/bind6_prog.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@
2525
#define SERV6_REWRITE_IP_3 0x00000001
2626
#define SERV6_REWRITE_PORT 6666
2727

28+
#ifndef IFNAMSIZ
29+
#define IFNAMSIZ 16
30+
#endif
31+
32+
static __inline int bind_to_device(struct bpf_sock_addr *ctx)
33+
{
34+
char veth1[IFNAMSIZ] = "test_sock_addr1";
35+
char veth2[IFNAMSIZ] = "test_sock_addr2";
36+
char missing[IFNAMSIZ] = "nonexistent_dev";
37+
char del_bind[IFNAMSIZ] = "";
38+
39+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
40+
&veth1, sizeof(veth1)))
41+
return 1;
42+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
43+
&veth2, sizeof(veth2)))
44+
return 1;
45+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
46+
&missing, sizeof(missing)) != -ENODEV)
47+
return 1;
48+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
49+
&del_bind, sizeof(del_bind)))
50+
return 1;
51+
52+
return 0;
53+
}
54+
2855
SEC("cgroup/bind6")
2956
int bind_v6_prog(struct bpf_sock_addr *ctx)
3057
{
@@ -76,6 +103,10 @@ int bind_v6_prog(struct bpf_sock_addr *ctx)
76103
return 0;
77104
}
78105

106+
/* Bind to device and unbind it. */
107+
if (bind_to_device(ctx))
108+
return 0;
109+
79110
ctx->user_ip6[0] = bpf_htonl(SERV6_REWRITE_IP_0);
80111
ctx->user_ip6[1] = bpf_htonl(SERV6_REWRITE_IP_1);
81112
ctx->user_ip6[2] = bpf_htonl(SERV6_REWRITE_IP_2);

0 commit comments

Comments
 (0)