2
# Script to add global static routes to the system
6
# Provides: networking-routes
7
# Required-Start: $network $local_fs
8
# Required-Stop: $local_fs
11
# Short-Description: Establish global networking routes for the system
12
# Description: Define global network routes for the system using
13
# the configuration defined in /etc/network/routes
14
# Global routes can be either routes for which
15
# the associated interface cannot be determined
16
# beforehand (maybe the device name is not known)
17
# or 'reject' routes to prevent the system from
18
# communicating with remote networks.
21
# Copyright (c) 2011 Javier Fernandez-Sanguino <jfs@debian.org>
23
# Some portions (specifically the code of the run_route() function) are derived
24
# from the ifup-route script in SuSE's sysconfig package.
27
# - Copyright (c) 2002 SuSE Linux AG Nuernberg, Germany. All rights reserved.
28
# - Author: Christian Zoz <zoz@suse.de>, 2002
29
# - Based on rcroute: Burchard Steinbild <bs@suse.de>, 1996
30
# Werner Fink <werner@suse.de>, 1996-2000
33
# This program is free software; you can redistribute it and/or modify it under
34
# the terms of the GNU General Public License as published by the Free Software
35
# Foundation; either version 2 of the License, or (at your option) any later
38
# This program is distributed in the hope that it will be useful, but WITHOUT
39
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
43
# You should have received a copy of the GNU General Public License along with
44
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
45
# Place, Suite 330, Boston, MA 02111-1307 USA
48
[ -x /sbin/route ] || exit 0
49
ROUTEFILE="/etc/network/routes"
50
# Abort (without error) if the configuration file does not exist
51
[ ! -r "$ROUTEFILE" ] && exit 0
53
. /lib/lsb/init-functions
55
VERBOSITY=${VERBOSITY:-0}
58
# Functions to read the route file and process it
62
local COMMAND="route $*"
63
export LC_MESSAGES=C # We need the return messages to be in english
64
RETMESSAGE="$($COMMAND 2>&1)"
66
if test $RETVALUE -ne 0 ; then
67
[ "$VERBOSITY" -eq 1 ] && echo "DEBUG: calling: '$COMMAND' FAILED"
68
# Process the messages and omits those that are not
71
# Omit 'File exists' since the route is already there..
72
*File*exists) return ;;
73
# 'No such process' is only omitted if the route is being
74
# deleted. If the route is being created, this error message
75
# might appear if the gateway is not reachable.
76
*No*such*process) [ "$1" = "del" ] && return ;;
79
log_failure_msg "Error while executing:" \
80
" Command '$COMMAND' returned: ${RETMESSAGE%%Usage:*}"\
81
" Configuration line: $LINE"
83
[ "$VERBOSITY" -eq 1 ] && echo "DEBUG: calling: '$COMMAND' SUCCEEDED"
92
cat $ROUTEFILE | egrep "^[^#].*any$" |
93
while read network netmask gateway interface ; do
94
if [ -n "$interface" ] && [ -n "$network" ] && [ -n "$netmask" ] && [ -n "$gateway" ] ; then
95
if [ "$gateway" != "reject" ] ; then
96
[ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Deleting global route for $network / $netmask through gateway $gateway"
97
run_route del -net $network netmask $netmask gw $gateway
98
[ $? -ne 0 ] && ret=$?
100
[ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Deleting reject route for $network / $netmask"
101
run_route del -net $network netmask $netmask reject
102
[ $? -ne 0 ] && ret=$?
106
echo "ERROR: Incorrect line for global network routes in $ROUTEFILE: '$network $netmask $gateway $interface'"
113
add_global_routes() {
115
cat $ROUTEFILE | egrep "^[^#].*any$" |
116
while read network netmask gateway interface ; do
117
if [ -n "$interface" ] && [ -n "$network" ] && [ -n "$netmask" ] && [ -n "$gateway" ] ; then
118
if [ "$gateway" != "reject" ] ; then
119
[ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Adding global route for $network / $netmask through gateway $gateway"
120
run_route add -net $network netmask $netmask gw $gateway
121
[ $? -ne 0 ] && ret=$?
123
[ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Adding global reject route for $network / $netmask"
124
run_route add -net $network netmask $netmask reject
125
[ $? -ne 0 ] && ret=$?
129
echo "ERROR: Incorrect line for global network routes in $ROUTEFILE: '$network $netmask $gateway $interface'"
138
log_action_begin_msg "Configuring network routes"
139
if add_global_routes; then
140
log_action_end_msg $?
142
log_action_end_msg $?
147
log_action_begin_msg "Deconfiguring network routes"
148
if del_global_routes; then
149
log_action_end_msg $?
151
log_action_end_msg $?
155
reload|force-reload|restart)
156
log_action_begin_msg "Reconfiguring network routes"
158
if add_global_routes; then
159
log_action_end_msg $?
161
log_action_end_msg $?
166
echo "Usage: /etc/init.d/networking-routes {start|stop|restart|reload}"