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