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.

402 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.2"
  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_connection=""
  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. elif [ "${status}" = "false" ]
  209. then
  210. trm_connection=""
  211. status="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_init
  226. json_add_object "data"
  227. json_add_string "travelmate_status" "${status}"
  228. json_add_string "travelmate_version" "${trm_ver}"
  229. json_add_string "station_id" "${sta_essid:-"-"}/${sta_bssid:-"-"}"
  230. json_add_string "station_interface" "${sta_iface:-"-"}"
  231. json_add_string "station_radio" "${sta_radio:-"-"}"
  232. json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  233. json_add_string "system" "${trm_sysver}"
  234. json_close_object
  235. json_dump > "${trm_rtfile}"
  236. 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:-"-"}"
  237. }
  238. # write to syslog
  239. #
  240. f_log()
  241. {
  242. local class="${1}"
  243. local log_msg="${2}"
  244. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  245. then
  246. logger -p "${class}" -t "travelmate-[${trm_ver}]" "${log_msg}"
  247. if [ "${class}" = "err" ]
  248. then
  249. trm_ifstatus="error"
  250. f_jsnup
  251. logger -p "${class}" -t "travelmate-[${trm_ver}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  252. exit 1
  253. fi
  254. fi
  255. }
  256. # main function for connection handling
  257. #
  258. f_main()
  259. {
  260. local cnt dev config scan scan_list scan_essid scan_bssid scan_quality sta sta_essid sta_bssid sta_radio sta_iface IFS=" "
  261. f_check "initial"
  262. if [ "${trm_ifstatus}" != "true" ]
  263. then
  264. config_load wireless
  265. config_foreach f_prep wifi-iface
  266. uci_commit wireless
  267. f_check "dev" "running"
  268. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo}, dev_list: ${trm_devlist}, sta_list: ${trm_stalist:0:800}"
  269. for dev in ${trm_devlist}
  270. do
  271. if [ -z "$(printf "%s" "${trm_stalist}" | grep -Fo "_${dev}")" ]
  272. then
  273. continue
  274. fi
  275. cnt=1
  276. while [ ${trm_maxretry} -eq 0 ] || [ ${cnt} -le ${trm_maxretry} ]
  277. do
  278. 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}')"
  279. f_log "debug" "f_main ::: dev: ${dev}, scan_list: ${scan_list:0:800}, cnt: ${cnt}, max_cnt: ${trm_maxretry}"
  280. if [ -n "${scan_list}" ]
  281. then
  282. for sta in ${trm_stalist}
  283. do
  284. config="${sta%%_*}"
  285. sta_radio="${sta##*_}"
  286. sta_essid="$(uci_get wireless "${config}" ssid)"
  287. sta_bssid="$(uci_get wireless "${config}" bssid)"
  288. sta_iface="$(uci_get wireless "${config}" network)"
  289. IFS=","
  290. for scan in ${scan_list}
  291. do
  292. if [ -z "${scan_quality}" ]
  293. then
  294. scan_quality="${scan}"
  295. elif [ -z "${scan_bssid}" ]
  296. then
  297. scan_bssid="${scan}"
  298. elif [ -z "${scan_essid}" ]
  299. then
  300. scan_essid="${scan}"
  301. fi
  302. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
  303. then
  304. if [ ${scan_quality} -ge ${trm_minquality} ]
  305. then
  306. if (([ "${scan_essid}" = "\"${sta_essid}\"" ] && ([ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ])) || \
  307. ([ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ])) && [ "${dev}" = "${sta_radio}" ]
  308. then
  309. f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_bssid: ${scan_bssid}, scan_essid: ${scan_essid}"
  310. uci_set wireless "${config}" disabled 0
  311. f_check "sta"
  312. if [ "${trm_ifstatus}" = "true" ]
  313. then
  314. uci_commit wireless
  315. f_log "info" "interface '${sta_iface}' on '${sta_radio}' connected to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' (${trm_sysver})"
  316. f_check "initial"
  317. return 0
  318. elif [ ${cnt} -eq ${trm_maxretry} ]
  319. then
  320. uci_set wireless "${config}" disabled 1
  321. if [ -n "${sta_essid}" ]
  322. then
  323. uci_set wireless "${config}" ssid "${sta_essid}_err"
  324. fi
  325. if [ -n "${sta_bssid}" ]
  326. then
  327. uci_set wireless "${config}" bssid "${sta_bssid}_err"
  328. fi
  329. uci_commit wireless
  330. f_log "info" "can't connect to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}', uplink disabled (${trm_sysver})"
  331. f_check "rev"
  332. else
  333. uci -q revert wireless
  334. f_log "info" "can't connect to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' (${trm_sysver})"
  335. f_check "rev"
  336. fi
  337. fi
  338. fi
  339. scan_quality=""
  340. scan_bssid=""
  341. scan_essid=""
  342. fi
  343. done
  344. IFS=" "
  345. done
  346. fi
  347. cnt=$((cnt+1))
  348. sleep $((${trm_maxwait}/6))
  349. done
  350. done
  351. if [ ! -s "${trm_rtfile}" ]
  352. then
  353. trm_ifstatus="false"
  354. f_jsnup
  355. fi
  356. elif [ ! -s "${trm_rtfile}" ]
  357. then
  358. f_jsnup
  359. fi
  360. }
  361. # source required system libraries
  362. #
  363. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  364. then
  365. . "/lib/functions.sh"
  366. . "/usr/share/libubox/jshn.sh"
  367. else
  368. f_log "err" "system libraries not found"
  369. fi
  370. # control travelmate actions
  371. #
  372. f_envload
  373. f_main
  374. while [ ${trm_automatic} -eq 1 ]
  375. do
  376. sleep ${trm_timeout}
  377. f_envload
  378. f_main
  379. done
  380. exit 0