summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/netns_cookie_prog.c
blob: aeff3a4f92875bf8667695cdce77c93101b156ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: GPL-2.0

#include "vmlinux.h"

#include <bpf/bpf_helpers.h>

#define AF_INET6 10

struct {
	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
	__uint(map_flags, BPF_F_NO_PREALLOC);
	__type(key, int);
	__type(value, int);
} sockops_netns_cookies SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
	__uint(map_flags, BPF_F_NO_PREALLOC);
	__type(key, int);
	__type(value, int);
} sk_msg_netns_cookies SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_SOCKMAP);
	__uint(max_entries, 2);
	__type(key, __u32);
	__type(value, __u64);
} sock_map SEC(".maps");

SEC("sockops")
int get_netns_cookie_sockops(struct bpf_sock_ops *ctx)
{
	struct bpf_sock *sk = ctx->sk;
	int *cookie;
	__u32 key = 0;

	if (ctx->family != AF_INET6)
		return 1;

	if (!sk)
		return 1;

	switch (ctx->op) {
	case BPF_SOCK_OPS_TCP_CONNECT_CB:
		cookie = bpf_sk_storage_get(&sockops_netns_cookies, sk, 0,
					    BPF_SK_STORAGE_GET_F_CREATE);
		if (!cookie)
			return 1;

		*cookie = bpf_get_netns_cookie(ctx);
		break;
	case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
		bpf_sock_map_update(ctx, &sock_map, &key, BPF_NOEXIST);
		break;
	default:
		break;
	}

	return 1;
}

SEC("sk_msg")
int get_netns_cookie_sk_msg(struct sk_msg_md *msg)
{
	struct bpf_sock *sk = msg->sk;
	int *cookie;

	if (msg->family != AF_INET6)
		return 1;

	if (!sk)
		return 1;

	cookie = bpf_sk_storage_get(&sk_msg_netns_cookies, sk, 0,
				    BPF_SK_STORAGE_GET_F_CREATE);
	if (!cookie)
		return 1;

	*cookie = bpf_get_netns_cookie(msg);

	return 1;
}

char _license[] SEC("license") = "GPL";