David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 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> |
Hannes Frederic Sowa | 567e696 | 2016-08-24 20:08:25 +0200 | [diff] [blame] | 28 | #include <glob.h> |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 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 | |
| 38 | static void usage(void) |
| 39 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 40 | fprintf(stderr, "Usage: ip tuntap { add | del | show | list | lst | help } [ dev PHYS_DEV ]\n"); |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 41 | fprintf(stderr, " [ mode { tun | tap } ] [ user USER ] [ group GROUP ]\n"); |
Thomas Graf | 113fab7 | 2016-09-08 14:56:34 +0200 | [diff] [blame] | 42 | fprintf(stderr, " [ one_queue ] [ pi ] [ vnet_hdr ] [ multi_queue ] [ name NAME ]\n"); |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 43 | fprintf(stderr, "\n"); |
| 44 | fprintf(stderr, "Where: USER := { STRING | NUMBER }\n"); |
| 45 | fprintf(stderr, " GROUP := { STRING | NUMBER }\n"); |
| 46 | exit(-1); |
| 47 | } |
| 48 | |
| 49 | static int tap_add_ioctl(struct ifreq *ifr, uid_t uid, gid_t gid) |
| 50 | { |
Dan McGee | 1313ceb | 2011-08-31 12:14:51 -0700 | [diff] [blame] | 51 | int fd; |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 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 | |
| 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 | } |
Stephen Hemminger | cc28aad | 2016-09-01 09:03:40 -0700 | [diff] [blame] | 108 | static int parse_args(int argc, char **argv, |
| 109 | struct ifreq *ifr, uid_t *uid, gid_t *gid) |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 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) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 122 | fprintf(stderr, "You managed to ask for more than one tunnel mode.\n"); |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 123 | exit(-1); |
| 124 | } |
| 125 | ifr->ifr_flags |= IFF_TUN; |
| 126 | } else if (matches(*argv, "tap") == 0) { |
| 127 | if (ifr->ifr_flags & IFF_TUN) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 128 | fprintf(stderr, "You managed to ask for more than one tunnel mode.\n"); |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 129 | exit(-1); |
| 130 | } |
| 131 | ifr->ifr_flags |= IFF_TAP; |
| 132 | } else { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 133 | fprintf(stderr, "Unknown tunnel mode \"%s\"\n", *argv); |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 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); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 145 | |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 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); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 162 | |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 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; |
Sriram Narasimhan | c41e038 | 2013-05-23 12:36:29 +0000 | [diff] [blame] | 175 | } else if (matches(*argv, "multi_queue") == 0) { |
| 176 | ifr->ifr_flags |= IFF_MULTI_QUEUE; |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 177 | } else if (matches(*argv, "dev") == 0) { |
| 178 | NEXT_ARG(); |
| 179 | strncpy(ifr->ifr_name, *argv, IFNAMSIZ-1); |
| 180 | } else { |
| 181 | if (matches(*argv, "name") == 0) { |
| 182 | NEXT_ARG(); |
| 183 | } else if (matches(*argv, "help") == 0) |
| 184 | usage(); |
| 185 | if (ifr->ifr_name[0]) |
| 186 | duparg2("name", *argv); |
| 187 | strncpy(ifr->ifr_name, *argv, IFNAMSIZ); |
| 188 | } |
| 189 | count++; |
| 190 | argc--; argv++; |
| 191 | } |
| 192 | |
vadimk | f1a505a | 2014-09-16 23:54:34 +0300 | [diff] [blame] | 193 | if (!(ifr->ifr_flags & TUN_TYPE_MASK)) { |
| 194 | fprintf(stderr, "You failed to specify a tunnel mode\n"); |
| 195 | return -1; |
| 196 | } |
| 197 | |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | static int do_add(int argc, char **argv) |
| 203 | { |
| 204 | struct ifreq ifr; |
| 205 | uid_t uid = -1; |
| 206 | gid_t gid = -1; |
| 207 | |
| 208 | if (parse_args(argc, argv, &ifr, &uid, &gid) < 0) |
| 209 | return -1; |
| 210 | |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 211 | return tap_add_ioctl(&ifr, uid, gid); |
| 212 | } |
| 213 | |
| 214 | static int do_del(int argc, char **argv) |
| 215 | { |
| 216 | struct ifreq ifr; |
| 217 | |
| 218 | if (parse_args(argc, argv, &ifr, NULL, NULL) < 0) |
| 219 | return -1; |
| 220 | |
| 221 | return tap_del_ioctl(&ifr); |
| 222 | } |
| 223 | |
| 224 | static int read_prop(char *dev, char *prop, long *value) |
| 225 | { |
| 226 | char fname[IFNAMSIZ+25], buf[80], *endp; |
| 227 | ssize_t len; |
| 228 | int fd; |
| 229 | long result; |
| 230 | |
| 231 | sprintf(fname, "/sys/class/net/%s/%s", dev, prop); |
| 232 | fd = open(fname, O_RDONLY); |
| 233 | if (fd < 0) { |
| 234 | if (strcmp(prop, "tun_flags")) |
| 235 | fprintf(stderr, "open %s: %s\n", fname, |
| 236 | strerror(errno)); |
| 237 | return -1; |
| 238 | } |
| 239 | len = read(fd, buf, sizeof(buf)-1); |
| 240 | close(fd); |
| 241 | if (len < 0) { |
| 242 | fprintf(stderr, "read %s: %s", fname, strerror(errno)); |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | buf[len] = 0; |
| 247 | result = strtol(buf, &endp, 0); |
| 248 | if (*endp != '\n') { |
| 249 | fprintf(stderr, "Failed to parse %s\n", fname); |
| 250 | return -1; |
| 251 | } |
| 252 | *value = result; |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static void print_flags(long flags) |
| 257 | { |
| 258 | if (flags & IFF_TUN) |
| 259 | printf(" tun"); |
| 260 | |
| 261 | if (flags & IFF_TAP) |
| 262 | printf(" tap"); |
| 263 | |
| 264 | if (!(flags & IFF_NO_PI)) |
| 265 | printf(" pi"); |
| 266 | |
| 267 | if (flags & IFF_ONE_QUEUE) |
| 268 | printf(" one_queue"); |
| 269 | |
| 270 | if (flags & IFF_VNET_HDR) |
| 271 | printf(" vnet_hdr"); |
| 272 | |
| 273 | flags &= ~(IFF_TUN|IFF_TAP|IFF_NO_PI|IFF_ONE_QUEUE|IFF_VNET_HDR); |
| 274 | if (flags) |
| 275 | printf(" UNKNOWN_FLAGS:%lx", flags); |
| 276 | } |
| 277 | |
Hannes Frederic Sowa | 567e696 | 2016-08-24 20:08:25 +0200 | [diff] [blame] | 278 | static char *pid_name(pid_t pid) |
| 279 | { |
| 280 | char *comm; |
| 281 | FILE *f; |
| 282 | int err; |
| 283 | |
| 284 | err = asprintf(&comm, "/proc/%d/comm", pid); |
| 285 | if (err < 0) |
| 286 | return NULL; |
| 287 | |
| 288 | f = fopen(comm, "r"); |
| 289 | free(comm); |
| 290 | if (!f) { |
| 291 | perror("fopen"); |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | if (fscanf(f, "%ms\n", &comm) != 1) { |
| 296 | perror("fscanf"); |
| 297 | comm = NULL; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | if (fclose(f)) |
| 302 | perror("fclose"); |
| 303 | |
| 304 | return comm; |
| 305 | } |
| 306 | |
| 307 | static void show_processes(const char *name) |
| 308 | { |
| 309 | glob_t globbuf = { }; |
| 310 | char **fd_path; |
| 311 | int err; |
| 312 | |
| 313 | err = glob("/proc/[0-9]*/fd/[0-9]*", GLOB_NOSORT, |
| 314 | NULL, &globbuf); |
| 315 | if (err) |
| 316 | return; |
| 317 | |
| 318 | fd_path = globbuf.gl_pathv; |
| 319 | while (*fd_path) { |
| 320 | const char *dev_net_tun = "/dev/net/tun"; |
| 321 | const size_t linkbuf_len = strlen(dev_net_tun) + 2; |
| 322 | char linkbuf[linkbuf_len], *fdinfo; |
| 323 | int pid, fd; |
| 324 | FILE *f; |
| 325 | |
| 326 | if (sscanf(*fd_path, "/proc/%d/fd/%d", &pid, &fd) != 2) |
| 327 | goto next; |
| 328 | |
| 329 | if (pid == getpid()) |
| 330 | goto next; |
| 331 | |
| 332 | err = readlink(*fd_path, linkbuf, linkbuf_len - 1); |
| 333 | if (err < 0) { |
| 334 | perror("readlink"); |
| 335 | goto next; |
| 336 | } |
| 337 | linkbuf[err] = '\0'; |
| 338 | if (strcmp(dev_net_tun, linkbuf)) |
| 339 | goto next; |
| 340 | |
| 341 | if (asprintf(&fdinfo, "/proc/%d/fdinfo/%d", pid, fd) < 0) |
| 342 | goto next; |
| 343 | |
| 344 | f = fopen(fdinfo, "r"); |
| 345 | free(fdinfo); |
| 346 | if (!f) { |
| 347 | perror("fopen"); |
| 348 | goto next; |
| 349 | } |
| 350 | |
| 351 | while (!feof(f)) { |
| 352 | char *key = NULL, *value = NULL; |
| 353 | |
| 354 | err = fscanf(f, "%m[^:]: %ms\n", &key, &value); |
| 355 | if (err == EOF) { |
| 356 | if (ferror(f)) |
| 357 | perror("fscanf"); |
| 358 | break; |
| 359 | } else if (err == 2 && |
Stephen Hemminger | cc28aad | 2016-09-01 09:03:40 -0700 | [diff] [blame] | 360 | !strcmp("iff", key) && |
| 361 | !strcmp(name, value)) { |
Hannes Frederic Sowa | 567e696 | 2016-08-24 20:08:25 +0200 | [diff] [blame] | 362 | char *pname = pid_name(pid); |
Stephen Hemminger | cc28aad | 2016-09-01 09:03:40 -0700 | [diff] [blame] | 363 | |
| 364 | printf(" %s(%d)", pname ? : "<NULL>", pid); |
Hannes Frederic Sowa | 567e696 | 2016-08-24 20:08:25 +0200 | [diff] [blame] | 365 | free(pname); |
| 366 | } |
| 367 | |
| 368 | free(key); |
| 369 | free(value); |
| 370 | } |
| 371 | if (fclose(f)) |
| 372 | perror("fclose"); |
| 373 | |
| 374 | next: |
| 375 | ++fd_path; |
| 376 | } |
| 377 | |
| 378 | globfree(&globbuf); |
Hannes Frederic Sowa | 567e696 | 2016-08-24 20:08:25 +0200 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 382 | static int do_show(int argc, char **argv) |
| 383 | { |
| 384 | DIR *dir; |
| 385 | struct dirent *d; |
| 386 | long flags, owner = -1, group = -1; |
| 387 | |
| 388 | dir = opendir("/sys/class/net"); |
| 389 | if (!dir) { |
| 390 | perror("opendir"); |
| 391 | return -1; |
| 392 | } |
| 393 | while ((d = readdir(dir))) { |
| 394 | if (d->d_name[0] == '.' && |
| 395 | (d->d_name[1] == 0 || d->d_name[1] == '.')) |
| 396 | continue; |
| 397 | |
| 398 | if (read_prop(d->d_name, "tun_flags", &flags)) |
| 399 | continue; |
| 400 | |
| 401 | read_prop(d->d_name, "owner", &owner); |
| 402 | read_prop(d->d_name, "group", &group); |
| 403 | |
| 404 | printf("%s:", d->d_name); |
| 405 | print_flags(flags); |
| 406 | if (owner != -1) |
| 407 | printf(" user %ld", owner); |
| 408 | if (group != -1) |
| 409 | printf(" group %ld", group); |
| 410 | printf("\n"); |
Hannes Frederic Sowa | 567e696 | 2016-08-24 20:08:25 +0200 | [diff] [blame] | 411 | if (show_details) { |
| 412 | printf("\tAttached to processes:"); |
| 413 | show_processes(d->d_name); |
| 414 | printf("\n"); |
| 415 | } |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 416 | } |
Thomas Jarosch | e9a927d | 2011-10-03 05:23:42 +0000 | [diff] [blame] | 417 | closedir(dir); |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | int do_iptuntap(int argc, char **argv) |
| 422 | { |
| 423 | if (argc > 0) { |
| 424 | if (matches(*argv, "add") == 0) |
| 425 | return do_add(argc-1, argv+1); |
Andreas Henriksson | 6e30461 | 2012-05-19 16:08:21 +0200 | [diff] [blame] | 426 | if (matches(*argv, "delete") == 0) |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 427 | return do_del(argc-1, argv+1); |
| 428 | if (matches(*argv, "show") == 0 || |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 429 | matches(*argv, "lst") == 0 || |
| 430 | matches(*argv, "list") == 0) |
| 431 | return do_show(argc-1, argv+1); |
David Woodhouse | 580fbd8 | 2009-09-19 09:48:43 -0700 | [diff] [blame] | 432 | if (matches(*argv, "help") == 0) |
| 433 | usage(); |
| 434 | } else |
| 435 | return do_show(0, NULL); |
| 436 | |
| 437 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip tuntap help\".\n", |
| 438 | *argv); |
| 439 | exit(-1); |
| 440 | } |