From: Michael Tremer Date: Mon, 17 Sep 2018 13:29:32 +0000 (+0200) Subject: ip-tunnel: Create a function that determines if all IP addresses match X-Git-Tag: 010~85 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2a588cab6ed605e0282516625b60248c9a7df7d1;p=network.git ip-tunnel: Create a function that determines if all IP addresses match Signed-off-by: Michael Tremer --- diff --git a/src/functions/functions.ip b/src/functions/functions.ip index 70bd92c0..f20a3d7a 100644 --- a/src/functions/functions.ip +++ b/src/functions/functions.ip @@ -65,6 +65,26 @@ ip_protocol_is_supported() { list_match ${proto} ${IP_SUPPORTED_PROTOCOLS} } +# Returns true if all IP addresses are of the same protocol +ip_protocol_match() { + local address="${1}" + shift + + # Get protocol of the first address + local protocol="$(ip_detect_protocol "${address}")" + + # Check if all other addresses match the protocol + for address in $@; do + local p="$(ip_detect_protocol "${address}")" + + if [ "${p}" != "${protocol}" ]; then + return ${EXIT_FALSE} + fi + done + + return ${EXIT_TRUE} +} + ip_is_valid() { local address=${1} assert isset address diff --git a/src/functions/functions.ip-tunnel b/src/functions/functions.ip-tunnel index 38164896..69377aea 100644 --- a/src/functions/functions.ip-tunnel +++ b/src/functions/functions.ip-tunnel @@ -98,21 +98,9 @@ ip_tunnel_add() { return ${EXIT_ERROR} fi - # Detect the IP protocol, which is important to decide which mode we have to use - local remote_address_protocol="$(ip_detect_protocol "${remote_address}")" - - # If we could not detect the IP protocol something with - # ${remote_address} is wrong - if ! isset remote_address_protocol; then - log ERROR "Could not determine remote address IP protocol" - return ${EXIT_ERROR} - fi - # We cannot mix IPv6 and IPv4 - if isset local_address && \ - [[ "${remote_address_protocol}" != "$(ip_detect_protocol "${local_address}")" ]] ; then - log ERROR "Local and remote address\ - are not from the same IP protocol" + if isset local_address && ! ip_protocol_match "${remote_address}" "${local_address}"; then + log ERROR "Local and remote address are not of the same IP protocol" return ${EXIT_ERROR} fi @@ -151,6 +139,7 @@ ip_tunnel_add() { fi # Determine the mode based on the IP protocol + local remote_address_protocol="$(ip_detect_protocol "${remote_address}")" mode=$(ip_tunnel_convert_mode_to_iproute2_mode "${mode}" "${remote_address_protocol}") log DEBUG "Creating tunnel device '${device}' (mode=${mode})..."