1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_endian.h>
8 #include <bpf/bpf_tracing.h>
12 struct socket_cookie {
18 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
19 __uint(map_flags, BPF_F_NO_PREALLOC);
21 __type(value, struct socket_cookie);
22 } socket_cookies SEC(".maps");
25 * These three programs get executed in a row on connect() syscalls. The
26 * userspace side of the test creates a client socket, issues a connect() on it
27 * and then checks that the local storage associated with this socket has:
28 * cookie_value == local_port << 8 | 0xFF
29 * The different parts of this cookie_value are appended by those hooks if they
30 * all agree on the output of bpf_get_socket_cookie().
32 SEC("cgroup/connect6")
33 int set_cookie(struct bpf_sock_addr *ctx)
35 struct socket_cookie *p;
37 if (ctx->family != AF_INET6 || ctx->user_family != AF_INET6)
40 p = bpf_sk_storage_get(&socket_cookies, ctx->sk, 0,
41 BPF_SK_STORAGE_GET_F_CREATE);
45 p->cookie_value = 0xF;
46 p->cookie_key = bpf_get_socket_cookie(ctx);
52 int update_cookie_sockops(struct bpf_sock_ops *ctx)
54 struct bpf_sock *sk = ctx->sk;
55 struct socket_cookie *p;
57 if (ctx->family != AF_INET6)
60 if (ctx->op != BPF_SOCK_OPS_TCP_CONNECT_CB)
66 p = bpf_sk_storage_get(&socket_cookies, sk, 0, 0);
70 if (p->cookie_key != bpf_get_socket_cookie(ctx))
73 p->cookie_value |= (ctx->local_port << 8);
78 SEC("fexit/inet_stream_connect")
79 int BPF_PROG(update_cookie_tracing, struct socket *sock,
80 struct sockaddr *uaddr, int addr_len, int flags)
82 struct socket_cookie *p;
84 if (uaddr->sa_family != AF_INET6)
87 p = bpf_sk_storage_get(&socket_cookies, sock->sk, 0, 0);
91 if (p->cookie_key != bpf_get_socket_cookie(sock->sk))
94 p->cookie_value |= 0xF0;
99 char _license[] SEC("license") = "GPL";