【问题标题】:Check port number in Linux Kernel Module using Netfilter使用 Netfilter 检查 Linux 内核模块中的端口号
【发布时间】:2012-03-07 03:20:09
【问题描述】:

参考page处的Netfilter钩子代码

要检查的端口声明为:

/* Port we want to drop packets on */
static const uint16_t port = 25;

比较如下:

return (tcph->dest == port) ? NF_DROP : NF_ACCEPT;

如果变量端口是 int32 类型,我们如何将其转换为 uint16_t 以便可以根据 tcph->dest 检查它。

谢谢。

【问题讨论】:

    标签: c linux kernel kernel-module netfilter


    【解决方案1】:

    TCP 端口只有 16 位宽,所以如果您的 port-variable 包含 0..65535 范围之外的任何内容,那么无论如何都会有问题。此外,您应该使用ntohs 来说明字节序差异。

    所以我建议如下:

    BUG_ON(port < 0 || port > 65535);
    return (ntohs(tcph->dest) == (u16)port) ? NF_DROP : NF_ACCEPT;
    

    【讨论】:

      【解决方案2】:

      为什么要使用端口int32?端口必须是 uint16_t。大于 16 的值bit 是错误的。

      【讨论】:

        猜你喜欢
        • 2014-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-11
        • 2015-01-13
        • 2017-09-20
        • 1970-01-01
        相关资源
        最近更新 更多