You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

734 lines
24 KiB

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