xref: /xnu-10002.61.3/tests/if_generation_id.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1 /*
2  * Copyright (c) 2023 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 static int s = -1;
52 
53 static void
cleanup(void)54 cleanup(void)
55 {
56 	if (s != -1) {
57 		(void)ifnet_destroy(s, ifname1, false);
58 		T_LOG("ifnet_destroy %s", ifname1);
59 	}
60 }
61 
62 T_DECL(if_creation_generation_id, "network interface creation generation id")
63 {
64 	T_ATEND(cleanup);
65 
66 #ifdef SIOCGIFGENERATIONID
67 	s = inet_dgram_socket();
68 
69 	strlcpy(ifname1, FETH_NAME, sizeof(ifname1));
70 	T_ASSERT_POSIX_SUCCESS(ifnet_create_2(s, ifname1, sizeof(ifname1)), NULL);
71 
72 	T_LOG("created %s", ifname1);
73 
74 	struct ifreq ifr = {};
75 
76 	strlcpy(ifr.ifr_name, ifname1, sizeof(ifr.ifr_name));
77 
78 	T_ASSERT_POSIX_SUCCESS(ioctl(s, SIOCGIFGENERATIONID, &ifr), NULL);
79 
80 	uint64_t if_generation_id = ifr.ifr_creation_generation_id;
81 	T_LOG("interface creation generation id: %llu", if_generation_id);
82 
83 	(void)ifnet_destroy(s, ifname1, false);
84 	T_LOG("destroyed %s", ifname1);
85 
86 	/* wait for the interface to be fully detached */
87 	sleep(1);
88 
89 	T_ASSERT_POSIX_SUCCESS(ifnet_create(s, ifname1), NULL);
90 
91 	T_LOG("re-created %s", ifname1);
92 
93 	T_ASSERT_POSIX_SUCCESS(ioctl(s, SIOCGIFGENERATIONID, &ifr), NULL);
94 
95 	T_LOG("interface creation generation id: %llu", ifr.ifr_creation_generation_id);
96 
97 	T_ASSERT_NE_ULLONG(if_generation_id, ifr.ifr_creation_generation_id,
98 	    "interface generation id are different");
99 #else
100 	T_SKIP("SIOCGIFGENERATIONID does not exist");
101 #endif
102 }
103