+#!/bin/bash
+###############################################################################
+# #
+# IPFire.org - A linux based firewall #
+# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <http://www.gnu.org/licenses/>. #
+# #
+###############################################################################
+
+function routing_has_default() {
+ ip route | grep -q "^default"
+}
+
+function routing_default_update() {
+ local zone
+ local routes
+
+ local gateway
+ local weight
+
+ log INFO "Updating default route."
+
+ for zone in $(zones_get_nonlocal); do
+ # Skip if zone is not up
+ red_db_exists ${zone} || continue
+
+ if [ "$(red_db_get ${zone} active)" = "1" ]; then
+ gateway=$(red_db_get ${zone} remote-ip-address)
+ weight=$(red_db_get ${zone} weight)
+
+ routes="${routes} nexthop via ${gateway}"
+
+ if [ -n "${weight}" ]; then
+ routes="${routes} weight ${weight}"
+ fi
+ else
+ log DEBUG "Ignoring zone '${zone}' which is not active."
+ fi
+ done
+
+ if [ -z "${routes}" ]; then
+ if routing_has_default; then
+ ip route del default
+ fi
+ return ${EXIT_OK}
+ fi
+
+ ip route replace default ${routes}
+}
+
+function routing_table_exists() {
+ local zone=${1}
+
+ grep -q "${zone}$" < /etc/iproute2/rt_tables
+}
+
+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
+
+ log INFO "Creating routing table for zone '${zone}'"
+
+ local id=$(( ${zone#red} + 1 ))
+
+ echo "${id} ${zone}" >> /etc/iproute2/rt_tables
+}
+
+function routing_table_remove() {
+ : # XXX do we need this?
+}