]> git.ipfire.org Git - network.git/commitdiff
routing: Create routing database and set routes for ipv4.
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 May 2011 14:13:04 +0000 (14:13 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 May 2011 14:13:04 +0000 (14:13 +0000)
functions.constants
functions.routing
functions.stp
functions.util
hooks/zones/bridge.configs/ipv4-static

index 5de3f7e9e766986d7318a1b8090e124c5abac861..bd2a41e8489c1a0a23d2e5a189ffacd18d9f907c 100644 (file)
@@ -37,6 +37,7 @@ CONFIG_FILE_PARAMS="COLOURS DEBUG SHELL TIMEOUT_RESTART"
 CONFIG_HOSTNAME="/etc/hostname"
 
 RED_DB_DIR=${RUN_DIR}/red
+ROUTING_DB_DIR=${RUN_DIR}/routing
 
 DB_CONNECTION_FILE="${LOG_DIR}/connections.db"
 
index fc4210f75e3c4e3daac8ceef90184b0dc957aee3..d94592673429af4db8d44c9cc1809dc103b8f07a 100644 (file)
@@ -24,24 +24,29 @@ function routing_has_default() {
 }
 
 function routing_default_update() {
-       local zone
        local routes
 
+       local zones=$(zones_get_nonlocal)
+       if [ -z "${zones}" ]; then
+               zones=$(zones_get_local)
+       fi
+
        local gateway
        local weight
+       local zone
 
-       log INFO "Updating default route."
+       local proto="ipv4"
 
-       for zone in $(zones_get_nonlocal); do
+       for zone in ${zones}; do
                # Skip if zone is not up
-               red_db_exists ${zone} || continue
+               routing_db_exists ${zone} ${proto} || continue
 
-               if [ "$(red_db_get ${zone} active)" = "1" ]; then
-                       gateway=$(red_db_get ${zone} remote-ip-address)
-                       weight=$(red_db_get ${zone} weight)
+               if [ "$(routing_db_get ${zone} ${proto} active)" = "1" ]; then
+                       gateway=$(routing_db_get ${zone} ${proto} remote-ip-address)
+                       weight=$(routing_db_get ${zone} ${proto} weight)
 
                        routes="${routes} nexthop via ${gateway}"
-                       
+
                        if [ -n "${weight}" ]; then
                                routes="${routes} weight ${weight}"
                        fi
@@ -50,15 +55,20 @@ function routing_default_update() {
                fi
        done
 
-       log INFO "Setting default route: ${routes}"
-
        if [ -z "${routes}" ]; then
+               log INFO "Removing default route."
+
                if routing_has_default; then
                        ip route del default
                fi
                return ${EXIT_OK}
        fi
 
+       # Remove too much spaces.
+       routes=$(echo ${routes})
+
+       log INFO "Setting default route: ${routes}"
+
        ip route replace default ${routes}
 }
 
@@ -71,11 +81,6 @@ function routing_table_exists() {
 function routing_table_create() {
        local zone=${1}
 
-       if ! zone_is_nonlocal ${zone}; then
-               error_log "Can only create routing tables for non-local zones."
-               return ${EXIT_ERROR}
-       fi
-
        if routing_table_exists ${zone}; then
                return ${EXIT_OK}
        fi
@@ -90,3 +95,100 @@ function routing_table_create() {
 function routing_table_remove() {
        : # XXX do we need this?
 }
+
+function routing_db_path() {
+       local zone=${1}
+       local proto=${2}
+
+       assert isset zone
+       assert isset proto
+       assert isoneof proto ${IP_SUPPORTED_PROTOCOLS}
+
+       echo "${ROUTING_DB_DIR}/${zone}/${proto}"
+}
+
+function routing_db_exists() {
+       [ -d "$(routing_db_path $@)" ]
+}
+
+function routing_db_create() {
+       routing_db_exists $@ && return ${EXIT_OK}
+
+       mkdir -p $(routing_db_path $@)
+}
+
+function routing_db_remove() {
+       rm -rf $(routing_db_path $@)
+}
+
+function routing_db_set() {
+       local zone=${1}
+       local proto=${2}
+       local parameter=${3}
+       shift 3
+
+       local value="$@"
+
+       log INFO "Updating database (${zone} - ${proto}): ${parameter} = ${value}"
+
+       routing_db_create ${zone} ${proto}
+
+       echo "${value}" > $(routing_db_path ${zone} ${proto})/${parameter}
+}
+
+function routing_db_get() {
+       local zone=${1}
+       local proto=${2}
+       local parameter=${3}
+       shift 3
+
+       cat $(routing_db_path ${zone} ${proto})/${parameter} 2>/dev/null
+}
+
+function routing_db_from_ppp() {
+       local zone=${1}
+       local proto=${2}
+
+       # Save ppp configuration
+       routing_db_set ${zone} ${proto} type "ppp"
+       routing_db_set ${zone} ${proto} local-ip-address ${PPP_IPLOCAL}
+       routing_db_set ${zone} ${proto} remote-ip-address ${PPP_IPREMOTE}
+
+       routing_db_set ${zone} ${proto} dns ${PPP_DNS1} ${PPP_DNS2}
+
+       routing_db_set ${zone} ${proto} remote-address ${PPP_MACREMOTE,,}
+}
+
+function routing_update() {
+       local zone=${1}
+
+       # Nothing to do for local zones.
+       if zone_is_local ${zone}; then
+               return ${EXIT_OK}
+       fi
+
+       local proto=${2}
+       local table=${zone}
+
+       # Create routing table if not exists
+       routing_table_create ${table}
+
+       log DEBUG "Flushing routing table ${table}"
+       cmd ip route flush table ${table}
+
+       local local_ip_address=$(routing_db_get ${zone} ${proto} local-ip-address)
+
+       # XXX does not work.
+       #log DEBUG "Adding route for subnet ${local_ip_address} to table ${table}"
+       #cmd ip route add table ${table} ${local_ip_address} dev ${zone}
+
+       local remote_ip_address=$(routing_db_get ${zone} ${proto} remote-ip-address)
+
+       if isset remote_ip_address; then
+               log DEBUG "Adding default route for table ${table}"
+
+               cmd ip route add table ${table} default nexthop via ${remote_ip_address}
+       fi
+
+       cmd ip rule add from ${local_ip_address} lookup ${table}
+}
index ba906baca98ac008dac5f67f8aaf55fddf9cda91..6502130f45f990128d22146002dd4a2856f48d8f 100644 (file)
@@ -88,7 +88,8 @@ function stp_enable() {
                        rstpctl setforcevers ${bridge} normal
                        ;;
                *)
-                       error_log "Unknown protocol version: ${mode}."
+                       log WARNING "Unknown protocol version: ${mode}."
+                       log WARNING "Using default mode."
                        ;;
        esac
 }
index 7ff209404ec614d2ee078723c4c7f96187a52d2f..e77119b8bf47d930206f72c6dc8a210aeebc1bdb 100644 (file)
@@ -161,7 +161,12 @@ function network_config_set() {
 }
 
 function network_config_read() {
+       # Save state of DEBUG and restore it later.
+       local debug=${DEBUG}
+
        config_read ${CONFIG_FILE}
+
+       DEBUG=${debug}
 }
 
 function network_config_write() {
@@ -308,7 +313,8 @@ function exec_cmd() {
 
        log DEBUG "Running command: ${cmd}"
 
-       ${SHELL} ${cmd}
+       DEBUG=${DEBUG} \
+               ${SHELL} ${cmd}
        local ret=$?
 
        #log DEBUG "Returned with code '${ret}'"
@@ -321,6 +327,19 @@ function exec_cmd() {
        return ${ret}
 }
 
+function cmd() {
+       local cmd=$@
+
+       log DEBUG "Running command: ${cmd}"
+
+       ${cmd}
+       local ret=$?
+
+       log DEBUG "Returned with code '${ret}'"
+
+       return ${ret}
+}
+
 function uppercase() {
        local input
        read input
index b218cf23bc0ad0094da0e9c794678ec813698b4d..b46864eff46198d46fbc8e62a28e6971a34b73aa 100755 (executable)
@@ -79,14 +79,14 @@ function _up() {
 
        ip_address_add ${zone} ${ADDRESS}/${PREFIX}
 
-       if zone_is_nonlocal ${zone} && [ -n "${GATEWAY}" ]; then
+       if [ -n "${GATEWAY}" ]; then
                # Save configuration
-               red_db_set ${zone} type "${HOOK}"
-               red_db_set ${zone} local-ip-address ${ADDRESS}/${PREFIX}
-               red_db_set ${zone} remote-ip-address ${GATEWAY}
-               
-               red_db_set ${zone} active 1
-               red_routing_update ${zone}
+               routing_db_set ${zone} ipv4 type "${HOOK}"
+               routing_db_set ${zone} ipv4 local-ip-address "${ADDRESS}/${PREFIX}"
+               routing_db_set ${zone} ipv4 remote-ip-address "${GATEWAY}"
+               routing_db_set ${zone} ipv4 active 1
+
+               routing_update ${zone} ipv4
        fi
 
        exit ${EXIT_OK}