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.

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