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