Skip to content

Instantly share code, notes, and snippets.

@updateing
Created April 4, 2018 01:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save updateing/3527984f1de1c1ac24c65b2cf1f650eb to your computer and use it in GitHub Desktop.
Save updateing/3527984f1de1c1ac24c65b2cf1f650eb to your computer and use it in GitHub Desktop.
Android phone as gateway
#!/system/bin/sh
# Share LTE over WiFi STA, use when AP is not possible.
# You have to make sure the phone can connect to LTE and WiFi at the same time,
# e.g. keep portal detection failing.
RMNET_DEV=$(ip route|grep rmnet|cut -d " " -f 3)
WLAN_SUBNET=$(ip route|grep wlan|cut -d " " -f 1)
WLAN_DEV=$(ip route|grep wlan|cut -d " " -f 3)
iptables -F natctrl_FORWARD
echo 1 > /proc/sys/net/ipv4/ip_forward
IPTABLES_MASQ_RULES=$(iptables -t nat -nL POSTROUTING | grep MASQUERADE)
if [ -z "$IPTABLES_MASQ_RULES" ]; then
iptables -t nat -A POSTROUTING -s $WLAN_SUBNET -j MASQUERADE
fi
ip route add table $RMNET_DEV $WLAN_SUBNET dev $WLAN_DEV
#!/system/bin/sh
# Share VPN over WiFi STA, use when AP is not possible.
# You have to make sure the phone can connect to VPN and WiFi at the same time,
# which means there must be ways other than WiFi to establish the VPN connection.
# In this way you get 3 transports LTE/VPN/WiFi active at the same time.
# You may even try to extend the rules to get all possible transports like
# USB/BT/WiFi/LTE/VPN active simultaneously :-)
WLAN_DEV=$(ip route|grep wlan|cut -d " " -f 3)
LAN_HOST=192.168.1.10
TUN_DEV=tun0
iptables -F natctrl_FORWARD
echo 1 > /proc/sys/net/ipv4/ip_forward
IPTABLES_MASQ_RULES=$(iptables -t nat -nL POSTROUTING | grep MASQUERADE)
if [ -z "$IPTABLES_MASQ_RULES" ]; then
iptables -t nat -A POSTROUTING -s $LAN_HOST -j MASQUERADE
fi
ROUTE_TEST_RESULT=$(ip route get 8.8.8.8 from $LAN_HOST iif $WLAN_DEV)
if [ -z `echo "$ROUTE_TEST_RESULT" | grep "table 61"` ]; then
ip rule add from $LAN_HOST lookup 61
fi
ip route add table 61 $LAN_HOST dev $WLAN_DEV
ip route add table 61 default dev $TUN_DEV
ip route add table local_network $LAN_HOST dev $WLAN_DEV
@gcleaves
Copy link

Hi. I'm trying to get these scripts to work without success. When I run share_lte.sh I see a warning that chain natctrl_FORWARD does not exist. Then when I set my Android IP address as the "router" on my Mac, I can't reach the internet.

My Android is rooted and running Lineage/Android 10 and I've enabled the developer option to keep WLAN and mobile data on at the same time. But I don't understand your comment, "You have to make sure the phone can connect to LTE and WiFi at the same time, e.g. keep portal detection failing." Could that be the problem?

@updateing
Copy link
Author

@gcleaves Missing natctrl_FORWARD could imply that these commands are out of date. I have not tried to do this reverse tethering thing on later Android versions.

@gcleaves
Copy link

Looks like it's tetherctrl_FORWARD nowadays.

@jnischler
Copy link

just testet on android 12
tetherctrl_FORWARD is correct
but unable to get routing working ...
i have two ethernet connections

eth0 LAN
eth1 WAN

would like to use nat from lan to wan but no success

did somone try this ?

regards
Julian

@gcleaves
Copy link

Hi. Not sure if my notes here might help you: https://gist.github.com/gcleaves/ec7a06f8c0bd436c1bc2eb922a246d26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment