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