1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWRE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
40 #include <arpa/inet.h>
42 #include <linux/if_tun.h>
43 #include <linux/types.h>
44 #include <linux/ethtool.h>
45 #include <linux/sockios.h>
46 #include <linux/version.h>
47 #include <sys/types.h>
48 #include <sys/ioctl.h>
49 #include <sys/socket.h>
50 #include <netpacket/packet.h>
51 #include <net/ethernet.h>
53 #include <net/if_arp.h>
54 #include <net/if_packet.h>
55 #include <net/route.h>
56 #include <netinet/in.h>
61 #include "fatal-signal.h"
64 #include "openflow/openflow.h"
66 #include "poll-loop.h"
67 #include "socket-util.h"
69 #define THIS_MODULE VLM_netdev
76 /* File descriptors. For ordinary network devices, the two fds below are
77 * the same; for tap devices, they differ. */
78 int netdev_fd; /* Network device. */
79 int tap_fd; /* TAP character device, if any, otherwise the
82 /* Cached network device information. */
84 uint8_t etheraddr[ETH_ADDR_LEN];
91 /* Bitmaps of OFPPF_* that describe features. All bits disabled if
92 * unsupported or unavailable. */
93 uint32_t curr; /* Current features. */
94 uint32_t advertised; /* Features being advertised by the port. */
95 uint32_t supported; /* Features supported by the port. */
96 uint32_t peer; /* Features advertised by the peer. */
98 int save_flags; /* Initial device flags. */
99 int changed_flags; /* Flags that we changed. */
102 /* All open network devices. */
103 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
105 /* An AF_INET socket (used for ioctl operations). */
106 static int af_inet_sock = -1;
108 /* This is set pretty low because we probably won't learn anything from the
109 * additional log messages. */
110 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
112 static void init_netdev(void);
113 static int do_open_netdev(const char *name, int ethertype, int tap_fd,
114 struct netdev **netdev_);
115 static int restore_flags(struct netdev *netdev);
116 static int get_flags(const struct netdev *, int fd, int *flagsp);
117 static int set_flags(struct netdev *, int fd, int flags);
119 /* Obtains the IPv6 address for 'name' into 'in6'. */
121 get_ipv6_address(const char *name, struct in6_addr *in6)
126 file = fopen("/proc/net/if_inet6", "r");
128 /* This most likely indicates that the host doesn't have IPv6 support,
129 * so it's not really a failure condition.*/
134 while (fgets(line, sizeof line, file)) {
135 uint8_t *s6 = in6->s6_addr;
139 if (sscanf(line, " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
140 "%*x %*x %*x %*x %16s\n",
141 &s6[0], &s6[1], &s6[2], &s6[3],
142 &s6[4], &s6[5], &s6[6], &s6[7],
143 &s6[8], &s6[9], &s6[10], &s6[11],
144 &s6[12], &s6[13], &s6[14], &s6[15],
146 && !strcmp(name, ifname))
157 do_ethtool(struct netdev *netdev)
160 struct ethtool_cmd ecmd;
163 netdev->supported = 0;
164 netdev->advertised = 0;
167 memset(&ifr, 0, sizeof ifr);
168 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
169 ifr.ifr_data = (caddr_t) &ecmd;
171 memset(&ecmd, 0, sizeof ecmd);
172 ecmd.cmd = ETHTOOL_GSET;
173 if (ioctl(netdev->netdev_fd, SIOCETHTOOL, &ifr) == 0) {
174 if (ecmd.supported & SUPPORTED_10baseT_Half) {
175 netdev->supported |= OFPPF_10MB_HD;
177 if (ecmd.supported & SUPPORTED_10baseT_Full) {
178 netdev->supported |= OFPPF_10MB_FD;
180 if (ecmd.supported & SUPPORTED_100baseT_Half) {
181 netdev->supported |= OFPPF_100MB_HD;
183 if (ecmd.supported & SUPPORTED_100baseT_Full) {
184 netdev->supported |= OFPPF_100MB_FD;
186 if (ecmd.supported & SUPPORTED_1000baseT_Half) {
187 netdev->supported |= OFPPF_1GB_HD;
189 if (ecmd.supported & SUPPORTED_1000baseT_Full) {
190 netdev->supported |= OFPPF_1GB_FD;
192 if (ecmd.supported & SUPPORTED_10000baseT_Full) {
193 netdev->supported |= OFPPF_10GB_FD;
195 if (ecmd.supported & SUPPORTED_TP) {
196 netdev->supported |= OFPPF_COPPER;
198 if (ecmd.supported & SUPPORTED_FIBRE) {
199 netdev->supported |= OFPPF_FIBER;
201 if (ecmd.supported & SUPPORTED_Autoneg) {
202 netdev->supported |= OFPPF_AUTONEG;
204 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
205 if (ecmd.supported & SUPPORTED_Pause) {
206 netdev->supported |= OFPPF_PAUSE;
208 if (ecmd.supported & SUPPORTED_Asym_Pause) {
209 netdev->supported |= OFPPF_PAUSE_ASYM;
211 #endif /* kernel >= 2.6.14 */
213 /* Set the advertised features */
214 if (ecmd.advertising & ADVERTISED_10baseT_Half) {
215 netdev->advertised |= OFPPF_10MB_HD;
217 if (ecmd.advertising & ADVERTISED_10baseT_Full) {
218 netdev->advertised |= OFPPF_10MB_FD;
220 if (ecmd.advertising & ADVERTISED_100baseT_Half) {
221 netdev->advertised |= OFPPF_100MB_HD;
223 if (ecmd.advertising & ADVERTISED_100baseT_Full) {
224 netdev->advertised |= OFPPF_100MB_FD;
226 if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
227 netdev->advertised |= OFPPF_1GB_HD;
229 if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
230 netdev->advertised |= OFPPF_1GB_FD;
232 if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
233 netdev->advertised |= OFPPF_10GB_FD;
235 if (ecmd.advertising & ADVERTISED_TP) {
236 netdev->advertised |= OFPPF_COPPER;
238 if (ecmd.advertising & ADVERTISED_FIBRE) {
239 netdev->advertised |= OFPPF_FIBER;
241 if (ecmd.advertising & ADVERTISED_Autoneg) {
242 netdev->advertised |= OFPPF_AUTONEG;
244 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
245 if (ecmd.advertising & ADVERTISED_Pause) {
246 netdev->advertised |= OFPPF_PAUSE;
248 if (ecmd.advertising & ADVERTISED_Asym_Pause) {
249 netdev->advertised |= OFPPF_PAUSE_ASYM;
251 #endif /* kernel >= 2.6.14 */
253 /* Set the current features */
254 if (ecmd.speed == SPEED_10) {
255 netdev->curr = (ecmd.duplex) ? OFPPF_10MB_FD : OFPPF_10MB_HD;
257 else if (ecmd.speed == SPEED_100) {
258 netdev->curr = (ecmd.duplex) ? OFPPF_100MB_FD : OFPPF_100MB_HD;
260 else if (ecmd.speed == SPEED_1000) {
261 netdev->curr = (ecmd.duplex) ? OFPPF_1GB_FD : OFPPF_1GB_HD;
263 else if (ecmd.speed == SPEED_10000) {
264 netdev->curr = OFPPF_10GB_FD;
267 if (ecmd.port == PORT_TP) {
268 netdev->curr |= OFPPF_COPPER;
270 else if (ecmd.port == PORT_FIBRE) {
271 netdev->curr |= OFPPF_FIBER;
275 netdev->curr |= OFPPF_AUTONEG;
278 VLOG_DBG("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
282 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
283 * successful, otherwise a positive errno value. On success, sets '*netdevp'
284 * to the new network device, otherwise to null.
286 * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
287 * capture frames of that type received on the device. It may also be one of
288 * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
291 netdev_open(const char *name, int ethertype, struct netdev **netdevp)
293 if (!strncmp(name, "tap:", 4)) {
294 return netdev_open_tap(name + 4, netdevp);
296 return do_open_netdev(name, ethertype, -1, netdevp);
300 /* Opens a TAP virtual network device. If 'name' is a nonnull, non-empty
301 * string, attempts to assign that name to the TAP device (failing if the name
302 * is already in use); otherwise, a name is automatically assigned. Returns
303 * zero if successful, otherwise a positive errno value. On success, sets
304 * '*netdevp' to the new network device, otherwise to null. */
306 netdev_open_tap(const char *name, struct netdev **netdevp)
308 static const char tap_dev[] = "/dev/net/tun";
313 tap_fd = open(tap_dev, O_RDWR);
315 ofp_error(errno, "opening \"%s\" failed", tap_dev);
319 memset(&ifr, 0, sizeof ifr);
320 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
322 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
324 if (ioctl(tap_fd, TUNSETIFF, &ifr) < 0) {
326 ofp_error(error, "ioctl(TUNSETIFF) on \"%s\" failed", tap_dev);
331 error = set_nonblocking(tap_fd);
333 ofp_error(error, "set_nonblocking on \"%s\" failed", tap_dev);
338 error = do_open_netdev(ifr.ifr_name, NETDEV_ETH_TYPE_NONE, tap_fd,
347 do_open_netdev(const char *name, int ethertype, int tap_fd,
348 struct netdev **netdev_)
351 struct sockaddr_ll sll;
353 unsigned int ifindex;
354 uint8_t etheraddr[ETH_ADDR_LEN];
360 struct netdev *netdev;
365 /* Create raw socket. */
366 netdev_fd = socket(PF_PACKET, SOCK_RAW,
367 htons(ethertype == NETDEV_ETH_TYPE_NONE ? 0
368 : ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
369 : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
375 /* Set non-blocking mode. */
376 error = set_nonblocking(netdev_fd);
378 goto error_already_set;
381 /* Get ethernet device index. */
382 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
383 if (ioctl(netdev_fd, SIOCGIFINDEX, &ifr) < 0) {
384 VLOG_ERR("ioctl(SIOCGIFINDEX) on %s device failed: %s",
385 name, strerror(errno));
388 ifindex = ifr.ifr_ifindex;
390 /* Bind to specific ethernet device. */
391 memset(&sll, 0, sizeof sll);
392 sll.sll_family = AF_PACKET;
393 sll.sll_ifindex = ifindex;
394 if (bind(netdev_fd, (struct sockaddr *) &sll, sizeof sll) < 0) {
395 VLOG_ERR("bind to %s failed: %s", name, strerror(errno));
399 if (ethertype != NETDEV_ETH_TYPE_NONE) {
400 /* Between the socket() and bind() calls above, the socket receives all
401 * packets of the requested type on all system interfaces. We do not
402 * want to receive that data, but there is no way to avoid it. So we
403 * must now drain out the receive queue. */
404 error = drain_rcvbuf(netdev_fd);
410 /* Get MAC address. */
411 if (ioctl(netdev_fd, SIOCGIFHWADDR, &ifr) < 0) {
412 VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
413 name, strerror(errno));
416 hwaddr_family = ifr.ifr_hwaddr.sa_family;
417 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
418 VLOG_WARN("%s device has unknown hardware address family %d",
419 name, hwaddr_family);
421 memcpy(etheraddr, ifr.ifr_hwaddr.sa_data, sizeof etheraddr);
424 if (ioctl(netdev_fd, SIOCGIFMTU, &ifr) < 0) {
425 VLOG_ERR("ioctl(SIOCGIFMTU) on %s device failed: %s",
426 name, strerror(errno));
431 /* Get TX queue length. */
432 if (ioctl(netdev_fd, SIOCGIFTXQLEN, &ifr) < 0) {
433 VLOG_ERR("ioctl(SIOCGIFTXQLEN) on %s device failed: %s",
434 name, strerror(errno));
437 txqlen = ifr.ifr_qlen;
439 get_ipv6_address(name, &in6);
441 /* Allocate network device. */
442 netdev = xmalloc(sizeof *netdev);
443 netdev->name = xstrdup(name);
444 netdev->ifindex = ifindex;
445 netdev->txqlen = txqlen;
446 netdev->hwaddr_family = hwaddr_family;
447 netdev->netdev_fd = netdev_fd;
448 netdev->tap_fd = tap_fd < 0 ? netdev_fd : tap_fd;
449 memcpy(netdev->etheraddr, etheraddr, sizeof etheraddr);
453 /* Get speed, features. */
456 /* Save flags to restore at close or exit. */
457 error = get_flags(netdev, netdev_fd, &netdev->save_flags);
459 goto error_already_set;
461 netdev->changed_flags = 0;
462 fatal_signal_block();
463 list_push_back(&netdev_list, &netdev->node);
464 fatal_signal_unblock();
480 /* Closes and destroys 'netdev'. */
482 netdev_close(struct netdev *netdev)
485 /* Bring down interface and drop promiscuous mode, if we brought up
486 * the interface or enabled promiscuous mode. */
488 fatal_signal_block();
489 error = restore_flags(netdev);
490 list_remove(&netdev->node);
491 fatal_signal_unblock();
493 VLOG_WARN("failed to restore network device flags on %s: %s",
494 netdev->name, strerror(error));
499 close(netdev->netdev_fd);
500 if (netdev->netdev_fd != netdev->tap_fd) {
501 close(netdev->tap_fd);
507 /* Pads 'buffer' out with zero-bytes to the minimum valid length of an
508 * Ethernet packet, if necessary. */
510 pad_to_minimum_length(struct ofpbuf *buffer)
512 if (buffer->size < ETH_TOTAL_MIN) {
513 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
517 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
518 * must have initialized with sufficient room for the packet. The space
519 * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
520 * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
521 * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
522 * need not be included.)
524 * If a packet is successfully retrieved, returns 0. In this case 'buffer' is
525 * guaranteed to contain at least ETH_TOTAL_MIN bytes. Otherwise, returns a
526 * positive errno value. Returns EAGAIN immediately if no packet is ready to
530 netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
534 assert(buffer->size == 0);
535 assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
537 n_bytes = read(netdev->tap_fd,
538 ofpbuf_tail(buffer), ofpbuf_tailroom(buffer));
539 } while (n_bytes < 0 && errno == EINTR);
541 if (errno != EAGAIN) {
542 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
543 strerror(errno), netdev->name);
547 buffer->size += n_bytes;
549 /* When the kernel internally sends out an Ethernet frame on an
550 * interface, it gives us a copy *before* padding the frame to the
551 * minimum length. Thus, when it sends out something like an ARP
552 * request, we see a too-short frame. So pad it out to the minimum
554 pad_to_minimum_length(buffer);
559 /* Registers with the poll loop to wake up from the next call to poll_block()
560 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
562 netdev_recv_wait(struct netdev *netdev)
564 poll_fd_wait(netdev->tap_fd, POLLIN);
567 /* Discards all packets waiting to be received from 'netdev'. */
569 netdev_drain(struct netdev *netdev)
571 if (netdev->tap_fd != netdev->netdev_fd) {
572 drain_fd(netdev->tap_fd, netdev->txqlen);
575 return drain_rcvbuf(netdev->netdev_fd);
579 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
580 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
581 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
582 * the packet is too big or too small to transmit on the device.
584 * The caller retains ownership of 'buffer' in all cases.
586 * The kernel maintains a packet transmission queue, so the caller is not
587 * expected to do additional queuing of packets. */
589 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
594 n_bytes = write(netdev->tap_fd, buffer->data, buffer->size);
595 } while (n_bytes < 0 && errno == EINTR);
598 /* The Linux AF_PACKET implementation never blocks waiting for room
599 * for packets, instead returning ENOBUFS. Translate this into EAGAIN
601 if (errno == ENOBUFS) {
603 } else if (errno != EAGAIN) {
604 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
605 netdev->name, strerror(errno));
608 } else if (n_bytes != buffer->size) {
610 "send partial Ethernet packet (%d bytes of %zu) on %s",
611 (int) n_bytes, buffer->size, netdev->name);
618 /* Registers with the poll loop to wake up from the next call to poll_block()
619 * when the packet transmission queue has sufficient room to transmit a packet
620 * with netdev_send().
622 * The kernel maintains a packet transmission queue, so the client is not
623 * expected to do additional queuing of packets. Thus, this function is
624 * unlikely to ever be used. It is included for completeness. */
626 netdev_send_wait(struct netdev *netdev)
628 if (netdev->tap_fd == netdev->netdev_fd) {
629 poll_fd_wait(netdev->tap_fd, POLLOUT);
631 /* TAP device always accepts packets.*/
632 poll_immediate_wake();
636 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
637 * otherwise a positive errno value. */
639 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
643 memset(&ifr, 0, sizeof ifr);
644 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
645 ifr.ifr_hwaddr.sa_family = netdev->hwaddr_family;
646 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
647 if (ioctl(netdev->netdev_fd, SIOCSIFHWADDR, &ifr) < 0) {
648 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
649 netdev->name, strerror(errno));
652 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
656 /* Returns a pointer to 'netdev''s MAC address. The caller must not modify or
657 * free the returned buffer. */
659 netdev_get_etheraddr(const struct netdev *netdev)
661 return netdev->etheraddr;
664 /* Returns the name of the network device that 'netdev' represents,
665 * e.g. "eth0". The caller must not modify or free the returned string. */
667 netdev_get_name(const struct netdev *netdev)
672 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
673 * in bytes, not including the hardware header; thus, this is typically 1500
674 * bytes for Ethernet devices. */
676 netdev_get_mtu(const struct netdev *netdev)
681 /* Checks the link status. Returns 1 or 0 to indicate the link is active
682 * or not, respectively. Any other return value indicates an error. */
684 netdev_get_link_status(const struct netdev *netdev)
687 struct ethtool_value edata;
689 memset(&ifr, 0, sizeof ifr);
690 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
691 ifr.ifr_data = (caddr_t) &edata;
693 memset(&edata, 0, sizeof edata);
694 edata.cmd = ETHTOOL_GLINK;
695 if (ioctl(netdev->netdev_fd, SIOCETHTOOL, &ifr) == 0) {
706 /* Returns the features supported by 'netdev' of type 'type', as a bitmap
707 * of bits from enum ofp_phy_features, in host byte order. */
709 netdev_get_features(struct netdev *netdev, int type)
713 case NETDEV_FEAT_CURRENT:
715 case NETDEV_FEAT_ADVERTISED:
716 return netdev->advertised;
717 case NETDEV_FEAT_SUPPORTED:
718 return netdev->supported;
719 case NETDEV_FEAT_PEER:
722 VLOG_WARN("Unknown feature type: %d\n", type);
727 /* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
728 * 'in4' is non-null) and returns true. Otherwise, returns false. */
730 netdev_get_in4(const struct netdev *netdev, struct in_addr *in4)
733 struct in_addr ip = { INADDR_ANY };
735 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
736 ifr.ifr_addr.sa_family = AF_INET;
737 if (ioctl(af_inet_sock, SIOCGIFADDR, &ifr) == 0) {
738 struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
741 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFADDR) failed: %s",
742 netdev->name, strerror(errno));
747 return ip.s_addr != INADDR_ANY;
751 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
753 struct sockaddr_in sin;
754 memset(&sin, 0, sizeof sin);
755 sin.sin_family = AF_INET;
759 memset(sa, 0, sizeof *sa);
760 memcpy(sa, &sin, sizeof sin);
764 do_set_addr(struct netdev *netdev, int sock,
765 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
770 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
771 make_in4_sockaddr(&ifr.ifr_addr, addr);
772 error = ioctl(sock, ioctl_nr, &ifr) < 0 ? errno : 0;
774 VLOG_WARN("ioctl(%s): %s", ioctl_name, strerror(error));
779 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
780 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
781 * positive errno value. */
783 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
787 error = do_set_addr(netdev, af_inet_sock,
788 SIOCSIFADDR, "SIOCSIFADDR", addr);
789 if (!error && addr.s_addr != INADDR_ANY) {
790 error = do_set_addr(netdev, af_inet_sock,
791 SIOCSIFNETMASK, "SIOCSIFNETMASK", mask);
796 /* Adds 'router' as a default gateway for 'netdev''s IP address. */
798 netdev_add_router(struct netdev *netdev, struct in_addr router)
800 struct in_addr any = { INADDR_ANY };
804 memset(&rt, 0, sizeof rt);
805 make_in4_sockaddr(&rt.rt_dst, any);
806 make_in4_sockaddr(&rt.rt_gateway, router);
807 make_in4_sockaddr(&rt.rt_genmask, any);
808 rt.rt_flags = RTF_UP | RTF_GATEWAY;
809 error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
811 VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error));
816 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
817 * 'in6' is non-null) and returns true. Otherwise, returns false. */
819 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
824 return memcmp(&netdev->in6, &in6addr_any, sizeof netdev->in6) != 0;
827 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
828 * Returns 0 if successful, otherwise a positive errno value. */
830 netdev_get_flags(const struct netdev *netdev, enum netdev_flags *flagsp)
834 error = get_flags(netdev, netdev->netdev_fd, &flags);
840 if (flags & IFF_UP) {
841 *flagsp |= NETDEV_UP;
843 if (flags & IFF_PROMISC) {
844 *flagsp |= NETDEV_PROMISC;
850 nd_to_iff_flags(enum netdev_flags nd)
853 if (nd & NETDEV_UP) {
856 if (nd & NETDEV_PROMISC) {
862 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
863 * 'on'. If 'permanent' is true, the changes will persist; otherwise, they
864 * will be reverted when 'netdev' is closed or the program exits. Returns 0 if
865 * successful, otherwise a positive errno value. */
867 do_update_flags(struct netdev *netdev, int fd, enum netdev_flags off,
868 enum netdev_flags on, bool permanent)
870 int old_flags, new_flags;
873 error = get_flags(netdev, fd, &old_flags);
878 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
880 netdev->changed_flags |= new_flags ^ old_flags;
882 if (new_flags != old_flags) {
883 error = set_flags(netdev, fd, new_flags);
888 /* Sets the flags for 'netdev' to 'flags'.
889 * If 'permanent' is true, the changes will persist; otherwise, they
890 * will be reverted when 'netdev' is closed or the program exits.
891 * Returns 0 if successful, otherwise a positive errno value. */
893 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
896 return do_update_flags(netdev, netdev->netdev_fd, -1, flags, permanent);
899 /* Turns on the specified 'flags' on 'netdev'.
900 * If 'permanent' is true, the changes will persist; otherwise, they
901 * will be reverted when 'netdev' is closed or the program exits.
902 * Returns 0 if successful, otherwise a positive errno value. */
904 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
907 return do_update_flags(netdev, netdev->netdev_fd, 0, flags, permanent);
910 /* Turns off the specified 'flags' on 'netdev'.
911 * If 'permanent' is true, the changes will persist; otherwise, they
912 * will be reverted when 'netdev' is closed or the program exits.
913 * Returns 0 if successful, otherwise a positive errno value. */
915 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
918 return do_update_flags(netdev, netdev->netdev_fd, flags, 0, permanent);
921 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
922 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
923 * returns 0. Otherwise, it returns a positive errno value; in particular,
924 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
926 netdev_arp_lookup(const struct netdev *netdev,
927 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
930 struct sockaddr_in *pa;
933 memset(&r, 0, sizeof r);
934 pa = (struct sockaddr_in *) &r.arp_pa;
935 pa->sin_family = AF_INET;
936 pa->sin_addr.s_addr = ip;
938 r.arp_ha.sa_family = ARPHRD_ETHER;
940 strncpy(r.arp_dev, netdev->name, sizeof r.arp_dev);
941 retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
943 memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
944 } else if (retval != ENXIO) {
945 VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
946 netdev->name, IP_ARGS(&ip), strerror(retval));
951 static void restore_all_flags(void *aux);
953 /* Set up a signal hook to restore network device flags on program
961 fatal_signal_add_hook(restore_all_flags, NULL, true);
962 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
963 if (af_inet_sock < 0) {
964 ofp_fatal(errno, "socket(AF_INET)");
969 /* Restore the network device flags on 'netdev' to those that were active
970 * before we changed them. Returns 0 if successful, otherwise a positive
973 * To avoid reentry, the caller must ensure that fatal signals are blocked. */
975 restore_flags(struct netdev *netdev)
980 /* Get current flags. */
981 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
982 if (ioctl(netdev->netdev_fd, SIOCGIFFLAGS, &ifr) < 0) {
986 /* Restore flags that we might have changed, if necessary. */
987 restore_flags = netdev->changed_flags & (IFF_PROMISC | IFF_UP);
988 if ((ifr.ifr_flags ^ netdev->save_flags) & restore_flags) {
989 ifr.ifr_flags &= ~restore_flags;
990 ifr.ifr_flags |= netdev->save_flags & restore_flags;
991 if (ioctl(netdev->netdev_fd, SIOCSIFFLAGS, &ifr) < 0) {
999 /* Retores all the flags on all network devices that we modified. Called from
1000 * a signal handler, so it does not attempt to report error conditions. */
1002 restore_all_flags(void *aux UNUSED)
1004 struct netdev *netdev;
1005 LIST_FOR_EACH (netdev, struct netdev, node, &netdev_list) {
1006 restore_flags(netdev);
1011 get_flags(const struct netdev *netdev, int fd, int *flags)
1014 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
1015 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
1016 VLOG_ERR("ioctl(SIOCGIFFLAGS) on %s device failed: %s",
1017 netdev->name, strerror(errno));
1020 *flags = ifr.ifr_flags;
1025 set_flags(struct netdev *netdev, int fd, int flags)
1028 strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
1029 ifr.ifr_flags = flags;
1030 if (ioctl(netdev->netdev_fd, SIOCSIFFLAGS, &ifr) < 0) {
1031 VLOG_ERR("ioctl(SIOCSIFFLAGS) on %s device failed: %s",
1032 netdev->name, strerror(errno));