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