bpf_net_helpers: introduce bpf_get_netns_cookie() helper
Linux Kernel documentation states:
* u64 bpf_get_netns_cookie(void *ctx)
Description
Retrieve the cookie (generated by the kernel) of the network
namespace the input *ctx* is associated with. The network
namespace cookie remains stable for its lifetime and provides
a global identifier that can be assumed unique. If *ctx* is
NULL, then the helper returns the cookie for the initial
network namespace. The cookie itself is very similar to that
of **bpf_get_socket_cookie**\ () helper, but for network
namespaces instead of sockets.
Return
A 8-byte long opaque number.
This was introduced in Linux 5.7, see get_netns_cookie in
https://elixir.bootlin.com/linux/v5.7/source/net/core/filter.c
vs
https://elixir.bootlin.com/linux/v5.6/source/net/core/filter.c
But only for sock & sock_addr, while sock_ops & sk_msg were
added later (present in 6.12)
Test: N/A
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Idccb7191552be8048a02fff2358e7d7898123dd9
diff --git a/bpf/progs/bpf_net_helpers.h b/bpf/progs/bpf_net_helpers.h
index a5664ba..4085ed4 100644
--- a/bpf/progs/bpf_net_helpers.h
+++ b/bpf/progs/bpf_net_helpers.h
@@ -84,6 +84,8 @@
#define ETH_IP6_TCP_OFFSET(field) (ETH_HLEN + IP6_TCP_OFFSET(field))
#define ETH_IP6_UDP_OFFSET(field) (ETH_HLEN + IP6_UDP_OFFSET(field))
+static uint64_t (*bpf_get_netns_cookie)(void* ctx) = (void*)BPF_FUNC_get_netns_cookie;
+
// this returns 0 iff skb->sk is NULL
static uint64_t (*bpf_get_socket_cookie)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_cookie;
static uint64_t (*bpf_get_sk_cookie)(struct bpf_sock* sk) = (void*)BPF_FUNC_get_socket_cookie;