1 /*
2 * Copyright (c) 2023-2024 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 #include <sys/errno.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include <darwintest.h>
36
37 T_GLOBAL_META(
38 T_META_NAMESPACE("xnu.net"),
39 T_META_ASROOT(true),
40 T_META_RADAR_COMPONENT_NAME("xnu"),
41 T_META_RADAR_COMPONENT_VERSION("networking"),
42 T_META_CHECK_LEAKS(false),
43 T_META_TAG_VM_PREFERRED);
44
45
46 static void
udp_port_scan(void)47 udp_port_scan(void)
48 {
49 int v4_udp_fd;
50
51 T_ASSERT_POSIX_SUCCESS(v4_udp_fd = socket(AF_INET, SOCK_DGRAM, 0),
52 "fd %d = socket(AF_INET, SOCK_DGRAM)", v4_udp_fd);
53
54 char *buffer = "hello";
55 size_t len = strlen(buffer) + 1;
56
57 for (in_port_t port = 1; port > 0 && port <= IPPORT_HILASTAUTO; port++) {
58 struct sockaddr_in sin = {};
59 sin.sin_len = sizeof(struct sockaddr_in);
60 sin.sin_family = AF_INET;
61 sin.sin_port = htons(port);
62 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
63
64 /*
65 * It is fine to get `ECONNREFUSED` because the port scanning does
66 * trigger ICMP port unreachable messages
67 */
68 ssize_t sent = sendto(v4_udp_fd, buffer, len, 0, (struct sockaddr *)&sin, sin.sin_len);
69 int saved_errno = errno;
70 T_QUIET; T_ASSERT_TRUE(sent >= 0 || errno == ECONNREFUSED, "sendto() to port %u: errno = %d (%s)",
71 port, saved_errno, strerror(saved_errno));
72 }
73
74 close(v4_udp_fd);
75
76 T_LOG("udp_port_scan done");
77 }
78
79 static int
open_raw_ipv4_socket(void)80 open_raw_ipv4_socket(void)
81 {
82 int fd;
83
84 T_ASSERT_POSIX_SUCCESS(fd = socket(AF_INET, SOCK_RAW, 0),
85 "fd %d = socket(AF_INET, SOCK_RAW)", fd);
86
87 return fd;
88 }
89
90 static int
open_raw_ipv6_socket(void)91 open_raw_ipv6_socket(void)
92 {
93 int fd;
94
95 T_ASSERT_POSIX_SUCCESS(fd = socket(AF_INET6, SOCK_RAW, 0),
96 "fd %d = socket(AF_INET6, SOCK_RAW)", fd);
97
98 int off = 0;
99 T_ASSERT_POSIX_SUCCESS(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(int)),
100 "setsockopt(%d, IPPROTO_IPV6, IPV6_V6ONLY)", fd);
101
102 return fd;
103 }
104
105 static void
close_raw_socket(int fd)106 close_raw_socket(int fd)
107 {
108 int optval;
109 socklen_t optlen = sizeof(optval);
110
111 T_ASSERT_POSIX_SUCCESS(getsockopt(fd, SOL_SOCKET, SO_NUMRCVPKT, &optval, &optlen),
112 "getsockopt(%d, SOL_SOCKET, SO_NUMRCVPKT)", fd);
113
114 T_LOG("fd %d SO_NUMRCVPKT %d", fd, optval);
115
116 (void)close(fd);
117 }
118
119 T_DECL(rip_no_input, "test reception of IPv4 packet on raw IPv6 socket ", T_META_TAG_VM_PREFERRED)
120 {
121 udp_port_scan();
122
123 T_PASS("%s", __func__);
124 }
125
126 T_DECL(rip_v4_input, "test reception of IPv4 packet on raw IPv6 socket ", T_META_TAG_VM_PREFERRED)
127 {
128 int v4_raw_fd1 = open_raw_ipv4_socket();
129
130 udp_port_scan();
131
132 close_raw_socket(v4_raw_fd1);
133
134 T_PASS("%s", __func__);
135 }
136
137 T_DECL(rip_v6_input, "test reception of IPv4 packet on raw IPv6 socket ", T_META_TAG_VM_PREFERRED)
138 {
139 int v6_raw_fd1 = open_raw_ipv6_socket();
140
141 udp_port_scan();
142
143 close_raw_socket(v6_raw_fd1);
144
145 T_PASS("%s", __func__);
146 }
147
148 T_DECL(rip_v4v4_input, "test reception of IPv4 packet on raw IPv6 socket ", T_META_TAG_VM_PREFERRED)
149 {
150 int v4_raw_fd1 = open_raw_ipv4_socket();
151 int v4_raw_fd2 = open_raw_ipv4_socket();
152
153 udp_port_scan();
154
155 close_raw_socket(v4_raw_fd1);
156 close_raw_socket(v4_raw_fd2);
157
158 T_PASS("%s", __func__);
159 }
160
161 T_DECL(rip_v6v6_input, "test reception of IPv4 packet on raw IPv6 socket ", T_META_TAG_VM_PREFERRED)
162 {
163 int v6_raw_fd1 = open_raw_ipv6_socket();
164 int v6_raw_fd2 = open_raw_ipv6_socket();
165
166 udp_port_scan();
167
168 close_raw_socket(v6_raw_fd1);
169 close_raw_socket(v6_raw_fd2);
170
171 T_PASS("%s", __func__);
172 }
173
174 T_DECL(rip_v4v6_input, "test reception of IPv4 packet on raw IPv6 socket ", T_META_TAG_VM_PREFERRED)
175 {
176 int v4_raw_fd1 = open_raw_ipv4_socket();
177 int v6_raw_fd1 = open_raw_ipv6_socket();
178
179 udp_port_scan();
180
181 close_raw_socket(v4_raw_fd1);
182 close_raw_socket(v6_raw_fd1);
183
184 T_PASS("%s", __func__);
185 }
186
187 T_DECL(rip_v4v4v6_input, "test reception of IPv4 packet on raw IPv6 socket ", T_META_TAG_VM_PREFERRED)
188 {
189 int v4_raw_fd1 = open_raw_ipv4_socket();
190 int v4_raw_fd2 = open_raw_ipv4_socket();
191 int v6_raw_fd = open_raw_ipv6_socket();
192
193 udp_port_scan();
194
195 close_raw_socket(v4_raw_fd1);
196 close_raw_socket(v4_raw_fd2);
197 close_raw_socket(v6_raw_fd);
198
199 T_PASS("%s", __func__);
200 }
201
202 T_DECL(rip_v4v6v6_input, "test reception of IPv4 packet on raw IPv6 socket ", T_META_TAG_VM_PREFERRED)
203 {
204 int v4_raw_fd1 = open_raw_ipv4_socket();
205 int v6_raw_fd1 = open_raw_ipv6_socket();
206 int v6_raw_fd2 = open_raw_ipv6_socket();
207
208 udp_port_scan();
209
210 close_raw_socket(v4_raw_fd1);
211 close_raw_socket(v6_raw_fd1);
212 close_raw_socket(v6_raw_fd2);
213
214 T_PASS("%s", __func__);
215 }
216
217 T_DECL(rip_v4v4v6v6_input, "test reception of IPv4 packet on raw IPv6 socket ", T_META_TAG_VM_PREFERRED)
218 {
219 int v4_raw_fd1 = open_raw_ipv4_socket();
220 int v4_raw_fd2 = open_raw_ipv4_socket();
221 int v6_raw_fd1 = open_raw_ipv6_socket();
222 int v6_raw_fd2 = open_raw_ipv6_socket();
223
224 udp_port_scan();
225
226 close_raw_socket(v4_raw_fd1);
227 close_raw_socket(v4_raw_fd2);
228 close_raw_socket(v6_raw_fd1);
229 close_raw_socket(v6_raw_fd2);
230
231 T_PASS("%s", __func__);
232 }
233