1

I want to decide on application level which gateway to use to connect to the internet.

It can't be done by simple subnet rules, because it is really the application which should be able to decide which gateway to use.

E.g. I want to use curl to download something and I want to specify somehow which gateway to use.

It is a Linux box having only one network card, IP address 10.0.0.1. There are two or more gateways (different ISPs): e.g. 10.0.0.100 and 10.0.0.101.

Is it possible to set up routing rules with iptables and iproute2 to manage that? How do I tell an application which route to use?

I tried this:
I added in /etc/iproute2/rt_tables: 100 out2
ip rule add prio 200 from 10.0.0.2 lookup out2
ip route add 10.0.0.0/24 dev eth0 src 10.0.0.2 table out2
ip route add default via 10.0.0.100 dev eth0 src 10.0.0.2 table out2
Still "telnet -b 10.0.0.2 www.google.com 80" returns "network is unreachable"

1
  • Maybe using owner-matching with --uid-owner in iptables rules?
    – pintpint
    Jan 6, 2012 at 16:31

2 Answers 2

3

I can think of one way to do it which is as follows.

  1. Define two IP addresses on your NIC like 10.0.0.1 & 10.0.0.2. This should be easy and straight-forward. This can be done using ifconfig.
  2. Configure source based routing such that each source IP is routed to one of the default gateways. This link can help you.
  3. Finally, you can tell your application to use bind one of the addresses 10.0.0.1 or 10.0.0.2. This way you can choose one of the gateways depending on the source IP selected. To test this, you can use telnet with -b source_ip option.

Here are the commands I used previously to enable source-based routing:

$ sudo ip rule add from 10.0.0.2 tab 1 priority 500
$ sudo ip route add default via 10.0.0.101 dev eth0 tab 1
$ sudo ip route flush cache

If you have the default gateway set to 10.0.0.100, then this should work for you. The packets sent from 10.0.0.1 should be sent to default gateway, and packets sent from 10.0.0.2 should be sent to 2nd gateway 10.0.0.101 as instructed above.

8
  • @pintpint: It is hard to read such information from a comment. You can edit your question instead. It will be better and easier to read. Did you forget to add the port 80 at the end of telnet command?
    – Khaled
    Jan 9, 2012 at 13:29
  • Sorry, I added it to my original question now...
    – pintpint
    Jan 9, 2012 at 13:32
  • Does the described setup really work for you? I think one problem might be that I have two IP addresses but only one NIC. Routing would be easier if I had two NICs. But that would not solve the problem because it is not scalable.
    – pintpint
    Jan 10, 2012 at 9:53
  • @pintpint: I think the tricky part is related to the source-based routing. I tried point 2 before and it worked for me. The other points should be easy.
    – Khaled
    Jan 10, 2012 at 9:58
  • What do you mean by "point 2"?
    – pintpint
    Jan 10, 2012 at 10:01
0

Another option. A lot of CLI utilitise use environment variables to do this type of configuration, http_proxy may be relevant to your situation, http://www.cyberciti.biz/faq/linux-unix-set-proxy-environment-variable/

You must log in to answer this question.

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