]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/hooks/ports/vlan
vlan: Add support for 802.1ad (QinQ)
[people/ms/network.git] / src / hooks / ports / vlan
index 98178e3d823ef38b5499ca849bdc73e92becf933..af563eed4bfa7b3ce9df31dd2c555411dba4de4b 100644 (file)
 
 HOOK_SETTINGS=(
        "ADDRESS"
+       "ID"
        "PARENT_PORT"
-       "TAG"
+       "PROTOCOL"
 )
 
+# Set the default to 802.1Q
+DEFAULT_PROTOCOL="${VLAN_SUPPORTED_PROTOCOLS[0]}"
+
 PORT_PARENTS_VAR="PARENT_PORT"
 
 hook_check_settings() {
+       assert ismac ADDRESS
        assert isset PARENT_PORT
-       assert isinteger TAG
 
-       if isset ADDRESS; then
-               assert ismac ADDRESS
-       fi
+       assert isset PROTOCOL
+       assert vlan_supported_protocol "${PROTOCOL}"
 
-       if [ ${TAG} -gt 4096 ]; then
-               error "TAG is greater than 4096."
-               exit ${EXIT_ERROR}
-       fi
-
-       local reserved
-       for reserved in 0 4095; do
-               if [ "${TAG}" = "${reserved}" ]; then
-                       error "TAG=${reserved} is reserved."
-                       exit ${EXIT_ERROR}
-               fi
-       done
+       assert isinteger ID
+       assert vlan_valid_id "${ID}"
 }
 
 hook_find_port_name() {
+       assert isset ID
        assert isset PARENT_PORT
-       assert isset TAG
 
-       print "${PARENT_PORT}${VLAN_PORT_INTERFIX}${TAG}"
+       print "${PARENT_PORT}${VLAN_PORT_INTERFIX}${ID}"
 }
 
 hook_parse_cmdline() {
@@ -70,6 +63,15 @@ hook_parse_cmdline() {
                                        return ${EXIT_CONF_ERROR}
                                fi
                                ;;
+                       --id=*)
+                               ID=$(cli_get_val "${1}")
+
+                               # Validate VLAN ID
+                               if ! vlan_valid_id "${ID}"; then
+                                       error "Invalid VLAN ID: ${ID}"
+                                       return ${EXIT_CONF_ERROR}
+                               fi
+                               ;;
                        --port=*)
                                PARENT_PORT=$(cli_get_val "${1}")
 
@@ -79,8 +81,15 @@ hook_parse_cmdline() {
                                        return ${EXIT_CONF_ERROR}
                                fi
                                ;;
-                       --tag=*)
-                               TAG=$(cli_get_val "${1}")
+                       --protocol=*)
+                               PROTOCOL="$(cli_get_val "${1}")"
+
+                               # Check if PROTOCOL is supported
+                               if ! vlan_supported_protocol "${PROTOCOL}"; then
+                                       error "Protocol '${PROTOCOL}' is not supported"
+                                       error "Choose one of ${VLAN_SUPPORTED_PROTOCOLS[*]}"
+                                       return ${EXIT_CONF_ERROR}
+                               fi
                                ;;
                        *)
                                error "Unknown argument '${1}'"
@@ -113,10 +122,23 @@ hook_create() {
                return ${EXIT_ERROR}
        fi
 
+       # Create the partent port first
+       if ! port_create "${PARENT_PORT}"; then
+               error "Could not bring up parent port: ${PARENT_PORT}"
+               return ${EXIT_ERROR}
+       fi
+
        # Create the VLAN device
-       vlan_create "${port}" "${PARENT_PORT}" "${TAG}" "${ADDRESS}"
+       if ! vlan_create "${port}" \
+                       --address="${ADDRESS}" \
+                       --id="${ID}" \
+                       --parent="${PARENT_PORT}" \
+                       --protocol="${PROTOCOL}"; then
+               error "Could not create port: ${port}"
+               return ${EXIT_ERROR}
+       fi
 
-       exit ${EXIT_OK}
+       return ${EXIT_OK}
 }
 
 hook_remove() {