LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-20-2009, 03:19 AM   #1
steve_austin
LQ Newbie
 
Registered: Mar 2009
Posts: 5

Rep: Reputation: 0
Accessing MAC header from a network packet


I am trying to access and obtain the destination MAC address of a captured network packet. I am using the following kernel module which uses netfilter hooks to process the packets.

Code:
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
static struct nf_hook_ops netfilter_ops_in; /* NF_IP_PRE_ROUTING */
/* Function prototype in <linux/netfilter> */
unsigned int main_hook(unsigned int hooknum,  
                  struct sk_buff **skb,
                  const struct net_device *in,
                  const struct net_device *out,
                  int (*okfn)(struct sk_buff*))
{
struct sk_buff* sk;
sk = (*skb);

unsigned char* rawp;
rawp = sk->mac.raw; //Accessing the MAC Header
return NF_ACCEPT;
}
int init_module()
{
        netfilter_ops_in.hook                   =       main_hook;
        netfilter_ops_in.pf                     =       PF_INET;
        netfilter_ops_in.hooknum                =       NF_IP_PRE_ROUTING;
        netfilter_ops_in.priority               =       NF_IP_PRI_FIRST;
      

        nf_register_hook(&netfilter_ops_in); /* register NF_IP_PRE_ROUTING hook */
      
return 0;
}
void cleanup_module()
{
nf_unregister_hook(&netfilter_ops_in); /*unregister NF_IP_PRE_ROUTING hook*/
}
When I compile this I get and error saying that the 'struct sk_buff' has no member called 'mac'. I need the mac header in order to obtain the destination mac address of an incoming packet.

Can anyone suggest what is wrong here ?
 
Old 03-20-2009, 08:47 AM   #2
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Rep: Reputation: 16
That happen because the change of netfilter. You code might be useful for 2.4 or earlier but not later ones.

Check this out it may help: http://lxr.linux.no/
 
Old 03-20-2009, 09:05 AM   #3
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Rep: Reputation: 16
http://lxr.linux.no/linux+v2.6.28.8/.../skbuff.h#L344

This might be what you want
 
Old 03-21-2009, 07:21 AM   #4
steve_austin
LQ Newbie
 
Registered: Mar 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks Kunsheng ... this was exactly the problem. I am able to access the MAC Header by using the 'mac_header' member of the struct.

However, I noticed that the Netfilter is capturing only those packets with Protocol Type = 08 00 (IP Protocol Packets). I need to be able to process ARP packets before they are processed by the system (Protocol Type = 08 06). Can this be done by the netfilter libraries, if not, could you suggest how I could do this.

Thanks
 
  


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
dump packet data to file, but no packet header Nathanael Linux - Networking 3 02-08-2006 10:27 AM
access IP header in packet linux_lover2005 Programming 3 04-11-2005 01:02 AM
does each header fields in packet sent on network uses htons? b123coder Programming 3 04-06-2005 08:36 AM
How to get the IP header of packet going through an interface thanh Programming 1 06-12-2004 12:26 PM
Edit packet header? mudvayne Linux - Networking 4 03-28-2003 01:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:17 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