1*f6217f89SApple OSS Distributions /*
2*f6217f89SApple OSS Distributions * Copyright (c) 2021 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/fcntl.h>
30*f6217f89SApple OSS Distributions #include <sys/socket.h>
31*f6217f89SApple OSS Distributions #include <netinet/in.h>
32*f6217f89SApple OSS Distributions #include <netinet/udp.h>
33*f6217f89SApple OSS Distributions #include <arpa/inet.h>
34*f6217f89SApple OSS Distributions
35*f6217f89SApple OSS Distributions #include <stdbool.h>
36*f6217f89SApple OSS Distributions #include <string.h>
37*f6217f89SApple OSS Distributions #include <unistd.h>
38*f6217f89SApple OSS Distributions
39*f6217f89SApple OSS Distributions #include <darwintest.h>
40*f6217f89SApple OSS Distributions
41*f6217f89SApple OSS Distributions static in_port_t listener_port;
42*f6217f89SApple OSS Distributions
43*f6217f89SApple OSS Distributions static void
tcp_listen(void)44*f6217f89SApple OSS Distributions tcp_listen(void)
45*f6217f89SApple OSS Distributions {
46*f6217f89SApple OSS Distributions int s = -1;
47*f6217f89SApple OSS Distributions
48*f6217f89SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(AF_INET6, SOCK_STREAM, 0), NULL);
49*f6217f89SApple OSS Distributions
50*f6217f89SApple OSS Distributions struct sockaddr_in6 sin6 = {};
51*f6217f89SApple OSS Distributions sin6.sin6_len = sizeof(struct sockaddr_in6);
52*f6217f89SApple OSS Distributions sin6.sin6_family = AF_INET6;
53*f6217f89SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bind(s, (struct sockaddr *)&sin6, sizeof(sin6)), NULL);
54*f6217f89SApple OSS Distributions
55*f6217f89SApple OSS Distributions socklen_t solen = sizeof(sin6);
56*f6217f89SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockname(s, (struct sockaddr *)&sin6, &solen), NULL);
57*f6217f89SApple OSS Distributions
58*f6217f89SApple OSS Distributions listener_port = sin6.sin6_port;
59*f6217f89SApple OSS Distributions
60*f6217f89SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(listen(s, 128), NULL);
61*f6217f89SApple OSS Distributions }
62*f6217f89SApple OSS Distributions
63*f6217f89SApple OSS Distributions static void
set_udp_kao_opt(int expected_errno,int domain,const char * domain_str,int type,const char * type_str,int proto,const char * proto_str)64*f6217f89SApple OSS Distributions set_udp_kao_opt(int expected_errno, int domain, const char *domain_str,
65*f6217f89SApple OSS Distributions int type, const char *type_str,
66*f6217f89SApple OSS Distributions int proto, const char *proto_str)
67*f6217f89SApple OSS Distributions {
68*f6217f89SApple OSS Distributions T_LOG("expect error %d for socket domain: %s type: %s protocol: %s",
69*f6217f89SApple OSS Distributions expected_errno, domain_str, type_str, proto_str);
70*f6217f89SApple OSS Distributions
71*f6217f89SApple OSS Distributions int s = -1;
72*f6217f89SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(domain, type, proto), NULL);
73*f6217f89SApple OSS Distributions
74*f6217f89SApple OSS Distributions union sockaddr_in_4_6 sa = {};
75*f6217f89SApple OSS Distributions
76*f6217f89SApple OSS Distributions if (domain == PF_INET) {
77*f6217f89SApple OSS Distributions sa.sin.sin_len = sizeof(struct sockaddr_in);
78*f6217f89SApple OSS Distributions sa.sin.sin_family = AF_INET;
79*f6217f89SApple OSS Distributions sa.sin.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);
80*f6217f89SApple OSS Distributions sa.sin.sin_port = listener_port;
81*f6217f89SApple OSS Distributions } else {
82*f6217f89SApple OSS Distributions sa.sin6.sin6_len = sizeof(struct sockaddr_in6);
83*f6217f89SApple OSS Distributions sa.sin6.sin6_family = AF_INET6;
84*f6217f89SApple OSS Distributions sa.sin6.sin6_addr = in6addr_loopback;
85*f6217f89SApple OSS Distributions sa.sin6.sin6_port = listener_port;
86*f6217f89SApple OSS Distributions }
87*f6217f89SApple OSS Distributions
88*f6217f89SApple OSS Distributions /*
89*f6217f89SApple OSS Distributions * Keep alive option needs a connected flow
90*f6217f89SApple OSS Distributions */
91*f6217f89SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(connect(s, &sa.sa, sa.sa.sa_len), NULL);
92*f6217f89SApple OSS Distributions
93*f6217f89SApple OSS Distributions /*
94*f6217f89SApple OSS Distributions * UDP_KEEPALIVE_OFFLOAD should only succeed for UDP sockets
95*f6217f89SApple OSS Distributions */
96*f6217f89SApple OSS Distributions struct udp_keepalive_offload keepAliveInfo = {};
97*f6217f89SApple OSS Distributions keepAliveInfo.ka_interval = 1;
98*f6217f89SApple OSS Distributions keepAliveInfo.ka_data_len = 1;
99*f6217f89SApple OSS Distributions keepAliveInfo.ka_type = UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY;
100*f6217f89SApple OSS Distributions
101*f6217f89SApple OSS Distributions if (expected_errno == 0) {
102*f6217f89SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(s, IPPROTO_UDP, UDP_KEEPALIVE_OFFLOAD,
103*f6217f89SApple OSS Distributions &keepAliveInfo, sizeof(keepAliveInfo)),
104*f6217f89SApple OSS Distributions "setsockopt IPPROTO_UDP, UDP_KEEPALIVE_OFFLOAD");
105*f6217f89SApple OSS Distributions } else {
106*f6217f89SApple OSS Distributions T_ASSERT_POSIX_FAILURE(setsockopt(s, IPPROTO_UDP, UDP_KEEPALIVE_OFFLOAD,
107*f6217f89SApple OSS Distributions &keepAliveInfo, sizeof(keepAliveInfo)), expected_errno,
108*f6217f89SApple OSS Distributions "setsockopt IPPROTO_UDP, UDP_KEEPALIVE_OFFLOAD");
109*f6217f89SApple OSS Distributions }
110*f6217f89SApple OSS Distributions
111*f6217f89SApple OSS Distributions /*
112*f6217f89SApple OSS Distributions * Verify that network layer options can be set
113*f6217f89SApple OSS Distributions */
114*f6217f89SApple OSS Distributions int optval = 10;
115*f6217f89SApple OSS Distributions if (domain == PF_INET) {
116*f6217f89SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(s, IPPROTO_IP, IP_TTL,
117*f6217f89SApple OSS Distributions &optval, sizeof(optval)), "setsockopt IPPROTO_IP, IP_TTL");
118*f6217f89SApple OSS Distributions } else {
119*f6217f89SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
120*f6217f89SApple OSS Distributions &optval, sizeof(optval)), "setsockopt IPPROTO_IPV6, IPV6_UNICAST_HOPS");
121*f6217f89SApple OSS Distributions }
122*f6217f89SApple OSS Distributions
123*f6217f89SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(s), NULL);
124*f6217f89SApple OSS Distributions }
125*f6217f89SApple OSS Distributions
126*f6217f89SApple OSS Distributions #define SET_UDP_KAO_OPT(e, d, t, p) set_udp_kao_opt(e, d, #d, t, #t, p, #p)
127*f6217f89SApple OSS Distributions
128*f6217f89SApple OSS Distributions T_DECL(test_udp_keep_alive_option, "TCP bind with a IPv6 multicast address",
129*f6217f89SApple OSS Distributions T_META_ENABLED(false) /* rdar://134506000 */)
130*f6217f89SApple OSS Distributions {
131*f6217f89SApple OSS Distributions tcp_listen();
132*f6217f89SApple OSS Distributions
133*f6217f89SApple OSS Distributions SET_UDP_KAO_OPT(0, PF_INET6, SOCK_DGRAM, 0);
134*f6217f89SApple OSS Distributions SET_UDP_KAO_OPT(0, PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
135*f6217f89SApple OSS Distributions SET_UDP_KAO_OPT(EINVAL, PF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6);
136*f6217f89SApple OSS Distributions SET_UDP_KAO_OPT(EINVAL, PF_INET6, SOCK_STREAM, 0);
137*f6217f89SApple OSS Distributions
138*f6217f89SApple OSS Distributions SET_UDP_KAO_OPT(0, PF_INET, SOCK_DGRAM, 0);
139*f6217f89SApple OSS Distributions SET_UDP_KAO_OPT(0, PF_INET, SOCK_DGRAM, IPPROTO_UDP);
140*f6217f89SApple OSS Distributions SET_UDP_KAO_OPT(EINVAL, PF_INET, SOCK_DGRAM, IPPROTO_ICMP);
141*f6217f89SApple OSS Distributions SET_UDP_KAO_OPT(EINVAL, PF_INET, SOCK_STREAM, 0);
142*f6217f89SApple OSS Distributions }
143