tunctl.c 4.5 KB

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