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"
28 #include "unaligned.h"
30 static void lldp_neighbor_id_hash_func(const void *p
, struct siphash
*state
) {
31 const LLDPNeighborID
*id
= p
;
33 siphash24_compress(id
->chassis_id
, id
->chassis_id_size
, state
);
34 siphash24_compress(&id
->chassis_id_size
, sizeof(id
->chassis_id_size
), state
);
35 siphash24_compress(id
->port_id
, id
->port_id_size
, state
);
36 siphash24_compress(&id
->port_id_size
, sizeof(id
->port_id_size
), state
);
39 static int lldp_neighbor_id_compare_func(const void *a
, const void *b
) {
40 const LLDPNeighborID
*x
= a
, *y
= b
;
43 r
= memcmp(x
->chassis_id
, y
->chassis_id
, MIN(x
->chassis_id_size
, y
->chassis_id_size
));
47 if (x
->chassis_id_size
< y
->chassis_id_size
)
50 if (x
->chassis_id_size
> y
->chassis_id_size
)
53 r
= memcmp(x
->port_id
, y
->port_id
, MIN(x
->port_id_size
, y
->port_id_size
));
57 if (x
->port_id_size
< y
->port_id_size
)
59 if (x
->port_id_size
> y
->port_id_size
)
65 const struct hash_ops lldp_neighbor_id_hash_ops
= {
66 .hash
= lldp_neighbor_id_hash_func
,
67 .compare
= lldp_neighbor_id_compare_func
70 int lldp_neighbor_prioq_compare_func(const void *a
, const void *b
) {
71 const sd_lldp_neighbor
*x
= a
, *y
= b
;
73 if (x
->until
< y
->until
)
76 if (x
->until
> y
->until
)
82 _public_ sd_lldp_neighbor
*sd_lldp_neighbor_ref(sd_lldp_neighbor
*n
) {
86 assert(n
->n_ref
> 0 || n
->lldp
);
92 static void lldp_neighbor_free(sd_lldp_neighbor
*n
) {
96 free(n
->id
.chassis_id
);
97 free(n
->port_description
);
99 free(n
->system_description
);
100 free(n
->chassis_id_as_string
);
101 free(n
->port_id_as_string
);
105 _public_ sd_lldp_neighbor
*sd_lldp_neighbor_unref(sd_lldp_neighbor
*n
) {
107 /* Drops one reference from the neighbor. Note that the object is not freed unless it is already unlinked from
108 * the sd_lldp object. */
113 assert(n
->n_ref
> 0);
116 if (n
->n_ref
<= 0 && !n
->lldp
)
117 lldp_neighbor_free(n
);
122 sd_lldp_neighbor
*lldp_neighbor_unlink(sd_lldp_neighbor
*n
) {
124 /* Removes the neighbor object from the LLDP object, and frees it if it also has no other reference. */
132 assert_se(hashmap_remove(n
->lldp
->neighbor_by_id
, &n
->id
) == n
);
133 assert_se(prioq_remove(n
->lldp
->neighbor_by_expiry
, n
, &n
->prioq_idx
) >= 0);
138 lldp_neighbor_free(n
);
143 sd_lldp_neighbor
*lldp_neighbor_new(size_t raw_size
) {
146 n
= malloc0(ALIGN(sizeof(sd_lldp_neighbor
)) + raw_size
);
150 n
->raw_size
= raw_size
;
156 static int parse_string(char **s
, const void *q
, size_t n
) {
164 log_lldp("Found duplicate string, ignoring field.");
168 /* Strip trailing NULs, just to be nice */
169 while (n
> 0 && p
[n
-1] == 0)
172 if (n
<= 0) /* Ignore empty strings */
175 /* Look for inner NULs */
176 if (memchr(p
, 0, n
)) {
177 log_lldp("Found inner NUL in string, ignoring field.");
181 /* Let's escape weird chars, for security reasons */
182 k
= cescape_length(p
, n
);
192 int lldp_neighbor_parse(sd_lldp_neighbor
*n
) {
193 struct ether_header h
;
200 if (n
->raw_size
< sizeof(struct ether_header
)) {
201 log_lldp("Recieved truncated packet, ignoring.");
205 memcpy(&h
, LLDP_NEIGHBOR_RAW(n
), sizeof(h
));
207 if (h
.ether_type
!= htobe16(ETHERTYPE_LLDP
)) {
208 log_lldp("Received packet with wrong type, ignoring.");
212 if (h
.ether_dhost
[0] != 0x01 ||
213 h
.ether_dhost
[1] != 0x80 ||
214 h
.ether_dhost
[2] != 0xc2 ||
215 h
.ether_dhost
[3] != 0x00 ||
216 h
.ether_dhost
[4] != 0x00 ||
217 !IN_SET(h
.ether_dhost
[5], 0x00, 0x03, 0x0e)) {
218 log_lldp("Received packet with wrong destination address, ignoring.");
222 memcpy(&n
->source_address
, h
.ether_shost
, sizeof(struct ether_addr
));
223 memcpy(&n
->destination_address
, h
.ether_dhost
, sizeof(struct ether_addr
));
225 p
= (const uint8_t*) LLDP_NEIGHBOR_RAW(n
) + sizeof(struct ether_header
);
226 left
= n
->raw_size
- sizeof(struct ether_header
);
233 log_lldp("TLV lacks header, ignoring.");
238 length
= p
[1] + (((uint16_t) (p
[0] & 1)) << 8);
242 log_lldp("TLV truncated, ignoring datagram.");
250 log_lldp("End marker TLV not zero-sized, ignoring datagram.");
254 log_lldp("Trailing garbage in datagram, ignoring datagram.");
260 case LLDP_TYPE_CHASSIS_ID
:
261 if (length
< 2 || length
> 256) { /* includes the chassis subtype, hence one extra byte */
262 log_lldp("Chassis ID field size out of range, ignoring datagram.");
265 if (n
->id
.chassis_id
) {
266 log_lldp("Duplicate chassis ID field, ignoring datagram.");
270 n
->id
.chassis_id
= memdup(p
, length
);
271 if (!n
->id
.chassis_id
)
274 n
->id
.chassis_id_size
= length
;
277 case LLDP_TYPE_PORT_ID
:
278 if (length
< 2 || length
> 256) { /* includes the port subtype, hence one extra byte */
279 log_lldp("Port ID field size out of range, ignoring datagram.");
283 log_lldp("Duplicate port ID field, ignoring datagram.");
287 n
->id
.port_id
= memdup(p
, length
);
291 n
->id
.port_id_size
= length
;
296 log_lldp("TTL field has wrong size, ignoring datagram.");
301 log_lldp("Duplicate TTL field, ignoring datagram.");
305 n
->ttl
= unaligned_read_be16(p
);
309 case LLDP_TYPE_PORT_DESCRIPTION
:
310 r
= parse_string(&n
->port_description
, p
, length
);
315 case LLDP_TYPE_SYSTEM_NAME
:
316 r
= parse_string(&n
->system_name
, p
, length
);
321 case LLDP_TYPE_SYSTEM_DESCRIPTION
:
322 r
= parse_string(&n
->system_description
, p
, length
);
327 case LLDP_TYPE_SYSTEM_CAPABILITIES
:
329 log_lldp("System capabilities field has wrong size, ignoring.");
331 n
->system_capabilities
= unaligned_read_be16(p
);
332 n
->enabled_capabilities
= unaligned_read_be16(p
+ 2);
333 n
->has_capabilities
= true;
338 case LLDP_TYPE_PRIVATE
:
340 log_lldp("Found private TLV that is too short, ignoring.");
346 p
+= length
, left
-= length
;
350 if (!n
->id
.chassis_id
|| !n
->id
.port_id
|| !n
->has_ttl
) {
351 log_lldp("One or more mandatory TLV missing in datagram. Ignoring.");
356 n
->rindex
= sizeof(struct ether_header
);
361 void lldp_neighbor_start_ttl(sd_lldp_neighbor
*n
) {
365 n
->until
= usec_add(now(clock_boottime_or_monotonic()), n
->ttl
* USEC_PER_SEC
);
370 prioq_reshuffle(n
->lldp
->neighbor_by_expiry
, n
, &n
->prioq_idx
);
373 bool lldp_neighbor_equal(const sd_lldp_neighbor
*a
, const sd_lldp_neighbor
*b
) {
380 if (a
->raw_size
!= b
->raw_size
)
383 return memcmp(LLDP_NEIGHBOR_RAW(a
), LLDP_NEIGHBOR_RAW(b
), a
->raw_size
) == 0;
386 _public_
int sd_lldp_neighbor_get_source_address(sd_lldp_neighbor
*n
, struct ether_addr
* address
) {
387 assert_return(n
, -EINVAL
);
388 assert_return(address
, -EINVAL
);
390 *address
= n
->source_address
;
394 _public_
int sd_lldp_neighbor_get_destination_address(sd_lldp_neighbor
*n
, struct ether_addr
* address
) {
395 assert_return(n
, -EINVAL
);
396 assert_return(address
, -EINVAL
);
398 *address
= n
->destination_address
;
402 _public_
int sd_lldp_neighbor_get_raw(sd_lldp_neighbor
*n
, const void **ret
, size_t *size
) {
403 assert_return(n
, -EINVAL
);
404 assert_return(ret
, -EINVAL
);
405 assert_return(size
, -EINVAL
);
407 *ret
= LLDP_NEIGHBOR_RAW(n
);
413 _public_
int sd_lldp_neighbor_get_chassis_id(sd_lldp_neighbor
*n
, uint8_t *type
, const void **ret
, size_t *size
) {
414 assert_return(n
, -EINVAL
);
415 assert_return(type
, -EINVAL
);
416 assert_return(ret
, -EINVAL
);
417 assert_return(size
, -EINVAL
);
419 assert(n
->id
.chassis_id_size
> 0);
421 *type
= *(uint8_t*) n
->id
.chassis_id
;
422 *ret
= (uint8_t*) n
->id
.chassis_id
+ 1;
423 *size
= n
->id
.chassis_id_size
- 1;
428 static int format_mac_address(const void *data
, size_t sz
, char **ret
) {
432 assert(data
|| sz
<= 0);
437 memcpy(&a
, (uint8_t*) data
+ 1, sizeof(a
));
439 k
= new(char, ETHER_ADDR_TO_STRING_MAX
);
443 *ret
= ether_addr_to_string(&a
, k
);
447 static int format_network_address(const void *data
, size_t sz
, char **ret
) {
448 union in_addr_union a
;
451 if (sz
== 6 && ((uint8_t*) data
)[1] == 1) {
452 memcpy(&a
.in
, (uint8_t*) data
+ 2, sizeof(a
.in
));
454 } else if (sz
== 18 && ((uint8_t*) data
)[1] == 2) {
455 memcpy(&a
.in6
, (uint8_t*) data
+ 2, sizeof(a
.in6
));
460 r
= in_addr_to_string(family
, &a
, ret
);
466 _public_
int sd_lldp_neighbor_get_chassis_id_as_string(sd_lldp_neighbor
*n
, const char **ret
) {
470 assert_return(n
, -EINVAL
);
471 assert_return(ret
, -EINVAL
);
473 if (n
->chassis_id_as_string
) {
474 *ret
= n
->chassis_id_as_string
;
478 assert(n
->id
.chassis_id_size
> 0);
480 switch (*(uint8_t*) n
->id
.chassis_id
) {
482 case LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT
:
483 case LLDP_CHASSIS_SUBTYPE_INTERFACE_ALIAS
:
484 case LLDP_CHASSIS_SUBTYPE_PORT_COMPONENT
:
485 case LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME
:
486 case LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED
:
487 k
= cescape_length((char*) n
->id
.chassis_id
+ 1, n
->id
.chassis_id_size
- 1);
493 case LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS
:
494 r
= format_mac_address(n
->id
.chassis_id
, n
->id
.chassis_id_size
, &k
);
502 case LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS
:
503 r
= format_network_address(n
->id
.chassis_id
, n
->id
.chassis_id_size
, &k
);
512 /* Generic fallback */
513 k
= hexmem(n
->id
.chassis_id
, n
->id
.chassis_id_size
);
518 *ret
= n
->chassis_id_as_string
= k
;
522 _public_
int sd_lldp_neighbor_get_port_id(sd_lldp_neighbor
*n
, uint8_t *type
, const void **ret
, size_t *size
) {
523 assert_return(n
, -EINVAL
);
524 assert_return(type
, -EINVAL
);
525 assert_return(ret
, -EINVAL
);
526 assert_return(size
, -EINVAL
);
528 assert(n
->id
.port_id_size
> 0);
530 *type
= *(uint8_t*) n
->id
.port_id
;
531 *ret
= (uint8_t*) n
->id
.port_id
+ 1;
532 *size
= n
->id
.port_id_size
- 1;
537 _public_
int sd_lldp_neighbor_get_port_id_as_string(sd_lldp_neighbor
*n
, const char **ret
) {
541 assert_return(n
, -EINVAL
);
542 assert_return(ret
, -EINVAL
);
544 if (n
->port_id_as_string
) {
545 *ret
= n
->port_id_as_string
;
549 assert(n
->id
.port_id_size
> 0);
551 switch (*(uint8_t*) n
->id
.port_id
) {
553 case LLDP_PORT_SUBTYPE_INTERFACE_ALIAS
:
554 case LLDP_PORT_SUBTYPE_PORT_COMPONENT
:
555 case LLDP_PORT_SUBTYPE_INTERFACE_NAME
:
556 case LLDP_PORT_SUBTYPE_LOCALLY_ASSIGNED
:
557 k
= cescape_length((char*) n
->id
.port_id
+ 1, n
->id
.port_id_size
- 1);
563 case LLDP_PORT_SUBTYPE_MAC_ADDRESS
:
564 r
= format_mac_address(n
->id
.port_id
, n
->id
.port_id_size
, &k
);
572 case LLDP_PORT_SUBTYPE_NETWORK_ADDRESS
:
573 r
= format_network_address(n
->id
.port_id
, n
->id
.port_id_size
, &k
);
582 /* Generic fallback */
583 k
= hexmem(n
->id
.port_id
, n
->id
.port_id_size
);
588 *ret
= n
->port_id_as_string
= k
;
592 _public_
int sd_lldp_neighbor_get_ttl(sd_lldp_neighbor
*n
, uint16_t *ret
) {
593 assert_return(n
, -EINVAL
);
594 assert_return(ret
, -EINVAL
);
600 _public_
int sd_lldp_neighbor_get_system_name(sd_lldp_neighbor
*n
, const char **ret
) {
601 assert_return(n
, -EINVAL
);
602 assert_return(ret
, -EINVAL
);
607 *ret
= n
->system_name
;
611 _public_
int sd_lldp_neighbor_get_system_description(sd_lldp_neighbor
*n
, const char **ret
) {
612 assert_return(n
, -EINVAL
);
613 assert_return(ret
, -EINVAL
);
615 if (!n
->system_description
)
618 *ret
= n
->system_description
;
622 _public_
int sd_lldp_neighbor_get_port_description(sd_lldp_neighbor
*n
, const char **ret
) {
623 assert_return(n
, -EINVAL
);
624 assert_return(ret
, -EINVAL
);
626 if (!n
->port_description
)
629 *ret
= n
->port_description
;
633 _public_
int sd_lldp_neighbor_get_system_capabilities(sd_lldp_neighbor
*n
, uint16_t *ret
) {
634 assert_return(n
, -EINVAL
);
635 assert_return(ret
, -EINVAL
);
637 if (!n
->has_capabilities
)
640 *ret
= n
->system_capabilities
;
644 _public_
int sd_lldp_neighbor_get_enabled_capabilities(sd_lldp_neighbor
*n
, uint16_t *ret
) {
645 assert_return(n
, -EINVAL
);
646 assert_return(ret
, -EINVAL
);
648 if (!n
->has_capabilities
)
651 *ret
= n
->enabled_capabilities
;
655 int sd_lldp_neighbor_from_raw(sd_lldp_neighbor
**ret
, const void *raw
, size_t raw_size
) {
656 _cleanup_(sd_lldp_neighbor_unrefp
) sd_lldp_neighbor
*n
= NULL
;
659 assert_return(ret
, -EINVAL
);
660 assert_return(raw
|| raw_size
<= 0, -EINVAL
);
662 n
= lldp_neighbor_new(raw_size
);
666 memcpy(LLDP_NEIGHBOR_RAW(n
), raw
, raw_size
);
667 r
= lldp_neighbor_parse(n
);
677 _public_
int sd_lldp_neighbor_tlv_rewind(sd_lldp_neighbor
*n
) {
678 assert_return(n
, -EINVAL
);
680 assert(n
->raw_size
>= sizeof(struct ether_header
));
681 n
->rindex
= sizeof(struct ether_header
);
686 _public_
int sd_lldp_neighbor_tlv_next(sd_lldp_neighbor
*n
) {
689 assert_return(n
, -EINVAL
);
691 if (n
->rindex
== n
->raw_size
) /* EOF */
694 if (n
->rindex
+ 2 > n
->raw_size
) /* Truncated message */
697 length
= LLDP_NEIGHBOR_LENGTH(n
);
698 if (n
->rindex
+ 2 + length
> n
->raw_size
)
701 n
->rindex
+= 2 + length
;
702 return n
->rindex
< n
->raw_size
;
705 _public_
int sd_lldp_neighbor_tlv_get_type(sd_lldp_neighbor
*n
, uint8_t *type
) {
706 assert_return(n
, -EINVAL
);
707 assert_return(type
, -EINVAL
);
709 if (n
->rindex
== n
->raw_size
) /* EOF */
712 if (n
->rindex
+ 2 > n
->raw_size
)
715 *type
= LLDP_NEIGHBOR_TYPE(n
);
719 _public_
int sd_lldp_neighbor_tlv_is_type(sd_lldp_neighbor
*n
, uint8_t type
) {
723 assert_return(n
, -EINVAL
);
725 r
= sd_lldp_neighbor_tlv_get_type(n
, &k
);
732 _public_
int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor
*n
, uint8_t oui
[3], uint8_t *subtype
) {
737 assert_return(n
, -EINVAL
);
738 assert_return(oui
, -EINVAL
);
739 assert_return(subtype
, -EINVAL
);
741 r
= sd_lldp_neighbor_tlv_is_type(n
, LLDP_TYPE_PRIVATE
);
747 length
= LLDP_NEIGHBOR_LENGTH(n
);
751 if (n
->rindex
+ 2 + length
> n
->raw_size
)
754 d
= LLDP_NEIGHBOR_DATA(n
);
761 _public_
int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor
*n
, const uint8_t oui
[3], uint8_t subtype
) {
765 r
= sd_lldp_neighbor_tlv_get_oui(n
, k
, &st
);
771 return memcmp(k
, oui
, 3) == 0 && st
== subtype
;
774 _public_
int sd_lldp_neighbor_tlv_get_raw(sd_lldp_neighbor
*n
, const void **ret
, size_t *size
) {
777 assert_return(n
, -EINVAL
);
778 assert_return(ret
, -EINVAL
);
779 assert_return(size
, -EINVAL
);
781 /* Note that this returns the full TLV, including the TLV header */
783 if (n
->rindex
+ 2 > n
->raw_size
)
786 length
= LLDP_NEIGHBOR_LENGTH(n
);
788 if (n
->rindex
+ 2 + length
> n
->raw_size
)
791 *ret
= (uint8_t*) LLDP_NEIGHBOR_RAW(n
) + n
->rindex
;