From: Michael Tremer Date: Thu, 23 Jul 2009 08:09:10 +0000 (+0200) Subject: network: Various changes. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e70f9d78e0f6cbf4e1a3efb1ac3f56957eb9fc97;p=ipfire-3.x.git network: Various changes. --- diff --git a/src/network/lib/functions b/src/network/lib/functions index 445b7642f..a2d1c26ac 100644 --- a/src/network/lib/functions +++ b/src/network/lib/functions @@ -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 +} diff --git a/src/network/lib/hooks/ethernet b/src/network/lib/hooks/ethernet index 83c390f8e..5a3902c54 100755 --- a/src/network/lib/hooks/ethernet +++ b/src/network/lib/hooks/ethernet @@ -18,30 +18,34 @@ 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 < ${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 diff --git a/src/network/lib/hooks/ipv4-static b/src/network/lib/hooks/ipv4-static index 0caa63d48..99bc810c5 100755 --- a/src/network/lib/hooks/ipv4-static +++ b/src/network/lib/hooks/ipv4-static @@ -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 <${CONFIG_ZONES}/${zone}/ipv4-static_$IP HOOK="${HOOK_NAME}" IP="${IP}" diff --git a/src/network/lib/hooks/vlan b/src/network/lib/hooks/vlan index c47f27626..1a131cc1a 100755 --- a/src/network/lib/hooks/vlan +++ b/src/network/lib/hooks/vlan @@ -18,38 +18,36 @@ 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 < ${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}" diff --git a/src/network/lib/port b/src/network/lib/port index 1c7c63890..3c3e4b51b 100755 --- a/src/network/lib/port +++ b/src/network/lib/port @@ -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) diff --git a/src/network/lib/zone b/src/network/lib/zone index b81f5a7c5..fb3bdf78f 100755 --- a/src/network/lib/zone +++ b/src/network/lib/zone @@ -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 ;; diff --git a/src/network/network b/src/network/network index d60056f42..6b89899c5 100644 --- a/src/network/network +++ b/src/network/network @@ -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}."