Move Autoconf's macro definitions into config.h.
[openvswitch] / lib / netdev.c
index a96f782cd1bb6d08ab2f44e7aced084808172e94..0bcef6712ffdac5499c705ad5abb401c4ce9cc48 100644 (file)
@@ -31,6 +31,7 @@
  * derivatives without specific, written prior permission.
  */
 
+#include <config.h>
 #include "netdev.h"
 
 #include <assert.h>
@@ -48,6 +49,7 @@
 #include <net/if.h>
 #include <net/if_arp.h>
 #include <net/if_packet.h>
+#include <net/route.h>
 #include <netinet/in.h>
 #include <stdlib.h>
 #include <string.h>
@@ -59,6 +61,7 @@
 #include "openflow.h"
 #include "packets.h"
 #include "poll-loop.h"
+#include "socket-util.h"
 
 #define THIS_MODULE VLM_netdev
 #include "vlog.h"
@@ -66,6 +69,7 @@
 struct netdev {
     struct list node;
     char *name;
+    int ifindex;
     int fd;
     uint8_t etheraddr[ETH_ADDR_LEN];
     int speed;
@@ -73,7 +77,8 @@ struct netdev {
     uint32_t features;
     struct in_addr in4;
     struct in6_addr in6;
-    int save_flags;
+    int save_flags;             /* Initial device flags. */
+    int changed_flags;          /* Flags that we changed. */
 };
 
 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
@@ -219,11 +224,9 @@ int
 netdev_open(const char *name, int ethertype, struct netdev **netdev_)
 {
     int fd;
-    struct sockaddr sa;
+    struct sockaddr_ll sll;
     struct ifreq ifr;
     unsigned int ifindex;
-    socklen_t rcvbuf_len;
-    size_t rcvbuf;
     uint8_t etheraddr[ETH_ADDR_LEN];
     struct in_addr in4;
     struct in6_addr in6;
@@ -234,12 +237,8 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_)
     *netdev_ = NULL;
     init_netdev();
 
-    /* Create raw socket.
-     *
-     * We have to use SOCK_PACKET, despite its deprecation, because only
-     * SOCK_PACKET lets us set the hardware source address of outgoing
-     * packets. */
-    fd = socket(PF_PACKET, SOCK_PACKET,
+    /* Create raw socket. */
+    fd = socket(PF_PACKET, SOCK_RAW,
                 htons(ethertype == NETDEV_ETH_TYPE_NONE ? 0
                       : ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
                       : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
@@ -248,37 +247,6 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_)
         return errno;
     }
 
-    /* Bind to specific ethernet device. */
-    memset(&sa, 0, sizeof sa);
-    sa.sa_family = AF_UNSPEC;
-    strncpy((char *) sa.sa_data, name, sizeof sa.sa_data);
-    if (bind(fd, &sa, sizeof sa) < 0) {
-        VLOG_ERR("bind to %s failed: %s", name, strerror(errno));
-        goto error;
-    }
-
-    /* Between the socket() and bind() calls above, the socket receives all
-     * packets on all system interfaces.  We do not want to receive that
-     * data, but there is no way to avoid it.  So we must now drain out the
-     * receive queue.  There is no way to know how long the receive queue is,
-     * but we know that the total number of bytes queued does not exceed the
-     * receive buffer size, so we pull packets until none are left or we've
-     * read that many bytes. */
-    rcvbuf_len = sizeof rcvbuf;
-    if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &rcvbuf_len) < 0) {
-        VLOG_ERR("getsockopt(SO_RCVBUF) on %s device failed: %s",
-                 name, strerror(errno));
-        goto error;
-    }
-    while (rcvbuf > 0) {
-        char buffer;
-        ssize_t n_bytes = recv(fd, &buffer, 1, MSG_TRUNC | MSG_DONTWAIT);
-        if (n_bytes <= 0) {
-            break;
-        }
-        rcvbuf -= n_bytes;
-    }
-
     /* Get ethernet device index. */
     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
     if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
@@ -288,6 +256,26 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_)
     }
     ifindex = ifr.ifr_ifindex;
 
+    /* Bind to specific ethernet device. */
+    memset(&sll, 0, sizeof sll);
+    sll.sll_family = AF_PACKET;
+    sll.sll_ifindex = ifindex;
+    if (bind(fd, (struct sockaddr *) &sll, sizeof sll) < 0) {
+        VLOG_ERR("bind to %s failed: %s", name, strerror(errno));
+        goto error;
+    }
+
+    if (ethertype != NETDEV_ETH_TYPE_NONE) {
+        /* Between the socket() and bind() calls above, the socket receives all
+         * packets of the requested type on all system interfaces.  We do not
+         * want to receive that data, but there is no way to avoid it.  So we
+         * must now drain out the receive queue. */
+        error = drain_rcvbuf(fd);
+        if (error) {
+            goto error;
+        }
+    }
+
     /* Get MAC address. */
     if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
         VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
