]> git.ipfire.org Git - ipfire-3.x.git/commitdiff
network: Some general changes.
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 26 Aug 2009 15:55:30 +0000 (17:55 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 26 Aug 2009 15:55:30 +0000 (17:55 +0200)
New hook for interface trunks (experimental).

Major rewritements of the port hooks.

src/network/functions
src/network/hooks/bonding
src/network/hooks/ethernet
src/network/hooks/vlan
src/network/network
src/network/port [deleted file]
src/network/zone

index 45a0dae802bb85ea91b619ac14b35880fddecc1d..e4dfed4286d8c17337007ef22c349153a565978f 100644 (file)
@@ -28,7 +28,15 @@ CONNECTIONS_FILE=/var/log/network/connections.db
 
 CONFIG_ZONES=${CONFIG_DIR}/zones
 CONFIG_PORTS=${CONFIG_DIR}/ports
+CONFIG_HOOKS=${CONFIG_DIR}/hooks
 CONFIG_PPP=${CONFIG_DIR}/ppp
+CONFIG_UUIDS=${CONFIG_DIR}/uuids
+
+# Create config directories
+for dir in ${CONFIG_ZONES} ${CONFIG_PORTS} ${CONFIG_HOOKS} ${CONFIG_PPP} ${CONFIG_UUIDS}; do
+       [ -d "${dir}" ] && continue
+       mkdir -p "${dir}"
+done
 
 COMMON_DEVICE=port+
 
@@ -50,6 +58,7 @@ function get_device_by_mac() {
        local device
 
        for device in /sys/class/net/*; do
+               [ -d "${device}" ] || continue
                if [ "$(cat $device/address)" = "$mac" ]; then
                        device=${device##*/}
                        # Skip virtual devices
@@ -140,16 +149,52 @@ function macify() {
 }
 
 function device_exists() {
-       ip link show $(devicify ${1}) &>/dev/null
+       [ -n "${1}" ] || return ${EXIT_ERROR}
+       local device=$(devicify ${1})
+       [ -n "${device}" ] || return ${EXIT_ERROR}
+       ip link show ${device} &>/dev/null
+}
+
+function device_is_bonding() {
+       [ -d "/sys/class/net/${1}/bonding" ]
+}
+
+function device_is_bonded() {
+       local dev
+       for dev in /sys/class/net/*; do
+               # Skip crappy files
+               [ -d "${dev}" ] || continue
+
+               # Continue if not a bonding device
+               device_is_bonding "${dev##*/}" || continue
+
+               if grep -q "\<${1}\>" ${dev}/bonding/slaves; then
+                       return 0
+               fi
+       done
+       return 1
 }
 
 function device_is_up() {
        ip link show $(devicify ${1}) 2>/dev/null | grep -qE "<.*UP.*>"
 }
 
