From 5f22c2131c55900500b3bc224a231decd666f81c Mon Sep 17 00:00:00 2001 From: Christian Dresel Date: Thu, 31 Dec 2020 08:46:40 +0100 Subject: [PATCH] fff-autoconfig: Add IPv4 support With this script, IPv4 can configurate automaticaly Signed-off-by: Christian Dresel --- .../files/usr/lib/autol3.d/20-ipv4.sh | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 src/packages/fff/fff-autoconfig/files/usr/lib/autol3.d/20-ipv4.sh diff --git a/src/packages/fff/fff-autoconfig/files/usr/lib/autol3.d/20-ipv4.sh b/src/packages/fff/fff-autoconfig/files/usr/lib/autol3.d/20-ipv4.sh new file mode 100755 index 00000000..2bea5c26 --- /dev/null +++ b/src/packages/fff/fff-autoconfig/files/usr/lib/autol3.d/20-ipv4.sh @@ -0,0 +1,74 @@ +#!/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 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 + uci set fff.autol3.ipv4address=$ip + uci set fff.autol3.ipv4assigner=$assigner + uci set fff.autol3.autoconfigl3=1 + uci commit fff + 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 set fff.autol3.autoconfigl3=1 + 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 commit gateway + asknew + 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 +