WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / prog_tests / sk_lookup.c
blob9ff0412e1fd384fab4cd2c16de31a894cedb0e89
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 // Copyright (c) 2020 Cloudflare
3 /*
4 * Test BPF attach point for INET socket lookup (BPF_SK_LOOKUP).
6 * Tests exercise:
7 * - attaching/detaching/querying programs to BPF_SK_LOOKUP hook,
8 * - redirecting socket lookup to a socket selected by BPF program,
9 * - failing a socket lookup on BPF program's request,
10 * - error scenarios for selecting a socket from BPF program,
11 * - accessing BPF program context,
12 * - attaching and running multiple BPF programs.
14 * Tests run in a dedicated network namespace.
17 #define _GNU_SOURCE
18 #include <arpa/inet.h>
19 #include <assert.h>
20 #include <errno.h>
21 #include <error.h>
22 #include <fcntl.h>
23 #include <sched.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
29 #include <bpf/libbpf.h>
30 #include <bpf/bpf.h>
32 #include "test_progs.h"
33 #include "bpf_rlimit.h"
34 #include "bpf_util.h"
35 #include "cgroup_helpers.h"
36 #include "network_helpers.h"
37 #include "testing_helpers.h"
38 #include "test_sk_lookup.skel.h"
40 /* External (address, port) pairs the client sends packets to. */
41 #define EXT_IP4 "127.0.0.1"
42 #define EXT_IP6 "fd00::1"
43 #define EXT_PORT 7007
45 /* Internal (address, port) pairs the server listens/receives at. */
46 #define INT_IP4 "127.0.0.2"
47 #define INT_IP4_V6 "::ffff:127.0.0.2"
48 #define INT_IP6 "fd00::2"
49 #define INT_PORT 8008
51 #define IO_TIMEOUT_SEC 3
53 enum server {
54 SERVER_A = 0,
55 SERVER_B = 1,
56 MAX_SERVERS,
59 enum {
60 PROG1 = 0,
61 PROG2,
64 struct inet_addr {
65 const char *ip;
66 unsigned short port;
69 struct test {
70 const char *desc;
71 struct bpf_program *lookup_prog;
72 struct bpf_program *reuseport_prog;
73 struct bpf_map *sock_map;
74 int sotype;
75 struct inet_addr connect_to;
76 struct inet_addr listen_at;
77 enum server accept_on;
78 bool reuseport_has_conns; /* Add a connected socket to reuseport group */
81 static __u32 duration; /* for CHECK macro */
83 static bool is_ipv6(const char *ip)
85 return !!strchr(ip, ':');
88 static int attach_reuseport(int sock_fd, struct bpf_program *reuseport_prog)
90 int err, prog_fd;
92 prog_fd = bpf_program__fd(reuseport_prog);
93 if (prog_fd < 0) {
94 errno = -prog_fd;
95 return -1;
98 err = setsockopt(sock_fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF,
99 &prog_fd, sizeof(prog_fd));
100 if (err)
101 return -1;
103 return 0;
106 static socklen_t inetaddr_len(const struct sockaddr_storage *addr)
108 return (addr->ss_family == AF_INET ? sizeof(struct sockaddr_in) :
109 addr->ss_family == AF_INET6 ? sizeof(struct sockaddr_in6) : 0);
112 static int make_socket(int sotype, const char *ip, int port,
113 struct sockaddr_storage *addr)
115 struct timeval timeo = { .tv_sec = IO_TIMEOUT_SEC };
116 int err, family, fd;
118 family = is_ipv6(ip) ? AF_INET6 : AF_INET;
119 err = make_sockaddr(family, ip, port, addr, NULL);
120 if (CHECK(err, "make_address", "failed\n"))
121 return -1;
123 fd = socket(addr->ss_family, sotype, 0);
124 if (CHECK(fd < 0, "socket", "failed\n")) {
125 log_err("failed to make socket");
126 return -1;
129 err = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));
130 if (CHECK(err, "setsockopt(SO_SNDTIMEO)", "failed\n")) {
131 log_err("failed to set SNDTIMEO");
132 close(fd);
133 return -1;
136 err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
137 if (CHECK(err, "setsockopt(SO_RCVTIMEO)", "failed\n")) {
138 log_err("failed to set RCVTIMEO");
139 close(fd);
140 return -1;
143 return fd;
146 static int make_server(int sotype, const char *ip, int port,
147 struct bpf_program *reuseport_prog)
149 struct sockaddr_storage addr = {0};
150 const int one = 1;
151 int err, fd = -1;
153 fd = make_socket(sotype, ip, port, &addr);
154 if (fd < 0)
155 return -1;
157 /* Enabled for UDPv6 sockets for IPv4-mapped IPv6 to work. */
158 if (sotype == SOCK_DGRAM) {
159 err = setsockopt(fd, SOL_IP, IP_RECVORIGDSTADDR, &one,
160 sizeof(one));
161 if (CHECK(err, "setsockopt(IP_RECVORIGDSTADDR)", "failed\n")) {
162 log_err("failed to enable IP_RECVORIGDSTADDR");
163 goto fail;
167 if (sotype == SOCK_DGRAM && addr.ss_family == AF_INET6) {
168 err = setsockopt(fd, SOL_IPV6, IPV6_RECVORIGDSTADDR, &one,
169 sizeof(one));
170 if (CHECK(err, "setsockopt(IPV6_RECVORIGDSTADDR)", "failed\n")) {
171 log_err("failed to enable IPV6_RECVORIGDSTADDR");
172 goto fail;
176 if (sotype == SOCK_STREAM) {
177 err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one,
178 sizeof(one));
179 if (CHECK(err, "setsockopt(SO_REUSEADDR)", "failed\n")) {
180 log_err("failed to enable SO_REUSEADDR");
181 goto fail;
185 if (reuseport_prog) {
186 err = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one,
187 sizeof(one));
188 if (CHECK(err, "setsockopt(SO_REUSEPORT)", "failed\n")) {
189 log_err("failed to enable SO_REUSEPORT");
190 goto fail;
194 err = bind(fd, (void *)&addr, inetaddr_len(&addr));
195 if (CHECK(err, "bind", "failed\n")) {
196 log_err("failed to bind listen socket");
197 goto fail;
200 if (sotype == SOCK_STREAM) {
201 err = listen(fd, SOMAXCONN);
202 if (CHECK(err, "make_server", "listen")) {
203 log_err("failed to listen on port %d", port);
204 goto fail;
208 /* Late attach reuseport prog so we can have one init path */
209 if (reuseport_prog) {
210 err = attach_reuseport(fd, reuseport_prog);
211 if (CHECK(err, "attach_reuseport", "failed\n")) {
212 log_err("failed to attach reuseport prog");
213 goto fail;
217 return fd;
218 fail:
219 close(fd);
220 return -1;
223 static int make_client(int sotype, const char *ip, int port)
225 struct sockaddr_storage addr = {0};
226 int err, fd;
228 fd = make_socket(sotype, ip, port, &addr);
229 if (fd < 0)
230 return -1;
232 err = connect(fd, (void *)&addr, inetaddr_len(&addr));
233 if (CHECK(err, "make_client", "connect")) {
234 log_err("failed to connect client socket");
235 goto fail;
238 return fd;
239 fail:
240 close(fd);
241 return -1;
244 static int send_byte(int fd)
246 ssize_t n;
248 errno = 0;
249 n = send(fd, "a", 1, 0);
250 if (CHECK(n <= 0, "send_byte", "send")) {
251 log_err("failed/partial send");
252 return -1;
254 return 0;
257 static int recv_byte(int fd)
259 char buf[1];
260 ssize_t n;
262 n = recv(fd, buf, sizeof(buf), 0);
263 if (CHECK(n <= 0, "recv_byte", "recv")) {
264 log_err("failed/partial recv");
265 return -1;
267 return 0;
270 static int tcp_recv_send(int server_fd)
272 char buf[1];
273 int ret, fd;
274 ssize_t n;
276 fd = accept(server_fd, NULL, NULL);
277 if (CHECK(fd < 0, "accept", "failed\n")) {
278 log_err("failed to accept");
279 return -1;
282 n = recv(fd, buf, sizeof(buf), 0);
283 if (CHECK(n <= 0, "recv", "failed\n")) {
284 log_err("failed/partial recv");
285 ret = -1;
286 goto close;
289 n = send(fd, buf, n, 0);
290 if (CHECK(n <= 0, "send", "failed\n")) {
291 log_err("failed/partial send");
292 ret = -1;
293 goto close;
296 ret = 0;
297 close:
298 close(fd);
299 return ret;
302 static void v4_to_v6(struct sockaddr_storage *ss)
304 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)ss;
305 struct sockaddr_in v4 = *(struct sockaddr_in *)ss;
307 v6->sin6_family = AF_INET6;
308 v6->sin6_port = v4.sin_port;
309 v6->sin6_addr.s6_addr[10] = 0xff;
310 v6->sin6_addr.s6_addr[11] = 0xff;
311 memcpy(&v6->sin6_addr.s6_addr[12], &v4.sin_addr.s_addr, 4);
312 memset(&v6->sin6_addr.s6_addr[0], 0, 10);
315 static int udp_recv_send(int server_fd)
317 char cmsg_buf[CMSG_SPACE(sizeof(struct sockaddr_storage))];
318 struct sockaddr_storage _src_addr = { 0 };
319 struct sockaddr_storage *src_addr = &_src_addr;
320 struct sockaddr_storage *dst_addr = NULL;
321 struct msghdr msg = { 0 };
322 struct iovec iov = { 0 };
323 struct cmsghdr *cm;
324 char buf[1];
325 int ret, fd;
326 ssize_t n;
328 iov.iov_base = buf;
329 iov.iov_len = sizeof(buf);
331 msg.msg_name = src_addr;
332 msg.msg_namelen = sizeof(*src_addr);
333 msg.msg_iov = &iov;
334 msg.msg_iovlen = 1;
335 msg.msg_control = cmsg_buf;
336 msg.msg_controllen = sizeof(cmsg_buf);
338 errno = 0;
339 n = recvmsg(server_fd, &msg, 0);
340 if (CHECK(n <= 0, "recvmsg", "failed\n")) {
341 log_err("failed to receive");
342 return -1;
344 if (CHECK(msg.msg_flags & MSG_CTRUNC, "recvmsg", "truncated cmsg\n"))
345 return -1;
347 for (cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm)) {
348 if ((cm->cmsg_level == SOL_IP &&
349 cm->cmsg_type == IP_ORIGDSTADDR) ||
350 (cm->cmsg_level == SOL_IPV6 &&
351 cm->cmsg_type == IPV6_ORIGDSTADDR)) {
352 dst_addr = (struct sockaddr_storage *)CMSG_DATA(cm);
353 break;
355 log_err("warning: ignored cmsg at level %d type %d",
356 cm->cmsg_level, cm->cmsg_type);
358 if (CHECK(!dst_addr, "recvmsg", "missing ORIGDSTADDR\n"))
359 return -1;
361 /* Server socket bound to IPv4-mapped IPv6 address */
362 if (src_addr->ss_family == AF_INET6 &&
363 dst_addr->ss_family == AF_INET) {
364 v4_to_v6(dst_addr);
367 /* Reply from original destination address. */
368 fd = socket(dst_addr->ss_family, SOCK_DGRAM, 0);
369 if (CHECK(fd < 0, "socket", "failed\n")) {
370 log_err("failed to create tx socket");
371 return -1;
374 ret = bind(fd, (struct sockaddr *)dst_addr, sizeof(*dst_addr));
375 if (CHECK(ret, "bind", "failed\n")) {
376 log_err("failed to bind tx socket");
377 goto out;
380 msg.msg_control = NULL;
381 msg.msg_controllen = 0;
382 n = sendmsg(fd, &msg, 0);
383 if (CHECK(n <= 0, "sendmsg", "failed\n")) {
384 log_err("failed to send echo reply");
385 ret = -1;
386 goto out;
389 ret = 0;
390 out:
391 close(fd);
392 return ret;
395 static int tcp_echo_test(int client_fd, int server_fd)
397 int err;
399 err = send_byte(client_fd);
400 if (err)
401 return -1;
402 err = tcp_recv_send(server_fd);
403 if (err)
404 return -1;
405 err = recv_byte(client_fd);
406 if (err)
407 return -1;
409 return 0;
412 static int udp_echo_test(int client_fd, int server_fd)
414 int err;
416 err = send_byte(client_fd);
417 if (err)
418 return -1;
419 err = udp_recv_send(server_fd);
420 if (err)
421 return -1;
422 err = recv_byte(client_fd);
423 if (err)
424 return -1;
426 return 0;
429 static struct bpf_link *attach_lookup_prog(struct bpf_program *prog)
431 struct bpf_link *link;
432 int net_fd;
434 net_fd = open("/proc/self/ns/net", O_RDONLY);
435 if (CHECK(net_fd < 0, "open", "failed\n")) {
436 log_err("failed to open /proc/self/ns/net");
437 return NULL;
440 link = bpf_program__attach_netns(prog, net_fd);
441 if (CHECK(IS_ERR(link), "bpf_program__attach_netns", "failed\n")) {
442 errno = -PTR_ERR(link);
443 log_err("failed to attach program '%s' to netns",
444 bpf_program__name(prog));
445 link = NULL;
448 close(net_fd);
449 return link;
452 static int update_lookup_map(struct bpf_map *map, int index, int sock_fd)
454 int err, map_fd;
455 uint64_t value;
457 map_fd = bpf_map__fd(map);
458 if (CHECK(map_fd < 0, "bpf_map__fd", "failed\n")) {
459 errno = -map_fd;
460 log_err("failed to get map FD");
461 return -1;
464 value = (uint64_t)sock_fd;
465 err = bpf_map_update_elem(map_fd, &index, &value, BPF_NOEXIST);
466 if (CHECK(err, "bpf_map_update_elem", "failed\n")) {
467 log_err("failed to update redir_map @ %d", index);
468 return -1;
471 return 0;
474 static void query_lookup_prog(struct test_sk_lookup *skel)
476 struct bpf_link *link[3] = {};
477 struct bpf_link_info info;
478 __u32 attach_flags = 0;
479 __u32 prog_ids[3] = {};
480 __u32 prog_cnt = 3;
481 __u32 prog_id;
482 int net_fd;
483 int err;
485 net_fd = open("/proc/self/ns/net", O_RDONLY);
486 if (CHECK(net_fd < 0, "open", "failed\n")) {
487 log_err("failed to open /proc/self/ns/net");
488 return;
491 link[0] = attach_lookup_prog(skel->progs.lookup_pass);
492 if (!link[0])
493 goto close;
494 link[1] = attach_lookup_prog(skel->progs.lookup_pass);
495 if (!link[1])
496 goto detach;
497 link[2] = attach_lookup_prog(skel->progs.lookup_drop);
498 if (!link[2])
499 goto detach;
501 err = bpf_prog_query(net_fd, BPF_SK_LOOKUP, 0 /* query flags */,
502 &attach_flags, prog_ids, &prog_cnt);
503 if (CHECK(err, "bpf_prog_query", "failed\n")) {
504 log_err("failed to query lookup prog");
505 goto detach;
508 errno = 0;
509 if (CHECK(attach_flags != 0, "bpf_prog_query",
510 "wrong attach_flags on query: %u", attach_flags))
511 goto detach;
512 if (CHECK(prog_cnt != 3, "bpf_prog_query",
513 "wrong program count on query: %u", prog_cnt))
514 goto detach;
515 prog_id = link_info_prog_id(link[0], &info);
516 CHECK(prog_ids[0] != prog_id, "bpf_prog_query",
517 "invalid program #0 id on query: %u != %u\n",
518 prog_ids[0], prog_id);
519 CHECK(info.netns.netns_ino == 0, "netns_ino",
520 "unexpected netns_ino: %u\n", info.netns.netns_ino);
521 prog_id = link_info_prog_id(link[1], &info);
522 CHECK(prog_ids[1] != prog_id, "bpf_prog_query",
523 "invalid program #1 id on query: %u != %u\n",
524 prog_ids[1], prog_id);
525 CHECK(info.netns.netns_ino == 0, "netns_ino",
526 "unexpected netns_ino: %u\n", info.netns.netns_ino);
527 prog_id = link_info_prog_id(link[2], &info);
528 CHECK(prog_ids[2] != prog_id, "bpf_prog_query",
529 "invalid program #2 id on query: %u != %u\n",
530 prog_ids[2], prog_id);
531 CHECK(info.netns.netns_ino == 0, "netns_ino",
532 "unexpected netns_ino: %u\n", info.netns.netns_ino);
534 err = bpf_link__detach(link[0]);
535 if (CHECK(err, "link_detach", "failed %d\n", err))
536 goto detach;
538 /* prog id is still there, but netns_ino is zeroed out */
539 prog_id = link_info_prog_id(link[0], &info);
540 CHECK(prog_ids[0] != prog_id, "bpf_prog_query",
541 "invalid program #0 id on query: %u != %u\n",
542 prog_ids[0], prog_id);
543 CHECK(info.netns.netns_ino != 0, "netns_ino",
544 "unexpected netns_ino: %u\n", info.netns.netns_ino);
546 detach:
547 if (link[2])
548 bpf_link__destroy(link[2]);
549 if (link[1])
550 bpf_link__destroy(link[1]);
551 if (link[0])
552 bpf_link__destroy(link[0]);
553 close:
554 close(net_fd);
557 static void run_lookup_prog(const struct test *t)
559 int server_fds[MAX_SERVERS] = { -1 };
560 int client_fd, reuse_conn_fd = -1;
561 struct bpf_link *lookup_link;
562 int i, err;
564 lookup_link = attach_lookup_prog(t->lookup_prog);
565 if (!lookup_link)
566 return;
568 for (i = 0; i < ARRAY_SIZE(server_fds); i++) {
569 server_fds[i] = make_server(t->sotype, t->listen_at.ip,
570 t->listen_at.port,
571 t->reuseport_prog);
572 if (server_fds[i] < 0)
573 goto close;
575 err = update_lookup_map(t->sock_map, i, server_fds[i]);
576 if (err)
577 goto close;
579 /* want just one server for non-reuseport test */
580 if (!t->reuseport_prog)
581 break;
584 /* Regular UDP socket lookup with reuseport behaves
585 * differently when reuseport group contains connected
586 * sockets. Check that adding a connected UDP socket to the
587 * reuseport group does not affect how reuseport works with
588 * BPF socket lookup.
590 if (t->reuseport_has_conns) {
591 struct sockaddr_storage addr = {};
592 socklen_t len = sizeof(addr);
594 /* Add an extra socket to reuseport group */
595 reuse_conn_fd = make_server(t->sotype, t->listen_at.ip,
596 t->listen_at.port,
597 t->reuseport_prog);
598 if (reuse_conn_fd < 0)
599 goto close;
601 /* Connect the extra socket to itself */
602 err = getsockname(reuse_conn_fd, (void *)&addr, &len);
603 if (CHECK(err, "getsockname", "errno %d\n", errno))
604 goto close;
605 err = connect(reuse_conn_fd, (void *)&addr, len);
606 if (CHECK(err, "connect", "errno %d\n", errno))
607 goto close;
610 client_fd = make_client(t->sotype, t->connect_to.ip, t->connect_to.port);
611 if (client_fd < 0)
612 goto close;
614 if (t->sotype == SOCK_STREAM)
615 tcp_echo_test(client_fd, server_fds[t->accept_on]);
616 else
617 udp_echo_test(client_fd, server_fds[t->accept_on]);
619 close(client_fd);
620 close:
621 if (reuse_conn_fd != -1)
622 close(reuse_conn_fd);
623 for (i = 0; i < ARRAY_SIZE(server_fds); i++) {
624 if (server_fds[i] != -1)
625 close(server_fds[i]);
627 bpf_link__destroy(lookup_link);
630 static void test_redirect_lookup(struct test_sk_lookup *skel)
632 const struct test tests[] = {
634 .desc = "TCP IPv4 redir port",
635 .lookup_prog = skel->progs.redir_port,
636 .sock_map = skel->maps.redir_map,
637 .sotype = SOCK_STREAM,
638 .connect_to = { EXT_IP4, EXT_PORT },
639 .listen_at = { EXT_IP4, INT_PORT },
642 .desc = "TCP IPv4 redir addr",
643 .lookup_prog = skel->progs.redir_ip4,
644 .sock_map = skel->maps.redir_map,
645 .sotype = SOCK_STREAM,
646 .connect_to = { EXT_IP4, EXT_PORT },
647 .listen_at = { INT_IP4, EXT_PORT },
650 .desc = "TCP IPv4 redir with reuseport",
651 .lookup_prog = skel->progs.select_sock_a,
652 .reuseport_prog = skel->progs.select_sock_b,
653 .sock_map = skel->maps.redir_map,
654 .sotype = SOCK_STREAM,
655 .connect_to = { EXT_IP4, EXT_PORT },
656 .listen_at = { INT_IP4, INT_PORT },
657 .accept_on = SERVER_B,
660 .desc = "TCP IPv4 redir skip reuseport",
661 .lookup_prog = skel->progs.select_sock_a_no_reuseport,
662 .reuseport_prog = skel->progs.select_sock_b,
663 .sock_map = skel->maps.redir_map,
664 .sotype = SOCK_STREAM,
665 .connect_to = { EXT_IP4, EXT_PORT },
666 .listen_at = { INT_IP4, INT_PORT },
667 .accept_on = SERVER_A,
670 .desc = "TCP IPv6 redir port",
671 .lookup_prog = skel->progs.redir_port,
672 .sock_map = skel->maps.redir_map,
673 .sotype = SOCK_STREAM,
674 .connect_to = { EXT_IP6, EXT_PORT },
675 .listen_at = { EXT_IP6, INT_PORT },
678 .desc = "TCP IPv6 redir addr",
679 .lookup_prog = skel->progs.redir_ip6,
680 .sock_map = skel->maps.redir_map,
681 .sotype = SOCK_STREAM,
682 .connect_to = { EXT_IP6, EXT_PORT },
683 .listen_at = { INT_IP6, EXT_PORT },
686 .desc = "TCP IPv4->IPv6 redir port",
687 .lookup_prog = skel->progs.redir_port,
688 .sock_map = skel->maps.redir_map,
689 .sotype = SOCK_STREAM,
690 .connect_to = { EXT_IP4, EXT_PORT },
691 .listen_at = { INT_IP4_V6, INT_PORT },
694 .desc = "TCP IPv6 redir with reuseport",
695 .lookup_prog = skel->progs.select_sock_a,
696 .reuseport_prog = skel->progs.select_sock_b,
697 .sock_map = skel->maps.redir_map,
698 .sotype = SOCK_STREAM,
699 .connect_to = { EXT_IP6, EXT_PORT },
700 .listen_at = { INT_IP6, INT_PORT },
701 .accept_on = SERVER_B,
704 .desc = "TCP IPv6 redir skip reuseport",
705 .lookup_prog = skel->progs.select_sock_a_no_reuseport,
706 .reuseport_prog = skel->progs.select_sock_b,
707 .sock_map = skel->maps.redir_map,
708 .sotype = SOCK_STREAM,
709 .connect_to = { EXT_IP6, EXT_PORT },
710 .listen_at = { INT_IP6, INT_PORT },
711 .accept_on = SERVER_A,
714 .desc = "UDP IPv4 redir port",
715 .lookup_prog = skel->progs.redir_port,
716 .sock_map = skel->maps.redir_map,
717 .sotype = SOCK_DGRAM,
718 .connect_to = { EXT_IP4, EXT_PORT },
719 .listen_at = { EXT_IP4, INT_PORT },
722 .desc = "UDP IPv4 redir addr",
723 .lookup_prog = skel->progs.redir_ip4,
724 .sock_map = skel->maps.redir_map,
725 .sotype = SOCK_DGRAM,
726 .connect_to = { EXT_IP4, EXT_PORT },
727 .listen_at = { INT_IP4, EXT_PORT },
730 .desc = "UDP IPv4 redir with reuseport",
731 .lookup_prog = skel->progs.select_sock_a,
732 .reuseport_prog = skel->progs.select_sock_b,
733 .sock_map = skel->maps.redir_map,
734 .sotype = SOCK_DGRAM,
735 .connect_to = { EXT_IP4, EXT_PORT },
736 .listen_at = { INT_IP4, INT_PORT },
737 .accept_on = SERVER_B,
740 .desc = "UDP IPv4 redir and reuseport with conns",
741 .lookup_prog = skel->progs.select_sock_a,
742 .reuseport_prog = skel->progs.select_sock_b,
743 .sock_map = skel->maps.redir_map,
744 .sotype = SOCK_DGRAM,
745 .connect_to = { EXT_IP4, EXT_PORT },
746 .listen_at = { INT_IP4, INT_PORT },
747 .accept_on = SERVER_B,
748 .reuseport_has_conns = true,
751 .desc = "UDP IPv4 redir skip reuseport",
752 .lookup_prog = skel->progs.select_sock_a_no_reuseport,
753 .reuseport_prog = skel->progs.select_sock_b,
754 .sock_map = skel->maps.redir_map,
755 .sotype = SOCK_DGRAM,
756 .connect_to = { EXT_IP4, EXT_PORT },
757 .listen_at = { INT_IP4, INT_PORT },
758 .accept_on = SERVER_A,
761 .desc = "UDP IPv6 redir port",
762 .lookup_prog = skel->progs.redir_port,
763 .sock_map = skel->maps.redir_map,
764 .sotype = SOCK_DGRAM,
765 .connect_to = { EXT_IP6, EXT_PORT },
766 .listen_at = { EXT_IP6, INT_PORT },
769 .desc = "UDP IPv6 redir addr",
770 .lookup_prog = skel->progs.redir_ip6,
771 .sock_map = skel->maps.redir_map,
772 .sotype = SOCK_DGRAM,
773 .connect_to = { EXT_IP6, EXT_PORT },
774 .listen_at = { INT_IP6, EXT_PORT },
777 .desc = "UDP IPv4->IPv6 redir port",
778 .lookup_prog = skel->progs.redir_port,
779 .sock_map = skel->maps.redir_map,
780 .sotype = SOCK_DGRAM,
781 .listen_at = { INT_IP4_V6, INT_PORT },
782 .connect_to = { EXT_IP4, EXT_PORT },
785 .desc = "UDP IPv6 redir and reuseport",
786 .lookup_prog = skel->progs.select_sock_a,
787 .reuseport_prog = skel->progs.select_sock_b,
788 .sock_map = skel->maps.redir_map,
789 .sotype = SOCK_DGRAM,
790 .connect_to = { EXT_IP6, EXT_PORT },
791 .listen_at = { INT_IP6, INT_PORT },
792 .accept_on = SERVER_B,
795 .desc = "UDP IPv6 redir and reuseport with conns",
796 .lookup_prog = skel->progs.select_sock_a,
797 .reuseport_prog = skel->progs.select_sock_b,
798 .sock_map = skel->maps.redir_map,
799 .sotype = SOCK_DGRAM,
800 .connect_to = { EXT_IP6, EXT_PORT },
801 .listen_at = { INT_IP6, INT_PORT },
802 .accept_on = SERVER_B,
803 .reuseport_has_conns = true,
806 .desc = "UDP IPv6 redir skip reuseport",
807 .lookup_prog = skel->progs.select_sock_a_no_reuseport,
808 .reuseport_prog = skel->progs.select_sock_b,
809 .sock_map = skel->maps.redir_map,
810 .sotype = SOCK_DGRAM,
811 .connect_to = { EXT_IP6, EXT_PORT },
812 .listen_at = { INT_IP6, INT_PORT },
813 .accept_on = SERVER_A,
816 const struct test *t;
818 for (t = tests; t < tests + ARRAY_SIZE(tests); t++) {
819 if (test__start_subtest(t->desc))
820 run_lookup_prog(t);
824 static void drop_on_lookup(const struct test *t)
826 struct sockaddr_storage dst = {};
827 int client_fd, server_fd, err;
828 struct bpf_link *lookup_link;
829 ssize_t n;
831 lookup_link = attach_lookup_prog(t->lookup_prog);
832 if (!lookup_link)
833 return;
835 server_fd = make_server(t->sotype, t->listen_at.ip, t->listen_at.port,
836 t->reuseport_prog);
837 if (server_fd < 0)
838 goto detach;
840 client_fd = make_socket(t->sotype, t->connect_to.ip,
841 t->connect_to.port, &dst);
842 if (client_fd < 0)
843 goto close_srv;
845 err = connect(client_fd, (void *)&dst, inetaddr_len(&dst));
846 if (t->sotype == SOCK_DGRAM) {
847 err = send_byte(client_fd);
848 if (err)
849 goto close_all;
851 /* Read out asynchronous error */
852 n = recv(client_fd, NULL, 0, 0);
853 err = n == -1;
855 if (CHECK(!err || errno != ECONNREFUSED, "connect",
856 "unexpected success or error\n"))
857 log_err("expected ECONNREFUSED on connect");
859 close_all:
860 close(client_fd);
861 close_srv:
862 close(server_fd);
863 detach:
864 bpf_link__destroy(lookup_link);
867 static void test_drop_on_lookup(struct test_sk_lookup *skel)
869 const struct test tests[] = {
871 .desc = "TCP IPv4 drop on lookup",
872 .lookup_prog = skel->progs.lookup_drop,
873 .sotype = SOCK_STREAM,
874 .connect_to = { EXT_IP4, EXT_PORT },
875 .listen_at = { EXT_IP4, EXT_PORT },
878 .desc = "TCP IPv6 drop on lookup",
879 .lookup_prog = skel->progs.lookup_drop,
880 .sotype = SOCK_STREAM,
881 .connect_to = { EXT_IP6, EXT_PORT },
882 .listen_at = { EXT_IP6, EXT_PORT },
885 .desc = "UDP IPv4 drop on lookup",
886 .lookup_prog = skel->progs.lookup_drop,
887 .sotype = SOCK_DGRAM,
888 .connect_to = { EXT_IP4, EXT_PORT },
889 .listen_at = { EXT_IP4, EXT_PORT },
892 .desc = "UDP IPv6 drop on lookup",
893 .lookup_prog = skel->progs.lookup_drop,
894 .sotype = SOCK_DGRAM,
895 .connect_to = { EXT_IP6, EXT_PORT },
896 .listen_at = { EXT_IP6, INT_PORT },
899 const struct test *t;
901 for (t = tests; t < tests + ARRAY_SIZE(tests); t++) {
902 if (test__start_subtest(t->desc))
903 drop_on_lookup(t);
907 static void drop_on_reuseport(const struct test *t)
909 struct sockaddr_storage dst = { 0 };
910 int client, server1, server2, err;
911 struct bpf_link *lookup_link;
912 ssize_t n;
914 lookup_link = attach_lookup_prog(t->lookup_prog);
915 if (!lookup_link)
916 return;
918 server1 = make_server(t->sotype, t->listen_at.ip, t->listen_at.port,
919 t->reuseport_prog);
920 if (server1 < 0)
921 goto detach;
923 err = update_lookup_map(t->sock_map, SERVER_A, server1);
924 if (err)
925 goto detach;
927 /* second server on destination address we should never reach */
928 server2 = make_server(t->sotype, t->connect_to.ip, t->connect_to.port,
929 NULL /* reuseport prog */);
930 if (server2 < 0)
931 goto close_srv1;
933 client = make_socket(t->sotype, t->connect_to.ip,
934 t->connect_to.port, &dst);
935 if (client < 0)
936 goto close_srv2;
938 err = connect(client, (void *)&dst, inetaddr_len(&dst));
939 if (t->sotype == SOCK_DGRAM) {
940 err = send_byte(client);
941 if (err)
942 goto close_all;
944 /* Read out asynchronous error */
945 n = recv(client, NULL, 0, 0);
946 err = n == -1;
948 if (CHECK(!err || errno != ECONNREFUSED, "connect",
949 "unexpected success or error\n"))
950 log_err("expected ECONNREFUSED on connect");
952 close_all:
953 close(client);
954 close_srv2:
955 close(server2);
956 close_srv1:
957 close(server1);
958 detach:
959 bpf_link__destroy(lookup_link);
962 static void test_drop_on_reuseport(struct test_sk_lookup *skel)
964 const struct test tests[] = {
966 .desc = "TCP IPv4 drop on reuseport",
967 .lookup_prog = skel->progs.select_sock_a,
968 .reuseport_prog = skel->progs.reuseport_drop,
969 .sock_map = skel->maps.redir_map,
970 .sotype = SOCK_STREAM,
971 .connect_to = { EXT_IP4, EXT_PORT },
972 .listen_at = { INT_IP4, INT_PORT },
975 .desc = "TCP IPv6 drop on reuseport",
976 .lookup_prog = skel->progs.select_sock_a,
977 .reuseport_prog = skel->progs.reuseport_drop,
978 .sock_map = skel->maps.redir_map,
979 .sotype = SOCK_STREAM,
980 .connect_to = { EXT_IP6, EXT_PORT },
981 .listen_at = { INT_IP6, INT_PORT },
984 .desc = "UDP IPv4 drop on reuseport",
985 .lookup_prog = skel->progs.select_sock_a,
986 .reuseport_prog = skel->progs.reuseport_drop,
987 .sock_map = skel->maps.redir_map,
988 .sotype = SOCK_DGRAM,
989 .connect_to = { EXT_IP4, EXT_PORT },
990 .listen_at = { INT_IP4, INT_PORT },
993 .desc = "TCP IPv6 drop on reuseport",
994 .lookup_prog = skel->progs.select_sock_a,
995 .reuseport_prog = skel->progs.reuseport_drop,
996 .sock_map = skel->maps.redir_map,
997 .sotype = SOCK_STREAM,
998 .connect_to = { EXT_IP6, EXT_PORT },
999 .listen_at = { INT_IP6, INT_PORT },
1002 const struct test *t;
1004 for (t = tests; t < tests + ARRAY_SIZE(tests); t++) {
1005 if (test__start_subtest(t->desc))
1006 drop_on_reuseport(t);
1010 static void run_sk_assign(struct test_sk_lookup *skel,
1011 struct bpf_program *lookup_prog,
1012 const char *listen_ip, const char *connect_ip)
1014 int client_fd, peer_fd, server_fds[MAX_SERVERS] = { -1 };
1015 struct bpf_link *lookup_link;
1016 int i, err;
1018 lookup_link = attach_lookup_prog(lookup_prog);
1019 if (!lookup_link)
1020 return;
1022 for (i = 0; i < ARRAY_SIZE(server_fds); i++) {
1023 server_fds[i] = make_server(SOCK_STREAM, listen_ip, 0, NULL);
1024 if (server_fds[i] < 0)
1025 goto close_servers;
1027 err = update_lookup_map(skel->maps.redir_map, i,
1028 server_fds[i]);
1029 if (err)
1030 goto close_servers;
1033 client_fd = make_client(SOCK_STREAM, connect_ip, EXT_PORT);
1034 if (client_fd < 0)
1035 goto close_servers;
1037 peer_fd = accept(server_fds[SERVER_B], NULL, NULL);
1038 if (CHECK(peer_fd < 0, "accept", "failed\n"))
1039 goto close_client;
1041 close(peer_fd);
1042 close_client:
1043 close(client_fd);
1044 close_servers:
1045 for (i = 0; i < ARRAY_SIZE(server_fds); i++) {
1046 if (server_fds[i] != -1)
1047 close(server_fds[i]);
1049 bpf_link__destroy(lookup_link);
1052 static void run_sk_assign_v4(struct test_sk_lookup *skel,
1053 struct bpf_program *lookup_prog)
1055 run_sk_assign(skel, lookup_prog, INT_IP4, EXT_IP4);
1058 static void run_sk_assign_v6(struct test_sk_lookup *skel,
1059 struct bpf_program *lookup_prog)
1061 run_sk_assign(skel, lookup_prog, INT_IP6, EXT_IP6);
1064 static void run_sk_assign_connected(struct test_sk_lookup *skel,
1065 int sotype)
1067 int err, client_fd, connected_fd, server_fd;
1068 struct bpf_link *lookup_link;
1070 server_fd = make_server(sotype, EXT_IP4, EXT_PORT, NULL);
1071 if (server_fd < 0)
1072 return;
1074 connected_fd = make_client(sotype, EXT_IP4, EXT_PORT);
1075 if (connected_fd < 0)
1076 goto out_close_server;
1078 /* Put a connected socket in redirect map */
1079 err = update_lookup_map(skel->maps.redir_map, SERVER_A, connected_fd);
1080 if (err)
1081 goto out_close_connected;
1083 lookup_link = attach_lookup_prog(skel->progs.sk_assign_esocknosupport);
1084 if (!lookup_link)
1085 goto out_close_connected;
1087 /* Try to redirect TCP SYN / UDP packet to a connected socket */
1088 client_fd = make_client(sotype, EXT_IP4, EXT_PORT);
1089 if (client_fd < 0)
1090 goto out_unlink_prog;
1091 if (sotype == SOCK_DGRAM) {
1092 send_byte(client_fd);
1093 recv_byte(server_fd);
1096 close(client_fd);
1097 out_unlink_prog:
1098 bpf_link__destroy(lookup_link);
1099 out_close_connected:
1100 close(connected_fd);
1101 out_close_server:
1102 close(server_fd);
1105 static void test_sk_assign_helper(struct test_sk_lookup *skel)
1107 if (test__start_subtest("sk_assign returns EEXIST"))
1108 run_sk_assign_v4(skel, skel->progs.sk_assign_eexist);
1109 if (test__start_subtest("sk_assign honors F_REPLACE"))
1110 run_sk_assign_v4(skel, skel->progs.sk_assign_replace_flag);
1111 if (test__start_subtest("sk_assign accepts NULL socket"))
1112 run_sk_assign_v4(skel, skel->progs.sk_assign_null);
1113 if (test__start_subtest("access ctx->sk"))
1114 run_sk_assign_v4(skel, skel->progs.access_ctx_sk);
1115 if (test__start_subtest("narrow access to ctx v4"))
1116 run_sk_assign_v4(skel, skel->progs.ctx_narrow_access);
1117 if (test__start_subtest("narrow access to ctx v6"))
1118 run_sk_assign_v6(skel, skel->progs.ctx_narrow_access);
1119 if (test__start_subtest("sk_assign rejects TCP established"))
1120 run_sk_assign_connected(skel, SOCK_STREAM);
1121 if (test__start_subtest("sk_assign rejects UDP connected"))
1122 run_sk_assign_connected(skel, SOCK_DGRAM);
1125 struct test_multi_prog {
1126 const char *desc;
1127 struct bpf_program *prog1;
1128 struct bpf_program *prog2;
1129 struct bpf_map *redir_map;
1130 struct bpf_map *run_map;
1131 int expect_errno;
1132 struct inet_addr listen_at;
1135 static void run_multi_prog_lookup(const struct test_multi_prog *t)
1137 struct sockaddr_storage dst = {};
1138 int map_fd, server_fd, client_fd;
1139 struct bpf_link *link1, *link2;
1140 int prog_idx, done, err;
1142 map_fd = bpf_map__fd(t->run_map);
1144 done = 0;
1145 prog_idx = PROG1;
1146 err = bpf_map_update_elem(map_fd, &prog_idx, &done, BPF_ANY);
1147 if (CHECK(err, "bpf_map_update_elem", "failed\n"))
1148 return;
1149 prog_idx = PROG2;
1150 err = bpf_map_update_elem(map_fd, &prog_idx, &done, BPF_ANY);
1151 if (CHECK(err, "bpf_map_update_elem", "failed\n"))
1152 return;
1154 link1 = attach_lookup_prog(t->prog1);
1155 if (!link1)
1156 return;
1157 link2 = attach_lookup_prog(t->prog2);
1158 if (!link2)
1159 goto out_unlink1;
1161 server_fd = make_server(SOCK_STREAM, t->listen_at.ip,
1162 t->listen_at.port, NULL);
1163 if (server_fd < 0)
1164 goto out_unlink2;
1166 err = update_lookup_map(t->redir_map, SERVER_A, server_fd);
1167 if (err)
1168 goto out_close_server;
1170 client_fd = make_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst);
1171 if (client_fd < 0)
1172 goto out_close_server;
1174 err = connect(client_fd, (void *)&dst, inetaddr_len(&dst));
1175 if (CHECK(err && !t->expect_errno, "connect",
1176 "unexpected error %d\n", errno))
1177 goto out_close_client;
1178 if (CHECK(err && t->expect_errno && errno != t->expect_errno,
1179 "connect", "unexpected error %d\n", errno))
1180 goto out_close_client;
1182 done = 0;
1183 prog_idx = PROG1;
1184 err = bpf_map_lookup_elem(map_fd, &prog_idx, &done);
1185 CHECK(err, "bpf_map_lookup_elem", "failed\n");
1186 CHECK(!done, "bpf_map_lookup_elem", "PROG1 !done\n");
1188 done = 0;
1189 prog_idx = PROG2;
1190 err = bpf_map_lookup_elem(map_fd, &prog_idx, &done);
1191 CHECK(err, "bpf_map_lookup_elem", "failed\n");
1192 CHECK(!done, "bpf_map_lookup_elem", "PROG2 !done\n");
1194 out_close_client:
1195 close(client_fd);
1196 out_close_server:
1197 close(server_fd);
1198 out_unlink2:
1199 bpf_link__destroy(link2);
1200 out_unlink1:
1201 bpf_link__destroy(link1);
1204 static void test_multi_prog_lookup(struct test_sk_lookup *skel)
1206 struct test_multi_prog tests[] = {
1208 .desc = "multi prog - pass, pass",
1209 .prog1 = skel->progs.multi_prog_pass1,
1210 .prog2 = skel->progs.multi_prog_pass2,
1211 .listen_at = { EXT_IP4, EXT_PORT },
1214 .desc = "multi prog - drop, drop",
1215 .prog1 = skel->progs.multi_prog_drop1,
1216 .prog2 = skel->progs.multi_prog_drop2,
1217 .listen_at = { EXT_IP4, EXT_PORT },
1218 .expect_errno = ECONNREFUSED,
1221 .desc = "multi prog - pass, drop",
1222 .prog1 = skel->progs.multi_prog_pass1,
1223 .prog2 = skel->progs.multi_prog_drop2,
1224 .listen_at = { EXT_IP4, EXT_PORT },
1225 .expect_errno = ECONNREFUSED,
1228 .desc = "multi prog - drop, pass",
1229 .prog1 = skel->progs.multi_prog_drop1,
1230 .prog2 = skel->progs.multi_prog_pass2,
1231 .listen_at = { EXT_IP4, EXT_PORT },
1232 .expect_errno = ECONNREFUSED,
1235 .desc = "multi prog - pass, redir",
1236 .prog1 = skel->progs.multi_prog_pass1,
1237 .prog2 = skel->progs.multi_prog_redir2,
1238 .listen_at = { INT_IP4, INT_PORT },
1241 .desc = "multi prog - redir, pass",
1242 .prog1 = skel->progs.multi_prog_redir1,
1243 .prog2 = skel->progs.multi_prog_pass2,
1244 .listen_at = { INT_IP4, INT_PORT },
1247 .desc = "multi prog - drop, redir",
1248 .prog1 = skel->progs.multi_prog_drop1,
1249 .prog2 = skel->progs.multi_prog_redir2,
1250 .listen_at = { INT_IP4, INT_PORT },
1253 .desc = "multi prog - redir, drop",
1254 .prog1 = skel->progs.multi_prog_redir1,
1255 .prog2 = skel->progs.multi_prog_drop2,
1256 .listen_at = { INT_IP4, INT_PORT },
1259 .desc = "multi prog - redir, redir",
1260 .prog1 = skel->progs.multi_prog_redir1,
1261 .prog2 = skel->progs.multi_prog_redir2,
1262 .listen_at = { INT_IP4, INT_PORT },
1265 struct test_multi_prog *t;
1267 for (t = tests; t < tests + ARRAY_SIZE(tests); t++) {
1268 t->redir_map = skel->maps.redir_map;
1269 t->run_map = skel->maps.run_map;
1270 if (test__start_subtest(t->desc))
1271 run_multi_prog_lookup(t);
1275 static void run_tests(struct test_sk_lookup *skel)
1277 if (test__start_subtest("query lookup prog"))
1278 query_lookup_prog(skel);
1279 test_redirect_lookup(skel);
1280 test_drop_on_lookup(skel);
1281 test_drop_on_reuseport(skel);
1282 test_sk_assign_helper(skel);
1283 test_multi_prog_lookup(skel);
1286 static int switch_netns(void)
1288 static const char * const setup_script[] = {
1289 "ip -6 addr add dev lo " EXT_IP6 "/128",
1290 "ip -6 addr add dev lo " INT_IP6 "/128",
1291 "ip link set dev lo up",
1292 NULL,
1294 const char * const *cmd;
1295 int err;
1297 err = unshare(CLONE_NEWNET);
1298 if (CHECK(err, "unshare", "failed\n")) {
1299 log_err("unshare(CLONE_NEWNET)");
1300 return -1;
1303 for (cmd = setup_script; *cmd; cmd++) {
1304 err = system(*cmd);
1305 if (CHECK(err, "system", "failed\n")) {
1306 log_err("system(%s)", *cmd);
1307 return -1;
1311 return 0;
1314 void test_sk_lookup(void)
1316 struct test_sk_lookup *skel;
1317 int err;
1319 err = switch_netns();
1320 if (err)
1321 return;
1323 skel = test_sk_lookup__open_and_load();
1324 if (CHECK(!skel, "skel open_and_load", "failed\n"))
1325 return;
1327 run_tests(skel);
1329 test_sk_lookup__destroy(skel);