1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
5 #include <sys/socket.h>
7 #include "bpf_helpers.h"
8 #include "bpf_endian.h"
10 struct bpf_map_def
SEC("maps") socket_cookies
= {
11 .type
= BPF_MAP_TYPE_HASH
,
12 .key_size
= sizeof(__u64
),
13 .value_size
= sizeof(__u32
),
14 .max_entries
= 1 << 8,
17 SEC("cgroup/connect6")
18 int set_cookie(struct bpf_sock_addr
*ctx
)
20 __u32 cookie_value
= 0xFF;
23 if (ctx
->family
!= AF_INET6
|| ctx
->user_family
!= AF_INET6
)
26 cookie_key
= bpf_get_socket_cookie(ctx
);
27 if (bpf_map_update_elem(&socket_cookies
, &cookie_key
, &cookie_value
, 0))
34 int update_cookie(struct bpf_sock_ops
*ctx
)
36 __u32 new_cookie_value
;
40 if (ctx
->family
!= AF_INET6
)
43 if (ctx
->op
!= BPF_SOCK_OPS_TCP_CONNECT_CB
)
46 cookie_key
= bpf_get_socket_cookie(ctx
);
48 cookie_value
= bpf_map_lookup_elem(&socket_cookies
, &cookie_key
);
52 new_cookie_value
= (ctx
->local_port
<< 8) | *cookie_value
;
53 bpf_map_update_elem(&socket_cookies
, &cookie_key
, &new_cookie_value
, 0);
58 int _version
SEC("version") = 1;
60 char _license
[] SEC("license") = "GPL";