From 65b4654000ef64d06a72f87542753ad16dbeb610 Mon Sep 17 00:00:00 2001
From: Felix Fietkau <nbd@openwrt.org>
Date: Fri, 2 Mar 2007 15:48:29 +0000
Subject: [PATCH] add support for static routes - based on the patch from #1365

SVN-Revision: 6457
---
 docs/network.tex                              | 18 +++++++++
 package/base-files/files/etc/functions.sh     |  5 ++-
 .../files/etc/hotplug.d/iface/10-routes       | 40 +++++++++++++++++++
 3 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 package/base-files/files/etc/hotplug.d/iface/10-routes

diff --git a/docs/network.tex b/docs/network.tex
index 41febef8af..7d811ba7f5 100644
--- a/docs/network.tex
+++ b/docs/network.tex
@@ -54,6 +54,24 @@ PPP based protocols (\texttt{pppoe}, \texttt{pptp}, ...) accept these options:
 
 For all protocol types, you can also specify the MTU by using the \texttt{mtu} option.
 
+\subsubsection{Setting up static routes}
+
+You can set up static routes for a specific interface that will be brought up 
+after the interface is configured.
+
+Simply add a config section like this:
+
+\begin{Verbatim}
+config route foo
+	option interface lan
+	option target 1.1.1.0
+	option netmask 255.255.255.0
+	option gateway 192.168.1.1
+\end{Verbatim}
+
+The name for the route section is optional, the \texttt{interface}, \texttt{target} and 
+\texttt{gateway} options are mandatory.
+Leaving out the \texttt{netmask} option will turn the route into a host route.
 
 \subsubsection{Setting up the switch (currently broadcom only)}
 
diff --git a/package/base-files/files/etc/functions.sh b/package/base-files/files/etc/functions.sh
index 931f4be472..1cb365d478 100755
--- a/package/base-files/files/etc/functions.sh
+++ b/package/base-files/files/etc/functions.sh
@@ -125,10 +125,13 @@ config_set() {
 
 config_foreach() {
 	local function="$1"
-	local section
+	local type="$2"
+	local section cfgtype
 	
 	[ -z "$CONFIG_SECTIONS" ] && return 0
 	for section in ${CONFIG_SECTIONS}; do
+		config_get cfgtype "$section" TYPE
+		[ -n "$type" -a "$cfgtype" != "$type" ] && continue
 		eval "$function \"\$section\""
 	done
 }
diff --git a/package/base-files/files/etc/hotplug.d/iface/10-routes b/package/base-files/files/etc/hotplug.d/iface/10-routes
new file mode 100644
index 0000000000..4b55f8bb1e
--- /dev/null
+++ b/package/base-files/files/etc/hotplug.d/iface/10-routes
@@ -0,0 +1,40 @@
+add_route() {
+	local config="$1"
+
+	# is this route intended for the
+	# $INTERFACE of this hotplug event
+	config_get interface "$config" interface
+	[ "$interface" != "$INTERFACE" ] && return 0
+	
+	# get the real interface name from network config
+	config_get dev "$interface" ifname
+
+	config_get target "$config" target
+	config_get netmask "$config" netmask
+	config_get gateway "$config" gateway
+	config_get metric "$config" metric
+
+	# make sure there is a gateway and a target
+	[ -n "$target" ] || {
+		echo "Missing target in route section $config"
+		return 1
+	}
+	[ -n "$gateway" ] || {
+		echo "Missing gateway in route section $config"
+		return 1
+	}
+
+	netmask="${netmask:-255.255.255.255}"
+	dest="${netmask:+-net "$target" netmask "$netmask"}"
+	dest="${dest:--host "$target"}"
+	
+	/sbin/route add $dest gw "$gateway" ${dev:+dev "$dev"} ${metric:+ metric "$metric"}
+}
+
+case "$ACTION" in
+	ifup)
+		include /lib/network
+		scan_interfaces
+		config_foreach "add_route" route
+	;;
+esac
-- 
GitLab