Use two network interfaces simultaneously

Posted on

Problem :

I often use Wi-Fi from some public networks which don’t allow some protocols (like torrent or IRC – next I will only speak about IRC). If I want to use one of those protocols, I have to use my phone 3G connection (through the usb0 interface).

I’d like to stay connected to the Wi-Fi for my web navigation (so use the classical wlan0 interface), but use another interface (here usb0) for some softwares (often only for weechat, a CLI IRC client).

Thus I have two (linked) questions :

  • how to simultaneously connect to two interfaces without all messing things up ?
  • how to configure one software to use an interface, and another software to use the other interface ? As weechat is a CLI software, maybe I just have to declare a specific environment variable ; but how to be sure that Firefox doesn’t use usb0 instead of wlan0 ?

Solution :

The simplest solution is to have two separate routing tables; one of them (wlan0, in your case) will be the default one, while we can use a neat trick to force a specific application to use the non-default table.

First, create two tables (substitute NAME1 with a name making sense to you, same thing with IP1, DEV1, and so on):

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 main
ip route add NET2 dev DEV2 src SRC2 table NAME2

Then a default route:

ip route add default via IP1 table main
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 main
ip rule add from IP2 table NAME2

Now we need to force weechat to use IP2.We do so by building a new bind module. It is trivial to do so, there are simple instructions in the reference. Once you have compiled the module and placed in /usr/lib, you issue the command:

 BIND_ADDR=IP2 LD_PRELOAD=/usr/lib/ weechat

and you are done. The advantage of this is that it can be easily scripted, and can be repeated for as many applications as you like.

If you define two sub-networks, for example:

  • 192.168.1.x / – wlan0
  • 192.168.2.x / – usb0

If you define gateway only for wlan0, all network traffic beside addressed directly to 192.168.2.x goes through wlan0.

You can route network traffic to specific IP addresses through you usb0 interface. You can also route the traffic over specific interface for a process in linux.

Leave a Reply

Your email address will not be published.