9

I have a usb cellular modem and a Home LAN connection on my Ubuntu 10.10 box.

Both work independently.

I want to know how to have both connected at the same time, and be able to specify which application uses which device to connect to the internet.

Does anyone know how to do this?

1 Answer 1

11

There are several possibilities, depending on how you want to decide what packets go where. Most of them will require some understanding of how TCP/IP networking works in Linux. The main tools you'll have to know to do complex things are iptables (Ubuntu: iptables Install iptables) and iproute2 (ip command) (Ubuntu: iproute Install iproute, iproute-doc Install iproute-doc).

If you can discriminate fully by target IP address, it's simple: route the IP addresses according to your wishes. For example, the following commands will cause all packets for 1.2.3.x and 1.2.4.2 to go via ppp0, and other packets to go via eth0.

route add -net 1.2.3.4/24 ppp0
route add -host 1.2.4.2 ppp0
route add -net 0.0.0.0/0 eth0

For more complex requirements, you need to start using iptables and ip route. For example, the following commands set up special routing tables so that all packets marked 1 go out via eth0 and all packets marked 2 go out via ppp0 (except that packets intended for localhost stick to the loopback interface).

ip rule add fwmark 1 table 1
ip route add 127.0.0.0/0 table 1 dev lo
ip route add 0.0.0.0/0 table 1 dev eth0
ip rule add fwmark 2 table 2
ip route add 127.0.0.0/8 table 2 dev lo
ip route add 0.0.0.0/0 table 2 dev ppp0

Now you can use iptables to “mangle” outgoing packets, adding a mark that will decide what route they take. For example, here's how to send all outgoing SMTP traffic (port 25) via eth0, and all traffic originated by an application running as the user proxy via ppp0.

iptables -t mangle -A OUTPUT -p tcp --dport 25 -j MARK --set-mark 1
iptables -t mangle -A OUTPUT -m owner --uid-owner proxy -j MARK --set-mark 2

See also 2 network interfaces connected to internet. Choose the one to use according to the domain name and bind software to different network interfaces.


You'll need to arrange for these commands to run when both interfaces are connected. I recommend that you write a script called /etc/network/if-up.d/0justin-routes that runs the commands you want. This script will be executed whenever a network interface is brought up; as its name begins with a 0 it will run early in that process, before application-specific setup that might expect the routes to be in place. There is a symmetric /etc/network/if-down.d/ in case you also want to do things when one of the interfaces comes down. (All associated routes will automatically be erased, which may leave some packets stranded when you'd like them to fall back to the other interface.)

The ifup scripts are documented in the interfaces(5) man page. The main thing to know is that the name of the interface being brought up or down is in the environement variable IFACE. You can find out whether the other interface is already up with if ifconfig | sed 's/ .*//' | grep -Fqx 'eth0'; then ….

2
  • @Justin: This is not stuff that I have much experience with, so please give feedback of what worked, what didn't work, what you don't understand, etc. Nov 21, 2010 at 21:12
  • 1
    I have got two additions to make: First, this sends out traffic on ppp0 with source address from eth0. You can change this using: iptables -t nat -A POSTROUTING -o tun0 -j SNAT --to-source ppp_ip, where ppp_ip is the ip address of that adapter. Secondly, you will need to turn off strict rp_filter at /proc/sys/net/ipv4/config/ppp0/rp_filter. It does work when set to relax mode, which is 2.
    – Karalga
    Apr 26, 2015 at 15:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .