1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions * Copyright (c) 2020-2022 Apple Inc. All rights reserved.
3*27b03b36SApple OSS Distributions *
4*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions *
6*27b03b36SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions *
15*27b03b36SApple OSS Distributions * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions *
18*27b03b36SApple OSS Distributions * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions * limitations under the License.
25*27b03b36SApple OSS Distributions *
26*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions */
28*27b03b36SApple OSS Distributions
29*27b03b36SApple OSS Distributions /* -*- compile-command: "xcrun --sdk iphoneos.internal make sioc-if-addr-bounds" -*- */
30*27b03b36SApple OSS Distributions
31*27b03b36SApple OSS Distributions #include <sys/ioctl.h>
32*27b03b36SApple OSS Distributions #include <sys/socket.h>
33*27b03b36SApple OSS Distributions #include <sys/sockio.h>
34*27b03b36SApple OSS Distributions
35*27b03b36SApple OSS Distributions #include <net/if.h>
36*27b03b36SApple OSS Distributions #include <net/if_dl.h>
37*27b03b36SApple OSS Distributions
38*27b03b36SApple OSS Distributions #include <netinet/in.h>
39*27b03b36SApple OSS Distributions #include <netinet/in_var.h>
40*27b03b36SApple OSS Distributions
41*27b03b36SApple OSS Distributions #include <string.h>
42*27b03b36SApple OSS Distributions #include <unistd.h>
43*27b03b36SApple OSS Distributions
44*27b03b36SApple OSS Distributions #include <arpa/inet.h>
45*27b03b36SApple OSS Distributions
46*27b03b36SApple OSS Distributions #include <darwintest.h>
47*27b03b36SApple OSS Distributions #include <darwintest_utils.h>
48*27b03b36SApple OSS Distributions
49*27b03b36SApple OSS Distributions #include <uuid/uuid.h>
50*27b03b36SApple OSS Distributions
51*27b03b36SApple OSS Distributions #include <ifaddrs.h>
52*27b03b36SApple OSS Distributions
53*27b03b36SApple OSS Distributions #include <arpa/inet.h>
54*27b03b36SApple OSS Distributions
55*27b03b36SApple OSS Distributions #include "ioc_str.h"
56*27b03b36SApple OSS Distributions
57*27b03b36SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.net"));
58*27b03b36SApple OSS Distributions
59*27b03b36SApple OSS Distributions #ifndef STRINGIFY
60*27b03b36SApple OSS Distributions #define __STR(x) #x /* just a helper macro */
61*27b03b36SApple OSS Distributions #define STRINGIFY(x) __STR(x)
62*27b03b36SApple OSS Distributions #endif /* STRINGIFY */
63*27b03b36SApple OSS Distributions
64*27b03b36SApple OSS Distributions #define IF_NAME "bridge"
65*27b03b36SApple OSS Distributions
66*27b03b36SApple OSS Distributions /* On some platforms with DEBUG kernel, we need to wait a while */
67*27b03b36SApple OSS Distributions #define SIFCREATE_RETRY 600
68*27b03b36SApple OSS Distributions
69*27b03b36SApple OSS Distributions #define PATTERN_SIZE 8
70*27b03b36SApple OSS Distributions
71*27b03b36SApple OSS Distributions static int
ifnet_destroy(int s,const char * ifname,bool fail_on_error)72*27b03b36SApple OSS Distributions ifnet_destroy(int s, const char * ifname, bool fail_on_error)
73*27b03b36SApple OSS Distributions {
74*27b03b36SApple OSS Distributions int err;
75*27b03b36SApple OSS Distributions struct ifreq ifr;
76*27b03b36SApple OSS Distributions
77*27b03b36SApple OSS Distributions bzero(&ifr, sizeof(ifr));
78*27b03b36SApple OSS Distributions strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
79*27b03b36SApple OSS Distributions err = ioctl(s, SIOCIFDESTROY, &ifr);
80*27b03b36SApple OSS Distributions if (fail_on_error) {
81*27b03b36SApple OSS Distributions T_QUIET;
82*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(err, "SIOCSIFDESTROY %s", ifr.ifr_name);
83*27b03b36SApple OSS Distributions }
84*27b03b36SApple OSS Distributions if (err < 0) {
85*27b03b36SApple OSS Distributions T_LOG("SIOCSIFDESTROY %s", ifr.ifr_name);
86*27b03b36SApple OSS Distributions }
87*27b03b36SApple OSS Distributions return err;
88*27b03b36SApple OSS Distributions }
89*27b03b36SApple OSS Distributions
90*27b03b36SApple OSS Distributions static int
ifnet_set_flags(int s,const char * ifname,uint16_t flags_set,uint16_t flags_clear)91*27b03b36SApple OSS Distributions ifnet_set_flags(int s, const char * ifname,
92*27b03b36SApple OSS Distributions uint16_t flags_set, uint16_t flags_clear)
93*27b03b36SApple OSS Distributions {
94*27b03b36SApple OSS Distributions uint16_t flags_after;
95*27b03b36SApple OSS Distributions uint16_t flags_before;
96*27b03b36SApple OSS Distributions struct ifreq ifr;
97*27b03b36SApple OSS Distributions int ret;
98*27b03b36SApple OSS Distributions
99*27b03b36SApple OSS Distributions bzero(&ifr, sizeof(ifr));
100*27b03b36SApple OSS Distributions strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
101*27b03b36SApple OSS Distributions ret = ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr);
102*27b03b36SApple OSS Distributions if (ret != 0) {
103*27b03b36SApple OSS Distributions T_LOG("SIOCGIFFLAGS %s", ifr.ifr_name);
104*27b03b36SApple OSS Distributions return ret;
105*27b03b36SApple OSS Distributions }
106*27b03b36SApple OSS Distributions flags_before = (uint16_t)ifr.ifr_flags;
107*27b03b36SApple OSS Distributions ifr.ifr_flags |= flags_set;
108*27b03b36SApple OSS Distributions ifr.ifr_flags &= ~(flags_clear);
109*27b03b36SApple OSS Distributions flags_after = (uint16_t)ifr.ifr_flags;
110*27b03b36SApple OSS Distributions if (flags_before == flags_after) {
111*27b03b36SApple OSS Distributions /* nothing to do */
112*27b03b36SApple OSS Distributions ret = 0;
113*27b03b36SApple OSS Distributions } else {
114*27b03b36SApple OSS Distributions /* issue the ioctl */
115*27b03b36SApple OSS Distributions T_QUIET;
116*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ioctl(s, SIOCSIFFLAGS, &ifr),
117*27b03b36SApple OSS Distributions "SIOCSIFFLAGS %s 0x%x",
118*27b03b36SApple OSS Distributions ifr.ifr_name, (uint16_t)ifr.ifr_flags);
119*27b03b36SApple OSS Distributions }
120*27b03b36SApple OSS Distributions return ret;
121*27b03b36SApple OSS Distributions }
122*27b03b36SApple OSS Distributions
123*27b03b36SApple OSS Distributions static int
ifnet_create(int s,char * ifname,size_t ifname_size)124*27b03b36SApple OSS Distributions ifnet_create(int s, char * ifname, size_t ifname_size)
125*27b03b36SApple OSS Distributions {
126*27b03b36SApple OSS Distributions int error = 0;
127*27b03b36SApple OSS Distributions struct ifreq ifr;
128*27b03b36SApple OSS Distributions
129*27b03b36SApple OSS Distributions bzero(&ifr, sizeof(ifr));
130*27b03b36SApple OSS Distributions strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
131*27b03b36SApple OSS Distributions
132*27b03b36SApple OSS Distributions for (int i = 0; i < SIFCREATE_RETRY; i++) {
133*27b03b36SApple OSS Distributions if (ioctl(s, SIOCIFCREATE, &ifr) < 0) {
134*27b03b36SApple OSS Distributions error = errno;
135*27b03b36SApple OSS Distributions T_LOG("SIOCSIFCREATE %s: %s", ifname,
136*27b03b36SApple OSS Distributions strerror(error));
137*27b03b36SApple OSS Distributions if (error == EBUSY) {
138*27b03b36SApple OSS Distributions /* interface is tearing down, try again */
139*27b03b36SApple OSS Distributions usleep(10000);
140*27b03b36SApple OSS Distributions } else if (error == EEXIST) {
141*27b03b36SApple OSS Distributions /* interface exists, try destroying it */
142*27b03b36SApple OSS Distributions (void)ifnet_destroy(s, ifname, false);
143*27b03b36SApple OSS Distributions } else {
144*27b03b36SApple OSS Distributions /* unexpected failure */
145*27b03b36SApple OSS Distributions break;
146*27b03b36SApple OSS Distributions }
147*27b03b36SApple OSS Distributions } else {
148*27b03b36SApple OSS Distributions error = 0;
149*27b03b36SApple OSS Distributions break;
150*27b03b36SApple OSS Distributions }
151*27b03b36SApple OSS Distributions }
152*27b03b36SApple OSS Distributions if (error == 0) {
153*27b03b36SApple OSS Distributions /* Copy back the interface name with unit number */
154*27b03b36SApple OSS Distributions strlcpy(ifname, ifr.ifr_name, ifname_size);
155*27b03b36SApple OSS Distributions error = ifnet_set_flags(s, ifname, IFF_UP, 0);
156*27b03b36SApple OSS Distributions }
157*27b03b36SApple OSS Distributions return error;
158*27b03b36SApple OSS Distributions }
159*27b03b36SApple OSS Distributions
160*27b03b36SApple OSS Distributions #define MAXBUF 32
161*27b03b36SApple OSS Distributions
162*27b03b36SApple OSS Distributions static void
HexDump(void * data,size_t len)163*27b03b36SApple OSS Distributions HexDump(void *data, size_t len)
164*27b03b36SApple OSS Distributions {
165*27b03b36SApple OSS Distributions size_t i, j, k;
166*27b03b36SApple OSS Distributions unsigned char *ptr = (unsigned char *)data;
167*27b03b36SApple OSS Distributions unsigned char buf[3 * MAXBUF + 1];
168*27b03b36SApple OSS Distributions
169*27b03b36SApple OSS Distributions for (i = 0; i < len; i += MAXBUF) {
170*27b03b36SApple OSS Distributions for (j = i, k = 0; j < i + MAXBUF && j < len; j++) {
171*27b03b36SApple OSS Distributions unsigned char msnbl = ptr[j] >> 4;
172*27b03b36SApple OSS Distributions unsigned char lsnbl = ptr[j] & 0x0f;
173*27b03b36SApple OSS Distributions
174*27b03b36SApple OSS Distributions buf[k++] = msnbl < 10 ? msnbl + '0' : msnbl + 'a' - 10;
175*27b03b36SApple OSS Distributions buf[k++] = lsnbl < 10 ? lsnbl + '0' : lsnbl + 'a' - 10;
176*27b03b36SApple OSS Distributions if ((j % 2) == 1) {
177*27b03b36SApple OSS Distributions buf[k++] = ' ';
178*27b03b36SApple OSS Distributions }
179*27b03b36SApple OSS Distributions if ((j % MAXBUF) == MAXBUF - 1) {
180*27b03b36SApple OSS Distributions buf[k++] = ' ';
181*27b03b36SApple OSS Distributions }
182*27b03b36SApple OSS Distributions }
183*27b03b36SApple OSS Distributions buf[k] = 0;
184*27b03b36SApple OSS Distributions T_LOG("%5zd: %s\n", i, buf);
185*27b03b36SApple OSS Distributions }
186*27b03b36SApple OSS Distributions }
187*27b03b36SApple OSS Distributions
188*27b03b36SApple OSS Distributions static size_t
snprint_dottedhex(char * str,size_t strsize,const void * data,const size_t datasize)189*27b03b36SApple OSS Distributions snprint_dottedhex(char *str, size_t strsize, const void *data, const size_t datasize)
190*27b03b36SApple OSS Distributions {
191*27b03b36SApple OSS Distributions size_t is = 0, ip = 0;
192*27b03b36SApple OSS Distributions const unsigned char *ptr = (const unsigned char *)data;
193*27b03b36SApple OSS Distributions
194*27b03b36SApple OSS Distributions for (is = 0, ip = 0; is + 3 < strsize - 1 && ip < datasize; ip++) {
195*27b03b36SApple OSS Distributions unsigned char msnbl = ptr[ip] >> 4;
196*27b03b36SApple OSS Distributions unsigned char lsnbl = ptr[ip] & 0x0f;
197*27b03b36SApple OSS Distributions
198*27b03b36SApple OSS Distributions if (ip > 0) {
199*27b03b36SApple OSS Distributions str[is++] = '.';
200*27b03b36SApple OSS Distributions }
201*27b03b36SApple OSS Distributions str[is++] = (char)(msnbl + (msnbl < 10 ? '0' : 'a' - 10));
202*27b03b36SApple OSS Distributions str[is++] = (char)(lsnbl + (lsnbl < 10 ? '0' : 'a' - 10));
203*27b03b36SApple OSS Distributions }
204*27b03b36SApple OSS Distributions str[is] = 0;
205*27b03b36SApple OSS Distributions return is;
206*27b03b36SApple OSS Distributions }
207*27b03b36SApple OSS Distributions
208*27b03b36SApple OSS Distributions static void
print_sockaddr_dl(const char * pre,const struct sockaddr * sa,const char * post)209*27b03b36SApple OSS Distributions print_sockaddr_dl(const char *pre, const struct sockaddr *sa, const char *post)
210*27b03b36SApple OSS Distributions {
211*27b03b36SApple OSS Distributions char nbuffer[256];
212*27b03b36SApple OSS Distributions char abuffer[256];
213*27b03b36SApple OSS Distributions char sbuffer[256];
214*27b03b36SApple OSS Distributions struct sockaddr_dl sdl = {};
215*27b03b36SApple OSS Distributions
216*27b03b36SApple OSS Distributions if (sa == NULL) {
217*27b03b36SApple OSS Distributions return;
218*27b03b36SApple OSS Distributions }
219*27b03b36SApple OSS Distributions memcpy(&sdl, sa, MIN(sizeof(sdl), sa->sa_len));
220*27b03b36SApple OSS Distributions strlcpy(nbuffer, sdl.sdl_data, sdl.sdl_nlen);
221*27b03b36SApple OSS Distributions snprint_dottedhex(abuffer, sizeof(abuffer), sdl.sdl_data + sdl.sdl_nlen, sdl.sdl_alen);
222*27b03b36SApple OSS Distributions snprint_dottedhex(sbuffer, sizeof(sbuffer), sdl.sdl_data + sdl.sdl_nlen + sdl.sdl_alen, sdl.sdl_slen);
223*27b03b36SApple OSS Distributions
224*27b03b36SApple OSS Distributions T_LOG("%ssdl_len %u sdl_family %u sdl_index %u sdl_type %u sdl_nlen %u (%s) sdl_alen %u (%s) sdl_slen %u (%s)%s",
225*27b03b36SApple OSS Distributions pre != NULL ? pre : "",
226*27b03b36SApple OSS Distributions sdl.sdl_len, sdl.sdl_family, sdl.sdl_index, sdl.sdl_type,
227*27b03b36SApple OSS Distributions sdl.sdl_nlen, nbuffer, sdl.sdl_alen, abuffer, sdl.sdl_slen, sbuffer,
228*27b03b36SApple OSS Distributions post != NULL ? post : "");
229*27b03b36SApple OSS Distributions }
230*27b03b36SApple OSS Distributions
231*27b03b36SApple OSS Distributions static void
print_sockaddr_in(const char * pre,const struct sockaddr * sa,const char * post)232*27b03b36SApple OSS Distributions print_sockaddr_in(const char *pre, const struct sockaddr *sa, const char *post)
233*27b03b36SApple OSS Distributions {
234*27b03b36SApple OSS Distributions char abuffer[256];
235*27b03b36SApple OSS Distributions char zbuffer[256];
236*27b03b36SApple OSS Distributions struct sockaddr_in sin = {};
237*27b03b36SApple OSS Distributions
238*27b03b36SApple OSS Distributions if (sa == NULL) {
239*27b03b36SApple OSS Distributions return;
240*27b03b36SApple OSS Distributions }
241*27b03b36SApple OSS Distributions
242*27b03b36SApple OSS Distributions memcpy(&sin, sa, MIN(sizeof(sin), sa->sa_len));
243*27b03b36SApple OSS Distributions inet_ntop(AF_INET, &sin.sin_addr, abuffer, sizeof(abuffer));
244*27b03b36SApple OSS Distributions snprint_dottedhex(zbuffer, sizeof(zbuffer), sin.sin_zero, sizeof(sin.sin_zero));
245*27b03b36SApple OSS Distributions
246*27b03b36SApple OSS Distributions T_LOG("%ssin_len %u sin_family %u sin_port %u sin_addr %s sin_zero %s%s",
247*27b03b36SApple OSS Distributions pre != NULL ? pre : "",
248*27b03b36SApple OSS Distributions sin.sin_len, sin.sin_family, htons(sin.sin_port), abuffer, zbuffer,
249*27b03b36SApple OSS Distributions post != NULL ? post : "");
250*27b03b36SApple OSS Distributions }
251*27b03b36SApple OSS Distributions
252*27b03b36SApple OSS Distributions static void
print_sockaddr_in6(const char * pre,const struct sockaddr * sa,const char * post)253*27b03b36SApple OSS Distributions print_sockaddr_in6(const char *pre, const struct sockaddr *sa, const char *post)
254*27b03b36SApple OSS Distributions {
255*27b03b36SApple OSS Distributions char abuffer[256];
256*27b03b36SApple OSS Distributions struct sockaddr_in6 sin6 = {};
257*27b03b36SApple OSS Distributions
258*27b03b36SApple OSS Distributions if (sa == NULL) {
259*27b03b36SApple OSS Distributions return;
260*27b03b36SApple OSS Distributions }
261*27b03b36SApple OSS Distributions
262*27b03b36SApple OSS Distributions memcpy(&sin6, sa, MIN(sizeof(sin6), sa->sa_len));
263*27b03b36SApple OSS Distributions inet_ntop(AF_INET6, &sin6.sin6_addr, abuffer, sizeof(abuffer));
264*27b03b36SApple OSS Distributions
265*27b03b36SApple OSS Distributions T_LOG("%ssin6_len %u sin6_family %u sin6_port %u sin6_flowinfo %u sin6_addr %s sin6_scope_id %u%s",
266*27b03b36SApple OSS Distributions pre != NULL ? pre : "",
267*27b03b36SApple OSS Distributions sin6.sin6_len, sin6.sin6_family, htons(sin6.sin6_port), sin6.sin6_flowinfo, abuffer, sin6.sin6_scope_id,
268*27b03b36SApple OSS Distributions post != NULL ? post : "");
269*27b03b36SApple OSS Distributions }
270*27b03b36SApple OSS Distributions
271*27b03b36SApple OSS Distributions static void
print_sockaddr(const char * pre,const struct sockaddr * sa,const char * post)272*27b03b36SApple OSS Distributions print_sockaddr(const char *pre, const struct sockaddr *sa, const char *post)
273*27b03b36SApple OSS Distributions {
274*27b03b36SApple OSS Distributions char buffer[256];
275*27b03b36SApple OSS Distributions
276*27b03b36SApple OSS Distributions if (sa == NULL) {
277*27b03b36SApple OSS Distributions return;
278*27b03b36SApple OSS Distributions }
279*27b03b36SApple OSS Distributions
280*27b03b36SApple OSS Distributions snprint_dottedhex(buffer, sizeof(buffer), sa->sa_data, sa->sa_len - 2);
281*27b03b36SApple OSS Distributions
282*27b03b36SApple OSS Distributions T_LOG("%ssa_len %u sa_family %u sa_data %s%s",
283*27b03b36SApple OSS Distributions pre != NULL ? pre : "",
284*27b03b36SApple OSS Distributions sa->sa_len, sa->sa_family, buffer,
285*27b03b36SApple OSS Distributions post != NULL ? post : "");
286*27b03b36SApple OSS Distributions }
287*27b03b36SApple OSS Distributions
288*27b03b36SApple OSS Distributions
289*27b03b36SApple OSS Distributions #define ROUNDUP(a, size) (((a) & ((size) - 1)) ? (1 + ((a)|(size - 1))) : (a))
290*27b03b36SApple OSS Distributions
291*27b03b36SApple OSS Distributions #define NEXT_SA(p) (struct sockaddr *) \
292*27b03b36SApple OSS Distributions ((caddr_t)p + (p->sa_len ? ROUNDUP(p->sa_len, sizeof(u_int32_t)) : \
293*27b03b36SApple OSS Distributions sizeof(u_long)))
294*27b03b36SApple OSS Distributions
295*27b03b36SApple OSS Distributions static size_t
get_rti_info(int addrs,struct sockaddr * sa,struct sockaddr ** rti_info)296*27b03b36SApple OSS Distributions get_rti_info(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
297*27b03b36SApple OSS Distributions {
298*27b03b36SApple OSS Distributions int i;
299*27b03b36SApple OSS Distributions size_t len = 0;
300*27b03b36SApple OSS Distributions
301*27b03b36SApple OSS Distributions for (i = 0; i < RTAX_MAX; i++) {
302*27b03b36SApple OSS Distributions if (addrs & (1 << i)) {
303*27b03b36SApple OSS Distributions rti_info[i] = sa;
304*27b03b36SApple OSS Distributions if (sa->sa_len < sizeof(struct sockaddr)) {
305*27b03b36SApple OSS Distributions len += sizeof(struct sockaddr);
306*27b03b36SApple OSS Distributions } else {
307*27b03b36SApple OSS Distributions len += sa->sa_len;
308*27b03b36SApple OSS Distributions }
309*27b03b36SApple OSS Distributions sa = NEXT_SA(sa);
310*27b03b36SApple OSS Distributions } else {
311*27b03b36SApple OSS Distributions rti_info[i] = NULL;
312*27b03b36SApple OSS Distributions }
313*27b03b36SApple OSS Distributions }
314*27b03b36SApple OSS Distributions return len;
315*27b03b36SApple OSS Distributions }
316*27b03b36SApple OSS Distributions
317*27b03b36SApple OSS Distributions static void
print_address(const char * pre,const struct sockaddr * sa,const char * post,u_char asFamily)318*27b03b36SApple OSS Distributions print_address(const char *pre, const struct sockaddr *sa, const char *post, u_char asFamily)
319*27b03b36SApple OSS Distributions {
320*27b03b36SApple OSS Distributions if (sa == NULL) {
321*27b03b36SApple OSS Distributions T_LOG("%s(NULL)%s",
322*27b03b36SApple OSS Distributions pre != NULL ? pre : "",
323*27b03b36SApple OSS Distributions post != NULL ? post : "");
324*27b03b36SApple OSS Distributions return;
325*27b03b36SApple OSS Distributions }
326*27b03b36SApple OSS Distributions if (sa->sa_len == 0) {
327*27b03b36SApple OSS Distributions T_LOG("%ssa_len 0%s",
328*27b03b36SApple OSS Distributions pre != NULL ? pre : "",
329*27b03b36SApple OSS Distributions post != NULL ? post : "");
330*27b03b36SApple OSS Distributions return;
331*27b03b36SApple OSS Distributions }
332*27b03b36SApple OSS Distributions if (sa->sa_len == 1) {
333*27b03b36SApple OSS Distributions T_LOG("%ssa_len 1%s",
334*27b03b36SApple OSS Distributions pre != NULL ? pre : "",
335*27b03b36SApple OSS Distributions post != NULL ? post : "");
336*27b03b36SApple OSS Distributions return;
337*27b03b36SApple OSS Distributions }
338*27b03b36SApple OSS Distributions
339*27b03b36SApple OSS Distributions // If not forced
340*27b03b36SApple OSS Distributions if (asFamily == AF_UNSPEC) {
341*27b03b36SApple OSS Distributions asFamily = sa->sa_family;
342*27b03b36SApple OSS Distributions }
343*27b03b36SApple OSS Distributions switch (asFamily) {
344*27b03b36SApple OSS Distributions case AF_INET: {
345*27b03b36SApple OSS Distributions print_sockaddr_in(pre, sa, post);
346*27b03b36SApple OSS Distributions break;
347*27b03b36SApple OSS Distributions }
348*27b03b36SApple OSS Distributions case AF_INET6: {
349*27b03b36SApple OSS Distributions print_sockaddr_in6(pre, sa, post);
350*27b03b36SApple OSS Distributions break;
351*27b03b36SApple OSS Distributions }
352*27b03b36SApple OSS Distributions case AF_LINK: {
353*27b03b36SApple OSS Distributions print_sockaddr_dl(pre, sa, post);
354*27b03b36SApple OSS Distributions break;
355*27b03b36SApple OSS Distributions }
356*27b03b36SApple OSS Distributions default:
357*27b03b36SApple OSS Distributions print_sockaddr(pre, sa, post);
358*27b03b36SApple OSS Distributions break;
359*27b03b36SApple OSS Distributions }
360*27b03b36SApple OSS Distributions }
361*27b03b36SApple OSS Distributions
362*27b03b36SApple OSS Distributions static void
print_rti_info(struct sockaddr * rti_info[])363*27b03b36SApple OSS Distributions print_rti_info(struct sockaddr *rti_info[])
364*27b03b36SApple OSS Distributions {
365*27b03b36SApple OSS Distributions struct sockaddr *sa;
366*27b03b36SApple OSS Distributions u_char asFamily = 0;
367*27b03b36SApple OSS Distributions
368*27b03b36SApple OSS Distributions if ((sa = rti_info[RTAX_IFA])) {
369*27b03b36SApple OSS Distributions asFamily = sa->sa_family;
370*27b03b36SApple OSS Distributions print_address(" RTAX_IFA ", sa, "\n", 0);
371*27b03b36SApple OSS Distributions }
372*27b03b36SApple OSS Distributions if ((sa = rti_info[RTAX_DST])) {
373*27b03b36SApple OSS Distributions asFamily = sa->sa_family;
374*27b03b36SApple OSS Distributions print_address(" RTAX_DST ", sa, "\n", 0);
375*27b03b36SApple OSS Distributions }
376*27b03b36SApple OSS Distributions if ((sa = rti_info[RTAX_BRD])) {
377*27b03b36SApple OSS Distributions print_address(" RTAX_BRD ", sa, "\n", asFamily);
378*27b03b36SApple OSS Distributions }
379*27b03b36SApple OSS Distributions
380*27b03b36SApple OSS Distributions if ((sa = rti_info[RTAX_NETMASK])) {
381*27b03b36SApple OSS Distributions print_address(" RTAX_NETMASK ", sa, "\n", asFamily);
382*27b03b36SApple OSS Distributions }
383*27b03b36SApple OSS Distributions
384*27b03b36SApple OSS Distributions if ((sa = rti_info[RTAX_GATEWAY])) {
385*27b03b36SApple OSS Distributions print_address(" RTAX_GATEWAY ", sa, "\n", 0);
386*27b03b36SApple OSS Distributions }
387*27b03b36SApple OSS Distributions
388*27b03b36SApple OSS Distributions if ((sa = rti_info[RTAX_GENMASK])) {
389*27b03b36SApple OSS Distributions print_address(" RTAX_GENMASK ", sa, "\n", asFamily);
390*27b03b36SApple OSS Distributions }
391*27b03b36SApple OSS Distributions
392*27b03b36SApple OSS Distributions if ((sa = rti_info[RTAX_AUTHOR])) {
393*27b03b36SApple OSS Distributions print_address(" RTAX_AUTHOR ", sa, "\n", asFamily);
394*27b03b36SApple OSS Distributions }
395*27b03b36SApple OSS Distributions
396*27b03b36SApple OSS Distributions if ((sa = rti_info[RTAX_IFP])) {
397*27b03b36SApple OSS Distributions print_address(" RTAX_IFP ", sa, "\n", 0);
398*27b03b36SApple OSS Distributions }
399*27b03b36SApple OSS Distributions }
400*27b03b36SApple OSS Distributions
401*27b03b36SApple OSS Distributions static void
print_rt_iflist2(const char * label)402*27b03b36SApple OSS Distributions print_rt_iflist2(const char *label)
403*27b03b36SApple OSS Distributions {
404*27b03b36SApple OSS Distributions size_t len;
405*27b03b36SApple OSS Distributions int mib[6] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0 };
406*27b03b36SApple OSS Distributions unsigned char *buf = NULL;
407*27b03b36SApple OSS Distributions unsigned char *lim, *next;
408*27b03b36SApple OSS Distributions struct if_msghdr *ifmsg;
409*27b03b36SApple OSS Distributions
410*27b03b36SApple OSS Distributions T_LOG("interface address list for %s", label);
411*27b03b36SApple OSS Distributions
412*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(sysctl(mib, 6, NULL, &len, NULL, 0), "sysctl NET_RT_IFLIST2");
413*27b03b36SApple OSS Distributions
414*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(buf = calloc(1, len), "rt_if_list_buf calloc(1, %zd)", len);
415*27b03b36SApple OSS Distributions
416*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(sysctl(mib, 6, buf, &len, NULL, 0), "sysctl NET_RT_IFLIST2");
417*27b03b36SApple OSS Distributions
418*27b03b36SApple OSS Distributions lim = buf + len;
419*27b03b36SApple OSS Distributions for (next = buf; next < lim; next += ifmsg->ifm_msglen) {
420*27b03b36SApple OSS Distributions ifmsg = (struct if_msghdr *)(void *)next;
421*27b03b36SApple OSS Distributions char ifname[IF_NAMESIZE + 1];
422*27b03b36SApple OSS Distributions
423*27b03b36SApple OSS Distributions if (ifmsg->ifm_type == RTM_IFINFO2) {
424*27b03b36SApple OSS Distributions struct if_msghdr2 *ifm = (struct if_msghdr2 *)ifmsg;
425*27b03b36SApple OSS Distributions struct sockaddr *sa = (struct sockaddr *)(ifm + 1);
426*27b03b36SApple OSS Distributions
427*27b03b36SApple OSS Distributions (void)if_indextoname(ifm->ifm_index, ifname);
428*27b03b36SApple OSS Distributions T_LOG("interface: %s", ifname);
429*27b03b36SApple OSS Distributions print_address(" PRIMARY ", sa, "", 0);
430*27b03b36SApple OSS Distributions } else if (ifmsg->ifm_type == RTM_NEWADDR) {
431*27b03b36SApple OSS Distributions struct sockaddr *rti_info[RTAX_MAX];
432*27b03b36SApple OSS Distributions struct ifa_msghdr *ifam = (struct ifa_msghdr *)ifmsg;
433*27b03b36SApple OSS Distributions
434*27b03b36SApple OSS Distributions (void) get_rti_info(ifam->ifam_addrs, (struct sockaddr *)(ifam + 1), rti_info);
435*27b03b36SApple OSS Distributions
436*27b03b36SApple OSS Distributions print_rti_info(rti_info);
437*27b03b36SApple OSS Distributions }
438*27b03b36SApple OSS Distributions }
439*27b03b36SApple OSS Distributions free(buf);
440*27b03b36SApple OSS Distributions }
441*27b03b36SApple OSS Distributions
442*27b03b36SApple OSS Distributions static int
check_rt_if_list_for_pattern(const char * label,unsigned char pattern,size_t pattern_size)443*27b03b36SApple OSS Distributions check_rt_if_list_for_pattern(const char *label, unsigned char pattern, size_t pattern_size)
444*27b03b36SApple OSS Distributions {
445*27b03b36SApple OSS Distributions size_t i;
446*27b03b36SApple OSS Distributions size_t len;
447*27b03b36SApple OSS Distributions int mib[6] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
448*27b03b36SApple OSS Distributions unsigned char *rt_if_list_buf = NULL;
449*27b03b36SApple OSS Distributions int count = 0;
450*27b03b36SApple OSS Distributions unsigned char *pattern_buf = NULL;
451*27b03b36SApple OSS Distributions
452*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(pattern_buf = calloc(1, pattern_size), "pattern_buf calloc(1, %zd)", pattern_size);
453*27b03b36SApple OSS Distributions memset(pattern_buf, pattern, pattern_size);
454*27b03b36SApple OSS Distributions
455*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(sysctl(mib, 6, NULL, &len, NULL, 0), "sysctl NET_RT_IFLIST");
456*27b03b36SApple OSS Distributions
457*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(rt_if_list_buf = calloc(1, len), "rt_if_list_buf calloc(1, %zd)", len);
458*27b03b36SApple OSS Distributions
459*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(sysctl(mib, 6, rt_if_list_buf, &len, NULL, 0), "sysctl NET_RT_IFLIST");
460*27b03b36SApple OSS Distributions
461*27b03b36SApple OSS Distributions if (label != NULL) {
462*27b03b36SApple OSS Distributions T_LOG("%s sysctl NET_RT_IFLIST buffer length %zd\n", label, len);
463*27b03b36SApple OSS Distributions }
464*27b03b36SApple OSS Distributions
465*27b03b36SApple OSS Distributions count = 0;
466*27b03b36SApple OSS Distributions for (i = 0; i < len - pattern_size; i++) {
467*27b03b36SApple OSS Distributions if (memcmp(rt_if_list_buf + i, pattern_buf, pattern_size) == 0) {
468*27b03b36SApple OSS Distributions count++;
469*27b03b36SApple OSS Distributions i += pattern_size - 1;
470*27b03b36SApple OSS Distributions }
471*27b03b36SApple OSS Distributions }
472*27b03b36SApple OSS Distributions
473*27b03b36SApple OSS Distributions if (label != NULL) {
474*27b03b36SApple OSS Distributions if (label != NULL && count > 0) {
475*27b03b36SApple OSS Distributions T_LOG("%s found pattern at %zd count %d times\n", label, i, count);
476*27b03b36SApple OSS Distributions HexDump(rt_if_list_buf, len);
477*27b03b36SApple OSS Distributions }
478*27b03b36SApple OSS Distributions }
479*27b03b36SApple OSS Distributions
480*27b03b36SApple OSS Distributions free(rt_if_list_buf);
481*27b03b36SApple OSS Distributions free(pattern_buf);
482*27b03b36SApple OSS Distributions
483*27b03b36SApple OSS Distributions return count;
484*27b03b36SApple OSS Distributions }
485*27b03b36SApple OSS Distributions
486*27b03b36SApple OSS Distributions static bool
find_unused_pattern_in_rt_if_list(unsigned char * pattern,size_t pattern_size)487*27b03b36SApple OSS Distributions find_unused_pattern_in_rt_if_list(unsigned char *pattern, size_t pattern_size)
488*27b03b36SApple OSS Distributions {
489*27b03b36SApple OSS Distributions bool found_pattern = false;
490*27b03b36SApple OSS Distributions unsigned char i;
491*27b03b36SApple OSS Distributions unsigned char *pattern_buf = NULL;
492*27b03b36SApple OSS Distributions
493*27b03b36SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(pattern_buf = calloc(1, pattern_size), "pattern_buf calloc(1, %zd)", pattern_size);
494*27b03b36SApple OSS Distributions
495*27b03b36SApple OSS Distributions /* Try 10 times to find an unused pattern */
496*27b03b36SApple OSS Distributions for (i = 1; i < 255; i++) {
497*27b03b36SApple OSS Distributions if (check_rt_if_list_for_pattern(NULL, i, pattern_size) == 0) {
498*27b03b36SApple OSS Distributions found_pattern = true;
499*27b03b36SApple OSS Distributions *pattern = i;
500*27b03b36SApple OSS Distributions memset(pattern_buf, i, pattern_size);
501*27b03b36SApple OSS Distributions T_LOG("PATTERN: ");
502*27b03b36SApple OSS Distributions HexDump(pattern_buf, pattern_size);
503*27b03b36SApple OSS Distributions break;
504*27b03b36SApple OSS Distributions }
505*27b03b36SApple OSS Distributions }
506*27b03b36SApple OSS Distributions free(pattern_buf);
507*27b03b36SApple OSS Distributions
508*27b03b36SApple OSS Distributions return found_pattern;
509*27b03b36SApple OSS Distributions }
510*27b03b36SApple OSS Distributions
511*27b03b36SApple OSS Distributions static const char *
ioc_str(unsigned long cmd)512*27b03b36SApple OSS Distributions ioc_str(unsigned long cmd)
513*27b03b36SApple OSS Distributions {
514*27b03b36SApple OSS Distributions #define X(a) case a: return #a;
515*27b03b36SApple OSS Distributions
516*27b03b36SApple OSS Distributions switch (cmd) {
517*27b03b36SApple OSS Distributions SIOC_LIST
518*27b03b36SApple OSS Distributions
519*27b03b36SApple OSS Distributions default:
520*27b03b36SApple OSS Distributions break;
521*27b03b36SApple OSS Distributions }
522*27b03b36SApple OSS Distributions return "";
523*27b03b36SApple OSS Distributions }
524*27b03b36SApple OSS Distributions
525*27b03b36SApple OSS Distributions struct ioc_ifreq {
526*27b03b36SApple OSS Distributions unsigned long ioc_cmd;
527*27b03b36SApple OSS Distributions uint8_t salen;
528*27b03b36SApple OSS Distributions uint8_t safamily;
529*27b03b36SApple OSS Distributions const char *sastr;
530*27b03b36SApple OSS Distributions int error; // 0 means no error, -1, end of list, otherwise expected errno
531*27b03b36SApple OSS Distributions };
532*27b03b36SApple OSS Distributions
533*27b03b36SApple OSS Distributions static struct ioc_ifreq ioc_list[] = {
534*27b03b36SApple OSS Distributions { SIOCSIFADDR, sizeof(struct sockaddr_in), AF_INET6, "10.2.3.1", EINVAL },
535*27b03b36SApple OSS Distributions { SIOCDIFADDR, sizeof(struct sockaddr_in), AF_INET6, "10.2.3.1", EADDRNOTAVAIL },
536*27b03b36SApple OSS Distributions { SIOCDIFADDR, sizeof(struct sockaddr_in6), AF_INET, "10.2.3.1", EADDRNOTAVAIL },
537*27b03b36SApple OSS Distributions { SIOCDIFADDR, sizeof(struct sockaddr_in), AF_INET, "10.2.3.1", EADDRNOTAVAIL },
538*27b03b36SApple OSS Distributions
539*27b03b36SApple OSS Distributions { SIOCSIFADDR, 0xf0, AF_INET6, "10.2.3.1", EINVAL },
540*27b03b36SApple OSS Distributions { SIOCDIFADDR, 0xf0, AF_INET6, "10.2.3.1", EADDRNOTAVAIL },
541*27b03b36SApple OSS Distributions { SIOCDIFADDR, 0xf0, AF_INET, "10.2.3.1", EADDRNOTAVAIL },
542*27b03b36SApple OSS Distributions { SIOCDIFADDR, 0xf0, AF_INET, "10.2.3.1", EADDRNOTAVAIL },
543*27b03b36SApple OSS Distributions
544*27b03b36SApple OSS Distributions { SIOCSIFADDR, 0, AF_INET6, "10.2.3.1", EINVAL },
545*27b03b36SApple OSS Distributions { SIOCDIFADDR, 0, AF_INET6, "10.2.3.1", EADDRNOTAVAIL },
546*27b03b36SApple OSS Distributions { SIOCDIFADDR, 0, AF_INET, "10.2.3.1", EADDRNOTAVAIL },
547*27b03b36SApple OSS Distributions { SIOCDIFADDR, 0, AF_INET, "10.2.3.1", EADDRNOTAVAIL },
548*27b03b36SApple OSS Distributions
549*27b03b36SApple OSS Distributions { SIOCSIFADDR, sizeof(struct sockaddr_in6), AF_INET, "10.2.3.2", 0 },
550*27b03b36SApple OSS Distributions { SIOCDIFADDR, sizeof(struct sockaddr_in), AF_INET6, "10.2.3.2", 0 },
551*27b03b36SApple OSS Distributions
552*27b03b36SApple OSS Distributions { SIOCSIFADDR, sizeof(struct sockaddr_in), AF_INET, "10.2.3.3", 0 },
553*27b03b36SApple OSS Distributions { SIOCDIFADDR, sizeof(struct sockaddr_in6), AF_INET, "10.2.3.3", 0 },
554*27b03b36SApple OSS Distributions
555*27b03b36SApple OSS Distributions { SIOCSIFADDR, sizeof(struct sockaddr_in6), AF_INET6, "10.2.3.4", EINVAL },
556*27b03b36SApple OSS Distributions { SIOCDIFADDR, sizeof(struct sockaddr_in6), AF_INET, "10.2.3.4", EADDRNOTAVAIL },
557*27b03b36SApple OSS Distributions
558*27b03b36SApple OSS Distributions { SIOCSIFADDR, sizeof(struct sockaddr_in), AF_INET, "10.2.3.5", 0 },
559*27b03b36SApple OSS Distributions { SIOCDIFADDR, sizeof(struct sockaddr_in), AF_INET, "0.0.0.0", EADDRNOTAVAIL },
560*27b03b36SApple OSS Distributions { SIOCDIFADDR, sizeof(struct sockaddr_in), AF_INET, "10.2.3.5", 0 },
561*27b03b36SApple OSS Distributions
562*27b03b36SApple OSS Distributions { SIOCSIFADDR, sizeof(struct sockaddr_in), AF_INET, "10.2.3.6", 0 },
563*27b03b36SApple OSS Distributions
564*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, sizeof(struct sockaddr_in), 0, "ff.00.00.00", 0 },
565*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, sizeof(struct sockaddr_in), AF_INET, "ff.00.00.00", 0 },
566*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, sizeof(struct sockaddr_in), AF_INET6, "ff.f.00.00", 0 },
567*27b03b36SApple OSS Distributions
568*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, sizeof(struct sockaddr_in6), 0, "ff.ff.00.00", 0 },
569*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, sizeof(struct sockaddr_in6), AF_INET, "ff.ff.00.00", 0 },
570*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, sizeof(struct sockaddr_in6), AF_INET6, "ff.ff.f0.00", 0 },
571*27b03b36SApple OSS Distributions
572*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, 0, 0, "ff.ff.00.00", 0 },
573*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, 0, AF_INET, "ff.ff.00.00", 0 },
574*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, 0, AF_INET6, "ff.ff.f0.00", 0 },
575*27b03b36SApple OSS Distributions
576*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, 0xf0, 0, "ff.ff.00.00", 0 },
577*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, 0xf0, AF_INET, "ff.ff.00.00", 0 },
578*27b03b36SApple OSS Distributions { SIOCSIFNETMASK, 0xf0, AF_INET6, "ff.ff.f0.00", 0 },
579*27b03b36SApple OSS Distributions
580*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, sizeof(struct sockaddr_in), 0, "10.255.255.255", 0 },
581*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, sizeof(struct sockaddr_in), AF_INET, "10.255.255.255", 0 },
582*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, sizeof(struct sockaddr_in), AF_INET6, "10.255.255.255", 0 },
583*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, sizeof(struct sockaddr_in6), AF_INET, "10.255.255.255", 0 },
584*27b03b36SApple OSS Distributions
585*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, 0xf0, 0, "10.255.255.255", 0 },
586*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, 0xf0, AF_INET, "10.255.255.255", 0 },
587*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, 0xf0, AF_INET6, "10.255.255.255", 0 },
588*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, 0xf0, AF_INET, "10.255.255.255", 0 },
589*27b03b36SApple OSS Distributions
590*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, 0, 0, "10.255.255.255", 0 },
591*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, 0, AF_INET, "10.255.255.255", 0 },
592*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, 0, AF_INET6, "10.255.255.255", 0 },
593*27b03b36SApple OSS Distributions { SIOCSIFBRDADDR, 0, AF_INET, "10.255.255.255", 0 },
594*27b03b36SApple OSS Distributions
595*27b03b36SApple OSS Distributions { 0, 0, 0, "", -1 },
596*27b03b36SApple OSS Distributions };
597*27b03b36SApple OSS Distributions
598*27b03b36SApple OSS Distributions static void
test_sioc_ifr_bounds(struct ioc_ifreq * ioc_ifreq,int s,const char * ifname)599*27b03b36SApple OSS Distributions test_sioc_ifr_bounds(struct ioc_ifreq *ioc_ifreq, int s, const char *ifname)
600*27b03b36SApple OSS Distributions {
601*27b03b36SApple OSS Distributions struct ifreq ifr = {};
602*27b03b36SApple OSS Distributions unsigned char pattern;
603*27b03b36SApple OSS Distributions struct sockaddr_in *sin;
604*27b03b36SApple OSS Distributions
605*27b03b36SApple OSS Distributions T_LOG("");
606*27b03b36SApple OSS Distributions T_LOG("TEST CASE: %s ioctl(%s, sa_len %u, sa_family %u, %s) -> %d", __func__,
607*27b03b36SApple OSS Distributions ioc_str(ioc_ifreq->ioc_cmd), ioc_ifreq->salen, ioc_ifreq->safamily, ioc_ifreq->sastr, ioc_ifreq->error);
608*27b03b36SApple OSS Distributions
609*27b03b36SApple OSS Distributions
610*27b03b36SApple OSS Distributions if (find_unused_pattern_in_rt_if_list(&pattern, PATTERN_SIZE) == false) {
611*27b03b36SApple OSS Distributions T_SKIP("Could not find unused pattern");
612*27b03b36SApple OSS Distributions }
613*27b03b36SApple OSS Distributions
614*27b03b36SApple OSS Distributions strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
615*27b03b36SApple OSS Distributions memset(&ifr.ifr_addr.sa_data, pattern, sizeof(ifr.ifr_dstaddr.sa_data));
616*27b03b36SApple OSS Distributions
617*27b03b36SApple OSS Distributions sin = (struct sockaddr_in *)(void *)&ifr.ifr_addr;
618*27b03b36SApple OSS Distributions sin->sin_len = ioc_ifreq->salen;
619*27b03b36SApple OSS Distributions sin->sin_family = ioc_ifreq->safamily;
620*27b03b36SApple OSS Distributions sin->sin_addr.s_addr = inet_addr(ioc_ifreq->sastr);
621*27b03b36SApple OSS Distributions
622*27b03b36SApple OSS Distributions int retval;
623*27b03b36SApple OSS Distributions if (ioc_ifreq->error == 0) {
624*27b03b36SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(retval = ioctl(s, ioc_ifreq->ioc_cmd, &ifr),
625*27b03b36SApple OSS Distributions "%s, %s: retval %d", ioc_str(ioc_ifreq->ioc_cmd), ioc_ifreq->sastr, retval);
626*27b03b36SApple OSS Distributions } else {
627*27b03b36SApple OSS Distributions T_EXPECT_POSIX_FAILURE(retval = ioctl(s, ioc_ifreq->ioc_cmd, &ifr), ioc_ifreq->error,
628*27b03b36SApple OSS Distributions "%s, %s: retval %d errno %s", ioc_str(ioc_ifreq->ioc_cmd), ioc_ifreq->sastr, retval, strerror(errno));
629*27b03b36SApple OSS Distributions }
630*27b03b36SApple OSS Distributions
631*27b03b36SApple OSS Distributions T_EXPECT_EQ(check_rt_if_list_for_pattern("test_sioc_ifr_bounds", pattern, PATTERN_SIZE), 0, "pattern should not be found");
632*27b03b36SApple OSS Distributions
633*27b03b36SApple OSS Distributions
634*27b03b36SApple OSS Distributions fflush(stdout);
635*27b03b36SApple OSS Distributions fflush(stderr);
636*27b03b36SApple OSS Distributions }
637*27b03b36SApple OSS Distributions
638*27b03b36SApple OSS Distributions T_DECL(sioc_ifr_bounds, "test bound checks on struct ifreq addresses passed to interface ioctls",
639*27b03b36SApple OSS Distributions T_META_ASROOT(true))
640*27b03b36SApple OSS Distributions {
641*27b03b36SApple OSS Distributions int s = -1;
642*27b03b36SApple OSS Distributions char ifname[IFNAMSIZ];
643*27b03b36SApple OSS Distributions
644*27b03b36SApple OSS Distributions T_LOG("%s", __func__);
645*27b03b36SApple OSS Distributions
646*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(s = socket(AF_INET, SOCK_DGRAM, 0), "socket");
647*27b03b36SApple OSS Distributions
648*27b03b36SApple OSS Distributions strlcpy(ifname, IF_NAME, sizeof(ifname));
649*27b03b36SApple OSS Distributions
650*27b03b36SApple OSS Distributions int error = 0;
651*27b03b36SApple OSS Distributions if ((error = ifnet_create(s, ifname, sizeof(ifname))) != 0) {
652*27b03b36SApple OSS Distributions if (error == EINVAL) {
653*27b03b36SApple OSS Distributions T_SKIP("The system does not support the %s cloning interface", IF_NAME);
654*27b03b36SApple OSS Distributions }
655*27b03b36SApple OSS Distributions T_SKIP("This test failed creating a %s cloning interface", IF_NAME);
656*27b03b36SApple OSS Distributions }
657*27b03b36SApple OSS Distributions T_LOG("created clone interface '%s'", ifname);
658*27b03b36SApple OSS Distributions
659*27b03b36SApple OSS Distributions struct ioc_ifreq *ioc_ifreq;
660*27b03b36SApple OSS Distributions for (ioc_ifreq = ioc_list; ioc_ifreq->error != -1; ioc_ifreq++) {
661*27b03b36SApple OSS Distributions test_sioc_ifr_bounds(ioc_ifreq, s, ifname);
662*27b03b36SApple OSS Distributions }
663*27b03b36SApple OSS Distributions print_rt_iflist2(__func__);
664*27b03b36SApple OSS Distributions (void)ifnet_destroy(s, ifname, true);
665*27b03b36SApple OSS Distributions
666*27b03b36SApple OSS Distributions close(s);
667*27b03b36SApple OSS Distributions }
668*27b03b36SApple OSS Distributions
669*27b03b36SApple OSS Distributions struct ioc_ifra {
670*27b03b36SApple OSS Distributions const char *description;
671*27b03b36SApple OSS Distributions
672*27b03b36SApple OSS Distributions uint8_t addr_len;
673*27b03b36SApple OSS Distributions uint8_t addr_fam;
674*27b03b36SApple OSS Distributions const char *addr_str;
675*27b03b36SApple OSS Distributions
676*27b03b36SApple OSS Distributions uint8_t broad_len;
677*27b03b36SApple OSS Distributions uint8_t broad_fam;
678*27b03b36SApple OSS Distributions const char *broad_str;
679*27b03b36SApple OSS Distributions
680*27b03b36SApple OSS Distributions uint8_t mask_len;
681*27b03b36SApple OSS Distributions uint8_t mask_fam;
682*27b03b36SApple OSS Distributions const char *mask_str;
683*27b03b36SApple OSS Distributions
684*27b03b36SApple OSS Distributions int error; // 0 means no error, -1, end of list, otherwise expected errno
685*27b03b36SApple OSS Distributions };
686*27b03b36SApple OSS Distributions
687*27b03b36SApple OSS Distributions static struct ioc_ifra ioc_ifra_list[] = {
688*27b03b36SApple OSS Distributions {
689*27b03b36SApple OSS Distributions .description = "fully formed",
690*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.1.1.1",
691*27b03b36SApple OSS Distributions .broad_len = sizeof(struct sockaddr_in), .broad_fam = AF_INET, .broad_str = "1.1.1.255",
692*27b03b36SApple OSS Distributions .mask_len = sizeof(struct sockaddr_in), .mask_fam = AF_INET, .mask_str = "255.255.0.0",
693*27b03b36SApple OSS Distributions .error = 0
694*27b03b36SApple OSS Distributions },
695*27b03b36SApple OSS Distributions {
696*27b03b36SApple OSS Distributions .description = "addr_len 0",
697*27b03b36SApple OSS Distributions .addr_len = 0, .addr_fam = AF_INET, .addr_str = "10.2.2.0",
698*27b03b36SApple OSS Distributions .broad_len = sizeof(struct sockaddr_in), .broad_fam = AF_INET, .broad_str = "10.2.2.255",
699*27b03b36SApple OSS Distributions .mask_len = sizeof(struct sockaddr_in), .mask_fam = AF_INET, .mask_str = "255.0.0.0",
700*27b03b36SApple OSS Distributions .error = 0
701*27b03b36SApple OSS Distributions },
702*27b03b36SApple OSS Distributions {
703*27b03b36SApple OSS Distributions .description = "addr_len 1",
704*27b03b36SApple OSS Distributions .addr_len = 1, .addr_fam = AF_INET, .addr_str = "10.2.2.1",
705*27b03b36SApple OSS Distributions .broad_len = sizeof(struct sockaddr_in), .broad_fam = AF_INET, .broad_str = "10.2.2.255",
706*27b03b36SApple OSS Distributions .mask_len = sizeof(struct sockaddr_in), .mask_fam = AF_INET, .mask_str = "255.0.0.0",
707*27b03b36SApple OSS Distributions .error = 0
708*27b03b36SApple OSS Distributions },
709*27b03b36SApple OSS Distributions {
710*27b03b36SApple OSS Distributions .description = "addr_len 250",
711*27b03b36SApple OSS Distributions .addr_len = 250, .addr_fam = AF_INET, .addr_str = "10.2.2.250",
712*27b03b36SApple OSS Distributions .broad_len = sizeof(struct sockaddr_in), .broad_fam = AF_INET, .broad_str = "10.2.2.255",
713*27b03b36SApple OSS Distributions .mask_len = sizeof(struct sockaddr_in), .mask_fam = AF_INET, .mask_str = "255.0.0.0",
714*27b03b36SApple OSS Distributions .error = 0
715*27b03b36SApple OSS Distributions },
716*27b03b36SApple OSS Distributions {
717*27b03b36SApple OSS Distributions .description = "addr_family AF_INET6",
718*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET6, .addr_str = "10.3.3.3",
719*27b03b36SApple OSS Distributions .broad_len = sizeof(struct sockaddr_in), .broad_fam = AF_INET, .broad_str = "10.3.255.255",
720*27b03b36SApple OSS Distributions .mask_len = sizeof(struct sockaddr_in), .mask_fam = AF_INET, .mask_str = "255.255.255.0",
721*27b03b36SApple OSS Distributions .error = EINVAL
722*27b03b36SApple OSS Distributions },
723*27b03b36SApple OSS Distributions {
724*27b03b36SApple OSS Distributions .description = "broadcast_len 0xf0",
725*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.4.4.4",
726*27b03b36SApple OSS Distributions .broad_len = 0xf0, .broad_fam = AF_INET, .broad_str = "10.4.4.255",
727*27b03b36SApple OSS Distributions .mask_len = sizeof(struct sockaddr_in), .mask_fam = AF_INET, .mask_str = "255.255.255.0",
728*27b03b36SApple OSS Distributions .error = 0
729*27b03b36SApple OSS Distributions },
730*27b03b36SApple OSS Distributions {
731*27b03b36SApple OSS Distributions .description = "broadcast_family AF_INET6",
732*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.5.5.5",
733*27b03b36SApple OSS Distributions .broad_len = sizeof(struct sockaddr_in), .broad_fam = AF_INET6, .broad_str = "10.5.5.255",
734*27b03b36SApple OSS Distributions .mask_len = sizeof(struct sockaddr_in), .mask_fam = AF_INET, .mask_str = "255.255.0.0",
735*27b03b36SApple OSS Distributions .error = 0
736*27b03b36SApple OSS Distributions },
737*27b03b36SApple OSS Distributions {
738*27b03b36SApple OSS Distributions .description = "mask_len 0xf0",
739*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.6.6.6",
740*27b03b36SApple OSS Distributions .broad_len = sizeof(struct sockaddr_in), .broad_fam = AF_INET, .broad_str = "1.6.6.255",
741*27b03b36SApple OSS Distributions .mask_len = 0xf0, .mask_fam = AF_INET, .mask_str = "255.255.0.0",
742*27b03b36SApple OSS Distributions .error = 0
743*27b03b36SApple OSS Distributions },
744*27b03b36SApple OSS Distributions {
745*27b03b36SApple OSS Distributions .description = "mask_family AF_INET6",
746*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.7.7.7",
747*27b03b36SApple OSS Distributions .broad_len = sizeof(struct sockaddr_in), .broad_fam = AF_INET, .broad_str = "10.7.7.255",
748*27b03b36SApple OSS Distributions .mask_len = sizeof(struct sockaddr_in), .mask_fam = AF_INET6, .mask_str = "255.255.0.0",
749*27b03b36SApple OSS Distributions .error = 0
750*27b03b36SApple OSS Distributions },
751*27b03b36SApple OSS Distributions {
752*27b03b36SApple OSS Distributions .description = "ifra address only",
753*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.8.8.8",
754*27b03b36SApple OSS Distributions .broad_len = 0, .broad_str = NULL,
755*27b03b36SApple OSS Distributions .mask_len = 0, .mask_str = NULL,
756*27b03b36SApple OSS Distributions .error = 0
757*27b03b36SApple OSS Distributions },
758*27b03b36SApple OSS Distributions {
759*27b03b36SApple OSS Distributions .description = "ifra mask len 1",
760*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.9.9.1",
761*27b03b36SApple OSS Distributions .broad_len = 0, .broad_str = NULL,
762*27b03b36SApple OSS Distributions .mask_len = 1, .mask_fam = AF_INET, .mask_str = "255.255.255.0",
763*27b03b36SApple OSS Distributions .error = 0
764*27b03b36SApple OSS Distributions },
765*27b03b36SApple OSS Distributions {
766*27b03b36SApple OSS Distributions .description = "ifra mask len 3",
767*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.9.9.3",
768*27b03b36SApple OSS Distributions .broad_len = 0, .broad_str = NULL,
769*27b03b36SApple OSS Distributions .mask_len = 1, .mask_fam = AF_INET, .mask_str = "255.255.255.0",
770*27b03b36SApple OSS Distributions .error = 0
771*27b03b36SApple OSS Distributions },
772*27b03b36SApple OSS Distributions {
773*27b03b36SApple OSS Distributions .description = "ifra mask len 5",
774*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.9.9.5",
775*27b03b36SApple OSS Distributions .broad_len = 0, .broad_str = NULL,
776*27b03b36SApple OSS Distributions .mask_len = 1, .mask_fam = AF_INET, .mask_str = "255.255.255.0",
777*27b03b36SApple OSS Distributions .error = 0
778*27b03b36SApple OSS Distributions },
779*27b03b36SApple OSS Distributions {
780*27b03b36SApple OSS Distributions .description = "ifra mask len 7",
781*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.9.9.7",
782*27b03b36SApple OSS Distributions .broad_len = 0, .broad_str = NULL,
783*27b03b36SApple OSS Distributions .mask_len = 1, .mask_fam = AF_INET, .mask_str = "255.255.255.0",
784*27b03b36SApple OSS Distributions .error = 0
785*27b03b36SApple OSS Distributions },
786*27b03b36SApple OSS Distributions {
787*27b03b36SApple OSS Distributions .description = "ifra mask len 9",
788*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.9.9.9",
789*27b03b36SApple OSS Distributions .broad_len = 0, .broad_str = NULL,
790*27b03b36SApple OSS Distributions .mask_len = 1, .mask_fam = AF_INET, .mask_str = "255.255.255.0",
791*27b03b36SApple OSS Distributions .error = 0
792*27b03b36SApple OSS Distributions },
793*27b03b36SApple OSS Distributions {
794*27b03b36SApple OSS Distributions .description = "ifra mask len 11",
795*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.9.9.11",
796*27b03b36SApple OSS Distributions .broad_len = 0, .broad_str = NULL,
797*27b03b36SApple OSS Distributions .mask_len = 1, .mask_fam = AF_INET, .mask_str = "255.255.255.0",
798*27b03b36SApple OSS Distributions .error = 0
799*27b03b36SApple OSS Distributions },
800*27b03b36SApple OSS Distributions {
801*27b03b36SApple OSS Distributions .description = "ifra mask len 13",
802*27b03b36SApple OSS Distributions .addr_len = sizeof(struct sockaddr_in), .addr_fam = AF_INET, .addr_str = "10.9.9.13",
803*27b03b36SApple OSS Distributions .broad_len = 0, .broad_str = NULL,
804*27b03b36SApple OSS Distributions .mask_len = 1, .mask_fam = AF_INET, .mask_str = "255.255.255.0",
805*27b03b36SApple OSS Distributions .error = 0
806*27b03b36SApple OSS Distributions },
807*27b03b36SApple OSS Distributions {
808*27b03b36SApple OSS Distributions .description = NULL,
809*27b03b36SApple OSS Distributions .error = -1
810*27b03b36SApple OSS Distributions }
811*27b03b36SApple OSS Distributions };
812*27b03b36SApple OSS Distributions
813*27b03b36SApple OSS Distributions T_DECL(sioc_ifra_addr_bounds, "test bound checks on socket address passed to interface ioctls",
814*27b03b36SApple OSS Distributions T_META_ASROOT(true))
815*27b03b36SApple OSS Distributions {
816*27b03b36SApple OSS Distributions int s = -1;
817*27b03b36SApple OSS Distributions
818*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(s = socket(AF_INET, SOCK_DGRAM, 0), "socket");
819*27b03b36SApple OSS Distributions
820*27b03b36SApple OSS Distributions char ifname[IFNAMSIZ];
821*27b03b36SApple OSS Distributions strlcpy(ifname, IF_NAME, sizeof(ifname));
822*27b03b36SApple OSS Distributions int error = 0;
823*27b03b36SApple OSS Distributions if ((error = ifnet_create(s, ifname, sizeof(ifname))) != 0) {
824*27b03b36SApple OSS Distributions if (error == EINVAL) {
825*27b03b36SApple OSS Distributions T_SKIP("The system does not support the %s cloning interface", IF_NAME);
826*27b03b36SApple OSS Distributions }
827*27b03b36SApple OSS Distributions T_SKIP("This test failed creating a %s cloning interface", IF_NAME);
828*27b03b36SApple OSS Distributions }
829*27b03b36SApple OSS Distributions T_LOG("created clone interface '%s'", ifname);
830*27b03b36SApple OSS Distributions
831*27b03b36SApple OSS Distributions struct ioc_ifra *ioc_ifra;
832*27b03b36SApple OSS Distributions
833*27b03b36SApple OSS Distributions for (ioc_ifra = ioc_ifra_list; ioc_ifra->error != -1; ioc_ifra++) {
834*27b03b36SApple OSS Distributions struct in_aliasreq ifra = {};
835*27b03b36SApple OSS Distributions unsigned char pattern;
836*27b03b36SApple OSS Distributions int retval;
837*27b03b36SApple OSS Distributions
838*27b03b36SApple OSS Distributions T_LOG("");
839*27b03b36SApple OSS Distributions T_LOG("TEST CASE: %s, ioctl(SIOCAIFADDR, %s)", ioc_ifra->description, ioc_ifra->addr_str != NULL ? ioc_ifra->addr_str : "");
840*27b03b36SApple OSS Distributions
841*27b03b36SApple OSS Distributions if (find_unused_pattern_in_rt_if_list(&pattern, PATTERN_SIZE) == false) {
842*27b03b36SApple OSS Distributions T_SKIP("Could not find unused pattern in rt_if_list");
843*27b03b36SApple OSS Distributions return;
844*27b03b36SApple OSS Distributions }
845*27b03b36SApple OSS Distributions
846*27b03b36SApple OSS Distributions memset(&ifra, pattern, sizeof(ifra));
847*27b03b36SApple OSS Distributions
848*27b03b36SApple OSS Distributions strlcpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
849*27b03b36SApple OSS Distributions
850*27b03b36SApple OSS Distributions ifra.ifra_addr.sin_len = ioc_ifra->addr_len;
851*27b03b36SApple OSS Distributions ifra.ifra_addr.sin_family = ioc_ifra->addr_fam;
852*27b03b36SApple OSS Distributions if (ioc_ifra->addr_str != NULL) {
853*27b03b36SApple OSS Distributions ifra.ifra_addr.sin_addr.s_addr = inet_addr(ioc_ifra->addr_str);
854*27b03b36SApple OSS Distributions }
855*27b03b36SApple OSS Distributions
856*27b03b36SApple OSS Distributions ifra.ifra_broadaddr.sin_len = ioc_ifra->broad_len;
857*27b03b36SApple OSS Distributions ifra.ifra_broadaddr.sin_family = ioc_ifra->broad_fam;
858*27b03b36SApple OSS Distributions if (ioc_ifra->broad_str != NULL) {
859*27b03b36SApple OSS Distributions ifra.ifra_broadaddr.sin_addr.s_addr = inet_addr(ioc_ifra->broad_str);
860*27b03b36SApple OSS Distributions }
861*27b03b36SApple OSS Distributions
862*27b03b36SApple OSS Distributions ifra.ifra_mask.sin_len = ioc_ifra->mask_len;
863*27b03b36SApple OSS Distributions ifra.ifra_mask.sin_family = ioc_ifra->mask_fam;
864*27b03b36SApple OSS Distributions if (ioc_ifra->mask_str != NULL) {
865*27b03b36SApple OSS Distributions ifra.ifra_mask.sin_addr.s_addr = inet_addr(ioc_ifra->mask_str);
866*27b03b36SApple OSS Distributions }
867*27b03b36SApple OSS Distributions
868*27b03b36SApple OSS Distributions if (ioc_ifra->error == 0) {
869*27b03b36SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(retval = ioctl(s, SIOCAIFADDR, &ifra), "SIOCAIFADDR retval %d", retval);
870*27b03b36SApple OSS Distributions } else {
871*27b03b36SApple OSS Distributions T_EXPECT_POSIX_FAILURE(retval = ioctl(s, SIOCAIFADDR, &ifra), EINVAL, "SIOCAIFADDR retval %d, %s", retval, strerror(errno));
872*27b03b36SApple OSS Distributions }
873*27b03b36SApple OSS Distributions
874*27b03b36SApple OSS Distributions T_EXPECT_EQ(check_rt_if_list_for_pattern("after ioctl SIOCAIFADDR", pattern, PATTERN_SIZE), 0, "pattern should not be found");
875*27b03b36SApple OSS Distributions }
876*27b03b36SApple OSS Distributions
877*27b03b36SApple OSS Distributions print_rt_iflist2(__func__);
878*27b03b36SApple OSS Distributions
879*27b03b36SApple OSS Distributions (void)ifnet_destroy(s, ifname, true);
880*27b03b36SApple OSS Distributions
881*27b03b36SApple OSS Distributions close(s);
882*27b03b36SApple OSS Distributions }
883*27b03b36SApple OSS Distributions
884*27b03b36SApple OSS Distributions T_DECL(sioc_ifr_dstaddr_leak, "test bound checks on socket address passed to interface ioctls",
885*27b03b36SApple OSS Distributions T_META_ASROOT(true))
886*27b03b36SApple OSS Distributions {
887*27b03b36SApple OSS Distributions int s = -1;
888*27b03b36SApple OSS Distributions struct ifreq ifr = {};
889*27b03b36SApple OSS Distributions unsigned char pattern;
890*27b03b36SApple OSS Distributions struct ifaddrs *ifap = NULL, *ifa;
891*27b03b36SApple OSS Distributions bool found_gif0 = false;
892*27b03b36SApple OSS Distributions
893*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(getifaddrs(&ifap), "getifaddrs");
894*27b03b36SApple OSS Distributions for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
895*27b03b36SApple OSS Distributions if (strcmp(ifa->ifa_name, "gif0") == 0) {
896*27b03b36SApple OSS Distributions found_gif0 = true;
897*27b03b36SApple OSS Distributions break;
898*27b03b36SApple OSS Distributions }
899*27b03b36SApple OSS Distributions }
900*27b03b36SApple OSS Distributions freeifaddrs(ifap);
901*27b03b36SApple OSS Distributions ifap = NULL;
902*27b03b36SApple OSS Distributions if (found_gif0 == false) {
903*27b03b36SApple OSS Distributions T_SKIP("gif0 does not exists");
904*27b03b36SApple OSS Distributions }
905*27b03b36SApple OSS Distributions
906*27b03b36SApple OSS Distributions if (find_unused_pattern_in_rt_if_list(&pattern, PATTERN_SIZE) == false) {
907*27b03b36SApple OSS Distributions T_SKIP("Could not find unused pattern");
908*27b03b36SApple OSS Distributions return;
909*27b03b36SApple OSS Distributions }
910*27b03b36SApple OSS Distributions
911*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(s = socket(AF_INET, SOCK_DGRAM, 0), "socket");
912*27b03b36SApple OSS Distributions
913*27b03b36SApple OSS Distributions strlcpy(ifr.ifr_name, "gif0", sizeof(ifr.ifr_name));
914*27b03b36SApple OSS Distributions ifr.ifr_dstaddr.sa_family = AF_INET6;
915*27b03b36SApple OSS Distributions ifr.ifr_dstaddr.sa_len = 0xf0;
916*27b03b36SApple OSS Distributions memset(&ifr.ifr_dstaddr.sa_data, pattern, PATTERN_SIZE);
917*27b03b36SApple OSS Distributions
918*27b03b36SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(ioctl(s, SIOCSIFDSTADDR, &ifr), "ioctl(SIOCSIFDSTADDR)");
919*27b03b36SApple OSS Distributions
920*27b03b36SApple OSS Distributions print_rt_iflist2(__func__);
921*27b03b36SApple OSS Distributions
922*27b03b36SApple OSS Distributions close(s);
923*27b03b36SApple OSS Distributions
924*27b03b36SApple OSS Distributions T_EXPECT_EQ(check_rt_if_list_for_pattern("AFTER", pattern, PATTERN_SIZE), 0, "pattern should not be found");
925*27b03b36SApple OSS Distributions }
926*27b03b36SApple OSS Distributions
927*27b03b36SApple OSS Distributions static struct ioc_ifreq ioc_list_config[] = {
928*27b03b36SApple OSS Distributions { SIOCSIFADDR, sizeof(struct sockaddr_in), AF_INET, "0.0.0.0", EINVAL },
929*27b03b36SApple OSS Distributions { SIOCSIFADDR, sizeof(struct sockaddr_in), AF_INET, "255.255.255.255", EINVAL },
930*27b03b36SApple OSS Distributions { SIOCSIFADDR, sizeof(struct sockaddr_in), AF_INET, "238.207.49.91", EINVAL },
931*27b03b36SApple OSS Distributions
932*27b03b36SApple OSS Distributions { SIOCAIFADDR_IN6, sizeof(struct sockaddr_in6), AF_INET6, "::", EINVAL },
933*27b03b36SApple OSS Distributions { SIOCAIFADDR_IN6, sizeof(struct sockaddr_in6), AF_INET6, "ff33:40:fd25:549c:675b::1", EINVAL },
934*27b03b36SApple OSS Distributions { SIOCAIFADDR_IN6, sizeof(struct sockaddr_in6), AF_INET6, "::ffff:0a0a:0a0a", EINVAL },
935*27b03b36SApple OSS Distributions { SIOCAIFADDR_IN6, sizeof(struct sockaddr_in6), AF_INET6, "::0a0a:0a0a", EINVAL },
936*27b03b36SApple OSS Distributions
937*27b03b36SApple OSS Distributions { 0, 0, 0, "", -1 },
938*27b03b36SApple OSS Distributions };
939*27b03b36SApple OSS Distributions
940*27b03b36SApple OSS Distributions static void
test_sioc_ifr_addr_config_v4(struct ioc_ifreq * ioc_ifreq,int s,const char * ifname)941*27b03b36SApple OSS Distributions test_sioc_ifr_addr_config_v4(struct ioc_ifreq *ioc_ifreq, int s, const char *ifname)
942*27b03b36SApple OSS Distributions {
943*27b03b36SApple OSS Distributions struct ifreq ifr = {};
944*27b03b36SApple OSS Distributions struct sockaddr_in *sin;
945*27b03b36SApple OSS Distributions
946*27b03b36SApple OSS Distributions T_LOG("");
947*27b03b36SApple OSS Distributions T_LOG("TEST CASE: %s ioctl(%s, sa_len %u, sa_family %u, %s) -> %d", __func__,
948*27b03b36SApple OSS Distributions ioc_str(ioc_ifreq->ioc_cmd), ioc_ifreq->salen, ioc_ifreq->safamily, ioc_ifreq->sastr, ioc_ifreq->error);
949*27b03b36SApple OSS Distributions
950*27b03b36SApple OSS Distributions
951*27b03b36SApple OSS Distributions strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
952*27b03b36SApple OSS Distributions
953*27b03b36SApple OSS Distributions sin = (struct sockaddr_in *)(void *)&ifr.ifr_addr;
954*27b03b36SApple OSS Distributions sin->sin_len = ioc_ifreq->salen;
955*27b03b36SApple OSS Distributions sin->sin_family = ioc_ifreq->safamily;
956*27b03b36SApple OSS Distributions sin->sin_addr.s_addr = inet_addr(ioc_ifreq->sastr);
957*27b03b36SApple OSS Distributions
958*27b03b36SApple OSS Distributions int retval;
959*27b03b36SApple OSS Distributions if (ioc_ifreq->error == 0) {
960*27b03b36SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(retval = ioctl(s, ioc_ifreq->ioc_cmd, &ifr),
961*27b03b36SApple OSS Distributions "%s, %s: retval %d", ioc_str(ioc_ifreq->ioc_cmd), ioc_ifreq->sastr, retval);
962*27b03b36SApple OSS Distributions } else {
963*27b03b36SApple OSS Distributions T_EXPECT_POSIX_FAILURE(retval = ioctl(s, ioc_ifreq->ioc_cmd, &ifr), ioc_ifreq->error,
964*27b03b36SApple OSS Distributions "%s, %s: retval %d errno %s", ioc_str(ioc_ifreq->ioc_cmd), ioc_ifreq->sastr, retval, strerror(errno));
965*27b03b36SApple OSS Distributions }
966*27b03b36SApple OSS Distributions
967*27b03b36SApple OSS Distributions fflush(stdout);
968*27b03b36SApple OSS Distributions fflush(stderr);
969*27b03b36SApple OSS Distributions }
970*27b03b36SApple OSS Distributions
971*27b03b36SApple OSS Distributions static void
test_sioc_ifr_addr_config_v6(struct ioc_ifreq * ioc_ifreq,int s,const char * ifname)972*27b03b36SApple OSS Distributions test_sioc_ifr_addr_config_v6(struct ioc_ifreq *ioc_ifreq, int s, const char *ifname)
973*27b03b36SApple OSS Distributions {
974*27b03b36SApple OSS Distributions struct in6_aliasreq ifra = {{ 0 }, { 0 }, { 0 }, { 0 }, 0, { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME }};
975*27b03b36SApple OSS Distributions struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(void *)&ifra.ifra_addr;
976*27b03b36SApple OSS Distributions struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)(void *)&ifra.ifra_prefixmask;
977*27b03b36SApple OSS Distributions T_LOG("");
978*27b03b36SApple OSS Distributions T_LOG("TEST CASE: %s ioctl(%s, sa_len %u, sa_family %u, %s) -> %d", __func__,
979*27b03b36SApple OSS Distributions ioc_str(ioc_ifreq->ioc_cmd), ioc_ifreq->salen, ioc_ifreq->safamily, ioc_ifreq->sastr, ioc_ifreq->error);
980*27b03b36SApple OSS Distributions
981*27b03b36SApple OSS Distributions strlcpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
982*27b03b36SApple OSS Distributions
983*27b03b36SApple OSS Distributions sin6->sin6_len = ioc_ifreq->salen;
984*27b03b36SApple OSS Distributions sin6->sin6_family = ioc_ifreq->safamily;
985*27b03b36SApple OSS Distributions T_ASSERT_EQ(inet_pton(AF_INET6, ioc_ifreq->sastr, &sin6->sin6_addr), 1, NULL);
986*27b03b36SApple OSS Distributions sin6->sin6_scope_id = if_nametoindex(ifname);
987*27b03b36SApple OSS Distributions
988*27b03b36SApple OSS Distributions mask6->sin6_len = ioc_ifreq->salen;
989*27b03b36SApple OSS Distributions mask6->sin6_family = ioc_ifreq->safamily;
990*27b03b36SApple OSS Distributions T_ASSERT_EQ(inet_pton(AF_INET6, "ffff:ffff:ffff:ffff::", &mask6->sin6_addr), 1, NULL);
991*27b03b36SApple OSS Distributions
992*27b03b36SApple OSS Distributions ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
993*27b03b36SApple OSS Distributions ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
994*27b03b36SApple OSS Distributions
995*27b03b36SApple OSS Distributions int retval;
996*27b03b36SApple OSS Distributions if (ioc_ifreq->error == 0) {
997*27b03b36SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(retval = ioctl(s, ioc_ifreq->ioc_cmd, &ifra),
998*27b03b36SApple OSS Distributions "%s, %s: retval %d", ioc_str(ioc_ifreq->ioc_cmd), ioc_ifreq->sastr, retval);
999*27b03b36SApple OSS Distributions } else {
1000*27b03b36SApple OSS Distributions T_EXPECT_POSIX_FAILURE(retval = ioctl(s, ioc_ifreq->ioc_cmd, &ifra), ioc_ifreq->error,
1001*27b03b36SApple OSS Distributions "%s, %s: retval %d errno %s", ioc_str(ioc_ifreq->ioc_cmd), ioc_ifreq->sastr, retval, strerror(errno));
1002*27b03b36SApple OSS Distributions }
1003*27b03b36SApple OSS Distributions
1004*27b03b36SApple OSS Distributions fflush(stdout);
1005*27b03b36SApple OSS Distributions fflush(stderr);
1006*27b03b36SApple OSS Distributions }
1007*27b03b36SApple OSS Distributions
1008*27b03b36SApple OSS Distributions
1009*27b03b36SApple OSS Distributions T_DECL(sioc_ifr_addr_config, "test failure cases for interface address configuration ioctls",
1010*27b03b36SApple OSS Distributions T_META_ASROOT(true))
1011*27b03b36SApple OSS Distributions {
1012*27b03b36SApple OSS Distributions int s = -1;
1013*27b03b36SApple OSS Distributions int s6 = -1;
1014*27b03b36SApple OSS Distributions char ifname[IFNAMSIZ];
1015*27b03b36SApple OSS Distributions
1016*27b03b36SApple OSS Distributions T_LOG("%s", __func__);
1017*27b03b36SApple OSS Distributions
1018*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(s = socket(AF_INET, SOCK_DGRAM, 0), "socket");
1019*27b03b36SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(s6 = socket(AF_INET6, SOCK_DGRAM, 0), "socket");
1020*27b03b36SApple OSS Distributions
1021*27b03b36SApple OSS Distributions strlcpy(ifname, IF_NAME, sizeof(ifname));
1022*27b03b36SApple OSS Distributions
1023*27b03b36SApple OSS Distributions int error = 0;
1024*27b03b36SApple OSS Distributions if ((error = ifnet_create(s, ifname, sizeof(ifname))) != 0) {
1025*27b03b36SApple OSS Distributions if (error == EINVAL) {
1026*27b03b36SApple OSS Distributions T_SKIP("The system does not support the %s cloning interface", IF_NAME);
1027*27b03b36SApple OSS Distributions }
1028*27b03b36SApple OSS Distributions T_SKIP("This test failed creating a %s cloning interface", IF_NAME);
1029*27b03b36SApple OSS Distributions }
1030*27b03b36SApple OSS Distributions T_LOG("created clone interface '%s'", ifname);
1031*27b03b36SApple OSS Distributions
1032*27b03b36SApple OSS Distributions struct ioc_ifreq *ioc_ifreq;
1033*27b03b36SApple OSS Distributions for (ioc_ifreq = ioc_list_config; ioc_ifreq->error != -1; ioc_ifreq++) {
1034*27b03b36SApple OSS Distributions if (ioc_ifreq->safamily == AF_INET) {
1035*27b03b36SApple OSS Distributions test_sioc_ifr_addr_config_v4(ioc_ifreq, s, ifname);
1036*27b03b36SApple OSS Distributions } else if (ioc_ifreq->safamily == AF_INET6) {
1037*27b03b36SApple OSS Distributions test_sioc_ifr_addr_config_v6(ioc_ifreq, s6, ifname);
1038*27b03b36SApple OSS Distributions }
1039*27b03b36SApple OSS Distributions }
1040*27b03b36SApple OSS Distributions print_rt_iflist2(__func__);
1041*27b03b36SApple OSS Distributions (void)ifnet_destroy(s, ifname, true);
1042*27b03b36SApple OSS Distributions
1043*27b03b36SApple OSS Distributions close(s);
1044*27b03b36SApple OSS Distributions close(s6);
1045*27b03b36SApple OSS Distributions }
1046*27b03b36SApple OSS Distributions
1047*27b03b36SApple OSS Distributions /**
1048*27b03b36SApple OSS Distributions ** Test SIOCPROTOATTACH, SIOCPROTOATTACH_IN6
1049*27b03b36SApple OSS Distributions **/
1050*27b03b36SApple OSS Distributions static int S_socket = -1;
1051*27b03b36SApple OSS Distributions
1052*27b03b36SApple OSS Distributions static int
get_inet_dgram_socket(void)1053*27b03b36SApple OSS Distributions get_inet_dgram_socket(void)
1054*27b03b36SApple OSS Distributions {
1055*27b03b36SApple OSS Distributions if (S_socket >= 0) {
1056*27b03b36SApple OSS Distributions return S_socket;
1057*27b03b36SApple OSS Distributions }
1058*27b03b36SApple OSS Distributions S_socket = socket(AF_INET, SOCK_DGRAM, 0);
1059*27b03b36SApple OSS Distributions T_QUIET;
1060*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(S_socket, "socket(AF_INET, SOCK_DGRAM, 0)");
1061*27b03b36SApple OSS Distributions return S_socket;
1062*27b03b36SApple OSS Distributions }
1063*27b03b36SApple OSS Distributions
1064*27b03b36SApple OSS Distributions static void
close_inet_dgram_socket(void)1065*27b03b36SApple OSS Distributions close_inet_dgram_socket(void)
1066*27b03b36SApple OSS Distributions {
1067*27b03b36SApple OSS Distributions if (S_socket >= 0) {
1068*27b03b36SApple OSS Distributions close(S_socket);
1069*27b03b36SApple OSS Distributions S_socket = -1;
1070*27b03b36SApple OSS Distributions }
1071*27b03b36SApple OSS Distributions }
1072*27b03b36SApple OSS Distributions
1073*27b03b36SApple OSS Distributions static int S_socket_6 = -1;
1074*27b03b36SApple OSS Distributions
1075*27b03b36SApple OSS Distributions static int
get_inet6_dgram_socket(void)1076*27b03b36SApple OSS Distributions get_inet6_dgram_socket(void)
1077*27b03b36SApple OSS Distributions {
1078*27b03b36SApple OSS Distributions if (S_socket_6 >= 0) {
1079*27b03b36SApple OSS Distributions return S_socket_6;
1080*27b03b36SApple OSS Distributions }
1081*27b03b36SApple OSS Distributions S_socket_6 = socket(AF_INET6, SOCK_DGRAM, 0);
1082*27b03b36SApple OSS Distributions T_QUIET;
1083*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(S_socket_6, "socket(AF_INET6, SOCK_DGRAM, 0)");
1084*27b03b36SApple OSS Distributions return S_socket_6;
1085*27b03b36SApple OSS Distributions }
1086*27b03b36SApple OSS Distributions
1087*27b03b36SApple OSS Distributions static void
close_inet6_dgram_socket(void)1088*27b03b36SApple OSS Distributions close_inet6_dgram_socket(void)
1089*27b03b36SApple OSS Distributions {
1090*27b03b36SApple OSS Distributions if (S_socket_6 >= 0) {
1091*27b03b36SApple OSS Distributions close(S_socket_6);
1092*27b03b36SApple OSS Distributions S_socket_6 = -1;
1093*27b03b36SApple OSS Distributions }
1094*27b03b36SApple OSS Distributions }
1095*27b03b36SApple OSS Distributions
1096*27b03b36SApple OSS Distributions static char ifname[IFNAMSIZ];
1097*27b03b36SApple OSS Distributions
1098*27b03b36SApple OSS Distributions static void
test_proto_attach_cleanup(void)1099*27b03b36SApple OSS Distributions test_proto_attach_cleanup(void)
1100*27b03b36SApple OSS Distributions {
1101*27b03b36SApple OSS Distributions if (ifname[0] != '\0') {
1102*27b03b36SApple OSS Distributions int s;
1103*27b03b36SApple OSS Distributions
1104*27b03b36SApple OSS Distributions s = get_inet_dgram_socket();
1105*27b03b36SApple OSS Distributions (void)ifnet_destroy(s, ifname, false);
1106*27b03b36SApple OSS Distributions T_LOG("Destroyed '%s'", ifname);
1107*27b03b36SApple OSS Distributions ifname[0] = '\0';
1108*27b03b36SApple OSS Distributions }
1109*27b03b36SApple OSS Distributions }
1110*27b03b36SApple OSS Distributions
1111*27b03b36SApple OSS Distributions static void
test_proto_attach_atend(void)1112*27b03b36SApple OSS Distributions test_proto_attach_atend(void)
1113*27b03b36SApple OSS Distributions {
1114*27b03b36SApple OSS Distributions test_proto_attach_cleanup();
1115*27b03b36SApple OSS Distributions close_inet_dgram_socket();
1116*27b03b36SApple OSS Distributions close_inet6_dgram_socket();
1117*27b03b36SApple OSS Distributions }
1118*27b03b36SApple OSS Distributions
1119*27b03b36SApple OSS Distributions static void
test_proto_attach(void)1120*27b03b36SApple OSS Distributions test_proto_attach(void)
1121*27b03b36SApple OSS Distributions {
1122*27b03b36SApple OSS Distributions int error;
1123*27b03b36SApple OSS Distributions struct ifreq ifr;
1124*27b03b36SApple OSS Distributions int s;
1125*27b03b36SApple OSS Distributions int s6;
1126*27b03b36SApple OSS Distributions
1127*27b03b36SApple OSS Distributions T_ATEND(test_proto_attach_atend);
1128*27b03b36SApple OSS Distributions s = get_inet_dgram_socket();
1129*27b03b36SApple OSS Distributions strlcpy(ifname, "feth", sizeof(ifname));
1130*27b03b36SApple OSS Distributions error = ifnet_create(s, ifname, sizeof(ifname));
1131*27b03b36SApple OSS Distributions if (error != 0) {
1132*27b03b36SApple OSS Distributions if (error == EINVAL) {
1133*27b03b36SApple OSS Distributions T_SKIP("%s cloning interface not supported", ifname);
1134*27b03b36SApple OSS Distributions }
1135*27b03b36SApple OSS Distributions T_SKIP("%s cloning interface failed", ifname);
1136*27b03b36SApple OSS Distributions }
1137*27b03b36SApple OSS Distributions T_LOG("Created '%s'", ifname);
1138*27b03b36SApple OSS Distributions
1139*27b03b36SApple OSS Distributions /* first proto attach should succeed */
1140*27b03b36SApple OSS Distributions bzero(&ifr, sizeof(ifr));
1141*27b03b36SApple OSS Distributions strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1142*27b03b36SApple OSS Distributions error = ioctl(s, SIOCPROTOATTACH, &ifr);
1143*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(error, "SIOCPROTOATTACH %s succeeded",
1144*27b03b36SApple OSS Distributions ifr.ifr_name);
1145*27b03b36SApple OSS Distributions
1146*27b03b36SApple OSS Distributions /* second proto attach should fail with EEXIST */
1147*27b03b36SApple OSS Distributions error = ioctl(s, SIOCPROTOATTACH, &ifr);
1148*27b03b36SApple OSS Distributions T_ASSERT_POSIX_FAILURE(error, EEXIST,
1149*27b03b36SApple OSS Distributions "SIOCPROTOATTACH %s failed as expected",
1150*27b03b36SApple OSS Distributions ifr.ifr_name);
1151*27b03b36SApple OSS Distributions
1152*27b03b36SApple OSS Distributions /* first proto attach should succeed */
1153*27b03b36SApple OSS Distributions s6 = get_inet6_dgram_socket();
1154*27b03b36SApple OSS Distributions bzero(&ifr, sizeof(ifr));
1155*27b03b36SApple OSS Distributions strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1156*27b03b36SApple OSS Distributions error = ioctl(s6, SIOCPROTOATTACH_IN6, &ifr);
1157*27b03b36SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(error,
1158*27b03b36SApple OSS Distributions "SIOCPROTOATTACH_IN6 %s succeeded",
1159*27b03b36SApple OSS Distributions ifr.ifr_name);
1160*27b03b36SApple OSS Distributions
1161*27b03b36SApple OSS Distributions /* second proto attach should fail with EEXIST */
1162*27b03b36SApple OSS Distributions error = ioctl(s6, SIOCPROTOATTACH_IN6, &ifr);
1163*27b03b36SApple OSS Distributions T_ASSERT_POSIX_FAILURE(error, EEXIST,
1164*27b03b36SApple OSS Distributions "SIOCPROTOATTACH_IN6 %s failed as expected",
1165*27b03b36SApple OSS Distributions ifr.ifr_name);
1166*27b03b36SApple OSS Distributions test_proto_attach_cleanup();
1167*27b03b36SApple OSS Distributions }
1168*27b03b36SApple OSS Distributions
1169*27b03b36SApple OSS Distributions T_DECL(sioc_proto_attach, "test protocol attachment",
1170*27b03b36SApple OSS Distributions T_META_ASROOT(true))
1171*27b03b36SApple OSS Distributions {
1172*27b03b36SApple OSS Distributions test_proto_attach();
1173*27b03b36SApple OSS Distributions }
1174