File: static-routes

package info (click to toggle)
ifupdown-extra 0.34
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176 kB
  • sloc: sh: 1,064; makefile: 15
file content (122 lines) | stat: -rwxr-xr-x 5,168 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/sh
#
# Script to setup a system's static routes based on the
# /etc/network/routes definitions.
#
# It tries to simplify network route management and make it easier
# to handle those as requested in bug #368228 ('Wish: Better Handling 
# of up/down route commands'). With this script routes do not have to be 
# introduced in /etc/network/interfaces (in 'up' and 'down' commands).
#
# This file includes a list of routes for different networks and follows
# the format:
# Network Netmask Gateway Interface
# 
# Example:
# 172.1.1.0 255.255.255.0 192.168.0.1 eth0
#
# Install this script in /etc/network/if-up.d/ (to setup the routes) and in 
# /etc/network/if-pre-down.d/ (if you want to remove the routes before
# deconfiguring the interface)
#
#   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 2 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, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#  
# You can also find a copy of the GNU General Public License at
# http://www.gnu.org/licenses/licenses.html#TOCLGPL
#
#
# TODO:
#  - No action is taken if the routes already exist (when adding them) or when
#    they dont (if you are removing), if they do you will get an error in
#    stderr but the script will continue 
#    Note: If you add the up/down in /etc/network/interfaces failure when
#          setting up a route breaks the interface configuration

ROUTEFILE="/etc/network/routes"

# Abort (without error) if the configuration file does not exist
[ ! -r "$ROUTEFILE" ] && exit 0
# Default value
VERBOSITY=${VERBOSITY:-0}

[ "$IFACE" ] || {
    $OUTPUT "ERROR: Variable IFACE not set in environment"
    exit 1
}

# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=901612
if [ ${IFACE} = "--all" ]; then IFACE="[[:alnum:]]+"; fi

del_static_routes() {
	# NOTE: We actually don't have to remove routes if downing an interface
	# since they will be removed nevertheless. In any case, this 
	# piece of code only runs if you install this file in 
	# /etc/network/if-pre-down.d/ (which you don't need to)
	cat $ROUTEFILE | egrep "^[^#].*[[:space:]]${IFACE}[[:space:]]*$" | 
	while read network netmask gateway interface ; do
            if [ -n "$interface" ] && [ -n "$network" ] && [ -n "$netmask" ] && [ -n "$gateway" ] ; then
                if [ "$gateway" != "reject" ] ; then
                    [ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Deleting route for $network / $netmask through gateway $gateway at $interface"
                    route del -net $network netmask $netmask gw $gateway dev $interface
                else
                    [ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Deleting reject route for $network / $netmask when bringing up $interface"
                    route del -net $network netmask $netmask reject
                fi

            else
                echo "ERROR: Incorrect line for $IFACE in $ROUTEFILE: '$network $netmask $gateway $interface'"
            fi
	done
}

add_static_routes() {
	cat $ROUTEFILE | egrep "^[^#].*[[:space:]]${IFACE}[[:space:]]*$" | 
	while read network netmask gateway interface ; do
            if [ -n "$interface" ] && [ -n "$network" ] && [ -n "$netmask" ] && [ -n "$gateway" ] ; then
                if [ "$gateway" != "reject" ] && [ "$gateway" != "blackhole" ] ; then
		    [ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Adding route for $network / $netmask through gateway $gateway at $interface"
        	    route add -net $network netmask $netmask gw $gateway dev $interface
                else
                    [ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Adding reject/blackhole route for $network / $netmask when bringing up $interface"
                    ip route add blackhole  $network/$netmask 
                fi

            else
                echo "ERROR: Incorrect line for $IFACE in $ROUTEFILE: '$network $netmask $gateway $interface'"
            fi
	done
}

check_static_routes() {
	cat $ROUTEFILE | egrep "^[^#].*[[:space:]]${IFACE}[[:space:]]*$" | 
	while read network netmask gateway interface ; do
            if [ -n "$interface" ] && [ -n "$network" ] && [ -n "$netmask" ] && [ -n "$gateway" ] ; then
                if [ "$gateway" != "reject" ] ; then
                    if ! route -n | egrep -q "${network}\s+${gateway}\s+${netmask}.*${interface}"; then
                        echo "ERROR: Route '$network $netmask $gateway $interface' defined in $ROUTEFILE is not configured"
                    fi
                fi
            fi
        done
}

case "$MODE" in 
	start) add_static_routes ;;
	stop)  del_static_routes ;;
        status) check_static_routes ;;
	*)     ;;
esac

exit 0