部分硬件实现图
一、使内核支持802.1Q功能
1、使用内核kernel版本4.19.232
2、make menuconfig 配置内核
3、进入Networking support
4、进入Networking options
5、把这些都编进内核
6、点击保存退出
CONFIG_GARP=y
CONFIG_MRP=y
CONFIG_BRIDGE_VLAN_FILTERING=y
CONFIG_VLAN_8021Q=y
CONFIG_VLAN_8021Q_GVRP=y
CONFIG_VLAN_8021Q_MVRP=y
7、在.config文件中最后会增加这几个宏定义
二、RTL8309M LAN 驱动加入到内核
1、修改文件
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
在函数
static int stmmac_init_phy(struct net_device *dev)
添加如下程序
if(phydev!=NULL)
{dev_err(priv->device, "%s phy_id:%x mii:%p bus_id:%x\n",__func__, phydev->phy_id,priv->mii,priv->plat->bus_id);if(priv->plat->bus_id == 0 && priv->mii!=NULL){rtl8309m_phy_fixup(priv->mii);}
}netdev_err(priv->dev, "%s: trying to attach to %p\n", __func__,phydev);
2、 rtl8309m_phy_fixup函数实现如果
#include "rtk_api.h"
#include "mdcmdio.h"
#include "rtk_api_ext.h"
#include "rtl8309n_asicdrv_ext.h"static int rtl8309m_phy_fixup(struct mii_bus *mii)
{int i;static int devinit = 0;rtk_portmask_t mbrmsk , untagmsk;if(devinit == 0){setSwitchDev(mii);rtk_switch_init();printk("rtk_switch_init\r\n");rtk_vlan_init();printk("rtk_vlan_ini\r\n");for(i=0;i<8;i++){mbrmsk.bits[0] = (1<<8)|(1<<i);untagmsk.bits[0] = (1<<8)|(1<<i);rtk_vlan_set(100+i,mbrmsk,untagmsk,1);printk("rtk_vlan_set port %d \r\n",i);}devinit = 1;}return 0;
}
3、以下函数接口通过下面文件实现
setSwitchDev(mii);
rtk_switch_init();
rtk_vlan_init();
rtk_vlan_set();
下载路径
https://download.csdn.net/download/u011425939/88002036
4、修改文件
drivers/net/ethernet/stmicro/stmmac/Makefile
mdcmdio.c rtk_api.c rtl8309n_asicdrv.c
把这个三个文件编译进去
5、编译kernel 重启下载到设备
三、vlan配置
1、在控制终端输入如下命令
brctl addbr br0 创建网桥
ifconfig br0 192.168.63.1 配置ip
brctl addif br0 eth1 把lan接口eth1 加入到网桥
vconfig add eth1 100 eth1增加vlan id 为100的端口
vconfig add eth1 101 eth1增加vlan id 为101的端口
vconfig add eth1 102 eth1增加vlan id 为102的端口
vconfig add eth1 103 eth1增加vlan id 为103的端口
ifconfig eth1.100 up 启动 eth1.100
ifconfig eth1.101 up 启动 eth1.101
ifconfig eth1.102 up 启动 eth1.102
ifconfig eth1.103 up 启动 eth1.103
注意这里的100 101 102 103 要跟上面vlan设置函数设置的vid要一致,不然实现不了vlan功能
rtk_vlan_set(100+i,mbrmsk,untagmsk,1);
2、ifconfig 查看配置
~ #
~ # ifconfig
br0 Link encap:Ethernet HWaddr 02:FB:88:C0:86:81 inet addr:192.168.63.1 Bcast:192.168.63.255 Mask:255.255.255.0UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:2 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:80 (80.0 B)eth0 Link encap:Ethernet HWaddr 32:89:B0:83:C6:69 inet addr:192.168.9.166 Bcast:192.168.9.255 Mask:255.255.255.0UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:119217 errors:0 dropped:38402 overruns:0 frame:0TX packets:14597 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:38667494 (36.8 MiB) TX bytes:3143376 (2.9 MiB)Interrupt:48 eth1 Link encap:Ethernet HWaddr 02:FB:88:C0:86:81 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:1102 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:188650 (184.2 KiB)Interrupt:48 eth1.100 Link encap:Ethernet HWaddr 02:FB:88:C0:86:81 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)eth1.101 Link encap:Ethernet HWaddr 02:FB:88:C0:86:81 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:1100 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:184142 (179.8 KiB)eth1.102 Link encap:Ethernet HWaddr 02:FB:88:C0:86:81 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)eth1.103 Link encap:Ethernet HWaddr 02:FB:88:C0:86:81 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)eth1.104 Link encap:Ethernet HWaddr 02:FB:88:C0:86:81 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)eth1.105 Link encap:Ethernet HWaddr 02:FB:88:C0:86:81 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)eth1.106 Link encap:Ethernet HWaddr 02:FB:88:C0:86:81 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)eth1.107 Link encap:Ethernet HWaddr 02:FB:88:C0:86:81 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions: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.0UP LOOPBACK RUNNING MTU:65536 Metric:1RX packets:1255 errors:0 dropped:0 overruns:0 frame:0TX packets:1255 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:308550 (301.3 KiB) TX bytes:308550 (301.3 KiB)ra0 Link encap:Ethernet HWaddr F0:C8:14:4B:15:FC UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)~ #
已经实现eth1.100-eth1.104 网络接口
四、通过vlan网络接口实现对单个lan网口进行广播通信
步骤如下
1、创建vlan接收广播信息
2、创建vlan发送广播信息
3、实现对接收数据的处理
以下示例是对接在lan口的网络摄像头进行ip设置的一个运用
对每个lan口使用vlan进行单独的广播,接在对应lan口上的摄像头应答返回ip,然后再发送修改ip的数据报给摄像头,以达到修改ip的目的。
下载路径
https://download.csdn.net/download/u011425939/88002042
五、抓包分析数据
1、向eth1.100端口发送查询摄像头IP数据包
<?xml version="1.0" encoding="UTF-8"?>
<Query>
<Id>e64823ad2a3d-98c56204-525e-46d6-B38A-73C80C478CF9</Id>
<Types>search</Types>
</Query>
此时在ip层协议下一层加入了802.1Q协议,此时的vlan vid 是100
这些数据包不会在eth1.101 eth1.102 eth1.103 对应的lan口 输出数据,因此可以达到修改对应lan口接的摄像头ip
2、接收到eth1.100端口数据
<?xml version="1.0" encoding="UTF-8"?>
<Answer><ID>5_e6:48:23:ad:2a:3d</ID>
<Operate>Find</Operate>
<MAC>54:D0:C4:C0:02:22</MAC>
<Result>Success</Result>
<IPv4>192.168.63.101</IPv4>
<HttpPort>80</HttpPort>
<IPv4Mask>255.255.255.0</IPv4Mask>
<IPv4Gateway>192.168.63.1</IPv4Gateway>
<DNS>8.8.8.8</DNS>
<Model>F-SC311-21-M51120100001</Model>
<SoftVer>10.1.0.6-r9-onvif</SoftVer>
<HardVer>V1.0</HardVer>
<DeviceName>IP</DeviceName>
<OemModel>F-SC311-21-M51120100001</OemModel>
<RunTime>1248696</RunTime>
</Answer>
接收到的数据不带802.1q协议,也就是不携带vlan vid,所以不是vlan数据
3、向eth1.100端口发送修改ip摄像头IP数据包
:<?xml version='1.0'encoding='UTF-8'?>
<Quest>
<ID>1_02:1b:bb:24:cc:8b</ID>
<Operate>Modify</Operate>
<CurMAC>54:D0:C4:C0:02:22</CurMAC>
<ModifyMAC>0</ModifyMAC>
<MAC>54:D0:C4:C0:02:22</MAC>
<IPv4>192.168.63.101</IPv4>
<HttpPort>80</HttpPort>
<IPv4Mask>255.255.255.0</IPv4Mask>
<IPv4Gateway>192.168.63.1</IPv4Gateway>
<DNS>8.8.8.8</DNS>
<UserName>admin</UserName>
<Password>c0b3731c7311444835b7c339498a75dc</Password>
</Quest>
4、返回修改后的ip
<?xml version="1.0" encoding="UTF-8"?>
<Answer><ID>1_02:1b:bb:24:cc:8b</ID>
<Operate>Modify</Operate>
<MAC>54:D0:C4:C0:02:22</MAC>
<Result>Success</Result>
<IPv4>192.168.63.101</IPv4>
<HttpPort>80</HttpPort>
<IPv4Mask>255.255.255.0</IPv4Mask>
<IPv4Gateway>192.168.63.1</IPv4Gateway>
<DNS>8.8.8.8</DNS>
<Model>F-SC311-21-M51120100001</Model>
<SoftVer>10.1.0.6-r9-onvif</SoftVer>
<HardVer>V1.0</HardVer>
<DeviceName>IP</DeviceName>
<OemModel>F-SC311-21-M51120100001</OemModel>
<RunTime>631171</RunTime>
</Answer>