Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access MAC address from sk_buff

Tags:

linux

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.

like image 243
sax0406 Avatar asked Apr 12 '12 16:04

sax0406


2 Answers

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.

like image 114
Kristof Provost Avatar answered Sep 22 '22 12:09

Kristof Provost


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.

like image 38
jørgensen Avatar answered Sep 18 '22 12:09

jørgensen