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.

413 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.0"
  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. trm_devlist=""
  51. trm_stalist=""
  52. trm_radiolist=""
  53. # load config and check 'enabled' option
  54. #
  55. option_cb()
  56. {
  57. local option="${1}"
  58. local value="${2}"
  59. eval "${option}=\"${value}\""
  60. }
  61. config_load travelmate
  62. if [ ${trm_enabled} -ne 1 ]
  63. then
  64. f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
  65. config_load wireless
  66. config_foreach f_prep wifi-iface
  67. uci_commit wireless
  68. ubus call network reload
  69. exit 0
  70. fi
  71. # validate input ranges
  72. #
  73. if [ ${trm_minquality} -lt 20 ] || [ ${trm_minquality} -gt 80 ]
  74. then
  75. trm_minquality=35
  76. fi
  77. if [ ${trm_maxretry} -lt 1 ] || [ ${trm_maxretry} -gt 10 ]
  78. then
  79. trm_maxretry=3
  80. fi
  81. if [ ${trm_maxwait} -lt 20 ] || [ ${trm_maxwait} -gt 40 ] || [ ${trm_maxwait} -ge ${trm_timeout} ]
  82. then
  83. trm_maxwait=30
  84. fi
  85. if [ ${trm_timeout} -lt 30 ] || [ ${trm_timeout} -gt 300 ] || [ ${trm_timeout} -le ${trm_maxwait} ]
  86. then
  87. trm_timeout=60
  88. fi
  89. }
  90. # gather radio information & bring down all STA interfaces
  91. #
  92. f_prep()
  93. {
  94. local eap_rc=0 config="${1}"
  95. local mode="$(uci_get wireless "${config}" mode)"
  96. local network="$(uci_get wireless "${config}" network)"
  97. local radio="$(uci_get wireless "${config}" device)"
  98. local disabled="$(uci_get wireless "${config}" disabled)"
  99. local eaptype="$(uci_get wireless "${config}" eap_type)"
  100. if ([ -z "${trm_radio}" ] || [ "${trm_radio}" = "${radio}" ]) && \
  101. [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo " ${radio}")" ]
  102. then
  103. trm_radiolist="${trm_radiolist} ${radio}"
  104. fi
  105. if [ "${mode}" = "sta" ] && [ "${network}" = "${trm_iface}" ]
  106. then
  107. if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
  108. then
  109. uci_set wireless "${config}" disabled 1
  110. fi
  111. if [ -n "${eaptype}" ]
  112. then
  113. eap_rc="$("${trm_wpa}" -veap >/dev/null 2>&1; printf "%u" ${?})"
  114. fi
  115. if [ -z "${eaptype}" ] || [ ${eap_rc} -eq 0 ]
  116. then
  117. trm_stalist="${trm_stalist} ${config}_${radio}"
  118. fi
  119. fi
  120. f_log "debug" "f_prep ::: config: ${config}, mode: ${mode}, network: ${network}, eap_rc: ${eap_rc}, radio: ${radio}, trm_radio: ${trm_radio:-"-"}, disabled: ${disabled}"
  121. }
  122. # check interface status
  123. #
  124. f_check()
  125. {
  126. local ifname radio dev_status config sta_essid sta_bssid result wait=1 mode="${1}" status="${2:-"false"}" IFS=" "
  127. trm_ifquality=0
  128. trm_ifstatus="false"
  129. if [ "${mode}" != "initial" ]
  130. then
  131. ubus call network reload
  132. fi
  133. while [ ${wait} -le ${trm_maxwait} ]
  134. do
  135. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  136. if [ -n "${dev_status}" ]
  137. then
  138. if [ "${mode}" = "dev" ]
  139. then
  140. if [ "${trm_ifstatus}" != "${status}" ]
  141. then
  142. trm_ifstatus="${status}"
  143. f_jsnup
  144. fi
  145. for radio in ${trm_radiolist}
  146. do
  147. result="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e "@.${radio}.up")"
  148. if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo " ${radio}")" ]
  149. then
  150. trm_devlist="${trm_devlist} ${radio}"
  151. fi
  152. done
  153. if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ ${wait} -eq ${trm_maxwait} ]
  154. then
  155. ifname="${trm_devlist}"
  156. break
  157. else
  158. trm_devlist=""
  159. fi
  160. elif [ "${mode}" = "rev" ]
  161. then
  162. wait=$(( ${trm_maxwait} / 3 ))
  163. sleep ${wait}
  164. break
  165. else
  166. ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  167. if [ -n "${ifname}" ]
  168. then
  169. trm_ifquality="$(${trm_iwinfo} ${ifname} info 2>/dev/null | awk -F "[\/| ]" '/Link Quality:/{printf "%i\n", (100 / $NF * $(NF-1)) }')"
  170. if [ ${trm_ifquality} -ge ${trm_minquality} ]
  171. then
  172. trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
  173. elif [ "${mode}" = "initial" ] && [ ${trm_ifquality} -lt ${trm_minquality} ]
  174. then
  175. trm_ifstatus="${status}"
  176. sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
  177. sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
  178. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality}), uplink disconnected (${trm_sysver})"
  179. fi
  180. fi
  181. fi
  182. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  183. then
  184. if ([ "${trm_ifstatus}" != "true" ] && [ "${trm_ifstatus}" != "${status}" ]) || [ ${trm_ifquality} -lt ${trm_minquality} ]
  185. then
  186. f_jsnup
  187. fi
  188. if [ "${mode}" = "initial" ] && [ "${trm_captive}" -eq 1 ] && [ "${trm_ifstatus}" = "true" ]
  189. then
  190. 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}')"
  191. if [ -n "${result}" ] && ([ -z "${trm_connection}" ] || [ "${result}" != "${trm_connection%/*}" ])
  192. then
  193. trm_connection="${result}/${trm_ifquality}"
  194. f_jsnup
  195. fi
  196. fi
  197. break
  198. fi
  199. fi
  200. wait=$(( wait + 1 ))
  201. sleep 1
  202. done
  203. 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}"
  204. }
  205. # update runtime information
  206. #
  207. f_jsnup()
  208. {
  209. local config sta_iface sta_radio sta_essid sta_bssid dev_status status="${trm_ifstatus}"
  210. if [ "${status}" = "true" ]
  211. then
  212. status="connected (${trm_connection:-"-"})"
  213. else
  214. trm_connection=""
  215. if [ "${status}" = "false" ]
  216. then
  217. status="not connected"
  218. fi
  219. fi
  220. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  221. if [ -n "${dev_status}" ]
  222. then
  223. config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  224. if [ -n "${config}" ]
  225. then
  226. sta_iface="$(uci_get wireless "${config}" network)"
  227. sta_radio="$(uci_get wireless "${config}" device)"
  228. sta_essid="$(uci_get wireless "${config}" ssid)"
  229. sta_bssid="$(uci_get wireless "${config}" bssid)"
  230. fi
  231. fi
  232. json_init
  233. json_add_object "data"
  234. json_add_string "travelmate_status" "${status}"
  235. json_add_string "travelmate_version" "${trm_ver}"
  236. json_add_string "station_id" "${sta_essid:-"-"}/${sta_bssid:-"-"}"
  237. json_add_string "station_interface" "${sta_iface:-"-"}"
  238. json_add_string "station_radio" "${sta_radio:-"-"}"
  239. json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  240. json_add_string "system" "${trm_sysver}"
  241. json_close_object
  242. json_dump > "${trm_rtfile}"
  243. 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:-"-"}"
  244. }
  245. # write to syslog
  246. #
  247. f_log()
  248. {
  249. local class="${1}"
  250. local log_msg="${2}"
  251. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  252. then
  253. logger -p "${class}" -t "travelmate-[${trm_ver}]" "${log_msg}"
  254. if [ "${class}" = "err" ]
  255. then
  256. trm_ifstatus="error"
  257. f_jsnup
  258. logger -p "${class}" -t "travelmate-[${trm_ver}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  259. exit 1
  260. fi
  261. fi
  262. }
  263. # main function for connection handling
  264. #
  265. f_main()
  266. {
  267. local cnt dev config scan scan_list scan_essid scan_bssid scan_quality sta sta_essid sta_bssid sta_radio sta_iface IFS=" "
  268. f_check "initial"
  269. if [ "${trm_ifstatus}" != "true" ]
  270. then
  271. config_load wireless
  272. config_foreach f_prep wifi-iface
  273. uci_commit wireless
  274. f_check "dev" "running"
  275. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo}, dev_list: ${trm_devlist}, sta_list: ${trm_stalist:0:800}"
  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 [ ${trm_maxretry} -eq 0 ] || [ ${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. IFS=","
  297. for scan in ${scan_list}
  298. do
  299. if [ -z "${scan_quality}" ]
  300. then
  301. scan_quality="${scan}"
  302. elif [ -z "${scan_bssid}" ]
  303. then
  304. scan_bssid="${scan}"
  305. elif [ -z "${scan_essid}" ]
  306. then
  307. scan_essid="${scan}"
  308. fi
  309. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
  310. then
  311. if [ ${scan_quality} -ge ${trm_minquality} ]
  312. then
  313. if (([ "${scan_essid}" = "\"${sta_essid}\"" ] && ([ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ])) || \
  314. ([ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ])) && [ "${dev}" = "${sta_radio}" ]
  315. then
  316. f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_bssid: ${scan_bssid}, scan_essid: ${scan_essid}"
  317. uci_set wireless "${config}" disabled 0
  318. f_check "sta"
  319. if [ "${trm_ifstatus}" = "true" ]
  320. then
  321. uci_commit wireless
  322. f_log "info" "interface '${sta_iface}' on '${sta_radio}' connected to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' (${trm_sysver})"
  323. return 0
  324. elif [ ${cnt} -eq ${trm_maxretry} ]
  325. then
  326. uci_set wireless "${config}" disabled 1
  327. if [ -n "${sta_essid}" ]
  328. then
  329. uci_set wireless "${config}" ssid "${sta_essid}_err"
  330. fi
  331. if [ -n "${sta_bssid}" ]
  332. then
  333. uci_set wireless "${config}" bssid "${sta_bssid}_err"
  334. fi
  335. uci_commit wireless
  336. f_log "info" "can't connect to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}', uplink disabled (${trm_sysver})"
  337. f_check "rev"
  338. else
  339. uci -q revert wireless
  340. f_log "info" "can't connect to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' (${trm_sysver})"
  341. f_check "rev"
  342. fi
  343. fi
  344. fi
  345. scan_quality=""
  346. scan_bssid=""
  347. scan_essid=""
  348. fi
  349. done
  350. IFS=" "
  351. done
  352. fi
  353. cnt=$(( cnt + 1 ))
  354. sleep $(( ${trm_maxwait} / 6 ))
  355. done
  356. done
  357. if [ ! -s "${trm_rtfile}" ]
  358. then
  359. trm_ifstatus="false"
  360. f_jsnup
  361. fi
  362. elif [ ! -s "${trm_rtfile}" ]
  363. then
  364. f_jsnup
  365. fi
  366. }
  367. # source required system libraries
  368. #
  369. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  370. then
  371. . "/lib/functions.sh"
  372. . "/usr/share/libubox/jshn.sh"
  373. else
  374. f_log "err" "system libraries not found"
  375. fi
  376. # control travelmate actions
  377. #
  378. while true
  379. do
  380. if [ -z "${trm_action}" ]
  381. then
  382. > "${trm_pidfile}"
  383. sleep ${trm_timeout}
  384. else
  385. printf '%s' "${$}" > "${trm_pidfile}"
  386. trm_action=""
  387. fi
  388. f_envload
  389. f_main
  390. done