2 # travelmate, a wlan connection manager for travel router
3 # written by Dirk Brenken (dev@brenken.org)
5 # This is free software, licensed under the GNU General Public License v3.
6 # You should have received a copy of the GNU General Public License
7 # along with this program. If not, see <http://www.gnu.org/licenses/>.
12 PATH
="/usr/sbin:/usr/bin:/sbin:/bin"
21 trm_captiveurl
="http://captive.apple.com"
29 trm_rtfile
="/tmp/trm_runtime.json"
30 trm_fetch
="$(command -v uclient-fetch)"
31 trm_iwinfo
="$(command -v iwinfo)"
32 trm_wpa
="$(command -v wpa_supplicant)"
33 trm_action
="${1:-"start"}"
34 trm_pidfile
="/var/run/travelmate.pid"
36 # trim leading and trailing whitespace characters
42 trim
="${trim#"${trim%%[![:space:]]*}"}"
43 trim
="${trim%"${trim##*[![:space:]]}"}"
47 # load travelmate environment
51 local IFS sys_call sys_desc sys_model
53 # (re-)initialize global list variables
55 unset trm_devlist trm_stalist trm_radiolist trm_active_sta
57 # get system information
59 sys_call
="$(ubus -S call system board 2>/dev/null)"
60 if [ -n "${sys_call}" ]
62 sys_desc
="$(printf '%s' "${sys_call}" | jsonfilter -e '@.release.description')"
63 sys_model
="$(printf '%s' "${sys_call}" | jsonfilter -e '@.model')"
64 trm_sysver
="${sys_model}, ${sys_desc}"
67 # get eap capabilities and rebind protection setting
69 trm_eap
="$("${trm_wpa}" -veap >/dev/null 2>&1; printf "%u
" ${?})"
70 trm_rebind
="$(uci_get dhcp "@dnsmasq
[0]" rebind_protection)"
72 # load config and check 'enabled' option
76 local name
="${1}" type="${2}"
77 if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]
81 local option
="${1}" value
="${2}"
82 eval "${option}=\"${value}\""
91 config_load travelmate
93 if [ ${trm_enabled} -ne 1 ]
95 f_log
"info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
99 # validate input ranges
101 if [ ${trm_minquality} -lt 20 ] ||
[ ${trm_minquality} -gt 80 ]
105 if [ ${trm_listexpiry} -lt 0 ] ||
[ ${trm_listexpiry} -gt 300 ]
109 if [ ${trm_maxretry} -lt 1 ] ||
[ ${trm_maxretry} -gt 10 ]
113 if [ ${trm_maxwait} -lt 20 ] || [ ${trm_maxwait} -gt 40 ] || [ ${trm_maxwait} -ge ${trm_timeout} ]
117 if [ ${trm_timeout} -lt 30 ] || [ ${trm_timeout} -gt 300 ] || [ ${trm_timeout} -le ${trm_maxwait} ]
122 # load json runtime file
124 json_load_file
"${trm_rtfile}" >/dev
/null
2>&1
125 json_select data
>/dev
/null
2>&1
130 json_add_object
"data"
134 # gather radio information & bring down all STA interfaces
138 local IFS config
="${1}" proactive
="${2}"
139 local mode
="$(uci_get wireless "${config}" mode)"
140 local network
="$(uci_get wireless "${config}" network)"
141 local radio
="$(uci_get wireless "${config}" device)"
142 local disabled
="$(uci_get wireless "${config}" disabled)"
143 local eaptype
="$(uci_get wireless "${config}" eap_type)"
145 if [ -n "${config}" ] && [ -n "${radio}" ] && [ -n "${mode}" ] && [ -n "${network}" ]
147 if [ -z "${trm_radio}" ] && [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo "${radio}")" ]
149 trm_radiolist="$
(f_trim
"${trm_radiolist} ${radio}")"
150 elif [ -n "${trm_radio}" ] && [ -z "${trm_radiolist}" ]
152 trm_radiolist="$
(f_trim
"$(printf "%s
" "${trm_radio}" | \
153 awk '{while(match(tolower($0),/radio[0-9]/)){ORS=" ";print substr(tolower($0),RSTART,RLENGTH);$0=substr($0,RSTART+RLENGTH)}}')")"
155 if [ "${mode}" = "sta" ] && [ "${network}" = "${trm_iface}" ]
157 if ([ -z "${disabled}" ] || [ "${disabled}" = "0" ]) && ([ ${proactive} -eq 0 ] || [ "${trm_ifstatus}" != "true
" ])
159 uci_set wireless "${config}" disabled 1
160 elif [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ -z "${trm_active_sta}" ] && [ ${proactive} -eq 1 ]
162 trm_active_sta
="${config}"
164 if [ -z "${eaptype}" ] || ([ -n "${eaptype}" ] && [ ${trm_eap:-1} -eq 0 ])
166 trm_stalist="$
(f_trim
"${trm_stalist} ${config}-${radio}")"
170 f_log "debug
" "f_prep
::: config
: ${config}, mode: ${mode}, network: ${network}, radio: ${radio}, trm_radio: ${trm_radio:-"-"}, trm_active_sta: ${trm_active_sta:-"-"}, proactive: ${proactive}, trm_eap: ${trm_eap:-"-"}, trm_rebind: ${trm_rebind:-"-"}, disabled: ${disabled}"
173 # check interface status
177 local IFS ifname radio dev_status last_status config sta_essid sta_bssid result cp_domain wait mode="${1}" status="${2:-"false"}"
179 if [ "${mode}" != "initial
" ] && [ "${status}" = "false
" ]
181 ubus call network reload
182 wait=$(( ${trm_maxwait} / 6 ))
187 while [ ${wait} -le ${trm_maxwait} ]
189 dev_status="$
(ubus
-S call network.wireless status
2>/dev
/null
)"
190 if [ -n "${dev_status}" ]
192 if [ "${mode}" = "dev
" ]
194 if [ "${trm_ifstatus}" != "${status}" ]
196 trm_ifstatus="${status}"
199 for radio in ${trm_radiolist}
201 result="$
(printf "%s" "${dev_status}" | jsonfilter
-l1 -e "@.${radio}.up")"
202 if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo "${radio}")" ]
204 trm_devlist
="$(f_trim "${trm_devlist} ${radio}")"
207 if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ ${wait} -eq ${trm_maxwait} ]
209 ifname="${trm_devlist}"
214 elif [ "${mode}" = "rev" ]
218 ifname="$
(printf "%s" "${dev_status}" | jsonfilter
-l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
219 if [ -n "${ifname}" ]
221 trm_ifquality="$
(${trm_iwinfo} ${ifname} info 2>/dev/null | awk -F "[\/| ]" '/Link Quality:/{printf "%i\n", (100 / $NF * $(NF-1)) }')"
222 if [ ${trm_captive} -eq 1 ]
224 result="$(${trm_fetch} --timeout=$(( ${trm_maxwait} / 3 )) "${trm_captiveurl}" -O /dev/null 2>&1 | \
225 awk '/^Failed to redirect|^Redirected
/{printf "%s" "net cp \047"$NF"\047";exit}/^Download completed
/{printf "%s" "net ok";exit}/^Failed|^Connection error
/{printf "%s" "net nok";exit}')"
227 if [ ${trm_ifquality} -ge ${trm_minquality} ] && ([ ${trm_captive} -eq 0 ] || [ ${trm_netcheck} -eq 0 ] || [ "${result%/*}" != "net nok" ])
229 trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
230 if [ "${trm_ifstatus}" = "true" ]
232 if [ ${trm_captive} -eq 1 ]
234 cp_domain="$(printf "%s" "${result}" | awk -F "['|
]" '/^net cp/{printf "%s
" $4}')"
235 if [ -n "${cp_domain}" ] && [ ${trm_rebind:-0} -eq 1 ] && [ -x "/etc/init.d/dnsmasq" ]
237 while [ -n "${cp_domain}" ] && [ -z "$(uci_get dhcp "@dnsmasq
[0]" rebind_domain | grep -Fo "${cp_domain}")" ]
239 uci
-q add_list dhcp.@dnsmasq
[0].rebind_domain
="${cp_domain}"
241 /etc
/init.d
/dnsmasq reload
242 f_log
"info" "captive portal domain '${cp_domain}' added to rebind whitelist"
243 result
="$(${trm_fetch} --timeout=$(( ${trm_maxwait} / 3 )) "${trm_captiveurl}" -O /dev
/null
2>&1 | \
244 awk '/^Failed to redirect|^Redirected/{printf "%s" "net cp \047"$NF"\047";exit}/^Download completed/{printf "%s" "net ok";exit}/^Failed|^Connection error/{printf "%s" "net nok";exit}')"
245 cp_domain="$
(printf "%s" "${result}" |
awk -F "['| ]" '/^net cp/{printf "%s" $4}')"
249 trm_connection="${result}/${trm_ifquality}"
254 if [ -n "${trm_connection}" ]
256 sta_essid="$
(printf "%s" "${dev_status}" | jsonfilter
-l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
257 sta_bssid="$
(printf "%s" "${dev_status}" | jsonfilter
-l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
258 if [ ${trm_ifquality} -lt ${trm_minquality} ]
260 f_log "info
" "uplink
'${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality})"
261 elif [ ${trm_captive} -eq 1 ] && [ ${trm_netcheck} -eq 1 ] && [ "${result%/*}" = "net nok" ]
263 f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' has no internet (${result})"
266 trm_ifstatus="${status}"
272 if [ -n "${trm_connection}" ]
275 trm_ifstatus="${status}"
285 f_log "debug
" "f_check
::: mode
: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, quality: ${trm_ifquality}, result: ${result:-"-"}, connection: ${trm_connection:-"-"}, wait: ${wait}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}, netcheck: ${trm_netcheck}"
288 # update runtime information
292 local IFS config d1 d2 d3 last_date last_station sta_iface sta_radio sta_essid sta_bssid last_status dev_status status="${trm_ifstatus}" faulty_list faulty_station="${1}"
294 dev_status="$
(ubus
-S call network.wireless status
2>/dev
/null
)"
295 if [ -n "${dev_status}" ]
297 config="$
(printf "%s" "${dev_status}" | jsonfilter
-l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
298 if [ -n "${config}" ]
300 sta_iface="$
(uci_get wireless
"${config}" network
)"
301 sta_radio="$
(uci_get wireless
"${config}" device
)"
302 sta_essid="$
(uci_get wireless
"${config}" ssid
)"
303 sta_bssid="$
(uci_get wireless
"${config}" bssid
)"
307 json_get_var last_date "last_rundate
"
308 json_get_var last_station "station_id
"
309 if [ "${status}" = "true
" ]
311 status="connected
(${trm_connection:-"-"})"
312 json_get_var last_status "travelmate_status
"
313 if [ "${last_status}" = "running / not connected" ] || [ "${last_station}" != "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" ]
315 last_date="$
(/bin
/date "+%Y.%m.%d-%H:%M:%S")"
319 status="running
/ not connected
"
321 if [ -z "${last_date}" ]
323 last_date="$
(/bin
/date "+%Y.%m.%d-%H:%M:%S")"
326 json_get_var faulty_list "faulty_stations
"
327 if [ -n "${faulty_list}" ] && [ ${trm_listexpiry} -gt 0 ]
329 d1="$
(/bin
/date -d "${last_date}" "+%s")"
330 d2="$
(/bin
/date "+%s")"
331 d3=$(( (d2 - d1) / 60 ))
332 if [ ${d3} -ge ${trm_listexpiry} ]
338 if [ -n "${faulty_station}" ]
340 if [ -z "$
(printf "%s" "${faulty_list}" |
grep -Fo "${faulty_station}")" ]
342 faulty_list="$
(f_trim
"${faulty_list} ${faulty_station}")"
345 json_add_string "travelmate_status
" "${status}"
346 json_add_string "travelmate_version
" "${trm_ver}"
347 json_add_string "station_id
" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
348 json_add_string "station_interface
" "${sta_iface:-"-"}"
349 json_add_string "faulty_stations
" "${faulty_list}"
350 json_add_string "last_rundate
" "${last_date}"
351 json_add_string "system
" "${trm_sysver}"
352 json_dump > "${trm_rtfile}"
353 f_log "debug
" "f_jsnup
::: config
: ${config:-"-"}, status: ${status:-"-"}, sta_iface: ${sta_iface:-"-"}, sta_radio: ${sta_radio:-"-"}, sta_essid: ${sta_essid:-"-"}, sta_bssid: ${sta_bssid:-"-"}, faulty_list: ${faulty_list:-"-"}, list_expiry: ${trm_listexpiry}"
360 local IFS class="${1}" log_msg="${2}"
362 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
364 logger
-p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
365 if [ "${class}" = "err" ]
369 logger
-p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
375 # main function for connection handling
379 local IFS cnt dev config scan scan_list scan_essid scan_bssid scan_quality faulty_list
380 local station_id sta sta_essid sta_bssid sta_radio sta_iface active_essid active_bssid active_radio
383 f_log
"debug" "f_main ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
384 if [ "${trm_ifstatus}" != "true" ] ||
[ ${trm_proactive} -eq 1 ]
387 config_foreach f_prep wifi-iface
${trm_proactive}
388 if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_active_sta}" ] && [ ${trm_proactive} -eq 1 ]
390 json_get_var station_id "station_id
"
391 active_radio="${station_id%%/*}"
392 active_essid="${station_id%/*}"
393 active_essid="${active_essid#*/}"
394 active_bssid="${station_id##*/}"
396 f_log "debug
" "f_main
::: active_radio
: ${active_radio}, active_essid: \"${active_essid}\", active_bssid: ${active_bssid:-"-"}"
401 json_get_var faulty_list "faulty_stations
"
402 f_log "debug
" "f_main
::: iwinfo
: ${trm_iwinfo:-"-"}, dev_list: ${trm_devlist:-"-"}, sta_list: ${trm_stalist:0:800}, faulty_list: ${faulty_list:-"-"}"
405 for dev in ${trm_devlist}
407 if [ -z "$
(printf "%s" "${trm_stalist}" |
grep -o "\-${dev}")" ]
409 f_log "debug
" "f_main
::: no station on
'${dev}' - continue"
414 for sta in ${trm_stalist}
417 sta_radio="${sta##*-}"
418 sta_essid="$
(uci_get wireless
"${config}" ssid
)"
419 sta_bssid="$
(uci_get wireless
"${config}" bssid
)"
420 sta_iface="$
(uci_get wireless
"${config}" network
)"
421 json_get_var faulty_list "faulty_stations
"
422 if [ -n "$
(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
424 f_log "debug
" "f_main
::: faulty station
'${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' - continue"
427 if [ "${dev}" = "${active_radio}" ] && [ "${sta_essid}" = "${active_essid}" ] && [ "${sta_bssid:-"-"}" = "${active_bssid}" ]
429 f_log "debug
" "f_main
::: active station prioritized
'${active_radio}/${active_essid}/${active_bssid:-"-"}' - break"
432 f_log "debug
" "f_main
::: sta_radio
: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
433 if [ -z "${scan_list}" ]
435 scan_list="$
(f_trim
"$("${trm_iwinfo}" "${dev}" scan 2>/dev/null | \
436 awk 'BEGIN{FS="[/ ]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i};gsub(/,/,".
",var2)}/Quality:/{printf "%i
,%s
,%s
\n",(100/$NF*$(NF-1)),var1,var2}' | \
437 sort -rn | awk 'BEGIN{ORS=","}{print $0}' | awk '{print substr($0,1,4096)}')")"
438 f_log "debug
" "f_main
::: scan_list
: ${scan_list:0:800}"
439 if [ -z "${scan_list}" ]
441 f_log "debug
" "f_main
::: no scan results on
'${dev}' - continue"
448 for scan in ${scan_list}
450 if [ -z "${scan_quality}" ]
452 scan_quality="${scan}"
453 elif [ -z "${scan_bssid}" ]
456 elif [ -z "${scan_essid}" ]
460 if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
462 if [ ${scan_quality} -ge ${trm_minquality} ]
464 if (([ "${scan_essid}" = "\"${sta_essid//,/.}\"" ] && ([ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ])) || \
465 ([ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ])) && [ "${dev}" = "${sta_radio}" ]
467 f_log "debug
" "f_main
::: scan_quality
: ${scan_quality}, scan_essid: ${scan_essid}, scan_bssid: ${scan_bssid:-"-"}"
468 if [ "${dev}" = "${active_radio}" ]
470 unset trm_connection active_radio active_essid active_bssid
471 uci_set wireless "${trm_active_sta}" disabled 1
477 while [ ${cnt} -le ${trm_maxretry} ]
479 uci_set wireless "${config}" disabled 0
481 if [ "${trm_ifstatus}" = "true
" ]
485 f_log "info
" "connected to uplink
'${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
488 uci -q revert wireless
490 if [ ${cnt} -eq ${trm_maxretry} ]
492 faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
493 f_jsnup "${faulty_station}"
494 f_log "info" "uplink disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
498 f_log "info
" "can
't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
502 sleep $(( ${trm_maxwait} / 6 ))
505 unset scan_quality scan_bssid scan_essid
509 unset scan_quality scan_bssid scan_essid
514 unset IFS scan_quality scan_bssid scan_essid
521 # source required system libraries
523 if [ -r "/lib
/functions.sh
" ] && [ -r "/usr
/share
/libubox
/jshn.sh
" ]
525 . "/lib
/functions.sh
"
526 . "/usr
/share
/libubox
/jshn.sh
"
528 f_log "err
" "system libraries not found
"
531 # control travelmate actions
536 if [ -z "${trm_action}" ]
545 sleep ${trm_timeout} 0
551 if [ ${rc} -eq 0 ] || ([ ${rc} -ne 0 ] && [ "${trm_ifstatus}" = "false" ])
556 elif [ "${trm_action}" = "stop" ]
559 f_log
"info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
562 f_log
"info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"