xref: /xnu-8020.140.41/tests/net_tun_pr_35136664.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions #include <sys/ioctl.h>
2*27b03b36SApple OSS Distributions #include <sys/socket.h>
3*27b03b36SApple OSS Distributions #include <sys/kern_control.h>
4*27b03b36SApple OSS Distributions #include <sys/sys_domain.h>
5*27b03b36SApple OSS Distributions 
6*27b03b36SApple OSS Distributions #include <net/if_utun.h>
7*27b03b36SApple OSS Distributions #include <net/if_ipsec.h>
8*27b03b36SApple OSS Distributions 
9*27b03b36SApple OSS Distributions #include <darwintest.h>
10*27b03b36SApple OSS Distributions #include <darwintest_utils.h>
11*27b03b36SApple OSS Distributions 
12*27b03b36SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.net"),
13*27b03b36SApple OSS Distributions     T_META_RUN_CONCURRENTLY(true),
14*27b03b36SApple OSS Distributions     T_META_ASROOT(true));
15*27b03b36SApple OSS Distributions 
16*27b03b36SApple OSS Distributions T_DECL(PR_35136664_utun,
17*27b03b36SApple OSS Distributions     "This bind a utun and close it without connecting")
18*27b03b36SApple OSS Distributions {
19*27b03b36SApple OSS Distributions 	int tunsock;
20*27b03b36SApple OSS Distributions 	struct ctl_info kernctl_info;
21*27b03b36SApple OSS Distributions 	struct sockaddr_ctl kernctl_addr;
22*27b03b36SApple OSS Distributions 
23*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(tunsock = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), NULL);
24*27b03b36SApple OSS Distributions 
25*27b03b36SApple OSS Distributions 	memset(&kernctl_info, 0, sizeof(kernctl_info));
26*27b03b36SApple OSS Distributions 	strlcpy(kernctl_info.ctl_name, UTUN_CONTROL_NAME, sizeof(kernctl_info.ctl_name));
27*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ioctl(tunsock, CTLIOCGINFO, &kernctl_info), NULL);
28*27b03b36SApple OSS Distributions 
29*27b03b36SApple OSS Distributions 	memset(&kernctl_addr, 0, sizeof(kernctl_addr));
30*27b03b36SApple OSS Distributions 	kernctl_addr.sc_len = sizeof(kernctl_addr);
31*27b03b36SApple OSS Distributions 	kernctl_addr.sc_family = AF_SYSTEM;
32*27b03b36SApple OSS Distributions 	kernctl_addr.ss_sysaddr = AF_SYS_CONTROL;
33*27b03b36SApple OSS Distributions 	kernctl_addr.sc_id = kernctl_info.ctl_id;
34*27b03b36SApple OSS Distributions 	kernctl_addr.sc_unit = 0;
35*27b03b36SApple OSS Distributions 
36*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(bind(tunsock, (struct sockaddr *)&kernctl_addr, sizeof(kernctl_addr)), NULL);
37*27b03b36SApple OSS Distributions 
38*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(close(tunsock), NULL);
39*27b03b36SApple OSS Distributions }
40*27b03b36SApple OSS Distributions 
41*27b03b36SApple OSS Distributions T_DECL(PR_35136664_ipsec,
42*27b03b36SApple OSS Distributions     "This bind a ipsec and close it without connecting")
43*27b03b36SApple OSS Distributions {
44*27b03b36SApple OSS Distributions 	int tunsock;
45*27b03b36SApple OSS Distributions 	struct ctl_info kernctl_info;
46*27b03b36SApple OSS Distributions 	struct sockaddr_ctl kernctl_addr;
47*27b03b36SApple OSS Distributions 
48*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(tunsock = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), NULL);
49*27b03b36SApple OSS Distributions 
50*27b03b36SApple OSS Distributions 	memset(&kernctl_info, 0, sizeof(kernctl_info));
51*27b03b36SApple OSS Distributions 	strlcpy(kernctl_info.ctl_name, IPSEC_CONTROL_NAME, sizeof(kernctl_info.ctl_name));
52*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ioctl(tunsock, CTLIOCGINFO, &kernctl_info), NULL);
53*27b03b36SApple OSS Distributions 
54*27b03b36SApple OSS Distributions 	memset(&kernctl_addr, 0, sizeof(kernctl_addr));
55*27b03b36SApple OSS Distributions 	kernctl_addr.sc_len = sizeof(kernctl_addr);
56*27b03b36SApple OSS Distributions 	kernctl_addr.sc_family = AF_SYSTEM;
57*27b03b36SApple OSS Distributions 	kernctl_addr.ss_sysaddr = AF_SYS_CONTROL;
58*27b03b36SApple OSS Distributions 	kernctl_addr.sc_id = kernctl_info.ctl_id;
59*27b03b36SApple OSS Distributions 	kernctl_addr.sc_unit = 0;
60*27b03b36SApple OSS Distributions 
61*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(bind(tunsock, (struct sockaddr *)&kernctl_addr, sizeof(kernctl_addr)), NULL);
62*27b03b36SApple OSS Distributions 
63*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(close(tunsock), NULL);
64*27b03b36SApple OSS Distributions }
65