Browse Source

Get secondary wifi adapter working on mesh

Bob Mottram 7 years ago
parent
commit
85729ed2f7
2 changed files with 107 additions and 82 deletions
  1. 103
    82
      src/freedombone-mesh-batman
  2. 4
    0
      src/freedombone-utils-wifi

+ 103
- 82
src/freedombone-mesh-batman View File

@@ -30,7 +30,9 @@
30 30
 
31 31
 PROJECT_NAME='freedombone'
32 32
 COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt
33
-HOTSPOT_PASSPHRASE='mesh'
33
+
34
+# hotspot passphrase must be 5 characters or longer
35
+HOTSPOT_PASSPHRASE="${PROJECT_NAME}"
34 36
 
35 37
 source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-wifi
36 38
 
@@ -114,16 +116,14 @@ function stop {
114 116
     fi
115 117
     if [ "$EIFACE" ]; then
116 118
         brctl delif $BRIDGE bat0
117
-        brctl delif $BRIDGE $EIFACE
118 119
         ifconfig $BRIDGE down || true
120
+        ethernet_connected=$(cat /sys/class/net/$EIFACE/carrier)
121
+        if [[ "$ethernet_connected" != "0" ]]; then
122
+            systemctl stop hostapd
123
+            brctl delif $BRIDGE $EIFACE
124
+            ifconfig $EIFACE down -promisc
125
+        fi
119 126
         brctl delbr $BRIDGE
120
-        ifconfig $EIFACE down -promisc
121
-    fi
122
-    if [ $IFACE_SECONDARY ]; then
123
-        systemctl stop hostapd
124
-        brctl delif $BRIDGE_HOTSPOT bat0
125
-        ifconfig $BRIDGE_HOTSPOT down || true
126
-        brctl delbr $BRIDGE_HOTSPOT
127 127
     fi
128 128
 
129 129
     avahi-autoipd -k $BRIDGE
@@ -131,11 +131,21 @@ function stop {
131 131
     ifconfig bat0 down -promisc
132 132
 
133 133
     batctl if del $IFACE
134
-    rmmod batman-adv
135 134
     ifconfig $IFACE mtu 1500
136 135
     ifconfig $IFACE down
137 136
     iwconfig $IFACE mode managed
138 137
 
138
+    if [ $IFACE_SECONDARY ]; then
139
+        systemctl stop hostapd
140
+        systemctl disable hostapd
141
+        batctl if del $IFACE_SECONDARY
142
+        ifconfig $IFACE_SECONDARY mtu 1500
143
+        ifconfig $IFACE_SECONDARY down
144
+        iwconfig $IFACE_SECONDARY mode managed
145
+    fi
146
+
147
+    rmmod batman-adv
148
+
139 149
     iptables -D INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT
140 150
     iptables -D INPUT -p udp --dport $TRACKER_PORT -j ACCEPT
141 151
     iptables -D INPUT -p tcp --dport 80 -j ACCEPT
@@ -205,6 +215,37 @@ function assign_peer_address {
205 215
     done
206 216
 }
207 217
 
218
+function add_wifi_interface {
219
+    ifname=$1
220
+    ifssid=$WIFI_SSID
221
+    if [ $2 ]; then
222
+        ifssid=$2
223
+    fi
224
+    ifmode=ad-hoc
225
+    if [ $3 ]; then
226
+        ifmode=$3
227
+    fi
228
+    ifchannel=$CHANNEL
229
+    if [ $4 ]; then
230
+        ifchannel=$4
231
+    fi
232
+
233
+    ifconfig $ifname down
234
+    ifconfig $ifname mtu 1532
235
+    peermac=$(assign_peer_address)
236
+    if [ ! $peermac ]; then
237
+        echo $"Unable to obtain MAC address for $peermac on $ifname"
238
+        return
239
+    fi
240
+    ifconfig $ifname hw ether $peermac
241
+    echo $"$ifname assigned MAC address $peermac"
242
+    iwconfig $ifname enc off
243
+    iwconfig $ifname mode $ifmode essid $ifssid channel $ifchannel
244
+
245
+    batctl if add $ifname
246
+    ifconfig $ifname up
247
+}
248
+
208 249
 function start {
209 250
     if [ -z "$IFACE" ] ; then
210 251
         echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
@@ -225,87 +266,67 @@ function start {
225 266
     # Might have to re-enable wifi
226 267
     rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
227 268
 
228
-    ifconfig $IFACE down
229
-    ifconfig $IFACE mtu 1532
230
-    peermac=$(assign_peer_address)
231
-    if [ ! $peermac ]; then
232
-        echo $"Unable to obtain MAC address for $peermac"
233
-        return
269
+    secondary_wifi_available=
270
+    if [ $IFACE_SECONDARY ]; then
271
+        if [[ $IFACE != $IFACE_SECONDARY ]]; then
272
+            if [ -d /etc/hostapd ]; then
273
+                if [ ${#HOTSPOT_PASSPHRASE} -gt 4 ]; then
274
+                    secondary_wifi_available=1
275
+                else
276
+                    echo $'Hotspot passphrase is too short'
277
+                fi
278
+            fi
279
+        fi
234 280
     fi
235
-    ifconfig $IFACE hw ether $peermac
236
-    echo $"$IFACE assigned MAC address $peermac"
237
-    iwconfig $IFACE enc off
238
-    iwconfig $IFACE mode ad-hoc essid $WIFI_SSID channel $CHANNEL
239 281
 
240 282
     modprobe batman-adv
241
-    batctl if add $IFACE
242
-    ifconfig $IFACE up
283
+
284
+    add_wifi_interface $IFACE $WIFI_SSID ad-hoc $CHANNEL
243 285
     avahi-autoipd --force-bind --daemonize --wait $IFACE
286
+
287
+    # NOTE: Don't connect the secondary wifi device. hostapd will handle that by itself
288
+
244 289
     ifconfig bat0 up promisc
245 290
 
291
+    brctl addbr $BRIDGE
292
+    brctl addif $BRIDGE bat0
293
+    ifconfig bat0 0.0.0.0
246 294
     if [ "$EIFACE" ] ; then
247
-        echo $'Trying ethernet bridge to the internet'
248
-        brctl addbr $BRIDGE
249
-        brctl addif $BRIDGE bat0
250
-        brctl addif $BRIDGE $EIFACE
251
-        ifconfig bat0 0.0.0.0
252
-        ifconfig $EIFACE 0.0.0.0
253
-        ifconfig $EIFACE up promisc
254
-        ifconfig $BRIDGE up
255
-        dhclient $BRIDGE
256
-        echo $'End of ethernet bridge'
257
-    fi
258
-
259
-    hotspot_enabled=
260
-    if [ $IFACE_SECONDARY ]; then
261
-        if [[ $IFACE != $IFACE_SECONDARY ]]; then
262
-            if [ -d /etc/hostapd ]; then
263
-                # bridge between mesh and wifi hotspot for mobile
264
-                HOTSPOT_NAME=$"${WIFI_SSID}-hotspot"
265
-                ifconfig $IFACE_SECONDARY down
266
-                ifconfig $IFACE_SECONDARY mtu 1500
267
-                CELLID=$(assign_peer_address)
268
-                if [ $CELLID ]; then
269
-                    ifconfig $IFACE_SECONDARY hw ether $CELLID
270
-                    echo $"Secondary wifi adaptor $IFACE_SECONDARY assigned to MAC address $CELLID"
271
-                    iwconfig $IFACE_SECONDARY enc open
272
-                    iwconfig $IFACE_SECONDARY mode managed essid $HOTSPOT_NAME channel ${HOTSPOT_CHANNEL}
273
-                    iwconfig $IFACE_SECONDARY ap $CELLID
274
-                    iwconfig $IFACE ap $CELLID
275
-
276
-                    brctl addbr $BRIDGE_HOTSPOT
277
-                    brctl addif $BRIDGE_HOTSPOT bat0
278
-                    brctl addif $BRIDGE_HOTSPOT $IFACE_SECONDARY
279
-                    ifconfig bat0 0.0.0.0
280
-                    ifconfig $IFACE_SECONDARY 0.0.0.0
281
-
282
-                    sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
283
-
284
-                    echo "interface=${IFACE_SECONDARY}" > /etc/hostapd/hostapd.conf
285
-                    echo "bridge=${BRIDGE_HOTSPOT}" >> /etc/hostapd/hostapd.conf
286
-                    echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
287
-                    echo "country_code=UK" >> /etc/hostapd/hostapd.conf
288
-                    echo "ssid=$HOTSPOT_NAME" >> /etc/hostapd/hostapd.conf
289
-                    echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
290
-                    echo "channel=${HOTSPOT_CHANNEL}" >> /etc/hostapd/hostapd.conf
291
-                    echo 'wpa=2' >> /etc/hostapd/hostapd.conf
292
-                    echo "wpa_passphrase=$HOTSPOT_PASSPHRASE" >> /etc/hostapd/hostapd.conf
293
-                    echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
294
-                    echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
295
-                    echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
296
-                    echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
297
-                    echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
298
-
299
-                    ifconfig $BRIDGE_HOTSPOT up
300
-                    avahi-autoipd --force-bind --daemonize --wait $BRIDGE_HOTSPOT
301
-                    ifconfig $IFACE_SECONDARY up promisc
302
-                    #ifconfig $IFACE_SECONDARY auto-dhcp start
303
-                    systemctl start hostapd
304
-                    hotspot_enabled=1
305
-                fi
306
-            fi
295
+        ethernet_connected=$(cat /sys/class/net/$EIFACE/carrier)
296
+        if [[ "$ethernet_connected" != "0" ]]; then
297
+            echo $'Trying ethernet bridge to the internet'
298
+            brctl addif $BRIDGE $EIFACE
299
+            ifconfig $EIFACE 0.0.0.0
300
+            ifconfig $EIFACE up promisc
301
+            echo $'End of ethernet bridge'
302
+        else
303
+            echo $"$EIFACE is not connected"
307 304
         fi
308 305
     fi
306
+    ifconfig $BRIDGE up
307
+    dhclient $BRIDGE
308
+
309
+    if [ $secondary_wifi_available ]; then
310
+        sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
311
+
312
+        echo "interface=${IFACE_SECONDARY}" > /etc/hostapd/hostapd.conf
313
+        echo "bridge=${BRIDGE}" >> /etc/hostapd/hostapd.conf
314
+        echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
315
+        echo "country_code=UK" >> /etc/hostapd/hostapd.conf
316
+        echo "ssid=${WIFI_SSID}-${HOSTNAME}" >> /etc/hostapd/hostapd.conf
317
+        echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
318
+        echo "channel=${HOTSPOT_CHANNEL}" >> /etc/hostapd/hostapd.conf
319
+        echo 'wpa=2' >> /etc/hostapd/hostapd.conf
320
+        echo "wpa_passphrase=$HOTSPOT_PASSPHRASE" >> /etc/hostapd/hostapd.conf
321
+        echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
322
+        echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
323
+        echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
324
+        echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
325
+        echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
326
+
327
+        systemctl enable hostapd
328
+        systemctl restart hostapd
329
+    fi
309 330
 
310 331
     iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
311 332
     iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT

+ 4
- 0
src/freedombone-utils-wifi View File

@@ -216,6 +216,10 @@ function update_wifi_adaptors {
216 216
     IFACE_SECONDARY=
217 217
 
218 218
     for i in $(seq 10 -1 0); do
219
+        ifdown --force wlan${i}
220
+    done
221
+
222
+    for i in $(seq 10 -1 0); do
219 223
         if grep -q "wlan${i}" /proc/net/dev; then
220 224
             if [ ! $IFACE ]; then
221 225
                 IFACE="wlan${i}"