How to detect metered connection?

I have a rsync script that runs regularly, but want to skip it when the router is connected to a detected metered wifi connection. Any ideas?

I made some headway.

In

/etc/udhcpc.user

I wrote this:

DHCPOPTS="$(uci get network.wwan.reqopts)" || uci set network.wwan.reqopts='43' && uci commit

if [ "${DHCPOPTS}" = "43" ]; then
   if [ "${opt43}" = "414e44524f49445f4d455445524544" ]; then
      # we don't commit here so its not saved on reboot
      uci set network.wwan.metered=1
      # also set dnsmasq to tell clients that we are metered
      uci set dhcp.lan.dhcp_option='43,ANDROID_METERED'
   else
      uci del network.wwan.metered
      uci del dhcp.lan.dhcp_option
   fi
fi

This tells udhcpc to request DHCP option 43 - the vendor specified information tag. This is set on Android and others to signify a metered tethered connection. The output received from an Android DHCP server is ‘414e44524f49445f4d455445524544’ which is ‘ANDROID_METERED’ in hex. I also set dnsmasq to tell clients that we are on a metered connection. This appears to work on my Android phones connecting to the GL-Inet router, but not my Windows machine.

If it is found that we are metered I set a temporary uci variable interface.wwan.metered to 1.

Then in my scripts that I don’t want to run on a metered connection I run:

uci get network.wwan.metered

…if the output value is 1 I don’t run it.

If you want to use this, paste the script into /etc/udhcpc.user - this file doesnt normally exist. Then reboot your router (you may need to reboot twice) – or run:

killall udhcpc
ubus call network restart

To remove it, run :

rm /etc/udhcpc.user
uci del network.wwan.reqopts
uci commit

Then reboot your router.

This is only tested on a wireless tethered connection - It wont work as is for a usb tether or usb modem.

1 Like