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.

692 lines
23 KiB

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