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.

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