[RFC,3/3] Load Wifi Settings

Submitted by Christian Dresel on Aug. 20, 2017, 8:29 a.m.

Details

Message ID 1503217779-24181-3-git-send-email-fff@chrisi01.de
State Not Applicable
Headers show

Commit Message

Christian Dresel Aug. 20, 2017, 8:29 a.m.
If we have internet we connect directly to the keyxchangev2 and load
the settings
if we haven't internet we try to connect to a hidden AP as station and
load the settings from the keyxchangev2
after set the new wireless settings we copy back the 60-fff-wireless
uci default and reboot the node

Signed-off-by: Christian Dresel <fff@chrisi01.de>
---
 .../fff/fff-vpn-select/files/usr/sbin/vpn-select   | 83 +++++++++++++++++++++-
 .../fff-wireless/files/lib/functions/fff/wireless  | 31 ++++++++
 2 files changed, 112 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
index c3d003f..e0904d6 100755
--- a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
+++ b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
@@ -1,7 +1,41 @@ 
 #!/bin/sh
 
+. /lib/functions/fff/wireless
+
 test -f /tmp/started || exit
 
+set_wifi() {
+wget -T15 "http://keyserver.freifunk-franken.de/${project}/wireless.php?mac=$mac" -O /tmp/wireless_${project}_output
+$NEWSETTINGS=$(cat /tmp/wireless_${project}_output)
+#check the settings the same as set if not set it new and reboot the Node if it the same do nothing
+#we get the settings in this format from keyxchangev2: ESSID_AP;ESSID_MESH;BSSID_MESH
+ESSID_AP_AKT=$(cat /etc/community.cfg  | grep ESSID_AP | cut -d "=" -f 2)
+ESSID_MESH_AKT=$(cat /etc/community.cfg  | grep ESSID_MESH | cut -d "=" -f 2)
+BSSID_MESH_AKT=$(cat /etc/community.cfg  | grep BSSID_MESH | cut -d "=" -f 2)
+CHECKSAME="$ESSID_AP_AKT;$ESSID_MESH_AKT;$BSSID_MESH_AKT"
+if [ "$CHECKSAME" == "$NEWSETTINGS" ]; then
+	echo "We have same settings and do nothing"
+else
+	#here we must write the config file new and reboot after
+	ESSID_AP_NEW=$(cat /tmp/wireless_${project}_output | cut -d ";" -f 0)
+	ESSID_MESH_NEW=$(cat /tmp/wireless_${project}_output | cut -d ";" -f 1)
+	BSSID_MESH_NEW=$(cat /tmp/wireless_${project}_output | cut -d ";" -f 2)
+	rm /etc/community.cfg
+	touch /etc/community.cfg
+	echo 'BATMAN_CHANNEL=1' >> /etc/community.cfg
+	echo 'BATMAN_CHANNEL_5GHZ=36' >> /etc/community.cfg
+	echo 'ESSID_AP=$ESSID_AP_NEW' >> /etc/community.cfg
+	echo 'ESSID_MESH=$ESSID_MESH_NEW' >> /etc/community.cfg
+	echo 'BSSID_MESH=$BSSID_MESH_NEW' >> /etc/community.cfg
+	echo 'VPN_PROJECT=fff' >> /etc/community.cfg
+	echo 'NTPD_IP=fe80::ff:feee:1%br-mesh' >> /etc/community.cfg
+	echo 'UPGRADE_PATH=http://[fe80::ff:feee:1%br-mesh]/dev/firmware/current' >> /etc/community.cfg
+	#and now reboot the Router to set the new settings
+	cp /rom/etc/uci-defaults/60-fff-wireless /etc/uci-defaults/60-fff-wireless 
+	reboot
+fi
+}
+
 make_config() {
 # remove old config
 >/etc/config/tunneldigger
@@ -35,6 +69,8 @@  for file in $filecounts; do
         rm /etc/fastd/${project}/peers/$file
     fi
 done
+#now we load the wireless Information from the keyxchangev2:
+set_wifi
 }
 
 # main
@@ -43,9 +79,10 @@  test_ipv4_host2="8.8.8.8"        # Google DNS
 test_ipv6_host1="heise.de"       # heise Zeitschriftenverlag
 
 # Only do something when the router has internet connection
-if ping -w5 -c3 "$test_ipv4_host1" &>/dev/null ||
+if (ping -w5 -c3 "$test_ipv4_host1" &>/dev/null ||
    ping -w5 -c3 "$test_ipv4_host2" &>/dev/null ||
-   ping6 -w5 -c3 "$test_ipv6_host1" &>/dev/null; then
+   ping6 -w5 -c3 "$test_ipv6_host1" &>/dev/null) &&
+   [ $(cat /tmp/novpn) != "1" ]; then
 
     # set some vars
     . /etc/community.cfg
