New function netdev_arp_lookup().
[sliver-openvswitch.git] / lib / netdev.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
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:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE 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
27  * SOFTWARE.
28  * 
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.
32  */
33
34 #include "netdev.h"
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <arpa/inet.h>
39 #include <inttypes.h>
40 #include <linux/types.h>
41 #include <linux/ethtool.h>
42 #include <linux/sockios.h>
43 #include <sys/types.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <netpacket/packet.h>
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_arp.h>
50 #include <net/if_packet.h>
51 #include <netinet/in.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include "list.h"
57 #include "fatal-signal.h"
58 #include "buffer.h"
59 #include "openflow.h"
60 #include "packets.h"
61 #include "poll-loop.h"
62
63 #define THIS_MODULE VLM_netdev
64 #include "vlog.h"
65
66 struct netdev {
67     struct list node;
68     char *name;
69     int fd;
70     uint8_t etheraddr[ETH_ADDR_LEN];
71     int speed;
72     int mtu;
73     uint32_t features;
74     struct in_addr in4;
75     struct in6_addr in6;
76     int save_flags;
77 };
78
79 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
80
81 /* An AF_INET socket (used for ioctl operations). */
82 static int af_inet_sock = -1;
83
84 static void init_netdev(void);
85 static int restore_flags(struct netdev *netdev);
86 static int get_flags(const struct netdev *, int *flagsp);
87 static int set_flags(struct netdev *, int flags);
88
89 /* Obtains the IPv4 address for 'name' into 'in4'.  Returns true if
90  * successful. */
91 static bool
92 get_ipv4_address(const char *name, struct in_addr *in4)
93 {
94     struct ifreq ifr;
95
96     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
97     ifr.ifr_addr.sa_family = AF_INET;
98     if (ioctl(af_inet_sock, SIOCGIFADDR, &ifr) == 0) {
99         struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
100         *in4 = sin->sin_addr;
101     } else {
102         in4->s_addr = INADDR_ANY;
103     }
104
105     return true;
106 }
107
108 /* Obtains the IPv6 address for 'name' into 'in6'. */
109 static void
110 get_ipv6_address(const char *name, struct in6_addr *in6)
111 {
112     FILE *file;
113     char line[128];
114
115     file = fopen("/proc/net/if_inet6", "r");
116     if (file == NULL) {
117         /* This most likely indicates that the host doesn't have IPv6 support,
118          * so it's not really a failure condition.*/
119         *in6 = in6addr_any;
120         return;
121     }
122
123     while (fgets(line, sizeof line, file)) {
124         uint8_t *s6 = in6->s6_addr;
125         char ifname[16 + 1];
126
127 #define X8 "%2"SCNx8
128         if (sscanf(line, " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
129                    "%*x %*x %*x %*x %16s\n",
130                    &s6[0], &s6[1], &s6[2], &s6[3],
131                    &s6[4], &s6[5], &s6[6], &s6[7],
132                    &s6[8], &s6[9], &s6[10], &s6[11],
133                    &s6[12], &s6[13], &s6[14], &s6[15],
134                    ifname) == 17
135             && !strcmp(name, ifname))
136         {
137             return;
138         }
139     }
140     *in6 = in6addr_any;
141
142     fclose(file);
143 }
144
145 static void
146 do_ethtool(struct netdev *netdev) 
147 {
148     struct ifreq ifr;
149     struct ethtool_cmd ecmd;
150
151     netdev->speed = 0;
152     netdev->features = 0;
153
154     memset(&ifr, 0, sizeof ifr);
155     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
156     ifr.ifr_data = (caddr_t) &ecmd;
157
158     memset(&ecmd, 0, sizeof ecmd);
159     ecmd.cmd = ETHTOOL_GSET;
160     if (ioctl(netdev->fd, SIOCETHTOOL, &ifr) == 0) {
161         if (ecmd.supported & SUPPORTED_10baseT_Half) {
162             netdev->features |= OFPPF_10MB_HD;
163         }
164         if (ecmd.supported & SUPPORTED_10baseT_Full) {
165             netdev->features |= OFPPF_10MB_FD;
166         }
167         if (ecmd.supported & SUPPORTED_100baseT_Half)  {
168             netdev->features |= OFPPF_100MB_HD;
169         }
170         if (ecmd.supported & SUPPORTED_100baseT_Full) {
171             netdev->features |= OFPPF_100MB_FD;
172         }
173         if (ecmd.supported & SUPPORTED_1000baseT_Half) {
174             netdev->features |= OFPPF_1GB_HD;
175         }
176         if (ecmd.supported & SUPPORTED_1000baseT_Full) {
177             netdev->features |= OFPPF_1GB_FD;
178         }
179         /* 10Gbps half-duplex doesn't exist... */
180         if (ecmd.supported & SUPPORTED_10000baseT_Full) {
181             netdev->features |= OFPPF_10GB_FD;
182         }
183
184         switch (ecmd.speed) {
185         case SPEED_10:
186             netdev->speed = 10;
187             break;
188
189         case SPEED_100:
190             netdev->speed = 100;
191             break;
192
193         case SPEED_1000:
194             netdev->speed = 1000;
195             break;
196
197         case SPEED_2500:
198             netdev->speed = 2500;
199             break;
200
201         case SPEED_10000:
202             netdev->speed = 10000;
203             break;
204         }
205     } else {
206         VLOG_DBG("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
207     }
208 }
209
210 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
211  * successful, otherwise a positive errno value.  On success, sets '*netdev'
212  * to the new network device, otherwise to null. */
213 int
214 netdev_open(const char *name, struct netdev **netdev_)
215 {
216     int fd;
217     struct sockaddr sa;
218     struct ifreq ifr;
219     unsigned int ifindex;
220     socklen_t rcvbuf_len;
221     size_t rcvbuf;
222     uint8_t etheraddr[ETH_ADDR_LEN];
223     struct in_addr in4;
224     struct in6_addr in6;
225     int mtu;
226     int error;
227     struct netdev *netdev;
228
229     *netdev_ = NULL;
230     init_netdev();
231
232     /* Create raw socket.
233      *
234      * We have to use SOCK_PACKET, despite its deprecation, because only
235      * SOCK_PACKET lets us set the hardware source address of outgoing
236      * packets. */
237     fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL));
238     if (fd < 0) {
239         return errno;
240     }
241
242     /* Bind to specific ethernet device. */
243     memset(&sa, 0, sizeof sa);
244     sa.sa_family = AF_UNSPEC;
245     strncpy((char *) sa.sa_data, name, sizeof sa.sa_data);
246     if (bind(fd, &sa, sizeof sa) < 0) {
247         VLOG_ERR("bind to %s failed: %s", name, strerror(errno));
248         goto error;
249     }
250
251     /* Between the socket() and bind() calls above, the socket receives all
252      * packets on all system interfaces.  We do not want to receive that
253      * data, but there is no way to avoid it.  So we must now drain out the
254      * receive queue.  There is no way to know how long the receive queue is,
255      * but we know that the total number of bytes queued does not exceed the
256      * receive buffer size, so we pull packets until none are left or we've
257      * read that many bytes. */
258     rcvbuf_len = sizeof rcvbuf;
259     if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &rcvbuf_len) < 0) {
260         VLOG_ERR("getsockopt(SO_RCVBUF) on %s device failed: %s",
261                  name, strerror(errno));
262         goto error;
263     }
264     while (rcvbuf > 0) {
265         char buffer;
266         ssize_t n_bytes = recv(fd, &buffer, 1, MSG_TRUNC | MSG_DONTWAIT);
267         if (n_bytes <= 0) {
268             break;
269         }
270         rcvbuf -= n_bytes;
271     }
272
273     /* Get ethernet device index. */
274     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
275     if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
276         VLOG_ERR("ioctl(SIOCGIFINDEX) on %s device failed: %s",
277                  name, strerror(errno));
278         goto error;
279     }
280     ifindex = ifr.ifr_ifindex;
281
282     /* Get MAC address. */
283     if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
284         VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
285                  name, strerror(errno));
286         goto error;
287     }
288     if (ifr.ifr_hwaddr.sa_family != AF_UNSPEC
289         && ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
290         VLOG_WARN("%s device has unknown hardware address family %d",
291                   name, (int) ifr.ifr_hwaddr.sa_family);
292     }
293     memcpy(etheraddr, ifr.ifr_hwaddr.sa_data, sizeof etheraddr);
294
295     /* Get MTU. */
296     if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
297         VLOG_ERR("ioctl(SIOCGIFMTU) on %s device failed: %s",
298                  name, strerror(errno));
299         goto error;
300     }
301     mtu = ifr.ifr_mtu;
302
303     if (!get_ipv4_address(name, &in4)) {
304         goto error;
305     }
306     get_ipv6_address(name, &in6);
307
308     /* Allocate network device. */
309     netdev = xmalloc(sizeof *netdev);
310     netdev->name = xstrdup(name);
311     netdev->fd = fd;
312     memcpy(netdev->etheraddr, etheraddr, sizeof etheraddr);
313     netdev->mtu = mtu;
314     netdev->in4 = in4;
315     netdev->in6 = in6;
316
317     /* Get speed, features. */
318     do_ethtool(netdev);
319
320     /* Save flags to restore at close or exit. */
321     error = get_flags(netdev, &netdev->save_flags);
322     if (error) {
323         goto preset_error;
324     }
325     fatal_signal_block();
326     list_push_back(&netdev_list, &netdev->node);
327     fatal_signal_unblock();
328
329     /* Success! */
330     *netdev_ = netdev;
331     return 0;
332
333 error:
334     error = errno;
335 preset_error:
336     close(fd);
337     return error;
338 }
339
340 /* Closes and destroys 'netdev'. */
341 void
342 netdev_close(struct netdev *netdev)
343 {
344     if (netdev) {
345         /* Bring down interface and drop promiscuous mode, if we brought up
346          * the interface or enabled promiscuous mode. */
347         int error;
348         fatal_signal_block();
349         error = restore_flags(netdev);
350         list_remove(&netdev->node);
351         fatal_signal_unblock();
352         if (error) {
353             VLOG_WARN("failed to restore network device flags on %s: %s",
354                       netdev->name, strerror(error));
355         }
356
357         /* Free. */
358         free(netdev->name);
359         close(netdev->fd);
360         free(netdev);
361     }
362 }
363
364 /* Pads 'buffer' out with zero-bytes to the minimum valid length of an
365  * Ethernet packet, if necessary.  */
366 static void
367 pad_to_minimum_length(struct buffer *buffer)
368 {
369     if (buffer->size < ETH_TOTAL_MIN) {
370         size_t shortage = ETH_TOTAL_MIN - buffer->size;
371         memset(buffer_put_uninit(buffer, shortage), 0, shortage);
372     }
373 }
374
375 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
376  * must have initialized with sufficient room for the packet.  The space
377  * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
378  * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
379  * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
380  * need not be included.)
381  *
382  * If a packet is successfully retrieved, returns 0.  In this case 'buffer' is
383  * guaranteed to contain at least ETH_TOTAL_MIN bytes.  Otherwise, returns a
384  * positive errno value.  Returns EAGAIN immediately if no packet is ready to
385  * be returned.
386  */
387 int
388 netdev_recv(struct netdev *netdev, struct buffer *buffer)
389 {
390     ssize_t n_bytes;
391
392     assert(buffer->size == 0);
393     assert(buffer_tailroom(buffer) >= ETH_TOTAL_MIN);
394     do {
395         n_bytes = recv(netdev->fd,
396                        buffer_tail(buffer), buffer_tailroom(buffer),
397                        MSG_DONTWAIT);
398     } while (n_bytes < 0 && errno == EINTR);
399     if (n_bytes < 0) {
400         if (errno != EAGAIN) {
401             VLOG_WARN("error receiving Ethernet packet on %s: %s",
402                       strerror(errno), netdev->name);
403         }
404         return errno;
405     } else {
406         buffer->size += n_bytes;
407
408         /* When the kernel internally sends out an Ethernet frame on an
409          * interface, it gives us a copy *before* padding the frame to the
410          * minimum length.  Thus, when it sends out something like an ARP
411          * request, we see a too-short frame.  So pad it out to the minimum
412          * length. */
413         pad_to_minimum_length(buffer);
414         return 0;
415     }
416 }
417
418 /* Registers with the poll loop to wake up from the next call to poll_block()
419  * when a packet is ready to be received with netdev_recv() on 'netdev'. */
420 void
421 netdev_recv_wait(struct netdev *netdev)
422 {
423     poll_fd_wait(netdev->fd, POLLIN);
424 }
425
426 /* Sends 'buffer' on 'netdev'.  Returns 0 if successful, otherwise a positive
427  * errno value.  Returns EAGAIN without blocking if the packet cannot be queued
428  * immediately.  Returns EMSGSIZE if a partial packet was transmitted or if
429  * the packet is too big to transmit on the device.
430  *
431  * The kernel maintains a packet transmission queue, so the caller is not
432  * expected to do additional queuing of packets. */
433 int
434 netdev_send(struct netdev *netdev, struct buffer *buffer)
435 {
436     ssize_t n_bytes;
437     const struct eth_header *eh;
438     struct sockaddr_pkt spkt;
439
440     /* Ensure packet is long enough.  (Although all incoming packets are at
441      * least ETH_TOTAL_MIN bytes long, we could have trimmed some data off a
442      * minimum-size packet, e.g. by dropping a vlan header.)
443      *
444      * The kernel does not require this, but it ensures that we always access
445      * valid memory in grabbing the sockaddr below. */
446     pad_to_minimum_length(buffer);
447
448     /* Construct packet sockaddr, which SOCK_PACKET requires. */
449     spkt.spkt_family = AF_PACKET;
450     strncpy((char *) spkt.spkt_device, netdev->name, sizeof spkt.spkt_device);
451     eh = buffer_at_assert(buffer, 0, sizeof *eh);
452     spkt.spkt_protocol = eh->eth_type;
453
454     do {
455         n_bytes = sendto(netdev->fd, buffer->data, buffer->size, 0,
456                          (const struct sockaddr *) &spkt, sizeof spkt);
457     } while (n_bytes < 0 && errno == EINTR);
458
459     if (n_bytes < 0) {
460         /* The Linux AF_PACKET implementation never blocks waiting for room
461          * for packets, instead returning ENOBUFS.  Translate this into EAGAIN
462          * for the caller. */
463         if (errno == ENOBUFS) {
464             return EAGAIN;
465         } else if (errno != EAGAIN) {
466             VLOG_WARN("error sending Ethernet packet on %s: %s",
467                       netdev->name, strerror(errno));
468         }
469         return errno;
470     } else if (n_bytes != buffer->size) {
471         VLOG_WARN("send partial Ethernet packet (%d bytes of %zu) on %s",
472                   (int) n_bytes, buffer->size, netdev->name);
473         return EMSGSIZE;
474     } else {
475         return 0;
476     }
477 }
478
479 /* Registers with the poll loop to wake up from the next call to poll_block()
480  * when the packet transmission queue has sufficient room to transmit a packet
481  * with netdev_send().
482  *
483  * The kernel maintains a packet transmission queue, so the client is not
484  * expected to do additional queuing of packets.  Thus, this function is
485  * unlikely to ever be used.  It is included for completeness. */
486 void
487 netdev_send_wait(struct netdev *netdev)
488 {
489     poll_fd_wait(netdev->fd, POLLOUT);
490 }
491
492 /* Returns a pointer to 'netdev''s MAC address.  The caller must not modify or
493  * free the returned buffer. */
494 const uint8_t *
495 netdev_get_etheraddr(const struct netdev *netdev)
496 {
497     return netdev->etheraddr;
498 }
499
500 /* Returns the name of the network device that 'netdev' represents,
501  * e.g. "eth0".  The caller must not modify or free the returned string. */
502 const char *
503 netdev_get_name(const struct netdev *netdev)
504 {
505     return netdev->name;
506 }
507
508 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
509  * in bytes, not including the hardware header; thus, this is typically 1500
510  * bytes for Ethernet devices. */
511 int
512 netdev_get_mtu(const struct netdev *netdev) 
513 {
514     return netdev->mtu;
515 }
516
517 /* Returns the current speed of the network device that 'netdev' represents, in
518  * megabits per second, or 0 if the speed is unknown. */
519 int
520 netdev_get_speed(const struct netdev *netdev) 
521 {
522     return netdev->speed;
523 }
524
525 /* Returns the features supported by 'netdev', as a bitmap of bits from enum
526  * ofp_phy_port, in host byte order. */
527 uint32_t
528 netdev_get_features(const struct netdev *netdev) 
529 {
530     return netdev->features;
531 }
532
533 /* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and
534  * returns true.  Otherwise, returns false. */
535 bool
536 netdev_get_in4(const struct netdev *netdev, struct in_addr *in4)
537 {
538     *in4 = netdev->in4;
539     return in4->s_addr != INADDR_ANY;
540 }
541
542 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
543  * returns true.  Otherwise, returns false. */
544 bool
545 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
546 {
547     *in6 = netdev->in6;
548     return memcmp(in6, &in6addr_any, sizeof *in6) != 0;
549 }
550
551 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
552  * Returns 0 if successful, otherwise a positive errno value. */
553 int
554 netdev_get_flags(const struct netdev *netdev, enum netdev_flags *flagsp)
555 {
556     int error, flags;
557
558     error = get_flags(netdev, &flags);
559     if (error) {
560         return error;
561     }
562
563     *flagsp = 0;
564     if (flags & IFF_UP) {
565         *flagsp |= NETDEV_UP;
566     }
567     if (flags & IFF_PROMISC) {
568         *flagsp |= NETDEV_PROMISC;
569     }
570     return 0;
571 }
572
573 /* Sets the flags for 'netdev' to 'nd_flags'.
574  * Returns 0 if successful, otherwise a positive errno value. */
575 int
576 netdev_set_flags(struct netdev *netdev, enum netdev_flags nd_flags)
577 {
578     int old_flags, new_flags;
579     int error;
580
581     error = get_flags(netdev, &old_flags);
582     if (error) {
583         return error;
584     }
585
586     new_flags = old_flags & ~(IFF_UP | IFF_PROMISC);
587     if (nd_flags & NETDEV_UP) {
588         new_flags |= IFF_UP;
589     }
590     if (nd_flags & NETDEV_PROMISC) {
591         new_flags |= IFF_PROMISC;
592     }
593     if (new_flags != old_flags) {
594         error = set_flags(netdev, new_flags);
595     }
596     return error;
597 }
598
599 /* Looks up the ARP table entry for 'ip' on 'netdev'.  If one exists and can be
600  * successfully retrieved, it stores the corresponding MAC address in 'mac' and
601  * returns 0.  Otherwise, it returns a positive errno value; in particular,
602  * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
603 int
604 netdev_arp_lookup(const struct netdev *netdev,
605                   uint32_t ip, uint8_t mac[ETH_ADDR_LEN]) 
606 {
607     struct arpreq r;
608     struct sockaddr_in *pa;
609     int retval;
610
611     memset(&r, 0, sizeof r);
612     pa = (struct sockaddr_in *) &r.arp_pa;
613     pa->sin_family = AF_INET;
614     pa->sin_addr.s_addr = ip;
615     pa->sin_port = 0;
616     r.arp_ha.sa_family = ARPHRD_ETHER;
617     r.arp_flags = 0;
618     strncpy(r.arp_dev, netdev->name, sizeof r.arp_dev);
619     retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
620     if (!retval) {
621         memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
622     } else if (retval != ENXIO) {
623         VLOG_WARN("%s: could not look up ARP entry for "IP_FMT": %s",
624                   netdev->name, IP_ARGS(&ip), strerror(retval));
625     }
626     return retval;
627 }
628 \f
629 static void restore_all_flags(void *aux);
630
631 /* Set up a signal hook to restore network device flags on program
632  * termination.  */
633 static void
634 init_netdev(void)
635 {
636     static bool inited;
637     if (!inited) {
638         inited = true;
639         fatal_signal_add_hook(restore_all_flags, NULL);
640         af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
641         if (af_inet_sock < 0) {
642             fatal(errno, "socket(AF_INET)");
643         }
644     }
645 }
646
647 /* Restore the network device flags on 'netdev' to those that were active
648  * before we changed them.  Returns 0 if successful, otherwise a positive
649  * errno value.
650  *
651  * To avoid reentry, the caller must ensure that fatal signals are blocked. */
652 static int
653 restore_flags(struct netdev *netdev)
654 {
655     struct ifreq ifr;
656
657     /* Get current flags. */
658     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
659     if (ioctl(netdev->fd, SIOCGIFFLAGS, &ifr) < 0) {
660         return errno;
661     }
662
663     /* Restore flags that we might have changed, if necessary. */
664     if ((ifr.ifr_flags ^ netdev->save_flags) & (IFF_PROMISC | IFF_UP)) {
665         ifr.ifr_flags &= ~(IFF_PROMISC | IFF_UP);
666         ifr.ifr_flags |= netdev->save_flags & (IFF_PROMISC | IFF_UP);
667         if (ioctl(netdev->fd, SIOCSIFFLAGS, &ifr) < 0) {
668             return errno;
669         }
670     }
671
672     return 0;
673 }
674
675 /* Retores all the flags on all network devices that we modified.  Called from
676  * a signal handler, so it does not attempt to report error conditions. */
677 static void
678 restore_all_flags(void *aux UNUSED)
679 {
680     struct netdev *netdev;
681     LIST_FOR_EACH (netdev, struct netdev, node, &netdev_list) {
682         restore_flags(netdev);
683     }
684 }
685
686 static int
687 get_flags(const struct netdev *netdev, int *flags)
688 {
689     struct ifreq ifr;
690     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
691     if (ioctl(netdev->fd, SIOCGIFFLAGS, &ifr) < 0) {
692         VLOG_ERR("ioctl(SIOCGIFFLAGS) on %s device failed: %s",
693                  netdev->name, strerror(errno));
694         return errno;
695     }
696     *flags = ifr.ifr_flags;
697     return 0;
698 }
699
700 static int
701 set_flags(struct netdev *netdev, int flags)
702 {
703     struct ifreq ifr;
704     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
705     ifr.ifr_flags = flags;
706     if (ioctl(netdev->fd, SIOCSIFFLAGS, &ifr) < 0) {
707         VLOG_ERR("ioctl(SIOCSIFFLAGS) on %s device failed: %s",
708                  netdev->name, strerror(errno));
709         return errno;
710     }
711     return 0;
712 }