tunctl.c 4.4 KB

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