]> git.ipfire.org Git - ipfire-3.x.git/commitdiff
network: Various changes.
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 23 Jul 2009 08:09:10 +0000 (10:09 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 23 Jul 2009 08:09:10 +0000 (10:09 +0200)
src/network/lib/functions
src/network/lib/hooks/ethernet
src/network/lib/hooks/ipv4-static
src/network/lib/hooks/vlan
src/network/lib/port
src/network/lib/zone
src/network/network

index 445b7642fdd2c7334612554127b44080056b8fdb..a2d1c26ac1b2b954d8e0e42cc64f0fec195672bb 100644 (file)
@@ -32,19 +32,19 @@ EXIT_OK=0
 EXIT_ERROR=1
 EXIT_CONF_ERROR=2
 
+VALID_ZONES="blue green orange red grey"
+
 [ -n "${DEBUG}"   ] || DEBUG=
 [ -n "${VERBOSE}" ] || VERBOSE=
 
 function is_mac() {
-       egrep -q "^[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]$" <<<$1
+       [[ $1 =~ ^[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]$ ]]
 }
 
 function get_device_by_mac() {
-       local mac
+       local mac=${1}
        local device
 
-       mac=$1
-
        for device in /sys/class/net/*; do
                if [ "$(cat $device/address)" = "$mac" ]; then
                        device=${device##*/}
@@ -233,8 +233,8 @@ function run_hooks() {
        for hook in $(find ${dir} -type f); do
                (
                        . ${hook}
-                       if [ -n "${HOOK}" ] && hook_exists ${HOOK}; then
-                               /lib/network/hooks/${HOOK} --config=${hook} $@ ${action}
+                       if [ -n "${HOOK}" ]; then
+                               hook_run ${HOOK} --config=${hook} $@ ${action}
                                RET=$?
                        else
                                echo -e "${FAILURE}Unable to process ${hook}. Either"
@@ -275,7 +275,7 @@ function hook_run() {
                log_failure_msg "Hook ${hook} cannot be found or is not executeable."
                return ${EXIT_ERROR}
        fi
-       decho "Running hook: ${hook} $@"
+       [ -n "${DEBUG}" ] && echo "Running hook: ${hook} $@"
        DEBUG=${DEBUG} VERBOSE=${VERBOSE} ${HOOKS_DIR}/${hook} $@
        return $?
 }
@@ -328,3 +328,58 @@ function zone_run() {
        DEBUG=${DEBUG} VERBOSE=${VERBOSE} ${HOME_DIR}/zone --zone=${zone} $@
        return $?
 }
+
+function zone_valid_name() {
+       local zone=${1}
+       local match
+
+       local i
+       for i in ${VALID_ZONES}; do
+               match="${match}|${i}[0-9]{1,5}"
+       done
+       [[ ${zone} =~ ${match:1:${#match}} ]]
+       return $?
+}
+
+function isset() {
+       local key=${1}
+       [ -n "${!key}" ] && return
+       if [[ ${key} =~ port|zone ]]; then
+               echo "ERROR: The --${key} flag is not set." >&2
+       else
+               echo "ERROR: The \"${key}\" variable is not set properly." >&2
+       fi
+       return 1
+}
+
+# Test if device is attached to the given bridge
+function zone_has_device_attached () {
+       local zone=${1}
+       local device=${2}
+
+       if device_exists ${device} || zone_exists ${zone}; then
+               return 2
+       fi
+
+       [ -d "/sys/class/net/${zone}/brif/${device}" ]
+       return $?
+}
+
+function device_has_ipv4() {
+       local device=${1}
+       local ip=${2}
+       ip addr show ${device} | grep inet | fgrep -q ${ip}
+}
+
+function check_config() {
+       local failed
+       local i
+
+       for i in $@; do
+               isset ${i} || failed=1
+       done
+       if [ "${failed}" = "1" ]; then
+               echo "Exiting..."
+               exit ${EXIT_ERROR}
+       fi
+}
index 83c390f8ee7fd97a982e8b242de3e63a126e9ed4..5a3902c543a48aa57b913fe586592a28cb52b8ed 100755 (executable)
 HOOK_NAME=ethernet
 HOOK_TYPE=port
 
-function check_config() {
-       : # XXX TODO
-}
-
 function port_name() {
        echo ${zone}s+
 }
 
 function do_up() {
-       : # Do nothing
+       device_is_up ${port} || ip link set $(devicify ${port}) up
 }
 
 function do_down() {
-       : # Do nothing
+       : # 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
        device_rename $(get_device ${port}) $(port_name)
        zone_add_port ${zone} $(get_device_by_mac ${port})
 }
 
 function do_detach() {
-       zone_del_port ${zone} $(get_device_by_mac ${port})
-       device_rename $(get_device_by_mac ${port}) ${COMMON_DEVICE}
+       if zone_has_device_attached ${zone} $(get_device ${port}); then
+               zone_del_port ${zone} $(get_device_by_mac ${port})
+               device_rename $(get_device_by_mac ${port}) ${COMMON_DEVICE}
+       fi
 }
 
 function do_status() {
@@ -120,12 +124,14 @@ case "${action}" in
                ;;
 
        up)
+               check_config port zone
                do_up
-       ;;
+               ;;
 
        down)
+               check_config port zone
                do_down
-       ;;
+               ;;
 
        add)
                cat <<EOF > ${CONFIG_PORTS}/${port}/ethernet
@@ -133,34 +139,39 @@ HOOK=ethernet
 EOF
                ln -sf ${CONFIG_PORTS}/${port}/ethernet \
                        ${CONFIG_ZONES}/${zone}/port-${port}-ethernet
-       ;;
+               ;;
 
        remove)
+               check_config port zone
                do_detach
-               #do_down
+               do_down
                rm -f \
                        ${CONFIG_ZONES}/${zone}/port-${port}-ethernet \
                        ${CONFIG_PORTS}/${port}/ethernet
-       ;;
+               ;;
 
        attach)
+               check_config port zone
                do_up
                do_attach
-       ;;
+               ;;
 
        detach)
+               check_config port zone
                do_detach
-       ;;
+               do_down
+               ;;
 
        status)
+               check_config port zone
                do_status
                exit ${?}
-       ;;
+               ;;
 
        *)
                echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach|status}"
                exit 1
