1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright 2022 Google LLC.
10 #include "test_progs.h"
11 #include "cgroup_helpers.h"
12 #include "network_helpers.h"
14 #include "connect_ping.skel.h"
17 #define BINDADDR_V6 { { { 0x20,0x01,0x0d,0xb8,0,0,0,0,0,0,0,0,0,0,0,1 } } }
18 static const struct in6_addr bindaddr_v6 = BINDADDR_V6;
20 static void subtest(int cgroup_fd, struct connect_ping *skel,
21 int family, int do_bind)
23 struct sockaddr_in sa4 = {
24 .sin_family = AF_INET,
25 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
27 struct sockaddr_in6 sa6 = {
28 .sin6_family = AF_INET6,
29 .sin6_addr = IN6ADDR_LOOPBACK_INIT,
31 struct sockaddr *sa = NULL;
38 sa = (struct sockaddr *)&sa4;
40 protocol = IPPROTO_ICMP;
43 sa = (struct sockaddr *)&sa6;
45 protocol = IPPROTO_ICMPV6;
49 memset(skel->bss, 0, sizeof(*skel->bss));
50 skel->bss->do_bind = do_bind;
52 sock_fd = socket(family, SOCK_DGRAM, protocol);
53 if (!ASSERT_GE(sock_fd, 0, "sock-create"))
56 if (!ASSERT_OK(connect(sock_fd, sa, sa_len), "connect"))
59 if (!ASSERT_EQ(skel->bss->invocations_v4, family == AF_INET ? 1 : 0,
62 if (!ASSERT_EQ(skel->bss->invocations_v6, family == AF_INET6 ? 1 : 0,
65 if (!ASSERT_EQ(skel->bss->has_error, 0, "has_error"))
68 if (!ASSERT_OK(getsockname(sock_fd, sa, &sa_len),
74 if (!ASSERT_EQ(sa4.sin_family, family, "sin_family"))
76 if (!ASSERT_EQ(sa4.sin_addr.s_addr,
77 htonl(do_bind ? 0x01010101 : INADDR_LOOPBACK),
82 if (!ASSERT_EQ(sa6.sin6_family, AF_INET6, "sin6_family"))
84 if (!ASSERT_EQ(memcmp(&sa6.sin6_addr,
85 do_bind ? &bindaddr_v6 : &in6addr_loopback,
86 sizeof(sa6.sin6_addr)),
96 void test_connect_ping(void)
98 struct connect_ping *skel;
101 if (!ASSERT_OK(unshare(CLONE_NEWNET | CLONE_NEWNS), "unshare"))
104 /* overmount sysfs, and making original sysfs private so overmount
105 * does not propagate to other mntns.
107 if (!ASSERT_OK(mount("none", "/sys", NULL, MS_PRIVATE, NULL),
108 "remount-private-sys"))
110 if (!ASSERT_OK(mount("sysfs", "/sys", "sysfs", 0, NULL),
113 if (!ASSERT_OK(mount("bpffs", "/sys/fs/bpf", "bpf", 0, NULL),
117 if (!ASSERT_OK(system("ip link set dev lo up"), "lo-up"))
119 if (!ASSERT_OK(system("ip addr add 1.1.1.1 dev lo"), "lo-addr-v4"))
121 if (!ASSERT_OK(system("ip -6 addr add 2001:db8::1 dev lo"), "lo-addr-v6"))
123 if (write_sysctl("/proc/sys/net/ipv4/ping_group_range", "0 0"))
126 cgroup_fd = test__join_cgroup("/connect_ping");
127 if (!ASSERT_GE(cgroup_fd, 0, "cg-create"))
130 skel = connect_ping__open_and_load();
131 if (!ASSERT_OK_PTR(skel, "skel-load"))
133 skel->links.connect_v4_prog =
134 bpf_program__attach_cgroup(skel->progs.connect_v4_prog, cgroup_fd);
135 if (!ASSERT_OK_PTR(skel->links.connect_v4_prog, "cg-attach-v4"))
137 skel->links.connect_v6_prog =
138 bpf_program__attach_cgroup(skel->progs.connect_v6_prog, cgroup_fd);
139 if (!ASSERT_OK_PTR(skel->links.connect_v6_prog, "cg-attach-v6"))
142 /* Connect a v4 ping socket to localhost, assert that only v4 is called,
143 * and called exactly once, and that the socket's bound address is
144 * original loopback address.
146 if (test__start_subtest("ipv4"))
147 subtest(cgroup_fd, skel, AF_INET, 0);
149 /* Connect a v4 ping socket to localhost, assert that only v4 is called,
150 * and called exactly once, and that the socket's bound address is
151 * address we explicitly bound.
153 if (test__start_subtest("ipv4-bind"))
154 subtest(cgroup_fd, skel, AF_INET, 1);
156 /* Connect a v6 ping socket to localhost, assert that only v6 is called,
157 * and called exactly once, and that the socket's bound address is
158 * original loopback address.
160 if (test__start_subtest("ipv6"))
161 subtest(cgroup_fd, skel, AF_INET6, 0);
163 /* Connect a v6 ping socket to localhost, assert that only v6 is called,
164 * and called exactly once, and that the socket's bound address is
165 * address we explicitly bound.
167 if (test__start_subtest("ipv6-bind"))
168 subtest(cgroup_fd, skel, AF_INET6, 1);
171 connect_ping__destroy(skel);
177 umount2("/sys", MNT_DETACH);