-function device_rename() {
-       local source=$1
-       local destination=$2
+function device_is_vlan() {
+       if [ ! -e "/proc/net/vlan/config" ]; then
+               return 1
+       fi
+       grep -q "^${1}" /proc/net/vlan/config
+}
+
+function device_has_vlans() {
+       if [ ! -e "/proc/net/vlan/config" ]; then
+               return 1
+       fi
+       grep -q "${1}$" /proc/net/vlan/config
+}
+
+function device_get_free() {
+       local destination=${1}
 
        # Replace + by a valid number
        if grep -q "+$" <<<${destination}; then
@@ -163,6 +208,12 @@ function device_rename() {
                        number=$(($number + 1))
                done
        fi
+       echo "${destination}"
+}
+
+function device_rename() {
+       local source=$1
+       local destination=$(device_get_free ${2})
 
        # Check if devices exists
        if ! device_exists ${source} || device_exists ${destination}; then
@@ -226,22 +277,43 @@ function zone_is_red() {
        [ "${zone#red}" != "${zone}" ]
 }
 
-function run_hooks() {
-       local action=${1}
-       local dir=${2}
+function _run_hooks() {
+       local action
+       local type
+
+       while [ $# -gt 0 ]; do
+               case "${1}" in
+                       --type=*)
+                               type=${1#--type=}
+                               ;;
+                       *)
+                               action="${1}"
+                               shift; break
+                               ;;
+               esac
+               shift
+       done
+
+       local dir=${1}; shift
        local failed
        local hook
        local hooks
-       shift 2
 
        if [ -z "${action}" ] || [ -z "${dir}" ]; then
                echo "Not enough parameters given." >&2
                return 1
        fi
 
-       for hook in $(find ${dir} -type f); do
+       for hook in $(find ${dir}); do
+               # Skip dirs
+               [ -d "${hook}" ] && continue
+
                (
                        . ${hook}
+                       # Skip hooks that are not of the given type
+                       if [ -n "${type}" ] && [ "$(hook_type ${HOOK})" != "${type}" ]; then
+                               continue
+                       fi
                        if [ -n "${HOOK}" ]; then
                                hook_run ${HOOK} --config=${hook} $@ ${action}
                                RET=$?
@@ -259,10 +331,22 @@ function run_hooks() {
        return ${failed}
 }
 
+function hooks_run_all() {
+       _run_hooks $@
+}
+
+function hooks_run_ports() {
+       _run_hooks --type="port" $@
+}
+
+function hooks_run_zones() {
+       _run_hooks --type="zone" $@
+}
+
 function hook_type() {
        local hook=${1}
        (
-               . $(hook_run ${hook} info)
+               eval $(${HOOKS_DIR}/${hook} info)
                echo "${HOOK_TYPE}"
        )
 }
@@ -386,6 +470,14 @@ function check_config() {
        fi
 }
 
+function mac_generate() {
+       local mac="00"
+       while [ "${#mac}" -lt 15 ]; do
+               mac="${mac}:$(cut -c 1-2 /proc/sys/kernel/random/uuid)"
+       done
+       echo "${mac}"
+}
+
 function connection() {
        local action
 
@@ -457,3 +549,7 @@ INSERT INTO connections(name, zone, interface, iplocal, ipremote, weight, dns, s
 EOF
 
 }
+
+function uuid() {
+       cat /proc/sys/kernel/random/uuid
+}
index 723e98df8ee9b14114f27376cca210b10eebb184..decf30fc6bec98b00ab77b4288120fe2119d6da0 100755 (executable)
 ########################################################################
 # Begin $NETWORK_DEVICES/services/bonding
 #
-# Description : Interface Bonding Script
+# Description : Bonding Script
 #
 # Authors     : Michael Tremer - michael.tremer@ipfire.org
 #
 # Version     : 00.00
 #
-# Notes       : This script collects two or more interfaces in one
-#               bonding intreface.
+# Notes       : This script adds bonding support.
 #
 ########################################################################
 
-. /etc/init/functions
-. ${CONFIG}
+. /lib/network/hook-header
 
-case "${2}" in
-       up)
-               MESSAGE="Bringing up bonding interface ${1}..."
-               ifenslave ${1} ${SLAVE_INTERFACES}
-               evaluate_retval
-       ;;
+HOOK_NAME=bonding
+HOOK_TYPE=port
 
-       down)
-               MESSAGE="Bringing down bonding interface ${1}..."
-               modprobe -r bonding 2>/dev/null
-               evaluate_retval
-       ;;
+DEFAULT_MODE=
+
+# TODO: Need to detect link failures!
+
+function port_name() {
+       echo "${zone}t+"
+}
+
+case "${action}" in
+       help)
+               ;;
+
+       info)
+               echo "HOOK_NAME=${HOOK_NAME}"
+               echo "HOOK_TYPE=${HOOK_TYPE}"
+               ;;
+
+       pre-up)
+               if ! grep -q ^bonding /proc/modules; then
+                       modprobe bonding
+                       echo "-bond0" > /sys/class/net/bonding_masters
+               fi
+
+               if device_exists ${MAC}; then
+                       device=$(devicify ${MAC})
+                       if ! device_is_bonding ${device}; then
+                               log_failure_msg "Device \"${device}\" is up, but not a bonding device."
+                               exit ${EXIT_ERR}
+                       fi
+                       exit ${EXIT_OK}
+               fi
+
+               device=$(device_get_free $(port_name))
+               echo "+${device}" > /sys/class/net/bonding_masters
+               ip link set ${device} address ${MAC}
+       
+               [ -n "${MODE}" ] && \
+                       echo "${MODE}" > /sys/class/net/${device}/bonding/mode
+       
+               for slave in ${SLAVES}; do
+                       if device_exists ${slave}; then
+                               if device_is_up ${slave}; then
+                                       log_warning_msg "Cannot enslave device \"${slave}\"."
+                                       continue
+                               fi
+                               echo "+$(devicify ${slave})" > /sys/class/net/${device}/bonding/slaves
+                               device_rename "$(devicify ${slave})" "${device}s+"
+                       else
+                               log_warning_msg "Device ${slave} does not exist."
+                       fi
+               done
+       
+               ip link set ${device} up
+       
+               log_success_msg "Setting up trunk ${MAC}..."
+               ;;
+
+       post-up)
+               device=$(devicify ${MAC})
+               if ! zone_has_device_attached ${zone} ${device}; then
+                       zone_add_port ${zone} ${device}
+               fi
+               ;;
+
+       pre-down)
+               device=$(devicify ${MAC})
+               if zone_has_device_attached ${zone} ${device}; then
+                       zone_del_port ${zone} ${device}
+               fi
+               ;;
+
+       post-down)
+               device=$(devicify ${MAC})
+               if port_is_up ${device}; then
+                       MESSAGE="Pulling down trunk ${MAC}..."
+                       ip link set ${device} down
+                       evaluate_retval
+                       echo "-${device}" > /sys/class/net/bonding_masters
+               fi
+               ;;
+
+       add)
+               MAC=$(mac_generate)
+               MODE=${DEFAULT_MODE}
+
+               while [ $# -gt 0 ]; do
+                       case "${1}" in
+                               --mac=*)
+                                       MAC=${1#--mac=}
+                                       ;;
+                               --mode=*)
+                                       MODE=${1#--mode=}
+                                       ;;
+                               *)
+                                       SLAVES="${SLAVES} $(macify ${1})"
+                                       ;;
+                       esac
+                       shift
+               done
+
+               UUID=$(uuid)
+               cat <<EOF > ${CONFIG_UUIDS}/${UUID}
+HOOK="${HOOK_NAME}"
+MAC="${MAC}"
+MODE="${MODE}"
+SLAVES="$(echo ${SLAVES})"
+EOF
+               ln -sf ${CONFIG_UUIDS}/${UUID} \
+                       ${CONFIG_ZONES}/${zone}/${HOOK_NAME}-${UUID}
+
+               log_success_msg "Configuration successfully saved!"
+               echo    "      MAC address : ${MAC}"
+               echo    "      Mode        : ${MODE}"
+               echo    "      Slaves      : $(echo ${SLAVES})"
+               ;;
+
+       rem)
+               ;;
+
+       status)
+               device_is_up $(devicify ${MAC})
+               RET=$?
+               if [ $RET -eq 0 ]; then
+                       log_success_msg "Port $(port_name) is up"
+               else
+                       log_failure_msg "Port $(port_name) is down"
+               fi
+               exit ${RET}
+               ;;
 
        *)
