[v3] configurehood: Prevent connecting two hoods

Submitted by Adrian Schmutzler on Dec. 7, 2017, 3:09 p.m.

Details

Message ID 1512659397-3125-1-git-send-email-freifunk@adrianschmutzler.de
State Superseded
Headers show

Commit Message

Adrian Schmutzler Dec. 7, 2017, 3:09 p.m.
To prevent connecting hoods, this patch loads keyxchange files
from the local network (eth0.3/eth0) before it uses the gateway.

Thus, if other files are provided via wXconfigap, they are just
ignored. If a router is connected to two hoods by cable, it will
just disable the interfaces where a second hood file is detected
and wait until the next call of configurehood.

If cable and wXmesh are different, the cable has precedence.

If two hoods are present via cable on the same eth, wXmesh has
precedence.

If two hoods are present via cable on different eth, the first
eth has precedence and all others are disabled.

If cable has precedence, wXmesh is configured with the hood
file from cable.

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>

---

Changes in v2:
- ifconfig eth down instead of ifconfig br-mesh down
- networking restart to get up interfaces again
- Cycle over eths instead of assuming just one

Changes in v3:
- Replace eth by $eth in inner for loop
- Use continue 2 instead of exit to go through all eths
- Only set oldhood once
- Use separate ethfile first, because otherwise a second eth
  would overwrite the file although disabled
---
 .../fff/fff-hoods/files/usr/sbin/configurehood     | 37 +++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/src/packages/fff/fff-hoods/files/usr/sbin/configurehood b/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
index 822e5fc..5986ae2 100755
--- a/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
+++ b/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
@@ -10,6 +10,7 @@  sectorlocal=/etc/sectorfile
 sectortmp=/tmp/sectorfile
 sectorcopy=/www/hood/sectorfile
 hiddenapfile=/tmp/hiddenapflag
+resetnetworkfile=/tmp/resetnetwork
 
 rm -f "$hoodfile"
 
@@ -65,6 +66,12 @@  fi
 lat=$(uci -q get fff.system.latitude)
 long=$(uci -q get fff.system.longitude)
 
+# reenable network interfaces in case we disabled them earlier
+if [ -f "$resetnetworkfile" ]; then
+	/etc/init.d/network restart
+	rm "$resetnetworkfile"
+fi
+
 # if we have Internet, we download the Hoodfile from the keyxchangev2
 if hasInternet ; then
 	wget -T15 -t5 "http://keyserver.freifunk-franken.de/v2/?lat=$lat&long=$long" -O "$hoodfile"
@@ -119,7 +126,35 @@  else
 		fi
 	else
 		echo "We have a Gateway in Range, we load the keyxchangev2data from fe80::1"
-		wget -T15 -t5 "http://[fe80::1%br-mesh]:2342/keyxchangev2data" -O "$hoodfile"
+		# check eth first
+		oldhood=""
+		ethfile="${hoodfile}eth"
+		for eth in $(batctl if | grep "eth" | sed -nE 's/.*(eth[^:]+):.*/\1/p'); do
+			for mac in $(batctl n | grep "$eth" | sed -nE 's/.*eth[0-9.]+\s+([^\s]+)\s.*/\1/p'); do
+				EUI="$(echo "$mac" | awk -F: '{ printf("%02x%s:%sff:fe%s:%s%s\n", xor(("0x"$1),2), $2, $3, $4, $5, $6) }')"
+				wget -T15 -t5 "http://[fe80::${EUI}%${eth}]:2342/keyxchangev2data" -O "$ethfile"
+				if [ -s "$ethfile" ]; then
+					json_load "$(cat "$ethfile")"
+					json_select hood
+					json_get_var newhood name
+					if [ -n "$oldhood" ] && [ -n "$newhood" ] && ( ! [ "$newhood" = "$oldhood" ] ) ; then
+						# 2nd hood found, kill interface and go on (next try in 5 min.)
+						echo "Two hoods detected. Remove cables to stay in just one."
+						ifconfig "$eth" down
+						touch "$resetnetworkfile"
+						continue 2 # go to the next interface
+					fi
+					mv "$ethfile" "$hoodfile" # Only use hoodfile if sane
+					[ -n "$oldhood" ] || oldhood="$newhood" # only set oldhood once
+				fi
+			done
+		done
+		if [ ! -s "$hoodfile" ]; then
+			# Only load hoodfile from gateway if not already present from local network
+			# - This gives local network a precedence (take the hood from local network)
+			# - This prevents file insertion from a third person, as I will only connect via LAN to who I trust
+			wget -T15 -t5 "http://[fe80::1%br-mesh]:2342/keyxchangev2data" -O "$hoodfile"
+		fi
 		#UPLINK: Do nothing
 	fi
 fi

Comments

Christian Dresel Dec. 17, 2017, 5:19 p.m.
Hallo

ich hab es mir nochmal angeschaut und hab noch eine Kleinigkeit Inline.

Ansonsten sieht es für mich logisch aus und ich werde es gleich mal testen.

