所以我找到了解决问题的方法,我在这里分享它以防有人感兴趣。归功于 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