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.

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