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.

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