Routing WIFI and LAN for specific traffic
I have two network devices aboard my macbook pro:
- WIFI (en1): Used for general traffic. Connects to an ip of 192.168.19.* via DHCP
- LAN (en0): Used for specific traffic. Connects to an ip of 192.168.2.10 as a static IP. Does not connect to a router, only a switch for direct
routingconnection.
I have 4 IP addresses I need to access on the LAN:
- 192.168.2.1
- 192.168.2.21
- 192.168.2.20
- 192.168.2.30
The rest of the traffic needs to go to WIFI. I have tried setting up a routing table for the specific ip addresses, but I only managed to mess up my network. I do not venture out into the world of networking too often, but this was the latest command I have been trying:
sudo route add -host 192.168.2.30 -interface en0
This command killed my ability to use ping. It told me that ping could not allocate memory (is that even possible)? It also killed my wifi access. Logging out and back in fixed the issue. I really do not mind to make this solution permanent, so I am fine with a temporary routing.
EDIT:
If I currently have been trying:
sudo route flush
sudo route add default 192.168.19.1
This gets everything to work for about a minute. But after such minute it "forgets" the routing to WiFi while retaining LAN's (en0) routing. If I unplug and replug my LAN (en0) cable, the process works for another minute.
EDIT 2:
These were some of commands entered as a request by d34dh0r53.
$ netstat -rn
Routing tables
Internet:
Destination Gateway Flags Refs Use Netif Expire
default 192.168.2.1 UGSc 4 0 en0
default 192.168.19.1 UGScI 0 0 en1
127 127.0.0.1 UCS 0 1 lo0
127.0.0.1 127.0.0.1 UH 5 1429023 lo0
169.254 link#4 UCS 0 0 en0
192.168.2 link#4 UCS 4 0 en0
192.168.2.1 0:27:22:2e:5f:1a UHLWIi 2 0 en0 1199
192.168.2.10 127.0.0.1 UHS 0 0 lo0
192.168.2.30 90:a2:da:0:f5:63 UHLWIi 1 1433 en0 1191
192.168.2.255 ff:ff:ff:ff:ff:ff UHLWbI 0 7 en0
192.168.19 link#5 UCS 2 0 en1
192.168.19.1 3e:d0:f8:aa:28:56 UHLWIi 0 2 en1 1192
192.168.19.3 127.0.0.1 UHS 1 0 lo0
192.168.19.255 ff:ff:ff:ff:ff:ff UHLWbI 0 5 en1
Internet6:
Destination Gateway Flags Netif Expire
::1 link#1 UHL lo0
fe80::%lo0/64 fe80::1%lo0 UcI lo0
fe80::1%lo0 link#1 UHLI lo0
fe80::%en0/64 link#4 UCI en0
fe80::226:4aff:fe00:b68a%en0 0:26:4a:0:b6:8a UHLI lo0
fe80::%en1/64 link#5 UCI en1
fe80::226:bbff:fe03:cbd%en1 0:26:bb:3:c:bd UHLI lo0
ff01::%lo0/32 fe80::1%lo0 UmCI lo0
ff01::%en0/32 link#4 UmCI en0
ff01::%en1/32 link#5 UmCI en1
ff02::%lo0/32 fe80::1%lo0 UmCI lo0
ff02::%en0/32 link#4 UmCI en0
ff02::%en1/32 link#5 UmCI en1
$ traceroute -n 192.168.2.1
traceroute to 192.168.2.1 (192.168.2.1), 64 hops max, 52 byte packets
1 192.168.2.1 2.499 ms 3.392 ms 3.829 ms
$ traceroute -n google.com
traceroute: unknown host google.com
Solution 1:
You shouldn't need to create any routing rules by hand for that configuration, provided that all the 192.168.2.x addresses you want to communicate with are down the interface with the 192.168.2.x address.
- Go to System Preferences -> Network
- Select your Ethernet device, make sure "Configure IPv4" is set to "Manually", that your subnet mask is set to 255.255.255.0, and that the router box is empty.
Once this is done, netstat -rn
should still show the routes for both of the subnets, but only a "link#4" route in place of the default route through 192.168.2.1.
If you don't want a default route, leave the router box blank. The value in the router box is only used to set up a default route through this network interface, and it isn't used for anything else.
Solution 2:
The problem is that your en0
interface is adding a default route which is taking precedence over the default route established by the 802.11 interface. I'm not exactly sure why, but it's either the fact that BSD is preferring a wired interface over a wireless one, or it's preferring a statically configured interface over a dynamically configured one. You can tell that the en0
default route is being used by the Refs
column; Refs
is a metric indicating the current number of active uses of the route, so we can see that it's getting all of the traffic.
The solution is to remove that route, preferable permanently from the routing table so that traffic that is destined for hosts other than those on your local networks traverses the default gateway established by DHCP on en1
. The first thing I would check is in the configuration panel for en0
make sure that you have not entered anything in the router
field. The information in that field is added as a default gateway. If that does not work we need to manually delete the route, the reason that route flush
does not work is that I believe it tells OS X to reload it's routing information from the interface configuration files, hence reverting your change after a short time. The following command should remove the default route for the en0
interface until either networking is restarted or the system is IPLed:
sudo route delete -net 0.0.0.0 192.168.2.1
If you want to make this change permanent you can either a) create a service in /Library/StartupItems
which seems like too much work to me or b) add that line to /etc/rc.local
with a command such as:
echo 'route delete -net 0.0.0.0 192.168.2.1' >> /etc/rc.local
You may need to add a sleep <number_of_seconds>
command before that line in /etc/rc.local
to avoid running the command before the interfaces are fully up and the routing table established.
Hope this helps and good luck!