1 /*
2 * Copyright (c) 2024 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 #define __APPLE_USE_RFC_3542 1
30
31 #include <darwintest.h>
32
33 #include <sys/ioctl.h>
34 #include <sys/sysctl.h>
35 #include <sys/uio.h>
36
37 #include <arpa/inet.h>
38
39 #include <netinet/ip.h>
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <strings.h>
44
45 #include "net_test_lib.h"
46
47 T_GLOBAL_META(
48 T_META_NAMESPACE("xnu.net"),
49 T_META_ASROOT(true),
50 T_META_RADAR_COMPONENT_NAME("xnu"),
51 T_META_RADAR_COMPONENT_VERSION("networking"),
52 T_META_CHECK_LEAKS(false));
53
54 /* need something greater than default MTU */
55 #define MAX_BUFFER_SIZE 2048
56
57 // 169.254
58 #define LL_NET 0xa9fe0000 //0x0a000000
59 // 169.254.1
60 #define LL_1_NET (LL_NET | 0x000100)
61
62 static void
get_ipv4_address(u_int unit,u_int addr_index,struct in_addr * ip)63 get_ipv4_address(u_int unit, u_int addr_index, struct in_addr *ip)
64 {
65 /* up to 255 units, 255 addresses */
66 ip->s_addr = htonl(LL_1_NET | (unit << 8) | addr_index);
67 return;
68 }
69
70 static void
network_interface_init(network_interface_t netif,const char * name,unsigned int unit,unsigned int address_index)71 network_interface_init(network_interface_t netif,
72 const char * name, unsigned int unit,
73 unsigned int address_index)
74 {
75 network_interface_create(netif, name);
76 get_ipv4_address(unit, address_index, &netif->ip);
77 ifnet_add_ip_address(netif->if_name, netif->ip,
78 inet_class_c_subnet_mask);
79 route_add_inet_scoped_subnet(netif->if_name, netif->if_index,
80 netif->ip, inet_class_c_subnet_mask);
81 }
82
83 static network_interface if_one = {0};
84 static network_interface if_two = {0};
85
86 static void
cleanup(void)87 cleanup(void)
88 {
89 network_interface_destroy(&if_one);
90 network_interface_destroy(&if_two);
91 }
92
93 T_DECL(v4mappedv6_dontfrag_sockopt, "Tests setting IPV6_DONTFRAG on an IPv4-mapped IPv6 address")
94 {
95 int sockfd = 0;
96 ssize_t n;
97 char buf[MAX_BUFFER_SIZE] = {0};
98 struct sockaddr_in6 sin6 = {0};
99
100 sin6.sin6_len = sizeof(struct sockaddr_in6);
101 sin6.sin6_family = AF_INET6;
102 sin6.sin6_port = htons(12345);
103
104 T_ATEND(cleanup);
105
106 network_interface_init(&if_one, FETH_NAME, 0, 1);
107 network_interface_init(&if_two, FETH_NAME, 0, 2);
108 fake_set_peer(if_one.if_name, if_two.if_name);
109 ifnet_set_mtu(if_one.if_name, 1500);
110
111 T_ASSERT_EQ(inet_pton(AF_INET6, "::ffff:169.254.1.2", &sin6.sin6_addr), 1, "inet_pton");
112
113 T_ASSERT_POSIX_SUCCESS(sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP), "create socket");
114
115 // This send should succeed (and fragment)
116 n = sendto(sockfd, buf, MAX_BUFFER_SIZE, 0,
117 (struct sockaddr *)&sin6, sizeof(sin6));
118 T_EXPECT_EQ(n, (ssize_t)MAX_BUFFER_SIZE, "Ensure we wrote MAX_BUFFER_SIZE");
119
120 int Option = 1;
121 T_ASSERT_POSIX_SUCCESS(setsockopt(sockfd, IPPROTO_IPV6, IPV6_DONTFRAG, &Option, sizeof(Option)), "setsockopt IPV6_DONTFRAG to 1");
122
123 // This send should fail because MAX_BUFFER_SIZE > MTU and we enabled DONTFRAG
124 n = sendto(sockfd, buf, MAX_BUFFER_SIZE, 0,
125 (struct sockaddr *)&sin6, sizeof(sin6));
126 T_EXPECT_EQ(errno, EMSGSIZE, "errno should be EMSGSIZE");
127 T_EXPECT_EQ(n, (ssize_t)-1, "Expect n of a certain size");
128
129 Option = 0;
130 T_ASSERT_POSIX_SUCCESS(setsockopt(sockfd, IPPROTO_IPV6, IPV6_DONTFRAG, &Option, sizeof(Option)), "setsockopt IPV6_DONTFRAG back to 0");
131
132 // This send should succeeed (and fragment) because we turned the option back off
133 n = sendto(sockfd, buf, MAX_BUFFER_SIZE, 0,
134 (struct sockaddr *)&sin6, sizeof(sin6));
135 T_EXPECT_EQ(n, (ssize_t)MAX_BUFFER_SIZE, "Ensure we wrote MAX_BUFFER_SIZE");
136 }
137