LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-24-2008, 10:22 AM   #16
xamitksx
LQ Newbie
 
Registered: May 2008
Posts: 5

Rep: Reputation: 0

Today i tried
BIND (though bind call was succesful )too but that also not working
-------------------------
As per man pages only sa.sll_protocol and sa.sll_ifindex are required for BIND
correct me if i am wrong ?
also the print of sa.sll_ifindex is coming out 0 , i dont konw why???
Actually I am using Bonded interface where bond2 is bonded of eth4 and eth 5
Am i using it in wrong way ??

memset(&sa, 0, sizeof(sa));
strncpy(ifr.ifr_name,"bond2",strlen("bond2")+1);
sa.sll_family = AF_PACKET;
interfaceId = ioctl(g_frame_read_sockfd ,SIOCGIFINDEX,&ifr);
printf("INTERFACE ID =%d\n",interfaceId); /*Its giving 0 as OUTPUT of Inteergfasc id */
sa.sll_protocol = htons(ETH_P_ALL);
// sa.sll_hatype = 1;
// sa.sll_pkttype = PACKET_HOST;
//sa.sll_halen = 18;
sa.sll_ifindex = interfaceId;


if ( bind( g_frame_read_sockfd , (struct sockaddr*) &sa, sizeof(sa)) < 0 )
{
printf("BIND failed \n");
}
 
Old 05-24-2008, 10:26 AM   #17
xamitksx
LQ Newbie
 
Registered: May 2008
Posts: 5

Rep: Reputation: 0
Also ORING is not working too as

ETH_P_IP 0x0800
ETH_P_ARP 0x0806
SO OR will take it as ETH_P_ARP and it is picking only ARP packets in that case
 
Old 05-25-2008, 06:33 AM   #18
baddad
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Post My approach

Hi xamitksx!

The IOCTL does not return the actual IF-Id , you can find it in the ifreq structure.
This works for me:

Code:
int SOCKET_ID = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
	if(SOCKET_ID == -1)
	{
		perror("Error 20001\nCould not create RAW Ethernet Socket!\n");
		exit(EXIT_FAILURE);
	}

	//obtaining IF index
	struct ifreq ifr;
	strncpy(ifr.ifr_name,IF_NAME,16);
	if(ioctl(SOCKET_ID,SIOCGIFINDEX,&ifr) != 0)
	{
		printf("Error 20009\nUnable to obtain IF index!\n");
		exit(EXIT_FAILURE);
	}
	SOCKET.IF_INDEX = ifr.ifr_ifindex;


	//obtaining MAC address
	strncpy(ifr.ifr_name,IF_NAME,16);
	if(ioctl(SOCKET_ID,SIOCGIFHWADDR,&ifr) != 0)
	{
		printf("Error 20010\nUnable to obtain MAC address!\n");
		exit(EXIT_FAILURE);
	}
	memcpy(SOCKET.MY_MAC,ifr.ifr_hwaddr.sa_data,6);

	//socket config
	struct sockaddr_ll socket_address;
	socket_address.sll_family = AF_PACKET;
	socket_address.sll_protocol = htons(ETH_P_ALL);
	socket_address.sll_ifindex = SOCKET.IF_INDEX;
	//socket_address.sll_pkttype = PACKET_OTHERHOST|PACKET_BROADCAST|PACKET_MULTICAST|PACKET_HOST;
	//socket_address.sll_halen = ETH_ALEN;
	//socket_address.sll_hatype = 0x0000; 

	//bind socket to NIC by IF_INDEX
	int bind_res = bind(SOCKET_ID,(struct sockaddr*)&socket_address,sizeof(socket_address));
	if(bind_res == -1)
	{
		printf("Error 20008\nCould not bind Socket to Device!");
		exit(EXIT_FAILURE);
	}

	//set promiscious mode manually
	strncpy(ifr.ifr_name,IF_NAME,16);
	ioctl(SOCKET_ID,SIOCGIFFLAGS,&ifr);
	//ifr.ifr_flags |= IFF_ALLMULTI;
	ifr.ifr_flags |= IFF_PROMISC;
	ioctl(SOCKET_ID,SIOCSIFFLAGS,&ifr);
