LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-18-2008, 10:27 AM   #1
algol68
LQ Newbie
 
Registered: Dec 2008
Location: New Hampshire
Distribution: Ubuntu
Posts: 3

Rep: Reputation: 0
Send packets to yourself, out one interface and in another


I'm developing software to be used for testing hardware, so I want to do something that would normally be, well, wrong. I have a host with multiple network interfaces all connected to a single switch, and I want to send packets out interface A and back in interface B. (And then out A and in C, etc.) If you just do something straightforward like pinging, the network stack figures out that the packets are going to itself and "helpfully" doesn't bother sending any packets over the wire.

I believe that when IP addresses are translated to MAC addresses, it figures out that the destination MAC address is an interface on itself and the kernel takes the packet and handles it as if it had just been received on the incoming interface.

I'm not very informed about iptables but it doesn't seem to me that iptables would help at all here. I think this would require sending and receiving packets below the IP level.

The problem is mostly straightforward if I have an external device on the switch that I can use as a ping target. However this test would be more valuable and useful if it did not involve an external device.

I wonder if anybody has a clever idea how to do this? Could I be mistaken and some magic iptables incantation do the trick? Can you point me to how I could write a program that sends and receives packets below the IP level?

Thanks very much.
 
Old 12-18-2008, 01:46 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
The raw sockets interface allows for this.
Code:
    int fd;
    fd=socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
There is a fair bit of help on the net; Google 'raw socket', or search these forums for details.
Note that using raw sockets requires root privileges, since none of the content of the packets can be vetted by a trusted stack.

Also, you should/may be able to coerce an existing IP stack to route to different interfaces depending on the destination address. Probably requires using separate subnets. Maybe someone else can provide details on this.


--- rod.

Last edited by theNbomr; 12-18-2008 at 01:49 PM.
 
Old 12-22-2008, 01:21 PM   #3
algol68
LQ Newbie
 
Registered: Dec 2008
Location: New Hampshire
Distribution: Ubuntu
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for the tip on raw sockets. I'll look into that. However it looks like raw sockets are still going through IP.

Unless I'm quite mistaken, getting an IP stack to route packets out a particular interface won't prevent the lower level of the stack from looking at the destination MAC, figuring out that the MAC is on the same host, and optimizing away actually sending the packet over the wire.
 
Old 12-22-2008, 02:02 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
According to the Linux man page for the socket interface, there is the possibility of binding a socket to a specified interface.
I was going to try to post an concise example of how to accomplish this, but as it is slightly less than trivial, I will instead refer you to the bacnet4linux project on sourceforge. In there, you can find code to attach a raw socket to a specified interface. See, in particular, the files main.c, net.c, and ethernet.c. I did not write that code, but have adapted and used the package extensively, and can say with complete certainty that it does work.
I think that using the raw sockets interface allows you to send datagrams that may not conform to the usual expectations of formatting (which is why it requires root privileges), and the usual 'optimization' is not performed.

--- rod.

Last edited by theNbomr; 12-22-2008 at 02:06 PM.
 
  


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
unable to send packets cranium2004 Linux - Networking 2 02-05-2005 10:28 PM
How do you send packets in ASM? Qwirt Programming 4 11-13-2004 03:11 PM
send packets at network Jamesminh Linux - Networking 0 10-01-2004 02:38 AM
How to send icmp packets on a particular interface? dravya Programming 3 07-29-2004 04:15 PM
recieve and send IP packets! Farhang Linux - Networking 1 07-25-2004 02:47 PM

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

All times are GMT -5. The time now is 07:42 AM.

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