Debian Codegraph @

src/libsystemd-network/sd-lldp-tx.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-event.h"
4 #include "sd-id128.h"
5 #include "sd-lldp-tx.h"
6
7 #include "alloc-util.h"
8 #include "env-util.h"
9 #include "ether-addr-util.h"
10 #include "fd-util.h"
11 #include "hostname-setup.h"
12 #include "hostname-util.h"
13 #include "json-util.h"
14 #include "network-common.h"
15 #include "random-util.h"
16 #include "socket-util.h"
17 #include "string-util.h"
18 #include "time-util.h"
19 #include "unaligned.h"
20 #include "web-util.h"
21
22 #define LLDP_APP_ID SD_ID128_MAKE(07,3a,43,bf,54,de,40,8d,8e,c4,96,ed,fd,94,72,dc)
23
24 /* The LLDP spec calls this "txFastInit", see 9.2.5.19 */
25 #define LLDP_FAST_TX_INIT 4U
26
27 /* The LLDP spec calls this "msgTxHold", see 9.2.5.6 */
28 #define LLDP_TX_HOLD 4U
29
30 /* The jitter range to add, see 9.2.2. */
31 #define LLDP_TX_JITTER_USEC (400U * USEC_PER_MSEC)
32
33 /* The LLDP spec calls this msgTxInterval, but we subtract half the jitter off it. */
34 #define LLDP_TX_INTERVAL_USEC (30U * USEC_PER_SEC - LLDP_TX_JITTER_USEC / 2)
35
36 /* The LLDP spec calls this msgFastTx, but we subtract half the jitter off it. */
37 #define LLDP_FAST_TX_INTERVAL_USEC (1U * USEC_PER_SEC - LLDP_TX_JITTER_USEC / 2)
38
39 #define LLDP_TX_TTL ((uint16_t) DIV_ROUND_UP(LLDP_TX_INTERVAL_USEC * LLDP_TX_HOLD + 1, USEC_PER_SEC))
40
41 static const struct ether_addr lldp_multicast_addr[_SD_LLDP_MULTICAST_MODE_MAX] = {
42 [SD_LLDP_MULTICAST_MODE_NEAREST_BRIDGE] = {{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e }},
43 [SD_LLDP_MULTICAST_MODE_NON_TPMR_BRIDGE] = {{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 }},
44 [SD_LLDP_MULTICAST_MODE_CUSTOMER_BRIDGE] = {{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }},
45 };
46
47 struct sd_lldp_tx {
48 unsigned n_ref;
49
50 int ifindex;
51 char *ifname;
52
53 sd_event *event;
54 int64_t event_priority;
55 sd_event_source *timer_event_source;
56
57 unsigned fast_tx;
58
59 sd_lldp_multicast_mode_t mode;
60 struct ether_addr hwaddr;
61
62 char *port_description;
63 char *hostname;
64 char *pretty_hostname;
65 char *mud_url;
66 uint16_t supported_capabilities;
67 uint16_t enabled_capabilities;
68 uint16_t vlan_id;
69 };
70
71 #define log_lldp_tx_errno(lldp_tx, error, fmt, ...) \
72 log_interface_prefix_full_errno( \
73 "LLDP Tx: ", \
74 sd_lldp_tx, lldp_tx, \
75 error, fmt, ##__VA_ARGS__)
76 #define log_lldp_tx(lldp_tx, fmt, ...) \
77 log_interface_prefix_full_errno_zerook( \
78 "LLDP Tx: ", \
79 sd_lldp_tx, lldp_tx, \
80 0, fmt, ##__VA_ARGS__)
81
82 static sd_lldp_tx *lldp_tx_free(sd_lldp_tx *lldp_tx) {
83 if (!lldp_tx)
84 return NULL;
85
86 sd_lldp_tx_detach_event(lldp_tx);
87
88 free(lldp_tx->port_description);
89 free(lldp_tx->hostname);
90 free(lldp_tx->pretty_hostname);
91 free(lldp_tx->mud_url);
92
93 free(lldp_tx->ifname);
94 return mfree(lldp_tx);
95 }
96
97 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_lldp_tx, sd_lldp_tx, lldp_tx_free);
98
99 int sd_lldp_tx_new(sd_lldp_tx **ret) {
100 _cleanup_(sd_lldp_tx_unrefp) sd_lldp_tx *lldp_tx = NULL;
101
102 assert_return(ret, -EINVAL);
103
104 lldp_tx = new(sd_lldp_tx, 1);
105 if (!lldp_tx)
106 return -ENOMEM;
107
108 *lldp_tx = (sd_lldp_tx) {
109 .n_ref = 1,
110 .mode = _SD_LLDP_MULTICAST_MODE_INVALID,
111 };
112
113 *ret = TAKE_PTR(lldp_tx);
114 return 0;
115 }
116
117 int sd_lldp_tx_set_ifindex(sd_lldp_tx *lldp_tx, int ifindex) {
118 assert_return(lldp_tx, -EINVAL);
119 assert_return(ifindex > 0, -EINVAL);
120
121 lldp_tx->ifindex = ifindex;
122 return 0;
123 }
124
125 int sd_lldp_tx_set_ifname(sd_lldp_tx *lldp_tx, const char *ifname) {
126 assert_return(lldp_tx, -EINVAL);
127 assert_return(ifname, -EINVAL);
128
129 if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
130 return -EINVAL;
131
132 return free_and_strdup(&lldp_tx->ifname, ifname);
133 }
134
135 int sd_lldp_tx_get_ifname(sd_lldp_tx *lldp_tx, const char **ret) {
136 int r;
137
138 assert_return(lldp_tx, -EINVAL);
139
140 r = get_ifname(lldp_tx->ifindex, &lldp_tx->ifname);
141 if (r < 0)
142 return r;
143
144 if (ret)
145 *ret = lldp_tx->ifname;
146
147 return 0;
148 }
149
150 int sd_lldp_tx_set_multicast_mode(sd_lldp_tx *lldp_tx, sd_lldp_multicast_mode_t mode) {
151 assert_return(lldp_tx, -EINVAL);
152 assert_return(mode >= 0 && mode < _SD_LLDP_MULTICAST_MODE_MAX, -EINVAL);
153
154 lldp_tx->mode = mode;
155 return 0;
156 }
157
158 int sd_lldp_tx_set_hwaddr(sd_lldp_tx *lldp_tx, const struct ether_addr *hwaddr) {
159 assert_return(lldp_tx, -EINVAL);
160 assert_return(hwaddr, -EINVAL);
161 assert_return(!ether_addr_is_null(hwaddr), -EINVAL);
162
163 lldp_tx->hwaddr = *hwaddr;
164 return 0;
165 }
166
167 int sd_lldp_tx_set_capabilities(sd_lldp_tx *lldp_tx, uint16_t supported, uint16_t enabled) {
168 assert_return(lldp_tx, -EINVAL);
169 assert_return((enabled & ~supported) == 0, -EINVAL);
170
171 lldp_tx->supported_capabilities = supported;
172 lldp_tx->enabled_capabilities = enabled;
173 return 0;
174 }
175
176 int sd_lldp_tx_set_port_description(sd_lldp_tx *lldp_tx, const char *port_description) {
177 assert_return(lldp_tx, -EINVAL);
178
179 /* An empty string unset the previously set hostname. */
180 if (strlen_ptr(port_description) >= 512)
181 return -EINVAL;
182
183 return free_and_strdup(&lldp_tx->port_description, empty_to_null(port_description));
184 }
185
186 int sd_lldp_tx_set_hostname(sd_lldp_tx *lldp_tx, const char *hostname) {
187 assert_return(lldp_tx, -EINVAL);
188
189 /* An empty string unset the previously set hostname. */
190 if (!isempty(hostname) && !hostname_is_valid(hostname, /* flags= */ 0))
191 return -EINVAL;
192
193 return free_and_strdup(&lldp_tx->hostname, empty_to_null(hostname));
194 }
195
196 int sd_lldp_tx_set_pretty_hostname(sd_lldp_tx *lldp_tx, const char *pretty_hostname) {
197 assert_return(lldp_tx, -EINVAL);
198
199 /* An empty string unset the previously set hostname. */
200 if (strlen_ptr(pretty_hostname) >= 512)
201 return -EINVAL;
202
203 return free_and_strdup(&lldp_tx->pretty_hostname, empty_to_null(pretty_hostname));
204 }
205
206 int sd_lldp_tx_set_mud_url(sd_lldp_tx *lldp_tx, const char *mud_url) {
207 assert_return(lldp_tx, -EINVAL);
208
209 /* An empty string unset the previously set hostname. */
210 if (!isempty(mud_url)) {
211 /* Unless the maximum length of each value is 511, the MUD url must be smaller than 256.
212 * See RFC 8520. */
213 if (strlen(mud_url) >= 256)
214 return -EINVAL;
215
216 if (!http_url_is_valid(mud_url))
217 return -EINVAL;
218 }
219
220 return free_and_strdup(&lldp_tx->mud_url, empty_to_null(mud_url));
221 }
222
223 int sd_lldp_tx_set_vlan_id(sd_lldp_tx *lldp_tx, uint16_t vlan_id) {
224 assert_return(lldp_tx, -EINVAL);
225
226 lldp_tx->vlan_id = vlan_id;
227 return 0;
228 }
229
230 static size_t lldp_tx_calculate_maximum_packet_size(sd_lldp_tx *lldp_tx, const char *hostname, const char *pretty_hostname) {
231 assert(lldp_tx);
232 assert(lldp_tx->ifindex > 0);
233
234 return sizeof(struct ether_header) +
235 /* Chassis ID */
236 2 + 1 + (SD_ID128_STRING_MAX - 1) +
237 /* Port ID */
238 2 + 1 + strlen_ptr(lldp_tx->ifname) +
239 /* TTL */
240 2 + 2 +
241 /* Port description */
242 2 + strlen_ptr(lldp_tx->port_description) +
243 /* System name */
244 2 + strlen_ptr(hostname) +
245 /* System description */
246 2 + strlen_ptr(pretty_hostname) +
247 /* System Capabilities */
248 2 + 4 +
249 /* MUD URL */
250 2 + sizeof(SD_LLDP_OUI_IANA_MUD) + strlen_ptr(lldp_tx->mud_url) +
251 /* VLAN ID */
252 2 + sizeof(SD_LLDP_OUI_802_1_VLAN_ID) + sizeof(uint16_t) +
253 /* End */
254 2;
255 }
256
257 static int packet_append_tlv_header(uint8_t *packet, size_t packet_size, size_t *offset, uint8_t type, size_t data_len) {
258 assert(packet);
259 assert(offset);
260
261 /*
262 * +--------+--------+--------------
263 * |TLV Type| len | value
264 * |(7 bits)|(9 bits)|(0-511 octets)
265 * +--------+--------+--------------
266 * where:
267 *
268 * len = indicates the length of value
269 */
270
271 /* The type field is 7-bits. */
272 if (type >= 128)
273 return -EINVAL;
274
275 /* The data length field is 9-bits. */
276 if (data_len >= 512)
277 return -EINVAL;
278
279 if (packet_size < 2 + data_len)
280 return -ENOBUFS;
281
282 if (*offset > packet_size - 2 - data_len)
283 return -ENOBUFS;
284
285 packet[(*offset)++] = (type << 1) | !!(data_len >> 8);
286 packet[(*offset)++] = data_len & (size_t) UINT8_MAX;
287
288 return 0;
289 }
290
291 static int packet_append_prefixed_string(
292 uint8_t *packet,
293 size_t packet_size,
294 size_t *offset,
295 uint8_t type,
296 size_t prefix_len,
297 const void *prefix,
298 const char *str) {
299
300 size_t len;
301 int r;
302
303 assert(packet);
304 assert(offset);
305 assert(prefix_len == 0 || prefix);
306
307 if (isempty(str))
308 return 0;
309
310 len = strlen(str);
311
312 /* Check for overflow */
313 if (len > SIZE_MAX - prefix_len)
314 return -ENOBUFS;
315
316 r = packet_append_tlv_header(packet, packet_size, offset, type, prefix_len + len);
317 if (r < 0)
318 return r;
319
320 memcpy_safe(packet + *offset, prefix, prefix_len);
321 *offset += prefix_len;
322
323 memcpy(packet + *offset, str, len);
324 *offset += len;
325
326 return 0;
327 }
328
329 static int packet_append_string(
330 uint8_t *packet,
331 size_t packet_size,
332 size_t *offset,
333 uint8_t type,
334 const char *str) {
335
336 return packet_append_prefixed_string(packet, packet_size, offset, type, 0, NULL, str);
337 }
338
339 static int lldp_tx_get_machine_id(sd_id128_t *ret) {
340 int r;
341
342 assert(ret);
343
344 /* Unfortunately we previously exposed machine ID. If the environment variable is set, then
345 * use the machine ID as is. Otherwise, use application specific one. */
346 r = secure_getenv_bool("SYSTEMD_LLDP_SEND_MACHINE_ID");
347 if (r < 0 && r != -ENXIO)
348 log_debug_errno(r, "Failed to parse $SYSTEMD_LLDP_SEND_MACHINE_ID, ignoring: %m");
349 if (r > 0)
350 return sd_id128_get_machine(ret);
351
352 return sd_id128_get_machine_app_specific(LLDP_APP_ID, ret);
353 }
354
355 static int lldp_tx_create_packet(sd_lldp_tx *lldp_tx, size_t *ret_packet_size, uint8_t **ret_packet) {
356 _cleanup_free_ char *hostname = NULL, *pretty_hostname = NULL;
357 _cleanup_free_ uint8_t *packet = NULL;
358 struct ether_header *header;
359 size_t packet_size, offset;
360 sd_id128_t machine_id;
361 int r;
362
363 assert(lldp_tx);
364 assert(lldp_tx->ifindex > 0);
365 assert(ret_packet_size);
366 assert(ret_packet);
367
368 /* If ifname is not set yet, set ifname from ifindex. */
369 r = sd_lldp_tx_get_ifname(lldp_tx, /* ret= */ NULL);
370 if (r < 0)
371 return r;
372
373 r = lldp_tx_get_machine_id(&machine_id);
374 if (r < 0)
375 return r;
376
377 if (!lldp_tx->hostname)
378 (void) gethostname_strict(&hostname);
379 if (!lldp_tx->pretty_hostname)
380 (void) get_pretty_hostname(&pretty_hostname);
381
382 packet_size = lldp_tx_calculate_maximum_packet_size(lldp_tx,
383 lldp_tx->hostname ?: hostname,
384 lldp_tx->pretty_hostname ?: pretty_hostname);
385
386 packet = new(uint8_t, packet_size);
387 if (!packet)
388 return -ENOMEM;
389
390 header = (struct ether_header*) packet;
391 header->ether_type = htobe16(ETH_P_LLDP);
392 memcpy(header->ether_dhost, lldp_multicast_addr + lldp_tx->mode, ETH_ALEN);
393 memcpy(header->ether_shost, &lldp_tx->hwaddr, ETH_ALEN);
394
395 offset = sizeof(struct ether_header);
396
397 /* The three mandatory TLVs must appear first, in this specific order:
398 * 1. Chassis ID
399 * 2. Port ID
400 * 3. Time To Live
401 */
402
403 r = packet_append_prefixed_string(packet, packet_size, &offset, SD_LLDP_TYPE_CHASSIS_ID,
404 1, (const uint8_t[]) { SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED },
405 SD_ID128_TO_STRING(machine_id));
406 if (r < 0)
407 return r;
408
409 r = packet_append_prefixed_string(packet, packet_size, &offset, SD_LLDP_TYPE_PORT_ID,
410 1, (const uint8_t[]) { SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME },
411 lldp_tx->ifname);
412 if (r < 0)
413 return r;
414
415 r = packet_append_tlv_header(packet, packet_size, &offset, SD_LLDP_TYPE_TTL, 2);
416 if (r < 0)
417 return r;
418
419 unaligned_write_be16(packet + offset, LLDP_TX_TTL);
420 offset += 2;
421
422 /* Optional TLVs follow, in no specific order: */
423
424 r = packet_append_string(packet, packet_size, &offset, SD_LLDP_TYPE_PORT_DESCRIPTION,
425 lldp_tx->port_description);
426 if (r < 0)
427 return r;
428
429 r = packet_append_string(packet, packet_size, &offset, SD_LLDP_TYPE_SYSTEM_NAME,
430 lldp_tx->hostname ?: hostname);
431 if (r < 0)
432 return r;
433
434 r = packet_append_string(packet, packet_size, &offset, SD_LLDP_TYPE_SYSTEM_DESCRIPTION,
435 lldp_tx->pretty_hostname ?: pretty_hostname);
436 if (r < 0)
437 return r;
438
439 r = packet_append_tlv_header(packet, packet_size, &offset, SD_LLDP_TYPE_SYSTEM_CAPABILITIES, 4);
440 if (r < 0)
441 return r;
442
443 unaligned_write_be16(packet + offset, lldp_tx->supported_capabilities);
444 offset += 2;
445 unaligned_write_be16(packet + offset, lldp_tx->enabled_capabilities);
446 offset += 2;
447
448 /* See section 12 of RFC 8520.
449 * +--------+--------+----------+---------+--------------
450 * |TLV Type| len | OUI |subtype | MUDString
451 * | =127 | |= 00 00 5E| = 1 |
452 * |(7 bits)|(9 bits)|(3 octets)|(1 octet)|(1-255 octets)
453 * +--------+--------+----------+---------+--------------
454 * where:
455 *
456 * o TLV Type = 127 indicates a vendor-specific TLV
457 * o len = indicates the TLV string length
458 * o OUI = 00 00 5E is the organizationally unique identifier of IANA
459 * o subtype = 1 (as assigned by IANA for the MUDstring)
460 * o MUDstring = the length MUST NOT exceed 255 octets
461 */
462 r = packet_append_prefixed_string(packet, packet_size, &offset, SD_LLDP_TYPE_PRIVATE,
463 sizeof(SD_LLDP_OUI_IANA_MUD), SD_LLDP_OUI_IANA_MUD,
464 lldp_tx->mud_url);
465 if (r < 0)
466 return r;
467
468 /* VLAN ID */
469 if (lldp_tx->vlan_id > 0) {
470 r = packet_append_tlv_header(packet, packet_size, &offset, SD_LLDP_TYPE_PRIVATE,
471 sizeof(SD_LLDP_OUI_802_1_VLAN_ID) + sizeof(uint16_t));
472 if (r < 0)
473 return r;
474
475 memcpy_safe(packet + offset, SD_LLDP_OUI_802_1_VLAN_ID, sizeof(SD_LLDP_OUI_802_1_VLAN_ID));
476 offset += sizeof(SD_LLDP_OUI_802_1_VLAN_ID);
477 unaligned_write_be16(packet + offset, lldp_tx->vlan_id);
478 offset += 2;
479 }
480
481 r = packet_append_tlv_header(packet, packet_size, &offset, SD_LLDP_TYPE_END, 0);
482 if (r < 0)
483 return r;
484
485 *ret_packet_size = offset;
486 *ret_packet = TAKE_PTR(packet);
487 return 0;
488 }
489
490 static int lldp_tx_send_packet(sd_lldp_tx *lldp_tx, size_t packet_size, const uint8_t *packet) {
491 _cleanup_close_ int fd = -EBADF;
492 union sockaddr_union sa;
493 ssize_t l;
494
495 assert(lldp_tx);
496 assert(lldp_tx->ifindex > 0);
497 assert(packet_size > sizeof(struct ether_header));
498 assert(packet);
499
500 sa = (union sockaddr_union) {
501 .ll.sll_family = AF_PACKET,
502 .ll.sll_protocol = htobe16(ETH_P_LLDP),
503 .ll.sll_ifindex = lldp_tx->ifindex,
504 .ll.sll_halen = ETH_ALEN,
505 };
506 memcpy(sa.ll.sll_addr, lldp_multicast_addr + lldp_tx->mode, ETH_ALEN);
507
508 fd = socket(AF_PACKET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
509 if (fd < 0)
510 return -errno;
511
512 l = sendto(fd, packet, packet_size, MSG_NOSIGNAL, &sa.sa, sizeof(sa.ll));
513 if (l < 0)
514 return -errno;
515
516 if ((size_t) l != packet_size)
517 return -EIO;
518
519 return 0;
520 }
521
522 static int lldp_tx_send(sd_lldp_tx *lldp_tx) {
523 _cleanup_free_ uint8_t *packet = NULL;
524 size_t packet_size = 0; /* avoid false maybe-uninitialized warning */
525 int r;
526
527 assert(lldp_tx);
528
529 r = lldp_tx_create_packet(lldp_tx, &packet_size, &packet);
530 if (r < 0)
531 return r;
532
533 return lldp_tx_send_packet(lldp_tx, packet_size, packet);
534 }
535
536 int sd_lldp_tx_attach_event(sd_lldp_tx *lldp_tx, sd_event *event, int64_t priority) {
537 int r;
538
539 assert_return(lldp_tx, -EINVAL);
540 assert_return(!lldp_tx->event, -EBUSY);
541
542 if (event)
543 lldp_tx->event = sd_event_ref(event);
544 else {
545 r = sd_event_default(&lldp_tx->event);
546 if (r < 0)
547 return r;
548 }
549
550 lldp_tx->event_priority = priority;
551
552 return 0;
553 }
554
555 int sd_lldp_tx_detach_event(sd_lldp_tx *lldp_tx) {
556 assert_return(lldp_tx, -EINVAL);
557
558 lldp_tx->timer_event_source = sd_event_source_disable_unref(lldp_tx->timer_event_source);
559 lldp_tx->event = sd_event_unref(lldp_tx->event);
560 return 0;
561 }
562
563 static usec_t lldp_tx_get_delay(sd_lldp_tx *lldp_tx) {
564 assert(lldp_tx);
565
566 return usec_add(lldp_tx->fast_tx > 0 ? LLDP_FAST_TX_INTERVAL_USEC : LLDP_TX_INTERVAL_USEC,
567 (usec_t) random_u64() % LLDP_TX_JITTER_USEC);
568 }
569
570 static int lldp_tx_reset_timer(sd_lldp_tx *lldp_tx) {
571 usec_t delay;
572 int r;
573
574 assert(lldp_tx);
575 assert(lldp_tx->timer_event_source);
576
577 delay = lldp_tx_get_delay(lldp_tx);
578
579 r = sd_event_source_set_time_relative(lldp_tx->timer_event_source, delay);
580 if (r < 0)
581 return r;
582
583 return sd_event_source_set_enabled(lldp_tx->timer_event_source, SD_EVENT_ONESHOT);
584 }
585
586 static int on_timer_event(sd_event_source *s, uint64_t usec, void *userdata) {
587 sd_lldp_tx *lldp_tx = ASSERT_PTR(userdata);
588 int r;
589
590 r = lldp_tx_send(lldp_tx);
591 if (r < 0)
592 log_lldp_tx_errno(lldp_tx, r, "Failed to send packet, ignoring: %m");
593
594 if (lldp_tx->fast_tx > 0)
595 lldp_tx->fast_tx--;
596
597 r = lldp_tx_reset_timer(lldp_tx);
598 if (r < 0)
599 log_lldp_tx_errno(lldp_tx, r, "Failed to reset timer: %m");
600
601 return 0;
602 }
603
604 int sd_lldp_tx_is_running(sd_lldp_tx *lldp_tx) {
605 int enabled;
606
607 if (!lldp_tx)
608 return 0;
609
610 if (!lldp_tx->timer_event_source)
611 return 0;
612
613 if (sd_event_source_get_enabled(lldp_tx->timer_event_source, &enabled) < 0)
614 return 0;
615
616 return enabled == SD_EVENT_ONESHOT;
617 }
618
619 int sd_lldp_tx_stop(sd_lldp_tx *lldp_tx) {
620 if (!lldp_tx)
621 return 0;
622
623 if (!lldp_tx->timer_event_source)
624 return 0;
625
626 (void) sd_event_source_set_enabled(lldp_tx->timer_event_source, SD_EVENT_OFF);
627
628 return 1;
629 }
630
631 int sd_lldp_tx_start(sd_lldp_tx *lldp_tx) {
632 usec_t delay;
633 int r;
634
635 assert_return(lldp_tx, -EINVAL);
636 assert_return(lldp_tx->event, -EINVAL);
637 assert_return(lldp_tx->ifindex > 0, -EINVAL);
638 assert_return(lldp_tx->mode >= 0 && lldp_tx->mode < _SD_LLDP_MULTICAST_MODE_MAX, -EINVAL);
639 assert_return(!ether_addr_is_null(&lldp_tx->hwaddr), -EINVAL);
640
641 if (sd_lldp_tx_is_running(lldp_tx))
642 return 0;
643
644 lldp_tx->fast_tx = LLDP_FAST_TX_INIT;
645
646 if (lldp_tx->timer_event_source) {
647 r = lldp_tx_reset_timer(lldp_tx);
648 if (r < 0)
649 return log_lldp_tx_errno(lldp_tx, r, "Failed to re-enable timer: %m");
650
651 return 0;
652 }
653
654 delay = lldp_tx_get_delay(lldp_tx);
655
656 r = sd_event_add_time_relative(lldp_tx->event, &lldp_tx->timer_event_source,
657 CLOCK_BOOTTIME, delay, 0,
658 on_timer_event, lldp_tx);
659 if (r < 0)
660 return r;
661
662 (void) sd_event_source_set_description(lldp_tx->timer_event_source, "lldp-tx-timer");
663 (void) sd_event_source_set_priority(lldp_tx->timer_event_source, lldp_tx->event_priority);
664
665 return 0;
666 }
667
668 int sd_lldp_tx_describe(sd_lldp_tx *lldp_tx, sd_json_variant **ret) {
669 int r;
670
671 assert(lldp_tx);
672 assert(ret);
673
674 /* If ifname is not set yet, set ifname from ifindex. */
675 const char *ifname;
676 r = sd_lldp_tx_get_ifname(lldp_tx, &ifname);
677 if (r < 0)
678 return r;
679
680 size_t port_id_len = strlen(ifname) + 1; /* +1 for subtype */
681 _cleanup_free_ uint8_t *port_id = new(uint8_t, port_id_len + 1); /* +1 for unused zero suffix for safety */
682 if (!port_id)
683 return -ENOMEM;
684
685 port_id[0] = SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME;
686 memcpy(port_id + 1, ifname, port_id_len); /* also copy the final NUL for safety */
687 assert(port_id[port_id_len] == 0);
688
689 sd_id128_t machine_id;
690 r = lldp_tx_get_machine_id(&machine_id);
691 if (r < 0)
692 return r;
693
694 size_t chassis_id_len = (SD_ID128_STRING_MAX - 1) + 1;
695 _cleanup_free_ uint8_t *chassis_id = new(uint8_t, chassis_id_len + 1);
696 if (!chassis_id)
697 return -ENOMEM;
698
699 chassis_id[0] = SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED;
700 memcpy(chassis_id + 1, SD_ID128_TO_STRING(machine_id), chassis_id_len);
701 assert(chassis_id[chassis_id_len] == 0);
702
703 _cleanup_free_ char *hostname = NULL;
704 if (!lldp_tx->hostname)
705 (void) gethostname_strict(&hostname);
706
707 _cleanup_free_ char *pretty_hostname = NULL;
708 if (!lldp_tx->pretty_hostname)
709 (void) get_pretty_hostname(&pretty_hostname);
710
711 return sd_json_buildo(
712 ret,
713 SD_JSON_BUILD_PAIR_ID128("ChassisID", machine_id),
714 SD_JSON_BUILD_PAIR_BYTE_ARRAY("RawChassisID", chassis_id, chassis_id_len),
715 SD_JSON_BUILD_PAIR_STRING("PortID", lldp_tx->ifname),
716 SD_JSON_BUILD_PAIR_BYTE_ARRAY("RawPortID", port_id, port_id_len),
717 JSON_BUILD_PAIR_STRING_NON_EMPTY("PortDescription", lldp_tx->port_description),
718 JSON_BUILD_PAIR_STRING_NON_EMPTY("SystemName", lldp_tx->hostname ?: hostname),
719 JSON_BUILD_PAIR_STRING_NON_EMPTY("SystemDescription", lldp_tx->pretty_hostname ?: pretty_hostname),
720 SD_JSON_BUILD_PAIR_UNSIGNED("EnabledCapabilities", lldp_tx->enabled_capabilities),
721 JSON_BUILD_PAIR_STRING_NON_EMPTY("MUDURL", lldp_tx->mud_url),
722 JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("VlanID", lldp_tx->vlan_id));
723 }