How do we determine IP Address of interface the packet arrived on

Jeff Haran Jeff.Haran at citrix.com
Wed Sep 24 12:34:37 EDT 2014


> -----Original Message-----
> From: kernelnewbies-bounces at kernelnewbies.org [mailto:kernelnewbies-
> bounces at kernelnewbies.org] On Behalf Of Vlad Dogaru
> Sent: Wednesday, September 24, 2014 12:42 AM
> To: Maninder Singh
> Cc: kernelnewbies at kernelnewbies.org
> Subject: Re: How do we determine IP Address of interface the packet arrived on
> 
> On Wed, Sep 24, 2014 at 10:19:34AM +0530, Maninder Singh wrote:
> > Hi,
> >
> > Each time a packet arrives at a router in a subnet during transit, I
> > need to determine the IP address of the incoming interface of the
> > router for that packet. I have a netfilter kernel module running at
> > each router but I dont understand which data structure will give me
> > thw IP address. Can someone please suggest how shall I get that?
> 
> An interface can have more than one IP address, so your question is a little
> strange.
> 
> However, you can find out all the IPv4 addresses a particular device has by
> iterating through dev->ip_ptr->ifa_list.
> 
> Hope this helps,
> Vlad

If what you are trying to determine is whether the incoming packet is destined to the router this code is running on, one option would be to do a route lookup on the destination address in the packet via a call to inet_addr_type(). If it returns RTN_LOCAL, then it will get routed locally. That might be easier that walking the ifa_list. It should also cover the case where the DA is an IP address assigned to another interface on the router that happened to get routed to a different subnet and thus got received on the "wrong" interface.

Of course, things like ip rules and alternative routing tables can make all this much more complicated.

Whether this works for you or not will depends what you are trying to accomplish here,

Jeff Haran




More information about the Kernelnewbies mailing list