-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: add sockopt test that exercises BPF_F_ALLOW_MULTI
sockopt test that verifies chaining behavior. v9: * setsockopt chaining example v7: * rework the test to verify cgroup getsockopt chaining Cc: Andrii Nakryiko <andriin@fb.com> Cc: Martin Lau <kafai@fb.com> Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
- Loading branch information
Stanislav Fomichev
authored and
Alexei Starovoitov
committed
Jun 27, 2019
1 parent
8a027dc
commit 65b4414
Showing
4 changed files
with
449 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ test_btf_dump | |
xdping | ||
test_sockopt | ||
test_sockopt_sk | ||
test_sockopt_multi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#include <netinet/in.h> | ||
#include <linux/bpf.h> | ||
#include "bpf_helpers.h" | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
__u32 _version SEC("version") = 1; | ||
|
||
SEC("cgroup/getsockopt/child") | ||
int _getsockopt_child(struct bpf_sockopt *ctx) | ||
{ | ||
__u8 *optval_end = ctx->optval_end; | ||
__u8 *optval = ctx->optval; | ||
|
||
if (ctx->level != SOL_IP || ctx->optname != IP_TOS) | ||
return 1; | ||
|
||
if (optval + 1 > optval_end) | ||
return 0; /* EPERM, bounds check */ | ||
|
||
if (optval[0] != 0x80) | ||
return 0; /* EPERM, unexpected optval from the kernel */ | ||
|
||
ctx->retval = 0; /* Reset system call return value to zero */ | ||
|
||
optval[0] = 0x90; | ||
ctx->optlen = 1; | ||
|
||
return 1; | ||
} | ||
|
||
SEC("cgroup/getsockopt/parent") | ||
int _getsockopt_parent(struct bpf_sockopt *ctx) | ||
{ | ||
__u8 *optval_end = ctx->optval_end; | ||
__u8 *optval = ctx->optval; | ||
|
||
if (ctx->level != SOL_IP || ctx->optname != IP_TOS) | ||
return 1; | ||
|
||
if (optval + 1 > optval_end) | ||
return 0; /* EPERM, bounds check */ | ||
|
||
if (optval[0] != 0x90) | ||
return 0; /* EPERM, unexpected optval from the kernel */ | ||
|
||
ctx->retval = 0; /* Reset system call return value to zero */ | ||
|
||
optval[0] = 0xA0; | ||
ctx->optlen = 1; | ||
|
||
return 1; | ||
} | ||
|
||
SEC("cgroup/setsockopt") | ||
int _setsockopt(struct bpf_sockopt *ctx) | ||
{ | ||
__u8 *optval_end = ctx->optval_end; | ||
__u8 *optval = ctx->optval; | ||
|
||
if (ctx->level != SOL_IP || ctx->optname != IP_TOS) | ||
return 1; | ||
|
||
if (optval + 1 > optval_end) | ||
return 0; /* EPERM, bounds check */ | ||
|
||
optval[0] += 0x10; | ||
ctx->optlen = 1; | ||
|
||
return 1; | ||
} |
Oops, something went wrong.