1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions * Copyright (c) 2023 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions *
4*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions *
6*2c2f96dcSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions *
15*2c2f96dcSApple OSS Distributions * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions *
18*2c2f96dcSApple OSS Distributions * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions * limitations under the License.
25*2c2f96dcSApple OSS Distributions *
26*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions */
28*2c2f96dcSApple OSS Distributions
29*2c2f96dcSApple OSS Distributions #include <sys/fcntl.h>
30*2c2f96dcSApple OSS Distributions #include <sys/socket.h>
31*2c2f96dcSApple OSS Distributions #include <net/if.h>
32*2c2f96dcSApple OSS Distributions #include <netinet/in.h>
33*2c2f96dcSApple OSS Distributions #include <netinet/tcp.h>
34*2c2f96dcSApple OSS Distributions #include <arpa/inet.h>
35*2c2f96dcSApple OSS Distributions
36*2c2f96dcSApple OSS Distributions #include <darwintest.h>
37*2c2f96dcSApple OSS Distributions #include <pthread.h>
38*2c2f96dcSApple OSS Distributions #include <stdbool.h>
39*2c2f96dcSApple OSS Distributions #include <string.h>
40*2c2f96dcSApple OSS Distributions #include <unistd.h>
41*2c2f96dcSApple OSS Distributions
42*2c2f96dcSApple OSS Distributions T_GLOBAL_META(
43*2c2f96dcSApple OSS Distributions T_META_NAMESPACE("xnu.net"),
44*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
45*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("networking"),
46*2c2f96dcSApple OSS Distributions T_META_CHECK_LEAKS(false));
47*2c2f96dcSApple OSS Distributions
48*2c2f96dcSApple OSS Distributions static int fd = -1;
49*2c2f96dcSApple OSS Distributions static bool finished = false;
50*2c2f96dcSApple OSS Distributions static bool is_tcp = false;
51*2c2f96dcSApple OSS Distributions
52*2c2f96dcSApple OSS Distributions static void
init_sin6_address(struct sockaddr_in6 * sin6)53*2c2f96dcSApple OSS Distributions init_sin6_address(struct sockaddr_in6 *sin6)
54*2c2f96dcSApple OSS Distributions {
55*2c2f96dcSApple OSS Distributions memset(sin6, 0, sizeof(struct sockaddr_in6));
56*2c2f96dcSApple OSS Distributions sin6->sin6_len = sizeof(struct sockaddr_in6);
57*2c2f96dcSApple OSS Distributions sin6->sin6_family = AF_INET6;
58*2c2f96dcSApple OSS Distributions }
59*2c2f96dcSApple OSS Distributions
60*2c2f96dcSApple OSS Distributions static void *
racer(void * arg)61*2c2f96dcSApple OSS Distributions racer(void *arg)
62*2c2f96dcSApple OSS Distributions {
63*2c2f96dcSApple OSS Distributions #pragma unused(arg)
64*2c2f96dcSApple OSS Distributions struct sockaddr_in6 sin6 = {};
65*2c2f96dcSApple OSS Distributions
66*2c2f96dcSApple OSS Distributions init_sin6_address(&sin6);
67*2c2f96dcSApple OSS Distributions T_ASSERT_EQ(inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6.sin6_addr), 1, NULL);
68*2c2f96dcSApple OSS Distributions
69*2c2f96dcSApple OSS Distributions while (finished == false) {
70*2c2f96dcSApple OSS Distributions (void)bind(fd, (struct sockaddr*)&sin6, sin6.sin6_len);
71*2c2f96dcSApple OSS Distributions }
72*2c2f96dcSApple OSS Distributions return NULL;
73*2c2f96dcSApple OSS Distributions }
74*2c2f96dcSApple OSS Distributions
75*2c2f96dcSApple OSS Distributions static void *
leader(void * arg)76*2c2f96dcSApple OSS Distributions leader(void *arg)
77*2c2f96dcSApple OSS Distributions {
78*2c2f96dcSApple OSS Distributions #pragma unused(arg)
79*2c2f96dcSApple OSS Distributions struct sockaddr_in6 sin6 = {};
80*2c2f96dcSApple OSS Distributions
81*2c2f96dcSApple OSS Distributions init_sin6_address(&sin6);
82*2c2f96dcSApple OSS Distributions T_ASSERT_EQ(inet_pton(AF_INET6, "::1", &sin6.sin6_addr), 1, NULL);
83*2c2f96dcSApple OSS Distributions
84*2c2f96dcSApple OSS Distributions while (finished == false) {
85*2c2f96dcSApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(fd = socket(AF_INET6, is_tcp ? SOCK_STREAM : SOCK_DGRAM, 0), "socket");
86*2c2f96dcSApple OSS Distributions
87*2c2f96dcSApple OSS Distributions (void)bind(fd, (struct sockaddr*)&sin6, sin6.sin6_len);
88*2c2f96dcSApple OSS Distributions
89*2c2f96dcSApple OSS Distributions close(fd);
90*2c2f96dcSApple OSS Distributions }
91*2c2f96dcSApple OSS Distributions return NULL;
92*2c2f96dcSApple OSS Distributions }
93*2c2f96dcSApple OSS Distributions
94*2c2f96dcSApple OSS Distributions T_DECL(ipv6_tcp_bind_race, "race bind calls with TCP sockets")
95*2c2f96dcSApple OSS Distributions {
96*2c2f96dcSApple OSS Distributions pthread_t runner1;
97*2c2f96dcSApple OSS Distributions if (pthread_create(&runner1, NULL, leader, NULL)) {
98*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("pthread_create failed");
99*2c2f96dcSApple OSS Distributions }
100*2c2f96dcSApple OSS Distributions
101*2c2f96dcSApple OSS Distributions pthread_t runner2;
102*2c2f96dcSApple OSS Distributions if (pthread_create(&runner2, NULL, racer, NULL)) {
103*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("pthread_create failed");
104*2c2f96dcSApple OSS Distributions }
105*2c2f96dcSApple OSS Distributions
106*2c2f96dcSApple OSS Distributions sleep(5);
107*2c2f96dcSApple OSS Distributions
108*2c2f96dcSApple OSS Distributions finished = true;
109*2c2f96dcSApple OSS Distributions
110*2c2f96dcSApple OSS Distributions pthread_join(runner1, 0);
111*2c2f96dcSApple OSS Distributions pthread_join(runner2, 0);
112*2c2f96dcSApple OSS Distributions }
113*2c2f96dcSApple OSS Distributions
114*2c2f96dcSApple OSS Distributions T_DECL(ipv6_udp_bind_race, "race bind calls with UDP sockets")
115*2c2f96dcSApple OSS Distributions {
116*2c2f96dcSApple OSS Distributions is_tcp = false;
117*2c2f96dcSApple OSS Distributions
118*2c2f96dcSApple OSS Distributions pthread_t runner1;
119*2c2f96dcSApple OSS Distributions if (pthread_create(&runner1, NULL, leader, NULL)) {
120*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("pthread_create failed");
121*2c2f96dcSApple OSS Distributions }
122*2c2f96dcSApple OSS Distributions
123*2c2f96dcSApple OSS Distributions pthread_t runner2;
124*2c2f96dcSApple OSS Distributions if (pthread_create(&runner2, NULL, racer, NULL)) {
125*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("pthread_create failed");
126*2c2f96dcSApple OSS Distributions }
127*2c2f96dcSApple OSS Distributions
128*2c2f96dcSApple OSS Distributions sleep(5);
129*2c2f96dcSApple OSS Distributions
130*2c2f96dcSApple OSS Distributions finished = true;
131*2c2f96dcSApple OSS Distributions
132*2c2f96dcSApple OSS Distributions pthread_join(runner1, 0);
133*2c2f96dcSApple OSS Distributions pthread_join(runner2, 0);
134*2c2f96dcSApple OSS Distributions }
135