-
-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathlldpd.c
More file actions
1666 lines (1532 loc) · 48.1 KB
/
Copy pathlldpd.c
File metadata and controls
1666 lines (1532 loc) · 48.1 KB
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: c; c-file-style: "openbsd" -*- */
/*
* Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "lldpd.h"
#include "trace.h"
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <libgen.h>
#include <assert.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <pwd.h>
#include <grp.h>
static void usage(void);
static struct protocol protos[] =
{
{ LLDPD_MODE_LLDP, 1, "LLDP", 'l', lldp_send, lldp_decode, NULL,
LLDP_MULTICAST_ADDR },
#ifdef ENABLE_CDP
{ LLDPD_MODE_CDPV1, 0, "CDPv1", 'c', cdpv1_send, cdp_decode, cdpv1_guess,
CDP_MULTICAST_ADDR },
{ LLDPD_MODE_CDPV2, 0, "CDPv2", 'c', cdpv2_send, cdp_decode, cdpv2_guess,
CDP_MULTICAST_ADDR },
#endif
#ifdef ENABLE_SONMP
{ LLDPD_MODE_SONMP, 0, "SONMP", 's', sonmp_send, sonmp_decode, NULL,
SONMP_MULTICAST_ADDR },
#endif
#ifdef ENABLE_EDP
{ LLDPD_MODE_EDP, 0, "EDP", 'e', edp_send, edp_decode, NULL,
EDP_MULTICAST_ADDR },
#endif
#ifdef ENABLE_FDP
{ LLDPD_MODE_FDP, 0, "FDP", 'f', fdp_send, cdp_decode, NULL,
FDP_MULTICAST_ADDR },
#endif
{ 0, 0, "any", ' ', NULL, NULL, NULL,
{0,0,0,0,0,0} }
};
static char **saved_argv;
#ifdef HAVE___PROGNAME
extern const char *__progname;
#else
# define __progname "lldpd"
#endif
static void
usage(void)
{
fprintf(stderr, "Usage: %s [OPTIONS ...]\n", __progname);
fprintf(stderr, "Version: %s\n", PACKAGE_STRING);
fprintf(stderr, "\n");
fprintf(stderr, "-d Do not daemonize.\n");
fprintf(stderr, "-r Receive-only mode\n");
fprintf(stderr, "-i Disable LLDP-MED inventory TLV transmission.\n");
fprintf(stderr, "-k Disable advertising of kernel release, version, machine.\n");
fprintf(stderr, "-S descr Override the default system description.\n");
fprintf(stderr, "-P name Override the default hardware platform.\n");
fprintf(stderr, "-m IP Specify the IPv4 management addresses of this system.\n");
fprintf(stderr, "-u file Specify the Unix-domain socket used for communication with lldpctl(8).\n");
fprintf(stderr, "-H mode Specify the behaviour when detecting multiple neighbors.\n");
fprintf(stderr, "-I iface Limit interfaces to use.\n");
#ifdef ENABLE_LLDPMED
fprintf(stderr, "-M class Enable emission of LLDP-MED frame. 'class' should be one of:\n");
fprintf(stderr, " 1 Generic Endpoint (Class I)\n");
fprintf(stderr, " 2 Media Endpoint (Class II)\n");
fprintf(stderr, " 3 Communication Device Endpoints (Class III)\n");
fprintf(stderr, " 4 Network Connectivity Device\n");
#endif
#ifdef USE_SNMP
fprintf(stderr, "-x Enable SNMP subagent.\n");
#endif
fprintf(stderr, "\n");
#if defined ENABLE_CDP || defined ENABLE_EDP || defined ENABLE_FDP || defined ENABLE_SONMP
fprintf(stderr, "Additional protocol support.\n");
#ifdef ENABLE_CDP
fprintf(stderr, "-c Enable the support of CDP protocol. (Cisco)\n");
#endif
#ifdef ENABLE_EDP
fprintf(stderr, "-e Enable the support of EDP protocol. (Extreme)\n");
#endif
#ifdef ENABLE_FDP
fprintf(stderr, "-f Enable the support of FDP protocol. (Foundry)\n");
#endif
#ifdef ENABLE_SONMP
fprintf(stderr, "-s Enable the support of SONMP protocol. (Nortel)\n");
#endif
fprintf(stderr, "\n");
#endif
fprintf(stderr, "see manual page lldpd(8) for more information\n");
exit(1);
}
struct lldpd_hardware *
lldpd_get_hardware(struct lldpd *cfg, char *name, int index, struct lldpd_ops *ops)
{
struct lldpd_hardware *hardware;
TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) {
if ((strcmp(hardware->h_ifname, name) == 0) &&
(hardware->h_ifindex == index) &&
((!ops) || (ops == hardware->h_ops)))
break;
}
return hardware;
}
struct lldpd_hardware *
lldpd_alloc_hardware(struct lldpd *cfg, char *name, int index)
{
struct lldpd_hardware *hardware;
log_debug("alloc", "allocate a new local port (%s)", name);
if ((hardware = (struct lldpd_hardware *)
calloc(1, sizeof(struct lldpd_hardware))) == NULL)
return NULL;
hardware->h_cfg = cfg;
strlcpy(hardware->h_ifname, name, sizeof(hardware->h_ifname));
hardware->h_ifindex = index;
hardware->h_lport.p_chassis = LOCAL_CHASSIS(cfg);
hardware->h_lport.p_chassis->c_refcount++;
TAILQ_INIT(&hardware->h_rports);
#ifdef ENABLE_LLDPMED
if (LOCAL_CHASSIS(cfg)->c_med_cap_available) {
hardware->h_lport.p_med_cap_enabled = LLDP_MED_CAP_CAP;
if (!cfg->g_config.c_noinventory)
hardware->h_lport.p_med_cap_enabled |= LLDP_MED_CAP_IV;
}
#endif
#ifdef ENABLE_DOT1
TAILQ_INIT(&hardware->h_lport.p_vlans);
TAILQ_INIT(&hardware->h_lport.p_ppvids);
TAILQ_INIT(&hardware->h_lport.p_pids);
#endif
levent_hardware_init(hardware);
return hardware;
}
struct lldpd_mgmt *
lldpd_alloc_mgmt(int family, void *addrptr, size_t addrsize, u_int32_t iface)
{
struct lldpd_mgmt *mgmt;
log_debug("alloc", "allocate a new management address (family: %d)", family);
if (family <= LLDPD_AF_UNSPEC || family >= LLDPD_AF_LAST) {
errno = EAFNOSUPPORT;
return NULL;
}
if (addrsize > LLDPD_MGMT_MAXADDRSIZE) {
errno = EOVERFLOW;
return NULL;
}
mgmt = calloc(1, sizeof(struct lldpd_mgmt));
if (mgmt == NULL) {
errno = ENOMEM;
return NULL;
}
mgmt->m_family = family;
assert(addrsize <= LLDPD_MGMT_MAXADDRSIZE);
memcpy(&mgmt->m_addr, addrptr, addrsize);
mgmt->m_addrsize = addrsize;
mgmt->m_iface = iface;
return mgmt;
}
void
lldpd_hardware_cleanup(struct lldpd *cfg, struct lldpd_hardware *hardware)
{
log_debug("alloc", "cleanup hardware port %s", hardware->h_ifname);
lldpd_port_cleanup(&hardware->h_lport, 1);
if (hardware->h_ops && hardware->h_ops->cleanup)
hardware->h_ops->cleanup(cfg, hardware);
levent_hardware_release(hardware);
free(hardware);
}
static void
lldpd_display_neighbors(struct lldpd *cfg)
{
if (!cfg->g_config.c_set_ifdescr) return;
struct lldpd_hardware *hardware;
TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) {
struct lldpd_port *port;
char *description;
const char *neighbor = NULL;
unsigned neighbors = 0;
TAILQ_FOREACH(port, &hardware->h_rports, p_entries) {
if (SMART_HIDDEN(port)) continue;
neighbors++;
neighbor = port->p_chassis->c_name;
}
if (neighbors == 0)
priv_iface_description(hardware->h_ifname,
"");
else if (neighbors == 1 && neighbor && *neighbor != '\0') {
if (asprintf(&description, "%s",
neighbor) != -1) {
priv_iface_description(hardware->h_ifname, description);
free(description);
}
} else {
if (asprintf(&description, "%d neighbor%s",
neighbors, (neighbors > 1)?"s":"") != -1) {
priv_iface_description(hardware->h_ifname,
description);
free(description);
}
}
}
}
static void
lldpd_count_neighbors(struct lldpd *cfg)
{
#if HAVE_SETPROCTITLE
struct lldpd_chassis *chassis;
const char *neighbor;
unsigned neighbors = 0;
TAILQ_FOREACH(chassis, &cfg->g_chassis, c_entries) {
neighbors++;
neighbor = chassis->c_name;
}
neighbors--;
if (neighbors == 0)
setproctitle("no neighbor");
else if (neighbors == 1 && neighbor && *neighbor != '\0')
setproctitle("connected to %s", neighbor);
else
setproctitle("%d neighbor%s", neighbors,
(neighbors > 1)?"s":"");
#endif
lldpd_display_neighbors(cfg);
}
static void
notify_clients_deletion(struct lldpd_hardware *hardware,
struct lldpd_port *rport)
{
TRACE(LLDPD_NEIGHBOR_DELETE(hardware->h_ifname,
rport->p_chassis->c_name,
rport->p_descr));
levent_ctl_notify(hardware->h_ifname, NEIGHBOR_CHANGE_DELETED,
rport);
#ifdef USE_SNMP
agent_notify(hardware, NEIGHBOR_CHANGE_DELETED, rport);
#endif
}
static void
lldpd_reset_timer(struct lldpd *cfg)
{
/* Reset timer for ports that have been changed. */
struct lldpd_hardware *hardware;
TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) {
/* We need to compute a checksum of the local port. To do this,
* we zero out fields that are not significant, marshal the
* port, compute the checksum, then restore. */
struct lldpd_port *port = &hardware->h_lport;
u_int16_t cksum;
u_int8_t *output = NULL;
size_t output_len;
char save[offsetof(struct lldpd_port, p_id_subtype)];
memcpy(save, port, sizeof(save));
/* coverity[suspicious_sizeof]
We intentionally partially memset port */
memset(port, 0, sizeof(save));
output_len = lldpd_port_serialize(port, (void**)&output);
memcpy(port, save, sizeof(save));
if (output_len == -1) {
log_warnx("localchassis",
"unable to serialize local port %s to check for differences",
hardware->h_ifname);
continue;
}
cksum = frame_checksum(output, output_len, 0);
free(output);
if (cksum != hardware->h_lport_cksum) {
log_debug("localchassis",
"change detected for port %s, resetting its timer",
hardware->h_ifname);
hardware->h_lport_cksum = cksum;
levent_schedule_pdu(hardware);
} else {
log_debug("localchassis",
"no change detected for port %s",
hardware->h_ifname);
}
}
}
void
lldpd_cleanup(struct lldpd *cfg)
{
struct lldpd_hardware *hardware, *hardware_next;
struct lldpd_chassis *chassis, *chassis_next;
log_debug("localchassis", "cleanup all ports");
for (hardware = TAILQ_FIRST(&cfg->g_hardware); hardware != NULL;
hardware = hardware_next) {
hardware_next = TAILQ_NEXT(hardware, h_entries);
if (!hardware->h_flags) {
TRACE(LLDPD_INTERFACES_DELETE(hardware->h_ifname));
TAILQ_REMOVE(&cfg->g_hardware, hardware, h_entries);
lldpd_remote_cleanup(hardware, notify_clients_deletion, 1);
lldpd_hardware_cleanup(cfg, hardware);
} else
lldpd_remote_cleanup(hardware, notify_clients_deletion, 0);
}
log_debug("localchassis", "cleanup all chassis");
for (chassis = TAILQ_FIRST(&cfg->g_chassis); chassis;
chassis = chassis_next) {
chassis_next = TAILQ_NEXT(chassis, c_entries);
if (chassis->c_refcount == 0) {
TAILQ_REMOVE(&cfg->g_chassis, chassis, c_entries);
lldpd_chassis_cleanup(chassis, 1);
}
}
lldpd_count_neighbors(cfg);
levent_schedule_cleanup(cfg);
}
/* Update chassis `ochassis' with values from `chassis'. The later one is not
expected to be part of a list! It will also be wiped from memory. */
static void
lldpd_move_chassis(struct lldpd_chassis *ochassis,
struct lldpd_chassis *chassis) {
struct lldpd_mgmt *mgmt, *mgmt_next;
/* We want to keep refcount, index and list stuff from the current
* chassis */
TAILQ_ENTRY(lldpd_chassis) entries;
int refcount = ochassis->c_refcount;
int index = ochassis->c_index;
memcpy(&entries, &ochassis->c_entries,
sizeof(entries));
lldpd_chassis_cleanup(ochassis, 0);
/* Make the copy. */
/* WARNING: this is a kludgy hack, we need in-place copy and cannot use
* marshaling. */
memcpy(ochassis, chassis, sizeof(struct lldpd_chassis));
TAILQ_INIT(&ochassis->c_mgmt);
/* Copy of management addresses */
for (mgmt = TAILQ_FIRST(&chassis->c_mgmt);
mgmt != NULL;
mgmt = mgmt_next) {
mgmt_next = TAILQ_NEXT(mgmt, m_entries);
TAILQ_REMOVE(&chassis->c_mgmt, mgmt, m_entries);
TAILQ_INSERT_TAIL(&ochassis->c_mgmt, mgmt, m_entries);
}
/* Restore saved values */
ochassis->c_refcount = refcount;
ochassis->c_index = index;
memcpy(&ochassis->c_entries, &entries, sizeof(entries));
/* Get rid of the new chassis */
free(chassis);
}
static int
lldpd_guess_type(struct lldpd *cfg, char *frame, int s)
{
int i;
if (s < ETHER_ADDR_LEN)
return -1;
for (i=0; cfg->g_protocols[i].mode != 0; i++) {
if (!cfg->g_protocols[i].enabled)
continue;
if (cfg->g_protocols[i].guess == NULL) {
if (memcmp(frame, cfg->g_protocols[i].mac, ETHER_ADDR_LEN) == 0) {
log_debug("decode", "guessed protocol is %s (from MAC address)",
cfg->g_protocols[i].name);
return cfg->g_protocols[i].mode;
}
} else {
if (cfg->g_protocols[i].guess(frame, s)) {
log_debug("decode", "guessed protocol is %s (from detector function)",
cfg->g_protocols[i].name);
return cfg->g_protocols[i].mode;
}
}
}
return -1;
}
static void
lldpd_decode(struct lldpd *cfg, char *frame, int s,
struct lldpd_hardware *hardware)
{
int i;
struct lldpd_chassis *chassis, *ochassis = NULL;
struct lldpd_port *port, *oport = NULL, *aport;
int guess = LLDPD_MODE_LLDP;
log_debug("decode", "decode a received frame on %s",
hardware->h_ifname);
if (s < sizeof(struct ether_header) + 4)
/* Too short, just discard it */
return;
/* Decapsulate VLAN frames */
struct ether_header eheader;
memcpy(&eheader, frame, sizeof(struct ether_header));
if (eheader.ether_type == htons(ETHERTYPE_VLAN)) {
/* VLAN decapsulation means to shift 4 bytes left the frame from
* offset 2*ETHER_ADDR_LEN */
memmove(frame + 2*ETHER_ADDR_LEN, frame + 2*ETHER_ADDR_LEN + 4, s - 2*ETHER_ADDR_LEN);
s -= 4;
}
TAILQ_FOREACH(oport, &hardware->h_rports, p_entries) {
if ((oport->p_lastframe != NULL) &&
(oport->p_lastframe->size == s) &&
(memcmp(oport->p_lastframe->frame, frame, s) == 0)) {
/* Already received the same frame */
log_debug("decode", "duplicate frame, no need to decode");
oport->p_lastupdate = time(NULL);
return;
}
}
guess = lldpd_guess_type(cfg, frame, s);
for (i=0; cfg->g_protocols[i].mode != 0; i++) {
if (!cfg->g_protocols[i].enabled)
continue;
if (cfg->g_protocols[i].mode == guess) {
log_debug("decode", "using decode function for %s protocol",
cfg->g_protocols[i].name);
if (cfg->g_protocols[i].decode(cfg, frame,
s, hardware, &chassis, &port) == -1) {
log_debug("decode", "function for %s protocol did not decode this frame",
cfg->g_protocols[i].name);
return;
}
chassis->c_protocol = port->p_protocol =
cfg->g_protocols[i].mode;
break;
}
}
if (cfg->g_protocols[i].mode == 0) {
log_debug("decode", "unable to guess frame type on %s",
hardware->h_ifname);
return;
}
TRACE(LLDPD_FRAME_DECODED(
hardware->h_ifname,
cfg->g_protocols[i].name,
chassis->c_name,
port->p_descr));
/* Do we already have the same MSAP somewhere? */
int count = 0;
log_debug("decode", "search for the same MSAP");
TAILQ_FOREACH(oport, &hardware->h_rports, p_entries) {
if (port->p_protocol == oport->p_protocol) {
count++;
if ((port->p_id_subtype == oport->p_id_subtype) &&
(port->p_id_len == oport->p_id_len) &&
(memcmp(port->p_id, oport->p_id, port->p_id_len) == 0) &&
(chassis->c_id_subtype == oport->p_chassis->c_id_subtype) &&
(chassis->c_id_len == oport->p_chassis->c_id_len) &&
(memcmp(chassis->c_id, oport->p_chassis->c_id,
chassis->c_id_len) == 0)) {
ochassis = oport->p_chassis;
log_debug("decode", "MSAP is already known");
break;
}
}
}
/* Do we have room for a new MSAP? */
if (!oport && cfg->g_config.c_max_neighbors) {
if (count == (cfg->g_config.c_max_neighbors - 1)) {
log_debug("decode",
"max neighbors %d reached for port %s, "
"dropping any new ones silently",
cfg->g_config.c_max_neighbors,
hardware->h_ifname);
} else if (count > cfg->g_config.c_max_neighbors - 1) {
log_debug("decode",
"too many neighbors for port %s, drop this new one",
hardware->h_ifname);
lldpd_port_cleanup(port, 1);
lldpd_chassis_cleanup(chassis, 1);
free(port);
return;
}
}
/* No, but do we already know the system? */
if (!oport) {
log_debug("decode", "MSAP is unknown, search for the chassis");
TAILQ_FOREACH(ochassis, &cfg->g_chassis, c_entries) {
if ((chassis->c_protocol == ochassis->c_protocol) &&
(chassis->c_id_subtype == ochassis->c_id_subtype) &&
(chassis->c_id_len == ochassis->c_id_len) &&
(memcmp(chassis->c_id, ochassis->c_id,
chassis->c_id_len) == 0))
break;
}
}
if (oport) {
/* The port is known, remove it before adding it back */
TAILQ_REMOVE(&hardware->h_rports, oport, p_entries);
lldpd_port_cleanup(oport, 1);
free(oport);
}
if (ochassis) {
lldpd_move_chassis(ochassis, chassis);
chassis = ochassis;
} else {
/* Chassis not known, add it */
log_debug("decode", "unknown chassis, add it to the list");
chassis->c_index = ++cfg->g_lastrid;
chassis->c_refcount = 0;
TAILQ_INSERT_TAIL(&cfg->g_chassis, chassis, c_entries);
i = 0; TAILQ_FOREACH(ochassis, &cfg->g_chassis, c_entries) i++;
log_debug("decode", "%d different systems are known", i);
}
/* Add port */
port->p_lastchange = port->p_lastupdate = time(NULL);
if ((port->p_lastframe = (struct lldpd_frame *)malloc(s +
sizeof(struct lldpd_frame))) != NULL) {
port->p_lastframe->size = s;
memcpy(port->p_lastframe->frame, frame, s);
}
TAILQ_INSERT_TAIL(&hardware->h_rports, port, p_entries);
port->p_chassis = chassis;
port->p_chassis->c_refcount++;
/* Several cases are possible :
1. chassis is new, its refcount was 0. It is now attached
to this port, its refcount is 1.
2. chassis already exists and was attached to another
port, we increase its refcount accordingly.
3. chassis already exists and was attached to the same
port, its refcount was decreased with
lldpd_port_cleanup() and is now increased again.
In all cases, if the port already existed, it has been
freed with lldpd_port_cleanup() and therefore, the refcount
of the chassis that was attached to it is decreased.
*/
/* coverity[use_after_free]
TAILQ_REMOVE does the right thing */
i = 0; TAILQ_FOREACH(aport, &hardware->h_rports, p_entries)
i++;
log_debug("decode", "%d neighbors for %s", i,
hardware->h_ifname);
if (!oport) hardware->h_insert_cnt++;
/* Notify */
log_debug("decode", "send notifications for changes on %s",
hardware->h_ifname);
if (oport) {
TRACE(LLDPD_NEIGHBOR_UPDATE(hardware->h_ifname,
chassis->c_name,
port->p_descr,
i));
levent_ctl_notify(hardware->h_ifname, NEIGHBOR_CHANGE_UPDATED, port);
#ifdef USE_SNMP
agent_notify(hardware, NEIGHBOR_CHANGE_UPDATED, port);
#endif
} else {
TRACE(LLDPD_NEIGHBOR_NEW(hardware->h_ifname,
chassis->c_name,
port->p_descr,
i));
levent_ctl_notify(hardware->h_ifname, NEIGHBOR_CHANGE_ADDED, port);
#ifdef USE_SNMP
agent_notify(hardware, NEIGHBOR_CHANGE_ADDED, port);
#endif
}
#ifdef ENABLE_LLDPMED
if (!oport && port->p_chassis->c_med_type) {
/* New neighbor, fast start */
if (hardware->h_cfg->g_config.c_enable_fast_start &&
!hardware->h_tx_fast) {
log_debug("decode", "%s: entering fast start due to "
"new neighbor", hardware->h_ifname);
hardware->h_tx_fast = hardware->h_cfg->g_config.c_tx_fast_init;
}
levent_schedule_pdu(hardware);
}
#endif
return;
}
/* Get the output of lsb_release -s -d. This is a slow function. It should be
called once. It return NULL if any problem happens. Otherwise, this is a
statically allocated buffer. The result includes the trailing \n */
static char *
lldpd_get_lsb_release() {
static char release[1024];
char *const command[] = { "lsb_release", "-s", "-d", NULL };
int pid, status, devnull, count;
int pipefd[2];
log_debug("localchassis", "grab LSB release");
if (pipe(pipefd)) {
log_warn("localchassis", "unable to get a pair of pipes");
return NULL;
}
pid = vfork();
switch (pid) {
case -1:
log_warn("localchassis", "unable to fork");
return NULL;
case 0:
/* Child, exec lsb_release */
close(pipefd[0]);
if ((devnull = open("/dev/null", O_RDWR, 0)) != -1) {
dup2(devnull, STDIN_FILENO);
dup2(devnull, STDERR_FILENO);
dup2(pipefd[1], STDOUT_FILENO);
if (devnull > 2) close(devnull);
if (pipefd[1] > 2) close(pipefd[1]);
execvp("lsb_release", command);
}
_exit(127);
break;
default:
/* Father, read the output from the children */
close(pipefd[1]);
count = 0;
do {
status = read(pipefd[0], release+count, sizeof(release)-count);
if ((status == -1) && (errno == EINTR)) continue;
if (status > 0)
count += status;
} while (count < sizeof(release) && (status > 0));
if (status < 0) {
log_info("localchassis", "unable to read from lsb_release");
close(pipefd[0]);
waitpid(pid, &status, 0);
return NULL;
}
close(pipefd[0]);
if (count >= sizeof(release)) {
log_info("localchassis", "output of lsb_release is too large");
waitpid(pid, &status, 0);
return NULL;
}
status = -1;
if (waitpid(pid, &status, 0) != pid)
return NULL;
if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
log_info("localchassis", "lsb_release information not available");
return NULL;
}
if (!count) {
log_info("localchassis", "lsb_release returned an empty string");
return NULL;
}
release[count] = '\0';
return release;
}
/* Should not be here */
return NULL;
}
/* Same like lldpd_get_lsb_release but reads /etc/os-release for PRETTY_NAME=. */
static char *
lldpd_get_os_release() {
static char release[1024];
char line[1024];
char *key, *val;
char *ptr1 = release;
log_debug("localchassis", "grab OS release");
FILE *fp = fopen("/etc/os-release", "r");
if (!fp) {
log_debug("localchassis", "could not open /etc/os-release");
fp = fopen("/usr/lib/os-release", "r");
}
if (!fp) {
log_info("localchassis",
"could not open either /etc/os-release or /usr/lib/os-release");
return NULL;
}
while ((fgets(line, sizeof(line), fp) != NULL)) {
key = strtok(line, "=");
val = strtok(NULL, "=");
if (strncmp(key, "PRETTY_NAME", sizeof(line)) == 0) {
strlcpy(release, val, sizeof(line));
break;
}
}
fclose(fp);
/* Remove trailing newline and all " in the string. */
ptr1 = release + strlen(release) - 1;
while (ptr1 != release &&
((*ptr1 == '"') || (*ptr1 == '\n'))) {
*ptr1 = '\0';
ptr1--;
}
if (release[0] == '"')
return release+1;
return release;
}
static void
lldpd_hide_ports(struct lldpd *cfg, struct lldpd_hardware *hardware, int mask) {
struct lldpd_port *port;
int protocols[LLDPD_MODE_MAX+1];
char buffer[256];
int i, j, k, found;
unsigned int min;
log_debug("smartfilter", "apply smart filter for port %s",
hardware->h_ifname);
/* Compute the number of occurrences of each protocol */
for (i = 0; i <= LLDPD_MODE_MAX; i++) protocols[i] = 0;
TAILQ_FOREACH(port, &hardware->h_rports, p_entries)
protocols[port->p_protocol]++;
/* Turn the protocols[] array into an array of
enabled/disabled protocols. 1 means enabled, 0
means disabled. */
min = (unsigned int)-1;
for (i = 0; i <= LLDPD_MODE_MAX; i++)
if (protocols[i] && (protocols[i] < min))
min = protocols[i];
found = 0;
for (i = 0; i <= LLDPD_MODE_MAX; i++)
if ((protocols[i] == min) && !found) {
/* If we need a tie breaker, we take
the first protocol only */
if (cfg->g_config.c_smart & mask &
(SMART_OUTGOING_ONE_PROTO | SMART_INCOMING_ONE_PROTO))
found = 1;
protocols[i] = 1;
} else protocols[i] = 0;
/* We set the p_hidden flag to 1 if the protocol is disabled */
TAILQ_FOREACH(port, &hardware->h_rports, p_entries) {
if (mask == SMART_OUTGOING)
port->p_hidden_out = protocols[port->p_protocol]?0:1;
else
port->p_hidden_in = protocols[port->p_protocol]?0:1;
}
/* If we want only one neighbor, we take the first one */
if (cfg->g_config.c_smart & mask &
(SMART_OUTGOING_ONE_NEIGH | SMART_INCOMING_ONE_NEIGH)) {
found = 0;
TAILQ_FOREACH(port, &hardware->h_rports, p_entries) {
if (mask == SMART_OUTGOING) {
if (found) port->p_hidden_out = 1;
if (!port->p_hidden_out)
found = 1;
}
if (mask == SMART_INCOMING) {
if (found) port->p_hidden_in = 1;
if (!port->p_hidden_in)
found = 1;
}
}
}
/* Print a debug message summarizing the operation */
for (i = 0; i <= LLDPD_MODE_MAX; i++) protocols[i] = 0;
k = j = 0;
TAILQ_FOREACH(port, &hardware->h_rports, p_entries) {
if (!(((mask == SMART_OUTGOING) && port->p_hidden_out) ||
((mask == SMART_INCOMING) && port->p_hidden_in))) {
k++;
protocols[port->p_protocol] = 1;
}
j++;
}
buffer[0] = '\0';
for (i=0; cfg->g_protocols[i].mode != 0; i++) {
if (cfg->g_protocols[i].enabled && protocols[cfg->g_protocols[i].mode]) {
if (strlen(buffer) +
strlen(cfg->g_protocols[i].name) + 3 > sizeof(buffer)) {
/* Unlikely, our buffer is too small */
memcpy(buffer + sizeof(buffer) - 4, "...", 4);
break;
}
if (buffer[0])
strncat(buffer, ", ", 2);
strncat(buffer, cfg->g_protocols[i].name, strlen(cfg->g_protocols[i].name));
}
}
log_debug("smartfilter", "%s: %s: %d visible neighbors (out of %d)",
hardware->h_ifname,
(mask == SMART_OUTGOING)?"out filter":"in filter",
k, j);
log_debug("smartfilter", "%s: protocols: %s",
hardware->h_ifname, buffer[0]?buffer:"(none)");
}
/* Hide unwanted ports depending on smart mode set by the user */
static void
lldpd_hide_all(struct lldpd *cfg)
{
struct lldpd_hardware *hardware;
if (!cfg->g_config.c_smart)
return;
log_debug("smartfilter", "apply smart filter results on all ports");
TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) {
if (cfg->g_config.c_smart & SMART_INCOMING_FILTER)
lldpd_hide_ports(cfg, hardware, SMART_INCOMING);
if (cfg->g_config.c_smart & SMART_OUTGOING_FILTER)
lldpd_hide_ports(cfg, hardware, SMART_OUTGOING);
}
}
void
lldpd_recv(struct lldpd *cfg, struct lldpd_hardware *hardware, int fd)
{
char *buffer = NULL;
int n;
log_debug("receive", "receive a frame on %s",
hardware->h_ifname);
if ((buffer = (char *)malloc(hardware->h_mtu)) == NULL) {
log_warn("receive", "failed to alloc reception buffer");
return;
}
if ((n = hardware->h_ops->recv(cfg, hardware,
fd, buffer,
hardware->h_mtu)) == -1) {
log_debug("receive", "discard frame received on %s",
hardware->h_ifname);
free(buffer);
return;
}
if (cfg->g_config.c_paused) {
log_debug("receive", "paused, ignore the frame on %s",
hardware->h_ifname);
free(buffer);
return;
}
hardware->h_rx_cnt++;
log_debug("receive", "decode received frame on %s",
hardware->h_ifname);
TRACE(LLDPD_FRAME_RECEIVED(hardware->h_ifname, buffer, (size_t)n));
lldpd_decode(cfg, buffer, n, hardware);
lldpd_hide_all(cfg); /* Immediatly hide */
lldpd_count_neighbors(cfg);
free(buffer);
}
void
lldpd_send(struct lldpd_hardware *hardware)
{
struct lldpd *cfg = hardware->h_cfg;
struct lldpd_port *port;
int i, sent;
if (cfg->g_config.c_receiveonly || cfg->g_config.c_paused) return;
if ((hardware->h_flags & IFF_RUNNING) == 0)
return;
log_debug("send", "send PDU on %s", hardware->h_ifname);
sent = 0;
for (i=0; cfg->g_protocols[i].mode != 0; i++) {
if (!cfg->g_protocols[i].enabled)
continue;
/* We send only if we have at least one remote system
* speaking this protocol or if the protocol is forced */
if (cfg->g_protocols[i].enabled > 1) {
cfg->g_protocols[i].send(cfg, hardware);
sent++;
continue;
}
TAILQ_FOREACH(port, &hardware->h_rports, p_entries) {
/* If this remote port is disabled, we don't
* consider it */
if (port->p_hidden_out)
continue;
if (port->p_protocol ==
cfg->g_protocols[i].mode) {
TRACE(LLDPD_FRAME_SEND(hardware->h_ifname,
cfg->g_protocols[i].name));
log_debug("send", "send PDU on %s with protocol %s",
hardware->h_ifname,
cfg->g_protocols[i].name);
cfg->g_protocols[i].send(cfg,
hardware);
sent++;
break;
}
}
}
if (!sent) {
/* Nothing was sent for this port, let's speak the first
* available protocol. */
for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
if (!cfg->g_protocols[i].enabled) continue;
TRACE(LLDPD_FRAME_SEND(hardware->h_ifname,
cfg->g_protocols[i].name));
log_debug("send", "fallback to protocol %s for %s",
cfg->g_protocols[i].name, hardware->h_ifname);
cfg->g_protocols[i].send(cfg,
hardware);
break;
}
if (cfg->g_protocols[i].mode == 0)
log_warnx("send", "no protocol enabled, dunno what to send");
}
}
#ifdef ENABLE_LLDPMED
static void
lldpd_med(struct lldpd_chassis *chassis)
{
static short int once = 0;
if (!once) {
chassis->c_med_hw = dmi_hw();
chassis->c_med_fw = dmi_fw();
chassis->c_med_sn = dmi_sn();
chassis->c_med_manuf = dmi_manuf();
chassis->c_med_model = dmi_model();
chassis->c_med_asset = dmi_asset();
once = 1;
}
}
#endif
static int
lldpd_routing_enabled(struct lldpd *cfg)
{
int routing;
if ((LOCAL_CHASSIS(cfg)->c_cap_available & LLDP_CAP_ROUTER) == 0)
return 0;
if ((routing = interfaces_routing_enabled(cfg)) == -1) {
log_debug("localchassis", "unable to check if routing is enabled");
return 0;
}
return routing;
}
static void
lldpd_update_localchassis(struct lldpd *cfg)
{
struct utsname un;
char *hp;
log_debug("localchassis", "update information for local chassis");