1*0f4c859eSApple OSS Distributions /*
2*0f4c859eSApple OSS Distributions * Copyright (c) 2021 Apple Inc. All rights reserved.
3*0f4c859eSApple OSS Distributions *
4*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*0f4c859eSApple OSS Distributions *
6*0f4c859eSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*0f4c859eSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*0f4c859eSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*0f4c859eSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*0f4c859eSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*0f4c859eSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*0f4c859eSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*0f4c859eSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*0f4c859eSApple OSS Distributions *
15*0f4c859eSApple OSS Distributions * Please obtain a copy of the License at
16*0f4c859eSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*0f4c859eSApple OSS Distributions *
18*0f4c859eSApple OSS Distributions * The Original Code and all software distributed under the License are
19*0f4c859eSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*0f4c859eSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*0f4c859eSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*0f4c859eSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*0f4c859eSApple OSS Distributions * Please see the License for the specific language governing rights and
24*0f4c859eSApple OSS Distributions * limitations under the License.
25*0f4c859eSApple OSS Distributions *
26*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*0f4c859eSApple OSS Distributions */
28*0f4c859eSApple OSS Distributions
29*0f4c859eSApple OSS Distributions #include <sys/fcntl.h>
30*0f4c859eSApple OSS Distributions #include <sys/socket.h>
31*0f4c859eSApple OSS Distributions #include <netinet/in.h>
32*0f4c859eSApple OSS Distributions #include <netinet/udp.h>
33*0f4c859eSApple OSS Distributions #include <arpa/inet.h>
34*0f4c859eSApple OSS Distributions
35*0f4c859eSApple OSS Distributions #include <stdbool.h>
36*0f4c859eSApple OSS Distributions #include <string.h>
37*0f4c859eSApple OSS Distributions #include <unistd.h>
38*0f4c859eSApple OSS Distributions
39*0f4c859eSApple OSS Distributions #include <darwintest.h>
40*0f4c859eSApple OSS Distributions
41*0f4c859eSApple OSS Distributions static in_port_t listener_port;
42*0f4c859eSApple OSS Distributions
43*0f4c859eSApple OSS Distributions static void
tcp_listen(void)44*0f4c859eSApple OSS Distributions tcp_listen(void)
45*0f4c859eSApple OSS Distributions {
46*0f4c859eSApple OSS Distributions int s = -1;
47*0f4c859eSApple OSS Distributions
48*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(AF_INET6, SOCK_STREAM, 0), NULL);
49*0f4c859eSApple OSS Distributions
50*0f4c859eSApple OSS Distributions struct sockaddr_in6 sin6 = {};
51*0f4c859eSApple OSS Distributions sin6.sin6_len = sizeof(struct sockaddr_in6);
52*0f4c859eSApple OSS Distributions sin6.sin6_family = AF_INET6;
53*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bind(s, (struct sockaddr *)&sin6, sizeof(sin6)), NULL);
54*0f4c859eSApple OSS Distributions
55*0f4c859eSApple OSS Distributions socklen_t solen = sizeof(sin6);
56*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockname(s, (struct sockaddr *)&sin6, &solen), NULL);
57*0f4c859eSApple OSS Distributions
58*0f4c859eSApple OSS Distributions listener_port = sin6.sin6_port;
59*0f4c859eSApple OSS Distributions
60*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(listen(s, 128), NULL);
61*0f4c859eSApple OSS Distributions }
62*0f4c859eSApple OSS Distributions
63*0f4c859eSApple 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*0f4c859eSApple OSS Distributions set_udp_kao_opt(int expected_errno, int domain, const char *domain_str,
65*0f4c859eSApple OSS Distributions int type, const char *type_str,
66*0f4c859eSApple OSS Distributions int proto, const char *proto_str)
67*0f4c859eSApple OSS Distributions {
68*0f4c859eSApple OSS Distributions T_LOG("expect error %d for socket domain: %s type: %s protocol: %s",
69*0f4c859eSApple OSS Distributions expected_errno, domain_str, type_str, proto_str);
70*0f4c859eSApple OSS Distributions
71*0f4c859eSApple OSS Distributions int s = -1;
72*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(domain, type, proto), NULL);
73*0f4c859eSApple OSS Distributions
74*0f4c859eSApple OSS Distributions union sockaddr_in_4_6 sa = {};
75*0f4c859eSApple OSS Distributions
76*0f4c859eSApple OSS Distributions if (domain == PF_INET) {
77*0f4c859eSApple OSS Distributions sa.sin.sin_len = sizeof(struct sockaddr_in);
78*0f4c859eSApple OSS Distributions sa.sin.sin_family = AF_INET;
79*0f4c859eSApple OSS Distributions sa.sin.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);
80*0f4c859eSApple OSS Distributions sa.sin.sin_port = listener_port;
81*0f4c859eSApple OSS Distributions } else {
82*0f4c859eSApple OSS Distributions sa.sin6.sin6_len = sizeof(struct sockaddr_in6);
83*0f4c859eSApple OSS Distributions sa.sin6.sin6_family = AF_INET6;
84*0f4c859eSApple OSS Distributions sa.sin6.sin6_addr = in6addr_loopback;
85*0f4c859eSApple OSS Distributions sa.sin6.sin6_port = listener_port;
86*0f4c859eSApple OSS Distributions }
87*0f4c859eSApple OSS Distributions
88*0f4c859eSApple OSS Distributions /*
89*0f4c859eSApple OSS Distributions * Keep alive option needs a connected flow
90*0f4c859eSApple OSS Distributions */
91*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(connect(s, &sa.sa, sa.sa.sa_len), NULL);
92*0f4c859eSApple OSS Distributions
93*0f4c859eSApple OSS Distributions /*
94*0f4c859eSApple OSS Distributions * UDP_KEEPALIVE_OFFLOAD should only succeed for UDP sockets
95*0f4c859eSApple OSS Distributions */
96*0f4c859eSApple OSS Distributions struct udp_keepalive_offload keepAliveInfo = {};
97*0f4c859eSApple OSS Distributions keepAliveInfo.ka_interval = 1;
98*0f4c859eSApple OSS Distributions keepAliveInfo.ka_data_len = 1;
99*0f4c859eSApple OSS Distributions keepAliveInfo.ka_type = UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY;
100*0f4c859eSApple OSS Distributions
101*0f4c859eSApple OSS Distributions if (expected_errno == 0) {
102*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(s, IPPROTO_UDP, UDP_KEEPALIVE_OFFLOAD,
103*0f4c859eSApple OSS Distributions &keepAliveInfo, sizeof(keepAliveInfo)),
104*0f4c859eSApple OSS Distributions "setsockopt IPPROTO_UDP, UDP_KEEPALIVE_OFFLOAD");
105*0f4c859eSApple OSS Distributions } else {
106*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_FAILURE(setsockopt(s, IPPROTO_UDP, UDP_KEEPALIVE_OFFLOAD,
107*0f4c859eSApple OSS Distributions &keepAliveInfo, sizeof(keepAliveInfo)), expected_errno,
108*0f4c859eSApple OSS Distributions "setsockopt IPPROTO_UDP, UDP_KEEPALIVE_OFFLOAD");
109*0f4c859eSApple OSS Distributions }
110*0f4c859eSApple OSS Distributions
111*0f4c859eSApple OSS Distributions /*
112*0f4c859eSApple OSS Distributions * Verify that network layer options can be set
113*0f4c859eSApple OSS Distributions */
114*0f4c859eSApple OSS Distributions int optval = 10;
115*0f4c859eSApple OSS Distributions if (domain == PF_INET) {
116*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(s, IPPROTO_IP, IP_TTL,
117*0f4c859eSApple OSS Distributions &optval, sizeof(optval)), "setsockopt IPPROTO_IP, IP_TTL");
118*0f4c859eSApple OSS Distributions } else {
119*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
120*0f4c859eSApple OSS Distributions &optval, sizeof(optval)), "setsockopt IPPROTO_IPV6, IPV6_UNICAST_HOPS");
121*0f4c859eSApple OSS Distributions }
122*0f4c859eSApple OSS Distributions
123*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(s), NULL);
124*0f4c859eSApple OSS Distributions }
125*0f4c859eSApple OSS Distributions
126*0f4c859eSApple OSS Distributions #define SET_UDP_KAO_OPT(e, d, t, p) set_udp_kao_opt(e, d, #d, t, #t, p, #p)
127*0f4c859eSApple OSS Distributions
128*0f4c859eSApple OSS Distributions T_DECL(test_udp_keep_alive_option, "TCP bind with a IPv6 multicast address")
129*0f4c859eSApple OSS Distributions {
130*0f4c859eSApple OSS Distributions tcp_listen();
131*0f4c859eSApple OSS Distributions
132*0f4c859eSApple OSS Distributions SET_UDP_KAO_OPT(0, PF_INET6, SOCK_DGRAM, 0);
133*0f4c859eSApple OSS Distributions SET_UDP_KAO_OPT(0, PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
134*0f4c859eSApple OSS Distributions SET_UDP_KAO_OPT(EINVAL, PF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6);
135*0f4c859eSApple OSS Distributions SET_UDP_KAO_OPT(EINVAL, PF_INET6, SOCK_STREAM, 0);
136*0f4c859eSApple OSS Distributions
137*0f4c859eSApple OSS Distributions SET_UDP_KAO_OPT(0, PF_INET, SOCK_DGRAM, 0);
138*0f4c859eSApple OSS Distributions SET_UDP_KAO_OPT(0, PF_INET, SOCK_DGRAM, IPPROTO_UDP);
139*0f4c859eSApple OSS Distributions SET_UDP_KAO_OPT(EINVAL, PF_INET, SOCK_DGRAM, IPPROTO_ICMP);
140*0f4c859eSApple OSS Distributions SET_UDP_KAO_OPT(EINVAL, PF_INET, SOCK_STREAM, 0);
141*0f4c859eSApple OSS Distributions }
142