From: Michael Tremer Date: Wed, 25 May 2011 14:13:04 +0000 (+0000) Subject: routing: Create routing database and set routes for ipv4. X-Git-Tag: 001~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b816e04bb993b47b95139b6b0ef3d858cb661c15;p=network.git routing: Create routing database and set routes for ipv4. --- diff --git a/functions.constants b/functions.constants index 5de3f7e9..bd2a41e8 100644 --- a/functions.constants +++ b/functions.constants @@ -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" diff --git a/functions.routing b/functions.routing index fc4210f7..d9459267 100644 --- a/functions.routing +++ b/functions.routing @@ -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} +} diff --git a/functions.stp b/functions.stp index ba906bac..6502130f 100644 --- a/functions.stp +++ b/functions.stp @@ -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 } diff --git a/functions.util b/functions.util index 7ff20940..e77119b8 100644 --- a/functions.util +++ b/functions.util @@ -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 diff --git a/hooks/zones/bridge.configs/ipv4-static b/hooks/zones/bridge.configs/ipv4-static index b218cf23..b46864ef 100755 --- a/hooks/zones/bridge.configs/ipv4-static +++ b/hooks/zones/bridge.configs/ipv4-static @@ -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}