treewide: replace IP string manipulation by owipcalc tool

The owipcalc tool provides an "add" algorithm which can be used
to concateneted IPv6 addresses from prefix and suffix.

Since it's available upstream and our string manipulation is ugly,
let's replace our IP concatenation with that tool. The package
consists of a single .c file with about 1000 lines resulting in
about 4 kB for the ipk package.

This patch does _not_ introduce any conceptual changes yet. Thus,
the "wrong" IPv6 prefix from KeyXchange will be expected in the
same format, it is just healed for the new code for now.

The change allows to get rid of some bloat, i.e. some quite trivial
custom functions on the way. This also drops the ipTidyColon()
function, as owipcalc seems to return the collapsed version by default.

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
Reviewed-by: Fabian Bläse <fabian@blaese.de>
This commit is contained in:
Adrian Schmutzler 2021-01-13 21:12:54 +01:00
parent f8b93325f2
commit 0092713196
7 changed files with 25 additions and 74 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)
@ -22,6 +22,7 @@ define Package/$(PKG_NAME)
+fff-web-hood \
+fff-wireless \
+jshn \
+owipcalc \
+@BUSYBOX_CONFIG_WGET \
+@BUSYBOX_CONFIG_FEATURE_WGET_TIMEOUT
endef

View File

@ -189,12 +189,13 @@ if [ -s "$hoodfiletmp" ]; then
json_get_var prefix ula_prefix
# Set $prefix::MAC as IP
if [ -n "$prefix" ] ; then
prefix="$(echo "$prefix" | sed -e 's,\\,,')"
mac="$(cat "/sys/class/net/br-client/address")"
addr="$(ipMacAssemble "$prefix" "$mac")"
addr="$(ipTidyColon "$addr")"
addr_eui="$(ipEUIAssemble "$prefix" "$mac")"
addr_eui="$(ipTidyColon "$addr_eui")"
# remove escape character
prefix=$(echo "$prefix" | sed -e 's,\\,,')
# create proper prefix
prefix=$(echo "$prefix" | sed -e 's,/,:/,')
mac=$(cat "/sys/class/net/br-client/address")
addr=$(owipcalc "$prefix" add "::$(ipMacSuffix "$mac")")
addr_eui=$(owipcalc "$prefix" add "::$(ipEUISuffix "$mac")")
for ip in $(ip -6 addr show dev br-client | grep inet6 | grep -v -e " $addr" -e " $addr_eui" -e " fe80::" -e " fdff::" | cut -f6 -d " "); do
ip -6 addr del "$ip" dev br-client
done

View File

@ -1,7 +1,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=fff-network
PKG_RELEASE:=27
PKG_RELEASE:=28
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
@ -12,7 +12,10 @@ define Package/$(PKG_NAME)
CATEGORY:=Freifunk
TITLE:=Freifunk-Franken network configuration
URL:=http://www.freifunk-franken.de
DEPENDS:=+fff-boardname +fff-firewall
DEPENDS:= \
+owipcalc \
+fff-boardname \
+fff-firewall
endef
define Package/$(PKG_NAME)/description

View File

@ -29,62 +29,6 @@ ipEUISuffix() {
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::)
[ $# -ne "2" ] && return 1
local prefix=$1
local suffix=$2
echo "$prefix" | sed -e 's,/,'$suffix'/,'
return 0
}
ipMacAssemble() {
# Concatenates a prefix (1st argument) and a MAC-based suffix (MAC address as 2nd argument) to a merged IPv6 address
# (Details on prefix: Check ipAssemble(), details on suffix: Check ipMacSuffix())
[ $# -ne "2" ] && return 1
local prefix=$1
local mac=$2
suffix="$(ipMacSuffix "$mac")"
ipAssemble "$prefix" "$suffix"
return 0
}
ipEUIAssemble() {
# Concatenates a prefix (1st argument) and an EUI-based suffix (MAC address as 2nd argument) to a merged IPv6 address
# (Details on prefix: Check ipAssemble(), details on suffix: Check ipEUISuffix())
[ $# -ne "2" ] && return 1
local prefix=$1
local mac=$2
suffix="$(ipEUISuffix "$mac")"
ipAssemble "$prefix" "$suffix"
return 0
}
ipTidyColon() {
# Collapses zeros to :: to yield short but valid IPv6 addresses
#
# Argument: IPv6 address to be shortened
[ $# -ne "1" ] && return 1
local addr=$1
echo "$addr" | grep -q "::" || addr="$(echo "$addr" | sed -e 's,:[0:]*:,::,')"
echo "$addr"
return 0
}
macFlipLocalBit() {
# Returns given MAC-address with locally administered bit flipped
#

View File

@ -195,9 +195,9 @@ else
ip -6 addr del $ip dev br-client
done
prefix="fdff:0::/64"
prefix="fdff::/64"
# Set $prefix::MAC as IP
addr="$(ipMacAssemble "$prefix" "$ROUTERMAC")"
addr=$(owipcalc "$prefix" add "::$(ipMacSuffix "$ROUTERMAC")")
ip -6 addr add $addr dev br-client
uci -q del network.globals
@ -207,12 +207,12 @@ else
uci -q set network.client.proto=static
# Set $prefix::1 as IP
addr="$(ipAssemble "$prefix" "1")"
addr=$(owipcalc "$prefix" add ::1)
ip -6 addr add $addr dev br-client
uci -q add_list network.client.ip6addr=$addr
# Set $prefix::link-local as IP
addr="$(ipEUIAssemble "$prefix" "$ROUTERMAC")"
addr=$(owipcalc "$prefix" add "::$(ipEUISuffix "$ROUTERMAC")")
ip -6 addr add $addr dev br-client
uci -q add_list network.client.ip6addr=$addr

View File

@ -1,7 +1,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=fff-wireguard
PKG_RELEASE:=4
PKG_RELEASE:=5
PKG_BUILD_DIR:=$(BUILD_DIR)/fff-wireguard
@ -12,9 +12,11 @@ define Package/fff-wireguard
CATEGORY:=Freifunk
TITLE:=Freifunk-Franken wireguard
URL:=https://www.freifunk-franken.de
DEPENDS:=+wireguard \
+fff-network \
+fff-babeld
DEPENDS:= \
+owipcalc \
+wireguard \
+fff-babeld \
+fff-network
endef
define Package/fff-wireguard/description

View File

@ -116,7 +116,7 @@ configure() {
uci -q del network.$prefixname.addresses
# add link local address
uci add_list network.$prefixname.addresses="$(ipEUIAssemble "fe80::/64" "$ROUTERMAC")"
uci add_list network.$prefixname.addresses="$(owipcalc "fe80::/64" add "::$(ipEUISuffix "$ROUTERMAC")")"
# add peer_ip
babel_add_peeraddr "network.$prefixname.addresses"