OSX Yosemite - How to add route having two default gateways

3

I'm on OSX Yosemite and I have two different active connections... - WIFI (en0) - Tethering USB (en7)

SnakeBook:~ root# netstat -nr
Routing tables

Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            192.168.1.1        UGSc           14        4     en0
default            192.168.42.129     UGScI           0        0     en7

Now...I need to add a route to browse some websites by tethering connection. For example, I want to run a speedtest on speedtest.net using en7. Speedtest is resolved this way by my DNS (192.168.1.1 that use OpenDNS)...

SnakeBook:~ root# nslookup speedtest.net
Server:     192.168.1.1
Address:    192.168.1.1#53

Non-authoritative answer:
Name:   speedtest.net
Address: 216.146.46.10
Name:   speedtest.net
Address: 216.146.46.11

Now...I added two routes:

SnakeBook:~ root# route add -host 216.146.46.10 192.168.42.129
add host 216.146.46.10: gateway 192.168.42.129
SnakeBook:~ root# route add -host 216.146.46.11 192.168.42.129
add host 216.146.46.11: gateway 192.168.42.129
SnakeBook:~ root# netstat -nr
Routing tables

Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            192.168.1.1        UGSc           14        4     en0
default            192.168.42.129     UGScI           0        0     en7
127                127.0.0.1          UCS             0        0     lo0
127.0.0.1          127.0.0.1          UH              3    13259     lo0
169.254            link#4             UCS             0        0     en0
169.254            link#12            UCSI            0        0     en7
192.168.1          link#4             UCS             3        0     en0
192.168.1.1/32     link#4             UCS             1        0     en0
192.168.1.1        0:1c:f0:38:4e:49   UHLWIir        17      509     en0   1199
192.168.1.4/32     link#4             UCS             0        0     en0
192.168.1.7        30:59:b7:a6:3b:cf  UHLWI           0        0     en0   1155
192.168.1.8        64:9a:be:ee:43:93  UHLWI           0        0     en0   1105
192.168.1.255      ff:ff:ff:ff:ff:ff  UHLWbI          0        3     en0
192.168.42         link#12            UCS             1        0     en7
192.168.42.114/32  link#12            UCS             1        0     en7
192.168.42.129/32  link#12            UCS             1        0     en7
192.168.42.129     76:0:b4:4:cc:38    UHLWIir         3        2     en7    713
192.168.42.255     ff:ff:ff:ff:ff:ff  UHLWbI          0        3     en7
216.146.46.10      192.168.42.129     UGHS            0        0     en7
216.146.46.11      192.168.42.129     UGHS            0        0     en7

and trying a traceroute on speedtest.net it seems works fine.

SnakeBook:~ root# traceroute speedtest.net
traceroute: Warning: speedtest.net has multiple addresses; using 216.146.46.11
traceroute to speedtest.net (216.146.46.11), 64 hops max, 52 byte packets
 1  192.168.42.129 (192.168.42.129)  0.760 ms  0.192 ms  0.366 ms
 ...
 ...    
12  * *^C

The problem is when I use my browser. Using browser, traffic always passes through en0 gw and not en7 gw. Is this problem caused by "two" default gateways ?

I tried to remove en7 gw, but It always deletes my en0 gw:

SnakeBook:tmp root# netstat -nr | grep default
default            192.168.1.1        UGSc           30        4     en0
default            192.168.42.129     UGScI           0        0     en7
SnakeBook:tmp root# 
SnakeBook:tmp root# 
SnakeBook:tmp root# route delete default 192.168.42.129
delete net default: gateway 192.168.42.129
SnakeBook:tmp root# 
SnakeBook:tmp root# 
SnakeBook:tmp root# netstat -nr | grep default
default            192.168.42.129     UGScI           0        0     en7

Any suggestion ?

Thanks!

Diego

Posted 2015-03-28T16:03:23.267

Reputation: 41

Answers

3

I was tackling a similar problem: How to set route specific interface metrics under Mac OS X and stumbled upon the answer to your question. What Mac OS X does is this: since it sees two routes to the target it chooses the one with the highest priority (as discussed in this great answer: https://superuser.com/a/525592/169461). Of course changing that priority for every route is not what you want.

What you do want to do is to add a route to this particular IP with the interface specified:

sudo route add -host 216.146.46.10 -iface en7

Note that after a reboot this route will be gone again. I have not been able to persist any static routes in OS X Yosemite yet (tried an approach using a .plist file in /Library/LaunchAgents as discussed here: https://www.jverdeyen.be/mac/persistent-static-routes-mac-os-x/).

titusn

Posted 2015-03-28T16:03:23.267

Reputation: 105