Add package fff-autoconfig-v4

With this package it is possible to automatically get an ipv4 address for the router

We use the decentralized communication over mqtt

Signed-off-by: Christian Dresel <freifunk@dresel.systems>
This commit is contained in:
Christian Dresel 2020-12-26 20:34:44 +01:00
parent f0e2146263
commit 3128fda4a5
4 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,45 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=fff-autoconfig-v4
PKG_RELEASE:=1
PKG_BUILD_DIR:=$(BUILD_DIR)/fff-autoconfig-v4
include $(INCLUDE_DIR)/package.mk
define Package/fff-autoconfig-v4
SECTION:=base
CATEGORY:=Freifunk
TITLE:=Freifunk-Franken layer3 automatic ipv4 configuration
URL:=https://www.freifunk-franken.de
DEPENDS:= \
+fff-ipv4snat \
+fff-layer3-config \
+fff-mqtt \
+jshn
endef
define Package/fff-autoconfig-v4/description
This package load automatic ipv4 over mqtt
endef
define Build/Prepare
echo "all: " > $(PKG_BUILD_DIR)/Makefile
endef
define Build/Configure
# nothing
endef
define Build/Compile
# nothing
endef
define Package/fff-autoconfig-v4/install
$(CP) ./files/* $(1)/
endef
$(eval $(call BuildPackage,fff-autoconfig-v4))

View File

@ -0,0 +1 @@
*/5 * * * * /usr/sbin/autov4

View File

@ -0,0 +1,73 @@
#!/bin/sh
# first check is fff.autol3.ipv4 enabled
if [ -z $(uci -q get fff.autol3.ipv4) ] ; then
exit;
fi
. /usr/share/libubox/jshn.sh
MACADDR=$(cat /sys/class/net/br-client/address | /bin/sed 's/://g')
MQTTSERVER=$(uci -q get fff.mqtt.server)
ASSIGNEROWN=$(uci -q get fff.autol3.ipv4assigner)
IPOWN=$(uci -q get fff.autol3.ipv4address)
asknew() {
# Load Blocklist
ipv6assignerblock=$(uci get fff.autol3.ipv6assignerblock)
for ipv6assignerblock in $ipv6assignerblock; do
block="$block -T /ipv6/usethis/+/$ipv6assignerblock"
done
mosquitto_pub -h $MQTTSERVER -p 1883 -t '/ipv4/asknew' -m "$MACADDR"
# use the first income message
# cancel after 5 second, after this time we don't get any IP address
autol3config=$(mosquitto_sub -h $MQTTSERVER -p 1883 -t '/ipv4/usethis/#' -C 1 $block)
if [ -z "$autol3config" ] ; then
echo "No IP get"
exit 1;
fi
json_load "$autol3config"
json_get_var assigner assigner
json_get_var ip ip
ip=$(echo $ip | sed 's/\/.*//')
uci set fff.autol3.ipv4address=$ip
uci set fff.autol3.ipv4assigner=$assigner
uci commit fff
uci del gateway.meta.peer_ip
uci del gateway.@client[0].nat
uci del gateway.@client[0].ipaddr
uci del gateway.@client[0].dhcp_start
uci set gateway.meta.peer_ip=$ip
uci set gateway.@client[0].nat='1'
uci set gateway.@client[0].ipaddr='192.168.0.1/16'
uci set gateway.@client[0].dhcp_start='192.168.1.1'
uci commit gateway
yes | configure-layer3 -c
configure-layer3 -a
exit;
}
canusethis() {
mosquitto_pub -h $MQTTSERVER -p 1883 -t /ipv4/canusethis/$ASSIGNEROWN -m "{ \"mac\":\"$MACADDR\", \"ip\":\"$IPOWN/32\" }"
canusethisanswer=$(mosquitto_sub -h $MQTTSERVER -p 1883 -t /ipv4/canusethisanswer/$MACADDR -C 1 -W 5)
# check if we get answer, if no load asknew again and get new ip address
if [ -z $canusethisanswer ] ; then
uci del fff.autol3.ipv4address
uci del fff.autol3.ipv4assigner
uci commit fff
asknew
fi
}
if [ -z $(uci -q get fff.autol3) ] ; then
uci set fff.autol3=autol3
fi
if [ -n $(uci -q get fff.autol3) ] && [ -z $(uci -q get fff.autol3.ipv4address) ] ; then
asknew
fi
if [ -n $(uci -q get fff.autol3.ipv4address) ] ; then
canusethis
fi

View File

@ -13,6 +13,7 @@ define Package/fff-layer3
TITLE:=Freifunk-Franken gateway configuration
URL:=https://www.freifunk-franken.de
DEPENDS:=+fff-alfred-monitoring-proxy \
+fff-autoconfig-v4 \
+fff-babeld \
+fff-boardname \
+fff-dhcp \