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