1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <arpa/inet.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>
#include "sd-event.h"
#include "sd-id128.h"
#include "sd-lldp-tx.h"
#include "alloc-util.h"
#include "ether-addr-util.h"
#include "fd-util.h"
#include "hostname-util.h"
#include "network-common.h"
#include "random-util.h"
#include "socket-util.h"
#include "string-util.h"
#include "time-util.h"
#include "unaligned.h"
#include "web-util.h"
/* The LLDP spec calls this "txFastInit", see 9.2.5.19 */
#define LLDP_FAST_TX_INIT 4U
/* The LLDP spec calls this "msgTxHold", see 9.2.5.6 */
#define LLDP_TX_HOLD 4U
/* The jitter range to add, see 9.2.2. */
#define LLDP_TX_JITTER_USEC (400U * USEC_PER_MSEC)
/* The LLDP spec calls this msgTxInterval, but we subtract half the jitter off it. */
#define LLDP_TX_INTERVAL_USEC (30U * USEC_PER_SEC - LLDP_TX_JITTER_USEC / 2)
/* The LLDP spec calls this msgFastTx, but we subtract half the jitter off it. */
#define LLDP_FAST_TX_INTERVAL_USEC (1U * USEC_PER_SEC - LLDP_TX_JITTER_USEC / 2)
#define LLDP_TX_TTL ((uint16_t) DIV_ROUND_UP(LLDP_TX_INTERVAL_USEC * LLDP_TX_HOLD + 1, USEC_PER_SEC))
static const struct ether_addr lldp_multicast_addr[_SD_LLDP_MULTICAST_MODE_MAX] = {
[SD_LLDP_MULTICAST_MODE_NEAREST_BRIDGE] = {{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e }},
[SD_LLDP_MULTICAST_MODE_NON_TPMR_BRIDGE] = {{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 }},
[SD_LLDP_MULTICAST_MODE_CUSTOMER_BRIDGE] = {{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }},
};
struct sd_lldp_tx {
unsigned n_ref;
int ifindex;
char *ifname;
sd_event *event;
int64_t event_priority;
sd_event_source *timer_event_source;
unsigned fast_tx;
sd_lldp_multicast_mode_t mode;
struct ether_addr hwaddr;
char *port_description;
char *hostname;
char *pretty_hostname;
char *mud_url;
uint16_t supported_capabilities;
uint16_t enabled_capabilities;
};
#define log_lldp_tx_errno(lldp_tx, error, fmt, ...) \
log_interface_prefix_full_errno( \
"LLDP Tx: ", \
sd_lldp_tx, lldp_tx, \
error, fmt, ##__VA_ARGS__)
#define log_lldp_tx(lldp_tx, fmt, ...) \
log_interface_prefix_full_errno_zerook( \
"LLDP Tx: ", \
sd_lldp_tx, lldp_tx, \
0, fmt, ##__VA_ARGS__)
static sd_lldp_tx *lldp_tx_free(sd_lldp_tx *lldp_tx) {
if (!lldp_tx)
return NULL;
sd_lldp_tx_detach_event(lldp_tx);
free(lldp_tx->port_description);
free(lldp_tx->hostname);
free(lldp_tx->pretty_hostname);
free(lldp_tx->mud_url);
free(lldp_tx->ifname);
return mfree(lldp_tx);
}
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_lldp_tx, sd_lldp_tx, lldp_tx_free);
int sd_lldp_tx_new(sd_lldp_tx **ret) {
_cleanup_(sd_lldp_tx_unrefp) sd_lldp_tx *lldp_tx = NULL;
assert_return(ret, -EINVAL);
lldp_tx = new(sd_lldp_tx, 1);
if (!lldp_tx)
return -ENOMEM;
*lldp_tx = (sd_lldp_tx) {
.n_ref = 1,
.mode = _SD_LLDP_MULTICAST_MODE_INVALID,
};
*ret = TAKE_PTR(lldp_tx);
return 0;
}
int sd_lldp_tx_set_ifindex(sd_lldp_tx *lldp_tx, int ifindex) {
assert_return(lldp_tx, -EINVAL);
assert_return(ifindex > 0, -EINVAL);
lldp_tx->ifindex = ifindex;
return 0;
}
int sd_lldp_tx_set_ifname(sd_lldp_tx *lldp_tx, const char *ifname) {
assert_return(lldp_tx, -EINVAL);
assert_return(ifname, -EINVAL);
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
return -EINVAL;
return free_and_strdup(&lldp_tx->ifname, ifname);
}
int sd_lldp_tx_get_ifname(sd_lldp_tx *lldp_tx, const char **ret) {
int r;
assert_return(lldp_tx, -EINVAL);
r = get_ifname(lldp_tx->ifindex, &lldp_tx->ifname);
if (r < 0)
return r;
if (ret)
*ret = lldp_tx->ifname;
return 0;
}
int sd_lldp_tx_set_multicast_mode(sd_lldp_tx *lldp_tx, sd_lldp_multicast_mode_t mode) {
assert_return(lldp_tx, -EINVAL);
assert_return(mode >= 0 && mode < _SD_LLDP_MULTICAST_MODE_MAX, -EINVAL);
lldp_tx->mode = mode;
return 0;
}
int sd_lldp_tx_set_hwaddr(sd_lldp_tx *lldp_tx, const struct ether_addr *hwaddr) {
assert_return(lldp_tx, -EINVAL);
assert_return(!ether_addr_is_null(hwaddr), -EINVAL);
lldp_tx->hwaddr = *hwaddr;
return 0;
}
int sd_lldp_tx_set_capabilities(sd_lldp_tx *lldp_tx, uint16_t supported, uint16_t enabled) {
assert_return(lldp_tx, -EINVAL);
assert_return((enabled & ~supported) == 0, -EINVAL);
lldp_tx->supported_capabilities = supported;
lldp_tx->enabled_capabilities = enabled;
return 0;
}
int sd_lldp_tx_set_port_description(sd_lldp_tx *lldp_tx, const char *port_description) {
assert_return(lldp_tx, -EINVAL);
/* An empty string unset the previously set hostname. */
if (strlen_ptr(port_description) >= 512)
return -EINVAL;
return free_and_strdup(&lldp_tx->port_description, empty_to_null(port_description));
}
int sd_lldp_tx_set_hostname(sd_lldp_tx *lldp_tx, const char *hostname) {
assert_return(lldp_tx, -EINVAL);
/* An empty string unset the previously set hostname. */
if (!isempty(hostname)) {
assert_cc(HOST_NAME_MAX < 512);
if (!hostname_is_valid(hostname, 0))
return -EINVAL;
}
return free_and_strdup(&lldp_tx->hostname, empty_to_null(hostname));
}
int sd_lldp_tx_set_pretty_hostname(sd_lldp_tx *lldp_tx, const char *pretty_hostname) {
assert_return(lldp_tx, -EINVAL);
/* An empty string unset the previously set hostname. */
if (strlen_ptr(pretty_hostname) >= 512)
return -EINVAL;
return free_and_strdup(&lldp_tx->pretty_hostname, empty_to_null(pretty_hostname));
}
int sd_lldp_tx_set_mud_url(sd_lldp_tx *lldp_tx, const char *mud_url) {
assert_return(lldp_tx, -EINVAL);
/* An empty string unset the previously set hostname. */
if (!isempty(mud_url)) {
/* Unless the maximum length of each value is 511, the MUD url must be smaller than 256.
* See RFC 8520. */
if (strlen(mud_url) >= 256)
return -EINVAL;
if (!http_url_is_valid(mud_url))
return -EINVAL;
}
return free_and_strdup(&lldp_tx->mud_url, empty_to_null(mud_url));
}
static size_t lldp_tx_calculate_maximum_packet_size(sd_lldp_tx *lldp_tx, const char *hostname, const char *pretty_hostname) {
assert(lldp_tx);
assert(lldp_tx->ifindex > 0);
return sizeof(struct ether_header) +
/* Chassis ID */
2 + 1 + (SD_ID128_STRING_MAX - 1) +
/* Port ID */
2 + 1 + strlen_ptr(lldp_tx->ifname) +
/* TTL */
2 + 2 +
/* Port description */
2 + strlen_ptr(lldp_tx->port_description) +
/* System name */
2 + strlen_ptr(hostname) +
/* System description */
2 + strlen_ptr(pretty_hostname) +
/* MUD URL */
2 + sizeof(SD_LLDP_OUI_IANA_MUD) + strlen_ptr(lldp_tx->mud_url) +
/* System Capabilities */
2 + 4 +
/* End */
2;
}
static int packet_append_tlv_header(uint8_t *packet, size_t packet_size, size_t *offset, uint8_t type, size_t data_len) {
assert(packet);
assert(offset);
/*
* +--------+--------+--------------
* |TLV Type| len | value
* |(7 bits)|(9 bits)|(0-511 octets)
* +--------+--------+--------------
* where:
*
* len = indicates the length of value
*/
/* The type field is 7-bits. */
if (type >= 128)
return -EINVAL;
/* The data length field is 9-bits. */
if (data_len >= 512)
return -EINVAL;
if (packet_size < 2 + data_len)
return -ENOBUFS;
if (*offset > packet_size - 2 - data_len)
return -ENOBUFS;
packet[(*offset)++] = (type << 1) | !!(data_len >> 8);
packet[(*offset)++] = data_len & (size_t) UINT8_MAX;
return 0;
}
static int packet_append_prefixed_string(
uint8_t *packet,
size_t packet_size,
size_t *offset,
uint8_t type,
size_t prefix_len,
const void *prefix,
const char *str) {
size_t len;
int r;
assert(packet);
assert(offset);
assert(prefix_len == 0 || prefix);
if (isempty(str))
return 0;
len = strlen(str);
/* Check for overflow */
if (len > SIZE_MAX - prefix_len)
return -ENOBUFS;
r = packet_append_tlv_header(packet, packet_size, offset, type, prefix_len + len);
if (r < 0)
return r;
memcpy_safe(packet + *offset, prefix, prefix_len);
*offset += prefix_len;
memcpy(packet + *offset, str, len);
*offset += len;
return 0;
}
static int packet_append_string(
uint8_t *packet,
size_t packet_size,
size_t *offset,
uint8_t type,
const char *str) {
return packet_append_prefixed_string(packet, packet_size, offset, type, 0, NULL, str);
}
static int lldp_tx_create_packet(sd_lldp_tx *lldp_tx, size_t *ret_packet_size, uint8_t **ret_packet) {
_cleanup_free_ char *hostname = NULL, *pretty_hostname = NULL;
_cleanup_free_ uint8_t *packet = NULL;
struct ether_header *header;
size_t packet_size, offset;
sd_id128_t machine_id;
int r;
assert(lldp_tx);
assert(lldp_tx->ifindex > 0);
assert(ret_packet_size);
assert(ret_packet);
/* If ifname is not set yet, set ifname from ifindex. */
r = sd_lldp_tx_get_ifname(lldp_tx, NULL);
if (r < 0)
return r;
r = sd_id128_get_machine(&machine_id);
if (r < 0)
return r;
if (!lldp_tx->hostname)
(void) gethostname_strict(&hostname);
if (!lldp_tx->pretty_hostname)
(void) get_pretty_hostname(&pretty_hostname);
packet_size = lldp_tx_calculate_maximum_packet_size(lldp_tx,
lldp_tx->hostname ?: hostname,
lldp_tx->pretty_hostname ?: pretty_hostname);
packet = new(uint8_t, packet_size);
if (!packet)
return -ENOMEM;
header = (struct ether_header*) packet;
header->ether_type = htobe16(ETHERTYPE_LLDP);
memcpy(header->ether_dhost, lldp_multicast_addr + lldp_tx->mode, ETH_ALEN);
memcpy(header->ether_shost, &lldp_tx->hwaddr, ETH_ALEN);
offset = sizeof(struct ether_header);
/* The three mandatory TLVs must appear first, in this specific order:
* 1. Chassis ID
* 2. Port ID
* 3. Time To Live
*/
r = packet_append_prefixed_string(packet, packet_size, &offset, SD_LLDP_TYPE_CHASSIS_ID,
1, (const uint8_t[]) { SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED },
SD_ID128_TO_STRING(machine_id));
if (r < 0)
return r;
r = packet_append_prefixed_string(packet, packet_size, &offset, SD_LLDP_TYPE_PORT_ID,
1, (const uint8_t[]) { SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME },
lldp_tx->ifname);
if (r < 0)
return r;
r = packet_append_tlv_header(packet, packet_size, &offset, SD_LLDP_TYPE_TTL, 2);
if (r < 0)
return r;
unaligned_write_be16(packet + offset, LLDP_TX_TTL);
offset += 2;
/* Optional TLVs follow, in no specific order: */
r = packet_append_string(packet, packet_size, &offset, SD_LLDP_TYPE_PORT_DESCRIPTION,
lldp_tx->port_description);
if (r < 0)
return r;
r = packet_append_string(packet, packet_size, &offset, SD_LLDP_TYPE_SYSTEM_NAME,
lldp_tx->hostname ?: hostname);
if (r < 0)
return r;
r = packet_append_string(packet, packet_size, &offset, SD_LLDP_TYPE_SYSTEM_DESCRIPTION,
lldp_tx->pretty_hostname ?: pretty_hostname);
if (r < 0)
return r;
/* See section 12 of RFC 8520.
* +--------+--------+----------+---------+--------------
* |TLV Type| len | OUI |subtype | MUDString
* | =127 | |= 00 00 5E| = 1 |
* |(7 bits)|(9 bits)|(3 octets)|(1 octet)|(1-255 octets)
* +--------+--------+----------+---------+--------------
* where:
*
* o TLV Type = 127 indicates a vendor-specific TLV
* o len = indicates the TLV string length
* o OUI = 00 00 5E is the organizationally unique identifier of IANA
* o subtype = 1 (as assigned by IANA for the MUDstring)
* o MUDstring = the length MUST NOT exceed 255 octets
*/
r = packet_append_prefixed_string(packet, packet_size, &offset, SD_LLDP_TYPE_PRIVATE,
sizeof(SD_LLDP_OUI_IANA_MUD), SD_LLDP_OUI_IANA_MUD,
lldp_tx->mud_url);
if (r < 0)
return r;
r = packet_append_tlv_header(packet, packet_size, &offset, SD_LLDP_TYPE_SYSTEM_CAPABILITIES, 4);
if (r < 0)
return r;
unaligned_write_be16(packet + offset, lldp_tx->supported_capabilities);
offset += 2;
unaligned_write_be16(packet + offset, lldp_tx->enabled_capabilities);
offset += 2;
r = packet_append_tlv_header(packet, packet_size, &offset, SD_LLDP_TYPE_END, 0);
if (r < 0)
return r;
*ret_packet_size = offset;
*ret_packet = TAKE_PTR(packet);
return 0;
}
static int lldp_tx_send_packet(sd_lldp_tx *lldp_tx, size_t packet_size, const uint8_t *packet) {
_cleanup_close_ int fd = -1;
union sockaddr_union sa;
ssize_t l;
assert(lldp_tx);
assert(lldp_tx->ifindex > 0);
assert(packet_size > sizeof(struct ether_header));
assert(packet);
sa = (union sockaddr_union) {
.ll.sll_family = AF_PACKET,
.ll.sll_protocol = htobe16(ETHERTYPE_LLDP),
.ll.sll_ifindex = lldp_tx->ifindex,
.ll.sll_halen = ETH_ALEN,
};
memcpy(sa.ll.sll_addr, lldp_multicast_addr + lldp_tx->mode, ETH_ALEN);
fd = socket(AF_PACKET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
if (fd < 0)
return -errno;
l = sendto(fd, packet, packet_size, MSG_NOSIGNAL, &sa.sa, sizeof(sa.ll));
if (l < 0)
return -errno;
if ((size_t) l != packet_size)
return -EIO;
return 0;
}
static int lldp_tx_send(sd_lldp_tx *lldp_tx) {
_cleanup_free_ uint8_t *packet = NULL;
size_t packet_size = 0; /* avoid false maybe-uninitialized warning */
int r;
assert(lldp_tx);
r = lldp_tx_create_packet(lldp_tx, &packet_size, &packet);
if (r < 0)
return r;
return lldp_tx_send_packet(lldp_tx, packet_size, packet);
}
int sd_lldp_tx_attach_event(sd_lldp_tx *lldp_tx, sd_event *event, int64_t priority) {
int r;
assert_return(lldp_tx, -EINVAL);
assert_return(!lldp_tx->event, -EBUSY);
if (event)
lldp_tx->event = sd_event_ref(event);
else {
r = sd_event_default(&lldp_tx->event);
if (r < 0)
return r;
}
lldp_tx->event_priority = priority;
return 0;
}
int sd_lldp_tx_detach_event(sd_lldp_tx *lldp_tx) {
assert_return(lldp_tx, -EINVAL);
lldp_tx->timer_event_source = sd_event_source_disable_unref(lldp_tx->timer_event_source);
lldp_tx->event = sd_event_unref(lldp_tx->event);
return 0;
}
static usec_t lldp_tx_get_delay(sd_lldp_tx *lldp_tx) {
assert(lldp_tx);
return usec_add(lldp_tx->fast_tx > 0 ? LLDP_FAST_TX_INTERVAL_USEC : LLDP_TX_INTERVAL_USEC,
(usec_t) random_u64() % LLDP_TX_JITTER_USEC);
}
static int lldp_tx_reset_timer(sd_lldp_tx *lldp_tx) {
usec_t delay;
int r;
assert(lldp_tx);
assert(lldp_tx->timer_event_source);
delay = lldp_tx_get_delay(lldp_tx);
r = sd_event_source_set_time_relative(lldp_tx->timer_event_source, delay);
if (r < 0)
return r;
return sd_event_source_set_enabled(lldp_tx->timer_event_source, SD_EVENT_ONESHOT);
}
static int on_timer_event(sd_event_source *s, uint64_t usec, void *userdata) {
sd_lldp_tx *lldp_tx = ASSERT_PTR(userdata);
int r;
r = lldp_tx_send(lldp_tx);
if (r < 0)
log_lldp_tx_errno(lldp_tx, r, "Failed to send packet, ignoring: %m");
if (lldp_tx->fast_tx > 0)
lldp_tx->fast_tx--;
r = lldp_tx_reset_timer(lldp_tx);
if (r < 0)
log_lldp_tx_errno(lldp_tx, r, "Failed to reset timer: %m");
return 0;
}
int sd_lldp_tx_is_running(sd_lldp_tx *lldp_tx) {
int enabled;
if (!lldp_tx)
return 0;
if (!lldp_tx->timer_event_source)
return 0;
if (sd_event_source_get_enabled(lldp_tx->timer_event_source, &enabled) < 0)
return 0;
return enabled == SD_EVENT_ONESHOT;
}
int sd_lldp_tx_stop(sd_lldp_tx *lldp_tx) {
if (!lldp_tx)
return 0;
if (!lldp_tx->timer_event_source)
return 0;
(void) sd_event_source_set_enabled(lldp_tx->timer_event_source, SD_EVENT_OFF);
return 1;
}
int sd_lldp_tx_start(sd_lldp_tx *lldp_tx) {
usec_t delay;
int r;
assert_return(lldp_tx, -EINVAL);
assert_return(lldp_tx->event, -EINVAL);
assert_return(lldp_tx->ifindex > 0, -EINVAL);
assert_return(lldp_tx->mode >= 0 && lldp_tx->mode < _SD_LLDP_MULTICAST_MODE_MAX, -EINVAL);
assert_return(!ether_addr_is_null(&lldp_tx->hwaddr), -EINVAL);
if (sd_lldp_tx_is_running(lldp_tx))
return 0;
lldp_tx->fast_tx = LLDP_FAST_TX_INIT;
if (lldp_tx->timer_event_source) {
r = lldp_tx_reset_timer(lldp_tx);
if (r < 0)
return log_lldp_tx_errno(lldp_tx, r, "Failed to re-enable timer: %m");
return 0;
}
delay = lldp_tx_get_delay(lldp_tx);
r = sd_event_add_time_relative(lldp_tx->event, &lldp_tx->timer_event_source,
CLOCK_BOOTTIME, delay, 0,
on_timer_event, lldp_tx);
if (r < 0)
return r;
(void) sd_event_source_set_description(lldp_tx->timer_event_source, "lldp-tx-timer");
(void) sd_event_source_set_priority(lldp_tx->timer_event_source, lldp_tx->event_priority);
return 0;
}
|