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.

325 lines
11 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="0.9.6"
  12. trm_sysver="unknown"
  13. trm_enabled=0
  14. trm_debug=0
  15. trm_automatic=1
  16. trm_maxretry=3
  17. trm_maxwait=30
  18. trm_timeout=60
  19. trm_iwinfo="$(command -v iwinfo)"
  20. trm_radio=""
  21. trm_rtfile="/tmp/trm_runtime.json"
  22. # f_envload: load travelmate environment
  23. #
  24. f_envload()
  25. {
  26. local sys_call sys_desc sys_model sys_ver
  27. # get system information
  28. #
  29. sys_call="$(ubus -S call system board 2>/dev/null)"
  30. if [ -n "${sys_call}" ]
  31. then
  32. sys_desc="$(printf '%s' "${sys_call}" | jsonfilter -e '@.release.description')"
  33. sys_model="$(printf '%s' "${sys_call}" | jsonfilter -e '@.model')"
  34. sys_ver="$(cat /etc/turris-version 2>/dev/null)"
  35. if [ -n "${sys_ver}" ]
  36. then
  37. sys_desc="${sys_desc}/${sys_ver}"
  38. fi
  39. trm_sysver="${sys_model}, ${sys_desc}"
  40. fi
  41. # initialize lists
  42. #
  43. trm_devlist=""
  44. trm_stalist=""
  45. trm_radiolist=""
  46. # load uci config and check 'enabled' option
  47. #
  48. option_cb()
  49. {
  50. local option="${1}"
  51. local value="${2}"
  52. eval "${option}=\"${value}\""
  53. }
  54. config_load travelmate
  55. if [ ${trm_enabled} -ne 1 ]
  56. then
  57. f_log "info " "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
  58. exit 0
  59. fi
  60. }
  61. # f_prepare: gather radio information & bring down all STA interfaces
  62. #
  63. f_prepare()
  64. {
  65. local config="${1}"
  66. local mode="$(uci -q get wireless."${config}".mode)"
  67. local radio="$(uci -q get wireless."${config}".device)"
  68. local disabled="$(uci -q get wireless."${config}".disabled)"
  69. if ([ -z "${trm_radio}" ] || [ "${trm_radio}" = "${radio}" ]) && \
  70. [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo " ${radio}")" ]
  71. then
  72. trm_radiolist="${trm_radiolist} ${radio}"
  73. fi
  74. if [ "${mode}" = "sta" ]
  75. then
  76. trm_stalist="${trm_stalist} ${config}_${radio}"
  77. if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
  78. then
  79. uci -q set wireless."${config}".disabled=1
  80. fi
  81. fi
  82. f_log "debug" "prepare: ${mode}, radio: ${radio}, config: ${config}, disabled: ${disabled}"
  83. }
  84. # f_check: check interface status
  85. #
  86. f_check()
  87. {
  88. local ifname radio status cnt=1 mode="${1}"
  89. trm_ifstatus="false"
  90. ubus call network reload
  91. while [ ${cnt} -le ${trm_maxwait} ]
  92. do
  93. status="$(ubus -S call network.wireless status 2>/dev/null)"
  94. if [ -n "${status}" ]
  95. then
  96. if [ "${mode}" = "dev" ]
  97. then
  98. for radio in ${trm_radiolist}
  99. do
  100. trm_ifstatus="$(printf "%s" "${status}" | jsonfilter -l1 -e "@.${radio}.up")"
  101. if [ "${trm_ifstatus}" = "true" ]
  102. then
  103. trm_devlist="${trm_devlist} ${radio}"
  104. else
  105. trm_devlist=""
  106. break
  107. fi
  108. done
  109. ifname="${trm_devlist}"
  110. else
  111. ifname="$(printf "%s" "${status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  112. if [ -n "${ifname}" ]
  113. then
  114. trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
  115. fi
  116. fi
  117. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  118. then
  119. break
  120. fi
  121. fi
  122. cnt=$((cnt+1))
  123. sleep 1
  124. done
  125. f_log "debug" "check: ${mode}, name: ${ifname}, status: ${trm_ifstatus}, count: ${cnt}, max-wait: ${trm_maxwait}, automatic: ${trm_automatic}"
  126. }
  127. # f_jsnupdate: update runtime information
  128. #
  129. f_jsnupdate()
  130. {
  131. local iface="${1}" radio="${2}" essid="${3:-"-"}" bssid="${4:-"-"}"
  132. json_init
  133. json_add_object "data"
  134. json_add_string "travelmate_version" "${trm_ver}"
  135. json_add_string "station_connection" "${trm_ifstatus}"
  136. json_add_string "station_id" "${essid}/${bssid}"
  137. json_add_string "station_interface" "${iface}"
  138. json_add_string "station_radio" "${radio}"
  139. json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  140. json_add_string "system" "${trm_sysver}"
  141. json_close_object
  142. json_dump > "${trm_rtfile}"
  143. }
  144. # f_status: output runtime information
  145. #
  146. f_status()
  147. {
  148. local key keylist value
  149. if [ -s "${trm_rtfile}" ]
  150. then
  151. printf "%s\n" "::: travelmate runtime information"
  152. json_load "$(cat "${trm_rtfile}" 2>/dev/null)"
  153. json_select data
  154. json_get_keys keylist
  155. for key in ${keylist}
  156. do
  157. json_get_var value "${key}"
  158. printf " %-18s : %s\n" "${key}" "${value}"
  159. done
  160. fi
  161. }
  162. # f_log: write to syslog, exit on error
  163. #
  164. f_log()
  165. {
  166. local class="${1}"
  167. local log_msg="${2}"
  168. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  169. then
  170. logger -t "travelmate-[${trm_ver}] ${class}" "${log_msg}"
  171. if [ "${class}" = "error" ]
  172. then
  173. logger -t "travelmate-[${trm_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  174. exit 255
  175. fi
  176. fi
  177. }
  178. # f_main: main function for connection handling
  179. #
  180. f_main()
  181. {
  182. local dev config raw_scan essid_list bssid_list sta_essid sta_bssid sta_radio sta_iface cnt=1
  183. f_check "initial"
  184. if [ "${trm_ifstatus}" != "true" ]
  185. then
  186. > "${trm_rtfile}"
  187. config_load wireless
  188. config_foreach f_prepare wifi-iface
  189. if [ -n "$(uci -q changes wireless)" ]
  190. then
  191. uci -q commit wireless
  192. fi
  193. f_check "dev"
  194. f_log "debug" "main: ${trm_devlist}, sta-list: ${trm_stalist}"
  195. for dev in ${trm_devlist}
  196. do
  197. cnt=1
  198. if [ -z "$(printf "%s" "${trm_stalist}" | grep -Fo "_${dev}")" ]
  199. then
  200. continue
  201. fi
  202. while [ ${trm_maxretry} -eq 0 ] || [ ${cnt} -le ${trm_maxretry} ]
  203. do
  204. raw_scan="$(${trm_iwinfo} "${dev}" scan)"
  205. essid_list="$(printf "%s" "${raw_scan}" | awk '/ESSID: "/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
  206. bssid_list="$(printf "%s" "${raw_scan}" | awk '/Address: /{ORS=" ";if (!seen[$5]++) print $5}')"
  207. f_log "debug" "main: ${trm_iwinfo}, dev: ${dev}, essid-list: ${essid_list}, bssid-list: ${bssid_list}"
  208. if [ -n "${essid_list}" ] || [ -n "${bssid_list}" ]
  209. then
  210. for sta in ${trm_stalist}
  211. do
  212. config="${sta%%_*}"
  213. sta_radio="${sta##*_}"
  214. sta_essid="$(uci -q get wireless."${config}".ssid)"
  215. sta_bssid="$(uci -q get wireless."${config}".bssid)"
  216. sta_iface="$(uci -q get wireless."${config}".network)"
  217. if (([ -n "$(printf "%s" "${essid_list}" | grep -Fo "\"${sta_essid}\"")" ] && [ -z "${sta_bssid}" ]) || \
  218. ([ -n "$(printf "%s" "${bssid_list}" | grep -Fo "${sta_bssid}")" ] && [ -z "$(printf "%s" "${essid_list}" | grep -Fo "\"${sta_essid}\"")" ]) || \
  219. ([ -n "$(printf "%s" "${essid_list}" | grep -Fo "\"${sta_essid}\"")" ] && [ -n "$(printf "%s" "${bssid_list}" | grep -Fo "${sta_bssid}")" ])) && \
  220. [ "${dev}" = "${sta_radio}" ]
  221. then
  222. uci -q set wireless."${config}".disabled=0
  223. f_check "sta"
  224. if [ "${trm_ifstatus}" = "true" ]
  225. then
  226. uci -q commit wireless
  227. f_log "info " "interface '${sta_iface}' on '${sta_radio}' connected to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' (${trm_sysver})"
  228. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
  229. return 0
  230. elif [ ${trm_maxretry} -ne 0 ] && [ ${cnt} -eq ${trm_maxretry} ]
  231. then
  232. uci -q set wireless."${config}".disabled=1
  233. if [ -n "${sta_essid}" ]
  234. then
  235. uci -q set wireless."${config}".ssid="${sta_essid}_err"
  236. fi
  237. if [ -n "${sta_bssid}" ]
  238. then
  239. uci -q set wireless."${config}".bssid="${sta_bssid}_err"
  240. fi
  241. uci -q commit wireless
  242. f_check "dev"
  243. f_log "info " "can't connect to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}), uplink disabled (${trm_sysver})"
  244. else
  245. if [ ${trm_maxretry} -eq 0 ]
  246. then
  247. cnt=0
  248. fi
  249. uci -q revert wireless
  250. f_check "dev"
  251. f_log "info " "can't connect to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}) (${trm_sysver})"
  252. fi
  253. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
  254. fi
  255. done
  256. fi
  257. cnt=$((cnt+1))
  258. sleep 5
  259. done
  260. done
  261. if [ ! -s "${trm_rtfile}" ]
  262. then
  263. trm_ifstatus="false"
  264. f_jsnupdate "n/a" "n/a"
  265. fi
  266. else
  267. if [ ! -s "${trm_rtfile}" ]
  268. then
  269. config="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  270. sta_radio="$(uci -q get wireless."${config}".device)"
  271. sta_essid="$(uci -q get wireless."${config}".ssid)"
  272. sta_bssid="$(uci -q get wireless."${config}".bssid)"
  273. sta_iface="$(uci -q get wireless."${config}".network)"
  274. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
  275. fi
  276. fi
  277. }
  278. # source required system libraries
  279. #
  280. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  281. then
  282. . "/lib/functions.sh"
  283. . "/usr/share/libubox/jshn.sh"
  284. else
  285. f_log "error" "system libraries not found"
  286. fi
  287. # handle different travelmate actions
  288. #
  289. f_envload
  290. case "${1}" in
  291. status)
  292. f_status
  293. ;;
  294. *)
  295. f_main
  296. while [ ${trm_automatic} -eq 1 ]
  297. do
  298. sleep ${trm_timeout}
  299. f_envload
  300. f_main
  301. done
  302. ;;
  303. esac
  304. exit 0