【问题标题】:Is it possible to access ip fragments with libnetfilter_queue是否可以使用 libnetfilter_queue 访问 ip 片段
【发布时间】:2015-07-06 03:58:42
【问题描述】:

我在 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),但结果还是一样。我只能观察已经重组的数据包,我肯定知道这些数据包以碎片形式到达。

非常感谢任何帮助。

【问题讨论】:

    标签: c linux iptables netfilter ip-fragmentation


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      所以我找到了解决问题的方法,我在这里分享它以防有人感兴趣。归功于 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 手册,函数中的 cmets 就很容易解释了)。

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多