1 /* -*- mode: c; c-file-style: "openbsd" -*- */
3 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <sys/ioctl.h>
25 #if defined(__clang__)
26 #pragma clang diagnostic push
27 #pragma clang diagnostic ignored "-Wdocumentation"
29 #include <linux/if_vlan.h>
30 #include <linux/if_bonding.h>
31 #include <linux/if_bridge.h>
32 #include <linux/wireless.h>
33 #include <linux/sockios.h>
34 #include <linux/if_packet.h>
35 #include <linux/ethtool.h>
36 #if defined(__clang__)
37 #pragma clang diagnostic pop
40 #define SYSFS_PATH_MAX 256
41 #define MAX_PORTS 1024
42 #define MAX_BRIDGES 1024
45 iflinux_eth_init(struct lldpd
*cfg
, struct lldpd_hardware
*hardware
)
49 log_debug("interfaces", "initialize ethernet device %s",
51 if ((fd
= priv_iface_init(hardware
->h_ifindex
, hardware
->h_ifname
)) == -1)
53 hardware
->h_sendfd
= fd
; /* Send */
55 interfaces_setup_multicast(cfg
, hardware
->h_ifname
, 0);
57 levent_hardware_add_fd(hardware
, fd
); /* Receive */
58 log_debug("interfaces", "interface %s initialized (fd=%d)", hardware
->h_ifname
,
63 /* Generic ethernet send/receive */
65 iflinux_eth_send(struct lldpd
*cfg
, struct lldpd_hardware
*hardware
,
66 char *buffer
, size_t size
)
68 log_debug("interfaces", "send PDU to ethernet device %s (fd=%d)",
69 hardware
->h_ifname
, hardware
->h_sendfd
);
70 return write(hardware
->h_sendfd
,
75 iflinux_generic_recv(struct lldpd_hardware
*hardware
,
76 int fd
, char *buffer
, size_t size
,
77 struct sockaddr_ll
*from
)
83 fromlen
= sizeof(*from
);
84 memset(from
, 0, fromlen
);
85 if ((n
= recvfrom(fd
, buffer
, size
, 0,
86 (struct sockaddr
*)from
,
88 if (errno
== EAGAIN
&& retry
== 0) {
89 /* There may be an error queued in the socket. Clear it and retry. */
90 levent_recv_error(fd
, hardware
->h_ifname
);
94 if (errno
== ENETDOWN
) {
95 log_debug("interfaces", "error while receiving frame on %s (network down)",
98 log_warn("interfaces", "error while receiving frame on %s (retry: %d)",
99 hardware
->h_ifname
, retry
);
100 hardware
->h_rx_discarded_cnt
++;
104 if (from
->sll_pkttype
== PACKET_OUTGOING
)
110 iflinux_eth_recv(struct lldpd
*cfg
, struct lldpd_hardware
*hardware
,
111 int fd
, char *buffer
, size_t size
)
114 struct sockaddr_ll from
;
116 log_debug("interfaces", "receive PDU from ethernet device %s",
118 if ((n
= iflinux_generic_recv(hardware
, fd
, buffer
, size
, &from
)) == -1)
124 iflinux_eth_close(struct lldpd
*cfg
, struct lldpd_hardware
*hardware
)
126 log_debug("interfaces", "close ethernet device %s",
128 interfaces_setup_multicast(cfg
, hardware
->h_ifname
, 1);
132 static struct lldpd_ops eth_ops
= {
133 .send
= iflinux_eth_send
,
134 .recv
= iflinux_eth_recv
,
135 .cleanup
= iflinux_eth_close
,
139 iflinux_is_bridge(struct lldpd
*cfg
,
140 struct interfaces_device_list
*interfaces
,
141 struct interfaces_device
*iface
)
144 struct interfaces_device
*port
;
145 char path
[SYSFS_PATH_MAX
];
148 if ((snprintf(path
, SYSFS_PATH_MAX
,
149 SYSFS_CLASS_NET
"%s/" SYSFS_BRIDGE_FDB
,
150 iface
->name
)) >= SYSFS_PATH_MAX
)
151 log_warnx("interfaces", "path truncated");
152 if ((f
= priv_open(path
)) < 0)
156 /* Also grab all ports */
157 TAILQ_FOREACH(port
, interfaces
, next
) {
158 if (port
->upper
) continue;
159 if (snprintf(path
, SYSFS_PATH_MAX
,
160 SYSFS_CLASS_NET
"%s/" SYSFS_BRIDGE_PORT_SUBDIR
"/%s/port_no",
161 iface
->name
, port
->name
) >= SYSFS_PATH_MAX
)
162 log_warnx("interfaces", "path truncated");
163 if ((f
= priv_open(path
)) < 0)
165 log_debug("interfaces",
166 "port %s is bridged to %s",
167 port
->name
, iface
->name
);
179 iflinux_is_vlan(struct lldpd
*cfg
,
180 struct interfaces_device_list
*interfaces
,
181 struct interfaces_device
*iface
)
184 struct vlan_ioctl_args ifv
= {};
185 ifv
.cmd
= GET_VLAN_REALDEV_NAME_CMD
;
186 strlcpy(ifv
.device1
, iface
->name
, sizeof(ifv
.device1
));
187 if (ioctl(cfg
->g_sock
, SIOCGIFVLAN
, &ifv
) >= 0) {
188 /* This is a VLAN, get the lower interface and the VID */
189 struct interfaces_device
*lower
=
190 interfaces_nametointerface(interfaces
, ifv
.u
.device2
);
192 log_debug("interfaces",
193 "unable to find lower interface for VLAN %s",
198 memset(&ifv
, 0, sizeof(ifv
));
199 ifv
.cmd
= GET_VLAN_VID_CMD
;
200 strlcpy(ifv
.device1
, iface
->name
, sizeof(ifv
.device1
));
201 if (ioctl(cfg
->g_sock
, SIOCGIFVLAN
, &ifv
) < 0) {
202 log_debug("interfaces",
203 "unable to find VID for VLAN %s",
208 iface
->lower
= lower
;
209 iface
->vlanid
= ifv
.u
.VID
;
217 iflinux_is_bond(struct lldpd
*cfg
,
218 struct interfaces_device_list
*interfaces
,
219 struct interfaces_device
*master
)
222 /* Shortcut if we detect the new team driver. Upper and lower links
223 * should already be set with netlink in this case. */
224 if (master
->driver
&& !strcmp(master
->driver
, "team")) {
228 struct ifreq ifr
= {};
229 struct ifbond ifb
= {};
230 strlcpy(ifr
.ifr_name
, master
->name
, sizeof(ifr
.ifr_name
));
231 ifr
.ifr_data
= (char *)&ifb
;
232 if (ioctl(cfg
->g_sock
, SIOCBONDINFOQUERY
, &ifr
) >= 0) {
233 while (ifb
.num_slaves
--) {
235 memset(&ifr
, 0, sizeof(ifr
));
236 memset(&ifs
, 0, sizeof(ifs
));
237 strlcpy(ifr
.ifr_name
, master
->name
, sizeof(ifr
.ifr_name
));
238 ifr
.ifr_data
= (char *)&ifs
;
239 ifs
.slave_id
= ifb
.num_slaves
;
240 if (ioctl(cfg
->g_sock
, SIOCBONDSLAVEINFOQUERY
, &ifr
) >= 0) {
241 struct interfaces_device
*slave
=
242 interfaces_nametointerface(interfaces
,
244 if (slave
== NULL
) continue;
245 if (slave
->upper
) continue;
246 log_debug("interfaces",
247 "interface %s is enslaved to %s",
248 slave
->name
, master
->name
);
249 slave
->upper
= master
;
259 * Get permanent MAC from ethtool.
261 * Return 0 on success, -1 on error.
264 iflinux_get_permanent_mac_ethtool(struct lldpd
*cfg
,
265 struct interfaces_device_list
*interfaces
,
266 struct interfaces_device
*iface
)
269 struct ifreq ifr
= {};
270 struct ethtool_perm_addr
*epaddr
= calloc(sizeof(struct ethtool_perm_addr
) + ETHER_ADDR_LEN
, 1);
271 if (epaddr
== NULL
) goto end
;
273 strlcpy(ifr
.ifr_name
, iface
->name
, sizeof(ifr
.ifr_name
));
274 epaddr
->cmd
= ETHTOOL_GPERMADDR
;
275 epaddr
->size
= ETHER_ADDR_LEN
;
276 ifr
.ifr_data
= (caddr_t
)epaddr
;
277 if (ioctl(cfg
->g_sock
, SIOCETHTOOL
, &ifr
) == -1) {
279 if (errno
== EPERM
&& !once
) {
280 log_warnx("interfaces",
281 "no permission to get permanent MAC address for %s (requires 2.6.19+)",
287 log_warn("interfaces", "cannot get permanent MAC address for %s",
291 if (epaddr
->data
[0] != 0 ||
292 epaddr
->data
[1] != 0 ||
293 epaddr
->data
[2] != 0 ||
294 epaddr
->data
[3] != 0 ||
295 epaddr
->data
[4] != 0 ||
296 epaddr
->data
[5] != 0) {
297 memcpy(iface
->address
, epaddr
->data
, ETHER_ADDR_LEN
);
301 log_debug("interfaces", "cannot get permanent MAC for %s (all 0)", iface
->name
);
308 * Get permanent MAC address for a bond device.
311 iflinux_get_permanent_mac_bond(struct lldpd
*cfg
,
312 struct interfaces_device_list
*interfaces
,
313 struct interfaces_device
*iface
)
315 struct interfaces_device
*master
= iface
->upper
;
318 const char *slaveif
= "Slave Interface: ";
319 const char *hwaddr
= "Permanent HW addr: ";
320 u_int8_t mac
[ETHER_ADDR_LEN
];
321 char path
[SYSFS_PATH_MAX
];
324 /* We have a bond, we need to query it to get real MAC addresses */
325 if (snprintf(path
, SYSFS_PATH_MAX
, "/proc/net/bonding/%s",
326 master
->name
) >= SYSFS_PATH_MAX
) {
327 log_warnx("interfaces", "path truncated");
330 if ((f
= priv_open(path
)) < 0) {
331 if (snprintf(path
, SYSFS_PATH_MAX
, "/proc/self/net/bonding/%s",
332 master
->name
) >= SYSFS_PATH_MAX
) {
333 log_warnx("interfaces", "path truncated");
339 log_warnx("interfaces",
340 "unable to get permanent MAC address for %s",
344 if ((netbond
= fdopen(f
, "r")) == NULL
) {
345 log_warn("interfaces", "unable to read stream from %s", path
);
350 We parse the file to search "Slave Interface: ". If found, go to
353 We parse the file to search "Permanent HW addr: ". If found, we get
356 while (fgets(line
, sizeof(line
), netbond
)) {
359 if (strncmp(line
, slaveif
, strlen(slaveif
)) == 0) {
360 if (line
[strlen(line
)-1] == '\n')
361 line
[strlen(line
)-1] = '\0';
362 if (strcmp(iface
->name
,
363 line
+ strlen(slaveif
)) == 0)
368 if (strncmp(line
, hwaddr
, strlen(hwaddr
)) == 0) {
369 if (line
[strlen(line
)-1] == '\n')
370 line
[strlen(line
)-1] = '\0';
371 if (sscanf(line
+ strlen(hwaddr
),
372 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
373 &mac
[0], &mac
[1], &mac
[2],
374 &mac
[3], &mac
[4], &mac
[5]) !=
376 log_warn("interfaces", "unable to parse %s",
377 line
+ strlen(hwaddr
));
381 memcpy(iface
->address
, mac
,
389 log_warnx("interfaces", "unable to find real MAC address for enslaved %s",
398 iflinux_get_permanent_mac(struct lldpd
*cfg
,
399 struct interfaces_device_list
*interfaces
,
400 struct interfaces_device
*iface
)
402 struct interfaces_device
*master
= iface
->upper
;
404 if (master
== NULL
|| master
->type
!= IFACE_BOND_T
)
406 if (iflinux_get_permanent_mac_ethtool(cfg
, interfaces
, iface
) == -1 &&
407 (master
->driver
== NULL
|| !strcmp(master
->driver
, "bonding")))
408 /* Fallback to old method for a bond */
409 iflinux_get_permanent_mac_bond(cfg
, interfaces
, iface
);
413 #define ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32 (SCHAR_MAX)
414 #define ETHTOOL_DECLARE_LINK_MODE_MASK(name) \
415 uint32_t name[ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32]
417 struct ethtool_link_usettings
{
418 struct ethtool_link_settings base
;
420 ETHTOOL_DECLARE_LINK_MODE_MASK(supported
);
421 ETHTOOL_DECLARE_LINK_MODE_MASK(advertising
);
422 ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising
);
427 iflinux_ethtool_link_mode_test_bit(unsigned int nr
, const uint32_t *mask
)
429 if (nr
>= 32 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32
)
431 return !!(mask
[nr
/ 32] & (1 << (nr
% 32)));
434 iflinux_ethtool_link_mode_unset_bit(unsigned int nr
, uint32_t *mask
)
436 if (nr
>= 32 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32
)
438 mask
[nr
/ 32] &= ~(1 << (nr
% 32));
441 iflinux_ethtool_link_mode_is_empty(const uint32_t *mask
)
443 for (unsigned int i
= 0;
444 i
< ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32
;
454 iflinux_ethtool_glink(struct lldpd
*cfg
, const char *ifname
, struct ethtool_link_usettings
*uset
) {
457 /* Try with ETHTOOL_GLINKSETTINGS first */
459 struct ethtool_link_settings req
;
460 uint32_t link_mode_data
[3 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32
];
462 static int8_t nwords
= 0;
463 struct ifreq ifr
= {};
464 strlcpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
467 /* Do a handshake first. We assume that this is device-independant. */
468 memset(&ecmd
, 0, sizeof(ecmd
));
469 ecmd
.req
.cmd
= ETHTOOL_GLINKSETTINGS
;
470 ifr
.ifr_data
= (caddr_t
)&ecmd
;
471 rc
= ioctl(cfg
->g_sock
, SIOCETHTOOL
, &ifr
);
473 nwords
= -ecmd
.req
.link_mode_masks_nwords
;
474 log_debug("interfaces", "glinksettings nwords is %" PRId8
, nwords
);
477 if (errno
== EPERM
&& !once
) {
478 log_warnx("interfaces",
479 "cannot get ethtool link information "
480 "with GLINKSETTINGS (requires 4.9+). "
481 "25G+ speeds may be missing in MAC/PHY TLVs");
488 memset(&ecmd
, 0, sizeof(ecmd
));
489 ecmd
.req
.cmd
= ETHTOOL_GLINKSETTINGS
;
490 ecmd
.req
.link_mode_masks_nwords
= nwords
;
491 ifr
.ifr_data
= (caddr_t
)&ecmd
;
492 rc
= ioctl(cfg
->g_sock
, SIOCETHTOOL
, &ifr
);
494 log_debug("interfaces", "got ethtool results for %s with GLINKSETTINGS",
496 memcpy(&uset
->base
, &ecmd
.req
, sizeof(uset
->base
));
497 unsigned int u32_offs
= 0;
498 memcpy(uset
->link_modes
.supported
,
499 &ecmd
.link_mode_data
[u32_offs
],
500 4 * ecmd
.req
.link_mode_masks_nwords
);
501 u32_offs
+= ecmd
.req
.link_mode_masks_nwords
;
502 memcpy(uset
->link_modes
.advertising
,
503 &ecmd
.link_mode_data
[u32_offs
],
504 4 * ecmd
.req
.link_mode_masks_nwords
);
505 u32_offs
+= ecmd
.req
.link_mode_masks_nwords
;
506 memcpy(uset
->link_modes
.lp_advertising
,
507 &ecmd
.link_mode_data
[u32_offs
],
508 4 * ecmd
.req
.link_mode_masks_nwords
);
513 /* Try with ETHTOOL_GSET */
514 struct ethtool_cmd ethc
;
515 memset(ðc
, 0, sizeof(ethc
));
516 ethc
.cmd
= ETHTOOL_GSET
;
517 ifr
.ifr_data
= (caddr_t
)ðc
;
518 rc
= ioctl(cfg
->g_sock
, SIOCETHTOOL
, &ifr
);
520 /* Do a partial copy (only what we need) */
521 log_debug("interfaces", "got ethtool results for %s with GSET",
523 memset(uset
, 0, sizeof(*uset
));
524 uset
->base
.cmd
= ETHTOOL_GSET
;
525 uset
->base
.link_mode_masks_nwords
= 1;
526 uset
->link_modes
.supported
[0] = ethc
.supported
;
527 uset
->link_modes
.advertising
[0] = ethc
.advertising
;
528 uset
->link_modes
.lp_advertising
[0] = ethc
.lp_advertising
;
529 uset
->base
.speed
= (ethc
.speed_hi
<< 16) | ethc
.speed
;
530 uset
->base
.duplex
= ethc
.duplex
;
531 uset
->base
.port
= ethc
.port
;
532 uset
->base
.autoneg
= ethc
.autoneg
;
535 if (errno
== EPERM
&& !once
) {
536 log_warnx("interfaces",
537 "cannot get ethtool link information "
538 "with GSET (requires 2.6.19+). "
539 "MAC/PHY TLV will be unavailable");
547 /* Fill up MAC/PHY for a given hardware port */
549 iflinux_macphy(struct lldpd
*cfg
, struct lldpd_hardware
*hardware
)
551 struct ethtool_link_usettings uset
;
552 struct lldpd_port
*port
= &hardware
->h_lport
;
554 int advertised_ethtool_to_rfc3636
[][2] = {
555 {ETHTOOL_LINK_MODE_10baseT_Half_BIT
, LLDP_DOT3_LINK_AUTONEG_10BASE_T
},
556 {ETHTOOL_LINK_MODE_10baseT_Full_BIT
, LLDP_DOT3_LINK_AUTONEG_10BASET_FD
},
557 {ETHTOOL_LINK_MODE_100baseT_Half_BIT
, LLDP_DOT3_LINK_AUTONEG_100BASE_TX
},
558 {ETHTOOL_LINK_MODE_100baseT_Full_BIT
, LLDP_DOT3_LINK_AUTONEG_100BASE_TXFD
},
559 {ETHTOOL_LINK_MODE_1000baseT_Half_BIT
, LLDP_DOT3_LINK_AUTONEG_1000BASE_T
},
560 {ETHTOOL_LINK_MODE_1000baseT_Full_BIT
, LLDP_DOT3_LINK_AUTONEG_1000BASE_TFD
},
561 {ETHTOOL_LINK_MODE_1000baseKX_Full_BIT
, LLDP_DOT3_LINK_AUTONEG_1000BASE_XFD
},
562 {ETHTOOL_LINK_MODE_Pause_BIT
, LLDP_DOT3_LINK_AUTONEG_FDX_PAUSE
},
563 {ETHTOOL_LINK_MODE_Asym_Pause_BIT
, LLDP_DOT3_LINK_AUTONEG_FDX_APAUSE
},
566 log_debug("interfaces", "ask ethtool for the appropriate MAC/PHY for %s",
568 if (iflinux_ethtool_glink(cfg
, hardware
->h_ifname
, &uset
) == 0) {
569 port
->p_macphy
.autoneg_support
= iflinux_ethtool_link_mode_test_bit(
570 ETHTOOL_LINK_MODE_Autoneg_BIT
, uset
.link_modes
.supported
);
571 port
->p_macphy
.autoneg_enabled
= (uset
.base
.autoneg
== AUTONEG_DISABLE
) ? 0 : 1;
572 for (j
=0; advertised_ethtool_to_rfc3636
[j
][0] >= 0; j
++) {
573 if (iflinux_ethtool_link_mode_test_bit(
574 advertised_ethtool_to_rfc3636
[j
][0],
575 uset
.link_modes
.advertising
)) {
576 port
->p_macphy
.autoneg_advertised
|=
577 advertised_ethtool_to_rfc3636
[j
][1];
578 iflinux_ethtool_link_mode_unset_bit(
579 advertised_ethtool_to_rfc3636
[j
][0],
580 uset
.link_modes
.advertising
);
583 iflinux_ethtool_link_mode_unset_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
, uset
.link_modes
.advertising
);
584 iflinux_ethtool_link_mode_unset_bit(ETHTOOL_LINK_MODE_TP_BIT
, uset
.link_modes
.advertising
);
585 iflinux_ethtool_link_mode_unset_bit(ETHTOOL_LINK_MODE_AUI_BIT
, uset
.link_modes
.advertising
);
586 iflinux_ethtool_link_mode_unset_bit(ETHTOOL_LINK_MODE_MII_BIT
, uset
.link_modes
.advertising
);
587 iflinux_ethtool_link_mode_unset_bit(ETHTOOL_LINK_MODE_FIBRE_BIT
, uset
.link_modes
.advertising
);
588 iflinux_ethtool_link_mode_unset_bit(ETHTOOL_LINK_MODE_BNC_BIT
, uset
.link_modes
.advertising
);
589 iflinux_ethtool_link_mode_unset_bit(ETHTOOL_LINK_MODE_Pause_BIT
, uset
.link_modes
.advertising
);
590 iflinux_ethtool_link_mode_unset_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT
, uset
.link_modes
.advertising
);
591 iflinux_ethtool_link_mode_unset_bit(ETHTOOL_LINK_MODE_Backplane_BIT
, uset
.link_modes
.advertising
);
592 if (!iflinux_ethtool_link_mode_is_empty(uset
.link_modes
.advertising
)) {
593 port
->p_macphy
.autoneg_advertised
|= LLDP_DOT3_LINK_AUTONEG_OTHER
;
595 switch (uset
.base
.speed
) {
597 port
->p_macphy
.mau_type
= (uset
.base
.duplex
== DUPLEX_FULL
) ? \
598 LLDP_DOT3_MAU_10BASETFD
: LLDP_DOT3_MAU_10BASETHD
;
599 if (uset
.base
.port
== PORT_BNC
) port
->p_macphy
.mau_type
= LLDP_DOT3_MAU_10BASE2
;
600 if (uset
.base
.port
== PORT_FIBRE
)
601 port
->p_macphy
.mau_type
= (uset
.base
.duplex
== DUPLEX_FULL
) ? \
602 LLDP_DOT3_MAU_10BASEFLFD
: LLDP_DOT3_MAU_10BASEFLHD
;
605 port
->p_macphy
.mau_type
= (uset
.base
.duplex
== DUPLEX_FULL
) ? \
606 LLDP_DOT3_MAU_100BASETXFD
: LLDP_DOT3_MAU_100BASETXHD
;
607 if (uset
.base
.port
== PORT_BNC
)
608 port
->p_macphy
.mau_type
= (uset
.base
.duplex
== DUPLEX_FULL
) ? \
609 LLDP_DOT3_MAU_100BASET2FD
: LLDP_DOT3_MAU_100BASET2HD
;
610 if (uset
.base
.port
== PORT_FIBRE
)
611 port
->p_macphy
.mau_type
= (uset
.base
.duplex
== DUPLEX_FULL
) ? \
612 LLDP_DOT3_MAU_100BASEFXFD
: LLDP_DOT3_MAU_100BASEFXHD
;
615 port
->p_macphy
.mau_type
= (uset
.base
.duplex
== DUPLEX_FULL
) ? \
616 LLDP_DOT3_MAU_1000BASETFD
: LLDP_DOT3_MAU_1000BASETHD
;
617 if (uset
.base
.port
== PORT_FIBRE
)
618 port
->p_macphy
.mau_type
= (uset
.base
.duplex
== DUPLEX_FULL
) ? \
619 LLDP_DOT3_MAU_1000BASEXFD
: LLDP_DOT3_MAU_1000BASEXHD
;
622 // For fiber, we tell 10GIGBASEX and for others,
623 // 10GIGBASER. It's not unusual to have 10GIGBASER on
624 // fiber either but we don't have 10GIGBASET for
625 // copper. No good solution.
626 port
->p_macphy
.mau_type
= (uset
.base
.port
== PORT_FIBRE
) ? \
627 LLDP_DOT3_MAU_10GIGBASELR
: LLDP_DOT3_MAU_10GIGBASECX4
;
630 // Same kind of approximation.
631 port
->p_macphy
.mau_type
= (uset
.base
.port
== PORT_FIBRE
) ? \
632 LLDP_DOT3_MAU_40GBASELR4
: LLDP_DOT3_MAU_40GBASECR4
;
636 port
->p_macphy
.mau_type
= (uset
.base
.port
== PORT_FIBRE
) ? \
637 LLDP_DOT3_MAU_100GBASELR4
: LLDP_DOT3_MAU_100GBASECR10
;
640 if (uset
.base
.port
== PORT_AUI
) port
->p_macphy
.mau_type
= LLDP_DOT3_MAU_AUI
;
643 #else /* ENABLE_DOT3 */
645 iflinux_macphy(struct lldpd
*cfg
, struct lldpd_hardware
*hardware
)
648 #endif /* ENABLE_DOT3 */
658 iface_bond_init(struct lldpd
*cfg
, struct lldpd_hardware
*hardware
)
660 struct bond_master
*master
= hardware
->h_data
;
664 if (!master
) return -1;
666 log_debug("interfaces", "initialize enslaved device %s",
669 /* First, we get a socket to the raw physical interface */
670 if ((fd
= priv_iface_init(hardware
->h_ifindex
,
671 hardware
->h_ifname
)) == -1)
673 hardware
->h_sendfd
= fd
;
674 interfaces_setup_multicast(cfg
, hardware
->h_ifname
, 0);
676 /* Then, we open a raw interface for the master */
677 log_debug("interfaces", "enslaved device %s has master %s(%d)",
678 hardware
->h_ifname
, master
->name
, master
->index
);
679 if ((fd
= priv_iface_init(master
->index
, master
->name
)) == -1) {
680 close(hardware
->h_sendfd
);
683 /* With bonding and older kernels (< 2.6.27) we need to listen
684 * to bond device. We use setsockopt() PACKET_ORIGDEV to get
685 * physical device instead of bond device (works with >=
687 if (setsockopt(fd
, SOL_PACKET
,
688 PACKET_ORIGDEV
, &un
, sizeof(un
)) == -1) {
689 log_info("interfaces", "unable to setsockopt for master bonding device of %s. "
690 "You will get inaccurate results",
693 interfaces_setup_multicast(cfg
, master
->name
, 0);
695 levent_hardware_add_fd(hardware
, hardware
->h_sendfd
);
696 levent_hardware_add_fd(hardware
, fd
);
697 log_debug("interfaces", "interface %s initialized (fd=%d,master=%s[%d])",
705 iface_bond_recv(struct lldpd
*cfg
, struct lldpd_hardware
*hardware
,
706 int fd
, char *buffer
, size_t size
)
709 struct sockaddr_ll from
;
710 struct bond_master
*master
= hardware
->h_data
;
712 log_debug("interfaces", "receive PDU from enslaved device %s",
714 if ((n
= iflinux_generic_recv(hardware
, fd
, buffer
, size
, &from
)) == -1)
716 if (fd
== hardware
->h_sendfd
)
717 /* We received this on the physical interface. */
719 /* We received this on the bonding interface. Is it really for us? */
720 if (from
.sll_ifindex
== hardware
->h_ifindex
)
723 if (from
.sll_ifindex
== master
->index
)
724 /* We don't know from which physical interface it comes (kernel
725 * < 2.6.24). In doubt, this is for us. */
727 return -1; /* Not for us */
731 iface_bond_close(struct lldpd
*cfg
, struct lldpd_hardware
*hardware
)
733 struct bond_master
*master
= hardware
->h_data
;
734 log_debug("interfaces", "closing enslaved device %s",
736 interfaces_setup_multicast(cfg
, hardware
->h_ifname
, 1);
737 interfaces_setup_multicast(cfg
, master
->name
, 1);
738 free(hardware
->h_data
); hardware
->h_data
= NULL
;
742 struct lldpd_ops bond_ops
= {
743 .send
= iflinux_eth_send
,
744 .recv
= iface_bond_recv
,
745 .cleanup
= iface_bond_close
,
749 iflinux_handle_bond(struct lldpd
*cfg
, struct interfaces_device_list
*interfaces
)
751 struct interfaces_device
*iface
;
752 struct interfaces_device
*master
;
753 struct lldpd_hardware
*hardware
;
754 struct bond_master
*bmaster
;
756 TAILQ_FOREACH(iface
, interfaces
, next
) {
757 if (!(iface
->type
& IFACE_PHYSICAL_T
)) continue;
758 if (iface
->ignore
) continue;
759 if (!iface
->upper
|| !(iface
->upper
->type
& IFACE_BOND_T
)) continue;
761 master
= iface
->upper
;
762 log_debug("interfaces", "%s is an acceptable enslaved device (master=%s)",
763 iface
->name
, master
->name
);
765 if ((hardware
= lldpd_get_hardware(cfg
,
767 iface
->index
)) == NULL
) {
768 if ((hardware
= lldpd_alloc_hardware(cfg
,
770 iface
->index
)) == NULL
) {
771 log_warnx("interfaces", "Unable to allocate space for %s",
777 if (hardware
->h_flags
) continue;
778 if (hardware
->h_ops
!= &bond_ops
) {
780 log_debug("interfaces",
781 "bond %s is converted from another type of interface",
783 if (hardware
->h_ops
&& hardware
->h_ops
->cleanup
)
784 hardware
->h_ops
->cleanup(cfg
, hardware
);
785 levent_hardware_release(hardware
);
786 levent_hardware_init(hardware
);
788 bmaster
= hardware
->h_data
= calloc(1, sizeof(struct bond_master
));
790 log_warn("interfaces", "not enough memory");
791 lldpd_hardware_cleanup(cfg
, hardware
);
794 } else bmaster
= hardware
->h_data
;
795 bmaster
->index
= master
->index
;
796 strlcpy(bmaster
->name
, master
->name
, IFNAMSIZ
);
797 if (hardware
->h_ops
!= &bond_ops
) {
798 if (iface_bond_init(cfg
, hardware
) != 0) {
799 log_warn("interfaces", "unable to initialize %s",
801 lldpd_hardware_cleanup(cfg
, hardware
);
804 hardware
->h_ops
= &bond_ops
;
805 hardware
->h_mangle
= 1;
808 interfaces_helper_add_hardware(cfg
, hardware
);
810 lldpd_port_cleanup(&hardware
->h_lport
, 0);
812 hardware
->h_flags
= iface
->flags
;
815 /* Get local address */
816 memcpy(&hardware
->h_lladdr
, iface
->address
, ETHER_ADDR_LEN
);
818 /* Fill information about port */
819 interfaces_helper_port_name_desc(cfg
, hardware
, iface
);
821 /* Fill additional info */
823 hardware
->h_lport
.p_aggregid
= master
->index
;
825 hardware
->h_mtu
= iface
->mtu
? iface
->mtu
: 1500;
830 /* Query each interface to get the appropriate driver */
832 iflinux_add_driver(struct lldpd
*cfg
,
833 struct interfaces_device_list
*interfaces
)
835 struct interfaces_device
*iface
;
836 TAILQ_FOREACH(iface
, interfaces
, next
) {
837 struct ethtool_drvinfo ethc
= {
838 .cmd
= ETHTOOL_GDRVINFO
841 .ifr_data
= (caddr_t
)ðc
843 if (iface
->driver
) continue;
845 strlcpy(ifr
.ifr_name
, iface
->name
, IFNAMSIZ
);
846 if (ioctl(cfg
->g_sock
, SIOCETHTOOL
, &ifr
) == 0) {
847 iface
->driver
= strdup(ethc
.driver
);
848 log_debug("interfaces", "driver for %s is `%s`",
849 iface
->name
, iface
->driver
);
854 /* Query each interface to see if it is a wireless one */
856 iflinux_add_wireless(struct lldpd
*cfg
,
857 struct interfaces_device_list
*interfaces
)
859 struct interfaces_device
*iface
;
860 TAILQ_FOREACH(iface
, interfaces
, next
) {
861 struct iwreq iwr
= {};
862 strlcpy(iwr
.ifr_name
, iface
->name
, IFNAMSIZ
);
863 if (ioctl(cfg
->g_sock
, SIOCGIWNAME
, &iwr
) >= 0) {
864 log_debug("interfaces", "%s is wireless",
866 iface
->type
|= IFACE_WIRELESS_T
| IFACE_PHYSICAL_T
;
871 /* Query each interface to see if it is a bridge */
873 iflinux_add_bridge(struct lldpd
*cfg
,
874 struct interfaces_device_list
*interfaces
)
876 struct interfaces_device
*iface
;
878 TAILQ_FOREACH(iface
, interfaces
, next
) {
879 if (iface
->type
& (IFACE_PHYSICAL_T
|
880 IFACE_VLAN_T
|IFACE_BOND_T
|IFACE_BRIDGE_T
))
882 if (iflinux_is_bridge(cfg
, interfaces
, iface
)) {
883 log_debug("interfaces",
884 "interface %s is a bridge",
886 iface
->type
|= IFACE_BRIDGE_T
;
891 /* Query each interface to see if it is a bond */
893 iflinux_add_bond(struct lldpd
*cfg
,
894 struct interfaces_device_list
*interfaces
)
896 struct interfaces_device
*iface
;
898 TAILQ_FOREACH(iface
, interfaces
, next
) {
899 if (iface
->type
& (IFACE_PHYSICAL_T
|IFACE_VLAN_T
|
900 IFACE_BOND_T
|IFACE_BRIDGE_T
))
902 if (iflinux_is_bond(cfg
, interfaces
, iface
)) {
903 log_debug("interfaces",
904 "interface %s is a bond",
906 iface
->type
|= IFACE_BOND_T
;
911 /* Query each interface to see if it is a vlan */
913 iflinux_add_vlan(struct lldpd
*cfg
,
914 struct interfaces_device_list
*interfaces
)
916 struct interfaces_device
*iface
;
918 TAILQ_FOREACH(iface
, interfaces
, next
) {
919 if (iface
->type
& (IFACE_PHYSICAL_T
|IFACE_VLAN_T
|
920 IFACE_BOND_T
|IFACE_BRIDGE_T
))
922 if (iflinux_is_vlan(cfg
, interfaces
, iface
)) {
923 log_debug("interfaces",
924 "interface %s is a VLAN",
926 iface
->type
|= IFACE_VLAN_T
;
932 iflinux_add_physical(struct lldpd
*cfg
,
933 struct interfaces_device_list
*interfaces
)
935 struct interfaces_device
*iface
;
936 /* Blacklist some drivers */
937 const char * const *rif
;
938 const char * const blacklisted_drivers
[] = {
944 TAILQ_FOREACH(iface
, interfaces
, next
) {
945 if (iface
->type
& (IFACE_VLAN_T
|
946 IFACE_BOND_T
|IFACE_BRIDGE_T
))
949 iface
->type
&= ~IFACE_PHYSICAL_T
;
951 /* We request that the interface is able to do either multicast
952 * or broadcast to be able to send discovery frames. */
953 if (!(iface
->flags
& (IFF_MULTICAST
|IFF_BROADCAST
))) {
954 log_debug("interfaces", "skip %s: not able to do multicast nor broadcast",
959 /* Check if the driver is not blacklisted */
962 for (rif
= blacklisted_drivers
; *rif
; rif
++) {
963 if (strcmp(iface
->driver
, *rif
) == 0) {
964 log_debug("interfaces", "skip %s: blacklisted driver",
973 /* If the interface is linked to another one, skip it too. */
974 if (iface
->lower
&& (!iface
->driver
|| (strcmp(iface
->driver
, "veth") && strcmp(iface
->driver
, "dsa")))) {
975 log_debug("interfaces", "skip %s: there is a lower interface (%s)",
976 iface
->name
, iface
->lower
->name
);
980 /* Get the real MAC address (for example, if the interface is enslaved) */
981 iflinux_get_permanent_mac(cfg
, interfaces
, iface
);
983 log_debug("interfaces",
984 "%s is a physical interface",
986 iface
->type
|= IFACE_PHYSICAL_T
;
991 interfaces_update(struct lldpd
*cfg
)
993 struct lldpd_hardware
*hardware
;
994 struct interfaces_device_list
*interfaces
;
995 struct interfaces_address_list
*addresses
;
996 interfaces
= netlink_get_interfaces(cfg
);
997 addresses
= netlink_get_addresses(cfg
);
998 if (interfaces
== NULL
|| addresses
== NULL
) {
999 log_warnx("interfaces", "cannot update the list of local interfaces");
1003 /* Add missing bits to list of interfaces */
1004 iflinux_add_driver(cfg
, interfaces
);
1005 if (LOCAL_CHASSIS(cfg
)->c_cap_available
& LLDP_CAP_WLAN
)
1006 iflinux_add_wireless(cfg
, interfaces
);
1007 if (LOCAL_CHASSIS(cfg
)->c_cap_available
& LLDP_CAP_BRIDGE
)
1008 iflinux_add_bridge(cfg
, interfaces
);
1009 iflinux_add_bond(cfg
, interfaces
);
1010 iflinux_add_vlan(cfg
, interfaces
);
1011 iflinux_add_physical(cfg
, interfaces
);
1013 interfaces_helper_whitelist(cfg
, interfaces
);
1014 #ifdef ENABLE_OLDIES
1015 iflinux_handle_bond(cfg
, interfaces
);
1017 interfaces_helper_physical(cfg
, interfaces
,
1021 interfaces_helper_vlan(cfg
, interfaces
);
1023 interfaces_helper_mgmt(cfg
, addresses
);
1024 interfaces_helper_chassis(cfg
, interfaces
);
1027 TAILQ_FOREACH(hardware
, &cfg
->g_hardware
, h_entries
) {
1028 if (!hardware
->h_flags
) continue;
1029 iflinux_macphy(cfg
, hardware
);
1030 interfaces_helper_promisc(cfg
, hardware
);
1035 interfaces_cleanup(struct lldpd
*cfg
)
1037 netlink_cleanup(cfg
);