This repository was archived by the owner on Jun 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathff-offline-ssid
executable file
·342 lines (303 loc) · 10.3 KB
/
ff-offline-ssid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/sh
# ff-offline-ssid by RubenKelevra 2013-2017 - [email protected]
# Lizenz: AGPL 3.0
#Warning: If SSID was changed in uci, which belongs to freifunk, you need to run "wifi" before you rerun this script.
#This script expect that an temporary SSID is set on boot. The script will read an online-ssid from uci and set it when node sees batman-gateways.
RUNTIME=60 #recommended runtime, to avoid memory-leaks.
END=0 #var
#Parameter Checking
if ! [ $# -eq 1 -o "$1" -eq "$1" ] 2>/dev/null; then
echo "Error: Please define a runtime in seconds, 60 is recommended" && exit 2
else
RUNTIME=$1
echo "Debug: Runtime will be $RUNTIME seconds, please restart me after this time"
echo ""
fi
START=`cat /proc/uptime | cut -d"." -f1`
END=$(( $START + $RUNTIME ))
END=$(( $END - 1 ))
#running-pid
OWNPID=$$
PIDFILE='/tmp/ff-offline-ssid.pid'
echo "Debug: Own PID is $OWNPID"
if [ ! -f "$PIDFILE" -a -f "$PIDFILE"_"STOP" ]; then
echo "Debug: No PID-File; but a stop file, wait one second..."
sleep 1 #sleep to fix possible race-conditions
fi
if [ ! -f "$PIDFILE" ]; then
echo -n "Debug: No PID-File found, writing own one..."
echo $OWNPID > $PIDFILE
echo " done."
[ -f "$PIDFILE"_"STOP" ] && rm "$PIDFILE"_"STOP" #cleanup
else
echo -n "Debug: PID-File found..."
#check if pid from pid-file is running
if ! kill -0 $(cat "$PIDFILE") > /dev/null 2>&1; then #it's not running
echo " but foreign process is no longer running."
echo -n "Writing own PID to file..."
echo $OWNPID > $PIDFILE
echo " done."
[ -f "$PIDFILE"_"STOP" ] && rm "$PIDFILE"_"STOP" #cleanup
else #pid is running
echo -n " foreign process is still running..."
if [ -f "$PIDFILE"_"STOP" ]; then # we're exiting now - one waiting process is enouth.
echo ""; echo "Warning: Offline-ssid exiting because pid-file (pid running!) and stopfile already exist."
exit 15
fi
echo "... request 'em to stop."
touch "$PIDFILE"_"STOP"
while [ `cat /proc/uptime | cut -d"." -f1` -lt $END ]; do # wait until the pid-process has ended gracefully
echo "waiting ..."
if [ ! -f $PIDFILE ]; then
echo "Debug: pid-file is now gone."
echo -n "Writing own PID-File..."
echo $OWNPID > $PIDFILE
echo " done."
[ -f "$PIDFILE"_"STOP" ] && rm "$PIDFILE"_"STOP" #cleanup
break
fi
sleep 1
done
fi
echo "";echo "starting up now...";echo ""
fi
# options
SLEEP=2 # sleep time in seconds between checks
ACTIVE_CHECK=1 # enable L2-ping checks to the selected gateways, if last-seen is too high. Needs batctl.
PING_CNT=10 # ping-packets for ACTIVE_CHECK: 3 is recommended, 5-10 for lossy connections
OGM_INT_MULT=2 # tolerable missing OGMs, before pinging (if ACTIVE_CHECK=1): 1 for fast responses, 2 for slower reactions, 3-4 for lossy connections
LED_STATUS=1 # change system-led, when offline: pulsing flashes with long pauses
SUB_CONF_ONLINE_SSID='ssid_online'
SUB_CONF_BOOT_SSID='ssid'
MINIMAL_ACCEPTABLE_GWQ=40
MIN_PING_PERCENT=80 #to be considered online while active checking
# vars
MODE=1
GWQ=0
GWM=""
GWLS=0
GWL=0
# checking batctl
if [ $ACTIVE_CHECK -eq 1 ]; then
batctl -v >/dev/null 2>&1 || { echo >&2 "batctl is required for Active-Checking, but it's not installed. Aborting."; exit 1; }
fi
# checking files and options
if [ "$(ls -l /var/run/hostapd-phy*.conf | wc -l)" -eq 0 ]; then
echo "Error: No Hostapd-Config-File was found" && exit 2
elif ! [ -f /etc/diag.sh ]; then
if [ $LED_STATUS -eq 1 ]; then
echo "Error: /etc/diag.sh not found." && exit 2
fi
fi
# init led-change-functions
if [ $LED_STATUS -eq 1 ]; then
. /etc/diag.sh
get_status_led
fi
HN=`cat /proc/sys/kernel/hostname`
if [ ${#HN} -gt 16 ]; then #cut hostname
HN="${HN:0:13}..."
fi
while [ `cat /proc/uptime | cut -d"." -f1` -lt $END ]; do
if [ -f "$PIDFILE"_"STOP" ]; then
echo "we're exiting now, because we're requested to."
rm $PIDFILE
exit 42
fi
case $MODE in
1) # check: does batman know any gateway?
GWQ=`cat /sys/kernel/debug/batman_adv/bat0/gateways | egrep ' \([\ 0-9]+\) ' | cut -d\( -f2 | cut -d\) -f1 | sort -n | tail -n1`
if ! [ "$GWQ" -eq "$GWQ" ] 2>/dev/null; then
GWQ=0
fi
echo -n "Debug: Gateway-quality is $GWQ"
if [ "$GWQ" -lt "$MINIMAL_ACCEPTABLE_GWQ" ]; then
echo " - this is not okay, we're offline"
OFFLINE=1
MODE=4
else
echo " - this seem fine"
OFFLINE=0
MODE=2
continue
fi
;;
2) #check: Selected gateway's last-seen
GWM=`cat /sys/kernel/debug/batman_adv/bat0/gateways | grep "^=>" | cut -d" " -f2`
if [ "$GWM" == "" ]; then
GWM="00:00:00:00:00"
fi
GWLS=`cat /sys/kernel/debug/batman_adv/bat0/originators | grep "^$GWM" | cut -d" " -f3-5 | cut -d"s" -f1 | cut -d"." -f1`
if ! [ "$GWLS" -eq "$GWLS" ] 2>/dev/null; then # there's no gateway found, skipping active ACTIVE_CHECK
MODE=4
OFFLINE=1
echo "Debug: No gateway in originators found - we're offline."
else
echo -n "Debug: Selected gateway last-seen $GWLS second(s) ago"
if [ $GWLS -gt $(( `cat /sys/devices/virtual/net/bat0/mesh/orig_interval` / 1000 * $OGM_INT_MULT)) ]; then
echo " - this is not okay, we seem to be offline"
OFFLINE=1
MODE=3
continue
else
echo " - this is fine, we're online"
OFFLINE=0
MODE=4
fi
fi
;;
3) #check: gateway is reachable
if [ $ACTIVE_CHECK -eq 1 ]; then
echo -n "Debug: Active-checking enabled, pinging selected gateway..."
GWL=`batctl ping -c$PING_CNT $GWM | grep "packet loss" | cut -d" " -f6 | cut -d"%" -f1`
if ! [ "$GWL" -eq "$GWL" ] 2>/dev/null; then
echo " ERROR: Ping-command failed"
OFFLINE=1
else
echo " done."
echo -n "Debug: Packetloss ($GWL %) "
if [ "$GWL" -gt "$MIN_PING_PERCENT" ]; then
OFFLINE=0
echo "fine."
else
OFFLINE=1
echo "to high."
fi
fi
MODE=4
else
echo "Debug: Active-checking disabled. Skipping"
MODE=4
fi
;;
4) #sleep
echo "Debug: Sleeping now for $SLEEP seconds"
sleep $SLEEP
echo ""
MODE=1
continue
;;
*)
echo "Error: Fatal error." && exit 2
;;
esac
LED_STATUS_SET=0 #Only change LED once if there are multiply phy's
for CLIENT_RADIO in $(uci show wireless | grep 'wireless.client_radio' | grep 'ifname' | grep '='\''client' | cut -d '=' -f1 | cut -d '.' -f2); do
FORCE_CHANGE=0
ISOFFLINE=0
SSID_BOOT=""
SSID_ONLINE=""
SSID_OFFLINE=""
NEED_CHANGE=0
HPID=""
PHY_DEV=""
IFNAME=""
SSID_ONLINE="$(uci get wireless.$CLIENT_RADIO.$SUB_CONF_ONLINE_SSID)"
SSID_BOOT="$(uci get wireless.$CLIENT_RADIO.$SUB_CONF_BOOT_SSID)"
IFNAME="$(uci get wireless.$CLIENT_RADIO.ifname)"
tmp="$(readlink -f /sys/devices/platform/*/net/$IFNAME/phy80211)"
[ -z "$tmp" ] && tmp=`echo "/$(cat /sys/devices/pci*/*/net/$IFNAME/phy80211/name)"`
PHY_DEV="${tmp##*/}"
unset tmp
if [ -z "$PHY_DEV" ]; then
echo "ERROR: Could not detect phy-dev for ifname '$IFNAME'. Skipping any SSID change."
continue
fi
HOSTAPD_CONF="/var/run/hostapd-$PHY_DEV.conf"
if ! [ -f $HOSTAPD_CONF ]; then
echo "ERROR: Hostapd conf missing (PHY_DEV=$PHY_DEV;IFNAME=$IFNAME). Skipping any SSID change."
continue
elif [ "$SSID_BOOT" == "" ]; then
echo "Error: UCI can't find Boot-SSID for interface '$IFNAME'. Append them with:"
echo "uci set wireless.$CLIENT_RADIO.$SUB_CONF_BOOT_SSID=x"
echo "uci commit wireless"
exit 2
elif [ "$SSID_ONLINE" == "" ]; then
echo "Error: UCI can't find SSID for interface '$IFNAME'. Append them with:"
echo "uci set wireless.$CLIENT_RADIO.$SUB_CONF_ONLINE_SSID=x"
echo "uci commit wireless"
exit 2
fi
# generate offline-SSID
if [ ${#SSID_ONLINE} -gt $(( 23 - ${#HN} )) ]; then #cut ssid to the maximum
SSID_OFFLINE="${SSID_0_ONLINE:0:$(( 20 - ${#HN} ))}..."
else
SSID_OFFLINE=$SSID_ONLINE
fi
SSID_OFFLINE="Offline-$SSID_OFFLINE-$HN"
if [ "$SSID_OFFLINE" == "" ]; then
echo "ERROR: generating of Offline-SSID failed for $IFNAME"
continue
fi
# get hostap-status
CURRENT_SSID=`cat $HOSTAPD_CONF | grep "^ssid=$SSID_ONLINE"`
[ -z "$CURRENT_SSID" ] && CURRENT_SSID=`cat $HOSTAPD_CONF | grep "^ssid=$SSID_OFFLINE"`
[ -z "$CURRENT_SSID" ] && CURRENT_SSID=`cat $HOSTAPD_CONF | grep "^ssid=$SSID_BOOT"`
CURRENT_SSID=${CURRENT_SSID:5} #rm ssid=
# echo "Debug: $IFNAME:"
# echo " SSID for boot : '$SSID_BOOT'"
# echo " SSID for online : '$SSID_ONLINE'"
# echo " SSID for offline: '$SSID_OFFLINE'"
# echo " Current SSID is : '$CURRENT_SSID'"
echo -n "Debug: Hostapd for $IFNAME is configured to SSID '$CURRENT_SSID', "
if [ "$CURRENT_SSID" == "$SSID_ONLINE" ]; then
ISOFFLINE=0
echo "our online-SSID"
elif [ "$CURRENT_SSID" == "$SSID_OFFLINE" ]; then
ISOFFLINE=1
echo "our offline-SSID"
elif [ "$CURRENT_SSID" == "$SSID_BOOT" ]; then
FORCE_CHANGE=1
echo "our boot-SSID"
else
FORCE_CHANGE=1
echo "WARNING: this seems wrong, we're changing it now."
fi
### Checking if update is needed ###
echo "Debug: OFFLINE=$OFFLINE ISOFFLINE=$ISOFFLINE FORCE_CHANGE=$FORCE_CHANGE"
if ! [ $FORCE_CHANGE -eq 1 ]; then
if [ $OFFLINE -eq 1 -a $ISOFFLINE -eq 0 ]; then
echo "Debug: Our check says, we're offline, need to change SSIDs"
NEED_CHANGE=1
elif [ $OFFLINE -eq 0 -a $ISOFFLINE -eq 1 ]; then
echo "Debug: Our check says, we're back online, need to change SSIDs"
NEED_CHANGE=1
fi
else
echo "Debug: Need to change SSIDs."
fi
### setting SSIDs and LED-Status (on the first interface only) ###
if [ $NEED_CHANGE -eq 1 -o $FORCE_CHANGE -eq 1 ]; then
if [ $OFFLINE -eq 1 ]; then
SSID="$SSID_OFFLINE"
if [ $LED_STATUS -eq 1 -a $LED_STATUS_SET -eq 0 ]; then
status_led_set_timer 50 1200
LED_STATUS_SET=1
fi
else
SSID="$SSID_ONLINE"
if [ $LED_STATUS -eq 1 -a $LED_STATUS_SET -eq 0 ]; then
status_led_on
LED_STATUS_SET=1
fi
fi
SSID="ssid=$SSID"
if ! [ -f $HOSTAPD_CONF ]; then
echo "ERROR: $HOSTAPD_CONF gone. Skipping the SSID-change."
continue
fi
sed -i -e "s/^ssid=$SSID_ONLINE/$SSID/" "$HOSTAPD_CONF"
sed -i -e "s/^ssid=$SSID_OFFLINE/$SSID/" "$HOSTAPD_CONF"
sed -i -e "s/^ssid=$SSID_BOOT/$SSID/" "$HOSTAPD_CONF"
HPID="$(ps | grep "$HOSTAPD_CONF" | grep -v "grep" | awk '{ print $1; }')"
if [ -z "$HPID" ]; then
echo "ERROR: Hostapd pid for $PHY_DEV could not be found. Skipping the SSID-change."
continue
fi
echo -n "Debug: HUPing hostapd PID $HPID now..."
kill -HUP $HPID
echo "done."
fi
done
done
[ -f "$PIDFILE" ] && rm "$PIDFILE" #cleanup