For the actual receiver I manually filter out the frames with the same SRC MAC than the local one with
Code:
if(memcmp(DATA_START+6,MY_MAC,6) != 0)

Hope I could help!

CU

PS: and... - your absolutely right about the OR-ing!

Last edited by baddad; 05-25-2008 at 06:35 AM.
 
Old 05-27-2008, 01:16 AM   #19
xamitksx
LQ Newbie
 
Registered: May 2008
Posts: 5

Rep: Reputation: 0
HI
baddad and theNbomr

Thanks for your help , its working now with bind call the error was

interfaceId = ioctl(g_frame_read_sockfd ,SIOCGIFINDEX,&ifr);
printf("INTERFACE ID =%d\n",interfaceId); /*Its giving 0 as OUTPUT of Inteergfasc id */
sa.sll_protocol = htons(ETH_P_ALL);
sa.sll_ifindex = interfaceId;

asingment of sa.sll_ifindex = interfaceId;
it should be sa.sll_ifindex = ifr.ifr_ifindex ;

It worked
Really I appreciate Your help in solving this problem
 
Old 07-13-2010, 10:48 AM   #20
falstaff
LQ Newbie
 
Registered: Mar 2003
Posts: 3

Rep: Reputation: 0
Arrow

I know, this is old, but I found it with google. I had the opposite problem: I wanted all packages, even the sent ones. But I bound it to a specific Ethernet Type (eg. 0x1234). Because of this, the Kernel seems to Filter outgoing packages wrong (incoming are filtered correctly). I then used ETH_P_ALL when bind.

To do the filtering by Ethernet Type I used a BPF (see here)

Cheers
falstaff

Last edited by falstaff; 07-13-2010 at 10:49 AM.
 
Old 05-22-2012, 07:09 AM   #21
reld
LQ Newbie
 
Registered: May 2012
Posts: 1

Rep: Reputation: Disabled
Google found me this thread, so I post my solution here. With recvfrom I managed to filter for PACKET_OUTGOING. This is more ellegant and reliable than checking the own source MAC.

Code:
        struct sockaddr_ll addr;
        socklen_t addr_len = sizeof(addr);
        int length = recvfrom(sock, packet, packetsize, 0, (struct sockaddr*)&addr, &addr_len);
        if (length <= 0) {
            perror("recvfrom failed");
            exit(0);
        }
        if (addr.sll_pkttype == PACKET_OUTGOING) continue; // drop it
 
Old 05-22-2012, 11:23 AM   #22
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by reld View Post
Google found me this thread, so I post my solution here. With recvfrom I managed to filter for PACKET_OUTGOING. This is more ellegant and reliable than checking the own source MAC.

Code:
        struct sockaddr_ll addr;
        socklen_t addr_len = sizeof(addr);
        int length = recvfrom(sock, packet, packetsize, 0, (struct sockaddr*)&addr, &addr_len);
        if (length <= 0) {
            perror("recvfrom failed");
            exit(0);
        }
        if (addr.sll_pkttype == PACKET_OUTGOING) continue; // drop it
The BPF solution is preferable - it runs in kernel space, and therefore you don't need to take the burden of a memcpy before deciding to drop the packet.
 
Old 11-27-2013, 12:44 PM   #23
selvamv
LQ Newbie
 
Registered: Nov 2013
Posts: 1

Rep: Reputation: Disabled
Thanks!!!

Thanks this thread helped to solve the problem which i was facing!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Raw socket programming with C arabindav Programming 17 06-09-2011 11:58 AM
Raw socket programming homePBX Linux - Networking 6 05-02-2008 12:25 PM
raw socket/ip packet help shouup Programming 14 04-24-2006 04:54 PM
Socket Raw linuxanswer Programming 1 04-01-2004 09:43 PM
raw socket permition moonloader Linux - Networking 2 12-08-2003 09:20 AM


All times are GMT -5. The time now is 01:21 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration