Android9下用ethernet 的Tether模式来做路由器功能
试用iptables实现:
# iptables -t nat -I POSTROUTING 1 -o wlan0 -j MASQUERADE 开启NAT
# iptables -t filter -A tetherctrl_FORWARD -i wlan0 -o eth0 -m state –state ESTABLISHED,RELATED -g tetherctrl_counters
# iptables -t filter -A tetherctrl_FORWARD -i eth0 -o wlan0 -m state –state INVALID -j DROP
# iptables -t filter -A tetherctrl_FORWARD -i eth0 -o wlan0 -g tetherctrl_counters
# iptables -t filter -D tetherctrl_FORWARD -j DROP
# iptables -t filter -A tetherctrl_FORWARD -j DROP
# iptables -t filter -D tetherctrl_counters -i eth0 -o wlan0 -j RETURN
# iptables -t filter -D tetherctrl_counters -i wlan0 -o eth0 -j RETURN
# iptables -t filter -A tetherctrl_counters -i eth0 -o wlan0 -j RETURN
# iptables -t filter -A tetherctrl_counters -i wlan0 -o eth0 -j RETURN
# ip rule add iif eth0 table wlan0 prio 18000
# ip route add table local_network 172.16.0.0/24 dev eth0 proto static scope link 增加路由到local_network路由表
# ip route flush cache
# sysctl -w net.ipv4.ip_forward=1 开启forward模式
另一种实现方式(模拟Tether方式):
先从id为101的physical类型网络移除老的eth0,然后把eth0加入local类型网络,这里的101和local都是Android的NetId概念:
# ndc netd network interface remove 101 eth0
# ndc netd network interface add local eth0
添加local_network路由:
# ndc netd network route add local eth0 172.16.0.0/24
开启NAT:
# ndc netd ipfwd enable actiontec
# ndc netd nat enable eth0 wlan0 1 172.16.0.0/24
添加规则:
# ip rule add iif eth0 table wlan0 prio 18000
博主在吗?有没有相关的代码修改?
代码里实际上也是类似这个样子修改,不过要找到对应的位置