无法从内核模块中的net_device获取mac地址 [英] unable to fetch mac address from net_device in kernel module

查看:268
本文介绍了无法从内核模块中的net_device获取mac地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的钩子函数

unsigned int
my_packet_pass_through_hook(const struct nf_hook_ops *ops,
              struct sk_buff *skb,
              const struct net_device *in,
              const struct net_device *out,
              int (*okfn)(struct sk_buff*)){

我已经在PREROUTING和POSTROUTING阶段注册的

.我只是在此函数中转储net_device信息. 在PREROUTING阶段中,我正在转储 const struct net_device * in ,而在POSTROUTING阶段中,我正在转储 const struct net_device * out net_devoce结构.

which i have registered in PREROUTING and POSTROUTING stages. i just dump the net_device information in this function. In PREROUTING stage, i am dumping const struct net_device *in whereas in POSTROUTING stage i am dumping const struct net_device *out net_devoce structure.

在两种情况下,我都无法打印设备的MAC地址.

In both the cases, i am not able to print the MAC address of the device.

printk(KERN_ALERT "             Mac Addr             = %s\n", iif->dev_addr);
printk(KERN_ALERT "             Broadcast  Addr      = %s\n", iif->broadcast);

输出

4月28日19:56:21 node2内核:[466.344567] Mac Addr =

Apr 28 19:56:21 node2 kernel: [ 466.344567] Mac Addr =

4月28日19:56:21 node2内核:[466.344568] Broadcast Addr =▒▒▒▒▒▒

Apr 28 19:56:21 node2 kernel: [ 466.344568] Broadcast Addr = ▒▒▒▒▒▒

请注意,我在节点2上运行我的模块,该模块将ping从节点1路由到节点3.因此,所有数据包都被转发了.我只读取数据包的字段并打印它,而无论如何都不会对其进行调节.因此,ping操作成功.

pls note, i am running my module on node 2 which routes the pings to node3 from node1. So, all packets are forwarded.I am only reading the fields of the packet and printing it without tempering it in anyway. So, pings are successful.

还有,有人可以启发我什么是okfn fn指针及其用法吗?

Also, could anyone pls enlighten me what is okfn fn pointer and its usage ?

非常感谢.

推荐答案

该字段正确,dev_addr是硬件地址,而broadcast保存了硬件广播地址,但是您不能像字符串一样打印它们!它们是一个无符号字符数组,其中每个字符都包含mac地址的一个八位位组.请使用专为mac地址设计的printk的特定修饰符:

The field is right, dev_addr is the hardware address, and broadcast holds the hardware broadcast address, but you cannot print them like a string! They are an array of unsigned chars, where each char holds a octet of the mac address. Use the specific modifier of printk designed for mac address instead:

printk(KERN_ALERT "   Mac Addr         = %pMF\n", iif->dev_addr);
printk(KERN_ALERT "   Broadcast  Addr  = %pMF\n", iif->broadcast);

您可以在printk文档中找到其他格式:

You can find other formats in the printk docs:

http://lxr.free-electrons.com/来源/文档/printk-formats.txt#L136

这篇关于无法从内核模块中的net_device获取mac地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