linux 下VLAN数据包格式

本文详细介绍了如何使用vconfig命令添加VLAN,并通过抓包分析了VLAN数据包的格式。展示了从添加VLAN到抓取数据包的具体过程,并对比了不同接口上的数据包差异。
主要是对VLAN的数据包格式进行了抓包分析。同时对vconfig命令的适用进行了说明。

PC10.10.107.168  AP10.10.107.231(eth0.100)

1:添加VLAN,使用vconfig

~# vconfig

BusyBoxv1.10.3 (2012-09-23 02:13:20 CST) multi-call binary

Usage:vconfig COMMAND [OPTIONS]...

Create andremove virtual ethernet devices

Options:

        add             [interface-name] [vlan_id]

        rem             [vlan-name]

        set_flag        [interface-name] [flag-num] [0 | 1]

        set_egress_map  [vlan-name] [skb_priority] [vlan_qos]

        set_ingress_map [vlan-name][skb_priority] [vlan_qos]

        set_name_type   [name-type]

 

tcpdump-i br0 -s 0 -w dump.pcap

./tcpdump-i eth0 -xxvv

 route -n

~ #vconfig add eth0 100

~ #ifconfig -a

br0       Link encap:Ethernet  HWaddr 00:03:7F:11:20:00 

          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500 Metric:1

          RX packets:8 errors:0 dropped:0overruns:0 frame:0

          TX packets:0 errors:0 dropped:0overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:410 (410.0 B)  TX bytes:0 (0.0 B)

 

eth0      Link encap:Ethernet  HWaddr 00:03:7F:11:20:00 

          UP BROADCAST RUNNING MULTICAST  MTU:1500 Metric:1

          RX packets:16 errors:0 dropped:0 overruns:0frame:0

          TX packets:0 errors:0 dropped:0overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:1108 (1.0 KiB)  TX bytes:0 (0.0 B)

 

eth0.100  Link encap:Ethernet  HWaddr 00:03:7F:11:20:00 

          BROADCAST MULTICAST  MTU:1500 Metric:1

          RX packets:0 errors:0 dropped:0overruns:0 frame:0

          TX packets:0 errors:0 dropped:0overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

 

lo        Link encap:Local Loopback 

          inet addr:127.0.0.1  Mask:255.0.0.0

          UP LOOPBACK RUNNING  MTU:16436 Metric:1

          RX packets:0 errors:0 dropped:0overruns:0 frame:0

          TX packets:0 errors:0 dropped:0overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

 

teql0     Link encap:UNSPEC  HWaddr00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 

          NOARP MTU:1500  Metric:1

          RX packets:0 errors:0 dropped:0overruns:0 frame:0

          TX packets:0 errors:0 dropped:0overruns:0 carrier:0

          collisions:0 txqueuelen:100

          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Ifconfig  eth0.100  10.10.107.231

         

~# ./tcpdump -i eth0 -xxvv

tcpdump:WARNING: can't create rx ring on packet socket 3: 99-Protocol not available

tcpdump:WARNING: eth0: no IPv4 address assigned

tcpdump:listening on eth0, link-type EN10MB (Ethernet), capture size 68 bytes

00:06:13.184401ARP, Ethernet (len 6), IPv4 (len 4),Request who-has 10.10.107.168 tell10.10.107.231, length 28

        0x0000: ffff ffff ffff 0003 7f112000 8100 0064

        0x0010: 08060001 08000604 0001 0003 7f11 2000

        0x0020: 0a0a 6be7 0000 0000 0000 0a0a 6ba8

00:06:13.184592ARP, Ethernet (len 6), IPv4 (len 4),Reply10.10.107.168 is-at e8:39:35:38:9e:4a(oui Unknown), length 46

        0x0000: 0003 7f11 2000e839 3538 9e4a0806 0001

        0x0010: 0800 0604 0002 e839 3538 9e4a 0a0a 6ba8

        0x0020: 0003 7f11 2000 0a0a 6be7 0000 0000 0000

        0x0030: 0000 0000 0000 0000 0000 0000

00:06:13.958903IP (tos 0x0, ttl 128, id 57182, offset 0, flags [none], proto ICMP (1), length60)

    10.10.107.168 > 10.10.107.231: ICMP echorequest, id 1024, seq 13840, length 40

        0x0000:  0003 7f11 2000 e839 3538 9e4a 0800 4500

        0x0010: 003c df5e 0000 8001 6fbf 0a0a 6ba8 0a0a

        0x0020: 6be7 0800 134c 0400 3610 6162 6364 6566

        0x0030: 6768 696a 6b6c 6d6e 6f70 7172 7374 7576

        0x0040: 7761 6263

00:06:14.196038ARP, Ethernet (len 6), IPv4 (len 4), Requestwho-has10.10.107.168 tell 10.10.107.231, length 28

        0x0000: ffff ffff ffff 0003 7f1120008100 0064

        0x0010: 0806 0001 0800 0604 0001 0003 7f11 2000

        0x0020: 0a0a 6be7 0000 0000 0000 0a0a 6ba8

00:06:14.196524ARP, Ethernet (len 6), IPv4 (len 4),Reply10.10.107.168 is-at e8:39:35:38:9e:4a (oui Unknown), length 46

        0x0000: 0003 7f11 2000 e839 35389e4a 0806 0001

        0x0010: 0800 0604 0002 e839 3538 9e4a 0a0a 6ba8

        0x0020: 0003 7f11 2000 0a0a 6be7 0000 0000 0000

        0x0030: 0000 0000 0000 0000 0000 0000

