mirror of
				git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
				synced 2025-09-04 20:19:47 +08:00 
			
		
		
		
	 194db0d958
			
		
	
	
		194db0d958
		
	
	
	
	
		
			
			Add test to use get_socket_cookie() from BPF programs of types BPF_PROG_TYPE_SOCK_OPS and BPF_PROG_TYPE_CGROUP_SOCK_ADDR. The test attaches two programs to cgroup, runs TCP server and client in the cgroup and checks that two operations are done properly on client socket when user calls connect(2): 1. In BPF_CGROUP_INET6_CONNECT socket cookie is used as the key to write new value in a map for client socket. 2. In BPF_CGROUP_SOCK_OPS (BPF_SOCK_OPS_TCP_CONNECT_CB callback) the value written in "1." is found by socket cookie, since it's the same socket, and updated. Finally the test verifies the value in the map. Signed-off-by: Andrey Ignatov <rdna@fb.com> Acked-by: Yonghong Song <yhs@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
		
			
				
	
	
		
			61 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| // SPDX-License-Identifier: GPL-2.0
 | |
| // Copyright (c) 2018 Facebook
 | |
| 
 | |
| #include <linux/bpf.h>
 | |
| #include <sys/socket.h>
 | |
| 
 | |
| #include "bpf_helpers.h"
 | |
| #include "bpf_endian.h"
 | |
| 
 | |
| struct bpf_map_def SEC("maps") socket_cookies = {
 | |
| 	.type = BPF_MAP_TYPE_HASH,
 | |
| 	.key_size = sizeof(__u64),
 | |
| 	.value_size = sizeof(__u32),
 | |
| 	.max_entries = 1 << 8,
 | |
| };
 | |
| 
 | |
| SEC("cgroup/connect6")
 | |
| int set_cookie(struct bpf_sock_addr *ctx)
 | |
| {
 | |
| 	__u32 cookie_value = 0xFF;
 | |
| 	__u64 cookie_key;
 | |
| 
 | |
| 	if (ctx->family != AF_INET6 || ctx->user_family != AF_INET6)
 | |
| 		return 1;
 | |
| 
 | |
| 	cookie_key = bpf_get_socket_cookie(ctx);
 | |
| 	if (bpf_map_update_elem(&socket_cookies, &cookie_key, &cookie_value, 0))
 | |
| 		return 0;
 | |
| 
 | |
| 	return 1;
 | |
| }
 | |
| 
 | |
| SEC("sockops")
 | |
| int update_cookie(struct bpf_sock_ops *ctx)
 | |
| {
 | |
| 	__u32 new_cookie_value;
 | |
| 	__u32 *cookie_value;
 | |
| 	__u64 cookie_key;
 | |
| 
 | |
| 	if (ctx->family != AF_INET6)
 | |
| 		return 1;
 | |
| 
 | |
| 	if (ctx->op != BPF_SOCK_OPS_TCP_CONNECT_CB)
 | |
| 		return 1;
 | |
| 
 | |
| 	cookie_key = bpf_get_socket_cookie(ctx);
 | |
| 
 | |
| 	cookie_value = bpf_map_lookup_elem(&socket_cookies, &cookie_key);
 | |
| 	if (!cookie_value)
 | |
| 		return 1;
 | |
| 
 | |
| 	new_cookie_value = (ctx->local_port << 8) | *cookie_value;
 | |
| 	bpf_map_update_elem(&socket_cookies, &cookie_key, &new_cookie_value, 0);
 | |
| 
 | |
| 	return 1;
 | |
| }
 | |
| 
 | |
| int _version SEC("version") = 1;
 | |
| 
 | |
| char _license[] SEC("license") = "GPL";
 |