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.

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