teknoraver / rpms / systemd

Forked from rpms/systemd 4 months ago
Clone

Blame 0001-Revert-network-lldp-do-not-save-LLDP-neighbors-under.patch

92997e
From 1ef7ac52b34cef84ad5d4103bdaab4a2578bad9e Mon Sep 17 00:00:00 2001
Ryan Wilson a2fc29
From: Ryan Wilson <ryantimwilson@meta4.com>
Ryan Wilson a2fc29
Date: Wed, 4 Dec 2024 16:53:40 -0800
Ryan Wilson a2fc29
Subject: [PATCH] Revert "network/lldp: do not save LLDP neighbors under
Ryan Wilson a2fc29
 /run/systemd"
Ryan Wilson a2fc29
Ryan Wilson a2fc29
This reverts commit 5a0f6adbb2e39914897f404ac97fecebcc2c385a.
Ryan Wilson a2fc29
---
Ryan Wilson a2fc29
 src/libsystemd-network/lldp-neighbor.c | 11 ++++
Ryan Wilson a2fc29
 src/network/networkd-link.c            |  7 ++-
Ryan Wilson a2fc29
 src/network/networkd-link.h            |  1 +
Ryan Wilson a2fc29
 src/network/networkd-lldp-rx.c         | 69 ++++++++++++++++++++++++++
Ryan Wilson a2fc29
 src/network/networkd-lldp-rx.h         |  1 +
Ryan Wilson a2fc29
 src/network/networkd-state-file.c      |  2 +
Ryan Wilson a2fc29
 src/network/networkd.c                 |  3 +-
Ryan Wilson a2fc29
 src/systemd/sd-lldp-rx.h               |  1 +
Ryan Wilson a2fc29
 tmpfiles.d/systemd-network.conf        |  1 +
Ryan Wilson a2fc29
 9 files changed, 94 insertions(+), 2 deletions(-)