@@ -317,6 +305,7 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_)
     /* Allocate network device. */
     netdev = xmalloc(sizeof *netdev);
     netdev->name = xstrdup(name);
+    netdev->ifindex = ifindex;
     netdev->fd = fd;
     memcpy(netdev->etheraddr, etheraddr, sizeof etheraddr);
     netdev->mtu = mtu;
@@ -331,6 +320,7 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_)
     if (error) {
         goto preset_error;
     }
+    netdev->changed_flags = 0;
     fatal_signal_block();
     list_push_back(&netdev_list, &netdev->node);
     fatal_signal_unblock();
@@ -432,6 +422,13 @@ netdev_recv_wait(struct netdev *netdev)
     poll_fd_wait(netdev->fd, POLLIN);
 }
 
+/* Discards all packets waiting to be received from 'netdev'. */
+void
+netdev_drain(struct netdev *netdev)
+{
+    drain_rcvbuf(netdev->fd);
+}
+
 /* Sends 'buffer' on 'netdev'.  Returns 0 if successful, otherwise a positive
  * errno value.  Returns EAGAIN without blocking if the packet cannot be queued
  * immediately.  Returns EMSGSIZE if a partial packet was transmitted or if
@@ -446,7 +443,6 @@ netdev_send(struct netdev *netdev, const struct buffer *buffer)
 {
     ssize_t n_bytes;
     const struct eth_header *eh;
-    struct sockaddr_pkt spkt;
 
     /* Pull out the Ethernet header. */
     if (buffer->size < ETH_HEADER_LEN) {
@@ -456,14 +452,8 @@ netdev_send(struct netdev *netdev, const struct buffer *buffer)
     }
     eh = buffer_at_assert(buffer, 0, sizeof *eh);
 
-    /* Construct packet sockaddr, which SOCK_PACKET requires. */
-    spkt.spkt_family = AF_PACKET;
-    strncpy((char *) spkt.spkt_device, netdev->name, sizeof spkt.spkt_device);
-    spkt.spkt_protocol = eh->eth_type;
-
     do {
-        n_bytes = sendto(netdev->fd, buffer->data, buffer->size, 0,
-                         (const struct sockaddr *) &spkt, sizeof spkt);
+        n_bytes = sendto(netdev->fd, buffer->data, buffer->size, 0, NULL, 0);
     } while (n_bytes < 0 && errno == EINTR);
 
     if (n_bytes < 0) {
@@ -549,6 +539,75 @@ netdev_get_in4(const struct netdev *netdev, struct in_addr *in4)
     return in4->s_addr != INADDR_ANY;
 }
 
+static void
+make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
+{
+    struct sockaddr_in sin;
+    memset(&sin, 0, sizeof sin);
+    sin.sin_family = AF_INET;
+    sin.sin_addr = addr;
+    sin.sin_port = 0;
+
+    memset(sa, 0, sizeof *sa);
+    memcpy(sa, &sin, sizeof sin);
+}
+
+static int
+do_set_addr(struct netdev *netdev, int sock,
+            int ioctl_nr, const char *ioctl_name, struct in_addr addr)
+{
+    struct ifreq ifr;
+    int error;
+
+    strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
+    make_in4_sockaddr(&ifr.ifr_addr, addr);
+    error = ioctl(sock, ioctl_nr, &ifr) < 0 ? errno : 0;
+    if (error) {
+        VLOG_WARN("ioctl(%s): %s", ioctl_name, strerror(error));
+    }
+    return error;
+}
+
+/* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask.  If
+ * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared.  Returns a
+ * positive errno value. */
+int
+netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
+{
+    int error;
+
+    error = do_set_addr(netdev, af_inet_sock,
+                        SIOCSIFADDR, "SIOCSIFADDR", addr);
+    if (!error) {
+        netdev->in4 = addr;
+        if (addr.s_addr != INADDR_ANY) {
+            error = do_set_addr(netdev, af_inet_sock,
+                                SIOCSIFNETMASK, "SIOCSIFNETMASK", mask);
+        }
+    }
+    return error;
+}
+
+/* Adds 'router' as a default gateway for 'netdev''s IP address. */
+int
+netdev_add_router(struct netdev *netdev, struct in_addr router)
+{
+    struct in_addr any = { INADDR_ANY };
+    struct rtentry rt;
+    int error;
+
+    memset(&rt, 0, sizeof rt);
+    make_in4_sockaddr(&rt.rt_dst, any);
+    make_in4_sockaddr(&rt.rt_gateway, router);
+    make_in4_sockaddr(&rt.rt_genmask, any);
+    rt.rt_flags = RTF_UP | RTF_GATEWAY;
+    error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
+    if (error) {
+        VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error));
+    }
+    return error;
+}
+
 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
  * returns true.  Otherwise, returns false. */
 bool
