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.

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