# Failover  floating static route



## tpfiler (Nov 19, 2021)

I was trying to figure this out using:








						Chapter 33. Advanced Networking
					

Advanced networking in FreeBSD:  basics of gateways and routes, CARP, how to configure multiple VLANs on FreeBSD, etc




					docs.freebsd.org
				








						ifconfig(8)
					






					www.freebsd.org
				








						routed(8)
					






					www.freebsd.org
				




But I did not seem to understand or figure out how to get this done in /etc/rc.conf

So I want to have a static route pointing to one gateway in day to day situations but I want another static route to take over in case the primary route path is unavailable. 

Usually I have done this with higher or lower metric added to static routes in cisco but I was hoping there was something similar in FreeBSD.

I understand up to the point on how to create a static route on rc.conf but adding metrics I could not find an example of it.

Does anyone know if this is capable? If so, may you kindly point to a resource that I can read that shows how it is done.

Thank you all for your time.

Best,
T


----------



## covacat (Nov 19, 2021)

afaik without a routing protocol/dynamic routing you can't
what you can do is ping your default gw from a script and if it fails issue a route change default
if it succeeds and you are on the secondary route change to the default gw

this works pretty well but does not cover the case when the remote gw is up and its uplink is down
to fix this you can add a route to known reliable host/ip (that you don't really use) thru your primary gateway (like 1.1.1.1)
and ping that instead your primary gw 

drawback is that if you want to access that host when the primary gw is down you wont be able to


----------



## SirDice (Nov 19, 2021)

tpfiler said:


> I understand up to the point on how to create a static route on rc.conf but adding metrics I could not find an example of it.


Metrics are useless on static routing. They only have a purpose with dynamic routing protocols like BGP or OSPF.


----------



## tpfiler (Nov 19, 2021)

covacat said:


> afaik without a routing protocol/dynamic routing you can't
> what you can do is ping your default gw from a script and if it fails issue a route change default
> if it succeeds and you are on the secondary route change to the default gw
> 
> ...


Ah! Darn, I was worried about that. Thank you for the suggestion covacat .
I am new to bash scripting, do you happen to know a good guide for setting up what you suggested? And where/directory I should activate the script?

Thank you for the great guidance.


----------



## covacat (Nov 19, 2021)

```
#!/bin/sh
#
NORMAL_ROUTE="10.10.10.10"
BACKUP_ROUTE="192.168.8.20"
ADMIN=it@some-domain.com
chroute()
{
route change default $1
if [ $? -eq 0 ]
then
#  logger -t "CH-ROUTE"  "Route changed to $2 ($1)"
#  echo "Route changed to $2 ($1)"|mail -s "Route changed to ($2) $1" $ADMIN
#  echo "$2 ($1)" > /home/sites/server-status/r.txt
else
# panic should change route but couldn't
fi
}

CURRENT_ROUTE=$(route -4 -n get default|grep gate|cut -w  -f 3)
ping -q -c 3 $NORMAL_ROUTE >/dev/null 2>&1
OK=$?
if [ $OK -eq 0 ]
 then
  [ "$CURRENT_ROUTE" = "$NORMAL_ROUTE" ] && exit 0
  chroute $NORMAL_ROUTE NORMAL_ROUTE
  else
  [ "$CURRENT_ROUTE" = "$BACKUP_ROUTE" ] && exit 0
  chroute $BACKUP_ROUTE BACKUP_ROUTE
 fi
```
if you have 2 ISPs then make sure your dns is set to a public DNS like 8.8.8.8 or 1.1.1.1 because you may not be able to query a specific ISP's DNS with an IP from another ISP
you can run it from cron every 2 minutes or so
if you need faster route change put everything in a while true and add a sleep of several second at the loops end and run it from /etc/rc.local


----------



## tpfiler (Nov 19, 2021)

covacat said:


> ```
> #!/bin/sh
> #
> NORMAL_ROUTE="10.10.10.10"
> ...


This is awesome! Thank you very much this is a great start. I will update on how I was able to implement it.


----------

