teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone
Blob Blame History Raw
From 11d47b91b0fd35aaf2db486783d80bdca7229d82 Mon Sep 17 00:00:00 2001
From: Ryan Wilson <ryantimwilson@meta4.com>
Date: Wed, 4 Dec 2024 16:15:30 -0800
Subject: [PATCH] networkctl: Make lldp/status backwards compatible with 255
 over dbus

---
 src/libsystemd-network/lldp-neighbor.c |  22 +++
 src/network/networkctl.c               | 208 ++++++++++++++++++++++++-
 src/systemd/sd-lldp-rx.h               |   1 +
 3 files changed, 224 insertions(+), 7 deletions(-)

diff --git a/src/libsystemd-network/lldp-neighbor.c b/src/libsystemd-network/lldp-neighbor.c
index a4384ac2e1..02af2954ae 100644
--- a/src/libsystemd-network/lldp-neighbor.c
+++ b/src/libsystemd-network/lldp-neighbor.c
@@ -629,6 +629,28 @@ int sd_lldp_neighbor_get_enabled_capabilities(sd_lldp_neighbor *n, uint16_t *ret
         return 0;
 }
 
+int sd_lldp_neighbor_from_raw(sd_lldp_neighbor **ret, const void *raw, size_t raw_size) {
+        _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
+        int r;
+
+        assert_return(ret, -EINVAL);
+        assert_return(raw || raw_size <= 0, -EINVAL);
+
+        n = lldp_neighbor_new(raw_size);
+        if (!n)
+                return -ENOMEM;
+
+        memcpy_safe(LLDP_NEIGHBOR_RAW(n), raw, raw_size);
+
+        r = lldp_neighbor_parse(n);
+        if (r < 0)
+                return r;
+
+        *ret = TAKE_PTR(n);
+
+        return r;
+}
+
 int sd_lldp_neighbor_tlv_rewind(sd_lldp_neighbor *n) {
         assert_return(n, -EINVAL);
 
diff --git a/src/network/networkctl.c b/src/network/networkctl.c
index a447c39a64..e31018e813 100644
--- a/src/network/networkctl.c
+++ b/src/network/networkctl.c
@@ -16,6 +16,7 @@
 #include "sd-device.h"
 #include "sd-dhcp-client.h"
 #include "sd-hwdb.h"
+#include "sd-lldp-rx.h"
 #include "sd-netlink.h"
 #include "sd-network.h"
 
@@ -173,7 +174,7 @@ int acquire_bus(sd_bus **ret) {
         if (networkd_is_running()) {
                 r = varlink_connect_networkd(/* ret_varlink = */ NULL);
                 if (r < 0)
-                        return r;
+                        log_warning("Varlink connection failed, fallback to D-Bus.");
         } else
                 log_warning("systemd-networkd is not running, output might be incomplete.");
 
@@ -1410,6 +1411,99 @@ static int dump_lldp_neighbors(Varlink *vl, Table *table, int ifindex) {
         return dump_list(table, "Connected To", buf);
 }
 
+static int open_lldp_neighbors_legacy(int ifindex, FILE **ret) {
+        _cleanup_fclose_ FILE *f = NULL;
+        char p[STRLEN("/run/systemd/netif/lldp/") + DECIMAL_STR_MAX(int)];
+
+        assert(ifindex >= 0);
+        assert(ret);
+
+        xsprintf(p, "/run/systemd/netif/lldp/%i", ifindex);
+
+        f = fopen(p, "re");
+        if (!f)
+                return -errno;
+
+        *ret = TAKE_PTR(f);
+        return 0;
+}
+
+static int next_lldp_neighbor_legacy(FILE *f, sd_lldp_neighbor **ret) {
+        _cleanup_free_ void *raw = NULL;
+        size_t l;
+        le64_t u;
+        int r;
+
+        assert(f);
+        assert(ret);
+
+        l = fread(&u, 1, sizeof(u), f);
+        if (l == 0 && feof(f))
+                return 0;
+        if (l != sizeof(u))
+                return -EBADMSG;
+
+        /* each LLDP packet is at most MTU size, but let's allow up to 4KiB just in case */
+        if (le64toh(u) >= 4096)
+                return -EBADMSG;
+
+        raw = new(uint8_t, le64toh(u));
+        if (!raw)
+                return -ENOMEM;
+
+        if (fread(raw, 1, le64toh(u), f) != le64toh(u))
+                return -EBADMSG;
+
+        r = sd_lldp_neighbor_from_raw(ret, raw, le64toh(u));
+        if (r < 0)
+                return r;
+
+        return 1;
+}
+
+static int dump_lldp_neighbors_legacy(Table *table, const char *prefix, int ifindex) {
+        _cleanup_strv_free_ char **buf = NULL;
+        _cleanup_fclose_ FILE *f = NULL;
+        int r;
+
+        assert(table);
+        assert(prefix);
+        assert(ifindex > 0);
+
+        r = open_lldp_neighbors_legacy(ifindex, &f);
+        if (r == -ENOENT)
+                return 0;
+        if (r < 0)
+                return r;
+
+        for (;;) {
+                const char *system_name = NULL, *port_id = NULL, *port_description = NULL;
+                _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
+
+                r = next_lldp_neighbor_legacy(f, &n);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        break;
+
+                (void) sd_lldp_neighbor_get_system_name(n, &system_name);
+                (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
+                (void) sd_lldp_neighbor_get_port_description(n, &port_description);
+
+                r = strv_extendf(&buf, "%s on port %s%s%s%s",
+                                 strna(system_name),
+                                 strna(port_id),
+                                 isempty(port_description) ? "" : " (",
+                                 strempty(port_description),
+                                 isempty(port_description) ? "" : ")");
+                if (r < 0)
+                        return log_oom();
+        }
+
+        return dump_list(table, prefix, buf);
+}
+
+
 static int dump_dhcp_leases(Table *table, const char *prefix, sd_bus *bus, const LinkInfo *link) {
         _cleanup_strv_free_ char **buf = NULL;
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
@@ -1696,7 +1790,6 @@ static int link_status_one(
 
         assert(bus);
         assert(rtnl);
-        assert(vl);
         assert(info);
 
         (void) sd_network_link_get_operational_state(info->ifindex, &operational_state);
@@ -2315,7 +2408,7 @@ static int link_status_one(
                         return table_log_add_error(r);
         }
 
-        r = dump_lldp_neighbors(vl, table, info->ifindex);
+        r = vl ? dump_lldp_neighbors(vl, table, info->ifindex) : dump_lldp_neighbors_legacy(table, "Connected To", info->ifindex);
         if (r < 0)
                 return r;
 
@@ -2449,8 +2542,10 @@ static int link_status(int argc, char *argv[], void *userdata) {
                 log_debug_errno(r, "Failed to open hardware database: %m");
 
         r = varlink_connect_networkd(&vl);
-        if (r < 0)
-                return r;
+        if (r < 0) {
+                log_warning("Varlink connection failed, fallback to D-Bus.");
+                vl = NULL;
+        }
 
         if (arg_all)
                 c = acquire_link_info(bus, rtnl, NULL, &links);
@@ -2584,6 +2679,103 @@ static int dump_lldp_neighbors_json(JsonVariant *reply, char * const *patterns)
         return json_variant_dump(v, arg_json_format_flags, NULL, NULL);
 }
 
+static int link_lldp_status_legacy(int argc, char *argv[], void *userdata) {
+        _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
+        _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
+        _cleanup_(table_unrefp) Table *table = NULL;
+        int r, c, m = 0;
+        uint16_t all = 0;
+        TableCell *cell;
+
+        r = sd_netlink_open(&rtnl);
+        if (r < 0)
+                return log_error_errno(r, "Failed to connect to netlink: %m");
+
+        c = acquire_link_info(NULL, rtnl, argc > 1 ? argv + 1 : NULL, &links);
+        if (c < 0)
+                return c;
+
+        pager_open(arg_pager_flags);
+
+        table = table_new("link",
+                          "chassis-id",
+                          "system-name",
+                          "caps",
+                          "port-id",
+                          "port-description");
+        if (!table)
+                return log_oom();
+
+        if (arg_full)
+                table_set_width(table, 0);
+
+        table_set_header(table, arg_legend);
+
+        assert_se(cell = table_get_cell(table, 0, 3));
+        table_set_minimum_width(table, cell, 11);
+        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
+
+        FOREACH_ARRAY(link, links, c) {
+                _cleanup_fclose_ FILE *f = NULL;
+
+                r = open_lldp_neighbors_legacy(link->ifindex, &f);
+                if (r == -ENOENT)
+                        continue;
+                if (r < 0) {
+                        log_warning_errno(r, "Failed to open LLDP data for %i, ignoring: %m", link->ifindex);
+                        continue;
+                }
+
+                for (;;) {
+                        const char *chassis_id = NULL, *port_id = NULL, *system_name = NULL, *port_description = NULL;
+                        _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
+                        _cleanup_free_ char *capabilities = NULL;
+                        uint16_t cc;
+
+                        r = next_lldp_neighbor_legacy(f, &n);
+                        if (r < 0) {
+                                log_warning_errno(r, "Failed to read neighbor data: %m");
+                                break;
+                        }
+                        if (r == 0)
+                                break;
+
+                        (void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id);
+                        (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
+                        (void) sd_lldp_neighbor_get_system_name(n, &system_name);
+                        (void) sd_lldp_neighbor_get_port_description(n, &port_description);
+
+                        if (sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0) {
+                                capabilities = lldp_capabilities_to_string(cc);
+                                all |= cc;
+                        }
+
+                        r = table_add_many(table,
+                                           TABLE_STRING, link->name,
+                                           TABLE_STRING, chassis_id,
+                                           TABLE_STRING, system_name,
+                                           TABLE_STRING, capabilities,
+                                           TABLE_STRING, port_id,
+                                           TABLE_STRING, port_description);
+                        if (r < 0)
+                                return table_log_add_error(r);
+
+                        m++;
+                }
+        }
+
+        r = table_print(table, NULL);
+        if (r < 0)
+                return table_log_print_error(r);
+
+        if (arg_legend) {
+                lldp_capabilities_legend(all);
+                printf("\n%i neighbors listed.\n", m);
+        }
+
+        return 0;
+}
+
 static int link_lldp_status(int argc, char *argv[], void *userdata) {
         _cleanup_(varlink_flush_close_unrefp) Varlink *vl = NULL;
         _cleanup_(table_unrefp) Table *table = NULL;
@@ -2594,8 +2786,10 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
         int r;
 
         r = varlink_connect_networkd(&vl);
-        if (r < 0)
-                return r;
+        if (r < 0) {
+                log_warning("Varlink connection failed, fallback to D-Bus.");
+                return link_lldp_status_legacy(argc, argv, userdata);
+        }
 
         r = varlink_call_and_log(vl, "io.systemd.Network.GetLLDPNeighbors", NULL, &reply);
         if (r < 0)
diff --git a/src/systemd/sd-lldp-rx.h b/src/systemd/sd-lldp-rx.h
index a7e1a9f376..154e37e2d8 100644
--- a/src/systemd/sd-lldp-rx.h
+++ b/src/systemd/sd-lldp-rx.h
@@ -88,6 +88,7 @@ int sd_lldp_neighbor_get_port_description(sd_lldp_neighbor *n, const char **ret)
 int sd_lldp_neighbor_get_mud_url(sd_lldp_neighbor *n, const char **ret);
 int sd_lldp_neighbor_get_system_capabilities(sd_lldp_neighbor *n, uint16_t *ret);
 int sd_lldp_neighbor_get_enabled_capabilities(sd_lldp_neighbor *n, uint16_t *ret);
+int sd_lldp_neighbor_from_raw(sd_lldp_neighbor **ret, const void *raw, size_t raw_size);
 
 /* Low-level, iterative TLV access. This is for everything else, it iteratively goes through all available TLVs
  * (including the ones covered with the calls above), and allows multiple TLVs for the same fields. */
-- 
2.43.5