2 This file is part of systemd.
4 Copyright 2016 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
22 #include "ether-addr-util.h"
23 #include "hexdecoct.h"
24 #include "in-addr-util.h"
25 #include "lldp-internal.h"
26 #include "lldp-neighbor.h"
27 #include "unaligned.h"
29 static void lldp_neighbor_id_hash_func(const void *p
, struct siphash
*state
) {
30 const LLDPNeighborID
*id
= p
;
32 siphash24_compress(id
->chassis_id
, id
->chassis_id_size
, state
);
33 siphash24_compress(&id
->chassis_id_size
, sizeof(id
->chassis_id_size
), state
);
34 siphash24_compress(id
->port_id
, id
->port_id_size
, state
);
35 siphash24_compress(&id
->port_id_size
, sizeof(id
->port_id_size
), state
);
38 static int lldp_neighbor_id_compare_func(const void *a
, const void *b
) {
39 const LLDPNeighborID
*x
= a
, *y
= b
;
42 r
= memcmp(x
->chassis_id
, y
->chassis_id
, MIN(x
->chassis_id_size
, y
->chassis_id_size
));
46 if (x
->chassis_id_size
< y
->chassis_id_size
)
49 if (x
->chassis_id_size
> y
->chassis_id_size
)
52 r
= memcmp(x
->port_id
, y
->port_id
, MIN(x
->port_id_size
, y
->port_id_size
));
56 if (x
->port_id_size
< y
->port_id_size
)
58 if (x
->port_id_size
> y
->port_id_size
)
64 const struct hash_ops lldp_neighbor_id_hash_ops
= {
65 .hash
= lldp_neighbor_id_hash_func
,
66 .compare
= lldp_neighbor_id_compare_func
69 int lldp_neighbor_prioq_compare_func(const void *a
, const void *b
) {
70 const sd_lldp_neighbor
*x
= a
, *y
= b
;
72 if (x
->until
< y
->until
)
75 if (x
->until
> y
->until
)
81 _public_ sd_lldp_neighbor
*sd_lldp_neighbor_ref(sd_lldp_neighbor
*n
) {
85 assert(n
->n_ref
> 0 || n
->lldp
);
91 static void lldp_neighbor_free(sd_lldp_neighbor
*n
) {
95 free(n
->id
.chassis_id
);
96 free(n
->port_description
);
98 free(n
->system_description
);
99 free(n
->chassis_id_as_string
);
100 free(n
->port_id_as_string
);
104 _public_ sd_lldp_neighbor
*sd_lldp_neighbor_unref(sd_lldp_neighbor
*n
) {
106 /* Drops one reference from the neighbor. Note that the object is not freed unless it is already unlinked from
107 * the sd_lldp object. */
112 assert(n
->n_ref
> 0);
115 if (n
->n_ref
<= 0 && !n
->lldp
)
116 lldp_neighbor_free(n
);
121 sd_lldp_neighbor
*lldp_neighbor_unlink(sd_lldp_neighbor
*n
) {
123 /* Removes the neighbor object from the LLDP object, and frees it if it also has no other reference. */
131 assert_se(hashmap_remove(n
->lldp
->neighbor_by_id
, &n
->id
) == n
);
132 assert_se(prioq_remove(n
->lldp
->neighbor_by_expiry
, n
, &n
->prioq_idx
) >= 0);
137 lldp_neighbor_free(n
);
142 sd_lldp_neighbor
*lldp_neighbor_new(size_t raw_size
) {
145 n
= malloc0(ALIGN(sizeof(sd_lldp_neighbor
)) + raw_size
);
149 n
->raw_size
= raw_size
;
155 static int parse_string(char **s
, const void *q
, size_t n
) {
163 log_lldp("Found duplicate string, ignoring field.");
167 /* Strip trailing NULs, just to be nice */
168 while (n
> 0 && p
[n
-1] == 0)
171 if (n
<= 0) /* Ignore empty strings */
174 /* Look for inner NULs */
175 if (memchr(p
, 0, n
)) {
176 log_lldp("Found inner NUL in string, ignoring field.");
180 /* Let's escape weird chars, for security reasons */
181 k
= cescape_length(p
, n
);
191 int lldp_neighbor_parse(sd_lldp_neighbor
*n
) {
192 struct ether_header h
;
199 if (n
->raw_size
< sizeof(struct ether_header
)) {
200 log_lldp("Received truncated packet, ignoring.");
204 memcpy(&h
, LLDP_NEIGHBOR_RAW(n
), sizeof(h
));
206 if (h
.ether_type
!= htobe16(ETHERTYPE_LLDP
)) {
207 log_lldp("Received packet with wrong type, ignoring.");
211 if (h
.ether_dhost
[0] != 0x01 ||
212 h
.ether_dhost
[1] != 0x80 ||
213 h
.ether_dhost
[2] != 0xc2 ||
214 h
.ether_dhost
[3] != 0x00 ||
215 h
.ether_dhost
[4] != 0x00 ||
216 !IN_SET(h
.ether_dhost
[5], 0x00, 0x03, 0x0e)) {
217 log_lldp("Received packet with wrong destination address, ignoring.");
221 memcpy(&n
->source_address
, h
.ether_shost
, sizeof(struct ether_addr
));
222 memcpy(&n
->destination_address
, h
.ether_dhost
, sizeof(struct ether_addr
));
224 p
= (const uint8_t*) LLDP_NEIGHBOR_RAW(n
) + sizeof(struct ether_header
);
225 left
= n
->raw_size
- sizeof(struct ether_header
);
232 log_lldp("TLV lacks header, ignoring.");
237 length
= p
[1] + (((uint16_t) (p
[0] & 1)) << 8);
241 log_lldp("TLV truncated, ignoring datagram.");
247 case SD_LLDP_TYPE_END
:
249 log_lldp("End marker TLV not zero-sized, ignoring datagram.");
253 /* Note that after processing the SD_LLDP_TYPE_END left could still be > 0
254 * as the message may contain padding (see IEEE 802.1AB-2016, sec. 8.5.12) */
258 case SD_LLDP_TYPE_CHASSIS_ID
:
259 if (length
< 2 || length
> 256) { /* includes the chassis subtype, hence one extra byte */
260 log_lldp("Chassis ID field size out of range, ignoring datagram.");
263 if (n
->id
.chassis_id
) {
264 log_lldp("Duplicate chassis ID field, ignoring datagram.");
268 n
->id
.chassis_id
= memdup(p
, length
);
269 if (!n
->id
.chassis_id
)
272 n
->id
.chassis_id_size
= length
;
275 case SD_LLDP_TYPE_PORT_ID
:
276 if (length
< 2 || length
> 256) { /* includes the port subtype, hence one extra byte */
277 log_lldp("Port ID field size out of range, ignoring datagram.");
281 log_lldp("Duplicate port ID field, ignoring datagram.");
285 n
->id
.port_id
= memdup(p
, length
);
289 n
->id
.port_id_size
= length
;
292 case SD_LLDP_TYPE_TTL
:
294 log_lldp("TTL field has wrong size, ignoring datagram.");
299 log_lldp("Duplicate TTL field, ignoring datagram.");
303 n
->ttl
= unaligned_read_be16(p
);
307 case SD_LLDP_TYPE_PORT_DESCRIPTION
:
308 r
= parse_string(&n
->port_description
, p
, length
);
313 case SD_LLDP_TYPE_SYSTEM_NAME
:
314 r
= parse_string(&n
->system_name
, p
, length
);
319 case SD_LLDP_TYPE_SYSTEM_DESCRIPTION
:
320 r
= parse_string(&n
->system_description
, p
, length
);
325 case SD_LLDP_TYPE_SYSTEM_CAPABILITIES
:
327 log_lldp("System capabilities field has wrong size, ignoring.");
329 n
->system_capabilities
= unaligned_read_be16(p
);
330 n
->enabled_capabilities
= unaligned_read_be16(p
+ 2);
331 n
->has_capabilities
= true;
336 case SD_LLDP_TYPE_PRIVATE
:
338 log_lldp("Found private TLV that is too short, ignoring.");
344 p
+= length
, left
-= length
;
348 if (!n
->id
.chassis_id
|| !n
->id
.port_id
|| !n
->has_ttl
) {
349 log_lldp("One or more mandatory TLV missing in datagram. Ignoring.");
354 n
->rindex
= sizeof(struct ether_header
);
359 void lldp_neighbor_start_ttl(sd_lldp_neighbor
*n
) {
365 /* Use the packet's timestamp if there is one known */
366 base
= triple_timestamp_by_clock(&n
->timestamp
, clock_boottime_or_monotonic());
367 if (base
<= 0 || base
== USEC_INFINITY
)
368 base
= now(clock_boottime_or_monotonic()); /* Otherwise, take the current time */
370 n
->until
= usec_add(base
, n
->ttl
* USEC_PER_SEC
);
375 prioq_reshuffle(n
->lldp
->neighbor_by_expiry
, n
, &n
->prioq_idx
);
378 bool lldp_neighbor_equal(const sd_lldp_neighbor
*a
, const sd_lldp_neighbor
*b
) {
385 if (a
->raw_size
!= b
->raw_size
)
388 return memcmp(LLDP_NEIGHBOR_RAW(a
), LLDP_NEIGHBOR_RAW(b
), a
->raw_size
) == 0;
391 _public_
int sd_lldp_neighbor_get_source_address(sd_lldp_neighbor
*n
, struct ether_addr
* address
) {
392 assert_return(n
, -EINVAL
);
393 assert_return(address
, -EINVAL
);
395 *address
= n
->source_address
;
399 _public_
int sd_lldp_neighbor_get_destination_address(sd_lldp_neighbor
*n
, struct ether_addr
* address
) {
400 assert_return(n
, -EINVAL
);
401 assert_return(address
, -EINVAL
);
403 *address
= n
->destination_address
;
407 _public_
int sd_lldp_neighbor_get_raw(sd_lldp_neighbor
*n
, const void **ret
, size_t *size
) {
408 assert_return(n
, -EINVAL
);
409 assert_return(ret
, -EINVAL
);
410 assert_return(size
, -EINVAL
);
412 *ret
= LLDP_NEIGHBOR_RAW(n
);
418 _public_
int sd_lldp_neighbor_get_chassis_id(sd_lldp_neighbor
*n
, uint8_t *type
, const void **ret
, size_t *size
) {
419 assert_return(n
, -EINVAL
);
420 assert_return(type
, -EINVAL
);
421 assert_return(ret
, -EINVAL
);
422 assert_return(size
, -EINVAL
);
424 assert(n
->id
.chassis_id_size
> 0);
426 *type
= *(uint8_t*) n
->id
.chassis_id
;
427 *ret
= (uint8_t*) n
->id
.chassis_id
+ 1;
428 *size
= n
->id
.chassis_id_size
- 1;
433 static int format_mac_address(const void *data
, size_t sz
, char **ret
) {
437 assert(data
|| sz
<= 0);
442 memcpy(&a
, (uint8_t*) data
+ 1, sizeof(a
));
444 k
= new(char, ETHER_ADDR_TO_STRING_MAX
);
448 *ret
= ether_addr_to_string(&a
, k
);
452 static int format_network_address(const void *data
, size_t sz
, char **ret
) {
453 union in_addr_union a
;
456 if (sz
== 6 && ((uint8_t*) data
)[1] == 1) {
457 memcpy(&a
.in
, (uint8_t*) data
+ 2, sizeof(a
.in
));
459 } else if (sz
== 18 && ((uint8_t*) data
)[1] == 2) {
460 memcpy(&a
.in6
, (uint8_t*) data
+ 2, sizeof(a
.in6
));
465 r
= in_addr_to_string(family
, &a
, ret
);
471 _public_
int sd_lldp_neighbor_get_chassis_id_as_string(sd_lldp_neighbor
*n
, const char **ret
) {
475 assert_return(n
, -EINVAL
);
476 assert_return(ret
, -EINVAL
);
478 if (n
->chassis_id_as_string
) {
479 *ret
= n
->chassis_id_as_string
;
483 assert(n
->id
.chassis_id_size
> 0);
485 switch (*(uint8_t*) n
->id
.chassis_id
) {
487 case SD_LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT
:
488 case SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_ALIAS
:
489 case SD_LLDP_CHASSIS_SUBTYPE_PORT_COMPONENT
:
490 case SD_LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME
:
491 case SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED
:
492 k
= cescape_length((char*) n
->id
.chassis_id
+ 1, n
->id
.chassis_id_size
- 1);
498 case SD_LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS
:
499 r
= format_mac_address(n
->id
.chassis_id
, n
->id
.chassis_id_size
, &k
);
507 case SD_LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS
:
508 r
= format_network_address(n
->id
.chassis_id
, n
->id
.chassis_id_size
, &k
);
517 /* Generic fallback */
518 k
= hexmem(n
->id
.chassis_id
, n
->id
.chassis_id_size
);
523 *ret
= n
->chassis_id_as_string
= k
;
527 _public_
int sd_lldp_neighbor_get_port_id(sd_lldp_neighbor
*n
, uint8_t *type
, const void **ret
, size_t *size
) {
528 assert_return(n
, -EINVAL
);
529 assert_return(type
, -EINVAL
);
530 assert_return(ret
, -EINVAL
);
531 assert_return(size
, -EINVAL
);
533 assert(n
->id
.port_id_size
> 0);
535 *type
= *(uint8_t*) n
->id
.port_id
;
536 *ret
= (uint8_t*) n
->id
.port_id
+ 1;
537 *size
= n
->id
.port_id_size
- 1;
542 _public_
int sd_lldp_neighbor_get_port_id_as_string(sd_lldp_neighbor
*n
, const char **ret
) {
546 assert_return(n
, -EINVAL
);
547 assert_return(ret
, -EINVAL
);
549 if (n
->port_id_as_string
) {
550 *ret
= n
->port_id_as_string
;
554 assert(n
->id
.port_id_size
> 0);
556 switch (*(uint8_t*) n
->id
.port_id
) {
558 case SD_LLDP_PORT_SUBTYPE_INTERFACE_ALIAS
:
559 case SD_LLDP_PORT_SUBTYPE_PORT_COMPONENT
:
560 case SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME
:
561 case SD_LLDP_PORT_SUBTYPE_LOCALLY_ASSIGNED
:
562 k
= cescape_length((char*) n
->id
.port_id
+ 1, n
->id
.port_id_size
- 1);
568 case SD_LLDP_PORT_SUBTYPE_MAC_ADDRESS
:
569 r
= format_mac_address(n
->id
.port_id
, n
->id
.port_id_size
, &k
);
577 case SD_LLDP_PORT_SUBTYPE_NETWORK_ADDRESS
:
578 r
= format_network_address(n
->id
.port_id
, n
->id
.port_id_size
, &k
);
587 /* Generic fallback */
588 k
= hexmem(n
->id
.port_id
, n
->id
.port_id_size
);
593 *ret
= n
->port_id_as_string
= k
;
597 _public_
int sd_lldp_neighbor_get_ttl(sd_lldp_neighbor
*n
, uint16_t *ret_sec
) {
598 assert_return(n
, -EINVAL
);
599 assert_return(ret_sec
, -EINVAL
);
605 _public_
int sd_lldp_neighbor_get_system_name(sd_lldp_neighbor
*n
, const char **ret
) {
606 assert_return(n
, -EINVAL
);
607 assert_return(ret
, -EINVAL
);
612 *ret
= n
->system_name
;
616 _public_
int sd_lldp_neighbor_get_system_description(sd_lldp_neighbor
*n
, const char **ret
) {
617 assert_return(n
, -EINVAL
);
618 assert_return(ret
, -EINVAL
);
620 if (!n
->system_description
)
623 *ret
= n
->system_description
;
627 _public_
int sd_lldp_neighbor_get_port_description(sd_lldp_neighbor
*n
, const char **ret
) {
628 assert_return(n
, -EINVAL
);
629 assert_return(ret
, -EINVAL
);
631 if (!n
->port_description
)
634 *ret
= n
->port_description
;
638 _public_
int sd_lldp_neighbor_get_system_capabilities(sd_lldp_neighbor
*n
, uint16_t *ret
) {
639 assert_return(n
, -EINVAL
);
640 assert_return(ret
, -EINVAL
);
642 if (!n
->has_capabilities
)
645 *ret
= n
->system_capabilities
;
649 _public_
int sd_lldp_neighbor_get_enabled_capabilities(sd_lldp_neighbor
*n
, uint16_t *ret
) {
650 assert_return(n
, -EINVAL
);
651 assert_return(ret
, -EINVAL
);
653 if (!n
->has_capabilities
)
656 *ret
= n
->enabled_capabilities
;
660 _public_
int sd_lldp_neighbor_from_raw(sd_lldp_neighbor
**ret
, const void *raw
, size_t raw_size
) {
661 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
664 assert_return(ret
, -EINVAL
);
665 assert_return(raw
|| raw_size
<= 0, -EINVAL
);
667 n
= lldp_neighbor_new(raw_size
);
671 memcpy(LLDP_NEIGHBOR_RAW(n
), raw
, raw_size
);
672 r
= lldp_neighbor_parse(n
);
682 _public_
int sd_lldp_neighbor_tlv_rewind(sd_lldp_neighbor
*n
) {
683 assert_return(n
, -EINVAL
);
685 assert(n
->raw_size
>= sizeof(struct ether_header
));
686 n
->rindex
= sizeof(struct ether_header
);
688 return n
->rindex
< n
->raw_size
;
691 _public_
int sd_lldp_neighbor_tlv_next(sd_lldp_neighbor
*n
) {
694 assert_return(n
, -EINVAL
);
696 if (n
->rindex
== n
->raw_size
) /* EOF */
699 if (n
->rindex
+ 2 > n
->raw_size
) /* Truncated message */
702 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
703 if (n
->rindex
+ 2 + length
> n
->raw_size
)
706 n
->rindex
+= 2 + length
;
707 return n
->rindex
< n
->raw_size
;
710 _public_
int sd_lldp_neighbor_tlv_get_type(sd_lldp_neighbor
*n
, uint8_t *type
) {
711 assert_return(n
, -EINVAL
);
712 assert_return(type
, -EINVAL
);
714 if (n
->rindex
== n
->raw_size
) /* EOF */
717 if (n
->rindex
+ 2 > n
->raw_size
)
720 *type
= LLDP_NEIGHBOR_TLV_TYPE(n
);
724 _public_
int sd_lldp_neighbor_tlv_is_type(sd_lldp_neighbor
*n
, uint8_t type
) {
728 assert_return(n
, -EINVAL
);
730 r
= sd_lldp_neighbor_tlv_get_type(n
, &k
);
737 _public_
int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor
*n
, uint8_t oui
[3], uint8_t *subtype
) {
742 assert_return(n
, -EINVAL
);
743 assert_return(oui
, -EINVAL
);
744 assert_return(subtype
, -EINVAL
);
746 r
= sd_lldp_neighbor_tlv_is_type(n
, SD_LLDP_TYPE_PRIVATE
);
752 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
756 if (n
->rindex
+ 2 + length
> n
->raw_size
)
759 d
= LLDP_NEIGHBOR_TLV_DATA(n
);
766 _public_
int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor
*n
, const uint8_t oui
[3], uint8_t subtype
) {
770 r
= sd_lldp_neighbor_tlv_get_oui(n
, k
, &st
);
776 return memcmp(k
, oui
, 3) == 0 && st
== subtype
;
779 _public_
int sd_lldp_neighbor_tlv_get_raw(sd_lldp_neighbor
*n
, const void **ret
, size_t *size
) {
782 assert_return(n
, -EINVAL
);
783 assert_return(ret
, -EINVAL
);
784 assert_return(size
, -EINVAL
);
786 /* Note that this returns the full TLV, including the TLV header */
788 if (n
->rindex
+ 2 > n
->raw_size
)
791 length
= LLDP_NEIGHBOR_TLV_LENGTH(n
);
792 if (n
->rindex
+ 2 + length
> n
->raw_size
)
795 *ret
= (uint8_t*) LLDP_NEIGHBOR_RAW(n
) + n
->rindex
;
801 _public_
int sd_lldp_neighbor_get_timestamp(sd_lldp_neighbor
*n
, clockid_t clock
, uint64_t *ret
) {
802 assert_return(n
, -EINVAL
);
803 assert_return(TRIPLE_TIMESTAMP_HAS_CLOCK(clock
), -EOPNOTSUPP
);
804 assert_return(clock_supported(clock
), -EOPNOTSUPP
);
805 assert_return(ret
, -EINVAL
);
807 if (!triple_timestamp_is_set(&n
->timestamp
))
810 *ret
= triple_timestamp_by_clock(&n
->timestamp
, clock
);