2017-10-05 15:37:44 +02:00
#!/bin/sh
. /usr/share/libubox/jshn.sh
2017-10-30 20:08:27 +01:00
. /lib/functions/fff/keyxchange
2017-11-03 09:18:01 +01:00
. /lib/functions/fff/network
2017-10-05 15:37:44 +02:00
. /lib/functions/fff/wireless
2017-10-05 15:37:46 +02:00
. /lib/functions/fff/timeserver
2018-06-12 21:07:00 +02:00
. /usr/lib/functions/fff/hoodfile
2017-10-05 15:37:44 +02:00
2018-01-02 15:46:37 +01:00
hoodlocal=/etc/hoodfile
2017-10-30 18:47:16 +01:00
2017-10-30 20:08:27 +01:00
rm -f "$hoodfile"
2017-10-05 15:37:44 +02:00
# Gatewaycheck function
isGatewayAvailable() {
if [ "$(batctl gwl | wc -l)" -gt 2 ]; then
return 0
else
return 1
fi
}
# Ping test
hasInternet() {
test_ipv4_host1="keyserver.freifunk-franken.de" # Freifunk-Franken keyserver
test_ipv4_host2="8.8.8.8" # Google DNS
test_ipv6_host1="heise.de" # heise Zeitschriftenverlag
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
return 0
fi
return 1
}
# Hidden AP check
2018-02-07 15:28:33 +01:00
if [ -s "$hoodfilecopy" ] && isGatewayAvailable ; then
2018-02-13 13:14:31 +01:00
needwifi="0"
2018-02-07 15:28:33 +01:00
for radio in $(uci show wireless | sed -n 's,.*\.\([a-z0-9]*\)=wifi-device,\1,p'); do
freq="2"
if [ "$(uci get "wireless.${radio}.channel")" -gt "14" ]; then
freq="5"
fi
# Break: wXconfig is up
uci -q get "wireless.w${freq}configap" > /dev/null && continue
# Break: No mesh interface
(uci -q get "wireless.w${freq}mesh" > /dev/null || uci -q get "wireless.w${freq}ibss" > /dev/null) || continue
# Create configap
iface="configap$freq"
uci set network.${iface}=interface
uci set network.${iface}.proto='static'
uci set network.${iface}.ip6addr='fe80::1/64'
2017-10-15 16:29:48 +02:00
uci commit network
2018-06-10 14:06:00 +02:00
reload_config
2018-02-07 15:28:33 +01:00
if ! wifiAddAP "$radio" "config.franken.freifunk.net" "$iface" "configap" "1"; then
echo "Can't add Config interface on $radio."
exit 1
fi
2018-02-13 13:14:31 +01:00
needwifi="1"
2018-02-07 15:28:33 +01:00
done
2017-10-05 15:37:44 +02:00
2018-02-13 13:14:31 +01:00
if [ "$needwifi" = "1" ] ; then
wifi
sleep 10
fi
2017-10-05 15:37:44 +02:00
fi
2018-01-02 15:46:37 +01:00
if [ -s "$hoodlocal" ]; then
hoodfile="$hoodlocal"
echo "Use local hood file"
2017-10-05 15:37:44 +02:00
else
2018-01-02 15:46:37 +01:00
# if we have Internet, we download the Hoodfile from the keyxchangev2
if hasInternet ; then
2018-06-12 21:07:00 +02:00
getKeyserverHoodfile "$hoodfile"
2018-01-02 15:46:37 +01:00
#if no Internet, we connect to the hidden AP and download the file from another Node in range
else
# connect to wireless hidden ap here and download the json File from the nearest router
# Only do that, when we have no gateway in range. If the Uplinkrouter changed the hood, we lost the GW and do this automatically again, I think! Nice idea?
if ! isGatewayAvailable ; then
#now we haven't a gateway in Range, we search for a hidden AP to get a keyxchangev2data file!
#first we delete all wifi settings
rm -f "$hoodfilecopy" # delete this, so interfaces are recreated if reconnect with unchanged hood file takes place
2018-06-10 14:05:59 +02:00
uci -q del "system.@system[0].hood"
uci -q commit system
2018-06-10 14:06:00 +02:00
reload_config
2017-11-23 22:50:09 +01:00
2018-01-02 15:46:37 +01:00
sleep 30 # Wait for the config AP, which may be created at the same time as this script has started
2017-10-05 15:37:44 +02:00
2018-06-12 21:07:01 +02:00
getEthernetHoodfile "$hoodfile" || getWirelessHoodfile "$hoodfile"
2018-01-02 15:46:37 +01:00
else
echo "We have a Gateway in Range, we load the keyxchangev2data from fe80::1"
2018-06-12 21:07:00 +02:00
getGatewayHoodfile "$hoodfile"
2017-12-18 15:03:34 +01:00
fi
2017-10-05 15:37:44 +02:00
fi
fi
2017-10-30 20:08:27 +01:00
if [ -s "$hoodfile" ]; then
2017-10-05 15:37:44 +02:00
# we get a json file in this format:
# https://pw.freifunk-franken.de/patch/205/
# but without signature, every hood file we get is valid!
2017-11-05 11:03:35 +01:00
catnew="$(cat "$hoodfile" | sed 's/"timestamp”: *"[0-9]*"/"timestamp":0/')"
catold="$(cat "$hoodfilecopy" 2>/dev/null | sed 's/"timestamp”: *"[0-9]*"/"timestamp":0/')"
2017-10-05 15:37:44 +02:00
sumnew=$(echo "$catnew" | sha256sum | cut -f1 -d " ")
sumold=$(echo "$catold" | sha256sum | cut -f1 -d " ")
2017-10-30 20:08:27 +01:00
json_load "$(cat "$hoodfile")"
2017-10-05 15:37:44 +02:00
2017-10-30 18:47:16 +01:00
if [ "$sumnew" != "$sumold" ] ; then
echo "New file detected, we reconfigure the Node";
2017-10-05 15:37:44 +02:00
json_select hood
json_get_var hood name
2017-10-30 18:47:16 +01:00
[ -n "$mesh_id" ] || json_get_var mesh_id mesh_id
[ -n "$mesh_bssid" ] || json_get_var mesh_bssid mesh_bssid
[ -n "$mesh_essid" ] || json_get_var mesh_essid mesh_essid
[ -n "$essid" ] || json_get_var essid essid
2017-10-05 15:37:46 +02:00
json_get_var ntpip ntp_ip
2017-10-30 18:47:16 +01:00
[ -n "$chan2ghz" ] || json_get_var chan2ghz channel2
[ -n "$mesh_type2" ] || json_get_var mesh_type2 mesh_type2
[ -n "$chan5ghz" ] || json_get_var chan5ghz channel5
[ -n "$mesh_type5" ] || json_get_var mesh_type5 mesh_type5
2017-10-26 08:57:12 +02:00
# Additional parameters may be activated in future versions
2017-10-05 15:37:44 +02:00
#json_get_var mode2 mode2
#json_get_var mode5 mode5
#json_get_var protocol protocol
json_select ".." # back to root
2017-10-30 18:47:16 +01:00
2017-10-26 08:57:12 +02:00
if ! ([ -n "$chan2ghz" ] && [ -n "$chan5ghz" ]) ; then
# If channel is missing, do nothing
exit 0
fi
2017-10-05 15:37:44 +02:00
echo "Setting hood name: $hood"
2017-10-18 20:27:22 +02:00
uci -q set "system.@system[0].hood=$hood"
uci -q commit system
2018-06-10 14:06:00 +02:00
reload_config
2017-10-05 15:37:44 +02:00
2017-11-02 14:31:15 +01:00
if ! wifiDelIface; then
2017-10-05 15:37:44 +02:00
echo "Can't delete current wifi setup"
exit 1
fi
for phy in $(iw phy | awk '/^Wiphy/{ print $2 }'); do
radio="$(wifiAddPhyCond "$phy" "2" "$chan2ghz")"
radio5="$(wifiAddPhyCond "$phy" "5" "$chan5ghz")"
[ -n "$radio5" ] && radio="$radio5"
if ! wifiAddAP "$radio" "$essid" "mesh" "ap" "0"; then
echo "Can't add AP interface on $radio."
exit 1
fi
2017-10-15 11:27:58 +02:00
# add 802.11s mesh if type == "802.11s"
if ( [ -n "$radio5" ] && [ "$mesh_type5" == "802.11s" ] ) || [ "$mesh_type2" == "802.11s" ]; then
if ! wifiAddMesh "$radio" "$mesh_id"; then
echo "Can't add Mesh interface on $radio."
exit 1
fi
fi
# add IBSS mesh if type == "ibss"
if ( [ -n "$radio5" ] && [ "$mesh_type5" == "ibss" ] ) || [ "$mesh_type2" == "ibss" ]; then
if ! wifiAddAdHocMesh "$radio" "$mesh_essid" "$mesh_bssid"; then
echo "Can't add AdHocMesh interface on $radio."
exit 1
fi
2017-10-05 15:37:44 +02:00
fi
done
echo "Loading wifi"
wifi
2017-10-05 15:37:46 +02:00
oldntp="$(uci -q get system.ntp.server)"
newntp="${ntpip}" # requires routable address, no link-local
[ "$newntp" = "$oldntp" ] || setTimeserver "${newntp}" # only rewrite if changed
2017-10-30 18:47:16 +01:00
# copy the file to webroot so that other mesh routers can download it;
2017-10-05 15:37:44 +02:00
# copy only after all other steps so IF can be reentered if something goes wrong
2017-10-30 20:08:27 +01:00
cp "$hoodfile" "$hoodfilecopy"
2017-10-05 15:37:44 +02:00
2017-12-22 13:10:25 +01:00
# This is a workaround to enable alfred on devices which do not see a configap during initial setup
/etc/init.d/alfred restart
2017-10-05 15:37:44 +02:00
else
echo "We have no new file. We do nothing. We try it again in 5 minutes...";
fi
2017-10-05 15:37:45 +02:00
# and now we get to vpn-select script and load VPNs directly from /tmp/keyxchangev2data
2017-10-05 15:37:44 +02:00
if hasInternet ; then
sh /usr/sbin/vpn-select
2018-02-13 13:27:37 +01:00
else
sh /usr/sbin/vpn-stop
2017-10-05 15:37:44 +02:00
fi
2017-10-08 18:28:54 +02:00
# now we load the prefix from the hoodfile and set this to br-mesh
json_select network
json_get_var prefix ula_prefix
# Set $prefix::MAC as IP
if [ -n "$prefix" ] ; then
prefix="$(echo "$prefix" | sed -e 's,\\,,')"
2018-01-03 00:37:07 +01:00
mac="$(cat "/sys/class/net/br-mesh/address")"
addr="$(ipMacAssemble "$prefix" "$mac")"
2017-11-03 09:18:01 +01:00
addr="$(ipTidyColon "$addr")"
2018-01-03 00:37:07 +01:00
addr_eui="$(ipEUIAssemble "$prefix" "$mac")"
2017-11-03 09:18:01 +01:00
addr_eui="$(ipTidyColon "$addr_eui")"
2017-10-08 18:28:54 +02:00
for ip in $(ip -6 addr show dev br-mesh | grep inet6 | grep -v -e " $addr" -e " $addr_eui" -e " fe80::" -e " fdff::" | cut -f6 -d " "); do
ip -6 addr del "$ip" dev br-mesh
done
if ! ( ip -6 addr show dev br-mesh | grep -q "$addr" ) ; then
ip -6 addr add "$addr" dev br-mesh
echo "Set ULA address to br-mesh: $addr"
else
echo "Address already set."
fi
# Set $prefix::link-local as IP
if ! ( ip -6 addr show dev br-mesh | grep -q "$addr_eui" ) ; then
ip -6 addr add "$addr_eui" dev br-mesh
echo "Set ULA EUI-64 address to br-mesh: $addr_eui"
else
echo "Address already set."
fi
if ! ( ip -6 route show dev br-mesh | grep -q "fc00::" ) ; then
ip -6 route add fc00::/7 via fe80::1 dev br-mesh
echo "Set ULA route to br-mesh."
else
echo "Route already set."
fi
fi
json_select ".." # back to root
2017-10-05 15:37:44 +02:00
else
echo "We haven't got a file. We do nothing. We try it again in 5 minutes...";
exit 0
fi