在Android4.2、4.4、5.1上面,参照之前写的《Linux下双网卡NAT组网》一文是可以验证成功的,不过在Android5.1上面,我们参照了系统里的WiFi热点、USB Tether、Bluetooth Tether功能,发现有另一组命令也可实现:
echo 0 > /proc/sys/net/ipv4/ip_forward
/system/bin/iptables -t nat -A natctrl_nat_POSTROUTING -o eth0 -j MASQUERADE
/system/bin/iptables -A natctrl_FORWARD -i eth0 -o ap0 -m state --state ESTABLISHED,RELATED -g natctrl_tether_counters
/system/bin/iptables -A natctrl_FORWARD -i ap0 -o eth0 -m state --state INVALID -j DROP
/system/bin/iptables -A natctrl_FORWARD -i ap0 -o eth0 -g natctrl_tether_counters
/system/bin/iptables -A natctrl_tether_counters -i ap0 -o eth0 -j RETURN
/system/bin/iptables -A natctrl_tether_counters -i eth0 -o ap0 -j RETURN
/system/bin/iptables -D natctrl_FORWARD -j DROP
/system/bin/iptables -A natctrl_FORWARD -j DROP
echo 1 > /proc/sys/net/ipv4/ip_forward
其中,ap0是连接内网的网卡,eth0是连接外网的网卡。