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/>.
24 #include "alloc-util.h"
27 #include "hostname-util.h"
28 #include "random-util.h"
29 #include "socket-util.h"
30 #include "string-util.h"
31 #include "unaligned.h"
34 #include "networkd-lldp-tx.h"
36 #define LLDP_MULTICAST_ADDR { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e }
38 /* The LLDP spec calls this "txFastInit", see 9.2.5.19 */
39 #define LLDP_TX_FAST_INIT 4U
41 /* The LLDP spec calls this "msgTxHold", see 9.2.5.6 */
42 #define LLDP_TX_HOLD 4U
44 /* The jitter range to add, see 9.2.2. */
45 #define LLDP_JITTER_USEC (400U * USEC_PER_MSEC)
47 /* The LLDP spec calls this msgTxInterval, but we subtract half the jitter off it. */
48 #define LLDP_TX_INTERVAL_USEC (30U * USEC_PER_SEC - LLDP_JITTER_USEC / 2)
50 /* The LLDP spec calls this msgFastTx, but we subtract half the jitter off it. */
51 #define LLDP_FAST_TX_USEC (1U * USEC_PER_SEC - LLDP_JITTER_USEC / 2)
53 static int lldp_write_tlv_header(uint8_t **p
, uint8_t id
, size_t sz
) {
61 (*p
)[0] = (id
<< 1) | !!(sz
& 256);
68 static int lldp_make_packet(
69 const struct ether_addr
*hwaddr
,
70 const char *machine_id
,
73 const char *port_description
,
75 const char *pretty_hostname
,
76 uint16_t system_capabilities
,
77 uint16_t enabled_capabilities
,
78 void **ret
, size_t *sz
) {
80 size_t machine_id_length
, ifname_length
, port_description_length
= 0, hostname_length
= 0, pretty_hostname_length
= 0;
81 _cleanup_free_
void *packet
= NULL
;
82 struct ether_header
*h
;
93 machine_id_length
= strlen(machine_id
);
94 ifname_length
= strlen(ifname
);
97 port_description_length
= strlen(port_description
);
100 hostname_length
= strlen(hostname
);
103 pretty_hostname_length
= strlen(pretty_hostname
);
105 l
= sizeof(struct ether_header
) +
107 2 + 1 + machine_id_length
+
109 2 + 1 + ifname_length
+
112 /* System Capabilities */
117 /* Port Description */
118 if (port_description
)
119 l
+= 2 + port_description_length
;
123 l
+= 2 + hostname_length
;
125 /* System Description */
127 l
+= 2 + pretty_hostname_length
;
133 h
= (struct ether_header
*) packet
;
134 h
->ether_type
= htobe16(ETHERTYPE_LLDP
);
135 memcpy(h
->ether_dhost
, &(struct ether_addr
) { LLDP_MULTICAST_ADDR
}, ETH_ALEN
);
136 memcpy(h
->ether_shost
, hwaddr
, ETH_ALEN
);
138 p
= (uint8_t*) packet
+ sizeof(struct ether_header
);
140 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_CHASSIS_ID
, 1 + machine_id_length
);
143 *(p
++) = SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED
;
144 p
= mempcpy(p
, machine_id
, machine_id_length
);
146 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_PORT_ID
, 1 + ifname_length
);
149 *(p
++) = SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME
;
150 p
= mempcpy(p
, ifname
, ifname_length
);
152 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_TTL
, 2);
155 unaligned_write_be16(p
, ttl
);
158 if (port_description
) {
159 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_PORT_DESCRIPTION
, port_description_length
);
162 p
= mempcpy(p
, port_description
, port_description_length
);
166 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_SYSTEM_NAME
, hostname_length
);
169 p
= mempcpy(p
, hostname
, hostname_length
);
172 if (pretty_hostname
) {
173 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_SYSTEM_DESCRIPTION
, pretty_hostname_length
);
176 p
= mempcpy(p
, pretty_hostname
, pretty_hostname_length
);
179 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_SYSTEM_CAPABILITIES
, 4);
182 unaligned_write_be16(p
, system_capabilities
);
184 unaligned_write_be16(p
, enabled_capabilities
);
187 r
= lldp_write_tlv_header(&p
, SD_LLDP_TYPE_END
, 0);
191 assert(p
== (uint8_t*) packet
+ l
);
200 static int lldp_send_packet(int ifindex
, const void *packet
, size_t packet_size
) {
202 union sockaddr_union sa
= {
203 .ll
.sll_family
= AF_PACKET
,
204 .ll
.sll_protocol
= htobe16(ETHERTYPE_LLDP
),
205 .ll
.sll_ifindex
= ifindex
,
206 .ll
.sll_halen
= ETH_ALEN
,
207 .ll
.sll_addr
= LLDP_MULTICAST_ADDR
,
210 _cleanup_close_
int fd
= -1;
214 assert(packet
|| packet_size
<= 0);
216 fd
= socket(PF_PACKET
, SOCK_RAW
|SOCK_CLOEXEC
, IPPROTO_RAW
);
220 l
= sendto(fd
, packet
, packet_size
, MSG_NOSIGNAL
, &sa
.sa
, sizeof(sa
.ll
));
224 if ((size_t) l
!= packet_size
)
230 static int link_send_lldp(Link
*link
) {
231 char machine_id_string
[SD_ID128_STRING_MAX
];
232 _cleanup_free_
char *hostname
= NULL
, *pretty_hostname
= NULL
;
233 _cleanup_free_
void *packet
= NULL
;
234 size_t packet_size
= 0;
235 sd_id128_t machine_id
;
240 r
= sd_id128_get_machine(&machine_id
);
244 (void) gethostname_strict(&hostname
);
245 (void) parse_env_file("/etc/machine-info", NEWLINE
, "PRETTY_HOSTNAME", &pretty_hostname
, NULL
);
247 assert_cc(LLDP_TX_INTERVAL_USEC
* LLDP_TX_HOLD
+ 1 <= (UINT16_MAX
- 1) * USEC_PER_SEC
);
248 ttl
= DIV_ROUND_UP(LLDP_TX_INTERVAL_USEC
* LLDP_TX_HOLD
+ 1, USEC_PER_SEC
);
250 caps
= (link
->network
&& link
->network
->ip_forward
!= ADDRESS_FAMILY_NO
) ?
251 SD_LLDP_SYSTEM_CAPABILITIES_ROUTER
:
252 SD_LLDP_SYSTEM_CAPABILITIES_STATION
;
254 r
= lldp_make_packet(&link
->mac
,
255 sd_id128_to_string(machine_id
, machine_id_string
),
258 link
->network
? link
->network
->description
: NULL
,
261 SD_LLDP_SYSTEM_CAPABILITIES_STATION
|SD_LLDP_SYSTEM_CAPABILITIES_BRIDGE
|SD_LLDP_SYSTEM_CAPABILITIES_ROUTER
,
263 &packet
, &packet_size
);
267 return lldp_send_packet(link
->ifindex
, packet
, packet_size
);
270 static int on_lldp_timer(sd_event_source
*s
, usec_t t
, void *userdata
) {
271 Link
*link
= userdata
;
272 usec_t current
, delay
, next
;
278 log_link_debug(link
, "Sending LLDP packet...");
280 r
= link_send_lldp(link
);
282 log_link_debug_errno(link
, r
, "Failed to send LLDP packet, ignoring: %m");
284 if (link
->lldp_tx_fast
> 0)
285 link
->lldp_tx_fast
--;
287 assert_se(sd_event_now(sd_event_source_get_event(s
), clock_boottime_or_monotonic(), ¤t
) >= 0);
289 delay
= link
->lldp_tx_fast
> 0 ? LLDP_FAST_TX_USEC
: LLDP_TX_INTERVAL_USEC
;
290 next
= usec_add(usec_add(current
, delay
), (usec_t
) random_u64() % LLDP_JITTER_USEC
);
292 r
= sd_event_source_set_time(s
, next
);
294 return log_link_error_errno(link
, r
, "Failed to restart LLDP timer: %m");
296 r
= sd_event_source_set_enabled(s
, SD_EVENT_ONESHOT
);
298 return log_link_error_errno(link
, r
, "Failed to enable LLDP timer: %m");
303 int link_lldp_tx_start(Link
*link
) {
309 /* Starts the LLDP transmission in "fast" mode. If it is already started, turns "fast" mode back on again. */
311 link
->lldp_tx_fast
= LLDP_TX_FAST_INIT
;
313 next
= usec_add(usec_add(now(clock_boottime_or_monotonic()), LLDP_FAST_TX_USEC
),
314 (usec_t
) random_u64() % LLDP_JITTER_USEC
);
316 if (link
->lldp_tx_event_source
) {
319 /* Lower the timeout, maybe */
320 r
= sd_event_source_get_time(link
->lldp_tx_event_source
, &old
);
327 return sd_event_source_set_time(link
->lldp_tx_event_source
, next
);
329 r
= sd_event_add_time(
330 link
->manager
->event
,
331 &link
->lldp_tx_event_source
,
332 clock_boottime_or_monotonic(),
340 (void) sd_event_source_set_description(link
->lldp_tx_event_source
, "lldp-tx");
346 void link_lldp_tx_stop(Link
*link
) {
349 link
->lldp_tx_event_source
= sd_event_source_unref(link
->lldp_tx_event_source
);