Solved How-to: Static route to gateway through an interface that has a dynamic (DHCP) IP address.

My 11.0-RELEASE-p9 machine has three network interface cards, two wired and one wireless; these are re0, nfe0, and ral0/wlan0, respectively. The machine's primary role is an Internet gateway for the home LAN. The LAN is on re0. Both nfe0 and wlan0 get DHCP addresses from two separate Internet Service Providers (ISP). nfe0 is the default gateway.

I would like to set up some static routes so that traffic going to a few specific destination IP addresses will go through the wlan0 gateway. For example, let's say all traffic sent to 8.8.8.8 needs to go through the wlan0 gateway. If wlan0 had a fixed address like 172.16.0.1, then something like this might work - route add -host 8.8.8.8 172.16.0.1 - but wlan0 does not have a fixed address.

I've tried - route add -host 8.8.8.8 -interface wlan0 - but that doesn't treat wlan0 as a gateway device, rather, I think wlan0 is treated as the final destination in this case.

Any ideas?

EDIT: There is a fundamental misunderstanding/confusion in this question; it gets resolved in the solution.
 
You should replace the wlan0 ip to the gateway ip in the route command. That gateway ip usually is a fixed ip.
 
You should replace the wlan0 ip to the gateway ip in the route command. That gateway ip usually is a fixed ip.

Replacing the wlan0 IP address with the IP address of the gateway that gave wlan0 its address seems more reasonable than what I've tried, thanks!

Here's a test:

netstat -rn
Code:
Routing tables

Internet:
Destination        Gateway            Flags     Netif Expire
default            10.0.0.1           UGS        nfe0
10.0.0.0/24        link#2             U          nfe0
10.0.0.240         link#2             UHS         lo0
127.0.0.1          link#3             UH          lo0
192.168.0.0/24     link#1             U           re0
192.168.0.1        link#1             UHS         lo0
192.168.48.0/22    link#4             U         wlan0
192.168.49.167     link#4             UHS         lo0
It looks like the wlan0 IP address is 192.168.49.167 and its gateway is within 192.168.48.0/22, presumably 192.168.48.1, so:

sudo route add -host 8.8.8.8 192.168.48.1
Code:
add host 8.8.8.8: gateway 192.168.48.1
netstat -rn
Code:
Routing tables

Internet:
Destination        Gateway            Flags     Netif Expire
default            10.0.0.1           UGS        nfe0
8.8.8.8            192.168.48.1       UGHS      wlan0
10.0.0.0/24        link#2             U          nfe0
10.0.0.240         link#2             UHS         lo0
127.0.0.1          link#3             UH          lo0
192.168.0.0/24     link#1             U           re0
192.168.0.1        link#1             UHS         lo0
192.168.48.0/22    link#4             U         wlan0
192.168.49.167     link#4             UHS         lo0
traceroute 8.8.8.8
Code:
traceroute to 8.8.8.8 (8.8.8.8), 64 hops max, 40 byte packets
 1  192.168.48.1 (192.168.48.1)  2.981 ms  0.487 ms  0.761 ms
 2  96.120.110.25 (96.120.110.25)  18.512 ms  19.076 ms  29.933 ms
^C
That seems to be doing exactly what I needed. Thanks, again!
 
Back
Top