1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions * Copyright (c) 2021-2022 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions *
4*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions *
6*5e3eaea3SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions *
15*5e3eaea3SApple OSS Distributions * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions *
18*5e3eaea3SApple OSS Distributions * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions * limitations under the License.
25*5e3eaea3SApple OSS Distributions *
26*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions */
28*5e3eaea3SApple OSS Distributions
29*5e3eaea3SApple OSS Distributions #define __APPLE_USE_RFC_3542 1
30*5e3eaea3SApple OSS Distributions #include <stdio.h>
31*5e3eaea3SApple OSS Distributions #include <stdlib.h>
32*5e3eaea3SApple OSS Distributions #include <string.h>
33*5e3eaea3SApple OSS Distributions #include <sys/types.h>
34*5e3eaea3SApple OSS Distributions #include <sys/errno.h>
35*5e3eaea3SApple OSS Distributions #include <sys/socket.h>
36*5e3eaea3SApple OSS Distributions #include <sys/sysctl.h>
37*5e3eaea3SApple OSS Distributions #include <sys/event.h>
38*5e3eaea3SApple OSS Distributions #include <sys/time.h>
39*5e3eaea3SApple OSS Distributions #include <arpa/inet.h>
40*5e3eaea3SApple OSS Distributions #include <netdb.h>
41*5e3eaea3SApple OSS Distributions #include <semaphore.h>
42*5e3eaea3SApple OSS Distributions #include <darwintest.h>
43*5e3eaea3SApple OSS Distributions
44*5e3eaea3SApple OSS Distributions #define LOGSOCKOPT(x) { \
45*5e3eaea3SApple OSS Distributions int __retv = (x); \
46*5e3eaea3SApple OSS Distributions if (__retv == -1) { \
47*5e3eaea3SApple OSS Distributions T_LOG("[:(] SETSOCKOPT FAILED: %s = -1, errno = %d\n", #x, errno); \
48*5e3eaea3SApple OSS Distributions } \
49*5e3eaea3SApple OSS Distributions }
50*5e3eaea3SApple OSS Distributions
51*5e3eaea3SApple OSS Distributions #define LISTENER_PORT 9999
52*5e3eaea3SApple OSS Distributions
53*5e3eaea3SApple OSS Distributions void do_kqueue(int kq, int sockfd);
54*5e3eaea3SApple OSS Distributions void handle_recv(struct msghdr *recvmsghdr, int packet_len);
55*5e3eaea3SApple OSS Distributions
56*5e3eaea3SApple OSS Distributions sem_t mutex;
57*5e3eaea3SApple OSS Distributions
58*5e3eaea3SApple OSS Distributions static void *
listener_thread(void * unused)59*5e3eaea3SApple OSS Distributions listener_thread(void *unused)
60*5e3eaea3SApple OSS Distributions {
61*5e3eaea3SApple OSS Distributions int sockfd;
62*5e3eaea3SApple OSS Distributions int clientlen;
63*5e3eaea3SApple OSS Distributions struct sockaddr_in serveraddr;
64*5e3eaea3SApple OSS Distributions struct sockaddr_in clientaddr;
65*5e3eaea3SApple OSS Distributions struct hostent *hostp;
66*5e3eaea3SApple OSS Distributions char *buf;
67*5e3eaea3SApple OSS Distributions char *hostaddrp;
68*5e3eaea3SApple OSS Distributions int optval;
69*5e3eaea3SApple OSS Distributions int n;
70*5e3eaea3SApple OSS Distributions
71*5e3eaea3SApple OSS Distributions sockfd = socket(AF_INET, SOCK_DGRAM, 0);
72*5e3eaea3SApple OSS Distributions if (sockfd < 0) {
73*5e3eaea3SApple OSS Distributions T_SKIP("cannot open listener sock");
74*5e3eaea3SApple OSS Distributions }
75*5e3eaea3SApple OSS Distributions
76*5e3eaea3SApple OSS Distributions bzero((char *)&serveraddr, sizeof(serveraddr));
77*5e3eaea3SApple OSS Distributions serveraddr.sin_family = AF_INET;
78*5e3eaea3SApple OSS Distributions serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
79*5e3eaea3SApple OSS Distributions serveraddr.sin_port = htons((unsigned short)LISTENER_PORT);
80*5e3eaea3SApple OSS Distributions serveraddr.sin_len = sizeof(struct sockaddr_in);
81*5e3eaea3SApple OSS Distributions
82*5e3eaea3SApple OSS Distributions if (bind(sockfd, (struct sockaddr *)&serveraddr,
83*5e3eaea3SApple OSS Distributions sizeof(serveraddr)) < 0) {
84*5e3eaea3SApple OSS Distributions T_SKIP("ERROR on binding");
85*5e3eaea3SApple OSS Distributions }
86*5e3eaea3SApple OSS Distributions
87*5e3eaea3SApple OSS Distributions clientlen = sizeof(clientaddr);
88*5e3eaea3SApple OSS Distributions buf = malloc(5);
89*5e3eaea3SApple OSS Distributions T_LOG("listener started \n");
90*5e3eaea3SApple OSS Distributions sem_post(&mutex);
91*5e3eaea3SApple OSS Distributions
92*5e3eaea3SApple OSS Distributions while (1) {
93*5e3eaea3SApple OSS Distributions n = recvfrom(sockfd, buf, 5, 0,
94*5e3eaea3SApple OSS Distributions (struct sockaddr *)&clientaddr, &clientlen);
95*5e3eaea3SApple OSS Distributions if (n < 0) {
96*5e3eaea3SApple OSS Distributions T_SKIP("ERROR in recvfrom");
97*5e3eaea3SApple OSS Distributions }
98*5e3eaea3SApple OSS Distributions T_LOG("received from sender: %.5s", buf);
99*5e3eaea3SApple OSS Distributions
100*5e3eaea3SApple OSS Distributions
101*5e3eaea3SApple OSS Distributions hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
102*5e3eaea3SApple OSS Distributions sizeof(clientaddr.sin_addr.s_addr),
103*5e3eaea3SApple OSS Distributions AF_INET);
104*5e3eaea3SApple OSS Distributions if (hostp == NULL) {
105*5e3eaea3SApple OSS Distributions T_SKIP("ERROR on gethostbyaddr");
106*5e3eaea3SApple OSS Distributions }
107*5e3eaea3SApple OSS Distributions hostaddrp = inet_ntoa(clientaddr.sin_addr);
108*5e3eaea3SApple OSS Distributions if (hostaddrp == NULL) {
109*5e3eaea3SApple OSS Distributions T_SKIP("ERROR on inet_ntoa\n");
110*5e3eaea3SApple OSS Distributions }
111*5e3eaea3SApple OSS Distributions
112*5e3eaea3SApple OSS Distributions n = sendto(sockfd, buf, n, 0,
113*5e3eaea3SApple OSS Distributions (struct sockaddr *)&clientaddr, clientlen);
114*5e3eaea3SApple OSS Distributions if (n < 0) {
115*5e3eaea3SApple OSS Distributions T_SKIP("ERROR in sendto");
116*5e3eaea3SApple OSS Distributions }
117*5e3eaea3SApple OSS Distributions T_LOG("sent response to sender");
118*5e3eaea3SApple OSS Distributions }
119*5e3eaea3SApple OSS Distributions }
120*5e3eaea3SApple OSS Distributions
121*5e3eaea3SApple OSS Distributions T_DECL(v4mappedv6_recvpktinfo, "Test setting and using IPV6_RECVPKTINFO on a v4-mapped-v6 address socket")
122*5e3eaea3SApple OSS Distributions {
123*5e3eaea3SApple OSS Distributions pthread_t t;
124*5e3eaea3SApple OSS Distributions sem_init(&mutex, 0, 1);
125*5e3eaea3SApple OSS Distributions int error = pthread_create(&t, NULL, &listener_thread, NULL);
126*5e3eaea3SApple OSS Distributions if (error != 0) {
127*5e3eaea3SApple OSS Distributions T_SKIP("cannot start listener thread");
128*5e3eaea3SApple OSS Distributions }
129*5e3eaea3SApple OSS Distributions
130*5e3eaea3SApple OSS Distributions sem_wait(&mutex);
131*5e3eaea3SApple OSS Distributions
132*5e3eaea3SApple OSS Distributions struct sockaddr_in6 local_addr = { .sin6_family = AF_INET6, .sin6_len = sizeof(struct sockaddr_in6) };
133*5e3eaea3SApple OSS Distributions struct sockaddr_in6 remote_addr = { };
134*5e3eaea3SApple OSS Distributions
135*5e3eaea3SApple OSS Distributions int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
136*5e3eaea3SApple OSS Distributions if (sockfd == -1) {
137*5e3eaea3SApple OSS Distributions T_SKIP("Couldn't create socket. errno = %d\n", errno);
138*5e3eaea3SApple OSS Distributions }
139*5e3eaea3SApple OSS Distributions
140*5e3eaea3SApple OSS Distributions int Option = 0;
141*5e3eaea3SApple OSS Distributions LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &Option, sizeof(Option)));
142*5e3eaea3SApple OSS Distributions
143*5e3eaea3SApple OSS Distributions Option = 1;
144*5e3eaea3SApple OSS Distributions LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &Option, sizeof(Option)));
145*5e3eaea3SApple OSS Distributions
146*5e3eaea3SApple OSS Distributions int ret = bind(sockfd, (const struct sockaddr *)&local_addr, sizeof(local_addr));
147*5e3eaea3SApple OSS Distributions if (ret == -1) {
148*5e3eaea3SApple OSS Distributions T_SKIP("Couldn't bind. errno = %d\n", ret);
149*5e3eaea3SApple OSS Distributions }
150*5e3eaea3SApple OSS Distributions
151*5e3eaea3SApple OSS Distributions struct in_addr v4addr = {
152*5e3eaea3SApple OSS Distributions .s_addr = htonl(INADDR_LOOPBACK)
153*5e3eaea3SApple OSS Distributions };
154*5e3eaea3SApple OSS Distributions
155*5e3eaea3SApple OSS Distributions // map v4 to v6
156*5e3eaea3SApple OSS Distributions remote_addr.sin6_family = AF_INET6;
157*5e3eaea3SApple OSS Distributions remote_addr.sin6_port = htons(LISTENER_PORT);
158*5e3eaea3SApple OSS Distributions remote_addr.sin6_len = sizeof(struct sockaddr_in6);
159*5e3eaea3SApple OSS Distributions memset(&(remote_addr.sin6_addr.s6_addr[10]), 0xff, 2);
160*5e3eaea3SApple OSS Distributions memcpy(&(remote_addr.sin6_addr.s6_addr[12]), &v4addr.s_addr, 4);
161*5e3eaea3SApple OSS Distributions
162*5e3eaea3SApple OSS Distributions // now remote_addr is a v4-mapped v6 address
163*5e3eaea3SApple OSS Distributions ret = connect(sockfd, (const struct sockaddr *)&remote_addr, sizeof(remote_addr));
164*5e3eaea3SApple OSS Distributions
165*5e3eaea3SApple OSS Distributions if (ret == -1) {
166*5e3eaea3SApple OSS Distributions T_SKIP("Couldn't connect. ret = %d, errno = %d\n", ret, errno);
167*5e3eaea3SApple OSS Distributions }
168*5e3eaea3SApple OSS Distributions
169*5e3eaea3SApple OSS Distributions T_LOG("Socket established. Binded & connected.\n");
170*5e3eaea3SApple OSS Distributions
171*5e3eaea3SApple OSS Distributions int kq = kqueue();
172*5e3eaea3SApple OSS Distributions
173*5e3eaea3SApple OSS Distributions if (kq == -1) {
174*5e3eaea3SApple OSS Distributions T_SKIP("Failed to kqueue. errno = %d\n", errno);
175*5e3eaea3SApple OSS Distributions }
176*5e3eaea3SApple OSS Distributions
177*5e3eaea3SApple OSS Distributions // add fd to kqueue
178*5e3eaea3SApple OSS Distributions struct kevent evSet = { };
179*5e3eaea3SApple OSS Distributions EV_SET(&evSet, sockfd, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (void *)NULL);
180*5e3eaea3SApple OSS Distributions if (kevent(kq, &evSet, 1, NULL, 0, NULL) < 0) {
181*5e3eaea3SApple OSS Distributions T_SKIP("kevent failed??\n");
182*5e3eaea3SApple OSS Distributions }
183*5e3eaea3SApple OSS Distributions
184*5e3eaea3SApple OSS Distributions // send pkt to notify remote
185*5e3eaea3SApple OSS Distributions sendto(sockfd, "ping", 4, 0, NULL, 0);
186*5e3eaea3SApple OSS Distributions do_kqueue(kq, sockfd);
187*5e3eaea3SApple OSS Distributions }
188*5e3eaea3SApple OSS Distributions
189*5e3eaea3SApple OSS Distributions void
do_kqueue(int kq,int sockfd)190*5e3eaea3SApple OSS Distributions do_kqueue(int kq, int sockfd)
191*5e3eaea3SApple OSS Distributions {
192*5e3eaea3SApple OSS Distributions int ret = 0;
193*5e3eaea3SApple OSS Distributions
194*5e3eaea3SApple OSS Distributions char control_space[CMSG_SPACE(8192)] = {};
195*5e3eaea3SApple OSS Distributions struct msghdr recvmsghdr = {};
196*5e3eaea3SApple OSS Distributions char packet_space[1500] = {};
197*5e3eaea3SApple OSS Distributions
198*5e3eaea3SApple OSS Distributions struct iovec recv_iov;
199*5e3eaea3SApple OSS Distributions recv_iov.iov_len = 1200; // just a safe buffer from the 1500 mut
200*5e3eaea3SApple OSS Distributions recv_iov.iov_base = &packet_space;
201*5e3eaea3SApple OSS Distributions
202*5e3eaea3SApple OSS Distributions recvmsghdr.msg_iov = &recv_iov;
203*5e3eaea3SApple OSS Distributions recvmsghdr.msg_iovlen = 1;
204*5e3eaea3SApple OSS Distributions recvmsghdr.msg_control = &control_space;
205*5e3eaea3SApple OSS Distributions recvmsghdr.msg_controllen = sizeof(control_space);
206*5e3eaea3SApple OSS Distributions recvmsghdr.msg_flags = 0;
207*5e3eaea3SApple OSS Distributions
208*5e3eaea3SApple OSS Distributions struct kevent evSet;
209*5e3eaea3SApple OSS Distributions struct kevent evList[32];
210*5e3eaea3SApple OSS Distributions
211*5e3eaea3SApple OSS Distributions struct timespec timeout;
212*5e3eaea3SApple OSS Distributions timeout.tv_sec = 10;
213*5e3eaea3SApple OSS Distributions timeout.tv_nsec = 0;
214*5e3eaea3SApple OSS Distributions
215*5e3eaea3SApple OSS Distributions int nev = kevent(kq, NULL, 0, evList, 32, &timeout);
216*5e3eaea3SApple OSS Distributions
217*5e3eaea3SApple OSS Distributions for (int i = 0; i < nev; i++) {
218*5e3eaea3SApple OSS Distributions T_LOG("[kevent] ident = %zd, filter = %hu, flags = %hd, fflags = %d, data = %zd, udata = %p\n",
219*5e3eaea3SApple OSS Distributions evList[i].ident, evList[i].filter, evList[i].flags, evList[i].fflags, evList[i].data, evList[i].udata);
220*5e3eaea3SApple OSS Distributions
221*5e3eaea3SApple OSS Distributions if (evList[i].filter == EVFILT_READ) {
222*5e3eaea3SApple OSS Distributions ret = recvmsg(sockfd, &recvmsghdr, 0);
223*5e3eaea3SApple OSS Distributions if (ret != -1) {
224*5e3eaea3SApple OSS Distributions handle_recv(&recvmsghdr, ret);
225*5e3eaea3SApple OSS Distributions } else {
226*5e3eaea3SApple OSS Distributions T_SKIP("sender recv failed");
227*5e3eaea3SApple OSS Distributions }
228*5e3eaea3SApple OSS Distributions return;
229*5e3eaea3SApple OSS Distributions }
230*5e3eaea3SApple OSS Distributions }
231*5e3eaea3SApple OSS Distributions
232*5e3eaea3SApple OSS Distributions T_SKIP("timeout after 10s");
233*5e3eaea3SApple OSS Distributions }
234*5e3eaea3SApple OSS Distributions
235*5e3eaea3SApple OSS Distributions void
handle_recv(struct msghdr * recvmsghdr,int packet_len)236*5e3eaea3SApple OSS Distributions handle_recv(struct msghdr *recvmsghdr, int packet_len)
237*5e3eaea3SApple OSS Distributions {
238*5e3eaea3SApple OSS Distributions T_LOG("[!!] Received %d bytes...", packet_len);
239*5e3eaea3SApple OSS Distributions
240*5e3eaea3SApple OSS Distributions for (int i = 0; i < MIN(packet_len, 5); i++) {
241*5e3eaea3SApple OSS Distributions T_LOG("%02x ", ((char *)recvmsghdr->msg_iov->iov_base)[i]);
242*5e3eaea3SApple OSS Distributions }
243*5e3eaea3SApple OSS Distributions
244*5e3eaea3SApple OSS Distributions T_LOG("\n");
245*5e3eaea3SApple OSS Distributions
246*5e3eaea3SApple OSS Distributions struct cmsghdr *cmsg;
247*5e3eaea3SApple OSS Distributions int received_cmsg_header = 0;
248*5e3eaea3SApple OSS Distributions for (cmsg = CMSG_FIRSTHDR(recvmsghdr); cmsg != NULL; cmsg = CMSG_NXTHDR(recvmsghdr, cmsg)) {
249*5e3eaea3SApple OSS Distributions // this never gets hit
250*5e3eaea3SApple OSS Distributions T_LOG("cmsg_level: %d, cmsg_type: %d\n", cmsg->cmsg_level, cmsg->cmsg_type);
251*5e3eaea3SApple OSS Distributions received_cmsg_header = 1;
252*5e3eaea3SApple OSS Distributions }
253*5e3eaea3SApple OSS Distributions
254*5e3eaea3SApple OSS Distributions T_EXPECT_TRUE(received_cmsg_header == 1, "recved cmsg hdr");
255*5e3eaea3SApple OSS Distributions T_PASS("Recvd cmsg hdr");
256*5e3eaea3SApple OSS Distributions }
257