* determine outgoing interface (eth0,eth1) for a packet according to the dest IP
@ 2006-04-25 7:31 John Que
2006-04-25 7:43 ` Andi Kleen
0 siblings, 1 reply; 9+ messages in thread
From: John Que @ 2006-04-25 7:31 UTC (permalink / raw)
To: netdev
Hello,
What is the right way to determine on which interface card
(eth0 or eth1) will a packet be sent (according to the dest IP)?
I have a machine with two NICS (eth0 and eth1).
I have 2 different gateways.
I need to know on which interface (eth0 or eth1) will the
packet be send according to the dest IP.
I want to do in in a User Space function.
I think of one way to do this is:
Parse the routing table (/proc/net/route) as is done in
net-tools. (to be more specific rprint_fib() in lib/inet_gr.c
of net-tools.
Than according to the IP address and the netmask check if the
dest IP is for one gatway or the second.
If not - than it is the default gateway.
Is there another way ? Is it the correct way ?
Regards,
John
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: determine outgoing interface (eth0,eth1) for a packet according to the dest IP
2006-04-25 7:31 determine outgoing interface (eth0,eth1) for a packet according to the dest IP John Que
@ 2006-04-25 7:43 ` Andi Kleen
2006-04-25 7:58 ` David S. Miller
2006-04-25 14:44 ` John Que
0 siblings, 2 replies; 9+ messages in thread
From: Andi Kleen @ 2006-04-25 7:43 UTC (permalink / raw)
To: John Que; +Cc: netdev
On Tuesday 25 April 2006 09:31, John Que wrote:
> Hello,
> What is the right way to determine on which interface card
> (eth0 or eth1) will a packet be sent (according to the dest IP)?
You can send a rtnetlink RTM_GETROUTE message to ask the kernel.
Result is the interface index in RTA_OIF, which can be converted
into a name.
-Andi
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: determine outgoing interface (eth0,eth1) for a packet according to the dest IP
2006-04-25 7:43 ` Andi Kleen
@ 2006-04-25 7:58 ` David S. Miller
2006-04-25 14:44 ` John Que
1 sibling, 0 replies; 9+ messages in thread
From: David S. Miller @ 2006-04-25 7:58 UTC (permalink / raw)
To: ak; +Cc: qwejohn, netdev
From: Andi Kleen <ak@suse.de>
Date: Tue, 25 Apr 2006 09:43:49 +0200
> On Tuesday 25 April 2006 09:31, John Que wrote:
> > Hello,
> > What is the right way to determine on which interface card
> > (eth0 or eth1) will a packet be sent (according to the dest IP)?
>
> You can send a rtnetlink RTM_GETROUTE message to ask the kernel.
> Result is the interface index in RTA_OIF, which can be converted
> into a name.
That scheme does not handle netfilter nor packet scheduler
classifier mangling and redirection of the packet.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: determine outgoing interface (eth0,eth1) for a packet according to the dest IP
2006-04-25 7:43 ` Andi Kleen
2006-04-25 7:58 ` David S. Miller
@ 2006-04-25 14:44 ` John Que
2006-04-25 14:48 ` Andi Kleen
1 sibling, 1 reply; 9+ messages in thread
From: John Que @ 2006-04-25 14:44 UTC (permalink / raw)
To: Andi Kleen; +Cc: netdev
Thanks a lot !
I had tried the sending RTM_GETROUTE message using a NETLINK_ROUTE
socket in a User Space program and it went OK.
It gaves correct routing struct which I could parse.
In fact it gave the rotuing table.
But in sending that message I did not specify a certain
dest IP.
Consider the follwing simple scenario: I have 2 gateways
(one on eth0,one on eth1), and I am sending
a packet to some dest IP ; I want to know according to
that ip on which interface (or gw) it will be out
But where do I specify that certain dest IP ?
Regards,
John
On 4/25/06, Andi Kleen <ak@suse.de> wrote:
> On Tuesday 25 April 2006 09:31, John Que wrote:
> > Hello,
> > What is the right way to determine on which interface card
> > (eth0 or eth1) will a packet be sent (according to the dest IP)?
>
> You can send a rtnetlink RTM_GETROUTE message to ask the kernel.
> Result is the interface index in RTA_OIF, which can be converted
> into a name.
>
> -Andi
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: determine outgoing interface (eth0,eth1) for a packet according to the dest IP
2006-04-25 14:44 ` John Que
@ 2006-04-25 14:48 ` Andi Kleen
2006-04-26 10:24 ` John Que
0 siblings, 1 reply; 9+ messages in thread
From: Andi Kleen @ 2006-04-25 14:48 UTC (permalink / raw)
To: John Que; +Cc: netdev
On Tuesday 25 April 2006 16:44, John Que wrote:
> Thanks a lot !
>
> I had tried the sending RTM_GETROUTE message using a NETLINK_ROUTE
> socket in a User Space program and it went OK.
>
> It gaves correct routing struct which I could parse.
> In fact it gave the rotuing table.
> But in sending that message I did not specify a certain
> dest IP.
> Consider the follwing simple scenario: I have 2 gateways
> (one on eth0,one on eth1), and I am sending
> a packet to some dest IP ; I want to know according to
> that ip on which interface (or gw) it will be out
>
> But where do I specify that certain dest IP ?
You add RTA_SRC and RTA_DST attributes to the query.
-Andi
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: determine outgoing interface (eth0,eth1) for a packet according to the dest IP
2006-04-25 14:48 ` Andi Kleen
@ 2006-04-26 10:24 ` John Que
2006-04-26 13:58 ` Herbert Xu
0 siblings, 1 reply; 9+ messages in thread
From: John Que @ 2006-04-26 10:24 UTC (permalink / raw)
To: Andi Kleen; +Cc: netdev
Hello,
Does anybody knows where suh a code example
which gets routing info according to the destination IP using
GETROUTE netlink sockets
can be found ?
I had spent time trying to write something which
uses RTA_SRC and RTA_DST and got some problems.
(I probably made something stupid.)
I googled for such examples and did not get much.
I saw that the zebra project and iproute uses
netlinks but it is complex.,
TIA
John
On 4/25/06, Andi Kleen <ak@suse.de> wrote:
> On Tuesday 25 April 2006 16:44, John Que wrote:
> > Thanks a lot !
> >
> > I had tried the sending RTM_GETROUTE message using a NETLINK_ROUTE
> > socket in a User Space program and it went OK.
> >
> > It gaves correct routing struct which I could parse.
> > In fact it gave the rotuing table.
> > But in sending that message I did not specify a certain
> > dest IP.
> > Consider the follwing simple scenario: I have 2 gateways
> > (one on eth0,one on eth1), and I am sending
> > a packet to some dest IP ; I want to know according to
> > that ip on which interface (or gw) it will be out
> >
> > But where do I specify that certain dest IP ?
>
> You add RTA_SRC and RTA_DST attributes to the query.
>
> -Andi
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: determine outgoing interface (eth0,eth1) for a packet according to the dest IP
2006-04-26 10:24 ` John Que
@ 2006-04-26 13:58 ` Herbert Xu
2006-04-30 9:17 ` Lennert Buytenhek
0 siblings, 1 reply; 9+ messages in thread
From: Herbert Xu @ 2006-04-26 13:58 UTC (permalink / raw)
To: John Que; +Cc: ak, netdev
John Que <qwejohn@gmail.com> wrote:
>
> I had spent time trying to write something which
> uses RTA_SRC and RTA_DST and got some problems.
> (I probably made something stupid.)
> I googled for such examples and did not get much.
> I saw that the zebra project and iproute uses
> netlinks but it is complex.,
Have a look at
http://gondor.apana.org.au/~herbert/findsaddr-linux.c
which I wrote for traceroute some years back. It should be easy
to adapt to get the interface instead of the source address.
Cheers,
--
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: determine outgoing interface (eth0,eth1) for a packet according to the dest IP
2006-04-26 13:58 ` Herbert Xu
@ 2006-04-30 9:17 ` Lennert Buytenhek
2006-04-30 12:55 ` Herbert Xu
0 siblings, 1 reply; 9+ messages in thread
From: Lennert Buytenhek @ 2006-04-30 9:17 UTC (permalink / raw)
To: Herbert Xu; +Cc: John Que, ak, netdev
On Wed, Apr 26, 2006 at 11:58:00PM +1000, Herbert Xu wrote:
> > I had spent time trying to write something which
> > uses RTA_SRC and RTA_DST and got some problems.
> > (I probably made something stupid.)
> > I googled for such examples and did not get much.
> > I saw that the zebra project and iproute uses
> > netlinks but it is complex.,
>
> Have a look at
>
> http://gondor.apana.org.au/~herbert/findsaddr-linux.c
>
> which I wrote for traceroute some years back.
A fragile and ugly but easier/shorter way of getting the source address
(which I've used a couple of times in the past) is to open a SOCK_DGRAM
socket, connect() it to the intended destination, and then do
getsockname().
(That doesn't give you the interface, though.)
--L
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2006-04-30 12:55 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-25 7:31 determine outgoing interface (eth0,eth1) for a packet according to the dest IP John Que
2006-04-25 7:43 ` Andi Kleen
2006-04-25 7:58 ` David S. Miller
2006-04-25 14:44 ` John Que
2006-04-25 14:48 ` Andi Kleen
2006-04-26 10:24 ` John Que
2006-04-26 13:58 ` Herbert Xu
2006-04-30 9:17 ` Lennert Buytenhek
2006-04-30 12:55 ` Herbert Xu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).