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.

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