1 /* vi: set sw=4 ts=4: */
3 * tun devices controller
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
10 * Licensed under GPLv2, see file LICENSE in this tarball for details.
12 #include <netinet/in.h>
14 #include <linux/if_tun.h>
17 /* TUNSETGROUP appeared in 2.6.23 */
19 #define TUNSETGROUP _IOW('T', 206, int)
22 #define IOCTL(a, b, c) ioctl_or_perror_and_die(a, b, c, NULL)
26 int tunctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
27 int tunctl_main(int argc UNUSED_PARAM, char **argv)
31 const char *opt_name = "tap%d";
32 const char *opt_device = "/dev/net/tun";
33 #if ENABLE_FEATURE_TUNCTL_UG
34 const char *opt_user, *opt_group;
35 long user = -1, group = -1;
40 OPT_f = 1 << 0, // control device name (/dev/net/tun)
41 OPT_t = 1 << 1, // create named interface
42 OPT_d = 1 << 2, // delete named interface
43 #if ENABLE_FEATURE_TUNCTL_UG
44 OPT_u = 1 << 3, // set new interface owner
45 OPT_g = 1 << 4, // set new interface group
46 OPT_b = 1 << 5, // brief output
50 opt_complementary = "=0:t--d:d--t"; // no arguments; t ^ d
51 opts = getopt32(argv, "f:t:d:" USE_FEATURE_TUNCTL_UG("u:g:b"),
52 &opt_device, &opt_name, &opt_name
53 USE_FEATURE_TUNCTL_UG(, &opt_user, &opt_group));
56 memset(&ifr, 0, sizeof(ifr));
57 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
58 strncpy_IFNAMSIZ(ifr.ifr_name, opt_name);
61 fd = xopen(opt_device, O_RDWR);
62 IOCTL(fd, TUNSETIFF, (void *)&ifr);
66 IOCTL(fd, TUNSETPERSIST, (void *)(uintptr_t)0);
67 bb_info_msg("Set '%s' %spersistent", ifr.ifr_name, "non");
72 #if ENABLE_FEATURE_TUNCTL_UG
74 group = xgroup2gid(opt_group);
75 IOCTL(fd, TUNSETGROUP, (void *)(uintptr_t)group);
79 user = xuname2uid(opt_user);
80 IOCTL(fd, TUNSETOWNER, (void *)(uintptr_t)user);
82 IOCTL(fd, TUNSETPERSIST, (void *)(uintptr_t)1);
85 #if ENABLE_FEATURE_TUNCTL_UG
89 printf("Set '%s' %spersistent", ifr.ifr_name, "");
90 printf(" and owned by uid %ld", user);
92 printf(" gid %ld", group);
105 int tunctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
106 int tunctl_main(int argc UNUSED_PARAM, char **argv)
110 const char *opt_name = "tap%d";
111 const char *opt_device = "/dev/net/tun";
115 OPT_f = 1 << 0, // control device name (/dev/net/tun)
116 OPT_t = 1 << 1, // create named interface
117 OPT_d = 1 << 2, // delete named interface
120 opt_complementary = "=0:t--d:d--t"; // no arguments; t ^ d
121 opts = getopt32(argv, "f:t:d:u:g:b", // u, g, b accepted and ignored
122 &opt_device, &opt_name, &opt_name, NULL, NULL);
124 // set interface name
125 memset(&ifr, 0, sizeof(ifr));
126 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
127 strncpy_IFNAMSIZ(ifr.ifr_name, opt_name);
130 fd = xopen(opt_device, O_RDWR);
131 IOCTL(fd, TUNSETIFF, (void *)&ifr);
133 // create or delete interface
134 IOCTL(fd, TUNSETPERSIST, (void *)(uintptr_t)(0 == (opts & OPT_d)));