yas*_*ine 2 c iptables linux-kernel
我想检查由iptables(1.4)破解的数据包的以太网头,所以我需要编写一个捕获数据包并应用我的函数的模块.我在以太网头中寻找mac目标值(仅用于测试目的),因此代码应如下所示:
static bool match(const struct sk_buff *skb, struct xt_action_param *par)
{
struct ethhdr *hdr;
hdr = eth_hdr(skb);
printk(KERN_INFO "hdr->h_dest 0x%x\n", hdr->h_dest);
printk(KERN_INFO "MACPROTO=%04x\n", hdr->h_proto);
Run Code Online (Sandbox Code Playgroud)
问题是,我不能得到正确的值,我有一些事情,甚至不是在现实框架(我检查了使用Wireshark),因此,它是正确的函数来获取以太网帧头attributs?
更新:
我使用了帖子中提供的解决方案,但仍然有错误的输出,就像结构指向错误的地方一样

这个图像显示的结果当我使用nc发送"aaa"字符串时,以太网头在to帧中应该是相同的,但是如果在结果中存在,则不是.
struct ethhdr 定义如下:
/*
* This is an Ethernet frame header.
*/
struct ethhdr {
unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
unsigned char h_source[ETH_ALEN]; /* source ether addr */
__be16 h_proto; /* packet type ID field */
} __attribute__((packed));
Run Code Online (Sandbox Code Playgroud)
但是您的代码正在尝试使用%x以下方法打印该字节数组:
printk(KERN_INFO "hdr->h_dest 0x%x\n", hdr->h_dest);
Run Code Online (Sandbox Code Playgroud)
这没有任何意义,可能会导致生成编译器警告.这是内核代码-你正在使用-Werror和-Wall,对不对?
好消息:printk支持直接打印MAC地址.来自documentation/printk-formats.txt:
MAC/FDDI addresses:
%pM 00:01:02:03:04:05
%pMR 05:04:03:02:01:00
%pMF 00-01-02-03-04-05
%pm 000102030405
%pmR 050403020100
For printing 6-byte MAC/FDDI addresses in hex notation. The 'M' and 'm'
specifiers result in a printed address with ('M') or without ('m') byte
separators. The default byte separator is the colon (':').
Where FDDI addresses are concerned the 'F' specifier can be used after
the 'M' specifier to use dash ('-') separators instead of the default
separator.
For Bluetooth addresses the 'R' specifier shall be used after the 'M'
specifier to use reversed byte order suitable for visual interpretation
of Bluetooth addresses which are in the little endian order.
Passed by reference.
Run Code Online (Sandbox Code Playgroud)
所以你可以使用这个:
printk(KERN_INFO "hdr->h_dest 0x%pM\n", hdr->h_dest);
Run Code Online (Sandbox Code Playgroud)
这些格式说明符在任何地方都vsnprintf可以使用.这是一个例子:
switch (dev->type) {
case ARPHRD_ETHER:
nf_log_buf_add(m, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
ntohs(eth_hdr(skb)->h_proto));
return;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)