Przeglądaj źródła

Assign unique peer address

Bob Mottram 8 lat temu
rodzic
commit
7702692aec
1 zmienionych plików z 168 dodań i 151 usunięć
  1. 168
    151
      src/freedombone-mesh-batman

+ 168
- 151
src/freedombone-mesh-batman Wyświetl plik

@@ -32,21 +32,21 @@ PROJECT_NAME='freedombone'
32 32
 COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt
33 33
 
34 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
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 42
 fi
43 43
 
44 44
 # Mesh definition
45 45
 WIFI_SSID=
46 46
 if ! grep -q "WIFI_SSID:" $COMPLETION_FILE; then
47
-	WIFI_SSID='mesh'
47
+    WIFI_SSID='mesh'
48 48
 else
49
-	WIFI_SSID=$(cat $COMPLETION_FILE | grep "WIFI_SSID:" | awk -F ':' '{print $2}')
49
+    WIFI_SSID=$(cat $COMPLETION_FILE | grep "WIFI_SSID:" | awk -F ':' '{print $2}')
50 50
 fi
51 51
 sed -i "s|WIFI_SSID:.*|WIFI_SSID:${WIFI_SSID}|g" $COMPLETION_FILE
52 52
 
@@ -54,9 +54,9 @@ CELLID='any'
54 54
 
55 55
 CHANNEL=
56 56
 if ! grep -q "Wifi channel:" $COMPLETION_FILE; then
57
-	CHANNEL=2
57
+    CHANNEL=2
58 58
 else
59
-	CHANNEL=$(cat $COMPLETION_FILE | grep "Wifi channel:" | awk -F ':' '{print $2}')
59
+    CHANNEL=$(cat $COMPLETION_FILE | grep "Wifi channel:" | awk -F ':' '{print $2}')
60 60
 fi
61 61
 sed -i "s|Wifi channel:.*|Wifi channel:${CHANNEL}|g" $COMPLETION_FILE
62 62
 
@@ -69,170 +69,187 @@ IFACE='wlan0'
69 69
 EIFACE=eth0
70 70
 
71 71
 if [[ $IFACE == "wlan0" ]]; then
72
-	if grep -q "wlan1" /proc/net/dev; then
73
-		IFACE=wlan1
74
-	fi
72
+    if grep -q "wlan1" /proc/net/dev; then
73
+        IFACE=wlan1
74
+    fi
75 75
 fi
76 76
 if [[ $IFACE == "wlan0" ]]; then
77
-	if grep -q "wlan2" /proc/net/dev; then
78
-		IFACE=wlan2
79
-	fi
77
+    if grep -q "wlan2" /proc/net/dev; then
78
+        IFACE=wlan2
79
+    fi
80 80
 fi
81 81
 if [[ $IFACE == "wlan0" ]]; then
82
-	if grep -q "wlan3" /proc/net/dev; then
83
-		IFACE=wlan3
84
-	fi
82
+    if grep -q "wlan3" /proc/net/dev; then
83
+        IFACE=wlan3
84
+    fi
85 85
 fi
86 86
 
87 87
 if [ -e /etc/default/batctl ]; then
88
-	. /etc/default/batctl
88
+    . /etc/default/batctl
89 89
 fi
90 90
 
91 91
 function status {
92
-	batctl o
92
+    batctl o
93 93
 }
94 94
 
