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