xref: /xnu-12377.81.4/tests/inet6_addr_mode.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2023-2024 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions  *
15*043036a2SApple OSS Distributions  * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions  *
18*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions  * limitations under the License.
25*043036a2SApple OSS Distributions  *
26*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions  */
28*043036a2SApple OSS Distributions 
29*043036a2SApple OSS Distributions #include <darwintest.h>
30*043036a2SApple OSS Distributions 
31*043036a2SApple OSS Distributions #include <sys/ioctl.h>
32*043036a2SApple OSS Distributions 
33*043036a2SApple OSS Distributions #include <stdlib.h>
34*043036a2SApple OSS Distributions #include <string.h>
35*043036a2SApple OSS Distributions #include <strings.h>
36*043036a2SApple OSS Distributions 
37*043036a2SApple OSS Distributions #include "net_test_lib.h"
38*043036a2SApple OSS Distributions 
39*043036a2SApple OSS Distributions T_GLOBAL_META(
40*043036a2SApple OSS Distributions 	T_META_NAMESPACE("xnu.net"),
41*043036a2SApple OSS Distributions 	T_META_ASROOT(true),
42*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
43*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("networking"),
44*043036a2SApple OSS Distributions 	T_META_CHECK_LEAKS(false));
45*043036a2SApple OSS Distributions 
46*043036a2SApple OSS Distributions static char ifname1[IF_NAMESIZE];
47*043036a2SApple OSS Distributions 
48*043036a2SApple OSS Distributions 
49*043036a2SApple OSS Distributions static void
cleanup(void)50*043036a2SApple OSS Distributions cleanup(void)
51*043036a2SApple OSS Distributions {
52*043036a2SApple OSS Distributions 	if (ifname1[0] != '\0') {
53*043036a2SApple OSS Distributions 		T_LOG("ifnet_destroy %s", ifname1);
54*043036a2SApple OSS Distributions 		(void)ifnet_destroy(ifname1, false);
55*043036a2SApple OSS Distributions 	}
56*043036a2SApple OSS Distributions }
57*043036a2SApple OSS Distributions 
58*043036a2SApple OSS Distributions static void
set_sockaddr_in6(struct sockaddr_in6 * sin6_p,const struct in6_addr * addr)59*043036a2SApple OSS Distributions set_sockaddr_in6(struct sockaddr_in6 *sin6_p, const struct in6_addr *addr)
60*043036a2SApple OSS Distributions {
61*043036a2SApple OSS Distributions 	sin6_p->sin6_family = AF_INET6;
62*043036a2SApple OSS Distributions 	sin6_p->sin6_len = sizeof(struct sockaddr_in6);
63*043036a2SApple OSS Distributions 	sin6_p->sin6_addr = *addr;
64*043036a2SApple OSS Distributions 	return;
65*043036a2SApple OSS Distributions }
66*043036a2SApple OSS Distributions 
67*043036a2SApple OSS Distributions static int
inet6_difaddr(const char * name,const struct in6_addr * addr)68*043036a2SApple OSS Distributions inet6_difaddr(const char *name, const struct in6_addr *addr)
69*043036a2SApple OSS Distributions {
70*043036a2SApple OSS Distributions 	struct in6_ifreq    ifr;
71*043036a2SApple OSS Distributions 	int             s6 = inet6_dgram_socket_get();
72*043036a2SApple OSS Distributions 
73*043036a2SApple OSS Distributions 	bzero(&ifr, sizeof(ifr));
74*043036a2SApple OSS Distributions 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
75*043036a2SApple OSS Distributions 	if (addr != NULL) {
76*043036a2SApple OSS Distributions 		set_sockaddr_in6(&ifr.ifr_ifru.ifru_addr, addr);
77*043036a2SApple OSS Distributions 	}
78*043036a2SApple OSS Distributions 	return ioctl(s6, SIOCDIFADDR_IN6, &ifr);
79*043036a2SApple OSS Distributions }
80*043036a2SApple OSS Distributions 
81*043036a2SApple OSS Distributions static void
in6_len2mask(struct in6_addr * mask,int len)82*043036a2SApple OSS Distributions in6_len2mask(struct in6_addr *mask, int len)
83*043036a2SApple OSS Distributions {
84*043036a2SApple OSS Distributions 	int i;
85*043036a2SApple OSS Distributions 
86*043036a2SApple OSS Distributions 	bzero(mask, sizeof(*mask));
87*043036a2SApple OSS Distributions 	for (i = 0; i < len / 8; i++) {
88*043036a2SApple OSS Distributions 		mask->s6_addr[i] = 0xff;
89*043036a2SApple OSS Distributions 	}
90*043036a2SApple OSS Distributions 	if (len % 8) {
91*043036a2SApple OSS Distributions 		mask->s6_addr[i] = (0xff00 >> (len % 8)) & 0xff;
92*043036a2SApple OSS Distributions 	}
93*043036a2SApple OSS Distributions }
94*043036a2SApple OSS Distributions 
95*043036a2SApple OSS Distributions static int
inet6_aifaddr(const char * name,const struct in6_addr * addr,const struct in6_addr * dstaddr,int prefix_length,int flags,u_int32_t valid_lifetime,u_int32_t preferred_lifetime)96*043036a2SApple OSS Distributions inet6_aifaddr(const char *name, const struct in6_addr *addr,
97*043036a2SApple OSS Distributions     const struct in6_addr *dstaddr, int prefix_length,
98*043036a2SApple OSS Distributions     int flags,
99*043036a2SApple OSS Distributions     u_int32_t valid_lifetime,
100*043036a2SApple OSS Distributions     u_int32_t preferred_lifetime)
101*043036a2SApple OSS Distributions {
102*043036a2SApple OSS Distributions 	struct in6_aliasreq ifra_in6;
103*043036a2SApple OSS Distributions 	int             s6 = inet6_dgram_socket_get();
104*043036a2SApple OSS Distributions 
105*043036a2SApple OSS Distributions 	bzero(&ifra_in6, sizeof(ifra_in6));
106*043036a2SApple OSS Distributions 	strncpy(ifra_in6.ifra_name, name, sizeof(ifra_in6.ifra_name));
107*043036a2SApple OSS Distributions 	ifra_in6.ifra_lifetime.ia6t_vltime = valid_lifetime;
108*043036a2SApple OSS Distributions 	ifra_in6.ifra_lifetime.ia6t_pltime = preferred_lifetime;
109*043036a2SApple OSS Distributions 	ifra_in6.ifra_flags = flags;
110*043036a2SApple OSS Distributions 	if (addr != NULL) {
111*043036a2SApple OSS Distributions 		set_sockaddr_in6(&ifra_in6.ifra_addr, addr);
112*043036a2SApple OSS Distributions 	}
113*043036a2SApple OSS Distributions 
114*043036a2SApple OSS Distributions 	if (dstaddr != NULL) {
115*043036a2SApple OSS Distributions 		set_sockaddr_in6(&ifra_in6.ifra_dstaddr, dstaddr);
116*043036a2SApple OSS Distributions 	}
117*043036a2SApple OSS Distributions 
118*043036a2SApple OSS Distributions 	if (prefix_length != 0) {
119*043036a2SApple OSS Distributions 		struct in6_addr     prefixmask;
120*043036a2SApple OSS Distributions 
121*043036a2SApple OSS Distributions 		in6_len2mask(&prefixmask, prefix_length);
122*043036a2SApple OSS Distributions 		set_sockaddr_in6(&ifra_in6.ifra_prefixmask, &prefixmask);
123*043036a2SApple OSS Distributions 	}
124*043036a2SApple OSS Distributions 
125*043036a2SApple OSS Distributions 	return ioctl(s6, SIOCAIFADDR_IN6, &ifra_in6);
126*043036a2SApple OSS Distributions }
127*043036a2SApple OSS Distributions 
128*043036a2SApple OSS Distributions static void
create_fake_interface(void)129*043036a2SApple OSS Distributions create_fake_interface(void)
130*043036a2SApple OSS Distributions {
131*043036a2SApple OSS Distributions 	int     error;
132*043036a2SApple OSS Distributions 
133*043036a2SApple OSS Distributions 	strlcpy(ifname1, FETH_NAME, sizeof(ifname1));
134*043036a2SApple OSS Distributions 	error = ifnet_create_2(ifname1, sizeof(ifname1));
135*043036a2SApple OSS Distributions 	if (error != 0) {
136*043036a2SApple OSS Distributions 		ifname1[0] = '\0';
137*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(error, "ifnet_create_2");
138*043036a2SApple OSS Distributions 	}
139*043036a2SApple OSS Distributions 	T_LOG("created %s", ifname1);
140*043036a2SApple OSS Distributions }
141*043036a2SApple OSS Distributions 
142*043036a2SApple OSS Distributions T_DECL(inet6_addr_mode_auto_to_manual, "inet6 address mode-switching (auto -> manual)")
143*043036a2SApple OSS Distributions {
144*043036a2SApple OSS Distributions 	struct in6_addr lladdr;
145*043036a2SApple OSS Distributions 	struct in6_addr newaddr;
146*043036a2SApple OSS Distributions 	unsigned int    if_index;
147*043036a2SApple OSS Distributions 
148*043036a2SApple OSS Distributions 	T_ATEND(cleanup);
149*043036a2SApple OSS Distributions 
150*043036a2SApple OSS Distributions 	create_fake_interface();
151*043036a2SApple OSS Distributions 	ifnet_start_ipv6(ifname1);
152*043036a2SApple OSS Distributions 
153*043036a2SApple OSS Distributions 	if_index = if_nametoindex(ifname1);
154*043036a2SApple OSS Distributions 	T_EXPECT_GT(if_index, 0, NULL);
155*043036a2SApple OSS Distributions 	T_ASSERT_EQ(inet6_get_linklocal_address(if_index, &lladdr), 1, NULL);
156*043036a2SApple OSS Distributions 
157*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(inet6_aifaddr(ifname1, &lladdr, NULL, 64, 123, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME), NULL);
158*043036a2SApple OSS Distributions 
159*043036a2SApple OSS Distributions 	/* Create address as an autoconfed address */
160*043036a2SApple OSS Distributions 	T_ASSERT_EQ(inet_pton(AF_INET6, "2001:db8::3", &newaddr), 1, NULL);
161*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(inet6_aifaddr(ifname1, &newaddr, NULL, 64, (IN6_IFF_AUTOCONF | IN6_IFF_TEMPORARY), ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME), NULL);
162*043036a2SApple OSS Distributions 
163*043036a2SApple OSS Distributions 	/* Now mark it as manual */
164*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(inet6_aifaddr(ifname1, &newaddr, NULL, 64, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME), NULL);
165*043036a2SApple OSS Distributions 
166*043036a2SApple OSS Distributions 	/* Deleting address should NOT result in panic */
167*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(inet6_difaddr(ifname1, &newaddr), NULL);
168*043036a2SApple OSS Distributions }
169*043036a2SApple OSS Distributions 
170*043036a2SApple OSS Distributions T_DECL(inet6_addr_mode_manual_to_auto, "inet6 address mode-switching (manual -> auto)")
171*043036a2SApple OSS Distributions {
172*043036a2SApple OSS Distributions 	struct in6_addr lladdr;
173*043036a2SApple OSS Distributions 	struct in6_addr newaddr;
174*043036a2SApple OSS Distributions 	unsigned int    if_index;
175*043036a2SApple OSS Distributions 
176*043036a2SApple OSS Distributions 	T_ATEND(cleanup);
177*043036a2SApple OSS Distributions 	create_fake_interface();
178*043036a2SApple OSS Distributions 
179*043036a2SApple OSS Distributions 	T_LOG("created %s", ifname1);
180*043036a2SApple OSS Distributions 
181*043036a2SApple OSS Distributions 	ifnet_start_ipv6(ifname1);
182*043036a2SApple OSS Distributions 
183*043036a2SApple OSS Distributions 	if_index = if_nametoindex(ifname1);
184*043036a2SApple OSS Distributions 	T_EXPECT_GT(if_index, 0, NULL);
185*043036a2SApple OSS Distributions 	T_ASSERT_EQ(inet6_get_linklocal_address(if_index, &lladdr), 1, NULL);
186*043036a2SApple OSS Distributions 
187*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(inet6_aifaddr(ifname1, &lladdr, NULL, 64, 123, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME), NULL);
188*043036a2SApple OSS Distributions 
189*043036a2SApple OSS Distributions 	/* Create address as a manual address */
190*043036a2SApple OSS Distributions 	T_ASSERT_EQ(inet_pton(AF_INET6, "2001:db8::1", &newaddr), 1, NULL);
191*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(inet6_aifaddr(ifname1, &newaddr, NULL, 64, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME), NULL);
192*043036a2SApple OSS Distributions 
193*043036a2SApple OSS Distributions 	/* Now make it autoconfed */
194*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(inet6_aifaddr(ifname1, &newaddr, NULL, 64, (IN6_IFF_AUTOCONF | IN6_IFF_TEMPORARY), ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME), NULL);
195*043036a2SApple OSS Distributions 
196*043036a2SApple OSS Distributions 	/* Deleting address should NOT result in panic */
197*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(inet6_difaddr(ifname1, &newaddr), NULL);
198*043036a2SApple OSS Distributions }
199