]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/hooks/ports/vlan
vlan: Rename PARENT_DEVICE to PARENT_PORT
[people/ms/network.git] / src / hooks / ports / vlan
index b95f6a980fa7f4b081e78d4d033096ba2fda9c3e..f19eda493aaaf2910a6b87de198923dcb3e931fb 100644 (file)
 
 . /usr/lib/network/header-port
 
-HOOK_SETTINGS="HOOK ADDRESS PARENT_DEVICE TAG"
+HOOK_SETTINGS=(
+       "ADDRESS"
+       "PARENT_PORT"
+       "TAG"
+)
 
-PORT_PARENTS_VAR="PARENT"
+PORT_PARENTS_VAR="PARENT_PORT"
 
-function hook_check_settings() {
-       assert isset PARENT_DEVICE
+hook_check_settings() {
+       assert isset PARENT_PORT
        assert isinteger TAG
 
        if isset ADDRESS; then
@@ -47,72 +51,67 @@ function hook_check_settings() {
        done
 }
 
-function hook_new() {
-       while [ $# -gt 0 ]; do
-               case "${1}" in
-                       --parent-device=*)
-                               PARENT_DEVICE=$(cli_get_val ${1})
-                               ;;
-                       --address=*)
-                               ADDRESS=$(cli_get_val ${1})
-                               ;;
-                       --tag=*)
-                               TAG=$(cli_get_val ${1})
-                               ;;
-                       *)
-                               warning "Unknown argument '${1}'"
-                               ;;
-               esac
-               shift
-       done
+hook_find_port_name() {
+       assert isset PARENT_PORT
+       assert isset TAG
 
-       local port="${PARENT_DEVICE}${VLAN_PORT_INTERFIX}${TAG}"
-
-       port_settings_write "${port}" ${HOOK_SETTINGS}
-
-       exit ${EXIT_OK}
+       print "${PARENT_PORT}${VLAN_PORT_INTERFIX}${TAG}"
 }
 
-function hook_edit() {
-       local port=${1}
-       assert isset port
-       shift
-
-       port_settings_read "${port}" ${HOOK_SETTINGS}
-
+hook_parse_cmdline() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --address=*)
-                               ADDRESS=$(cli_get_val ${1})
+                               ADDRESS=$(cli_get_val "${1}")
+
+                               # Validate address
+                               if ! mac_is_valid "${ADDRESS}"; then
+                                       error "Invalid MAC address given: ${ADDRESS}"
+                                       return ${EXIT_CONF_ERROR}
+                               fi
+                               ;;
+                       --port=*)
+                               PARENT_PORT=$(cli_get_val "${1}")
+
+                               # Check if PARENT_PORT exists
+                               if ! port_exists "${PARENT_PORT}"; then
+                                       error "Port '${PARENT_PORT}' does not exist"
+                                       return ${EXIT_CONF_ERROR}
+                               fi
+                               ;;
+                       --tag=*)
+                               TAG=$(cli_get_val "${1}")
                                ;;
                        *)
-                               warning "Unknown argument '${1}'"
+                               error "Unknown argument '${1}'"
+                               return ${EXIT_CONF_ERROR}
                                ;;
                esac
                shift
        done
 
-       port_settings_write "${port}" ${HOOK_SETTINGS}
-
-       exit ${EXIT_OK} 
+       # Generate a random MAC address if none given
+       if ! isset ADDRESS; then
+               ADDRESS="$(mac_generate)"
+       fi
 }
 
-function hook_create() {
+hook_create() {
        local port="${1}"
        assert isset port
 
        device_exists "${port}" && exit ${EXIT_OK}
 
        # Read configruation
-       port_settings_read "${port}" ${HOOK_SETTINGS}
+       port_settings_read "${port}"
 
        # Create the VLAN device
-       vlan_create "${port}" "${PARENT_DEVICE}" "${TAG}" "${ADDRESS}"
+       vlan_create "${port}" "${PARENT_PORT}" "${TAG}" "${ADDRESS}"
 
        exit ${EXIT_OK}
 }
 
-function hook_remove() {
+hook_remove() {
        local port="${1}"
        assert isset port