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