How to route the incoming connections through a specific interface?

7,464

Solution 1

I found the solution for this myself: Routing for multiple uplinks/providers

A common configuration is the following, in which there are two providers that connect a local network (or even a single machine) to the big Internet.

                                                               ________
                                          +------------+        /
                                          |            |       |
                            +-------------+ Provider 1 +-------
        __                  |             |            |     /
    ___/  \_         +------+-------+     +------------+    |
  _/        \__      |     if1      |                      /
 /             \     |              |                      |
| Local network -----+ Linux router |                      |     Internet
 \_           __/    |              |                      |
   \__     __/       |     if2      |                      \
      \___/          +------+-------+     +------------+    |
                            |             |            |     \
                            +-------------+ Provider 2 +-------
                                          |            |       |
                                          +------------+        \________

how to route answers to packets coming in over a particular provider, say Provider 1, back out again over that same provider.

Let us first set some symbolical names. Let $IF1 be the name of the first interface (if1 in the picture above) and $IF2 the name of the second interface. Then let $IP1 be the IP address associated with $IF1 and $IP2 the IP address associated with $IF2. Next, let $P1 be the IP address of the gateway at Provider 1, and $P2 the IP address of the gateway at provider 2. Finally, let $P1_NET be the IP network $P1 is in, and $P2_NET the IP network $P2 is in.

One creates two additional routing tables, say T1 and T2. These are added in /etc/iproute2/rt_tables. Then you set up routing in these tables as follows:

  ip route add $P1_NET dev $IF1 src $IP1 table T1
  ip route add default via $P1 table T1
  ip route add $P2_NET dev $IF2 src $IP2 table T2
  ip route add default via $P2 table T2

Nothing spectacular, just build a route to the gateway and build a default route via that gateway, as you would do in the case of a single upstream provider, but put the routes in a separate table per provider. Note that the network route suffices, as it tells you how to find any host in that network, which includes the gateway, as specified above. Next you set up the main routing table. It is a good idea to route things to the direct neighbour through the interface connected to that neighbour. Note the `src' arguments, they make sure the right outgoing IP address is chosen.

    ip route add $P1_NET dev $IF1 src $IP1
    ip route add $P2_NET dev $IF2 src $IP2

Then, your preference for default route: ip route add default via $P1

Next, you set up the routing rules. These actually choose what routing table to route with. You want to make sure that you route out a given interface if you already have the corresponding source address: ip rule add from $IP1 table T1 ip rule add from $IP2 table T2

Solution 2

You don't route incoming to a specific - you route outgoing...

the last hop (router or whatever) that targets you will decide which interface you are contacted on... e.g. if a connection is established from 192.168.1.x, it will not route to the nic connected to 192.168.3.x.

maybe I am missing something here... if this isn't what you want to hear, I think you need to draw your network topology or give us some more information.

Share:
7,464

Related videos on Youtube

rahul
Author by

rahul

Updated on September 18, 2022

Comments

  • rahul
    rahul over 1 year

    I have multiple ethernet cards with two different set of networks configured on them.

    The output of route -n is given here::

         Kernel IP routing table 
          Destination  Gateway        Genmask        Flags Metric Ref Use Iface
       0.0.0.0         192.168.3.1     0.0.0.0         UG    0      0        0 eth0
       169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth1
       192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth1
       192.168.3.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
    

    The outgoing connections are fine. The problem is with incoming connections: How can I route the incoming traffic to go through 192.168.1.X ?

    Another thing that I would like to add is, I need the outgoing connections to go through the 192.168.3.1.

  • rahul
    rahul over 11 years
    Thanks for the reply. I have changed my default route to 192.168.3.X for some specific purpose. That cant be changed. I hope that makes it clear. Also i need to give outside access to the same machine, which doesn't happen even though the forwading is done correctly in the router.
  • Justin
    Justin over 11 years
    What is the IP of your router in this example?
  • slhck
    slhck over 11 years
    Could you maybe edit your answer and include all the important steps to solve your specific problem, please? Links might go down, and it's good to have the info here.
  • rahul
    rahul over 11 years
    @slhck sure thing. can you please review the same.