简体   繁体   中英

Getting MAC address for a given IP from ARP entry (Linux kernel)

I am writing a kernel module and need, for a given IP, to verify if there is an ARP entry on the ARP cache of the kernel for that IP. If it exists, I need to then fetch the MAC address from this ARP entry.

I am at this point trying to understand the arp_find function on usr/src/linux-version/net/ipv4/arp.c but I don't quite get it... Could someone please post an example of how can I accomplish this task or direct me to something that can help me out? I would greatly appreciate it!

EDIT: my IP address is stored as a struct in_addr

EDIT #2: I found my answer -- for those who have this problem, check the function neigh_lookup on net/core/neighbour.c . The pkey parameter is just a pointer to your IPv4 address ( struct in_addr ).

I think in Linux kernel, the ARP cache only stores the neighbor's mac addresses. You can use arp_ioctl() or follow arp_ioctl() approach to implement your task. arp_ioctl() does two major things: (1) find the net_device; (2) arp lookup;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM