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.

419 lines
13 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.4"
  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}" ] && [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo " ${radio}")" ]
  95. then
  96. trm_radiolist="${trm_radiolist} ${radio}"
  97. elif [ -n "${trm_radio}" ] && [ -z "${trm_radiolist}" ]
  98. then
  99. trm_radiolist="$(printf "%s" "${trm_radio}" | awk '{while(match(tolower($0),/radio[0-9]/)){ORS=" ";print substr(tolower($0),RSTART,RLENGTH);$0=substr($0,RSTART+RLENGTH)}}')"
  100. fi
  101. if [ "${mode}" = "sta" ] && [ "${network}" = "${trm_iface}" ]
  102. then
  103. if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
  104. then
  105. uci_set wireless "${config}" disabled 1
  106. fi
  107. if [ -n "${eaptype}" ]
  108. then
  109. eap_rc="$("${trm_wpa}" -veap >/dev/null 2>&1; printf "%u" ${?})"
  110. fi
  111. if [ -z "${eaptype}" ] || [ ${eap_rc} -eq 0 ]
  112. then
  113. trm_stalist="${trm_stalist} ${config}_${radio}"
  114. fi
  115. fi
  116. f_log "debug" "f_prep ::: config: ${config}, mode: ${mode}, network: ${network}, eap_rc: ${eap_rc}, radio: ${radio}, trm_radio: ${trm_radio:-"-"}, disabled: ${disabled}"
  117. }
  118. # check interface status
  119. #
  120. f_check()
  121. {
  122. local IFS ifname radio dev_status config sta_essid sta_bssid result wait=1 mode="${1}" status="${2:-"false"}"
  123. trm_ifquality=0
  124. trm_ifstatus="false"
  125. if [ "${mode}" != "initial" ]
  126. then
  127. ubus call network reload
  128. fi
  129. while [ ${wait} -le ${trm_maxwait} ]
  130. do
  131. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  132. if [ -n "${dev_status}" ]
  133. then
  134. if [ "${mode}" = "dev" ]
  135. then
  136. if [ "${trm_ifstatus}" != "${status}" ]
  137. then
  138. trm_ifstatus="${status}"
  139. f_jsnup
  140. fi
  141. for radio in ${trm_radiolist}
  142. do
  143. result="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e "@.${radio}.up")"
  144. if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo " ${radio}")" ]
  145. then
  146. trm_devlist="${trm_devlist} ${radio}"
  147. fi
  148. done
  149. if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ ${wait} -eq ${trm_maxwait} ]
  150. then
  151. ifname="${trm_devlist}"
  152. break
  153. else
  154. unset trm_devlist
  155. fi
  156. elif [ "${mode}" = "rev" ]
  157. then
  158. wait=$(( ${trm_maxwait} / 3 ))
  159. sleep ${wait}
  160. break
  161. else
  162. ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  163. if [ -n "${ifname}" ]
  164. then
  165. trm_ifquality="$(${trm_iwinfo} ${ifname} info 2>/dev/null | awk -F "[\/| ]" '/Link Quality:/{printf "%i\n", (100 / $NF * $(NF-1)) }')"
  166. if [ ${trm_ifquality} -ge ${trm_minquality} ]
  167. then
  168. trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
  169. elif [ "${mode}" = "initial" ] && [ ${trm_ifquality} -lt ${trm_minquality} ]
  170. then
  171. trm_ifstatus="${status}"
  172. sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
  173. sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
  174. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality}), uplink disconnected (${trm_sysver})"
  175. fi
  176. fi
  177. fi
  178. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  179. then
  180. if ([ "${trm_ifstatus}" != "true" ] && [ "${trm_ifstatus}" != "${status}" ]) || [ ${trm_ifquality} -lt ${trm_minquality} ]
  181. then
  182. f_jsnup
  183. fi
  184. if [ "${mode}" = "initial" ] && [ "${trm_captive}" -eq 1 ] && [ "${trm_ifstatus}" = "true" ]
  185. then
  186. 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}')"
  187. if [ -n "${result}" ] && ([ -z "${trm_connection}" ] || [ "${result}" != "${trm_connection%/*}" ])
  188. then
  189. trm_connection="${result}/${trm_ifquality}"
  190. f_jsnup
  191. fi
  192. fi
  193. break
  194. fi
  195. fi
  196. wait=$(( wait + 1 ))
  197. sleep 1
  198. done
  199. 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}"
  200. }
  201. # update runtime information
  202. #
  203. f_jsnup()
  204. {
  205. local config sta_iface sta_radio sta_essid sta_bssid dev_status status="${trm_ifstatus}" faulty_list faulty_station="${1}"
  206. if [ "${status}" = "true" ]
  207. then
  208. status="connected (${trm_connection:-"-"})"
  209. else
  210. unset trm_connection
  211. status="running / not connected"
  212. fi
  213. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  214. if [ -n "${dev_status}" ]
  215. then
  216. config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  217. if [ -n "${config}" ]
  218. then
  219. sta_iface="$(uci_get wireless "${config}" network)"
  220. sta_radio="$(uci_get wireless "${config}" device)"
  221. sta_essid="$(uci_get wireless "${config}" ssid)"
  222. sta_bssid="$(uci_get wireless "${config}" bssid)"
  223. fi
  224. fi
  225. json_get_var faulty_list "faulty_stations"
  226. if [ -n "${faulty_station}" ]
  227. then
  228. if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
  229. then
  230. faulty_list="${faulty_list} ${faulty_station}"
  231. fi
  232. fi
  233. json_add_string "travelmate_status" "${status}"
  234. json_add_string "travelmate_version" "${trm_ver}"
  235. json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
  236. json_add_string "station_interface" "${sta_iface:-"-"}"
  237. json_add_string "faulty_stations" "${faulty_list}"
  238. json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  239. json_add_string "system" "${trm_sysver}"
  240. json_dump > "${trm_rtfile}"
  241. 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:-"-"}"
  242. }
  243. # write to syslog
  244. #
  245. f_log()
  246. {
  247. local class="${1}"
  248. local log_msg="${2}"
  249. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  250. then
  251. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
  252. if [ "${class}" = "err" ]
  253. then
  254. trm_ifstatus="error"
  255. f_jsnup
  256. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  257. exit 1
  258. fi
  259. fi
  260. }
  261. # main function for connection handling
  262. #
  263. f_main()
  264. {
  265. 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
  266. f_check "initial"
  267. if [ "${trm_ifstatus}" != "true" ]
  268. then
  269. config_load wireless
  270. config_foreach f_prep wifi-iface
  271. uci_commit wireless
  272. f_check "dev" "running / not connected"
  273. json_get_var faulty_list "faulty_stations"
  274. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo}, dev_list: ${trm_devlist}, sta_list: ${trm_stalist:0:800}, faulty_list: ${faulty_list:-"-"}"
  275. for dev in ${trm_devlist}
  276. do
  277. if [ -z "$(printf "%s" "${trm_stalist}" | grep -Fo "_${dev}")" ]
  278. then
  279. continue
  280. fi
  281. cnt=1
  282. while [ ${cnt} -le ${trm_maxretry} ]
  283. do
  284. 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}')"
  285. f_log "debug" "f_main ::: dev: ${dev}, scan_list: ${scan_list:0:800}, cnt: ${cnt}, max_cnt: ${trm_maxretry}"
  286. if [ -n "${scan_list}" ]
  287. then
  288. for sta in ${trm_stalist}
  289. do
  290. config="${sta%%_*}"
  291. sta_radio="${sta##*_}"
  292. sta_essid="$(uci_get wireless "${config}" ssid)"
  293. sta_bssid="$(uci_get wireless "${config}" bssid)"
  294. sta_iface="$(uci_get wireless "${config}" network)"
  295. json_get_var faulty_list "faulty_stations"
  296. if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
  297. then
  298. continue
  299. fi
  300. IFS=","
  301. for scan in ${scan_list}
  302. do
  303. if [ -z "${scan_quality}" ]
  304. then
  305. scan_quality="${scan}"
  306. elif [ -z "${scan_bssid}" ]
  307. then
  308. scan_bssid="${scan}"
  309. elif [ -z "${scan_essid}" ]
  310. then
  311. scan_essid="${scan}"
  312. fi
  313. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
  314. then
  315. if [ ${scan_quality} -ge ${trm_minquality} ]
  316. then
  317. if (([ "${scan_essid}" = "\"${sta_essid}\"" ] && ([ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ])) || \
  318. ([ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ])) && [ "${dev}" = "${sta_radio}" ]
  319. then
  320. f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_bssid: ${scan_bssid}, scan_essid: ${scan_essid}"
  321. uci_set wireless "${config}" disabled 0
  322. f_check "sta"
  323. if [ "${trm_ifstatus}" = "true" ]
  324. then
  325. uci_commit wireless
  326. f_check "initial"
  327. f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  328. return 0
  329. elif [ ${cnt} -eq ${trm_maxretry} ]
  330. then
  331. uci_set wireless "${config}" disabled 1
  332. uci_commit wireless
  333. faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
  334. f_jsnup "${faulty_station}"
  335. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}', uplink disabled (${trm_sysver})"
  336. f_check "rev"
  337. break
  338. else
  339. uci -q revert wireless
  340. f_jsnup
  341. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  342. f_check "rev"
  343. break
  344. fi
  345. fi
  346. fi
  347. unset scan_quality scan_bssid scan_essid
  348. fi
  349. done
  350. unset IFS scan_quality scan_bssid scan_essid
  351. done
  352. fi
  353. cnt=$(( cnt + 1 ))
  354. sleep $(( ${trm_maxwait} / 6 ))
  355. done
  356. done
  357. fi
  358. }
  359. # source required system libraries
  360. #
  361. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  362. then
  363. . "/lib/functions.sh"
  364. . "/usr/share/libubox/jshn.sh"
  365. else
  366. f_log "err" "system libraries not found"
  367. fi
  368. # initialize json runtime file
  369. #
  370. json_load_file "${trm_rtfile}" >/dev/null 2>&1
  371. json_select data >/dev/null 2>&1
  372. if [ ${?} -ne 0 ]
  373. then
  374. > "${trm_rtfile}"
  375. json_init
  376. json_add_object "data"
  377. fi
  378. # control travelmate actions
  379. #
  380. while true
  381. do
  382. if [ -z "${trm_action}" ]
  383. then
  384. sleep ${trm_timeout}
  385. elif [ "${trm_action}" = "stop" ]
  386. then
  387. > "${trm_rtfile}"
  388. f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
  389. exit 0
  390. else
  391. f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
  392. unset trm_action
  393. fi
  394. f_envload
  395. f_main
  396. done