-               echo "Usage: ${0} [interface] {up|down}"
+               echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach|status}"
                exit 1
        ;;
 esac
index f4773b0811b13fabc506652575c2e82aa77f4019..61839176ebc2f0fc4bf436edfd08a00f1fa625d8 100755 (executable)
@@ -21,48 +21,6 @@ function port_name() {
        echo ${zone}p+
 }
 
-function do_up() {
-       device_is_up ${port} || ip link set $(devicify ${port}) up
-}
-
-function do_down() {
-       : # TODO
-       # Check if there are depending devices (like vlans, etc)
-       # If not, then pull down the device.
-}
-
-function do_attach() {
-       if zone_has_device_attached ${zone} $(get_device ${port}); then
-               # Device is already attached to the bridge
-               return 0
-       fi
-       message="Attaching ethernet port ${port}..."
-       device_rename $(get_device ${port}) $(port_name)
-       zone_add_port ${zone} $(get_device_by_mac ${port})
-       evaluate_retval
-}
-
-function do_detach() {
-       if zone_has_device_attached ${zone} $(get_device ${port}); then
-               message="Detatching ethernet port ${port}..."
-               zone_del_port ${zone} $(get_device_by_mac ${port})
-               device_rename $(get_device_by_mac ${port}) ${COMMON_DEVICE}
-               evaluate_retval
-       fi
-}
-
-function do_status() {
-       device_is_up ${port}
-       RET=$?
-       if [ $RET -eq 0 ]; then
-               log_success_msg "Port $(port_name) is up"
-       else
-               log_failure_msg "Port $(port_name) is down"
-       fi
-       return $RET
-       # TODO: Check if device is attached to a bridge.
-}
-
 case "${action}" in
        help)
                echo -e "${BOLD}Hook (${HOOK_NAME}) help:"
