From e635453d9864904c76f693b7f832d40ab888d1e5 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Thu, 6 Oct 2016 19:09:13 +0100
Subject: [PATCH] Bridging the mesh to a wifi hotspot

---
 src/freedombone-mesh-batman | 112 ++++++++++++++++++++++++++++--------
 src/freedombone-utils-wifi  |  12 ----
 src/freedombone-wifi        |   2 +-
 3 files changed, 88 insertions(+), 38 deletions(-)

diff --git a/src/freedombone-mesh-batman b/src/freedombone-mesh-batman
index e5d7de3eb..fb2563a10 100755
--- a/src/freedombone-mesh-batman
+++ b/src/freedombone-mesh-batman
@@ -30,6 +30,35 @@
 
 PROJECT_NAME='freedombone'
 COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt
+HOTSPOT_PASSPHRASE='mesh'
+
+function count_wlan {
+    # counts the number of wlan devices
+    ctr=0
+
+    for i in $(seq 0 -1 10); do
+        if grep -q "wlan${i}" /proc/net/dev; then
+            ctr=$((ctr + 1))
+        fi
+    done
+    echo $ctr
+}
+
+function update_wifi_adaptors {
+    IFACE=
+    IFACE_SECONDARY=
+
+    for i in $(seq 10 -1 0); do
+        if grep -q "wlan${i}" /proc/net/dev; then
+            if [ ! $IFACE ]; then
+                IFACE="wlan${i}"
+            else
+                IFACE_SECONDARY="wlan${i}"
+                return
+            fi
+        fi
+    done
+}
 
 if [[ $1 == "start" ]]; then
     # install avahi
@@ -68,33 +97,22 @@ TAHOELAFS_PORT=50213
 
 # Ethernet bridge definition (bridged to bat0)
 BRIDGE=br-mesh
-IFACE='wlan0'
+BRIDGE_HOTSPOT=br-hotspot
+IFACE=
+IFACE_SECONDARY=
 EIFACE=eth0
+WLAN_ADAPTORS=$(count_wlan)
 
-if [[ $IFACE == "wlan0" ]]; then
-    if grep -q "wlan5" /proc/net/dev; then
-        IFACE=wlan5
-    fi
-fi
-if [[ $IFACE == "wlan0" ]]; then
-    if grep -q "wlan4" /proc/net/dev; then
-        IFACE=wlan4
-    fi
+if [ $WLAN_ADAPTORS -eq 0 ]; then
+    echo $'No wlan adaptors found'
+    exit 0
 fi
-if [[ $IFACE == "wlan0" ]]; then
-    if grep -q "wlan3" /proc/net/dev; then
-        IFACE=wlan3
-    fi
-fi
-if [[ $IFACE == "wlan0" ]]; then
-    if grep -q "wlan2" /proc/net/dev; then
-        IFACE=wlan2
-    fi
-fi
-if [[ $IFACE == "wlan0" ]]; then
-    if grep -q "wlan1" /proc/net/dev; then
-        IFACE=wlan1
-    fi
+
+update_wifi_adaptors
+
+if [ ! $IFACE ]; then
+    echo $'No wlan adaptor'
+    exit 0
 fi
 
 if [ -e /etc/default/batctl ]; then
