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 "unaligned.h"
12 static void lldp_neighbor_id_hash_func(const void *p
, struct siphash
*state
) {
13 const LLDPNeighborID
*id
= p
;
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 static int lldp_neighbor_id_compare_func(const void *a
, const void *b
) {
22 const LLDPNeighborID
*x
= a
, *y
= b
;
25 r
= memcmp(x
->chassis_id
, y
->chassis_id
, MIN(x
->chassis_id_size
, y
->chassis_id_size
));
29 if (x
->chassis_id_size
< y
->chassis_id_size
)
32 if (x
->chassis_id_size
> y
->chassis_id_size
)
35 r
= memcmp(x
->port_id
, y
->port_id
, MIN(x
->port_id_size
, y
->port_id_size
));
39 if (x
->port_id_size
< y
->port_id_size
)
41 if (x
->port_id_size
> y
->port_id_size
)
47 const struct hash_ops lldp_neighbor_id_hash_ops
= {
48 .hash
= lldp_neighbor_id_hash_func
,
49 .compare
= lldp_neighbor_id_compare_func
52 int lldp_neighbor_prioq_compare_func(const void *a
, const void *b
) {
53 const sd_lldp_neighbor
*x
= a
, *y
= b
;
55 if (x
->until
< y
->until
)
58 if (x
->until
> y
->until
)
64 _public_ sd_lldp_neighbor
*sd_lldp_neighbor_ref(sd_lldp_neighbor
*n
) {
68 assert(n
->n_ref
> 0 || n
->lldp
);
74 static void lldp_neighbor_free(sd_lldp_neighbor
*n
) {
78 free(n
->id
.chassis_id
);
79 free(n
->port_description
);
81 free(n
->system_description
);
82 free(n
->chassis_id_as_string
);
83 free(n
->port_id_as_string
);
87 _public_ sd_lldp_neighbor
*sd_lldp_neighbor_unref(sd_lldp_neighbor
*n
) {
89 /* Drops one reference from the neighbor. Note that the object is not freed unless it is already unlinked from
90 * the sd_lldp object. */
98 if (n
->n_ref
<= 0 && !n
->lldp
)
99 lldp_neighbor_free(n
);
104 sd_lldp_neighbor
*lldp_neighbor_unlink(sd_lldp_neighbor
*n
) {
106 /* Removes the neighbor object from the LLDP object, and frees it if it also has no other reference. */
114 assert_se(hashmap_remove(n
->lldp
->neighbor_by_id
, &n
->id
) == n
);
115 assert_se(prioq_remove(n
->lldp
->neighbor_by_expiry
, n
, &n
->prioq_idx
) >= 0);
120 lldp_neighbor_free(n
);
125 sd_lldp_neighbor
*lldp_neighbor_new(size_t raw_size
) {
128 n
= malloc0(ALIGN(sizeof(sd_lldp_neighbor
)) + raw_size
);
132 n
->raw_size
= raw_size
;
138 static int parse_string(char **s
, const void *q
, size_t n
) {
146 log_lldp("Found duplicate string, ignoring field.");
150 /* Strip trailing NULs, just to be nice */
151 while (n
> 0 && p
[n
-1] == 0)
154 if (n
<= 0) /* Ignore empty strings */
157 /* Look for inner NULs */
158 if (memchr(p
, 0, n
)) {
159 log_lldp("Found inner NUL in string, ignoring field.");
163 /* Let's escape weird chars, for security reasons */
164 k
= cescape_length(p
, n
);
174 int lldp_neighbor_parse(sd_lldp_neighbor
*n
) {
175 struct ether_header h
;
182 if (n
->raw_size
< sizeof(struct ether_header
)) {
183 log_lldp("Received truncated packet, ignoring.");
187 memcpy(&h
, LLDP_NEIGHBOR_RAW(n
), sizeof(h
));
189 if (h
.ether_type
!= htobe16(ETHERTYPE_LLDP
)) {
190 log_lldp("Received packet with wrong type, ignoring.");
194 if (h
.ether_dhost
[0] != 0x01 ||
195 h
.ether_dhost
[1] != 0x80 ||
196 h
.ether_dhost
[2] != 0xc2 ||
197 h
.ether_dhost
[3] != 0x00 ||
198 h
.ether_dhost
[4] != 0x00 ||
199 !IN_SET(h
.ether_dhost
[5], 0x00, 0x03, 0x0e)) {
200 log_lldp("Received packet with wrong destination address, ignoring.");
204 memcpy(&n
->source_address
, h
.ether_shost
, sizeof(struct ether_addr
));
205 memcpy(&n
->destination_address
, h
.ether_dhost
, sizeof(struct ether_addr
));
207 p
= (const uint8_t*) LLDP_NEIGHBOR_RAW(n
) + sizeof(struct ether_header
);
208 left
= n
->raw_size
- sizeof(struct ether_header
);
215 log_lldp("TLV lacks header, ignoring.");
220 length
= p
[1] + (((uint16_t) (p
[0] & 1)) << 8);
224 log_lldp("TLV truncated, ignoring datagram.");
230 case SD_LLDP_TYPE_END
:
232 log_lldp("End marker TLV not zero-sized, ignoring datagram.");
236 /* Note that after processing the SD_LLDP_TYPE_END left could still be > 0
237 * as the message may contain padding (see IEEE 802.1AB-2016, sec. 8.5.12) */
241 case SD_LLDP_TYPE_CHASSIS_ID
:
242 if (length
< 2 || length
> 256) { /* includes the chassis subtype, hence one extra byte */
243 log_lldp("Chassis ID field size out of range, ignoring datagram.");
246 if (n
->id
.chassis_id
) {
247 log_lldp("Duplicate chassis ID field, ignoring datagram.");
251 n
->id
.chassis_id
= memdup(p
, length
);
252 if (!n
->id
.chassis_id
)
255 n
->id
.chassis_id_size
= length
;
258 case SD_LLDP_TYPE_PORT_ID
:
259 if (length
< 2 || length
> 256) { /* includes the port subtype, hence one extra byte */
260 log_lldp("Port ID field size out of range, ignoring datagram.");
264 log_lldp("Duplicate port ID field, ignoring datagram.");
268 n
->id
.port_id
= memdup(p
, length
);
272 n
->id
.port_id_size
= length
;
275 case SD_LLDP_TYPE_TTL
:
277 log_lldp("TTL field has wrong size, ignoring datagram.");
282 log_lldp("Duplicate TTL field, ignoring datagram.");
286 n
->ttl
= unaligned_read_be16(p
);
290 case SD_LLDP_TYPE_PORT_DESCRIPTION
:
291 r
= parse_string(&n
->port_description
, p
, length
);
296 case SD_LLDP_TYPE_SYSTEM_NAME
:
297 r
= parse_string(&n
->system_name
, p
, length
);
302 case SD_LLDP_TYPE_SYSTEM_DESCRIPTION
:
303 r
= parse_string(&n
->system_description
, p
, length
);
308 case SD_LLDP_TYPE_SYSTEM_CAPABILITIES
:
310 log_lldp("System capabilities field has wrong size, ignoring.");
312 n
->system_capabilities
= unaligned_read_be16(p
);
313 n
->enabled_capabilities
= unaligned_read_be16(p
+ 2);
314 n
->has_capabilities
= true;
319 case SD_LLDP_TYPE_PRIVATE
:
321 log_lldp("Found private TLV that is too short, ignoring.");
326 p
+= length
, left
-= length
;
330 if (!n
->id
.chassis_id
|| !n
->id
.port_id
|| !n
->has_ttl
) {
331 log_lldp("One or more mandatory TLV missing in datagram. Ignoring.");
336 n
->rindex
= sizeof(struct ether_header
);
341 void lldp_neighbor_start_ttl(sd_lldp_neighbor
*n
) {
347 /* Use the packet's timestamp if there is one known */
348 base
= triple_timestamp_by_clock(&n
->timestamp
, clock_boottime_or_monotonic());
349 if (base
<= 0 || base
== USEC_INFINITY
)
350 base
= now(clock_boottime_or_monotonic()); /* Otherwise, take the current time */
352 n
->until
= usec_add(base
, n
->ttl
* USEC_PER_SEC
);
357 prioq_reshuffle(n
->lldp
->neighbor_by_expiry
, n
, &n
->prioq_idx
);
360 bool lldp_neighbor_equal(const sd_lldp_neighbor
*a
, const sd_lldp_neighbor
*b
) {
367 if (a
->raw_size
!= b
->raw_size
)
370 return memcmp(LLDP_NEIGHBOR_RAW(a
), LLDP_NEIGHBOR_RAW(b
), a
->raw_size
) == 0;
373 _public_
int sd_lldp_neighbor_get_source_address(sd_lldp_neighbor
*n
, struct ether_addr
* address
) {
374 assert_return(n
, -EINVAL
);
375 assert_return(address
, -EINVAL
);
377 *address
= n
->source_address
;
381 _public_
int sd_lldp_neighbor_get_destination_address(sd_lldp_neighbor
*n
, struct ether_addr
* address
) {
382 assert_return(n
, -EINVAL
);
383 assert_return(address
, -EINVAL
);
385 *address
= n
->destination_address
;
389 _public_
int sd_lldp_neighbor_get_raw(sd_lldp_neighbor
*n
, const void **ret
, size_t *size
) {
390 assert_return(n
, -EINVAL
);
391 assert_return(ret
, -EINVAL
);
392 assert_return(size
, -EINVAL
);
394 *ret
= LLDP_NEIGHBOR_RAW(n
);
400 _public_
int sd_lldp_neighbor_get_chassis_id(sd_lldp_neighbor
*n
, uint8_t *type
, const void **ret
, size_t *size
) {
401 assert_return(n
, -EINVAL
);
402 assert_return(type
, -EINVAL
);
403 assert_return(ret
, -EINVAL
);
404 assert_return(size
, -EINVAL
);
406 assert(n
->id
.chassis_id_size
> 0);
408 *type
= *(uint8_t*) n
->id
.chassis_id
;
409 *ret
= (uint8_t*) n
->id
.chassis_id
+ 1;
410 *size
= n
->id
.chassis_id_size
- 1;
415 static int format_mac_address(const void *data
, size_t sz
, char **ret
) {
419 assert(data
|| sz
<= 0);
424 memcpy(&a
, (uint8_t*) data
+ 1, sizeof(a
));
426 k
= new(char, ETHER_ADDR_TO_STRING_MAX
);
430 *ret
= ether_addr_to_string(&a
, k
);
434 static int format_network_address(const void *data
, size_t sz
, char **ret
) {
435 union in_addr_union a
;
438 if (sz
== 6 && ((uint8_t*) data
)[1] == 1) {
439 memcpy(&a
.in
, (uint8_t*) data
+ 2, sizeof(a
.in
));
441 } else if (sz
== 18 && ((uint8_t*) data
)[1] == 2) {
442 memcpy(&a
.in6
, (uint8_t*) data
+ 2, sizeof(a
.in6
));
447 r
= in_addr_to_string(family
, &a
, ret
);
453 _public_
int sd_lldp_neighbor_get_chassis_id_as_string(sd_lldp_neighbor
*n
, const char **ret
) {
457 assert_return(n
, -EINVAL
);
458 assert_return(ret
, -EINVAL
);
460 if (n
->chassis_id_as_string
) {
461 *ret
= n
->chassis_id_as_string
;
465 assert(n
->id
.chassis_id_size
> 0);
467 switch (*(uint8_t*) n
->id
.chassis_id
) {
469 case SD_LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT
:
470 case SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_ALIAS
:
471 case SD_LLDP_CHASSIS_SUBTYPE_PORT_COMPONENT
:
472 case SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME
:
473 case SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED
:
474 k
= cescape_length((char*) n
->id
.chassis_id
+ 1, n
->id
.chassis_id_size
- 1);
480 case SD_LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS
:
481 r
= format_mac_address(n
->id
.chassis_id
, n
->id
.chassis_id_size
, &k
);
489 case SD_LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS
:
490 r
= format_network_address(n
->id
.chassis_id
, n
->id
.chassis_id_size
, &k
);
499 /* Generic fallback */
500 k
= hexmem(n
->id
.chassis_id
, n
->id
.chassis_id_size
);
505 *ret
= n
->chassis_id_as_string
= k
;
509 _public_
int sd_lldp_neighbor_get_port_id(sd_lldp_neighbor
*n
, uint8_t *type
, const void **ret
, size_t *size
) {
510 assert_return(n
, -EINVAL
);
511 assert_return(type
, -EINVAL
);
512 assert_return(ret
, -EINVAL
);
513 assert_return(size
, -EINVAL
);
515 assert(n
->id
.port_id_size
> 0);
517 *type
= *(uint8_t*) n
->id
.port_id
;
518 *ret
= (uint8_t*) n
->id
.port_id
+ 1;
519 *size
= n
->id
.port_id_size
- 1;
524 _public_
int sd_lldp_neighbor_get_port_id_as_string(sd_lldp_neighbor
*n
, const char **ret
) {
528 assert_return(n
, -EINVAL
);
529 assert_return(ret
, -EINVAL
);
531 if (n
->port_id_as_string
) {
532 *ret
= n
->port_id_as_string
;
536 assert(n
->id
.port_id_size
> 0);
538 switch (*(uint8_t*) n
->id
.port_id
) {
540 case SD_LLDP_PORT_SUBTYPE_INTERFACE_ALIAS
:
541 case SD_LLDP_PORT_SUBTYPE_PORT_COMPONENT
:
542 case SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME
:
543 case SD_LLDP_PORT_SUBTYPE_LOCALLY_ASSIGNED
:
544 k
= cescape_length((char*) n
->id
.port_id
+ 1, n
->id
.port_id_size
- 1);
550 case SD_LLDP_PORT_SUBTYPE_MAC_ADDRESS
:
551 r
= format_mac_address(n
->id
.port_id
, n
->id
.port_id_size
, &k
);
559 case SD_LLDP_PORT_SUBTYPE_NETWORK_ADDRESS
:
560 r
= format_network_address(n
->id
.port_id
, n
->id
.port_id_size
, &k
);
569 /* Generic fallback */
570 k
= hexmem(n
->id
.port_id
, n
->id
.port_id_size
);
575 *ret
= n
->port_id_as_string
= k
;
579 _public_
int sd_lldp_neighbor_get_ttl(sd_lldp_neighbor
*n
, uint16_t *ret_sec
) {
580 assert_return(n
, -EINVAL
);
581 assert_return(ret_sec
, -EINVAL
);
587 _public_
int sd_lldp_neighbor_get_system_name(sd_lldp_neighbor
*n
, const char **ret
) {
588 assert_return(n
, -EINVAL
);
589 assert_return(ret
, -EINVAL
);
594 *ret
= n
->system_name
;
598 _public_
int sd_lldp_neighbor_get_system_description(sd_lldp_neighbor
*n
, const char **ret
) {
599 assert_return(n
, -EINVAL
);
600 assert_return(ret
, -EINVAL
);
602 if (!n
->system_description
)
605 *ret
= n
->system_description
;
609 _public_
int sd_lldp_neighbor_get_port_description(sd_lldp_neighbor
*n
, const char **ret
) {
610 assert_return(n
, -EINVAL
);
611 assert_return(ret
, -EINVAL
);
613 if (!n
->port_description
)
616 *ret
= n
->port_description
;
620 _public_
int sd_lldp_neighbor_get_system_capabilities(sd_lldp_neighbor
*n
, uint16_t *ret
) {
621 assert_return(n
, -EINVAL
);
622 assert_return(ret
, -EINVAL
);
624 if (!n
->has_capabilities
)
627 *ret
= n
->system_capabilities
;
631 _public_
int sd_lldp_neighbor_get_enabled_capabilities(sd_lldp_neighbor
*n
, uint16_t *ret
) {
632 assert_return(n
, -EINVAL
);
633 assert_return(ret
, -EINVAL
);
635 if (!n
->has_capabilities
)
638 *ret
= n
->enabled_capabilities
;
642 _public_
int sd_lldp_neighbor_from_raw(sd_lldp_neighbor
**ret
, const void *raw
, size_t raw_size
) {
643 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
646 assert_return(ret
, -EINVAL
);
647 assert_return(raw
|| raw_size
<= 0, -EINVAL
);
649 n
= lldp_neighbor_new(raw_size
);
653 memcpy(LLDP_NEIGHBOR_RAW(n
), raw
, raw_size
);
654 r
= lldp_neighbor_parse(n
);
663 _public_
int sd_lldp_neighbor_tlv_rewind(sd_lldp_neighbor
*n
) {
664 assert_return(n
, -EINVAL
);
666 assert(n
->raw_size
>= sizeof(struct ether_header
));
667 n
->rindex
= sizeof(struct ether_header
);
669 return n
->rindex
< n
->raw_size
;
672 _public_
int sd_lldp_neighbor_tlv_next(sd_lldp_neighbor
*n
) {
675 assert_return(n
, -EINVAL
);
677 if (n
->rindex
== n
->raw_size
) /* EOF */
680 if (n
->rindex
+ 2 > n
->raw_size
) /* Truncated message */
683 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
684 if (n
->rindex
+ 2 + length
> n
->raw_size
)
687 n
->rindex
+= 2 + length
;
688 return n
->rindex
< n
->raw_size
;
691 _public_
int sd_lldp_neighbor_tlv_get_type(sd_lldp_neighbor
*n
, uint8_t *type
) {
692 assert_return(n
, -EINVAL
);
693 assert_return(type
, -EINVAL
);
695 if (n
->rindex
== n
->raw_size
) /* EOF */
698 if (n
->rindex
+ 2 > n
->raw_size
)
701 *type
= LLDP_NEIGHBOR_TLV_TYPE(n
);
705 _public_
int sd_lldp_neighbor_tlv_is_type(sd_lldp_neighbor
*n
, uint8_t type
) {
709 assert_return(n
, -EINVAL
);
711 r
= sd_lldp_neighbor_tlv_get_type(n
, &k
);
718 _public_
int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor
*n
, uint8_t oui
[3], uint8_t *subtype
) {
723 assert_return(n
, -EINVAL
);
724 assert_return(oui
, -EINVAL
);
725 assert_return(subtype
, -EINVAL
);
727 r
= sd_lldp_neighbor_tlv_is_type(n
, SD_LLDP_TYPE_PRIVATE
);
733 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
737 if (n
->rindex
+ 2 + length
> n
->raw_size
)
740 d
= LLDP_NEIGHBOR_TLV_DATA(n
);
747 _public_
int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor
*n
, const uint8_t oui
[3], uint8_t subtype
) {
751 r
= sd_lldp_neighbor_tlv_get_oui(n
, k
, &st
);
757 return memcmp(k
, oui
, 3) == 0 && st
== subtype
;
760 _public_
int sd_lldp_neighbor_tlv_get_raw(sd_lldp_neighbor
*n
, const void **ret
, size_t *size
) {
763 assert_return(n
, -EINVAL
);
764 assert_return(ret
, -EINVAL
);
765 assert_return(size
, -EINVAL
);
767 /* Note that this returns the full TLV, including the TLV header */
769 if (n
->rindex
+ 2 > n
->raw_size
)
772 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
773 if (n
->rindex
+ 2 + length
> n
->raw_size
)
776 *ret
= (uint8_t*) LLDP_NEIGHBOR_RAW(n
) + n
->rindex
;
782 _public_
int sd_lldp_neighbor_get_timestamp(sd_lldp_neighbor
*n
, clockid_t clock
, uint64_t *ret
) {
783 assert_return(n
, -EINVAL
);
784 assert_return(TRIPLE_TIMESTAMP_HAS_CLOCK(clock
), -EOPNOTSUPP
);
785 assert_return(clock_supported(clock
), -EOPNOTSUPP
);
786 assert_return(ret
, -EINVAL
);
788 if (!triple_timestamp_is_set(&n
->timestamp
))
791 *ret
= triple_timestamp_by_clock(&n
->timestamp
, clock
);