mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2025-01-11 00:04:33 +08:00
3e689141e6
Fix all selftests to include libbpf header files with the bpf/ prefix, to
be consistent with external users of the library. Also ensure that all
includes of exported libbpf header files (those that are exported on 'make
install' of the library) use bracketed includes instead of quoted.
To not break the build, keep the old include path until everything has been
changed to the new one; a subsequent patch will remove that.
Fixes: 6910d7d386
("selftests/bpf: Ensure bpf_helper_defs.h are taken from selftests dir")
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/157952560568.1683545.9649335788846513446.stgit@toke.dk
133 lines
3.0 KiB
C
133 lines
3.0 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <string.h>
|
|
#include <netinet/in.h>
|
|
#include <netinet/tcp.h>
|
|
#include <linux/bpf.h>
|
|
#include <bpf/bpf_helpers.h>
|
|
|
|
char _license[] SEC("license") = "GPL";
|
|
__u32 _version SEC("version") = 1;
|
|
|
|
#define SOL_CUSTOM 0xdeadbeef
|
|
|
|
struct sockopt_sk {
|
|
__u8 val;
|
|
};
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
|
|
__uint(map_flags, BPF_F_NO_PREALLOC);
|
|
__type(key, int);
|
|
__type(value, struct sockopt_sk);
|
|
} socket_storage_map SEC(".maps");
|
|
|
|
SEC("cgroup/getsockopt")
|
|
int _getsockopt(struct bpf_sockopt *ctx)
|
|
{
|
|
__u8 *optval_end = ctx->optval_end;
|
|
__u8 *optval = ctx->optval;
|
|
struct sockopt_sk *storage;
|
|
|
|
if (ctx->level == SOL_IP && ctx->optname == IP_TOS)
|
|
/* Not interested in SOL_IP:IP_TOS;
|
|
* let next BPF program in the cgroup chain or kernel
|
|
* handle it.
|
|
*/
|
|
return 1;
|
|
|
|
if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) {
|
|
/* Not interested in SOL_SOCKET:SO_SNDBUF;
|
|
* let next BPF program in the cgroup chain or kernel
|
|
* handle it.
|
|
*/
|
|
return 1;
|
|
}
|
|
|
|
if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
|
|
/* Not interested in SOL_TCP:TCP_CONGESTION;
|
|
* let next BPF program in the cgroup chain or kernel
|
|
* handle it.
|
|
*/
|
|
return 1;
|
|
}
|
|
|
|
if (ctx->level != SOL_CUSTOM)
|
|
return 0; /* EPERM, deny everything except custom level */
|
|
|
|
if (optval + 1 > optval_end)
|
|
return 0; /* EPERM, bounds check */
|
|
|
|
storage = bpf_sk_storage_get(&socket_storage_map, ctx->sk, 0,
|
|
BPF_SK_STORAGE_GET_F_CREATE);
|
|
if (!storage)
|
|
return 0; /* EPERM, couldn't get sk storage */
|
|
|
|
if (!ctx->retval)
|
|
return 0; /* EPERM, kernel should not have handled
|
|
* SOL_CUSTOM, something is wrong!
|
|
*/
|
|
ctx->retval = 0; /* Reset system call return value to zero */
|
|
|
|
optval[0] = storage->val;
|
|
ctx->optlen = 1;
|
|
|
|
return 1;
|
|
}
|
|
|
|
SEC("cgroup/setsockopt")
|
|
int _setsockopt(struct bpf_sockopt *ctx)
|
|
{
|
|
__u8 *optval_end = ctx->optval_end;
|
|
__u8 *optval = ctx->optval;
|
|
struct sockopt_sk *storage;
|
|
|
|
if (ctx->level == SOL_IP && ctx->optname == IP_TOS)
|
|
/* Not interested in SOL_IP:IP_TOS;
|
|
* let next BPF program in the cgroup chain or kernel
|
|
* handle it.
|
|
*/
|
|
return 1;
|
|
|
|
if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) {
|
|
/* Overwrite SO_SNDBUF value */
|
|
|
|
if (optval + sizeof(__u32) > optval_end)
|
|
return 0; /* EPERM, bounds check */
|
|
|
|
*(__u32 *)optval = 0x55AA;
|
|
ctx->optlen = 4;
|
|
|
|
return 1;
|
|
}
|
|
|
|
if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
|
|
/* Always use cubic */
|
|
|
|
if (optval + 5 > optval_end)
|
|
return 0; /* EPERM, bounds check */
|
|
|
|
memcpy(optval, "cubic", 5);
|
|
ctx->optlen = 5;
|
|
|
|
return 1;
|
|
}
|
|
|
|
if (ctx->level != SOL_CUSTOM)
|
|
return 0; /* EPERM, deny everything except custom level */
|
|
|
|
if (optval + 1 > optval_end)
|
|
return 0; /* EPERM, bounds check */
|
|
|
|
storage = bpf_sk_storage_get(&socket_storage_map, ctx->sk, 0,
|
|
BPF_SK_STORAGE_GET_F_CREATE);
|
|
if (!storage)
|
|
return 0; /* EPERM, couldn't get sk storage */
|
|
|
|
storage->val = optval[0];
|
|
ctx->optlen = -1; /* BPF has consumed this option, don't call kernel
|
|
* setsockopt handler.
|
|
*/
|
|
|
|
return 1;
|
|
}
|