add a comment about bpf_get_socket_cookie()
Kernel implementation:
net/core/filter.c:
BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb) {
return skb->sk ? sock_gen_cookie(skb->sk) : 0;
}
net/core/sock_diag.c:
static atomic64_t cookie_gen;
u64 sock_gen_cookie(struct sock *sk) {
while (1) {
u64 res = atomic64_read(&sk->sk_cookie);
if (res) return res;
res = atomic64_inc_return(&cookie_gen);
atomic64_cmpxchg(&sk->sk_cookie, 0, res);
}
}
As we can see sock_gen_cookie cannot return 0.
Test: N/A, comment update only
Bug: 141532657
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I387e1b1570599c4e0c8b169316289ce39fe6a441
diff --git a/bpf_progs/bpf_net_helpers.h b/bpf_progs/bpf_net_helpers.h
index 239cf31..714b7e6 100644
--- a/bpf_progs/bpf_net_helpers.h
+++ b/bpf_progs/bpf_net_helpers.h
@@ -22,7 +22,9 @@
#include <stdbool.h>
#include <stdint.h>
+// 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 uint32_t (*bpf_get_socket_uid)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_uid;
static int (*bpf_skb_load_bytes)(struct __sk_buff* skb, int off, void* to,
int len) = (void*)BPF_FUNC_skb_load_bytes;