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.

476 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.4"
  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. if [ "${status}" = "false" ]
  138. then
  139. ubus call network reload
  140. fi
  141. fi
  142. while [ ${wait} -le ${trm_maxwait} ]
  143. do
  144. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  145. if [ -n "${dev_status}" ]
  146. then
  147. if [ "${mode}" = "dev" ]
  148. then
  149. if [ "${trm_ifstatus}" != "${status}" ]
  150. then
  151. trm_ifstatus="${status}"
  152. f_jsnup
  153. fi
  154. for radio in ${trm_radiolist}
  155. do
  156. result="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e "@.${radio}.up")"
  157. if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo "${radio}")" ]
  158. then
  159. trm_devlist="$(f_trim "${trm_devlist} ${radio}")"
  160. fi
  161. done
  162. if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ ${wait} -eq ${trm_maxwait} ]
  163. then
  164. ifname="${trm_devlist}"
  165. break
  166. else
  167. unset trm_devlist
  168. fi
  169. elif [ "${mode}" = "rev" ]
  170. then
  171. wait=$(( ${trm_maxwait} / 3 ))
  172. sleep ${wait}
  173. break
  174. else
  175. ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  176. if [ -n "${ifname}" ]
  177. then
  178. trm_ifquality="$(${trm_iwinfo} ${ifname} info 2>/dev/null | awk -F "[\/| ]" '/Link Quality:/{printf "%i\n", (100 / $NF * $(NF-1)) }')"
  179. if [ ${trm_ifquality} -ge ${trm_minquality} ]
  180. then
  181. trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
  182. elif [ "${mode}" = "initial" ] && [ ${trm_ifquality} -lt ${trm_minquality} ]
  183. then
  184. trm_ifstatus="${status}"
  185. sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
  186. sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
  187. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality}), uplink disconnected (${trm_sysver})"
  188. fi
  189. fi
  190. fi
  191. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  192. then
  193. if ([ "${trm_ifstatus}" != "true" ] && [ "${trm_ifstatus}" != "${status}" ]) || \
  194. ([ "${trm_ifstatus}" = "true" ] && [ "${mode}" = "sta" ] && [ -n "${trm_active_sta}" ]) || \
  195. [ ${trm_ifquality} -lt ${trm_minquality} ]
  196. then
  197. f_jsnup
  198. fi
  199. if [ "${mode}" = "initial" ] && [ ${trm_captive} -eq 1 ] && [ "${trm_ifstatus}" = "true" ]
  200. then
  201. result="$(${trm_fetch} --timeout=$(( ${trm_maxwait} / 3 )) "${trm_captiveurl}" -O /dev/null 2>&1 | \
  202. 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}')"
  203. if [ -n "${result}" ] && ([ -z "${trm_connection}" ] || [ "${result}" != "${trm_connection%/*}" ])
  204. then
  205. trm_connection="${result}/${trm_ifquality}"
  206. f_jsnup
  207. fi
  208. fi
  209. break
  210. fi
  211. fi
  212. wait=$(( wait + 1 ))
  213. sleep 1
  214. done
  215. f_log "debug" "f_check::: mode: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, quality: ${trm_ifquality}, result: ${result:-"-"}, connection: ${trm_connection:-"-"}, wait: ${wait}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}"
  216. }
  217. # update runtime information
  218. #
  219. f_jsnup()
  220. {
  221. local config sta_iface sta_radio sta_essid sta_bssid dev_status status="${trm_ifstatus}" faulty_list faulty_station="${1}"
  222. if [ "${status}" = "true" ]
  223. then
  224. status="connected (${trm_connection:-"-"})"
  225. else
  226. unset trm_connection
  227. status="running / not connected"
  228. fi
  229. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  230. if [ -n "${dev_status}" ]
  231. then
  232. config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  233. if [ -n "${config}" ]
  234. then
  235. sta_iface="$(uci_get wireless "${config}" network)"
  236. sta_radio="$(uci_get wireless "${config}" device)"
  237. sta_essid="$(uci_get wireless "${config}" ssid)"
  238. sta_bssid="$(uci_get wireless "${config}" bssid)"
  239. fi
  240. fi
  241. json_get_var faulty_list "faulty_stations"
  242. if [ -n "${faulty_station}" ]
  243. then
  244. if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
  245. then
  246. faulty_list="$(f_trim "${faulty_list} ${faulty_station}")"
  247. fi
  248. fi
  249. json_add_string "travelmate_status" "${status}"
  250. json_add_string "travelmate_version" "${trm_ver}"
  251. json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
  252. json_add_string "station_interface" "${sta_iface:-"-"}"
  253. json_add_string "faulty_stations" "${faulty_list}"
  254. json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  255. json_add_string "system" "${trm_sysver}"
  256. json_dump > "${trm_rtfile}"
  257. 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:-"-"}"
  258. }
  259. # write to syslog
  260. #
  261. f_log()
  262. {
  263. local class="${1}"
  264. local log_msg="${2}"
  265. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  266. then
  267. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
  268. if [ "${class}" = "err" ]
  269. then
  270. trm_ifstatus="error"
  271. f_jsnup
  272. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  273. exit 1
  274. fi
  275. fi
  276. }
  277. # main function for connection handling
  278. #
  279. f_main()
  280. {
  281. local IFS cnt dev config scan scan_list scan_essid scan_bssid scan_quality faulty_list
  282. local sta sta_essid sta_bssid sta_radio sta_iface active_essid active_bssid active_radio
  283. f_check "initial"
  284. f_log "debug" "f_main ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
  285. if [ "${trm_ifstatus}" != "true" ] || [ ${trm_proactive} -eq 1 ]
  286. then
  287. config_load wireless
  288. config_foreach f_prep wifi-iface ${trm_proactive}
  289. if [ "${trm_ifstatus}" = "true" ] && [ ${trm_proactive} -eq 1 ]
  290. then
  291. json_get_var station_id "station_id"
  292. active_radio="${station_id%%/*}"
  293. active_essid="${station_id%/*}"
  294. active_essid="${active_essid#*/}"
  295. active_bssid="${station_id##*/}"
  296. f_check "dev" "true"
  297. else
  298. uci_commit wireless
  299. f_check "dev"
  300. fi
  301. json_get_var faulty_list "faulty_stations"
  302. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo:-"-"}, dev_list: ${trm_devlist:-"-"}, sta_list: ${trm_stalist:0:800}, faulty_list: ${faulty_list:-"-"}"
  303. for dev in ${trm_devlist}
  304. do
  305. f_log "debug" "f_main ::: device: ${dev}"
  306. if [ -z "$(printf "%s" "${trm_stalist}" | grep -o "\-${dev}")" ]
  307. then
  308. f_log "debug" "f_main ::: no station on '${dev}' - continue"
  309. continue
  310. fi
  311. cnt=1
  312. while [ ${cnt} -le ${trm_maxretry} ]
  313. do
  314. f_log "debug" "f_main ::: cnt: ${cnt}, max_cnt: ${trm_maxretry}"
  315. for sta in ${trm_stalist}
  316. do
  317. config="${sta%%-*}"
  318. sta_radio="${sta##*-}"
  319. sta_essid="$(uci_get wireless "${config}" ssid)"
  320. sta_bssid="$(uci_get wireless "${config}" bssid)"
  321. sta_iface="$(uci_get wireless "${config}" network)"
  322. json_get_var faulty_list "faulty_stations"
  323. if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
  324. then
  325. f_log "debug" "f_main ::: faulty station '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' - continue"
  326. continue
  327. fi
  328. if [ "${dev}" = "${active_radio}" ] && [ "${sta_essid}" = "${active_essid}" ] && [ "${sta_bssid:-"-"}" = "${active_bssid}" ]
  329. then
  330. f_log "debug" "f_main ::: active station prioritized '${active_radio}/${active_essid}/${active_bssid:-"-"}' - break"
  331. break 3
  332. fi
  333. if [ -z "${scan_list}" ]
  334. then
  335. scan_list="$(f_trim "$("${trm_iwinfo}" "${dev}" scan 2>/dev/null | \
  336. awk 'BEGIN{FS="[/ ]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++) \
  337. if(var2==""){var2=$i}else{var2=var2" "$i}}/Quality:/{printf "%i,%s,%s\n",(100/$NF*$(NF-1)),var1,var2}' | \
  338. sort -rn | awk '{ORS=",";print $0}')")"
  339. f_log "debug" "f_main ::: scan_list: ${scan_list:0:800}"
  340. if [ -z "${scan_list}" ]
  341. then
  342. f_log "debug" "f_main ::: no scan results on '${dev}' - continue"
  343. continue 3
  344. fi
  345. fi
  346. IFS=","
  347. for scan in ${scan_list}
  348. do
  349. if [ -z "${scan_quality}" ]
  350. then
  351. scan_quality="${scan}"
  352. elif [ -z "${scan_bssid}" ]
  353. then
  354. scan_bssid="${scan}"
  355. elif [ -z "${scan_essid}" ]
  356. then
  357. scan_essid="${scan}"
  358. fi
  359. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
  360. then
  361. if [ ${scan_quality} -ge ${trm_minquality} ]
  362. then
  363. if (([ "${scan_essid}" = "\"${sta_essid}\"" ] && ([ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ])) || \
  364. ([ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ])) && [ "${dev}" = "${sta_radio}" ]
  365. then
  366. 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}"
  367. if [ "${dev}" = "${active_radio}" ] && [ -n "${trm_active_sta}" ]
  368. then
  369. uci_set wireless "${trm_active_sta}" disabled 1
  370. unset trm_connection
  371. fi
  372. uci_set wireless "${config}" disabled 0
  373. f_check "sta"
  374. if [ "${trm_ifstatus}" = "true" ]
  375. then
  376. uci_commit wireless
  377. f_check "initial"
  378. f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  379. return 0
  380. elif [ ${cnt} -eq ${trm_maxretry} ]
  381. then
  382. uci -q revert wireless
  383. f_check "rev"
  384. if [ "${dev}" = "${active_radio}" ] && [ -n "${trm_active_sta}" ]
  385. then
  386. f_check "initial"
  387. fi
  388. faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
  389. f_jsnup "${faulty_station}"
  390. f_log "info" "uplink disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  391. break
  392. else
  393. uci -q revert wireless
  394. f_check "rev"
  395. f_jsnup
  396. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${trm_sysver})"
  397. unset scan_list
  398. break
  399. fi
  400. fi
  401. fi
  402. unset scan_quality scan_bssid scan_essid
  403. fi
  404. done
  405. unset IFS scan_quality scan_bssid scan_essid
  406. done
  407. cnt=$(( cnt + 1 ))
  408. sleep $(( ${trm_maxwait} / 6 ))
  409. done
  410. unset scan_list
  411. done
  412. fi
  413. }
  414. # source required system libraries
  415. #
  416. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  417. then
  418. . "/lib/functions.sh"
  419. . "/usr/share/libubox/jshn.sh"
  420. else
  421. f_log "err" "system libraries not found"
  422. fi
  423. # initialize json runtime file
  424. #
  425. json_load_file "${trm_rtfile}" >/dev/null 2>&1
  426. json_select data >/dev/null 2>&1
  427. if [ ${?} -ne 0 ]
  428. then
  429. > "${trm_rtfile}"
  430. json_init
  431. json_add_object "data"
  432. fi
  433. # control travelmate actions
  434. #
  435. while true
  436. do
  437. if [ -z "${trm_action}" ]
  438. then
  439. sleep ${trm_timeout}
  440. elif [ "${trm_action}" = "stop" ]
  441. then
  442. > "${trm_rtfile}"
  443. f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
  444. exit 0
  445. else
  446. f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
  447. unset trm_action
  448. fi
  449. f_envload
  450. f_main
  451. done