1*699cd480SApple OSS Distributions /*
2*699cd480SApple OSS Distributions * Copyright (c) 2023 Apple Inc. All rights reserved.
3*699cd480SApple OSS Distributions *
4*699cd480SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*699cd480SApple OSS Distributions *
6*699cd480SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*699cd480SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*699cd480SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*699cd480SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*699cd480SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*699cd480SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*699cd480SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*699cd480SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*699cd480SApple OSS Distributions *
15*699cd480SApple OSS Distributions * Please obtain a copy of the License at
16*699cd480SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*699cd480SApple OSS Distributions *
18*699cd480SApple OSS Distributions * The Original Code and all software distributed under the License are
19*699cd480SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*699cd480SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*699cd480SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*699cd480SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*699cd480SApple OSS Distributions * Please see the License for the specific language governing rights and
24*699cd480SApple OSS Distributions * limitations under the License.
25*699cd480SApple OSS Distributions *
26*699cd480SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*699cd480SApple OSS Distributions */
28*699cd480SApple OSS Distributions
29*699cd480SApple OSS Distributions #include <sys/socket.h>
30*699cd480SApple OSS Distributions #include <netinet/in.h>
31*699cd480SApple OSS Distributions #include <string.h>
32*699cd480SApple OSS Distributions
33*699cd480SApple OSS Distributions #include <darwintest.h>
34*699cd480SApple OSS Distributions
35*699cd480SApple OSS Distributions T_GLOBAL_META(
36*699cd480SApple OSS Distributions T_META_NAMESPACE("xnu.net"),
37*699cd480SApple OSS Distributions T_META_ASROOT(true),
38*699cd480SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
39*699cd480SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("networking"),
40*699cd480SApple OSS Distributions T_META_CHECK_LEAKS(false));
41*699cd480SApple OSS Distributions
42*699cd480SApple OSS Distributions
43*699cd480SApple OSS Distributions static void
udp_port_scan(void)44*699cd480SApple OSS Distributions udp_port_scan(void)
45*699cd480SApple OSS Distributions {
46*699cd480SApple OSS Distributions int v4_udp_fd;
47*699cd480SApple OSS Distributions
48*699cd480SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(v4_udp_fd = socket(AF_INET, SOCK_DGRAM, 0),
49*699cd480SApple OSS Distributions "fd %d = socket(AF_INET, SOCK_DGRAM)", v4_udp_fd);
50*699cd480SApple OSS Distributions
51*699cd480SApple OSS Distributions char *buffer = "hello";
52*699cd480SApple OSS Distributions size_t len = strlen(buffer) + 1;
53*699cd480SApple OSS Distributions
54*699cd480SApple OSS Distributions for (in_port_t port = 1; port > 0 && port <= IPPORT_HILASTAUTO; port++) {
55*699cd480SApple OSS Distributions struct sockaddr_in sin = {};
56*699cd480SApple OSS Distributions sin.sin_len = sizeof(struct sockaddr_in);
57*699cd480SApple OSS Distributions sin.sin_family = AF_INET;
58*699cd480SApple OSS Distributions sin.sin_port = htons(port);
59*699cd480SApple OSS Distributions sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
60*699cd480SApple OSS Distributions
61*699cd480SApple OSS Distributions ssize_t sent;
62*699cd480SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(sent = sendto(v4_udp_fd, buffer, len, 0, (struct sockaddr *)&sin, sin.sin_len),
63*699cd480SApple OSS Distributions "sendto() to port %u", port);
64*699cd480SApple OSS Distributions }
65*699cd480SApple OSS Distributions
66*699cd480SApple OSS Distributions close(v4_udp_fd);
67*699cd480SApple OSS Distributions
68*699cd480SApple OSS Distributions T_LOG("udp_port_scan done");
69*699cd480SApple OSS Distributions }
70*699cd480SApple OSS Distributions
71*699cd480SApple OSS Distributions static int
open_raw_ipv4_socket(void)72*699cd480SApple OSS Distributions open_raw_ipv4_socket(void)
73*699cd480SApple OSS Distributions {
74*699cd480SApple OSS Distributions int fd;
75*699cd480SApple OSS Distributions
76*699cd480SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd = socket(AF_INET, SOCK_RAW, 0),
77*699cd480SApple OSS Distributions "fd %d = socket(AF_INET, SOCK_RAW)", fd);
78*699cd480SApple OSS Distributions
79*699cd480SApple OSS Distributions return fd;
80*699cd480SApple OSS Distributions }
81*699cd480SApple OSS Distributions
82*699cd480SApple OSS Distributions static int
open_raw_ipv6_socket(void)83*699cd480SApple OSS Distributions open_raw_ipv6_socket(void)
84*699cd480SApple OSS Distributions {
85*699cd480SApple OSS Distributions int fd;
86*699cd480SApple OSS Distributions
87*699cd480SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd = socket(AF_INET6, SOCK_RAW, 0),
88*699cd480SApple OSS Distributions "fd %d = socket(AF_INET6, SOCK_RAW)", fd);
89*699cd480SApple OSS Distributions
90*699cd480SApple OSS Distributions int off = 0;
91*699cd480SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(int)),
92*699cd480SApple OSS Distributions "setsockopt(%d, IPPROTO_IPV6, IPV6_V6ONLY)", fd);
93*699cd480SApple OSS Distributions
94*699cd480SApple OSS Distributions return fd;
95*699cd480SApple OSS Distributions }
96*699cd480SApple OSS Distributions
97*699cd480SApple OSS Distributions static void
close_raw_socket(int fd)98*699cd480SApple OSS Distributions close_raw_socket(int fd)
99*699cd480SApple OSS Distributions {
100*699cd480SApple OSS Distributions int optval;
101*699cd480SApple OSS Distributions socklen_t optlen = sizeof(optval);
102*699cd480SApple OSS Distributions
103*699cd480SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockopt(fd, SOL_SOCKET, SO_NUMRCVPKT, &optval, &optlen),
104*699cd480SApple OSS Distributions "getsockopt(%d, SOL_SOCKET, SO_NUMRCVPKT)", fd);
105*699cd480SApple OSS Distributions
106*699cd480SApple OSS Distributions T_LOG("fd %d SO_NUMRCVPKT %d", fd, optval);
107*699cd480SApple OSS Distributions
108*699cd480SApple OSS Distributions (void)close(fd);
109*699cd480SApple OSS Distributions }
110*699cd480SApple OSS Distributions
111*699cd480SApple OSS Distributions T_DECL(rip_no_input, "test reception of IPv4 packet on raw IPv6 socket ")
112*699cd480SApple OSS Distributions {
113*699cd480SApple OSS Distributions udp_port_scan();
114*699cd480SApple OSS Distributions
115*699cd480SApple OSS Distributions T_PASS("%s", __func__);
116*699cd480SApple OSS Distributions }
117*699cd480SApple OSS Distributions
118*699cd480SApple OSS Distributions T_DECL(rip_v4_input, "test reception of IPv4 packet on raw IPv6 socket ")
119*699cd480SApple OSS Distributions {
120*699cd480SApple OSS Distributions int v4_raw_fd1 = open_raw_ipv4_socket();
121*699cd480SApple OSS Distributions
122*699cd480SApple OSS Distributions udp_port_scan();
123*699cd480SApple OSS Distributions
124*699cd480SApple OSS Distributions close_raw_socket(v4_raw_fd1);
125*699cd480SApple OSS Distributions
126*699cd480SApple OSS Distributions T_PASS("%s", __func__);
127*699cd480SApple OSS Distributions }
128*699cd480SApple OSS Distributions
129*699cd480SApple OSS Distributions T_DECL(rip_v6_input, "test reception of IPv4 packet on raw IPv6 socket ")
130*699cd480SApple OSS Distributions {
131*699cd480SApple OSS Distributions int v6_raw_fd1 = open_raw_ipv6_socket();
132*699cd480SApple OSS Distributions
133*699cd480SApple OSS Distributions udp_port_scan();
134*699cd480SApple OSS Distributions
135*699cd480SApple OSS Distributions close_raw_socket(v6_raw_fd1);
136*699cd480SApple OSS Distributions
137*699cd480SApple OSS Distributions T_PASS("%s", __func__);
138*699cd480SApple OSS Distributions }
139*699cd480SApple OSS Distributions
140*699cd480SApple OSS Distributions T_DECL(rip_v4v4_input, "test reception of IPv4 packet on raw IPv6 socket ")
141*699cd480SApple OSS Distributions {
142*699cd480SApple OSS Distributions int v4_raw_fd1 = open_raw_ipv4_socket();
143*699cd480SApple OSS Distributions int v4_raw_fd2 = open_raw_ipv4_socket();
144*699cd480SApple OSS Distributions
145*699cd480SApple OSS Distributions udp_port_scan();
146*699cd480SApple OSS Distributions
147*699cd480SApple OSS Distributions close_raw_socket(v4_raw_fd1);
148*699cd480SApple OSS Distributions close_raw_socket(v4_raw_fd2);
149*699cd480SApple OSS Distributions
150*699cd480SApple OSS Distributions T_PASS("%s", __func__);
151*699cd480SApple OSS Distributions }
152*699cd480SApple OSS Distributions
153*699cd480SApple OSS Distributions T_DECL(rip_v6v6_input, "test reception of IPv4 packet on raw IPv6 socket ")
154*699cd480SApple OSS Distributions {
155*699cd480SApple OSS Distributions int v6_raw_fd1 = open_raw_ipv6_socket();
156*699cd480SApple OSS Distributions int v6_raw_fd2 = open_raw_ipv6_socket();
157*699cd480SApple OSS Distributions
158*699cd480SApple OSS Distributions udp_port_scan();
159*699cd480SApple OSS Distributions
160*699cd480SApple OSS Distributions close_raw_socket(v6_raw_fd1);
161*699cd480SApple OSS Distributions close_raw_socket(v6_raw_fd2);
162*699cd480SApple OSS Distributions
163*699cd480SApple OSS Distributions T_PASS("%s", __func__);
164*699cd480SApple OSS Distributions }
165*699cd480SApple OSS Distributions
166*699cd480SApple OSS Distributions T_DECL(rip_v4v6_input, "test reception of IPv4 packet on raw IPv6 socket ")
167*699cd480SApple OSS Distributions {
168*699cd480SApple OSS Distributions int v4_raw_fd1 = open_raw_ipv4_socket();
169*699cd480SApple OSS Distributions int v6_raw_fd1 = open_raw_ipv6_socket();
170*699cd480SApple OSS Distributions
171*699cd480SApple OSS Distributions udp_port_scan();
172*699cd480SApple OSS Distributions
173*699cd480SApple OSS Distributions close_raw_socket(v4_raw_fd1);
174*699cd480SApple OSS Distributions close_raw_socket(v6_raw_fd1);
175*699cd480SApple OSS Distributions
176*699cd480SApple OSS Distributions T_PASS("%s", __func__);
177*699cd480SApple OSS Distributions }
178*699cd480SApple OSS Distributions
179*699cd480SApple OSS Distributions T_DECL(rip_v4v4v6_input, "test reception of IPv4 packet on raw IPv6 socket ")
180*699cd480SApple OSS Distributions {
181*699cd480SApple OSS Distributions int v4_raw_fd1 = open_raw_ipv4_socket();
182*699cd480SApple OSS Distributions int v4_raw_fd2 = open_raw_ipv4_socket();
183*699cd480SApple OSS Distributions int v6_raw_fd = open_raw_ipv6_socket();
184*699cd480SApple OSS Distributions
185*699cd480SApple OSS Distributions udp_port_scan();
186*699cd480SApple OSS Distributions
187*699cd480SApple OSS Distributions close_raw_socket(v4_raw_fd1);
188*699cd480SApple OSS Distributions close_raw_socket(v4_raw_fd2);
189*699cd480SApple OSS Distributions close_raw_socket(v6_raw_fd);
190*699cd480SApple OSS Distributions
191*699cd480SApple OSS Distributions T_PASS("%s", __func__);
192*699cd480SApple OSS Distributions }
193*699cd480SApple OSS Distributions
194*699cd480SApple OSS Distributions T_DECL(rip_v4v6v6_input, "test reception of IPv4 packet on raw IPv6 socket ")
195*699cd480SApple OSS Distributions {
196*699cd480SApple OSS Distributions int v4_raw_fd1 = open_raw_ipv4_socket();
197*699cd480SApple OSS Distributions int v6_raw_fd1 = open_raw_ipv6_socket();
198*699cd480SApple OSS Distributions int v6_raw_fd2 = open_raw_ipv6_socket();
199*699cd480SApple OSS Distributions
200*699cd480SApple OSS Distributions udp_port_scan();
201*699cd480SApple OSS Distributions
202*699cd480SApple OSS Distributions close_raw_socket(v4_raw_fd1);
203*699cd480SApple OSS Distributions close_raw_socket(v6_raw_fd1);
204*699cd480SApple OSS Distributions close_raw_socket(v6_raw_fd2);
205*699cd480SApple OSS Distributions
206*699cd480SApple OSS Distributions T_PASS("%s", __func__);
207*699cd480SApple OSS Distributions }
208*699cd480SApple OSS Distributions
209*699cd480SApple OSS Distributions T_DECL(rip_v4v4v6v6_input, "test reception of IPv4 packet on raw IPv6 socket ")
210*699cd480SApple OSS Distributions {
211*699cd480SApple OSS Distributions int v4_raw_fd1 = open_raw_ipv4_socket();
212*699cd480SApple OSS Distributions int v4_raw_fd2 = open_raw_ipv4_socket();
213*699cd480SApple OSS Distributions int v6_raw_fd1 = open_raw_ipv6_socket();
214*699cd480SApple OSS Distributions int v6_raw_fd2 = open_raw_ipv6_socket();
215*699cd480SApple OSS Distributions
216*699cd480SApple OSS Distributions udp_port_scan();
217*699cd480SApple OSS Distributions
218*699cd480SApple OSS Distributions close_raw_socket(v4_raw_fd1);
219*699cd480SApple OSS Distributions close_raw_socket(v4_raw_fd2);
220*699cd480SApple OSS Distributions close_raw_socket(v6_raw_fd1);
221*699cd480SApple OSS Distributions close_raw_socket(v6_raw_fd2);
222*699cd480SApple OSS Distributions
223*699cd480SApple OSS Distributions T_PASS("%s", __func__);
224*699cd480SApple OSS Distributions }
225