1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <net/if_arp.h>
8 #include "alloc-util.h"
12 #include "hostname-util.h"
13 #include "missing_network.h"
14 #include "networkd-link.h"
15 #include "networkd-lldp-tx.h"
16 #include "networkd-manager.h"
17 #include "parse-util.h"
18 #include "random-util.h"
19 #include "socket-util.h"
20 #include "string-util.h"
22 #include "unaligned.h"
25 /* The LLDP spec calls this "txFastInit", see 9.2.5.19 */
26 #define LLDP_TX_FAST_INIT 4U
28 /* The LLDP spec calls this "msgTxHold", see 9.2.5.6 */
29 #define LLDP_TX_HOLD 4U
31 /* The jitter range to add, see 9.2.2. */
32 #define LLDP_JITTER_USEC (400U * USEC_PER_MSEC)
34 /* The LLDP spec calls this msgTxInterval, but we subtract half the jitter off it. */
35 #define LLDP_TX_INTERVAL_USEC (30U * USEC_PER_SEC - LLDP_JITTER_USEC / 2)
37 /* The LLDP spec calls this msgFastTx, but we subtract half the jitter off it. */
38 #define LLDP_FAST_TX_USEC (1U * USEC_PER_SEC - LLDP_JITTER_USEC / 2)
40 static const struct ether_addr lldp_multicast_addr
[_LLDP_EMIT_MAX
] = {
41 [LLDP_EMIT_NEAREST_BRIDGE
] = {{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e }},
42 [LLDP_EMIT_NON_TPMR_BRIDGE
] = {{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 }},
43 [LLDP_EMIT_CUSTOMER_BRIDGE
] = {{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }},
46 bool link_lldp_emit_enabled(Link
*link
) {
49 if (link
->flags
& IFF_LOOPBACK
)
52 if (link
->iftype
!= ARPHRD_ETHER
)
58 if (link
->kind
&& STR_IN_SET(link
->kind
, "bridge", "bond"))
61 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
64 static int lldp_write_tlv_header(uint8_t **p
, uint8_t id
, size_t sz
) {
72 (*p
)[0] = (id
<< 1) | !!(sz
& 256);
79 static int lldp_make_packet(
81 const struct ether_addr
*hwaddr
,
82 const char *machine_id
,
85 const char *port_description
,
87 const char *pretty_hostname
,
88 uint16_t system_capabilities
,
89 uint16_t enabled_capabilities
,
91 void **ret
, size_t *sz
) {
93 size_t machine_id_length
, ifname_length
, port_description_length
= 0, hostname_length
= 0,
94 pretty_hostname_length
= 0, mud_length
= 0;
95 _cleanup_free_
void *packet
= NULL
;
96 struct ether_header
*h
;
101 assert(mode
> LLDP_EMIT_NO
);
102 assert(mode
< _LLDP_EMIT_MAX
);
109 machine_id_length
= strlen(machine_id
);
110 ifname_length
= strlen(ifname
);
112 if (port_description
)
113 port_description_length
= strlen(port_description
);
116 hostname_length
= strlen(hostname
);
119 pretty_hostname_length
= strlen(pretty_hostname
);
122 mud_length
= strlen(mud
);
124 l
= sizeof(struct ether_header
) +
126 2 + 1 + machine_id_length
+
128 2 + 1 + ifname_length
+
131 /* System Capabilities */
136 /* Port Description */
137 if (port_description
)
138 l
+= 2 + port_description_length
;
142 l
+= 2 + hostname_length
;
144 /* System Description */
146 l
+= 2 + pretty_hostname_length
;
150 l
+= 2 + sizeof(SD_LLDP_OUI_MUD
) + 1 + mud_length
;
156 h
= (struct ether_header
*) packet
;
157 h
->ether_type
= htobe16(ETHERTYPE_LLDP
);
158 memcpy(h
->ether_dhost
, lldp_multicast_addr
+ mode
, ETH_ALEN
);
159 memcpy(h
->ether_shost
, hwaddr
, ETH_ALEN
);
161 p
= (uint8_t*) packet
+ sizeof(struct ether_header
);
163 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_CHASSIS_ID
, 1 + machine_id_length
);
166 *(p
++) = SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED
;
167 p
= mempcpy(p
, machine_id
, machine_id_length
);
169 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_PORT_ID
, 1 + ifname_length
);
172 *(p
++) = SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME
;
173 p
= mempcpy(p
, ifname
, ifname_length
);
175 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_TTL
, 2);
178 unaligned_write_be16(p
, ttl
);
181 if (port_description
) {
182 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_PORT_DESCRIPTION
, port_description_length
);
185 p
= mempcpy(p
, port_description
, port_description_length
);
189 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_SYSTEM_NAME
, hostname_length
);
192 p
= mempcpy(p
, hostname
, hostname_length
);
195 if (pretty_hostname
) {
196 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_SYSTEM_DESCRIPTION
, pretty_hostname_length
);
199 p
= mempcpy(p
, pretty_hostname
, pretty_hostname_length
);
203 uint8_t oui_mud
[sizeof(SD_LLDP_OUI_MUD
)] = {0x00, 0x00, 0x5E};
205 * +--------+--------+----------+---------+--------------
206 * |TLV Type| len | OUI |subtype | MUDString
207 * | =127 | |= 00 00 5E| = 1 |
208 * |(7 bits)|(9 bits)|(3 octets)|(1 octet)|(1-255 octets)
209 * +--------+--------+----------+---------+--------------
212 * o TLV Type = 127 indicates a vendor-specific TLV
213 * o len = indicates the TLV string length
214 * o OUI = 00 00 5E is the organizationally unique identifier of IANA
215 * o subtype = 1 (as assigned by IANA for the MUDstring)
216 * o MUDstring = the length MUST NOT exceed 255 octets
219 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_PRIVATE
, sizeof(SD_LLDP_OUI_MUD
) + 1 + mud_length
);
223 p
= mempcpy(p
, &oui_mud
, sizeof(SD_LLDP_OUI_MUD
));
224 *(p
++) = SD_LLDP_OUI_SUBTYPE_MUD_USAGE_DESCRIPTION
;
225 p
= mempcpy(p
, mud
, mud_length
);
228 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_SYSTEM_CAPABILITIES
, 4);
231 unaligned_write_be16(p
, system_capabilities
);
233 unaligned_write_be16(p
, enabled_capabilities
);
236 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_END
, 0);
240 assert(p
== (uint8_t*) packet
+ l
);
242 *ret
= TAKE_PTR(packet
);
248 static int lldp_send_packet(
250 const struct ether_addr
*address
,
252 size_t packet_size
) {
254 union sockaddr_union sa
= {
255 .ll
.sll_family
= AF_PACKET
,
256 .ll
.sll_protocol
= htobe16(ETHERTYPE_LLDP
),
257 .ll
.sll_ifindex
= ifindex
,
258 .ll
.sll_halen
= ETH_ALEN
,
261 _cleanup_close_
int fd
= -1;
266 assert(packet
|| packet_size
<= 0);
268 memcpy(sa
.ll
.sll_addr
, address
, ETH_ALEN
);
270 fd
= socket(PF_PACKET
, SOCK_RAW
|SOCK_CLOEXEC
, IPPROTO_RAW
);
274 l
= sendto(fd
, packet
, packet_size
, MSG_NOSIGNAL
, &sa
.sa
, sizeof(sa
.ll
));
278 if ((size_t) l
!= packet_size
)
284 static int link_send_lldp(Link
*link
) {
285 char machine_id_string
[SD_ID128_STRING_MAX
];
286 _cleanup_free_
char *hostname
= NULL
, *pretty_hostname
= NULL
;
287 _cleanup_free_
void *packet
= NULL
;
288 size_t packet_size
= 0;
289 sd_id128_t machine_id
;
296 if (!link
->network
|| link
->network
->lldp_emit
== LLDP_EMIT_NO
)
299 assert(link
->network
->lldp_emit
< _LLDP_EMIT_MAX
);
301 r
= sd_id128_get_machine(&machine_id
);
305 (void) gethostname_strict(&hostname
);
306 (void) parse_env_file(NULL
, "/etc/machine-info", "PRETTY_HOSTNAME", &pretty_hostname
);
308 assert_cc(LLDP_TX_INTERVAL_USEC
* LLDP_TX_HOLD
+ 1 <= (UINT16_MAX
- 1) * USEC_PER_SEC
);
309 ttl
= DIV_ROUND_UP(LLDP_TX_INTERVAL_USEC
* LLDP_TX_HOLD
+ 1, USEC_PER_SEC
);
311 caps
= (link
->network
&& link
->network
->ip_forward
!= ADDRESS_FAMILY_NO
) ?
312 SD_LLDP_SYSTEM_CAPABILITIES_ROUTER
:
313 SD_LLDP_SYSTEM_CAPABILITIES_STATION
;
315 r
= lldp_make_packet(link
->network
->lldp_emit
,
317 sd_id128_to_string(machine_id
, machine_id_string
),
320 link
->network
? link
->network
->description
: NULL
,
323 SD_LLDP_SYSTEM_CAPABILITIES_STATION
|SD_LLDP_SYSTEM_CAPABILITIES_BRIDGE
|SD_LLDP_SYSTEM_CAPABILITIES_ROUTER
,
325 link
->network
? link
->network
->lldp_mud
: NULL
,
326 &packet
, &packet_size
);
330 return lldp_send_packet(link
->ifindex
, lldp_multicast_addr
+ link
->network
->lldp_emit
, packet
, packet_size
);
333 static int on_lldp_timer(sd_event_source
*s
, usec_t t
, void *userdata
) {
334 Link
*link
= userdata
;
341 log_link_debug(link
, "Sending LLDP packet...");
343 r
= link_send_lldp(link
);
345 log_link_debug_errno(link
, r
, "Failed to send LLDP packet, ignoring: %m");
347 if (link
->lldp_tx_fast
> 0)
348 link
->lldp_tx_fast
--;
350 delay
= link
->lldp_tx_fast
> 0 ? LLDP_FAST_TX_USEC
: LLDP_TX_INTERVAL_USEC
;
351 delay
= usec_add(delay
, (usec_t
) random_u64() % LLDP_JITTER_USEC
);
353 r
= sd_event_source_set_time_relative(s
, delay
);
355 return log_link_error_errno(link
, r
, "Failed to restart LLDP timer: %m");
357 r
= sd_event_source_set_enabled(s
, SD_EVENT_ONESHOT
);
359 return log_link_error_errno(link
, r
, "Failed to enable LLDP timer: %m");
364 int link_lldp_emit_start(Link
*link
) {
370 if (!link
->network
|| link
->network
->lldp_emit
== LLDP_EMIT_NO
) {
371 link_lldp_emit_stop(link
);
375 /* Starts the LLDP transmission in "fast" mode. If it is already started, turns "fast" mode back on again. */
377 link
->lldp_tx_fast
= LLDP_TX_FAST_INIT
;
379 next
= usec_add(usec_add(now(clock_boottime_or_monotonic()), LLDP_FAST_TX_USEC
),
380 (usec_t
) random_u64() % LLDP_JITTER_USEC
);
382 if (link
->lldp_emit_event_source
) {
385 /* Lower the timeout, maybe */
386 r
= sd_event_source_get_time(link
->lldp_emit_event_source
, &old
);
393 return sd_event_source_set_time(link
->lldp_emit_event_source
, next
);
395 r
= sd_event_add_time(
396 link
->manager
->event
,
397 &link
->lldp_emit_event_source
,
398 clock_boottime_or_monotonic(),
406 (void) sd_event_source_set_description(link
->lldp_emit_event_source
, "lldp-tx");
412 void link_lldp_emit_stop(Link
*link
) {
415 link
->lldp_emit_event_source
= sd_event_source_unref(link
->lldp_emit_event_source
);
418 int config_parse_lldp_emit(
420 const char *filename
,
423 unsigned section_line
,
430 LLDPEmit
*emit
= data
;
438 *emit
= LLDP_EMIT_NO
;
439 else if (streq(rvalue
, "nearest-bridge"))
440 *emit
= LLDP_EMIT_NEAREST_BRIDGE
;
441 else if (streq(rvalue
, "non-tpmr-bridge"))
442 *emit
= LLDP_EMIT_NON_TPMR_BRIDGE
;
443 else if (streq(rvalue
, "customer-bridge"))
444 *emit
= LLDP_EMIT_CUSTOMER_BRIDGE
;
446 r
= parse_boolean(rvalue
);
448 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Failed to parse LLDP emission setting, ignoring: %s", rvalue
);
452 *emit
= r
? LLDP_EMIT_NEAREST_BRIDGE
: LLDP_EMIT_NO
;
458 int config_parse_lldp_mud(
460 const char *filename
,
463 unsigned section_line
,
470 _cleanup_free_
char *unescaped
= NULL
;
478 r
= cunescape(rvalue
, 0, &unescaped
);
480 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
481 "Failed to Failed to unescape LLDP MUD URL, ignoring: %s", rvalue
);
485 if (!http_url_is_valid(unescaped
) || strlen(unescaped
) > 255) {
486 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
487 "Failed to parse LLDP MUD URL '%s', ignoring: %m", rvalue
);
492 return free_and_replace(n
->lldp_mud
, unescaped
);