Switching between network adapters

Hi Everyone,

My laptop has both Ethernet and wireless cards, managed by the em(4) and iwn(4) drivers, the latter being associated with wlan0. Normally Ethernet is not connected, and I'm using wifi only. Sometimes I need to transfer a large file on my subnetwork, and for this I'd like to plug in an Ethernet cable, switch to Ethernet, do the transfer and switch back to wifi. So I shut down wlan0, bring up em0, obtain an IP for it, remove the old default route from the routing table, and add the new route:

# ifconfig wlan0 down
# ifconfig em0 up
# dhclient em0
# route delete default
# route add default 192.168.1.1

After this my wired connection works. The problem happens when I try to switch back using the same steps, swapping em0 for wlan0. This time the routing table stubbornly insists on keeping em0 for the default destination, even after I bring the interface down (it still has an IP address!)

What's the right way of doing this? I find it strange that route won't let us select an interface when adding routes. What happens when, like in this case, there are two interfaces, both having an IP address?
 
Thanks for the tip--I didn't try lagg, and reading through the docs, it would solve my problem, though perhaps in a more complex way than what's required. If I understand correctly, lagg would combine the configured wired and wireless interfaces, then prefer the wired one when available, without any further selection on my part. However, most of the time I'm using wifi only, and I wouldn't like to keep my main interface as a fallback, when ethernet is not available. I thought the most efficient way would be manually specifying which adapter I want to use. I don't mind doing this in the terminal like above, if there's a way to get it to work properly.

The output of netstat -r shows the interface selected for each destination. Why doesn't route add let us specify the interface we want? If I have two configured network interfaces on the same subnetwork, each with its own IP address, is there any way to specify which one I want to add to the routing table?
 
Unfortunately I can't test this at the moment, but I think you can achieve what you want with route command.
Clip from man page:
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.
 
Assuming that your interfaces are configured in /etc/rc.conf, the simplest thing is service netif stop|start <iface>. So to switch interfaces I do service netif stop wlan0 && service netif start re1. Another thing I'm assuming is that you use DHCP, so the default gateway is being provided to you. This will update the default route and the network submask to use whatever interface you specify.

Why doesn't route add let us specify the interface we want?
That one I don't know, and I've spent a lot of time frustrated with it.
 
So to switch interfaces I do service netif stop wlan0 && service netif start re1.
Make sure to also run service routing restart. The stopping of wlan0 would remove any routes on that interface too, including your default gateway.
Another thing I'm assuming is that you use DHCP, so the default gateway is being provided to you.
If you use DHCP you should use service dhclient [stop|start|restart] <interface>.
 
Back
Top