xref: /xnu-11215.61.5/tests/so_bindtodevice.c (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1*4f1223e8SApple OSS Distributions /*
2*4f1223e8SApple OSS Distributions  * Copyright (c) 2023 Apple Inc. All rights reserved.
3*4f1223e8SApple OSS Distributions  *
4*4f1223e8SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*4f1223e8SApple OSS Distributions  *
6*4f1223e8SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*4f1223e8SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*4f1223e8SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*4f1223e8SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*4f1223e8SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*4f1223e8SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*4f1223e8SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*4f1223e8SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*4f1223e8SApple OSS Distributions  *
15*4f1223e8SApple OSS Distributions  * Please obtain a copy of the License at
16*4f1223e8SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*4f1223e8SApple OSS Distributions  *
18*4f1223e8SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*4f1223e8SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*4f1223e8SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*4f1223e8SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*4f1223e8SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*4f1223e8SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*4f1223e8SApple OSS Distributions  * limitations under the License.
25*4f1223e8SApple OSS Distributions  *
26*4f1223e8SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*4f1223e8SApple OSS Distributions  */
28*4f1223e8SApple OSS Distributions 
29*4f1223e8SApple OSS Distributions #include <sys/socket.h>
30*4f1223e8SApple OSS Distributions 
31*4f1223e8SApple OSS Distributions #include <net/if.h>
32*4f1223e8SApple OSS Distributions 
33*4f1223e8SApple OSS Distributions #include <netinet/in.h>
34*4f1223e8SApple OSS Distributions 
35*4f1223e8SApple OSS Distributions #include <stdbool.h>
36*4f1223e8SApple OSS Distributions #include <stdlib.h>
37*4f1223e8SApple OSS Distributions #include <string.h>
38*4f1223e8SApple OSS Distributions #include <unistd.h>
39*4f1223e8SApple OSS Distributions 
40*4f1223e8SApple OSS Distributions #include <arpa/inet.h>
41*4f1223e8SApple OSS Distributions 
42*4f1223e8SApple OSS Distributions #include <darwintest.h>
43*4f1223e8SApple OSS Distributions 
44*4f1223e8SApple OSS Distributions T_GLOBAL_META(
45*4f1223e8SApple OSS Distributions 	T_META_NAMESPACE("xnu.net"),
46*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
47*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("networking"),
48*4f1223e8SApple OSS Distributions 	T_META_ASROOT(true)
49*4f1223e8SApple OSS Distributions 	);
50*4f1223e8SApple OSS Distributions 
51*4f1223e8SApple OSS Distributions static void
test_so_bindtodevice(int domain,int type,int proto)52*4f1223e8SApple OSS Distributions test_so_bindtodevice(int domain, int type, int proto)
53*4f1223e8SApple OSS Distributions {
54*4f1223e8SApple OSS Distributions #ifndef SO_BINDTODEVICE
55*4f1223e8SApple OSS Distributions 	T_SKIP("SO_BINDTODEVICE not defined")
56*4f1223e8SApple OSS Distributions #else
57*4f1223e8SApple OSS Distributions 	int fd;
58*4f1223e8SApple OSS Distributions 	int boundif_level = domain == PF_INET ? IPPROTO_IP : IPPROTO_IPV6;
59*4f1223e8SApple OSS Distributions 	int boundif_name = domain == PF_INET ? IP_BOUND_IF : IPV6_BOUND_IF;
60*4f1223e8SApple OSS Distributions 	const char *boundif_str = domain == PF_INET ? "IP_BOUND_IF" : "IPV6_BOUND_IF";
61*4f1223e8SApple OSS Distributions 	char ifname[IFNAMSIZ + 1] = { 0 };
62*4f1223e8SApple OSS Distributions 	int intval;
63*4f1223e8SApple OSS Distributions 	socklen_t len;
64*4f1223e8SApple OSS Distributions 	unsigned int ifindex = if_nametoindex("lo0");
65*4f1223e8SApple OSS Distributions 
66*4f1223e8SApple OSS Distributions 	T_LOG("test_so_bindtodevice(%d, %d, %d)", domain, type, proto);
67*4f1223e8SApple OSS Distributions 
68*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd = socket(domain, type, proto), NULL);
69*4f1223e8SApple OSS Distributions 
70*4f1223e8SApple OSS Distributions 	/*
71*4f1223e8SApple OSS Distributions 	 * First test the default values
72*4f1223e8SApple OSS Distributions 	 */
73*4f1223e8SApple OSS Distributions 	intval = -1;
74*4f1223e8SApple OSS Distributions 	len = sizeof(int);
75*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(fd, boundif_level, boundif_name, &intval, &len),
76*4f1223e8SApple OSS Distributions 	    "get default  %s, intval %d", boundif_str, intval);
77*4f1223e8SApple OSS Distributions 
78*4f1223e8SApple OSS Distributions 	T_ASSERT_EQ_INT(intval, 0, "default interface index 0");
79*4f1223e8SApple OSS Distributions 
80*4f1223e8SApple OSS Distributions 	len = sizeof(ifname);
81*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, &len),
82*4f1223e8SApple OSS Distributions 	    "get default SO_BINDTODEVICE: \"%s\"", ifname);
83*4f1223e8SApple OSS Distributions 
84*4f1223e8SApple OSS Distributions 	T_ASSERT_EQ_STR(ifname, "", "default interface name empty");
85*4f1223e8SApple OSS Distributions 
86*4f1223e8SApple OSS Distributions 	/*
87*4f1223e8SApple OSS Distributions 	 * Verify that SO_BINDTODEVICE can get the interface set by xxx_BOUND_IF
88*4f1223e8SApple OSS Distributions 	 */
89*4f1223e8SApple OSS Distributions 	intval = (int)ifindex;
90*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(fd, boundif_level, boundif_name, &intval, len),
91*4f1223e8SApple OSS Distributions 	    "set lo0 %s, intval %d", boundif_str, intval);
92*4f1223e8SApple OSS Distributions 
93*4f1223e8SApple OSS Distributions 	intval = -1;
94*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(fd, boundif_level, boundif_name, &intval, &len),
95*4f1223e8SApple OSS Distributions 	    "get %s, intval %d", boundif_str, intval);
96*4f1223e8SApple OSS Distributions 
97*4f1223e8SApple OSS Distributions 	T_ASSERT_EQ_INT(intval, (int)ifindex, "loopback interface index");
98*4f1223e8SApple OSS Distributions 
99*4f1223e8SApple OSS Distributions 	len = sizeof(ifname);
100*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, &len),
101*4f1223e8SApple OSS Distributions 	    "get SO_BINDTODEVICE: \"%s\", len %u", ifname, len);
102*4f1223e8SApple OSS Distributions 
103*4f1223e8SApple OSS Distributions 	T_ASSERT_EQ_STR(ifname, "lo0", "loopback interface name");
104*4f1223e8SApple OSS Distributions 
105*4f1223e8SApple OSS Distributions 	/*
106*4f1223e8SApple OSS Distributions 	 * Verify that an empty string clears the bound interface
107*4f1223e8SApple OSS Distributions 	 */
108*4f1223e8SApple OSS Distributions 	strlcpy(ifname, "", sizeof(ifname));
109*4f1223e8SApple OSS Distributions 	len = IFNAMSIZ;
110*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, len), NULL);
111*4f1223e8SApple OSS Distributions 	T_LOG("set SO_BINDTODEVICE `\"\"` %s: len %d", boundif_str, intval);
112*4f1223e8SApple OSS Distributions 
113*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(fd, boundif_level, boundif_name, &intval, &len), NULL);
114*4f1223e8SApple OSS Distributions 	T_LOG("cleared %s, intval %d", boundif_str, intval);
115*4f1223e8SApple OSS Distributions 
116*4f1223e8SApple OSS Distributions 	T_ASSERT_EQ_INT(intval, 0, "default interface index 0");
117*4f1223e8SApple OSS Distributions 
118*4f1223e8SApple OSS Distributions 	/*
119*4f1223e8SApple OSS Distributions 	 * Verify interface set by SO_BINDTODEVICE is gotten by xxx_BOUND_IF
120*4f1223e8SApple OSS Distributions 	 */
121*4f1223e8SApple OSS Distributions 	strlcpy(ifname, "lo0", sizeof(ifname));
122*4f1223e8SApple OSS Distributions 	len = (socklen_t)strlen(ifname);
123*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, len),
124*4f1223e8SApple OSS Distributions 	    "set SO_BINDTODEVICE: \"%s\", len %u", ifname, len);
125*4f1223e8SApple OSS Distributions 
126*4f1223e8SApple OSS Distributions 	len = sizeof(ifname);
127*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, &len),
128*4f1223e8SApple OSS Distributions 	    "lo0 SO_BINDTODEVICE: \"%s\"", ifname);
129*4f1223e8SApple OSS Distributions 
130*4f1223e8SApple OSS Distributions 	T_ASSERT_EQ_STR(ifname, "lo0", "loopback interface name");
131*4f1223e8SApple OSS Distributions 
132*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(fd, boundif_level, boundif_name, &intval, &len),
133*4f1223e8SApple OSS Distributions 	    "lo0 %s, intval %d", boundif_str, intval);
134*4f1223e8SApple OSS Distributions 
135*4f1223e8SApple OSS Distributions 	T_ASSERT_EQ_INT(intval, (int)ifindex, "loopback interface index");
136*4f1223e8SApple OSS Distributions 
137*4f1223e8SApple OSS Distributions 	/*
138*4f1223e8SApple OSS Distributions 	 * Verify that a NULL string clears the bound interface
139*4f1223e8SApple OSS Distributions 	 */
140*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, NULL, 0),
141*4f1223e8SApple OSS Distributions 	    "set SO_BINDTODEVICE: NULL, len 0");
142*4f1223e8SApple OSS Distributions 
143*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(fd, boundif_level, boundif_name, &intval, &len), NULL);
144*4f1223e8SApple OSS Distributions 	T_LOG("cleared \"%s\", intval %d", boundif_str, intval);
145*4f1223e8SApple OSS Distributions 
146*4f1223e8SApple OSS Distributions 	T_ASSERT_EQ_INT(intval, 0, "default interface index 0");
147*4f1223e8SApple OSS Distributions 
148*4f1223e8SApple OSS Distributions 	/*
149*4f1223e8SApple OSS Distributions 	 * Verify bounds
150*4f1223e8SApple OSS Distributions 	 */
151*4f1223e8SApple OSS Distributions 	strlcpy(ifname, "lo0", sizeof(ifname));
152*4f1223e8SApple OSS Distributions 
153*4f1223e8SApple OSS Distributions 	len = IFNAMSIZ;
154*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, len),
155*4f1223e8SApple OSS Distributions 	    "set SO_BINDTODEVICE: \"%s\", len %u (IFNAMSIZ) OK", ifname, len);
156*4f1223e8SApple OSS Distributions 
157*4f1223e8SApple OSS Distributions 	len = IFNAMSIZ + 1;
158*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_FAILURE(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, len), EINVAL,
159*4f1223e8SApple OSS Distributions 	    "set SO_BINDTODEVICE: \"%s\", len %u (IFNAMSIZ + 1) expected EINVAL", ifname, len);
160*4f1223e8SApple OSS Distributions #endif
161*4f1223e8SApple OSS Distributions }
162*4f1223e8SApple OSS Distributions 
163*4f1223e8SApple OSS Distributions T_DECL(so_bindtodevice_tcp_ipv4, "SO_BINDTODEVICE TCP IPv4")
164*4f1223e8SApple OSS Distributions {
165*4f1223e8SApple OSS Distributions 	test_so_bindtodevice(PF_INET, SOCK_STREAM, 0);
166*4f1223e8SApple OSS Distributions }
167*4f1223e8SApple OSS Distributions 
168*4f1223e8SApple OSS Distributions T_DECL(so_bindtodevice_tcp_ipv6, "SO_BINDTODEVICE TCP IPv6")
169*4f1223e8SApple OSS Distributions {
170*4f1223e8SApple OSS Distributions 	test_so_bindtodevice(PF_INET6, SOCK_STREAM, 0);
171*4f1223e8SApple OSS Distributions }
172*4f1223e8SApple OSS Distributions 
173*4f1223e8SApple OSS Distributions T_DECL(so_bindtodevice_udp_ipv4, "SO_BINDTODEVICE UDP IPv4")
174*4f1223e8SApple OSS Distributions {
175*4f1223e8SApple OSS Distributions 	test_so_bindtodevice(PF_INET, SOCK_DGRAM, 0);
176*4f1223e8SApple OSS Distributions }
177*4f1223e8SApple OSS Distributions 
178*4f1223e8SApple OSS Distributions T_DECL(so_bindtodevice_udp_ipv6, "SO_BINDTODEVICE UDP IPv6")
179*4f1223e8SApple OSS Distributions {
180*4f1223e8SApple OSS Distributions 	test_so_bindtodevice(PF_INET6, SOCK_DGRAM, 0);
181*4f1223e8SApple OSS Distributions }
182*4f1223e8SApple OSS Distributions 
183*4f1223e8SApple OSS Distributions T_DECL(so_bindtodevice_icmp_ipv4, "SO_BINDTODEVICE ICMP IPv4")
184*4f1223e8SApple OSS Distributions {
185*4f1223e8SApple OSS Distributions 	test_so_bindtodevice(PF_INET, SOCK_DGRAM, IPPROTO_ICMP);
186*4f1223e8SApple OSS Distributions }
187*4f1223e8SApple OSS Distributions 
188*4f1223e8SApple OSS Distributions T_DECL(so_bindtodevice_icmp_ipv6, "SO_BINDTODEVICE ICMP IPv6")
189*4f1223e8SApple OSS Distributions {
190*4f1223e8SApple OSS Distributions 	test_so_bindtodevice(PF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6);
191*4f1223e8SApple OSS Distributions }
192*4f1223e8SApple OSS Distributions 
193*4f1223e8SApple OSS Distributions T_DECL(so_bindtodevice_raw_ipv4, "SO_BINDTODEVICE RAW IPv4")
194*4f1223e8SApple OSS Distributions {
195*4f1223e8SApple OSS Distributions 	test_so_bindtodevice(PF_INET, SOCK_RAW, 0);
196*4f1223e8SApple OSS Distributions }
197*4f1223e8SApple OSS Distributions 
198*4f1223e8SApple OSS Distributions T_DECL(so_bindtodevice_raw_ipv6, "SO_BINDTODEVICE RAW IPv6")
199*4f1223e8SApple OSS Distributions {
200*4f1223e8SApple OSS Distributions 	test_so_bindtodevice(PF_INET6, SOCK_RAW, 0);
201*4f1223e8SApple OSS Distributions }
202