1 /* SPDX-License-Identifier: LGPL-2.1+ */
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"
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
);
53 free(n
->chassis_id_as_string
);
54 free(n
->port_id_as_string
);
58 _public_ sd_lldp_neighbor
*sd_lldp_neighbor_unref(sd_lldp_neighbor
*n
) {
60 /* Drops one reference from the neighbor. Note that the object is not freed unless it is already unlinked from
61 * the sd_lldp object. */
69 if (n
->n_ref
<= 0 && !n
->lldp
)
70 lldp_neighbor_free(n
);
75 sd_lldp_neighbor
*lldp_neighbor_unlink(sd_lldp_neighbor
*n
) {
77 /* Removes the neighbor object from the LLDP object, and frees it if it also has no other reference. */
85 /* Only remove the neighbor object from the hash table if it's in there, don't complain if it isn't. This is
86 * because we are used as destructor call for hashmap_clear() and thus sometimes are called to de-register
87 * ourselves from the hashtable and sometimes are called after we already are de-registered. */
89 (void) hashmap_remove_value(n
->lldp
->neighbor_by_id
, &n
->id
, n
);
91 assert_se(prioq_remove(n
->lldp
->neighbor_by_expiry
, n
, &n
->prioq_idx
) >= 0);
96 lldp_neighbor_free(n
);
101 sd_lldp_neighbor
*lldp_neighbor_new(size_t raw_size
) {
104 n
= malloc0(ALIGN(sizeof(sd_lldp_neighbor
)) + raw_size
);
108 n
->raw_size
= raw_size
;
114 static int parse_string(char **s
, const void *q
, size_t n
) {
122 log_lldp("Found duplicate string, ignoring field.");
126 /* Strip trailing NULs, just to be nice */
127 while (n
> 0 && p
[n
-1] == 0)
130 if (n
<= 0) /* Ignore empty strings */
133 /* Look for inner NULs */
134 if (memchr(p
, 0, n
)) {
135 log_lldp("Found inner NUL in string, ignoring field.");
139 /* Let's escape weird chars, for security reasons */
140 k
= cescape_length(p
, n
);
150 int lldp_neighbor_parse(sd_lldp_neighbor
*n
) {
151 struct ether_header h
;
158 if (n
->raw_size
< sizeof(struct ether_header
)) {
159 log_lldp("Received truncated packet, ignoring.");
163 memcpy(&h
, LLDP_NEIGHBOR_RAW(n
), sizeof(h
));
165 if (h
.ether_type
!= htobe16(ETHERTYPE_LLDP
)) {
166 log_lldp("Received packet with wrong type, ignoring.");
170 if (h
.ether_dhost
[0] != 0x01 ||
171 h
.ether_dhost
[1] != 0x80 ||
172 h
.ether_dhost
[2] != 0xc2 ||
173 h
.ether_dhost
[3] != 0x00 ||
174 h
.ether_dhost
[4] != 0x00 ||
175 !IN_SET(h
.ether_dhost
[5], 0x00, 0x03, 0x0e)) {
176 log_lldp("Received packet with wrong destination address, ignoring.");
180 memcpy(&n
->source_address
, h
.ether_shost
, sizeof(struct ether_addr
));
181 memcpy(&n
->destination_address
, h
.ether_dhost
, sizeof(struct ether_addr
));
183 p
= (const uint8_t*) LLDP_NEIGHBOR_RAW(n
) + sizeof(struct ether_header
);
184 left
= n
->raw_size
- sizeof(struct ether_header
);
191 log_lldp("TLV lacks header, ignoring.");
196 length
= p
[1] + (((uint16_t) (p
[0] & 1)) << 8);
200 log_lldp("TLV truncated, ignoring datagram.");
206 case SD_LLDP_TYPE_END
:
208 log_lldp("End marker TLV not zero-sized, ignoring datagram.");
212 /* Note that after processing the SD_LLDP_TYPE_END left could still be > 0
213 * as the message may contain padding (see IEEE 802.1AB-2016, sec. 8.5.12) */
217 case SD_LLDP_TYPE_CHASSIS_ID
:
218 if (length
< 2 || length
> 256) { /* includes the chassis subtype, hence one extra byte */
219 log_lldp("Chassis ID field size out of range, ignoring datagram.");
222 if (n
->id
.chassis_id
) {
223 log_lldp("Duplicate chassis ID field, ignoring datagram.");
227 n
->id
.chassis_id
= memdup(p
, length
);
228 if (!n
->id
.chassis_id
)
231 n
->id
.chassis_id_size
= length
;
234 case SD_LLDP_TYPE_PORT_ID
:
235 if (length
< 2 || length
> 256) { /* includes the port subtype, hence one extra byte */
236 log_lldp("Port ID field size out of range, ignoring datagram.");
240 log_lldp("Duplicate port ID field, ignoring datagram.");
244 n
->id
.port_id
= memdup(p
, length
);
248 n
->id
.port_id_size
= length
;
251 case SD_LLDP_TYPE_TTL
:
253 log_lldp("TTL field has wrong size, ignoring datagram.");
258 log_lldp("Duplicate TTL field, ignoring datagram.");
262 n
->ttl
= unaligned_read_be16(p
);
266 case SD_LLDP_TYPE_PORT_DESCRIPTION
:
267 r
= parse_string(&n
->port_description
, p
, length
);
272 case SD_LLDP_TYPE_SYSTEM_NAME
:
273 r
= parse_string(&n
->system_name
, p
, length
);
278 case SD_LLDP_TYPE_SYSTEM_DESCRIPTION
:
279 r
= parse_string(&n
->system_description
, p
, length
);
284 case SD_LLDP_TYPE_SYSTEM_CAPABILITIES
:
286 log_lldp("System capabilities field has wrong size, ignoring.");
288 n
->system_capabilities
= unaligned_read_be16(p
);
289 n
->enabled_capabilities
= unaligned_read_be16(p
+ 2);
290 n
->has_capabilities
= true;
295 case SD_LLDP_TYPE_PRIVATE
:
297 log_lldp("Found private TLV that is too short, ignoring.");
302 p
+= length
, left
-= length
;
306 if (!n
->id
.chassis_id
|| !n
->id
.port_id
|| !n
->has_ttl
) {
307 log_lldp("One or more mandatory TLV missing in datagram. Ignoring.");
312 n
->rindex
= sizeof(struct ether_header
);
317 void lldp_neighbor_start_ttl(sd_lldp_neighbor
*n
) {
323 /* Use the packet's timestamp if there is one known */
324 base
= triple_timestamp_by_clock(&n
->timestamp
, clock_boottime_or_monotonic());
325 if (base
<= 0 || base
== USEC_INFINITY
)
326 base
= now(clock_boottime_or_monotonic()); /* Otherwise, take the current time */
328 n
->until
= usec_add(base
, n
->ttl
* USEC_PER_SEC
);
333 prioq_reshuffle(n
->lldp
->neighbor_by_expiry
, n
, &n
->prioq_idx
);
336 bool lldp_neighbor_equal(const sd_lldp_neighbor
*a
, const sd_lldp_neighbor
*b
) {
343 if (a
->raw_size
!= b
->raw_size
)
346 return memcmp(LLDP_NEIGHBOR_RAW(a
), LLDP_NEIGHBOR_RAW(b
), a
->raw_size
) == 0;
349 _public_
int sd_lldp_neighbor_get_source_address(sd_lldp_neighbor
*n
, struct ether_addr
* address
) {
350 assert_return(n
, -EINVAL
);
351 assert_return(address
, -EINVAL
);
353 *address
= n
->source_address
;
357 _public_
int sd_lldp_neighbor_get_destination_address(sd_lldp_neighbor
*n
, struct ether_addr
* address
) {
358 assert_return(n
, -EINVAL
);
359 assert_return(address
, -EINVAL
);
361 *address
= n
->destination_address
;
365 _public_
int sd_lldp_neighbor_get_raw(sd_lldp_neighbor
*n
, const void **ret
, size_t *size
) {
366 assert_return(n
, -EINVAL
);
367 assert_return(ret
, -EINVAL
);
368 assert_return(size
, -EINVAL
);
370 *ret
= LLDP_NEIGHBOR_RAW(n
);
376 _public_
int sd_lldp_neighbor_get_chassis_id(sd_lldp_neighbor
*n
, uint8_t *type
, const void **ret
, size_t *size
) {
377 assert_return(n
, -EINVAL
);
378 assert_return(type
, -EINVAL
);
379 assert_return(ret
, -EINVAL
);
380 assert_return(size
, -EINVAL
);
382 assert(n
->id
.chassis_id_size
> 0);
384 *type
= *(uint8_t*) n
->id
.chassis_id
;
385 *ret
= (uint8_t*) n
->id
.chassis_id
+ 1;
386 *size
= n
->id
.chassis_id_size
- 1;
391 static int format_mac_address(const void *data
, size_t sz
, char **ret
) {
395 assert(data
|| sz
<= 0);
400 memcpy(&a
, (uint8_t*) data
+ 1, sizeof(a
));
402 k
= new(char, ETHER_ADDR_TO_STRING_MAX
);
406 *ret
= ether_addr_to_string(&a
, k
);
410 static int format_network_address(const void *data
, size_t sz
, char **ret
) {
411 union in_addr_union a
;
414 if (sz
== 6 && ((uint8_t*) data
)[1] == 1) {
415 memcpy(&a
.in
, (uint8_t*) data
+ 2, sizeof(a
.in
));
417 } else if (sz
== 18 && ((uint8_t*) data
)[1] == 2) {
418 memcpy(&a
.in6
, (uint8_t*) data
+ 2, sizeof(a
.in6
));
423 r
= in_addr_to_string(family
, &a
, ret
);
429 _public_
int sd_lldp_neighbor_get_chassis_id_as_string(sd_lldp_neighbor
*n
, const char **ret
) {
433 assert_return(n
, -EINVAL
);
434 assert_return(ret
, -EINVAL
);
436 if (n
->chassis_id_as_string
) {
437 *ret
= n
->chassis_id_as_string
;
441 assert(n
->id
.chassis_id_size
> 0);
443 switch (*(uint8_t*) n
->id
.chassis_id
) {
445 case SD_LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT
:
446 case SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_ALIAS
:
447 case SD_LLDP_CHASSIS_SUBTYPE_PORT_COMPONENT
:
448 case SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME
:
449 case SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED
:
450 k
= cescape_length((char*) n
->id
.chassis_id
+ 1, n
->id
.chassis_id_size
- 1);
456 case SD_LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS
:
457 r
= format_mac_address(n
->id
.chassis_id
, n
->id
.chassis_id_size
, &k
);
465 case SD_LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS
:
466 r
= format_network_address(n
->id
.chassis_id
, n
->id
.chassis_id_size
, &k
);
475 /* Generic fallback */
476 k
= hexmem(n
->id
.chassis_id
, n
->id
.chassis_id_size
);
481 *ret
= n
->chassis_id_as_string
= k
;
485 _public_
int sd_lldp_neighbor_get_port_id(sd_lldp_neighbor
*n
, uint8_t *type
, const void **ret
, size_t *size
) {
486 assert_return(n
, -EINVAL
);
487 assert_return(type
, -EINVAL
);
488 assert_return(ret
, -EINVAL
);
489 assert_return(size
, -EINVAL
);
491 assert(n
->id
.port_id_size
> 0);
493 *type
= *(uint8_t*) n
->id
.port_id
;
494 *ret
= (uint8_t*) n
->id
.port_id
+ 1;
495 *size
= n
->id
.port_id_size
- 1;
500 _public_
int sd_lldp_neighbor_get_port_id_as_string(sd_lldp_neighbor
*n
, const char **ret
) {
504 assert_return(n
, -EINVAL
);
505 assert_return(ret
, -EINVAL
);
507 if (n
->port_id_as_string
) {
508 *ret
= n
->port_id_as_string
;
512 assert(n
->id
.port_id_size
> 0);
514 switch (*(uint8_t*) n
->id
.port_id
) {
516 case SD_LLDP_PORT_SUBTYPE_INTERFACE_ALIAS
:
517 case SD_LLDP_PORT_SUBTYPE_PORT_COMPONENT
:
518 case SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME
:
519 case SD_LLDP_PORT_SUBTYPE_LOCALLY_ASSIGNED
:
520 k
= cescape_length((char*) n
->id
.port_id
+ 1, n
->id
.port_id_size
- 1);
526 case SD_LLDP_PORT_SUBTYPE_MAC_ADDRESS
:
527 r
= format_mac_address(n
->id
.port_id
, n
->id
.port_id_size
, &k
);
535 case SD_LLDP_PORT_SUBTYPE_NETWORK_ADDRESS
:
536 r
= format_network_address(n
->id
.port_id
, n
->id
.port_id_size
, &k
);
545 /* Generic fallback */
546 k
= hexmem(n
->id
.port_id
, n
->id
.port_id_size
);
551 *ret
= n
->port_id_as_string
= k
;
555 _public_
int sd_lldp_neighbor_get_ttl(sd_lldp_neighbor
*n
, uint16_t *ret_sec
) {
556 assert_return(n
, -EINVAL
);
557 assert_return(ret_sec
, -EINVAL
);
563 _public_
int sd_lldp_neighbor_get_system_name(sd_lldp_neighbor
*n
, const char **ret
) {
564 assert_return(n
, -EINVAL
);
565 assert_return(ret
, -EINVAL
);
570 *ret
= n
->system_name
;
574 _public_
int sd_lldp_neighbor_get_system_description(sd_lldp_neighbor
*n
, const char **ret
) {
575 assert_return(n
, -EINVAL
);
576 assert_return(ret
, -EINVAL
);
578 if (!n
->system_description
)
581 *ret
= n
->system_description
;
585 _public_
int sd_lldp_neighbor_get_port_description(sd_lldp_neighbor
*n
, const char **ret
) {
586 assert_return(n
, -EINVAL
);
587 assert_return(ret
, -EINVAL
);
589 if (!n
->port_description
)
592 *ret
= n
->port_description
;
596 _public_
int sd_lldp_neighbor_get_system_capabilities(sd_lldp_neighbor
*n
, uint16_t *ret
) {
597 assert_return(n
, -EINVAL
);
598 assert_return(ret
, -EINVAL
);
600 if (!n
->has_capabilities
)
603 *ret
= n
->system_capabilities
;
607 _public_
int sd_lldp_neighbor_get_enabled_capabilities(sd_lldp_neighbor
*n
, uint16_t *ret
) {
608 assert_return(n
, -EINVAL
);
609 assert_return(ret
, -EINVAL
);
611 if (!n
->has_capabilities
)
614 *ret
= n
->enabled_capabilities
;
618 _public_
int sd_lldp_neighbor_from_raw(sd_lldp_neighbor
**ret
, const void *raw
, size_t raw_size
) {
619 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
622 assert_return(ret
, -EINVAL
);
623 assert_return(raw
|| raw_size
<= 0, -EINVAL
);
625 n
= lldp_neighbor_new(raw_size
);
629 memcpy(LLDP_NEIGHBOR_RAW(n
), raw
, raw_size
);
630 r
= lldp_neighbor_parse(n
);
639 _public_
int sd_lldp_neighbor_tlv_rewind(sd_lldp_neighbor
*n
) {
640 assert_return(n
, -EINVAL
);
642 assert(n
->raw_size
>= sizeof(struct ether_header
));
643 n
->rindex
= sizeof(struct ether_header
);
645 return n
->rindex
< n
->raw_size
;
648 _public_
int sd_lldp_neighbor_tlv_next(sd_lldp_neighbor
*n
) {
651 assert_return(n
, -EINVAL
);
653 if (n
->rindex
== n
->raw_size
) /* EOF */
656 if (n
->rindex
+ 2 > n
->raw_size
) /* Truncated message */
659 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
660 if (n
->rindex
+ 2 + length
> n
->raw_size
)
663 n
->rindex
+= 2 + length
;
664 return n
->rindex
< n
->raw_size
;
667 _public_
int sd_lldp_neighbor_tlv_get_type(sd_lldp_neighbor
*n
, uint8_t *type
) {
668 assert_return(n
, -EINVAL
);
669 assert_return(type
, -EINVAL
);
671 if (n
->rindex
== n
->raw_size
) /* EOF */
674 if (n
->rindex
+ 2 > n
->raw_size
)
677 *type
= LLDP_NEIGHBOR_TLV_TYPE(n
);
681 _public_
int sd_lldp_neighbor_tlv_is_type(sd_lldp_neighbor
*n
, uint8_t type
) {
685 assert_return(n
, -EINVAL
);
687 r
= sd_lldp_neighbor_tlv_get_type(n
, &k
);
694 _public_
int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor
*n
, uint8_t oui
[_SD_ARRAY_STATIC
3], uint8_t *subtype
) {
699 assert_return(n
, -EINVAL
);
700 assert_return(oui
, -EINVAL
);
701 assert_return(subtype
, -EINVAL
);
703 r
= sd_lldp_neighbor_tlv_is_type(n
, SD_LLDP_TYPE_PRIVATE
);
709 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
713 if (n
->rindex
+ 2 + length
> n
->raw_size
)
716 d
= LLDP_NEIGHBOR_TLV_DATA(n
);
723 _public_
int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor
*n
, const uint8_t oui
[_SD_ARRAY_STATIC
3], uint8_t subtype
) {
727 r
= sd_lldp_neighbor_tlv_get_oui(n
, k
, &st
);
733 return memcmp(k
, oui
, 3) == 0 && st
== subtype
;
736 _public_
int sd_lldp_neighbor_tlv_get_raw(sd_lldp_neighbor
*n
, const void **ret
, size_t *size
) {
739 assert_return(n
, -EINVAL
);
740 assert_return(ret
, -EINVAL
);
741 assert_return(size
, -EINVAL
);
743 /* Note that this returns the full TLV, including the TLV header */
745 if (n
->rindex
+ 2 > n
->raw_size
)
748 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
749 if (n
->rindex
+ 2 + length
> n
->raw_size
)
752 *ret
= (uint8_t*) LLDP_NEIGHBOR_RAW(n
) + n
->rindex
;
758 _public_
int sd_lldp_neighbor_get_timestamp(sd_lldp_neighbor
*n
, clockid_t clock
, uint64_t *ret
) {
759 assert_return(n
, -EINVAL
);
760 assert_return(TRIPLE_TIMESTAMP_HAS_CLOCK(clock
), -EOPNOTSUPP
);
761 assert_return(clock_supported(clock
), -EOPNOTSUPP
);
762 assert_return(ret
, -EINVAL
);
764 if (!triple_timestamp_is_set(&n
->timestamp
))
767 *ret
= triple_timestamp_by_clock(&n
->timestamp
, clock
);