xref: /xnu-11215.41.3/tests/if_generation_id.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 2023-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 #include <darwintest.h>
30 
31 #include <sys/ioctl.h>
32 
33 #include <net/if.h>
34 #include <net/if_arp.h>
35 #include <net/if_fake_var.h>
36 
37 #include <stdlib.h>
38 #include <string.h>
39 #include <strings.h>
40 
41 #include "net_test_lib.h"
42 
43 T_GLOBAL_META(
44 	T_META_NAMESPACE("xnu.net"),
45 	T_META_ASROOT(true),
46 	T_META_RADAR_COMPONENT_NAME("xnu"),
47 	T_META_RADAR_COMPONENT_VERSION("networking"),
48 	T_META_CHECK_LEAKS(false));
49 
50 static char ifname1[IF_NAMESIZE];
51 
52 static void
cleanup(void)53 cleanup(void)
54 {
55 	if (ifname1[0] != '\0') {
56 		(void)ifnet_destroy(ifname1, false);
57 		T_LOG("ifnet_destroy %s", ifname1);
58 	}
59 }
60 
61 T_DECL(if_creation_generation_id, "network interface creation generation id")
62 {
63 	int     error;
64 	int     s = inet_dgram_socket_get();
65 
66 	T_ATEND(cleanup);
67 
68 #ifdef SIOCGIFGENERATIONID
69 	strlcpy(ifname1, FETH_NAME, sizeof(ifname1));
70 	error = ifnet_create_2(ifname1, sizeof(ifname1));
71 	if (error != 0) {
72 		ifname1[0] = '\0';
73 		T_ASSERT_POSIX_SUCCESS(error, "ifnet_create_2");
74 	}
75 	T_LOG("created %s", ifname1);
76 
77 	struct ifreq ifr = {};
78 
79 	strlcpy(ifr.ifr_name, ifname1, sizeof(ifr.ifr_name));
80 
81 	T_ASSERT_POSIX_SUCCESS(ioctl(s, SIOCGIFGENERATIONID, &ifr), NULL);
82 
83 	uint64_t if_generation_id = ifr.ifr_creation_generation_id;
84 	T_LOG("interface creation generation id: %llu", if_generation_id);
85 
86 	(void)ifnet_destroy(ifname1, true);
87 	T_LOG("destroyed %s", ifname1);
88 
89 	/* ifnet_create() will retry if creating fails due to EBUSY */
90 	T_ASSERT_POSIX_SUCCESS(ifnet_create(ifname1), NULL);
91 
92 	T_LOG("re-created %s", ifname1);
93 
94 	T_ASSERT_POSIX_SUCCESS(ioctl(s, SIOCGIFGENERATIONID, &ifr), NULL);
95 
96 	T_LOG("interface creation generation id: %llu", ifr.ifr_creation_generation_id);
97 
98 	T_ASSERT_NE_ULLONG(if_generation_id, ifr.ifr_creation_generation_id,
99 	    "interface generation id are different");
100 #else
101 	T_SKIP("SIOCGIFGENERATIONID does not exist");
102 #endif
103 }
104