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 #include <glob.h>
29
30 #include "rt_names.h"
31 #include "utils.h"
32 #include "ip_common.h"
33
34 #define TUNDEV "/dev/net/tun"
35
36 static void usage(void) __attribute__((noreturn));
37
usage(void)38 static void usage(void)
39 {
40 fprintf(stderr, "Usage: ip tuntap { add | del | show | list | lst | help } [ dev PHYS_DEV ]\n");
41 fprintf(stderr, " [ mode { tun | tap } ] [ user USER ] [ group GROUP ]\n");
42 fprintf(stderr, " [ one_queue ] [ pi ] [ vnet_hdr ] [ multi_queue ] [ name NAME ]\n");
43 fprintf(stderr, "\n");
44 fprintf(stderr, "Where: USER := { STRING | NUMBER }\n");
45 fprintf(stderr, " GROUP := { STRING | NUMBER }\n");
46 exit(-1);
47 }
48
tap_add_ioctl(struct ifreq * ifr,uid_t uid,gid_t gid)49 static int tap_add_ioctl(struct ifreq *ifr, uid_t uid, gid_t gid)
50 {
51 int fd;
52 int ret = -1;
53
54 #ifdef IFF_TUN_EXCL
55 ifr->ifr_flags |= IFF_TUN_EXCL;
56 #endif
57
58 fd = open(TUNDEV, O_RDWR);
59 if (fd < 0) {
60 perror("open");
61 return -1;
62 }
63 if (ioctl(fd, TUNSETIFF, ifr)) {
64 perror("ioctl(TUNSETIFF)");
65 goto out;
66 }
67 if (uid != -1 && ioctl(fd, TUNSETOWNER, uid)) {
68 perror("ioctl(TUNSETOWNER)");
69 goto out;
70 }
71 if (gid != -1 && ioctl(fd, TUNSETGROUP, gid)) {
72 perror("ioctl(TUNSETGROUP)");
73 goto out;
74 }
75 if (ioctl(fd, TUNSETPERSIST, 1)) {
76 perror("ioctl(TUNSETPERSIST)");
77 goto out;
78 }
79 ret = 0;
80 out:
81 close(fd);
82 return ret;
83 }
84
tap_del_ioctl(struct ifreq * ifr)85 static int tap_del_ioctl(struct ifreq *ifr)
86 {
87 int fd = open(TUNDEV, O_RDWR);
88 int ret = -1;
89
90 if (fd < 0) {
91 perror("open");
92 return -1;
93 }
94 if (ioctl(fd, TUNSETIFF, ifr)) {
95 perror("ioctl(TUNSETIFF)");
96 goto out;
97 }
98 if (ioctl(fd, TUNSETPERSIST, 0)) {
99 perror("ioctl(TUNSETPERSIST)");
100 goto out;
101 }
102 ret = 0;
103 out:
104 close(fd);
105 return ret;
106
107 }
parse_args(int argc,char ** argv,struct ifreq * ifr,uid_t * uid,gid_t * gid)108 static int parse_args(int argc, char **argv,
109 struct ifreq *ifr, uid_t *uid, gid_t *gid)
110 {
111 int count = 0;
112
113 memset(ifr, 0, sizeof(*ifr));
114
115 ifr->ifr_flags |= IFF_NO_PI;
116
117 while (argc > 0) {
118 if (matches(*argv, "mode") == 0) {
119 NEXT_ARG();
120 if (matches(*argv, "tun") == 0) {
121 if (ifr->ifr_flags & IFF_TAP) {
122 fprintf(stderr, "You managed to ask for more than one tunnel mode.\n");
123 exit(-1);
124 }
125 ifr->ifr_flags |= IFF_TUN;
126 } else if (matches(*argv, "tap") == 0) {
127 if (ifr->ifr_flags & IFF_TUN) {
128 fprintf(stderr, "You managed to ask for more than one tunnel mode.\n");
129 exit(-1);
130 }
131 ifr->ifr_flags |= IFF_TAP;
132 } else {
133 fprintf(stderr, "Unknown tunnel mode \"%s\"\n", *argv);
134 exit(-1);
135 }
136 } else if (uid && matches(*argv, "user") == 0) {
137 char *end;
138 unsigned long user;
139
140 NEXT_ARG();
141 if (**argv && ((user = strtol(*argv, &end, 10)), !*end))
142 *uid = user;
143 else {
144 struct passwd *pw = getpwnam(*argv);
145
146 if (!pw) {
147 fprintf(stderr, "invalid user \"%s\"\n", *argv);
148 exit(-1);
149 }
150 *uid = pw->pw_uid;
151 }
152 } else if (gid && matches(*argv, "group") == 0) {
153 char *end;
154 unsigned long group;
155
156 NEXT_ARG();
157
158 if (**argv && ((group = strtol(*argv, &end, 10)), !*end))
159 *gid = group;
160 else {
161 struct group *gr = getgrnam(*argv);
162
163 if (!gr) {
164 fprintf(stderr, "invalid group \"%s\"\n", *argv);
165 exit(-1);
166 }
167 *gid = gr->gr_gid;
168 }
169 } else if (matches(*argv, "pi") == 0) {
170 ifr->ifr_flags &= ~IFF_NO_PI;
171 } else if (matches(*argv, "one_queue") == 0) {
172 ifr->ifr_flags |= IFF_ONE_QUEUE;
173 } else if (matches(*argv, "vnet_hdr") == 0) {
174 ifr->ifr_flags |= IFF_VNET_HDR;
175 } else if (matches(*argv, "multi_queue") == 0) {
176 ifr->ifr_flags |= IFF_MULTI_QUEUE;
177 } else if (matches(*argv, "dev") == 0) {
178 NEXT_ARG();
179 if (get_ifname(ifr->ifr_name, *argv))
180 invarg("\"dev\" not a valid ifname", *argv);
181 } else {
182 if (matches(*argv, "name") == 0) {
183 NEXT_ARG();
184 } else if (matches(*argv, "help") == 0)
185 usage();
186 if (ifr->ifr_name[0])
187 duparg2("name", *argv);
188 if (get_ifname(ifr->ifr_name, *argv))
189 invarg("\"name\" not a valid ifname", *argv);
190 }
191 count++;
192 argc--; argv++;
193 }
194
195 if (!(ifr->ifr_flags & TUN_TYPE_MASK)) {
196 fprintf(stderr, "You failed to specify a tunnel mode\n");
197 return -1;
198 }
199
200 return 0;
201 }
202
203
do_add(int argc,char ** argv)204 static int do_add(int argc, char **argv)
205 {
206 struct ifreq ifr;
207 uid_t uid = -1;
208 gid_t gid = -1;
209
210 if (parse_args(argc, argv, &ifr, &uid, &gid) < 0)
211 return -1;
212
213 return tap_add_ioctl(&ifr, uid, gid);
214 }
215
do_del(int argc,char ** argv)216 static int do_del(int argc, char **argv)
217 {
218 struct ifreq ifr;
219
220 if (parse_args(argc, argv, &ifr, NULL, NULL) < 0)
221 return -1;
222
223 return tap_del_ioctl(&ifr);
224 }
225
read_prop(char * dev,char * prop,long * value)226 static int read_prop(char *dev, char *prop, long *value)
227 {
228 char fname[IFNAMSIZ+25], buf[80], *endp;
229 ssize_t len;
230 int fd;
231 long result;
232
233 sprintf(fname, "/sys/class/net/%s/%s", dev, prop);
234 fd = open(fname, O_RDONLY);
235 if (fd < 0) {
236 if (strcmp(prop, "tun_flags"))
237 fprintf(stderr, "open %s: %s\n", fname,
238 strerror(errno));
239 return -1;
240 }
241 len = read(fd, buf, sizeof(buf)-1);
242 close(fd);
243 if (len < 0) {
244 fprintf(stderr, "read %s: %s", fname, strerror(errno));
245 return -1;
246 }
247
248 buf[len] = 0;
249 result = strtol(buf, &endp, 0);
250 if (*endp != '\n') {
251 fprintf(stderr, "Failed to parse %s\n", fname);
252 return -1;
253 }
254 *value = result;
255 return 0;
256 }
257
print_flags(long flags)258 static void print_flags(long flags)
259 {
260 if (flags & IFF_TUN)
261 printf(" tun");
262
263 if (flags & IFF_TAP)
264 printf(" tap");
265
266 if (!(flags & IFF_NO_PI))
267 printf(" pi");
268
269 if (flags & IFF_ONE_QUEUE)
270 printf(" one_queue");
271
272 if (flags & IFF_VNET_HDR)
273 printf(" vnet_hdr");
274
275 flags &= ~(IFF_TUN|IFF_TAP|IFF_NO_PI|IFF_ONE_QUEUE|IFF_VNET_HDR);
276 if (flags)
277 printf(" UNKNOWN_FLAGS:%lx", flags);
278 }
279
pid_name(pid_t pid)280 static char *pid_name(pid_t pid)
281 {
282 char *comm;
283 FILE *f;
284 int err;
285
286 err = asprintf(&comm, "/proc/%d/comm", pid);
287 if (err < 0)
288 return NULL;
289
290 f = fopen(comm, "r");
291 free(comm);
292 if (!f) {
293 perror("fopen");
294 return NULL;
295 }
296
297 if (fscanf(f, "%ms\n", &comm) != 1) {
298 perror("fscanf");
299 comm = NULL;
300 }
301
302
303 if (fclose(f))
304 perror("fclose");
305
306 return comm;
307 }
308
show_processes(const char * name)309 static void show_processes(const char *name)
310 {
311 glob_t globbuf = { };
312 char **fd_path;
313 int err;
314
315 err = glob("/proc/[0-9]*/fd/[0-9]*", GLOB_NOSORT,
316 NULL, &globbuf);
317 if (err)
318 return;
319
320 fd_path = globbuf.gl_pathv;
321 while (*fd_path) {
322 const char *dev_net_tun = "/dev/net/tun";
323 const size_t linkbuf_len = strlen(dev_net_tun) + 2;
324 char linkbuf[linkbuf_len], *fdinfo;
325 int pid, fd;
326 FILE *f;
327
328 if (sscanf(*fd_path, "/proc/%d/fd/%d", &pid, &fd) != 2)
329 goto next;
330
331 if (pid == getpid())
332 goto next;
333
334 err = readlink(*fd_path, linkbuf, linkbuf_len - 1);
335 if (err < 0) {
336 perror("readlink");
337 goto next;
338 }
339 linkbuf[err] = '\0';
340 if (strcmp(dev_net_tun, linkbuf))
341 goto next;
342
343 if (asprintf(&fdinfo, "/proc/%d/fdinfo/%d", pid, fd) < 0)
344 goto next;
345
346 f = fopen(fdinfo, "r");
347 free(fdinfo);
348 if (!f) {
349 perror("fopen");
350 goto next;
351 }
352
353 while (!feof(f)) {
354 char *key = NULL, *value = NULL;
355
356 err = fscanf(f, "%m[^:]: %ms\n", &key, &value);
357 if (err == EOF) {
358 if (ferror(f))
359 perror("fscanf");
360 break;
361 } else if (err == 2 &&
362 !strcmp("iff", key) &&
363 !strcmp(name, value)) {
364 char *pname = pid_name(pid);
365
366 printf(" %s(%d)", pname ? : "<NULL>", pid);
367 free(pname);
368 }
369
370 free(key);
371 free(value);
372 }
373 if (fclose(f))
374 perror("fclose");
375
376 next:
377 ++fd_path;
378 }
379
380 globfree(&globbuf);
381 }
382
383
do_show(int argc,char ** argv)384 static int do_show(int argc, char **argv)
385 {
386 DIR *dir;
387 struct dirent *d;
388 long flags, owner = -1, group = -1;
389
390 dir = opendir("/sys/class/net");
391 if (!dir) {
392 perror("opendir");
393 return -1;
394 }
395 while ((d = readdir(dir))) {
396 if (d->d_name[0] == '.' &&
397 (d->d_name[1] == 0 || d->d_name[1] == '.'))
398 continue;
399
400 if (read_prop(d->d_name, "tun_flags", &flags))
401 continue;
402
403 read_prop(d->d_name, "owner", &owner);
404 read_prop(d->d_name, "group", &group);
405
406 printf("%s:", d->d_name);
407 print_flags(flags);
408 if (owner != -1)
409 printf(" user %ld", owner);
410 if (group != -1)
411 printf(" group %ld", group);
412 printf("\n");
413 if (show_details) {
414 printf("\tAttached to processes:");
415 show_processes(d->d_name);
416 printf("\n");
417 }
418 }
419 closedir(dir);
420 return 0;
421 }
422
do_iptuntap(int argc,char ** argv)423 int do_iptuntap(int argc, char **argv)
424 {
425 if (argc > 0) {
426 if (matches(*argv, "add") == 0)
427 return do_add(argc-1, argv+1);
428 if (matches(*argv, "delete") == 0)
429 return do_del(argc-1, argv+1);
430 if (matches(*argv, "show") == 0 ||
431 matches(*argv, "lst") == 0 ||
432 matches(*argv, "list") == 0)
433 return do_show(argc-1, argv+1);
434 if (matches(*argv, "help") == 0)
435 usage();
436 } else
437 return do_show(0, NULL);
438
439 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tuntap help\".\n",
440 *argv);
441 exit(-1);
442 }
443