• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <rusty@rustcorp.com.au>
6  * Ported to LTP: subrata <subrata@linux.vnet.ibm.com>
7  */
8 
9 /*
10  * This program tests whether all the valid IFF flags are
11  * returned properly by implementation of TUNGETFEATURES ioctl
12  * on kernel 2.6.27
13  */
14 
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <linux/if_tun.h>
21 #include "tst_test.h"
22 
23 #ifndef TUNGETFEATURES
24 #define TUNGETFEATURES _IOR('T', 207, unsigned int)
25 #endif
26 
27 #ifndef IFF_VNET_HDR
28 #define IFF_VNET_HDR	0x4000
29 #endif
30 
31 #ifndef IFF_MULTI_QUEUE
32 #define IFF_MULTI_QUEUE	0x0100
33 #endif
34 
35 #ifndef IFF_NAPI
36 #define IFF_NAPI	0x0010
37 #endif
38 
39 #ifndef IFF_NAPI_FRAGS
40 #define IFF_NAPI_FRAGS	0x0020
41 #endif
42 
43 #ifndef IFF_NO_CARRIER
44 #define IFF_NO_CARRIER	0x0040
45 #endif
46 
47 static struct {
48 	unsigned int flag;
49 	const char *name;
50 } known_flags[] = {
51 	{IFF_TUN, "TUN"},
52 	{IFF_TAP, "TAP"},
53 	{IFF_NO_PI, "NO_PI"},
54 	{IFF_ONE_QUEUE, "ONE_QUEUE"},
55 	{IFF_VNET_HDR, "VNET_HDR"},
56 	{IFF_MULTI_QUEUE, "MULTI_QUEUE"},
57 	{IFF_NAPI, "IFF_NAPI"},
58 	{IFF_NAPI_FRAGS, "IFF_NAPI_FRAGS"},
59 	{IFF_NO_CARRIER, "IFF_NO_CARRIER"}
60 };
61 
verify_features(void)62 static void verify_features(void)
63 {
64 	unsigned int features, i;
65 
66 	int netfd = open("/dev/net/tun", O_RDWR);
67 
68 	/* Android has tun at /dev/tun */
69 	if (netfd == -1 && (errno == ENODEV || errno == ENOENT))
70 		netfd = open("/dev/tun", O_RDWR);
71 
72 	if (netfd == -1) {
73 		if (errno == ENODEV || errno == ENOENT)
74 			tst_brk(TCONF, "TUN support is missing?");
75 
76 		tst_brk(TBROK | TERRNO, "opening /dev/net/tun failed");
77 	}
78 
79 	SAFE_IOCTL(netfd, TUNGETFEATURES, &features);
80 
81 	tst_res(TINFO, "Available features are: %#x", features);
82 	for (i = 0; i < ARRAY_SIZE(known_flags); i++) {
83 		if (features & known_flags[i].flag) {
84 			features &= ~known_flags[i].flag;
85 			tst_res(TPASS, "%s %#x", known_flags[i].name,
86 				 known_flags[i].flag);
87 		}
88 	}
89 	if (features)
90 		tst_res(TFAIL, "(UNKNOWN %#x)", features);
91 
92 	SAFE_CLOSE(netfd);
93 }
94 
95 static struct tst_test test = {
96 	.test_all = verify_features,
97 	.needs_root = 1,
98 };
99