I have modified
rc.inet1 and
rc.inet1.conf files to be able to add new static routes through rc.inet1.conf (maybe it will be useful). Any suggestions/modifications/comments would be highly appreciated (this is my first "serious" work in this field)
rc.inet1
Code:
# IMMEDIATELY AFTER GATEWAY FUNCTIONS
###########################
# STATIC ROUTES FUNCTIONS #
###########################
# set MAXROUTES variable - the maximum number of routes to search for
MAXROUTES=${MAXROUTES:-20}
# Function to create route.
route_up() {
# Determine position 'i' of this routee in the ROUTE array:
i=0
while [ $i -lt $MAXROUTES ]; do
[ "${ROUTE[$i]}" = "${1}" ] && break
i=$(($i+1))
done
case ${ROUTETYPE[$i]} in
'net')
# try to determine if mask was provided as simple number (e.g. 24) or as IP address (255.255.255.0)
# and set maskpart variable accordingly (to "/mask" or " netmask mask"
maskpart="/${ROUTEMASK[$i]}"
point=`echo "$maskpart" | grep "\."`
if [ "$point" == "$maskpart" ]; then
maskpart=" netmask ${ROUTEMASK[$i]}";
fi
if /sbin/route -n | grep "^${1}" 1> /dev/null ; then
echo "/etc/rc.d/rc.inet1: route $1 already exists." | $LOGGER ;
else
`/sbin/route add -net $1$maskpart gw ${ROUTEGATEWAY[$i]}`
fi
;;
'host')
if /sbin/route -n | grep "^${1}" 1> /dev/null ; then
echo "/etc/rc.d/rc.inet1: route $1 already exists." | $LOGGER ;
else
`/sbin/route add -host $1 gw ${ROUTEGATEWAY[$i]}`
fi
;;
*)
echo "/etc/rc.d/rc.inet1: Invalid route type (${ROUTETYPE[$i]}) for route $1; route not set" | $LOGGER
;;
esac
}
# Function to delete route.
route_down() {
# Determine position 'i' of this routee in the ROUTE array:
i=0
while [ $i -lt $MAXROUTES ]; do
[ "${ROUTE[$i]}" = "${1}" ] && break
i=$(($i+1))
done
case ${ROUTETYPE[$i]} in
'net')
maskpart="/${ROUTEMASK[$i]}"
point=`echo "$maskpart" | grep "\."`
if [ "$point" == "$maskpart" ]; then
maskpart=" netmask ${ROUTEMASK[$i]}";
fi
if /sbin/route -n | grep "^${1}" 1> /dev/null ; then
`/sbin/route del -net $1$maskpart gw ${ROUTEGATEWAY[$i]}`
else
echo "/etc/rc.d/rc.inet1: route $1 does not exist." | $LOGGER ;
fi
;;
'host')
if /sbin/route -n | grep "^${1}" 1> /dev/null ; then
`/sbin/route del -host $1 gw ${ROUTEGATEWAY[$i]}`;
else
echo "/etc/rc.d/rc.inet1: route $1 does not exist." | $LOGGER ;
fi
;;
*)
echo "/etc/rc.d/rc.inet1: Invalid route type (${ROUTETYPE[$i]}) for route $1; route not deleted" | $LOGGER
;;
esac
}
rc.inet1 - modifications to start(), stop() functions and main case switch
Code:
# Function to start the network:
start() {
lo_up
for i in ${IFNAME[@]} ; do
if_up $i
done
gateway_up
# create routes
for i in ${ROUTE[@]} ; do
route_up $i
done
}
# Function to stop the network:
stop() {
#delete routes
for i in ${ROUTE[@]} ; do
route_down $i
done
gateway_down
for i in ${IFNAME[@]} ; do
if_down $i
done
lo_down
}
############
### MAIN ###
############
case "$1" in
'start') # "start" brings up all configured interfaces:
start
;;
'stop') # "stop" takes down all configured interfaces:
stop
;;
'restart') # "restart" restarts the network:
stop
start
;;
'routes_start')
for i in ${ROUTE[@]} ; do
route_up $i
done
;;
'routes_stop')
for i in ${ROUTE[@]} ; do
route_down $i
done
;;
# ...
to create a 'static' route, it is necessary to add next code to
rc.inet1.conf:
rc.inet1.conf
Code:
# Static routes
# first network
ROUTE[0]="10.10.1.16"
ROUTETYPE[0]="net"
ROUTEMASK[0]="255.255.255.240"
ROUTEGATEWAY[0]="192.168.0.254"
# another network
ROUTE[1]="10.10.3.16"
ROUTETYPE[1]="net"
ROUTEMASK[1]="28"
ROUTEGATEWAY[1]="192.168.0.200"
# a host (without mask)
ROUTE[2]="10.10.4.25"
ROUTETYPE[2]="host"
ROUTEGATEWAY[2]="192.168.0.200"
Values in ROUTE[] should be unique. It is possible to extend script to support other route command options. And it can be supplied as a patch for rc.inet1, however I haven't tried to create it.
Another problem that I cannot solve for the moment is "disappearing" of the routes after waking up from the sleep state... Maybe anyone has suggestions for this?