tunctl.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. //usage:#define tunctl_trivial_usage
  13. //usage: "[-f device] ([-t name] | -d name)" IF_FEATURE_TUNCTL_UG(" [-u owner] [-g group] [-b]")
  14. //usage:#define tunctl_full_usage "\n\n"
  15. //usage: "Create or delete tun interfaces\n"
  16. //usage: "\n -f name tun device (/dev/net/tun)"
  17. //usage: "\n -t name Create iface 'name'"
  18. //usage: "\n -d name Delete iface 'name'"
  19. //usage: IF_FEATURE_TUNCTL_UG(
  20. //usage: "\n -u owner Set iface owner"
  21. //usage: "\n -g group Set iface group"
  22. //usage: "\n -b Brief output"
  23. //usage: )
  24. //usage:
  25. //usage:#define tunctl_example_usage
  26. //usage: "# tunctl\n"
  27. //usage: "# tunctl -d tun0\n"
  28. #include <netinet/in.h>
  29. #include <net/if.h>
  30. #include <linux/if_tun.h>
  31. #include "libbb.h"
  32. /* TUNSETGROUP appeared in 2.6.23 */
  33. #ifndef TUNSETGROUP
  34. #define TUNSETGROUP _IOW('T', 206, int)
  35. #endif
  36. #define IOCTL(a, b, c) ioctl_or_perror_and_die(a, b, c, NULL)
  37. #if 1
  38. int tunctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  39. int tunctl_main(int argc UNUSED_PARAM, char **argv)
  40. {
  41. struct ifreq ifr;
  42. int fd;
  43. const char *opt_name = "tap%d";
  44. const char *opt_device = "/dev/net/tun";
  45. #if ENABLE_FEATURE_TUNCTL_UG
  46. const char *opt_user, *opt_group;
  47. long user = -1, group = -1;
  48. #endif
  49. unsigned opts;
  50. enum {
  51. OPT_f = 1 << 0, // control device name (/dev/net/tun)
  52. OPT_t = 1 << 1, // create named interface
  53. OPT_d = 1 << 2, // delete named interface
  54. #if ENABLE_FEATURE_TUNCTL_UG
  55. OPT_u = 1 << 3, // set new interface owner
  56. OPT_g = 1 << 4, // set new interface group
  57. OPT_b = 1 << 5, // brief output
  58. #endif
  59. };
  60. opt_complementary = "=0:t--d:d--t"; // no arguments; t ^ d
  61. opts = getopt32(argv, "f:t:d:" IF_FEATURE_TUNCTL_UG("u:g:b"),
  62. &opt_device, &opt_name, &opt_name
  63. IF_FEATURE_TUNCTL_UG(, &opt_user, &opt_group));
  64. // select device
  65. memset(&ifr, 0, sizeof(ifr));
  66. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  67. strncpy_IFNAMSIZ(ifr.ifr_name, opt_name);
  68. // open device
  69. fd = xopen(opt_device, O_RDWR);
  70. IOCTL(fd, TUNSETIFF, (void *)&ifr);
  71. // delete?
  72. if (opts & OPT_d) {
  73. IOCTL(fd, TUNSETPERSIST, (void *)(uintptr_t)0);
  74. bb_info_msg("Set '%s' %spersistent", ifr.ifr_name, "non");
  75. return EXIT_SUCCESS;
  76. }
  77. // create
  78. #if ENABLE_FEATURE_TUNCTL_UG
  79. if (opts & OPT_g) {
  80. group = xgroup2gid(opt_group);
  81. IOCTL(fd, TUNSETGROUP, (void *)(uintptr_t)group);
  82. } else
  83. user = geteuid();
  84. if (opts & OPT_u)
  85. user = xuname2uid(opt_user);
  86. IOCTL(fd, TUNSETOWNER, (void *)(uintptr_t)user);
  87. #endif
  88. IOCTL(fd, TUNSETPERSIST, (void *)(uintptr_t)1);
  89. // show info
  90. #if ENABLE_FEATURE_TUNCTL_UG
  91. if (opts & OPT_b) {
  92. puts(ifr.ifr_name);
  93. } else {
  94. printf("Set '%s' %spersistent", ifr.ifr_name, "");
  95. printf(" and owned by uid %ld", user);
  96. if (group != -1)
  97. printf(" gid %ld", group);
  98. bb_putchar('\n');
  99. }
  100. #else
  101. puts(ifr.ifr_name);
  102. #endif
  103. return EXIT_SUCCESS;
  104. }
  105. #else
  106. /* -210 bytes: */
  107. int tunctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  108. int tunctl_main(int argc UNUSED_PARAM, char **argv)
  109. {
  110. struct ifreq ifr;
  111. int fd;
  112. const char *opt_name = "tap%d";
  113. const char *opt_device = "/dev/net/tun";
  114. unsigned opts;
  115. enum {
  116. OPT_f = 1 << 0, // control device name (/dev/net/tun)
  117. OPT_t = 1 << 1, // create named interface
  118. OPT_d = 1 << 2, // delete named interface
  119. };
  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);
  123. // set interface name
  124. memset(&ifr, 0, sizeof(ifr));
  125. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  126. strncpy_IFNAMSIZ(ifr.ifr_name, opt_name);
  127. // open device
  128. fd = xopen(opt_device, O_RDWR);
  129. IOCTL(fd, TUNSETIFF, (void *)&ifr);
  130. // create or delete interface
  131. IOCTL(fd, TUNSETPERSIST, (void *)(uintptr_t)(0 == (opts & OPT_d)));
  132. return EXIT_SUCCESS;
  133. }
  134. #endif