Patchwork [3/4] use netlink to remove vlan interface

login
register
mail settings
Submitter michael-dev@fami-braun.de
Date July 7, 2012, 8:52 a.m.
Message ID <20120707085217.24229.76730.stgit@localhost6.localdomain6>
Download mbox | patch
Permalink /patch/169567/
State Superseded
Headers show

Comments

michael-dev@fami-braun.de - July 7, 2012, 8:52 a.m.
---
 src/ap/vlan_init.c |   35 -----------------------------------
 src/ap/vlan_util.c |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/ap/vlan_util.h |    1 +
 3 files changed, 53 insertions(+), 35 deletions(-)

Patch

diff --git a/src/ap/vlan_init.c b/src/ap/vlan_init.c
index b1e8f42..8f6be69 100644
--- a/src/ap/vlan_init.c
+++ b/src/ap/vlan_init.c
@@ -336,41 +336,6 @@  static int br_getnumports(const char *br_name)
 }
 
 
-static int vlan_rem(const char *if_name)
-{
-	int fd;
-	struct vlan_ioctl_args if_request;
-
-	wpa_printf(MSG_DEBUG, "VLAN: vlan_rem(%s)", if_name);
-	if ((os_strlen(if_name) + 1) > sizeof(if_request.device1)) {
-		wpa_printf(MSG_ERROR, "VLAN: Interface name too long: '%s'",
-			   if_name);
-		return -1;
-	}
-
-	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
-		wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
-			   "failed: %s", __func__, strerror(errno));
-		return -1;
-	}
-
-	os_memset(&if_request, 0, sizeof(if_request));
-
-	os_strlcpy(if_request.device1, if_name, sizeof(if_request.device1));
-	if_request.cmd = DEL_VLAN_CMD;
-
-	if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
-		wpa_printf(MSG_ERROR, "VLAN: %s: DEL_VLAN_CMD failed for %s: "
-			   "%s", __func__, if_name, strerror(errno));
-		close(fd);
-		return -1;
-	}
-
-	close(fd);
-	return 0;
-}
-
-
 static void vlan_newlink(char *ifname, struct hostapd_data *hapd)
 {
 	char vlan_ifname[IFNAMSIZ];
diff --git a/src/ap/vlan_util.c b/src/ap/vlan_util.c
index ca18963..8d97479 100644
--- a/src/ap/vlan_util.c
+++ b/src/ap/vlan_util.c
@@ -134,4 +134,56 @@  vlan_add_error:
 	return ret;
 }
 
+
+int vlan_rem(const char *if_name)
+{
+	int ret = -1;
+	struct nl_sock *handle = 0;
+	struct nl_cache *cache = 0;
+	struct rtnl_link *link = 0;
+
+	wpa_printf(MSG_DEBUG, "VLAN: vlan_rem(if_name=%s)",
+		   if_name);
+
+	handle = nl_socket_alloc();
+	if (!handle) { 
+		wpa_printf(MSG_ERROR, "VLAN: failed to open netlink socket");
+		goto vlan_rem_error;
+	}
+
+	if (nl_connect(handle, NETLINK_ROUTE) < 0) {
+		wpa_printf(MSG_ERROR, "VLAN: failed to connect to netlink");
+		goto vlan_rem_error;
+	}
+
+	if (rtnl_link_alloc_cache(handle, AF_UNSPEC, &cache) < 0) {
+		cache = NULL;
+		wpa_printf(MSG_ERROR, "VLAN: failed to alloc cache");
+		goto vlan_rem_error;
+	}
+
+	if (!(link = rtnl_link_get_by_name(cache, if_name))) {
+		/* link does not exist */
+		wpa_printf(MSG_ERROR, "VLAN: interface %s does not exists", if_name);
+		goto vlan_rem_error;
+	}
+
+	if (rtnl_link_delete(handle, link) < 0) {
+		wpa_printf(MSG_ERROR, "VLAN: failed to remove link %s", if_name);
+		goto vlan_rem_error;
+	}
+
+	ret = 0;
+
+vlan_rem_error:
+	if (link)
+		rtnl_link_put(link);
+	if (cache)
+		nl_cache_free(cache);
+	if (handle)
+		nl_socket_free(handle);
+	return ret;
+}
+
+
 #endif
diff --git a/src/ap/vlan_util.h b/src/ap/vlan_util.h
index 1fd146a..c73c2fe 100644
--- a/src/ap/vlan_util.h
+++ b/src/ap/vlan_util.h
@@ -17,5 +17,6 @@ 
 #define VLAN_UTIL_H
 
 int vlan_add(const char *if_name, int vid, const char* vlan_if_name);
+int vlan_rem(const char *if_name);
 
 #endif