@@ -99,49 +57,66 @@ case "${action}" in
                echo "HOOK_TYPE=${HOOK_TYPE}"
                ;;
 
-       up)
-               check_config port zone
-               do_up
+       pre-up)
+               device_is_up ${MAC} || ip link set $(devicify ${MAC}) up
                ;;
-
-       down)
-               check_config port zone
-               do_down
+       
+       post-up)
+               if zone_has_device_attached ${zone} $(get_device ${MAC}); then
+                       # Device is already attached to the bridge
+                       exit ${EXIT_OK}
+               fi
+               message="Attaching ethernet port ${MAC}..."
+               device_rename $(get_device ${MAC}) $(port_name)
+               zone_add_port ${zone} $(get_device_by_mac ${MAC})
+               evaluate_retval
+               ;;
+               
+       pre-down)
+               if zone_has_device_attached ${zone} $(get_device ${MAC}); then
+                       message="Detatching ethernet port ${MAC}..."
+                       zone_del_port ${zone} $(get_device_by_mac ${MAC})
+                       device_rename $(get_device_by_mac ${MAC}) ${COMMON_DEVICE}
+                       evaluate_retval
+               fi
+               ;;
+       
+       post-down)
+               ## Possibly pull down the device (if there are no more vlan devices up...)
                ;;
 
        add)
-               cat <<EOF > ${CONFIG_PORTS}/${port}/ethernet
-HOOK=ethernet
+               ### XXX error handling
+
+               for dev in $@; do
+                       MAC=$(macify ${dev})
+                       UUID=$(uuid)
+                       cat <<EOF > ${CONFIG_UUIDS}/${UUID}
+HOOK="${HOOK_NAME}"
+MAC="${MAC}"
 EOF
-               ln -sf ${CONFIG_PORTS}/${port}/ethernet \
-                       ${CONFIG_ZONES}/${zone}/port-${port}-ethernet
-               ;;
-
-       remove)
-               check_config port zone
-               do_detach
-               do_down
-               rm -f \
-                       ${CONFIG_ZONES}/${zone}/port-${port}-ethernet \
-                       ${CONFIG_PORTS}/${port}/ethernet
-               ;;
+                       ln -sf ${CONFIG_UUIDS}/${UUID} \
+                               ${CONFIG_ZONES}/${zone}/${HOOK_NAME}-${UUID}
 
-       attach)
-               check_config port zone
-               do_up
-               do_attach
+                       log_success_msg "Configuration successfully saved!"
+                       echo    "      Device      : $(devicify ${MAC})"
+                       echo    "      MAC address : ${MAC}"
+               done
                ;;
 
