/
interfaces-linux.c
799 lines (731 loc) · 22.9 KB
/
interfaces-linux.c
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
/* -*- 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 <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#endif
#include <linux/if_vlan.h>
#include <linux/if_bonding.h>
#include <linux/if_bridge.h>
#include <linux/wireless.h>
#include <linux/sockios.h>
#include <linux/if_packet.h>
#include <linux/ethtool.h>
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#define SYSFS_PATH_MAX 256
#define MAX_PORTS 1024
#define MAX_BRIDGES 1024
static int
iflinux_eth_init(struct lldpd *cfg, struct lldpd_hardware *hardware)
{
int fd;
log_debug("interfaces", "initialize ethernet device %s",
hardware->h_ifname);
if ((fd = priv_iface_init(hardware->h_ifindex, hardware->h_ifname)) == -1)
return -1;
hardware->h_sendfd = fd; /* Send */
interfaces_setup_multicast(cfg, hardware->h_ifname, 0);
levent_hardware_add_fd(hardware, fd); /* Receive */
log_debug("interfaces", "interface %s initialized (fd=%d)", hardware->h_ifname,
fd);
return 0;
}
/* Generic ethernet send/receive */
static int
iflinux_eth_send(struct lldpd *cfg, struct lldpd_hardware *hardware,
char *buffer, size_t size)
{
log_debug("interfaces", "send PDU to ethernet device %s (fd=%d)",
hardware->h_ifname, hardware->h_sendfd);
return write(hardware->h_sendfd,
buffer, size);
}
static int
iflinux_eth_recv(struct lldpd *cfg, struct lldpd_hardware *hardware,
int fd, char *buffer, size_t size)
{
int n;
struct sockaddr_ll from = {};
socklen_t fromlen;
log_debug("interfaces", "receive PDU from ethernet device %s",
hardware->h_ifname);
fromlen = sizeof(from);
if ((n = recvfrom(fd,
buffer,
size, 0,
(struct sockaddr *)&from,
&fromlen)) == -1) {
log_warn("interfaces", "error while receiving frame on %s",
hardware->h_ifname);
hardware->h_rx_discarded_cnt++;
return -1;
}
if (from.sll_pkttype == PACKET_OUTGOING)
return -1;
return n;
}
static int
iflinux_eth_close(struct lldpd *cfg, struct lldpd_hardware *hardware)
{
log_debug("interfaces", "close ethernet device %s",
hardware->h_ifname);
interfaces_setup_multicast(cfg, hardware->h_ifname, 1);
return 0;
}
static struct lldpd_ops eth_ops = {
.send = iflinux_eth_send,
.recv = iflinux_eth_recv,
.cleanup = iflinux_eth_close,
};
static int
iflinux_is_bridge(struct lldpd *cfg,
struct interfaces_device_list *interfaces,
struct interfaces_device *iface)
{
#ifdef ENABLE_OLDIES
struct interfaces_device *port;
char path[SYSFS_PATH_MAX];
int f;
if ((snprintf(path, SYSFS_PATH_MAX,
SYSFS_CLASS_NET "%s/" SYSFS_BRIDGE_FDB,
iface->name)) >= SYSFS_PATH_MAX)
log_warnx("interfaces", "path truncated");
if ((f = priv_open(path)) < 0)
return 0;
close(f);
/* Also grab all ports */
TAILQ_FOREACH(port, interfaces, next) {
if (port->upper) continue;
if (snprintf(path, SYSFS_PATH_MAX,
SYSFS_CLASS_NET "%s/" SYSFS_BRIDGE_PORT_SUBDIR "/%s/port_no",
iface->name, port->name) >= SYSFS_PATH_MAX)
log_warnx("interfaces", "path truncated");
if ((f = priv_open(path)) < 0)
continue;
log_debug("interfaces",
"port %s is bridged to %s",
port->name, iface->name);
port->upper = iface;
close(f);
}
return 1;
#else
return 0;
#endif
}
static int
iflinux_is_vlan(struct lldpd *cfg,
struct interfaces_device_list *interfaces,
struct interfaces_device *iface)
{
#ifdef ENABLE_OLDIES
struct vlan_ioctl_args ifv = {};
ifv.cmd = GET_VLAN_REALDEV_NAME_CMD;
strlcpy(ifv.device1, iface->name, sizeof(ifv.device1));
if (ioctl(cfg->g_sock, SIOCGIFVLAN, &ifv) >= 0) {
/* This is a VLAN, get the lower interface and the VID */
struct interfaces_device *lower =
interfaces_nametointerface(interfaces, ifv.u.device2);
if (!lower) {
log_debug("interfaces",
"unable to find lower interface for VLAN %s",
iface->name);
return 0;
}
memset(&ifv, 0, sizeof(ifv));
ifv.cmd = GET_VLAN_VID_CMD;
strlcpy(ifv.device1, iface->name, sizeof(ifv.device1));
if (ioctl(cfg->g_sock, SIOCGIFVLAN, &ifv) < 0) {
log_debug("interfaces",
"unable to find VID for VLAN %s",
iface->name);
return 0;
}
iface->lower = lower;
iface->vlanid = ifv.u.VID;
return 1;
}
#endif
return 0;
}
static int
iflinux_is_bond(struct lldpd *cfg,
struct interfaces_device_list *interfaces,
struct interfaces_device *master)
{
#ifdef ENABLE_OLDIES
/* Shortcut if we detect the new team driver. Upper and lower links
* should already be set with netlink in this case. */
if (master->driver && !strcmp(master->driver, "team")) {
return 1;
}
struct ifreq ifr = {};
struct ifbond ifb = {};
strlcpy(ifr.ifr_name, master->name, sizeof(ifr.ifr_name));
ifr.ifr_data = (char *)&ifb;
if (ioctl(cfg->g_sock, SIOCBONDINFOQUERY, &ifr) >= 0) {
while (ifb.num_slaves--) {
struct ifslave ifs;
memset(&ifr, 0, sizeof(ifr));
memset(&ifs, 0, sizeof(ifs));
strlcpy(ifr.ifr_name, master->name, sizeof(ifr.ifr_name));
ifr.ifr_data = (char *)&ifs;
ifs.slave_id = ifb.num_slaves;
if (ioctl(cfg->g_sock, SIOCBONDSLAVEINFOQUERY, &ifr) >= 0) {
struct interfaces_device *slave =
interfaces_nametointerface(interfaces,
ifs.slave_name);
if (slave == NULL) continue;
if (slave->upper) continue;
log_debug("interfaces",
"interface %s is enslaved to %s",
slave->name, master->name);
slave->upper = master;
}
}
return 1;
}
#endif
return 0;
}
/* Get the permanent MAC address using ethtool as a fallback There is a slight
* difference with /proc/net/bonding method. The /proc/net/bonding method will
* retrieve the MAC address of the interface before it was added to the
* bond. The ethtool method will retrieve the real permanent MAC address. For
* some devices, there is no such address (for example, virtual devices like
* veth). */
static void
iflinux_get_permanent_mac_ethtool(struct lldpd *cfg,
struct interfaces_device_list *interfaces,
struct interfaces_device *iface)
{
struct interfaces_device *master;
u_int8_t mac[ETHER_ADDR_LEN];
/* We could do that for any interface, but let's do that only for
* aggregates. */
if ((master = iface->upper) == NULL || master->type != IFACE_BOND_T)
return;
log_debug("interfaces", "get MAC address for %s",
iface->name);
if (priv_iface_mac(iface->name, mac, ETHER_ADDR_LEN) != 0) {
log_warnx("interfaces",
"unable to get permanent MAC address for %s",
master->name);
return;
}
size_t i;
for (i = 0; i < ETHER_ADDR_LEN; i++)
if (mac[i] != 0) break;
if (i == ETHER_ADDR_LEN) {
log_warnx("interfaces",
"no permanent MAC address found for %s",
master->name);
return;
}
memcpy(iface->address, mac,
ETHER_ADDR_LEN);
}
static void
iflinux_get_permanent_mac(struct lldpd *cfg,
struct interfaces_device_list *interfaces,
struct interfaces_device *iface)
{
struct interfaces_device *master;
int f, state = 0;
FILE *netbond;
const char *slaveif = "Slave Interface: ";
const char *hwaddr = "Permanent HW addr: ";
u_int8_t mac[ETHER_ADDR_LEN];
char path[SYSFS_PATH_MAX];
char line[100];
if ((master = iface->upper) == NULL || master->type != IFACE_BOND_T)
return;
/* We have a bond, we need to query it to get real MAC addresses */
if (snprintf(path, SYSFS_PATH_MAX, "/proc/net/bonding/%s",
master->name) >= SYSFS_PATH_MAX) {
log_warnx("interfaces", "path truncated");
return;
}
if ((f = priv_open(path)) < 0) {
if (snprintf(path, SYSFS_PATH_MAX, "/proc/self/net/bonding/%s",
master->name) >= SYSFS_PATH_MAX) {
log_warnx("interfaces", "path truncated");
return;
}
f = priv_open(path);
}
if (f < 0) {
iflinux_get_permanent_mac_ethtool(cfg, interfaces, iface);
return;
}
if ((netbond = fdopen(f, "r")) == NULL) {
log_warn("interfaces", "unable to read stream from %s", path);
close(f);
return;
}
/* State 0:
We parse the file to search "Slave Interface: ". If found, go to
state 1.
State 1:
We parse the file to search "Permanent HW addr: ". If found, we get
the mac.
*/
while (fgets(line, sizeof(line), netbond)) {
switch (state) {
case 0:
if (strncmp(line, slaveif, strlen(slaveif)) == 0) {
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = '\0';
if (strcmp(iface->name,
line + strlen(slaveif)) == 0)
state++;
}
break;
case 1:
if (strncmp(line, hwaddr, strlen(hwaddr)) == 0) {
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = '\0';
if (sscanf(line + strlen(hwaddr),
"%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
&mac[0], &mac[1], &mac[2],
&mac[3], &mac[4], &mac[5]) !=
ETHER_ADDR_LEN) {
log_warn("interfaces", "unable to parse %s",
line + strlen(hwaddr));
fclose(netbond);
return;
}
memcpy(iface->address, mac,
ETHER_ADDR_LEN);
fclose(netbond);
return;
}
break;
}
}
log_warnx("interfaces", "unable to find real mac address for %s",
iface->name);
fclose(netbond);
}
/* Fill up MAC/PHY for a given hardware port */
static void
iflinux_macphy(struct lldpd_hardware *hardware)
{
#ifdef ENABLE_DOT3
struct ethtool_cmd ethc;
struct lldpd_port *port = &hardware->h_lport;
int j;
int advertised_ethtool_to_rfc3636[][2] = {
{ADVERTISED_10baseT_Half, LLDP_DOT3_LINK_AUTONEG_10BASE_T},
{ADVERTISED_10baseT_Full, LLDP_DOT3_LINK_AUTONEG_10BASET_FD},
{ADVERTISED_100baseT_Half, LLDP_DOT3_LINK_AUTONEG_100BASE_TX},
{ADVERTISED_100baseT_Full, LLDP_DOT3_LINK_AUTONEG_100BASE_TXFD},
{ADVERTISED_1000baseT_Half, LLDP_DOT3_LINK_AUTONEG_1000BASE_T},
{ADVERTISED_1000baseT_Full, LLDP_DOT3_LINK_AUTONEG_1000BASE_TFD},
{ADVERTISED_10000baseT_Full, LLDP_DOT3_LINK_AUTONEG_OTHER},
{ADVERTISED_Pause, LLDP_DOT3_LINK_AUTONEG_FDX_PAUSE},
{ADVERTISED_Asym_Pause, LLDP_DOT3_LINK_AUTONEG_FDX_APAUSE},
{ADVERTISED_2500baseX_Full, LLDP_DOT3_LINK_AUTONEG_OTHER},
{0,0}};
log_debug("interfaces", "ask ethtool for the appropriate MAC/PHY for %s",
hardware->h_ifname);
if (priv_ethtool(hardware->h_ifname, ðc, sizeof(struct ethtool_cmd)) == 0) {
port->p_macphy.autoneg_support = (ethc.supported & SUPPORTED_Autoneg) ? 1 : 0;
port->p_macphy.autoneg_enabled = (ethc.autoneg == AUTONEG_DISABLE) ? 0 : 1;
for (j=0; advertised_ethtool_to_rfc3636[j][0]; j++) {
if (ethc.advertising & advertised_ethtool_to_rfc3636[j][0])
port->p_macphy.autoneg_advertised |=
advertised_ethtool_to_rfc3636[j][1];
}
switch (ethc.speed) {
case SPEED_10:
port->p_macphy.mau_type = (ethc.duplex == DUPLEX_FULL) ? \
LLDP_DOT3_MAU_10BASETFD : LLDP_DOT3_MAU_10BASETHD;
if (ethc.port == PORT_BNC) port->p_macphy.mau_type = LLDP_DOT3_MAU_10BASE2;
if (ethc.port == PORT_FIBRE)
port->p_macphy.mau_type = (ethc.duplex == DUPLEX_FULL) ? \
LLDP_DOT3_MAU_10BASEFLFD : LLDP_DOT3_MAU_10BASEFLHD;
break;
case SPEED_100:
port->p_macphy.mau_type = (ethc.duplex == DUPLEX_FULL) ? \
LLDP_DOT3_MAU_100BASETXFD : LLDP_DOT3_MAU_100BASETXHD;
if (ethc.port == PORT_BNC)
port->p_macphy.mau_type = (ethc.duplex == DUPLEX_FULL) ? \
LLDP_DOT3_MAU_100BASET2FD : LLDP_DOT3_MAU_100BASET2HD;
if (ethc.port == PORT_FIBRE)
port->p_macphy.mau_type = (ethc.duplex == DUPLEX_FULL) ? \
LLDP_DOT3_MAU_100BASEFXFD : LLDP_DOT3_MAU_100BASEFXHD;
break;
case SPEED_1000:
port->p_macphy.mau_type = (ethc.duplex == DUPLEX_FULL) ? \
LLDP_DOT3_MAU_1000BASETFD : LLDP_DOT3_MAU_1000BASETHD;
if (ethc.port == PORT_FIBRE)
port->p_macphy.mau_type = (ethc.duplex == DUPLEX_FULL) ? \
LLDP_DOT3_MAU_1000BASEXFD : LLDP_DOT3_MAU_1000BASEXHD;
break;
case SPEED_10000:
port->p_macphy.mau_type = (ethc.port == PORT_FIBRE) ? \
LLDP_DOT3_MAU_10GIGBASEX : LLDP_DOT3_MAU_10GIGBASER;
break;
}
if (ethc.port == PORT_AUI) port->p_macphy.mau_type = LLDP_DOT3_MAU_AUI;
}
#endif
}
struct bond_master {
char name[IFNAMSIZ];
int index;
};
static int
iface_bond_init(struct lldpd *cfg, struct lldpd_hardware *hardware)
{
struct bond_master *master = hardware->h_data;
int fd;
int un = 1;
if (!master) return -1;
log_debug("interfaces", "initialize enslaved device %s",
hardware->h_ifname);
/* First, we get a socket to the raw physical interface */
if ((fd = priv_iface_init(hardware->h_ifindex,
hardware->h_ifname)) == -1)
return -1;
hardware->h_sendfd = fd;
interfaces_setup_multicast(cfg, hardware->h_ifname, 0);
/* Then, we open a raw interface for the master */
log_debug("interfaces", "enslaved device %s has master %s(%d)",
hardware->h_ifname, master->name, master->index);
if ((fd = priv_iface_init(master->index, master->name)) == -1) {
close(hardware->h_sendfd);
return -1;
}
/* With bonding and older kernels (< 2.6.27) we need to listen
* to bond device. We use setsockopt() PACKET_ORIGDEV to get
* physical device instead of bond device (works with >=
* 2.6.24). */
if (setsockopt(fd, SOL_PACKET,
PACKET_ORIGDEV, &un, sizeof(un)) == -1) {
log_info("interfaces", "unable to setsockopt for master bonding device of %s. "
"You will get inaccurate results",
hardware->h_ifname);
}
interfaces_setup_multicast(cfg, master->name, 0);
levent_hardware_add_fd(hardware, hardware->h_sendfd);
levent_hardware_add_fd(hardware, fd);
log_debug("interfaces", "interface %s initialized (fd=%d,master=%s[%d])",
hardware->h_ifname,
hardware->h_sendfd,
master->name, fd);
return 0;
}
static int
iface_bond_recv(struct lldpd *cfg, struct lldpd_hardware *hardware,
int fd, char *buffer, size_t size)
{
int n;
struct sockaddr_ll from = {};
socklen_t fromlen;
struct bond_master *master = hardware->h_data;
log_debug("interfaces", "receive PDU from enslaved device %s",
hardware->h_ifname);
fromlen = sizeof(from);
if ((n = recvfrom(fd, buffer, size, 0,
(struct sockaddr *)&from,
&fromlen)) == -1) {
log_warn("interfaces", "error while receiving frame on %s",
hardware->h_ifname);
hardware->h_rx_discarded_cnt++;
return -1;
}
if (from.sll_pkttype == PACKET_OUTGOING)
return -1;
if (fd == hardware->h_sendfd)
/* We received this on the physical interface. */
return n;
/* We received this on the bonding interface. Is it really for us? */
if (from.sll_ifindex == hardware->h_ifindex)
/* This is for us */
return n;
if (from.sll_ifindex == master->index)
/* We don't know from which physical interface it comes (kernel
* < 2.6.24). In doubt, this is for us. */
return n;
return -1; /* Not for us */
}
static int
iface_bond_close(struct lldpd *cfg, struct lldpd_hardware *hardware)
{
struct bond_master *master = hardware->h_data;
log_debug("interfaces", "closing enslaved device %s",
hardware->h_ifname);
interfaces_setup_multicast(cfg, hardware->h_ifname, 1);
interfaces_setup_multicast(cfg, master->name, 1);
free(hardware->h_data); hardware->h_data = NULL;
return 0;
}
struct lldpd_ops bond_ops = {
.send = iflinux_eth_send,
.recv = iface_bond_recv,
.cleanup = iface_bond_close,
};
static void
iflinux_handle_bond(struct lldpd *cfg, struct interfaces_device_list *interfaces)
{
struct interfaces_device *iface;
struct interfaces_device *master;
struct lldpd_hardware *hardware;
struct bond_master *bmaster;
int created;
TAILQ_FOREACH(iface, interfaces, next) {
if (!(iface->type & IFACE_PHYSICAL_T)) continue;
if (iface->ignore) continue;
if (!iface->upper || !(iface->upper->type & IFACE_BOND_T)) continue;
master = iface->upper;
log_debug("interfaces", "%s is an acceptable enslaved device (master=%s)",
iface->name, master->name);
created = 0;
if ((hardware = lldpd_get_hardware(cfg,
iface->name,
iface->index)) == NULL) {
if ((hardware = lldpd_alloc_hardware(cfg,
iface->name,
iface->index)) == NULL) {
log_warnx("interfaces", "Unable to allocate space for %s",
iface->name);
continue;
}
created = 1;
}
if (hardware->h_flags) continue;
if (hardware->h_ops != &bond_ops) {
if (!created) {
log_debug("interfaces",
"bond %s is converted from another type of interface",
hardware->h_ifname);
if (hardware->h_ops && hardware->h_ops->cleanup)
hardware->h_ops->cleanup(cfg, hardware);
levent_hardware_release(hardware);
levent_hardware_init(hardware);
}
bmaster = hardware->h_data = calloc(1, sizeof(struct bond_master));
if (!bmaster) {
log_warn("interfaces", "not enough memory");
lldpd_hardware_cleanup(cfg, hardware);
continue;
}
} else bmaster = hardware->h_data;
bmaster->index = master->index;
strlcpy(bmaster->name, master->name, IFNAMSIZ);
if (hardware->h_ops != &bond_ops) {
if (iface_bond_init(cfg, hardware) != 0) {
log_warn("interfaces", "unable to initialize %s",
hardware->h_ifname);
lldpd_hardware_cleanup(cfg, hardware);
continue;
}
hardware->h_ops = &bond_ops;
hardware->h_mangle = 1;
}
if (created)
interfaces_helper_add_hardware(cfg, hardware);
else
lldpd_port_cleanup(&hardware->h_lport, 0);
hardware->h_flags = iface->flags;
iface->ignore = 1;
/* Get local address */
memcpy(&hardware->h_lladdr, iface->address, ETHER_ADDR_LEN);
/* Fill information about port */
interfaces_helper_port_name_desc(cfg, hardware, iface);
/* Fill additional info */
#ifdef ENABLE_DOT3
hardware->h_lport.p_aggregid = master->index;
#endif
hardware->h_mtu = iface->mtu ? iface->mtu : 1500;
}
}
/* Query each interface to get the appropriate driver */
static void
iflinux_add_driver(struct lldpd *cfg,
struct interfaces_device_list *interfaces)
{
struct interfaces_device *iface;
TAILQ_FOREACH(iface, interfaces, next) {
struct ethtool_drvinfo ethc = {
.cmd = ETHTOOL_GDRVINFO
};
struct ifreq ifr = {
.ifr_data = (caddr_t)ðc
};
if (iface->driver) continue;
strlcpy(ifr.ifr_name, iface->name, IFNAMSIZ);
if (ioctl(cfg->g_sock, SIOCETHTOOL, &ifr) == 0) {
iface->driver = strdup(ethc.driver);
log_debug("interfaces", "driver for %s is `%s`",
iface->name, iface->driver);
}
}
}
/* Query each interface to see if it is a wireless one */
static void
iflinux_add_wireless(struct lldpd *cfg,
struct interfaces_device_list *interfaces)
{
struct interfaces_device *iface;
TAILQ_FOREACH(iface, interfaces, next) {
struct iwreq iwr = {};
strlcpy(iwr.ifr_name, iface->name, IFNAMSIZ);
if (ioctl(cfg->g_sock, SIOCGIWNAME, &iwr) >= 0) {
log_debug("interfaces", "%s is wireless",
iface->name);
iface->type |= IFACE_WIRELESS_T | IFACE_PHYSICAL_T;
}
}
}
/* Query each interface to see if it is a bridge */
static void
iflinux_add_bridge(struct lldpd *cfg,
struct interfaces_device_list *interfaces)
{
struct interfaces_device *iface;
TAILQ_FOREACH(iface, interfaces, next) {
if (iface->type & (IFACE_PHYSICAL_T|
IFACE_VLAN_T|IFACE_BOND_T|IFACE_BRIDGE_T))
continue;
if (iflinux_is_bridge(cfg, interfaces, iface)) {
log_debug("interfaces",
"interface %s is a bridge",
iface->name);
iface->type |= IFACE_BRIDGE_T;
}
}
}
/* Query each interface to see if it is a bond */
static void
iflinux_add_bond(struct lldpd *cfg,
struct interfaces_device_list *interfaces)
{
struct interfaces_device *iface;
TAILQ_FOREACH(iface, interfaces, next) {
if (iface->type & (IFACE_PHYSICAL_T|IFACE_VLAN_T|
IFACE_BOND_T|IFACE_BRIDGE_T))
continue;
if (iflinux_is_bond(cfg, interfaces, iface)) {
log_debug("interfaces",
"interface %s is a bond",
iface->name);
iface->type |= IFACE_BOND_T;
}
}
}
/* Query each interface to see if it is a vlan */
static void
iflinux_add_vlan(struct lldpd *cfg,
struct interfaces_device_list *interfaces)
{
struct interfaces_device *iface;
TAILQ_FOREACH(iface, interfaces, next) {
if (iface->type & (IFACE_PHYSICAL_T|IFACE_VLAN_T|
IFACE_BOND_T|IFACE_BRIDGE_T))
continue;
if (iflinux_is_vlan(cfg, interfaces, iface)) {
log_debug("interfaces",
"interface %s is a VLAN",
iface->name);
iface->type |= IFACE_VLAN_T;
}
}
}
static void
iflinux_add_physical(struct lldpd *cfg,
struct interfaces_device_list *interfaces)
{
struct interfaces_device *iface;
TAILQ_FOREACH(iface, interfaces, next) {
if (iface->type & (IFACE_VLAN_T|
IFACE_BOND_T|IFACE_BRIDGE_T))
continue;
iface->type &= ~IFACE_PHYSICAL_T;
/* We request that the interface is able to do either multicast
* or broadcast to be able to send discovery frames. */
if (!(iface->flags & (IFF_MULTICAST|IFF_BROADCAST))) {
log_debug("interfaces", "skip %s: not able to do multicast nor broadcast",
iface->name);
continue;
}
/* If the interface is linked to another one, skip it too. */
if (iface->lower) {
log_debug("interfaces", "skip %s: there is a lower interface (%s)",
iface->name, iface->lower->name);
continue;
}
/* Get the real MAC address (for example, if the interface is enslaved) */
iflinux_get_permanent_mac(cfg, interfaces, iface);
log_debug("interfaces",
"%s is a physical interface",
iface->name);
iface->type |= IFACE_PHYSICAL_T;
}
}
void
interfaces_update(struct lldpd *cfg)
{
struct lldpd_hardware *hardware;
struct interfaces_device_list *interfaces;
struct interfaces_address_list *addresses;
interfaces = netlink_get_interfaces(cfg);
addresses = netlink_get_addresses(cfg);
if (interfaces == NULL || addresses == NULL) {
log_warnx("interfaces", "cannot update the list of local interfaces");
return;
}
/* Add missing bits to list of interfaces */
iflinux_add_driver(cfg, interfaces);
if (LOCAL_CHASSIS(cfg)->c_cap_available & LLDP_CAP_WLAN)
iflinux_add_wireless(cfg, interfaces);
if (LOCAL_CHASSIS(cfg)->c_cap_available & LLDP_CAP_BRIDGE)
iflinux_add_bridge(cfg, interfaces);
iflinux_add_bond(cfg, interfaces);
iflinux_add_vlan(cfg, interfaces);
iflinux_add_physical(cfg, interfaces);
interfaces_helper_whitelist(cfg, interfaces);
iflinux_handle_bond(cfg, interfaces);
interfaces_helper_physical(cfg, interfaces,
ð_ops,
iflinux_eth_init);
#ifdef ENABLE_DOT1
interfaces_helper_vlan(cfg, interfaces);
#endif
interfaces_helper_mgmt(cfg, addresses);
interfaces_helper_chassis(cfg, interfaces);
/* Mac/PHY */
TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) {
if (!hardware->h_flags) continue;
iflinux_macphy(hardware);
interfaces_helper_promisc(cfg, hardware);
}
}
void
interfaces_cleanup(struct lldpd *cfg)
{
netlink_cleanup(cfg);
}