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.

469 lines
15 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.3.0"
  12. trm_sysver="unknown"
  13. trm_enabled=0
  14. trm_debug=0
  15. trm_captive=1
  16. trm_proactive=1
  17. trm_captiveurl="http://captive.apple.com"
  18. trm_minquality=35
  19. trm_maxretry=3
  20. trm_maxwait=30
  21. trm_timeout=60
  22. trm_radio=""
  23. trm_connection=""
  24. trm_rtfile="/tmp/trm_runtime.json"
  25. trm_fetch="$(command -v uclient-fetch)"
  26. trm_iwinfo="$(command -v iwinfo)"
  27. trm_wpa="$(command -v wpa_supplicant)"
  28. trm_action="${1:-"start"}"
  29. trm_pidfile="/var/run/travelmate.pid"
  30. # trim leading and trailing whitespace characters
  31. #
  32. f_trim()
  33. {
  34. local trim="$1"
  35. trim="${trim#"${trim%%[![:space:]]*}"}"
  36. trim="${trim%"${trim##*[![:space:]]}"}"
  37. printf '%s' "${trim}"
  38. }
  39. # load travelmate environment
  40. #
  41. f_envload()
  42. {
  43. local sys_call sys_desc sys_model sys_ver
  44. # get system information
  45. #
  46. sys_call="$(ubus -S call system board 2>/dev/null)"
  47. if [ -n "${sys_call}" ]
  48. then
  49. sys_desc="$(printf '%s' "${sys_call}" | jsonfilter -e '@.release.description')"
  50. sys_model="$(printf '%s' "${sys_call}" | jsonfilter -e '@.model')"
  51. trm_sysver="${sys_model}, ${sys_desc}"
  52. fi
  53. # (re-)initialize global list variables
  54. #
  55. unset trm_devlist trm_stalist trm_radiolist trm_active_sta
  56. # load config and check 'enabled' option
  57. #
  58. option_cb()
  59. {
  60. local option="${1}"
  61. local value="${2}"
  62. eval "${option}=\"${value}\""
  63. }
  64. config_load travelmate
  65. if [ ${trm_enabled} -ne 1 ]
  66. then
  67. f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
  68. exit 0
  69. fi
  70. # validate input ranges
  71. #
  72. if [ ${trm_minquality} -lt 20 ] || [ ${trm_minquality} -gt 80 ]
  73. then
  74. trm_minquality=35
  75. fi
  76. if [ ${trm_maxretry} -lt 1 ] || [ ${trm_maxretry} -gt 10 ]
  77. then
  78. trm_maxretry=3
  79. fi
  80. if [ ${trm_maxwait} -lt 20 ] || [ ${trm_maxwait} -gt 40 ] || [ ${trm_maxwait} -ge ${trm_timeout} ]
  81. then
  82. trm_maxwait=30
  83. fi
  84. if [ ${trm_timeout} -lt 30 ] || [ ${trm_timeout} -gt 300 ] || [ ${trm_timeout} -le ${trm_maxwait} ]
  85. then
  86. trm_timeout=60
  87. fi
  88. }
  89. # gather radio information & bring down all STA interfaces
  90. #
  91. f_prep()
  92. {
  93. local eap_rc=0 config="${1}" proactive="${2}"
  94. local mode="$(uci_get wireless "${config}" mode)"
  95. local network="$(uci_get wireless "${config}" network)"
  96. local radio="$(uci_get wireless "${config}" device)"
  97. local disabled="$(uci_get wireless "${config}" disabled)"
  98. local eaptype="$(uci_get wireless "${config}" eap_type)"
  99. if [ -z "${trm_radio}" ] && [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo " ${radio}")" ]
  100. then
  101. trm_radiolist="$(f_trim "${trm_radiolist} ${radio}")"
  102. elif [ -n "${trm_radio}" ] && [ -z "${trm_radiolist}" ]
  103. then
  104. trm_radiolist="$(f_trim "$(printf "%s" "${trm_radio}" | \
  105. awk '{while(match(tolower($0),/radio[0-9]/)){ORS=" ";print substr(tolower($0),RSTART,RLENGTH);$0=substr($0,RSTART+RLENGTH)}}')")"
  106. fi
  107. if [ "${mode}" = "sta" ] && [ "${network}" = "${trm_iface}" ]
  108. then
  109. if ([ -z "${disabled}" ] || [ "${disabled}" = "0" ]) && ([ ${proactive} -eq 0 ] || [ "${trm_ifstatus}" != "true" ])
  110. then
  111. uci_set wireless "${config}" disabled 1
  112. elif [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ -z "${trm_active_sta}" ] && [ ${proactive} -eq 1 ]
  113. then
  114. trm_active_sta="${config}"
  115. fi
  116. if [ -n "${eaptype}" ]
  117. then
  118. eap_rc="$("${trm_wpa}" -veap >/dev/null 2>&1; printf "%u" ${?})"
  119. fi
  120. if [ -z "${eaptype}" ] || [ ${eap_rc} -eq 0 ]
  121. then
  122. trm_stalist="$(f_trim "${trm_stalist} ${config}_${radio}")"
  123. fi
  124. fi
  125. f_log "debug" "f_prep ::: config: ${config}, mode: ${mode}, network: ${network}, eap_rc: ${eap_rc}, radio: ${radio}, trm_radio: ${trm_radio:-"-"}, trm_active_sta: ${trm_active_sta:-"-"}, proactive: ${proactive}, disabled: ${disabled}"
  126. }
  127. # check interface status
  128. #
  129. f_check()
  130. {
  131. local IFS ifname radio dev_status config sta_essid sta_bssid result wait=1 mode="${1}" status="${2:-"false"}"
  132. trm_ifquality=0
  133. if [ "${mode}" = "initial" ]
  134. then
  135. trm_ifstatus="false"
  136. else
  137. ubus call network reload
  138. fi
  139. while [ ${wait} -le ${trm_maxwait} ]
  140. do
  141. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  142. if [ -n "${dev_status}" ]
  143. then
  144. if [ "${mode}" = "dev" ]
  145. then
  146. if [ "${trm_ifstatus}" != "${status}" ]
  147. then
  148. trm_ifstatus="${status}"
  149. f_jsnup
  150. fi
  151. for radio in ${trm_radiolist}
  152. do
  153. result="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e "@.${radio}.up")"
  154. if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo " ${radio}")" ]
  155. then
  156. trm_devlist="$(f_trim "${trm_devlist} ${radio}")"
  157. fi
  158. done
  159. if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ ${wait} -eq ${trm_maxwait} ]
  160. then
  161. ifname="${trm_devlist}"
  162. break
  163. else
  164. unset trm_devlist
  165. fi
  166. elif [ "${mode}" = "rev" ]
  167. then
  168. wait=$(( ${trm_maxwait} / 3 ))
  169. sleep ${wait}
  170. break
  171. else
  172. ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  173. if [ -n "${ifname}" ]
  174. then
  175. trm_ifquality="$(${trm_iwinfo} ${ifname} info 2>/dev/null | awk -F "[\/| ]" '/Link Quality:/{printf "%i\n", (100 / $NF * $(NF-1)) }')"
  176. if [ ${trm_ifquality} -ge ${trm_minquality} ]
  177. then
  178. trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
  179. elif [ "${mode}" = "initial" ] && [ ${trm_ifquality} -lt ${trm_minquality} ]
  180. then
  181. trm_ifstatus="${status}"
  182. sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
  183. sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
  184. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality}), uplink disconnected (${trm_sysver})"
  185. fi
  186. fi
  187. fi
  188. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  189. then
  190. if ([ "${trm_ifstatus}" != "true" ] && [ "${trm_ifstatus}" != "${status}" ]) || \
  191. ([ "${trm_ifstatus}" = "true" ] && [ "${mode}" = "sta" ] && [ -n "${trm_active_sta}" ]) || \
  192. [ ${trm_ifquality} -lt ${trm_minquality} ]
  193. then
  194. f_jsnup
  195. fi
  196. if [ "${mode}" = "initial" ] && [ "${trm_captive}" -eq 1 ] && [ "${trm_ifstatus}" = "true" ]
  197. then
  198. result="$(${trm_fetch} --timeout=$(( ${trm_maxwait} / 3 )) --spider "${trm_captiveurl}" 2>&1 | \
  199. awk '/^Redirected/{printf "%s" "net cp \047"$NF"\047";exit}/^Download completed/{printf "%s" "net ok";exit}/^Failed|^Connection error/{printf "%s" "net nok";exit}')"
  200. if [ -n "${result}" ] && ([ -z "${trm_connection}" ] || [ "${result}" != "${trm_connection%/*}" ])
  201. then
  202. trm_connection="${result}/${trm_ifquality}"
  203. f_jsnup
  204. fi
  205. fi
  206. break
  207. fi
  208. fi
  209. wait=$(( wait + 1 ))
  210. sleep 1
  211. done
  212. f_log "debug" "f_check::: mode: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, quality: ${trm_ifquality}, connection: ${trm_connection:-"-"}, wait: ${wait}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}"
  213. }
  214. # update runtime information
  215. #
  216. f_jsnup()
  217. {
  218. local config sta_iface sta_radio sta_essid sta_bssid dev_status status="${trm_ifstatus}" faulty_list faulty_station="${1}"
  219. if [ "${status}" = "true" ]
  220. then
  221. status="connected (${trm_connection:-"-"})"
  222. else
  223. unset trm_connection
  224. status="running / not connected"
  225. fi
  226. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  227. if [ -n "${dev_status}" ]
  228. then
  229. config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  230. if [ -n "${config}" ]
  231. then
  232. sta_iface="$(uci_get wireless "${config}" network)"
  233. sta_radio="$(uci_get wireless "${config}" device)"
  234. sta_essid="$(uci_get wireless "${config}" ssid)"
  235. sta_bssid="$(uci_get wireless "${config}" bssid)"
  236. fi
  237. fi
  238. json_get_var faulty_list "faulty_stations"
  239. if [ -n "${faulty_station}" ]
  240. then
  241. if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
  242. then
  243. faulty_list="$(f_trim "${faulty_list} ${faulty_station}")"
  244. fi
  245. fi
  246. json_add_string "travelmate_status" "${status}"
  247. json_add_string "travelmate_version" "${trm_ver}"
  248. json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
  249. json_add_string "station_interface" "${sta_iface:-"-"}"
  250. json_add_string "faulty_stations" "${faulty_list}"
  251. json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  252. json_add_string "system" "${trm_sysver}"
  253. json_dump > "${trm_rtfile}"
  254. f_log "debug" "f_jsnup::: config: ${config:-"-"}, status: ${status:-"-"}, sta_iface: ${sta_iface:-"-"}, sta_radio: ${sta_radio:-"-"}, sta_essid: ${sta_essid:-"-"}, sta_bssid: ${sta_bssid:-"-"}, faulty_list: ${faulty_list:-"-"}"
  255. }
  256. # write to syslog
  257. #
  258. f_log()
  259. {
  260. local class="${1}"
  261. local log_msg="${2}"
  262. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  263. then
  264. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
  265. if [ "${class}" = "err" ]
  266. then
  267. trm_ifstatus="error"
  268. f_jsnup
  269. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  270. exit 1
  271. fi
  272. fi
  273. }
  274. # main function for connection handling
  275. #
  276. f_main()
  277. {
  278. local IFS cnt dev config scan scan_list scan_essid scan_bssid scan_quality faulty_list
  279. local sta sta_essid sta_bssid sta_radio sta_iface active_essid active_bssid active_radio active_prio
  280. f_check "initial"
  281. if [ "${trm_ifstatus}" != "true" ] || [ ${trm_proactive} -eq 1 ]
  282. then
  283. config_load wireless
  284. config_foreach f_prep wifi-iface ${trm_proactive}
  285. if [ "${trm_ifstatus}" = "true" ] && [ ${trm_proactive} -eq 1 ]
  286. then
  287. json_get_var station_id "station_id"
  288. active_radio="${station_id%%/*}"
  289. active_essid="${station_id%/*}"
  290. active_essid="${active_essid#*/}"
  291. active_bssid="${station_id##*/}"
  292. f_check "dev" "true"
  293. else
  294. uci_commit wireless
  295. f_check "dev"
  296. fi
  297. json_get_var faulty_list "faulty_stations"
  298. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo:-"-"}, dev_list: ${trm_devlist:-"-"}, sta_list: ${trm_stalist:0:800}, faulty_list: ${faulty_list:-"-"}"
  299. for dev in ${trm_devlist}
  300. do
  301. f_log "debug" "f_main ::: dev: ${dev}"
  302. if [ -z "$(printf "%s" "${trm_stalist}" | grep -Fo "_${dev}")" ]
  303. then
  304. f_log "debug" "f_main ::: no station on '${dev}' - continue"
  305. continue
  306. fi
  307. cnt=1
  308. while [ ${cnt} -le ${trm_maxretry} ]
  309. do
  310. f_log "debug" "f_main ::: cnt: ${cnt}, max_cnt: ${trm_maxretry}"
  311. for sta in ${trm_stalist}
  312. do
  313. config="${sta%%_*}"
  314. sta_radio="${sta##*_}"
  315. sta_essid="$(uci_get wireless "${config}" ssid)"
  316. sta_bssid="$(uci_get wireless "${config}" bssid)"
  317. sta_iface="$(uci_get wireless "${config}" network)"
  318. json_get_var faulty_list "faulty_stations"
  319. if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
  320. then
  321. f_log "debug" "f_main ::: faulty station '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' - continue"
  322. continue
  323. fi
  324. if ([ "${dev}" = "${active_radio}" ] && [ "${sta_essid}" = "${active_essid}" ] && [ "${sta_bssid:-"-"}" = "${active_bssid}" ]) || \
  325. ([ "${dev}" != "${active_radio}" ] && [ "${active_prio}" = "true" ])
  326. then
  327. active_prio="true"
  328. f_log "debug" "f_main ::: active station prioritized '${active_radio}/${active_essid}/${active_bssid:-"-"}' - break"
  329. break
  330. fi
  331. if [ -z "${scan_list}" ]
  332. then
  333. scan_list="$(f_trim "$(${trm_iwinfo} "${dev}" scan 2>/dev/null | \
  334. awk 'BEGIN{FS="[/ ]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++) \
  335. if(var2==""){var2=$i}else{var2=var2" "$i}}/Quality:/{printf "%i,%s,%s\n",(100/$NF*$(NF-1)),var1,var2}' | \
  336. sort -rn | awk '{ORS=",";print $0}')")"
  337. f_log "debug" "f_main ::: scan_list: ${scan_list:0:800}"
  338. fi
  339. IFS=","
  340. for scan in ${scan_list}
  341. do
  342. if [ -z "${scan_quality}" ]
  343. then
  344. scan_quality="${scan}"
  345. elif [ -z "${scan_bssid}" ]
  346. then
  347. scan_bssid="${scan}"
  348. elif [ -z "${scan_essid}" ]
  349. then
  350. scan_essid="${scan}"
  351. fi
  352. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
  353. then
  354. if [ ${scan_quality} -ge ${trm_minquality} ]
  355. then
  356. if (([ "${scan_essid}" = "\"${sta_essid}\"" ] && ([ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ])) || \
  357. ([ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ])) && [ "${dev}" = "${sta_radio}" ]
  358. then
  359. f_log "debug" "f_main ::: scan_quality: ${scan_quality}, sta_bssid: ${sta_bssid}, scan_bssid: ${scan_bssid}, sta_essid: \"${sta_essid}\", scan_essid: ${scan_essid}"
  360. if [ "${dev}" = "${active_radio}" ] && [ -n "${trm_active_sta}" ]
  361. then
  362. uci_set wireless "${trm_active_sta}" disabled 1
  363. unset trm_connection
  364. fi
  365. uci_set wireless "${config}" disabled 0
  366. f_check "sta"
  367. if [ "${trm_ifstatus}" = "true" ]
  368. then
  369. uci_commit wireless
  370. f_check "initial"
  371. f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  372. return 0
  373. elif [ ${cnt} -eq ${trm_maxretry} ]
  374. then
  375. uci -q revert wireless
  376. f_check "rev"
  377. if [ "${dev}" = "${active_radio}" ] && [ -n "${trm_active_sta}" ]
  378. then
  379. f_check "initial"
  380. fi
  381. faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
  382. f_jsnup "${faulty_station}"
  383. f_log "info" "uplink disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  384. break
  385. else
  386. uci -q revert wireless
  387. f_check "rev"
  388. f_jsnup
  389. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  390. unset scan_list
  391. break
  392. fi
  393. fi
  394. fi
  395. unset scan_quality scan_bssid scan_essid
  396. fi
  397. done
  398. unset IFS scan_quality scan_bssid scan_essid
  399. done
  400. cnt=$(( cnt + 1 ))
  401. sleep $(( ${trm_maxwait} / 6 ))
  402. done
  403. unset scan_list
  404. done
  405. fi
  406. }
  407. # source required system libraries
  408. #
  409. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  410. then
  411. . "/lib/functions.sh"
  412. . "/usr/share/libubox/jshn.sh"
  413. else
  414. f_log "err" "system libraries not found"
  415. fi
  416. # initialize json runtime file
  417. #
  418. json_load_file "${trm_rtfile}" >/dev/null 2>&1
  419. json_select data >/dev/null 2>&1
  420. if [ ${?} -ne 0 ]
  421. then
  422. > "${trm_rtfile}"
  423. json_init
  424. json_add_object "data"
  425. fi
  426. # control travelmate actions
  427. #
  428. while true
  429. do
  430. if [ -z "${trm_action}" ]
  431. then
  432. sleep ${trm_timeout}
  433. elif [ "${trm_action}" = "stop" ]
  434. then
  435. > "${trm_rtfile}"
  436. f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
  437. exit 0
  438. else
  439. f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
  440. unset trm_action
  441. fi
  442. f_envload
  443. f_main
  444. done