-       detach)
-               check_config port zone
-               do_detach
-               do_down
+       rem)
+               # XXX to be done
                ;;
 
        status)
-               check_config port zone
-               do_status
-               exit ${?}
+               device_is_up ${MAC}
+               RET=$?
+               if [ $RET -eq 0 ]; then
+                       echo "Port $(port_name) is up"
+               else
+                       echo "Port $(port_name) is down"
+               fi
+               exit ${RET}
                ;;
 
        *)
index a5fe21f29c7930bd5c92715f6b07e8795bce8666..2211aee18d13ec1067defaeb4d4a2b8d65e58824 100755 (executable)
@@ -21,52 +21,6 @@ function port_name() {
        echo "${zone}v${ID}"
 }
 
-function do_up() {
-       if ! port_is_up $(port_name); then
-               grep -q ^8021q /proc/modules || modprobe 8021q
-               MESSAGE="Adding VLAN ${ID} to port ${port}..."
-               if ! device_is_up $(devicify ${port}); then
-                       ip link set $(devicify ${port}) up
-               fi
-               vconfig add $(devicify ${port}) ${ID} >/dev/null
-               evaluate_retval
-               ebtables -t broute -A BROUTING -p 802_1Q --vlan-id=${ID} -j DROP
-       fi
-}
-
-function do_down() {
-       if port_is_up $(port_name); then
-               MESSAGE="Removing VLAN ${ID} from port ${port}..."
-               vconfig rem $(get_device_by_mac_and_vid ${port} ${ID}) >/dev/null
-               evaluate_retval
-               ebtables -t broute -D BROUTING -p 802_1Q --vlan-id=${ID} -j DROP
-       fi
-}
-
-function do_attach() {
-       if ! zone_has_device_attached ${zone} $(port_name); then
-               device_rename $(get_device_by_mac_and_vid ${port} ${ID}) $(port_name)
-               zone_add_port ${zone} $(get_device ${port} ${ID})
-       fi
-}
-
-function do_detach() {
-       if zone_has_device_attached ${zone} $(port_name); then
-               zone_del_port ${zone} $(get_device_by_mac_and_vid ${port} ${ID})
-       fi
-}
-
-function do_status() {
-       device_is_up $(port_name)
-       RET=$?
-       if [ $RET -eq 0 ]; then
-               log_success_msg "Port $(port_name) is up"
-       else
-               log_failure_msg "Port $(port_name) is down"
-       fi
-       return $RET
-}
-
 case "${action}" in
        help)
                ;;
@@ -76,52 +30,79 @@ case "${action}" in
                echo "HOOK_TYPE=${HOOK_TYPE}"
                ;;
 
-       up)
-               check_config port ID
-               do_up
-               ;;
+       pre-up)
+               # Load the kernel module
+               grep -q ^8021q /proc/modules || modprobe 8021q
 
-       down)
-               check_config port ID
-               do_down
+               if ! port_is_up $(port_name); then
+                       MESSAGE="Adding VLAN ${ID} to port ${MAC}..."
+
+                       if ! device_is_up $(devicify ${MAC}); then
+                               ip link set $(devicify ${MAC}) up
+                       fi
+                       vconfig add $(devicify ${MAC}) ${ID} >/dev/null
+                       evaluate_retval
+
+                       ebtables -t broute -A BROUTING -p 802_1Q --vlan-id=${ID} -j DROP
+               fi
                ;;
 
