1

我在 C 中使用libnetfilter_queue来捕获数据包。我正在设置一个 iptable 规则来排队传入的数据包,这些数据包稍后将由用户空间实现处理,如下所示iptables -A INPUT -j NFQUEUE --queue-num 0:我使用nfqnl_test示例作为实现捕获的框架。一切都按预期工作。但是,我注意到不可能在 ip 片段级别检查队列。也就是说,如果一个数据包是以片段的形式进来的,它在被放入队列之前首先被重新组装。但我想使用片段。那么有没有办法强制执行这种行为?我想要的是一个队列,我可以在其中观察原始传入数据包(碎片和未碎片),以便我能够相应地对它们采取行动。

我读到重组确实发生过。另一方面,使用 iptables 有-f可用的标志,所以应该有我正在寻找的“碎片粒度”。我也尝试过调整 iptable 规则(例如iptables -t raw -D PREROUTING -i eth0 -j NFQUEUE --queue-num 0),但结果还是一样。我只能观察到已经重组的数据包,我肯定知道它是分段到达的。

非常感谢任何帮助。

4

2 回答 2

3

所以我找到了解决问题的方法,我在这里分享它以防有人感兴趣。归功于 netfilter 邮件列表中的 Adel,他提出了可能的解决方法。基本上,解决方案是使用 nftables 并设置一个优先级低于碎片整理的链。我已经用 C 代码测试了这个设置,它似乎工作得很好(我没有注意到任何副作用)。不过不得不提的是,我只是用它来观察IP分片,并没有对其进行篡改。

下面有两个函数可以设置 nftables 然后删除它们。

void set_nftable() {

    int status = 0;

    // Create a nftable 
    status = system("nft add table ip filter");

    // Add a chain to the nftable called "predefrag" which has lower priority than the defragmentation -450 < -400
    status = system("nft add chain ip filter predefrag { type filter hook prerouting priority -- -450 \\; }");

    // Set the nftable rule (queue packets to be accessed by a user-space application)
    status = system("nft add filter predefrag meta iif eth0 counter queue num 0 bypass"); 
}

void remove_nftable() {

    int status = 0;

    // Flush the rules that are stored in the chains that belong to the nftable
    status = system("nft flush table ip filter");

    // Delete the chain from the nftable
    status = system("nft delete chain ip filter predefrag");

    // Delete the nftable
    status = system("nft delete table ip filter");
}

使用这些函数,nfqnl_test代码可用于捕获 IP 片段。下面是设置 nftables 和了解它们如何工作的有用链接(一旦熟悉了 nftables 手册,函数中的注释就很容易解释了)。

http://wiki.nftables.org/wiki-nftables/index.php/Building_and_installing_nftables_from_sources

http://wiki.nftables.org/wiki-nftables/index.php/Configuring_tables

http://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains

http://wiki.nftables.org/wiki-nftables/index.php/Simple_rule_management

http://wiki.nftables.org/wiki-nftables/index.php/Queueing_to_userspace

于 2015-06-04T08:28:43.077 回答
0

首先,请确保您的网卡不是重新组装的网卡。

现代网卡(如LROUFO等)执行的各种卸载技术可以重新组装 IP 级别的片段。我建议使用ethtool -k检查相关界面上哪些卸载处于活动状态,然后使用ethtool -K.

于 2015-05-07T13:56:21.263 回答