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