@@ -580,10 +639,26 @@ netdev_get_flags(const struct netdev *netdev, enum netdev_flags *flagsp)
     return 0;
 }
 
-/* Sets the flags for 'netdev' to 'nd_flags'.
- * Returns 0 if successful, otherwise a positive errno value. */
-int
-netdev_set_flags(struct netdev *netdev, enum netdev_flags nd_flags)
+static int
+nd_to_iff_flags(enum netdev_flags nd)
+{
+    int iff = 0;
+    if (nd & NETDEV_UP) {
+        iff |= IFF_UP;
+    }
+    if (nd & NETDEV_PROMISC) {
+        iff |= IFF_PROMISC;
+    }
+    return iff;
+}
+
+/* On 'netdev', turns off the flags in 'off' and then turns on the flags in
+ * 'on'.  If 'permanent' is true, the changes will persist; otherwise, they
+ * will be reverted when 'netdev' is closed or the program exits.  Returns 0 if
+ * successful, otherwise a positive errno value. */
+static int
+do_update_flags(struct netdev *netdev, enum netdev_flags off,
+                enum netdev_flags on, bool permanent)
 {
     int old_flags, new_flags;
     int error;
@@ -593,12 +668,9 @@ netdev_set_flags(struct netdev *netdev, enum netdev_flags nd_flags)
         return error;
     }
 
-    new_flags = old_flags & ~(IFF_UP | IFF_PROMISC);
-    if (nd_flags & NETDEV_UP) {
-        new_flags |= IFF_UP;
-    }
-    if (nd_flags & NETDEV_PROMISC) {
-        new_flags |= IFF_PROMISC;
+    new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
+    if (!permanent) {
+        netdev->changed_flags |= new_flags ^ old_flags; 
     }
     if (new_flags != old_flags) {
         error = set_flags(netdev, new_flags);
@@ -606,6 +678,39 @@ netdev_set_flags(struct netdev *netdev, enum netdev_flags nd_flags)
     return error;
 }
 
+/* Sets the flags for 'netdev' to 'flags'.
+ * If 'permanent' is true, the changes will persist; otherwise, they
+ * will be reverted when 'netdev' is closed or the program exits.
+ * Returns 0 if successful, otherwise a positive errno value. */
+int
+netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
+                 bool permanent)
+{
+    return do_update_flags(netdev, -1, flags, permanent);
+}
+
+/* Turns on the specified 'flags' on 'netdev'.
+ * If 'permanent' is true, the changes will persist; otherwise, they
+ * will be reverted when 'netdev' is closed or the program exits.
+ * Returns 0 if successful, otherwise a positive errno value. */
+int
+netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
+                     bool permanent)
+{
+    return do_update_flags(netdev, 0, flags, permanent);
+}
+
+/* Turns off the specified 'flags' on 'netdev'.
+ * If 'permanent' is true, the changes will persist; otherwise, they
+ * will be reverted when 'netdev' is closed or the program exits.
+ * Returns 0 if successful, otherwise a positive errno value. */
+int
+netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
+                      bool permanent)
+{
+    return do_update_flags(netdev, flags, 0, permanent);
+}
+
 /* Looks up the ARP table entry for 'ip' on 'netdev'.  If one exists and can be
  * successfully retrieved, it stores the corresponding MAC address in 'mac' and
  * returns 0.  Otherwise, it returns a positive errno value; in particular,
@@ -663,6 +768,7 @@ static int
 restore_flags(struct netdev *netdev)
 {
     struct ifreq ifr;
+    int restore_flags;
 
     /* Get current flags. */
     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
@@ -671,9 +777,10 @@ restore_flags(struct netdev *netdev)
     }
 
     /* Restore flags that we might have changed, if necessary. */
-    if ((ifr.ifr_flags ^ netdev->save_flags) & (IFF_PROMISC | IFF_UP)) {
-        ifr.ifr_flags &= ~(IFF_PROMISC | IFF_UP);
-        ifr.ifr_flags |= netdev->save_flags & (IFF_PROMISC | IFF_UP);
+    restore_flags = netdev->changed_flags & (IFF_PROMISC | IFF_UP);
+    if ((ifr.ifr_flags ^ netdev->save_flags) & restore_flags) {
+        ifr.ifr_flags &= ~restore_flags;
+        ifr.ifr_flags |= netdev->save_flags & restore_flags;
         if (ioctl(netdev->fd, SIOCSIFFLAGS, &ifr) < 0) {
             return errno;
         }