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