summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/libsystemd-network/lldp-internal.c97
-rw-r--r--src/libsystemd-network/lldp-internal.h4
-rw-r--r--src/libsystemd-network/sd-lldp.c214
-rw-r--r--src/libsystemd-network/sd-lldp.h9
-rw-r--r--src/libsystemd/sd-network/sd-network.c26
-rw-r--r--src/network/networkctl.c258
-rw-r--r--src/network/networkd-link.c44
-rw-r--r--src/network/networkd-link.h1
-rw-r--r--src/network/networkd.c6
-rw-r--r--src/systemd/sd-network.h2
10 files changed, 659 insertions, 2 deletions
diff --git a/src/libsystemd-network/lldp-internal.c b/src/libsystemd-network/lldp-internal.c
index 7085a02491..f86c11e6ca 100644
--- a/src/libsystemd-network/lldp-internal.c
+++ b/src/libsystemd-network/lldp-internal.c
@@ -86,6 +86,8 @@ int lldp_read_port_id(tlv_packet *tlv,
goto out1;
switch (subtype) {
+ case LLDP_PORT_SUBTYPE_PORT_COMPONENT:
+ case LLDP_PORT_SUBTYPE_INTERFACE_ALIAS:
case LLDP_PORT_SUBTYPE_INTERFACE_NAME:
r = tlv_packet_read_string(tlv, &s, length);
@@ -127,12 +129,105 @@ int lldp_read_ttl(tlv_packet *tlv, uint16_t *ttl) {
r = tlv_packet_read_u16(tlv, ttl);
- (void) lldp_tlv_packet_exit_container(tlv);
+ (void)lldp_tlv_packet_exit_container(tlv);
out:
return r;
}
+int lldp_read_system_name(tlv_packet *tlv,
+ uint16_t *length,
+ char **data) {
+ char *s;
+ int r;
+
+ assert_return(tlv, -EINVAL);
+
+ r = lldp_tlv_packet_enter_container(tlv, LLDP_TYPE_SYSTEM_NAME);
+ if (r < 0)
+ return r;
+
+ r = tlv_packet_read_string(tlv, &s, length);
+ if (r < 0)
+ goto out;
+
+ *data = (char *) s;
+
+ out:
+ (void)lldp_tlv_packet_exit_container(tlv);
+
+ return r;
+}
+
+int lldp_read_system_description(tlv_packet *tlv,
+ uint16_t *length,
+ char **data) {
+ char *s;
+ int r;
+
+ assert_return(tlv, -EINVAL);
+
+ r = lldp_tlv_packet_enter_container(tlv, LLDP_TYPE_SYSTEM_DESCRIPTION);
+ if (r < 0)
+ return r;
+
+ r = tlv_packet_read_string(tlv, &s, length);
+ if (r < 0)
+ goto out;
+
+ *data = (char *) s;
+
+ out:
+ (void)lldp_tlv_packet_exit_container(tlv);
+
+ return r;
+}
+
+int lldp_read_port_description(tlv_packet *tlv,
+ uint16_t *length,
+ char **data) {
+ char *s;
+ int r;
+
+ assert_return(tlv, -EINVAL);
+
+ r = lldp_tlv_packet_enter_container(tlv, LLDP_TYPE_PORT_DESCRIPTION);
+ if (r < 0)
+ return r;
+
+ r = tlv_packet_read_string(tlv, &s, length);
+ if (r < 0)
+ goto out;
+
+ *data = (char *) s;
+
+ out:
+ (void)lldp_tlv_packet_exit_container(tlv);
+
+ return r;
+}
+
+int lldp_read_system_capability(tlv_packet *tlv, uint16_t *data) {
+ int r;
+
+ assert_return(tlv, -EINVAL);
+
+ r = lldp_tlv_packet_enter_container(tlv, LLDP_TYPE_SYSTEM_CAPABILITIES);
+ if (r < 0)
+ return r;
+
+ r = tlv_packet_read_u16(tlv, data);
+ if (r < 0)
+ goto out;
+
+ return 0;
+ out:
+
+ (void)lldp_tlv_packet_exit_container(tlv);
+
+ return r;
+}
+
/* 10.5.5.2.2 mibUpdateObjects ()
* The mibUpdateObjects () procedure updates the MIB objects corresponding to
* the TLVs contained in the received LLDPDU for the LLDP remote system
diff --git a/src/libsystemd-network/lldp-internal.h b/src/libsystemd-network/lldp-internal.h
index dbaf389792..028a35ffdd 100644
--- a/src/libsystemd-network/lldp-internal.h
+++ b/src/libsystemd-network/lldp-internal.h
@@ -89,5 +89,9 @@ int lldp_mib_remove_objects(lldp_chassis *c, tlv_packet *tlv);
int lldp_read_chassis_id(tlv_packet *tlv, uint8_t *type, uint16_t *length, uint8_t **data);
int lldp_read_port_id(tlv_packet *tlv, uint8_t *type, uint16_t *length, uint8_t **data);
int lldp_read_ttl(tlv_packet *tlv, uint16_t *ttl);
+int lldp_read_system_name(tlv_packet *tlv, uint16_t *length, char **data);
+int lldp_read_system_description(tlv_packet *tlv, uint16_t *length, char **data);
+int lldp_read_system_capability(tlv_packet *tlv, uint16_t *data);
+int lldp_read_port_description(tlv_packet *tlv, uint16_t *length, char **data);
#define log_lldp(fmt, ...) log_internal(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, "LLDP: " fmt, ##__VA_ARGS__)
diff --git a/src/libsystemd-network/sd-lldp.c b/src/libsystemd-network/sd-lldp.c
index 97d1578446..45881e5f64 100644
--- a/src/libsystemd-network/sd-lldp.c
+++ b/src/libsystemd-network/sd-lldp.c
@@ -30,7 +30,21 @@
#include "lldp-port.h"
#include "sd-lldp.h"
#include "prioq.h"
+#include "strv.h"
#include "lldp-internal.h"
+#include "ether-addr-util.h"
+
+typedef enum LLDPAgentRXState {
+ LLDP_AGENT_RX_WAIT_PORT_OPERATIONAL = 4,
+ LLDP_AGENT_RX_DELETE_AGED_INFO,
+ LLDP_AGENT_RX_LLDP_INITIALIZE,
+ LLDP_AGENT_RX_WAIT_FOR_FRAME,
+ LLDP_AGENT_RX_RX_FRAME,
+ LLDP_AGENT_RX_DELETE_INFO,
+ LLDP_AGENT_RX_UPDATE_INFO,
+ _LLDP_AGENT_RX_STATE_MAX,
+ _LLDP_AGENT_RX_INVALID = -1,
+} LLDPAgentRXState;
/* Section 10.5.2.2 Reception counters */
struct lldp_agent_statitics {
@@ -48,6 +62,11 @@ struct sd_lldp {
Prioq *by_expiry;
Hashmap *neighbour_mib;
+ sd_lldp_cb_t cb;
+
+ void *userdata;
+
+ LLDPAgentRXState rx_state;
lldp_agent_statitics statitics;
};
@@ -87,6 +106,8 @@ static const struct hash_ops chassis_id_hash_ops = {
};
static void lldp_mib_delete_objects(sd_lldp *lldp);
+static void lldp_set_state(sd_lldp *lldp, LLDPAgentRXState state);
+static void lldp_run_state_machine(sd_lldp *ll);
static int lldp_receive_frame(sd_lldp *lldp, tlv_packet *tlv) {
int r;
@@ -95,13 +116,19 @@ static int lldp_receive_frame(sd_lldp *lldp, tlv_packet *tlv) {
assert(tlv);
/* Remove expired packets */
- if (prioq_size(lldp->by_expiry) > 0)
+ if (prioq_size(lldp->by_expiry) > 0) {
+
+ lldp_set_state(lldp, LLDP_AGENT_RX_DELETE_INFO);
+
lldp_mib_delete_objects(lldp);
+ }
r = lldp_mib_add_objects(lldp->by_expiry, lldp->neighbour_mib, tlv);
if (r < 0)
goto out;
+ lldp_set_state(lldp, LLDP_AGENT_RX_UPDATE_INFO);
+
log_lldp("Packet added. MIB size: %d , PQ size: %d",
hashmap_size(lldp->neighbour_mib),
prioq_size(lldp->by_expiry));
@@ -114,6 +141,8 @@ static int lldp_receive_frame(sd_lldp *lldp, tlv_packet *tlv) {
if (r < 0)
log_lldp("Receive frame failed: %s", strerror(-r));
+ lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_FOR_FRAME);
+
return 0;
}
@@ -142,6 +171,8 @@ int lldp_handle_packet(tlv_packet *tlv, uint16_t length) {
goto out;
}
+ lldp_set_state(lldp, LLDP_AGENT_RX_RX_FRAME);
+
p = tlv->pdu;
p += sizeof(struct ether_header);
@@ -304,6 +335,8 @@ int lldp_handle_packet(tlv_packet *tlv, uint16_t length) {
return lldp_receive_frame(lldp, tlv);
out:
+ lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_FOR_FRAME);
+
if (malformed) {
lldp->statitics.stats_frames_discarded_total ++;
lldp->statitics.stats_frames_in_errors_total ++;
@@ -326,6 +359,23 @@ static int ttl_expiry_item_prioq_compare_func(const void *a, const void *b) {
return 0;
}
+static void lldp_set_state(sd_lldp *lldp, LLDPAgentRXState state) {
+
+ assert(lldp);
+ assert(state < _LLDP_AGENT_RX_STATE_MAX);
+
+ lldp->rx_state = state;
+
+ lldp_run_state_machine(lldp);
+}
+
+static void lldp_run_state_machine(sd_lldp *lldp) {
+
+ if (lldp->rx_state == LLDP_AGENT_RX_UPDATE_INFO)
+ if (lldp->cb)
+ lldp->cb(lldp, LLDP_AGENT_RX_UPDATE_INFO, lldp->userdata);
+}
+
/* 10.5.5.2.1 mibDeleteObjects ()
* The mibDeleteObjects () procedure deletes all information in the LLDP remote
* systems MIB associated with the MSAP identifier if an LLDPDU is received with
@@ -377,6 +427,150 @@ static void lldp_mib_objects_flush(sd_lldp *lldp) {
assert(prioq_size(lldp->by_expiry) == 0);
}
+int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
+ _cleanup_free_ char *s = NULL, *t = NULL, *k = NULL;
+ _cleanup_free_ char *temp_path = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ uint8_t *mac, *port_id, type;
+ lldp_neighbour_port *p;
+ uint16_t data = 0, length = 0;
+ char buf[LINE_MAX];
+ lldp_chassis *c;
+ usec_t time;
+ Iterator i;
+ int r;
+
+ assert(lldp);
+ assert(lldp_file);
+
+ r = fopen_temporary(lldp_file, &f, &temp_path);
+ if (r < 0)
+ goto finish;
+
+ fchmod(fileno(f), 0644);
+
+ HASHMAP_FOREACH(c, lldp->neighbour_mib, i) {
+ LIST_FOREACH(port, p, c->ports) {
+
+ r = lldp_read_chassis_id(p->packet, &type, &length, &mac);
+ if (r < 0)
+ continue;
+
+ memzero(buf, LINE_MAX);
+
+ sprintf(buf, "'_Chassis=%02x:%02x:%02x:%02x:%02x:%02x' '_CType=%d' ",
+ mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], type);
+
+ s = strdup(buf);
+ if (!s)
+ return -ENOMEM;
+
+ r = lldp_read_port_id(p->packet, &type, &length, &port_id);
+ if (r < 0) {
+ free(s);
+ continue;
+ }
+
+ memzero(buf, LINE_MAX);
+ if (type != LLDP_PORT_SUBTYPE_MAC_ADDRESS) {
+
+ k = strndup((char *) port_id, length -1);
+ if (!k)
+ return -ENOMEM;
+
+ sprintf(buf, "'_Port=%s' '_PType=%d' ", k , type);
+
+ t = strappend(s, buf);
+
+ free(k);
+ } else {
+
+ mac = port_id;
+
+ sprintf(buf, "'_Port=%02x:%02x:%02x:%02x:%02x:%02x' '_PType=%d' ",
+ mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], type);
+
+ t = strappend(s, buf);
+ }
+
+ if (!t)
+ return -ENOMEM;
+
+ free(s);
+ s = t;
+
+ time = now(CLOCK_BOOTTIME);
+
+ /* Don't write expired packets */
+ if(time - p->until <= 0) {
+ free(s);
+ continue;
+ }
+
+ memzero(buf, LINE_MAX);
+ sprintf(buf, "'_TTL=%lu' ", p->until);
+
+ t = strappend(s, buf);
+ if (!t)
+ return -ENOMEM;
+
+ free(s);
+ s = t;
+
+ r = lldp_read_system_name(p->packet, &length, &k);
+ if (r < 0)
+ k = strappend(s, "'_NAME=N/A' ");
+ else {
+ t = strndup(k, length);
+ if (!t)
+ return -ENOMEM;
+
+ k = strjoin(s, "'_NAME=", t, "' ", NULL);
+ }
+
+ if (!k)
+ return -ENOMEM;
+
+ free(s);
+ s = k;
+
+ memzero(buf, LINE_MAX);
+
+ (void)lldp_read_system_capability(p->packet, &data);
+
+ sprintf(buf, "'_CAP=%x'", data);
+
+ t = strappend(s, buf);
+ if (!t)
+ return -ENOMEM;
+
+ fprintf(f, "%s\n", t);
+
+ free(s);
+ free(t);
+ }
+ }
+ r = 0;
+
+ s = NULL;
+ t = NULL;
+ k = NULL;
+
+ fflush(f);
+
+ if (ferror(f) || rename(temp_path, lldp_file) < 0) {
+ r = -errno;
+ unlink(lldp_file);
+ unlink(temp_path);
+ }
+
+ finish:
+ if (r < 0)
+ log_error("Failed to save lldp data %s: %s", lldp_file, strerror(-r));
+
+ return r;
+}
+
int sd_lldp_start(sd_lldp *lldp) {
int r;
@@ -385,14 +579,21 @@ int sd_lldp_start(sd_lldp *lldp) {
lldp->port->status = LLDP_PORT_STATUS_ENABLED;
+ lldp_set_state(lldp, LLDP_AGENT_RX_LLDP_INITIALIZE);
+
r = lldp_port_start(lldp->port);
if (r < 0) {
log_lldp("Failed to start Port : %s , %s",
lldp->port->ifname,
strerror(-r));
+
+ lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_PORT_OPERATIONAL);
+
return r;
}
+ lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_FOR_FRAME);
+
return 0;
}
@@ -441,6 +642,15 @@ int sd_lldp_detach_event(sd_lldp *lldp) {
return 0;
}
+int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_cb_t cb, void *userdata) {
+ assert_return(lldp, -EINVAL);
+
+ lldp->cb = cb;
+ lldp->userdata = userdata;
+
+ return 0;
+}
+
void sd_lldp_free(sd_lldp *lldp) {
if (!lldp)
@@ -486,6 +696,8 @@ int sd_lldp_new(int ifindex,
if (r < 0)
return r;
+ lldp->rx_state = LLDP_AGENT_RX_WAIT_PORT_OPERATIONAL;
+
*ret = lldp;
lldp = NULL;
diff --git a/src/libsystemd-network/sd-lldp.h b/src/libsystemd-network/sd-lldp.h
index 95d802df84..ee064c6fc8 100644
--- a/src/libsystemd-network/sd-lldp.h
+++ b/src/libsystemd-network/sd-lldp.h
@@ -28,6 +28,12 @@
typedef struct sd_lldp sd_lldp;
typedef struct lldp_agent_statitics lldp_agent_statitics;
+typedef void (*sd_lldp_cb_t)(sd_lldp *lldp, int event, void *userdata);
+
+enum {
+ UPDATE_INFO = 10,
+};
+
typedef enum LLDPPortStatus {
LLDP_PORT_STATUS_NONE,
LLDP_PORT_STATUS_ENABLED,
@@ -48,4 +54,7 @@ int sd_lldp_stop(sd_lldp *lldp);
int sd_lldp_attach_event(sd_lldp *lldp, sd_event *event, int priority);
int sd_lldp_detach_event(sd_lldp *lldp);
+int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_cb_t cb, void *userdata);
+int sd_lldp_save(sd_lldp *lldp, const char *file);
+
int lldp_handle_packet(tlv_packet *m, uint16_t length);
diff --git a/src/libsystemd/sd-network/sd-network.c b/src/libsystemd/sd-network/sd-network.c
index d63e6f9dfa..c735cac3b1 100644
--- a/src/libsystemd/sd-network/sd-network.c
+++ b/src/libsystemd/sd-network/sd-network.c
@@ -192,6 +192,32 @@ _public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) {
return 0;
}
+_public_ int sd_network_link_get_lldp(int ifindex, char **lldp) {
+ _cleanup_free_ char *s = NULL, *p = NULL;
+ size_t size;
+ int r;
+
+ assert_return(ifindex > 0, -EINVAL);
+ assert_return(lldp, -EINVAL);
+
+ if (asprintf(&p, "/run/systemd/netif/lldp/%d", ifindex) < 0)
+ return -ENOMEM;
+
+ r = read_full_file(p, &s, &size);
+ if (r == -ENOENT)
+ return -ENODATA;
+ if (r < 0)
+ return r;
+ if (size <= 0)
+ return -ENODATA;
+
+ *lldp = s;
+ s = NULL;
+
+ return 0;
+}
+
+
static int network_get_link_strv(const char *key, int ifindex, char ***ret) {
_cleanup_free_ char *p = NULL, *s = NULL;
_cleanup_strv_free_ char **a = NULL;
diff --git a/src/network/networkctl.c b/src/network/networkctl.c
index aa868f5c3a..74735effcc 100644
--- a/src/network/networkctl.c
+++ b/src/network/networkctl.c
@@ -32,6 +32,7 @@
#include "build.h"
#include "util.h"
#include "pager.h"
+#include "lldp.h"
#include "rtnl-util.h"
#include "udev-util.h"
#include "hwdb-util.h"
@@ -744,6 +745,261 @@ static int link_status(int argc, char *argv[], void *userdata) {
return 0;
}
+const char *lldp_system_capability_to_string(LLDPSystemCapabilities d) _const_;
+LLDPSystemCapabilities lldp_system_capability_from_string(const char *d) _pure_;
+
+static const char* const lldp_system_capability_table[_LLDP_SYSTEM_CAPABILITIES_MAX + 1] = {
+ [LLDP_SYSTEM_CAPABILITIES_OTHER] = "O",
+ [LLDP_SYSTEM_CAPABILITIES_REPEATER] = "P",
+ [LLDP_SYSTEM_CAPABILITIES_BRIDGE] = "B",
+ [LLDP_SYSTEM_CAPABILITIES_WLAN_AP] = "W",
+ [LLDP_SYSTEM_CAPABILITIES_ROUTER] = "R",
+ [LLDP_SYSTEM_CAPABILITIES_PHONE] = "T",
+ [LLDP_SYSTEM_CAPABILITIES_DOCSIS] = "D",
+ [LLDP_SYSTEM_CAPABILITIES_STATION] = "A",
+ [LLDP_SYSTEM_CAPABILITIES_CVLAN] = "C",
+ [LLDP_SYSTEM_CAPABILITIES_SVLAN] = "S",
+ [LLDP_SYSTEM_CAPABILITIES_TPMR] = "M",
+ [_LLDP_SYSTEM_CAPABILITIES_MAX] = "N/A",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(lldp_system_capability, LLDPSystemCapabilities);
+
+static char *lldp_system_caps(uint16_t cap) {
+ _cleanup_free_ char *s = NULL, *t = NULL;
+ char *capability;
+
+ t = strdup("[ ");
+ if (!t)
+ return NULL;
+
+ if (cap & LLDP_SYSTEM_CAPABILITIES_OTHER) {
+ s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_OTHER), " ", NULL);
+ if (!s)
+ return NULL;
+
+ free(t);
+ t = s;
+ }
+
+ if (cap & LLDP_SYSTEM_CAPABILITIES_REPEATER) {
+ s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_REPEATER), " ", NULL);
+ if (!s)
+ return NULL;
+
+ free(t);
+ t = s;
+ }
+
+ if (cap & LLDP_SYSTEM_CAPABILITIES_BRIDGE) {
+ s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_BRIDGE), " ", NULL);
+ if (!s)
+ return NULL;
+
+ free(t);
+ t = s;
+ }
+
+ if (cap & LLDP_SYSTEM_CAPABILITIES_WLAN_AP) {
+ s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_WLAN_AP), " ", NULL);
+ if (!s)
+ return NULL;
+
+ free(t);
+ t = s;
+ }
+
+ if (cap & LLDP_SYSTEM_CAPABILITIES_ROUTER) {
+ s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_ROUTER), " ", NULL);
+ if (!s)
+ return NULL;
+
+ free(t);
+ t = s;
+ }
+
+ if (cap & LLDP_SYSTEM_CAPABILITIES_PHONE) {
+ s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_PHONE), " ", NULL);
+ if (!s)
+ return NULL;
+
+ free(t);
+ t = s;
+ }
+
+ if (cap & LLDP_SYSTEM_CAPABILITIES_DOCSIS) {
+ s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_DOCSIS), " ", NULL);
+ if (!s)
+ return NULL;
+
+ free(t);
+ t = s;
+ }
+
+ if (cap & LLDP_SYSTEM_CAPABILITIES_STATION) {
+ s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_STATION), " ", NULL);
+ if (!s)
+ return NULL;
+
+ free(t);
+ t = s;
+ }
+
+ if (cap & LLDP_SYSTEM_CAPABILITIES_CVLAN) {
+ s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_CVLAN), " ", NULL);
+ if (!s)
+ return NULL;
+
+ free(t);
+ t = s;
+ }
+
+ if (cap & LLDP_SYSTEM_CAPABILITIES_SVLAN) {
+ s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_SVLAN), " ", NULL);
+ if (!s)
+ return NULL;
+
+ free(t);
+ t = s;
+ }
+
+ if (cap & LLDP_SYSTEM_CAPABILITIES_TPMR) {
+ s = strappend(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_TPMR));
+ if (!s)
+ return NULL;
+
+ free(t);
+ }
+
+ if (!s) {
+ s = strappend(t, lldp_system_capability_to_string(_LLDP_SYSTEM_CAPABILITIES_MAX));
+ if (!s)
+ return NULL;
+
+ free(t);
+ }
+
+ t = strappend(s, "]");
+ if (!s)
+ return NULL;
+
+ free(s);
+ capability = t;
+
+ s = NULL;
+ t = NULL;
+
+ return capability;
+}
+
+static int link_lldp_status(int argc, char *argv[], void *userdata) {
+ _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
+ _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
+ _cleanup_free_ LinkInfo *links = NULL;
+ const char *state, *word;
+
+ usec_t time, until, ttl;
+ uint32_t capability;
+ char buf[LINE_MAX];
+ int i, r, c, j;
+ size_t ll;
+ char **s;
+
+ pager_open_if_enabled();
+
+ r = sd_rtnl_open(&rtnl, 0);
+ if (r < 0)
+ return log_error_errno(r, "Failed to connect to netlink: %m");
+
+ r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
+ if (r < 0)
+ return rtnl_log_create_error(r);
+
+ r = sd_rtnl_message_request_dump(req, true);
+ if (r < 0)
+ return rtnl_log_create_error(r);
+
+ r = sd_rtnl_call(rtnl, req, 0, &reply);
+ if (r < 0)
+ return log_error_errno(r, "Failed to enumerate links: %m");
+
+ c = decode_and_sort_links(reply, &links);
+ if (c < 0)
+ return rtnl_log_parse_error(c);
+
+ printf("Capability Codes: (O) - Other, (P) - Repeater, (B) - Bridge , (W) - WLAN Access Point, (R) = Router, (T) - Telephone,\n"
+ "(D) - Data Over Cable Service Interface Specifications, (A) - Station, (C) - Customer VLAN, (S) - Service VLAN,\n"
+ "(M) - Two-port MAC Relay (TPMR)\n\n");
+
+ printf("%s %16s %24s %16s %16s\n", "Local Intf", "Device ID", "Port ID", "TTL", "Capability");
+
+ for (i = j = 0; i < c; i++) {
+ _cleanup_free_ char *chassis = NULL, *port = NULL, *cap = NULL, *lldp = NULL;
+ _cleanup_strv_free_ char **l = NULL;
+
+ r = sd_network_link_get_lldp(links[i].ifindex, &lldp);
+ if (r < 0)
+ continue;
+
+ l = strv_split_newlines(lldp);
+ if (!l)
+ return -ENOMEM;
+
+ STRV_FOREACH(s, l) {
+ FOREACH_WORD_QUOTED(word, ll, *s, state) {
+ _cleanup_free_ char *t = NULL, *a = NULL, *b = NULL;
+
+ t = strndup(word, ll);
+ if (!t)
+ return -ENOMEM;
+
+ r = split_pair(t, "=", &a, &b);
+ if (r < 0)
+ continue;
+
+ if (streq(a, "_Chassis")) {
+
+ memzero(buf, LINE_MAX);
+
+ chassis = strdup(b);
+ if (!chassis)
+ return -ENOMEM;
+
+ } else if (streq(a, "_Port")) {
+
+ port = strdup(b);
+ if (!port)
+ return -ENOMEM;
+
+ } else if (streq(a, "_TTL")) {
+
+ time = now(CLOCK_BOOTTIME);
+
+ sscanf(b, "%lu", &until);
+
+ ttl = (until - time) / USEC_PER_SEC;
+
+
+ } else if (streq(a, "_CAP")) {
+ sscanf(b, "%x", &capability);
+
+ cap = lldp_system_caps(capability);
+ }
+
+ }
+
+ if (until > time) {
+ printf("%10s %24s %16s %16lu %16s\n", links[i].name, chassis, port, ttl, cap);
+ j++;
+ }
+ }
+ }
+
+ printf("\nTotal entries displayed: %d\n", j);
+
+ return 0;
+}
+
static void help(void) {
printf("%s [OPTIONS...]\n\n"
"Query and control the networking subsystem.\n\n"
@@ -755,6 +1011,7 @@ static void help(void) {
"Commands:\n"
" list List links\n"
" status LINK Show link status\n"
+ " lldp Show lldp information\n"
, program_invocation_short_name);
}
@@ -820,6 +1077,7 @@ static int networkctl_main(int argc, char *argv[]) {
const Verb verbs[] = {
{ "list", VERB_ANY, 1, VERB_DEFAULT, list_links },
{ "status", 1, VERB_ANY, 0, link_status },
+ { "lldp", VERB_ANY, 1, VERB_DEFAULT, link_lldp_status },
{}
};
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 725e22b93f..93b53ee3c0 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -219,6 +219,12 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
if (r < 0)
return -ENOMEM;
+ r = asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d",
+ link->ifindex);
+ if (r < 0)
+ return -ENOMEM;
+
+
r = hashmap_ensure_allocated(&manager->links, NULL);
if (r < 0)
return r;
@@ -259,6 +265,9 @@ static void link_free(Link *link) {
unlink(link->lease_file);
free(link->lease_file);
+ unlink(link->lldp_file);
+ free(link->lldp_file);
+
sd_ipv4ll_unref(link->ipv4ll);
sd_dhcp6_client_unref(link->dhcp6_client);
sd_icmp6_nd_unref(link->icmp6_router_discovery);
@@ -895,6 +904,23 @@ static int link_set_bridge(Link *link) {
return r;
}
+static void lldp_handler(sd_lldp *lldp, int event, void *userdata) {
+ Link *link = userdata;
+ int r;
+
+ assert(link);
+ assert(link->network);
+ assert(link->manager);
+
+ if (event != UPDATE_INFO)
+ return;
+
+ r = sd_lldp_save(link->lldp, link->lldp_file);
+ if (r < 0)
+ log_link_warning(link, "could not save LLDP");
+
+}
+
static int link_acquire_conf(Link *link) {
int r;
@@ -1237,6 +1263,11 @@ static int link_configure(Link *link) {
r = sd_lldp_attach_event(link->lldp, NULL, 0);
if (r < 0)
return r;
+
+ r = sd_lldp_set_callback(link->lldp,
+ lldp_handler, link);
+ if (r < 0)
+ return r;
}
if (link_has_carrier(link)) {
@@ -1855,6 +1886,19 @@ int link_save(Link *link) {
} else
unlink(link->lease_file);
+ if (link->lldp) {
+ assert(link->network);
+
+ r = sd_lldp_save(link->lldp, link->lldp_file);
+ if (r < 0)
+ goto fail;
+
+ fprintf(f,
+ "LLDP_FILE=%s\n",
+ link->lldp_file);
+ } else
+ unlink(link->lldp_file);
+
r = fflush_and_check(f);
if (r < 0)
goto fail;
diff --git a/src/network/networkd-link.h b/src/network/networkd-link.h
index 6bb59d2190..0e2f558325 100644
--- a/src/network/networkd-link.h
+++ b/src/network/networkd-link.h
@@ -93,6 +93,7 @@ struct Link {
sd_dhcp6_client *dhcp6_client;
sd_lldp *lldp;
+ char *lldp_file;
};
Link *link_unref(Link *link);
diff --git a/src/network/networkd.c b/src/network/networkd.c
index 0b386d4069..ced319de1d 100644
--- a/src/network/networkd.c
+++ b/src/network/networkd.c
@@ -64,6 +64,12 @@ int main(int argc, char *argv[]) {
if (r < 0)
log_error_errno(r, "Could not create runtime directory 'leases': %m");
+ r = mkdir_safe_label("/run/systemd/netif/lldp", 0755, uid, gid);
+ if (r < 0)
+ log_error("Could not create runtime directory 'lldp': %s",
+ strerror(-r));
+
+
r = drop_privileges(uid, gid,
(1ULL << CAP_NET_ADMIN) |
(1ULL << CAP_NET_BIND_SERVICE) |
diff --git a/src/systemd/sd-network.h b/src/systemd/sd-network.h
index bb69940930..027730d11d 100644
--- a/src/systemd/sd-network.h
+++ b/src/systemd/sd-network.h
@@ -111,6 +111,8 @@ int sd_network_link_get_ntp(int ifindex, char ***addr);
*/
int sd_network_link_get_llmnr(int ifindex, char **llmnr);
+int sd_network_link_get_lldp(int ifindex, char **lldp);
+
/* Get the DNS domain names for a given link. */
int sd_network_link_get_domains(int ifindex, char ***domains);