]> git.ipfire.org Git - ipfire-3.x.git/commitdiff
network: Some more code improvements.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 29 Aug 2009 16:34:18 +0000 (18:34 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 29 Aug 2009 16:34:18 +0000 (18:34 +0200)
12 files changed:
src/network/functions
src/network/functions.ppp
src/network/hooks/ipv4-dhcp
src/network/hooks/ipv4-static
src/network/hooks/ipv4-static-route
src/network/hooks/mtu
src/network/hooks/pppoe
src/network/hooks/pppoe.helper
src/network/hooks/stp
src/network/hooks/vlan
src/network/network
src/network/ppp/ip-updown

index 6b78d2214e9f24b82dfedc96293656f608ccf6f8..33b85a744ef365065fec73083fc9159724b15b9e 100644 (file)
@@ -175,6 +175,10 @@ function device_is_bonded() {
        return 1
 }
 
+function device_is_bridge() {
+       [ -d "/sys/class/net/${1}/bridge" ]
+}
+
 function device_is_up() {
        ip link show $(devicify ${1}) 2>/dev/null | grep -qE "<.*UP.*>"
 }
@@ -186,6 +190,32 @@ function device_is_vlan() {
        grep -q "^${1}" /proc/net/vlan/config
 }
 
+function device_is_ppp() {
+       # XXX need something better
+       [ "${1:0:3}" = "ppp" ]
+}
+
+function device_is_real() {
+       local device=${1}
+
+       [ "${device}" = "lo" ] && \
+               return ${EXIT_ERROR}
+
+       device_is_bonding ${device} &&  \
+               return ${EXIT_ERROR}
+
+       device_is_bridge ${device} && \
+               return ${EXIT_ERROR}
+
+       device_is_ppp ${device} && \
+               return ${EXIT_ERROR}
+
+       device_is_vlan ${device} && \
+               return ${EXIT_ERROR}
+
+       return ${EXIT_OK}
+}
+
 function device_has_vlans() {
        if [ ! -e "/proc/net/vlan/config" ]; then
                return 1
@@ -258,6 +288,24 @@ function zone_is_up() {
        zone_exists $@ && device_is_up $@
 }
 
+function zone_is_forwarding() {
+       local seconds=45
+       local zone=${1}
+       
+       local device
+       while [ ${seconds} -gt 0 ]; do
+               for device in /sys/class/net/${zone}/brif/*; do
+                       [ -e "${device}/state" ] || continue
+                       if [ "$(<${device}/state)" = "3" ]; then
+                               return ${EXIT_OK}
+                       fi
+               done
+               sleep 1
+               seconds=$((${seconds} - 1))
+       done
+       return ${EXIT_ERROR}
+}
+
 function bridge_devices() {
        local bridge=$1
        [ -z "${bridge}" ] && return 2
@@ -364,6 +412,21 @@ function hook_type() {
        )
 }
 
+function hook_list() {
+       local type=${1}
+       local hook
+       for hook in ${HOOKS_DIR}/*; do
+               hook=${hook##*/}
+               
+               [[ ${hook} =~ helper$ ]] && continue
+               
+               if [ -n "${type}" ] && [ "$(hook_type ${hook})" != "${type}" ]; then
+                       continue
+               fi
+               echo "${hook}"
+       done
+}
+
 function config_get_hook() {
        local config=${1}
        if [ ! -e "${config}" ]; then
index 4ab0cda5313782a5e2dcc2e69639c52fe28f3f39..7598f0ba05ca9e88391846b68c1656b9f721b25e 100644 (file)
@@ -19,7 +19,7 @@
 #                                                                             #
 ###############################################################################
 
-PPP_RUN=/var/run/ppp
+RED_RUN=/var/run/network/red
 PPP_SECRETS=/etc/ppp/secrets
 
 function ppp_pre_up() {
@@ -79,3 +79,32 @@ INSERT INTO connections(date, duration, rcvd, sent) VALUES('$(date -u '+%s')', '
 EOF
 }
 
+function ppp_linkname_get() {
+       local config=${1}
+       (
+               . ${config}
+               echo "${LINKNAME}"
+       )
+}
+
+function red_defaultroute_update() {
+       local command="ip route replace default"
+
+       for uplink in ${RED_RUN}/*; do
+               [ -d "${uplink}" ] || continue
+
+               # Skip if no gateway given
+               [ -e "${uplink}/gateway" ] || continue
+
+               command="${command} nexthop via $(<${uplink}/gateway)"
+               if [ -e "${uplink}/weight" ]; then
+                       command="${command} weight $(<${uplink}/weight)"
+               fi
+       done
+       $command
+       ip route flush cache
+}
+
+function red_dns_update() {
+       : # XXX todo
+}
index 829182f6262a3b6eebeb7ebe5951ee97de832456..43c44199a0dae03b5bfece18916d341c657a87d2 100755 (executable)
@@ -70,7 +70,11 @@ EOF
 
        rem)
                ;;
-       
+
+       discover)
+               exit ${EXIT_ERROR}
+               ;;
+
        *)
                echo "Usage: ${0} {config|pre-up|post-up|pre-down|post-down|status} [interface]"
                exit ${EXIT_ERROR}
index 7db442884c2398ce2392910a5fa57585b6dfbd13..34af5572f5fdd347be83ed8af114ba46fc73b87b 100755 (executable)
@@ -155,6 +155,10 @@ EOF
 
        rem)
                ;;
+       
+       discover)
+               exit ${EXIT_ERROR}
+               ;;
 
        *)
                echo "Usage: ${0} {config|pre-up|post-up|pre-down|post-down|status} [interface]"
index f4515fe1e4fcd143252abb9a4d42a1763aca0b04..9e389864e0824c905a6c5e301adf3716d24568bd 100755 (executable)
@@ -12,8 +12,7 @@
 #
 ########################################################################
 
-. /etc/init/functions
-. /lib/network/functions
+. /lib/network/hook-header
 
 HOOK_NAME=ipv4-static-route
 HOOK_TYPE=zone
@@ -72,32 +71,8 @@ function check_config() {
        fi
 }
 
-# First, parse the command line
-
-while [ $# -gt 0 ]; do
-       case "${1}" in
-               --zone=*)
-                       zone=${1#--zone=}
-                       ;;
-               --config=*)
-                       . ${1#--config=}
-                       check_config
-                       ;;
-               -*)
-                       log_failure_msg "Unrecognized option: ${1}"
-                       exit ${EXIT_ERROR}
-                       ;;
-               *)
-                       action=${1}
-                       shift
-                       break
-                       ;;
-       esac
-       shift
-done
-
 case "${action}" in
-       config)
+       add)
                while [ $# -gt 0 ]; do
                        case "${1}" in
                                --ip=*)
@@ -152,6 +127,11 @@ EOF
        post-down)
                ;;
        
+       discover)
+
+               exit ${EXIT_ERROR}
+               ;;
+       
        *)
                echo "Usage: ${0} [interface] {up|down}"
                exit 1
index f22e79f678c86c8b38a107ba7ab3c0e959c2bb40..707345a6d795af7da44585cfd2f7765ffa3c56d7 100755 (executable)
@@ -71,6 +71,10 @@ MTU="${MTU}"
 EOF
                exit $?
                ;;
+       
+       discover)
+               exit ${EXIT_ERROR}
+               ;;
 
        *)
                usage
index a1b9b182154a85ab7fc02fa4f0e7e80b150bb67c..d27a51778f2c860c82f6a2ae5501a586ee2be875 100755 (executable)
@@ -31,12 +31,15 @@ case "${action}" in
                ;;
 
        status)
-               #check_config
-               # XXX Is there a better way?
-               if (ip route get ${IP} | grep -q ^local); then
+               echo -e "#  ${CLR_BOLD_CYN}PPPoE: ${NAME}${NORMAL}"
+               echo -n "#    pppd's PID: "
+               pid=$(head -n1 /var/run/ppp-${NAME}.pid 2>/dev/null)
+               if [ -n "${pid}" ] && [ -d "/proc/${pid}" ]; then
+                       echo -e "${CLR_BOLD_GRN}${pid}${NORMAL}"
                        exit ${EXIT_OK}
                else
-                       exit ${EXIT_ERROR}
+                       echo -e "${CLR_BOLD_RED}${pid-off}${NORMAL}"
+                       exit ${EXIT_OK}
                fi
                ;;
 
@@ -45,11 +48,11 @@ case "${action}" in
 
                check_config NAME
                # Creating necessary files
-               [ -d "${PPP_RUN}/${NAME}" ] || mkdir -p ${PPP_RUN}/${NAME}
+               [ -d "${RED_RUN}/${NAME}" ] || mkdir -p ${RED_RUN}/${NAME}
 
                ppp_secret "${USER}" "${SECRET}"
 
-               cat <<EOF >${PPP_RUN}/${NAME}/options
+               cat <<EOF >${RED_RUN}/${NAME}/options
 # Naming options
 name ${NAME}
 linkname ${NAME}
@@ -81,9 +84,14 @@ EOF
        post-up)
                check_config zone NAME
                MESSAGE="Starting PPP Daemon on interface ${zone}..."
-               pppd file ${PPP_RUN}/${NAME}/options >/dev/null
-               evaluate_retval
-               
+               if zone_is_forwarding ${zone}; then
+                       pppd file ${RED_RUN}/${NAME}/options >/dev/null
+                       evaluate_retval
+               else
+                       log_failure_msg "Zone ${zone} is not forwaring any traffic..."
+                       exit ${EXIT_ERROR}
+               fi
+
                ppp_post_up
                ;;
 
@@ -91,7 +99,7 @@ EOF
                ppp_pre_down
 
                MESSAGE="Stopping PPP Daemon on interface ${zone}..."
-               pid=$(head -n1 /var/run/ppp-${NAME}.pid)
+               pid=$(head -n1 /var/run/ppp-${NAME}.pid 2>/dev/null)
                if [ -n "${pid}" ]; then
                        kill ${pid} &>/dev/null
                        evaluate_retval
@@ -102,7 +110,7 @@ EOF
                ppp_post_down
                ;;
 
-       config)
+       add)
                # A pregenerated connection name
                NAME=$(</proc/sys/kernel/random/uuid)
                DEFAULTROUTE=1
@@ -140,8 +148,8 @@ EOF
                        shift
                done
 
-               check_config zone NAME USER
-               cat <<EOF >${CONFIG_ZONES}/${zone}/${HOOK_NAME}_${NAME}
+               UUID=$(uuid)
+               cat <<EOF >${CONFIG_UUIDS}/${UUID}
 HOOK="${HOOK_NAME}"
 USER="${USER}"
 SECRET="${SECRET}"
@@ -152,8 +160,8 @@ PEERDNS="${PEERDNS}"
 AUTH="${AUTH}"
 EOF
 
-               ln -sf ${CONFIG_ZONES}/${zone}/${HOOK_NAME}_${NAME} \
-                       ${CONFIG_PPP}/${NAME}
+               ln -sf ${CONFIG_UUIDS}/${UUID} \
+                       ${CONFIG_ZONES}/${zone}/${HOOK_NAME}-${UUID}
 
                exit ${EXIT_OK}
                ;;
@@ -162,9 +170,14 @@ EOF
                output=$(pppoe-discovery -I ${zone} \
                        -U $(</proc/sys/kernel/random/uuid) 2>&1)
                if grep -q "Timeout" <<<${output}; then
+                       echo "${HOOK_NAME}: FAILED"
                        exit ${EXIT_ERROR}
                else
-                       echo "${output}"
+                       echo "${HOOK_NAME}: OK"
+                       echo "${output}" | while read line; do
+                               [ "${line:0:1}" = "A" ] || continue
+                               echo "${HOOK_NAME}: ${line}"
+                       done
                        exit ${EXIT_OK}
                fi
                ;;
index be89fedd13db8f1cb8139aa100a3b8e2d9bbda5a..693ba3de930408b5e670722d12bbb65c73561c7d 100755 (executable)
@@ -18,8 +18,15 @@ done
 
 zone=${DEVICE}
 
+DIR=${RED_RUN}/${LINKNAME}
+
 case "${action}" in
        ip-up)
+               mkdir -p ${DIR} 2>/dev/null
+
+               echo "${IPREMOTE}" > ${DIR}/remote-ip-address
+               echo "${IPLOCAL}"  > ${DIR}/local-ip-address
+
                # Update firewall with new IP address(es)
 
                # Prepare main routing table
@@ -29,18 +36,20 @@ case "${action}" in
                ip route add table ${zone} default via ${IPREMOTE} dev ${IFNAME}
 
                if [ "${DEFAULTROUTE}" = "1" ]; then
-                       : # Set default route
-               fi
+                       ln -sf remote-ip-address ${DIR}/gateway
+                       [ -n "${WEIGHT}" ] && \
+                               echo "${WEIGHT}" > ${DIR}/weight
 
-               ip route flush cache
+                       red_defaultroute_update
+               fi
 
                if [ "${PEERDNS}" = "1" ]; then
-                       : # $DNS1 --> pdns server
+                       echo "${DNS1}" > ${DIR}/dns
+                       if [ -n "${DNS2}" ] && [ "${DNS1}" != "${DNS2}" ]; then
+                               echo "${DNS2}" > ${DIR}/dns
+                       fi
+                       red_dns_update
                fi
-
-               connection --up --zone=${zone} --name=${NAME} \
-                       --iplocal=${IPLOCAL} --ipremote=${IPREMOTE} --dns="${DNS1} ${DNS2}" \
-                       --weight=${WEIGHT} --pid=${PPPD_PID}
                ;;
 
        ip-down)
@@ -58,8 +67,6 @@ case "${action}" in
 
                # Save statistics
                ppp_stat "${NAME}" "${CONNECT_TIME}" "${BYTES_RCVD}" "${BYTES_SENT}"
-
-               connection --down --zone=${zone}
                ;;
 esac
 
index 503c4e9e8ed991de54e2818cd835d25dbfe72c3a..9e1c465e0bc2959eb3ff566b990ef996ba730044 100755 (executable)
 #
 ########################################################################
 
-. /etc/init/functions
-. /lib/network/functions
+. /lib/network/hook-header
 
 HOOK_NAME=stp
 HOOK_TYPE=zone
 
-function check_config() {
-       : # XXX TODO
-}
-
-# First, parse the command line
-
-while [ $# -gt 0 ]; do
-       case "${1}" in
-               --zone=*)
-                       zone=${1#--zone=}
-                       ;;
-               --config=*)
-                       . ${1#--config=}
-                       check_config
-                       ;;
-               -*)
-                       log_failure_msg "Unrecognized option: ${1}"
-                       exit ${EXIT_ERROR}
-                       ;;
-               *)
-                       action=${1}
-                       shift
-                       break
-                       ;;
-       esac
-       shift
-done
-
-# Second, do the $action
-
 case "${action}" in
        help)
                ;;
@@ -114,6 +83,10 @@ EOF
 
        rem)
                ;;
+       
+       discover)
+               exit ${EXIT_ERROR}
+               ;;
 
        *)
                echo "Usage: ${0} {pre-up|post-up|pre-down|post-down|config} [interface]"
index 2211aee18d13ec1067defaeb4d4a2b8d65e58824..e4d99e64e1fdd487d86791977e47e8fe61ce22c2 100755 (executable)
@@ -43,13 +43,15 @@ case "${action}" in
                        vconfig add $(devicify ${MAC}) ${ID} >/dev/null
                        evaluate_retval
 
+                       device_rename $(get_device_by_mac_and_vid ${MAC} ${ID}) $(port_name)
+                       ip link set $(port_name) up
+
                        ebtables -t broute -A BROUTING -p 802_1Q --vlan-id=${ID} -j DROP
                fi
                ;;
 
        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
                ;;
@@ -95,13 +97,17 @@ EOF
                ;;
 
        status)
-               device_is_up $(port_name)
-               RET=$?
-               if [ $RET -eq 0 ]; then
-                       log_success_msg "Port $(port_name) is up"
+               echo -e "#  ${CLR_BOLD_CYN}VLAN port $(port_name)${NORMAL}"
+               echo -n "#    State: "
+               if device_is_up $(port_name); then
+                       echo -e "${CLR_BOLD_GRN}up${NORMAL}"
+                       RET=${EXIT_OK}
                else
-                       log_failure_msg "Port $(port_name) is down"
+                       echo -e "${CLR_BOLD_RED}down${NORMAL}"
+                       RET=${EXIT_ERROR}
                fi
+               echo "#    ID   : ${ID}"
+               echo "#"
                exit ${RET}
                ;;
 
index 38ca3ba43ddd614a1f737cb86ae05a47d404233a..bfdccb9ba219017020921f695166a30fc363243e 100644 (file)
@@ -222,17 +222,59 @@ function cmd() {
        fi
 }
 
+function size() {
+       local size=${1}
+
+       local units
+       units[0]="Bytes "
+       units[1]="kBytes"
+       units[2]="MBytes"
+       units[3]="GBytes"
+       units[4]="TBytes"
+       
+       local count=${#units}
+       while [ ${count} -gt 0 ]; do
+               if [ ${size} -lt 1024 ]; then
+                       break
+               fi
+               size=$((${size} / 1024))
+               count=$((${count} - 1))
+       done
+       printf "%4d %s\n" "${size}" "${units[$((${#units} - ${count}))]}"
+}
+
 function port_show() {
-       local port
+       local port=$(devicify $1)
+
+       echo    "##################################################"
+       echo    "#"
+       echo -e "# Port ${CLR_BOLD_BLU}${port}${NORMAL}"
+       echo    "# ------------------------------------------------"
 
-       port=$(devicify $1)
+       echo -n "# State: "
+       if device_is_up ${port}; then
+               echo -e "${CLR_BOLD_GRN}up${NORMAL}"
+       else
+               echo -e "${CLR_BOLD_RED}down${NORMAL}"
+       fi
 
-       if ! port_exists ${port}; then
-               error "Port ${BOLD}${port}${NORMAL} does not exist."
-               return 1
+       echo -n "# Link : "
+       if device_has_carrier ${port}; then
+               echo -e "${CLR_BOLD_GRN}yes${NORMAL}"
+       else
+               echo -e "${CLR_BOLD_RED}no${NORMAL}"
        fi
 
-       ip -s link show $port
+       if device_is_up ${port}; then
+               echo "#"
+               echo "# Statistics:"
+               echo -n "#   RX: $(size $(</sys/class/net/${port}/statistics/rx_bytes))"
+               echo    " ($(</sys/class/net/${port}/statistics/rx_packets) packets)"
+               echo -n "#   TX: $(size $(</sys/class/net/${port}/statistics/tx_bytes))"
+               echo    " ($(</sys/class/net/${port}/statistics/tx_packets) packets)"
+       fi
+
+       echo "#"
 }
 
 function port_add() {
@@ -294,6 +336,14 @@ function port_del() {
        fi
 }
 
+function zone_discover() {
+       local zone=${1}
+
+       for hook in $(hook_list zone); do
+               hook_run ${hook} --zone=${zone} discover
+       done
+}
+
 function zone_show() {
        local zone
        zone=$1
@@ -468,6 +518,10 @@ while [ "$#" -gt 0 ]; do
                                        port_del $@
                                        _exit --reload $?
                                        ;;
+                               discover)
+                                       zone_discover $@
+                                       _exit $?
+                                       ;;
                                help)
                                        usage zone 0
                                        ;;
@@ -485,6 +539,18 @@ while [ "$#" -gt 0 ]; do
                                        ;;
                        esac
                        ;;
+               show)
+                       case "${1}" in
+                               ports)
+                                       for port in /sys/class/net/*; do
+                                               port=${port##*/}
+                                               device_is_real ${port} || continue
+                                               port_show ${port}
+                                       done
+                                       _exit 0
+                                       ;;
+                       esac
+                       ;;
                -*)
                        error "Option \"$arg\" is not known."
                        ;;
index 067a3081d76d4b3af9fc66e8b46d239b8326d1cf..24f60d3e0ca45515bc93bf24c756e48f717df92f 100644 (file)
@@ -23,13 +23,18 @@ umask 022
 export PATH=/usr/sbin:/sbin:/usr/bin:/bin
 
 . /lib/network/functions
+. /lib/network/functions.ppp
 
-if [ -e "${CONFIG_PPP}/${LINKNAME}" ]; then
-       . ${CONFIG_PPP}/${LINKNAME}
-fi
+for config in ${CONFIG_ZONES}/${DEVICE}/*; do
+       if [ "$(ppp_linkname_get ${config})" = "${LINKNAME}" ]; then
+               CONFIG=${config}
+               . ${CONFIG}
+               break
+       fi
+done
 
 if [ -n "${HOOK}" ] && [ -x "${HOOKS_DIR}/${HOOK}.helper" ]; then
-       exec ${HOOKS_DIR}/${HOOK}.helper --config=${CONFIG_PPP}/${LINKNAME} \
+       exec ${HOOKS_DIR}/${HOOK}.helper --config=${CONFIG} \
                $(basename $0) $@
 fi