Hello, list!
As a result of my QEMU experience I added to BB a stripped down
version of tunctl -- a tool for creating/deleting tap* network
interfaces.
If you find it useful please consider applying. Feedback and critics
are welcome.
Best regards,
--
Vladimir
--- busybox.orig/include/applets.h Sat Dec 13 16:54:14 2008
+++ busybox/include/applets.h Thu Jan 15 19:03:41 2009
@@ -382,6 +382,7 @@
USE_TRUE(APPLET_NOFORK(true, true, _BB_DIR_BIN, _BB_SUID_NEVER, true))
USE_TTY(APPLET(tty, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
USE_TTYSIZE(APPLET(ttysize, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
+USE_TUNCTL(APPLET(tunctl, _BB_DIR_SBIN, _BB_SUID_NEVER))
//USE_TUNE2FS(APPLET(tune2fs, _BB_DIR_SBIN, _BB_SUID_NEVER))
USE_APP_UDHCPC(APPLET(udhcpc, _BB_DIR_SBIN, _BB_SUID_NEVER))
USE_APP_UDHCPD(APPLET(udhcpd, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
--- busybox.orig/include/usage.h Thu Dec 25 21:11:54 2008
+++ busybox/include/usage.h Sun Jan 18 11:46:38 2009
@@ -4457,6 +4457,23 @@
#define ttysize_full_usage "\n\n" \
"Print dimension(s) of standard input's terminal, on error return 80x25"
+#define tunctl_trivial_usage \
+ "[-f device] ([-t name] | -d name)" USE_FEATURE_TUNCTL_UG(" [-u owner] [-g group] [-b]")
+#define tunctl_full_usage "\n\n" \
+ "Create or delete tun interfaces" \
+ "\nOptions:" \
+ "\n -f name tun device (/dev/net/tun)" \
+ "\n -t name Create iface 'name'" \
+ "\n -d name Delete iface 'name'" \
+USE_FEATURE_TUNCTL_UG( \
+ "\n -u owner Set iface owner" \
+ "\n -g group Set iface group" \
+ "\n -b Brief output" \
+)
+#define tunctl_example_usage \
+ "# tunctl\n" \
+ "# tunctl -d tun0\n"
+
#define tune2fs_trivial_usage \
"[-c max-mounts-count] [-e errors-behavior] [-g group] " \
"[-i interval[d|m|w]] [-j] [-J journal-options] [-l] [-s sparse-flag] " \
--- busybox.orig/networking/Config.in Sun Nov 23 08:54:42 2008
+++ busybox/networking/Config.in Thu Jan 15 20:50:35 2009
@@ -920,6 +920,20 @@
tcpsvd listens on a TCP port and runs a program for each new
connection.
+config TUNCTL
+ bool "tunctl"
+ default n
+ help
+ tunctl creates or deletes tun devices.
+
+config FEATURE_TUNCTL_UG
+ bool "Support owner:group assignment"
+ default n
+ depends on TUNCTL
+ help
+ Allow to specify owner and group of newly created interface.
+ 340 bytes of pure bloat. Say no here.
+
config UDPSVD
bool "udpsvd"
default n
--- busybox.orig/networking/Kbuild Thu Nov 13 19:03:38 2008
+++ busybox/networking/Kbuild Thu Jan 15 19:12:58 2009
@@ -36,6 +36,7 @@
lib-$(CONFIG_TFTP) += tftp.o
lib-$(CONFIG_TFTPD) += tftp.o
lib-$(CONFIG_TRACEROUTE) += traceroute.o
+lib-$(CONFIG_TUNCTL) += tunctl.o
lib-$(CONFIG_VCONFIG) += vconfig.o
lib-$(CONFIG_WGET) += wget.o
lib-$(CONFIG_ZCIP) += zcip.o
--- /dev/null Thu Jan 1 03:00:00 1970
+++ busybox/networking/tunctl.c Sun Jan 18 11:45:48 2009
@@ -0,0 +1,138 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * tun devices controller
+ *
+ * Copyright (C) 2008 by Vladimir Dronnikov <[email protected]>
+ *
+ * Original code:
+ * Jeff Dike
+ *
+ * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ */
+#include <netinet/in.h>
+#include <net/if.h>
+#include <linux/if_tun.h>
+#include "libbb.h"
+
+/* TUNSETGROUP appeared in 2.6.23 */
+#ifndef TUNSETGROUP
+#define TUNSETGROUP _IOW('T', 206, int)
+#endif
+
+#define IOCTL(a,b,c) ioctl_or_perror_and_die(a,b,c,NULL)
+
+#if 0
+int tunctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int tunctl_main(int argc UNUSED_PARAM, char **argv)
+{
+ struct ifreq ifr;
+ int fd;
+ const char *opt_name = opt_name, *opt_device = "/dev/net/tun";
+#if ENABLE_FEATURE_TUNCTL_UG
+ const char *opt_user, *opt_group;
+ long user = -1, group = -1;
+#endif
+ unsigned opts;
+
+ enum {
+ OPT_f = 1 << 0, // control device name (/dev/net/tun)
+ OPT_t = 1 << 1, // create named interface
+ OPT_d = 1 << 2, // delete named interface
+#if ENABLE_FEATURE_TUNCTL_UG
+ OPT_u = 1 << 3, // set new interface owner
+ OPT_g = 1 << 4, // set new interface group
+ OPT_b = 1 << 5, // brief output
+#endif
+ };
+
+ opt_complementary = "=0:t--d:d--t"; // no arguments; t ^ d
+ opts = getopt32(argv, "f:t:d:" USE_FEATURE_TUNCTL_UG("u:g:b"),
+ &opt_device, &opt_name, &opt_name USE_FEATURE_TUNCTL_UG(, &opt_user, &opt_group));
+
+ // select device
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
+ strncpy_IFNAMSIZ(ifr.ifr_name, opt_name);
+
+ // open device
+ fd = xopen(opt_device, O_RDWR);
+ IOCTL(fd, TUNSETIFF, (void *)&ifr);
+
+ // delete?
+ if (opts & OPT_d) {
+ IOCTL(fd, TUNSETPERSIST, (void *)0);
+ bb_info_msg("Set '%s' nonpersistent", ifr.ifr_name);
+ return EXIT_SUCCESS;
+ }
+
+ // create
+#if ENABLE_FEATURE_TUNCTL_UG
+ if (opts & OPT_u)
+ user = xuname2uid(opt_user);
+ if (opts & OPT_g)
+ group = xgroup2gid(opt_group);
+ if (user == -1 && group == -1)
+ user = geteuid();
+
+ if (user != -1)
+ IOCTL(fd, TUNSETOWNER, (void *)user);
+ if (group != -1)
+ IOCTL(fd, TUNSETGROUP, (void *)group);
+#endif
+ IOCTL(fd, TUNSETPERSIST, (void *)1);
+
+ // show info
+#if ENABLE_FEATURE_TUNCTL_UG
+ if (opts & OPT_b) {
+ puts(ifr.ifr_name);
+ } else {
+ printf("Set '%s' persistent", ifr.ifr_name);
+ if (user != -1 || group != -1) {
+ printf(" and owned by");
+ if (user != -1)
+ printf(" uid %ld", user);
+ if (group != -1)
+ printf(" gid %ld", group);
+ }
+ puts("");
+ }
+#else
+ puts(ifr.ifr_name);
+#endif
+
+ return EXIT_SUCCESS;
+}
+#endif
+
+int tunctl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int tunctl_main(int argc UNUSED_PARAM, char **argv)
+{
+ struct ifreq ifr;
+ int fd;
+ const char *opt_name = "", *opt_device = "/dev/net/tun";
+ unsigned opts;
+
+ enum {
+ OPT_f = 1 << 0, // control device name (/dev/net/tun)
+ OPT_t = 1 << 1, // create named interface
+ OPT_d = 1 << 2, // delete named interface
+ };
+
+ opt_complementary = "=0:t--d:d--t"; // no arguments; t ^ d
+ opts = getopt32(argv, "f:t:d:u:g:b", // u, g, b accepted and ignored
+ &opt_device, &opt_name, &opt_name, NULL, NULL);
+
+ // set interface name
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
+ strncpy_IFNAMSIZ(ifr.ifr_name, opt_name);
+
+ // open device
+ fd = xopen(opt_device, O_RDWR);
+ IOCTL(fd, TUNSETIFF, (void *)&ifr);
+
+ // create or delete interface
+ IOCTL(fd, TUNSETPERSIST, (void *)(0 == (opts & OPT_d)));
+
+ return EXIT_SUCCESS;
+}
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox