Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Commit

Permalink
Add --metered option to indicate that network is metered and/or shoul…
Browse files Browse the repository at this point in the history
…d be used sparingly

This extremely useful StackExchange post summarizes the different ways of
indicating this to clients: https://unix.stackexchange.com/a/631812

1. Microsoft's "network cost information element"
   (https://learn.microsoft.com/fi-fi/windows-hardware/drivers/mobilebroadband/network-cost-information-element)
   which seems to be quasi-standard at this point.

   Recent versions of NetworkManager also understand it, but due to a
   mistake in earlier versions of Microsoft's documentation, older versions
   parse the element incorrectly (gory details at
   https://openwrt.org/docs/guide-user/network/wifi/ms-meteredconnection#under_the_hood).

2. Apple's vendor element
   (https://apple.stackexchange.com/a/457630) seems to be undocumented,
   but it can be copied/used as is.

3. Adding the value "ANDROID_METERED" as part of the force-sent DHCP option
   43 causes Android devices to perceive this wireless network as metered
   (see https://unix.stackexchange.com/a/631812 as well as
   https://www.lorier.net/docs/android-metered.html).
  • Loading branch information
dlenski committed Dec 3, 2023
1 parent 6e83bbc commit a08bb4d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
34 changes: 32 additions & 2 deletions create_ap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ usage() {
echo " Use: 'nat' for NAT (default)"
echo " 'bridge' for bridging"
echo " 'none' for no Internet sharing (equivalent to -n)"
echo " --metered Indicate to clients that WiFi is metered and/or should be used sparingly."
echo " --wps Enable WPS (Wireless Protected Setup) with push button (PBC) and PIN modes"
echo " --psk Use 64 hex digits pre-shared-key instead of passphrase"
echo " --hidden Make the Access Point hidden (do not broadcast the SSID)"
Expand Down Expand Up @@ -672,6 +673,7 @@ MAC_FILTER=0
MAC_FILTER_ACCEPT=/etc/hostapd/hostapd.accept
ISOLATE_CLIENTS=0
SHARE_METHOD=nat
METERED=0
IEEE80211N=0
IEEE80211AC=0
HT_CAPAB='[HT40+]'
Expand All @@ -697,7 +699,7 @@ REDIRECT_TO_LOCALHOST=0
CONFIG_OPTS=(CHANNEL GATEWAY WPA_VERSION ETC_HOSTS DHCP_DNS DHCP_DNS6 NO_DNS NO_DNSMASQ HIDDEN MAC_FILTER MAC_FILTER_ACCEPT ISOLATE_CLIENTS
SHARE_METHOD IEEE80211N IEEE80211AC HT_CAPAB VHT_CAPAB DRIVER NO_VIRT COUNTRY FREQ_BAND
NEW_MACADDR DAEMONIZE DAEMON_PIDFILE DAEMON_LOGFILE NO_HAVEGED WIFI_IFACE INTERNET_IFACE
SSID PASSPHRASE USE_PSK BEACON_INTERVAL DTIM_PERIOD IPV6 ADDN_HOSTS WPS)
SSID PASSPHRASE USE_PSK BEACON_INTERVAL DTIM_PERIOD IPV6 ADDN_HOSTS WPS METERED)

FIX_UNMANAGED=0
LIST_RUNNING=0
Expand Down Expand Up @@ -1158,7 +1160,7 @@ for ((i=0; i<$#; i++)); do
fi
done

GETOPT_ARGS=$(getopt -o hc:w:g:de:nm: -l "help","hidden","hostapd-debug:","redirect-to-localhost","mac-filter","mac-filter-accept:","isolate-clients","ieee80211n","ieee80211ac","ht_capab:","vht_capab:","driver:","no-virt","fix-unmanaged","country:","freq-band:","mac:","dhcp-dns:","dhcp-dns6:","daemon","pidfile:","logfile:","stop:","list","list-running","list-clients:","version","psk","no-haveged","no-dns","no-dnsmasq","ipv6","mkconfig:","config:","wps","wps-pbc:","wps-pin:" -n "$PROGNAME" -- "$@")
GETOPT_ARGS=$(getopt -o hc:w:g:de:nm: -l "help","hidden","hostapd-debug:","redirect-to-localhost","mac-filter","mac-filter-accept:","isolate-clients","ieee80211n","ieee80211ac","ht_capab:","vht_capab:","driver:","no-virt","fix-unmanaged","country:","freq-band:","mac:","dhcp-dns:","dhcp-dns6:","daemon","pidfile:","logfile:","stop:","list","list-running","list-clients:","version","psk","no-haveged","no-dns","no-dnsmasq","ipv6","mkconfig:","config:","wps","wps-pbc:","wps-pin:","metered" -n "$PROGNAME" -- "$@")
[[ $? -ne 0 ]] && exit 1
eval set -- "$GETOPT_ARGS"

Expand Down Expand Up @@ -1227,6 +1229,10 @@ while :; do
SHARE_METHOD="$1"
shift
;;
--metered)
shift
METERED=1
;;
--ieee80211n)
shift
IEEE80211N=1
Expand Down Expand Up @@ -1801,8 +1807,24 @@ ignore_broadcast_ssid=$HIDDEN
ap_isolate=$ISOLATE_CLIENTS
beacon_int=${BEACON_INTERVAL}
dtim_period=${DTIM_PERIOD}
#device_name=Fnord
#manufacturer=Vark
EOF

if [[ $METERED -ne 0 ]]; then
# Add Microsoft's "network cost information element"
# (https://learn.microsoft.com/fi-fi/windows-hardware/drivers/mobilebroadband/network-cost-information-element)
# and Apple's vendor element that indicates a personal hotspot
# (https://apple.stackexchange.com/questions/199163/how-does-ios-and-os-x-detect-when-a-wi-fi-network-is-a-personal-hotspot)
# The Microsoft element seems to be quasi-standardized now.

# We intentionally set the last byte (which is reserved and
# supposed to always be 00) to 02 in order to work around a bug caused by
# previously-incorrect documentation by Microsoft. See:
# https://openwrt.org/docs/guide-user/network/wifi/ms-meteredconnection#under_the_hood
echo "vendor_elements=dd080050f21102000002dd0a0017f206010103010000" >> $CONFDIR/hostapd.conf
fi

if [[ $WPS -eq 1 ]]; then
echo "Enabling WPS push button (PBC) and PIN modes!"
# WPS configuration: AP configured, do not allow external WPS Registrars,
Expand Down Expand Up @@ -1897,6 +1919,14 @@ dhcp-range=${GATEWAY%.*}.1,${GATEWAY%.*}.254,255.255.255.0,24h
dhcp-option-force=option:router,${GATEWAY}
dhcp-option-force=option:dns-server,${DHCP_DNS}
EOF

# Android devices apparently detect metered networks using
# this DHCP vendor option, according to
# https://www.lorier.net/docs/android-metered.html
if [[ $METERED -ne 0 ]]; then
echo "dhcp-option-force=43,ANDROID_METERED" >> $CONFDIR/dnsmasq.conf
fi

if [[ $IPV6 -ne 0 ]]; then
if [[ "$NO_DNS" -ne 0 ]]; then
RA_MODE="ra-names"
Expand Down
2 changes: 2 additions & 0 deletions create_ap.conf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ GATEWAY=10.0.0.1
IPV6=0
# Set whether the connected clients can see each other or not
ISOLATE_CLIENTS=0
# Indicate to clients that WiFi is metered and/or should be used sparingly
METERED=0

########## Wireless Options ##########

Expand Down

0 comments on commit a08bb4d

Please sign in to comment.