[PATCHv2 03/12] vlan: factor out per-vid code in newlink/dellink

Michael Braun michael-dev at fami-braun.de
Sun Dec 6 12:47:34 PST 2015


In order to prepare for adding tagged vlan support in vlan_init.c, vlan_newlink
and vlan_dellink are split into multiple functions. This reduces indention and
eases adding tagged vlans as well.

Signed-off-by: Michael Braun <michael-dev at fami-braun.de>
---
 src/ap/vlan_init.c | 248 +++++++++++++++++++++++++++++------------------------
 1 file changed, 135 insertions(+), 113 deletions(-)

diff --git a/src/ap/vlan_init.c b/src/ap/vlan_init.c
index 7beca3f..73de433 100644
--- a/src/ap/vlan_init.c
+++ b/src/ap/vlan_init.c
@@ -622,155 +622,179 @@ static int vlan_set_name_type(unsigned int name_type)
 
 #endif /* CONFIG_VLAN_NETLINK */
 
-
-static void vlan_newlink(char *ifname, struct hostapd_data *hapd)
+static void vlan_newlink_tagged(int vlan_naming, char *tagged_interface,
+				char *br_name, int vid,
+				struct hostapd_data *hapd)
 {
 	char vlan_ifname[IFNAMSIZ];
-	char br_name[IFNAMSIZ];
-	struct hostapd_vlan *vlan = hapd->conf->vlan;
+	int clean;
+
+	if (vlan_naming ==  DYNAMIC_VLAN_NAMING_WITH_DEVICE)
+		os_snprintf(vlan_ifname, sizeof(vlan_ifname), "%s.%d",
+			     tagged_interface,  vid);
+	else
+		os_snprintf(vlan_ifname, sizeof(vlan_ifname), "vlan%d",
+			     vid);
+
+	clean = 0;
+	ifconfig_up(tagged_interface);
+	if (!vlan_add(tagged_interface, vid, vlan_ifname))
+		clean |= DVLAN_CLEAN_VLAN;
+
+	if (!br_addif(br_name, vlan_ifname))
+		clean |= DVLAN_CLEAN_VLAN_PORT;
+
+	dyn_iface_get(hapd, vlan_ifname, clean);
+
+	ifconfig_up(vlan_ifname);
+}
+
+static void vlan_bridge_name(char *br_name, struct hostapd_data *hapd, int vid)
+{
+	char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
+
+	if (hapd->conf->vlan_bridge[0]) {
+		os_snprintf(br_name, IFNAMSIZ, "%s%d",
+			    hapd->conf->vlan_bridge,  vid);
+	} else if (tagged_interface) {
+		os_snprintf(br_name, IFNAMSIZ, "br%s.%d",
+			    tagged_interface, vid);
+	} else {
+		os_snprintf(br_name, IFNAMSIZ,
+			    "brvlan%d", vid);
+	}
+}
+
+static void vlan_get_bridge(char *br_name, struct hostapd_data *hapd, int vid)
+{
 	char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
 	int vlan_naming = hapd->conf->ssid.vlan_naming;
-	int clean, untagged;
 
-	wpa_printf(MSG_DEBUG, "VLAN: vlan_newlink(%s)", ifname);
+	dyn_iface_get(hapd, br_name, br_addbr(br_name) ? 0 : DVLAN_CLEAN_BR);
 
-	while (vlan) {
-		untagged = vlan->vlan_desc.untagged;
-		if (os_strcmp(ifname, vlan->ifname) == 0 && !vlan->configured) {
-			vlan->configured = 1;
-
-			if (hapd->conf->vlan_bridge[0]) {
-				os_snprintf(br_name, sizeof(br_name), "%s%d",
-					    hapd->conf->vlan_bridge,
-					    untagged);
-			} else if (tagged_interface) {
-				os_snprintf(br_name, sizeof(br_name),
-				            "br%s.%d", tagged_interface,
-					    untagged);
-			} else {
-				os_snprintf(br_name, sizeof(br_name),
-					    "brvlan%d", untagged);
-			}
+	ifconfig_up(br_name);
 
-			dyn_iface_get(hapd, br_name,
-				      br_addbr(br_name) ? 0 : DVLAN_CLEAN_BR);
+	if (tagged_interface)
+		vlan_newlink_tagged(vlan_naming, tagged_interface, br_name,
+				    vid, hapd);
+}
 
-			ifconfig_up(br_name);
+static void vlan_newlink(char *ifname, struct hostapd_data *hapd)
+{
+	char br_name[IFNAMSIZ];
+	struct hostapd_vlan *vlan;
+	int untagged;
 
-			if (tagged_interface) {
-				if (vlan_naming ==
-				    DYNAMIC_VLAN_NAMING_WITH_DEVICE)
-					os_snprintf(vlan_ifname,
-						    sizeof(vlan_ifname),
-						    "%s.%d", tagged_interface,
-						    untagged);
-				else
-					os_snprintf(vlan_ifname,
-						    sizeof(vlan_ifname),
-						    "vlan%d", untagged);
+	wpa_printf(MSG_DEBUG, "VLAN: vlan_newlink(%s)", ifname);
 
-				clean = 0;
-				ifconfig_up(tagged_interface);
-				if (!vlan_add(tagged_interface, untagged,
-					      vlan_ifname))
-					clean |= DVLAN_CLEAN_VLAN;
+	for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
+		if (vlan->configured)
+			continue;
+		if (os_strcmp(ifname, vlan->ifname))
+			continue;
 
-				if (!br_addif(br_name, vlan_ifname))
-					clean |= DVLAN_CLEAN_VLAN_PORT;
+		vlan->configured = 1;
 
-				dyn_iface_get(hapd, vlan_ifname, clean);
+		untagged = vlan->vlan_desc.untagged;
 
-				ifconfig_up(vlan_ifname);
-			}
+		if (untagged > 0 && untagged <= MAX_VLAN_ID) {
+			vlan_bridge_name(br_name, hapd, untagged);
+
+			vlan_get_bridge(br_name, hapd, untagged);
 
 			if (!br_addif(br_name, ifname))
 				vlan->clean |= DVLAN_CLEAN_WLAN_PORT;
+		}
 
-			ifconfig_up(ifname);
+		ifconfig_up(ifname);
 
-			break;
-		}
-		vlan = vlan->next;
+		break;
 	}
 }
 
