====== FreeBSD的策略路由 ======
FreeBSD的策略路由要比Linux的繁琐. 总的来说靠的是 FIB (routing table) 和 防火墙 (ipfw/pf) 包转发
以下只说实现方式, 不提原理
===== FIB =====
==== 启用多路由表 ====
默认的kernel是只有1个fib.
sysctl的net.fibs只读选项, 所以如果要修改只能在/boot/loader.conf操作并且重启.
net.fibs="32"
或者加在内核配置里面重新编译内核, 重启
options ROUTETABLES=32
以上的32代表启用32个FIB.
现代系统 net.fibs 可以动态调整了.
sysctl net.fibs=32
==== 设置路由表 ====
/sbin/route add default 10.1.1.1 -fib 1
/sbin/route add -inet6 default 2001:abcd::1 -fib 1
==== 查询FIB ====
netstat -rn -F 1
route -n get 8.8.8.8 -fib 1
==== 使用FIB ====
让某程序使用路由表1 (fib 1)
setfib 1 /path/program
让某个服务运行在路由表1上
# /etc/rc.conf
xxx_fib="1"
===== 防火墙的包转发 =====
防火墙的原理是
- 把某个源ip发出去的包重定向到指定的下一节点(默认会去默认网关了)
- 把来自某个网卡的包, 再从这个网卡出去
==== pf ====
pass in quick on gre1 reply-to ( gre1 10.1.1.1 ) inet from any to gre1
pass out quick on $ext_if route-to ( gre1 10.1.1.1 ) inet from gre1 to any
指定某些IP出口使用特定路由表
pass inet from 192.168.101/24 to any rtable 1
==== ipfw ====
ipfw add fwd 10.1.1.1 ip from 10.1.1.2 to any via $ext_if
ipfw add setfib 1 ip from any to any via gre1
指定某些IP出口使用特定路由表
ipfw add setfib 1 ip from 192.168.101/24 to any
{{tag>freebsd pbr}}