1 /*
2 * iptunnel.c "ip tuntap"
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: David Woodhouse <David.Woodhouse@intel.com>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <sys/ioctl.h>
21 #include <linux/if.h>
22 #include <linux/if_tun.h>
23 #include <pwd.h>
24 #include <grp.h>
25 #include <fcntl.h>
26 #include <dirent.h>
27 #include <errno.h>
28
29 #include "rt_names.h"
30 #include "utils.h"
31 #include "ip_common.h"
32
33 #define TUNDEV "/dev/net/tun"
34
35 static void usage(void) __attribute__((noreturn));
36
usage(void)37 static void usage(void)
38 {
39 fprintf(stderr, "Usage: ip tuntap { add | del | show | list | lst | help } [ dev PHYS_DEV ] \n");
40 fprintf(stderr, " [ mode { tun | tap } ] [ user USER ] [ group GROUP ]\n");
41 fprintf(stderr, " [ one_queue ] [ pi ] [ vnet_hdr ] [ multi_queue ]\n");
42 fprintf(stderr, "\n");
43 fprintf(stderr, "Where: USER := { STRING | NUMBER }\n");
44 fprintf(stderr, " GROUP := { STRING | NUMBER }\n");
45 exit(-1);
46 }
47
tap_add_ioctl(struct ifreq * ifr,uid_t uid,gid_t gid)48 static int tap_add_ioctl(struct ifreq *ifr, uid_t uid, gid_t gid)
49 {
50 int fd;
51 int ret = -1;
52
53 #ifdef IFF_TUN_EXCL
54 ifr->ifr_flags |= IFF_TUN_EXCL;
55 #endif
56
57 fd = open(TUNDEV, O_RDWR);
58 if (fd < 0) {
59 perror("open");
60 return -1;
61 }
62 if (ioctl(fd, TUNSETIFF, ifr)) {
63 perror("ioctl(TUNSETIFF)");
64 goto out;
65 }
66 if (uid != -1 && ioctl(fd, TUNSETOWNER, uid)) {
67 perror("ioctl(TUNSETOWNER)");
68 goto out;
69 }
70 if (gid != -1 && ioctl(fd, TUNSETGROUP, gid)) {
71 perror("ioctl(TUNSETGROUP)");
72 goto out;
73 }
74 if (ioctl(fd, TUNSETPERSIST, 1)) {
75 perror("ioctl(TUNSETPERSIST)");
76 goto out;
77 }
78 ret = 0;
79 out:
80 close(fd);
81 return ret;
82 }
83
tap_del_ioctl(struct ifreq * ifr)84 static int tap_del_ioctl(struct ifreq *ifr)
85 {
86 int fd = open(TUNDEV, O_RDWR);
87 int ret = -1;
88
89 if (fd < 0) {
90 perror("open");
91 return -1;
92 }
93 if (ioctl(fd, TUNSETIFF, ifr)) {
94 perror("ioctl(TUNSETIFF)");
95 goto out;
96 }
97 if (ioctl(fd, TUNSETPERSIST, 0)) {
98 perror("ioctl(TUNSETPERSIST)");
99 goto out;
100 }
101 ret = 0;
102 out:
103 close(fd);
104 return ret;
105
106 }
parse_args(int argc,char ** argv,struct ifreq * ifr,uid_t * uid,gid_t * gid)107 static int parse_args(int argc, char **argv, struct ifreq *ifr, uid_t *uid, gid_t *gid)
108 {
109 int count = 0;
110
111 memset(ifr, 0, sizeof(*ifr));
112
113 ifr->ifr_flags |= IFF_NO_PI;
114
115 while (argc > 0) {
116 if (matches(*argv, "mode") == 0) {
117 NEXT_ARG();
118 if (matches(*argv, "tun") == 0) {
119 if (ifr->ifr_flags & IFF_TAP) {
120 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
121 exit(-1);
122 }
123 ifr->ifr_flags |= IFF_TUN;
124 } else if (matches(*argv, "tap") == 0) {
125 if (ifr->ifr_flags & IFF_TUN) {
126 fprintf(stderr,"You managed to ask for more than one tunnel mode.\n");
127 exit(-1);
128 }
129 ifr->ifr_flags |= IFF_TAP;
130 } else {
131 fprintf(stderr,"Unknown tunnel mode \"%s\"\n", *argv);
132 exit(-1);
133 }
134 } else if (uid && matches(*argv, "user") == 0) {
135 char *end;
136 unsigned long user;
137
138 NEXT_ARG();
139 if (**argv && ((user = strtol(*argv, &end, 10)), !*end))
140 *uid = user;
141 else {
142 struct passwd *pw = getpwnam(*argv);
143 if (!pw) {
144 fprintf(stderr, "invalid user \"%s\"\n", *argv);
145 exit(-1);
146 }
147 *uid = pw->pw_uid;
148 }
149 } else if (gid && matches(*argv, "group") == 0) {
150 char *end;
151 unsigned long group;
152
153 NEXT_ARG();
154
155 if (**argv && ((group = strtol(*argv, &end, 10)), !*end))
156 *gid = group;
157 else {
158 struct group *gr = getgrnam(*argv);
159 if (!gr) {
160 fprintf(stderr, "invalid group \"%s\"\n", *argv);
161 exit(-1);
162 }
163 *gid = gr->gr_gid;
164 }
165 } else if (matches(*argv, "pi") == 0) {
166 ifr->ifr_flags &= ~IFF_NO_PI;
167 } else if (matches(*argv, "one_queue") == 0) {
168 ifr->ifr_flags |= IFF_ONE_QUEUE;
169 } else if (matches(*argv, "vnet_hdr") == 0) {
170 ifr->ifr_flags |= IFF_VNET_HDR;
171 } else if (matches(*argv, "multi_queue") == 0) {
172 ifr->ifr_flags |= IFF_MULTI_QUEUE;
173 } else if (matches(*argv, "dev") == 0) {
174 NEXT_ARG();
175 strncpy(ifr->ifr_name, *argv, IFNAMSIZ-1);
176 } else {
177 if (matches(*argv, "name") == 0) {
178 NEXT_ARG();
179 } else if (matches(*argv, "help") == 0)
180 usage();
181 if (ifr->ifr_name[0])
182 duparg2("name", *argv);
183 strncpy(ifr->ifr_name, *argv, IFNAMSIZ);
184 }
185 count++;
186 argc--; argv++;
187 }
188
189 if (!(ifr->ifr_flags & TUN_TYPE_MASK)) {
190 fprintf(stderr, "You failed to specify a tunnel mode\n");
191 return -1;
192 }
193
194 return 0;
195 }
196
197
do_add(int argc,char ** argv)198 static int do_add(int argc, char **argv)
199 {
200 struct ifreq ifr;
201 uid_t uid = -1;
202 gid_t gid = -1;
203
204 if (parse_args(argc, argv, &ifr, &uid, &gid) < 0)
205 return -1;
206
207 return tap_add_ioctl(&ifr, uid, gid);
208 }
209
do_del(int argc,char ** argv)210 static int do_del(int argc, char **argv)
211 {
212 struct ifreq ifr;
213
214 if (parse_args(argc, argv, &ifr, NULL, NULL) < 0)
215 return -1;
216
217 return tap_del_ioctl(&ifr);
218 }
219
read_prop(char * dev,char * prop,long * value)220 static int read_prop(char *dev, char *prop, long *value)
221 {
222 char fname[IFNAMSIZ+25], buf[80], *endp;
223 ssize_t len;
224 int fd;
225 long result;
226
227 sprintf(fname, "/sys/class/net/%s/%s", dev, prop);
228 fd = open(fname, O_RDONLY);
229 if (fd < 0) {
230 if (strcmp(prop, "tun_flags"))
231 fprintf(stderr, "open %s: %s\n", fname,
232 strerror(errno));
233 return -1;
234 }
235 len = read(fd, buf, sizeof(buf)-1);
236 close(fd);
237 if (len < 0) {
238 fprintf(stderr, "read %s: %s", fname, strerror(errno));
239 return -1;
240 }
241
242 buf[len] = 0;
243 result = strtol(buf, &endp, 0);
244 if (*endp != '\n') {
245 fprintf(stderr, "Failed to parse %s\n", fname);
246 return -1;
247 }
248 *value = result;
249 return 0;
250 }
251
print_flags(long flags)252 static void print_flags(long flags)
253 {
254 if (flags & IFF_TUN)
255 printf(" tun");
256
257 if (flags & IFF_TAP)
258 printf(" tap");
259
260 if (!(flags & IFF_NO_PI))
261 printf(" pi");
262
263 if (flags & IFF_ONE_QUEUE)
264 printf(" one_queue");
265
266 if (flags & IFF_VNET_HDR)
267 printf(" vnet_hdr");
268
269 flags &= ~(IFF_TUN|IFF_TAP|IFF_NO_PI|IFF_ONE_QUEUE|IFF_VNET_HDR);
270 if (flags)
271 printf(" UNKNOWN_FLAGS:%lx", flags);
272 }
273
do_show(int argc,char ** argv)274 static int do_show(int argc, char **argv)
275 {
276 DIR *dir;
277 struct dirent *d;
278 long flags, owner = -1, group = -1;
279
280 dir = opendir("/sys/class/net");
281 if (!dir) {
282 perror("opendir");
283 return -1;
284 }
285 while ((d = readdir(dir))) {
286 if (d->d_name[0] == '.' &&
287 (d->d_name[1] == 0 || d->d_name[1] == '.'))
288 continue;
289
290 if (read_prop(d->d_name, "tun_flags", &flags))
291 continue;
292
293 read_prop(d->d_name, "owner", &owner);
294 read_prop(d->d_name, "group", &group);
295
296 printf("%s:", d->d_name);
297 print_flags(flags);
298 if (owner != -1)
299 printf(" user %ld", owner);
300 if (group != -1)
301 printf(" group %ld", group);
302 printf("\n");
303 }
304 closedir(dir);
305 return 0;
306 }
307
do_iptuntap(int argc,char ** argv)308 int do_iptuntap(int argc, char **argv)
309 {
310 if (argc > 0) {
311 if (matches(*argv, "add") == 0)
312 return do_add(argc-1, argv+1);
313 if (matches(*argv, "delete") == 0)
314 return do_del(argc-1, argv+1);
315 if (matches(*argv, "show") == 0 ||
316 matches(*argv, "lst") == 0 ||
317 matches(*argv, "list") == 0)
318 return do_show(argc-1, argv+1);
319 if (matches(*argv, "help") == 0)
320 usage();
321 } else
322 return do_show(0, NULL);
323
324 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tuntap help\".\n",
325 *argv);
326 exit(-1);
327 }
328