-
-static void vlan_dellink(char *ifname, struct hostapd_data *hapd)
+static void vlan_dellink_tagged(int vlan_naming, char *tagged_interface,
+				char *br_name, int vid,
+				struct hostapd_data *hapd)
 {
 	char vlan_ifname[IFNAMSIZ];
-	char br_name[IFNAMSIZ];
-	struct hostapd_vlan *first, *prev, *vlan = hapd->conf->vlan;
+	int clean;
+
+	if (vlan_naming ==  DYNAMIC_VLAN_NAMING_WITH_DEVICE)
+		os_snprintf(vlan_ifname, sizeof(vlan_ifname), "%s.%d",
+			     tagged_interface,  vid);
+	else
+		os_snprintf(vlan_ifname, sizeof(vlan_ifname), "vlan%d",
+			     vid);
+
+	clean = dyn_iface_put(hapd, vlan_ifname);
+
+	if (clean & DVLAN_CLEAN_VLAN_PORT)
+		br_delif(br_name, vlan_ifname);
+
+	if (clean & DVLAN_CLEAN_VLAN) {
+		ifconfig_down(vlan_ifname);
+		vlan_rem(vlan_ifname);
+	}
+}
+
+static void vlan_put_bridge(char *br_name, struct hostapd_data *hapd, int vid)
+{
+	int clean;
 	char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
 	int vlan_naming = hapd->conf->ssid.vlan_naming;
-	int clean, untagged;
+
+	if (tagged_interface)
+		vlan_dellink_tagged(vlan_naming, tagged_interface, br_name,
+				    vid, hapd);
+
+	clean = dyn_iface_put(hapd, br_name);
+	if ((clean & DVLAN_CLEAN_BR) &&
+	    br_getnumports(br_name) == 0) {
+		ifconfig_down(br_name);
+		br_delbr(br_name);
+	}
+}
+
+static void vlan_dellink(char *ifname, struct hostapd_data *hapd)
+{
+	struct hostapd_vlan *first, *prev, *vlan = hapd->conf->vlan;
+	char br_name[IFNAMSIZ];
+	int untagged;
 
 	wpa_printf(MSG_DEBUG, "VLAN: vlan_dellink(%s)", ifname);
 
 	first = prev = vlan;
 
 	while (vlan) {
+		if (os_strcmp(ifname, vlan->ifname)) {
+			prev = vlan;
+			vlan = vlan->next;
+			continue;
+		}
+
+		if (!vlan->configured)
+			goto skip_counting;
+
 		untagged = vlan->vlan_desc.untagged;
-		if (os_strcmp(ifname, vlan->ifname) == 0 &&
-		    vlan->configured) {
-			if (hapd->conf->vlan_bridge[0]) {
-				os_snprintf(br_name, sizeof(br_name), "%s%d",
-					    hapd->conf->vlan_bridge,
-					    untagged);
-			} else if (tagged_interface) {
-				os_snprintf(br_name, sizeof(br_name),
-				            "br%s.%d", tagged_interface,
-					    untagged);
-			} else {
-				os_snprintf(br_name, sizeof(br_name),
-					    "brvlan%d", untagged);
-			}
+
+		if (untagged > 0 && untagged <= MAX_VLAN_ID) {
+			vlan_bridge_name(br_name, hapd, untagged);
 
 			if (vlan->clean & DVLAN_CLEAN_WLAN_PORT)
 				br_delif(br_name, vlan->ifname);
 
-			if (tagged_interface) {
-				if (vlan_naming ==
-				    DYNAMIC_VLAN_NAMING_WITH_DEVICE)
-					os_snprintf(vlan_ifname,
-						    sizeof(vlan_ifname),
-						    "%s.%d", tagged_interface,
-						    untagged);
-				else
-					os_snprintf(vlan_ifname,
-						    sizeof(vlan_ifname),
-						    "vlan%d", untagged);
-
-				clean = dyn_iface_put(hapd, vlan_ifname);
-
-				if (clean & DVLAN_CLEAN_VLAN_PORT)
-					br_delif(br_name, vlan_ifname);
-
-				if (clean & DVLAN_CLEAN_VLAN) {
-					ifconfig_down(vlan_ifname);
-					vlan_rem(vlan_ifname);
-				}
-			}
-
-			clean = dyn_iface_put(hapd, br_name);
-			if ((clean & DVLAN_CLEAN_BR) &&
-			    br_getnumports(br_name) == 0) {
-				ifconfig_down(br_name);
-				br_delbr(br_name);
-			}
+			vlan_put_bridge(br_name, hapd, untagged);
 		}
 
-		if (os_strcmp(ifname, vlan->ifname) == 0) {
-			if (vlan == first) {
-				hapd->conf->vlan = vlan->next;
-			} else {
-				prev->next = vlan->next;
-			}
-			os_free(vlan);
+skip_counting:
+		if (vlan == first)
+			hapd->conf->vlan = vlan->next;
+		else
+			prev->next = vlan->next;
 
-			break;
-		}
-		prev = vlan;
-		vlan = vlan->next;
+		os_free(vlan);
+
+		break;
 	}
 }
 
@@ -1045,9 +1069,7 @@ struct hostapd_vlan * vlan_add_dynamic(struct hostapd_data *hapd,
 	struct hostapd_vlan *n = NULL;
 	char *ifname, *pos;
 
-	if (vlan == NULL || vlan_desc.untagged <= 0 ||
-	    vlan_desc.untagged > MAX_VLAN_ID ||
-	    vlan->vlan_id != VLAN_ID_WILDCARD)
+	if (vlan == NULL || vlan->vlan_id != VLAN_ID_WILDCARD)
 		return NULL;
 
 	wpa_printf(MSG_DEBUG, "VLAN: %s(vlan_id=%d ifname=%s)",
-- 
2.1.4




More information about the Hostap mailing list