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.

291 lines
9.0 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.7.4"
  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_iw="$(command -v iw)"
  20. trm_radio=""
  21. trm_rtfile="/tmp/trm_runtime.json"
  22. # source required system libraries
  23. #
  24. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  25. then
  26. . "/lib/functions.sh"
  27. . "/usr/share/libubox/jshn.sh"
  28. else
  29. f_log "error" "system libraries not found"
  30. fi
  31. # f_envload: load travelmate environment
  32. #
  33. f_envload()
  34. {
  35. # initialize lists
  36. #
  37. trm_aplist=""
  38. trm_stalist=""
  39. trm_radiolist=""
  40. # load uci config and check 'enabled' option
  41. #
  42. option_cb()
  43. {
  44. local option="${1}"
  45. local value="${2}"
  46. eval "${option}=\"${value}\""
  47. }
  48. config_load travelmate
  49. if [ ${trm_enabled} -ne 1 ]
  50. then
  51. f_log "info " "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
  52. exit 0
  53. fi
  54. # check for wireless tool
  55. #
  56. if [ -z "${trm_iw}" ]
  57. then
  58. f_log "error" "no wireless tool found, please install package 'iw'"
  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 [ "${mode}" = "ap" ] && ([ -z "${disabled}" ] || [ "${disabled}" = "0" ]) && \
  70. ([ -z "${trm_radio}" ] || [ "${trm_radio}" = "${radio}" ])
  71. then
  72. trm_radiolist="${trm_radiolist} ${radio}"
  73. elif [ "${mode}" = "sta" ]
  74. then
  75. trm_stalist="${trm_stalist} ${config}_${radio}"
  76. if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
  77. then
  78. uci -q set wireless."${config}".disabled=1
  79. fi
  80. fi
  81. f_log "debug" "mode: ${mode}, radio: ${radio}, config: ${config}, disabled: ${disabled}"
  82. }
  83. # f_check: check interface status
  84. #
  85. f_check()
  86. {
  87. local ifname radio status cnt=1 mode="${1}"
  88. ubus call network reload
  89. trm_ifstatus="false"
  90. while [ ${cnt} -le ${trm_maxwait} ]
  91. do
  92. status="$(ubus -S call network.wireless status 2>/dev/null)"
  93. if [ -n "${status}" ]
  94. then
  95. if [ "${mode}" = "ap" ]
  96. then
  97. for radio in ${trm_radiolist}
  98. do
  99. trm_ifstatus="$(printf "%s" "${status}" | jsonfilter -e "@.${radio}.up")"
  100. if [ "${trm_ifstatus}" = "true" ]
  101. then
  102. trm_aplist="${trm_aplist} $(printf "%s" "${status}" | jsonfilter -e "@.${radio}.interfaces[@.config.mode=\"ap\"].ifname")_${radio}"
  103. ifname="${trm_aplist}"
  104. else
  105. trm_aplist=""
  106. trm_ifstatus="false"
  107. break
  108. fi
  109. done
  110. else
  111. ifname="$(printf "%s" "${status}" | jsonfilter -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 -e "@.interface[@.device=\"${ifname}\"].up")"
  115. fi
  116. fi
  117. fi
  118. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  119. then
  120. break
  121. fi
  122. cnt=$((cnt+1))
  123. sleep 1
  124. done
  125. f_log "debug" "mode: ${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}" ssid="${3}"
  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_ssid" "${ssid}"
  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 config ssid_list ap ap_radio sta_ssid 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 "ap"
  194. f_log "debug" "ap-list: ${trm_aplist}, sta-list: ${trm_stalist}"
  195. for ap in ${trm_aplist}
  196. do
  197. cnt=1
  198. ap_radio="${ap##*_}"
  199. ap="${ap%%_*}"
  200. if [ -z "$(printf "%s" "${trm_stalist}" | grep -Fo "_${ap_radio}")" ]
  201. then
  202. continue
  203. fi
  204. while [ ${cnt} -le ${trm_maxretry} ]
  205. do
  206. ssid_list="$(${trm_iw} dev "${ap}" scan 2>/dev/null | \
  207. awk '/SSID: /{if(!seen[$0]++){printf "\"";for(i=2; i<=NF; i++)if(i==2)printf $i;else printf " "$i;printf "\" "}}')"
  208. f_log "debug" "iw: ${trm_iw}, ap: ${ap}, ssids: ${ssid_list}"
  209. if [ -n "${ssid_list}" ]
  210. then
  211. for sta in ${trm_stalist}
  212. do
  213. config="${sta%%_*}"
  214. sta_radio="${sta##*_}"
  215. sta_ssid="$(uci -q get wireless."${config}".ssid)"
  216. sta_iface="$(uci -q get wireless."${config}".network)"
  217. if [ -n "$(printf "%s" "${ssid_list}" | grep -Fo "\"${sta_ssid}\"")" ] && [ "${ap_radio}" = "${sta_radio}" ]
  218. then
  219. uci -q set wireless."${config}".disabled=0
  220. f_check "sta"
  221. if [ "${trm_ifstatus}" = "true" ]
  222. then
  223. uci -q commit wireless
  224. f_log "info " "interface '${sta_iface}' on '${sta_radio}' connected to uplink '${sta_ssid}' (${trm_sysver})"
  225. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
  226. return 0
  227. else
  228. uci -q revert wireless
  229. f_log "info " "interface '${sta_iface}' on '${sta_radio}' can't connect to uplink '${sta_ssid}' (${trm_sysver})"
  230. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
  231. fi
  232. fi
  233. done
  234. fi
  235. cnt=$((cnt+1))
  236. sleep 5
  237. done
  238. done
  239. if [ ! -s "${trm_rtfile}" ]
  240. then
  241. f_jsnupdate "n/a" "n/a" "n/a"
  242. fi
  243. else
  244. if [ ! -s "${trm_rtfile}" ]
  245. then
  246. config="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  247. sta_radio="$(uci -q get wireless."${config}".device)"
  248. sta_ssid="$(uci -q get wireless."${config}".ssid)"
  249. sta_iface="$(uci -q get wireless."${config}".network)"
  250. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
  251. fi
  252. fi
  253. }
  254. # handle different travelmate actions
  255. #
  256. f_envload
  257. case "${1}" in
  258. status)
  259. f_status
  260. ;;
  261. *)
  262. f_main
  263. while [ ${trm_automatic} -eq 1 ]
  264. do
  265. sleep ${trm_timeout}
  266. f_envload
  267. f_main
  268. done
  269. ;;
  270. esac
  271. exit 0