# multiple default gateways



## Snail (Aug 23, 2018)

Hello,

My adsl connection being slow, I bought a usb 4G key and each ip is provided by dhcp.
To be able to register both default gateways and use them in a next step, I wrote /etc/rc.conf.d/networking like so:

```
ifconfig_igb0="DHCP fib 0"
ifconfig_ue0="SYNCDHCP fib 1"
```

But 
	
	



```
setfib 1 netstat -rn
```
 does never return a default gateway.
Only 
	
	



```
netstat -rn
```
 does.

I tried from several computers, and the default gateway is always the same on a computer, but not the same between 2 different computers.

My questions are :

why is fib 1 not filled up with a default gateway ?
how is the "default default" gateway (the one from fib 0) chosen ?
Is it possible to force the choice of this "default default" gateway ?


----------



## leebrown66 (Aug 24, 2018)

1. This thread may be relevant, in essence use SYNCDHCP for both.  Unfortunately the thread stops so no clue if that actually works.
2 & 3. The routing table used is determined by the fib the program is running under.  i.e. `setfib 1 ping www.google.com` will use the gw from fib 1 wheras `ping www.google.com` will use the gw from fib 0.  Your firewall of choice can also influence this, so you can for example send port 22(SSH) to fib 0 and port 80(WWW) to fib 1.


----------



## Snail (Aug 24, 2018)

Reading back my questions, it appears to me that I was not really clear, so please allow me to retry.

Not being at home, I am using another ( virtual) machine, this is why the ip addresses are now private and the interfaces are different.
There are 2 networks, both of them having a dedicated dhcp server.

1) environment

On 192.168.122.0/24:

the interface is em0
the default gateway's address (provided by the dhcp server) is 192.168.122.1
On 192.168.130.0/24:

the interface is vtnet0
the default gateway's address (provided by the dhcp server) is 192.168.130.131
2) tests
a) em0 only
/etc/rc.conf.d/network contains only:

```
ifconfig_em0="DHCP"
```
`root@tests:~ # netstat -nr |grep default
default            192.168.122.1      UGS         em0`
==> the default gateway is correctly set

b) vtnet0 only
/etc/rc.conf.d/network contains only: 

```
ifconfig_vtnet0="DHCP"
```

`root@tests:~ # netstat -nr |grep default
default            192.168.130.131      UGS         vtnet0`
==> the default gateway is correctly set

c) both interfaces with DHCP
/etc/rc.conf.d/network contains 

```
ifconfig_em0="DHCP"
ifconfig_vtnet0="DHCP"
```
`root@tests:~ # netstat -nr |grep default
default            192.168.130.131      UGS         vtnet0`
==> From two possible gateways, one was chosen.

d) both interfaces with SYNCDHCP
/etc/rc.conf.d/network contains 

```
ifconfig_em0="SYNCDHCP"
ifconfig_vtnet0="SYNCDHCP"
```
`root@tests:~ # netstat -nr |grep default
default            192.168.122.1      UGS         em0`
==> This time, the other gateway was chosen.

e) setfib and DHCP
/etc/rc.conf.d/network contains 

```
ifconfig_em0="DHCP fib 0"
ifconfig_vtnet0="DHCP fib 1"
```
`root@tests:~ # setfib 0 netstat -nr |grep default
default            192.168.122.1      UGS         em0
root@tests:~ # setfib 1 netstat -nr | grep default`
==> DHCP seems to prefer the first gateway

f) setfib and SYNCDHCP
/etc/rc.conf.d/network contains 

```
ifconfig_em0="SYNCDHCP fib 0"
ifconfig_vtnet0="SYNCDHCP fib 1"
```
`root@tests:~ # setfib 0 netstat -nr |grep default
default            192.168.130.131      UGS         vtnet0
root@tests:~ # setfib 1 netstat -nr | grep default`
==> SYNCDHCP seems to prefer the other gateway

g) as f) but by reverting fib 0 and fib 1
The result is the same as f)
3) questions

why does setfib 1 not contain any default gateway ?
Comparing c to d or e to f, it appears that the chosen default gateway (the one returned by netstat -nr) may differ between 2 different configurations. So, how is it chosen ?
Plus, is it possible to "force" this choice? I thought this would be driven by the interface set as fib 0, but g) does show that this is not that simple.


----------



## Deleted member 45312 (Aug 24, 2018)

I am using something similar but with static configuration. I have these lines in /etc/rc.conf :

```
# HUAWEI 4G router
defaultrouter="192.168.8.1"

ifconfig_em0="inet 192.168.8.10 netmask 255.255.255.0"
ifconfig_re0="inet 192.168.0.10 netmask 255.255.255.0 fib 1"

static_routes="fibadsl"
route_fibadsl="default 192.168.0.254 -fib 1"
apache24_fib="1"
```
And this in /boot/loader.conf :

```
net.fibs=2
```


----------



## leebrown66 (Aug 24, 2018)

Snail - the root problem I suspect is the the dhclient daemon is always running as fib 0, which is why the default gateway never makes it into fib 1.  One way around this (maybe not the best), would be to write an /etc/rc.local which runs the two dhclients as fib 0 and fib 1 and don't try to use DHCP in the rc.conf.

Something like:

setfib 0 dhclient -b em0
setfib 1 dhclient -b vtnet0

Actually just try it manually, replacing -b with -d, see if it has the desired effect first.


----------



## Snail (Aug 24, 2018)

dlegrand  : thank you for helping.
leebrown66 : That is a clever suspicion. I tried to dig into /etc/default/rc.conf,/etc/rc.d/routing and /etc/rc.d/netif but could not find anything interesting for now.
I ended up writing an rc script reading 
	
	



```
setfib 1 dhclient -b vtnet0 (ue0 actually)
```
 running before pf script and after netif script.
It works, but I think that I am going to fill a bug, in the hope that it will make things simpler. Or at least to get an explanation. I really want to understand.


----------

