【问题标题】:Netfilter hook doesn't see all packetsNetfilter 钩子没有看到所有的数据包
【发布时间】:2020-03-14 08:20:28
【问题描述】:

我写了一个内核模块,它使用 netfilter hook 转储多播 DNS 数据包。

static uint32_t myhook(uint32_t hooknum, struct sk_buff *skb, const struct net_device *in,  const struct net_device *out, int (*okfn) (struct sk_buff *))
{
    struct iphdr *ip_header;
    uint8_t proto;
    struct udphdr *udp_header;
    unsigned int sip, dip, sport = 0, dport = 0;

    if(!skb)
        return NF_ACCEPT;

    if(ntohs(skb->protocol) != ETH_P_IP)
        return NF_ACCEPT;

    ip_header = (struct iphdr *)skb_network_header(skb);
    proto = ip_header->protocol;

    if (proto != IPPROTO_UDP)
        return NF_ACCEPT;

    udp_header = (struct udphdr *)skb_transport_header(skb);
    sip = (unsigned int)ntohl(ip_header->saddr);
    dip = (unsigned int)ntohl(ip_header->daddr);
    sport = (unsigned int)ntohs(udp_header->source);
    dport = (unsigned int)ntohs(udp_header->dest);

    if (dport == 5353)
        pr_err("sip: %pI4h, sport: %u; dip: %pI4h, dport: %u\n", &sip, sport, &dip, dport);

    return NF_ACCEPT;
}

/*
pre_routing_hook_ops.hooknum = NF_INET_PRE_ROUTING;
pre_routing_hook_ops.pf = PF_INET;
pre_routing_hook_ops.priority = NF_IP_PRI_FIRST;
pre_routing_hook_ops.hook = (nf_hookfn *) myhook;
*/

此内核模块不会记录所有多播 dns 数据包的信息。但是,当我添加此iptables 日志规则时:

iptables -t mangle -I PREROUTING 1 -j LOG --log-prefix="mylog" --log-level 4 --ipv4 -p udp --sport 5353

这条iptables 规则查看/记录所有多播 dns 数据包。我假设iptables 也使用了 netfilter 钩子。我不知道为什么它会看到所有 mdns 数据包,但看不到我的内核模块。任何想法为什么?提前致谢。

【问题讨论】:

  • +1 用于询问与编程和开发有关的 iptables 问题

标签: linux-kernel iptables kernel-module netfilter


【解决方案1】:

您的 iptables 规则正在检查 端口 5353,而您的 netfilter 代码正在检查 目标 端口 5353。虽然大多数 mDNS 数据包将同时具有5353 的源端口和目标端口,RFC 6762 实际上不需要。

祝你好运!

【讨论】:

    猜你喜欢
    • 2021-05-19
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    相关资源
    最近更新 更多