A FreeBSD system can be configured as the default gateway, or router, for a network if it is a dual-homed system.
A dual-homed system is a host which resides on at least two different networks.
Typically, each network is connected to a separate network interface, though IP
aliasing can be used to bind multiple addresses, each on a different subnet, to one physical interface.
In order for the system to forward packets between interfaces, FreeBSD must be configured as a router.
Internet standards and good engineering practice prevent the FreeBSD Project from enabling this feature by default, but it can be configured to start at boot by adding this line to /etc/rc.conf
:
gateway_enable="YES" # Set to YES if this host will be a gateway
To enable routing now, set the sysctl(8)
variable net.inet.ip.forwarding
to 1
.
To stop routing, reset this variable to 0
.
The routing table of a router needs additional routes so it knows how to reach other networks.
Routes can be either added manually using static routes or routes can be automatically learned using a routing protocol.
Static routes are appropriate for small networks and this section describes how to add a static routing entry for a small network.
|
For large networks, static routes quickly become unscalable.
FreeBSD comes with the standard BSD routing daemon routed(8)
, which provides the routing protocols RIP , versions 1 and 2, and IRDP .
Support for the BGP and OSPF routing protocols can be installed using the net/zebra
package or port.
|
Consider the following network:
In this scenario, RouterA
is a FreeBSD machine that is acting as a router to the rest of the Internet.
It has a default route set to 10.0.0.1
which allows it to connect with the outside world. RouterB
is already configured to use 192.168.1.1
as its default gateway.
Before adding any static routes, the routing table on RouterA
looks like this:
% netstat -nrRouting tables
Internet:
Destination Gateway Flags Refs Use Netif Expire
default 10.0.0.1 UGS 0 49378 xl0
127.0.0.1 127.0.0.1 UH 0 6 lo0
10.0.0.0/24 link#1 UC 0 0 xl0
192.168.1.0/24 link#2 UC 0 0 xl1
With the current routing table, RouterA
does not have a route to the 192.168.2.0/24
network.
The following command adds the Internal Net
2
network to RouterA
's routing table using 192.168.1.2
as the next hop:
# route add -net 192.168.2.0/24 192.168.1.2
Now, RouterA
can reach any host on the 192.168.2.0/24
network.
However, the routing information will not persist if the FreeBSD system reboots.
If a static route needs to be persistent, add it to /etc/rc.conf
:
# Add Internal Net 2 as a persistent static route
static_routes="internalnet2"
route_internalnet2="-net 192.168.2.0/24 192.168.1.2"
The static_routes
configuration variable is a list of strings separated by a space, where each string references a route name.
The variable route_internalnet2
contains the static route for that route name.
Using more than one string in static_routes
creates multiple static routes.
The following shows an example of adding static routes for the 192.168.0.0/24
and 192.168.1.0/24
networks:
static_routes="net1 net2"
route_net1="-net 192.168.0.0/24 192.168.0.1"
route_net2="-net 192.168.1.0/24 192.168.1.1"