]> git.ipfire.org Git - network.git/commitdiff
ip-tunnel: Create a function that determines if all IP addresses match
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 17 Sep 2018 13:29:32 +0000 (15:29 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 17 Sep 2018 13:29:32 +0000 (15:29 +0200)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/functions/functions.ip
src/functions/functions.ip-tunnel

index 70bd92c036c95835a37c74f8d311aa1882730d07..f20a3d7adb15e458acb026d0749ae37ef7c22bf7 100644 (file)
@@ -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
index 38164896309044de6bc8d28e8da46b7872c3e893..69377aea1d801891976596c8488cc6000ceb8eee 100644 (file)
@@ -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})..."