wget: add a note about bug 3625
[oweals/busybox.git] / networking / tunctl.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * tun devices controller
4  *
5  * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
6  *
7  * Original code:
8  *      Jeff Dike
9  *
10  * Licensed under GPLv2, see file LICENSE in this source tree.
11  */
12
13 //usage:#define tunctl_trivial_usage
14 //usage:       "[-f device] ([-t name] | -d name)" IF_FEATURE_TUNCTL_UG(" [-u owner] [-g group] [-b]")
15 //usage:#define tunctl_full_usage "\n\n"
16 //usage:       "Create or delete tun interfaces\n"
17 //usage:     "\nOptions:"
18 //usage:     "\n        -f name         tun device (/dev/net/tun)"
19 //usage:     "\n        -t name         Create iface 'name'"
20 //usage:     "\n        -d name         Delete iface 'name'"
21 //usage:        IF_FEATURE_TUNCTL_UG(
22 //usage:     "\n        -u owner        Set iface owner"
23 //usage:     "\n        -g group        Set iface group"
24 //usage:     "\n        -b              Brief output"
25 //usage:        )
26 //usage:
27 //usage:#define tunctl_example_usage
28 //usage:       "# tunctl\n"
29 //usage:       "# tunctl -d tun0\n"
30
31 #include <netinet/in.h>
32 #include <net/if.h>
33 #include <linux/if_tun.h>
34 #include "libbb.h"
35
36 /* TUNSETGROUP appeared in 2.6.23 */
37 #ifndef TUNSETGROUP
38 #define TUNSETGROUP _IOW('T', 206, int)
39 #endif
40
41 #define IOCTL(a, b, c) ioctl_or_perror_and_die(a, b, c, NULL)
42
43 #if 1
44
45 int tunctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
46 int tunctl_main(int argc UNUSED_PARAM, char **argv)
47 {
48         struct ifreq ifr;
49         int fd;
50         const char *opt_name = "tap%d";
51         const char *opt_device = "/dev/net/tun";
52 #if ENABLE_FEATURE_TUNCTL_UG
53         const char *opt_user, *opt_group;
54         long user = -1, group = -1;
55 #endif
56         unsigned opts;
57
58         enum {
59                 OPT_f = 1 << 0, // control device name (/dev/net/tun)
60                 OPT_t = 1 << 1, // create named interface
61                 OPT_d = 1 << 2, // delete named interface
62 #if ENABLE_FEATURE_TUNCTL_UG
63                 OPT_u = 1 << 3, // set new interface owner
64                 OPT_g = 1 << 4, // set new interface group
65                 OPT_b = 1 << 5, // brief output
66 #endif
67         };
68
69         opt_complementary = "=0:t--d:d--t"; // no arguments; t ^ d
70         opts = getopt32(argv, "f:t:d:" IF_FEATURE_TUNCTL_UG("u:g:b"),
71                         &opt_device, &opt_name, &opt_name
72                         IF_FEATURE_TUNCTL_UG(, &opt_user, &opt_group));
73
74         // select device
75         memset(&ifr, 0, sizeof(ifr));
76         ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
77         strncpy_IFNAMSIZ(ifr.ifr_name, opt_name);
78
79         // open device
80         fd = xopen(opt_device, O_RDWR);
81         IOCTL(fd, TUNSETIFF, (void *)&ifr);
82
83         // delete?
84         if (opts & OPT_d) {
85                 IOCTL(fd, TUNSETPERSIST, (void *)(uintptr_t)0);
86                 bb_info_msg("Set '%s' %spersistent", ifr.ifr_name, "non");
87                 return EXIT_SUCCESS;
88         }
89
90         // create
91 #if ENABLE_FEATURE_TUNCTL_UG
92         if (opts & OPT_g) {
93                 group = xgroup2gid(opt_group);
94                 IOCTL(fd, TUNSETGROUP, (void *)(uintptr_t)group);
95         } else
96                 user = geteuid();
97         if (opts & OPT_u)
98                 user = xuname2uid(opt_user);
99         IOCTL(fd, TUNSETOWNER, (void *)(uintptr_t)user);
100 #endif
101         IOCTL(fd, TUNSETPERSIST, (void *)(uintptr_t)1);
102
103         // show info
104 #if ENABLE_FEATURE_TUNCTL_UG
105         if (opts & OPT_b) {
106                 puts(ifr.ifr_name);
107         } else {
108                 printf("Set '%s' %spersistent", ifr.ifr_name, "");
109                 printf(" and owned by uid %ld", user);
110                 if (group != -1)
111                         printf(" gid %ld", group);
112                 bb_putchar('\n');
113         }
114 #else
115         puts(ifr.ifr_name);
116 #endif
117         return EXIT_SUCCESS;
118 }
119
120 #else
121
122 /* -210 bytes: */
123
124 int tunctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
125 int tunctl_main(int argc UNUSED_PARAM, char **argv)
126 {
127         struct ifreq ifr;
128         int fd;
129         const char *opt_name = "tap%d";
130         const char *opt_device = "/dev/net/tun";
131         unsigned opts;
132
133         enum {
134                 OPT_f = 1 << 0, // control device name (/dev/net/tun)
135                 OPT_t = 1 << 1, // create named interface
136                 OPT_d = 1 << 2, // delete named interface
137         };
138
139         opt_complementary = "=0:t--d:d--t"; // no arguments; t ^ d
140         opts = getopt32(argv, "f:t:d:u:g:b", // u, g, b accepted and ignored
141                         &opt_device, &opt_name, &opt_name, NULL, NULL);
142
143         // set interface name
144         memset(&ifr, 0, sizeof(ifr));
145         ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
146         strncpy_IFNAMSIZ(ifr.ifr_name, opt_name);
147
148         // open device
149         fd = xopen(opt_device, O_RDWR);
150         IOCTL(fd, TUNSETIFF, (void *)&ifr);
151
152         // create or delete interface
153         IOCTL(fd, TUNSETPERSIST, (void *)(uintptr_t)(0 == (opts & OPT_d)));
154
155         return EXIT_SUCCESS;
156 }
157
158 #endif