-       add)
-               ID=$1
-               check_config port zone ID
-               cat <<EOF > ${CONFIG_PORTS}/${port}/vlan-${ID}
-HOOK=vlan
-ID=${ID}
-EOF
-               ln -sf ${CONFIG_PORTS}/${port}/vlan-${ID} \
-                       ${CONFIG_ZONES}/${zone}/port-${port}-vlan-${ID}
+       post-up)
+               if ! zone_has_device_attached ${zone} $(port_name); then
+                       device_rename $(get_device_by_mac_and_vid ${MAC} ${ID}) $(port_name)
+                       zone_add_port ${zone} $(get_device ${MAC} ${ID})
+               fi
+               ;;
+       
+       pre-down)
+               if zone_has_device_attached ${zone} $(port_name); then
+                       zone_del_port ${zone} $(get_device_by_mac_and_vid ${MAC} ${ID})
+               fi
                ;;
+               
+       post-down)
+               if port_is_up $(port_name); then
+                       MESSAGE="Removing VLAN ${ID} from port ${MAC}..."
+
+                       vconfig rem $(get_device_by_mac_and_vid ${MAC} ${ID}) >/dev/null
+                       evaluate_retval
 
-       remove)
-               check_config port zone ID
-               do_detach
-               do_down
-               rm -f \
-                       ${CONFIG_PORTS}/${port}/vlan-${ID} \
-                       ${CONFIG_ZONES}/${zone}/port-${port}-vlan-${ID}
+                       ebtables -t broute -D BROUTING -p 802_1Q --vlan-id=${ID} -j DROP
+               fi
                ;;
 
-       attach)
-               check_config port zone ID
-               do_up
-               do_attach
+       add)
+               MAC=$(macify ${1})
+               ID=${2} # Must be integer between 1 and 4096
+
+               UUID=$(uuid)
+               cat <<EOF > ${CONFIG_UUIDS}/${UUID}
+HOOK="${HOOK_NAME}"
+ID="${ID}"
+MAC="${MAC}"
+EOF
+               ln -sf ${CONFIG_UUIDS}/${UUID} \
+                       ${CONFIG_ZONES}/${zone}/${HOOK_NAME}-${UUID}
+
+               log_success_msg "Configuration successfully saved!"
+               echo    "      Device      : $(devicify ${MAC})"
+               echo    "      MAC address : ${MAC}"
+               echo    "      VLAN tag    : ${ID}"
                ;;
 
-       detach)
-               check_config port zone ID
-               do_detach
-               do_down
+       rem)
+               # XXX to be done
                ;;
 
        status)
-               check_config zone ID
-               do_status
-               exit ${?}
+               device_is_up $(port_name)
+               RET=$?
+               if [ $RET -eq 0 ]; then
+                       log_success_msg "Port $(port_name) is up"
+               else
+                       log_failure_msg "Port $(port_name) is down"
+               fi
+               exit ${RET}
                ;;
 
        *)