95 95
 function stop {
96
-	if [ -z "$IFACE" ]; then
97
-		echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
98
-		return
99
-	fi
100
-	if [ "$EIFACE" ]; then
101
-		brctl delif $BRIDGE bat0
102
-		brctl delif $BRIDGE $EIFACE
103
-		ifconfig $BRIDGE down || true
104
-		brctl delbr $BRIDGE
105
-		ifconfig $EIFACE down -promisc
106
-	fi
107
-
108
-	avahi-autoipd -k $BRIDGE
109
-	avahi-autoipd -k $IFACE
110
-	ifconfig bat0 down -promisc
111
-
112
-	batctl if del $IFACE
113
-	rmmod batman-adv
114
-	ifconfig $IFACE mtu 1500
115
-	ifconfig $IFACE down
116
-	iwconfig $IFACE mode managed
117
-
118
-	iptables -D INPUT -p tcp --dport 548 -j ACCEPT
119
-	iptables -D INPUT -p udp --dport 548 -j ACCEPT
120
-	iptables -D INPUT -p tcp --dport 5353 -j ACCEPT
121
-	iptables -D INPUT -p udp --dport 5353 -j ACCEPT
122
-	iptables -D INPUT -p tcp --dport 5354 -j ACCEPT
123
-	iptables -D INPUT -p udp --dport 5354 -j ACCEPT
124
-	iptables -D INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
125
-	iptables -D INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
126
-	iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
127
-
128
-	systemctl restart network-manager
96
+    if [ -z "$IFACE" ]; then
97
+        echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
98
+        return
99
+    fi
100
+    if [ "$EIFACE" ]; then
101
+        brctl delif $BRIDGE bat0
102
+        brctl delif $BRIDGE $EIFACE
103
+        ifconfig $BRIDGE down || true
104
+        brctl delbr $BRIDGE
105
+        ifconfig $EIFACE down -promisc
106
+    fi
107
+
108
+    avahi-autoipd -k $BRIDGE
109
+    avahi-autoipd -k $IFACE
110
+    ifconfig bat0 down -promisc
111
+
112
+    batctl if del $IFACE
113
+    rmmod batman-adv
114
+    ifconfig $IFACE mtu 1500
115
+    ifconfig $IFACE down
116
+    iwconfig $IFACE mode managed
117
+
118
+    iptables -D INPUT -p tcp --dport 548 -j ACCEPT
119
+    iptables -D INPUT -p udp --dport 548 -j ACCEPT
120
+    iptables -D INPUT -p tcp --dport 5353 -j ACCEPT
121
+    iptables -D INPUT -p udp --dport 5353 -j ACCEPT
122
+    iptables -D INPUT -p tcp --dport 5354 -j ACCEPT
123
+    iptables -D INPUT -p udp --dport 5354 -j ACCEPT
124
+    iptables -D INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
125
+    iptables -D INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
126
+    iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
127
+
128
+    systemctl restart network-manager
129 129
 }
130 130
 
