我正在研究一个内核模块代码,我需要在其中查看路由表以获取我的 ARP 表条目daddr
:
|--------------------------------------------------|
-------+ enp0s1 192.168.2.0/24 192.168.3.0/24 enp0s2 +-----
|--------------------------------------------------|
例如,我需要获取neighbour
192.168.3.111 的条目,并且该条目已永久添加到表中:
% ip neigh add 192.168.3.111 lladdr 00:11:22:33:44:55 dev enp0s2 nud permanent
% ip neigh sh
...
192.168.3.111 dev enp0s2 lladdr 00:11:22:33:44:55 PERMANENT
% ip route show
...
192.168.3.0/24 dev enp0s2 proto kernel scope link src 192.168.3.2
我想出了以下代码:
struct rtable *rt;
struct flowi4 fl4;
struct dst_entry dst;
struct neighbour *neigh;
u8 mac[ETH_ALEN];
...
memset(&fl4, 0, sizeof fl4);
fl4.daddr = daddr;
fl4.flowi4_proto = IPPROTO_UDP;
rt = ip_route_output_key(net, &fl4);
if (IS_ERR(rt))
goto err;
...
dst = rt->dst;
neigh = dst_neigh_lookup(&dst, &fl4.daddr);
if (!neigh) {
...
}
neigh_ha_snapshot(mac, neigh, neigh->dev);
neigh_release(neigh);
ip_rt_put(rt);
但是neigh_ha_snapshot
不返回正确MAC
的地址,实际上我认为它返回垃圾,有时ff:ff:ff:ff:ff:ff
,有时是多播01:xx:xx:xx:xx:xx
。
我究竟做错了什么?