On 07.12.2017 16:09, Adrian Schmutzler wrote:
> To prevent connecting hoods, this patch loads keyxchange files
> from the local network (eth0.3/eth0) before it uses the gateway.
> 
> Thus, if other files are provided via wXconfigap, they are just
> ignored. If a router is connected to two hoods by cable, it will
> just disable the interfaces where a second hood file is detected
> and wait until the next call of configurehood.
> 
> If cable and wXmesh are different, the cable has precedence.
> 
> If two hoods are present via cable on the same eth, wXmesh has
> precedence.
> 
> If two hoods are present via cable on different eth, the first
> eth has precedence and all others are disabled.
> 
> If cable has precedence, wXmesh is configured with the hood
> file from cable.
> 
> Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
> 
> ---
> 
> Changes in v2:
> - ifconfig eth down instead of ifconfig br-mesh down
> - networking restart to get up interfaces again
> - Cycle over eths instead of assuming just one
> 
> Changes in v3:
> - Replace eth by $eth in inner for loop
> - Use continue 2 instead of exit to go through all eths
> - Only set oldhood once
> - Use separate ethfile first, because otherwise a second eth
>   would overwrite the file although disabled
> ---
>  .../fff/fff-hoods/files/usr/sbin/configurehood     | 37 +++++++++++++++++++++-
>  1 file changed, 36 insertions(+), 1 deletion(-)
> 
> diff --git a/src/packages/fff/fff-hoods/files/usr/sbin/configurehood b/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
> index 822e5fc..5986ae2 100755
> --- a/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
> +++ b/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
> @@ -10,6 +10,7 @@ sectorlocal=/etc/sectorfile
>  sectortmp=/tmp/sectorfile
>  sectorcopy=/www/hood/sectorfile
>  hiddenapfile=/tmp/hiddenapflag
> +resetnetworkfile=/tmp/resetnetwork
>  
>  rm -f "$hoodfile"
>  
> @@ -65,6 +66,12 @@ fi
>  lat=$(uci -q get fff.system.latitude)
>  long=$(uci -q get fff.system.longitude)
>  
> +# reenable network interfaces in case we disabled them earlier
> +if [ -f "$resetnetworkfile" ]; then
> +	/etc/init.d/network restart
> +	rm "$resetnetworkfile"
> +fi
> +
>  # if we have Internet, we download the Hoodfile from the keyxchangev2
>  if hasInternet ; then
>  	wget -T15 -t5 "http://keyserver.freifunk-franken.de/v2/?lat=$lat&long=$long" -O "$hoodfile"
> @@ -119,7 +126,35 @@ else
>  		fi
>  	else
>  		echo "We have a Gateway in Range, we load the keyxchangev2data from fe80::1"
> -		wget -T15 -t5 "http://[fe80::1%br-mesh]:2342/keyxchangev2data" -O "$hoodfile"
> +		# check eth first
> +		oldhood=""
> +		ethfile="${hoodfile}eth"
> +		for eth in $(batctl if | grep "eth" | sed -nE 's/.*(eth[^:]+):.*/\1/p'); do
> +			for mac in $(batctl n | grep "$eth" | sed -nE 's/.*eth[0-9.]+\s+([^\s]+)\s.*/\1/p'); do
> +				EUI="$(echo "$mac" | awk -F: '{ printf("%02x%s:%sff:fe%s:%s%s\n", xor(("0x"$1),2), $2, $3, $4, $5, $6) }')"
> +				wget -T15 -t5 "http://[fe80::${EUI}%${eth}]:2342/keyxchangev2data" -O "$ethfile"

5x15 Sekunden, wenn da paar Sachen drinnen sind die keine Hoodfile
anbieten (z.b. weil Batmanports im priv. Netz mit 15 weiteren Geräten
hämngt) sind wir ruck zuck bei den 5 Minuten bis das Script wieder startet.

Da wir ja per Ethernet laden und da eigentlich ordentlich
Geschwindigkeit haben, denke ich könnten wir das ganze drastisch
reduzieren. 2x1Sekunde würde wohl locker ausreichen.

Vielleicht wäre es irgendwann (in einen weiteren Patch) auch sinnvoll,
am anfang von configurehood zu prüfen ob es denn noch läuft und dann es
gar nicht erst ausführen (im bestcase wenn das 5x passiert ist, wird das
alte getötet weil dann is wohl was faul). Wer weiß was sonst passiert
wenn das 2x läuft ich denke das ist doch sehr unschön.

mfg

Christian

> +				if [ -s "$ethfile" ]; then
> +					json_load "$(cat "$ethfile")"
> +					json_select hood
> +					json_get_var newhood name
> +					if [ -n "$oldhood" ] && [ -n "$newhood" ] && ( ! [ "$newhood" = "$oldhood" ] ) ; then
> +						# 2nd hood found, kill interface and go on (next try in 5 min.)
> +						echo "Two hoods detected. Remove cables to stay in just one."
> +						ifconfig "$eth" down
> +						touch "$resetnetworkfile"
> +						continue 2 # go to the next interface
> +					fi
> +					mv "$ethfile" "$hoodfile" # Only use hoodfile if sane
> +					[ -n "$oldhood" ] || oldhood="$newhood" # only set oldhood once
> +				fi
> +			done
> +		done
> +		if [ ! -s "$hoodfile" ]; then
> +			# Only load hoodfile from gateway if not already present from local network
> +			# - This gives local network a precedence (take the hood from local network)
> +			# - This prevents file insertion from a third person, as I will only connect via LAN to who I trust
> +			wget -T15 -t5 "http://[fe80::1%br-mesh]:2342/keyxchangev2data" -O "$hoodfile"
> +		fi
>  		#UPLINK: Do nothing
>  	fi
>  fi
>