@@ -126,6 +144,11 @@ function stop {
         brctl delbr $BRIDGE
         ifconfig $EIFACE down -promisc
     fi
+    if [ $IFACE_SECONDARY ]; then
+        brctl delif $BRIDGE_HOTSPOT bat0
+        ifconfig $BRIDGE_HOTSPOT down || true
+        brctl delbr $BRIDGE_HOTSPOT
+    fi
 
     avahi-autoipd -k $BRIDGE
     avahi-autoipd -k $IFACE
@@ -220,7 +243,6 @@ function start {
     modprobe batman-adv
     batctl if add $IFACE
     ifconfig $IFACE up
-    avahi-autoipd --force-bind --daemonize --wait $BRIDGE
     avahi-autoipd --force-bind --daemonize --wait $IFACE
     ifconfig bat0 up promisc
 
@@ -242,6 +264,46 @@ function start {
         ifconfig $EIFACE 0.0.0.0
         ifconfig $EIFACE up promisc
         ifconfig $BRIDGE up
+        avahi-autoipd --force-bind --daemonize --wait $BRIDGE
+    fi
+
+    if [ $IFACE_SECONDARY ]; then
+        if [[ $IFACE != $IFACE_SECONDARY ]]; then
+            if [ -d /etc/hostapd ]; then
+                # bridge between mesh and wifi hotspot for mobile
+                HOTSPOT_NAME=$"${WIFI_SSID}-hotspot"
+                ifconfig $IFACE_SECONDARY down
+                iwconfig $IFACE_SECONDARY mode managed essid $HOTSPOT_NAME channel ${CHANNEL}
+                iwconfig $IFACE_SECONDARY ap $CELLID
+
+                brctl addbr $BRIDGE_HOTSPOT
+                brctl addif $BRIDGE_HOTSPOT bat0
+                brctl addif $BRIDGE_HOTSPOT $IFACE_SECONDARY
+                ifconfig bat0 0.0.0.0
+                ifconfig $IFACE_SECONDARY 0.0.0.0
+
+                sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
+
+                echo "interface=$IFACE_SECONDARY" > /etc/hostapd/hostapd.conf
+                echo "bridge=${BRIDGE_HOTSPOT}" >> /etc/hostapd/hostapd.conf
+                echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
+                echo "country_code=UK" >> /etc/hostapd/hostapd.conf
+                echo "ssid=$HOTSPOT_NAME" >> /etc/hostapd/hostapd.conf
+                echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
+                echo "channel=${CHANNEL}" >> /etc/hostapd/hostapd.conf
+                echo 'wpa=2' >> /etc/hostapd/hostapd.conf
+                echo "wpa_passphrase=$HOTSPOT_PASSPHRASE" >> /etc/hostapd/hostapd.conf
+                echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
+                echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
+                echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
+                echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
+                echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
+
+                ifconfig $IFACE_SECONDARY up promisc
+                ifconfig $BRIDGE_HOTSPOT up
+                avahi-autoipd --force-bind --daemonize --wait $BRIDGE_HOTSPOT
+            fi
+        fi
     fi
 
     iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
diff --git a/src/freedombone-utils-wifi b/src/freedombone-utils-wifi
index 12871cbe8..aba9430be 100755
--- a/src/freedombone-utils-wifi
+++ b/src/freedombone-utils-wifi
@@ -39,18 +39,6 @@ WIFI_NETWORKS_FILE=~/${PROJECT_NAME}-wifi.cfg
 # repo for atheros AR9271 wifi driver
 ATHEROS_WIFI_REPO="https://github.com/qca/open-ath9k-htc-firmware.git"
 
-function count_wlan {
-    # counts the number of wlan devices
-    ctr=0
-
-    for i in $(seq 0 10); do
-        if grep -q "wlan${i}" /proc/net/dev; then
-            ctr=$((ctr + 1))
-        fi
-    done
-    echo $ctr
-}
-
 function setup_wifi {
     if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
         return
diff --git a/src/freedombone-wifi b/src/freedombone-wifi
index 0b07cf4a1..1de9ac72d 100755
--- a/src/freedombone-wifi
+++ b/src/freedombone-wifi
@@ -110,7 +110,7 @@ function hotspot_on {
     echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
     echo '' >> /etc/hostapd/hostapd.conf
     echo '## Shared Key Authentication ##'
-    echo 'auth_algs=1'
+    echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
     echo '' >> /etc/hostapd/hostapd.conf
     echo '## Accept all MAC address ###' >> /etc/hostapd/hostapd.conf
     echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
-- 
GitLab