Przeglądaj źródła

Get secondary wifi adapter working on mesh

Bob Mottram 7 lat temu
rodzic
commit
85729ed2f7
2 zmienionych plików z 107 dodań i 82 usunięć
  1. 103
    82
      src/freedombone-mesh-batman
  2. 4
    0
      src/freedombone-utils-wifi

+ 103
- 82
src/freedombone-mesh-batman Wyświetl plik

30
 
30
 
31
 PROJECT_NAME='freedombone'
31
 PROJECT_NAME='freedombone'
32
 COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt
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
 source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-wifi
37
 source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-wifi
36
 
38
 
114
     fi
116
     fi
115
     if [ "$EIFACE" ]; then
117
     if [ "$EIFACE" ]; then
116
         brctl delif $BRIDGE bat0
118
         brctl delif $BRIDGE bat0
117
-        brctl delif $BRIDGE $EIFACE
118
         ifconfig $BRIDGE down || true
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
         brctl delbr $BRIDGE
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
     fi
127
     fi
128
 
128
 
129
     avahi-autoipd -k $BRIDGE
129
     avahi-autoipd -k $BRIDGE
131
     ifconfig bat0 down -promisc
131
     ifconfig bat0 down -promisc
132
 
132
 
133
     batctl if del $IFACE
133
     batctl if del $IFACE
134
-    rmmod batman-adv
135
     ifconfig $IFACE mtu 1500
134
     ifconfig $IFACE mtu 1500
136
     ifconfig $IFACE down
135
     ifconfig $IFACE down
137
     iwconfig $IFACE mode managed
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
     iptables -D INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT
149
     iptables -D INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT
140
     iptables -D INPUT -p udp --dport $TRACKER_PORT -j ACCEPT
150
     iptables -D INPUT -p udp --dport $TRACKER_PORT -j ACCEPT
141
     iptables -D INPUT -p tcp --dport 80 -j ACCEPT
151
     iptables -D INPUT -p tcp --dport 80 -j ACCEPT
205
     done
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
 function start {
249
 function start {
209
     if [ -z "$IFACE" ] ; then
250
     if [ -z "$IFACE" ] ; then
210
         echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
251
         echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
225
     # Might have to re-enable wifi
266
     # Might have to re-enable wifi
226
     rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
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
     fi
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
     modprobe batman-adv
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
     avahi-autoipd --force-bind --daemonize --wait $IFACE
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
     ifconfig bat0 up promisc
289
     ifconfig bat0 up promisc
245
 
290
 
291
+    brctl addbr $BRIDGE
292
+    brctl addif $BRIDGE bat0
293
+    ifconfig bat0 0.0.0.0
246
     if [ "$EIFACE" ] ; then
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
         fi
304
         fi
308
     fi
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
     iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
331
     iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
311
     iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
332
     iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT

+ 4
- 0
src/freedombone-utils-wifi Wyświetl plik

216
     IFACE_SECONDARY=
216
     IFACE_SECONDARY=
217
 
217
 
218
     for i in $(seq 10 -1 0); do
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
         if grep -q "wlan${i}" /proc/net/dev; then
223
         if grep -q "wlan${i}" /proc/net/dev; then
220
             if [ ! $IFACE ]; then
224
             if [ ! $IFACE ]; then
221
                 IFACE="wlan${i}"
225
                 IFACE="wlan${i}"