Skip to content

Commit

Permalink
selftests: forwarding: lib: Add helpers for IP address handling
Browse files Browse the repository at this point in the history
In order to generate IGMPv3 and MLDv2 packets on the fly, we will need
helpers to expand IPv4 and IPv6 addresses given as parameters in
mausezahn payload notation. Add helpers that do it.

Signed-off-by: Petr Machata <petrm@nvidia.com>
Acked-by: Nikolay Aleksandrov <razor@blackwall.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Petr Machata authored and David S. Miller committed Feb 6, 2023
1 parent f7ccf60 commit fcf4927
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tools/testing/selftests/net/forwarding/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,43 @@ hw_stats_monitor_test()
log_test "${type}_stats notifications"
}

ipv4_to_bytes()
{
local IP=$1; shift

printf '%02x:' ${IP//./ } |
sed 's/:$//'
}

# Convert a given IPv6 address, `IP' such that the :: token, if present, is
# expanded, and each 16-bit group is padded with zeroes to be 4 hexadecimal
# digits. An optional `BYTESEP' parameter can be given to further separate
# individual bytes of each 16-bit group.
expand_ipv6()
{
local IP=$1; shift
local bytesep=$1; shift

local cvt_ip=${IP/::/_}
local colons=${cvt_ip//[^:]/}
local allcol=:::::::
# IP where :: -> the appropriate number of colons:
local allcol_ip=${cvt_ip/_/${allcol:${#colons}}}

echo $allcol_ip | tr : '\n' |
sed s/^/0000/ |
sed 's/.*\(..\)\(..\)/\1'"$bytesep"'\2/' |
tr '\n' : |
sed 's/:$//'
}

ipv6_to_bytes()
{
local IP=$1; shift

expand_ipv6 "$IP" :
}

igmpv3_is_in_get()
{
local igmpv3
Expand Down

0 comments on commit fcf4927

Please sign in to comment.