Linux – How to tell if the multiple network interfaces are working ok

linuxnetworkingroutingubuntu 14.04wireless-networking

On Ubuntu 14.04 x86-64 I have a wireless broadband connection and a USB tethered one (it's on 4G, not the same wireless):

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.42.129  0.0.0.0         UG    0      0        0 usb0
192.168.0.0     0.0.0.0         255.255.255.0   U     9      0        0 wlan1
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 usb0

I have then the following also:

ifconfig

...

usb0      Link encap:Ethernet  HWaddr 02:36:2c:04:05:7c  
          inet addr:192.168.42.248  Bcast:192.168.42.255  Mask:255.255.255.0
          inet6 addr: fe80::36:2cff:fe04:57c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3367 errors:1 dropped:0 overruns:0 frame:1
          TX packets:3669 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2434689 (2.4 MB)  TX bytes:637911 (637.9 KB)

wlan1     Link encap:Ethernet  HWaddr 14:cc:20:75:e1:8d  
          inet addr:192.168.0.4  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::16cc:20ff:fe75:e18d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3806 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4110 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2283536 (2.2 MB)  TX bytes:583794 (583.7 KB)

It all looks like it's supposed to be working as intended (i.e. both network connections are up, my machine got 2 IPs, …).
Then I wanted to use the bind shim to force processes on one connection (or another) and those only work when I specify the IP of the default connection/gateway.
The following works (please note this is on the tethered USB – 4G):

BIND_ADDR="192.168.42.248" LD_PRELOAD=./bind.so chromium-browser

And this doesn't (my broadband):

BIND_ADDR="192.168.0.4" LD_PRELOAD=./bind.so chromium-browser

Of course, when I unplug the USB phone, wlan0 magically starts working again…

What am I doing wrong? Any idea?

Best Answer

The reason why your bind does not work has nothing to with bind, a trick I use too, and everything to do with the fact that you have only one default gateway, on the usb0 NIC, while you should have one on either NIC, otherwise connection to WAN sites from wlan0 will not be routed.

Linux allows the existence of multiple gateways (one per interface, virtual or not), under the regimen of policy-based routing. What you need to do is to have two separate routing tables, one for usb0 and one for wlan0, and a rule telling the kernel which one to use; then binding an application to either interface will allow you to reach WAN sites.

You handle two routing tables as follows: First, create two tables (Replace and with sensible names, same with IP1, DEV1, and so on):

echo 200 <NAME1> >> /etc/iproute2/rt_tables
echo 201 <NAME2> >> /etc/iproute2/rt_tables

Add a gateway to each routing table (if needed):

ip route add <NET1> dev <DEV1> src <SRC1> table <NAME1>
ip route add <NET2> dev <DEV2> src <SRC2> table <NAME2>

Then a default route:

ip route add default via <IP1> table <NAME1>
ip route add default via <IP2> table <NAME2>

Then the rules to select the route table based on the source address:

ip rule add from <IP1> table <NAME1>
ip rule add from <IP2> table <NAME2>

Now you can bind application to either interface.