1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
5 #include "ether-addr-util.h"
7 #include "in-addr-util.h"
8 #include "lldp-internal.h"
9 #include "lldp-neighbor.h"
10 #include "memory-util.h"
11 #include "missing_network.h"
12 #include "unaligned.h"
14 static void lldp_neighbor_id_hash_func(const LLDPNeighborID
*id
, struct siphash
*state
) {
15 siphash24_compress(id
->chassis_id
, id
->chassis_id_size
, state
);
16 siphash24_compress(&id
->chassis_id_size
, sizeof(id
->chassis_id_size
), state
);
17 siphash24_compress(id
->port_id
, id
->port_id_size
, state
);
18 siphash24_compress(&id
->port_id_size
, sizeof(id
->port_id_size
), state
);
21 int lldp_neighbor_id_compare_func(const LLDPNeighborID
*x
, const LLDPNeighborID
*y
) {
22 return memcmp_nn(x
->chassis_id
, x
->chassis_id_size
, y
->chassis_id
, y
->chassis_id_size
)
23 ?: memcmp_nn(x
->port_id
, x
->port_id_size
, y
->port_id
, y
->port_id_size
);
26 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(lldp_neighbor_hash_ops
, LLDPNeighborID
, lldp_neighbor_id_hash_func
, lldp_neighbor_id_compare_func
,
27 sd_lldp_neighbor
, lldp_neighbor_unlink
);
29 int lldp_neighbor_prioq_compare_func(const void *a
, const void *b
) {
30 const sd_lldp_neighbor
*x
= a
, *y
= b
;
32 return CMP(x
->until
, y
->until
);
35 _public_ sd_lldp_neighbor
*sd_lldp_neighbor_ref(sd_lldp_neighbor
*n
) {
39 assert(n
->n_ref
> 0 || n
->lldp
);
45 static void lldp_neighbor_free(sd_lldp_neighbor
*n
) {
49 free(n
->id
.chassis_id
);
50 free(n
->port_description
);
52 free(n
->system_description
);
54 free(n
->chassis_id_as_string
);
55 free(n
->port_id_as_string
);
59 _public_ sd_lldp_neighbor
*sd_lldp_neighbor_unref(sd_lldp_neighbor
*n
) {
61 /* Drops one reference from the neighbor. Note that the object is not freed unless it is already unlinked from
62 * the sd_lldp object. */
70 if (n
->n_ref
<= 0 && !n
->lldp
)
71 lldp_neighbor_free(n
);
76 sd_lldp_neighbor
*lldp_neighbor_unlink(sd_lldp_neighbor
*n
) {
78 /* Removes the neighbor object from the LLDP object, and frees it if it also has no other reference. */
86 /* Only remove the neighbor object from the hash table if it's in there, don't complain if it isn't. This is
87 * because we are used as destructor call for hashmap_clear() and thus sometimes are called to de-register
88 * ourselves from the hashtable and sometimes are called after we already are de-registered. */
90 (void) hashmap_remove_value(n
->lldp
->neighbor_by_id
, &n
->id
, n
);
92 assert_se(prioq_remove(n
->lldp
->neighbor_by_expiry
, n
, &n
->prioq_idx
) >= 0);
97 lldp_neighbor_free(n
);
102 sd_lldp_neighbor
*lldp_neighbor_new(size_t raw_size
) {
105 n
= malloc0(ALIGN(sizeof(sd_lldp_neighbor
)) + raw_size
);
109 n
->raw_size
= raw_size
;
115 static int parse_string(char **s
, const void *q
, size_t n
) {
123 log_lldp("Found duplicate string, ignoring field.");
127 /* Strip trailing NULs, just to be nice */
128 while (n
> 0 && p
[n
-1] == 0)
131 if (n
<= 0) /* Ignore empty strings */
134 /* Look for inner NULs */
135 if (memchr(p
, 0, n
)) {
136 log_lldp("Found inner NUL in string, ignoring field.");
140 /* Let's escape weird chars, for security reasons */
141 k
= cescape_length(p
, n
);
151 int lldp_neighbor_parse(sd_lldp_neighbor
*n
) {
152 struct ether_header h
;
159 if (n
->raw_size
< sizeof(struct ether_header
)) {
160 log_lldp("Received truncated packet, ignoring.");
164 memcpy(&h
, LLDP_NEIGHBOR_RAW(n
), sizeof(h
));
166 if (h
.ether_type
!= htobe16(ETHERTYPE_LLDP
)) {
167 log_lldp("Received packet with wrong type, ignoring.");
171 if (h
.ether_dhost
[0] != 0x01 ||
172 h
.ether_dhost
[1] != 0x80 ||
173 h
.ether_dhost
[2] != 0xc2 ||
174 h
.ether_dhost
[3] != 0x00 ||
175 h
.ether_dhost
[4] != 0x00 ||
176 !IN_SET(h
.ether_dhost
[5], 0x00, 0x03, 0x0e)) {
177 log_lldp("Received packet with wrong destination address, ignoring.");
181 memcpy(&n
->source_address
, h
.ether_shost
, sizeof(struct ether_addr
));
182 memcpy(&n
->destination_address
, h
.ether_dhost
, sizeof(struct ether_addr
));
184 p
= (const uint8_t*) LLDP_NEIGHBOR_RAW(n
) + sizeof(struct ether_header
);
185 left
= n
->raw_size
- sizeof(struct ether_header
);
192 log_lldp("TLV lacks header, ignoring.");
197 length
= p
[1] + (((uint16_t) (p
[0] & 1)) << 8);
201 log_lldp("TLV truncated, ignoring datagram.");
207 case SD_LLDP_TYPE_END
:
209 log_lldp("End marker TLV not zero-sized, ignoring datagram.");
213 /* Note that after processing the SD_LLDP_TYPE_END left could still be > 0
214 * as the message may contain padding (see IEEE 802.1AB-2016, sec. 8.5.12) */
218 case SD_LLDP_TYPE_CHASSIS_ID
:
219 if (length
< 2 || length
> 256) { /* includes the chassis subtype, hence one extra byte */
220 log_lldp("Chassis ID field size out of range, ignoring datagram.");
223 if (n
->id
.chassis_id
) {
224 log_lldp("Duplicate chassis ID field, ignoring datagram.");
228 n
->id
.chassis_id
= memdup(p
, length
);
229 if (!n
->id
.chassis_id
)
232 n
->id
.chassis_id_size
= length
;
235 case SD_LLDP_TYPE_PORT_ID
:
236 if (length
< 2 || length
> 256) { /* includes the port subtype, hence one extra byte */
237 log_lldp("Port ID field size out of range, ignoring datagram.");
241 log_lldp("Duplicate port ID field, ignoring datagram.");
245 n
->id
.port_id
= memdup(p
, length
);
249 n
->id
.port_id_size
= length
;
252 case SD_LLDP_TYPE_TTL
:
254 log_lldp("TTL field has wrong size, ignoring datagram.");
259 log_lldp("Duplicate TTL field, ignoring datagram.");
263 n
->ttl
= unaligned_read_be16(p
);
267 case SD_LLDP_TYPE_PORT_DESCRIPTION
:
268 r
= parse_string(&n
->port_description
, p
, length
);
273 case SD_LLDP_TYPE_SYSTEM_NAME
:
274 r
= parse_string(&n
->system_name
, p
, length
);
279 case SD_LLDP_TYPE_SYSTEM_DESCRIPTION
:
280 r
= parse_string(&n
->system_description
, p
, length
);
285 case SD_LLDP_TYPE_SYSTEM_CAPABILITIES
:
287 log_lldp("System capabilities field has wrong size, ignoring.");
289 n
->system_capabilities
= unaligned_read_be16(p
);
290 n
->enabled_capabilities
= unaligned_read_be16(p
+ 2);
291 n
->has_capabilities
= true;
296 case SD_LLDP_TYPE_PRIVATE
: {
298 log_lldp("Found private TLV that is too short, ignoring.");
300 /* RFC 8520: MUD URL */
301 if (memcmp(p
, SD_LLDP_OUI_MUD
, sizeof(SD_LLDP_OUI_MUD
)) == 0 &&
302 p
[sizeof(SD_LLDP_OUI_MUD
)] == SD_LLDP_OUI_SUBTYPE_MUD_USAGE_DESCRIPTION
) {
303 r
= parse_string(&n
->mud_url
, p
+ sizeof(SD_LLDP_OUI_MUD
) + 1,
304 length
- 1 - sizeof(SD_LLDP_OUI_MUD
));
314 p
+= length
, left
-= length
;
318 if (!n
->id
.chassis_id
|| !n
->id
.port_id
|| !n
->has_ttl
) {
319 log_lldp("One or more mandatory TLV missing in datagram. Ignoring.");
324 n
->rindex
= sizeof(struct ether_header
);
329 void lldp_neighbor_start_ttl(sd_lldp_neighbor
*n
) {
335 /* Use the packet's timestamp if there is one known */
336 base
= triple_timestamp_by_clock(&n
->timestamp
, clock_boottime_or_monotonic());
337 if (base
<= 0 || base
== USEC_INFINITY
)
338 base
= now(clock_boottime_or_monotonic()); /* Otherwise, take the current time */
340 n
->until
= usec_add(base
, n
->ttl
* USEC_PER_SEC
);
345 prioq_reshuffle(n
->lldp
->neighbor_by_expiry
, n
, &n
->prioq_idx
);
348 bool lldp_neighbor_equal(const sd_lldp_neighbor
*a
, const sd_lldp_neighbor
*b
) {
355 if (a
->raw_size
!= b
->raw_size
)
358 return memcmp(LLDP_NEIGHBOR_RAW(a
), LLDP_NEIGHBOR_RAW(b
), a
->raw_size
) == 0;
361 _public_
int sd_lldp_neighbor_get_source_address(sd_lldp_neighbor
*n
, struct ether_addr
* address
) {
362 assert_return(n
, -EINVAL
);
363 assert_return(address
, -EINVAL
);
365 *address
= n
->source_address
;
369 _public_
int sd_lldp_neighbor_get_destination_address(sd_lldp_neighbor
*n
, struct ether_addr
* address
) {
370 assert_return(n
, -EINVAL
);
371 assert_return(address
, -EINVAL
);
373 *address
= n
->destination_address
;
377 _public_
int sd_lldp_neighbor_get_raw(sd_lldp_neighbor
*n
, const void **ret
, size_t *size
) {
378 assert_return(n
, -EINVAL
);
379 assert_return(ret
, -EINVAL
);
380 assert_return(size
, -EINVAL
);
382 *ret
= LLDP_NEIGHBOR_RAW(n
);
388 _public_
int sd_lldp_neighbor_get_chassis_id(sd_lldp_neighbor
*n
, uint8_t *type
, const void **ret
, size_t *size
) {
389 assert_return(n
, -EINVAL
);
390 assert_return(type
, -EINVAL
);
391 assert_return(ret
, -EINVAL
);
392 assert_return(size
, -EINVAL
);
394 assert(n
->id
.chassis_id_size
> 0);
396 *type
= *(uint8_t*) n
->id
.chassis_id
;
397 *ret
= (uint8_t*) n
->id
.chassis_id
+ 1;
398 *size
= n
->id
.chassis_id_size
- 1;
403 static int format_mac_address(const void *data
, size_t sz
, char **ret
) {
407 assert(data
|| sz
<= 0);
412 memcpy(&a
, (uint8_t*) data
+ 1, sizeof(a
));
414 k
= new(char, ETHER_ADDR_TO_STRING_MAX
);
418 *ret
= ether_addr_to_string(&a
, k
);
422 static int format_network_address(const void *data
, size_t sz
, char **ret
) {
423 union in_addr_union a
;
426 if (sz
== 6 && ((uint8_t*) data
)[1] == 1) {
427 memcpy(&a
.in
, (uint8_t*) data
+ 2, sizeof(a
.in
));
429 } else if (sz
== 18 && ((uint8_t*) data
)[1] == 2) {
430 memcpy(&a
.in6
, (uint8_t*) data
+ 2, sizeof(a
.in6
));
435 r
= in_addr_to_string(family
, &a
, ret
);
441 _public_
int sd_lldp_neighbor_get_chassis_id_as_string(sd_lldp_neighbor
*n
, const char **ret
) {
445 assert_return(n
, -EINVAL
);
446 assert_return(ret
, -EINVAL
);
448 if (n
->chassis_id_as_string
) {
449 *ret
= n
->chassis_id_as_string
;
453 assert(n
->id
.chassis_id_size
> 0);
455 switch (*(uint8_t*) n
->id
.chassis_id
) {
457 case SD_LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT
:
458 case SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_ALIAS
:
459 case SD_LLDP_CHASSIS_SUBTYPE_PORT_COMPONENT
:
460 case SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME
:
461 case SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED
:
462 k
= cescape_length((char*) n
->id
.chassis_id
+ 1, n
->id
.chassis_id_size
- 1);
468 case SD_LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS
:
469 r
= format_mac_address(n
->id
.chassis_id
, n
->id
.chassis_id_size
, &k
);
477 case SD_LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS
:
478 r
= format_network_address(n
->id
.chassis_id
, n
->id
.chassis_id_size
, &k
);
487 /* Generic fallback */
488 k
= hexmem(n
->id
.chassis_id
, n
->id
.chassis_id_size
);
493 *ret
= n
->chassis_id_as_string
= k
;
497 _public_
int sd_lldp_neighbor_get_port_id(sd_lldp_neighbor
*n
, uint8_t *type
, const void **ret
, size_t *size
) {
498 assert_return(n
, -EINVAL
);
499 assert_return(type
, -EINVAL
);
500 assert_return(ret
, -EINVAL
);
501 assert_return(size
, -EINVAL
);
503 assert(n
->id
.port_id_size
> 0);
505 *type
= *(uint8_t*) n
->id
.port_id
;
506 *ret
= (uint8_t*) n
->id
.port_id
+ 1;
507 *size
= n
->id
.port_id_size
- 1;
512 _public_
int sd_lldp_neighbor_get_port_id_as_string(sd_lldp_neighbor
*n
, const char **ret
) {
516 assert_return(n
, -EINVAL
);
517 assert_return(ret
, -EINVAL
);
519 if (n
->port_id_as_string
) {
520 *ret
= n
->port_id_as_string
;
524 assert(n
->id
.port_id_size
> 0);
526 switch (*(uint8_t*) n
->id
.port_id
) {
528 case SD_LLDP_PORT_SUBTYPE_INTERFACE_ALIAS
:
529 case SD_LLDP_PORT_SUBTYPE_PORT_COMPONENT
:
530 case SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME
:
531 case SD_LLDP_PORT_SUBTYPE_LOCALLY_ASSIGNED
:
532 k
= cescape_length((char*) n
->id
.port_id
+ 1, n
->id
.port_id_size
- 1);
538 case SD_LLDP_PORT_SUBTYPE_MAC_ADDRESS
:
539 r
= format_mac_address(n
->id
.port_id
, n
->id
.port_id_size
, &k
);
547 case SD_LLDP_PORT_SUBTYPE_NETWORK_ADDRESS
:
548 r
= format_network_address(n
->id
.port_id
, n
->id
.port_id_size
, &k
);
557 /* Generic fallback */
558 k
= hexmem(n
->id
.port_id
, n
->id
.port_id_size
);
563 *ret
= n
->port_id_as_string
= k
;
567 _public_
int sd_lldp_neighbor_get_ttl(sd_lldp_neighbor
*n
, uint16_t *ret_sec
) {
568 assert_return(n
, -EINVAL
);
569 assert_return(ret_sec
, -EINVAL
);
575 _public_
int sd_lldp_neighbor_get_system_name(sd_lldp_neighbor
*n
, const char **ret
) {
576 assert_return(n
, -EINVAL
);
577 assert_return(ret
, -EINVAL
);
582 *ret
= n
->system_name
;
586 _public_
int sd_lldp_neighbor_get_system_description(sd_lldp_neighbor
*n
, const char **ret
) {
587 assert_return(n
, -EINVAL
);
588 assert_return(ret
, -EINVAL
);
590 if (!n
->system_description
)
593 *ret
= n
->system_description
;
597 _public_
int sd_lldp_neighbor_get_port_description(sd_lldp_neighbor
*n
, const char **ret
) {
598 assert_return(n
, -EINVAL
);
599 assert_return(ret
, -EINVAL
);
601 if (!n
->port_description
)
604 *ret
= n
->port_description
;
608 _public_
int sd_lldp_neighbor_get_mud_url(sd_lldp_neighbor
*n
, const char **ret
) {
609 assert_return(n
, -EINVAL
);
610 assert_return(ret
, -EINVAL
);
619 _public_
int sd_lldp_neighbor_get_system_capabilities(sd_lldp_neighbor
*n
, uint16_t *ret
) {
620 assert_return(n
, -EINVAL
);
621 assert_return(ret
, -EINVAL
);
623 if (!n
->has_capabilities
)
626 *ret
= n
->system_capabilities
;
630 _public_
int sd_lldp_neighbor_get_enabled_capabilities(sd_lldp_neighbor
*n
, uint16_t *ret
) {
631 assert_return(n
, -EINVAL
);
632 assert_return(ret
, -EINVAL
);
634 if (!n
->has_capabilities
)
637 *ret
= n
->enabled_capabilities
;
641 _public_
int sd_lldp_neighbor_from_raw(sd_lldp_neighbor
**ret
, const void *raw
, size_t raw_size
) {
642 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
645 assert_return(ret
, -EINVAL
);
646 assert_return(raw
|| raw_size
<= 0, -EINVAL
);
648 n
= lldp_neighbor_new(raw_size
);
652 memcpy(LLDP_NEIGHBOR_RAW(n
), raw
, raw_size
);
653 r
= lldp_neighbor_parse(n
);
662 _public_
int sd_lldp_neighbor_tlv_rewind(sd_lldp_neighbor
*n
) {
663 assert_return(n
, -EINVAL
);
665 assert(n
->raw_size
>= sizeof(struct ether_header
));
666 n
->rindex
= sizeof(struct ether_header
);
668 return n
->rindex
< n
->raw_size
;
671 _public_
int sd_lldp_neighbor_tlv_next(sd_lldp_neighbor
*n
) {
674 assert_return(n
, -EINVAL
);
676 if (n
->rindex
== n
->raw_size
) /* EOF */
679 if (n
->rindex
+ 2 > n
->raw_size
) /* Truncated message */
682 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
683 if (n
->rindex
+ 2 + length
> n
->raw_size
)
686 n
->rindex
+= 2 + length
;
687 return n
->rindex
< n
->raw_size
;
690 _public_
int sd_lldp_neighbor_tlv_get_type(sd_lldp_neighbor
*n
, uint8_t *type
) {
691 assert_return(n
, -EINVAL
);
692 assert_return(type
, -EINVAL
);
694 if (n
->rindex
== n
->raw_size
) /* EOF */
697 if (n
->rindex
+ 2 > n
->raw_size
)
700 *type
= LLDP_NEIGHBOR_TLV_TYPE(n
);
704 _public_
int sd_lldp_neighbor_tlv_is_type(sd_lldp_neighbor
*n
, uint8_t type
) {
708 assert_return(n
, -EINVAL
);
710 r
= sd_lldp_neighbor_tlv_get_type(n
, &k
);
717 _public_
int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor
*n
, uint8_t oui
[_SD_ARRAY_STATIC
3], uint8_t *subtype
) {
722 assert_return(n
, -EINVAL
);
723 assert_return(oui
, -EINVAL
);
724 assert_return(subtype
, -EINVAL
);
726 r
= sd_lldp_neighbor_tlv_is_type(n
, SD_LLDP_TYPE_PRIVATE
);
732 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
736 if (n
->rindex
+ 2 + length
> n
->raw_size
)
739 d
= LLDP_NEIGHBOR_TLV_DATA(n
);
746 _public_
int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor
*n
, const uint8_t oui
[_SD_ARRAY_STATIC
3], uint8_t subtype
) {
750 r
= sd_lldp_neighbor_tlv_get_oui(n
, k
, &st
);
756 return memcmp(k
, oui
, 3) == 0 && st
== subtype
;
759 _public_
int sd_lldp_neighbor_tlv_get_raw(sd_lldp_neighbor
*n
, const void **ret
, size_t *size
) {
762 assert_return(n
, -EINVAL
);
763 assert_return(ret
, -EINVAL
);
764 assert_return(size
, -EINVAL
);
766 /* Note that this returns the full TLV, including the TLV header */
768 if (n
->rindex
+ 2 > n
->raw_size
)
771 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
772 if (n
->rindex
+ 2 + length
> n
->raw_size
)
775 *ret
= (uint8_t*) LLDP_NEIGHBOR_RAW(n
) + n
->rindex
;
781 _public_
int sd_lldp_neighbor_get_timestamp(sd_lldp_neighbor
*n
, clockid_t clock
, uint64_t *ret
) {
782 assert_return(n
, -EINVAL
);
783 assert_return(TRIPLE_TIMESTAMP_HAS_CLOCK(clock
), -EOPNOTSUPP
);
784 assert_return(clock_supported(clock
), -EOPNOTSUPP
);
785 assert_return(ret
, -EINVAL
);
787 if (!triple_timestamp_is_set(&n
->timestamp
))
790 *ret
= triple_timestamp_by_clock(&n
->timestamp
, clock
);