[1/5] ip-tunnel: add new function

Message ID 1520187899-5759-1-git-send-email-jonatan.schlag@ipfire.org
State Accepted
Commit 376629dceea324fb713e957fabe57e3d69ca4c3b
Headers
Series [1/5] ip-tunnel: add new function |

Commit Message

Jonatan Schlag March 5, 2018, 5:24 a.m. UTC
  To be undependent from the IP protocol we use, when we use tunnel modes
in our code, this function converts the modes
to the modes the iproute2 tool uses
which often depend on the IP protocol version.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
 src/functions/functions.ip-tunnel | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
  

Patch

diff --git a/src/functions/functions.ip-tunnel b/src/functions/functions.ip-tunnel
index 7bb4e3f..550b0b3 100644
--- a/src/functions/functions.ip-tunnel
+++ b/src/functions/functions.ip-tunnel
@@ -21,6 +21,34 @@ 
 
 IP_TUNNEL_MODES="gre sit vti"
 
+# This function converts our modes into the type
+# the iproute2 tool uses
+ip_tunnel_convert_mode_to_iproute2_mode() {
+	local mode=${1}
+	local protocol=${2}
+
+	if ! isset mode || ! isset protocol; then
+		log ERROR "Did not get mode and/or protocol"
+		return ${EXIT_ERROR}
+	fi
+
+	if [[ "${protocol}" = "ipv4" ]]; then
+		# When we use IPv4 we can use our modes
+		echo "${mode}"
+	fi
+
+	if [[ "${protocol}" = "ipv6" ]]; then
+		# When we use IPv6 we have to convert
+		case "${mode}" in
+			"vti")
+				echo "vti6"
+				;;
+			"gre")
+				echo "ip6gre"
+		esac
+	fi
+}
+
 ip_tunnel_add() {
 	local device=${1}
 	shift