index 4c78b04095379a029721eddeda3bc36aa69964a5..8c198b49aa8e4efcfca2dd77d70992998a2c6322 100644 (file)
@@ -234,46 +234,24 @@ function port_show() {
 }
 
 function port_add() {
-       local port=${2}
        local zone=${1}
-       local hook=${3}
-       shift 3
-       
-       if [ -n "${port}" ]; then
-               port=$(macify ${port})
-       else
-               error "No port given on command line."
-               return 1
-       fi
-
-       if [ -z "${hook}" ]; then
-               hook="ethernet"
-       fi
+       local hook=${2}
+       shift 2
 
        decho "Function: port_add"
-       decho "  Zone: ${zone} Port: ${port} Hook: ${hook} $@"
+       decho "  Zone: ${zone} Hook: ${hook} $@"
 
        if ! zone_exists ${zone}; then
                error "Zone ${BOLD}${zone}${NORMAL} does not exist."
                return 1
        fi
 
-       if [ -z "$port" ]; then
-               error "Port ${BOLD}${port}${NORMAL} could not be found."
-               return 1
-       fi
-
-       if ! device_exists ${port}; then
-               error "Port ${BOLD}${port}${NORMAL} does not exist."
-               return 1
-       fi
-
        mkdir -p ${CONFIG_PORTS}/${port} 2>/dev/null
        if hook_exists ${hook}; then
-               /lib/network/hooks/${hook} --port=${port} --zone=${zone} add $@
+               /lib/network/hooks/${hook} --zone=${zone} add $@
                RET=$?
                if [ "$RET" -eq "0" ]; then
-                       vecho "Successfully added port ${BOLD}${port}${NORMAL} (${hook} $@) to ${BOLD}${zone}${NORMAL}."
+                       vecho "Successfully added port to ${BOLD}${zone}${NORMAL}."
                else
                        error "Hook ${BOLD}${hook}${NORMAL} exited with $RET."
                        return $RET
diff --git a/src/network/port b/src/network/port
deleted file mode 100755 (executable)
index 25867f7..0000000
+++ /dev/null
@@ -1,119 +0,0 @@
-#!/bin/bash
-###############################################################################
-#                                                                             #
-# IPFire.org - A linux based firewall                                         #
-# Copyright (C) 2009  Michael Tremer & Christian Schmidt                      #
-#                                                                             #
-# This program is free software: you can redistribute it and/or modify        #
-# it under the terms of the GNU General Public License as published by        #
-# the Free Software Foundation, either version 3 of the License, or           #
-# (at your option) any later version.                                         #
-#                                                                             #
-# This program is distributed in the hope that it will be useful,             #
-# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
-# GNU General Public License for more details.                                #
-#                                                                             #
-# You should have received a copy of the GNU General Public License           #
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
-#                                                                             #
-###############################################################################
-
-. /etc/init/functions
-. /lib/network/functions
-
-# Parse the command line
-while [ $# -gt 0 ]; do
-       case "${1}" in
-               --port=*)
-                       port=$(macify ${1#--port=})
-                       ;;
-               --zone=*)
-                       zone=${1#--zone=}
-                       ;;
-               -*)
-                       log_failure_msg "Unrecognized option: ${1}"
-                       exit ${EXIT_ERROR}
-                       ;;
-               *)
-                       action=${1}
-                       break
-                       ;;
-       esac
-       shift
-done
-
-if [ -z "${port}" ] || [ -z "${action}" ]; then
-       echo "Usage: $0 <port> <up|down|attach|detach|status> [hooks]"
-       echo
-       exit 1
-fi
-
-if ! device_exists ${port}; then
-       log_failure_msg "Port ${port} does not exist."
-       exit 1
-fi
-
-for arg in ${@-$(find ${CONFIG_PORTS}/${port}/)}; do
-       [ -L "${arg}" ] && arg=$(readlink ${arg})
-       arg=${arg##*/}
-       [ -e "${CONFIG_PORTS}/${port}/${arg}" ] || continue
-       hooks="${hooks} ${CONFIG_PORTS}/${port}/${arg}"
-done
-
-case "$action" in
-       up)
-               message="Setting up port ${port}..."
-               ip link set $(devicify ${port}) up
-               evaluate_retval
-               ;;
-
-       down)
-               ;;
-
-       attach)
-               ;;
-
-       detach)
-               ;;
-
-       status)
-               ;;
-
-       *)
-               log_failure_msg "\"${action}\" is not a valid command."
-               exit 1
-               ;;
-esac
-
-for hook in ${hooks}; do
-       [ -d "${hook}" ] && continue
-       (
-               . ${hook}
-               if [ -n "${HOOK}" ] && hook_exists ${HOOK}; then
-                       hook_run ${HOOK} --config=${hook} --port=${port} --zone=${zone} ${action}
-                       RET=$?
-               else
-                       echo -e "${FAILURE}Unable to process ${hook}. Either"
-                       echo -e "${FAILURE}the HOOK variable was not set,"
-                       echo -e "${FAILURE}or the specified hook cannot be executed."
-                       message=""
-                       log_failure_msg
-               fi
-               exit ${RET}
-       ) || failed=1
-done
-
-case "${action}" in
-       down)
-               # If no ports are running yet, push device down.
-               if ! $0 ${port} status &>/dev/null; then
-                       message="Pushing down port ${port}..."
-                       ip link set $(devicify ${port}) down
-                       evaluate_retval
-               fi
-               ;;
-       status)
-               exit ${failed}
-               ;;
-esac
index ace962fe3040bd135230d8ec4cf1f50af196cdc2..0f5b355da5a76b687dba73160847829c97fc0365 100755 (executable)
@@ -46,14 +46,12 @@ if ! zone_exists ${zone}; then
 fi
 
 case "$action" in
