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.

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