xref: /freebsd/tests/sys/net/transient_tuntap.c (revision a1174b3b1174754b1f69406bff4456d002e8f583)
1 /*-
2  * Copyright (c) 2024 Kyle Evans <kevans@FreeBSD.org>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 /*
8  * This test simply configures the tunnel as transient and exits.  By the time
9  * we return, the tunnel should be gone because the last reference disappears.
10  */
11 
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
14 #include <net/if_tun.h>
15 #include <net/if_tap.h>
16 
17 #include <assert.h>
18 #include <err.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 int
main(int argc,char * argv[])24 main(int argc, char *argv[])
25 {
26 	unsigned long tunreq;
27 	const char *tundev;
28 	int one = 1, tunfd;
29 
30 	assert(argc > 1);
31 	tundev = argv[1];
32 
33 	tunfd = open(tundev, O_RDWR);
34 	assert(tunfd >= 0);
35 
36 	/*
37 	 * These are technically the same request, but we'll use the technically
38 	 * correct one just in case.
39 	 */
40 	if (strstr(tundev, "tun") != NULL) {
41 		tunreq = TUNSTRANSIENT;
42 	} else {
43 		assert(strstr(tundev, "tap") != NULL);
44 		tunreq = TAPSTRANSIENT;
45 	}
46 
47 	if (ioctl(tunfd, tunreq, &one) == -1)
48 		err(1, "ioctl");
49 
50 	/* Final close should destroy the tunnel automagically. */
51 	close(tunfd);
52 
53 	return (0);
54 }
55