Skip to content

Commit a0ac616

Browse files
fomichevkernel-patches-bot
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 <[email protected]>
1 parent dc1e7d5 commit a0ac616

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,8 +19,35 @@
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+
2226
int _version SEC("version") = 1;
2327

28+
static __inline int bind_to_device(struct bpf_sock_addr *ctx)
29+
{
30+
char veth1[IFNAMSIZ] = "test_sock_addr1";
31+
char veth2[IFNAMSIZ] = "test_sock_addr2";
32+
char missing[IFNAMSIZ] = "nonexistent_dev";
33+
char del_bind[IFNAMSIZ] = "";
34+
35+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
36+
&veth1, sizeof(veth1)))
37+
return 1;
38+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
39+
&veth2, sizeof(veth2)))
40+
return 1;
41+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
42+
&missing, sizeof(missing)) != -ENODEV)
43+
return 1;
44+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
45+
&del_bind, sizeof(del_bind)))
46+
return 1;
47+
48+
return 0;
49+
}
50+
2451
SEC("cgroup/bind4")
2552
int bind_v4_prog(struct bpf_sock_addr *ctx)
2653
{
@@ -64,6 +91,10 @@ int bind_v4_prog(struct bpf_sock_addr *ctx)
6491
if (ctx->user_ip4 != user_ip4)
6592
return 0;
6693

94+
/* Bind to device and unbind it. */
95+
if (bind_to_device(ctx))
96+
return 0;
97+
6798
ctx->user_ip4 = bpf_htonl(SERV4_REWRITE_IP);
6899
ctx->user_port = bpf_htons(SERV4_REWRITE_PORT);
69100

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

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

28+
#ifndef IFNAMSIZ
29+
#define IFNAMSIZ 16
30+
#endif
31+
2832
int _version SEC("version") = 1;
2933

34+
static __inline int bind_to_device(struct bpf_sock_addr *ctx)
35+
{
36+
char veth1[IFNAMSIZ] = "test_sock_addr1";
37+
char veth2[IFNAMSIZ] = "test_sock_addr2";
38+
char missing[IFNAMSIZ] = "nonexistent_dev";
39+
char del_bind[IFNAMSIZ] = "";
40+
41+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
42+
&veth1, sizeof(veth1)))
43+
return 1;
44+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
45+
&veth2, sizeof(veth2)))
46+
return 1;
47+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
48+
&missing, sizeof(missing)) != -ENODEV)
49+
return 1;
50+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
51+
&del_bind, sizeof(del_bind)))
52+
return 1;
53+
54+
return 0;
55+
}
56+
3057
SEC("cgroup/bind6")
3158
int bind_v6_prog(struct bpf_sock_addr *ctx)
3259
{
@@ -78,6 +105,10 @@ int bind_v6_prog(struct bpf_sock_addr *ctx)
78105
return 0;
79106
}
80107

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

0 commit comments

Comments
 (0)