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.

266 lines
8.6 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.6.0"
  12. trm_sysver="$(ubus -S call system board | jsonfilter -e '@.release.description')"
  13. trm_enabled=0
  14. trm_debug=0
  15. trm_automatic=0
  16. trm_maxwait=30
  17. trm_maxretry=3
  18. trm_timeout=60
  19. trm_iw=1
  20. # source required system library
  21. #
  22. if [ -r "/lib/functions.sh" ]
  23. then
  24. . "/lib/functions.sh"
  25. else
  26. f_log "error" "required system library not found"
  27. fi
  28. # f_envload: load travelmate environment
  29. #
  30. f_envload()
  31. {
  32. # initialize lists
  33. #
  34. trm_aplist=""
  35. trm_stalist=""
  36. trm_radiolist=""
  37. # load uci config and check 'enabled' option
  38. #
  39. option_cb()
  40. {
  41. local option="${1}"
  42. local value="${2}"
  43. eval "${option}=\"${value}\""
  44. }
  45. config_load travelmate
  46. if [ ${trm_enabled} -ne 1 ]
  47. then
  48. f_log "info " "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
  49. exit 0
  50. fi
  51. # check for preferred wireless tool
  52. #
  53. if [ ${trm_iw} -eq 1 ]
  54. then
  55. trm_scanner="$(which iw)"
  56. else
  57. trm_scanner="$(which iwinfo)"
  58. fi
  59. if [ -z "${trm_scanner}" ]
  60. then
  61. f_log "error" "no wireless tool for wlan scanning found, please install 'iw' or 'iwinfo'"
  62. fi
  63. }
  64. # f_prepare: gather radio information & bring down all STA interfaces
  65. #
  66. f_prepare()
  67. {
  68. local config="${1}"
  69. local mode="$(uci -q get wireless."${config}".mode)"
  70. local radio="$(uci -q get wireless."${config}".device)"
  71. local disabled="$(uci -q get wireless."${config}".disabled)"
  72. if [ "${mode}" = "ap" ] && ([ -z "${disabled}" ] || [ "${disabled}" = "0" ]) && \
  73. ([ -z "${trm_radio}" ] || [ "${trm_radio}" = "${radio}" ])
  74. then
  75. trm_radiolist="${trm_radiolist} ${radio}"
  76. elif [ "${mode}" = "sta" ]
  77. then
  78. trm_stalist="${trm_stalist} ${config}_${radio}"
  79. if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
  80. then
  81. uci -q set wireless."${config}".disabled=1
  82. fi
  83. fi
  84. f_log "debug" "mode: ${mode}, radio: ${radio}, config: ${config}, disabled: ${disabled}"
  85. }
  86. # f_check: check interface status
  87. #
  88. f_check()
  89. {
  90. local ifname radio cnt=1 mode="${1}"
  91. trm_ifstatus="false"
  92. while [ ${cnt} -le ${trm_maxwait} ]
  93. do
  94. if [ "${mode}" = "ap" ]
  95. then
  96. for radio in ${trm_radiolist}
  97. do
  98. trm_ifstatus="$(ubus -S call network.wireless status | jsonfilter -e "@.${radio}.up")"
  99. if [ "${trm_ifstatus}" = "true" ]
  100. then
  101. trm_aplist="${trm_aplist} $(ubus -S call network.wireless status | jsonfilter -e "@.${radio}.interfaces[@.config.mode=\"ap\"].ifname")_${radio}"
  102. ifname="${trm_aplist}"
  103. else
  104. trm_aplist=""
  105. trm_ifstatus="false"
  106. break
  107. fi
  108. done
  109. else
  110. ifname="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  111. if [ -n "${ifname}" ]
  112. then
  113. trm_ifstatus="$(ubus -S call network.interface dump | jsonfilter -e "@.interface[@.device=\"${ifname}\"].up")"
  114. fi
  115. fi
  116. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  117. then
  118. break
  119. fi
  120. cnt=$((cnt+1))
  121. sleep 1
  122. done
  123. f_log "debug" "mode: ${mode}, name: ${ifname}, status: ${trm_ifstatus}, count: ${cnt}, max-wait: ${trm_maxwait}, automatic: ${trm_automatic}"
  124. }
  125. # f_ubus: update ubus service
  126. #
  127. f_ubus()
  128. {
  129. local active_triggers iface="${1}" radio="${2}" ssid="${3}"
  130. for name in ${trm_iface}
  131. do
  132. active_triggers="${active_triggers}[\"interface.*.down\",[\"if\",[\"eq\",\"interface\",\"${name}\"],[\"run_script\",\"/etc/init.d/travelmate\",\"start\"],1000]],"
  133. done
  134. active_triggers="${active_triggers}[\"config.change\",[\"if\",[\"eq\",\"package\",\"travelmate\"],[\"run_script\",\"/etc/init.d/travelmate\",\"start\"],1000]]"
  135. ubus call service add "{\"name\":\"travelmate\",
  136. \"instances\":{\"travelmate\":{\"command\":[\"/usr/bin/travelmate.sh\"],
  137. \"data\":{\"travelmate_version\":\"${trm_ver}\",
  138. \"station_interface\":\"${iface}\",
  139. \"station_radio\":\"${radio}\",
  140. \"station_ssid\":\"${ssid}\",
  141. \"last_rundate\":\"$(/bin/date "+%d.%m.%Y %H:%M:%S")\",
  142. \"online_status\":\"${trm_ifstatus}\",
  143. \"system\":\"${trm_sysver}\"}}},
  144. \"triggers\":[${active_triggers}]}"
  145. }
  146. # f_log: write to syslog, exit on error
  147. #
  148. f_log()
  149. {
  150. local class="${1}"
  151. local log_msg="${2}"
  152. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  153. then
  154. logger -t "travelmate-[${trm_ver}] ${class}" "${log_msg}"
  155. if [ "${class}" = "error" ]
  156. then
  157. logger -t "travelmate-[${trm_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  158. exit 255
  159. fi
  160. fi
  161. }
  162. # f_main: main function for connection handling
  163. #
  164. f_main()
  165. {
  166. local config ssid_list ap ap_radio sta_ssid sta_radio sta_iface cnt=1
  167. f_check "initial"
  168. if [ "${trm_ifstatus}" != "true" ]
  169. then
  170. config_load wireless
  171. config_foreach f_prepare wifi-iface
  172. if [ -n "$(uci -q changes wireless)" ]
  173. then
  174. uci -q commit wireless
  175. ubus call network reload
  176. sleep 5
  177. fi
  178. f_check "ap"
  179. f_log "debug" "ap-list: ${trm_aplist}, sta-list: ${trm_stalist}"
  180. if [ -z "${trm_aplist}" ] || [ -z "${trm_stalist}" ]
  181. then
  182. f_log "error" "no usable AP/STA configuration found"
  183. fi
  184. for ap in ${trm_aplist}
  185. do
  186. cnt=1
  187. ap_radio="${ap##*_}"
  188. ap="${ap%%_*}"
  189. if [ -z "$(printf "${trm_stalist}" | grep -Fo "_${ap_radio}")" ]
  190. then
  191. continue
  192. fi
  193. while [ ${cnt} -le ${trm_maxretry} ]
  194. do
  195. if [ ${trm_iw} -eq 1 ]
  196. then
  197. ssid_list="$(${trm_scanner} dev "${ap}" scan 2>/dev/null | \
  198. awk '/SSID: /{if(!seen[$0]++){printf "\"";for(i=2; i<=NF; i++)if(i==2)printf $i;else printf " "$i;printf "\" "}}')"
  199. else
  200. ssid_list="$(${trm_scanner} "${ap}" scan | \
  201. awk '/ESSID: ".*"/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
  202. fi
  203. f_log "debug" "scanner: ${trm_scanner}, ap: ${ap}, ssids: ${ssid_list}"
  204. if [ -n "${ssid_list}" ]
  205. then
  206. for sta in ${trm_stalist}
  207. do
  208. config="${sta%%_*}"
  209. sta_radio="${sta##*_}"
  210. sta_ssid="$(uci -q get wireless."${config}".ssid)"
  211. sta_iface="$(uci -q get wireless."${config}".network)"
  212. if [ -n "$(printf "${ssid_list}" | grep -Fo "\"${sta_ssid}\"")" ] && [ "${ap_radio}" = "${sta_radio}" ]
  213. then
  214. uci -q set wireless."${config}".disabled=0
  215. ubus call network reload
  216. f_check "sta"
  217. if [ "${trm_ifstatus}" = "true" ]
  218. then
  219. uci -q commit wireless
  220. f_log "info " "interface '${sta_iface}' on '${sta_radio}' connected to uplink '${sta_ssid}' (${trm_sysver})"
  221. sleep 5
  222. f_ubus "${sta_iface}" "${sta_radio}" "${sta_ssid}"
  223. return 0
  224. else
  225. uci -q revert wireless
  226. ubus call network reload
  227. f_log "info " "interface '${sta_iface}' on '${sta_radio}' can't connect to uplink '${sta_ssid}' (${trm_sysver})"
  228. f_ubus "${sta_iface}" "${sta_radio}" "${sta_ssid}"
  229. fi
  230. fi
  231. done
  232. fi
  233. cnt=$((cnt+1))
  234. sleep 5
  235. done
  236. done
  237. fi
  238. }
  239. f_envload
  240. f_main
  241. while [ ${trm_automatic} -eq 1 ]
  242. do
  243. sleep ${trm_timeout}
  244. f_envload
  245. f_main
  246. done
  247. exit 0