summaryrefslogtreecommitdiff
path: root/tizen/src/util
diff options
context:
space:
mode:
authorSeokYeon Hwang <syeon.hwang@samsung.com>2016-01-04 19:38:25 +0900
committerJinhyung Choi <jinh0.choi@samsung.com>2016-01-05 21:13:28 +0900
commit67a8399cae344e2261475fb054914385624f19a5 (patch)
tree6412b6258b139aa3b3d6edd9b6a404f4580bb592 /tizen/src/util
parent06cdac5f8bd6f40cb1e1210732d37ef290012bb3 (diff)
downloadqemu-67a8399cae344e2261475fb054914385624f19a5.tar.gz
qemu-67a8399cae344e2261475fb054914385624f19a5.tar.bz2
qemu-67a8399cae344e2261475fb054914385624f19a5.zip
net: clean-up tap handling logic
Tizen emulator specific logic is extracted to independent source file. Tap interface prefix is changed to "tap-tizen". Change-Id: I38e218936003e8bc8de51a4d0b3413872defe4c3 Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com> Signed-off-by: Munkyu Im <munkyu.im@samsung.com>
Diffstat (limited to 'tizen/src/util')
-rw-r--r--tizen/src/util/Makefile.objs3
-rw-r--r--tizen/src/util/tap_helper.c153
-rw-r--r--tizen/src/util/tap_helper.h34
3 files changed, 190 insertions, 0 deletions
diff --git a/tizen/src/util/Makefile.objs b/tizen/src/util/Makefile.objs
index b860b561fb..6c2564f2f1 100644
--- a/tizen/src/util/Makefile.objs
+++ b/tizen/src/util/Makefile.objs
@@ -16,6 +16,9 @@ obj-$(CONFIG_LINUX) += osutil-linux.o
obj-$(CONFIG_WIN32) += osutil-win32.o
obj-$(CONFIG_DARWIN) += osutil-darwin.o
+# tap helper
+obj-$(CONFIG_LINUX) += tap_helper.o
+
#sdb
obj-y += sdb.o
diff --git a/tizen/src/util/tap_helper.c b/tizen/src/util/tap_helper.c
new file mode 100644
index 0000000000..6c142f6154
--- /dev/null
+++ b/tizen/src/util/tap_helper.c
@@ -0,0 +1,153 @@
+/*
+ * TAP helper
+ *
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact:
+ * MunKyu Im <munkyu.im@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <linux/if_tun.h>
+
+#include "qemu-common.h"
+#include "qemu/error-report.h"
+
+#include "tap_helper.h"
+
+#define PATH_NET_TUN "/dev/net/tun"
+
+#include "util/new_debug_ch.h"
+DECLARE_DEBUG_CHANNEL(tap_helper);
+
+#define START_BASE_PORT 26100
+#define END_BASE_PORT 26200
+
+static bool launch_openvpn(bool ismake, const char *ifname)
+{
+ int pid, status;
+ const char* args[] = {
+ "/usr/bin/sudo",
+ "/usr/sbin/openvpn",
+ "",
+ "--dev",
+ ifname,
+ NULL
+ };
+ if (ismake) {
+ LOG_INFO("launch_openvpn make tap: %s\n", ifname);
+ args[2] = "--mktun";
+ } else {
+ LOG_INFO("launch_openvpn remove tap: %s\n", ifname);
+ args[2] = "--rmtun";
+ }
+ /* try to launch network script */
+ pid = fork();
+ if (pid == 0) {
+ execv(args[0], (char**)args);
+ _exit(1);
+ } else if (pid > 0) {
+ while (waitpid(pid, &status, 0) != pid) {
+ /* loop */
+ }
+
+ if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
+ return true;
+ }
+ }
+
+ LOG_WARNING("Could not launch openvpn\n");
+ return false;
+}
+
+void tap_create(char *ifname)
+{
+ int fd, ret, i;
+ struct ifreq ifr;
+ char dname[128];
+ int cause;
+
+ ifname[0] = '\0';
+
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
+
+ // clean-up legacy ifnames
+ for (i = START_BASE_PORT ; i < END_BASE_PORT; i += 10) {
+ g_snprintf(dname, sizeof(dname), "tap%d", i);
+ g_strlcpy(ifr.ifr_name, dname, IFNAMSIZ);
+
+ TFR(fd = open(PATH_NET_TUN, O_RDWR));
+ if (fd < 0) {
+ error_report("could not open %s: %m", PATH_NET_TUN);
+ return;
+ }
+
+ ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
+
+ close(fd);
+
+ if (ret == 0) {
+ if (!launch_openvpn(false, dname)) {
+ LOG_INFO("Failed to remove tap interface [%s]."
+ "The other emulator may be using this interface.\n", dname);
+ }
+ }
+ }
+
+ // clean-up and find first unused ifname
+ for (i = 0; i < 10; i++) {
+ g_snprintf(dname, sizeof(dname), "tap_tizen%d", i);
+ g_strlcpy(ifr.ifr_name, dname, IFNAMSIZ);
+
+ TFR(fd = open(PATH_NET_TUN, O_RDWR));
+ if (fd < 0) {
+ error_report("could not open %s: %m", PATH_NET_TUN);
+ return;
+ }
+
+ ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
+ cause = errno;
+ close(fd);
+
+ // ret 0: exist but not used
+ // ret -1 and cause EPERM: not exist
+ if (ret == 0 || (ret == -1 && cause == EPERM)) {
+ if (ret == 0 && !launch_openvpn(false, dname)) {
+ LOG_INFO("Failed to remove tap interface [%s]."
+ "The other emulator may be using this interface.\n", dname);
+ continue;
+ }
+ if (ifname[0] == '\0') {
+ g_strlcpy(ifname, dname, 128);
+ }
+ }
+ }
+
+ // create tap
+ if (ifname == '\0' || !launch_openvpn(true, ifname)) {
+ LOG_INFO("Tap interface creattion failed.\n");
+ }
+
+ return;
+}
diff --git a/tizen/src/util/tap_helper.h b/tizen/src/util/tap_helper.h
new file mode 100644
index 0000000000..a14169ef2b
--- /dev/null
+++ b/tizen/src/util/tap_helper.h
@@ -0,0 +1,34 @@
+/*
+ * TAP helper
+ *
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact:
+ * MunKyu Im <munkyu.im@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#ifndef __TAP_HELPER_H__
+#define __TAP_HELPER_H__
+
+void tap_create(char *);
+
+#endif //__TAP_HELPER_H__