1*94d3b452SApple OSS Distributions /*
2*94d3b452SApple OSS Distributions * Copyright (c) 2022 Apple Inc. All rights reserved.
3*94d3b452SApple OSS Distributions *
4*94d3b452SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*94d3b452SApple OSS Distributions *
6*94d3b452SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*94d3b452SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*94d3b452SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*94d3b452SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*94d3b452SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*94d3b452SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*94d3b452SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*94d3b452SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*94d3b452SApple OSS Distributions *
15*94d3b452SApple OSS Distributions * Please obtain a copy of the License at
16*94d3b452SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*94d3b452SApple OSS Distributions *
18*94d3b452SApple OSS Distributions * The Original Code and all software distributed under the License are
19*94d3b452SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*94d3b452SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*94d3b452SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*94d3b452SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*94d3b452SApple OSS Distributions * Please see the License for the specific language governing rights and
24*94d3b452SApple OSS Distributions * limitations under the License.
25*94d3b452SApple OSS Distributions *
26*94d3b452SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*94d3b452SApple OSS Distributions */
28*94d3b452SApple OSS Distributions
29*94d3b452SApple OSS Distributions #include <sys/errno.h>
30*94d3b452SApple OSS Distributions #include <sys/socket.h>
31*94d3b452SApple OSS Distributions #include <sys/sysctl.h>
32*94d3b452SApple OSS Distributions
33*94d3b452SApple OSS Distributions #include <netinet/in.h>
34*94d3b452SApple OSS Distributions
35*94d3b452SApple OSS Distributions #include <err.h>
36*94d3b452SApple OSS Distributions #include <stdarg.h>
37*94d3b452SApple OSS Distributions #include <stdbool.h>
38*94d3b452SApple OSS Distributions #include <stdint.h>
39*94d3b452SApple OSS Distributions #include <stdio.h>
40*94d3b452SApple OSS Distributions #include <stdlib.h>
41*94d3b452SApple OSS Distributions #include <string.h>
42*94d3b452SApple OSS Distributions #include <sysexits.h>
43*94d3b452SApple OSS Distributions #include <unistd.h>
44*94d3b452SApple OSS Distributions
45*94d3b452SApple OSS Distributions #include <arpa/inet.h>
46*94d3b452SApple OSS Distributions
47*94d3b452SApple OSS Distributions #include <darwintest.h>
48*94d3b452SApple OSS Distributions
49*94d3b452SApple OSS Distributions /*
50*94d3b452SApple OSS Distributions * rdar://89640053 (Rome 22A201 - panic: assertion failed: RB_EMPTY(&imf->imf_sources))
51*94d3b452SApple OSS Distributions */
52*94d3b452SApple OSS Distributions
53*94d3b452SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.net"),
54*94d3b452SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
55*94d3b452SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("networking"),
56*94d3b452SApple OSS Distributions T_META_ASROOT(true));
57*94d3b452SApple OSS Distributions
58*94d3b452SApple OSS Distributions static void
print_mreq(const char * opt,struct ip_mreq * mreq)59*94d3b452SApple OSS Distributions print_mreq(const char *opt, struct ip_mreq *mreq)
60*94d3b452SApple OSS Distributions {
61*94d3b452SApple OSS Distributions char *imr_multiaddr = strdup(inet_ntoa(mreq->imr_multiaddr));
62*94d3b452SApple OSS Distributions char *imr_interface = strdup(inet_ntoa(mreq->imr_interface));
63*94d3b452SApple OSS Distributions
64*94d3b452SApple OSS Distributions T_LOG("%s imr_multiaddr: %s imr_interface: %s",
65*94d3b452SApple OSS Distributions opt, imr_multiaddr, imr_interface);
66*94d3b452SApple OSS Distributions
67*94d3b452SApple OSS Distributions free(imr_multiaddr);
68*94d3b452SApple OSS Distributions free(imr_interface);
69*94d3b452SApple OSS Distributions }
70*94d3b452SApple OSS Distributions
71*94d3b452SApple OSS Distributions static void
print_mreq_src(const char * opt,struct ip_mreq_source * mreq_src)72*94d3b452SApple OSS Distributions print_mreq_src(const char *opt, struct ip_mreq_source *mreq_src)
73*94d3b452SApple OSS Distributions {
74*94d3b452SApple OSS Distributions char *imr_multiaddr = strdup(inet_ntoa(mreq_src->imr_multiaddr));
75*94d3b452SApple OSS Distributions char *imr_sourceaddr = strdup(inet_ntoa(mreq_src->imr_sourceaddr));
76*94d3b452SApple OSS Distributions char *imr_interface = strdup(inet_ntoa(mreq_src->imr_interface));
77*94d3b452SApple OSS Distributions
78*94d3b452SApple OSS Distributions T_LOG("%s imr_multiaddr: %s imr_sourceaddr: %s imr_interface: %s",
79*94d3b452SApple OSS Distributions opt, imr_multiaddr, imr_sourceaddr, imr_interface);
80*94d3b452SApple OSS Distributions
81*94d3b452SApple OSS Distributions free(imr_multiaddr);
82*94d3b452SApple OSS Distributions free(imr_sourceaddr);
83*94d3b452SApple OSS Distributions free(imr_interface);
84*94d3b452SApple OSS Distributions }
85*94d3b452SApple OSS Distributions
86*94d3b452SApple OSS Distributions static void
test_ip_drop_membership(uint32_t max)87*94d3b452SApple OSS Distributions test_ip_drop_membership(uint32_t max)
88*94d3b452SApple OSS Distributions {
89*94d3b452SApple OSS Distributions uint32_t maddr = 0xef000000; /* 239.0.0.0 */
90*94d3b452SApple OSS Distributions int fd;
91*94d3b452SApple OSS Distributions
92*94d3b452SApple OSS Distributions if (max == 0) {
93*94d3b452SApple OSS Distributions max = 1;
94*94d3b452SApple OSS Distributions }
95*94d3b452SApple OSS Distributions
96*94d3b452SApple OSS Distributions fd = socket(AF_INET, SOCK_RAW, 1);
97*94d3b452SApple OSS Distributions if (fd < 0) {
98*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(-1, "socket(AF_INET, SOCK_DGRAM, 1)");
99*94d3b452SApple OSS Distributions }
100*94d3b452SApple OSS Distributions
101*94d3b452SApple OSS Distributions for (uint32_t i = 0; i < max; i++) {
102*94d3b452SApple OSS Distributions struct ip_mreq mreq = {};
103*94d3b452SApple OSS Distributions struct ip_mreq_source mreq_src = {};
104*94d3b452SApple OSS Distributions
105*94d3b452SApple OSS Distributions maddr += 1;
106*94d3b452SApple OSS Distributions
107*94d3b452SApple OSS Distributions mreq.imr_multiaddr.s_addr = htonl(maddr);
108*94d3b452SApple OSS Distributions mreq.imr_interface.s_addr = htonl(INADDR_LOOPBACK);
109*94d3b452SApple OSS Distributions
110*94d3b452SApple OSS Distributions print_mreq("IP_ADD_MEMBERSHIP", &mreq);
111*94d3b452SApple OSS Distributions if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) == -1) {
112*94d3b452SApple OSS Distributions /*
113*94d3b452SApple OSS Distributions * The call is expected to fail when we reach the limit of membership
114*94d3b452SApple OSS Distributions * and when the device does not have an IP address
115*94d3b452SApple OSS Distributions */
116*94d3b452SApple OSS Distributions if (errno == ETOOMANYREFS || errno == EADDRNOTAVAIL || errno == ENOMEM) {
117*94d3b452SApple OSS Distributions T_LOG("setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP) %s", strerror(errno));
118*94d3b452SApple OSS Distributions break;
119*94d3b452SApple OSS Distributions }
120*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(-1, "setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP)");
121*94d3b452SApple OSS Distributions }
122*94d3b452SApple OSS Distributions
123*94d3b452SApple OSS Distributions maddr += 1;
124*94d3b452SApple OSS Distributions
125*94d3b452SApple OSS Distributions mreq_src.imr_multiaddr.s_addr = htonl(maddr);
126*94d3b452SApple OSS Distributions mreq_src.imr_sourceaddr.s_addr = htonl(INADDR_LOOPBACK);
127*94d3b452SApple OSS Distributions mreq_src.imr_interface.s_addr = htonl(INADDR_LOOPBACK);
128*94d3b452SApple OSS Distributions
129*94d3b452SApple OSS Distributions print_mreq_src("IP_ADD_SOURCE_MEMBERSHIP", &mreq_src);
130*94d3b452SApple OSS Distributions
131*94d3b452SApple OSS Distributions if (setsockopt(fd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, (void *)&mreq_src, sizeof(mreq_src)) == -1) {
132*94d3b452SApple OSS Distributions if (errno == ETOOMANYREFS || errno == EADDRNOTAVAIL || errno == ENOMEM) {
133*94d3b452SApple OSS Distributions T_LOG("setsockopt(IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP) %s", strerror(errno));
134*94d3b452SApple OSS Distributions break;
135*94d3b452SApple OSS Distributions }
136*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(-1, "setsockopt(IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP)");
137*94d3b452SApple OSS Distributions }
138*94d3b452SApple OSS Distributions
139*94d3b452SApple OSS Distributions print_mreq("IP_DROP_MEMBERSHIP", &mreq);
140*94d3b452SApple OSS Distributions if (setsockopt(fd, 0, IP_DROP_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) == -1) {
141*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(-1, "setsockopt(IPPROTO_IP, IP_DROP_MEMBERSHIP)");
142*94d3b452SApple OSS Distributions }
143*94d3b452SApple OSS Distributions
144*94d3b452SApple OSS Distributions print_mreq("IP_ADD_MEMBERSHIP", &mreq);
145*94d3b452SApple OSS Distributions if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) == -1) {
146*94d3b452SApple OSS Distributions if (errno == ETOOMANYREFS || errno == EADDRNOTAVAIL) {
147*94d3b452SApple OSS Distributions T_LOG("setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP) %s", strerror(errno));
148*94d3b452SApple OSS Distributions break;
149*94d3b452SApple OSS Distributions }
150*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(-1, "setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP)");
151*94d3b452SApple OSS Distributions }
152*94d3b452SApple OSS Distributions }
153*94d3b452SApple OSS Distributions close(fd);
154*94d3b452SApple OSS Distributions }
155*94d3b452SApple OSS Distributions
156*94d3b452SApple OSS Distributions T_DECL(ip_drop_membership, "test IP_DROP_MEMBERSHIP")
157*94d3b452SApple OSS Distributions {
158*94d3b452SApple OSS Distributions test_ip_drop_membership(0xffff);
159*94d3b452SApple OSS Distributions
160*94d3b452SApple OSS Distributions T_PASS("ip_drop_membership");
161*94d3b452SApple OSS Distributions }
162