iproute2在openwrt中实现策略路由

安装ip-full

opkg install ip-full

新增路由表 id:252(1-252可用),name:wifi2zt id:251,name:ovpn2ta id:250,name:bg

1
2
3
echo "252 wifi2zt" >> /etc/iproute2/rt_tables
echo "251 ovpn2ta" >> /etc/iproute2/rt_tables
echo "250 bg" >> /etc/iproute2/rt_tables

为新建的路由表指定路由

1
2
3
ip route add 0.0.0.0/0 via 10.49.160.1 table wifi2zt
ip route add 0.0.0.0/0 via 172.16.21.254 table ovpn2ta
ip route add 0.0.0.0/0 via 192.168.170.1 table bg

添加策略路由

1
2
3
ip rule add from 192.168.171.0/24 table wifi2zt #源IP为192.168.171.0/24,添加到wifi2zt表,使用网关:10.49.160.1
ip rule add from 192.168.170.0/24 table ovpn2ta #源IP为192.168.170.0/24,添加到ovpn2ta表,使用网关:172.16.21.254
ip rule add to 192.168.169.0/24 table bg #目的IP为192.168.169.0/24,添加到bg表,使用网关:192.168.170.1

查看路由表及清空路由表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
root@LEDE:/etc/iproute2# ip route list table ovpn2ta   #查看ovpn2ta表
default via 172.16.21.254 dev tun0
root@LEDE:/etc/iproute2#


root@LEDE:/etc/iproute2# ip rule list  #查看策略
0: from all lookup local
32740: from all to 192.168.168.0/24 lookup ovpn2ta
32741: from all to 192.168.169.0/24 lookup ovpn2ta
32742: from all to 192.168.170.0/24 lookup bg
32763: from all lookup main
32764: from all lookup default
root@LEDE:/etc/iproute2#


root@LEDE:/etc/iproute2# ip route flush table bg #清空路由表