1 /*
2 * Copyright (c) 2021 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
99 hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
100 sizeof(clientaddr.sin_addr.s_addr),
101 AF_INET);
102 if (hostp == NULL) {
103 T_SKIP("ERROR on gethostbyaddr");
104 }
105 hostaddrp = inet_ntoa(clientaddr.sin_addr);
106 if (hostaddrp == NULL) {
107 T_SKIP("ERROR on inet_ntoa\n");
108 }
109
110 n = sendto(sockfd, buf, n, 0,
111 (struct sockaddr *)&clientaddr, clientlen);
112 if (n < 0) {
113 T_SKIP("ERROR in sendto");
114 }
115 }
116 }
117
118 T_DECL(v4mappedv6_recvpktinfo, "Test setting and using IPV6_RECVPKTINFO on a v4-mapped-v6 address socket")
119 {
120 pthread_t t;
121 sem_init(&mutex, 0, 1);
122 int error = pthread_create(&t, NULL, &listener_thread, NULL);
123 if (error != 0) {
124 T_SKIP("cannot start listener thread");
125 }
126
127 sem_wait(&mutex);
128
129 struct sockaddr_in6 local_addr = { .sin6_family = AF_INET6, .sin6_len = sizeof(struct sockaddr_in6) };
130 struct sockaddr_in6 remote_addr = { };
131
132 int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
133 if (sockfd == -1) {
134 T_SKIP("Couldn't create socket. errno = %d\n", errno);
135 }
136
137 int Option = 0;
138 LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &Option, sizeof(Option)));
139
140 Option = 1;
141 LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &Option, sizeof(Option)));
142
143 int ret = bind(sockfd, (const struct sockaddr *)&local_addr, sizeof(local_addr));
144 if (ret == -1) {
145 T_SKIP("Couldn't bind. errno = %d\n", ret);
146 }
147
148 struct in_addr v4addr = {
149 .s_addr = htonl(INADDR_LOOPBACK)
150 };
151
152 // map v4 to v6
153 remote_addr.sin6_family = AF_INET6;
154 remote_addr.sin6_port = htons(LISTENER_PORT);
155 remote_addr.sin6_len = sizeof(struct sockaddr_in6);
156 memset(&(remote_addr.sin6_addr.s6_addr[10]), 0xff, 2);
157 memcpy(&(remote_addr.sin6_addr.s6_addr[12]), &v4addr.s_addr, 4);
158
159 // now remote_addr is a v4-mapped v6 address
160 ret = connect(sockfd, (const struct sockaddr *)&remote_addr, sizeof(remote_addr));
161
162 if (ret == -1) {
163 T_SKIP("Couldn't connect. ret = %d, errno = %d\n", ret, errno);
164 }
165
166 T_LOG("Socket established. Binded & connected.\n");
167
168 int kq = kqueue();
169
170 if (kq == -1) {
171 T_SKIP("Failed to kqueue. errno = %d\n", errno);
172 }
173
174 // add fd to kqueue
175 struct kevent evSet = { };
176 EV_SET(&evSet, sockfd, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (void *)NULL);
177 if (kevent(kq, &evSet, 1, NULL, 0, NULL) < 0) {
178 T_SKIP("kevent failed??\n");
179 }
180
181 // send pkt to notify remote
182 sendto(sockfd, "ping", 4, 0, NULL, 0);
183 do_kqueue(kq, sockfd);
184 }
185
186 void
do_kqueue(int kq,int sockfd)187 do_kqueue(int kq, int sockfd)
188 {
189 int ret = 0;
190
191 char control_space[CMSG_SPACE(8192)] = {};
192 struct msghdr recvmsghdr = {};
193 char packet_space[1500] = {};
194
195 struct iovec recv_iov;
196 recv_iov.iov_len = 1200; // just a safe buffer from the 1500 mut
197 recv_iov.iov_base = &packet_space;
198
199 recvmsghdr.msg_iov = &recv_iov;
200 recvmsghdr.msg_iovlen = 1;
201 recvmsghdr.msg_control = &control_space;
202 recvmsghdr.msg_controllen = sizeof(control_space);
203 recvmsghdr.msg_flags = 0;
204
205 struct kevent evSet;
206 struct kevent evList[32];
207
208 struct timespec timeout;
209 timeout.tv_sec = 10;
210 timeout.tv_nsec = 0;
211
212 int nev = kevent(kq, NULL, 0, evList, 32, &timeout);
213
214 for (int i = 0; i < nev; i++) {
215 T_LOG("[kevent] ident = %zd, filter = %hu, flags = %hd, fflags = %d, data = %zd, udata = %p\n",
216 evList[i].ident, evList[i].filter, evList[i].flags, evList[i].fflags, evList[i].data, evList[i].udata);
217
218 if (evList[i].filter == EVFILT_READ) {
219 ret = recvmsg(sockfd, &recvmsghdr, 0);
220 if (ret != -1) {
221 handle_recv(&recvmsghdr, ret);
222 } else {
223 T_SKIP("sender recv failed");
224 }
225 return;
226 }
227 }
228
229 T_FAIL("timeout after 10s");
230 }
231
232 void
handle_recv(struct msghdr * recvmsghdr,int packet_len)233 handle_recv(struct msghdr *recvmsghdr, int packet_len)
234 {
235 T_LOG("[!!] Received %d bytes...", packet_len);
236
237 for (int i = 0; i < MIN(packet_len, 5); i++) {
238 T_LOG("%02x ", ((char *)recvmsghdr->msg_iov->iov_base)[i]);
239 }
240
241 T_LOG("\n");
242
243 struct cmsghdr *cmsg;
244 int received_cmsg_header = 0;
245 for (cmsg = CMSG_FIRSTHDR(recvmsghdr); cmsg != NULL; cmsg = CMSG_NXTHDR(recvmsghdr, cmsg)) {
246 // this never gets hit
247 T_LOG("cmsg_level: %d, cmsg_type: %d\n", cmsg->cmsg_level, cmsg->cmsg_type);
248 received_cmsg_header = 1;
249 }
250
251 T_EXPECT_TRUE(received_cmsg_header == 1, "recved cmsg hdr");
252 T_PASS("Recvd cmsg hdr");
253 }
254