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.

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