# [solved] two gateways, single subnet, single nic

## evermind

Hi

I want a server with a single nic to be able to receive/send packages from/to

two different gateways within the same subnet.

overview of our setup:

* Each gateway has its own ISP connection.

* gateway1 192.168.2.1

* gateway2 192.168.2.26

* server 192.168.2.22

The receive part is working, but the send part is not.

I know that this fails because of the used default gw.

I want to know if it is possible to know from which

gateway a packet came?

EDIT: The background for this setup is that we are changing our ISP

and we want to have a smooth transition.

thx for your answersLast edited by evermind on Fri Dec 24, 2010 12:02 pm; edited 1 time in total

----------

## nativemad

Hi, 

first off, i don't think that it works with just one IP on the nic!

https://forums.gentoo.org/viewtopic-t-823198-start-0.html There are the following key rules

 *Quote:*   

> ip rule add from 192.168.0.3 table T1
> 
> ip rule add from 192.168.10.3 table T2

 

192.168.0.3 is one ip and 192.168.10.3 is the other... I am not aware that these rules can be set to the used gateway................  :Rolling Eyes: 

But what you can do if you don't like the vlan-solution above are alias ip's like here https://forums.gentoo.org/viewtopic-t-834203-start-0.html

I'm not sure if it works within one single subnet, but theoretically it should!   :Razz: 

So i suppose you can add a secondary alias ip to the hosts and adopt my routes and rules from the first link! 

HTH, Cheers

----------

## DawgG

```
route add -net <NET1> gw 192.168.2.1 netmask <mask> metric 1 eth0

route add -net <NET2> gw 192.168.2.26 netmask <mask> metric 1 eth0
```

should also work; this just does the routing

for the second question you know the net the packet came from so you'll know the gateway, too; else you could always inspect the arp-headers (i don't know how practical that is, though.)

GOOD LUCK!

----------

## evermind

thx for the replies,

@nativemad

I thought about the use of another alias before, but this is too much overhead

I have to re-configure the whole gateway(router) -- and some applications

we use won't work as they are bundled to specific IP-addresses.

@DawgG

I am aware of the NET1 NET2 routing stuff, but I think in this case this won't work

as I am routing towards the internet and not to specific subnets.

But how can I know on which gateway the packet arrived?

The way I can determine if the packet is from a specific gateway is by

knowing its MAC and then create some iptables/routeing rules.

But my knowledge is too less so ATM. my starting point is to mark

the a packet by the MAC addresss it arrived.

```

iptables -t mangle -A INPUT -m mac --mac-source XX:XX:XX:XX:XX:XX -j MARK --set-mark 1

```

whats next? How can I determine that for the packet that is generated because

of that incoming packet is redirected to that gateway that I know by its MAC?

----------

## nativemad

```
ip rule add fwmark 1 table T1
```

----------

## evermind

 *nativemad wrote:*   

> 
> 
> ```
> ip rule add fwmark 1 table T1
> ```
> ...

 

I already tried kind some variations of this, but I will try again

Next question. My Thoughts on this are this:

* I only need this for one gateway

* the second gateway can use the main default route

Are this thougt correct?

Thx again for helping

----------

## nativemad

 *evermind wrote:*   

> 
> 
> Next question. My Thoughts on this are this:
> 
> * I only need this for one gateway
> ...

  Yes, i think that should be enough.

----------

## evermind

I finally get it working here is what I've done

```
# mark all packets that match mac XX:XX:XX:XX:XX:XX (first gw)

iptables -t mangle -A INPUT -m mac --mac-source XX:XX:XX:XX:XX:XX -j MARK --set-mark 1

iptables -t mangle -A INPUT -m mac --mac-source XX:XX:XX:XX:XX:XX -j CONNMARK --save-mark

# mark all packets that  match mac YY:YY:YY:YY:YY:YY (second gw)

iptables -t mangle -A INPUT -m mac --mac-source YY:YY:YY:YY:YY:YY -j MARK --set-mark 2

iptables -t mangle -A INPUT -m mac --mac-source YY:YY:YY:YY:YY:YY -j CONNMARK --save-mark

# restore the marks for all marked outgoing packets

iptables -t mangle -A OUTPUT -j CONNMARK --restore-mark

# add below to /etc/iproute2/rt_tables

echo "200 gw2.1" >> /etc/iproute2/rt_tables

echo "201 gw2.26" >> /etc/iproute2/rt_tables

# add the gateways to the appropriate routing table

ip route add default via 192.168.2.26 dev eth0 table gw2.26

ip route add default via 192.168.2.1 dev eth0 table gw2.1

# As default route I found that something have to stay there

# you can either create a dummy device

ifconfig dummy0 1.2.3.4

route add default dev dummy0

# OR stick to whatever is set there. I've choosen to

# let the original there

```

thx again for all your helpLast edited by evermind on Sat Dec 25, 2010 2:08 am; edited 1 time in total

----------

## nativemad

Don't you need an `ip rule`?

----------

## evermind

 *nativemad wrote:*   

> Don't you need an `ip rule`?

 

of course, I forget to mention it

```
ip rule add  fwmark 1 table gw2.1

ip rule add  fwmark 2 table gw2.26
```

----------