-       ;;
+               ;;
 esac
 
 # End $NETWORK_DEVICES/services/ethernet
index 0caa63d489d1db0a5fba02b4035fe73cc4515d31..99bc810c59c29627ff0e7f7cf214b096bc3a96aa 100755 (executable)
@@ -98,9 +98,11 @@ case "${action}" in
 
        post-up)
                check_config
-               MESSAGE="Adding IPv4 address ${IP} to zone ${zone} interface..."
-               ip addr add ${args} dev ${zone}
-               evaluate_retval
+               if ! device_has_ipv4 ${zone} ${IP}; then
+                       MESSAGE="Adding IPv4 address ${IP} to zone ${zone} interface..."
+                       ip addr add ${args} dev ${zone}
+                       evaluate_retval
+               fi
        
                if [ -n "${GATEWAY}" ]; then
                        if ip route | grep -q default; then
@@ -121,16 +123,17 @@ case "${action}" in
                        evaluate_retval
                fi
        
-               MESSAGE="Removing IPv4 address ${IP} from zone ${zone}..."
-               ip addr del ${args} dev ${zone}
-               evaluate_retval
+               if device_has_ipv4 ${zone} ${IP}; then
+                       MESSAGE="Removing IPv4 address ${IP} from zone ${zone}..."
+                       ip addr del ${args} dev ${zone}
+                       evaluate_retval
+               fi
                ;;
 
        post-down)
                ;;
 
        config)
-               shift 2
                while [ $# -gt 0 ]; do
                        case "$1" in
                                --ip=*)
@@ -158,7 +161,7 @@ case "${action}" in
                        esac
                        shift
                done
-               check_config
+               #check_config
                cat <<EOF >${CONFIG_ZONES}/${zone}/ipv4-static_$IP
 HOOK="${HOOK_NAME}"
 IP="${IP}"
index c47f276266c9e18cbc3a60993a1a1c31fdd53479..1a131cc1a72c6517b0174aa85c041790d7b53772 100755 (executable)
 HOOK_NAME=vlan
 HOOK_TYPE=port
 
-function check_config() {
-       : # TODO
-}
-
 function port_name() {
-       echo "${ZONE}v${ID}"
+       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}..."
-               vconfig add ${device} ${ID} >/dev/null
+               vconfig add $(devicify ${port}) ${ID} >/dev/null
                evaluate_retval
