1*699cd480SApple OSS Distributions /* -*- compile-command: "xcrun --sdk iphoneos.internal make net_tuntests" -*- */
2*699cd480SApple OSS Distributions
3*699cd480SApple OSS Distributions #include <inttypes.h>
4*699cd480SApple OSS Distributions #include <stdbool.h>
5*699cd480SApple OSS Distributions #include <stdio.h>
6*699cd480SApple OSS Distributions #include <stdlib.h>
7*699cd480SApple OSS Distributions #include <time.h>
8*699cd480SApple OSS Distributions #include <string.h>
9*699cd480SApple OSS Distributions #include <unistd.h>
10*699cd480SApple OSS Distributions #include <poll.h>
11*699cd480SApple OSS Distributions #include <sys/types.h>
12*699cd480SApple OSS Distributions #include <sys/event.h>
13*699cd480SApple OSS Distributions #include <sys/time.h>
14*699cd480SApple OSS Distributions #include <uuid/uuid.h>
15*699cd480SApple OSS Distributions #include <arpa/inet.h>
16*699cd480SApple OSS Distributions #include <sys/sysctl.h>
17*699cd480SApple OSS Distributions #include <sys/kern_control.h>
18*699cd480SApple OSS Distributions #include <sys/ioctl.h>
19*699cd480SApple OSS Distributions #include <sys/socket.h>
20*699cd480SApple OSS Distributions #include <sys/kern_control.h>
21*699cd480SApple OSS Distributions #include <sys/sys_domain.h>
22*699cd480SApple OSS Distributions
23*699cd480SApple OSS Distributions #include <net/if.h>
24*699cd480SApple OSS Distributions #include <net/if_ipsec.h>
25*699cd480SApple OSS Distributions #include <net/if_utun.h>
26*699cd480SApple OSS Distributions #include <netinet/in.h>
27*699cd480SApple OSS Distributions #include <netinet/in_var.h>
28*699cd480SApple OSS Distributions #include <net/pfkeyv2.h>
29*699cd480SApple OSS Distributions #include <netinet6/ipsec.h>
30*699cd480SApple OSS Distributions
31*699cd480SApple OSS Distributions #include <darwintest.h>
32*699cd480SApple OSS Distributions #include <darwintest_utils.h>
33*699cd480SApple OSS Distributions
34*699cd480SApple OSS Distributions #include <skywalk/os_skywalk_private.h> // for SK_FEATURE_*
35*699cd480SApple OSS Distributions
36*699cd480SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.net.tun"));
37*699cd480SApple OSS Distributions
38*699cd480SApple OSS Distributions /* Disable all these test until <rdar://problem/49124468> is fixed */
39*699cd480SApple OSS Distributions T_GLOBAL_META(T_META_ENABLED(false));
40*699cd480SApple OSS Distributions
41*699cd480SApple OSS Distributions #if 0
42*699cd480SApple OSS Distributions #undef T_QUIET
43*699cd480SApple OSS Distributions #define T_QUIET
44*699cd480SApple OSS Distributions #endif
45*699cd480SApple OSS Distributions
46*699cd480SApple OSS Distributions #if 0
47*699cd480SApple OSS Distributions static void
48*699cd480SApple OSS Distributions log_hexdump(const void *inp, size_t len)
49*699cd480SApple OSS Distributions {
50*699cd480SApple OSS Distributions unsigned i, off = 0;
51*699cd480SApple OSS Distributions char buf[9 + 16 * 3 + 1];
52*699cd480SApple OSS Distributions for (i = 0; i < len; i++) {
53*699cd480SApple OSS Distributions if (i % 16 == 0) {
54*699cd480SApple OSS Distributions off = (unsigned)snprintf(buf, sizeof(buf), "%08x:", i);
55*699cd480SApple OSS Distributions }
56*699cd480SApple OSS Distributions off += (unsigned)snprintf(buf + off, sizeof(buf) - off, " %02x", (((const uint8_t *)inp)[i]) & 0xff);
57*699cd480SApple OSS Distributions if (i % 16 == 15) {
58*699cd480SApple OSS Distributions T_LOG("%s", buf);
59*699cd480SApple OSS Distributions }
60*699cd480SApple OSS Distributions }
61*699cd480SApple OSS Distributions if (len % 16) {
62*699cd480SApple OSS Distributions T_LOG("%s", buf);
63*699cd480SApple OSS Distributions }
64*699cd480SApple OSS Distributions }
65*699cd480SApple OSS Distributions #else
66*699cd480SApple OSS Distributions static void
log_hexdump(const void * inp,size_t len)67*699cd480SApple OSS Distributions log_hexdump(const void *inp, size_t len)
68*699cd480SApple OSS Distributions {
69*699cd480SApple OSS Distributions #pragma unused(inp, len)
70*699cd480SApple OSS Distributions }
71*699cd480SApple OSS Distributions #endif
72*699cd480SApple OSS Distributions
73*699cd480SApple OSS Distributions static bool
is_netagent_enabled(void)74*699cd480SApple OSS Distributions is_netagent_enabled(void)
75*699cd480SApple OSS Distributions {
76*699cd480SApple OSS Distributions int enabled = 0;
77*699cd480SApple OSS Distributions size_t len = sizeof(enabled);
78*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(sysctlbyname("net.link.generic.system.enable_netagent", &enabled, &len, NULL, 0), NULL);
79*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_EQ(len, sizeof(enabled), NULL);
80*699cd480SApple OSS Distributions return enabled == 1;
81*699cd480SApple OSS Distributions }
82*699cd480SApple OSS Distributions
83*699cd480SApple OSS Distributions static bool g_is_ipsec_test;
84*699cd480SApple OSS Distributions static bool g_is_utun_test;
85*699cd480SApple OSS Distributions static int g_OPT_ENABLE_NETIF = -1;
86*699cd480SApple OSS Distributions static int g_OPT_ENABLE_FLOWSWITCH = -1;
87*699cd480SApple OSS Distributions static int g_OPT_ENABLE_CHANNEL = -1;
88*699cd480SApple OSS Distributions static int g_OPT_GET_CHANNEL_UUID = -1;
89*699cd480SApple OSS Distributions static int g_OPT_IFNAME = -1;
90*699cd480SApple OSS Distributions static char *g_CONTROL_NAME = NULL;
91*699cd480SApple OSS Distributions
92*699cd480SApple OSS Distributions static int create_tunsock_old(int enable_netif, int enable_flowswitch, int channel_count, uuid_t uuid[]);
93*699cd480SApple OSS Distributions static int create_tunsock_new(int enable_netif, int enable_flowswitch, int channel_count, uuid_t uuid[]);
94*699cd480SApple OSS Distributions static int (*create_tunsock)(int enable_netif, int enable_flowswitch, int channel_count, uuid_t uuid[]);
95*699cd480SApple OSS Distributions
96*699cd480SApple OSS Distributions static void
setup_ipsec_test(void)97*699cd480SApple OSS Distributions setup_ipsec_test(void)
98*699cd480SApple OSS Distributions {
99*699cd480SApple OSS Distributions T_LOG("Configuring for ipsec tests");
100*699cd480SApple OSS Distributions g_OPT_ENABLE_NETIF = IPSEC_OPT_ENABLE_NETIF;
101*699cd480SApple OSS Distributions g_OPT_ENABLE_FLOWSWITCH = IPSEC_OPT_ENABLE_FLOWSWITCH;
102*699cd480SApple OSS Distributions g_OPT_ENABLE_CHANNEL = IPSEC_OPT_ENABLE_CHANNEL;
103*699cd480SApple OSS Distributions g_OPT_GET_CHANNEL_UUID = IPSEC_OPT_GET_CHANNEL_UUID;
104*699cd480SApple OSS Distributions g_OPT_IFNAME = IPSEC_OPT_IFNAME;
105*699cd480SApple OSS Distributions g_CONTROL_NAME = IPSEC_CONTROL_NAME;
106*699cd480SApple OSS Distributions create_tunsock = create_tunsock_new;
107*699cd480SApple OSS Distributions g_is_ipsec_test = true;
108*699cd480SApple OSS Distributions }
109*699cd480SApple OSS Distributions
110*699cd480SApple OSS Distributions static void
setup_utun_test(void)111*699cd480SApple OSS Distributions setup_utun_test(void)
112*699cd480SApple OSS Distributions {
113*699cd480SApple OSS Distributions T_LOG("Configuring for utun tests");
114*699cd480SApple OSS Distributions g_OPT_ENABLE_NETIF = UTUN_OPT_ENABLE_NETIF;
115*699cd480SApple OSS Distributions g_OPT_ENABLE_FLOWSWITCH = UTUN_OPT_ENABLE_FLOWSWITCH;
116*699cd480SApple OSS Distributions g_OPT_ENABLE_CHANNEL = UTUN_OPT_ENABLE_CHANNEL;
117*699cd480SApple OSS Distributions g_OPT_GET_CHANNEL_UUID = UTUN_OPT_GET_CHANNEL_UUID;
118*699cd480SApple OSS Distributions g_OPT_IFNAME = UTUN_OPT_IFNAME;
119*699cd480SApple OSS Distributions g_CONTROL_NAME = UTUN_CONTROL_NAME;
120*699cd480SApple OSS Distributions create_tunsock = create_tunsock_old;
121*699cd480SApple OSS Distributions g_is_utun_test = true;
122*699cd480SApple OSS Distributions }
123*699cd480SApple OSS Distributions
124*699cd480SApple OSS Distributions static bool
setblocking(int s,bool blocking)125*699cd480SApple OSS Distributions setblocking(int s, bool blocking)
126*699cd480SApple OSS Distributions {
127*699cd480SApple OSS Distributions int flags;
128*699cd480SApple OSS Distributions bool ret;
129*699cd480SApple OSS Distributions
130*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(flags = fcntl(s, F_GETFL, 0), NULL);
131*699cd480SApple OSS Distributions
132*699cd480SApple OSS Distributions ret = !(flags & O_NONBLOCK);
133*699cd480SApple OSS Distributions
134*699cd480SApple OSS Distributions if (blocking) {
135*699cd480SApple OSS Distributions flags &= ~O_NONBLOCK;
136*699cd480SApple OSS Distributions } else {
137*699cd480SApple OSS Distributions flags |= O_NONBLOCK;
138*699cd480SApple OSS Distributions }
139*699cd480SApple OSS Distributions
140*699cd480SApple OSS Distributions #if 0
141*699cd480SApple OSS Distributions T_LOG("Setting fd %d from %s to %s\n",
142*699cd480SApple OSS Distributions s, ret ? "blocking" : "nonblocking",
143*699cd480SApple OSS Distributions blocking ? "blocking" : "nonblocking");
144*699cd480SApple OSS Distributions #endif
145*699cd480SApple OSS Distributions
146*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(flags = fcntl(s, F_SETFL, flags), NULL);
147*699cd480SApple OSS Distributions
148*699cd480SApple OSS Distributions return ret;
149*699cd480SApple OSS Distributions }
150*699cd480SApple OSS Distributions
151*699cd480SApple OSS Distributions
152*699cd480SApple OSS Distributions static void
check_enables(int tunsock,int enable_netif,int enable_flowswitch,int channel_count,uuid_t uuid[])153*699cd480SApple OSS Distributions check_enables(int tunsock, int enable_netif, int enable_flowswitch, int channel_count, uuid_t uuid[])
154*699cd480SApple OSS Distributions {
155*699cd480SApple OSS Distributions int scratch;
156*699cd480SApple OSS Distributions socklen_t scratchlen, uuidlen;
157*699cd480SApple OSS Distributions uuid_t scratchuuid[channel_count];
158*699cd480SApple OSS Distributions if (!uuid) {
159*699cd480SApple OSS Distributions uuid = scratchuuid;
160*699cd480SApple OSS Distributions }
161*699cd480SApple OSS Distributions
162*699cd480SApple OSS Distributions //T_LOG("checking tunsock %d", tunsock);
163*699cd480SApple OSS Distributions
164*699cd480SApple OSS Distributions if (g_is_ipsec_test && channel_count && !enable_netif) {
165*699cd480SApple OSS Distributions /* Unfortunately, the connect incorrectly unwinds the bind if it get an error.
166*699cd480SApple OSS Distributions * until that is fixed, expect EINVAL here
167*699cd480SApple OSS Distributions */
168*699cd480SApple OSS Distributions scratchlen = sizeof(scratch);
169*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_NETIF,
170*699cd480SApple OSS Distributions &scratch, &scratchlen), EINVAL, NULL);
171*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
172*699cd480SApple OSS Distributions &scratch, &scratchlen), EINVAL, NULL);
173*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_CHANNEL,
174*699cd480SApple OSS Distributions &scratch, &scratchlen), EINVAL, NULL);
175*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
176*699cd480SApple OSS Distributions uuid_clear(uuid[i]);
177*699cd480SApple OSS Distributions }
178*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
179*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
180*699cd480SApple OSS Distributions uuid, &uuidlen), EINVAL, NULL);
181*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
182*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(uuid_is_null(uuid[i]), NULL);
183*699cd480SApple OSS Distributions }
184*699cd480SApple OSS Distributions return;
185*699cd480SApple OSS Distributions }
186*699cd480SApple OSS Distributions
187*699cd480SApple OSS Distributions
188*699cd480SApple OSS Distributions scratchlen = sizeof(scratch);
189*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_NETIF,
190*699cd480SApple OSS Distributions &scratch, &scratchlen), NULL);
191*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)scratchlen, sizeof(scratch), NULL);
192*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ(scratch, enable_netif, NULL);
193*699cd480SApple OSS Distributions
194*699cd480SApple OSS Distributions scratchlen = sizeof(scratch);
195*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
196*699cd480SApple OSS Distributions &scratch, &scratchlen), NULL);
197*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)scratchlen, sizeof(scratch), NULL);
198*699cd480SApple OSS Distributions if (is_netagent_enabled()) {
199*699cd480SApple OSS Distributions if (enable_netif) {
200*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ(scratch, enable_flowswitch, NULL);
201*699cd480SApple OSS Distributions } else {
202*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ(scratch, 0, NULL);
203*699cd480SApple OSS Distributions }
204*699cd480SApple OSS Distributions } else {
205*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ(scratch, 0, NULL);
206*699cd480SApple OSS Distributions }
207*699cd480SApple OSS Distributions
208*699cd480SApple OSS Distributions scratchlen = sizeof(scratch);
209*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_CHANNEL,
210*699cd480SApple OSS Distributions &scratch, &scratchlen), NULL);
211*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)scratchlen, sizeof(scratch), NULL);
212*699cd480SApple OSS Distributions if (g_is_ipsec_test && !enable_netif) {
213*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ(scratch, 0, NULL);
214*699cd480SApple OSS Distributions } else {
215*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ(scratch, (int)channel_count, NULL);
216*699cd480SApple OSS Distributions }
217*699cd480SApple OSS Distributions
218*699cd480SApple OSS Distributions if (scratch) {
219*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
220*699cd480SApple OSS Distributions uuid_clear(uuid[i]);
221*699cd480SApple OSS Distributions }
222*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
223*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
224*699cd480SApple OSS Distributions uuid, &uuidlen), NULL);
225*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)uuidlen, sizeof(uuid_t) * (unsigned long)channel_count, NULL);
226*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
227*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_FALSE(uuid_is_null(uuid[i]), NULL);
228*699cd480SApple OSS Distributions }
229*699cd480SApple OSS Distributions } else {
230*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
231*699cd480SApple OSS Distributions uuid_clear(uuid[i]);
232*699cd480SApple OSS Distributions }
233*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
234*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
235*699cd480SApple OSS Distributions uuid, &uuidlen), ENXIO, NULL);
236*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)uuidlen, sizeof(uuid_t) * (unsigned long)channel_count, NULL);
237*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
238*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(uuid_is_null(uuid[i]), NULL);
239*699cd480SApple OSS Distributions }
240*699cd480SApple OSS Distributions }
241*699cd480SApple OSS Distributions }
242*699cd480SApple OSS Distributions
243*699cd480SApple OSS Distributions static void
tunsock_get_ifname(int s,char ifname[IFXNAMSIZ])244*699cd480SApple OSS Distributions tunsock_get_ifname(int s, char ifname[IFXNAMSIZ])
245*699cd480SApple OSS Distributions {
246*699cd480SApple OSS Distributions socklen_t optlen = IFXNAMSIZ;
247*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_POSIX_ZERO(getsockopt(s, SYSPROTO_CONTROL, g_OPT_IFNAME, ifname, &optlen), NULL);
248*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(optlen > 0, NULL);
249*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(ifname[optlen - 1] == '\0', NULL);
250*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(strlen(ifname) + 1 == optlen, "got ifname \"%s\" len %zd expected %u", ifname, strlen(ifname), optlen);
251*699cd480SApple OSS Distributions }
252*699cd480SApple OSS Distributions
253*699cd480SApple OSS Distributions static short
ifnet_get_flags(int s,const char ifname[IFNAMSIZ])254*699cd480SApple OSS Distributions ifnet_get_flags(int s, const char ifname[IFNAMSIZ])
255*699cd480SApple OSS Distributions {
256*699cd480SApple OSS Distributions struct ifreq ifr;
257*699cd480SApple OSS Distributions memset(&ifr, 0, sizeof(ifr));
258*699cd480SApple OSS Distributions strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
259*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr), NULL);
260*699cd480SApple OSS Distributions return ifr.ifr_flags;
261*699cd480SApple OSS Distributions }
262*699cd480SApple OSS Distributions
263*699cd480SApple OSS Distributions static void
ifnet_add_addr4(const char ifname[IFNAMSIZ],struct in_addr * addr,struct in_addr * mask,struct in_addr * broadaddr)264*699cd480SApple OSS Distributions ifnet_add_addr4(const char ifname[IFNAMSIZ], struct in_addr *addr, struct in_addr *mask, struct in_addr *broadaddr)
265*699cd480SApple OSS Distributions {
266*699cd480SApple OSS Distributions struct sockaddr_in *sin;
267*699cd480SApple OSS Distributions struct in_aliasreq ifra;
268*699cd480SApple OSS Distributions int s;
269*699cd480SApple OSS Distributions
270*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(s = socket(AF_INET, SOCK_DGRAM, 0), NULL);
271*699cd480SApple OSS Distributions
272*699cd480SApple OSS Distributions memset(&ifra, 0, sizeof(ifra));
273*699cd480SApple OSS Distributions strlcpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
274*699cd480SApple OSS Distributions
275*699cd480SApple OSS Distributions if (addr != NULL) {
276*699cd480SApple OSS Distributions sin = &ifra.ifra_addr;
277*699cd480SApple OSS Distributions sin->sin_len = sizeof(*sin);
278*699cd480SApple OSS Distributions sin->sin_family = AF_INET;
279*699cd480SApple OSS Distributions sin->sin_addr = *addr;
280*699cd480SApple OSS Distributions }
281*699cd480SApple OSS Distributions
282*699cd480SApple OSS Distributions if (mask != NULL) {
283*699cd480SApple OSS Distributions sin = &ifra.ifra_mask;
284*699cd480SApple OSS Distributions sin->sin_len = sizeof(*sin);
285*699cd480SApple OSS Distributions sin->sin_family = AF_INET;
286*699cd480SApple OSS Distributions sin->sin_addr = *mask;
287*699cd480SApple OSS Distributions }
288*699cd480SApple OSS Distributions
289*699cd480SApple OSS Distributions if (broadaddr != NULL || (addr != NULL &&
290*699cd480SApple OSS Distributions (ifnet_get_flags(s, ifname) & IFF_POINTOPOINT) != 0)) {
291*699cd480SApple OSS Distributions sin = &ifra.ifra_broadaddr;
292*699cd480SApple OSS Distributions sin->sin_len = sizeof(*sin);
293*699cd480SApple OSS Distributions sin->sin_family = AF_INET;
294*699cd480SApple OSS Distributions sin->sin_addr = (broadaddr != NULL) ? *broadaddr : *addr;
295*699cd480SApple OSS Distributions }
296*699cd480SApple OSS Distributions
297*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(ioctl(s, SIOCAIFADDR, &ifra), NULL);
298*699cd480SApple OSS Distributions
299*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(s), NULL);
300*699cd480SApple OSS Distributions }
301*699cd480SApple OSS Distributions
302*699cd480SApple OSS Distributions static int g_pfkeyso = -1;
303*699cd480SApple OSS Distributions static struct in_addr g_addr1, g_addr2;
304*699cd480SApple OSS Distributions
305*699cd480SApple OSS Distributions static void
create_sa(const char ifname[IFXNAMSIZ],uint8_t type,uint32_t spi,struct in_addr * src,struct in_addr * dst)306*699cd480SApple OSS Distributions create_sa(const char ifname[IFXNAMSIZ], uint8_t type, uint32_t spi, struct in_addr *src, struct in_addr *dst)
307*699cd480SApple OSS Distributions {
308*699cd480SApple OSS Distributions if (g_pfkeyso == -1) {
309*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(g_pfkeyso = socket(PF_KEY, SOCK_RAW, PF_KEY_V2), NULL);
310*699cd480SApple OSS Distributions }
311*699cd480SApple OSS Distributions
312*699cd480SApple OSS Distributions /*
313*699cd480SApple OSS Distributions * <base, SA, (lifetime(HS),) address(SD), (address(P),)
314*699cd480SApple OSS Distributions * key(AE), (identity(SD),) (sensitivity)>
315*699cd480SApple OSS Distributions */
316*699cd480SApple OSS Distributions
317*699cd480SApple OSS Distributions struct {
318*699cd480SApple OSS Distributions struct sadb_msg msg __attribute((aligned(sizeof(uint64_t))));
319*699cd480SApple OSS Distributions struct sadb_key key __attribute((aligned(sizeof(uint64_t))));
320*699cd480SApple OSS Distributions struct sadb_sa sa __attribute((aligned(sizeof(uint64_t))));
321*699cd480SApple OSS Distributions struct sadb_x_sa2 sa2 __attribute((aligned(sizeof(uint64_t))));
322*699cd480SApple OSS Distributions struct sadb_x_ipsecif ipsecif __attribute((aligned(sizeof(uint64_t))));
323*699cd480SApple OSS Distributions struct {
324*699cd480SApple OSS Distributions struct sadb_address addr __attribute((aligned(sizeof(uint64_t))));
325*699cd480SApple OSS Distributions struct sockaddr_in saddr __attribute((aligned(sizeof(uint64_t))));
326*699cd480SApple OSS Distributions } src;
327*699cd480SApple OSS Distributions struct {
328*699cd480SApple OSS Distributions struct sadb_address addr __attribute((aligned(sizeof(uint64_t))));
329*699cd480SApple OSS Distributions struct sockaddr_in saddr __attribute((aligned(sizeof(uint64_t))));
330*699cd480SApple OSS Distributions } dst;
331*699cd480SApple OSS Distributions } addcmd;
332*699cd480SApple OSS Distributions
333*699cd480SApple OSS Distributions memset(&addcmd, 0, sizeof(addcmd));
334*699cd480SApple OSS Distributions
335*699cd480SApple OSS Distributions addcmd.msg.sadb_msg_version = PF_KEY_V2;
336*699cd480SApple OSS Distributions addcmd.msg.sadb_msg_type = type;
337*699cd480SApple OSS Distributions addcmd.msg.sadb_msg_errno = 0;
338*699cd480SApple OSS Distributions addcmd.msg.sadb_msg_satype = SADB_SATYPE_ESP;
339*699cd480SApple OSS Distributions addcmd.msg.sadb_msg_len = PFKEY_UNIT64(sizeof(addcmd));
340*699cd480SApple OSS Distributions addcmd.msg.sadb_msg_reserved = 0;
341*699cd480SApple OSS Distributions addcmd.msg.sadb_msg_seq = 0;
342*699cd480SApple OSS Distributions addcmd.msg.sadb_msg_pid = (unsigned)getpid();
343*699cd480SApple OSS Distributions
344*699cd480SApple OSS Distributions addcmd.key.sadb_key_len = PFKEY_UNIT64(sizeof(addcmd.key));
345*699cd480SApple OSS Distributions addcmd.key.sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
346*699cd480SApple OSS Distributions addcmd.key.sadb_key_bits = 0;
347*699cd480SApple OSS Distributions addcmd.key.sadb_key_reserved = 0;
348*699cd480SApple OSS Distributions
349*699cd480SApple OSS Distributions addcmd.sa.sadb_sa_len = PFKEY_UNIT64(sizeof(addcmd.sa));
350*699cd480SApple OSS Distributions addcmd.sa.sadb_sa_exttype = SADB_EXT_SA;
351*699cd480SApple OSS Distributions addcmd.sa.sadb_sa_spi = htonl(spi);
352*699cd480SApple OSS Distributions addcmd.sa.sadb_sa_replay = 0;
353*699cd480SApple OSS Distributions addcmd.sa.sadb_sa_state = 0;
354*699cd480SApple OSS Distributions addcmd.sa.sadb_sa_auth = SADB_AALG_NONE;
355*699cd480SApple OSS Distributions addcmd.sa.sadb_sa_encrypt = SADB_EALG_NULL;
356*699cd480SApple OSS Distributions addcmd.sa.sadb_sa_flags = SADB_X_EXT_CYCSEQ;
357*699cd480SApple OSS Distributions
358*699cd480SApple OSS Distributions addcmd.sa2.sadb_x_sa2_len = PFKEY_UNIT64(sizeof(addcmd.sa2));
359*699cd480SApple OSS Distributions addcmd.sa2.sadb_x_sa2_exttype = SADB_X_EXT_SA2;
360*699cd480SApple OSS Distributions addcmd.sa2.sadb_x_sa2_mode = IPSEC_MODE_ANY;
361*699cd480SApple OSS Distributions addcmd.sa2.sadb_x_sa2_alwaysexpire = 1;
362*699cd480SApple OSS Distributions addcmd.sa2.sadb_x_sa2_flags = SADB_X_EXT_SA2_DELETE_ON_DETACH;
363*699cd480SApple OSS Distributions addcmd.sa2.sadb_x_sa2_sequence = 0;
364*699cd480SApple OSS Distributions addcmd.sa2.sadb_x_sa2_reqid = 0;
365*699cd480SApple OSS Distributions
366*699cd480SApple OSS Distributions addcmd.ipsecif.sadb_x_ipsecif_len = PFKEY_UNIT64(sizeof(addcmd.ipsecif));
367*699cd480SApple OSS Distributions addcmd.ipsecif.sadb_x_ipsecif_exttype = SADB_X_EXT_IPSECIF;
368*699cd480SApple OSS Distributions memset(addcmd.ipsecif.sadb_x_ipsecif_internal_if, 0, sizeof(addcmd.ipsecif.sadb_x_ipsecif_internal_if));
369*699cd480SApple OSS Distributions memset(addcmd.ipsecif.sadb_x_ipsecif_outgoing_if, 0, sizeof(addcmd.ipsecif.sadb_x_ipsecif_outgoing_if));
370*699cd480SApple OSS Distributions strlcpy(addcmd.ipsecif.sadb_x_ipsecif_ipsec_if, ifname, sizeof(addcmd.ipsecif.sadb_x_ipsecif_ipsec_if));
371*699cd480SApple OSS Distributions addcmd.ipsecif.sadb_x_ipsecif_init_disabled = 0;
372*699cd480SApple OSS Distributions addcmd.ipsecif.reserved = 0;
373*699cd480SApple OSS Distributions
374*699cd480SApple OSS Distributions addcmd.src.addr.sadb_address_len = PFKEY_UNIT64(sizeof(addcmd.src));
375*699cd480SApple OSS Distributions addcmd.src.addr.sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
376*699cd480SApple OSS Distributions addcmd.src.addr.sadb_address_proto = IPSEC_ULPROTO_ANY;
377*699cd480SApple OSS Distributions addcmd.src.addr.sadb_address_prefixlen = sizeof(struct in_addr) << 3; //XXX Why?
378*699cd480SApple OSS Distributions addcmd.src.addr.sadb_address_reserved = 0;
379*699cd480SApple OSS Distributions addcmd.src.saddr.sin_len = sizeof(addcmd.src.saddr);
380*699cd480SApple OSS Distributions addcmd.src.saddr.sin_family = AF_INET;
381*699cd480SApple OSS Distributions addcmd.src.saddr.sin_port = htons(0);
382*699cd480SApple OSS Distributions addcmd.src.saddr.sin_addr = *src;
383*699cd480SApple OSS Distributions
384*699cd480SApple OSS Distributions addcmd.dst.addr.sadb_address_len = PFKEY_UNIT64(sizeof(addcmd.dst));
385*699cd480SApple OSS Distributions addcmd.dst.addr.sadb_address_exttype = SADB_EXT_ADDRESS_DST;
386*699cd480SApple OSS Distributions addcmd.dst.addr.sadb_address_proto = IPSEC_ULPROTO_ANY;
387*699cd480SApple OSS Distributions addcmd.dst.addr.sadb_address_prefixlen = sizeof(struct in_addr) << 3; //XXX Why?
388*699cd480SApple OSS Distributions addcmd.dst.addr.sadb_address_reserved = 0;
389*699cd480SApple OSS Distributions addcmd.dst.saddr.sin_len = sizeof(addcmd.dst.saddr);
390*699cd480SApple OSS Distributions addcmd.dst.saddr.sin_family = AF_INET;
391*699cd480SApple OSS Distributions addcmd.dst.saddr.sin_port = htons(0);
392*699cd480SApple OSS Distributions addcmd.dst.saddr.sin_addr = *dst;
393*699cd480SApple OSS Distributions
394*699cd480SApple OSS Distributions log_hexdump(&addcmd, sizeof(addcmd));
395*699cd480SApple OSS Distributions
396*699cd480SApple OSS Distributions ssize_t slen;
397*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(slen = send(g_pfkeyso, &addcmd, sizeof(addcmd), 0), NULL);
398*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ(slen, (ssize_t)sizeof(addcmd), NULL);
399*699cd480SApple OSS Distributions }
400*699cd480SApple OSS Distributions
401*699cd480SApple OSS Distributions /* This version of the test expects channels to be enabled after connect.
402*699cd480SApple OSS Distributions * Once the utun driver is converted, switch to create_tunsock_new
403*699cd480SApple OSS Distributions */
404*699cd480SApple OSS Distributions static int
create_tunsock_old(int enable_netif,int enable_flowswitch,int channel_count,uuid_t uuid[])405*699cd480SApple OSS Distributions create_tunsock_old(int enable_netif, int enable_flowswitch, int channel_count, uuid_t uuid[])
406*699cd480SApple OSS Distributions {
407*699cd480SApple OSS Distributions int tunsock;
408*699cd480SApple OSS Distributions struct ctl_info kernctl_info;
409*699cd480SApple OSS Distributions struct sockaddr_ctl kernctl_addr;
410*699cd480SApple OSS Distributions uuid_t scratchuuid[channel_count];
411*699cd480SApple OSS Distributions if (!uuid) {
412*699cd480SApple OSS Distributions uuid = scratchuuid;
413*699cd480SApple OSS Distributions }
414*699cd480SApple OSS Distributions socklen_t uuidlen;
415*699cd480SApple OSS Distributions
416*699cd480SApple OSS Distributions startover:
417*699cd480SApple OSS Distributions
418*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(tunsock = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), NULL);
419*699cd480SApple OSS Distributions
420*699cd480SApple OSS Distributions memset(&kernctl_info, 0, sizeof(kernctl_info));
421*699cd480SApple OSS Distributions strlcpy(kernctl_info.ctl_name, g_CONTROL_NAME, sizeof(kernctl_info.ctl_name));
422*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(ioctl(tunsock, CTLIOCGINFO, &kernctl_info), NULL);
423*699cd480SApple OSS Distributions
424*699cd480SApple OSS Distributions memset(&kernctl_addr, 0, sizeof(kernctl_addr));
425*699cd480SApple OSS Distributions kernctl_addr.sc_len = sizeof(kernctl_addr);
426*699cd480SApple OSS Distributions kernctl_addr.sc_family = AF_SYSTEM;
427*699cd480SApple OSS Distributions kernctl_addr.ss_sysaddr = AF_SYS_CONTROL;
428*699cd480SApple OSS Distributions kernctl_addr.sc_id = kernctl_info.ctl_id;
429*699cd480SApple OSS Distributions kernctl_addr.sc_unit = 0;
430*699cd480SApple OSS Distributions
431*699cd480SApple OSS Distributions T_LOG("%s: enable_netif = %d, enable_flowswitch = %d, channel_count = %d",
432*699cd480SApple OSS Distributions __func__, enable_netif, enable_flowswitch, channel_count);
433*699cd480SApple OSS Distributions
434*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_NETIF,
435*699cd480SApple OSS Distributions &enable_netif, sizeof(enable_netif)), EINVAL, NULL);
436*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
437*699cd480SApple OSS Distributions &enable_flowswitch, sizeof(enable_flowswitch)), EINVAL, NULL);
438*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_CHANNEL,
439*699cd480SApple OSS Distributions &channel_count, sizeof(channel_count)), EINVAL, NULL);
440*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
441*699cd480SApple OSS Distributions uuid_clear(uuid[i]);
442*699cd480SApple OSS Distributions }
443*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
444*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
445*699cd480SApple OSS Distributions uuid, &uuidlen), EINVAL, NULL);
446*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)uuidlen, sizeof(uuid_t) * (unsigned long)channel_count, NULL);
447*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
448*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(uuid_is_null(uuid[i]), NULL);
449*699cd480SApple OSS Distributions }
450*699cd480SApple OSS Distributions
451*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(bind(tunsock, (struct sockaddr *)&kernctl_addr, sizeof(kernctl_addr)), NULL);
452*699cd480SApple OSS Distributions
453*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_NETIF,
454*699cd480SApple OSS Distributions &enable_netif, sizeof(enable_netif)), NULL);
455*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
456*699cd480SApple OSS Distributions &enable_flowswitch, sizeof(enable_flowswitch)), EINVAL, NULL);
457*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_CHANNEL,
458*699cd480SApple OSS Distributions &channel_count, sizeof(channel_count)), EINVAL, NULL);
459*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
460*699cd480SApple OSS Distributions uuid_clear(uuid[i]);
461*699cd480SApple OSS Distributions }
462*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
463*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
464*699cd480SApple OSS Distributions uuid, &uuidlen), ENXIO, NULL);
465*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)uuidlen, sizeof(uuid_t) * (unsigned long)channel_count, NULL);
466*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
467*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(uuid_is_null(uuid[i]), NULL);
468*699cd480SApple OSS Distributions }
469*699cd480SApple OSS Distributions
470*699cd480SApple OSS Distributions int error = connect(tunsock, (struct sockaddr *)&kernctl_addr, sizeof(kernctl_addr));
471*699cd480SApple OSS Distributions if (error == -1 && errno == EBUSY) {
472*699cd480SApple OSS Distributions /* XXX remove this retry nonsense when this is fixed:
473*699cd480SApple OSS Distributions * <rdar://problem/37340313> creating an interface without specifying specific interface name should not return EBUSY
474*699cd480SApple OSS Distributions */
475*699cd480SApple OSS Distributions close(tunsock);
476*699cd480SApple OSS Distributions T_LOG("connect got EBUSY, sleeping 1 second before retry");
477*699cd480SApple OSS Distributions sleep(1);
478*699cd480SApple OSS Distributions goto startover;
479*699cd480SApple OSS Distributions }
480*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(error, "connect()");
481*699cd480SApple OSS Distributions
482*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_NETIF,
483*699cd480SApple OSS Distributions &enable_netif, sizeof(enable_netif)), EINVAL, NULL);
484*699cd480SApple OSS Distributions
485*699cd480SApple OSS Distributions if (is_netagent_enabled()) {
486*699cd480SApple OSS Distributions if (enable_netif) {
487*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
488*699cd480SApple OSS Distributions &enable_flowswitch, sizeof(enable_flowswitch)), NULL);
489*699cd480SApple OSS Distributions } else {
490*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
491*699cd480SApple OSS Distributions &enable_flowswitch, sizeof(enable_flowswitch)), ENOENT, NULL);
492*699cd480SApple OSS Distributions }
493*699cd480SApple OSS Distributions } else {
494*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
495*699cd480SApple OSS Distributions &enable_flowswitch, sizeof(enable_flowswitch)), ENOTSUP, NULL);
496*699cd480SApple OSS Distributions }
497*699cd480SApple OSS Distributions
498*699cd480SApple OSS Distributions if (channel_count) {
499*699cd480SApple OSS Distributions if (g_is_ipsec_test && !enable_netif) {
500*699cd480SApple OSS Distributions /* ipsec doesn't support channels without a netif */
501*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_CHANNEL,
502*699cd480SApple OSS Distributions &channel_count, sizeof(channel_count)), EOPNOTSUPP, NULL);
503*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
504*699cd480SApple OSS Distributions uuid_clear(uuid[i]);
505*699cd480SApple OSS Distributions }
506*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
507*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
508*699cd480SApple OSS Distributions uuid, &uuidlen), ENXIO, NULL);
509*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)uuidlen, sizeof(uuid_t) * (unsigned long)channel_count, NULL);
510*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
511*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(uuid_is_null(uuid[i]), NULL);
512*699cd480SApple OSS Distributions }
513*699cd480SApple OSS Distributions } else {
514*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_CHANNEL,
515*699cd480SApple OSS Distributions &channel_count, sizeof(channel_count)), NULL);
516*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
517*699cd480SApple OSS Distributions uuid_clear(uuid[i]);
518*699cd480SApple OSS Distributions }
519*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
520*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
521*699cd480SApple OSS Distributions uuid, &uuidlen), NULL);
522*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)uuidlen, sizeof(uuid_t) * (unsigned long)channel_count, NULL);
523*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
524*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_FALSE(uuid_is_null(uuid[i]), NULL);
525*699cd480SApple OSS Distributions }
526*699cd480SApple OSS Distributions }
527*699cd480SApple OSS Distributions } else {
528*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_CHANNEL,
529*699cd480SApple OSS Distributions &channel_count, sizeof(channel_count)), ENXIO, NULL);
530*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
531*699cd480SApple OSS Distributions uuid_clear(uuid[i]);
532*699cd480SApple OSS Distributions }
533*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
534*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
535*699cd480SApple OSS Distributions uuid, &uuidlen), ENXIO, NULL);
536*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)uuidlen, sizeof(uuid_t) * (unsigned long)channel_count, NULL);
537*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
538*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(uuid_is_null(uuid[i]), NULL);
539*699cd480SApple OSS Distributions }
540*699cd480SApple OSS Distributions }
541*699cd480SApple OSS Distributions
542*699cd480SApple OSS Distributions check_enables(tunsock, enable_netif, enable_flowswitch, channel_count, uuid);
543*699cd480SApple OSS Distributions
544*699cd480SApple OSS Distributions //T_LOG("Returning tunsock %d", tunsock);
545*699cd480SApple OSS Distributions
546*699cd480SApple OSS Distributions return tunsock;
547*699cd480SApple OSS Distributions }
548*699cd480SApple OSS Distributions
549*699cd480SApple OSS Distributions /* This version of the test expects channels to be enabled before connect
550*699cd480SApple OSS Distributions * Once the utun driver is converted, rename this to just create_tunsock
551*699cd480SApple OSS Distributions */
552*699cd480SApple OSS Distributions static int
create_tunsock_new(int enable_netif,int enable_flowswitch,int channel_count,uuid_t uuid[])553*699cd480SApple OSS Distributions create_tunsock_new(int enable_netif, int enable_flowswitch, int channel_count, uuid_t uuid[])
554*699cd480SApple OSS Distributions {
555*699cd480SApple OSS Distributions int tunsock;
556*699cd480SApple OSS Distributions struct ctl_info kernctl_info;
557*699cd480SApple OSS Distributions struct sockaddr_ctl kernctl_addr;
558*699cd480SApple OSS Distributions uuid_t scratchuuid[channel_count];
559*699cd480SApple OSS Distributions if (!uuid) {
560*699cd480SApple OSS Distributions uuid = scratchuuid;
561*699cd480SApple OSS Distributions }
562*699cd480SApple OSS Distributions socklen_t uuidlen;
563*699cd480SApple OSS Distributions
564*699cd480SApple OSS Distributions startover:
565*699cd480SApple OSS Distributions
566*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(tunsock = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), NULL);
567*699cd480SApple OSS Distributions
568*699cd480SApple OSS Distributions memset(&kernctl_info, 0, sizeof(kernctl_info));
569*699cd480SApple OSS Distributions strlcpy(kernctl_info.ctl_name, g_CONTROL_NAME, sizeof(kernctl_info.ctl_name));
570*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(ioctl(tunsock, CTLIOCGINFO, &kernctl_info), NULL);
571*699cd480SApple OSS Distributions
572*699cd480SApple OSS Distributions memset(&kernctl_addr, 0, sizeof(kernctl_addr));
573*699cd480SApple OSS Distributions kernctl_addr.sc_len = sizeof(kernctl_addr);
574*699cd480SApple OSS Distributions kernctl_addr.sc_family = AF_SYSTEM;
575*699cd480SApple OSS Distributions kernctl_addr.ss_sysaddr = AF_SYS_CONTROL;
576*699cd480SApple OSS Distributions kernctl_addr.sc_id = kernctl_info.ctl_id;
577*699cd480SApple OSS Distributions kernctl_addr.sc_unit = 0;
578*699cd480SApple OSS Distributions
579*699cd480SApple OSS Distributions T_LOG("%s: enable_netif = %d, enable_flowswitch = %d, channel_count = %d",
580*699cd480SApple OSS Distributions __func__, enable_netif, enable_flowswitch, channel_count);
581*699cd480SApple OSS Distributions
582*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_NETIF,
583*699cd480SApple OSS Distributions &enable_netif, sizeof(enable_netif)), EINVAL, NULL);
584*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
585*699cd480SApple OSS Distributions &enable_flowswitch, sizeof(enable_flowswitch)), EINVAL, NULL);
586*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_CHANNEL,
587*699cd480SApple OSS Distributions &channel_count, sizeof(channel_count)), EINVAL, NULL);
588*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
589*699cd480SApple OSS Distributions uuid_clear(uuid[i]);
590*699cd480SApple OSS Distributions }
591*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
592*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
593*699cd480SApple OSS Distributions uuid, &uuidlen), EINVAL, NULL);
594*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)uuidlen, sizeof(uuid_t) * (unsigned long)channel_count, NULL);
595*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
596*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(uuid_is_null(uuid[i]), NULL);
597*699cd480SApple OSS Distributions }
598*699cd480SApple OSS Distributions
599*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(bind(tunsock, (struct sockaddr *)&kernctl_addr, sizeof(kernctl_addr)), NULL);
600*699cd480SApple OSS Distributions
601*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_NETIF,
602*699cd480SApple OSS Distributions &enable_netif, sizeof(enable_netif)), NULL);
603*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
604*699cd480SApple OSS Distributions &enable_flowswitch, sizeof(enable_flowswitch)), EINVAL, NULL);
605*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_CHANNEL,
606*699cd480SApple OSS Distributions &channel_count, sizeof(channel_count)), NULL);
607*699cd480SApple OSS Distributions
608*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
609*699cd480SApple OSS Distributions uuid_clear(uuid[i]);
610*699cd480SApple OSS Distributions }
611*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
612*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
613*699cd480SApple OSS Distributions uuid, &uuidlen), ENXIO, NULL);
614*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)uuidlen, sizeof(uuid_t) * (unsigned long)channel_count, NULL);
615*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
616*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(uuid_is_null(uuid[i]), NULL);
617*699cd480SApple OSS Distributions }
618*699cd480SApple OSS Distributions
619*699cd480SApple OSS Distributions int error = connect(tunsock, (struct sockaddr *)&kernctl_addr, sizeof(kernctl_addr));
620*699cd480SApple OSS Distributions if (error == -1 && errno == EBUSY) {
621*699cd480SApple OSS Distributions /* XXX remove this retry nonsense when this is fixed:
622*699cd480SApple OSS Distributions * <rdar://problem/37340313> creating an interface without specifying specific interface name should not return EBUSY
623*699cd480SApple OSS Distributions */
624*699cd480SApple OSS Distributions close(tunsock);
625*699cd480SApple OSS Distributions T_LOG("connect got EBUSY, sleeping 1 second before retry");
626*699cd480SApple OSS Distributions sleep(1);
627*699cd480SApple OSS Distributions goto startover;
628*699cd480SApple OSS Distributions }
629*699cd480SApple OSS Distributions if (g_is_ipsec_test && channel_count && !enable_netif) {
630*699cd480SApple OSS Distributions /* ipsec doesn't support channels without a netif */
631*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(error, ENOTSUP, "connect() == -1 && errno == ENOTSUP");
632*699cd480SApple OSS Distributions } else {
633*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(error, "connect() == 0");
634*699cd480SApple OSS Distributions }
635*699cd480SApple OSS Distributions
636*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_NETIF,
637*699cd480SApple OSS Distributions &enable_netif, sizeof(enable_netif)), EINVAL, NULL);
638*699cd480SApple OSS Distributions
639*699cd480SApple OSS Distributions if (g_is_ipsec_test && channel_count && !enable_netif) {
640*699cd480SApple OSS Distributions /* Connect failed above, so we get EINVAL */
641*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
642*699cd480SApple OSS Distributions &enable_flowswitch, sizeof(enable_flowswitch)), EINVAL, NULL);
643*699cd480SApple OSS Distributions } else {
644*699cd480SApple OSS Distributions if (is_netagent_enabled()) {
645*699cd480SApple OSS Distributions if (enable_netif) {
646*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
647*699cd480SApple OSS Distributions &enable_flowswitch, sizeof(enable_flowswitch)), NULL);
648*699cd480SApple OSS Distributions } else {
649*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
650*699cd480SApple OSS Distributions &enable_flowswitch, sizeof(enable_flowswitch)), ENOENT, NULL);
651*699cd480SApple OSS Distributions }
652*699cd480SApple OSS Distributions } else {
653*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_FLOWSWITCH,
654*699cd480SApple OSS Distributions &enable_flowswitch, sizeof(enable_flowswitch)), ENOTSUP, NULL);
655*699cd480SApple OSS Distributions }
656*699cd480SApple OSS Distributions }
657*699cd480SApple OSS Distributions
658*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(setsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_ENABLE_CHANNEL,
659*699cd480SApple OSS Distributions &channel_count, sizeof(channel_count)), EINVAL, NULL);
660*699cd480SApple OSS Distributions
661*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
662*699cd480SApple OSS Distributions uuid_clear(uuid[i]);
663*699cd480SApple OSS Distributions }
664*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
665*699cd480SApple OSS Distributions if (!channel_count || (g_is_ipsec_test && channel_count && !enable_netif)) {
666*699cd480SApple OSS Distributions /* ipsec doesn't support channels without a netif */
667*699cd480SApple OSS Distributions if (g_is_ipsec_test && channel_count && !enable_netif) {
668*699cd480SApple OSS Distributions /* Unfortunately, the connect incorrectly unwinds the bind if it get an error.
669*699cd480SApple OSS Distributions * until that is fixed, expect EINVAL here
670*699cd480SApple OSS Distributions */
671*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
672*699cd480SApple OSS Distributions uuid, &uuidlen), EINVAL, NULL);
673*699cd480SApple OSS Distributions } else {
674*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
675*699cd480SApple OSS Distributions uuid, &uuidlen), ENXIO, NULL);
676*699cd480SApple OSS Distributions }
677*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)uuidlen, sizeof(uuid_t) * (unsigned long)channel_count, NULL);
678*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
679*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(uuid_is_null(uuid[i]), NULL);
680*699cd480SApple OSS Distributions }
681*699cd480SApple OSS Distributions } else {
682*699cd480SApple OSS Distributions uuidlen = sizeof(uuid_t) * (unsigned int)channel_count;
683*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(getsockopt(tunsock, SYSPROTO_CONTROL, g_OPT_GET_CHANNEL_UUID,
684*699cd480SApple OSS Distributions uuid, &uuidlen), NULL);
685*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG((unsigned long)uuidlen, sizeof(uuid_t) * (unsigned long)channel_count, NULL);
686*699cd480SApple OSS Distributions for (int i = 0; i < channel_count; i++) {
687*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_FALSE(uuid_is_null(uuid[i]), NULL);
688*699cd480SApple OSS Distributions }
689*699cd480SApple OSS Distributions }
690*699cd480SApple OSS Distributions
691*699cd480SApple OSS Distributions check_enables(tunsock, enable_netif, enable_flowswitch, channel_count, uuid);
692*699cd480SApple OSS Distributions
693*699cd480SApple OSS Distributions //T_LOG("Returning tunsock %d", tunsock);
694*699cd480SApple OSS Distributions
695*699cd480SApple OSS Distributions return tunsock;
696*699cd480SApple OSS Distributions }
697*699cd480SApple OSS Distributions
698*699cd480SApple OSS Distributions static int (*create_tunsock)(int enable_netif, int enable_flowswitch, int channel_count, uuid_t uuid[]) = create_tunsock_new;
699*699cd480SApple OSS Distributions
700*699cd480SApple OSS Distributions #if 0
701*699cd480SApple OSS Distributions static void
702*699cd480SApple OSS Distributions ipsec_stats(void)
703*699cd480SApple OSS Distributions {
704*699cd480SApple OSS Distributions struct ifmibdata ifmd;
705*699cd480SApple OSS Distributions
706*699cd480SApple OSS Distributions len = sizeof(struct ifmibdata);
707*699cd480SApple OSS Distributions name[3] = IFMIB_IFDATA;
708*699cd480SApple OSS Distributions name[4] = interesting_row;
709*699cd480SApple OSS Distributions name[5] = IpFDATA_GENERAL;
710*699cd480SApple OSS Distributions if (sysctl(name, 6, &ifmd, &len, (void *)0, 0) == -1) {
711*699cd480SApple OSS Distributions err(1, "sysctl IFDATA_GENERAL %d", interesting_row);
712*699cd480SApple OSS Distributions }
713*699cd480SApple OSS Distributions }
714*699cd480SApple OSS Distributions #endif
715*699cd480SApple OSS Distributions
716*699cd480SApple OSS Distributions static void
permute_enables(void)717*699cd480SApple OSS Distributions permute_enables(void)
718*699cd480SApple OSS Distributions {
719*699cd480SApple OSS Distributions int tunsock;
720*699cd480SApple OSS Distributions T_EXPECT_GE(tunsock = create_tunsock(false, false, false, NULL), 0, NULL);
721*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(tunsock), NULL);
722*699cd480SApple OSS Distributions T_EXPECT_GE(tunsock = create_tunsock(false, false, true, NULL), 0, NULL);
723*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(tunsock), NULL);
724*699cd480SApple OSS Distributions T_EXPECT_GE(tunsock = create_tunsock(false, true, false, NULL), 0, NULL);
725*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(tunsock), NULL);
726*699cd480SApple OSS Distributions T_EXPECT_GE(tunsock = create_tunsock(false, true, true, NULL), 0, NULL);
727*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(tunsock), NULL);
728*699cd480SApple OSS Distributions T_EXPECT_GE(tunsock = create_tunsock(true, false, false, NULL), 0, NULL);
729*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(tunsock), NULL);
730*699cd480SApple OSS Distributions T_EXPECT_GE(tunsock = create_tunsock(true, false, true, NULL), 0, NULL);
731*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(tunsock), NULL);
732*699cd480SApple OSS Distributions T_EXPECT_GE(tunsock = create_tunsock(true, true, false, NULL), 0, NULL);
733*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(tunsock), NULL);
734*699cd480SApple OSS Distributions T_EXPECT_GE(tunsock = create_tunsock(true, true, true, NULL), 0, NULL);
735*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(tunsock), NULL);
736*699cd480SApple OSS Distributions }
737*699cd480SApple OSS Distributions
738*699cd480SApple OSS Distributions T_DECL(ipsec_enables, "This test checks combinations of netif/channel/flowswitch on ipsec")
739*699cd480SApple OSS Distributions {
740*699cd480SApple OSS Distributions setup_ipsec_test();
741*699cd480SApple OSS Distributions permute_enables();
742*699cd480SApple OSS Distributions }
743*699cd480SApple OSS Distributions
744*699cd480SApple OSS Distributions T_DECL(utun_enables, "This test checks combinations of netif/channel/flowswitch on utun")
745*699cd480SApple OSS Distributions {
746*699cd480SApple OSS Distributions setup_utun_test();
747*699cd480SApple OSS Distributions permute_enables();
748*699cd480SApple OSS Distributions }
749*699cd480SApple OSS Distributions
750*699cd480SApple OSS Distributions static int g_tunsock = -1;
751*699cd480SApple OSS Distributions
752*699cd480SApple OSS Distributions static void
cleanup_tunsock(void)753*699cd480SApple OSS Distributions cleanup_tunsock(void)
754*699cd480SApple OSS Distributions {
755*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(g_tunsock), NULL);
756*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(close(g_tunsock), EBADF, NULL);
757*699cd480SApple OSS Distributions if (g_is_ipsec_test) {
758*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(g_pfkeyso), NULL);
759*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(close(g_pfkeyso), EBADF, NULL);
760*699cd480SApple OSS Distributions }
761*699cd480SApple OSS Distributions }
762*699cd480SApple OSS Distributions
763*699cd480SApple OSS Distributions static void
setup_tunsock(int channel_count,uuid_t uuids[])764*699cd480SApple OSS Distributions setup_tunsock(int channel_count, uuid_t uuids[])
765*699cd480SApple OSS Distributions {
766*699cd480SApple OSS Distributions T_ASSERT_GE(g_tunsock = create_tunsock(true, false, channel_count, uuids), 0, NULL);
767*699cd480SApple OSS Distributions T_ATEND(cleanup_tunsock);
768*699cd480SApple OSS Distributions
769*699cd480SApple OSS Distributions char ifname[IFXNAMSIZ];
770*699cd480SApple OSS Distributions tunsock_get_ifname(g_tunsock, ifname);
771*699cd480SApple OSS Distributions
772*699cd480SApple OSS Distributions T_LOG("Created interface %s", ifname);
773*699cd480SApple OSS Distributions
774*699cd480SApple OSS Distributions uint32_t ifaddr = (10 << 24) | ((unsigned)getpid() & 0xffff) << 8 | 160;
775*699cd480SApple OSS Distributions struct in_addr mask;
776*699cd480SApple OSS Distributions g_addr1.s_addr = htonl(ifaddr);
777*699cd480SApple OSS Distributions g_addr2.s_addr = htonl(ifaddr + 1);
778*699cd480SApple OSS Distributions mask.s_addr = htonl(0xffffffff);
779*699cd480SApple OSS Distributions
780*699cd480SApple OSS Distributions ifnet_add_addr4(ifname, &g_addr1, &mask, &g_addr2);
781*699cd480SApple OSS Distributions
782*699cd480SApple OSS Distributions if (g_is_ipsec_test) {
783*699cd480SApple OSS Distributions create_sa(ifname, SADB_ADD, 12345, &g_addr1, &g_addr2);
784*699cd480SApple OSS Distributions create_sa(ifname, SADB_ADD, 12346, &g_addr2, &g_addr1);
785*699cd480SApple OSS Distributions }
786*699cd480SApple OSS Distributions }
787*699cd480SApple OSS Distributions
788*699cd480SApple OSS Distributions T_DECL(setup_ipsec, "This test sets up an ipsec interface")
789*699cd480SApple OSS Distributions {
790*699cd480SApple OSS Distributions setup_ipsec_test();
791*699cd480SApple OSS Distributions setup_tunsock(1, NULL);
792*699cd480SApple OSS Distributions }
793*699cd480SApple OSS Distributions
794*699cd480SApple OSS Distributions T_DECL(setup_utun, "This test sets up a utun interface")
795*699cd480SApple OSS Distributions {
796*699cd480SApple OSS Distributions setup_utun_test();
797*699cd480SApple OSS Distributions setup_tunsock(1, NULL);
798*699cd480SApple OSS Distributions }
799*699cd480SApple OSS Distributions
800*699cd480SApple OSS Distributions static const int SOCKET_TRAFFIC_CLASSES[] = {
801*699cd480SApple OSS Distributions SO_TC_BK_SYS, // BK
802*699cd480SApple OSS Distributions SO_TC_BK, // BK
803*699cd480SApple OSS Distributions SO_TC_BE, // BE
804*699cd480SApple OSS Distributions SO_TC_RD, // BE
805*699cd480SApple OSS Distributions SO_TC_OAM, // BE
806*699cd480SApple OSS Distributions SO_TC_AV, // VI
807*699cd480SApple OSS Distributions SO_TC_RV, // VI
808*699cd480SApple OSS Distributions SO_TC_VI, // VI
809*699cd480SApple OSS Distributions SO_TC_VO, // VO
810*699cd480SApple OSS Distributions SO_TC_CTL, // VO
811*699cd480SApple OSS Distributions };
812*699cd480SApple OSS Distributions
813*699cd480SApple OSS Distributions // this should match ipsec_find_tx_ring_by_svc in ipsec driver
814*699cd480SApple OSS Distributions static const int SOCKET_TC_TO_RING[] = {
815*699cd480SApple OSS Distributions 3,
816*699cd480SApple OSS Distributions 3,
817*699cd480SApple OSS Distributions 2,
818*699cd480SApple OSS Distributions 2,
819*699cd480SApple OSS Distributions 2,
820*699cd480SApple OSS Distributions 1,
821*699cd480SApple OSS Distributions 1,
822*699cd480SApple OSS Distributions 1,
823*699cd480SApple OSS Distributions 0,
824*699cd480SApple OSS Distributions 0,
825*699cd480SApple OSS Distributions };
826*699cd480SApple OSS Distributions
827*699cd480SApple OSS Distributions /* How many sockets map to this ring */
828*699cd480SApple OSS Distributions static const int RING_TO_TC_COUNT[] = {
829*699cd480SApple OSS Distributions 2, 3, 3, 2,
830*699cd480SApple OSS Distributions };
831*699cd480SApple OSS Distributions
832*699cd480SApple OSS Distributions static void
setup_channels_and_rings(int kq,int channel_count,channel_t channels[],channel_ring_t rxrings[],channel_ring_t txrings[],uuid_t uuids[],int cfds[])833*699cd480SApple OSS Distributions setup_channels_and_rings(int kq, int channel_count, channel_t channels[], channel_ring_t rxrings[], channel_ring_t txrings[], uuid_t uuids[], int cfds[])
834*699cd480SApple OSS Distributions {
835*699cd480SApple OSS Distributions setup_tunsock(channel_count, uuids);
836*699cd480SApple OSS Distributions
837*699cd480SApple OSS Distributions #if 0
838*699cd480SApple OSS Distributions // give time to enable a tcpdump if desired
839*699cd480SApple OSS Distributions T_LOG("Sleeping 10");
840*699cd480SApple OSS Distributions sleep(10);
841*699cd480SApple OSS Distributions T_LOG("Done");
842*699cd480SApple OSS Distributions #endif
843*699cd480SApple OSS Distributions
844*699cd480SApple OSS Distributions for (int ri = 0; ri < channel_count; ri++) {
845*699cd480SApple OSS Distributions if (rxrings) {
846*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(channels[ri] = os_channel_create(uuids[ri], 0), NULL);
847*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(rxrings[ri] = os_channel_rx_ring(channels[ri],
848*699cd480SApple OSS Distributions os_channel_ring_id(channels[ri], CHANNEL_FIRST_RX_RING)), NULL);
849*699cd480SApple OSS Distributions }
850*699cd480SApple OSS Distributions if (txrings) {
851*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(channels[ri] = os_channel_create(uuids[ri], 0), NULL);
852*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(rxrings[ri] = os_channel_rx_ring(channels[ri],
853*699cd480SApple OSS Distributions os_channel_ring_id(channels[ri], CHANNEL_FIRST_TX_RING)), NULL);
854*699cd480SApple OSS Distributions }
855*699cd480SApple OSS Distributions
856*699cd480SApple OSS Distributions struct kevent kev;
857*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(cfds[ri] = os_channel_get_fd(channels[ri]), NULL);
858*699cd480SApple OSS Distributions EV_SET(&kev, cfds[ri], EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, (void *)(uintptr_t)ri);
859*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(kevent(kq, &kev, 1, NULL, 0, NULL), NULL);
860*699cd480SApple OSS Distributions }
861*699cd480SApple OSS Distributions }
862*699cd480SApple OSS Distributions
863*699cd480SApple OSS Distributions static void
cleanup_channels_and_rings(int channel_count,channel_t channels[],channel_ring_t rxrings[],channel_ring_t txrings[],uuid_t uuids[])864*699cd480SApple OSS Distributions cleanup_channels_and_rings(int channel_count, channel_t channels[], channel_ring_t rxrings[], channel_ring_t txrings[], uuid_t uuids[])
865*699cd480SApple OSS Distributions {
866*699cd480SApple OSS Distributions for (int ri = 0; ri < channel_count; ri++) {
867*699cd480SApple OSS Distributions if (rxrings) {
868*699cd480SApple OSS Distributions rxrings[ri] = NULL;
869*699cd480SApple OSS Distributions }
870*699cd480SApple OSS Distributions if (txrings) {
871*699cd480SApple OSS Distributions rxrings[ri] = NULL;
872*699cd480SApple OSS Distributions }
873*699cd480SApple OSS Distributions os_channel_destroy(channels[ri]);
874*699cd480SApple OSS Distributions channels[ri] = NULL;
875*699cd480SApple OSS Distributions uuid_clear(uuids[ri]);
876*699cd480SApple OSS Distributions }
877*699cd480SApple OSS Distributions }
878*699cd480SApple OSS Distributions
879*699cd480SApple OSS Distributions static void
setup_sockets(int sockets[SO_TC_MAX],int type)880*699cd480SApple OSS Distributions setup_sockets(int sockets[SO_TC_MAX], int type)
881*699cd480SApple OSS Distributions {
882*699cd480SApple OSS Distributions for (int si = 0; si < SO_TC_MAX; si++) {
883*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(sockets[si] = socket(PF_INET, type, 0), NULL);
884*699cd480SApple OSS Distributions
885*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(setsockopt(sockets[si], SOL_SOCKET,
886*699cd480SApple OSS Distributions SO_TRAFFIC_CLASS, &SOCKET_TRAFFIC_CLASSES[si], sizeof(SOCKET_TRAFFIC_CLASSES[si])), NULL);
887*699cd480SApple OSS Distributions
888*699cd480SApple OSS Distributions // XXX setsockopt(IP_BOUND_IF) here?
889*699cd480SApple OSS Distributions
890*699cd480SApple OSS Distributions struct sockaddr_in sin;
891*699cd480SApple OSS Distributions memset(&sin, 0, sizeof(sin));
892*699cd480SApple OSS Distributions sin.sin_len = sizeof(sin);
893*699cd480SApple OSS Distributions sin.sin_family = AF_INET;
894*699cd480SApple OSS Distributions sin.sin_addr = g_addr1;
895*699cd480SApple OSS Distributions
896*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(bind(sockets[si], (struct sockaddr *)&sin, sizeof(sin)), NULL);
897*699cd480SApple OSS Distributions
898*699cd480SApple OSS Distributions char sbuf[INET6_ADDRSTRLEN];
899*699cd480SApple OSS Distributions inet_ntop(sin.sin_family, &sin.sin_addr.s_addr, sbuf, sizeof(sbuf));
900*699cd480SApple OSS Distributions #if 0
901*699cd480SApple OSS Distributions T_LOG("%s socket %d bound to %s port %d",
902*699cd480SApple OSS Distributions type == SOCK_DGRAM ? "udp" : type == SOCK_STREAM ? "tcp" : "???",
903*699cd480SApple OSS Distributions sockets[si], sbuf, ntohs(sin.sin_port));
904*699cd480SApple OSS Distributions #endif
905*699cd480SApple OSS Distributions setblocking(sockets[si], false);
906*699cd480SApple OSS Distributions }
907*699cd480SApple OSS Distributions }
908*699cd480SApple OSS Distributions
909*699cd480SApple OSS Distributions static void
cleanup_sockets(int sockets[SO_TC_MAX])910*699cd480SApple OSS Distributions cleanup_sockets(int sockets[SO_TC_MAX])
911*699cd480SApple OSS Distributions {
912*699cd480SApple OSS Distributions for (int si = 0; si < SO_TC_MAX; si++) {
913*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(sockets[si]), NULL);
914*699cd480SApple OSS Distributions sockets[si] = -1;
915*699cd480SApple OSS Distributions }
916*699cd480SApple OSS Distributions }
917*699cd480SApple OSS Distributions
918*699cd480SApple OSS Distributions static void
drain_ring(channel_ring_t rxring)919*699cd480SApple OSS Distributions drain_ring(channel_ring_t rxring)
920*699cd480SApple OSS Distributions {
921*699cd480SApple OSS Distributions uint32_t i, sc = os_channel_available_slot_count(rxring);
922*699cd480SApple OSS Distributions channel_slot_t rxprev = NULL;
923*699cd480SApple OSS Distributions for (i = 0; i < sc; i++) {
924*699cd480SApple OSS Distributions slot_prop_t rxprop;
925*699cd480SApple OSS Distributions channel_slot_t rxslot;
926*699cd480SApple OSS Distributions
927*699cd480SApple OSS Distributions memset(&rxprop, 0, sizeof(rxprop));
928*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_NOTNULL(rxslot = os_channel_get_next_slot(rxring, rxprev, &rxprop), NULL);
929*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_NE_UINT(0, rxprop.sp_len, NULL);
930*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL((void *)rxprop.sp_buf_ptr, NULL);
931*699cd480SApple OSS Distributions
932*699cd480SApple OSS Distributions log_hexdump((void *)rxprop.sp_buf_ptr, rxprop.sp_len);
933*699cd480SApple OSS Distributions
934*699cd480SApple OSS Distributions rxprev = rxslot;
935*699cd480SApple OSS Distributions }
936*699cd480SApple OSS Distributions if (sc) {
937*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_ZERO(os_channel_advance_slot(rxring, rxprev), NULL);
938*699cd480SApple OSS Distributions }
939*699cd480SApple OSS Distributions }
940*699cd480SApple OSS Distributions
941*699cd480SApple OSS Distributions static void
send_one_packet(int s,int type)942*699cd480SApple OSS Distributions send_one_packet(int s, int type)
943*699cd480SApple OSS Distributions {
944*699cd480SApple OSS Distributions struct sockaddr_in sin;
945*699cd480SApple OSS Distributions memset(&sin, 0, sizeof(sin));
946*699cd480SApple OSS Distributions sin.sin_len = sizeof(sin);
947*699cd480SApple OSS Distributions sin.sin_family = AF_INET;
948*699cd480SApple OSS Distributions sin.sin_addr = g_addr2;
949*699cd480SApple OSS Distributions sin.sin_port = ntohs(12345);
950*699cd480SApple OSS Distributions
951*699cd480SApple OSS Distributions if (type == SOCK_STREAM) {
952*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_FAILURE(connect(s, (struct sockaddr *)&sin, sizeof(sin)), EINPROGRESS, NULL);
953*699cd480SApple OSS Distributions }
954*699cd480SApple OSS Distributions if (type == SOCK_DGRAM) {
955*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_EQ_LONG((long)sizeof(s), sendto(s, &s, sizeof(s), 0,
956*699cd480SApple OSS Distributions (struct sockaddr *)&sin, sizeof(sin)), NULL);
957*699cd480SApple OSS Distributions }
958*699cd480SApple OSS Distributions }
959*699cd480SApple OSS Distributions
960*699cd480SApple OSS Distributions static void
expect_empty_rings(int channel_count,channel_ring_t rings[])961*699cd480SApple OSS Distributions expect_empty_rings(int channel_count, channel_ring_t rings[])
962*699cd480SApple OSS Distributions {
963*699cd480SApple OSS Distributions /* Check all the rings and make sure there are no packets */
964*699cd480SApple OSS Distributions for (int ri = 0; ri < channel_count; ri++) {
965*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_UINT(0U, os_channel_available_slot_count(rings[ri]), NULL);
966*699cd480SApple OSS Distributions }
967*699cd480SApple OSS Distributions }
968*699cd480SApple OSS Distributions
969*699cd480SApple OSS Distributions static void
xfer_1_packet_singly(int channel_count,int type)970*699cd480SApple OSS Distributions xfer_1_packet_singly(int channel_count, int type)
971*699cd480SApple OSS Distributions {
972*699cd480SApple OSS Distributions uuid_t uuids[channel_count];
973*699cd480SApple OSS Distributions channel_t channels[channel_count];
974*699cd480SApple OSS Distributions int sockets[SO_TC_MAX];
975*699cd480SApple OSS Distributions channel_ring_t rxrings[channel_count];
976*699cd480SApple OSS Distributions int cfds[channel_count];
977*699cd480SApple OSS Distributions int kq;
978*699cd480SApple OSS Distributions
979*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(kq = kqueue(), NULL);
980*699cd480SApple OSS Distributions
981*699cd480SApple OSS Distributions setup_channels_and_rings(kq, channel_count, channels, rxrings, NULL, uuids, cfds);
982*699cd480SApple OSS Distributions
983*699cd480SApple OSS Distributions setup_sockets(sockets, type);
984*699cd480SApple OSS Distributions
985*699cd480SApple OSS Distributions for (int si = 0; si < SO_TC_MAX; si++) {
986*699cd480SApple OSS Distributions expect_empty_rings(channel_count, rxrings);
987*699cd480SApple OSS Distributions
988*699cd480SApple OSS Distributions send_one_packet(sockets[si], type);
989*699cd480SApple OSS Distributions
990*699cd480SApple OSS Distributions int expected_ring = channel_count == 1 ? 0 : SOCKET_TC_TO_RING[si];
991*699cd480SApple OSS Distributions
992*699cd480SApple OSS Distributions /* Wait for the packet delivery and check that it's only one packet and on the correct ring */
993*699cd480SApple OSS Distributions struct kevent kev[channel_count + 1];
994*699cd480SApple OSS Distributions int nev;
995*699cd480SApple OSS Distributions memset(kev, 0, sizeof(kev));
996*699cd480SApple OSS Distributions struct timespec to = { 0, 100 * NSEC_PER_MSEC }; // 100 ms
997*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(nev = kevent(kq, NULL, 0, kev, channel_count + 1, &to), NULL);
998*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_INT(nev, 1, NULL);
999*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_PTR((void *)kev[0].ident, (void *)(uintptr_t)cfds[expected_ring], NULL);
1000*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_PTR(kev[0].udata, (void *)(uintptr_t)expected_ring, NULL);
1001*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_SHORT(kev[0].filter, (short)EVFILT_READ, NULL);
1002*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_FALSE(kev[0].flags & EV_ERROR, NULL);
1003*699cd480SApple OSS Distributions
1004*699cd480SApple OSS Distributions /* Make sure it comes out the expected interface */
1005*699cd480SApple OSS Distributions for (int ri = 0; ri < channel_count; ri++) {
1006*699cd480SApple OSS Distributions errno = 0;
1007*699cd480SApple OSS Distributions
1008*699cd480SApple OSS Distributions uint32_t sc = os_channel_available_slot_count(rxrings[ri]);
1009*699cd480SApple OSS Distributions
1010*699cd480SApple OSS Distributions /* Check that the packet appears only on the expected ring and
1011*699cd480SApple OSS Distributions * is the only packet on the expected ring.
1012*699cd480SApple OSS Distributions */
1013*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_UINT(ri == expected_ring, sc, NULL);
1014*699cd480SApple OSS Distributions
1015*699cd480SApple OSS Distributions if ((ri == expected_ring) == sc) {
1016*699cd480SApple OSS Distributions T_PASS("tc index %d ring %d expected ring %d slot count %u", si, ri, expected_ring, sc);
1017*699cd480SApple OSS Distributions } else {
1018*699cd480SApple OSS Distributions T_FAIL("tc index %d ring %d expected ring %d slot count %u", si, ri, expected_ring, sc);
1019*699cd480SApple OSS Distributions }
1020*699cd480SApple OSS Distributions
1021*699cd480SApple OSS Distributions drain_ring(rxrings[ri]);
1022*699cd480SApple OSS Distributions }
1023*699cd480SApple OSS Distributions }
1024*699cd480SApple OSS Distributions
1025*699cd480SApple OSS Distributions cleanup_sockets(sockets);
1026*699cd480SApple OSS Distributions
1027*699cd480SApple OSS Distributions cleanup_channels_and_rings(channel_count, channels, rxrings, NULL, uuids);
1028*699cd480SApple OSS Distributions
1029*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(kq), NULL);
1030*699cd480SApple OSS Distributions }
1031*699cd480SApple OSS Distributions
1032*699cd480SApple OSS Distributions T_DECL(ipsec35889979u1s, "transfers 1 packet at a time of each sevice class over udp to a single ring")
1033*699cd480SApple OSS Distributions {
1034*699cd480SApple OSS Distributions setup_ipsec_test();
1035*699cd480SApple OSS Distributions xfer_1_packet_singly(1, SOCK_DGRAM);
1036*699cd480SApple OSS Distributions }
1037*699cd480SApple OSS Distributions
1038*699cd480SApple OSS Distributions T_DECL(ipsec35889979u4s, "transfers 1 packet at a time of each sevice class over udp to 4 rings")
1039*699cd480SApple OSS Distributions {
1040*699cd480SApple OSS Distributions setup_ipsec_test();
1041*699cd480SApple OSS Distributions xfer_1_packet_singly(4, SOCK_DGRAM);
1042*699cd480SApple OSS Distributions }
1043*699cd480SApple OSS Distributions
1044*699cd480SApple OSS Distributions T_DECL(ipsec35889979t1s, "transfers 1 packet at a time of each sevice class over tcp to a single ring")
1045*699cd480SApple OSS Distributions {
1046*699cd480SApple OSS Distributions setup_ipsec_test();
1047*699cd480SApple OSS Distributions xfer_1_packet_singly(1, SOCK_STREAM);
1048*699cd480SApple OSS Distributions }
1049*699cd480SApple OSS Distributions
1050*699cd480SApple OSS Distributions
1051*699cd480SApple OSS Distributions T_DECL(ipsec35889979t4s, "transfers 1 packet at a time of each sevice class over tcp to 4 rings",
1052*699cd480SApple OSS Distributions /* This test will fail because tcp syn packets get elevated
1053*699cd480SApple OSS Distributions * due to ack prioritization
1054*699cd480SApple OSS Distributions */
1055*699cd480SApple OSS Distributions T_META_ENABLED(false))
1056*699cd480SApple OSS Distributions {
1057*699cd480SApple OSS Distributions setup_ipsec_test();
1058*699cd480SApple OSS Distributions xfer_1_packet_singly(4, SOCK_STREAM);
1059*699cd480SApple OSS Distributions }
1060*699cd480SApple OSS Distributions
1061*699cd480SApple OSS Distributions static void
xfer_1_packet_together(int channel_count,int type)1062*699cd480SApple OSS Distributions xfer_1_packet_together(int channel_count, int type)
1063*699cd480SApple OSS Distributions {
1064*699cd480SApple OSS Distributions uuid_t uuids[channel_count];
1065*699cd480SApple OSS Distributions channel_t channels[channel_count];
1066*699cd480SApple OSS Distributions int sockets[SO_TC_MAX];
1067*699cd480SApple OSS Distributions channel_ring_t rxrings[channel_count];
1068*699cd480SApple OSS Distributions int cfds[channel_count];
1069*699cd480SApple OSS Distributions int kq;
1070*699cd480SApple OSS Distributions
1071*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(kq = kqueue(), NULL);
1072*699cd480SApple OSS Distributions
1073*699cd480SApple OSS Distributions setup_channels_and_rings(kq, channel_count, channels, rxrings, NULL, uuids, cfds);
1074*699cd480SApple OSS Distributions
1075*699cd480SApple OSS Distributions setup_sockets(sockets, type);
1076*699cd480SApple OSS Distributions
1077*699cd480SApple OSS Distributions for (int si = 0; si < SO_TC_MAX; si++) {
1078*699cd480SApple OSS Distributions expect_empty_rings(channel_count, rxrings);
1079*699cd480SApple OSS Distributions
1080*699cd480SApple OSS Distributions send_one_packet(sockets[si], type);
1081*699cd480SApple OSS Distributions }
1082*699cd480SApple OSS Distributions
1083*699cd480SApple OSS Distributions /* Sleep to make sure all packets get delivered */
1084*699cd480SApple OSS Distributions struct timespec to = { 0, 100 * NSEC_PER_MSEC }; // 100 ms
1085*699cd480SApple OSS Distributions nanosleep(&to, NULL);
1086*699cd480SApple OSS Distributions
1087*699cd480SApple OSS Distributions /* Wait for the packet delivery and check that all rings event */
1088*699cd480SApple OSS Distributions struct kevent kev[channel_count + 1];
1089*699cd480SApple OSS Distributions int nev;
1090*699cd480SApple OSS Distributions memset(kev, 0, sizeof(kev));
1091*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(nev = kevent(kq, NULL, 0, kev, channel_count + 1, &to), NULL);
1092*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_INT(nev, channel_count, NULL);
1093*699cd480SApple OSS Distributions
1094*699cd480SApple OSS Distributions uint32_t found[channel_count];
1095*699cd480SApple OSS Distributions memset(found, 0, sizeof(found));
1096*699cd480SApple OSS Distributions for (int e = 0; e < nev; e++) {
1097*699cd480SApple OSS Distributions T_LOG("kevent %lu filter 0x%4x flags 0x%04x fflags 0x%08x data %"PRIdPTR" udata %p",
1098*699cd480SApple OSS Distributions kev[e].ident, kev[e].filter, kev[e].flags, kev[e].fflags, kev[e].data, kev[e].udata);
1099*699cd480SApple OSS Distributions
1100*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_GE_PTR(kev[e].udata, (void *)0, NULL);
1101*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_LT_PTR(kev[e].udata, (void *)(intptr_t)channel_count, NULL);
1102*699cd480SApple OSS Distributions int ri = (int)kev[e].udata;
1103*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_UINT(found[ri], 0U, NULL);
1104*699cd480SApple OSS Distributions
1105*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_ULONG(kev[e].ident, (uintptr_t)cfds[ri], NULL);
1106*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_SHORT(kev[e].filter, (short)EVFILT_READ, NULL);
1107*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_FALSE(kev[e].flags & EV_ERROR, NULL);
1108*699cd480SApple OSS Distributions
1109*699cd480SApple OSS Distributions if (channel_count == 1) {
1110*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_LONG(kev[e].data, (long)SO_TC_MAX, NULL);
1111*699cd480SApple OSS Distributions } else {
1112*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_EQ_LONG(kev[e].data, (long)RING_TO_TC_COUNT[ri], NULL);
1113*699cd480SApple OSS Distributions }
1114*699cd480SApple OSS Distributions
1115*699cd480SApple OSS Distributions found[ri] += (uint32_t)kev[e].data;
1116*699cd480SApple OSS Distributions }
1117*699cd480SApple OSS Distributions /* Check that something came out of all rings */
1118*699cd480SApple OSS Distributions for (int ri = 0; ri < channel_count; ri++) {
1119*699cd480SApple OSS Distributions T_QUIET; T_EXPECT_NE_UINT(found[ri], 0U, NULL);
1120*699cd480SApple OSS Distributions }
1121*699cd480SApple OSS Distributions
1122*699cd480SApple OSS Distributions /* Make sure it comes out the expected interface */
1123*699cd480SApple OSS Distributions for (int ri = 0; ri < channel_count; ri++) {
1124*699cd480SApple OSS Distributions uint32_t sc = os_channel_available_slot_count(rxrings[ri]);
1125*699cd480SApple OSS Distributions if (channel_count == 1) {
1126*699cd480SApple OSS Distributions if (sc == SO_TC_MAX) {
1127*699cd480SApple OSS Distributions T_PASS("ring %d got %"PRIu32" slots expecting %"PRIu32"", ri, sc, SO_TC_MAX);
1128*699cd480SApple OSS Distributions } else {
1129*699cd480SApple OSS Distributions T_FAIL("ring %d got %"PRIu32" slots expecting %"PRIu32"", ri, sc, SO_TC_MAX);
1130*699cd480SApple OSS Distributions }
1131*699cd480SApple OSS Distributions } else {
1132*699cd480SApple OSS Distributions if (sc == (uint32_t)RING_TO_TC_COUNT[ri]) {
1133*699cd480SApple OSS Distributions T_PASS("ring %d got %"PRIu32" slots expecting %"PRIu32"", ri, sc, (uint32_t)RING_TO_TC_COUNT[ri]);
1134*699cd480SApple OSS Distributions } else {
1135*699cd480SApple OSS Distributions T_FAIL("ring %d got %"PRIu32" slots expecting %"PRIu32"", ri, sc, (uint32_t)RING_TO_TC_COUNT[ri]);
1136*699cd480SApple OSS Distributions }
1137*699cd480SApple OSS Distributions }
1138*699cd480SApple OSS Distributions
1139*699cd480SApple OSS Distributions drain_ring(rxrings[ri]);
1140*699cd480SApple OSS Distributions }
1141*699cd480SApple OSS Distributions
1142*699cd480SApple OSS Distributions cleanup_sockets(sockets);
1143*699cd480SApple OSS Distributions
1144*699cd480SApple OSS Distributions cleanup_channels_and_rings(channel_count, channels, rxrings, NULL, uuids);
1145*699cd480SApple OSS Distributions
1146*699cd480SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(kq), NULL);
1147*699cd480SApple OSS Distributions }
1148*699cd480SApple OSS Distributions
1149*699cd480SApple OSS Distributions T_DECL(ipsec35889979u1m, "transfers 1 packet together of each sevice class over udp to a single ring")
1150*699cd480SApple OSS Distributions {
1151*699cd480SApple OSS Distributions setup_ipsec_test();
1152*699cd480SApple OSS Distributions xfer_1_packet_together(1, SOCK_DGRAM);
1153*699cd480SApple OSS Distributions }
1154*699cd480SApple OSS Distributions
1155*699cd480SApple OSS Distributions T_DECL(ipsec35889979u4m, "transfers 1 packet together of each sevice class over udp to 4 rings")
1156*699cd480SApple OSS Distributions {
1157*699cd480SApple OSS Distributions setup_ipsec_test();
1158*699cd480SApple OSS Distributions xfer_1_packet_together(4, SOCK_DGRAM);
1159*699cd480SApple OSS Distributions }
1160*699cd480SApple OSS Distributions
1161*699cd480SApple OSS Distributions T_DECL(ipsec35889979t1m, "transfers 1 packet together of each sevice class over tcp to a single ring")
1162*699cd480SApple OSS Distributions {
1163*699cd480SApple OSS Distributions setup_ipsec_test();
1164*699cd480SApple OSS Distributions xfer_1_packet_together(1, SOCK_STREAM);
1165*699cd480SApple OSS Distributions }
1166*699cd480SApple OSS Distributions
1167*699cd480SApple OSS Distributions T_DECL(ipsec35889979t4m, "transfers 1 packet together of each sevice class over tcp to 4 rings",
1168*699cd480SApple OSS Distributions /* This test will fail because tcp syn packets get elevated
1169*699cd480SApple OSS Distributions * due to ack prioritization
1170*699cd480SApple OSS Distributions */
1171*699cd480SApple OSS Distributions T_META_ENABLED(false))
1172*699cd480SApple OSS Distributions {
1173*699cd480SApple OSS Distributions setup_ipsec_test();
1174*699cd480SApple OSS Distributions xfer_1_packet_together(4, SOCK_STREAM);
1175*699cd480SApple OSS Distributions }
1176