@@ -86,4 +123,46 @@  if ping -w5 -c3 "$test_ipv4_host1" &>/dev/null ||
         fi
 
     fi
+else
+	# now we check to have Batmangateways in range
+	if [ "$(batctl gwl | grep "no gateway")" ]; then
+		#now we haven't a gateway in Range, we search for a hidden AP to get Internet and reach the KeyXchangev2 
+		#set this to not open a vpn because we have in the next step Internet
+		echo "1" > /tmp/novpn
+		#first we delete all wifi settings
+		if ! wifiDelAll; then
+			echo "Can't delete current wifi setup"
+			exit 1
+		fi
+		#now we look for phy and add this
+		for phy in $(iw phy | awk '/^Wiphy/{ print $2 }'); do
+			if iw phy "$phy" info | grep -q -m1 "2... MHz"; then
+				echo "$phy is 2.4 GHz"
+				radio=$(wifiAddPhy "$phy" "$BATMAN_CHANNEL")
+				if [ -z "$radio" ]; then
+					echo "Can't create radio for $phy"
+					exit 1
+				fi
+			fi
+			if iw phy "$phy" info | grep -q -m1 "5... MHz"; then
+				echo "$phy is 5 GHz"
+				radio=$(wifiAddPhy "$phy" "$BATMAN_CHANNEL_5GHZ")
+				if [ -z "$radio" ]; then
+					echo "Can't create radio for $phy"
+					exit 1
+				fi
+			fi
+			#and here we add the station
+			if ! wifiAddSta "$radio" "$ESSID_AP"; then
+				echo "Can't add Sta interface on $radio."
+				exit 1
+			fi
+		done
+		#and here we can check to reach the keyxchangev2
+
+		set_wifi	
+	else
+		echo "we have a gateway in range and do nothing"
+	fi
+
 fi
diff --git a/src/packages/fff/fff-wireless/files/lib/functions/fff/wireless b/src/packages/fff/fff-wireless/files/lib/functions/fff/wireless
index 32cfdb5..9ed9826 100644
--- a/src/packages/fff/fff-wireless/files/lib/functions/fff/wireless
+++ b/src/packages/fff/fff-wireless/files/lib/functions/fff/wireless
@@ -116,4 +116,35 @@  wifiAddAP() {
 	echo "${iface}"
 }
 
+wifiAddSta() {
+	if [ $# -ne "2" ]
+	then
+		echo "Usage: wifiAddSta <radio> <essid>"
+		return 1
+	fi
+
+	local radio=$1
+	local essid=$2
+
+	local channel=$(uci get "wireless.${radio}.channel")
+	local iface="w2Sta"
+	if [ "$channel" -gt "14" ]; then
+		iface="w5Sta"
+	fi
+	uci batch <<-__EOF__
+		set wireless.${iface}='wifi-iface'
+		set wireless.${iface}.device='${radio}'
+		set wireless.${iface}.network='StaWiFi'
+		set wireless.${iface}.ifname='${iface}'
+		set wireless.${iface}.mode='sta'
+		set wireless.${iface}.ssid='${essid}'
+
+		commit wireless
+	__EOF__
+	
+	#i think we need here some settings in uci network to geht a IP via DHCP!
+
+	echo "${iface}"
+}
+
 # vim: set noexpandtab:tabstop=4

Comments

Christian Dresel Aug. 20, 2017, 8:39 a.m.
Hallo zusammen

wir haben gestern am F3 Netze e.V. Sommercamp ein bisschen über den
dezentralen KeyXchange gesprochen.

Irgendwie hätte den schon jeder gerne aber es ist gefühlt einfach nicht
die Man-Power da um das Ding fertig zu stellen und vorallem dann auch zu
warten und Bugs zu pflegen die unweigerlich auftreten werden.
Genaugenommen fehlt aktuell sogar die Zeit alle Patches ins Git zu
bekommen, geschweige denn eine neue Beta/Release zu erstellen.

So war dann mal eine Überlegung zwischen drinn, einen KeyXchangeV2 zu
verwenden und weiterhin auf einer zentralen MySql Datenbank mit PHP
Script zu bleiben.

Als Erweiterung soll dazu kommen, das der KeyXchange nun die Wireless
Daten an die Knoten weiter gibt, so das jeder Router sich nun eigens
konfigurieren kann und wir die elendige Geschichten mit den Loops los
werden (es nervt mich langsam ein bisschen ;)). Dazu können wir hier
auch problemlos das Batman mit hochziehen.

Ich hab einfach mal ein RFC zusammen geschustert wie ich mir sowas in
etwa vorstellen könnte (und hab dabei auch gleich mal meinen Umgang mit
git getestet und versucht meine Englischkenntnisse etwas zu verbessern ;)).

