1 #!/bin/sh /etc/rc.common
2 # Copyright (c) 2016-2024 Dirk Brenken (dev@brenken.org)
3 # This is free software, licensed under the GNU General Public License v3.
5 # set (s)hellcheck exceptions
6 # shellcheck disable=all
11 extra_command
"scan" "[<radio>|<ifname>] Scan for available nearby uplinks"
12 extra_command
"assoc" "[<radio>|<ifname>] Get MAC adresses of associated wlan stations"
13 extra_command
"setup" "[<iface>] [<zone>] [<metric>] Setup the travelmate uplink interface, by default 'trm_wwan' with firewall zone 'wan' and metric '100'"
15 trm_init
="/etc/init.d/travelmate"
16 trm_script
="/usr/bin/travelmate.sh"
17 trm_pidfile
="/var/run/travelmate.pid"
18 trm_scanfile
="/var/run/travelmate.scan"
21 if [ -s "${trm_pidfile}" ]; then
24 rc_procd start_service
28 if "${trm_init}" enabled
; then
29 if [ "${action}" = "boot" ]; then
32 procd_open_instance
"travelmate"
33 procd_set_param
command "${trm_script}" "${@}"
34 procd_set_param pidfile
"${trm_pidfile}"
35 procd_set_param nice
"$(uci_get travelmate global trm_nice "0")"
36 procd_set_param stdout
1
37 procd_set_param stderr
1
43 local ppid pid timeout
45 timeout
="$(uci_get travelmate global trm_timeout)"
47 if [ -s "${trm_pidfile}" ]; then
48 ppid
="$(cat "${trm_pidfile}" 2>/dev/null)"
49 if [ -n "${ppid}" ]; then
50 pid
="$(pgrep -xnf "sleep ${timeout:-60} 0" -P ${ppid} 2>/dev/null)"
51 if [ -n "${pid}" ]; then
52 kill -INT ${pid} 2>/dev
/null
59 rc_procd
"${trm_script}" stop
63 local key keylist value rtfile
65 rtfile
="$(uci_get travelmate global trm_rtfile "/tmp
/trm_runtime.json
")"
66 json_load_file
"${rtfile}" >/dev
/null
2>&1
67 if json_select data
>/dev
/null
2>&1; then
68 printf "%s\n" "::: travelmate runtime information"
70 for key
in ${keylist}; do
71 json_get_var value
"${key}"
72 printf " + %-18s : %s\n" "${key}" "${value}"
75 printf "%s\n" "::: no travelmate runtime information available"
80 local result radio
="${1}"
83 if [ -z "${radio}" ]; then
84 radio
="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@[@.up=true].interfaces[0].ifname')"
86 result
="$(iwinfo "${radio}" scan 2>/dev/null |
87 awk 'BEGIN{FS="[[:space
:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";
88 for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i}}/Channel:/{var3=$NF}/Quality:/{split($NF,var0,"/")}/Encryption:/{var4="";
89 for(j=12;j<=NF;j++)if(var4==""){var4=$j}else{var4=var4" "$j};printf " %-11i%-10s%-35s%-20s%s
\n",(var0[1]*100/var0[2]),var3,var2,var1,var4}' |
91 printf "::: %s\n:::\n" "Available nearby uplinks on '${radio}'"
92 if [ -n "${result}" ]; then
93 printf "%s\n" "${result}" > "${trm_scanfile}"
94 printf "%-15s%-10s%-35s%-20s%s\n" " Strength" "Channel" "ESSID" "BSSID" "Encryption"
95 printf "%s\n" " --------------------------------------------------------------------------------------"
96 printf "%s\n" "${result}"
98 printf "%s\n" "::: Empty resultset"
103 local iface cnt
=0 input
="${1:-"trm_wwan"}" zone="${2:-"wan"}" metric="${3:-"100"}"
105 iface
="$(uci_get travelmate global trm_iface)"
106 input
="${input//[+*~%&\$@\"\' ]/}"
107 zone
="${zone//[+*~%&\$@\"\' ]/}"
108 metric
="${metric//[^0-9]/}"
110 if [ -n "${iface}" ] && [ "${iface}" = "${input}" ]; then
111 printf "%s\n" "The uplink interface '${input}' has been already configured"
112 elif [ -n "${input}" ]; then
113 if [ -n "${iface}" ]; then
115 del network."${iface}"
116 del network."${iface}6"
120 set travelmate.global.trm_enabled="1"
121 set travelmate.global.trm_iface="${input}"
122 set network."${input}"="interface"
123 set network."${input}".proto="dhcp"
124 set network."${input}".metric="${metric}"
125 set network."${input}6"=interface
126 set network."${input}6".device="@${input}"
127 set network."${input}6".proto="dhcpv6"
132 while [ -n "$(uci -q get firewall.@zone["${cnt}"].name)" ]; do
133 if [ "$(uci -q get firewall.@zone["${cnt}"].name)" = "${zone}" ]; then
134 if [ -n "${iface}" ]; then
136 del_list firewall.@zone["${cnt}"].network="${iface}"
137 del_list firewall.@zone["${cnt}"].network="${iface}6"
141 add_list firewall.@zone["${cnt}"].network="${input}"
142 add_list firewall.@zone["${cnt}"].network="${input}6"
150 if [ -n "${iface}" ]; then
152 while [ -n "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" ]; do
153 if [ "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" = "${iface}" ]; then
154 uci
-q set wireless.@wifi-iface
["${cnt}"].network
="${input}"
158 uci
-q commit wireless
160 /etc
/init.d
/network reload
>/dev
/null
2>&1
161 /etc
/init.d
/firewall reload
>/dev
/null
2>&1
162 "${trm_init}" restart
167 local result radio
="${1}"
169 if [ -z "${radio}" ]; then
170 radio
="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -q -l1 -e '@[@.*.*.config.mode="ap
"].interfaces[0].ifname')"
172 result
="$(iwinfo "${radio}" assoc 2>/dev/null | awk '/^[A-Z0-9:]+/{printf " %s
\n",$1}')"
173 printf "%s\n" "::: Associated wlan stations on '${radio}'"
175 if [ -n "${result}" ]; then
176 printf "%s\n" " MAC addresses"
177 printf "%s\n" " -----------------"
178 printf "%s\n" "${result}"
180 printf "%s\n" "::: Empty resultset"
187 iface
="$(uci_get travelmate global trm_iface)"
188 delay
="$(uci_get travelmate global trm_triggerdelay "2")"
189 PROCD_RELOAD_DELAY
=$
((delay
* 1000))
191 if [ -n "${iface}" ]; then
192 procd_add_interface_trigger
"interface.*.down" "${iface}" "${trm_init}" reload
194 procd_add_raw_trigger
"interface.*.up" "${PROCD_RELOAD_DELAY}" "${trm_init}" start
195 procd_add_config_trigger
"config.change" "travelmate" "${trm_init}" restart