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.

729 lines
23 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 result
  211. result="$(${trm_fetch} --timeout=$((trm_maxwait/6)) "${trm_captiveurl}" -O /dev/null 2>&1 | tail -n 1)"
  212. result="$(printf "%s" "${result//[\?\$\%\&\+\|\'\"\:]*/}" | 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}')"
  213. printf "%s" "${result}"
  214. f_log "debug" "f_net ::: fetch: ${trm_fetch}, timeout: $((trm_maxwait/6)), url: ${trm_captiveurl}, result: ${result}"
  215. }
  216. # check interface status
  217. #
  218. f_check()
  219. {
  220. 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"}"
  221. if [ "${mode}" != "initial" ] && [ "${mode}" != "dev" ] && [ "${status}" = "false" ]
  222. then
  223. "${trm_wifi}" "${trm_wificmd}"
  224. sleep $((trm_maxwait/6))
  225. fi
  226. while [ "${wait_time}" -le "${trm_maxwait}" ]
  227. do
  228. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  229. if [ -n "${dev_status}" ]
  230. then
  231. if [ "${mode}" = "dev" ]
  232. then
  233. if [ "${trm_ifstatus}" != "${status}" ]
  234. then
  235. trm_ifstatus="${status}"
  236. f_jsnup
  237. fi
  238. for radio in ${trm_radiolist}
  239. do
  240. result="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e "@.${radio}.up")"
  241. if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo "${radio}")" ]
  242. then
  243. trm_devlist="$(f_trim "${trm_devlist} ${radio}")"
  244. fi
  245. done
  246. if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ "${wait_time}" -eq "${trm_maxwait}" ]
  247. then
  248. ifname="${trm_devlist}"
  249. break
  250. else
  251. unset trm_devlist
  252. fi
  253. elif [ "${mode}" = "rev" ]
  254. then
  255. break
  256. else
  257. ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  258. if [ -n "${ifname}" ]
  259. then
  260. trm_ifquality="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk -F "[ ]" '/Link Quality:/{split($NF,var0,"/");printf "%i\n",(var0[1]*100/var0[2])}')"
  261. if [ "${mode}" = "initial" ] && [ "${trm_captive}" -eq 1 ]
  262. then
  263. result="$(f_net)"
  264. if [ "${cp_domain}" = "true" ]
  265. then
  266. cp_domain="$(printf "%s" "${result}" | awk -F "[\\'| ]" '/^net cp/{printf "%s" $4}')"
  267. uci_section="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  268. fi
  269. fi
  270. if [ "${trm_ifquality}" -ge "${trm_minquality}" ] && [ "${result}" != "net nok" ]
  271. then
  272. trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
  273. if [ "${trm_ifstatus}" = "true" ]
  274. then
  275. if [ "${mode}" = "sta" ] && [ "${trm_captive}" -eq 1 ]
  276. then
  277. while true
  278. do
  279. result="$(f_net)"
  280. cp_domain="$(printf "%s" "${result}" | awk -F "[\\'| ]" '/^net cp/{printf "%s" $4}')"
  281. uci_section="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  282. if [ "${trm_netcheck}" -eq 1 ] && [ "${result}" = "net nok" ]
  283. then
  284. trm_ifstatus="${status}"
  285. f_jsnup
  286. break 2
  287. fi
  288. if [ -z "${cp_domain}" ] || [ -n "$(uci_get "dhcp" "@dnsmasq[0]" "rebind_domain" | grep -Fo "${cp_domain}")" ]
  289. then
  290. break
  291. fi
  292. uci -q add_list dhcp.@dnsmasq[0].rebind_domain="${cp_domain}"
  293. f_log "info" "captive portal domain '${cp_domain}' added to to dhcp rebind whitelist"
  294. if [ -z "$(uci_get "travelmate" "${uci_section}")" ]
  295. then
  296. uci_add travelmate "login" "${uci_section}"
  297. uci_set travelmate "${uci_section}" "command" "none"
  298. f_log "info" "captive portal login section '${uci_section}' added to travelmate config section"
  299. fi
  300. done
  301. if [ -n "$(uci -q changes "dhcp")" ]
  302. then
  303. uci_commit "dhcp"
  304. /etc/init.d/dnsmasq reload
  305. fi
  306. if [ -n "$(uci -q changes "travelmate")" ]
  307. then
  308. uci_commit "travelmate"
  309. fi
  310. fi
  311. if [ -n "${cp_domain}" ] && [ "${cp_domain}" != "false" ] && [ -n "${uci_section}" ] && [ "${trm_captive}" -eq 1 ]
  312. then
  313. trm_connection="${result:-"-"}/${trm_ifquality}"
  314. f_jsnup
  315. login_command="$(uci_get "travelmate" "${uci_section}" "command")"
  316. if [ -x "${login_command}" ]
  317. then
  318. login_command_args="$(uci_get "travelmate" "${uci_section}" "command_args")"
  319. "${login_command}" ${login_command_args} >/dev/null 2>&1
  320. rc=${?}
  321. f_log "info" "captive portal login '${login_command:0:40} ${login_command_args:0:20}' for '${cp_domain}' has been executed with rc '${rc}'"
  322. if [ "${rc}" -eq 0 ]
  323. then
  324. result="$(f_net)"
  325. fi
  326. fi
  327. fi
  328. trm_connection="${result:-"-"}/${trm_ifquality}"
  329. f_jsnup
  330. break
  331. fi
  332. elif [ -n "${trm_connection}" ]
  333. then
  334. uci_section="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  335. if [ "${trm_ifquality}" -lt "${trm_minquality}" ]
  336. then
  337. unset trm_connection
  338. trm_ifstatus="${status}"
  339. f_log "info" "uplink '${uci_section}' is out of range (${trm_ifquality}/${trm_minquality})"
  340. elif [ "${trm_netcheck}" -eq 1 ] && [ "${result}" = "net nok" ]
  341. then
  342. unset trm_connection
  343. trm_ifstatus="${status}"
  344. f_log "info" "uplink '${uci_section}' has no internet (${result})"
  345. fi
  346. f_jsnup
  347. break
  348. elif [ "${mode}" = "initial" ]
  349. then
  350. f_jsnup
  351. break
  352. fi
  353. elif [ -n "${trm_connection}" ]
  354. then
  355. unset trm_connection
  356. trm_ifstatus="${status}"
  357. f_jsnup
  358. break
  359. elif [ "${mode}" = "initial" ]
  360. then
  361. f_jsnup
  362. break
  363. fi
  364. fi
  365. fi
  366. wait_time=$((wait_time+1))
  367. sleep 1
  368. done
  369. 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}"
  370. }
  371. # update runtime information
  372. #
  373. f_jsnup()
  374. {
  375. 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}"
  376. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  377. if [ -n "${dev_status}" ]
  378. then
  379. uci_section="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  380. if [ -n "${uci_section}" ]
  381. then
  382. sta_iface="$(uci_get "wireless" "${uci_section}" "network")"
  383. sta_radio="$(uci_get "wireless" "${uci_section}" "device")"
  384. sta_essid="$(uci_get "wireless" "${uci_section}" "ssid")"
  385. sta_bssid="$(uci_get "wireless" "${uci_section}" "bssid")"
  386. fi
  387. fi
  388. json_get_var last_date "last_rundate"
  389. json_get_var last_station "station_id"
  390. if [ "${status}" = "true" ]
  391. then
  392. status="connected (${trm_connection:-"-"})"
  393. json_get_var last_status "travelmate_status"
  394. if [ "${last_status}" = "running / not connected" ] || [ "${last_station}" != "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" ]
  395. then
  396. last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
  397. fi
  398. elif [ "${status}" = "error" ]
  399. then
  400. unset trm_connection
  401. status="program error"
  402. else
  403. unset trm_connection
  404. status="running / not connected"
  405. fi
  406. if [ -z "${last_date}" ]
  407. then
  408. last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
  409. fi
  410. json_get_var faulty_list "faulty_stations"
  411. if [ -n "${faulty_list}" ] && [ "${trm_listexpiry}" -gt 0 ]
  412. then
  413. d1="$(date -d "${last_date}" "+%s")"
  414. d2="$(date "+%s")"
  415. d3=$(((d2 - d1)/60))
  416. if [ "${d3}" -ge "${trm_listexpiry}" ]
  417. then
  418. faulty_list=""
  419. fi
  420. fi
  421. if [ -n "${faulty_station}" ]
  422. then
  423. if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
  424. then
  425. faulty_list="$(f_trim "${faulty_list} ${faulty_station}")"
  426. last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
  427. fi
  428. fi
  429. if [ "${trm_eapcheck}" -eq 0 ]
  430. then
  431. wpa_status="EAP"
  432. else
  433. wpa_status="-"
  434. fi
  435. if [ "${trm_saecheck}" -eq 0 ]
  436. then
  437. wpa_status="${wpa_status}/SAE"
  438. else
  439. wpa_status="${wpa_status}/-"
  440. fi
  441. if [ "${trm_owecheck}" -eq 0 ]
  442. then
  443. wpa_status="${wpa_status}/OWE"
  444. else
  445. wpa_status="${wpa_status}/-"
  446. fi
  447. json_add_string "travelmate_status" "${status}"
  448. json_add_string "travelmate_version" "${trm_ver}"
  449. json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
  450. json_add_string "station_interface" "${sta_iface:-"-"}"
  451. json_add_string "faulty_stations" "${faulty_list}"
  452. json_add_string "wpa_capabilities" "${wpa_status:-"-"}"
  453. json_add_string "last_rundate" "${last_date}"
  454. json_add_string "system" "${trm_sysver}"
  455. json_dump > "${trm_rtfile}"
  456. 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}"
  457. }
  458. # write to syslog
  459. #
  460. f_log()
  461. {
  462. local IFS class="${1}" log_msg="${2}"
  463. if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${trm_debug}" -eq 1 ]; }
  464. then
  465. if [ -x "${trm_logger}" ]
  466. then
  467. "${trm_logger}" -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
  468. else
  469. printf "%s %s %s\\n" "${class}" "travelmate-${trm_ver}[${$}]" "${log_msg}"
  470. fi
  471. if [ "${class}" = "err" ]
  472. then
  473. trm_ifstatus="error"
  474. f_jsnup
  475. > "${trm_pidfile}"
  476. exit 1
  477. fi
  478. fi
  479. }
  480. # main function for connection handling
  481. #
  482. f_main()
  483. {
  484. local IFS cnt dev config spec scan_dev scan_list scan_essid scan_bssid scan_open scan_quality uci_essid cfg_essid faulty_list
  485. local station_id sta sta_essid sta_bssid sta_radio sta_iface active_essid active_bssid active_radio
  486. f_check "initial" "false" "true"
  487. f_log "debug" "f_main ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
  488. if [ "${trm_ifstatus}" != "true" ] || [ "${trm_proactive}" -eq 1 ]
  489. then
  490. config_load wireless
  491. config_foreach f_prepif wifi-iface ${trm_proactive}
  492. if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_active_sta}" ] && [ "${trm_proactive}" -eq 1 ]
  493. then
  494. json_get_var station_id "station_id"
  495. active_radio="${station_id%%/*}"
  496. active_essid="${station_id%/*}"
  497. active_essid="${active_essid#*/}"
  498. active_bssid="${station_id##*/}"
  499. f_check "dev" "true"
  500. f_log "debug" "f_main ::: active_radio: ${active_radio}, active_essid: \"${active_essid}\", active_bssid: ${active_bssid:-"-"}"
  501. else
  502. uci_commit "wireless"
  503. f_check "dev"
  504. fi
  505. json_get_var faulty_list "faulty_stations"
  506. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo:-"-"}, dev_list: ${trm_devlist:-"-"}, sta_list: ${trm_stalist:0:${trm_scanbuffer}}, faulty_list: ${faulty_list:-"-"}"
  507. # radio loop
  508. #
  509. for dev in ${trm_devlist}
  510. do
  511. if [ -z "$(printf "%s" "${trm_stalist}" | grep -o "\\-${dev}")" ]
  512. then
  513. f_log "debug" "f_main ::: no station on '${dev}' - continue"
  514. continue
  515. fi
  516. # station loop
  517. #
  518. for sta in ${trm_stalist}
  519. do
  520. config="${sta%%-*}"
  521. sta_radio="${sta##*-}"
  522. sta_essid="$(uci_get "wireless" "${config}" "ssid")"
  523. sta_bssid="$(uci_get "wireless" "${config}" "bssid")"
  524. sta_iface="$(uci_get "wireless" "${config}" "network")"
  525. json_get_var faulty_list "faulty_stations"
  526. if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
  527. then
  528. f_log "debug" "f_main ::: faulty station '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' - continue"
  529. continue
  530. fi
  531. if [ "${dev}" = "${active_radio}" ] && [ "${sta_essid}" = "${active_essid}" ] && [ "${sta_bssid:-"-"}" = "${active_bssid}" ]
  532. then
  533. f_log "debug" "f_main ::: active station prioritized '${active_radio}/${active_essid}/${active_bssid:-"-"}' - break"
  534. break 2
  535. fi
  536. f_log "debug" "f_main ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
  537. if [ -z "${scan_list}" ]
  538. then
  539. scan_dev="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -l1 -e "@.${dev}.interfaces[@.config.mode=\"sta\"].ifname")"
  540. scan_list="$("${trm_iwinfo}" "${scan_dev:-${dev}}" scan 2>/dev/null | \
  541. awk 'BEGIN{FS="[[:space:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i};
  542. 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}' | \
  543. sort -rn | awk -v buf="${trm_scanbuffer}" 'BEGIN{ORS=","}{print substr($0,1,buf)}')"
  544. f_log "debug" "f_main ::: scan_radio: ${dev}, scan_device: ${scan_dev:-"-"}, scan_buffer: ${trm_scanbuffer}, scan_list: ${scan_list:-"-"}"
  545. if [ -z "${scan_list}" ]
  546. then
  547. f_log "debug" "f_main ::: no scan results on '${dev}/${scan_dev:-"-"}' - continue"
  548. continue 2
  549. fi
  550. fi
  551. # scan loop
  552. #
  553. IFS=","
  554. for spec in ${scan_list}
  555. do
  556. if [ -z "${scan_quality}" ]
  557. then
  558. scan_quality="${spec}"
  559. elif [ -z "${scan_bssid}" ]
  560. then
  561. scan_bssid="${spec}"
  562. elif [ -z "${scan_essid}" ]
  563. then
  564. scan_essid="${spec}"
  565. elif [ -z "${scan_open}" ]
  566. then
  567. scan_open="${spec}"
  568. fi
  569. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ] && [ -n "${scan_open}" ]
  570. then
  571. if [ "${scan_quality}" -ge "${trm_minquality}" ]
  572. then
  573. if { { [ "${scan_essid}" = "\"${sta_essid//,/.}\"" ] && { [ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ]; } } || \
  574. { [ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ]; } } && [ "${dev}" = "${sta_radio}" ]
  575. then
  576. f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_essid: ${scan_essid}, scan_bssid: ${scan_bssid:-"-"}, scan_open: ${scan_open}"
  577. if [ -n "${active_radio}" ]
  578. then
  579. uci_set "wireless" "${trm_active_sta}" "disabled" "1"
  580. uci_commit "wireless"
  581. f_log "debug" "f_main ::: active uplink connection '${active_radio}/${active_essid}/${active_bssid:-"-"}' terminated"
  582. unset trm_connection active_radio active_essid active_bssid
  583. fi
  584. # retry loop
  585. #
  586. cnt=1
  587. while [ "${cnt}" -le "${trm_maxretry}" ]
  588. do
  589. uci_set "wireless" "${config}" "disabled" "0"
  590. trm_radio="${sta_radio}"
  591. f_check "sta"
  592. if [ "${trm_ifstatus}" = "true" ]
  593. then
  594. unset IFS scan_list
  595. uci_commit "wireless"
  596. f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  597. return 0
  598. else
  599. uci -q revert "wireless"
  600. f_check "rev"
  601. if [ "${cnt}" -eq "${trm_maxretry}" ]
  602. then
  603. faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
  604. f_jsnup "${faulty_station}"
  605. f_log "info" "uplink disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  606. break 2
  607. else
  608. f_jsnup
  609. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  610. fi
  611. fi
  612. cnt=$((cnt+1))
  613. sleep $((trm_maxwait/6))
  614. done
  615. elif [ "${trm_autoadd}" -eq 1 ] && [ "${scan_open}" = "+" ] && [ "${scan_essid}" != "unknown" ]
  616. then
  617. cfg_essid="${scan_essid#*\"}"
  618. cfg_essid="${cfg_essid%\"*}"
  619. uci_essid="${cfg_essid//[^[:alnum:]_]/_}"
  620. if [ -z "$(uci_get "wireless" "trm_${uci_essid}")" ]
  621. then
  622. uci_add "wireless" "wifi-iface" "trm_${uci_essid}"
  623. uci_set "wireless" "trm_${uci_essid}" "mode" "sta"
  624. uci_set "wireless" "trm_${uci_essid}" "network" "${trm_iface}"
  625. uci_set "wireless" "trm_${uci_essid}" "device" "${sta_radio}"
  626. uci_set "wireless" "trm_${uci_essid}" "ssid" "${cfg_essid}"
  627. uci_set "wireless" "trm_${uci_essid}" "encryption" "none"
  628. uci_set "wireless" "trm_${uci_essid}" "disabled" "1"
  629. uci_commit "wireless"
  630. f_log "info" "open uplink '${sta_radio}/${cfg_essid}' added to wireless config"
  631. fi
  632. fi
  633. unset scan_quality scan_bssid scan_essid scan_open
  634. continue
  635. else
  636. unset scan_quality scan_bssid scan_essid scan_open
  637. continue
  638. fi
  639. fi
  640. done
  641. unset IFS scan_quality scan_bssid scan_essid scan_open
  642. done
  643. unset scan_list
  644. done
  645. fi
  646. }
  647. # source required system libraries
  648. #
  649. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  650. then
  651. . "/lib/functions.sh"
  652. . "/usr/share/libubox/jshn.sh"
  653. else
  654. f_log "err" "system libraries not found"
  655. fi
  656. # control travelmate actions
  657. #
  658. f_env
  659. while true
  660. do
  661. if [ -z "${trm_action}" ]
  662. then
  663. rc=0
  664. while true
  665. do
  666. if [ "${rc}" -eq 0 ]
  667. then
  668. f_check "initial"
  669. fi
  670. sleep ${trm_timeout} 0
  671. rc=${?}
  672. if [ "${rc}" -ne 0 ]
  673. then
  674. f_check "initial"
  675. fi
  676. if [ "${rc}" -eq 0 ] || { [ "${rc}" -ne 0 ] && [ "${trm_ifstatus}" = "false" ]; }
  677. then
  678. break
  679. fi
  680. done
  681. elif [ "${trm_action}" = "stop" ]
  682. then
  683. f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
  684. > "${trm_rtfile}"
  685. > "${trm_pidfile}"
  686. exit 0
  687. else
  688. f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
  689. unset trm_action
  690. fi
  691. json_cleanup
  692. f_env
  693. f_main
  694. done