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