]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.vlan
vlan: Rename tag to id
[people/ms/network.git] / src / functions / functions.vlan
index 8bcf169df8846eccab4ac1db66164c13b23eb078..9a70c95480e3a99609e3e7e5c66b0a729e5f411f 100644 (file)
@@ -24,80 +24,81 @@ PROC_NET_VLAN_CONFIG="${PROC_NET_VLAN}/config"
 
 VLAN_PORT_INTERFIX="v"
 
-function vlan_init() {
-       ebtables-restore <<EOF
-*filter
-:INPUT ACCEPT
-:FORWARD ACCEPT
-:OUTPUT ACCEPT
-
-*broute
-:BROUTING ACCEPT
--A BROUTING -p 802_1Q -j DROP
-EOF
-}
+vlan_create() {
+       local device="${1}"
+       shift
 
-function vlan_create() {
-       local device=${1}
        assert isset device
 
-       local parent=${2}
-       assert isset parent
-
-       local tag=${3}
-       assert isinteger tag
-
-       local address=${4}
-       if isset address; then
-               assert ismac address
+       local address
+       local id
+       local parent
+
+       # Parse command line arguments
+       while [ $# -gt 0 ]; do
+               case "${1}" in
+                       --address=*)
+                               address=$(cli_get_val "${1}")
+                               ;;
+                       --id=*)
+                               id=$(cli_get_val "${1}")
+                               ;;
+                       --parent=*)
+                               parent=$(cli_get_val "${1}")
+                               ;;
+                       *)
+                               error "Unrecognized argument: ${1}"
+                               return ${EXIT_ERROR}
+                               ;;
+               esac
+               shift
+       done
+
+       # Generate a random MAC address if none was passed
+       if ! isset address; then
+               address="$(mac_generate)"
        fi
 
-       # Check if a device with the name does already exist.
-       if device_exists ${device}; then
-               log ERROR "device '${device}' does already exist"
+       # Check if address is valid
+       if ! ismac address; then
+               log ERROR "Invalid mac address: ${address}"
                return ${EXIT_ERROR}
        fi
 
-       # Check if the parent device exists.
-       if ! device_exists ${parent}; then
-               log ERROR "parent device '${parent}' does not exist"
+       # Check if a device with the name does already exist
+       if device_exists "${device}"; then
+               log ERROR "Device '${device}' already exists"
                return ${EXIT_ERROR}
        fi
 
-       # Load ebtables stuff.
-       vlan_init
-
-       local command="ip link add link ${parent} name ${device}"
-
-       if isset address; then
-               command="${command} address ${address}"
+       # Check if the parent device exists
+       if ! device_exists "${parent}"; then
+               log ERROR "Parent device '${parent}' does not exist"
+               return ${EXIT_ERROR}
        fi
 
-       command="${command} type vlan id ${tag}"
+       # Make the command
+       local command=(
+               ip link add link "${parent}" name "${device}"
+                       address "${address}" type vlan id "${id}"
+       )
 
-       cmd_quiet ${command}
-       local ret=$?
-
-       if [ ${ret} -eq ${EXIT_OK} ]; then
-               log DEBUG "vlan device '${device}' has been created"
-       else
-               log ERROR "could not create vlan device '${device}': ${ret}"
+       # Run the command
+       if ! cmd_quiet "${command[*]}"; then
+               log ERROR "Could not create VLAN device ${device}: $?"
+               return ${EXIT_ERROR}
        fi
 
-       return ${ret}
-}
+       log DEBUG "Created VLAN device ${device} (parent = ${parent}, id = ${id})"
 
-function vlan_remove() {
-       local device=${1}
-       assert isset device
-
-       # Set down device (if not already done).
-       device_set_down ${device}
+       return ${EXIT_OK}
+}
 
-       device_delete ${device}
+vlan_remove() {
+       device_delete "$@"
 }
 
-function vlan_get_parent() {
+vlan_get_parent() {
        local device=${1}
        assert isset device
 
@@ -115,7 +116,7 @@ function vlan_get_parent() {
        return ${EXIT_ERROR}
 }
 
-function vlan_get_id() {
+vlan_get_id() {
        local device=${1}
        assert isset device
 
@@ -133,7 +134,7 @@ function vlan_get_id() {
        return ${EXIT_ERROR}
 }
 
-function vlan_get_by_parent_and_vid() {
+vlan_get_by_parent_and_vid() {
        local parent=${1}
        assert isset parent