fwknop: init script improvements
[feed/packages.git] / net / travelmate / files / travelmate.sh
1 #!/bin/sh
2 # travelmate, a wlan connection manager for travel router
3 # written by Dirk Brenken (dev@brenken.org)
4
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/>.
8
9 # (s)hellcheck exceptions
10 # shellcheck disable=1091 disable=2039 disable=2143 disable=2181 disable=2188
11
12 # set initial defaults
13 #
14 LC_ALL=C
15 PATH="/usr/sbin:/usr/bin:/sbin:/bin"
16 trm_ver="1.5.3"
17 trm_enabled=0
18 trm_debug=0
19 trm_iface="trm_wwan"
20 trm_captive=1
21 trm_proactive=1
22 trm_netcheck=0
23 trm_autoadd=0
24 trm_captiveurl="http://captive.apple.com"
25 trm_scanbuffer=1024
26 trm_minquality=35
27 trm_maxretry=5
28 trm_maxwait=30
29 trm_timeout=60
30 trm_listexpiry=0
31 trm_radio=""
32 trm_connection=""
33 trm_rtfile="/tmp/trm_runtime.json"
34 trm_wifi="$(command -v wifi)"
35 trm_wificmd="reload"
36 trm_fetch="$(command -v uclient-fetch)"
37 trm_iwinfo="$(command -v iwinfo)"
38 trm_wpa="$(command -v wpa_supplicant)"
39 trm_logger="$(command -v logger)"
40 trm_action="${1:-"start"}"
41 trm_pidfile="/var/run/travelmate.pid"
42
43 # load travelmate environment
44 #
45 f_env()
46 {
47 local IFS check wpa_checks
48
49 # (re-)initialize global list variables
50 #
51 unset trm_devlist trm_stalist trm_radiolist trm_active_sta
52
53 # get system information
54 #
55 trm_sysver="$(ubus -S call system board 2>/dev/null | jsonfilter -e '@.model' -e '@.release.description' | \
56 awk 'BEGIN{ORS=", "}{print $0}' | awk '{print substr($0,1,length($0)-2)}')"
57
58 # load config and check 'enabled' option
59 #
60 config_cb()
61 {
62 local name="${1}" type="${2}"
63 if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]
64 then
65 option_cb()
66 {
67 local option="${1}" value="${2}"
68 eval "${option}=\"${value}\""
69 }
70 else
71 option_cb()
72 {
73 return 0
74 }
75 fi
76 }
77 config_load travelmate
78
79 if [ "${trm_enabled}" -ne 1 ]
80 then
81 f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
82 > "${trm_pidfile}"
83 exit 0
84 fi
85
86 # get wpa_supplicant capabilities
87 #
88 wpa_checks="eap sae owe"
89 for check in ${wpa_checks}
90 do
91 if [ -x "${trm_wpa}" ]
92 then
93 eval "trm_${check}check=\"$("${trm_wpa}" -v${check} >/dev/null 2>&1; printf "%u" "${?}")\""
94 else
95 eval "trm_${check}check=\"1\""
96 fi
97 done
98
99 # get wifi reconf capabilities
100 #
101 if [ -n "$(grep -F "reconf" "${trm_wifi}" 2>/dev/null)" ]
102 then
103 trm_wificmd="reconf"
104 fi
105
106 # enable 'disabled' wifi devices
107 #
108 config_load wireless
109 config_foreach f_prepdev wifi-device
110 if [ -n "$(uci -q changes "wireless")" ]
111 then
112 uci_commit "wireless"
113 "${trm_wifi}" "${trm_wificmd}"
114 sleep $((trm_maxwait/6))
115 fi
116
117 # validate input ranges
118 #
119 if [ "${trm_minquality}" -lt 20 ] || [ "${trm_minquality}" -gt 80 ]
120 then
121 trm_minquality=35
122 fi
123 if [ "${trm_listexpiry}" -lt 0 ] || [ "${trm_listexpiry}" -gt 300 ]
124 then
125 trm_listexpiry=0
126 fi
127 if [ "${trm_maxretry}" -lt 1 ] || [ "${trm_maxretry}" -gt 10 ]
128 then
129 trm_maxretry=5
130 fi
131 if [ "${trm_maxwait}" -lt 20 ] || [ "${trm_maxwait}" -gt 40 ] || [ "${trm_maxwait}" -ge "${trm_timeout}" ]
132 then
133 trm_maxwait=30
134 fi
135 if [ "${trm_timeout}" -lt 30 ] || [ "${trm_timeout}" -gt 300 ] || [ "${trm_timeout}" -le "${trm_maxwait}" ]
136 then
137 trm_timeout=60
138 fi
139
140 # load json runtime file
141 #
142 json_load_file "${trm_rtfile}" >/dev/null 2>&1
143 json_select data >/dev/null 2>&1
144 if [ "${?}" -ne 0 ]
145 then
146 > "${trm_rtfile}"
147 json_init
148 json_add_object "data"
149 fi
150 f_log "debug" "f_env ::: trm_eapcheck: ${trm_eapcheck:-"-"}, trm_saecheck: ${trm_saecheck:-"-"}, trm_owecheck: ${trm_owecheck:-"-"}, trm_wificmd: ${trm_wificmd}"
151 }
152
153 # trim leading and trailing whitespace characters
154 #
155 f_trim()
156 {
157 local IFS trim="${1}"
158
159 trim="${trim#"${trim%%[![:space:]]*}"}"
160 trim="${trim%"${trim##*[![:space:]]}"}"
161 printf '%s' "${trim}"
162 }
163
164 # prepare the 'wifi-device' sections
165 #
166 f_prepdev()
167 {
168 local IFS disabled config="${1}"
169
170 disabled="$(uci_get "wireless" "${config}" "disabled")"
171 if [ "${disabled}" = "1" ]
172 then
173 uci_set wireless "${config}" disabled 0
174 fi
175 f_log "debug" "f_prepdev ::: config: ${config}, disabled: ${disabled:-"-"}"
176 }
177
178 # prepare the 'wifi-iface' sections
179 #
180 f_prepif()
181 {
182 local IFS mode network radio encryption eaptype disabled config="${1}" proactive="${2}"
183
184 mode="$(uci_get "wireless" "${config}" "mode")"
185 network="$(uci_get "wireless" "${config}" "network")"
186 radio="$(uci_get "wireless" "${config}" "device")"
187 encryption="$(uci_get "wireless" "${config}" "encryption")"
188 eaptype="$(uci_get "wireless" "${config}" "eap_type")"
189 disabled="$(uci_get "wireless" "${config}" "disabled")"
190 if [ -n "${config}" ] && [ -n "${radio}" ] && [ -n "${mode}" ] && [ -n "${network}" ]
191 then
192 if [ -z "${trm_radio}" ] && [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo "${radio}")" ]
193 then
194 trm_radiolist="$(f_trim "${trm_radiolist} ${radio}")"
195 elif [ -n "${trm_radio}" ] && [ -z "${trm_radiolist}" ]
196 then
197 trm_radiolist="$(f_trim "$(printf "%s" "${trm_radio}" | \
198 awk '{while(match(tolower($0),/radio[0-9]/)){ORS=" ";print substr(tolower($0),RSTART,RLENGTH);$0=substr($0,RSTART+RLENGTH)}}')")"
199 fi
200 if [ "${mode}" = "sta" ] && [ "${network}" = "${trm_iface}" ]
201 then
202 if { [ -z "${disabled}" ] || [ "${disabled}" = "0" ]; } && { [ "${proactive}" -eq 0 ] || [ "${trm_ifstatus}" != "true" ]; }
203 then
204 uci_set wireless "${config}" disabled 1
205 elif [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ "${proactive}" -eq 1 ]
206 then
207 if [ -z "${trm_active_sta}" ]
208 then
209 trm_active_sta="${config}"
210 else
211 uci_set wireless "${config}" disabled 1
212 fi
213 fi
214 if [ -z "${eaptype}" ] || { [ -n "${eaptype}" ] && [ "${trm_eapcheck}" -eq 0 ]; }
215 then
216 if { [ "${encryption%-*}" != "sae" ] && [ "${encryption%-*}" != "wpa3" ] && [ "${encryption}" != "owe" ]; } || \
217 { { [ "${encryption%-*}" = "sae" ] || [ "${encryption%-*}" = "wpa3" ]; } && [ "${trm_saecheck}" -eq 0 ]; } || \
218 { [ "${encryption}" = "owe" ] && [ "${trm_owecheck}" -eq 0 ]; }
219 then
220 trm_stalist="$(f_trim "${trm_stalist} ${config}-${radio}")"
221 fi
222 fi
223 fi
224 fi
225 f_log "debug" "f_prepif ::: config: ${config}, mode: ${mode}, network: ${network}, radio: ${radio}, trm_radio: ${trm_radio:-"-"}, trm_active_sta: ${trm_active_sta:-"-"}, proactive: ${proactive}, disabled: ${disabled}"
226 }
227
228 # check net status
229 #
230 f_net()
231 {
232 local IFS result
233
234 result="$(${trm_fetch} --timeout=$((trm_maxwait/6)) "${trm_captiveurl}" -O /dev/null 2>&1 | \
235 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}')"
236 printf "%s" "${result}"
237 f_log "debug" "f_net ::: fetch: ${trm_fetch}, timeout: $((trm_maxwait/6)), url: ${trm_captiveurl}, result: ${result}"
238 }
239
240 # check interface status
241 #
242 f_check()
243 {
244 local IFS ifname radio dev_status config sta_essid sta_bssid result uci_essid uci_bssid login_command login_command_args wait_time=1 mode="${1}" status="${2:-"false"}" cp_domain="${3:-"false"}"
245
246 if [ "${mode}" != "initial" ] && [ "${mode}" != "dev" ] && [ "${status}" = "false" ]
247 then
248 "${trm_wifi}" "${trm_wificmd}"
249 sleep $((trm_maxwait/6))
250 fi
251
252 while [ "${wait_time}" -le "${trm_maxwait}" ]
253 do
254 dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
255 if [ -n "${dev_status}" ]
256 then
257 if [ "${mode}" = "dev" ]
258 then
259 if [ "${trm_ifstatus}" != "${status}" ]
260 then
261 trm_ifstatus="${status}"
262 f_jsnup
263 fi
264 for radio in ${trm_radiolist}
265 do
266 result="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e "@.${radio}.up")"
267 if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo "${radio}")" ]
268 then
269 trm_devlist="$(f_trim "${trm_devlist} ${radio}")"
270 fi
271 done
272 if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ "${wait_time}" -eq "${trm_maxwait}" ]
273 then
274 ifname="${trm_devlist}"
275 break
276 else
277 unset trm_devlist
278 fi
279 elif [ "${mode}" = "rev" ]
280 then
281 break
282 else
283 ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
284 if [ -n "${ifname}" ]
285 then
286 trm_ifquality="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk -F "[ ]" '/Link Quality:/{split($NF,var0,"/");printf "%i\n",(var0[1]*100/var0[2])}')"
287 if [ "${mode}" = "initial" ] && [ "${trm_captive}" -eq 1 ]
288 then
289 result="$(f_net)"
290 if [ "${cp_domain}" = "true" ]
291 then
292 cp_domain="$(printf "%s" "${result}" | awk -F "[\\'| ]" '/^net cp/{printf "%s" $4}')"
293 uci_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].config.ssid')"
294 uci_essid="${uci_essid//[^[:alnum:]_]/_}"
295 uci_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].config.bssid')"
296 uci_bssid="${uci_bssid//[^[:alnum:]_]/_}"
297 fi
298 fi
299 if [ "${trm_ifquality}" -ge "${trm_minquality}" ] && [ "${result}" != "net nok" ]
300 then
301 trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
302 if [ "${trm_ifstatus}" = "true" ]
303 then
304 if [ "${mode}" = "sta" ] && [ "${trm_captive}" -eq 1 ]
305 then
306 while true
307 do
308 result="$(f_net)"
309 cp_domain="$(printf "%s" "${result}" | awk -F "[\\'| ]" '/^net cp/{printf "%s" $4}')"
310 uci_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].config.ssid')"
311 uci_essid="${uci_essid//[^[:alnum:]_]/_}"
312 uci_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].config.bssid')"
313 uci_bssid="${uci_bssid//[^[:alnum:]_]/_}"
314 if [ "${trm_netcheck}" -eq 1 ] && [ "${result}" = "net nok" ]
315 then
316 trm_ifstatus="${status}"
317 f_jsnup
318 break 2
319 fi
320 if [ -z "${cp_domain}" ] || [ -n "$(uci_get "dhcp" "@dnsmasq[0]" "rebind_domain" | grep -Fo "${cp_domain}")" ]
321 then
322 break
323 fi
324 uci -q add_list dhcp.@dnsmasq[0].rebind_domain="${cp_domain}"
325 f_log "info" "captive portal domain '${cp_domain}' added to to dhcp rebind whitelist"
326 if [ -z "$(uci_get "travelmate" "${trm_radio}_${uci_essid}_${uci_bssid}")" ]
327 then
328 uci_add travelmate "login" "${trm_radio}_${uci_essid}_${uci_bssid}"
329 uci_set travelmate "${trm_radio}_${uci_essid}_${uci_bssid}" "command" "none"
330 f_log "info" "captive portal login section '${trm_radio}_${uci_essid}_${uci_bssid}' added to travelmate config section"
331 fi
332 done
333 if [ -n "$(uci -q changes "dhcp")" ]
334 then
335 uci_commit "dhcp"
336 /etc/init.d/dnsmasq reload
337 fi
338 if [ -n "$(uci -q changes "travelmate")" ]
339 then
340 uci_commit "travelmate"
341 fi
342 fi
343 if [ -n "${cp_domain}" ] && [ "${cp_domain}" != "false" ] && [ -n "${uci_essid}" ] && [ "${trm_captive}" -eq 1 ]
344 then
345 trm_connection="${result:-"-"}/${trm_ifquality}"
346 f_jsnup
347 login_command="$(uci_get "travelmate" "${trm_radio}_${uci_essid}_${uci_bssid}" "command")"
348 if [ -x "${login_command}" ]
349 then
350 login_command_args="$(uci_get "travelmate" "${trm_radio}_${uci_essid}_${uci_bssid}" "command_args")"
351 "${login_command}" ${login_command_args} >/dev/null 2>&1
352 rc=${?}
353 f_log "info" "captive portal login '${login_command:0:40} ${login_command_args:0:20}' for '${cp_domain}' has been executed with rc '${rc}'"
354 if [ "${rc}" -eq 0 ]
355 then
356 result="$(f_net)"
357 fi
358 fi
359 fi
360 trm_connection="${result:-"-"}/${trm_ifquality}"
361 f_jsnup
362 break
363 fi
364 elif [ -n "${trm_connection}" ]
365 then
366 sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
367 sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
368 if [ "${trm_ifquality}" -lt "${trm_minquality}" ]
369 then
370 unset trm_connection
371 trm_ifstatus="${status}"
372 f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality})"
373 elif [ "${trm_netcheck}" -eq 1 ] && [ "${result}" = "net nok" ]
374 then
375 unset trm_connection
376 trm_ifstatus="${status}"
377 f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' has no internet (${result})"
378 fi
379 f_jsnup
380 break
381 elif [ "${mode}" = "initial" ]
382 then
383 f_jsnup
384 break
385 fi
386 elif [ -n "${trm_connection}" ]
387 then
388 unset trm_connection
389 trm_ifstatus="${status}"
390 f_jsnup
391 break
392 elif [ "${mode}" = "initial" ]
393 then
394 f_jsnup
395 break
396 fi
397 fi
398 fi
399 wait_time=$((wait_time+1))
400 sleep 1
401 done
402 f_log "debug" "f_check ::: mode: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, connection: ${trm_connection:-"-"}, wait: ${wait_time}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}, netcheck: ${trm_netcheck}"
403 }
404
405 # update runtime information
406 #
407 f_jsnup()
408 {
409 local IFS config d1 d2 d3 last_date last_station sta_iface sta_radio sta_essid sta_bssid last_status dev_status wpa_status status="${trm_ifstatus}" faulty_list faulty_station="${1}"
410
411 dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
412 if [ -n "${dev_status}" ]
413 then
414 config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
415 if [ -n "${config}" ]
416 then
417 sta_iface="$(uci_get "wireless" "${config}" "network")"
418 sta_radio="$(uci_get "wireless" "${config}" "device")"
419 sta_essid="$(uci_get "wireless" "${config}" "ssid")"
420 sta_bssid="$(uci_get "wireless" "${config}" "bssid")"
421 fi
422 fi
423
424 json_get_var last_date "last_rundate"
425 json_get_var last_station "station_id"
426 if [ "${status}" = "true" ]
427 then
428 status="connected (${trm_connection:-"-"})"
429 json_get_var last_status "travelmate_status"
430 if [ "${last_status}" = "running / not connected" ] || [ "${last_station}" != "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" ]
431 then
432 last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
433 fi
434 elif [ "${status}" = "error" ]
435 then
436 unset trm_connection
437 status="program error"
438 else
439 unset trm_connection
440 status="running / not connected"
441 fi
442 if [ -z "${last_date}" ]
443 then
444 last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
445 fi
446
447 json_get_var faulty_list "faulty_stations"
448 if [ -n "${faulty_list}" ] && [ "${trm_listexpiry}" -gt 0 ]
449 then
450 d1="$(date -d "${last_date}" "+%s")"
451 d2="$(date "+%s")"
452 d3=$(((d2 - d1)/60))
453 if [ "${d3}" -ge "${trm_listexpiry}" ]
454 then
455 faulty_list=""
456 fi
457 fi
458
459 if [ -n "${faulty_station}" ]
460 then
461 if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
462 then
463 faulty_list="$(f_trim "${faulty_list} ${faulty_station}")"
464 last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
465 fi
466 fi
467
468 if [ "${trm_eapcheck}" -eq 0 ]
469 then
470 wpa_status="EAP"
471 else
472 wpa_status="-"
473 fi
474 if [ "${trm_saecheck}" -eq 0 ]
475 then
476 wpa_status="${wpa_status}/SAE"
477 else
478 wpa_status="${wpa_status}/-"
479 fi
480 if [ "${trm_owecheck}" -eq 0 ]
481 then
482 wpa_status="${wpa_status}/OWE"
483 else
484 wpa_status="${wpa_status}/-"
485 fi
486 json_add_string "travelmate_status" "${status}"
487 json_add_string "travelmate_version" "${trm_ver}"
488 json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
489 json_add_string "station_interface" "${sta_iface:-"-"}"
490 json_add_string "faulty_stations" "${faulty_list}"
491 json_add_string "wpa_capabilities" "${wpa_status:-"-"}"
492 json_add_string "last_rundate" "${last_date}"
493 json_add_string "system" "${trm_sysver}"
494 json_dump > "${trm_rtfile}"
495 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}"
496 }
497
498 # write to syslog
499 #
500 f_log()
501 {
502 local IFS class="${1}" log_msg="${2}"
503
504 if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${trm_debug}" -eq 1 ]; }
505 then
506 if [ -x "${trm_logger}" ]
507 then
508 "${trm_logger}" -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
509 else
510 printf "%s %s %s\\n" "${class}" "travelmate-${trm_ver}[${$}]" "${log_msg}"
511 fi
512 if [ "${class}" = "err" ]
513 then
514 trm_ifstatus="error"
515 f_jsnup
516 > "${trm_pidfile}"
517 exit 1
518 fi
519 fi
520 }
521
522 # main function for connection handling
523 #
524 f_main()
525 {
526 local IFS cnt dev config spec scan_list scan_essid scan_bssid scan_open scan_quality uci_essid cfg_essid faulty_list
527 local station_id sta sta_essid sta_bssid sta_radio sta_iface active_essid active_bssid active_radio
528
529 f_check "initial" "false" "true"
530 f_log "debug" "f_main ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
531 if [ "${trm_ifstatus}" != "true" ] || [ "${trm_proactive}" -eq 1 ]
532 then
533 config_load wireless
534 config_foreach f_prepif wifi-iface ${trm_proactive}
535 if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_active_sta}" ] && [ "${trm_proactive}" -eq 1 ]
536 then
537 json_get_var station_id "station_id"
538 active_radio="${station_id%%/*}"
539 active_essid="${station_id%/*}"
540 active_essid="${active_essid#*/}"
541 active_bssid="${station_id##*/}"
542 f_check "dev" "true"
543 f_log "debug" "f_main ::: active_radio: ${active_radio}, active_essid: \"${active_essid}\", active_bssid: ${active_bssid:-"-"}"
544 else
545 uci_commit "wireless"
546 f_check "dev"
547 fi
548 json_get_var faulty_list "faulty_stations"
549 f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo:-"-"}, dev_list: ${trm_devlist:-"-"}, sta_list: ${trm_stalist:0:${trm_scanbuffer}}, faulty_list: ${faulty_list:-"-"}"
550 # radio loop
551 #
552 for dev in ${trm_devlist}
553 do
554 if [ -z "$(printf "%s" "${trm_stalist}" | grep -o "\\-${dev}")" ]
555 then
556 f_log "debug" "f_main ::: no station on '${dev}' - continue"
557 continue
558 fi
559 # station loop
560 #
561 for sta in ${trm_stalist}
562 do
563 config="${sta%%-*}"
564 sta_radio="${sta##*-}"
565 sta_essid="$(uci_get "wireless" "${config}" "ssid")"
566 sta_bssid="$(uci_get "wireless" "${config}" "bssid")"
567 sta_iface="$(uci_get "wireless" "${config}" "network")"
568 json_get_var faulty_list "faulty_stations"
569 if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
570 then
571 f_log "debug" "f_main ::: faulty station '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' - continue"
572 continue
573 fi
574 if [ "${dev}" = "${active_radio}" ] && [ "${sta_essid}" = "${active_essid}" ] && [ "${sta_bssid:-"-"}" = "${active_bssid}" ]
575 then
576 f_log "debug" "f_main ::: active station prioritized '${active_radio}/${active_essid}/${active_bssid:-"-"}' - break"
577 break 2
578 fi
579 f_log "debug" "f_main ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
580 if [ -z "${scan_list}" ]
581 then
582 scan_list="$("${trm_iwinfo}" "${dev}" scan 2>/dev/null | \
583 awk 'BEGIN{FS="[[:space:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i};
584 gsub(/,/,".",var2)}/Quality:/{split($NF,var0,"/")}/Encryption:/{if($NF=="none"){var3="+"}else{var3="-"};printf "%i,%s,%s,%s\n",(var0[1]*100/var0[2]),var1,var2,var3}' | \
585 sort -rn | awk -v buf="${trm_scanbuffer}" 'BEGIN{ORS=","}{print substr($0,1,buf)}')"
586 f_log "debug" "f_main ::: scan_radio: ${dev}, scan_buffer: ${trm_scanbuffer}, scan_list: ${scan_list}"
587 if [ -z "${scan_list}" ]
588 then
589 f_log "debug" "f_main ::: no scan results on '${dev}' - continue"
590 continue 2
591 fi
592 fi
593 # scan loop
594 #
595 IFS=","
596 for spec in ${scan_list}
597 do
598 if [ -z "${scan_quality}" ]
599 then
600 scan_quality="${spec}"
601 elif [ -z "${scan_bssid}" ]
602 then
603 scan_bssid="${spec}"
604 elif [ -z "${scan_essid}" ]
605 then
606 scan_essid="${spec}"
607 elif [ -z "${scan_open}" ]
608 then
609 scan_open="${spec}"
610 fi
611 if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ] && [ -n "${scan_open}" ]
612 then
613 if [ "${scan_quality}" -ge "${trm_minquality}" ]
614 then
615 if { { [ "${scan_essid}" = "\"${sta_essid//,/.}\"" ] && { [ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ]; } } || \
616 { [ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ]; } } && [ "${dev}" = "${sta_radio}" ]
617 then
618 f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_essid: ${scan_essid}, scan_bssid: ${scan_bssid:-"-"}, scan_open: ${scan_open}"
619 if [ -n "${active_radio}" ]
620 then
621 uci_set "wireless" "${trm_active_sta}" "disabled" "1"
622 uci_commit "wireless"
623 f_log "debug" "f_main ::: active uplink connection '${active_radio}/${active_essid}/${active_bssid:-"-"}' terminated"
624 unset trm_connection active_radio active_essid active_bssid
625 fi
626 # retry loop
627 #
628 cnt=1
629 while [ "${cnt}" -le "${trm_maxretry}" ]
630 do
631 uci_set "wireless" "${config}" "disabled" "0"
632 trm_radio="${sta_radio}"
633 f_check "sta"
634 if [ "${trm_ifstatus}" = "true" ]
635 then
636 unset IFS scan_list
637 uci_commit "wireless"
638 f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
639 return 0
640 else
641 uci -q revert "wireless"
642 f_check "rev"
643 if [ "${cnt}" -eq "${trm_maxretry}" ]
644 then
645 faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
646 f_jsnup "${faulty_station}"
647 f_log "info" "uplink disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
648 break 2
649 else
650 f_jsnup
651 f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
652 fi
653 fi
654 cnt=$((cnt+1))
655 sleep $((trm_maxwait/6))
656 done
657 elif [ "${trm_autoadd}" -eq 1 ] && [ "${scan_open}" = "+" ] && [ "${scan_essid}" != "unknown" ]
658 then
659 cfg_essid="${scan_essid#*\"}"
660 cfg_essid="${cfg_essid%\"*}"
661 uci_essid="${cfg_essid//[^[:alnum:]_]/_}"
662 if [ -z "$(uci_get "wireless" "trm_${uci_essid}")" ]
663 then
664 uci_add "wireless" "wifi-iface" "trm_${uci_essid}"
665 uci_set "wireless" "trm_${uci_essid}" "mode" "sta"
666 uci_set "wireless" "trm_${uci_essid}" "network" "${trm_iface}"
667 uci_set "wireless" "trm_${uci_essid}" "device" "${sta_radio}"
668 uci_set "wireless" "trm_${uci_essid}" "ssid" "${cfg_essid}"
669 uci_set "wireless" "trm_${uci_essid}" "encryption" "none"
670 uci_set "wireless" "trm_${uci_essid}" "disabled" "1"
671 uci_commit "wireless"
672 f_log "info" "open uplink '${sta_radio}/${cfg_essid}' added to wireless config"
673 fi
674 fi
675 unset scan_quality scan_bssid scan_essid scan_open
676 continue
677 else
678 unset scan_quality scan_bssid scan_essid scan_open
679 continue
680 fi
681 fi
682 done
683 unset IFS scan_quality scan_bssid scan_essid scan_open
684 done
685 unset scan_list
686 done
687 fi
688 }
689
690 # source required system libraries
691 #
692 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
693 then
694 . "/lib/functions.sh"
695 . "/usr/share/libubox/jshn.sh"
696 else
697 f_log "err" "system libraries not found"
698 fi
699
700 # control travelmate actions
701 #
702 f_env
703 while true
704 do
705 if [ -z "${trm_action}" ]
706 then
707 rc=0
708 while true
709 do
710 if [ "${rc}" -eq 0 ]
711 then
712 f_check "initial"
713 fi
714 sleep ${trm_timeout} 0
715 rc=${?}
716 if [ "${rc}" -ne 0 ]
717 then
718 f_check "initial"
719 fi
720 if [ "${rc}" -eq 0 ] || { [ "${rc}" -ne 0 ] && [ "${trm_ifstatus}" = "false" ]; }
721 then
722 break
723 fi
724 done
725 elif [ "${trm_action}" = "stop" ]
726 then
727 f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
728 > "${trm_rtfile}"
729 > "${trm_pidfile}"
730 exit 0
731 else
732 f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
733 unset trm_action
734 fi
735 json_cleanup
736 f_env
737 f_main
738 done