]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
interfaces: sending/receiving on raw interfaces is OS specific
authorVincent Bernat <bernat@luffy.cx>
Tue, 25 Dec 2012 11:15:36 +0000 (12:15 +0100)
committerVincent Bernat <bernat@luffy.cx>
Tue, 25 Dec 2012 11:15:36 +0000 (12:15 +0100)
On Linux, we can send/receive with `recvfrom()`. With BSD and the use
of BPF, we send receive from BPF buffers. Therefore, ethernet
operations are moved into `interfaces-linux.c`.

src/daemon/interfaces-linux.c
src/daemon/interfaces.c
src/daemon/lldpd.h

index 88020365fe81d184579337bc58f1f6dde3392290..c4be843f6d74a4ef4a741b52f36e9a79e259668b 100644 (file)
@@ -78,6 +78,58 @@ iflinux_eth_init(struct lldpd *cfg, struct lldpd_hardware *hardware)
        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
 old_iflinux_is_bridge(struct lldpd *cfg,
     struct interfaces_device_list *interfaces,
@@ -782,6 +834,7 @@ interfaces_update(struct lldpd *cfg)
        interfaces_helper_whitelist(cfg, interfaces);
        iflinux_handle_bond(cfg, interfaces);
        interfaces_helper_physical(cfg, interfaces,
+           &eth_ops,
            iflinux_eth_init);
 #ifdef ENABLE_DOT1
        interfaces_helper_vlan(cfg, interfaces);
index 14f54763f849f5c695e5a753449c04b7d6d3d6de..e3908efd11b23df5f71e5a12c14f45213cf1ef66 100644 (file)
 #include <netpacket/packet.h>
 #include <arpa/inet.h>
 
-/* Generic ethernet send/receive */
-static int
-iface_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
-iface_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
-iface_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;
-}
-
-struct lldpd_ops eth_ops = {
-       .send = iface_eth_send,
-       .recv = iface_eth_recv,
-       .cleanup = iface_eth_close,
-};
-
 /* Generic ethernet interface initialization */
 /**
  * Enable multicast on the given interface.
@@ -548,6 +496,7 @@ interfaces_helper_port_name_desc(struct lldpd_hardware *hardware,
 void
 interfaces_helper_physical(struct lldpd *cfg,
     struct interfaces_device_list *interfaces,
+    struct lldpd_ops *ops,
     int(*init)(struct lldpd *, struct lldpd_hardware *))
 {
        struct interfaces_device *iface;
@@ -562,7 +511,7 @@ interfaces_helper_physical(struct lldpd *cfg,
                if ((hardware = lldpd_get_hardware(cfg,
                            iface->name,
                            iface->index,
-                           &eth_ops)) == NULL) {
+                           ops)) == NULL) {
                        if  ((hardware = lldpd_alloc_hardware(cfg,
                                    iface->name,
                                    iface->index)) == NULL) {
@@ -577,7 +526,7 @@ interfaces_helper_physical(struct lldpd *cfg,
                                lldpd_hardware_cleanup(cfg, hardware);
                                continue;
                        }
-                       hardware->h_ops = &eth_ops;
+                       hardware->h_ops = ops;
                        TAILQ_INSERT_TAIL(&cfg->g_hardware, hardware, h_entries);
                } else {
                        if (hardware->h_flags) continue; /* Already seen this time */
index 400285aa8071389d91e8abd5363a0327701c4831..690065537b7ed5572549aab303503ee0d3445024 100644 (file)
@@ -314,6 +314,7 @@ void interfaces_helper_chassis(struct lldpd *,
     struct interfaces_device_list *);
 void interfaces_helper_physical(struct lldpd *,
     struct interfaces_device_list *,
+    struct lldpd_ops *,
     int(*init)(struct lldpd *, struct lldpd_hardware *));
 void interfaces_helper_port_name_desc(struct lldpd_hardware *,
     struct interfaces_device *);