1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 // Copyright (c) 2020 Cloudflare
4 * Test BPF attach point for INET socket lookup (BPF_SK_LOOKUP).
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.
18 #include <arpa/inet.h>
25 #include <sys/types.h>
29 #include <bpf/libbpf.h>
32 #include "test_progs.h"
33 #include "bpf_rlimit.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"
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"
51 #define IO_TIMEOUT_SEC 3
71 struct bpf_program
*lookup_prog
;
72 struct bpf_program
*reuseport_prog
;
73 struct bpf_map
*sock_map
;
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
)
92 prog_fd
= bpf_program__fd(reuseport_prog
);
98 err
= setsockopt(sock_fd
, SOL_SOCKET
, SO_ATTACH_REUSEPORT_EBPF
,
99 &prog_fd
, sizeof(prog_fd
));
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
};
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"))
123 fd
= socket(addr
->ss_family
, sotype
, 0);
124 if (CHECK(fd
< 0, "socket", "failed\n")) {
125 log_err("failed to make socket");
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");
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");
146 static int make_server(int sotype
, const char *ip
, int port
,
147 struct bpf_program
*reuseport_prog
)
149 struct sockaddr_storage addr
= {0};
153 fd
= make_socket(sotype
, ip
, port
, &addr
);
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
,
161 if (CHECK(err
, "setsockopt(IP_RECVORIGDSTADDR)", "failed\n")) {
162 log_err("failed to enable IP_RECVORIGDSTADDR");
167 if (sotype
== SOCK_DGRAM
&& addr
.ss_family
== AF_INET6
) {
168 err
= setsockopt(fd
, SOL_IPV6
, IPV6_RECVORIGDSTADDR
, &one
,
170 if (CHECK(err
, "setsockopt(IPV6_RECVORIGDSTADDR)", "failed\n")) {
171 log_err("failed to enable IPV6_RECVORIGDSTADDR");
176 if (sotype
== SOCK_STREAM
) {
177 err
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
,
179 if (CHECK(err
, "setsockopt(SO_REUSEADDR)", "failed\n")) {
180 log_err("failed to enable SO_REUSEADDR");
185 if (reuseport_prog
) {
186 err
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEPORT
, &one
,
188 if (CHECK(err
, "setsockopt(SO_REUSEPORT)", "failed\n")) {
189 log_err("failed to enable SO_REUSEPORT");
194 err
= bind(fd
, (void *)&addr
, inetaddr_len(&addr
));
195 if (CHECK(err
, "bind", "failed\n")) {
196 log_err("failed to bind listen socket");
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
);
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");
223 static int make_client(int sotype
, const char *ip
, int port
)
225 struct sockaddr_storage addr
= {0};
228 fd
= make_socket(sotype
, ip
, port
, &addr
);
232 err
= connect(fd
, (void *)&addr
, inetaddr_len(&addr
));
233 if (CHECK(err
, "make_client", "connect")) {
234 log_err("failed to connect client socket");
244 static int send_byte(int fd
)
249 n
= send(fd
, "a", 1, 0);
250 if (CHECK(n
<= 0, "send_byte", "send")) {
251 log_err("failed/partial send");
257 static int recv_byte(int fd
)
262 n
= recv(fd
, buf
, sizeof(buf
), 0);
263 if (CHECK(n
<= 0, "recv_byte", "recv")) {
264 log_err("failed/partial recv");
270 static int tcp_recv_send(int server_fd
)
276 fd
= accept(server_fd
, NULL
, NULL
);
277 if (CHECK(fd
< 0, "accept", "failed\n")) {
278 log_err("failed to accept");
282 n
= recv(fd
, buf
, sizeof(buf
), 0);
283 if (CHECK(n
<= 0, "recv", "failed\n")) {
284 log_err("failed/partial recv");
289 n
= send(fd
, buf
, n
, 0);
290 if (CHECK(n
<= 0, "send", "failed\n")) {
291 log_err("failed/partial send");
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 };
329 iov
.iov_len
= sizeof(buf
);
331 msg
.msg_name
= src_addr
;
332 msg
.msg_namelen
= sizeof(*src_addr
);
335 msg
.msg_control
= cmsg_buf
;
336 msg
.msg_controllen
= sizeof(cmsg_buf
);
339 n
= recvmsg(server_fd
, &msg
, 0);
340 if (CHECK(n
<= 0, "recvmsg", "failed\n")) {
341 log_err("failed to receive");
344 if (CHECK(msg
.msg_flags
& MSG_CTRUNC
, "recvmsg", "truncated cmsg\n"))
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
);
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"))
361 /* Server socket bound to IPv4-mapped IPv6 address */
362 if (src_addr
->ss_family
== AF_INET6
&&
363 dst_addr
->ss_family
== AF_INET
) {
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");
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");
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");
395 static int tcp_echo_test(int client_fd
, int server_fd
)
399 err
= send_byte(client_fd
);
402 err
= tcp_recv_send(server_fd
);
405 err
= recv_byte(client_fd
);
412 static int udp_echo_test(int client_fd
, int server_fd
)
416 err
= send_byte(client_fd
);
419 err
= udp_recv_send(server_fd
);
422 err
= recv_byte(client_fd
);
429 static struct bpf_link
*attach_lookup_prog(struct bpf_program
*prog
)
431 struct bpf_link
*link
;
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");
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
));
452 static int update_lookup_map(struct bpf_map
*map
, int index
, int sock_fd
)
457 map_fd
= bpf_map__fd(map
);
458 if (CHECK(map_fd
< 0, "bpf_map__fd", "failed\n")) {
460 log_err("failed to get map FD");
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
);
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] = {};
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");
491 link
[0] = attach_lookup_prog(skel
->progs
.lookup_pass
);
494 link
[1] = attach_lookup_prog(skel
->progs
.lookup_pass
);
497 link
[2] = attach_lookup_prog(skel
->progs
.lookup_drop
);
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");
509 if (CHECK(attach_flags
!= 0, "bpf_prog_query",
510 "wrong attach_flags on query: %u", attach_flags
))
512 if (CHECK(prog_cnt
!= 3, "bpf_prog_query",
513 "wrong program count on query: %u", prog_cnt
))
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
))
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
);
548 bpf_link__destroy(link
[2]);
550 bpf_link__destroy(link
[1]);
552 bpf_link__destroy(link
[0]);
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
;
564 lookup_link
= attach_lookup_prog(t
->lookup_prog
);
568 for (i
= 0; i
< ARRAY_SIZE(server_fds
); i
++) {
569 server_fds
[i
] = make_server(t
->sotype
, t
->listen_at
.ip
,
572 if (server_fds
[i
] < 0)
575 err
= update_lookup_map(t
->sock_map
, i
, server_fds
[i
]);
579 /* want just one server for non-reuseport test */
580 if (!t
->reuseport_prog
)
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
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
,
598 if (reuse_conn_fd
< 0)
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
))
605 err
= connect(reuse_conn_fd
, (void *)&addr
, len
);
606 if (CHECK(err
, "connect", "errno %d\n", errno
))
610 client_fd
= make_client(t
->sotype
, t
->connect_to
.ip
, t
->connect_to
.port
);
614 if (t
->sotype
== SOCK_STREAM
)
615 tcp_echo_test(client_fd
, server_fds
[t
->accept_on
]);
617 udp_echo_test(client_fd
, server_fds
[t
->accept_on
]);
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
))
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
;
831 lookup_link
= attach_lookup_prog(t
->lookup_prog
);
835 server_fd
= make_server(t
->sotype
, t
->listen_at
.ip
, t
->listen_at
.port
,
840 client_fd
= make_socket(t
->sotype
, t
->connect_to
.ip
,
841 t
->connect_to
.port
, &dst
);
845 err
= connect(client_fd
, (void *)&dst
, inetaddr_len(&dst
));
846 if (t
->sotype
== SOCK_DGRAM
) {
847 err
= send_byte(client_fd
);
851 /* Read out asynchronous error */
852 n
= recv(client_fd
, NULL
, 0, 0);
855 if (CHECK(!err
|| errno
!= ECONNREFUSED
, "connect",
856 "unexpected success or error\n"))
857 log_err("expected ECONNREFUSED on connect");
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
))
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
;
914 lookup_link
= attach_lookup_prog(t
->lookup_prog
);
918 server1
= make_server(t
->sotype
, t
->listen_at
.ip
, t
->listen_at
.port
,
923 err
= update_lookup_map(t
->sock_map
, SERVER_A
, server1
);
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 */);
933 client
= make_socket(t
->sotype
, t
->connect_to
.ip
,
934 t
->connect_to
.port
, &dst
);
938 err
= connect(client
, (void *)&dst
, inetaddr_len(&dst
));
939 if (t
->sotype
== SOCK_DGRAM
) {
940 err
= send_byte(client
);
944 /* Read out asynchronous error */
945 n
= recv(client
, NULL
, 0, 0);
948 if (CHECK(!err
|| errno
!= ECONNREFUSED
, "connect",
949 "unexpected success or error\n"))
950 log_err("expected ECONNREFUSED on connect");
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
;
1018 lookup_link
= attach_lookup_prog(lookup_prog
);
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)
1027 err
= update_lookup_map(skel
->maps
.redir_map
, i
,
1033 client_fd
= make_client(SOCK_STREAM
, connect_ip
, EXT_PORT
);
1037 peer_fd
= accept(server_fds
[SERVER_B
], NULL
, NULL
);
1038 if (CHECK(peer_fd
< 0, "accept", "failed\n"))
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
,
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
);
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
);
1081 goto out_close_connected
;
1083 lookup_link
= attach_lookup_prog(skel
->progs
.sk_assign_esocknosupport
);
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
);
1090 goto out_unlink_prog
;
1091 if (sotype
== SOCK_DGRAM
) {
1092 send_byte(client_fd
);
1093 recv_byte(server_fd
);
1098 bpf_link__destroy(lookup_link
);
1099 out_close_connected
:
1100 close(connected_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
{
1127 struct bpf_program
*prog1
;
1128 struct bpf_program
*prog2
;
1129 struct bpf_map
*redir_map
;
1130 struct bpf_map
*run_map
;
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
);
1146 err
= bpf_map_update_elem(map_fd
, &prog_idx
, &done
, BPF_ANY
);
1147 if (CHECK(err
, "bpf_map_update_elem", "failed\n"))
1150 err
= bpf_map_update_elem(map_fd
, &prog_idx
, &done
, BPF_ANY
);
1151 if (CHECK(err
, "bpf_map_update_elem", "failed\n"))
1154 link1
= attach_lookup_prog(t
->prog1
);
1157 link2
= attach_lookup_prog(t
->prog2
);
1161 server_fd
= make_server(SOCK_STREAM
, t
->listen_at
.ip
,
1162 t
->listen_at
.port
, NULL
);
1166 err
= update_lookup_map(t
->redir_map
, SERVER_A
, server_fd
);
1168 goto out_close_server
;
1170 client_fd
= make_socket(SOCK_STREAM
, EXT_IP4
, EXT_PORT
, &dst
);
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
;
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");
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");
1199 bpf_link__destroy(link2
);
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",
1294 const char * const *cmd
;
1297 err
= unshare(CLONE_NEWNET
);
1298 if (CHECK(err
, "unshare", "failed\n")) {
1299 log_err("unshare(CLONE_NEWNET)");
1303 for (cmd
= setup_script
; *cmd
; cmd
++) {
1305 if (CHECK(err
, "system", "failed\n")) {
1306 log_err("system(%s)", *cmd
);
1314 void test_sk_lookup(void)
1316 struct test_sk_lookup
*skel
;
1319 err
= switch_netns();
1323 skel
= test_sk_lookup__open_and_load();
1324 if (CHECK(!skel
, "skel open_and_load", "failed\n"))
1329 test_sk_lookup__destroy(skel
);