Welcome to Soft32 Linux Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

raw sockets and UDP broadcast : a strange phenomenon

 
   Soft32 Home -> Linux -> Networking RSS
Next:  enable/disable serial port dynamically  
Author Message
markryde

External


Since: Nov 27, 2008
Posts: 8



(Msg. 1) Posted: Tue Jul 21, 2009 7:24 am
Post subject: raw sockets and UDP broadcast : a strange phenomenon
Archived from groups: comp>os>linux>networking (more info?)

Hello,

I noticed something which seems to me strange regarding
raw sockets and broadcasts:

I ran a short program which created a simple raw socket with
UDP protocol. It achieves this by calling:
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);

(full code is below, it is a very short program)
Then I called recv(), I noticed that I get many packets.
Looking in a sniffer, and using IP_PKTINFO socket option, I found out
that
the packets I get are broadcast UDP packets.
When I change the protocol type to other types instead of UDP, for
example IPPROTO_IP/
IPPROTO_ICMP/IPPROTO_RAW, by:
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
or
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
or
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
I don't see traffic **at all**. I mean recv() does not get any
packets.

Looking further into the sniff, I see that these broadcasts are
for example from a DHCP daemon. (bootstrap protocol).
What I don't understand is why at all these packets are received
by recv() of a raw packet (even if the protocol is IPPROTO_UDP)?
these packets, which I see in the sniffer, are, as far as I
understand, UDP broadcast packets,
and have **nothing** to do with raw packets at all!

Any ideas?
Here is the full short program:
// raw_server.c

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <netinet/icmp6.h>
#include <netinet/ip_icmp.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netdb.h>
#include <errno.h>
#include <locale.h>
#include <sys/utsname.h>
#include <linux/types.h>
#include <linux/errqueue.h>
#include <sys/types.h>

#define MAX_BUF 1024

int main(int argc, char **argv)
{
int sd;
int i;
int res;
char buf[MAX_BUF];

//sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
//sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
// sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
sd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);

if (sd<0)
perror("socket");
while (1)
{
res = recv(sd,buf, sizeof(buf),0);
if (res<0)
perror("recv");
else
printf("res=%d recv ok\n",res);
}
}
Regards,
MR
Back to top
Login to vote
David Schwartz

External


Since: Apr 25, 2007
Posts: 134



(Msg. 2) Posted: Tue Jul 21, 2009 4:18 pm
Post subject: Re: raw sockets and UDP broadcast : a strange phenomenon [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jul 21, 7:24 am, "markr...@gmail.com" <markr... DeleteThis @gmail.com> wrote:

> What I don't understand is why at all these packets are received
> by recv() of a raw packet (even if the protocol is IPPROTO_UDP)?
> these packets, which I see in the sniffer, are, as far as I
> understand, UDP broadcast packets,
> and have **nothing** to do with raw packets at all!

You seem to have an incorrect conceptual understanding of what raw
means. Raw simply means not processed by the kernel. A UDP packet, if
unprocessed by the kernel, is also raw. There is not some special type
of packet data that reflects a raw packet -- if there was, you
couldn't use the raw interface to create UDP packets at all.

Or perhaps I'm misunderstanding you. Since you can create UDP
broadcast packets with a raw socket, why would you say that UDP
broadcast packets "have nothing to do with raw packets at all"?

DS
Back to top
Login to vote
Maxwell Lol

External


Since: Nov 11, 2008
Posts: 60



(Msg. 3) Posted: Tue Jul 21, 2009 8:46 pm
Post subject: Re: raw sockets and UDP broadcast : a strange phenomenon [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"markryde@gmail.com" <markryde.TakeThisOut@gmail.com> writes:

> I ran a short program which created a simple raw socket with
> UDP protocol. It achieves this by calling:
> sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);

> I found out
> that
> the packets I get are broadcast UDP packets.

From

RAW(7) Linux Programmer's Manual RAW(7)
DESCRIPTION
Raw sockets allow new IPv4 protocols to be implemented in user space.
A raw socket receives or sends the raw datagram not including link
level headers.


So when you send a packet, you can make the packet contain any IP
address you want. When you receive a packet, it will contain the full
IP header.


Perhaps you are thinking of putting a socket into promiscuous mode, so
you can see packets addressed to other machines besides your own?

try packet(7) and setsockopt(2)
Or even better, find some example source code.
Look for PACKET_MR_PROMISC

Note that most switches make this tricky to do. You need a hub, or
port mirroring, or ettercap.
Back to top
Login to vote
Display posts from previous:   
Related Topics:
WiFi browsing for Windows, but not for Linux? - That seems to be the problem a colleague of mine is currently experiencing in France. Details here: ..

Setting up iwlwifi and wpa supplicant on Fedora 7 - Could someone give me the steps (script) to setup iwlwifi for WPA. I have iwlwifi installed from the Fedora..

VPN OpenSwan&Xl2tp problem with big udp packets - Hi all, I'm having a problem with my vpn server using openswan et xl2tp on a gentoo linux. All the tcp traffic seems....

Hacked, now trying to disinfect - Yeah, i know, it can't happen in Linux. But it has been happening to our work servers for several months. Due to poor....

[Samba] Invalid ICMP type 3 to broadcast - My syslog is filling up with the following messages. If i shutdown samba the messages cease. Dec 17 08:48:24..

Add .splice_read to sockets? - Hi! Is there any plans to make splice(socket, ..., pipe, ...) work? Thanks! -- Paul P 'Stingray' Komkoff Jr //..
       Soft32 Home -> Linux -> Networking All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Categories:
 Windows
  Linux
 Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]