Access MAC address from sk_buff

来源:互联网 发布:iphone写作软件 编辑:程序博客网 时间:2024/05/01 20:42

http://stackoverflow.com/questions/10127806/access-mac-address-from-sk-buff

Access MAC address from sk_buff



struct ethhdr {
unsigned char h_dest[ETH_ALEN]; /* destination eth addr*/
unsigned char h_source[ETH_ALEN]; /* source ether addr*/
unsigned shorth_proto;/* packet type ID field*/
};


up vote1down votefavorite

I'm writing a kernel module to get the MAC address from a packet stored in sk_buff. I've used the following code to print the MAC address of source and destination:

struct ethhdr *mh = eth_hdr(skb);  printk(KERN_EMERG "Source MAC=%x:%x:%x:%x:%x:%x\n",mh->h_source[0],mh->h_source[1],mh->h_source[2],mh->h_source[3],mh->h_source[4],mh->h_source[5]);  

The destination address can be accessed using h_dest inplace of h_source.
My problem is that the source MAC address is always a8:c0:0:0:a8:c0 and the destination MAC address is always some junk value instead of my own MAC address.
Can anyone help me out on this? I want to get the correct MAC addresses.

share|edit
  

2 Answers

activeoldestvotes
up vote1down vote

Where exactly in the kernel are you doing this?

a8:c0 looks quite suspicious. It translates to 168.192 in decimal, which makes me suspect that you're in fact looking at the IPv4 header rather than the Ethernet header.

share|edit
  
up vote0down vote

Using eth_hdr only makes sense if the skb actually has a MAC header. If it does not, the skb's MAC header pointer will just point to other data in the packet.

share|edit