-       start|up)
+       start|up|reload)
                message="Bringing up zone ${zone}..."
 
-               run_hooks pre-up ${CONFIG_ZONES}/${zone} --zone=${zone}
+               hooks_run_all pre-up ${CONFIG_ZONES}/${zone} --zone=${zone}
 
-               # Check if bridge already exists
-               zone_status=$(brctl show 2>/dev/null)
-               if ! echo "${zone_status}" | grep -q "^${zone}"; then
+               if ! zone_is_up ${zone}; then   
                        # Create and bring up the zone
                        brctl addbr ${zone} || failed=1
                        brctl stp ${zone} on || failed=1
@@ -63,27 +61,18 @@ case "$action" in
                        evaluate_retval standard
                fi
 
-               # Attach ports
-               for config in $(find ${CONFIG_ZONES}/${zone}/ -name "port-*" 2>/dev/null); do
-                       port=${config##*/}; port=${port#port-}; port=${port%%-*}
-                       /lib/network/port --port=${port} --zone=${zone} attach ${config}
-               done
-
-               run_hooks post-up ${CONFIG_ZONES}/${zone} --zone=${zone}
+               # First bring up the ports to be able to start something like
+               # a dhcp client that needs a running interface.
+               hooks_run_ports post-up ${CONFIG_ZONES}/${zone} --zone=${zone}
+               hooks_run_zones post-up ${CONFIG_ZONES}/${zone} --zone=${zone}
                ;;
 
        stop|down)
                message="Bringing down zone ${zone}..."
-               # Check if bridge already exists
-               zone_status=$(brctl show 2>/dev/null)
-               if echo "${zone_status}" | grep -q "^${zone}"; then
-                       run_hooks pre-down ${CONFIG_ZONES}/${zone} --zone=${zone}
 
-                       # Detach ports
-                       for config in $(find ${CONFIG_ZONES}/${zone}/ -name "port-*" 2>/dev/null); do
-                               port=${config##*/}; port=${port#port-}; port=${port%%-*}
-                               /lib/network/port --port=${port} --zone=${zone} detach ${config}
-                       done
+               if zone_is_up ${zone}; then
+                       hooks_run_zones pre-down ${CONFIG_ZONES}/${zone} --zone=${zone}
+                       hooks_run_ports pre-down ${CONFIG_ZONES}/${zone} --zone=${zone}
 
                        # Bring down the zone and delete it
                        ip link set ${zone} down || failed=1
@@ -91,28 +80,13 @@ case "$action" in
                        (exit ${failed})
                        evaluate_retval standard
 
-                       run_hooks post-down ${CONFIG_ZONES}/${zone} --zone=${zone}
+                       hooks_run_all post-down ${CONFIG_ZONES}/${zone} --zone=${zone}
                else
                        log_warning_msg ${message}
                        log_warning_msg "Zone ${zone} does not exist."
                fi
                ;;
 
-       reload)
-               if ! zone_is_up ${zone}; then
-                       $0 --zone=${zone} start
-                       exit $?
-               fi
-
-               # Attach all ports
-               for config in $(find ${CONFIG_ZONES}/${zone}/ -name "port-*" 2>/dev/null); do
-                       port=${config##*/}; port=${port#port-}; port=${port%%-*}
-                       /lib/network/port --port=${port} --zone=${zone} attach ${config}
-               done
-
-               run_hooks post-up ${CONFIG_ZONES}/${zone} --zone=${zone}
-               ;;
-
        *)
                exit 1
                ;;