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.

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