1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
3 // Copyright (c) 2019 Cloudflare
10 #include <arpa/inet.h>
11 #include <netinet/in.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
16 #include <bpf/libbpf.h>
18 #include "bpf_rlimit.h"
19 #include "cgroup_helpers.h"
21 static int start_server(const struct sockaddr
*addr
, socklen_t len
)
25 fd
= socket(addr
->sa_family
, SOCK_STREAM
, 0);
27 log_err("Failed to create server socket");
31 if (bind(fd
, addr
, len
) == -1) {
32 log_err("Failed to bind server socket");
36 if (listen(fd
, 128) == -1) {
37 log_err("Failed to listen on server socket");
50 static int connect_to_server(int server_fd
)
52 struct sockaddr_storage addr
;
53 socklen_t len
= sizeof(addr
);
56 if (getsockname(server_fd
, (struct sockaddr
*)&addr
, &len
)) {
57 log_err("Failed to get server addr");
61 fd
= socket(addr
.ss_family
, SOCK_STREAM
, 0);
63 log_err("Failed to create client socket");
67 if (connect(fd
, (const struct sockaddr
*)&addr
, len
) == -1) {
68 log_err("Fail to connect to server");
81 static int get_map_fd_by_prog_id(int prog_id
, bool *xdp
)
83 struct bpf_prog_info info
= {};
84 __u32 info_len
= sizeof(info
);
89 prog_fd
= bpf_prog_get_fd_by_id(prog_id
);
91 log_err("Failed to get fd by prog id %d", prog_id
);
96 info
.map_ids
= (__u64
)(unsigned long)map_ids
;
98 if (bpf_obj_get_info_by_fd(prog_fd
, &info
, &info_len
)) {
99 log_err("Failed to get info by prog fd %d", prog_fd
);
103 if (!info
.nr_map_ids
) {
104 log_err("No maps found for prog fd %d", prog_fd
);
108 *xdp
= info
.type
== BPF_PROG_TYPE_XDP
;
110 map_fd
= bpf_map_get_fd_by_id(map_ids
[0]);
112 log_err("Failed to get fd by map id %d", map_ids
[0]);
119 static int run_test(int server_fd
, int results_fd
, bool xdp
)
121 int client
= -1, srv_client
= -1;
130 if (bpf_map_update_elem(results_fd
, &key
, &value
, 0) < 0) {
131 log_err("Can't clear results");
135 if (bpf_map_update_elem(results_fd
, &key_gen
, &value_gen
, 0) < 0) {
136 log_err("Can't clear results");
140 if (bpf_map_update_elem(results_fd
, &key_mss
, &value_mss
, 0) < 0) {
141 log_err("Can't clear results");
145 client
= connect_to_server(server_fd
);
149 srv_client
= accept(server_fd
, NULL
, 0);
150 if (srv_client
== -1) {
151 log_err("Can't accept connection");
155 if (bpf_map_lookup_elem(results_fd
, &key
, &value
) < 0) {
156 log_err("Can't lookup result");
161 log_err("Didn't match syncookie: %u", value
);
165 if (bpf_map_lookup_elem(results_fd
, &key_gen
, &value_gen
) < 0) {
166 log_err("Can't lookup result");
170 if (xdp
&& value_gen
== 0) {
171 // SYN packets do not get passed through generic XDP, skip the
173 printf("Skipping XDP cookie check\n");
177 if (bpf_map_lookup_elem(results_fd
, &key_mss
, &value_mss
) < 0) {
178 log_err("Can't lookup result");
182 if (value
!= value_gen
) {
183 log_err("BPF generated cookie does not match kernel one");
187 if (value_mss
< 536 || value_mss
> USHRT_MAX
) {
188 log_err("Unexpected MSS retrieved");
202 int main(int argc
, char **argv
)
204 struct sockaddr_in addr4
;
205 struct sockaddr_in6 addr6
;
213 fprintf(stderr
, "Usage: %s prog_id\n", argv
[0]);
217 results
= get_map_fd_by_prog_id(atoi(argv
[1]), &xdp
);
219 log_err("Can't get map");
223 memset(&addr4
, 0, sizeof(addr4
));
224 addr4
.sin_family
= AF_INET
;
225 addr4
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
228 memset(&addr6
, 0, sizeof(addr6
));
229 addr6
.sin6_family
= AF_INET6
;
230 addr6
.sin6_addr
= in6addr_loopback
;
233 server
= start_server((const struct sockaddr
*)&addr4
, sizeof(addr4
));
237 server_v6
= start_server((const struct sockaddr
*)&addr6
,
242 if (run_test(server
, results
, xdp
))
245 if (run_test(server_v6
, results
, xdp
))