通过三层交换机来处理 VLAN 间路由,可以节省防火墙上的资源,同时提升网络访问性能。三层交换机负责 VLAN 间路由 + DHCP,OPNsense 只做出口、防火墙、NAT。
vlan地址参数如下:
|
VLAN |
用途 |
网段 |
网关 |
|---|---|---|---|
|
10 |
管理 |
192.168.10.0/24 |
192.168.10.1 |
|
20 |
办公 |
192.168.20.0/24 |
192.168.20.1 |
|
30 |
IoT |
192.168.30.0/24 |
192.168.30.1 |
|
99 |
上联 OPNsense |
192.168.99.0/30 |
192.168.99.1 |
下面为配置过程。
交换机配置
交换机为华为S5735S-L32ST4X-A,其他类型交换机可以参考。
1、创建 vlan和SVI
system-view vlan batch 10 20 30 99 interface Vlanif10 description VLAN10-MGMT ip address 192.168.10.1 255.255.255.0 dhcp select global interface Vlanif20 description VLAN20-OFFICE ip address 192.168.20.1 255.255.255.0 dhcp select global interface Vlanif30 description VLAN30-IOT ip address 192.168.30.1 255.255.255.0 dhcp select global interface Vlanif99 description UPLINK-OPNSENSE ip address 192.168.99.1 255.255.255.252
2、启用 dhcp
启用 DHCP Server(全局)
dhcp enable
vlan配置dhcp地址池
ip pool VLAN10_POOL network 192.168.10.0 mask 255.255.255.0 gateway-list 192.168.10.1 dns-list 223.5.5.5 8.8.8.8 excluded-ip-address 192.168.10.1 192.168.10.50 lease day 7
ip pool VLAN20_POOL network 192.168.20.0 mask 255.255.255.0 gateway-list 192.168.20.1 dns-list 223.5.5.5 8.8.8.8 excluded-ip-address 192.168.20.1 192.168.20.50 lease day 7
ip pool VLAN30_POOL network 192.168.30.0 mask 255.255.255.0 gateway-list 192.168.30.1 dns-list 223.5.5.5 8.8.8.8 excluded-ip-address 192.168.30.1 192.168.30.50 lease day 7
3、添加接入端口
interface GigabitEthernet0/0/1 description MGMT-PC port link-type access port default vlan 10 interface GigabitEthernet0/0/2 description OFFICE-PC port link-type access port default vlan 20 interface GigabitEthernet0/0/3 description IOT port link-type access port default vlan 30
4、添加上联端口
interface GigabitEthernet0/0/24 description UPLINK-OPNSENSE port link-type access port default vlan 99
5、添加默认路由
ip route-static 0.0.0.0 0.0.0.0 192.168.99.2
6、保存配置
save
OPNsense 配置
1、lan接口配置
转到接口>lan,将 lan接口地址配置为交换机默认路由的下一跳地址192.168.99.2。
2、添加网关
转到系统>网关>配置,在lan接口上添加一个网关,地址为交换机下联口地址192.168.99.1。
3、添加回程路由
转到系统>路由>配置,添加三条回程路由。
其他两条回程路由类似。
4、添加防火墙规则
转到防火墙>规则>LAN,添加一条 any to any 规则。注意这里的源不能为LAN NET,必须为 any,或者 vlan的子网。
5、添加出站规则
与 pfSense 在添加静态路由后会自动添加出站规则不同,OPNsense 需要手动添加出站规则。
转到防火墙 >NAT >出站,将出站改为混合出站,然后添加以下出站规则:
现在,VLAN间路由在三层交换机上配置完成,每个VLAN网络上的主机都可以通过 OPNsense 防火墙访问互联网。
注意事项:
- 只能在三层交换机上创建 ACL(访问控制列表)来限制每个 VLAN 之间的流量,而不是在 OPNsense 上使用防火墙规则来进行控制。
- 在三层交换机上添加其他的VLAN SVI时,需要同时在 OPNsense 添加必要的静态路由和防火墙/NAT规则,以便可以进行 Internet 访问。




