How can I tell if my multiple network interfaces are working ok?

3

3

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?

Emanuele

Posted 2015-12-26T15:42:50.610

Reputation: 661

From the looks of it, your .0.0 route doesn't work when you're bound to the 42.0 network because your default route points to a gateway at 42.129, which you can't access on the other network. You need a route for 0.0.0.0/0 on 192.168.0.0/24, probably whatever your router is. – Xyon – 2015-12-26T15:47:45.813

@Xyon How can I verify that? Even if I use the bind shim? That LD_PRELOAD trick is supposed to force all binds and connect to 192.168.0.4? – Emanuele – 2015-12-26T15:50:54.803

Well it not pulling external sites suggests that it is working, because otherwise you'd get connectivity through the other link. Your broadband network isn't completely set up, I don't know how bind is supposed to handle that situation but the kernel doesn't know where to send your packets. – Xyon – 2015-12-26T15:52:32.800

@Xyon I guess I'll have to write a simple test executable to see what errors the kernel is returning when creating TCP/UDP connections? – Emanuele – 2015-12-26T15:53:46.190

No, I wouldn't go that far. What you're trying to do needs a second route table, bound to your other interface, so the system understands how to handle requests from each interface. See https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System for a good resource on the concept.

– Xyon – 2015-12-26T15:55:54.797

Pls read my answer. – MariusMatutiae – 2015-12-26T16:00:06.170

You both are right, in fact if I use BIND_ADDR="192.168.0.4" LD_PRELOAD=./bind.so firefox, I can open the broadband router just fine (on 192.168.0.1). – Emanuele – 2015-12-26T16:34:07.067

@MariusMatutiae would this do it in one line: ip route add from 192.168.0.0/24 gw 192.168.0.1 dev wlan1 or maybe ip route add from 192.168.0.0/24 via 192.168.0.1? – Emanuele – 2015-12-27T10:24:58.477

Answers

3

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.

MariusMatutiae

Posted 2015-12-26T15:42:50.610

Reputation: 41 321

Thanks, this looks like the answer I was looking for. Do you know if there's a way to do it programmatically? Are the changes persistent? – Emanuele – 2015-12-26T16:27:48.403

@Emanuele No, these changes are not persistent. The easiest thing is to put all of the above in an executable file, call it my_config, then, in /etc/network/interfaces, use auto usb0; interface usb0 inet dhcp; post-up /path/to/my_config, and raise interfaces with ifup/ifdown usb0/wlan0. – MariusMatutiae – 2015-12-26T17:17:06.527

Actually my next question would have been what is the one liner command I can use to setup a temporary default gateway for a given IP address. Again, I don't want a static configuration, more than happy to input the one liner command each time. Thanks! – Emanuele – 2015-12-26T18:53:26.327

@Emanuele Put the above commands into an executable file, you have your one liner; to go back to the previous situation, bring don the two interfaces, then back up, and erase the two lines in the /etc/iproute2/rt_tables file. – MariusMatutiae – 2015-12-26T19:15:18.203

Would ip route add 192.168.0.0/24 dev wlan1 (or ip route add 192.168.0.0/24 dev 192.168.0.1 do the trick of temporarily route all the packets for this subnet through that gateway? – Emanuele – 2015-12-26T22:29:41.257