File: staticroutes

package info (click to toggle)
ifupdown-scripts-zg2 0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 500 kB
  • ctags: 76
  • sloc: sh: 3,606; makefile: 35
file content (110 lines) | stat: -rwxr-xr-x 3,678 bytes parent folder | download
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
#!/bin/bash

# IFACE      = Logical interface name
# MODE       = start | stop
# METHOD     = manual, otherwise exit!
# IF_ROUTEn  = static routes (network/prefix gateway)
# IF_DEVICE  = device to be used

. /etc/network/ifupdown-scripts-zg2.d/common-functions

case "$MODE" in
  start)
    # adds static routes given in config file
    for R in ${!IF_ROUTE*}; do
      eval S=\$$R
      verbose "route entry $S"

      # a "route_foo" statement in the config file can use two different
      # forms of syntax:
      
      # route_foo prefix gateway/device metric/table
      # route_foo ip route <cmd line> and
      
      # if the string after "route_foo" in the config file does not start
      # with "ip route", the following words are interpreted as
      # prefix         - prefix for the route, used as "to" parameter.
      # gateway/device - if IP address, gateway for the route,
      #                                 used as "via" parameter,
      #                  otherwise,     device for the route,
      #                                 used as "dev" parameter.
      # metric/table   - if numeric,    metric for the route,
      #					used as "metric" parameter.
      #                  otherwise,     table for the route,
      #					used as "route" parameter.
      # From these values, appropriate "ip route" command lines are
      # constructed for interface startup and shutdown. This should cover
      # almost all non-exotic cases.

      # if the string after "route_foo" in the config file starts with
      # "ip route", the remainder of the string is taken as a full ip route
      # command line which will be appended to "ip route add" on interface
      # startup and to "ip route del" on interface shutdown.

      ROUTEPARM=""
      if [[ "$S" = "ip route"* ]]; then
        ROUTEPARM="${S#ip route }"
	verbose "explicit ip command ip route $ROUTEPARM"
      else
	# parse prefix gateway/device metric/table to variables and build
	# ROUTEPARM
	
	# take each word in $S and prefix it with the first word of PARAMS.
	# then cut each word from PARAMS.
	# concatenate each of the resulting strings to a valid ip command line.
	
	PARAMS="to gatewaydevice metrictable END"
	for word in $S; do
	  if [ "$PARAMS" = "END" ]; then
	    abort "too many parameters in static route $S"
	  fi
	  PARAM="${PARAMS%% *}"
	  verbose "parse PARAM $PARAM word $word"
	  case "${PARAMS%% *}" in
	    gatewaydevice)
	      verbose "parse word $word"
	      if echo $word | grep --quiet '^\([0-9a-f:\.]\+\)\(%\([a-z0-9]\+\)\)\?$'; then
	        GATEWAY="$(echo $word | sed 's/^\([0-9a-f:\.]\+\)\(%\([a-z0-9]\+\)\)\?$/\1/')"
	        ROUTEPARM="$ROUTEPARM via $GATEWAY"
	        DEVICE="$(echo $word | sed 's/^\([0-9a-f:\.]\+\)\(%\([a-z0-9]\+\)\)\?$/\3/')"
		verbose "parse GATEWAY $GATEWAY"
		verbose "parse DEVICE $DEVICE"
		if [ -n "$DEVICE" ]; then
	          ROUTEPARM="$ROUTEPARM dev $DEVICE"
		fi
	      else
	        ROUTEPARM="$ROUTEPARM dev $word"
	      fi
	      ;;
	    metrictable)
	      if echo $word | grep --quiet '^[0-9]\+$'; then
	        ROUTEPARM="$ROUTEPARM metric $word"
	      else
	        ROUTEPARM="$ROUTEPARM table $word"
	      fi
	      ;;
	    *)
	      ROUTEPARM="$ROUTEPARM ${PARAMS%% *} $word"
	      ;;
	  esac
	  PARAMS="${PARAMS#* }"
	done
	if [ "${PARAMS%% *}" = "dest" ]; then
	  # no destination given, take interface instead
	  ROUTEPARM="$ROUTEPARM dev $IF_DEVICE"
	fi
	verbose "constructed ip command ip route add/del $ROUTEPARM"
      fi

      cmd "ip route add $ROUTEPARM"
      add_down "routes" "route del $ROUTEPARM"
    done
    ;;
  stop)
    exec_down "routes" "ip"
    ;;
  *)
    ;;
esac

# end of file