131 131
 function verify {
132
-	tempfile="$(mktemp)"
133
-	batctl o > $tempfile
134
-	if grep -q "disabled" $tempfile; then
135
-		echo $'B.A.T.M.A.N. not enabled'
136
-		rm $tempfile
137
-		stop
138
-		exit 726835
139
-	fi
140
-	echo $'B.A.T.M.A.N. is running'
141
-	rm $tempfile
132
+    tempfile="$(mktemp)"
133
+    batctl o > $tempfile
134
+    if grep -q "disabled" $tempfile; then
135
+        echo $'B.A.T.M.A.N. not enabled'
136
+        rm $tempfile
137
+        stop
138
+        exit 726835
139
+    fi
140
+    echo $'B.A.T.M.A.N. is running'
141
+    rm $tempfile
142
+}
143
+
144
+function assign_peer_address {
145
+    for i in {1..6}; do
146
+        number=$RANDOM
147
+        let "number %= 255"
148
+        octet=$(echo "obase=16;$number" | bc)
149
+        if [ ${#octet} -lt 2 ]; then
150
+            octet="0${octet}"
151
+        fi
152
+        if [ $i -gt 1 ]; then
153
+            echo -n ":"
154
+        fi
155
+        echo -n "${octet}"
156
+    done
157
+    echo ''
142 158
 }
143 159
 
144 160
 function start {
145
-	if [ -z "$IFACE" ] ; then
146
-		echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
147
-		exit 723657
148
-	fi
149
-	echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE"
150
-
151
-	systemctl stop network-manager
152
-	sleep 5
153
-
154
-	# remove an avahi service which isn't used
155
-	if [ -f /etc/avahi/services/udisks.service ]; then
156
-		sudo rm /etc/avahi/services/udisks.service
157
-	fi
158
-
159
-	# Might have to re-enable wifi
160
-	rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
161
-
162
-	ifconfig $IFACE down
163
-	ifconfig $IFACE mtu 1532
164
-	iwconfig $IFACE enc off
165
-	iwconfig $IFACE mode ad-hoc essid $WIFI_SSID channel $CHANNEL
166
-	sleep 1
167
-	iwconfig $IFACE ap $CELLID
168
-
169
-	modprobe batman-adv
170
-	batctl if add $IFACE
171
-	ifconfig $IFACE up
172
-	avahi-autoipd --force-bind --daemonize --wait $BRIDGE
173
-	avahi-autoipd --force-bind --daemonize --wait $IFACE
174
-	ifconfig bat0 up promisc
175
-
176
-	#Use persistent HWAddr
177
-	ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //")
178
-	if [ ! -f /var/lib/mesh-node/bat0 ]; then
179
-		mkdir /var/lib/mesh-node
180
-		echo "${ether_new}" > /var/lib/mesh-node/bat0
181
-	else
182
-		ether=$(cat /var/lib/mesh-node/bat0)
183
-		ifconfig bat0 hw ether ${ether}
184
-	fi
185
-
186
-	if [ "$EIFACE" ] ; then
187
-		brctl addbr $BRIDGE
188
-		brctl addif $BRIDGE bat0
189
-		brctl addif $BRIDGE $EIFACE
190
-		ifconfig bat0 0.0.0.0
191
-		ifconfig $EIFACE 0.0.0.0
192
-		ifconfig $EIFACE up promisc
193
-		ifconfig $BRIDGE up
194
-	fi
195
-
196
-	iptables -A INPUT -p tcp --dport 548 -j ACCEPT
197
-	iptables -A INPUT -p udp --dport 548 -j ACCEPT
198
-	iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
199
-	iptables -A INPUT -p udp --dport 5353 -j ACCEPT
200
-	iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
201
-	iptables -A INPUT -p udp --dport 5354 -j ACCEPT
202
-	iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
203
-	iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
204
-	iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
205
-
206
-	systemctl restart avahi-daemon
207
-
208
-	verify
161
+    if [ -z "$IFACE" ] ; then
162
+        echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
163
+        exit 723657
164
+    fi
165
+    echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE"
166
+
167
+    systemctl stop network-manager
168
+    sleep 5
169
+
170
+    # remove an avahi service which isn't used
171
+    if [ -f /etc/avahi/services/udisks.service ]; then
172
+        sudo rm /etc/avahi/services/udisks.service
173
+    fi
174
+
175
+    # Might have to re-enable wifi
176
+    rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
177
+
178
+    ifconfig $IFACE down
179
+    ifconfig $IFACE mtu 1532
180
+    ifconfig $IFACE hw ether $(assign_peer_address)
181
+    iwconfig $IFACE enc off
182
+    iwconfig $IFACE mode ad-hoc essid $WIFI_SSID channel $CHANNEL
183
+    sleep 1
184
+    iwconfig $IFACE ap $CELLID
185
+
186
+    modprobe batman-adv
187
+    batctl if add $IFACE
188
+    ifconfig $IFACE up
189
+    avahi-autoipd --force-bind --daemonize --wait $BRIDGE
190
+    avahi-autoipd --force-bind --daemonize --wait $IFACE
191
+    ifconfig bat0 up promisc
192
+
193
+    #Use persistent HWAddr
194
+    ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //")
195
+    if [ ! -f /var/lib/mesh-node/bat0 ]; then
196
+        mkdir /var/lib/mesh-node
197
+        echo "${ether_new}" > /var/lib/mesh-node/bat0
198
+    else
199
+        ether=$(cat /var/lib/mesh-node/bat0)
200
+        ifconfig bat0 hw ether ${ether}
201
+    fi
202
+
203
+    if [ "$EIFACE" ] ; then
204
+        brctl addbr $BRIDGE
205
+        brctl addif $BRIDGE bat0
206
+        brctl addif $BRIDGE $EIFACE
207
+        ifconfig bat0 0.0.0.0
208
+        ifconfig $EIFACE 0.0.0.0
209
+        ifconfig $EIFACE up promisc
210
+        ifconfig $BRIDGE up
211
+    fi
212
+
213
+    iptables -A INPUT -p tcp --dport 548 -j ACCEPT
214
+    iptables -A INPUT -p udp --dport 548 -j ACCEPT
215
+    iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
216
+    iptables -A INPUT -p udp --dport 5353 -j ACCEPT
217
+    iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
218
+    iptables -A INPUT -p udp --dport 5354 -j ACCEPT
219
+    iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
220
+    iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
221
+    iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
222
+
223
+    systemctl restart avahi-daemon
224
+
225
+    verify
209 226
 }
210 227
 
211 228
 if ! grep -q "$IFACE" /proc/net/dev; then
212
-	echo 'Interface $IFACE was not found'
213
-	stop
214
-	exit 1
229
+    echo 'Interface $IFACE was not found'
230
+    stop
231
+    exit 1
215 232
 fi
216 233
 
217 234
 case "$1" in
218
-	start|stop|status)
219
-		$1
220
-		;;
221
-	restart)
222
-		stop
223
-		sleep 10
224
-		start
225
-		;;
226
-	ping)
227
-		batctl ping $2
228
-		;;
229
-	ls|list)
230
-		avahi-browse -atl
231
-		;;
232
-	*)
233
-		echo "error: invalid parameter $1"
234
-		echo 'usage: $0 {start|stop|restart|status|ping|ls|list}'
235
-		exit 2
236
-		;;
235
+    start|stop|status)
236
+        $1
237
+        ;;
238
+    restart)
239
+        stop
240
+        sleep 10
241
+        start
242
+        ;;
243
+    ping)
244
+        batctl ping $2
245
+        ;;
246
+    ls|list)
247
+        avahi-browse -atl
248
+        ;;
249
+    *)
250
+        echo "error: invalid parameter $1"
251
+        echo 'usage: $0 {start|stop|restart|status|ping|ls|list}'
252
+        exit 2
253
+        ;;
237 254
 esac
238 255
 exit 0