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.

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