Ryan Wilson a2fc29
Ryan Wilson a2fc29
diff --git a/src/libsystemd-network/lldp-neighbor.c b/src/libsystemd-network/lldp-neighbor.c
92997e
index 4e51a55bd1..39864d18f7 100644
Ryan Wilson a2fc29
--- a/src/libsystemd-network/lldp-neighbor.c
Ryan Wilson a2fc29
+++ b/src/libsystemd-network/lldp-neighbor.c
92997e
@@ -377,6 +377,17 @@ int sd_lldp_neighbor_get_destination_address(sd_lldp_neighbor *n, struct ether_a
Ryan Wilson a2fc29
         return 0;
Ryan Wilson a2fc29
 }
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
+int sd_lldp_neighbor_get_raw(sd_lldp_neighbor *n, const void **ret, size_t *size) {
Ryan Wilson a2fc29
+        assert_return(n, -EINVAL);
Ryan Wilson a2fc29
+        assert_return(ret, -EINVAL);
Ryan Wilson a2fc29
+        assert_return(size, -EINVAL);
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        *ret = LLDP_NEIGHBOR_RAW(n);
Ryan Wilson a2fc29
+        *size = n->raw_size;
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        return 0;
Ryan Wilson a2fc29
+}
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
 int sd_lldp_neighbor_get_chassis_id(sd_lldp_neighbor *n, uint8_t *type, const void **ret, size_t *size) {
Ryan Wilson a2fc29
         assert_return(n, -EINVAL);
Ryan Wilson a2fc29
         assert_return(type, -EINVAL);
Ryan Wilson a2fc29
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
92997e
index 3c042e6c18..6a740b186a 100644
Ryan Wilson a2fc29
--- a/src/network/networkd-link.c
Ryan Wilson a2fc29
+++ b/src/network/networkd-link.c
92997e
@@ -275,6 +275,7 @@ static Link *link_free(Link *link) {
Ryan Wilson a2fc29
         free(link->driver);
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
         unlink_and_free(link->lease_file);
Ryan Wilson a2fc29
+        unlink_and_free(link->lldp_file);
Ryan Wilson a2fc29
         unlink_and_free(link->state_file);
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
         sd_device_unref(link->dev);
92997e
@@ -2662,7 +2663,7 @@ static Link *link_drop_or_unref(Link *link) {
Ryan Wilson a2fc29
 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
 static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
Ryan Wilson a2fc29
-        _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL;
Ryan Wilson a2fc29
+        _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL, *lldp_file = NULL;
Ryan Wilson a2fc29
         _cleanup_(link_drop_or_unrefp) Link *link = NULL;
Ryan Wilson a2fc29
         unsigned short iftype;
Ryan Wilson a2fc29
         int r, ifindex;
92997e
@@ -2703,6 +2704,9 @@ static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
                 if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
Ryan Wilson a2fc29
                         return log_oom_debug();
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+                if (asprintf(&lldp_file, "/run/systemd/netif/lldp/%d", ifindex) < 0)
Ryan Wilson a2fc29
+                        return log_oom_debug();
Ryan Wilson a2fc29
         }
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
         link = new(Link, 1);
92997e
@@ -2725,6 +2729,7 @@ static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
                 .state_file = TAKE_PTR(state_file),
Ryan Wilson a2fc29
                 .lease_file = TAKE_PTR(lease_file),
Ryan Wilson a2fc29
+                .lldp_file = TAKE_PTR(lldp_file),
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
                 .n_dns = UINT_MAX,
Ryan Wilson a2fc29
                 .dns_default_route = -1,
Ryan Wilson a2fc29
diff --git a/src/network/networkd-link.h b/src/network/networkd-link.h
92997e
index 113217cdb6..229ce976bc 100644
Ryan Wilson a2fc29
--- a/src/network/networkd-link.h
Ryan Wilson a2fc29
+++ b/src/network/networkd-link.h
92997e
@@ -194,6 +194,7 @@ typedef struct Link {
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
         /* This is about LLDP reception */
Ryan Wilson a2fc29
         sd_lldp_rx *lldp_rx;
Ryan Wilson a2fc29
+        char *lldp_file;
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
         /* This is about LLDP transmission */
Ryan Wilson a2fc29
         sd_lldp_tx *lldp_tx;
Ryan Wilson a2fc29
diff --git a/src/network/networkd-lldp-rx.c b/src/network/networkd-lldp-rx.c
92997e
index 6ba198282e..853e0a0ace 100644
Ryan Wilson a2fc29
--- a/src/network/networkd-lldp-rx.c
Ryan Wilson a2fc29
+++ b/src/network/networkd-lldp-rx.c
Ryan Wilson a2fc29
@@ -52,6 +52,8 @@ static void lldp_rx_handler(sd_lldp_rx *lldp_rx, sd_lldp_rx_event_t event, sd_ll
Ryan Wilson a2fc29
         Link *link = ASSERT_PTR(userdata);
Ryan Wilson a2fc29
         int r;
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
+        (void) link_lldp_save(link);
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
         if (link->lldp_tx && event == SD_LLDP_RX_EVENT_ADDED) {
Ryan Wilson a2fc29
                 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
@@ -102,3 +104,70 @@ int link_lldp_rx_configure(Link *link) {
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
         return 0;
Ryan Wilson a2fc29
 }
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+int link_lldp_save(Link *link) {
Ryan Wilson a2fc29
+        _cleanup_(unlink_and_freep) char *temp_path = NULL;
Ryan Wilson a2fc29
+        _cleanup_fclose_ FILE *f = NULL;
Ryan Wilson a2fc29
+        sd_lldp_neighbor **l = NULL;
Ryan Wilson a2fc29
+        int n = 0, r, i;
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        assert(link);
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        if (isempty(link->lldp_file))
Ryan Wilson a2fc29
+                return 0; /* Do not update state file when running in test mode. */
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        if (!link->lldp_rx) {
Ryan Wilson a2fc29
+                (void) unlink(link->lldp_file);
Ryan Wilson a2fc29
+                return 0;
Ryan Wilson a2fc29
+        }
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        r = sd_lldp_rx_get_neighbors(link->lldp_rx, &l);
Ryan Wilson a2fc29
+        if (r < 0)
Ryan Wilson a2fc29
+                return r;
Ryan Wilson a2fc29
+        if (r == 0) {
Ryan Wilson a2fc29
+                (void) unlink(link->lldp_file);
Ryan Wilson a2fc29
+                return 0;
Ryan Wilson a2fc29
+        }
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        n = r;
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        r = fopen_temporary(link->lldp_file, &f, &temp_path);
Ryan Wilson a2fc29
+        if (r < 0)
Ryan Wilson a2fc29
+                goto finish;
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        (void) fchmod(fileno(f), 0644);
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        for (i = 0; i < n; i++) {
Ryan Wilson a2fc29
+                const void *p;
Ryan Wilson a2fc29
+                le64_t u;
Ryan Wilson a2fc29
+                size_t sz;
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+                r = sd_lldp_neighbor_get_raw(l[i], &p, &sz);
Ryan Wilson a2fc29
+                if (r < 0)
Ryan Wilson a2fc29
+                        goto finish;
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+                u = htole64(sz);
Ryan Wilson a2fc29
+                fwrite(&u, 1, sizeof(u), f);
Ryan Wilson a2fc29
+                fwrite(p, 1, sz, f);
Ryan Wilson a2fc29
+        }
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        r = fflush_and_check(f);
Ryan Wilson a2fc29
+        if (r < 0)
Ryan Wilson a2fc29
+                goto finish;
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        r = conservative_rename(temp_path, link->lldp_file);
Ryan Wilson a2fc29
+        if (r < 0)
Ryan Wilson a2fc29
+                goto finish;
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+finish:
Ryan Wilson a2fc29
+        if (r < 0)
Ryan Wilson a2fc29
+                log_link_error_errno(link, r, "Failed to save LLDP data to %s: %m", link->lldp_file);
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        if (l) {
Ryan Wilson a2fc29
+                for (i = 0; i < n; i++)
Ryan Wilson a2fc29
+                        sd_lldp_neighbor_unref(l[i]);
Ryan Wilson a2fc29
+                free(l);
Ryan Wilson a2fc29
+        }
Ryan Wilson a2fc29
+
Ryan Wilson a2fc29
+        return r;
Ryan Wilson a2fc29
+}
Ryan Wilson a2fc29
diff --git a/src/network/networkd-lldp-rx.h b/src/network/networkd-lldp-rx.h
Ryan Wilson a2fc29
index 75c9f8ca86..22f6602bd0 100644
Ryan Wilson a2fc29
--- a/src/network/networkd-lldp-rx.h
Ryan Wilson a2fc29
+++ b/src/network/networkd-lldp-rx.h
Ryan Wilson a2fc29
@@ -14,6 +14,7 @@ typedef enum LLDPMode {
Ryan Wilson a2fc29
 } LLDPMode;
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
 int link_lldp_rx_configure(Link *link);
Ryan Wilson a2fc29
+int link_lldp_save(Link *link);
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
 const char* lldp_mode_to_string(LLDPMode m) _const_;
Ryan Wilson a2fc29
 LLDPMode lldp_mode_from_string(const char *s) _pure_;
Ryan Wilson a2fc29
diff --git a/src/network/networkd-state-file.c b/src/network/networkd-state-file.c
92997e
index da917dd897..2e32fbc300 100644
Ryan Wilson a2fc29
--- a/src/network/networkd-state-file.c
Ryan Wilson a2fc29
+++ b/src/network/networkd-state-file.c
92997e
@@ -714,6 +714,8 @@ static int link_save(Link *link) {
Ryan Wilson a2fc29
         if (link->state == LINK_STATE_LINGER)
Ryan Wilson a2fc29
                 return 0;
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
+        link_lldp_save(link);
Ryan Wilson a2fc29
+
92997e
         admin_state = ASSERT_PTR(link_state_to_string(link->state));
92997e
         oper_state = ASSERT_PTR(link_operstate_to_string(link->operstate));
92997e
         carrier_state = ASSERT_PTR(link_carrier_state_to_string(link->carrier_state));
Ryan Wilson a2fc29
diff --git a/src/network/networkd.c b/src/network/networkd.c
92997e
index 883f16d81b..12edb68583 100644
Ryan Wilson a2fc29
--- a/src/network/networkd.c
Ryan Wilson a2fc29
+++ b/src/network/networkd.c
92997e
@@ -75,7 +75,8 @@ static int run(int argc, char *argv[]) {
Ryan Wilson a2fc29
          * to support old kernels not supporting AmbientCapabilities=. */
Ryan Wilson a2fc29
         FOREACH_STRING(p,
Ryan Wilson a2fc29
                        "/run/systemd/netif/links/",
Ryan Wilson a2fc29
-                       "/run/systemd/netif/leases/") {
Ryan Wilson a2fc29
+                       "/run/systemd/netif/leases/",
Ryan Wilson a2fc29
+                       "/run/systemd/netif/lldp/") {
Ryan Wilson a2fc29
                 r = mkdir_safe_label(p, 0755, UID_INVALID, GID_INVALID, MKDIR_WARN_MODE);
Ryan Wilson a2fc29
                 if (r < 0)
Ryan Wilson a2fc29
                         log_warning_errno(r, "Could not create directory '%s': %m", p);
Ryan Wilson a2fc29
diff --git a/src/systemd/sd-lldp-rx.h b/src/systemd/sd-lldp-rx.h
92997e
index b697643a07..fedf9956cf 100644
Ryan Wilson a2fc29
--- a/src/systemd/sd-lldp-rx.h
Ryan Wilson a2fc29
+++ b/src/systemd/sd-lldp-rx.h
Ryan Wilson a2fc29
@@ -75,6 +75,7 @@ sd_lldp_neighbor *sd_lldp_neighbor_unref(sd_lldp_neighbor *n);
Ryan Wilson a2fc29
 int sd_lldp_neighbor_get_source_address(sd_lldp_neighbor *n, struct ether_addr* address);
Ryan Wilson a2fc29
 int sd_lldp_neighbor_get_destination_address(sd_lldp_neighbor *n, struct ether_addr* address);
Ryan Wilson a2fc29
 int sd_lldp_neighbor_get_timestamp(sd_lldp_neighbor *n, clockid_t clock, uint64_t *ret);
Ryan Wilson a2fc29
+int sd_lldp_neighbor_get_raw(sd_lldp_neighbor *n, const void **ret, size_t *size);
Ryan Wilson a2fc29
 
Ryan Wilson a2fc29
 /* High-level, direct, parsed out field access. These fields exist at most once, hence may be queried directly. */
Ryan Wilson a2fc29
 int sd_lldp_neighbor_get_chassis_id(sd_lldp_neighbor *n, uint8_t *type, const void **ret, size_t *size);
Ryan Wilson a2fc29
diff --git a/tmpfiles.d/systemd-network.conf b/tmpfiles.d/systemd-network.conf
92997e
index 75b61b7d07..881937d456 100644
Ryan Wilson a2fc29
--- a/tmpfiles.d/systemd-network.conf
Ryan Wilson a2fc29
+++ b/tmpfiles.d/systemd-network.conf
Ryan Wilson a2fc29
@@ -10,4 +10,5 @@
92997e
 d$ /run/systemd/netif        0755 systemd-network systemd-network -
92997e
 d$ /run/systemd/netif/links  0755 systemd-network systemd-network -
92997e
 d$ /run/systemd/netif/leases 0755 systemd-network systemd-network -
92997e
+d$ /run/systemd/netif/lldp   0755 systemd-network systemd-network -
92997e
 d$ /var/lib/systemd/network  0755 systemd-network systemd-network -
Ryan Wilson a2fc29
-- 
92997e
2.47.1
Ryan Wilson a2fc29