I am have a Linux machine with 2 network interface cards, I need to use one for incoming packets, other for outgoing packets.
-
Using a different address when sending would break all upper-layer protocols, except maybe SCTP.– BatchyXApr 3, 2014 at 6:51
-
@BatchyX Is this possible anyhow to use one interface for incoming packets and other for outgoing ..I am a newbie so could you please hint some factors– AnkitApr 3, 2014 at 6:57
-
If you use only one IP address, this is almost trivial to do.– BatchyXApr 3, 2014 at 7:02
-
But... if your if2 sends out packets, the server will reply to ip2. How do you want to tell the server to reply to ip1 instead?– KonerakApr 3, 2014 at 7:05
-
@BatchyX can changing routing table could be the solution .please take a look at it .and reply to me .today is the last day for me to do this.stackoverflow.com/questions/8531835/…– AnkitApr 3, 2014 at 7:09
1 Answer
If you accept to use a single IP/prefix on your machine (let's name it X.X.X.X/Y, with gateway G.G.G.G), then it can be trivially be done.
Add your address to
eth0
, but with a full prefix subnet.e.g.
ip addr add X.X.X.X/32 dev eth0
Do not set an address on eth1. If there is one, clear it with
ip -4 addr flush dev eth1
Add the subnet and gateway routes via
eth1
ip route add X.X.X.X/Y dev eth1
ip route add default via G.G.G.G dev eth1
If on Ubuntu, or on other Linux distributions that enables Reverse Path Filtering by default, disable it, as they assume and enforce symmetric routing.
sysctl -w net.ipv4.conf.eth0.rp_filter = 0
sysctl -w net.ipv4.conf.eth1.rp_filter = 0
If both your network interfaces are connected to a switch, then you will need to not answer ARP on eth1. Since we added the IP address to
eth0
, we can just setarp_ignore
oneth1
to 1 (only answer ARP foreth1
addresses) or 8 (don't answer anything).sysctl -w net.ipv4.conf.eth1.arp_ignore = 8
Once this configuration is done, there will be only one traffic that will flow in the reverse order: If the kernel receives an ARP request for X.X.X.X from eth0
, then it will reply using eth0
.