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.

428 lines
16 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. # set initial defaults
  8. #
  9. LC_ALL=C
  10. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  11. trm_ver="1.2.2"
  12. trm_sysver="unknown"
  13. trm_enabled=0
  14. trm_debug=0
  15. trm_captive=1
  16. trm_captiveurl="http://captive.apple.com"
  17. trm_minquality=35
  18. trm_maxretry=3
  19. trm_maxwait=30
  20. trm_timeout=60
  21. trm_radio=""
  22. trm_connection=""
  23. trm_rtfile="/tmp/trm_runtime.json"
  24. trm_fetch="$(command -v uclient-fetch)"
  25. trm_iwinfo="$(command -v iwinfo)"
  26. trm_wpa="$(command -v wpa_supplicant)"
  27. trm_action="${1:-"start"}"
  28. trm_pidfile="/var/run/travelmate.pid"
  29. # load travelmate environment
  30. #
  31. f_envload()
  32. {
  33. local sys_call sys_desc sys_model sys_ver
  34. # get system information
  35. #
  36. sys_call="$(ubus -S call system board 2>/dev/null)"
  37. if [ -n "${sys_call}" ]
  38. then
  39. sys_desc="$(printf '%s' "${sys_call}" | jsonfilter -e '@.release.description')"
  40. sys_model="$(printf '%s' "${sys_call}" | jsonfilter -e '@.model')"
  41. sys_ver="$(cat /etc/turris-version 2>/dev/null)"
  42. if [ -n "${sys_ver}" ]
  43. then
  44. sys_desc="${sys_desc}/${sys_ver}"
  45. fi
  46. trm_sysver="${sys_model}, ${sys_desc}"
  47. fi
  48. # (re-)initialize global list variables
  49. #
  50. unset trm_devlist trm_stalist trm_radiolist
  51. # load config and check 'enabled' option
  52. #
  53. option_cb()
  54. {
  55. local option="${1}"
  56. local value="${2}"
  57. eval "${option}=\"${value}\""
  58. }
  59. config_load travelmate
  60. if [ ${trm_enabled} -ne 1 ]
  61. then
  62. f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
  63. exit 0
  64. fi
  65. # validate input ranges
  66. #
  67. if [ ${trm_minquality} -lt 20 ] || [ ${trm_minquality} -gt 80 ]
  68. then
  69. trm_minquality=35
  70. fi
  71. if [ ${trm_maxretry} -lt 1 ] || [ ${trm_maxretry} -gt 10 ]
  72. then
  73. trm_maxretry=3
  74. fi
  75. if [ ${trm_maxwait} -lt 20 ] || [ ${trm_maxwait} -gt 40 ] || [ ${trm_maxwait} -ge ${trm_timeout} ]
  76. then
  77. trm_maxwait=30
  78. fi
  79. if [ ${trm_timeout} -lt 30 ] || [ ${trm_timeout} -gt 300 ] || [ ${trm_timeout} -le ${trm_maxwait} ]
  80. then
  81. trm_timeout=60
  82. fi
  83. }
  84. # gather radio information & bring down all STA interfaces
  85. #
  86. f_prep()
  87. {
  88. local eap_rc=0 config="${1}"
  89. local mode="$(uci_get wireless "${config}" mode)"
  90. local network="$(uci_get wireless "${config}" network)"
  91. local radio="$(uci_get wireless "${config}" device)"
  92. local disabled="$(uci_get wireless "${config}" disabled)"
  93. local eaptype="$(uci_get wireless "${config}" eap_type)"
  94. if ([ -z "${trm_radio}" ] || [ "${trm_radio}" = "${radio}" ]) && \
  95. [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo " ${radio}")" ]
  96. then
  97. trm_radiolist="${trm_radiolist} ${radio}"
  98. fi
  99. if [ "${mode}" = "sta" ] && [ "${network}" = "${trm_iface}" ]
  100. then
  101. if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
  102. then
  103. uci_set wireless "${config}" disabled 1
  104. fi
  105. if [ -n "${eaptype}" ]
  106. then
  107. eap_rc="$("${trm_wpa}" -veap >/dev/null 2>&1; printf "%u" ${?})"
  108. fi
  109. if [ -z "${eaptype}" ] || [ ${eap_rc} -eq 0 ]
  110. then
  111. trm_stalist="${trm_stalist} ${config}_${radio}"
  112. fi
  113. fi
  114. f_log "debug" "f_prep ::: config: ${config}, mode: ${mode}, network: ${network}, eap_rc: ${eap_rc}, radio: ${radio}, trm_radio: ${trm_radio:-"-"}, disabled: ${disabled}"
  115. }
  116. # check interface status
  117. #
  118. f_check()
  119. {
  120. local IFS ifname radio dev_status config sta_essid sta_bssid result wait=1 mode="${1}" status="${2:-"false"}"
  121. trm_ifquality=0
  122. trm_ifstatus="false"
  123. if [ "${mode}" != "initial" ]
  124. then
  125. ubus call network reload
  126. fi
  127. while [ ${wait} -le ${trm_maxwait} ]
  128. do
  129. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  130. if [ -n "${dev_status}" ]
  131. then
  132. if [ "${mode}" = "dev" ]
  133. then
  134. if [ "${trm_ifstatus}" != "${status}" ]
  135. then
  136. trm_ifstatus="${status}"
  137. f_jsnup
  138. fi
  139. for radio in ${trm_radiolist}
  140. do
  141. result="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e "@.${radio}.up")"
  142. if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo " ${radio}")" ]
  143. then
  144. trm_devlist="${trm_devlist} ${radio}"
  145. fi
  146. done
  147. if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ ${wait} -eq ${trm_maxwait} ]
  148. then
  149. ifname="${trm_devlist}"
  150. break
  151. else
  152. unset trm_devlist
  153. fi
  154. elif [ "${mode}" = "rev" ]
  155. then
  156. wait=$(( ${trm_maxwait} / 3 ))
  157. sleep ${wait}
  158. break
  159. else
  160. ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  161. if [ -n "${ifname}" ]
  162. then
  163. trm_ifquality="$(${trm_iwinfo} ${ifname} info 2>/dev/null | awk -F "[\/| ]" '/Link Quality:/{printf "%i\n", (100 / $NF * $(NF-1)) }')"
  164. if [ ${trm_ifquality} -ge ${trm_minquality} ]
  165. then
  166. trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
  167. elif [ "${mode}" = "initial" ] && [ ${trm_ifquality} -lt ${trm_minquality} ]
  168. then
  169. trm_ifstatus="${status}"
  170. sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
  171. sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
  172. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality}), uplink disconnected (${trm_sysver})"
  173. fi
  174. fi
  175. fi
  176. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  177. then
  178. if ([ "${trm_ifstatus}" != "true" ] && [ "${trm_ifstatus}" != "${status}" ]) || [ ${trm_ifquality} -lt ${trm_minquality} ]
  179. then
  180. f_jsnup
  181. fi
  182. if [ "${mode}" = "initial" ] && [ "${trm_captive}" -eq 1 ] && [ "${trm_ifstatus}" = "true" ]
  183. then
  184. result="$(${trm_fetch} --timeout=$(( ${trm_maxwait} / 3 )) --spider "${trm_captiveurl}" 2>&1 | awk '/^Redirected/{printf "%s" "net cp \047"$NF"\047";exit}/^Download completed/{printf "%s" "net ok";exit}/^Failed|^Connection error/{printf "%s" "net nok";exit}')"
  185. if [ -n "${result}" ] && ([ -z "${trm_connection}" ] || [ "${result}" != "${trm_connection%/*}" ])
  186. then
  187. trm_connection="${result}/${trm_ifquality}"
  188. f_jsnup
  189. fi
  190. fi
  191. break
  192. fi
  193. fi
  194. wait=$(( wait + 1 ))
  195. sleep 1
  196. done
  197. f_log "debug" "f_check::: mode: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, quality: ${trm_ifquality}, connection: ${trm_connection:-"-"}, wait: ${wait}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}"
  198. }
  199. # update runtime information
  200. #
  201. f_jsnup()
  202. {
  203. local config sta_iface sta_radio sta_essid sta_bssid dev_status status="${trm_ifstatus}" faulty_list faulty_station="${1}"
  204. if [ "${status}" = "true" ]
  205. then
  206. status="connected (${trm_connection:-"-"})"
  207. else
  208. unset trm_connection
  209. if [ "${status}" = "false" ]
  210. then
  211. status="not connected"
  212. fi
  213. fi
  214. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  215. if [ -n "${dev_status}" ]
  216. then
  217. config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  218. if [ -n "${config}" ]
  219. then
  220. sta_iface="$(uci_get wireless "${config}" network)"
  221. sta_radio="$(uci_get wireless "${config}" device)"
  222. sta_essid="$(uci_get wireless "${config}" ssid)"
  223. sta_bssid="$(uci_get wireless "${config}" bssid)"
  224. fi
  225. fi
  226. json_get_var faulty_list "faulty_stations"
  227. if [ -n "${faulty_station}" ]
  228. then
  229. if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
  230. then
  231. faulty_list="${faulty_list} ${faulty_station}"
  232. fi
  233. fi
  234. json_add_string "travelmate_status" "${status}"
  235. json_add_string "travelmate_version" "${trm_ver}"
  236. json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
  237. json_add_string "station_interface" "${sta_iface:-"-"}"
  238. json_add_string "faulty_stations" "${faulty_list}"
  239. json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  240. json_add_string "system" "${trm_sysver}"
  241. json_dump > "${trm_rtfile}"
  242. 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:-"-"}"
  243. }
  244. # write to syslog
  245. #
  246. f_log()
  247. {
  248. local class="${1}"
  249. local log_msg="${2}"
  250. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  251. then
  252. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
  253. if [ "${class}" = "err" ]
  254. then
  255. trm_ifstatus="error"
  256. f_jsnup
  257. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  258. exit 1
  259. fi
  260. fi
  261. }
  262. # main function for connection handling
  263. #
  264. f_main()
  265. {
  266. local IFS cnt dev config scan scan_list scan_essid scan_bssid scan_quality sta sta_essid sta_bssid sta_radio sta_iface faulty_list
  267. f_check "initial"
  268. if [ "${trm_ifstatus}" != "true" ]
  269. then
  270. config_load wireless
  271. config_foreach f_prep wifi-iface
  272. uci_commit wireless
  273. f_check "dev" "running"
  274. json_get_var faulty_list "faulty_stations"
  275. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo}, dev_list: ${trm_devlist}, sta_list: ${trm_stalist:0:800}, faulty_list: ${faulty_list:-"-"}"
  276. for dev in ${trm_devlist}
  277. do
  278. if [ -z "$(printf "%s" "${trm_stalist}" | grep -Fo "_${dev}")" ]
  279. then
  280. continue
  281. fi
  282. cnt=1
  283. while [ ${cnt} -le ${trm_maxretry} ]
  284. do
  285. scan_list="$(${trm_iwinfo} "${dev}" scan 2>/dev/null | awk 'BEGIN{FS="[/ ]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i}}/Quality:/{printf "%i,%s,%s\n",(100/$NF*$(NF-1)),var1,var2}' | sort -rn | awk '{ORS=",";print $0}')"
  286. f_log "debug" "f_main ::: dev: ${dev}, scan_list: ${scan_list:0:800}, cnt: ${cnt}, max_cnt: ${trm_maxretry}"
  287. if [ -n "${scan_list}" ]
  288. then
  289. for sta in ${trm_stalist}
  290. do
  291. config="${sta%%_*}"
  292. sta_radio="${sta##*_}"
  293. sta_essid="$(uci_get wireless "${config}" ssid)"
  294. sta_bssid="$(uci_get wireless "${config}" bssid)"
  295. sta_iface="$(uci_get wireless "${config}" network)"
  296. json_get_var faulty_list "faulty_stations"
  297. if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
  298. then
  299. continue
  300. fi
  301. IFS=","
  302. for scan in ${scan_list}
  303. do
  304. if [ -z "${scan_quality}" ]
  305. then
  306. scan_quality="${scan}"
  307. elif [ -z "${scan_bssid}" ]
  308. then
  309. scan_bssid="${scan}"
  310. elif [ -z "${scan_essid}" ]
  311. then
  312. scan_essid="${scan}"
  313. fi
  314. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
  315. then
  316. if [ ${scan_quality} -ge ${trm_minquality} ]
  317. then
  318. if (([ "${scan_essid}" = "\"${sta_essid}\"" ] && ([ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ])) || \
  319. ([ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ])) && [ "${dev}" = "${sta_radio}" ]
  320. then
  321. f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_bssid: ${scan_bssid}, scan_essid: ${scan_essid}"
  322. uci_set wireless "${config}" disabled 0
  323. f_check "sta"
  324. if [ "${trm_ifstatus}" = "true" ]
  325. then
  326. uci_commit wireless
  327. f_check "initial"
  328. f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  329. return 0
  330. elif [ ${cnt} -eq ${trm_maxretry} ]
  331. then
  332. uci_set wireless "${config}" disabled 1
  333. uci_commit wireless
  334. faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
  335. f_jsnup "${faulty_station}"
  336. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}', uplink disabled (${trm_sysver})"
  337. f_check "rev"
  338. break
  339. else
  340. uci -q revert wireless
  341. f_jsnup
  342. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  343. f_check "rev"
  344. break
  345. fi
  346. fi
  347. fi
  348. unset scan_quality scan_bssid scan_essid
  349. fi
  350. done
  351. unset IFS scan_quality scan_bssid scan_essid
  352. done
  353. fi
  354. cnt=$(( cnt + 1 ))
  355. sleep $(( ${trm_maxwait} / 6 ))
  356. done
  357. done
  358. if [ ! -s "${trm_rtfile}" ]
  359. then
  360. trm_ifstatus="false"
  361. f_jsnup
  362. fi
  363. elif [ ! -s "${trm_rtfile}" ]
  364. then
  365. f_jsnup
  366. fi
  367. }
  368. # source required system libraries
  369. #
  370. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  371. then
  372. . "/lib/functions.sh"
  373. . "/usr/share/libubox/jshn.sh"
  374. else
  375. f_log "err" "system libraries not found"
  376. fi
  377. # initialize json runtime file
  378. #
  379. if [ ! -s "${trm_rtfile}" ]
  380. then
  381. json_init
  382. json_add_object "data"
  383. else
  384. json_load_file "${trm_rtfile}"
  385. json_select data
  386. fi
  387. # control travelmate actions
  388. #
  389. while true
  390. do
  391. if [ -z "${trm_action}" ]
  392. then
  393. sleep ${trm_timeout}
  394. elif [ "${trm_action}" = "stop" ]
  395. then
  396. > "${trm_rtfile}"
  397. f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
  398. exit 0
  399. else
  400. f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
  401. unset trm_action
  402. fi
  403. f_envload
  404. f_main
  405. done