[PATCH 4/6] parse untagged/tagged VLANs from config files

michael-dev at fami-braun.de michael-dev
Mon Apr 8 09:18:39 PDT 2013


From: Michael Braun <michael-dev at fami-braun.de>

Signed-hostap: Michael Braun <michael-dev at fami-braun.de>
---
 hostapd/config_file.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 115 insertions(+), 9 deletions(-)

diff --git a/hostapd/config_file.c b/hostapd/config_file.c
index 12d627a..a6ba770 100644
--- a/hostapd/config_file.c
+++ b/hostapd/config_file.c
@@ -19,6 +19,7 @@
 #include "radius/radius_client.h"
 #include "ap/wpa_auth.h"
 #include "ap/ap_config.h"
+#include "ap/vlan_init.h"
 #include "config_file.h"
 
 
@@ -33,6 +34,10 @@ static int hostapd_config_read_vlan_file(struct hostapd_bss_config *bss,
 	char buf[128], *pos, *pos2;
 	int line = 0, vlan_id;
 	struct hostapd_vlan *vlan;
+	struct vlan_info vlan_desc;
+	#ifdef CONFIG_VLAN_TAGGED
+	int i;
+	#endif
 
 	f = fopen(fname, "r");
 	if (!f) {
@@ -60,14 +65,65 @@ static int hostapd_config_read_vlan_file(struct hostapd_bss_config *bss,
 			vlan_id = VLAN_ID_WILDCARD;
 			pos = buf + 1;
 		} else {
-			vlan_id = strtol(buf, &pos, 10);
-			if (buf == pos || vlan_id < 1 ||
-			    vlan_id > MAX_VLAN_ID) {
-				wpa_printf(MSG_ERROR, "Invalid VLAN ID at "
+			os_memset(&vlan_desc, 0, sizeof(vlan_desc));
+			pos = buf;
+			#ifdef CONFIG_VLAN_TAGGED
+			/* count number of tagged vlans, separated by minus */
+			pos2 = pos;
+			while (*pos2 != '\0' && *pos2 != ' ' && *pos2 != '\t') {
+				if (*pos2 == 't')
+					vlan_desc.num_tagged++;
+				pos2++;
+			}
+			/* replace first minus with \0 for atoi */
+			pos2 = pos;
+			while (*pos2 != '\0' && *pos2 != ' ' && *pos2 != '\t' && *pos2 != 't')
+				pos2++;
+			*pos2 = '\0'; pos2++;
+			#endif
+			vlan_desc.untagged = atoi(pos);
+			if (vlan_desc.untagged < 0 && vlan_desc.untagged > MAX_VLAN_ID) {
+				wpa_printf(MSG_ERROR, "Invalid VLAN ID %d at "
+					   "line %d in '%s'", vlan_desc.untagged, line, fname);
+				fclose(f);
+			}
+			#ifndef CONFIG_VLAN_TAGGED
+			while (*pos != '\0' && *pos != ' ' && *pos != '\t')
+				pos++;
+			#else
+			pos = pos2;
+			if (vlan_desc.num_tagged > 0) {
+				vlan_desc.tagged = os_zalloc(sizeof(*vlan_desc.tagged) * vlan_desc.num_tagged);
+				if (!vlan_desc.tagged) {
+					wpa_printf(MSG_ERROR, "Out of memory parsing '%s' at "
+						   "line %d in '%s'", buf, line, fname);
+					fclose(f);
+					return -1;
+				}
+				for (i=0; i < vlan_desc.num_tagged; i++) {
+					while (*pos2 != '\0' && *pos2 != ' ' && *pos2 != '\t' && *pos2 != 't')
+						pos2++;
+					*pos2 = '\0'; pos2++;
+					vlan_desc.tagged[i] = atoi(pos);
+					if (vlan_desc.tagged[i] < 1 && vlan_desc.tagged[i] > MAX_VLAN_ID) {
+						wpa_printf(MSG_ERROR, "Invalid tagged VLAN ID %d at "
+									   "line %d in '%s'", vlan_desc.tagged[i], line, fname);
+						fclose(f);
+					}
+					pos = pos2;
+				}
+				vlan_cleanup(&vlan_desc);
+			}
+			#endif
+			if (!vlan_desc.untagged && !vlan_desc.num_tagged) {
+				wpa_printf(MSG_ERROR, "No VLAN ID at "
 					   "line %d in '%s'", line, fname);
 				fclose(f);
 				return -1;
 			}
+			vlan_id = vlan_get_id(&vlan_desc);
+			os_free(vlan_desc.tagged);
+			os_memset(&vlan_desc, 0, sizeof(vlan_desc));
 		}
 
 		while (*pos == ' ' || *pos == '\t')
@@ -124,7 +180,11 @@ static int hostapd_config_read_maclist(const char *fname,
 	int line = 0;
 	u8 addr[ETH_ALEN];
 	struct mac_acl_entry *newacl;
-	int vlan_id;
+	struct vlan_info vlan_desc;
+	#ifdef CONFIG_VLAN_TAGGED
+	char *pos2;
+	int i;
+	#endif
 
 	if (!fname)
 		return 0;
@@ -158,14 +218,58 @@ static int hostapd_config_read_maclist(const char *fname,
 			return -1;
 		}
 
-		vlan_id = 0;
+		os_memset(&vlan_desc, 0, sizeof(vlan_desc));
 		pos = buf;
 		while (*pos != '\0' && *pos != ' ' && *pos != '\t')
 			pos++;
 		while (*pos == ' ' || *pos == '\t')
 			pos++;
-		if (*pos != '\0')
-			vlan_id = atoi(pos);
+		if (*pos != '\0') {
+			#ifdef CONFIG_VLAN_TAGGED
+			/* count number of tagged vlans, separated by minus */
+			pos2 = pos;
+			while (*pos2 != '\0' && *pos2 != ' ' && *pos2 != '\t') {
+				if (*pos2 == 't')
+					vlan_desc.num_tagged++;
+				pos2++;
+			}
+			/* replace first minus with \0 for atoi */
+			pos2 = pos;
+			while (*pos2 != '\0' && *pos2 != ' ' && *pos2 != '\t' && *pos2 != 't')
+				pos2++;
+			*pos2 = '\0'; pos2++;
+			#endif
+			vlan_desc.untagged = atoi(pos);
+			if (vlan_desc.untagged < 0 && vlan_desc.untagged > MAX_VLAN_ID) {
+				wpa_printf(MSG_ERROR, "Invalid VLAN ID %d at "
+					   "line %d in '%s'", vlan_desc.untagged, line, fname);
+				fclose(f);
+			}
+			#ifdef CONFIG_VLAN_TAGGED
+			if (vlan_desc.num_tagged > 0) {
+				vlan_desc.tagged = os_zalloc(sizeof(*vlan_desc.tagged) * vlan_desc.num_tagged);
+				if (!vlan_desc.tagged) {
+					wpa_printf(MSG_ERROR, "Out of memory parsing '%s' at "
+						   "line %d in '%s'", buf, line, fname);
+					fclose(f);
+					return -1;
+				}
+				for (i=0; i < vlan_desc.num_tagged; i++) {
+					pos = pos2;
+					while (*pos2 != '\0' && *pos2 != ' ' && *pos2 != '\t' && *pos2 != 't')
+						pos2++;
+					*pos2 = '\0'; pos2++;
+					vlan_desc.tagged[i] = atoi(pos);
+					if (vlan_desc.tagged[i] < 1 && vlan_desc.tagged[i] > MAX_VLAN_ID) {
+						wpa_printf(MSG_ERROR, "Invalid tagged VLAN ID %d at "
+									   "line %d in '%s'", vlan_desc.tagged[i], line, fname);
+						fclose(f);
+					}
+				}
+				vlan_cleanup(&vlan_desc);
+			}
+			#endif
+		}
 
 		newacl = os_realloc_array(*acl, *num + 1, sizeof(**acl));
 		if (newacl == NULL) {
@@ -176,8 +280,10 @@ static int hostapd_config_read_maclist(const char *fname,
 
 		*acl = newacl;
 		os_memcpy((*acl)[*num].addr, addr, ETH_ALEN);
-		(*acl)[*num].vlan_id = vlan_id;
+		(*acl)[*num].vlan_id = vlan_get_id(&vlan_desc);
 		(*num)++;
+		os_free(vlan_desc.tagged);
+		os_memset(&vlan_desc, 0, sizeof(vlan_desc));
 	}
 
 	fclose(f);
-- 
1.8.1.5




More information about the Hostap mailing list