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