From: Michael Tremer Date: Sat, 29 Aug 2009 16:34:18 +0000 (+0200) Subject: network: Some more code improvements. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ff69702e4ade5e63d88b6ea50573b17e9f533ef;p=ipfire-3.x.git network: Some more code improvements. --- diff --git a/src/network/functions b/src/network/functions index 6b78d2214..33b85a744 100644 --- a/src/network/functions +++ b/src/network/functions @@ -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 diff --git a/src/network/functions.ppp b/src/network/functions.ppp index 4ab0cda53..7598f0ba0 100644 --- a/src/network/functions.ppp +++ b/src/network/functions.ppp @@ -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 +} diff --git a/src/network/hooks/ipv4-dhcp b/src/network/hooks/ipv4-dhcp index 829182f62..43c44199a 100755 --- a/src/network/hooks/ipv4-dhcp +++ b/src/network/hooks/ipv4-dhcp @@ -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} diff --git a/src/network/hooks/ipv4-static b/src/network/hooks/ipv4-static index 7db442884..34af5572f 100755 --- a/src/network/hooks/ipv4-static +++ b/src/network/hooks/ipv4-static @@ -155,6 +155,10 @@ EOF rem) ;; + + discover) + exit ${EXIT_ERROR} + ;; *) echo "Usage: ${0} {config|pre-up|post-up|pre-down|post-down|status} [interface]" diff --git a/src/network/hooks/ipv4-static-route b/src/network/hooks/ipv4-static-route index f4515fe1e..9e389864e 100755 --- a/src/network/hooks/ipv4-static-route +++ b/src/network/hooks/ipv4-static-route @@ -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 diff --git a/src/network/hooks/mtu b/src/network/hooks/mtu index f22e79f67..707345a6d 100755 --- a/src/network/hooks/mtu +++ b/src/network/hooks/mtu @@ -71,6 +71,10 @@ MTU="${MTU}" EOF exit $? ;; + + discover) + exit ${EXIT_ERROR} + ;; *) usage diff --git a/src/network/hooks/pppoe b/src/network/hooks/pppoe index a1b9b1821..d27a51778 100755 --- a/src/network/hooks/pppoe +++ b/src/network/hooks/pppoe @@ -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 <${PPP_RUN}/${NAME}/options + cat <${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=$(${CONFIG_ZONES}/${zone}/${HOOK_NAME}_${NAME} + UUID=$(uuid) + cat <${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 $(&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 ;; diff --git a/src/network/hooks/pppoe.helper b/src/network/hooks/pppoe.helper index be89fedd1..693ba3de9 100755 --- a/src/network/hooks/pppoe.helper +++ b/src/network/hooks/pppoe.helper @@ -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 diff --git a/src/network/hooks/stp b/src/network/hooks/stp index 503c4e9e8..9e1c465e0 100755 --- a/src/network/hooks/stp +++ b/src/network/hooks/stp @@ -12,42 +12,11 @@ # ######################################################################## -. /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]" diff --git a/src/network/hooks/vlan b/src/network/hooks/vlan index 2211aee18..e4d99e64e 100755 --- a/src/network/hooks/vlan +++ b/src/network/hooks/vlan @@ -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} ;; diff --git a/src/network/network b/src/network/network index 38ca3ba43..bfdccb9ba 100644 --- a/src/network/network +++ b/src/network/network @@ -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 $(