Redirecting flows among one machine's interfaces
Hi all,
I am not very familiar with iptables, but what I am trying to achieve is the following:
I have a machine with two interfaces (let's say if1 -- with ip address a.b.c.d -- and if2 -- with ip address x.y.z.w) both connected to the internet. One of the two interfaces is the default interface (e.g., if1). Whatever flow is initiated towards a destination in the Internet is going out if1. I want to use iptables in order to redirect some of the flows to if2. Is there a way to do this using iptables?
I have tried some rules (e.g., iptables -A FORWARD -d k.l.m.n -o if2 -j ACCEPT // where k.l.m.n is the destination ip address of the flow I want to redirect) but with no luck. From what I could figure out the rule FORWARD is mainly for packets that go through the machine and not that much for packets originating from the machine. Is there a way to perform what I want with iptables ? Given that this is possible, another question is weather iptables changing the source ip address from a.b.c.d to x.y.z.w or not.
Thanks a lot and hopefully someone can seed light on this.
Best,
Kostas
Quoted 11 lines · Kostas Pelechrinis
Hi all,
I am not very familiar with iptables, but what I am trying to achieve is the following:
I have a machine with two interfaces (let's say if1 -- with ip address a.b.c.d -- and if2 -- with ip address x.y.z.w) both connected to the internet. One of the two interfaces is the default interface (e.g., if1). Whatever flow is initiated towards a destination in the Internet is going out if1. I want to use iptables in order to redirect some of the flows to if2. Is there a way to do this using iptables?
Using -j MARK and iproute2 policy routing.
Quoted 3 lines
I have tried some rules (e.g., iptables -A FORWARD -d k.l.m.n -o if2 -j ACCEPT // where k.l.m.n is the destination ip address of the flow I want to redirect) but with no luck.
Well guess why: it _tests_ whether the outgoing interface is (already) if2.
Thanks a lot. I am checking iproute2 now but can you give any pointers/hints.
Thanks a lot,
Kostas
Quoted 39 lines · Jan Engelhardt
From: Jan Engelhardt <[email protected]>
Subject: Re: Redirecting flows among one machine's interfaces
To: "Kostas Pelechrinis" <[email protected]>
Cc: [email protected]
Date: Tuesday, November 9, 2010, 6:33 PMQuoted 3 lines · Kostas Pelechrinis
Hi all,
I am not very familiar with iptables, but what I am
trying to achieve
Quoted 3 lines
is the following:
I have a machine with two interfaces (let's say if1 --
with ip address
Quoted 1 line
a.b.c.d -- and if2 -- with ip address x.y.z.w) both
connected to the
Quoted 1 line
internet. One of the two interfaces is the default
interface (e.g.,
Quoted 1 line
if1). Whatever flow is initiated towards a
destination in the Internet
Quoted 1 line
is going out if1. I want to use iptables in order
to redirect some of
Quoted 1 line
the flows to if2. Is there a way to do this using
iptables?
Using -j MARK and iproute2 policy routing.
Quoted 1 line
I have tried some rules (e.g., iptables -A FORWARD -d
k.l.m.n -o if2 -j
Quoted 1 line
ACCEPT // where k.l.m.n is the destination ip address
of the flow I
Quoted 1 line
want to redirect) but with no luck.
Well guess why: it _tests_ whether the outgoing interface is (already)
if2.
Hello,
Quoted 7 lines · Kostas Pelechrinis
I have a machine with two interfaces (let's say if1 -- with ip address a.b.c.d -- and if2 -- with ip address x.y.z.w) both connected to the internet. One of the two interfaces is the default interface (e.g., if1).
Whatever flow is initiated towards a destination in the Internet is
going out if1. I want to use iptables in order to redirect some of the flows to if2. Is there a way to do this using iptables?
This is a FAQ. You will find useful information in the Linux Advanced Routing and Traffic Control (LARTC) HOWTO.
Quoted 5 lines
I have tried some rules (e.g., iptables -A FORWARD -d k.l.m.n -o if2 -j ACCEPT // where k.l.m.n is the destination ip address of the flow I want to redirect) but with no luck. From what I could figure out the rule FORWARD is mainly for packets that go through the machine and not that much for packets originating from the machine.
Indeed. Besides, the rule justs matches packets going out through the interface if2. It does not have any effect on the routing decision, which took place before the FORWARD chain.
Quoted 2 lines
another question is weather iptables changing the source ip address from a.b.c.d to x.y.z.w or not.
Iptables does not change the source address implicitly as a result of a rerouting decision. You have to do it explicitly with rules using SNAT or MASQUERADE.