From 9cc74b58c40c27967bf145c4b217e417228ed583 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Wed, 19 Aug 2020 02:46:04 -0600 Subject: [PATCH 1/9] interface-file: remap legacy ifupdown & ifupdown2 tunnel parameters --- executor-scripts/linux/tunnel | 30 ++++++++++++++++++++++++++++++ libifupdown/interface-file.c | 7 ++++++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100755 executor-scripts/linux/tunnel diff --git a/executor-scripts/linux/tunnel b/executor-scripts/linux/tunnel new file mode 100755 index 0000000..07c6141 --- /dev/null +++ b/executor-scripts/linux/tunnel @@ -0,0 +1,30 @@ +#!/bin/sh + +# Based on alpine's tunnel configuration script. +# Copyright (c) 2017 Kaarle Ritvanen + +PARAMS=$(set | sed -E ' + s/^IF_TUNNEL_([A-Z0-9_]+)=(.+)/\1\n\2/ + ta + d + :a + h + y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ + P + g + s/.*\n// + ') + +[ "$PARAMS" ] || exit 0 + +case "$PHASE" in +pre-up) + eval ip tunnel add $IFACE $PARAMS + ;; +post-down) + ip tunnel del $IFACE + ;; +depend) + echo "$IF_TUNNEL_DEV" + ;; +esac diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c index 82a3328..69a7b42 100644 --- a/libifupdown/interface-file.c +++ b/libifupdown/interface-file.c @@ -24,9 +24,14 @@ struct remap_token { const char *alternative; }; +/* this list must be in alphabetical order for bsearch */ static const struct remap_token tokens[] = { + {"endpoint", "tunnel-remote"}, /* legacy ifupdown */ + {"local", "tunnel-local"}, /* legacy ifupdown */ {"provider", "ppp-provider"}, /* legacy ifupdown, ifupdown2 */ - {"vrf", "vrf-member"}, + {"tunnel-endpoint", "tunnel-remote"}, /* ifupdown2 */ + {"tunnel-physdev", "tunnel-dev"}, /* ifupdown2 */ + {"vrf", "vrf-member"}, /* ifupdown2 */ }; static int From 36271e86576744fc53084f50c41166b020d8b220 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Wed, 19 Aug 2020 02:56:02 -0600 Subject: [PATCH 2/9] build: install tunnel executor by default --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3874745..77778ba 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,8 @@ EXECUTOR_SCRIPTS_CORE ?= \ EXECUTOR_SCRIPTS_OPT ?= \ bridge \ - vrf + vrf \ + tunnel EXECUTOR_SCRIPTS ?= ${EXECUTOR_SCRIPTS_CORE} ${EXECUTOR_SCRIPTS_OPT} From a4f147b5d01bf5b235198fd104d4d59c151ed97a Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Wed, 19 Aug 2020 03:00:17 -0600 Subject: [PATCH 3/9] tests: add some tunnel configuration fixtures --- tests/fixtures/tunnel-ifupdown2.interfaces | 10 ++++++++++ tests/fixtures/tunnel-legacy.interfaces | 10 ++++++++++ tests/fixtures/tunnel.interfaces | 10 ++++++++++ 3 files changed, 30 insertions(+) create mode 100644 tests/fixtures/tunnel-ifupdown2.interfaces create mode 100644 tests/fixtures/tunnel-legacy.interfaces create mode 100644 tests/fixtures/tunnel.interfaces diff --git a/tests/fixtures/tunnel-ifupdown2.interfaces b/tests/fixtures/tunnel-ifupdown2.interfaces new file mode 100644 index 0000000..82a36c5 --- /dev/null +++ b/tests/fixtures/tunnel-ifupdown2.interfaces @@ -0,0 +1,10 @@ +auto tun0 +iface tun0 + tunnel-local 203.0.113.2 + tunnel-endpoint 203.0.113.1 + tunnel-physdev eth0 + address 203.0.113.10/30 + gateway 203.0.113.9 + +iface eth0 + address 203.0.113.2/30 diff --git a/tests/fixtures/tunnel-legacy.interfaces b/tests/fixtures/tunnel-legacy.interfaces new file mode 100644 index 0000000..ff6505c --- /dev/null +++ b/tests/fixtures/tunnel-legacy.interfaces @@ -0,0 +1,10 @@ +auto tun0 +iface tun0 + local 203.0.113.2 + endpoint 203.0.113.1 + tunnel-dev eth0 + address 203.0.113.10/30 + gateway 203.0.113.9 + +iface eth0 + address 203.0.113.2/30 diff --git a/tests/fixtures/tunnel.interfaces b/tests/fixtures/tunnel.interfaces new file mode 100644 index 0000000..586b401 --- /dev/null +++ b/tests/fixtures/tunnel.interfaces @@ -0,0 +1,10 @@ +auto tun0 +iface tun0 + tunnel-local 203.0.113.2 + tunnel-remote 203.0.113.1 + tunnel-dev eth0 + address 203.0.113.10/30 + gateway 203.0.113.9 + +iface eth0 + address 203.0.113.2/30 From c24143982f64a35a57498badec1361b0c0579a98 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Wed, 19 Aug 2020 03:12:43 -0600 Subject: [PATCH 4/9] interface-file: map 'mode' to tunnel-mode --- libifupdown/interface-file.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c index 69a7b42..fe862c3 100644 --- a/libifupdown/interface-file.c +++ b/libifupdown/interface-file.c @@ -28,6 +28,7 @@ struct remap_token { static const struct remap_token tokens[] = { {"endpoint", "tunnel-remote"}, /* legacy ifupdown */ {"local", "tunnel-local"}, /* legacy ifupdown */ + {"mode", "tunnel-mode"}, /* legacy ifupdown */ {"provider", "ppp-provider"}, /* legacy ifupdown, ifupdown2 */ {"tunnel-endpoint", "tunnel-remote"}, /* ifupdown2 */ {"tunnel-physdev", "tunnel-dev"}, /* ifupdown2 */ From 953666f1fb50a6b0955413a0748d18b21603ae2a Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Wed, 19 Aug 2020 03:13:02 -0600 Subject: [PATCH 5/9] tests: add tests for managing the tunnel executor --- tests/fixtures/tunnel-ifupdown2.interfaces | 1 + tests/fixtures/tunnel-legacy.interfaces | 1 + tests/fixtures/tunnel.interfaces | 1 + tests/ifquery_test | 38 +++++++++++++++++++++- 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/fixtures/tunnel-ifupdown2.interfaces b/tests/fixtures/tunnel-ifupdown2.interfaces index 82a36c5..4e93b91 100644 --- a/tests/fixtures/tunnel-ifupdown2.interfaces +++ b/tests/fixtures/tunnel-ifupdown2.interfaces @@ -2,6 +2,7 @@ auto tun0 iface tun0 tunnel-local 203.0.113.2 tunnel-endpoint 203.0.113.1 + tunnel-mode gre tunnel-physdev eth0 address 203.0.113.10/30 gateway 203.0.113.9 diff --git a/tests/fixtures/tunnel-legacy.interfaces b/tests/fixtures/tunnel-legacy.interfaces index ff6505c..bb030bd 100644 --- a/tests/fixtures/tunnel-legacy.interfaces +++ b/tests/fixtures/tunnel-legacy.interfaces @@ -2,6 +2,7 @@ auto tun0 iface tun0 local 203.0.113.2 endpoint 203.0.113.1 + mode gre tunnel-dev eth0 address 203.0.113.10/30 gateway 203.0.113.9 diff --git a/tests/fixtures/tunnel.interfaces b/tests/fixtures/tunnel.interfaces index 586b401..4bf46de 100644 --- a/tests/fixtures/tunnel.interfaces +++ b/tests/fixtures/tunnel.interfaces @@ -2,6 +2,7 @@ auto tun0 iface tun0 tunnel-local 203.0.113.2 tunnel-remote 203.0.113.1 + tunnel-mode gre tunnel-dev eth0 address 203.0.113.10/30 gateway 203.0.113.9 diff --git a/tests/ifquery_test b/tests/ifquery_test index 67284ec..79f9341 100755 --- a/tests/ifquery_test +++ b/tests/ifquery_test @@ -23,7 +23,12 @@ tests_init \ vrf_ifupdown2_rewrite \ vrf_ifupdown2_dependency \ ppp_dependency \ - ppp_legacy_rewrite + ppp_legacy_rewrite \ + tunnel_dependency \ + tunnel_legacy_dependency \ + tunnel_legacy_rewrite \ + tunnel_ifupdown2_dependency \ + tunnel_ifupdown2_rewrite noargs_body() { atf_check -s exit:1 -e ignore ifquery -S/dev/null @@ -142,3 +147,34 @@ ppp_legacy_rewrite_body() { atf_check -s exit:0 -o match:"ppp-provider someisp" \ ifquery -E $EXECUTORS_LINUX -i $FIXTURES/ppp-legacy.interfaces ppp0 } + +tunnel_dependency_body() { + atf_check -s exit:0 -o match:"requires eth0" \ + ifquery -E $EXECUTORS_LINUX -i $FIXTURES/tunnel.interfaces tun0 +} + +tunnel_legacy_dependency_body() { + atf_check -s exit:0 -o match:"requires eth0" \ + ifquery -E $EXECUTORS_LINUX -i $FIXTURES/tunnel-legacy.interfaces tun0 +} + +tunnel_ifupdown2_dependency_body() { + atf_check -s exit:0 -o match:"requires eth0" \ + ifquery -E $EXECUTORS_LINUX -i $FIXTURES/tunnel-ifupdown2.interfaces tun0 +} + +tunnel_legacy_rewrite_body() { + atf_check -s exit:0 \ + -o match:"tunnel-local 203.0.113.2" \ + -o match:"tunnel-remote 203.0.113.1" \ + -o match:"tunnel-mode gre" \ + ifquery -E $EXECUTORS_LINUX -i $FIXTURES/tunnel-legacy.interfaces tun0 +} + +tunnel_ifupdown2_rewrite_body() { + atf_check -s exit:0 \ + -o match:"tunnel-local 203.0.113.2" \ + -o match:"tunnel-remote 203.0.113.1" \ + -o match:"tunnel-mode gre" \ + ifquery -E $EXECUTORS_LINUX -i $FIXTURES/tunnel-ifupdown2.interfaces tun0 +} From 5fb1b4b26ca4f1e71d973c8ece57364571897318 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Wed, 19 Aug 2020 03:13:56 -0600 Subject: [PATCH 6/9] tunnel executor: support mocking --- executor-scripts/linux/tunnel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/executor-scripts/linux/tunnel b/executor-scripts/linux/tunnel index 07c6141..8257a7f 100755 --- a/executor-scripts/linux/tunnel +++ b/executor-scripts/linux/tunnel @@ -19,10 +19,10 @@ PARAMS=$(set | sed -E ' case "$PHASE" in pre-up) - eval ip tunnel add $IFACE $PARAMS + ${MOCK} eval ip tunnel add $IFACE $PARAMS ;; post-down) - ip tunnel del $IFACE + ${MOCK} ip tunnel del $IFACE ;; depend) echo "$IF_TUNNEL_DEV" From 023805105217fbbd0bd09d356b53c91a9cb3ceb3 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Wed, 19 Aug 2020 03:22:44 -0600 Subject: [PATCH 7/9] tests: add tunnel executor tests --- tests/linux/Kyuafile | 1 + tests/linux/tunnel_test | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100755 tests/linux/tunnel_test diff --git a/tests/linux/Kyuafile b/tests/linux/Kyuafile index 471a8ff..99ad1e8 100644 --- a/tests/linux/Kyuafile +++ b/tests/linux/Kyuafile @@ -8,3 +8,4 @@ atf_test_program{name='dhcp_test'} atf_test_program{name='static_test'} atf_test_program{name='vrf_test'} atf_test_program{name='ppp_test'} +atf_test_program{name='tunnel_test'} diff --git a/tests/linux/tunnel_test b/tests/linux/tunnel_test new file mode 100755 index 0000000..c469a78 --- /dev/null +++ b/tests/linux/tunnel_test @@ -0,0 +1,30 @@ +#!/usr/bin/env atf-sh + +. $(atf_get_srcdir)/../test_env.sh +EXECUTOR="$(atf_get_srcdir)/../../executor-scripts/linux/tunnel" + +tests_init \ + tunnel_bringup \ + tunnel_teardown + +tunnel_bringup_body() { + export MOCK=echo IFACE=tun0 PHASE=pre-up IF_TUNNEL_TYPE=gre \ + IF_TUNNEL_LOCAL=1.2.3.4 IF_TUNNEL_REMOTE=5.6.7.8 \ + IF_TUNNEL_TTL=255 + atf_check -s exit:0 \ + -o match:"ip tunnel add tun0" \ + -o match:"type 'gre'" \ + -o match:"ttl '255'" \ + -o match:"local '1.2.3.4'" \ + -o match:"remote '5.6.7.8'" \ + ${EXECUTOR} +} + +tunnel_teardown_body() { + export MOCK=echo IFACE=tun0 PHASE=post-down IF_TUNNEL_TYPE=gre \ + IF_TUNNEL_LOCAL=1.2.3.4 IF_TUNNEL_REMOTE=5.6.7.8 \ + IF_TUNNEL_TTL=255 + atf_check -s exit:0 \ + -o match:"ip tunnel del tun0" \ + ${EXECUTOR} +} From 075352b631b1b768783b4191d74de1c86053f68a Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Wed, 19 Aug 2020 03:29:13 -0600 Subject: [PATCH 8/9] interface-file: remap ttl to tunnel-ttl --- libifupdown/interface-file.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c index fe862c3..674cecd 100644 --- a/libifupdown/interface-file.c +++ b/libifupdown/interface-file.c @@ -30,6 +30,7 @@ static const struct remap_token tokens[] = { {"local", "tunnel-local"}, /* legacy ifupdown */ {"mode", "tunnel-mode"}, /* legacy ifupdown */ {"provider", "ppp-provider"}, /* legacy ifupdown, ifupdown2 */ + {"ttl", "tunnel-ttl"}, /* legacy ifupdown */ {"tunnel-endpoint", "tunnel-remote"}, /* ifupdown2 */ {"tunnel-physdev", "tunnel-dev"}, /* ifupdown2 */ {"vrf", "vrf-member"}, /* ifupdown2 */ From 93116920da057df730d76b8026d0d50908045b8b Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Wed, 19 Aug 2020 03:32:10 -0600 Subject: [PATCH 9/9] tests: verify ttl is remapped to tunnel-ttl --- tests/fixtures/tunnel-ifupdown2.interfaces | 1 + tests/fixtures/tunnel-legacy.interfaces | 1 + tests/fixtures/tunnel.interfaces | 1 + tests/ifquery_test | 2 ++ 4 files changed, 5 insertions(+) diff --git a/tests/fixtures/tunnel-ifupdown2.interfaces b/tests/fixtures/tunnel-ifupdown2.interfaces index 4e93b91..f38e139 100644 --- a/tests/fixtures/tunnel-ifupdown2.interfaces +++ b/tests/fixtures/tunnel-ifupdown2.interfaces @@ -4,6 +4,7 @@ iface tun0 tunnel-endpoint 203.0.113.1 tunnel-mode gre tunnel-physdev eth0 + tunnel-ttl 255 address 203.0.113.10/30 gateway 203.0.113.9 diff --git a/tests/fixtures/tunnel-legacy.interfaces b/tests/fixtures/tunnel-legacy.interfaces index bb030bd..c20ecf7 100644 --- a/tests/fixtures/tunnel-legacy.interfaces +++ b/tests/fixtures/tunnel-legacy.interfaces @@ -4,6 +4,7 @@ iface tun0 endpoint 203.0.113.1 mode gre tunnel-dev eth0 + ttl 255 address 203.0.113.10/30 gateway 203.0.113.9 diff --git a/tests/fixtures/tunnel.interfaces b/tests/fixtures/tunnel.interfaces index 4bf46de..12a081b 100644 --- a/tests/fixtures/tunnel.interfaces +++ b/tests/fixtures/tunnel.interfaces @@ -4,6 +4,7 @@ iface tun0 tunnel-remote 203.0.113.1 tunnel-mode gre tunnel-dev eth0 + tunnel-ttl 255 address 203.0.113.10/30 gateway 203.0.113.9 diff --git a/tests/ifquery_test b/tests/ifquery_test index 79f9341..281f1e2 100755 --- a/tests/ifquery_test +++ b/tests/ifquery_test @@ -168,6 +168,7 @@ tunnel_legacy_rewrite_body() { -o match:"tunnel-local 203.0.113.2" \ -o match:"tunnel-remote 203.0.113.1" \ -o match:"tunnel-mode gre" \ + -o match:"tunnel-ttl 255" \ ifquery -E $EXECUTORS_LINUX -i $FIXTURES/tunnel-legacy.interfaces tun0 } @@ -176,5 +177,6 @@ tunnel_ifupdown2_rewrite_body() { -o match:"tunnel-local 203.0.113.2" \ -o match:"tunnel-remote 203.0.113.1" \ -o match:"tunnel-mode gre" \ + -o match:"tunnel-ttl 255" \ ifquery -E $EXECUTORS_LINUX -i $FIXTURES/tunnel-ifupdown2.interfaces tun0 }