1 /*
2 * Copyright (c) 2022 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
32 #include <netinet/in.h>
33
34 #include <err.h>
35 #include <stdarg.h>
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sysexits.h>
42 #include <unistd.h>
43
44 #include <arpa/inet.h>
45
46 #include <darwintest.h>
47
48 /*
49 * rdar://89640053 (Rome 22A201 - panic: assertion failed: RB_EMPTY(&imf->imf_sources))
50 */
51
52 T_GLOBAL_META(T_META_NAMESPACE("xnu.net"),
53 T_META_RADAR_COMPONENT_NAME("xnu"),
54 T_META_RADAR_COMPONENT_VERSION("networking"),
55 T_META_ASROOT(true));
56
57 static void
print_mreq(const char * opt,struct ip_mreq * mreq)58 print_mreq(const char *opt, struct ip_mreq *mreq)
59 {
60 char *imr_multiaddr = strdup(inet_ntoa(mreq->imr_multiaddr));
61 char *imr_interface = strdup(inet_ntoa(mreq->imr_interface));
62
63 T_LOG("%s imr_multiaddr: %s imr_interface: %s",
64 opt, imr_multiaddr, imr_interface);
65
66 free(imr_multiaddr);
67 free(imr_interface);
68 }
69
70 static void
print_mreq_src(const char * opt,struct ip_mreq_source * mreq_src)71 print_mreq_src(const char *opt, struct ip_mreq_source *mreq_src)
72 {
73 char *imr_multiaddr = strdup(inet_ntoa(mreq_src->imr_multiaddr));
74 char *imr_sourceaddr = strdup(inet_ntoa(mreq_src->imr_sourceaddr));
75 char *imr_interface = strdup(inet_ntoa(mreq_src->imr_interface));
76
77 T_LOG("%s imr_multiaddr: %s imr_sourceaddr: %s imr_interface: %s",
78 opt, imr_multiaddr, imr_sourceaddr, imr_interface);
79
80 free(imr_multiaddr);
81 free(imr_sourceaddr);
82 free(imr_interface);
83 }
84
85 static void
test_ip_drop_membership(uint32_t max)86 test_ip_drop_membership(uint32_t max)
87 {
88 uint32_t maddr = 0xef000000; /* 239.0.0.0 */
89 int fd;
90
91 if (max == 0) {
92 max = 1;
93 }
94
95 fd = socket(AF_INET, SOCK_RAW, 1);
96 if (fd < 0) {
97 T_ASSERT_POSIX_SUCCESS(-1, "socket(AF_INET, SOCK_DGRAM, 1)");
98 }
99
100 for (uint32_t i = 0; i < max; i++) {
101 struct ip_mreq mreq = {};
102 struct ip_mreq_source mreq_src = {};
103
104 maddr += 1;
105
106 mreq.imr_multiaddr.s_addr = htonl(maddr);
107 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
108
109 print_mreq("IP_ADD_MEMBERSHIP", &mreq);
110 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) == -1) {
111 /*
112 * The call is expected to fail when we reach the limit of membership
113 * and when the device does not have an IP address
114 */
115 if (errno == ETOOMANYREFS || errno == EADDRNOTAVAIL) {
116 T_LOG("setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP) %s", strerror(errno));
117 break;
118 }
119 T_ASSERT_POSIX_SUCCESS(-1, "setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP)");
120 }
121
122 maddr += 1;
123
124 mreq_src.imr_multiaddr.s_addr = htonl(maddr);
125 mreq_src.imr_sourceaddr.s_addr = htonl(INADDR_ANY);
126 mreq_src.imr_interface.s_addr = htonl(INADDR_ANY);
127
128 print_mreq_src("IP_ADD_SOURCE_MEMBERSHIP", &mreq_src);
129
130 if (setsockopt(fd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, (void *)&mreq_src, sizeof(mreq_src)) == -1) {
131 if (errno == ETOOMANYREFS || errno == EADDRNOTAVAIL) {
132 T_LOG("setsockopt(IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP) %s", strerror(errno));
133 break;
134 }
135 T_ASSERT_POSIX_SUCCESS(-1, "setsockopt(IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP)");
136 }
137
138 print_mreq("IP_DROP_MEMBERSHIP", &mreq);
139 if (setsockopt(fd, 0, IP_DROP_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) == -1) {
140 T_ASSERT_POSIX_SUCCESS(-1, "setsockopt(IPPROTO_IP, IP_DROP_MEMBERSHIP)");
141 }
142
143 print_mreq("IP_ADD_MEMBERSHIP", &mreq);
144 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) == -1) {
145 if (errno == ETOOMANYREFS || errno == EADDRNOTAVAIL) {
146 T_LOG("setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP) %s", strerror(errno));
147 break;
148 }
149 T_ASSERT_POSIX_SUCCESS(-1, "setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP)");
150 }
151 }
152 }
153
154 T_DECL(ip_drop_membership, "test IP_DROP_MEMBERSHIP")
155 {
156 test_ip_drop_membership(0xffff);
157 T_PASS("ip_drop_membership");
158 }
159