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