通过mangle对报文添加标记,路由规则根据mark值控制匹配路由。
准备工作: | linux ubuntu 虚拟机(192.168.10.105) win10宿主机(192.168.10.104) xcap自定义发包工具 |
模拟策略路由:
设置虚拟机网口ip | ens33 12.12.12.12 ens38 192.168.10.105 | ||||
添加两个静态路由 | 0.0.0.0 12.12.12.1 0.0.0.0 UG 0 0 0 ens33 0.0.0.0 192.168.10.1 0.0.0.0 UG 0 0 0 ens38 为了可以 正常转发报文,并且12.12.12.1为不存在的终端,所以令12.12.12.1的mac与192.168.10.1的mac地址一样。 | ||||
处理转发目的IP为192.168.200.1的报文 |
| ||||
通过xcap向虚拟机的ens33接口发送报文 | | ||||
在没有添加路由规则时,在本地接收到的报文 | 由此可知,报文的转出转入在同一接口运行。 | ||||
添加添加路由表,并设置路由规则。 | linux存在255张路由表,系统自带4张路由表。 0#表 系统保留表 253#表 defulte table 没特别指定的默认路由都放在改表 254#表 main table 没指明路由表的所有路由放在该表 255#表 locale table 保存本地接口地址,广播地址、NAT地址 由系统维护,用户不得更改 默认使用254
| ||||
添加路由规则,将标志mark==2的报文通过路由表3进行转发。 | ip rule add from all fwmark 2 table 3 此时发现,接收到的报文已经根据路由策略在ens38口转发。 | ||||
查看mangle表的命中信息 | iptables -t mangle -L -v |
命令总结:
使用到的添加命令 | iptables -t mangle -A PREROUTING -I -d -j MARK --set-mark[] ip route add via[] dev[] table[] ip rule add from[] fwmark[] table[] pref[]优先级 |
查看 | iptables -t[] -L -v ip route show table table_num ip rule show |
删除 | iptables -F -t mangle|iptables -D PREROUTING 2 -t mangle ip route flush table[] ip rule del pref[] table[] nat[] from[] |