# FreeBSD default routing question concerning DHCP



## urbanriot (Aug 10, 2013)

I read http://www.freebsd.org/doc/handbook/network-routing.html#network-routing-default and I'm still a little unclear on how FreeBSD automatically determines which interface is providing the default route. 

I set up this FreeBSD configuration this morning to aid me in resolving my queries and this is the relevant aspect of my rc.conf:


```
gateway_enable="YES"
ifconfig_sis0="DHCP"
ifconfig_em0="inet 10.1.1.5 netmask 255.255.255.192"
defaultrouter=""
```

And here's the output of my routing table (I made up the external address): 


```
Destination        Gateway            Flags    Refs      Use  Netif Expire
default            d215-251-233-1.hom UGS         0    90442   sis0
10.1.1.0/26        link#6             U           0   121839    em0
10.1.1.5           link#6             UHS         0        0    lo0
localhost          link#10            UH          0      406    lo0
215.251.233.0/21   link#5             U           0        0   sis0
d215-251-170-96.ho link#5             UHS         0        0    lo0
```

In this instance, the sis0 interface is configured for DHCP and is connected directly to a standard home internet connection and FreeBSD automatically assigned it flags "G" and "S".


 How did FreeBSD determine that it should assign "G" flag to the internet connection and make it the default route? This is obviously what I want but I'm curious how it makes that determination.
 What happens when I have a second DHCP internet connection, how does FreeBSD determine which of those two to assign "G" and make the default route? 

So concerning question 2, let's say this is my new rc.conf:


```
gateway_enable="YES"
ifconfig_sis0="DHCP"
ifconfig_xl0="DHCP"
ifconfig_em0="inet 10.1.1.5 netmask 255.255.255.192"
defaultrouter=""
```

And let's say my new routing table is: 


```
Destination        Gateway            Flags    Refs      Use  Netif Expire
default            d215-251-233-1.hom UGS         0    90442   sis0
10.1.1.0/26        link#6             U           0   121839    em0
10.1.1.5           link#6             UHS         0        0    lo0
localhost          link#10            UH          0      406    lo0
222.216.241.0/24   link#7             U           0       12    xl0
222.216.241.224    link#7             UHS         0        0    lo0
215.251.233.0/21   link#5             U           0        0   sis0
d215-251-170-96.ho link#5             UHS         0        0    lo0
```

So now I have two DHCP addresses, 222.216.241.224 and 215.251.170.96, and FreeBSD has decided that the 222.216.241.224 is still the default. (These numbers are not mine, I basically made them up for this example)

 Let's say I'd rather it utilize 215.251.170.96, how do I advise my router to prioritize one NIC over the other? How can I say defaultrouter always = "xl0"?
Thanks in advance!


----------



## kpa (Aug 10, 2013)

1) dhclient(8) calls ifconfig(8) and route(8) to configure the interface and set up the routes including the default route.

2) and 3) I'd guess the interface that was configured later holds the default route. I think you can control which interface you prefer to have the default route in dhclient.conf(5).


----------



## urbanriot (Aug 10, 2013)

I suppose I also have a related new question as well...

4) How can I add a static route to utilize a specific interface, rather than a specific address? Man route states:

"In a change or add command where the destination and gateway are not sufficient to specify the route (as in the ISO case where several interfaces may have the same address), the *-ifp* or *-ifa* modifiers may be used to determine the interface or interface address"

Yet it doesn't give an example and my googling falls short. 

`route add 1.2.3.4/19 -ifp em1` doesn't work and `route add 1.2.3.4/19 -interface em1` leads to an unrouted location.


----------



## urbanriot (Feb 17, 2015)

It's a couple years later but I'm still having an issue with my posts above. I can easily do this with alternative platforms but I'm not getting the hang of it with FreeBSD.

To summarize my first and second post, I'm basically asking how a multihomed FreeBSD server can be forced to prioritize a specific interface when there are two NICs that receive DHCP addresses, addresses that both change.

Since the addresses both change, I can't specify the defaultrouter by address and the default router chosen by dhclient.conf has been random and reviewing the documentation of dhclient.conf(5) isn't illuminating.

Thanks in advance!


----------



## junovitch@ (Feb 17, 2015)

FreeBSD doesn't do multipath routing yet so in short you can't.  You would need some external means to handle it.  Examples would be a routing daemon that talks to upstream and fixes route, devd(8) or net/ifstated initiated actions based on interface state, or use of multiple routing tables and handling moving packets between FIBs with a firewall.


----------



## wblock@ (Feb 17, 2015)

What about lagg(4)?


----------



## urbanriot (Feb 18, 2015)

junovitch said:


> FreeBSD doesn't do multipath routing yet so in short you can't.



This is what I was afraid of. Do you know if OpenBSD treats this scenario any differently? I can achieve what I'm trying to do in Linux but I prefer working with FreeBSD.

I was manually re-creating routes via route command



wblock@ said:


> What about lagg(4)?



I was wondering the same thing but since my literal intention is not primarily fault tolerance or load balancing, I was concerned this option may not apply. I have a need for a list of static routes to pass through specific interfaces depending on the target CIDR range and a cursory glance of the man page suggests utilizing lagg may not allow for this configuration.

Thanks for the interactivity gents.


----------



## urbanriot (Feb 18, 2015)

Googling I see that many people are trying to achieve what I'm wanting to achieve over the years, this guy especially articulates it well - http://serverfault.com/questions/355929/route-add-default-iface-em1-in-rc-conf-on-freebsd

But this guy might guide me down the right track - https://forums.freebsd.org/threads/setting-routes-with-a-dynamic-ip.47917/

I'll evaluate this and see if it helps...


----------



## kpa (Feb 18, 2015)

Look at pfSense, it should be able to do what you want with dynamically changing WAN addresses and gateways.

www.pfsense.org


----------



## urbanriot (Feb 18, 2015)

kpa said:


> Look at pfSense, it should be able to do what you want with dynamically changing WAN addresses and gateways.



If looking at it would tell me how to do it with stock FreeBSD then perhaps I will.


----------



## kpa (Feb 18, 2015)

urbanriot said:


> If looking at it would tell me how to do it with stock FreeBSD then perhaps I will.



It's pretty difficult to do what pfSense is doing on stock FreeBSD. PfSense uses a system that dynamically reloads and reconfigures absolutely everything including gateway settings and anything derived from them when the system detects a change somewhere, for example when a WAN address changes. You might be able to replicate the functionality of pfSense with a lot of devd(8) magic and custom scripting but it's a lot of work.


----------



## Brad Huntting (Aug 20, 2015)

dhclient(8) won't change the default route if the dhcp server doesn't supply one. So configure dhclient(8) to only send a "request" for "routers" on one interface. This is what I used in /etc/dhclient.conf to solve this problem.

```
#...
interface "fxp0" {
    request subnet-mask, broadcast-address time-offset;
}
```
Note fxp0 is the internal corporate network with a slow internet connection, em0 (not mentioned) has a fast internet connection.


----------

