From: Michael Tremer Date: Wed, 4 Mar 2009 12:58:34 +0000 (+0100) Subject: Worked on networking stuff. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=90af6f2436222dcbd02c6654340cc2f4860a51c2;p=ipfire-3.x.git Worked on networking stuff. Added help to network command. Some more important changes. --- diff --git a/src/initscripts/networking/functions b/src/initscripts/networking/functions index 5dd1bec52..ba0c74757 100644 --- a/src/initscripts/networking/functions +++ b/src/initscripts/networking/functions @@ -53,19 +53,24 @@ function get_device_by_mac_and_vid() { local i local VID local DEVICE - for i in $(cat /proc/net/vlan/config 2>/dev/null); do - awk -F'|' '{ print $2 $3 }' | read DEVICE VID PARENT - if [ -n "${VID}" ] || [ -n "${DEVICE}" ]; then - continue - fi - if [ "${vid}" = "${VID}" ] && [ "$(macify ${PARENT})" = "${mac}" ]; then - echo "${DEVICE}" - return 0 - fi - done + grep '|' < /proc/net/vlan/config 2>/dev/null | sed "s/|//g" | \ + while read DEVICE VID PARENT; do + if [ "${vid}" = "${VID}" ] && [ "$(macify ${PARENT})" = "${mac}" ]; then + echo "${DEVICE}" + return 0 + fi + done return 1 } +function get_device() { + if [ ${#@} -gt 1 ]; then + get_device_by_mac_and_vid $@ + else + get_device_by_mac $@ + fi +} + function get_mac_by_device() { local device device=$1 @@ -76,6 +81,10 @@ function get_mac_by_device() { return 1 } +function get_mac() { + get_mac_by_device $@ +} + function devicify() { local device local mac @@ -135,7 +144,23 @@ function bridge_devices() { } function zone_add_port() { - brctl addif ${1} ${2} + local zone + local port + + zone=${1} + port=${2} + + brctl addif ${zone} ${port} +} + +function zone_del_port() { + local zone + local port + + zone=${1} + port=${2} + + brctl delif ${zone} ${port} } function all_zones() { diff --git a/src/initscripts/networking/hooks/ethernet b/src/initscripts/networking/hooks/ethernet index 72842d165..8823f9959 100644 --- a/src/initscripts/networking/hooks/ethernet +++ b/src/initscripts/networking/hooks/ethernet @@ -41,8 +41,16 @@ EOF ${CONFIG_PORTS}/${port}/ethernet ;; + attach) + zone_add_port ${ZONE} $(get_device ${port}) + ;; + + detach) + zone_del_port ${ZONE} $(get_device ${port}) + ;; + *) - echo "Usage: ${0} [interface] {up|down|add|remove}" + echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach}" exit 1 ;; esac diff --git a/src/initscripts/networking/hooks/vlan b/src/initscripts/networking/hooks/vlan index 907c72f0d..3647882e0 100644 --- a/src/initscripts/networking/hooks/vlan +++ b/src/initscripts/networking/hooks/vlan @@ -21,16 +21,20 @@ device=$(devicify ${port}) case "${2}" in up) - modprobe 8021q - MESSAGE="Adding VLAN ${ID} to port ${port}..." - vconfig add ${device} ${ID} &>/dev/null - evaluate_retval + if ! get_device ${port} ${ID}; then + modprobe 8021q + MESSAGE="Adding VLAN ${ID} to port ${port}..." + vconfig add ${device} ${ID} &>/dev/null + evaluate_retval + fi ;; down) - MESSAGE="Removing VLAN ${ID} from port ${port}..." - vconfig rem ${device} ${ID} &>/dev/null - evaluate_retval + if get_device ${port} ${ID}; then + MESSAGE="Removing VLAN ${ID} from port ${port}..." + vconfig rem ${device} ${ID} &>/dev/null + evaluate_retval + fi ;; add) @@ -44,13 +48,23 @@ EOF ;; remove) + # Should we do this here? What about detaching? + CONFIG=${CONFIG_ZONES}/${ZONE}/port-${port}-vlan-${ID} ${0} ${port} down rm -f \ ${CONFIG_PORTS}/${port}/vlan-${ID} \ ${CONFIG_ZONES}/${ZONE}/port-${port}-vlan-${ID} ;; + attach) + zone_add_port ${ZONE} $(get_device ${port} ${ID}) + ;; + + detach) + zone_del_port ${ZONE} $(get_device ${port} ${ID}) + ;; + *) - echo "Usage: ${0} [interface] {up|down|add|remove}" + echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach}" exit 1 ;; esac diff --git a/src/initscripts/networking/net-hotplug b/src/initscripts/networking/net-hotplug index fd79681e5..405eff231 100644 --- a/src/initscripts/networking/net-hotplug +++ b/src/initscripts/networking/net-hotplug @@ -25,9 +25,7 @@ if [ "$INTERFACE" = "" ]; then exit 1 fi -MAC=$(get_mac_by_device ${INTERFACE}) - -logger -t net "Received uevent for $INTERFACE ($ACTION)." +MAC=$(get_mac ${INTERFACE}) case $ACTION in add|register) @@ -46,12 +44,8 @@ case $ACTION in # interfaces that are registered then brought up *) export IN_HOTPLUG=1 - for file in ${NETWORK_DEVICES}/*/*; do - if [ "$(basename ${file})" = "${MAC}" ]; then - /etc/init.d/networking/ifup ${file} - exit $? - fi - done + /etc/init.d/networking/port ${MAC} up + # XXX Walk through zones here? ;; esac ;; @@ -64,12 +58,7 @@ case $ACTION in ;; *) export IN_HOTPLUG=1 - for file in ${NETWORK_DEVICES}/*/*; do - if [ "$(basename ${file})" = "${MAC}" ]; then - /etc/init.d/networking/ifdown ${file} - exit $? - fi - done + : # Nothing to do here at the moment ;; esac ;; diff --git a/src/initscripts/networking/port b/src/initscripts/networking/port index f2b7e611b..f85556be2 100644 --- a/src/initscripts/networking/port +++ b/src/initscripts/networking/port @@ -43,8 +43,6 @@ for arg in ${@-$(find ${CONFIG_PORTS}/${port}/)}; do hooks="${hooks} ${CONFIG_PORTS}/${port}/${arg}" done -echo "DEBUG: hooks: $hooks" - # Getting zone variable #local zone #if [ -z "$ZONE" ]; then diff --git a/src/initscripts/networking/zone b/src/initscripts/networking/zone index 54e06c41b..a8b5cd6a8 100644 --- a/src/initscripts/networking/zone +++ b/src/initscripts/networking/zone @@ -28,24 +28,30 @@ action=$2 case "$action" in up) message="Bringing up zone ${zone}..." - + # Check if bridge already exists zone_status=`brctl show 2>/dev/null` if ! echo "${zone_status}" | grep -q "^${zone}"; then - #/etc/init.d/networking/hooks pre-up ${CONFIG_ZONES}/${zone} - # Create and bring up the zone brctl addbr ${zone} || failed=1 ip link set ${zone} up || failed=1 (exit ${failed}) evaluate_retval standard + # When we boot, we should bring up every port [ "${IN_BOOT}" = "1" ] && \ for port in ${CONFIG_ZONES}/${zone}/port-*; do - #port=$(readlink ${port}) ZONE=${zone} /etc/init.d/networking/port ${port} up done + # Attach ports + for config in ${CONFIG_ZONES}/${zone}/port-*; do + port=${config##*/}; port=${port#port-}; port=${port%%-*} + echo "DEBUG: port $port" + ZONE=${zone} CONFIG=${config} \ + /etc/init.d/networking/port ${port} attach + done + ###/etc/init.d/networking/hooks post-up ${CONFIG_ZONES}/${zone} else log_warning_msg @@ -60,11 +66,12 @@ case "$action" in zone_status=`brctl show 2>/dev/null` if echo "${zone_status}" | grep -q "^${zone}"; then #/etc/init.d/networking/hooks pre-down ${CONFIG_ZONES}/${zone} - # remove devices - for port in ${CONFIG_ZONES}/${zone}/port-*; do - brctl delif ${zone} ${port##*/} - /etc/init.d/networking/port ${port} down + # Detach ports + for config in ${CONFIG_ZONES}/${zone}/port-*; do + port=${file#port-}; port=${port%%-*} + ZONE=${zone} CONFIG=${config} \ + /etc/init.d/networking/port ${port} detach done # Bring down the zone and delete it @@ -72,8 +79,6 @@ case "$action" in brctl delbr ${zone} || failed=1 (exit ${failed}) evaluate_retval standard - - #/etc/init.d/networking/hooks post-down ${CONFIG_ZONES}/${zone} else log_warning_msg message="Zone ${zone} does not exist." diff --git a/src/network/network b/src/network/network index 59422f601..a0faf0b0c 100644 --- a/src/network/network +++ b/src/network/network @@ -29,8 +29,91 @@ ERROR="\\033[1;31m" . /etc/init.d/networking/functions function usage() { - echo "Usage $0 - TODO" - _exit $1 + echo -e "${BOLD}Usage $0${NORMAL}\n" + case "$1" in + main|"") + echo "This script will help you configuring your network." + echo "You should know that there are two different things:" + echo + echo " zone: A group of ports." + echo " port: Connection to somewhere." + echo + echo " $0 [global flags] ... or" + echo " $0 [global flags] " + echo + echo -e "${BOLD}Global flags:${NORMAL}" + echo " --verbose -v - Turn on verbose mode." + echo " --debug -d - Turn on debug mode." + echo + echo -e "${BOLD}Command line options:${NORMAL}" + echo " help - Prints this help message." + echo " start - Starts the whole network." + echo " stop - Stops the whole network." + echo " restart - Restarts the whole network." + echo " reload - Reloads the whole network." + echo + echo " zone - Run \"$0 zone help\" for more information." + echo " port - Run \"$0 port help\" for more information." + echo + ;; + port) + echo -e "${BOLD}Port Configuration:${NORMAL}" + echo + echo " $0 [global options] port ..." + echo + echo -e "${BOLD}Commands:${NORMAL}" + echo -e " ${BOLD}show:${NORMAL}" + echo " Displays information about a given port." + echo + echo " Requires a \"port\"." + echo " Example: $0 port show 00:11:22:33:44:55" + echo " $0 port show black0" + echo + ;; + zone) + echo -e "${BOLD}Zone Configuration:${NORMAL}" + echo + echo " $0 [global options] zone ..." + echo + echo -e "${BOLD}Commands:${NORMAL}" + echo -e " ${BOLD}show:${NORMAL}" + echo " Displays information about a given zone." + echo + echo " Requires a \"zone\"." + echo " Example: $0 zone show green0" + echo + echo -e " ${BOLD}add:${NORMAL}" + echo " Adds a new zone." + echo + echo " Requires a \"zone\"." + echo " Example: $0 zone add green0" + echo + echo -e " ${BOLD}del:${NORMAL}" + echo " Deletes a zone." + echo + echo " Requires a \"zone\"." + echo " Example: $0 zone del green0" + echo + echo -e " ${BOLD}addport:${NORMAL}" + echo " Adds a port to a zone." + echo + echo " Requires a \"zone\" and \"port\"." + echo " Example: $0 zone addport green0" + echo + echo " You may also pass a hook and its parameters:" + echo " $0 zone addport green0 black0 vlan 10" + echo + echo -e " ${BOLD}delport:${NORMAL}" + echo " Deletes a port from a zone." + echo + echo " Requires a \"zone\" and \"port\"." + echo " Example: $0 zone delport green0" + echo + echo " You may also pass a hook and its parameters:" + echo " $0 zone delport green0 black0 vlan 10" + echo + esac + _exit ${2-1} } function debug() { @@ -246,7 +329,7 @@ while [ "$#" -gt 0 ]; do vecho "${BOLD}Verbose mode is enabled.${NORMAL}" ;; help|-h|--help) - usage 0 + usage main 0 ;; start|stop|restart|reload) exec /etc/init.d/network $arg @@ -259,6 +342,9 @@ while [ "$#" -gt 0 ]; do port_show $@ _exit $? ;; + help) + usage port 0 + ;; esac ;; zone|zo|z) @@ -285,10 +371,12 @@ while [ "$#" -gt 0 ]; do port_del $@ _exit $? ;; + help) + usage zone 0 + ;; esac ;; - *) - usage - ;; esac done + +usage main