fff-network: Fix incorrect IPv6 syntax in functions

Some of our network helper functions did require the use of an invalid
ipv6 prefix syntax to function correctly.

Specifically, if all 64 bits of the prefix are given, the caller has to
omit one colon for our assemble-functions to work properly. Therefore
the prefix '2001:db8:1:2::/64' had to be written as '2001:db8:1:2:/64'.

Also, our functions did not allow to specify the suffix with leading
colons.

This changes the behaviour of these functions to accept properly
formatted IPv6 prefixes and suffixes. All calls to these functions are
adjusted accordingly.

There is one notebly exeption: The incorrect prefix syntax in hoodfiles
cannot be adjusted yet, because that would break devices with using
older firmware. To keep compatibility with older firmware versions, the
hoodfiles prefix syntax is not yet adjusted, and a special case is added
to configurehood to allow the use of the old incorrect syntax.

While this new solution still is kind of hacky and does things that
should actually be done by a proper address parser, it does at least fix
the before mentioned problems.

Fixes: #27

Signed-off-by: Fabian Bläse <fabian@blaese.de>
This commit is contained in:
Fabian Bläse 2021-01-08 20:18:25 +01:00
parent 5469399112
commit daab44944e
5 changed files with 37 additions and 10 deletions

View File

@ -1,7 +1,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=fff-hoods
PKG_RELEASE:=17
PKG_RELEASE:=18
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)

View File

@ -190,6 +190,14 @@ if [ -s "$hoodfiletmp" ]; then
# Set $prefix::MAC as IP
if [ -n "$prefix" ] ; then
prefix="$(echo "$prefix" | sed -e 's,\\,,')"
# in earlier firmware versions the prefix had to be written
# in an incorrect syntax (missing a trailing colon).
# To make hoodfiles with this old incorrect syntax work with
# newer firmware versions like this one, we have to fix the
# incorrect syntax here.
prefix="$(echo "$prefix" | sed -e 's,\([^:]\):\/,\1::/,')"
mac="$(cat "/sys/class/net/br-client/address")"
addr="$(ipMacAssemble "$prefix" "$mac")"
addr="$(ipTidyColon "$addr")"

View File

@ -1,7 +1,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=fff-network
PKG_RELEASE:=26
PKG_RELEASE:=27
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)

View File

@ -2,7 +2,7 @@
# License GPLv3
ipMacSuffix() {
# Returns the lower 64 bits of an IPv6 address (0:aabb:ccdd:eeff)
# Returns the lower 64 bits of an IPv6 address (::aabb:ccdd:eeff)
# based on the provided MAC address (aa:bb:cc:bb:ee:ff)
#
# Argument: MAC address (with colons)
@ -11,12 +11,12 @@ ipMacSuffix() {
local mac=$1
echo "$mac" | awk -F: '{ print "0:"$1$2":"$3$4":"$5$6 }'
echo "$mac" | awk -F: '{ print "::"$1$2":"$3$4":"$5$6 }'
return 0
}
ipEUISuffix() {
# Returns the EUI (interface ID, a8bb:ccff:fedd:eeff)
# Returns the EUI (interface ID, ::a8bb:ccff:fedd:eeff)
# based on the provided MAC address (aa:bb:cc:bb:ee:ff)
#
# Argument: MAC address (with colons)
@ -25,21 +25,40 @@ ipEUISuffix() {
local mac=$1
echo "$mac" | awk -F: '{ printf("%02x%s:%sff:fe%s:%s%s\n", xor(("0x"$1),2), $2, $3, $4, $5, $6) }'
echo "$mac" | awk -F: '{ printf("::%02x%s:%sff:fe%s:%s%s\n", xor(("0x"$1),2), $2, $3, $4, $5, $6) }'
return 0
}
ipAssemble() {
# Concatenates a prefix (1st argument) and a suffix (2nd argument) to a merged IPv6 address
# (The prefix has to bear the subnet: fdff::/64)
# (The prefix must only contain the higher 64 bits (correct: 0:0:0:0: or 0:: - wrong: 0:0:0:0::)
# (The prefix must only contain the upper 64 bits (0:0:0:0:: or 0::)
# (The suffix must only contain the lower 64 bits (::0:0:0:0 or ::0)
[ $# -ne "2" ] && return 1
local prefix=$1
local suffix=$2
local suffix
local result
# remove leading colons
suffix=$(echo "$2" | sed -e 's,^:*,,')
# check if prefix contains the mandatory trailing double-colon
if ! echo "$prefix" | grep "::/" >/dev/null; then
return 1
fi
# concatenate prefix and suffix
result=$(echo "$prefix" | sed -e "s,/,$suffix/,")
# remove double-colon if both prefix and suffix are fully expanded
if [ "$(echo "$result" | awk -F":" '{print NF-1}')" -gt 7 ]; then
result=$(echo "$result" | sed -e 's,::,:,')
fi
echo "$result"
echo "$prefix" | sed -e 's,/,'$suffix'/,'
return 0
}

View File

@ -207,7 +207,7 @@ else
uci -q set network.client.proto=static
# Set $prefix::1 as IP
addr="$(ipAssemble "$prefix" "1")"
addr="$(ipAssemble "$prefix" "::1")"
ip -6 addr add $addr dev br-client
uci -q add_list network.client.ip6addr=$addr