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