xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ioctl/ioctl03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2008
4  * Copyright (c) Linux Test Project, 2017-2019
5  * Author: Rusty Russell <[email protected]>
6  * Ported to LTP: subrata <[email protected]>
7  * Test ioriginally written for kernel 2.6.27.
8  */
9 
10 /*\
11  * [Description]
12  *
13  * Test whether all the valid IFF flags are returned properly by implementation
14  * of TUNGETFEATURES ioctl.
15  */
16 
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <errno.h>
22 #include <linux/if_tun.h>
23 #include "tst_test.h"
24 
25 #ifndef TUNGETFEATURES
26 #define TUNGETFEATURES _IOR('T', 207, unsigned int)
27 #endif
28 
29 #ifndef IFF_VNET_HDR
30 #define IFF_VNET_HDR	0x4000
31 #endif
32 
33 #ifndef IFF_MULTI_QUEUE
34 #define IFF_MULTI_QUEUE	0x0100
35 #endif
36 
37 #ifndef IFF_NAPI
38 #define IFF_NAPI	0x0010
39 #endif
40 
41 #ifndef IFF_NAPI_FRAGS
42 #define IFF_NAPI_FRAGS	0x0020
43 #endif
44 
45 #ifndef IFF_NO_CARRIER
46 #define IFF_NO_CARRIER	0x0040
47 #endif
48 
49 static struct {
50 	unsigned int flag;
51 	const char *name;
52 } known_flags[] = {
53 	{IFF_TUN, "TUN"},
54 	{IFF_TAP, "TAP"},
55 	{IFF_NO_PI, "NO_PI"},
56 	{IFF_ONE_QUEUE, "ONE_QUEUE"},
57 	{IFF_VNET_HDR, "VNET_HDR"},
58 	{IFF_MULTI_QUEUE, "MULTI_QUEUE"},
59 	{IFF_NAPI, "IFF_NAPI"},
60 	{IFF_NAPI_FRAGS, "IFF_NAPI_FRAGS"},
61 	{IFF_NO_CARRIER, "IFF_NO_CARRIER"}
62 };
63 
verify_features(void)64 static void verify_features(void)
65 {
66 	unsigned int features, i;
67 
68 	int netfd = open("/dev/net/tun", O_RDWR);
69 
70 	/* Android has tun at /dev/tun */
71 	if (netfd == -1 && (errno == ENODEV || errno == ENOENT))
72 		netfd = open("/dev/tun", O_RDWR);
73 
74 	if (netfd == -1) {
75 		if (errno == ENODEV || errno == ENOENT)
76 			tst_brk(TCONF, "TUN support is missing?");
77 
78 		tst_brk(TBROK | TERRNO, "opening /dev/net/tun failed");
79 	}
80 
81 	SAFE_IOCTL(netfd, TUNGETFEATURES, &features);
82 
83 	tst_res(TINFO, "Available features are: %#x", features);
84 	for (i = 0; i < ARRAY_SIZE(known_flags); i++) {
85 		if (features & known_flags[i].flag) {
86 			features &= ~known_flags[i].flag;
87 			tst_res(TPASS, "%s %#x", known_flags[i].name,
88 				 known_flags[i].flag);
89 		}
90 	}
91 	if (features)
92 		tst_res(TFAIL, "(UNKNOWN %#x)", features);
93 
94 	SAFE_CLOSE(netfd);
95 }
96 
97 static struct tst_test test = {
98 	.test_all = verify_features,
99 	.needs_root = 1,
100 };
101