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