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