|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
×
配置代码:
/ ip address
add address=192.168.0.1/24 network=192.168.0.0 broadcast=192.168.0.255 interface=Local
add address=10.111.0.2/24 network=10.111.0.0 broadcast=10.111.0.255 interface=wlan2
add address=10.112.0.2/24 network=10.112.0.0 broadcast=10.112.0.255 interface=wlan1
/ ip firewall mangle
add chain=prerouting in-interface=Local connection-state=new nth=1,1,0 \
action=mark-connection new-connection-mark=odd passthrough=yes
add chain=prerouting in-interface=Local connection-mark=odd action=mark-routing \
new-routing-mark=odd passthrough=no
add chain=prerouting in-interface=Local connection-state=new nth=1,1,1 \
action=mark-connection new-connection-mark=even passthrough=yes
add chain=prerouting in-interface=Local connection-mark=even action=mark-routing new-routing-mark=even passthrough=no
/ ip firewall nat
add chain=srcnat connection-mark=odd action=src-nat to-addresses=10.111.0.2 \
to-ports=0-65535
add chain=srcnat connection-mark=even action=src-nat to-addresses=10.112.0.2 \
to-ports=0-65535
/ ip route
add dst-address=0.0.0.0/0 gateway=10.111.0.1 scope=255 target-scope=10 routing-mark=odd
add dst-address=0.0.0.0/0 gateway=10.112.0.1 scope=255 target-scope=10 routing-mark=even
add dst-address=0.0.0.0/0 gateway=10.112.0.1 scope=255 target-scope=10
原理与命令讲解
我们从所有的连接中,提取每次新建立的连接connection=new,并对他们做nth的标记,将这些连接中相关的奇数(odd)包和偶数(even)包分离开,并走两个不同的网关(GatewayA与GatewayB)出去。这样就能保持每次连接的持续性。
首先我们通过对每段代码做分析,理解他们是怎么运行
IP 配置:
/ ip address
add address=192.168.0.1/24 network=192.168.0.0 broadcast=192.168.0.255 interface=Local
add address=10.111.0.2/24 network=10.111.0.0 broadcast=10.111.0.255 interface=wlan2
add address=10.112.0.2/24 network=10.112.0.0 broadcast=10.112.0.255 interface=wlan1
路由器的两个WAN口地址分别是10.111.0.2/24和10.112.0.2/24,LAN口的地址是192.168.0.1/24,内网网卡命名为LOCAL
Mangle配置
下面是通过nth来分配用户连接会话:
/ ip firewall mangle
add chain=prerouting in-interface=Local connection-state=new nth=1,1,0 \
action=mark-connection new-connection-mark=odd passthrough=yes
add chain=prerouting in-interface=Local connection-mark=odd action=mark-routing \
new-routing-mark=odd passthrough=no
首先,每隔一个包建立一个新的会话,并用“odd”做标记,因此所有属于同一会话的连续的数据包将被放到标记为odd的连接中,注意,我们将这些数据包传递给第二条和第三条规则(passthrough=yes)。第二条规则在prerouting链表将所有标记属于“odd”的连接放入routing mark的“odd”标记中,并将以上连接在此停止处理。
/ ip firewall mangle
add chain=prerouting in-interface=Local connection-state=new nth=1,1,1 \
action=mark-connection new-connection-mark=even passthrough=yes
add chain=prerouting in-interface=Local connection-mark=even action=mark-routing \
new-routing-mark=even passthrough=no
这两个规则将剩下的传输做之前的同样处理,并将剩下一半的传输标记为“even”。
NAT配置
/ ip firewall nat
add chain=srcnat connection-mark=odd action=src-nat to-addresses=10.111.0.2 \
to-ports=0-65535
add chain=srcnat connection-mark=even action=src-nat to-addresses=10.112.0.2 \
to-ports=0-65535
被标记为ODD的数据NAT为10.111.0.2,以EVEN为标记的数据NAT为10.112.0.2
路由配置
/ ip route
add dst-address=0.0.0.0/0 gateway=10.111.0.1 scope=255 target-scope=10 routing-mark=odd
add dst-address=0.0.0.0/0 gateway=10.112.0.1 scope=255 target-scope=10 routing-mark=even
被标记为ODD的数据用10.111.0.1为网关,同样,被标记为EVEN的数据从10.112.0.1这个网关出去。
/ ip route
add dst-address=0.0.0.0/0 gateway=10.112.0.1 scope=255 target-scope=10
最后,没有做任何标记的数据从10.112.0.2这个网关出去,也是给路由器一个默然网关。
如果是三条或者多条线路的负载均衡则需要修改NTH参数,比如是三条线路的设置修改为: nth=Every,Counter,Packet nth=2,3,0. 2,3,1. 2,3,2 ,共需要在mangle中添加三组规则。而三条以上则以持类推。在3.0中nth参数做了修改,去掉了counter属性,输入的时候我只需要设置Every,Packet两个参数。 |
|