Das ganze hier ist absolut ungetestet (und in 2 oder 3h zusammen
geschustert) und soll nur die Idee aufzeigen was ich mir so vorstellen
könnte. Ich hab mit absicht relativ viel Kommentare und sinnlose if
elses mit Ausgaben eingebaut damit man (hoffentlich) versteht warum
manches dies tut.

Als Erweiterung könnte ich mir im WebUI noch einen Knopf vorstellen:
- Lade alle Hoodinformationen vom KeyXchangev2
Wenn man den drückt werden alle Hoodinformationen heruntergeladen und
man kann manuell eine Hood auswählen. Wenn im keyxchangev2 dann auch
Hardhöhe, StPaul, marterlach, etc. angelegt sind, ist darüber ein
manuelles verbinden in diese Hoods ebenfalls möglich. Dies ist in meinen
RFC noch nicht eingebaut.

Rückfragen bitte jederzeit und ich würde mir hier eine Diskussion
wünschen wie der Rest der Community dazu steht.

mfg

Christian

On 20.08.2017 10:29, Christian Dresel wrote:
> If we have internet we connect directly to the keyxchangev2 and load
> the settings
> if we haven't internet we try to connect to a hidden AP as station and
> load the settings from the keyxchangev2
> after set the new wireless settings we copy back the 60-fff-wireless
> uci default and reboot the node
> 
> Signed-off-by: Christian Dresel <fff@chrisi01.de>
> ---
>  .../fff/fff-vpn-select/files/usr/sbin/vpn-select   | 83 +++++++++++++++++++++-
>  .../fff-wireless/files/lib/functions/fff/wireless  | 31 ++++++++
>  2 files changed, 112 insertions(+), 2 deletions(-)
> 
> diff --git a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
> index c3d003f..e0904d6 100755
> --- a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
> +++ b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select
> @@ -1,7 +1,41 @@
>  #!/bin/sh
>  
> +. /lib/functions/fff/wireless
> +
>  test -f /tmp/started || exit
>  
> +set_wifi() {
> +wget -T15 "http://keyserver.freifunk-franken.de/${project}/wireless.php?mac=$mac" -O /tmp/wireless_${project}_output
> +$NEWSETTINGS=$(cat /tmp/wireless_${project}_output)
> +#check the settings the same as set if not set it new and reboot the Node if it the same do nothing
> +#we get the settings in this format from keyxchangev2: ESSID_AP;ESSID_MESH;BSSID_MESH
> +ESSID_AP_AKT=$(cat /etc/community.cfg  | grep ESSID_AP | cut -d "=" -f 2)
> +ESSID_MESH_AKT=$(cat /etc/community.cfg  | grep ESSID_MESH | cut -d "=" -f 2)
> +BSSID_MESH_AKT=$(cat /etc/community.cfg  | grep BSSID_MESH | cut -d "=" -f 2)
> +CHECKSAME="$ESSID_AP_AKT;$ESSID_MESH_AKT;$BSSID_MESH_AKT"
> +if [ "$CHECKSAME" == "$NEWSETTINGS" ]; then
> +	echo "We have same settings and do nothing"
> +else
> +	#here we must write the config file new and reboot after
> +	ESSID_AP_NEW=$(cat /tmp/wireless_${project}_output | cut -d ";" -f 0)
> +	ESSID_MESH_NEW=$(cat /tmp/wireless_${project}_output | cut -d ";" -f 1)
> +	BSSID_MESH_NEW=$(cat /tmp/wireless_${project}_output | cut -d ";" -f 2)
> +	rm /etc/community.cfg
> +	touch /etc/community.cfg
> +	echo 'BATMAN_CHANNEL=1' >> /etc/community.cfg
> +	echo 'BATMAN_CHANNEL_5GHZ=36' >> /etc/community.cfg
> +	echo 'ESSID_AP=$ESSID_AP_NEW' >> /etc/community.cfg
> +	echo 'ESSID_MESH=$ESSID_MESH_NEW' >> /etc/community.cfg
> +	echo 'BSSID_MESH=$BSSID_MESH_NEW' >> /etc/community.cfg
> +	echo 'VPN_PROJECT=fff' >> /etc/community.cfg
> +	echo 'NTPD_IP=fe80::ff:feee:1%br-mesh' >> /etc/community.cfg
> +	echo 'UPGRADE_PATH=http://[fe80::ff:feee:1%br-mesh]/dev/firmware/current' >> /etc/community.cfg
> +	#and now reboot the Router to set the new settings
> +	cp /rom/etc/uci-defaults/60-fff-wireless /etc/uci-defaults/60-fff-wireless 
> +	reboot
> +fi
> +}
> +
>  make_config() {
>  # remove old config
>  >/etc/config/tunneldigger
> @@ -35,6 +69,8 @@ for file in $filecounts; do
>          rm /etc/fastd/${project}/peers/$file
>      fi
>  done
> +#now we load the wireless Information from the keyxchangev2:
> +set_wifi
>  }
>  
>  # main
> @@ -43,9 +79,10 @@ test_ipv4_host2="8.8.8.8"        # Google DNS
>  test_ipv6_host1="heise.de"       # heise Zeitschriftenverlag
>  
>  # Only do something when the router has internet connection
> -if ping -w5 -c3 "$test_ipv4_host1" &>/dev/null ||
> +if (ping -w5 -c3 "$test_ipv4_host1" &>/dev/null ||
>     ping -w5 -c3 "$test_ipv4_host2" &>/dev/null ||
> -   ping6 -w5 -c3 "$test_ipv6_host1" &>/dev/null; then
> +   ping6 -w5 -c3 "$test_ipv6_host1" &>/dev/null) &&
> +   [ $(cat /tmp/novpn) != "1" ]; then
>  
>      # set some vars
>      . /etc/community.cfg
> @@ -86,4 +123,46 @@ if ping -w5 -c3 "$test_ipv4_host1" &>/dev/null ||
>          fi
>  
>      fi
> +else
> +	# now we check to have Batmangateways in range
> +	if [ "$(batctl gwl | grep "no gateway")" ]; then
> +		#now we haven't a gateway in Range, we search for a hidden AP to get Internet and reach the KeyXchangev2 
> +		#set this to not open a vpn because we have in the next step Internet
> +		echo "1" > /tmp/novpn
> +		#first we delete all wifi settings
> +		if ! wifiDelAll; then
> +			echo "Can't delete current wifi setup"
> +			exit 1
> +		fi
> +		#now we look for phy and add this
> +		for phy in $(iw phy | awk '/^Wiphy/{ print $2 }'); do
> +			if iw phy "$phy" info | grep -q -m1 "2... MHz"; then
> +				echo "$phy is 2.4 GHz"
> +				radio=$(wifiAddPhy "$phy" "$BATMAN_CHANNEL")
> +				if [ -z "$radio" ]; then
> +					echo "Can't create radio for $phy"
> +					exit 1
> +				fi
> +			fi
> +			if iw phy "$phy" info | grep -q -m1 "5... MHz"; then
> +				echo "$phy is 5 GHz"
> +				radio=$(wifiAddPhy "$phy" "$BATMAN_CHANNEL_5GHZ")
> +				if [ -z "$radio" ]; then
> +					echo "Can't create radio for $phy"
> +					exit 1
> +				fi
> +			fi
> +			#and here we add the station
> +			if ! wifiAddSta "$radio" "$ESSID_AP"; then
> +				echo "Can't add Sta interface on $radio."
> +				exit 1
> +			fi
> +		done
> +		#and here we can check to reach the keyxchangev2
> +
> +		set_wifi	
> +	else
> +		echo "we have a gateway in range and do nothing"
> +	fi
> +
>  fi
> diff --git a/src/packages/fff/fff-wireless/files/lib/functions/fff/wireless b/src/packages/fff/fff-wireless/files/lib/functions/fff/wireless
> index 32cfdb5..9ed9826 100644
> --- a/src/packages/fff/fff-wireless/files/lib/functions/fff/wireless
> +++ b/src/packages/fff/fff-wireless/files/lib/functions/fff/wireless
> @@ -116,4 +116,35 @@ wifiAddAP() {
>  	echo "${iface}"
>  }
>  
> +wifiAddSta() {
> +	if [ $# -ne "2" ]
> +	then
> +		echo "Usage: wifiAddSta <radio> <essid>"
> +		return 1
> +	fi
> +
> +	local radio=$1
> +	local essid=$2
> +
> +	local channel=$(uci get "wireless.${radio}.channel")
> +	local iface="w2Sta"
> +	if [ "$channel" -gt "14" ]; then
> +		iface="w5Sta"
> +	fi
> +	uci batch <<-__EOF__
> +		set wireless.${iface}='wifi-iface'
> +		set wireless.${iface}.device='${radio}'
> +		set wireless.${iface}.network='StaWiFi'
> +		set wireless.${iface}.ifname='${iface}'
> +		set wireless.${iface}.mode='sta'
> +		set wireless.${iface}.ssid='${essid}'
> +
> +		commit wireless
> +	__EOF__
> +	
> +	#i think we need here some settings in uci network to geht a IP via DHCP!
> +
> +	echo "${iface}"
> +}
> +
>  # vim: set noexpandtab:tabstop=4
>