1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <net/if_arp.h>
10 #include "networkd-link.h"
11 #include "networkd-lldp-rx.h"
12 #include "networkd-lldp-tx.h"
13 #include "networkd-manager.h"
14 #include "networkd-network.h"
15 #include "string-table.h"
16 #include "string-util.h"
18 #include "tmpfile-util.h"
20 DEFINE_CONFIG_PARSE_ENUM(config_parse_lldp_mode
, lldp_mode
, LLDPMode
, "Failed to parse LLDP= setting.");
22 static const char* const lldp_mode_table
[_LLDP_MODE_MAX
] = {
23 [LLDP_MODE_NO
] = "no",
24 [LLDP_MODE_YES
] = "yes",
25 [LLDP_MODE_ROUTERS_ONLY
] = "routers-only",
28 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(lldp_mode
, LLDPMode
, LLDP_MODE_YES
);
30 static bool link_lldp_rx_enabled(Link
*link
) {
33 if (link
->flags
& IFF_LOOPBACK
)
36 if (link
->iftype
!= ARPHRD_ETHER
)
42 /* LLDP should be handled on bridge and bond slaves as those have a direct connection to their peers,
43 * not on the bridge/bond master. Linux doesn't even (by default) forward lldp packets to the bridge
45 if (link
->kind
&& STR_IN_SET(link
->kind
, "bridge", "bond"))
48 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
51 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event_t event
, sd_lldp_neighbor
*n
, void *userdata
) {
52 Link
*link
= userdata
;
57 (void) link_lldp_save(link
);
59 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
60 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
62 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
64 r
= link_lldp_emit_start(link
);
66 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
70 int link_lldp_rx_configure(Link
*link
) {
73 if (!link_lldp_rx_enabled(link
))
79 r
= sd_lldp_new(&link
->lldp
);
83 r
= sd_lldp_attach_event(link
->lldp
, link
->manager
->event
, 0);
87 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
91 r
= sd_lldp_match_capabilities(link
->lldp
,
92 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
93 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
94 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
98 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->hw_addr
.ether
);
102 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
106 r
= link_update_lldp(link
);
113 int link_update_lldp(Link
*link
) {
121 if (link
->flags
& IFF_UP
) {
122 r
= sd_lldp_start(link
->lldp
);
124 return log_link_warning_errno(link
, r
, "Failed to start LLDP: %m");
126 log_link_debug(link
, "Started LLDP.");
128 r
= sd_lldp_stop(link
->lldp
);
130 return log_link_warning_errno(link
, r
, "Failed to stop LLDP: %m");
132 log_link_debug(link
, "Stopped LLDP.");
138 int link_lldp_save(Link
*link
) {
139 _cleanup_(unlink_and_freep
) char *temp_path
= NULL
;
140 _cleanup_fclose_
FILE *f
= NULL
;
141 sd_lldp_neighbor
**l
= NULL
;
145 assert(link
->lldp_file
);
148 (void) unlink(link
->lldp_file
);
152 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
156 (void) unlink(link
->lldp_file
);
162 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
166 (void) fchmod(fileno(f
), 0644);
168 for (i
= 0; i
< n
; i
++) {
173 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
178 (void) fwrite(&u
, 1, sizeof(u
), f
);
179 (void) fwrite(p
, 1, sz
, f
);
182 r
= fflush_and_check(f
);
186 r
= conservative_rename(temp_path
, link
->lldp_file
);
192 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
195 for (i
= 0; i
< n
; i
++)
196 sd_lldp_neighbor_unref(l
[i
]);