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