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.

337 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.0"
  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" ]
  111. then
  112. trm_devlist="${trm_devlist} ${radio}"
  113. else
  114. trm_devlist=""
  115. break
  116. fi
  117. done
  118. ifname="${trm_devlist}"
  119. else
  120. ifname="$(printf "%s" "${status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  121. if [ -n "${ifname}" ]
  122. then
  123. trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
  124. fi
  125. fi
  126. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  127. then
  128. break
  129. fi
  130. fi
  131. cnt=$((cnt+1))
  132. sleep 1
  133. done
  134. f_log "debug" "check: ${mode}, name: ${ifname}, status: ${trm_ifstatus}, count: ${cnt}, max-wait: ${trm_maxwait}, automatic: ${trm_automatic}"
  135. }
  136. # f_jsnupdate: update runtime information
  137. #
  138. f_jsnupdate()
  139. {
  140. local iface="${1}" radio="${2}" essid="${3:-"-"}" bssid="${4:-"-"}"
  141. json_init
  142. json_add_object "data"
  143. json_add_string "travelmate_version" "${trm_ver}"
  144. json_add_string "station_connection" "${trm_ifstatus}"
  145. json_add_string "station_id" "${essid}/${bssid}"
  146. json_add_string "station_interface" "${iface}"
  147. json_add_string "station_radio" "${radio}"
  148. json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  149. json_add_string "system" "${trm_sysver}"
  150. json_close_object
  151. json_dump > "${trm_rtfile}"
  152. }
  153. # f_status: output runtime information
  154. #
  155. f_status()
  156. {
  157. local key keylist value
  158. if [ -s "${trm_rtfile}" ]
  159. then
  160. printf "%s\n" "::: travelmate runtime information"
  161. json_load "$(cat "${trm_rtfile}" 2>/dev/null)"
  162. json_select data
  163. json_get_keys keylist
  164. for key in ${keylist}
  165. do
  166. json_get_var value "${key}"
  167. printf " %-18s : %s\n" "${key}" "${value}"
  168. done
  169. fi
  170. }
  171. # f_log: write to syslog, exit on error
  172. #
  173. f_log()
  174. {
  175. local class="${1}"
  176. local log_msg="${2}"
  177. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  178. then
  179. logger -t "travelmate-[${trm_ver}] ${class}" "${log_msg}"
  180. if [ "${class}" = "error" ]
  181. then
  182. logger -t "travelmate-[${trm_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  183. exit 255
  184. fi
  185. fi
  186. }
  187. # f_main: main function for connection handling
  188. #
  189. f_main()
  190. {
  191. local dev config raw_scan essid_list bssid_list sta_essid sta_bssid sta_radio sta_iface cnt=1
  192. f_check "initial"
  193. if [ "${trm_ifstatus}" != "true" ]
  194. then
  195. > "${trm_rtfile}"
  196. config_load wireless
  197. config_foreach f_prepare wifi-iface
  198. if [ -n "$(uci -q changes wireless)" ]
  199. then
  200. uci -q commit wireless
  201. fi
  202. f_check "dev"
  203. f_log "debug" "main: ${trm_devlist}, sta-list: ${trm_stalist}"
  204. for dev in ${trm_devlist}
  205. do
  206. cnt=1
  207. if [ -z "$(printf "%s" "${trm_stalist}" | grep -Fo "_${dev}")" ]
  208. then
  209. continue
  210. fi
  211. while [ ${trm_maxretry} -eq 0 ] || [ ${cnt} -le ${trm_maxretry} ]
  212. do
  213. raw_scan="$(${trm_iwinfo} "${dev}" scan)"
  214. essid_list="$(printf "%s" "${raw_scan}" | awk '/ESSID: "/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
  215. bssid_list="$(printf "%s" "${raw_scan}" | awk '/Address: /{ORS=" ";if (!seen[$5]++) print $5}')"
  216. f_log "debug" "main: ${trm_iwinfo}, dev: ${dev}"
  217. f_log "debug" "main: ${essid_list}"
  218. f_log "debug" "main: ${bssid_list}"
  219. if [ -n "${essid_list}" ] || [ -n "${bssid_list}" ]
  220. then
  221. for sta in ${trm_stalist}
  222. do
  223. config="${sta%%_*}"
  224. sta_radio="${sta##*_}"
  225. sta_essid="$(uci -q get wireless."${config}".ssid)"
  226. sta_bssid="$(uci -q get wireless."${config}".bssid)"
  227. sta_iface="$(uci -q get wireless."${config}".network)"
  228. if (([ -n "$(printf "%s" "${essid_list}" | grep -Fo "\"${sta_essid}\"")" ] && [ -z "${sta_bssid}" ]) || \
  229. ([ -n "$(printf "%s" "${bssid_list}" | grep -Fo "${sta_bssid}")" ] && [ -z "$(printf "%s" "${essid_list}" | grep -Fo "\"${sta_essid}\"")" ]) || \
  230. ([ -n "$(printf "%s" "${essid_list}" | grep -Fo "\"${sta_essid}\"")" ] && [ -n "$(printf "%s" "${bssid_list}" | grep -Fo "${sta_bssid}")" ])) && \
  231. [ "${dev}" = "${sta_radio}" ]
  232. then
  233. uci -q set wireless."${config}".disabled=0
  234. f_check "sta"
  235. if [ "${trm_ifstatus}" = "true" ]
  236. then
  237. uci -q commit wireless
  238. f_log "info " "interface '${sta_iface}' on '${sta_radio}' connected to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' (${trm_sysver})"
  239. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
  240. return 0
  241. elif [ ${trm_maxretry} -ne 0 ] && [ ${cnt} -eq ${trm_maxretry} ]
  242. then
  243. uci -q set wireless."${config}".disabled=1
  244. if [ -n "${sta_essid}" ]
  245. then
  246. uci -q set wireless."${config}".ssid="${sta_essid}_err"
  247. fi
  248. if [ -n "${sta_bssid}" ]
  249. then
  250. uci -q set wireless."${config}".bssid="${sta_bssid}_err"
  251. fi
  252. uci -q commit wireless
  253. f_check "dev"
  254. f_log "info " "can't connect to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}), uplink disabled (${trm_sysver})"
  255. else
  256. if [ ${trm_maxretry} -eq 0 ]
  257. then
  258. cnt=0
  259. fi
  260. uci -q revert wireless
  261. f_check "dev"
  262. f_log "info " "can't connect to uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}) (${trm_sysver})"
  263. fi
  264. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
  265. fi
  266. done
  267. fi
  268. cnt=$((cnt+1))
  269. sleep 5
  270. done
  271. done
  272. if [ ! -s "${trm_rtfile}" ]
  273. then
  274. trm_ifstatus="false"
  275. f_jsnupdate "n/a" "n/a"
  276. fi
  277. else
  278. if [ ! -s "${trm_rtfile}" ]
  279. then
  280. config="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  281. sta_radio="$(uci -q get wireless."${config}".device)"
  282. sta_essid="$(uci -q get wireless."${config}".ssid)"
  283. sta_bssid="$(uci -q get wireless."${config}".bssid)"
  284. sta_iface="$(uci -q get wireless."${config}".network)"
  285. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
  286. fi
  287. fi
  288. }
  289. # source required system libraries
  290. #
  291. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  292. then
  293. . "/lib/functions.sh"
  294. . "/usr/share/libubox/jshn.sh"
  295. else
  296. f_log "error" "system libraries not found"
  297. fi
  298. # handle different travelmate actions
  299. #
  300. f_envload
  301. case "${1}" in
  302. status)
  303. f_status
  304. ;;
  305. *)
  306. f_main
  307. while [ ${trm_automatic} -eq 1 ]
  308. do
  309. sleep ${trm_timeout}
  310. f_envload
  311. f_main
  312. done
  313. ;;
  314. esac
  315. exit 0