mirror of
				git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
				synced 2025-09-04 20:19:47 +08:00 
			
		
		
		
	 dd65acf72d
			
		
	
	
		dd65acf72d
		
	
	
	
	
		
			
			Since commit 6c4fc209fc ("bpf: remove useless version check for prog
load") these "version" sections, which result in bpf_attr.kern_version
being set, have been unnecessary.
Remove them so that it's obvious to folks using selftests as a guide that
"modern" BPF progs don't need this section.
Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211007231234.2223081-1-davemarchevsky@fb.com
		
	
			
		
			
				
	
	
		
			97 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			97 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| // SPDX-License-Identifier: GPL-2.0
 | |
| #include <linux/bpf.h>
 | |
| #include <bpf/bpf_helpers.h>
 | |
| 
 | |
| char _license[] SEC("license") = "GPL";
 | |
| 
 | |
| #define SOL_CUSTOM			0xdeadbeef
 | |
| #define CUSTOM_INHERIT1			0
 | |
| #define CUSTOM_INHERIT2			1
 | |
| #define CUSTOM_LISTENER			2
 | |
| 
 | |
| struct sockopt_inherit {
 | |
| 	__u8 val;
 | |
| };
 | |
| 
 | |
| struct {
 | |
| 	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
 | |
| 	__uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
 | |
| 	__type(key, int);
 | |
| 	__type(value, struct sockopt_inherit);
 | |
| } cloned1_map SEC(".maps");
 | |
| 
 | |
| struct {
 | |
| 	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
 | |
| 	__uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
 | |
| 	__type(key, int);
 | |
| 	__type(value, struct sockopt_inherit);
 | |
| } cloned2_map SEC(".maps");
 | |
| 
 | |
| struct {
 | |
| 	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
 | |
| 	__uint(map_flags, BPF_F_NO_PREALLOC);
 | |
| 	__type(key, int);
 | |
| 	__type(value, struct sockopt_inherit);
 | |
| } listener_only_map SEC(".maps");
 | |
| 
 | |
| static __inline struct sockopt_inherit *get_storage(struct bpf_sockopt *ctx)
 | |
| {
 | |
| 	if (ctx->optname == CUSTOM_INHERIT1)
 | |
| 		return bpf_sk_storage_get(&cloned1_map, ctx->sk, 0,
 | |
| 					  BPF_SK_STORAGE_GET_F_CREATE);
 | |
| 	else if (ctx->optname == CUSTOM_INHERIT2)
 | |
| 		return bpf_sk_storage_get(&cloned2_map, ctx->sk, 0,
 | |
| 					  BPF_SK_STORAGE_GET_F_CREATE);
 | |
| 	else
 | |
| 		return bpf_sk_storage_get(&listener_only_map, ctx->sk, 0,
 | |
| 					  BPF_SK_STORAGE_GET_F_CREATE);
 | |
| }
 | |
| 
 | |
| SEC("cgroup/getsockopt")
 | |
| int _getsockopt(struct bpf_sockopt *ctx)
 | |
| {
 | |
| 	__u8 *optval_end = ctx->optval_end;
 | |
| 	struct sockopt_inherit *storage;
 | |
| 	__u8 *optval = ctx->optval;
 | |
| 
 | |
| 	if (ctx->level != SOL_CUSTOM)
 | |
| 		return 1; /* only interested in SOL_CUSTOM */
 | |
| 
 | |
| 	if (optval + 1 > optval_end)
 | |
| 		return 0; /* EPERM, bounds check */
 | |
| 
 | |
| 	storage = get_storage(ctx);
 | |
| 	if (!storage)
 | |
| 		return 0; /* EPERM, couldn't get sk storage */
 | |
| 
 | |
| 	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;
 | |
| 	struct sockopt_inherit *storage;
 | |
| 	__u8 *optval = ctx->optval;
 | |
| 
 | |
| 	if (ctx->level != SOL_CUSTOM)
 | |
| 		return 1; /* only interested in SOL_CUSTOM */
 | |
| 
 | |
| 	if (optval + 1 > optval_end)
 | |
| 		return 0; /* EPERM, bounds check */
 | |
| 
 | |
| 	storage = get_storage(ctx);
 | |
| 	if (!storage)
 | |
| 		return 0; /* EPERM, couldn't get sk storage */
 | |
| 
 | |
| 	storage->val = optval[0];
 | |
| 	ctx->optlen = -1;
 | |
| 
 | |
| 	return 1;
 | |
| }
 |