From c4ac9e44a7e231647e96fb1b5887cfa41f213d00 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 14 Aug 2025 15:00:27 +0200 Subject: [PATCH] confd: new helper function, get all l3 interfaces Used by infix-firewall.c when figuring out interfaces that are not explicitly assigned to any zone. Placing them in the default zone Signed-off-by: Joachim Wiberg --- src/confd/src/ietf-interfaces.c | 66 +++++++++++++++++++++++++++++++++ src/confd/src/ietf-interfaces.h | 1 + 2 files changed, 67 insertions(+) diff --git a/src/confd/src/ietf-interfaces.c b/src/confd/src/ietf-interfaces.c index db017cc3..bd6e0df5 100644 --- a/src/confd/src/ietf-interfaces.c +++ b/src/confd/src/ietf-interfaces.c @@ -908,6 +908,72 @@ cleanup: return err; } +int ietf_interfaces_get_all_l3(const struct lyd_node *tree, char ***ifaces) +{ + struct lyd_node *interfaces, *cif; + char **names = NULL; + size_t capacity = 0; + size_t num = 0; + const char *ifname; + + if (!tree || !ifaces) + return -EINVAL; + + *ifaces = NULL; + + interfaces = lydx_get_descendant((struct lyd_node *)tree, "interfaces", "interface", NULL); + if (!interfaces) { + *ifaces = calloc(1, sizeof(char *)); + return *ifaces ? 0 : -ENOMEM; + } + + LYX_LIST_FOR_EACH(interfaces, cif, "interface") { + ifname = lydx_get_cattr(cif, "name"); + if (!ifname) + continue; + + if (is_member_port(cif)) + continue; + + if (iftype_from_iface(cif) == IFT_LO) + continue; + + if (lydx_get_child(cif, "container-network")) + continue; + + if (num + 1 >= capacity) { + capacity = capacity ? capacity * 2 : 8; + char **new_names = realloc(names, capacity * sizeof(char *)); + if (!new_names) { + for (size_t i = 0; i < num; i++) + free(names[i]); + free(names); + return -ENOMEM; + } + names = new_names; + } + + names[num] = strdup(ifname); + if (!names[num]) { + for (size_t i = 0; i < num; i++) + free(names[i]); + free(names); + return -ENOMEM; + } + + num++; + } + + if (num == 0) { + *ifaces = calloc(1, sizeof(char *)); + return *ifaces ? 0 : -ENOMEM; + } + + names[num] = NULL; + *ifaces = names; + return 0; +} + int ietf_interfaces_init(struct confd *confd) { int rc; diff --git a/src/confd/src/ietf-interfaces.h b/src/confd/src/ietf-interfaces.h index 81892ff2..655f6a83 100644 --- a/src/confd/src/ietf-interfaces.h +++ b/src/confd/src/ietf-interfaces.h @@ -102,6 +102,7 @@ int netdag_gen_ethtool(struct dagger *net, struct lyd_node *cif, struct lyd_node /* ietf-interfaces.c */ const char *get_chassis_addr(void); int link_gen_address(struct lyd_node *cif, FILE *ip); +int ietf_interfaces_get_all_l3(const struct lyd_node *tree, char ***ifaces); /* ietf-ip.c */ int netdag_gen_ipv6_autoconf(struct dagger *net, struct lyd_node *cif,