freebsd:multiple_interface_policy_based_routing
FreeBSD的多网卡策略路由
- 网卡bce0: 192.168.0.10/24 网关 192.168.0.1, 系统默认路由
- 网卡bce1: 192.168.1.10/24 网关 192.168.1.1
- 网卡bce2: 192.168.2.10/24 网关 192.168.2.1
设置范例
/etc/sysctl.conf
默认的kernel是只有1个fib. 启用 8 组 fib. 另外从 13.0 开始, 系统默认不再在其他 fib 添加所有地址, 为了简化工作启用 net.add_addr_allfibs 用于添加所有设备的地址到所有路由表.
net.fibs=8 net.add_addr_allfibs=1
/etc/rc.conf
ifconfig_bce0="inet 192.168.0.10/24" ifconfig_bce1="inet 192.168.1.10/24" ifconfig_bce2="inet 192.168.2.10/24" defaultrouter="192.168.0.1" static_routes="bce1 bce2" route_bce1="-inet default 192.168.1.1" route_bce2="-inet default 192.168.2.1"
防火墙设置
选择 pf 或者 ipfw 其中一项即可.
由于程序默认运行在 FIB 0 上面, 因此上行数据依照 FIB 0 的路由表, 使用默认路由设置. (其他非默认网卡的同网段路由表是在 FIB 0 上, 所以同一网段的通讯不受影响).
所以运行在FIB 0的程序, 使用非默认网卡(bce1和bce2), 上行数据发送至同网段以外目标, 要设置转发规则来进行基于源地址的策略路由.
pf
pass out quick inet from 192.168.1.10 to any flags S/SA keep state rtable 1 pass out quick inet from 192.168.2.10 to any flags S/SA keep state rtable 2 pass in quick on bce1 reply-to (bce1 192.168.1.1) inet from any to 192.168.1.10 flags S/SA keep state pass in quick on bce2 reply-to (bce2 192.168.2.1) inet from any to 192.168.2.10 flags S/SA keep state
ipfw
ipfw add setfib 1 ip from 192.168.1.10 to any ipfw add setfib 2 ip from 192.168.2.10 to any
freebsd/multiple_interface_policy_based_routing.txt · 最后更改: 2024/07/30 02:36 由 Hshh