1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
8 #include <netinet/in.h>
10 #include <sys/socket.h>
13 #include <bpf/libbpf.h>
15 #include "bpf_rlimit.h"
16 #include "cgroup_helpers.h"
18 #define CG_PATH "/foo"
19 #define SOCKET_COOKIE_PROG "./socket_cookie_prog.o"
21 static int start_server(void)
23 struct sockaddr_in6 addr
;
26 fd
= socket(AF_INET6
, SOCK_STREAM
, 0);
28 log_err("Failed to create server socket");
32 memset(&addr
, 0, sizeof(addr
));
33 addr
.sin6_family
= AF_INET6
;
34 addr
.sin6_addr
= in6addr_loopback
;
37 if (bind(fd
, (const struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
38 log_err("Failed to bind server socket");
42 if (listen(fd
, 128) == -1) {
43 log_err("Failed to listen on server socket");
56 static int connect_to_server(int server_fd
)
58 struct sockaddr_storage addr
;
59 socklen_t len
= sizeof(addr
);
62 fd
= socket(AF_INET6
, SOCK_STREAM
, 0);
64 log_err("Failed to create client socket");
68 if (getsockname(server_fd
, (struct sockaddr
*)&addr
, &len
)) {
69 log_err("Failed to get server addr");
73 if (connect(fd
, (const struct sockaddr
*)&addr
, len
) == -1) {
74 log_err("Fail to connect to server");
87 static int validate_map(struct bpf_map
*map
, int client_fd
)
89 __u32 cookie_expected_value
;
90 struct sockaddr_in6 addr
;
91 socklen_t len
= sizeof(addr
);
98 log_err("Map not found in BPF object");
102 map_fd
= bpf_map__fd(map
);
104 err
= bpf_map_get_next_key(map_fd
, NULL
, &cookie_key
);
106 log_err("Can't get cookie key from map");
110 err
= bpf_map_lookup_elem(map_fd
, &cookie_key
, &cookie_value
);
112 log_err("Can't get cookie value from map");
116 err
= getsockname(client_fd
, (struct sockaddr
*)&addr
, &len
);
118 log_err("Can't get client local addr");
122 cookie_expected_value
= (ntohs(addr
.sin6_port
) << 8) | 0xFF;
123 if (cookie_value
!= cookie_expected_value
) {
124 log_err("Unexpected value in map: %x != %x", cookie_value
,
125 cookie_expected_value
);
136 static int run_test(int cgfd
)
138 enum bpf_attach_type attach_type
;
139 struct bpf_prog_load_attr attr
;
140 struct bpf_program
*prog
;
141 struct bpf_object
*pobj
;
142 const char *prog_name
;
148 memset(&attr
, 0, sizeof(attr
));
149 attr
.file
= SOCKET_COOKIE_PROG
;
150 attr
.prog_type
= BPF_PROG_TYPE_UNSPEC
;
152 err
= bpf_prog_load_xattr(&attr
, &pobj
, &prog_fd
);
154 log_err("Failed to load %s", attr
.file
);
158 bpf_object__for_each_program(prog
, pobj
) {
159 prog_name
= bpf_program__title(prog
, /*needs_copy*/ false);
161 if (strcmp(prog_name
, "cgroup/connect6") == 0) {
162 attach_type
= BPF_CGROUP_INET6_CONNECT
;
163 } else if (strcmp(prog_name
, "sockops") == 0) {
164 attach_type
= BPF_CGROUP_SOCK_OPS
;
166 log_err("Unexpected prog: %s", prog_name
);
170 err
= bpf_prog_attach(bpf_program__fd(prog
), cgfd
, attach_type
,
171 BPF_F_ALLOW_OVERRIDE
);
173 log_err("Failed to attach prog %s", prog_name
);
178 server_fd
= start_server();
182 client_fd
= connect_to_server(server_fd
);
186 if (validate_map(bpf_map__next(NULL
, pobj
), client_fd
))
195 bpf_object__close(pobj
);
196 printf("%s\n", err
? "FAILED" : "PASSED");
200 int main(int argc
, char **argv
)
205 if (setup_cgroup_environment())
208 cgfd
= create_and_get_cgroup(CG_PATH
);
212 if (join_cgroup(CG_PATH
))
223 cleanup_cgroup_environment();