有上面,可以知道,在我们添加VLAN ID=100时,我们通过ping 10.10.107.168来ping PC就会有上面的信息。村抓得包,我们可以知道,AP àPC 是已经加了VLAN TAG的。

注意上面的数据在试eth0上面抓到的。

而在eth0.100上面的数据如下:只有request。没有replay

~ # ./tcpdump-i eth0.100 -xxvv

deviceeth0.100 entered promiscuous mode

tcpdump:WARNING: can't create rx ring on packet socket 3: 99-Protocol not available

tcpdump:listening on eth0.100, link-type EN10MB (Ethernet), capture size 68 bytes

00:03:35.064079ARP, Ethernet (len 6), IPv4 (len 4),Request who-has10.10.107.168 tell 10.10.107.231, length 28

        0x0000: ffff ffff ffff 0003 7f11 2000 0806 0001

        0x0010: 0800 0604 0001 0003 7f11 2000 0a0a 6be7

        0x0020:  0000 0000 0000 0a0a 6ba8

00:03:36.064043ARP, Ethernet (len 6), IPv4 (len 4),Request who-has10.10.107.168 tell 10.10.107.231, length 28

        0x0000: ffff ffff ffff 0003 7f11 2000 0806 0001

        0x0010: 0800 0604 0001 0003 7f11 2000 0a0a 6be7

        0x0020: 0000 0000 0000 0a0a 6ba8

00:03:37.068137ARP, Ethernet (len 6), IPv4 (len 4),Request who-has10.10.107.168 tell 10.10.107.231, length 28

        0x0000: ffff ffff ffff 0003 7f11 2000 0806 0001

        0x0010: 0800 0604 0001 0003 7f11 2000 0a0a 6be7

        0x0020: 0000 0000 0000 0a0a 6ba8

2:更新部分

vlan协议数据帧格式:


vconfig命令的使用说明:


linux下vconfig命令实现说明:




vconfig命令设置engree操作:



和上面的图类似


分别通过vconfig 设置不同的priority和vlan_qos,其中priority已经在vconfig的使用中说明了,是skb_buf的数据字段。在我自己的ethernet驱动程序中此值是0;





Linux 系统中,VLAN 过滤主要通过 Linux Bridge VLAN Filtering 技术实现,其核心作用是确保网络流量仅在被允许的 VLAN 间传输,提升网络安全性和管理效率[^1]。 #### 配置 VLAN 过滤 可通过 `bridge` 命令行工具配置 VLAN 过滤规则。例如,要为桥接设备 `br0` 启用 VLAN 过滤并设置特定端口的 VLAN 规则: ```bash # 启用桥接设备的 VLAN 过滤 ip link set dev br0 type bridge vlan_filtering 1 # 为桥接设备的端口 eth0 设置 VLAN 规则 bridge vlan add dev eth0 vid 10 pvid untagged bridge vlan add dev eth0 vid 20 tagged ``` 上述命令中,`vlan_filtering 1` 用于启用 VLAN 过滤,`bridge vlan add` 用于添加 VLAN 规则,`vid` 指定 VLAN ID,`pvid` 表示端口的默认 VLAN ID,`untagged` 表示该 VLAN 的帧不打标签,`tagged` 表示该 VLAN 的帧打标签。 #### 内核层面的实现 在内核中,VLAN 过滤主要通过 `netfilter` 框架和桥接模块实现。`netfilter` 是 Linux 内核中的一个数据包过滤和处理框架,可用于实现自定义的 VLAN 过滤规则。桥接模块则负责在桥接设备上处理 VLAN 流量。 以下是一个简单的内核模块示例,用于实现自定义的 VLAN 过滤逻辑: ```c #include <linux/module.h> #include <linux/netfilter.h> #include <linux/netfilter_bridge.h> #include <linux/vlan.h> static struct nf_hook_ops vlan_filter_hook; static unsigned int vlan_filter_hook_func(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { u16 vlan_tci; int vlan_id; // 检查是否包含 VLAN tag if (vlan_tx_tag_present(skb)) { // 获取 VLAN TCI(Tag Control Information) vlan_tci = vlan_tx_tag_get(skb); // 提取 VLAN ID vlan_id = VLAN_TAG_GET_VID(vlan_tci); // 自定义过滤规则:只允许 VLAN ID 为 10 的数据包通过 if (vlan_id != 10) { return NF_DROP; } } return NF_ACCEPT; } static int __init vlan_filter_init(void) { vlan_filter_hook.hook = vlan_filter_hook_func; vlan_filter_hook.pf = NFPROTO_BRIDGE; vlan_filter_hook.hooknum = NF_BR_PRE_ROUTING; vlan_filter_hook.priority = NF_BR_PRI_FIRST; nf_register_net_hook(&init_net, &vlan_filter_hook); return 0; } static void __exit vlan_filter_exit(void) { nf_unregister_net_hook(&init_net, &vlan_filter_hook); } module_init(vlan_filter_init); module_exit(vlan_filter_exit); MODULE_LICENSE("GPL"); ``` 上述代码中,`vlan_filter_hook_func` 是自定义的 VLAN 过滤函数,可根据 VLAN ID 决定是否允许数据包通过。`vlan_filter_init` 用于注册过滤钩子,`vlan_filter_exit` 用于注销过滤钩子。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值