Peer to peer routing in FreeBSD

November 21, 2013 by Paweł Biernacki

Some hosting providers started offering IPs or IP aliases with /32 bit mask (for example Hetzner). Virtual interfaces such as gif or tun have built-in ability to act as peer to peer devices and ifconfig utility can use that ability to easily setup of these interfaces, but ethernet devices need some special treatment.

The obvious way of configuring it by setting

1
2
ifconfig_em0="1.2.3.4/32"
defaultroute="2.3.4.1"

will not work because default gateway is not in rage of mask on the interface. However there is an easy way to make it work that nicely fit with rc.conf. The first thing is to setup the IP configuration for an interface

ifconfig em0 1.2.3.4 / 32

(change em0 to the correct name of your interface and obviously adjust the IP address). Next we need to add host route to our default gateway, but because the address will not fit in the /32 bit mask we supply -ifp flag to route to indicate the interface that should be used:

route add 2.3.4.1 1.2.3.4 -static -iface -ifp em0

Then we can finally add a default route, simply specifying

route add default 2.3.4.1

Now we can put it all together and save it in /etc/rc.conf. Static routes can be defined instatic_routes variable, and then expanded by name and executed in order of definitions:

1
2
3
4
ifconfig_em0="1.2.3.4/32"
static_routes="em0_p2p em0_default"
route_em0_p2p="2.3.4.1 1.2.3.4 -static -iface -ifp em0"
route_em0_default="default 2.3.4.1"
Posted in: FreeBSD