From 9caffc01c23e945adc2922f18ba0e1e304fd6c45 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Thu, 20 Aug 2020 02:59:43 -0600 Subject: [PATCH 1/6] static executor: if vrf-table is set, apply the static route to a specific table --- executor-scripts/linux/static | 5 ++++- tests/fixtures/vrf.interfaces | 1 + tests/linux/static_test | 18 +++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/executor-scripts/linux/static b/executor-scripts/linux/static index d7e3ee3..e14ca6b 100755 --- a/executor-scripts/linux/static +++ b/executor-scripts/linux/static @@ -1,5 +1,8 @@ #!/bin/sh +VRF_TABLE="" +[ -n "$IF_VRF_TABLE" ] && VRF_TABLE="table $IF_VRF_TABLE" + addr_family() { if [ "$1" != "${1#*[0-9].[0-9]}" ]; then echo "-4" @@ -20,7 +23,7 @@ configure_addresses() { configure_gateways() { for i in $(ifquery -p gateway -i $INTERFACES_FILE $IFACE); do addrfam=$(addr_family $i) - ${MOCK} ip $addrfam route $1 default via $i + ${MOCK} ip $addrfam route $1 default via $i $VRF_TABLE done } diff --git a/tests/fixtures/vrf.interfaces b/tests/fixtures/vrf.interfaces index bfcf11b..fc40df2 100644 --- a/tests/fixtures/vrf.interfaces +++ b/tests/fixtures/vrf.interfaces @@ -1,5 +1,6 @@ iface vrf-red vrf-table 1 + gateway 203.0.113.2 auto eth0 iface eth0 diff --git a/tests/linux/static_test b/tests/linux/static_test index a07ea8e..79bf820 100755 --- a/tests/linux/static_test +++ b/tests/linux/static_test @@ -4,7 +4,7 @@ EXECUTOR="$(atf_get_srcdir)/../../executor-scripts/linux/static" FIXTURES="$(atf_get_srcdir)/../fixtures" -tests_init up down +tests_init up down vrf_up vrf_down up_body() { export IFACE=eth0 PHASE=up MOCK=echo INTERFACES_FILE="$FIXTURES/static-eth0.interfaces" @@ -25,3 +25,19 @@ down_body() { -o match:'route del default via 2001:db8:1000:2::1' \ ${EXECUTOR} } + +vrf_up_body() { + export IFACE=vrf-red PHASE=up MOCK=echo INTERFACES_FILE="$FIXTURES/vrf.interfaces" \ + IF_VRF_TABLE=1 + atf_check -s exit:0 \ + -o match:'route add default via 203.0.113.2 table 1' \ + ${EXECUTOR} +} + +vrf_down_body() { + export IFACE=vrf-red PHASE=down MOCK=echo INTERFACES_FILE="$FIXTURES/vrf.interfaces" \ + IF_VRF_TABLE=1 + atf_check -s exit:0 \ + -o match:'route del default via 203.0.113.2 table 1' \ + ${EXECUTOR} +} From d463d455da3a7360f39e8ff60d0737ca61c04996 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Thu, 20 Aug 2020 03:08:29 -0600 Subject: [PATCH 2/6] simplify executor `use` statements --- libifupdown/interface-file.c | 12 ++++++------ libifupdown/interface.c | 23 +++++++++++++---------- libifupdown/interface.h | 1 + 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c index 674cecd..bd6e0ba 100644 --- a/libifupdown/interface-file.c +++ b/libifupdown/interface-file.c @@ -125,11 +125,11 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) if (!strcmp(token, "dhcp")) { cur_iface->is_dhcp = true; - lif_dict_add(&cur_iface->vars, "use", strdup("dhcp")); + lif_interface_use_executor(cur_iface, "dhcp"); } else if (!strcmp(token, "ppp")) { - lif_dict_add(&cur_iface->vars, "use", strdup("ppp")); + lif_interface_use_executor(cur_iface, "ppp"); } else if (!strcmp(token, "inherits")) { @@ -176,7 +176,7 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) else if (!strcmp(executor, "link")) continue; - lif_dict_add(&cur_iface->vars, token, strdup(executor)); + lif_interface_use_executor(cur_iface, executor); } else if (!strcmp(token, "inherit")) { @@ -214,12 +214,12 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) /* Check if token looks like - and assume is an addon */ char *word_end = strchr(token, '-'); - if (word_end) + if (word_end != NULL) { /* Copy word1 to not mangle *token */ char *addon = strndup(token, word_end - token); - if (lif_dict_add_once(&cur_iface->vars, "use", addon, (lif_dict_cmp_t) strcmp) == NULL) - free(addon); + lif_interface_use_executor(cur_iface, addon); + free(addon); /* pass requires as compatibility env vars to appropriate executors (bridge, bond) */ if (!strcmp(addon, "dhcp")) diff --git a/libifupdown/interface.c b/libifupdown/interface.c index 8841c13..3f3bb69 100644 --- a/libifupdown/interface.c +++ b/libifupdown/interface.c @@ -63,10 +63,7 @@ lif_interface_init(struct lif_interface *interface, const char *ifname) interface->ifname = strdup(ifname); - if (strchr(ifname, '.') == NULL) - lif_dict_add(&interface->vars, "use", strdup("link")); - else - lif_dict_add(&interface->vars, "use", strdup("vlan")); + lif_interface_use_executor(interface, strchr(ifname, '.') == NULL ? "link" : "vlan"); } bool @@ -80,11 +77,8 @@ lif_interface_address_add(struct lif_interface *interface, const char *address) return false; } - if (!interface->is_static) - { - lif_dict_add(&interface->vars, "use", strdup("static")); - interface->is_static = true; - } + lif_interface_use_executor(interface, "static"); + interface->is_static = true; lif_dict_add(&interface->vars, "address", addr); @@ -137,6 +131,15 @@ lif_interface_fini(struct lif_interface *interface) free(interface->ifname); } +void +lif_interface_use_executor(struct lif_interface *interface, const char *executor) +{ + char *exec_addon = strdup(executor); + + if (lif_dict_add_once(&interface->vars, "use", exec_addon, (lif_dict_cmp_t) strcmp) == NULL) + free(exec_addon); +} + void lif_interface_collection_init(struct lif_dict *collection) { @@ -147,7 +150,7 @@ lif_interface_collection_init(struct lif_dict *collection) /* always enable loopback interface as part of a collection */ if_lo = lif_interface_collection_find(collection, "lo"); if_lo->is_auto = true; - lif_dict_add(&if_lo->vars, "use", strdup("loopback")); + lif_interface_use_executor(if_lo, "loopback"); } void diff --git a/libifupdown/interface.h b/libifupdown/interface.h index 045f848..8b808b3 100644 --- a/libifupdown/interface.h +++ b/libifupdown/interface.h @@ -68,6 +68,7 @@ extern void lif_interface_init(struct lif_interface *interface, const char *ifna extern bool lif_interface_address_add(struct lif_interface *interface, const char *address); extern void lif_interface_address_delete(struct lif_interface *interface, const char *address); extern void lif_interface_fini(struct lif_interface *interface); +extern void lif_interface_use_executor(struct lif_interface *interface, const char *executor); extern void lif_interface_collection_init(struct lif_dict *collection); extern void lif_interface_collection_fini(struct lif_dict *collection); From 846df3272f3117cad0adccd12a772c808c540023 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Thu, 20 Aug 2020 03:10:26 -0600 Subject: [PATCH 3/6] remove lif_interface.is_static, no longer used --- libifupdown/interface-file.c | 5 ----- libifupdown/interface.c | 2 -- libifupdown/interface.h | 1 - 3 files changed, 8 deletions(-) diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c index bd6e0ba..5508715 100644 --- a/libifupdown/interface-file.c +++ b/libifupdown/interface-file.c @@ -168,11 +168,6 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) cur_iface->is_bridge = true; else if (!strcmp(executor, "bond")) cur_iface->is_bond = true; - else if (!strcmp(executor, "static")) - { - cur_iface->is_static = true; - continue; - } else if (!strcmp(executor, "link")) continue; diff --git a/libifupdown/interface.c b/libifupdown/interface.c index 3f3bb69..c25591d 100644 --- a/libifupdown/interface.c +++ b/libifupdown/interface.c @@ -78,7 +78,6 @@ lif_interface_address_add(struct lif_interface *interface, const char *address) } lif_interface_use_executor(interface, "static"); - interface->is_static = true; lif_dict_add(&interface->vars, "address", addr); @@ -231,7 +230,6 @@ lif_interface_collection_inherit(struct lif_interface *interface, struct lif_dic return false; lif_dict_add(&interface->vars, "inherit", strdup(ifname)); - interface->is_static = parent->is_static; interface->is_dhcp = parent->is_dhcp; interface->is_bond = parent->is_bond; interface->is_bridge = parent->is_bridge; diff --git a/libifupdown/interface.h b/libifupdown/interface.h index 8b808b3..a562d63 100644 --- a/libifupdown/interface.h +++ b/libifupdown/interface.h @@ -51,7 +51,6 @@ struct lif_interface { bool is_auto; bool is_bridge; bool is_bond; - bool is_static; struct lif_dict vars; From e5c592f2c1bcd5bd657a5aeb92ff0cb80fa82fe7 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Thu, 20 Aug 2020 03:14:57 -0600 Subject: [PATCH 4/6] remove lif_interface.is_dhcp too --- libifupdown/interface-file.c | 15 ++------------- libifupdown/interface.c | 1 - libifupdown/interface.h | 1 - libifupdown/lifecycle.c | 5 +---- 4 files changed, 3 insertions(+), 19 deletions(-) diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c index 5508715..4eaedd1 100644 --- a/libifupdown/interface-file.c +++ b/libifupdown/interface-file.c @@ -123,14 +123,9 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) while (*token) { if (!strcmp(token, "dhcp")) - { - cur_iface->is_dhcp = true; lif_interface_use_executor(cur_iface, "dhcp"); - } else if (!strcmp(token, "ppp")) - { lif_interface_use_executor(cur_iface, "ppp"); - } else if (!strcmp(token, "inherits")) { token = lif_next_token(&bufp); @@ -162,14 +157,10 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) } /* pass requires as compatibility env vars to appropriate executors (bridge, bond) */ - if (!strcmp(executor, "dhcp")) - cur_iface->is_dhcp = true; - else if (!strcmp(executor, "bridge")) + if (!strcmp(executor, "bridge")) cur_iface->is_bridge = true; else if (!strcmp(executor, "bond")) cur_iface->is_bond = true; - else if (!strcmp(executor, "link")) - continue; lif_interface_use_executor(cur_iface, executor); } @@ -217,9 +208,7 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) free(addon); /* pass requires as compatibility env vars to appropriate executors (bridge, bond) */ - if (!strcmp(addon, "dhcp")) - cur_iface->is_dhcp = true; - else if (!strcmp(addon, "bridge")) + if (!strcmp(addon, "bridge")) cur_iface->is_bridge = true; else if (!strcmp(addon, "bond")) cur_iface->is_bond = true; diff --git a/libifupdown/interface.c b/libifupdown/interface.c index c25591d..fe9acea 100644 --- a/libifupdown/interface.c +++ b/libifupdown/interface.c @@ -230,7 +230,6 @@ lif_interface_collection_inherit(struct lif_interface *interface, struct lif_dic return false; lif_dict_add(&interface->vars, "inherit", strdup(ifname)); - interface->is_dhcp = parent->is_dhcp; interface->is_bond = parent->is_bond; interface->is_bridge = parent->is_bridge; diff --git a/libifupdown/interface.h b/libifupdown/interface.h index a562d63..3f2c287 100644 --- a/libifupdown/interface.h +++ b/libifupdown/interface.h @@ -47,7 +47,6 @@ extern bool lif_address_unparse(const struct lif_address *address, char *buf, si struct lif_interface { char *ifname; - bool is_dhcp; bool is_auto; bool is_bridge; bool is_bond; diff --git a/libifupdown/lifecycle.c b/libifupdown/lifecycle.c index dc31474..d2a32e3 100644 --- a/libifupdown/lifecycle.c +++ b/libifupdown/lifecycle.c @@ -104,10 +104,7 @@ build_environment(char **envp[], const struct lif_execute_opts *opts, struct lif lif_environment_push(envp, "IFACE", lifname); lif_environment_push(envp, "PHASE", phase); lif_environment_push(envp, "MODE", mode); - - /* try to provide $METHOD for ifupdown1 scripts if we can */ - if (iface->is_dhcp) - lif_environment_push(envp, "METHOD", "dhcp"); + lif_environment_push(envp, "METHOD", "none"); if (opts->verbose) lif_environment_push(envp, "VERBOSE", "1"); From 0928f07af513276febad98b2da6890b8300badee Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Thu, 20 Aug 2020 03:20:07 -0600 Subject: [PATCH 5/6] interface-file: imply static executor use if gateway is specified --- libifupdown/interface-file.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c index 4eaedd1..1f2d19d 100644 --- a/libifupdown/interface-file.c +++ b/libifupdown/interface-file.c @@ -192,6 +192,19 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) lif_interface_address_add(cur_iface, addr); } + else if (!strcmp(token, "gateway")) + { + char *addr = lif_next_token(&bufp); + + if (cur_iface == NULL) + { + fprintf(stderr, "%s: gateway '%s' without interface\n", filename, addr); + goto parse_error; + } + + lif_interface_use_executor(cur_iface, "static"); + lif_dict_add(&cur_iface->vars, token, strdup(addr)); + } else if (cur_iface != NULL) { token = maybe_remap_token(token); From 4fdd91beee8bf7d659b4061854ab0153655c1761 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Thu, 20 Aug 2020 03:20:26 -0600 Subject: [PATCH 6/6] test: add test for vrf static route support --- tests/ifquery_test | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/ifquery_test b/tests/ifquery_test index 3c4361e..a53c22c 100755 --- a/tests/ifquery_test +++ b/tests/ifquery_test @@ -22,6 +22,7 @@ tests_init \ vrf_dependency \ vrf_ifupdown2_rewrite \ vrf_ifupdown2_dependency \ + vrf_implicit_static_gateway \ ppp_dependency \ ppp_legacy_rewrite \ tunnel_dependency \ @@ -139,6 +140,11 @@ vrf_ifupdown2_dependency_body() { ifquery -E $EXECUTORS_LINUX -i $FIXTURES/vrf-ifupdown2.interfaces eth0 } +vrf_implicit_static_gateway_body() { + atf_check -s exit:0 -o match:"use static" \ + ifquery -E $EXECUTORS_LINUX -i $FIXTURES/vrf.interfaces vrf-red +} + ppp_dependency_body() { atf_check -s exit:0 -o match:"requires eth0" \ ifquery -E $EXECUTORS_LINUX -i $FIXTURES/ppp.interfaces ppp0