+               ebtables -t broute -A BROUTING -p 802_1Q -i $(devicify ${port}) -j DROP
        fi
 }
 
 function do_down() {
        if port_is_up $(port_name); then
                MESSAGE="Removing VLAN ${ID} from port ${port}..."
-               vconfig rem ${device} ${ID} >/dev/null
+               vconfig rem $(get_device_by_mac_and_vid ${port} ${ID}) >/dev/null
                evaluate_retval
+               ebtables -t broute -D BROUTING -p 802_1Q -i $(devicify ${port}) -j DROP
        fi
 }
 
 function do_attach() {
-       rename_device $(get_device_by_mac_and_vid ${port} ${ID}) $(port_name)
-       zone_add_port ${ZONE} $(get_device ${port} ${ID})
+       device_rename $(get_device_by_mac_and_vid ${port} ${ID}) $(port_name)
+       zone_add_port ${zone} $(get_device ${port} ${ID})
 }
 
 function do_detach() {
-       zone_del_port ${ZONE} $(get_device_by_mac_and_vid ${port} ${ID})
+       zone_del_port ${zone} $(get_device_by_mac_and_vid ${port} ${ID})
 }
 
 function do_status() {
@@ -69,7 +67,6 @@ while [ $# -gt 0 ]; do
        case "${1}" in
                --port=*)
                        port=$(macify ${1#--port=})
-                       device=$(devicify ${port})
                        ;;
                --config=*)
                        . ${1#--config=}
@@ -92,44 +89,61 @@ while [ $# -gt 0 ]; do
 done
 
 case "${action}" in
+       help)
+               ;;
+
+       info)
+               echo "HOOK_NAME=${HOOK_NAME}"
+               echo "HOOK_TYPE=${HOOK_TYPE}"
+               ;;
+
        up)
+               check_config port ID
                do_up
-       ;;
+               ;;
 
        down)
+               check_config port ID
                do_down
-       ;;
+               ;;
 
        add)
-               ID=$3
+               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}
-       ;;
+                       ${CONFIG_ZONES}/${zone}/port-${port}-vlan-${ID}
+               ;;
 
        remove)
+               check_config port zone ID
                do_detach
                do_down
                rm -f \
                        ${CONFIG_PORTS}/${port}/vlan-${ID} \
-                       ${CONFIG_ZONES}/${ZONE}/port-${port}-vlan-${ID}
-       ;;
+                       ${CONFIG_ZONES}/${zone}/port-${port}-vlan-${ID}
+               ;;
 
        attach)
+               check_config port zone ID
+               do_up
                do_attach
-       ;;
-       
+               ;;
+
        detach)
+               check_config port zone ID
                do_detach
-       ;;
+               do_down
+               ;;
 
        status)
+               check_config zone ID
                do_status
                exit ${?}
-       ;;
+               ;;
 
        *)
                echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach|status}"
index 1c7c63890726474a1de699e66be949d1d9f4e593..3c3e4b51bf8e99fcb71a03bcbfec3073176f1e90 100755 (executable)
@@ -54,8 +54,6 @@ if ! device_exists ${port}; then
        exit 1
 fi
 
-device=$(devicify ${port})
-
 for arg in ${@-$(find ${CONFIG_PORTS}/${port}/)}; do
        [ -L "${arg}" ] && arg=$(readlink ${arg})
        arg=${arg##*/}
@@ -74,7 +72,6 @@ case "$action" in
                ;;
 
        attach)
-               ip link set $(devicify ${port}) up # is this required here?
                ;;
 
        detach)
index b81f5a7c5bcd33297ab867bfa29a5e00d7d07752..fb3bdf78fcf1905a48a21eddd3761b65a36dc322 100755 (executable)
@@ -96,6 +96,21 @@ case "$action" in
                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
                ;;      
index d60056f420e5e2c9a52afbea477713b653d6c603..6b89899c5f6ad56711e336ba3fe1d7680b83ac6b 100644 (file)
@@ -367,6 +367,11 @@ function zone_add() {
                error "Zone ${BOLD}${zone}${NORMAL} already exists."
                return 1
        fi
+       
+       if ! zone_valid_name ${zone}; then
+               error "The given zone name is not valid."
+               return 1
+       fi
 
        mkdir -p ${CONFIG_ZONES}/${zone}
        vecho "Successfully added zone ${BOLD}${zone}${NORMAL}."