【问题标题】:Writing Netfilter module to accept only vlan packets编写 Netfilter 模块以仅接受 vlan 数据包
【发布时间】:2018-03-12 19:47:28
【问题描述】:

我正在尝试构建 Netfilter。第一阶段是只接受 vlan 数据包,然后检查它是否是正确的 vlan ID。问题我没有收到任何 vlan 数据包。 代码的简单示例:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/if_ether.h>


static struct nf_hook_ops nfho;

static void __exit_filter(void) {
    printk("__exit_filter\n");
        nf_unregister_hook(&nfho); //unregister our hook
}

static u32
recv_packet_handler(const struct nf_hook_ops *ops,
           struct sk_buff *skb,
           const struct net_device *in,
           const struct net_device *out,
           int (*okfn)(struct sk_buff *)){

    //NEED TO GET HERE VLAN PACKETS

    return NF_ACCEPT; 
}

static int __init_filter(void) {
        nfho.hook     = recv_packet_handler;
        nfho.hooknum  = NF_INET_PRE_ROUTING;
        nfho.pf       = PF_INET; 
        nfho.priority = NF_IP_PRI_FIRST;
        nf_register_hook(&nfho); 

    printk("insert filter\n");
        return 0;
}

module_init(__init_filter);
module_exit(__exit_filter);

我想问题出在nfho.pf,但我不知道该怎么办。感谢帮助

【问题讨论】:

  • 我系统中的网桥不通过 vlan 数据包,这就是为什么我需要在数据包通过网桥之前在 eth0 级别捕获数据包。所以更改为 NFPROTO_BRIDGE 也不起作用。
  • 这不是更好吗,特别是如果您需要基于 vlan 标签进行过滤以在 eth0 tc ingress hook 处进行过滤?如果是用于 L2 VLAN,为什么要在 IP 层呢?
  • 我不知道该怎么做……你能给我举个例子吗?
  • 我需要在自己的内核模块中使用过滤器。尝试在tc、tcpdump和libcap的源代码中搜索,如何获取所有包,但我没有找到方法。奇怪的是,tcpdump 确实显示了 vlan 包。不要买我的模块。

标签: linux-kernel kernel-module netfilter vlan


【解决方案1】:

所以,经过很长时间后,我发现 /config/network 由于配置错误而没有按预期运行。如果你想在你的 OpenWrt 中启用 Vlan,你需要这个命令:

uci add network switch_vlan
uci set network.@switch_vlan[-1].device='switch0'
uci set network.@switch_vlan[-1].vlan=[any number from ?-15]
uci set network.@switch_vlan[-1].ports='0t 2t 5t'
uci set network.@switch_vlan[-1].vid=[any number between ?-4094]
uci commit network

请注意,“端口”是多种多样的,并且取决于硬件。在一个 AP 中我使用了 2t,而在另一个 AP 中我需要 5t,因此我将两者都包括在内以处理这两种情况。您的情况可能不同。

【讨论】:

    猜你喜欢
    • 2021-01-10
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多