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