]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.vlan
vlan: Validate ID
[people/ms/network.git] / src / functions / functions.vlan
index 99a8baa375d513f0b664cd3d7e838d8127bdff87..ee2fb3400062a62c49f35b49a73f5a4b563f3e1b 100644 (file)
@@ -24,17 +24,21 @@ PROC_NET_VLAN_CONFIG="${PROC_NET_VLAN}/config"
 
 VLAN_PORT_INTERFIX="v"
 
-vlan_init() {
-       ebtables-restore <<EOF
-*filter
-:INPUT ACCEPT
-:FORWARD ACCEPT
-:OUTPUT ACCEPT
-
-*broute
-:BROUTING ACCEPT
--A BROUTING -p 802_1Q -j DROP
-EOF
+vlan_valid_id() {
+       local id="${1}"
+
+       # Must be an integer
+       if ! isinteger id; then
+               return ${EXIT_FALSE}
+       fi
+
+       # Must be between 1 and 4095
+       if [ ${id} -ge 1 ] && [ ${id} -le 4096 ]; then
+               return ${EXIT_TRUE}
+       fi
+
+       # Otherwise this is invalid
+       return ${EXIT_FALSE}
 }
 
 vlan_create() {
@@ -44,8 +48,8 @@ vlan_create() {
        assert isset device
 
        local address
+       local id=1
        local parent
-       local tag
 
        # Parse command line arguments
        while [ $# -gt 0 ]; do
@@ -53,12 +57,12 @@ vlan_create() {
                        --address=*)
                                address=$(cli_get_val "${1}")
                                ;;
+                       --id=*)
+                               id=$(cli_get_val "${1}")
+                               ;;
                        --parent=*)
                                parent=$(cli_get_val "${1}")
                                ;;
-                       --tag=*)
-                               tag=$(cli_get_val "${1}")
-                               ;;
                        *)
                                error "Unrecognized argument: ${1}"
                                return ${EXIT_ERROR}
@@ -78,6 +82,12 @@ vlan_create() {
                return ${EXIT_ERROR}
        fi
 
+       # Check VLAN ID
+       if ! vlan_valid_id "${id}"; then
+               log ERROR "Invalid VLAN ID: ${id}"
+               return ${EXIT_ERROR}
+       fi
+
        # Check if a device with the name does already exist
        if device_exists "${device}"; then
                log ERROR "Device '${device}' already exists"
@@ -90,13 +100,10 @@ vlan_create() {
                return ${EXIT_ERROR}
        fi
 
-       # Load ebtables stuff.
-       vlan_init
-
        # Make the command
        local command=(
                ip link add link "${parent}" name "${device}"
-                       address "${address}" type vlan id "${tag}"
+                       address "${address}" type vlan id "${id}"
        )
 
        # Run the command
@@ -105,7 +112,7 @@ vlan_create() {
                return ${EXIT_ERROR}
        fi
 
-       log DEBUG "Created VLAN device ${device} (parent = ${parent}, id = ${tag})"
+       log DEBUG "Created VLAN device ${device} (parent = ${parent}, id = ${id})"
 
        return ${EXIT_OK}
 }