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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With