|
@@ -0,0 +1,222 @@
|
|
1
|
+#!/bin/bash
|
|
2
|
+#
|
|
3
|
+# .---. . .
|
|
4
|
+# | | |
|
|
5
|
+# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
|
|
6
|
+# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
|
|
7
|
+# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
|
|
8
|
+#
|
|
9
|
+# Freedom in the Cloud
|
|
10
|
+#
|
|
11
|
+# Used to enable or disable batman mesh protocol on wlanX
|
|
12
|
+#
|
|
13
|
+# License
|
|
14
|
+# =======
|
|
15
|
+#
|
|
16
|
+# Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
|
|
17
|
+#
|
|
18
|
+# This program is free software: you can redistribute it and/or modify
|
|
19
|
+# it under the terms of the GNU General Public License as published by
|
|
20
|
+# the Free Software Foundation, either version 3 of the License, or
|
|
21
|
+# (at your option) any later version.
|
|
22
|
+#
|
|
23
|
+# This program is distributed in the hope that it will be useful,
|
|
24
|
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
25
|
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
26
|
+# GNU General Public License for more details.
|
|
27
|
+#
|
|
28
|
+# You should have received a copy of the GNU General Public License
|
|
29
|
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
30
|
+
|
|
31
|
+PROJECT_NAME='freedombone'
|
|
32
|
+COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt
|
|
33
|
+
|
|
34
|
+if [[ $1 == "start" ]]; then
|
|
35
|
+ # install avahi
|
|
36
|
+ sed -i "s|#host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf
|
|
37
|
+ sed -i "s|host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf
|
|
38
|
+ sed -i "s|use-ipv4=.*|use-ipv4=yes|g" /etc/avahi/avahi-daemon.conf
|
|
39
|
+ sed -i "s|use-ipv6=.*|use-ipv6=no|g" /etc/avahi/avahi-daemon.conf
|
|
40
|
+ sed -i "s|#disallow-other-stacks=.*|disallow-other-stacks=yes|g" /etc/avahi/avahi-daemon.conf
|
|
41
|
+ sed -i "s|hosts:.*|hosts: files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf
|
|
42
|
+fi
|
|
43
|
+
|
|
44
|
+# Mesh definition
|
|
45
|
+ESSID=
|
|
46
|
+if ! grep -q "ESSID:" $COMPLETION_FILE; then
|
|
47
|
+ ESSID='mesh'
|
|
48
|
+else
|
|
49
|
+ ESSID=$(cat $COMPLETION_FILE | grep "ESSID:" | awk -F ':' '{print $2}')
|
|
50
|
+fi
|
|
51
|
+sed -i "s|ESSID:.*|ESSID:${ESSID}|g" $COMPLETION_FILE
|
|
52
|
+
|
|
53
|
+CELLID='any'
|
|
54
|
+
|
|
55
|
+CHANNEL=
|
|
56
|
+if ! grep -q "Wifi channel:" $COMPLETION_FILE; then
|
|
57
|
+ CHANNEL=2
|
|
58
|
+else
|
|
59
|
+ CHANNEL=$(cat $COMPLETION_FILE | grep "Wifi channel:" | awk -F ':' '{print $2}')
|
|
60
|
+fi
|
|
61
|
+sed -i "s|Wifi channel:.*|Wifi channel:${CHANNEL}|g" $COMPLETION_FILE
|
|
62
|
+
|
|
63
|
+ZERONET_PORT=15441
|
|
64
|
+IPFS_PORT=4001
|
|
65
|
+
|
|
66
|
+# Ethernet bridge definition (bridged to bat0)
|
|
67
|
+BRIDGE=br-mesh
|
|
68
|
+IFACE='wlan0'
|
|
69
|
+EIFACE=eth0
|
|
70
|
+
|
|
71
|
+if [[ $IFACE == "wlan0" ]]; then
|
|
72
|
+ if grep -q "wlan1" /proc/net/dev; then
|
|
73
|
+ IFACE=wlan1
|
|
74
|
+ fi
|
|
75
|
+fi
|
|
76
|
+if [[ $IFACE == "wlan0" ]]; then
|
|
77
|
+ if grep -q "wlan2" /proc/net/dev; then
|
|
78
|
+ IFACE=wlan2
|
|
79
|
+ fi
|
|
80
|
+fi
|
|
81
|
+if [[ $IFACE == "wlan0" ]]; then
|
|
82
|
+ if grep -q "wlan3" /proc/net/dev; then
|
|
83
|
+ IFACE=wlan3
|
|
84
|
+ fi
|
|
85
|
+fi
|
|
86
|
+
|
|
87
|
+if [ -e /etc/default/batctl ]; then
|
|
88
|
+ . /etc/default/batctl
|
|
89
|
+fi
|
|
90
|
+
|
|
91
|
+start() {
|
|
92
|
+ if [ -z "$IFACE" ] ; then
|
|
93
|
+ echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
|
|
94
|
+ return
|
|
95
|
+ fi
|
|
96
|
+ echo 'info: enabling batman-adv mesh network $ESSID on $IFACE'
|
|
97
|
+
|
|
98
|
+ systemctl stop network-manager
|
|
99
|
+ sleep 5
|
|
100
|
+
|
|
101
|
+ # remove an avahi service which isn't used
|
|
102
|
+ if [ -f /etc/avahi/services/udisks.service ]; then
|
|
103
|
+ sudo rm /etc/avahi/services/udisks.service
|
|
104
|
+ fi
|
|
105
|
+
|
|
106
|
+ # Might have to re-enable wifi
|
|
107
|
+ rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
|
|
108
|
+
|
|
109
|
+ ifconfig $IFACE down
|
|
110
|
+ ifconfig $IFACE mtu 1532
|
|
111
|
+ iwconfig $IFACE enc off
|
|
112
|
+ iwconfig $IFACE mode ad-hoc essid $ESSID channel $CHANNEL
|
|
113
|
+ sleep 1
|
|
114
|
+ iwconfig $IFACE ap $CELLID
|
|
115
|
+
|
|
116
|
+ modprobe batman-adv
|
|
117
|
+ batctl if add $IFACE
|
|
118
|
+ ifconfig $IFACE up
|
|
119
|
+ avahi-autoipd --force-bind --daemonize --wait $BRIDGE
|
|
120
|
+ avahi-autoipd --force-bind --daemonize --wait $IFACE
|
|
121
|
+ ifconfig bat0 up promisc
|
|
122
|
+
|
|
123
|
+ #Use persistent HWAddr
|
|
124
|
+ ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //")
|
|
125
|
+ if [ ! -f /var/lib/mesh-node/bat0 ]; then
|
|
126
|
+ mkdir /var/lib/mesh-node
|
|
127
|
+ echo "${ether_new}" > /var/lib/mesh-node/bat0
|
|
128
|
+ else
|
|
129
|
+ ether=$(cat /var/lib/mesh-node/bat0)
|
|
130
|
+ ifconfig bat0 hw ether ${ether}
|
|
131
|
+ fi
|
|
132
|
+
|
|
133
|
+ if [ "$EIFACE" ] ; then
|
|
134
|
+ brctl addbr $BRIDGE
|
|
135
|
+ brctl addif $BRIDGE bat0
|
|
136
|
+ brctl addif $BRIDGE $EIFACE
|
|
137
|
+ ifconfig bat0 0.0.0.0
|
|
138
|
+ ifconfig $EIFACE 0.0.0.0
|
|
139
|
+ ifconfig $EIFACE up promisc
|
|
140
|
+ ifconfig $BRIDGE up
|
|
141
|
+ fi
|
|
142
|
+
|
|
143
|
+ iptables -A INPUT -p tcp --dport 548 -j ACCEPT
|
|
144
|
+ iptables -A INPUT -p udp --dport 548 -j ACCEPT
|
|
145
|
+ iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
|
|
146
|
+ iptables -A INPUT -p udp --dport 5353 -j ACCEPT
|
|
147
|
+ iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
|
|
148
|
+ iptables -A INPUT -p udp --dport 5354 -j ACCEPT
|
|
149
|
+ iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
|
|
150
|
+ iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
|
|
151
|
+ iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
|
|
152
|
+
|
|
153
|
+ systemctl restart avahi-daemon
|
|
154
|
+}
|
|
155
|
+
|
|
156
|
+stop() {
|
|
157
|
+ if [ -z "$IFACE" ]; then
|
|
158
|
+ echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
|
|
159
|
+ return
|
|
160
|
+ fi
|
|
161
|
+ if [ "$EIFACE" ]; then
|
|
162
|
+ brctl delif $BRIDGE bat0
|
|
163
|
+ brctl delif $BRIDGE $EIFACE
|
|
164
|
+ ifconfig $BRIDGE down || true
|
|
165
|
+ brctl delbr $BRIDGE
|
|
166
|
+ ifconfig $EIFACE down -promisc
|
|
167
|
+ fi
|
|
168
|
+
|
|
169
|
+ avahi-autoipd -k $BRIDGE
|
|
170
|
+ avahi-autoipd -k $IFACE
|
|
171
|
+ ifconfig bat0 down -promisc
|
|
172
|
+
|
|
173
|
+ batctl if del $IFACE
|
|
174
|
+ rmmod batman-adv
|
|
175
|
+ ifconfig $IFACE mtu 1500
|
|
176
|
+ ifconfig $IFACE down
|
|
177
|
+ iwconfig $IFACE mode managed
|
|
178
|
+
|
|
179
|
+ iptables -D INPUT -p tcp --dport 548 -j ACCEPT
|
|
180
|
+ iptables -D INPUT -p udp --dport 548 -j ACCEPT
|
|
181
|
+ iptables -D INPUT -p tcp --dport 5353 -j ACCEPT
|
|
182
|
+ iptables -D INPUT -p udp --dport 5353 -j ACCEPT
|
|
183
|
+ iptables -D INPUT -p tcp --dport 5354 -j ACCEPT
|
|
184
|
+ iptables -D INPUT -p udp --dport 5354 -j ACCEPT
|
|
185
|
+ iptables -D INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
|
|
186
|
+ iptables -D INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
|
|
187
|
+ iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
|
|
188
|
+
|
|
189
|
+ systemctl restart network-manager
|
|
190
|
+}
|
|
191
|
+
|
|
192
|
+if ! grep -q "$IFACE" /proc/net/dev; then
|
|
193
|
+ echo 'Interface $IFACE was not found'
|
|
194
|
+ stop
|
|
195
|
+ exit 1
|
|
196
|
+fi
|
|
197
|
+
|
|
198
|
+case "$1" in
|
|
199
|
+ start|stop)
|
|
200
|
+ $1
|
|
201
|
+ ;;
|
|
202
|
+ restart)
|
|
203
|
+ stop
|
|
204
|
+ sleep 10
|
|
205
|
+ start
|
|
206
|
+ ;;
|
|
207
|
+ status)
|
|
208
|
+ batctl o
|
|
209
|
+ ;;
|
|
210
|
+ ping)
|
|
211
|
+ batctl ping $2
|
|
212
|
+ ;;
|
|
213
|
+ ls|list)
|
|
214
|
+ avahi-browse -atl
|
|
215
|
+ ;;
|
|
216
|
+ *)
|
|
217
|
+ echo "error: invalid parameter $1"
|
|
218
|
+ echo 'usage: $0 {start|stop|restart|status|ping|ls|list}'
|
|
219
|
+ exit 2
|
|
220
|
+ ;;
|
|
221
|
+esac
|
|
222
|
+exit 0
|