LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 04-15-2016, 01:29 AM   #1
pjlnaveen
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Rep: Reputation: Disabled
Linux Network stack behavior with SO_BINDTODEVICE


Hi,

In my Linux target I have an interface, say if0. The interface IP for this if0 is 128.0.0.16. There is a process P1, which has opened a raw socket (AF_INET, SOCK_RAW, xyz_protocol). It has done a SO_BINDTODEVICE to this interface if0. The socket also binds to this local interface IP address (128.0.0.16). Now this process (thru' this socket) is able to recvfrom() all packets arriving form a different Linux machine to this interface destined for this xyz_protocol. Everything is fine until now.

Now, there is another local process P2 running in the same machine as process P1. This process P2 also has a requirement to send similar xyz_protocol packets to this process P1. We don't want to create a separate IPC channel between these two processes. Rather we want to use the same socket infra. So, from process P2, I am again opening a similar socket (AF_INET, SOCK_RAW, xyz_protocol). I also do the same SO_BINDTODEVICE to this interface if0. The socket also binds to the same local interface IP address, as done by P1. The bind() is OK/success.

Now I send a packet from process P2 with both source and destination IP as 128.0.0.16. I am expecting the process P1 to receive the packet, as 128.0.0.16 is a local interface IP and the IP layer is supposed to loop it back to the higher application waiting for this xyz_protocol. But P1 doesn't seem to receive it.

My question is, is this is the right way of doing it. Of course it is not, as it is not working... Then how else can P2's packet can be made to be received by P1.

If destination & source address match, what is the logic inside IP stack. Will it blindly loop it back to higher layers?

In process P1 we make the socket to do a SO_BINDTODEVICE to if0? Is that the reason why is not able to receive the local packets from process P2. I read through some linux man pages. It says that SO_BINDTODEVICE will make the socket to receive packets only from the associated interface. Maybe that's why its not honoring the local packets from process P2?

Regards
Peter
 
Old 04-16-2016, 10:40 AM   #2
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 509

Rep: Reputation: 203Reputation: 203Reputation: 203
Hi there,

Is there a reason you are binding to a specific interface, and not just to the IP?

Yes, as you mentioned, SO_BINDTODEVICE means "If a socket is bound to an interface, only packets received from that particular interface are processed by the socket." - see http://linux.die.net/man/7/socket.

Local traffic is usually not sent out onto the network, so these packets would not be received via the (physical) interface you have bound to.

There are some settings which, together with appropriate routing, could be used to force traffic out onto the wire between 2 local interfaces, namely:

Code:
sysctl -w net.ipv4.conf.eth0.accept_local=1
sysctl -w net.ipv4.conf.all.rp_filter=2
See the doc at https://www.kernel.org/doc/Documenta.../ip-sysctl.txt

I don't think there is any way of achieving the same for a single interface, though.
 
1 members found this post helpful.
Old 04-18-2016, 10:05 AM   #3
pjlnaveen
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi Cliffordw,

Thanks for the clarification. SO_BINDTODEVICE to the interface is in a piece of code which i don't own. The owner is of the opinion that this socket should receive the traffic from that interface *ONLY*. Hence this SO_BINDTODEVICE has been done.

Binding the interface IP to socket will only ensure that traffic leaving the system from this socket will have src IP as this IP.
However, it will not ensure that this socket will receive traffic only from this interface.. Am i right?

Looks like I don't have any option..

Regards
Peter N
 
Old 04-18-2016, 11:05 AM   #4
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 509

Rep: Reputation: 203Reputation: 203Reputation: 203
Hi Peter,

Quote:
Originally Posted by pjlnaveen View Post
Binding the interface IP to socket will only ensure that traffic leaving the system from this socket will have src IP as this IP.
However, it will not ensure that this socket will receive traffic only from this interface.. Am i right?
No, as far as I know, SO_BINDTODEVICE means "If a socket is bound to an interface, only packets received from that particular interface are processed by the socket." - see http://linux.die.net/man/7/socket.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SO_BINDTODEVICE problem nicomedia41 Linux - Networking 3 08-03-2010 07:10 AM
Network stack behavior handling packets on multiple cores zman2245 Linux - Kernel 5 03-24-2010 12:41 PM
How Linux Network Stack Works....Please Guide!!! gauravholey Linux - Networking 1 06-03-2009 09:17 PM
using linux network stack through other programs fotisaueb Linux - Networking 1 07-02-2005 08:39 PM
Linux Network Stack. tech1222 Linux - Networking 0 09-15-2004 08:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 07:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration