xref: /openbmc/linux/tools/testing/selftests/bpf/progs/netns_cookie_prog.c (revision 374e74de96310cc63b9e3cde876e031107e6af6c)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "vmlinux.h"
4 
5 #include <bpf/bpf_helpers.h>
6 
7 #define AF_INET6 10
8 
9 struct {
10 	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
11 	__uint(map_flags, BPF_F_NO_PREALLOC);
12 	__type(key, int);
13 	__type(value, int);
14 } netns_cookies SEC(".maps");
15 
16 SEC("sockops")
17 int get_netns_cookie_sockops(struct bpf_sock_ops *ctx)
18 {
19 	struct bpf_sock *sk = ctx->sk;
20 	int *cookie;
21 
22 	if (ctx->family != AF_INET6)
23 		return 1;
24 
25 	if (ctx->op != BPF_SOCK_OPS_TCP_CONNECT_CB)
26 		return 1;
27 
28 	if (!sk)
29 		return 1;
30 
31 	cookie = bpf_sk_storage_get(&netns_cookies, sk, 0,
32 				BPF_SK_STORAGE_GET_F_CREATE);
33 	if (!cookie)
34 		return 1;
35 
36 	*cookie = bpf_get_netns_cookie(ctx);
37 
38 	return 1;
39 }
40