3
0

tunctl.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.2 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' %spersistent", ifr.ifr_name, "");
  113. printf(" and owned by uid %ld", user);
  114. if (group != -1)
  115. printf(" gid %ld", group);
  116. bb_putchar('\n');
  117. }
  118. #else
  119. puts(ifr.ifr_name);
  120. #endif
  121. return EXIT_SUCCESS;
  122. }
  123. #else
  124. /* -210 bytes: */
  125. int tunctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  126. int tunctl_main(int argc UNUSED_PARAM, char **argv)
  127. {
  128. struct ifreq ifr;
  129. int fd;
  130. const char *opt_name = "tap%d";
  131. const char *opt_device = "/dev/net/tun";
  132. unsigned opts;
  133. enum {
  134. OPT_f = 1 << 0, // control device name (/dev/net/tun)
  135. OPT_t = 1 << 1, // create named interface
  136. OPT_d = 1 << 2, // delete named interface
  137. };
  138. opts = getopt32(argv, "^"
  139. "f:t:d:u:g:b" // u, g, b accepted and ignored
  140. "\0"
  141. "=0:t--d:d--t", // no arguments; t ^ d
  142. &opt_device, &opt_name, &opt_name, NULL, NULL
  143. );
  144. // set interface name
  145. memset(&ifr, 0, sizeof(ifr));
  146. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  147. strncpy_IFNAMSIZ(ifr.ifr_name, opt_name);
  148. // open device
  149. fd = xopen(opt_device, O_RDWR);
  150. IOCTL(fd, TUNSETIFF, (void *)&ifr);
  151. // create or delete interface
  152. IOCTL(fd, TUNSETPERSIST, (void *)(uintptr_t)(0 == (opts & OPT_d)));
  153. return EXIT_SUCCESS;
  154. }
  155. #endif