forked from freifunk-franken/firmware
fff-network/fff-hoods: Create functions for IPv6 rewriting
This puts the code for creating IPv6 addresses from MAC address and EUI into functions, so it can be reused. This should be particularly helpful if code is rewritten later. Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de> Tested-by: Adrian Schmutzler <freifunk@adrianschmutzler.de> Reviewed-by: Tim Niemeyer <tim@tn-x.org>
This commit is contained in:
parent
85510f25b3
commit
566d762148
|
@ -2,6 +2,7 @@
|
|||
|
||||
. /usr/share/libubox/jshn.sh
|
||||
. /lib/functions/fff/keyxchange
|
||||
. /lib/functions/fff/network
|
||||
. /lib/functions/fff/wireless
|
||||
. /lib/functions/fff/timeserver
|
||||
|
||||
|
@ -242,12 +243,10 @@ if [ -s "$hoodfile" ]; then
|
|||
# Set $prefix::MAC as IP
|
||||
if [ -n "$prefix" ] ; then
|
||||
prefix="$(echo "$prefix" | sed -e 's,\\,,')"
|
||||
suffix="$(awk -F: '{ print "0:"$1$2":"$3$4":"$5$6 }' /sys/class/net/br-mesh/address)"
|
||||
suffix_eui="$(awk -F: '{ printf("%02x%s:%sff:fe%s:%s%s\n", xor(("0x"$1),2), $2, $3, $4, $5, $6) }' /sys/class/net/br-mesh/address)"
|
||||
addr="$(echo "$prefix" | sed -e 's,/,'$suffix'/,')"
|
||||
echo $addr | grep -q "::" || addr="$(echo "$addr" | sed -e 's,:[0:]*:,::,')"
|
||||
addr_eui="$(echo "$prefix" | sed -e 's,/,'$suffix_eui'/,')"
|
||||
echo $addr_eui | grep -q "::" || addr_eui="$(echo "$addr_eui" | sed -e 's,:[0:]*:,::,')"
|
||||
addr="$(ipMacAssemble "$prefix" "br-mesh")"
|
||||
addr="$(ipTidyColon "$addr")"
|
||||
addr_eui="$(ipEUIAssemble "$prefix" "br-mesh")"
|
||||
addr_eui="$(ipTidyColon "$addr_eui")"
|
||||
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
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
#!/bin/sh
|
||||
# Copyright 2017 Adrian Schmutzler
|
||||
# License GPLv3
|
||||
|
||||
ipMacSuffix() {
|
||||
# Returns the lower 64 bits of an IPv6 address (0:aabb:ccdd:eeff)
|
||||
# based on the mac address (aa:bb:cc:bb:ee:ff) of a specified interface
|
||||
#
|
||||
# Argument: name of interface (e.g. br-mesh)
|
||||
|
||||
[ $# -ne "1" ] && return 1
|
||||
|
||||
local iface=$1
|
||||
|
||||
awk -F: '{ print "0:"$1$2":"$3$4":"$5$6 }' "/sys/class/net/${iface}/address"
|
||||
return 0
|
||||
}
|
||||
|
||||
ipEUISuffix() {
|
||||
# Returns the EUI (interface ID, a8bb:ccff:fedd:eeff)
|
||||
# based on the mac address (aa:bb:cc:bb:ee:ff) of a specified interface
|
||||
#
|
||||
# Argument: name of interface (e.g. br-mesh)
|
||||
|
||||
[ $# -ne "1" ] && return 1
|
||||
|
||||
local iface=$1
|
||||
|
||||
awk -F: '{ printf("%02x%s:%sff:fe%s:%s%s\n", xor(("0x"$1),2), $2, $3, $4, $5, $6) }' "/sys/class/net/${iface}/address"
|
||||
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 (interface 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 iface=$2
|
||||
|
||||
suffix="$(ipMacSuffix "$iface")"
|
||||
ipAssemble "$prefix" "$suffix"
|
||||
return 0
|
||||
}
|
||||
|
||||
ipEUIAssemble() {
|
||||
# Concatenates a prefix (1st argument) and an EUI-based suffix (interface 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 iface=$2
|
||||
|
||||
suffix="$(ipEUISuffix "$iface")"
|
||||
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
|
||||
}
|
|
@ -9,6 +9,8 @@
|
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
|
||||
. /lib/functions/fff/network
|
||||
|
||||
BOARD="$(uci get board.model.name)"
|
||||
. /etc/network.$BOARD
|
||||
|
||||
|
@ -218,8 +220,7 @@ else
|
|||
|
||||
prefix="fdff:0::/64"
|
||||
# Set $prefix::MAC as IP
|
||||
suffix=$(awk -F: '{ print $1$2":"$3$4":"$5$6 }' /sys/class/net/br-mesh/address)
|
||||
addr=$(echo $prefix | sed -e 's/\//'$suffix'\//')
|
||||
addr="$(ipMacAssemble "$prefix" "br-mesh")"
|
||||
ip -6 addr add $addr dev br-mesh
|
||||
|
||||
uci -q del network.globals
|
||||
|
@ -229,14 +230,12 @@ else
|
|||
uci -q set network.mesh.proto=static
|
||||
|
||||
# Set $prefix::1 as IP
|
||||
suffix="1"
|
||||
addr=$(echo $prefix | sed -e 's/\//'$suffix'\//')
|
||||
addr="$(ipAssemble "$prefix" "1")"
|
||||
ip -6 addr add $addr dev br-mesh
|
||||
uci -q add_list network.mesh.ip6addr=$addr
|
||||
|
||||
# Set $prefix::link-local as IP
|
||||
suffix=$(awk -F: '{ printf("%02x%s:%sff:fe%s:%s%s\n", xor(("0x"$1),2), $2, $3, $4, $5, $6) }' /sys/class/net/br-mesh/address)
|
||||
addr=$(echo $prefix | sed -e 's/\//'$suffix'\//')
|
||||
addr="$(ipEUIAssemble "$prefix" "br-mesh")"
|
||||
ip -6 addr add $addr dev br-mesh
|
||||
uci -q add_list network.mesh.ip6addr=$addr
|
||||
|
||||
|
|
Loading…
Reference in New Issue