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 static struct {
44 unsigned int flag;
45 const char *name;
46 } known_flags[] = {
47 {IFF_TUN, "TUN"},
48 {IFF_TAP, "TAP"},
49 {IFF_NO_PI, "NO_PI"},
50 {IFF_ONE_QUEUE, "ONE_QUEUE"},
51 {IFF_VNET_HDR, "VNET_HDR"},
52 {IFF_MULTI_QUEUE, "MULTI_QUEUE"},
53 {IFF_NAPI, "IFF_NAPI"},
54 {IFF_NAPI_FRAGS, "IFF_NAPI_FRAGS"}
55 };
56
verify_features(void)57 static void verify_features(void)
58 {
59 unsigned int features, i;
60
61 int netfd = open("/dev/net/tun", O_RDWR);
62
63 /* Android has tun at /dev/tun */
64 if (netfd == -1 && (errno == ENODEV || errno == ENOENT))
65 netfd = open("/dev/tun", O_RDWR);
66
67 if (netfd == -1) {
68 if (errno == ENODEV || errno == ENOENT)
69 tst_brk(TCONF, "TUN support is missing?");
70
71 tst_brk(TBROK | TERRNO, "opening /dev/net/tun failed");
72 }
73
74 SAFE_IOCTL(netfd, TUNGETFEATURES, &features);
75
76 tst_res(TINFO, "Available features are: %#x", features);
77 for (i = 0; i < ARRAY_SIZE(known_flags); i++) {
78 if (features & known_flags[i].flag) {
79 features &= ~known_flags[i].flag;
80 tst_res(TPASS, "%s %#x", known_flags[i].name,
81 known_flags[i].flag);
82 }
83 }
84 if (features)
85 tst_res(TFAIL, "(UNKNOWN %#x)", features);
86
87 SAFE_CLOSE(netfd);
88 }
89
90 static struct tst_test test = {
91 .test_all = verify_features,
92 .needs_root = 1,
93 };
94