-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathudhcpc.user
73 lines (61 loc) · 1.94 KB
/
udhcpc.user
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
#!/bin/sh
# This callback is activated on dhcp events from wan interface
for file in /etc/sudomesh/*; do
depends="$depends $file"
done
for file in $depends; do
if [ -f "$file" ]
then
. "$file"
else
logger "$file does not exist. meshrouting depends on it"
exit 2
fi
done
ip_calc=$(ipcalc.sh "$ip" "$subnet")
cidr_prefix=$(echo "$ip_calc" | grep -o 'PREFIX=\([0-9]\+\)' | sed -n -e 's/PREFIX=//p');
network=$(echo "$ip_calc" | grep -o 'NETWORK=\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)' | sed -n -e 's/NETWORK=//p');
clear_rules() {
ip rule del from "$network"/"$cidr_prefix" prio 13000 table main
ip rule del to "$network"/"$cidr_prefix" prio 13000 table main
}
setup_rules() {
# To keep from continuously adding the same rule, we delete first
clear_rules
ip rule add from "$network"/"$cidr_prefix" prio 13000 table main
ip rule add to "$network"/"$cidr_prefix" prio 13000 table main
ip route flush cache
}
check_mesh_routes() {
local try_count=0
local try_max=400
local try_sleep=5
local try_restart_interval=20
while [ "$try_count" -lt "$try_max" ]; do
# extract gateway ip from babeld managed routing table "public"
local exitnode_mesh_ip=$(ip route show table public | grep $TUN | grep via | awk -F ' ' '{print $3
}' | uniq | head -n1)
if [ -n "$exitnode_mesh_ip" ]; then
logger "found a mesh route to [$exitnode_mesh_ip] via [$TUN]."
break
fi
try_count=`expr $try_count + 1`
# restart every once in a while
if [ `expr $try_count % $try_restart_interval` -eq 0 ]; then
logger "no mesh routes available yet via [$TUN] on try [$try_count]: restarting meshrouting..."
/etc/init.d/meshrouting restart
else
logger "no mesh routes available yet via [$TUN] on try [$try_count]: checking again in [$try_sleep]s..."
sleep $try_sleep
fi
done
}
case "$1" in
deconfig)
clear_rules
;;
renew|bound)
setup_rules
check_mesh_routes
;;
esac